Blame


1 ebfc5784 2024-01-07 op /* $OpenBSD: tls_verify.c,v 1.29 2023/11/22 18:23:09 op Exp $ */
2 f9ab77a8 2023-08-23 op /*
3 f9ab77a8 2023-08-23 op * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
4 f9ab77a8 2023-08-23 op *
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.
8 f9ab77a8 2023-08-23 op *
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.
16 f9ab77a8 2023-08-23 op */
17 f9ab77a8 2023-08-23 op
18 f9ab77a8 2023-08-23 op #include "config.h"
19 f9ab77a8 2023-08-23 op
20 f9ab77a8 2023-08-23 op #include <sys/socket.h>
21 f9ab77a8 2023-08-23 op
22 f9ab77a8 2023-08-23 op #include <arpa/inet.h>
23 f9ab77a8 2023-08-23 op #include <netinet/in.h>
24 f9ab77a8 2023-08-23 op
25 f9ab77a8 2023-08-23 op #include <string.h>
26 f9ab77a8 2023-08-23 op
27 f9ab77a8 2023-08-23 op #include <openssl/x509v3.h>
28 f9ab77a8 2023-08-23 op
29 f9ab77a8 2023-08-23 op #include <tls.h>
30 f9ab77a8 2023-08-23 op #include "tls_internal.h"
31 f9ab77a8 2023-08-23 op
32 f9ab77a8 2023-08-23 op static int
33 f9ab77a8 2023-08-23 op tls_match_name(const char *cert_name, const char *name)
34 f9ab77a8 2023-08-23 op {
35 f9ab77a8 2023-08-23 op const char *cert_domain, *domain, *next_dot;
36 f9ab77a8 2023-08-23 op
37 f9ab77a8 2023-08-23 op if (strcasecmp(cert_name, name) == 0)
38 f9ab77a8 2023-08-23 op return 0;
39 f9ab77a8 2023-08-23 op
40 f9ab77a8 2023-08-23 op /* Wildcard match? */
41 f9ab77a8 2023-08-23 op if (cert_name[0] == '*') {
42 f9ab77a8 2023-08-23 op /*
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"
46 f9ab77a8 2023-08-23 op * - etc.
47 f9ab77a8 2023-08-23 op * Reject "*.tld".
48 f9ab77a8 2023-08-23 op * No attempt to prevent the use of eg. "*.co.uk".
49 f9ab77a8 2023-08-23 op */
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')
53 f9ab77a8 2023-08-23 op return -1;
54 f9ab77a8 2023-08-23 op /* Disallow "*foo" */
55 f9ab77a8 2023-08-23 op if (cert_domain[0] != '.')
56 f9ab77a8 2023-08-23 op return -1;
57 f9ab77a8 2023-08-23 op /* Disallow "*.." */
58 f9ab77a8 2023-08-23 op if (cert_domain[1] == '.')
59 f9ab77a8 2023-08-23 op return -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)
63 f9ab77a8 2023-08-23 op return -1;
64 f9ab77a8 2023-08-23 op /* Disallow "*.bar.." */
65 f9ab77a8 2023-08-23 op if (next_dot[1] == '.')
66 f9ab77a8 2023-08-23 op return -1;
67 f9ab77a8 2023-08-23 op
68 f9ab77a8 2023-08-23 op domain = strchr(name, '.');
69 f9ab77a8 2023-08-23 op
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] == '.')
72 f9ab77a8 2023-08-23 op return -1;
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)
75 f9ab77a8 2023-08-23 op return -1;
76 f9ab77a8 2023-08-23 op
77 f9ab77a8 2023-08-23 op if (strcasecmp(cert_domain, domain) == 0)
78 f9ab77a8 2023-08-23 op return 0;
79 f9ab77a8 2023-08-23 op }
80 f9ab77a8 2023-08-23 op
81 f9ab77a8 2023-08-23 op return -1;
82 f9ab77a8 2023-08-23 op }
83 f9ab77a8 2023-08-23 op
84 f9ab77a8 2023-08-23 op /*
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.
88 f9ab77a8 2023-08-23 op */
89 f9ab77a8 2023-08-23 op static int
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)
92 f9ab77a8 2023-08-23 op {
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;
96 f9ab77a8 2023-08-23 op int count, i;
97 ebfc5784 2024-01-07 op int critical = 0;
98 ebfc5784 2024-01-07 op int rv = -1;
99 f9ab77a8 2023-08-23 op
100 f9ab77a8 2023-08-23 op *alt_match = 0;
101 f9ab77a8 2023-08-23 op *alt_exists = 0;
102 f9ab77a8 2023-08-23 op
103 ebfc5784 2024-01-07 op altname_stack = X509_get_ext_d2i(cert, NID_subject_alt_name, &critical,
104 ebfc5784 2024-01-07 op NULL);
105 ebfc5784 2024-01-07 op if (altname_stack == NULL) {
106 ebfc5784 2024-01-07 op if (critical != -1) {
107 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "error decoding subjectAltName");
108 ebfc5784 2024-01-07 op goto err;
109 ebfc5784 2024-01-07 op }
110 ebfc5784 2024-01-07 op goto done;
111 ebfc5784 2024-01-07 op }
112 f9ab77a8 2023-08-23 op
113 f9ab77a8 2023-08-23 op if (inet_pton(AF_INET, name, &addrbuf) == 1) {
114 f9ab77a8 2023-08-23 op type = GEN_IPADD;
115 f9ab77a8 2023-08-23 op addrlen = 4;
116 f9ab77a8 2023-08-23 op } else if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
117 f9ab77a8 2023-08-23 op type = GEN_IPADD;
118 f9ab77a8 2023-08-23 op addrlen = 16;
119 f9ab77a8 2023-08-23 op } else {
120 f9ab77a8 2023-08-23 op type = GEN_DNS;
121 f9ab77a8 2023-08-23 op addrlen = 0;
122 f9ab77a8 2023-08-23 op }
123 f9ab77a8 2023-08-23 op
124 f9ab77a8 2023-08-23 op count = sk_GENERAL_NAME_num(altname_stack);
125 f9ab77a8 2023-08-23 op for (i = 0; i < count; i++) {
126 f9ab77a8 2023-08-23 op GENERAL_NAME *altname;
127 f9ab77a8 2023-08-23 op
128 f9ab77a8 2023-08-23 op altname = sk_GENERAL_NAME_value(altname_stack, i);
129 f9ab77a8 2023-08-23 op
130 f9ab77a8 2023-08-23 op if (altname->type == GEN_DNS || altname->type == GEN_IPADD)
131 f9ab77a8 2023-08-23 op *alt_exists = 1;
132 f9ab77a8 2023-08-23 op
133 f9ab77a8 2023-08-23 op if (altname->type != type)
134 f9ab77a8 2023-08-23 op continue;
135 f9ab77a8 2023-08-23 op
136 f9ab77a8 2023-08-23 op if (type == GEN_DNS) {
137 f9ab77a8 2023-08-23 op const unsigned char *data;
138 f9ab77a8 2023-08-23 op int format, len;
139 f9ab77a8 2023-08-23 op
140 f9ab77a8 2023-08-23 op format = ASN1_STRING_type(altname->d.dNSName);
141 f9ab77a8 2023-08-23 op if (format == V_ASN1_IA5STRING) {
142 f9ab77a8 2023-08-23 op data = ASN1_STRING_get0_data(altname->d.dNSName);
143 f9ab77a8 2023-08-23 op len = ASN1_STRING_length(altname->d.dNSName);
144 f9ab77a8 2023-08-23 op
145 f9ab77a8 2023-08-23 op if (len < 0 || (size_t)len != strlen(data)) {
146 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
147 f9ab77a8 2023-08-23 op "error verifying name '%s': "
148 f9ab77a8 2023-08-23 op "NUL byte in subjectAltName, "
149 f9ab77a8 2023-08-23 op "probably a malicious certificate",
150 f9ab77a8 2023-08-23 op name);
151 ebfc5784 2024-01-07 op goto err;
152 f9ab77a8 2023-08-23 op }
153 f9ab77a8 2023-08-23 op
154 f9ab77a8 2023-08-23 op /*
155 f9ab77a8 2023-08-23 op * Per RFC 5280 section 4.2.1.6:
156 f9ab77a8 2023-08-23 op * " " is a legal domain name, but that
157 f9ab77a8 2023-08-23 op * dNSName must be rejected.
158 f9ab77a8 2023-08-23 op */
159 f9ab77a8 2023-08-23 op if (strcmp(data, " ") == 0) {
160 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
161 f9ab77a8 2023-08-23 op "error verifying name '%s': "
162 f9ab77a8 2023-08-23 op "a dNSName of \" \" must not be "
163 f9ab77a8 2023-08-23 op "used", name);
164 ebfc5784 2024-01-07 op goto err;
165 f9ab77a8 2023-08-23 op }
166 f9ab77a8 2023-08-23 op
167 f9ab77a8 2023-08-23 op if (tls_match_name(data, name) == 0) {
168 f9ab77a8 2023-08-23 op *alt_match = 1;
169 ebfc5784 2024-01-07 op goto done;
170 f9ab77a8 2023-08-23 op }
171 f9ab77a8 2023-08-23 op } else {
172 f9ab77a8 2023-08-23 op #ifdef DEBUG
173 f9ab77a8 2023-08-23 op fprintf(stdout, "%s: unhandled subjectAltName "
174 f9ab77a8 2023-08-23 op "dNSName encoding (%d)\n", getprogname(),
175 f9ab77a8 2023-08-23 op format);
176 f9ab77a8 2023-08-23 op #endif
177 f9ab77a8 2023-08-23 op }
178 f9ab77a8 2023-08-23 op
179 f9ab77a8 2023-08-23 op } else if (type == GEN_IPADD) {
180 f9ab77a8 2023-08-23 op const unsigned char *data;
181 f9ab77a8 2023-08-23 op int datalen;
182 f9ab77a8 2023-08-23 op
183 f9ab77a8 2023-08-23 op datalen = ASN1_STRING_length(altname->d.iPAddress);
184 f9ab77a8 2023-08-23 op data = ASN1_STRING_get0_data(altname->d.iPAddress);
185 f9ab77a8 2023-08-23 op
186 f9ab77a8 2023-08-23 op if (datalen < 0) {
187 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
188 f9ab77a8 2023-08-23 op "Unexpected negative length for an "
189 f9ab77a8 2023-08-23 op "IP address: %d", datalen);
190 ebfc5784 2024-01-07 op goto err;
191 f9ab77a8 2023-08-23 op }
192 f9ab77a8 2023-08-23 op
193 f9ab77a8 2023-08-23 op /*
194 f9ab77a8 2023-08-23 op * Per RFC 5280 section 4.2.1.6:
195 f9ab77a8 2023-08-23 op * IPv4 must use 4 octets and IPv6 must use 16 octets.
196 f9ab77a8 2023-08-23 op */
197 f9ab77a8 2023-08-23 op if (datalen == addrlen &&
198 f9ab77a8 2023-08-23 op memcmp(data, &addrbuf, addrlen) == 0) {
199 f9ab77a8 2023-08-23 op *alt_match = 1;
200 ebfc5784 2024-01-07 op goto done;
201 f9ab77a8 2023-08-23 op }
202 f9ab77a8 2023-08-23 op }
203 f9ab77a8 2023-08-23 op }
204 f9ab77a8 2023-08-23 op
205 ebfc5784 2024-01-07 op done:
206 ebfc5784 2024-01-07 op rv = 0;
207 ebfc5784 2024-01-07 op
208 ebfc5784 2024-01-07 op err:
209 f9ab77a8 2023-08-23 op sk_GENERAL_NAME_pop_free(altname_stack, GENERAL_NAME_free);
210 f9ab77a8 2023-08-23 op return rv;
211 f9ab77a8 2023-08-23 op }
212 f9ab77a8 2023-08-23 op
213 f9ab77a8 2023-08-23 op static int
214 f9ab77a8 2023-08-23 op tls_check_common_name(struct tls *ctx, X509 *cert, const char *name,
215 f9ab77a8 2023-08-23 op int *cn_match)
216 f9ab77a8 2023-08-23 op {
217 ebfc5784 2024-01-07 op unsigned char *utf8_bytes = NULL;
218 f9ab77a8 2023-08-23 op X509_NAME *subject_name;
219 f9ab77a8 2023-08-23 op char *common_name = NULL;
220 f9ab77a8 2023-08-23 op union tls_addr addrbuf;
221 f9ab77a8 2023-08-23 op int common_name_len;
222 ebfc5784 2024-01-07 op ASN1_STRING *data;
223 ebfc5784 2024-01-07 op int lastpos = -1;
224 f9ab77a8 2023-08-23 op int rv = -1;
225 f9ab77a8 2023-08-23 op
226 f9ab77a8 2023-08-23 op *cn_match = 0;
227 f9ab77a8 2023-08-23 op
228 f9ab77a8 2023-08-23 op subject_name = X509_get_subject_name(cert);
229 f9ab77a8 2023-08-23 op if (subject_name == NULL)
230 f9ab77a8 2023-08-23 op goto done;
231 f9ab77a8 2023-08-23 op
232 ebfc5784 2024-01-07 op lastpos = X509_NAME_get_index_by_NID(subject_name,
233 ebfc5784 2024-01-07 op NID_commonName, lastpos);
234 ebfc5784 2024-01-07 op if (lastpos == -1)
235 f9ab77a8 2023-08-23 op goto done;
236 ebfc5784 2024-01-07 op if (lastpos < 0)
237 f9ab77a8 2023-08-23 op goto err;
238 ebfc5784 2024-01-07 op if (X509_NAME_get_index_by_NID(subject_name, NID_commonName, lastpos)
239 ebfc5784 2024-01-07 op != -1) {
240 ebfc5784 2024-01-07 op /*
241 ebfc5784 2024-01-07 op * Having multiple CN's is possible, and even happened back in
242 ebfc5784 2024-01-07 op * the glory days of mullets and Hammer pants. In anything like
243 ebfc5784 2024-01-07 op * a modern TLS cert, CN is as close to deprecated as it gets,
244 ebfc5784 2024-01-07 op * and having more than one is bad. We therefore fail if we have
245 ebfc5784 2024-01-07 op * more than one CN fed to us in the subject, treating the
246 ebfc5784 2024-01-07 op * certificate as hostile.
247 ebfc5784 2024-01-07 op */
248 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "error verifying name '%s': "
249 ebfc5784 2024-01-07 op "Certificate subject contains multiple Common Name fields, "
250 ebfc5784 2024-01-07 op "probably a malicious or malformed certificate", name);
251 ebfc5784 2024-01-07 op goto err;
252 f9ab77a8 2023-08-23 op }
253 f9ab77a8 2023-08-23 op
254 ebfc5784 2024-01-07 op data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name,
255 ebfc5784 2024-01-07 op lastpos));
256 ebfc5784 2024-01-07 op /*
257 ebfc5784 2024-01-07 op * Fail if we cannot encode the CN bytes as UTF-8.
258 ebfc5784 2024-01-07 op */
259 ebfc5784 2024-01-07 op if ((common_name_len = ASN1_STRING_to_UTF8(&utf8_bytes, data)) < 0) {
260 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "error verifying name '%s': "
261 ebfc5784 2024-01-07 op "Common Name field cannot be encoded as a UTF-8 string, "
262 ebfc5784 2024-01-07 op "probably a malicious certificate", name);
263 ebfc5784 2024-01-07 op goto err;
264 ebfc5784 2024-01-07 op }
265 ebfc5784 2024-01-07 op /*
266 ebfc5784 2024-01-07 op * Fail if the CN is of invalid length. RFC 5280 specifies that a CN
267 ebfc5784 2024-01-07 op * must be between 1 and 64 bytes long.
268 ebfc5784 2024-01-07 op */
269 ebfc5784 2024-01-07 op if (common_name_len < 1 || common_name_len > 64) {
270 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "error verifying name '%s': "
271 ebfc5784 2024-01-07 op "Common Name field has invalid length, "
272 ebfc5784 2024-01-07 op "probably a malicious certificate", name);
273 ebfc5784 2024-01-07 op goto err;
274 ebfc5784 2024-01-07 op }
275 ebfc5784 2024-01-07 op /*
276 ebfc5784 2024-01-07 op * Fail if the resulting text contains a NUL byte.
277 ebfc5784 2024-01-07 op */
278 ebfc5784 2024-01-07 op if (memchr(utf8_bytes, 0, common_name_len) != NULL) {
279 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "error verifying name '%s': "
280 f9ab77a8 2023-08-23 op "NUL byte in Common Name field, "
281 f9ab77a8 2023-08-23 op "probably a malicious certificate", name);
282 f9ab77a8 2023-08-23 op goto err;
283 f9ab77a8 2023-08-23 op }
284 f9ab77a8 2023-08-23 op
285 ebfc5784 2024-01-07 op common_name = strndup(utf8_bytes, common_name_len);
286 ebfc5784 2024-01-07 op if (common_name == NULL) {
287 ebfc5784 2024-01-07 op tls_set_error(ctx, "out of memory");
288 ebfc5784 2024-01-07 op goto err;
289 ebfc5784 2024-01-07 op }
290 ebfc5784 2024-01-07 op
291 f9ab77a8 2023-08-23 op /*
292 f9ab77a8 2023-08-23 op * We don't want to attempt wildcard matching against IP addresses,
293 f9ab77a8 2023-08-23 op * so perform a simple comparison here.
294 f9ab77a8 2023-08-23 op */
295 f9ab77a8 2023-08-23 op if (inet_pton(AF_INET, name, &addrbuf) == 1 ||
296 f9ab77a8 2023-08-23 op inet_pton(AF_INET6, name, &addrbuf) == 1) {
297 f9ab77a8 2023-08-23 op if (strcmp(common_name, name) == 0)
298 f9ab77a8 2023-08-23 op *cn_match = 1;
299 f9ab77a8 2023-08-23 op goto done;
300 f9ab77a8 2023-08-23 op }
301 f9ab77a8 2023-08-23 op
302 f9ab77a8 2023-08-23 op if (tls_match_name(common_name, name) == 0)
303 f9ab77a8 2023-08-23 op *cn_match = 1;
304 f9ab77a8 2023-08-23 op
305 f9ab77a8 2023-08-23 op done:
306 f9ab77a8 2023-08-23 op rv = 0;
307 f9ab77a8 2023-08-23 op
308 f9ab77a8 2023-08-23 op err:
309 ebfc5784 2024-01-07 op free(utf8_bytes);
310 f9ab77a8 2023-08-23 op free(common_name);
311 f9ab77a8 2023-08-23 op return rv;
312 f9ab77a8 2023-08-23 op }
313 f9ab77a8 2023-08-23 op
314 f9ab77a8 2023-08-23 op int
315 f9ab77a8 2023-08-23 op tls_check_name(struct tls *ctx, X509 *cert, const char *name, int *match)
316 f9ab77a8 2023-08-23 op {
317 f9ab77a8 2023-08-23 op int alt_exists;
318 f9ab77a8 2023-08-23 op
319 f9ab77a8 2023-08-23 op *match = 0;
320 f9ab77a8 2023-08-23 op
321 f9ab77a8 2023-08-23 op if (tls_check_subject_altname(ctx, cert, name, match,
322 f9ab77a8 2023-08-23 op &alt_exists) == -1)
323 f9ab77a8 2023-08-23 op return -1;
324 f9ab77a8 2023-08-23 op
325 f9ab77a8 2023-08-23 op /*
326 f9ab77a8 2023-08-23 op * As per RFC 6125 section 6.4.4, if any known alternate name existed
327 f9ab77a8 2023-08-23 op * in the certificate, we do not attempt to match on the CN.
328 f9ab77a8 2023-08-23 op */
329 f9ab77a8 2023-08-23 op if (*match || alt_exists)
330 f9ab77a8 2023-08-23 op return 0;
331 f9ab77a8 2023-08-23 op
332 f9ab77a8 2023-08-23 op return tls_check_common_name(ctx, cert, name, match);
333 f9ab77a8 2023-08-23 op }