summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_name.c
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
committercvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
commitdecf84ba5550c1656a7fdb51b5b81969590c3f03 (patch)
tree44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/asn1/x_name.c
parent7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff)
downloadopenbsd-libressl-v2.1.5.tar.gz
openbsd-libressl-v2.1.5.tar.bz2
openbsd-libressl-v2.1.5.zip
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libcrypto/asn1/x_name.c')
-rw-r--r--src/lib/libcrypto/asn1/x_name.c609
1 files changed, 0 insertions, 609 deletions
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c
deleted file mode 100644
index 51c5a0ae41..0000000000
--- a/src/lib/libcrypto/asn1/x_name.c
+++ /dev/null
@@ -1,609 +0,0 @@
1/* $OpenBSD: x_name.c,v 1.29 2015/02/14 15:29:29 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
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.
8 *
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).
15 *
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.
22 *
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)"
40 *
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.
52 *
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.]
57 */
58
59#include <ctype.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/asn1t.h>
64#include <openssl/err.h>
65#include <openssl/x509.h>
66
67#include "asn1_locl.h"
68
69typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
70DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
71
72static 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);
75
76static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
77 const ASN1_ITEM *it, int tag, int aclass);
78static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
79static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
80
81static int x509_name_encode(X509_NAME *a);
82static int x509_name_canon(X509_NAME *a);
83static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
84static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname,
85 unsigned char **in);
86
87static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent,
88 const char *fname, const ASN1_PCTX *pctx);
89
90static const ASN1_TEMPLATE X509_NAME_ENTRY_seq_tt[] = {
91 {
92 .offset = offsetof(X509_NAME_ENTRY, object),
93 .field_name = "object",
94 .item = &ASN1_OBJECT_it,
95 },
96 {
97 .offset = offsetof(X509_NAME_ENTRY, value),
98 .field_name = "value",
99 .item = &ASN1_PRINTABLE_it,
100 },
101};
102
103const 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",
110};
111
112
113X509_NAME_ENTRY *
114d2i_X509_NAME_ENTRY(X509_NAME_ENTRY **a, const unsigned char **in, long len)
115{
116 return (X509_NAME_ENTRY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
117 &X509_NAME_ENTRY_it);
118}
119
120int
121i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a, unsigned char **out)
122{
123 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_ENTRY_it);
124}
125
126X509_NAME_ENTRY *
127X509_NAME_ENTRY_new(void)
128{
129 return (X509_NAME_ENTRY *)ASN1_item_new(&X509_NAME_ENTRY_it);
130}
131
132void
133X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
134{
135 ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_ENTRY_it);
136}
137
138X509_NAME_ENTRY *
139X509_NAME_ENTRY_dup(X509_NAME_ENTRY *x)
140{
141 return ASN1_item_dup(&X509_NAME_ENTRY_it, x);
142}
143
144/* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
145 * so declare two template wrappers for this
146 */
147
148ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
149 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
150ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
151
152ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
153 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
154ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
155
156/* Normally that's where it would end: we'd have two nested STACK structures
157 * representing the ASN1. Unfortunately X509_NAME uses a completely different
158 * form and caches encodings so we have to process the internal form and convert
159 * to the external form.
160 */
161
162const ASN1_EXTERN_FUNCS x509_name_ff = {
163 NULL,
164 x509_name_ex_new,
165 x509_name_ex_free,
166 0, /* Default clear behaviour is OK */
167 x509_name_ex_d2i,
168 x509_name_ex_i2d,
169 x509_name_ex_print
170};
171
172const ASN1_ITEM X509_NAME_it = {
173 .itype = ASN1_ITYPE_EXTERN,
174 .utype = V_ASN1_SEQUENCE,
175 .templates = NULL,
176 .tcount = 0,
177 .funcs = &x509_name_ff,
178 .size = 0,
179 .sname = "X509_NAME",
180};
181
182X509_NAME *
183d2i_X509_NAME(X509_NAME **a, const unsigned char **in, long len)
184{
185 return (X509_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
186 &X509_NAME_it);
187}
188
189int
190i2d_X509_NAME(X509_NAME *a, unsigned char **out)
191{
192 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_it);
193}
194
195X509_NAME *
196X509_NAME_new(void)
197{
198 return (X509_NAME *)ASN1_item_new(&X509_NAME_it);
199}
200
201void
202X509_NAME_free(X509_NAME *a)
203{
204 ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_it);
205}
206
207X509_NAME *
208X509_NAME_dup(X509_NAME *x)
209{
210 return ASN1_item_dup(&X509_NAME_it, x);
211}
212
213static int
214x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
215{
216 X509_NAME *ret = NULL;
217
218 ret = malloc(sizeof(X509_NAME));
219 if (!ret)
220 goto memerr;
221 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
222 goto memerr;
223 if ((ret->bytes = BUF_MEM_new()) == NULL)
224 goto memerr;
225 ret->canon_enc = NULL;
226 ret->canon_enclen = 0;
227 ret->modified = 1;
228 *val = (ASN1_VALUE *)ret;
229 return 1;
230
231memerr:
232 ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
233 if (ret) {
234 if (ret->entries)
235 sk_X509_NAME_ENTRY_free(ret->entries);
236 free(ret);
237 }
238 return 0;
239}
240
241static void
242x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
243{
244 X509_NAME *a;
245
246 if (!pval || !*pval)
247 return;
248 a = (X509_NAME *)*pval;
249
250 BUF_MEM_free(a->bytes);
251 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
252 free(a->canon_enc);
253 free(a);
254 *pval = NULL;
255}
256
257static int
258x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
259 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
260{
261 const unsigned char *p = *in, *q;
262 union {
263 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
264 ASN1_VALUE *a;
265 } intname = {NULL};
266 union {
267 X509_NAME *x;
268 ASN1_VALUE *a;
269 } nm = {NULL};
270 int i, j, ret;
271 STACK_OF(X509_NAME_ENTRY) *entries;
272 X509_NAME_ENTRY *entry;
273 q = p;
274
275 /* Get internal representation of Name */
276 ret = ASN1_item_ex_d2i(&intname.a, &p, len,
277 ASN1_ITEM_rptr(X509_NAME_INTERNAL), tag, aclass, opt, ctx);
278
279 if (ret <= 0)
280 return ret;
281
282 if (*val)
283 x509_name_ex_free(val, NULL);
284 if (!x509_name_ex_new(&nm.a, NULL))
285 goto err;
286 /* We've decoded it: now cache encoding */
287 if (!BUF_MEM_grow(nm.x->bytes, p - q))
288 goto err;
289 memcpy(nm.x->bytes->data, q, p - q);
290
291 /* Convert internal representation to X509_NAME structure */
292 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
293 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
294 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
295 entry = sk_X509_NAME_ENTRY_value(entries, j);
296 entry->set = i;
297 if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
298 goto err;
299 }
300 sk_X509_NAME_ENTRY_free(entries);
301 }
302 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
303 ret = x509_name_canon(nm.x);
304 if (!ret)
305 goto err;
306 nm.x->modified = 0;
307 *val = nm.a;
308 *in = p;
309 return ret;
310
311err:
312 if (nm.x != NULL)
313 X509_NAME_free(nm.x);
314 ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
315 return 0;
316}
317
318static int
319x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it,
320 int tag, int aclass)
321{
322 int ret;
323 X509_NAME *a = (X509_NAME *)*val;
324
325 if (a->modified) {
326 ret = x509_name_encode(a);
327 if (ret < 0)
328 return ret;
329 ret = x509_name_canon(a);
330 if (ret < 0)
331 return ret;
332 }
333 ret = a->bytes->length;
334 if (out != NULL) {
335 memcpy(*out, a->bytes->data, ret);
336 *out += ret;
337 }
338 return ret;
339}
340
341static void
342local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
343{
344 sk_X509_NAME_ENTRY_free(ne);
345}
346
347static void
348local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
349{
350 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
351}
352
353static int
354x509_name_encode(X509_NAME *a)
355{
356 union {
357 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
358 ASN1_VALUE *a;
359 } intname = {NULL};
360 int len;
361 unsigned char *p;
362 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
363 X509_NAME_ENTRY *entry;
364 int i, set = -1;
365
366 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
367 if (!intname.s)
368 goto memerr;
369 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
370 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
371 if (entry->set != set) {
372 entries = sk_X509_NAME_ENTRY_new_null();
373 if (!entries)
374 goto memerr;
375 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s,
376 entries))
377 goto memerr;
378 set = entry->set;
379 }
380 if (!sk_X509_NAME_ENTRY_push(entries, entry))
381 goto memerr;
382 }
383 len = ASN1_item_ex_i2d(&intname.a, NULL,
384 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
385 if (!BUF_MEM_grow(a->bytes, len))
386 goto memerr;
387 p = (unsigned char *)a->bytes->data;
388 ASN1_item_ex_i2d(&intname.a, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
389 -1, -1);
390 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
391 local_sk_X509_NAME_ENTRY_free);
392 a->modified = 0;
393 return len;
394
395memerr:
396 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
397 local_sk_X509_NAME_ENTRY_free);
398 ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
399 return -1;
400}
401
402static int
403x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, const char *fname,
404 const ASN1_PCTX *pctx)
405{
406 if (X509_NAME_print_ex(out, (X509_NAME *)*pval, indent,
407 pctx->nm_flags) <= 0)
408 return 0;
409 return 2;
410}
411
412/* This function generates the canonical encoding of the Name structure.
413 * In it all strings are converted to UTF8, leading, trailing and
414 * multiple spaces collapsed, converted to lower case and the leading
415 * SEQUENCE header removed.
416 *
417 * In future we could also normalize the UTF8 too.
418 *
419 * By doing this comparison of Name structures can be rapidly
420 * performed by just using memcmp() of the canonical encoding.
421 * By omitting the leading SEQUENCE name constraints of type
422 * dirName can also be checked with a simple memcmp().
423 */
424
425static int
426x509_name_canon(X509_NAME *a)
427{
428 unsigned char *p;
429 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
430 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
431 X509_NAME_ENTRY *entry, *tmpentry = NULL;
432 int i, len, set = -1, ret = 0;
433
434 if (a->canon_enc) {
435 free(a->canon_enc);
436 a->canon_enc = NULL;
437 }
438 /* Special case: empty X509_NAME => null encoding */
439 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
440 a->canon_enclen = 0;
441 return 1;
442 }
443 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
444 if (!intname)
445 goto err;
446 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
447 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
448 if (entry->set != set) {
449 entries = sk_X509_NAME_ENTRY_new_null();
450 if (!entries)
451 goto err;
452 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries))
453 goto err;
454 set = entry->set;
455 }
456 tmpentry = X509_NAME_ENTRY_new();
457 if (tmpentry == NULL)
458 goto err;
459 tmpentry->object = OBJ_dup(entry->object);
460 if (tmpentry->object == NULL)
461 goto err;
462 if (!asn1_string_canon(tmpentry->value, entry->value))
463 goto err;
464 if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
465 goto err;
466 tmpentry = NULL;
467 }
468
469 /* Finally generate encoding */
470 len = i2d_name_canon(intname, NULL);
471 if (len < 0)
472 goto err;
473 p = malloc(len);
474 if (p == NULL)
475 goto err;
476 a->canon_enc = p;
477 a->canon_enclen = len;
478 i2d_name_canon(intname, &p);
479 ret = 1;
480
481err:
482 if (tmpentry)
483 X509_NAME_ENTRY_free(tmpentry);
484 if (intname)
485 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
486 local_sk_X509_NAME_ENTRY_pop_free);
487 return ret;
488}
489
490/* Bitmap of all the types of string that will be canonicalized. */
491
492#define ASN1_MASK_CANON \
493 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
494 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
495 | B_ASN1_VISIBLESTRING)
496
497
498static int
499asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
500{
501 unsigned char *to, *from;
502 int len, i;
503
504 /* If type not in bitmask just copy string across */
505 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
506 if (!ASN1_STRING_copy(out, in))
507 return 0;
508 return 1;
509 }
510
511 out->type = V_ASN1_UTF8STRING;
512 out->length = ASN1_STRING_to_UTF8(&out->data, in);
513 if (out->length == -1)
514 return 0;
515
516 to = out->data;
517 from = to;
518
519 len = out->length;
520
521 /* Convert string in place to canonical form.
522 * Ultimately we may need to handle a wider range of characters
523 * but for now ignore anything with MSB set and rely on the
524 * isspace() and tolower() functions.
525 */
526
527 /* Ignore leading spaces */
528 while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
529 from++;
530 len--;
531 }
532
533 to = from + len - 1;
534
535 /* Ignore trailing spaces */
536 while ((len > 0) && !(*to & 0x80) && isspace(*to)) {
537 to--;
538 len--;
539 }
540
541 to = out->data;
542
543 i = 0;
544 while (i < len) {
545 /* If MSB set just copy across */
546 if (*from & 0x80) {
547 *to++ = *from++;
548 i++;
549 }
550 /* Collapse multiple spaces */
551 else if (isspace(*from)) {
552 /* Copy one space across */
553 *to++ = ' ';
554 /* Ignore subsequent spaces. Note: don't need to
555 * check len here because we know the last
556 * character is a non-space so we can't overflow.
557 */
558 do {
559 from++;
560 i++;
561 } while (!(*from & 0x80) && isspace(*from));
562 } else {
563 *to++ = tolower(*from);
564 from++;
565 i++;
566 }
567 }
568
569 out->length = to - out->data;
570
571 return 1;
572}
573
574static int
575i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, unsigned char **in)
576{
577 int i, len, ltmp;
578 ASN1_VALUE *v;
579 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
580
581 len = 0;
582 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
583 v = sk_ASN1_VALUE_value(intname, i);
584 ltmp = ASN1_item_ex_i2d(&v, in,
585 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
586 if (ltmp < 0)
587 return ltmp;
588 len += ltmp;
589 }
590 return len;
591}
592
593int
594X509_NAME_set(X509_NAME **xn, X509_NAME *name)
595{
596 X509_NAME *in;
597
598 if (!xn || !name)
599 return (0);
600
601 if (*xn != name) {
602 in = X509_NAME_dup(name);
603 if (in != NULL) {
604 X509_NAME_free(*xn);
605 *xn = in;
606 }
607 }
608 return (*xn != NULL);
609}