Skip to content

Commit

Permalink
Return OPENSSL_timeval by value
Browse files Browse the repository at this point in the history
Also resolve an old TODO.

Change-Id: I67e531d547e2a1f40a7ab547d7211ebbec28102d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/72908
Reviewed-by: Nick Harper <[email protected]>
Commit-Queue: David Benjamin <[email protected]>
  • Loading branch information
davidben authored and Boringssl LUCI CQ committed Nov 15, 2024
1 parent c691779 commit a5d14be
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 49 deletions.
12 changes: 5 additions & 7 deletions ssl/d1_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void dtls1_start_timer(SSL *ssl) {
}

// Set timeout to current time
ssl_get_current_time(ssl, &ssl->d1->next_timeout);
ssl->d1->next_timeout = ssl_ctx_get_current_time(ssl->ctx.get());

// Add duration to current time
ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
Expand Down Expand Up @@ -165,7 +165,7 @@ static void dtls1_double_timeout(SSL *ssl) {

void dtls1_stop_timer(SSL *ssl) {
ssl->d1->num_timeouts = 0;
OPENSSL_memset(&ssl->d1->next_timeout, 0, sizeof(ssl->d1->next_timeout));
ssl->d1->next_timeout = {0, 0};
ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
}

Expand Down Expand Up @@ -209,8 +209,7 @@ int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
return 0;
}

struct OPENSSL_timeval timenow;
ssl_get_current_time(ssl, &timenow);
OPENSSL_timeval timenow = ssl_ctx_get_current_time(ssl->ctx.get());

// If timer already expired, set remaining time to 0.
if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
Expand All @@ -221,8 +220,7 @@ int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
}

// Calculate time left until timer expires.
struct OPENSSL_timeval ret;
OPENSSL_memcpy(&ret, &ssl->d1->next_timeout, sizeof(ret));
OPENSSL_timeval ret = ssl->d1->next_timeout;
ret.tv_sec -= timenow.tv_sec;
if (ret.tv_usec >= timenow.tv_usec) {
ret.tv_usec -= timenow.tv_usec;
Expand All @@ -234,7 +232,7 @@ int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
// If remaining time is less than 15 ms, set it to 0 to prevent issues
// because of small divergences with socket timeouts.
if (ret.tv_sec == 0 && ret.tv_usec < 15000) {
OPENSSL_memset(&ret, 0, sizeof(ret));
ret = {0, 0};
}

// Clamp the result in case of overflow.
Expand Down
3 changes: 1 addition & 2 deletions ssl/extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1927,8 +1927,7 @@ static bool ext_pre_shared_key_add_clienthello(const SSL_HANDSHAKE *hs,
return true;
}

struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());
uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;

Expand Down
3 changes: 1 addition & 2 deletions ssl/handshake_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,7 @@ static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
OPENSSL_memcpy(ssl->s3->server_random, hints->server_random_tls12.data(),
SSL3_RANDOM_SIZE);
} else {
struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());
CRYPTO_store_u32_be(ssl->s3->server_random,
static_cast<uint32_t>(now.tv_sec));
if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Expand Down
4 changes: 1 addition & 3 deletions ssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4054,9 +4054,7 @@ bool ssl_can_write(const SSL *ssl);
// ssl_can_read returns wheter |ssl| is allowed to read.
bool ssl_can_read(const SSL *ssl);

void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock);
void ssl_ctx_get_current_time(const SSL_CTX *ctx,
struct OPENSSL_timeval *out_clock);
OPENSSL_timeval ssl_ctx_get_current_time(const SSL_CTX *ctx);

// ssl_reset_error_state resets state for |SSL_get_error|.
void ssl_reset_error_state(SSL *ssl);
Expand Down
34 changes: 11 additions & 23 deletions ssl/ssl_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,54 +364,42 @@ void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type,
const_cast<SSL *>(ssl), ssl->msg_callback_arg);
}

void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
// TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
// |ssl| arg from |current_time_cb| if possible.
ssl_ctx_get_current_time(ssl->ctx.get(), out_clock);
}

