2 * Copyright 1999-2016 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 */
14 #include "internal/cryptlib.h"
15 #include <openssl/conf.h>
16 #include <openssl/crypto.h>
17 #include <openssl/x509v3.h>
18 #include "internal/x509_int.h"
19 #include <openssl/bn.h>
22 static char *strip_spaces(char *name);
23 static int sk_strcmp(const char *const *a, const char *const *b);
24 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
26 static void str_free(OPENSSL_STRING str);
27 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email);
29 static int ipv4_from_asc(unsigned char *v4, const char *in);
30 static int ipv6_from_asc(unsigned char *v6, const char *in);
31 static int ipv6_cb(const char *elem, int len, void *usr);
32 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
34 /* Add a CONF_VALUE name value pair to stack */
36 int X509V3_add_value(const char *name, const char *value,
37 STACK_OF(CONF_VALUE) **extlist)
39 CONF_VALUE *vtmp = NULL;
40 char *tname = NULL, *tvalue = NULL;
41 int sk_allocated = (*extlist == NULL);
43 if (name && (tname = OPENSSL_strdup(name)) == NULL)
45 if (value && (tvalue = OPENSSL_strdup(value)) == NULL)
47 if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)
49 if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL)
54 if (!sk_CONF_VALUE_push(*extlist, vtmp))
58 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
60 sk_CONF_VALUE_free(*extlist);
69 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
70 STACK_OF(CONF_VALUE) **extlist)
72 return X509V3_add_value(name, (const char *)value, extlist);
75 /* Free function for STACK_OF(CONF_VALUE) */
77 void X509V3_conf_free(CONF_VALUE *conf)
81 OPENSSL_free(conf->name);
82 OPENSSL_free(conf->value);
83 OPENSSL_free(conf->section);
87 int X509V3_add_value_bool(const char *name, int asn1_bool,
88 STACK_OF(CONF_VALUE) **extlist)
91 return X509V3_add_value(name, "TRUE", extlist);
92 return X509V3_add_value(name, "FALSE", extlist);
95 int X509V3_add_value_bool_nf(const char *name, int asn1_bool,
96 STACK_OF(CONF_VALUE) **extlist)
99 return X509V3_add_value(name, "TRUE", extlist);
103 static char *bignum_to_string(const BIGNUM *bn)
109 * Display large numbers in hex and small numbers in decimal. Converting to
110 * decimal takes quadratic time and is no more useful than hex for large
113 if (BN_num_bits(bn) < 128)
114 return BN_bn2dec(bn);
120 len = strlen(tmp) + 3;
121 ret = OPENSSL_malloc(len);
123 X509V3err(X509V3_F_BIGNUM_TO_STRING, ERR_R_MALLOC_FAILURE);
128 /* Prepend "0x", but place it after the "-" if negative. */
130 OPENSSL_strlcpy(ret, "-0x", len);
131 OPENSSL_strlcat(ret, tmp + 1, len);
133 OPENSSL_strlcpy(ret, "0x", len);
134 OPENSSL_strlcat(ret, tmp, len);
140 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
142 BIGNUM *bntmp = NULL;
147 if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL
148 || (strtmp = bignum_to_string(bntmp)) == NULL)
149 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
154 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)
156 BIGNUM *bntmp = NULL;
161 if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL
162 || (strtmp = bignum_to_string(bntmp)) == NULL)
163 X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
168 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value)
175 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
180 X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
183 if (value[0] == '-') {
189 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
196 ret = BN_hex2bn(&bn, value);
198 ret = BN_dec2bn(&bn, value);
200 if (!ret || value[ret]) {
202 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
206 if (isneg && BN_is_zero(bn))
209 aint = BN_to_ASN1_INTEGER(bn, NULL);
212 X509V3err(X509V3_F_S2I_ASN1_INTEGER,
213 X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
217 aint->type |= V_ASN1_NEG;
221 int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint,
222 STACK_OF(CONF_VALUE) **extlist)
229 if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL)
231 ret = X509V3_add_value(name, strtmp, extlist);
232 OPENSSL_free(strtmp);
236 int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool)
240 if ((btmp = value->value) == NULL)
242 if (strcmp(btmp, "TRUE") == 0
243 || strcmp(btmp, "true") == 0
244 || strcmp(btmp, "Y") == 0
245 || strcmp(btmp, "y") == 0
246 || strcmp(btmp, "YES") == 0
247 || strcmp(btmp, "yes") == 0) {
251 if (strcmp(btmp, "FALSE") == 0
252 || strcmp(btmp, "false") == 0
253 || strcmp(btmp, "N") == 0
254 || strcmp(btmp, "n") == 0
255 || strcmp(btmp, "NO") == 0
256 || strcmp(btmp, "no") == 0) {
261 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
262 X509V3_R_INVALID_BOOLEAN_STRING);
263 X509V3_conf_err(value);
267 int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint)
271 if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) {
272 X509V3_conf_err(value);
286 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
290 STACK_OF(CONF_VALUE) *values = NULL;
293 /* We are going to modify the line so copy it first */
294 linebuf = OPENSSL_strdup(line);
295 if (linebuf == NULL) {
296 X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);
301 /* Go through all characters */
302 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
310 ntmp = strip_spaces(q);
312 X509V3err(X509V3_F_X509V3_PARSE_LIST,
313 X509V3_R_INVALID_NULL_NAME);
317 } else if (c == ',') {
319 ntmp = strip_spaces(q);
322 X509V3err(X509V3_F_X509V3_PARSE_LIST,
323 X509V3_R_INVALID_NULL_NAME);
326 X509V3_add_value(ntmp, NULL, &values);
334 vtmp = strip_spaces(q);
336 X509V3err(X509V3_F_X509V3_PARSE_LIST,
337 X509V3_R_INVALID_NULL_VALUE);
340 X509V3_add_value(ntmp, vtmp, &values);
348 if (state == HDR_VALUE) {
349 vtmp = strip_spaces(q);
351 X509V3err(X509V3_F_X509V3_PARSE_LIST,
352 X509V3_R_INVALID_NULL_VALUE);
355 X509V3_add_value(ntmp, vtmp, &values);
357 ntmp = strip_spaces(q);
359 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
362 X509V3_add_value(ntmp, NULL, &values);
364 OPENSSL_free(linebuf);
368 OPENSSL_free(linebuf);
369 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
374 /* Delete leading and trailing spaces from a string */
375 static char *strip_spaces(char *name)
378 /* Skip over leading spaces */
380 while (*p && isspace((unsigned char)*p))
384 q = p + strlen(p) - 1;
385 while ((q != p) && isspace((unsigned char)*q))
396 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
399 int name_cmp(const char *name, const char *cmp)
404 if ((ret = strncmp(name, cmp, len)))
407 if (!c || (c == '.'))
412 static int sk_strcmp(const char *const *a, const char *const *b)
414 return strcmp(*a, *b);
417 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
420 STACK_OF(OPENSSL_STRING) *ret;
422 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
423 ret = get_email(X509_get_subject_name(x), gens);
424 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
428 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
430 AUTHORITY_INFO_ACCESS *info;
431 STACK_OF(OPENSSL_STRING) *ret = NULL;
434 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
437 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
438 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
439 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
440 if (ad->location->type == GEN_URI) {
442 (&ret, ad->location->d.uniformResourceIdentifier))
447 AUTHORITY_INFO_ACCESS_free(info);
451 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
454 STACK_OF(X509_EXTENSION) *exts;
455 STACK_OF(OPENSSL_STRING) *ret;
457 exts = X509_REQ_get_extensions(x);
458 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
459 ret = get_email(X509_REQ_get_subject_name(x), gens);
460 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
461 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
465 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
468 STACK_OF(OPENSSL_STRING) *ret = NULL;
470 ASN1_IA5STRING *email;
473 /* Now add any email address(es) to STACK */
475 /* First supplied X509_NAME */
476 while ((i = X509_NAME_get_index_by_NID(name,
477 NID_pkcs9_emailAddress, i)) >= 0) {
478 ne = X509_NAME_get_entry(name, i);
479 email = X509_NAME_ENTRY_get_data(ne);
480 if (!append_ia5(&ret, email))
483 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
484 gen = sk_GENERAL_NAME_value(gens, i);
485 if (gen->type != GEN_EMAIL)
487 if (!append_ia5(&ret, gen->d.ia5))
493 static void str_free(OPENSSL_STRING str)
498 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email)
501 /* First some sanity checks */
502 if (email->type != V_ASN1_IA5STRING)
504 if (!email->data || !email->length)
507 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
510 /* Don't add duplicates */
511 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
513 emtmp = OPENSSL_strdup((char *)email->data);
514 if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
515 OPENSSL_free(emtmp); /* free on push failure */
516 X509_email_free(*sk);
523 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
525 sk_OPENSSL_STRING_pop_free(sk, str_free);
528 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
529 const unsigned char *subject, size_t subject_len,
532 /* Skip pattern prefix to match "wildcard" subject */
533 static void skip_prefix(const unsigned char **p, size_t *plen,
537 const unsigned char *pattern = *p;
538 size_t pattern_len = *plen;
541 * If subject starts with a leading '.' followed by more octets, and
542 * pattern is longer, compare just an equal-length suffix with the
543 * full subject (starting at the '.'), provided the prefix contains
546 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
549 while (pattern_len > subject_len && *pattern) {
550 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
557 /* Skip if entire prefix acceptable */
558 if (pattern_len == subject_len) {
564 /* Compare while ASCII ignoring case. */
565 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
566 const unsigned char *subject, size_t subject_len,
569 skip_prefix(&pattern, &pattern_len, subject_len, flags);
570 if (pattern_len != subject_len)
572 while (pattern_len) {
573 unsigned char l = *pattern;
574 unsigned char r = *subject;
575 /* The pattern must not contain NUL characters. */
579 if ('A' <= l && l <= 'Z')
581 if ('A' <= r && r <= 'Z')
593 /* Compare using memcmp. */
594 static int equal_case(const unsigned char *pattern, size_t pattern_len,
595 const unsigned char *subject, size_t subject_len,
598 skip_prefix(&pattern, &pattern_len, subject_len, flags);
599 if (pattern_len != subject_len)
601 return !memcmp(pattern, subject, pattern_len);
605 * RFC 5280, section 7.5, requires that only the domain is compared in a
606 * case-insensitive manner.
608 static int equal_email(const unsigned char *a, size_t a_len,
609 const unsigned char *b, size_t b_len,
610 unsigned int unused_flags)
616 * We search backwards for the '@' character, so that we do not have to
617 * deal with quoted local-parts. The domain part is compared in a
618 * case-insensitive manner.
622 if (a[i] == '@' || b[i] == '@') {
623 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
630 return equal_case(a, i, b, i, 0);
634 * Compare the prefix and suffix with the subject, and check that the
635 * characters in-between are valid.
637 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
638 const unsigned char *suffix, size_t suffix_len,
639 const unsigned char *subject, size_t subject_len,
642 const unsigned char *wildcard_start;
643 const unsigned char *wildcard_end;
644 const unsigned char *p;
648 if (subject_len < prefix_len + suffix_len)
650 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
652 wildcard_start = subject + prefix_len;
653 wildcard_end = subject + (subject_len - suffix_len);
654 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
657 * If the wildcard makes up the entire first label, it must match at
658 * least one character.
660 if (prefix_len == 0 && *suffix == '.') {
661 if (wildcard_start == wildcard_end)
664 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
667 /* IDNA labels cannot match partial wildcards */
669 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
671 /* The wildcard may match a literal '*' */
672 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
675 * Check that the part matched by the wildcard contains only
676 * permitted characters and only matches a single label unless
677 * allow_multi is set.
679 for (p = wildcard_start; p != wildcard_end; ++p)
680 if (!(('0' <= *p && *p <= '9') ||
681 ('A' <= *p && *p <= 'Z') ||
682 ('a' <= *p && *p <= 'z') ||
683 *p == '-' || (allow_multi && *p == '.')))
688 #define LABEL_START (1 << 0)
689 #define LABEL_END (1 << 1)
690 #define LABEL_HYPHEN (1 << 2)
691 #define LABEL_IDNA (1 << 3)
693 static const unsigned char *valid_star(const unsigned char *p, size_t len,
696 const unsigned char *star = 0;
698 int state = LABEL_START;
700 for (i = 0; i < len; ++i) {
702 * Locate first and only legal wildcard, either at the start
703 * or end of a non-IDNA first and not final label.
706 int atstart = (state & LABEL_START);
707 int atend = (i == len - 1 || p[i + 1] == '.');
709 * At most one wildcard per pattern.
710 * No wildcards in IDNA labels.
711 * No wildcards after the first label.
713 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
715 /* Only full-label '*.example.com' wildcards? */
716 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
717 && (!atstart || !atend))
719 /* No 'foo*bar' wildcards */
720 if (!atstart && !atend)
723 state &= ~LABEL_START;
724 } else if (('a' <= p[i] && p[i] <= 'z')
725 || ('A' <= p[i] && p[i] <= 'Z')
726 || ('0' <= p[i] && p[i] <= '9')) {
727 if ((state & LABEL_START) != 0
728 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
730 state &= ~(LABEL_HYPHEN | LABEL_START);
731 } else if (p[i] == '.') {
732 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
736 } else if (p[i] == '-') {
737 /* no domain/subdomain starts with '-' */
738 if ((state & LABEL_START) != 0)
740 state |= LABEL_HYPHEN;
746 * The final label must not end in a hyphen or ".", and
747 * there must be at least two dots after the star.
749 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
754 /* Compare using wildcards. */
755 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
756 const unsigned char *subject, size_t subject_len,
759 const unsigned char *star = NULL;
762 * Subject names starting with '.' can only match a wildcard pattern
763 * via a subject sub-domain pattern suffix match.
765 if (!(subject_len > 1 && subject[0] == '.'))
766 star = valid_star(pattern, pattern_len, flags);
768 return equal_nocase(pattern, pattern_len,
769 subject, subject_len, flags);
770 return wildcard_match(pattern, star - pattern,
771 star + 1, (pattern + pattern_len) - star - 1,
772 subject, subject_len, flags);
776 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
777 * cmp_type > 0 only compare if string matches the type, otherwise convert it
781 static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal,
782 unsigned int flags, const char *b, size_t blen,
787 if (!a->data || !a->length)
790 if (cmp_type != a->type)
792 if (cmp_type == V_ASN1_IA5STRING)
793 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
794 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
796 if (rv > 0 && peername)
797 *peername = OPENSSL_strndup((char *)a->data, a->length);
801 astrlen = ASN1_STRING_to_UTF8(&astr, a);
804 * -1 could be an internal malloc failure or a decoding error from
805 * malformed input; we can't distinguish.
809 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
810 if (rv > 0 && peername)
811 *peername = OPENSSL_strndup((char *)astr, astrlen);
817 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
818 unsigned int flags, int check_type, char **peername)
820 GENERAL_NAMES *gens = NULL;
821 X509_NAME *name = NULL;
823 int cnid = NID_undef;
829 /* See below, this flag is internal-only */
830 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
831 if (check_type == GEN_EMAIL) {
832 cnid = NID_pkcs9_emailAddress;
833 alt_type = V_ASN1_IA5STRING;
835 } else if (check_type == GEN_DNS) {
836 cnid = NID_commonName;
837 /* Implicit client-side DNS sub-domain pattern */
838 if (chklen > 1 && chk[0] == '.')
839 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
840 alt_type = V_ASN1_IA5STRING;
841 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
842 equal = equal_nocase;
844 equal = equal_wildcard;
846 alt_type = V_ASN1_OCTET_STRING;
851 chklen = strlen(chk);
853 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
855 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
858 gen = sk_GENERAL_NAME_value(gens, i);
859 if (gen->type != check_type)
862 if (check_type == GEN_EMAIL)
863 cstr = gen->d.rfc822Name;
864 else if (check_type == GEN_DNS)
865 cstr = gen->d.dNSName;
867 cstr = gen->d.iPAddress;
868 /* Positive on success, negative on error! */
869 if ((rv = do_check_string(cstr, alt_type, equal, flags,
870 chk, chklen, peername)) != 0)
873 GENERAL_NAMES_free(gens);
876 if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))
880 /* We're done if CN-ID is not pertinent */
881 if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))
885 name = X509_get_subject_name(x);
886 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
887 const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i);
888 const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne);
890 /* Positive on success, negative on error! */
891 if ((rv = do_check_string(str, -1, equal, flags,
892 chk, chklen, peername)) != 0)
898 int X509_check_host(X509 *x, const char *chk, size_t chklen,
899 unsigned int flags, char **peername)
904 * Embedded NULs are disallowed, except as the last character of a
905 * string of length 2 or more (tolerate caller including terminating
906 * NUL in string length).
909 chklen = strlen(chk);
910 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
912 if (chklen > 1 && chk[chklen - 1] == '\0')
914 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
917 int X509_check_email(X509 *x, const char *chk, size_t chklen,
923 * Embedded NULs are disallowed, except as the last character of a
924 * string of length 2 or more (tolerate caller including terminating
925 * NUL in string length).
928 chklen = strlen((char *)chk);
929 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
931 if (chklen > 1 && chk[chklen - 1] == '\0')
933 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
936 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
941 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
944 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
946 unsigned char ipout[16];
951 iplen = (size_t)a2i_ipadd(ipout, ipasc);
954 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
958 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
962 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
964 unsigned char ipout[16];
965 ASN1_OCTET_STRING *ret;
968 /* If string contains a ':' assume IPv6 */
970 iplen = a2i_ipadd(ipout, ipasc);
975 ret = ASN1_OCTET_STRING_new();
978 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
979 ASN1_OCTET_STRING_free(ret);
985 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
987 ASN1_OCTET_STRING *ret = NULL;
988 unsigned char ipout[32];
989 char *iptmp = NULL, *p;
991 p = strchr(ipasc, '/');
994 iptmp = OPENSSL_strdup(ipasc);
997 p = iptmp + (p - ipasc);
1000 iplen1 = a2i_ipadd(ipout, iptmp);
1005 iplen2 = a2i_ipadd(ipout + iplen1, p);
1007 OPENSSL_free(iptmp);
1010 if (!iplen2 || (iplen1 != iplen2))
1013 ret = ASN1_OCTET_STRING_new();
1016 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1022 OPENSSL_free(iptmp);
1023 ASN1_OCTET_STRING_free(ret);
1027 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
1029 /* If string contains a ':' assume IPv6 */
1031 if (strchr(ipasc, ':')) {
1032 if (!ipv6_from_asc(ipout, ipasc))
1036 if (!ipv4_from_asc(ipout, ipasc))
1042 static int ipv4_from_asc(unsigned char *v4, const char *in)
1045 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1047 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1048 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1058 /* Temporary store for IPV6 output */
1059 unsigned char tmp[16];
1060 /* Total number of bytes in tmp */
1062 /* The position of a zero (corresponding to '::') */
1064 /* Number of zeroes */
1068 static int ipv6_from_asc(unsigned char *v6, const char *in)
1072 v6stat.zero_pos = -1;
1073 v6stat.zero_cnt = 0;
1075 * Treat the IPv6 representation as a list of values separated by ':'.
1076 * The presence of a '::' will parse as one, two or three zero length
1079 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1082 /* Now for some sanity checks */
1084 if (v6stat.zero_pos == -1) {
1085 /* If no '::' must have exactly 16 bytes */
1086 if (v6stat.total != 16)
1089 /* If '::' must have less than 16 bytes */
1090 if (v6stat.total == 16)
1092 /* More than three zeroes is an error */
1093 if (v6stat.zero_cnt > 3)
1095 /* Can only have three zeroes if nothing else present */
1096 else if (v6stat.zero_cnt == 3) {
1097 if (v6stat.total > 0)
1100 /* Can only have two zeroes if at start or end */
1101 else if (v6stat.zero_cnt == 2) {
1102 if ((v6stat.zero_pos != 0)
1103 && (v6stat.zero_pos != v6stat.total))
1106 /* Can only have one zero if *not* start or end */
1108 if ((v6stat.zero_pos == 0)
1109 || (v6stat.zero_pos == v6stat.total))
1116 if (v6stat.zero_pos >= 0) {
1117 /* Copy initial part */
1118 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1120 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1121 /* Copy final part */
1122 if (v6stat.total != v6stat.zero_pos)
1123 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1124 v6stat.tmp + v6stat.zero_pos,
1125 v6stat.total - v6stat.zero_pos);
1127 memcpy(v6, v6stat.tmp, 16);
1132 static int ipv6_cb(const char *elem, int len, void *usr)
1135 /* Error if 16 bytes written */
1139 /* Zero length element, corresponds to '::' */
1140 if (s->zero_pos == -1)
1141 s->zero_pos = s->total;
1142 /* If we've already got a :: its an error */
1143 else if (s->zero_pos != s->total)
1147 /* If more than 4 characters could be final a.b.c.d form */
1149 /* Need at least 4 bytes left */
1152 /* Must be end of string */
1155 if (!ipv4_from_asc(s->tmp + s->total, elem))
1159 if (!ipv6_hex(s->tmp + s->total, elem, len))
1168 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1171 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1174 unsigned int num = 0;
1182 x = OPENSSL_hexchar2int(c);
1188 out[1] = num & 0xff;
1192 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1193 unsigned long chtype)
1196 int i, mval, spec_char, plus_char;
1201 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1202 v = sk_CONF_VALUE_value(dn_sk, i);
1205 * Skip past any leading X. X: X, etc to allow for multiple instances
1207 for (p = type; *p; p++) {
1208 #ifndef CHARSET_EBCDIC
1209 spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
1211 spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1212 || (*p == os_toascii['.']));
1221 #ifndef CHARSET_EBCDIC
1222 plus_char = (*type == '+');
1224 plus_char = (*type == os_toascii['+']);
1231 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1232 (unsigned char *)v->value, -1, -1,