2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
11 #if defined(__TANDEM) && defined(_SPT_MODEL_)
12 # include <spthread.h>
13 # include <spt_extensions.h> /* timeval */
16 #include <openssl/rand.h>
17 #include <openssl/engine.h>
18 #include "internal/refcount.h"
19 #include "internal/cryptlib.h"
20 #include "ssl_local.h"
21 #include "statem/statem_local.h"
23 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
);
24 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
);
25 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
);
27 DEFINE_STACK_OF(SSL_SESSION
)
29 __owur
static ossl_inline
int sess_timedout(OSSL_TIME t
, SSL_SESSION
*ss
)
31 return ossl_time_compare(t
, ss
->calc_timeout
) > 0;
35 * Returns -1/0/+1 as other XXXcmp-type functions
36 * Takes calculated timeout into consideration
38 __owur
static ossl_inline
int timeoutcmp(SSL_SESSION
*a
, SSL_SESSION
*b
)
40 return ossl_time_compare(a
->calc_timeout
, b
->calc_timeout
);
44 * Calculates effective timeout
45 * Locking must be done by the caller of this function
47 void ssl_session_calculate_timeout(SSL_SESSION
*ss
)
49 ss
->calc_timeout
= ossl_time_add(ss
->time
, ss
->timeout
);
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().
61 SSL_SESSION
*SSL_get_session(const SSL
*ssl
)
62 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
64 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
72 SSL_SESSION
*SSL_get1_session(SSL
*ssl
)
73 /* variant of SSL_get_session: caller really gets something */
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.
82 if (!CRYPTO_THREAD_read_lock(ssl
->lock
))
84 sess
= SSL_get_session(ssl
);
86 SSL_SESSION_up_ref(sess
);
87 CRYPTO_THREAD_unlock(ssl
->lock
);
91 int SSL_SESSION_set_ex_data(SSL_SESSION
*s
, int idx
, void *arg
)
93 return CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
);
96 void *SSL_SESSION_get_ex_data(const SSL_SESSION
*s
, int idx
)
98 return CRYPTO_get_ex_data(&s
->ex_data
, idx
);
101 SSL_SESSION
*SSL_SESSION_new(void)
105 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
))
108 ss
= OPENSSL_zalloc(sizeof(*ss
));
112 ss
->verify_result
= 1; /* avoid 0 (= X509_V_OK) just in case */
113 /* 5 minute timeout by default */
114 ss
->timeout
= ossl_seconds2time(60 * 5 + 4);
115 ss
->time
= ossl_time_now();
116 ssl_session_calculate_timeout(ss
);
117 if (!CRYPTO_NEW_REF(&ss
->references
, 1)) {
122 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
)) {
123 CRYPTO_FREE_REF(&ss
->references
);
130 SSL_SESSION
*SSL_SESSION_dup(const SSL_SESSION
*src
)
132 return ssl_session_dup(src
, 1);
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.
139 SSL_SESSION
*ssl_session_dup(const SSL_SESSION
*src
, int ticket
)
143 dest
= OPENSSL_malloc(sizeof(*dest
));
146 memcpy(dest
, src
, sizeof(*dest
));
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
152 #ifndef OPENSSL_NO_PSK
153 dest
->psk_identity_hint
= NULL
;
154 dest
->psk_identity
= NULL
;
156 dest
->ext
.hostname
= NULL
;
157 dest
->ext
.tick
= NULL
;
158 dest
->ext
.alpn_selected
= NULL
;
159 #ifndef OPENSSL_NO_SRP
160 dest
->srp_username
= NULL
;
162 dest
->peer_chain
= NULL
;
164 dest
->peer_rpk
= NULL
;
165 dest
->ticket_appdata
= NULL
;
166 memset(&dest
->ex_data
, 0, sizeof(dest
->ex_data
));
168 /* As the copy is not in the cache, we remove the associated pointers */
173 if (!CRYPTO_NEW_REF(&dest
->references
, 1)) {
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
);
183 if (src
->peer
!= NULL
) {
184 if (!X509_up_ref(src
->peer
)) {
185 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
188 dest
->peer
= src
->peer
;
191 if (src
->peer_chain
!= NULL
) {
192 dest
->peer_chain
= X509_chain_up_ref(src
->peer_chain
);
193 if (dest
->peer_chain
== NULL
) {
194 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
199 if (src
->peer_rpk
!= NULL
) {
200 if (!EVP_PKEY_up_ref(src
->peer_rpk
))
202 dest
->peer_rpk
= src
->peer_rpk
;
205 #ifndef OPENSSL_NO_PSK
206 if (src
->psk_identity_hint
) {
207 dest
->psk_identity_hint
= OPENSSL_strdup(src
->psk_identity_hint
);
208 if (dest
->psk_identity_hint
== NULL
)
211 if (src
->psk_identity
) {
212 dest
->psk_identity
= OPENSSL_strdup(src
->psk_identity
);
213 if (dest
->psk_identity
== NULL
)
218 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
,
219 &dest
->ex_data
, &src
->ex_data
)) {
220 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
224 if (src
->ext
.hostname
) {
225 dest
->ext
.hostname
= OPENSSL_strdup(src
->ext
.hostname
);
226 if (dest
->ext
.hostname
== NULL
)
230 if (ticket
!= 0 && src
->ext
.tick
!= NULL
) {
232 OPENSSL_memdup(src
->ext
.tick
, src
->ext
.ticklen
);
233 if (dest
->ext
.tick
== NULL
)
236 dest
->ext
.tick_lifetime_hint
= 0;
237 dest
->ext
.ticklen
= 0;
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
)
247 #ifndef OPENSSL_NO_SRP
248 if (src
->srp_username
) {
249 dest
->srp_username
= OPENSSL_strdup(src
->srp_username
);
250 if (dest
->srp_username
== NULL
)
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
)
264 SSL_SESSION_free(dest
);
268 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION
*s
, unsigned int *len
)
271 *len
= (unsigned int)s
->session_id_length
;
272 return s
->session_id
;
274 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION
*s
,
278 *len
= (unsigned int)s
->sid_ctx_length
;
282 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION
*s
)
284 return s
->compress_meth
;
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 ...
298 #define MAX_SESS_ID_ATTEMPTS 10
299 static int def_generate_session_id(SSL
*ssl
, unsigned char *id
,
300 unsigned int *id_len
)
302 unsigned int retry
= 0;
304 if (RAND_bytes_ex(ssl
->ctx
->libctx
, id
, *id_len
, 0) <= 0)
306 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
311 } while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
312 (++retry
< MAX_SESS_ID_ATTEMPTS
)) ;
313 if (retry
< MAX_SESS_ID_ATTEMPTS
)
315 /* else - woops a session_id match */
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).
327 int ssl_generate_session_id(SSL_CONNECTION
*s
, SSL_SESSION
*ss
)
330 GEN_SESSION_CB cb
= def_generate_session_id
;
331 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
333 switch (s
->version
) {
341 case DTLS1_2_VERSION
:
342 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
345 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_UNSUPPORTED_SSL_VERSION
);
350 * If RFC5077 ticket, use empty session ID (as server).
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.
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.
364 if (s
->ext
.ticket_expected
) {
365 ss
->session_id_length
= 0;
369 /* Choose which callback will set the session ID */
370 if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s
)->lock
))
372 if (!CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
)) {
373 CRYPTO_THREAD_unlock(ssl
->lock
);
374 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
375 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED
);
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
);
383 CRYPTO_THREAD_unlock(ssl
->lock
);
384 /* Choose a session ID */
385 memset(ss
->session_id
, 0, ss
->session_id_length
);
386 tmp
= (int)ss
->session_id_length
;
387 if (!cb(ssl
, ss
->session_id
, &tmp
)) {
388 /* The callback failed */
389 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
390 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED
);
394 * Don't allow the callback to set the session length to zero. nor
395 * set it higher than it was.
397 if (tmp
== 0 || tmp
> ss
->session_id_length
) {
398 /* The callback set an illegal length */
399 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
400 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH
);
403 ss
->session_id_length
= tmp
;
404 /* Finally, check for a conflict */
405 if (SSL_has_matching_session_id(ssl
, ss
->session_id
,
406 (unsigned int)ss
->session_id_length
)) {
407 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_SSL_SESSION_ID_CONFLICT
);
414 int ssl_get_new_session(SSL_CONNECTION
*s
, int session
)
416 /* This gets used by clients and servers. */
418 SSL_SESSION
*ss
= NULL
;
420 if ((ss
= SSL_SESSION_new()) == NULL
) {
421 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_SSL_LIB
);
425 /* If the context has a default timeout, use it */
426 if (ossl_time_is_zero(s
->session_ctx
->session_timeout
))
427 ss
->timeout
= SSL_CONNECTION_GET_SSL(s
)->method
->get_timeout();
429 ss
->timeout
= s
->session_ctx
->session_timeout
;
430 ssl_session_calculate_timeout(ss
);
432 SSL_SESSION_free(s
->session
);
436 if (SSL_CONNECTION_IS_TLS13(s
)) {
438 * We generate the session id while constructing the
439 * NewSessionTicket in TLSv1.3.
441 ss
->session_id_length
= 0;
442 } else if (!ssl_generate_session_id(s
, ss
)) {
443 /* SSLfatal() already called */
444 SSL_SESSION_free(ss
);
449 ss
->session_id_length
= 0;
452 if (s
->sid_ctx_length
> sizeof(ss
->sid_ctx
)) {
453 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
454 SSL_SESSION_free(ss
);
457 memcpy(ss
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
);
458 ss
->sid_ctx_length
= s
->sid_ctx_length
;
460 ss
->ssl_version
= s
->version
;
461 ss
->verify_result
= X509_V_OK
;
463 /* If client supports extended master secret set it in session */
464 if (s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
)
465 ss
->flags
|= SSL_SESS_FLAG_EXTMS
;
470 SSL_SESSION
*lookup_sess_in_cache(SSL_CONNECTION
*s
,
471 const unsigned char *sess_id
,
474 SSL_SESSION
*ret
= NULL
;
476 if ((s
->session_ctx
->session_cache_mode
477 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP
) == 0) {
480 data
.ssl_version
= s
->version
;
481 if (!ossl_assert(sess_id_len
<= SSL_MAX_SSL_SESSION_ID_LENGTH
))
484 memcpy(data
.session_id
, sess_id
, sess_id_len
);
485 data
.session_id_length
= sess_id_len
;
487 if (!CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
))
489 ret
= lh_SSL_SESSION_retrieve(s
->session_ctx
->sessions
, &data
);
491 /* don't allow other threads to steal it: */
492 SSL_SESSION_up_ref(ret
);
494 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
496 ssl_tsan_counter(s
->session_ctx
, &s
->session_ctx
->stats
.sess_miss
);
499 if (ret
== NULL
&& s
->session_ctx
->get_session_cb
!= NULL
) {
502 ret
= s
->session_ctx
->get_session_cb(SSL_CONNECTION_GET_SSL(s
),
503 sess_id
, sess_id_len
, ©
);
506 ssl_tsan_counter(s
->session_ctx
,
507 &s
->session_ctx
->stats
.sess_cb_hit
);
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
517 SSL_SESSION_up_ref(ret
);
520 * Add the externally cached session to the internal cache as
521 * well if and only if we are supposed to.
523 if ((s
->session_ctx
->session_cache_mode
&
524 SSL_SESS_CACHE_NO_INTERNAL_STORE
) == 0) {
526 * Either return value of SSL_CTX_add_session should not
527 * interrupt the session resumption process. The return
528 * value is intentionally ignored.
530 (void)SSL_CTX_add_session(s
->session_ctx
, ret
);
539 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
540 * connection. It is only called by servers.
542 * hello: The parsed ClientHello data
546 * 0: no session found
547 * 1: a session may have been found.
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.
552 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
553 * if the server should issue a new session ticket (to 0 otherwise).
555 int ssl_get_prev_session(SSL_CONNECTION
*s
, CLIENTHELLO_MSG
*hello
)
557 /* This is used only by servers. */
559 SSL_SESSION
*ret
= NULL
;
561 int try_session_cache
= 0;
564 if (SSL_CONNECTION_IS_TLS13(s
)) {
566 * By default we will send a new ticket. This can be overridden in the
569 s
->ext
.ticket_expected
= 1;
570 if (!tls_parse_extension(s
, TLSEXT_IDX_psk_kex_modes
,
571 SSL_EXT_CLIENT_HELLO
, hello
->pre_proc_exts
,
573 || !tls_parse_extension(s
, TLSEXT_IDX_psk
, SSL_EXT_CLIENT_HELLO
,
574 hello
->pre_proc_exts
, NULL
, 0))
579 /* sets s->ext.ticket_expected */
580 r
= tls_get_ticket_from_client(s
, hello
, &ret
);
582 case SSL_TICKET_FATAL_ERR_MALLOC
:
583 case SSL_TICKET_FATAL_ERR_OTHER
:
585 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
587 case SSL_TICKET_NONE
:
588 case SSL_TICKET_EMPTY
:
589 if (hello
->session_id_len
> 0) {
590 try_session_cache
= 1;
591 ret
= lookup_sess_in_cache(s
, hello
->session_id
,
592 hello
->session_id_len
);
595 case SSL_TICKET_NO_DECRYPT
:
596 case SSL_TICKET_SUCCESS
:
597 case SSL_TICKET_SUCCESS_RENEW
:
605 /* Now ret is non-NULL and we own one of its reference counts. */
607 /* Check TLS version consistency */
608 if (ret
->ssl_version
!= s
->version
)
611 if (ret
->sid_ctx_length
!= s
->sid_ctx_length
612 || memcmp(ret
->sid_ctx
, s
->sid_ctx
, ret
->sid_ctx_length
)) {
614 * We have the session requested by the client, but we don't want to
615 * use it in this context.
617 goto err
; /* treat like cache miss */
620 if ((s
->verify_mode
& SSL_VERIFY_PEER
) && s
->sid_ctx_length
== 0) {
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
631 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
632 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED
);
637 if (sess_timedout(ossl_time_now(), ret
)) {
638 ssl_tsan_counter(s
->session_ctx
, &s
->session_ctx
->stats
.sess_timeout
);
639 if (try_session_cache
) {
640 /* session was from the cache, so remove it */
641 SSL_CTX_remove_session(s
->session_ctx
, ret
);
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 */
649 if (!(s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
)) {
650 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_INCONSISTENT_EXTMS
);
654 } else if (s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
) {
655 /* If new session includes extms, but old does not: do not resume */
659 if (!SSL_CONNECTION_IS_TLS13(s
)) {
660 /* We already did this for TLS1.3 */
661 SSL_SESSION_free(s
->session
);
665 ssl_tsan_counter(s
->session_ctx
, &s
->session_ctx
->stats
.sess_hit
);
666 s
->verify_result
= s
->session
->verify_result
;
671 SSL_SESSION_free(ret
);
672 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
673 if (SSL_CONNECTION_IS_TLS13(s
))
676 if (!try_session_cache
) {
678 * The session was from a ticket, so we should issue a ticket for
681 s
->ext
.ticket_expected
= 1;
690 int SSL_CTX_add_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
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
700 SSL_SESSION_up_ref(c
);
702 * if session c is in already in cache, we take back the increment later
705 if (!CRYPTO_THREAD_write_lock(ctx
->lock
)) {
709 s
= lh_SSL_SESSION_insert(ctx
->sessions
, c
);
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.
716 if (s
!= NULL
&& s
!= c
) {
717 /* We *are* in trouble ... */
718 SSL_SESSION_list_remove(ctx
, s
);
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)
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 ... */
732 * ... so take back the extra reference and also don't add
733 * the session to the SSL_SESSION_list at this time
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
) {
740 c
->time
= ossl_time_now();
741 ssl_session_calculate_timeout(c
);
746 * new cache entry -- remove old ones if cache has become too large
747 * delete cache entry *before* add, so we don't remove the one we're adding!
752 if (SSL_CTX_sess_get_cache_size(ctx
) > 0) {
753 while (SSL_CTX_sess_number(ctx
) >= SSL_CTX_sess_get_cache_size(ctx
)) {
754 if (!remove_session_lock(ctx
, ctx
->session_cache_tail
, 0))
757 ssl_tsan_counter(ctx
, &ctx
->stats
.sess_cache_full
);
762 SSL_SESSION_list_add(ctx
, c
);
766 * existing cache entry -- decrement previously incremented reference
767 * count because it already takes into account the cache
770 SSL_SESSION_free(s
); /* s == c */
773 CRYPTO_THREAD_unlock(ctx
->lock
);
777 int SSL_CTX_remove_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
779 return remove_session_lock(ctx
, c
, 1);
782 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
)
787 if ((c
!= NULL
) && (c
->session_id_length
!= 0)) {
789 if (!CRYPTO_THREAD_write_lock(ctx
->lock
))
792 if ((r
= lh_SSL_SESSION_retrieve(ctx
->sessions
, c
)) != NULL
) {
794 r
= lh_SSL_SESSION_delete(ctx
->sessions
, r
);
795 SSL_SESSION_list_remove(ctx
, r
);
797 c
->not_resumable
= 1;
800 CRYPTO_THREAD_unlock(ctx
->lock
);
802 if (ctx
->remove_session_cb
!= NULL
)
803 ctx
->remove_session_cb(ctx
, c
);
811 void SSL_SESSION_free(SSL_SESSION
*ss
)
817 CRYPTO_DOWN_REF(&ss
->references
, &i
);
818 REF_PRINT_COUNT("SSL_SESSION", ss
);
821 REF_ASSERT_ISNT(i
< 0);
823 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
);
825 OPENSSL_cleanse(ss
->master_key
, sizeof(ss
->master_key
));
826 OPENSSL_cleanse(ss
->session_id
, sizeof(ss
->session_id
));
828 EVP_PKEY_free(ss
->peer_rpk
);
829 OSSL_STACK_OF_X509_free(ss
->peer_chain
);
830 OPENSSL_free(ss
->ext
.hostname
);
831 OPENSSL_free(ss
->ext
.tick
);
832 #ifndef OPENSSL_NO_PSK
833 OPENSSL_free(ss
->psk_identity_hint
);
834 OPENSSL_free(ss
->psk_identity
);
836 #ifndef OPENSSL_NO_SRP
837 OPENSSL_free(ss
->srp_username
);
839 OPENSSL_free(ss
->ext
.alpn_selected
);
840 OPENSSL_free(ss
->ticket_appdata
);
841 CRYPTO_FREE_REF(&ss
->references
);
842 OPENSSL_clear_free(ss
, sizeof(*ss
));
845 int SSL_SESSION_up_ref(SSL_SESSION
*ss
)
849 if (CRYPTO_UP_REF(&ss
->references
, &i
) <= 0)
852 REF_PRINT_COUNT("SSL_SESSION", ss
);
853 REF_ASSERT_ISNT(i
< 2);
854 return ((i
> 1) ? 1 : 0);
857 int SSL_set_session(SSL
*s
, SSL_SESSION
*session
)
859 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
864 ssl_clear_bad_session(sc
);
865 if (s
->defltmeth
!= s
->method
) {
866 if (!SSL_set_ssl_method(s
, s
->defltmeth
))
870 if (session
!= NULL
) {
871 SSL_SESSION_up_ref(session
);
872 sc
->verify_result
= session
->verify_result
;
874 SSL_SESSION_free(sc
->session
);
875 sc
->session
= session
;
880 int SSL_SESSION_set1_id(SSL_SESSION
*s
, const unsigned char *sid
,
881 unsigned int sid_len
)
883 if (sid_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
884 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_TOO_LONG
);
887 s
->session_id_length
= sid_len
;
888 if (sid
!= s
->session_id
)
889 memcpy(s
->session_id
, sid
, sid_len
);
893 long SSL_SESSION_set_timeout(SSL_SESSION
*s
, long t
)
895 OSSL_TIME new_timeout
= ossl_seconds2time(t
);
897 if (s
== NULL
|| t
< 0)
899 if (s
->owner
!= NULL
) {
900 if (!CRYPTO_THREAD_write_lock(s
->owner
->lock
))
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
);
907 s
->timeout
= new_timeout
;
908 ssl_session_calculate_timeout(s
);
913 long SSL_SESSION_get_timeout(const SSL_SESSION
*s
)
917 return (long)ossl_time_to_time_t(s
->timeout
);
920 long SSL_SESSION_get_time(const SSL_SESSION
*s
)
922 return (long) SSL_SESSION_get_time_ex(s
);
925 time_t SSL_SESSION_get_time_ex(const SSL_SESSION
*s
)
929 return ossl_time_to_time_t(s
->time
);
932 time_t SSL_SESSION_set_time_ex(SSL_SESSION
*s
, time_t t
)
934 OSSL_TIME new_time
= ossl_time_from_time_t(t
);
938 if (s
->owner
!= NULL
) {
939 if (!CRYPTO_THREAD_write_lock(s
->owner
->lock
))
942 ssl_session_calculate_timeout(s
);
943 SSL_SESSION_list_add(s
->owner
, s
);
944 CRYPTO_THREAD_unlock(s
->owner
->lock
);
947 ssl_session_calculate_timeout(s
);
952 long SSL_SESSION_set_time(SSL_SESSION
*s
, long t
)
954 return (long) SSL_SESSION_set_time_ex(s
, (time_t) t
);
957 int SSL_SESSION_get_protocol_version(const SSL_SESSION
*s
)
959 return s
->ssl_version
;
962 int SSL_SESSION_set_protocol_version(SSL_SESSION
*s
, int version
)
964 s
->ssl_version
= version
;
968 const SSL_CIPHER
*SSL_SESSION_get0_cipher(const SSL_SESSION
*s
)
973 int SSL_SESSION_set_cipher(SSL_SESSION
*s
, const SSL_CIPHER
*cipher
)
979 const char *SSL_SESSION_get0_hostname(const SSL_SESSION
*s
)
981 return s
->ext
.hostname
;
984 int SSL_SESSION_set1_hostname(SSL_SESSION
*s
, const char *hostname
)
986 OPENSSL_free(s
->ext
.hostname
);
987 if (hostname
== NULL
) {
988 s
->ext
.hostname
= NULL
;
991 s
->ext
.hostname
= OPENSSL_strdup(hostname
);
993 return s
->ext
.hostname
!= NULL
;
996 int SSL_SESSION_has_ticket(const SSL_SESSION
*s
)
998 return (s
->ext
.ticklen
> 0) ? 1 : 0;
1001 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION
*s
)
1003 return s
->ext
.tick_lifetime_hint
;
1006 void SSL_SESSION_get0_ticket(const SSL_SESSION
*s
, const unsigned char **tick
,
1009 *len
= s
->ext
.ticklen
;
1011 *tick
= s
->ext
.tick
;
1014 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION
*s
)
1016 return s
->ext
.max_early_data
;
1019 int SSL_SESSION_set_max_early_data(SSL_SESSION
*s
, uint32_t max_early_data
)
1021 s
->ext
.max_early_data
= max_early_data
;
1026 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION
*s
,
1027 const unsigned char **alpn
,
1030 *alpn
= s
->ext
.alpn_selected
;
1031 *len
= s
->ext
.alpn_selected_len
;
1034 int SSL_SESSION_set1_alpn_selected(SSL_SESSION
*s
, const unsigned char *alpn
,
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;
1043 s
->ext
.alpn_selected
= OPENSSL_memdup(alpn
, len
);
1044 if (s
->ext
.alpn_selected
== NULL
) {
1045 s
->ext
.alpn_selected_len
= 0;
1048 s
->ext
.alpn_selected_len
= len
;
1053 X509
*SSL_SESSION_get0_peer(SSL_SESSION
*s
)
1058 EVP_PKEY
*SSL_SESSION_get0_peer_rpk(SSL_SESSION
*s
)
1063 int SSL_SESSION_set1_id_context(SSL_SESSION
*s
, const unsigned char *sid_ctx
,
1064 unsigned int sid_ctx_len
)
1066 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
1067 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
1070 s
->sid_ctx_length
= sid_ctx_len
;
1071 if (sid_ctx
!= s
->sid_ctx
)
1072 memcpy(s
->sid_ctx
, sid_ctx
, sid_ctx_len
);
1077 int SSL_SESSION_is_resumable(const SSL_SESSION
*s
)
1080 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1083 return !s
->not_resumable
1084 && (s
->session_id_length
> 0 || s
->ext
.ticklen
> 0);
1087 long SSL_CTX_set_timeout(SSL_CTX
*s
, long t
)
1093 l
= (long)ossl_time2seconds(s
->session_timeout
);
1094 s
->session_timeout
= ossl_seconds2time(t
);
1098 long SSL_CTX_get_timeout(const SSL_CTX
*s
)
1102 return (long)ossl_time2seconds(s
->session_timeout
);
1105 int SSL_set_session_secret_cb(SSL
*s
,
1106 tls_session_secret_cb_fn tls_session_secret_cb
,
1109 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1114 sc
->ext
.session_secret_cb
= tls_session_secret_cb
;
1115 sc
->ext
.session_secret_cb_arg
= arg
;
1119 int SSL_set_session_ticket_ext_cb(SSL
*s
, tls_session_ticket_ext_cb_fn cb
,
1122 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1127 sc
->ext
.session_ticket_cb
= cb
;
1128 sc
->ext
.session_ticket_cb_arg
= arg
;
1132 int SSL_set_session_ticket_ext(SSL
*s
, void *ext_data
, int ext_len
)
1134 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1139 if (sc
->version
>= TLS1_VERSION
) {
1140 OPENSSL_free(sc
->ext
.session_ticket
);
1141 sc
->ext
.session_ticket
= NULL
;
1142 sc
->ext
.session_ticket
=
1143 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT
) + ext_len
);
1144 if (sc
->ext
.session_ticket
== NULL
)
1147 if (ext_data
!= NULL
) {
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
);
1152 sc
->ext
.session_ticket
->length
= 0;
1153 sc
->ext
.session_ticket
->data
= NULL
;
1162 void SSL_CTX_flush_sessions(SSL_CTX
*s
, long t
)
1164 STACK_OF(SSL_SESSION
) *sk
;
1165 SSL_SESSION
*current
;
1167 const OSSL_TIME timeout
= ossl_time_from_time_t(t
);
1169 if (!CRYPTO_THREAD_write_lock(s
->lock
))
1172 sk
= sk_SSL_SESSION_new_null();
1173 i
= lh_SSL_SESSION_get_down_load(s
->sessions
);
1174 lh_SSL_SESSION_set_down_load(s
->sessions
, 0);
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
1181 * But still do the remove_session_cb() within the lock.
1183 while (s
->session_cache_tail
!= NULL
) {
1184 current
= s
->session_cache_tail
;
1185 if (t
== 0 || sess_timedout(timeout
, current
)) {
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
);
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
1198 if (sk
== NULL
|| !sk_SSL_SESSION_push(sk
, current
))
1199 SSL_SESSION_free(current
);
1205 lh_SSL_SESSION_set_down_load(s
->sessions
, i
);
1206 CRYPTO_THREAD_unlock(s
->lock
);
1208 sk_SSL_SESSION_pop_free(sk
, SSL_SESSION_free
);
1211 int ssl_clear_bad_session(SSL_CONNECTION
*s
)
1213 if ((s
->session
!= NULL
) &&
1214 !(s
->shutdown
& SSL_SENT_SHUTDOWN
) &&
1215 !(SSL_in_init(SSL_CONNECTION_GET_SSL(s
))
1216 || SSL_in_before(SSL_CONNECTION_GET_SSL(s
)))) {
1217 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1223 /* locked by SSL_CTX in the calling function */
1224 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1226 if ((s
->next
== NULL
) || (s
->prev
== NULL
))
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
;
1236 ctx
->session_cache_tail
= s
->prev
;
1237 s
->prev
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
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
);
1245 /* middle of list */
1246 s
->next
->prev
= s
->prev
;
1247 s
->prev
->next
= s
->next
;
1250 s
->prev
= s
->next
= NULL
;
1254 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1258 if ((s
->next
!= NULL
) && (s
->prev
!= NULL
))
1259 SSL_SESSION_list_remove(ctx
, s
);
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
);
1267 if (timeoutcmp(s
, ctx
->session_cache_head
) >= 0) {
1269 * if we timeout after (or the same time as) the first
1270 * session, put us first - usual case
1272 s
->next
= ctx
->session_cache_head
;
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
;
1280 s
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1281 ctx
->session_cache_tail
= s
;
1284 * we timeout somewhere in-between - if there is only
1285 * one session in the cache it will be caught above
1287 next
= ctx
->session_cache_head
->next
;
1288 while (next
!= (SSL_SESSION
*)&(ctx
->session_cache_tail
)) {
1289 if (timeoutcmp(s
, next
) >= 0) {
1291 s
->prev
= next
->prev
;
1292 next
->prev
->next
= s
;
1303 void SSL_CTX_sess_set_new_cb(SSL_CTX
*ctx
,
1304 int (*cb
) (struct ssl_st
*ssl
, SSL_SESSION
*sess
))
1306 ctx
->new_session_cb
= cb
;
1309 int (*SSL_CTX_sess_get_new_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, SSL_SESSION
*sess
) {
1310 return ctx
->new_session_cb
;
1313 void SSL_CTX_sess_set_remove_cb(SSL_CTX
*ctx
,
1314 void (*cb
) (SSL_CTX
*ctx
, SSL_SESSION
*sess
))
1316 ctx
->remove_session_cb
= cb
;
1319 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX
*ctx
)) (SSL_CTX
*ctx
,
1320 SSL_SESSION
*sess
) {
1321 return ctx
->remove_session_cb
;
1324 void SSL_CTX_sess_set_get_cb(SSL_CTX
*ctx
,
1325 SSL_SESSION
*(*cb
) (SSL
*ssl
,
1326 const unsigned char *data
,
1327 int len
, int *copy
))
1329 ctx
->get_session_cb
= cb
;
1332 SSL_SESSION
*(*SSL_CTX_sess_get_get_cb(SSL_CTX
*ctx
)) (SSL
*ssl
,
1336 return ctx
->get_session_cb
;
1339 void SSL_CTX_set_info_callback(SSL_CTX
*ctx
,
1340 void (*cb
) (const SSL
*ssl
, int type
, int val
))
1342 ctx
->info_callback
= cb
;
1345 void (*SSL_CTX_get_info_callback(SSL_CTX
*ctx
)) (const SSL
*ssl
, int type
,
1347 return ctx
->info_callback
;
1350 void SSL_CTX_set_client_cert_cb(SSL_CTX
*ctx
,
1351 int (*cb
) (SSL
*ssl
, X509
**x509
,
1354 ctx
->client_cert_cb
= cb
;
1357 int (*SSL_CTX_get_client_cert_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, X509
**x509
,
1359 return ctx
->client_cert_cb
;
1362 void SSL_CTX_set_cookie_generate_cb(SSL_CTX
*ctx
,
1363 int (*cb
) (SSL
*ssl
,
1364 unsigned char *cookie
,
1365 unsigned int *cookie_len
))
1367 ctx
->app_gen_cookie_cb
= cb
;
1370 void SSL_CTX_set_cookie_verify_cb(SSL_CTX
*ctx
,
1371 int (*cb
) (SSL
*ssl
,
1372 const unsigned char *cookie
,
1373 unsigned int cookie_len
))
1375 ctx
->app_verify_cookie_cb
= cb
;
1378 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION
*ss
, const void *data
, size_t len
)
1380 OPENSSL_free(ss
->ticket_appdata
);
1381 ss
->ticket_appdata_len
= 0;
1382 if (data
== NULL
|| len
== 0) {
1383 ss
->ticket_appdata
= NULL
;
1386 ss
->ticket_appdata
= OPENSSL_memdup(data
, len
);
1387 if (ss
->ticket_appdata
!= NULL
) {
1388 ss
->ticket_appdata_len
= len
;
1394 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION
*ss
, void **data
, size_t *len
)
1396 *data
= ss
->ticket_appdata
;
1397 *len
= ss
->ticket_appdata_len
;
1401 void SSL_CTX_set_stateless_cookie_generate_cb(
1403 int (*cb
) (SSL
*ssl
,
1404 unsigned char *cookie
,
1405 size_t *cookie_len
))
1407 ctx
->gen_stateless_cookie_cb
= cb
;
1410 void SSL_CTX_set_stateless_cookie_verify_cb(
1412 int (*cb
) (SSL
*ssl
,
1413 const unsigned char *cookie
,
1416 ctx
->verify_stateless_cookie_cb
= cb
;
1419 IMPLEMENT_PEM_rw(SSL_SESSION
, SSL_SESSION
, PEM_STRING_SSL_SESSION
, SSL_SESSION
)