1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <ctype.h>
58
59 #include <openssl/asn1.h>
60 #include <openssl/asn1t.h>
61 #include <openssl/buf.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/stack.h>
66 #include <openssl/x509.h>
67
68 #include "../asn1/asn1_locl.h"
69
70
71 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
72 DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
73
74 static int x509_name_ex_d2i(ASN1_VALUE **val,
75 const unsigned char **in, long len,
76 const ASN1_ITEM *it,
77 int tag, int aclass, char opt, ASN1_TLC *ctx);
78
79 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
80 const ASN1_ITEM *it, int tag, int aclass);
81 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
82 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
83
84 static int x509_name_encode(X509_NAME *a);
85 static int x509_name_canon(X509_NAME *a);
86 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
87 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
88 unsigned char **in);
89
90
91 static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
92 int indent,
93 const char *fname,
94 const ASN1_PCTX *pctx);
95
96 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
97 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
98 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
99 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
100
101 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
102 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
103
104 /* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
105 * so declare two template wrappers for this
106 */
107
108 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
109 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
110 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
111
112 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
113 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
114 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
115
116 /* Normally that's where it would end: we'd have two nested STACK structures
117 * representing the ASN1. Unfortunately X509_NAME uses a completely different
118 * form and caches encodings so we have to process the internal form and convert
119 * to the external form.
120 */
121
122 const ASN1_EXTERN_FUNCS x509_name_ff = {
123 NULL,
124 x509_name_ex_new,
125 x509_name_ex_free,
126 0, /* Default clear behaviour is OK */
127 x509_name_ex_d2i,
128 x509_name_ex_i2d,
129 x509_name_ex_print
130 };
131
IMPLEMENT_EXTERN_ASN1(X509_NAME,V_ASN1_SEQUENCE,x509_name_ff)132 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
133
134 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
135 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
136
137 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
138 {
139 X509_NAME *ret = NULL;
140 ret = OPENSSL_malloc(sizeof(X509_NAME));
141 if(!ret) goto memerr;
142 if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
143 goto memerr;
144 if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr;
145 ret->canon_enc = NULL;
146 ret->canon_enclen = 0;
147 ret->modified=1;
148 *val = (ASN1_VALUE *)ret;
149 return 1;
150
151 memerr:
152 OPENSSL_PUT_ERROR(X509, x509_name_ex_new, ERR_R_MALLOC_FAILURE);
153 if (ret)
154 {
155 if (ret->entries)
156 sk_X509_NAME_ENTRY_free(ret->entries);
157 OPENSSL_free(ret);
158 }
159 return 0;
160 }
161
x509_name_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)162 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
163 {
164 X509_NAME *a;
165 if(!pval || !*pval)
166 return;
167 a = (X509_NAME *)*pval;
168
169 BUF_MEM_free(a->bytes);
170 sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
171 if (a->canon_enc)
172 OPENSSL_free(a->canon_enc);
173 OPENSSL_free(a);
174 *pval = NULL;
175 }
176
x509_name_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)177 static int x509_name_ex_d2i(ASN1_VALUE **val,
178 const unsigned char **in, long len, const ASN1_ITEM *it,
179 int tag, int aclass, char opt, ASN1_TLC *ctx)
180 {
181 const unsigned char *p = *in, *q;
182 union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
183 ASN1_VALUE *a; } intname = {NULL};
184 union { X509_NAME *x; ASN1_VALUE *a; } nm = {NULL};
185 size_t i, j;
186 int ret;
187 STACK_OF(X509_NAME_ENTRY) *entries;
188 X509_NAME_ENTRY *entry;
189 q = p;
190
191 /* Get internal representation of Name */
192 ret = ASN1_item_ex_d2i(&intname.a,
193 &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
194 tag, aclass, opt, ctx);
195
196 if(ret <= 0) return ret;
197
198 if(*val) x509_name_ex_free(val, NULL);
199 if(!x509_name_ex_new(&nm.a, NULL)) goto err;
200 /* We've decoded it: now cache encoding */
201 if(!BUF_MEM_grow(nm.x->bytes, p - q)) goto err;
202 memcpy(nm.x->bytes->data, q, p - q);
203
204 /* Convert internal representation to X509_NAME structure */
205 for(i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
206 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
207 for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
208 entry = sk_X509_NAME_ENTRY_value(entries, j);
209 entry->set = i;
210 if(!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
211 goto err;
212 }
213 sk_X509_NAME_ENTRY_free(entries);
214 }
215 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
216 ret = x509_name_canon(nm.x);
217 if (!ret)
218 goto err;
219 nm.x->modified = 0;
220 *val = nm.a;
221 *in = p;
222 return ret;
223 err:
224 if (nm.x != NULL)
225 X509_NAME_free(nm.x);
226 OPENSSL_PUT_ERROR(X509, x509_name_ex_d2i, ERR_R_ASN1_LIB);
227 return 0;
228 }
229
x509_name_ex_i2d(ASN1_VALUE ** val,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)230 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
231 {
232 int ret;
233 X509_NAME *a = (X509_NAME *)*val;
234 if(a->modified) {
235 ret = x509_name_encode(a);
236 if(ret < 0)
237 return ret;
238 ret = x509_name_canon(a);
239 if(ret < 0)
240 return ret;
241 }
242 ret = a->bytes->length;
243 if(out != NULL) {
244 memcpy(*out,a->bytes->data,ret);
245 *out+=ret;
246 }
247 return ret;
248 }
249
local_sk_X509_NAME_ENTRY_free(STACK_OF (X509_NAME_ENTRY)* ne)250 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
251 {
252 sk_X509_NAME_ENTRY_free(ne);
253 }
254
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF (X509_NAME_ENTRY)* ne)255 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
256 {
257 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
258 }
259
x509_name_encode(X509_NAME * a)260 static int x509_name_encode(X509_NAME *a)
261 {
262 union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
263 ASN1_VALUE *a; } intname = {NULL};
264 int len;
265 unsigned char *p;
266 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
267 X509_NAME_ENTRY *entry;
268 int set = -1;
269 size_t i;
270 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
271 if(!intname.s) goto memerr;
272 for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
273 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
274 if(entry->set != set) {
275 entries = sk_X509_NAME_ENTRY_new_null();
276 if(!entries) goto memerr;
277 if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s,
278 entries))
279 goto memerr;
280 set = entry->set;
281 }
282 if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
283 }
284 len = ASN1_item_ex_i2d(&intname.a, NULL,
285 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
286 if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
287 p=(unsigned char *)a->bytes->data;
288 ASN1_item_ex_i2d(&intname.a,
289 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
290 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
291 local_sk_X509_NAME_ENTRY_free);
292 a->modified = 0;
293 return len;
294 memerr:
295 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
296 local_sk_X509_NAME_ENTRY_free);
297 OPENSSL_PUT_ERROR(X509, x509_name_encode, ERR_R_MALLOC_FAILURE);
298 return -1;
299 }
300
x509_name_ex_print(BIO * out,ASN1_VALUE ** pval,int indent,const char * fname,const ASN1_PCTX * pctx)301 static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval,
302 int indent,
303 const char *fname,
304 const ASN1_PCTX *pctx)
305 {
306 if (X509_NAME_print_ex(out, (X509_NAME *)*pval,
307 indent, pctx->nm_flags) <= 0)
308 return 0;
309 return 2;
310 }
311
312 /* This function generates the canonical encoding of the Name structure.
313 * In it all strings are converted to UTF8, leading, trailing and
314 * multiple spaces collapsed, converted to lower case and the leading
315 * SEQUENCE header removed.
316 *
317 * In future we could also normalize the UTF8 too.
318 *
319 * By doing this comparison of Name structures can be rapidly
320 * perfomed by just using memcmp() of the canonical encoding.
321 * By omitting the leading SEQUENCE name constraints of type
322 * dirName can also be checked with a simple memcmp().
323 */
324
x509_name_canon(X509_NAME * a)325 static int x509_name_canon(X509_NAME *a)
326 {
327 unsigned char *p;
328 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
329 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
330 X509_NAME_ENTRY *entry, *tmpentry = NULL;
331 int set = -1, ret = 0;
332 size_t i;
333
334 if (a->canon_enc)
335 {
336 OPENSSL_free(a->canon_enc);
337 a->canon_enc = NULL;
338 }
339 /* Special case: empty X509_NAME => null encoding */
340 if (sk_X509_NAME_ENTRY_num(a->entries) == 0)
341 {
342 a->canon_enclen = 0;
343 return 1;
344 }
345 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
346 if(!intname)
347 goto err;
348 for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++)
349 {
350 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
351 if(entry->set != set)
352 {
353 entries = sk_X509_NAME_ENTRY_new_null();
354 if(!entries)
355 goto err;
356 if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries))
357 goto err;
358 set = entry->set;
359 }
360 tmpentry = X509_NAME_ENTRY_new();
361 tmpentry->object = OBJ_dup(entry->object);
362 if (!asn1_string_canon(tmpentry->value, entry->value))
363 goto err;
364 if(!sk_X509_NAME_ENTRY_push(entries, tmpentry))
365 goto err;
366 tmpentry = NULL;
367 }
368
369 /* Finally generate encoding */
370
371 a->canon_enclen = i2d_name_canon(intname, NULL);
372
373 p = OPENSSL_malloc(a->canon_enclen);
374
375 if (!p)
376 goto err;
377
378 a->canon_enc = p;
379
380 i2d_name_canon(intname, &p);
381
382 ret = 1;
383
384 err:
385
386 if (tmpentry)
387 X509_NAME_ENTRY_free(tmpentry);
388 if (intname)
389 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
390 local_sk_X509_NAME_ENTRY_pop_free);
391 return ret;
392 }
393
394 /* Bitmap of all the types of string that will be canonicalized. */
395
396 #define ASN1_MASK_CANON \
397 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
398 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
399 | B_ASN1_VISIBLESTRING)
400
401
asn1_string_canon(ASN1_STRING * out,ASN1_STRING * in)402 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
403 {
404 unsigned char *to, *from;
405 int len, i;
406
407 /* If type not in bitmask just copy string across */
408 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON))
409 {
410 if (!ASN1_STRING_copy(out, in))
411 return 0;
412 return 1;
413 }
414
415 out->type = V_ASN1_UTF8STRING;
416 out->length = ASN1_STRING_to_UTF8(&out->data, in);
417 if (out->length == -1)
418 return 0;
419
420 to = out->data;
421 from = to;
422
423 len = out->length;
424
425 /* Convert string in place to canonical form.
426 * Ultimately we may need to handle a wider range of characters
427 * but for now ignore anything with MSB set and rely on the
428 * isspace() and tolower() functions.
429 */
430
431 /* Ignore leading spaces */
432 while((len > 0) && !(*from & 0x80) && isspace(*from))
433 {
434 from++;
435 len--;
436 }
437
438 to = from + len - 1;
439
440 /* Ignore trailing spaces */
441 while ((len > 0) && !(*to & 0x80) && isspace(*to))
442 {
443 to--;
444 len--;
445 }
446
447 to = out->data;
448
449 i = 0;
450 while(i < len)
451 {
452 /* If MSB set just copy across */
453 if (*from & 0x80)
454 {
455 *to++ = *from++;
456 i++;
457 }
458 /* Collapse multiple spaces */
459 else if (isspace(*from))
460 {
461 /* Copy one space across */
462 *to++ = ' ';
463 /* Ignore subsequent spaces. Note: don't need to
464 * check len here because we know the last
465 * character is a non-space so we can't overflow.
466 */
467 do
468 {
469 from++;
470 i++;
471 }
472 while(!(*from & 0x80) && isspace(*from));
473 }
474 else
475 {
476 *to++ = tolower(*from);
477 from++;
478 i++;
479 }
480 }
481
482 out->length = to - out->data;
483
484 return 1;
485
486 }
487
i2d_name_canon(STACK_OF (STACK_OF_X509_NAME_ENTRY)* _intname,unsigned char ** in)488 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname,
489 unsigned char **in)
490 {
491 int len, ltmp;
492 size_t i;
493 ASN1_VALUE *v;
494 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
495
496 len = 0;
497 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++)
498 {
499 v = sk_ASN1_VALUE_value(intname, i);
500 ltmp = ASN1_item_ex_i2d(&v, in,
501 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
502 if (ltmp < 0)
503 return ltmp;
504 len += ltmp;
505 }
506 return len;
507 }
508
X509_NAME_set(X509_NAME ** xn,X509_NAME * name)509 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
510 {
511 X509_NAME *in;
512
513 if (!xn || !name) return(0);
514
515 if (*xn != name)
516 {
517 in=X509_NAME_dup(name);
518 if (in != NULL)
519 {
520 X509_NAME_free(*xn);
521 *xn=in;
522 }
523 }
524 return(*xn != NULL);
525 }
526
527 IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)
528