3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
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
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
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
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 */
63 #include "internal/cryptlib.h"
64 #include <openssl/conf.h>
65 #include <openssl/x509v3.h>
66 #include "internal/x509_int.h"
67 #include <openssl/bn.h>
69 static char *strip_spaces(char *name);
70 static int sk_strcmp(const char *const *a, const char *const *b);
71 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
73 static void str_free(OPENSSL_STRING str);
74 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
76 static int ipv4_from_asc(unsigned char *v4, const char *in);
77 static int ipv6_from_asc(unsigned char *v6, const char *in);
78 static int ipv6_cb(const char *elem, int len, void *usr);
79 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
81 /* Add a CONF_VALUE name value pair to stack */
83 int X509V3_add_value(const char *name, const char *value,
84 STACK_OF(CONF_VALUE) **extlist)
86 CONF_VALUE *vtmp = NULL;
87 char *tname = NULL, *tvalue = NULL;
89 if (name && (tname = BUF_strdup(name)) == NULL)
91 if (value && (tvalue = BUF_strdup(value)) == NULL)
93 if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)
95 if (*extlist == NULL && (*extlist = sk_CONF_VALUE_new_null()) == NULL)
100 if (!sk_CONF_VALUE_push(*extlist, vtmp))
104 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
107 OPENSSL_free(tvalue);
111 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
112 STACK_OF(CONF_VALUE) **extlist)
114 return X509V3_add_value(name, (const char *)value, extlist);
117 /* Free function for STACK_OF(CONF_VALUE) */
119 void X509V3_conf_free(CONF_VALUE *conf)
123 OPENSSL_free(conf->name);
124 OPENSSL_free(conf->value);
125 OPENSSL_free(conf->section);
129 int X509V3_add_value_bool(const char *name, int asn1_bool,
130 STACK_OF(CONF_VALUE) **extlist)
133 return X509V3_add_value(name, "TRUE", extlist);
134 return X509V3_add_value(name, "FALSE", extlist);
137 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
138 STACK_OF(CONF_VALUE) **extlist)
141 return X509V3_add_value(name, "TRUE", extlist);
145 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
147 BIGNUM *bntmp = NULL;
152 if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL
153 || (strtmp = BN_bn2dec(bntmp)) == NULL)
154 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
159 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
161 BIGNUM *bntmp = NULL;
166 if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL
167 || (strtmp = BN_bn2dec(bntmp)) == NULL)
168 X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
173 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
180 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
184 if (value[0] == '-') {
190 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
197 ret = BN_hex2bn(&bn, value);
199 ret = BN_dec2bn(&bn, value);
201 if (!ret || value[ret]) {
203 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
207 if (isneg && BN_is_zero(bn))
210 aint = BN_to_ASN1_INTEGER(bn, NULL);
213 X509V3err(X509V3_F_S2I_ASN1_INTEGER,
214 X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
218 aint->type |= V_ASN1_NEG;
222 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
223 STACK_OF(CONF_VALUE) **extlist)
230 if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL)
232 ret = X509V3_add_value(name, strtmp, extlist);
233 OPENSSL_free(strtmp);
237 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
241 if ((btmp = value->value) == NULL)
243 if (strcmp(btmp, "TRUE") == 0
244 || strcmp(btmp, "true") == 0
245 || strcmp(btmp, "Y") == 0
246 || strcmp(btmp, "y") == 0
247 || strcmp(btmp, "YES") == 0
248 || strcmp(btmp, "yes") == 0) {
252 if (strcmp(btmp, "FALSE") == 0
253 || strcmp(btmp, "false") == 0
254 || strcmp(btmp, "N") == 0
255 || strcmp(btmp, "n") == 0
256 || strcmp(btmp, "NO") == 0
257 || strcmp(btmp, "no") == 0) {
262 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
263 X509V3_R_INVALID_BOOLEAN_STRING);
264 X509V3_conf_err(value);
268 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
272 if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) {
273 X509V3_conf_err(value);
287 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
291 STACK_OF(CONF_VALUE) *values = NULL;
294 /* We are going to modify the line so copy it first */
295 linebuf = BUF_strdup(line);
296 if (linebuf == NULL) {
297 X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);
302 /* Go through all characters */
303 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
311 ntmp = strip_spaces(q);
313 X509V3err(X509V3_F_X509V3_PARSE_LIST,
314 X509V3_R_INVALID_NULL_NAME);
318 } else if (c == ',') {
320 ntmp = strip_spaces(q);
323 X509V3err(X509V3_F_X509V3_PARSE_LIST,
324 X509V3_R_INVALID_NULL_NAME);
327 X509V3_add_value(ntmp, NULL, &values);
335 vtmp = strip_spaces(q);
337 X509V3err(X509V3_F_X509V3_PARSE_LIST,
338 X509V3_R_INVALID_NULL_VALUE);
341 X509V3_add_value(ntmp, vtmp, &values);
349 if (state == HDR_VALUE) {
350 vtmp = strip_spaces(q);
352 X509V3err(X509V3_F_X509V3_PARSE_LIST,
353 X509V3_R_INVALID_NULL_VALUE);
356 X509V3_add_value(ntmp, vtmp, &values);
358 ntmp = strip_spaces(q);
360 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
363 X509V3_add_value(ntmp, NULL, &values);
365 OPENSSL_free(linebuf);
369 OPENSSL_free(linebuf);
370 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
375 /* Delete leading and trailing spaces from a string */
376 static char *strip_spaces(char *name)
379 /* Skip over leading spaces */
381 while (*p && isspace((unsigned char)*p))
385 q = p + strlen(p) - 1;
386 while ((q != p) && isspace((unsigned char)*q))
395 /* hex string utilities */
398 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
399 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
400 * on EBCDIC machines)
403 char *hex_to_string(const unsigned char *buffer, long len)
406 const unsigned char *p;
408 const static char hexdig[] = "0123456789ABCDEF";
411 if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {
412 X509V3err(X509V3_F_HEX_TO_STRING, ERR_R_MALLOC_FAILURE);
416 for (i = 0, p = buffer; i < len; i++, p++) {
417 *q++ = hexdig[(*p >> 4) & 0xf];
418 *q++ = hexdig[*p & 0xf];
422 #ifdef CHARSET_EBCDIC
423 ebcdic2ascii(tmp, tmp, q - tmp - 1);
430 * Give a string of hex digits convert to a buffer
433 unsigned char *string_to_hex(const char *str, long *len)
435 unsigned char *hexbuf, *q;
436 unsigned char ch, cl, *p;
438 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_INVALID_NULL_ARGUMENT);
441 if ((hexbuf = OPENSSL_malloc(strlen(str) >> 1)) == NULL)
443 for (p = (unsigned char *)str, q = hexbuf; *p;) {
445 #ifdef CHARSET_EBCDIC
446 ch = os_toebcdic[ch];
451 #ifdef CHARSET_EBCDIC
452 cl = os_toebcdic[cl];
455 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ODD_NUMBER_OF_DIGITS);
456 OPENSSL_free(hexbuf);
464 if ((ch >= '0') && (ch <= '9'))
466 else if ((ch >= 'a') && (ch <= 'f'))
471 if ((cl >= '0') && (cl <= '9'))
473 else if ((cl >= 'a') && (cl <= 'f'))
478 *q++ = (ch << 4) | cl;
487 OPENSSL_free(hexbuf);
488 X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE);
492 OPENSSL_free(hexbuf);
493 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT);
499 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
502 int name_cmp(const char *name, const char *cmp)
507 if ((ret = strncmp(name, cmp, len)))
510 if (!c || (c == '.'))
515 static int sk_strcmp(const char *const *a, const char *const *b)
517 return strcmp(*a, *b);
520 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
523 STACK_OF(OPENSSL_STRING) *ret;
525 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
526 ret = get_email(X509_get_subject_name(x), gens);
527 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
531 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
533 AUTHORITY_INFO_ACCESS *info;
534 STACK_OF(OPENSSL_STRING) *ret = NULL;
537 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
540 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
541 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
542 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
543 if (ad->location->type == GEN_URI) {
545 (&ret, ad->location->d.uniformResourceIdentifier))
550 AUTHORITY_INFO_ACCESS_free(info);
554 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
557 STACK_OF(X509_EXTENSION) *exts;
558 STACK_OF(OPENSSL_STRING) *ret;
560 exts = X509_REQ_get_extensions(x);
561 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
562 ret = get_email(X509_REQ_get_subject_name(x), gens);
563 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
564 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
568 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
571 STACK_OF(OPENSSL_STRING) *ret = NULL;
573 ASN1_IA5STRING *email;
576 /* Now add any email address(es) to STACK */
578 /* First supplied X509_NAME */
579 while ((i = X509_NAME_get_index_by_NID(name,
580 NID_pkcs9_emailAddress, i)) >= 0) {
581 ne = X509_NAME_get_entry(name, i);
582 email = X509_NAME_ENTRY_get_data(ne);
583 if (!append_ia5(&ret, email))
586 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
587 gen = sk_GENERAL_NAME_value(gens, i);
588 if (gen->type != GEN_EMAIL)
590 if (!append_ia5(&ret, gen->d.ia5))
596 static void str_free(OPENSSL_STRING str)
601 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
604 /* First some sanity checks */
605 if (email->type != V_ASN1_IA5STRING)
607 if (!email->data || !email->length)
610 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
613 /* Don't add duplicates */
614 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
616 emtmp = BUF_strdup((char *)email->data);
617 if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
618 X509_email_free(*sk);
625 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
627 sk_OPENSSL_STRING_pop_free(sk, str_free);
630 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
631 const unsigned char *subject, size_t subject_len,
634 /* Skip pattern prefix to match "wildcard" subject */
635 static void skip_prefix(const unsigned char **p, size_t *plen,
636 const unsigned char *subject, size_t subject_len,
639 const unsigned char *pattern = *p;
640 size_t pattern_len = *plen;
643 * If subject starts with a leading '.' followed by more octets, and
644 * pattern is longer, compare just an equal-length suffix with the
645 * full subject (starting at the '.'), provided the prefix contains
648 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
651 while (pattern_len > subject_len && *pattern) {
652 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
659 /* Skip if entire prefix acceptable */
660 if (pattern_len == subject_len) {
666 /* Compare while ASCII ignoring case. */
667 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
668 const unsigned char *subject, size_t subject_len,
671 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
672 if (pattern_len != subject_len)
674 while (pattern_len) {
675 unsigned char l = *pattern;
676 unsigned char r = *subject;
677 /* The pattern must not contain NUL characters. */
681 if ('A' <= l && l <= 'Z')
683 if ('A' <= r && r <= 'Z')
695 /* Compare using memcmp. */
696 static int equal_case(const unsigned char *pattern, size_t pattern_len,
697 const unsigned char *subject, size_t subject_len,
700 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
701 if (pattern_len != subject_len)
703 return !memcmp(pattern, subject, pattern_len);
707 * RFC 5280, section 7.5, requires that only the domain is compared in a
708 * case-insensitive manner.
710 static int equal_email(const unsigned char *a, size_t a_len,
711 const unsigned char *b, size_t b_len,
712 unsigned int unused_flags)
718 * We search backwards for the '@' character, so that we do not have to
719 * deal with quoted local-parts. The domain part is compared in a
720 * case-insensitive manner.
724 if (a[i] == '@' || b[i] == '@') {
725 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
732 return equal_case(a, i, b, i, 0);
736 * Compare the prefix and suffix with the subject, and check that the
737 * characters in-between are valid.
739 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
740 const unsigned char *suffix, size_t suffix_len,
741 const unsigned char *subject, size_t subject_len,
744 const unsigned char *wildcard_start;
745 const unsigned char *wildcard_end;
746 const unsigned char *p;
750 if (subject_len < prefix_len + suffix_len)
752 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
754 wildcard_start = subject + prefix_len;
755 wildcard_end = subject + (subject_len - suffix_len);
756 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
759 * If the wildcard makes up the entire first label, it must match at
760 * least one character.
762 if (prefix_len == 0 && *suffix == '.') {
763 if (wildcard_start == wildcard_end)
766 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
769 /* IDNA labels cannot match partial wildcards */
771 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
773 /* The wildcard may match a literal '*' */
774 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
777 * Check that the part matched by the wildcard contains only
778 * permitted characters and only matches a single label unless
779 * allow_multi is set.
781 for (p = wildcard_start; p != wildcard_end; ++p)
782 if (!(('0' <= *p && *p <= '9') ||
783 ('A' <= *p && *p <= 'Z') ||
784 ('a' <= *p && *p <= 'z') ||
785 *p == '-' || (allow_multi && *p == '.')))
790 #define LABEL_START (1 << 0)
791 #define LABEL_END (1 << 1)
792 #define LABEL_HYPHEN (1 << 2)
793 #define LABEL_IDNA (1 << 3)
795 static const unsigned char *valid_star(const unsigned char *p, size_t len,
798 const unsigned char *star = 0;
800 int state = LABEL_START;
802 for (i = 0; i < len; ++i) {
804 * Locate first and only legal wildcard, either at the start
805 * or end of a non-IDNA first and not final label.
808 int atstart = (state & LABEL_START);
809 int atend = (i == len - 1 || p[i + 1] == '.');
811 * At most one wildcard per pattern.
812 * No wildcards in IDNA labels.
813 * No wildcards after the first label.
815 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
817 /* Only full-label '*.example.com' wildcards? */
818 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
819 && (!atstart || !atend))
821 /* No 'foo*bar' wildcards */
822 if (!atstart && !atend)
825 state &= ~LABEL_START;
826 } else if (('a' <= p[i] && p[i] <= 'z')
827 || ('A' <= p[i] && p[i] <= 'Z')
828 || ('0' <= p[i] && p[i] <= '9')) {
829 if ((state & LABEL_START) != 0
830 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
832 state &= ~(LABEL_HYPHEN | LABEL_START);
833 } else if (p[i] == '.') {
834 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
838 } else if (p[i] == '-') {
839 if ((state & LABEL_HYPHEN) != 0)
841 state |= LABEL_HYPHEN;
847 * The final label must not end in a hyphen or ".", and
848 * there must be at least two dots after the star.
850 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
855 /* Compare using wildcards. */
856 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
857 const unsigned char *subject, size_t subject_len,
860 const unsigned char *star = NULL;
863 * Subject names starting with '.' can only match a wildcard pattern
864 * via a subject sub-domain pattern suffix match.
866 if (!(subject_len > 1 && subject[0] == '.'))
867 star = valid_star(pattern, pattern_len, flags);
869 return equal_nocase(pattern, pattern_len,
870 subject, subject_len, flags);
871 return wildcard_match(pattern, star - pattern,
872 star + 1, (pattern + pattern_len) - star - 1,
873 subject, subject_len, flags);
877 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
878 * cmp_type > 0 only compare if string matches the type, otherwise convert it
882 static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
883 unsigned int flags, const char *b, size_t blen,
888 if (!a->data || !a->length)
891 if (cmp_type != a->type)
893 if (cmp_type == V_ASN1_IA5STRING)
894 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
895 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
897 if (rv > 0 && peername)
898 *peername = BUF_strndup((char *)a->data, a->length);
902 astrlen = ASN1_STRING_to_UTF8(&astr, a);
905 * -1 could be an internal malloc failure or a decoding error from
906 * malformed input; we can't distinguish.
910 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
911 if (rv > 0 && peername)
912 *peername = BUF_strndup((char *)astr, astrlen);
918 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
919 unsigned int flags, int check_type, char **peername)
921 GENERAL_NAMES *gens = NULL;
922 X509_NAME *name = NULL;
924 int cnid = NID_undef;
930 /* See below, this flag is internal-only */
931 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
932 if (check_type == GEN_EMAIL) {
933 cnid = NID_pkcs9_emailAddress;
934 alt_type = V_ASN1_IA5STRING;
936 } else if (check_type == GEN_DNS) {
937 cnid = NID_commonName;
938 /* Implicit client-side DNS sub-domain pattern */
939 if (chklen > 1 && chk[0] == '.')
940 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
941 alt_type = V_ASN1_IA5STRING;
942 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
943 equal = equal_nocase;
945 equal = equal_wildcard;
947 alt_type = V_ASN1_OCTET_STRING;
952 chklen = strlen(chk);
954 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
956 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
959 gen = sk_GENERAL_NAME_value(gens, i);
960 if (gen->type != check_type)
963 if (check_type == GEN_EMAIL)
964 cstr = gen->d.rfc822Name;
965 else if (check_type == GEN_DNS)
966 cstr = gen->d.dNSName;
968 cstr = gen->d.iPAddress;
969 /* Positive on success, negative on error! */
970 if ((rv = do_check_string(cstr, alt_type, equal, flags,
971 chk, chklen, peername)) != 0)
974 GENERAL_NAMES_free(gens);
977 if (cnid == NID_undef
979 && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
983 /* We're done if CN-ID is not pertinent */
984 if (cnid == NID_undef)
988 name = X509_get_subject_name(x);
989 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
992 ne = X509_NAME_get_entry(name, i);
993 str = X509_NAME_ENTRY_get_data(ne);
994 /* Positive on success, negative on error! */
995 if ((rv = do_check_string(str, -1, equal, flags,
996 chk, chklen, peername)) != 0)
1002 int X509_check_host(X509 *x, const char *chk, size_t chklen,
1003 unsigned int flags, char **peername)
1008 * Embedded NULs are disallowed, except as the last character of a
1009 * string of length 2 or more (tolerate caller including terminating
1010 * NUL in string length).
1013 chklen = strlen(chk);
1014 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1016 if (chklen > 1 && chk[chklen - 1] == '\0')
1018 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1021 int X509_check_email(X509 *x, const char *chk, size_t chklen,
1027 * Embedded NULs are disallowed, except as the last character of a
1028 * string of length 2 or more (tolerate caller including terminating
1029 * NUL in string length).
1032 chklen = strlen((char *)chk);
1033 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1035 if (chklen > 1 && chk[chklen - 1] == '\0')
1037 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1040 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
1045 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1048 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1050 unsigned char ipout[16];
1055 iplen = (size_t)a2i_ipadd(ipout, ipasc);
1058 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1062 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1066 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1068 unsigned char ipout[16];
1069 ASN1_OCTET_STRING *ret;
1072 /* If string contains a ':' assume IPv6 */
1074 iplen = a2i_ipadd(ipout, ipasc);
1079 ret = ASN1_OCTET_STRING_new();
1082 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1083 ASN1_OCTET_STRING_free(ret);
1089 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1091 ASN1_OCTET_STRING *ret = NULL;
1092 unsigned char ipout[32];
1093 char *iptmp = NULL, *p;
1095 p = strchr(ipasc, '/');
1098 iptmp = BUF_strdup(ipasc);
1101 p = iptmp + (p - ipasc);
1104 iplen1 = a2i_ipadd(ipout, iptmp);
1109 iplen2 = a2i_ipadd(ipout + iplen1, p);
1111 OPENSSL_free(iptmp);
1114 if (!iplen2 || (iplen1 != iplen2))
1117 ret = ASN1_OCTET_STRING_new();
1120 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1126 OPENSSL_free(iptmp);
1127 ASN1_OCTET_STRING_free(ret);
1131 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1133 /* If string contains a ':' assume IPv6 */
1135 if (strchr(ipasc, ':')) {
1136 if (!ipv6_from_asc(ipout, ipasc))
1140 if (!ipv4_from_asc(ipout, ipasc))
1146 static int ipv4_from_asc(unsigned char *v4, const char *in)
1149 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1151 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1152 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1162 /* Temporary store for IPV6 output */
1163 unsigned char tmp[16];
1164 /* Total number of bytes in tmp */
1166 /* The position of a zero (corresponding to '::') */
1168 /* Number of zeroes */
1172 static int ipv6_from_asc(unsigned char *v6, const char *in)
1176 v6stat.zero_pos = -1;
1177 v6stat.zero_cnt = 0;
1179 * Treat the IPv6 representation as a list of values separated by ':'.
1180 * The presence of a '::' will parse as one, two or three zero length
1183 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1186 /* Now for some sanity checks */
1188 if (v6stat.zero_pos == -1) {
1189 /* If no '::' must have exactly 16 bytes */
1190 if (v6stat.total != 16)
1193 /* If '::' must have less than 16 bytes */
1194 if (v6stat.total == 16)
1196 /* More than three zeroes is an error */
1197 if (v6stat.zero_cnt > 3)
1199 /* Can only have three zeroes if nothing else present */
1200 else if (v6stat.zero_cnt == 3) {
1201 if (v6stat.total > 0)
1204 /* Can only have two zeroes if at start or end */
1205 else if (v6stat.zero_cnt == 2) {
1206 if ((v6stat.zero_pos != 0)
1207 && (v6stat.zero_pos != v6stat.total))
1210 /* Can only have one zero if *not* start or end */
1212 if ((v6stat.zero_pos == 0)
1213 || (v6stat.zero_pos == v6stat.total))
1220 if (v6stat.zero_pos >= 0) {
1221 /* Copy initial part */
1222 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1224 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1225 /* Copy final part */
1226 if (v6stat.total != v6stat.zero_pos)
1227 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1228 v6stat.tmp + v6stat.zero_pos,
1229 v6stat.total - v6stat.zero_pos);
1231 memcpy(v6, v6stat.tmp, 16);
1236 static int ipv6_cb(const char *elem, int len, void *usr)
1239 /* Error if 16 bytes written */
1243 /* Zero length element, corresponds to '::' */
1244 if (s->zero_pos == -1)
1245 s->zero_pos = s->total;
1246 /* If we've already got a :: its an error */
1247 else if (s->zero_pos != s->total)
1251 /* If more than 4 characters could be final a.b.c.d form */
1253 /* Need at least 4 bytes left */
1256 /* Must be end of string */
1259 if (!ipv4_from_asc(s->tmp + s->total, elem))
1263 if (!ipv6_hex(s->tmp + s->total, elem, len))
1272 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1275 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1278 unsigned int num = 0;
1284 if ((c >= '0') && (c <= '9'))
1286 else if ((c >= 'A') && (c <= 'F'))
1287 num |= c - 'A' + 10;
1288 else if ((c >= 'a') && (c <= 'f'))
1289 num |= c - 'a' + 10;
1294 out[1] = num & 0xff;
1298 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1299 unsigned long chtype)
1307 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1308 v = sk_CONF_VALUE_value(dn_sk, i);
1311 * Skip past any leading X. X: X, etc to allow for multiple instances
1313 for (p = type; *p; p++)
1314 #ifndef CHARSET_EBCDIC
1315 if ((*p == ':') || (*p == ',') || (*p == '.'))
1317 if ((*p == os_toascii[':']) || (*p == os_toascii[','])
1318 || (*p == os_toascii['.']))
1326 #ifndef CHARSET_EBCDIC
1329 if (*type == os_toascii['+'])
1336 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1337 (unsigned char *)v->value, -1, -1,