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 <ctype.h>
62 #include <stdio.h>
63 #include <string.h>
64
65 #include <openssl/bn.h>
66 #include <openssl/buf.h>
67 #include <openssl/conf.h>
68 #include <openssl/err.h>
69 #include <openssl/mem.h>
70 #include <openssl/obj.h>
71 #include <openssl/x509v3.h>
72
73 #include "../conf/internal.h"
74 #include "../internal.h"
75 #include "internal.h"
76
77
78 static char *strip_spaces(char *name);
79 static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b);
80 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
81 GENERAL_NAMES *gens);
82 static void str_free(OPENSSL_STRING str);
83 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
84
85 static int ipv4_from_asc(unsigned char *v4, const char *in);
86 static int ipv6_from_asc(unsigned char *v6, const char *in);
87 static int ipv6_cb(const char *elem, int len, void *usr);
88 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
89
90 /* Add a CONF_VALUE name value pair to stack */
91
X509V3_add_value(const char * name,const char * value,STACK_OF (CONF_VALUE)** extlist)92 int X509V3_add_value(const char *name, const char *value,
93 STACK_OF(CONF_VALUE) **extlist)
94 {
95 CONF_VALUE *vtmp = NULL;
96 char *tname = NULL, *tvalue = NULL;
97 if (name && !(tname = BUF_strdup(name)))
98 goto err;
99 if (value && !(tvalue = BUF_strdup(value)))
100 goto err;
101 if (!(vtmp = CONF_VALUE_new()))
102 goto err;
103 if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
104 goto err;
105 vtmp->section = NULL;
106 vtmp->name = tname;
107 vtmp->value = tvalue;
108 if (!sk_CONF_VALUE_push(*extlist, vtmp))
109 goto err;
110 return 1;
111 err:
112 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
113 if (vtmp)
114 OPENSSL_free(vtmp);
115 if (tname)
116 OPENSSL_free(tname);
117 if (tvalue)
118 OPENSSL_free(tvalue);
119 return 0;
120 }
121
X509V3_add_value_uchar(const char * name,const unsigned char * value,STACK_OF (CONF_VALUE)** extlist)122 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
123 STACK_OF(CONF_VALUE) **extlist)
124 {
125 return X509V3_add_value(name, (const char *)value, extlist);
126 }
127
128 /* Free function for STACK_OF(CONF_VALUE) */
129
X509V3_conf_free(CONF_VALUE * conf)130 void X509V3_conf_free(CONF_VALUE *conf)
131 {
132 if (!conf)
133 return;
134 if (conf->name)
135 OPENSSL_free(conf->name);
136 if (conf->value)
137 OPENSSL_free(conf->value);
138 if (conf->section)
139 OPENSSL_free(conf->section);
140 OPENSSL_free(conf);
141 }
142
X509V3_add_value_bool(const char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)143 int X509V3_add_value_bool(const char *name, int asn1_bool,
144 STACK_OF(CONF_VALUE) **extlist)
145 {
146 if (asn1_bool)
147 return X509V3_add_value(name, "TRUE", extlist);
148 return X509V3_add_value(name, "FALSE", extlist);
149 }
150
X509V3_add_value_bool_nf(char * name,int asn1_bool,STACK_OF (CONF_VALUE)** extlist)151 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
152 STACK_OF(CONF_VALUE) **extlist)
153 {
154 if (asn1_bool)
155 return X509V3_add_value(name, "TRUE", extlist);
156 return 1;
157 }
158
bignum_to_string(const BIGNUM * bn)159 static char *bignum_to_string(const BIGNUM *bn)
160 {
161 char *tmp, *ret;
162 size_t len;
163
164 /*
165 * Display large numbers in hex and small numbers in decimal. Converting to
166 * decimal takes quadratic time and is no more useful than hex for large
167 * numbers.
168 */
169 if (BN_num_bits(bn) < 32) {
170 return BN_bn2dec(bn);
171 }
172
173 tmp = BN_bn2hex(bn);
174 if (tmp == NULL) {
175 return NULL;
176 }
177
178 len = strlen(tmp) + 3;
179 ret = OPENSSL_malloc(len);
180 if (ret == NULL) {
181 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
182 OPENSSL_free(tmp);
183 return NULL;
184 }
185
186 /* Prepend "0x", but place it after the "-" if negative. */
187 if (tmp[0] == '-') {
188 BUF_strlcpy(ret, "-0x", len);
189 BUF_strlcat(ret, tmp + 1, len);
190 } else {
191 BUF_strlcpy(ret, "0x", len);
192 BUF_strlcat(ret, tmp, len);
193 }
194 OPENSSL_free(tmp);
195 return ret;
196 }
197
i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD * method,ASN1_ENUMERATED * a)198 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
199 {
200 BIGNUM *bntmp = NULL;
201 char *strtmp = NULL;
202 if (!a)
203 return NULL;
204 if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
205 !(strtmp = bignum_to_string(bntmp)))
206 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
207 BN_free(bntmp);
208 return strtmp;
209 }
210
i2s_ASN1_INTEGER(X509V3_EXT_METHOD * method,ASN1_INTEGER * a)211 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
212 {
213 BIGNUM *bntmp = NULL;
214 char *strtmp = NULL;
215 if (!a)
216 return NULL;
217 if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
218 !(strtmp = bignum_to_string(bntmp)))
219 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
220 BN_free(bntmp);
221 return strtmp;
222 }
223
s2i_ASN1_INTEGER(X509V3_EXT_METHOD * method,char * value)224 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
225 {
226 BIGNUM *bn = NULL;
227 ASN1_INTEGER *aint;
228 int isneg, ishex;
229 int ret;
230 if (!value) {
231 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
232 return 0;
233 }
234 bn = BN_new();
235 if (value[0] == '-') {
236 value++;
237 isneg = 1;
238 } else
239 isneg = 0;
240
241 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
242 value += 2;
243 ishex = 1;
244 } else
245 ishex = 0;
246
247 if (ishex)
248 ret = BN_hex2bn(&bn, value);
249 else
250 ret = BN_dec2bn(&bn, value);
251
252 if (!ret || value[ret]) {
253 BN_free(bn);
254 OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR);
255 return 0;
256 }
257
258 if (isneg && BN_is_zero(bn))
259 isneg = 0;
260
261 aint = BN_to_ASN1_INTEGER(bn, NULL);
262 BN_free(bn);
263 if (!aint) {
264 OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
265 return 0;
266 }
267 if (isneg)
268 aint->type |= V_ASN1_NEG;
269 return aint;
270 }
271
X509V3_add_value_int(const char * name,ASN1_INTEGER * aint,STACK_OF (CONF_VALUE)** extlist)272 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
273 STACK_OF(CONF_VALUE) **extlist)
274 {
275 char *strtmp;
276 int ret;
277 if (!aint)
278 return 1;
279 if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
280 return 0;
281 ret = X509V3_add_value(name, strtmp, extlist);
282 OPENSSL_free(strtmp);
283 return ret;
284 }
285
X509V3_get_value_bool(CONF_VALUE * value,int * asn1_bool)286 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
287 {
288 char *btmp;
289 if (!(btmp = value->value))
290 goto err;
291 if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
292 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
293 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
294 *asn1_bool = 0xff;
295 return 1;
296 } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
297 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
298 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
299 *asn1_bool = 0;
300 return 1;
301 }
302 err:
303 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING);
304 X509V3_conf_err(value);
305 return 0;
306 }
307
X509V3_get_value_int(CONF_VALUE * value,ASN1_INTEGER ** aint)308 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
309 {
310 ASN1_INTEGER *itmp;
311 if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
312 X509V3_conf_err(value);
313 return 0;
314 }
315 *aint = itmp;
316 return 1;
317 }
318
319 #define HDR_NAME 1
320 #define HDR_VALUE 2
321
322 /*
323 * #define DEBUG
324 */
325
STACK_OF(CONF_VALUE)326 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
327 {
328 char *p, *q, c;
329 char *ntmp, *vtmp;
330 STACK_OF(CONF_VALUE) *values = NULL;
331 char *linebuf;
332 int state;
333 /* We are going to modify the line so copy it first */
334 linebuf = BUF_strdup(line);
335 if (linebuf == NULL) {
336 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
337 goto err;
338 }
339 state = HDR_NAME;
340 ntmp = NULL;
341 /* Go through all characters */
342 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
343 p++) {
344
345 switch (state) {
346 case HDR_NAME:
347 if (c == ':') {
348 state = HDR_VALUE;
349 *p = 0;
350 ntmp = strip_spaces(q);
351 if (!ntmp) {
352 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
353 goto err;
354 }
355 q = p + 1;
356 } else if (c == ',') {
357 *p = 0;
358 ntmp = strip_spaces(q);
359 q = p + 1;
360 #if 0
361 printf("%s\n", ntmp);
362 #endif
363 if (!ntmp) {
364 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
365 goto err;
366 }
367 X509V3_add_value(ntmp, NULL, &values);
368 }
369 break;
370
371 case HDR_VALUE:
372 if (c == ',') {
373 state = HDR_NAME;
374 *p = 0;
375 vtmp = strip_spaces(q);
376 #if 0
377 printf("%s\n", ntmp);
378 #endif
379 if (!vtmp) {
380 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
381 goto err;
382 }
383 X509V3_add_value(ntmp, vtmp, &values);
384 ntmp = NULL;
385 q = p + 1;
386 }
387
388 }
389 }
390
391 if (state == HDR_VALUE) {
392 vtmp = strip_spaces(q);
393 #if 0
394 printf("%s=%s\n", ntmp, vtmp);
395 #endif
396 if (!vtmp) {
397 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
398 goto err;
399 }
400 X509V3_add_value(ntmp, vtmp, &values);
401 } else {
402 ntmp = strip_spaces(q);
403 #if 0
404 printf("%s\n", ntmp);
405 #endif
406 if (!ntmp) {
407 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
408 goto err;
409 }
410 X509V3_add_value(ntmp, NULL, &values);
411 }
412 OPENSSL_free(linebuf);
413 return values;
414
415 err:
416 OPENSSL_free(linebuf);
417 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
418 return NULL;
419
420 }
421
422 /* Delete leading and trailing spaces from a string */
strip_spaces(char * name)423 static char *strip_spaces(char *name)
424 {
425 char *p, *q;
426 /* Skip over leading spaces */
427 p = name;
428 while (*p && isspace((unsigned char)*p))
429 p++;
430 if (!*p)
431 return NULL;
432 q = p + strlen(p) - 1;
433 while ((q != p) && isspace((unsigned char)*q))
434 q--;
435 if (p != q)
436 q[1] = 0;
437 if (!*p)
438 return NULL;
439 return p;
440 }
441
442 /* hex string utilities */
443
444 /*
445 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
446 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
447 * on EBCDIC machines)
448 */
449
x509v3_bytes_to_hex(const unsigned char * buffer,long len)450 char *x509v3_bytes_to_hex(const unsigned char *buffer, long len)
451 {
452 char *tmp, *q;
453 const unsigned char *p;
454 int i;
455 static const char hexdig[] = "0123456789ABCDEF";
456 if (!buffer || !len)
457 return NULL;
458 if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
459 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
460 return NULL;
461 }
462 q = tmp;
463 for (i = 0, p = buffer; i < len; i++, p++) {
464 *q++ = hexdig[(*p >> 4) & 0xf];
465 *q++ = hexdig[*p & 0xf];
466 *q++ = ':';
467 }
468 q[-1] = 0;
469
470 return tmp;
471 }
472
x509v3_hex_to_bytes(const char * str,long * len)473 unsigned char *x509v3_hex_to_bytes(const char *str, long *len)
474 {
475 unsigned char *hexbuf, *q;
476 unsigned char ch, cl, *p;
477 if (!str) {
478 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
479 return NULL;
480 }
481 if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
482 goto err;
483 for (p = (unsigned char *)str, q = hexbuf; *p;) {
484 ch = *p++;
485 if (ch == ':')
486 continue;
487 cl = *p++;
488 if (!cl) {
489 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS);
490 OPENSSL_free(hexbuf);
491 return NULL;
492 }
493
494 if ((ch >= '0') && (ch <= '9'))
495 ch -= '0';
496 else if ((ch >= 'a') && (ch <= 'f'))
497 ch -= 'a' - 10;
498 else if ((ch >= 'A') && (ch <= 'F'))
499 ch -= 'A' - 10;
500 else
501 goto badhex;
502
503 if ((cl >= '0') && (cl <= '9'))
504 cl -= '0';
505 else if ((cl >= 'a') && (cl <= 'f'))
506 cl -= 'a' - 10;
507 else if ((cl >= 'A') && (cl <= 'F'))
508 cl -= 'A' - 10;
509 else
510 goto badhex;
511
512 *q++ = (ch << 4) | cl;
513 }
514
515 if (len)
516 *len = q - hexbuf;
517
518 return hexbuf;
519
520 err:
521 if (hexbuf)
522 OPENSSL_free(hexbuf);
523 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
524 return NULL;
525
526 badhex:
527 OPENSSL_free(hexbuf);
528 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT);
529 return NULL;
530
531 }
532
x509v3_name_cmp(const char * name,const char * cmp)533 int x509v3_name_cmp(const char *name, const char *cmp)
534 {
535 int len, ret;
536 char c;
537 len = strlen(cmp);
538 if ((ret = strncmp(name, cmp, len)))
539 return ret;
540 c = name[len];
541 if (!c || (c == '.'))
542 return 0;
543 return 1;
544 }
545
sk_strcmp(const OPENSSL_STRING * a,const OPENSSL_STRING * b)546 static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b)
547 {
548 return strcmp(*a, *b);
549 }
550
STACK_OF(OPENSSL_STRING)551 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
552 {
553 GENERAL_NAMES *gens;
554 STACK_OF(OPENSSL_STRING) *ret;
555
556 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
557 ret = get_email(X509_get_subject_name(x), gens);
558 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
559 return ret;
560 }
561
STACK_OF(OPENSSL_STRING)562 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
563 {
564 AUTHORITY_INFO_ACCESS *info;
565 STACK_OF(OPENSSL_STRING) *ret = NULL;
566 size_t i;
567
568 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
569 if (!info)
570 return NULL;
571 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
572 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
573 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
574 if (ad->location->type == GEN_URI) {
575 if (!append_ia5
576 (&ret, ad->location->d.uniformResourceIdentifier))
577 break;
578 }
579 }
580 }
581 AUTHORITY_INFO_ACCESS_free(info);
582 return ret;
583 }
584
STACK_OF(OPENSSL_STRING)585 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
586 {
587 GENERAL_NAMES *gens;
588 STACK_OF(X509_EXTENSION) *exts;
589 STACK_OF(OPENSSL_STRING) *ret;
590
591 exts = X509_REQ_get_extensions(x);
592 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
593 ret = get_email(X509_REQ_get_subject_name(x), gens);
594 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
595 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
596 return ret;
597 }
598
STACK_OF(OPENSSL_STRING)599 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
600 GENERAL_NAMES *gens)
601 {
602 STACK_OF(OPENSSL_STRING) *ret = NULL;
603 X509_NAME_ENTRY *ne;
604 ASN1_IA5STRING *email;
605 GENERAL_NAME *gen;
606 int i;
607 size_t j;
608 /* Now add any email address(es) to STACK */
609 i = -1;
610 /* First supplied X509_NAME */
611 while ((i = X509_NAME_get_index_by_NID(name,
612 NID_pkcs9_emailAddress, i)) >= 0) {
613 ne = X509_NAME_get_entry(name, i);
614 email = X509_NAME_ENTRY_get_data(ne);
615 if (!append_ia5(&ret, email))
616 return NULL;
617 }
618 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
619 gen = sk_GENERAL_NAME_value(gens, j);
620 if (gen->type != GEN_EMAIL)
621 continue;
622 if (!append_ia5(&ret, gen->d.ia5))
623 return NULL;
624 }
625 return ret;
626 }
627
str_free(OPENSSL_STRING str)628 static void str_free(OPENSSL_STRING str)
629 {
630 OPENSSL_free(str);
631 }
632
append_ia5(STACK_OF (OPENSSL_STRING)** sk,ASN1_IA5STRING * email)633 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
634 {
635 char *emtmp;
636 /* First some sanity checks */
637 if (email->type != V_ASN1_IA5STRING)
638 return 1;
639 if (!email->data || !email->length)
640 return 1;
641 if (!*sk)
642 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
643 if (!*sk)
644 return 0;
645 /* Don't add duplicates */
646 sk_OPENSSL_STRING_sort(*sk);
647 if (sk_OPENSSL_STRING_find(*sk, NULL, (char *)email->data))
648 return 1;
649 emtmp = BUF_strdup((char *)email->data);
650 if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
651 X509_email_free(*sk);
652 *sk = NULL;
653 return 0;
654 }
655 return 1;
656 }
657
X509_email_free(STACK_OF (OPENSSL_STRING)* sk)658 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
659 {
660 sk_OPENSSL_STRING_pop_free(sk, str_free);
661 }
662
663 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
664 const unsigned char *subject, size_t subject_len,
665 unsigned int flags);
666
667 /* Skip pattern prefix to match "wildcard" subject */
skip_prefix(const unsigned char ** p,size_t * plen,const unsigned char * subject,size_t subject_len,unsigned int flags)668 static void skip_prefix(const unsigned char **p, size_t *plen,
669 const unsigned char *subject, size_t subject_len,
670 unsigned int flags)
671 {
672 const unsigned char *pattern = *p;
673 size_t pattern_len = *plen;
674
675 /*
676 * If subject starts with a leading '.' followed by more octets, and
677 * pattern is longer, compare just an equal-length suffix with the
678 * full subject (starting at the '.'), provided the prefix contains
679 * no NULs.
680 */
681 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
682 return;
683
684 while (pattern_len > subject_len && *pattern) {
685 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
686 *pattern == '.')
687 break;
688 ++pattern;
689 --pattern_len;
690 }
691
692 /* Skip if entire prefix acceptable */
693 if (pattern_len == subject_len) {
694 *p = pattern;
695 *plen = pattern_len;
696 }
697 }
698
699 /* Compare while ASCII ignoring case. */
equal_nocase(const unsigned char * pattern,size_t pattern_len,const unsigned char * subject,size_t subject_len,unsigned int flags)700 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
701 const unsigned char *subject, size_t subject_len,
702 unsigned int flags)
703 {
704 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
705 if (pattern_len != subject_len)
706 return 0;
707 while (pattern_len) {
708 unsigned char l = *pattern;
709 unsigned char r = *subject;
710 /* The pattern must not contain NUL characters. */
711 if (l == 0)
712 return 0;
713 if (l != r) {
714 if ('A' <= l && l <= 'Z')
715 l = (l - 'A') + 'a';
716 if ('A' <= r && r <= 'Z')
717 r = (r - 'A') + 'a';
718 if (l != r)
719 return 0;
720 }
721 ++pattern;
722 ++subject;
723 --pattern_len;
724 }
725 return 1;
726 }
727
728 /* Compare using OPENSSL_memcmp. */
equal_case(const unsigned char * pattern,size_t pattern_len,const unsigned char * subject,size_t subject_len,unsigned int flags)729 static int equal_case(const unsigned char *pattern, size_t pattern_len,
730 const unsigned char *subject, size_t subject_len,
731 unsigned int flags)
732 {
733 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
734 if (pattern_len != subject_len)
735 return 0;
736 return !OPENSSL_memcmp(pattern, subject, pattern_len);
737 }
738
739 /*
740 * RFC 5280, section 7.5, requires that only the domain is compared in a
741 * case-insensitive manner.
742 */
equal_email(const unsigned char * a,size_t a_len,const unsigned char * b,size_t b_len,unsigned int unused_flags)743 static int equal_email(const unsigned char *a, size_t a_len,
744 const unsigned char *b, size_t b_len,
745 unsigned int unused_flags)
746 {
747 size_t i = a_len;
748 if (a_len != b_len)
749 return 0;
750 /*
751 * We search backwards for the '@' character, so that we do not have to
752 * deal with quoted local-parts. The domain part is compared in a
753 * case-insensitive manner.
754 */
755 while (i > 0) {
756 --i;
757 if (a[i] == '@' || b[i] == '@') {
758 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
759 return 0;
760 break;
761 }
762 }
763 if (i == 0)
764 i = a_len;
765 return equal_case(a, i, b, i, 0);
766 }
767
768 /*
769 * Compare the prefix and suffix with the subject, and check that the
770 * characters in-between are valid.
771 */
wildcard_match(const unsigned char * prefix,size_t prefix_len,const unsigned char * suffix,size_t suffix_len,const unsigned char * subject,size_t subject_len,unsigned int flags)772 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
773 const unsigned char *suffix, size_t suffix_len,
774 const unsigned char *subject, size_t subject_len,
775 unsigned int flags)
776 {
777 const unsigned char *wildcard_start;
778 const unsigned char *wildcard_end;
779 const unsigned char *p;
780 int allow_multi = 0;
781 int allow_idna = 0;
782
783 if (subject_len < prefix_len + suffix_len)
784 return 0;
785 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
786 return 0;
787 wildcard_start = subject + prefix_len;
788 wildcard_end = subject + (subject_len - suffix_len);
789 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
790 return 0;
791 /*
792 * If the wildcard makes up the entire first label, it must match at
793 * least one character.
794 */
795 if (prefix_len == 0 && *suffix == '.') {
796 if (wildcard_start == wildcard_end)
797 return 0;
798 allow_idna = 1;
799 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
800 allow_multi = 1;
801 }
802 /* IDNA labels cannot match partial wildcards */
803 if (!allow_idna &&
804 subject_len >= 4
805 && OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0)
806 return 0;
807 /* The wildcard may match a literal '*' */
808 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
809 return 1;
810 /*
811 * Check that the part matched by the wildcard contains only
812 * permitted characters and only matches a single label unless
813 * allow_multi is set.
814 */
815 for (p = wildcard_start; p != wildcard_end; ++p)
816 if (!(('0' <= *p && *p <= '9') ||
817 ('A' <= *p && *p <= 'Z') ||
818 ('a' <= *p && *p <= 'z') ||
819 *p == '-' || (allow_multi && *p == '.')))
820 return 0;
821 return 1;
822 }
823
824 #define LABEL_START (1 << 0)
825 #define LABEL_END (1 << 1)
826 #define LABEL_HYPHEN (1 << 2)
827 #define LABEL_IDNA (1 << 3)
828
valid_star(const unsigned char * p,size_t len,unsigned int flags)829 static const unsigned char *valid_star(const unsigned char *p, size_t len,
830 unsigned int flags)
831 {
832 const unsigned char *star = 0;
833 size_t i;
834 int state = LABEL_START;
835 int dots = 0;
836 for (i = 0; i < len; ++i) {
837 /*
838 * Locate first and only legal wildcard, either at the start
839 * or end of a non-IDNA first and not final label.
840 */
841 if (p[i] == '*') {
842 int atstart = (state & LABEL_START);
843 int atend = (i == len - 1 || p[i + 1] == '.');
844 /*
845 * At most one wildcard per pattern.
846 * No wildcards in IDNA labels.
847 * No wildcards after the first label.
848 */
849 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
850 return NULL;
851 /* Only full-label '*.example.com' wildcards? */
852 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
853 && (!atstart || !atend))
854 return NULL;
855 /* No 'foo*bar' wildcards */
856 if (!atstart && !atend)
857 return NULL;
858 star = &p[i];
859 state &= ~LABEL_START;
860 } else if (('a' <= p[i] && p[i] <= 'z')
861 || ('A' <= p[i] && p[i] <= 'Z')
862 || ('0' <= p[i] && p[i] <= '9')) {
863 if ((state & LABEL_START) != 0
864 && len - i >= 4
865 && OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0)
866 state |= LABEL_IDNA;
867 state &= ~(LABEL_HYPHEN | LABEL_START);
868 } else if (p[i] == '.') {
869 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
870 return NULL;
871 state = LABEL_START;
872 ++dots;
873 } else if (p[i] == '-') {
874 /* no domain/subdomain starts with '-' */
875 if ((state & LABEL_START) != 0)
876 return NULL;
877 state |= LABEL_HYPHEN;
878 } else
879 return NULL;
880 }
881
882 /*
883 * The final label must not end in a hyphen or ".", and
884 * there must be at least two dots after the star.
885 */
886 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
887 return NULL;
888 return star;
889 }
890
891 /* Compare using wildcards. */
equal_wildcard(const unsigned char * pattern,size_t pattern_len,const unsigned char * subject,size_t subject_len,unsigned int flags)892 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
893 const unsigned char *subject, size_t subject_len,
894 unsigned int flags)
895 {
896 const unsigned char *star = NULL;
897
898 /*
899 * Subject names starting with '.' can only match a wildcard pattern
900 * via a subject sub-domain pattern suffix match.
901 */
902 if (!(subject_len > 1 && subject[0] == '.'))
903 star = valid_star(pattern, pattern_len, flags);
904 if (star == NULL)
905 return equal_nocase(pattern, pattern_len,
906 subject, subject_len, flags);
907 return wildcard_match(pattern, star - pattern,
908 star + 1, (pattern + pattern_len) - star - 1,
909 subject, subject_len, flags);
910 }
911
912 /*
913 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
914 * cmp_type > 0 only compare if string matches the type, otherwise convert it
915 * to UTF8.
916 */
917
do_check_string(ASN1_STRING * a,int cmp_type,equal_fn equal,unsigned int flags,const char * b,size_t blen,char ** peername)918 static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
919 unsigned int flags, const char *b, size_t blen,
920 char **peername)
921 {
922 int rv = 0;
923
924 if (!a->data || !a->length)
925 return 0;
926 if (cmp_type > 0) {
927 if (cmp_type != a->type)
928 return 0;
929 if (cmp_type == V_ASN1_IA5STRING)
930 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
931 else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen))
932 rv = 1;
933 if (rv > 0 && peername)
934 *peername = BUF_strndup((char *)a->data, a->length);
935 } else {
936 int astrlen;
937 unsigned char *astr;
938 astrlen = ASN1_STRING_to_UTF8(&astr, a);
939 if (astrlen < 0)
940 return -1;
941 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
942 if (rv > 0 && peername)
943 *peername = BUF_strndup((char *)astr, astrlen);
944 OPENSSL_free(astr);
945 }
946 return rv;
947 }
948
do_x509_check(X509 * x,const char * chk,size_t chklen,unsigned int flags,int check_type,char ** peername)949 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
950 unsigned int flags, int check_type, char **peername)
951 {
952 GENERAL_NAMES *gens = NULL;
953 X509_NAME *name = NULL;
954 size_t i;
955 int j;
956 int cnid = NID_undef;
957 int alt_type;
958 int san_present = 0;
959 int rv = 0;
960 equal_fn equal;
961
962 /* See below, this flag is internal-only */
963 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
964 if (check_type == GEN_EMAIL) {
965 cnid = NID_pkcs9_emailAddress;
966 alt_type = V_ASN1_IA5STRING;
967 equal = equal_email;
968 } else if (check_type == GEN_DNS) {
969 cnid = NID_commonName;
970 /* Implicit client-side DNS sub-domain pattern */
971 if (chklen > 1 && chk[0] == '.')
972 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
973 alt_type = V_ASN1_IA5STRING;
974 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
975 equal = equal_nocase;
976 else
977 equal = equal_wildcard;
978 } else {
979 alt_type = V_ASN1_OCTET_STRING;
980 equal = equal_case;
981 }
982
983 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
984 if (gens) {
985 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
986 GENERAL_NAME *gen;
987 ASN1_STRING *cstr;
988 gen = sk_GENERAL_NAME_value(gens, i);
989 if (gen->type != check_type)
990 continue;
991 san_present = 1;
992 if (check_type == GEN_EMAIL)
993 cstr = gen->d.rfc822Name;
994 else if (check_type == GEN_DNS)
995 cstr = gen->d.dNSName;
996 else
997 cstr = gen->d.iPAddress;
998 /* Positive on success, negative on error! */
999 if ((rv = do_check_string(cstr, alt_type, equal, flags,
1000 chk, chklen, peername)) != 0)
1001 break;
1002 }
1003 GENERAL_NAMES_free(gens);
1004 if (rv != 0)
1005 return rv;
1006 if (cnid == NID_undef
1007 || (san_present
1008 && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
1009 return 0;
1010 }
1011
1012 /* We're done if CN-ID is not pertinent */
1013 if (cnid == NID_undef)
1014 return 0;
1015
1016 j = -1;
1017 name = X509_get_subject_name(x);
1018 while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) {
1019 X509_NAME_ENTRY *ne;
1020 ASN1_STRING *str;
1021 ne = X509_NAME_get_entry(name, j);
1022 str = X509_NAME_ENTRY_get_data(ne);
1023 /* Positive on success, negative on error! */
1024 if ((rv = do_check_string(str, -1, equal, flags,
1025 chk, chklen, peername)) != 0)
1026 return rv;
1027 }
1028 return 0;
1029 }
1030
X509_check_host(X509 * x,const char * chk,size_t chklen,unsigned int flags,char ** peername)1031 int X509_check_host(X509 *x, const char *chk, size_t chklen,
1032 unsigned int flags, char **peername)
1033 {
1034 if (chk == NULL)
1035 return -2;
1036 if (OPENSSL_memchr(chk, '\0', chklen))
1037 return -2;
1038 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1039 }
1040
X509_check_email(X509 * x,const char * chk,size_t chklen,unsigned int flags)1041 int X509_check_email(X509 *x, const char *chk, size_t chklen,
1042 unsigned int flags)
1043 {
1044 if (chk == NULL)
1045 return -2;
1046 if (OPENSSL_memchr(chk, '\0', chklen))
1047 return -2;
1048 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1049 }
1050
X509_check_ip(X509 * x,const unsigned char * chk,size_t chklen,unsigned int flags)1051 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
1052 unsigned int flags)
1053 {
1054 if (chk == NULL)
1055 return -2;
1056 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1057 }
1058
X509_check_ip_asc(X509 * x,const char * ipasc,unsigned int flags)1059 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1060 {
1061 unsigned char ipout[16];
1062 size_t iplen;
1063
1064 if (ipasc == NULL)
1065 return -2;
1066 iplen = (size_t)a2i_ipadd(ipout, ipasc);
1067 if (iplen == 0)
1068 return -2;
1069 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1070 }
1071
1072 /*
1073 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1074 * with RFC3280.
1075 */
1076
a2i_IPADDRESS(const char * ipasc)1077 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1078 {
1079 unsigned char ipout[16];
1080 ASN1_OCTET_STRING *ret;
1081 int iplen;
1082
1083 /* If string contains a ':' assume IPv6 */
1084
1085 iplen = a2i_ipadd(ipout, ipasc);
1086
1087 if (!iplen)
1088 return NULL;
1089
1090 ret = ASN1_OCTET_STRING_new();
1091 if (!ret)
1092 return NULL;
1093 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1094 ASN1_OCTET_STRING_free(ret);
1095 return NULL;
1096 }
1097 return ret;
1098 }
1099
a2i_IPADDRESS_NC(const char * ipasc)1100 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1101 {
1102 ASN1_OCTET_STRING *ret = NULL;
1103 unsigned char ipout[32];
1104 char *iptmp = NULL, *p;
1105 int iplen1, iplen2;
1106 p = strchr(ipasc, '/');
1107 if (!p)
1108 return NULL;
1109 iptmp = BUF_strdup(ipasc);
1110 if (!iptmp)
1111 return NULL;
1112 p = iptmp + (p - ipasc);
1113 *p++ = 0;
1114
1115 iplen1 = a2i_ipadd(ipout, iptmp);
1116
1117 if (!iplen1)
1118 goto err;
1119
1120 iplen2 = a2i_ipadd(ipout + iplen1, p);
1121
1122 OPENSSL_free(iptmp);
1123 iptmp = NULL;
1124
1125 if (!iplen2 || (iplen1 != iplen2))
1126 goto err;
1127
1128 ret = ASN1_OCTET_STRING_new();
1129 if (!ret)
1130 goto err;
1131 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1132 goto err;
1133
1134 return ret;
1135
1136 err:
1137 if (iptmp)
1138 OPENSSL_free(iptmp);
1139 if (ret)
1140 ASN1_OCTET_STRING_free(ret);
1141 return NULL;
1142 }
1143
a2i_ipadd(unsigned char * ipout,const char * ipasc)1144 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1145 {
1146 /* If string contains a ':' assume IPv6 */
1147
1148 if (strchr(ipasc, ':')) {
1149 if (!ipv6_from_asc(ipout, ipasc))
1150 return 0;
1151 return 16;
1152 } else {
1153 if (!ipv4_from_asc(ipout, ipasc))
1154 return 0;
1155 return 4;
1156 }
1157 }
1158
ipv4_from_asc(unsigned char * v4,const char * in)1159 static int ipv4_from_asc(unsigned char *v4, const char *in)
1160 {
1161 int a0, a1, a2, a3;
1162 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1163 return 0;
1164 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1165 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1166 return 0;
1167 v4[0] = a0;
1168 v4[1] = a1;
1169 v4[2] = a2;
1170 v4[3] = a3;
1171 return 1;
1172 }
1173
1174 typedef struct {
1175 /* Temporary store for IPV6 output */
1176 unsigned char tmp[16];
1177 /* Total number of bytes in tmp */
1178 int total;
1179 /* The position of a zero (corresponding to '::') */
1180 int zero_pos;
1181 /* Number of zeroes */
1182 int zero_cnt;
1183 } IPV6_STAT;
1184
ipv6_from_asc(unsigned char * v6,const char * in)1185 static int ipv6_from_asc(unsigned char *v6, const char *in)
1186 {
1187 IPV6_STAT v6stat;
1188 v6stat.total = 0;
1189 v6stat.zero_pos = -1;
1190 v6stat.zero_cnt = 0;
1191 /*
1192 * Treat the IPv6 representation as a list of values separated by ':'.
1193 * The presence of a '::' will parse as one, two or three zero length
1194 * elements.
1195 */
1196 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1197 return 0;
1198
1199 /* Now for some sanity checks */
1200
1201 if (v6stat.zero_pos == -1) {
1202 /* If no '::' must have exactly 16 bytes */
1203 if (v6stat.total != 16)
1204 return 0;
1205 } else {
1206 /* If '::' must have less than 16 bytes */
1207 if (v6stat.total == 16)
1208 return 0;
1209 /* More than three zeroes is an error */
1210 if (v6stat.zero_cnt > 3)
1211 return 0;
1212 /* Can only have three zeroes if nothing else present */
1213 else if (v6stat.zero_cnt == 3) {
1214 if (v6stat.total > 0)
1215 return 0;
1216 }
1217 /* Can only have two zeroes if at start or end */
1218 else if (v6stat.zero_cnt == 2) {
1219 if ((v6stat.zero_pos != 0)
1220 && (v6stat.zero_pos != v6stat.total))
1221 return 0;
1222 } else
1223 /* Can only have one zero if *not* start or end */
1224 {
1225 if ((v6stat.zero_pos == 0)
1226 || (v6stat.zero_pos == v6stat.total))
1227 return 0;
1228 }
1229 }
1230
1231 /* Format result */
1232
1233 if (v6stat.zero_pos >= 0) {
1234 /* Copy initial part */
1235 OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1236 /* Zero middle */
1237 OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1238 /* Copy final part */
1239 if (v6stat.total != v6stat.zero_pos)
1240 OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1241 v6stat.tmp + v6stat.zero_pos,
1242 v6stat.total - v6stat.zero_pos);
1243 } else
1244 OPENSSL_memcpy(v6, v6stat.tmp, 16);
1245
1246 return 1;
1247 }
1248
ipv6_cb(const char * elem,int len,void * usr)1249 static int ipv6_cb(const char *elem, int len, void *usr)
1250 {
1251 IPV6_STAT *s = usr;
1252 /* Error if 16 bytes written */
1253 if (s->total == 16)
1254 return 0;
1255 if (len == 0) {
1256 /* Zero length element, corresponds to '::' */
1257 if (s->zero_pos == -1)
1258 s->zero_pos = s->total;
1259 /* If we've already got a :: its an error */
1260 else if (s->zero_pos != s->total)
1261 return 0;
1262 s->zero_cnt++;
1263 } else {
1264 /* If more than 4 characters could be final a.b.c.d form */
1265 if (len > 4) {
1266 /* Need at least 4 bytes left */
1267 if (s->total > 12)
1268 return 0;
1269 /* Must be end of string */
1270 if (elem[len])
1271 return 0;
1272 if (!ipv4_from_asc(s->tmp + s->total, elem))
1273 return 0;
1274 s->total += 4;
1275 } else {
1276 if (!ipv6_hex(s->tmp + s->total, elem, len))
1277 return 0;
1278 s->total += 2;
1279 }
1280 }
1281 return 1;
1282 }
1283
1284 /*
1285 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1286 */
1287
ipv6_hex(unsigned char * out,const char * in,int inlen)1288 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1289 {
1290 unsigned char c;
1291 unsigned int num = 0;
1292 if (inlen > 4)
1293 return 0;
1294 while (inlen--) {
1295 c = *in++;
1296 num <<= 4;
1297 if ((c >= '0') && (c <= '9'))
1298 num |= c - '0';
1299 else if ((c >= 'A') && (c <= 'F'))
1300 num |= c - 'A' + 10;
1301 else if ((c >= 'a') && (c <= 'f'))
1302 num |= c - 'a' + 10;
1303 else
1304 return 0;
1305 }
1306 out[0] = num >> 8;
1307 out[1] = num & 0xff;
1308 return 1;
1309 }
1310
X509V3_NAME_from_section(X509_NAME * nm,STACK_OF (CONF_VALUE)* dn_sk,unsigned long chtype)1311 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk,
1312 unsigned long chtype)
1313 {
1314 CONF_VALUE *v;
1315 int mval;
1316 size_t i;
1317 char *p, *type;
1318 if (!nm)
1319 return 0;
1320
1321 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1322 v = sk_CONF_VALUE_value(dn_sk, i);
1323 type = v->name;
1324 /*
1325 * Skip past any leading X. X: X, etc to allow for multiple instances
1326 */
1327 for (p = type; *p; p++)
1328 if ((*p == ':') || (*p == ',') || (*p == '.')) {
1329 p++;
1330 if (*p)
1331 type = p;
1332 break;
1333 }
1334 if (*type == '+') {
1335 mval = -1;
1336 type++;
1337 } else
1338 mval = 0;
1339 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1340 (unsigned char *)v->value, -1, -1,
1341 mval))
1342 return 0;
1343
1344 }
1345 return 1;
1346 }
1347