1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
110 /* ====================================================================
111 * Copyright 2005 Nokia. All rights reserved.
113 * The portions of the attached software ("Contribution") is developed by
114 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
117 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
118 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
119 * support (see RFC 4279) to OpenSSL.
121 * No patent licenses or other rights except those expressly stated in
122 * the OpenSSL open source license shall be deemed granted or received
123 * expressly, by implication, estoppel, or otherwise.
125 * No assurances are provided by Nokia that the Contribution does not
126 * infringe the patent or other intellectual property rights of any third
127 * party or that the license provides you with all the necessary rights
128 * to make use of the Contribution.
130 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
131 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
132 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
133 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
138 #include <openssl/lhash.h>
139 #include <openssl/rand.h>
140 #include <openssl/engine.h>
141 #include "ssl_locl.h"
143 static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
144 static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
145 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
147 SSL_SESSION *SSL_get_session(const SSL *ssl)
148 /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
150 return (ssl->session);
153 SSL_SESSION *SSL_get1_session(SSL *ssl)
154 /* variant of SSL_get_session: caller really gets something */
158 * Need to lock this all up rather than just use CRYPTO_add so that
159 * somebody doesn't free ssl->session between when we check it's non-null
160 * and when we up the reference count.
162 CRYPTO_THREAD_read_lock(ssl->lock);
165 SSL_SESSION_up_ref(sess);
166 CRYPTO_THREAD_unlock(ssl->lock);
170 int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
172 return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
175 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
177 return (CRYPTO_get_ex_data(&s->ex_data, idx));
180 SSL_SESSION *SSL_SESSION_new(void)
184 ss = OPENSSL_zalloc(sizeof(*ss));
186 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
190 ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
192 ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
193 ss->time = (unsigned long)time(NULL);
194 ss->lock = CRYPTO_THREAD_lock_new();
195 if (ss->lock == NULL) {
196 SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
201 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
202 CRYPTO_THREAD_lock_free(ss->lock);
210 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
211 * ticket == 0 then no ticket information is duplicated, otherwise it is.
213 SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
217 dest = OPENSSL_malloc(sizeof(*src));
221 memcpy(dest, src, sizeof(*dest));
224 * Set the various pointers to NULL so that we can call SSL_SESSION_free in
225 * the case of an error whilst halfway through constructing dest
227 #ifndef OPENSSL_NO_PSK
228 dest->psk_identity_hint = NULL;
229 dest->psk_identity = NULL;
231 dest->ciphers = NULL;
232 dest->tlsext_hostname = NULL;
233 #ifndef OPENSSL_NO_EC
234 dest->tlsext_ecpointformatlist = NULL;
235 dest->tlsext_ellipticcurvelist = NULL;
237 dest->tlsext_tick = NULL;
238 #ifndef OPENSSL_NO_SRP
239 dest->srp_username = NULL;
241 memset(&dest->ex_data, 0, sizeof(dest->ex_data));
243 /* We deliberately don't copy the prev and next pointers */
247 dest->references = 1;
249 dest->lock = CRYPTO_THREAD_lock_new();
250 if (dest->lock == NULL)
253 if (src->peer != NULL)
254 X509_up_ref(src->peer);
256 if (src->peer_chain != NULL) {
257 dest->peer_chain = X509_chain_up_ref(src->peer_chain);
258 if (dest->peer_chain == NULL)
262 #ifndef OPENSSL_NO_PSK
263 if (src->psk_identity_hint) {
264 dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
265 if (dest->psk_identity_hint == NULL) {
269 if (src->psk_identity) {
270 dest->psk_identity = OPENSSL_strdup(src->psk_identity);
271 if (dest->psk_identity == NULL) {
277 if(src->ciphers != NULL) {
278 dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
279 if (dest->ciphers == NULL)
283 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
284 &dest->ex_data, &src->ex_data)) {
288 if (src->tlsext_hostname) {
289 dest->tlsext_hostname = OPENSSL_strdup(src->tlsext_hostname);
290 if (dest->tlsext_hostname == NULL) {
294 #ifndef OPENSSL_NO_EC
295 if (src->tlsext_ecpointformatlist) {
296 dest->tlsext_ecpointformatlist =
297 OPENSSL_memdup(src->tlsext_ecpointformatlist,
298 src->tlsext_ecpointformatlist_length);
299 if (dest->tlsext_ecpointformatlist == NULL)
302 if (src->tlsext_ellipticcurvelist) {
303 dest->tlsext_ellipticcurvelist =
304 OPENSSL_memdup(src->tlsext_ellipticcurvelist,
305 src->tlsext_ellipticcurvelist_length);
306 if (dest->tlsext_ellipticcurvelist == NULL)
312 dest->tlsext_tick = OPENSSL_memdup(src->tlsext_tick, src->tlsext_ticklen);
313 if(dest->tlsext_tick == NULL)
316 dest->tlsext_tick_lifetime_hint = 0;
317 dest->tlsext_ticklen = 0;
320 #ifndef OPENSSL_NO_SRP
321 if (src->srp_username) {
322 dest->srp_username = OPENSSL_strdup(src->srp_username);
323 if (dest->srp_username == NULL) {
331 SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
332 SSL_SESSION_free(dest);
336 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s,
340 *len = s->session_id_length;
341 return s->session_id;
344 unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
346 return s->compress_meth;
350 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
351 * the ID with random junk repeatedly until we have no conflict is going to
352 * complete in one iteration pretty much "most" of the time (btw:
353 * understatement). So, if it takes us 10 iterations and we still can't avoid
354 * a conflict - well that's a reasonable point to call it quits. Either the
355 * RAND code is broken or someone is trying to open roughly very close to
356 * 2^256 SSL sessions to our server. How you might store that many sessions
357 * is perhaps a more interesting question ...
360 #define MAX_SESS_ID_ATTEMPTS 10
361 static int def_generate_session_id(const SSL *ssl, unsigned char *id,
362 unsigned int *id_len)
364 unsigned int retry = 0;
366 if (RAND_bytes(id, *id_len) <= 0)
368 while (SSL_has_matching_session_id(ssl, id, *id_len) &&
369 (++retry < MAX_SESS_ID_ATTEMPTS)) ;
370 if (retry < MAX_SESS_ID_ATTEMPTS)
372 /* else - woops a session_id match */
374 * XXX We should also check the external cache -- but the probability of
375 * a collision is negligible, and we could not prevent the concurrent
376 * creation of sessions with identical IDs since we currently don't have
377 * means to atomically check whether a session ID already exists and make
378 * a reservation for it if it does not (this problem applies to the
379 * internal cache as well).
384 int ssl_get_new_session(SSL *s, int session)
386 /* This gets used by clients and servers. */
389 SSL_SESSION *ss = NULL;
390 GEN_SESSION_CB cb = def_generate_session_id;
392 if ((ss = SSL_SESSION_new()) == NULL)
395 /* If the context has a default timeout, use it */
396 if (s->session_ctx->session_timeout == 0)
397 ss->timeout = SSL_get_default_timeout(s);
399 ss->timeout = s->session_ctx->session_timeout;
401 SSL_SESSION_free(s->session);
405 if (s->version == SSL3_VERSION) {
406 ss->ssl_version = SSL3_VERSION;
407 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
408 } else if (s->version == TLS1_VERSION) {
409 ss->ssl_version = TLS1_VERSION;
410 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
411 } else if (s->version == TLS1_1_VERSION) {
412 ss->ssl_version = TLS1_1_VERSION;
413 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
414 } else if (s->version == TLS1_2_VERSION) {
415 ss->ssl_version = TLS1_2_VERSION;
416 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
417 } else if (s->version == DTLS1_BAD_VER) {
418 ss->ssl_version = DTLS1_BAD_VER;
419 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
420 } else if (s->version == DTLS1_VERSION) {
421 ss->ssl_version = DTLS1_VERSION;
422 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
423 } else if (s->version == DTLS1_2_VERSION) {
424 ss->ssl_version = DTLS1_2_VERSION;
425 ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
427 SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
428 SSL_SESSION_free(ss);
433 * If RFC5077 ticket, use empty session ID (as server).
435 * (a) ssl_get_prev_session() does lookahead into the
436 * ClientHello extensions to find the session ticket.
437 * When ssl_get_prev_session() fails, s3_srvr.c calls
438 * ssl_get_new_session() in ssl3_get_client_hello().
439 * At that point, it has not yet parsed the extensions,
440 * however, because of the lookahead, it already knows
441 * whether a ticket is expected or not.
443 * (b) s3_clnt.c calls ssl_get_new_session() before parsing
444 * ServerHello extensions, and before recording the session
445 * ID received from the server, so this block is a noop.
447 if (s->tlsext_ticket_expected) {
448 ss->session_id_length = 0;
452 /* Choose which callback will set the session ID */
453 CRYPTO_THREAD_read_lock(s->lock);
454 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
455 if (s->generate_session_id)
456 cb = s->generate_session_id;
457 else if (s->session_ctx->generate_session_id)
458 cb = s->session_ctx->generate_session_id;
459 CRYPTO_THREAD_unlock(s->session_ctx->lock);
460 CRYPTO_THREAD_unlock(s->lock);
461 /* Choose a session ID */
462 tmp = ss->session_id_length;
463 if (!cb(s, ss->session_id, &tmp)) {
464 /* The callback failed */
465 SSLerr(SSL_F_SSL_GET_NEW_SESSION,
466 SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
467 SSL_SESSION_free(ss);
471 * Don't allow the callback to set the session length to zero. nor
472 * set it higher than it was.
474 if (tmp == 0 || tmp > ss->session_id_length) {
475 /* The callback set an illegal length */
476 SSLerr(SSL_F_SSL_GET_NEW_SESSION,
477 SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
478 SSL_SESSION_free(ss);
481 ss->session_id_length = tmp;
482 /* Finally, check for a conflict */
483 if (SSL_has_matching_session_id(s, ss->session_id,
484 ss->session_id_length)) {
485 SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT);
486 SSL_SESSION_free(ss);
491 if (s->tlsext_hostname) {
492 ss->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
493 if (ss->tlsext_hostname == NULL) {
494 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
495 SSL_SESSION_free(ss);
500 ss->session_id_length = 0;
503 if (s->sid_ctx_length > sizeof ss->sid_ctx) {
504 SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
505 SSL_SESSION_free(ss);
508 memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
509 ss->sid_ctx_length = s->sid_ctx_length;
511 ss->ssl_version = s->version;
512 ss->verify_result = X509_V_OK;
514 /* If client supports extended master secret set it in session */
515 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
516 ss->flags |= SSL_SESS_FLAG_EXTMS;
522 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
523 * connection. It is only called by servers.
525 * ext: ClientHello extensions (including length prefix)
526 * session_id: ClientHello session ID.
530 * 0: a session may have been found.
533 * - If a session is found then s->session is pointed at it (after freeing an
534 * existing session if need be) and s->verify_result is set from the session.
535 * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
536 * if the server should issue a new session ticket (to 0 otherwise).
538 int ssl_get_prev_session(SSL *s, const PACKET *ext, const PACKET *session_id)
540 /* This is used only by servers. */
542 SSL_SESSION *ret = NULL;
544 int try_session_cache = 1;
547 if (PACKET_remaining(session_id) == 0)
548 try_session_cache = 0;
550 /* sets s->tlsext_ticket_expected and extended master secret flag */
551 r = tls_check_serverhello_tlsext_early(s, ext, session_id, &ret);
553 case -1: /* Error during processing */
556 case 0: /* No ticket found */
557 case 1: /* Zero length ticket found */
558 break; /* Ok to carry on processing session id. */
559 case 2: /* Ticket found but not decrypted. */
560 case 3: /* Ticket decrypted, *ret has been set. */
561 try_session_cache = 0;
567 if (try_session_cache &&
569 !(s->session_ctx->session_cache_mode &
570 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
573 data.ssl_version = s->version;
574 if (!PACKET_copy_all(session_id, data.session_id,
575 sizeof(data.session_id),
579 data.session_id_length = local_len;
580 CRYPTO_THREAD_read_lock(s->session_ctx->lock);
581 ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
583 /* don't allow other threads to steal it: */
584 SSL_SESSION_up_ref(ret);
586 CRYPTO_THREAD_unlock(s->session_ctx->lock);
588 s->session_ctx->stats.sess_miss++;
591 if (try_session_cache &&
592 ret == NULL && s->session_ctx->get_session_cb != NULL) {
594 ret = s->session_ctx->get_session_cb(s, PACKET_data(session_id),
595 PACKET_remaining(session_id),
599 s->session_ctx->stats.sess_cb_hit++;
602 * Increment reference count now if the session callback asks us
603 * to do so (note that if the session structures returned by the
604 * callback are shared between threads, it must handle the
605 * reference count itself [i.e. copy == 0], or things won't be
609 SSL_SESSION_up_ref(ret);
612 * Add the externally cached session to the internal cache as
613 * well if and only if we are supposed to.
616 (s->session_ctx->session_cache_mode &
617 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
619 * The following should not return 1, otherwise, things are
622 if (SSL_CTX_add_session(s->session_ctx, ret))
631 /* Now ret is non-NULL and we own one of its reference counts. */
633 if (ret->sid_ctx_length != s->sid_ctx_length
634 || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
636 * We have the session requested by the client, but we don't want to
637 * use it in this context.
639 goto err; /* treat like cache miss */
642 if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
644 * We can't be sure if this session is being used out of context,
645 * which is especially important for SSL_VERIFY_PEER. The application
646 * should have used SSL[_CTX]_set_session_id_context. For this error
647 * case, we generate an error instead of treating the event like a
648 * cache miss (otherwise it would be easy for applications to
649 * effectively disable the session cache by accident without anyone
653 SSLerr(SSL_F_SSL_GET_PREV_SESSION,
654 SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
659 if (ret->cipher == NULL) {
660 unsigned char buf[5], *p;
666 if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR)
667 ret->cipher = ssl_get_cipher_by_char(s, &(buf[2]));
669 ret->cipher = ssl_get_cipher_by_char(s, &(buf[1]));
670 if (ret->cipher == NULL)
674 if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
675 s->session_ctx->stats.sess_timeout++;
676 if (try_session_cache) {
677 /* session was from the cache, so remove it */
678 SSL_CTX_remove_session(s->session_ctx, ret);
683 /* Check extended master secret extension consistency */
684 if (ret->flags & SSL_SESS_FLAG_EXTMS) {
685 /* If old session includes extms, but new does not: abort handshake */
686 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
687 SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS);
688 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
692 } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
693 /* If new session includes extms, but old does not: do not resume */
697 s->session_ctx->stats.sess_hit++;
699 SSL_SESSION_free(s->session);
701 s->verify_result = s->session->verify_result;
706 SSL_SESSION_free(ret);
708 if (!try_session_cache) {
710 * The session was from a ticket, so we should issue a ticket for
713 s->tlsext_ticket_expected = 1;
722 int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
728 * add just 1 reference count for the SSL_CTX's session cache even though
729 * it has two ways of access: each session is in a doubly linked list and
732 SSL_SESSION_up_ref(c);
734 * if session c is in already in cache, we take back the increment later
737 CRYPTO_THREAD_write_lock(ctx->lock);
738 s = lh_SSL_SESSION_insert(ctx->sessions, c);
741 * s != NULL iff we already had a session with the given PID. In this
742 * case, s == c should hold (then we did not really modify
743 * ctx->sessions), or we're in trouble.
745 if (s != NULL && s != c) {
746 /* We *are* in trouble ... */
747 SSL_SESSION_list_remove(ctx, s);
750 * ... so pretend the other session did not exist in cache (we cannot
751 * handle two SSL_SESSION structures with identical session ID in the
752 * same cache, which could happen e.g. when two threads concurrently
753 * obtain the same session from an external cache)
758 /* Put at the head of the queue unless it is already in the cache */
760 SSL_SESSION_list_add(ctx, c);
764 * existing cache entry -- decrement previously incremented reference
765 * count because it already takes into account the cache
768 SSL_SESSION_free(s); /* s == c */
772 * new cache entry -- remove old ones if cache has become too large
777 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
778 while (SSL_CTX_sess_number(ctx) >
779 SSL_CTX_sess_get_cache_size(ctx)) {
780 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
783 ctx->stats.sess_cache_full++;
787 CRYPTO_THREAD_unlock(ctx->lock);
791 int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
793 return remove_session_lock(ctx, c, 1);
796 static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
801 if ((c != NULL) && (c->session_id_length != 0)) {
803 CRYPTO_THREAD_write_lock(ctx->lock);
804 if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
806 r = lh_SSL_SESSION_delete(ctx->sessions, c);
807 SSL_SESSION_list_remove(ctx, c);
811 CRYPTO_THREAD_unlock(ctx->lock);
814 r->not_resumable = 1;
815 if (ctx->remove_session_cb != NULL)
816 ctx->remove_session_cb(ctx, r);
824 void SSL_SESSION_free(SSL_SESSION *ss)
831 CRYPTO_atomic_add(&ss->references, -1, &i, ss->lock);
832 REF_PRINT_COUNT("SSL_SESSION", ss);
835 REF_ASSERT_ISNT(i < 0);
837 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
839 OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
840 OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
842 sk_X509_pop_free(ss->peer_chain, X509_free);
843 sk_SSL_CIPHER_free(ss->ciphers);
844 OPENSSL_free(ss->tlsext_hostname);
845 OPENSSL_free(ss->tlsext_tick);
846 #ifndef OPENSSL_NO_EC
847 ss->tlsext_ecpointformatlist_length = 0;
848 OPENSSL_free(ss->tlsext_ecpointformatlist);
849 ss->tlsext_ellipticcurvelist_length = 0;
850 OPENSSL_free(ss->tlsext_ellipticcurvelist);
851 #endif /* OPENSSL_NO_EC */
852 #ifndef OPENSSL_NO_PSK
853 OPENSSL_free(ss->psk_identity_hint);
854 OPENSSL_free(ss->psk_identity);
856 #ifndef OPENSSL_NO_SRP
857 OPENSSL_free(ss->srp_username);
859 CRYPTO_THREAD_lock_free(ss->lock);
860 OPENSSL_clear_free(ss, sizeof(*ss));
863 int SSL_SESSION_up_ref(SSL_SESSION *ss)
867 if (CRYPTO_atomic_add(&ss->references, 1, &i, ss->lock) <= 0)
870 REF_PRINT_COUNT("SSL_SESSION", ss);
871 REF_ASSERT_ISNT(i < 2);
872 return ((i > 1) ? 1 : 0);
875 int SSL_set_session(SSL *s, SSL_SESSION *session)
878 if (session != NULL) {
879 if (s->ctx->method != s->method) {
880 if (!SSL_set_ssl_method(s, s->ctx->method))
884 SSL_SESSION_up_ref(session);
885 SSL_SESSION_free(s->session);
886 s->session = session;
887 s->verify_result = s->session->verify_result;
890 SSL_SESSION_free(s->session);
892 if (s->ctx->method != s->method) {
893 if (!SSL_set_ssl_method(s, s->ctx->method))
901 long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
909 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
916 long SSL_SESSION_get_time(const SSL_SESSION *s)
923 long SSL_SESSION_set_time(SSL_SESSION *s, long t)
931 const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
933 return s->tlsext_hostname;
936 int SSL_SESSION_has_ticket(const SSL_SESSION *s)
938 return (s->tlsext_ticklen > 0) ? 1 : 0;
941 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
943 return s->tlsext_tick_lifetime_hint;
946 void SSL_SESSION_get0_ticket(const SSL_SESSION *s, unsigned char **tick,
949 *len = s->tlsext_ticklen;
951 *tick = s->tlsext_tick;
954 X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
959 int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
960 unsigned int sid_ctx_len)
962 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
963 SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
964 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
967 s->sid_ctx_length = sid_ctx_len;
968 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
973 long SSL_CTX_set_timeout(SSL_CTX *s, long t)
978 l = s->session_timeout;
979 s->session_timeout = t;
983 long SSL_CTX_get_timeout(const SSL_CTX *s)
987 return (s->session_timeout);
990 int SSL_set_session_secret_cb(SSL *s,
991 int (*tls_session_secret_cb) (SSL *s,
1003 s->tls_session_secret_cb = tls_session_secret_cb;
1004 s->tls_session_secret_cb_arg = arg;
1008 int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1013 s->tls_session_ticket_ext_cb = cb;
1014 s->tls_session_ticket_ext_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->tlsext_session_ticket);
1022 s->tlsext_session_ticket = NULL;
1023 s->tlsext_session_ticket =
1024 OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1025 if (s->tlsext_session_ticket == NULL) {
1026 SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1031 s->tlsext_session_ticket->length = ext_len;
1032 s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
1033 memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
1035 s->tlsext_session_ticket->length = 0;
1036 s->tlsext_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 = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;
1081 CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;
1082 lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
1083 CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 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->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,
1150 ctx->new_session_cb = cb;
1153 int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
1154 return ctx->new_session_cb;
1157 void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1158 void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
1160 ctx->remove_session_cb = cb;
1163 void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
1164 SSL_SESSION *sess) {
1165 return ctx->remove_session_cb;
1168 void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1169 SSL_SESSION *(*cb) (struct ssl_st *ssl,
1170 const unsigned char *data,
1171 int len, int *copy))
1173 ctx->get_session_cb = cb;
1176 SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
1177 const unsigned char *data,
1178 int len, int *copy) {
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, const unsigned char *cookie,
1233 unsigned int cookie_len))
1235 ctx->app_verify_cookie_cb = cb;
1238 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION,