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