Blob


1 /* $OpenBSD: tls.c,v 1.98 2023/07/02 06:37:27 beck Exp $ */
2 /*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include "config.h"
20 #include <sys/socket.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <openssl/bio.h>
29 #include <openssl/err.h>
30 #include <openssl/evp.h>
31 #include <openssl/pem.h>
32 #include <openssl/safestack.h>
33 #include <openssl/ssl.h>
34 #include <openssl/x509.h>
36 #include <tls.h>
37 #include "tls_internal.h"
39 static struct tls_config *tls_config_default;
41 static int tls_init_rv = -1;
43 static void
44 tls_do_init(void)
45 {
46 OPENSSL_init_ssl(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
48 if (BIO_sock_init() != 1)
49 return;
51 if ((tls_config_default = tls_config_new_internal()) == NULL)
52 return;
54 tls_config_default->refcount++;
56 tls_init_rv = 0;
57 }
59 int
60 tls_init(void)
61 {
62 if (tls_init_rv == -1)
63 tls_do_init();
64 return tls_init_rv;
65 }
67 const char *
68 tls_error(struct tls *ctx)
69 {
70 return ctx->error.msg;
71 }
73 void
74 tls_error_clear(struct tls_error *error)
75 {
76 free(error->msg);
77 error->msg = NULL;
78 error->num = 0;
79 error->tls = 0;
80 }
82 static int
83 tls_error_vset(struct tls_error *error, int errnum, const char *fmt, va_list ap)
84 {
85 char *errmsg = NULL;
86 int rv = -1;
88 tls_error_clear(error);
90 error->num = errnum;
91 error->tls = 1;
93 if (vasprintf(&errmsg, fmt, ap) == -1) {
94 errmsg = NULL;
95 goto err;
96 }
98 if (errnum == -1) {
99 error->msg = errmsg;
100 return (0);
103 if (asprintf(&error->msg, "%s: %s", errmsg, strerror(errnum)) == -1) {
104 error->msg = NULL;
105 goto err;
107 rv = 0;
109 err:
110 free(errmsg);
112 return (rv);
115 int
116 tls_error_set(struct tls_error *error, const char *fmt, ...)
118 va_list ap;
119 int errnum, rv;
121 errnum = errno;
123 va_start(ap, fmt);
124 rv = tls_error_vset(error, errnum, fmt, ap);
125 va_end(ap);
127 return (rv);
130 int
131 tls_error_setx(struct tls_error *error, const char *fmt, ...)
133 va_list ap;
134 int rv;
136 va_start(ap, fmt);
137 rv = tls_error_vset(error, -1, fmt, ap);
138 va_end(ap);
140 return (rv);
143 int
144 tls_config_set_error(struct tls_config *config, const char *fmt, ...)
146 va_list ap;
147 int errnum, rv;
149 errnum = errno;
151 va_start(ap, fmt);
152 rv = tls_error_vset(&config->error, errnum, fmt, ap);
153 va_end(ap);
155 return (rv);
158 int
159 tls_config_set_errorx(struct tls_config *config, const char *fmt, ...)
161 va_list ap;
162 int rv;
164 va_start(ap, fmt);
165 rv = tls_error_vset(&config->error, -1, fmt, ap);
166 va_end(ap);
168 return (rv);
171 int
172 tls_set_error(struct tls *ctx, const char *fmt, ...)
174 va_list ap;
175 int errnum, rv;
177 errnum = errno;
179 va_start(ap, fmt);
180 rv = tls_error_vset(&ctx->error, errnum, fmt, ap);
181 va_end(ap);
183 return (rv);
186 int
187 tls_set_errorx(struct tls *ctx, const char *fmt, ...)
189 va_list ap;
190 int rv;
192 va_start(ap, fmt);
193 rv = tls_error_vset(&ctx->error, -1, fmt, ap);
194 va_end(ap);
196 return (rv);
199 int
200 tls_set_ssl_errorx(struct tls *ctx, const char *fmt, ...)
202 va_list ap;
203 int rv;
205 /* Only set an error if a more specific one does not already exist. */
206 if (ctx->error.tls != 0)
207 return (0);
209 va_start(ap, fmt);
210 rv = tls_error_vset(&ctx->error, -1, fmt, ap);
211 va_end(ap);
213 return (rv);
216 struct tls_sni_ctx *
217 tls_sni_ctx_new(void)
219 return (calloc(1, sizeof(struct tls_sni_ctx)));
222 void
223 tls_sni_ctx_free(struct tls_sni_ctx *sni_ctx)
225 if (sni_ctx == NULL)
226 return;
228 SSL_CTX_free(sni_ctx->ssl_ctx);
229 X509_free(sni_ctx->ssl_cert);
231 free(sni_ctx);
234 struct tls *
235 tls_new(void)
237 struct tls *ctx;
239 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
240 return (NULL);
242 tls_reset(ctx);
244 if (tls_configure(ctx, tls_config_default) == -1) {
245 free(ctx);
246 return NULL;
249 return (ctx);
252 int
253 tls_configure(struct tls *ctx, struct tls_config *config)
255 if (config == NULL)
256 config = tls_config_default;
258 config->refcount++;
260 tls_config_free(ctx->config);
262 ctx->config = config;
263 ctx->keypair = config->keypair;
265 if ((ctx->flags & TLS_SERVER) != 0)
266 return (tls_configure_server(ctx));
268 return (0);
271 int
272 tls_cert_hash(X509 *cert, char **hash)
274 char d[EVP_MAX_MD_SIZE], *dhex = NULL;
275 int dlen, rv = -1;
277 free(*hash);
278 *hash = NULL;
280 if (X509_digest(cert, EVP_sha256(), d, &dlen) != 1)
281 goto err;
283 if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
284 goto err;
286 if (asprintf(hash, "SHA256:%s", dhex) == -1) {
287 *hash = NULL;
288 goto err;
291 rv = 0;
292 err:
293 free(dhex);
295 return (rv);
298 int
299 tls_cert_pubkey_hash(X509 *cert, char **hash)
301 char d[EVP_MAX_MD_SIZE], *dhex = NULL;
302 int dlen, rv = -1;
304 free(*hash);
305 *hash = NULL;
307 if (X509_pubkey_digest(cert, EVP_sha256(), d, &dlen) != 1)
308 goto err;
310 if (tls_hex_string(d, dlen, &dhex, NULL) != 0)
311 goto err;
313 if (asprintf(hash, "SHA256:%s", dhex) == -1) {
314 *hash = NULL;
315 goto err;
318 rv = 0;
320 err:
321 free(dhex);
323 return (rv);
326 static int
327 tls_keypair_to_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY **pkey)
329 BIO *bio = NULL;
330 X509 *x509 = NULL;
331 char *mem;
332 size_t len;
333 int ret = -1;
335 *pkey = NULL;
337 if (ctx->config->use_fake_private_key) {
338 mem = keypair->cert_mem;
339 len = keypair->cert_len;
340 } else {
341 mem = keypair->key_mem;
342 len = keypair->key_len;
345 if (mem == NULL)
346 return (0);
348 if (len > INT_MAX) {
349 tls_set_errorx(ctx, ctx->config->use_fake_private_key ?
350 "cert too long" : "key too long");
351 goto err;
354 if ((bio = BIO_new_mem_buf(mem, len)) == NULL) {
355 tls_set_errorx(ctx, "failed to create buffer");
356 goto err;
359 if (ctx->config->use_fake_private_key) {
360 if ((x509 = PEM_read_bio_X509(bio, NULL, tls_password_cb,
361 NULL)) == NULL) {
362 tls_set_errorx(ctx, "failed to read X509 certificate");
363 goto err;
365 if ((*pkey = X509_get_pubkey(x509)) == NULL) {
366 tls_set_errorx(ctx, "failed to retrieve pubkey");
367 goto err;
369 } else {
370 if ((*pkey = PEM_read_bio_PrivateKey(bio, NULL, tls_password_cb,
371 NULL)) == NULL) {
372 tls_set_errorx(ctx, "failed to read private key");
373 goto err;
377 ret = 0;
378 err:
379 BIO_free(bio);
380 X509_free(x509);
381 return (ret);
384 static int
385 tls_keypair_setup_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY *pkey)
387 RSA_METHOD *rsa_method;
388 EC_KEY_METHOD *ecdsa_method;
389 RSA *rsa = NULL;
390 EC_KEY *eckey = NULL;
391 int ret = -1;
393 /* Only install the pubkey hash if fake private keys are used. */
394 if (!ctx->config->skip_private_key_check)
395 return (0);
397 if (keypair->pubkey_hash == NULL) {
398 tls_set_errorx(ctx, "public key hash not set");
399 goto err;
402 switch (EVP_PKEY_id(pkey)) {
403 case EVP_PKEY_RSA:
404 if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL ||
405 RSA_set_ex_data(rsa, 0, keypair->pubkey_hash) == 0) {
406 tls_set_errorx(ctx, "RSA key setup failure");
407 goto err;
409 if (ctx->config->sign_cb != NULL) {
410 rsa_method = tls_signer_rsa_method();
411 if (rsa_method == NULL ||
412 RSA_set_ex_data(rsa, 1, ctx->config) == 0 ||
413 RSA_set_method(rsa, rsa_method) == 0) {
414 tls_set_errorx(ctx, "failed to setup RSA key");
415 goto err;
418 /* Reset the key to work around caching in OpenSSL 3. */
419 if (EVP_PKEY_set1_RSA(pkey, rsa) == 0) {
420 tls_set_errorx(ctx, "failed to set RSA key");
421 goto err;
423 break;
424 case EVP_PKEY_EC:
425 if ((eckey = EVP_PKEY_get1_EC_KEY(pkey)) == NULL ||
426 EC_KEY_set_ex_data(eckey, 0, keypair->pubkey_hash) == 0) {
427 tls_set_errorx(ctx, "EC key setup failure");
428 goto err;
430 if (ctx->config->sign_cb != NULL) {
431 ecdsa_method = tls_signer_ecdsa_method();
432 if (ecdsa_method == NULL ||
433 EC_KEY_set_ex_data(eckey, 1, ctx->config) == 0 ||
434 EC_KEY_set_method(eckey, ecdsa_method) == 0) {
435 tls_set_errorx(ctx, "failed to setup EC key");
436 goto err;
439 /* Reset the key to work around caching in OpenSSL 3. */
440 if (EVP_PKEY_set1_EC_KEY(pkey, eckey) == 0) {
441 tls_set_errorx(ctx, "failed to set EC key");
442 goto err;
444 break;
445 default:
446 tls_set_errorx(ctx, "incorrect key type");
447 goto err;
450 ret = 0;
452 err:
453 RSA_free(rsa);
454 EC_KEY_free(eckey);
455 return (ret);
458 int
459 tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx,
460 struct tls_keypair *keypair, int required)
462 EVP_PKEY *pkey = NULL;
464 if (!required &&
465 keypair->cert_mem == NULL &&
466 keypair->key_mem == NULL)
467 return(0);
469 if (keypair->cert_mem != NULL) {
470 if (keypair->cert_len > INT_MAX) {
471 tls_set_errorx(ctx, "certificate too long");
472 goto err;
475 if (SSL_CTX_use_certificate_chain_mem(ssl_ctx,
476 keypair->cert_mem, keypair->cert_len) != 1) {
477 tls_set_errorx(ctx, "failed to load certificate");
478 goto err;
482 if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1)
483 goto err;
484 if (pkey != NULL) {
485 if (tls_keypair_setup_pkey(ctx, keypair, pkey) == -1)
486 goto err;
487 if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) {
488 tls_set_errorx(ctx, "failed to load private key");
489 goto err;
491 EVP_PKEY_free(pkey);
492 pkey = NULL;
495 if (!ctx->config->skip_private_key_check &&
496 SSL_CTX_check_private_key(ssl_ctx) != 1) {
497 tls_set_errorx(ctx, "private/public key mismatch");
498 goto err;
501 return (0);
503 err:
504 EVP_PKEY_free(pkey);
506 return (-1);
509 int
510 tls_configure_ssl(struct tls *ctx, SSL_CTX *ssl_ctx)
512 SSL_CTX_clear_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
514 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
515 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
517 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2);
518 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv3);
519 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1);
520 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_1);
522 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
523 SSL_CTX_clear_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
525 if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_2) == 0)
526 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_2);
527 if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_3) == 0)
528 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TLSv1_3);
530 if (ctx->config->alpn != NULL) {
531 if (SSL_CTX_set_alpn_protos(ssl_ctx, ctx->config->alpn,
532 ctx->config->alpn_len) != 0) {
533 tls_set_errorx(ctx, "failed to set alpn");
534 goto err;
538 if (ctx->config->ciphers != NULL) {
539 if (SSL_CTX_set_cipher_list(ssl_ctx,
540 ctx->config->ciphers) != 1) {
541 tls_set_errorx(ctx, "failed to set ciphers");
542 goto err;
546 if (ctx->config->verify_time == 0) {
547 X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(ssl_ctx),
548 X509_V_FLAG_NO_CHECK_TIME);
551 /* Disable any form of session caching by default */
552 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_OFF);
553 SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_TICKET);
555 return (0);
557 err:
558 return (-1);
561 static int
562 tls_ssl_cert_verify_cb(X509_STORE_CTX *x509_ctx, void *arg)
564 struct tls *ctx = arg;
565 int x509_err;
567 if (ctx->config->verify_cert == 0)
568 return (1);
570 if ((X509_verify_cert(x509_ctx)) < 0) {
571 tls_set_errorx(ctx, "X509 verify cert failed");
572 return (0);
575 x509_err = X509_STORE_CTX_get_error(x509_ctx);
576 if (x509_err == X509_V_OK)
577 return (1);
579 tls_set_errorx(ctx, "certificate verification failed: %s",
580 X509_verify_cert_error_string(x509_err));
582 return (0);
585 int
586 tls_configure_ssl_verify(struct tls *ctx, SSL_CTX *ssl_ctx, int verify)
588 size_t ca_len = ctx->config->ca_len;
589 char *ca_mem = ctx->config->ca_mem;
590 char *crl_mem = ctx->config->crl_mem;
591 size_t crl_len = ctx->config->crl_len;
592 char *ca_free = NULL;
593 STACK_OF(X509_INFO) *xis = NULL;
594 X509_STORE *store;
595 X509_INFO *xi;
596 BIO *bio = NULL;
597 int rv = -1;
598 int i;
600 SSL_CTX_set_verify(ssl_ctx, verify, NULL);
601 SSL_CTX_set_cert_verify_callback(ssl_ctx, tls_ssl_cert_verify_cb, ctx);
603 if (ctx->config->verify_depth >= 0)
604 SSL_CTX_set_verify_depth(ssl_ctx, ctx->config->verify_depth);
606 if (ctx->config->verify_cert == 0)
607 goto done;
609 /* If no CA has been specified, attempt to load the default. */
610 if (ctx->config->ca_mem == NULL && ctx->config->ca_path == NULL) {
611 if (tls_config_load_file(&ctx->error, "CA", tls_default_ca_cert_file(),
612 &ca_mem, &ca_len) != 0)
613 goto err;
614 ca_free = ca_mem;
617 if (ca_mem != NULL) {
618 if (ca_len > INT_MAX) {
619 tls_set_errorx(ctx, "ca too long");
620 goto err;
622 if (SSL_CTX_load_verify_mem(ssl_ctx, ca_mem, ca_len) != 1) {
623 tls_set_errorx(ctx, "ssl verify memory setup failure");
624 goto err;
626 } else if (SSL_CTX_load_verify_locations(ssl_ctx, NULL,
627 ctx->config->ca_path) != 1) {
628 tls_set_errorx(ctx, "ssl verify locations failure");
629 goto err;
632 if (crl_mem != NULL) {
633 if (crl_len > INT_MAX) {
634 tls_set_errorx(ctx, "crl too long");
635 goto err;
637 if ((bio = BIO_new_mem_buf(crl_mem, crl_len)) == NULL) {
638 tls_set_errorx(ctx, "failed to create buffer");
639 goto err;
641 if ((xis = PEM_X509_INFO_read_bio(bio, NULL, tls_password_cb,
642 NULL)) == NULL) {
643 tls_set_errorx(ctx, "failed to parse crl");
644 goto err;
646 store = SSL_CTX_get_cert_store(ssl_ctx);
647 for (i = 0; i < sk_X509_INFO_num(xis); i++) {
648 xi = sk_X509_INFO_value(xis, i);
649 if (xi->crl == NULL)
650 continue;
651 if (!X509_STORE_add_crl(store, xi->crl)) {
652 tls_set_error(ctx, "failed to add crl");
653 goto err;
656 X509_STORE_set_flags(store,
657 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
660 done:
661 rv = 0;
663 err:
664 sk_X509_INFO_pop_free(xis, X509_INFO_free);
665 BIO_free(bio);
666 free(ca_free);
668 return (rv);
671 void
672 tls_free(struct tls *ctx)
674 if (ctx == NULL)
675 return;
677 tls_reset(ctx);
679 free(ctx);
682 void
683 tls_reset(struct tls *ctx)
685 struct tls_sni_ctx *sni, *nsni;
687 tls_config_free(ctx->config);
688 ctx->config = NULL;
690 SSL_CTX_free(ctx->ssl_ctx);
691 SSL_free(ctx->ssl_conn);
692 X509_free(ctx->ssl_peer_cert);
694 ctx->ssl_conn = NULL;
695 ctx->ssl_ctx = NULL;
696 ctx->ssl_peer_cert = NULL;
697 /* X509 objects in chain are freed with the SSL */
698 ctx->ssl_peer_chain = NULL;
700 ctx->socket = -1;
701 ctx->state = 0;
703 free(ctx->servername);
704 ctx->servername = NULL;
706 free(ctx->error.msg);
707 ctx->error.msg = NULL;
708 ctx->error.num = -1;
710 tls_conninfo_free(ctx->conninfo);
711 ctx->conninfo = NULL;
713 tls_ocsp_free(ctx->ocsp);
714 ctx->ocsp = NULL;
716 for (sni = ctx->sni_ctx; sni != NULL; sni = nsni) {
717 nsni = sni->next;
718 tls_sni_ctx_free(sni);
720 ctx->sni_ctx = NULL;
722 ctx->read_cb = NULL;
723 ctx->write_cb = NULL;
724 ctx->cb_arg = NULL;
727 int
728 tls_ssl_error(struct tls *ctx, SSL *ssl_conn, int ssl_ret, const char *prefix)
730 const char *errstr = "unknown error";
731 unsigned long err;
732 int ssl_err;
734 ssl_err = SSL_get_error(ssl_conn, ssl_ret);
735 switch (ssl_err) {
736 case SSL_ERROR_NONE:
737 case SSL_ERROR_ZERO_RETURN:
738 return (0);
740 case SSL_ERROR_WANT_READ:
741 return (TLS_WANT_POLLIN);
743 case SSL_ERROR_WANT_WRITE:
744 return (TLS_WANT_POLLOUT);
746 case SSL_ERROR_SYSCALL:
747 if ((err = ERR_peek_error()) != 0) {
748 errstr = ERR_error_string(err, NULL);
749 } else if (ssl_ret == 0) {
750 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
751 ctx->state |= TLS_EOF_NO_CLOSE_NOTIFY;
752 return (0);
754 errstr = "unexpected EOF";
755 } else if (ssl_ret == -1) {
756 errstr = strerror(errno);
758 tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
759 return (-1);
761 case SSL_ERROR_SSL:
762 if ((err = ERR_peek_error()) != 0) {
763 errstr = ERR_error_string(err, NULL);
765 tls_set_ssl_errorx(ctx, "%s failed: %s", prefix, errstr);
766 return (-1);
768 case SSL_ERROR_WANT_CONNECT:
769 case SSL_ERROR_WANT_ACCEPT:
770 case SSL_ERROR_WANT_X509_LOOKUP:
771 default:
772 tls_set_ssl_errorx(ctx, "%s failed (%d)", prefix, ssl_err);
773 return (-1);
777 int
778 tls_handshake(struct tls *ctx)
780 int rv = -1;
782 tls_error_clear(&ctx->error);
784 if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
785 tls_set_errorx(ctx, "invalid operation for context");
786 goto out;
789 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) != 0) {
790 tls_set_errorx(ctx, "handshake already completed");
791 goto out;
794 if ((ctx->flags & TLS_CLIENT) != 0)
795 rv = tls_handshake_client(ctx);
796 else if ((ctx->flags & TLS_SERVER_CONN) != 0)
797 rv = tls_handshake_server(ctx);
799 if (rv == 0) {
800 ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn);
801 ctx->ssl_peer_chain = SSL_get_peer_cert_chain(ctx->ssl_conn);
802 if (tls_conninfo_populate(ctx) == -1)
803 rv = -1;
804 if (ctx->ocsp == NULL)
805 ctx->ocsp = tls_ocsp_setup_from_peer(ctx);
807 out:
808 /* Prevent callers from performing incorrect error handling */
809 errno = 0;
810 return (rv);
813 ssize_t
814 tls_read(struct tls *ctx, void *buf, size_t buflen)
816 ssize_t rv = -1;
817 int ssl_ret;
819 tls_error_clear(&ctx->error);
821 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
822 if ((rv = tls_handshake(ctx)) != 0)
823 goto out;
826 if (buflen > INT_MAX) {
827 tls_set_errorx(ctx, "buflen too long");
828 goto out;
831 ERR_clear_error();
832 if ((ssl_ret = SSL_read(ctx->ssl_conn, buf, buflen)) > 0) {
833 rv = (ssize_t)ssl_ret;
834 goto out;
836 rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "read");
838 out:
839 /* Prevent callers from performing incorrect error handling */
840 errno = 0;
841 return (rv);
844 ssize_t
845 tls_write(struct tls *ctx, const void *buf, size_t buflen)
847 ssize_t rv = -1;
848 int ssl_ret;
850 tls_error_clear(&ctx->error);
852 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0) {
853 if ((rv = tls_handshake(ctx)) != 0)
854 goto out;
857 if (buflen > INT_MAX) {
858 tls_set_errorx(ctx, "buflen too long");
859 goto out;
862 ERR_clear_error();
863 if ((ssl_ret = SSL_write(ctx->ssl_conn, buf, buflen)) > 0) {
864 rv = (ssize_t)ssl_ret;
865 goto out;
867 rv = (ssize_t)tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "write");
869 out:
870 /* Prevent callers from performing incorrect error handling */
871 errno = 0;
872 return (rv);
875 int
876 tls_close(struct tls *ctx)
878 int ssl_ret;
879 int rv = 0;
881 tls_error_clear(&ctx->error);
883 if ((ctx->flags & (TLS_CLIENT | TLS_SERVER_CONN)) == 0) {
884 tls_set_errorx(ctx, "invalid operation for context");
885 rv = -1;
886 goto out;
889 if (ctx->state & TLS_SSL_NEEDS_SHUTDOWN) {
890 ERR_clear_error();
891 ssl_ret = SSL_shutdown(ctx->ssl_conn);
892 if (ssl_ret < 0) {
893 rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret,
894 "shutdown");
895 if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
896 goto out;
898 ctx->state &= ~TLS_SSL_NEEDS_SHUTDOWN;
901 if (ctx->socket != -1) {
902 if (shutdown(ctx->socket, SHUT_RDWR) != 0) {
903 if (rv == 0 &&
904 errno != ENOTCONN && errno != ECONNRESET) {
905 tls_set_error(ctx, "shutdown");
906 rv = -1;
909 if (close(ctx->socket) != 0) {
910 if (rv == 0) {
911 tls_set_error(ctx, "close");
912 rv = -1;
915 ctx->socket = -1;
918 if ((ctx->state & TLS_EOF_NO_CLOSE_NOTIFY) != 0) {
919 tls_set_errorx(ctx, "EOF without close notify");
920 rv = -1;
923 out:
924 /* Prevent callers from performing incorrect error handling */
925 errno = 0;
926 return (rv);