GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/asn1/x_name.c Lines: 173 214 80.8 %
Date: 2017-11-07 Branches: 69 126 54.8 %

Line Branch Exec Source
1
/* $OpenBSD: x_name.c,v 1.33 2017/01/29 17:49:22 beck 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
static 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
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",
110
};
111
112
113
X509_NAME_ENTRY *
114
d2i_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
120
int
121
i2d_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
126
X509_NAME_ENTRY *
127
X509_NAME_ENTRY_new(void)
128
{
129
244944
	return (X509_NAME_ENTRY *)ASN1_item_new(&X509_NAME_ENTRY_it);
130
}
131
132
void
133
X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
134
{
135
488856
	ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_ENTRY_it);
136
244428
}
137
138
X509_NAME_ENTRY *
139
X509_NAME_ENTRY_dup(X509_NAME_ENTRY *x)
140
{
141
584
	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
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,
154
};
155
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",
164
};
165
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,
172
};
173
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",
182
};
183
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.
188
 */
189
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
198
};
199
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",
208
};
209
210
X509_NAME *
211
d2i_X509_NAME(X509_NAME **a, const unsigned char **in, long len)
212
{
213
	return (X509_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
214
	    &X509_NAME_it);
215
}
216
217
int
218
i2d_X509_NAME(X509_NAME *a, unsigned char **out)
219
{
220
36
	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_NAME_it);
221
}
222
223
X509_NAME *
224
X509_NAME_new(void)
225
{
226
280
	return (X509_NAME *)ASN1_item_new(&X509_NAME_it);
227
}
228
229
void
230
X509_NAME_free(X509_NAME *a)
231
{
232
848
	ASN1_item_free((ASN1_VALUE *)a, &X509_NAME_it);
233
424
}
234
235
X509_NAME *
236
X509_NAME_dup(X509_NAME *x)
237
{
238
512
	return ASN1_item_dup(&X509_NAME_it, x);
239
}
240
241
static int
242
x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
243
{
244
	X509_NAME *ret = NULL;
245
246
125028
	ret = malloc(sizeof(X509_NAME));
247
62514
	if (!ret)
248
		goto memerr;
249
62514
	if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL)
250
		goto memerr;
251
62514
	if ((ret->bytes = BUF_MEM_new()) == NULL)
252
		goto memerr;
253
62514
	ret->canon_enc = NULL;
254
62514
	ret->canon_enclen = 0;
255
62514
	ret->modified = 1;
256
62514
	*val = (ASN1_VALUE *)ret;
257
62514
	return 1;
258
259
memerr:
260
	ASN1error(ERR_R_MALLOC_FAILURE);
261
	if (ret) {
262
		if (ret->entries)
263
			sk_X509_NAME_ENTRY_free(ret->entries);
264
		free(ret);
265
	}
266
	return 0;
267
62514
}
268
269
static void
270
x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
271
{
272
	X509_NAME *a;
273
274

187320
	if (!pval || !*pval)
275
		return;
276
62440
	a = (X509_NAME *)*pval;
277
278
62440
	BUF_MEM_free(a->bytes);
279
62440
	sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
280
62440
	free(a->canon_enc);
281
62440
	free(a);
282
62440
	*pval = NULL;
283
124880
}
284
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)
288
{
289
62276
	const unsigned char *p = *in, *q;
290
31138
	union {
291
		STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
292
		ASN1_VALUE *a;
293
31138
	} intname = {NULL};
294
31138
	union {
295
		X509_NAME *x;
296
		ASN1_VALUE *a;
297
31138
	} nm = {NULL};
298
	int i, j, ret;
299
	STACK_OF(X509_NAME_ENTRY) *entries;
300
	X509_NAME_ENTRY *entry;
301
31138
	q = p;
302
303
	/* Get internal representation of Name */
304
31138
	ret = ASN1_item_ex_d2i(&intname.a, &p, len,
305
	    &X509_NAME_INTERNAL_it, tag, aclass, opt, ctx);
306
307
31138
	if (ret <= 0)
308
		return ret;
309
310
31138
	if (*val)
311
30874
		x509_name_ex_free(val, NULL);
312
31138
	if (!x509_name_ex_new(&nm.a, NULL))
313
		goto err;
314
	/* We've decoded it: now cache encoding */
315
31138
	if (!BUF_MEM_grow(nm.x->bytes, p - q))
316
		goto err;
317
31138
	memcpy(nm.x->bytes->data, q, p - q);
318
319
	/* Convert internal representation to X509_NAME structure */
320
306148
	for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) {
321
121936
		entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i);
322
487744
		for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) {
323
121936
			entry = sk_X509_NAME_ENTRY_value(entries, j);
324
121936
			entry->set = i;
325
121936
			if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry))
