1 /* $OpenBSD: tls_verify.c,v 1.18 2016/11/04 15:32:40 jsing Exp $ */
3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include <openssl/x509v3.h>
28 #include "tls_internal.h"
30 static int tls_match_name(const char *cert_name
, const char *name
);
31 static int tls_check_subject_altname(struct tls
*ctx
, X509
*cert
,
33 static int tls_check_common_name(struct tls
*ctx
, X509
*cert
, const char *name
);
36 tls_match_name(const char *cert_name
, const char *name
)
38 const char *cert_domain
, *domain
, *next_dot
;
40 if (strcasecmp(cert_name
, name
) == 0)
44 if (cert_name
[0] == '*') {
48 * - "*.sub.domain.tld"
51 * No attempt to prevent the use of eg. "*.co.uk".
53 cert_domain
= &cert_name
[1];
55 if (cert_domain
[0] == '\0')
58 if (cert_domain
[0] != '.')
61 if (cert_domain
[1] == '.')
63 next_dot
= strchr(&cert_domain
[1], '.');
64 /* Disallow "*.bar" */
67 /* Disallow "*.bar.." */
68 if (next_dot
[1] == '.')
71 domain
= strchr(name
, '.');
73 /* No wildcard match against a name with no host part. */
76 /* No wildcard match against a name with no domain part. */
77 if (domain
== NULL
|| strlen(domain
) == 1)
80 if (strcasecmp(cert_domain
, domain
) == 0)
87 /* See RFC 5280 section 4.2.1.6 for SubjectAltName details. */
89 tls_check_subject_altname(struct tls
*ctx
, X509
*cert
, const char *name
)
91 STACK_OF(GENERAL_NAME
) *altname_stack
= NULL
;
92 union tls_addr addrbuf
;
97 altname_stack
= X509_get_ext_d2i(cert
, NID_subject_alt_name
,
99 if (altname_stack
== NULL
)
102 if (inet_pton(AF_INET
, name
, &addrbuf
) == 1) {
105 } else if (inet_pton(AF_INET6
, name
, &addrbuf
) == 1) {
113 count
= sk_GENERAL_NAME_num(altname_stack
);
114 for (i
= 0; i
< count
; i
++) {
115 GENERAL_NAME
*altname
;
117 altname
= sk_GENERAL_NAME_value(altname_stack
, i
);
118 if (altname
->type
!= type
)
121 if (type
== GEN_DNS
) {
125 format
= ASN1_STRING_type(altname
->d
.dNSName
);
126 if (format
== V_ASN1_IA5STRING
) {
127 data
= ASN1_STRING_data(altname
->d
.dNSName
);
128 len
= ASN1_STRING_length(altname
->d
.dNSName
);
130 if (len
< 0 || (size_t)len
!= strlen(data
)) {
132 "error verifying name '%s': "
133 "NUL byte in subjectAltName, "
134 "probably a malicious certificate",
141 * Per RFC 5280 section 4.2.1.6:
142 * " " is a legal domain name, but that
143 * dNSName must be rejected.
145 if (strcmp(data
, " ") == 0) {
147 "error verifying name '%s': "
148 "a dNSName of \" \" must not be "
154 if (tls_match_name(data
, name
) == 0) {
160 fprintf(stdout
, "%s: unhandled subjectAltName "
161 "dNSName encoding (%d)\n", getprogname(),
166 } else if (type
== GEN_IPADD
) {
170 datalen
= ASN1_STRING_length(altname
->d
.iPAddress
);
171 data
= ASN1_STRING_data(altname
->d
.iPAddress
);
175 "Unexpected negative length for an "
176 "IP address: %d", datalen
);
182 * Per RFC 5280 section 4.2.1.6:
183 * IPv4 must use 4 octets and IPv6 must use 16 octets.
185 if (datalen
== addrlen
&&
186 memcmp(data
, &addrbuf
, addrlen
) == 0) {
193 sk_GENERAL_NAME_pop_free(altname_stack
, GENERAL_NAME_free
);
198 tls_check_common_name(struct tls
*ctx
, X509
*cert
, const char *name
)
200 X509_NAME
*subject_name
;
201 char *common_name
= NULL
;
202 union tls_addr addrbuf
;
206 subject_name
= X509_get_subject_name(cert
);
207 if (subject_name
== NULL
)
210 common_name_len
= X509_NAME_get_text_by_NID(subject_name
,
211 NID_commonName
, NULL
, 0);
212 if (common_name_len
< 0)
215 common_name
= calloc(common_name_len
+ 1, 1);
216 if (common_name
== NULL
)
219 X509_NAME_get_text_by_NID(subject_name
, NID_commonName
, common_name
,
220 common_name_len
+ 1);
222 /* NUL bytes in CN? */
223 if (common_name_len
< 0 ||
224 (size_t)common_name_len
!= strlen(common_name
)) {
225 tls_set_errorx(ctx
, "error verifying name '%s': "
226 "NUL byte in Common Name field, "
227 "probably a malicious certificate", name
);
232 if (inet_pton(AF_INET
, name
, &addrbuf
) == 1 ||
233 inet_pton(AF_INET6
, name
, &addrbuf
) == 1) {
235 * We don't want to attempt wildcard matching against IP
236 * addresses, so perform a simple comparison here.
238 if (strcmp(common_name
, name
) == 0)
245 if (tls_match_name(common_name
, name
) == 0)
253 tls_check_name(struct tls
*ctx
, X509
*cert
, const char *name
)
257 rv
= tls_check_subject_altname(ctx
, cert
, name
);
258 if (rv
== 0 || rv
== -2)
261 return tls_check_common_name(ctx
, cert
, name
);