xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_alt.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* v3_alt.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*2139Sjp161948  * project.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948  * Copyright (c) 1999-2003 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/conf.h>
620Sstevel@tonic-gate #include <openssl/x509v3.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
650Sstevel@tonic-gate static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
660Sstevel@tonic-gate static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
670Sstevel@tonic-gate static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
68*2139Sjp161948 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
69*2139Sjp161948 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
70*2139Sjp161948 
710Sstevel@tonic-gate X509V3_EXT_METHOD v3_alt[] = {
720Sstevel@tonic-gate { NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
730Sstevel@tonic-gate 0,0,0,0,
740Sstevel@tonic-gate 0,0,
750Sstevel@tonic-gate (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
760Sstevel@tonic-gate (X509V3_EXT_V2I)v2i_subject_alt,
770Sstevel@tonic-gate NULL, NULL, NULL},
780Sstevel@tonic-gate 
790Sstevel@tonic-gate { NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
800Sstevel@tonic-gate 0,0,0,0,
810Sstevel@tonic-gate 0,0,
820Sstevel@tonic-gate (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
830Sstevel@tonic-gate (X509V3_EXT_V2I)v2i_issuer_alt,
840Sstevel@tonic-gate NULL, NULL, NULL},
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
STACK_OF(CONF_VALUE)870Sstevel@tonic-gate STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
880Sstevel@tonic-gate 		GENERAL_NAMES *gens, STACK_OF(CONF_VALUE) *ret)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	int i;
910Sstevel@tonic-gate 	GENERAL_NAME *gen;
920Sstevel@tonic-gate 	for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
930Sstevel@tonic-gate 		gen = sk_GENERAL_NAME_value(gens, i);
940Sstevel@tonic-gate 		ret = i2v_GENERAL_NAME(method, gen, ret);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	if(!ret) return sk_CONF_VALUE_new_null();
970Sstevel@tonic-gate 	return ret;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
STACK_OF(CONF_VALUE)1000Sstevel@tonic-gate STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
1010Sstevel@tonic-gate 				GENERAL_NAME *gen, STACK_OF(CONF_VALUE) *ret)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	unsigned char *p;
104*2139Sjp161948 	char oline[256], htmp[5];
105*2139Sjp161948 	int i;
1060Sstevel@tonic-gate 	switch (gen->type)
1070Sstevel@tonic-gate 	{
1080Sstevel@tonic-gate 		case GEN_OTHERNAME:
1090Sstevel@tonic-gate 		X509V3_add_value("othername","<unsupported>", &ret);
1100Sstevel@tonic-gate 		break;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		case GEN_X400:
1130Sstevel@tonic-gate 		X509V3_add_value("X400Name","<unsupported>", &ret);
1140Sstevel@tonic-gate 		break;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		case GEN_EDIPARTY:
1170Sstevel@tonic-gate 		X509V3_add_value("EdiPartyName","<unsupported>", &ret);
1180Sstevel@tonic-gate 		break;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		case GEN_EMAIL:
1210Sstevel@tonic-gate 		X509V3_add_value_uchar("email",gen->d.ia5->data, &ret);
1220Sstevel@tonic-gate 		break;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		case GEN_DNS:
1250Sstevel@tonic-gate 		X509V3_add_value_uchar("DNS",gen->d.ia5->data, &ret);
1260Sstevel@tonic-gate 		break;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		case GEN_URI:
1290Sstevel@tonic-gate 		X509V3_add_value_uchar("URI",gen->d.ia5->data, &ret);
1300Sstevel@tonic-gate 		break;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		case GEN_DIRNAME:
1330Sstevel@tonic-gate 		X509_NAME_oneline(gen->d.dirn, oline, 256);
1340Sstevel@tonic-gate 		X509V3_add_value("DirName",oline, &ret);
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		case GEN_IPADD:
1380Sstevel@tonic-gate 		p = gen->d.ip->data;
139*2139Sjp161948 		if(gen->d.ip->length == 4)
140*2139Sjp161948 			BIO_snprintf(oline, sizeof oline,
141*2139Sjp161948 				     "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
142*2139Sjp161948 		else if(gen->d.ip->length == 16)
143*2139Sjp161948 			{
144*2139Sjp161948 			oline[0] = 0;
145*2139Sjp161948 			for (i = 0; i < 8; i++)
146*2139Sjp161948 				{
147*2139Sjp161948 				BIO_snprintf(htmp, sizeof htmp,
148*2139Sjp161948 					     "%X", p[0] << 8 | p[1]);
149*2139Sjp161948 				p += 2;
150*2139Sjp161948 				strcat(oline, htmp);
151*2139Sjp161948 				if (i != 7)
152*2139Sjp161948 					strcat(oline, ":");
153*2139Sjp161948 				}
154*2139Sjp161948 			}
155*2139Sjp161948 		else
156*2139Sjp161948 			{
1570Sstevel@tonic-gate 			X509V3_add_value("IP Address","<invalid>", &ret);
1580Sstevel@tonic-gate 			break;
159*2139Sjp161948 			}
1600Sstevel@tonic-gate 		X509V3_add_value("IP Address",oline, &ret);
1610Sstevel@tonic-gate 		break;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		case GEN_RID:
1640Sstevel@tonic-gate 		i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
1650Sstevel@tonic-gate 		X509V3_add_value("Registered ID",oline, &ret);
1660Sstevel@tonic-gate 		break;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	return ret;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
GENERAL_NAME_print(BIO * out,GENERAL_NAME * gen)1710Sstevel@tonic-gate int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	unsigned char *p;
174*2139Sjp161948 	int i;
1750Sstevel@tonic-gate 	switch (gen->type)
1760Sstevel@tonic-gate 	{
1770Sstevel@tonic-gate 		case GEN_OTHERNAME:
1780Sstevel@tonic-gate 		BIO_printf(out, "othername:<unsupported>");
1790Sstevel@tonic-gate 		break;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		case GEN_X400:
1820Sstevel@tonic-gate 		BIO_printf(out, "X400Name:<unsupported>");
1830Sstevel@tonic-gate 		break;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		case GEN_EDIPARTY:
1860Sstevel@tonic-gate 		/* Maybe fix this: it is supported now */
1870Sstevel@tonic-gate 		BIO_printf(out, "EdiPartyName:<unsupported>");
1880Sstevel@tonic-gate 		break;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		case GEN_EMAIL:
1910Sstevel@tonic-gate 		BIO_printf(out, "email:%s",gen->d.ia5->data);
1920Sstevel@tonic-gate 		break;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		case GEN_DNS:
1950Sstevel@tonic-gate 		BIO_printf(out, "DNS:%s",gen->d.ia5->data);
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		case GEN_URI:
1990Sstevel@tonic-gate 		BIO_printf(out, "URI:%s",gen->d.ia5->data);
2000Sstevel@tonic-gate 		break;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		case GEN_DIRNAME:
2030Sstevel@tonic-gate 		BIO_printf(out, "DirName: ");
2040Sstevel@tonic-gate 		X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		case GEN_IPADD:
2080Sstevel@tonic-gate 		p = gen->d.ip->data;
209*2139Sjp161948 		if(gen->d.ip->length == 4)
210*2139Sjp161948 			BIO_printf(out, "IP Address:%d.%d.%d.%d",
211*2139Sjp161948 						p[0], p[1], p[2], p[3]);
212*2139Sjp161948 		else if(gen->d.ip->length == 16)
213*2139Sjp161948 			{
214*2139Sjp161948 			BIO_printf(out, "IP Address");
215*2139Sjp161948 			for (i = 0; i < 8; i++)
216*2139Sjp161948 				{
217*2139Sjp161948 				BIO_printf(out, ":%X", p[0] << 8 | p[1]);
218*2139Sjp161948 				p += 2;
219*2139Sjp161948 				}
220*2139Sjp161948 			BIO_puts(out, "\n");
221*2139Sjp161948 			}
222*2139Sjp161948 		else
223*2139Sjp161948 			{
2240Sstevel@tonic-gate 			BIO_printf(out,"IP Address:<invalid>");
2250Sstevel@tonic-gate 			break;
226*2139Sjp161948 			}
2270Sstevel@tonic-gate 		break;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		case GEN_RID:
2300Sstevel@tonic-gate 		BIO_printf(out, "Registered ID");
2310Sstevel@tonic-gate 		i2a_ASN1_OBJECT(out, gen->d.rid);
2320Sstevel@tonic-gate 		break;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	return 1;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
v2i_issuer_alt(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)2370Sstevel@tonic-gate static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
2380Sstevel@tonic-gate 				 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	GENERAL_NAMES *gens = NULL;
2410Sstevel@tonic-gate 	CONF_VALUE *cnf;
2420Sstevel@tonic-gate 	int i;
2430Sstevel@tonic-gate 	if(!(gens = sk_GENERAL_NAME_new_null())) {
244*2139Sjp161948 		X509V3err(X509V3_F_V2I_ISSUER_ALT,ERR_R_MALLOC_FAILURE);
2450Sstevel@tonic-gate 		return NULL;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
2480Sstevel@tonic-gate 		cnf = sk_CONF_VALUE_value(nval, i);
2490Sstevel@tonic-gate 		if(!name_cmp(cnf->name, "issuer") && cnf->value &&
2500Sstevel@tonic-gate 						!strcmp(cnf->value, "copy")) {
2510Sstevel@tonic-gate 			if(!copy_issuer(ctx, gens)) goto err;
2520Sstevel@tonic-gate 		} else {
2530Sstevel@tonic-gate 			GENERAL_NAME *gen;
2540Sstevel@tonic-gate 			if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
2550Sstevel@tonic-gate 								 goto err;
2560Sstevel@tonic-gate 			sk_GENERAL_NAME_push(gens, gen);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	return gens;
2600Sstevel@tonic-gate 	err:
2610Sstevel@tonic-gate 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
2620Sstevel@tonic-gate 	return NULL;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /* Append subject altname of issuer to issuer alt name of subject */
2660Sstevel@tonic-gate 
copy_issuer(X509V3_CTX * ctx,GENERAL_NAMES * gens)2670Sstevel@tonic-gate static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	GENERAL_NAMES *ialt;
2700Sstevel@tonic-gate 	GENERAL_NAME *gen;
2710Sstevel@tonic-gate 	X509_EXTENSION *ext;
2720Sstevel@tonic-gate 	int i;
2730Sstevel@tonic-gate 	if(ctx && (ctx->flags == CTX_TEST)) return 1;
2740Sstevel@tonic-gate 	if(!ctx || !ctx->issuer_cert) {
2750Sstevel@tonic-gate 		X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_NO_ISSUER_DETAILS);
2760Sstevel@tonic-gate 		goto err;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate         i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
2790Sstevel@tonic-gate 	if(i < 0) return 1;
2800Sstevel@tonic-gate         if(!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
2810Sstevel@tonic-gate                         !(ialt = X509V3_EXT_d2i(ext)) ) {
2820Sstevel@tonic-gate 		X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_ISSUER_DECODE_ERROR);
2830Sstevel@tonic-gate 		goto err;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	for(i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
2870Sstevel@tonic-gate 		gen = sk_GENERAL_NAME_value(ialt, i);
2880Sstevel@tonic-gate 		if(!sk_GENERAL_NAME_push(gens, gen)) {
2890Sstevel@tonic-gate 			X509V3err(X509V3_F_COPY_ISSUER,ERR_R_MALLOC_FAILURE);
2900Sstevel@tonic-gate 			goto err;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	sk_GENERAL_NAME_free(ialt);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	return 1;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	err:
2980Sstevel@tonic-gate 	return 0;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
v2i_subject_alt(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)3020Sstevel@tonic-gate static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
3030Sstevel@tonic-gate 				 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	GENERAL_NAMES *gens = NULL;
3060Sstevel@tonic-gate 	CONF_VALUE *cnf;
3070Sstevel@tonic-gate 	int i;
3080Sstevel@tonic-gate 	if(!(gens = sk_GENERAL_NAME_new_null())) {
309*2139Sjp161948 		X509V3err(X509V3_F_V2I_SUBJECT_ALT,ERR_R_MALLOC_FAILURE);
3100Sstevel@tonic-gate 		return NULL;
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
3130Sstevel@tonic-gate 		cnf = sk_CONF_VALUE_value(nval, i);
3140Sstevel@tonic-gate 		if(!name_cmp(cnf->name, "email") && cnf->value &&
3150Sstevel@tonic-gate 						!strcmp(cnf->value, "copy")) {
3160Sstevel@tonic-gate 			if(!copy_email(ctx, gens, 0)) goto err;
3170Sstevel@tonic-gate 		} else if(!name_cmp(cnf->name, "email") && cnf->value &&
3180Sstevel@tonic-gate 						!strcmp(cnf->value, "move")) {
3190Sstevel@tonic-gate 			if(!copy_email(ctx, gens, 1)) goto err;
3200Sstevel@tonic-gate 		} else {
3210Sstevel@tonic-gate 			GENERAL_NAME *gen;
3220Sstevel@tonic-gate 			if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
3230Sstevel@tonic-gate 								 goto err;
3240Sstevel@tonic-gate 			sk_GENERAL_NAME_push(gens, gen);
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	return gens;
3280Sstevel@tonic-gate 	err:
3290Sstevel@tonic-gate 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
3300Sstevel@tonic-gate 	return NULL;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /* Copy any email addresses in a certificate or request to
3340Sstevel@tonic-gate  * GENERAL_NAMES
3350Sstevel@tonic-gate  */
3360Sstevel@tonic-gate 
copy_email(X509V3_CTX * ctx,GENERAL_NAMES * gens,int move_p)3370Sstevel@tonic-gate static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	X509_NAME *nm;
3400Sstevel@tonic-gate 	ASN1_IA5STRING *email = NULL;
3410Sstevel@tonic-gate 	X509_NAME_ENTRY *ne;
3420Sstevel@tonic-gate 	GENERAL_NAME *gen = NULL;
3430Sstevel@tonic-gate 	int i;
344*2139Sjp161948 	if(ctx != NULL && ctx->flags == CTX_TEST)
345*2139Sjp161948 		return 1;
3460Sstevel@tonic-gate 	if(!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
3470Sstevel@tonic-gate 		X509V3err(X509V3_F_COPY_EMAIL,X509V3_R_NO_SUBJECT_DETAILS);
3480Sstevel@tonic-gate 		goto err;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	/* Find the subject name */
3510Sstevel@tonic-gate 	if(ctx->subject_cert) nm = X509_get_subject_name(ctx->subject_cert);
3520Sstevel@tonic-gate 	else nm = X509_REQ_get_subject_name(ctx->subject_req);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/* Now add any email address(es) to STACK */
3550Sstevel@tonic-gate 	i = -1;
3560Sstevel@tonic-gate 	while((i = X509_NAME_get_index_by_NID(nm,
3570Sstevel@tonic-gate 					 NID_pkcs9_emailAddress, i)) >= 0) {
3580Sstevel@tonic-gate 		ne = X509_NAME_get_entry(nm, i);
3590Sstevel@tonic-gate 		email = M_ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
3600Sstevel@tonic-gate                 if (move_p)
3610Sstevel@tonic-gate                         {
3620Sstevel@tonic-gate                         X509_NAME_delete_entry(nm, i);
3630Sstevel@tonic-gate                         i--;
3640Sstevel@tonic-gate                         }
3650Sstevel@tonic-gate 		if(!email || !(gen = GENERAL_NAME_new())) {
3660Sstevel@tonic-gate 			X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
3670Sstevel@tonic-gate 			goto err;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 		gen->d.ia5 = email;
3700Sstevel@tonic-gate 		email = NULL;
3710Sstevel@tonic-gate 		gen->type = GEN_EMAIL;
3720Sstevel@tonic-gate 		if(!sk_GENERAL_NAME_push(gens, gen)) {
3730Sstevel@tonic-gate 			X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
3740Sstevel@tonic-gate 			goto err;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		gen = NULL;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	return 1;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	err:
3830Sstevel@tonic-gate 	GENERAL_NAME_free(gen);
3840Sstevel@tonic-gate 	M_ASN1_IA5STRING_free(email);
3850Sstevel@tonic-gate 	return 0;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
v2i_GENERAL_NAMES(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)3890Sstevel@tonic-gate GENERAL_NAMES *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method,
3900Sstevel@tonic-gate 				X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	GENERAL_NAME *gen;
3930Sstevel@tonic-gate 	GENERAL_NAMES *gens = NULL;
3940Sstevel@tonic-gate 	CONF_VALUE *cnf;
3950Sstevel@tonic-gate 	int i;
3960Sstevel@tonic-gate 	if(!(gens = sk_GENERAL_NAME_new_null())) {
3970Sstevel@tonic-gate 		X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
3980Sstevel@tonic-gate 		return NULL;
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
4010Sstevel@tonic-gate 		cnf = sk_CONF_VALUE_value(nval, i);
4020Sstevel@tonic-gate 		if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err;
4030Sstevel@tonic-gate 		sk_GENERAL_NAME_push(gens, gen);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	return gens;
4060Sstevel@tonic-gate 	err:
4070Sstevel@tonic-gate 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
4080Sstevel@tonic-gate 	return NULL;
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
v2i_GENERAL_NAME(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,CONF_VALUE * cnf)4110Sstevel@tonic-gate GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
4120Sstevel@tonic-gate 							 CONF_VALUE *cnf)
413*2139Sjp161948 	{
414*2139Sjp161948 	return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
415*2139Sjp161948 	}
4160Sstevel@tonic-gate 
v2i_GENERAL_NAME_ex(GENERAL_NAME * out,X509V3_EXT_METHOD * method,X509V3_CTX * ctx,CONF_VALUE * cnf,int is_nc)417*2139Sjp161948 GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
418*2139Sjp161948 				X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
419*2139Sjp161948 						 CONF_VALUE *cnf, int is_nc)
420*2139Sjp161948 	{
421*2139Sjp161948 	char is_string = 0;
422*2139Sjp161948 	int type;
423*2139Sjp161948 	GENERAL_NAME *gen = NULL;
4240Sstevel@tonic-gate 
425*2139Sjp161948 	char *name, *value;
426*2139Sjp161948 
427*2139Sjp161948 	name = cnf->name;
428*2139Sjp161948 	value = cnf->value;
4290Sstevel@tonic-gate 
430*2139Sjp161948 	if(!value)
431*2139Sjp161948 		{
432*2139Sjp161948 		X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_MISSING_VALUE);
433*2139Sjp161948 		return NULL;
434*2139Sjp161948 		}
435*2139Sjp161948 
436*2139Sjp161948 	if (out)
437*2139Sjp161948 		gen = out;
438*2139Sjp161948 	else
439*2139Sjp161948 		{
440*2139Sjp161948 		gen = GENERAL_NAME_new();
441*2139Sjp161948 		if(gen == NULL)
442*2139Sjp161948 			{
443*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,ERR_R_MALLOC_FAILURE);
444*2139Sjp161948 			return NULL;
445*2139Sjp161948 			}
446*2139Sjp161948 		}
4470Sstevel@tonic-gate 
448*2139Sjp161948 	if(!name_cmp(name, "email"))
449*2139Sjp161948 		{
450*2139Sjp161948 		is_string = 1;
451*2139Sjp161948 		type = GEN_EMAIL;
452*2139Sjp161948 		}
453*2139Sjp161948 	else if(!name_cmp(name, "URI"))
454*2139Sjp161948 		{
455*2139Sjp161948 		is_string = 1;
456*2139Sjp161948 		type = GEN_URI;
457*2139Sjp161948 		}
458*2139Sjp161948 	else if(!name_cmp(name, "DNS"))
459*2139Sjp161948 		{
460*2139Sjp161948 		is_string = 1;
461*2139Sjp161948 		type = GEN_DNS;
462*2139Sjp161948 		}
463*2139Sjp161948 	else if(!name_cmp(name, "RID"))
464*2139Sjp161948 		{
465*2139Sjp161948 		ASN1_OBJECT *obj;
466*2139Sjp161948 		if(!(obj = OBJ_txt2obj(value,0)))
467*2139Sjp161948 			{
468*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_BAD_OBJECT);
469*2139Sjp161948 			ERR_add_error_data(2, "value=", value);
470*2139Sjp161948 			goto err;
471*2139Sjp161948 			}
472*2139Sjp161948 		gen->d.rid = obj;
473*2139Sjp161948 		type = GEN_RID;
474*2139Sjp161948 		}
475*2139Sjp161948 	else if(!name_cmp(name, "IP"))
476*2139Sjp161948 		{
477*2139Sjp161948 		if (is_nc)
478*2139Sjp161948 			gen->d.ip = a2i_IPADDRESS_NC(value);
479*2139Sjp161948 		else
480*2139Sjp161948 			gen->d.ip = a2i_IPADDRESS(value);
481*2139Sjp161948 		if(gen->d.ip == NULL)
482*2139Sjp161948 			{
483*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_BAD_IP_ADDRESS);
484*2139Sjp161948 			ERR_add_error_data(2, "value=", value);
485*2139Sjp161948 			goto err;
486*2139Sjp161948 			}
487*2139Sjp161948 		type = GEN_IPADD;
488*2139Sjp161948 		}
489*2139Sjp161948 	else if(!name_cmp(name, "dirName"))
490*2139Sjp161948 		{
491*2139Sjp161948 		type = GEN_DIRNAME;
492*2139Sjp161948 		if (!do_dirname(gen, value, ctx))
493*2139Sjp161948 			{
494*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_DIRNAME_ERROR);
495*2139Sjp161948 			goto err;
496*2139Sjp161948 			}
497*2139Sjp161948 		}
498*2139Sjp161948 	else if(!name_cmp(name, "otherName"))
499*2139Sjp161948 		{
500*2139Sjp161948 		if (!do_othername(gen, value, ctx))
501*2139Sjp161948 			{
502*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_OTHERNAME_ERROR);
503*2139Sjp161948 			goto err;
504*2139Sjp161948 			}
505*2139Sjp161948 		type = GEN_OTHERNAME;
506*2139Sjp161948 		}
507*2139Sjp161948 	else
508*2139Sjp161948 		{
509*2139Sjp161948 		X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,X509V3_R_UNSUPPORTED_OPTION);
510*2139Sjp161948 		ERR_add_error_data(2, "name=", name);
5110Sstevel@tonic-gate 		goto err;
512*2139Sjp161948 		}
513*2139Sjp161948 
514*2139Sjp161948 	if(is_string)
515*2139Sjp161948 		{
516*2139Sjp161948 		if(!(gen->d.ia5 = M_ASN1_IA5STRING_new()) ||
517*2139Sjp161948 			      !ASN1_STRING_set(gen->d.ia5, (unsigned char*)value,
518*2139Sjp161948 					       strlen(value)))
519*2139Sjp161948 			{
520*2139Sjp161948 			X509V3err(X509V3_F_V2I_GENERAL_NAME_EX,ERR_R_MALLOC_FAILURE);
5210Sstevel@tonic-gate 			goto err;
522*2139Sjp161948 			}
523*2139Sjp161948 		}
524*2139Sjp161948 
525*2139Sjp161948 	gen->type = type;
526*2139Sjp161948 
527*2139Sjp161948 	return gen;
528*2139Sjp161948 
529*2139Sjp161948 	err:
530*2139Sjp161948 	GENERAL_NAME_free(gen);
531*2139Sjp161948 	return NULL;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
do_othername(GENERAL_NAME * gen,char * value,X509V3_CTX * ctx)534*2139Sjp161948 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
535*2139Sjp161948 	{
536*2139Sjp161948 	char *objtmp = NULL, *p;
537*2139Sjp161948 	int objlen;
538*2139Sjp161948 	if (!(p = strchr(value, ';')))
539*2139Sjp161948 		return 0;
540*2139Sjp161948 	if (!(gen->d.otherName = OTHERNAME_new()))
541*2139Sjp161948 		return 0;
542*2139Sjp161948 	/* Free this up because we will overwrite it.
543*2139Sjp161948 	 * no need to free type_id because it is static
544*2139Sjp161948 	 */
545*2139Sjp161948 	ASN1_TYPE_free(gen->d.otherName->value);
546*2139Sjp161948 	if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
547*2139Sjp161948 		return 0;
548*2139Sjp161948 	objlen = p - value;
549*2139Sjp161948 	objtmp = OPENSSL_malloc(objlen + 1);
550*2139Sjp161948 	strncpy(objtmp, value, objlen);
551*2139Sjp161948 	objtmp[objlen] = 0;
552*2139Sjp161948 	gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
553*2139Sjp161948 	OPENSSL_free(objtmp);
554*2139Sjp161948 	if (!gen->d.otherName->type_id)
555*2139Sjp161948 		return 0;
556*2139Sjp161948 	return 1;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
do_dirname(GENERAL_NAME * gen,char * value,X509V3_CTX * ctx)559*2139Sjp161948 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
560*2139Sjp161948 	{
561*2139Sjp161948 	int ret;
562*2139Sjp161948 	STACK_OF(CONF_VALUE) *sk;
563*2139Sjp161948 	X509_NAME *nm;
564*2139Sjp161948 	if (!(nm = X509_NAME_new()))
565*2139Sjp161948 		return 0;
566*2139Sjp161948 	sk = X509V3_get_section(ctx, value);
567*2139Sjp161948 	if (!sk)
568*2139Sjp161948 		{
569*2139Sjp161948 		X509V3err(X509V3_F_DO_DIRNAME,X509V3_R_SECTION_NOT_FOUND);
570*2139Sjp161948 		ERR_add_error_data(2, "section=", value);
571*2139Sjp161948 		X509_NAME_free(nm);
572*2139Sjp161948 		return 0;
573*2139Sjp161948 		}
574*2139Sjp161948 	/* FIXME: should allow other character types... */
575*2139Sjp161948 	ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
576*2139Sjp161948 	if (!ret)
577*2139Sjp161948 		X509_NAME_free(nm);
578*2139Sjp161948 	gen->d.dirn = nm;
579*2139Sjp161948 
580*2139Sjp161948 	return ret;
581*2139Sjp161948 	}
582