xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_utl.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* v3_utl.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  */
58*2139Sjp161948 
59*2139Sjp161948 /*
60*2139Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
61*2139Sjp161948  * Use is subject to license terms.
62*2139Sjp161948  */
63*2139Sjp161948 
64*2139Sjp161948 #pragma ident	"%Z%%M%	%I%	%E% SMI"
65*2139Sjp161948 
660Sstevel@tonic-gate /* X509 v3 extension utilities */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <stdio.h>
700Sstevel@tonic-gate #include <ctype.h>
710Sstevel@tonic-gate #include "cryptlib.h"
720Sstevel@tonic-gate #include <openssl/conf.h>
730Sstevel@tonic-gate #include <openssl/x509v3.h>
74*2139Sjp161948 #include <openssl/bn.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static char *strip_spaces(char *name);
770Sstevel@tonic-gate static int sk_strcmp(const char * const *a, const char * const *b);
780Sstevel@tonic-gate static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens);
790Sstevel@tonic-gate static void str_free(void *str);
800Sstevel@tonic-gate static int append_ia5(STACK **sk, ASN1_IA5STRING *email);
810Sstevel@tonic-gate 
82*2139Sjp161948 static int a2i_ipadd(unsigned char *ipout, const char *ipasc);
83*2139Sjp161948 static int ipv4_from_asc(unsigned char *v4, const char *in);
84*2139Sjp161948 static int ipv6_from_asc(unsigned char *v6, const char *in);
85*2139Sjp161948 static int ipv6_cb(const char *elem, int len, void *usr);
86*2139Sjp161948 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
87*2139Sjp161948 
880Sstevel@tonic-gate /* Add a CONF_VALUE name value pair to stack */
890Sstevel@tonic-gate 
X509V3_add_value(const char * name,const char * value,STACK_OF (CONF_VALUE)** extlist)900Sstevel@tonic-gate int X509V3_add_value(const char *name, const char *value,
910Sstevel@tonic-gate 						STACK_OF(CONF_VALUE) **extlist)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	CONF_VALUE *vtmp = NULL;
940Sstevel@tonic-gate 	char *tname = NULL, *tvalue = NULL;
950Sstevel@tonic-gate 	if(name && !(tname = BUF_strdup(name))) goto err;
960Sstevel@tonic-gate 	if(value && !(tvalue = BUF_strdup(value))) goto err;;
970Sstevel@tonic-gate 	if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
980Sstevel@tonic-gate 	if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
990Sstevel@tonic-gate 	vtmp->section = NULL;
1000Sstevel@tonic-gate 	vtmp->name = tname;
1010Sstevel@tonic-gate 	vtmp->value = tvalue;
1020Sstevel@tonic-gate 	if(!sk_CONF_VALUE_push(*extlist, vtmp)) goto err;
1030Sstevel@tonic-gate 	return 1;
1040Sstevel@tonic-gate 	err:
1050Sstevel@tonic-gate 	X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
1060Sstevel@tonic-gate 	if(vtmp) OPENSSL_free(vtmp);
1070Sstevel@tonic-gate 	if(tname) OPENSSL_free(tname);
1080Sstevel@tonic-gate 	if(tvalue) OPENSSL_free(tvalue);
1090Sstevel@tonic-gate 	return 0;
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
X509V3_add_value_uchar(const char * name,const unsigned char * value,STACK_OF (CONF_VALUE)** extlist)1120Sstevel@tonic-gate int X509V3_add_value_uchar(const char *name, const unsigned char *value,
1130Sstevel@tonic-gate 			   STACK_OF(CONF_VALUE) **extlist)
1140Sstevel@tonic-gate     {
1150Sstevel@tonic-gate     return X509V3_add_value(name,(const char *)value,extlist);
1160Sstevel@tonic-gate     }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /* Free function for STACK_OF(CONF_VALUE) */
1190Sstevel@tonic-gate 
X509V3_conf_free(CONF_VALUE * conf)1200Sstevel@tonic-gate void X509V3_conf_free(CONF_VALUE *conf)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	if(!conf) return;
1230Sstevel@tonic-gate 	if(conf->name) OPENSSL_free(conf->name);
1240Sstevel@tonic-gate 	if(conf->value) OPENSSL_free(conf->value);
1250Sstevel@tonic-gate 	if(conf->section) OPENSSL_free(conf->section);
1260Sstevel@tonic-gate 	OPENSSL_free(conf);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
X509V3_add_value_bool(const char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)1290Sstevel@tonic-gate int X509V3_add_value_bool(const char *name, int asn1_bool,
1300Sstevel@tonic-gate 						STACK_OF(CONF_VALUE) **extlist)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
1330Sstevel@tonic-gate 	return X509V3_add_value(name, "FALSE", extlist);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
X509V3_add_value_bool_nf(char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)1360Sstevel@tonic-gate int X509V3_add_value_bool_nf(char *name, int asn1_bool,
1370Sstevel@tonic-gate 						STACK_OF(CONF_VALUE) **extlist)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
1400Sstevel@tonic-gate 	return 1;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD * method,ASN1_ENUMERATED * a)1440Sstevel@tonic-gate char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	BIGNUM *bntmp = NULL;
1470Sstevel@tonic-gate 	char *strtmp = NULL;
1480Sstevel@tonic-gate 	if(!a) return NULL;
1490Sstevel@tonic-gate 	if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
1500Sstevel@tonic-gate 	    !(strtmp = BN_bn2dec(bntmp)) )
1510Sstevel@tonic-gate 		X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
1520Sstevel@tonic-gate 	BN_free(bntmp);
1530Sstevel@tonic-gate 	return strtmp;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
i2s_ASN1_INTEGER(X509V3_EXT_METHOD * method,ASN1_INTEGER * a)1560Sstevel@tonic-gate char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	BIGNUM *bntmp = NULL;
1590Sstevel@tonic-gate 	char *strtmp = NULL;
1600Sstevel@tonic-gate 	if(!a) return NULL;
1610Sstevel@tonic-gate 	if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
1620Sstevel@tonic-gate 	    !(strtmp = BN_bn2dec(bntmp)) )
1630Sstevel@tonic-gate 		X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
1640Sstevel@tonic-gate 	BN_free(bntmp);
1650Sstevel@tonic-gate 	return strtmp;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
s2i_ASN1_INTEGER(X509V3_EXT_METHOD * method,char * value)1680Sstevel@tonic-gate ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	BIGNUM *bn = NULL;
1710Sstevel@tonic-gate 	ASN1_INTEGER *aint;
1720Sstevel@tonic-gate 	int isneg, ishex;
1730Sstevel@tonic-gate 	int ret;
1740Sstevel@tonic-gate 	if (!value) {
1750Sstevel@tonic-gate 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE);
1760Sstevel@tonic-gate 		return 0;
1770Sstevel@tonic-gate 	}
178*2139Sjp161948 	bn = BN_new();
1790Sstevel@tonic-gate 	if (value[0] == '-') {
1800Sstevel@tonic-gate 		value++;
1810Sstevel@tonic-gate 		isneg = 1;
1820Sstevel@tonic-gate 	} else isneg = 0;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
1850Sstevel@tonic-gate 		value += 2;
1860Sstevel@tonic-gate 		ishex = 1;
1870Sstevel@tonic-gate 	} else ishex = 0;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (ishex) ret = BN_hex2bn(&bn, value);
1900Sstevel@tonic-gate 	else ret = BN_dec2bn(&bn, value);
1910Sstevel@tonic-gate 
192*2139Sjp161948 	if (!ret || value[ret]) {
193*2139Sjp161948 		BN_free(bn);
1940Sstevel@tonic-gate 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR);
1950Sstevel@tonic-gate 		return 0;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (isneg && BN_is_zero(bn)) isneg = 0;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	aint = BN_to_ASN1_INTEGER(bn, NULL);
2010Sstevel@tonic-gate 	BN_free(bn);
2020Sstevel@tonic-gate 	if (!aint) {
2030Sstevel@tonic-gate 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
2040Sstevel@tonic-gate 		return 0;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	if (isneg) aint->type |= V_ASN1_NEG;
2070Sstevel@tonic-gate 	return aint;
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
X509V3_add_value_int(const char * name,ASN1_INTEGER * aint,STACK_OF (CONF_VALUE)** extlist)2100Sstevel@tonic-gate int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
2110Sstevel@tonic-gate 	     STACK_OF(CONF_VALUE) **extlist)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	char *strtmp;
2140Sstevel@tonic-gate 	int ret;
2150Sstevel@tonic-gate 	if(!aint) return 1;
2160Sstevel@tonic-gate 	if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
2170Sstevel@tonic-gate 	ret = X509V3_add_value(name, strtmp, extlist);
2180Sstevel@tonic-gate 	OPENSSL_free(strtmp);
2190Sstevel@tonic-gate 	return ret;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
X509V3_get_value_bool(CONF_VALUE * value,int * asn1_bool)2220Sstevel@tonic-gate int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	char *btmp;
2250Sstevel@tonic-gate 	if(!(btmp = value->value)) goto err;
2260Sstevel@tonic-gate 	if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
2270Sstevel@tonic-gate 		 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
2280Sstevel@tonic-gate 		|| !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
2290Sstevel@tonic-gate 		*asn1_bool = 0xff;
2300Sstevel@tonic-gate 		return 1;
2310Sstevel@tonic-gate 	} else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
2320Sstevel@tonic-gate 		 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
2330Sstevel@tonic-gate 		|| !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
2340Sstevel@tonic-gate 		*asn1_bool = 0;
2350Sstevel@tonic-gate 		return 1;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	err:
2380Sstevel@tonic-gate 	X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
2390Sstevel@tonic-gate 	X509V3_conf_err(value);
2400Sstevel@tonic-gate 	return 0;
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
X509V3_get_value_int(CONF_VALUE * value,ASN1_INTEGER ** aint)2430Sstevel@tonic-gate int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	ASN1_INTEGER *itmp;
2460Sstevel@tonic-gate 	if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
2470Sstevel@tonic-gate 		X509V3_conf_err(value);
2480Sstevel@tonic-gate 		return 0;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	*aint = itmp;
2510Sstevel@tonic-gate 	return 1;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #define HDR_NAME	1
2550Sstevel@tonic-gate #define HDR_VALUE	2
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*#define DEBUG*/
2580Sstevel@tonic-gate 
STACK_OF(CONF_VALUE)2590Sstevel@tonic-gate STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	char *p, *q, c;
2620Sstevel@tonic-gate 	char *ntmp, *vtmp;
2630Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *values = NULL;
2640Sstevel@tonic-gate 	char *linebuf;
2650Sstevel@tonic-gate 	int state;
2660Sstevel@tonic-gate 	/* We are going to modify the line so copy it first */
2670Sstevel@tonic-gate 	linebuf = BUF_strdup(line);
2680Sstevel@tonic-gate 	state = HDR_NAME;
2690Sstevel@tonic-gate 	ntmp = NULL;
2700Sstevel@tonic-gate 	/* Go through all characters */
2710Sstevel@tonic-gate 	for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		switch(state) {
2740Sstevel@tonic-gate 			case HDR_NAME:
2750Sstevel@tonic-gate 			if(c == ':') {
2760Sstevel@tonic-gate 				state = HDR_VALUE;
2770Sstevel@tonic-gate 				*p = 0;
2780Sstevel@tonic-gate 				ntmp = strip_spaces(q);
2790Sstevel@tonic-gate 				if(!ntmp) {
2800Sstevel@tonic-gate 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
2810Sstevel@tonic-gate 					goto err;
2820Sstevel@tonic-gate 				}
2830Sstevel@tonic-gate 				q = p + 1;
2840Sstevel@tonic-gate 			} else if(c == ',') {
2850Sstevel@tonic-gate 				*p = 0;
2860Sstevel@tonic-gate 				ntmp = strip_spaces(q);
2870Sstevel@tonic-gate 				q = p + 1;
2880Sstevel@tonic-gate #if 0
2890Sstevel@tonic-gate 				printf("%s\n", ntmp);
2900Sstevel@tonic-gate #endif
2910Sstevel@tonic-gate 				if(!ntmp) {
2920Sstevel@tonic-gate 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
2930Sstevel@tonic-gate 					goto err;
2940Sstevel@tonic-gate 				}
2950Sstevel@tonic-gate 				X509V3_add_value(ntmp, NULL, &values);
2960Sstevel@tonic-gate 			}
2970Sstevel@tonic-gate 			break ;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 			case HDR_VALUE:
3000Sstevel@tonic-gate 			if(c == ',') {
3010Sstevel@tonic-gate 				state = HDR_NAME;
3020Sstevel@tonic-gate 				*p = 0;
3030Sstevel@tonic-gate 				vtmp = strip_spaces(q);
3040Sstevel@tonic-gate #if 0
3050Sstevel@tonic-gate 				printf("%s\n", ntmp);
3060Sstevel@tonic-gate #endif
3070Sstevel@tonic-gate 				if(!vtmp) {
3080Sstevel@tonic-gate 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
3090Sstevel@tonic-gate 					goto err;
3100Sstevel@tonic-gate 				}
3110Sstevel@tonic-gate 				X509V3_add_value(ntmp, vtmp, &values);
3120Sstevel@tonic-gate 				ntmp = NULL;
3130Sstevel@tonic-gate 				q = p + 1;
3140Sstevel@tonic-gate 			}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	if(state == HDR_VALUE) {
3200Sstevel@tonic-gate 		vtmp = strip_spaces(q);
3210Sstevel@tonic-gate #if 0
3220Sstevel@tonic-gate 		printf("%s=%s\n", ntmp, vtmp);
3230Sstevel@tonic-gate #endif
3240Sstevel@tonic-gate 		if(!vtmp) {
3250Sstevel@tonic-gate 			X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
3260Sstevel@tonic-gate 			goto err;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 		X509V3_add_value(ntmp, vtmp, &values);
3290Sstevel@tonic-gate 	} else {
3300Sstevel@tonic-gate 		ntmp = strip_spaces(q);
3310Sstevel@tonic-gate #if 0
3320Sstevel@tonic-gate 		printf("%s\n", ntmp);
3330Sstevel@tonic-gate #endif
3340Sstevel@tonic-gate 		if(!ntmp) {
3350Sstevel@tonic-gate 			X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
3360Sstevel@tonic-gate 			goto err;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 		X509V3_add_value(ntmp, NULL, &values);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate OPENSSL_free(linebuf);
3410Sstevel@tonic-gate return values;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate err:
3440Sstevel@tonic-gate OPENSSL_free(linebuf);
3450Sstevel@tonic-gate sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
3460Sstevel@tonic-gate return NULL;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /* Delete leading and trailing spaces from a string */
strip_spaces(char * name)3510Sstevel@tonic-gate static char *strip_spaces(char *name)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	char *p, *q;
3540Sstevel@tonic-gate 	/* Skip over leading spaces */
3550Sstevel@tonic-gate 	p = name;
3560Sstevel@tonic-gate 	while(*p && isspace((unsigned char)*p)) p++;
3570Sstevel@tonic-gate 	if(!*p) return NULL;
3580Sstevel@tonic-gate 	q = p + strlen(p) - 1;
3590Sstevel@tonic-gate 	while((q != p) && isspace((unsigned char)*q)) q--;
3600Sstevel@tonic-gate 	if(p != q) q[1] = 0;
3610Sstevel@tonic-gate 	if(!*p) return NULL;
3620Sstevel@tonic-gate 	return p;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /* hex string utilities */
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
3680Sstevel@tonic-gate  * hex representation
3690Sstevel@tonic-gate  * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate 
hex_to_string(unsigned char * buffer,long len)3720Sstevel@tonic-gate char *hex_to_string(unsigned char *buffer, long len)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	char *tmp, *q;
3750Sstevel@tonic-gate 	unsigned char *p;
3760Sstevel@tonic-gate 	int i;
3770Sstevel@tonic-gate 	static char hexdig[] = "0123456789ABCDEF";
3780Sstevel@tonic-gate 	if(!buffer || !len) return NULL;
3790Sstevel@tonic-gate 	if(!(tmp = OPENSSL_malloc(len * 3 + 1))) {
3800Sstevel@tonic-gate 		X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
3810Sstevel@tonic-gate 		return NULL;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	q = tmp;
3840Sstevel@tonic-gate 	for(i = 0, p = buffer; i < len; i++,p++) {
3850Sstevel@tonic-gate 		*q++ = hexdig[(*p >> 4) & 0xf];
3860Sstevel@tonic-gate 		*q++ = hexdig[*p & 0xf];
3870Sstevel@tonic-gate 		*q++ = ':';
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	q[-1] = 0;
3900Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
3910Sstevel@tonic-gate 	ebcdic2ascii(tmp, tmp, q - tmp - 1);
3920Sstevel@tonic-gate #endif
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return tmp;
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /* Give a string of hex digits convert to
3980Sstevel@tonic-gate  * a buffer
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate 
string_to_hex(char * str,long * len)4010Sstevel@tonic-gate unsigned char *string_to_hex(char *str, long *len)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	unsigned char *hexbuf, *q;
4040Sstevel@tonic-gate 	unsigned char ch, cl, *p;
4050Sstevel@tonic-gate 	if(!str) {
4060Sstevel@tonic-gate 		X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
4070Sstevel@tonic-gate 		return NULL;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 	if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err;
4100Sstevel@tonic-gate 	for(p = (unsigned char *)str, q = hexbuf; *p;) {
4110Sstevel@tonic-gate 		ch = *p++;
4120Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
4130Sstevel@tonic-gate 		ch = os_toebcdic[ch];
4140Sstevel@tonic-gate #endif
4150Sstevel@tonic-gate 		if(ch == ':') continue;
4160Sstevel@tonic-gate 		cl = *p++;
4170Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
4180Sstevel@tonic-gate 		cl = os_toebcdic[cl];
4190Sstevel@tonic-gate #endif
4200Sstevel@tonic-gate 		if(!cl) {
4210Sstevel@tonic-gate 			X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
4220Sstevel@tonic-gate 			OPENSSL_free(hexbuf);
4230Sstevel@tonic-gate 			return NULL;
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 		if(isupper(ch)) ch = tolower(ch);
4260Sstevel@tonic-gate 		if(isupper(cl)) cl = tolower(cl);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		if((ch >= '0') && (ch <= '9')) ch -= '0';
4290Sstevel@tonic-gate 		else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
4300Sstevel@tonic-gate 		else goto badhex;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		if((cl >= '0') && (cl <= '9')) cl -= '0';
4330Sstevel@tonic-gate 		else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
4340Sstevel@tonic-gate 		else goto badhex;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		*q++ = (ch << 4) | cl;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if(len) *len = q - hexbuf;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return hexbuf;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	err:
4440Sstevel@tonic-gate 	if(hexbuf) OPENSSL_free(hexbuf);
4450Sstevel@tonic-gate 	X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
4460Sstevel@tonic-gate 	return NULL;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	badhex:
4490Sstevel@tonic-gate 	OPENSSL_free(hexbuf);
4500Sstevel@tonic-gate 	X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
4510Sstevel@tonic-gate 	return NULL;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /* V2I name comparison function: returns zero if 'name' matches
4560Sstevel@tonic-gate  * cmp or cmp.*
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate 
name_cmp(const char * name,const char * cmp)4590Sstevel@tonic-gate int name_cmp(const char *name, const char *cmp)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	int len, ret;
4620Sstevel@tonic-gate 	char c;
4630Sstevel@tonic-gate 	len = strlen(cmp);
4640Sstevel@tonic-gate 	if((ret = strncmp(name, cmp, len))) return ret;
4650Sstevel@tonic-gate 	c = name[len];
4660Sstevel@tonic-gate 	if(!c || (c=='.')) return 0;
4670Sstevel@tonic-gate 	return 1;
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
sk_strcmp(const char * const * a,const char * const * b)4700Sstevel@tonic-gate static int sk_strcmp(const char * const *a, const char * const *b)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	return strcmp(*a, *b);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
X509_get1_email(X509 * x)4750Sstevel@tonic-gate STACK *X509_get1_email(X509 *x)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	GENERAL_NAMES *gens;
4780Sstevel@tonic-gate 	STACK *ret;
4790Sstevel@tonic-gate 	gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
4800Sstevel@tonic-gate 	ret = get_email(X509_get_subject_name(x), gens);
4810Sstevel@tonic-gate 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
4820Sstevel@tonic-gate 	return ret;
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
X509_REQ_get1_email(X509_REQ * x)4850Sstevel@tonic-gate STACK *X509_REQ_get1_email(X509_REQ *x)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	GENERAL_NAMES *gens;
4880Sstevel@tonic-gate 	STACK_OF(X509_EXTENSION) *exts;
4890Sstevel@tonic-gate 	STACK *ret;
4900Sstevel@tonic-gate 	exts = X509_REQ_get_extensions(x);
4910Sstevel@tonic-gate 	gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
4920Sstevel@tonic-gate 	ret = get_email(X509_REQ_get_subject_name(x), gens);
4930Sstevel@tonic-gate 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
4940Sstevel@tonic-gate 	sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
4950Sstevel@tonic-gate 	return ret;
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 
get_email(X509_NAME * name,GENERAL_NAMES * gens)4990Sstevel@tonic-gate static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	STACK *ret = NULL;
5020Sstevel@tonic-gate 	X509_NAME_ENTRY *ne;
5030Sstevel@tonic-gate 	ASN1_IA5STRING *email;
5040Sstevel@tonic-gate 	GENERAL_NAME *gen;
5050Sstevel@tonic-gate 	int i;
5060Sstevel@tonic-gate 	/* Now add any email address(es) to STACK */
5070Sstevel@tonic-gate 	i = -1;
5080Sstevel@tonic-gate 	/* First supplied X509_NAME */
5090Sstevel@tonic-gate 	while((i = X509_NAME_get_index_by_NID(name,
5100Sstevel@tonic-gate 					 NID_pkcs9_emailAddress, i)) >= 0) {
5110Sstevel@tonic-gate 		ne = X509_NAME_get_entry(name, i);
5120Sstevel@tonic-gate 		email = X509_NAME_ENTRY_get_data(ne);
5130Sstevel@tonic-gate 		if(!append_ia5(&ret, email)) return NULL;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
5160Sstevel@tonic-gate 	{
5170Sstevel@tonic-gate 		gen = sk_GENERAL_NAME_value(gens, i);
5180Sstevel@tonic-gate 		if(gen->type != GEN_EMAIL) continue;
5190Sstevel@tonic-gate 		if(!append_ia5(&ret, gen->d.ia5)) return NULL;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	return ret;
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
str_free(void * str)5240Sstevel@tonic-gate static void str_free(void *str)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	OPENSSL_free(str);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
append_ia5(STACK ** sk,ASN1_IA5STRING * email)5290Sstevel@tonic-gate static int append_ia5(STACK **sk, ASN1_IA5STRING *email)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	char *emtmp;
5320Sstevel@tonic-gate 	/* First some sanity checks */
5330Sstevel@tonic-gate 	if(email->type != V_ASN1_IA5STRING) return 1;
5340Sstevel@tonic-gate 	if(!email->data || !email->length) return 1;
5350Sstevel@tonic-gate 	if(!*sk) *sk = sk_new(sk_strcmp);
5360Sstevel@tonic-gate 	if(!*sk) return 0;
5370Sstevel@tonic-gate 	/* Don't add duplicates */
5380Sstevel@tonic-gate 	if(sk_find(*sk, (char *)email->data) != -1) return 1;
5390Sstevel@tonic-gate 	emtmp = BUF_strdup((char *)email->data);
5400Sstevel@tonic-gate 	if(!emtmp || !sk_push(*sk, emtmp)) {
5410Sstevel@tonic-gate 		X509_email_free(*sk);
5420Sstevel@tonic-gate 		*sk = NULL;
5430Sstevel@tonic-gate 		return 0;
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 	return 1;
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
X509_email_free(STACK * sk)5480Sstevel@tonic-gate void X509_email_free(STACK *sk)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	sk_pop_free(sk, str_free);
5510Sstevel@tonic-gate }
552*2139Sjp161948 
553*2139Sjp161948 /* Convert IP addresses both IPv4 and IPv6 into an
554*2139Sjp161948  * OCTET STRING compatible with RFC3280.
555*2139Sjp161948  */
556*2139Sjp161948 
a2i_IPADDRESS(const char * ipasc)557*2139Sjp161948 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
558*2139Sjp161948 	{
559*2139Sjp161948 	unsigned char ipout[16];
560*2139Sjp161948 	ASN1_OCTET_STRING *ret;
561*2139Sjp161948 	int iplen;
562*2139Sjp161948 
563*2139Sjp161948 	/* If string contains a ':' assume IPv6 */
564*2139Sjp161948 
565*2139Sjp161948 	iplen = a2i_ipadd(ipout, ipasc);
566*2139Sjp161948 
567*2139Sjp161948 	if (!iplen)
568*2139Sjp161948 		return NULL;
569*2139Sjp161948 
570*2139Sjp161948 	ret = ASN1_OCTET_STRING_new();
571*2139Sjp161948 	if (!ret)
572*2139Sjp161948 		return NULL;
573*2139Sjp161948 	if (!ASN1_OCTET_STRING_set(ret, ipout, iplen))
574*2139Sjp161948 		{
575*2139Sjp161948 		ASN1_OCTET_STRING_free(ret);
576*2139Sjp161948 		return NULL;
577*2139Sjp161948 		}
578*2139Sjp161948 	return ret;
579*2139Sjp161948 	}
580*2139Sjp161948 
a2i_IPADDRESS_NC(const char * ipasc)581*2139Sjp161948 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
582*2139Sjp161948 	{
583*2139Sjp161948 	ASN1_OCTET_STRING *ret = NULL;
584*2139Sjp161948 	unsigned char ipout[32];
585*2139Sjp161948 	char *iptmp = NULL, *p;
586*2139Sjp161948 	int iplen1, iplen2;
587*2139Sjp161948 	p = strchr(ipasc,'/');
588*2139Sjp161948 	if (!p)
589*2139Sjp161948 		return NULL;
590*2139Sjp161948 	iptmp = BUF_strdup(ipasc);
591*2139Sjp161948 	if (!iptmp)
592*2139Sjp161948 		return NULL;
593*2139Sjp161948 	p = iptmp + (p - ipasc);
594*2139Sjp161948 	*p++ = 0;
595*2139Sjp161948 
596*2139Sjp161948 	iplen1 = a2i_ipadd(ipout, iptmp);
597*2139Sjp161948 
598*2139Sjp161948 	if (!iplen1)
599*2139Sjp161948 		goto err;
600*2139Sjp161948 
601*2139Sjp161948 	iplen2 = a2i_ipadd(ipout + iplen1, p);
602*2139Sjp161948 
603*2139Sjp161948 	OPENSSL_free(iptmp);
604*2139Sjp161948 	iptmp = NULL;
605*2139Sjp161948 
606*2139Sjp161948 	if (!iplen2 || (iplen1 != iplen2))
607*2139Sjp161948 		goto err;
608*2139Sjp161948 
609*2139Sjp161948 	ret = ASN1_OCTET_STRING_new();
610*2139Sjp161948 	if (!ret)
611*2139Sjp161948 		goto err;
612*2139Sjp161948 	if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
613*2139Sjp161948 		goto err;
614*2139Sjp161948 
615*2139Sjp161948 	return ret;
616*2139Sjp161948 
617*2139Sjp161948 	err:
618*2139Sjp161948 	if (iptmp)
619*2139Sjp161948 		OPENSSL_free(iptmp);
620*2139Sjp161948 	if (ret)
621*2139Sjp161948 		ASN1_OCTET_STRING_free(ret);
622*2139Sjp161948 	return NULL;
623*2139Sjp161948 	}
624*2139Sjp161948 
625*2139Sjp161948 
a2i_ipadd(unsigned char * ipout,const char * ipasc)626*2139Sjp161948 static int a2i_ipadd(unsigned char *ipout, const char *ipasc)
627*2139Sjp161948 	{
628*2139Sjp161948 	/* If string contains a ':' assume IPv6 */
629*2139Sjp161948 
630*2139Sjp161948 	if (strchr(ipasc, ':'))
631*2139Sjp161948 		{
632*2139Sjp161948 		if (!ipv6_from_asc(ipout, ipasc))
633*2139Sjp161948 			return 0;
634*2139Sjp161948 		return 16;
635*2139Sjp161948 		}
636*2139Sjp161948 	else
637*2139Sjp161948 		{
638*2139Sjp161948 		if (!ipv4_from_asc(ipout, ipasc))
639*2139Sjp161948 			return 0;
640*2139Sjp161948 		return 4;
641*2139Sjp161948 		}
642*2139Sjp161948 	}
643*2139Sjp161948 
644*2139Sjp161948 #if	defined(_BOOT)
645*2139Sjp161948 /* This function was copied from bio/b_sock.c */
646*2139Sjp161948 /* The reason I have implemented this instead of using sscanf is because
647*2139Sjp161948  * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
get_ip(const char * str,unsigned char ip[4])648*2139Sjp161948 static int get_ip(const char *str, unsigned char ip[4])
649*2139Sjp161948 	{
650*2139Sjp161948 	unsigned int tmp[4];
651*2139Sjp161948 	int num=0,c,ok=0;
652*2139Sjp161948 
653*2139Sjp161948 	tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
654*2139Sjp161948 
655*2139Sjp161948 	for (;;)
656*2139Sjp161948 		{
657*2139Sjp161948 		c= *(str++);
658*2139Sjp161948 		if ((c >= '0') && (c <= '9'))
659*2139Sjp161948 			{
660*2139Sjp161948 			ok=1;
661*2139Sjp161948 			tmp[num]=tmp[num]*10+c-'0';
662*2139Sjp161948 			if (tmp[num] > 255) return(0);
663*2139Sjp161948 			}
664*2139Sjp161948 		else if (c == '.')
665*2139Sjp161948 			{
666*2139Sjp161948 			if (!ok) return(-1);
667*2139Sjp161948 			if (num == 3) return(0);
668*2139Sjp161948 			num++;
669*2139Sjp161948 			ok=0;
670*2139Sjp161948 			}
671*2139Sjp161948 		else if (c == '\0' && (num == 3) && ok)
672*2139Sjp161948 			break;
673*2139Sjp161948 		else
674*2139Sjp161948 			return(0);
675*2139Sjp161948 		}
676*2139Sjp161948 	ip[0]=tmp[0];
677*2139Sjp161948 	ip[1]=tmp[1];
678*2139Sjp161948 	ip[2]=tmp[2];
679*2139Sjp161948 	ip[3]=tmp[3];
680*2139Sjp161948 	return(1);
681*2139Sjp161948 	}
682*2139Sjp161948 #endif /* _BOOT */
683*2139Sjp161948 
ipv4_from_asc(unsigned char * v4,const char * in)684*2139Sjp161948 static int ipv4_from_asc(unsigned char *v4, const char *in)
685*2139Sjp161948 	{
686*2139Sjp161948 	int a0, a1, a2, a3;
687*2139Sjp161948 
688*2139Sjp161948 #if	defined(_BOOT)
689*2139Sjp161948 	if (get_ip(in, v4) != 1)
690*2139Sjp161948 		return 0;
691*2139Sjp161948 #else	/* _BOOT */
692*2139Sjp161948 	if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
693*2139Sjp161948 		return 0;
694*2139Sjp161948 	if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
695*2139Sjp161948 		|| (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
696*2139Sjp161948 		return 0;
697*2139Sjp161948 	v4[0] = a0;
698*2139Sjp161948 	v4[1] = a1;
699*2139Sjp161948 	v4[2] = a2;
700*2139Sjp161948 	v4[3] = a3;
701*2139Sjp161948 #endif	/* _BOOT */
702*2139Sjp161948 	return 1;
703*2139Sjp161948 	}
704*2139Sjp161948 
705*2139Sjp161948 typedef struct {
706*2139Sjp161948 		/* Temporary store for IPV6 output */
707*2139Sjp161948 		unsigned char tmp[16];
708*2139Sjp161948 		/* Total number of bytes in tmp */
709*2139Sjp161948 		int total;
710*2139Sjp161948 		/* The position of a zero (corresponding to '::') */
711*2139Sjp161948 		int zero_pos;
712*2139Sjp161948 		/* Number of zeroes */
713*2139Sjp161948 		int zero_cnt;
714*2139Sjp161948 	} IPV6_STAT;
715*2139Sjp161948 
716*2139Sjp161948 
ipv6_from_asc(unsigned char * v6,const char * in)717*2139Sjp161948 static int ipv6_from_asc(unsigned char *v6, const char *in)
718*2139Sjp161948 	{
719*2139Sjp161948 	IPV6_STAT v6stat;
720*2139Sjp161948 	v6stat.total = 0;
721*2139Sjp161948 	v6stat.zero_pos = -1;
722*2139Sjp161948 	v6stat.zero_cnt = 0;
723*2139Sjp161948 	/* Treat the IPv6 representation as a list of values
724*2139Sjp161948 	 * separated by ':'. The presence of a '::' will parse
725*2139Sjp161948  	 * as one, two or three zero length elements.
726*2139Sjp161948 	 */
727*2139Sjp161948 	if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
728*2139Sjp161948 		return 0;
729*2139Sjp161948 
730*2139Sjp161948 	/* Now for some sanity checks */
731*2139Sjp161948 
732*2139Sjp161948 	if (v6stat.zero_pos == -1)
733*2139Sjp161948 		{
734*2139Sjp161948 		/* If no '::' must have exactly 16 bytes */
735*2139Sjp161948 		if (v6stat.total != 16)
736*2139Sjp161948 			return 0;
737*2139Sjp161948 		}
738*2139Sjp161948 	else
739*2139Sjp161948 		{
740*2139Sjp161948 		/* If '::' must have less than 16 bytes */
741*2139Sjp161948 		if (v6stat.total == 16)
742*2139Sjp161948 			return 0;
743*2139Sjp161948 		/* More than three zeroes is an error */
744*2139Sjp161948 		if (v6stat.zero_cnt > 3)
745*2139Sjp161948 			return 0;
746*2139Sjp161948 		/* Can only have three zeroes if nothing else present */
747*2139Sjp161948 		else if (v6stat.zero_cnt == 3)
748*2139Sjp161948 			{
749*2139Sjp161948 			if (v6stat.total > 0)
750*2139Sjp161948 				return 0;
751*2139Sjp161948 			}
752*2139Sjp161948 		/* Can only have two zeroes if at start or end */
753*2139Sjp161948 		else if (v6stat.zero_cnt == 2)
754*2139Sjp161948 			{
755*2139Sjp161948 			if ((v6stat.zero_pos != 0)
756*2139Sjp161948 				&& (v6stat.zero_pos != v6stat.total))
757*2139Sjp161948 				return 0;
758*2139Sjp161948 			}
759*2139Sjp161948 		else
760*2139Sjp161948 		/* Can only have one zero if *not* start or end */
761*2139Sjp161948 			{
762*2139Sjp161948 			if ((v6stat.zero_pos == 0)
763*2139Sjp161948 				|| (v6stat.zero_pos == v6stat.total))
764*2139Sjp161948 				return 0;
765*2139Sjp161948 			}
766*2139Sjp161948 		}
767*2139Sjp161948 
768*2139Sjp161948 	/* Format result */
769*2139Sjp161948 
770*2139Sjp161948 	/* Copy initial part */
771*2139Sjp161948 	if (v6stat.zero_pos > 0)
772*2139Sjp161948 		memcpy(v6, v6stat.tmp, v6stat.zero_pos);
773*2139Sjp161948 	/* Zero middle */
774*2139Sjp161948 	if (v6stat.total != 16)
775*2139Sjp161948 		memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
776*2139Sjp161948 	/* Copy final part */
777*2139Sjp161948 	if (v6stat.total != v6stat.zero_pos)
778*2139Sjp161948 		memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
779*2139Sjp161948 			v6stat.tmp + v6stat.zero_pos,
780*2139Sjp161948 			v6stat.total - v6stat.zero_pos);
781*2139Sjp161948 
782*2139Sjp161948 	return 1;
783*2139Sjp161948 	}
784*2139Sjp161948 
ipv6_cb(const char * elem,int len,void * usr)785*2139Sjp161948 static int ipv6_cb(const char *elem, int len, void *usr)
786*2139Sjp161948 	{
787*2139Sjp161948 	IPV6_STAT *s = usr;
788*2139Sjp161948 	/* Error if 16 bytes written */
789*2139Sjp161948 	if (s->total == 16)
790*2139Sjp161948 		return 0;
791*2139Sjp161948 	if (len == 0)
792*2139Sjp161948 		{
793*2139Sjp161948 		/* Zero length element, corresponds to '::' */
794*2139Sjp161948 		if (s->zero_pos == -1)
795*2139Sjp161948 			s->zero_pos = s->total;
796*2139Sjp161948 		/* If we've already got a :: its an error */
797*2139Sjp161948 		else if (s->zero_pos != s->total)
798*2139Sjp161948 			return 0;
799*2139Sjp161948 		s->zero_cnt++;
800*2139Sjp161948 		}
801*2139Sjp161948 	else
802*2139Sjp161948 		{
803*2139Sjp161948 		/* If more than 4 characters could be final a.b.c.d form */
804*2139Sjp161948 		if (len > 4)
805*2139Sjp161948 			{
806*2139Sjp161948 			/* Need at least 4 bytes left */
807*2139Sjp161948 			if (s->total > 12)
808*2139Sjp161948 				return 0;
809*2139Sjp161948 			/* Must be end of string */
810*2139Sjp161948 			if (elem[len])
811*2139Sjp161948 				return 0;
812*2139Sjp161948 			if (!ipv4_from_asc(s->tmp + s->total, elem))
813*2139Sjp161948 				return 0;
814*2139Sjp161948 			s->total += 4;
815*2139Sjp161948 			}
816*2139Sjp161948 		else
817*2139Sjp161948 			{
818*2139Sjp161948 			if (!ipv6_hex(s->tmp + s->total, elem, len))
819*2139Sjp161948 				return 0;
820*2139Sjp161948 			s->total += 2;
821*2139Sjp161948 			}
822*2139Sjp161948 		}
823*2139Sjp161948 	return 1;
824*2139Sjp161948 	}
825*2139Sjp161948 
826*2139Sjp161948 /* Convert a string of up to 4 hex digits into the corresponding
827*2139Sjp161948  * IPv6 form.
828*2139Sjp161948  */
829*2139Sjp161948 
ipv6_hex(unsigned char * out,const char * in,int inlen)830*2139Sjp161948 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
831*2139Sjp161948 	{
832*2139Sjp161948 	unsigned char c;
833*2139Sjp161948 	unsigned int num = 0;
834*2139Sjp161948 	if (inlen > 4)
835*2139Sjp161948 		return 0;
836*2139Sjp161948 	while(inlen--)
837*2139Sjp161948 		{
838*2139Sjp161948 		c = *in++;
839*2139Sjp161948 		num <<= 4;
840*2139Sjp161948 		if ((c >= '0') && (c <= '9'))
841*2139Sjp161948 			num |= c - '0';
842*2139Sjp161948 		else if ((c >= 'A') && (c <= 'F'))
843*2139Sjp161948 			num |= c - 'A' + 10;
844*2139Sjp161948 		else if ((c >= 'a') && (c <= 'f'))
845*2139Sjp161948 			num |=  c - 'a' + 10;
846*2139Sjp161948 		else
847*2139Sjp161948 			return 0;
848*2139Sjp161948 		}
849*2139Sjp161948 	out[0] = num >> 8;
850*2139Sjp161948 	out[1] = num & 0xff;
851*2139Sjp161948 	return 1;
852*2139Sjp161948 	}
853*2139Sjp161948 
854*2139Sjp161948 
X509V3_NAME_from_section(X509_NAME * nm,STACK_OF (CONF_VALUE)* dn_sk,unsigned long chtype)855*2139Sjp161948 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE)*dn_sk,
856*2139Sjp161948 						unsigned long chtype)
857*2139Sjp161948 	{
858*2139Sjp161948 	CONF_VALUE *v;
859*2139Sjp161948 	int i, mval;
860*2139Sjp161948 	char *p, *type;
861*2139Sjp161948 	if (!nm)
862*2139Sjp161948 		return 0;
863*2139Sjp161948 
864*2139Sjp161948 	for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
865*2139Sjp161948 		{
866*2139Sjp161948 		v=sk_CONF_VALUE_value(dn_sk,i);
867*2139Sjp161948 		type=v->name;
868*2139Sjp161948 		/* Skip past any leading X. X: X, etc to allow for
869*2139Sjp161948 		 * multiple instances
870*2139Sjp161948 		 */
871*2139Sjp161948 		for(p = type; *p ; p++)
872*2139Sjp161948 #ifndef CHARSET_EBCDIC
873*2139Sjp161948 			if ((*p == ':') || (*p == ',') || (*p == '.'))
874*2139Sjp161948 #else
875*2139Sjp161948 			if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.']))
876*2139Sjp161948 #endif
877*2139Sjp161948 				{
878*2139Sjp161948 				p++;
879*2139Sjp161948 				if(*p) type = p;
880*2139Sjp161948 				break;
881*2139Sjp161948 				}
882*2139Sjp161948 #ifndef CHARSET_EBCDIC
883*2139Sjp161948 		if (*type == '+')
884*2139Sjp161948 #else
885*2139Sjp161948 		if (*type == os_toascii['+'])
886*2139Sjp161948 #endif
887*2139Sjp161948 			{
888*2139Sjp161948 			mval = -1;
889*2139Sjp161948 			type++;
890*2139Sjp161948 			}
891*2139Sjp161948 		else
892*2139Sjp161948 			mval = 0;
893*2139Sjp161948 		if (!X509_NAME_add_entry_by_txt(nm,type, chtype,
894*2139Sjp161948 				(unsigned char *) v->value,-1,-1,mval))
895*2139Sjp161948 					return 0;
896*2139Sjp161948 
897*2139Sjp161948 		}
898*2139Sjp161948 	return 1;
899*2139Sjp161948 	}
900