]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_sess.c
ecp_nistp256.c: Fix exponent in comment
[thirdparty/openssl.git] / ssl / ssl_sess.c
CommitLineData
846e33c7 1/*
3c95ef22 2 * Copyright 1995-2023 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 */
113 ss->references = 1;
f0131dc0
P
114 /* 5 minute timeout by default */
115 ss->timeout = ossl_seconds2time(60 * 5 + 4);
116 ss->time = ossl_time_now();
25959e04 117 ssl_session_calculate_timeout(ss);
16203f7b
AG
118 ss->lock = CRYPTO_THREAD_lock_new();
119 if (ss->lock == NULL) {
e077455e 120 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
16203f7b
AG
121 OPENSSL_free(ss);
122 return NULL;
123 }
124
25a807bc
F
125 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
126 CRYPTO_THREAD_lock_free(ss->lock);
127 OPENSSL_free(ss);
128 return NULL;
129 }
16203f7b 130 return ss;
0f113f3e 131}
d02b48c6 132
9fdcc21f 133SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
07927bed
MC
134{
135 return ssl_session_dup(src, 1);
136}
137
98ece4ee
MC
138/*
139 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
140 * ticket == 0 then no ticket information is duplicated, otherwise it is.
141 */
9fdcc21f 142SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
98ece4ee
MC
143{
144 SSL_SESSION *dest;
145
8ad36917 146 dest = OPENSSL_malloc(sizeof(*dest));
98ece4ee
MC
147 if (dest == NULL) {
148 goto err;
149 }
150 memcpy(dest, src, sizeof(*dest));
151
708cf593
MC
152 /*
153 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
154 * the case of an error whilst halfway through constructing dest
155 */
156#ifndef OPENSSL_NO_PSK
157 dest->psk_identity_hint = NULL;
158 dest->psk_identity = NULL;
159#endif
aff8c126 160 dest->ext.hostname = NULL;
aff8c126 161 dest->ext.tick = NULL;
eed3ec90 162 dest->ext.alpn_selected = NULL;
708cf593
MC
163#ifndef OPENSSL_NO_SRP
164 dest->srp_username = NULL;
165#endif
1ee21259
TS
166 dest->peer_chain = NULL;
167 dest->peer = NULL;
3c95ef22 168 dest->peer_rpk = NULL;
df0fed9a 169 dest->ticket_appdata = NULL;
51598215 170 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
9b6a8254 171
708cf593
MC
172 /* We deliberately don't copy the prev and next pointers */
173 dest->prev = NULL;
174 dest->next = NULL;
175
176 dest->references = 1;
177
16203f7b 178 dest->lock = CRYPTO_THREAD_lock_new();
e077455e
RL
179 if (dest->lock == NULL) {
180 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
16203f7b 181 goto err;
e077455e 182 }
16203f7b 183
e077455e
RL
184 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) {
185 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
1ee21259 186 goto err;
e077455e 187 }
1ee21259
TS
188
189 if (src->peer != NULL) {
e077455e
RL
190 if (!X509_up_ref(src->peer)) {
191 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
1ee21259 192 goto err;
e077455e 193 }
1ee21259
TS
194 dest->peer = src->peer;
195 }
708cf593 196
36f038f1
DSH
197 if (src->peer_chain != NULL) {
198 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
e077455e
RL
199 if (dest->peer_chain == NULL) {
200 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
36f038f1 201 goto err;
e077455e 202 }
36f038f1 203 }
3c95ef22
TS
204
205 if (src->peer_rpk != NULL) {
206 if (!EVP_PKEY_up_ref(src->peer_rpk))
207 goto err;
208 dest->peer_rpk = src->peer_rpk;
209 }
210
98ece4ee
MC
211#ifndef OPENSSL_NO_PSK
212 if (src->psk_identity_hint) {
7644a9ae 213 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
e077455e 214 if (dest->psk_identity_hint == NULL)
98ece4ee 215 goto err;
98ece4ee
MC
216 }
217 if (src->psk_identity) {
7644a9ae 218 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
e077455e 219 if (dest->psk_identity == NULL)
98ece4ee 220 goto err;
98ece4ee
MC
221 }
222#endif
223
98ece4ee 224 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
a230b26e 225 &dest->ex_data, &src->ex_data)) {
e077455e 226 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
98ece4ee
MC
227 goto err;
228 }
229
aff8c126
RS
230 if (src->ext.hostname) {
231 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
e077455e 232 if (dest->ext.hostname == NULL)
98ece4ee 233 goto err;
98ece4ee 234 }
98ece4ee 235
1ee21259 236 if (ticket != 0 && src->ext.tick != NULL) {
aff8c126
RS
237 dest->ext.tick =
238 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
239 if (dest->ext.tick == NULL)
98ece4ee 240 goto err;
708cf593 241 } else {
aff8c126
RS
242 dest->ext.tick_lifetime_hint = 0;
243 dest->ext.ticklen = 0;
98ece4ee
MC
244 }
245
27232cc3
MC
246 if (src->ext.alpn_selected != NULL) {
247 dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
248 src->ext.alpn_selected_len);
249 if (dest->ext.alpn_selected == NULL)
eed3ec90 250 goto err;
eed3ec90
TS
251 }
252
98ece4ee 253#ifndef OPENSSL_NO_SRP
98ece4ee 254 if (src->srp_username) {
7644a9ae 255 dest->srp_username = OPENSSL_strdup(src->srp_username);
e077455e 256 if (dest->srp_username == NULL)
98ece4ee 257 goto err;
98ece4ee
MC
258 }
259#endif
260
df0fed9a
TS
261 if (src->ticket_appdata != NULL) {
262 dest->ticket_appdata =
263 OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
264 if (dest->ticket_appdata == NULL)
265 goto err;
266 }
267
98ece4ee 268 return dest;
a230b26e 269 err:
98ece4ee
MC
270 SSL_SESSION_free(dest);
271 return NULL;
272}
273
a230b26e 274const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
0f113f3e
MC
275{
276 if (len)
348240c6 277 *len = (unsigned int)s->session_id_length;
0f113f3e
MC
278 return s->session_id;
279}
fddfc0af
RG
280const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
281 unsigned int *len)
282{
283 if (len != NULL)
348240c6 284 *len = (unsigned int)s->sid_ctx_length;
fddfc0af
RG
285 return s->sid_ctx;
286}
4879ec7b 287
f9b0b452 288unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
0f113f3e
MC
289{
290 return s->compress_meth;
291}
292
293/*
294 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
295 * the ID with random junk repeatedly until we have no conflict is going to
296 * complete in one iteration pretty much "most" of the time (btw:
297 * understatement). So, if it takes us 10 iterations and we still can't avoid
298 * a conflict - well that's a reasonable point to call it quits. Either the
299 * RAND code is broken or someone is trying to open roughly very close to
300 * 2^256 SSL sessions to our server. How you might store that many sessions
301 * is perhaps a more interesting question ...
302 */
dc644fe2
GT
303
304#define MAX_SESS_ID_ATTEMPTS 10
ae3947de 305static int def_generate_session_id(SSL *ssl, unsigned char *id,
0f113f3e 306 unsigned int *id_len)
dc644fe2 307{
0f113f3e 308 unsigned int retry = 0;
2b9e2afc 309 do {
0f8815aa 310 if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0)
0f113f3e 311 return 0;
2b9e2afc
PA
312#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
313 if (retry > 0) {
314 id[0]++;
315 }
316#endif
317 } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
0f113f3e
MC
318 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
319 if (retry < MAX_SESS_ID_ATTEMPTS)
320 return 1;
321 /* else - woops a session_id match */
322 /*
323 * XXX We should also check the external cache -- but the probability of
324 * a collision is negligible, and we could not prevent the concurrent
325 * creation of sessions with identical IDs since we currently don't have
326 * means to atomically check whether a session ID already exists and make
327 * a reservation for it if it does not (this problem applies to the
328 * internal cache as well).
329 */
330 return 0;
dc644fe2
GT
331}
332
38b051a1 333int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss)
a84e5c9a
TS
334{
335 unsigned int tmp;
336 GEN_SESSION_CB cb = def_generate_session_id;
38b051a1 337 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
a84e5c9a
TS
338
339 switch (s->version) {
340 case SSL3_VERSION:
341 case TLS1_VERSION:
342 case TLS1_1_VERSION:
343 case TLS1_2_VERSION:
344 case TLS1_3_VERSION:
345 case DTLS1_BAD_VER:
346 case DTLS1_VERSION:
347 case DTLS1_2_VERSION:
348 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
349 break;
350 default:
c48ffbcc 351 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
a84e5c9a
TS
352 return 0;
353 }
354
355 /*-
356 * If RFC5077 ticket, use empty session ID (as server).
357 * Note that:
358 * (a) ssl_get_prev_session() does lookahead into the
359 * ClientHello extensions to find the session ticket.
360 * When ssl_get_prev_session() fails, statem_srvr.c calls
361 * ssl_get_new_session() in tls_process_client_hello().
362 * At that point, it has not yet parsed the extensions,
363 * however, because of the lookahead, it already knows
364 * whether a ticket is expected or not.
365 *
366 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
367 * ServerHello extensions, and before recording the session
368 * ID received from the server, so this block is a noop.
369 */
370 if (s->ext.ticket_expected) {
371 ss->session_id_length = 0;
372 return 1;
373 }
374
375 /* Choose which callback will set the session ID */
38b051a1 376 if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock))
cd3f8c1b
RS
377 return 0;
378 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
38b051a1 379 CRYPTO_THREAD_unlock(ssl->lock);
cd3f8c1b
RS
380 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
381 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
382 return 0;
383 }
a84e5c9a
TS
384 if (s->generate_session_id)
385 cb = s->generate_session_id;
386 else if (s->session_ctx->generate_session_id)
387 cb = s->session_ctx->generate_session_id;
388 CRYPTO_THREAD_unlock(s->session_ctx->lock);
38b051a1 389 CRYPTO_THREAD_unlock(ssl->lock);
a84e5c9a
TS
390 /* Choose a session ID */
391 memset(ss->session_id, 0, ss->session_id_length);
392 tmp = (int)ss->session_id_length;
38b051a1 393 if (!cb(ssl, ss->session_id, &tmp)) {
a84e5c9a 394 /* The callback failed */
c48ffbcc 395 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6 396 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
a84e5c9a
TS
397 return 0;
398 }
399 /*
400 * Don't allow the callback to set the session length to zero. nor
401 * set it higher than it was.
402 */
403 if (tmp == 0 || tmp > ss->session_id_length) {
404 /* The callback set an illegal length */
c48ffbcc 405 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
f63a17d6 406 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
a84e5c9a
TS
407 return 0;
408 }
409 ss->session_id_length = tmp;
410 /* Finally, check for a conflict */
38b051a1 411 if (SSL_has_matching_session_id(ssl, ss->session_id,
a84e5c9a 412 (unsigned int)ss->session_id_length)) {
c48ffbcc 413 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
a84e5c9a
TS
414 return 0;
415 }
416
417 return 1;
418}
419
38b051a1 420int ssl_get_new_session(SSL_CONNECTION *s, int session)
0f113f3e
MC
421{
422 /* This gets used by clients and servers. */
423
0f113f3e 424 SSL_SESSION *ss = NULL;
0f113f3e 425
f63a17d6 426 if ((ss = SSL_SESSION_new()) == NULL) {
e077455e 427 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
a84e5c9a 428 return 0;
f63a17d6 429 }
0f113f3e
MC
430
431 /* If the context has a default timeout, use it */
f0131dc0
P
432 if (ossl_time_is_zero(s->session_ctx->session_timeout))
433 ss->timeout = SSL_CONNECTION_GET_SSL(s)->method->get_timeout();
0f113f3e
MC
434 else
435 ss->timeout = s->session_ctx->session_timeout;
a65c8d8f 436 ssl_session_calculate_timeout(ss);
0f113f3e 437
62adbcee
RS
438 SSL_SESSION_free(s->session);
439 s->session = NULL;
0f113f3e
MC
440
441 if (session) {
38b051a1 442 if (SSL_CONNECTION_IS_TLS13(s)) {
32305f88
MC
443 /*
444 * We generate the session id while constructing the
445 * NewSessionTicket in TLSv1.3.
446 */
447 ss->session_id_length = 0;
448 } else if (!ssl_generate_session_id(s, ss)) {
f63a17d6 449 /* SSLfatal() already called */
0f113f3e 450 SSL_SESSION_free(ss);
a84e5c9a 451 return 0;
0f113f3e 452 }
e481f9b9 453
0f113f3e
MC
454 } else {
455 ss->session_id_length = 0;
456 }
457
cbe29648 458 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
c48ffbcc 459 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
460 SSL_SESSION_free(ss);
461 return 0;
462 }
463 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
464 ss->sid_ctx_length = s->sid_ctx_length;
465 s->session = ss;
466 ss->ssl_version = s->version;
467 ss->verify_result = X509_V_OK;
468
e7f0d921 469 /* If client supports extended master secret set it in session */
555cbb32 470 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
e7f0d921
DSH
471 ss->flags |= SSL_SESS_FLAG_EXTMS;
472
a84e5c9a 473 return 1;
0f113f3e 474}
d02b48c6 475
38b051a1
TM
476SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
477 const unsigned char *sess_id,
6cc0b3c2
MC
478 size_t sess_id_len)
479{
480 SSL_SESSION *ret = NULL;
6cc0b3c2
MC
481
482 if ((s->session_ctx->session_cache_mode
483 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
484 SSL_SESSION data;
485
486 data.ssl_version = s->version;
487 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
488 return NULL;
489
490 memcpy(data.session_id, sess_id, sess_id_len);
491 data.session_id_length = sess_id_len;
492
cd3f8c1b
RS
493 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
494 return NULL;
6cc0b3c2
MC
495 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
496 if (ret != NULL) {
497 /* don't allow other threads to steal it: */
498 SSL_SESSION_up_ref(ret);
499 }
500 CRYPTO_THREAD_unlock(s->session_ctx->lock);
501 if (ret == NULL)
acce0557 502 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
6cc0b3c2
MC
503 }
504
505 if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
506 int copy = 1;
507
38b051a1
TM
508 ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_SSL(s),
509 sess_id, sess_id_len, &copy);
6cc0b3c2
MC
510
511 if (ret != NULL) {
acce0557
P
512 ssl_tsan_counter(s->session_ctx,
513 &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 */
38b051a1 561int ssl_get_prev_session(SSL_CONNECTION *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
38b051a1 570 if (SSL_CONNECTION_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
f0131dc0 643 if (sess_timedout(ossl_time_now(), ret)) {
acce0557 644 ssl_tsan_counter(s->session_ctx, &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
38b051a1 665 if (!SSL_CONNECTION_IS_TLS13(s)) {
1053a6e2
MC
666 /* We already did this for TLS1.3 */
667 SSL_SESSION_free(s->session);
668 s->session = ret;
669 }
0f113f3e 670
acce0557 671 ssl_tsan_counter(s->session_ctx, &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 */
38b051a1 679 if (SSL_CONNECTION_IS_TLS13(s))
128ae276 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) {
f0131dc0 746 c->time = ossl_time_now();
25959e04
TS
747 ssl_session_calculate_timeout(c);
748 }
0f113f3e 749
4842a27b 750 if (s == NULL) {
0f113f3e
MC
751 /*
752 * new cache entry -- remove old ones if cache has become too large
4842a27b 753 * delete cache entry *before* add, so we don't remove the one we're adding!
0f113f3e
MC
754 */
755
756 ret = 1;
757
758 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
4842a27b 759 while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) {
0f113f3e
MC
760 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
761 break;
762 else
acce0557 763 ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full);
0f113f3e
MC
764 }
765 }
766 }
4842a27b
TS
767
768 SSL_SESSION_list_add(ctx, c);
769
770 if (s != NULL) {
771 /*
772 * existing cache entry -- decrement previously incremented reference
773 * count because it already takes into account the cache
774 */
775
776 SSL_SESSION_free(s); /* s == c */
777 ret = 0;
778 }
16203f7b
AG
779 CRYPTO_THREAD_unlock(ctx->lock);
780 return ret;
0f113f3e 781}
d02b48c6 782
6b691a5c 783int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
801294f8 784{
0f113f3e 785 return remove_session_lock(ctx, c, 1);
801294f8
DSH
786}
787
0fda2e37 788static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
0f113f3e
MC
789{
790 SSL_SESSION *r;
791 int ret = 0;
792
793 if ((c != NULL) && (c->session_id_length != 0)) {
cd3f8c1b
RS
794 if (lck) {
795 if (!CRYPTO_THREAD_write_lock(ctx->lock))
796 return 0;
797 }
66d7de16 798 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
0f113f3e 799 ret = 1;
66d7de16
MC
800 r = lh_SSL_SESSION_delete(ctx->sessions, r);
801 SSL_SESSION_list_remove(ctx, r);
0f113f3e 802 }
e4612d02 803 c->not_resumable = 1;
0f113f3e
MC
804
805 if (lck)
16203f7b 806 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e 807
e4612d02
MC
808 if (ctx->remove_session_cb != NULL)
809 ctx->remove_session_cb(ctx, c);
c0a58e03
MH
810
811 if (ret)
812 SSL_SESSION_free(r);
cd3f8c1b 813 }
26a7d938 814 return ret;
0f113f3e 815}
d02b48c6 816
6b691a5c 817void SSL_SESSION_free(SSL_SESSION *ss)
0f113f3e
MC
818{
819 int i;
d02b48c6 820
e6e9170d
RS
821 if (ss == NULL)
822 return;
2f545ae4 823 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
f3f1cf84 824 REF_PRINT_COUNT("SSL_SESSION", ss);
0f113f3e
MC
825 if (i > 0)
826 return;
f3f1cf84 827 REF_ASSERT_ISNT(i < 0);
d02b48c6 828
0f113f3e 829 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
58964a49 830
cbe29648
RS
831 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
832 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
222561fe 833 X509_free(ss->peer);
3c95ef22 834 EVP_PKEY_free(ss->peer_rpk);
79b2a2f2 835 OSSL_STACK_OF_X509_free(ss->peer_chain);
aff8c126
RS
836 OPENSSL_free(ss->ext.hostname);
837 OPENSSL_free(ss->ext.tick);
ddac1974 838#ifndef OPENSSL_NO_PSK
25aaa98a
RS
839 OPENSSL_free(ss->psk_identity_hint);
840 OPENSSL_free(ss->psk_identity);
edc032b5
BL
841#endif
842#ifndef OPENSSL_NO_SRP
25aaa98a 843 OPENSSL_free(ss->srp_username);
ed3883d2 844#endif
f6370040 845 OPENSSL_free(ss->ext.alpn_selected);
df0fed9a 846 OPENSSL_free(ss->ticket_appdata);
16203f7b 847 CRYPTO_THREAD_lock_free(ss->lock);
4b45c6e5 848 OPENSSL_clear_free(ss, sizeof(*ss));
0f113f3e 849}
d02b48c6 850
16203f7b
AG
851int SSL_SESSION_up_ref(SSL_SESSION *ss)
852{
853 int i;
854
2f545ae4 855 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
16203f7b
AG
856 return 0;
857
858 REF_PRINT_COUNT("SSL_SESSION", ss);
859 REF_ASSERT_ISNT(i < 2);
860 return ((i > 1) ? 1 : 0);
861}
862
6b691a5c 863int SSL_set_session(SSL *s, SSL_SESSION *session)
0f113f3e 864{
38b051a1
TM
865 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
866
867 if (sc == NULL)
868 return 0;
869
870 ssl_clear_bad_session(sc);
a7f41885
MC
871 if (s->defltmeth != s->method) {
872 if (!SSL_set_ssl_method(s, s->defltmeth))
e70656cf
MC
873 return 0;
874 }
0f113f3e 875
e70656cf 876 if (session != NULL) {
16203f7b 877 SSL_SESSION_up_ref(session);
38b051a1 878 sc->verify_result = session->verify_result;
0f113f3e 879 }
38b051a1
TM
880 SSL_SESSION_free(sc->session);
881 sc->session = session;
e70656cf
MC
882
883 return 1;
0f113f3e 884}
d02b48c6 885
fddfc0af
RG
886int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
887 unsigned int sid_len)
888{
889 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
6849b73c 890 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
fddfc0af
RG
891 return 0;
892 }
893 s->session_id_length = sid_len;
6aad9393
RG
894 if (sid != s->session_id)
895 memcpy(s->session_id, sid, sid_len);
fddfc0af
RG
896 return 1;
897}
898
6b691a5c 899long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
0f113f3e 900{
f0131dc0 901 OSSL_TIME new_timeout = ossl_seconds2time(t);
25959e04
TS
902
903 if (s == NULL || t < 0)
26a7d938 904 return 0;
25959e04
TS
905 if (s->owner != NULL) {
906 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
907 return 0;
908 s->timeout = new_timeout;
909 ssl_session_calculate_timeout(s);
910 SSL_SESSION_list_add(s->owner, s);
911 CRYPTO_THREAD_unlock(s->owner->lock);
912 } else {
913 s->timeout = new_timeout;
914 ssl_session_calculate_timeout(s);
915 }
208fb891 916 return 1;
0f113f3e 917}
d02b48c6 918
0821bcd4 919long SSL_SESSION_get_timeout(const SSL_SESSION *s)
0f113f3e
MC
920{
921 if (s == NULL)
26a7d938 922 return 0;
f0131dc0 923 return (long)ossl_time_to_time_t(s->timeout);
0f113f3e 924}
d02b48c6 925
0821bcd4 926long SSL_SESSION_get_time(const SSL_SESSION *s)
0f113f3e
MC
927{
928 if (s == NULL)
26a7d938 929 return 0;
f0131dc0 930 return (long)ossl_time_to_time_t(s->time);
0f113f3e 931}
d02b48c6 932
6b691a5c 933long SSL_SESSION_set_time(SSL_SESSION *s, long t)
0f113f3e 934{
f0131dc0 935 OSSL_TIME new_time = ossl_time_from_time_t((time_t)t);
25959e04 936
0f113f3e 937 if (s == NULL)
26a7d938 938 return 0;
25959e04
TS
939 if (s->owner != NULL) {
940 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
941 return 0;
942 s->time = new_time;
943 ssl_session_calculate_timeout(s);
944 SSL_SESSION_list_add(s->owner, s);
945 CRYPTO_THREAD_unlock(s->owner->lock);
946 } else {
947 s->time = new_time;
948 ssl_session_calculate_timeout(s);
949 }
26a7d938 950 return t;
0f113f3e 951}
d02b48c6 952
bd01f649
TS
953int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
954{
bd01f649
TS
955 return s->ssl_version;
956}
957
5a43d511
MC
958int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
959{
960 s->ssl_version = version;
961 return 1;
962}
963
e9281323
RS
964const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
965{
966 return s->cipher;
967}
1a993d1d
MC
968
969int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
970{
971 s->cipher = cipher;
972 return 1;
973}
e9281323 974
4b6b8487
LC
975const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
976{
aff8c126 977 return s->ext.hostname;
4b6b8487
LC
978}
979
67738645
MC
980int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
981{
982 OPENSSL_free(s->ext.hostname);
983 if (hostname == NULL) {
984 s->ext.hostname = NULL;
985 return 1;
986 }
987 s->ext.hostname = OPENSSL_strdup(hostname);
988
989 return s->ext.hostname != NULL;
990}
991
f2baac27
MC
992int SSL_SESSION_has_ticket(const SSL_SESSION *s)
993{
aff8c126 994 return (s->ext.ticklen > 0) ? 1 : 0;
f2baac27
MC
995}
996
997unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
998{
aff8c126 999 return s->ext.tick_lifetime_hint;
f2baac27
MC
1000}
1001
48593cb1 1002void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
a230b26e 1003 size_t *len)
b7c9187b 1004{
aff8c126 1005 *len = s->ext.ticklen;
61986d32 1006 if (tick != NULL)
aff8c126 1007 *tick = s->ext.tick;
b7c9187b
MC
1008}
1009
fcc47578
MC
1010uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
1011{
1012 return s->ext.max_early_data;
1013}
1014
98e1d934
MC
1015int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
1016{
1017 s->ext.max_early_data = max_early_data;
1018
1019 return 1;
1020}
1021
67738645
MC
1022void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
1023 const unsigned char **alpn,
1024 size_t *len)
1025{
1026 *alpn = s->ext.alpn_selected;
1027 *len = s->ext.alpn_selected_len;
1028}
1029
1030int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
1031 size_t len)
1032{
1033 OPENSSL_free(s->ext.alpn_selected);
1034 if (alpn == NULL || len == 0) {
1035 s->ext.alpn_selected = NULL;
1036 s->ext.alpn_selected_len = 0;
1037 return 1;
1038 }
1039 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
1040 if (s->ext.alpn_selected == NULL) {
1041 s->ext.alpn_selected_len = 0;
1042 return 0;
1043 }
1044 s->ext.alpn_selected_len = len;
1045
1046 return 1;
1047}
1048
08557cf2 1049X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
0f113f3e
MC
1050{
1051 return s->peer;
1052}
1053
3c95ef22
TS
1054EVP_PKEY *SSL_SESSION_get0_peer_rpk(SSL_SESSION *s)
1055{
1056 return s->peer_rpk;
1057}
1058
0f113f3e
MC
1059int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1060 unsigned int sid_ctx_len)
1061{
1062 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
6849b73c 1063 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
0f113f3e
MC
1064 return 0;
1065 }
1066 s->sid_ctx_length = sid_ctx_len;
6aad9393
RG
1067 if (sid_ctx != s->sid_ctx)
1068 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
0f113f3e
MC
1069
1070 return 1;
1071}
08557cf2 1072
e586eac8
MC
1073int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1074{
1075 /*
1076 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1077 * session ID.
1078 */
1079 return !s->not_resumable
1080 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1081}
1082
6b691a5c 1083long SSL_CTX_set_timeout(SSL_CTX *s, long t)
0f113f3e
MC
1084{
1085 long l;
f0131dc0 1086
0f113f3e 1087 if (s == NULL)
26a7d938 1088 return 0;
f0131dc0
P
1089 l = (long)ossl_time2seconds(s->session_timeout);
1090 s->session_timeout = ossl_seconds2time(t);
26a7d938 1091 return l;
0f113f3e 1092}
413c4f45 1093
0821bcd4 1094long SSL_CTX_get_timeout(const SSL_CTX *s)
0f113f3e
MC
1095{
1096 if (s == NULL)
26a7d938 1097 return 0;
f0131dc0 1098 return (long)ossl_time2seconds(s->session_timeout);
0f113f3e 1099}
413c4f45 1100
0f113f3e 1101int SSL_set_session_secret_cb(SSL *s,
aff8c126 1102 tls_session_secret_cb_fn tls_session_secret_cb,
0f113f3e
MC
1103 void *arg)
1104{
38b051a1
TM
1105 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1106
1107 if (sc == NULL)
26a7d938 1108 return 0;
38b051a1
TM
1109
1110 sc->ext.session_secret_cb = tls_session_secret_cb;
1111 sc->ext.session_secret_cb_arg = arg;
208fb891 1112 return 1;
0f113f3e 1113}
12bf56c0
DSH
1114
1115int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
0f113f3e
MC
1116 void *arg)
1117{
38b051a1
TM
1118 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1119
1120 if (sc == NULL)
26a7d938 1121 return 0;
38b051a1
TM
1122
1123 sc->ext.session_ticket_cb = cb;
1124 sc->ext.session_ticket_cb_arg = arg;
208fb891 1125 return 1;
0f113f3e 1126}
12bf56c0
DSH
1127
1128int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
0f113f3e 1129{
38b051a1
TM
1130 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1131
1132 if (sc == NULL)
1133 return 0;
1134
1135 if (sc->version >= TLS1_VERSION) {
1136 OPENSSL_free(sc->ext.session_ticket);
1137 sc->ext.session_ticket = NULL;
1138 sc->ext.session_ticket =
0f113f3e 1139 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
e077455e 1140 if (sc->ext.session_ticket == NULL)
0f113f3e 1141 return 0;
0f113f3e 1142
1ed327f7 1143 if (ext_data != NULL) {
38b051a1
TM
1144 sc->ext.session_ticket->length = ext_len;
1145 sc->ext.session_ticket->data = sc->ext.session_ticket + 1;
1146 memcpy(sc->ext.session_ticket->data, ext_data, ext_len);
0f113f3e 1147 } else {
38b051a1
TM
1148 sc->ext.session_ticket->length = 0;
1149 sc->ext.session_ticket->data = NULL;
0f113f3e
MC
1150 }
1151
1152 return 1;
1153 }
1154
1155 return 0;
1156}
0f113f3e 1157
6b691a5c 1158void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
0f113f3e 1159{
25959e04
TS
1160 STACK_OF(SSL_SESSION) *sk;
1161 SSL_SESSION *current;
0f113f3e 1162 unsigned long i;
f0131dc0 1163 const OSSL_TIME timeout = ossl_time_from_time_t(t);
0f113f3e 1164
cd3f8c1b
RS
1165 if (!CRYPTO_THREAD_write_lock(s->lock))
1166 return;
25959e04
TS
1167
1168 sk = sk_SSL_SESSION_new_null();
739a1eb1
RS
1169 i = lh_SSL_SESSION_get_down_load(s->sessions);
1170 lh_SSL_SESSION_set_down_load(s->sessions, 0);
25959e04
TS
1171
1172 /*
1173 * Iterate over the list from the back (oldest), and stop
1174 * when a session can no longer be removed.
1175 * Add the session to a temporary list to be freed outside
1176 * the SSL_CTX lock.
1177 * But still do the remove_session_cb() within the lock.
1178 */
1179 while (s->session_cache_tail != NULL) {
1180 current = s->session_cache_tail;
f0131dc0 1181 if (t == 0 || sess_timedout(timeout, current)) {
25959e04
TS
1182 lh_SSL_SESSION_delete(s->sessions, current);
1183 SSL_SESSION_list_remove(s, current);
1184 current->not_resumable = 1;
1185 if (s->remove_session_cb != NULL)
1186 s->remove_session_cb(s, current);
1187 /*
1188 * Throw the session on a stack, it's entirely plausible
1189 * that while freeing outside the critical section, the
1190 * session could be re-added, so avoid using the next/prev
1191 * pointers. If the stack failed to create, or the session
1192 * couldn't be put on the stack, just free it here
1193 */
1194 if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
1195 SSL_SESSION_free(current);
1196 } else {
1197 break;
1198 }
1199 }
1200
739a1eb1 1201 lh_SSL_SESSION_set_down_load(s->sessions, i);
16203f7b 1202 CRYPTO_THREAD_unlock(s->lock);
25959e04
TS
1203
1204 sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
0f113f3e 1205}
d02b48c6 1206
38b051a1 1207int ssl_clear_bad_session(SSL_CONNECTION *s)
0f113f3e
MC
1208{
1209 if ((s->session != NULL) &&
1210 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
38b051a1
TM
1211 !(SSL_in_init(SSL_CONNECTION_GET_SSL(s))
1212 || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) {
e2bb9b9b 1213 SSL_CTX_remove_session(s->session_ctx, s->session);
208fb891 1214 return 1;
0f113f3e 1215 } else
26a7d938 1216 return 0;
0f113f3e 1217}
58964a49
RE
1218
1219/* locked by SSL_CTX in the calling function */
6b691a5c 1220static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e
MC
1221{
1222 if ((s->next == NULL) || (s->prev == NULL))
1223 return;
1224
1225 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1226 /* last element in list */
1227 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1228 /* only one element in list */
1229 ctx->session_cache_head = NULL;
1230 ctx->session_cache_tail = NULL;
1231 } else {
1232 ctx->session_cache_tail = s->prev;
1233 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1234 }
1235 } else {
1236 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1237 /* first element in list */
1238 ctx->session_cache_head = s->next;
1239 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1240 } else {
1241 /* middle of list */
1242 s->next->prev = s->prev;
1243 s->prev->next = s->next;
1244 }
1245 }
1246 s->prev = s->next = NULL;
25959e04 1247 s->owner = NULL;
0f113f3e 1248}
58964a49 1249
6b691a5c 1250static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
0f113f3e 1251{
25959e04
TS
1252 SSL_SESSION *next;
1253
0f113f3e
MC
1254 if ((s->next != NULL) && (s->prev != NULL))
1255 SSL_SESSION_list_remove(ctx, s);
1256
1257 if (ctx->session_cache_head == NULL) {
1258 ctx->session_cache_head = s;
1259 ctx->session_cache_tail = s;
1260 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1261 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1262 } else {
25959e04
TS
1263 if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
1264 /*
1265 * if we timeout after (or the same time as) the first
1266 * session, put us first - usual case
1267 */
1268 s->next = ctx->session_cache_head;
1269 s->next->prev = s;
1270 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1271 ctx->session_cache_head = s;
1272 } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
1273 /* if we timeout before the last session, put us last */
1274 s->prev = ctx->session_cache_tail;
1275 s->prev->next = s;
1276 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1277 ctx->session_cache_tail = s;
1278 } else {
1279 /*
1280 * we timeout somewhere in-between - if there is only
1281 * one session in the cache it will be caught above
1282 */
1283 next = ctx->session_cache_head->next;
1284 while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) {
1285 if (timeoutcmp(s, next) >= 0) {
1286 s->next = next;
1287 s->prev = next->prev;
1288 next->prev->next = s;
1289 next->prev = s;
1290 break;
1291 }
1292 next = next->next;
1293 }
1294 }
0f113f3e 1295 }
25959e04 1296 s->owner = ctx;
0f113f3e 1297}
58964a49 1298
7806f3dd 1299void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
a230b26e 1300 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
0f113f3e
MC
1301{
1302 ctx->new_session_cb = cb;
1303}
7806f3dd 1304
0f113f3e
MC
1305int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1306 return ctx->new_session_cb;
1307}
7806f3dd
NL
1308
1309void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
0f113f3e
MC
1310 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1311{
1312 ctx->remove_session_cb = cb;
1313}
7806f3dd 1314
0f113f3e
MC
1315void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1316 SSL_SESSION *sess) {
1317 return ctx->remove_session_cb;
1318}
7806f3dd
NL
1319
1320void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
38b051a1 1321 SSL_SESSION *(*cb) (SSL *ssl,
b6981744
EK
1322 const unsigned char *data,
1323 int len, int *copy))
0f113f3e
MC
1324{
1325 ctx->get_session_cb = cb;
1326}
1327
1328SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
a230b26e
EK
1329 const unsigned char
1330 *data, int len,
1331 int *copy) {
0f113f3e
MC
1332 return ctx->get_session_cb;
1333}
1334
1335void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1336 void (*cb) (const SSL *ssl, int type, int val))
1337{
1338 ctx->info_callback = cb;
1339}
1340
1341void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1342 int val) {
1343 return ctx->info_callback;
1344}
7806f3dd
NL
1345
1346void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
0f113f3e
MC
1347 int (*cb) (SSL *ssl, X509 **x509,
1348 EVP_PKEY **pkey))
1349{
1350 ctx->client_cert_cb = cb;
1351}
7806f3dd 1352
0f113f3e
MC
1353int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1354 EVP_PKEY **pkey) {
1355 return ctx->client_cert_cb;
1356}
7806f3dd
NL
1357
1358void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
0f113f3e
MC
1359 int (*cb) (SSL *ssl,
1360 unsigned char *cookie,
1361 unsigned int *cookie_len))
1362{
1363 ctx->app_gen_cookie_cb = cb;
1364}
7806f3dd
NL
1365
1366void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
a230b26e
EK
1367 int (*cb) (SSL *ssl,
1368 const unsigned char *cookie,
0f113f3e
MC
1369 unsigned int cookie_len))
1370{
1371 ctx->app_verify_cookie_cb = cb;
1372}
7806f3dd 1373
df0fed9a
TS
1374int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1375{
1376 OPENSSL_free(ss->ticket_appdata);
1377 ss->ticket_appdata_len = 0;
1378 if (data == NULL || len == 0) {
1379 ss->ticket_appdata = NULL;
1380 return 1;
1381 }
1382 ss->ticket_appdata = OPENSSL_memdup(data, len);
1383 if (ss->ticket_appdata != NULL) {
1384 ss->ticket_appdata_len = len;
1385 return 1;
1386 }
1387 return 0;
1388}
1389
1390int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1391{
1392 *data = ss->ticket_appdata;
1393 *len = ss->ticket_appdata_len;
1394 return 1;
1395}
1396
3fa2812f
BS
1397void SSL_CTX_set_stateless_cookie_generate_cb(
1398 SSL_CTX *ctx,
1399 int (*cb) (SSL *ssl,
1400 unsigned char *cookie,
1401 size_t *cookie_len))
1402{
1403 ctx->gen_stateless_cookie_cb = cb;
1404}
1405
1406void SSL_CTX_set_stateless_cookie_verify_cb(
1407 SSL_CTX *ctx,
1408 int (*cb) (SSL *ssl,
1409 const unsigned char *cookie,
1410 size_t cookie_len))
1411{
1412 ctx->verify_stateless_cookie_cb = cb;
1413}
1414
a230b26e 1415IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)