Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / crypto / asn1 / x_name.c
bloba7ce6d4f77759cf8b78752c1a5934f4cf9022721
1 /* $OpenBSD: x_name.c,v 1.30 2015/07/15 17:41:56 miod Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 #include <ctype.h>
60 #include <stdio.h>
61 #include <string.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/err.h>
65 #include <openssl/x509.h>
67 #include "asn1_locl.h"
69 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
70 DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
72 static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
73 long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
74 ASN1_TLC *ctx);
76 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
77 const ASN1_ITEM *it, int tag, int aclass);
78 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
79 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
81 static int x509_name_encode(X509_NAME *a);
82 static int x509_name_canon(X509_NAME *a);
83 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
84 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
85 unsigned char **in);
87 static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent,
88 const char *fname, const ASN1_PCTX *pctx);
90 static const ASN1_TEMPLATE X509_NAME_ENTRY_seq_tt[] = {
92 .offset = offsetof(X509_NAME_ENTRY, object),
93 .field_name = "object",
94 .item = &ASN1_OBJECT_it,
97 .offset = offsetof(X509_NAME_ENTRY, value),
98 .field_name = "value",
99 .item = &ASN1_PRINTABLE_it,
103 const ASN1_ITEM X509_NAME_ENTRY_it = {
104 .itype = ASN1_ITYPE_SEQUENCE,
105 .utype = V_ASN1_SEQUENCE,
106 .templates = X509_NAME_ENTRY_seq_tt,
107 .tcount = sizeof(X509_NAME_ENTRY_seq_tt) / sizeof(ASN1_TEMPLATE),
108 .size = sizeof(X509_NAME_ENTRY),
109 .sname = "X509_NAME_ENTRY",
113 X509_NAME_ENTRY *
114 d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **a, const unsigned char **in, long len)
116 return (X509_NAME_ENTRY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
117 &X509_NAME_ENTRY_it);
121 i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a, unsigned char **out)
123 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_ENTRY_it);
126 X509_NAME_ENTRY *
127 X509_NAME_ENTRY_new(void)
129 return (X509_NAME_ENTRY *)ASN1_item_new(&X509_NAME_ENTRY_it);
132 void
133 X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
135 ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_ENTRY_it);
138 X509_NAME_ENTRY *
139 X509_NAME_ENTRY_dup(X509_NAME_ENTRY *x)
141 return ASN1_item_dup(&X509_NAME_ENTRY_it, x);
144 /* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
145 * so declare two template wrappers for this
148 static const ASN1_TEMPLATE X509_NAME_ENTRIES_item_tt = {
149 .flags = ASN1_TFLG_SET_OF,
150 .tag = 0,
151 .offset = 0,
152 .field_name = "RDNS",
153 .item = &X509_NAME_ENTRY_it,
156 const ASN1_ITEM X509_NAME_ENTRIES_it = {
157 .itype = ASN1_ITYPE_PRIMITIVE,
158 .utype = -1,
159 .templates = &X509_NAME_ENTRIES_item_tt,
160 .tcount = 0,
161 .funcs = NULL,
162 .size = 0,
163 .sname = "X509_NAME_ENTRIES",
166 static const ASN1_TEMPLATE X509_NAME_INTERNAL_item_tt = {
167 .flags = ASN1_TFLG_SEQUENCE_OF,
168 .tag = 0,
169 .offset = 0,
170 .field_name = "Name",
171 .item = &X509_NAME_ENTRIES_it,
174 const ASN1_ITEM X509_NAME_INTERNAL_it = {
175 .itype = ASN1_ITYPE_PRIMITIVE,
176 .utype = -1,
177 .templates = &X509_NAME_INTERNAL_item_tt,
178 .tcount = 0,
179 .funcs = NULL,
180 .size = 0,
181 .sname = "X509_NAME_INTERNAL",
184 /* Normally that's where it would end: we'd have two nested STACK structures
185 * representing the ASN1. Unfortunately X509_NAME uses a completely different
186 * form and caches encodings so we have to process the internal form and convert
187 * to the external form.
190 const ASN1_EXTERN_FUNCS x509_name_ff = {
191 NULL,
192 x509_name_ex_new,
193 x509_name_ex_free,
194 0, /* Default clear behaviour is OK */
195 x509_name_ex_d2i,
196 x509_name_ex_i2d,
197 x509_name_ex_print
200 const ASN1_ITEM X509_NAME_it = {
201 .itype = ASN1_ITYPE_EXTERN,
202 .utype = V_ASN1_SEQUENCE,
203 .templates = NULL,
204 .tcount = 0,
205 .funcs = &x509_name_ff,
206 .size = 0,
207 .sname = "X509_NAME",
210 X509_NAME *
211 d2i_X509_NAME(X509_NAME **a, const unsigned char **in, long len)
213 return (X509_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
214 &X509_NAME_it);
218 i2d_X509_NAME(X509_NAME *a, unsigned char **out)
220 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_it);
223 X509_NAME *
224 X509_NAME_new(void)
226 return (X509_NAME *)ASN1_item_new(&X509_NAME_it);
229 void
230 X509_NAME_free(X509_NAME *a)
232 ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_it);
235 X509_NAME *
236 X509_NAME_dup(X509_NAME *x)
238 return ASN1_item_dup(&X509_NAME_it, x);
241 static int
242 x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
244 X509_NAME *ret = NULL;
246 ret = malloc(sizeof(X509_NAME));
247 if (!ret)
248 goto memerr;
249 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
250 goto memerr;
251 if ((ret->bytes = BUF_MEM_new()) == NULL)
252 goto memerr;
253 ret->canon_enc = NULL;
254 ret->canon_enclen = 0;
255 ret->modified = 1;
256 *val = (ASN1_VALUE *)ret;
257 return 1;
259 memerr:
260 ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
261 if (ret) {
262 if (ret->entries)
263 sk_X509_NAME_ENTRY_free(ret->entries);
264 free(ret);
266 return 0;
269 static void
270 x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
272 X509_NAME *a;
274 if (!pval || !*pval)
275 return;
276 a = (X509_NAME *)*pval;
278 BUF_MEM_free(a->bytes);
279 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
280 free(a->canon_enc);
281 free(a);
282 *pval = NULL;
285 static int
286 x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
287 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
289 const unsigned char *p = *in, *q;
290 union {
291 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
292 ASN1_VALUE *a;
293 } intname = {NULL};
294 union {
295 X509_NAME *x;
296 ASN1_VALUE *a;
297 } nm = {NULL};
298 int i, j, ret;
299 STACK_OF(X509_NAME_ENTRY) *entries;
300 X509_NAME_ENTRY *entry;
301 q = p;
303 /* Get internal representation of Name */
304 ret = ASN1_item_ex_d2i(&intname.a, &p, len,
305 ASN1_ITEM_rptr(X509_NAME_INTERNAL), tag, aclass, opt, ctx);
307 if (ret <= 0)
308 return ret;
310 if (*val)
311 x509_name_ex_free(val, NULL);
312 if (!x509_name_ex_new(&nm.a, NULL))
313 goto err;
314 /* We've decoded it: now cache encoding */
315 if (!BUF_MEM_grow(nm.x->bytes, p - q))
316 goto err;
317 memcpy(nm.x->bytes->data, q, p - q);
319 /* Convert internal representation to X509_NAME structure */
320 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
321 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
322 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
323 entry = sk_X509_NAME_ENTRY_value(entries, j);
324 entry->set = i;
325 if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
326 goto err;
328 sk_X509_NAME_ENTRY_free(entries);
330 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
331 ret = x509_name_canon(nm.x);
332 if (!ret)
333 goto err;
334 nm.x->modified = 0;
335 *val = nm.a;
336 *in = p;
337 return ret;
339 err:
340 if (nm.x != NULL)
341 X509_NAME_free(nm.x);
342 ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
343 return 0;
346 static int
347 x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it,
348 int tag, int aclass)
350 int ret;
351 X509_NAME *a = (X509_NAME *)*val;
353 if (a->modified) {
354 ret = x509_name_encode(a);
355 if (ret < 0)
356 return ret;
357 ret = x509_name_canon(a);
358 if (ret < 0)
359 return ret;
361 ret = a->bytes->length;
362 if (out != NULL) {
363 memcpy(*out, a->bytes->data, ret);
364 *out += ret;
366 return ret;
369 static void
370 local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
372 sk_X509_NAME_ENTRY_free(ne);
375 static void
376 local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
378 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
381 static int
382 x509_name_encode(X509_NAME *a)
384 union {
385 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
386 ASN1_VALUE *a;
387 } intname = {NULL};
388 int len;
389 unsigned char *p;
390 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
391 X509_NAME_ENTRY *entry;
392 int i, set = -1;
394 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
395 if (!intname.s)
396 goto memerr;
397 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
398 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
399 if (entry->set != set) {
400 entries = sk_X509_NAME_ENTRY_new_null();
401 if (!entries)
402 goto memerr;
403 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s,
404 entries))
405 goto memerr;
406 set = entry->set;
408 if (entries == NULL /* if entry->set is bogusly -1 */ ||
409 !sk_X509_NAME_ENTRY_push(entries, entry))
410 goto memerr;
412 len = ASN1_item_ex_i2d(&intname.a, NULL,
413 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
414 if (!BUF_MEM_grow(a->bytes, len))
415 goto memerr;
416 p = (unsigned char *)a->bytes->data;
417 ASN1_item_ex_i2d(&intname.a, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
418 -1, -1);
419 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
420 local_sk_X509_NAME_ENTRY_free);
421 a->modified = 0;
422 return len;
424 memerr:
425 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
426 local_sk_X509_NAME_ENTRY_free);
427 ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
428 return -1;
431 static int
432 x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, const char *fname,
433 const ASN1_PCTX *pctx)
435 if (X509_NAME_print_ex(out, (X509_NAME *)*pval, indent,
436 pctx->nm_flags) <= 0)
437 return 0;
438 return 2;
441 /* This function generates the canonical encoding of the Name structure.
442 * In it all strings are converted to UTF8, leading, trailing and
443 * multiple spaces collapsed, converted to lower case and the leading
444 * SEQUENCE header removed.
446 * In future we could also normalize the UTF8 too.
448 * By doing this comparison of Name structures can be rapidly
449 * performed by just using memcmp() of the canonical encoding.
450 * By omitting the leading SEQUENCE name constraints of type
451 * dirName can also be checked with a simple memcmp().
454 static int
455 x509_name_canon(X509_NAME *a)
457 unsigned char *p;
458 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
459 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
460 X509_NAME_ENTRY *entry, *tmpentry = NULL;
461 int i, len, set = -1, ret = 0;
463 if (a->canon_enc) {
464 free(a->canon_enc);
465 a->canon_enc = NULL;
467 /* Special case: empty X509_NAME => null encoding */
468 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
469 a->canon_enclen = 0;
470 return 1;
472 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
473 if (!intname)
474 goto err;
475 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
476 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
477 if (entry->set != set) {
478 entries = sk_X509_NAME_ENTRY_new_null();
479 if (!entries)
480 goto err;
481 if (sk_STACK_OF_X509_NAME_ENTRY_push(intname,
482 entries) == 0) {
483 sk_X509_NAME_ENTRY_free(entries);
484 goto err;
486 set = entry->set;
488 tmpentry = X509_NAME_ENTRY_new();
489 if (tmpentry == NULL)
490 goto err;
491 tmpentry->object = OBJ_dup(entry->object);
492 if (tmpentry->object == NULL)
493 goto err;
494 if (!asn1_string_canon(tmpentry->value, entry->value))
495 goto err;
496 if (entries == NULL /* if entry->set is bogusly -1 */ ||
497 !sk_X509_NAME_ENTRY_push(entries, tmpentry))
498 goto err;
499 tmpentry = NULL;
502 /* Finally generate encoding */
503 len = i2d_name_canon(intname, NULL);
504 if (len < 0)
505 goto err;
506 p = malloc(len);
507 if (p == NULL)
508 goto err;
509 a->canon_enc = p;
510 a->canon_enclen = len;
511 i2d_name_canon(intname, &p);
512 ret = 1;
514 err:
515 if (tmpentry)
516 X509_NAME_ENTRY_free(tmpentry);
517 if (intname)
518 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
519 local_sk_X509_NAME_ENTRY_pop_free);
520 return ret;
523 /* Bitmap of all the types of string that will be canonicalized. */
525 #define ASN1_MASK_CANON \
526 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
527 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
528 | B_ASN1_VISIBLESTRING)
531 static int
532 asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
534 unsigned char *to, *from;
535 int len, i;
537 /* If type not in bitmask just copy string across */
538 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
539 if (!ASN1_STRING_copy(out, in))
540 return 0;
541 return 1;
544 out->type = V_ASN1_UTF8STRING;
545 out->length = ASN1_STRING_to_UTF8(&out->data, in);
546 if (out->length == -1)
547 return 0;
549 to = out->data;
550 from = to;
552 len = out->length;
554 /* Convert string in place to canonical form.
555 * Ultimately we may need to handle a wider range of characters
556 * but for now ignore anything with MSB set and rely on the
557 * isspace() and tolower() functions.
560 /* Ignore leading spaces */
561 while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
562 from++;
563 len--;
566 to = from + len - 1;
568 /* Ignore trailing spaces */
569 while ((len > 0) && !(*to & 0x80) && isspace(*to)) {
570 to--;
571 len--;
574 to = out->data;
576 i = 0;
577 while (i < len) {
578 /* If MSB set just copy across */
579 if (*from & 0x80) {
580 *to++ = *from++;
581 i++;
583 /* Collapse multiple spaces */
584 else if (isspace(*from)) {
585 /* Copy one space across */
586 *to++ = ' ';
587 /* Ignore subsequent spaces. Note: don't need to
588 * check len here because we know the last
589 * character is a non-space so we can't overflow.
591 do {
592 from++;
593 i++;
594 } while (!(*from & 0x80) && isspace(*from));
595 } else {
596 *to++ = tolower(*from);
597 from++;
598 i++;
602 out->length = to - out->data;
604 return 1;
607 static int
608 i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, unsigned char **in)
610 int i, len, ltmp;
611 ASN1_VALUE *v;
612 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
614 len = 0;
615 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
616 v = sk_ASN1_VALUE_value(intname, i);
617 ltmp = ASN1_item_ex_i2d(&v, in,
618 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
619 if (ltmp < 0)
620 return ltmp;
621 len += ltmp;
623 return len;
627 X509_NAME_set(X509_NAME **xn, X509_NAME *name)
629 X509_NAME *in;
631 if (!xn || !name)
632 return (0);
634 if (*xn != name) {
635 in = X509_NAME_dup(name);
636 if (in != NULL) {
637 X509_NAME_free(*xn);
638 *xn = in;
641 return (*xn != NULL);