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