1 /* $OpenBSD: v3_utl.c,v 1.31 2018/05/19 10:50:08 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 /* X509 v3 extension utilities */
64 #include <openssl/bn.h>
65 #include <openssl/conf.h>
66 #include <openssl/err.h>
67 #include <openssl/x509v3.h>
69 static char *strip_spaces(char *name
);
70 static int sk_strcmp(const char * const *a
, const char * const *b
);
71 static STACK_OF(OPENSSL_STRING
) *get_email(X509_NAME
*name
,
73 static void str_free(OPENSSL_STRING str
);
74 static int append_ia5(STACK_OF(OPENSSL_STRING
) **sk
, ASN1_IA5STRING
*email
);
76 static int ipv4_from_asc(unsigned char *v4
, const char *in
);
77 static int ipv6_from_asc(unsigned char *v6
, const char *in
);
78 static int ipv6_cb(const char *elem
, int len
, void *usr
);
79 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
);
81 /* Add a CONF_VALUE name value pair to stack */
84 X509V3_add_value(const char *name
, const char *value
,
85 STACK_OF(CONF_VALUE
) **extlist
)
87 CONF_VALUE
*vtmp
= NULL
;
88 char *tname
= NULL
, *tvalue
= NULL
;
90 if (name
&& !(tname
= strdup(name
)))
92 if (value
&& !(tvalue
= strdup(value
)))
94 if (!(vtmp
= malloc(sizeof(CONF_VALUE
))))
96 if (!*extlist
&& !(*extlist
= sk_CONF_VALUE_new_null()))
100 vtmp
->value
= tvalue
;
101 if (!sk_CONF_VALUE_push(*extlist
, vtmp
))
106 X509V3error(ERR_R_MALLOC_FAILURE
);
114 X509V3_add_value_uchar(const char *name
, const unsigned char *value
,
115 STACK_OF(CONF_VALUE
) **extlist
)
117 return X509V3_add_value(name
, (const char *)value
, extlist
);
120 /* Free function for STACK_OF(CONF_VALUE) */
123 X509V3_conf_free(CONF_VALUE
*conf
)
134 X509V3_add_value_bool(const char *name
, int asn1_bool
,
135 STACK_OF(CONF_VALUE
) **extlist
)
138 return X509V3_add_value(name
, "TRUE", extlist
);
139 return X509V3_add_value(name
, "FALSE", extlist
);
143 X509V3_add_value_bool_nf(const char *name
, int asn1_bool
,
144 STACK_OF(CONF_VALUE
) **extlist
)
147 return X509V3_add_value(name
, "TRUE", extlist
);
153 i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, const ASN1_ENUMERATED
*a
)
155 BIGNUM
*bntmp
= NULL
;
160 if (!(bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) ||
161 !(strtmp
= BN_bn2dec(bntmp
)))
162 X509V3error(ERR_R_MALLOC_FAILURE
);
168 i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const ASN1_INTEGER
*a
)
170 BIGNUM
*bntmp
= NULL
;
175 if (!(bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) ||
176 !(strtmp
= BN_bn2dec(bntmp
)))
177 X509V3error(ERR_R_MALLOC_FAILURE
);
183 s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const char *value
)
191 X509V3error(X509V3_R_INVALID_NULL_VALUE
);
195 if (value
[0] == '-') {
201 if (value
[0] == '0' && ((value
[1] == 'x') || (value
[1] == 'X'))) {
208 ret
= BN_hex2bn(&bn
, value
);
210 ret
= BN_dec2bn(&bn
, value
);
212 if (!ret
|| value
[ret
]) {
214 X509V3error(X509V3_R_BN_DEC2BN_ERROR
);
218 if (isneg
&& BN_is_zero(bn
))
221 aint
= BN_to_ASN1_INTEGER(bn
, NULL
);
224 X509V3error(X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
228 aint
->type
|= V_ASN1_NEG
;
233 X509V3_add_value_int(const char *name
, const ASN1_INTEGER
*aint
,
234 STACK_OF(CONF_VALUE
) **extlist
)
241 if (!(strtmp
= i2s_ASN1_INTEGER(NULL
, aint
)))
243 ret
= X509V3_add_value(name
, strtmp
, extlist
);
249 X509V3_get_value_bool(const CONF_VALUE
*value
, int *asn1_bool
)
253 if (!(btmp
= value
->value
))
255 if (!strcmp(btmp
, "TRUE") || !strcmp(btmp
, "true") ||
256 !strcmp(btmp
, "Y") || !strcmp(btmp
, "y") ||
257 !strcmp(btmp
, "YES") || !strcmp(btmp
, "yes")) {
260 } else if (!strcmp(btmp
, "FALSE") || !strcmp(btmp
, "false") ||
261 !strcmp(btmp
, "N") || !strcmp(btmp
, "n") ||
262 !strcmp(btmp
, "NO") || !strcmp(btmp
, "no")) {
268 X509V3error(X509V3_R_INVALID_BOOLEAN_STRING
);
269 X509V3_conf_err(value
);
274 X509V3_get_value_int(const CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
278 if (!(itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
))) {
279 X509V3_conf_err(value
);
291 STACK_OF(CONF_VALUE
) *
292 X509V3_parse_list(const char *line
)
296 STACK_OF(CONF_VALUE
) *values
= NULL
;
300 /* We are going to modify the line so copy it first */
301 if ((linebuf
= strdup(line
)) == NULL
) {
302 X509V3error(ERR_R_MALLOC_FAILURE
);
308 /* Go through all characters */
309 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') &&
317 ntmp
= strip_spaces(q
);
319 X509V3error(X509V3_R_INVALID_NULL_NAME
);
323 } else if (c
== ',') {
325 ntmp
= strip_spaces(q
);
328 X509V3error(X509V3_R_INVALID_NULL_NAME
);
331 X509V3_add_value(ntmp
, NULL
, &values
);
339 vtmp
= strip_spaces(q
);
341 X509V3error(X509V3_R_INVALID_NULL_VALUE
);
344 X509V3_add_value(ntmp
, vtmp
, &values
);
352 if (state
== HDR_VALUE
) {
353 vtmp
= strip_spaces(q
);
355 X509V3error(X509V3_R_INVALID_NULL_VALUE
);
358 X509V3_add_value(ntmp
, vtmp
, &values
);
360 ntmp
= strip_spaces(q
);
362 X509V3error(X509V3_R_INVALID_NULL_NAME
);
365 X509V3_add_value(ntmp
, NULL
, &values
);
372 sk_CONF_VALUE_pop_free(values
, X509V3_conf_free
);
377 /* Delete leading and trailing spaces from a string */
379 strip_spaces(char *name
)
383 /* Skip over leading spaces */
385 while (*p
&& isspace((unsigned char)*p
))
389 q
= p
+ strlen(p
) - 1;
390 while ((q
!= p
) && isspace((unsigned char)*q
))
399 /* hex string utilities */
401 /* Given a buffer of length 'len' return a malloc'ed string with its
405 hex_to_string(const unsigned char *buffer
, long len
)
408 const unsigned char *p
;
410 static const char hexdig
[] = "0123456789ABCDEF";
414 if (!(tmp
= malloc(len
* 3 + 1))) {
415 X509V3error(ERR_R_MALLOC_FAILURE
);
419 for (i
= 0, p
= buffer
; i
< len
; i
++, p
++) {
420 *q
++ = hexdig
[(*p
>> 4) & 0xf];
421 *q
++ = hexdig
[*p
& 0xf];
428 /* Give a string of hex digits convert to
433 string_to_hex(const char *str
, long *len
)
435 unsigned char *hexbuf
, *q
;
436 unsigned char ch
, cl
, *p
;
438 X509V3error(X509V3_R_INVALID_NULL_ARGUMENT
);
441 if (!(hexbuf
= malloc(strlen(str
) >> 1)))
443 for (p
= (unsigned char *)str
, q
= hexbuf
; *p
; ) {
449 X509V3error(X509V3_R_ODD_NUMBER_OF_DIGITS
);
456 if ((ch
>= '0') && (ch
<= '9'))
458 else if ((ch
>= 'a') && (ch
<= 'f'))
463 if ((cl
>= '0') && (cl
<= '9'))
465 else if ((cl
>= 'a') && (cl
<= 'f'))
470 *q
++ = (ch
<< 4) | cl
;
480 X509V3error(ERR_R_MALLOC_FAILURE
);
485 X509V3error(X509V3_R_ILLEGAL_HEX_DIGIT
);
489 /* V2I name comparison function: returns zero if 'name' matches
494 name_cmp(const char *name
, const char *cmp
)
500 if ((ret
= strncmp(name
, cmp
, len
)))
509 sk_strcmp(const char * const *a
, const char * const *b
)
511 return strcmp(*a
, *b
);
514 STACK_OF(OPENSSL_STRING
) *X509_get1_email(X509
*x
)
517 STACK_OF(OPENSSL_STRING
) *ret
;
519 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
520 ret
= get_email(X509_get_subject_name(x
), gens
);
521 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
525 STACK_OF(OPENSSL_STRING
) *X509_get1_ocsp(X509
*x
)
527 AUTHORITY_INFO_ACCESS
*info
;
528 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
531 info
= X509_get_ext_d2i(x
, NID_info_access
, NULL
, NULL
);
534 for (i
= 0; i
< sk_ACCESS_DESCRIPTION_num(info
); i
++) {
535 ACCESS_DESCRIPTION
*ad
= sk_ACCESS_DESCRIPTION_value(info
, i
);
536 if (OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
) {
537 if (ad
->location
->type
== GEN_URI
) {
538 if (!append_ia5(&ret
,
539 ad
->location
->d
.uniformResourceIdentifier
))
544 AUTHORITY_INFO_ACCESS_free(info
);
548 STACK_OF(OPENSSL_STRING
) *X509_REQ_get1_email(X509_REQ
*x
)
551 STACK_OF(X509_EXTENSION
) *exts
;
552 STACK_OF(OPENSSL_STRING
) *ret
;
554 exts
= X509_REQ_get_extensions(x
);
555 gens
= X509V3_get_d2i(exts
, NID_subject_alt_name
, NULL
, NULL
);
556 ret
= get_email(X509_REQ_get_subject_name(x
), gens
);
557 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
558 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
564 STACK_OF(OPENSSL_STRING
) *get_email(X509_NAME
*name
, GENERAL_NAMES
*gens
)
566 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
568 ASN1_IA5STRING
*email
;
572 /* Now add any email address(es) to STACK */
575 /* First supplied X509_NAME */
576 while ((i
= X509_NAME_get_index_by_NID(name
,
577 NID_pkcs9_emailAddress
, i
)) >= 0) {
578 ne
= X509_NAME_get_entry(name
, i
);
579 email
= X509_NAME_ENTRY_get_data(ne
);
580 if (!append_ia5(&ret
, email
))
583 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
584 gen
= sk_GENERAL_NAME_value(gens
, i
);
585 if (gen
->type
!= GEN_EMAIL
)
587 if (!append_ia5(&ret
, gen
->d
.ia5
))
594 str_free(OPENSSL_STRING str
)
600 append_ia5(STACK_OF(OPENSSL_STRING
) **sk
, ASN1_IA5STRING
*email
)
604 /* First some sanity checks */
605 if (email
->type
!= V_ASN1_IA5STRING
)
607 if (!email
->data
|| !email
->length
)
610 *sk
= sk_OPENSSL_STRING_new(sk_strcmp
);
613 /* Don't add duplicates */
614 if (sk_OPENSSL_STRING_find(*sk
, (char *)email
->data
) != -1)
616 emtmp
= strdup((char *)email
->data
);
617 if (!emtmp
|| !sk_OPENSSL_STRING_push(*sk
, emtmp
)) {
618 X509_email_free(*sk
);
626 X509_email_free(STACK_OF(OPENSSL_STRING
) *sk
)
628 sk_OPENSSL_STRING_pop_free(sk
, str_free
);
631 typedef int (*equal_fn
) (const unsigned char *pattern
, size_t pattern_len
,
632 const unsigned char *subject
, size_t subject_len
, unsigned int flags
);
634 /* Skip pattern prefix to match "wildcard" subject */
635 static void skip_prefix(const unsigned char **p
, size_t *plen
,
636 const unsigned char *subject
, size_t subject_len
, unsigned int flags
)
638 const unsigned char *pattern
= *p
;
639 size_t pattern_len
= *plen
;
642 * If subject starts with a leading '.' followed by more octets, and
643 * pattern is longer, compare just an equal-length suffix with the
644 * full subject (starting at the '.'), provided the prefix contains
647 if ((flags
& _X509_CHECK_FLAG_DOT_SUBDOMAINS
) == 0)
650 while (pattern_len
> subject_len
&& *pattern
) {
651 if ((flags
& X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
) &&
658 /* Skip if entire prefix acceptable */
659 if (pattern_len
== subject_len
) {
666 * Open/BoringSSL uses memcmp for "equal_case" while their
667 * "equal_nocase" function is a hand-rolled strncasecmp that does not
668 * allow \0 in the pattern. Since an embedded \0 is likely a sign of
669 * problems, we simply don't allow it in either case, and then we use
670 * standard libc funcitons.
673 /* Compare using strncasecmp */
674 static int equal_nocase(const unsigned char *pattern
, size_t pattern_len
,
675 const unsigned char *subject
, size_t subject_len
,
678 if (memchr(pattern
, '\0', pattern_len
) != NULL
)
680 if (memchr(subject
, '\0', subject_len
) != NULL
)
682 skip_prefix(&pattern
, &pattern_len
, subject
, subject_len
, flags
);
683 if (pattern_len
!= subject_len
)
685 return (strncasecmp(pattern
, subject
, pattern_len
) == 0);
688 /* Compare using strncmp. */
689 static int equal_case(const unsigned char *pattern
, size_t pattern_len
,
690 const unsigned char *subject
, size_t subject_len
,
693 if (memchr(pattern
, 0, pattern_len
) != NULL
)
695 if (memchr(subject
, 0, subject_len
) != NULL
)
697 skip_prefix(&pattern
, &pattern_len
, subject
, subject_len
, flags
);
698 if (pattern_len
!= subject_len
)
700 return (strncmp(pattern
, subject
, pattern_len
) == 0);
704 * RFC 5280, section 7.5, requires that only the domain is compared in a
705 * case-insensitive manner.
707 static int equal_email(const unsigned char *a
, size_t a_len
,
708 const unsigned char *b
, size_t b_len
,
709 unsigned int unused_flags
)
715 * We search backwards for the '@' character, so that we do not have to
716 * deal with quoted local-parts. The domain part is compared in a
717 * case-insensitive manner.
721 if (a
[pos
] == '@' || b
[pos
] == '@') {
722 if (!equal_nocase(a
+ pos
, a_len
- pos
, b
+ pos
, a_len
- pos
, 0))
729 return equal_case(a
, pos
, b
, pos
, 0);
733 * Compare the prefix and suffix with the subject, and check that the
734 * characters in-between are valid.
736 static int wildcard_match(const unsigned char *prefix
, size_t prefix_len
,
737 const unsigned char *suffix
, size_t suffix_len
,
738 const unsigned char *subject
, size_t subject_len
, unsigned int flags
)
740 const unsigned char *wildcard_start
;
741 const unsigned char *wildcard_end
;
742 const unsigned char *p
;
746 if (subject_len
< prefix_len
+ suffix_len
)
748 if (!equal_nocase(prefix
, prefix_len
, subject
, prefix_len
, flags
))
750 wildcard_start
= subject
+ prefix_len
;
751 wildcard_end
= subject
+ (subject_len
- suffix_len
);
752 if (!equal_nocase(wildcard_end
, suffix_len
, suffix
, suffix_len
, flags
))
755 * If the wildcard makes up the entire first label, it must match at
756 * least one character.
758 if (prefix_len
== 0 && *suffix
== '.') {
759 if (wildcard_start
== wildcard_end
)
762 if (flags
& X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
)
765 /* IDNA labels cannot match partial wildcards */
768 && strncasecmp((char *)subject
, "xn--", 4) == 0)
770 /* The wildcard may match a literal '*' */
771 if (wildcard_end
== wildcard_start
+ 1 && *wildcard_start
== '*')
774 * Check that the part matched by the wildcard contains only
775 * permitted characters and only matches a single label unless
776 * allow_multi is set.
778 for (p
= wildcard_start
; p
!= wildcard_end
; ++p
)
779 if (!(('0' <= *p
&& *p
<= '9') || ('A' <= *p
&& *p
<= 'Z') ||
780 ('a' <= *p
&& *p
<= 'z') || *p
== '-' ||
781 (allow_multi
&& *p
== '.')))
786 #define LABEL_START (1 << 0)
787 #define LABEL_END (1 << 1)
788 #define LABEL_HYPHEN (1 << 2)
789 #define LABEL_IDNA (1 << 3)
791 static const unsigned char *valid_star(const unsigned char *p
, size_t len
,
794 const unsigned char *star
= 0;
796 int state
= LABEL_START
;
798 for (i
= 0; i
< len
; ++i
) {
800 * Locate first and only legal wildcard, either at the start
801 * or end of a non-IDNA first and not final label.
804 int atstart
= (state
& LABEL_START
);
805 int atend
= (i
== len
- 1 || p
[i
+ 1] == '.');
807 * At most one wildcard per pattern.
808 * No wildcards in IDNA labels.
809 * No wildcards after the first label.
811 if (star
!= NULL
|| (state
& LABEL_IDNA
) != 0 || dots
)
813 /* Only full-label '*.example.com' wildcards? */
814 if ((flags
& X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
)
815 && (!atstart
|| !atend
))
817 /* No 'foo*bar' wildcards */
818 if (!atstart
&& !atend
)
821 state
&= ~LABEL_START
;
822 } else if ((state
& LABEL_START
) != 0) {
824 * At the start of a label, skip any "xn--" and
825 * remain in the LABEL_START state, but set the
828 if ((state
& LABEL_IDNA
) == 0 && len
- i
>= 4
829 && strncasecmp((char *)&p
[i
], "xn--", 4) == 0) {
834 /* Labels must start with a letter or digit */
835 state
&= ~LABEL_START
;
836 if (('a' <= p
[i
] && p
[i
] <= 'z')
837 || ('A' <= p
[i
] && p
[i
] <= 'Z')
838 || ('0' <= p
[i
] && p
[i
] <= '9'))
841 } else if (('a' <= p
[i
] && p
[i
] <= 'z')
842 || ('A' <= p
[i
] && p
[i
] <= 'Z')
843 || ('0' <= p
[i
] && p
[i
] <= '9')) {
846 } else if (p
[i
] == '.') {
847 if (state
& (LABEL_HYPHEN
| LABEL_START
))
851 } else if (p
[i
] == '-') {
852 /* no domain/subdomain starts with '-' */
853 if ((state
& LABEL_START
) != 0)
855 state
|= LABEL_HYPHEN
;
861 * The final label must not end in a hyphen or ".", and
862 * there must be at least two dots after the star.
864 if ((state
& (LABEL_START
| LABEL_HYPHEN
)) != 0 || dots
< 2)
869 /* Compare using wildcards. */
870 static int equal_wildcard(const unsigned char *pattern
, size_t pattern_len
,
871 const unsigned char *subject
, size_t subject_len
, unsigned int flags
)
873 const unsigned char *star
= NULL
;
876 * Subject names starting with '.' can only match a wildcard pattern
877 * via a subject sub-domain pattern suffix match.
879 if (!(subject_len
> 1 && subject
[0] == '.'))
880 star
= valid_star(pattern
, pattern_len
, flags
);
882 return equal_nocase(pattern
, pattern_len
,
883 subject
, subject_len
, flags
);
884 return wildcard_match(pattern
, star
- pattern
,
885 star
+ 1, (pattern
+ pattern_len
) - star
- 1,
886 subject
, subject_len
, flags
);
890 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
891 * cmp_type > 0 only compare if string matches the type, otherwise convert it
896 do_check_string(ASN1_STRING
*a
, int cmp_type
, equal_fn equal
,
897 unsigned int flags
, const char *b
, size_t blen
, char **peername
)
901 if (!a
->data
|| !a
->length
)
904 if (cmp_type
!= a
->type
)
906 if (cmp_type
== V_ASN1_IA5STRING
)
907 rv
= equal(a
->data
, a
->length
, (unsigned char *)b
,
909 else if (a
->length
== (int)blen
&& !memcmp(a
->data
, b
, blen
))
911 if (rv
> 0 && peername
&&
912 (*peername
= strndup((char *)a
->data
, a
->length
)) == NULL
)
917 astrlen
= ASN1_STRING_to_UTF8(&astr
, a
);
920 rv
= equal(astr
, astrlen
, (unsigned char *)b
, blen
, flags
);
921 if (rv
> 0 && peername
&&
922 (*peername
= strndup((char *)astr
, astrlen
)) == NULL
)
929 static int do_x509_check(X509
*x
, const char *chk
, size_t chklen
,
930 unsigned int flags
, int check_type
, char **peername
)
932 GENERAL_NAMES
*gens
= NULL
;
933 X509_NAME
*name
= NULL
;
936 int cnid
= NID_undef
;
942 /* See below, this flag is internal-only */
943 flags
&= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS
;
944 if (check_type
== GEN_EMAIL
) {
945 cnid
= NID_pkcs9_emailAddress
;
946 alt_type
= V_ASN1_IA5STRING
;
948 } else if (check_type
== GEN_DNS
) {
949 cnid
= NID_commonName
;
950 /* Implicit client-side DNS sub-domain pattern */
951 if (chklen
> 1 && chk
[0] == '.')
952 flags
|= _X509_CHECK_FLAG_DOT_SUBDOMAINS
;
953 alt_type
= V_ASN1_IA5STRING
;
954 if (flags
& X509_CHECK_FLAG_NO_WILDCARDS
)
955 equal
= equal_nocase
;
957 equal
= equal_wildcard
;
959 alt_type
= V_ASN1_OCTET_STRING
;
963 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
965 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
968 gen
= sk_GENERAL_NAME_value(gens
, i
);
969 if (gen
->type
!= check_type
)
972 if (check_type
== GEN_EMAIL
)
973 cstr
= gen
->d
.rfc822Name
;
974 else if (check_type
== GEN_DNS
)
975 cstr
= gen
->d
.dNSName
;
977 cstr
= gen
->d
.iPAddress
;
978 /* Positive on success, negative on error! */
979 if ((rv
= do_check_string(cstr
, alt_type
, equal
, flags
,
980 chk
, chklen
, peername
)) != 0)
983 GENERAL_NAMES_free(gens
);
986 if (cnid
== NID_undef
||
988 !(flags
& X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
)))
992 /* We're done if CN-ID is not pertinent */
993 if (cnid
== NID_undef
)
997 name
= X509_get_subject_name(x
);
998 while ((j
= X509_NAME_get_index_by_NID(name
, cnid
, j
)) >= 0) {
1001 if ((ne
= X509_NAME_get_entry(name
, j
)) == NULL
)
1003 if ((str
= X509_NAME_ENTRY_get_data(ne
)) == NULL
)
1005 /* Positive on success, negative on error! */
1006 if ((rv
= do_check_string(str
, -1, equal
, flags
,
1007 chk
, chklen
, peername
)) != 0)
1013 int X509_check_host(X509
*x
, const char *chk
, size_t chklen
,
1014 unsigned int flags
, char **peername
)
1019 chklen
= strlen(chk
);
1020 else if (memchr(chk
, '\0', chklen
))
1022 return do_x509_check(x
, chk
, chklen
, flags
, GEN_DNS
, peername
);
1025 int X509_check_email(X509
*x
, const char *chk
, size_t chklen
,
1031 chklen
= strlen(chk
);
1032 else if (memchr(chk
, '\0', chklen
))
1034 return do_x509_check(x
, chk
, chklen
, flags
, GEN_EMAIL
, NULL
);
1037 int X509_check_ip(X509
*x
, const unsigned char *chk
, size_t chklen
,
1042 return do_x509_check(x
, (char *)chk
, chklen
, flags
, GEN_IPADD
, NULL
);
1045 int X509_check_ip_asc(X509
*x
, const char *ipasc
, unsigned int flags
)
1047 unsigned char ipout
[16];
1052 iplen
= (size_t)a2i_ipadd(ipout
, ipasc
);
1055 return do_x509_check(x
, (char *)ipout
, iplen
, flags
, GEN_IPADD
, NULL
);
1058 /* Convert IP addresses both IPv4 and IPv6 into an
1059 * OCTET STRING compatible with RFC3280.
1063 a2i_IPADDRESS(const char *ipasc
)
1065 unsigned char ipout
[16];
1066 ASN1_OCTET_STRING
*ret
;
1069 /* If string contains a ':' assume IPv6 */
1071 iplen
= a2i_ipadd(ipout
, ipasc
);
1076 ret
= ASN1_OCTET_STRING_new();
1079 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen
)) {
1080 ASN1_OCTET_STRING_free(ret
);
1087 a2i_IPADDRESS_NC(const char *ipasc
)
1089 ASN1_OCTET_STRING
*ret
= NULL
;
1090 unsigned char ipout
[32];
1091 char *iptmp
= NULL
, *p
;
1094 p
= strchr(ipasc
, '/');
1097 iptmp
= strdup(ipasc
);
1100 p
= iptmp
+ (p
- ipasc
);
1103 iplen1
= a2i_ipadd(ipout
, iptmp
);
1108 iplen2
= a2i_ipadd(ipout
+ iplen1
, p
);
1113 if (!iplen2
|| (iplen1
!= iplen2
))
1116 ret
= ASN1_OCTET_STRING_new();
1119 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen1
+ iplen2
))
1127 ASN1_OCTET_STRING_free(ret
);
1133 a2i_ipadd(unsigned char *ipout
, const char *ipasc
)
1135 /* If string contains a ':' assume IPv6 */
1137 if (strchr(ipasc
, ':')) {
1138 if (!ipv6_from_asc(ipout
, ipasc
))
1142 if (!ipv4_from_asc(ipout
, ipasc
))
1149 ipv4_from_asc(unsigned char *v4
, const char *in
)
1152 if (sscanf(in
, "%d.%d.%d.%d", &a0
, &a1
, &a2
, &a3
) != 4)
1154 if ((a0
< 0) || (a0
> 255) || (a1
< 0) || (a1
> 255) ||
1155 (a2
< 0) || (a2
> 255) || (a3
< 0) || (a3
> 255))
1165 /* Temporary store for IPV6 output */
1166 unsigned char tmp
[16];
1167 /* Total number of bytes in tmp */
1169 /* The position of a zero (corresponding to '::') */
1171 /* Number of zeroes */
1177 ipv6_from_asc(unsigned char *v6
, const char *in
)
1182 v6stat
.zero_pos
= -1;
1183 v6stat
.zero_cnt
= 0;
1185 /* Treat the IPv6 representation as a list of values
1186 * separated by ':'. The presence of a '::' will parse
1187 * as one, two or three zero length elements.
1189 if (!CONF_parse_list(in
, ':', 0, ipv6_cb
, &v6stat
))
1192 /* Now for some sanity checks */
1194 if (v6stat
.zero_pos
== -1) {
1195 /* If no '::' must have exactly 16 bytes */
1196 if (v6stat
.total
!= 16)
1199 /* If '::' must have less than 16 bytes */
1200 if (v6stat
.total
== 16)
1202 /* More than three zeroes is an error */
1203 if (v6stat
.zero_cnt
> 3)
1205 /* Can only have three zeroes if nothing else present */
1206 else if (v6stat
.zero_cnt
== 3) {
1207 if (v6stat
.total
> 0)
1210 /* Can only have two zeroes if at start or end */
1211 else if (v6stat
.zero_cnt
== 2) {
1212 if ((v6stat
.zero_pos
!= 0) &&
1213 (v6stat
.zero_pos
!= v6stat
.total
))
1216 /* Can only have one zero if *not* start or end */
1218 if ((v6stat
.zero_pos
== 0) ||
1219 (v6stat
.zero_pos
== v6stat
.total
))
1226 if (v6stat
.zero_pos
>= 0) {
1227 /* Copy initial part */
1228 memcpy(v6
, v6stat
.tmp
, v6stat
.zero_pos
);
1230 memset(v6
+ v6stat
.zero_pos
, 0, 16 - v6stat
.total
);
1231 /* Copy final part */
1232 if (v6stat
.total
!= v6stat
.zero_pos
)
1233 memcpy(v6
+ v6stat
.zero_pos
+ 16 - v6stat
.total
,
1234 v6stat
.tmp
+ v6stat
.zero_pos
,
1235 v6stat
.total
- v6stat
.zero_pos
);
1237 memcpy(v6
, v6stat
.tmp
, 16);
1243 ipv6_cb(const char *elem
, int len
, void *usr
)
1247 /* Error if 16 bytes written */
1251 /* Zero length element, corresponds to '::' */
1252 if (s
->zero_pos
== -1)
1253 s
->zero_pos
= s
->total
;
1254 /* If we've already got a :: its an error */
1255 else if (s
->zero_pos
!= s
->total
)
1259 /* If more than 4 characters could be final a.b.c.d form */
1261 /* Need at least 4 bytes left */
1264 /* Must be end of string */
1267 if (!ipv4_from_asc(s
->tmp
+ s
->total
, elem
))
1271 if (!ipv6_hex(s
->tmp
+ s
->total
, elem
, len
))
1279 /* Convert a string of up to 4 hex digits into the corresponding
1284 ipv6_hex(unsigned char *out
, const char *in
, int inlen
)
1287 unsigned int num
= 0;
1294 if ((c
>= '0') && (c
<= '9'))
1296 else if ((c
>= 'A') && (c
<= 'F'))
1297 num
|= c
- 'A' + 10;
1298 else if ((c
>= 'a') && (c
<= 'f'))
1299 num
|= c
- 'a' + 10;
1304 out
[1] = num
& 0xff;
1309 X509V3_NAME_from_section(X509_NAME
*nm
, STACK_OF(CONF_VALUE
)*dn_sk
,
1310 unsigned long chtype
)
1319 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1320 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1322 /* Skip past any leading X. X: X, etc to allow for
1323 * multiple instances
1325 for (p
= type
; *p
; p
++)
1326 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1337 if (!X509_NAME_add_entry_by_txt(nm
, type
, chtype
,
1338 (unsigned char *) v
->value
, -1, -1, mval
))