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