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/rand.h>
13 #include <openssl/engine.h>
14 #include "internal/refcount.h"
16 #include "statem/statem_locl.h"
18 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
19 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
20 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
23 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
24 * unlike in earlier protocol versions, the session ticket may not have been
25 * sent yet even though a handshake has finished. The session ticket data could
26 * come in sometime later...or even change if multiple session ticket messages
27 * are sent from the server. The preferred way for applications to obtain
28 * a resumable session is to use SSL_CTX_sess_set_new_cb().
31 SSL_SESSION *SSL_get_session(const SSL *ssl)
32 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
37 SSL_SESSION *SSL_get1_session(SSL *ssl)
38 /* variant of SSL_get_session: caller really gets something */
42 * Need to lock this all up rather than just use CRYPTO_add so that
43 * somebody doesn't free ssl->session between when we check it's non-null
44 * and when we up the reference count.
46 CRYPTO_THREAD_read_lock(ssl->lock);
49 SSL_SESSION_up_ref(sess);
50 CRYPTO_THREAD_unlock(ssl->lock);
54 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
56 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
59 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
61 return CRYPTO_get_ex_data(&s->ex_data, idx);
64 SSL_SESSION *SSL_SESSION_new(void)
68 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
71 ss = OPENSSL_zalloc(sizeof(*ss));
73 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
77 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
79 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
80 ss->time = (unsigned long)time(NULL);
81 ss->lock = CRYPTO_THREAD_lock_new();
82 if (ss->lock == NULL) {
83 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
88 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
89 CRYPTO_THREAD_lock_free(ss->lock);
96 SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *src)
98 return ssl_session_dup(src, 1);
102 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
103 * ticket == 0 then no ticket information is duplicated, otherwise it is.
105 SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
109 dest = OPENSSL_malloc(sizeof(*src));
113 memcpy(dest, src, sizeof(*dest));
116 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
117 * the case of an error whilst halfway through constructing dest
119 #ifndef OPENSSL_NO_PSK
120 dest->psk_identity_hint = NULL;
121 dest->psk_identity = NULL;
123 dest->ciphers = NULL;
124 dest->ext.hostname = NULL;
125 #ifndef OPENSSL_NO_EC
126 dest->ext.ecpointformats = NULL;
127 dest->ext.supportedgroups = NULL;
129 dest->ext.tick = NULL;
130 dest->ext.alpn_selected = NULL;
131 #ifndef OPENSSL_NO_SRP
132 dest->srp_username = NULL;
134 dest->peer_chain = NULL;
136 dest->ext.tick_nonce = NULL;
137 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
139 /* We deliberately don't copy the prev and next pointers */
143 dest->references = 1;
145 dest->lock = CRYPTO_THREAD_lock_new();
146 if (dest->lock == NULL)
149 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data))
152 if (src->peer != NULL) {
153 if (!X509_up_ref(src->peer))
155 dest->peer = src->peer;
158 if (src->peer_chain != NULL) {
159 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
160 if (dest->peer_chain == NULL)
163 #ifndef OPENSSL_NO_PSK
164 if (src->psk_identity_hint) {
165 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
166 if (dest->psk_identity_hint == NULL) {
170 if (src->psk_identity) {
171 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
172 if (dest->psk_identity == NULL) {
178 if (src->ciphers != NULL) {
179 dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
180 if (dest->ciphers == NULL)
184 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
185 &dest->ex_data, &src->ex_data)) {
189 if (src->ext.hostname) {
190 dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
191 if (dest->ext.hostname == NULL) {
195 #ifndef OPENSSL_NO_EC
196 if (src->ext.ecpointformats) {
197 dest->ext.ecpointformats =
198 OPENSSL_memdup(src->ext.ecpointformats,
199 src->ext.ecpointformats_len);
200 if (dest->ext.ecpointformats == NULL)
203 if (src->ext.supportedgroups) {
204 dest->ext.supportedgroups =
205 OPENSSL_memdup(src->ext.supportedgroups,
206 src->ext.supportedgroups_len);
207 if (dest->ext.supportedgroups == NULL)
212 if (ticket != 0 && src->ext.tick != NULL) {
214 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
215 if (dest->ext.tick == NULL)
218 dest->ext.tick_lifetime_hint = 0;
219 dest->ext.ticklen = 0;
222 if (src->ext.alpn_selected) {
223 dest->ext.alpn_selected =
224 (unsigned char*)OPENSSL_strndup((char*)src->ext.alpn_selected,
225 src->ext.alpn_selected_len);
226 if (dest->ext.alpn_selected == NULL) {
231 if (src->ext.tick_nonce != NULL) {
232 dest->ext.tick_nonce = OPENSSL_memdup(src->ext.tick_nonce,
233 src->ext.tick_nonce_len);
234 if (dest->ext.tick_nonce == NULL)
238 #ifndef OPENSSL_NO_SRP
239 if (src->srp_username) {
240 dest->srp_username = OPENSSL_strdup(src->srp_username);
241 if (dest->srp_username == NULL) {
249 SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
250 SSL_SESSION_free(dest);
254 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
257 *len = (unsigned int)s->session_id_length;
258 return s->session_id;
260 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
264 *len = (unsigned int)s->sid_ctx_length;
268 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
270 return s->compress_meth;
274 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
275 * the ID with random junk repeatedly until we have no conflict is going to
276 * complete in one iteration pretty much "most" of the time (btw:
277 * understatement). So, if it takes us 10 iterations and we still can't avoid
278 * a conflict - well that's a reasonable point to call it quits. Either the
279 * RAND code is broken or someone is trying to open roughly very close to
280 * 2^256 SSL sessions to our server. How you might store that many sessions
281 * is perhaps a more interesting question ...
284 #define MAX_SESS_ID_ATTEMPTS 10
285 static int def_generate_session_id(SSL *ssl, unsigned char *id,
286 unsigned int *id_len)
288 unsigned int retry = 0;
290 if (ssl_randbytes(ssl, id, *id_len) <= 0)
292 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
293 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
294 if (retry < MAX_SESS_ID_ATTEMPTS)
296 /* else - woops a session_id match */
298 * XXX We should also check the external cache -- but the probability of
299 * a collision is negligible, and we could not prevent the concurrent
300 * creation of sessions with identical IDs since we currently don't have
301 * means to atomically check whether a session ID already exists and make
302 * a reservation for it if it does not (this problem applies to the
303 * internal cache as well).
308 int ssl_generate_session_id(SSL *s, SSL_SESSION *ss)
311 GEN_SESSION_CB cb = def_generate_session_id;
313 switch (s->version) {
321 case DTLS1_2_VERSION:
322 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
325 SSLerr(SSL_F_SSL_GENERATE_SESSION_ID, SSL_R_UNSUPPORTED_SSL_VERSION);
330 * If RFC5077 ticket, use empty session ID (as server).
332 * (a) ssl_get_prev_session() does lookahead into the
333 * ClientHello extensions to find the session ticket.
334 * When ssl_get_prev_session() fails, statem_srvr.c calls
335 * ssl_get_new_session() in tls_process_client_hello().
336 * At that point, it has not yet parsed the extensions,
337 * however, because of the lookahead, it already knows
338 * whether a ticket is expected or not.
340 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
341 * ServerHello extensions, and before recording the session
342 * ID received from the server, so this block is a noop.
344 if (s->ext.ticket_expected) {
345 ss->session_id_length = 0;
349 /* Choose which callback will set the session ID */
350 CRYPTO_THREAD_read_lock(s->lock);
351 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
352 if (s->generate_session_id)
353 cb = s->generate_session_id;
354 else if (s->session_ctx->generate_session_id)
355 cb = s->session_ctx->generate_session_id;
356 CRYPTO_THREAD_unlock(s->session_ctx->lock);
357 CRYPTO_THREAD_unlock(s->lock);
358 /* Choose a session ID */
359 memset(ss->session_id, 0, ss->session_id_length);
360 tmp = (int)ss->session_id_length;
361 if (!cb(s, ss->session_id, &tmp)) {
362 /* The callback failed */
363 SSLerr(SSL_F_SSL_GENERATE_SESSION_ID,
364 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
368 * Don't allow the callback to set the session length to zero. nor
369 * set it higher than it was.
371 if (tmp == 0 || tmp > ss->session_id_length) {
372 /* The callback set an illegal length */
373 SSLerr(SSL_F_SSL_GENERATE_SESSION_ID,
374 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
377 ss->session_id_length = tmp;
378 /* Finally, check for a conflict */
379 if (SSL_has_matching_session_id(s, ss->session_id,
380 (unsigned int)ss->session_id_length)) {
381 SSLerr(SSL_F_SSL_GENERATE_SESSION_ID, SSL_R_SSL_SESSION_ID_CONFLICT);
388 int ssl_get_new_session(SSL *s, int session)
390 /* This gets used by clients and servers. */
392 SSL_SESSION *ss = NULL;
394 if ((ss = SSL_SESSION_new()) == NULL)
397 /* If the context has a default timeout, use it */
398 if (s->session_ctx->session_timeout == 0)
399 ss->timeout = SSL_get_default_timeout(s);
401 ss->timeout = s->session_ctx->session_timeout;
403 SSL_SESSION_free(s->session);
407 if (!ssl_generate_session_id(s, ss)) {
408 SSL_SESSION_free(ss);
412 if (s->ext.hostname) {
413 ss->ext.hostname = OPENSSL_strdup(s->ext.hostname);
414 if (ss->ext.hostname == NULL) {
415 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
416 SSL_SESSION_free(ss);
421 ss->session_id_length = 0;
424 if (s->sid_ctx_length > sizeof ss->sid_ctx) {
425 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
426 SSL_SESSION_free(ss);
429 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
430 ss->sid_ctx_length = s->sid_ctx_length;
432 ss->ssl_version = s->version;
433 ss->verify_result = X509_V_OK;
435 /* If client supports extended master secret set it in session */
436 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
437 ss->flags |= SSL_SESS_FLAG_EXTMS;
443 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
444 * connection. It is only called by servers.
446 * hello: The parsed ClientHello data
450 * 0: no session found
451 * 1: a session may have been found.
454 * - If a session is found then s->session is pointed at it (after freeing an
455 * existing session if need be) and s->verify_result is set from the session.
456 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
457 * if the server should issue a new session ticket (to 0 otherwise).
459 int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello, int *al)
461 /* This is used only by servers. */
463 SSL_SESSION *ret = NULL;
464 int fatal = 0, discard;
465 int try_session_cache = 0;
468 if (SSL_IS_TLS13(s)) {
469 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
470 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
472 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
473 hello->pre_proc_exts, NULL, 0, al))
478 /* sets s->ext.ticket_expected */
479 r = tls_get_ticket_from_client(s, hello, &ret);
481 case TICKET_FATAL_ERR_MALLOC:
482 case TICKET_FATAL_ERR_OTHER:
487 if (hello->session_id_len > 0)
488 try_session_cache = 1;
490 case TICKET_NO_DECRYPT:
492 case TICKET_SUCCESS_RENEW:
497 if (try_session_cache &&
499 !(s->session_ctx->session_cache_mode &
500 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
503 data.ssl_version = s->version;
504 memcpy(data.session_id, hello->session_id, hello->session_id_len);
505 data.session_id_length = hello->session_id_len;
507 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
508 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
510 /* don't allow other threads to steal it: */
511 SSL_SESSION_up_ref(ret);
513 CRYPTO_THREAD_unlock(s->session_ctx->lock);
515 CRYPTO_atomic_add(&s->session_ctx->stats.sess_miss, 1, &discard,
516 s->session_ctx->lock);
519 if (try_session_cache &&
520 ret == NULL && s->session_ctx->get_session_cb != NULL) {
523 ret = s->session_ctx->get_session_cb(s, hello->session_id,
524 hello->session_id_len,
528 CRYPTO_atomic_add(&s->session_ctx->stats.sess_cb_hit, 1, &discard,
529 s->session_ctx->lock);
532 * Increment reference count now if the session callback asks us
533 * to do so (note that if the session structures returned by the
534 * callback are shared between threads, it must handle the
535 * reference count itself [i.e. copy == 0], or things won't be
539 SSL_SESSION_up_ref(ret);
542 * Add the externally cached session to the internal cache as
543 * well if and only if we are supposed to.
546 (s->session_ctx->session_cache_mode &
547 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
549 * Either return value of SSL_CTX_add_session should not
550 * interrupt the session resumption process. The return
551 * value is intentionally ignored.
553 SSL_CTX_add_session(s->session_ctx, ret);
561 /* Now ret is non-NULL and we own one of its reference counts. */
563 /* Check TLS version consistency */
564 if (ret->ssl_version != s->version)
567 if (ret->sid_ctx_length != s->sid_ctx_length
568 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
570 * We have the session requested by the client, but we don't want to
571 * use it in this context.
573 goto err; /* treat like cache miss */
576 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
578 * We can't be sure if this session is being used out of context,
579 * which is especially important for SSL_VERIFY_PEER. The application
580 * should have used SSL[_CTX]_set_session_id_context. For this error
581 * case, we generate an error instead of treating the event like a
582 * cache miss (otherwise it would be easy for applications to
583 * effectively disable the session cache by accident without anyone
587 SSLerr(SSL_F_SSL_GET_PREV_SESSION,
588 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
593 if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
594 CRYPTO_atomic_add(&s->session_ctx->stats.sess_timeout, 1, &discard,
595 s->session_ctx->lock);
596 if (try_session_cache) {
597 /* session was from the cache, so remove it */
598 SSL_CTX_remove_session(s->session_ctx, ret);
603 /* Check extended master secret extension consistency */
604 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
605 /* If old session includes extms, but new does not: abort handshake */
606 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
607 SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS);
608 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
612 } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
613 /* If new session includes extms, but old does not: do not resume */
617 if (!SSL_IS_TLS13(s)) {
618 /* We already did this for TLS1.3 */
619 SSL_SESSION_free(s->session);
623 CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
624 s->session_ctx->lock);
625 s->verify_result = s->session->verify_result;
630 SSL_SESSION_free(ret);
631 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
635 if (!try_session_cache) {
637 * The session was from a ticket, so we should issue a ticket for
640 s->ext.ticket_expected = 1;
644 *al = SSL_AD_INTERNAL_ERROR;
651 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
653 int ret = 0, discard;
657 * add just 1 reference count for the SSL_CTX's session cache even though
658 * it has two ways of access: each session is in a doubly linked list and
661 SSL_SESSION_up_ref(c);
663 * if session c is in already in cache, we take back the increment later
666 CRYPTO_THREAD_write_lock(ctx->lock);
667 s = lh_SSL_SESSION_insert(ctx->sessions, c);
670 * s != NULL iff we already had a session with the given PID. In this
671 * case, s == c should hold (then we did not really modify
672 * ctx->sessions), or we're in trouble.
674 if (s != NULL && s != c) {
675 /* We *are* in trouble ... */
676 SSL_SESSION_list_remove(ctx, s);
679 * ... so pretend the other session did not exist in cache (we cannot
680 * handle two SSL_SESSION structures with identical session ID in the
681 * same cache, which could happen e.g. when two threads concurrently
682 * obtain the same session from an external cache)
685 } else if (s == NULL &&
686 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
687 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
690 * ... so take back the extra reference and also don't add
691 * the session to the SSL_SESSION_list at this time
696 /* Put at the head of the queue unless it is already in the cache */
698 SSL_SESSION_list_add(ctx, c);
702 * existing cache entry -- decrement previously incremented reference
703 * count because it already takes into account the cache
706 SSL_SESSION_free(s); /* s == c */
710 * new cache entry -- remove old ones if cache has become too large
715 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
716 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
717 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
720 CRYPTO_atomic_add(&ctx->stats.sess_cache_full, 1, &discard,
725 CRYPTO_THREAD_unlock(ctx->lock);
729 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
731 return remove_session_lock(ctx, c, 1);
734 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
739 if ((c != NULL) && (c->session_id_length != 0)) {
741 CRYPTO_THREAD_write_lock(ctx->lock);
742 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
744 r = lh_SSL_SESSION_delete(ctx->sessions, c);
745 SSL_SESSION_list_remove(ctx, c);
747 c->not_resumable = 1;
750 CRYPTO_THREAD_unlock(ctx->lock);
755 if (ctx->remove_session_cb != NULL)
756 ctx->remove_session_cb(ctx, c);
762 void SSL_SESSION_free(SSL_SESSION *ss)
769 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
770 REF_PRINT_COUNT("SSL_SESSION", ss);
773 REF_ASSERT_ISNT(i < 0);
775 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
777 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
778 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
780 sk_X509_pop_free(ss->peer_chain, X509_free);
781 sk_SSL_CIPHER_free(ss->ciphers);
782 OPENSSL_free(ss->ext.hostname);
783 OPENSSL_free(ss->ext.tick);
784 #ifndef OPENSSL_NO_EC
785 OPENSSL_free(ss->ext.ecpointformats);
786 ss->ext.ecpointformats = NULL;
787 ss->ext.ecpointformats_len = 0;
788 OPENSSL_free(ss->ext.supportedgroups);
789 ss->ext.supportedgroups = NULL;
790 ss->ext.supportedgroups_len = 0;
791 #endif /* OPENSSL_NO_EC */
792 #ifndef OPENSSL_NO_PSK
793 OPENSSL_free(ss->psk_identity_hint);
794 OPENSSL_free(ss->psk_identity);
796 #ifndef OPENSSL_NO_SRP
797 OPENSSL_free(ss->srp_username);
799 OPENSSL_free(ss->ext.alpn_selected);
800 OPENSSL_free(ss->ext.tick_nonce);
801 CRYPTO_THREAD_lock_free(ss->lock);
802 OPENSSL_clear_free(ss, sizeof(*ss));
805 int SSL_SESSION_up_ref(SSL_SESSION *ss)
809 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
812 REF_PRINT_COUNT("SSL_SESSION", ss);
813 REF_ASSERT_ISNT(i < 2);
814 return ((i > 1) ? 1 : 0);
817 int SSL_set_session(SSL *s, SSL_SESSION *session)
819 ssl_clear_bad_session(s);
820 if (s->ctx->method != s->method) {
821 if (!SSL_set_ssl_method(s, s->ctx->method))
825 if (session != NULL) {
826 SSL_SESSION_up_ref(session);
827 s->verify_result = session->verify_result;
829 SSL_SESSION_free(s->session);
830 s->session = session;
835 int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
836 unsigned int sid_len)
838 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
839 SSLerr(SSL_F_SSL_SESSION_SET1_ID,
840 SSL_R_SSL_SESSION_ID_TOO_LONG);
843 s->session_id_length = sid_len;
844 if (sid != s->session_id)
845 memcpy(s->session_id, sid, sid_len);
849 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
857 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
864 long SSL_SESSION_get_time(const SSL_SESSION *s)
871 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
879 int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
881 return s->ssl_version;
884 int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
886 s->ssl_version = version;
890 const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
895 int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
901 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
903 return s->ext.hostname;
906 int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
908 OPENSSL_free(s->ext.hostname);
909 if (hostname == NULL) {
910 s->ext.hostname = NULL;
913 s->ext.hostname = OPENSSL_strdup(hostname);
915 return s->ext.hostname != NULL;
918 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
920 return (s->ext.ticklen > 0) ? 1 : 0;
923 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
925 return s->ext.tick_lifetime_hint;
928 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
931 *len = s->ext.ticklen;
936 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
938 return s->ext.max_early_data;
941 int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
943 s->ext.max_early_data = max_early_data;
948 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
949 const unsigned char **alpn,
952 *alpn = s->ext.alpn_selected;
953 *len = s->ext.alpn_selected_len;
956 int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
959 OPENSSL_free(s->ext.alpn_selected);
960 if (alpn == NULL || len == 0) {
961 s->ext.alpn_selected = NULL;
962 s->ext.alpn_selected_len = 0;
965 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
966 if (s->ext.alpn_selected == NULL) {
967 s->ext.alpn_selected_len = 0;
970 s->ext.alpn_selected_len = len;
975 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
980 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
981 unsigned int sid_ctx_len)
983 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
984 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
985 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
988 s->sid_ctx_length = sid_ctx_len;
989 if (sid_ctx != s->sid_ctx)
990 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
995 int SSL_SESSION_is_resumable(const SSL_SESSION *s)
998 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1001 return !s->not_resumable
1002 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1005 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1010 l = s->session_timeout;
1011 s->session_timeout = t;
1015 long SSL_CTX_get_timeout(const SSL_CTX *s)
1019 return s->session_timeout;
1022 int SSL_set_session_secret_cb(SSL *s,
1023 tls_session_secret_cb_fn tls_session_secret_cb,
1028 s->ext.session_secret_cb = tls_session_secret_cb;
1029 s->ext.session_secret_cb_arg = arg;
1033 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1038 s->ext.session_ticket_cb = cb;
1039 s->ext.session_ticket_cb_arg = arg;
1043 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1045 if (s->version >= TLS1_VERSION) {
1046 OPENSSL_free(s->ext.session_ticket);
1047 s->ext.session_ticket = NULL;
1048 s->ext.session_ticket =
1049 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1050 if (s->ext.session_ticket == NULL) {
1051 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1055 if (ext_data != NULL) {
1056 s->ext.session_ticket->length = ext_len;
1057 s->ext.session_ticket->data = s->ext.session_ticket + 1;
1058 memcpy(s->ext.session_ticket->data, ext_data, ext_len);
1060 s->ext.session_ticket->length = 0;
1061 s->ext.session_ticket->data = NULL;
1070 typedef struct timeout_param_st {
1073 LHASH_OF(SSL_SESSION) *cache;
1076 static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
1078 if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
1080 * The reason we don't call SSL_CTX_remove_session() is to save on
1083 (void)lh_SSL_SESSION_delete(p->cache, s);
1084 SSL_SESSION_list_remove(p->ctx, s);
1085 s->not_resumable = 1;
1086 if (p->ctx->remove_session_cb != NULL)
1087 p->ctx->remove_session_cb(p->ctx, s);
1088 SSL_SESSION_free(s);
1092 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
1094 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1100 tp.cache = s->sessions;
1101 if (tp.cache == NULL)
1104 CRYPTO_THREAD_write_lock(s->lock);
1105 i = lh_SSL_SESSION_get_down_load(s->sessions);
1106 lh_SSL_SESSION_set_down_load(s->sessions, 0);
1107 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
1108 lh_SSL_SESSION_set_down_load(s->sessions, i);
1109 CRYPTO_THREAD_unlock(s->lock);
1112 int ssl_clear_bad_session(SSL *s)
1114 if ((s->session != NULL) &&
1115 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1116 !(SSL_in_init(s) || SSL_in_before(s))) {
1117 SSL_CTX_remove_session(s->session_ctx, s->session);
1123 /* locked by SSL_CTX in the calling function */
1124 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1126 if ((s->next == NULL) || (s->prev == NULL))
1129 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1130 /* last element in list */
1131 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1132 /* only one element in list */
1133 ctx->session_cache_head = NULL;
1134 ctx->session_cache_tail = NULL;
1136 ctx->session_cache_tail = s->prev;
1137 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1140 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1141 /* first element in list */
1142 ctx->session_cache_head = s->next;
1143 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1145 /* middle of list */
1146 s->next->prev = s->prev;
1147 s->prev->next = s->next;
1150 s->prev = s->next = NULL;
1153 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1155 if ((s->next != NULL) && (s->prev != NULL))
1156 SSL_SESSION_list_remove(ctx, s);
1158 if (ctx->session_cache_head == NULL) {
1159 ctx->session_cache_head = s;
1160 ctx->session_cache_tail = s;
1161 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1162 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1164 s->next = ctx->session_cache_head;
1166 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1167 ctx->session_cache_head = s;
1171 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1172 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
1174 ctx->new_session_cb = cb;
1177 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1178 return ctx->new_session_cb;
1181 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1182 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1184 ctx->remove_session_cb = cb;
1187 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1188 SSL_SESSION *sess) {
1189 return ctx->remove_session_cb;
1192 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1193 SSL_SESSION *(*cb) (struct ssl_st *ssl,
1194 const unsigned char *data,
1195 int len, int *copy))
1197 ctx->get_session_cb = cb;
1200 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1204 return ctx->get_session_cb;
1207 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1208 void (*cb) (const SSL *ssl, int type, int val))
1210 ctx->info_callback = cb;
1213 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1215 return ctx->info_callback;
1218 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1219 int (*cb) (SSL *ssl, X509 **x509,
1222 ctx->client_cert_cb = cb;
1225 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1227 return ctx->client_cert_cb;
1230 #ifndef OPENSSL_NO_ENGINE
1231 int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
1233 if (!ENGINE_init(e)) {
1234 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
1237 if (!ENGINE_get_ssl_client_cert_function(e)) {
1238 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
1239 SSL_R_NO_CLIENT_CERT_METHOD);
1243 ctx->client_cert_engine = e;
1248 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1249 int (*cb) (SSL *ssl,
1250 unsigned char *cookie,
1251 unsigned int *cookie_len))
1253 ctx->app_gen_cookie_cb = cb;
1256 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1257 int (*cb) (SSL *ssl,
1258 const unsigned char *cookie,
1259 unsigned int cookie_len))
1261 ctx->app_verify_cookie_cb = cb;
1264 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)