2 * Copyright (c) 2003-2006,2008,2010-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 * nameTemplates.c - ASN1 templates for X509 Name, GeneralName, etc.
26 #include "SecAsn1Templates.h"
27 #include "nameTemplates.h"
28 #include "X509Templates.h"
29 #include "keyTemplates.h"
31 #include <security_utilities/simulatecrash_assert.h>
35 SecAsn1Item value
; // unparsed, BER-encoded
38 // MARK: ----- Generalized NSS_TaggedItem template chooser support -----
41 * Generalized Template chooser.
43 const SecAsn1Template
* SecAsn1TaggedTemplateChooser(
44 /* Five args passed to specific SecAsn1TemplateChooser */
45 void *arg
, // currently not used
50 /* array of tag/template pairs */
51 const NSS_TagChoice
*chooser
)
53 unsigned char tag
= 0;
54 const SecAsn1Template
*templ
= NULL
;
55 NSS_TaggedItem
*item
= (NSS_TaggedItem
*)dest
;
58 assert((chooser
!= NULL
) && (chooser
->templ
!= NULL
));
61 /* encoding: tag from an NSS_TaggedItem at *dest */
65 /* decoding: tag from raw bytes being decoded */
66 tag
= buf
[0] & SEC_ASN1_TAGNUM_MASK
;
67 /* and tell caller what's coming */
71 * If buffer length is 0, leave tag = 0. No choice will have this
75 /* infer template from tag */
76 const NSS_TagChoice
*thisChoice
;
77 for(thisChoice
=chooser
; thisChoice
->templ
!= NULL
; thisChoice
++) {
78 if(tag
== thisChoice
->tag
) {
79 templ
= thisChoice
->templ
;
85 * Tag not found. On decoding, this is the caller's fault
86 * and they'll have to deal with it.
87 * On decode, pick a template guaranteed to cause a decoding
88 * failure - the template from the first array of
89 * NSS_TagChoices should do the trick since its tag didn't match.
91 templ
= chooser
[0].templ
;
96 // MARK: ----- X509 Name, RDN ------
98 /* AttributeTypeAndValue */
101 * NSS_ATV Template chooser.
103 static const NSS_TagChoice atvChoices
[] = {
104 { SEC_ASN1_PRINTABLE_STRING
, kSecAsn1PrintableStringTemplate
} ,
105 { SEC_ASN1_TELETEX_STRING
, kSecAsn1TeletexStringTemplate
},
106 { SEC_ASN1_UNIVERSAL_STRING
, kSecAsn1UniversalStringTemplate
},
107 { SEC_ASN1_UTF8_STRING
, kSecAsn1UTF8StringTemplate
},
108 { SEC_ASN1_BMP_STRING
, kSecAsn1BMPStringTemplate
},
109 { SEC_ASN1_IA5_STRING
, kSecAsn1IA5StringTemplate
},
113 static const SecAsn1Template
* NSS_ATVChooser(
120 return SecAsn1TaggedTemplateChooser(arg
, enc
, buf
, len
, dest
, atvChoices
);
123 static const SecAsn1TemplateChooserPtr NSS_ATVChooserPtr
= NSS_ATVChooser
;
125 const SecAsn1Template kSecAsn1ATVTemplate
[] = {
127 0, NULL
, sizeof(NSS_ATV
) },
128 { SEC_ASN1_OBJECT_ID
,
129 offsetof(NSS_ATV
,type
), },
130 { SEC_ASN1_INLINE
| SEC_ASN1_DYNAMIC
,
131 offsetof(NSS_ATV
,value
),
132 &NSS_ATVChooserPtr
},
136 /* RelativeDistinguishedName */
137 const SecAsn1Template kSecAsn1RDNTemplate
[] = {
139 offsetof(NSS_RDN
,atvs
), kSecAsn1ATVTemplate
, sizeof(NSS_RDN
) }
143 const SecAsn1Template kSecAsn1NameTemplate
[] = {
144 { SEC_ASN1_SEQUENCE_OF
,
145 offsetof(NSS_Name
,rdns
), kSecAsn1RDNTemplate
, sizeof(NSS_Name
) }
148 // MARK: ----- OtherName, GeneralizedName -----
151 * CE_OtherName.value expressed as ASN_ANY, not en/decoded.
153 const SecAsn1Template NSS_OtherNameTemplate
[] = {
155 0, NULL
, sizeof(CE_OtherName
) },
156 { SEC_ASN1_OBJECT_ID
,
157 offsetof(CE_OtherName
,typeId
), },
159 offsetof(CE_OtherName
,value
), },
164 * For decoding an OtherName when it's a context-specific CHOICE
167 const SecAsn1Template kSecAsn1GenNameOtherNameTemplate
[] = {
168 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_OtherName
,
169 0, NSS_OtherNameTemplate
, sizeof(CE_OtherName
) }
173 * NSS_GeneralName template chooser.
174 * First, a crufty set of templates specific to this context.
175 * All offsets are zero (the fundamental type is a NSS_TaggedItem).
177 * NOTE WELL: RFC2459 says that all of the choices within a
178 * GeneralName (which these templates implement) have implicit
179 * context-specific tags.
180 * HOWEVER: RFC2538 and the real world indicate that the directoryName
181 * choice is EXPLICITLY tagged. This causes an extra layer of DER -
182 * the "thing" is wrapped in a header consisting of the tag byte
183 * (SEC_ASN1_CONTEXT_SPECIFIC plus context tag plus SEC_ASN1_CONSTRUCTED)
184 * and the length field.
186 * To actually implement this in the current pile-of-cruft context,
187 * the directoryName and otherName choices are processed here with
188 * NSS_InnerAnyTemplate which strips off the explicit tag layer, leaving
189 * further processing to the app.
191 * I sure hope we don't find certs that actually conform to RFC2459 on
192 * this. We might have to handle both. Be forewarned.
195 /* inner contents of an ASN_ANY */
197 #define NSS_GEN_NAME_OFFSET (offsetof(NSS_GeneralName,item))
198 #define NSS_GEN_NAME_SIZE (sizeof(NSS_GeneralName))
200 const SecAsn1Template kSecAsn1OtherNameTemplate
[] = {
201 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_OtherName
,
202 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
204 const SecAsn1Template kSecAsn1RFC822NameTemplate
[] = {
205 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_RFC822Name
,
206 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
208 const SecAsn1Template kSecAsn1DNSNameTemplate
[] = {
209 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_DNSName
,
210 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
212 const SecAsn1Template kSecAsn1X400AddressTemplate
[] = {
213 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_X400Address
,
214 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
216 const SecAsn1Template kSecAsn1DirectoryNameTemplate
[] = {
217 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
|
218 SEC_ASN1_EXPLICIT
| NGT_DirectoryName
,
219 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
221 const SecAsn1Template kSecAsn1EdiPartyNameTemplate
[] = {
222 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_EdiPartyName
,
223 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
225 const SecAsn1Template kSecAsn1URITemplate
[] = {
226 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_URI
,
227 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
229 const SecAsn1Template kSecAsn1IPAddressTemplate
[] = {
230 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_IPAddress
,
231 NSS_GEN_NAME_OFFSET
, kSecAsn1OctetStringTemplate
, NSS_GEN_NAME_SIZE
}
233 const SecAsn1Template kSecAsn1RegisteredIDTemplate
[] = {
234 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_RegisteredID
,
235 NSS_GEN_NAME_OFFSET
, kSecAsn1ObjectIDTemplate
, NSS_GEN_NAME_SIZE
}
238 static const NSS_TagChoice genNameChoices
[] = {
239 { NGT_OtherName
, kSecAsn1OtherNameTemplate
} ,
240 { NGT_RFC822Name
, kSecAsn1RFC822NameTemplate
},
241 { NGT_DNSName
, kSecAsn1DNSNameTemplate
},
242 { NGT_X400Address
, kSecAsn1X400AddressTemplate
},
243 { NGT_DirectoryName
, kSecAsn1DirectoryNameTemplate
},
244 { NGT_EdiPartyName
, kSecAsn1EdiPartyNameTemplate
},
245 { NGT_URI
, kSecAsn1URITemplate
},
246 { NGT_IPAddress
, kSecAsn1IPAddressTemplate
},
247 { NGT_RegisteredID
, kSecAsn1RegisteredIDTemplate
},
251 static const SecAsn1Template
* NSS_genNameChooser(
258 return SecAsn1TaggedTemplateChooser(arg
, enc
, buf
, len
, dest
, genNameChoices
);
261 static const SecAsn1TemplateChooserPtr NSS_genNameChooserPtr
=
264 const SecAsn1Template kSecAsn1GeneralNameTemplate
[] = {
265 { SEC_ASN1_DYNAMIC
| SEC_ASN1_CONTEXT_SPECIFIC
,
266 offsetof(NSS_GeneralName
,item
), // Needed?
267 &NSS_genNameChooserPtr
},