| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2005 Nokia. All rights reserved. |
| 4 |
// |
| 5 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 6 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 7 |
// support (see RFC 4279) to OpenSSL. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
#include <openssl/ssl.h> |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include <openssl/err.h> |
| 20 |
#include <openssl/hmac.h> |
| 21 |
#include <openssl/lhash.h> |
| 22 |
#include <openssl/mem.h> |
| 23 |
#include <openssl/rand.h> |
| 24 |
|
| 25 |
#include "internal.h" |
| 26 |
#include "../crypto/internal.h" |
| 27 |
|
| 28 |
|
| 29 |
BSSL_NAMESPACE_BEGIN |
| 30 |
|
| 31 |
// The address of this is a magic value, a pointer to which is returned by |
| 32 |
// SSL_magic_pending_session_ptr(). It allows a session callback to indicate |
| 33 |
// that it needs to asynchronously fetch session information. |
| 34 |
static const char g_pending_session_magic = 0; |
| 35 |
|
| 36 |
static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 37 |
CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 38 |
|
| 39 |
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session); |
| 40 |
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session); |
| 41 |
|
| 42 |
UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method) { |
| 43 |
return MakeUnique<SSL_SESSION>(x509_method); |
| 44 |
} |
| 45 |
|
| 46 |
uint32_t ssl_hash_session_id(Span<const uint8_t> session_id) { |
| 47 |
// Take the first four bytes of |session_id|. Session IDs are generated by the |
| 48 |
// server randomly, so we can assume even using the first four bytes results |
| 49 |
// in a good distribution. |
| 50 |
uint8_t tmp_storage[sizeof(uint32_t)]; |
| 51 |
if (session_id.size() < sizeof(tmp_storage)) { |
| 52 |
OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage)); |
| 53 |
OPENSSL_memcpy(tmp_storage, session_id.data(), session_id.size()); |
| 54 |
session_id = tmp_storage; |
| 55 |
} |
| 56 |
|
| 57 |
uint32_t hash = |
| 58 |
((uint32_t)session_id[0]) | |
| 59 |
((uint32_t)session_id[1] << 8) | |
| 60 |
((uint32_t)session_id[2] << 16) | |
| 61 |
((uint32_t)session_id[3] << 24); |
| 62 |
|
| 63 |
return hash; |
| 64 |
} |
| 65 |
|
| 66 |
UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) { |
| 67 |
UniquePtr<SSL_SESSION> new_session = ssl_session_new(session->x509_method); |
| 68 |
if (!new_session) { |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
new_session->is_server = session->is_server; |
| 73 |
new_session->ssl_version = session->ssl_version; |
| 74 |
new_session->is_quic = session->is_quic; |
| 75 |
new_session->sid_ctx_length = session->sid_ctx_length; |
| 76 |
OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length); |
| 77 |
|
| 78 |
// Copy the key material. |
| 79 |
new_session->secret_length = session->secret_length; |
| 80 |
OPENSSL_memcpy(new_session->secret, session->secret, session->secret_length); |
| 81 |
new_session->cipher = session->cipher; |
| 82 |
|
| 83 |
// Copy authentication state. |
| 84 |
if (session->psk_identity != nullptr) { |
| 85 |
new_session->psk_identity.reset( |
| 86 |
OPENSSL_strdup(session->psk_identity.get())); |
| 87 |
if (new_session->psk_identity == nullptr) { |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
} |
| 91 |
if (session->certs != nullptr) { |
| 92 |
new_session->certs.reset(sk_CRYPTO_BUFFER_deep_copy( |
| 93 |
session->certs.get(), buffer_up_ref, CRYPTO_BUFFER_free)); |
| 94 |
if (new_session->certs == nullptr) { |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (!session->x509_method->session_dup(new_session.get(), session)) { |
| 100 |
return nullptr; |
| 101 |
} |
| 102 |
|
| 103 |
new_session->verify_result = session->verify_result; |
| 104 |
|
| 105 |
new_session->ocsp_response = UpRef(session->ocsp_response); |
| 106 |
new_session->signed_cert_timestamp_list = |
| 107 |
UpRef(session->signed_cert_timestamp_list); |
| 108 |
|
| 109 |
OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256, |
| 110 |
SHA256_DIGEST_LENGTH); |
| 111 |
new_session->peer_sha256_valid = session->peer_sha256_valid; |
| 112 |
|
| 113 |
new_session->peer_signature_algorithm = session->peer_signature_algorithm; |
| 114 |
|
| 115 |
new_session->timeout = session->timeout; |
| 116 |
new_session->auth_timeout = session->auth_timeout; |
| 117 |
new_session->time = session->time; |
| 118 |
|
| 119 |
// Copy non-authentication connection properties. |
| 120 |
if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) { |
| 121 |
new_session->session_id_length = session->session_id_length; |
| 122 |
OPENSSL_memcpy(new_session->session_id, session->session_id, |
| 123 |
session->session_id_length); |
| 124 |
|
| 125 |
new_session->group_id = session->group_id; |
| 126 |
|
| 127 |
OPENSSL_memcpy(new_session->original_handshake_hash, |
| 128 |
session->original_handshake_hash, |
| 129 |
session->original_handshake_hash_len); |
| 130 |
new_session->original_handshake_hash_len = |
| 131 |
session->original_handshake_hash_len; |
| 132 |
new_session->ticket_lifetime_hint = session->ticket_lifetime_hint; |
| 133 |
new_session->ticket_age_add = session->ticket_age_add; |
| 134 |
new_session->ticket_max_early_data = session->ticket_max_early_data; |
| 135 |
new_session->extended_master_secret = session->extended_master_secret; |
| 136 |
new_session->has_application_settings = session->has_application_settings; |
| 137 |
|
| 138 |
if (!new_session->early_alpn.CopyFrom(session->early_alpn) || |
| 139 |
!new_session->quic_early_data_context.CopyFrom( |
| 140 |
session->quic_early_data_context) || |
| 141 |
!new_session->local_application_settings.CopyFrom( |
| 142 |
session->local_application_settings) || |
| 143 |
!new_session->peer_application_settings.CopyFrom( |
| 144 |
session->peer_application_settings)) { |
| 145 |
return nullptr; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Copy the ticket. |
| 150 |
if (dup_flags & SSL_SESSION_INCLUDE_TICKET && |
| 151 |
!new_session->ticket.CopyFrom(session->ticket)) { |
| 152 |
return nullptr; |
| 153 |
} |
| 154 |
|
| 155 |
// The new_session does not get a copy of the ex_data. |
| 156 |
|
| 157 |
new_session->not_resumable = true; |
| 158 |
return new_session; |
| 159 |
} |
| 160 |
|
| 161 |
void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) { |
| 162 |
struct OPENSSL_timeval now; |
| 163 |
ssl_get_current_time(ssl, &now); |
| 164 |
|
| 165 |
// To avoid overflows and underflows, if we've gone back in time, update the |
| 166 |
// time, but mark the session expired. |
| 167 |
if (session->time > now.tv_sec) { |
| 168 |
session->time = now.tv_sec; |
| 169 |
session->timeout = 0; |
| 170 |
session->auth_timeout = 0; |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// Adjust the session time and timeouts. If the session has already expired, |
| 175 |
// clamp the timeouts at zero. |
| 176 |
uint64_t delta = now.tv_sec - session->time; |
| 177 |
session->time = now.tv_sec; |
| 178 |
if (session->timeout < delta) { |
| 179 |
session->timeout = 0; |
| 180 |
} else { |
| 181 |
session->timeout -= delta; |
| 182 |
} |
| 183 |
if (session->auth_timeout < delta) { |
| 184 |
session->auth_timeout = 0; |
| 185 |
} else { |
| 186 |
session->auth_timeout -= delta; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
| 191 |
uint32_t timeout) { |
| 192 |
// Rebase the timestamp relative to the current time so |timeout| is measured |
| 193 |
// correctly. |
| 194 |
ssl_session_rebase_time(ssl, session); |
| 195 |
|
| 196 |
if (session->timeout > timeout) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
session->timeout = timeout; |
| 201 |
if (session->timeout > session->auth_timeout) { |
| 202 |
session->timeout = session->auth_timeout; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
uint16_t ssl_session_protocol_version(const SSL_SESSION *session) { |
| 207 |
uint16_t ret; |
| 208 |
if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) { |
| 209 |
// An |SSL_SESSION| will never have an invalid version. This is enforced by |
| 210 |
// the parser. |
| 211 |
assert(0); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) { |
| 219 |
return ssl_get_handshake_digest(ssl_session_protocol_version(session), |
| 220 |
session->cipher); |
| 221 |
} |
| 222 |
|
| 223 |
bool ssl_get_new_session(SSL_HANDSHAKE *hs) { |
| 224 |
SSL *const ssl = hs->ssl; |
| 225 |
if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) { |
| 226 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED); |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method); |
| 231 |
if (session == NULL) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
session->is_server = ssl->server; |
| 236 |
session->ssl_version = ssl->version; |
| 237 |
session->is_quic = ssl->quic_method != nullptr; |
| 238 |
|
| 239 |
// Fill in the time from the |SSL_CTX|'s clock. |
| 240 |
struct OPENSSL_timeval now; |
| 241 |
ssl_get_current_time(ssl, &now); |
| 242 |
session->time = now.tv_sec; |
| 243 |
|
| 244 |
uint16_t version = ssl_protocol_version(ssl); |
| 245 |
if (version >= TLS1_3_VERSION) { |
| 246 |
// TLS 1.3 uses tickets as authenticators, so we are willing to use them for |
| 247 |
// longer. |
| 248 |
session->timeout = ssl->session_ctx->session_psk_dhe_timeout; |
| 249 |
session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT; |
| 250 |
} else { |
| 251 |
// TLS 1.2 resumption does not incorporate new key material, so we use a |
| 252 |
// much shorter timeout. |
| 253 |
session->timeout = ssl->session_ctx->session_timeout; |
| 254 |
session->auth_timeout = ssl->session_ctx->session_timeout; |
| 255 |
} |
| 256 |
|
| 257 |
if (hs->config->cert->sid_ctx_length > sizeof(session->sid_ctx)) { |
| 258 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 259 |
return false; |
| 260 |
} |
| 261 |
OPENSSL_memcpy(session->sid_ctx, hs->config->cert->sid_ctx, |
| 262 |
hs->config->cert->sid_ctx_length); |
| 263 |
session->sid_ctx_length = hs->config->cert->sid_ctx_length; |
| 264 |
|
| 265 |
// The session is marked not resumable until it is completely filled in. |
| 266 |
session->not_resumable = true; |
| 267 |
session->verify_result = X509_V_ERR_INVALID_CALL; |
| 268 |
|
| 269 |
hs->new_session = std::move(session); |
| 270 |
ssl_set_session(ssl, NULL); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) { |
| 275 |
OPENSSL_timeval now; |
| 276 |
ssl_ctx_get_current_time(ctx, &now); |
| 277 |
{ |
| 278 |
// Avoid acquiring a write lock in the common case (i.e. a non-default key |
| 279 |
// is used or the default keys have not expired yet). |
| 280 |
MutexReadLock lock(&ctx->lock); |
| 281 |
if (ctx->ticket_key_current && |
| 282 |
(ctx->ticket_key_current->next_rotation_tv_sec == 0 || |
| 283 |
ctx->ticket_key_current->next_rotation_tv_sec > now.tv_sec) && |
| 284 |
(!ctx->ticket_key_prev || |
| 285 |
ctx->ticket_key_prev->next_rotation_tv_sec > now.tv_sec)) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
MutexWriteLock lock(&ctx->lock); |
| 291 |
if (!ctx->ticket_key_current || |
| 292 |
(ctx->ticket_key_current->next_rotation_tv_sec != 0 && |
| 293 |
ctx->ticket_key_current->next_rotation_tv_sec <= now.tv_sec)) { |
| 294 |
// The current key has not been initialized or it is expired. |
| 295 |
auto new_key = bssl::MakeUnique<TicketKey>(); |
| 296 |
if (!new_key) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
RAND_bytes(new_key->name, 16); |
| 300 |
RAND_bytes(new_key->hmac_key, 16); |
| 301 |
RAND_bytes(new_key->aes_key, 16); |
| 302 |
new_key->next_rotation_tv_sec = |
| 303 |
now.tv_sec + SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
| 304 |
if (ctx->ticket_key_current) { |
| 305 |
// The current key expired. Rotate it to prev and bump up its rotation |
| 306 |
// timestamp. Note that even with the new rotation time it may still be |
| 307 |
// expired and get dropped below. |
| 308 |
ctx->ticket_key_current->next_rotation_tv_sec += |
| 309 |
SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
| 310 |
ctx->ticket_key_prev = std::move(ctx->ticket_key_current); |
| 311 |
} |
| 312 |
ctx->ticket_key_current = std::move(new_key); |
| 313 |
} |
| 314 |
|
| 315 |
// Drop an expired prev key. |
| 316 |
if (ctx->ticket_key_prev && |
| 317 |
ctx->ticket_key_prev->next_rotation_tv_sec <= now.tv_sec) { |
| 318 |
ctx->ticket_key_prev.reset(); |
| 319 |
} |
| 320 |
|
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
static int ssl_encrypt_ticket_with_cipher_ctx(SSL_HANDSHAKE *hs, CBB *out, |
| 325 |
const uint8_t *session_buf, |
| 326 |
size_t session_len) { |
| 327 |
ScopedEVP_CIPHER_CTX ctx; |
| 328 |
ScopedHMAC_CTX hctx; |
| 329 |
|
| 330 |
// If the session is too long, emit a dummy value rather than abort the |
| 331 |
// connection. |
| 332 |
static const size_t kMaxTicketOverhead = |
| 333 |
16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 334 |
if (session_len > 0xffff - kMaxTicketOverhead) { |
| 335 |
static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 336 |
return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder, |
| 337 |
strlen(kTicketPlaceholder)); |
| 338 |
} |
| 339 |
|
| 340 |
// Initialize HMAC and cipher contexts. If callback present it does all the |
| 341 |
// work otherwise use generated values from parent ctx. |
| 342 |
SSL_CTX *tctx = hs->ssl->session_ctx.get(); |
| 343 |
uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 344 |
uint8_t key_name[16]; |
| 345 |
if (tctx->ticket_key_cb != NULL) { |
| 346 |
if (tctx->ticket_key_cb(hs->ssl, key_name, iv, ctx.get(), hctx.get(), |
| 347 |
1 /* encrypt */) < 0) { |
| 348 |
return 0; |
| 349 |
} |
| 350 |
} else { |
| 351 |
// Rotate ticket key if necessary. |
| 352 |
if (!ssl_ctx_rotate_ticket_encryption_key(tctx)) { |
| 353 |
return 0; |
| 354 |
} |
| 355 |
MutexReadLock lock(&tctx->lock); |
| 356 |
if (!RAND_bytes(iv, 16) || |
| 357 |
!EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL, |
| 358 |
tctx->ticket_key_current->aes_key, iv) || |
| 359 |
!HMAC_Init_ex(hctx.get(), tctx->ticket_key_current->hmac_key, 16, |
| 360 |
tlsext_tick_md(), NULL)) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
OPENSSL_memcpy(key_name, tctx->ticket_key_current->name, 16); |
| 364 |
} |
| 365 |
|
| 366 |
uint8_t *ptr; |
| 367 |
if (!CBB_add_bytes(out, key_name, 16) || |
| 368 |
!CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) || |
| 369 |
!CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) { |
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
size_t total = 0; |
| 374 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 375 |
OPENSSL_memcpy(ptr, session_buf, session_len); |
| 376 |
total = session_len; |
| 377 |
#else |
| 378 |
int len; |
| 379 |
if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) { |
| 380 |
return 0; |
| 381 |
} |
| 382 |
total += len; |
| 383 |
if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) { |
| 384 |
return 0; |
| 385 |
} |
| 386 |
total += len; |
| 387 |
#endif |
| 388 |
if (!CBB_did_write(out, total)) { |
| 389 |
return 0; |
| 390 |
} |
| 391 |
|
| 392 |
unsigned hlen; |
| 393 |
if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) || |
| 394 |
!CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) || |
| 395 |
!HMAC_Final(hctx.get(), ptr, &hlen) || |
| 396 |
!CBB_did_write(out, hlen)) { |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
return 1; |
| 401 |
} |
| 402 |
|
| 403 |
static int ssl_encrypt_ticket_with_method(SSL_HANDSHAKE *hs, CBB *out, |
| 404 |
const uint8_t *session_buf, |
| 405 |
size_t session_len) { |
| 406 |
SSL *const ssl = hs->ssl; |
| 407 |
const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method; |
| 408 |
const size_t max_overhead = method->max_overhead(ssl); |
| 409 |
const size_t max_out = session_len + max_overhead; |
| 410 |
if (max_out < max_overhead) { |
| 411 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
uint8_t *ptr; |
| 416 |
if (!CBB_reserve(out, &ptr, max_out)) { |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
size_t out_len; |
| 421 |
if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, |
| 422 |
session_len)) { |
| 423 |
OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED); |
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
if (!CBB_did_write(out, out_len)) { |
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
return 1; |
| 432 |
} |
| 433 |
|
| 434 |
bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
| 435 |
const SSL_SESSION *session) { |
| 436 |
// Serialize the SSL_SESSION to be encoded into the ticket. |
| 437 |
uint8_t *session_buf = nullptr; |
| 438 |
size_t session_len; |
| 439 |
if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) { |
| 440 |
return false; |
| 441 |
} |
| 442 |
bssl::UniquePtr<uint8_t> free_session_buf(session_buf); |
| 443 |
|
| 444 |
if (hs->ssl->session_ctx->ticket_aead_method) { |
| 445 |
return ssl_encrypt_ticket_with_method(hs, out, session_buf, session_len); |
| 446 |
} else { |
| 447 |
return ssl_encrypt_ticket_with_cipher_ctx(hs, out, session_buf, |
| 448 |
session_len); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
| 453 |
const SSL_SESSION *session) { |
| 454 |
if (session == NULL) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
return session->sid_ctx_length == hs->config->cert->sid_ctx_length && |
| 459 |
OPENSSL_memcmp(session->sid_ctx, hs->config->cert->sid_ctx, |
| 460 |
hs->config->cert->sid_ctx_length) == 0; |
| 461 |
} |
| 462 |
|
| 463 |
bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) { |
| 464 |
if (session == NULL) { |
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
struct OPENSSL_timeval now; |
| 469 |
ssl_get_current_time(ssl, &now); |
| 470 |
|
| 471 |
// Reject tickets from the future to avoid underflow. |
| 472 |
if (now.tv_sec < session->time) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
return session->timeout > now.tv_sec - session->time; |
| 477 |
} |
| 478 |
|
| 479 |
bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
| 480 |
const SSL_SESSION *session) { |
| 481 |
const SSL *const ssl = hs->ssl; |
| 482 |
return ssl_session_is_context_valid(hs, session) && |
| 483 |
// The session must have been created by the same type of end point as |
| 484 |
// we're now using it with. |
| 485 |
ssl->server == session->is_server && |
| 486 |
// The session must not be expired. |
| 487 |
ssl_session_is_time_valid(ssl, session) && |
| 488 |
// Only resume if the session's version matches the negotiated |
| 489 |
// version. |
| 490 |
ssl->version == session->ssl_version && |
| 491 |
// Only resume if the session's cipher matches the negotiated one. This |
| 492 |
// is stricter than necessary for TLS 1.3, which allows cross-cipher |
| 493 |
// resumption if the PRF hashes match. We require an exact match for |
| 494 |
// simplicity. If loosening this, the 0-RTT accept logic must be |
| 495 |
// updated to check the cipher. |
| 496 |
hs->new_cipher == session->cipher && |
| 497 |
// If the session contains a client certificate (either the full |
| 498 |
// certificate or just the hash) then require that the form of the |
| 499 |
// certificate matches the current configuration. |
| 500 |
((sk_CRYPTO_BUFFER_num(session->certs.get()) == 0 && |
| 501 |
!session->peer_sha256_valid) || |
| 502 |
session->peer_sha256_valid == |
| 503 |
hs->config->retain_only_sha256_of_client_certs) && |
| 504 |
// Only resume if the underlying transport protocol hasn't changed. |
| 505 |
// This is to prevent cross-protocol resumption between QUIC and TCP. |
| 506 |
(hs->ssl->quic_method != nullptr) == session->is_quic; |
| 507 |
} |
| 508 |
|
| 509 |
// ssl_lookup_session looks up |session_id| in the session cache and sets |
| 510 |
// |*out_session| to an |SSL_SESSION| object if found. |
| 511 |
static enum ssl_hs_wait_t ssl_lookup_session( |
| 512 |
SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
| 513 |
Span<const uint8_t> session_id) { |
| 514 |
SSL *const ssl = hs->ssl; |
| 515 |
out_session->reset(); |
| 516 |
|
| 517 |
if (session_id.empty() || session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 518 |
return ssl_hs_ok; |
| 519 |
} |
| 520 |
|
| 521 |
UniquePtr<SSL_SESSION> session; |
| 522 |
// Try the internal cache, if it exists. |
| 523 |
if (!(ssl->session_ctx->session_cache_mode & |
| 524 |
SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
| 525 |
uint32_t hash = ssl_hash_session_id(session_id); |
| 526 |
auto cmp = [](const void *key, const SSL_SESSION *sess) -> int { |
| 527 |
Span<const uint8_t> key_id = |
| 528 |
*reinterpret_cast<const Span<const uint8_t> *>(key); |
| 529 |
Span<const uint8_t> sess_id = |
| 530 |
MakeConstSpan(sess->session_id, sess->session_id_length); |
| 531 |
return key_id == sess_id ? 0 : 1; |
| 532 |
}; |
| 533 |
CRYPTO_MUTEX_lock_read(&ssl->session_ctx->lock); |
| 534 |
// |lh_SSL_SESSION_retrieve_key| returns a non-owning pointer. |
| 535 |
session = UpRef(lh_SSL_SESSION_retrieve_key(ssl->session_ctx->sessions, |
| 536 |
&session_id, hash, cmp)); |
| 537 |
CRYPTO_MUTEX_unlock_read(&ssl->session_ctx->lock); |
| 538 |
// TODO(davidben): This should probably move it to the front of the list. |
| 539 |
if (session == nullptr) { |
| 540 |
ssl_update_counter(ssl->session_ctx.get(), |
| 541 |
ssl->session_ctx->stats.sess_miss, true); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
// Fall back to the external cache, if it exists. |
| 546 |
if (!session && ssl->session_ctx->get_session_cb != nullptr) { |
| 547 |
int copy = 1; |
| 548 |
session.reset(ssl->session_ctx->get_session_cb(ssl, session_id.data(), |
| 549 |
session_id.size(), ©)); |
| 550 |
if (!session) { |
| 551 |
return ssl_hs_ok; |
| 552 |
} |
| 553 |
|
| 554 |
if (session.get() == SSL_magic_pending_session_ptr()) { |
| 555 |
session.release(); // This pointer is not actually owned. |
| 556 |
return ssl_hs_pending_session; |
| 557 |
} |
| 558 |
ssl_update_counter(ssl->session_ctx.get(), |
| 559 |
ssl->session_ctx->stats.sess_cb_hit, true); |
| 560 |
|
| 561 |
// Increment reference count now if the session callback asks us to do so |
| 562 |
// (note that if the session structures returned by the callback are shared |
| 563 |
// between threads, it must handle the reference count itself [i.e. copy == |
| 564 |
// 0], or things won't be thread-safe). |
| 565 |
if (copy) { |
| 566 |
SSL_SESSION_up_ref(session.get()); |
| 567 |
} |
| 568 |
|
| 569 |
// Add the externally cached session to the internal cache if necessary. |
| 570 |
if (!(ssl->session_ctx->session_cache_mode & |
| 571 |
SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
| 572 |
SSL_CTX_add_session(ssl->session_ctx.get(), session.get()); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
if (!ssl_session_is_time_valid(ssl, session.get())) { |
| 577 |
ssl_update_counter(ssl->session_ctx.get(), |
| 578 |
ssl->session_ctx->stats.sess_timeout, true); |
| 579 |
if (session) { |
| 580 |
// The session was from the cache, so remove it. |
| 581 |
SSL_CTX_remove_session(ssl->session_ctx.get(), session.get()); |
| 582 |
session.reset(); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if (session) { |
| 587 |
ssl_update_counter(ssl->session_ctx.get(), ssl->session_ctx->stats.sess_hit, |
| 588 |
true); |
| 589 |
} |
| 590 |
*out_session = std::move(session); |
| 591 |
return ssl_hs_ok; |
| 592 |
} |
| 593 |
|
| 594 |
enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
| 595 |
UniquePtr<SSL_SESSION> *out_session, |
| 596 |
bool *out_tickets_supported, |
| 597 |
bool *out_renew_ticket, |
| 598 |
const SSL_CLIENT_HELLO *client_hello) { |
| 599 |
// This is used only by servers. |
| 600 |
assert(hs->ssl->server); |
| 601 |
UniquePtr<SSL_SESSION> session; |
| 602 |
bool renew_ticket = false; |
| 603 |
|
| 604 |
// If tickets are disabled, always behave as if no tickets are present. |
| 605 |
CBS ticket; |
| 606 |
const bool tickets_supported = |
| 607 |
!(SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) && |
| 608 |
ssl_client_hello_get_extension(client_hello, &ticket, |
| 609 |
TLSEXT_TYPE_session_ticket); |
| 610 |
if (tickets_supported && CBS_len(&ticket) != 0) { |
| 611 |
switch (ssl_process_ticket(hs, &session, &renew_ticket, ticket, |
| 612 |
MakeConstSpan(client_hello->session_id, |
| 613 |
client_hello->session_id_len))) { |
| 614 |
case ssl_ticket_aead_success: |
| 615 |
break; |
| 616 |
case ssl_ticket_aead_ignore_ticket: |
| 617 |
assert(!session); |
| 618 |
break; |
| 619 |
case ssl_ticket_aead_error: |
| 620 |
return ssl_hs_error; |
| 621 |
case ssl_ticket_aead_retry: |
| 622 |
return ssl_hs_pending_ticket; |
| 623 |
} |
| 624 |
} else { |
| 625 |
// The client didn't send a ticket, so the session ID is a real ID. |
| 626 |
enum ssl_hs_wait_t lookup_ret = ssl_lookup_session( |
| 627 |
hs, &session, |
| 628 |
MakeConstSpan(client_hello->session_id, client_hello->session_id_len)); |
| 629 |
if (lookup_ret != ssl_hs_ok) { |
| 630 |
return lookup_ret; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
*out_session = std::move(session); |
| 635 |
*out_tickets_supported = tickets_supported; |
| 636 |
*out_renew_ticket = renew_ticket; |
| 637 |
return ssl_hs_ok; |
| 638 |
} |
| 639 |
|
| 640 |
static bool remove_session(SSL_CTX *ctx, SSL_SESSION *session, bool lock) { |
| 641 |
if (session == nullptr || session->session_id_length == 0) { |
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
if (lock) { |
| 646 |
CRYPTO_MUTEX_lock_write(&ctx->lock); |
| 647 |
} |
| 648 |
|
| 649 |
SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions, session); |
| 650 |
bool found = found_session == session; |
| 651 |
if (found) { |
| 652 |
found_session = lh_SSL_SESSION_delete(ctx->sessions, session); |
| 653 |
SSL_SESSION_list_remove(ctx, session); |
| 654 |
} |
| 655 |
|
| 656 |
if (lock) { |
| 657 |
CRYPTO_MUTEX_unlock_write(&ctx->lock); |
| 658 |
} |
| 659 |
|
| 660 |
if (found) { |
| 661 |
// TODO(https://crbug.com/boringssl/251): Callbacks should not be called |
| 662 |
// under a lock. |
| 663 |
if (ctx->remove_session_cb != nullptr) { |
| 664 |
ctx->remove_session_cb(ctx, found_session); |
| 665 |
} |
| 666 |
SSL_SESSION_free(found_session); |
| 667 |
} |
| 668 |
|
| 669 |
return found; |
| 670 |
} |
| 671 |
|
| 672 |
void ssl_set_session(SSL *ssl, SSL_SESSION *session) { |
| 673 |
if (ssl->session.get() == session) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
ssl->session = UpRef(session); |
| 678 |
|
| 679 |
if (ssl->session) { |
| 680 |
ssl->verify_result = ssl->session->verify_result; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
// locked by SSL_CTX in the calling function |
| 685 |
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) { |
| 686 |
if (session->next == NULL || session->prev == NULL) { |
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) { |
| 691 |
// last element in list |
| 692 |
if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
| 693 |
// only one element in list |
| 694 |
ctx->session_cache_head = NULL; |
| 695 |
ctx->session_cache_tail = NULL; |
| 696 |
} else { |
| 697 |
ctx->session_cache_tail = session->prev; |
| 698 |
session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 699 |
} |
| 700 |
} else { |
| 701 |
if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
| 702 |
// first element in list |
| 703 |
ctx->session_cache_head = session->next; |
| 704 |
session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 705 |
} else { // middle of list |
| 706 |
session->next->prev = session->prev; |
| 707 |
session->prev->next = session->next; |
| 708 |
} |
| 709 |
} |
| 710 |
session->prev = session->next = NULL; |
| 711 |
} |
| 712 |
|
| 713 |
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) { |
| 714 |
if (session->next != NULL && session->prev != NULL) { |
| 715 |
SSL_SESSION_list_remove(ctx, session); |
| 716 |
} |
| 717 |
|
| 718 |
if (ctx->session_cache_head == NULL) { |
| 719 |
ctx->session_cache_head = session; |
| 720 |
ctx->session_cache_tail = session; |
| 721 |
session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 722 |
session->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 723 |
} else { |
| 724 |
session->next = ctx->session_cache_head; |
| 725 |
session->next->prev = session; |
| 726 |
session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 727 |
ctx->session_cache_head = session; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
static bool add_session_locked(SSL_CTX *ctx, UniquePtr<SSL_SESSION> session) { |
| 732 |
SSL_SESSION *new_session = session.get(); |
| 733 |
SSL_SESSION *old_session; |
| 734 |
if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, new_session)) { |
| 735 |
return false; |
| 736 |
} |
| 737 |
// |ctx->sessions| took ownership of |new_session| and gave us back a |
| 738 |
// reference to |old_session|. (|old_session| may be the same as |
| 739 |
// |new_session|, in which case we traded identical references with |
| 740 |
// |ctx->sessions|.) |
| 741 |
session.release(); |
| 742 |
session.reset(old_session); |
| 743 |
|
| 744 |
if (old_session != nullptr) { |
| 745 |
if (old_session == new_session) { |
| 746 |
// |session| was already in the cache. There are no linked list pointers |
| 747 |
// to update. |
| 748 |
return false; |
| 749 |
} |
| 750 |
|
| 751 |
// There was a session ID collision. |old_session| was replaced with |
| 752 |
// |session| in the hash table, so |old_session| must be removed from the |
| 753 |
// linked list to match. |
| 754 |
SSL_SESSION_list_remove(ctx, old_session); |
| 755 |
} |
| 756 |
|
| 757 |
// This does not increment the reference count. Although |session| is inserted |
| 758 |
// into two structures (a doubly-linked list and the hash table), |ctx| only |
| 759 |
// takes one reference. |
| 760 |
SSL_SESSION_list_add(ctx, new_session); |
| 761 |
|
| 762 |
// Enforce any cache size limits. |
| 763 |
if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
| 764 |
while (lh_SSL_SESSION_num_items(ctx->sessions) > |
| 765 |
SSL_CTX_sess_get_cache_size(ctx)) { |
| 766 |
if (!remove_session(ctx, ctx->session_cache_tail, |
| 767 |
/*lock=*/false)) { |
| 768 |
break; |
| 769 |
} |
| 770 |
ssl_update_counter(ctx, ctx->stats.sess_cache_full, /*lock=*/ false); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
return true; |
| 775 |
} |
| 776 |
|
| 777 |
void ssl_update_cache(SSL *ssl) { |
| 778 |
SSL_CTX *ctx = ssl->session_ctx.get(); |
| 779 |
SSL_SESSION *session = ssl->s3->established_session.get(); |
| 780 |
int mode = SSL_is_server(ssl) ? SSL_SESS_CACHE_SERVER : SSL_SESS_CACHE_CLIENT; |
| 781 |
if (!SSL_SESSION_is_resumable(session) || |
| 782 |
(ctx->session_cache_mode & mode) != mode) { |
| 783 |
return; |
| 784 |
} |
| 785 |
|
| 786 |
// Clients never use the internal session cache. |
| 787 |
if (ssl->server && |
| 788 |
!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
| 789 |
UniquePtr<SSL_SESSION> ref = UpRef(session); |
| 790 |
bool remove_expired_sessions = false; |
| 791 |
{ |
| 792 |
MutexWriteLock lock(&ctx->lock); |
| 793 |
add_session_locked(ctx, std::move(ref)); |
| 794 |
|
| 795 |
if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) { |
| 796 |
// Automatically flush the internal session cache every 255 connections. |
| 797 |
ctx->handshakes_since_cache_flush++; |
| 798 |
if (ctx->handshakes_since_cache_flush >= 255) { |
| 799 |
remove_expired_sessions = true; |
| 800 |
ctx->handshakes_since_cache_flush = 0; |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
if (remove_expired_sessions) { |
| 806 |
// |SSL_CTX_flush_sessions| takes the lock we just released. We could |
| 807 |
// merge the critical sections, but we'd then call user code under a |
| 808 |
// lock, or compute |now| earlier, even when not flushing. |
| 809 |
OPENSSL_timeval now; |
| 810 |
ssl_get_current_time(ssl, &now); |
| 811 |
SSL_CTX_flush_sessions(ctx, now.tv_sec); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
if (ctx->new_session_cb != nullptr) { |
| 816 |
UniquePtr<SSL_SESSION> ref = UpRef(session); |
| 817 |
if (ctx->new_session_cb(ssl, ref.get())) { |
| 818 |
// |new_session_cb|'s return value signals whether it took ownership. |
| 819 |
ref.release(); |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
BSSL_NAMESPACE_END |
| 825 |
|
| 826 |
using namespace bssl; |
| 827 |
|
| 828 |
ssl_session_st::ssl_session_st(const SSL_X509_METHOD *method) |
| 829 |
: RefCounted(CheckSubClass()), |
| 830 |
x509_method(method), |
| 831 |
extended_master_secret(false), |
| 832 |
peer_sha256_valid(false), |
| 833 |
not_resumable(false), |
| 834 |
ticket_age_add_valid(false), |
| 835 |
is_server(false), |
| 836 |
is_quic(false), |
| 837 |
has_application_settings(false) { |
| 838 |
CRYPTO_new_ex_data(&ex_data); |
| 839 |
time = ::time(nullptr); |
| 840 |
} |
| 841 |
|
| 842 |
ssl_session_st::~ssl_session_st() { |
| 843 |
CRYPTO_free_ex_data(&g_ex_data_class, this, &ex_data); |
| 844 |
x509_method->session_clear(this); |
| 845 |
} |
| 846 |
|
| 847 |
SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) { |
| 848 |
return ssl_session_new(ctx->x509_method).release(); |
| 849 |
} |
| 850 |
|
| 851 |
int SSL_SESSION_up_ref(SSL_SESSION *session) { |
| 852 |
session->UpRefInternal(); |
| 853 |
return 1; |
| 854 |
} |
| 855 |
|
| 856 |
void SSL_SESSION_free(SSL_SESSION *session) { |
| 857 |
if (session != nullptr) { |
| 858 |
session->DecRefInternal(); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session, |
| 863 |
unsigned *out_len) { |
| 864 |
if (out_len != NULL) { |
| 865 |
*out_len = session->session_id_length; |
| 866 |
} |
| 867 |
return session->session_id; |
| 868 |
} |
| 869 |
|
| 870 |
int SSL_SESSION_set1_id(SSL_SESSION *session, const uint8_t *sid, |
| 871 |
size_t sid_len) { |
| 872 |
if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 873 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); |
| 874 |
return 0; |
| 875 |
} |
| 876 |
|
| 877 |
// Use memmove in case someone passes in the output of |SSL_SESSION_get_id|. |
| 878 |
OPENSSL_memmove(session->session_id, sid, sid_len); |
| 879 |
session->session_id_length = sid_len; |
| 880 |
return 1; |
| 881 |
} |
| 882 |
|
| 883 |
uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) { |
| 884 |
return session->timeout; |
| 885 |
} |
| 886 |
|
| 887 |
uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) { |
| 888 |
if (session == NULL) { |
| 889 |
// NULL should crash, but silently accept it here for compatibility. |
| 890 |
return 0; |
| 891 |
} |
| 892 |
return session->time; |
| 893 |
} |
| 894 |
|
| 895 |
X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) { |
| 896 |
return session->x509_peer; |
| 897 |
} |
| 898 |
|
| 899 |
const STACK_OF(CRYPTO_BUFFER) * |
| 900 |
SSL_SESSION_get0_peer_certificates(const SSL_SESSION *session) { |
| 901 |
return session->certs.get(); |
| 902 |
} |
| 903 |
|
| 904 |
void SSL_SESSION_get0_signed_cert_timestamp_list(const SSL_SESSION *session, |
| 905 |
const uint8_t **out, |
| 906 |
size_t *out_len) { |
| 907 |
if (session->signed_cert_timestamp_list) { |
| 908 |
*out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get()); |
| 909 |
*out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get()); |
| 910 |
} else { |
| 911 |
*out = nullptr; |
| 912 |
*out_len = 0; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
void SSL_SESSION_get0_ocsp_response(const SSL_SESSION *session, |
| 917 |
const uint8_t **out, size_t *out_len) { |
| 918 |
if (session->ocsp_response) { |
| 919 |
*out = CRYPTO_BUFFER_data(session->ocsp_response.get()); |
| 920 |
*out_len = CRYPTO_BUFFER_len(session->ocsp_response.get()); |
| 921 |
} else { |
| 922 |
*out = nullptr; |
| 923 |
*out_len = 0; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out, |
| 928 |
size_t max_out) { |
| 929 |
// TODO(davidben): Fix secret_length's type and remove these casts. |
| 930 |
if (max_out == 0) { |
| 931 |
return (size_t)session->secret_length; |
| 932 |
} |
| 933 |
if (max_out > (size_t)session->secret_length) { |
| 934 |
max_out = (size_t)session->secret_length; |
| 935 |
} |
| 936 |
OPENSSL_memcpy(out, session->secret, max_out); |
| 937 |
return max_out; |
| 938 |
} |
| 939 |
|
| 940 |
uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) { |
| 941 |
if (session == NULL) { |
| 942 |
return 0; |
| 943 |
} |
| 944 |
|
| 945 |
session->time = time; |
| 946 |
return time; |
| 947 |
} |
| 948 |
|
| 949 |
uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) { |
| 950 |
if (session == NULL) { |
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
session->timeout = timeout; |
| 955 |
session->auth_timeout = timeout; |
| 956 |
return 1; |
| 957 |
} |
| 958 |
|
| 959 |
const uint8_t *SSL_SESSION_get0_id_context(const SSL_SESSION *session, |
| 960 |
unsigned *out_len) { |
| 961 |
if (out_len != NULL) { |
| 962 |
*out_len = session->sid_ctx_length; |
| 963 |
} |
| 964 |
return session->sid_ctx; |
| 965 |
} |
| 966 |
|
| 967 |
int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx, |
| 968 |
size_t sid_ctx_len) { |
| 969 |
if (sid_ctx_len > sizeof(session->sid_ctx)) { |
| 970 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 971 |
return 0; |
| 972 |
} |
| 973 |
|
| 974 |
static_assert(sizeof(session->sid_ctx) < 256, "sid_ctx_len does not fit"); |
| 975 |
session->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 976 |
OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len); |
| 977 |
|
| 978 |
return 1; |
| 979 |
} |
| 980 |
|
| 981 |
int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) { |
| 982 |
return ssl_session_protocol_version(session) >= TLS1_3_VERSION; |
| 983 |
} |
| 984 |
|
| 985 |
int SSL_SESSION_is_resumable(const SSL_SESSION *session) { |
| 986 |
return !session->not_resumable && |
| 987 |
(session->session_id_length != 0 || !session->ticket.empty()); |
| 988 |
} |
| 989 |
|
| 990 |
int SSL_SESSION_has_ticket(const SSL_SESSION *session) { |
| 991 |
return !session->ticket.empty(); |
| 992 |
} |
| 993 |
|
| 994 |
void SSL_SESSION_get0_ticket(const SSL_SESSION *session, |
| 995 |
const uint8_t **out_ticket, size_t *out_len) { |
| 996 |
if (out_ticket != nullptr) { |
| 997 |
*out_ticket = session->ticket.data(); |
| 998 |
} |
| 999 |
*out_len = session->ticket.size(); |
| 1000 |
} |
| 1001 |
|
| 1002 |
int SSL_SESSION_set_ticket(SSL_SESSION *session, const uint8_t *ticket, |
| 1003 |
size_t ticket_len) { |
| 1004 |
return session->ticket.CopyFrom(MakeConstSpan(ticket, ticket_len)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
uint32_t SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *session) { |
| 1008 |
return session->ticket_lifetime_hint; |
| 1009 |
} |
| 1010 |
|
| 1011 |
const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *session) { |
| 1012 |
return session->cipher; |
| 1013 |
} |
| 1014 |
|
| 1015 |
int SSL_SESSION_has_peer_sha256(const SSL_SESSION *session) { |
| 1016 |
return session->peer_sha256_valid; |
| 1017 |
} |
| 1018 |
|
| 1019 |
void SSL_SESSION_get0_peer_sha256(const SSL_SESSION *session, |
| 1020 |
const uint8_t **out_ptr, size_t *out_len) { |
| 1021 |
if (session->peer_sha256_valid) { |
| 1022 |
*out_ptr = session->peer_sha256; |
| 1023 |
*out_len = sizeof(session->peer_sha256); |
| 1024 |
} else { |
| 1025 |
*out_ptr = nullptr; |
| 1026 |
*out_len = 0; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
int SSL_SESSION_early_data_capable(const SSL_SESSION *session) { |
| 1031 |
return ssl_session_protocol_version(session) >= TLS1_3_VERSION && |
| 1032 |
session->ticket_max_early_data != 0; |
| 1033 |
} |
| 1034 |
|
| 1035 |
SSL_SESSION *SSL_SESSION_copy_without_early_data(SSL_SESSION *session) { |
| 1036 |
if (!SSL_SESSION_early_data_capable(session)) { |
| 1037 |
return UpRef(session).release(); |
| 1038 |
} |
| 1039 |
|
| 1040 |
bssl::UniquePtr<SSL_SESSION> copy = |
| 1041 |
SSL_SESSION_dup(session, SSL_SESSION_DUP_ALL); |
| 1042 |
if (!copy) { |
| 1043 |
return nullptr; |
| 1044 |
} |
| 1045 |
|
| 1046 |
copy->ticket_max_early_data = 0; |
| 1047 |
// Copied sessions are non-resumable until they're completely filled in. |
| 1048 |
copy->not_resumable = session->not_resumable; |
| 1049 |
assert(!SSL_SESSION_early_data_capable(copy.get())); |
| 1050 |
return copy.release(); |
| 1051 |
} |
| 1052 |
|
| 1053 |
SSL_SESSION *SSL_magic_pending_session_ptr(void) { |
| 1054 |
return (SSL_SESSION *)&g_pending_session_magic; |
| 1055 |
} |
| 1056 |
|
| 1057 |
SSL_SESSION *SSL_get_session(const SSL *ssl) { |
| 1058 |
// Once the initially handshake completes, we return the most recently |
| 1059 |
// established session. In particular, if there is a pending renegotiation, we |
| 1060 |
// do not return information about it until it completes. |
| 1061 |
// |
| 1062 |
// Code in the handshake must either use |hs->new_session| (if updating a |
| 1063 |
// partial session) or |ssl_handshake_session| (if trying to query properties |
| 1064 |
// consistently across TLS 1.2 resumption and other handshakes). |
| 1065 |
if (ssl->s3->established_session != nullptr) { |
| 1066 |
return ssl->s3->established_session.get(); |
| 1067 |
} |
| 1068 |
|
| 1069 |
// Otherwise, we must be in the initial handshake. |
| 1070 |
SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 1071 |
assert(hs != nullptr); |
| 1072 |
assert(!ssl->s3->initial_handshake_complete); |
| 1073 |
|
| 1074 |
// Return the 0-RTT session, if in the 0-RTT state. While the handshake has |
| 1075 |
// not actually completed, the public accessors all report properties as if |
| 1076 |
// it has. |
| 1077 |
if (hs->early_session) { |
| 1078 |
return hs->early_session.get(); |
| 1079 |
} |
| 1080 |
|
| 1081 |
// Otherwise, return the partial session. |
| 1082 |
return (SSL_SESSION *)ssl_handshake_session(hs); |
| 1083 |
} |
| 1084 |
|
| 1085 |
SSL_SESSION *SSL_get1_session(SSL *ssl) { |
| 1086 |
SSL_SESSION *ret = SSL_get_session(ssl); |
| 1087 |
if (ret != NULL) { |
| 1088 |
SSL_SESSION_up_ref(ret); |
| 1089 |
} |
| 1090 |
return ret; |
| 1091 |
} |
| 1092 |
|
| 1093 |
int SSL_SESSION_get_ex_new_index(long argl, void *argp, |
| 1094 |
CRYPTO_EX_unused *unused, |
| 1095 |
CRYPTO_EX_dup *dup_unused, |
| 1096 |
CRYPTO_EX_free *free_func) { |
| 1097 |
int index; |
| 1098 |
if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 1099 |
free_func)) { |
| 1100 |
return -1; |
| 1101 |
} |
| 1102 |
return index; |
| 1103 |
} |
| 1104 |
|
| 1105 |
int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) { |
| 1106 |
return CRYPTO_set_ex_data(&session->ex_data, idx, arg); |
| 1107 |
} |
| 1108 |
|
| 1109 |
void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) { |
| 1110 |
return CRYPTO_get_ex_data(&session->ex_data, idx); |
| 1111 |
} |
| 1112 |
|
| 1113 |
int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) { |
| 1114 |
UniquePtr<SSL_SESSION> owned_session = UpRef(session); |
| 1115 |
MutexWriteLock lock(&ctx->lock); |
| 1116 |
return add_session_locked(ctx, std::move(owned_session)); |
| 1117 |
} |
| 1118 |
|
| 1119 |
int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) { |
| 1120 |
return remove_session(ctx, session, /*lock=*/true); |
| 1121 |
} |
| 1122 |
|
| 1123 |
int SSL_set_session(SSL *ssl, SSL_SESSION *session) { |
| 1124 |
// SSL_set_session may only be called before the handshake has started. |
| 1125 |
if (ssl->s3->initial_handshake_complete || |
| 1126 |
ssl->s3->hs == NULL || |
| 1127 |
ssl->s3->hs->state != 0) { |
| 1128 |
abort(); |
| 1129 |
} |
| 1130 |
|
| 1131 |
ssl_set_session(ssl, session); |
| 1132 |
return 1; |
| 1133 |
} |
| 1134 |
|
| 1135 |
uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) { |
| 1136 |
if (ctx == NULL) { |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
// Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|. |
| 1141 |
if (timeout == 0) { |
| 1142 |
timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 1143 |
} |
| 1144 |
|
| 1145 |
uint32_t old_timeout = ctx->session_timeout; |
| 1146 |
ctx->session_timeout = timeout; |
| 1147 |
return old_timeout; |
| 1148 |
} |
| 1149 |
|
| 1150 |
uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) { |
| 1151 |
if (ctx == NULL) { |
| 1152 |
return 0; |
| 1153 |
} |
| 1154 |
|
| 1155 |
return ctx->session_timeout; |
| 1156 |
} |
| 1157 |
|
| 1158 |
void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) { |
| 1159 |
ctx->session_psk_dhe_timeout = timeout; |
| 1160 |
} |
| 1161 |
|
| 1162 |
typedef struct timeout_param_st { |
| 1163 |
SSL_CTX *ctx; |
| 1164 |
uint64_t time; |
| 1165 |
LHASH_OF(SSL_SESSION) *cache; |
| 1166 |
} TIMEOUT_PARAM; |
| 1167 |
|
| 1168 |
static void timeout_doall_arg(SSL_SESSION *session, void *void_param) { |
| 1169 |
TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param); |
| 1170 |
|
| 1171 |
if (param->time == 0 || |
| 1172 |
session->time + session->timeout < session->time || |
| 1173 |
param->time > (session->time + session->timeout)) { |
| 1174 |
// TODO(davidben): This can probably just call |remove_session|. |
| 1175 |
(void) lh_SSL_SESSION_delete(param->cache, session); |
| 1176 |
SSL_SESSION_list_remove(param->ctx, session); |
| 1177 |
// TODO(https://crbug.com/boringssl/251): Callbacks should not be called |
| 1178 |
// under a lock. |
| 1179 |
if (param->ctx->remove_session_cb != NULL) { |
| 1180 |
param->ctx->remove_session_cb(param->ctx, session); |
| 1181 |
} |
| 1182 |
SSL_SESSION_free(session); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) { |
| 1187 |
TIMEOUT_PARAM tp; |
| 1188 |
|
| 1189 |
tp.ctx = ctx; |
| 1190 |
tp.cache = ctx->sessions; |
| 1191 |
if (tp.cache == NULL) { |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
tp.time = time; |
| 1195 |
MutexWriteLock lock(&ctx->lock); |
| 1196 |
lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); |
| 1197 |
} |
| 1198 |
|
| 1199 |
void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
| 1200 |
int (*cb)(SSL *ssl, SSL_SESSION *session)) { |
| 1201 |
ctx->new_session_cb = cb; |
| 1202 |
} |
| 1203 |
|
| 1204 |
int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) { |
| 1205 |
return ctx->new_session_cb; |
| 1206 |
} |
| 1207 |
|
| 1208 |
void SSL_CTX_sess_set_remove_cb( |
| 1209 |
SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) { |
| 1210 |
ctx->remove_session_cb = cb; |
| 1211 |
} |
| 1212 |
|
| 1213 |
void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx, |
| 1214 |
SSL_SESSION *session) { |
| 1215 |
return ctx->remove_session_cb; |
| 1216 |
} |
| 1217 |
|
| 1218 |
void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
| 1219 |
SSL_SESSION *(*cb)(SSL *ssl, const uint8_t *id, |
| 1220 |
int id_len, int *out_copy)) { |
| 1221 |
ctx->get_session_cb = cb; |
| 1222 |
} |
| 1223 |
|
| 1224 |
SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, |
| 1225 |
const uint8_t *id, |
| 1226 |
int id_len, |
| 1227 |
int *out_copy) { |
| 1228 |
return ctx->get_session_cb; |
| 1229 |
} |
| 1230 |
|
| 1231 |
void SSL_CTX_set_info_callback( |
| 1232 |
SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) { |
| 1233 |
ctx->info_callback = cb; |
| 1234 |
} |
| 1235 |
|
| 1236 |
void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, |
| 1237 |
int value) { |
| 1238 |
return ctx->info_callback; |
| 1239 |
} |