OpenSSL: update to 1.0.2c
[tomato.git] / release / src / router / openssl / crypto / x509v3 / v3_utl.c
blobbdd7b95f4570b43cb6d65fe9b33d7084d3eeec6c
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
59 /* X509 v3 extension utilities */
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>
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);
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);
80 /* Add a CONF_VALUE name value pair to stack */
82 int X509V3_add_value(const char *name, const char *value,
83 STACK_OF(CONF_VALUE) **extlist)
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;
112 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
113 STACK_OF(CONF_VALUE) **extlist)
115 return X509V3_add_value(name, (const char *)value, extlist);
118 /* Free function for STACK_OF(CONF_VALUE) */
120 void X509V3_conf_free(CONF_VALUE *conf)
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);
133 int X509V3_add_value_bool(const char *name, int asn1_bool,
134 STACK_OF(CONF_VALUE) **extlist)
136 if (asn1_bool)
137 return X509V3_add_value(name, "TRUE", extlist);
138 return X509V3_add_value(name, "FALSE", extlist);
141 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
142 STACK_OF(CONF_VALUE) **extlist)
144 if (asn1_bool)
145 return X509V3_add_value(name, "TRUE", extlist);
146 return 1;
149 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
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;
162 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
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;
175 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
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;
185 bn = BN_new();
186 if (value[0] == '-') {
187 value++;
188 isneg = 1;
189 } else
190 isneg = 0;
192 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
193 value += 2;
194 ishex = 1;
195 } else
196 ishex = 0;
198 if (ishex)
199 ret = BN_hex2bn(&bn, value);
200 else
201 ret = BN_dec2bn(&bn, value);
203 if (!ret || value[ret]) {
204 BN_free(bn);
205 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
206 return 0;
209 if (isneg && BN_is_zero(bn))
210 isneg = 0;
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;
219 if (isneg)
220 aint->type |= V_ASN1_NEG;
221 return aint;
224 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
225 STACK_OF(CONF_VALUE) **extlist)
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;
238 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
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;
254 err:
255 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
256 X509V3_R_INVALID_BOOLEAN_STRING);
257 X509V3_conf_err(value);
258 return 0;
261 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
263 ASN1_INTEGER *itmp;
264 if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
265 X509V3_conf_err(value);
266 return 0;
268 *aint = itmp;
269 return 1;
272 #define HDR_NAME 1
273 #define HDR_VALUE 2
276 * #define DEBUG
279 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
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;
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++) {
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;
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;
322 X509V3_add_value(ntmp, NULL, &values);
324 break;
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;
339 X509V3_add_value(ntmp, vtmp, &values);
340 ntmp = NULL;
341 q = p + 1;
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;
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;
367 X509V3_add_value(ntmp, NULL, &values);
369 OPENSSL_free(linebuf);
370 return values;
372 err:
373 OPENSSL_free(linebuf);
374 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
375 return NULL;
379 /* Delete leading and trailing spaces from a string */
380 static char *strip_spaces(char *name)
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;
399 /* hex string utilities */
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)
407 char *hex_to_string(const unsigned char *buffer, long len)
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;
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++ = ':';
425 q[-1] = 0;
426 #ifdef CHARSET_EBCDIC
427 ebcdic2ascii(tmp, tmp, q - tmp - 1);
428 #endif
430 return tmp;
434 * Give a string of hex digits convert to a buffer
437 unsigned char *string_to_hex(const char *str, long *len)
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;
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;
463 if (isupper(ch))
464 ch = tolower(ch);
465 if (isupper(cl))
466 cl = tolower(cl);
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;
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;
482 *q++ = (ch << 4) | cl;
485 if (len)
486 *len = q - hexbuf;
488 return hexbuf;
490 err:
491 if (hexbuf)
492 OPENSSL_free(hexbuf);
493 X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE);
494 return NULL;
496 badhex:
497 OPENSSL_free(hexbuf);
498 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT);
499 return NULL;
504 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
507 int name_cmp(const char *name, const char *cmp)
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;
520 static int sk_strcmp(const char *const *a, const char *const *b)
522 return strcmp(*a, *b);
525 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
527 GENERAL_NAMES *gens;
528 STACK_OF(OPENSSL_STRING) *ret;
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;
536 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
538 AUTHORITY_INFO_ACCESS *info;
539 STACK_OF(OPENSSL_STRING) *ret = NULL;
540 int i;
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;
555 AUTHORITY_INFO_ACCESS_free(info);
556 return ret;
559 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
561 GENERAL_NAMES *gens;
562 STACK_OF(X509_EXTENSION) *exts;
563 STACK_OF(OPENSSL_STRING) *ret;
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;
573 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
574 GENERAL_NAMES *gens)
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;
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;
598 return ret;
601 static void str_free(OPENSSL_STRING str)
603 OPENSSL_free(str);
606 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
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;
627 return 1;
630 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
632 sk_OPENSSL_STRING_pop_free(sk, str_free);
635 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
636 const unsigned char *subject, size_t subject_len,
637 unsigned int flags);
639 /* Skip pattern prefix to match "wildcard" subject */
640 static void skip_prefix(const unsigned char **p, size_t *plen,
641 const unsigned char *subject, size_t subject_len,
642 unsigned int flags)
644 const unsigned char *pattern = *p;
645 size_t pattern_len = *plen;
648 * If subject starts with a leading '.' followed by more octets, and
649 * pattern is longer, compare just an equal-length suffix with the
650 * full subject (starting at the '.'), provided the prefix contains
651 * no NULs.
653 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
654 return;
656 while (pattern_len > subject_len && *pattern) {
657 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
658 *pattern == '.')
659 break;
660 ++pattern;
661 --pattern_len;
664 /* Skip if entire prefix acceptable */
665 if (pattern_len == subject_len) {
666 *p = pattern;
667 *plen = pattern_len;
671 /* Compare while ASCII ignoring case. */
672 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
673 const unsigned char *subject, size_t subject_len,
674 unsigned int flags)
676 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
677 if (pattern_len != subject_len)
678 return 0;
679 while (pattern_len) {
680 unsigned char l = *pattern;
681 unsigned char r = *subject;
682 /* The pattern must not contain NUL characters. */
683 if (l == 0)
684 return 0;
685 if (l != r) {
686 if ('A' <= l && l <= 'Z')
687 l = (l - 'A') + 'a';
688 if ('A' <= r && r <= 'Z')
689 r = (r - 'A') + 'a';
690 if (l != r)
691 return 0;
693 ++pattern;
694 ++subject;
695 --pattern_len;
697 return 1;
700 /* Compare using memcmp. */
701 static int equal_case(const unsigned char *pattern, size_t pattern_len,
702 const unsigned char *subject, size_t subject_len,
703 unsigned int flags)
705 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
706 if (pattern_len != subject_len)
707 return 0;
708 return !memcmp(pattern, subject, pattern_len);
712 * RFC 5280, section 7.5, requires that only the domain is compared in a
713 * case-insensitive manner.
715 static int equal_email(const unsigned char *a, size_t a_len,
716 const unsigned char *b, size_t b_len,
717 unsigned int unused_flags)
719 size_t i = a_len;
720 if (a_len != b_len)
721 return 0;
723 * We search backwards for the '@' character, so that we do not have to
724 * deal with quoted local-parts. The domain part is compared in a
725 * case-insensitive manner.
727 while (i > 0) {
728 --i;
729 if (a[i] == '@' || b[i] == '@') {
730 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
731 return 0;
732 break;
735 if (i == 0)
736 i = a_len;
737 return equal_case(a, i, b, i, 0);
741 * Compare the prefix and suffix with the subject, and check that the
742 * characters in-between are valid.
744 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
745 const unsigned char *suffix, size_t suffix_len,
746 const unsigned char *subject, size_t subject_len,
747 unsigned int flags)
749 const unsigned char *wildcard_start;
750 const unsigned char *wildcard_end;
751 const unsigned char *p;
752 int allow_multi = 0;
753 int allow_idna = 0;
755 if (subject_len < prefix_len + suffix_len)
756 return 0;
757 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
758 return 0;
759 wildcard_start = subject + prefix_len;
760 wildcard_end = subject + (subject_len - suffix_len);
761 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
762 return 0;
764 * If the wildcard makes up the entire first label, it must match at
765 * least one character.
767 if (prefix_len == 0 && *suffix == '.') {
768 if (wildcard_start == wildcard_end)
769 return 0;
770 allow_idna = 1;
771 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
772 allow_multi = 1;
774 /* IDNA labels cannot match partial wildcards */
775 if (!allow_idna &&
776 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
777 return 0;
778 /* The wildcard may match a literal '*' */
779 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
780 return 1;
782 * Check that the part matched by the wildcard contains only
783 * permitted characters and only matches a single label unless
784 * allow_multi is set.
786 for (p = wildcard_start; p != wildcard_end; ++p)
787 if (!(('0' <= *p && *p <= '9') ||
788 ('A' <= *p && *p <= 'Z') ||
789 ('a' <= *p && *p <= 'z') ||
790 *p == '-' || (allow_multi && *p == '.')))
791 return 0;
792 return 1;
795 #define LABEL_START (1 << 0)
796 #define LABEL_END (1 << 1)
797 #define LABEL_HYPHEN (1 << 2)
798 #define LABEL_IDNA (1 << 3)
800 static const unsigned char *valid_star(const unsigned char *p, size_t len,
801 unsigned int flags)
803 const unsigned char *star = 0;
804 size_t i;
805 int state = LABEL_START;
806 int dots = 0;
807 for (i = 0; i < len; ++i) {
809 * Locate first and only legal wildcard, either at the start
810 * or end of a non-IDNA first and not final label.
812 if (p[i] == '*') {
813 int atstart = (state & LABEL_START);
814 int atend = (i == len - 1 || p[i + 1] == '.');
816 * At most one wildcard per pattern.
817 * No wildcards in IDNA labels.
818 * No wildcards after the first label.
820 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
821 return NULL;
822 /* Only full-label '*.example.com' wildcards? */
823 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
824 && (!atstart || !atend))
825 return NULL;
826 /* No 'foo*bar' wildcards */
827 if (!atstart && !atend)
828 return NULL;
829 star = &p[i];
830 state &= ~LABEL_START;
831 } else if (('a' <= p[i] && p[i] <= 'z')
832 || ('A' <= p[i] && p[i] <= 'Z')
833 || ('0' <= p[i] && p[i] <= '9')) {
834 if ((state & LABEL_START) != 0
835 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
836 state |= LABEL_IDNA;
837 state &= ~(LABEL_HYPHEN | LABEL_START);
838 } else if (p[i] == '.') {
839 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
840 return NULL;
841 state = LABEL_START;
842 ++dots;
843 } else if (p[i] == '-') {
844 if ((state & LABEL_HYPHEN) != 0)
845 return NULL;
846 state |= LABEL_HYPHEN;
847 } else
848 return NULL;
852 * The final label must not end in a hyphen or ".", and
853 * there must be at least two dots after the star.
855 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
856 return NULL;
857 return star;
860 /* Compare using wildcards. */
861 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
862 const unsigned char *subject, size_t subject_len,
863 unsigned int flags)
865 const unsigned char *star = NULL;
868 * Subject names starting with '.' can only match a wildcard pattern
869 * via a subject sub-domain pattern suffix match.
871 if (!(subject_len > 1 && subject[0] == '.'))
872 star = valid_star(pattern, pattern_len, flags);
873 if (star == NULL)
874 return equal_nocase(pattern, pattern_len,
875 subject, subject_len, flags);
876 return wildcard_match(pattern, star - pattern,
877 star + 1, (pattern + pattern_len) - star - 1,
878 subject, subject_len, flags);
882 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
883 * cmp_type > 0 only compare if string matches the type, otherwise convert it
884 * to UTF8.
887 static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
888 unsigned int flags, const char *b, size_t blen,
889 char **peername)
891 int rv = 0;
893 if (!a->data || !a->length)
894 return 0;
895 if (cmp_type > 0) {
896 if (cmp_type != a->type)
897 return 0;
898 if (cmp_type == V_ASN1_IA5STRING)
899 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
900 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
901 rv = 1;
902 if (rv > 0 && peername)
903 *peername = BUF_strndup((char *)a->data, a->length);
904 } else {
905 int astrlen;
906 unsigned char *astr;
907 astrlen = ASN1_STRING_to_UTF8(&astr, a);
908 if (astrlen < 0) {
910 * -1 could be an internal malloc failure or a decoding error from
911 * malformed input; we can't distinguish.
913 return -1;
915 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
916 if (rv > 0 && peername)
917 *peername = BUF_strndup((char *)astr, astrlen);
918 OPENSSL_free(astr);
920 return rv;
923 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
924 unsigned int flags, int check_type, char **peername)
926 GENERAL_NAMES *gens = NULL;
927 X509_NAME *name = NULL;
928 int i;
929 int cnid;
930 int alt_type;
931 int san_present = 0;
932 int rv = 0;
933 equal_fn equal;
935 /* See below, this flag is internal-only */
936 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
937 if (check_type == GEN_EMAIL) {
938 cnid = NID_pkcs9_emailAddress;
939 alt_type = V_ASN1_IA5STRING;
940 equal = equal_email;
941 } else if (check_type == GEN_DNS) {
942 cnid = NID_commonName;
943 /* Implicit client-side DNS sub-domain pattern */
944 if (chklen > 1 && chk[0] == '.')
945 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
946 alt_type = V_ASN1_IA5STRING;
947 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
948 equal = equal_nocase;
949 else
950 equal = equal_wildcard;
951 } else {
952 cnid = 0;
953 alt_type = V_ASN1_OCTET_STRING;
954 equal = equal_case;
957 if (chklen == 0)
958 chklen = strlen(chk);
960 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
961 if (gens) {
962 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
963 GENERAL_NAME *gen;
964 ASN1_STRING *cstr;
965 gen = sk_GENERAL_NAME_value(gens, i);
966 if (gen->type != check_type)
967 continue;
968 san_present = 1;
969 if (check_type == GEN_EMAIL)
970 cstr = gen->d.rfc822Name;
971 else if (check_type == GEN_DNS)
972 cstr = gen->d.dNSName;
973 else
974 cstr = gen->d.iPAddress;
975 /* Positive on success, negative on error! */
976 if ((rv = do_check_string(cstr, alt_type, equal, flags,
977 chk, chklen, peername)) != 0)
978 break;
980 GENERAL_NAMES_free(gens);
981 if (rv != 0)
982 return rv;
983 if (!cnid
984 || (san_present
985 && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
986 return 0;
988 i = -1;
989 name = X509_get_subject_name(x);
990 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
991 X509_NAME_ENTRY *ne;
992 ASN1_STRING *str;
993 ne = X509_NAME_get_entry(name, i);
994 str = X509_NAME_ENTRY_get_data(ne);
995 /* Positive on success, negative on error! */
996 if ((rv = do_check_string(str, -1, equal, flags,
997 chk, chklen, peername)) != 0)
998 return rv;
1000 return 0;
1003 int X509_check_host(X509 *x, const char *chk, size_t chklen,
1004 unsigned int flags, char **peername)
1006 if (chk == NULL)
1007 return -2;
1009 * Embedded NULs are disallowed, except as the last character of a
1010 * string of length 2 or more (tolerate caller including terminating
1011 * NUL in string length).
1013 if (chklen == 0)
1014 chklen = strlen(chk);
1015 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1016 return -2;
1017 if (chklen > 1 && chk[chklen - 1] == '\0')
1018 --chklen;
1019 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1022 int X509_check_email(X509 *x, const char *chk, size_t chklen,
1023 unsigned int flags)
1025 if (chk == NULL)
1026 return -2;
1028 * Embedded NULs are disallowed, except as the last character of a
1029 * string of length 2 or more (tolerate caller including terminating
1030 * NUL in string length).
1032 if (chklen == 0)
1033 chklen = strlen((char *)chk);
1034 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1035 return -2;
1036 if (chklen > 1 && chk[chklen - 1] == '\0')
1037 --chklen;
1038 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1041 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
1042 unsigned int flags)
1044 if (chk == NULL)
1045 return -2;
1046 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1049 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1051 unsigned char ipout[16];
1052 size_t iplen;
1054 if (ipasc == NULL)
1055 return -2;
1056 iplen = (size_t)a2i_ipadd(ipout, ipasc);
1057 if (iplen == 0)
1058 return -2;
1059 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1063 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1064 * with RFC3280.
1067 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1069 unsigned char ipout[16];
1070 ASN1_OCTET_STRING *ret;
1071 int iplen;
1073 /* If string contains a ':' assume IPv6 */
1075 iplen = a2i_ipadd(ipout, ipasc);
1077 if (!iplen)
1078 return NULL;
1080 ret = ASN1_OCTET_STRING_new();
1081 if (!ret)
1082 return NULL;
1083 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1084 ASN1_OCTET_STRING_free(ret);
1085 return NULL;
1087 return ret;
1090 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1092 ASN1_OCTET_STRING *ret = NULL;
1093 unsigned char ipout[32];
1094 char *iptmp = NULL, *p;
1095 int iplen1, iplen2;
1096 p = strchr(ipasc, '/');
1097 if (!p)
1098 return NULL;
1099 iptmp = BUF_strdup(ipasc);
1100 if (!iptmp)
1101 return NULL;
1102 p = iptmp + (p - ipasc);
1103 *p++ = 0;
1105 iplen1 = a2i_ipadd(ipout, iptmp);
1107 if (!iplen1)
1108 goto err;
1110 iplen2 = a2i_ipadd(ipout + iplen1, p);
1112 OPENSSL_free(iptmp);
1113 iptmp = NULL;
1115 if (!iplen2 || (iplen1 != iplen2))
1116 goto err;
1118 ret = ASN1_OCTET_STRING_new();
1119 if (!ret)
1120 goto err;
1121 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1122 goto err;
1124 return ret;
1126 err:
1127 if (iptmp)
1128 OPENSSL_free(iptmp);
1129 if (ret)
1130 ASN1_OCTET_STRING_free(ret);
1131 return NULL;
1134 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1136 /* If string contains a ':' assume IPv6 */
1138 if (strchr(ipasc, ':')) {
1139 if (!ipv6_from_asc(ipout, ipasc))
1140 return 0;
1141 return 16;
1142 } else {
1143 if (!ipv4_from_asc(ipout, ipasc))
1144 return 0;
1145 return 4;
1149 static int ipv4_from_asc(unsigned char *v4, const char *in)
1151 int a0, a1, a2, a3;
1152 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1153 return 0;
1154 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1155 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1156 return 0;
1157 v4[0] = a0;
1158 v4[1] = a1;
1159 v4[2] = a2;
1160 v4[3] = a3;
1161 return 1;
1164 typedef struct {
1165 /* Temporary store for IPV6 output */
1166 unsigned char tmp[16];
1167 /* Total number of bytes in tmp */
1168 int total;
1169 /* The position of a zero (corresponding to '::') */
1170 int zero_pos;
1171 /* Number of zeroes */
1172 int zero_cnt;
1173 } IPV6_STAT;
1175 static int ipv6_from_asc(unsigned char *v6, const char *in)
1177 IPV6_STAT v6stat;
1178 v6stat.total = 0;
1179 v6stat.zero_pos = -1;
1180 v6stat.zero_cnt = 0;
1182 * Treat the IPv6 representation as a list of values separated by ':'.
1183 * The presence of a '::' will parse as one, two or three zero length
1184 * elements.
1186 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1187 return 0;
1189 /* Now for some sanity checks */
1191 if (v6stat.zero_pos == -1) {
1192 /* If no '::' must have exactly 16 bytes */
1193 if (v6stat.total != 16)
1194 return 0;
1195 } else {
1196 /* If '::' must have less than 16 bytes */
1197 if (v6stat.total == 16)
1198 return 0;
1199 /* More than three zeroes is an error */
1200 if (v6stat.zero_cnt > 3)
1201 return 0;
1202 /* Can only have three zeroes if nothing else present */
1203 else if (v6stat.zero_cnt == 3) {
1204 if (v6stat.total > 0)
1205 return 0;
1207 /* Can only have two zeroes if at start or end */
1208 else if (v6stat.zero_cnt == 2) {
1209 if ((v6stat.zero_pos != 0)
1210 && (v6stat.zero_pos != v6stat.total))
1211 return 0;
1212 } else
1213 /* Can only have one zero if *not* start or end */
1215 if ((v6stat.zero_pos == 0)
1216 || (v6stat.zero_pos == v6stat.total))
1217 return 0;
1221 /* Format result */
1223 if (v6stat.zero_pos >= 0) {
1224 /* Copy initial part */
1225 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1226 /* Zero middle */
1227 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1228 /* Copy final part */
1229 if (v6stat.total != v6stat.zero_pos)
1230 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1231 v6stat.tmp + v6stat.zero_pos,
1232 v6stat.total - v6stat.zero_pos);
1233 } else
1234 memcpy(v6, v6stat.tmp, 16);
1236 return 1;
1239 static int ipv6_cb(const char *elem, int len, void *usr)
1241 IPV6_STAT *s = usr;
1242 /* Error if 16 bytes written */
1243 if (s->total == 16)
1244 return 0;
1245 if (len == 0) {
1246 /* Zero length element, corresponds to '::' */
1247 if (s->zero_pos == -1)
1248 s->zero_pos = s->total;
1249 /* If we've already got a :: its an error */
1250 else if (s->zero_pos != s->total)
1251 return 0;
1252 s->zero_cnt++;
1253 } else {
1254 /* If more than 4 characters could be final a.b.c.d form */
1255 if (len > 4) {
1256 /* Need at least 4 bytes left */
1257 if (s->total > 12)
1258 return 0;
1259 /* Must be end of string */
1260 if (elem[len])
1261 return 0;
1262 if (!ipv4_from_asc(s->tmp + s->total, elem))
1263 return 0;
1264 s->total += 4;
1265 } else {
1266 if (!ipv6_hex(s->tmp + s->total, elem, len))
1267 return 0;
1268 s->total += 2;
1271 return 1;
1275 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1278 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1280 unsigned char c;
1281 unsigned int num = 0;
1282 if (inlen > 4)
1283 return 0;
1284 while (inlen--) {
1285 c = *in++;
1286 num <<= 4;
1287 if ((c >= '0') && (c <= '9'))
1288 num |= c - '0';
1289 else if ((c >= 'A') && (c <= 'F'))
1290 num |= c - 'A' + 10;
1291 else if ((c >= 'a') && (c <= 'f'))
1292 num |= c - 'a' + 10;
1293 else
1294 return 0;
1296 out[0] = num >> 8;
1297 out[1] = num & 0xff;
1298 return 1;
1301 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1302 unsigned long chtype)
1304 CONF_VALUE *v;
1305 int i, mval;
1306 char *p, *type;
1307 if (!nm)
1308 return 0;
1310 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1311 v = sk_CONF_VALUE_value(dn_sk, i);
1312 type = v->name;
1314 * Skip past any leading X. X: X, etc to allow for multiple instances
1316 for (p = type; *p; p++)
1317 #ifndef CHARSET_EBCDIC
1318 if ((*p == ':') || (*p == ',') || (*p == '.'))
1319 #else
1320 if ((*p == os_toascii[':']) || (*p == os_toascii[','])
1321 || (*p == os_toascii['.']))
1322 #endif
1324 p++;
1325 if (*p)
1326 type = p;
1327 break;
1329 #ifndef CHARSET_EBCDIC
1330 if (*type == '+')
1331 #else
1332 if (*type == os_toascii['+'])
1333 #endif
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;
1345 return 1;