Blame


1 ebfc5784 2024-01-07 op /* $OpenBSD: tls.c,v 1.98 2023/07/02 06:37:27 beck Exp $ */
2 f9ab77a8 2023-08-23 op /*
3 f9ab77a8 2023-08-23 op * Copyright (c) 2014 Joel Sing <jsing@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 <errno.h>
23 f9ab77a8 2023-08-23 op #include <limits.h>
24 f9ab77a8 2023-08-23 op #include <stdlib.h>
25 f9ab77a8 2023-08-23 op #include <string.h>
26 f9ab77a8 2023-08-23 op #include <unistd.h>
27 f9ab77a8 2023-08-23 op
28 f9ab77a8 2023-08-23 op #include <openssl/bio.h>
29 f9ab77a8 2023-08-23 op #include <openssl/err.h>
30 f9ab77a8 2023-08-23 op #include <openssl/evp.h>
31 f9ab77a8 2023-08-23 op #include <openssl/pem.h>
32 f9ab77a8 2023-08-23 op #include <openssl/safestack.h>
33 f9ab77a8 2023-08-23 op #include <openssl/ssl.h>
34 f9ab77a8 2023-08-23 op #include <openssl/x509.h>
35 f9ab77a8 2023-08-23 op
36 f9ab77a8 2023-08-23 op #include <tls.h>
37 f9ab77a8 2023-08-23 op #include "tls_internal.h"
38 f9ab77a8 2023-08-23 op
39 f9ab77a8 2023-08-23 op static struct tls_config *tls_config_default;
40 f9ab77a8 2023-08-23 op
41 f9ab77a8 2023-08-23 op static int tls_init_rv = -1;
42 f9ab77a8 2023-08-23 op
43 f9ab77a8 2023-08-23 op static void
44 f9ab77a8 2023-08-23 op tls_do_init(void)
45 f9ab77a8 2023-08-23 op {
46 f9ab77a8 2023-08-23 op OPENSSL_init_ssl(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
47 f9ab77a8 2023-08-23 op
48 f9ab77a8 2023-08-23 op if (BIO_sock_init() != 1)
49 f9ab77a8 2023-08-23 op return;
50 f9ab77a8 2023-08-23 op
51 f9ab77a8 2023-08-23 op if ((tls_config_default = tls_config_new_internal()) == NULL)
52 f9ab77a8 2023-08-23 op return;
53 f9ab77a8 2023-08-23 op
54 f9ab77a8 2023-08-23 op tls_config_default->refcount++;
55 f9ab77a8 2023-08-23 op
56 f9ab77a8 2023-08-23 op tls_init_rv = 0;
57 f9ab77a8 2023-08-23 op }
58 f9ab77a8 2023-08-23 op
59 f9ab77a8 2023-08-23 op int
60 f9ab77a8 2023-08-23 op tls_init(void)
61 f9ab77a8 2023-08-23 op {
62 f9ab77a8 2023-08-23 op if (tls_init_rv == -1)
63 f9ab77a8 2023-08-23 op tls_do_init();
64 f9ab77a8 2023-08-23 op return tls_init_rv;
65 f9ab77a8 2023-08-23 op }
66 f9ab77a8 2023-08-23 op
67 f9ab77a8 2023-08-23 op const char *
68 f9ab77a8 2023-08-23 op tls_error(struct tls *ctx)
69 f9ab77a8 2023-08-23 op {
70 f9ab77a8 2023-08-23 op return ctx->error.msg;
71 f9ab77a8 2023-08-23 op }
72 f9ab77a8 2023-08-23 op
73 f9ab77a8 2023-08-23 op void
74 f9ab77a8 2023-08-23 op tls_error_clear(struct tls_error *error)
75 f9ab77a8 2023-08-23 op {
76 f9ab77a8 2023-08-23 op free(error->msg);
77 f9ab77a8 2023-08-23 op error->msg = NULL;
78 f9ab77a8 2023-08-23 op error->num = 0;
79 f9ab77a8 2023-08-23 op error->tls = 0;
80 f9ab77a8 2023-08-23 op }
81 f9ab77a8 2023-08-23 op
82 f9ab77a8 2023-08-23 op static int
83 f9ab77a8 2023-08-23 op tls_error_vset(struct tls_error *error, int errnum, const char *fmt, va_list ap)
84 f9ab77a8 2023-08-23 op {
85 f9ab77a8 2023-08-23 op char *errmsg = NULL;
86 f9ab77a8 2023-08-23 op int rv = -1;
87 f9ab77a8 2023-08-23 op
88 f9ab77a8 2023-08-23 op tls_error_clear(error);
89 f9ab77a8 2023-08-23 op
90 f9ab77a8 2023-08-23 op error->num = errnum;
91 f9ab77a8 2023-08-23 op error->tls = 1;
92 f9ab77a8 2023-08-23 op
93 f9ab77a8 2023-08-23 op if (vasprintf(&errmsg, fmt, ap) == -1) {
94 f9ab77a8 2023-08-23 op errmsg = NULL;
95 f9ab77a8 2023-08-23 op goto err;
96 f9ab77a8 2023-08-23 op }
97 f9ab77a8 2023-08-23 op
98 f9ab77a8 2023-08-23 op if (errnum == -1) {
99 f9ab77a8 2023-08-23 op error->msg = errmsg;
100 f9ab77a8 2023-08-23 op return (0);
101 f9ab77a8 2023-08-23 op }
102 f9ab77a8 2023-08-23 op
103 f9ab77a8 2023-08-23 op if (asprintf(&error->msg, "%s: %s", errmsg, strerror(errnum)) == -1) {
104 f9ab77a8 2023-08-23 op error->msg = NULL;
105 f9ab77a8 2023-08-23 op goto err;
106 f9ab77a8 2023-08-23 op }
107 f9ab77a8 2023-08-23 op rv = 0;
108 f9ab77a8 2023-08-23 op
109 f9ab77a8 2023-08-23 op err:
110 f9ab77a8 2023-08-23 op free(errmsg);
111 f9ab77a8 2023-08-23 op
112 f9ab77a8 2023-08-23 op return (rv);
113 f9ab77a8 2023-08-23 op }
114 f9ab77a8 2023-08-23 op
115 f9ab77a8 2023-08-23 op int
116 f9ab77a8 2023-08-23 op tls_error_set(struct tls_error *error, const char *fmt, ...)
117 f9ab77a8 2023-08-23 op {
118 f9ab77a8 2023-08-23 op va_list ap;
119 f9ab77a8 2023-08-23 op int errnum, rv;
120 f9ab77a8 2023-08-23 op
121 f9ab77a8 2023-08-23 op errnum = errno;
122 f9ab77a8 2023-08-23 op
123 f9ab77a8 2023-08-23 op va_start(ap, fmt);
124 f9ab77a8 2023-08-23 op rv = tls_error_vset(error, errnum, fmt, ap);
125 f9ab77a8 2023-08-23 op va_end(ap);
126 f9ab77a8 2023-08-23 op
127 f9ab77a8 2023-08-23 op return (rv);
128 f9ab77a8 2023-08-23 op }
129 f9ab77a8 2023-08-23 op
130 f9ab77a8 2023-08-23 op int
131 f9ab77a8 2023-08-23 op tls_error_setx(struct tls_error *error, const char *fmt, ...)
132 f9ab77a8 2023-08-23 op {
133 f9ab77a8 2023-08-23 op va_list ap;
134 f9ab77a8 2023-08-23 op int rv;
135 f9ab77a8 2023-08-23 op
136 f9ab77a8 2023-08-23 op va_start(ap, fmt);
137 f9ab77a8 2023-08-23 op rv = tls_error_vset(error, -1, fmt, ap);
138 f9ab77a8 2023-08-23 op va_end(ap);
139 f9ab77a8 2023-08-23 op
140 f9ab77a8 2023-08-23 op return (rv);
141 f9ab77a8 2023-08-23 op }
142 f9ab77a8 2023-08-23 op
143 f9ab77a8 2023-08-23 op int
144 f9ab77a8 2023-08-23 op tls_config_set_error(struct tls_config *config, const char *fmt, ...)
145 f9ab77a8 2023-08-23 op {
146 f9ab77a8 2023-08-23 op va_list ap;
147 f9ab77a8 2023-08-23 op int errnum, rv;
148 f9ab77a8 2023-08-23 op
149 f9ab77a8 2023-08-23 op errnum = errno;
150 f9ab77a8 2023-08-23 op
151 f9ab77a8 2023-08-23 op va_start(ap, fmt);
152 f9ab77a8 2023-08-23 op rv = tls_error_vset(&config->error, errnum, fmt, ap);
153 f9ab77a8 2023-08-23 op va_end(ap);
154 f9ab77a8 2023-08-23 op
155 f9ab77a8 2023-08-23 op return (rv);
156 f9ab77a8 2023-08-23 op }
157 f9ab77a8 2023-08-23 op
158 f9ab77a8 2023-08-23 op int
159 f9ab77a8 2023-08-23 op tls_config_set_errorx(struct tls_config *config, const char *fmt, ...)
160 f9ab77a8 2023-08-23 op {
161 f9ab77a8 2023-08-23 op va_list ap;
162 f9ab77a8 2023-08-23 op int rv;
163 f9ab77a8 2023-08-23 op
164 f9ab77a8 2023-08-23 op va_start(ap, fmt);
165 f9ab77a8 2023-08-23 op rv = tls_error_vset(&config->error, -1, fmt, ap);
166 f9ab77a8 2023-08-23 op va_end(ap);
167 f9ab77a8 2023-08-23 op
168 f9ab77a8 2023-08-23 op return (rv);
169 f9ab77a8 2023-08-23 op }
170 f9ab77a8 2023-08-23 op
171 f9ab77a8 2023-08-23 op int
172 f9ab77a8 2023-08-23 op tls_set_error(struct tls *ctx, const char *fmt, ...)
173 f9ab77a8 2023-08-23 op {
174 f9ab77a8 2023-08-23 op va_list ap;
175 f9ab77a8 2023-08-23 op int errnum, rv;
176 f9ab77a8 2023-08-23 op
177 f9ab77a8 2023-08-23 op errnum = errno;
178 f9ab77a8 2023-08-23 op
179 f9ab77a8 2023-08-23 op va_start(ap, fmt);
180 f9ab77a8 2023-08-23 op rv = tls_error_vset(&ctx->error, errnum, fmt, ap);
181 f9ab77a8 2023-08-23 op va_end(ap);
182 f9ab77a8 2023-08-23 op
183 f9ab77a8 2023-08-23 op return (rv);
184 f9ab77a8 2023-08-23 op }
185 f9ab77a8 2023-08-23 op
186 f9ab77a8 2023-08-23 op int
187 f9ab77a8 2023-08-23 op tls_set_errorx(struct tls *ctx, const char *fmt, ...)
188 f9ab77a8 2023-08-23 op {
189 f9ab77a8 2023-08-23 op va_list ap;
190 f9ab77a8 2023-08-23 op int rv;
191 f9ab77a8 2023-08-23 op
192 f9ab77a8 2023-08-23 op va_start(ap, fmt);
193 f9ab77a8 2023-08-23 op rv = tls_error_vset(&ctx->error, -1, fmt, ap);
194 f9ab77a8 2023-08-23 op va_end(ap);
195 f9ab77a8 2023-08-23 op
196 f9ab77a8 2023-08-23 op return (rv);
197 f9ab77a8 2023-08-23 op }
198 f9ab77a8 2023-08-23 op
199 f9ab77a8 2023-08-23 op int
200 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(struct tls *ctx, const char *fmt, ...)
201 f9ab77a8 2023-08-23 op {
202 f9ab77a8 2023-08-23 op va_list ap;
203 f9ab77a8 2023-08-23 op int rv;
204 f9ab77a8 2023-08-23 op
205 f9ab77a8 2023-08-23 op /* Only set an error if a more specific one does not already exist. */
206 f9ab77a8 2023-08-23 op if (ctx->error.tls != 0)
207 f9ab77a8 2023-08-23 op return (0);
208 f9ab77a8 2023-08-23 op
209 f9ab77a8 2023-08-23 op va_start(ap, fmt);
210 f9ab77a8 2023-08-23 op rv = tls_error_vset(&ctx->error, -1, fmt, ap);
211 f9ab77a8 2023-08-23 op va_end(ap);
212 f9ab77a8 2023-08-23 op
213 f9ab77a8 2023-08-23 op return (rv);
214 f9ab77a8 2023-08-23 op }
215 f9ab77a8 2023-08-23 op
216 f9ab77a8 2023-08-23 op struct tls_sni_ctx *
217 f9ab77a8 2023-08-23 op tls_sni_ctx_new(void)
218 f9ab77a8 2023-08-23 op {
219 f9ab77a8 2023-08-23 op return (calloc(1, sizeof(struct tls_sni_ctx)));
220 f9ab77a8 2023-08-23 op }
221 f9ab77a8 2023-08-23 op
222 f9ab77a8 2023-08-23 op void
223 f9ab77a8 2023-08-23 op tls_sni_ctx_free(struct tls_sni_ctx *sni_ctx)
224 f9ab77a8 2023-08-23 op {
225 f9ab77a8 2023-08-23 op if (sni_ctx == NULL)
226 f9ab77a8 2023-08-23 op return;
227 f9ab77a8 2023-08-23 op
228 f9ab77a8 2023-08-23 op SSL_CTX_free(sni_ctx->ssl_ctx);
229 f9ab77a8 2023-08-23 op X509_free(sni_ctx->ssl_cert);
230 f9ab77a8 2023-08-23 op
231 f9ab77a8 2023-08-23 op free(sni_ctx);
232 f9ab77a8 2023-08-23 op }
233 f9ab77a8 2023-08-23 op
234 f9ab77a8 2023-08-23 op struct tls *
235 f9ab77a8 2023-08-23 op tls_new(void)
236 f9ab77a8 2023-08-23 op {
237 f9ab77a8 2023-08-23 op struct tls *ctx;
238 f9ab77a8 2023-08-23 op
239 f9ab77a8 2023-08-23 op if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
240 f9ab77a8 2023-08-23 op return (NULL);
241 f9ab77a8 2023-08-23 op
242 f9ab77a8 2023-08-23 op tls_reset(ctx);
243 f9ab77a8 2023-08-23 op
244 f9ab77a8 2023-08-23 op if (tls_configure(ctx, tls_config_default) == -1) {
245 f9ab77a8 2023-08-23 op free(ctx);
246 f9ab77a8 2023-08-23 op return NULL;
247 f9ab77a8 2023-08-23 op }
248 f9ab77a8 2023-08-23 op
249 f9ab77a8 2023-08-23 op return (ctx);
250 f9ab77a8 2023-08-23 op }
251 f9ab77a8 2023-08-23 op
252 f9ab77a8 2023-08-23 op int
253 f9ab77a8 2023-08-23 op tls_configure(struct tls *ctx, struct tls_config *config)
254 f9ab77a8 2023-08-23 op {
255 f9ab77a8 2023-08-23 op if (config == NULL)
256 f9ab77a8 2023-08-23 op config = tls_config_default;
257 f9ab77a8 2023-08-23 op
258 f9ab77a8 2023-08-23 op config->refcount++;
259 f9ab77a8 2023-08-23 op
260 f9ab77a8 2023-08-23 op tls_config_free(ctx->config);
261 f9ab77a8 2023-08-23 op
262 f9ab77a8 2023-08-23 op ctx->config = config;
263 f9ab77a8 2023-08-23 op ctx->keypair = config->keypair;
264 f9ab77a8 2023-08-23 op
265 f9ab77a8 2023-08-23 op if ((ctx->flags & TLS_SERVER) != 0)
266 f9ab77a8 2023-08-23 op return (tls_configure_server(ctx));
267 f9ab77a8 2023-08-23 op
268 f9ab77a8 2023-08-23 op return (0);
269 f9ab77a8 2023-08-23 op }
270 f9ab77a8 2023-08-23 op
271 f9ab77a8 2023-08-23 op int
272 f9ab77a8 2023-08-23 op tls_cert_hash(X509 *cert, char **hash)
273 f9ab77a8 2023-08-23 op {
274 f9ab77a8 2023-08-23 op char d[EVP_MAX_MD_SIZE], *dhex = NULL;
275 f9ab77a8 2023-08-23 op int dlen, rv = -1;
276 f9ab77a8 2023-08-23 op
277 f9ab77a8 2023-08-23 op free(*hash);
278 f9ab77a8 2023-08-23 op *hash = NULL;
279 f9ab77a8 2023-08-23 op
280 f9ab77a8 2023-08-23 op if (X509_digest(cert, EVP_sha256(), d, &dlen) != 1)
281 f9ab77a8 2023-08-23 op goto err;
282 f9ab77a8 2023-08-23 op
283 f9ab77a8 2023-08-23 op if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
284 f9ab77a8 2023-08-23 op goto err;
285 f9ab77a8 2023-08-23 op
286 f9ab77a8 2023-08-23 op if (asprintf(hash, "SHA256:%s", dhex) == -1) {
287 f9ab77a8 2023-08-23 op *hash = NULL;
288 f9ab77a8 2023-08-23 op goto err;
289 f9ab77a8 2023-08-23 op }
290 f9ab77a8 2023-08-23 op
291 f9ab77a8 2023-08-23 op rv = 0;
292 f9ab77a8 2023-08-23 op err:
293 f9ab77a8 2023-08-23 op free(dhex);
294 f9ab77a8 2023-08-23 op
295 f9ab77a8 2023-08-23 op return (rv);
296 f9ab77a8 2023-08-23 op }
297 f9ab77a8 2023-08-23 op
298 f9ab77a8 2023-08-23 op int
299 f9ab77a8 2023-08-23 op tls_cert_pubkey_hash(X509 *cert, char **hash)
300 f9ab77a8 2023-08-23 op {
301 f9ab77a8 2023-08-23 op char d[EVP_MAX_MD_SIZE], *dhex = NULL;
302 f9ab77a8 2023-08-23 op int dlen, rv = -1;
303 f9ab77a8 2023-08-23 op
304 f9ab77a8 2023-08-23 op free(*hash);
305 f9ab77a8 2023-08-23 op *hash = NULL;
306 f9ab77a8 2023-08-23 op
307 f9ab77a8 2023-08-23 op if (X509_pubkey_digest(cert, EVP_sha256(), d, &dlen) != 1)
308 f9ab77a8 2023-08-23 op goto err;
309 f9ab77a8 2023-08-23 op
310 f9ab77a8 2023-08-23 op if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
311 f9ab77a8 2023-08-23 op goto err;
312 f9ab77a8 2023-08-23 op
313 f9ab77a8 2023-08-23 op if (asprintf(hash, "SHA256:%s", dhex) == -1) {
314 f9ab77a8 2023-08-23 op *hash = NULL;
315 f9ab77a8 2023-08-23 op goto err;
316 f9ab77a8 2023-08-23 op }
317 f9ab77a8 2023-08-23 op
318 f9ab77a8 2023-08-23 op rv = 0;
319 f9ab77a8 2023-08-23 op
320 f9ab77a8 2023-08-23 op err:
321 f9ab77a8 2023-08-23 op free(dhex);
322 f9ab77a8 2023-08-23 op
323 f9ab77a8 2023-08-23 op return (rv);
324 f9ab77a8 2023-08-23 op }
325 f9ab77a8 2023-08-23 op
326 f9ab77a8 2023-08-23 op static int
327 f9ab77a8 2023-08-23 op tls_keypair_to_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY **pkey)
328 f9ab77a8 2023-08-23 op {
329 f9ab77a8 2023-08-23 op BIO *bio = NULL;
330 f9ab77a8 2023-08-23 op X509 *x509 = NULL;
331 f9ab77a8 2023-08-23 op char *mem;
332 f9ab77a8 2023-08-23 op size_t len;
333 f9ab77a8 2023-08-23 op int ret = -1;
334 f9ab77a8 2023-08-23 op
335 f9ab77a8 2023-08-23 op *pkey = NULL;
336 f9ab77a8 2023-08-23 op
337 f9ab77a8 2023-08-23 op if (ctx->config->use_fake_private_key) {
338 f9ab77a8 2023-08-23 op mem = keypair->cert_mem;
339 f9ab77a8 2023-08-23 op len = keypair->cert_len;
340 f9ab77a8 2023-08-23 op } else {
341 f9ab77a8 2023-08-23 op mem = keypair->key_mem;
342 f9ab77a8 2023-08-23 op len = keypair->key_len;
343 f9ab77a8 2023-08-23 op }
344 f9ab77a8 2023-08-23 op
345 f9ab77a8 2023-08-23 op if (mem == NULL)
346 f9ab77a8 2023-08-23 op return (0);
347 f9ab77a8 2023-08-23 op
348 f9ab77a8 2023-08-23 op if (len > INT_MAX) {
349 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, ctx->config->use_fake_private_key ?
350 f9ab77a8 2023-08-23 op "cert too long" : "key too long");
351 f9ab77a8 2023-08-23 op goto err;
352 f9ab77a8 2023-08-23 op }
353 f9ab77a8 2023-08-23 op
354 f9ab77a8 2023-08-23 op if ((bio = BIO_new_mem_buf(mem, len)) == NULL) {
355 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to create buffer");
356 f9ab77a8 2023-08-23 op goto err;
357 f9ab77a8 2023-08-23 op }
358 f9ab77a8 2023-08-23 op
359 f9ab77a8 2023-08-23 op if (ctx->config->use_fake_private_key) {
360 f9ab77a8 2023-08-23 op if ((x509 = PEM_read_bio_X509(bio, NULL, tls_password_cb,
361 f9ab77a8 2023-08-23 op NULL)) == NULL) {
362 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to read X509 certificate");
363 f9ab77a8 2023-08-23 op goto err;
364 f9ab77a8 2023-08-23 op }
365 f9ab77a8 2023-08-23 op if ((*pkey = X509_get_pubkey(x509)) == NULL) {
366 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to retrieve pubkey");
367 f9ab77a8 2023-08-23 op goto err;
368 f9ab77a8 2023-08-23 op }
369 f9ab77a8 2023-08-23 op } else {
370 f9ab77a8 2023-08-23 op if ((*pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_password_cb,
371 f9ab77a8 2023-08-23 op NULL)) == NULL) {
372 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to read private key");
373 f9ab77a8 2023-08-23 op goto err;
374 f9ab77a8 2023-08-23 op }
375 f9ab77a8 2023-08-23 op }
376 f9ab77a8 2023-08-23 op
377 f9ab77a8 2023-08-23 op ret = 0;
378 f9ab77a8 2023-08-23 op err:
379 f9ab77a8 2023-08-23 op BIO_free(bio);
380 f9ab77a8 2023-08-23 op X509_free(x509);
381 f9ab77a8 2023-08-23 op return (ret);
382 f9ab77a8 2023-08-23 op }
383 f9ab77a8 2023-08-23 op
384 f9ab77a8 2023-08-23 op static int
385 f9ab77a8 2023-08-23 op tls_keypair_setup_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY *pkey)
386 f9ab77a8 2023-08-23 op {
387 ebfc5784 2024-01-07 op RSA_METHOD *rsa_method;
388 ebfc5784 2024-01-07 op EC_KEY_METHOD *ecdsa_method;
389 f9ab77a8 2023-08-23 op RSA *rsa = NULL;
390 f9ab77a8 2023-08-23 op EC_KEY *eckey = NULL;
391 f9ab77a8 2023-08-23 op int ret = -1;
392 f9ab77a8 2023-08-23 op
393 f9ab77a8 2023-08-23 op /* Only install the pubkey hash if fake private keys are used. */
394 f9ab77a8 2023-08-23 op if (!ctx->config->skip_private_key_check)
395 f9ab77a8 2023-08-23 op return (0);
396 f9ab77a8 2023-08-23 op
397 f9ab77a8 2023-08-23 op if (keypair->pubkey_hash == NULL) {
398 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "public key hash not set");
399 f9ab77a8 2023-08-23 op goto err;
400 f9ab77a8 2023-08-23 op }
401 f9ab77a8 2023-08-23 op
402 f9ab77a8 2023-08-23 op switch (EVP_PKEY_id(pkey)) {
403 f9ab77a8 2023-08-23 op case EVP_PKEY_RSA:
404 f9ab77a8 2023-08-23 op if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL ||
405 ebfc5784 2024-01-07 op RSA_set_ex_data(rsa, 0, keypair->pubkey_hash) == 0) {
406 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "RSA key setup failure");
407 f9ab77a8 2023-08-23 op goto err;
408 f9ab77a8 2023-08-23 op }
409 ebfc5784 2024-01-07 op if (ctx->config->sign_cb != NULL) {
410 ebfc5784 2024-01-07 op rsa_method = tls_signer_rsa_method();
411 ebfc5784 2024-01-07 op if (rsa_method == NULL ||
412 ebfc5784 2024-01-07 op RSA_set_ex_data(rsa, 1, ctx->config) == 0 ||
413 ebfc5784 2024-01-07 op RSA_set_method(rsa, rsa_method) == 0) {
414 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "failed to setup RSA key");
415 ebfc5784 2024-01-07 op goto err;
416 ebfc5784 2024-01-07 op }
417 ebfc5784 2024-01-07 op }
418 ebfc5784 2024-01-07 op /* Reset the key to work around caching in OpenSSL 3. */
419 ebfc5784 2024-01-07 op if (EVP_PKEY_set1_RSA(pkey, rsa) == 0) {
420 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "failed to set RSA key");
421 ebfc5784 2024-01-07 op goto err;
422 ebfc5784 2024-01-07 op }
423 f9ab77a8 2023-08-23 op break;
424 f9ab77a8 2023-08-23 op case EVP_PKEY_EC:
425 f9ab77a8 2023-08-23 op if ((eckey = EVP_PKEY_get1_EC_KEY(pkey)) == NULL ||
426 ebfc5784 2024-01-07 op EC_KEY_set_ex_data(eckey, 0, keypair->pubkey_hash) == 0) {
427 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "EC key setup failure");
428 ebfc5784 2024-01-07 op goto err;
429 ebfc5784 2024-01-07 op }
430 ebfc5784 2024-01-07 op if (ctx->config->sign_cb != NULL) {
431 ebfc5784 2024-01-07 op ecdsa_method = tls_signer_ecdsa_method();
432 ebfc5784 2024-01-07 op if (ecdsa_method == NULL ||
433 ebfc5784 2024-01-07 op EC_KEY_set_ex_data(eckey, 1, ctx->config) == 0 ||
434 ebfc5784 2024-01-07 op EC_KEY_set_method(eckey, ecdsa_method) == 0) {
435 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "failed to setup EC key");
436 ebfc5784 2024-01-07 op goto err;
437 ebfc5784 2024-01-07 op }
438 ebfc5784 2024-01-07 op }
439 ebfc5784 2024-01-07 op /* Reset the key to work around caching in OpenSSL 3. */
440 ebfc5784 2024-01-07 op if (EVP_PKEY_set1_EC_KEY(pkey, eckey) == 0) {
441 ebfc5784 2024-01-07 op tls_set_errorx(ctx, "failed to set EC key");
442 f9ab77a8 2023-08-23 op goto err;
443 f9ab77a8 2023-08-23 op }
444 f9ab77a8 2023-08-23 op break;
445 f9ab77a8 2023-08-23 op default:
446 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "incorrect key type");
447 f9ab77a8 2023-08-23 op goto err;
448 f9ab77a8 2023-08-23 op }
449 f9ab77a8 2023-08-23 op
450 f9ab77a8 2023-08-23 op ret = 0;
451 f9ab77a8 2023-08-23 op
452 f9ab77a8 2023-08-23 op err:
453 f9ab77a8 2023-08-23 op RSA_free(rsa);
454 f9ab77a8 2023-08-23 op EC_KEY_free(eckey);
455 f9ab77a8 2023-08-23 op return (ret);
456 f9ab77a8 2023-08-23 op }
457 f9ab77a8 2023-08-23 op
458 f9ab77a8 2023-08-23 op int
459 f9ab77a8 2023-08-23 op tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx,
460 f9ab77a8 2023-08-23 op struct tls_keypair *keypair, int required)
461 f9ab77a8 2023-08-23 op {
462 f9ab77a8 2023-08-23 op EVP_PKEY *pkey = NULL;
463 f9ab77a8 2023-08-23 op
464 f9ab77a8 2023-08-23 op if (!required &&
465 f9ab77a8 2023-08-23 op keypair->cert_mem == NULL &&
466 f9ab77a8 2023-08-23 op keypair->key_mem == NULL)
467 f9ab77a8 2023-08-23 op return(0);
468 f9ab77a8 2023-08-23 op
469 f9ab77a8 2023-08-23 op if (keypair->cert_mem != NULL) {
470 f9ab77a8 2023-08-23 op if (keypair->cert_len > INT_MAX) {
471 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "certificate too long");
472 f9ab77a8 2023-08-23 op goto err;
473 f9ab77a8 2023-08-23 op }
474 f9ab77a8 2023-08-23 op
475 f9ab77a8 2023-08-23 op if (SSL_CTX_use_certificate_chain_mem(ssl_ctx,
476 f9ab77a8 2023-08-23 op keypair->cert_mem, keypair->cert_len) != 1) {
477 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to load certificate");
478 f9ab77a8 2023-08-23 op goto err;
479 f9ab77a8 2023-08-23 op }
480 f9ab77a8 2023-08-23 op }
481 f9ab77a8 2023-08-23 op
482 f9ab77a8 2023-08-23 op if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1)
483 f9ab77a8 2023-08-23 op goto err;
484 f9ab77a8 2023-08-23 op if (pkey != NULL) {
485 f9ab77a8 2023-08-23 op if (tls_keypair_setup_pkey(ctx, keypair, pkey) == -1)
486 f9ab77a8 2023-08-23 op goto err;
487 f9ab77a8 2023-08-23 op if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) {
488 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to load private key");
489 f9ab77a8 2023-08-23 op goto err;
490 f9ab77a8 2023-08-23 op }
491 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
492 f9ab77a8 2023-08-23 op pkey = NULL;
493 f9ab77a8 2023-08-23 op }
494 f9ab77a8 2023-08-23 op
495 f9ab77a8 2023-08-23 op if (!ctx->config->skip_private_key_check &&
496 f9ab77a8 2023-08-23 op SSL_CTX_check_private_key(ssl_ctx) != 1) {
497 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "private/public key mismatch");
498 f9ab77a8 2023-08-23 op goto err;
499 f9ab77a8 2023-08-23 op }
500 f9ab77a8 2023-08-23 op
501 f9ab77a8 2023-08-23 op return (0);
502 f9ab77a8 2023-08-23 op
503 f9ab77a8 2023-08-23 op err:
504 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
505 f9ab77a8 2023-08-23 op
506 f9ab77a8 2023-08-23 op return (-1);
507 f9ab77a8 2023-08-23 op }
508 f9ab77a8 2023-08-23 op
509 f9ab77a8 2023-08-23 op int
510 f9ab77a8 2023-08-23 op tls_configure_ssl(struct tls *ctx, SSL_CTX *ssl_ctx)
511 f9ab77a8 2023-08-23 op {
512 f9ab77a8 2023-08-23 op SSL_CTX_clear_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
513 f9ab77a8 2023-08-23 op
514 f9ab77a8 2023-08-23 op SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
515 f9ab77a8 2023-08-23 op SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
516 f9ab77a8 2023-08-23 op
517 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2);
518 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv3);
519 ebfc5784 2024-01-07 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1);
520 ebfc5784 2024-01-07 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_1);
521 f9ab77a8 2023-08-23 op
522 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
523 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
524 f9ab77a8 2023-08-23 op
525 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_2) == 0)
526 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
527 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_3) == 0)
528 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
529 f9ab77a8 2023-08-23 op
530 f9ab77a8 2023-08-23 op if (ctx->config->alpn != NULL) {
531 f9ab77a8 2023-08-23 op if (SSL_CTX_set_alpn_protos(ssl_ctx, ctx->config->alpn,
532 f9ab77a8 2023-08-23 op ctx->config->alpn_len) != 0) {
533 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to set alpn");
534 f9ab77a8 2023-08-23 op goto err;
535 f9ab77a8 2023-08-23 op }
536 f9ab77a8 2023-08-23 op }
537 f9ab77a8 2023-08-23 op
538 f9ab77a8 2023-08-23 op if (ctx->config->ciphers != NULL) {
539 f9ab77a8 2023-08-23 op if (SSL_CTX_set_cipher_list(ssl_ctx,
540 f9ab77a8 2023-08-23 op ctx->config->ciphers) != 1) {
541 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to set ciphers");
542 f9ab77a8 2023-08-23 op goto err;
543 f9ab77a8 2023-08-23 op }
544 f9ab77a8 2023-08-23 op }
545 f9ab77a8 2023-08-23 op
546 f9ab77a8 2023-08-23 op if (ctx->config->verify_time == 0) {
547 f9ab77a8 2023-08-23 op X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(ssl_ctx),
548 f9ab77a8 2023-08-23 op X509_V_FLAG_NO_CHECK_TIME);
549 f9ab77a8 2023-08-23 op }
550 f9ab77a8 2023-08-23 op
551 f9ab77a8 2023-08-23 op /* Disable any form of session caching by default */
552 f9ab77a8 2023-08-23 op SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_OFF);
553 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
554 f9ab77a8 2023-08-23 op
555 f9ab77a8 2023-08-23 op return (0);
556 f9ab77a8 2023-08-23 op
557 f9ab77a8 2023-08-23 op err:
558 f9ab77a8 2023-08-23 op return (-1);
559 f9ab77a8 2023-08-23 op }
560 f9ab77a8 2023-08-23 op
561 f9ab77a8 2023-08-23 op static int
562 f9ab77a8 2023-08-23 op tls_ssl_cert_verify_cb(X509_STORE_CTX *x509_ctx, void *arg)
563 f9ab77a8 2023-08-23 op {
564 f9ab77a8 2023-08-23 op struct tls *ctx = arg;
565 f9ab77a8 2023-08-23 op int x509_err;
566 f9ab77a8 2023-08-23 op
567 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
568 f9ab77a8 2023-08-23 op return (1);
569 f9ab77a8 2023-08-23 op
570 f9ab77a8 2023-08-23 op if ((X509_verify_cert(x509_ctx)) < 0) {
571 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "X509 verify cert failed");
572 f9ab77a8 2023-08-23 op return (0);
573 f9ab77a8 2023-08-23 op }
574 f9ab77a8 2023-08-23 op
575 f9ab77a8 2023-08-23 op x509_err = X509_STORE_CTX_get_error(x509_ctx);
576 f9ab77a8 2023-08-23 op if (x509_err == X509_V_OK)
577 f9ab77a8 2023-08-23 op return (1);
578 f9ab77a8 2023-08-23 op
579 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "certificate verification failed: %s",
580 f9ab77a8 2023-08-23 op X509_verify_cert_error_string(x509_err));
581 f9ab77a8 2023-08-23 op
582 f9ab77a8 2023-08-23 op return (0);
583 f9ab77a8 2023-08-23 op }
584 f9ab77a8 2023-08-23 op
585 f9ab77a8 2023-08-23 op int
586 f9ab77a8 2023-08-23 op tls_configure_ssl_verify(struct tls *ctx, SSL_CTX *ssl_ctx, int verify)
587 f9ab77a8 2023-08-23 op {
588 f9ab77a8 2023-08-23 op size_t ca_len = ctx->config->ca_len;
589 f9ab77a8 2023-08-23 op char *ca_mem = ctx->config->ca_mem;
590 f9ab77a8 2023-08-23 op char *crl_mem = ctx->config->crl_mem;
591 f9ab77a8 2023-08-23 op size_t crl_len = ctx->config->crl_len;
592 f9ab77a8 2023-08-23 op char *ca_free = NULL;
593 f9ab77a8 2023-08-23 op STACK_OF(X509_INFO) *xis = NULL;
594 f9ab77a8 2023-08-23 op X509_STORE *store;
595 f9ab77a8 2023-08-23 op X509_INFO *xi;
596 f9ab77a8 2023-08-23 op BIO *bio = NULL;
597 f9ab77a8 2023-08-23 op int rv = -1;
598 f9ab77a8 2023-08-23 op int i;
599 f9ab77a8 2023-08-23 op
600 f9ab77a8 2023-08-23 op SSL_CTX_set_verify(ssl_ctx, verify, NULL);
601 f9ab77a8 2023-08-23 op SSL_CTX_set_cert_verify_callback(ssl_ctx, tls_ssl_cert_verify_cb, ctx);
602 f9ab77a8 2023-08-23 op
603 f9ab77a8 2023-08-23 op if (ctx->config->verify_depth >= 0)
604 f9ab77a8 2023-08-23 op SSL_CTX_set_verify_depth(ssl_ctx, ctx->config->verify_depth);
605 f9ab77a8 2023-08-23 op
606 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
607 f9ab77a8 2023-08-23 op goto done;
608 f9ab77a8 2023-08-23 op
609 f9ab77a8 2023-08-23 op /* If no CA has been specified, attempt to load the default. */
610 f9ab77a8 2023-08-23 op if (ctx->config->ca_mem == NULL && ctx->config->ca_path == NULL) {
611 f9ab77a8 2023-08-23 op if (tls_config_load_file(&ctx->error, "CA", tls_default_ca_cert_file(),
612 f9ab77a8 2023-08-23 op &ca_mem, &ca_len) != 0)
613 f9ab77a8 2023-08-23 op goto err;
614 f9ab77a8 2023-08-23 op ca_free = ca_mem;
615 f9ab77a8 2023-08-23 op }
616 f9ab77a8 2023-08-23 op
617 f9ab77a8 2023-08-23 op if (ca_mem != NULL) {
618 f9ab77a8 2023-08-23 op if (ca_len > INT_MAX) {
619 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ca too long");
620 f9ab77a8 2023-08-23 op goto err;
621 f9ab77a8 2023-08-23 op }
622 f9ab77a8 2023-08-23 op if (SSL_CTX_load_verify_mem(ssl_ctx, ca_mem, ca_len) != 1) {
623 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ssl verify memory setup failure");
624 f9ab77a8 2023-08-23 op goto err;
625 f9ab77a8 2023-08-23 op }
626 f9ab77a8 2023-08-23 op } else if (SSL_CTX_load_verify_locations(ssl_ctx, NULL,
627 f9ab77a8 2023-08-23 op ctx->config->ca_path) != 1) {
628 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ssl verify locations failure");
629 f9ab77a8 2023-08-23 op goto err;
630 f9ab77a8 2023-08-23 op }
631 f9ab77a8 2023-08-23 op
632 f9ab77a8 2023-08-23 op if (crl_mem != NULL) {
633 f9ab77a8 2023-08-23 op if (crl_len > INT_MAX) {
634 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "crl too long");
635 f9ab77a8 2023-08-23 op goto err;
636 f9ab77a8 2023-08-23 op }
637 f9ab77a8 2023-08-23 op if ((bio = BIO_new_mem_buf(crl_mem, crl_len)) == NULL) {
638 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to create buffer");
639 f9ab77a8 2023-08-23 op goto err;
640 f9ab77a8 2023-08-23 op }
641 f9ab77a8 2023-08-23 op if ((xis = PEM_X509_INFO_read_bio(bio, NULL, tls_password_cb,
642 f9ab77a8 2023-08-23 op NULL)) == NULL) {
643 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to parse crl");
644 f9ab77a8 2023-08-23 op goto err;
645 f9ab77a8 2023-08-23 op }
646 f9ab77a8 2023-08-23 op store = SSL_CTX_get_cert_store(ssl_ctx);
647 f9ab77a8 2023-08-23 op for (i = 0; i < sk_X509_INFO_num(xis); i++) {
648 f9ab77a8 2023-08-23 op xi = sk_X509_INFO_value(xis, i);
649 f9ab77a8 2023-08-23 op if (xi->crl == NULL)
650 f9ab77a8 2023-08-23 op continue;
651 f9ab77a8 2023-08-23 op if (!X509_STORE_add_crl(store, xi->crl)) {
652 f9ab77a8 2023-08-23 op tls_set_error(ctx, "failed to add crl");
653 f9ab77a8 2023-08-23 op goto err;
654 f9ab77a8 2023-08-23 op }
655 f9ab77a8 2023-08-23 op }
656 f9ab77a8 2023-08-23 op X509_STORE_set_flags(store,
657 f9ab77a8 2023-08-23 op X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
658 f9ab77a8 2023-08-23 op }
659 f9ab77a8 2023-08-23 op
660 f9ab77a8 2023-08-23 op done:
661 f9ab77a8 2023-08-23 op rv = 0;
662 f9ab77a8 2023-08-23 op
663 f9ab77a8 2023-08-23 op err:
664 f9ab77a8 2023-08-23 op sk_X509_INFO_pop_free(xis, X509_INFO_free);
665 f9ab77a8 2023-08-23 op BIO_free(bio);
666 f9ab77a8 2023-08-23 op free(ca_free);
667 f9ab77a8 2023-08-23 op
668 f9ab77a8 2023-08-23 op return (rv);
669 f9ab77a8 2023-08-23 op }
670 f9ab77a8 2023-08-23 op
671 f9ab77a8 2023-08-23 op void
672 f9ab77a8 2023-08-23 op tls_free(struct tls *ctx)
673 f9ab77a8 2023-08-23 op {
674 f9ab77a8 2023-08-23 op if (ctx == NULL)
675 f9ab77a8 2023-08-23 op return;
676 f9ab77a8 2023-08-23 op
677 f9ab77a8 2023-08-23 op tls_reset(ctx);
678 f9ab77a8 2023-08-23 op
679 f9ab77a8 2023-08-23 op free(ctx);
680 f9ab77a8 2023-08-23 op }
681 f9ab77a8 2023-08-23 op
682 f9ab77a8 2023-08-23 op void
683 f9ab77a8 2023-08-23 op tls_reset(struct tls *ctx)
684 f9ab77a8 2023-08-23 op {
685 f9ab77a8 2023-08-23 op struct tls_sni_ctx *sni, *nsni;
686 f9ab77a8 2023-08-23 op
687 f9ab77a8 2023-08-23 op tls_config_free(ctx->config);
688 f9ab77a8 2023-08-23 op ctx->config = NULL;
689 f9ab77a8 2023-08-23 op
690 f9ab77a8 2023-08-23 op SSL_CTX_free(ctx->ssl_ctx);
691 f9ab77a8 2023-08-23 op SSL_free(ctx->ssl_conn);
692 f9ab77a8 2023-08-23 op X509_free(ctx->ssl_peer_cert);
693 f9ab77a8 2023-08-23 op
694 f9ab77a8 2023-08-23 op ctx->ssl_conn = NULL;
695 f9ab77a8 2023-08-23 op ctx->ssl_ctx = NULL;
696 f9ab77a8 2023-08-23 op ctx->ssl_peer_cert = NULL;
697 f9ab77a8 2023-08-23 op /* X509 objects in chain are freed with the SSL */
698 f9ab77a8 2023-08-23 op ctx->ssl_peer_chain = NULL;
699 f9ab77a8 2023-08-23 op
700 f9ab77a8 2023-08-23 op ctx->socket = -1;
701 f9ab77a8 2023-08-23 op ctx->state = 0;
702 f9ab77a8 2023-08-23 op
703 f9ab77a8 2023-08-23 op free(ctx->servername);
704 f9ab77a8 2023-08-23 op ctx->servername = NULL;
705 f9ab77a8 2023-08-23 op
706 f9ab77a8 2023-08-23 op free(ctx->error.msg);
707 f9ab77a8 2023-08-23 op ctx->error.msg = NULL;
708 f9ab77a8 2023-08-23 op ctx->error.num = -1;
709 f9ab77a8 2023-08-23 op
710 f9ab77a8 2023-08-23 op tls_conninfo_free(ctx->conninfo);
711 f9ab77a8 2023-08-23 op ctx->conninfo = NULL;
712 f9ab77a8 2023-08-23 op
713 f9ab77a8 2023-08-23 op tls_ocsp_free(ctx->ocsp);
714 f9ab77a8 2023-08-23 op ctx->ocsp = NULL;
715 f9ab77a8 2023-08-23 op
716 f9ab77a8 2023-08-23 op for (sni = ctx->sni_ctx; sni != NULL; sni = nsni) {
717 f9ab77a8 2023-08-23 op nsni = sni->next;
718 f9ab77a8 2023-08-23 op tls_sni_ctx_free(sni);
719 f9ab77a8 2023-08-23 op }
720 f9ab77a8 2023-08-23 op ctx->sni_ctx = NULL;
721 f9ab77a8 2023-08-23 op
722 f9ab77a8 2023-08-23 op ctx->read_cb = NULL;
723 f9ab77a8 2023-08-23 op ctx->write_cb = NULL;
724 f9ab77a8 2023-08-23 op ctx->cb_arg = NULL;
725 f9ab77a8 2023-08-23 op }
726 f9ab77a8 2023-08-23 op
727 f9ab77a8 2023-08-23 op int
728 f9ab77a8 2023-08-23 op tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix)
729 f9ab77a8 2023-08-23 op {
730 f9ab77a8 2023-08-23 op const char *errstr = "unknown error";
731 f9ab77a8 2023-08-23 op unsigned long err;
732 f9ab77a8 2023-08-23 op int ssl_err;
733 f9ab77a8 2023-08-23 op
734 f9ab77a8 2023-08-23 op ssl_err = SSL_get_error(ssl_conn, ssl_ret);
735 f9ab77a8 2023-08-23 op switch (ssl_err) {
736 f9ab77a8 2023-08-23 op case SSL_ERROR_NONE:
737 f9ab77a8 2023-08-23 op case SSL_ERROR_ZERO_RETURN:
738 f9ab77a8 2023-08-23 op return (0);
739 f9ab77a8 2023-08-23 op
740 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_READ:
741 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLIN);
742 f9ab77a8 2023-08-23 op
743 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_WRITE:
744 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLOUT);
745 f9ab77a8 2023-08-23 op
746 f9ab77a8 2023-08-23 op case SSL_ERROR_SYSCALL:
747 f9ab77a8 2023-08-23 op if ((err = ERR_peek_error()) != 0) {
748 f9ab77a8 2023-08-23 op errstr = ERR_error_string(err, NULL);
749 f9ab77a8 2023-08-23 op } else if (ssl_ret == 0) {
750 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
751 f9ab77a8 2023-08-23 op ctx->state |= TLS_EOF_NO_CLOSE_NOTIFY;
752 f9ab77a8 2023-08-23 op return (0);
753 f9ab77a8 2023-08-23 op }
754 f9ab77a8 2023-08-23 op errstr = "unexpected EOF";
755 f9ab77a8 2023-08-23 op } else if (ssl_ret == -1) {
756 f9ab77a8 2023-08-23 op errstr = strerror(errno);
757 f9ab77a8 2023-08-23 op }
758 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
759 f9ab77a8 2023-08-23 op return (-1);
760 f9ab77a8 2023-08-23 op
761 f9ab77a8 2023-08-23 op case SSL_ERROR_SSL:
762 f9ab77a8 2023-08-23 op if ((err = ERR_peek_error()) != 0) {
763 f9ab77a8 2023-08-23 op errstr = ERR_error_string(err, NULL);
764 f9ab77a8 2023-08-23 op }
765 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
766 f9ab77a8 2023-08-23 op return (-1);
767 f9ab77a8 2023-08-23 op
768 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_CONNECT:
769 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_ACCEPT:
770 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_X509_LOOKUP:
771 f9ab77a8 2023-08-23 op default:
772 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed (%d)", prefix, ssl_err);
773 f9ab77a8 2023-08-23 op return (-1);
774 f9ab77a8 2023-08-23 op }
775 f9ab77a8 2023-08-23 op }
776 f9ab77a8 2023-08-23 op
777 f9ab77a8 2023-08-23 op int
778 f9ab77a8 2023-08-23 op tls_handshake(struct tls *ctx)
779 f9ab77a8 2023-08-23 op {
780 f9ab77a8 2023-08-23 op int rv = -1;
781 f9ab77a8 2023-08-23 op
782 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
783 f9ab77a8 2023-08-23 op
784 f9ab77a8 2023-08-23 op if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
785 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "invalid operation for context");
786 f9ab77a8 2023-08-23 op goto out;
787 f9ab77a8 2023-08-23 op }
788 f9ab77a8 2023-08-23 op
789 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
790 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "handshake already completed");
791 f9ab77a8 2023-08-23 op goto out;
792 f9ab77a8 2023-08-23 op }
793 f9ab77a8 2023-08-23 op
794 f9ab77a8 2023-08-23 op if ((ctx->flags & TLS_CLIENT) != 0)
795 f9ab77a8 2023-08-23 op rv = tls_handshake_client(ctx);
796 f9ab77a8 2023-08-23 op else if ((ctx->flags & TLS_SERVER_CONN) != 0)
797 f9ab77a8 2023-08-23 op rv = tls_handshake_server(ctx);
798 f9ab77a8 2023-08-23 op
799 f9ab77a8 2023-08-23 op if (rv == 0) {
800 f9ab77a8 2023-08-23 op ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn);
801 f9ab77a8 2023-08-23 op ctx->ssl_peer_chain = SSL_get_peer_cert_chain(ctx->ssl_conn);
802 f9ab77a8 2023-08-23 op if (tls_conninfo_populate(ctx) == -1)
803 f9ab77a8 2023-08-23 op rv = -1;
804 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
805 f9ab77a8 2023-08-23 op ctx->ocsp = tls_ocsp_setup_from_peer(ctx);
806 f9ab77a8 2023-08-23 op }
807 f9ab77a8 2023-08-23 op out:
808 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
809 f9ab77a8 2023-08-23 op errno = 0;
810 f9ab77a8 2023-08-23 op return (rv);
811 f9ab77a8 2023-08-23 op }
812 f9ab77a8 2023-08-23 op
813 f9ab77a8 2023-08-23 op ssize_t
814 f9ab77a8 2023-08-23 op tls_read(struct tls *ctx, void *buf, size_t buflen)
815 f9ab77a8 2023-08-23 op {
816 f9ab77a8 2023-08-23 op ssize_t rv = -1;
817 f9ab77a8 2023-08-23 op int ssl_ret;
818 f9ab77a8 2023-08-23 op
819 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
820 f9ab77a8 2023-08-23 op
821 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
822 f9ab77a8 2023-08-23 op if ((rv = tls_handshake(ctx)) != 0)
823 f9ab77a8 2023-08-23 op goto out;
824 f9ab77a8 2023-08-23 op }
825 f9ab77a8 2023-08-23 op
826 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
827 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
828 f9ab77a8 2023-08-23 op goto out;
829 f9ab77a8 2023-08-23 op }
830 f9ab77a8 2023-08-23 op
831 f9ab77a8 2023-08-23 op ERR_clear_error();
832 f9ab77a8 2023-08-23 op if ((ssl_ret = SSL_read(ctx->ssl_conn, buf, buflen)) > 0) {
833 f9ab77a8 2023-08-23 op rv = (ssize_t)ssl_ret;
834 f9ab77a8 2023-08-23 op goto out;
835 f9ab77a8 2023-08-23 op }
836 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "read");
837 f9ab77a8 2023-08-23 op
838 f9ab77a8 2023-08-23 op out:
839 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
840 f9ab77a8 2023-08-23 op errno = 0;
841 f9ab77a8 2023-08-23 op return (rv);
842 f9ab77a8 2023-08-23 op }
843 f9ab77a8 2023-08-23 op
844 f9ab77a8 2023-08-23 op ssize_t
845 f9ab77a8 2023-08-23 op tls_write(struct tls *ctx, const void *buf, size_t buflen)
846 f9ab77a8 2023-08-23 op {
847 f9ab77a8 2023-08-23 op ssize_t rv = -1;
848 f9ab77a8 2023-08-23 op int ssl_ret;
849 f9ab77a8 2023-08-23 op
850 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
851 f9ab77a8 2023-08-23 op
852 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
853 f9ab77a8 2023-08-23 op if ((rv = tls_handshake(ctx)) != 0)
854 f9ab77a8 2023-08-23 op goto out;
855 f9ab77a8 2023-08-23 op }
856 f9ab77a8 2023-08-23 op
857 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
858 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
859 f9ab77a8 2023-08-23 op goto out;
860 f9ab77a8 2023-08-23 op }
861 f9ab77a8 2023-08-23 op
862 f9ab77a8 2023-08-23 op ERR_clear_error();
863 f9ab77a8 2023-08-23 op if ((ssl_ret = SSL_write(ctx->ssl_conn, buf, buflen)) > 0) {
864 f9ab77a8 2023-08-23 op rv = (ssize_t)ssl_ret;
865 f9ab77a8 2023-08-23 op goto out;
866 f9ab77a8 2023-08-23 op }
867 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "write");
868 f9ab77a8 2023-08-23 op
869 f9ab77a8 2023-08-23 op out:
870 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
871 f9ab77a8 2023-08-23 op errno = 0;
872 f9ab77a8 2023-08-23 op return (rv);
873 f9ab77a8 2023-08-23 op }
874 f9ab77a8 2023-08-23 op
875 f9ab77a8 2023-08-23 op int
876 f9ab77a8 2023-08-23 op tls_close(struct tls *ctx)
877 f9ab77a8 2023-08-23 op {
878 f9ab77a8 2023-08-23 op int ssl_ret;
879 f9ab77a8 2023-08-23 op int rv = 0;
880 f9ab77a8 2023-08-23 op
881 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
882 f9ab77a8 2023-08-23 op
883 f9ab77a8 2023-08-23 op if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
884 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "invalid operation for context");
885 f9ab77a8 2023-08-23 op rv = -1;
886 f9ab77a8 2023-08-23 op goto out;
887 f9ab77a8 2023-08-23 op }
888 f9ab77a8 2023-08-23 op
889 f9ab77a8 2023-08-23 op if (ctx->state & TLS_SSL_NEEDS_SHUTDOWN) {
890 f9ab77a8 2023-08-23 op ERR_clear_error();
891 f9ab77a8 2023-08-23 op ssl_ret = SSL_shutdown(ctx->ssl_conn);
892 f9ab77a8 2023-08-23 op if (ssl_ret < 0) {
893 f9ab77a8 2023-08-23 op rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret,
894 f9ab77a8 2023-08-23 op "shutdown");
895 f9ab77a8 2023-08-23 op if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
896 f9ab77a8 2023-08-23 op goto out;
897 f9ab77a8 2023-08-23 op }
898 f9ab77a8 2023-08-23 op ctx->state &= ~TLS_SSL_NEEDS_SHUTDOWN;
899 f9ab77a8 2023-08-23 op }
900 f9ab77a8 2023-08-23 op
901 f9ab77a8 2023-08-23 op if (ctx->socket != -1) {
902 f9ab77a8 2023-08-23 op if (shutdown(ctx->socket, SHUT_RDWR) != 0) {
903 f9ab77a8 2023-08-23 op if (rv == 0 &&
904 f9ab77a8 2023-08-23 op errno != ENOTCONN && errno != ECONNRESET) {
905 f9ab77a8 2023-08-23 op tls_set_error(ctx, "shutdown");
906 f9ab77a8 2023-08-23 op rv = -1;
907 f9ab77a8 2023-08-23 op }
908 f9ab77a8 2023-08-23 op }
909 f9ab77a8 2023-08-23 op if (close(ctx->socket) != 0) {
910 f9ab77a8 2023-08-23 op if (rv == 0) {
911 f9ab77a8 2023-08-23 op tls_set_error(ctx, "close");
912 f9ab77a8 2023-08-23 op rv = -1;
913 f9ab77a8 2023-08-23 op }
914 f9ab77a8 2023-08-23 op }
915 f9ab77a8 2023-08-23 op ctx->socket = -1;
916 f9ab77a8 2023-08-23 op }
917 f9ab77a8 2023-08-23 op
918 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_EOF_NO_CLOSE_NOTIFY) != 0) {
919 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "EOF without close notify");
920 f9ab77a8 2023-08-23 op rv = -1;
921 f9ab77a8 2023-08-23 op }
922 f9ab77a8 2023-08-23 op
923 f9ab77a8 2023-08-23 op out:
924 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
925 f9ab77a8 2023-08-23 op errno = 0;
926 f9ab77a8 2023-08-23 op return (rv);
927 f9ab77a8 2023-08-23 op }