summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/libpkix/pkix_pl/module/test_ekuchecker.c
blob: ccd05a5bf8330ccdcf0f20e53bec568023e168f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * test_ekuchecker.c
 *
 * Test Extend Key Usage Checker
 *
 */

#include "testutil.h"
#include "testutil_nss.h"

#define PKIX_TEST_MAX_CERTS 10

static void *plContext = NULL;

static void
printUsage1(char *pName)
{
    printf("\nUSAGE: %s test-purpose [ENE|EE] ", pName);
    printf("[E]oid[,oid]* <data-dir> cert [certs].\n");
}

static void
printUsageMax(PKIX_UInt32 numCerts)
{
    printf("\nUSAGE ERROR: number of certs %d exceed maximum %d\n",
           numCerts, PKIX_TEST_MAX_CERTS);
}

static PKIX_Error *
testCertSelectorMatchCallback(
    PKIX_CertSelector *selector,
    PKIX_PL_Cert *cert,
    PKIX_Boolean *pResult,
    void *plContext)
{
    *pResult = PKIX_TRUE;

    return (0);
}

static PKIX_Error *
testEkuSetup(
    PKIX_ValidateParams *valParams,
    char *ekuOidString,
    PKIX_Boolean *only4EE)
{
    PKIX_ProcessingParams *procParams = NULL;
    PKIX_List *ekuList = NULL;
    PKIX_PL_OID *ekuOid = NULL;
    PKIX_ComCertSelParams *selParams = NULL;
    PKIX_CertSelector *certSelector = NULL;
    PKIX_Boolean last_token = PKIX_FALSE;
    PKIX_UInt32 i, tokeni;

    PKIX_TEST_STD_VARS();

    subTest("PKIX_ValidateParams_GetProcessingParams");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams(valParams, &procParams, plContext));

    /* Get extended key usage OID(s) from command line, separated by ","  */

    if (ekuOidString[0] == '"') {
        /* erase doble quotes, if any */
        i = 1;
        while (ekuOidString[i] != '"' && ekuOidString[i] != '\0') {
            ekuOidString[i - 1] = ekuOidString[i];
            i++;
        }
        ekuOidString[i - 1] = '\0';
    }

    if (ekuOidString[0] == '\0') {
        ekuList = NULL;
    } else {

        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&ekuList, plContext));

        /* if OID string start with E, only check for last cert */
        if (ekuOidString[0] == 'E') {
            *only4EE = PKIX_TRUE;
            tokeni = 2;
            i = 1;
        } else {
            *only4EE = PKIX_FALSE;
            tokeni = 1;
            i = 0;
        }

        while (last_token != PKIX_TRUE) {
            while (ekuOidString[tokeni] != ',' &&
                   ekuOidString[tokeni] != '\0') {
                tokeni++;
            }
            if (ekuOidString[tokeni] == '\0') {
                last_token = PKIX_TRUE;
            } else {
                ekuOidString[tokeni] = '\0';
                tokeni++;
            }

            PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create(&ekuOidString[i], &ekuOid, plContext));

            PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem(ekuList, (PKIX_PL_Object *)ekuOid, plContext));

            PKIX_TEST_DECREF_BC(ekuOid);
            i = tokeni;
        }
    }

    /* Set extended key usage link to processing params */

    subTest("PKIX_ComCertSelParams_Create");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_Create(&selParams, plContext));

    subTest("PKIX_ComCertSelParams_SetExtendedKeyUsage");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCertSelParams_SetExtendedKeyUsage(selParams, ekuList, plContext));

    subTest("PKIX_CertSelector_Create");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create(testCertSelectorMatchCallback,
                                                       NULL,
                                                       &certSelector,
                                                       plContext));

    subTest("PKIX_CertSelector_SetCommonCertSelectorParams");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_SetCommonCertSelectorParams(certSelector, selParams, plContext));

    subTest("PKIX_ProcessingParams_SetTargetCertConstraints");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetTargetCertConstraints(procParams, certSelector, plContext));

