1 /* Copyright (c) 2005-2009 Apple Inc. All Rights Reserved. */
4 * DER_Cert.c - support for decoding X509 certificates
6 * Created Nov. 4 2005 by Doug Mitchell.
9 #include <libDER/DER_Decode.h>
10 #include <libDER/DER_CertCrl.h>
11 #include <libDER/asn1Types.h>
14 * DERItemSpecs for X509 certificates.
17 /* top level cert with three components */
18 const DERItemSpec DERSignedCertCrlItemSpecs
[] =
20 { DER_OFFSET(DERSignedCertCrl
, tbs
),
22 DER_DEC_NO_OPTS
| DER_DEC_SAVE_DER
},
23 { DER_OFFSET(DERSignedCertCrl
, sigAlg
),
26 { DER_OFFSET(DERSignedCertCrl
, sig
),
31 const DERSize DERNumSignedCertCrlItemSpecs
=
32 sizeof(DERSignedCertCrlItemSpecs
) / sizeof(DERItemSpec
);
35 const DERItemSpec DERTBSCertItemSpecs
[] =
37 { DER_OFFSET(DERTBSCert
, version
),
38 ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 0,
39 DER_DEC_OPTIONAL
}, /* version - EXPLICIT */
40 { DER_OFFSET(DERTBSCert
, serialNum
),
43 { DER_OFFSET(DERTBSCert
, tbsSigAlg
),
46 { DER_OFFSET(DERTBSCert
, issuer
),
49 { DER_OFFSET(DERTBSCert
, validity
),
52 { DER_OFFSET(DERTBSCert
, subject
),
55 { DER_OFFSET(DERTBSCert
, subjectPubKey
),
58 /* libsecurity_asn1 has these two as CONSTRUCTED, but the ASN.1 spec
59 * doesn't look that way to me. I don't have any certs that have these
61 { DER_OFFSET(DERTBSCert
, issuerID
),
62 ASN1_CONTEXT_SPECIFIC
| 1,
64 { DER_OFFSET(DERTBSCert
, subjectID
),
65 ASN1_CONTEXT_SPECIFIC
| 2,
67 { DER_OFFSET(DERTBSCert
, extensions
),
68 ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 3,
71 const DERSize DERNumTBSCertItemSpecs
= sizeof(DERTBSCertItemSpecs
) / sizeof(DERItemSpec
);
74 const DERItemSpec DERValidityItemSpecs
[] =
76 { DER_OFFSET(DERValidity
, notBefore
),
78 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
},
79 { DER_OFFSET(DERValidity
, notAfter
),
81 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
}
83 const DERSize DERNumValidityItemSpecs
=
84 sizeof(DERValidityItemSpecs
) / sizeof(DERItemSpec
);
86 /* DERAttributeTypeAndValue */
87 const DERItemSpec DERAttributeTypeAndValueItemSpecs
[] = {
88 { DER_OFFSET(DERAttributeTypeAndValue
, type
),
91 { DER_OFFSET(DERAttributeTypeAndValue
, value
),
93 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
}
96 const DERSize DERNumAttributeTypeAndValueItemSpecs
=
97 sizeof(DERAttributeTypeAndValueItemSpecs
) / sizeof(DERItemSpec
);
100 const DERItemSpec DERExtensionItemSpecs
[] =
102 { DER_OFFSET(DERExtension
, extnID
),
105 { DER_OFFSET(DERExtension
, critical
),
108 { DER_OFFSET(DERExtension
, extnValue
),
112 const DERSize DERNumExtensionItemSpecs
=
113 sizeof(DERExtensionItemSpecs
) / sizeof(DERItemSpec
);
115 /* DERBasicConstraints */
116 const DERItemSpec DERBasicConstraintsItemSpecs
[] =
118 { DER_OFFSET(DERBasicConstraints
, cA
),
121 { DER_OFFSET(DERBasicConstraints
, pathLenConstraint
),
125 const DERSize DERNumBasicConstraintsItemSpecs
=
126 sizeof(DERBasicConstraintsItemSpecs
) / sizeof(DERItemSpec
);
128 /* DERPrivateKeyUsagePeriod. */
129 const DERItemSpec DERPrivateKeyUsagePeriodItemSpecs
[] =
131 { DER_OFFSET(DERPrivateKeyUsagePeriod
, notBefore
),
132 ASN1_CONTEXT_SPECIFIC
| 0,
134 { DER_OFFSET(DERPrivateKeyUsagePeriod
, notAfter
),
135 ASN1_CONTEXT_SPECIFIC
| 1,
138 const DERSize DERNumPrivateKeyUsagePeriodItemSpecs
=
139 sizeof(DERPrivateKeyUsagePeriodItemSpecs
) / sizeof(DERItemSpec
);
141 /* DERDistributionPoint. */
142 const DERItemSpec DERDistributionPointItemSpecs
[] =
144 { DER_OFFSET(DERDistributionPoint
, distributionPoint
),
145 ASN1_CONTEXT_SPECIFIC
| ASN1_CONSTRUCTED
| 0,
147 { DER_OFFSET(DERDistributionPoint
, reasons
),
148 ASN1_CONTEXT_SPECIFIC
| 1,
150 { DER_OFFSET(DERDistributionPoint
, cRLIssuer
),
151 ASN1_CONTEXT_SPECIFIC
| ASN1_CONSTRUCTED
| 2,
154 const DERSize DERNumDistributionPointItemSpecs
=
155 sizeof(DERDistributionPointItemSpecs
) / sizeof(DERItemSpec
);
157 /* DERPolicyInformation. */
158 const DERItemSpec DERPolicyInformationItemSpecs
[] =
160 { DER_OFFSET(DERPolicyInformation
, policyIdentifier
),
163 { DER_OFFSET(DERPolicyInformation
, policyQualifiers
),
164 ASN1_CONSTR_SEQUENCE
,
167 const DERSize DERNumPolicyInformationItemSpecs
=
168 sizeof(DERPolicyInformationItemSpecs
) / sizeof(DERItemSpec
);
170 /* DERPolicyQualifierInfo. */
171 const DERItemSpec DERPolicyQualifierInfoItemSpecs
[] =
173 { DER_OFFSET(DERPolicyQualifierInfo
, policyQualifierID
),
176 { DER_OFFSET(DERPolicyQualifierInfo
, qualifier
),
177 0, /* no tag - ANY */
178 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
}
180 const DERSize DERNumPolicyQualifierInfoItemSpecs
=
181 sizeof(DERPolicyQualifierInfoItemSpecs
) / sizeof(DERItemSpec
);
184 const DERItemSpec DERUserNoticeItemSpecs
[] =
186 { DER_OFFSET(DERUserNotice
, noticeRef
),
187 ASN1_CONSTR_SEQUENCE
,
189 { DER_OFFSET(DERUserNotice
, explicitText
),
190 0, /* no tag - ANY */
191 DER_DEC_ASN_ANY
| DER_DEC_OPTIONAL
| DER_DEC_SAVE_DER
}
193 const DERSize DERNumUserNoticeItemSpecs
=
194 sizeof(DERUserNoticeItemSpecs
) / sizeof(DERItemSpec
);
196 /* DERNoticeReference. */
197 const DERItemSpec DERNoticeReferenceItemSpecs
[] =
199 { DER_OFFSET(DERNoticeReference
, organization
),
200 0, /* no tag - ANY */
201 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
},
202 { DER_OFFSET(DERNoticeReference
, noticeNumbers
),
203 ASN1_CONSTR_SEQUENCE
,
206 const DERSize DERNumNoticeReferenceItemSpecs
=
207 sizeof(DERNoticeReferenceItemSpecs
) / sizeof(DERItemSpec
);
209 /* DERPolicyMapping. */
210 const DERItemSpec DERPolicyMappingItemSpecs
[] =
212 { DER_OFFSET(DERPolicyMapping
, issuerDomainPolicy
),
215 { DER_OFFSET(DERPolicyMapping
, subjectDomainPolicy
),
219 const DERSize DERNumPolicyMappingItemSpecs
=
220 sizeof(DERPolicyMappingItemSpecs
) / sizeof(DERItemSpec
);
222 /* DERAccessDescription. */
223 const DERItemSpec DERAccessDescriptionItemSpecs
[] =
225 { DER_OFFSET(DERAccessDescription
, accessMethod
),
228 { DER_OFFSET(DERAccessDescription
, accessLocation
),
229 0, /* no tag - ANY */
230 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
}
232 const DERSize DERNumAccessDescriptionItemSpecs
=
233 sizeof(DERAccessDescriptionItemSpecs
) / sizeof(DERItemSpec
);
235 /* DERAuthorityKeyIdentifier. */
236 const DERItemSpec DERAuthorityKeyIdentifierItemSpecs
[] =
238 { DER_OFFSET(DERAuthorityKeyIdentifier
, keyIdentifier
),
239 ASN1_CONTEXT_SPECIFIC
| 0,
241 { DER_OFFSET(DERAuthorityKeyIdentifier
, authorityCertIssuer
),
242 ASN1_CONTEXT_SPECIFIC
| ASN1_CONSTRUCTED
| 1,
244 { DER_OFFSET(DERAuthorityKeyIdentifier
, authorityCertSerialNumber
),
245 ASN1_CONTEXT_SPECIFIC
| 2,
248 const DERSize DERNumAuthorityKeyIdentifierItemSpecs
=
249 sizeof(DERAuthorityKeyIdentifierItemSpecs
) / sizeof(DERItemSpec
);
252 const DERItemSpec DEROtherNameItemSpecs
[] =
254 { DER_OFFSET(DEROtherName
, typeIdentifier
),
257 { DER_OFFSET(DEROtherName
, value
),
258 ASN1_CONTEXT_SPECIFIC
| ASN1_CONSTRUCTED
| 0,
261 const DERSize DERNumOtherNameItemSpecs
=
262 sizeof(DEROtherNameItemSpecs
) / sizeof(DERItemSpec
);
264 /* DERPolicyConstraints. */
265 const DERItemSpec DERPolicyConstraintsItemSpecs
[] =
267 { DER_OFFSET(DERPolicyConstraints
, requireExplicitPolicy
),
268 ASN1_CONTEXT_SPECIFIC
| 0,
270 { DER_OFFSET(DERPolicyConstraints
, inhibitPolicyMapping
),
271 ASN1_CONTEXT_SPECIFIC
| 1,
274 const DERSize DERNumPolicyConstraintsItemSpecs
=
275 sizeof(DERPolicyConstraintsItemSpecs
) / sizeof(DERItemSpec
);
278 const DERItemSpec DERTBSCrlItemSpecs
[] =
280 { DER_OFFSET(DERTBSCrl
, version
),
283 { DER_OFFSET(DERTBSCrl
, tbsSigAlg
),
284 ASN1_CONSTR_SEQUENCE
,
286 { DER_OFFSET(DERTBSCrl
, issuer
),
287 ASN1_CONSTR_SEQUENCE
,
289 { DER_OFFSET(DERTBSCrl
, thisUpdate
),
290 0, /* no tag - ANY */
291 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
},
292 { DER_OFFSET(DERTBSCrl
, nextUpdate
),
293 0, /* no tag - ANY */
294 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
},
295 { DER_OFFSET(DERTBSCrl
, revokedCerts
),
296 ASN1_CONSTR_SEQUENCE
,
298 { DER_OFFSET(DERTBSCrl
, extensions
),
299 ASN1_CONSTRUCTED
| ASN1_CONTEXT_SPECIFIC
| 0,
302 const DERSize DERNumTBSCrlItemSpecs
= sizeof(DERTBSCrlItemSpecs
) / sizeof(DERItemSpec
);
305 const DERItemSpec DERRevokedCertItemSpecs
[] =
307 { DER_OFFSET(DERRevokedCert
, serialNum
),
310 { DER_OFFSET(DERRevokedCert
, revocationDate
),
311 0, /* no tag - ANY */
312 DER_DEC_ASN_ANY
| DER_DEC_SAVE_DER
},
313 { DER_OFFSET(DERRevokedCert
, extensions
),
314 ASN1_CONSTR_SEQUENCE
,
318 const DERSize DERNumRevokedCertItemSpecs
=
319 sizeof(DERRevokedCertItemSpecs
) / sizeof(DERItemSpec
);