Debian Codegraph @

external/boringssl/crypto/x509/x_name.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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 #include <string.h>
59
60 #include <openssl/asn1.h>
61 #include <openssl/asn1t.h>
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68
69 #include "../asn1/asn1_locl.h"
70
71 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
72 DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
73
74 /*
75 * Maximum length of X509_NAME: much larger than anything we should
76 * ever see in practice.
77 */
78
79 #define X509_NAME_MAX (1024 * 1024)
80
81 static int x509_name_ex_d2i(ASN1_VALUE **val,
82 const unsigned char **in, long len,
83 const ASN1_ITEM *it,
84 int tag, int aclass, char opt, ASN1_TLC *ctx);
85
86 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
87 const ASN1_ITEM *it, int tag, int aclass);
88 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it);
89 static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it);
90
91 static int x509_name_encode(X509_NAME *a);
92 static int x509_name_canon(X509_NAME *a);
93 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in);
94 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname,
95 unsigned char **in);
96
97 ASN1_SEQUENCE(X509_NAME_ENTRY) = {
98 ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT),
99 ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE)
100 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
101
102 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
103 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
104
105 /*
106 * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
107 * declare two template wrappers for this
108 */
109
110 ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) =
111 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY)
112 ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES)
113
114 ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) =
115 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES)
116 ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL)
117
118 /*
119 * Normally that's where it would end: we'd have two nested STACK structures
120 * representing the ASN1. Unfortunately X509_NAME uses a completely different
121 * form and caches encodings so we have to process the internal form and
122 * convert to the external form.
123 */
124
125 static const ASN1_EXTERN_FUNCS x509_name_ff = {
126 NULL,
127 x509_name_ex_new,
128 x509_name_ex_free,
129 0, /* Default clear behaviour is OK */
130 x509_name_ex_d2i,
131 x509_name_ex_i2d,
132 NULL,
133 };
134
135 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff)
136
137 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
138
139 IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
140
141 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
142 {
143 X509_NAME *ret = NULL;
144 ret = OPENSSL_malloc(sizeof(X509_NAME));
145 if (!ret)
146 goto memerr;
147 if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
148 goto memerr;
149 if ((ret->bytes = BUF_MEM_new()) == NULL)
150 goto memerr;
151 ret->canon_enc = NULL;
152 ret->canon_enclen = 0;
153 ret->modified = 1;
154 *val = (ASN1_VALUE *)ret;
155 return 1;
156
157 memerr:
158 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
159 if (ret) {
160 if (ret->entries)
161 sk_X509_NAME_ENTRY_free(ret->entries);
162 OPENSSL_free(ret);
163 }
164 return 0;
165 }
166
167 static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
168 {
169 X509_NAME *a;
170 if (!pval || !*pval)
171 return;
172 a = (X509_NAME *)*pval;
173
174 BUF_MEM_free(a->bytes);
175 sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
176 if (a->canon_enc)
177 OPENSSL_free(a->canon_enc);
178 OPENSSL_free(a);
179 *pval = NULL;
180 }
181
182 static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
183 {
184 sk_X509_NAME_ENTRY_free(ne);
185 }
186
187 static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
188 {
189 sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
190 }
191
192 static int x509_name_ex_d2i(ASN1_VALUE **val,
193 const unsigned char **in, long len,
194 const ASN1_ITEM *it, int tag, int aclass,
195 char opt, ASN1_TLC *ctx)
196 {
197 const unsigned char *p = *in, *q;
198 union {
199 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
200 ASN1_VALUE *a;
201 } intname = {
202 NULL
203 };
204 union {
205 X509_NAME *x;
206 ASN1_VALUE *a;
207 } nm = {
208 NULL
209 };
210 size_t i, j;
211 int ret;
212 STACK_OF(X509_NAME_ENTRY) *entries;
213 X509_NAME_ENTRY *entry;
214 /* Bound the size of an X509_NAME we are willing to parse. */
215 if (len > X509_NAME_MAX) {
216 len = X509_NAME_MAX;
217 }
218 q = p;
219
220 /* Get internal representation of Name */
221 ret = ASN1_item_ex_d2i(&intname.a,
222 &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
223 tag, aclass, opt, ctx);
224
225 if (ret <= 0)
226 return ret;
227
228 if (*val)
229 x509_name_ex_free(val, NULL);
230 /* We've decoded it: now cache encoding */
231 if (!x509_name_ex_new(&nm.a, NULL) || !BUF_MEM_grow(nm.x->bytes, p - q)) {
232 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
233 local_sk_X509_NAME_ENTRY_pop_free);
234 goto err;
235 }
236 memcpy(nm.x->bytes->data, q, p - q);
237
238 /* Convert internal representation to X509_NAME structure */
239 for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
240 entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
241 for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
242 entry = sk_X509_NAME_ENTRY_value(entries, j);
243 entry->set = i;
244 if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
245 goto err;
246 }
247 sk_X509_NAME_ENTRY_free(entries);
248 }
249 sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
250 ret = x509_name_canon(nm.x);
251 if (!ret)
252 goto err;
253 nm.x->modified = 0;
254 *val = nm.a;
255 *in = p;
256 return ret;
257 err:
258 if (nm.x != NULL)
259 X509_NAME_free(nm.x);
260 OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
261 return 0;
262 }
263
264 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out,
265 const ASN1_ITEM *it, int tag, int aclass)
266 {
267 int ret;
268 X509_NAME *a = (X509_NAME *)*val;
269 if (a->modified) {
270 ret = x509_name_encode(a);
271 if (ret < 0)
272 return ret;
273 ret = x509_name_canon(a);
274 if (ret < 0)
275 return ret;
276 }
277 ret = a->bytes->length;
278 if (out != NULL) {
279 memcpy(*out, a->bytes->data, ret);
280 *out += ret;
281 }
282 return ret;
283 }
284
285 static int x509_name_encode(X509_NAME *a)
286 {
287 union {
288 STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
289 ASN1_VALUE *a;
290 } intname = {
291 NULL
292 };
293 int len;
294 unsigned char *p;
295 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
296 X509_NAME_ENTRY *entry;
297 int set = -1;
298 size_t i;
299 intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
300 if (!intname.s)
301 goto memerr;
302 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
303 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
304 if (entry->set != set) {
305 entries = sk_X509_NAME_ENTRY_new_null();
306 if (!entries)
307 goto memerr;
308 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries))
309 goto memerr;
310 set = entry->set;
311 }
312 if (!sk_X509_NAME_ENTRY_push(entries, entry))
313 goto memerr;
314 }
315 len = ASN1_item_ex_i2d(&intname.a, NULL,
316 ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
317 if (!BUF_MEM_grow(a->bytes, len))
318 goto memerr;
319 p = (unsigned char *)a->bytes->data;
320 ASN1_item_ex_i2d(&intname.a,
321 &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
322 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
323 local_sk_X509_NAME_ENTRY_free);
324 a->modified = 0;
325 return len;
326 memerr:
327 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
328 local_sk_X509_NAME_ENTRY_free);
329 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
330 return -1;
331 }
332
333 /*
334 * This function generates the canonical encoding of the Name structure. In
335 * it all strings are converted to UTF8, leading, trailing and multiple
336 * spaces collapsed, converted to lower case and the leading SEQUENCE header
337 * removed. In future we could also normalize the UTF8 too. By doing this
338 * comparison of Name structures can be rapidly perfomed by just using
339 * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name
340 * constraints of type dirName can also be checked with a simple memcmp().
341 */
342
343 static int x509_name_canon(X509_NAME *a)
344 {
345 unsigned char *p;
346 STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL;
347 STACK_OF(X509_NAME_ENTRY) *entries = NULL;
348 X509_NAME_ENTRY *entry, *tmpentry = NULL;
349 int set = -1, ret = 0;
350 size_t i;
351
352 if (a->canon_enc) {
353 OPENSSL_free(a->canon_enc);
354 a->canon_enc = NULL;
355 }
356 /* Special case: empty X509_NAME => null encoding */
357 if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
358 a->canon_enclen = 0;
359 return 1;
360 }
361 intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
362 if (!intname)
363 goto err;
364 for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
365 entry = sk_X509_NAME_ENTRY_value(a->entries, i);
366 if (entry->set != set) {
367 entries = sk_X509_NAME_ENTRY_new_null();
368 if (!entries)
369 goto err;
370 if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) {
371 sk_X509_NAME_ENTRY_free(entries);
372 goto err;
373 }
374 set = entry->set;
375 }
376 tmpentry = X509_NAME_ENTRY_new();
377 if (tmpentry == NULL)
378 goto err;
379 tmpentry->object = OBJ_dup(entry->object);
380 if (!asn1_string_canon(tmpentry->value, entry->value))
381 goto err;
382 if (!sk_X509_NAME_ENTRY_push(entries, tmpentry))
383 goto err;
384 tmpentry = NULL;
385 }
386
387 /* Finally generate encoding */
388
389 a->canon_enclen = i2d_name_canon(intname, NULL);
390
391 p = OPENSSL_malloc(a->canon_enclen);
392
393 if (!p)
394 goto err;
395
396 a->canon_enc = p;
397
398 i2d_name_canon(intname, &p);
399
400 ret = 1;
401
402 err:
403
404 if (tmpentry)
405 X509_NAME_ENTRY_free(tmpentry);
406 if (intname)
407 sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
408 local_sk_X509_NAME_ENTRY_pop_free);
409 return ret;
410 }
411
412 /* Bitmap of all the types of string that will be canonicalized. */
413
414 #define ASN1_MASK_CANON \
415 (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \
416 | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \
417 | B_ASN1_VISIBLESTRING)
418
419 static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
420 {
421 unsigned char *to, *from;
422 int len, i;
423
424 /* If type not in bitmask just copy string across */
425 if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
426 if (!ASN1_STRING_copy(out, in))
427 return 0;
428 return 1;
429 }
430
431 out->type = V_ASN1_UTF8STRING;
432 out->length = ASN1_STRING_to_UTF8(&out->data, in);
433 if (out->length == -1)
434 return 0;
435
436 to = out->data;
437 from = to;
438
439 len = out->length;
440
441 /*
442 * Convert string in place to canonical form. Ultimately we may need to
443 * handle a wider range of characters but for now ignore anything with
444 * MSB set and rely on the isspace() and tolower() functions.
445 */
446
447 /* Ignore leading spaces */
448 while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
449 from++;
450 len--;
451 }
452
453 to = from + len;
454
455 /* Ignore trailing spaces */
456 while ((len > 0) && !(to[-1] & 0x80) && isspace(to[-1])) {
457 to--;
458 len--;
459 }
460
461 to = out->data;
462
463 i = 0;
464 while (i < len) {
465 /* If MSB set just copy across */
466 if (*from & 0x80) {
467 *to++ = *from++;
468 i++;
469 }
470 /* Collapse multiple spaces */
471 else if (isspace(*from)) {
472 /* Copy one space across */
473 *to++ = ' ';
474 /*
475 * Ignore subsequent spaces. Note: don't need to check len here
476 * because we know the last character is a non-space so we can't
477 * overflow.
478 */
479 do {
480 from++;
481 i++;
482 }
483 while (!(*from & 0x80) && isspace(*from));
484 } else {
485 *to++ = tolower(*from);
486 from++;
487 i++;
488 }
489 }
490
491 out->length = to - out->data;
492
493 return 1;
494
495 }
496
497 static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname,
498 unsigned char **in)
499 {
500 int len, ltmp;
501 size_t i;
502 ASN1_VALUE *v;
503 STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
504
505 len = 0;
506 for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
507 v = sk_ASN1_VALUE_value(intname, i);
508 ltmp = ASN1_item_ex_i2d(&v, in,
509 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1);
510 if (ltmp < 0)
511 return ltmp;
512 len += ltmp;
513 }
514 return len;
515 }
516
517 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
518 {
519 X509_NAME *in;
520
521 if (!xn || !name)
522 return (0);
523
524 if (*xn != name) {
525 in = X509_NAME_dup(name);
526 if (in != NULL) {
527 X509_NAME_free(*xn);
528 *xn = in;
529 }
530 }
531 return (*xn != NULL);
532 }
533
534 IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)