326
				goto err;
327
		}
328
121936
		sk_X509_NAME_ENTRY_free(entries);
329
	}
330
31138
	sk_STACK_OF_X509_NAME_ENTRY_free(intname.s);
331
31138
	ret = x509_name_canon(nm.x);
332
31138
	if (!ret)
333
		goto err;
334
31138
	nm.x->modified = 0;
335
31138
	*val = nm.a;
336
31138
	*in = p;
337
31138
	return ret;
338
339
err:
340
	if (nm.x != NULL)
341
		X509_NAME_free(nm.x);
342
	ASN1error(ERR_R_NESTED_ASN1_ERROR);
343
	return 0;
344
31138
}
345
346
static int
347
x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it,
348
    int tag, int aclass)
349
{
350
	int ret;
351
3160
	X509_NAME *a = (X509_NAME *)*val;
352
353
1580
	if (a->modified) {
354
146
		ret = x509_name_encode(a);
355
146
		if (ret < 0)
356
			return ret;
357
146
		ret = x509_name_canon(a);
358
146
		if (ret < 0)
359
			return ret;
360
	}
361
1580
	ret = a->bytes->length;
362
1580
	if (out != NULL) {
363
534
		memcpy(*out, a->bytes->data, ret);
364
534
		*out += ret;
365
534
	}
366
1580
	return ret;
367
1580
}
368
369
static void
370
local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne)
371
{
372
568
	sk_X509_NAME_ENTRY_free(ne);
373
284
}
374
375
static void
376
local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne)
377
{
378
244440
	sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free);
379
122220
}
380
381
static int
382
x509_name_encode(X509_NAME *a)
383
{
384
292
	union {
385
		STACK_OF(STACK_OF_X509_NAME_ENTRY) *s;
386
		ASN1_VALUE *a;
387
146
	} intname = {NULL};
388
	int len;
389
146
	unsigned char *p;
390
	STACK_OF(X509_NAME_ENTRY) *entries = NULL;
391
	X509_NAME_ENTRY *entry;
392
	int i, set = -1;
393
394
146
	intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null();
395
146
	if (!intname.s)
396
		goto memerr;
397
860
	for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
398
284
		entry = sk_X509_NAME_ENTRY_value(a->entries, i);
399
284
		if (entry->set != set) {
400
284
			entries = sk_X509_NAME_ENTRY_new_null();
401
284
			if (!entries)
402
				goto memerr;
403
284
			if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s,
404
			    entries))
405
				goto memerr;
406
284
			set = entry->set;
407
284
		}
408

568
		if (entries == NULL /* if entry->set is bogusly -1 */ ||
409
284
		    !sk_X509_NAME_ENTRY_push(entries, entry))
410
			goto memerr;
411
	}
412
146
	len = ASN1_item_ex_i2d(&intname.a, NULL,
413
	    &X509_NAME_INTERNAL_it, -1, -1);
414
146
	if (!BUF_MEM_grow(a->bytes, len))
415
		goto memerr;
416
146
	p = (unsigned char *)a->bytes->data;
417
146
	ASN1_item_ex_i2d(&intname.a, &p, &X509_NAME_INTERNAL_it,
418
	    -1, -1);
419
146
	sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
420
	    local_sk_X509_NAME_ENTRY_free);
421
146
	a->modified = 0;
422
146
	return len;
423
424
memerr:
425
	sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s,
426
	    local_sk_X509_NAME_ENTRY_free);
427
	ASN1error(ERR_R_MALLOC_FAILURE);
428
	return -1;
429
146
}
430
431
static int
432
x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, const char *fname,
433
    const ASN1_PCTX *pctx)
434
{
435
	if (X509_NAME_print_ex(out, (X509_NAME *)*pval, indent,
436
	    pctx->nm_flags) <= 0)
437
		return 0;
438
	return 2;
439
}
440
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.
445
 *
446
 * In future we could also normalize the UTF8 too.
447
 *
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().
452
 */
453
454
static int
455
x509_name_canon(X509_NAME *a)
456
{
457
62568
	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;
462
463
31284
	if (a->canon_enc) {
464
		free(a->canon_enc);
465
		a->canon_enc = NULL;
466
	}
467
	/* Special case: empty X509_NAME => null encoding */
468
31284
	if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
469
		a->canon_enclen = 0;
470
		return 1;
471
	}
472
31284
	intname = sk_STACK_OF_X509_NAME_ENTRY_new_null();
473
31284
	if (!intname)
474
		goto err;
475
307008
	for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
476
122220
		entry = sk_X509_NAME_ENTRY_value(a->entries, i);
