2 * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* X509 v3 extension utilities */
13 #include "internal/cryptlib.h"
15 #include "crypto/ctype.h"
16 #include <openssl/conf.h>
17 #include <openssl/crypto.h>
18 #include <openssl/x509v3.h>
19 #include "crypto/x509.h"
20 #include <openssl/bn.h>
23 static char *strip_spaces(char *name);
24 static int sk_strcmp(const char *const *a, const char *const *b);
25 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
27 static void str_free(OPENSSL_STRING str);
28 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email);
30 static int ipv4_from_asc(unsigned char *v4, const char *in);
31 static int ipv6_from_asc(unsigned char *v6, const char *in);
32 static int ipv6_cb(const char *elem, int len, void *usr);
33 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
35 /* Add a CONF_VALUE name value pair to stack */
37 int X509V3_add_value(const char *name, const char *value,
38 STACK_OF(CONF_VALUE) **extlist)
40 CONF_VALUE *vtmp = NULL;
41 char *tname = NULL, *tvalue = NULL;
42 int sk_allocated = (*extlist == NULL);
44 if (name && (tname = OPENSSL_strdup(name)) == NULL)
46 if (value && (tvalue = OPENSSL_strdup(value)) == NULL)
48 if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)
50 if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL)
55 if (!sk_CONF_VALUE_push(*extlist, vtmp))
59 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
61 sk_CONF_VALUE_free(*extlist);
70 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
71 STACK_OF(CONF_VALUE) **extlist)
73 return X509V3_add_value(name, (const char *)value, extlist);
76 /* Free function for STACK_OF(CONF_VALUE) */
78 void X509V3_conf_free(CONF_VALUE *conf)
82 OPENSSL_free(conf->name);
83 OPENSSL_free(conf->value);
84 OPENSSL_free(conf->section);
88 int X509V3_add_value_bool(const char *name, int asn1_bool,
89 STACK_OF(CONF_VALUE) **extlist)
92 return X509V3_add_value(name, "TRUE", extlist);
93 return X509V3_add_value(name, "FALSE", extlist);
96 int X509V3_add_value_bool_nf(const char *name, int asn1_bool,
97 STACK_OF(CONF_VALUE) **extlist)
100 return X509V3_add_value(name, "TRUE", extlist);
104 static char *bignum_to_string(const BIGNUM *bn)
110 * Display large numbers in hex and small numbers in decimal. Converting to
111 * decimal takes quadratic time and is no more useful than hex for large
114 if (BN_num_bits(bn) < 128)
115 return BN_bn2dec(bn);
121 len = strlen(tmp) + 3;
122 ret = OPENSSL_malloc(len);
124 X509V3err(X509V3_F_BIGNUM_TO_STRING, ERR_R_MALLOC_FAILURE);
129 /* Prepend "0x", but place it after the "-" if negative. */
131 OPENSSL_strlcpy(ret, "-0x", len);
132 OPENSSL_strlcat(ret, tmp + 1, len);
134 OPENSSL_strlcpy(ret, "0x", len);
135 OPENSSL_strlcat(ret, tmp, len);
141 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
143 BIGNUM *bntmp = NULL;
148 if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL
149 || (strtmp = bignum_to_string(bntmp)) == NULL)
150 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
155 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)
157 BIGNUM *bntmp = NULL;
162 if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL
163 || (strtmp = bignum_to_string(bntmp)) == NULL)
164 X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
169 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value)
176 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
181 X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
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, const 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(const 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(const 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 = OPENSSL_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 && ossl_isspace(*p))
385 q = p + strlen(p) - 1;
386 while ((q != p) && ossl_isspace(*q))
397 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
400 int name_cmp(const char *name, const char *cmp)
405 if ((ret = strncmp(name, cmp, len)))
408 if (!c || (c == '.'))
413 static int sk_strcmp(const char *const *a, const char *const *b)
415 return strcmp(*a, *b);
418 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
421 STACK_OF(OPENSSL_STRING) *ret;
423 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
424 ret = get_email(X509_get_subject_name(x), gens);
425 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
429 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
431 AUTHORITY_INFO_ACCESS *info;
432 STACK_OF(OPENSSL_STRING) *ret = NULL;
435 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
438 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
439 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
440 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
441 if (ad->location->type == GEN_URI) {
443 (&ret, ad->location->d.uniformResourceIdentifier))
448 AUTHORITY_INFO_ACCESS_free(info);
452 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
455 STACK_OF(X509_EXTENSION) *exts;
456 STACK_OF(OPENSSL_STRING) *ret;
458 exts = X509_REQ_get_extensions(x);
459 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
460 ret = get_email(X509_REQ_get_subject_name(x), gens);
461 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
462 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
466 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
469 STACK_OF(OPENSSL_STRING) *ret = NULL;
471 const ASN1_IA5STRING *email;
475 /* Now add any email address(es) to STACK */
476 /* First supplied X509_NAME */
477 while ((i = X509_NAME_get_index_by_NID(name,
478 NID_pkcs9_emailAddress, i)) >= 0) {
479 ne = X509_NAME_get_entry(name, i);
480 email = X509_NAME_ENTRY_get_data(ne);
481 if (!append_ia5(&ret, email))
484 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
485 gen = sk_GENERAL_NAME_value(gens, i);
486 if (gen->type != GEN_EMAIL)
488 if (!append_ia5(&ret, gen->d.ia5))
494 static void str_free(OPENSSL_STRING str)
499 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email)
502 /* First some sanity checks */
503 if (email->type != V_ASN1_IA5STRING)
505 if (!email->data || !email->length)
508 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
511 /* Don't add duplicates */
512 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
514 emtmp = OPENSSL_strdup((char *)email->data);
515 if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
516 OPENSSL_free(emtmp); /* free on push failure */
517 X509_email_free(*sk);
524 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
526 sk_OPENSSL_STRING_pop_free(sk, str_free);
529 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
530 const unsigned char *subject, size_t subject_len,
533 /* Skip pattern prefix to match "wildcard" subject */
534 static void skip_prefix(const unsigned char **p, size_t *plen,
538 const unsigned char *pattern = *p;
539 size_t pattern_len = *plen;
542 * If subject starts with a leading '.' followed by more octets, and
543 * pattern is longer, compare just an equal-length suffix with the
544 * full subject (starting at the '.'), provided the prefix contains
547 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
550 while (pattern_len > subject_len && *pattern) {
551 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
558 /* Skip if entire prefix acceptable */
559 if (pattern_len == subject_len) {
565 /* Compare while ASCII ignoring case. */
566 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
567 const unsigned char *subject, size_t subject_len,
570 skip_prefix(&pattern, &pattern_len, subject_len, flags);
571 if (pattern_len != subject_len)
573 while (pattern_len) {
574 unsigned char l = *pattern;
575 unsigned char r = *subject;
576 /* The pattern must not contain NUL characters. */
580 if ('A' <= l && l <= 'Z')
582 if ('A' <= r && r <= 'Z')
594 /* Compare using memcmp. */
595 static int equal_case(const unsigned char *pattern, size_t pattern_len,
596 const unsigned char *subject, size_t subject_len,
599 skip_prefix(&pattern, &pattern_len, subject_len, flags);
600 if (pattern_len != subject_len)
602 return !memcmp(pattern, subject, pattern_len);
606 * RFC 5280, section 7.5, requires that only the domain is compared in a
607 * case-insensitive manner.
609 static int equal_email(const unsigned char *a, size_t a_len,
610 const unsigned char *b, size_t b_len,
611 unsigned int unused_flags)
617 * We search backwards for the '@' character, so that we do not have to
618 * deal with quoted local-parts. The domain part is compared in a
619 * case-insensitive manner.
623 if (a[i] == '@' || b[i] == '@') {
624 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
631 return equal_case(a, i, b, i, 0);
635 * Compare the prefix and suffix with the subject, and check that the
636 * characters in-between are valid.
638 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
639 const unsigned char *suffix, size_t suffix_len,
640 const unsigned char *subject, size_t subject_len,
643 const unsigned char *wildcard_start;
644 const unsigned char *wildcard_end;
645 const unsigned char *p;
649 if (subject_len < prefix_len + suffix_len)
651 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
653 wildcard_start = subject + prefix_len;
654 wildcard_end = subject + (subject_len - suffix_len);
655 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
658 * If the wildcard makes up the entire first label, it must match at
659 * least one character.
661 if (prefix_len == 0 && *suffix == '.') {
662 if (wildcard_start == wildcard_end)
665 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
668 /* IDNA labels cannot match partial wildcards */
670 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
672 /* The wildcard may match a literal '*' */
673 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
676 * Check that the part matched by the wildcard contains only
677 * permitted characters and only matches a single label unless
678 * allow_multi is set.
680 for (p = wildcard_start; p != wildcard_end; ++p)
681 if (!(('0' <= *p && *p <= '9') ||
682 ('A' <= *p && *p <= 'Z') ||
683 ('a' <= *p && *p <= 'z') ||
684 *p == '-' || (allow_multi && *p == '.')))
689 #define LABEL_START (1 << 0)
690 #define LABEL_END (1 << 1)
691 #define LABEL_HYPHEN (1 << 2)
692 #define LABEL_IDNA (1 << 3)
694 static const unsigned char *valid_star(const unsigned char *p, size_t len,
697 const unsigned char *star = 0;
699 int state = LABEL_START;
701 for (i = 0; i < len; ++i) {
703 * Locate first and only legal wildcard, either at the start
704 * or end of a non-IDNA first and not final label.
707 int atstart = (state & LABEL_START);
708 int atend = (i == len - 1 || p[i + 1] == '.');
710 * At most one wildcard per pattern.
711 * No wildcards in IDNA labels.
712 * No wildcards after the first label.
714 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
716 /* Only full-label '*.example.com' wildcards? */
717 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
718 && (!atstart || !atend))
720 /* No 'foo*bar' wildcards */
721 if (!atstart && !atend)
724 state &= ~LABEL_START;
725 } else if (('a' <= p[i] && p[i] <= 'z')
726 || ('A' <= p[i] && p[i] <= 'Z')
727 || ('0' <= p[i] && p[i] <= '9')) {
728 if ((state & LABEL_START) != 0
729 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
731 state &= ~(LABEL_HYPHEN | LABEL_START);
732 } else if (p[i] == '.') {
733 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
737 } else if (p[i] == '-') {
738 /* no domain/subdomain starts with '-' */
739 if ((state & LABEL_START) != 0)
741 state |= LABEL_HYPHEN;
747 * The final label must not end in a hyphen or ".", and
748 * there must be at least two dots after the star.
750 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
755 /* Compare using wildcards. */
756 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
757 const unsigned char *subject, size_t subject_len,
760 const unsigned char *star = NULL;
763 * Subject names starting with '.' can only match a wildcard pattern
764 * via a subject sub-domain pattern suffix match.
766 if (!(subject_len > 1 && subject[0] == '.'))
767 star = valid_star(pattern, pattern_len, flags);
769 return equal_nocase(pattern, pattern_len,
770 subject, subject_len, flags);
771 return wildcard_match(pattern, star - pattern,
772 star + 1, (pattern + pattern_len) - star - 1,
773 subject, subject_len, flags);
777 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
778 * cmp_type > 0 only compare if string matches the type, otherwise convert it
782 static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal,
783 unsigned int flags, const char *b, size_t blen,
788 if (!a->data || !a->length)
791 if (cmp_type != a->type)
793 if (cmp_type == V_ASN1_IA5STRING)
794 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
795 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
797 if (rv > 0 && peername)
798 *peername = OPENSSL_strndup((char *)a->data, a->length);
802 astrlen = ASN1_STRING_to_UTF8(&astr, a);
805 * -1 could be an internal malloc failure or a decoding error from
806 * malformed input; we can't distinguish.
810 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
811 if (rv > 0 && peername)
812 *peername = OPENSSL_strndup((char *)astr, astrlen);
818 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
819 unsigned int flags, int check_type, char **peername)
821 GENERAL_NAMES *gens = NULL;
822 X509_NAME *name = NULL;
824 int cnid = NID_undef;
830 /* See below, this flag is internal-only */
831 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
832 if (check_type == GEN_EMAIL) {
833 cnid = NID_pkcs9_emailAddress;
834 alt_type = V_ASN1_IA5STRING;
836 } else if (check_type == GEN_DNS) {
837 cnid = NID_commonName;
838 /* Implicit client-side DNS sub-domain pattern */
839 if (chklen > 1 && chk[0] == '.')
840 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
841 alt_type = V_ASN1_IA5STRING;
842 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
843 equal = equal_nocase;
845 equal = equal_wildcard;
847 alt_type = V_ASN1_OCTET_STRING;
852 chklen = strlen(chk);
854 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
856 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
859 gen = sk_GENERAL_NAME_value(gens, i);
860 if (gen->type != check_type)
863 if (check_type == GEN_EMAIL)
864 cstr = gen->d.rfc822Name;
865 else if (check_type == GEN_DNS)
866 cstr = gen->d.dNSName;
868 cstr = gen->d.iPAddress;
869 /* Positive on success, negative on error! */
870 if ((rv = do_check_string(cstr, alt_type, equal, flags,
871 chk, chklen, peername)) != 0)
874 GENERAL_NAMES_free(gens);
877 if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))
881 /* We're done if CN-ID is not pertinent */
882 if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))
886 name = X509_get_subject_name(x);
887 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
888 const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i);
889 const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne);
891 /* Positive on success, negative on error! */
892 if ((rv = do_check_string(str, -1, equal, flags,
893 chk, chklen, peername)) != 0)
899 int X509_check_host(X509 *x, const char *chk, size_t chklen,
900 unsigned int flags, char **peername)
905 * Embedded NULs are disallowed, except as the last character of a
906 * string of length 2 or more (tolerate caller including terminating
907 * NUL in string length).
910 chklen = strlen(chk);
911 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
913 if (chklen > 1 && chk[chklen - 1] == '\0')
915 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
918 int X509_check_email(X509 *x, const char *chk, size_t chklen,
924 * Embedded NULs are disallowed, except as the last character of a
925 * string of length 2 or more (tolerate caller including terminating
926 * NUL in string length).
929 chklen = strlen((char *)chk);
930 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
932 if (chklen > 1 && chk[chklen - 1] == '\0')
934 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
937 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
942 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
945 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
947 unsigned char ipout[16];
952 iplen = (size_t)a2i_ipadd(ipout, ipasc);
955 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
959 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
963 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
965 unsigned char ipout[16];
966 ASN1_OCTET_STRING *ret;
969 /* If string contains a ':' assume IPv6 */
971 iplen = a2i_ipadd(ipout, ipasc);
976 ret = ASN1_OCTET_STRING_new();
979 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
980 ASN1_OCTET_STRING_free(ret);
986 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
988 ASN1_OCTET_STRING *ret = NULL;
989 unsigned char ipout[32];
990 char *iptmp = NULL, *p;
992 p = strchr(ipasc, '/');
995 iptmp = OPENSSL_strdup(ipasc);
998 p = iptmp + (p - ipasc);
1001 iplen1 = a2i_ipadd(ipout, iptmp);
1006 iplen2 = a2i_ipadd(ipout + iplen1, p);
1008 OPENSSL_free(iptmp);
1011 if (!iplen2 || (iplen1 != iplen2))
1014 ret = ASN1_OCTET_STRING_new();
1017 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1023 OPENSSL_free(iptmp);
1024 ASN1_OCTET_STRING_free(ret);
1028 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1030 /* If string contains a ':' assume IPv6 */
1032 if (strchr(ipasc, ':')) {
1033 if (!ipv6_from_asc(ipout, ipasc))
1037 if (!ipv4_from_asc(ipout, ipasc))
1043 static int ipv4_from_asc(unsigned char *v4, const char *in)
1046 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1048 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1049 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1059 /* Temporary store for IPV6 output */
1060 unsigned char tmp[16];
1061 /* Total number of bytes in tmp */
1063 /* The position of a zero (corresponding to '::') */
1065 /* Number of zeroes */
1069 static int ipv6_from_asc(unsigned char *v6, const char *in)
1073 v6stat.zero_pos = -1;
1074 v6stat.zero_cnt = 0;
1076 * Treat the IPv6 representation as a list of values separated by ':'.
1077 * The presence of a '::' will parse as one, two or three zero length
1080 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1083 /* Now for some sanity checks */
1085 if (v6stat.zero_pos == -1) {
1086 /* If no '::' must have exactly 16 bytes */
1087 if (v6stat.total != 16)
1090 /* If '::' must have less than 16 bytes */
1091 if (v6stat.total == 16)
1093 /* More than three zeroes is an error */
1094 if (v6stat.zero_cnt > 3)
1096 /* Can only have three zeroes if nothing else present */
1097 else if (v6stat.zero_cnt == 3) {
1098 if (v6stat.total > 0)
1101 /* Can only have two zeroes if at start or end */
1102 else if (v6stat.zero_cnt == 2) {
1103 if ((v6stat.zero_pos != 0)
1104 && (v6stat.zero_pos != v6stat.total))
1107 /* Can only have one zero if *not* start or end */
1109 if ((v6stat.zero_pos == 0)
1110 || (v6stat.zero_pos == v6stat.total))
1117 if (v6stat.zero_pos >= 0) {
1118 /* Copy initial part */
1119 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1121 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1122 /* Copy final part */
1123 if (v6stat.total != v6stat.zero_pos)
1124 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1125 v6stat.tmp + v6stat.zero_pos,
1126 v6stat.total - v6stat.zero_pos);
1128 memcpy(v6, v6stat.tmp, 16);
1133 static int ipv6_cb(const char *elem, int len, void *usr)
1136 /* Error if 16 bytes written */
1140 /* Zero length element, corresponds to '::' */
1141 if (s->zero_pos == -1)
1142 s->zero_pos = s->total;
1143 /* If we've already got a :: its an error */
1144 else if (s->zero_pos != s->total)
1148 /* If more than 4 characters could be final a.b.c.d form */
1150 /* Need at least 4 bytes left */
1153 /* Must be end of string */
1156 if (!ipv4_from_asc(s->tmp + s->total, elem))
1160 if (!ipv6_hex(s->tmp + s->total, elem, len))
1169 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1172 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1175 unsigned int num = 0;
1183 x = OPENSSL_hexchar2int(c);
1189 out[1] = num & 0xff;
1193 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1194 unsigned long chtype)
1197 int i, mval, spec_char, plus_char;
1202 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1203 v = sk_CONF_VALUE_value(dn_sk, i);
1206 * Skip past any leading X. X: X, etc to allow for multiple instances
1208 for (p = type; *p; p++) {
1209 #ifndef CHARSET_EBCDIC
1210 spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
1212 spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1213 || (*p == os_toascii['.']));
1222 #ifndef CHARSET_EBCDIC
1223 plus_char = (*type == '+');
1225 plus_char = (*type == os_toascii['+']);
1232 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1233 (unsigned char *)v->value, -1, -1,