| 1 |  |  | /* $OpenBSD: v3_alt.c,v 1.27 2017/01/29 17:49:23 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 |  |  | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, | 
    
    | 67 |  |  |     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); | 
    
    | 68 |  |  | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, | 
    
    | 69 |  |  |     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); | 
    
    | 70 |  |  | static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); | 
    
    | 71 |  |  | static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens); | 
    
    | 72 |  |  | static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx); | 
    
    | 73 |  |  | static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx); | 
    
    | 74 |  |  |  | 
    
    | 75 |  |  | const X509V3_EXT_METHOD v3_alt[] = { | 
    
    | 76 |  |  | 	{ | 
    
    | 77 |  |  | 		.ext_nid = NID_subject_alt_name, | 
    
    | 78 |  |  | 		.ext_flags = 0, | 
    
    | 79 |  |  | 		.it = &GENERAL_NAMES_it, | 
    
    | 80 |  |  | 		.ext_new = NULL, | 
    
    | 81 |  |  | 		.ext_free = NULL, | 
    
    | 82 |  |  | 		.d2i = NULL, | 
    
    | 83 |  |  | 		.i2d = NULL, | 
    
    | 84 |  |  | 		.i2s = NULL, | 
    
    | 85 |  |  | 		.s2i = NULL, | 
    
    | 86 |  |  | 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, | 
    
    | 87 |  |  | 		.v2i = (X509V3_EXT_V2I)v2i_subject_alt, | 
    
    | 88 |  |  | 		.i2r = NULL, | 
    
    | 89 |  |  | 		.r2i = NULL, | 
    
    | 90 |  |  | 		.usr_data = NULL, | 
    
    | 91 |  |  | 	}, | 
    
    | 92 |  |  | 	{ | 
    
    | 93 |  |  | 		.ext_nid = NID_issuer_alt_name, | 
    
    | 94 |  |  | 		.ext_flags = 0, | 
    
    | 95 |  |  | 		.it = &GENERAL_NAMES_it, | 
    
    | 96 |  |  | 		.ext_new = NULL, | 
    
    | 97 |  |  | 		.ext_free = NULL, | 
    
    | 98 |  |  | 		.d2i = NULL, | 
    
    | 99 |  |  | 		.i2d = NULL, | 
    
    | 100 |  |  | 		.i2s = NULL, | 
    
    | 101 |  |  | 		.s2i = NULL, | 
    
    | 102 |  |  | 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, | 
    
    | 103 |  |  | 		.v2i = (X509V3_EXT_V2I)v2i_issuer_alt, | 
    
    | 104 |  |  | 		.i2r = NULL, | 
    
    | 105 |  |  | 		.r2i = NULL, | 
    
    | 106 |  |  | 		.usr_data = NULL, | 
    
    | 107 |  |  | 	}, | 
    
    | 108 |  |  | 	{ | 
    
    | 109 |  |  | 		.ext_nid = NID_certificate_issuer, | 
    
    | 110 |  |  | 		.ext_flags = 0, | 
    
    | 111 |  |  | 		.it = &GENERAL_NAMES_it, | 
    
    | 112 |  |  | 		.ext_new = NULL, | 
    
    | 113 |  |  | 		.ext_free = NULL, | 
    
    | 114 |  |  | 		.d2i = NULL, | 
    
    | 115 |  |  | 		.i2d = NULL, | 
    
    | 116 |  |  | 		.i2s = NULL, | 
    
    | 117 |  |  | 		.s2i = NULL, | 
    
    | 118 |  |  | 		.i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, | 
    
    | 119 |  |  | 		.v2i = NULL, | 
    
    | 120 |  |  | 		.i2r = NULL, | 
    
    | 121 |  |  | 		.r2i = NULL, | 
    
    | 122 |  |  | 		.usr_data = NULL, | 
    
    | 123 |  |  | 	}, | 
    
    | 124 |  |  | }; | 
    
    | 125 |  |  |  | 
    
    | 126 |  |  | STACK_OF(CONF_VALUE) * | 
    
    | 127 |  |  | i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, GENERAL_NAMES *gens, | 
    
    | 128 |  |  |     STACK_OF(CONF_VALUE) *ret) | 
    
    | 129 |  |  | { | 
    
    | 130 |  |  | 	int i; | 
    
    | 131 |  |  | 	GENERAL_NAME *gen; | 
    
    | 132 |  |  |  | 
    
    | 133 |  |  | 	for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
    
    | 134 |  |  | 		gen = sk_GENERAL_NAME_value(gens, i); | 
    
    | 135 |  |  | 		ret = i2v_GENERAL_NAME(method, gen, ret); | 
    
    | 136 |  |  | 	} | 
    
    | 137 |  |  | 	if (!ret) | 
    
    | 138 |  |  | 		return sk_CONF_VALUE_new_null(); | 
    
    | 139 |  |  | 	return ret; | 
    
    | 140 |  |  | } | 
    
    | 141 |  |  |  | 
    
    | 142 |  |  | STACK_OF(CONF_VALUE) * | 
    
    | 143 |  |  | i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen, | 
    
    | 144 |  |  |     STACK_OF(CONF_VALUE) *ret) | 
    
    | 145 |  |  | { | 
    
    | 146 |  |  | 	unsigned char *p; | 
    
    | 147 |  |  | 	char oline[256], htmp[5]; | 
    
    | 148 |  |  | 	int i; | 
    
    | 149 |  |  |  | 
    
    | 150 |  |  | 	switch (gen->type) { | 
    
    | 151 |  |  | 	case GEN_OTHERNAME: | 
    
    | 152 |  |  | 		X509V3_add_value("othername", "<unsupported>", &ret); | 
    
    | 153 |  |  | 		break; | 
    
    | 154 |  |  |  | 
    
    | 155 |  |  | 	case GEN_X400: | 
    
    | 156 |  |  | 		X509V3_add_value("X400Name", "<unsupported>", &ret); | 
    
    | 157 |  |  | 		break; | 
    
    | 158 |  |  |  | 
    
    | 159 |  |  | 	case GEN_EDIPARTY: | 
    
    | 160 |  |  | 		X509V3_add_value("EdiPartyName", "<unsupported>", &ret); | 
    
    | 161 |  |  | 		break; | 
    
    | 162 |  |  |  | 
    
    | 163 |  |  | 	case GEN_EMAIL: | 
    
    | 164 |  |  | 		X509V3_add_value_uchar("email", gen->d.ia5->data, &ret); | 
    
    | 165 |  |  | 		break; | 
    
    | 166 |  |  |  | 
    
    | 167 |  |  | 	case GEN_DNS: | 
    
    | 168 |  |  | 		X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret); | 
    
    | 169 |  |  | 		break; | 
    
    | 170 |  |  |  | 
    
    | 171 |  |  | 	case GEN_URI: | 
    
    | 172 |  |  | 		X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret); | 
    
    | 173 |  |  | 		break; | 
    
    | 174 |  |  |  | 
    
    | 175 |  |  | 	case GEN_DIRNAME: | 
    
    | 176 |  |  | 		X509_NAME_oneline(gen->d.dirn, oline, 256); | 
    
    | 177 |  |  | 		X509V3_add_value("DirName", oline, &ret); | 
    
    | 178 |  |  | 		break; | 
    
    | 179 |  |  |  | 
    
    | 180 |  |  | 	case GEN_IPADD: | 
    
    | 181 |  |  | 		p = gen->d.ip->data; | 
    
    | 182 |  |  | 		if (gen->d.ip->length == 4) | 
    
    | 183 |  |  | 			(void) snprintf(oline, sizeof oline, | 
    
    | 184 |  |  | 			    "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); | 
    
    | 185 |  |  | 		else if (gen->d.ip->length == 16) { | 
    
    | 186 |  |  | 			oline[0] = 0; | 
    
    | 187 |  |  | 			for (i = 0; i < 8; i++) { | 
    
    | 188 |  |  | 				(void) snprintf(htmp, sizeof htmp, | 
    
    | 189 |  |  | 				    "%X", p[0] << 8 | p[1]); | 
    
    | 190 |  |  | 				p += 2; | 
    
    | 191 |  |  | 				strlcat(oline, htmp, sizeof(oline)); | 
    
    | 192 |  |  | 				if (i != 7) | 
    
    | 193 |  |  | 					strlcat(oline, ":", sizeof(oline)); | 
    
    | 194 |  |  | 			} | 
    
    | 195 |  |  | 		} else { | 
    
    | 196 |  |  | 			X509V3_add_value("IP Address", "<invalid>", &ret); | 
    
    | 197 |  |  | 			break; | 
    
    | 198 |  |  | 		} | 
    
    | 199 |  |  | 		X509V3_add_value("IP Address", oline, &ret); | 
    
    | 200 |  |  | 		break; | 
    
    | 201 |  |  |  | 
    
    | 202 |  |  | 	case GEN_RID: | 
    
    | 203 |  |  | 		i2t_ASN1_OBJECT(oline, 256, gen->d.rid); | 
    
    | 204 |  |  | 		X509V3_add_value("Registered ID", oline, &ret); | 
    
    | 205 |  |  | 		break; | 
    
    | 206 |  |  | 	} | 
    
    | 207 |  |  | 	return ret; | 
    
    | 208 |  |  | } | 
    
    | 209 |  |  |  | 
    
    | 210 |  |  | int | 
    
    | 211 |  |  | GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) | 
    
    | 212 |  |  | { | 
    
    | 213 |  |  | 	unsigned char *p; | 
    
    | 214 |  |  | 	int i; | 
    
    | 215 |  |  |  | 
    
    | 216 |  |  | 	switch (gen->type) { | 
    
    | 217 |  |  | 	case GEN_OTHERNAME: | 
    
    | 218 |  |  | 		BIO_printf(out, "othername:<unsupported>"); | 
    
    | 219 |  |  | 		break; | 
    
    | 220 |  |  |  | 
    
    | 221 |  |  | 	case GEN_X400: | 
    
    | 222 |  |  | 		BIO_printf(out, "X400Name:<unsupported>"); | 
    
    | 223 |  |  | 		break; | 
    
    | 224 |  |  |  | 
    
    | 225 |  |  | 	case GEN_EDIPARTY: | 
    
    | 226 |  |  | 		/* Maybe fix this: it is supported now */ | 
    
    | 227 |  |  | 		BIO_printf(out, "EdiPartyName:<unsupported>"); | 
    
    | 228 |  |  | 		break; | 
    
    | 229 |  |  |  | 
    
    | 230 |  |  | 	case GEN_EMAIL: | 
    
    | 231 |  |  | 		BIO_printf(out, "email:%s", gen->d.ia5->data); | 
    
    | 232 |  |  | 		break; | 
    
    | 233 |  |  |  | 
    
    | 234 |  |  | 	case GEN_DNS: | 
    
    | 235 |  |  | 		BIO_printf(out, "DNS:%s", gen->d.ia5->data); | 
    
    | 236 |  |  | 		break; | 
    
    | 237 |  |  |  | 
    
    | 238 |  |  | 	case GEN_URI: | 
    
    | 239 |  |  | 		BIO_printf(out, "URI:%s", gen->d.ia5->data); | 
    
    | 240 |  |  | 		break; | 
    
    | 241 |  |  |  | 
    
    | 242 |  |  | 	case GEN_DIRNAME: | 
    
    | 243 |  |  | 		BIO_printf(out, "DirName: "); | 
    
    | 244 |  |  | 		X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); | 
    
    | 245 |  |  | 		break; | 
    
    | 246 |  |  |  | 
    
    | 247 |  |  | 	case GEN_IPADD: | 
    
    | 248 |  |  | 		p = gen->d.ip->data; | 
    
    | 249 |  |  | 		if (gen->d.ip->length == 4) | 
    
    | 250 |  |  | 			BIO_printf(out, "IP Address:%d.%d.%d.%d", | 
    
    | 251 |  |  | 			    p[0], p[1], p[2], p[3]); | 
    
    | 252 |  |  | 		else if (gen->d.ip->length == 16) { | 
    
    | 253 |  |  | 			BIO_printf(out, "IP Address"); | 
    
    | 254 |  |  | 			for (i = 0; i < 8; i++) { | 
    
    | 255 |  |  | 				BIO_printf(out, ":%X", p[0] << 8 | p[1]); | 
    
    | 256 |  |  | 				p += 2; | 
    
    | 257 |  |  | 			} | 
    
    | 258 |  |  | 			BIO_puts(out, "\n"); | 
    
    | 259 |  |  | 		} else { | 
    
    | 260 |  |  | 			BIO_printf(out, "IP Address:<invalid>"); | 
    
    | 261 |  |  | 			break; | 
    
    | 262 |  |  | 		} | 
    
    | 263 |  |  | 		break; | 
    
    | 264 |  |  |  | 
    
    | 265 |  |  | 	case GEN_RID: | 
    
    | 266 |  |  | 		BIO_printf(out, "Registered ID"); | 
    
    | 267 |  |  | 		i2a_ASN1_OBJECT(out, gen->d.rid); | 
    
    | 268 |  |  | 		break; | 
    
    | 269 |  |  | 	} | 
    
    | 270 |  |  | 	return 1; | 
    
    | 271 |  |  | } | 
    
    | 272 |  |  |  | 
    
    | 273 |  |  | static GENERAL_NAMES * | 
    
    | 274 |  |  | v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, | 
    
    | 275 |  |  |     STACK_OF(CONF_VALUE) *nval) | 
    
    | 276 |  |  | { | 
    
    | 277 |  |  | 	GENERAL_NAMES *gens = NULL; | 
    
    | 278 |  |  | 	CONF_VALUE *cnf; | 
    
    | 279 |  |  | 	int i; | 
    
    | 280 |  |  |  | 
    
    | 281 |  |  | 	if ((gens = sk_GENERAL_NAME_new_null()) == NULL) { | 
    
    | 282 |  |  | 		X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 283 |  |  | 		return NULL; | 
    
    | 284 |  |  | 	} | 
    
    | 285 |  |  | 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { | 
    
    | 286 |  |  | 		cnf = sk_CONF_VALUE_value(nval, i); | 
    
    | 287 |  |  | 		if (name_cmp(cnf->name, "issuer") == 0 && cnf->value != NULL && | 
    
    | 288 |  |  | 		    strcmp(cnf->value, "copy") == 0) { | 
    
    | 289 |  |  | 			if (!copy_issuer(ctx, gens)) | 
    
    | 290 |  |  | 				goto err; | 
    
    | 291 |  |  | 		} else { | 
    
    | 292 |  |  | 			GENERAL_NAME *gen; | 
    
    | 293 |  |  | 			if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) | 
    
    | 294 |  |  | 				goto err; | 
    
    | 295 |  |  | 			if (sk_GENERAL_NAME_push(gens, gen) == 0) { | 
    
    | 296 |  |  | 				GENERAL_NAME_free(gen); | 
    
    | 297 |  |  | 				goto err; | 
    
    | 298 |  |  | 			} | 
    
    | 299 |  |  | 		} | 
    
    | 300 |  |  | 	} | 
    
    | 301 |  |  | 	return gens; | 
    
    | 302 |  |  |  | 
    
    | 303 |  |  | err: | 
    
    | 304 |  |  | 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | 
    
    | 305 |  |  | 	return NULL; | 
    
    | 306 |  |  | } | 
    
    | 307 |  |  |  | 
    
    | 308 |  |  | /* Append subject altname of issuer to issuer alt name of subject */ | 
    
    | 309 |  |  |  | 
    
    | 310 |  |  | static int | 
    
    | 311 |  |  | copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens) | 
    
    | 312 |  |  | { | 
    
    | 313 |  |  | 	GENERAL_NAMES *ialt; | 
    
    | 314 |  |  | 	GENERAL_NAME *gen; | 
    
    | 315 |  |  | 	X509_EXTENSION *ext; | 
    
    | 316 |  |  | 	int i; | 
    
    | 317 |  |  |  | 
    
    | 318 |  |  | 	if (ctx && (ctx->flags == CTX_TEST)) | 
    
    | 319 |  |  | 		return 1; | 
    
    | 320 |  |  | 	if (!ctx || !ctx->issuer_cert) { | 
    
    | 321 |  |  | 		X509V3error(X509V3_R_NO_ISSUER_DETAILS); | 
    
    | 322 |  |  | 		goto err; | 
    
    | 323 |  |  | 	} | 
    
    | 324 |  |  | 	i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); | 
    
    | 325 |  |  | 	if (i < 0) | 
    
    | 326 |  |  | 		return 1; | 
    
    | 327 |  |  | 	if (!(ext = X509_get_ext(ctx->issuer_cert, i)) || | 
    
    | 328 |  |  | 	    !(ialt = X509V3_EXT_d2i(ext))) { | 
    
    | 329 |  |  | 		X509V3error(X509V3_R_ISSUER_DECODE_ERROR); | 
    
    | 330 |  |  | 		goto err; | 
    
    | 331 |  |  | 	} | 
    
    | 332 |  |  |  | 
    
    | 333 |  |  | 	for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) { | 
    
    | 334 |  |  | 		gen = sk_GENERAL_NAME_value(ialt, i); | 
    
    | 335 |  |  | 		if (!sk_GENERAL_NAME_push(gens, gen)) { | 
    
    | 336 |  |  | 			X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 337 |  |  | 			goto err; | 
    
    | 338 |  |  | 		} | 
    
    | 339 |  |  | 	} | 
    
    | 340 |  |  | 	sk_GENERAL_NAME_free(ialt); | 
    
    | 341 |  |  |  | 
    
    | 342 |  |  | 	return 1; | 
    
    | 343 |  |  |  | 
    
    | 344 |  |  | err: | 
    
    | 345 |  |  | 	return 0; | 
    
    | 346 |  |  |  | 
    
    | 347 |  |  | } | 
    
    | 348 |  |  |  | 
    
    | 349 |  |  | static GENERAL_NAMES * | 
    
    | 350 |  |  | v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, | 
    
    | 351 |  |  |     STACK_OF(CONF_VALUE) *nval) | 
    
    | 352 |  |  | { | 
    
    | 353 |  |  | 	GENERAL_NAMES *gens = NULL; | 
    
    | 354 |  |  | 	CONF_VALUE *cnf; | 
    
    | 355 |  |  | 	int i; | 
    
    | 356 |  |  |  | 
    
    | 357 |  |  | 	if (!(gens = sk_GENERAL_NAME_new_null())) { | 
    
    | 358 |  |  | 		X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 359 |  |  | 		return NULL; | 
    
    | 360 |  |  | 	} | 
    
    | 361 |  |  | 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { | 
    
    | 362 |  |  | 		cnf = sk_CONF_VALUE_value(nval, i); | 
    
    | 363 |  |  | 		if (!name_cmp(cnf->name, "email") && cnf->value && | 
    
    | 364 |  |  | 		    !strcmp(cnf->value, "copy")) { | 
    
    | 365 |  |  | 			if (!copy_email(ctx, gens, 0)) | 
    
    | 366 |  |  | 				goto err; | 
    
    | 367 |  |  | 		} else if (!name_cmp(cnf->name, "email") && cnf->value && | 
    
    | 368 |  |  | 		    !strcmp(cnf->value, "move")) { | 
    
    | 369 |  |  | 			if (!copy_email(ctx, gens, 1)) | 
    
    | 370 |  |  | 				goto err; | 
    
    | 371 |  |  | 		} else { | 
    
    | 372 |  |  | 			GENERAL_NAME *gen; | 
    
    | 373 |  |  | 			if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) | 
    
    | 374 |  |  | 				goto err; | 
    
    | 375 |  |  | 			if (sk_GENERAL_NAME_push(gens, gen) == 0) { | 
    
    | 376 |  |  | 				GENERAL_NAME_free(gen); | 
    
    | 377 |  |  | 				goto err; | 
    
    | 378 |  |  | 			} | 
    
    | 379 |  |  | 		} | 
    
    | 380 |  |  | 	} | 
    
    | 381 |  |  | 	return gens; | 
    
    | 382 |  |  |  | 
    
    | 383 |  |  | err: | 
    
    | 384 |  |  | 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | 
    
    | 385 |  |  | 	return NULL; | 
    
    | 386 |  |  | } | 
    
    | 387 |  |  |  | 
    
    | 388 |  |  | /* Copy any email addresses in a certificate or request to | 
    
    | 389 |  |  |  * GENERAL_NAMES | 
    
    | 390 |  |  |  */ | 
    
    | 391 |  |  |  | 
    
    | 392 |  |  | static int | 
    
    | 393 |  |  | copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) | 
    
    | 394 |  |  | { | 
    
    | 395 |  |  | 	X509_NAME *nm; | 
    
    | 396 |  |  | 	ASN1_IA5STRING *email = NULL; | 
    
    | 397 |  |  | 	X509_NAME_ENTRY *ne; | 
    
    | 398 |  |  | 	GENERAL_NAME *gen = NULL; | 
    
    | 399 |  |  | 	int i; | 
    
    | 400 |  |  |  | 
    
    | 401 |  |  | 	if (ctx != NULL && ctx->flags == CTX_TEST) | 
    
    | 402 |  |  | 		return 1; | 
    
    | 403 |  |  | 	if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) { | 
    
    | 404 |  |  | 		X509V3error(X509V3_R_NO_SUBJECT_DETAILS); | 
    
    | 405 |  |  | 		goto err; | 
    
    | 406 |  |  | 	} | 
    
    | 407 |  |  | 	/* Find the subject name */ | 
    
    | 408 |  |  | 	if (ctx->subject_cert) | 
    
    | 409 |  |  | 		nm = X509_get_subject_name(ctx->subject_cert); | 
    
    | 410 |  |  | 	else | 
    
    | 411 |  |  | 		nm = X509_REQ_get_subject_name(ctx->subject_req); | 
    
    | 412 |  |  |  | 
    
    | 413 |  |  | 	/* Now add any email address(es) to STACK */ | 
    
    | 414 |  |  | 	i = -1; | 
    
    | 415 |  |  | 	while ((i = X509_NAME_get_index_by_NID(nm, | 
    
    | 416 |  |  | 	    NID_pkcs9_emailAddress, i)) >= 0) { | 
    
    | 417 |  |  | 		ne = X509_NAME_get_entry(nm, i); | 
    
    | 418 |  |  | 		email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); | 
    
    | 419 |  |  | 		if (move_p) { | 
    
    | 420 |  |  | 			X509_NAME_delete_entry(nm, i); | 
    
    | 421 |  |  | 			X509_NAME_ENTRY_free(ne); | 
    
    | 422 |  |  | 			i--; | 
    
    | 423 |  |  | 		} | 
    
    | 424 |  |  | 		if (!email || !(gen = GENERAL_NAME_new())) { | 
    
    | 425 |  |  | 			X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 426 |  |  | 			goto err; | 
    
    | 427 |  |  | 		} | 
    
    | 428 |  |  | 		gen->d.ia5 = email; | 
    
    | 429 |  |  | 		email = NULL; | 
    
    | 430 |  |  | 		gen->type = GEN_EMAIL; | 
    
    | 431 |  |  | 		if (!sk_GENERAL_NAME_push(gens, gen)) { | 
    
    | 432 |  |  | 			X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 433 |  |  | 			goto err; | 
    
    | 434 |  |  | 		} | 
    
    | 435 |  |  | 		gen = NULL; | 
    
    | 436 |  |  | 	} | 
    
    | 437 |  |  |  | 
    
    | 438 |  |  | 	return 1; | 
    
    | 439 |  |  |  | 
    
    | 440 |  |  | err: | 
    
    | 441 |  |  | 	GENERAL_NAME_free(gen); | 
    
    | 442 |  |  | 	ASN1_IA5STRING_free(email); | 
    
    | 443 |  |  | 	return 0; | 
    
    | 444 |  |  | } | 
    
    | 445 |  |  |  | 
    
    | 446 |  |  | GENERAL_NAMES * | 
    
    | 447 |  |  | v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, | 
    
    | 448 |  |  |     STACK_OF(CONF_VALUE) *nval) | 
    
    | 449 |  |  | { | 
    
    | 450 |  |  | 	GENERAL_NAME *gen; | 
    
    | 451 |  |  | 	GENERAL_NAMES *gens = NULL; | 
    
    | 452 |  |  | 	CONF_VALUE *cnf; | 
    
    | 453 |  |  | 	int i; | 
    
    | 454 |  |  |  | 
    
    | 455 |  |  | 	if (!(gens = sk_GENERAL_NAME_new_null())) { | 
    
    | 456 |  |  | 		X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 457 |  |  | 		return NULL; | 
    
    | 458 |  |  | 	} | 
    
    | 459 |  |  | 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { | 
    
    | 460 |  |  | 		cnf = sk_CONF_VALUE_value(nval, i); | 
    
    | 461 |  |  | 		if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) | 
    
    | 462 |  |  | 			goto err; | 
    
    | 463 |  |  | 		if (sk_GENERAL_NAME_push(gens, gen) == 0) { | 
    
    | 464 |  |  | 			GENERAL_NAME_free(gen); | 
    
    | 465 |  |  | 			goto err; | 
    
    | 466 |  |  | 		} | 
    
    | 467 |  |  | 	} | 
    
    | 468 |  |  | 	return gens; | 
    
    | 469 |  |  |  | 
    
    | 470 |  |  | err: | 
    
    | 471 |  |  | 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); | 
    
    | 472 |  |  | 	return NULL; | 
    
    | 473 |  |  | } | 
    
    | 474 |  |  |  | 
    
    | 475 |  |  | GENERAL_NAME * | 
    
    | 476 |  |  | v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, | 
    
    | 477 |  |  |     CONF_VALUE *cnf) | 
    
    | 478 |  |  | { | 
    
    | 479 |  |  | 	return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); | 
    
    | 480 |  |  | } | 
    
    | 481 |  |  |  | 
    
    | 482 |  |  | GENERAL_NAME * | 
    
    | 483 |  |  | a2i_GENERAL_NAME(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, | 
    
    | 484 |  |  |     X509V3_CTX *ctx, int gen_type, char *value, int is_nc) | 
    
    | 485 |  |  | { | 
    
    | 486 |  |  | 	char is_string = 0; | 
    
    | 487 |  |  | 	GENERAL_NAME *gen = NULL; | 
    
    | 488 |  |  |  | 
    
    | 489 |  |  | 	if (!value) { | 
    
    | 490 |  |  | 		X509V3error(X509V3_R_MISSING_VALUE); | 
    
    | 491 |  |  | 		return NULL; | 
    
    | 492 |  |  | 	} | 
    
    | 493 |  |  |  | 
    
    | 494 |  |  | 	if (out) | 
    
    | 495 |  |  | 		gen = out; | 
    
    | 496 |  |  | 	else { | 
    
    | 497 |  |  | 		gen = GENERAL_NAME_new(); | 
    
    | 498 |  |  | 		if (gen == NULL) { | 
    
    | 499 |  |  | 			X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 500 |  |  | 			return NULL; | 
    
    | 501 |  |  | 		} | 
    
    | 502 |  |  | 	} | 
    
    | 503 |  |  |  | 
    
    | 504 |  |  | 	switch (gen_type) { | 
    
    | 505 |  |  | 	case GEN_URI: | 
    
    | 506 |  |  | 	case GEN_EMAIL: | 
    
    | 507 |  |  | 	case GEN_DNS: | 
    
    | 508 |  |  | 		is_string = 1; | 
    
    | 509 |  |  | 		break; | 
    
    | 510 |  |  |  | 
    
    | 511 |  |  | 	case GEN_RID: | 
    
    | 512 |  |  | 		{ | 
    
    | 513 |  |  | 			ASN1_OBJECT *obj; | 
    
    | 514 |  |  | 			if (!(obj = OBJ_txt2obj(value, 0))) { | 
    
    | 515 |  |  | 				X509V3error(X509V3_R_BAD_OBJECT); | 
    
    | 516 |  |  | 				ERR_asprintf_error_data("value=%s", value); | 
    
    | 517 |  |  | 				goto err; | 
    
    | 518 |  |  | 			} | 
    
    | 519 |  |  | 			gen->d.rid = obj; | 
    
    | 520 |  |  | 		} | 
    
    | 521 |  |  | 		break; | 
    
    | 522 |  |  |  | 
    
    | 523 |  |  | 	case GEN_IPADD: | 
    
    | 524 |  |  | 		if (is_nc) | 
    
    | 525 |  |  | 			gen->d.ip = a2i_IPADDRESS_NC(value); | 
    
    | 526 |  |  | 		else | 
    
    | 527 |  |  | 			gen->d.ip = a2i_IPADDRESS(value); | 
    
    | 528 |  |  | 		if (gen->d.ip == NULL) { | 
    
    | 529 |  |  | 			X509V3error(X509V3_R_BAD_IP_ADDRESS); | 
    
    | 530 |  |  | 			ERR_asprintf_error_data("value=%s", value); | 
    
    | 531 |  |  | 			goto err; | 
    
    | 532 |  |  | 		} | 
    
    | 533 |  |  | 		break; | 
    
    | 534 |  |  |  | 
    
    | 535 |  |  | 	case GEN_DIRNAME: | 
    
    | 536 |  |  | 		if (!do_dirname(gen, value, ctx)) { | 
    
    | 537 |  |  | 			X509V3error(X509V3_R_DIRNAME_ERROR); | 
    
    | 538 |  |  | 			goto err; | 
    
    | 539 |  |  | 		} | 
    
    | 540 |  |  | 		break; | 
    
    | 541 |  |  |  | 
    
    | 542 |  |  | 	case GEN_OTHERNAME: | 
    
    | 543 |  |  | 		if (!do_othername(gen, value, ctx)) { | 
    
    | 544 |  |  | 			X509V3error(X509V3_R_OTHERNAME_ERROR); | 
    
    | 545 |  |  | 			goto err; | 
    
    | 546 |  |  | 		} | 
    
    | 547 |  |  | 		break; | 
    
    | 548 |  |  |  | 
    
    | 549 |  |  | 	default: | 
    
    | 550 |  |  | 		X509V3error(X509V3_R_UNSUPPORTED_TYPE); | 
    
    | 551 |  |  | 		goto err; | 
    
    | 552 |  |  | 	} | 
    
    | 553 |  |  |  | 
    
    | 554 |  |  | 	if (is_string) { | 
    
    | 555 |  |  | 		if (!(gen->d.ia5 = ASN1_IA5STRING_new()) || | 
    
    | 556 |  |  | 		    !ASN1_STRING_set(gen->d.ia5, (unsigned char*)value, | 
    
    | 557 |  |  | 			strlen(value))) { | 
    
    | 558 |  |  | 			X509V3error(ERR_R_MALLOC_FAILURE); | 
    
    | 559 |  |  | 			goto err; | 
    
    | 560 |  |  | 		} | 
    
    | 561 |  |  | 	} | 
    
    | 562 |  |  |  | 
    
    | 563 |  |  | 	gen->type = gen_type; | 
    
    | 564 |  |  |  | 
    
    | 565 |  |  | 	return gen; | 
    
    | 566 |  |  |  | 
    
    | 567 |  |  | err: | 
    
    | 568 |  |  | 	if (out == NULL) | 
    
    | 569 |  |  | 		GENERAL_NAME_free(gen); | 
    
    | 570 |  |  | 	return NULL; | 
    
    | 571 |  |  | } | 
    
    | 572 |  |  |  | 
    
    | 573 |  |  | GENERAL_NAME * | 
    
    | 574 |  |  | v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, | 
    
    | 575 |  |  |     X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) | 
    
    | 576 |  |  | { | 
    
    | 577 |  |  | 	int type; | 
    
    | 578 |  |  | 	char *name, *value; | 
    
    | 579 |  |  |  | 
    
    | 580 |  |  | 	name = cnf->name; | 
    
    | 581 |  |  | 	value = cnf->value; | 
    
    | 582 |  |  |  | 
    
    | 583 |  |  | 	if (!value) { | 
    
    | 584 |  |  | 		X509V3error(X509V3_R_MISSING_VALUE); | 
    
    | 585 |  |  | 		return NULL; | 
    
    | 586 |  |  | 	} | 
    
    | 587 |  |  |  | 
    
    | 588 |  |  | 	if (!name_cmp(name, "email")) | 
    
    | 589 |  |  | 		type = GEN_EMAIL; | 
    
    | 590 |  |  | 	else if (!name_cmp(name, "URI")) | 
    
    | 591 |  |  | 		type = GEN_URI; | 
    
    | 592 |  |  | 	else if (!name_cmp(name, "DNS")) | 
    
    | 593 |  |  | 		type = GEN_DNS; | 
    
    | 594 |  |  | 	else if (!name_cmp(name, "RID")) | 
    
    | 595 |  |  | 		type = GEN_RID; | 
    
    | 596 |  |  | 	else if (!name_cmp(name, "IP")) | 
    
    | 597 |  |  | 		type = GEN_IPADD; | 
    
    | 598 |  |  | 	else if (!name_cmp(name, "dirName")) | 
    
    | 599 |  |  | 		type = GEN_DIRNAME; | 
    
    | 600 |  |  | 	else if (!name_cmp(name, "otherName")) | 
    
    | 601 |  |  | 		type = GEN_OTHERNAME; | 
    
    | 602 |  |  | 	else { | 
    
    | 603 |  |  | 		X509V3error(X509V3_R_UNSUPPORTED_OPTION); | 
    
    | 604 |  |  | 		ERR_asprintf_error_data("name=%s", name); | 
    
    | 605 |  |  | 		return NULL; | 
    
    | 606 |  |  | 	} | 
    
    | 607 |  |  |  | 
    
    | 608 |  |  | 	return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); | 
    
    | 609 |  |  | } | 
    
    | 610 |  |  |  | 
    
    | 611 |  |  | static int | 
    
    | 612 |  |  | do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx) | 
    
    | 613 |  |  | { | 
    
    | 614 |  |  | 	char *objtmp = NULL, *p; | 
    
    | 615 |  |  | 	int objlen; | 
    
    | 616 |  |  |  | 
    
    | 617 |  |  | 	if (!(p = strchr(value, ';'))) | 
    
    | 618 |  |  | 		return 0; | 
    
    | 619 |  |  | 	if (!(gen->d.otherName = OTHERNAME_new())) | 
    
    | 620 |  |  | 		return 0; | 
    
    | 621 |  |  | 	/* Free this up because we will overwrite it. | 
    
    | 622 |  |  | 	 * no need to free type_id because it is static | 
    
    | 623 |  |  | 	 */ | 
    
    | 624 |  |  | 	ASN1_TYPE_free(gen->d.otherName->value); | 
    
    | 625 |  |  | 	if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx))) | 
    
    | 626 |  |  | 		return 0; | 
    
    | 627 |  |  | 	objlen = p - value; | 
    
    | 628 |  |  | 	objtmp = malloc(objlen + 1); | 
    
    | 629 |  |  | 	if (objtmp) { | 
    
    | 630 |  |  | 		strlcpy(objtmp, value, objlen + 1); | 
    
    | 631 |  |  | 		gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); | 
    
    | 632 |  |  | 		free(objtmp); | 
    
    | 633 |  |  | 	} else | 
    
    | 634 |  |  | 		gen->d.otherName->type_id = NULL; | 
    
    | 635 |  |  | 	if (!gen->d.otherName->type_id) | 
    
    | 636 |  |  | 		return 0; | 
    
    | 637 |  |  | 	return 1; | 
    
    | 638 |  |  | } | 
    
    | 639 |  |  |  | 
    
    | 640 |  |  | static int | 
    
    | 641 |  |  | do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx) | 
    
    | 642 |  |  | { | 
    
    | 643 |  |  | 	int ret; | 
    
    | 644 |  |  | 	STACK_OF(CONF_VALUE) *sk; | 
    
    | 645 |  |  | 	X509_NAME *nm; | 
    
    | 646 |  |  |  | 
    
    | 647 |  |  | 	if (!(nm = X509_NAME_new())) | 
    
    | 648 |  |  | 		return 0; | 
    
    | 649 |  |  | 	sk = X509V3_get_section(ctx, value); | 
    
    | 650 |  |  | 	if (!sk) { | 
    
    | 651 |  |  | 		X509V3error(X509V3_R_SECTION_NOT_FOUND); | 
    
    | 652 |  |  | 		ERR_asprintf_error_data("section=%s", value); | 
    
    | 653 |  |  | 		X509_NAME_free(nm); | 
    
    | 654 |  |  | 		return 0; | 
    
    | 655 |  |  | 	} | 
    
    | 656 |  |  | 	/* FIXME: should allow other character types... */ | 
    
    | 657 |  |  | 	ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC); | 
    
    | 658 |  |  | 	if (!ret) | 
    
    | 659 |  |  | 		X509_NAME_free(nm); | 
    
    | 660 |  |  | 	gen->d.dirn = nm; | 
    
    | 661 |  |  | 	X509V3_section_free(ctx, sk); | 
    
    | 662 |  |  |  | 
    
    | 663 |  |  | 	return ret; | 
    
    | 664 |  |  | } |