2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2005 Nokia. All rights reserved.
13 * The portions of the attached software ("Contribution") is developed by
14 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
17 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
18 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
19 * support (see RFC 4279) to OpenSSL.
21 * No patent licenses or other rights except those expressly stated in
22 * the OpenSSL open source license shall be deemed granted or received
23 * expressly, by implication, estoppel, or otherwise.
25 * No assurances are provided by Nokia that the Contribution does not
26 * infringe the patent or other intellectual property rights of any third
27 * party or that the license provides you with all the necessary rights
28 * to make use of the Contribution.
30 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
31 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
32 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
33 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
38 #include <openssl/lhash.h>
39 #include <openssl/rand.h>
40 #include <openssl/engine.h>
43 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
);
44 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
);
45 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
);
47 SSL_SESSION
*SSL_get_session(const SSL
*ssl
)
48 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
50 return (ssl
->session
);
53 SSL_SESSION
*SSL_get1_session(SSL
*ssl
)
54 /* variant of SSL_get_session: caller really gets something */
58 * Need to lock this all up rather than just use CRYPTO_add so that
59 * somebody doesn't free ssl->session between when we check it's non-null
60 * and when we up the reference count.
62 CRYPTO_THREAD_read_lock(ssl
->lock
);
65 SSL_SESSION_up_ref(sess
);
66 CRYPTO_THREAD_unlock(ssl
->lock
);
70 int SSL_SESSION_set_ex_data(SSL_SESSION
*s
, int idx
, void *arg
)
72 return (CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
));
75 void *SSL_SESSION_get_ex_data(const SSL_SESSION
*s
, int idx
)
77 return (CRYPTO_get_ex_data(&s
->ex_data
, idx
));
80 SSL_SESSION
*SSL_SESSION_new(void)
84 ss
= OPENSSL_zalloc(sizeof(*ss
));
86 SSLerr(SSL_F_SSL_SESSION_NEW
, ERR_R_MALLOC_FAILURE
);
90 ss
->verify_result
= 1; /* avoid 0 (= X509_V_OK) just in case */
92 ss
->timeout
= 60 * 5 + 4; /* 5 minute timeout by default */
93 ss
->time
= (unsigned long)time(NULL
);
94 ss
->lock
= CRYPTO_THREAD_lock_new();
95 if (ss
->lock
== NULL
) {
96 SSLerr(SSL_F_SSL_SESSION_NEW
, ERR_R_MALLOC_FAILURE
);
101 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
)) {
102 CRYPTO_THREAD_lock_free(ss
->lock
);
110 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
111 * ticket == 0 then no ticket information is duplicated, otherwise it is.
113 SSL_SESSION
*ssl_session_dup(SSL_SESSION
*src
, int ticket
)
117 dest
= OPENSSL_malloc(sizeof(*src
));
121 memcpy(dest
, src
, sizeof(*dest
));
124 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
125 * the case of an error whilst halfway through constructing dest
127 #ifndef OPENSSL_NO_PSK
128 dest
->psk_identity_hint
= NULL
;
129 dest
->psk_identity
= NULL
;
131 dest
->ciphers
= NULL
;
132 dest
->tlsext_hostname
= NULL
;
133 #ifndef OPENSSL_NO_EC
134 dest
->tlsext_ecpointformatlist
= NULL
;
135 dest
->tlsext_ellipticcurvelist
= NULL
;
137 dest
->tlsext_tick
= NULL
;
138 #ifndef OPENSSL_NO_SRP
139 dest
->srp_username
= NULL
;
141 memset(&dest
->ex_data
, 0, sizeof(dest
->ex_data
));
143 /* We deliberately don't copy the prev and next pointers */
147 dest
->references
= 1;
149 dest
->lock
= CRYPTO_THREAD_lock_new();
150 if (dest
->lock
== NULL
)
153 if (src
->peer
!= NULL
)
154 X509_up_ref(src
->peer
);
156 if (src
->peer_chain
!= NULL
) {
157 dest
->peer_chain
= X509_chain_up_ref(src
->peer_chain
);
158 if (dest
->peer_chain
== NULL
)
162 #ifndef OPENSSL_NO_PSK
163 if (src
->psk_identity_hint
) {
164 dest
->psk_identity_hint
= OPENSSL_strdup(src
->psk_identity_hint
);
165 if (dest
->psk_identity_hint
== NULL
) {
169 if (src
->psk_identity
) {
170 dest
->psk_identity
= OPENSSL_strdup(src
->psk_identity
);
171 if (dest
->psk_identity
== NULL
) {
177 if(src
->ciphers
!= NULL
) {
178 dest
->ciphers
= sk_SSL_CIPHER_dup(src
->ciphers
);
179 if (dest
->ciphers
== NULL
)
183 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
,
184 &dest
->ex_data
, &src
->ex_data
)) {
188 if (src
->tlsext_hostname
) {
189 dest
->tlsext_hostname
= OPENSSL_strdup(src
->tlsext_hostname
);
190 if (dest
->tlsext_hostname
== NULL
) {
194 #ifndef OPENSSL_NO_EC
195 if (src
->tlsext_ecpointformatlist
) {
196 dest
->tlsext_ecpointformatlist
=
197 OPENSSL_memdup(src
->tlsext_ecpointformatlist
,
198 src
->tlsext_ecpointformatlist_length
);
199 if (dest
->tlsext_ecpointformatlist
== NULL
)
202 if (src
->tlsext_ellipticcurvelist
) {
203 dest
->tlsext_ellipticcurvelist
=
204 OPENSSL_memdup(src
->tlsext_ellipticcurvelist
,
205 src
->tlsext_ellipticcurvelist_length
);
206 if (dest
->tlsext_ellipticcurvelist
== NULL
)
212 dest
->tlsext_tick
= OPENSSL_memdup(src
->tlsext_tick
, src
->tlsext_ticklen
);
213 if(dest
->tlsext_tick
== NULL
)
216 dest
->tlsext_tick_lifetime_hint
= 0;
217 dest
->tlsext_ticklen
= 0;
220 #ifndef OPENSSL_NO_SRP
221 if (src
->srp_username
) {
222 dest
->srp_username
= OPENSSL_strdup(src
->srp_username
);
223 if (dest
->srp_username
== NULL
) {
231 SSLerr(SSL_F_SSL_SESSION_DUP
, ERR_R_MALLOC_FAILURE
);
232 SSL_SESSION_free(dest
);
236 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION
*s
,
240 *len
= s
->session_id_length
;
241 return s
->session_id
;
244 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION
*s
)
246 return s
->compress_meth
;
250 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
251 * the ID with random junk repeatedly until we have no conflict is going to
252 * complete in one iteration pretty much "most" of the time (btw:
253 * understatement). So, if it takes us 10 iterations and we still can't avoid
254 * a conflict - well that's a reasonable point to call it quits. Either the
255 * RAND code is broken or someone is trying to open roughly very close to
256 * 2^256 SSL sessions to our server. How you might store that many sessions
257 * is perhaps a more interesting question ...
260 #define MAX_SESS_ID_ATTEMPTS 10
261 static int def_generate_session_id(const SSL
*ssl
, unsigned char *id
,
262 unsigned int *id_len
)
264 unsigned int retry
= 0;
266 if (RAND_bytes(id
, *id_len
) <= 0)
268 while (SSL_has_matching_session_id(ssl
, id
, *id_len
) &&
269 (++retry
< MAX_SESS_ID_ATTEMPTS
)) ;
270 if (retry
< MAX_SESS_ID_ATTEMPTS
)
272 /* else - woops a session_id match */
274 * XXX We should also check the external cache -- but the probability of
275 * a collision is negligible, and we could not prevent the concurrent
276 * creation of sessions with identical IDs since we currently don't have
277 * means to atomically check whether a session ID already exists and make
278 * a reservation for it if it does not (this problem applies to the
279 * internal cache as well).
284 int ssl_get_new_session(SSL
*s
, int session
)
286 /* This gets used by clients and servers. */
289 SSL_SESSION
*ss
= NULL
;
290 GEN_SESSION_CB cb
= def_generate_session_id
;
292 if ((ss
= SSL_SESSION_new()) == NULL
)
295 /* If the context has a default timeout, use it */
296 if (s
->session_ctx
->session_timeout
== 0)
297 ss
->timeout
= SSL_get_default_timeout(s
);
299 ss
->timeout
= s
->session_ctx
->session_timeout
;
301 SSL_SESSION_free(s
->session
);
305 if (s
->version
== SSL3_VERSION
) {
306 ss
->ssl_version
= SSL3_VERSION
;
307 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
308 } else if (s
->version
== TLS1_VERSION
) {
309 ss
->ssl_version
= TLS1_VERSION
;
310 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
311 } else if (s
->version
== TLS1_1_VERSION
) {
312 ss
->ssl_version
= TLS1_1_VERSION
;
313 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
314 } else if (s
->version
== TLS1_2_VERSION
) {
315 ss
->ssl_version
= TLS1_2_VERSION
;
316 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
317 } else if (s
->version
== DTLS1_BAD_VER
) {
318 ss
->ssl_version
= DTLS1_BAD_VER
;
319 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
320 } else if (s
->version
== DTLS1_VERSION
) {
321 ss
->ssl_version
= DTLS1_VERSION
;
322 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
323 } else if (s
->version
== DTLS1_2_VERSION
) {
324 ss
->ssl_version
= DTLS1_2_VERSION
;
325 ss
->session_id_length
= SSL3_SSL_SESSION_ID_LENGTH
;
327 SSLerr(SSL_F_SSL_GET_NEW_SESSION
, SSL_R_UNSUPPORTED_SSL_VERSION
);
328 SSL_SESSION_free(ss
);
333 * If RFC5077 ticket, use empty session ID (as server).
335 * (a) ssl_get_prev_session() does lookahead into the
336 * ClientHello extensions to find the session ticket.
337 * When ssl_get_prev_session() fails, statem_srvr.c calls
338 * ssl_get_new_session() in tls_process_client_hello().
339 * At that point, it has not yet parsed the extensions,
340 * however, because of the lookahead, it already knows
341 * whether a ticket is expected or not.
343 * (b) statem_clnt.c calls ssl_get_new_session() before parsing
344 * ServerHello extensions, and before recording the session
345 * ID received from the server, so this block is a noop.
347 if (s
->tlsext_ticket_expected
) {
348 ss
->session_id_length
= 0;
352 /* Choose which callback will set the session ID */
353 CRYPTO_THREAD_read_lock(s
->lock
);
354 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
355 if (s
->generate_session_id
)
356 cb
= s
->generate_session_id
;
357 else if (s
->session_ctx
->generate_session_id
)
358 cb
= s
->session_ctx
->generate_session_id
;
359 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
360 CRYPTO_THREAD_unlock(s
->lock
);
361 /* Choose a session ID */
362 tmp
= ss
->session_id_length
;
363 if (!cb(s
, ss
->session_id
, &tmp
)) {
364 /* The callback failed */
365 SSLerr(SSL_F_SSL_GET_NEW_SESSION
,
366 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED
);
367 SSL_SESSION_free(ss
);
371 * Don't allow the callback to set the session length to zero. nor
372 * set it higher than it was.
374 if (tmp
== 0 || tmp
> ss
->session_id_length
) {
375 /* The callback set an illegal length */
376 SSLerr(SSL_F_SSL_GET_NEW_SESSION
,
377 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH
);
378 SSL_SESSION_free(ss
);
381 ss
->session_id_length
= tmp
;
382 /* Finally, check for a conflict */
383 if (SSL_has_matching_session_id(s
, ss
->session_id
,
384 ss
->session_id_length
)) {
385 SSLerr(SSL_F_SSL_GET_NEW_SESSION
, SSL_R_SSL_SESSION_ID_CONFLICT
);
386 SSL_SESSION_free(ss
);
391 if (s
->tlsext_hostname
) {
392 ss
->tlsext_hostname
= OPENSSL_strdup(s
->tlsext_hostname
);
393 if (ss
->tlsext_hostname
== NULL
) {
394 SSLerr(SSL_F_SSL_GET_NEW_SESSION
, ERR_R_INTERNAL_ERROR
);
395 SSL_SESSION_free(ss
);
400 ss
->session_id_length
= 0;
403 if (s
->sid_ctx_length
> sizeof ss
->sid_ctx
) {
404 SSLerr(SSL_F_SSL_GET_NEW_SESSION
, ERR_R_INTERNAL_ERROR
);
405 SSL_SESSION_free(ss
);
408 memcpy(ss
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
);
409 ss
->sid_ctx_length
= s
->sid_ctx_length
;
411 ss
->ssl_version
= s
->version
;
412 ss
->verify_result
= X509_V_OK
;
414 /* If client supports extended master secret set it in session */
415 if (s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
)
416 ss
->flags
|= SSL_SESS_FLAG_EXTMS
;
422 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
423 * connection. It is only called by servers.
425 * ext: ClientHello extensions (including length prefix)
426 * session_id: ClientHello session ID.
430 * 0: a session may have been found.
433 * - If a session is found then s->session is pointed at it (after freeing an
434 * existing session if need be) and s->verify_result is set from the session.
435 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
436 * if the server should issue a new session ticket (to 0 otherwise).
438 int ssl_get_prev_session(SSL
*s
, const PACKET
*ext
, const PACKET
*session_id
)
440 /* This is used only by servers. */
442 SSL_SESSION
*ret
= NULL
;
444 int try_session_cache
= 1;
447 if (PACKET_remaining(session_id
) == 0)
448 try_session_cache
= 0;
450 /* sets s->tlsext_ticket_expected and extended master secret flag */
451 r
= tls_check_serverhello_tlsext_early(s
, ext
, session_id
, &ret
);
453 case -1: /* Error during processing */
456 case 0: /* No ticket found */
457 case 1: /* Zero length ticket found */
458 break; /* Ok to carry on processing session id. */
459 case 2: /* Ticket found but not decrypted. */
460 case 3: /* Ticket decrypted, *ret has been set. */
461 try_session_cache
= 0;
467 if (try_session_cache
&&
469 !(s
->session_ctx
->session_cache_mode
&
470 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP
)) {
473 data
.ssl_version
= s
->version
;
474 if (!PACKET_copy_all(session_id
, data
.session_id
,
475 sizeof(data
.session_id
),
479 data
.session_id_length
= local_len
;
480 CRYPTO_THREAD_read_lock(s
->session_ctx
->lock
);
481 ret
= lh_SSL_SESSION_retrieve(s
->session_ctx
->sessions
, &data
);
483 /* don't allow other threads to steal it: */
484 SSL_SESSION_up_ref(ret
);
486 CRYPTO_THREAD_unlock(s
->session_ctx
->lock
);
488 s
->session_ctx
->stats
.sess_miss
++;
491 if (try_session_cache
&&
492 ret
== NULL
&& s
->session_ctx
->get_session_cb
!= NULL
) {
494 ret
= s
->session_ctx
->get_session_cb(s
, PACKET_data(session_id
),
495 PACKET_remaining(session_id
),
499 s
->session_ctx
->stats
.sess_cb_hit
++;
502 * Increment reference count now if the session callback asks us
503 * to do so (note that if the session structures returned by the
504 * callback are shared between threads, it must handle the
505 * reference count itself [i.e. copy == 0], or things won't be
509 SSL_SESSION_up_ref(ret
);
512 * Add the externally cached session to the internal cache as
513 * well if and only if we are supposed to.
516 (s
->session_ctx
->session_cache_mode
&
517 SSL_SESS_CACHE_NO_INTERNAL_STORE
)) {
519 * The following should not return 1, otherwise, things are
522 if (SSL_CTX_add_session(s
->session_ctx
, ret
))
531 /* Now ret is non-NULL and we own one of its reference counts. */
533 if (ret
->sid_ctx_length
!= s
->sid_ctx_length
534 || memcmp(ret
->sid_ctx
, s
->sid_ctx
, ret
->sid_ctx_length
)) {
536 * We have the session requested by the client, but we don't want to
537 * use it in this context.
539 goto err
; /* treat like cache miss */
542 if ((s
->verify_mode
& SSL_VERIFY_PEER
) && s
->sid_ctx_length
== 0) {
544 * We can't be sure if this session is being used out of context,
545 * which is especially important for SSL_VERIFY_PEER. The application
546 * should have used SSL[_CTX]_set_session_id_context. For this error
547 * case, we generate an error instead of treating the event like a
548 * cache miss (otherwise it would be easy for applications to
549 * effectively disable the session cache by accident without anyone
553 SSLerr(SSL_F_SSL_GET_PREV_SESSION
,
554 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED
);
559 if (ret
->cipher
== NULL
) {
560 unsigned char buf
[5], *p
;
566 if ((ret
->ssl_version
>> 8) >= SSL3_VERSION_MAJOR
)
567 ret
->cipher
= ssl_get_cipher_by_char(s
, &(buf
[2]));
569 ret
->cipher
= ssl_get_cipher_by_char(s
, &(buf
[1]));
570 if (ret
->cipher
== NULL
)
574 if (ret
->timeout
< (long)(time(NULL
) - ret
->time
)) { /* timeout */
575 s
->session_ctx
->stats
.sess_timeout
++;
576 if (try_session_cache
) {
577 /* session was from the cache, so remove it */
578 SSL_CTX_remove_session(s
->session_ctx
, ret
);
583 /* Check extended master secret extension consistency */
584 if (ret
->flags
& SSL_SESS_FLAG_EXTMS
) {
585 /* If old session includes extms, but new does not: abort handshake */
586 if (!(s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
)) {
587 SSLerr(SSL_F_SSL_GET_PREV_SESSION
, SSL_R_INCONSISTENT_EXTMS
);
588 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
592 } else if (s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
) {
593 /* If new session includes extms, but old does not: do not resume */
597 s
->session_ctx
->stats
.sess_hit
++;
599 SSL_SESSION_free(s
->session
);
601 s
->verify_result
= s
->session
->verify_result
;
606 SSL_SESSION_free(ret
);
608 if (!try_session_cache
) {
610 * The session was from a ticket, so we should issue a ticket for
613 s
->tlsext_ticket_expected
= 1;
622 int SSL_CTX_add_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
628 * add just 1 reference count for the SSL_CTX's session cache even though
629 * it has two ways of access: each session is in a doubly linked list and
632 SSL_SESSION_up_ref(c
);
634 * if session c is in already in cache, we take back the increment later
637 CRYPTO_THREAD_write_lock(ctx
->lock
);
638 s
= lh_SSL_SESSION_insert(ctx
->sessions
, c
);
641 * s != NULL iff we already had a session with the given PID. In this
642 * case, s == c should hold (then we did not really modify
643 * ctx->sessions), or we're in trouble.
645 if (s
!= NULL
&& s
!= c
) {
646 /* We *are* in trouble ... */
647 SSL_SESSION_list_remove(ctx
, s
);
650 * ... so pretend the other session did not exist in cache (we cannot
651 * handle two SSL_SESSION structures with identical session ID in the
652 * same cache, which could happen e.g. when two threads concurrently
653 * obtain the same session from an external cache)
658 /* Put at the head of the queue unless it is already in the cache */
660 SSL_SESSION_list_add(ctx
, c
);
664 * existing cache entry -- decrement previously incremented reference
665 * count because it already takes into account the cache
668 SSL_SESSION_free(s
); /* s == c */
672 * new cache entry -- remove old ones if cache has become too large
677 if (SSL_CTX_sess_get_cache_size(ctx
) > 0) {
678 while (SSL_CTX_sess_number(ctx
) >
679 SSL_CTX_sess_get_cache_size(ctx
)) {
680 if (!remove_session_lock(ctx
, ctx
->session_cache_tail
, 0))
683 ctx
->stats
.sess_cache_full
++;
687 CRYPTO_THREAD_unlock(ctx
->lock
);
691 int SSL_CTX_remove_session(SSL_CTX
*ctx
, SSL_SESSION
*c
)
693 return remove_session_lock(ctx
, c
, 1);
696 static int remove_session_lock(SSL_CTX
*ctx
, SSL_SESSION
*c
, int lck
)
701 if ((c
!= NULL
) && (c
->session_id_length
!= 0)) {
703 CRYPTO_THREAD_write_lock(ctx
->lock
);
704 if ((r
= lh_SSL_SESSION_retrieve(ctx
->sessions
, c
)) == c
) {
706 r
= lh_SSL_SESSION_delete(ctx
->sessions
, c
);
707 SSL_SESSION_list_remove(ctx
, c
);
711 CRYPTO_THREAD_unlock(ctx
->lock
);
714 r
->not_resumable
= 1;
715 if (ctx
->remove_session_cb
!= NULL
)
716 ctx
->remove_session_cb(ctx
, r
);
724 void SSL_SESSION_free(SSL_SESSION
*ss
)
731 CRYPTO_atomic_add(&ss
->references
, -1, &i
, ss
->lock
);
732 REF_PRINT_COUNT("SSL_SESSION", ss
);
735 REF_ASSERT_ISNT(i
< 0);
737 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION
, ss
, &ss
->ex_data
);
739 OPENSSL_cleanse(ss
->master_key
, sizeof ss
->master_key
);
740 OPENSSL_cleanse(ss
->session_id
, sizeof ss
->session_id
);
742 sk_X509_pop_free(ss
->peer_chain
, X509_free
);
743 sk_SSL_CIPHER_free(ss
->ciphers
);
744 OPENSSL_free(ss
->tlsext_hostname
);
745 OPENSSL_free(ss
->tlsext_tick
);
746 #ifndef OPENSSL_NO_EC
747 ss
->tlsext_ecpointformatlist_length
= 0;
748 OPENSSL_free(ss
->tlsext_ecpointformatlist
);
749 ss
->tlsext_ellipticcurvelist_length
= 0;
750 OPENSSL_free(ss
->tlsext_ellipticcurvelist
);
751 #endif /* OPENSSL_NO_EC */
752 #ifndef OPENSSL_NO_PSK
753 OPENSSL_free(ss
->psk_identity_hint
);
754 OPENSSL_free(ss
->psk_identity
);
756 #ifndef OPENSSL_NO_SRP
757 OPENSSL_free(ss
->srp_username
);
759 CRYPTO_THREAD_lock_free(ss
->lock
);
760 OPENSSL_clear_free(ss
, sizeof(*ss
));
763 int SSL_SESSION_up_ref(SSL_SESSION
*ss
)
767 if (CRYPTO_atomic_add(&ss
->references
, 1, &i
, ss
->lock
) <= 0)
770 REF_PRINT_COUNT("SSL_SESSION", ss
);
771 REF_ASSERT_ISNT(i
< 2);
772 return ((i
> 1) ? 1 : 0);
775 int SSL_set_session(SSL
*s
, SSL_SESSION
*session
)
778 if (session
!= NULL
) {
779 if (s
->ctx
->method
!= s
->method
) {
780 if (!SSL_set_ssl_method(s
, s
->ctx
->method
))
784 SSL_SESSION_up_ref(session
);
785 SSL_SESSION_free(s
->session
);
786 s
->session
= session
;
787 s
->verify_result
= s
->session
->verify_result
;
790 SSL_SESSION_free(s
->session
);
792 if (s
->ctx
->method
!= s
->method
) {
793 if (!SSL_set_ssl_method(s
, s
->ctx
->method
))
801 long SSL_SESSION_set_timeout(SSL_SESSION
*s
, long t
)
809 long SSL_SESSION_get_timeout(const SSL_SESSION
*s
)
816 long SSL_SESSION_get_time(const SSL_SESSION
*s
)
823 long SSL_SESSION_set_time(SSL_SESSION
*s
, long t
)
831 int SSL_SESSION_get_protocol_version(const SSL_SESSION
*s
)
833 return s
->ssl_version
;
836 const char *SSL_SESSION_get0_hostname(const SSL_SESSION
*s
)
838 return s
->tlsext_hostname
;
841 int SSL_SESSION_has_ticket(const SSL_SESSION
*s
)
843 return (s
->tlsext_ticklen
> 0) ? 1 : 0;
846 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION
*s
)
848 return s
->tlsext_tick_lifetime_hint
;
851 void SSL_SESSION_get0_ticket(const SSL_SESSION
*s
, unsigned char **tick
,
854 *len
= s
->tlsext_ticklen
;
856 *tick
= s
->tlsext_tick
;
859 X509
*SSL_SESSION_get0_peer(SSL_SESSION
*s
)
864 int SSL_SESSION_set1_id_context(SSL_SESSION
*s
, const unsigned char *sid_ctx
,
865 unsigned int sid_ctx_len
)
867 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
868 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT
,
869 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
872 s
->sid_ctx_length
= sid_ctx_len
;
873 memcpy(s
->sid_ctx
, sid_ctx
, sid_ctx_len
);
878 long SSL_CTX_set_timeout(SSL_CTX
*s
, long t
)
883 l
= s
->session_timeout
;
884 s
->session_timeout
= t
;
888 long SSL_CTX_get_timeout(const SSL_CTX
*s
)
892 return (s
->session_timeout
);
895 int SSL_set_session_secret_cb(SSL
*s
,
896 int (*tls_session_secret_cb
) (SSL
*s
,
908 s
->tls_session_secret_cb
= tls_session_secret_cb
;
909 s
->tls_session_secret_cb_arg
= arg
;
913 int SSL_set_session_ticket_ext_cb(SSL
*s
, tls_session_ticket_ext_cb_fn cb
,
918 s
->tls_session_ticket_ext_cb
= cb
;
919 s
->tls_session_ticket_ext_cb_arg
= arg
;
923 int SSL_set_session_ticket_ext(SSL
*s
, void *ext_data
, int ext_len
)
925 if (s
->version
>= TLS1_VERSION
) {
926 OPENSSL_free(s
->tlsext_session_ticket
);
927 s
->tlsext_session_ticket
= NULL
;
928 s
->tlsext_session_ticket
=
929 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT
) + ext_len
);
930 if (s
->tlsext_session_ticket
== NULL
) {
931 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT
, ERR_R_MALLOC_FAILURE
);
936 s
->tlsext_session_ticket
->length
= ext_len
;
937 s
->tlsext_session_ticket
->data
= s
->tlsext_session_ticket
+ 1;
938 memcpy(s
->tlsext_session_ticket
->data
, ext_data
, ext_len
);
940 s
->tlsext_session_ticket
->length
= 0;
941 s
->tlsext_session_ticket
->data
= NULL
;
950 typedef struct timeout_param_st
{
953 LHASH_OF(SSL_SESSION
) *cache
;
956 static void timeout_cb(SSL_SESSION
*s
, TIMEOUT_PARAM
*p
)
958 if ((p
->time
== 0) || (p
->time
> (s
->time
+ s
->timeout
))) { /* timeout */
960 * The reason we don't call SSL_CTX_remove_session() is to save on
963 (void)lh_SSL_SESSION_delete(p
->cache
, s
);
964 SSL_SESSION_list_remove(p
->ctx
, s
);
965 s
->not_resumable
= 1;
966 if (p
->ctx
->remove_session_cb
!= NULL
)
967 p
->ctx
->remove_session_cb(p
->ctx
, s
);
972 IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION
, TIMEOUT_PARAM
);
974 void SSL_CTX_flush_sessions(SSL_CTX
*s
, long t
)
980 tp
.cache
= s
->sessions
;
981 if (tp
.cache
== NULL
)
984 CRYPTO_THREAD_write_lock(s
->lock
);
985 i
= lh_SSL_SESSION_get_down_load(s
->sessions
);
986 lh_SSL_SESSION_set_down_load(s
->sessions
, 0);
987 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp
.cache
, timeout_cb
, &tp
);
988 lh_SSL_SESSION_set_down_load(s
->sessions
, i
);
989 CRYPTO_THREAD_unlock(s
->lock
);
992 int ssl_clear_bad_session(SSL
*s
)
994 if ((s
->session
!= NULL
) &&
995 !(s
->shutdown
& SSL_SENT_SHUTDOWN
) &&
996 !(SSL_in_init(s
) || SSL_in_before(s
))) {
997 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1003 /* locked by SSL_CTX in the calling function */
1004 static void SSL_SESSION_list_remove(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1006 if ((s
->next
== NULL
) || (s
->prev
== NULL
))
1009 if (s
->next
== (SSL_SESSION
*)&(ctx
->session_cache_tail
)) {
1010 /* last element in list */
1011 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1012 /* only one element in list */
1013 ctx
->session_cache_head
= NULL
;
1014 ctx
->session_cache_tail
= NULL
;
1016 ctx
->session_cache_tail
= s
->prev
;
1017 s
->prev
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1020 if (s
->prev
== (SSL_SESSION
*)&(ctx
->session_cache_head
)) {
1021 /* first element in list */
1022 ctx
->session_cache_head
= s
->next
;
1023 s
->next
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1025 /* middle of list */
1026 s
->next
->prev
= s
->prev
;
1027 s
->prev
->next
= s
->next
;
1030 s
->prev
= s
->next
= NULL
;
1033 static void SSL_SESSION_list_add(SSL_CTX
*ctx
, SSL_SESSION
*s
)
1035 if ((s
->next
!= NULL
) && (s
->prev
!= NULL
))
1036 SSL_SESSION_list_remove(ctx
, s
);
1038 if (ctx
->session_cache_head
== NULL
) {
1039 ctx
->session_cache_head
= s
;
1040 ctx
->session_cache_tail
= s
;
1041 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1042 s
->next
= (SSL_SESSION
*)&(ctx
->session_cache_tail
);
1044 s
->next
= ctx
->session_cache_head
;
1046 s
->prev
= (SSL_SESSION
*)&(ctx
->session_cache_head
);
1047 ctx
->session_cache_head
= s
;
1051 void SSL_CTX_sess_set_new_cb(SSL_CTX
*ctx
,
1052 int (*cb
) (struct ssl_st
*ssl
,
1055 ctx
->new_session_cb
= cb
;
1058 int (*SSL_CTX_sess_get_new_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, SSL_SESSION
*sess
) {
1059 return ctx
->new_session_cb
;
1062 void SSL_CTX_sess_set_remove_cb(SSL_CTX
*ctx
,
1063 void (*cb
) (SSL_CTX
*ctx
, SSL_SESSION
*sess
))
1065 ctx
->remove_session_cb
= cb
;
1068 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX
*ctx
)) (SSL_CTX
*ctx
,
1069 SSL_SESSION
*sess
) {
1070 return ctx
->remove_session_cb
;
1073 void SSL_CTX_sess_set_get_cb(SSL_CTX
*ctx
,
1074 SSL_SESSION
*(*cb
) (struct ssl_st
*ssl
,
1075 const unsigned char *data
,
1076 int len
, int *copy
))
1078 ctx
->get_session_cb
= cb
;
1081 SSL_SESSION
*(*SSL_CTX_sess_get_get_cb(SSL_CTX
*ctx
)) (SSL
*ssl
,
1082 const unsigned char *data
,
1083 int len
, int *copy
) {
1084 return ctx
->get_session_cb
;
1087 void SSL_CTX_set_info_callback(SSL_CTX
*ctx
,
1088 void (*cb
) (const SSL
*ssl
, int type
, int val
))
1090 ctx
->info_callback
= cb
;
1093 void (*SSL_CTX_get_info_callback(SSL_CTX
*ctx
)) (const SSL
*ssl
, int type
,
1095 return ctx
->info_callback
;
1098 void SSL_CTX_set_client_cert_cb(SSL_CTX
*ctx
,
1099 int (*cb
) (SSL
*ssl
, X509
**x509
,
1102 ctx
->client_cert_cb
= cb
;
1105 int (*SSL_CTX_get_client_cert_cb(SSL_CTX
*ctx
)) (SSL
*ssl
, X509
**x509
,
1107 return ctx
->client_cert_cb
;
1110 #ifndef OPENSSL_NO_ENGINE
1111 int SSL_CTX_set_client_cert_engine(SSL_CTX
*ctx
, ENGINE
*e
)
1113 if (!ENGINE_init(e
)) {
1114 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
, ERR_R_ENGINE_LIB
);
1117 if (!ENGINE_get_ssl_client_cert_function(e
)) {
1118 SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE
,
1119 SSL_R_NO_CLIENT_CERT_METHOD
);
1123 ctx
->client_cert_engine
= e
;
1128 void SSL_CTX_set_cookie_generate_cb(SSL_CTX
*ctx
,
1129 int (*cb
) (SSL
*ssl
,
1130 unsigned char *cookie
,
1131 unsigned int *cookie_len
))
1133 ctx
->app_gen_cookie_cb
= cb
;
1136 void SSL_CTX_set_cookie_verify_cb(SSL_CTX
*ctx
,
1137 int (*cb
) (SSL
*ssl
, const unsigned char *cookie
,
1138 unsigned int cookie_len
))
1140 ctx
->app_verify_cookie_cb
= cb
;
1143 IMPLEMENT_PEM_rw(SSL_SESSION
, SSL_SESSION
, PEM_STRING_SSL_SESSION
,