]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/nameTemplates.c
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_asn1 / lib / nameTemplates.c
1 /*
2 * Copyright (c) 2003-2006,2008,2010-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 *
23 * nameTemplates.c - ASN1 templates for X509 Name, GeneralName, etc.
24 */
25
26 #include "SecAsn1Templates.h"
27 #include "nameTemplates.h"
28 #include "X509Templates.h"
29 #include "keyTemplates.h"
30 #include <stddef.h>
31 #include <security_utilities/simulatecrash_assert.h>
32
33 typedef struct {
34 SecAsn1Oid typeId;
35 SecAsn1Item value; // unparsed, BER-encoded
36 } CE_OtherName;
37
38 // MARK: ----- Generalized NSS_TaggedItem template chooser support -----
39
40 /*
41 * Generalized Template chooser.
42 */
43 const SecAsn1Template * SecAsn1TaggedTemplateChooser(
44 /* Five args passed to specific SecAsn1TemplateChooser */
45 void *arg, // currently not used
46 Boolean enc,
47 const char *buf,
48 size_t len,
49 void *dest,
50 /* array of tag/template pairs */
51 const NSS_TagChoice *chooser)
52 {
53 unsigned char tag = 0;
54 const SecAsn1Template *templ = NULL;
55 NSS_TaggedItem *item = (NSS_TaggedItem *)dest;
56
57 assert(item != NULL);
58 assert((chooser != NULL) && (chooser->templ != NULL));
59
60 if(enc) {
61 /* encoding: tag from an NSS_TaggedItem at *dest */
62 tag = item->tag;
63 }
64 else if (len > 0) {
65 /* decoding: tag from raw bytes being decoded */
66 tag = buf[0] & SEC_ASN1_TAGNUM_MASK;
67 /* and tell caller what's coming */
68 item->tag = tag;
69 }
70 /*
71 * If buffer length is 0, leave tag = 0. No choice will have this
72 * the invalid tag.
73 */
74
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;
80 break;
81 }
82 }
83 if(templ == NULL) {
84 /*
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.
90 */
91 templ = chooser[0].templ;
92 }
93 return templ;
94 }
95
96 // MARK: ----- X509 Name, RDN ------
97
98 /* AttributeTypeAndValue */
99
100 /*
101 * NSS_ATV Template chooser.
102 */
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 },
110 { 0, NULL}
111 };
112
113 static const SecAsn1Template * NSS_ATVChooser(
114 void *arg,
115 Boolean enc,
116 const char *buf,
117 size_t len,
118 void *dest)
119 {
120 return SecAsn1TaggedTemplateChooser(arg, enc, buf, len, dest, atvChoices);
121 }
122
123 static const SecAsn1TemplateChooserPtr NSS_ATVChooserPtr = NSS_ATVChooser;
124
125 const SecAsn1Template kSecAsn1ATVTemplate[] = {
126 { SEC_ASN1_SEQUENCE,
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 },
133 { 0, }
134 };
135
136 /* RelativeDistinguishedName */
137 const SecAsn1Template kSecAsn1RDNTemplate[] = {
138 { SEC_ASN1_SET_OF,
139 offsetof(NSS_RDN,atvs), kSecAsn1ATVTemplate, sizeof(NSS_RDN) }
140 };
141
142 /* X509 Name */
143 const SecAsn1Template kSecAsn1NameTemplate[] = {
144 { SEC_ASN1_SEQUENCE_OF,
145 offsetof(NSS_Name,rdns), kSecAsn1RDNTemplate, sizeof(NSS_Name) }
146 };
147
148 // MARK: ----- OtherName, GeneralizedName -----
149
150 /*
151 * CE_OtherName.value expressed as ASN_ANY, not en/decoded.
152 */
153 const SecAsn1Template NSS_OtherNameTemplate[] = {
154 { SEC_ASN1_SEQUENCE,
155 0, NULL, sizeof(CE_OtherName) },
156 { SEC_ASN1_OBJECT_ID,
157 offsetof(CE_OtherName,typeId), },
158 { SEC_ASN1_ANY,
159 offsetof(CE_OtherName,value), },
160 { 0, }
161 };
162
163 /*
164 * For decoding an OtherName when it's a context-specific CHOICE
165 * of a GeneralName.
166 */
167 const SecAsn1Template kSecAsn1GenNameOtherNameTemplate[] = {
168 { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | NGT_OtherName,
169 0, NSS_OtherNameTemplate, sizeof(CE_OtherName) }
170 };
171
172 /*
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).
176 *
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.
185 *
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.
190 *
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.
193 */
194
195 /* inner contents of an ASN_ANY */
196
197 #define NSS_GEN_NAME_OFFSET (offsetof(NSS_GeneralName,item))
198 #define NSS_GEN_NAME_SIZE (sizeof(NSS_GeneralName))
199
200 const SecAsn1Template kSecAsn1OtherNameTemplate[] = {
201 { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | NGT_OtherName,
202 NSS_GEN_NAME_OFFSET, kSecAsn1AnyTemplate, NSS_GEN_NAME_SIZE }
203 };
204 const SecAsn1Template kSecAsn1RFC822NameTemplate[] = {
205 { SEC_ASN1_CONTEXT_SPECIFIC | NGT_RFC822Name,
206 NSS_GEN_NAME_OFFSET, kSecAsn1IA5StringTemplate, NSS_GEN_NAME_SIZE }
207 };
208 const SecAsn1Template kSecAsn1DNSNameTemplate[] = {
209 { SEC_ASN1_CONTEXT_SPECIFIC | NGT_DNSName,
210 NSS_GEN_NAME_OFFSET, kSecAsn1IA5StringTemplate, NSS_GEN_NAME_SIZE }
211 };
212 const SecAsn1Template kSecAsn1X400AddressTemplate[] = {
213 { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | NGT_X400Address,
214 NSS_GEN_NAME_OFFSET, kSecAsn1AnyTemplate, NSS_GEN_NAME_SIZE }
215 };
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 }
220 };
221 const SecAsn1Template kSecAsn1EdiPartyNameTemplate[] = {
222 { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | NGT_EdiPartyName,
223 NSS_GEN_NAME_OFFSET, kSecAsn1AnyTemplate, NSS_GEN_NAME_SIZE }
224 };
225 const SecAsn1Template kSecAsn1URITemplate[] = {
226 { SEC_ASN1_CONTEXT_SPECIFIC | NGT_URI,
227 NSS_GEN_NAME_OFFSET, kSecAsn1IA5StringTemplate, NSS_GEN_NAME_SIZE }
228 };
229 const SecAsn1Template kSecAsn1IPAddressTemplate[] = {
230 { SEC_ASN1_CONTEXT_SPECIFIC | NGT_IPAddress,
231 NSS_GEN_NAME_OFFSET, kSecAsn1OctetStringTemplate, NSS_GEN_NAME_SIZE }
232 };
233 const SecAsn1Template kSecAsn1RegisteredIDTemplate[] = {
234 { SEC_ASN1_CONTEXT_SPECIFIC | NGT_RegisteredID,
235 NSS_GEN_NAME_OFFSET, kSecAsn1ObjectIDTemplate, NSS_GEN_NAME_SIZE }
236 };
237
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 },
248 { 0, NULL}
249 };
250
251 static const SecAsn1Template * NSS_genNameChooser(
252 void *arg,
253 Boolean enc,
254 const char *buf,
255 size_t len,
256 void *dest)
257 {
258 return SecAsn1TaggedTemplateChooser(arg, enc, buf, len, dest, genNameChoices);
259 }
260
261 static const SecAsn1TemplateChooserPtr NSS_genNameChooserPtr =
262 NSS_genNameChooser;
263
264 const SecAsn1Template kSecAsn1GeneralNameTemplate[] = {
265 { SEC_ASN1_DYNAMIC | SEC_ASN1_CONTEXT_SPECIFIC,
266 offsetof(NSS_GeneralName,item), // Needed?
267 &NSS_genNameChooserPtr },
268 { 0, } // Needed?
269 };