2 * Copyright 1995-2018 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"
15 #include "internal/cryptlib.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 */
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->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 * sizeof(*src->ext.supportedgroups));
209 if (dest->ext.supportedgroups == NULL)
214 if (ticket != 0 && src->ext.tick != NULL) {
216 OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
217 if (dest->ext.tick == NULL)
220 dest->ext.tick_lifetime_hint = 0;
221 dest->ext.ticklen = 0;
224 if (src->ext.alpn_selected != NULL) {
225 dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
226 src->ext.alpn_selected_len);
227 if (dest->ext.alpn_selected == NULL)
231 #ifndef OPENSSL_NO_SRP
232 if (src->srp_username) {
233 dest->srp_username = OPENSSL_strdup(src->srp_username);
234 if (dest->srp_username == NULL) {
240 if (src->ticket_appdata != NULL) {
241 dest->ticket_appdata =
242 OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
243 if (dest->ticket_appdata == 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 (RAND_bytes(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 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
326 SSL_R_UNSUPPORTED_SSL_VERSION);
331 * If RFC5077 ticket, use empty session ID (as server).
333 * (a) ssl_get_prev_session() does lookahead into the
334 * ClientHello extensions to find the session ticket.
335 * When ssl_get_prev_session() fails, statem_srvr.c calls
336 * ssl_get_new_session() in tls_process_client_hello().
337 * At that point, it has not yet parsed the extensions,
338 * however, because of the lookahead, it already knows
339 * whether a ticket is expected or not.
341 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
342 * ServerHello extensions, and before recording the session
343 * ID received from the server, so this block is a noop.
345 if (s->ext.ticket_expected) {
346 ss->session_id_length = 0;
350 /* Choose which callback will set the session ID */
351 CRYPTO_THREAD_read_lock(s->lock);
352 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
353 if (s->generate_session_id)
354 cb = s->generate_session_id;
355 else if (s->session_ctx->generate_session_id)
356 cb = s->session_ctx->generate_session_id;
357 CRYPTO_THREAD_unlock(s->session_ctx->lock);
358 CRYPTO_THREAD_unlock(s->lock);
359 /* Choose a session ID */
360 memset(ss->session_id, 0, ss->session_id_length);
361 tmp = (int)ss->session_id_length;
362 if (!cb(s, ss->session_id, &tmp)) {
363 /* The callback failed */
364 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
365 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
369 * Don't allow the callback to set the session length to zero. nor
370 * set it higher than it was.
372 if (tmp == 0 || tmp > ss->session_id_length) {
373 /* The callback set an illegal length */
374 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
375 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
378 ss->session_id_length = tmp;
379 /* Finally, check for a conflict */
380 if (SSL_has_matching_session_id(s, ss->session_id,
381 (unsigned int)ss->session_id_length)) {
382 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GENERATE_SESSION_ID,
383 SSL_R_SSL_SESSION_ID_CONFLICT);
390 int ssl_get_new_session(SSL *s, int session)
392 /* This gets used by clients and servers. */
394 SSL_SESSION *ss = NULL;
396 if ((ss = SSL_SESSION_new()) == NULL) {
397 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_NEW_SESSION,
398 ERR_R_MALLOC_FAILURE);
402 /* If the context has a default timeout, use it */
403 if (s->session_ctx->session_timeout == 0)
404 ss->timeout = SSL_get_default_timeout(s);
406 ss->timeout = s->session_ctx->session_timeout;
408 SSL_SESSION_free(s->session);
412 if (SSL_IS_TLS13(s)) {
414 * We generate the session id while constructing the
415 * NewSessionTicket in TLSv1.3.
417 ss->session_id_length = 0;
418 } else if (!ssl_generate_session_id(s, ss)) {
419 /* SSLfatal() already called */
420 SSL_SESSION_free(ss);
424 if (s->ext.hostname) {
425 ss->ext.hostname = OPENSSL_strdup(s->ext.hostname);
426 if (ss->ext.hostname == NULL) {
427 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_NEW_SESSION,
428 ERR_R_INTERNAL_ERROR);
429 SSL_SESSION_free(ss);
434 ss->session_id_length = 0;
437 if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
438 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_NEW_SESSION,
439 ERR_R_INTERNAL_ERROR);
440 SSL_SESSION_free(ss);
443 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
444 ss->sid_ctx_length = s->sid_ctx_length;
446 ss->ssl_version = s->version;
447 ss->verify_result = X509_V_OK;
449 /* If client supports extended master secret set it in session */
450 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
451 ss->flags |= SSL_SESS_FLAG_EXTMS;
456 SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id,
459 SSL_SESSION *ret = NULL;
462 if ((s->session_ctx->session_cache_mode
463 & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
466 data.ssl_version = s->version;
467 if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
470 memcpy(data.session_id, sess_id, sess_id_len);
471 data.session_id_length = sess_id_len;
473 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
474 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
476 /* don't allow other threads to steal it: */
477 SSL_SESSION_up_ref(ret);
479 CRYPTO_THREAD_unlock(s->session_ctx->lock);
481 CRYPTO_atomic_add(&s->session_ctx->stats.sess_miss, 1, &discard,
482 s->session_ctx->lock);
485 if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
488 ret = s->session_ctx->get_session_cb(s, sess_id, sess_id_len, ©);
491 CRYPTO_atomic_add(&s->session_ctx->stats.sess_cb_hit, 1, &discard,
492 s->session_ctx->lock);
495 * Increment reference count now if the session callback asks us
496 * to do so (note that if the session structures returned by the
497 * callback are shared between threads, it must handle the
498 * reference count itself [i.e. copy == 0], or things won't be
502 SSL_SESSION_up_ref(ret);
505 * Add the externally cached session to the internal cache as
506 * well if and only if we are supposed to.
508 if ((s->session_ctx->session_cache_mode &
509 SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
511 * Either return value of SSL_CTX_add_session should not
512 * interrupt the session resumption process. The return
513 * value is intentionally ignored.
515 (void)SSL_CTX_add_session(s->session_ctx, ret);
524 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
525 * connection. It is only called by servers.
527 * hello: The parsed ClientHello data
531 * 0: no session found
532 * 1: a session may have been found.
535 * - If a session is found then s->session is pointed at it (after freeing an
536 * existing session if need be) and s->verify_result is set from the session.
537 * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
538 * if the server should issue a new session ticket (to 0 otherwise).
540 int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello)
542 /* This is used only by servers. */
544 SSL_SESSION *ret = NULL;
545 int fatal = 0, discard;
546 int try_session_cache = 0;
549 if (SSL_IS_TLS13(s)) {
551 * By default we will send a new ticket. This can be overridden in the
554 s->ext.ticket_expected = 1;
555 if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
556 SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
558 || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
559 hello->pre_proc_exts, NULL, 0))
564 /* sets s->ext.ticket_expected */
565 r = tls_get_ticket_from_client(s, hello, &ret);
567 case SSL_TICKET_FATAL_ERR_MALLOC:
568 case SSL_TICKET_FATAL_ERR_OTHER:
570 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_PREV_SESSION,
571 ERR_R_INTERNAL_ERROR);
573 case SSL_TICKET_NONE:
574 case SSL_TICKET_EMPTY:
575 if (hello->session_id_len > 0) {
576 try_session_cache = 1;
577 ret = lookup_sess_in_cache(s, hello->session_id,
578 hello->session_id_len);
581 case SSL_TICKET_NO_DECRYPT:
582 case SSL_TICKET_SUCCESS:
583 case SSL_TICKET_SUCCESS_RENEW:
591 /* Now ret is non-NULL and we own one of its reference counts. */
593 /* Check TLS version consistency */
594 if (ret->ssl_version != s->version)
597 if (ret->sid_ctx_length != s->sid_ctx_length
598 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
600 * We have the session requested by the client, but we don't want to
601 * use it in this context.
603 goto err; /* treat like cache miss */
606 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
608 * We can't be sure if this session is being used out of context,
609 * which is especially important for SSL_VERIFY_PEER. The application
610 * should have used SSL[_CTX]_set_session_id_context. For this error
611 * case, we generate an error instead of treating the event like a
612 * cache miss (otherwise it would be easy for applications to
613 * effectively disable the session cache by accident without anyone
617 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_GET_PREV_SESSION,
618 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
623 if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
624 CRYPTO_atomic_add(&s->session_ctx->stats.sess_timeout, 1, &discard,
625 s->session_ctx->lock);
626 if (try_session_cache) {
627 /* session was from the cache, so remove it */
628 SSL_CTX_remove_session(s->session_ctx, ret);
633 /* Check extended master secret extension consistency */
634 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
635 /* If old session includes extms, but new does not: abort handshake */
636 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
637 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL_GET_PREV_SESSION,
638 SSL_R_INCONSISTENT_EXTMS);
642 } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
643 /* If new session includes extms, but old does not: do not resume */
647 if (!SSL_IS_TLS13(s)) {
648 /* We already did this for TLS1.3 */
649 SSL_SESSION_free(s->session);
653 CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
654 s->session_ctx->lock);
655 s->verify_result = s->session->verify_result;
660 SSL_SESSION_free(ret);
661 /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
665 if (!try_session_cache) {
667 * The session was from a ticket, so we should issue a ticket for
670 s->ext.ticket_expected = 1;
679 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
681 int ret = 0, discard;
685 * add just 1 reference count for the SSL_CTX's session cache even though
686 * it has two ways of access: each session is in a doubly linked list and
689 SSL_SESSION_up_ref(c);
691 * if session c is in already in cache, we take back the increment later
694 CRYPTO_THREAD_write_lock(ctx->lock);
695 s = lh_SSL_SESSION_insert(ctx->sessions, c);
698 * s != NULL iff we already had a session with the given PID. In this
699 * case, s == c should hold (then we did not really modify
700 * ctx->sessions), or we're in trouble.
702 if (s != NULL && s != c) {
703 /* We *are* in trouble ... */
704 SSL_SESSION_list_remove(ctx, s);
707 * ... so pretend the other session did not exist in cache (we cannot
708 * handle two SSL_SESSION structures with identical session ID in the
709 * same cache, which could happen e.g. when two threads concurrently
710 * obtain the same session from an external cache)
713 } else if (s == NULL &&
714 lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
715 /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
718 * ... so take back the extra reference and also don't add
719 * the session to the SSL_SESSION_list at this time
724 /* Put at the head of the queue unless it is already in the cache */
726 SSL_SESSION_list_add(ctx, c);
730 * existing cache entry -- decrement previously incremented reference
731 * count because it already takes into account the cache
734 SSL_SESSION_free(s); /* s == c */
738 * new cache entry -- remove old ones if cache has become too large
743 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
744 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
745 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
748 CRYPTO_atomic_add(&ctx->stats.sess_cache_full, 1, &discard,
753 CRYPTO_THREAD_unlock(ctx->lock);
757 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
759 return remove_session_lock(ctx, c, 1);
762 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
767 if ((c != NULL) && (c->session_id_length != 0)) {
769 CRYPTO_THREAD_write_lock(ctx->lock);
770 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
772 r = lh_SSL_SESSION_delete(ctx->sessions, r);
773 SSL_SESSION_list_remove(ctx, r);
775 c->not_resumable = 1;
778 CRYPTO_THREAD_unlock(ctx->lock);
780 if (ctx->remove_session_cb != NULL)
781 ctx->remove_session_cb(ctx, c);
790 void SSL_SESSION_free(SSL_SESSION *ss)
796 CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);
797 REF_PRINT_COUNT("SSL_SESSION", ss);
800 REF_ASSERT_ISNT(i < 0);
802 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
804 OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
805 OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
807 sk_X509_pop_free(ss->peer_chain, X509_free);
808 sk_SSL_CIPHER_free(ss->ciphers);
809 OPENSSL_free(ss->ext.hostname);
810 OPENSSL_free(ss->ext.tick);
811 #ifndef OPENSSL_NO_EC
812 OPENSSL_free(ss->ext.ecpointformats);
813 ss->ext.ecpointformats = NULL;
814 ss->ext.ecpointformats_len = 0;
815 OPENSSL_free(ss->ext.supportedgroups);
816 ss->ext.supportedgroups = NULL;
817 ss->ext.supportedgroups_len = 0;
818 #endif /* OPENSSL_NO_EC */
819 #ifndef OPENSSL_NO_PSK
820 OPENSSL_free(ss->psk_identity_hint);
821 OPENSSL_free(ss->psk_identity);
823 #ifndef OPENSSL_NO_SRP
824 OPENSSL_free(ss->srp_username);
826 OPENSSL_free(ss->ext.alpn_selected);
827 OPENSSL_free(ss->ticket_appdata);
828 CRYPTO_THREAD_lock_free(ss->lock);
829 OPENSSL_clear_free(ss, sizeof(*ss));
832 int SSL_SESSION_up_ref(SSL_SESSION *ss)
836 if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0)
839 REF_PRINT_COUNT("SSL_SESSION", ss);
840 REF_ASSERT_ISNT(i < 2);
841 return ((i > 1) ? 1 : 0);
844 int SSL_set_session(SSL *s, SSL_SESSION *session)
846 ssl_clear_bad_session(s);
847 if (s->ctx->method != s->method) {
848 if (!SSL_set_ssl_method(s, s->ctx->method))
852 if (session != NULL) {
853 SSL_SESSION_up_ref(session);
854 s->verify_result = session->verify_result;
856 SSL_SESSION_free(s->session);
857 s->session = session;
862 int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
863 unsigned int sid_len)
865 if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
866 SSLerr(SSL_F_SSL_SESSION_SET1_ID,
867 SSL_R_SSL_SESSION_ID_TOO_LONG);
870 s->session_id_length = sid_len;
871 if (sid != s->session_id)
872 memcpy(s->session_id, sid, sid_len);
876 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
884 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
891 long SSL_SESSION_get_time(const SSL_SESSION *s)
898 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
906 int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
908 return s->ssl_version;
911 int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
913 s->ssl_version = version;
917 const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
922 int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
928 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
930 return s->ext.hostname;
933 int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
935 OPENSSL_free(s->ext.hostname);
936 if (hostname == NULL) {
937 s->ext.hostname = NULL;
940 s->ext.hostname = OPENSSL_strdup(hostname);
942 return s->ext.hostname != NULL;
945 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
947 return (s->ext.ticklen > 0) ? 1 : 0;
950 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
952 return s->ext.tick_lifetime_hint;
955 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
958 *len = s->ext.ticklen;
963 uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
965 return s->ext.max_early_data;
968 int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
970 s->ext.max_early_data = max_early_data;
975 void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
976 const unsigned char **alpn,
979 *alpn = s->ext.alpn_selected;
980 *len = s->ext.alpn_selected_len;
983 int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
986 OPENSSL_free(s->ext.alpn_selected);
987 if (alpn == NULL || len == 0) {
988 s->ext.alpn_selected = NULL;
989 s->ext.alpn_selected_len = 0;
992 s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
993 if (s->ext.alpn_selected == NULL) {
994 s->ext.alpn_selected_len = 0;
997 s->ext.alpn_selected_len = len;
1002 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
1007 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1008 unsigned int sid_ctx_len)
1010 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1011 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
1012 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1015 s->sid_ctx_length = sid_ctx_len;
1016 if (sid_ctx != s->sid_ctx)
1017 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1022 int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1025 * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1028 return !s->not_resumable
1029 && (s->session_id_length > 0 || s->ext.ticklen > 0);
1032 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1037 l = s->session_timeout;
1038 s->session_timeout = t;
1042 long SSL_CTX_get_timeout(const SSL_CTX *s)
1046 return s->session_timeout;
1049 int SSL_set_session_secret_cb(SSL *s,
1050 tls_session_secret_cb_fn tls_session_secret_cb,
1055 s->ext.session_secret_cb = tls_session_secret_cb;
1056 s->ext.session_secret_cb_arg = arg;
1060 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1065 s->ext.session_ticket_cb = cb;
1066 s->ext.session_ticket_cb_arg = arg;
1070 int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1072 if (s->version >= TLS1_VERSION) {
1073 OPENSSL_free(s->ext.session_ticket);
1074 s->ext.session_ticket = NULL;
1075 s->ext.session_ticket =
1076 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1077 if (s->ext.session_ticket == NULL) {
1078 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1082 if (ext_data != NULL) {
1083 s->ext.session_ticket->length = ext_len;
1084 s->ext.session_ticket->data = s->ext.session_ticket + 1;
1085 memcpy(s->ext.session_ticket->data, ext_data, ext_len);
1087 s->ext.session_ticket->length = 0;
1088 s->ext.session_ticket->data = NULL;
1097 typedef struct timeout_param_st {
1100 LHASH_OF(SSL_SESSION) *cache;
1103 static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
1105 if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
1107 * The reason we don't call SSL_CTX_remove_session() is to save on
1110 (void)lh_SSL_SESSION_delete(p->cache, s);
1111 SSL_SESSION_list_remove(p->ctx, s);
1112 s->not_resumable = 1;
1113 if (p->ctx->remove_session_cb != NULL)
1114 p->ctx->remove_session_cb(p->ctx, s);
1115 SSL_SESSION_free(s);
1119 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
1121 void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1127 tp.cache = s->sessions;
1128 if (tp.cache == NULL)
1131 CRYPTO_THREAD_write_lock(s->lock);
1132 i = lh_SSL_SESSION_get_down_load(s->sessions);
1133 lh_SSL_SESSION_set_down_load(s->sessions, 0);
1134 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
1135 lh_SSL_SESSION_set_down_load(s->sessions, i);
1136 CRYPTO_THREAD_unlock(s->lock);
1139 int ssl_clear_bad_session(SSL *s)
1141 if ((s->session != NULL) &&
1142 !(s->shutdown & SSL_SENT_SHUTDOWN) &&
1143 !(SSL_in_init(s) || SSL_in_before(s))) {
1144 SSL_CTX_remove_session(s->session_ctx, s->session);
1150 /* locked by SSL_CTX in the calling function */
1151 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1153 if ((s->next == NULL) || (s->prev == NULL))
1156 if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1157 /* last element in list */
1158 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1159 /* only one element in list */
1160 ctx->session_cache_head = NULL;
1161 ctx->session_cache_tail = NULL;
1163 ctx->session_cache_tail = s->prev;
1164 s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1167 if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1168 /* first element in list */
1169 ctx->session_cache_head = s->next;
1170 s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1172 /* middle of list */
1173 s->next->prev = s->prev;
1174 s->prev->next = s->next;
1177 s->prev = s->next = NULL;
1180 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1182 if ((s->next != NULL) && (s->prev != NULL))
1183 SSL_SESSION_list_remove(ctx, s);
1185 if (ctx->session_cache_head == NULL) {
1186 ctx->session_cache_head = s;
1187 ctx->session_cache_tail = s;
1188 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1189 s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1191 s->next = ctx->session_cache_head;
1193 s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1194 ctx->session_cache_head = s;
1198 void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1199 int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
1201 ctx->new_session_cb = cb;
1204 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1205 return ctx->new_session_cb;
1208 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1209 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1211 ctx->remove_session_cb = cb;
1214 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1215 SSL_SESSION *sess) {
1216 return ctx->remove_session_cb;
1219 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1220 SSL_SESSION *(*cb) (struct ssl_st *ssl,
1221 const unsigned char *data,
1222 int len, int *copy))
1224 ctx->get_session_cb = cb;
1227 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1231 return ctx->get_session_cb;
1234 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1235 void (*cb) (const SSL *ssl, int type, int val))
1237 ctx->info_callback = cb;
1240 void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
1242 return ctx->info_callback;
1245 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1246 int (*cb) (SSL *ssl, X509 **x509,
1249 ctx->client_cert_cb = cb;
1252 int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
1254 return ctx->client_cert_cb;
1257 #ifndef OPENSSL_NO_ENGINE
1258 int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
1260 if (!ENGINE_init(e)) {
1261 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
1264 if (!ENGINE_get_ssl_client_cert_function(e)) {
1265 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
1266 SSL_R_NO_CLIENT_CERT_METHOD);
1270 ctx->client_cert_engine = e;
1275 void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1276 int (*cb) (SSL *ssl,
1277 unsigned char *cookie,
1278 unsigned int *cookie_len))
1280 ctx->app_gen_cookie_cb = cb;
1283 void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1284 int (*cb) (SSL *ssl,
1285 const unsigned char *cookie,
1286 unsigned int cookie_len))
1288 ctx->app_verify_cookie_cb = cb;
1291 int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1293 OPENSSL_free(ss->ticket_appdata);
1294 ss->ticket_appdata_len = 0;
1295 if (data == NULL || len == 0) {
1296 ss->ticket_appdata = NULL;
1299 ss->ticket_appdata = OPENSSL_memdup(data, len);
1300 if (ss->ticket_appdata != NULL) {
1301 ss->ticket_appdata_len = len;
1307 int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1309 *data = ss->ticket_appdata;
1310 *len = ss->ticket_appdata_len;
1314 void SSL_CTX_set_stateless_cookie_generate_cb(
1316 int (*cb) (SSL *ssl,
1317 unsigned char *cookie,
1318 size_t *cookie_len))
1320 ctx->gen_stateless_cookie_cb = cb;
1323 void SSL_CTX_set_stateless_cookie_verify_cb(
1325 int (*cb) (SSL *ssl,
1326 const unsigned char *cookie,
1329 ctx->verify_stateless_cookie_cb = cb;
1332 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)