void ssl_ctx_get_current_time(const SSL_CTX *ctx,
struct OPENSSL_timeval *out_clock) {
OPENSSL_timeval ssl_ctx_get_current_time(const SSL_CTX *ctx) {
if (ctx->current_time_cb != NULL) {
// TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
// https://crbug.com/boringssl/155.
struct timeval clock;
ctx->current_time_cb(nullptr /* ssl */, &clock);
if (clock.tv_sec < 0) {
assert(0);
out_clock->tv_sec = 0;
out_clock->tv_usec = 0;
return {0, 0};
} else {
out_clock->tv_sec = (uint64_t)clock.tv_sec;
out_clock->tv_usec = (uint32_t)clock.tv_usec;
return {static_cast<uint64_t>(clock.tv_sec),
static_cast<uint32_t>(clock.tv_usec)};
}
return;
}

#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
out_clock->tv_sec = 1234;
out_clock->tv_usec = 1234;
return {1234, 1234};
#elif defined(OPENSSL_WINDOWS)
struct _timeb time;
_ftime(&time);
if (time.time < 0) {
assert(0);
out_clock->tv_sec = 0;
out_clock->tv_usec = 0;
return {0, 0};
} else {
out_clock->tv_sec = time.time;
out_clock->tv_usec = time.millitm * 1000;
return {static_cast<uint64_t>(time.time),
static_cast<uint32_t>(time.millitm * 1000)};
}
#else
struct timeval clock;
gettimeofday(&clock, NULL);
if (clock.tv_sec < 0) {
assert(0);
out_clock->tv_sec = 0;
out_clock->tv_usec = 0;
return {0, 0};
} else {
out_clock->tv_sec = (uint64_t)clock.tv_sec;
out_clock->tv_usec = (uint32_t)clock.tv_usec;
return {static_cast<uint64_t>(clock.tv_sec),
static_cast<uint32_t>(clock.tv_usec)};
}
#endif
}
Expand Down
15 changes: 5 additions & 10 deletions ssl/ssl_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) {
}

void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) {
struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());

// To avoid overflows and underflows, if we've gone back in time, update the
// time, but mark the session expired.
Expand Down Expand Up @@ -354,8 +353,7 @@ bool ssl_get_new_session(SSL_HANDSHAKE *hs) {
session->is_quic = ssl->quic_method != nullptr;

// Fill in the time from the |SSL_CTX|'s clock.
struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());
session->time = now.tv_sec;

uint16_t version = ssl_protocol_version(ssl);
Expand Down Expand Up @@ -386,8 +384,7 @@ bool ssl_get_new_session(SSL_HANDSHAKE *hs) {
}

bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) {
OPENSSL_timeval now;
ssl_ctx_get_current_time(ctx, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ctx);
{
// Avoid acquiring a write lock in the common case (i.e. a non-default key
// is used or the default keys have not expired yet).
Expand Down Expand Up @@ -593,8 +590,7 @@ bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) {
return false;
}

struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());

// Reject tickets from the future to avoid underflow.
if (now.tv_sec < session->time) {
Expand Down Expand Up @@ -912,8 +908,7 @@ void ssl_update_cache(SSL *ssl) {
// |SSL_CTX_flush_sessions| takes the lock we just released. We could
// merge the critical sections, but we'd then call user code under a
// lock, or compute |now| earlier, even when not flushing.
OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());
SSL_CTX_flush_sessions(ctx, now.tv_sec);
}
}
Expand Down
3 changes: 1 addition & 2 deletions ssl/tls13_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ static enum ssl_ticket_aead_result_t select_session(
client_ticket_age -= session->ticket_age_add;
client_ticket_age /= 1000;

struct OPENSSL_timeval now;
ssl_get_current_time(ssl, &now);
OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get());

// Compute the server ticket age in seconds.
assert(now.tv_sec >= session->time);
Expand Down

0 comments on commit a5d14be

Please sign in to comment.