xref: /onnv-gate/usr/src/common/openssl/ssl/ssl_sess.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* ssl/ssl_sess.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <openssl/lhash.h>
610Sstevel@tonic-gate #include <openssl/rand.h>
620Sstevel@tonic-gate #include "ssl_locl.h"
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
650Sstevel@tonic-gate static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
660Sstevel@tonic-gate static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
670Sstevel@tonic-gate 
SSL_get_session(const SSL * ssl)68*2139Sjp161948 SSL_SESSION *SSL_get_session(const SSL *ssl)
690Sstevel@tonic-gate /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
700Sstevel@tonic-gate 	{
710Sstevel@tonic-gate 	return(ssl->session);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
SSL_get1_session(SSL * ssl)740Sstevel@tonic-gate SSL_SESSION *SSL_get1_session(SSL *ssl)
750Sstevel@tonic-gate /* variant of SSL_get_session: caller really gets something */
760Sstevel@tonic-gate 	{
770Sstevel@tonic-gate 	SSL_SESSION *sess;
780Sstevel@tonic-gate 	/* Need to lock this all up rather than just use CRYPTO_add so that
790Sstevel@tonic-gate 	 * somebody doesn't free ssl->session between when we check it's
800Sstevel@tonic-gate 	 * non-null and when we up the reference count. */
810Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
820Sstevel@tonic-gate 	sess = ssl->session;
830Sstevel@tonic-gate 	if(sess)
840Sstevel@tonic-gate 		sess->references++;
850Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
860Sstevel@tonic-gate 	return(sess);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
SSL_SESSION_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)890Sstevel@tonic-gate int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
900Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
910Sstevel@tonic-gate 	{
920Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
930Sstevel@tonic-gate 			new_func, dup_func, free_func);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
SSL_SESSION_set_ex_data(SSL_SESSION * s,int idx,void * arg)960Sstevel@tonic-gate int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
970Sstevel@tonic-gate 	{
980Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
SSL_SESSION_get_ex_data(const SSL_SESSION * s,int idx)101*2139Sjp161948 void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
1020Sstevel@tonic-gate 	{
1030Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
SSL_SESSION_new(void)1060Sstevel@tonic-gate SSL_SESSION *SSL_SESSION_new(void)
1070Sstevel@tonic-gate 	{
1080Sstevel@tonic-gate 	SSL_SESSION *ss;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
1110Sstevel@tonic-gate 	if (ss == NULL)
1120Sstevel@tonic-gate 		{
1130Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
1140Sstevel@tonic-gate 		return(0);
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 	memset(ss,0,sizeof(SSL_SESSION));
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
1190Sstevel@tonic-gate 	ss->references=1;
1200Sstevel@tonic-gate 	ss->timeout=60*5+4; /* 5 minute timeout by default */
1210Sstevel@tonic-gate 	ss->time=time(NULL);
1220Sstevel@tonic-gate 	ss->prev=NULL;
1230Sstevel@tonic-gate 	ss->next=NULL;
1240Sstevel@tonic-gate 	ss->compress_meth=0;
1250Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
1260Sstevel@tonic-gate 	return(ss);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
SSL_SESSION_get_id(const SSL_SESSION * s,unsigned int * len)129*2139Sjp161948 const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
130*2139Sjp161948 	{
131*2139Sjp161948 	if(len)
132*2139Sjp161948 		*len = s->session_id_length;
133*2139Sjp161948 	return s->session_id;
134*2139Sjp161948 	}
135*2139Sjp161948 
1360Sstevel@tonic-gate /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
1370Sstevel@tonic-gate  * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
1380Sstevel@tonic-gate  * until we have no conflict is going to complete in one iteration pretty much
1390Sstevel@tonic-gate  * "most" of the time (btw: understatement). So, if it takes us 10 iterations
1400Sstevel@tonic-gate  * and we still can't avoid a conflict - well that's a reasonable point to call
1410Sstevel@tonic-gate  * it quits. Either the RAND code is broken or someone is trying to open roughly
1420Sstevel@tonic-gate  * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
1430Sstevel@tonic-gate  * store that many sessions is perhaps a more interesting question ... */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #define MAX_SESS_ID_ATTEMPTS 10
def_generate_session_id(const SSL * ssl,unsigned char * id,unsigned int * id_len)1460Sstevel@tonic-gate static int def_generate_session_id(const SSL *ssl, unsigned char *id,
1470Sstevel@tonic-gate 				unsigned int *id_len)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	unsigned int retry = 0;
1500Sstevel@tonic-gate 	do
151*2139Sjp161948 		if (RAND_pseudo_bytes(id, *id_len) <= 0)
152*2139Sjp161948 			return 0;
1530Sstevel@tonic-gate 	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
1540Sstevel@tonic-gate 		(++retry < MAX_SESS_ID_ATTEMPTS));
1550Sstevel@tonic-gate 	if(retry < MAX_SESS_ID_ATTEMPTS)
1560Sstevel@tonic-gate 		return 1;
1570Sstevel@tonic-gate 	/* else - woops a session_id match */
1580Sstevel@tonic-gate 	/* XXX We should also check the external cache --
1590Sstevel@tonic-gate 	 * but the probability of a collision is negligible, and
1600Sstevel@tonic-gate 	 * we could not prevent the concurrent creation of sessions
1610Sstevel@tonic-gate 	 * with identical IDs since we currently don't have means
1620Sstevel@tonic-gate 	 * to atomically check whether a session ID already exists
1630Sstevel@tonic-gate 	 * and make a reservation for it if it does not
1640Sstevel@tonic-gate 	 * (this problem applies to the internal cache as well).
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	return 0;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
ssl_get_new_session(SSL * s,int session)1690Sstevel@tonic-gate int ssl_get_new_session(SSL *s, int session)
1700Sstevel@tonic-gate 	{
1710Sstevel@tonic-gate 	/* This gets used by clients and servers. */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	unsigned int tmp;
1740Sstevel@tonic-gate 	SSL_SESSION *ss=NULL;
1750Sstevel@tonic-gate 	GEN_SESSION_CB cb = def_generate_session_id;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((ss=SSL_SESSION_new()) == NULL) return(0);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* If the context has a default timeout, use it */
1800Sstevel@tonic-gate 	if (s->ctx->session_timeout == 0)
1810Sstevel@tonic-gate 		ss->timeout=SSL_get_default_timeout(s);
1820Sstevel@tonic-gate 	else
1830Sstevel@tonic-gate 		ss->timeout=s->ctx->session_timeout;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (s->session != NULL)
1860Sstevel@tonic-gate 		{
1870Sstevel@tonic-gate 		SSL_SESSION_free(s->session);
1880Sstevel@tonic-gate 		s->session=NULL;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (session)
1920Sstevel@tonic-gate 		{
1930Sstevel@tonic-gate 		if (s->version == SSL2_VERSION)
1940Sstevel@tonic-gate 			{
1950Sstevel@tonic-gate 			ss->ssl_version=SSL2_VERSION;
1960Sstevel@tonic-gate 			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 		else if (s->version == SSL3_VERSION)
1990Sstevel@tonic-gate 			{
2000Sstevel@tonic-gate 			ss->ssl_version=SSL3_VERSION;
2010Sstevel@tonic-gate 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 		else if (s->version == TLS1_VERSION)
2040Sstevel@tonic-gate 			{
2050Sstevel@tonic-gate 			ss->ssl_version=TLS1_VERSION;
2060Sstevel@tonic-gate 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
2070Sstevel@tonic-gate 			}
208*2139Sjp161948 		else if (s->version == DTLS1_VERSION)
209*2139Sjp161948 			{
210*2139Sjp161948 			ss->ssl_version=DTLS1_VERSION;
211*2139Sjp161948 			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
212*2139Sjp161948 			}
2130Sstevel@tonic-gate 		else
2140Sstevel@tonic-gate 			{
2150Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
2160Sstevel@tonic-gate 			SSL_SESSION_free(ss);
2170Sstevel@tonic-gate 			return(0);
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 		/* Choose which callback will set the session ID */
2200Sstevel@tonic-gate 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
2210Sstevel@tonic-gate 		if(s->generate_session_id)
2220Sstevel@tonic-gate 			cb = s->generate_session_id;
2230Sstevel@tonic-gate 		else if(s->ctx->generate_session_id)
2240Sstevel@tonic-gate 			cb = s->ctx->generate_session_id;
2250Sstevel@tonic-gate 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
2260Sstevel@tonic-gate 		/* Choose a session ID */
2270Sstevel@tonic-gate 		tmp = ss->session_id_length;
2280Sstevel@tonic-gate 		if(!cb(s, ss->session_id, &tmp))
2290Sstevel@tonic-gate 			{
2300Sstevel@tonic-gate 			/* The callback failed */
2310Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2320Sstevel@tonic-gate 				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
2330Sstevel@tonic-gate 			SSL_SESSION_free(ss);
2340Sstevel@tonic-gate 			return(0);
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate 		/* Don't allow the callback to set the session length to zero.
2370Sstevel@tonic-gate 		 * nor set it higher than it was. */
2380Sstevel@tonic-gate 		if(!tmp || (tmp > ss->session_id_length))
2390Sstevel@tonic-gate 			{
2400Sstevel@tonic-gate 			/* The callback set an illegal length */
2410Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2420Sstevel@tonic-gate 				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
2430Sstevel@tonic-gate 			SSL_SESSION_free(ss);
2440Sstevel@tonic-gate 			return(0);
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 		/* If the session length was shrunk and we're SSLv2, pad it */
2470Sstevel@tonic-gate 		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
2480Sstevel@tonic-gate 			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
2490Sstevel@tonic-gate 		else
2500Sstevel@tonic-gate 			ss->session_id_length = tmp;
2510Sstevel@tonic-gate 		/* Finally, check for a conflict */
2520Sstevel@tonic-gate 		if(SSL_has_matching_session_id(s, ss->session_id,
2530Sstevel@tonic-gate 						ss->session_id_length))
2540Sstevel@tonic-gate 			{
2550Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
2560Sstevel@tonic-gate 				SSL_R_SSL_SESSION_ID_CONFLICT);
2570Sstevel@tonic-gate 			SSL_SESSION_free(ss);
2580Sstevel@tonic-gate 			return(0);
2590Sstevel@tonic-gate 			}
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	else
2620Sstevel@tonic-gate 		{
2630Sstevel@tonic-gate 		ss->session_id_length=0;
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (s->sid_ctx_length > sizeof ss->sid_ctx)
2670Sstevel@tonic-gate 		{
2680Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
2690Sstevel@tonic-gate 		SSL_SESSION_free(ss);
2700Sstevel@tonic-gate 		return 0;
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
2730Sstevel@tonic-gate 	ss->sid_ctx_length=s->sid_ctx_length;
2740Sstevel@tonic-gate 	s->session=ss;
2750Sstevel@tonic-gate 	ss->ssl_version=s->version;
2760Sstevel@tonic-gate 	ss->verify_result = X509_V_OK;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	return(1);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
ssl_get_prev_session(SSL * s,unsigned char * session_id,int len)2810Sstevel@tonic-gate int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len)
2820Sstevel@tonic-gate 	{
2830Sstevel@tonic-gate 	/* This is used only by servers. */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	SSL_SESSION *ret=NULL,data;
2860Sstevel@tonic-gate 	int fatal = 0;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	data.ssl_version=s->version;
2890Sstevel@tonic-gate 	data.session_id_length=len;
2900Sstevel@tonic-gate 	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
2910Sstevel@tonic-gate 		goto err;
2920Sstevel@tonic-gate 	memcpy(data.session_id,session_id,len);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
2950Sstevel@tonic-gate 		{
2960Sstevel@tonic-gate 		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
2970Sstevel@tonic-gate 		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
2980Sstevel@tonic-gate 		if (ret != NULL)
2990Sstevel@tonic-gate 		    /* don't allow other threads to steal it: */
3000Sstevel@tonic-gate 		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
3010Sstevel@tonic-gate 		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (ret == NULL)
3050Sstevel@tonic-gate 		{
3060Sstevel@tonic-gate 		int copy=1;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		s->ctx->stats.sess_miss++;
3090Sstevel@tonic-gate 		ret=NULL;
3100Sstevel@tonic-gate 		if (s->ctx->get_session_cb != NULL
3110Sstevel@tonic-gate 		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
3120Sstevel@tonic-gate 		       != NULL)
3130Sstevel@tonic-gate 			{
3140Sstevel@tonic-gate 			s->ctx->stats.sess_cb_hit++;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 			/* Increment reference count now if the session callback
3170Sstevel@tonic-gate 			 * asks us to do so (note that if the session structures
3180Sstevel@tonic-gate 			 * returned by the callback are shared between threads,
3190Sstevel@tonic-gate 			 * it must handle the reference count itself [i.e. copy == 0],
3200Sstevel@tonic-gate 			 * or things won't be thread-safe). */
3210Sstevel@tonic-gate 			if (copy)
3220Sstevel@tonic-gate 				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 			/* Add the externally cached session to the internal
3250Sstevel@tonic-gate 			 * cache as well if and only if we are supposed to. */
3260Sstevel@tonic-gate 			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
3270Sstevel@tonic-gate 				/* The following should not return 1, otherwise,
3280Sstevel@tonic-gate 				 * things are very strange */
3290Sstevel@tonic-gate 				SSL_CTX_add_session(s->ctx,ret);
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 		if (ret == NULL)
3320Sstevel@tonic-gate 			goto err;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* Now ret is non-NULL, and we own one of its reference counts. */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if((s->verify_mode&SSL_VERIFY_PEER)
3380Sstevel@tonic-gate 	   && (!s->sid_ctx_length || ret->sid_ctx_length != s->sid_ctx_length
3390Sstevel@tonic-gate 	       || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length)))
3400Sstevel@tonic-gate 	    {
3410Sstevel@tonic-gate 		/* We've found the session named by the client, but we don't
3420Sstevel@tonic-gate 		 * want to use it in this context. */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		if (s->sid_ctx_length == 0)
3450Sstevel@tonic-gate 			{
3460Sstevel@tonic-gate 			/* application should have used SSL[_CTX]_set_session_id_context
3470Sstevel@tonic-gate 			 * -- we could tolerate this and just pretend we never heard
3480Sstevel@tonic-gate 			 * of this session, but then applications could effectively
3490Sstevel@tonic-gate 			 * disable the session cache by accident without anyone noticing */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
3520Sstevel@tonic-gate 			fatal = 1;
3530Sstevel@tonic-gate 			goto err;
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 		else
3560Sstevel@tonic-gate 			{
3570Sstevel@tonic-gate #if 0 /* The client cannot always know when a session is not appropriate,
3580Sstevel@tonic-gate 	   * so we shouldn't generate an error message. */
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
3610Sstevel@tonic-gate #endif
3620Sstevel@tonic-gate 			goto err; /* treat like cache miss */
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (ret->cipher == NULL)
3670Sstevel@tonic-gate 		{
3680Sstevel@tonic-gate 		unsigned char buf[5],*p;
3690Sstevel@tonic-gate 		unsigned long l;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		p=buf;
3720Sstevel@tonic-gate 		l=ret->cipher_id;
3730Sstevel@tonic-gate 		l2n(l,p);
3740Sstevel@tonic-gate 		if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
3750Sstevel@tonic-gate 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
3760Sstevel@tonic-gate 		else
3770Sstevel@tonic-gate 			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
3780Sstevel@tonic-gate 		if (ret->cipher == NULL)
3790Sstevel@tonic-gate 			goto err;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate #if 0 /* This is way too late. */
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/* If a thread got the session, then 'swaped', and another got
3860Sstevel@tonic-gate 	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
3870Sstevel@tonic-gate 	 * be in trouble.  So I'll increment it now, then double decrement
3880Sstevel@tonic-gate 	 * later - am I speaking rubbish?. */
3890Sstevel@tonic-gate 	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
3900Sstevel@tonic-gate #endif
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if ((long)(ret->time+ret->timeout) < (long)time(NULL)) /* timeout */
3930Sstevel@tonic-gate 		{
3940Sstevel@tonic-gate 		s->ctx->stats.sess_timeout++;
3950Sstevel@tonic-gate 		/* remove it from the cache */
3960Sstevel@tonic-gate 		SSL_CTX_remove_session(s->ctx,ret);
3970Sstevel@tonic-gate 		goto err;
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	s->ctx->stats.sess_hit++;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/* ret->time=time(NULL); */ /* rezero timeout? */
4030Sstevel@tonic-gate 	/* again, just leave the session
4040Sstevel@tonic-gate 	 * if it is the same session, we have just incremented and
4050Sstevel@tonic-gate 	 * then decremented the reference count :-) */
4060Sstevel@tonic-gate 	if (s->session != NULL)
4070Sstevel@tonic-gate 		SSL_SESSION_free(s->session);
4080Sstevel@tonic-gate 	s->session=ret;
4090Sstevel@tonic-gate 	s->verify_result = s->session->verify_result;
4100Sstevel@tonic-gate 	return(1);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate  err:
4130Sstevel@tonic-gate 	if (ret != NULL)
4140Sstevel@tonic-gate 		SSL_SESSION_free(ret);
4150Sstevel@tonic-gate 	if (fatal)
4160Sstevel@tonic-gate 		return -1;
4170Sstevel@tonic-gate 	else
4180Sstevel@tonic-gate 		return 0;
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
SSL_CTX_add_session(SSL_CTX * ctx,SSL_SESSION * c)4210Sstevel@tonic-gate int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
4220Sstevel@tonic-gate 	{
4230Sstevel@tonic-gate 	int ret=0;
4240Sstevel@tonic-gate 	SSL_SESSION *s;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/* add just 1 reference count for the SSL_CTX's session cache
4270Sstevel@tonic-gate 	 * even though it has two ways of access: each session is in a
4280Sstevel@tonic-gate 	 * doubly linked list and an lhash */
4290Sstevel@tonic-gate 	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
4300Sstevel@tonic-gate 	/* if session c is in already in cache, we take back the increment later */
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
4330Sstevel@tonic-gate 	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/* s != NULL iff we already had a session with the given PID.
4360Sstevel@tonic-gate 	 * In this case, s == c should hold (then we did not really modify
4370Sstevel@tonic-gate 	 * ctx->sessions), or we're in trouble. */
4380Sstevel@tonic-gate 	if (s != NULL && s != c)
4390Sstevel@tonic-gate 		{
4400Sstevel@tonic-gate 		/* We *are* in trouble ... */
4410Sstevel@tonic-gate 		SSL_SESSION_list_remove(ctx,s);
4420Sstevel@tonic-gate 		SSL_SESSION_free(s);
4430Sstevel@tonic-gate 		/* ... so pretend the other session did not exist in cache
4440Sstevel@tonic-gate 		 * (we cannot handle two SSL_SESSION structures with identical
4450Sstevel@tonic-gate 		 * session ID in the same cache, which could happen e.g. when
4460Sstevel@tonic-gate 		 * two threads concurrently obtain the same session from an external
4470Sstevel@tonic-gate 		 * cache) */
4480Sstevel@tonic-gate 		s = NULL;
4490Sstevel@tonic-gate 		}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate  	/* Put at the head of the queue unless it is already in the cache */
4520Sstevel@tonic-gate 	if (s == NULL)
4530Sstevel@tonic-gate 		SSL_SESSION_list_add(ctx,c);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (s != NULL)
4560Sstevel@tonic-gate 		{
4570Sstevel@tonic-gate 		/* existing cache entry -- decrement previously incremented reference
4580Sstevel@tonic-gate 		 * count because it already takes into account the cache */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		SSL_SESSION_free(s); /* s == c */
4610Sstevel@tonic-gate 		ret=0;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	else
4640Sstevel@tonic-gate 		{
4650Sstevel@tonic-gate 		/* new cache entry -- remove old ones if cache has become too large */
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		ret=1;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
4700Sstevel@tonic-gate 			{
4710Sstevel@tonic-gate 			while (SSL_CTX_sess_number(ctx) >
4720Sstevel@tonic-gate 				SSL_CTX_sess_get_cache_size(ctx))
4730Sstevel@tonic-gate 				{
4740Sstevel@tonic-gate 				if (!remove_session_lock(ctx,
4750Sstevel@tonic-gate 					ctx->session_cache_tail, 0))
4760Sstevel@tonic-gate 					break;
4770Sstevel@tonic-gate 				else
4780Sstevel@tonic-gate 					ctx->stats.sess_cache_full++;
4790Sstevel@tonic-gate 				}
4800Sstevel@tonic-gate 			}
4810Sstevel@tonic-gate 		}
4820Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
4830Sstevel@tonic-gate 	return(ret);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
SSL_CTX_remove_session(SSL_CTX * ctx,SSL_SESSION * c)4860Sstevel@tonic-gate int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	return remove_session_lock(ctx, c, 1);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
remove_session_lock(SSL_CTX * ctx,SSL_SESSION * c,int lck)4910Sstevel@tonic-gate static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
4920Sstevel@tonic-gate 	{
4930Sstevel@tonic-gate 	SSL_SESSION *r;
4940Sstevel@tonic-gate 	int ret=0;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if ((c != NULL) && (c->session_id_length != 0))
4970Sstevel@tonic-gate 		{
4980Sstevel@tonic-gate 		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
4990Sstevel@tonic-gate 		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
5000Sstevel@tonic-gate 			{
5010Sstevel@tonic-gate 			ret=1;
5020Sstevel@tonic-gate 			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
5030Sstevel@tonic-gate 			SSL_SESSION_list_remove(ctx,c);
5040Sstevel@tonic-gate 			}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		if (ret)
5090Sstevel@tonic-gate 			{
5100Sstevel@tonic-gate 			r->not_resumable=1;
5110Sstevel@tonic-gate 			if (ctx->remove_session_cb != NULL)
5120Sstevel@tonic-gate 				ctx->remove_session_cb(ctx,r);
5130Sstevel@tonic-gate 			SSL_SESSION_free(r);
5140Sstevel@tonic-gate 			}
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 	else
5170Sstevel@tonic-gate 		ret=0;
5180Sstevel@tonic-gate 	return(ret);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
SSL_SESSION_free(SSL_SESSION * ss)5210Sstevel@tonic-gate void SSL_SESSION_free(SSL_SESSION *ss)
5220Sstevel@tonic-gate 	{
5230Sstevel@tonic-gate 	int i;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if(ss == NULL)
5260Sstevel@tonic-gate 	    return;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
5290Sstevel@tonic-gate #ifdef REF_PRINT
5300Sstevel@tonic-gate 	REF_PRINT("SSL_SESSION",ss);
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate 	if (i > 0) return;
5330Sstevel@tonic-gate #ifdef REF_CHECK
5340Sstevel@tonic-gate 	if (i < 0)
5350Sstevel@tonic-gate 		{
5360Sstevel@tonic-gate 		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
5370Sstevel@tonic-gate 		abort(); /* ok */
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate #endif
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
5440Sstevel@tonic-gate 	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
5450Sstevel@tonic-gate 	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
5460Sstevel@tonic-gate 	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
5470Sstevel@tonic-gate 	if (ss->peer != NULL) X509_free(ss->peer);
5480Sstevel@tonic-gate 	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
5490Sstevel@tonic-gate 	OPENSSL_cleanse(ss,sizeof(*ss));
5500Sstevel@tonic-gate 	OPENSSL_free(ss);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
SSL_set_session(SSL * s,SSL_SESSION * session)5530Sstevel@tonic-gate int SSL_set_session(SSL *s, SSL_SESSION *session)
5540Sstevel@tonic-gate 	{
5550Sstevel@tonic-gate 	int ret=0;
5560Sstevel@tonic-gate 	SSL_METHOD *meth;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (session != NULL)
5590Sstevel@tonic-gate 		{
5600Sstevel@tonic-gate 		meth=s->ctx->method->get_ssl_method(session->ssl_version);
5610Sstevel@tonic-gate 		if (meth == NULL)
5620Sstevel@tonic-gate 			meth=s->method->get_ssl_method(session->ssl_version);
5630Sstevel@tonic-gate 		if (meth == NULL)
5640Sstevel@tonic-gate 			{
5650Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
5660Sstevel@tonic-gate 			return(0);
5670Sstevel@tonic-gate 			}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (meth != s->method)
5700Sstevel@tonic-gate 			{
5710Sstevel@tonic-gate 			if (!SSL_set_ssl_method(s,meth))
5720Sstevel@tonic-gate 				return(0);
5730Sstevel@tonic-gate 			if (s->ctx->session_timeout == 0)
5740Sstevel@tonic-gate 				session->timeout=SSL_get_default_timeout(s);
5750Sstevel@tonic-gate 			else
5760Sstevel@tonic-gate 				session->timeout=s->ctx->session_timeout;
5770Sstevel@tonic-gate 			}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
5800Sstevel@tonic-gate                 if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
5810Sstevel@tonic-gate                     session->krb5_client_princ_len > 0)
5820Sstevel@tonic-gate                 {
5830Sstevel@tonic-gate                     s->kssl_ctx->client_princ = (char *)malloc(session->krb5_client_princ_len + 1);
5840Sstevel@tonic-gate                     memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
5850Sstevel@tonic-gate                             session->krb5_client_princ_len);
5860Sstevel@tonic-gate                     s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
5870Sstevel@tonic-gate                 }
5880Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
5910Sstevel@tonic-gate 		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
5920Sstevel@tonic-gate 		if (s->session != NULL)
5930Sstevel@tonic-gate 			SSL_SESSION_free(s->session);
5940Sstevel@tonic-gate 		s->session=session;
5950Sstevel@tonic-gate 		s->verify_result = s->session->verify_result;
5960Sstevel@tonic-gate 		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
5970Sstevel@tonic-gate 		ret=1;
5980Sstevel@tonic-gate 		}
5990Sstevel@tonic-gate 	else
6000Sstevel@tonic-gate 		{
6010Sstevel@tonic-gate 		if (s->session != NULL)
6020Sstevel@tonic-gate 			{
6030Sstevel@tonic-gate 			SSL_SESSION_free(s->session);
6040Sstevel@tonic-gate 			s->session=NULL;
6050Sstevel@tonic-gate 			}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		meth=s->ctx->method;
6080Sstevel@tonic-gate 		if (meth != s->method)
6090Sstevel@tonic-gate 			{
6100Sstevel@tonic-gate 			if (!SSL_set_ssl_method(s,meth))
6110Sstevel@tonic-gate 				return(0);
6120Sstevel@tonic-gate 			}
6130Sstevel@tonic-gate 		ret=1;
6140Sstevel@tonic-gate 		}
6150Sstevel@tonic-gate 	return(ret);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
SSL_SESSION_set_timeout(SSL_SESSION * s,long t)6180Sstevel@tonic-gate long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
6190Sstevel@tonic-gate 	{
6200Sstevel@tonic-gate 	if (s == NULL) return(0);
6210Sstevel@tonic-gate 	s->timeout=t;
6220Sstevel@tonic-gate 	return(1);
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
SSL_SESSION_get_timeout(const SSL_SESSION * s)625*2139Sjp161948 long SSL_SESSION_get_timeout(const SSL_SESSION *s)
6260Sstevel@tonic-gate 	{
6270Sstevel@tonic-gate 	if (s == NULL) return(0);
6280Sstevel@tonic-gate 	return(s->timeout);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
SSL_SESSION_get_time(const SSL_SESSION * s)631*2139Sjp161948 long SSL_SESSION_get_time(const SSL_SESSION *s)
6320Sstevel@tonic-gate 	{
6330Sstevel@tonic-gate 	if (s == NULL) return(0);
6340Sstevel@tonic-gate 	return(s->time);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
SSL_SESSION_set_time(SSL_SESSION * s,long t)6370Sstevel@tonic-gate long SSL_SESSION_set_time(SSL_SESSION *s, long t)
6380Sstevel@tonic-gate 	{
6390Sstevel@tonic-gate 	if (s == NULL) return(0);
6400Sstevel@tonic-gate 	s->time=t;
6410Sstevel@tonic-gate 	return(t);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
SSL_CTX_set_timeout(SSL_CTX * s,long t)6440Sstevel@tonic-gate long SSL_CTX_set_timeout(SSL_CTX *s, long t)
6450Sstevel@tonic-gate 	{
6460Sstevel@tonic-gate 	long l;
6470Sstevel@tonic-gate 	if (s == NULL) return(0);
6480Sstevel@tonic-gate 	l=s->session_timeout;
6490Sstevel@tonic-gate 	s->session_timeout=t;
6500Sstevel@tonic-gate 	return(l);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
SSL_CTX_get_timeout(const SSL_CTX * s)653*2139Sjp161948 long SSL_CTX_get_timeout(const SSL_CTX *s)
6540Sstevel@tonic-gate 	{
6550Sstevel@tonic-gate 	if (s == NULL) return(0);
6560Sstevel@tonic-gate 	return(s->session_timeout);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate typedef struct timeout_param_st
6600Sstevel@tonic-gate 	{
6610Sstevel@tonic-gate 	SSL_CTX *ctx;
6620Sstevel@tonic-gate 	long time;
6630Sstevel@tonic-gate 	LHASH *cache;
6640Sstevel@tonic-gate 	} TIMEOUT_PARAM;
6650Sstevel@tonic-gate 
timeout(SSL_SESSION * s,TIMEOUT_PARAM * p)6660Sstevel@tonic-gate static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
6670Sstevel@tonic-gate 	{
6680Sstevel@tonic-gate 	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
6690Sstevel@tonic-gate 		{
6700Sstevel@tonic-gate 		/* The reason we don't call SSL_CTX_remove_session() is to
6710Sstevel@tonic-gate 		 * save on locking overhead */
6720Sstevel@tonic-gate 		lh_delete(p->cache,s);
6730Sstevel@tonic-gate 		SSL_SESSION_list_remove(p->ctx,s);
6740Sstevel@tonic-gate 		s->not_resumable=1;
6750Sstevel@tonic-gate 		if (p->ctx->remove_session_cb != NULL)
6760Sstevel@tonic-gate 			p->ctx->remove_session_cb(p->ctx,s);
6770Sstevel@tonic-gate 		SSL_SESSION_free(s);
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
IMPLEMENT_LHASH_DOALL_ARG_FN(timeout,SSL_SESSION *,TIMEOUT_PARAM *)6810Sstevel@tonic-gate static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
6840Sstevel@tonic-gate 	{
6850Sstevel@tonic-gate 	unsigned long i;
6860Sstevel@tonic-gate 	TIMEOUT_PARAM tp;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	tp.ctx=s;
6890Sstevel@tonic-gate 	tp.cache=s->sessions;
6900Sstevel@tonic-gate 	if (tp.cache == NULL) return;
6910Sstevel@tonic-gate 	tp.time=t;
6920Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
6930Sstevel@tonic-gate 	i=tp.cache->down_load;
6940Sstevel@tonic-gate 	tp.cache->down_load=0;
6950Sstevel@tonic-gate 	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
6960Sstevel@tonic-gate 	tp.cache->down_load=i;
6970Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
ssl_clear_bad_session(SSL * s)7000Sstevel@tonic-gate int ssl_clear_bad_session(SSL *s)
7010Sstevel@tonic-gate 	{
7020Sstevel@tonic-gate 	if (	(s->session != NULL) &&
7030Sstevel@tonic-gate 		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
7040Sstevel@tonic-gate 		!(SSL_in_init(s) || SSL_in_before(s)))
7050Sstevel@tonic-gate 		{
7060Sstevel@tonic-gate 		SSL_CTX_remove_session(s->ctx,s->session);
7070Sstevel@tonic-gate 		return(1);
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	else
7100Sstevel@tonic-gate 		return(0);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /* locked by SSL_CTX in the calling function */
SSL_SESSION_list_remove(SSL_CTX * ctx,SSL_SESSION * s)7140Sstevel@tonic-gate static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
7150Sstevel@tonic-gate 	{
7160Sstevel@tonic-gate 	if ((s->next == NULL) || (s->prev == NULL)) return;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
7190Sstevel@tonic-gate 		{ /* last element in list */
7200Sstevel@tonic-gate 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
7210Sstevel@tonic-gate 			{ /* only one element in list */
7220Sstevel@tonic-gate 			ctx->session_cache_head=NULL;
7230Sstevel@tonic-gate 			ctx->session_cache_tail=NULL;
7240Sstevel@tonic-gate 			}
7250Sstevel@tonic-gate 		else
7260Sstevel@tonic-gate 			{
7270Sstevel@tonic-gate 			ctx->session_cache_tail=s->prev;
7280Sstevel@tonic-gate 			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
7290Sstevel@tonic-gate 			}
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 	else
7320Sstevel@tonic-gate 		{
7330Sstevel@tonic-gate 		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
7340Sstevel@tonic-gate 			{ /* first element in list */
7350Sstevel@tonic-gate 			ctx->session_cache_head=s->next;
7360Sstevel@tonic-gate 			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 		else
7390Sstevel@tonic-gate 			{ /* middle of list */
7400Sstevel@tonic-gate 			s->next->prev=s->prev;
7410Sstevel@tonic-gate 			s->prev->next=s->next;
7420Sstevel@tonic-gate 			}
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 	s->prev=s->next=NULL;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
SSL_SESSION_list_add(SSL_CTX * ctx,SSL_SESSION * s)7470Sstevel@tonic-gate static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
7480Sstevel@tonic-gate 	{
7490Sstevel@tonic-gate 	if ((s->next != NULL) && (s->prev != NULL))
7500Sstevel@tonic-gate 		SSL_SESSION_list_remove(ctx,s);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if (ctx->session_cache_head == NULL)
7530Sstevel@tonic-gate 		{
7540Sstevel@tonic-gate 		ctx->session_cache_head=s;
7550Sstevel@tonic-gate 		ctx->session_cache_tail=s;
7560Sstevel@tonic-gate 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
7570Sstevel@tonic-gate 		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 	else
7600Sstevel@tonic-gate 		{
7610Sstevel@tonic-gate 		s->next=ctx->session_cache_head;
7620Sstevel@tonic-gate 		s->next->prev=s;
7630Sstevel@tonic-gate 		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
7640Sstevel@tonic-gate 		ctx->session_cache_head=s;
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
768