2 * Copyright 1995-2018 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
12 #include <openssl/rand.h>
13 #include <openssl/engine.h>
14 #include "internal/refcount.h"
15 #include "internal/cryptlib.h"
16 #include "ssl_local.h"
17 #include "statem/statem_local.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 */
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(const 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(const 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
->ext
.hostname
= NULL
;
125 dest
->ext
.tick
= NULL
;
126 dest
->ext
.alpn_selected
= NULL
;
127 #ifndef OPENSSL_NO_SRP
128 dest
->srp_username
= NULL
;
130 dest
->peer_chain
= NULL
;
132 dest
->ticket_appdata
= NULL
;
133 memset(&dest
->ex_data
, 0, sizeof(dest
->ex_data
));
135 /* We deliberately don't copy the prev and next pointers */
139 dest
->references
= 1;
141 dest
->lock
= CRYPTO_THREAD_lock_new();
142 if (dest
->lock
== NULL
)
145 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, dest
, &dest
->ex_data
))
148 if (src
->peer
!= NULL
) {
149 if (!X509_up_ref(src
->peer
))
151 dest
->peer
= src
->peer
;
154 if (src
->peer_chain
!= NULL
) {
155 dest
->peer_chain
= X509_chain_up_ref(src
->peer_chain
);
156 if (dest
->peer_chain
== NULL
)
159 #ifndef OPENSSL_NO_PSK
160 if (src
->psk_identity_hint
) {
161 dest
->psk_identity_hint
= OPENSSL_strdup(src
->psk_identity_hint
);
162 if (dest
->psk_identity_hint
== NULL
) {
166 if (src
->psk_identity
) {
167 dest
->psk_identity
= OPENSSL_strdup(src
->psk_identity
);
168 if (dest
->psk_identity
== NULL
) {
174 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
,
175 &dest
->ex_data
, &src
->ex_data
)) {
179 if (src
->ext
.hostname
) {
180 dest
->ext
.hostname
= OPENSSL_strdup(src
->ext
.hostname
);
181 if (dest
->ext
.hostname
== NULL
) {
186 if (ticket
!= 0 && src
->ext
.tick
!= NULL
) {
188 OPENSSL_memdup(src
->ext
.tick
, src
->ext
.ticklen
);
189 if (dest
->ext
.tick
== NULL
)
192 dest
->ext
.tick_lifetime_hint
= 0;
193 dest
->ext
.ticklen
= 0;
196 if (src
->ext
.alpn_selected
!= NULL
) {
197 dest
->ext
.alpn_selected
= OPENSSL_memdup(src
->ext
.alpn_selected
,
198 src
->ext
.alpn_selected_len
);
199 if (dest
->ext
.alpn_selected
== NULL
)
203 #ifndef OPENSSL_NO_SRP
204 if (src
->srp_username
) {
205 dest
->srp_username
= OPENSSL_strdup(src
->srp_username
);
206 if (dest
->srp_username
== NULL
) {
212 if (src
->ticket_appdata
!= NULL
) {
213 dest
->ticket_appdata
=
214 OPENSSL_memdup(src
->ticket_appdata
, src
->ticket_appdata_len
);
215 if (dest
->ticket_appdata
== NULL
)
221 SSLerr(SSL_F_SSL_SESSION_DUP
, ERR_R_MALLOC_FAILURE
);
222 SSL_SESSION_free(dest
);
226 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION
*s
, unsigned int *len
)
229 *len
= (unsigned int)s
->session_id_length
;
230 return s
->session_id
;
232 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION
*s
,
236 *len
= (unsigned int)s
->sid_ctx_length
;
240 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION
*s
)
242 return s
->compress_meth
;
246 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
247 * the ID with random junk repeatedly until we have no conflict is going to
248 * complete in one iteration pretty much "most" of the time (btw:
249 * understatement). So, if it takes us 10 iterations and we still can't avoid
250 * a conflict - well that's a reasonable point to call it quits. Either the
251 * RAND code is broken or someone is trying to open roughly very close to
252 * 2^256 SSL sessions to our server. How you might store that many sessions
253 * is perhaps a more interesting question ...
256 #define MAX_SESS_ID_ATTEMPTS 10
257 static int def_generate_session_id(SSL
*ssl
, unsigned char *id
,
258 unsigned int *id_len
)
260 unsigned int retry
= 0;
262 if (RAND_bytes(id
, *id_len
) <= 0)
264 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
265 (++retry
< MAX_SESS_ID_ATTEMPTS
)) ;
266 if (retry
< MAX_SESS_ID_ATTEMPTS
)
268 /* else - woops a session_id match */
270 * XXX We should also check the external cache -- but the probability of
271 * a collision is negligible, and we could not prevent the concurrent
272 * creation of sessions with identical IDs since we currently don't have
273 * means to atomically check whether a session ID already exists and make
274 * a reservation for it if it does not (this problem applies to the
275 * internal cache as well).
280 int ssl_generate_session_id(SSL
*s
, SSL_SESSION
*ss
)
283 GEN_SESSION_CB cb
= def_generate_session_id
;
285 switch (s
->version
) {
293 case DTLS1_2_VERSION
:
294 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
297 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
298 SSL_R_UNSUPPORTED_SSL_VERSION
);
303 * If RFC5077 ticket, use empty session ID (as server).
305 * (a) ssl_get_prev_session() does lookahead into the
306 * ClientHello extensions to find the session ticket.
307 * When ssl_get_prev_session() fails, statem_srvr.c calls
308 * ssl_get_new_session() in tls_process_client_hello().
309 * At that point, it has not yet parsed the extensions,
310 * however, because of the lookahead, it already knows
311 * whether a ticket is expected or not.
313 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
314 * ServerHello extensions, and before recording the session
315 * ID received from the server, so this block is a noop.
317 if (s
->ext
.ticket_expected
) {
318 ss
->session_id_length
= 0;
322 /* Choose which callback will set the session ID */
323 CRYPTO_THREAD_read_lock(s
->lock
);
324 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
325 if (s
->generate_session_id
)
326 cb
= s
->generate_session_id
;
327 else if (s
->session_ctx
->generate_session_id
)
328 cb
= s
->session_ctx
->generate_session_id
;
329 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
330 CRYPTO_THREAD_unlock(s
->lock
);
331 /* Choose a session ID */
332 memset(ss
->session_id
, 0, ss
->session_id_length
);
333 tmp
= (int)ss
->session_id_length
;
334 if (!cb(s
, ss
->session_id
, &tmp
)) {
335 /* The callback failed */
336 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
337 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED
);
341 * Don't allow the callback to set the session length to zero. nor
342 * set it higher than it was.
344 if (tmp
== 0 || tmp
> ss
->session_id_length
) {
345 /* The callback set an illegal length */
346 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
347 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH
);
350 ss
->session_id_length
= tmp
;
351 /* Finally, check for a conflict */
352 if (SSL_has_matching_session_id(s
, ss
->session_id
,
353 (unsigned int)ss
->session_id_length
)) {
354 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
355 SSL_R_SSL_SESSION_ID_CONFLICT
);
362 int ssl_get_new_session(SSL
*s
, int session
)
364 /* This gets used by clients and servers. */
366 SSL_SESSION
*ss
= NULL
;
368 if ((ss
= SSL_SESSION_new()) == NULL
) {
369 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_NEW_SESSION
,
370 ERR_R_MALLOC_FAILURE
);
374 /* If the context has a default timeout, use it */
375 if (s
->session_ctx
->session_timeout
== 0)
376 ss
->timeout
= SSL_get_default_timeout(s
);
378 ss
->timeout
= s
->session_ctx
->session_timeout
;
380 SSL_SESSION_free(s
->session
);
384 if (SSL_IS_TLS13(s
)) {
386 * We generate the session id while constructing the
387 * NewSessionTicket in TLSv1.3.
389 ss
->session_id_length
= 0;
390 } else if (!ssl_generate_session_id(s
, ss
)) {
391 /* SSLfatal() already called */
392 SSL_SESSION_free(ss
);
397 ss
->session_id_length
= 0;
400 if (s
->sid_ctx_length
> sizeof(ss
->sid_ctx
)) {
401 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_NEW_SESSION
,
402 ERR_R_INTERNAL_ERROR
);
403 SSL_SESSION_free(ss
);
406 memcpy(ss
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
);
407 ss
->sid_ctx_length
= s
->sid_ctx_length
;
409 ss
->ssl_version
= s
->version
;
410 ss
->verify_result
= X509_V_OK
;
412 /* If client supports extended master secret set it in session */
413 if (s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
)
414 ss
->flags
|= SSL_SESS_FLAG_EXTMS
;
419 SSL_SESSION
*lookup_sess_in_cache(SSL
*s
, const unsigned char *sess_id
,
422 SSL_SESSION
*ret
= NULL
;
424 if ((s
->session_ctx
->session_cache_mode
425 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP
) == 0) {
428 data
.ssl_version
= s
->version
;
429 if (!ossl_assert(sess_id_len
<= SSL_MAX_SSL_SESSION_ID_LENGTH
))
432 memcpy(data
.session_id
, sess_id
, sess_id_len
);
433 data
.session_id_length
= sess_id_len
;
435 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
436 ret
= lh_SSL_SESSION_retrieve(s
->session_ctx
->sessions
, &data
);
438 /* don't allow other threads to steal it: */
439 SSL_SESSION_up_ref(ret
);
441 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
443 tsan_counter(&s
->session_ctx
->stats
.sess_miss
);
446 if (ret
== NULL
&& s
->session_ctx
->get_session_cb
!= NULL
) {
449 ret
= s
->session_ctx
->get_session_cb(s
, sess_id
, sess_id_len
, ©
);
452 tsan_counter(&s
->session_ctx
->stats
.sess_cb_hit
);
455 * Increment reference count now if the session callback asks us
456 * to do so (note that if the session structures returned by the
457 * callback are shared between threads, it must handle the
458 * reference count itself [i.e. copy == 0], or things won't be
462 SSL_SESSION_up_ref(ret
);
465 * Add the externally cached session to the internal cache as
466 * well if and only if we are supposed to.
468 if ((s
->session_ctx
->session_cache_mode
&
469 SSL_SESS_CACHE_NO_INTERNAL_STORE
) == 0) {
471 * Either return value of SSL_CTX_add_session should not
472 * interrupt the session resumption process. The return
473 * value is intentionally ignored.
475 (void)SSL_CTX_add_session(s
->session_ctx
, ret
);
484 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
485 * connection. It is only called by servers.
487 * hello: The parsed ClientHello data
491 * 0: no session found
492 * 1: a session may have been found.
495 * - If a session is found then s->session is pointed at it (after freeing an
496 * existing session if need be) and s->verify_result is set from the session.
497 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
498 * if the server should issue a new session ticket (to 0 otherwise).
500 int ssl_get_prev_session(SSL
*s
, CLIENTHELLO_MSG
*hello
)
502 /* This is used only by servers. */
504 SSL_SESSION
*ret
= NULL
;
506 int try_session_cache
= 0;
509 if (SSL_IS_TLS13(s
)) {
511 * By default we will send a new ticket. This can be overridden in the
514 s
->ext
.ticket_expected
= 1;
515 if (!tls_parse_extension(s
, TLSEXT_IDX_psk_kex_modes
,
516 SSL_EXT_CLIENT_HELLO
, hello
->pre_proc_exts
,
518 || !tls_parse_extension(s
, TLSEXT_IDX_psk
, SSL_EXT_CLIENT_HELLO
,
519 hello
->pre_proc_exts
, NULL
, 0))
524 /* sets s->ext.ticket_expected */
525 r
= tls_get_ticket_from_client(s
, hello
, &ret
);
527 case SSL_TICKET_FATAL_ERR_MALLOC
:
528 case SSL_TICKET_FATAL_ERR_OTHER
:
530 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_PREV_SESSION
,
531 ERR_R_INTERNAL_ERROR
);
533 case SSL_TICKET_NONE
:
534 case SSL_TICKET_EMPTY
:
535 if (hello
->session_id_len
> 0) {
536 try_session_cache
= 1;
537 ret
= lookup_sess_in_cache(s
, hello
->session_id
,
538 hello
->session_id_len
);
541 case SSL_TICKET_NO_DECRYPT
:
542 case SSL_TICKET_SUCCESS
:
543 case SSL_TICKET_SUCCESS_RENEW
:
551 /* Now ret is non-NULL and we own one of its reference counts. */
553 /* Check TLS version consistency */
554 if (ret
->ssl_version
!= s
->version
)
557 if (ret
->sid_ctx_length
!= s
->sid_ctx_length
558 || memcmp(ret
->sid_ctx
, s
->sid_ctx
, ret
->sid_ctx_length
)) {
560 * We have the session requested by the client, but we don't want to
561 * use it in this context.
563 goto err
; /* treat like cache miss */
566 if ((s
->verify_mode
& SSL_VERIFY_PEER
) && s
->sid_ctx_length
== 0) {
568 * We can't be sure if this session is being used out of context,
569 * which is especially important for SSL_VERIFY_PEER. The application
570 * should have used SSL[_CTX]_set_session_id_context. For this error
571 * case, we generate an error instead of treating the event like a
572 * cache miss (otherwise it would be easy for applications to
573 * effectively disable the session cache by accident without anyone
577 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_PREV_SESSION
,
578 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED
);
583 if (ret
->timeout
< (long)(time(NULL
) - ret
->time
)) { /* timeout */
584 tsan_counter(&s
->session_ctx
->stats
.sess_timeout
);
585 if (try_session_cache
) {
586 /* session was from the cache, so remove it */
587 SSL_CTX_remove_session(s
->session_ctx
, ret
);
592 /* Check extended master secret extension consistency */
593 if (ret
->flags
& SSL_SESS_FLAG_EXTMS
) {
594 /* If old session includes extms, but new does not: abort handshake */
595 if (!(s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
)) {
596 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_SSL_GET_PREV_SESSION
,
597 SSL_R_INCONSISTENT_EXTMS
);
601 } else if (s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
) {
602 /* If new session includes extms, but old does not: do not resume */
606 if (!SSL_IS_TLS13(s
)) {
607 /* We already did this for TLS1.3 */
608 SSL_SESSION_free(s
->session
);
612 tsan_counter(&s
->session_ctx
->stats
.sess_hit
);
613 s
->verify_result
= s
->session
->verify_result
;
618 SSL_SESSION_free(ret
);
619 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
623 if (!try_session_cache
) {
625 * The session was from a ticket, so we should issue a ticket for
628 s
->ext
.ticket_expected
= 1;
637 int SSL_CTX_add_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
643 * add just 1 reference count for the SSL_CTX's session cache even though
644 * it has two ways of access: each session is in a doubly linked list and
647 SSL_SESSION_up_ref(c
);
649 * if session c is in already in cache, we take back the increment later
652 CRYPTO_THREAD_write_lock(ctx
->lock
);
653 s
= lh_SSL_SESSION_insert(ctx
->sessions
, c
);
656 * s != NULL iff we already had a session with the given PID. In this
657 * case, s == c should hold (then we did not really modify
658 * ctx->sessions), or we're in trouble.
660 if (s
!= NULL
&& s
!= c
) {
661 /* We *are* in trouble ... */
662 SSL_SESSION_list_remove(ctx
, s
);
665 * ... so pretend the other session did not exist in cache (we cannot
666 * handle two SSL_SESSION structures with identical session ID in the
667 * same cache, which could happen e.g. when two threads concurrently
668 * obtain the same session from an external cache)
671 } else if (s
== NULL
&&
672 lh_SSL_SESSION_retrieve(ctx
->sessions
, c
) == NULL
) {
673 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
676 * ... so take back the extra reference and also don't add
677 * the session to the SSL_SESSION_list at this time
682 /* Put at the head of the queue unless it is already in the cache */
684 SSL_SESSION_list_add(ctx
, c
);
688 * existing cache entry -- decrement previously incremented reference
689 * count because it already takes into account the cache
692 SSL_SESSION_free(s
); /* s == c */
696 * new cache entry -- remove old ones if cache has become too large
701 if (SSL_CTX_sess_get_cache_size(ctx
) > 0) {
702 while (SSL_CTX_sess_number(ctx
) > SSL_CTX_sess_get_cache_size(ctx
)) {
703 if (!remove_session_lock(ctx
, ctx
->session_cache_tail
, 0))
706 tsan_counter(&ctx
->stats
.sess_cache_full
);
710 CRYPTO_THREAD_unlock(ctx
->lock
);
714 int SSL_CTX_remove_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
716 return remove_session_lock(ctx
, c
, 1);
719 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
)
724 if ((c
!= NULL
) && (c
->session_id_length
!= 0)) {
726 CRYPTO_THREAD_write_lock(ctx
->lock
);
727 if ((r
= lh_SSL_SESSION_retrieve(ctx
->sessions
, c
)) != NULL
) {
729 r
= lh_SSL_SESSION_delete(ctx
->sessions
, r
);
730 SSL_SESSION_list_remove(ctx
, r
);
732 c
->not_resumable
= 1;
735 CRYPTO_THREAD_unlock(ctx
->lock
);
737 if (ctx
->remove_session_cb
!= NULL
)
738 ctx
->remove_session_cb(ctx
, c
);
747 void SSL_SESSION_free(SSL_SESSION
*ss
)
753 CRYPTO_DOWN_REF(&ss
->references
, &i
, ss
->lock
);
754 REF_PRINT_COUNT("SSL_SESSION", ss
);
757 REF_ASSERT_ISNT(i
< 0);
759 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
);
761 OPENSSL_cleanse(ss
->master_key
, sizeof(ss
->master_key
));
762 OPENSSL_cleanse(ss
->session_id
, sizeof(ss
->session_id
));
764 sk_X509_pop_free(ss
->peer_chain
, X509_free
);
765 OPENSSL_free(ss
->ext
.hostname
);
766 OPENSSL_free(ss
->ext
.tick
);
767 #ifndef OPENSSL_NO_PSK
768 OPENSSL_free(ss
->psk_identity_hint
);
769 OPENSSL_free(ss
->psk_identity
);
771 #ifndef OPENSSL_NO_SRP
772 OPENSSL_free(ss
->srp_username
);
774 OPENSSL_free(ss
->ext
.alpn_selected
);
775 OPENSSL_free(ss
->ticket_appdata
);
776 CRYPTO_THREAD_lock_free(ss
->lock
);
777 OPENSSL_clear_free(ss
, sizeof(*ss
));
780 int SSL_SESSION_up_ref(SSL_SESSION
*ss
)
784 if (CRYPTO_UP_REF(&ss
->references
, &i
, ss
->lock
) <= 0)
787 REF_PRINT_COUNT("SSL_SESSION", ss
);
788 REF_ASSERT_ISNT(i
< 2);
789 return ((i
> 1) ? 1 : 0);
792 int SSL_set_session(SSL
*s
, SSL_SESSION
*session
)
794 ssl_clear_bad_session(s
);
795 if (s
->ctx
->method
!= s
->method
) {
796 if (!SSL_set_ssl_method(s
, s
->ctx
->method
))
800 if (session
!= NULL
) {
801 SSL_SESSION_up_ref(session
);
802 s
->verify_result
= session
->verify_result
;
804 SSL_SESSION_free(s
->session
);
805 s
->session
= session
;
810 int SSL_SESSION_set1_id(SSL_SESSION
*s
, const unsigned char *sid
,
811 unsigned int sid_len
)
813 if (sid_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
814 SSLerr(SSL_F_SSL_SESSION_SET1_ID
,
815 SSL_R_SSL_SESSION_ID_TOO_LONG
);
818 s
->session_id_length
= sid_len
;
819 if (sid
!= s
->session_id
)
820 memcpy(s
->session_id
, sid
, sid_len
);
824 long SSL_SESSION_set_timeout(SSL_SESSION
*s
, long t
)
832 long SSL_SESSION_get_timeout(const SSL_SESSION
*s
)
839 long SSL_SESSION_get_time(const SSL_SESSION
*s
)
846 long SSL_SESSION_set_time(SSL_SESSION
*s
, long t
)
854 int SSL_SESSION_get_protocol_version(const SSL_SESSION
*s
)
856 return s
->ssl_version
;
859 int SSL_SESSION_set_protocol_version(SSL_SESSION
*s
, int version
)
861 s
->ssl_version
= version
;
865 const SSL_CIPHER
*SSL_SESSION_get0_cipher(const SSL_SESSION
*s
)
870 int SSL_SESSION_set_cipher(SSL_SESSION
*s
, const SSL_CIPHER
*cipher
)
876 const char *SSL_SESSION_get0_hostname(const SSL_SESSION
*s
)
878 return s
->ext
.hostname
;
881 int SSL_SESSION_set1_hostname(SSL_SESSION
*s
, const char *hostname
)
883 OPENSSL_free(s
->ext
.hostname
);
884 if (hostname
== NULL
) {
885 s
->ext
.hostname
= NULL
;
888 s
->ext
.hostname
= OPENSSL_strdup(hostname
);
890 return s
->ext
.hostname
!= NULL
;
893 int SSL_SESSION_has_ticket(const SSL_SESSION
*s
)
895 return (s
->ext
.ticklen
> 0) ? 1 : 0;
898 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION
*s
)
900 return s
->ext
.tick_lifetime_hint
;
903 void SSL_SESSION_get0_ticket(const SSL_SESSION
*s
, const unsigned char **tick
,
906 *len
= s
->ext
.ticklen
;
911 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION
*s
)
913 return s
->ext
.max_early_data
;
916 int SSL_SESSION_set_max_early_data(SSL_SESSION
*s
, uint32_t max_early_data
)
918 s
->ext
.max_early_data
= max_early_data
;
923 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION
*s
,
924 const unsigned char **alpn
,
927 *alpn
= s
->ext
.alpn_selected
;
928 *len
= s
->ext
.alpn_selected_len
;
931 int SSL_SESSION_set1_alpn_selected(SSL_SESSION
*s
, const unsigned char *alpn
,
934 OPENSSL_free(s
->ext
.alpn_selected
);
935 if (alpn
== NULL
|| len
== 0) {
936 s
->ext
.alpn_selected
= NULL
;
937 s
->ext
.alpn_selected_len
= 0;
940 s
->ext
.alpn_selected
= OPENSSL_memdup(alpn
, len
);
941 if (s
->ext
.alpn_selected
== NULL
) {
942 s
->ext
.alpn_selected_len
= 0;
945 s
->ext
.alpn_selected_len
= len
;
950 X509
*SSL_SESSION_get0_peer(SSL_SESSION
*s
)
955 int SSL_SESSION_set1_id_context(SSL_SESSION
*s
, const unsigned char *sid_ctx
,
956 unsigned int sid_ctx_len
)
958 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
959 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT
,
960 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
963 s
->sid_ctx_length
= sid_ctx_len
;
964 if (sid_ctx
!= s
->sid_ctx
)
965 memcpy(s
->sid_ctx
, sid_ctx
, sid_ctx_len
);
970 int SSL_SESSION_is_resumable(const SSL_SESSION
*s
)
973 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
976 return !s
->not_resumable
977 && (s
->session_id_length
> 0 || s
->ext
.ticklen
> 0);
980 long SSL_CTX_set_timeout(SSL_CTX
*s
, long t
)
985 l
= s
->session_timeout
;
986 s
->session_timeout
= t
;
990 long SSL_CTX_get_timeout(const SSL_CTX
*s
)
994 return s
->session_timeout
;
997 int SSL_set_session_secret_cb(SSL
*s
,
998 tls_session_secret_cb_fn tls_session_secret_cb
,
1003 s
->ext
.session_secret_cb
= tls_session_secret_cb
;
1004 s
->ext
.session_secret_cb_arg
= arg
;
1008 int SSL_set_session_ticket_ext_cb(SSL
*s
, tls_session_ticket_ext_cb_fn cb
,
1013 s
->ext
.session_ticket_cb
= cb
;
1014 s
->ext
.session_ticket_cb_arg
= arg
;
1018 int SSL_set_session_ticket_ext(SSL
*s
, void *ext_data
, int ext_len
)
1020 if (s
->version
>= TLS1_VERSION
) {
1021 OPENSSL_free(s
->ext
.session_ticket
);
1022 s
->ext
.session_ticket
= NULL
;
1023 s
->ext
.session_ticket
=
1024 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT
) + ext_len
);
1025 if (s
->ext
.session_ticket
== NULL
) {
1026 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT
, ERR_R_MALLOC_FAILURE
);
1030 if (ext_data
!= NULL
) {
1031 s
->ext
.session_ticket
->length
= ext_len
;
1032 s
->ext
.session_ticket
->data
= s
->ext
.session_ticket
+ 1;
1033 memcpy(s
->ext
.session_ticket
->data
, ext_data
, ext_len
);
1035 s
->ext
.session_ticket
->length
= 0;
1036 s
->ext
.session_ticket
->data
= NULL
;
1045 typedef struct timeout_param_st
{
1048 LHASH_OF(SSL_SESSION
) *cache
;
1051 static void timeout_cb(SSL_SESSION
*s
, TIMEOUT_PARAM
*p
)
1053 if ((p
->time
== 0) || (p
->time
> (s
->time
+ s
->timeout
))) { /* timeout */
1055 * The reason we don't call SSL_CTX_remove_session() is to save on
1058 (void)lh_SSL_SESSION_delete(p
->cache
, s
);
1059 SSL_SESSION_list_remove(p
->ctx
, s
);
1060 s
->not_resumable
= 1;
1061 if (p
->ctx
->remove_session_cb
!= NULL
)
1062 p
->ctx
->remove_session_cb(p
->ctx
, s
);
1063 SSL_SESSION_free(s
);
1067 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION
, TIMEOUT_PARAM
);
1069 void SSL_CTX_flush_sessions(SSL_CTX
*s
, long t
)
1075 tp
.cache
= s
->sessions
;
1076 if (tp
.cache
== NULL
)
1079 CRYPTO_THREAD_write_lock(s
->lock
);
1080 i
= lh_SSL_SESSION_get_down_load(s
->sessions
);
1081 lh_SSL_SESSION_set_down_load(s
->sessions
, 0);
1082 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp
.cache
, timeout_cb
, &tp
);
1083 lh_SSL_SESSION_set_down_load(s
->sessions
, i
);
1084 CRYPTO_THREAD_unlock(s
->lock
);
1087 int ssl_clear_bad_session(SSL
*s
)
1089 if ((s
->session
!= NULL
) &&
1090 !(s
->shutdown
& SSL_SENT_SHUTDOWN
) &&
1091 !(SSL_in_init(s
) || SSL_in_before(s
))) {
1092 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1098 /* locked by SSL_CTX in the calling function */
1099 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1101 if ((s
->next
== NULL
) || (s
->prev
== NULL
))
1104 if (s
->next
== (SSL_SESSION
*)&(ctx
->session_cache_tail
)) {
1105 /* last element in list */
1106 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1107 /* only one element in list */
1108 ctx
->session_cache_head
= NULL
;
1109 ctx
->session_cache_tail
= NULL
;
1111 ctx
->session_cache_tail
= s
->prev
;
1112 s
->prev
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1115 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1116 /* first element in list */
1117 ctx
->session_cache_head
= s
->next
;
1118 s
->next
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1120 /* middle of list */
1121 s
->next
->prev
= s
->prev
;
1122 s
->prev
->next
= s
->next
;
1125 s
->prev
= s
->next
= NULL
;
1128 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1130 if ((s
->next
!= NULL
) && (s
->prev
!= NULL
))
1131 SSL_SESSION_list_remove(ctx
, s
);
1133 if (ctx
->session_cache_head
== NULL
) {
1134 ctx
->session_cache_head
= s
;
1135 ctx
->session_cache_tail
= s
;
1136 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1137 s
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1139 s
->next
= ctx
->session_cache_head
;
1141 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1142 ctx
->session_cache_head
= s
;
1146 void SSL_CTX_sess_set_new_cb(SSL_CTX
*ctx
,
1147 int (*cb
) (struct ssl_st
*ssl
, SSL_SESSION
*sess
))
1149 ctx
->new_session_cb
= cb
;
1152 int (*SSL_CTX_sess_get_new_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, SSL_SESSION
*sess
) {
1153 return ctx
->new_session_cb
;
1156 void SSL_CTX_sess_set_remove_cb(SSL_CTX
*ctx
,
1157 void (*cb
) (SSL_CTX
*ctx
, SSL_SESSION
*sess
))
1159 ctx
->remove_session_cb
= cb
;
1162 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX
*ctx
)) (SSL_CTX
*ctx
,
1163 SSL_SESSION
*sess
) {
1164 return ctx
->remove_session_cb
;
1167 void SSL_CTX_sess_set_get_cb(SSL_CTX
*ctx
,
1168 SSL_SESSION
*(*cb
) (struct ssl_st
*ssl
,
1169 const unsigned char *data
,
1170 int len
, int *copy
))
1172 ctx
->get_session_cb
= cb
;
1175 SSL_SESSION
*(*SSL_CTX_sess_get_get_cb(SSL_CTX
*ctx
)) (SSL
*ssl
,
1179 return ctx
->get_session_cb
;
1182 void SSL_CTX_set_info_callback(SSL_CTX
*ctx
,
1183 void (*cb
) (const SSL
*ssl
, int type
, int val
))
1185 ctx
->info_callback
= cb
;
1188 void (*SSL_CTX_get_info_callback(SSL_CTX
*ctx
)) (const SSL
*ssl
, int type
,
1190 return ctx
->info_callback
;
1193 void SSL_CTX_set_client_cert_cb(SSL_CTX
*ctx
,
1194 int (*cb
) (SSL
*ssl
, X509
**x509
,
1197 ctx
->client_cert_cb
= cb
;
1200 int (*SSL_CTX_get_client_cert_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, X509
**x509
,
1202 return ctx
->client_cert_cb
;
1205 #ifndef OPENSSL_NO_ENGINE
1206 int SSL_CTX_set_client_cert_engine(SSL_CTX
*ctx
, ENGINE
*e
)
1208 if (!ENGINE_init(e
)) {
1209 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
, ERR_R_ENGINE_LIB
);
1212 if (!ENGINE_get_ssl_client_cert_function(e
)) {
1213 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
,
1214 SSL_R_NO_CLIENT_CERT_METHOD
);
1218 ctx
->client_cert_engine
= e
;
1223 void SSL_CTX_set_cookie_generate_cb(SSL_CTX
*ctx
,
1224 int (*cb
) (SSL
*ssl
,
1225 unsigned char *cookie
,
1226 unsigned int *cookie_len
))
1228 ctx
->app_gen_cookie_cb
= cb
;
1231 void SSL_CTX_set_cookie_verify_cb(SSL_CTX
*ctx
,
1232 int (*cb
) (SSL
*ssl
,
1233 const unsigned char *cookie
,
1234 unsigned int cookie_len
))
1236 ctx
->app_verify_cookie_cb
= cb
;
1239 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION
*ss
, const void *data
, size_t len
)
1241 OPENSSL_free(ss
->ticket_appdata
);
1242 ss
->ticket_appdata_len
= 0;
1243 if (data
== NULL
|| len
== 0) {
1244 ss
->ticket_appdata
= NULL
;
1247 ss
->ticket_appdata
= OPENSSL_memdup(data
, len
);
1248 if (ss
->ticket_appdata
!= NULL
) {
1249 ss
->ticket_appdata_len
= len
;
1255 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION
*ss
, void **data
, size_t *len
)
1257 *data
= ss
->ticket_appdata
;
1258 *len
= ss
->ticket_appdata_len
;
1262 void SSL_CTX_set_stateless_cookie_generate_cb(
1264 int (*cb
) (SSL
*ssl
,
1265 unsigned char *cookie
,
1266 size_t *cookie_len
))
1268 ctx
->gen_stateless_cookie_cb
= cb
;
1271 void SSL_CTX_set_stateless_cookie_verify_cb(
1273 int (*cb
) (SSL
*ssl
,
1274 const unsigned char *cookie
,
1277 ctx
->verify_stateless_cookie_cb
= cb
;
1280 IMPLEMENT_PEM_rw(SSL_SESSION
, SSL_SESSION
, PEM_STRING_SSL_SESSION
, SSL_SESSION
)