xref: /openbsd-src/lib/libcrypto/x509/x509_alt.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /* $OpenBSD: x509_alt.c,v 1.14 2022/11/14 17:48:50 beck Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2003 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/conf.h>
63 #include <openssl/err.h>
64 #include <openssl/x509v3.h>
65 
66 #include "x509_internal.h"
67 
68 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
69     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
70 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
71     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
72 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
73 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
74 static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
75 static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
76 
77 const X509V3_EXT_METHOD v3_alt[] = {
78 	{
79 		.ext_nid = NID_subject_alt_name,
80 		.ext_flags = 0,
81 		.it = &GENERAL_NAMES_it,
82 		.ext_new = NULL,
83 		.ext_free = NULL,
84 		.d2i = NULL,
85 		.i2d = NULL,
86 		.i2s = NULL,
87 		.s2i = NULL,
88 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
89 		.v2i = (X509V3_EXT_V2I)v2i_subject_alt,
90 		.i2r = NULL,
91 		.r2i = NULL,
92 		.usr_data = NULL,
93 	},
94 	{
95 		.ext_nid = NID_issuer_alt_name,
96 		.ext_flags = 0,
97 		.it = &GENERAL_NAMES_it,
98 		.ext_new = NULL,
99 		.ext_free = NULL,
100 		.d2i = NULL,
101 		.i2d = NULL,
102 		.i2s = NULL,
103 		.s2i = NULL,
104 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
105 		.v2i = (X509V3_EXT_V2I)v2i_issuer_alt,
106 		.i2r = NULL,
107 		.r2i = NULL,
108 		.usr_data = NULL,
109 	},
110 	{
111 		.ext_nid = NID_certificate_issuer,
112 		.ext_flags = 0,
113 		.it = &GENERAL_NAMES_it,
114 		.ext_new = NULL,
115 		.ext_free = NULL,
116 		.d2i = NULL,
117 		.i2d = NULL,
118 		.i2s = NULL,
119 		.s2i = NULL,
120 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
121 		.v2i = NULL,
122 		.i2r = NULL,
123 		.r2i = NULL,
124 		.usr_data = NULL,
125 	},
126 };
127 
128 STACK_OF(CONF_VALUE) *
129 i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, GENERAL_NAMES *gens,
130     STACK_OF(CONF_VALUE) *ret)
131 {
132 	STACK_OF(CONF_VALUE) *free_ret = NULL;
133 	GENERAL_NAME *gen;
134 	int i;
135 
136 	if (ret == NULL) {
137 		if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL)
138 			return NULL;
139 	}
140 
141 	for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
142 		if ((gen = sk_GENERAL_NAME_value(gens, i)) == NULL)
143 			goto err;
144 		if ((ret = i2v_GENERAL_NAME(method, gen, ret)) == NULL)
145 			goto err;
146 	}
147 
148 	return ret;
149 
150  err:
151 	sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free);
152 
153 	return NULL;
154 }
155 LCRYPTO_ALIAS(i2v_GENERAL_NAMES)
156 
157 STACK_OF(CONF_VALUE) *
158 i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen,
159     STACK_OF(CONF_VALUE) *ret)
160 {
161 	STACK_OF(CONF_VALUE) *free_ret = NULL;
162 	unsigned char *p;
163 	char oline[256], htmp[5];
164 	int i;
165 
166 	if (ret == NULL) {
167 		if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL)
168 			return NULL;
169 	}
170 
171 	switch (gen->type) {
172 	case GEN_OTHERNAME:
173 		if (!X509V3_add_value("othername", "<unsupported>", &ret))
174 			goto err;
175 		break;
176 
177 	case GEN_X400:
178 		if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
179 			goto err;
180 		break;
181 
182 	case GEN_EDIPARTY:
183 		if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
184 			goto err;
185 		break;
186 
187 	case GEN_EMAIL:
188 		if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))
189 			goto err;
190 		break;
191 
192 	case GEN_DNS:
193 		if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))
194 			goto err;
195 		break;
196 
197 	case GEN_URI:
198 		if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))
199 			goto err;
200 		break;
201 
202 	case GEN_DIRNAME:
203 		if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL)
204 			goto err;
205 		if (!X509V3_add_value("DirName", oline, &ret))
206 			goto err;
207 		break;
208 
209 	case GEN_IPADD: /* XXX */
210 		p = gen->d.ip->data;
211 		if (gen->d.ip->length == 4)
212 			(void) snprintf(oline, sizeof oline,
213 			    "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
214 		else if (gen->d.ip->length == 16) {
215 			oline[0] = 0;
216 			for (i = 0; i < 8; i++) {
217 				(void) snprintf(htmp, sizeof htmp,
218 				    "%X", p[0] << 8 | p[1]);
219 				p += 2;
220 				strlcat(oline, htmp, sizeof(oline));
221 				if (i != 7)
222 					strlcat(oline, ":", sizeof(oline));
223 			}
224 		} else {
225 			if (!X509V3_add_value("IP Address", "<invalid>", &ret))
226 				goto err;
227 			break;
228 		}
229 		if (!X509V3_add_value("IP Address", oline, &ret))
230 			goto err;
231 		break;
232 
233 	case GEN_RID:
234 		if (!i2t_ASN1_OBJECT(oline, 256, gen->d.rid))
235 			goto err;
236 		if (!X509V3_add_value("Registered ID", oline, &ret))
237 			goto err;
238 		break;
239 	}
240 
241 	return ret;
242 
243  err:
244 	sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free);
245 
246 	return NULL;
247 }
248 LCRYPTO_ALIAS(i2v_GENERAL_NAME)
249 
250 int
251 GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
252 {
253 	unsigned char *p;
254 	int i;
255 
256 	switch (gen->type) {
257 	case GEN_OTHERNAME:
258 		BIO_printf(out, "othername:<unsupported>");
259 		break;
260 
261 	case GEN_X400:
262 		BIO_printf(out, "X400Name:<unsupported>");
263 		break;
264 
265 	case GEN_EDIPARTY:
266 		/* Maybe fix this: it is supported now */
267 		BIO_printf(out, "EdiPartyName:<unsupported>");
268 		break;
269 
270 	case GEN_EMAIL:
271 		BIO_printf(out, "email:%.*s", gen->d.ia5->length,
272 		    gen->d.ia5->data);
273 		break;
274 
275 	case GEN_DNS:
276 		BIO_printf(out, "DNS:%.*s", gen->d.ia5->length,
277 		    gen->d.ia5->data);
278 		break;
279 
280 	case GEN_URI:
281 		BIO_printf(out, "URI:%.*s", gen->d.ia5->length,
282 		    gen->d.ia5->data);
283 		break;
284 
285 	case GEN_DIRNAME:
286 		BIO_printf(out, "DirName: ");
287 		X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
288 		break;
289 
290 	case GEN_IPADD:
291 		p = gen->d.ip->data;
292 		if (gen->d.ip->length == 4)
293 			BIO_printf(out, "IP Address:%d.%d.%d.%d",
294 			    p[0], p[1], p[2], p[3]);
295 		else if (gen->d.ip->length == 16) {
296 			BIO_printf(out, "IP Address");
297 			for (i = 0; i < 8; i++) {
298 				BIO_printf(out, ":%X", p[0] << 8 | p[1]);
299 				p += 2;
300 			}
301 			BIO_puts(out, "\n");
302 		} else {
303 			BIO_printf(out, "IP Address:<invalid>");
304 			break;
305 		}
306 		break;
307 
308 	case GEN_RID:
309 		BIO_printf(out, "Registered ID");
310 		i2a_ASN1_OBJECT(out, gen->d.rid);
311 		break;
312 	}
313 	return 1;
314 }
315 LCRYPTO_ALIAS(GENERAL_NAME_print)
316 
317 static GENERAL_NAMES *
318 v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
319     STACK_OF(CONF_VALUE) *nval)
320 {
321 	GENERAL_NAMES *gens = NULL;
322 	CONF_VALUE *cnf;
323 	int i;
324 
325 	if ((gens = sk_GENERAL_NAME_new_null()) == NULL) {
326 		X509V3error(ERR_R_MALLOC_FAILURE);
327 		return NULL;
328 	}
329 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
330 		cnf = sk_CONF_VALUE_value(nval, i);
331 		if (name_cmp(cnf->name, "issuer") == 0 && cnf->value != NULL &&
332 		    strcmp(cnf->value, "copy") == 0) {
333 			if (!copy_issuer(ctx, gens))
334 				goto err;
335 		} else {
336 			GENERAL_NAME *gen;
337 			if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
338 				goto err;
339 			if (sk_GENERAL_NAME_push(gens, gen) == 0) {
340 				GENERAL_NAME_free(gen);
341 				goto err;
342 			}
343 		}
344 	}
345 	return gens;
346 
347 err:
348 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
349 	return NULL;
350 }
351 
352 /* Append subject altname of issuer to issuer alt name of subject */
353 
354 static int
355 copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
356 {
357 	GENERAL_NAMES *ialt;
358 	GENERAL_NAME *gen;
359 	X509_EXTENSION *ext;
360 	int i;
361 
362 	if (ctx && (ctx->flags == CTX_TEST))
363 		return 1;
364 	if (!ctx || !ctx->issuer_cert) {
365 		X509V3error(X509V3_R_NO_ISSUER_DETAILS);
366 		goto err;
367 	}
368 	i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
369 	if (i < 0)
370 		return 1;
371 	if (!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
372 	    !(ialt = X509V3_EXT_d2i(ext))) {
373 		X509V3error(X509V3_R_ISSUER_DECODE_ERROR);
374 		goto err;
375 	}
376 
377 	for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
378 		gen = sk_GENERAL_NAME_value(ialt, i);
379 		if (!sk_GENERAL_NAME_push(gens, gen)) {
380 			X509V3error(ERR_R_MALLOC_FAILURE);
381 			goto err;
382 		}
383 	}
384 	sk_GENERAL_NAME_free(ialt);
385 
386 	return 1;
387 
388 err:
389 	return 0;
390 
391 }
392 
393 static GENERAL_NAMES *
394 v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
395     STACK_OF(CONF_VALUE) *nval)
396 {
397 	GENERAL_NAMES *gens = NULL;
398 	CONF_VALUE *cnf;
399 	int i;
400 
401 	if (!(gens = sk_GENERAL_NAME_new_null())) {
402 		X509V3error(ERR_R_MALLOC_FAILURE);
403 		return NULL;
404 	}
405 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
406 		cnf = sk_CONF_VALUE_value(nval, i);
407 		if (!name_cmp(cnf->name, "email") && cnf->value &&
408 		    !strcmp(cnf->value, "copy")) {
409 			if (!copy_email(ctx, gens, 0))
410 				goto err;
411 		} else if (!name_cmp(cnf->name, "email") && cnf->value &&
412 		    !strcmp(cnf->value, "move")) {
413 			if (!copy_email(ctx, gens, 1))
414 				goto err;
415 		} else {
416 			GENERAL_NAME *gen;
417 			if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
418 				goto err;
419 			if (sk_GENERAL_NAME_push(gens, gen) == 0) {
420 				GENERAL_NAME_free(gen);
421 				goto err;
422 			}
423 		}
424 	}
425 	return gens;
426 
427 err:
428 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
429 	return NULL;
430 }
431 
432 /* Copy any email addresses in a certificate or request to
433  * GENERAL_NAMES
434  */
435 
436 static int
437 copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
438 {
439 	X509_NAME *nm;
440 	ASN1_IA5STRING *email = NULL;
441 	X509_NAME_ENTRY *ne;
442 	GENERAL_NAME *gen = NULL;
443 	int i;
444 
445 	if (ctx != NULL && ctx->flags == CTX_TEST)
446 		return 1;
447 	if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
448 		X509V3error(X509V3_R_NO_SUBJECT_DETAILS);
449 		goto err;
450 	}
451 	/* Find the subject name */
452 	if (ctx->subject_cert)
453 		nm = X509_get_subject_name(ctx->subject_cert);
454 	else
455 		nm = X509_REQ_get_subject_name(ctx->subject_req);
456 
457 	/* Now add any email address(es) to STACK */
458 	i = -1;
459 	while ((i = X509_NAME_get_index_by_NID(nm,
460 	    NID_pkcs9_emailAddress, i)) >= 0) {
461 		ne = X509_NAME_get_entry(nm, i);
462 		email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
463 		if (move_p) {
464 			X509_NAME_delete_entry(nm, i);
465 			X509_NAME_ENTRY_free(ne);
466 			i--;
467 		}
468 		if (!email || !(gen = GENERAL_NAME_new())) {
469 			X509V3error(ERR_R_MALLOC_FAILURE);
470 			goto err;
471 		}
472 		gen->d.ia5 = email;
473 		email = NULL;
474 		gen->type = GEN_EMAIL;
475 		if (!sk_GENERAL_NAME_push(gens, gen)) {
476 			X509V3error(ERR_R_MALLOC_FAILURE);
477 			goto err;
478 		}
479 		gen = NULL;
480 	}
481 
482 	return 1;
483 
484 err:
485 	GENERAL_NAME_free(gen);
486 	ASN1_IA5STRING_free(email);
487 	return 0;
488 }
489 
490 GENERAL_NAMES *
491 v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
492     STACK_OF(CONF_VALUE) *nval)
493 {
494 	GENERAL_NAME *gen;
495 	GENERAL_NAMES *gens = NULL;
496 	CONF_VALUE *cnf;
497 	int i;
498 
499 	if (!(gens = sk_GENERAL_NAME_new_null())) {
500 		X509V3error(ERR_R_MALLOC_FAILURE);
501 		return NULL;
502 	}
503 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
504 		cnf = sk_CONF_VALUE_value(nval, i);
505 		if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
506 			goto err;
507 		if (sk_GENERAL_NAME_push(gens, gen) == 0) {
508 			GENERAL_NAME_free(gen);
509 			goto err;
510 		}
511 	}
512 	return gens;
513 
514 err:
515 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
516 	return NULL;
517 }
518 LCRYPTO_ALIAS(v2i_GENERAL_NAMES)
519 
520 GENERAL_NAME *
521 v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
522     CONF_VALUE *cnf)
523 {
524 	return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
525 }
526 LCRYPTO_ALIAS(v2i_GENERAL_NAME)
527 
528 GENERAL_NAME *
529 a2i_GENERAL_NAME(GENERAL_NAME *out, const X509V3_EXT_METHOD *method,
530     X509V3_CTX *ctx, int gen_type, const char *value, int is_nc)
531 {
532 	char is_string = 0;
533 	GENERAL_NAME *gen = NULL;
534 
535 	if (!value) {
536 		X509V3error(X509V3_R_MISSING_VALUE);
537 		return NULL;
538 	}
539 
540 	if (out)
541 		gen = out;
542 	else {
543 		gen = GENERAL_NAME_new();
544 		if (gen == NULL) {
545 			X509V3error(ERR_R_MALLOC_FAILURE);
546 			return NULL;
547 		}
548 	}
549 
550 	switch (gen_type) {
551 	case GEN_URI:
552 	case GEN_EMAIL:
553 	case GEN_DNS:
554 		is_string = 1;
555 		break;
556 
557 	case GEN_RID:
558 		{
559 			ASN1_OBJECT *obj;
560 			if (!(obj = OBJ_txt2obj(value, 0))) {
561 				X509V3error(X509V3_R_BAD_OBJECT);
562 				ERR_asprintf_error_data("value=%s", value);
563 				goto err;
564 			}
565 			gen->d.rid = obj;
566 		}
567 		break;
568 
569 	case GEN_IPADD:
570 		if (is_nc)
571 			gen->d.ip = a2i_IPADDRESS_NC(value);
572 		else
573 			gen->d.ip = a2i_IPADDRESS(value);
574 		if (gen->d.ip == NULL) {
575 			X509V3error(X509V3_R_BAD_IP_ADDRESS);
576 			ERR_asprintf_error_data("value=%s", value);
577 			goto err;
578 		}
579 		break;
580 
581 	case GEN_DIRNAME:
582 		if (!do_dirname(gen, value, ctx)) {
583 			X509V3error(X509V3_R_DIRNAME_ERROR);
584 			goto err;
585 		}
586 		break;
587 
588 	case GEN_OTHERNAME:
589 		if (!do_othername(gen, value, ctx)) {
590 			X509V3error(X509V3_R_OTHERNAME_ERROR);
591 			goto err;
592 		}
593 		break;
594 
595 	default:
596 		X509V3error(X509V3_R_UNSUPPORTED_TYPE);
597 		goto err;
598 	}
599 
600 	if (is_string) {
601 		if (!(gen->d.ia5 = ASN1_IA5STRING_new()) ||
602 		    !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) {
603 			X509V3error(ERR_R_MALLOC_FAILURE);
604 			goto err;
605 		}
606 	}
607 
608 	gen->type = gen_type;
609 
610 	return gen;
611 
612 err:
613 	if (out == NULL)
614 		GENERAL_NAME_free(gen);
615 	return NULL;
616 }
617 LCRYPTO_ALIAS(a2i_GENERAL_NAME)
618 
619 GENERAL_NAME *
620 v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method,
621     X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
622 {
623 	uint8_t *bytes = NULL;
624 	char *name, *value;
625 	GENERAL_NAME *ret;
626 	size_t len = 0;
627 	int type;
628 	CBS cbs;
629 
630 	name = cnf->name;
631 	value = cnf->value;
632 
633 	if (!value) {
634 		X509V3error(X509V3_R_MISSING_VALUE);
635 		return NULL;
636 	}
637 
638 	if (!name_cmp(name, "email"))
639 		type = GEN_EMAIL;
640 	else if (!name_cmp(name, "URI"))
641 		type = GEN_URI;
642 	else if (!name_cmp(name, "DNS"))
643 		type = GEN_DNS;
644 	else if (!name_cmp(name, "RID"))
645 		type = GEN_RID;
646 	else if (!name_cmp(name, "IP"))
647 		type = GEN_IPADD;
648 	else if (!name_cmp(name, "dirName"))
649 		type = GEN_DIRNAME;
650 	else if (!name_cmp(name, "otherName"))
651 		type = GEN_OTHERNAME;
652 	else {
653 		X509V3error(X509V3_R_UNSUPPORTED_OPTION);
654 		ERR_asprintf_error_data("name=%s", name);
655 		return NULL;
656 	}
657 
658 	ret = a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
659 	if (ret == NULL)
660 		return NULL;
661 
662 	/*
663 	 * Validate what we have for sanity.
664 	 */
665 
666 	if (is_nc) {
667 		struct x509_constraints_name *constraints_name = NULL;
668 
669 		if (!x509_constraints_validate(ret, &constraints_name, NULL)) {
670 			X509V3error(X509V3_R_BAD_OBJECT);
671 			ERR_asprintf_error_data("name=%s", name);
672 			goto err;
673 		}
674 		x509_constraints_name_free(constraints_name);
675 		return ret;
676 	}
677 
678 	type = x509_constraints_general_to_bytes(ret, &bytes, &len);
679 	CBS_init(&cbs, bytes, len);
680 	switch (type) {
681 	case GEN_DNS:
682 		if (!x509_constraints_valid_sandns(&cbs)) {
683 			X509V3error(X509V3_R_BAD_OBJECT);
684 			ERR_asprintf_error_data("name=%s value='%.*s'", name,
685 			    (int)len, bytes);
686 			goto err;
687 		}
688 		break;
689 	case GEN_URI:
690 		if (!x509_constraints_uri_host(bytes, len, NULL)) {
691 			X509V3error(X509V3_R_BAD_OBJECT);
692 			ERR_asprintf_error_data("name=%s value='%.*s'", name,
693 			    (int)len, bytes);
694 			goto err;
695 		}
696 		break;
697 	case GEN_EMAIL:
698 		if (!x509_constraints_parse_mailbox(&cbs, NULL)) {
699 			X509V3error(X509V3_R_BAD_OBJECT);
700 			ERR_asprintf_error_data("name=%s value='%.*s'", name,
701 			    (int)len, bytes);
702 			goto err;
703 		}
704 		break;
705 	case GEN_IPADD:
706 		if (len != 4 && len != 16) {
707 			X509V3error(X509V3_R_BAD_IP_ADDRESS);
708 			ERR_asprintf_error_data("name=%s len=%zu", name, len);
709 			goto err;
710 		}
711 		break;
712 	default:
713 		break;
714 	}
715 	return ret;
716  err:
717 	if (out == NULL)
718 		GENERAL_NAME_free(ret);
719 	return NULL;
720 }
721 LCRYPTO_ALIAS(v2i_GENERAL_NAME_ex)
722 
723 static int
724 do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
725 {
726 	char *objtmp = NULL, *p;
727 	int objlen;
728 
729 	if (!(p = strchr(value, ';')))
730 		return 0;
731 	if (!(gen->d.otherName = OTHERNAME_new()))
732 		return 0;
733 	/* Free this up because we will overwrite it.
734 	 * no need to free type_id because it is static
735 	 */
736 	ASN1_TYPE_free(gen->d.otherName->value);
737 	if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
738 		return 0;
739 	objlen = p - value;
740 	objtmp = malloc(objlen + 1);
741 	if (objtmp) {
742 		strlcpy(objtmp, value, objlen + 1);
743 		gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
744 		free(objtmp);
745 	} else
746 		gen->d.otherName->type_id = NULL;
747 	if (!gen->d.otherName->type_id)
748 		return 0;
749 	return 1;
750 }
751 
752 static int
753 do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
754 {
755 	int ret;
756 	STACK_OF(CONF_VALUE) *sk;
757 	X509_NAME *nm;
758 
759 	if (!(nm = X509_NAME_new()))
760 		return 0;
761 	sk = X509V3_get_section(ctx, value);
762 	if (!sk) {
763 		X509V3error(X509V3_R_SECTION_NOT_FOUND);
764 		ERR_asprintf_error_data("section=%s", value);
765 		X509_NAME_free(nm);
766 		return 0;
767 	}
768 	/* FIXME: should allow other character types... */
769 	ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
770 	if (!ret)
771 		X509_NAME_free(nm);
772 	gen->d.dirn = nm;
773 	X509V3_section_free(ctx, sk);
774 
775 	return ret;
776 }
777