477
122220
		if (entry->set != set) {
478
122220
			entries = sk_X509_NAME_ENTRY_new_null();
479
122220
			if (!entries)
480
				goto err;
481
244440
			if (sk_STACK_OF_X509_NAME_ENTRY_push(intname,
482
122220
			    entries) == 0) {
483
				sk_X509_NAME_ENTRY_free(entries);
484
				goto err;
485
			}
486
122220
			set = entry->set;
487
122220
		}
488
122220
		tmpentry = X509_NAME_ENTRY_new();
489
122220
		if (tmpentry == NULL)
490
			goto err;
491
122220
		tmpentry->object = OBJ_dup(entry->object);
492
122220
		if (tmpentry->object == NULL)
493
			goto err;
494
244440
		if (!asn1_string_canon(tmpentry->value, entry->value))
495
			goto err;
496
244440
		if (entries == NULL /* if entry->set is bogusly -1 */ ||
497
122220
		    !sk_X509_NAME_ENTRY_push(entries, tmpentry))
498
			goto err;
499
		tmpentry = NULL;
500
	}
501
502
	/* Finally generate encoding */
503
31284
	len = i2d_name_canon(intname, NULL);
504
31284
	if (len < 0)
505
		goto err;
506
31284
	p = malloc(len);
507
31284
	if (p == NULL)
508
		goto err;
509
31284
	a->canon_enc = p;
510
31284
	a->canon_enclen = len;
511
31284
	i2d_name_canon(intname, &p);
512
31284
	ret = 1;
513
514
err:
515
31284
	if (tmpentry)
516
		X509_NAME_ENTRY_free(tmpentry);
517
31284
	if (intname)
518
31284
		sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname,
519
		    local_sk_X509_NAME_ENTRY_pop_free);
520
31284
	return ret;
521
31284
}
522
523
/* Bitmap of all the types of string that will be canonicalized. */
524
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)
529
530
531
static int
532
asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in)
533
{
534
	unsigned char *to, *from;
535
	int len, i;
536
537
	/* If type not in bitmask just copy string across */
538
244440
	if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) {
539
		if (!ASN1_STRING_copy(out, in))
540
			return 0;
541
		return 1;
542
	}
543
544
122220
	out->type = V_ASN1_UTF8STRING;
545
122220
	out->length = ASN1_STRING_to_UTF8(&out->data, in);
546
122220
	if (out->length == -1)
547
		return 0;
548
549
122220
	to = out->data;
550
	from = to;
551
552
	len = out->length;
553
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.
558
	 */
559
560
	/* Ignore leading spaces */
561

611100
	while ((len > 0) && !(*from & 0x80) && isspace(*from)) {
562
		from++;
563
		len--;
564
	}
565
566
122220
	to = from + len - 1;
567
568
	/* Ignore trailing spaces */
569

488880
	while ((len > 0) && !(*to & 0x80) && isspace(*to)) {
570
		to--;
571
		len--;
572
	}
573
574
122220
	to = out->data;
575
576
	i = 0;
577
2290901
	while (i < len) {
578
		/* If MSB set just copy across */
579
2046461
		if (*from & 0x80) {
580
5184
			*to++ = *from++;
581
5184
			i++;
582
5184
		}
583
		/* Collapse multiple spaces */
584
2041277
		else if (isspace(*from)) {
585
			/* Copy one space across */
586
184704
			*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.
590
			 */
591
184704
			do {
592
184704
				from++;
593
184704
				i++;
594

369408
			} while (!(*from & 0x80) && isspace(*from));
595
		} else {
596
1856573
			*to++ = tolower(*from);
597
1856573
			from++;
598
1856573
			i++;
599
		}
600
	}
601
602
122220
	out->length = to - out->data;
603
604
122220
	return 1;
605
122220
}
606
607
static int
608
i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, unsigned char **in)
609
{
610
	int i, len, ltmp;
611
125136
	ASN1_VALUE *v;
612
62568
	STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname;
613
614
	len = 0;
615
614016
	for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) {
616
244440
		v = sk_ASN1_VALUE_value(intname, i);
617
244440
		ltmp = ASN1_item_ex_i2d(&v, in,
618
		    &X509_NAME_ENTRIES_it, -1, -1);
619
244440
		if (ltmp < 0)
620
			return ltmp;
621
244440
		len += ltmp;
622
	}
623
62568
	return len;
624
62568
}
625
626
int
627
X509_NAME_set(X509_NAME **xn, X509_NAME *name)
628
{
629
	X509_NAME *in;
630
631
492
	if (!xn || !name)
632
		return (0);
633
634
246
	if (*xn != name) {
635
246
		in = X509_NAME_dup(name);
636
246
		if (in != NULL) {
637
246
			X509_NAME_free(*xn);
638
246
			*xn = in;
639
246
		}
640
	}
641
246
	return (*xn != NULL);
642
246
}