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