1 /* $OpenBSD: tls.c,v 1.59 2017/01/26 12:56:37 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
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.
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.
18 #include <sys/socket.h>
25 #include <openssl/bio.h>
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 #include <openssl/pem.h>
29 #include <openssl/x509.h>
32 #include "tls_internal.h"
34 static struct tls_config
*tls_config_default
;
39 static int tls_initialised
= 0;
44 SSL_load_error_strings();
47 if (BIO_sock_init() != 1)
50 if ((tls_config_default
= tls_config_new()) == NULL
)
59 tls_error(struct tls
*ctx
)
61 return ctx
->error
.msg
;
65 tls_error_clear(struct tls_error
*error
)
74 tls_error_vset(struct tls_error
*error
, int errnum
, const char *fmt
, va_list ap
)
79 tls_error_clear(error
);
84 if (vasprintf(&errmsg
, fmt
, ap
) == -1) {
94 if (asprintf(&error
->msg
, "%s: %s", errmsg
, strerror(errnum
)) == -1) {
107 tls_error_set(struct tls_error
*error
, const char *fmt
, ...)
115 rv
= tls_error_vset(error
, errnum
, fmt
, ap
);
122 tls_error_setx(struct tls_error
*error
, const char *fmt
, ...)
128 rv
= tls_error_vset(error
, -1, fmt
, ap
);
135 tls_config_set_error(struct tls_config
*config
, const char *fmt
, ...)
143 rv
= tls_error_vset(&config
->error
, errnum
, fmt
, ap
);
150 tls_config_set_errorx(struct tls_config
*config
, const char *fmt
, ...)
156 rv
= tls_error_vset(&config
->error
, -1, fmt
, ap
);
163 tls_set_error(struct tls
*ctx
, const char *fmt
, ...)
171 rv
= tls_error_vset(&ctx
->error
, errnum
, fmt
, ap
);
178 tls_set_errorx(struct tls
*ctx
, const char *fmt
, ...)
184 rv
= tls_error_vset(&ctx
->error
, -1, fmt
, ap
);
191 tls_set_ssl_errorx(struct tls
*ctx
, const char *fmt
, ...)
196 /* Only set an error if a more specific one does not already exist. */
197 if (ctx
->error
.tls
!= 0)
201 rv
= tls_error_vset(&ctx
->error
, -1, fmt
, ap
);
208 tls_sni_ctx_new(void)
210 return (calloc(1, sizeof(struct tls_sni_ctx
)));
214 tls_sni_ctx_free(struct tls_sni_ctx
*sni_ctx
)
219 SSL_CTX_free(sni_ctx
->ssl_ctx
);
220 X509_free(sni_ctx
->ssl_cert
);
230 if ((ctx
= calloc(1, sizeof(*ctx
))) == NULL
)
233 ctx
->config
= tls_config_default
;
241 tls_configure(struct tls
*ctx
, struct tls_config
*config
)
244 config
= tls_config_default
;
246 ctx
->config
= config
;
248 if ((ctx
->flags
& TLS_SERVER
) != 0)
249 return (tls_configure_server(ctx
));
255 tls_configure_ssl_keypair(struct tls
*ctx
, SSL_CTX
*ssl_ctx
,
256 struct tls_keypair
*keypair
, int required
)
258 EVP_PKEY
*pkey
= NULL
;
263 keypair
->cert_mem
== NULL
&&
264 keypair
->key_mem
== NULL
)
267 if (keypair
->cert_mem
!= NULL
) {
268 if (keypair
->cert_len
> INT_MAX
) {
269 tls_set_errorx(ctx
, "certificate too long");
273 if (SSL_CTX_use_certificate_chain_mem(ssl_ctx
,
274 keypair
->cert_mem
, keypair
->cert_len
) != 1) {
275 tls_set_errorx(ctx
, "failed to load certificate");
280 if (keypair
->key_mem
!= NULL
) {
281 if (keypair
->key_len
> INT_MAX
) {
282 tls_set_errorx(ctx
, "key too long");
286 if ((bio
= BIO_new_mem_buf(keypair
->key_mem
,
287 keypair
->key_len
)) == NULL
) {
288 tls_set_errorx(ctx
, "failed to create buffer");
291 if ((pkey
= PEM_read_bio_PrivateKey(bio
, NULL
, NULL
,
293 tls_set_errorx(ctx
, "failed to read private key");
296 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1) {
297 tls_set_errorx(ctx
, "failed to load private key");
306 if (SSL_CTX_check_private_key(ssl_ctx
) != 1) {
307 tls_set_errorx(ctx
, "private/public key mismatch");
322 tls_configure_ssl(struct tls
*ctx
, SSL_CTX
*ssl_ctx
)
324 SSL_CTX_set_mode(ssl_ctx
, SSL_MODE_ENABLE_PARTIAL_WRITE
);
325 SSL_CTX_set_mode(ssl_ctx
, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
);
327 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_SSLv2
);
328 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_SSLv3
);
330 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TLSv1
);
331 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TLSv1_1
);
332 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TLSv1_2
);
334 if ((ctx
->config
->protocols
& TLS_PROTOCOL_TLSv1_0
) == 0)
335 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TLSv1
);
336 if ((ctx
->config
->protocols
& TLS_PROTOCOL_TLSv1_1
) == 0)
337 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TLSv1_1
);
338 if ((ctx
->config
->protocols
& TLS_PROTOCOL_TLSv1_2
) == 0)
339 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TLSv1_2
);
341 if (ctx
->config
->alpn
!= NULL
) {
342 if (SSL_CTX_set_alpn_protos(ssl_ctx
, ctx
->config
->alpn
,
343 ctx
->config
->alpn_len
) != 0) {
344 tls_set_errorx(ctx
, "failed to set alpn");
349 if (ctx
->config
->ciphers
!= NULL
) {
350 if (SSL_CTX_set_cipher_list(ssl_ctx
,
351 ctx
->config
->ciphers
) != 1) {
352 tls_set_errorx(ctx
, "failed to set ciphers");
357 if (ctx
->config
->verify_time
== 0) {
358 X509_VERIFY_PARAM_set_flags(ssl_ctx
->param
,
359 X509_V_FLAG_NO_CHECK_TIME
);
362 /* Disable any form of session caching by default */
363 SSL_CTX_set_session_cache_mode(ssl_ctx
, SSL_SESS_CACHE_OFF
);
364 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
373 tls_ssl_cert_verify_cb(X509_STORE_CTX
*x509_ctx
, void *arg
)
375 struct tls
*ctx
= arg
;
378 if (ctx
->config
->verify_cert
== 0)
381 if ((X509_verify_cert(x509_ctx
)) < 0) {
382 tls_set_errorx(ctx
, "X509 verify cert failed");
386 x509_err
= X509_STORE_CTX_get_error(x509_ctx
);
387 if (x509_err
== X509_V_OK
)
390 tls_set_errorx(ctx
, "certificate verification failed: %s",
391 X509_verify_cert_error_string(x509_err
));
397 tls_configure_ssl_verify(struct tls
*ctx
, SSL_CTX
*ssl_ctx
, int verify
)
399 size_t ca_len
= ctx
->config
->ca_len
;
400 char *ca_mem
= ctx
->config
->ca_mem
;
401 char *ca_free
= NULL
;
404 SSL_CTX_set_verify(ssl_ctx
, verify
, NULL
);
405 SSL_CTX_set_cert_verify_callback(ssl_ctx
, tls_ssl_cert_verify_cb
, ctx
);
407 if (ctx
->config
->verify_depth
>= 0)
408 SSL_CTX_set_verify_depth(ssl_ctx
, ctx
->config
->verify_depth
);
410 if (ctx
->config
->verify_cert
== 0)
413 /* If no CA has been specified, attempt to load the default. */
414 if (ctx
->config
->ca_mem
== NULL
&& ctx
->config
->ca_path
== NULL
) {
415 if (tls_config_load_file(&ctx
->error
, "CA", _PATH_SSL_CA_FILE
,
416 &ca_mem
, &ca_len
) != 0)
421 if (ca_mem
!= NULL
) {
422 if (ca_len
> INT_MAX
) {
423 tls_set_errorx(ctx
, "ca too long");
426 if (SSL_CTX_load_verify_mem(ssl_ctx
, ca_mem
, ca_len
) != 1) {
427 tls_set_errorx(ctx
, "ssl verify memory setup failure");
430 } else if (SSL_CTX_load_verify_locations(ssl_ctx
, NULL
,
431 ctx
->config
->ca_path
) != 1) {
432 tls_set_errorx(ctx
, "ssl verify locations failure");
446 tls_free(struct tls
*ctx
)
457 tls_reset(struct tls
*ctx
)
459 struct tls_sni_ctx
*sni
, *nsni
;
461 SSL_CTX_free(ctx
->ssl_ctx
);
462 SSL_free(ctx
->ssl_conn
);
463 X509_free(ctx
->ssl_peer_cert
);
465 ctx
->ssl_conn
= NULL
;
467 ctx
->ssl_peer_cert
= NULL
;
472 free(ctx
->servername
);
473 ctx
->servername
= NULL
;
475 free(ctx
->error
.msg
);
476 ctx
->error
.msg
= NULL
;
479 tls_conninfo_free(ctx
->conninfo
);
480 ctx
->conninfo
= NULL
;
482 tls_ocsp_free(ctx
->ocsp
);
485 for (sni
= ctx
->sni_ctx
; sni
!= NULL
; sni
= nsni
) {
487 tls_sni_ctx_free(sni
);
492 ctx
->write_cb
= NULL
;
497 tls_ssl_error(struct tls
*ctx
, SSL
*ssl_conn
, int ssl_ret
, const char *prefix
)
499 const char *errstr
= "unknown error";
503 ssl_err
= SSL_get_error(ssl_conn
, ssl_ret
);
506 case SSL_ERROR_ZERO_RETURN
:
509 case SSL_ERROR_WANT_READ
:
510 return (TLS_WANT_POLLIN
);
512 case SSL_ERROR_WANT_WRITE
:
513 return (TLS_WANT_POLLOUT
);
515 case SSL_ERROR_SYSCALL
:
516 if ((err
= ERR_peek_error()) != 0) {
517 errstr
= ERR_error_string(err
, NULL
);
518 } else if (ssl_ret
== 0) {
519 if ((ctx
->state
& TLS_HANDSHAKE_COMPLETE
) != 0) {
520 ctx
->state
|= TLS_EOF_NO_CLOSE_NOTIFY
;
523 errstr
= "unexpected EOF";
524 } else if (ssl_ret
== -1) {
525 errstr
= strerror(errno
);
527 tls_set_ssl_errorx(ctx
, "%s failed: %s", prefix
, errstr
);
531 if ((err
= ERR_peek_error()) != 0) {
532 errstr
= ERR_error_string(err
, NULL
);
534 tls_set_ssl_errorx(ctx
, "%s failed: %s", prefix
, errstr
);
537 case SSL_ERROR_WANT_CONNECT
:
538 case SSL_ERROR_WANT_ACCEPT
:
539 case SSL_ERROR_WANT_X509_LOOKUP
:
541 tls_set_ssl_errorx(ctx
, "%s failed (%i)", prefix
, ssl_err
);
547 tls_handshake(struct tls
*ctx
)
551 tls_error_clear(&ctx
->error
);
553 if ((ctx
->flags
& (TLS_CLIENT
| TLS_SERVER_CONN
)) == 0) {
554 tls_set_errorx(ctx
, "invalid operation for context");
558 if ((ctx
->flags
& TLS_CLIENT
) != 0)
559 rv
= tls_handshake_client(ctx
);
560 else if ((ctx
->flags
& TLS_SERVER_CONN
) != 0)
561 rv
= tls_handshake_server(ctx
);
564 ctx
->ssl_peer_cert
= SSL_get_peer_certificate(ctx
->ssl_conn
);
565 if (tls_conninfo_populate(ctx
) == -1)
567 if (ctx
->ocsp
== NULL
)
568 ctx
->ocsp
= tls_ocsp_setup_from_peer(ctx
);
571 /* Prevent callers from performing incorrect error handling */
577 tls_read(struct tls
*ctx
, void *buf
, size_t buflen
)
582 tls_error_clear(&ctx
->error
);
584 if ((ctx
->state
& TLS_HANDSHAKE_COMPLETE
) == 0) {
585 if ((rv
= tls_handshake(ctx
)) != 0)
589 if (buflen
> INT_MAX
) {
590 tls_set_errorx(ctx
, "buflen too long");
595 if ((ssl_ret
= SSL_read(ctx
->ssl_conn
, buf
, buflen
)) > 0) {
596 rv
= (ssize_t
)ssl_ret
;
599 rv
= (ssize_t
)tls_ssl_error(ctx
, ctx
->ssl_conn
, ssl_ret
, "read");
602 /* Prevent callers from performing incorrect error handling */
608 tls_write(struct tls
*ctx
, const void *buf
, size_t buflen
)
613 tls_error_clear(&ctx
->error
);
615 if ((ctx
->state
& TLS_HANDSHAKE_COMPLETE
) == 0) {
616 if ((rv
= tls_handshake(ctx
)) != 0)
620 if (buflen
> INT_MAX
) {
621 tls_set_errorx(ctx
, "buflen too long");
626 if ((ssl_ret
= SSL_write(ctx
->ssl_conn
, buf
, buflen
)) > 0) {
627 rv
= (ssize_t
)ssl_ret
;
630 rv
= (ssize_t
)tls_ssl_error(ctx
, ctx
->ssl_conn
, ssl_ret
, "write");
633 /* Prevent callers from performing incorrect error handling */
639 tls_close(struct tls
*ctx
)
644 tls_error_clear(&ctx
->error
);
646 if ((ctx
->flags
& (TLS_CLIENT
| TLS_SERVER_CONN
)) == 0) {
647 tls_set_errorx(ctx
, "invalid operation for context");
652 if (ctx
->state
& TLS_SSL_NEEDS_SHUTDOWN
) {
654 ssl_ret
= SSL_shutdown(ctx
->ssl_conn
);
656 rv
= tls_ssl_error(ctx
, ctx
->ssl_conn
, ssl_ret
,
658 if (rv
== TLS_WANT_POLLIN
|| rv
== TLS_WANT_POLLOUT
)
661 ctx
->state
&= ~TLS_SSL_NEEDS_SHUTDOWN
;
664 if (ctx
->socket
!= -1) {
665 if (shutdown(ctx
->socket
, SHUT_RDWR
) != 0) {
667 errno
!= ENOTCONN
&& errno
!= ECONNRESET
) {
668 tls_set_error(ctx
, "shutdown");
672 if (close(ctx
->socket
) != 0) {
674 tls_set_error(ctx
, "close");
681 if ((ctx
->state
& TLS_EOF_NO_CLOSE_NOTIFY
) != 0) {
682 tls_set_errorx(ctx
, "EOF without close notify");
687 /* Prevent callers from performing incorrect error handling */