cleanup:

    PKIX_TEST_DECREF_AC(selParams);
    PKIX_TEST_DECREF_AC(certSelector);
    PKIX_TEST_DECREF_AC(procParams);
    PKIX_TEST_DECREF_AC(ekuOid);
    PKIX_TEST_DECREF_AC(ekuList);

    PKIX_TEST_RETURN();

    return (0);
}

static PKIX_Error *
testEkuChecker(
    PKIX_ValidateParams *valParams,
    PKIX_Boolean only4EE)
{
    PKIX_ProcessingParams *procParams = NULL;

    PKIX_TEST_STD_VARS();

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams(valParams, &procParams, plContext));

    subTest("PKIX_ProcessingParams_SetRevocationEnabled - disable");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled(procParams, PKIX_FALSE, plContext));

    if (only4EE == PKIX_FALSE) {
        subTest("PKIX_PL_EkuChecker_Create");
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_EkuChecker_Create(procParams, plContext));
    }

cleanup:

    PKIX_TEST_DECREF_AC(procParams);

    PKIX_TEST_RETURN();

    return (0);
}

int
test_ekuchecker(int argc, char *argv[])
{
    PKIX_List *chain = NULL;
    PKIX_ValidateParams *valParams = NULL;
    PKIX_ValidateResult *valResult = NULL;
    PKIX_UInt32 actualMinorVersion;
    char *certNames[PKIX_TEST_MAX_CERTS];
    char *dirName = NULL;
    PKIX_PL_Cert *certs[PKIX_TEST_MAX_CERTS];
    PKIX_UInt32 chainLength = 0;
    PKIX_UInt32 i = 0;
    PKIX_UInt32 j = 0;
    PKIX_Boolean testValid = PKIX_FALSE;
    PKIX_Boolean only4EE = PKIX_FALSE;

    PKIX_TEST_STD_VARS();

    if (argc < 5) {
        printUsage1(argv[0]);
        return (0);
    }

    startTests("EKU Checker");

    PKIX_TEST_EXPECT_NO_ERROR(
        PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));

    /* ENE = expect no error; EE = expect error */
    if (PORT_Strcmp(argv[2 + j], "ENE") == 0) {
        testValid = PKIX_TRUE;
    } else if (PORT_Strcmp(argv[2 + j], "EE") == 0) {
        testValid = PKIX_FALSE;
    } else {
        printUsage1(argv[0]);
        return (0);
    }

    dirName = argv[4 + j];

    chainLength = (argc - j) - 6;
    if (chainLength > PKIX_TEST_MAX_CERTS) {
        printUsageMax(chainLength);
    }

    for (i = 0; i < chainLength; i++) {

        certNames[i] = argv[6 + i + j];
        certs[i] = NULL;
    }

    subTest(argv[1 + j]);

    subTest("Extended-Key-Usage-Checker");

    subTest("Extended-Key-Usage-Checker - Create Cert Chain");

    chain = createCertChainPlus(dirName, certNames, certs, chainLength, plContext);

    subTest("Extended-Key-Usage-Checker - Create Params");

    valParams = createValidateParams(dirName,
                                     argv[5 +
                                          j],
                                     NULL,
                                     NULL,
                                     NULL,
                                     PKIX_FALSE,
                                     PKIX_FALSE,
                                     PKIX_FALSE,
                                     PKIX_FALSE,
                                     chain,
                                     plContext);

    subTest("Default CertStore");

    testEkuSetup(valParams, argv[3 + j], &only4EE);

    testEkuChecker(valParams, only4EE);

    subTest("Extended-Key-Usage-Checker - Validate Chain");

    if (testValid == PKIX_TRUE) {
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateChain(valParams, &valResult, NULL, plContext));
    } else {
        PKIX_TEST_EXPECT_ERROR(PKIX_ValidateChain(valParams, &valResult, NULL, plContext));
    }

cleanup:

    PKIX_TEST_DECREF_AC(chain);
    PKIX_TEST_DECREF_AC(valParams);
    PKIX_TEST_DECREF_AC(valResult);

    PKIX_Shutdown(plContext);

    PKIX_TEST_RETURN();

    endTests("EKU Checker");

    return (0);
}