xref: /freebsd/crypto/openssl/ssl/ssl_sess.c (revision 6be8ae0724a74a8210e92579a2e3e25fc6a9cb26)
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>
6274664626SKris Kennaway #include "ssl_locl.h"
6374664626SKris Kennaway 
6474664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
6574664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
6674664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
6774664626SKris Kennaway 
6874664626SKris Kennaway SSL_SESSION *SSL_get_session(SSL *ssl)
69f579bf8eSKris Kennaway /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
7074664626SKris Kennaway 	{
7174664626SKris Kennaway 	return(ssl->session);
7274664626SKris Kennaway 	}
7374664626SKris Kennaway 
74f579bf8eSKris Kennaway SSL_SESSION *SSL_get1_session(SSL *ssl)
75f579bf8eSKris Kennaway /* variant of SSL_get_session: caller really gets something */
76f579bf8eSKris Kennaway 	{
77f579bf8eSKris Kennaway 	SSL_SESSION *sess;
78f579bf8eSKris Kennaway 	/* Need to lock this all up rather than just use CRYPTO_add so that
79f579bf8eSKris Kennaway 	 * somebody doesn't free ssl->session between when we check it's
80f579bf8eSKris Kennaway 	 * non-null and when we up the reference count. */
8150ef0093SJacques Vidrine 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
82f579bf8eSKris Kennaway 	sess = ssl->session;
83f579bf8eSKris Kennaway 	if(sess)
84f579bf8eSKris Kennaway 		sess->references++;
8550ef0093SJacques Vidrine 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
86f579bf8eSKris Kennaway 	return(sess);
87f579bf8eSKris Kennaway 	}
88f579bf8eSKris Kennaway 
89f579bf8eSKris Kennaway int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
90f579bf8eSKris Kennaway 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
9174664626SKris Kennaway 	{
925c87c606SMark Murray 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
935c87c606SMark Murray 			new_func, dup_func, free_func);
9474664626SKris Kennaway 	}
9574664626SKris Kennaway 
9674664626SKris Kennaway int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
9774664626SKris Kennaway 	{
9874664626SKris Kennaway 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
9974664626SKris Kennaway 	}
10074664626SKris Kennaway 
10174664626SKris Kennaway void *SSL_SESSION_get_ex_data(SSL_SESSION *s, int idx)
10274664626SKris Kennaway 	{
10374664626SKris Kennaway 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
10474664626SKris Kennaway 	}
10574664626SKris Kennaway 
10674664626SKris Kennaway SSL_SESSION *SSL_SESSION_new(void)
10774664626SKris Kennaway 	{
10874664626SKris Kennaway 	SSL_SESSION *ss;
10974664626SKris Kennaway 
110ddd58736SKris Kennaway 	ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
11174664626SKris Kennaway 	if (ss == NULL)
11274664626SKris Kennaway 		{
11374664626SKris Kennaway 		SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
11474664626SKris Kennaway 		return(0);
11574664626SKris Kennaway 		}
11674664626SKris Kennaway 	memset(ss,0,sizeof(SSL_SESSION));
11774664626SKris Kennaway 
118f579bf8eSKris Kennaway 	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
11974664626SKris Kennaway 	ss->references=1;
12074664626SKris Kennaway 	ss->timeout=60*5+4; /* 5 minute timeout by default */
12174664626SKris Kennaway 	ss->time=time(NULL);
12274664626SKris Kennaway 	ss->prev=NULL;
12374664626SKris Kennaway 	ss->next=NULL;
12474664626SKris Kennaway 	ss->compress_meth=0;
1255c87c606SMark Murray 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
12674664626SKris Kennaway 	return(ss);
12774664626SKris Kennaway 	}
12874664626SKris Kennaway 
1295c87c606SMark Murray /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
1305c87c606SMark Murray  * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
1315c87c606SMark Murray  * until we have no conflict is going to complete in one iteration pretty much
1325c87c606SMark Murray  * "most" of the time (btw: understatement). So, if it takes us 10 iterations
1335c87c606SMark Murray  * and we still can't avoid a conflict - well that's a reasonable point to call
1345c87c606SMark Murray  * it quits. Either the RAND code is broken or someone is trying to open roughly
1355c87c606SMark Murray  * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
1365c87c606SMark Murray  * store that many sessions is perhaps a more interesting question ... */
1375c87c606SMark Murray 
1385c87c606SMark Murray #define MAX_SESS_ID_ATTEMPTS 10
1395c87c606SMark Murray static int def_generate_session_id(const SSL *ssl, unsigned char *id,
1405c87c606SMark Murray 				unsigned int *id_len)
1415c87c606SMark Murray {
1425c87c606SMark Murray 	unsigned int retry = 0;
1435c87c606SMark Murray 	do
1446be8ae07SJacques Vidrine 		if(RAND_pseudo_bytes(id, *id_len) <= 0)
1456be8ae07SJacques Vidrine 			return 0;
1465c87c606SMark Murray 	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
1475c87c606SMark Murray 		(++retry < MAX_SESS_ID_ATTEMPTS));
1485c87c606SMark Murray 	if(retry < MAX_SESS_ID_ATTEMPTS)
1495c87c606SMark Murray 		return 1;
1505c87c606SMark Murray 	/* else - woops a session_id match */
1515c87c606SMark Murray 	/* XXX We should also check the external cache --
1525c87c606SMark Murray 	 * but the probability of a collision is negligible, and
1535c87c606SMark Murray 	 * we could not prevent the concurrent creation of sessions
1545c87c606SMark Murray 	 * with identical IDs since we currently don't have means
1555c87c606SMark Murray 	 * to atomically check whether a session ID already exists
1565c87c606SMark Murray 	 * and make a reservation for it if it does not
1575c87c606SMark Murray 	 * (this problem applies to the internal cache as well).
1585c87c606SMark Murray 	 */
1595c87c606SMark Murray 	return 0;
1605c87c606SMark Murray }
1615c87c606SMark Murray 
16274664626SKris Kennaway int ssl_get_new_session(SSL *s, int session)
16374664626SKris Kennaway 	{
16474664626SKris Kennaway 	/* This gets used by clients and servers. */
16574664626SKris Kennaway 
1665c87c606SMark Murray 	unsigned int tmp;
16774664626SKris Kennaway 	SSL_SESSION *ss=NULL;
1685c87c606SMark Murray 	GEN_SESSION_CB cb = def_generate_session_id;
16974664626SKris Kennaway 
17074664626SKris Kennaway 	if ((ss=SSL_SESSION_new()) == NULL) return(0);
17174664626SKris Kennaway 
17274664626SKris Kennaway 	/* If the context has a default timeout, use it */
17374664626SKris Kennaway 	if (s->ctx->session_timeout == 0)
17474664626SKris Kennaway 		ss->timeout=SSL_get_default_timeout(s);
17574664626SKris Kennaway 	else
17674664626SKris Kennaway 		ss->timeout=s->ctx->session_timeout;
17774664626SKris Kennaway 
17874664626SKris Kennaway 	if (s->session != NULL)
17974664626SKris Kennaway 		{
18074664626SKris Kennaway 		SSL_SESSION_free(s->session);
18174664626SKris Kennaway 		s->session=NULL;
18274664626SKris Kennaway 		}
18374664626SKris Kennaway 
18474664626SKris Kennaway 	if (session)
18574664626SKris Kennaway 		{
18674664626SKris Kennaway 		if (s->version == SSL2_VERSION)
18774664626SKris Kennaway 			{
18874664626SKris Kennaway 			ss->ssl_version=SSL2_VERSION;
18974664626SKris Kennaway 			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
19074664626SKris Kennaway 			}
19174664626SKris Kennaway 		else if (s->version == SSL3_VERSION)
19274664626SKris Kennaway 			{
19374664626SKris Kennaway 			ss->ssl_version=SSL3_VERSION;
19474664626SKris Kennaway 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
19574664626SKris Kennaway 			}
19674664626SKris Kennaway 		else if (s->version == TLS1_VERSION)
19774664626SKris Kennaway 			{
19874664626SKris Kennaway 			ss->ssl_version=TLS1_VERSION;
19974664626SKris Kennaway 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
20074664626SKris Kennaway 			}
20174664626SKris Kennaway 		else
20274664626SKris Kennaway 			{
20374664626SKris Kennaway 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
20474664626SKris Kennaway 			SSL_SESSION_free(ss);
20574664626SKris Kennaway 			return(0);
20674664626SKris Kennaway 			}
2075c87c606SMark Murray 		/* Choose which callback will set the session ID */
20874664626SKris Kennaway 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
2095c87c606SMark Murray 		if(s->generate_session_id)
2105c87c606SMark Murray 			cb = s->generate_session_id;
2115c87c606SMark Murray 		else if(s->ctx->generate_session_id)
2125c87c606SMark Murray 			cb = s->ctx->generate_session_id;
21374664626SKris Kennaway 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
2145c87c606SMark Murray 		/* Choose a session ID */
2155c87c606SMark Murray 		tmp = ss->session_id_length;
2165c87c606SMark Murray 		if(!cb(s, ss->session_id, &tmp))
2175c87c606SMark Murray 			{
2185c87c606SMark Murray 			/* The callback failed */
2195c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2205c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
2215c87c606SMark Murray 			SSL_SESSION_free(ss);
2225c87c606SMark Murray 			return(0);
2235c87c606SMark Murray 			}
2245c87c606SMark Murray 		/* Don't allow the callback to set the session length to zero.
2255c87c606SMark Murray 		 * nor set it higher than it was. */
2265c87c606SMark Murray 		if(!tmp || (tmp > ss->session_id_length))
2275c87c606SMark Murray 			{
2285c87c606SMark Murray 			/* The callback set an illegal length */
2295c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2305c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
2315c87c606SMark Murray 			SSL_SESSION_free(ss);
2325c87c606SMark Murray 			return(0);
2335c87c606SMark Murray 			}
2345c87c606SMark Murray 		/* If the session length was shrunk and we're SSLv2, pad it */
2355c87c606SMark Murray 		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
2365c87c606SMark Murray 			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
2375c87c606SMark Murray 		else
2385c87c606SMark Murray 			ss->session_id_length = tmp;
2395c87c606SMark Murray 		/* Finally, check for a conflict */
2405c87c606SMark Murray 		if(SSL_has_matching_session_id(s, ss->session_id,
2415c87c606SMark Murray 						ss->session_id_length))
2425c87c606SMark Murray 			{
2435c87c606SMark Murray 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2445c87c606SMark Murray 				SSL_R_SSL_SESSION_ID_CONFLICT);
2455c87c606SMark Murray 			SSL_SESSION_free(ss);
2465c87c606SMark Murray 			return(0);
24774664626SKris Kennaway 			}
24874664626SKris Kennaway 		}
24974664626SKris Kennaway 	else
25074664626SKris Kennaway 		{
25174664626SKris Kennaway 		ss->session_id_length=0;
25274664626SKris Kennaway 		}
25374664626SKris Kennaway 
25448454956SJacques Vidrine 	if (s->sid_ctx_length > sizeof ss->sid_ctx)
25548454956SJacques Vidrine 		{
2565c87c606SMark Murray 		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
25748454956SJacques Vidrine 		SSL_SESSION_free(ss);
25848454956SJacques Vidrine 		return 0;
25948454956SJacques Vidrine 		}
26074664626SKris Kennaway 	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
26174664626SKris Kennaway 	ss->sid_ctx_length=s->sid_ctx_length;
26274664626SKris Kennaway 	s->session=ss;
26374664626SKris Kennaway 	ss->ssl_version=s->version;
264f579bf8eSKris Kennaway 	ss->verify_result = X509_V_OK;
26574664626SKris Kennaway 
26674664626SKris Kennaway 	return(1);
26774664626SKris Kennaway 	}
26874664626SKris Kennaway 
26974664626SKris Kennaway int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len)
27074664626SKris Kennaway 	{
27174664626SKris Kennaway 	/* This is used only by servers. */
27274664626SKris Kennaway 
27374664626SKris Kennaway 	SSL_SESSION *ret=NULL,data;
27474664626SKris Kennaway 	int fatal = 0;
27574664626SKris Kennaway 
27674664626SKris Kennaway 	data.ssl_version=s->version;
27774664626SKris Kennaway 	data.session_id_length=len;
27874664626SKris Kennaway 	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
27974664626SKris Kennaway 		goto err;
28074664626SKris Kennaway 	memcpy(data.session_id,session_id,len);
28174664626SKris Kennaway 
28274664626SKris Kennaway 	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
28374664626SKris Kennaway 		{
28474664626SKris Kennaway 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
285f579bf8eSKris Kennaway 		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
28674664626SKris Kennaway 		if (ret != NULL)
28774664626SKris Kennaway 		    /* don't allow other threads to steal it: */
28874664626SKris Kennaway 		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
28974664626SKris Kennaway 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
29074664626SKris Kennaway 		}
29174664626SKris Kennaway 
29274664626SKris Kennaway 	if (ret == NULL)
29374664626SKris Kennaway 		{
29474664626SKris Kennaway 		int copy=1;
29574664626SKris Kennaway 
29674664626SKris Kennaway 		s->ctx->stats.sess_miss++;
29774664626SKris Kennaway 		ret=NULL;
29874664626SKris Kennaway 		if (s->ctx->get_session_cb != NULL
29974664626SKris Kennaway 		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
30074664626SKris Kennaway 		       != NULL)
30174664626SKris Kennaway 			{
30274664626SKris Kennaway 			s->ctx->stats.sess_cb_hit++;
30374664626SKris Kennaway 
30474664626SKris Kennaway 			/* Increment reference count now if the session callback
30574664626SKris Kennaway 			 * asks us to do so (note that if the session structures
30674664626SKris Kennaway 			 * returned by the callback are shared between threads,
30774664626SKris Kennaway 			 * it must handle the reference count itself [i.e. copy == 0],
30874664626SKris Kennaway 			 * or things won't be thread-safe). */
30974664626SKris Kennaway 			if (copy)
31074664626SKris Kennaway 				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
31174664626SKris Kennaway 
3125c87c606SMark Murray 			/* Add the externally cached session to the internal
3135c87c606SMark Murray 			 * cache as well if and only if we are supposed to. */
3145c87c606SMark Murray 			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
31574664626SKris Kennaway 				/* The following should not return 1, otherwise,
31674664626SKris Kennaway 				 * things are very strange */
31774664626SKris Kennaway 				SSL_CTX_add_session(s->ctx,ret);
31874664626SKris Kennaway 			}
31974664626SKris Kennaway 		if (ret == NULL)
32074664626SKris Kennaway 			goto err;
32174664626SKris Kennaway 		}
32274664626SKris Kennaway 
32374664626SKris Kennaway 	/* Now ret is non-NULL, and we own one of its reference counts. */
32474664626SKris Kennaway 
32574664626SKris Kennaway 	if((s->verify_mode&SSL_VERIFY_PEER)
32674664626SKris Kennaway 	   && (!s->sid_ctx_length || ret->sid_ctx_length != s->sid_ctx_length
32774664626SKris Kennaway 	       || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length)))
32874664626SKris Kennaway 	    {
32974664626SKris Kennaway 		/* We've found the session named by the client, but we don't
33074664626SKris Kennaway 		 * want to use it in this context. */
33174664626SKris Kennaway 
33274664626SKris Kennaway 		if (s->sid_ctx_length == 0)
33374664626SKris Kennaway 			{
33474664626SKris Kennaway 			/* application should have used SSL[_CTX]_set_session_id_context
33574664626SKris Kennaway 			 * -- we could tolerate this and just pretend we never heard
33674664626SKris Kennaway 			 * of this session, but then applications could effectively
33774664626SKris Kennaway 			 * disable the session cache by accident without anyone noticing */
33874664626SKris Kennaway 
33974664626SKris Kennaway 			SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
34074664626SKris Kennaway 			fatal = 1;
34174664626SKris Kennaway 			goto err;
34274664626SKris Kennaway 			}
34374664626SKris Kennaway 		else
34474664626SKris Kennaway 			{
34574664626SKris Kennaway #if 0 /* The client cannot always know when a session is not appropriate,
34674664626SKris Kennaway 	   * so we shouldn't generate an error message. */
34774664626SKris Kennaway 
34874664626SKris Kennaway 			SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
34974664626SKris Kennaway #endif
35074664626SKris Kennaway 			goto err; /* treat like cache miss */
35174664626SKris Kennaway 			}
35274664626SKris Kennaway 		}
35374664626SKris Kennaway 
35474664626SKris Kennaway 	if (ret->cipher == NULL)
35574664626SKris Kennaway 		{
35674664626SKris Kennaway 		unsigned char buf[5],*p;
35774664626SKris Kennaway 		unsigned long l;
35874664626SKris Kennaway 
35974664626SKris Kennaway 		p=buf;
36074664626SKris Kennaway 		l=ret->cipher_id;
36174664626SKris Kennaway 		l2n(l,p);
36274664626SKris Kennaway 		if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
36374664626SKris Kennaway 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
36474664626SKris Kennaway 		else
36574664626SKris Kennaway 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
36674664626SKris Kennaway 		if (ret->cipher == NULL)
36774664626SKris Kennaway 			goto err;
36874664626SKris Kennaway 		}
36974664626SKris Kennaway 
37074664626SKris Kennaway 
37174664626SKris Kennaway #if 0 /* This is way too late. */
37274664626SKris Kennaway 
37374664626SKris Kennaway 	/* If a thread got the session, then 'swaped', and another got
374ddd58736SKris Kennaway 	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
37574664626SKris Kennaway 	 * be in trouble.  So I'll increment it now, then double decrement
37674664626SKris Kennaway 	 * later - am I speaking rubbish?. */
37774664626SKris Kennaway 	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
37874664626SKris Kennaway #endif
37974664626SKris Kennaway 
38074664626SKris Kennaway 	if ((long)(ret->time+ret->timeout) < (long)time(NULL)) /* timeout */
38174664626SKris Kennaway 		{
38274664626SKris Kennaway 		s->ctx->stats.sess_timeout++;
38374664626SKris Kennaway 		/* remove it from the cache */
38474664626SKris Kennaway 		SSL_CTX_remove_session(s->ctx,ret);
38574664626SKris Kennaway 		goto err;
38674664626SKris Kennaway 		}
38774664626SKris Kennaway 
38874664626SKris Kennaway 	s->ctx->stats.sess_hit++;
38974664626SKris Kennaway 
39074664626SKris Kennaway 	/* ret->time=time(NULL); */ /* rezero timeout? */
39174664626SKris Kennaway 	/* again, just leave the session
39274664626SKris Kennaway 	 * if it is the same session, we have just incremented and
39374664626SKris Kennaway 	 * then decremented the reference count :-) */
39474664626SKris Kennaway 	if (s->session != NULL)
39574664626SKris Kennaway 		SSL_SESSION_free(s->session);
39674664626SKris Kennaway 	s->session=ret;
397f579bf8eSKris Kennaway 	s->verify_result = s->session->verify_result;
39874664626SKris Kennaway 	return(1);
39974664626SKris Kennaway 
40074664626SKris Kennaway  err:
40174664626SKris Kennaway 	if (ret != NULL)
40274664626SKris Kennaway 		SSL_SESSION_free(ret);
40374664626SKris Kennaway 	if (fatal)
40474664626SKris Kennaway 		return -1;
40574664626SKris Kennaway 	else
40674664626SKris Kennaway 		return 0;
40774664626SKris Kennaway 	}
40874664626SKris Kennaway 
40974664626SKris Kennaway int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
41074664626SKris Kennaway 	{
41174664626SKris Kennaway 	int ret=0;
41274664626SKris Kennaway 	SSL_SESSION *s;
41374664626SKris Kennaway 
414f579bf8eSKris Kennaway 	/* add just 1 reference count for the SSL_CTX's session cache
415f579bf8eSKris Kennaway 	 * even though it has two ways of access: each session is in a
416f579bf8eSKris Kennaway 	 * doubly linked list and an lhash */
41774664626SKris Kennaway 	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
418f579bf8eSKris Kennaway 	/* if session c is in already in cache, we take back the increment later */
41974664626SKris Kennaway 
42074664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
421f579bf8eSKris Kennaway 	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
42274664626SKris Kennaway 
423f579bf8eSKris Kennaway 	/* s != NULL iff we already had a session with the given PID.
424f579bf8eSKris Kennaway 	 * In this case, s == c should hold (then we did not really modify
425f579bf8eSKris Kennaway 	 * ctx->sessions), or we're in trouble. */
426f579bf8eSKris Kennaway 	if (s != NULL && s != c)
427f579bf8eSKris Kennaway 		{
428f579bf8eSKris Kennaway 		/* We *are* in trouble ... */
429f579bf8eSKris Kennaway 		SSL_SESSION_list_remove(ctx,s);
430f579bf8eSKris Kennaway 		SSL_SESSION_free(s);
431f579bf8eSKris Kennaway 		/* ... so pretend the other session did not exist in cache
432f579bf8eSKris Kennaway 		 * (we cannot handle two SSL_SESSION structures with identical
433f579bf8eSKris Kennaway 		 * session ID in the same cache, which could happen e.g. when
434f579bf8eSKris Kennaway 		 * two threads concurrently obtain the same session from an external
435f579bf8eSKris Kennaway 		 * cache) */
436f579bf8eSKris Kennaway 		s = NULL;
437f579bf8eSKris Kennaway 		}
438f579bf8eSKris Kennaway 
439f579bf8eSKris Kennaway  	/* Put at the head of the queue unless it is already in the cache */
44074664626SKris Kennaway 	if (s == NULL)
44174664626SKris Kennaway 		SSL_SESSION_list_add(ctx,c);
44274664626SKris Kennaway 
44374664626SKris Kennaway 	if (s != NULL)
44474664626SKris Kennaway 		{
445f579bf8eSKris Kennaway 		/* existing cache entry -- decrement previously incremented reference
446f579bf8eSKris Kennaway 		 * count because it already takes into account the cache */
447f579bf8eSKris Kennaway 
448f579bf8eSKris Kennaway 		SSL_SESSION_free(s); /* s == c */
44974664626SKris Kennaway 		ret=0;
45074664626SKris Kennaway 		}
45174664626SKris Kennaway 	else
45274664626SKris Kennaway 		{
453f579bf8eSKris Kennaway 		/* new cache entry -- remove old ones if cache has become too large */
454f579bf8eSKris Kennaway 
45574664626SKris Kennaway 		ret=1;
45674664626SKris Kennaway 
45774664626SKris Kennaway 		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
45874664626SKris Kennaway 			{
45974664626SKris Kennaway 			while (SSL_CTX_sess_number(ctx) >
46074664626SKris Kennaway 				SSL_CTX_sess_get_cache_size(ctx))
46174664626SKris Kennaway 				{
46274664626SKris Kennaway 				if (!remove_session_lock(ctx,
46374664626SKris Kennaway 					ctx->session_cache_tail, 0))
46474664626SKris Kennaway 					break;
46574664626SKris Kennaway 				else
46674664626SKris Kennaway 					ctx->stats.sess_cache_full++;
46774664626SKris Kennaway 				}
46874664626SKris Kennaway 			}
46974664626SKris Kennaway 		}
47074664626SKris Kennaway 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
47174664626SKris Kennaway 	return(ret);
47274664626SKris Kennaway 	}
47374664626SKris Kennaway 
47474664626SKris Kennaway int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
47574664626SKris Kennaway {
47674664626SKris Kennaway 	return remove_session_lock(ctx, c, 1);
47774664626SKris Kennaway }
47874664626SKris Kennaway 
47974664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
48074664626SKris Kennaway 	{
48174664626SKris Kennaway 	SSL_SESSION *r;
48274664626SKris Kennaway 	int ret=0;
48374664626SKris Kennaway 
48474664626SKris Kennaway 	if ((c != NULL) && (c->session_id_length != 0))
48574664626SKris Kennaway 		{
48674664626SKris Kennaway 		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
487c1803d78SJacques Vidrine 		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
48874664626SKris Kennaway 			{
48974664626SKris Kennaway 			ret=1;
490c1803d78SJacques Vidrine 			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
49174664626SKris Kennaway 			SSL_SESSION_list_remove(ctx,c);
49274664626SKris Kennaway 			}
49374664626SKris Kennaway 
49474664626SKris Kennaway 		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
49574664626SKris Kennaway 
49674664626SKris Kennaway 		if (ret)
49774664626SKris Kennaway 			{
49874664626SKris Kennaway 			r->not_resumable=1;
49974664626SKris Kennaway 			if (ctx->remove_session_cb != NULL)
50074664626SKris Kennaway 				ctx->remove_session_cb(ctx,r);
50174664626SKris Kennaway 			SSL_SESSION_free(r);
50274664626SKris Kennaway 			}
50374664626SKris Kennaway 		}
50474664626SKris Kennaway 	else
50574664626SKris Kennaway 		ret=0;
50674664626SKris Kennaway 	return(ret);
50774664626SKris Kennaway 	}
50874664626SKris Kennaway 
50974664626SKris Kennaway void SSL_SESSION_free(SSL_SESSION *ss)
51074664626SKris Kennaway 	{
51174664626SKris Kennaway 	int i;
51274664626SKris Kennaway 
51374664626SKris Kennaway 	if(ss == NULL)
51474664626SKris Kennaway 	    return;
51574664626SKris Kennaway 
51674664626SKris Kennaway 	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
51774664626SKris Kennaway #ifdef REF_PRINT
51874664626SKris Kennaway 	REF_PRINT("SSL_SESSION",ss);
51974664626SKris Kennaway #endif
52074664626SKris Kennaway 	if (i > 0) return;
52174664626SKris Kennaway #ifdef REF_CHECK
52274664626SKris Kennaway 	if (i < 0)
52374664626SKris Kennaway 		{
52474664626SKris Kennaway 		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
52574664626SKris Kennaway 		abort(); /* ok */
52674664626SKris Kennaway 		}
52774664626SKris Kennaway #endif
52874664626SKris Kennaway 
5295c87c606SMark Murray 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
53074664626SKris Kennaway 
5315c87c606SMark Murray 	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
5325c87c606SMark Murray 	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
5335c87c606SMark Murray 	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
53474664626SKris Kennaway 	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
53574664626SKris Kennaway 	if (ss->peer != NULL) X509_free(ss->peer);
53674664626SKris Kennaway 	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
5375c87c606SMark Murray 	OPENSSL_cleanse(ss,sizeof(*ss));
538ddd58736SKris Kennaway 	OPENSSL_free(ss);
53974664626SKris Kennaway 	}
54074664626SKris Kennaway 
54174664626SKris Kennaway int SSL_set_session(SSL *s, SSL_SESSION *session)
54274664626SKris Kennaway 	{
54374664626SKris Kennaway 	int ret=0;
54474664626SKris Kennaway 	SSL_METHOD *meth;
54574664626SKris Kennaway 
54674664626SKris Kennaway 	if (session != NULL)
54774664626SKris Kennaway 		{
54874664626SKris Kennaway 		meth=s->ctx->method->get_ssl_method(session->ssl_version);
54974664626SKris Kennaway 		if (meth == NULL)
55074664626SKris Kennaway 			meth=s->method->get_ssl_method(session->ssl_version);
55174664626SKris Kennaway 		if (meth == NULL)
55274664626SKris Kennaway 			{
55374664626SKris Kennaway 			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
55474664626SKris Kennaway 			return(0);
55574664626SKris Kennaway 			}
55674664626SKris Kennaway 
55774664626SKris Kennaway 		if (meth != s->method)
55874664626SKris Kennaway 			{
55974664626SKris Kennaway 			if (!SSL_set_ssl_method(s,meth))
56074664626SKris Kennaway 				return(0);
56174664626SKris Kennaway 			if (s->ctx->session_timeout == 0)
56274664626SKris Kennaway 				session->timeout=SSL_get_default_timeout(s);
56374664626SKris Kennaway 			else
56474664626SKris Kennaway 				session->timeout=s->ctx->session_timeout;
56574664626SKris Kennaway 			}
56674664626SKris Kennaway 
5675c87c606SMark Murray #ifndef OPENSSL_NO_KRB5
5685c87c606SMark Murray                 if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
5695c87c606SMark Murray                     session->krb5_client_princ_len > 0)
5705c87c606SMark Murray                 {
5715c87c606SMark Murray                     s->kssl_ctx->client_princ = (char *)malloc(session->krb5_client_princ_len + 1);
5725c87c606SMark Murray                     memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
5735c87c606SMark Murray                             session->krb5_client_princ_len);
5745c87c606SMark Murray                     s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
5755c87c606SMark Murray                 }
5765c87c606SMark Murray #endif /* OPENSSL_NO_KRB5 */
5775c87c606SMark Murray 
57874664626SKris Kennaway 		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
57974664626SKris Kennaway 		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
58074664626SKris Kennaway 		if (s->session != NULL)
58174664626SKris Kennaway 			SSL_SESSION_free(s->session);
58274664626SKris Kennaway 		s->session=session;
583de7cdddaSKris Kennaway 		s->verify_result = s->session->verify_result;
58474664626SKris Kennaway 		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
58574664626SKris Kennaway 		ret=1;
58674664626SKris Kennaway 		}
58774664626SKris Kennaway 	else
58874664626SKris Kennaway 		{
58974664626SKris Kennaway 		if (s->session != NULL)
59074664626SKris Kennaway 			{
59174664626SKris Kennaway 			SSL_SESSION_free(s->session);
59274664626SKris Kennaway 			s->session=NULL;
59374664626SKris Kennaway 			}
59474664626SKris Kennaway 
59574664626SKris Kennaway 		meth=s->ctx->method;
59674664626SKris Kennaway 		if (meth != s->method)
59774664626SKris Kennaway 			{
59874664626SKris Kennaway 			if (!SSL_set_ssl_method(s,meth))
59974664626SKris Kennaway 				return(0);
60074664626SKris Kennaway 			}
60174664626SKris Kennaway 		ret=1;
60274664626SKris Kennaway 		}
60374664626SKris Kennaway 	return(ret);
60474664626SKris Kennaway 	}
60574664626SKris Kennaway 
60674664626SKris Kennaway long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
60774664626SKris Kennaway 	{
60874664626SKris Kennaway 	if (s == NULL) return(0);
60974664626SKris Kennaway 	s->timeout=t;
61074664626SKris Kennaway 	return(1);
61174664626SKris Kennaway 	}
61274664626SKris Kennaway 
61374664626SKris Kennaway long SSL_SESSION_get_timeout(SSL_SESSION *s)
61474664626SKris Kennaway 	{
61574664626SKris Kennaway 	if (s == NULL) return(0);
61674664626SKris Kennaway 	return(s->timeout);
61774664626SKris Kennaway 	}
61874664626SKris Kennaway 
61974664626SKris Kennaway long SSL_SESSION_get_time(SSL_SESSION *s)
62074664626SKris Kennaway 	{
62174664626SKris Kennaway 	if (s == NULL) return(0);
62274664626SKris Kennaway 	return(s->time);
62374664626SKris Kennaway 	}
62474664626SKris Kennaway 
62574664626SKris Kennaway long SSL_SESSION_set_time(SSL_SESSION *s, long t)
62674664626SKris Kennaway 	{
62774664626SKris Kennaway 	if (s == NULL) return(0);
62874664626SKris Kennaway 	s->time=t;
62974664626SKris Kennaway 	return(t);
63074664626SKris Kennaway 	}
63174664626SKris Kennaway 
63274664626SKris Kennaway long SSL_CTX_set_timeout(SSL_CTX *s, long t)
63374664626SKris Kennaway 	{
63474664626SKris Kennaway 	long l;
63574664626SKris Kennaway 	if (s == NULL) return(0);
63674664626SKris Kennaway 	l=s->session_timeout;
63774664626SKris Kennaway 	s->session_timeout=t;
63874664626SKris Kennaway 	return(l);
63974664626SKris Kennaway 	}
64074664626SKris Kennaway 
64174664626SKris Kennaway long SSL_CTX_get_timeout(SSL_CTX *s)
64274664626SKris Kennaway 	{
64374664626SKris Kennaway 	if (s == NULL) return(0);
64474664626SKris Kennaway 	return(s->session_timeout);
64574664626SKris Kennaway 	}
64674664626SKris Kennaway 
64774664626SKris Kennaway typedef struct timeout_param_st
64874664626SKris Kennaway 	{
64974664626SKris Kennaway 	SSL_CTX *ctx;
65074664626SKris Kennaway 	long time;
65174664626SKris Kennaway 	LHASH *cache;
65274664626SKris Kennaway 	} TIMEOUT_PARAM;
65374664626SKris Kennaway 
65474664626SKris Kennaway static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
65574664626SKris Kennaway 	{
65674664626SKris Kennaway 	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
65774664626SKris Kennaway 		{
65874664626SKris Kennaway 		/* The reason we don't call SSL_CTX_remove_session() is to
65974664626SKris Kennaway 		 * save on locking overhead */
660f579bf8eSKris Kennaway 		lh_delete(p->cache,s);
66174664626SKris Kennaway 		SSL_SESSION_list_remove(p->ctx,s);
66274664626SKris Kennaway 		s->not_resumable=1;
66374664626SKris Kennaway 		if (p->ctx->remove_session_cb != NULL)
66474664626SKris Kennaway 			p->ctx->remove_session_cb(p->ctx,s);
66574664626SKris Kennaway 		SSL_SESSION_free(s);
66674664626SKris Kennaway 		}
66774664626SKris Kennaway 	}
66874664626SKris Kennaway 
6695c87c606SMark Murray static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
6705c87c606SMark Murray 
67174664626SKris Kennaway void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
67274664626SKris Kennaway 	{
67374664626SKris Kennaway 	unsigned long i;
67474664626SKris Kennaway 	TIMEOUT_PARAM tp;
67574664626SKris Kennaway 
67674664626SKris Kennaway 	tp.ctx=s;
67774664626SKris Kennaway 	tp.cache=s->sessions;
67874664626SKris Kennaway 	if (tp.cache == NULL) return;
67974664626SKris Kennaway 	tp.time=t;
68074664626SKris Kennaway 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
68174664626SKris Kennaway 	i=tp.cache->down_load;
68274664626SKris Kennaway 	tp.cache->down_load=0;
6835c87c606SMark Murray 	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
68474664626SKris Kennaway 	tp.cache->down_load=i;
68574664626SKris Kennaway 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
68674664626SKris Kennaway 	}
68774664626SKris Kennaway 
68874664626SKris Kennaway int ssl_clear_bad_session(SSL *s)
68974664626SKris Kennaway 	{
69074664626SKris Kennaway 	if (	(s->session != NULL) &&
69174664626SKris Kennaway 		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
69274664626SKris Kennaway 		!(SSL_in_init(s) || SSL_in_before(s)))
69374664626SKris Kennaway 		{
69474664626SKris Kennaway 		SSL_CTX_remove_session(s->ctx,s->session);
69574664626SKris Kennaway 		return(1);
69674664626SKris Kennaway 		}
69774664626SKris Kennaway 	else
69874664626SKris Kennaway 		return(0);
69974664626SKris Kennaway 	}
70074664626SKris Kennaway 
70174664626SKris Kennaway /* locked by SSL_CTX in the calling function */
70274664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
70374664626SKris Kennaway 	{
70474664626SKris Kennaway 	if ((s->next == NULL) || (s->prev == NULL)) return;
70574664626SKris Kennaway 
70674664626SKris Kennaway 	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
70774664626SKris Kennaway 		{ /* last element in list */
70874664626SKris Kennaway 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
70974664626SKris Kennaway 			{ /* only one element in list */
71074664626SKris Kennaway 			ctx->session_cache_head=NULL;
71174664626SKris Kennaway 			ctx->session_cache_tail=NULL;
71274664626SKris Kennaway 			}
71374664626SKris Kennaway 		else
71474664626SKris Kennaway 			{
71574664626SKris Kennaway 			ctx->session_cache_tail=s->prev;
71674664626SKris Kennaway 			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
71774664626SKris Kennaway 			}
71874664626SKris Kennaway 		}
71974664626SKris Kennaway 	else
72074664626SKris Kennaway 		{
72174664626SKris Kennaway 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
72274664626SKris Kennaway 			{ /* first element in list */
72374664626SKris Kennaway 			ctx->session_cache_head=s->next;
72474664626SKris Kennaway 			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
72574664626SKris Kennaway 			}
72674664626SKris Kennaway 		else
72774664626SKris Kennaway 			{ /* middle of list */
72874664626SKris Kennaway 			s->next->prev=s->prev;
72974664626SKris Kennaway 			s->prev->next=s->next;
73074664626SKris Kennaway 			}
73174664626SKris Kennaway 		}
73274664626SKris Kennaway 	s->prev=s->next=NULL;
73374664626SKris Kennaway 	}
73474664626SKris Kennaway 
73574664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
73674664626SKris Kennaway 	{
73774664626SKris Kennaway 	if ((s->next != NULL) && (s->prev != NULL))
73874664626SKris Kennaway 		SSL_SESSION_list_remove(ctx,s);
73974664626SKris Kennaway 
74074664626SKris Kennaway 	if (ctx->session_cache_head == NULL)
74174664626SKris Kennaway 		{
74274664626SKris Kennaway 		ctx->session_cache_head=s;
74374664626SKris Kennaway 		ctx->session_cache_tail=s;
74474664626SKris Kennaway 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
74574664626SKris Kennaway 		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
74674664626SKris Kennaway 		}
74774664626SKris Kennaway 	else
74874664626SKris Kennaway 		{
74974664626SKris Kennaway 		s->next=ctx->session_cache_head;
75074664626SKris Kennaway 		s->next->prev=s;
75174664626SKris Kennaway 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
75274664626SKris Kennaway 		ctx->session_cache_head=s;
75374664626SKris Kennaway 		}
75474664626SKris Kennaway 	}
75574664626SKris Kennaway 
756