1 ebfc5784 2024-01-07 op /* $OpenBSD: tls.c,v 1.98 2023/07/02 06:37:27 beck Exp $ */
3 f9ab77a8 2023-08-23 op * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
5 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
6 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
7 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
9 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f9ab77a8 2023-08-23 op #include "config.h"
20 f9ab77a8 2023-08-23 op #include <sys/socket.h>
22 f9ab77a8 2023-08-23 op #include <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>
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>
36 f9ab77a8 2023-08-23 op #include <tls.h>
37 f9ab77a8 2023-08-23 op #include "tls_internal.h"
39 f9ab77a8 2023-08-23 op static struct tls_config *tls_config_default;
41 f9ab77a8 2023-08-23 op static int tls_init_rv = -1;
44 f9ab77a8 2023-08-23 op tls_do_init(void)
46 f9ab77a8 2023-08-23 op OPENSSL_init_ssl(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
48 f9ab77a8 2023-08-23 op if (BIO_sock_init() != 1)
51 f9ab77a8 2023-08-23 op if ((tls_config_default = tls_config_new_internal()) == NULL)
54 f9ab77a8 2023-08-23 op tls_config_default->refcount++;
56 f9ab77a8 2023-08-23 op tls_init_rv = 0;
60 f9ab77a8 2023-08-23 op tls_init(void)
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;
68 f9ab77a8 2023-08-23 op tls_error(struct tls *ctx)
70 f9ab77a8 2023-08-23 op return ctx->error.msg;
74 f9ab77a8 2023-08-23 op tls_error_clear(struct tls_error *error)
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;
83 f9ab77a8 2023-08-23 op tls_error_vset(struct tls_error *error, int errnum, const char *fmt, va_list ap)
85 f9ab77a8 2023-08-23 op char *errmsg = NULL;
88 f9ab77a8 2023-08-23 op tls_error_clear(error);
90 f9ab77a8 2023-08-23 op error->num = errnum;
91 f9ab77a8 2023-08-23 op error->tls = 1;
93 f9ab77a8 2023-08-23 op if (vasprintf(&errmsg, fmt, ap) == -1) {
94 f9ab77a8 2023-08-23 op errmsg = NULL;
98 f9ab77a8 2023-08-23 op if (errnum == -1) {
99 f9ab77a8 2023-08-23 op error->msg = errmsg;
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;
110 f9ab77a8 2023-08-23 op free(errmsg);
116 f9ab77a8 2023-08-23 op tls_error_set(struct tls_error *error, const char *fmt, ...)
119 f9ab77a8 2023-08-23 op int errnum, rv;
121 f9ab77a8 2023-08-23 op errnum = errno;
123 f9ab77a8 2023-08-23 op va_start(ap, fmt);
124 f9ab77a8 2023-08-23 op rv = tls_error_vset(error, errnum, fmt, ap);
131 f9ab77a8 2023-08-23 op tls_error_setx(struct tls_error *error, const char *fmt, ...)
136 f9ab77a8 2023-08-23 op va_start(ap, fmt);
137 f9ab77a8 2023-08-23 op rv = tls_error_vset(error, -1, fmt, ap);
144 f9ab77a8 2023-08-23 op tls_config_set_error(struct tls_config *config, const char *fmt, ...)
147 f9ab77a8 2023-08-23 op int errnum, rv;
149 f9ab77a8 2023-08-23 op errnum = errno;
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);
159 f9ab77a8 2023-08-23 op tls_config_set_errorx(struct tls_config *config, const char *fmt, ...)
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);
172 f9ab77a8 2023-08-23 op tls_set_error(struct tls *ctx, const char *fmt, ...)
175 f9ab77a8 2023-08-23 op int errnum, rv;
177 f9ab77a8 2023-08-23 op errnum = errno;
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);
187 f9ab77a8 2023-08-23 op tls_set_errorx(struct tls *ctx, const char *fmt, ...)
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);
200 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(struct tls *ctx, const char *fmt, ...)
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)
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);
216 f9ab77a8 2023-08-23 op struct tls_sni_ctx *
217 f9ab77a8 2023-08-23 op tls_sni_ctx_new(void)
219 f9ab77a8 2023-08-23 op return (calloc(1, sizeof(struct tls_sni_ctx)));
223 f9ab77a8 2023-08-23 op tls_sni_ctx_free(struct tls_sni_ctx *sni_ctx)
225 f9ab77a8 2023-08-23 op if (sni_ctx == NULL)
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);
231 f9ab77a8 2023-08-23 op free(sni_ctx);
235 f9ab77a8 2023-08-23 op tls_new(void)
237 f9ab77a8 2023-08-23 op struct tls *ctx;
239 f9ab77a8 2023-08-23 op if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
240 f9ab77a8 2023-08-23 op return (NULL);
242 f9ab77a8 2023-08-23 op tls_reset(ctx);
244 f9ab77a8 2023-08-23 op if (tls_configure(ctx, tls_config_default) == -1) {
249 f9ab77a8 2023-08-23 op return (ctx);
253 f9ab77a8 2023-08-23 op tls_configure(struct tls *ctx, struct tls_config *config)
255 f9ab77a8 2023-08-23 op if (config == NULL)
256 f9ab77a8 2023-08-23 op config = tls_config_default;
258 f9ab77a8 2023-08-23 op config->refcount++;
260 f9ab77a8 2023-08-23 op tls_config_free(ctx->config);
262 f9ab77a8 2023-08-23 op ctx->config = config;
263 f9ab77a8 2023-08-23 op ctx->keypair = config->keypair;
265 f9ab77a8 2023-08-23 op if ((ctx->flags & TLS_SERVER) != 0)
266 f9ab77a8 2023-08-23 op return (tls_configure_server(ctx));
272 f9ab77a8 2023-08-23 op tls_cert_hash(X509 *cert, char **hash)
274 f9ab77a8 2023-08-23 op char d[EVP_MAX_MD_SIZE], *dhex = NULL;
275 f9ab77a8 2023-08-23 op int dlen, rv = -1;
278 f9ab77a8 2023-08-23 op *hash = NULL;
280 f9ab77a8 2023-08-23 op if (X509_digest(cert, EVP_sha256(), d, &dlen) != 1)
283 f9ab77a8 2023-08-23 op if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
286 f9ab77a8 2023-08-23 op if (asprintf(hash, "SHA256:%s", dhex) == -1) {
287 f9ab77a8 2023-08-23 op *hash = NULL;
299 f9ab77a8 2023-08-23 op tls_cert_pubkey_hash(X509 *cert, char **hash)
301 f9ab77a8 2023-08-23 op char d[EVP_MAX_MD_SIZE], *dhex = NULL;
302 f9ab77a8 2023-08-23 op int dlen, rv = -1;
305 f9ab77a8 2023-08-23 op *hash = NULL;
307 f9ab77a8 2023-08-23 op if (X509_pubkey_digest(cert, EVP_sha256(), d, &dlen) != 1)
310 f9ab77a8 2023-08-23 op if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
313 f9ab77a8 2023-08-23 op if (asprintf(hash, "SHA256:%s", dhex) == -1) {
314 f9ab77a8 2023-08-23 op *hash = NULL;
327 f9ab77a8 2023-08-23 op tls_keypair_to_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY **pkey)
329 f9ab77a8 2023-08-23 op BIO *bio = NULL;
330 f9ab77a8 2023-08-23 op X509 *x509 = NULL;
333 f9ab77a8 2023-08-23 op int ret = -1;
335 f9ab77a8 2023-08-23 op *pkey = NULL;
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;
341 f9ab77a8 2023-08-23 op mem = keypair->key_mem;
342 f9ab77a8 2023-08-23 op len = keypair->key_len;
345 f9ab77a8 2023-08-23 op if (mem == NULL)
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");
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");
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");
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");
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");
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);
385 f9ab77a8 2023-08-23 op tls_keypair_setup_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY *pkey)
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;
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)
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");
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");
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");
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");
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");
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");
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");
446 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "incorrect key type");
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);
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)
462 f9ab77a8 2023-08-23 op EVP_PKEY *pkey = NULL;
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)
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");
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");
482 f9ab77a8 2023-08-23 op if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1)
484 f9ab77a8 2023-08-23 op if (pkey != NULL) {
485 f9ab77a8 2023-08-23 op if (tls_keypair_setup_pkey(ctx, keypair, pkey) == -1)
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");
491 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
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");
504 f9ab77a8 2023-08-23 op EVP_PKEY_free(pkey);
510 f9ab77a8 2023-08-23 op tls_configure_ssl(struct tls *ctx, SSL_CTX *ssl_ctx)
512 f9ab77a8 2023-08-23 op SSL_CTX_clear_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
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);
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);
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);
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);
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");
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");
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);
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);
562 f9ab77a8 2023-08-23 op tls_ssl_cert_verify_cb(X509_STORE_CTX *x509_ctx, void *arg)
564 f9ab77a8 2023-08-23 op struct tls *ctx = arg;
565 f9ab77a8 2023-08-23 op int x509_err;
567 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
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");
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)
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));
586 f9ab77a8 2023-08-23 op tls_configure_ssl_verify(struct tls *ctx, SSL_CTX *ssl_ctx, int verify)
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;
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);
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);
606 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0)
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)
614 f9ab77a8 2023-08-23 op ca_free = ca_mem;
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");
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");
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");
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");
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");
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");
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)
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");
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);
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);
672 f9ab77a8 2023-08-23 op tls_free(struct tls *ctx)
674 f9ab77a8 2023-08-23 op if (ctx == NULL)
677 f9ab77a8 2023-08-23 op tls_reset(ctx);
683 f9ab77a8 2023-08-23 op tls_reset(struct tls *ctx)
685 f9ab77a8 2023-08-23 op struct tls_sni_ctx *sni, *nsni;
687 f9ab77a8 2023-08-23 op tls_config_free(ctx->config);
688 f9ab77a8 2023-08-23 op ctx->config = NULL;
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);
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;
700 f9ab77a8 2023-08-23 op ctx->socket = -1;
701 f9ab77a8 2023-08-23 op ctx->state = 0;
703 f9ab77a8 2023-08-23 op free(ctx->servername);
704 f9ab77a8 2023-08-23 op ctx->servername = NULL;
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;
710 f9ab77a8 2023-08-23 op tls_conninfo_free(ctx->conninfo);
711 f9ab77a8 2023-08-23 op ctx->conninfo = NULL;
713 f9ab77a8 2023-08-23 op tls_ocsp_free(ctx->ocsp);
714 f9ab77a8 2023-08-23 op ctx->ocsp = NULL;
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);
720 f9ab77a8 2023-08-23 op ctx->sni_ctx = NULL;
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;
728 f9ab77a8 2023-08-23 op tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix)
730 f9ab77a8 2023-08-23 op const char *errstr = "unknown error";
731 f9ab77a8 2023-08-23 op unsigned long err;
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:
740 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_READ:
741 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLIN);
743 f9ab77a8 2023-08-23 op case SSL_ERROR_WANT_WRITE:
744 f9ab77a8 2023-08-23 op return (TLS_WANT_POLLOUT);
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;
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);
758 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
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);
765 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
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:
772 f9ab77a8 2023-08-23 op tls_set_ssl_errorx(ctx, "%s failed (%d)", prefix, ssl_err);
778 f9ab77a8 2023-08-23 op tls_handshake(struct tls *ctx)
782 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
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");
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");
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);
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)
804 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
805 f9ab77a8 2023-08-23 op ctx->ocsp = tls_ocsp_setup_from_peer(ctx);
808 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
814 f9ab77a8 2023-08-23 op tls_read(struct tls *ctx, void *buf, size_t buflen)
816 f9ab77a8 2023-08-23 op ssize_t rv = -1;
819 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
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)
826 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
827 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
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;
836 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "read");
839 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
845 f9ab77a8 2023-08-23 op tls_write(struct tls *ctx, const void *buf, size_t buflen)
847 f9ab77a8 2023-08-23 op ssize_t rv = -1;
850 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
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)
857 f9ab77a8 2023-08-23 op if (buflen > INT_MAX) {
858 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "buflen too long");
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;
867 f9ab77a8 2023-08-23 op rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "write");
870 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */
876 f9ab77a8 2023-08-23 op tls_close(struct tls *ctx)
881 f9ab77a8 2023-08-23 op tls_error_clear(&ctx->error);
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");
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,
895 f9ab77a8 2023-08-23 op if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
898 f9ab77a8 2023-08-23 op ctx->state &= ~TLS_SSL_NEEDS_SHUTDOWN;
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");
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");
915 f9ab77a8 2023-08-23 op ctx->socket = -1;
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");
924 f9ab77a8 2023-08-23 op /* Prevent callers from performing incorrect error handling */