2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 */
12 #include "internal/e_os.h"
13 #include "internal/cryptlib.h"
16 #include "crypto/ctype.h"
17 #include <openssl/conf.h>
18 #include <openssl/crypto.h>
19 #include <openssl/x509v3.h>
20 #include "crypto/x509.h"
21 #include <openssl/bn.h>
23 #include "x509_local.h"
25 static char *strip_spaces(char *name
);
26 static int sk_strcmp(const char *const *a
, const char *const *b
);
27 static STACK_OF(OPENSSL_STRING
) *get_email(const X509_NAME
*name
,
29 static void str_free(OPENSSL_STRING str
);
30 static int append_ia5(STACK_OF(OPENSSL_STRING
) **sk
,
31 const ASN1_IA5STRING
*email
);
33 static int ipv4_from_asc(unsigned char *v4
, const char *in
);
34 static int ipv6_from_asc(unsigned char *v6
, const char *in
);
35 static int ipv6_cb(const char *elem
, int len
, void *usr
);
36 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
);
38 /* Add a CONF_VALUE name value pair to stack */
40 static int x509v3_add_len_value(const char *name
, const char *value
,
41 size_t vallen
, STACK_OF(CONF_VALUE
) **extlist
)
43 CONF_VALUE
*vtmp
= NULL
;
44 char *tname
= NULL
, *tvalue
= NULL
;
45 int sk_allocated
= (*extlist
== NULL
);
47 if (name
!= NULL
&& (tname
= OPENSSL_strdup(name
)) == NULL
)
50 /* We don't allow embedded NUL characters */
51 if (memchr(value
, 0, vallen
) != NULL
)
53 tvalue
= OPENSSL_strndup(value
, vallen
);
57 if ((vtmp
= OPENSSL_malloc(sizeof(*vtmp
))) == NULL
)
59 if (sk_allocated
&& (*extlist
= sk_CONF_VALUE_new_null()) == NULL
)
64 if (!sk_CONF_VALUE_push(*extlist
, vtmp
))
68 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
70 sk_CONF_VALUE_free(*extlist
);
79 int X509V3_add_value(const char *name
, const char *value
,
80 STACK_OF(CONF_VALUE
) **extlist
)
82 return x509v3_add_len_value(name
, value
,
83 value
!= NULL
? strlen((const char *)value
) : 0,
87 int X509V3_add_value_uchar(const char *name
, const unsigned char *value
,
88 STACK_OF(CONF_VALUE
) **extlist
)
90 return x509v3_add_len_value(name
, (const char *)value
,
91 value
!= NULL
? strlen((const char *)value
) : 0,
95 int x509v3_add_len_value_uchar(const char *name
, const unsigned char *value
,
96 size_t vallen
, STACK_OF(CONF_VALUE
) **extlist
)
98 return x509v3_add_len_value(name
, (const char *)value
, vallen
, extlist
);
101 /* Free function for STACK_OF(CONF_VALUE) */
103 void X509V3_conf_free(CONF_VALUE
*conf
)
107 OPENSSL_free(conf
->name
);
108 OPENSSL_free(conf
->value
);
109 OPENSSL_free(conf
->section
);
113 int X509V3_add_value_bool(const char *name
, int asn1_bool
,
114 STACK_OF(CONF_VALUE
) **extlist
)
117 return X509V3_add_value(name
, "TRUE", extlist
);
118 return X509V3_add_value(name
, "FALSE", extlist
);
121 int X509V3_add_value_bool_nf(const char *name
, int asn1_bool
,
122 STACK_OF(CONF_VALUE
) **extlist
)
125 return X509V3_add_value(name
, "TRUE", extlist
);
129 static char *bignum_to_string(const BIGNUM
*bn
)
135 * Display large numbers in hex and small numbers in decimal. Converting to
136 * decimal takes quadratic time and is no more useful than hex for large
139 if (BN_num_bits(bn
) < 128)
140 return BN_bn2dec(bn
);
146 len
= strlen(tmp
) + 3;
147 ret
= OPENSSL_malloc(len
);
149 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
154 /* Prepend "0x", but place it after the "-" if negative. */
156 OPENSSL_strlcpy(ret
, "-0x", len
);
157 OPENSSL_strlcat(ret
, tmp
+ 1, len
);
159 OPENSSL_strlcpy(ret
, "0x", len
);
160 OPENSSL_strlcat(ret
, tmp
, len
);
166 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, const ASN1_ENUMERATED
*a
)
168 BIGNUM
*bntmp
= NULL
;
173 if ((bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) == NULL
174 || (strtmp
= bignum_to_string(bntmp
)) == NULL
)
175 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
180 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const ASN1_INTEGER
*a
)
182 BIGNUM
*bntmp
= NULL
;
187 if ((bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) == NULL
188 || (strtmp
= bignum_to_string(bntmp
)) == NULL
)
189 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
194 ASN1_INTEGER
*s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const char *value
)
202 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_NULL_VALUE
);
207 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
210 if (value
[0] == '-') {
217 if (value
[0] == '0' && ((value
[1] == 'x') || (value
[1] == 'X'))) {
225 ret
= BN_hex2bn(&bn
, value
);
227 ret
= BN_dec2bn(&bn
, value
);
229 if (!ret
|| value
[ret
]) {
231 ERR_raise(ERR_LIB_X509V3
, X509V3_R_BN_DEC2BN_ERROR
);
235 if (isneg
&& BN_is_zero(bn
))
238 aint
= BN_to_ASN1_INTEGER(bn
, NULL
);
241 ERR_raise(ERR_LIB_X509V3
, X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
245 aint
->type
|= V_ASN1_NEG
;
249 int X509V3_add_value_int(const char *name
, const ASN1_INTEGER
*aint
,
250 STACK_OF(CONF_VALUE
) **extlist
)
257 if ((strtmp
= i2s_ASN1_INTEGER(NULL
, aint
)) == NULL
)
259 ret
= X509V3_add_value(name
, strtmp
, extlist
);
260 OPENSSL_free(strtmp
);
264 int X509V3_get_value_bool(const CONF_VALUE
*value
, int *asn1_bool
)
268 if ((btmp
= value
->value
) == NULL
)
270 if (strcmp(btmp
, "TRUE") == 0
271 || strcmp(btmp
, "true") == 0
272 || strcmp(btmp
, "Y") == 0
273 || strcmp(btmp
, "y") == 0
274 || strcmp(btmp
, "YES") == 0
275 || strcmp(btmp
, "yes") == 0) {
279 if (strcmp(btmp
, "FALSE") == 0
280 || strcmp(btmp
, "false") == 0
281 || strcmp(btmp
, "N") == 0
282 || strcmp(btmp
, "n") == 0
283 || strcmp(btmp
, "NO") == 0
284 || strcmp(btmp
, "no") == 0) {
289 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_BOOLEAN_STRING
);
290 X509V3_conf_add_error_name_value(value
);
294 int X509V3_get_value_int(const CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
298 if ((itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
)) == NULL
) {
299 X509V3_conf_add_error_name_value(value
);
313 STACK_OF(CONF_VALUE
) *X509V3_parse_list(const char *line
)
317 STACK_OF(CONF_VALUE
) *values
= NULL
;
321 /* We are going to modify the line so copy it first */
322 linebuf
= OPENSSL_strdup(line
);
323 if (linebuf
== NULL
) {
324 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
329 /* Go through all characters */
330 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') && (c
!= '\n');
338 ntmp
= strip_spaces(q
);
340 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_EMPTY_NAME
);
344 } else if (c
== ',') {
346 ntmp
= strip_spaces(q
);
349 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_EMPTY_NAME
);
352 X509V3_add_value(ntmp
, NULL
, &values
);
360 vtmp
= strip_spaces(q
);
362 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_NULL_VALUE
);
365 X509V3_add_value(ntmp
, vtmp
, &values
);
373 if (state
== HDR_VALUE
) {
374 vtmp
= strip_spaces(q
);
376 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_NULL_VALUE
);
379 X509V3_add_value(ntmp
, vtmp
, &values
);
381 ntmp
= strip_spaces(q
);
383 ERR_raise(ERR_LIB_X509V3
, X509V3_R_INVALID_EMPTY_NAME
);
386 X509V3_add_value(ntmp
, NULL
, &values
);
388 OPENSSL_free(linebuf
);
392 OPENSSL_free(linebuf
);
393 sk_CONF_VALUE_pop_free(values
, X509V3_conf_free
);
398 /* Delete leading and trailing spaces from a string */
399 static char *strip_spaces(char *name
)
403 /* Skip over leading spaces */
405 while (*p
&& ossl_isspace(*p
))
409 q
= p
+ strlen(p
) - 1;
410 while ((q
!= p
) && ossl_isspace(*q
))
421 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
424 int ossl_v3_name_cmp(const char *name
, const char *cmp
)
430 if ((ret
= strncmp(name
, cmp
, len
)))
433 if (!c
|| (c
== '.'))
438 static int sk_strcmp(const char *const *a
, const char *const *b
)
440 return strcmp(*a
, *b
);
443 STACK_OF(OPENSSL_STRING
) *X509_get1_email(X509
*x
)
446 STACK_OF(OPENSSL_STRING
) *ret
;
448 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
449 ret
= get_email(X509_get_subject_name(x
), gens
);
450 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
454 STACK_OF(OPENSSL_STRING
) *X509_get1_ocsp(X509
*x
)
456 AUTHORITY_INFO_ACCESS
*info
;
457 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
460 info
= X509_get_ext_d2i(x
, NID_info_access
, NULL
, NULL
);
463 for (i
= 0; i
< sk_ACCESS_DESCRIPTION_num(info
); i
++) {
464 ACCESS_DESCRIPTION
*ad
= sk_ACCESS_DESCRIPTION_value(info
, i
);
465 if (OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
) {
466 if (ad
->location
->type
== GEN_URI
) {
468 (&ret
, ad
->location
->d
.uniformResourceIdentifier
))
473 AUTHORITY_INFO_ACCESS_free(info
);
477 STACK_OF(OPENSSL_STRING
) *X509_REQ_get1_email(X509_REQ
*x
)
480 STACK_OF(X509_EXTENSION
) *exts
;
481 STACK_OF(OPENSSL_STRING
) *ret
;
483 exts
= X509_REQ_get_extensions(x
);
484 gens
= X509V3_get_d2i(exts
, NID_subject_alt_name
, NULL
, NULL
);
485 ret
= get_email(X509_REQ_get_subject_name(x
), gens
);
486 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
487 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
491 static STACK_OF(OPENSSL_STRING
) *get_email(const X509_NAME
*name
,
494 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
496 const ASN1_IA5STRING
*email
;
500 /* Now add any email address(es) to STACK */
501 /* First supplied X509_NAME */
502 while ((i
= X509_NAME_get_index_by_NID(name
,
503 NID_pkcs9_emailAddress
, i
)) >= 0) {
504 ne
= X509_NAME_get_entry(name
, i
);
505 email
= X509_NAME_ENTRY_get_data(ne
);
506 if (!append_ia5(&ret
, email
))
509 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
510 gen
= sk_GENERAL_NAME_value(gens
, i
);
511 if (gen
->type
!= GEN_EMAIL
)
513 if (!append_ia5(&ret
, gen
->d
.ia5
))
519 static void str_free(OPENSSL_STRING str
)
524 static int append_ia5(STACK_OF(OPENSSL_STRING
) **sk
,
525 const ASN1_IA5STRING
*email
)
529 /* First some sanity checks */
530 if (email
->type
!= V_ASN1_IA5STRING
)
532 if (email
->data
== NULL
|| email
->length
== 0)
534 if (memchr(email
->data
, 0, email
->length
) != NULL
)
537 *sk
= sk_OPENSSL_STRING_new(sk_strcmp
);
541 emtmp
= OPENSSL_strndup((char *)email
->data
, email
->length
);
545 /* Don't add duplicates */
546 if (sk_OPENSSL_STRING_find(*sk
, emtmp
) != -1) {
550 if (!sk_OPENSSL_STRING_push(*sk
, emtmp
)) {
551 OPENSSL_free(emtmp
); /* free on push failure */
552 X509_email_free(*sk
);
559 void X509_email_free(STACK_OF(OPENSSL_STRING
) *sk
)
561 sk_OPENSSL_STRING_pop_free(sk
, str_free
);
564 typedef int (*equal_fn
) (const unsigned char *pattern
, size_t pattern_len
,
565 const unsigned char *subject
, size_t subject_len
,
568 /* Skip pattern prefix to match "wildcard" subject */
569 static void skip_prefix(const unsigned char **p
, size_t *plen
,
573 const unsigned char *pattern
= *p
;
574 size_t pattern_len
= *plen
;
577 * If subject starts with a leading '.' followed by more octets, and
578 * pattern is longer, compare just an equal-length suffix with the
579 * full subject (starting at the '.'), provided the prefix contains
582 if ((flags
& _X509_CHECK_FLAG_DOT_SUBDOMAINS
) == 0)
585 while (pattern_len
> subject_len
&& *pattern
) {
586 if ((flags
& X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
) &&
593 /* Skip if entire prefix acceptable */
594 if (pattern_len
== subject_len
) {
600 /* Compare while ASCII ignoring case. */
601 static int equal_nocase(const unsigned char *pattern
, size_t pattern_len
,
602 const unsigned char *subject
, size_t subject_len
,
605 skip_prefix(&pattern
, &pattern_len
, subject_len
, flags
);
606 if (pattern_len
!= subject_len
)
608 while (pattern_len
!= 0) {
609 unsigned char l
= *pattern
;
610 unsigned char r
= *subject
;
612 /* The pattern must not contain NUL characters. */
616 if ('A' <= l
&& l
<= 'Z')
618 if ('A' <= r
&& r
<= 'Z')
630 /* Compare using memcmp. */
631 static int equal_case(const unsigned char *pattern
, size_t pattern_len
,
632 const unsigned char *subject
, size_t subject_len
,
635 skip_prefix(&pattern
, &pattern_len
, subject_len
, flags
);
636 if (pattern_len
!= subject_len
)
638 return !memcmp(pattern
, subject
, pattern_len
);
642 * RFC 5280, section 7.5, requires that only the domain is compared in a
643 * case-insensitive manner.
645 static int equal_email(const unsigned char *a
, size_t a_len
,
646 const unsigned char *b
, size_t b_len
,
647 unsigned int unused_flags
)
654 * We search backwards for the '@' character, so that we do not have to
655 * deal with quoted local-parts. The domain part is compared in a
656 * case-insensitive manner.
660 if (a
[i
] == '@' || b
[i
] == '@') {
661 if (!equal_nocase(a
+ i
, a_len
- i
, b
+ i
, a_len
- i
, 0))
668 return equal_case(a
, i
, b
, i
, 0);
672 * Compare the prefix and suffix with the subject, and check that the
673 * characters in-between are valid.
675 static int wildcard_match(const unsigned char *prefix
, size_t prefix_len
,
676 const unsigned char *suffix
, size_t suffix_len
,
677 const unsigned char *subject
, size_t subject_len
,
680 const unsigned char *wildcard_start
;
681 const unsigned char *wildcard_end
;
682 const unsigned char *p
;
686 if (subject_len
< prefix_len
+ suffix_len
)
688 if (!equal_nocase(prefix
, prefix_len
, subject
, prefix_len
, flags
))
690 wildcard_start
= subject
+ prefix_len
;
691 wildcard_end
= subject
+ (subject_len
- suffix_len
);
692 if (!equal_nocase(wildcard_end
, suffix_len
, suffix
, suffix_len
, flags
))
695 * If the wildcard makes up the entire first label, it must match at
696 * least one character.
698 if (prefix_len
== 0 && *suffix
== '.') {
699 if (wildcard_start
== wildcard_end
)
702 if (flags
& X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
)
705 /* IDNA labels cannot match partial wildcards */
707 subject_len
>= 4 && HAS_CASE_PREFIX((const char *)subject
, "xn--"))
709 /* The wildcard may match a literal '*' */
710 if (wildcard_end
== wildcard_start
+ 1 && *wildcard_start
== '*')
713 * Check that the part matched by the wildcard contains only
714 * permitted characters and only matches a single label unless
715 * allow_multi is set.
717 for (p
= wildcard_start
; p
!= wildcard_end
; ++p
)
718 if (!(('0' <= *p
&& *p
<= '9') ||
719 ('A' <= *p
&& *p
<= 'Z') ||
720 ('a' <= *p
&& *p
<= 'z') ||
721 *p
== '-' || (allow_multi
&& *p
== '.')))
726 #define LABEL_START (1 << 0)
727 #define LABEL_END (1 << 1)
728 #define LABEL_HYPHEN (1 << 2)
729 #define LABEL_IDNA (1 << 3)
731 static const unsigned char *valid_star(const unsigned char *p
, size_t len
,
734 const unsigned char *star
= 0;
736 int state
= LABEL_START
;
739 for (i
= 0; i
< len
; ++i
) {
741 * Locate first and only legal wildcard, either at the start
742 * or end of a non-IDNA first and not final label.
745 int atstart
= (state
& LABEL_START
);
746 int atend
= (i
== len
- 1 || p
[i
+ 1] == '.');
748 * At most one wildcard per pattern.
749 * No wildcards in IDNA labels.
750 * No wildcards after the first label.
752 if (star
!= NULL
|| (state
& LABEL_IDNA
) != 0 || dots
)
754 /* Only full-label '*.example.com' wildcards? */
755 if ((flags
& X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
)
756 && (!atstart
|| !atend
))
758 /* No 'foo*bar' wildcards */
759 if (!atstart
&& !atend
)
762 state
&= ~LABEL_START
;
763 } else if (('a' <= p
[i
] && p
[i
] <= 'z')
764 || ('A' <= p
[i
] && p
[i
] <= 'Z')
765 || ('0' <= p
[i
] && p
[i
] <= '9')) {
766 if ((state
& LABEL_START
) != 0
767 && len
- i
>= 4 && HAS_CASE_PREFIX((const char *)&p
[i
], "xn--"))
769 state
&= ~(LABEL_HYPHEN
| LABEL_START
);
770 } else if (p
[i
] == '.') {
771 if ((state
& (LABEL_HYPHEN
| LABEL_START
)) != 0)
775 } else if (p
[i
] == '-') {
776 /* no domain/subdomain starts with '-' */
777 if ((state
& LABEL_START
) != 0)
779 state
|= LABEL_HYPHEN
;
786 * The final label must not end in a hyphen or ".", and
787 * there must be at least two dots after the star.
789 if ((state
& (LABEL_START
| LABEL_HYPHEN
)) != 0 || dots
< 2)
794 /* Compare using wildcards. */
795 static int equal_wildcard(const unsigned char *pattern
, size_t pattern_len
,
796 const unsigned char *subject
, size_t subject_len
,
799 const unsigned char *star
= NULL
;
802 * Subject names starting with '.' can only match a wildcard pattern
803 * via a subject sub-domain pattern suffix match.
805 if (!(subject_len
> 1 && subject
[0] == '.'))
806 star
= valid_star(pattern
, pattern_len
, flags
);
808 return equal_nocase(pattern
, pattern_len
,
809 subject
, subject_len
, flags
);
810 return wildcard_match(pattern
, star
- pattern
,
811 star
+ 1, (pattern
+ pattern_len
) - star
- 1,
812 subject
, subject_len
, flags
);
816 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
817 * cmp_type > 0 only compare if string matches the type, otherwise convert it
821 static int do_check_string(const ASN1_STRING
*a
, int cmp_type
, equal_fn equal
,
822 unsigned int flags
, const char *b
, size_t blen
,
827 if (!a
->data
|| !a
->length
)
830 if (cmp_type
!= a
->type
)
832 if (cmp_type
== V_ASN1_IA5STRING
)
833 rv
= equal(a
->data
, a
->length
, (unsigned char *)b
, blen
, flags
);
834 else if (a
->length
== (int)blen
&& !memcmp(a
->data
, b
, blen
))
836 if (rv
> 0 && peername
)
837 *peername
= OPENSSL_strndup((char *)a
->data
, a
->length
);
841 astrlen
= ASN1_STRING_to_UTF8(&astr
, a
);
844 * -1 could be an internal malloc failure or a decoding error from
845 * malformed input; we can't distinguish.
849 rv
= equal(astr
, astrlen
, (unsigned char *)b
, blen
, flags
);
850 if (rv
> 0 && peername
)
851 *peername
= OPENSSL_strndup((char *)astr
, astrlen
);
857 static int do_x509_check(X509
*x
, const char *chk
, size_t chklen
,
858 unsigned int flags
, int check_type
, char **peername
)
860 GENERAL_NAMES
*gens
= NULL
;
861 const X509_NAME
*name
= NULL
;
863 int cnid
= NID_undef
;
869 /* See below, this flag is internal-only */
870 flags
&= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS
;
871 if (check_type
== GEN_EMAIL
) {
872 cnid
= NID_pkcs9_emailAddress
;
873 alt_type
= V_ASN1_IA5STRING
;
875 } else if (check_type
== GEN_DNS
) {
876 cnid
= NID_commonName
;
877 /* Implicit client-side DNS sub-domain pattern */
878 if (chklen
> 1 && chk
[0] == '.')
879 flags
|= _X509_CHECK_FLAG_DOT_SUBDOMAINS
;
880 alt_type
= V_ASN1_IA5STRING
;
881 if (flags
& X509_CHECK_FLAG_NO_WILDCARDS
)
882 equal
= equal_nocase
;
884 equal
= equal_wildcard
;
886 alt_type
= V_ASN1_OCTET_STRING
;
891 chklen
= strlen(chk
);
893 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
895 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
899 gen
= sk_GENERAL_NAME_value(gens
, i
);
900 if ((gen
->type
== GEN_OTHERNAME
) && (check_type
== GEN_EMAIL
)) {
901 if (OBJ_obj2nid(gen
->d
.otherName
->type_id
) ==
902 NID_id_on_SmtpUTF8Mailbox
) {
906 * If it is not a UTF8String then that is unexpected and we
907 * treat it as no match
909 if (gen
->d
.otherName
->value
->type
== V_ASN1_UTF8STRING
) {
910 cstr
= gen
->d
.otherName
->value
->value
.utf8string
;
912 /* Positive on success, negative on error! */
913 if ((rv
= do_check_string(cstr
, 0, equal
, flags
,
914 chk
, chklen
, peername
)) != 0)
920 if ((gen
->type
!= check_type
) && (gen
->type
!= GEN_OTHERNAME
))
924 if (check_type
== GEN_EMAIL
)
925 cstr
= gen
->d
.rfc822Name
;
926 else if (check_type
== GEN_DNS
)
927 cstr
= gen
->d
.dNSName
;
929 cstr
= gen
->d
.iPAddress
;
930 /* Positive on success, negative on error! */
931 if ((rv
= do_check_string(cstr
, alt_type
, equal
, flags
,
932 chk
, chklen
, peername
)) != 0)
935 GENERAL_NAMES_free(gens
);
938 if (san_present
&& !(flags
& X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
))
942 /* We're done if CN-ID is not pertinent */
943 if (cnid
== NID_undef
|| (flags
& X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
))
947 name
= X509_get_subject_name(x
);
948 while ((i
= X509_NAME_get_index_by_NID(name
, cnid
, i
)) >= 0) {
949 const X509_NAME_ENTRY
*ne
= X509_NAME_get_entry(name
, i
);
950 const ASN1_STRING
*str
= X509_NAME_ENTRY_get_data(ne
);
952 /* Positive on success, negative on error! */
953 if ((rv
= do_check_string(str
, -1, equal
, flags
,
954 chk
, chklen
, peername
)) != 0)
960 int X509_check_host(X509
*x
, const char *chk
, size_t chklen
,
961 unsigned int flags
, char **peername
)
966 * Embedded NULs are disallowed, except as the last character of a
967 * string of length 2 or more (tolerate caller including terminating
968 * NUL in string length).
971 chklen
= strlen(chk
);
972 else if (memchr(chk
, '\0', chklen
> 1 ? chklen
- 1 : chklen
))
974 if (chklen
> 1 && chk
[chklen
- 1] == '\0')
976 return do_x509_check(x
, chk
, chklen
, flags
, GEN_DNS
, peername
);
979 int X509_check_email(X509
*x
, const char *chk
, size_t chklen
,
985 * Embedded NULs are disallowed, except as the last character of a
986 * string of length 2 or more (tolerate caller including terminating
987 * NUL in string length).
990 chklen
= strlen((char *)chk
);
991 else if (memchr(chk
, '\0', chklen
> 1 ? chklen
- 1 : chklen
))
993 if (chklen
> 1 && chk
[chklen
- 1] == '\0')
995 return do_x509_check(x
, chk
, chklen
, flags
, GEN_EMAIL
, NULL
);
998 int X509_check_ip(X509
*x
, const unsigned char *chk
, size_t chklen
,
1003 return do_x509_check(x
, (char *)chk
, chklen
, flags
, GEN_IPADD
, NULL
);
1006 int X509_check_ip_asc(X509
*x
, const char *ipasc
, unsigned int flags
)
1008 unsigned char ipout
[16];
1013 iplen
= (size_t)ossl_a2i_ipadd(ipout
, ipasc
);
1016 return do_x509_check(x
, (char *)ipout
, iplen
, flags
, GEN_IPADD
, NULL
);
1019 char *ossl_ipaddr_to_asc(unsigned char *p
, int len
)
1022 * 40 is enough space for the longest IPv6 address + nul terminator byte
1023 * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX\0
1026 int i
= 0, remain
= 0, bytes
= 0;
1030 BIO_snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
1033 for (out
= buf
, i
= 8, remain
= sizeof(buf
);
1034 i
-- > 0 && bytes
>= 0;
1035 remain
-= bytes
, out
+= bytes
) {
1036 const char *template = (i
> 0 ? "%X:" : "%X");
1038 bytes
= BIO_snprintf(out
, remain
, template, p
[0] << 8 | p
[1]);
1043 BIO_snprintf(buf
, sizeof(buf
), "<invalid length=%d>", len
);
1046 return OPENSSL_strdup(buf
);
1050 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1054 ASN1_OCTET_STRING
*a2i_IPADDRESS(const char *ipasc
)
1056 unsigned char ipout
[16];
1057 ASN1_OCTET_STRING
*ret
;
1060 /* If string contains a ':' assume IPv6 */
1062 iplen
= ossl_a2i_ipadd(ipout
, ipasc
);
1067 ret
= ASN1_OCTET_STRING_new();
1070 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen
)) {
1071 ASN1_OCTET_STRING_free(ret
);
1077 ASN1_OCTET_STRING
*a2i_IPADDRESS_NC(const char *ipasc
)
1079 ASN1_OCTET_STRING
*ret
= NULL
;
1080 unsigned char ipout
[32];
1081 char *iptmp
= NULL
, *p
;
1084 p
= strchr(ipasc
, '/');
1087 iptmp
= OPENSSL_strdup(ipasc
);
1090 p
= iptmp
+ (p
- ipasc
);
1093 iplen1
= ossl_a2i_ipadd(ipout
, iptmp
);
1098 iplen2
= ossl_a2i_ipadd(ipout
+ iplen1
, p
);
1100 OPENSSL_free(iptmp
);
1103 if (!iplen2
|| (iplen1
!= iplen2
))
1106 ret
= ASN1_OCTET_STRING_new();
1109 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen1
+ iplen2
))
1115 OPENSSL_free(iptmp
);
1116 ASN1_OCTET_STRING_free(ret
);
1120 int ossl_a2i_ipadd(unsigned char *ipout
, const char *ipasc
)
1122 /* If string contains a ':' assume IPv6 */
1124 if (strchr(ipasc
, ':')) {
1125 if (!ipv6_from_asc(ipout
, ipasc
))
1129 if (!ipv4_from_asc(ipout
, ipasc
))
1135 static int ipv4_from_asc(unsigned char *v4
, const char *in
)
1138 int a0
, a1
, a2
, a3
, n
;
1140 if (sscanf(in
, "%d.%d.%d.%d%n", &a0
, &a1
, &a2
, &a3
, &n
) != 4)
1142 if ((a0
< 0) || (a0
> 255) || (a1
< 0) || (a1
> 255)
1143 || (a2
< 0) || (a2
> 255) || (a3
< 0) || (a3
> 255))
1146 if (!(*p
== '\0' || ossl_isspace(*p
)))
1156 /* Temporary store for IPV6 output */
1157 unsigned char tmp
[16];
1158 /* Total number of bytes in tmp */
1160 /* The position of a zero (corresponding to '::') */
1162 /* Number of zeroes */
1166 static int ipv6_from_asc(unsigned char *v6
, const char *in
)
1171 v6stat
.zero_pos
= -1;
1172 v6stat
.zero_cnt
= 0;
1174 * Treat the IPv6 representation as a list of values separated by ':'.
1175 * The presence of a '::' will parse as one, two or three zero length
1178 if (!CONF_parse_list(in
, ':', 0, ipv6_cb
, &v6stat
))
1181 /* Now for some sanity checks */
1183 if (v6stat
.zero_pos
== -1) {
1184 /* If no '::' must have exactly 16 bytes */
1185 if (v6stat
.total
!= 16)
1188 /* If '::' must have less than 16 bytes */
1189 if (v6stat
.total
== 16)
1191 /* More than three zeroes is an error */
1192 if (v6stat
.zero_cnt
> 3) {
1194 /* Can only have three zeroes if nothing else present */
1195 } else if (v6stat
.zero_cnt
== 3) {
1196 if (v6stat
.total
> 0)
1198 } else if (v6stat
.zero_cnt
== 2) {
1199 /* Can only have two zeroes if at start or end */
1200 if ((v6stat
.zero_pos
!= 0)
1201 && (v6stat
.zero_pos
!= v6stat
.total
))
1204 /* Can only have one zero if *not* start or end */
1205 if ((v6stat
.zero_pos
== 0)
1206 || (v6stat
.zero_pos
== v6stat
.total
))
1213 if (v6stat
.zero_pos
>= 0) {
1214 /* Copy initial part */
1215 memcpy(v6
, v6stat
.tmp
, v6stat
.zero_pos
);
1217 memset(v6
+ v6stat
.zero_pos
, 0, 16 - v6stat
.total
);
1218 /* Copy final part */
1219 if (v6stat
.total
!= v6stat
.zero_pos
)
1220 memcpy(v6
+ v6stat
.zero_pos
+ 16 - v6stat
.total
,
1221 v6stat
.tmp
+ v6stat
.zero_pos
,
1222 v6stat
.total
- v6stat
.zero_pos
);
1224 memcpy(v6
, v6stat
.tmp
, 16);
1230 static int ipv6_cb(const char *elem
, int len
, void *usr
)
1234 /* Error if 16 bytes written */
1238 /* Zero length element, corresponds to '::' */
1239 if (s
->zero_pos
== -1)
1240 s
->zero_pos
= s
->total
;
1241 /* If we've already got a :: its an error */
1242 else if (s
->zero_pos
!= s
->total
)
1246 /* If more than 4 characters could be final a.b.c.d form */
1248 /* Need at least 4 bytes left */
1251 /* Must be end of string */
1254 if (!ipv4_from_asc(s
->tmp
+ s
->total
, elem
))
1258 if (!ipv6_hex(s
->tmp
+ s
->total
, elem
, len
))
1267 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1270 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
)
1273 unsigned int num
= 0;
1281 x
= OPENSSL_hexchar2int(c
);
1287 out
[1] = num
& 0xff;
1291 int X509V3_NAME_from_section(X509_NAME
*nm
, STACK_OF(CONF_VALUE
) *dn_sk
,
1292 unsigned long chtype
)
1295 int i
, mval
, spec_char
, plus_char
;
1301 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1302 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1305 * Skip past any leading X. X: X, etc to allow for multiple instances
1307 for (p
= type
; *p
; p
++) {
1308 #ifndef CHARSET_EBCDIC
1309 spec_char
= ((*p
== ':') || (*p
== ',') || (*p
== '.'));
1311 spec_char
= ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1312 || (*p
== os_toascii
['.']));
1321 #ifndef CHARSET_EBCDIC
1322 plus_char
= (*type
== '+');
1324 plus_char
= (*type
== os_toascii
['+']);
1332 if (!X509_NAME_add_entry_by_txt(nm
, type
, chtype
,
1333 (unsigned char *)v
->value
, -1, -1,