Skip to content

Commit 7bb196a

Browse files
committed
Handle SSL_shutdown while in init more appropriately
Calling SSL_shutdown while in init previously gave a "1" response, meaning everything was successfully closed down (even though it wasn't). Better is to send our close_notify, but fail when trying to receive one. The problem with doing a shutdown while in the middle of a handshake is that once our close_notify is sent we shouldn't really do anything else (including process handshake/CCS messages) until we've received a close_notify back from the peer. However the peer might send a CCS before acting on our close_notify - so we won't be able to read it because we're not acting on CCS messages! Reviewed-by: Viktor Dukhovni <[email protected]>
1 parent 3aeb934 commit 7bb196a

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

include/openssl/ssl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,7 @@ void ERR_load_SSL_strings(void);
19921992
# define SSL_F_SSL3_SETUP_KEY_BLOCK 157
19931993
# define SSL_F_SSL3_SETUP_READ_BUFFER 156
19941994
# define SSL_F_SSL3_SETUP_WRITE_BUFFER 291
1995+
# define SSL_F_SSL3_SHUTDOWN 396
19951996
# define SSL_F_SSL3_WRITE_BYTES 158
19961997
# define SSL_F_SSL3_WRITE_PENDING 159
19971998
# define SSL_F_SSL_ACCEPT 390
@@ -2344,6 +2345,7 @@ void ERR_load_SSL_strings(void);
23442345
# define SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING 345
23452346
# define SSL_R_SERVERHELLO_TLSEXT 275
23462347
# define SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED 277
2348+
# define SSL_R_SHUTDOWN_WHILE_IN_INIT 407
23472349
# define SSL_R_SIGNATURE_ALGORITHMS_ERROR 360
23482350
# define SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE 220
23492351
# define SSL_R_SRP_A_CALC 361

ssl/s3_lib.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4328,6 +4328,21 @@ int ssl3_shutdown(SSL *s)
43284328
return (ret);
43294329
}
43304330
} else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
4331+
if (SSL_in_init(s)) {
4332+
/*
4333+
* We can't shutdown properly if we are in the middle of a
4334+
* handshake. Doing so is problematic because the peer may send a
4335+
* CCS before it acts on our close_notify. However we should not
4336+
* continue to process received handshake messages or CCS once our
4337+
* close_notify has been sent. Therefore any close_notify from
4338+
* the peer will be unreadable because we have not moved to the next
4339+
* cipher state. Its best just to avoid this can-of-worms. Return
4340+
* an error if we are wanting to wait for a close_notify from the
4341+
* peer and we are in init.
4342+
*/
4343+
SSLerr(SSL_F_SSL3_SHUTDOWN, SSL_R_SHUTDOWN_WHILE_IN_INIT);
4344+
return -1;
4345+
}
43314346
/*
43324347
* If we are waiting for a close from our peer, we are closed
43334348
*/

ssl/ssl_err.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static ERR_STRING_DATA SSL_str_functs[] = {
128128
{ERR_FUNC(SSL_F_SSL3_SETUP_KEY_BLOCK), "ssl3_setup_key_block"},
129129
{ERR_FUNC(SSL_F_SSL3_SETUP_READ_BUFFER), "ssl3_setup_read_buffer"},
130130
{ERR_FUNC(SSL_F_SSL3_SETUP_WRITE_BUFFER), "ssl3_setup_write_buffer"},
131+
{ERR_FUNC(SSL_F_SSL3_SHUTDOWN), "ssl3_shutdown"},
131132
{ERR_FUNC(SSL_F_SSL3_WRITE_BYTES), "ssl3_write_bytes"},
132133
{ERR_FUNC(SSL_F_SSL3_WRITE_PENDING), "ssl3_write_pending"},
133134
{ERR_FUNC(SSL_F_SSL_ACCEPT), "SSL_accept"},
@@ -577,6 +578,7 @@ static ERR_STRING_DATA SSL_str_reasons[] = {
577578
{ERR_REASON(SSL_R_SERVERHELLO_TLSEXT), "serverhello tlsext"},
578579
{ERR_REASON(SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED),
579580
"session id context uninitialized"},
581+
{ERR_REASON(SSL_R_SHUTDOWN_WHILE_IN_INIT), "shutdown while in init"},
580582
{ERR_REASON(SSL_R_SIGNATURE_ALGORITHMS_ERROR),
581583
"signature algorithms error"},
582584
{ERR_REASON(SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE),

ssl/ssl_lib.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,7 @@ int SSL_shutdown(SSL *s)
15641564
return -1;
15651565
}
15661566

1567-
if (!SSL_in_init(s))
1568-
return (s->method->ssl_shutdown(s));
1569-
else
1570-
return (1);
1567+
return s->method->ssl_shutdown(s);
15711568
}
15721569

15731570
int SSL_renegotiate(SSL *s)

0 commit comments

Comments
 (0)