tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_validatechain_NB.c (11338B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 /*
      5 * test_validatechain_NB.c
      6 *
      7 * Test ValidateChain (nonblocking I/O) function
      8 *
      9 */
     10 
     11 #include "testutil.h"
     12 #include "testutil_nss.h"
     13 
     14 static void *plContext = NULL;
     15 
     16 static void
     17 printUsage(void)
     18 {
     19    (void)printf("\nUSAGE:\ntest_validateChain_NB TestName [ENE|EE] "
     20                 "<certStoreDirectory> <trustedCert> <targetCert>\n\n");
     21    (void)printf("Validates a chain of certificates between "
     22                 "<trustedCert> and <targetCert>\n"
     23                 "using the certs and CRLs in <certStoreDirectory>. "
     24                 "If ENE is specified,\n"
     25                 "then an Error is Not Expected. "
     26                 "If EE is specified, an Error is Expected.\n");
     27 }
     28 
     29 static char *
     30 createFullPathName(
     31    char *dirName,
     32    char *certFile,
     33    void *plContext)
     34 {
     35    PKIX_UInt32 certFileLen;
     36    PKIX_UInt32 dirNameLen;
     37    char *certPathName = NULL;
     38 
     39    PKIX_TEST_STD_VARS();
     40 
     41    certFileLen = PL_strlen(certFile);
     42    dirNameLen = PL_strlen(dirName);
     43 
     44    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc(dirNameLen +
     45                                                 certFileLen +
     46                                                 2,
     47                                             (void **)&certPathName,
     48                                             plContext));
     49 
     50    PL_strcpy(certPathName, dirName);
     51    PL_strcat(certPathName, "/");
     52    PL_strcat(certPathName, certFile);
     53    printf("certPathName = %s\n", certPathName);
     54 
     55 cleanup:
     56 
     57    PKIX_TEST_RETURN();
     58 
     59    return (certPathName);
     60 }
     61 
     62 static PKIX_Error *
     63 testSetupCertStore(PKIX_ValidateParams *valParams, char *ldapName)
     64 {
     65    PKIX_PL_String *dirString = NULL;
     66    PKIX_CertStore *certStore = NULL;
     67    PKIX_ProcessingParams *procParams = NULL;
     68    PKIX_PL_LdapDefaultClient *ldapClient = NULL;
     69 
     70    PKIX_TEST_STD_VARS();
     71 
     72    subTest("PKIX_PL_CollectionCertStoreContext_Create");
     73 
     74    /* Create LDAPCertStore */
     75    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_LdapDefaultClient_CreateByName(ldapName,
     76                                                                     0,    /* timeout */
     77                                                                     NULL, /* bindPtr */
     78                                                                     &ldapClient,
     79                                                                     plContext));
     80 
     81    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_LdapCertStore_Create((PKIX_PL_LdapClient *)ldapClient,
     82                                                           &certStore,
     83                                                           plContext));
     84 
     85    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams(valParams, &procParams, plContext));
     86 
     87    subTest("PKIX_ProcessingParams_AddCertStore");
     88    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_AddCertStore(procParams, certStore, plContext));
     89 
     90    subTest("PKIX_ProcessingParams_SetRevocationEnabled");
     91    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled(procParams, PKIX_TRUE, plContext));
     92 
     93 cleanup:
     94 
     95    PKIX_TEST_DECREF_AC(dirString);
     96    PKIX_TEST_DECREF_AC(procParams);
     97    PKIX_TEST_DECREF_AC(certStore);
     98    PKIX_TEST_DECREF_AC(ldapClient);
     99 
    100    PKIX_TEST_RETURN();
    101 
    102    return (0);
    103 }
    104 
    105 static char *levels[] = {
    106    "None", "Fatal Error", "Error", "Warning", "Debug", "Trace"
    107 };
    108 
    109 static PKIX_Error *
    110 loggerCallback(
    111    PKIX_Logger *logger,
    112    PKIX_PL_String *message,
    113    PKIX_UInt32 logLevel,
    114    PKIX_ERRORCLASS logComponent,
    115    void *plContext)
    116 {
    117 #define resultSize 150
    118    char *msg = NULL;
    119    char result[resultSize];
    120 
    121    PKIX_TEST_STD_VARS();
    122 
    123    msg = PKIX_String2ASCII(message, plContext);
    124    PR_snprintf(result, resultSize,
    125                "Logging %s (%s): %s",
    126                levels[logLevel],
    127                PKIX_ERRORCLASSNAMES[logComponent],
    128                msg);
    129    subTest(result);
    130 
    131 cleanup:
    132    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(msg, plContext));
    133    PKIX_TEST_RETURN();
    134 }
    135 
    136 static void
    137 testLogErrors(
    138    PKIX_ERRORCLASS module,
    139    PKIX_UInt32 loggingLevel,
    140    PKIX_List *loggers,
    141    void *plContext)
    142 {
    143    PKIX_Logger *logger = NULL;
    144    PKIX_PL_String *component = NULL;
    145 
    146    PKIX_TEST_STD_VARS();
    147 
    148    PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_Create(loggerCallback, NULL, &logger, plContext));
    149    PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetLoggingComponent(logger, module, plContext));
    150    PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetMaxLoggingLevel(logger, loggingLevel, plContext));
    151    PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem(loggers, (PKIX_PL_Object *)logger, plContext));
    152 
    153 cleanup:
    154    PKIX_TEST_DECREF_AC(logger);
    155    PKIX_TEST_DECREF_AC(component);
    156 
    157    PKIX_TEST_RETURN();
    158 }
    159 
    160 int
    161 test_validatechain_NB(int argc, char *argv[])
    162 {
    163 
    164    PKIX_ValidateParams *valParams = NULL;
    165    PKIX_ValidateResult *valResult = NULL;
    166    PKIX_UInt32 actualMinorVersion;
    167    PKIX_UInt32 j = 0;
    168    PKIX_UInt32 k = 0;
    169    PKIX_UInt32 chainLength = 0;
    170    PKIX_Boolean testValid = PKIX_TRUE;
    171    PKIX_List *chainCerts = NULL;
    172    PKIX_PL_Cert *dirCert = NULL;
    173    char *dirCertName = NULL;
    174    char *anchorCertName = NULL;
    175    char *dirName = NULL;
    176    PKIX_UInt32 certIndex = 0;
    177    PKIX_UInt32 anchorIndex = 0;
    178    PKIX_UInt32 checkerIndex = 0;
    179    PKIX_Boolean revChecking = PKIX_FALSE;
    180    PKIX_List *checkers = NULL;
    181    PRPollDesc *pollDesc = NULL;
    182    PRErrorCode errorCode = 0;
    183    PKIX_PL_Socket *socket = NULL;
    184    char *ldapName = NULL;
    185    PKIX_VerifyNode *verifyTree = NULL;
    186    PKIX_PL_String *verifyString = NULL;
    187 
    188    PKIX_List *loggers = NULL;
    189    PKIX_Logger *logger = NULL;
    190    char *logging = NULL;
    191    PKIX_PL_String *component = NULL;
    192 
    193    PKIX_TEST_STD_VARS();
    194 
    195    if (argc < 5) {
    196        printUsage();
    197        return (0);
    198    }
    199 
    200    startTests("ValidateChain_NB");
    201 
    202    PKIX_TEST_EXPECT_NO_ERROR(
    203        PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
    204 
    205    /* ENE = expect no error; EE = expect error */
    206    if (PORT_Strcmp(argv[2 + j], "ENE") == 0) {
    207        testValid = PKIX_TRUE;
    208    } else if (PORT_Strcmp(argv[2 + j], "EE") == 0) {
    209        testValid = PKIX_FALSE;
    210    } else {
    211        printUsage();
    212        return (0);
    213    }
    214 
    215    subTest(argv[1 + j]);
    216 
    217    dirName = argv[3 + j];
    218 
    219    chainLength = argc - j - 5;
    220 
    221    PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&chainCerts, plContext));
    222 
    223    for (k = 0; k < chainLength; k++) {
    224 
    225        dirCert = createCert(dirName, argv[5 + k + j], plContext);
    226 
    227        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem(chainCerts, (PKIX_PL_Object *)dirCert, plContext));
    228 
    229        PKIX_TEST_DECREF_BC(dirCert);
    230    }
    231 
    232    valParams = createValidateParams(dirName,
    233                                     argv[4 +
    234                                          j],
    235                                     NULL,
    236                                     NULL,
    237                                     NULL,
    238                                     PKIX_FALSE,
    239                                     PKIX_FALSE,
    240                                     PKIX_FALSE,
    241                                     PKIX_FALSE,
    242                                     chainCerts,
    243                                     plContext);
    244 
    245    ldapName = PR_GetEnvSecure("LDAP");
    246    /* Is LDAP set in the environment? */
    247    if ((ldapName == NULL) || (*ldapName == '\0')) {
    248        testError("LDAP not set in environment");
    249        goto cleanup;
    250    }
    251 
    252    pkixTestErrorResult = pkix_pl_Socket_CreateByName(PKIX_FALSE,               /* isServer */
    253                                                      PR_SecondsToInterval(30), /* try 30 secs for connect */
    254                                                      ldapName,
    255                                                      &errorCode,
    256                                                      &socket,
    257                                                      plContext);
    258 
    259    if (pkixTestErrorResult != NULL) {
    260        PKIX_PL_Object_DecRef((PKIX_PL_Object *)pkixTestErrorResult, plContext);
    261        pkixTestErrorResult = NULL;
    262        testError("Unable to connect to LDAP Server");
    263        goto cleanup;
    264    }
    265 
    266    PKIX_TEST_DECREF_BC(socket);
    267 
    268    testSetupCertStore(valParams, ldapName);
    269 
    270    logging = PR_GetEnvSecure("LOGGING");
    271    /* Is LOGGING set in the environment? */
    272    if ((logging != NULL) && (*logging != '\0')) {
    273 
    274        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&loggers, plContext));
    275 
    276        testLogErrors(PKIX_VALIDATE_ERROR, 2, loggers, plContext);
    277        testLogErrors(PKIX_CERTCHAINCHECKER_ERROR, 2, loggers, plContext);
    278        testLogErrors(PKIX_LDAPDEFAULTCLIENT_ERROR, 2, loggers, plContext);
    279        testLogErrors(PKIX_CERTSTORE_ERROR, 2, loggers, plContext);
    280 
    281        PKIX_TEST_EXPECT_NO_ERROR(PKIX_SetLoggers(loggers, plContext));
    282    }
    283 
    284    pkixTestErrorResult = PKIX_ValidateChain_NB(valParams,
    285                                                &certIndex,
    286                                                &anchorIndex,
    287                                                &checkerIndex,
    288                                                &revChecking,
    289                                                &checkers,
    290                                                (void **)&pollDesc,
    291                                                &valResult,
    292                                                &verifyTree,
    293                                                plContext);
    294 
    295    while (pollDesc != NULL) {
    296 
    297        if (PR_Poll(pollDesc, 1, 0) < 0) {
    298            testError("PR_Poll failed");
    299        }
    300 
    301        pkixTestErrorResult = PKIX_ValidateChain_NB(valParams,
    302                                                    &certIndex,
    303                                                    &anchorIndex,
    304                                                    &checkerIndex,
    305                                                    &revChecking,
    306                                                    &checkers,
    307                                                    (void **)&pollDesc,
    308                                                    &valResult,
    309                                                    &verifyTree,
    310                                                    plContext);
    311    }
    312 
    313    if (pkixTestErrorResult) {
    314        if (testValid == PKIX_FALSE) { /* EE */
    315            (void)printf("EXPECTED ERROR RECEIVED!\n");
    316        } else { /* ENE */
    317            testError("UNEXPECTED ERROR RECEIVED");
    318        }
    319        PKIX_TEST_DECREF_BC(pkixTestErrorResult);
    320    } else {
    321 
    322        if (testValid == PKIX_TRUE) { /* ENE */
    323            (void)printf("EXPECTED NON-ERROR RECEIVED!\n");
    324        } else { /* EE */
    325            (void)printf("UNEXPECTED NON-ERROR RECEIVED!\n");
    326        }
    327    }
    328 
    329 cleanup:
    330 
    331    if (verifyTree) {
    332        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)verifyTree, &verifyString, plContext));
    333        (void)printf("verifyTree is\n%s\n",
    334                     verifyString->escAsciiString);
    335    }
    336 
    337    PKIX_TEST_DECREF_AC(verifyString);
    338    PKIX_TEST_DECREF_AC(verifyTree);
    339    PKIX_TEST_DECREF_AC(checkers);
    340    PKIX_TEST_DECREF_AC(chainCerts);
    341    PKIX_TEST_DECREF_AC(valParams);
    342    PKIX_TEST_DECREF_AC(valResult);
    343 
    344    PKIX_Shutdown(plContext);
    345 
    346    PKIX_TEST_RETURN();
    347 
    348    endTests("ValidateChain_NB");
    349 
    350    return (0);
    351 }