2 * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* X509 v3 extension utilities */
13 #include "internal/cryptlib.h"
15 #include "internal/ctype.h"
16 #include <openssl/conf.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 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, const ASN1_ENUMERATED
*a
)
105 BIGNUM
*bntmp
= NULL
;
110 if ((bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) == NULL
111 || (strtmp
= BN_bn2dec(bntmp
)) == NULL
)
112 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED
, ERR_R_MALLOC_FAILURE
);
117 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const ASN1_INTEGER
*a
)
119 BIGNUM
*bntmp
= NULL
;
124 if ((bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) == NULL
125 || (strtmp
= BN_bn2dec(bntmp
)) == NULL
)
126 X509V3err(X509V3_F_I2S_ASN1_INTEGER
, ERR_R_MALLOC_FAILURE
);
131 ASN1_INTEGER
*s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, const char *value
)
138 X509V3err(X509V3_F_S2I_ASN1_INTEGER
, X509V3_R_INVALID_NULL_VALUE
);
143 X509V3err(X509V3_F_S2I_ASN1_INTEGER
, ERR_R_MALLOC_FAILURE
);
146 if (value
[0] == '-') {
152 if (value
[0] == '0' && ((value
[1] == 'x') || (value
[1] == 'X'))) {
159 ret
= BN_hex2bn(&bn
, value
);
161 ret
= BN_dec2bn(&bn
, value
);
163 if (!ret
|| value
[ret
]) {
165 X509V3err(X509V3_F_S2I_ASN1_INTEGER
, X509V3_R_BN_DEC2BN_ERROR
);
169 if (isneg
&& BN_is_zero(bn
))
172 aint
= BN_to_ASN1_INTEGER(bn
, NULL
);
175 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,
176 X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
180 aint
->type
|= V_ASN1_NEG
;
184 int X509V3_add_value_int(const char *name
, const ASN1_INTEGER
*aint
,
185 STACK_OF(CONF_VALUE
) **extlist
)
192 if ((strtmp
= i2s_ASN1_INTEGER(NULL
, aint
)) == NULL
)
194 ret
= X509V3_add_value(name
, strtmp
, extlist
);
195 OPENSSL_free(strtmp
);
199 int X509V3_get_value_bool(const CONF_VALUE
*value
, int *asn1_bool
)
203 if ((btmp
= value
->value
) == NULL
)
205 if (strcmp(btmp
, "TRUE") == 0
206 || strcmp(btmp
, "true") == 0
207 || strcmp(btmp
, "Y") == 0
208 || strcmp(btmp
, "y") == 0
209 || strcmp(btmp
, "YES") == 0
210 || strcmp(btmp
, "yes") == 0) {
214 if (strcmp(btmp
, "FALSE") == 0
215 || strcmp(btmp
, "false") == 0
216 || strcmp(btmp
, "N") == 0
217 || strcmp(btmp
, "n") == 0
218 || strcmp(btmp
, "NO") == 0
219 || strcmp(btmp
, "no") == 0) {
224 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL
,
225 X509V3_R_INVALID_BOOLEAN_STRING
);
226 X509V3_conf_err(value
);
230 int X509V3_get_value_int(const CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
234 if ((itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
)) == NULL
) {
235 X509V3_conf_err(value
);
249 STACK_OF(CONF_VALUE
) *X509V3_parse_list(const char *line
)
253 STACK_OF(CONF_VALUE
) *values
= NULL
;
256 /* We are going to modify the line so copy it first */
257 linebuf
= OPENSSL_strdup(line
);
258 if (linebuf
== NULL
) {
259 X509V3err(X509V3_F_X509V3_PARSE_LIST
, ERR_R_MALLOC_FAILURE
);
264 /* Go through all characters */
265 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') && (c
!= '\n');
273 ntmp
= strip_spaces(q
);
275 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
276 X509V3_R_INVALID_NULL_NAME
);
280 } else if (c
== ',') {
282 ntmp
= strip_spaces(q
);
285 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
286 X509V3_R_INVALID_NULL_NAME
);
289 X509V3_add_value(ntmp
, NULL
, &values
);
297 vtmp
= strip_spaces(q
);
299 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
300 X509V3_R_INVALID_NULL_VALUE
);
303 X509V3_add_value(ntmp
, vtmp
, &values
);
311 if (state
== HDR_VALUE
) {
312 vtmp
= strip_spaces(q
);
314 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
315 X509V3_R_INVALID_NULL_VALUE
);
318 X509V3_add_value(ntmp
, vtmp
, &values
);
320 ntmp
= strip_spaces(q
);
322 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
325 X509V3_add_value(ntmp
, NULL
, &values
);
327 OPENSSL_free(linebuf
);
331 OPENSSL_free(linebuf
);
332 sk_CONF_VALUE_pop_free(values
, X509V3_conf_free
);
337 /* Delete leading and trailing spaces from a string */
338 static char *strip_spaces(char *name
)
341 /* Skip over leading spaces */
343 while (*p
&& ossl_isspace(*p
))
347 q
= p
+ strlen(p
) - 1;
348 while ((q
!= p
) && ossl_isspace(*q
))
359 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
362 int name_cmp(const char *name
, const char *cmp
)
367 if ((ret
= strncmp(name
, cmp
, len
)))
370 if (!c
|| (c
== '.'))
375 static int sk_strcmp(const char *const *a
, const char *const *b
)
377 return strcmp(*a
, *b
);
380 STACK_OF(OPENSSL_STRING
) *X509_get1_email(X509
*x
)
383 STACK_OF(OPENSSL_STRING
) *ret
;
385 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
386 ret
= get_email(X509_get_subject_name(x
), gens
);
387 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
391 STACK_OF(OPENSSL_STRING
) *X509_get1_ocsp(X509
*x
)
393 AUTHORITY_INFO_ACCESS
*info
;
394 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
397 info
= X509_get_ext_d2i(x
, NID_info_access
, NULL
, NULL
);
400 for (i
= 0; i
< sk_ACCESS_DESCRIPTION_num(info
); i
++) {
401 ACCESS_DESCRIPTION
*ad
= sk_ACCESS_DESCRIPTION_value(info
, i
);
402 if (OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
) {
403 if (ad
->location
->type
== GEN_URI
) {
405 (&ret
, ad
->location
->d
.uniformResourceIdentifier
))
410 AUTHORITY_INFO_ACCESS_free(info
);
414 STACK_OF(OPENSSL_STRING
) *X509_REQ_get1_email(X509_REQ
*x
)
417 STACK_OF(X509_EXTENSION
) *exts
;
418 STACK_OF(OPENSSL_STRING
) *ret
;
420 exts
= X509_REQ_get_extensions(x
);
421 gens
= X509V3_get_d2i(exts
, NID_subject_alt_name
, NULL
, NULL
);
422 ret
= get_email(X509_REQ_get_subject_name(x
), gens
);
423 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
424 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
428 static STACK_OF(OPENSSL_STRING
) *get_email(X509_NAME
*name
,
431 STACK_OF(OPENSSL_STRING
) *ret
= NULL
;
433 const ASN1_IA5STRING
*email
;
437 /* Now add any email address(es) to STACK */
438 /* First supplied X509_NAME */
439 while ((i
= X509_NAME_get_index_by_NID(name
,
440 NID_pkcs9_emailAddress
, i
)) >= 0) {
441 ne
= X509_NAME_get_entry(name
, i
);
442 email
= X509_NAME_ENTRY_get_data(ne
);
443 if (!append_ia5(&ret
, email
))
446 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
447 gen
= sk_GENERAL_NAME_value(gens
, i
);
448 if (gen
->type
!= GEN_EMAIL
)
450 if (!append_ia5(&ret
, gen
->d
.ia5
))
456 static void str_free(OPENSSL_STRING str
)
461 static int append_ia5(STACK_OF(OPENSSL_STRING
) **sk
, const ASN1_IA5STRING
*email
)
464 /* First some sanity checks */
465 if (email
->type
!= V_ASN1_IA5STRING
)
467 if (!email
->data
|| !email
->length
)
470 *sk
= sk_OPENSSL_STRING_new(sk_strcmp
);
473 /* Don't add duplicates */
474 if (sk_OPENSSL_STRING_find(*sk
, (char *)email
->data
) != -1)
476 emtmp
= OPENSSL_strdup((char *)email
->data
);
477 if (emtmp
== NULL
|| !sk_OPENSSL_STRING_push(*sk
, emtmp
)) {
478 OPENSSL_free(emtmp
); /* free on push failure */
479 X509_email_free(*sk
);
486 void X509_email_free(STACK_OF(OPENSSL_STRING
) *sk
)
488 sk_OPENSSL_STRING_pop_free(sk
, str_free
);
491 typedef int (*equal_fn
) (const unsigned char *pattern
, size_t pattern_len
,
492 const unsigned char *subject
, size_t subject_len
,
495 /* Skip pattern prefix to match "wildcard" subject */
496 static void skip_prefix(const unsigned char **p
, size_t *plen
,
500 const unsigned char *pattern
= *p
;
501 size_t pattern_len
= *plen
;
504 * If subject starts with a leading '.' followed by more octets, and
505 * pattern is longer, compare just an equal-length suffix with the
506 * full subject (starting at the '.'), provided the prefix contains
509 if ((flags
& _X509_CHECK_FLAG_DOT_SUBDOMAINS
) == 0)
512 while (pattern_len
> subject_len
&& *pattern
) {
513 if ((flags
& X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
) &&
520 /* Skip if entire prefix acceptable */
521 if (pattern_len
== subject_len
) {
527 /* Compare while ASCII ignoring case. */
528 static int equal_nocase(const unsigned char *pattern
, size_t pattern_len
,
529 const unsigned char *subject
, size_t subject_len
,
532 skip_prefix(&pattern
, &pattern_len
, subject_len
, flags
);
533 if (pattern_len
!= subject_len
)
535 while (pattern_len
) {
536 unsigned char l
= *pattern
;
537 unsigned char r
= *subject
;
538 /* The pattern must not contain NUL characters. */
542 if ('A' <= l
&& l
<= 'Z')
544 if ('A' <= r
&& r
<= 'Z')
556 /* Compare using memcmp. */
557 static int equal_case(const unsigned char *pattern
, size_t pattern_len
,
558 const unsigned char *subject
, size_t subject_len
,
561 skip_prefix(&pattern
, &pattern_len
, subject_len
, flags
);
562 if (pattern_len
!= subject_len
)
564 return !memcmp(pattern
, subject
, pattern_len
);
568 * RFC 5280, section 7.5, requires that only the domain is compared in a
569 * case-insensitive manner.
571 static int equal_email(const unsigned char *a
, size_t a_len
,
572 const unsigned char *b
, size_t b_len
,
573 unsigned int unused_flags
)
579 * We search backwards for the '@' character, so that we do not have to
580 * deal with quoted local-parts. The domain part is compared in a
581 * case-insensitive manner.
585 if (a
[i
] == '@' || b
[i
] == '@') {
586 if (!equal_nocase(a
+ i
, a_len
- i
, b
+ i
, a_len
- i
, 0))
593 return equal_case(a
, i
, b
, i
, 0);
597 * Compare the prefix and suffix with the subject, and check that the
598 * characters in-between are valid.
600 static int wildcard_match(const unsigned char *prefix
, size_t prefix_len
,
601 const unsigned char *suffix
, size_t suffix_len
,
602 const unsigned char *subject
, size_t subject_len
,
605 const unsigned char *wildcard_start
;
606 const unsigned char *wildcard_end
;
607 const unsigned char *p
;
611 if (subject_len
< prefix_len
+ suffix_len
)
613 if (!equal_nocase(prefix
, prefix_len
, subject
, prefix_len
, flags
))
615 wildcard_start
= subject
+ prefix_len
;
616 wildcard_end
= subject
+ (subject_len
- suffix_len
);
617 if (!equal_nocase(wildcard_end
, suffix_len
, suffix
, suffix_len
, flags
))
620 * If the wildcard makes up the entire first label, it must match at
621 * least one character.
623 if (prefix_len
== 0 && *suffix
== '.') {
624 if (wildcard_start
== wildcard_end
)
627 if (flags
& X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
)
630 /* IDNA labels cannot match partial wildcards */
632 subject_len
>= 4 && strncasecmp((char *)subject
, "xn--", 4) == 0)
634 /* The wildcard may match a literal '*' */
635 if (wildcard_end
== wildcard_start
+ 1 && *wildcard_start
== '*')
638 * Check that the part matched by the wildcard contains only
639 * permitted characters and only matches a single label unless
640 * allow_multi is set.
642 for (p
= wildcard_start
; p
!= wildcard_end
; ++p
)
643 if (!(('0' <= *p
&& *p
<= '9') ||
644 ('A' <= *p
&& *p
<= 'Z') ||
645 ('a' <= *p
&& *p
<= 'z') ||
646 *p
== '-' || (allow_multi
&& *p
== '.')))
651 #define LABEL_START (1 << 0)
652 #define LABEL_END (1 << 1)
653 #define LABEL_HYPHEN (1 << 2)
654 #define LABEL_IDNA (1 << 3)
656 static const unsigned char *valid_star(const unsigned char *p
, size_t len
,
659 const unsigned char *star
= 0;
661 int state
= LABEL_START
;
663 for (i
= 0; i
< len
; ++i
) {
665 * Locate first and only legal wildcard, either at the start
666 * or end of a non-IDNA first and not final label.
669 int atstart
= (state
& LABEL_START
);
670 int atend
= (i
== len
- 1 || p
[i
+ 1] == '.');
672 * At most one wildcard per pattern.
673 * No wildcards in IDNA labels.
674 * No wildcards after the first label.
676 if (star
!= NULL
|| (state
& LABEL_IDNA
) != 0 || dots
)
678 /* Only full-label '*.example.com' wildcards? */
679 if ((flags
& X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
)
680 && (!atstart
|| !atend
))
682 /* No 'foo*bar' wildcards */
683 if (!atstart
&& !atend
)
686 state
&= ~LABEL_START
;
687 } else if (('a' <= p
[i
] && p
[i
] <= 'z')
688 || ('A' <= p
[i
] && p
[i
] <= 'Z')
689 || ('0' <= p
[i
] && p
[i
] <= '9')) {
690 if ((state
& LABEL_START
) != 0
691 && len
- i
>= 4 && strncasecmp((char *)&p
[i
], "xn--", 4) == 0)
693 state
&= ~(LABEL_HYPHEN
| LABEL_START
);
694 } else if (p
[i
] == '.') {
695 if ((state
& (LABEL_HYPHEN
| LABEL_START
)) != 0)
699 } else if (p
[i
] == '-') {
700 /* no domain/subdomain starts with '-' */
701 if ((state
& LABEL_START
) != 0)
703 state
|= LABEL_HYPHEN
;
709 * The final label must not end in a hyphen or ".", and
710 * there must be at least two dots after the star.
712 if ((state
& (LABEL_START
| LABEL_HYPHEN
)) != 0 || dots
< 2)
717 /* Compare using wildcards. */
718 static int equal_wildcard(const unsigned char *pattern
, size_t pattern_len
,
719 const unsigned char *subject
, size_t subject_len
,
722 const unsigned char *star
= NULL
;
725 * Subject names starting with '.' can only match a wildcard pattern
726 * via a subject sub-domain pattern suffix match.
728 if (!(subject_len
> 1 && subject
[0] == '.'))
729 star
= valid_star(pattern
, pattern_len
, flags
);
731 return equal_nocase(pattern
, pattern_len
,
732 subject
, subject_len
, flags
);
733 return wildcard_match(pattern
, star
- pattern
,
734 star
+ 1, (pattern
+ pattern_len
) - star
- 1,
735 subject
, subject_len
, flags
);
739 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
740 * cmp_type > 0 only compare if string matches the type, otherwise convert it
744 static int do_check_string(const ASN1_STRING
*a
, int cmp_type
, equal_fn equal
,
745 unsigned int flags
, const char *b
, size_t blen
,
750 if (!a
->data
|| !a
->length
)
753 if (cmp_type
!= a
->type
)
755 if (cmp_type
== V_ASN1_IA5STRING
)
756 rv
= equal(a
->data
, a
->length
, (unsigned char *)b
, blen
, flags
);
757 else if (a
->length
== (int)blen
&& !memcmp(a
->data
, b
, blen
))
759 if (rv
> 0 && peername
)
760 *peername
= OPENSSL_strndup((char *)a
->data
, a
->length
);
764 astrlen
= ASN1_STRING_to_UTF8(&astr
, a
);
767 * -1 could be an internal malloc failure or a decoding error from
768 * malformed input; we can't distinguish.
772 rv
= equal(astr
, astrlen
, (unsigned char *)b
, blen
, flags
);
773 if (rv
> 0 && peername
)
774 *peername
= OPENSSL_strndup((char *)astr
, astrlen
);
780 static int do_x509_check(X509
*x
, const char *chk
, size_t chklen
,
781 unsigned int flags
, int check_type
, char **peername
)
783 GENERAL_NAMES
*gens
= NULL
;
784 X509_NAME
*name
= NULL
;
786 int cnid
= NID_undef
;
792 /* See below, this flag is internal-only */
793 flags
&= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS
;
794 if (check_type
== GEN_EMAIL
) {
795 cnid
= NID_pkcs9_emailAddress
;
796 alt_type
= V_ASN1_IA5STRING
;
798 } else if (check_type
== GEN_DNS
) {
799 cnid
= NID_commonName
;
800 /* Implicit client-side DNS sub-domain pattern */
801 if (chklen
> 1 && chk
[0] == '.')
802 flags
|= _X509_CHECK_FLAG_DOT_SUBDOMAINS
;
803 alt_type
= V_ASN1_IA5STRING
;
804 if (flags
& X509_CHECK_FLAG_NO_WILDCARDS
)
805 equal
= equal_nocase
;
807 equal
= equal_wildcard
;
809 alt_type
= V_ASN1_OCTET_STRING
;
814 chklen
= strlen(chk
);
816 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
818 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
821 gen
= sk_GENERAL_NAME_value(gens
, i
);
822 if (gen
->type
!= check_type
)
825 if (check_type
== GEN_EMAIL
)
826 cstr
= gen
->d
.rfc822Name
;
827 else if (check_type
== GEN_DNS
)
828 cstr
= gen
->d
.dNSName
;
830 cstr
= gen
->d
.iPAddress
;
831 /* Positive on success, negative on error! */
832 if ((rv
= do_check_string(cstr
, alt_type
, equal
, flags
,
833 chk
, chklen
, peername
)) != 0)
836 GENERAL_NAMES_free(gens
);
839 if (san_present
&& !(flags
& X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
))
843 /* We're done if CN-ID is not pertinent */
844 if (cnid
== NID_undef
|| (flags
& X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
))
848 name
= X509_get_subject_name(x
);
849 while ((i
= X509_NAME_get_index_by_NID(name
, cnid
, i
)) >= 0) {
850 const X509_NAME_ENTRY
*ne
= X509_NAME_get_entry(name
, i
);
851 const ASN1_STRING
*str
= X509_NAME_ENTRY_get_data(ne
);
853 /* Positive on success, negative on error! */
854 if ((rv
= do_check_string(str
, -1, equal
, flags
,
855 chk
, chklen
, peername
)) != 0)
861 int X509_check_host(X509
*x
, const char *chk
, size_t chklen
,
862 unsigned int flags
, char **peername
)
867 * Embedded NULs are disallowed, except as the last character of a
868 * string of length 2 or more (tolerate caller including terminating
869 * NUL in string length).
872 chklen
= strlen(chk
);
873 else if (memchr(chk
, '\0', chklen
> 1 ? chklen
- 1 : chklen
))
875 if (chklen
> 1 && chk
[chklen
- 1] == '\0')
877 return do_x509_check(x
, chk
, chklen
, flags
, GEN_DNS
, peername
);
880 int X509_check_email(X509
*x
, const char *chk
, size_t chklen
,
886 * Embedded NULs are disallowed, except as the last character of a
887 * string of length 2 or more (tolerate caller including terminating
888 * NUL in string length).
891 chklen
= strlen((char *)chk
);
892 else if (memchr(chk
, '\0', chklen
> 1 ? chklen
- 1 : chklen
))
894 if (chklen
> 1 && chk
[chklen
- 1] == '\0')
896 return do_x509_check(x
, chk
, chklen
, flags
, GEN_EMAIL
, NULL
);
899 int X509_check_ip(X509
*x
, const unsigned char *chk
, size_t chklen
,
904 return do_x509_check(x
, (char *)chk
, chklen
, flags
, GEN_IPADD
, NULL
);
907 int X509_check_ip_asc(X509
*x
, const char *ipasc
, unsigned int flags
)
909 unsigned char ipout
[16];
914 iplen
= (size_t)a2i_ipadd(ipout
, ipasc
);
917 return do_x509_check(x
, (char *)ipout
, iplen
, flags
, GEN_IPADD
, NULL
);
921 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
925 ASN1_OCTET_STRING
*a2i_IPADDRESS(const char *ipasc
)
927 unsigned char ipout
[16];
928 ASN1_OCTET_STRING
*ret
;
931 /* If string contains a ':' assume IPv6 */
933 iplen
= a2i_ipadd(ipout
, ipasc
);
938 ret
= ASN1_OCTET_STRING_new();
941 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen
)) {
942 ASN1_OCTET_STRING_free(ret
);
948 ASN1_OCTET_STRING
*a2i_IPADDRESS_NC(const char *ipasc
)
950 ASN1_OCTET_STRING
*ret
= NULL
;
951 unsigned char ipout
[32];
952 char *iptmp
= NULL
, *p
;
954 p
= strchr(ipasc
, '/');
957 iptmp
= OPENSSL_strdup(ipasc
);
960 p
= iptmp
+ (p
- ipasc
);
963 iplen1
= a2i_ipadd(ipout
, iptmp
);
968 iplen2
= a2i_ipadd(ipout
+ iplen1
, p
);
973 if (!iplen2
|| (iplen1
!= iplen2
))
976 ret
= ASN1_OCTET_STRING_new();
979 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen1
+ iplen2
))
986 ASN1_OCTET_STRING_free(ret
);
990 int a2i_ipadd(unsigned char *ipout
, const char *ipasc
)
992 /* If string contains a ':' assume IPv6 */
994 if (strchr(ipasc
, ':')) {
995 if (!ipv6_from_asc(ipout
, ipasc
))
999 if (!ipv4_from_asc(ipout
, ipasc
))
1005 static int ipv4_from_asc(unsigned char *v4
, const char *in
)
1008 if (sscanf(in
, "%d.%d.%d.%d", &a0
, &a1
, &a2
, &a3
) != 4)
1010 if ((a0
< 0) || (a0
> 255) || (a1
< 0) || (a1
> 255)
1011 || (a2
< 0) || (a2
> 255) || (a3
< 0) || (a3
> 255))
1021 /* Temporary store for IPV6 output */
1022 unsigned char tmp
[16];
1023 /* Total number of bytes in tmp */
1025 /* The position of a zero (corresponding to '::') */
1027 /* Number of zeroes */
1031 static int ipv6_from_asc(unsigned char *v6
, const char *in
)
1035 v6stat
.zero_pos
= -1;
1036 v6stat
.zero_cnt
= 0;
1038 * Treat the IPv6 representation as a list of values separated by ':'.
1039 * The presence of a '::' will parse as one, two or three zero length
1042 if (!CONF_parse_list(in
, ':', 0, ipv6_cb
, &v6stat
))
1045 /* Now for some sanity checks */
1047 if (v6stat
.zero_pos
== -1) {
1048 /* If no '::' must have exactly 16 bytes */
1049 if (v6stat
.total
!= 16)
1052 /* If '::' must have less than 16 bytes */
1053 if (v6stat
.total
== 16)
1055 /* More than three zeroes is an error */
1056 if (v6stat
.zero_cnt
> 3)
1058 /* Can only have three zeroes if nothing else present */
1059 else if (v6stat
.zero_cnt
== 3) {
1060 if (v6stat
.total
> 0)
1063 /* Can only have two zeroes if at start or end */
1064 else if (v6stat
.zero_cnt
== 2) {
1065 if ((v6stat
.zero_pos
!= 0)
1066 && (v6stat
.zero_pos
!= v6stat
.total
))
1069 /* Can only have one zero if *not* start or end */
1071 if ((v6stat
.zero_pos
== 0)
1072 || (v6stat
.zero_pos
== v6stat
.total
))
1079 if (v6stat
.zero_pos
>= 0) {
1080 /* Copy initial part */
1081 memcpy(v6
, v6stat
.tmp
, v6stat
.zero_pos
);
1083 memset(v6
+ v6stat
.zero_pos
, 0, 16 - v6stat
.total
);
1084 /* Copy final part */
1085 if (v6stat
.total
!= v6stat
.zero_pos
)
1086 memcpy(v6
+ v6stat
.zero_pos
+ 16 - v6stat
.total
,
1087 v6stat
.tmp
+ v6stat
.zero_pos
,
1088 v6stat
.total
- v6stat
.zero_pos
);
1090 memcpy(v6
, v6stat
.tmp
, 16);
1095 static int ipv6_cb(const char *elem
, int len
, void *usr
)
1098 /* Error if 16 bytes written */
1102 /* Zero length element, corresponds to '::' */
1103 if (s
->zero_pos
== -1)
1104 s
->zero_pos
= s
->total
;
1105 /* If we've already got a :: its an error */
1106 else if (s
->zero_pos
!= s
->total
)
1110 /* If more than 4 characters could be final a.b.c.d form */
1112 /* Need at least 4 bytes left */
1115 /* Must be end of string */
1118 if (!ipv4_from_asc(s
->tmp
+ s
->total
, elem
))
1122 if (!ipv6_hex(s
->tmp
+ s
->total
, elem
, len
))
1131 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
1134 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
)
1137 unsigned int num
= 0;
1145 x
= OPENSSL_hexchar2int(c
);
1151 out
[1] = num
& 0xff;
1155 int X509V3_NAME_from_section(X509_NAME
*nm
, STACK_OF(CONF_VALUE
) *dn_sk
,
1156 unsigned long chtype
)
1159 int i
, mval
, spec_char
, plus_char
;
1164 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1165 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1168 * Skip past any leading X. X: X, etc to allow for multiple instances
1170 for (p
= type
; *p
; p
++) {
1171 #ifndef CHARSET_EBCDIC
1172 spec_char
= ((*p
== ':') || (*p
== ',') || (*p
== '.'));
1174 spec_char
= ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1175 || (*p
== os_toascii
['.']));
1184 #ifndef CHARSET_EBCDIC
1185 plus_char
= (*type
== '+');
1187 plus_char
= (*type
== os_toascii
['+']);
1194 if (!X509_NAME_add_entry_by_txt(nm
, type
, chtype
,
1195 (unsigned char *)v
->value
, -1, -1,