xref: /freebsd/crypto/openssl/ssl/ssl_sess.c (revision db522d3ae42d8f706499b4b4bc97836292ab180b)
174664626SKris Kennaway /* ssl/ssl_sess.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include <openssl/lhash.h>
6174664626SKris Kennaway #include <openssl/rand.h>
62db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_ENGINE
63db522d3aSSimon L. B. Nielsen #include <openssl/engine.h>
64db522d3aSSimon L. B. Nielsen #endif
6574664626SKris Kennaway #include "ssl_locl.h"
6674664626SKris Kennaway 
6774664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
6874664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
6974664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
7074664626SKris Kennaway 
713b4e3dcbSSimon L. B. Nielsen SSL_SESSION *SSL_get_session(const SSL *ssl)
72f579bf8eSKris Kennaway /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
7374664626SKris Kennaway 	{
7474664626SKris Kennaway 	return(ssl->session);
7574664626SKris Kennaway 	}
7674664626SKris Kennaway 
77f579bf8eSKris Kennaway SSL_SESSION *SSL_get1_session(SSL *ssl)
78f579bf8eSKris Kennaway /* variant of SSL_get_session: caller really gets something */
79f579bf8eSKris Kennaway 	{
80f579bf8eSKris Kennaway 	SSL_SESSION *sess;
81f579bf8eSKris Kennaway 	/* Need to lock this all up rather than just use CRYPTO_add so that
82f579bf8eSKris Kennaway 	 * somebody doesn't free ssl->session between when we check it's
83f579bf8eSKris Kennaway 	 * non-null and when we up the reference count. */
8450ef0093SJacques Vidrine 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
85f579bf8eSKris Kennaway 	sess = ssl->session;
86f579bf8eSKris Kennaway 	if(sess)
87f579bf8eSKris Kennaway 		sess->references++;
8850ef0093SJacques Vidrine 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
89f579bf8eSKris Kennaway 	return(sess);
90f579bf8eSKris Kennaway 	}
91f579bf8eSKris Kennaway 
92f579bf8eSKris Kennaway int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
93f579bf8eSKris Kennaway 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
9474664626SKris Kennaway 	{
955c87c606SMark Murray 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
965c87c606SMark Murray 			new_func, dup_func, free_func);
9774664626SKris Kennaway 	}
9874664626SKris Kennaway 
9974664626SKris Kennaway int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
10074664626SKris Kennaway 	{
10174664626SKris Kennaway 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
10274664626SKris Kennaway 	}
10374664626SKris Kennaway 
1043b4e3dcbSSimon L. B. Nielsen void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
10574664626SKris Kennaway 	{
10674664626SKris Kennaway 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
10774664626SKris Kennaway 	}
10874664626SKris Kennaway 
10974664626SKris Kennaway SSL_SESSION *SSL_SESSION_new(void)
11074664626SKris Kennaway 	{
11174664626SKris Kennaway 	SSL_SESSION *ss;
11274664626SKris Kennaway 
113ddd58736SKris Kennaway 	ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
11474664626SKris Kennaway 	if (ss == NULL)
11574664626SKris Kennaway 		{
11674664626SKris Kennaway 		SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
11774664626SKris Kennaway 		return(0);
11874664626SKris Kennaway 		}
11974664626SKris Kennaway 	memset(ss,0,sizeof(SSL_SESSION));
12074664626SKris Kennaway 
121f579bf8eSKris Kennaway 	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
12274664626SKris Kennaway 	ss->references=1;
12374664626SKris Kennaway 	ss->timeout=60*5+4; /* 5 minute timeout by default */
1243b4e3dcbSSimon L. B. Nielsen 	ss->time=(unsigned long)time(NULL);
12574664626SKris Kennaway 	ss->prev=NULL;
12674664626SKris Kennaway 	ss->next=NULL;
12774664626SKris Kennaway 	ss->compress_meth=0;
128db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
129db522d3aSSimon L. B. Nielsen 	ss->tlsext_hostname = NULL;
130db522d3aSSimon L. B. Nielsen #endif
1315c87c606SMark Murray 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
13274664626SKris Kennaway 	return(ss);
13374664626SKris Kennaway 	}
13474664626SKris Kennaway 
1353b4e3dcbSSimon L. B. Nielsen const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
1363b4e3dcbSSimon L. B. Nielsen 	{
1373b4e3dcbSSimon L. B. Nielsen 	if(len)
1383b4e3dcbSSimon L. B. Nielsen 		*len = s->session_id_length;
1393b4e3dcbSSimon L. B. Nielsen 	return s->session_id;
1403b4e3dcbSSimon L. B. Nielsen 	}
1413b4e3dcbSSimon L. B. Nielsen 
1425c87c606SMark Murray /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
1435c87c606SMark Murray  * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
1445c87c606SMark Murray  * until we have no conflict is going to complete in one iteration pretty much
1455c87c606SMark Murray  * "most" of the time (btw: understatement). So, if it takes us 10 iterations
1465c87c606SMark Murray  * and we still can't avoid a conflict - well that's a reasonable point to call
1475c87c606SMark Murray  * it quits. Either the RAND code is broken or someone is trying to open roughly
1485c87c606SMark Murray  * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
1495c87c606SMark Murray  * store that many sessions is perhaps a more interesting question ... */
1505c87c606SMark Murray 
1515c87c606SMark Murray #define MAX_SESS_ID_ATTEMPTS 10
1525c87c606SMark Murray static int def_generate_session_id(const SSL *ssl, unsigned char *id,
1535c87c606SMark Murray 				unsigned int *id_len)
1545c87c606SMark Murray {
1555c87c606SMark Murray 	unsigned int retry = 0;
1565c87c606SMark Murray 	do
1576be8ae07SJacques Vidrine 		if (RAND_pseudo_bytes(id, *id_len) <= 0)
1586be8ae07SJacques Vidrine 			return 0;
1595c87c606SMark Murray 	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
1605c87c606SMark Murray 		(++retry < MAX_SESS_ID_ATTEMPTS));
1615c87c606SMark Murray 	if(retry < MAX_SESS_ID_ATTEMPTS)
1625c87c606SMark Murray 		return 1;
1635c87c606SMark Murray 	/* else - woops a session_id match */
1645c87c606SMark Murray 	/* XXX We should also check the external cache --
1655c87c606SMark Murray 	 * but the probability of a collision is negligible, and
1665c87c606SMark Murray 	 * we could not prevent the concurrent creation of sessions
1675c87c606SMark Murray 	 * with identical IDs since we currently don't have means
1685c87c606SMark Murray 	 * to atomically check whether a session ID already exists
1695c87c606SMark Murray 	 * and make a reservation for it if it does not
1705c87c606SMark Murray 	 * (this problem applies to the internal cache as well).
1715c87c606SMark Murray 	 */
1725c87c606SMark Murray 	return 0;
1735c87c606SMark Murray }
1745c87c606SMark Murray 
17574664626SKris Kennaway int ssl_get_new_session(SSL *s, int session)
17674664626SKris Kennaway 	{
17774664626SKris Kennaway 	/* This gets used by clients and servers. */
17874664626SKris Kennaway 
1795c87c606SMark Murray 	unsigned int tmp;
18074664626SKris Kennaway 	SSL_SESSION *ss=NULL;
1815c87c606SMark Murray 	GEN_SESSION_CB cb = def_generate_session_id;
18274664626SKris Kennaway 
18374664626SKris Kennaway 	if ((ss=SSL_SESSION_new()) == NULL) return(0);
18474664626SKris Kennaway 
18574664626SKris Kennaway 	/* If the context has a default timeout, use it */
18674664626SKris Kennaway 	if (s->ctx->session_timeout == 0)
18774664626SKris Kennaway 		ss->timeout=SSL_get_default_timeout(s);
18874664626SKris Kennaway 	else
18974664626SKris Kennaway 		ss->timeout=s->ctx->session_timeout;
19074664626SKris Kennaway 
19174664626SKris Kennaway 	if (s->session != NULL)
19274664626SKris Kennaway 		{
19374664626SKris Kennaway 		SSL_SESSION_free(s->session);
19474664626SKris Kennaway 		s->session=NULL;
19574664626SKris Kennaway 		}
19674664626SKris Kennaway 
19774664626SKris Kennaway 	if (session)
19874664626SKris Kennaway 		{
19974664626SKris Kennaway 		if (s->version == SSL2_VERSION)
20074664626SKris Kennaway 			{
20174664626SKris Kennaway 			ss->ssl_version=SSL2_VERSION;
20274664626SKris Kennaway 			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
20374664626SKris Kennaway 			}
20474664626SKris Kennaway 		else if (s->version == SSL3_VERSION)
20574664626SKris Kennaway 			{
20674664626SKris Kennaway 			ss->ssl_version=SSL3_VERSION;
20774664626SKris Kennaway 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
20874664626SKris Kennaway 			}
20974664626SKris Kennaway 		else if (s->version == TLS1_VERSION)
21074664626SKris Kennaway 			{
21174664626SKris Kennaway 			ss->ssl_version=TLS1_VERSION;
21274664626SKris Kennaway 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
21374664626SKris Kennaway 			}
2143b4e3dcbSSimon L. B. Nielsen 		else if (s->version == DTLS1_VERSION)
2153b4e3dcbSSimon L. B. Nielsen 			{
2163b4e3dcbSSimon L. B. Nielsen 			ss->ssl_version=DTLS1_VERSION;
2173b4e3dcbSSimon L. B. Nielsen 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
2183b4e3dcbSSimon L. B. Nielsen 			}
21974664626SKris Kennaway 		else
22074664626SKris Kennaway 			{
22174664626SKris Kennaway 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
22274664626SKris Kennaway 			SSL_SESSION_free(ss);
22374664626SKris Kennaway 			return(0);
22474664626SKris Kennaway 			}
225db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
226db522d3aSSimon L. B. Nielsen 		/* If RFC4507 ticket use empty session ID */
227db522d3aSSimon L. B. Nielsen 		if (s->tlsext_ticket_expected)
228db522d3aSSimon L. B. Nielsen 			{
229db522d3aSSimon L. B. Nielsen 			ss->session_id_length = 0;
230db522d3aSSimon L. B. Nielsen 			goto sess_id_done;
231db522d3aSSimon L. B. Nielsen 			}
232db522d3aSSimon L. B. Nielsen #endif
2335c87c606SMark Murray 		/* Choose which callback will set the session ID */
23474664626SKris Kennaway 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
2355c87c606SMark Murray 		if(s->generate_session_id)
2365c87c606SMark Murray 			cb = s->generate_session_id;
2375c87c606SMark Murray 		else if(s->ctx->generate_session_id)
2385c87c606SMark Murray 			cb = s->ctx->generate_session_id;
23974664626SKris Kennaway 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
2405c87c606SMark Murray 		/* Choose a session ID */
2415c87c606SMark Murray 		tmp = ss->session_id_length;
2425c87c606SMark Murray 		if(!cb(s, ss->session_id, &tmp))
2435c87c606SMark Murray 			{
2445c87c606SMark Murray 			/* The callback failed */
2455c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2465c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
2475c87c606SMark Murray 			SSL_SESSION_free(ss);
2485c87c606SMark Murray 			return(0);
2495c87c606SMark Murray 			}
2505c87c606SMark Murray 		/* Don't allow the callback to set the session length to zero.
2515c87c606SMark Murray 		 * nor set it higher than it was. */
2525c87c606SMark Murray 		if(!tmp || (tmp > ss->session_id_length))
2535c87c606SMark Murray 			{
2545c87c606SMark Murray 			/* The callback set an illegal length */
2555c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2565c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
2575c87c606SMark Murray 			SSL_SESSION_free(ss);
2585c87c606SMark Murray 			return(0);
2595c87c606SMark Murray 			}
2605c87c606SMark Murray 		/* If the session length was shrunk and we're SSLv2, pad it */
2615c87c606SMark Murray 		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
2625c87c606SMark Murray 			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
2635c87c606SMark Murray 		else
2645c87c606SMark Murray 			ss->session_id_length = tmp;
2655c87c606SMark Murray 		/* Finally, check for a conflict */
2665c87c606SMark Murray 		if(SSL_has_matching_session_id(s, ss->session_id,
2675c87c606SMark Murray 						ss->session_id_length))
2685c87c606SMark Murray 			{
2695c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2705c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_CONFLICT);
2715c87c606SMark Murray 			SSL_SESSION_free(ss);
2725c87c606SMark Murray 			return(0);
27374664626SKris Kennaway 			}
274db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
275db522d3aSSimon L. B. Nielsen 		sess_id_done:
276db522d3aSSimon L. B. Nielsen 		if (s->tlsext_hostname) {
277db522d3aSSimon L. B. Nielsen 			ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
278db522d3aSSimon L. B. Nielsen 			if (ss->tlsext_hostname == NULL) {
279db522d3aSSimon L. B. Nielsen 				SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
280db522d3aSSimon L. B. Nielsen 				SSL_SESSION_free(ss);
281db522d3aSSimon L. B. Nielsen 				return 0;
282db522d3aSSimon L. B. Nielsen 				}
283db522d3aSSimon L. B. Nielsen 			}
284db522d3aSSimon L. B. Nielsen #endif
28574664626SKris Kennaway 		}
28674664626SKris Kennaway 	else
28774664626SKris Kennaway 		{
28874664626SKris Kennaway 		ss->session_id_length=0;
28974664626SKris Kennaway 		}
29074664626SKris Kennaway 
29148454956SJacques Vidrine 	if (s->sid_ctx_length > sizeof ss->sid_ctx)
29248454956SJacques Vidrine 		{
2935c87c606SMark Murray 		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
29448454956SJacques Vidrine 		SSL_SESSION_free(ss);
29548454956SJacques Vidrine 		return 0;
29648454956SJacques Vidrine 		}
29774664626SKris Kennaway 	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
29874664626SKris Kennaway 	ss->sid_ctx_length=s->sid_ctx_length;
29974664626SKris Kennaway 	s->session=ss;
30074664626SKris Kennaway 	ss->ssl_version=s->version;
301f579bf8eSKris Kennaway 	ss->verify_result = X509_V_OK;
30274664626SKris Kennaway 
30374664626SKris Kennaway 	return(1);
30474664626SKris Kennaway 	}
30574664626SKris Kennaway 
306db522d3aSSimon L. B. Nielsen int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
307db522d3aSSimon L. B. Nielsen 			const unsigned char *limit)
30874664626SKris Kennaway 	{
30974664626SKris Kennaway 	/* This is used only by servers. */
31074664626SKris Kennaway 
311db522d3aSSimon L. B. Nielsen 	SSL_SESSION *ret=NULL;
31274664626SKris Kennaway 	int fatal = 0;
313db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
314db522d3aSSimon L. B. Nielsen 	int r;
315db522d3aSSimon L. B. Nielsen #endif
31674664626SKris Kennaway 
31774664626SKris Kennaway 	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
31874664626SKris Kennaway 		goto err;
319db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
320db522d3aSSimon L. B. Nielsen  	r = tls1_process_ticket(s, session_id, len, limit, &ret);
321db522d3aSSimon L. B. Nielsen 	if (r == -1)
32274664626SKris Kennaway 		{
323db522d3aSSimon L. B. Nielsen 		fatal = 1;
324db522d3aSSimon L. B. Nielsen  		goto err;
325db522d3aSSimon L. B. Nielsen 		}
326db522d3aSSimon L. B. Nielsen 	else if (r == 0 || (!ret && !len))
327db522d3aSSimon L. B. Nielsen 		goto err;
328db522d3aSSimon L. B. Nielsen 	else if (!ret && !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
329db522d3aSSimon L. B. Nielsen #else
330db522d3aSSimon L. B. Nielsen 	if (len == 0)
331db522d3aSSimon L. B. Nielsen 		goto err;
332db522d3aSSimon L. B. Nielsen 	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
333db522d3aSSimon L. B. Nielsen #endif
334db522d3aSSimon L. B. Nielsen 		{
335db522d3aSSimon L. B. Nielsen 		SSL_SESSION data;
336db522d3aSSimon L. B. Nielsen 		data.ssl_version=s->version;
337db522d3aSSimon L. B. Nielsen 		data.session_id_length=len;
338db522d3aSSimon L. B. Nielsen 		if (len == 0)
339db522d3aSSimon L. B. Nielsen 			return 0;
340db522d3aSSimon L. B. Nielsen  		memcpy(data.session_id,session_id,len);
34174664626SKris Kennaway 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
342f579bf8eSKris Kennaway 		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
34374664626SKris Kennaway 		if (ret != NULL)
34474664626SKris Kennaway 		    /* don't allow other threads to steal it: */
34574664626SKris Kennaway 		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
34674664626SKris Kennaway 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
34774664626SKris Kennaway 		}
34874664626SKris Kennaway 
34974664626SKris Kennaway 	if (ret == NULL)
35074664626SKris Kennaway 		{
35174664626SKris Kennaway 		int copy=1;
35274664626SKris Kennaway 
35374664626SKris Kennaway 		s->ctx->stats.sess_miss++;
35474664626SKris Kennaway 		ret=NULL;
35574664626SKris Kennaway 		if (s->ctx->get_session_cb != NULL
35674664626SKris Kennaway 		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
35774664626SKris Kennaway 		       != NULL)
35874664626SKris Kennaway 			{
35974664626SKris Kennaway 			s->ctx->stats.sess_cb_hit++;
36074664626SKris Kennaway 
36174664626SKris Kennaway 			/* Increment reference count now if the session callback
36274664626SKris Kennaway 			 * asks us to do so (note that if the session structures
36374664626SKris Kennaway 			 * returned by the callback are shared between threads,
36474664626SKris Kennaway 			 * it must handle the reference count itself [i.e. copy == 0],
36574664626SKris Kennaway 			 * or things won't be thread-safe). */
36674664626SKris Kennaway 			if (copy)
36774664626SKris Kennaway 				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
36874664626SKris Kennaway 
3695c87c606SMark Murray 			/* Add the externally cached session to the internal
3705c87c606SMark Murray 			 * cache as well if and only if we are supposed to. */
3715c87c606SMark Murray 			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
37274664626SKris Kennaway 				/* The following should not return 1, otherwise,
37374664626SKris Kennaway 				 * things are very strange */
37474664626SKris Kennaway 				SSL_CTX_add_session(s->ctx,ret);
37574664626SKris Kennaway 			}
37674664626SKris Kennaway 		if (ret == NULL)
37774664626SKris Kennaway 			goto err;
37874664626SKris Kennaway 		}
37974664626SKris Kennaway 
38074664626SKris Kennaway 	/* Now ret is non-NULL, and we own one of its reference counts. */
38174664626SKris Kennaway 
382db522d3aSSimon L. B. Nielsen 	if (ret->sid_ctx_length != s->sid_ctx_length
383db522d3aSSimon L. B. Nielsen 	    || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
38474664626SKris Kennaway 		{
38574664626SKris Kennaway 		/* We've found the session named by the client, but we don't
38674664626SKris Kennaway 		 * want to use it in this context. */
38774664626SKris Kennaway 
38874664626SKris Kennaway #if 0 /* The client cannot always know when a session is not appropriate,
38974664626SKris Kennaway        * so we shouldn't generate an error message. */
39074664626SKris Kennaway 
39174664626SKris Kennaway 		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
39274664626SKris Kennaway #endif
39374664626SKris Kennaway 		goto err; /* treat like cache miss */
39474664626SKris Kennaway 		}
395db522d3aSSimon L. B. Nielsen 
396db522d3aSSimon L. B. Nielsen 	if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
397db522d3aSSimon L. B. Nielsen 		{
398db522d3aSSimon L. B. Nielsen 		/* We can't be sure if this session is being used out of
399db522d3aSSimon L. B. Nielsen 		 * context, which is especially important for SSL_VERIFY_PEER.
400db522d3aSSimon L. B. Nielsen 		 * The application should have used SSL[_CTX]_set_session_id_context.
401db522d3aSSimon L. B. Nielsen 		 *
402db522d3aSSimon L. B. Nielsen 		 * For this error case, we generate an error instead of treating
403db522d3aSSimon L. B. Nielsen 		 * the event like a cache miss (otherwise it would be easy for
404db522d3aSSimon L. B. Nielsen 		 * applications to effectively disable the session cache by
405db522d3aSSimon L. B. Nielsen 		 * accident without anyone noticing).
406db522d3aSSimon L. B. Nielsen 		 */
407db522d3aSSimon L. B. Nielsen 
408db522d3aSSimon L. B. Nielsen 		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
409db522d3aSSimon L. B. Nielsen 		fatal = 1;
410db522d3aSSimon L. B. Nielsen 		goto err;
41174664626SKris Kennaway 		}
41274664626SKris Kennaway 
41374664626SKris Kennaway 	if (ret->cipher == NULL)
41474664626SKris Kennaway 		{
41574664626SKris Kennaway 		unsigned char buf[5],*p;
41674664626SKris Kennaway 		unsigned long l;
41774664626SKris Kennaway 
41874664626SKris Kennaway 		p=buf;
41974664626SKris Kennaway 		l=ret->cipher_id;
42074664626SKris Kennaway 		l2n(l,p);
42174664626SKris Kennaway 		if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
42274664626SKris Kennaway 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
42374664626SKris Kennaway 		else
42474664626SKris Kennaway 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
42574664626SKris Kennaway 		if (ret->cipher == NULL)
42674664626SKris Kennaway 			goto err;
42774664626SKris Kennaway 		}
42874664626SKris Kennaway 
42974664626SKris Kennaway 
43074664626SKris Kennaway #if 0 /* This is way too late. */
43174664626SKris Kennaway 
43274664626SKris Kennaway 	/* If a thread got the session, then 'swaped', and another got
433ddd58736SKris Kennaway 	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
43474664626SKris Kennaway 	 * be in trouble.  So I'll increment it now, then double decrement
43574664626SKris Kennaway 	 * later - am I speaking rubbish?. */
43674664626SKris Kennaway 	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
43774664626SKris Kennaway #endif
43874664626SKris Kennaway 
4393b4e3dcbSSimon L. B. Nielsen 	if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
44074664626SKris Kennaway 		{
44174664626SKris Kennaway 		s->ctx->stats.sess_timeout++;
44274664626SKris Kennaway 		/* remove it from the cache */
44374664626SKris Kennaway 		SSL_CTX_remove_session(s->ctx,ret);
44474664626SKris Kennaway 		goto err;
44574664626SKris Kennaway 		}
44674664626SKris Kennaway 
44774664626SKris Kennaway 	s->ctx->stats.sess_hit++;
44874664626SKris Kennaway 
44974664626SKris Kennaway 	/* ret->time=time(NULL); */ /* rezero timeout? */
45074664626SKris Kennaway 	/* again, just leave the session
45174664626SKris Kennaway 	 * if it is the same session, we have just incremented and
45274664626SKris Kennaway 	 * then decremented the reference count :-) */
45374664626SKris Kennaway 	if (s->session != NULL)
45474664626SKris Kennaway 		SSL_SESSION_free(s->session);
45574664626SKris Kennaway 	s->session=ret;
456f579bf8eSKris Kennaway 	s->verify_result = s->session->verify_result;
45774664626SKris Kennaway 	return(1);
45874664626SKris Kennaway 
45974664626SKris Kennaway  err:
46074664626SKris Kennaway 	if (ret != NULL)
46174664626SKris Kennaway 		SSL_SESSION_free(ret);
46274664626SKris Kennaway 	if (fatal)
46374664626SKris Kennaway 		return -1;
46474664626SKris Kennaway 	else
46574664626SKris Kennaway 		return 0;
46674664626SKris Kennaway 	}
46774664626SKris Kennaway 
46874664626SKris Kennaway int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
46974664626SKris Kennaway 	{
47074664626SKris Kennaway 	int ret=0;
47174664626SKris Kennaway 	SSL_SESSION *s;
47274664626SKris Kennaway 
473f579bf8eSKris Kennaway 	/* add just 1 reference count for the SSL_CTX's session cache
474f579bf8eSKris Kennaway 	 * even though it has two ways of access: each session is in a
475f579bf8eSKris Kennaway 	 * doubly linked list and an lhash */
47674664626SKris Kennaway 	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
477f579bf8eSKris Kennaway 	/* if session c is in already in cache, we take back the increment later */
47874664626SKris Kennaway 
47974664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
480f579bf8eSKris Kennaway 	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
48174664626SKris Kennaway 
482f579bf8eSKris Kennaway 	/* s != NULL iff we already had a session with the given PID.
483f579bf8eSKris Kennaway 	 * In this case, s == c should hold (then we did not really modify
484f579bf8eSKris Kennaway 	 * ctx->sessions), or we're in trouble. */
485f579bf8eSKris Kennaway 	if (s != NULL && s != c)
486f579bf8eSKris Kennaway 		{
487f579bf8eSKris Kennaway 		/* We *are* in trouble ... */
488f579bf8eSKris Kennaway 		SSL_SESSION_list_remove(ctx,s);
489f579bf8eSKris Kennaway 		SSL_SESSION_free(s);
490f579bf8eSKris Kennaway 		/* ... so pretend the other session did not exist in cache
491f579bf8eSKris Kennaway 		 * (we cannot handle two SSL_SESSION structures with identical
492f579bf8eSKris Kennaway 		 * session ID in the same cache, which could happen e.g. when
493f579bf8eSKris Kennaway 		 * two threads concurrently obtain the same session from an external
494f579bf8eSKris Kennaway 		 * cache) */
495f579bf8eSKris Kennaway 		s = NULL;
496f579bf8eSKris Kennaway 		}
497f579bf8eSKris Kennaway 
498f579bf8eSKris Kennaway  	/* Put at the head of the queue unless it is already in the cache */
49974664626SKris Kennaway 	if (s == NULL)
50074664626SKris Kennaway 		SSL_SESSION_list_add(ctx,c);
50174664626SKris Kennaway 
50274664626SKris Kennaway 	if (s != NULL)
50374664626SKris Kennaway 		{
504f579bf8eSKris Kennaway 		/* existing cache entry -- decrement previously incremented reference
505f579bf8eSKris Kennaway 		 * count because it already takes into account the cache */
506f579bf8eSKris Kennaway 
507f579bf8eSKris Kennaway 		SSL_SESSION_free(s); /* s == c */
50874664626SKris Kennaway 		ret=0;
50974664626SKris Kennaway 		}
51074664626SKris Kennaway 	else
51174664626SKris Kennaway 		{
512f579bf8eSKris Kennaway 		/* new cache entry -- remove old ones if cache has become too large */
513f579bf8eSKris Kennaway 
51474664626SKris Kennaway 		ret=1;
51574664626SKris Kennaway 
51674664626SKris Kennaway 		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
51774664626SKris Kennaway 			{
51874664626SKris Kennaway 			while (SSL_CTX_sess_number(ctx) >
51974664626SKris Kennaway 				SSL_CTX_sess_get_cache_size(ctx))
52074664626SKris Kennaway 				{
52174664626SKris Kennaway 				if (!remove_session_lock(ctx,
52274664626SKris Kennaway 					ctx->session_cache_tail, 0))
52374664626SKris Kennaway 					break;
52474664626SKris Kennaway 				else
52574664626SKris Kennaway 					ctx->stats.sess_cache_full++;
52674664626SKris Kennaway 				}
52774664626SKris Kennaway 			}
52874664626SKris Kennaway 		}
52974664626SKris Kennaway 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
53074664626SKris Kennaway 	return(ret);
53174664626SKris Kennaway 	}
53274664626SKris Kennaway 
53374664626SKris Kennaway int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
53474664626SKris Kennaway {
53574664626SKris Kennaway 	return remove_session_lock(ctx, c, 1);
53674664626SKris Kennaway }
53774664626SKris Kennaway 
53874664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
53974664626SKris Kennaway 	{
54074664626SKris Kennaway 	SSL_SESSION *r;
54174664626SKris Kennaway 	int ret=0;
54274664626SKris Kennaway 
54374664626SKris Kennaway 	if ((c != NULL) && (c->session_id_length != 0))
54474664626SKris Kennaway 		{
54574664626SKris Kennaway 		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
546c1803d78SJacques Vidrine 		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
54774664626SKris Kennaway 			{
54874664626SKris Kennaway 			ret=1;
549c1803d78SJacques Vidrine 			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
55074664626SKris Kennaway 			SSL_SESSION_list_remove(ctx,c);
55174664626SKris Kennaway 			}
55274664626SKris Kennaway 
55374664626SKris Kennaway 		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
55474664626SKris Kennaway 
55574664626SKris Kennaway 		if (ret)
55674664626SKris Kennaway 			{
55774664626SKris Kennaway 			r->not_resumable=1;
55874664626SKris Kennaway 			if (ctx->remove_session_cb != NULL)
55974664626SKris Kennaway 				ctx->remove_session_cb(ctx,r);
56074664626SKris Kennaway 			SSL_SESSION_free(r);
56174664626SKris Kennaway 			}
56274664626SKris Kennaway 		}
56374664626SKris Kennaway 	else
56474664626SKris Kennaway 		ret=0;
56574664626SKris Kennaway 	return(ret);
56674664626SKris Kennaway 	}
56774664626SKris Kennaway 
56874664626SKris Kennaway void SSL_SESSION_free(SSL_SESSION *ss)
56974664626SKris Kennaway 	{
57074664626SKris Kennaway 	int i;
57174664626SKris Kennaway 
57274664626SKris Kennaway 	if(ss == NULL)
57374664626SKris Kennaway 	    return;
57474664626SKris Kennaway 
57574664626SKris Kennaway 	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
57674664626SKris Kennaway #ifdef REF_PRINT
57774664626SKris Kennaway 	REF_PRINT("SSL_SESSION",ss);
57874664626SKris Kennaway #endif
57974664626SKris Kennaway 	if (i > 0) return;
58074664626SKris Kennaway #ifdef REF_CHECK
58174664626SKris Kennaway 	if (i < 0)
58274664626SKris Kennaway 		{
58374664626SKris Kennaway 		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
58474664626SKris Kennaway 		abort(); /* ok */
58574664626SKris Kennaway 		}
58674664626SKris Kennaway #endif
58774664626SKris Kennaway 
5885c87c606SMark Murray 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
58974664626SKris Kennaway 
5905c87c606SMark Murray 	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
5915c87c606SMark Murray 	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
5925c87c606SMark Murray 	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
59374664626SKris Kennaway 	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
59474664626SKris Kennaway 	if (ss->peer != NULL) X509_free(ss->peer);
59574664626SKris Kennaway 	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
596db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_TLSEXT
597db522d3aSSimon L. B. Nielsen 	if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
598db522d3aSSimon L. B. Nielsen 	if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
599db522d3aSSimon L. B. Nielsen #endif
6005c87c606SMark Murray 	OPENSSL_cleanse(ss,sizeof(*ss));
601ddd58736SKris Kennaway 	OPENSSL_free(ss);
60274664626SKris Kennaway 	}
60374664626SKris Kennaway 
60474664626SKris Kennaway int SSL_set_session(SSL *s, SSL_SESSION *session)
60574664626SKris Kennaway 	{
60674664626SKris Kennaway 	int ret=0;
60774664626SKris Kennaway 	SSL_METHOD *meth;
60874664626SKris Kennaway 
60974664626SKris Kennaway 	if (session != NULL)
61074664626SKris Kennaway 		{
61174664626SKris Kennaway 		meth=s->ctx->method->get_ssl_method(session->ssl_version);
61274664626SKris Kennaway 		if (meth == NULL)
61374664626SKris Kennaway 			meth=s->method->get_ssl_method(session->ssl_version);
61474664626SKris Kennaway 		if (meth == NULL)
61574664626SKris Kennaway 			{
61674664626SKris Kennaway 			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
61774664626SKris Kennaway 			return(0);
61874664626SKris Kennaway 			}
61974664626SKris Kennaway 
62074664626SKris Kennaway 		if (meth != s->method)
62174664626SKris Kennaway 			{
62274664626SKris Kennaway 			if (!SSL_set_ssl_method(s,meth))
62374664626SKris Kennaway 				return(0);
62474664626SKris Kennaway 			if (s->ctx->session_timeout == 0)
62574664626SKris Kennaway 				session->timeout=SSL_get_default_timeout(s);
62674664626SKris Kennaway 			else
62774664626SKris Kennaway 				session->timeout=s->ctx->session_timeout;
62874664626SKris Kennaway 			}
62974664626SKris Kennaway 
6305c87c606SMark Murray #ifndef OPENSSL_NO_KRB5
6315c87c606SMark Murray                 if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
6325c87c606SMark Murray                     session->krb5_client_princ_len > 0)
6335c87c606SMark Murray                 {
6345471f83eSSimon L. B. Nielsen                     s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
6355c87c606SMark Murray                     memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
6365c87c606SMark Murray                             session->krb5_client_princ_len);
6375c87c606SMark Murray                     s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
6385c87c606SMark Murray                 }
6395c87c606SMark Murray #endif /* OPENSSL_NO_KRB5 */
6405c87c606SMark Murray 
64174664626SKris Kennaway 		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
64274664626SKris Kennaway 		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
64374664626SKris Kennaway 		if (s->session != NULL)
64474664626SKris Kennaway 			SSL_SESSION_free(s->session);
64574664626SKris Kennaway 		s->session=session;
646de7cdddaSKris Kennaway 		s->verify_result = s->session->verify_result;
64774664626SKris Kennaway 		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
64874664626SKris Kennaway 		ret=1;
64974664626SKris Kennaway 		}
65074664626SKris Kennaway 	else
65174664626SKris Kennaway 		{
65274664626SKris Kennaway 		if (s->session != NULL)
65374664626SKris Kennaway 			{
65474664626SKris Kennaway 			SSL_SESSION_free(s->session);
65574664626SKris Kennaway 			s->session=NULL;
65674664626SKris Kennaway 			}
65774664626SKris Kennaway 
65874664626SKris Kennaway 		meth=s->ctx->method;
65974664626SKris Kennaway 		if (meth != s->method)
66074664626SKris Kennaway 			{
66174664626SKris Kennaway 			if (!SSL_set_ssl_method(s,meth))
66274664626SKris Kennaway 				return(0);
66374664626SKris Kennaway 			}
66474664626SKris Kennaway 		ret=1;
66574664626SKris Kennaway 		}
66674664626SKris Kennaway 	return(ret);
66774664626SKris Kennaway 	}
66874664626SKris Kennaway 
66974664626SKris Kennaway long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
67074664626SKris Kennaway 	{
67174664626SKris Kennaway 	if (s == NULL) return(0);
67274664626SKris Kennaway 	s->timeout=t;
67374664626SKris Kennaway 	return(1);
67474664626SKris Kennaway 	}
67574664626SKris Kennaway 
6763b4e3dcbSSimon L. B. Nielsen long SSL_SESSION_get_timeout(const SSL_SESSION *s)
67774664626SKris Kennaway 	{
67874664626SKris Kennaway 	if (s == NULL) return(0);
67974664626SKris Kennaway 	return(s->timeout);
68074664626SKris Kennaway 	}
68174664626SKris Kennaway 
6823b4e3dcbSSimon L. B. Nielsen long SSL_SESSION_get_time(const SSL_SESSION *s)
68374664626SKris Kennaway 	{
68474664626SKris Kennaway 	if (s == NULL) return(0);
68574664626SKris Kennaway 	return(s->time);
68674664626SKris Kennaway 	}
68774664626SKris Kennaway 
68874664626SKris Kennaway long SSL_SESSION_set_time(SSL_SESSION *s, long t)
68974664626SKris Kennaway 	{
69074664626SKris Kennaway 	if (s == NULL) return(0);
69174664626SKris Kennaway 	s->time=t;
69274664626SKris Kennaway 	return(t);
69374664626SKris Kennaway 	}
69474664626SKris Kennaway 
69574664626SKris Kennaway long SSL_CTX_set_timeout(SSL_CTX *s, long t)
69674664626SKris Kennaway 	{
69774664626SKris Kennaway 	long l;
69874664626SKris Kennaway 	if (s == NULL) return(0);
69974664626SKris Kennaway 	l=s->session_timeout;
70074664626SKris Kennaway 	s->session_timeout=t;
70174664626SKris Kennaway 	return(l);
70274664626SKris Kennaway 	}
70374664626SKris Kennaway 
7043b4e3dcbSSimon L. B. Nielsen long SSL_CTX_get_timeout(const SSL_CTX *s)
70574664626SKris Kennaway 	{
70674664626SKris Kennaway 	if (s == NULL) return(0);
70774664626SKris Kennaway 	return(s->session_timeout);
70874664626SKris Kennaway 	}
70974664626SKris Kennaway 
71074664626SKris Kennaway typedef struct timeout_param_st
71174664626SKris Kennaway 	{
71274664626SKris Kennaway 	SSL_CTX *ctx;
71374664626SKris Kennaway 	long time;
71474664626SKris Kennaway 	LHASH *cache;
71574664626SKris Kennaway 	} TIMEOUT_PARAM;
71674664626SKris Kennaway 
71774664626SKris Kennaway static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
71874664626SKris Kennaway 	{
71974664626SKris Kennaway 	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
72074664626SKris Kennaway 		{
72174664626SKris Kennaway 		/* The reason we don't call SSL_CTX_remove_session() is to
72274664626SKris Kennaway 		 * save on locking overhead */
723f579bf8eSKris Kennaway 		lh_delete(p->cache,s);
72474664626SKris Kennaway 		SSL_SESSION_list_remove(p->ctx,s);
72574664626SKris Kennaway 		s->not_resumable=1;
72674664626SKris Kennaway 		if (p->ctx->remove_session_cb != NULL)
72774664626SKris Kennaway 			p->ctx->remove_session_cb(p->ctx,s);
72874664626SKris Kennaway 		SSL_SESSION_free(s);
72974664626SKris Kennaway 		}
73074664626SKris Kennaway 	}
73174664626SKris Kennaway 
7325c87c606SMark Murray static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
7335c87c606SMark Murray 
73474664626SKris Kennaway void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
73574664626SKris Kennaway 	{
73674664626SKris Kennaway 	unsigned long i;
73774664626SKris Kennaway 	TIMEOUT_PARAM tp;
73874664626SKris Kennaway 
73974664626SKris Kennaway 	tp.ctx=s;
74074664626SKris Kennaway 	tp.cache=s->sessions;
74174664626SKris Kennaway 	if (tp.cache == NULL) return;
74274664626SKris Kennaway 	tp.time=t;
74374664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
74474664626SKris Kennaway 	i=tp.cache->down_load;
74574664626SKris Kennaway 	tp.cache->down_load=0;
7465c87c606SMark Murray 	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
74774664626SKris Kennaway 	tp.cache->down_load=i;
74874664626SKris Kennaway 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
74974664626SKris Kennaway 	}
75074664626SKris Kennaway 
75174664626SKris Kennaway int ssl_clear_bad_session(SSL *s)
75274664626SKris Kennaway 	{
75374664626SKris Kennaway 	if (	(s->session != NULL) &&
75474664626SKris Kennaway 		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
75574664626SKris Kennaway 		!(SSL_in_init(s) || SSL_in_before(s)))
75674664626SKris Kennaway 		{
75774664626SKris Kennaway 		SSL_CTX_remove_session(s->ctx,s->session);
75874664626SKris Kennaway 		return(1);
75974664626SKris Kennaway 		}
76074664626SKris Kennaway 	else
76174664626SKris Kennaway 		return(0);
76274664626SKris Kennaway 	}
76374664626SKris Kennaway 
76474664626SKris Kennaway /* locked by SSL_CTX in the calling function */
76574664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
76674664626SKris Kennaway 	{
76774664626SKris Kennaway 	if ((s->next == NULL) || (s->prev == NULL)) return;
76874664626SKris Kennaway 
76974664626SKris Kennaway 	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
77074664626SKris Kennaway 		{ /* last element in list */
77174664626SKris Kennaway 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
77274664626SKris Kennaway 			{ /* only one element in list */
77374664626SKris Kennaway 			ctx->session_cache_head=NULL;
77474664626SKris Kennaway 			ctx->session_cache_tail=NULL;
77574664626SKris Kennaway 			}
77674664626SKris Kennaway 		else
77774664626SKris Kennaway 			{
77874664626SKris Kennaway 			ctx->session_cache_tail=s->prev;
77974664626SKris Kennaway 			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
78074664626SKris Kennaway 			}
78174664626SKris Kennaway 		}
78274664626SKris Kennaway 	else
78374664626SKris Kennaway 		{
78474664626SKris Kennaway 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
78574664626SKris Kennaway 			{ /* first element in list */
78674664626SKris Kennaway 			ctx->session_cache_head=s->next;
78774664626SKris Kennaway 			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
78874664626SKris Kennaway 			}
78974664626SKris Kennaway 		else
79074664626SKris Kennaway 			{ /* middle of list */
79174664626SKris Kennaway 			s->next->prev=s->prev;
79274664626SKris Kennaway 			s->prev->next=s->next;
79374664626SKris Kennaway 			}
79474664626SKris Kennaway 		}
79574664626SKris Kennaway 	s->prev=s->next=NULL;
79674664626SKris Kennaway 	}
79774664626SKris Kennaway 
79874664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
79974664626SKris Kennaway 	{
80074664626SKris Kennaway 	if ((s->next != NULL) && (s->prev != NULL))
80174664626SKris Kennaway 		SSL_SESSION_list_remove(ctx,s);
80274664626SKris Kennaway 
80374664626SKris Kennaway 	if (ctx->session_cache_head == NULL)
80474664626SKris Kennaway 		{
80574664626SKris Kennaway 		ctx->session_cache_head=s;
80674664626SKris Kennaway 		ctx->session_cache_tail=s;
80774664626SKris Kennaway 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
80874664626SKris Kennaway 		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
80974664626SKris Kennaway 		}
81074664626SKris Kennaway 	else
81174664626SKris Kennaway 		{
81274664626SKris Kennaway 		s->next=ctx->session_cache_head;
81374664626SKris Kennaway 		s->next->prev=s;
81474664626SKris Kennaway 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
81574664626SKris Kennaway 		ctx->session_cache_head=s;
81674664626SKris Kennaway 		}
81774664626SKris Kennaway 	}
81874664626SKris Kennaway 
8195471f83eSSimon L. B. Nielsen void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
8205471f83eSSimon L. B. Nielsen 	int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
8215471f83eSSimon L. B. Nielsen 	{
8225471f83eSSimon L. B. Nielsen 	ctx->new_session_cb=cb;
8235471f83eSSimon L. B. Nielsen 	}
8245471f83eSSimon L. B. Nielsen 
8255471f83eSSimon L. B. Nielsen int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
8265471f83eSSimon L. B. Nielsen 	{
8275471f83eSSimon L. B. Nielsen 	return ctx->new_session_cb;
8285471f83eSSimon L. B. Nielsen 	}
8295471f83eSSimon L. B. Nielsen 
8305471f83eSSimon L. B. Nielsen void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
8315471f83eSSimon L. B. Nielsen 	void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
8325471f83eSSimon L. B. Nielsen 	{
8335471f83eSSimon L. B. Nielsen 	ctx->remove_session_cb=cb;
8345471f83eSSimon L. B. Nielsen 	}
8355471f83eSSimon L. B. Nielsen 
8365471f83eSSimon L. B. Nielsen void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
8375471f83eSSimon L. B. Nielsen 	{
8385471f83eSSimon L. B. Nielsen 	return ctx->remove_session_cb;
8395471f83eSSimon L. B. Nielsen 	}
8405471f83eSSimon L. B. Nielsen 
8415471f83eSSimon L. B. Nielsen void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
8425471f83eSSimon L. B. Nielsen 	SSL_SESSION *(*cb)(struct ssl_st *ssl,
8435471f83eSSimon L. B. Nielsen 	         unsigned char *data,int len,int *copy))
8445471f83eSSimon L. B. Nielsen 	{
8455471f83eSSimon L. B. Nielsen 	ctx->get_session_cb=cb;
8465471f83eSSimon L. B. Nielsen 	}
8475471f83eSSimon L. B. Nielsen 
8485471f83eSSimon L. B. Nielsen SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
8495471f83eSSimon L. B. Nielsen 	         unsigned char *data,int len,int *copy)
8505471f83eSSimon L. B. Nielsen 	{
8515471f83eSSimon L. B. Nielsen 	return ctx->get_session_cb;
8525471f83eSSimon L. B. Nielsen 	}
8535471f83eSSimon L. B. Nielsen 
8545471f83eSSimon L. B. Nielsen void SSL_CTX_set_info_callback(SSL_CTX *ctx,
8555471f83eSSimon L. B. Nielsen 	void (*cb)(const SSL *ssl,int type,int val))
8565471f83eSSimon L. B. Nielsen 	{
8575471f83eSSimon L. B. Nielsen 	ctx->info_callback=cb;
8585471f83eSSimon L. B. Nielsen 	}
8595471f83eSSimon L. B. Nielsen 
8605471f83eSSimon L. B. Nielsen void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
8615471f83eSSimon L. B. Nielsen 	{
8625471f83eSSimon L. B. Nielsen 	return ctx->info_callback;
8635471f83eSSimon L. B. Nielsen 	}
8645471f83eSSimon L. B. Nielsen 
8655471f83eSSimon L. B. Nielsen void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
8665471f83eSSimon L. B. Nielsen 	int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
8675471f83eSSimon L. B. Nielsen 	{
8685471f83eSSimon L. B. Nielsen 	ctx->client_cert_cb=cb;
8695471f83eSSimon L. B. Nielsen 	}
8705471f83eSSimon L. B. Nielsen 
8715471f83eSSimon L. B. Nielsen int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
8725471f83eSSimon L. B. Nielsen 	{
8735471f83eSSimon L. B. Nielsen 	return ctx->client_cert_cb;
8745471f83eSSimon L. B. Nielsen 	}
8755471f83eSSimon L. B. Nielsen 
876db522d3aSSimon L. B. Nielsen #ifndef OPENSSL_NO_ENGINE
877db522d3aSSimon L. B. Nielsen int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
878db522d3aSSimon L. B. Nielsen 	{
879db522d3aSSimon L. B. Nielsen 	if (!ENGINE_init(e))
880db522d3aSSimon L. B. Nielsen 		{
881db522d3aSSimon L. B. Nielsen 		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
882db522d3aSSimon L. B. Nielsen 		return 0;
883db522d3aSSimon L. B. Nielsen 		}
884db522d3aSSimon L. B. Nielsen 	if(!ENGINE_get_ssl_client_cert_function(e))
885db522d3aSSimon L. B. Nielsen 		{
886db522d3aSSimon L. B. Nielsen 		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD);
887db522d3aSSimon L. B. Nielsen 		ENGINE_finish(e);
888db522d3aSSimon L. B. Nielsen 		return 0;
889db522d3aSSimon L. B. Nielsen 		}
890db522d3aSSimon L. B. Nielsen 	ctx->client_cert_engine = e;
891db522d3aSSimon L. B. Nielsen 	return 1;
892db522d3aSSimon L. B. Nielsen 	}
893db522d3aSSimon L. B. Nielsen #endif
894db522d3aSSimon L. B. Nielsen 
8955471f83eSSimon L. B. Nielsen void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
8965471f83eSSimon L. B. Nielsen 	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
8975471f83eSSimon L. B. Nielsen 	{
8985471f83eSSimon L. B. Nielsen 	ctx->app_gen_cookie_cb=cb;
8995471f83eSSimon L. B. Nielsen 	}
9005471f83eSSimon L. B. Nielsen 
9015471f83eSSimon L. B. Nielsen void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
9025471f83eSSimon L. B. Nielsen 	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
9035471f83eSSimon L. B. Nielsen 	{
9045471f83eSSimon L. B. Nielsen 	ctx->app_verify_cookie_cb=cb;
9055471f83eSSimon L. B. Nielsen 	}
9065471f83eSSimon L. B. Nielsen 
907