2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the OpenSSL license (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
12 #include <openssl/lhash.h>
13 #include <openssl/rand.h>
14 #include <openssl/engine.h>
15 #include "internal/refcount.h"
17 #include "statem/statem_locl.h"
19 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
20 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
21 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
24 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
25 * unlike in earlier protocol versions, the session ticket may not have been
26 * sent yet even though a handshake has finished. The session ticket data could
27 * come in sometime later...or even change if multiple session ticket messages
28 * are sent from the server. The preferred way for applications to obtain
29 * a resumable session is to use SSL_CTX_sess_set_new_cb().
32 SSL_SESSION *SSL_get_session(const SSL *ssl)
33 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
35 return (ssl->session);
38 SSL_SESSION *SSL_get1_session(SSL *ssl)
39 /* variant of SSL_get_session: caller really gets something */
43 * Need to lock this all up rather than just use CRYPTO_add so that
44 * somebody doesn't free ssl->session between when we check it's non-null
45 * and when we up the reference count.
47 CRYPTO_THREAD_read_lock(ssl->lock);
50 SSL_SESSION_up_ref(sess);
51 CRYPTO_THREAD_unlock(ssl->lock);
55 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
57 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
60 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
62 return (CRYPTO_get_ex_data(&s->ex_data, idx));
65 SSL_SESSION *SSL_SESSION_new(void)
69 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
72 ss = OPENSSL_zalloc(sizeof(*ss));
74 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
78 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
80 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
81 ss->time = (unsigned long)time(NULL);
82 ss->lock = CRYPTO_THREAD_lock_new();
83 if (ss->lock == NULL) {
84 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
89 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
90 CRYPTO_THREAD_lock_free(ss->lock);
97 SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *src)
99 return ssl_session_dup(src, 1);
103 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
104 * ticket == 0 then no ticket information is duplicated, otherwise it is.
106 SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
110 dest = OPENSSL_malloc(sizeof(*src));
114 memcpy(dest, src, sizeof(*dest));
117 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
118 * the case of an error whilst halfway through constructing dest
120 #ifndef OPENSSL_NO_PSK
121 dest->psk_identity_hint = NULL;
122 dest->psk_identity = NULL;
124 dest->ciphers = NULL;
125 dest->ext.hostname = NULL;
126 #ifndef OPENSSL_NO_EC
127 dest->ext.ecpointformats = NULL;
128 dest->ext.supportedgroups = NULL;
130 dest->ext.tick = NULL;
131 dest->ext.alpn_selected = NULL;
132 #ifndef OPENSSL_NO_SRP
133 dest->srp_username = NULL;
135 dest->peer_chain = NULL;
137 dest->ext.tick_nonce = NULL;
138 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
140 /* We deliberately don't copy the prev and next pointers */
144 dest->references = 1;
146 dest->lock = CRYPTO_THREAD_lock_new();
147 if (dest->lock == NULL)
150 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data))
153 if (src->peer != NULL) {
154 if (!X509_up_ref(src->peer))
156 dest->peer = src->peer;
159 if (src->peer_chain != NULL) {
160 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
161 if (dest->peer_chain == NULL)
164 #ifndef OPENSSL_NO_PSK
165 if (src->psk_identity_hint) {
166 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
167 if (dest->psk_identity_hint == NULL) {
171 if (src->psk_identity) {
172 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
173 if (dest->psk_identity == NULL) {
179 if (src->ciphers != NULL) {
180 dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
181 if (dest->ciphers == NULL)
185 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
186 &dest->ex_data, &src->ex_data)) {
190 if (src->ext.hostname) {
191 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
192 if (dest->ext.hostname == NULL) {
196 #ifndef OPENSSL_NO_EC
197 if (src->ext.ecpointformats) {
198 dest->ext.ecpointformats =
199 OPENSSL_memdup(src->ext.ecpointformats,
200 src->ext.ecpointformats_len);
201 if (dest->ext.ecpointformats == NULL)
204 if (src->ext.supportedgroups) {
205 dest->ext.supportedgroups =
206 OPENSSL_memdup(src->ext.supportedgroups,
207 src->ext.supportedgroups_len);
208 if (dest->ext.supportedgroups == NULL)
213 if (ticket != 0 && src->ext.tick != NULL) {
215 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
216 if (dest->ext.tick == NULL)
219 dest->ext.tick_lifetime_hint = 0;
220 dest->ext.ticklen = 0;
223 if (src->ext.alpn_selected) {
224 dest->ext.alpn_selected =
225 (unsigned char*)OPENSSL_strndup((char*)src->ext.alpn_selected,
226 src->ext.alpn_selected_len);
227 if (dest->ext.alpn_selected == NULL) {
232 if (src->ext.tick_nonce != NULL) {
233 dest->ext.tick_nonce = OPENSSL_memdup(src->ext.tick_nonce,
234 src->ext.tick_nonce_len);
235 if (dest->ext.tick_nonce == NULL)
239 #ifndef OPENSSL_NO_SRP
240 if (src->srp_username) {
241 dest->srp_username = OPENSSL_strdup(src->srp_username);
242 if (dest->srp_username == NULL) {
250 SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
251 SSL_SESSION_free(dest);
255 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
258 *len = (unsigned int)s->session_id_length;
259 return s->session_id;
261 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
265 *len = (unsigned int)s->sid_ctx_length;
269 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
271 return s->compress_meth;
275 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
276 * the ID with random junk repeatedly until we have no conflict is going to
277 * complete in one iteration pretty much "most" of the time (btw:
278 * understatement). So, if it takes us 10 iterations and we still can't avoid
279 * a conflict - well that's a reasonable point to call it quits. Either the
280 * RAND code is broken or someone is trying to open roughly very close to
281 * 2^256 SSL sessions to our server. How you might store that many sessions
282 * is perhaps a more interesting question ...
285 #define MAX_SESS_ID_ATTEMPTS 10
286 static int def_generate_session_id(SSL *ssl, unsigned char *id,
287 unsigned int *id_len)
289 unsigned int retry = 0;
291 if (ssl_randbytes(ssl, id, *id_len) <= 0)
293 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
294 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
295 if (retry < MAX_SESS_ID_ATTEMPTS)
297 /* else - woops a session_id match */
299 * XXX We should also check the external cache -- but the probability of
300 * a collision is negligible, and we could not prevent the concurrent
301 * creation of sessions with identical IDs since we currently don't have
302 * means to atomically check whether a session ID already exists and make
303 * a reservation for it if it does not (this problem applies to the
304 * internal cache as well).
309 int ssl_get_new_session(SSL *s, int session)
311 /* This gets used by clients and servers. */
314 SSL_SESSION *ss = NULL;
315 GEN_SESSION_CB cb = def_generate_session_id;
317 if ((ss = SSL_SESSION_new()) == NULL)
320 /* If the context has a default timeout, use it */
321 if (s->session_ctx->session_timeout == 0)
322 ss->timeout = SSL_get_default_timeout(s);
324 ss->timeout = s->session_ctx->session_timeout;
326 SSL_SESSION_free(s->session);
330 if (s->version == SSL3_VERSION) {
331 ss->ssl_version = SSL3_VERSION;
332 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
333 } else if (s->version == TLS1_VERSION) {
334 ss->ssl_version = TLS1_VERSION;
335 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
336 } else if (s->version == TLS1_1_VERSION) {
337 ss->ssl_version = TLS1_1_VERSION;
338 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
339 } else if (s->version == TLS1_2_VERSION) {
340 ss->ssl_version = TLS1_2_VERSION;
341 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
342 } else if (s->version == TLS1_3_VERSION) {
343 ss->ssl_version = TLS1_3_VERSION;
344 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
345 } else if (s->version == DTLS1_BAD_VER) {
346 ss->ssl_version = DTLS1_BAD_VER;
347 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
348 } else if (s->version == DTLS1_VERSION) {
349 ss->ssl_version = DTLS1_VERSION;
350 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
351 } else if (s->version == DTLS1_2_VERSION) {
352 ss->ssl_version = DTLS1_2_VERSION;
353 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
355 SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
356 SSL_SESSION_free(ss);
361 * If RFC5077 ticket, use empty session ID (as server).
363 * (a) ssl_get_prev_session() does lookahead into the
364 * ClientHello extensions to find the session ticket.
365 * When ssl_get_prev_session() fails, statem_srvr.c calls
366 * ssl_get_new_session() in tls_process_client_hello().
367 * At that point, it has not yet parsed the extensions,
368 * however, because of the lookahead, it already knows
369 * whether a ticket is expected or not.
371 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
372 * ServerHello extensions, and before recording the session
373 * ID received from the server, so this block is a noop.
375 if (s->ext.ticket_expected) {
376 ss->session_id_length = 0;
380 /* Choose which callback will set the session ID */
381 CRYPTO_THREAD_read_lock(s->lock);
382 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
383 if (s->generate_session_id)
384 cb = s->generate_session_id;
385 else if (s->session_ctx->generate_session_id)
386 cb = s->session_ctx->generate_session_id;
387 CRYPTO_THREAD_unlock(s->session_ctx->lock);
388 CRYPTO_THREAD_unlock(s->lock);
389 /* Choose a session ID */
390 memset(ss->session_id, 0, ss->session_id_length);
391 tmp = (int)ss->session_id_length;
392 if (!cb(s, ss->session_id, &tmp)) {
393 /* The callback failed */
394 SSLerr(SSL_F_SSL_GET_NEW_SESSION,
395 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
396 SSL_SESSION_free(ss);
400 * Don't allow the callback to set the session length to zero. nor
401 * set it higher than it was.
403 if (tmp == 0 || tmp > ss->session_id_length) {
404 /* The callback set an illegal length */
405 SSLerr(SSL_F_SSL_GET_NEW_SESSION,
406 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
407 SSL_SESSION_free(ss);
410 ss->session_id_length = tmp;
411 /* Finally, check for a conflict */
412 if (SSL_has_matching_session_id(s, ss->session_id,
413 (unsigned int)ss->session_id_length)) {
414 SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT);
415 SSL_SESSION_free(ss);
420 if (s->ext.hostname) {
421 ss->ext.hostname = OPENSSL_strdup(s->ext.hostname);
422 if (ss->ext.hostname == NULL) {
423 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
424 SSL_SESSION_free(ss);
429 ss->session_id_length = 0;
432 if (s->sid_ctx_length > sizeof ss->sid_ctx) {
433 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
434 SSL_SESSION_free(ss);
437 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
438 ss->sid_ctx_length = s->sid_ctx_length;
440 ss->ssl_version = s->version;
441 ss->verify_result = X509_V_OK;
443 /* If client supports extended master secret set it in session */
444 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
445 ss->flags |= SSL_SESS_FLAG_EXTMS;
451 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
452 * connection. It is only called by servers.
454 * hello: The parsed ClientHello data
458 * 0: no session found
459 * 1: a session may have been found.
462 * - If a session is found then s->session is pointed at it (after freeing an
463 * existing session if need be) and s->verify_result is set from the session.
464 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
465 * if the server should issue a new session ticket (to 0 otherwise).
467 int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello, int *al)
469 /* This is used only by servers. */
471 SSL_SESSION *ret = NULL;
473 int try_session_cache = 0;
476 if (SSL_IS_TLS13(s)) {
477 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
478 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
480 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
481 hello->pre_proc_exts, NULL, 0, al))
486 /* sets s->ext.ticket_expected */
487 r = tls_get_ticket_from_client(s, hello, &ret);
489 case TICKET_FATAL_ERR_MALLOC:
490 case TICKET_FATAL_ERR_OTHER:
495 if (hello->session_id_len > 0)
496 try_session_cache = 1;
498 case TICKET_NO_DECRYPT:
500 case TICKET_SUCCESS_RENEW:
505 if (try_session_cache &&
507 !(s->session_ctx->session_cache_mode &
508 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
511 data.ssl_version = s->version;
512 memcpy(data.session_id, hello->session_id, hello->session_id_len);
513 data.session_id_length = hello->session_id_len;
515 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
516 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
518 /* don't allow other threads to steal it: */
519 SSL_SESSION_up_ref(ret);
521 CRYPTO_THREAD_unlock(s->session_ctx->lock);
523 s->session_ctx->stats.sess_miss++;
526 if (try_session_cache &&
527 ret == NULL && s->session_ctx->get_session_cb != NULL) {
530 ret = s->session_ctx->get_session_cb(s, hello->session_id,
531 hello->session_id_len,
535 s->session_ctx->stats.sess_cb_hit++;
538 * Increment reference count now if the session callback asks us
539 * to do so (note that if the session structures returned by the
540 * callback are shared between threads, it must handle the
541 * reference count itself [i.e. copy == 0], or things won't be
545 SSL_SESSION_up_ref(ret);
548 * Add the externally cached session to the internal cache as
549 * well if and only if we are supposed to.
552 (s->session_ctx->session_cache_mode &
553 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
555 * Either return value of SSL_CTX_add_session should not
556 * interrupt the session resumption process. The return
557 * value is intentionally ignored.
559 SSL_CTX_add_session(s->session_ctx, ret);
567 /* Now ret is non-NULL and we own one of its reference counts. */
569 /* Check TLS version consistency */
570 if (ret->ssl_version != s->version)
573 if (ret->sid_ctx_length != s->sid_ctx_length
574 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
576 * We have the session requested by the client, but we don't want to
577 * use it in this context.
579 goto err; /* treat like cache miss */
582 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
584 * We can't be sure if this session is being used out of context,
585 * which is especially important for SSL_VERIFY_PEER. The application
586 * should have used SSL[_CTX]_set_session_id_context. For this error
587 * case, we generate an error instead of treating the event like a
588 * cache miss (otherwise it would be easy for applications to
589 * effectively disable the session cache by accident without anyone
593 SSLerr(SSL_F_SSL_GET_PREV_SESSION,
594 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
599 if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
600 s->session_ctx->stats.sess_timeout++;
601 if (try_session_cache) {
602 /* session was from the cache, so remove it */
603 SSL_CTX_remove_session(s->session_ctx, ret);
608 /* Check extended master secret extension consistency */
609 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
610 /* If old session includes extms, but new does not: abort handshake */
611 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
612 SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS);
613 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
617 } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
618 /* If new session includes extms, but old does not: do not resume */
622 if (!SSL_IS_TLS13(s)) {
623 /* We already did this for TLS1.3 */
624 SSL_SESSION_free(s->session);
628 s->session_ctx->stats.sess_hit++;
629 s->verify_result = s->session->verify_result;
634 SSL_SESSION_free(ret);
635 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
639 if (!try_session_cache) {
641 * The session was from a ticket, so we should issue a ticket for
644 s->ext.ticket_expected = 1;
648 *al = SSL_AD_INTERNAL_ERROR;
655 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
661 * add just 1 reference count for the SSL_CTX's session cache even though
662 * it has two ways of access: each session is in a doubly linked list and
665 SSL_SESSION_up_ref(c);
667 * if session c is in already in cache, we take back the increment later
670 CRYPTO_THREAD_write_lock(ctx->lock);
671 s = lh_SSL_SESSION_insert(ctx->sessions, c);
674 * s != NULL iff we already had a session with the given PID. In this
675 * case, s == c should hold (then we did not really modify
676 * ctx->sessions), or we're in trouble.
678 if (s != NULL && s != c) {
679 /* We *are* in trouble ... */
680 SSL_SESSION_list_remove(ctx, s);
683 * ... so pretend the other session did not exist in cache (we cannot
684 * handle two SSL_SESSION structures with identical session ID in the
685 * same cache, which could happen e.g. when two threads concurrently
686 * obtain the same session from an external cache)
689 } else if (s == NULL &&
690 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
691 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
694 * ... so take back the extra reference and also don't add
695 * the session to the SSL_SESSION_list at this time
700 /* Put at the head of the queue unless it is already in the cache */
702 SSL_SESSION_list_add(ctx, c);
706 * existing cache entry -- decrement previously incremented reference
707 * count because it already takes into account the cache
710 SSL_SESSION_free(s); /* s == c */
714 * new cache entry -- remove old ones if cache has become too large
719 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
720 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
721 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
724 ctx->stats.sess_cache_full++;
728 CRYPTO_THREAD_unlock(ctx->lock);
732 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
734 return remove_session_lock(ctx, c, 1);
737 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
742 if ((c != NULL) && (c->session_id_length != 0)) {
744 CRYPTO_THREAD_write_lock(ctx->lock);
745 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
747 r = lh_SSL_SESSION_delete(ctx->sessions, c);
748 SSL_SESSION_list_remove(ctx, c);
750 c->not_resumable = 1;
753 CRYPTO_THREAD_unlock(ctx->lock);
758 if (ctx->remove_session_cb != NULL)
759 ctx->remove_session_cb(ctx, c);
765 void SSL_SESSION_free(SSL_SESSION *ss)
772 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
773 REF_PRINT_COUNT("SSL_SESSION", ss);
776 REF_ASSERT_ISNT(i < 0);
778 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
780 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
781 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
783 sk_X509_pop_free(ss->peer_chain, X509_free);
784 sk_SSL_CIPHER_free(ss->ciphers);
785 OPENSSL_free(ss->ext.hostname);
786 OPENSSL_free(ss->ext.tick);
787 #ifndef OPENSSL_NO_EC
788 OPENSSL_free(ss->ext.ecpointformats);
789 ss->ext.ecpointformats = NULL;
790 ss->ext.ecpointformats_len = 0;
791 OPENSSL_free(ss->ext.supportedgroups);
792 ss->ext.supportedgroups = NULL;
793 ss->ext.supportedgroups_len = 0;
794 #endif /* OPENSSL_NO_EC */
795 #ifndef OPENSSL_NO_PSK
796 OPENSSL_free(ss->psk_identity_hint);
797 OPENSSL_free(ss->psk_identity);
799 #ifndef OPENSSL_NO_SRP
800 OPENSSL_free(ss->srp_username);
802 OPENSSL_free(ss->ext.alpn_selected);
803 OPENSSL_free(ss->ext.tick_nonce);
804 CRYPTO_THREAD_lock_free(ss->lock);
805 OPENSSL_clear_free(ss, sizeof(*ss));
808 int SSL_SESSION_up_ref(SSL_SESSION *ss)
812 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
815 REF_PRINT_COUNT("SSL_SESSION", ss);
816 REF_ASSERT_ISNT(i < 2);
817 return ((i > 1) ? 1 : 0);
820 int SSL_set_session(SSL *s, SSL_SESSION *session)
822 ssl_clear_bad_session(s);
823 if (s->ctx->method != s->method) {
824 if (!SSL_set_ssl_method(s, s->ctx->method))
828 if (session != NULL) {
829 SSL_SESSION_up_ref(session);
830 s->verify_result = session->verify_result;
832 SSL_SESSION_free(s->session);
833 s->session = session;
838 int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
839 unsigned int sid_len)
841 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
842 SSLerr(SSL_F_SSL_SESSION_SET1_ID,
843 SSL_R_SSL_SESSION_ID_TOO_LONG);
846 s->session_id_length = sid_len;
847 if (sid != s->session_id)
848 memcpy(s->session_id, sid, sid_len);
852 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
860 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
867 long SSL_SESSION_get_time(const SSL_SESSION *s)
874 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
882 int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
884 return s->ssl_version;
887 int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
889 s->ssl_version = version;
893 const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
898 int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
904 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
906 return s->ext.hostname;
909 int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
911 OPENSSL_free(s->ext.hostname);
912 if (hostname == NULL) {
913 s->ext.hostname = NULL;
916 s->ext.hostname = OPENSSL_strdup(hostname);
918 return s->ext.hostname != NULL;
921 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
923 return (s->ext.ticklen > 0) ? 1 : 0;
926 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
928 return s->ext.tick_lifetime_hint;
931 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
934 *len = s->ext.ticklen;
939 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
941 return s->ext.max_early_data;
944 int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
946 s->ext.max_early_data = max_early_data;
951 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
952 const unsigned char **alpn,
955 *alpn = s->ext.alpn_selected;
956 *len = s->ext.alpn_selected_len;
959 int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
962 OPENSSL_free(s->ext.alpn_selected);
963 if (alpn == NULL || len == 0) {
964 s->ext.alpn_selected = NULL;
965 s->ext.alpn_selected_len = 0;
968 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
969 if (s->ext.alpn_selected == NULL) {
970 s->ext.alpn_selected_len = 0;
973 s->ext.alpn_selected_len = len;
978 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
983 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
984 unsigned int sid_ctx_len)
986 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
987 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
988 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
991 s->sid_ctx_length = sid_ctx_len;
992 if (sid_ctx != s->sid_ctx)
993 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
998 int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1001 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1004 return !s->not_resumable
1005 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1008 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1013 l = s->session_timeout;
1014 s->session_timeout = t;
1018 long SSL_CTX_get_timeout(const SSL_CTX *s)
1022 return (s->session_timeout);
1025 int SSL_set_session_secret_cb(SSL *s,
1026 tls_session_secret_cb_fn tls_session_secret_cb,
1031 s->ext.session_secret_cb = tls_session_secret_cb;
1032 s->ext.session_secret_cb_arg = arg;
1036 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1041 s->ext.session_ticket_cb = cb;
1042 s->ext.session_ticket_cb_arg = arg;
1046 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1048 if (s->version >= TLS1_VERSION) {
1049 OPENSSL_free(s->ext.session_ticket);
1050 s->ext.session_ticket = NULL;
1051 s->ext.session_ticket =
1052 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1053 if (s->ext.session_ticket == NULL) {
1054 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1058 if (ext_data != NULL) {
1059 s->ext.session_ticket->length = ext_len;
1060 s->ext.session_ticket->data = s->ext.session_ticket + 1;
1061 memcpy(s->ext.session_ticket->data, ext_data, ext_len);
1063 s->ext.session_ticket->length = 0;
1064 s->ext.session_ticket->data = NULL;
1073 typedef struct timeout_param_st {
1076 LHASH_OF(SSL_SESSION) *cache;
1079 static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
1081 if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
1083 * The reason we don't call SSL_CTX_remove_session() is to save on
1086 (void)lh_SSL_SESSION_delete(p->cache, s);
1087 SSL_SESSION_list_remove(p->ctx, s);
1088 s->not_resumable = 1;
1089 if (p->ctx->remove_session_cb != NULL)
1090 p->ctx->remove_session_cb(p->ctx, s);
1091 SSL_SESSION_free(s);
1095 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
1097 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1103 tp.cache = s->sessions;
1104 if (tp.cache == NULL)
1107 CRYPTO_THREAD_write_lock(s->lock);
1108 i = lh_SSL_SESSION_get_down_load(s->sessions);
1109 lh_SSL_SESSION_set_down_load(s->sessions, 0);
1110 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
1111 lh_SSL_SESSION_set_down_load(s->sessions, i);
1112 CRYPTO_THREAD_unlock(s->lock);
1115 int ssl_clear_bad_session(SSL *s)
1117 if ((s->session != NULL) &&
1118 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1119 !(SSL_in_init(s) || SSL_in_before(s))) {
1120 SSL_CTX_remove_session(s->session_ctx, s->session);
1126 /* locked by SSL_CTX in the calling function */
1127 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1129 if ((s->next == NULL) || (s->prev == NULL))
1132 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1133 /* last element in list */
1134 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1135 /* only one element in list */
1136 ctx->session_cache_head = NULL;
1137 ctx->session_cache_tail = NULL;
1139 ctx->session_cache_tail = s->prev;
1140 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1143 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1144 /* first element in list */
1145 ctx->session_cache_head = s->next;
1146 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1148 /* middle of list */
1149 s->next->prev = s->prev;
1150 s->prev->next = s->next;
1153 s->prev = s->next = NULL;
1156 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1158 if ((s->next != NULL) && (s->prev != NULL))
1159 SSL_SESSION_list_remove(ctx, s);
1161 if (ctx->session_cache_head == NULL) {
1162 ctx->session_cache_head = s;
1163 ctx->session_cache_tail = s;
1164 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1165 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1167 s->next = ctx->session_cache_head;
1169 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1170 ctx->session_cache_head = s;
1174 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1175 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
1177 ctx->new_session_cb = cb;
1180 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1181 return ctx->new_session_cb;
1184 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1185 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1187 ctx->remove_session_cb = cb;
1190 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1191 SSL_SESSION *sess) {
1192 return ctx->remove_session_cb;
1195 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1196 SSL_SESSION *(*cb) (struct ssl_st *ssl,
1197 const unsigned char *data,
1198 int len, int *copy))
1200 ctx->get_session_cb = cb;
1203 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1207 return ctx->get_session_cb;
1210 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1211 void (*cb) (const SSL *ssl, int type, int val))
1213 ctx->info_callback = cb;
1216 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1218 return ctx->info_callback;
1221 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1222 int (*cb) (SSL *ssl, X509 **x509,
1225 ctx->client_cert_cb = cb;
1228 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1230 return ctx->client_cert_cb;
1233 #ifndef OPENSSL_NO_ENGINE
1234 int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
1236 if (!ENGINE_init(e)) {
1237 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
1240 if (!ENGINE_get_ssl_client_cert_function(e)) {
1241 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
1242 SSL_R_NO_CLIENT_CERT_METHOD);
1246 ctx->client_cert_engine = e;
1251 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1252 int (*cb) (SSL *ssl,
1253 unsigned char *cookie,
1254 unsigned int *cookie_len))
1256 ctx->app_gen_cookie_cb = cb;
1259 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1260 int (*cb) (SSL *ssl,
1261 const unsigned char *cookie,
1262 unsigned int cookie_len))
1264 ctx->app_verify_cookie_cb = cb;
1267 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)