ssl_sess.c revision 284295
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
135284295Sdelphij/*
136284295Sdelphij * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
137284295Sdelphij * ticket == 0 then no ticket information is duplicated, otherwise it is.
138284295Sdelphij */
139284295SdelphijSSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
140284295Sdelphij{
141284295Sdelphij    SSL_SESSION *dest;
142284295Sdelphij
143284295Sdelphij    dest = OPENSSL_malloc(sizeof(*src));
144284295Sdelphij    if (dest == NULL) {
145284295Sdelphij        goto err;
146284295Sdelphij    }
147284295Sdelphij    memcpy(dest, src, sizeof(*dest));
148284295Sdelphij
149284295Sdelphij    /*
150284295Sdelphij     * Set the various pointers to NULL so that we can call SSL_SESSION_free in
151284295Sdelphij     * the case of an error whilst halfway through constructing dest
152284295Sdelphij     */
153284295Sdelphij    dest->ciphers = NULL;
154284295Sdelphij#ifndef OPENSSL_NO_TLSEXT
155284295Sdelphij    dest->tlsext_hostname = NULL;
156284295Sdelphij#endif
157284295Sdelphij    dest->tlsext_tick = NULL;
158284295Sdelphij    memset(&dest->ex_data, 0, sizeof(dest->ex_data));
159284295Sdelphij
160284295Sdelphij    /* We deliberately don't copy the prev and next pointers */
161284295Sdelphij    dest->prev = NULL;
162284295Sdelphij    dest->next = NULL;
163284295Sdelphij
164284295Sdelphij    dest->references = 1;
165284295Sdelphij
166284295Sdelphij    if (src->sess_cert != NULL)
167284295Sdelphij        CRYPTO_add(&src->sess_cert->references, 1, CRYPTO_LOCK_SSL_SESS_CERT);
168284295Sdelphij
169284295Sdelphij    if (src->peer != NULL)
170284295Sdelphij        CRYPTO_add(&src->peer->references, 1, CRYPTO_LOCK_X509);
171284295Sdelphij
172284295Sdelphij    if(src->ciphers != NULL) {
173284295Sdelphij        dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
174284295Sdelphij        if (dest->ciphers == NULL)
175284295Sdelphij            goto err;
176284295Sdelphij    }
177284295Sdelphij
178284295Sdelphij    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
179284295Sdelphij                                            &dest->ex_data, &src->ex_data)) {
180284295Sdelphij        goto err;
181284295Sdelphij    }
182284295Sdelphij
183284295Sdelphij#ifndef OPENSSL_NO_TLSEXT
184284295Sdelphij    if (src->tlsext_hostname) {
185284295Sdelphij        dest->tlsext_hostname = BUF_strdup(src->tlsext_hostname);
186284295Sdelphij        if (dest->tlsext_hostname == NULL) {
187284295Sdelphij            goto err;
188284295Sdelphij        }
189284295Sdelphij    }
190284295Sdelphij#endif
191284295Sdelphij
192284295Sdelphij    if (ticket != 0) {
193284295Sdelphij        dest->tlsext_tick = BUF_memdup(src->tlsext_tick, src->tlsext_ticklen);
194284295Sdelphij        if(dest->tlsext_tick == NULL)
195284295Sdelphij            goto err;
196284295Sdelphij    } else {
197284295Sdelphij        dest->tlsext_tick_lifetime_hint = 0;
198284295Sdelphij        dest->tlsext_ticklen = 0;
199284295Sdelphij    }
200284295Sdelphij
201284295Sdelphij    return dest;
202284295Sdelphijerr:
203284295Sdelphij    SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
204284295Sdelphij    SSL_SESSION_free(dest);
205284295Sdelphij    return NULL;
206284295Sdelphij}
207284295Sdelphij
208160814Ssimonconst unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
209160814Ssimon	{
210160814Ssimon	if(len)
211160814Ssimon		*len = s->session_id_length;
212160814Ssimon	return s->session_id;
213160814Ssimon	}
214160814Ssimon
215109998Smarkm/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
216109998Smarkm * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
217109998Smarkm * until we have no conflict is going to complete in one iteration pretty much
218109998Smarkm * "most" of the time (btw: understatement). So, if it takes us 10 iterations
219109998Smarkm * and we still can't avoid a conflict - well that's a reasonable point to call
220109998Smarkm * it quits. Either the RAND code is broken or someone is trying to open roughly
221109998Smarkm * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
222109998Smarkm * store that many sessions is perhaps a more interesting question ... */
223109998Smarkm
224109998Smarkm#define MAX_SESS_ID_ATTEMPTS 10
225109998Smarkmstatic int def_generate_session_id(const SSL *ssl, unsigned char *id,
226109998Smarkm				unsigned int *id_len)
227109998Smarkm{
228109998Smarkm	unsigned int retry = 0;
229109998Smarkm	do
230160814Ssimon		if (RAND_pseudo_bytes(id, *id_len) <= 0)
231142425Snectar			return 0;
232109998Smarkm	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
233109998Smarkm		(++retry < MAX_SESS_ID_ATTEMPTS));
234109998Smarkm	if(retry < MAX_SESS_ID_ATTEMPTS)
235109998Smarkm		return 1;
236109998Smarkm	/* else - woops a session_id match */
237109998Smarkm	/* XXX We should also check the external cache --
238109998Smarkm	 * but the probability of a collision is negligible, and
239109998Smarkm	 * we could not prevent the concurrent creation of sessions
240109998Smarkm	 * with identical IDs since we currently don't have means
241109998Smarkm	 * to atomically check whether a session ID already exists
242109998Smarkm	 * and make a reservation for it if it does not
243109998Smarkm	 * (this problem applies to the internal cache as well).
244109998Smarkm	 */
245109998Smarkm	return 0;
246109998Smarkm}
247109998Smarkm
24855714Skrisint ssl_get_new_session(SSL *s, int session)
24955714Skris	{
25055714Skris	/* This gets used by clients and servers. */
25155714Skris
252109998Smarkm	unsigned int tmp;
25355714Skris	SSL_SESSION *ss=NULL;
254109998Smarkm	GEN_SESSION_CB cb = def_generate_session_id;
25555714Skris
25655714Skris	if ((ss=SSL_SESSION_new()) == NULL) return(0);
25755714Skris
25855714Skris	/* If the context has a default timeout, use it */
25955714Skris	if (s->ctx->session_timeout == 0)
26055714Skris		ss->timeout=SSL_get_default_timeout(s);
26155714Skris	else
26255714Skris		ss->timeout=s->ctx->session_timeout;
26355714Skris
26455714Skris	if (s->session != NULL)
26555714Skris		{
26655714Skris		SSL_SESSION_free(s->session);
26755714Skris		s->session=NULL;
26855714Skris		}
26955714Skris
27055714Skris	if (session)
27155714Skris		{
27255714Skris		if (s->version == SSL2_VERSION)
27355714Skris			{
27455714Skris			ss->ssl_version=SSL2_VERSION;
27555714Skris			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
27655714Skris			}
27755714Skris		else if (s->version == SSL3_VERSION)
27855714Skris			{
27955714Skris			ss->ssl_version=SSL3_VERSION;
28055714Skris			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
28155714Skris			}
28255714Skris		else if (s->version == TLS1_VERSION)
28355714Skris			{
28455714Skris			ss->ssl_version=TLS1_VERSION;
28555714Skris			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
28655714Skris			}
287205128Ssimon		else if (s->version == DTLS1_BAD_VER)
288205128Ssimon			{
289205128Ssimon			ss->ssl_version=DTLS1_BAD_VER;
290205128Ssimon			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
291205128Ssimon			}
292160814Ssimon		else if (s->version == DTLS1_VERSION)
293160814Ssimon			{
294160814Ssimon			ss->ssl_version=DTLS1_VERSION;
295160814Ssimon			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
296160814Ssimon			}
29755714Skris		else
29855714Skris			{
29955714Skris			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
30055714Skris			SSL_SESSION_free(ss);
30155714Skris			return(0);
30255714Skris			}
303194206Ssimon#ifndef OPENSSL_NO_TLSEXT
304194206Ssimon		/* If RFC4507 ticket use empty session ID */
305194206Ssimon		if (s->tlsext_ticket_expected)
306194206Ssimon			{
307194206Ssimon			ss->session_id_length = 0;
308194206Ssimon			goto sess_id_done;
309194206Ssimon			}
310194206Ssimon#endif
311109998Smarkm		/* Choose which callback will set the session ID */
312109998Smarkm		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
313109998Smarkm		if(s->generate_session_id)
314109998Smarkm			cb = s->generate_session_id;
315109998Smarkm		else if(s->ctx->generate_session_id)
316109998Smarkm			cb = s->ctx->generate_session_id;
317109998Smarkm		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
318109998Smarkm		/* Choose a session ID */
319109998Smarkm		tmp = ss->session_id_length;
320109998Smarkm		if(!cb(s, ss->session_id, &tmp))
32155714Skris			{
322109998Smarkm			/* The callback failed */
323109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
324109998Smarkm				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
325109998Smarkm			SSL_SESSION_free(ss);
326109998Smarkm			return(0);
32755714Skris			}
328109998Smarkm		/* Don't allow the callback to set the session length to zero.
329109998Smarkm		 * nor set it higher than it was. */
330109998Smarkm		if(!tmp || (tmp > ss->session_id_length))
331109998Smarkm			{
332109998Smarkm			/* The callback set an illegal length */
333109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
334109998Smarkm				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
335109998Smarkm			SSL_SESSION_free(ss);
336109998Smarkm			return(0);
337109998Smarkm			}
338109998Smarkm		/* If the session length was shrunk and we're SSLv2, pad it */
339109998Smarkm		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
340109998Smarkm			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
341109998Smarkm		else
342109998Smarkm			ss->session_id_length = tmp;
343109998Smarkm		/* Finally, check for a conflict */
344109998Smarkm		if(SSL_has_matching_session_id(s, ss->session_id,
345109998Smarkm						ss->session_id_length))
346109998Smarkm			{
347109998Smarkm			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
348109998Smarkm				SSL_R_SSL_SESSION_ID_CONFLICT);
349109998Smarkm			SSL_SESSION_free(ss);
350109998Smarkm			return(0);
351109998Smarkm			}
352194206Ssimon#ifndef OPENSSL_NO_TLSEXT
353194206Ssimon		sess_id_done:
354194206Ssimon		if (s->tlsext_hostname) {
355194206Ssimon			ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
356194206Ssimon			if (ss->tlsext_hostname == NULL) {
357194206Ssimon				SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
358194206Ssimon				SSL_SESSION_free(ss);
359194206Ssimon				return 0;
360194206Ssimon				}
361194206Ssimon			}
362194206Ssimon#endif
36355714Skris		}
36455714Skris	else
36555714Skris		{
36655714Skris		ss->session_id_length=0;
36755714Skris		}
36855714Skris
369101615Snectar	if (s->sid_ctx_length > sizeof ss->sid_ctx)
370101615Snectar		{
371109998Smarkm		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
372101615Snectar		SSL_SESSION_free(ss);
373101615Snectar		return 0;
374101615Snectar		}
37555714Skris	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
37655714Skris	ss->sid_ctx_length=s->sid_ctx_length;
37755714Skris	s->session=ss;
37855714Skris	ss->ssl_version=s->version;
37959191Skris	ss->verify_result = X509_V_OK;
38055714Skris
38155714Skris	return(1);
38255714Skris	}
38355714Skris
384194206Ssimonint ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
385194206Ssimon			const unsigned char *limit)
38655714Skris	{
38755714Skris	/* This is used only by servers. */
38855714Skris
389194206Ssimon	SSL_SESSION *ret=NULL;
39055714Skris	int fatal = 0;
391194206Ssimon#ifndef OPENSSL_NO_TLSEXT
392194206Ssimon	int r;
393194206Ssimon#endif
394194206Ssimon
39555714Skris	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
39655714Skris		goto err;
397194206Ssimon#ifndef OPENSSL_NO_TLSEXT
398194206Ssimon 	r = tls1_process_ticket(s, session_id, len, limit, &ret);
399194206Ssimon	if (r == -1)
400194206Ssimon		{
401194206Ssimon		fatal = 1;
402194206Ssimon 		goto err;
403194206Ssimon		}
404194206Ssimon	else if (r == 0 || (!ret && !len))
405194206Ssimon		goto err;
406194206Ssimon	else if (!ret && !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
407194206Ssimon#else
408194206Ssimon	if (len == 0)
409194206Ssimon		goto err;
41055714Skris	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
411194206Ssimon#endif
41255714Skris		{
413194206Ssimon		SSL_SESSION data;
414194206Ssimon		data.ssl_version=s->version;
415194206Ssimon		data.session_id_length=len;
416194206Ssimon		if (len == 0)
417194206Ssimon			return 0;
418194206Ssimon 		memcpy(data.session_id,session_id,len);
41955714Skris		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
42059191Skris		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
42155714Skris		if (ret != NULL)
42255714Skris		    /* don't allow other threads to steal it: */
42355714Skris		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
42455714Skris		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
42555714Skris		}
42655714Skris
42755714Skris	if (ret == NULL)
42855714Skris		{
42955714Skris		int copy=1;
43055714Skris
43155714Skris		s->ctx->stats.sess_miss++;
43255714Skris		ret=NULL;
43355714Skris		if (s->ctx->get_session_cb != NULL
43455714Skris		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
43555714Skris		       != NULL)
43655714Skris			{
43755714Skris			s->ctx->stats.sess_cb_hit++;
43855714Skris
43955714Skris			/* Increment reference count now if the session callback
44055714Skris			 * asks us to do so (note that if the session structures
44155714Skris			 * returned by the callback are shared between threads,
44255714Skris			 * it must handle the reference count itself [i.e. copy == 0],
44355714Skris			 * or things won't be thread-safe). */
44455714Skris			if (copy)
44555714Skris				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
44655714Skris
447109998Smarkm			/* Add the externally cached session to the internal
448109998Smarkm			 * cache as well if and only if we are supposed to. */
449109998Smarkm			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
450109998Smarkm				/* The following should not return 1, otherwise,
451109998Smarkm				 * things are very strange */
452109998Smarkm				SSL_CTX_add_session(s->ctx,ret);
45355714Skris			}
45455714Skris		if (ret == NULL)
45555714Skris			goto err;
45655714Skris		}
45755714Skris
45855714Skris	/* Now ret is non-NULL, and we own one of its reference counts. */
45955714Skris
460194206Ssimon	if (ret->sid_ctx_length != s->sid_ctx_length
461194206Ssimon	    || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
462194206Ssimon		{
46355714Skris		/* We've found the session named by the client, but we don't
46455714Skris		 * want to use it in this context. */
46555714Skris
46655714Skris#if 0 /* The client cannot always know when a session is not appropriate,
467194206Ssimon       * so we shouldn't generate an error message. */
46855714Skris
469194206Ssimon		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
47055714Skris#endif
471194206Ssimon		goto err; /* treat like cache miss */
47255714Skris		}
473194206Ssimon
474194206Ssimon	if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
475194206Ssimon		{
476194206Ssimon		/* We can't be sure if this session is being used out of
477194206Ssimon		 * context, which is especially important for SSL_VERIFY_PEER.
478194206Ssimon		 * The application should have used SSL[_CTX]_set_session_id_context.
479194206Ssimon		 *
480194206Ssimon		 * For this error case, we generate an error instead of treating
481194206Ssimon		 * the event like a cache miss (otherwise it would be easy for
482194206Ssimon		 * applications to effectively disable the session cache by
483194206Ssimon		 * accident without anyone noticing).
484194206Ssimon		 */
485194206Ssimon
486194206Ssimon		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
487194206Ssimon		fatal = 1;
488194206Ssimon		goto err;
489194206Ssimon		}
49055714Skris
49155714Skris	if (ret->cipher == NULL)
49255714Skris		{
49355714Skris		unsigned char buf[5],*p;
49455714Skris		unsigned long l;
49555714Skris
49655714Skris		p=buf;
49755714Skris		l=ret->cipher_id;
49855714Skris		l2n(l,p);
499205128Ssimon		if ((ret->ssl_version>>8) >= SSL3_VERSION_MAJOR)
50055714Skris			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
50155714Skris		else
50255714Skris			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
50355714Skris		if (ret->cipher == NULL)
50455714Skris			goto err;
50555714Skris		}
50655714Skris
50755714Skris
50855714Skris#if 0 /* This is way too late. */
50955714Skris
51055714Skris	/* If a thread got the session, then 'swaped', and another got
51168651Skris	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
51255714Skris	 * be in trouble.  So I'll increment it now, then double decrement
51355714Skris	 * later - am I speaking rubbish?. */
51455714Skris	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
51555714Skris#endif
51655714Skris
517160814Ssimon	if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
51855714Skris		{
51955714Skris		s->ctx->stats.sess_timeout++;
52055714Skris		/* remove it from the cache */
52155714Skris		SSL_CTX_remove_session(s->ctx,ret);
52255714Skris		goto err;
52355714Skris		}
52455714Skris
52555714Skris	s->ctx->stats.sess_hit++;
52655714Skris
52755714Skris	/* ret->time=time(NULL); */ /* rezero timeout? */
52855714Skris	/* again, just leave the session
52955714Skris	 * if it is the same session, we have just incremented and
53055714Skris	 * then decremented the reference count :-) */
53155714Skris	if (s->session != NULL)
53255714Skris		SSL_SESSION_free(s->session);
53355714Skris	s->session=ret;
53459191Skris	s->verify_result = s->session->verify_result;
53555714Skris	return(1);
53655714Skris
53755714Skris err:
53855714Skris	if (ret != NULL)
53955714Skris		SSL_SESSION_free(ret);
54055714Skris	if (fatal)
54155714Skris		return -1;
54255714Skris	else
54355714Skris		return 0;
54455714Skris	}
54555714Skris
54655714Skrisint SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
54755714Skris	{
54855714Skris	int ret=0;
54955714Skris	SSL_SESSION *s;
55055714Skris
55159191Skris	/* add just 1 reference count for the SSL_CTX's session cache
55259191Skris	 * even though it has two ways of access: each session is in a
55359191Skris	 * doubly linked list and an lhash */
55455714Skris	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
55559191Skris	/* if session c is in already in cache, we take back the increment later */
55655714Skris
55755714Skris	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
55859191Skris	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
55955714Skris
56059191Skris	/* s != NULL iff we already had a session with the given PID.
56159191Skris	 * In this case, s == c should hold (then we did not really modify
56259191Skris	 * ctx->sessions), or we're in trouble. */
56359191Skris	if (s != NULL && s != c)
56459191Skris		{
56559191Skris		/* We *are* in trouble ... */
56659191Skris		SSL_SESSION_list_remove(ctx,s);
56759191Skris		SSL_SESSION_free(s);
56859191Skris		/* ... so pretend the other session did not exist in cache
56959191Skris		 * (we cannot handle two SSL_SESSION structures with identical
57059191Skris		 * session ID in the same cache, which could happen e.g. when
57159191Skris		 * two threads concurrently obtain the same session from an external
57259191Skris		 * cache) */
57359191Skris		s = NULL;
57459191Skris		}
57559191Skris
57659191Skris 	/* Put at the head of the queue unless it is already in the cache */
57755714Skris	if (s == NULL)
57855714Skris		SSL_SESSION_list_add(ctx,c);
57955714Skris
58055714Skris	if (s != NULL)
58155714Skris		{
58259191Skris		/* existing cache entry -- decrement previously incremented reference
58359191Skris		 * count because it already takes into account the cache */
58459191Skris
58559191Skris		SSL_SESSION_free(s); /* s == c */
58655714Skris		ret=0;
58755714Skris		}
58855714Skris	else
58955714Skris		{
59059191Skris		/* new cache entry -- remove old ones if cache has become too large */
59159191Skris
59255714Skris		ret=1;
59355714Skris
59455714Skris		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
59555714Skris			{
59655714Skris			while (SSL_CTX_sess_number(ctx) >
59755714Skris				SSL_CTX_sess_get_cache_size(ctx))
59855714Skris				{
59955714Skris				if (!remove_session_lock(ctx,
60055714Skris					ctx->session_cache_tail, 0))
60155714Skris					break;
60255714Skris				else
60355714Skris					ctx->stats.sess_cache_full++;
60455714Skris				}
60555714Skris			}
60655714Skris		}
60755714Skris	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
60855714Skris	return(ret);
60955714Skris	}
61055714Skris
61155714Skrisint SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
61255714Skris{
61355714Skris	return remove_session_lock(ctx, c, 1);
61455714Skris}
61555714Skris
61655714Skrisstatic int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
61755714Skris	{
61855714Skris	SSL_SESSION *r;
61955714Skris	int ret=0;
62055714Skris
62155714Skris	if ((c != NULL) && (c->session_id_length != 0))
62255714Skris		{
62355714Skris		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
624100928Snectar		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
62555714Skris			{
62655714Skris			ret=1;
627100928Snectar			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
62855714Skris			SSL_SESSION_list_remove(ctx,c);
62955714Skris			}
63055714Skris
63155714Skris		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
63255714Skris
63355714Skris		if (ret)
63455714Skris			{
63555714Skris			r->not_resumable=1;
63655714Skris			if (ctx->remove_session_cb != NULL)
63755714Skris				ctx->remove_session_cb(ctx,r);
63855714Skris			SSL_SESSION_free(r);
63955714Skris			}
64055714Skris		}
64155714Skris	else
64255714Skris		ret=0;
64355714Skris	return(ret);
64455714Skris	}
64555714Skris
64655714Skrisvoid SSL_SESSION_free(SSL_SESSION *ss)
64755714Skris	{
64855714Skris	int i;
64955714Skris
65055714Skris	if(ss == NULL)
65155714Skris	    return;
65255714Skris
65355714Skris	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
65455714Skris#ifdef REF_PRINT
65555714Skris	REF_PRINT("SSL_SESSION",ss);
65655714Skris#endif
65755714Skris	if (i > 0) return;
65855714Skris#ifdef REF_CHECK
65955714Skris	if (i < 0)
66055714Skris		{
66155714Skris		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
66255714Skris		abort(); /* ok */
66355714Skris		}
66455714Skris#endif
66555714Skris
666109998Smarkm	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
66755714Skris
668109998Smarkm	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
669109998Smarkm	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
670109998Smarkm	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
67155714Skris	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
67255714Skris	if (ss->peer != NULL) X509_free(ss->peer);
67355714Skris	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
674194206Ssimon#ifndef OPENSSL_NO_TLSEXT
675194206Ssimon	if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
676194206Ssimon	if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
677194206Ssimon#endif
678109998Smarkm	OPENSSL_cleanse(ss,sizeof(*ss));
67968651Skris	OPENSSL_free(ss);
68055714Skris	}
68155714Skris
68255714Skrisint SSL_set_session(SSL *s, SSL_SESSION *session)
68355714Skris	{
68455714Skris	int ret=0;
68555714Skris	SSL_METHOD *meth;
68655714Skris
68755714Skris	if (session != NULL)
68855714Skris		{
68955714Skris		meth=s->ctx->method->get_ssl_method(session->ssl_version);
69055714Skris		if (meth == NULL)
69155714Skris			meth=s->method->get_ssl_method(session->ssl_version);
69255714Skris		if (meth == NULL)
69355714Skris			{
69455714Skris			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
69555714Skris			return(0);
69655714Skris			}
69755714Skris
69855714Skris		if (meth != s->method)
69955714Skris			{
70055714Skris			if (!SSL_set_ssl_method(s,meth))
70155714Skris				return(0);
70255714Skris			if (s->ctx->session_timeout == 0)
70355714Skris				session->timeout=SSL_get_default_timeout(s);
70455714Skris			else
70555714Skris				session->timeout=s->ctx->session_timeout;
70655714Skris			}
70755714Skris
708109998Smarkm#ifndef OPENSSL_NO_KRB5
709109998Smarkm                if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
710109998Smarkm                    session->krb5_client_princ_len > 0)
711109998Smarkm                {
712167612Ssimon                    s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
713109998Smarkm                    memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
714109998Smarkm                            session->krb5_client_princ_len);
715109998Smarkm                    s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
716109998Smarkm                }
717109998Smarkm#endif /* OPENSSL_NO_KRB5 */
718109998Smarkm
71955714Skris		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
72055714Skris		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
72155714Skris		if (s->session != NULL)
72255714Skris			SSL_SESSION_free(s->session);
72355714Skris		s->session=session;
72472613Skris		s->verify_result = s->session->verify_result;
72555714Skris		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
72655714Skris		ret=1;
72755714Skris		}
72855714Skris	else
72955714Skris		{
73055714Skris		if (s->session != NULL)
73155714Skris			{
73255714Skris			SSL_SESSION_free(s->session);
73355714Skris			s->session=NULL;
73455714Skris			}
73555714Skris
73655714Skris		meth=s->ctx->method;
73755714Skris		if (meth != s->method)
73855714Skris			{
73955714Skris			if (!SSL_set_ssl_method(s,meth))
74055714Skris				return(0);
74155714Skris			}
74255714Skris		ret=1;
74355714Skris		}
74455714Skris	return(ret);
74555714Skris	}
74655714Skris
74755714Skrislong SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
74855714Skris	{
74955714Skris	if (s == NULL) return(0);
75055714Skris	s->timeout=t;
75155714Skris	return(1);
75255714Skris	}
75355714Skris
754160814Ssimonlong SSL_SESSION_get_timeout(const SSL_SESSION *s)
75555714Skris	{
75655714Skris	if (s == NULL) return(0);
75755714Skris	return(s->timeout);
75855714Skris	}
75955714Skris
760160814Ssimonlong SSL_SESSION_get_time(const SSL_SESSION *s)
76155714Skris	{
76255714Skris	if (s == NULL) return(0);
76355714Skris	return(s->time);
76455714Skris	}
76555714Skris
76655714Skrislong SSL_SESSION_set_time(SSL_SESSION *s, long t)
76755714Skris	{
76855714Skris	if (s == NULL) return(0);
76955714Skris	s->time=t;
77055714Skris	return(t);
77155714Skris	}
77255714Skris
77355714Skrislong SSL_CTX_set_timeout(SSL_CTX *s, long t)
77455714Skris	{
77555714Skris	long l;
77655714Skris	if (s == NULL) return(0);
77755714Skris	l=s->session_timeout;
77855714Skris	s->session_timeout=t;
77955714Skris	return(l);
78055714Skris	}
78155714Skris
782160814Ssimonlong SSL_CTX_get_timeout(const SSL_CTX *s)
78355714Skris	{
78455714Skris	if (s == NULL) return(0);
78555714Skris	return(s->session_timeout);
78655714Skris	}
78755714Skris
78855714Skristypedef struct timeout_param_st
78955714Skris	{
79055714Skris	SSL_CTX *ctx;
79155714Skris	long time;
79255714Skris	LHASH *cache;
79355714Skris	} TIMEOUT_PARAM;
79455714Skris
79555714Skrisstatic void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
79655714Skris	{
79755714Skris	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
79855714Skris		{
79955714Skris		/* The reason we don't call SSL_CTX_remove_session() is to
80055714Skris		 * save on locking overhead */
80159191Skris		lh_delete(p->cache,s);
80255714Skris		SSL_SESSION_list_remove(p->ctx,s);
80355714Skris		s->not_resumable=1;
80455714Skris		if (p->ctx->remove_session_cb != NULL)
80555714Skris			p->ctx->remove_session_cb(p->ctx,s);
80655714Skris		SSL_SESSION_free(s);
80755714Skris		}
80855714Skris	}
80955714Skris
810109998Smarkmstatic IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
811109998Smarkm
81255714Skrisvoid SSL_CTX_flush_sessions(SSL_CTX *s, long t)
81355714Skris	{
81455714Skris	unsigned long i;
81555714Skris	TIMEOUT_PARAM tp;
81655714Skris
81755714Skris	tp.ctx=s;
81855714Skris	tp.cache=s->sessions;
81955714Skris	if (tp.cache == NULL) return;
82055714Skris	tp.time=t;
82155714Skris	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
82255714Skris	i=tp.cache->down_load;
82355714Skris	tp.cache->down_load=0;
824109998Smarkm	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
82555714Skris	tp.cache->down_load=i;
82655714Skris	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
82755714Skris	}
82855714Skris
82955714Skrisint ssl_clear_bad_session(SSL *s)
83055714Skris	{
83155714Skris	if (	(s->session != NULL) &&
83255714Skris		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
83355714Skris		!(SSL_in_init(s) || SSL_in_before(s)))
83455714Skris		{
83555714Skris		SSL_CTX_remove_session(s->ctx,s->session);
83655714Skris		return(1);
83755714Skris		}
83855714Skris	else
83955714Skris		return(0);
84055714Skris	}
84155714Skris
84255714Skris/* locked by SSL_CTX in the calling function */
84355714Skrisstatic void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
84455714Skris	{
84555714Skris	if ((s->next == NULL) || (s->prev == NULL)) return;
84655714Skris
84755714Skris	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
84855714Skris		{ /* last element in list */
84955714Skris		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
85055714Skris			{ /* only one element in list */
85155714Skris			ctx->session_cache_head=NULL;
85255714Skris			ctx->session_cache_tail=NULL;
85355714Skris			}
85455714Skris		else
85555714Skris			{
85655714Skris			ctx->session_cache_tail=s->prev;
85755714Skris			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
85855714Skris			}
85955714Skris		}
86055714Skris	else
86155714Skris		{
86255714Skris		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
86355714Skris			{ /* first element in list */
86455714Skris			ctx->session_cache_head=s->next;
86555714Skris			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
86655714Skris			}
86755714Skris		else
86855714Skris			{ /* middle of list */
86955714Skris			s->next->prev=s->prev;
87055714Skris			s->prev->next=s->next;
87155714Skris			}
87255714Skris		}
87355714Skris	s->prev=s->next=NULL;
87455714Skris	}
87555714Skris
87655714Skrisstatic void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
87755714Skris	{
87855714Skris	if ((s->next != NULL) && (s->prev != NULL))
87955714Skris		SSL_SESSION_list_remove(ctx,s);
88055714Skris
88155714Skris	if (ctx->session_cache_head == NULL)
88255714Skris		{
88355714Skris		ctx->session_cache_head=s;
88455714Skris		ctx->session_cache_tail=s;
88555714Skris		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
88655714Skris		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
88755714Skris		}
88855714Skris	else
88955714Skris		{
89055714Skris		s->next=ctx->session_cache_head;
89155714Skris		s->next->prev=s;
89255714Skris		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
89355714Skris		ctx->session_cache_head=s;
89455714Skris		}
89555714Skris	}
89655714Skris
897167612Ssimonvoid SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
898167612Ssimon	int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
899167612Ssimon	{
900167612Ssimon	ctx->new_session_cb=cb;
901167612Ssimon	}
902167612Ssimon
903167612Ssimonint (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
904167612Ssimon	{
905167612Ssimon	return ctx->new_session_cb;
906167612Ssimon	}
907167612Ssimon
908167612Ssimonvoid SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
909167612Ssimon	void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
910167612Ssimon	{
911167612Ssimon	ctx->remove_session_cb=cb;
912167612Ssimon	}
913167612Ssimon
914167612Ssimonvoid (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
915167612Ssimon	{
916167612Ssimon	return ctx->remove_session_cb;
917167612Ssimon	}
918167612Ssimon
919167612Ssimonvoid SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
920167612Ssimon	SSL_SESSION *(*cb)(struct ssl_st *ssl,
921167612Ssimon	         unsigned char *data,int len,int *copy))
922167612Ssimon	{
923167612Ssimon	ctx->get_session_cb=cb;
924167612Ssimon	}
925167612Ssimon
926167612SsimonSSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
927167612Ssimon	         unsigned char *data,int len,int *copy)
928167612Ssimon	{
929167612Ssimon	return ctx->get_session_cb;
930167612Ssimon	}
931167612Ssimon
932167612Ssimonvoid SSL_CTX_set_info_callback(SSL_CTX *ctx,
933167612Ssimon	void (*cb)(const SSL *ssl,int type,int val))
934167612Ssimon	{
935167612Ssimon	ctx->info_callback=cb;
936167612Ssimon	}
937167612Ssimon
938167612Ssimonvoid (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
939167612Ssimon	{
940167612Ssimon	return ctx->info_callback;
941167612Ssimon	}
942167612Ssimon
943167612Ssimonvoid SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
944167612Ssimon	int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
945167612Ssimon	{
946167612Ssimon	ctx->client_cert_cb=cb;
947167612Ssimon	}
948167612Ssimon
949167612Ssimonint (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
950167612Ssimon	{
951167612Ssimon	return ctx->client_cert_cb;
952167612Ssimon	}
953167612Ssimon
954194206Ssimon#ifndef OPENSSL_NO_ENGINE
955194206Ssimonint SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
956194206Ssimon	{
957194206Ssimon	if (!ENGINE_init(e))
958194206Ssimon		{
959194206Ssimon		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
960194206Ssimon		return 0;
961194206Ssimon		}
962194206Ssimon	if(!ENGINE_get_ssl_client_cert_function(e))
963194206Ssimon		{
964194206Ssimon		SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD);
965194206Ssimon		ENGINE_finish(e);
966194206Ssimon		return 0;
967194206Ssimon		}
968194206Ssimon	ctx->client_cert_engine = e;
969194206Ssimon	return 1;
970194206Ssimon	}
971194206Ssimon#endif
972194206Ssimon
973167612Ssimonvoid SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
974167612Ssimon	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
975167612Ssimon	{
976167612Ssimon	ctx->app_gen_cookie_cb=cb;
977167612Ssimon	}
978167612Ssimon
979167612Ssimonvoid SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
980167612Ssimon	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
981167612Ssimon	{
982167612Ssimon	ctx->app_verify_cookie_cb=cb;
983167612Ssimon	}
984167612Ssimon
985