ssl_sess.c revision 194206
155714Skris/* ssl/ssl_sess.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <openssl/lhash.h>
6155714Skris#include <openssl/rand.h>
62194206Ssimon#ifndef OPENSSL_NO_ENGINE
63194206Ssimon#include <openssl/engine.h>
64194206Ssimon#endif
6555714Skris#include "ssl_locl.h"
6655714Skris
6755714Skrisstatic void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
6855714Skrisstatic void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
6955714Skrisstatic int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
7055714Skris
71160814SsimonSSL_SESSION *SSL_get_session(const SSL *ssl)
7259191Skris/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
7355714Skris	{
7455714Skris	return(ssl->session);
7555714Skris	}
7655714Skris
7759191SkrisSSL_SESSION *SSL_get1_session(SSL *ssl)
7859191Skris/* variant of SSL_get_session: caller really gets something */
7955714Skris	{
8059191Skris	SSL_SESSION *sess;
8159191Skris	/* Need to lock this all up rather than just use CRYPTO_add so that
8259191Skris	 * somebody doesn't free ssl->session between when we check it's
8359191Skris	 * non-null and when we up the reference count. */
84120631Snectar	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
8559191Skris	sess = ssl->session;
8659191Skris	if(sess)
8759191Skris		sess->references++;
88120631Snectar	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
8959191Skris	return(sess);
9059191Skris	}
9159191Skris
9259191Skrisint SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
9359191Skris	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
9459191Skris	{
95109998Smarkm	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
96109998Smarkm			new_func, dup_func, free_func);
9755714Skris	}
9855714Skris
9955714Skrisint SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
10055714Skris	{
10155714Skris	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
10255714Skris	}
10355714Skris
104160814Ssimonvoid *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
10555714Skris	{
10655714Skris	return(CRYPTO_get_ex_data(&s->ex_data,idx));
10755714Skris	}
10855714Skris
10955714SkrisSSL_SESSION *SSL_SESSION_new(void)
11055714Skris	{
11155714Skris	SSL_SESSION *ss;
11255714Skris
11368651Skris	ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
11455714Skris	if (ss == NULL)
11555714Skris		{
11655714Skris		SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
11755714Skris		return(0);
11855714Skris		}
11955714Skris	memset(ss,0,sizeof(SSL_SESSION));
12055714Skris
12159191Skris	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
12255714Skris	ss->references=1;
12355714Skris	ss->timeout=60*5+4; /* 5 minute timeout by default */
124160814Ssimon	ss->time=(unsigned long)time(NULL);
12555714Skris	ss->prev=NULL;
12655714Skris	ss->next=NULL;
12755714Skris	ss->compress_meth=0;
128194206Ssimon#ifndef OPENSSL_NO_TLSEXT
129194206Ssimon	ss->tlsext_hostname = NULL;
130194206Ssimon#endif
131109998Smarkm	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
13255714Skris	return(ss);
13355714Skris	}
13455714Skris
135160814Ssimonconst unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
136160814Ssimon	{
137160814Ssimon	if(len)
138160814Ssimon		*len = s->session_id_length;
139160814Ssimon	return s->session_id;
140160814Ssimon	}
141160814Ssimon
142109998Smarkm/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
143109998Smarkm * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
144109998Smarkm * until we have no conflict is going to complete in one iteration pretty much
145109998Smarkm * "most" of the time (btw: understatement). So, if it takes us 10 iterations
146109998Smarkm * and we still can't avoid a conflict - well that's a reasonable point to call
147109998Smarkm * it quits. Either the RAND code is broken or someone is trying to open roughly
148109998Smarkm * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
149109998Smarkm * store that many sessions is perhaps a more interesting question ... */
150109998Smarkm
151109998Smarkm#define MAX_SESS_ID_ATTEMPTS 10
152109998Smarkmstatic int def_generate_session_id(const SSL *ssl, unsigned char *id,
153109998Smarkm				unsigned int *id_len)
154109998Smarkm{
155109998Smarkm	unsigned int retry = 0;
156109998Smarkm	do
157160814Ssimon		if (RAND_pseudo_bytes(id, *id_len) <= 0)
158142425Snectar			return 0;
159109998Smarkm	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
160109998Smarkm		(++retry < MAX_SESS_ID_ATTEMPTS));
161109998Smarkm	if(retry < MAX_SESS_ID_ATTEMPTS)
162109998Smarkm		return 1;
163109998Smarkm	/* else - woops a session_id match */
164109998Smarkm	/* XXX We should also check the external cache --
165109998Smarkm	 * but the probability of a collision is negligible, and
166109998Smarkm	 * we could not prevent the concurrent creation of sessions
167109998Smarkm	 * with identical IDs since we currently don't have means
168109998Smarkm	 * to atomically check whether a session ID already exists
169109998Smarkm	 * and make a reservation for it if it does not
170109998Smarkm	 * (this problem applies to the internal cache as well).
171109998Smarkm	 */
172109998Smarkm	return 0;
173109998Smarkm}
174109998Smarkm
17555714Skrisint ssl_get_new_session(SSL *s, int session)
17655714Skris	{
17755714Skris	/* This gets used by clients and servers. */
17855714Skris
179109998Smarkm	unsigned int tmp;
18055714Skris	SSL_SESSION *ss=NULL;
181109998Smarkm	GEN_SESSION_CB cb = def_generate_session_id;
18255714Skris
18355714Skris	if ((ss=SSL_SESSION_new()) == NULL) return(0);
18455714Skris
18555714Skris	/* If the context has a default timeout, use it */
18655714Skris	if (s->ctx->session_timeout == 0)
18755714Skris		ss->timeout=SSL_get_default_timeout(s);
18855714Skris	else
18955714Skris		ss->timeout=s->ctx->session_timeout;
19055714Skris
19155714Skris	if (s->session != NULL)
19255714Skris		{
19355714Skris		SSL_SESSION_free(s->session);
19455714Skris		s->session=NULL;
19555714Skris		}
19655714Skris
19755714Skris	if (session)
19855714Skris		{
19955714Skris		if (s->version == SSL2_VERSION)
20055714Skris			{
20155714Skris			ss->ssl_version=SSL2_VERSION;
20255714Skris			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
20355714Skris			}
20455714Skris		else if (s->version == SSL3_VERSION)
20555714Skris			{
20655714Skris			ss->ssl_version=SSL3_VERSION;
20755714Skris			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
20855714Skris			}
20955714Skris		else if (s->version == TLS1_VERSION)
21055714Skris			{
21155714Skris			ss->ssl_version=TLS1_VERSION;
21255714Skris			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
21355714Skris			}
214160814Ssimon		else if (s->version == DTLS1_VERSION)
215160814Ssimon			{
216160814Ssimon			ss->ssl_version=DTLS1_VERSION;
217160814Ssimon			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
218160814Ssimon			}
21955714Skris		else
22055714Skris			{
22155714Skris			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
22255714Skris			SSL_SESSION_free(ss);
22355714Skris			return(0);
22455714Skris			}
225194206Ssimon#ifndef OPENSSL_NO_TLSEXT
226194206Ssimon		/* If RFC4507 ticket use empty session ID */
227194206Ssimon		if (s->tlsext_ticket_expected)
228194206Ssimon			{
229194206Ssimon			ss->session_id_length = 0;
230194206Ssimon			goto sess_id_done;
231194206Ssimon			}
232194206Ssimon#endif
233109998Smarkm		/* Choose which callback will set the session ID */
234109998Smarkm		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
235109998Smarkm		if(s->generate_session_id)
236109998Smarkm			cb = s->generate_session_id;
237109998Smarkm		else if(s->ctx->generate_session_id)
238109998Smarkm			cb = s->ctx->generate_session_id;
239109998Smarkm		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
240109998Smarkm		/* Choose a session ID */
241109998Smarkm		tmp = ss->session_id_length;
242109998Smarkm		if(!cb(s, ss->session_id, &tmp))
24355714Skris			{
244109998Smarkm			/* The callback failed */
245109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
246109998Smarkm				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
247109998Smarkm			SSL_SESSION_free(ss);
248109998Smarkm			return(0);
24955714Skris			}
250109998Smarkm		/* Don't allow the callback to set the session length to zero.
251109998Smarkm		 * nor set it higher than it was. */
252109998Smarkm		if(!tmp || (tmp > ss->session_id_length))
253109998Smarkm			{
254109998Smarkm			/* The callback set an illegal length */
255109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
256109998Smarkm				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
257109998Smarkm			SSL_SESSION_free(ss);
258109998Smarkm			return(0);
259109998Smarkm			}
260109998Smarkm		/* If the session length was shrunk and we're SSLv2, pad it */
261109998Smarkm		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
262109998Smarkm			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
263109998Smarkm		else
264109998Smarkm			ss->session_id_length = tmp;
265109998Smarkm		/* Finally, check for a conflict */
266109998Smarkm		if(SSL_has_matching_session_id(s, ss->session_id,
267109998Smarkm						ss->session_id_length))
268109998Smarkm			{
269109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
270109998Smarkm				SSL_R_SSL_SESSION_ID_CONFLICT);
271109998Smarkm			SSL_SESSION_free(ss);
272109998Smarkm			return(0);
273109998Smarkm			}
274194206Ssimon#ifndef OPENSSL_NO_TLSEXT
275194206Ssimon		sess_id_done:
276194206Ssimon		if (s->tlsext_hostname) {
277194206Ssimon			ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
278194206Ssimon			if (ss->tlsext_hostname == NULL) {
279194206Ssimon				SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
280194206Ssimon				SSL_SESSION_free(ss);
281194206Ssimon				return 0;
282194206Ssimon				}
283194206Ssimon			}
284194206Ssimon#endif
28555714Skris		}
28655714Skris	else
28755714Skris		{
28855714Skris		ss->session_id_length=0;
28955714Skris		}
29055714Skris
291101615Snectar	if (s->sid_ctx_length > sizeof ss->sid_ctx)
292101615Snectar		{
293109998Smarkm		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
294101615Snectar		SSL_SESSION_free(ss);
295101615Snectar		return 0;
296101615Snectar		}
29755714Skris	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
29855714Skris	ss->sid_ctx_length=s->sid_ctx_length;
29955714Skris	s->session=ss;
30055714Skris	ss->ssl_version=s->version;
30159191Skris	ss->verify_result = X509_V_OK;
30255714Skris
30355714Skris	return(1);
30455714Skris	}
30555714Skris
306194206Ssimonint ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
307194206Ssimon			const unsigned char *limit)
30855714Skris	{
30955714Skris	/* This is used only by servers. */
31055714Skris
311194206Ssimon	SSL_SESSION *ret=NULL;
31255714Skris	int fatal = 0;
313194206Ssimon#ifndef OPENSSL_NO_TLSEXT
314194206Ssimon	int r;
315194206Ssimon#endif
316194206Ssimon
31755714Skris	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
31855714Skris		goto err;
319194206Ssimon#ifndef OPENSSL_NO_TLSEXT
320194206Ssimon 	r = tls1_process_ticket(s, session_id, len, limit, &ret);
321194206Ssimon	if (r == -1)
322194206Ssimon		{
323194206Ssimon		fatal = 1;
324194206Ssimon 		goto err;
325194206Ssimon		}
326194206Ssimon	else if (r == 0 || (!ret && !len))
327194206Ssimon		goto err;
328194206Ssimon	else if (!ret && !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
329194206Ssimon#else
330194206Ssimon	if (len == 0)
331194206Ssimon		goto err;
33255714Skris	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
333194206Ssimon#endif
33455714Skris		{
335194206Ssimon		SSL_SESSION data;
336194206Ssimon		data.ssl_version=s->version;
337194206Ssimon		data.session_id_length=len;
338194206Ssimon		if (len == 0)
339194206Ssimon			return 0;
340194206Ssimon 		memcpy(data.session_id,session_id,len);
34155714Skris		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
34259191Skris		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
34355714Skris		if (ret != NULL)
34455714Skris		    /* don't allow other threads to steal it: */
34555714Skris		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
34655714Skris		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
34755714Skris		}
34855714Skris
34955714Skris	if (ret == NULL)
35055714Skris		{
35155714Skris		int copy=1;
35255714Skris
35355714Skris		s->ctx->stats.sess_miss++;
35455714Skris		ret=NULL;
35555714Skris		if (s->ctx->get_session_cb != NULL
35655714Skris		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
35755714Skris		       != NULL)
35855714Skris			{
35955714Skris			s->ctx->stats.sess_cb_hit++;
36055714Skris
36155714Skris			/* Increment reference count now if the session callback
36255714Skris			 * asks us to do so (note that if the session structures
36355714Skris			 * returned by the callback are shared between threads,
36455714Skris			 * it must handle the reference count itself [i.e. copy == 0],
36555714Skris			 * or things won't be thread-safe). */
36655714Skris			if (copy)
36755714Skris				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
36855714Skris
369109998Smarkm			/* Add the externally cached session to the internal
370109998Smarkm			 * cache as well if and only if we are supposed to. */
371109998Smarkm			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
372109998Smarkm				/* The following should not return 1, otherwise,
373109998Smarkm				 * things are very strange */
374109998Smarkm				SSL_CTX_add_session(s->ctx,ret);
37555714Skris			}
37655714Skris		if (ret == NULL)
37755714Skris			goto err;
37855714Skris		}
37955714Skris
38055714Skris	/* Now ret is non-NULL, and we own one of its reference counts. */
38155714Skris
382194206Ssimon	if (ret->sid_ctx_length != s->sid_ctx_length
383194206Ssimon	    || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
384194206Ssimon		{
38555714Skris		/* We've found the session named by the client, but we don't
38655714Skris		 * want to use it in this context. */
38755714Skris
38855714Skris#if 0 /* The client cannot always know when a session is not appropriate,
389194206Ssimon       * so we shouldn't generate an error message. */
39055714Skris
391194206Ssimon		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
39255714Skris#endif
393194206Ssimon		goto err; /* treat like cache miss */
39455714Skris		}
395194206Ssimon
396194206Ssimon	if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
397194206Ssimon		{
398194206Ssimon		/* We can't be sure if this session is being used out of
399194206Ssimon		 * context, which is especially important for SSL_VERIFY_PEER.
400194206Ssimon		 * The application should have used SSL[_CTX]_set_session_id_context.
401194206Ssimon		 *
402194206Ssimon		 * For this error case, we generate an error instead of treating
403194206Ssimon		 * the event like a cache miss (otherwise it would be easy for
404194206Ssimon		 * applications to effectively disable the session cache by
405194206Ssimon		 * accident without anyone noticing).
406194206Ssimon		 */
407194206Ssimon
408194206Ssimon		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
409194206Ssimon		fatal = 1;
410194206Ssimon		goto err;
411194206Ssimon		}
41255714Skris
41355714Skris	if (ret->cipher == NULL)
41455714Skris		{
41555714Skris		unsigned char buf[5],*p;
41655714Skris		unsigned long l;
41755714Skris
41855714Skris		p=buf;
41955714Skris		l=ret->cipher_id;
42055714Skris		l2n(l,p);
42155714Skris		if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
42255714Skris			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
42355714Skris		else
42455714Skris			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
42555714Skris		if (ret->cipher == NULL)
42655714Skris			goto err;
42755714Skris		}
42855714Skris
42955714Skris
43055714Skris#if 0 /* This is way too late. */
43155714Skris
43255714Skris	/* If a thread got the session, then 'swaped', and another got
43368651Skris	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
43455714Skris	 * be in trouble.  So I'll increment it now, then double decrement
43555714Skris	 * later - am I speaking rubbish?. */
43655714Skris	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
43755714Skris#endif
43855714Skris
439160814Ssimon	if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
44055714Skris		{
44155714Skris		s->ctx->stats.sess_timeout++;
44255714Skris		/* remove it from the cache */
44355714Skris		SSL_CTX_remove_session(s->ctx,ret);
44455714Skris		goto err;
44555714Skris		}
44655714Skris
44755714Skris	s->ctx->stats.sess_hit++;
44855714Skris
44955714Skris	/* ret->time=time(NULL); */ /* rezero timeout? */
45055714Skris	/* again, just leave the session
45155714Skris	 * if it is the same session, we have just incremented and
45255714Skris	 * then decremented the reference count :-) */
45355714Skris	if (s->session != NULL)
45455714Skris		SSL_SESSION_free(s->session);
45555714Skris	s->session=ret;
45659191Skris	s->verify_result = s->session->verify_result;
45755714Skris	return(1);
45855714Skris
45955714Skris err:
46055714Skris	if (ret != NULL)
46155714Skris		SSL_SESSION_free(ret);
46255714Skris	if (fatal)
46355714Skris		return -1;
46455714Skris	else
46555714Skris		return 0;
46655714Skris	}
46755714Skris
46855714Skrisint SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
46955714Skris	{
47055714Skris	int ret=0;
47155714Skris	SSL_SESSION *s;
47255714Skris
47359191Skris	/* add just 1 reference count for the SSL_CTX's session cache
47459191Skris	 * even though it has two ways of access: each session is in a
47559191Skris	 * doubly linked list and an lhash */
47655714Skris	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
47759191Skris	/* if session c is in already in cache, we take back the increment later */
47855714Skris
47955714Skris	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
48059191Skris	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
48155714Skris
48259191Skris	/* s != NULL iff we already had a session with the given PID.
48359191Skris	 * In this case, s == c should hold (then we did not really modify
48459191Skris	 * ctx->sessions), or we're in trouble. */
48559191Skris	if (s != NULL && s != c)
48659191Skris		{
48759191Skris		/* We *are* in trouble ... */
48859191Skris		SSL_SESSION_list_remove(ctx,s);
48959191Skris		SSL_SESSION_free(s);
49059191Skris		/* ... so pretend the other session did not exist in cache
49159191Skris		 * (we cannot handle two SSL_SESSION structures with identical
49259191Skris		 * session ID in the same cache, which could happen e.g. when
49359191Skris		 * two threads concurrently obtain the same session from an external
49459191Skris		 * cache) */
49559191Skris		s = NULL;
49659191Skris		}
49759191Skris
49859191Skris 	/* Put at the head of the queue unless it is already in the cache */
49955714Skris	if (s == NULL)
50055714Skris		SSL_SESSION_list_add(ctx,c);
50155714Skris
50255714Skris	if (s != NULL)
50355714Skris		{
50459191Skris		/* existing cache entry -- decrement previously incremented reference
50559191Skris		 * count because it already takes into account the cache */
50659191Skris
50759191Skris		SSL_SESSION_free(s); /* s == c */
50855714Skris		ret=0;
50955714Skris		}
51055714Skris	else
51155714Skris		{
51259191Skris		/* new cache entry -- remove old ones if cache has become too large */
51359191Skris
51455714Skris		ret=1;
51555714Skris
51655714Skris		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
51755714Skris			{
51855714Skris			while (SSL_CTX_sess_number(ctx) >
51955714Skris				SSL_CTX_sess_get_cache_size(ctx))
52055714Skris				{
52155714Skris				if (!remove_session_lock(ctx,
52255714Skris					ctx->session_cache_tail, 0))
52355714Skris					break;
52455714Skris				else
52555714Skris					ctx->stats.sess_cache_full++;
52655714Skris				}
52755714Skris			}
52855714Skris		}
52955714Skris	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
53055714Skris	return(ret);
53155714Skris	}
53255714Skris
53355714Skrisint SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
53455714Skris{
53555714Skris	return remove_session_lock(ctx, c, 1);
53655714Skris}
53755714Skris
53855714Skrisstatic int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
53955714Skris	{
54055714Skris	SSL_SESSION *r;
54155714Skris	int ret=0;
54255714Skris
54355714Skris	if ((c != NULL) && (c->session_id_length != 0))
54455714Skris		{
54555714Skris		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
546100928Snectar		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
54755714Skris			{
54855714Skris			ret=1;
549100928Snectar			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
55055714Skris			SSL_SESSION_list_remove(ctx,c);
55155714Skris			}
55255714Skris
55355714Skris		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
55455714Skris
55555714Skris		if (ret)
55655714Skris			{
55755714Skris			r->not_resumable=1;
55855714Skris			if (ctx->remove_session_cb != NULL)
55955714Skris				ctx->remove_session_cb(ctx,r);
56055714Skris			SSL_SESSION_free(r);
56155714Skris			}
56255714Skris		}
56355714Skris	else
56455714Skris		ret=0;
56555714Skris	return(ret);
56655714Skris	}
56755714Skris
56855714Skrisvoid SSL_SESSION_free(SSL_SESSION *ss)
56955714Skris	{
57055714Skris	int i;
57155714Skris
57255714Skris	if(ss == NULL)
57355714Skris	    return;
57455714Skris
57555714Skris	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
57655714Skris#ifdef REF_PRINT
57755714Skris	REF_PRINT("SSL_SESSION",ss);
57855714Skris#endif
57955714Skris	if (i > 0) return;
58055714Skris#ifdef REF_CHECK
58155714Skris	if (i < 0)
58255714Skris		{
58355714Skris		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
58455714Skris		abort(); /* ok */
58555714Skris		}
58655714Skris#endif
58755714Skris
588109998Smarkm	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
58955714Skris
590109998Smarkm	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
591109998Smarkm	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
592109998Smarkm	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
59355714Skris	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
59455714Skris	if (ss->peer != NULL) X509_free(ss->peer);
59555714Skris	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
596194206Ssimon#ifndef OPENSSL_NO_TLSEXT
597194206Ssimon	if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
598194206Ssimon	if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
599194206Ssimon#endif
600109998Smarkm	OPENSSL_cleanse(ss,sizeof(*ss));
60168651Skris	OPENSSL_free(ss);
60255714Skris	}
60355714Skris
60455714Skrisint SSL_set_session(SSL *s, SSL_SESSION *session)
60555714Skris	{
60655714Skris	int ret=0;
60755714Skris	SSL_METHOD *meth;
60855714Skris
60955714Skris	if (session != NULL)
61055714Skris		{
61155714Skris		meth=s->ctx->method->get_ssl_method(session->ssl_version);
61255714Skris		if (meth == NULL)
61355714Skris			meth=s->method->get_ssl_method(session->ssl_version);
61455714Skris		if (meth == NULL)
61555714Skris			{
61655714Skris			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
61755714Skris			return(0);
61855714Skris			}
61955714Skris
62055714Skris		if (meth != s->method)
62155714Skris			{
62255714Skris			if (!SSL_set_ssl_method(s,meth))
62355714Skris				return(0);
62455714Skris			if (s->ctx->session_timeout == 0)
62555714Skris				session->timeout=SSL_get_default_timeout(s);
62655714Skris			else
62755714Skris				session->timeout=s->ctx->session_timeout;
62855714Skris			}
62955714Skris
630109998Smarkm#ifndef OPENSSL_NO_KRB5
631109998Smarkm                if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
632109998Smarkm                    session->krb5_client_princ_len > 0)
633109998Smarkm                {
634167612Ssimon                    s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
635109998Smarkm                    memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
636109998Smarkm                            session->krb5_client_princ_len);
637109998Smarkm                    s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
638109998Smarkm                }
639109998Smarkm#endif /* OPENSSL_NO_KRB5 */
640109998Smarkm
64155714Skris		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
64255714Skris		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
64355714Skris		if (s->session != NULL)
64455714Skris			SSL_SESSION_free(s->session);
64555714Skris		s->session=session;
64672613Skris		s->verify_result = s->session->verify_result;
64755714Skris		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
64855714Skris		ret=1;
64955714Skris		}
65055714Skris	else
65155714Skris		{
65255714Skris		if (s->session != NULL)
65355714Skris			{
65455714Skris			SSL_SESSION_free(s->session);
65555714Skris			s->session=NULL;
65655714Skris			}
65755714Skris
65855714Skris		meth=s->ctx->method;
65955714Skris		if (meth != s->method)
66055714Skris			{
66155714Skris			if (!SSL_set_ssl_method(s,meth))
66255714Skris				return(0);
66355714Skris			}
66455714Skris		ret=1;
66555714Skris		}
66655714Skris	return(ret);
66755714Skris	}
66855714Skris
66955714Skrislong SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
67055714Skris	{
67155714Skris	if (s == NULL) return(0);
67255714Skris	s->timeout=t;
67355714Skris	return(1);
67455714Skris	}
67555714Skris
676160814Ssimonlong SSL_SESSION_get_timeout(const SSL_SESSION *s)
67755714Skris	{
67855714Skris	if (s == NULL) return(0);
67955714Skris	return(s->timeout);
68055714Skris	}
68155714Skris
682160814Ssimonlong SSL_SESSION_get_time(const SSL_SESSION *s)
68355714Skris	{
68455714Skris	if (s == NULL) return(0);
68555714Skris	return(s->time);
68655714Skris	}
68755714Skris
68855714Skrislong SSL_SESSION_set_time(SSL_SESSION *s, long t)
68955714Skris	{
69055714Skris	if (s == NULL) return(0);
69155714Skris	s->time=t;
69255714Skris	return(t);
69355714Skris	}
69455714Skris
69555714Skrislong SSL_CTX_set_timeout(SSL_CTX *s, long t)
69655714Skris	{
69755714Skris	long l;
69855714Skris	if (s == NULL) return(0);
69955714Skris	l=s->session_timeout;
70055714Skris	s->session_timeout=t;
70155714Skris	return(l);
70255714Skris	}
70355714Skris
704160814Ssimonlong SSL_CTX_get_timeout(const SSL_CTX *s)
70555714Skris	{
70655714Skris	if (s == NULL) return(0);
70755714Skris	return(s->session_timeout);
70855714Skris	}
70955714Skris
71055714Skristypedef struct timeout_param_st
71155714Skris	{
71255714Skris	SSL_CTX *ctx;
71355714Skris	long time;
71455714Skris	LHASH *cache;
71555714Skris	} TIMEOUT_PARAM;
71655714Skris
71755714Skrisstatic void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
71855714Skris	{
71955714Skris	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
72055714Skris		{
72155714Skris		/* The reason we don't call SSL_CTX_remove_session() is to
72255714Skris		 * save on locking overhead */
72359191Skris		lh_delete(p->cache,s);
72455714Skris		SSL_SESSION_list_remove(p->ctx,s);
72555714Skris		s->not_resumable=1;
72655714Skris		if (p->ctx->remove_session_cb != NULL)
72755714Skris			p->ctx->remove_session_cb(p->ctx,s);
72855714Skris		SSL_SESSION_free(s);
72955714Skris		}
73055714Skris	}
73155714Skris
732109998Smarkmstatic IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
733109998Smarkm
73455714Skrisvoid SSL_CTX_flush_sessions(SSL_CTX *s, long t)
73555714Skris	{
73655714Skris	unsigned long i;
73755714Skris	TIMEOUT_PARAM tp;
73855714Skris
73955714Skris	tp.ctx=s;
74055714Skris	tp.cache=s->sessions;
74155714Skris	if (tp.cache == NULL) return;
74255714Skris	tp.time=t;
74355714Skris	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
74455714Skris	i=tp.cache->down_load;
74555714Skris	tp.cache->down_load=0;
746109998Smarkm	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
74755714Skris	tp.cache->down_load=i;
74855714Skris	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
74955714Skris	}
75055714Skris
75155714Skrisint ssl_clear_bad_session(SSL *s)
75255714Skris	{
75355714Skris	if (	(s->session != NULL) &&
75455714Skris		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
75555714Skris		!(SSL_in_init(s) || SSL_in_before(s)))
75655714Skris		{
75755714Skris		SSL_CTX_remove_session(s->ctx,s->session);
75855714Skris		return(1);
75955714Skris		}
76055714Skris	else
76155714Skris		return(0);
76255714Skris	}
76355714Skris
76455714Skris/* locked by SSL_CTX in the calling function */
76555714Skrisstatic void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
76655714Skris	{
76755714Skris	if ((s->next == NULL) || (s->prev == NULL)) return;
76855714Skris
76955714Skris	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
77055714Skris		{ /* last element in list */
77155714Skris		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
77255714Skris			{ /* only one element in list */
77355714Skris			ctx->session_cache_head=NULL;
77455714Skris			ctx->session_cache_tail=NULL;
77555714Skris			}
77655714Skris		else
77755714Skris			{
77855714Skris			ctx->session_cache_tail=s->prev;
77955714Skris			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
78055714Skris			}
78155714Skris		}
78255714Skris	else
78355714Skris		{
78455714Skris		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
78555714Skris			{ /* first element in list */
78655714Skris			ctx->session_cache_head=s->next;
78755714Skris			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
78855714Skris			}
78955714Skris		else
79055714Skris			{ /* middle of list */
79155714Skris			s->next->prev=s->prev;
79255714Skris			s->prev->next=s->next;
79355714Skris			}
79455714Skris		}
79555714Skris	s->prev=s->next=NULL;
79655714Skris	}
79755714Skris
79855714Skrisstatic void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
79955714Skris	{
80055714Skris	if ((s->next != NULL) && (s->prev != NULL))
80155714Skris		SSL_SESSION_list_remove(ctx,s);
80255714Skris
80355714Skris	if (ctx->session_cache_head == NULL)
80455714Skris		{
80555714Skris		ctx->session_cache_head=s;
80655714Skris		ctx->session_cache_tail=s;
80755714Skris		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
80855714Skris		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
80955714Skris		}
81055714Skris	else
81155714Skris		{
81255714Skris		s->next=ctx->session_cache_head;
81355714Skris		s->next->prev=s;
81455714Skris		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
81555714Skris		ctx->session_cache_head=s;
81655714Skris		}
81755714Skris	}
81855714Skris
819167612Ssimonvoid SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
820167612Ssimon	int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
821167612Ssimon	{
822167612Ssimon	ctx->new_session_cb=cb;
823167612Ssimon	}
824167612Ssimon
825167612Ssimonint (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
826167612Ssimon	{
827167612Ssimon	return ctx->new_session_cb;
828167612Ssimon	}
829167612Ssimon
830167612Ssimonvoid SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
831167612Ssimon	void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
832167612Ssimon	{
833167612Ssimon	ctx->remove_session_cb=cb;
834167612Ssimon	}
835167612Ssimon
836167612Ssimonvoid (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
837167612Ssimon	{
838167612Ssimon	return ctx->remove_session_cb;
839167612Ssimon	}
840167612Ssimon
841167612Ssimonvoid SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
842167612Ssimon	SSL_SESSION *(*cb)(struct ssl_st *ssl,
843167612Ssimon	         unsigned char *data,int len,int *copy))
844167612Ssimon	{
845167612Ssimon	ctx->get_session_cb=cb;
846167612Ssimon	}
847167612Ssimon
848167612SsimonSSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
849167612Ssimon	         unsigned char *data,int len,int *copy)
850167612Ssimon	{
851167612Ssimon	return ctx->get_session_cb;
852167612Ssimon	}
853167612Ssimon
854167612Ssimonvoid SSL_CTX_set_info_callback(SSL_CTX *ctx,
855167612Ssimon	void (*cb)(const SSL *ssl,int type,int val))
856167612Ssimon	{
857167612Ssimon	ctx->info_callback=cb;
858167612Ssimon	}
859167612Ssimon
860167612Ssimonvoid (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
861167612Ssimon	{
862167612Ssimon	return ctx->info_callback;
863167612Ssimon	}
864167612Ssimon
865167612Ssimonvoid SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
866167612Ssimon	int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
867167612Ssimon	{
868167612Ssimon	ctx->client_cert_cb=cb;
869167612Ssimon	}
870167612Ssimon
871167612Ssimonint (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
872167612Ssimon	{
873167612Ssimon	return ctx->client_cert_cb;
874167612Ssimon	}
875167612Ssimon
876194206Ssimon#ifndef OPENSSL_NO_ENGINE
877194206Ssimonint SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
878194206Ssimon	{
879194206Ssimon	if (!ENGINE_init(e))
880194206Ssimon		{
881194206Ssimon		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
882194206Ssimon		return 0;
883194206Ssimon		}
884194206Ssimon	if(!ENGINE_get_ssl_client_cert_function(e))
885194206Ssimon		{
886194206Ssimon		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD);
887194206Ssimon		ENGINE_finish(e);
888194206Ssimon		return 0;
889194206Ssimon		}
890194206Ssimon	ctx->client_cert_engine = e;
891194206Ssimon	return 1;
892194206Ssimon	}
893194206Ssimon#endif
894194206Ssimon
895167612Ssimonvoid SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
896167612Ssimon	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
897167612Ssimon	{
898167612Ssimon	ctx->app_gen_cookie_cb=cb;
899167612Ssimon	}
900167612Ssimon
901167612Ssimonvoid SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
902167612Ssimon	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
903167612Ssimon	{
904167612Ssimon	ctx->app_verify_cookie_cb=cb;
905167612Ssimon	}
906167612Ssimon
907