9 #include <security_cdsa_utils/cuFileIo.h>
10 #include <utilLib/common.h>
11 #include <clAppUtils/clutils.h>
12 #include <clAppUtils/CertParser.h>
13 #include <Security/Security.h>
14 #include "crlNetwork.h"
15 #include <security_cdsa_utils/cuPrintCert.h>
18 static void usage(char **argv
)
20 printf("usage: %s [options]\n", argv
[0]);
22 printf(" -c certFile -- obtain CRL via net from this cert\n");
23 printf(" -C crlFile -- CRL from this file\n");
24 printf(" -p -- parse the CRL\n");
25 printf(" -o outFile -- write the fetched CRL to this file\n");
26 printf(" -v -- verbose CRL dump\n");
31 static int fetchCrlViaGeneralNames(
32 const CE_GeneralNames
*names
,
33 unsigned char **crl
, // mallocd and RETURNED
34 size_t *crlLen
) // RETURNED
36 CSSM_DATA crlData
= {0, NULL
};
39 for(unsigned nameDex
=0; nameDex
<names
->numNames
; nameDex
++) {
40 CE_GeneralName
*name
= &names
->generalName
[nameDex
];
41 switch(name
->nameType
) {
43 if(name
->name
.Length
< 5) {
46 if(strncmp((char *)name
->name
.Data
, "ldap:", 5) &&
47 strncmp((char *)name
->name
.Data
, "http:", 5) &&
48 strncmp((char *)name
->name
.Data
, "https:", 6)) {
49 /* eventually handle other schemes here */
53 /* OK, we can do this */
54 crtn
= crlNetFetch(&name
->name
, LT_Crl
, &crlData
);
56 printf("...net fetch error\n");
60 *crlLen
= crlData
.Length
;
64 printf("fetchCrlViaGeneralNames: unknown"
65 "nameType (%u)", (unsigned)name
->nameType
);
69 printf("...GNT_URI name not found in GeneralNames\n");
75 unsigned char **crl
, // mallocd and RETURNED
76 size_t *crlLen
) // RETURNED
78 CE_CRLDistPointsSyntax
*dps
= (CE_CRLDistPointsSyntax
*)
79 cert
.extensionForOid(CSSMOID_CrlDistributionPoints
);
84 /* not an error, just indicate NULL return */
85 printf("***No CrlDistributionPoints in this cert.\n");
88 for(unsigned dex
=0; dex
<dps
->numDistPoints
; dex
++) {
90 CE_CRLDistributionPoint
*dp
= &dps
->distPoints
[dex
];
91 if(dp
->distPointName
== NULL
) {
94 switch(dp
->distPointName
->nameType
) {
95 case CE_CDNT_NameRelativeToCrlIssuer
:
96 printf("...CE_CDNT_NameRelativeToCrlIssuer not implemented\n");
99 case CE_CDNT_FullName
:
101 CE_GeneralNames
*names
= dp
->distPointName
->dpn
.fullName
;
102 int rtn
= fetchCrlViaGeneralNames(names
, crl
, crlLen
);
106 /* else try again if there's another name */
108 } /* CE_CDNT_FullName */
112 printf("unknown distPointName->nameType (%u)\n",
113 (unsigned)dp
->distPointName
->nameType
);
115 } /* switch distPointName->nameType */
116 } /* for each distPoints */
117 printf("...CrlDistributionPoints found, but nothing we can use.\n");
121 int main(int argc
, char **argv
)
123 char *certFile
= NULL
;
124 char *crlFile
= NULL
;
125 unsigned char *certData
;
126 unsigned certDataLen
;
127 bool doParse
= false;
128 char *outFile
= NULL
;
129 CSSM_BOOL verbose
= CSSM_FALSE
;
130 unsigned char *crl
= NULL
;
140 while ((arg
= getopt(argc
, argv
, "c:C:po:vh")) != -1) {
164 if((certFile
!= NULL
) && (crlFile
!= NULL
)) {
165 printf("***crlFile and certFile are mutually exclusive.\n");
168 if((certFile
== NULL
) && (crlFile
== NULL
)) {
169 printf("***Must specify either certFile or crlFile\n");
174 CSSM_CL_HANDLE clHand
= clStartup();
175 CertParser
parser(clHand
);
179 if(readFile(crlFile
, &crl
, &len
)) {
180 printf("***Error reading %s. Aborting.\n", crlFile
);
186 if(readFile(certFile
, &certData
, &certDataLen
)) {
187 printf("***Error reading %s. Aborting.\n", certFile
);
190 CSSM_DATA cdata
= {certDataLen
, certData
};
191 crtn
= parser
.initWithData(cdata
);
193 printf("Error parsing cert %s. Aborting.\n", certFile
);
196 rtn
= fetchCrl(parser
, &crl
, &crlLen
);
198 printf("***aborting.\n");
205 printf("...parse specified but no CRL found.\n");
208 if(certFile
!= NULL
) {
209 printf("============== CRL for cert %s ==============\n", certFile
);
211 printCrl(crl
, crlLen
, verbose
);
212 if(certFile
!= NULL
) {
213 printf("============== end of CRL ==============\n");
219 printf("...outFile specified but no CRL found.\n");
222 if(writeFile(outFile
, crl
, crlLen
)) {
223 printf("***Error writing CRL to %s.\n", outFile
);
227 printf("...wrote %u bytes to %s\n", (unsigned)crlLen
, outFile
);