1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * test_customcrlchecker.c
40 * Test Custom CRL Checking
45 #include "testutil_nss.h"
47 #define PKIX_TEST_MAX_CERTS 10
48 #define PKIX_TEST_COLLECTIONCERTSTORE_NUM_CRLS 5
50 void *plContext
= NULL
;
51 char *dirName
= NULL
; /* also used in callback */
53 void printUsage1(char *pName
){
54 printf("\nUSAGE: %s test-purpose [ENE|EE] ", pName
);
55 printf("cert [certs].\n");
58 void printUsageMax(PKIX_UInt32 numCerts
){
59 printf("\nUSAGE ERROR: number of certs %d exceed maximum %d\n",
60 numCerts
, PKIX_TEST_MAX_CERTS
);
65 PKIX_CertStore
*store
,
66 PKIX_CRLSelector
*crlSelector
,
71 char *crlFileNames
[] = {"chem.crl",
77 PKIX_PL_CRL
*crl
= NULL
;
78 PKIX_List
*crlList
= NULL
;
83 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&crlList
, plContext
));
85 while (crlFileNames
[i
]) {
87 crl
= createCRL(dirName
, crlFileNames
[i
++], plContext
);
91 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
92 (crlList
, (PKIX_PL_Object
*)crl
, plContext
));
94 PKIX_TEST_DECREF_BC(crl
);
104 return (0); /* this function is called by libpkix */
110 PKIX_CertStore
*store
,
111 PKIX_CRLSelector
*crlSelector
,
113 PKIX_List
**pCrlList
,
121 PKIX_CertStore
*store
,
122 PKIX_CertSelector
*certSelector
,
132 PKIX_CertStore
*store
,
133 PKIX_CertSelector
*certSelector
,
142 testCRLSelectorMatchCallback(
143 PKIX_CRLSelector
*selector
,
147 PKIX_ComCRLSelParams
*comCrlSelParams
= NULL
;
148 PKIX_List
*issuerList
= NULL
;
149 PKIX_PL_X500Name
*issuer
= NULL
;
150 PKIX_PL_X500Name
*crlIssuer
= NULL
;
151 PKIX_UInt32 numIssuers
= 0;
153 PKIX_Boolean result
= PKIX_FALSE
;
154 PKIX_Error
*error
= NULL
;
155 PKIX_PL_String
*textString
= NULL
;
156 char *errorText
= "Not an error, CRL Select mismatch";
158 PKIX_TEST_STD_VARS();
160 subTest("Custom_Selector_MatchCallback");
162 if (selector
!= NULL
) {
163 PKIX_TEST_EXPECT_NO_ERROR
164 (PKIX_CRLSelector_GetCommonCRLSelectorParams
165 (selector
, &comCrlSelParams
, plContext
));
169 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CRL_GetIssuer
170 (crl
, &crlIssuer
, plContext
));
173 if (comCrlSelParams
!= NULL
) {
174 PKIX_TEST_EXPECT_NO_ERROR
175 (PKIX_ComCRLSelParams_GetIssuerNames
176 (comCrlSelParams
, &issuerList
, plContext
));
179 if (issuerList
!= NULL
) {
181 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
182 (issuerList
, &numIssuers
, plContext
));
184 for (i
= 0; i
< numIssuers
; i
++){
186 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem
188 i
, (PKIX_PL_Object
**)&issuer
,
191 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals
192 ((PKIX_PL_Object
*)crlIssuer
,
193 (PKIX_PL_Object
*)issuer
,
197 if (result
!= PKIX_TRUE
) {
201 if (i
== numIssuers
-1) {
203 PKIX_TEST_EXPECT_NO_ERROR
204 (PKIX_PL_String_Create
211 PKIX_TEST_EXPECT_NO_ERROR
220 PKIX_TEST_DECREF_AC(issuer
);
225 PKIX_TEST_DECREF_AC(issuer
);
232 PKIX_TEST_DECREF_AC(comCrlSelParams
);
233 PKIX_TEST_DECREF_AC(crlIssuer
);
234 PKIX_TEST_DECREF_AC(issuer
);
235 PKIX_TEST_DECREF_AC(issuerList
);
236 PKIX_TEST_DECREF_AC(textString
);
245 testAddIssuerName(PKIX_ComCRLSelParams
*comCrlSelParams
, char *issuerName
)
247 PKIX_PL_String
*issuerString
= NULL
;
248 PKIX_PL_X500Name
*issuer
= NULL
;
249 PKIX_UInt32 length
= 0;
251 PKIX_TEST_STD_VARS();
253 subTest("PKIX_ComCRLSelParams_AddIssuerName");
255 length
= PL_strlen(issuerName
);
257 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
264 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_X500Name_Create(issuerString
,
268 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCRLSelParams_AddIssuerName
269 (comCrlSelParams
, issuer
, plContext
));
273 PKIX_TEST_DECREF_AC(issuerString
);
274 PKIX_TEST_DECREF_AC(issuer
);
282 testCustomCertStore(PKIX_ValidateParams
*valParams
)
284 PKIX_CertStore_CRLCallback crlCallback
;
285 PKIX_CertStore
*certStore
= NULL
;
286 PKIX_ProcessingParams
*procParams
= NULL
;
287 char *issuerName1
= "cn=science,o=mit,c=us";
288 char *issuerName2
= "cn=physics,o=mit,c=us";
289 char *issuerName3
= "cn=prof noall,o=mit,c=us";
290 char *issuerName4
= "cn=testing CRL,o=test,c=us";
291 PKIX_ComCRLSelParams
*comCrlSelParams
= NULL
;
292 PKIX_CRLSelector
*crlSelector
= NULL
;
293 PKIX_List
*crlList
= NULL
;
294 PKIX_UInt32 numCrl
= 0;
295 void *nbioContext
= NULL
;
297 PKIX_TEST_STD_VARS();
299 subTest("PKIX_PL_CollectionCertStore_Create");
301 /* Create CRLSelector, link in CollectionCertStore */
303 subTest("PKIX_ComCRLSelParams_AddIssuerNames");
305 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ComCRLSelParams_Create
306 (&comCrlSelParams
, plContext
));
309 testAddIssuerName(comCrlSelParams
, issuerName1
);
310 testAddIssuerName(comCrlSelParams
, issuerName2
);
311 testAddIssuerName(comCrlSelParams
, issuerName3
);
312 testAddIssuerName(comCrlSelParams
, issuerName4
);
315 subTest("PKIX_CRLSelector_SetCommonCRLSelectorParams");
316 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CRLSelector_Create
317 (testCRLSelectorMatchCallback
,
322 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CRLSelector_SetCommonCRLSelectorParams
323 (crlSelector
, comCrlSelParams
, plContext
));
325 /* Create CertStore, link in CRLSelector */
327 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateParams_GetProcessingParams
328 (valParams
, &procParams
, plContext
));
330 subTest("PKIX_CertStore_Create");
331 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_Create
336 NULL
, /* trustCallback */
337 (PKIX_PL_Object
*)crlSelector
, /* fake */
338 PKIX_FALSE
, /* cacheFlag */
339 PKIX_TRUE
, /* localFlag */
344 subTest("PKIX_ProcessingParams_AddCertStore");
345 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_AddCertStore
346 (procParams
, certStore
, plContext
));
348 subTest("PKIX_ProcessingParams_SetRevocationEnabled");
349 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ProcessingParams_SetRevocationEnabled
350 (procParams
, PKIX_TRUE
, plContext
));
352 subTest("PKIX_CertStore_GetCRLCallback");
353 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCRLCallback
358 subTest("Getting CRL by CRL Callback");
359 PKIX_TEST_EXPECT_NO_ERROR(crlCallback
366 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
371 if (numCrl
!= PKIX_TEST_COLLECTIONCERTSTORE_NUM_CRLS
) {
372 pkixTestErrorMsg
= "unexpected CRL number mismatch";
377 PKIX_TEST_DECREF_AC(crlList
);
378 PKIX_TEST_DECREF_AC(comCrlSelParams
);
379 PKIX_TEST_DECREF_AC(crlSelector
);
380 PKIX_TEST_DECREF_AC(procParams
);
381 PKIX_TEST_DECREF_AC(certStore
);
389 * Validate Certificate Chain with Certificate Revocation List
390 * Certificate Chain is built based on input certs' sequence.
391 * CRL is fetched from the directory specified in CollectionCertStore.
392 * while CollectionCertStore is linked in CertStore Object which then
393 * linked in ProcessParam. During validation, CRLChecker will invoke
394 * the crlCallback (this test uses PKIX_PL_CollectionCertStore_GetCRL)
395 * to get CRL data for revocation check.
396 * This test set criteria in CRLSelector which is linked in
397 * CommonCRLSelectorParam. When CRL data is fetched into cache for
398 * revocation check, CRL's are filtered based on the criteria set.
401 int main(int argc
, char *argv
[]){
403 PKIX_List
*chain
= NULL
;
404 PKIX_ValidateParams
*valParams
= NULL
;
405 PKIX_ValidateResult
*valResult
= NULL
;
406 PKIX_UInt32 actualMinorVersion
;
407 char *certNames
[PKIX_TEST_MAX_CERTS
];
408 PKIX_PL_Cert
*certs
[PKIX_TEST_MAX_CERTS
];
409 PKIX_VerifyNode
*verifyTree
= NULL
;
410 PKIX_PL_String
*verifyString
= NULL
;
411 PKIX_UInt32 chainLength
= 0;
414 PKIX_Boolean testValid
= PKIX_TRUE
;
415 PKIX_Boolean useArenas
= PKIX_FALSE
;
416 char *anchorName
= NULL
;
418 PKIX_TEST_STD_VARS();
421 printUsage1(argv
[0]);
425 startTests("CRL Checker");
427 useArenas
= PKIX_TEST_ARENAS_ARG(argv
[1]);
429 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
430 (PKIX_TRUE
, /* nssInitNeeded */
438 /* ENE = expect no error; EE = expect error */
439 if (PORT_Strcmp(argv
[2+j
], "ENE") == 0) {
440 testValid
= PKIX_TRUE
;
441 } else if (PORT_Strcmp(argv
[2+j
], "EE") == 0) {
442 testValid
= PKIX_FALSE
;
444 printUsage1(argv
[0]);
448 chainLength
= (argc
- j
) - 5;
449 if (chainLength
> PKIX_TEST_MAX_CERTS
) {
450 printUsageMax(chainLength
);
453 for (i
= 0; i
< chainLength
; i
++) {
455 certNames
[i
] = argv
[(5 + j
) +i
];
463 subTest("Custom-CRL-Checker - Create Cert Chain");
465 chain
= createCertChainPlus
466 (dirName
, certNames
, certs
, chainLength
, plContext
);
468 subTest("Custom-CRL-Checker - Create Params");
470 anchorName
= argv
[4+j
];
472 valParams
= createValidateParams
485 subTest("Custom-CRL-Checker - Set Processing Params for CertStore");
487 testCustomCertStore(valParams
);
489 subTest("Custom-CRL-Checker - Validate Chain");
491 if (testValid
== PKIX_TRUE
) {
492 PKIX_TEST_EXPECT_NO_ERROR(PKIX_ValidateChain
493 (valParams
, &valResult
, &verifyTree
, plContext
));
495 PKIX_TEST_EXPECT_ERROR(PKIX_ValidateChain
496 (valParams
, &valResult
, &verifyTree
, plContext
));
501 PKIX_TEST_DECREF_AC(verifyString
);
502 PKIX_TEST_DECREF_AC(verifyTree
);
503 PKIX_TEST_DECREF_AC(chain
);
504 PKIX_TEST_DECREF_AC(valParams
);
505 PKIX_TEST_DECREF_AC(valResult
);
507 PKIX_Shutdown(plContext
);
511 endTests("CRL Checker");