2 * extenGrab - write the unparsed extension blobs of a specified
3 * cert to files for external examination
5 #include <Security/SecAsn1Coder.h>
6 #include <Security/X509Templates.h>
7 #include <Security/cssmapple.h>
11 #include <security_cdsa_utils/cuOidParser.h>
12 #include <security_cdsa_utils/cuFileIo.h>
14 static void usage(char **argv
)
16 printf("Usage: %s certFile outFileBase [r for CRL, default is cert]\n",
22 * How many items in a NULL-terminated array of pointers?
24 static unsigned nssArraySize(
36 int main(int argc
, char **argv
)
43 NSS_Certificate signedCert
;
46 const SecAsn1Template
*templ
;
47 NSS_CertExtension
***extenp
;
49 for(int arg
=3; arg
<argc
; arg
++) {
50 switch(argv
[arg
][0]) {
52 doCert
= false; // i.e. CRL
60 memset(&signedCert
, 0, sizeof(signedCert
));
61 decodeTarget
= &signedCert
;
62 templ
= kSecAsn1SignedCertTemplate
;
63 extenp
= &signedCert
.tbs
.extensions
;
66 memset(&signedCrl
, 0, sizeof(signedCrl
));
67 decodeTarget
= &signedCrl
;
68 templ
= kSecAsn1SignedCrlTemplate
;
69 extenp
= &signedCrl
.tbs
.extensions
;
72 const char *certFile
= argv
[1];
73 const char *outBase
= argv
[2];
74 unsigned char *rawCert
;
77 if(readFile(certFile
, &rawCert
, &rawCertLen
)) {
78 printf("***Can't read cert file. Abortihng.\n");
82 SecAsn1CoderRef coder
;
83 CSSM_DATA rawItem
= {rawCertLen
, rawCert
};
85 OSStatus ortn
= SecAsn1CoderCreate(&coder
);
87 cssmPerror("SecAsn1CoderCreate", ortn
);
90 if(SecAsn1DecodeData(coder
, &rawItem
, templ
, decodeTarget
)) {
91 printf("SecAsn1DecodeData(signed) error\n");
95 NSS_CertExtension
**extens
= *extenp
;
96 unsigned numExtens
= nssArraySize((const void **)extens
);
98 printf("There appear to be zero extensions in this item.\n");
103 char oidStr
[OID_PARSER_STRING_SIZE
];
104 char outFileName
[200];
106 for(unsigned dex
=0; dex
<numExtens
; dex
++) {
107 NSS_CertExtension
*exten
= extens
[dex
];
108 parser
.oidParse(exten
->extnId
.Data
, exten
->extnId
.Length
, oidStr
);
109 printf("Extension %u : %s\n", dex
, oidStr
);
110 sprintf(outFileName
, "%s_%u", outBase
, dex
);
111 if(writeFile(outFileName
, exten
->value
.Data
, exten
->value
.Length
)) {
112 printf("***Error writing %s. Aborting.\n",
117 printf("...wrote %lu bytes to %s\n",
118 exten
->value
.Length
, outFileName
);
121 SecAsn1CoderRelease(coder
);