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 */
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,
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)))
89 if (value && !(tvalue = BUF_strdup(value)))
91 if (!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
93 if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
98 if (!sk_CONF_VALUE_push(*extlist, vtmp))
102 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
108 OPENSSL_free(tvalue);
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)
125 OPENSSL_free(conf->name);
127 OPENSSL_free(conf->value);
129 OPENSSL_free(conf->section);
133 int X509V3_add_value_bool(const char *name, int asn1_bool,
134 STACK_OF(CONF_VALUE) **extlist)
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)
145 return X509V3_add_value(name, "TRUE", extlist);
149 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
151 BIGNUM *bntmp = 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);
162 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
164 BIGNUM *bntmp = 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);
175 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
182 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
186 if (value[0] == '-') {
192 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
199 ret = BN_hex2bn(&bn, value);
201 ret = BN_dec2bn(&bn, value);
203 if (!ret || value[ret]) {
205 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
209 if (isneg && BN_is_zero(bn))
212 aint = BN_to_ASN1_INTEGER(bn, NULL);
215 X509V3err(X509V3_F_S2I_ASN1_INTEGER,
216 X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
220 aint->type |= V_ASN1_NEG;
224 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
225 STACK_OF(CONF_VALUE) **extlist)
231 if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
233 ret = X509V3_add_value(name, strtmp, extlist);
234 OPENSSL_free(strtmp);
238 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
241 if (!(btmp = value->value))
243 if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
244 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
245 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
248 } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
249 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
250 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
255 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
256 X509V3_R_INVALID_BOOLEAN_STRING);
257 X509V3_conf_err(value);
261 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
264 if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
265 X509V3_conf_err(value);
279 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
283 STACK_OF(CONF_VALUE) *values = NULL;
286 /* We are going to modify the line so copy it first */
287 linebuf = BUF_strdup(line);
290 /* Go through all characters */
291 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
299 ntmp = strip_spaces(q);
301 X509V3err(X509V3_F_X509V3_PARSE_LIST,
302 X509V3_R_INVALID_NULL_NAME);
306 } else if (c == ',') {
308 ntmp = strip_spaces(q);
311 X509V3err(X509V3_F_X509V3_PARSE_LIST,
312 X509V3_R_INVALID_NULL_NAME);
315 X509V3_add_value(ntmp, NULL, &values);
323 vtmp = strip_spaces(q);
325 X509V3err(X509V3_F_X509V3_PARSE_LIST,
326 X509V3_R_INVALID_NULL_VALUE);
329 X509V3_add_value(ntmp, vtmp, &values);
337 if (state == HDR_VALUE) {
338 vtmp = strip_spaces(q);
340 X509V3err(X509V3_F_X509V3_PARSE_LIST,
341 X509V3_R_INVALID_NULL_VALUE);
344 X509V3_add_value(ntmp, vtmp, &values);
346 ntmp = strip_spaces(q);
348 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
351 X509V3_add_value(ntmp, NULL, &values);
353 OPENSSL_free(linebuf);
357 OPENSSL_free(linebuf);
358 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
363 /* Delete leading and trailing spaces from a string */
364 static char *strip_spaces(char *name)
367 /* Skip over leading spaces */
369 while (*p && isspace((unsigned char)*p))
373 q = p + strlen(p) - 1;
374 while ((q != p) && isspace((unsigned char)*q))
383 /* hex string utilities */
386 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
387 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
388 * on EBCDIC machines)
391 char *hex_to_string(const unsigned char *buffer, long len)
394 const unsigned char *p;
396 const static char hexdig[] = "0123456789ABCDEF";
399 if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
400 X509V3err(X509V3_F_HEX_TO_STRING, ERR_R_MALLOC_FAILURE);
404 for (i = 0, p = buffer; i < len; i++, p++) {
405 *q++ = hexdig[(*p >> 4) & 0xf];
406 *q++ = hexdig[*p & 0xf];
410 #ifdef CHARSET_EBCDIC
411 ebcdic2ascii(tmp, tmp, q - tmp - 1);
418 * Give a string of hex digits convert to a buffer
421 unsigned char *string_to_hex(const char *str, long *len)
423 unsigned char *hexbuf, *q;
424 unsigned char ch, cl, *p;
426 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_INVALID_NULL_ARGUMENT);
429 if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
431 for (p = (unsigned char *)str, q = hexbuf; *p;) {
433 #ifdef CHARSET_EBCDIC
434 ch = os_toebcdic[ch];
439 #ifdef CHARSET_EBCDIC
440 cl = os_toebcdic[cl];
443 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ODD_NUMBER_OF_DIGITS);
444 OPENSSL_free(hexbuf);
452 if ((ch >= '0') && (ch <= '9'))
454 else if ((ch >= 'a') && (ch <= 'f'))
459 if ((cl >= '0') && (cl <= '9'))
461 else if ((cl >= 'a') && (cl <= 'f'))
466 *q++ = (ch << 4) | cl;
476 OPENSSL_free(hexbuf);
477 X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE);
481 OPENSSL_free(hexbuf);
482 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT);
488 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
491 int name_cmp(const char *name, const char *cmp)
496 if ((ret = strncmp(name, cmp, len)))
499 if (!c || (c == '.'))
504 static int sk_strcmp(const char *const *a, const char *const *b)
506 return strcmp(*a, *b);
509 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
512 STACK_OF(OPENSSL_STRING) *ret;
514 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
515 ret = get_email(X509_get_subject_name(x), gens);
516 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
520 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
522 AUTHORITY_INFO_ACCESS *info;
523 STACK_OF(OPENSSL_STRING) *ret = NULL;
526 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
529 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
530 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
531 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
532 if (ad->location->type == GEN_URI) {
534 (&ret, ad->location->d.uniformResourceIdentifier))
539 AUTHORITY_INFO_ACCESS_free(info);
543 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
546 STACK_OF(X509_EXTENSION) *exts;
547 STACK_OF(OPENSSL_STRING) *ret;
549 exts = X509_REQ_get_extensions(x);
550 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
551 ret = get_email(X509_REQ_get_subject_name(x), gens);
552 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
553 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
557 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
560 STACK_OF(OPENSSL_STRING) *ret = NULL;
562 ASN1_IA5STRING *email;
565 /* Now add any email address(es) to STACK */
567 /* First supplied X509_NAME */
568 while ((i = X509_NAME_get_index_by_NID(name,
569 NID_pkcs9_emailAddress, i)) >= 0) {
570 ne = X509_NAME_get_entry(name, i);
571 email = X509_NAME_ENTRY_get_data(ne);
572 if (!append_ia5(&ret, email))
575 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
576 gen = sk_GENERAL_NAME_value(gens, i);
577 if (gen->type != GEN_EMAIL)
579 if (!append_ia5(&ret, gen->d.ia5))
585 static void str_free(OPENSSL_STRING str)
590 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
593 /* First some sanity checks */
594 if (email->type != V_ASN1_IA5STRING)
596 if (!email->data || !email->length)
599 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
602 /* Don't add duplicates */
603 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
605 emtmp = BUF_strdup((char *)email->data);
606 if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
607 X509_email_free(*sk);
614 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
616 sk_OPENSSL_STRING_pop_free(sk, str_free);
619 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
620 const unsigned char *subject, size_t subject_len,
623 /* Skip pattern prefix to match "wildcard" subject */
624 static void skip_prefix(const unsigned char **p, size_t *plen,
625 const unsigned char *subject, size_t subject_len,
628 const unsigned char *pattern = *p;
629 size_t pattern_len = *plen;
632 * If subject starts with a leading '.' followed by more octets, and
633 * pattern is longer, compare just an equal-length suffix with the
634 * full subject (starting at the '.'), provided the prefix contains
637 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
640 while (pattern_len > subject_len && *pattern) {
641 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
648 /* Skip if entire prefix acceptable */
649 if (pattern_len == subject_len) {
655 /* Compare while ASCII ignoring case. */
656 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
657 const unsigned char *subject, size_t subject_len,
660 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
661 if (pattern_len != subject_len)
663 while (pattern_len) {
664 unsigned char l = *pattern;
665 unsigned char r = *subject;
666 /* The pattern must not contain NUL characters. */
670 if ('A' <= l && l <= 'Z')
672 if ('A' <= r && r <= 'Z')
684 /* Compare using memcmp. */
685 static int equal_case(const unsigned char *pattern, size_t pattern_len,
686 const unsigned char *subject, size_t subject_len,
689 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
690 if (pattern_len != subject_len)
692 return !memcmp(pattern, subject, pattern_len);
696 * RFC 5280, section 7.5, requires that only the domain is compared in a
697 * case-insensitive manner.
699 static int equal_email(const unsigned char *a, size_t a_len,
700 const unsigned char *b, size_t b_len,
701 unsigned int unused_flags)
707 * We search backwards for the '@' character, so that we do not have to
708 * deal with quoted local-parts. The domain part is compared in a
709 * case-insensitive manner.
713 if (a[i] == '@' || b[i] == '@') {
714 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
721 return equal_case(a, i, b, i, 0);
725 * Compare the prefix and suffix with the subject, and check that the
726 * characters in-between are valid.
728 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
729 const unsigned char *suffix, size_t suffix_len,
730 const unsigned char *subject, size_t subject_len,
733 const unsigned char *wildcard_start;
734 const unsigned char *wildcard_end;
735 const unsigned char *p;
739 if (subject_len < prefix_len + suffix_len)
741 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
743 wildcard_start = subject + prefix_len;
744 wildcard_end = subject + (subject_len - suffix_len);
745 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
748 * If the wildcard makes up the entire first label, it must match at
749 * least one character.
751 if (prefix_len == 0 && *suffix == '.') {
752 if (wildcard_start == wildcard_end)
755 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
758 /* IDNA labels cannot match partial wildcards */
760 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
762 /* The wildcard may match a literal '*' */
763 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
766 * Check that the part matched by the wildcard contains only
767 * permitted characters and only matches a single label unless
768 * allow_multi is set.
770 for (p = wildcard_start; p != wildcard_end; ++p)
771 if (!(('0' <= *p && *p <= '9') ||
772 ('A' <= *p && *p <= 'Z') ||
773 ('a' <= *p && *p <= 'z') ||
774 *p == '-' || (allow_multi && *p == '.')))
779 #define LABEL_START (1 << 0)
780 #define LABEL_END (1 << 1)
781 #define LABEL_HYPHEN (1 << 2)
782 #define LABEL_IDNA (1 << 3)
784 static const unsigned char *valid_star(const unsigned char *p, size_t len,
787 const unsigned char *star = 0;
789 int state = LABEL_START;
791 for (i = 0; i < len; ++i) {
793 * Locate first and only legal wildcard, either at the start
794 * or end of a non-IDNA first and not final label.
797 int atstart = (state & LABEL_START);
798 int atend = (i == len - 1 || p[i + i] == '.');
800 * At most one wildcard per pattern.
801 * No wildcards in IDNA labels.
802 * No wildcards after the first label.
804 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
806 /* Only full-label '*.example.com' wildcards? */
807 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
808 && (!atstart || !atend))
810 /* No 'foo*bar' wildcards */
811 if (!atstart && !atend)
814 state &= ~LABEL_START;
815 } else if (('a' <= p[i] && p[i] <= 'z')
816 || ('A' <= p[i] && p[i] <= 'Z')
817 || ('0' <= p[i] && p[i] <= '9')) {
818 if ((state & LABEL_START) != 0
819 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
821 state &= ~(LABEL_HYPHEN | LABEL_START);
822 } else if (p[i] == '.') {
823 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
827 } else if (p[i] == '-') {
828 if ((state & LABEL_HYPHEN) != 0)
830 state |= LABEL_HYPHEN;
836 * The final label must not end in a hyphen or ".", and
837 * there must be at least two dots after the star.
839 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
844 /* Compare using wildcards. */
845 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
846 const unsigned char *subject, size_t subject_len,
849 const unsigned char *star = NULL;
852 * Subject names starting with '.' can only match a wildcard pattern
853 * via a subject sub-domain pattern suffix match.
855 if (!(subject_len > 1 && subject[0] == '.'))
856 star = valid_star(pattern, pattern_len, flags);
858 return equal_nocase(pattern, pattern_len,
859 subject, subject_len, flags);
860 return wildcard_match(pattern, star - pattern,
861 star + 1, (pattern + pattern_len) - star - 1,
862 subject, subject_len, flags);
866 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
867 * cmp_type > 0 only compare if string matches the type, otherwise convert it
871 static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
872 unsigned int flags, const char *b, size_t blen,
877 if (!a->data || !a->length)
880 if (cmp_type != a->type)
882 if (cmp_type == V_ASN1_IA5STRING)
883 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
884 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
886 if (rv > 0 && peername)
887 *peername = BUF_strndup((char *)a->data, a->length);
891 astrlen = ASN1_STRING_to_UTF8(&astr, a);
894 * -1 could be an internal malloc failure or a decoding error from
895 * malformed input; we can't distinguish.
899 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
900 if (rv > 0 && peername)
901 *peername = BUF_strndup((char *)astr, astrlen);
907 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
908 unsigned int flags, int check_type, char **peername)
910 GENERAL_NAMES *gens = NULL;
911 X509_NAME *name = NULL;
919 /* See below, this flag is internal-only */
920 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
921 if (check_type == GEN_EMAIL) {
922 cnid = NID_pkcs9_emailAddress;
923 alt_type = V_ASN1_IA5STRING;
925 } else if (check_type == GEN_DNS) {
926 cnid = NID_commonName;
927 /* Implicit client-side DNS sub-domain pattern */
928 if (chklen > 1 && chk[0] == '.')
929 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
930 alt_type = V_ASN1_IA5STRING;
931 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
932 equal = equal_nocase;
934 equal = equal_wildcard;
937 alt_type = V_ASN1_OCTET_STRING;
942 chklen = strlen(chk);
944 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
946 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
949 gen = sk_GENERAL_NAME_value(gens, i);
950 if (gen->type != check_type)
953 if (check_type == GEN_EMAIL)
954 cstr = gen->d.rfc822Name;
955 else if (check_type == GEN_DNS)
956 cstr = gen->d.dNSName;
958 cstr = gen->d.iPAddress;
959 /* Positive on success, negative on error! */
960 if ((rv = do_check_string(cstr, alt_type, equal, flags,
961 chk, chklen, peername)) != 0)
964 GENERAL_NAMES_free(gens);
969 && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)))
973 name = X509_get_subject_name(x);
974 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
977 ne = X509_NAME_get_entry(name, i);
978 str = X509_NAME_ENTRY_get_data(ne);
979 /* Positive on success, negative on error! */
980 if ((rv = do_check_string(str, -1, equal, flags,
981 chk, chklen, peername)) != 0)
987 int X509_check_host(X509 *x, const char *chk, size_t chklen,
988 unsigned int flags, char **peername)
993 * Embedded NULs are disallowed, except as the last character of a
994 * string of length 2 or more (tolerate caller including terminating
995 * NUL in string length).
998 chklen = strlen(chk);
999 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1001 if (chklen > 1 && chk[chklen - 1] == '\0')
1003 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1006 int X509_check_email(X509 *x, const char *chk, size_t chklen,
1012 * Embedded NULs are disallowed, except as the last character of a
1013 * string of length 2 or more (tolerate caller including terminating
1014 * NUL in string length).
1017 chklen = strlen((char *)chk);
1018 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
1020 if (chklen > 1 && chk[chklen - 1] == '\0')
1022 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1025 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
1030 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1033 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
1035 unsigned char ipout[16];
1040 iplen = (size_t)a2i_ipadd(ipout, ipasc);
1043 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1047 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1051 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
1053 unsigned char ipout[16];
1054 ASN1_OCTET_STRING *ret;
1057 /* If string contains a ':' assume IPv6 */
1059 iplen = a2i_ipadd(ipout, ipasc);
1064 ret = ASN1_OCTET_STRING_new();
1067 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1068 ASN1_OCTET_STRING_free(ret);
1074 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
1076 ASN1_OCTET_STRING *ret = NULL;
1077 unsigned char ipout[32];
1078 char *iptmp = NULL, *p;
1080 p = strchr(ipasc, '/');
1083 iptmp = BUF_strdup(ipasc);
1086 p = iptmp + (p - ipasc);
1089 iplen1 = a2i_ipadd(ipout, iptmp);
1094 iplen2 = a2i_ipadd(ipout + iplen1, p);
1096 OPENSSL_free(iptmp);
1099 if (!iplen2 || (iplen1 != iplen2))
1102 ret = ASN1_OCTET_STRING_new();
1105 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1112 OPENSSL_free(iptmp);
1114 ASN1_OCTET_STRING_free(ret);
1118 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1120 /* If string contains a ':' assume IPv6 */
1122 if (strchr(ipasc, ':')) {
1123 if (!ipv6_from_asc(ipout, ipasc))
1127 if (!ipv4_from_asc(ipout, ipasc))
1133 static int ipv4_from_asc(unsigned char *v4, const char *in)
1136 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1138 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1139 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1149 /* Temporary store for IPV6 output */
1150 unsigned char tmp[16];
1151 /* Total number of bytes in tmp */
1153 /* The position of a zero (corresponding to '::') */
1155 /* Number of zeroes */
1159 static int ipv6_from_asc(unsigned char *v6, const char *in)
1163 v6stat.zero_pos = -1;
1164 v6stat.zero_cnt = 0;
1166 * Treat the IPv6 representation as a list of values separated by ':'.
1167 * The presence of a '::' will parse as one, two or three zero length
1170 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1173 /* Now for some sanity checks */
1175 if (v6stat.zero_pos == -1) {
1176 /* If no '::' must have exactly 16 bytes */
1177 if (v6stat.total != 16)
1180 /* If '::' must have less than 16 bytes */
1181 if (v6stat.total == 16)
1183 /* More than three zeroes is an error */
1184 if (v6stat.zero_cnt > 3)
1186 /* Can only have three zeroes if nothing else present */
1187 else if (v6stat.zero_cnt == 3) {
1188 if (v6stat.total > 0)
1191 /* Can only have two zeroes if at start or end */
1192 else if (v6stat.zero_cnt == 2) {
1193 if ((v6stat.zero_pos != 0)
1194 && (v6stat.zero_pos != v6stat.total))
1197 /* Can only have one zero if *not* start or end */
1199 if ((v6stat.zero_pos == 0)
1200 || (v6stat.zero_pos == v6stat.total))
1207 if (v6stat.zero_pos >= 0) {
1208 /* Copy initial part */
1209 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1211 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1212 /* Copy final part */
1213 if (v6stat.total != v6stat.zero_pos)
1214 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1215 v6stat.tmp + v6stat.zero_pos,
1216 v6stat.total - v6stat.zero_pos);
1218 memcpy(v6, v6stat.tmp, 16);
1223 static int ipv6_cb(const char *elem, int len, void *usr)
1226 /* Error if 16 bytes written */
1230 /* Zero length element, corresponds to '::' */
1231 if (s->zero_pos == -1)
1232 s->zero_pos = s->total;
1233 /* If we've already got a :: its an error */
1234 else if (s->zero_pos != s->total)
1238 /* If more than 4 characters could be final a.b.c.d form */
1240 /* Need at least 4 bytes left */
1243 /* Must be end of string */
1246 if (!ipv4_from_asc(s->tmp + s->total, elem))
1250 if (!ipv6_hex(s->tmp + s->total, elem, len))
1259 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1262 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1265 unsigned int num = 0;
1271 if ((c >= '0') && (c <= '9'))
1273 else if ((c >= 'A') && (c <= 'F'))
1274 num |= c - 'A' + 10;
1275 else if ((c >= 'a') && (c <= 'f'))
1276 num |= c - 'a' + 10;
1281 out[1] = num & 0xff;
1285 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1286 unsigned long chtype)
1294 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1295 v = sk_CONF_VALUE_value(dn_sk, i);
1298 * Skip past any leading X. X: X, etc to allow for multiple instances
1300 for (p = type; *p; p++)
1301 #ifndef CHARSET_EBCDIC
1302 if ((*p == ':') || (*p == ',') || (*p == '.'))
1304 if ((*p == os_toascii[':']) || (*p == os_toascii[','])
1305 || (*p == os_toascii['.']))
1313 #ifndef CHARSET_EBCDIC
1316 if (*type == os_toascii['+'])
1323 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1324 (unsigned char *)v->value, -1, -1,