From 4d2e7ce47bab29c7c872790edd1e262776176678 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Fri, 8 May 2015 13:29:45 -0400 Subject: [PATCH] Remove OPENSSL_timeval. With DTLSv1_get_timeout de-ctrl-ified, the type checker complains about OPENSSL_timeval. Existing callers all use the real timeval. Now that OPENSSL_timeval is not included in any public structs, simply forward-declare timeval itself in ssl.h and pull in winsock2.h in internal headers. Change-Id: Ieaf110e141578488048c28cdadb14881301a2ce1 Reviewed-on: https://boringssl-review.googlesource.com/4682 Reviewed-by: Adam Langley --- include/openssl/ssl.h | 20 +++++++------------- ssl/d1_lib.c | 18 +++++++++--------- ssl/internal.h | 14 +++++++++++--- ssl/test/bssl_shim.cc | 7 ++++--- ssl/test/packeted_bio.cc | 5 ++--- ssl/test/packeted_bio.h | 12 ++++++++++-- 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h index 7c08adcd..aadc52f0 100644 --- a/include/openssl/ssl.h +++ b/include/openssl/ssl.h @@ -162,6 +162,11 @@ /* wpa_supplicant expects to get the version functions from ssl.h */ #include +/* Forward-declare struct timeval. On Windows, it is defined in winsock2.h and + * Windows headers define too many macros to be included in public headers. + * However, only a forward declaration is needed. */ +struct timeval; + #if defined(__cplusplus) extern "C" { #endif @@ -449,17 +454,6 @@ struct ssl_session_st { char extended_master_secret; }; -#if defined(OPENSSL_WINDOWS) -/* Because of Windows header issues, we can't get the normal declaration of - * timeval. */ -typedef struct OPENSSL_timeval_st { - long tv_sec; - long tv_usec; -} OPENSSL_timeval; -#else -typedef struct timeval OPENSSL_timeval; -#endif - /* SSL_OP_LEGACY_SERVER_CONNECT allows initial connection to servers that don't * support RI */ #define SSL_OP_LEGACY_SERVER_CONNECT 0x00000004L @@ -1037,7 +1031,7 @@ struct ssl_ctx_st { /* current_time_cb, if not NULL, is the function to use to get the current * time. It sets |*out_clock| to the current time. */ - void (*current_time_cb)(const SSL *ssl, OPENSSL_timeval *out_clock); + void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock); }; OPENSSL_EXPORT LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx); @@ -1634,7 +1628,7 @@ DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION) * * NOTE: This function must be queried again whenever the handshake state * machine changes, including when |DTLSv1_handle_timeout| is called. */ -OPENSSL_EXPORT int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out); +OPENSSL_EXPORT int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out); /* DTLSv1_handle_timeout is called when a DTLS handshake timeout expires. If no * timeout had expired, it returns 0. Otherwise, it retransmits the previous diff --git a/ssl/d1_lib.c b/ssl/d1_lib.c index e4809cac..e53156f2 100644 --- a/ssl/d1_lib.c +++ b/ssl/d1_lib.c @@ -81,7 +81,7 @@ * before failing the DTLS handshake. */ #define DTLS1_MAX_TIMEOUTS 12 -static void get_current_time(const SSL *ssl, OPENSSL_timeval *out_clock); +static void get_current_time(const SSL *ssl, struct timeval *out_clock); int dtls1_new(SSL *s) { DTLS1_STATE *d1; @@ -176,7 +176,7 @@ void dtls1_start_timer(SSL *s) { &s->d1->next_timeout); } -int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out) { +int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) { if (!SSL_IS_DTLS(ssl)) { return 0; } @@ -187,19 +187,19 @@ int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out) { } /* Get current time */ - OPENSSL_timeval timenow; + struct timeval timenow; get_current_time(ssl, &timenow); /* If timer already expired, set remaining time to 0 */ if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec || (ssl->d1->next_timeout.tv_sec == timenow.tv_sec && ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) { - memset(out, 0, sizeof(OPENSSL_timeval)); + memset(out, 0, sizeof(struct timeval)); return 1; } /* Calculate time left until timer expires */ - memcpy(out, &ssl->d1->next_timeout, sizeof(OPENSSL_timeval)); + memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval)); out->tv_sec -= timenow.tv_sec; out->tv_usec -= timenow.tv_usec; if (out->tv_usec < 0) { @@ -210,14 +210,14 @@ int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out) { /* If remaining time is less than 15 ms, set it to 0 to prevent issues * because of small devergences with socket timeouts. */ if (out->tv_sec == 0 && out->tv_usec < 15000) { - memset(out, 0, sizeof(OPENSSL_timeval)); + memset(out, 0, sizeof(struct timeval)); } return 1; } int dtls1_is_timer_expired(SSL *s) { - OPENSSL_timeval timeleft; + struct timeval timeleft; /* Get time left until timeout, return false if no timer running */ if (!DTLSv1_get_timeout(s, &timeleft)) { @@ -244,7 +244,7 @@ void dtls1_double_timeout(SSL *s) { void dtls1_stop_timer(SSL *s) { /* Reset everything */ s->d1->num_timeouts = 0; - memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval)); + memset(&s->d1->next_timeout, 0, sizeof(struct timeval)); s->d1->timeout_duration = 1; BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &s->d1->next_timeout); @@ -294,7 +294,7 @@ int DTLSv1_handle_timeout(SSL *ssl) { return dtls1_retransmit_buffered_messages(ssl); } -static void get_current_time(const SSL *ssl, OPENSSL_timeval *out_clock) { +static void get_current_time(const SSL *ssl, struct timeval *out_clock) { if (ssl->ctx->current_time_cb != NULL) { ssl->ctx->current_time_cb(ssl, out_clock); return; diff --git a/ssl/internal.h b/ssl/internal.h index ee76a73c..1d8b2bf6 100644 --- a/ssl/internal.h +++ b/ssl/internal.h @@ -149,6 +149,15 @@ #include #include +#if defined(OPENSSL_WINDOWS) +/* Windows defines struct timeval in winsock2.h. */ +#pragma warning(push, 3) +#include +#pragma warning(pop) +#else +#include +#endif + /* Cipher suites. */ @@ -739,9 +748,8 @@ typedef struct dtls1_state_st { unsigned int num_timeouts; /* Indicates when the last handshake msg or heartbeat sent will - * timeout. Because of header issues on Windows, this cannot actually be a - * struct timeval. */ - OPENSSL_timeval next_timeout; + * timeout. */ + struct timeval next_timeout; /* Timeout duration */ unsigned short timeout_duration; diff --git a/ssl/test/bssl_shim.cc b/ssl/test/bssl_shim.cc index 9750adbf..1cf96f21 100644 --- a/ssl/test/bssl_shim.cc +++ b/ssl/test/bssl_shim.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #else #include @@ -79,10 +80,10 @@ struct TestState { // async_bio is async BIO which pauses reads and writes. BIO *async_bio = nullptr; // clock is the current time for the SSL connection. - OPENSSL_timeval clock; + timeval clock; // clock_delta is how far the clock advanced in the most recent failed // |BIO_read|. - OPENSSL_timeval clock_delta; + timeval clock_delta; ScopedEVP_PKEY channel_id; bool cert_ready = false; ScopedSSL_SESSION session; @@ -285,7 +286,7 @@ static unsigned PskServerCallback(SSL *ssl, const char *identity, return config->psk.size(); } -static void CurrentTimeCallback(const SSL *ssl, OPENSSL_timeval *out_clock) { +static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { *out_clock = GetTestState(ssl)->clock; } diff --git a/ssl/test/packeted_bio.cc b/ssl/test/packeted_bio.cc index a2d1a5d0..e831082a 100644 --- a/ssl/test/packeted_bio.cc +++ b/ssl/test/packeted_bio.cc @@ -110,8 +110,7 @@ static int PacketedRead(BIO *bio, char *out, int outl) { (static_cast(buf[6]) << 8) | static_cast(buf[7]); timeout /= 1000; // Convert nanoseconds to microseconds. - OPENSSL_timeval *out_timeout = - reinterpret_cast(bio->ptr); + timeval *out_timeout = reinterpret_cast(bio->ptr); assert(out_timeout->tv_usec == 0); assert(out_timeout->tv_sec == 0); out_timeout->tv_usec = timeout % 1000000; @@ -209,7 +208,7 @@ const BIO_METHOD g_packeted_bio_method = { } // namespace -ScopedBIO PacketedBioCreate(OPENSSL_timeval *out_timeout) { +ScopedBIO PacketedBioCreate(timeval *out_timeout) { ScopedBIO bio(BIO_new(&g_packeted_bio_method)); if (!bio) { return nullptr; diff --git a/ssl/test/packeted_bio.h b/ssl/test/packeted_bio.h index 7f58297d..30697a5b 100644 --- a/ssl/test/packeted_bio.h +++ b/ssl/test/packeted_bio.h @@ -15,11 +15,19 @@ #ifndef HEADER_PACKETED_BIO #define HEADER_PACKETED_BIO +#include #include -#include #include "../../crypto/test/scoped_types.h" +#if defined(OPENSSL_WINDOWS) +#pragma warning(push, 3) +#include +#pragma warning(pop) +#else +#include +#endif + // PacketedBioCreate creates a filter BIO which implements a reliable in-order // blocking datagram socket. The resulting BIO, on |BIO_read|, may simulate a @@ -30,7 +38,7 @@ // Note: The read timeout simulation is intended to be used with the async BIO // wrapper. It doesn't simulate BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, used in DTLS's // blocking mode. -ScopedBIO PacketedBioCreate(OPENSSL_timeval *out_timeout); +ScopedBIO PacketedBioCreate(timeval *out_timeout); #endif // HEADER_PACKETED_BIO