2 * Copyright (c) 2005-2007,2010 Apple Inc. All Rights Reserved.
4 * parseCert.c - parse a DER-encoded X509 certificate using libDER.
11 #include <libDER/libDER.h>
12 #include <libDER/asn1Types.h>
13 #include <libDER/DER_CertCrl.h>
14 #include <libDER/DER_Keys.h>
15 #include <libDERUtils/fileIo.h>
16 #include <libDERUtils/libDERUtils.h>
17 #include <libDERUtils/printFields.h>
19 static void usage(char **argv
)
21 printf("usage: %s certFile [options]\n", argv
[0]);
23 printf(" -v -- verbose \n");
28 static void printValidity(
35 drtn
= DERParseSequenceContent(validity
,
36 DERNumValidityItemSpecs
, DERValidityItemSpecs
,
39 DERPerror("DERParseSequenceContent(validity)", drtn
);
42 decodePrintItem("notBefore", IT_Leaf
, verbose
, &derv
.notBefore
);
43 decodePrintItem("notAfter", IT_Leaf
, verbose
, &derv
.notAfter
);
47 int main(int argc
, char **argv
)
49 unsigned char *certData
= NULL
;
50 unsigned certDataLen
= 0;
51 DERSignedCertCrl signedCert
;
63 if(readFile(argv
[1], &certData
, &certDataLen
)) {
64 printf("***Error reading cert from %s. Aborting.\n", argv
[1]);
69 while ((arg
= getopt(argc
, argv
, "vh")) != -1) {
82 /* Top level decode of signed cert into 3 components */
84 item
.length
= certDataLen
;
85 drtn
= DERParseSequence(&item
, DERNumSignedCertCrlItemSpecs
, DERSignedCertCrlItemSpecs
,
86 &signedCert
, sizeof(signedCert
));
88 DERPerror("DERParseSequence(SignedCert)", drtn
);
91 printItem("TBSCert", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &signedCert
.tbs
);
95 /* decode the TBSCert - it was saved in full DER form */
96 drtn
= DERParseSequence(&signedCert
.tbs
,
97 DERNumTBSCertItemSpecs
, DERTBSCertItemSpecs
,
100 DERPerror("DERParseSequenceContent(TBSCert)", drtn
);
103 if(tbs
.version
.data
) {
104 /* unwrap the explicitly tagged integer.... */
105 decodePrintItem("version", IT_Leaf
, verbose
, &tbs
.version
);
107 printItem("serialNum", IT_Leaf
, verbose
, ASN1_INTEGER
, &tbs
.serialNum
);
109 printItem("tbsSigAlg", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &tbs
.tbsSigAlg
);
111 printAlgId(&tbs
.tbsSigAlg
, verbose
);
114 printItem("issuer", IT_Leaf
, verbose
, ASN1_CONSTR_SEQUENCE
, &tbs
.issuer
);
115 printItem("subject", IT_Leaf
, verbose
, ASN1_CONSTR_SEQUENCE
, &tbs
.subject
);
117 printItem("validity", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &tbs
.validity
);
119 printValidity(&tbs
.validity
, verbose
);
122 printItem("subjectPubKey", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
,
125 printSubjPubKeyInfo(&tbs
.subjectPubKey
, verbose
);
128 if(tbs
.issuerID
.data
) {
129 /* found tag is implicit context specific: tell printItem what it really is */
130 printItem("issuerID", IT_Leaf
, verbose
, ASN1_BIT_STRING
, &tbs
.issuerID
);
132 if(tbs
.subjectID
.data
) {
133 printItem("subjectID", IT_Leaf
, verbose
, ASN1_BIT_STRING
, &tbs
.subjectID
);
135 if(tbs
.extensions
.data
) {
136 printItem("extensions", IT_Leaf
, verbose
, ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 3,
141 printItem("sigAlg", IT_Branch
, verbose
, ASN1_CONSTR_SEQUENCE
, &signedCert
.sigAlg
);
143 printAlgId(&signedCert
.sigAlg
, verbose
);
146 printItem("sig", IT_Leaf
, verbose
, ASN1_BIT_STRING
, &signedCert
.sig
);