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/x509v3.h>
17 #include "internal/x509_int.h"
18 #include <openssl/bn.h>
21 static char *strip_spaces(char *name);
22 static int sk_strcmp(const char *const *a, const char *const *b);
23 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
25 static void str_free(OPENSSL_STRING str);
26 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email);
28 static int ipv4_from_asc(unsigned char *v4, const char *in);
29 static int ipv6_from_asc(unsigned char *v6, const char *in);
30 static int ipv6_cb(const char *elem, int len, void *usr);
31 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
33 /* Add a CONF_VALUE name value pair to stack */
35 int X509V3_add_value(const char *name, const char *value,
36 STACK_OF(CONF_VALUE) **extlist)
38 CONF_VALUE *vtmp = NULL;
39 char *tname = NULL, *tvalue = NULL;
41 if (name && (tname = OPENSSL_strdup(name)) == NULL)
43 if (value && (tvalue = OPENSSL_strdup(value)) == NULL)
45 if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)
47 if (*extlist == NULL && (*extlist = sk_CONF_VALUE_new_null()) == NULL)
52 if (!sk_CONF_VALUE_push(*extlist, vtmp))
56 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
63 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
64 STACK_OF(CONF_VALUE) **extlist)
66 return X509V3_add_value(name, (const char *)value, extlist);
69 /* Free function for STACK_OF(CONF_VALUE) */
71 void X509V3_conf_free(CONF_VALUE *conf)
75 OPENSSL_free(conf->name);
76 OPENSSL_free(conf->value);
77 OPENSSL_free(conf->section);
81 int X509V3_add_value_bool(const char *name, int asn1_bool,
82 STACK_OF(CONF_VALUE) **extlist)
85 return X509V3_add_value(name, "TRUE", extlist);
86 return X509V3_add_value(name, "FALSE", extlist);
89 int X509V3_add_value_bool_nf(const char *name, int asn1_bool,
90 STACK_OF(CONF_VALUE) **extlist)
93 return X509V3_add_value(name, "TRUE", extlist);
97 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
104 if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL
105 || (strtmp = BN_bn2dec(bntmp)) == NULL)
106 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
111 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)
113 BIGNUM *bntmp = NULL;
118 if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL
119 || (strtmp = BN_bn2dec(bntmp)) == NULL)
120 X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
125 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value)
132 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
137 X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
140 if (value[0] == '-') {
146 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
153 ret = BN_hex2bn(&bn, value);
155 ret = BN_dec2bn(&bn, value);
157 if (!ret || value[ret]) {
159 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
163 if (isneg && BN_is_zero(bn))
166 aint = BN_to_ASN1_INTEGER(bn, NULL);
169 X509V3err(X509V3_F_S2I_ASN1_INTEGER,
170 X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
174 aint->type |= V_ASN1_NEG;
178 int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint,
179 STACK_OF(CONF_VALUE) **extlist)
186 if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL)
188 ret = X509V3_add_value(name, strtmp, extlist);
189 OPENSSL_free(strtmp);
193 int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool)
197 if ((btmp = value->value) == NULL)
199 if (strcmp(btmp, "TRUE") == 0
200 || strcmp(btmp, "true") == 0
201 || strcmp(btmp, "Y") == 0
202 || strcmp(btmp, "y") == 0
203 || strcmp(btmp, "YES") == 0
204 || strcmp(btmp, "yes") == 0) {
208 if (strcmp(btmp, "FALSE") == 0
209 || strcmp(btmp, "false") == 0
210 || strcmp(btmp, "N") == 0
211 || strcmp(btmp, "n") == 0
212 || strcmp(btmp, "NO") == 0
213 || strcmp(btmp, "no") == 0) {
218 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
219 X509V3_R_INVALID_BOOLEAN_STRING);
220 X509V3_conf_err(value);
224 int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint)
228 if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) {
229 X509V3_conf_err(value);
243 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
247 STACK_OF(CONF_VALUE) *values = NULL;
250 /* We are going to modify the line so copy it first */
251 linebuf = OPENSSL_strdup(line);
252 if (linebuf == NULL) {
253 X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);
258 /* Go through all characters */
259 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
267 ntmp = strip_spaces(q);
269 X509V3err(X509V3_F_X509V3_PARSE_LIST,
270 X509V3_R_INVALID_NULL_NAME);
274 } else if (c == ',') {
276 ntmp = strip_spaces(q);
279 X509V3err(X509V3_F_X509V3_PARSE_LIST,
280 X509V3_R_INVALID_NULL_NAME);
283 X509V3_add_value(ntmp, NULL, &values);
291 vtmp = strip_spaces(q);
293 X509V3err(X509V3_F_X509V3_PARSE_LIST,
294 X509V3_R_INVALID_NULL_VALUE);
297 X509V3_add_value(ntmp, vtmp, &values);
305 if (state == HDR_VALUE) {
306 vtmp = strip_spaces(q);
308 X509V3err(X509V3_F_X509V3_PARSE_LIST,
309 X509V3_R_INVALID_NULL_VALUE);
312 X509V3_add_value(ntmp, vtmp, &values);
314 ntmp = strip_spaces(q);
316 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
319 X509V3_add_value(ntmp, NULL, &values);
321 OPENSSL_free(linebuf);
325 OPENSSL_free(linebuf);
326 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
331 /* Delete leading and trailing spaces from a string */
332 static char *strip_spaces(char *name)
335 /* Skip over leading spaces */
337 while (*p && isspace((unsigned char)*p))
341 q = p + strlen(p) - 1;
342 while ((q != p) && isspace((unsigned char)*q))
353 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
356 int name_cmp(const char *name, const char *cmp)
361 if ((ret = strncmp(name, cmp, len)))
364 if (!c || (c == '.'))
369 static int sk_strcmp(const char *const *a, const char *const *b)
371 return strcmp(*a, *b);
374 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
377 STACK_OF(OPENSSL_STRING) *ret;
379 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
380 ret = get_email(X509_get_subject_name(x), gens);
381 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
385 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
387 AUTHORITY_INFO_ACCESS *info;
388 STACK_OF(OPENSSL_STRING) *ret = NULL;
391 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
394 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
395 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
396 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
397 if (ad->location->type == GEN_URI) {
399 (&ret, ad->location->d.uniformResourceIdentifier))
404 AUTHORITY_INFO_ACCESS_free(info);
408 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
411 STACK_OF(X509_EXTENSION) *exts;
412 STACK_OF(OPENSSL_STRING) *ret;
414 exts = X509_REQ_get_extensions(x);
415 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
416 ret = get_email(X509_REQ_get_subject_name(x), gens);
417 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
418 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
422 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
425 STACK_OF(OPENSSL_STRING) *ret = NULL;
427 const ASN1_IA5STRING *email;
431 /* Now add any email address(es) to STACK */
432 /* First supplied X509_NAME */
433 while ((i = X509_NAME_get_index_by_NID(name,
434 NID_pkcs9_emailAddress, i)) >= 0) {
435 ne = X509_NAME_get_entry(name, i);
436 email = X509_NAME_ENTRY_get_data(ne);
437 if (!append_ia5(&ret, email))
440 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
441 gen = sk_GENERAL_NAME_value(gens, i);
442 if (gen->type != GEN_EMAIL)
444 if (!append_ia5(&ret, gen->d.ia5))
450 static void str_free(OPENSSL_STRING str)
455 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email)
458 /* First some sanity checks */
459 if (email->type != V_ASN1_IA5STRING)
461 if (!email->data || !email->length)
464 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
467 /* Don't add duplicates */
468 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
470 emtmp = OPENSSL_strdup((char *)email->data);
471 if (emtmp == NULL || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
472 OPENSSL_free(emtmp); /* free on push failure */
473 X509_email_free(*sk);
480 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
482 sk_OPENSSL_STRING_pop_free(sk, str_free);
485 typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
486 const unsigned char *subject, size_t subject_len,
489 /* Skip pattern prefix to match "wildcard" subject */
490 static void skip_prefix(const unsigned char **p, size_t *plen,
494 const unsigned char *pattern = *p;
495 size_t pattern_len = *plen;
498 * If subject starts with a leading '.' followed by more octets, and
499 * pattern is longer, compare just an equal-length suffix with the
500 * full subject (starting at the '.'), provided the prefix contains
503 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
506 while (pattern_len > subject_len && *pattern) {
507 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
514 /* Skip if entire prefix acceptable */
515 if (pattern_len == subject_len) {
521 /* Compare while ASCII ignoring case. */
522 static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
523 const unsigned char *subject, size_t subject_len,
526 skip_prefix(&pattern, &pattern_len, subject_len, flags);
527 if (pattern_len != subject_len)
529 while (pattern_len) {
530 unsigned char l = *pattern;
531 unsigned char r = *subject;
532 /* The pattern must not contain NUL characters. */
536 if ('A' <= l && l <= 'Z')
538 if ('A' <= r && r <= 'Z')
550 /* Compare using memcmp. */
551 static int equal_case(const unsigned char *pattern, size_t pattern_len,
552 const unsigned char *subject, size_t subject_len,
555 skip_prefix(&pattern, &pattern_len, subject_len, flags);
556 if (pattern_len != subject_len)
558 return !memcmp(pattern, subject, pattern_len);
562 * RFC 5280, section 7.5, requires that only the domain is compared in a
563 * case-insensitive manner.
565 static int equal_email(const unsigned char *a, size_t a_len,
566 const unsigned char *b, size_t b_len,
567 unsigned int unused_flags)
573 * We search backwards for the '@' character, so that we do not have to
574 * deal with quoted local-parts. The domain part is compared in a
575 * case-insensitive manner.
579 if (a[i] == '@' || b[i] == '@') {
580 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
587 return equal_case(a, i, b, i, 0);
591 * Compare the prefix and suffix with the subject, and check that the
592 * characters in-between are valid.
594 static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
595 const unsigned char *suffix, size_t suffix_len,
596 const unsigned char *subject, size_t subject_len,
599 const unsigned char *wildcard_start;
600 const unsigned char *wildcard_end;
601 const unsigned char *p;
605 if (subject_len < prefix_len + suffix_len)
607 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
609 wildcard_start = subject + prefix_len;
610 wildcard_end = subject + (subject_len - suffix_len);
611 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
614 * If the wildcard makes up the entire first label, it must match at
615 * least one character.
617 if (prefix_len == 0 && *suffix == '.') {
618 if (wildcard_start == wildcard_end)
621 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
624 /* IDNA labels cannot match partial wildcards */
626 subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0)
628 /* The wildcard may match a literal '*' */
629 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
632 * Check that the part matched by the wildcard contains only
633 * permitted characters and only matches a single label unless
634 * allow_multi is set.
636 for (p = wildcard_start; p != wildcard_end; ++p)
637 if (!(('0' <= *p && *p <= '9') ||
638 ('A' <= *p && *p <= 'Z') ||
639 ('a' <= *p && *p <= 'z') ||
640 *p == '-' || (allow_multi && *p == '.')))
645 #define LABEL_START (1 << 0)
646 #define LABEL_END (1 << 1)
647 #define LABEL_HYPHEN (1 << 2)
648 #define LABEL_IDNA (1 << 3)
650 static const unsigned char *valid_star(const unsigned char *p, size_t len,
653 const unsigned char *star = 0;
655 int state = LABEL_START;
657 for (i = 0; i < len; ++i) {
659 * Locate first and only legal wildcard, either at the start
660 * or end of a non-IDNA first and not final label.
663 int atstart = (state & LABEL_START);
664 int atend = (i == len - 1 || p[i + 1] == '.');
666 * At most one wildcard per pattern.
667 * No wildcards in IDNA labels.
668 * No wildcards after the first label.
670 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
672 /* Only full-label '*.example.com' wildcards? */
673 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
674 && (!atstart || !atend))
676 /* No 'foo*bar' wildcards */
677 if (!atstart && !atend)
680 state &= ~LABEL_START;
681 } else if (('a' <= p[i] && p[i] <= 'z')
682 || ('A' <= p[i] && p[i] <= 'Z')
683 || ('0' <= p[i] && p[i] <= '9')) {
684 if ((state & LABEL_START) != 0
685 && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0)
687 state &= ~(LABEL_HYPHEN | LABEL_START);
688 } else if (p[i] == '.') {
689 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
693 } else if (p[i] == '-') {
694 /* no domain/subdomain starts with '-' */
695 if ((state & LABEL_START) != 0)
697 state |= LABEL_HYPHEN;
703 * The final label must not end in a hyphen or ".", and
704 * there must be at least two dots after the star.
706 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
711 /* Compare using wildcards. */
712 static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
713 const unsigned char *subject, size_t subject_len,
716 const unsigned char *star = NULL;
719 * Subject names starting with '.' can only match a wildcard pattern
720 * via a subject sub-domain pattern suffix match.
722 if (!(subject_len > 1 && subject[0] == '.'))
723 star = valid_star(pattern, pattern_len, flags);
725 return equal_nocase(pattern, pattern_len,
726 subject, subject_len, flags);
727 return wildcard_match(pattern, star - pattern,
728 star + 1, (pattern + pattern_len) - star - 1,
729 subject, subject_len, flags);
733 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
734 * cmp_type > 0 only compare if string matches the type, otherwise convert it
738 static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal,
739 unsigned int flags, const char *b, size_t blen,
744 if (!a->data || !a->length)
747 if (cmp_type != a->type)
749 if (cmp_type == V_ASN1_IA5STRING)
750 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
751 else if (a->length == (int)blen && !memcmp(a->data, b, blen))
753 if (rv > 0 && peername)
754 *peername = OPENSSL_strndup((char *)a->data, a->length);
758 astrlen = ASN1_STRING_to_UTF8(&astr, a);
761 * -1 could be an internal malloc failure or a decoding error from
762 * malformed input; we can't distinguish.
766 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
767 if (rv > 0 && peername)
768 *peername = OPENSSL_strndup((char *)astr, astrlen);
774 static int do_x509_check(X509 *x, const char *chk, size_t chklen,
775 unsigned int flags, int check_type, char **peername)
777 GENERAL_NAMES *gens = NULL;
778 X509_NAME *name = NULL;
780 int cnid = NID_undef;
786 /* See below, this flag is internal-only */
787 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
788 if (check_type == GEN_EMAIL) {
789 cnid = NID_pkcs9_emailAddress;
790 alt_type = V_ASN1_IA5STRING;
792 } else if (check_type == GEN_DNS) {
793 cnid = NID_commonName;
794 /* Implicit client-side DNS sub-domain pattern */
795 if (chklen > 1 && chk[0] == '.')
796 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
797 alt_type = V_ASN1_IA5STRING;
798 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
799 equal = equal_nocase;
801 equal = equal_wildcard;
803 alt_type = V_ASN1_OCTET_STRING;
808 chklen = strlen(chk);
810 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
812 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
815 gen = sk_GENERAL_NAME_value(gens, i);
816 if (gen->type != check_type)
819 if (check_type == GEN_EMAIL)
820 cstr = gen->d.rfc822Name;
821 else if (check_type == GEN_DNS)
822 cstr = gen->d.dNSName;
824 cstr = gen->d.iPAddress;
825 /* Positive on success, negative on error! */
826 if ((rv = do_check_string(cstr, alt_type, equal, flags,
827 chk, chklen, peername)) != 0)
830 GENERAL_NAMES_free(gens);
833 if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))
837 /* We're done if CN-ID is not pertinent */
838 if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))
842 name = X509_get_subject_name(x);
843 while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
844 const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i);
845 const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne);
847 /* Positive on success, negative on error! */
848 if ((rv = do_check_string(str, -1, equal, flags,
849 chk, chklen, peername)) != 0)
855 int X509_check_host(X509 *x, const char *chk, size_t chklen,
856 unsigned int flags, char **peername)
861 * Embedded NULs are disallowed, except as the last character of a
862 * string of length 2 or more (tolerate caller including terminating
863 * NUL in string length).
866 chklen = strlen(chk);
867 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
869 if (chklen > 1 && chk[chklen - 1] == '\0')
871 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
874 int X509_check_email(X509 *x, const char *chk, size_t chklen,
880 * Embedded NULs are disallowed, except as the last character of a
881 * string of length 2 or more (tolerate caller including terminating
882 * NUL in string length).
885 chklen = strlen((char *)chk);
886 else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))
888 if (chklen > 1 && chk[chklen - 1] == '\0')
890 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
893 int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
898 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
901 int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
903 unsigned char ipout[16];
908 iplen = (size_t)a2i_ipadd(ipout, ipasc);
911 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
915 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
919 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
921 unsigned char ipout[16];
922 ASN1_OCTET_STRING *ret;
925 /* If string contains a ':' assume IPv6 */
927 iplen = a2i_ipadd(ipout, ipasc);
932 ret = ASN1_OCTET_STRING_new();
935 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
936 ASN1_OCTET_STRING_free(ret);
942 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
944 ASN1_OCTET_STRING *ret = NULL;
945 unsigned char ipout[32];
946 char *iptmp = NULL, *p;
948 p = strchr(ipasc, '/');
951 iptmp = OPENSSL_strdup(ipasc);
954 p = iptmp + (p - ipasc);
957 iplen1 = a2i_ipadd(ipout, iptmp);
962 iplen2 = a2i_ipadd(ipout + iplen1, p);
967 if (!iplen2 || (iplen1 != iplen2))
970 ret = ASN1_OCTET_STRING_new();
973 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
980 ASN1_OCTET_STRING_free(ret);
984 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
986 /* If string contains a ':' assume IPv6 */
988 if (strchr(ipasc, ':')) {
989 if (!ipv6_from_asc(ipout, ipasc))
993 if (!ipv4_from_asc(ipout, ipasc))
999 static int ipv4_from_asc(unsigned char *v4, const char *in)
1002 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1004 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1005 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1015 /* Temporary store for IPV6 output */
1016 unsigned char tmp[16];
1017 /* Total number of bytes in tmp */
1019 /* The position of a zero (corresponding to '::') */
1021 /* Number of zeroes */
1025 static int ipv6_from_asc(unsigned char *v6, const char *in)
1029 v6stat.zero_pos = -1;
1030 v6stat.zero_cnt = 0;
1032 * Treat the IPv6 representation as a list of values separated by ':'.
1033 * The presence of a '::' will parse as one, two or three zero length
1036 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1039 /* Now for some sanity checks */
1041 if (v6stat.zero_pos == -1) {
1042 /* If no '::' must have exactly 16 bytes */
1043 if (v6stat.total != 16)
1046 /* If '::' must have less than 16 bytes */
1047 if (v6stat.total == 16)
1049 /* More than three zeroes is an error */
1050 if (v6stat.zero_cnt > 3)
1052 /* Can only have three zeroes if nothing else present */
1053 else if (v6stat.zero_cnt == 3) {
1054 if (v6stat.total > 0)
1057 /* Can only have two zeroes if at start or end */
1058 else if (v6stat.zero_cnt == 2) {
1059 if ((v6stat.zero_pos != 0)
1060 && (v6stat.zero_pos != v6stat.total))
1063 /* Can only have one zero if *not* start or end */
1065 if ((v6stat.zero_pos == 0)
1066 || (v6stat.zero_pos == v6stat.total))
1073 if (v6stat.zero_pos >= 0) {
1074 /* Copy initial part */
1075 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
1077 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
1078 /* Copy final part */
1079 if (v6stat.total != v6stat.zero_pos)
1080 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1081 v6stat.tmp + v6stat.zero_pos,
1082 v6stat.total - v6stat.zero_pos);
1084 memcpy(v6, v6stat.tmp, 16);
1089 static int ipv6_cb(const char *elem, int len, void *usr)
1092 /* Error if 16 bytes written */
1096 /* Zero length element, corresponds to '::' */
1097 if (s->zero_pos == -1)
1098 s->zero_pos = s->total;
1099 /* If we've already got a :: its an error */
1100 else if (s->zero_pos != s->total)
1104 /* If more than 4 characters could be final a.b.c.d form */
1106 /* Need at least 4 bytes left */
1109 /* Must be end of string */
1112 if (!ipv4_from_asc(s->tmp + s->total, elem))
1116 if (!ipv6_hex(s->tmp + s->total, elem, len))
1125 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1128 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
1131 unsigned int num = 0;
1139 x = OPENSSL_hexchar2int(c);
1145 out[1] = num & 0xff;
1149 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
1150 unsigned long chtype)
1153 int i, mval, spec_char, plus_char;
1158 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1159 v = sk_CONF_VALUE_value(dn_sk, i);
1162 * Skip past any leading X. X: X, etc to allow for multiple instances
1164 for (p = type; *p; p++) {
1165 #ifndef CHARSET_EBCDIC
1166 spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));
1168 spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])
1169 || (*p == os_toascii['.']));
1178 #ifndef CHARSET_EBCDIC
1179 plus_char = (*type == '+');
1181 plus_char = (*type == os_toascii['+']);
1188 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1189 (unsigned char *)v->value, -1, -1,