]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_sess.c
Optimize session cache flushing
[thirdparty/openssl.git] / ssl / ssl_sess.c
CommitLineData
846e33c7 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
c80149d9 3 * Copyright 2005 Nokia. All rights reserved.
f1fd4544 4 *
2c18d164 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
f1fd4544 9 */
846e33c7 10
650c6687
RB
11#if defined(__TANDEM) && defined(_SPT_MODEL_)
12# include <spthread.h>
13# include <spt_extensions.h> /* timeval */
14#endif
d02b48c6 15#include <stdio.h>
ec577822 16#include <openssl/rand.h>
3c27208f 17#include <openssl/engine.h>
cd420b0b 18#include "internal/refcount.h"
6cc0b3c2 19#include "internal/cryptlib.h"
706457b7
DMSP
20#include "ssl_local.h"
21#include "statem/statem_local.h"
d02b48c6 22
58964a49 23static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
0f113f3e 24static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
801294f8 25static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
58964a49 26
25959e04
TS
27DEFINE_STACK_OF(SSL_SESSION)
28
29__owur static int sess_timedout(time_t t, SSL_SESSION *ss)
30{
31 /* if timeout overflowed, it can never timeout! */
32 if (ss->timeout_ovf)
33 return 0;
34 return t > ss->calc_timeout;
35}
36
37/*
38 * Returns -1/0/+1 as other XXXcmp-type functions
39 * Takes overflow of calculated timeout into consideration
40 */
41__owur static int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b)
42{
43 /* if only one overflowed, then it is greater */
44 if (a->timeout_ovf && !b->timeout_ovf)
45 return 1;
46 if (!a->timeout_ovf && b->timeout_ovf)
47 return -1;
48 /* No overflow, or both overflowed, so straight compare is safe */
49 if (a->calc_timeout < b->calc_timeout)
50 return -1;
51 if (a->calc_timeout > b->calc_timeout)
52 return 1;
53 return 0;
54}
55
56/*
57 * Calculates effective timeout, saving overflow state
58 * Locking must be done by the caller of this function
59 */
60void ssl_session_calculate_timeout(SSL_SESSION *ss)
61{
62 /* Force positive timeout */
63 if (ss->timeout < 0)
64 ss->timeout = 0;
65 ss->calc_timeout = ss->time + ss->timeout;
66 /*
67 * |timeout| is always zero or positive, so the check for
68 * overflow only needs to consider if |time| is positive
69 */
70 ss->timeout_ovf = ss->time > 0 && ss->calc_timeout < ss->time;
71 /*
72 * N.B. Realistic overflow can only occur in our lifetimes on a
73 * 32-bit machine in January 2038.
74 * However, There are no controls to limit the |timeout|
75 * value, except to keep it positive.
76 */
77}
78
e7a28df7 79/*
e586eac8
MC
80 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
81 * unlike in earlier protocol versions, the session ticket may not have been
82 * sent yet even though a handshake has finished. The session ticket data could
83 * come in sometime later...or even change if multiple session ticket messages
84 * are sent from the server. The preferred way for applications to obtain
85 * a resumable session is to use SSL_CTX_sess_set_new_cb().
e7a28df7
MC
86 */
87
0821bcd4 88SSL_SESSION *SSL_get_session(const SSL *ssl)
52732b38 89/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
0f113f3e 90{
26a7d938 91 return ssl->session;
0f113f3e 92}
52732b38
BM
93
94SSL_SESSION *SSL_get1_session(SSL *ssl)
95/* variant of SSL_get_session: caller really gets something */
0f113f3e
MC
96{
97 SSL_SESSION *sess;
98 /*
99 * Need to lock this all up rather than just use CRYPTO_add so that
100 * somebody doesn't free ssl->session between when we check it's non-null
101 * and when we up the reference count.
102 */
cd3f8c1b
RS
103 if (!CRYPTO_THREAD_read_lock(ssl->lock))
104 return NULL;
0f113f3e
MC
105 sess = ssl->session;
106 if (sess)
16203f7b
AG
107 SSL_SESSION_up_ref(sess);
108 CRYPTO_THREAD_unlock(ssl->lock);
109 return sess;
0f113f3e
MC
110}
111
6b691a5c 112int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
0f113f3e 113{
26a7d938 114 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
0f113f3e 115}
58964a49 116
0821bcd4 117void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
0f113f3e 118{
26a7d938 119 return CRYPTO_get_ex_data(&s->ex_data, idx);
0f113f3e 120}
58964a49 121
6b691a5c 122SSL_SESSION *SSL_SESSION_new(void)
0f113f3e
MC
123{
124 SSL_SESSION *ss;
125
534a43ff
MC
126 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
127 return NULL;
128
b51bce94 129 ss = OPENSSL_zalloc(sizeof(*ss));
0f113f3e 130 if (ss == NULL) {
6849b73c 131 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
16203f7b 132 return NULL;
0f113f3e 133 }
0f113f3e
MC
134
135 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
136 ss->references = 1;
137 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
25959e04
TS
138 ss->time = time(NULL);
139 ssl_session_calculate_timeout(ss);
16203f7b
AG
140 ss->lock = CRYPTO_THREAD_lock_new();
141 if (ss->lock == NULL) {
6849b73c 142 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
16203f7b
AG
143 OPENSSL_free(ss);
144 return NULL;
145 }
146
25a807bc
F
147 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
148 CRYPTO_THREAD_lock_free(ss->lock);
149 OPENSSL_free(ss);
150 return NULL;
151 }
16203f7b 152 return ss;
0f113f3e 153}
d02b48c6 154
9fdcc21f 155SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
07927bed
MC
156{
157 return ssl_session_dup(src, 1);
158}
159
98ece4ee
MC
160/*
161 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
162 * ticket == 0 then no ticket information is duplicated, otherwise it is.
163 */
9fdcc21f 164SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
98ece4ee
MC
165{
166 SSL_SESSION *dest;
167
8ad36917 168 dest = OPENSSL_malloc(sizeof(*dest));
98ece4ee
MC
169 if (dest == NULL) {
170 goto err;
171 }
172 memcpy(dest, src, sizeof(*dest));
173
708cf593
MC
174 /*
175 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
176 * the case of an error whilst halfway through constructing dest
177 */
178#ifndef OPENSSL_NO_PSK
179 dest->psk_identity_hint = NULL;
180 dest->psk_identity = NULL;
181#endif
aff8c126 182 dest->ext.hostname = NULL;
aff8c126 183 dest->ext.tick = NULL;
eed3ec90 184 dest->ext.alpn_selected = NULL;
708cf593
MC
185#ifndef OPENSSL_NO_SRP
186 dest->srp_username = NULL;
187#endif
1ee21259
TS
188 dest->peer_chain = NULL;
189 dest->peer = NULL;
df0fed9a 190 dest->ticket_appdata = NULL;
51598215 191 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
9b6a8254 192
708cf593
MC
193 /* We deliberately don't copy the prev and next pointers */
194 dest->prev = NULL;
195 dest->next = NULL;
196
197 dest->references = 1;
198
16203f7b
AG
199 dest->lock = CRYPTO_THREAD_lock_new();
200 if (dest->lock == NULL)
201 goto err;
202
1ee21259
TS
203 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data))
204 goto err;
205
206 if (src->peer != NULL) {
207 if (!X509_up_ref(src->peer))
208 goto err;
209 dest->peer = src->peer;
210 }
708cf593 211
36f038f1
DSH
212 if (src->peer_chain != NULL) {
213 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
214 if (dest->peer_chain == NULL)
215 goto err;
216 }
98ece4ee
MC
217#ifndef OPENSSL_NO_PSK
218 if (src->psk_identity_hint) {
7644a9ae 219 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
98ece4ee
MC
220 if (dest->psk_identity_hint == NULL) {
221 goto err;
222 }
98ece4ee
MC
223 }
224 if (src->psk_identity) {
7644a9ae 225 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
98ece4ee
MC
226 if (dest->psk_identity == NULL) {
227 goto err;
228 }
98ece4ee
MC
229 }
230#endif
231
98ece4ee 232 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
a230b26e 233 &dest->ex_data, &src->ex_data)) {
98ece4ee
MC
234 goto err;
235 }
236
aff8c126
RS
237 if (src->ext.hostname) {
238 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
239 if (dest->ext.hostname == NULL) {
98ece4ee
MC
240 goto err;
241 }
98ece4ee 242 }
98ece4ee 243
1ee21259 244 if (ticket != 0 && src->ext.tick != NULL) {
aff8c126
RS
245 dest->ext.tick =
246 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
247 if (dest->ext.tick == NULL)
98ece4ee 248 goto err;
708cf593 249 } else {
aff8c126
RS
250 dest->ext.tick_lifetime_hint = 0;
251 dest->ext.ticklen = 0;
98ece4ee
MC
252 }
253
27232cc3
MC
254 if (src->ext.alpn_selected != NULL) {
255 dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
256 src->ext.alpn_selected_len);
257 if (dest->ext.alpn_selected == NULL)
eed3ec90 258 goto err;
eed3ec90
TS
259 }
260
98ece4ee 261#ifndef OPENSSL_NO_SRP
98ece4ee 262 if (src->srp_username) {
7644a9ae 263 dest->srp_username = OPENSSL_strdup(src->srp_username);
98ece4ee
MC
264 if (dest->srp_username == NULL) {
265 goto err;
266 }
98ece4ee
MC
267 }
268#endif
269
df0fed9a
TS
270 if (src->ticket_appdata != NULL) {
271 dest->ticket_appdata =
272 OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
273 if (dest->ticket_appdata == NULL)
274 goto err;
275 }
276
98ece4ee 277 return dest;
a230b26e 278 err:
6849b73c 279 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
98ece4ee
MC
280 SSL_SESSION_free(dest);
281 return NULL;
282}
283
a230b26e 284const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
0f113f3e
MC
285{
286 if (len)
348240c6 287 *len = (unsigned int)s->session_id_length;
0f113f3e
MC
288 return s->session_id;
289}
fddfc0af
RG
290const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
291 unsigned int *len)
292{
293 if (len != NULL)
348240c6 294 *len = (unsigned int)s->sid_ctx_length;
fddfc0af
RG
295 return s->sid_ctx;
296}
4879ec7b 297
f9b0b452 298unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
0f113f3e
MC
299{
300 return s->compress_meth;
301}
302
303/*
304 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
305 * the ID with random junk repeatedly until we have no conflict is going to
306 * complete in one iteration pretty much "most" of the time (btw:
307 * understatement). So, if it takes us 10 iterations and we still can't avoid
308 * a conflict - well that's a reasonable point to call it quits. Either the
309 * RAND code is broken or someone is trying to open roughly very close to
310 * 2^256 SSL sessions to our server. How you might store that many sessions
311 * is perhaps a more interesting question ...
312 */
dc644fe2
GT
313
314#define MAX_SESS_ID_ATTEMPTS 10
ae3947de 315static int def_generate_session_id(SSL *ssl, unsigned char *id,
0f113f3e 316 unsigned int *id_len)
dc644fe2 317{
0f113f3e
MC
318 unsigned int retry = 0;
319 do
0f8815aa 320 if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0)
0f113f3e
MC
321 return 0;
322 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
323 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
324 if (retry < MAX_SESS_ID_ATTEMPTS)
325 return 1;
326 /* else - woops a session_id match */
327 /*
328 * XXX We should also check the external cache -- but the probability of
329 * a collision is negligible, and we could not prevent the concurrent
330 * creation of sessions with identical IDs since we currently don't have
331 * means to atomically check whether a session ID already exists and make
332 * a reservation for it if it does not (this problem applies to the
333 * internal cache as well).
334 */
335 return 0;
dc644fe2
GT
336}
337
a84e5c9a
TS
338int ssl_generate_session_id(SSL *s, SSL_SESSION *ss)
339{
340 unsigned int tmp;
341 GEN_SESSION_CB cb = def_generate_session_id;
342
343 switch (s->version) {
344 case SSL3_VERSION:
345 case TLS1_VERSION:
346 case TLS1_1_VERSION:
347 case TLS1_2_VERSION:
348 case TLS1_3_VERSION:
349 case DTLS1_BAD_VER:
350 case DTLS1_VERSION:
351 case DTLS1_2_VERSION:
352 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
353 break;
354 default:
c48ffbcc 355 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
a84e5c9a
TS
356 return 0;
357 }
358
359 /*-
360 * If RFC5077 ticket, use empty session ID (as server).
361 * Note that:
362 * (a) ssl_get_prev_session() does lookahead into the
363 * ClientHello extensions to find the session ticket.
364 * When ssl_get_prev_session() fails, statem_srvr.c calls
365 * ssl_get_new_session() in tls_process_client_hello().
366 * At that point, it has not yet parsed the extensions,
367 * however, because of the lookahead, it already knows
368 * whether a ticket is expected or not.
369 *
370 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
371 * ServerHello extensions, and before recording the session
372 * ID received from the server, so this block is a noop.
373 */
374 if (s->ext.ticket_expected) {
375 ss->session_id_length = 0;
376 return 1;
377 }
378
379 /* Choose which callback will set the session ID */
cd3f8c1b
RS
380 if (!CRYPTO_THREAD_read_lock(s->lock))
381 return 0;
382 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
383 CRYPTO_THREAD_unlock(s->lock);
384 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
385 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
386 return 0;
387 }
a84e5c9a
TS
388 if (s->generate_session_id)
389 cb = s->generate_session_id;
390 else if (s->session_ctx->generate_session_id)
391 cb = s->session_ctx->generate_session_id;
392 CRYPTO_THREAD_unlock(s->session_ctx->lock);
393 CRYPTO_THREAD_unlock(s->lock);
394 /* Choose a session ID */
395 memset(ss->session_id, 0, ss->session_id_length);
396 tmp = (int)ss->session_id_length;
397 if (!cb(s, ss->session_id, &tmp)) {
398 /* The callback failed */
c48ffbcc 399 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6 400 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
a84e5c9a
TS
401 return 0;
402 }
403 /*
404 * Don't allow the callback to set the session length to zero. nor
405 * set it higher than it was.
406 */
407 if (tmp == 0 || tmp > ss->session_id_length) {
408 /* The callback set an illegal length */
c48ffbcc 409 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6 410 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
a84e5c9a
TS
411 return 0;
412 }
413 ss->session_id_length = tmp;
414 /* Finally, check for a conflict */
415 if (SSL_has_matching_session_id(s, ss->session_id,
416 (unsigned int)ss->session_id_length)) {
c48ffbcc 417 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
a84e5c9a
TS
418 return 0;
419 }
420
421 return 1;
422}
423
6b691a5c 424int ssl_get_new_session(SSL *s, int session)
0f113f3e
MC
425{
426 /* This gets used by clients and servers. */
427
0f113f3e 428 SSL_SESSION *ss = NULL;
0f113f3e 429
f63a17d6 430 if ((ss = SSL_SESSION_new()) == NULL) {
c48ffbcc 431 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
a84e5c9a 432 return 0;
f63a17d6 433 }
0f113f3e
MC
434
435 /* If the context has a default timeout, use it */
436 if (s->session_ctx->session_timeout == 0)
437 ss->timeout = SSL_get_default_timeout(s);
438 else
439 ss->timeout = s->session_ctx->session_timeout;
440
62adbcee
RS
441 SSL_SESSION_free(s->session);
442 s->session = NULL;
0f113f3e
MC
443
444 if (session) {
32305f88
MC
445 if (SSL_IS_TLS13(s)) {
446 /*
447 * We generate the session id while constructing the
448 * NewSessionTicket in TLSv1.3.
449 */
450 ss->session_id_length = 0;
451 } else if (!ssl_generate_session_id(s, ss)) {
f63a17d6 452 /* SSLfatal() already called */
0f113f3e 453 SSL_SESSION_free(ss);
a84e5c9a 454 return 0;
0f113f3e 455 }
e481f9b9 456
0f113f3e
MC
457 } else {
458 ss->session_id_length = 0;
459 }
460
cbe29648 461 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
c48ffbcc 462 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
463 SSL_SESSION_free(ss);
464 return 0;
465 }
466 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
467 ss->sid_ctx_length = s->sid_ctx_length;
468 s->session = ss;
469 ss->ssl_version = s->version;
470 ss->verify_result = X509_V_OK;
471
e7f0d921 472 /* If client supports extended master secret set it in session */
555cbb32 473 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
e7f0d921
DSH
474 ss->flags |= SSL_SESS_FLAG_EXTMS;
475
a84e5c9a 476 return 1;
0f113f3e 477}
d02b48c6 478
6cc0b3c2
MC
479SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id,
480 size_t sess_id_len)
481{
482 SSL_SESSION *ret = NULL;
6cc0b3c2
MC
483
484 if ((s->session_ctx->session_cache_mode
485 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
486 SSL_SESSION data;
487
488 data.ssl_version = s->version;
489 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
490 return NULL;
491
492 memcpy(data.session_id, sess_id, sess_id_len);
493 data.session_id_length = sess_id_len;
494
cd3f8c1b
RS
495 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
496 return NULL;
6cc0b3c2
MC
497 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
498 if (ret != NULL) {
499 /* don't allow other threads to steal it: */
500 SSL_SESSION_up_ref(ret);
501 }
502 CRYPTO_THREAD_unlock(s->session_ctx->lock);
503 if (ret == NULL)
9ef9088c 504 tsan_counter(&s->session_ctx->stats.sess_miss);
6cc0b3c2
MC
505 }
506
507 if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
508 int copy = 1;
509
510 ret = s->session_ctx->get_session_cb(s, sess_id, sess_id_len, &copy);
511
512 if (ret != NULL) {
9ef9088c 513 tsan_counter(&s->session_ctx->stats.sess_cb_hit);
6cc0b3c2
MC
514
515 /*
516 * Increment reference count now if the session callback asks us
517 * to do so (note that if the session structures returned by the
518 * callback are shared between threads, it must handle the
519 * reference count itself [i.e. copy == 0], or things won't be
520 * thread-safe).
521 */
522 if (copy)
523 SSL_SESSION_up_ref(ret);
524
525 /*
526 * Add the externally cached session to the internal cache as
527 * well if and only if we are supposed to.
528 */
529 if ((s->session_ctx->session_cache_mode &
530 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
531 /*
532 * Either return value of SSL_CTX_add_session should not
533 * interrupt the session resumption process. The return
534 * value is intentionally ignored.
535 */
536 (void)SSL_CTX_add_session(s->session_ctx, ret);
537 }
538 }
539 }
540
541 return ret;
542}
543
3a83462d
MC
544/*-
545 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
c519e89f
BM
546 * connection. It is only called by servers.
547 *
de7d61d5 548 * hello: The parsed ClientHello data
c519e89f
BM
549 *
550 * Returns:
1053a6e2
MC
551 * -1: fatal error
552 * 0: no session found
553 * 1: a session may have been found.
c519e89f
BM
554 *
555 * Side effects:
556 * - If a session is found then s->session is pointed at it (after freeing an
557 * existing session if need be) and s->verify_result is set from the session.
aff8c126 558 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
c519e89f
BM
559 * if the server should issue a new session ticket (to 0 otherwise).
560 */
f63a17d6 561int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello)
0f113f3e
MC
562{
563 /* This is used only by servers. */
b56bce4f 564
0f113f3e 565 SSL_SESSION *ret = NULL;
9ef9088c 566 int fatal = 0;
1053a6e2 567 int try_session_cache = 0;
61fb5923 568 SSL_TICKET_STATUS r;
d02b48c6 569
1053a6e2 570 if (SSL_IS_TLS13(s)) {
61fb5923
MC
571 /*
572 * By default we will send a new ticket. This can be overridden in the
573 * ticket processing.
574 */
575 s->ext.ticket_expected = 1;
fe874d27
MC
576 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
577 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
f63a17d6 578 NULL, 0)
fe874d27 579 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
f63a17d6 580 hello->pre_proc_exts, NULL, 0))
1053a6e2
MC
581 return -1;
582
583 ret = s->session;
584 } else {
585 /* sets s->ext.ticket_expected */
586 r = tls_get_ticket_from_client(s, hello, &ret);
587 switch (r) {
df0fed9a
TS
588 case SSL_TICKET_FATAL_ERR_MALLOC:
589 case SSL_TICKET_FATAL_ERR_OTHER:
1053a6e2 590 fatal = 1;
c48ffbcc 591 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1053a6e2 592 goto err;
df0fed9a
TS
593 case SSL_TICKET_NONE:
594 case SSL_TICKET_EMPTY:
6cc0b3c2 595 if (hello->session_id_len > 0) {
0afca811 596 try_session_cache = 1;
6cc0b3c2
MC
597 ret = lookup_sess_in_cache(s, hello->session_id,
598 hello->session_id_len);
599 }
61c32649 600 break;
df0fed9a
TS
601 case SSL_TICKET_NO_DECRYPT:
602 case SSL_TICKET_SUCCESS:
603 case SSL_TICKET_SUCCESS_RENEW:
1053a6e2 604 break;
1053a6e2 605 }
0f113f3e 606 }
c519e89f 607
0f113f3e
MC
608 if (ret == NULL)
609 goto err;
610
611 /* Now ret is non-NULL and we own one of its reference counts. */
612
128ae276
MC
613 /* Check TLS version consistency */
614 if (ret->ssl_version != s->version)
615 goto err;
616
0f113f3e
MC
617 if (ret->sid_ctx_length != s->sid_ctx_length
618 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
619 /*
620 * We have the session requested by the client, but we don't want to
621 * use it in this context.
622 */
623 goto err; /* treat like cache miss */
624 }
625
626 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
627 /*
628 * We can't be sure if this session is being used out of context,
629 * which is especially important for SSL_VERIFY_PEER. The application
630 * should have used SSL[_CTX]_set_session_id_context. For this error
631 * case, we generate an error instead of treating the event like a
632 * cache miss (otherwise it would be easy for applications to
633 * effectively disable the session cache by accident without anyone
634 * noticing).
635 */
636
c48ffbcc 637 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6 638 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
0f113f3e
MC
639 fatal = 1;
640 goto err;
641 }
642
25959e04 643 if (sess_timedout(time(NULL), ret)) {
9ef9088c 644 tsan_counter(&s->session_ctx->stats.sess_timeout);
0f113f3e
MC
645 if (try_session_cache) {
646 /* session was from the cache, so remove it */
647 SSL_CTX_remove_session(s->session_ctx, ret);
648 }
649 goto err;
650 }
651
e7f0d921
DSH
652 /* Check extended master secret extension consistency */
653 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
654 /* If old session includes extms, but new does not: abort handshake */
555cbb32 655 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
c48ffbcc 656 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS);
e7f0d921
DSH
657 fatal = 1;
658 goto err;
659 }
555cbb32 660 } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
e7f0d921
DSH
661 /* If new session includes extms, but old does not: do not resume */
662 goto err;
663 }
664
1053a6e2
MC
665 if (!SSL_IS_TLS13(s)) {
666 /* We already did this for TLS1.3 */
667 SSL_SESSION_free(s->session);
668 s->session = ret;
669 }
0f113f3e 670
9ef9088c 671 tsan_counter(&s->session_ctx->stats.sess_hit);
0f113f3e
MC
672 s->verify_result = s->session->verify_result;
673 return 1;
8876bc05
BM
674
675 err:
0f113f3e
MC
676 if (ret != NULL) {
677 SSL_SESSION_free(ret);
1f5b44e9 678 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
128ae276
MC
679 if (SSL_IS_TLS13(s))
680 s->session = NULL;
e481f9b9 681
0f113f3e
MC
682 if (!try_session_cache) {
683 /*
684 * The session was from a ticket, so we should issue a ticket for
685 * the new session
686 */
aff8c126 687 s->ext.ticket_expected = 1;
0f113f3e 688 }
0f113f3e 689 }
f63a17d6 690 if (fatal)
0f113f3e 691 return -1;
40f805ad
MC
692
693 return 0;
0f113f3e 694}
d02b48c6 695
6b691a5c 696int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
0f113f3e 697{
9ef9088c 698 int ret = 0;
0f113f3e
MC
699 SSL_SESSION *s;
700
701 /*
702 * add just 1 reference count for the SSL_CTX's session cache even though
703 * it has two ways of access: each session is in a doubly linked list and
704 * an lhash
705 */
16203f7b 706 SSL_SESSION_up_ref(c);
0f113f3e
MC
707 /*
708 * if session c is in already in cache, we take back the increment later
709 */
710
cd3f8c1b
RS
711 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
712 SSL_SESSION_free(c);
713 return 0;
714 }
0f113f3e
MC
715 s = lh_SSL_SESSION_insert(ctx->sessions, c);
716
717 /*
718 * s != NULL iff we already had a session with the given PID. In this
719 * case, s == c should hold (then we did not really modify
720 * ctx->sessions), or we're in trouble.
721 */
722 if (s != NULL && s != c) {
723 /* We *are* in trouble ... */
724 SSL_SESSION_list_remove(ctx, s);
725 SSL_SESSION_free(s);
726 /*
727 * ... so pretend the other session did not exist in cache (we cannot
728 * handle two SSL_SESSION structures with identical session ID in the
729 * same cache, which could happen e.g. when two threads concurrently
730 * obtain the same session from an external cache)
731 */
732 s = NULL;
38088ce9
BE
733 } else if (s == NULL &&
734 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
735 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
736
737 /*
738 * ... so take back the extra reference and also don't add
739 * the session to the SSL_SESSION_list at this time
740 */
741 s = c;
0f113f3e
MC
742 }
743
25959e04
TS
744 /* Adjust last used time, and add back into the cache at the appropriate spot */
745 if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) {
746 c->time = time(NULL);
747 ssl_session_calculate_timeout(c);
748 }
749 SSL_SESSION_list_add(ctx, c);
0f113f3e
MC
750
751 if (s != NULL) {
752 /*
753 * existing cache entry -- decrement previously incremented reference
754 * count because it already takes into account the cache
755 */
756
757 SSL_SESSION_free(s); /* s == c */
758 ret = 0;
759 } else {
760 /*
761 * new cache entry -- remove old ones if cache has become too large
762 */
763
764 ret = 1;
765
766 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
a230b26e 767 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
0f113f3e
MC
768 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
769 break;
770 else
9ef9088c 771 tsan_counter(&ctx->stats.sess_cache_full);
0f113f3e
MC
772 }
773 }
774 }
16203f7b
AG
775 CRYPTO_THREAD_unlock(ctx->lock);
776 return ret;
0f113f3e 777}
d02b48c6 778
6b691a5c 779int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
801294f8 780{
0f113f3e 781 return remove_session_lock(ctx, c, 1);
801294f8
DSH
782}
783
0fda2e37 784static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
0f113f3e
MC
785{
786 SSL_SESSION *r;
787 int ret = 0;
788
789 if ((c != NULL) && (c->session_id_length != 0)) {
cd3f8c1b
RS
790 if (lck) {
791 if (!CRYPTO_THREAD_write_lock(ctx->lock))
792 return 0;
793 }
66d7de16 794 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
0f113f3e 795 ret = 1;
66d7de16
MC
796 r = lh_SSL_SESSION_delete(ctx->sessions, r);
797 SSL_SESSION_list_remove(ctx, r);
0f113f3e 798 }
e4612d02 799 c->not_resumable = 1;
0f113f3e
MC
800
801 if (lck)
16203f7b 802 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e 803
e4612d02
MC
804 if (ctx->remove_session_cb != NULL)
805 ctx->remove_session_cb(ctx, c);
c0a58e03
MH
806
807 if (ret)
808 SSL_SESSION_free(r);
cd3f8c1b 809 }
26a7d938 810 return ret;
0f113f3e 811}
d02b48c6 812
6b691a5c 813void SSL_SESSION_free(SSL_SESSION *ss)
0f113f3e
MC
814{
815 int i;
d02b48c6 816
e6e9170d
RS
817 if (ss == NULL)
818 return;
2f545ae4 819 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
f3f1cf84 820 REF_PRINT_COUNT("SSL_SESSION", ss);
0f113f3e
MC
821 if (i > 0)
822 return;
f3f1cf84 823 REF_ASSERT_ISNT(i < 0);
d02b48c6 824
0f113f3e 825 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
58964a49 826
cbe29648
RS
827 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
828 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
222561fe 829 X509_free(ss->peer);
c34b0f99 830 sk_X509_pop_free(ss->peer_chain, X509_free);
aff8c126
RS
831 OPENSSL_free(ss->ext.hostname);
832 OPENSSL_free(ss->ext.tick);
ddac1974 833#ifndef OPENSSL_NO_PSK
25aaa98a
RS
834 OPENSSL_free(ss->psk_identity_hint);
835 OPENSSL_free(ss->psk_identity);
edc032b5
BL
836#endif
837#ifndef OPENSSL_NO_SRP
25aaa98a 838 OPENSSL_free(ss->srp_username);
ed3883d2 839#endif
f6370040 840 OPENSSL_free(ss->ext.alpn_selected);
df0fed9a 841 OPENSSL_free(ss->ticket_appdata);
16203f7b 842 CRYPTO_THREAD_lock_free(ss->lock);
4b45c6e5 843 OPENSSL_clear_free(ss, sizeof(*ss));
0f113f3e 844}
d02b48c6 845
16203f7b
AG
846int SSL_SESSION_up_ref(SSL_SESSION *ss)
847{
848 int i;
849
2f545ae4 850 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
16203f7b
AG
851 return 0;
852
853 REF_PRINT_COUNT("SSL_SESSION", ss);
854 REF_ASSERT_ISNT(i < 2);
855 return ((i > 1) ? 1 : 0);
856}
857
6b691a5c 858int SSL_set_session(SSL *s, SSL_SESSION *session)
0f113f3e 859{
e70656cf
MC
860 ssl_clear_bad_session(s);
861 if (s->ctx->method != s->method) {
862 if (!SSL_set_ssl_method(s, s->ctx->method))
863 return 0;
864 }
0f113f3e 865
e70656cf 866 if (session != NULL) {
16203f7b 867 SSL_SESSION_up_ref(session);
e70656cf 868 s->verify_result = session->verify_result;
0f113f3e 869 }
e70656cf
MC
870 SSL_SESSION_free(s->session);
871 s->session = session;
872
873 return 1;
0f113f3e 874}
d02b48c6 875
fddfc0af
RG
876int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
877 unsigned int sid_len)
878{
879 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
6849b73c 880 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
fddfc0af
RG
881 return 0;
882 }
883 s->session_id_length = sid_len;
6aad9393
RG
884 if (sid != s->session_id)
885 memcpy(s->session_id, sid, sid_len);
fddfc0af
RG
886 return 1;
887}
888
6b691a5c 889long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
0f113f3e 890{
25959e04
TS
891 time_t new_timeout = (time_t)t;
892
893 if (s == NULL || t < 0)
26a7d938 894 return 0;
25959e04
TS
895 if (s->owner != NULL) {
896 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
897 return 0;
898 s->timeout = new_timeout;
899 ssl_session_calculate_timeout(s);
900 SSL_SESSION_list_add(s->owner, s);
901 CRYPTO_THREAD_unlock(s->owner->lock);
902 } else {
903 s->timeout = new_timeout;
904 ssl_session_calculate_timeout(s);
905 }
208fb891 906 return 1;
0f113f3e 907}
d02b48c6 908
0821bcd4 909long SSL_SESSION_get_timeout(const SSL_SESSION *s)
0f113f3e
MC
910{
911 if (s == NULL)
26a7d938 912 return 0;
25959e04 913 return (long)s->timeout;
0f113f3e 914}
d02b48c6 915
0821bcd4 916long SSL_SESSION_get_time(const SSL_SESSION *s)
0f113f3e
MC
917{
918 if (s == NULL)
26a7d938 919 return 0;
25959e04 920 return (long)s->time;
0f113f3e 921}
d02b48c6 922
6b691a5c 923long SSL_SESSION_set_time(SSL_SESSION *s, long t)
0f113f3e 924{
25959e04
TS
925 time_t new_time = (time_t)t;
926
0f113f3e 927 if (s == NULL)
26a7d938 928 return 0;
25959e04
TS
929 if (s->owner != NULL) {
930 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
931 return 0;
932 s->time = new_time;
933 ssl_session_calculate_timeout(s);
934 SSL_SESSION_list_add(s->owner, s);
935 CRYPTO_THREAD_unlock(s->owner->lock);
936 } else {
937 s->time = new_time;
938 ssl_session_calculate_timeout(s);
939 }
26a7d938 940 return t;
0f113f3e 941}
d02b48c6 942
bd01f649
TS
943int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
944{
bd01f649
TS
945 return s->ssl_version;
946}
947
5a43d511
MC
948int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
949{
950 s->ssl_version = version;
951 return 1;
952}
953
e9281323
RS
954const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
955{
956 return s->cipher;
957}
1a993d1d
MC
958
959int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
960{
961 s->cipher = cipher;
962 return 1;
963}
e9281323 964
4b6b8487
LC
965const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
966{
aff8c126 967 return s->ext.hostname;
4b6b8487
LC
968}
969
67738645
MC
970int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
971{
972 OPENSSL_free(s->ext.hostname);
973 if (hostname == NULL) {
974 s->ext.hostname = NULL;
975 return 1;
976 }
977 s->ext.hostname = OPENSSL_strdup(hostname);
978
979 return s->ext.hostname != NULL;
980}
981
f2baac27
MC
982int SSL_SESSION_has_ticket(const SSL_SESSION *s)
983{
aff8c126 984 return (s->ext.ticklen > 0) ? 1 : 0;
f2baac27
MC
985}
986
987unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
988{
aff8c126 989 return s->ext.tick_lifetime_hint;
f2baac27
MC
990}
991
48593cb1 992void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
a230b26e 993 size_t *len)
b7c9187b 994{
aff8c126 995 *len = s->ext.ticklen;
61986d32 996 if (tick != NULL)
aff8c126 997 *tick = s->ext.tick;
b7c9187b
MC
998}
999
fcc47578
MC
1000uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
1001{
1002 return s->ext.max_early_data;
1003}
1004
98e1d934
MC
1005int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
1006{
1007 s->ext.max_early_data = max_early_data;
1008
1009 return 1;
1010}
1011
67738645
MC
1012void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
1013 const unsigned char **alpn,
1014 size_t *len)
1015{
1016 *alpn = s->ext.alpn_selected;
1017 *len = s->ext.alpn_selected_len;
1018}
1019
1020int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
1021 size_t len)
1022{
1023 OPENSSL_free(s->ext.alpn_selected);
1024 if (alpn == NULL || len == 0) {
1025 s->ext.alpn_selected = NULL;
1026 s->ext.alpn_selected_len = 0;
1027 return 1;
1028 }
1029 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
1030 if (s->ext.alpn_selected == NULL) {
1031 s->ext.alpn_selected_len = 0;
1032 return 0;
1033 }
1034 s->ext.alpn_selected_len = len;
1035
1036 return 1;
1037}
1038
08557cf2 1039X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
0f113f3e
MC
1040{
1041 return s->peer;
1042}
1043
1044int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1045 unsigned int sid_ctx_len)
1046{
1047 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
6849b73c 1048 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
0f113f3e
MC
1049 return 0;
1050 }
1051 s->sid_ctx_length = sid_ctx_len;
6aad9393
RG
1052 if (sid_ctx != s->sid_ctx)
1053 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
0f113f3e
MC
1054
1055 return 1;
1056}
08557cf2 1057
e586eac8
MC
1058int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1059{
1060 /*
1061 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1062 * session ID.
1063 */
1064 return !s->not_resumable
1065 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1066}
1067
6b691a5c 1068long SSL_CTX_set_timeout(SSL_CTX *s, long t)
0f113f3e
MC
1069{
1070 long l;
1071 if (s == NULL)
26a7d938 1072 return 0;
0f113f3e
MC
1073 l = s->session_timeout;
1074 s->session_timeout = t;
26a7d938 1075 return l;
0f113f3e 1076}
413c4f45 1077
0821bcd4 1078long SSL_CTX_get_timeout(const SSL_CTX *s)
0f113f3e
MC
1079{
1080 if (s == NULL)
26a7d938
K
1081 return 0;
1082 return s->session_timeout;
0f113f3e 1083}
413c4f45 1084
0f113f3e 1085int SSL_set_session_secret_cb(SSL *s,
aff8c126 1086 tls_session_secret_cb_fn tls_session_secret_cb,
0f113f3e
MC
1087 void *arg)
1088{
1089 if (s == NULL)
26a7d938 1090 return 0;
aff8c126
RS
1091 s->ext.session_secret_cb = tls_session_secret_cb;
1092 s->ext.session_secret_cb_arg = arg;
208fb891 1093 return 1;
0f113f3e 1094}
12bf56c0
DSH
1095
1096int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
0f113f3e
MC
1097 void *arg)
1098{
1099 if (s == NULL)
26a7d938 1100 return 0;
aff8c126
RS
1101 s->ext.session_ticket_cb = cb;
1102 s->ext.session_ticket_cb_arg = arg;
208fb891 1103 return 1;
0f113f3e 1104}
12bf56c0
DSH
1105
1106int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
0f113f3e
MC
1107{
1108 if (s->version >= TLS1_VERSION) {
aff8c126
RS
1109 OPENSSL_free(s->ext.session_ticket);
1110 s->ext.session_ticket = NULL;
1111 s->ext.session_ticket =
0f113f3e 1112 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
aff8c126 1113 if (s->ext.session_ticket == NULL) {
6849b73c 1114 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1115 return 0;
1116 }
1117
1ed327f7 1118 if (ext_data != NULL) {
aff8c126
RS
1119 s->ext.session_ticket->length = ext_len;
1120 s->ext.session_ticket->data = s->ext.session_ticket + 1;
1121 memcpy(s->ext.session_ticket->data, ext_data, ext_len);
0f113f3e 1122 } else {
aff8c126
RS
1123 s->ext.session_ticket->length = 0;
1124 s->ext.session_ticket->data = NULL;
0f113f3e
MC
1125 }
1126
1127 return 1;
1128 }
1129
1130 return 0;
1131}
0f113f3e 1132
6b691a5c 1133void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
0f113f3e 1134{
25959e04
TS
1135 STACK_OF(SSL_SESSION) *sk;
1136 SSL_SESSION *current;
0f113f3e 1137 unsigned long i;
0f113f3e 1138
cd3f8c1b
RS
1139 if (!CRYPTO_THREAD_write_lock(s->lock))
1140 return;
25959e04
TS
1141
1142 sk = sk_SSL_SESSION_new_null();
739a1eb1
RS
1143 i = lh_SSL_SESSION_get_down_load(s->sessions);
1144 lh_SSL_SESSION_set_down_load(s->sessions, 0);
25959e04
TS
1145
1146 /*
1147 * Iterate over the list from the back (oldest), and stop
1148 * when a session can no longer be removed.
1149 * Add the session to a temporary list to be freed outside
1150 * the SSL_CTX lock.
1151 * But still do the remove_session_cb() within the lock.
1152 */
1153 while (s->session_cache_tail != NULL) {
1154 current = s->session_cache_tail;
1155 if (t == 0 || sess_timedout((time_t)t, current)) {
1156 lh_SSL_SESSION_delete(s->sessions, current);
1157 SSL_SESSION_list_remove(s, current);
1158 current->not_resumable = 1;
1159 if (s->remove_session_cb != NULL)
1160 s->remove_session_cb(s, current);
1161 /*
1162 * Throw the session on a stack, it's entirely plausible
1163 * that while freeing outside the critical section, the
1164 * session could be re-added, so avoid using the next/prev
1165 * pointers. If the stack failed to create, or the session
1166 * couldn't be put on the stack, just free it here
1167 */
1168 if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
1169 SSL_SESSION_free(current);
1170 } else {
1171 break;
1172 }
1173 }
1174
739a1eb1 1175 lh_SSL_SESSION_set_down_load(s->sessions, i);
16203f7b 1176 CRYPTO_THREAD_unlock(s->lock);
25959e04
TS
1177
1178 sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
0f113f3e 1179}
d02b48c6 1180
6b691a5c 1181int ssl_clear_bad_session(SSL *s)
0f113f3e
MC
1182{
1183 if ((s->session != NULL) &&
1184 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1185 !(SSL_in_init(s) || SSL_in_before(s))) {
e2bb9b9b 1186 SSL_CTX_remove_session(s->session_ctx, s->session);
208fb891 1187 return 1;
0f113f3e 1188 } else
26a7d938 1189 return 0;
0f113f3e 1190}
58964a49
RE
1191
1192/* locked by SSL_CTX in the calling function */
6b691a5c 1193static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e
MC
1194{
1195 if ((s->next == NULL) || (s->prev == NULL))
1196 return;
1197
1198 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1199 /* last element in list */
1200 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1201 /* only one element in list */
1202 ctx->session_cache_head = NULL;
1203 ctx->session_cache_tail = NULL;
1204 } else {
1205 ctx->session_cache_tail = s->prev;
1206 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1207 }
1208 } else {
1209 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1210 /* first element in list */
1211 ctx->session_cache_head = s->next;
1212 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1213 } else {
1214 /* middle of list */
1215 s->next->prev = s->prev;
1216 s->prev->next = s->next;
1217 }
1218 }
1219 s->prev = s->next = NULL;
25959e04 1220 s->owner = NULL;
0f113f3e 1221}
58964a49 1222
6b691a5c 1223static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e 1224{
25959e04
TS
1225 SSL_SESSION *next;
1226
0f113f3e
MC
1227 if ((s->next != NULL) && (s->prev != NULL))
1228 SSL_SESSION_list_remove(ctx, s);
1229
1230 if (ctx->session_cache_head == NULL) {
1231 ctx->session_cache_head = s;
1232 ctx->session_cache_tail = s;
1233 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1234 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1235 } else {
25959e04
TS
1236 if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
1237 /*
1238 * if we timeout after (or the same time as) the first
1239 * session, put us first - usual case
1240 */
1241 s->next = ctx->session_cache_head;
1242 s->next->prev = s;
1243 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1244 ctx->session_cache_head = s;
1245 } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
1246 /* if we timeout before the last session, put us last */
1247 s->prev = ctx->session_cache_tail;
1248 s->prev->next = s;
1249 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1250 ctx->session_cache_tail = s;
1251 } else {
1252 /*
1253 * we timeout somewhere in-between - if there is only
1254 * one session in the cache it will be caught above
1255 */
1256 next = ctx->session_cache_head->next;
1257 while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) {
1258 if (timeoutcmp(s, next) >= 0) {
1259 s->next = next;
1260 s->prev = next->prev;
1261 next->prev->next = s;
1262 next->prev = s;
1263 break;
1264 }
1265 next = next->next;
1266 }
1267 }
0f113f3e 1268 }
25959e04 1269 s->owner = ctx;
0f113f3e 1270}
58964a49 1271
7806f3dd 1272void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
a230b26e 1273 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
0f113f3e
MC
1274{
1275 ctx->new_session_cb = cb;
1276}
7806f3dd 1277
0f113f3e
MC
1278int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1279 return ctx->new_session_cb;
1280}
7806f3dd
NL
1281
1282void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
0f113f3e
MC
1283 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1284{
1285 ctx->remove_session_cb = cb;
1286}
7806f3dd 1287
0f113f3e
MC
1288void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1289 SSL_SESSION *sess) {
1290 return ctx->remove_session_cb;
1291}
7806f3dd
NL
1292
1293void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
0f113f3e 1294 SSL_SESSION *(*cb) (struct ssl_st *ssl,
b6981744
EK
1295 const unsigned char *data,
1296 int len, int *copy))
0f113f3e
MC
1297{
1298 ctx->get_session_cb = cb;
1299}
1300
1301SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
a230b26e
EK
1302 const unsigned char
1303 *data, int len,
1304 int *copy) {
0f113f3e
MC
1305 return ctx->get_session_cb;
1306}
1307
1308void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1309 void (*cb) (const SSL *ssl, int type, int val))
1310{
1311 ctx->info_callback = cb;
1312}
1313
1314void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1315 int val) {
1316 return ctx->info_callback;
1317}
7806f3dd
NL
1318
1319void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
0f113f3e
MC
1320 int (*cb) (SSL *ssl, X509 **x509,
1321 EVP_PKEY **pkey))
1322{
1323 ctx->client_cert_cb = cb;
1324}
7806f3dd 1325
0f113f3e
MC
1326int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1327 EVP_PKEY **pkey) {
1328 return ctx->client_cert_cb;
1329}
7806f3dd
NL
1330
1331void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
0f113f3e
MC
1332 int (*cb) (SSL *ssl,
1333 unsigned char *cookie,
1334 unsigned int *cookie_len))
1335{
1336 ctx->app_gen_cookie_cb = cb;
1337}
7806f3dd
NL
1338
1339void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
a230b26e
EK
1340 int (*cb) (SSL *ssl,
1341 const unsigned char *cookie,
0f113f3e
MC
1342 unsigned int cookie_len))
1343{
1344 ctx->app_verify_cookie_cb = cb;
1345}
7806f3dd 1346
df0fed9a
TS
1347int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1348{
1349 OPENSSL_free(ss->ticket_appdata);
1350 ss->ticket_appdata_len = 0;
1351 if (data == NULL || len == 0) {
1352 ss->ticket_appdata = NULL;
1353 return 1;
1354 }
1355 ss->ticket_appdata = OPENSSL_memdup(data, len);
1356 if (ss->ticket_appdata != NULL) {
1357 ss->ticket_appdata_len = len;
1358 return 1;
1359 }
1360 return 0;
1361}
1362
1363int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1364{
1365 *data = ss->ticket_appdata;
1366 *len = ss->ticket_appdata_len;
1367 return 1;
1368}
1369
3fa2812f
BS
1370void SSL_CTX_set_stateless_cookie_generate_cb(
1371 SSL_CTX *ctx,
1372 int (*cb) (SSL *ssl,
1373 unsigned char *cookie,
1374 size_t *cookie_len))
1375{
1376 ctx->gen_stateless_cookie_cb = cb;
1377}
1378
1379void SSL_CTX_set_stateless_cookie_verify_cb(
1380 SSL_CTX *ctx,
1381 int (*cb) (SSL *ssl,
1382 const unsigned char *cookie,
1383 size_t cookie_len))
1384{
1385 ctx->verify_stateless_cookie_cb = cb;
1386}
1387
a230b26e 1388IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)