Skip to content

Commit

Permalink
Use a flag to track when we need to call SSL_shutdown(). This avoids an
Browse files Browse the repository at this point in the history
issue where by calling tls_close() on a TLS context that has not attempted
a handshake, results in an unexpected failure.

Reported by Vinay Sajip.

ok beck@
  • Loading branch information
jsing committed Jan 26, 2017
1 parent dd784a9 commit 6be5c4c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/lib/libtls/tls.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: tls.c,v 1.57 2017/01/13 17:09:51 deraadt Exp $ */
/* $OpenBSD: tls.c,v 1.58 2017/01/22 08:27:50 claudio Exp $ */
/*
* Copyright (c) 2014 Joel Sing <[email protected]>
*
Expand Down Expand Up @@ -649,7 +649,7 @@ tls_close(struct tls *ctx)
goto out;
}

if (ctx->ssl_conn != NULL) {
if (ctx->state & TLS_SSL_NEEDS_SHUTDOWN) {
ERR_clear_error();
ssl_ret = SSL_shutdown(ctx->ssl_conn);
if (ssl_ret < 0) {
Expand All @@ -658,6 +658,7 @@ tls_close(struct tls *ctx)
if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
goto out;
}
ctx->state &= ~TLS_SSL_NEEDS_SHUTDOWN;
}

if (ctx->socket != -1) {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/libtls/tls_client.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: tls_client.c,v 1.38 2016/12/26 16:20:58 jsing Exp $ */
/* $OpenBSD: tls_client.c,v 1.39 2017/01/12 16:15:58 jsing Exp $ */
/*
* Copyright (c) 2014 Joel Sing <[email protected]>
*
Expand Down Expand Up @@ -297,6 +297,8 @@ tls_handshake_client(struct tls *ctx)
goto err;
}

ctx->state |= TLS_SSL_NEEDS_SHUTDOWN;

ERR_clear_error();
if ((ssl_ret = SSL_connect(ctx->ssl_conn)) != 1) {
rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "handshake");
Expand Down
3 changes: 2 additions & 1 deletion src/lib/libtls/tls_internal.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: tls_internal.h,v 1.50 2016/11/05 15:13:26 beck Exp $ */
/* $OpenBSD: tls_internal.h,v 1.51 2017/01/24 01:48:05 claudio Exp $ */
/*
* Copyright (c) 2014 Jeremie Courreges-Anglas <[email protected]>
* Copyright (c) 2014 Joel Sing <[email protected]>
Expand Down Expand Up @@ -118,6 +118,7 @@ struct tls_conninfo {

#define TLS_EOF_NO_CLOSE_NOTIFY (1 << 0)
#define TLS_HANDSHAKE_COMPLETE (1 << 1)
#define TLS_SSL_NEEDS_SHUTDOWN (1 << 2)

struct tls_ocsp_result {
const char *result_msg;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/libtls/tls_server.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: tls_server.c,v 1.32 2017/01/12 16:15:58 jsing Exp $ */
/* $OpenBSD: tls_server.c,v 1.33 2017/01/24 01:48:05 claudio Exp $ */
/*
* Copyright (c) 2014 Joel Sing <[email protected]>
*
Expand Down Expand Up @@ -457,6 +457,8 @@ tls_handshake_server(struct tls *ctx)
goto err;
}

ctx->state |= TLS_SSL_NEEDS_SHUTDOWN;

ERR_clear_error();
if ((ssl_ret = SSL_accept(ctx->ssl_conn)) != 1) {
rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "handshake");
Expand Down

0 comments on commit 6be5c4c

Please sign in to comment.