2 * Copyright 1995-2022 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(time_t t, SSL_SESSION *ss)
31 return ossl_time_compare(ossl_time_from_time_t(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 /* Force positive timeout */
53 ss->calc_timeout = ossl_time_add(ossl_time_from_time_t(ss->time),
54 ossl_time_from_time_t(ss->timeout));
58 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
59 * unlike in earlier protocol versions, the session ticket may not have been
60 * sent yet even though a handshake has finished. The session ticket data could
61 * come in sometime later...or even change if multiple session ticket messages
62 * are sent from the server. The preferred way for applications to obtain
63 * a resumable session is to use SSL_CTX_sess_set_new_cb().
66 SSL_SESSION *SSL_get_session(const SSL *ssl)
67 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
69 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
77 SSL_SESSION *SSL_get1_session(SSL *ssl)
78 /* variant of SSL_get_session: caller really gets something */
83 * Need to lock this all up rather than just use CRYPTO_add so that
84 * somebody doesn't free ssl->session between when we check it's non-null
85 * and when we up the reference count.
87 if (!CRYPTO_THREAD_read_lock(ssl->lock))
89 sess = SSL_get_session(ssl);
91 SSL_SESSION_up_ref(sess);
92 CRYPTO_THREAD_unlock(ssl->lock);
96 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
98 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
101 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
103 return CRYPTO_get_ex_data(&s->ex_data, idx);
106 SSL_SESSION *SSL_SESSION_new(void)
110 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
113 ss = OPENSSL_zalloc(sizeof(*ss));
115 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
119 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
121 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
122 ss->time = time(NULL);
123 ssl_session_calculate_timeout(ss);
124 ss->lock = CRYPTO_THREAD_lock_new();
125 if (ss->lock == NULL) {
126 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
131 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
132 CRYPTO_THREAD_lock_free(ss->lock);
139 SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
141 return ssl_session_dup(src, 1);
145 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
146 * ticket == 0 then no ticket information is duplicated, otherwise it is.
148 SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
152 dest = OPENSSL_malloc(sizeof(*dest));
156 memcpy(dest, src, sizeof(*dest));
159 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
160 * the case of an error whilst halfway through constructing dest
162 #ifndef OPENSSL_NO_PSK
163 dest->psk_identity_hint = NULL;
164 dest->psk_identity = NULL;
166 dest->ext.hostname = NULL;
167 dest->ext.tick = NULL;
168 dest->ext.alpn_selected = NULL;
169 #ifndef OPENSSL_NO_SRP
170 dest->srp_username = NULL;
172 dest->peer_chain = NULL;
174 dest->ticket_appdata = NULL;
175 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
177 /* We deliberately don't copy the prev and next pointers */
181 dest->references = 1;
183 dest->lock = CRYPTO_THREAD_lock_new();
184 if (dest->lock == NULL)
187 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data))
190 if (src->peer != NULL) {
191 if (!X509_up_ref(src->peer))
193 dest->peer = src->peer;
196 if (src->peer_chain != NULL) {
197 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
198 if (dest->peer_chain == NULL)
201 #ifndef OPENSSL_NO_PSK
202 if (src->psk_identity_hint) {
203 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
204 if (dest->psk_identity_hint == NULL) {
208 if (src->psk_identity) {
209 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
210 if (dest->psk_identity == NULL) {
216 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
217 &dest->ex_data, &src->ex_data)) {
221 if (src->ext.hostname) {
222 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
223 if (dest->ext.hostname == NULL) {
228 if (ticket != 0 && src->ext.tick != NULL) {
230 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
231 if (dest->ext.tick == NULL)
234 dest->ext.tick_lifetime_hint = 0;
235 dest->ext.ticklen = 0;
238 if (src->ext.alpn_selected != NULL) {
239 dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
240 src->ext.alpn_selected_len);
241 if (dest->ext.alpn_selected == NULL)
245 #ifndef OPENSSL_NO_SRP
246 if (src->srp_username) {
247 dest->srp_username = OPENSSL_strdup(src->srp_username);
248 if (dest->srp_username == NULL) {
254 if (src->ticket_appdata != NULL) {
255 dest->ticket_appdata =
256 OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
257 if (dest->ticket_appdata == NULL)
263 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
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 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
307 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
308 if (retry < MAX_SESS_ID_ATTEMPTS)
310 /* else - woops a session_id match */
312 * XXX We should also check the external cache -- but the probability of
313 * a collision is negligible, and we could not prevent the concurrent
314 * creation of sessions with identical IDs since we currently don't have
315 * means to atomically check whether a session ID already exists and make
316 * a reservation for it if it does not (this problem applies to the
317 * internal cache as well).
322 int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss)
325 GEN_SESSION_CB cb = def_generate_session_id;
326 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
328 switch (s->version) {
336 case DTLS1_2_VERSION:
337 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
340 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
345 * If RFC5077 ticket, use empty session ID (as server).
347 * (a) ssl_get_prev_session() does lookahead into the
348 * ClientHello extensions to find the session ticket.
349 * When ssl_get_prev_session() fails, statem_srvr.c calls
350 * ssl_get_new_session() in tls_process_client_hello().
351 * At that point, it has not yet parsed the extensions,
352 * however, because of the lookahead, it already knows
353 * whether a ticket is expected or not.
355 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
356 * ServerHello extensions, and before recording the session
357 * ID received from the server, so this block is a noop.
359 if (s->ext.ticket_expected) {
360 ss->session_id_length = 0;
364 /* Choose which callback will set the session ID */
365 if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock))
367 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
368 CRYPTO_THREAD_unlock(ssl->lock);
369 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
370 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
373 if (s->generate_session_id)
374 cb = s->generate_session_id;
375 else if (s->session_ctx->generate_session_id)
376 cb = s->session_ctx->generate_session_id;
377 CRYPTO_THREAD_unlock(s->session_ctx->lock);
378 CRYPTO_THREAD_unlock(ssl->lock);
379 /* Choose a session ID */
380 memset(ss->session_id, 0, ss->session_id_length);
381 tmp = (int)ss->session_id_length;
382 if (!cb(ssl, ss->session_id, &tmp)) {
383 /* The callback failed */
384 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
385 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
389 * Don't allow the callback to set the session length to zero. nor
390 * set it higher than it was.
392 if (tmp == 0 || tmp > ss->session_id_length) {
393 /* The callback set an illegal length */
394 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
395 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
398 ss->session_id_length = tmp;
399 /* Finally, check for a conflict */
400 if (SSL_has_matching_session_id(ssl, ss->session_id,
401 (unsigned int)ss->session_id_length)) {
402 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
409 int ssl_get_new_session(SSL_CONNECTION *s, int session)
411 /* This gets used by clients and servers. */
413 SSL_SESSION *ss = NULL;
415 if ((ss = SSL_SESSION_new()) == NULL) {
416 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
420 /* If the context has a default timeout, use it */
421 if (s->session_ctx->session_timeout == 0)
422 ss->timeout = SSL_get_default_timeout(SSL_CONNECTION_GET_SSL(s));
424 ss->timeout = s->session_ctx->session_timeout;
425 ssl_session_calculate_timeout(ss);
427 SSL_SESSION_free(s->session);
431 if (SSL_CONNECTION_IS_TLS13(s)) {
433 * We generate the session id while constructing the
434 * NewSessionTicket in TLSv1.3.
436 ss->session_id_length = 0;
437 } else if (!ssl_generate_session_id(s, ss)) {
438 /* SSLfatal() already called */
439 SSL_SESSION_free(ss);
444 ss->session_id_length = 0;
447 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
448 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
449 SSL_SESSION_free(ss);
452 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
453 ss->sid_ctx_length = s->sid_ctx_length;
455 ss->ssl_version = s->version;
456 ss->verify_result = X509_V_OK;
458 /* If client supports extended master secret set it in session */
459 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
460 ss->flags |= SSL_SESS_FLAG_EXTMS;
465 SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
466 const unsigned char *sess_id,
469 SSL_SESSION *ret = NULL;
471 if ((s->session_ctx->session_cache_mode
472 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
475 data.ssl_version = s->version;
476 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
479 memcpy(data.session_id, sess_id, sess_id_len);
480 data.session_id_length = sess_id_len;
482 if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
484 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
486 /* don't allow other threads to steal it: */
487 SSL_SESSION_up_ref(ret);
489 CRYPTO_THREAD_unlock(s->session_ctx->lock);
491 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
494 if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
497 ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_SSL(s),
498 sess_id, sess_id_len, ©);
501 ssl_tsan_counter(s->session_ctx,
502 &s->session_ctx->stats.sess_cb_hit);
505 * Increment reference count now if the session callback asks us
506 * to do so (note that if the session structures returned by the
507 * callback are shared between threads, it must handle the
508 * reference count itself [i.e. copy == 0], or things won't be
512 SSL_SESSION_up_ref(ret);
515 * Add the externally cached session to the internal cache as
516 * well if and only if we are supposed to.
518 if ((s->session_ctx->session_cache_mode &
519 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
521 * Either return value of SSL_CTX_add_session should not
522 * interrupt the session resumption process. The return
523 * value is intentionally ignored.
525 (void)SSL_CTX_add_session(s->session_ctx, ret);
534 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
535 * connection. It is only called by servers.
537 * hello: The parsed ClientHello data
541 * 0: no session found
542 * 1: a session may have been found.
545 * - If a session is found then s->session is pointed at it (after freeing an
546 * existing session if need be) and s->verify_result is set from the session.
547 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
548 * if the server should issue a new session ticket (to 0 otherwise).
550 int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello)
552 /* This is used only by servers. */
554 SSL_SESSION *ret = NULL;
556 int try_session_cache = 0;
559 if (SSL_CONNECTION_IS_TLS13(s)) {
561 * By default we will send a new ticket. This can be overridden in the
564 s->ext.ticket_expected = 1;
565 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
566 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
568 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
569 hello->pre_proc_exts, NULL, 0))
574 /* sets s->ext.ticket_expected */
575 r = tls_get_ticket_from_client(s, hello, &ret);
577 case SSL_TICKET_FATAL_ERR_MALLOC:
578 case SSL_TICKET_FATAL_ERR_OTHER:
580 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
582 case SSL_TICKET_NONE:
583 case SSL_TICKET_EMPTY:
584 if (hello->session_id_len > 0) {
585 try_session_cache = 1;
586 ret = lookup_sess_in_cache(s, hello->session_id,
587 hello->session_id_len);
590 case SSL_TICKET_NO_DECRYPT:
591 case SSL_TICKET_SUCCESS:
592 case SSL_TICKET_SUCCESS_RENEW:
600 /* Now ret is non-NULL and we own one of its reference counts. */
602 /* Check TLS version consistency */
603 if (ret->ssl_version != s->version)
606 if (ret->sid_ctx_length != s->sid_ctx_length
607 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
609 * We have the session requested by the client, but we don't want to
610 * use it in this context.
612 goto err; /* treat like cache miss */
615 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
617 * We can't be sure if this session is being used out of context,
618 * which is especially important for SSL_VERIFY_PEER. The application
619 * should have used SSL[_CTX]_set_session_id_context. For this error
620 * case, we generate an error instead of treating the event like a
621 * cache miss (otherwise it would be easy for applications to
622 * effectively disable the session cache by accident without anyone
626 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
627 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
632 if (sess_timedout(time(NULL), ret)) {
633 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout);
634 if (try_session_cache) {
635 /* session was from the cache, so remove it */
636 SSL_CTX_remove_session(s->session_ctx, ret);
641 /* Check extended master secret extension consistency */
642 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
643 /* If old session includes extms, but new does not: abort handshake */
644 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
645 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS);
649 } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
650 /* If new session includes extms, but old does not: do not resume */
654 if (!SSL_CONNECTION_IS_TLS13(s)) {
655 /* We already did this for TLS1.3 */
656 SSL_SESSION_free(s->session);
660 ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit);
661 s->verify_result = s->session->verify_result;
666 SSL_SESSION_free(ret);
667 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
668 if (SSL_CONNECTION_IS_TLS13(s))
671 if (!try_session_cache) {
673 * The session was from a ticket, so we should issue a ticket for
676 s->ext.ticket_expected = 1;
685 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
691 * add just 1 reference count for the SSL_CTX's session cache even though
692 * it has two ways of access: each session is in a doubly linked list and
695 SSL_SESSION_up_ref(c);
697 * if session c is in already in cache, we take back the increment later
700 if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
704 s = lh_SSL_SESSION_insert(ctx->sessions, c);
707 * s != NULL iff we already had a session with the given PID. In this
708 * case, s == c should hold (then we did not really modify
709 * ctx->sessions), or we're in trouble.
711 if (s != NULL && s != c) {
712 /* We *are* in trouble ... */
713 SSL_SESSION_list_remove(ctx, s);
716 * ... so pretend the other session did not exist in cache (we cannot
717 * handle two SSL_SESSION structures with identical session ID in the
718 * same cache, which could happen e.g. when two threads concurrently
719 * obtain the same session from an external cache)
722 } else if (s == NULL &&
723 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
724 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
727 * ... so take back the extra reference and also don't add
728 * the session to the SSL_SESSION_list at this time
733 /* Adjust last used time, and add back into the cache at the appropriate spot */
734 if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) {
735 c->time = time(NULL);
736 ssl_session_calculate_timeout(c);
741 * new cache entry -- remove old ones if cache has become too large
742 * delete cache entry *before* add, so we don't remove the one we're adding!
747 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
748 while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) {
749 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
752 ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full);
757 SSL_SESSION_list_add(ctx, c);
761 * existing cache entry -- decrement previously incremented reference
762 * count because it already takes into account the cache
765 SSL_SESSION_free(s); /* s == c */
768 CRYPTO_THREAD_unlock(ctx->lock);
772 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
774 return remove_session_lock(ctx, c, 1);
777 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
782 if ((c != NULL) && (c->session_id_length != 0)) {
784 if (!CRYPTO_THREAD_write_lock(ctx->lock))
787 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
789 r = lh_SSL_SESSION_delete(ctx->sessions, r);
790 SSL_SESSION_list_remove(ctx, r);
792 c->not_resumable = 1;
795 CRYPTO_THREAD_unlock(ctx->lock);
797 if (ctx->remove_session_cb != NULL)
798 ctx->remove_session_cb(ctx, c);
806 void SSL_SESSION_free(SSL_SESSION *ss)
812 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
813 REF_PRINT_COUNT("SSL_SESSION", ss);
816 REF_ASSERT_ISNT(i < 0);
818 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
820 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
821 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
823 OSSL_STACK_OF_X509_free(ss->peer_chain);
824 OPENSSL_free(ss->ext.hostname);
825 OPENSSL_free(ss->ext.tick);
826 #ifndef OPENSSL_NO_PSK
827 OPENSSL_free(ss->psk_identity_hint);
828 OPENSSL_free(ss->psk_identity);
830 #ifndef OPENSSL_NO_SRP
831 OPENSSL_free(ss->srp_username);
833 OPENSSL_free(ss->ext.alpn_selected);
834 OPENSSL_free(ss->ticket_appdata);
835 CRYPTO_THREAD_lock_free(ss->lock);
836 OPENSSL_clear_free(ss, sizeof(*ss));
839 int SSL_SESSION_up_ref(SSL_SESSION *ss)
843 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
846 REF_PRINT_COUNT("SSL_SESSION", ss);
847 REF_ASSERT_ISNT(i < 2);
848 return ((i > 1) ? 1 : 0);
851 int SSL_set_session(SSL *s, SSL_SESSION *session)
853 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
858 ssl_clear_bad_session(sc);
859 if (s->ctx->method != s->method) {
860 if (!SSL_set_ssl_method(s, s->ctx->method))
864 if (session != NULL) {
865 SSL_SESSION_up_ref(session);
866 sc->verify_result = session->verify_result;
868 SSL_SESSION_free(sc->session);
869 sc->session = session;
874 int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
875 unsigned int sid_len)
877 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
878 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
881 s->session_id_length = sid_len;
882 if (sid != s->session_id)
883 memcpy(s->session_id, sid, sid_len);
887 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
889 time_t new_timeout = (time_t)t;
891 if (s == NULL || t < 0)
893 if (s->owner != NULL) {
894 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
896 s->timeout = new_timeout;
897 ssl_session_calculate_timeout(s);
898 SSL_SESSION_list_add(s->owner, s);
899 CRYPTO_THREAD_unlock(s->owner->lock);
901 s->timeout = new_timeout;
902 ssl_session_calculate_timeout(s);
907 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
911 return (long)s->timeout;
914 long SSL_SESSION_get_time(const SSL_SESSION *s)
918 return (long)s->time;
921 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
923 time_t new_time = (time_t)t;
927 if (s->owner != NULL) {
928 if (!CRYPTO_THREAD_write_lock(s->owner->lock))
931 ssl_session_calculate_timeout(s);
932 SSL_SESSION_list_add(s->owner, s);
933 CRYPTO_THREAD_unlock(s->owner->lock);
936 ssl_session_calculate_timeout(s);
941 int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
943 return s->ssl_version;
946 int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
948 s->ssl_version = version;
952 const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
957 int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
963 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
965 return s->ext.hostname;
968 int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
970 OPENSSL_free(s->ext.hostname);
971 if (hostname == NULL) {
972 s->ext.hostname = NULL;
975 s->ext.hostname = OPENSSL_strdup(hostname);
977 return s->ext.hostname != NULL;
980 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
982 return (s->ext.ticklen > 0) ? 1 : 0;
985 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
987 return s->ext.tick_lifetime_hint;
990 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
993 *len = s->ext.ticklen;
998 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
1000 return s->ext.max_early_data;
1003 int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
1005 s->ext.max_early_data = max_early_data;
1010 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
1011 const unsigned char **alpn,
1014 *alpn = s->ext.alpn_selected;
1015 *len = s->ext.alpn_selected_len;
1018 int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
1021 OPENSSL_free(s->ext.alpn_selected);
1022 if (alpn == NULL || len == 0) {
1023 s->ext.alpn_selected = NULL;
1024 s->ext.alpn_selected_len = 0;
1027 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
1028 if (s->ext.alpn_selected == NULL) {
1029 s->ext.alpn_selected_len = 0;
1032 s->ext.alpn_selected_len = len;
1037 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
1042 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1043 unsigned int sid_ctx_len)
1045 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1046 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1049 s->sid_ctx_length = sid_ctx_len;
1050 if (sid_ctx != s->sid_ctx)
1051 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1056 int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1059 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1062 return !s->not_resumable
1063 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1066 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1071 l = s->session_timeout;
1072 s->session_timeout = t;
1076 long SSL_CTX_get_timeout(const SSL_CTX *s)
1080 return s->session_timeout;
1083 int SSL_set_session_secret_cb(SSL *s,
1084 tls_session_secret_cb_fn tls_session_secret_cb,
1087 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1092 sc->ext.session_secret_cb = tls_session_secret_cb;
1093 sc->ext.session_secret_cb_arg = arg;
1097 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1100 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1105 sc->ext.session_ticket_cb = cb;
1106 sc->ext.session_ticket_cb_arg = arg;
1110 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1112 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1117 if (sc->version >= TLS1_VERSION) {
1118 OPENSSL_free(sc->ext.session_ticket);
1119 sc->ext.session_ticket = NULL;
1120 sc->ext.session_ticket =
1121 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1122 if (sc->ext.session_ticket == NULL) {
1123 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1127 if (ext_data != NULL) {
1128 sc->ext.session_ticket->length = ext_len;
1129 sc->ext.session_ticket->data = sc->ext.session_ticket + 1;
1130 memcpy(sc->ext.session_ticket->data, ext_data, ext_len);
1132 sc->ext.session_ticket->length = 0;
1133 sc->ext.session_ticket->data = NULL;
1142 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1144 STACK_OF(SSL_SESSION) *sk;
1145 SSL_SESSION *current;
1148 if (!CRYPTO_THREAD_write_lock(s->lock))
1151 sk = sk_SSL_SESSION_new_null();
1152 i = lh_SSL_SESSION_get_down_load(s->sessions);
1153 lh_SSL_SESSION_set_down_load(s->sessions, 0);
1156 * Iterate over the list from the back (oldest), and stop
1157 * when a session can no longer be removed.
1158 * Add the session to a temporary list to be freed outside
1160 * But still do the remove_session_cb() within the lock.
1162 while (s->session_cache_tail != NULL) {
1163 current = s->session_cache_tail;
1164 if (t == 0 || sess_timedout((time_t)t, current)) {
1165 lh_SSL_SESSION_delete(s->sessions, current);
1166 SSL_SESSION_list_remove(s, current);
1167 current->not_resumable = 1;
1168 if (s->remove_session_cb != NULL)
1169 s->remove_session_cb(s, current);
1171 * Throw the session on a stack, it's entirely plausible
1172 * that while freeing outside the critical section, the
1173 * session could be re-added, so avoid using the next/prev
1174 * pointers. If the stack failed to create, or the session
1175 * couldn't be put on the stack, just free it here
1177 if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
1178 SSL_SESSION_free(current);
1184 lh_SSL_SESSION_set_down_load(s->sessions, i);
1185 CRYPTO_THREAD_unlock(s->lock);
1187 sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
1190 int ssl_clear_bad_session(SSL_CONNECTION *s)
1192 if ((s->session != NULL) &&
1193 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1194 !(SSL_in_init(SSL_CONNECTION_GET_SSL(s))
1195 || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) {
1196 SSL_CTX_remove_session(s->session_ctx, s->session);
1202 /* locked by SSL_CTX in the calling function */
1203 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1205 if ((s->next == NULL) || (s->prev == NULL))
1208 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1209 /* last element in list */
1210 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1211 /* only one element in list */
1212 ctx->session_cache_head = NULL;
1213 ctx->session_cache_tail = NULL;
1215 ctx->session_cache_tail = s->prev;
1216 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1219 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1220 /* first element in list */
1221 ctx->session_cache_head = s->next;
1222 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1224 /* middle of list */
1225 s->next->prev = s->prev;
1226 s->prev->next = s->next;
1229 s->prev = s->next = NULL;
1233 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1237 if ((s->next != NULL) && (s->prev != NULL))
1238 SSL_SESSION_list_remove(ctx, s);
1240 if (ctx->session_cache_head == NULL) {
1241 ctx->session_cache_head = s;
1242 ctx->session_cache_tail = s;
1243 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1244 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1246 if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
1248 * if we timeout after (or the same time as) the first
1249 * session, put us first - usual case
1251 s->next = ctx->session_cache_head;
1253 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1254 ctx->session_cache_head = s;
1255 } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
1256 /* if we timeout before the last session, put us last */
1257 s->prev = ctx->session_cache_tail;
1259 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1260 ctx->session_cache_tail = s;
1263 * we timeout somewhere in-between - if there is only
1264 * one session in the cache it will be caught above
1266 next = ctx->session_cache_head->next;
1267 while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) {
1268 if (timeoutcmp(s, next) >= 0) {
1270 s->prev = next->prev;
1271 next->prev->next = s;
1282 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1283 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
1285 ctx->new_session_cb = cb;
1288 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1289 return ctx->new_session_cb;
1292 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1293 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1295 ctx->remove_session_cb = cb;
1298 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1299 SSL_SESSION *sess) {
1300 return ctx->remove_session_cb;
1303 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1304 SSL_SESSION *(*cb) (SSL *ssl,
1305 const unsigned char *data,
1306 int len, int *copy))
1308 ctx->get_session_cb = cb;
1311 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1315 return ctx->get_session_cb;
1318 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1319 void (*cb) (const SSL *ssl, int type, int val))
1321 ctx->info_callback = cb;
1324 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1326 return ctx->info_callback;
1329 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1330 int (*cb) (SSL *ssl, X509 **x509,
1333 ctx->client_cert_cb = cb;
1336 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1338 return ctx->client_cert_cb;
1341 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1342 int (*cb) (SSL *ssl,
1343 unsigned char *cookie,
1344 unsigned int *cookie_len))
1346 ctx->app_gen_cookie_cb = cb;
1349 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1350 int (*cb) (SSL *ssl,
1351 const unsigned char *cookie,
1352 unsigned int cookie_len))
1354 ctx->app_verify_cookie_cb = cb;
1357 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1359 OPENSSL_free(ss->ticket_appdata);
1360 ss->ticket_appdata_len = 0;
1361 if (data == NULL || len == 0) {
1362 ss->ticket_appdata = NULL;
1365 ss->ticket_appdata = OPENSSL_memdup(data, len);
1366 if (ss->ticket_appdata != NULL) {
1367 ss->ticket_appdata_len = len;
1373 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1375 *data = ss->ticket_appdata;
1376 *len = ss->ticket_appdata_len;
1380 void SSL_CTX_set_stateless_cookie_generate_cb(
1382 int (*cb) (SSL *ssl,
1383 unsigned char *cookie,
1384 size_t *cookie_len))
1386 ctx->gen_stateless_cookie_cb = cb;
1389 void SSL_CTX_set_stateless_cookie_verify_cb(
1391 int (*cb) (SSL *ssl,
1392 const unsigned char *cookie,
1395 ctx->verify_stateless_cookie_cb = cb;
1398 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)