From 2be4aa71648e1f72530c53e2dc6f4ecb7bcfca2c Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Mon, 2 Jan 2017 07:56:51 -0500 Subject: [PATCH] Add a helper function for resetting SSL_get_error state. We repeat this in a bunch of places. Change-Id: Iee2c95a13e1645453f101d8be4be9ac78d520387 Reviewed-on: https://boringssl-review.googlesource.com/13051 Reviewed-by: David Benjamin --- ssl/d1_lib.c | 4 +--- ssl/internal.h | 3 +++ ssl/ssl_lib.c | 24 ++++++++++-------------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/ssl/d1_lib.c b/ssl/d1_lib.c index ba3c844b9..258e9ab29 100644 --- a/ssl/d1_lib.c +++ b/ssl/d1_lib.c @@ -236,9 +236,7 @@ int dtls1_check_timeout_num(SSL *ssl) { } int DTLSv1_handle_timeout(SSL *ssl) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); + ssl_reset_error_state(ssl); if (!SSL_is_dtls(ssl)) { return -1; diff --git a/ssl/internal.h b/ssl/internal.h index 97ae6a012..e26fa13ef 100644 --- a/ssl/internal.h +++ b/ssl/internal.h @@ -1946,6 +1946,9 @@ void ssl_set_client_disabled(SSL *ssl); void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock); +/* ssl_reset_error_state resets state for |SSL_get_error|. */ +void ssl_reset_error_state(SSL *ssl); + #if defined(__cplusplus) } /* extern C */ diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index 806bdba8d..2e9f4a6e2 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c @@ -612,11 +612,16 @@ BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; } -int SSL_do_handshake(SSL *ssl) { +void ssl_reset_error_state(SSL *ssl) { + /* Functions which use |SSL_get_error| must reset I/O and error state on + * entry. */ ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ ERR_clear_error(); ERR_clear_system_error(); +} + +int SSL_do_handshake(SSL *ssl) { + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); @@ -735,10 +740,7 @@ static int ssl_do_post_handshake(SSL *ssl) { } static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); @@ -784,10 +786,7 @@ int SSL_peek(SSL *ssl, void *buf, int num) { } int SSL_write(SSL *ssl, const void *buf, int num) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); @@ -815,10 +814,7 @@ int SSL_write(SSL *ssl, const void *buf, int num) { } int SSL_shutdown(SSL *ssl) { - ssl->rwstate = SSL_NOTHING; - /* Functions which use SSL_get_error must clear the error queue on entry. */ - ERR_clear_error(); - ERR_clear_system_error(); + ssl_reset_error_state(ssl); if (ssl->handshake_func == NULL) { OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);