2 * Attempt to verify either one cert file, or every file in cwd,
3 * with specified issuer cert. Used to study vulnerability to
4 * NISCC cert DOS attacks.
6 #include <Security/Security.h>
7 #include <Security/cuFileIo.h>
8 #include <Security/cuCdsaUtils.h>
14 static void usage(char **argv
)
16 printf("usage: %s [-v(erbose)] issuerCertFile [certFile]\n", argv
[0]);
21 * Known file names to NOT parse
23 static const char *skipTheseFiles
[] =
25 /* standard entries */
33 /* returns false if specified fileName is in skipTheseFiles[] */
34 static bool shouldWeParse(
35 const char *fileName
) // C string
37 for(const char **stf
=skipTheseFiles
; *stf
!=NULL
; stf
++) {
38 const char *tf
= *stf
;
39 if(!strcmp(fileName
, *stf
)) {
47 * Just try to verify. Returns true on any reasonable outcome.
50 CSSM_CL_HANDLE clHand
,
51 CSSM_CC_HANDLE ccHand
,
52 const unsigned char *certData
,
56 CSSM_DATA cdata
= {certDataLen
, (uint8
*)certData
};
59 crtn
= CSSM_CL_CertVerifyWithKey(clHand
, ccHand
, &cdata
);
61 /* hard-coded list of acceptable outcomes */
68 case CSSMERR_CL_VERIFICATION_FAILURE
:
73 case CSSMERR_CL_UNKNOWN_FORMAT
:
79 cuPrintError("CSSM_CL_CertVerifyWithKey", crtn
);
85 int main(int argc
, char **argv
)
88 unsigned char *certData
;
90 unsigned char *issuerData
;
91 unsigned issuerDataLen
;
97 if((argc
< 2 ) || (argc
> 4)) {
100 if(argv
[1][0] == '-') {
112 CSSM_CSP_HANDLE cspHand
= cuCspStartup(CSSM_TRUE
);
113 CSSM_CL_HANDLE clHand
= cuClStartup();
114 if((cspHand
== 0) || (clHand
== 0)) {
118 /* read issuer cert, extract its public key for quick verify */
119 char *fn
= argv
[filearg
++];
120 if(readFile(fn
, &issuerData
, &issuerDataLen
)) {
121 printf("\n***Error reading file %s. Aborting.\n", fn
);
124 CSSM_DATA issuerCert
= {issuerDataLen
, issuerData
};
125 CSSM_KEY_PTR issuerPubKey
;
126 CSSM_RETURN crtn
= CSSM_CL_CertGetKeyInfo(clHand
, &issuerCert
,
129 cuPrintError("CSSM_CL_CertGetKeyInfo", crtn
);
133 /* a reusable signature context */
134 CSSM_CC_HANDLE ccHand
;
135 crtn
= CSSM_CSP_CreateSignatureContext(cspHand
,
136 CSSM_ALGID_SHA1WithRSA
,
141 cuPrintError("CSSM_CSP_CreateSignatureContext", crtn
);
146 /* read & parse one file */
147 char *fn
= argv
[filearg
++];
149 printf("...reading %s\n", fn
);
151 if(readFile(fn
, &certData
, &certDataLen
)) {
152 printf("\n***Error reading file %s. Aborting.\n", fn
);
155 if(!vfyCert(clHand
, ccHand
, certData
, certDataLen
, verbose
)) {
156 printf("\n***GOT AN EXCEPTION ON %s\n", fn
);
163 printf("Huh? Can't open . as a directory.\n");
168 char filename
[MAXNAMLEN
+ 1];
169 memmove(filename
, de
->d_name
, de
->d_namlen
);
170 filename
[de
->d_namlen
] = '\0';
171 if(shouldWeParse(filename
)) {
173 printf("...%s", filename
);
176 if(readFile(filename
, &certData
, &certDataLen
)) {
177 printf("\n***Error reading file %s. Aborting.\n", filename
);
180 if(!vfyCert(clHand
, ccHand
, certData
, certDataLen
, verbose
)) {
181 printf("\n***GOT AN EXCEPTION ON %s\n", filename
);
190 printf("\nisccCertVerify did not crash.\n");