1 f9ab77a8 2023-08-23 op /* $OpenBSD: tls_verify.c,v 1.23 2023/05/11 07:35:27 tb Exp $ */
3 f9ab77a8 2023-08-23 op * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
5 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
6 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
7 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
9 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f9ab77a8 2023-08-23 op #include "config.h"
20 f9ab77a8 2023-08-23 op #include <sys/socket.h>
22 f9ab77a8 2023-08-23 op #include <arpa/inet.h>
23 f9ab77a8 2023-08-23 op #include <netinet/in.h>
25 f9ab77a8 2023-08-23 op #include <string.h>
27 f9ab77a8 2023-08-23 op #include <openssl/x509v3.h>
29 f9ab77a8 2023-08-23 op #include <tls.h>
30 f9ab77a8 2023-08-23 op #include "tls_internal.h"
33 f9ab77a8 2023-08-23 op tls_match_name(const char *cert_name, const char *name)
35 f9ab77a8 2023-08-23 op const char *cert_domain, *domain, *next_dot;
37 f9ab77a8 2023-08-23 op if (strcasecmp(cert_name, name) == 0)
40 f9ab77a8 2023-08-23 op /* Wildcard match? */
41 f9ab77a8 2023-08-23 op if (cert_name[0] == '*') {
43 f9ab77a8 2023-08-23 op * Valid wildcards:
44 f9ab77a8 2023-08-23 op * - "*.domain.tld"
45 f9ab77a8 2023-08-23 op * - "*.sub.domain.tld"
47 f9ab77a8 2023-08-23 op * Reject "*.tld".
48 f9ab77a8 2023-08-23 op * No attempt to prevent the use of eg. "*.co.uk".
50 f9ab77a8 2023-08-23 op cert_domain = &cert_name[1];
51 f9ab77a8 2023-08-23 op /* Disallow "*" */
52 f9ab77a8 2023-08-23 op if (cert_domain[0] == '\0')
54 f9ab77a8 2023-08-23 op /* Disallow "*foo" */
55 f9ab77a8 2023-08-23 op if (cert_domain[0] != '.')
57 f9ab77a8 2023-08-23 op /* Disallow "*.." */
58 f9ab77a8 2023-08-23 op if (cert_domain[1] == '.')
60 f9ab77a8 2023-08-23 op next_dot = strchr(&cert_domain[1], '.');
61 f9ab77a8 2023-08-23 op /* Disallow "*.bar" */
62 f9ab77a8 2023-08-23 op if (next_dot == NULL)
64 f9ab77a8 2023-08-23 op /* Disallow "*.bar.." */
65 f9ab77a8 2023-08-23 op if (next_dot[1] == '.')
68 f9ab77a8 2023-08-23 op domain = strchr(name, '.');
70 f9ab77a8 2023-08-23 op /* No wildcard match against a name with no host part. */
71 f9ab77a8 2023-08-23 op if (name[0] == '.')
73 f9ab77a8 2023-08-23 op /* No wildcard match against a name with no domain part. */
74 f9ab77a8 2023-08-23 op if (domain == NULL || strlen(domain) == 1)
77 f9ab77a8 2023-08-23 op if (strcasecmp(cert_domain, domain) == 0)
85 f9ab77a8 2023-08-23 op * See RFC 5280 section 4.2.1.6 for SubjectAltName details.
86 f9ab77a8 2023-08-23 op * alt_match is set to 1 if a matching alternate name is found.
87 f9ab77a8 2023-08-23 op * alt_exists is set to 1 if any known alternate name exists in the certificate.
90 f9ab77a8 2023-08-23 op tls_check_subject_altname(struct tls *ctx, X509 *cert, const char *name,
91 f9ab77a8 2023-08-23 op int *alt_match, int *alt_exists)
93 f9ab77a8 2023-08-23 op STACK_OF(GENERAL_NAME) *altname_stack = NULL;
94 f9ab77a8 2023-08-23 op union tls_addr addrbuf;
95 f9ab77a8 2023-08-23 op int addrlen, type;
99 f9ab77a8 2023-08-23 op *alt_match = 0;
100 f9ab77a8 2023-08-23 op *alt_exists = 0;
102 f9ab77a8 2023-08-23 op altname_stack = X509_get_ext_d2i(cert, NID_subject_alt_name,
104 f9ab77a8 2023-08-23 op if (altname_stack == NULL)
107 f9ab77a8 2023-08-23 op if (inet_pton(AF_INET, name, &addrbuf) == 1) {
108 f9ab77a8 2023-08-23 op type = GEN_IPADD;
110 f9ab77a8 2023-08-23 op } else if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
111 f9ab77a8 2023-08-23 op type = GEN_IPADD;
112 f9ab77a8 2023-08-23 op addrlen = 16;
114 f9ab77a8 2023-08-23 op type = GEN_DNS;
118 f9ab77a8 2023-08-23 op count = sk_GENERAL_NAME_num(altname_stack);
119 f9ab77a8 2023-08-23 op for (i = 0; i < count; i++) {
120 f9ab77a8 2023-08-23 op GENERAL_NAME *altname;
122 f9ab77a8 2023-08-23 op altname = sk_GENERAL_NAME_value(altname_stack, i);
124 f9ab77a8 2023-08-23 op if (altname->type == GEN_DNS || altname->type == GEN_IPADD)
125 f9ab77a8 2023-08-23 op *alt_exists = 1;
127 f9ab77a8 2023-08-23 op if (altname->type != type)
130 f9ab77a8 2023-08-23 op if (type == GEN_DNS) {
131 f9ab77a8 2023-08-23 op const unsigned char *data;
132 f9ab77a8 2023-08-23 op int format, len;
134 f9ab77a8 2023-08-23 op format = ASN1_STRING_type(altname->d.dNSName);
135 f9ab77a8 2023-08-23 op if (format == V_ASN1_IA5STRING) {
136 f9ab77a8 2023-08-23 op data = ASN1_STRING_get0_data(altname->d.dNSName);
137 f9ab77a8 2023-08-23 op len = ASN1_STRING_length(altname->d.dNSName);
139 f9ab77a8 2023-08-23 op if (len < 0 || (size_t)len != strlen(data)) {
140 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
141 f9ab77a8 2023-08-23 op "error verifying name '%s': "
142 f9ab77a8 2023-08-23 op "NUL byte in subjectAltName, "
143 f9ab77a8 2023-08-23 op "probably a malicious certificate",
150 f9ab77a8 2023-08-23 op * Per RFC 5280 section 4.2.1.6:
151 f9ab77a8 2023-08-23 op * " " is a legal domain name, but that
152 f9ab77a8 2023-08-23 op * dNSName must be rejected.
154 f9ab77a8 2023-08-23 op if (strcmp(data, " ") == 0) {
155 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
156 f9ab77a8 2023-08-23 op "error verifying name '%s': "
157 f9ab77a8 2023-08-23 op "a dNSName of \" \" must not be "
158 f9ab77a8 2023-08-23 op "used", name);
163 f9ab77a8 2023-08-23 op if (tls_match_name(data, name) == 0) {
164 f9ab77a8 2023-08-23 op *alt_match = 1;
169 f9ab77a8 2023-08-23 op fprintf(stdout, "%s: unhandled subjectAltName "
170 f9ab77a8 2023-08-23 op "dNSName encoding (%d)\n", getprogname(),
175 f9ab77a8 2023-08-23 op } else if (type == GEN_IPADD) {
176 f9ab77a8 2023-08-23 op const unsigned char *data;
179 f9ab77a8 2023-08-23 op datalen = ASN1_STRING_length(altname->d.iPAddress);
180 f9ab77a8 2023-08-23 op data = ASN1_STRING_get0_data(altname->d.iPAddress);
182 f9ab77a8 2023-08-23 op if (datalen < 0) {
183 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
184 f9ab77a8 2023-08-23 op "Unexpected negative length for an "
185 f9ab77a8 2023-08-23 op "IP address: %d", datalen);
191 f9ab77a8 2023-08-23 op * Per RFC 5280 section 4.2.1.6:
192 f9ab77a8 2023-08-23 op * IPv4 must use 4 octets and IPv6 must use 16 octets.
194 f9ab77a8 2023-08-23 op if (datalen == addrlen &&
195 f9ab77a8 2023-08-23 op memcmp(data, &addrbuf, addrlen) == 0) {
196 f9ab77a8 2023-08-23 op *alt_match = 1;
202 f9ab77a8 2023-08-23 op sk_GENERAL_NAME_pop_free(altname_stack, GENERAL_NAME_free);
207 f9ab77a8 2023-08-23 op tls_check_common_name(struct tls *ctx, X509 *cert, const char *name,
208 f9ab77a8 2023-08-23 op int *cn_match)
210 f9ab77a8 2023-08-23 op X509_NAME *subject_name;
211 f9ab77a8 2023-08-23 op char *common_name = NULL;
212 f9ab77a8 2023-08-23 op union tls_addr addrbuf;
213 f9ab77a8 2023-08-23 op int common_name_len;
216 f9ab77a8 2023-08-23 op *cn_match = 0;
218 f9ab77a8 2023-08-23 op subject_name = X509_get_subject_name(cert);
219 f9ab77a8 2023-08-23 op if (subject_name == NULL)
222 f9ab77a8 2023-08-23 op common_name_len = X509_NAME_get_text_by_NID(subject_name,
223 f9ab77a8 2023-08-23 op NID_commonName, NULL, 0);
224 f9ab77a8 2023-08-23 op if (common_name_len < 0)
227 f9ab77a8 2023-08-23 op common_name = calloc(common_name_len + 1, 1);
228 f9ab77a8 2023-08-23 op if (common_name == NULL) {
229 f9ab77a8 2023-08-23 op tls_set_error(ctx, "out of memory");
233 f9ab77a8 2023-08-23 op X509_NAME_get_text_by_NID(subject_name, NID_commonName, common_name,
234 f9ab77a8 2023-08-23 op common_name_len + 1);
236 f9ab77a8 2023-08-23 op /* NUL bytes in CN? */
237 f9ab77a8 2023-08-23 op if (common_name_len < 0 ||
238 f9ab77a8 2023-08-23 op (size_t)common_name_len != strlen(common_name)) {
239 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "error verifying name '%s': "
240 f9ab77a8 2023-08-23 op "NUL byte in Common Name field, "
241 f9ab77a8 2023-08-23 op "probably a malicious certificate", name);
246 f9ab77a8 2023-08-23 op * We don't want to attempt wildcard matching against IP addresses,
247 f9ab77a8 2023-08-23 op * so perform a simple comparison here.
249 f9ab77a8 2023-08-23 op if (inet_pton(AF_INET, name, &addrbuf) == 1 ||
250 f9ab77a8 2023-08-23 op inet_pton(AF_INET6, name, &addrbuf) == 1) {
251 f9ab77a8 2023-08-23 op if (strcmp(common_name, name) == 0)
252 f9ab77a8 2023-08-23 op *cn_match = 1;
256 f9ab77a8 2023-08-23 op if (tls_match_name(common_name, name) == 0)
257 f9ab77a8 2023-08-23 op *cn_match = 1;
263 f9ab77a8 2023-08-23 op free(common_name);
268 f9ab77a8 2023-08-23 op tls_check_name(struct tls *ctx, X509 *cert, const char *name, int *match)
270 f9ab77a8 2023-08-23 op int alt_exists;
274 f9ab77a8 2023-08-23 op if (tls_check_subject_altname(ctx, cert, name, match,
275 f9ab77a8 2023-08-23 op &alt_exists) == -1)
279 f9ab77a8 2023-08-23 op * As per RFC 6125 section 6.4.4, if any known alternate name existed
280 f9ab77a8 2023-08-23 op * in the certificate, we do not attempt to match on the CN.
282 f9ab77a8 2023-08-23 op if (*match || alt_exists)
285 f9ab77a8 2023-08-23 op return tls_check_common_name(ctx, cert, name, match);