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 dest
->ticket_appdata
= 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
) {
248 if (src
->ticket_appdata
!= NULL
) {
249 dest
->ticket_appdata
=
250 OPENSSL_memdup(src
->ticket_appdata
, src
->ticket_appdata_len
);
251 if (dest
->ticket_appdata
== NULL
)
257 SSLerr(SSL_F_SSL_SESSION_DUP
, ERR_R_MALLOC_FAILURE
);
258 SSL_SESSION_free(dest
);
262 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION
*s
, unsigned int *len
)
265 *len
= (unsigned int)s
->session_id_length
;
266 return s
->session_id
;
268 const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION
*s
,
272 *len
= (unsigned int)s
->sid_ctx_length
;
276 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION
*s
)
278 return s
->compress_meth
;
282 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
283 * the ID with random junk repeatedly until we have no conflict is going to
284 * complete in one iteration pretty much "most" of the time (btw:
285 * understatement). So, if it takes us 10 iterations and we still can't avoid
286 * a conflict - well that's a reasonable point to call it quits. Either the
287 * RAND code is broken or someone is trying to open roughly very close to
288 * 2^256 SSL sessions to our server. How you might store that many sessions
289 * is perhaps a more interesting question ...
292 #define MAX_SESS_ID_ATTEMPTS 10
293 static int def_generate_session_id(SSL
*ssl
, unsigned char *id
,
294 unsigned int *id_len
)
296 unsigned int retry
= 0;
298 if (ssl_randbytes(ssl
, id
, *id_len
) <= 0)
300 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
301 (++retry
< MAX_SESS_ID_ATTEMPTS
)) ;
302 if (retry
< MAX_SESS_ID_ATTEMPTS
)
304 /* else - woops a session_id match */
306 * XXX We should also check the external cache -- but the probability of
307 * a collision is negligible, and we could not prevent the concurrent
308 * creation of sessions with identical IDs since we currently don't have
309 * means to atomically check whether a session ID already exists and make
310 * a reservation for it if it does not (this problem applies to the
311 * internal cache as well).
316 int ssl_generate_session_id(SSL
*s
, SSL_SESSION
*ss
)
319 GEN_SESSION_CB cb
= def_generate_session_id
;
321 switch (s
->version
) {
329 case DTLS1_2_VERSION
:
330 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
333 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
334 SSL_R_UNSUPPORTED_SSL_VERSION
);
339 * If RFC5077 ticket, use empty session ID (as server).
341 * (a) ssl_get_prev_session() does lookahead into the
342 * ClientHello extensions to find the session ticket.
343 * When ssl_get_prev_session() fails, statem_srvr.c calls
344 * ssl_get_new_session() in tls_process_client_hello().
345 * At that point, it has not yet parsed the extensions,
346 * however, because of the lookahead, it already knows
347 * whether a ticket is expected or not.
349 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
350 * ServerHello extensions, and before recording the session
351 * ID received from the server, so this block is a noop.
353 if (s
->ext
.ticket_expected
) {
354 ss
->session_id_length
= 0;
358 /* Choose which callback will set the session ID */
359 CRYPTO_THREAD_read_lock(s
->lock
);
360 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
361 if (s
->generate_session_id
)
362 cb
= s
->generate_session_id
;
363 else if (s
->session_ctx
->generate_session_id
)
364 cb
= s
->session_ctx
->generate_session_id
;
365 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
366 CRYPTO_THREAD_unlock(s
->lock
);
367 /* Choose a session ID */
368 memset(ss
->session_id
, 0, ss
->session_id_length
);
369 tmp
= (int)ss
->session_id_length
;
370 if (!cb(s
, ss
->session_id
, &tmp
)) {
371 /* The callback failed */
372 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
373 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED
);
377 * Don't allow the callback to set the session length to zero. nor
378 * set it higher than it was.
380 if (tmp
== 0 || tmp
> ss
->session_id_length
) {
381 /* The callback set an illegal length */
382 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
383 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH
);
386 ss
->session_id_length
= tmp
;
387 /* Finally, check for a conflict */
388 if (SSL_has_matching_session_id(s
, ss
->session_id
,
389 (unsigned int)ss
->session_id_length
)) {
390 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GENERATE_SESSION_ID
,
391 SSL_R_SSL_SESSION_ID_CONFLICT
);
398 int ssl_get_new_session(SSL
*s
, int session
)
400 /* This gets used by clients and servers. */
402 SSL_SESSION
*ss
= NULL
;
404 if ((ss
= SSL_SESSION_new()) == NULL
) {
405 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_NEW_SESSION
,
406 ERR_R_MALLOC_FAILURE
);
410 /* If the context has a default timeout, use it */
411 if (s
->session_ctx
->session_timeout
== 0)
412 ss
->timeout
= SSL_get_default_timeout(s
);
414 ss
->timeout
= s
->session_ctx
->session_timeout
;
416 SSL_SESSION_free(s
->session
);
420 if (!ssl_generate_session_id(s
, ss
)) {
421 /* SSLfatal() already called */
422 SSL_SESSION_free(ss
);
426 if (s
->ext
.hostname
) {
427 ss
->ext
.hostname
= OPENSSL_strdup(s
->ext
.hostname
);
428 if (ss
->ext
.hostname
== NULL
) {
429 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_NEW_SESSION
,
430 ERR_R_INTERNAL_ERROR
);
431 SSL_SESSION_free(ss
);
436 ss
->session_id_length
= 0;
439 if (s
->sid_ctx_length
> sizeof(ss
->sid_ctx
)) {
440 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_NEW_SESSION
,
441 ERR_R_INTERNAL_ERROR
);
442 SSL_SESSION_free(ss
);
445 memcpy(ss
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
);
446 ss
->sid_ctx_length
= s
->sid_ctx_length
;
448 ss
->ssl_version
= s
->version
;
449 ss
->verify_result
= X509_V_OK
;
451 /* If client supports extended master secret set it in session */
452 if (s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
)
453 ss
->flags
|= SSL_SESS_FLAG_EXTMS
;
459 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
460 * connection. It is only called by servers.
462 * hello: The parsed ClientHello data
466 * 0: no session found
467 * 1: a session may have been found.
470 * - If a session is found then s->session is pointed at it (after freeing an
471 * existing session if need be) and s->verify_result is set from the session.
472 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
473 * if the server should issue a new session ticket (to 0 otherwise).
475 int ssl_get_prev_session(SSL
*s
, CLIENTHELLO_MSG
*hello
)
477 /* This is used only by servers. */
479 SSL_SESSION
*ret
= NULL
;
480 int fatal
= 0, discard
;
481 int try_session_cache
= 0;
484 if (SSL_IS_TLS13(s
)) {
485 if (!tls_parse_extension(s
, TLSEXT_IDX_psk_kex_modes
,
486 SSL_EXT_CLIENT_HELLO
, hello
->pre_proc_exts
,
488 || !tls_parse_extension(s
, TLSEXT_IDX_psk
, SSL_EXT_CLIENT_HELLO
,
489 hello
->pre_proc_exts
, NULL
, 0))
494 /* sets s->ext.ticket_expected */
495 r
= tls_get_ticket_from_client(s
, hello
, &ret
);
497 case SSL_TICKET_FATAL_ERR_MALLOC
:
498 case SSL_TICKET_FATAL_ERR_OTHER
:
500 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_PREV_SESSION
,
501 ERR_R_INTERNAL_ERROR
);
503 case SSL_TICKET_NONE
:
504 case SSL_TICKET_EMPTY
:
505 if (hello
->session_id_len
> 0)
506 try_session_cache
= 1;
508 case SSL_TICKET_NO_DECRYPT
:
509 case SSL_TICKET_SUCCESS
:
510 case SSL_TICKET_SUCCESS_RENEW
:
515 if (try_session_cache
&&
517 !(s
->session_ctx
->session_cache_mode
&
518 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP
)) {
521 data
.ssl_version
= s
->version
;
522 memcpy(data
.session_id
, hello
->session_id
, hello
->session_id_len
);
523 data
.session_id_length
= hello
->session_id_len
;
525 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
526 ret
= lh_SSL_SESSION_retrieve(s
->session_ctx
->sessions
, &data
);
528 /* don't allow other threads to steal it: */
529 SSL_SESSION_up_ref(ret
);
531 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
533 CRYPTO_atomic_add(&s
->session_ctx
->stats
.sess_miss
, 1, &discard
,
534 s
->session_ctx
->lock
);
537 if (try_session_cache
&&
538 ret
== NULL
&& s
->session_ctx
->get_session_cb
!= NULL
) {
541 ret
= s
->session_ctx
->get_session_cb(s
, hello
->session_id
,
542 hello
->session_id_len
,
546 CRYPTO_atomic_add(&s
->session_ctx
->stats
.sess_cb_hit
, 1, &discard
,
547 s
->session_ctx
->lock
);
550 * Increment reference count now if the session callback asks us
551 * to do so (note that if the session structures returned by the
552 * callback are shared between threads, it must handle the
553 * reference count itself [i.e. copy == 0], or things won't be
557 SSL_SESSION_up_ref(ret
);
560 * Add the externally cached session to the internal cache as
561 * well if and only if we are supposed to.
564 (s
->session_ctx
->session_cache_mode
&
565 SSL_SESS_CACHE_NO_INTERNAL_STORE
)) {
567 * Either return value of SSL_CTX_add_session should not
568 * interrupt the session resumption process. The return
569 * value is intentionally ignored.
571 SSL_CTX_add_session(s
->session_ctx
, ret
);
579 /* Now ret is non-NULL and we own one of its reference counts. */
581 /* Check TLS version consistency */
582 if (ret
->ssl_version
!= s
->version
)
585 if (ret
->sid_ctx_length
!= s
->sid_ctx_length
586 || memcmp(ret
->sid_ctx
, s
->sid_ctx
, ret
->sid_ctx_length
)) {
588 * We have the session requested by the client, but we don't want to
589 * use it in this context.
591 goto err
; /* treat like cache miss */
594 if ((s
->verify_mode
& SSL_VERIFY_PEER
) && s
->sid_ctx_length
== 0) {
596 * We can't be sure if this session is being used out of context,
597 * which is especially important for SSL_VERIFY_PEER. The application
598 * should have used SSL[_CTX]_set_session_id_context. For this error
599 * case, we generate an error instead of treating the event like a
600 * cache miss (otherwise it would be easy for applications to
601 * effectively disable the session cache by accident without anyone
605 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL_GET_PREV_SESSION
,
606 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED
);
611 if (ret
->timeout
< (long)(time(NULL
) - ret
->time
)) { /* timeout */
612 CRYPTO_atomic_add(&s
->session_ctx
->stats
.sess_timeout
, 1, &discard
,
613 s
->session_ctx
->lock
);
614 if (try_session_cache
) {
615 /* session was from the cache, so remove it */
616 SSL_CTX_remove_session(s
->session_ctx
, ret
);
621 /* Check extended master secret extension consistency */
622 if (ret
->flags
& SSL_SESS_FLAG_EXTMS
) {
623 /* If old session includes extms, but new does not: abort handshake */
624 if (!(s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
)) {
625 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_SSL_GET_PREV_SESSION
,
626 SSL_R_INCONSISTENT_EXTMS
);
630 } else if (s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
) {
631 /* If new session includes extms, but old does not: do not resume */
635 if (!SSL_IS_TLS13(s
)) {
636 /* We already did this for TLS1.3 */
637 SSL_SESSION_free(s
->session
);
641 CRYPTO_atomic_add(&s
->session_ctx
->stats
.sess_hit
, 1, &discard
,
642 s
->session_ctx
->lock
);
643 s
->verify_result
= s
->session
->verify_result
;
648 SSL_SESSION_free(ret
);
649 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
653 if (!try_session_cache
) {
655 * The session was from a ticket, so we should issue a ticket for
658 s
->ext
.ticket_expected
= 1;
667 int SSL_CTX_add_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
669 int ret
= 0, discard
;
673 * add just 1 reference count for the SSL_CTX's session cache even though
674 * it has two ways of access: each session is in a doubly linked list and
677 SSL_SESSION_up_ref(c
);
679 * if session c is in already in cache, we take back the increment later
682 CRYPTO_THREAD_write_lock(ctx
->lock
);
683 s
= lh_SSL_SESSION_insert(ctx
->sessions
, c
);
686 * s != NULL iff we already had a session with the given PID. In this
687 * case, s == c should hold (then we did not really modify
688 * ctx->sessions), or we're in trouble.
690 if (s
!= NULL
&& s
!= c
) {
691 /* We *are* in trouble ... */
692 SSL_SESSION_list_remove(ctx
, s
);
695 * ... so pretend the other session did not exist in cache (we cannot
696 * handle two SSL_SESSION structures with identical session ID in the
697 * same cache, which could happen e.g. when two threads concurrently
698 * obtain the same session from an external cache)
701 } else if (s
== NULL
&&
702 lh_SSL_SESSION_retrieve(ctx
->sessions
, c
) == NULL
) {
703 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
706 * ... so take back the extra reference and also don't add
707 * the session to the SSL_SESSION_list at this time
712 /* Put at the head of the queue unless it is already in the cache */
714 SSL_SESSION_list_add(ctx
, c
);
718 * existing cache entry -- decrement previously incremented reference
719 * count because it already takes into account the cache
722 SSL_SESSION_free(s
); /* s == c */
726 * new cache entry -- remove old ones if cache has become too large
731 if (SSL_CTX_sess_get_cache_size(ctx
) > 0) {
732 while (SSL_CTX_sess_number(ctx
) > SSL_CTX_sess_get_cache_size(ctx
)) {
733 if (!remove_session_lock(ctx
, ctx
->session_cache_tail
, 0))
736 CRYPTO_atomic_add(&ctx
->stats
.sess_cache_full
, 1, &discard
,
741 CRYPTO_THREAD_unlock(ctx
->lock
);
745 int SSL_CTX_remove_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
747 return remove_session_lock(ctx
, c
, 1);
750 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
)
755 if ((c
!= NULL
) && (c
->session_id_length
!= 0)) {
757 CRYPTO_THREAD_write_lock(ctx
->lock
);
758 if ((r
= lh_SSL_SESSION_retrieve(ctx
->sessions
, c
)) == c
) {
760 r
= lh_SSL_SESSION_delete(ctx
->sessions
, c
);
761 SSL_SESSION_list_remove(ctx
, c
);
763 c
->not_resumable
= 1;
766 CRYPTO_THREAD_unlock(ctx
->lock
);
771 if (ctx
->remove_session_cb
!= NULL
)
772 ctx
->remove_session_cb(ctx
, c
);
778 void SSL_SESSION_free(SSL_SESSION
*ss
)
785 CRYPTO_DOWN_REF(&ss
->references
, &i
, ss
->lock
);
786 REF_PRINT_COUNT("SSL_SESSION", ss
);
789 REF_ASSERT_ISNT(i
< 0);
791 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
);
793 OPENSSL_cleanse(ss
->master_key
, sizeof(ss
->master_key
));
794 OPENSSL_cleanse(ss
->session_id
, sizeof(ss
->session_id
));
796 sk_X509_pop_free(ss
->peer_chain
, X509_free
);
797 sk_SSL_CIPHER_free(ss
->ciphers
);
798 OPENSSL_free(ss
->ext
.hostname
);
799 OPENSSL_free(ss
->ext
.tick
);
800 #ifndef OPENSSL_NO_EC
801 OPENSSL_free(ss
->ext
.ecpointformats
);
802 ss
->ext
.ecpointformats
= NULL
;
803 ss
->ext
.ecpointformats_len
= 0;
804 OPENSSL_free(ss
->ext
.supportedgroups
);
805 ss
->ext
.supportedgroups
= NULL
;
806 ss
->ext
.supportedgroups_len
= 0;
807 #endif /* OPENSSL_NO_EC */
808 #ifndef OPENSSL_NO_PSK
809 OPENSSL_free(ss
->psk_identity_hint
);
810 OPENSSL_free(ss
->psk_identity
);
812 #ifndef OPENSSL_NO_SRP
813 OPENSSL_free(ss
->srp_username
);
815 OPENSSL_free(ss
->ext
.alpn_selected
);
816 OPENSSL_free(ss
->ext
.tick_nonce
);
817 OPENSSL_free(ss
->ticket_appdata
);
818 CRYPTO_THREAD_lock_free(ss
->lock
);
819 OPENSSL_clear_free(ss
, sizeof(*ss
));
822 int SSL_SESSION_up_ref(SSL_SESSION
*ss
)
826 if (CRYPTO_UP_REF(&ss
->references
, &i
, ss
->lock
) <= 0)
829 REF_PRINT_COUNT("SSL_SESSION", ss
);
830 REF_ASSERT_ISNT(i
< 2);
831 return ((i
> 1) ? 1 : 0);
834 int SSL_set_session(SSL
*s
, SSL_SESSION
*session
)
836 ssl_clear_bad_session(s
);
837 if (s
->ctx
->method
!= s
->method
) {
838 if (!SSL_set_ssl_method(s
, s
->ctx
->method
))
842 if (session
!= NULL
) {
843 SSL_SESSION_up_ref(session
);
844 s
->verify_result
= session
->verify_result
;
846 SSL_SESSION_free(s
->session
);
847 s
->session
= session
;
852 int SSL_SESSION_set1_id(SSL_SESSION
*s
, const unsigned char *sid
,
853 unsigned int sid_len
)
855 if (sid_len
> SSL_MAX_SSL_SESSION_ID_LENGTH
) {
856 SSLerr(SSL_F_SSL_SESSION_SET1_ID
,
857 SSL_R_SSL_SESSION_ID_TOO_LONG
);
860 s
->session_id_length
= sid_len
;
861 if (sid
!= s
->session_id
)
862 memcpy(s
->session_id
, sid
, sid_len
);
866 long SSL_SESSION_set_timeout(SSL_SESSION
*s
, long t
)
874 long SSL_SESSION_get_timeout(const SSL_SESSION
*s
)
881 long SSL_SESSION_get_time(const SSL_SESSION
*s
)
888 long SSL_SESSION_set_time(SSL_SESSION
*s
, long t
)
896 int SSL_SESSION_get_protocol_version(const SSL_SESSION
*s
)
898 return s
->ssl_version
;
901 int SSL_SESSION_set_protocol_version(SSL_SESSION
*s
, int version
)
903 s
->ssl_version
= version
;
907 const SSL_CIPHER
*SSL_SESSION_get0_cipher(const SSL_SESSION
*s
)
912 int SSL_SESSION_set_cipher(SSL_SESSION
*s
, const SSL_CIPHER
*cipher
)
918 const char *SSL_SESSION_get0_hostname(const SSL_SESSION
*s
)
920 return s
->ext
.hostname
;
923 int SSL_SESSION_set1_hostname(SSL_SESSION
*s
, const char *hostname
)
925 OPENSSL_free(s
->ext
.hostname
);
926 if (hostname
== NULL
) {
927 s
->ext
.hostname
= NULL
;
930 s
->ext
.hostname
= OPENSSL_strdup(hostname
);
932 return s
->ext
.hostname
!= NULL
;
935 int SSL_SESSION_has_ticket(const SSL_SESSION
*s
)
937 return (s
->ext
.ticklen
> 0) ? 1 : 0;
940 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION
*s
)
942 return s
->ext
.tick_lifetime_hint
;
945 void SSL_SESSION_get0_ticket(const SSL_SESSION
*s
, const unsigned char **tick
,
948 *len
= s
->ext
.ticklen
;
953 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION
*s
)
955 return s
->ext
.max_early_data
;
958 int SSL_SESSION_set_max_early_data(SSL_SESSION
*s
, uint32_t max_early_data
)
960 s
->ext
.max_early_data
= max_early_data
;
965 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION
*s
,
966 const unsigned char **alpn
,
969 *alpn
= s
->ext
.alpn_selected
;
970 *len
= s
->ext
.alpn_selected_len
;
973 int SSL_SESSION_set1_alpn_selected(SSL_SESSION
*s
, const unsigned char *alpn
,
976 OPENSSL_free(s
->ext
.alpn_selected
);
977 if (alpn
== NULL
|| len
== 0) {
978 s
->ext
.alpn_selected
= NULL
;
979 s
->ext
.alpn_selected_len
= 0;
982 s
->ext
.alpn_selected
= OPENSSL_memdup(alpn
, len
);
983 if (s
->ext
.alpn_selected
== NULL
) {
984 s
->ext
.alpn_selected_len
= 0;
987 s
->ext
.alpn_selected_len
= len
;
992 X509
*SSL_SESSION_get0_peer(SSL_SESSION
*s
)
997 int SSL_SESSION_set1_id_context(SSL_SESSION
*s
, const unsigned char *sid_ctx
,
998 unsigned int sid_ctx_len
)
1000 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
1001 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT
,
1002 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
1005 s
->sid_ctx_length
= sid_ctx_len
;
1006 if (sid_ctx
!= s
->sid_ctx
)
1007 memcpy(s
->sid_ctx
, sid_ctx
, sid_ctx_len
);
1012 int SSL_SESSION_is_resumable(const SSL_SESSION
*s
)
1015 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1018 return !s
->not_resumable
1019 && (s
->session_id_length
> 0 || s
->ext
.ticklen
> 0);
1022 long SSL_CTX_set_timeout(SSL_CTX
*s
, long t
)
1027 l
= s
->session_timeout
;
1028 s
->session_timeout
= t
;
1032 long SSL_CTX_get_timeout(const SSL_CTX
*s
)
1036 return s
->session_timeout
;
1039 int SSL_set_session_secret_cb(SSL
*s
,
1040 tls_session_secret_cb_fn tls_session_secret_cb
,
1045 s
->ext
.session_secret_cb
= tls_session_secret_cb
;
1046 s
->ext
.session_secret_cb_arg
= arg
;
1050 int SSL_set_session_ticket_ext_cb(SSL
*s
, tls_session_ticket_ext_cb_fn cb
,
1055 s
->ext
.session_ticket_cb
= cb
;
1056 s
->ext
.session_ticket_cb_arg
= arg
;
1060 int SSL_set_session_ticket_ext(SSL
*s
, void *ext_data
, int ext_len
)
1062 if (s
->version
>= TLS1_VERSION
) {
1063 OPENSSL_free(s
->ext
.session_ticket
);
1064 s
->ext
.session_ticket
= NULL
;
1065 s
->ext
.session_ticket
=
1066 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT
) + ext_len
);
1067 if (s
->ext
.session_ticket
== NULL
) {
1068 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT
, ERR_R_MALLOC_FAILURE
);
1072 if (ext_data
!= NULL
) {
1073 s
->ext
.session_ticket
->length
= ext_len
;
1074 s
->ext
.session_ticket
->data
= s
->ext
.session_ticket
+ 1;
1075 memcpy(s
->ext
.session_ticket
->data
, ext_data
, ext_len
);
1077 s
->ext
.session_ticket
->length
= 0;
1078 s
->ext
.session_ticket
->data
= NULL
;
1087 typedef struct timeout_param_st
{
1090 LHASH_OF(SSL_SESSION
) *cache
;
1093 static void timeout_cb(SSL_SESSION
*s
, TIMEOUT_PARAM
*p
)
1095 if ((p
->time
== 0) || (p
->time
> (s
->time
+ s
->timeout
))) { /* timeout */
1097 * The reason we don't call SSL_CTX_remove_session() is to save on
1100 (void)lh_SSL_SESSION_delete(p
->cache
, s
);
1101 SSL_SESSION_list_remove(p
->ctx
, s
);
1102 s
->not_resumable
= 1;
1103 if (p
->ctx
->remove_session_cb
!= NULL
)
1104 p
->ctx
->remove_session_cb(p
->ctx
, s
);
1105 SSL_SESSION_free(s
);
1109 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION
, TIMEOUT_PARAM
);
1111 void SSL_CTX_flush_sessions(SSL_CTX
*s
, long t
)
1117 tp
.cache
= s
->sessions
;
1118 if (tp
.cache
== NULL
)
1121 CRYPTO_THREAD_write_lock(s
->lock
);
1122 i
= lh_SSL_SESSION_get_down_load(s
->sessions
);
1123 lh_SSL_SESSION_set_down_load(s
->sessions
, 0);
1124 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp
.cache
, timeout_cb
, &tp
);
1125 lh_SSL_SESSION_set_down_load(s
->sessions
, i
);
1126 CRYPTO_THREAD_unlock(s
->lock
);
1129 int ssl_clear_bad_session(SSL
*s
)
1131 if ((s
->session
!= NULL
) &&
1132 !(s
->shutdown
& SSL_SENT_SHUTDOWN
) &&
1133 !(SSL_in_init(s
) || SSL_in_before(s
))) {
1134 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1140 /* locked by SSL_CTX in the calling function */
1141 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1143 if ((s
->next
== NULL
) || (s
->prev
== NULL
))
1146 if (s
->next
== (SSL_SESSION
*)&(ctx
->session_cache_tail
)) {
1147 /* last element in list */
1148 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1149 /* only one element in list */
1150 ctx
->session_cache_head
= NULL
;
1151 ctx
->session_cache_tail
= NULL
;
1153 ctx
->session_cache_tail
= s
->prev
;
1154 s
->prev
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1157 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1158 /* first element in list */
1159 ctx
->session_cache_head
= s
->next
;
1160 s
->next
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1162 /* middle of list */
1163 s
->next
->prev
= s
->prev
;
1164 s
->prev
->next
= s
->next
;
1167 s
->prev
= s
->next
= NULL
;
1170 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1172 if ((s
->next
!= NULL
) && (s
->prev
!= NULL
))
1173 SSL_SESSION_list_remove(ctx
, s
);
1175 if (ctx
->session_cache_head
== NULL
) {
1176 ctx
->session_cache_head
= s
;
1177 ctx
->session_cache_tail
= s
;
1178 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1179 s
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1181 s
->next
= ctx
->session_cache_head
;
1183 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1184 ctx
->session_cache_head
= s
;
1188 void SSL_CTX_sess_set_new_cb(SSL_CTX
*ctx
,
1189 int (*cb
) (struct ssl_st
*ssl
, SSL_SESSION
*sess
))
1191 ctx
->new_session_cb
= cb
;
1194 int (*SSL_CTX_sess_get_new_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, SSL_SESSION
*sess
) {
1195 return ctx
->new_session_cb
;
1198 void SSL_CTX_sess_set_remove_cb(SSL_CTX
*ctx
,
1199 void (*cb
) (SSL_CTX
*ctx
, SSL_SESSION
*sess
))
1201 ctx
->remove_session_cb
= cb
;
1204 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX
*ctx
)) (SSL_CTX
*ctx
,
1205 SSL_SESSION
*sess
) {
1206 return ctx
->remove_session_cb
;
1209 void SSL_CTX_sess_set_get_cb(SSL_CTX
*ctx
,
1210 SSL_SESSION
*(*cb
) (struct ssl_st
*ssl
,
1211 const unsigned char *data
,
1212 int len
, int *copy
))
1214 ctx
->get_session_cb
= cb
;
1217 SSL_SESSION
*(*SSL_CTX_sess_get_get_cb(SSL_CTX
*ctx
)) (SSL
*ssl
,
1221 return ctx
->get_session_cb
;
1224 void SSL_CTX_set_info_callback(SSL_CTX
*ctx
,
1225 void (*cb
) (const SSL
*ssl
, int type
, int val
))
1227 ctx
->info_callback
= cb
;
1230 void (*SSL_CTX_get_info_callback(SSL_CTX
*ctx
)) (const SSL
*ssl
, int type
,
1232 return ctx
->info_callback
;
1235 void SSL_CTX_set_client_cert_cb(SSL_CTX
*ctx
,
1236 int (*cb
) (SSL
*ssl
, X509
**x509
,
1239 ctx
->client_cert_cb
= cb
;
1242 int (*SSL_CTX_get_client_cert_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, X509
**x509
,
1244 return ctx
->client_cert_cb
;
1247 #ifndef OPENSSL_NO_ENGINE
1248 int SSL_CTX_set_client_cert_engine(SSL_CTX
*ctx
, ENGINE
*e
)
1250 if (!ENGINE_init(e
)) {
1251 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
, ERR_R_ENGINE_LIB
);
1254 if (!ENGINE_get_ssl_client_cert_function(e
)) {
1255 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
,
1256 SSL_R_NO_CLIENT_CERT_METHOD
);
1260 ctx
->client_cert_engine
= e
;
1265 void SSL_CTX_set_cookie_generate_cb(SSL_CTX
*ctx
,
1266 int (*cb
) (SSL
*ssl
,
1267 unsigned char *cookie
,
1268 unsigned int *cookie_len
))
1270 ctx
->app_gen_cookie_cb
= cb
;
1273 void SSL_CTX_set_cookie_verify_cb(SSL_CTX
*ctx
,
1274 int (*cb
) (SSL
*ssl
,
1275 const unsigned char *cookie
,
1276 unsigned int cookie_len
))
1278 ctx
->app_verify_cookie_cb
= cb
;
1281 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION
*ss
, const void *data
, size_t len
)
1283 OPENSSL_free(ss
->ticket_appdata
);
1284 ss
->ticket_appdata_len
= 0;
1285 if (data
== NULL
|| len
== 0) {
1286 ss
->ticket_appdata
= NULL
;
1289 ss
->ticket_appdata
= OPENSSL_memdup(data
, len
);
1290 if (ss
->ticket_appdata
!= NULL
) {
1291 ss
->ticket_appdata_len
= len
;
1297 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION
*ss
, void **data
, size_t *len
)
1299 *data
= ss
->ticket_appdata
;
1300 *len
= ss
->ticket_appdata_len
;
1304 IMPLEMENT_PEM_rw(SSL_SESSION
, SSL_SESSION
, PEM_STRING_SSL_SESSION
, SSL_SESSION
)