Blame


1 f9ab77a8 2023-08-23 op /* $OpenBSD: tls.c,v 1.97 2023/06/18 11:43:03 op 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 f9ab77a8 2023-08-23 op RSA *rsa = NULL;
388 f9ab77a8 2023-08-23 op EC_KEY *eckey = NULL;
389 f9ab77a8 2023-08-23 op int ret = -1;
390 f9ab77a8 2023-08-23 op
391 f9ab77a8 2023-08-23 op /* Only install the pubkey hash if fake private keys are used. */
392 f9ab77a8 2023-08-23 op if (!ctx->config->skip_private_key_check)
393 f9ab77a8 2023-08-23 op return (0);
394 f9ab77a8 2023-08-23 op
395 f9ab77a8 2023-08-23 op if (keypair->pubkey_hash == NULL) {
396 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "public key hash not set");
397 f9ab77a8 2023-08-23 op goto err;
398 f9ab77a8 2023-08-23 op }
399 f9ab77a8 2023-08-23 op
400 f9ab77a8 2023-08-23 op switch (EVP_PKEY_id(pkey)) {
401 f9ab77a8 2023-08-23 op case EVP_PKEY_RSA:
402 f9ab77a8 2023-08-23 op if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL ||
403 f9ab77a8 2023-08-23 op RSA_set_ex_data(rsa, 0, keypair->pubkey_hash) == 0 ||
404 f9ab77a8 2023-08-23 op EVP_PKEY_set1_RSA(pkey, rsa) == 0) {
405 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "RSA key setup failure");
406 f9ab77a8 2023-08-23 op goto err;
407 f9ab77a8 2023-08-23 op }
408 f9ab77a8 2023-08-23 op break;
409 f9ab77a8 2023-08-23 op case EVP_PKEY_EC:
410 f9ab77a8 2023-08-23 op if ((eckey = EVP_PKEY_get1_EC_KEY(pkey)) == NULL ||
411 f9ab77a8 2023-08-23 op EC_KEY_set_ex_data(eckey, 0, keypair->pubkey_hash) == 0 ||
412 f9ab77a8 2023-08-23 op EVP_PKEY_set1_EC_KEY(pkey, eckey) == 0) {
413 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "EC key setup failure");
414 f9ab77a8 2023-08-23 op goto err;
415 f9ab77a8 2023-08-23 op }
416 f9ab77a8 2023-08-23 op break;
417 f9ab77a8 2023-08-23 op default:
418 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "incorrect key type");
419 f9ab77a8 2023-08-23 op goto err;
420 f9ab77a8 2023-08-23 op }
421 f9ab77a8 2023-08-23 op
422 f9ab77a8 2023-08-23 op ret = 0;
423 f9ab77a8 2023-08-23 op
424 f9ab77a8 2023-08-23 op err:
425 f9ab77a8 2023-08-23 op RSA_free(rsa);
426 f9ab77a8 2023-08-23 op EC_KEY_free(eckey);
427 f9ab77a8 2023-08-23 op return (ret);
428 f9ab77a8 2023-08-23 op }
429 f9ab77a8 2023-08-23 op
430 f9ab77a8 2023-08-23 op int
431 f9ab77a8 2023-08-23 op tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx,
432 f9ab77a8 2023-08-23 op struct tls_keypair *keypair, int required)
433 f9ab77a8 2023-08-23 op {
434 f9ab77a8 2023-08-23 op EVP_PKEY *pkey = NULL;
435 f9ab77a8 2023-08-23 op
436 f9ab77a8 2023-08-23 op if (!required &&
437 f9ab77a8 2023-08-23 op keypair->cert_mem == NULL &&
438 f9ab77a8 2023-08-23 op keypair->key_mem == NULL)
439 f9ab77a8 2023-08-23 op return(0);
440 f9ab77a8 2023-08-23 op
441 f9ab77a8 2023-08-23 op if (keypair->cert_mem != NULL) {
442 f9ab77a8 2023-08-23 op if (keypair->cert_len > INT_MAX) {
443 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "certificate too long");
444 f9ab77a8 2023-08-23 op goto err;
445 f9ab77a8 2023-08-23 op }
446 f9ab77a8 2023-08-23 op
447 f9ab77a8 2023-08-23 op if (SSL_CTX_use_certificate_chain_mem(ssl_ctx,
448 f9ab77a8 2023-08-23 op keypair->cert_mem, keypair->cert_len) != 1) {
449 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to load certificate");
450 f9ab77a8 2023-08-23 op goto err;
451 f9ab77a8 2023-08-23 op }
452 f9ab77a8 2023-08-23 op }
453 f9ab77a8 2023-08-23 op
454 f9ab77a8 2023-08-23 op if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1)
455 f9ab77a8 2023-08-23 op goto err;
456 f9ab77a8 2023-08-23 op if (pkey != NULL) {
457 f9ab77a8 2023-08-23 op if (tls_keypair_setup_pkey(ctx, keypair, pkey) == -1)
458 f9ab77a8 2023-08-23 op goto err;
459 f9ab77a8 2023-08-23 op if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) {
460 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to load private key");
461 f9ab77a8 2023-08-23 op goto err;
462 f9ab77a8 2023-08-23 op }
463 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
464 f9ab77a8 2023-08-23 op pkey = NULL;
465 f9ab77a8 2023-08-23 op }
466 f9ab77a8 2023-08-23 op
467 f9ab77a8 2023-08-23 op if (!ctx->config->skip_private_key_check &&
468 f9ab77a8 2023-08-23 op SSL_CTX_check_private_key(ssl_ctx) != 1) {
469 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "private/public key mismatch");
470 f9ab77a8 2023-08-23 op goto err;
471 f9ab77a8 2023-08-23 op }
472 f9ab77a8 2023-08-23 op
473 f9ab77a8 2023-08-23 op return (0);
474 f9ab77a8 2023-08-23 op
475 f9ab77a8 2023-08-23 op err:
476 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
477 f9ab77a8 2023-08-23 op
478 f9ab77a8 2023-08-23 op return (-1);
479 f9ab77a8 2023-08-23 op }
480 f9ab77a8 2023-08-23 op
481 f9ab77a8 2023-08-23 op int
482 f9ab77a8 2023-08-23 op tls_configure_ssl(struct tls *ctx, SSL_CTX *ssl_ctx)
483 f9ab77a8 2023-08-23 op {
484 f9ab77a8 2023-08-23 op SSL_CTX_clear_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
485 f9ab77a8 2023-08-23 op
486 f9ab77a8 2023-08-23 op SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
487 f9ab77a8 2023-08-23 op SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
488 f9ab77a8 2023-08-23 op
489 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2);
490 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv3);
491 f9ab77a8 2023-08-23 op
492 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1);
493 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_1);
494 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
495 f9ab77a8 2023-08-23 op SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
496 f9ab77a8 2023-08-23 op
497 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_0) == 0)
498 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1);
499 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_1) == 0)
500 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_1);
501 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_2) == 0)
502 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
503 f9ab77a8 2023-08-23 op if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_3) == 0)
504 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
505 f9ab77a8 2023-08-23 op
506 f9ab77a8 2023-08-23 op if (ctx->config->alpn != NULL) {
507 f9ab77a8 2023-08-23 op if (SSL_CTX_set_alpn_protos(ssl_ctx, ctx->config->alpn,
508 f9ab77a8 2023-08-23 op ctx->config->alpn_len) != 0) {
509 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to set alpn");
510 f9ab77a8 2023-08-23 op goto err;
511 f9ab77a8 2023-08-23 op }
512 f9ab77a8 2023-08-23 op }
513 f9ab77a8 2023-08-23 op
514 f9ab77a8 2023-08-23 op if (ctx->config->ciphers != NULL) {
515 f9ab77a8 2023-08-23 op if (SSL_CTX_set_cipher_list(ssl_ctx,
516 f9ab77a8 2023-08-23 op ctx->config->ciphers) != 1) {
517 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to set ciphers");
518 f9ab77a8 2023-08-23 op goto err;
519 f9ab77a8 2023-08-23 op }
520 f9ab77a8 2023-08-23 op }
521 f9ab77a8 2023-08-23 op
522 f9ab77a8 2023-08-23 op if (ctx->config->verify_time == 0) {
523 f9ab77a8 2023-08-23 op X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(ssl_ctx),
524 f9ab77a8 2023-08-23 op X509_V_FLAG_NO_CHECK_TIME);
525 f9ab77a8 2023-08-23 op }
526 f9ab77a8 2023-08-23 op
527 f9ab77a8 2023-08-23 op /* Disable any form of session caching by default */
528 f9ab77a8 2023-08-23 op SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_OFF);
529 f9ab77a8 2023-08-23 op SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
530 f9ab77a8 2023-08-23 op
531 f9ab77a8 2023-08-23 op return (0);
532 f9ab77a8 2023-08-23 op
533 f9ab77a8 2023-08-23 op err:
534 f9ab77a8 2023-08-23 op return (-1);
535 f9ab77a8 2023-08-23 op }
536 f9ab77a8 2023-08-23 op
537 f9ab77a8 2023-08-23 op static int
538 f9ab77a8 2023-08-23 op tls_ssl_cert_verify_cb(X509_STORE_CTX *x509_ctx, void *arg)
539 f9ab77a8 2023-08-23 op {
540 f9ab77a8 2023-08-23 op struct tls *ctx = arg;
541 f9ab77a8 2023-08-23 op int x509_err;
542 f9ab77a8 2023-08-23 op
543 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
544 f9ab77a8 2023-08-23 op return (1);
545 f9ab77a8 2023-08-23 op
546 f9ab77a8 2023-08-23 op if ((X509_verify_cert(x509_ctx)) < 0) {
547 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "X509 verify cert failed");
548 f9ab77a8 2023-08-23 op return (0);
549 f9ab77a8 2023-08-23 op }
550 f9ab77a8 2023-08-23 op
551 f9ab77a8 2023-08-23 op x509_err = X509_STORE_CTX_get_error(x509_ctx);
552 f9ab77a8 2023-08-23 op if (x509_err == X509_V_OK)
553 f9ab77a8 2023-08-23 op return (1);
554 f9ab77a8 2023-08-23 op
555 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "certificate verification failed: %s",
556 f9ab77a8 2023-08-23 op X509_verify_cert_error_string(x509_err));
557 f9ab77a8 2023-08-23 op
558 f9ab77a8 2023-08-23 op return (0);
559 f9ab77a8 2023-08-23 op }
560 f9ab77a8 2023-08-23 op
561 f9ab77a8 2023-08-23 op int
562 f9ab77a8 2023-08-23 op tls_configure_ssl_verify(struct tls *ctx, SSL_CTX *ssl_ctx, int verify)
563 f9ab77a8 2023-08-23 op {
564 f9ab77a8 2023-08-23 op size_t ca_len = ctx->config->ca_len;
565 f9ab77a8 2023-08-23 op char *ca_mem = ctx->config->ca_mem;
566 f9ab77a8 2023-08-23 op char *crl_mem = ctx->config->crl_mem;
567 f9ab77a8 2023-08-23 op size_t crl_len = ctx->config->crl_len;
568 f9ab77a8 2023-08-23 op char *ca_free = NULL;
569 f9ab77a8 2023-08-23 op STACK_OF(X509_INFO) *xis = NULL;
570 f9ab77a8 2023-08-23 op X509_STORE *store;
571 f9ab77a8 2023-08-23 op X509_INFO *xi;
572 f9ab77a8 2023-08-23 op BIO *bio = NULL;
573 f9ab77a8 2023-08-23 op int rv = -1;
574 f9ab77a8 2023-08-23 op int i;
575 f9ab77a8 2023-08-23 op
576 f9ab77a8 2023-08-23 op SSL_CTX_set_verify(ssl_ctx, verify, NULL);
577 f9ab77a8 2023-08-23 op SSL_CTX_set_cert_verify_callback(ssl_ctx, tls_ssl_cert_verify_cb, ctx);
578 f9ab77a8 2023-08-23 op
579 f9ab77a8 2023-08-23 op if (ctx->config->verify_depth >= 0)
580 f9ab77a8 2023-08-23 op SSL_CTX_set_verify_depth(ssl_ctx, ctx->config->verify_depth);
581 f9ab77a8 2023-08-23 op
582 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
583 f9ab77a8 2023-08-23 op goto done;
584 f9ab77a8 2023-08-23 op
585 f9ab77a8 2023-08-23 op /* If no CA has been specified, attempt to load the default. */
586 f9ab77a8 2023-08-23 op if (ctx->config->ca_mem == NULL && ctx->config->ca_path == NULL) {
587 f9ab77a8 2023-08-23 op if (tls_config_load_file(&ctx->error, "CA", tls_default_ca_cert_file(),
588 f9ab77a8 2023-08-23 op &ca_mem, &ca_len) != 0)
589 f9ab77a8 2023-08-23 op goto err;
590 f9ab77a8 2023-08-23 op ca_free = ca_mem;
591 f9ab77a8 2023-08-23 op }
592 f9ab77a8 2023-08-23 op
593 f9ab77a8 2023-08-23 op if (ca_mem != NULL) {
594 f9ab77a8 2023-08-23 op if (ca_len > INT_MAX) {
595 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ca too long");
596 f9ab77a8 2023-08-23 op goto err;
597 f9ab77a8 2023-08-23 op }
598 f9ab77a8 2023-08-23 op if (SSL_CTX_load_verify_mem(ssl_ctx, ca_mem, ca_len) != 1) {
599 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ssl verify memory setup failure");
600 f9ab77a8 2023-08-23 op goto err;
601 f9ab77a8 2023-08-23 op }
602 f9ab77a8 2023-08-23 op } else if (SSL_CTX_load_verify_locations(ssl_ctx, NULL,
603 f9ab77a8 2023-08-23 op ctx->config->ca_path) != 1) {
604 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ssl verify locations failure");
605 f9ab77a8 2023-08-23 op goto err;
606 f9ab77a8 2023-08-23 op }
607 f9ab77a8 2023-08-23 op
608 f9ab77a8 2023-08-23 op if (crl_mem != NULL) {
609 f9ab77a8 2023-08-23 op if (crl_len > INT_MAX) {
610 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "crl too long");
611 f9ab77a8 2023-08-23 op goto err;
612 f9ab77a8 2023-08-23 op }
613 f9ab77a8 2023-08-23 op if ((bio = BIO_new_mem_buf(crl_mem, crl_len)) == NULL) {
614 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to create buffer");
615 f9ab77a8 2023-08-23 op goto err;
616 f9ab77a8 2023-08-23 op }
617 f9ab77a8 2023-08-23 op if ((xis = PEM_X509_INFO_read_bio(bio, NULL, tls_password_cb,
618 f9ab77a8 2023-08-23 op NULL)) == NULL) {
619 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "failed to parse crl");
620 f9ab77a8 2023-08-23 op goto err;
621 f9ab77a8 2023-08-23 op }
622 f9ab77a8 2023-08-23 op store = SSL_CTX_get_cert_store(ssl_ctx);
623 f9ab77a8 2023-08-23 op for (i = 0; i < sk_X509_INFO_num(xis); i++) {
624 f9ab77a8 2023-08-23 op xi = sk_X509_INFO_value(xis, i);
625 f9ab77a8 2023-08-23 op if (xi->crl == NULL)
626 f9ab77a8 2023-08-23 op continue;
627 f9ab77a8 2023-08-23 op if (!X509_STORE_add_crl(store, xi->crl)) {
628 f9ab77a8 2023-08-23 op tls_set_error(ctx, "failed to add crl");
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 X509_STORE_set_flags(store,
633 f9ab77a8 2023-08-23 op X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
634 f9ab77a8 2023-08-23 op }
635 f9ab77a8 2023-08-23 op
636 f9ab77a8 2023-08-23 op done:
637 f9ab77a8 2023-08-23 op rv = 0;
638 f9ab77a8 2023-08-23 op
639 f9ab77a8 2023-08-23 op err:
640 f9ab77a8 2023-08-23 op sk_X509_INFO_pop_free(xis, X509_INFO_free);
641 f9ab77a8 2023-08-23 op BIO_free(bio);
642 f9ab77a8 2023-08-23 op free(ca_free);
643 f9ab77a8 2023-08-23 op
644 f9ab77a8 2023-08-23 op return (rv);
645 f9ab77a8 2023-08-23 op }
646 f9ab77a8 2023-08-23 op
647 f9ab77a8 2023-08-23 op void
648 f9ab77a8 2023-08-23 op tls_free(struct tls *ctx)
649 f9ab77a8 2023-08-23 op {
650 f9ab77a8 2023-08-23 op if (ctx == NULL)
651 f9ab77a8 2023-08-23 op return;
652 f9ab77a8 2023-08-23 op
653 f9ab77a8 2023-08-23 op tls_reset(ctx);
654 f9ab77a8 2023-08-23 op
655 f9ab77a8 2023-08-23 op free(ctx);
656 f9ab77a8 2023-08-23 op }
657 f9ab77a8 2023-08-23 op
658 f9ab77a8 2023-08-23 op void
659 f9ab77a8 2023-08-23 op tls_reset(struct tls *ctx)
660 f9ab77a8 2023-08-23 op {
661 f9ab77a8 2023-08-23 op struct tls_sni_ctx *sni, *nsni;
662 f9ab77a8 2023-08-23 op
663 f9ab77a8 2023-08-23 op tls_config_free(ctx->config);
664 f9ab77a8 2023-08-23 op ctx->config = NULL;
665 f9ab77a8 2023-08-23 op
666 f9ab77a8 2023-08-23 op SSL_CTX_free(ctx->ssl_ctx);
667 f9ab77a8 2023-08-23 op SSL_free(ctx->ssl_conn);
668 f9ab77a8 2023-08-23 op X509_free(ctx->ssl_peer_cert);
669 f9ab77a8 2023-08-23 op
670 f9ab77a8 2023-08-23 op ctx->ssl_conn = NULL;
671 f9ab77a8 2023-08-23 op ctx->ssl_ctx = NULL;
672 f9ab77a8 2023-08-23 op ctx->ssl_peer_cert = NULL;
673 f9ab77a8 2023-08-23 op /* X509 objects in chain are freed with the SSL */
674 f9ab77a8 2023-08-23 op ctx->ssl_peer_chain = NULL;
675 f9ab77a8 2023-08-23 op
676 f9ab77a8 2023-08-23 op ctx->socket = -1;
677 f9ab77a8 2023-08-23 op ctx->state = 0;
678 f9ab77a8 2023-08-23 op
679 f9ab77a8 2023-08-23 op free(ctx->servername);
680 f9ab77a8 2023-08-23 op ctx->servername = NULL;
681 f9ab77a8 2023-08-23 op
682 f9ab77a8 2023-08-23 op free(ctx->error.msg);
683 f9ab77a8 2023-08-23 op ctx->error.msg = NULL;
684 f9ab77a8 2023-08-23 op ctx->error.num = -1;
685 f9ab77a8 2023-08-23 op
686 f9ab77a8 2023-08-23 op tls_conninfo_free(ctx->conninfo);
687 f9ab77a8 2023-08-23 op ctx->conninfo = NULL;
688 f9ab77a8 2023-08-23 op
689 f9ab77a8 2023-08-23 op tls_ocsp_free(ctx->ocsp);
690 f9ab77a8 2023-08-23 op ctx->ocsp = NULL;
691 f9ab77a8 2023-08-23 op
692 f9ab77a8 2023-08-23 op for (sni = ctx->sni_ctx; sni != NULL; sni = nsni) {
693 f9ab77a8 2023-08-23 op nsni = sni->next;
694 f9ab77a8 2023-08-23 op tls_sni_ctx_free(sni);
695 f9ab77a8 2023-08-23 op }
696 f9ab77a8 2023-08-23 op ctx->sni_ctx = NULL;
697 f9ab77a8 2023-08-23 op
698 f9ab77a8 2023-08-23 op ctx->read_cb = NULL;
699 f9ab77a8 2023-08-23 op ctx->write_cb = NULL;
700 f9ab77a8 2023-08-23 op ctx->cb_arg = NULL;
701 f9ab77a8 2023-08-23 op }
702 f9ab77a8 2023-08-23 op
703 f9ab77a8 2023-08-23 op int
704 f9ab77a8 2023-08-23 op tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix)
705 f9ab77a8 2023-08-23 op {
706 f9ab77a8 2023-08-23 op const char *errstr = "unknown error";
707 f9ab77a8 2023-08-23 op unsigned long err;
708 f9ab77a8 2023-08-23 op int ssl_err;
709 f9ab77a8 2023-08-23 op
710 f9ab77a8 2023-08-23 op ssl_err = SSL_get_error(ssl_conn, ssl_ret);
711 f9ab77a8 2023-08-23 op switch (ssl_err) {
712 f9ab77a8 2023-08-23 op case SSL_ERROR_NONE:
713 f9ab77a8 2023-08-23 op case SSL_ERROR_ZERO_RETURN:
714 f9ab77a8 2023-08-23 op return (0);
715 f9ab77a8 2023-08-23 op
716 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_READ:
717 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLIN);
718 f9ab77a8 2023-08-23 op
719 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_WRITE:
720 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLOUT);
721 f9ab77a8 2023-08-23 op
722 f9ab77a8 2023-08-23 op case SSL_ERROR_SYSCALL:
723 f9ab77a8 2023-08-23 op if ((err = ERR_peek_error()) != 0) {
724 f9ab77a8 2023-08-23 op errstr = ERR_error_string(err, NULL);
725 f9ab77a8 2023-08-23 op } else if (ssl_ret == 0) {
726 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
727 f9ab77a8 2023-08-23 op ctx->state |= TLS_EOF_NO_CLOSE_NOTIFY;
728 f9ab77a8 2023-08-23 op return (0);
729 f9ab77a8 2023-08-23 op }
730 f9ab77a8 2023-08-23 op errstr = "unexpected EOF";
731 f9ab77a8 2023-08-23 op } else if (ssl_ret == -1) {
732 f9ab77a8 2023-08-23 op errstr = strerror(errno);
733 f9ab77a8 2023-08-23 op }
734 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
735 f9ab77a8 2023-08-23 op return (-1);
736 f9ab77a8 2023-08-23 op
737 f9ab77a8 2023-08-23 op case SSL_ERROR_SSL:
738 f9ab77a8 2023-08-23 op if ((err = ERR_peek_error()) != 0) {
739 f9ab77a8 2023-08-23 op errstr = ERR_error_string(err, NULL);
740 f9ab77a8 2023-08-23 op }
741 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
742 f9ab77a8 2023-08-23 op return (-1);
743 f9ab77a8 2023-08-23 op
744 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_CONNECT:
745 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_ACCEPT:
746 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_X509_LOOKUP:
747 f9ab77a8 2023-08-23 op default:
748 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed (%d)", prefix, ssl_err);
749 f9ab77a8 2023-08-23 op return (-1);
750 f9ab77a8 2023-08-23 op }
751 f9ab77a8 2023-08-23 op }
752 f9ab77a8 2023-08-23 op
753 f9ab77a8 2023-08-23 op int
754 f9ab77a8 2023-08-23 op tls_handshake(struct tls *ctx)
755 f9ab77a8 2023-08-23 op {
756 f9ab77a8 2023-08-23 op int rv = -1;
757 f9ab77a8 2023-08-23 op
758 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
759 f9ab77a8 2023-08-23 op
760 f9ab77a8 2023-08-23 op if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
761 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "invalid operation for context");
762 f9ab77a8 2023-08-23 op goto out;
763 f9ab77a8 2023-08-23 op }
764 f9ab77a8 2023-08-23 op
765 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
766 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "handshake already completed");
767 f9ab77a8 2023-08-23 op goto out;
768 f9ab77a8 2023-08-23 op }
769 f9ab77a8 2023-08-23 op
770 f9ab77a8 2023-08-23 op if ((ctx->flags & TLS_CLIENT) != 0)
771 f9ab77a8 2023-08-23 op rv = tls_handshake_client(ctx);
772 f9ab77a8 2023-08-23 op else if ((ctx->flags & TLS_SERVER_CONN) != 0)
773 f9ab77a8 2023-08-23 op rv = tls_handshake_server(ctx);
774 f9ab77a8 2023-08-23 op
775 f9ab77a8 2023-08-23 op if (rv == 0) {
776 f9ab77a8 2023-08-23 op ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn);
777 f9ab77a8 2023-08-23 op ctx->ssl_peer_chain = SSL_get_peer_cert_chain(ctx->ssl_conn);
778 f9ab77a8 2023-08-23 op if (tls_conninfo_populate(ctx) == -1)
779 f9ab77a8 2023-08-23 op rv = -1;
780 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
781 f9ab77a8 2023-08-23 op ctx->ocsp = tls_ocsp_setup_from_peer(ctx);
782 f9ab77a8 2023-08-23 op }
783 f9ab77a8 2023-08-23 op out:
784 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
785 f9ab77a8 2023-08-23 op errno = 0;
786 f9ab77a8 2023-08-23 op return (rv);
787 f9ab77a8 2023-08-23 op }
788 f9ab77a8 2023-08-23 op
789 f9ab77a8 2023-08-23 op ssize_t
790 f9ab77a8 2023-08-23 op tls_read(struct tls *ctx, void *buf, size_t buflen)
791 f9ab77a8 2023-08-23 op {
792 f9ab77a8 2023-08-23 op ssize_t rv = -1;
793 f9ab77a8 2023-08-23 op int ssl_ret;
794 f9ab77a8 2023-08-23 op
795 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
796 f9ab77a8 2023-08-23 op
797 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
798 f9ab77a8 2023-08-23 op if ((rv = tls_handshake(ctx)) != 0)
799 f9ab77a8 2023-08-23 op goto out;
800 f9ab77a8 2023-08-23 op }
801 f9ab77a8 2023-08-23 op
802 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
803 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
804 f9ab77a8 2023-08-23 op goto out;
805 f9ab77a8 2023-08-23 op }
806 f9ab77a8 2023-08-23 op
807 f9ab77a8 2023-08-23 op ERR_clear_error();
808 f9ab77a8 2023-08-23 op if ((ssl_ret = SSL_read(ctx->ssl_conn, buf, buflen)) > 0) {
809 f9ab77a8 2023-08-23 op rv = (ssize_t)ssl_ret;
810 f9ab77a8 2023-08-23 op goto out;
811 f9ab77a8 2023-08-23 op }
812 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "read");
813 f9ab77a8 2023-08-23 op
814 f9ab77a8 2023-08-23 op out:
815 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
816 f9ab77a8 2023-08-23 op errno = 0;
817 f9ab77a8 2023-08-23 op return (rv);
818 f9ab77a8 2023-08-23 op }
819 f9ab77a8 2023-08-23 op
820 f9ab77a8 2023-08-23 op ssize_t
821 f9ab77a8 2023-08-23 op tls_write(struct tls *ctx, const void *buf, size_t buflen)
822 f9ab77a8 2023-08-23 op {
823 f9ab77a8 2023-08-23 op ssize_t rv = -1;
824 f9ab77a8 2023-08-23 op int ssl_ret;
825 f9ab77a8 2023-08-23 op
826 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
827 f9ab77a8 2023-08-23 op
828 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
829 f9ab77a8 2023-08-23 op if ((rv = tls_handshake(ctx)) != 0)
830 f9ab77a8 2023-08-23 op goto out;
831 f9ab77a8 2023-08-23 op }
832 f9ab77a8 2023-08-23 op
833 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
834 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
835 f9ab77a8 2023-08-23 op goto out;
836 f9ab77a8 2023-08-23 op }
837 f9ab77a8 2023-08-23 op
838 f9ab77a8 2023-08-23 op ERR_clear_error();
839 f9ab77a8 2023-08-23 op if ((ssl_ret = SSL_write(ctx->ssl_conn, buf, buflen)) > 0) {
840 f9ab77a8 2023-08-23 op rv = (ssize_t)ssl_ret;
841 f9ab77a8 2023-08-23 op goto out;
842 f9ab77a8 2023-08-23 op }
843 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "write");
844 f9ab77a8 2023-08-23 op
845 f9ab77a8 2023-08-23 op out:
846 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
847 f9ab77a8 2023-08-23 op errno = 0;
848 f9ab77a8 2023-08-23 op return (rv);
849 f9ab77a8 2023-08-23 op }
850 f9ab77a8 2023-08-23 op
851 f9ab77a8 2023-08-23 op int
852 f9ab77a8 2023-08-23 op tls_close(struct tls *ctx)
853 f9ab77a8 2023-08-23 op {
854 f9ab77a8 2023-08-23 op int ssl_ret;
855 f9ab77a8 2023-08-23 op int rv = 0;
856 f9ab77a8 2023-08-23 op
857 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
858 f9ab77a8 2023-08-23 op
859 f9ab77a8 2023-08-23 op if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
860 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "invalid operation for context");
861 f9ab77a8 2023-08-23 op rv = -1;
862 f9ab77a8 2023-08-23 op goto out;
863 f9ab77a8 2023-08-23 op }
864 f9ab77a8 2023-08-23 op
865 f9ab77a8 2023-08-23 op if (ctx->state & TLS_SSL_NEEDS_SHUTDOWN) {
866 f9ab77a8 2023-08-23 op ERR_clear_error();
867 f9ab77a8 2023-08-23 op ssl_ret = SSL_shutdown(ctx->ssl_conn);
868 f9ab77a8 2023-08-23 op if (ssl_ret < 0) {
869 f9ab77a8 2023-08-23 op rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret,
870 f9ab77a8 2023-08-23 op "shutdown");
871 f9ab77a8 2023-08-23 op if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
872 f9ab77a8 2023-08-23 op goto out;
873 f9ab77a8 2023-08-23 op }
874 f9ab77a8 2023-08-23 op ctx->state &= ~TLS_SSL_NEEDS_SHUTDOWN;
875 f9ab77a8 2023-08-23 op }
876 f9ab77a8 2023-08-23 op
877 f9ab77a8 2023-08-23 op if (ctx->socket != -1) {
878 f9ab77a8 2023-08-23 op if (shutdown(ctx->socket, SHUT_RDWR) != 0) {
879 f9ab77a8 2023-08-23 op if (rv == 0 &&
880 f9ab77a8 2023-08-23 op errno != ENOTCONN && errno != ECONNRESET) {
881 f9ab77a8 2023-08-23 op tls_set_error(ctx, "shutdown");
882 f9ab77a8 2023-08-23 op rv = -1;
883 f9ab77a8 2023-08-23 op }
884 f9ab77a8 2023-08-23 op }
885 f9ab77a8 2023-08-23 op if (close(ctx->socket) != 0) {
886 f9ab77a8 2023-08-23 op if (rv == 0) {
887 f9ab77a8 2023-08-23 op tls_set_error(ctx, "close");
888 f9ab77a8 2023-08-23 op rv = -1;
889 f9ab77a8 2023-08-23 op }
890 f9ab77a8 2023-08-23 op }
891 f9ab77a8 2023-08-23 op ctx->socket = -1;
892 f9ab77a8 2023-08-23 op }
893 f9ab77a8 2023-08-23 op
894 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_EOF_NO_CLOSE_NOTIFY) != 0) {
895 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "EOF without close notify");
896 f9ab77a8 2023-08-23 op rv = -1;
897 f9ab77a8 2023-08-23 op }
898 f9ab77a8 2023-08-23 op
899 f9ab77a8 2023-08-23 op out:
900 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
901 f9ab77a8 2023-08-23 op errno = 0;
902 f9ab77a8 2023-08-23 op return (rv);
903 f9ab77a8 2023-08-23 op }