2 * certcrl - generic cert/CRL verifier
4 #include <security_cdsa_utils/cuFileIo.h>
5 #include <utilLib/common.h>
6 #include <clAppUtils/clutils.h>
10 #include <Security/cssm.h>
11 #include <clAppUtils/BlobList.h>
12 #include <clAppUtils/certVerify.h>
15 static void usage(char **argv
)
17 printf("Usage: %s [options]\n", argv
[0]);
19 printf(" -c certFileName [...]\n");
20 printf(" -C rootCertFileName [...]\n");
21 printf(" -r crlFileName [...]\n");
22 printf(" -d certDbName\n");
23 printf(" -D crlDlDbName\n");
24 printf(" -s (use system anchor certs)\n");
25 printf(" -g (use Trust Settings)\n");
26 printf(" -i (implicit anchors)\n");
27 printf(" -l=loopCount (default = 1)\n");
28 printf(" -f (leaf cert is a CA)\n");
29 printf(" -w(rite CRLs to dlDbName)\n");
30 printf("Policy options:\n");
31 printf(" -y ssl|smime|swuSign|codeSign|pkgSign|resourceSign|iChat|pkinitServer|\n"
32 " pkinitClient|IPSec\n");
33 printf(" -h sslHostName (implies SSL policy; default is basic)\n");
34 printf(" -t SSL client side (implies SSL policy, default is server side)\n");
35 printf(" -E senderEmail (implies SMIME policy unless iChat is specified)\n");
36 printf("Revocation options:\n");
37 printf(" -R revocationPolicy (crl|ocsp|both|none); default = none\n");
38 printf(" -a (allow certs unverified by CRL or OCSP)\n");
39 printf(" -A (require CRL verification if present in cert\n");
40 printf(" -4 (require CRL verification for all certs)\n");
41 printf(" -Q (require OCSP if present in cert)\n");
42 printf(" -5 (require OCSP verification for all certs)\n");
43 printf(" -u responderURI\n");
44 printf(" -U responderCert\n");
45 printf(" -H (OCSP cache disable)\n");
46 printf(" -W (network OCSP disable)\n");
47 printf(" -o generate OCSP nonce\n");
48 printf(" -O require nonce in OCSP response\n");
49 printf("Misc. options:\n");
50 printf(" -n (no network fetch of CRLs)\n");
51 printf(" -N (no network fetch of certs)\n");
52 printf(" -k keyUsage (In HEX starting with 0x)\n");
53 printf(" -T verifyTime (in CSSM_TIMESTRING format, like 20041217154316)\n");
54 printf(" -e=expectedError (default is CSSM_OK)\n");
55 printf(" -S scriptFile\n");
56 printf(" -p (print script variable names)\n");
57 printf(" -P (pause after each script test)\n");
58 printf(" -v (verbose)\n");
59 printf(" -q (quiet)\n");
60 printf(" -L (silent)\n");
66 /* add files named by successive items in argv to blobList, up until the
68 static void gatherFiles(
74 if((currArg
== argc
) || (argv
[currArg
][0] == '-')) {
75 /* need at least one file name */
79 char *argp
= argv
[currArg
];
81 /* done with this file list */
85 int rtn
= blobList
.addFile(argv
[currArg
]);
95 int main(int argc
, char **argv
)
101 CSSM_DL_HANDLE dlHand
;
105 CSSM_DL_DB_HANDLE_PTR crlDbHandPtr
= NULL
;
106 CSSM_DL_DB_LIST dlDbList
;
107 CSSM_DL_DB_HANDLE dlDbHandles
[2];
109 CSSM_RETURN silent
= CSSM_FALSE
;
110 CSSM_BOOL scriptPause
= CSSM_FALSE
;
112 CertVerifyArgs vfyArgs
;
113 memset(&vfyArgs
, 0, sizeof(vfyArgs
));
115 vfyArgs
.version
= CERT_VFY_ARGS_VERS
;
116 vfyArgs
.certs
= &certs
;
117 vfyArgs
.roots
= &roots
;
118 vfyArgs
.crls
= &crls
;
120 /* for historical reasons the defaults for these are true */
121 vfyArgs
.crlNetFetchEnable
= CSSM_TRUE
;
122 vfyArgs
.certNetFetchEnable
= CSSM_TRUE
;
124 /* user-specd variables */
126 const char *crlDbName
= NULL
;
127 const char *certDbName
= NULL
;
128 char *scriptFile
= NULL
;
133 for(arg
=1; arg
<argc
; arg
++) {
140 loops
= atoi(&argp
[3]);
144 gatherFiles(crls
, argv
, argc
, arg
);
148 gatherFiles(certs
, argv
, argc
, arg
);
152 gatherFiles(roots
, argv
, argc
, arg
);
155 vfyArgs
.verbose
= CSSM_TRUE
;
158 vfyArgs
.quiet
= CSSM_TRUE
;
161 vfyArgs
.useSystemAnchors
= CSSM_TRUE
;
164 vfyArgs
.useTrustSettings
= CSSM_TRUE
;
167 vfyArgs
.implicitAnchors
= CSSM_TRUE
;
170 vfyArgs
.allowUnverified
= CSSM_TRUE
;
173 vfyArgs
.expectedErrStr
= &argp
[3];
176 vfyArgs
.crlNetFetchEnable
= CSSM_FALSE
;
179 vfyArgs
.certNetFetchEnable
= CSSM_FALSE
;
182 vfyArgs
.leafCertIsCA
= CSSM_TRUE
;
189 certDbName
= argv
[arg
];
196 crlDbName
= argv
[arg
];
203 scriptFile
= argv
[arg
];
210 vfyArgs
.sslHost
= argv
[arg
];
211 vfyArgs
.vfyPolicy
= CVP_SSL
;
218 if(vfyArgs
.vfyPolicy
== CVP_Basic
) {
219 /* user hasn't specified; now default to SMIME - still
220 * can override (e.g., for iChat) */
221 vfyArgs
.vfyPolicy
= CVP_SMIME
;
223 vfyArgs
.senderEmail
= argv
[arg
];
230 vfyArgs
.intendedKeyUse
= hexToBin(argv
[arg
]);
233 vfyArgs
.sslClient
= CSSM_TRUE
;
234 vfyArgs
.vfyPolicy
= CVP_SSL
;
242 if(parsePolicyString(argp
, &vfyArgs
.vfyPolicy
)) {
243 printf("Bogus policyValue (%s)\n", argp
);
244 printPolicyStrings();
254 if(!strcmp(argp
, "none")) {
255 vfyArgs
.revokePolicy
= CRP_None
;
257 else if(!strcmp(argp
, "crl")) {
258 vfyArgs
.revokePolicy
= CRP_CRL
;
260 else if(!strcmp(argp
, "ocsp")) {
261 vfyArgs
.revokePolicy
= CRP_OCSP
;
263 else if(!strcmp(argp
, "both")) {
264 vfyArgs
.revokePolicy
= CRP_CRL_OCSP
;
275 vfyArgs
.responderURI
= argv
[arg
];
276 /* no implied policy yet - could be CRP_OCSP or CRP_CRL_OCSP */
279 if(readFile(argv
[arg
], (unsigned char **)vfyArgs
.responderCert
,
280 &vfyArgs
.responderCertLen
)) {
281 printf("***Error reading responderCert from %s. Aborting.\n",
285 /* no implied policy yet - could be CRP_OCSP or CRP_CRL_OCSP */
288 vfyArgs
.disableCache
= CSSM_TRUE
;
291 vfyArgs
.disableOcspNet
= CSSM_TRUE
;
294 vfyArgs
.requireOcspIfPresent
= CSSM_TRUE
;
297 vfyArgs
.requireOcspForAll
= CSSM_TRUE
;
300 vfyArgs
.generateOcspNonce
= CSSM_TRUE
;
303 vfyArgs
.requireOcspRespNonce
= CSSM_TRUE
;
306 vfyArgs
.requireCrlIfPresent
= CSSM_TRUE
;
309 vfyArgs
.requireCrlForAll
= CSSM_TRUE
;
316 vfyArgs
.vfyTime
= argv
[arg
];
322 scriptPause
= CSSM_TRUE
;
325 silent
= CSSM_TRUE
; // inhibits start banner
326 vfyArgs
.quiet
= CSSM_TRUE
; // inhibits stdout from certVerify
333 if((vfyArgs
.responderCert
!= NULL
) || (vfyArgs
.responderURI
!= NULL
)) {
334 switch(vfyArgs
.revokePolicy
) {
336 vfyArgs
.revokePolicy
= CRP_OCSP
;
342 printf("*** OCSP options (responderURI, responderCert) only valid "
343 "with OCSP policy\n");
348 vfyArgs
.clHand
= clStartup();
349 if(vfyArgs
.clHand
== CSSM_INVALID_HANDLE
) {
352 vfyArgs
.tpHand
= tpStartup();
353 if(vfyArgs
.tpHand
== CSSM_INVALID_HANDLE
) {
356 vfyArgs
.cspHand
= cspStartup();
357 if(vfyArgs
.cspHand
== CSSM_INVALID_HANDLE
) {
360 dlHand
= dlStartup();
361 if(dlHand
== CSSM_INVALID_HANDLE
) {
366 testStartBanner("certcrl", argc
, argv
);
371 vars
.allowUnverified
= vfyArgs
.allowUnverified
;
372 vars
.requireCrlIfPresent
= vfyArgs
.requireCrlIfPresent
;
373 vars
.requireOcspIfPresent
= vfyArgs
.requireOcspIfPresent
;
374 vars
.crlNetFetchEnable
= vfyArgs
.crlNetFetchEnable
;
375 vars
.certNetFetchEnable
= vfyArgs
.certNetFetchEnable
;
376 vars
.useSystemAnchors
= vfyArgs
.useSystemAnchors
;
377 vars
.useTrustSettings
= vfyArgs
.useTrustSettings
;
378 vars
.leafCertIsCA
= vfyArgs
.leafCertIsCA
;
379 vars
.cacheDisable
= vfyArgs
.disableCache
;
380 vars
.ocspNetFetchDisable
= vfyArgs
.disableOcspNet
;
381 vars
.requireCrlForAll
= vfyArgs
.requireCrlForAll
;
382 vars
.requireOcspForAll
= vfyArgs
.requireOcspForAll
;
383 return runScript(scriptFile
, vfyArgs
.tpHand
, vfyArgs
.clHand
,
384 vfyArgs
.cspHand
, dlHand
,
385 &vars
, vfyArgs
.quiet
, vfyArgs
.verbose
, scriptPause
);
388 /* open DlDbs if enabled */
389 dlDbList
.NumHandles
= 0;
390 dlDbList
.DLDBHandle
= &dlDbHandles
[0];
391 dlDbList
.DLDBHandle
[0].DLHandle
= dlHand
;
392 dlDbList
.DLDBHandle
[1].DLHandle
= dlHand
;
393 if(certDbName
!= NULL
) {
394 crtn
= CSSM_DL_DbOpen(dlHand
,
398 NULL
, // CSSM_ACCESS_CREDENTIALS *AccessCred
399 NULL
, // void *OpenParameters
400 &dlDbList
.DLDBHandle
[0].DBHandle
);
402 printError("CSSM_DL_DbOpen", crtn
);
403 printf("***Error opening DB %s. Aborting.\n", certDbName
);
406 dlDbList
.NumHandles
++;
407 vfyArgs
.dlDbList
= &dlDbList
;
409 if(crlDbName
!= NULL
) {
410 vfyArgs
.crlDlDb
= &dlDbList
.DLDBHandle
[dlDbList
.NumHandles
];
411 crtn
= CSSM_DL_DbOpen(dlHand
,
414 CSSM_DB_ACCESS_READ
| CSSM_DB_ACCESS_WRITE
,
415 NULL
, // CSSM_ACCESS_CREDENTIALS *AccessCred
416 NULL
, // void *OpenParameters
417 &crlDbHandPtr
->DBHandle
);
419 printError("CSSM_DL_DbOpen", crtn
);
420 printf("***Error opening DB %s. Aborting.\n", crlDbName
);
423 dlDbList
.NumHandles
++;
424 vfyArgs
.dlDbList
= &dlDbList
;
426 for(loop
=0; loop
<loops
; loop
++) {
427 rtn
= certVerify(&vfyArgs
);
434 printf("CR to continue, q to quit: ");