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"
35 SecAsn1Item value
; // unparsed, BER-encoded
38 // MARK: ----- Generalized NSS_TaggedItem template chooser support -----
41 * Generalized Template chooser.
43 const SecAsn1Template
* SecAsn1TaggedTemplateChooser(
44 /* Four args passed to specific SecAsn1TemplateChooser */
45 void *arg
, // currently not used
49 /* array of tag/template pairs */
50 const NSS_TagChoice
*chooser
)
52 unsigned char tag
= 0;
53 const SecAsn1Template
*templ
= NULL
;
54 NSS_TaggedItem
*item
= (NSS_TaggedItem
*)dest
;
57 assert((chooser
!= NULL
) && (chooser
->templ
!= NULL
));
60 /* encoding: tag from an NSS_TaggedItem at *dest */
64 /* decoding: tag from raw bytes being decoded */
65 tag
= buf
[0] & SEC_ASN1_TAGNUM_MASK
;
66 /* and tell caller what's coming */
70 /* infer template from tag */
71 const NSS_TagChoice
*thisChoice
;
72 for(thisChoice
=chooser
; thisChoice
->templ
!= NULL
; thisChoice
++) {
73 if(tag
== thisChoice
->tag
) {
74 templ
= thisChoice
->templ
;
80 * Tag not found. On decoding, this is the caller's fault
81 * and they'll have to deal with it.
82 * On decode, pick a template guaranteed to cause a decoding
83 * failure - the template from the first array of
84 * NSS_TagChoices should do the trick since its tag didn't match.
86 templ
= chooser
[0].templ
;
91 // MARK: ----- X509 Name, RDN ------
93 /* AttributeTypeAndValue */
96 * NSS_ATV Template chooser.
98 static const NSS_TagChoice atvChoices
[] = {
99 { SEC_ASN1_PRINTABLE_STRING
, kSecAsn1PrintableStringTemplate
} ,
100 { SEC_ASN1_TELETEX_STRING
, kSecAsn1TeletexStringTemplate
},
101 { SEC_ASN1_UNIVERSAL_STRING
, kSecAsn1UniversalStringTemplate
},
102 { SEC_ASN1_UTF8_STRING
, kSecAsn1UTF8StringTemplate
},
103 { SEC_ASN1_BMP_STRING
, kSecAsn1BMPStringTemplate
},
104 { SEC_ASN1_IA5_STRING
, kSecAsn1IA5StringTemplate
},
108 static const SecAsn1Template
* NSS_ATVChooser(
114 return SecAsn1TaggedTemplateChooser(arg
, enc
, buf
, dest
, atvChoices
);
117 static const SecAsn1TemplateChooserPtr NSS_ATVChooserPtr
= NSS_ATVChooser
;
119 const SecAsn1Template kSecAsn1ATVTemplate
[] = {
121 0, NULL
, sizeof(NSS_ATV
) },
122 { SEC_ASN1_OBJECT_ID
,
123 offsetof(NSS_ATV
,type
), },
124 { SEC_ASN1_INLINE
| SEC_ASN1_DYNAMIC
,
125 offsetof(NSS_ATV
,value
),
126 &NSS_ATVChooserPtr
},
130 /* RelativeDistinguishedName */
131 const SecAsn1Template kSecAsn1RDNTemplate
[] = {
133 offsetof(NSS_RDN
,atvs
), kSecAsn1ATVTemplate
, sizeof(NSS_RDN
) }
137 const SecAsn1Template kSecAsn1NameTemplate
[] = {
138 { SEC_ASN1_SEQUENCE_OF
,
139 offsetof(NSS_Name
,rdns
), kSecAsn1RDNTemplate
, sizeof(NSS_Name
) }
142 // MARK: ----- OtherName, GeneralizedName -----
145 * CE_OtherName.value expressed as ASN_ANY, not en/decoded.
147 const SecAsn1Template NSS_OtherNameTemplate
[] = {
149 0, NULL
, sizeof(CE_OtherName
) },
150 { SEC_ASN1_OBJECT_ID
,
151 offsetof(CE_OtherName
,typeId
), },
153 offsetof(CE_OtherName
,value
), },
158 * For decoding an OtherName when it's a context-specific CHOICE
161 const SecAsn1Template kSecAsn1GenNameOtherNameTemplate
[] = {
162 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_OtherName
,
163 0, NSS_OtherNameTemplate
, sizeof(CE_OtherName
) }
167 * NSS_GeneralName template chooser.
168 * First, a crufty set of templates specific to this context.
169 * All offsets are zero (the fundamental type is a NSS_TaggedItem).
171 * NOTE WELL: RFC2459 says that all of the choices within a
172 * GeneralName (which these templates implement) have implicit
173 * context-specific tags.
174 * HOWEVER: RFC2538 and the real world indicate that the directoryName
175 * choice is EXPLICITLY tagged. This causes an extra layer of DER -
176 * the "thing" is wrapped in a header consisting of the tag byte
177 * (SEC_ASN1_CONTEXT_SPECIFIC plus context tag plus SEC_ASN1_CONSTRUCTED)
178 * and the length field.
180 * To actually implement this in the current pile-of-cruft context,
181 * the directoryName and otherName choices are processed here with
182 * NSS_InnerAnyTemplate which strips off the explicit tag layer, leaving
183 * further processing to the app.
185 * I sure hope we don't find certs that actually conform to RFC2459 on
186 * this. We might have to handle both. Be forewarned.
189 /* inner contents of an ASN_ANY */
191 #define NSS_GEN_NAME_OFFSET (offsetof(NSS_GeneralName,item))
192 #define NSS_GEN_NAME_SIZE (sizeof(NSS_GeneralName))
194 const SecAsn1Template kSecAsn1OtherNameTemplate
[] = {
195 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_OtherName
,
196 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
198 const SecAsn1Template kSecAsn1RFC822NameTemplate
[] = {
199 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_RFC822Name
,
200 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
202 const SecAsn1Template kSecAsn1DNSNameTemplate
[] = {
203 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_DNSName
,
204 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
206 const SecAsn1Template kSecAsn1X400AddressTemplate
[] = {
207 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_X400Address
,
208 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
210 const SecAsn1Template kSecAsn1DirectoryNameTemplate
[] = {
211 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
|
212 SEC_ASN1_EXPLICIT
| NGT_DirectoryName
,
213 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
215 const SecAsn1Template kSecAsn1EdiPartyNameTemplate
[] = {
216 { SEC_ASN1_CONTEXT_SPECIFIC
| SEC_ASN1_CONSTRUCTED
| NGT_EdiPartyName
,
217 NSS_GEN_NAME_OFFSET
, kSecAsn1AnyTemplate
, NSS_GEN_NAME_SIZE
}
219 const SecAsn1Template kSecAsn1URITemplate
[] = {
220 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_URI
,
221 NSS_GEN_NAME_OFFSET
, kSecAsn1IA5StringTemplate
, NSS_GEN_NAME_SIZE
}
223 const SecAsn1Template kSecAsn1IPAddressTemplate
[] = {
224 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_IPAddress
,
225 NSS_GEN_NAME_OFFSET
, kSecAsn1OctetStringTemplate
, NSS_GEN_NAME_SIZE
}
227 const SecAsn1Template kSecAsn1RegisteredIDTemplate
[] = {
228 { SEC_ASN1_CONTEXT_SPECIFIC
| NGT_RegisteredID
,
229 NSS_GEN_NAME_OFFSET
, kSecAsn1ObjectIDTemplate
, NSS_GEN_NAME_SIZE
}
232 static const NSS_TagChoice genNameChoices
[] = {
233 { NGT_OtherName
, kSecAsn1OtherNameTemplate
} ,
234 { NGT_RFC822Name
, kSecAsn1RFC822NameTemplate
},
235 { NGT_DNSName
, kSecAsn1DNSNameTemplate
},
236 { NGT_X400Address
, kSecAsn1X400AddressTemplate
},
237 { NGT_DirectoryName
, kSecAsn1DirectoryNameTemplate
},
238 { NGT_EdiPartyName
, kSecAsn1EdiPartyNameTemplate
},
239 { NGT_URI
, kSecAsn1URITemplate
},
240 { NGT_IPAddress
, kSecAsn1IPAddressTemplate
},
241 { NGT_RegisteredID
, kSecAsn1RegisteredIDTemplate
},
245 static const SecAsn1Template
* NSS_genNameChooser(
251 return SecAsn1TaggedTemplateChooser(arg
, enc
, buf
, dest
, genNameChoices
);
254 static const SecAsn1TemplateChooserPtr NSS_genNameChooserPtr
=
257 const SecAsn1Template kSecAsn1GeneralNameTemplate
[] = {
258 { SEC_ASN1_DYNAMIC
| SEC_ASN1_CONTEXT_SPECIFIC
,
259 offsetof(NSS_GeneralName
,item
), // Needed?
260 &NSS_genNameChooserPtr
},