2 * Parse a cert, dump its subject name (in normalized DER-encoded form),
3 * key size, and public key blob to stdout.
5 #include <utilLib/common.h>
6 #include <utilLib/cspwrap.h>
7 #include <security_cdsa_utils/cuFileIo.h>
8 #include <security_cdsa_utils/cuPrintCert.h>
9 #include <clAppUtils/clutils.h>
13 #include <Security/cssm.h>
14 #include <Security/oidscert.h>
15 #include <Security/SecAsn1Coder.h>
16 #include <Security/X509Templates.h>
18 #define WRITE_NAME_FILE 1
20 /* allow checking various DER encoded fields */
21 #define SUBJECT_NAME_OID CSSMOID_X509V1SubjectName /* normalized */
22 // #define SUBJECT_NAME_OID CSSMOID_X509V1SubjectNameStd /* non normalized */
25 * Print the contents of a CSSM_DATA, like so:
27 * static const uint8 <label>_bytes[] = {
30 * static const CSSM_DATA <label> = { length, (uint8 *)<label>_bytes} ;
32 static void dumpDataBlob(
34 const CSSM_DATA_PTR data
)
38 printf("static const uint8 %s_bytes[] = {\n ", label
);
39 for(i
=0; i
<data
->Length
; i
++) {
40 printf("0x%02x", data
->Data
[i
]);
41 if(i
!= (data
->Length
- 1)) {
49 printf("static const CSSM_DATA %s = { %lu, (uint8 *)%s_bytes };\n",
50 label
, data
->Length
, label
);
53 static void printHeader(
55 CSSM_DATA_PTR subject
)
58 OidParser
parser(false); // quick parser, no config file
60 printf("/***********************\n");
61 printf("Cert File Name: %s\n", fileName
);
62 field
.FieldValue
= *subject
;
63 field
.FieldOid
= CSSMOID_X509V1SubjectNameCStruct
;
64 printCertField(field
, parser
, true);
65 printf(" ***********************/\n");
68 int main(int argc
, char **argv
)
70 CSSM_CL_HANDLE clHand
; // CL handle
71 CSSM_DATA rawCert
= {0, NULL
};
73 CSSM_HANDLE ResultsHandle
= 0;
83 NSS_Certificate signedCert
;
84 SecAsn1CoderRef coder
;
87 printf("usage: %s certFile\n", argv
[0]);
94 printf("clStartup failure; aborting\n");
98 /* subsequent errors to abort: */
99 /* read a in raw cert */
101 rtn
= readFile(argv
[1], &rawCert
.Data
, &len
);
103 printf("Error %s reading file %s\n", strerror(rtn
), argv
[1]);
106 rawCert
.Length
= len
;
108 /* C string of file name, terminating at '.' or space */
109 nameLen
= strlen(argv
[1]);
110 memmove(baseName
, argv
[1], nameLen
);
111 baseName
[nameLen
] = '\0';
112 cp
= strchr(baseName
, '.');
116 cp
= strchr(baseName
, ' ');
121 /* print filename and parsed subject name as comment */
122 crtn
= CSSM_CL_CertGetFirstFieldValue(
125 &CSSMOID_X509V1SubjectNameCStruct
,
130 printError("CSSM_CL_CertGetFirstFieldValue(CSSMOID_X509V1SubjectNameCStruct)", crtn
);
133 CSSM_CL_CertAbortQuery(clHand
, ResultsHandle
);
135 printf("Error extracting subject name\n");
138 printHeader(argv
[1], value
);
139 CSSM_CL_FreeFieldValue(clHand
, &CSSMOID_X509V1SubjectNameCStruct
, value
);
141 /* print normalized & encoded subject name as C data */
142 crtn
= CSSM_CL_CertGetFirstFieldValue(
150 printError("CSSM_CL_CertGetFirstFieldValue(CSSMOID_X509V1SubjectName)", crtn
);
153 CSSM_CL_CertAbortQuery(clHand
, ResultsHandle
);
155 printf("Error extracting subject name\n");
158 sprintf(blobName
, "%s_subject", baseName
);
159 dumpDataBlob(blobName
, value
);
161 writeFile(blobName
, value
->Data
, (unsigned)value
->Length
);
163 CSSM_CL_FreeFieldValue(clHand
, &SUBJECT_NAME_OID
, value
);
165 /* print key blob as data */
166 crtn
= CSSM_CL_CertGetFirstFieldValue(
169 &CSSMOID_CSSMKeyStruct
,
174 printError("CSSM_CL_CertGetFirstFieldValue(CSSMOID_CSSMKeyStruct)", crtn
);
177 CSSM_CL_CertAbortQuery(clHand
, ResultsHandle
);
179 printf("Error extracting public key\n");
182 if(value
->Length
!= sizeof(CSSM_KEY
)) {
183 printf("CSSMOID_CSSMKeyStruct length error\n");
186 key
= (CSSM_KEY_PTR
)value
->Data
;
187 sprintf(blobName
, "%s_pubKey", baseName
);
188 dumpDataBlob(blobName
, &key
->KeyData
);
189 keySize
= key
->KeyHeader
.LogicalKeySizeInBits
;
190 CSSM_CL_FreeFieldValue(clHand
, &CSSMOID_CSSMKeyStruct
, value
);
192 /* unnormalized DER-encoded issuer */
193 SecAsn1CoderCreate(&coder
);
194 memset(&signedCert
, 0, sizeof(signedCert
));
195 if(SecAsn1DecodeData(coder
, &rawCert
, kSecAsn1SignedCertTemplate
, &signedCert
)) {
196 printf("***Error NSS-decoding certificate\n");
199 sprintf(blobName
, "%s_derIssuer", baseName
);
200 dumpDataBlob(blobName
, &signedCert
.tbs
.derIssuer
);
202 /* now the the struct containing all three */
203 printf("\n { &%s_subject, &%s_pubKey, %u },\n", baseName
, baseName
, (unsigned)keySize
);
207 CSSM_ModuleDetach(clHand
);
209 SecAsn1CoderRelease(coder
);