Coverage Report

Created: 2025-06-11 06:40

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