xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_utl.c (revision 2139:6243c3338933)
1 /* v3_utl.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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 /*
60  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
61  * Use is subject to license terms.
62  */
63 
64 #pragma ident	"%Z%%M%	%I%	%E% SMI"
65 
66 /* X509 v3 extension utilities */
67 
68 
69 #include <stdio.h>
70 #include <ctype.h>
71 #include "cryptlib.h"
72 #include <openssl/conf.h>
73 #include <openssl/x509v3.h>
74 #include <openssl/bn.h>
75 
76 static char *strip_spaces(char *name);
77 static int sk_strcmp(const char * const *a, const char * const *b);
78 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens);
79 static void str_free(void *str);
80 static int append_ia5(STACK **sk, ASN1_IA5STRING *email);
81 
82 static int a2i_ipadd(unsigned char *ipout, const char *ipasc);
83 static int ipv4_from_asc(unsigned char *v4, const char *in);
84 static int ipv6_from_asc(unsigned char *v6, const char *in);
85 static int ipv6_cb(const char *elem, int len, void *usr);
86 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
87 
88 /* Add a CONF_VALUE name value pair to stack */
89 
X509V3_add_value(const char * name,const char * value,STACK_OF (CONF_VALUE)** extlist)90 int X509V3_add_value(const char *name, const char *value,
91 						STACK_OF(CONF_VALUE) **extlist)
92 {
93 	CONF_VALUE *vtmp = NULL;
94 	char *tname = NULL, *tvalue = NULL;
95 	if(name && !(tname = BUF_strdup(name))) goto err;
96 	if(value && !(tvalue = BUF_strdup(value))) goto err;;
97 	if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
98 	if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
99 	vtmp->section = NULL;
100 	vtmp->name = tname;
101 	vtmp->value = tvalue;
102 	if(!sk_CONF_VALUE_push(*extlist, vtmp)) goto err;
103 	return 1;
104 	err:
105 	X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
106 	if(vtmp) OPENSSL_free(vtmp);
107 	if(tname) OPENSSL_free(tname);
108 	if(tvalue) OPENSSL_free(tvalue);
109 	return 0;
110 }
111 
X509V3_add_value_uchar(const char * name,const unsigned char * value,STACK_OF (CONF_VALUE)** extlist)112 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
113 			   STACK_OF(CONF_VALUE) **extlist)
114     {
115     return X509V3_add_value(name,(const char *)value,extlist);
116     }
117 
118 /* Free function for STACK_OF(CONF_VALUE) */
119 
X509V3_conf_free(CONF_VALUE * conf)120 void X509V3_conf_free(CONF_VALUE *conf)
121 {
122 	if(!conf) return;
123 	if(conf->name) OPENSSL_free(conf->name);
124 	if(conf->value) OPENSSL_free(conf->value);
125 	if(conf->section) OPENSSL_free(conf->section);
126 	OPENSSL_free(conf);
127 }
128 
X509V3_add_value_bool(const char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)129 int X509V3_add_value_bool(const char *name, int asn1_bool,
130 						STACK_OF(CONF_VALUE) **extlist)
131 {
132 	if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
133 	return X509V3_add_value(name, "FALSE", extlist);
134 }
135 
X509V3_add_value_bool_nf(char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)136 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
137 						STACK_OF(CONF_VALUE) **extlist)
138 {
139 	if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
140 	return 1;
141 }
142 
143 
i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD * method,ASN1_ENUMERATED * a)144 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
145 {
146 	BIGNUM *bntmp = NULL;
147 	char *strtmp = NULL;
148 	if(!a) return NULL;
149 	if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
150 	    !(strtmp = BN_bn2dec(bntmp)) )
151 		X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
152 	BN_free(bntmp);
153 	return strtmp;
154 }
155 
i2s_ASN1_INTEGER(X509V3_EXT_METHOD * method,ASN1_INTEGER * a)156 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
157 {
158 	BIGNUM *bntmp = NULL;
159 	char *strtmp = NULL;
160 	if(!a) return NULL;
161 	if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
162 	    !(strtmp = BN_bn2dec(bntmp)) )
163 		X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
164 	BN_free(bntmp);
165 	return strtmp;
166 }
167 
s2i_ASN1_INTEGER(X509V3_EXT_METHOD * method,char * value)168 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
169 {
170 	BIGNUM *bn = NULL;
171 	ASN1_INTEGER *aint;
172 	int isneg, ishex;
173 	int ret;
174 	if (!value) {
175 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE);
176 		return 0;
177 	}
178 	bn = BN_new();
179 	if (value[0] == '-') {
180 		value++;
181 		isneg = 1;
182 	} else isneg = 0;
183 
184 	if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
185 		value += 2;
186 		ishex = 1;
187 	} else ishex = 0;
188 
189 	if (ishex) ret = BN_hex2bn(&bn, value);
190 	else ret = BN_dec2bn(&bn, value);
191 
192 	if (!ret || value[ret]) {
193 		BN_free(bn);
194 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR);
195 		return 0;
196 	}
197 
198 	if (isneg && BN_is_zero(bn)) isneg = 0;
199 
200 	aint = BN_to_ASN1_INTEGER(bn, NULL);
201 	BN_free(bn);
202 	if (!aint) {
203 		X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
204 		return 0;
205 	}
206 	if (isneg) aint->type |= V_ASN1_NEG;
207 	return aint;
208 }
209 
X509V3_add_value_int(const char * name,ASN1_INTEGER * aint,STACK_OF (CONF_VALUE)** extlist)210 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
211 	     STACK_OF(CONF_VALUE) **extlist)
212 {
213 	char *strtmp;
214 	int ret;
215 	if(!aint) return 1;
216 	if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
217 	ret = X509V3_add_value(name, strtmp, extlist);
218 	OPENSSL_free(strtmp);
219 	return ret;
220 }
221 
X509V3_get_value_bool(CONF_VALUE * value,int * asn1_bool)222 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
223 {
224 	char *btmp;
225 	if(!(btmp = value->value)) goto err;
226 	if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
227 		 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
228 		|| !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
229 		*asn1_bool = 0xff;
230 		return 1;
231 	} else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
232 		 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
233 		|| !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
234 		*asn1_bool = 0;
235 		return 1;
236 	}
237 	err:
238 	X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
239 	X509V3_conf_err(value);
240 	return 0;
241 }
242 
X509V3_get_value_int(CONF_VALUE * value,ASN1_INTEGER ** aint)243 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
244 {
245 	ASN1_INTEGER *itmp;
246 	if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
247 		X509V3_conf_err(value);
248 		return 0;
249 	}
250 	*aint = itmp;
251 	return 1;
252 }
253 
254 #define HDR_NAME	1
255 #define HDR_VALUE	2
256 
257 /*#define DEBUG*/
258 
STACK_OF(CONF_VALUE)259 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
260 {
261 	char *p, *q, c;
262 	char *ntmp, *vtmp;
263 	STACK_OF(CONF_VALUE) *values = NULL;
264 	char *linebuf;
265 	int state;
266 	/* We are going to modify the line so copy it first */
267 	linebuf = BUF_strdup(line);
268 	state = HDR_NAME;
269 	ntmp = NULL;
270 	/* Go through all characters */
271 	for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
272 
273 		switch(state) {
274 			case HDR_NAME:
275 			if(c == ':') {
276 				state = HDR_VALUE;
277 				*p = 0;
278 				ntmp = strip_spaces(q);
279 				if(!ntmp) {
280 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
281 					goto err;
282 				}
283 				q = p + 1;
284 			} else if(c == ',') {
285 				*p = 0;
286 				ntmp = strip_spaces(q);
287 				q = p + 1;
288 #if 0
289 				printf("%s\n", ntmp);
290 #endif
291 				if(!ntmp) {
292 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
293 					goto err;
294 				}
295 				X509V3_add_value(ntmp, NULL, &values);
296 			}
297 			break ;
298 
299 			case HDR_VALUE:
300 			if(c == ',') {
301 				state = HDR_NAME;
302 				*p = 0;
303 				vtmp = strip_spaces(q);
304 #if 0
305 				printf("%s\n", ntmp);
306 #endif
307 				if(!vtmp) {
308 					X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
309 					goto err;
310 				}
311 				X509V3_add_value(ntmp, vtmp, &values);
312 				ntmp = NULL;
313 				q = p + 1;
314 			}
315 
316 		}
317 	}
318 
319 	if(state == HDR_VALUE) {
320 		vtmp = strip_spaces(q);
321 #if 0
322 		printf("%s=%s\n", ntmp, vtmp);
323 #endif
324 		if(!vtmp) {
325 			X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
326 			goto err;
327 		}
328 		X509V3_add_value(ntmp, vtmp, &values);
329 	} else {
330 		ntmp = strip_spaces(q);
331 #if 0
332 		printf("%s\n", ntmp);
333 #endif
334 		if(!ntmp) {
335 			X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
336 			goto err;
337 		}
338 		X509V3_add_value(ntmp, NULL, &values);
339 	}
340 OPENSSL_free(linebuf);
341 return values;
342 
343 err:
344 OPENSSL_free(linebuf);
345 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
346 return NULL;
347 
348 }
349 
350 /* Delete leading and trailing spaces from a string */
strip_spaces(char * name)351 static char *strip_spaces(char *name)
352 {
353 	char *p, *q;
354 	/* Skip over leading spaces */
355 	p = name;
356 	while(*p && isspace((unsigned char)*p)) p++;
357 	if(!*p) return NULL;
358 	q = p + strlen(p) - 1;
359 	while((q != p) && isspace((unsigned char)*q)) q--;
360 	if(p != q) q[1] = 0;
361 	if(!*p) return NULL;
362 	return p;
363 }
364 
365 /* hex string utilities */
366 
367 /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
368  * hex representation
369  * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
370  */
371 
hex_to_string(unsigned char * buffer,long len)372 char *hex_to_string(unsigned char *buffer, long len)
373 {
374 	char *tmp, *q;
375 	unsigned char *p;
376 	int i;
377 	static char hexdig[] = "0123456789ABCDEF";
378 	if(!buffer || !len) return NULL;
379 	if(!(tmp = OPENSSL_malloc(len * 3 + 1))) {
380 		X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
381 		return NULL;
382 	}
383 	q = tmp;
384 	for(i = 0, p = buffer; i < len; i++,p++) {
385 		*q++ = hexdig[(*p >> 4) & 0xf];
386 		*q++ = hexdig[*p & 0xf];
387 		*q++ = ':';
388 	}
389 	q[-1] = 0;
390 #ifdef CHARSET_EBCDIC
391 	ebcdic2ascii(tmp, tmp, q - tmp - 1);
392 #endif
393 
394 	return tmp;
395 }
396 
397 /* Give a string of hex digits convert to
398  * a buffer
399  */
400 
string_to_hex(char * str,long * len)401 unsigned char *string_to_hex(char *str, long *len)
402 {
403 	unsigned char *hexbuf, *q;
404 	unsigned char ch, cl, *p;
405 	if(!str) {
406 		X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
407 		return NULL;
408 	}
409 	if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err;
410 	for(p = (unsigned char *)str, q = hexbuf; *p;) {
411 		ch = *p++;
412 #ifdef CHARSET_EBCDIC
413 		ch = os_toebcdic[ch];
414 #endif
415 		if(ch == ':') continue;
416 		cl = *p++;
417 #ifdef CHARSET_EBCDIC
418 		cl = os_toebcdic[cl];
419 #endif
420 		if(!cl) {
421 			X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
422 			OPENSSL_free(hexbuf);
423 			return NULL;
424 		}
425 		if(isupper(ch)) ch = tolower(ch);
426 		if(isupper(cl)) cl = tolower(cl);
427 
428 		if((ch >= '0') && (ch <= '9')) ch -= '0';
429 		else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
430 		else goto badhex;
431 
432 		if((cl >= '0') && (cl <= '9')) cl -= '0';
433 		else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
434 		else goto badhex;
435 
436 		*q++ = (ch << 4) | cl;
437 	}
438 
439 	if(len) *len = q - hexbuf;
440 
441 	return hexbuf;
442 
443 	err:
444 	if(hexbuf) OPENSSL_free(hexbuf);
445 	X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
446 	return NULL;
447 
448 	badhex:
449 	OPENSSL_free(hexbuf);
450 	X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
451 	return NULL;
452 
453 }
454 
455 /* V2I name comparison function: returns zero if 'name' matches
456  * cmp or cmp.*
457  */
458 
name_cmp(const char * name,const char * cmp)459 int name_cmp(const char *name, const char *cmp)
460 {
461 	int len, ret;
462 	char c;
463 	len = strlen(cmp);
464 	if((ret = strncmp(name, cmp, len))) return ret;
465 	c = name[len];
466 	if(!c || (c=='.')) return 0;
467 	return 1;
468 }
469 
sk_strcmp(const char * const * a,const char * const * b)470 static int sk_strcmp(const char * const *a, const char * const *b)
471 {
472 	return strcmp(*a, *b);
473 }
474 
X509_get1_email(X509 * x)475 STACK *X509_get1_email(X509 *x)
476 {
477 	GENERAL_NAMES *gens;
478 	STACK *ret;
479 	gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
480 	ret = get_email(X509_get_subject_name(x), gens);
481 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
482 	return ret;
483 }
484 
X509_REQ_get1_email(X509_REQ * x)485 STACK *X509_REQ_get1_email(X509_REQ *x)
486 {
487 	GENERAL_NAMES *gens;
488 	STACK_OF(X509_EXTENSION) *exts;
489 	STACK *ret;
490 	exts = X509_REQ_get_extensions(x);
491 	gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
492 	ret = get_email(X509_REQ_get_subject_name(x), gens);
493 	sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
494 	sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
495 	return ret;
496 }
497 
498 
get_email(X509_NAME * name,GENERAL_NAMES * gens)499 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)
500 {
501 	STACK *ret = NULL;
502 	X509_NAME_ENTRY *ne;
503 	ASN1_IA5STRING *email;
504 	GENERAL_NAME *gen;
505 	int i;
506 	/* Now add any email address(es) to STACK */
507 	i = -1;
508 	/* First supplied X509_NAME */
509 	while((i = X509_NAME_get_index_by_NID(name,
510 					 NID_pkcs9_emailAddress, i)) >= 0) {
511 		ne = X509_NAME_get_entry(name, i);
512 		email = X509_NAME_ENTRY_get_data(ne);
513 		if(!append_ia5(&ret, email)) return NULL;
514 	}
515 	for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
516 	{
517 		gen = sk_GENERAL_NAME_value(gens, i);
518 		if(gen->type != GEN_EMAIL) continue;
519 		if(!append_ia5(&ret, gen->d.ia5)) return NULL;
520 	}
521 	return ret;
522 }
523 
str_free(void * str)524 static void str_free(void *str)
525 {
526 	OPENSSL_free(str);
527 }
528 
append_ia5(STACK ** sk,ASN1_IA5STRING * email)529 static int append_ia5(STACK **sk, ASN1_IA5STRING *email)
530 {
531 	char *emtmp;
532 	/* First some sanity checks */
533 	if(email->type != V_ASN1_IA5STRING) return 1;
534 	if(!email->data || !email->length) return 1;
535 	if(!*sk) *sk = sk_new(sk_strcmp);
536 	if(!*sk) return 0;
537 	/* Don't add duplicates */
538 	if(sk_find(*sk, (char *)email->data) != -1) return 1;
539 	emtmp = BUF_strdup((char *)email->data);
540 	if(!emtmp || !sk_push(*sk, emtmp)) {
541 		X509_email_free(*sk);
542 		*sk = NULL;
543 		return 0;
544 	}
545 	return 1;
546 }
547 
X509_email_free(STACK * sk)548 void X509_email_free(STACK *sk)
549 {
550 	sk_pop_free(sk, str_free);
551 }
552 
553 /* Convert IP addresses both IPv4 and IPv6 into an
554  * OCTET STRING compatible with RFC3280.
555  */
556 
a2i_IPADDRESS(const char * ipasc)557 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
558 	{
559 	unsigned char ipout[16];
560 	ASN1_OCTET_STRING *ret;
561 	int iplen;
562 
563 	/* If string contains a ':' assume IPv6 */
564 
565 	iplen = a2i_ipadd(ipout, ipasc);
566 
567 	if (!iplen)
568 		return NULL;
569 
570 	ret = ASN1_OCTET_STRING_new();
571 	if (!ret)
572 		return NULL;
573 	if (!ASN1_OCTET_STRING_set(ret, ipout, iplen))
574 		{
575 		ASN1_OCTET_STRING_free(ret);
576 		return NULL;
577 		}
578 	return ret;
579 	}
580 
a2i_IPADDRESS_NC(const char * ipasc)581 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
582 	{
583 	ASN1_OCTET_STRING *ret = NULL;
584 	unsigned char ipout[32];
585 	char *iptmp = NULL, *p;
586 	int iplen1, iplen2;
587 	p = strchr(ipasc,'/');
588 	if (!p)
589 		return NULL;
590 	iptmp = BUF_strdup(ipasc);
591 	if (!iptmp)
592 		return NULL;
593 	p = iptmp + (p - ipasc);
594 	*p++ = 0;
595 
596 	iplen1 = a2i_ipadd(ipout, iptmp);
597 
598 	if (!iplen1)
599 		goto err;
600 
601 	iplen2 = a2i_ipadd(ipout + iplen1, p);
602 
603 	OPENSSL_free(iptmp);
604 	iptmp = NULL;
605 
606 	if (!iplen2 || (iplen1 != iplen2))
607 		goto err;
608 
609 	ret = ASN1_OCTET_STRING_new();
610 	if (!ret)
611 		goto err;
612 	if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
613 		goto err;
614 
615 	return ret;
616 
617 	err:
618 	if (iptmp)
619 		OPENSSL_free(iptmp);
620 	if (ret)
621 		ASN1_OCTET_STRING_free(ret);
622 	return NULL;
623 	}
624 
625 
a2i_ipadd(unsigned char * ipout,const char * ipasc)626 static int a2i_ipadd(unsigned char *ipout, const char *ipasc)
627 	{
628 	/* If string contains a ':' assume IPv6 */
629 
630 	if (strchr(ipasc, ':'))
631 		{
632 		if (!ipv6_from_asc(ipout, ipasc))
633 			return 0;
634 		return 16;
635 		}
636 	else
637 		{
638 		if (!ipv4_from_asc(ipout, ipasc))
639 			return 0;
640 		return 4;
641 		}
642 	}
643 
644 #if	defined(_BOOT)
645 /* This function was copied from bio/b_sock.c */
646 /* The reason I have implemented this instead of using sscanf is because
647  * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
get_ip(const char * str,unsigned char ip[4])648 static int get_ip(const char *str, unsigned char ip[4])
649 	{
650 	unsigned int tmp[4];
651 	int num=0,c,ok=0;
652 
653 	tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
654 
655 	for (;;)
656 		{
657 		c= *(str++);
658 		if ((c >= '0') && (c <= '9'))
659 			{
660 			ok=1;
661 			tmp[num]=tmp[num]*10+c-'0';
662 			if (tmp[num] > 255) return(0);
663 			}
664 		else if (c == '.')
665 			{
666 			if (!ok) return(-1);
667 			if (num == 3) return(0);
668 			num++;
669 			ok=0;
670 			}
671 		else if (c == '\0' && (num == 3) && ok)
672 			break;
673 		else
674 			return(0);
675 		}
676 	ip[0]=tmp[0];
677 	ip[1]=tmp[1];
678 	ip[2]=tmp[2];
679 	ip[3]=tmp[3];
680 	return(1);
681 	}
682 #endif /* _BOOT */
683 
ipv4_from_asc(unsigned char * v4,const char * in)684 static int ipv4_from_asc(unsigned char *v4, const char *in)
685 	{
686 	int a0, a1, a2, a3;
687 
688 #if	defined(_BOOT)
689 	if (get_ip(in, v4) != 1)
690 		return 0;
691 #else	/* _BOOT */
692 	if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
693 		return 0;
694 	if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
695 		|| (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
696 		return 0;
697 	v4[0] = a0;
698 	v4[1] = a1;
699 	v4[2] = a2;
700 	v4[3] = a3;
701 #endif	/* _BOOT */
702 	return 1;
703 	}
704 
705 typedef struct {
706 		/* Temporary store for IPV6 output */
707 		unsigned char tmp[16];
708 		/* Total number of bytes in tmp */
709 		int total;
710 		/* The position of a zero (corresponding to '::') */
711 		int zero_pos;
712 		/* Number of zeroes */
713 		int zero_cnt;
714 	} IPV6_STAT;
715 
716 
ipv6_from_asc(unsigned char * v6,const char * in)717 static int ipv6_from_asc(unsigned char *v6, const char *in)
718 	{
719 	IPV6_STAT v6stat;
720 	v6stat.total = 0;
721 	v6stat.zero_pos = -1;
722 	v6stat.zero_cnt = 0;
723 	/* Treat the IPv6 representation as a list of values
724 	 * separated by ':'. The presence of a '::' will parse
725  	 * as one, two or three zero length elements.
726 	 */
727 	if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
728 		return 0;
729 
730 	/* Now for some sanity checks */
731 
732 	if (v6stat.zero_pos == -1)
733 		{
734 		/* If no '::' must have exactly 16 bytes */
735 		if (v6stat.total != 16)
736 			return 0;
737 		}
738 	else
739 		{
740 		/* If '::' must have less than 16 bytes */
741 		if (v6stat.total == 16)
742 			return 0;
743 		/* More than three zeroes is an error */
744 		if (v6stat.zero_cnt > 3)
745 			return 0;
746 		/* Can only have three zeroes if nothing else present */
747 		else if (v6stat.zero_cnt == 3)
748 			{
749 			if (v6stat.total > 0)
750 				return 0;
751 			}
752 		/* Can only have two zeroes if at start or end */
753 		else if (v6stat.zero_cnt == 2)
754 			{
755 			if ((v6stat.zero_pos != 0)
756 				&& (v6stat.zero_pos != v6stat.total))
757 				return 0;
758 			}
759 		else
760 		/* Can only have one zero if *not* start or end */
761 			{
762 			if ((v6stat.zero_pos == 0)
763 				|| (v6stat.zero_pos == v6stat.total))
764 				return 0;
765 			}
766 		}
767 
768 	/* Format result */
769 
770 	/* Copy initial part */
771 	if (v6stat.zero_pos > 0)
772 		memcpy(v6, v6stat.tmp, v6stat.zero_pos);
773 	/* Zero middle */
774 	if (v6stat.total != 16)
775 		memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
776 	/* Copy final part */
777 	if (v6stat.total != v6stat.zero_pos)
778 		memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
779 			v6stat.tmp + v6stat.zero_pos,
780 			v6stat.total - v6stat.zero_pos);
781 
782 	return 1;
783 	}
784 
ipv6_cb(const char * elem,int len,void * usr)785 static int ipv6_cb(const char *elem, int len, void *usr)
786 	{
787 	IPV6_STAT *s = usr;
788 	/* Error if 16 bytes written */
789 	if (s->total == 16)
790 		return 0;
791 	if (len == 0)
792 		{
793 		/* Zero length element, corresponds to '::' */
794 		if (s->zero_pos == -1)
795 			s->zero_pos = s->total;
796 		/* If we've already got a :: its an error */
797 		else if (s->zero_pos != s->total)
798 			return 0;
799 		s->zero_cnt++;
800 		}
801 	else
802 		{
803 		/* If more than 4 characters could be final a.b.c.d form */
804 		if (len > 4)
805 			{
806 			/* Need at least 4 bytes left */
807 			if (s->total > 12)
808 				return 0;
809 			/* Must be end of string */
810 			if (elem[len])
811 				return 0;
812 			if (!ipv4_from_asc(s->tmp + s->total, elem))
813 				return 0;
814 			s->total += 4;
815 			}
816 		else
817 			{
818 			if (!ipv6_hex(s->tmp + s->total, elem, len))
819 				return 0;
820 			s->total += 2;
821 			}
822 		}
823 	return 1;
824 	}
825 
826 /* Convert a string of up to 4 hex digits into the corresponding
827  * IPv6 form.
828  */
829 
ipv6_hex(unsigned char * out,const char * in,int inlen)830 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
831 	{
832 	unsigned char c;
833 	unsigned int num = 0;
834 	if (inlen > 4)
835 		return 0;
836 	while(inlen--)
837 		{
838 		c = *in++;
839 		num <<= 4;
840 		if ((c >= '0') && (c <= '9'))
841 			num |= c - '0';
842 		else if ((c >= 'A') && (c <= 'F'))
843 			num |= c - 'A' + 10;
844 		else if ((c >= 'a') && (c <= 'f'))
845 			num |=  c - 'a' + 10;
846 		else
847 			return 0;
848 		}
849 	out[0] = num >> 8;
850 	out[1] = num & 0xff;
851 	return 1;
852 	}
853 
854 
X509V3_NAME_from_section(X509_NAME * nm,STACK_OF (CONF_VALUE)* dn_sk,unsigned long chtype)855 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE)*dn_sk,
856 						unsigned long chtype)
857 	{
858 	CONF_VALUE *v;
859 	int i, mval;
860 	char *p, *type;
861 	if (!nm)
862 		return 0;
863 
864 	for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
865 		{
866 		v=sk_CONF_VALUE_value(dn_sk,i);
867 		type=v->name;
868 		/* Skip past any leading X. X: X, etc to allow for
869 		 * multiple instances
870 		 */
871 		for(p = type; *p ; p++)
872 #ifndef CHARSET_EBCDIC
873 			if ((*p == ':') || (*p == ',') || (*p == '.'))
874 #else
875 			if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.']))
876 #endif
877 				{
878 				p++;
879 				if(*p) type = p;
880 				break;
881 				}
882 #ifndef CHARSET_EBCDIC
883 		if (*type == '+')
884 #else
885 		if (*type == os_toascii['+'])
886 #endif
887 			{
888 			mval = -1;
889 			type++;
890 			}
891 		else
892 			mval = 0;
893 		if (!X509_NAME_add_entry_by_txt(nm,type, chtype,
894 				(unsigned char *) v->value,-1,-1,mval))
895 					return 0;
896 
897 		}
898 	return 1;
899 	}
900