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