os/ossrv/ssl/libssl/src/ssl_sess.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* ssl/ssl_sess.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include <openssl/lhash.h>
sl@0
    61
#include <openssl/rand.h>
sl@0
    62
#include "ssl_locl.h"
sl@0
    63
sl@0
    64
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
sl@0
    65
static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
sl@0
    66
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
sl@0
    67
sl@0
    68
EXPORT_C SSL_SESSION *SSL_get_session(const SSL *ssl)
sl@0
    69
/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
sl@0
    70
	{
sl@0
    71
	return(ssl->session);
sl@0
    72
	}
sl@0
    73
sl@0
    74
EXPORT_C SSL_SESSION *SSL_get1_session(SSL *ssl)
sl@0
    75
/* variant of SSL_get_session: caller really gets something */
sl@0
    76
	{
sl@0
    77
	SSL_SESSION *sess;
sl@0
    78
	/* Need to lock this all up rather than just use CRYPTO_add so that
sl@0
    79
	 * somebody doesn't free ssl->session between when we check it's
sl@0
    80
	 * non-null and when we up the reference count. */
sl@0
    81
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
sl@0
    82
	sess = ssl->session;
sl@0
    83
	if(sess)
sl@0
    84
		sess->references++;
sl@0
    85
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
sl@0
    86
	return(sess);
sl@0
    87
	}
sl@0
    88
sl@0
    89
EXPORT_C int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
sl@0
    90
	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
sl@0
    91
	{
sl@0
    92
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
sl@0
    93
			new_func, dup_func, free_func);
sl@0
    94
	}
sl@0
    95
sl@0
    96
EXPORT_C int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
sl@0
    97
	{
sl@0
    98
	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
sl@0
    99
	}
sl@0
   100
sl@0
   101
EXPORT_C void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
sl@0
   102
	{
sl@0
   103
	return(CRYPTO_get_ex_data(&s->ex_data,idx));
sl@0
   104
	}
sl@0
   105
sl@0
   106
EXPORT_C SSL_SESSION *SSL_SESSION_new(void)
sl@0
   107
	{
sl@0
   108
	SSL_SESSION *ss;
sl@0
   109
sl@0
   110
	ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
sl@0
   111
	if (ss == NULL)
sl@0
   112
		{
sl@0
   113
		SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
sl@0
   114
		return(0);
sl@0
   115
		}
sl@0
   116
	memset(ss,0,sizeof(SSL_SESSION));
sl@0
   117
sl@0
   118
	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
sl@0
   119
	ss->references=1;
sl@0
   120
	ss->timeout=60*5+4; /* 5 minute timeout by default */
sl@0
   121
	ss->time=(unsigned long)time(NULL);
sl@0
   122
	ss->prev=NULL;
sl@0
   123
	ss->next=NULL;
sl@0
   124
	ss->compress_meth=0;
sl@0
   125
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
sl@0
   126
	return(ss);
sl@0
   127
	}
sl@0
   128
sl@0
   129
EXPORT_C const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
sl@0
   130
	{
sl@0
   131
	if(len)
sl@0
   132
		*len = s->session_id_length;
sl@0
   133
	return s->session_id;
sl@0
   134
	}
sl@0
   135
sl@0
   136
/* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
sl@0
   137
 * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
sl@0
   138
 * until we have no conflict is going to complete in one iteration pretty much
sl@0
   139
 * "most" of the time (btw: understatement). So, if it takes us 10 iterations
sl@0
   140
 * and we still can't avoid a conflict - well that's a reasonable point to call
sl@0
   141
 * it quits. Either the RAND code is broken or someone is trying to open roughly
sl@0
   142
 * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
sl@0
   143
 * store that many sessions is perhaps a more interesting question ... */
sl@0
   144
sl@0
   145
#define MAX_SESS_ID_ATTEMPTS 10
sl@0
   146
static int def_generate_session_id(const SSL *ssl, unsigned char *id,
sl@0
   147
				unsigned int *id_len)
sl@0
   148
{
sl@0
   149
	unsigned int retry = 0;
sl@0
   150
	do
sl@0
   151
		if (RAND_pseudo_bytes(id, *id_len) <= 0)
sl@0
   152
			return 0;
sl@0
   153
	while(SSL_has_matching_session_id(ssl, id, *id_len) &&
sl@0
   154
		(++retry < MAX_SESS_ID_ATTEMPTS));
sl@0
   155
	if(retry < MAX_SESS_ID_ATTEMPTS)
sl@0
   156
		return 1;
sl@0
   157
	/* else - woops a session_id match */
sl@0
   158
	/* XXX We should also check the external cache --
sl@0
   159
	 * but the probability of a collision is negligible, and
sl@0
   160
	 * we could not prevent the concurrent creation of sessions
sl@0
   161
	 * with identical IDs since we currently don't have means
sl@0
   162
	 * to atomically check whether a session ID already exists
sl@0
   163
	 * and make a reservation for it if it does not
sl@0
   164
	 * (this problem applies to the internal cache as well).
sl@0
   165
	 */
sl@0
   166
	return 0;
sl@0
   167
}
sl@0
   168
sl@0
   169
int ssl_get_new_session(SSL *s, int session)
sl@0
   170
	{
sl@0
   171
	/* This gets used by clients and servers. */
sl@0
   172
sl@0
   173
	unsigned int tmp;
sl@0
   174
	SSL_SESSION *ss=NULL;
sl@0
   175
	GEN_SESSION_CB cb = def_generate_session_id;
sl@0
   176
sl@0
   177
	if ((ss=SSL_SESSION_new()) == NULL) return(0);
sl@0
   178
sl@0
   179
	/* If the context has a default timeout, use it */
sl@0
   180
	if (s->ctx->session_timeout == 0)
sl@0
   181
		ss->timeout=SSL_get_default_timeout(s);
sl@0
   182
	else
sl@0
   183
		ss->timeout=s->ctx->session_timeout;
sl@0
   184
sl@0
   185
	if (s->session != NULL)
sl@0
   186
		{
sl@0
   187
		SSL_SESSION_free(s->session);
sl@0
   188
		s->session=NULL;
sl@0
   189
		}
sl@0
   190
sl@0
   191
	if (session)
sl@0
   192
		{
sl@0
   193
		if (s->version == SSL2_VERSION)
sl@0
   194
			{
sl@0
   195
			ss->ssl_version=SSL2_VERSION;
sl@0
   196
			ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
sl@0
   197
			}
sl@0
   198
		else if (s->version == SSL3_VERSION)
sl@0
   199
			{
sl@0
   200
			ss->ssl_version=SSL3_VERSION;
sl@0
   201
			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
sl@0
   202
			}
sl@0
   203
		else if (s->version == TLS1_VERSION)
sl@0
   204
			{
sl@0
   205
			ss->ssl_version=TLS1_VERSION;
sl@0
   206
			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
sl@0
   207
			}
sl@0
   208
		else if (s->version == DTLS1_VERSION)
sl@0
   209
			{
sl@0
   210
			ss->ssl_version=DTLS1_VERSION;
sl@0
   211
			ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
sl@0
   212
			}
sl@0
   213
		else
sl@0
   214
			{
sl@0
   215
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
sl@0
   216
			SSL_SESSION_free(ss);
sl@0
   217
			return(0);
sl@0
   218
			}
sl@0
   219
		/* Choose which callback will set the session ID */
sl@0
   220
		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
sl@0
   221
		if(s->generate_session_id)
sl@0
   222
			cb = s->generate_session_id;
sl@0
   223
		else if(s->ctx->generate_session_id)
sl@0
   224
			cb = s->ctx->generate_session_id;
sl@0
   225
		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
sl@0
   226
		/* Choose a session ID */
sl@0
   227
		tmp = ss->session_id_length;
sl@0
   228
		if(!cb(s, ss->session_id, &tmp))
sl@0
   229
			{
sl@0
   230
			/* The callback failed */
sl@0
   231
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
sl@0
   232
				SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
sl@0
   233
			SSL_SESSION_free(ss);
sl@0
   234
			return(0);
sl@0
   235
			}
sl@0
   236
		/* Don't allow the callback to set the session length to zero.
sl@0
   237
		 * nor set it higher than it was. */
sl@0
   238
		if(!tmp || (tmp > ss->session_id_length))
sl@0
   239
			{
sl@0
   240
			/* The callback set an illegal length */
sl@0
   241
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
sl@0
   242
				SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
sl@0
   243
			SSL_SESSION_free(ss);
sl@0
   244
			return(0);
sl@0
   245
			}
sl@0
   246
		/* If the session length was shrunk and we're SSLv2, pad it */
sl@0
   247
		if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
sl@0
   248
			memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
sl@0
   249
		else
sl@0
   250
			ss->session_id_length = tmp;
sl@0
   251
		/* Finally, check for a conflict */
sl@0
   252
		if(SSL_has_matching_session_id(s, ss->session_id,
sl@0
   253
						ss->session_id_length))
sl@0
   254
			{
sl@0
   255
			SSLerr(SSL_F_SSL_GET_NEW_SESSION,
sl@0
   256
				SSL_R_SSL_SESSION_ID_CONFLICT);
sl@0
   257
			SSL_SESSION_free(ss);
sl@0
   258
			return(0);
sl@0
   259
			}
sl@0
   260
		}
sl@0
   261
	else
sl@0
   262
		{
sl@0
   263
		ss->session_id_length=0;
sl@0
   264
		}
sl@0
   265
sl@0
   266
	if (s->sid_ctx_length > sizeof ss->sid_ctx)
sl@0
   267
		{
sl@0
   268
		SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
sl@0
   269
		SSL_SESSION_free(ss);
sl@0
   270
		return 0;
sl@0
   271
		}
sl@0
   272
	memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
sl@0
   273
	ss->sid_ctx_length=s->sid_ctx_length;
sl@0
   274
	s->session=ss;
sl@0
   275
	ss->ssl_version=s->version;
sl@0
   276
	ss->verify_result = X509_V_OK;
sl@0
   277
sl@0
   278
	return(1);
sl@0
   279
	}
sl@0
   280
sl@0
   281
int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
sl@0
   282
			const unsigned char *limit)
sl@0
   283
	{
sl@0
   284
	/* This is used only by servers. */
sl@0
   285
sl@0
   286
	SSL_SESSION *ret=NULL;
sl@0
   287
	int fatal = 0;
sl@0
   288
  
sl@0
   289
	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
sl@0
   290
		goto err;
sl@0
   291
sl@0
   292
	if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
sl@0
   293
sl@0
   294
		{
sl@0
   295
		SSL_SESSION data;
sl@0
   296
		data.ssl_version=s->version;
sl@0
   297
		data.session_id_length=len;
sl@0
   298
		if (len == 0)
sl@0
   299
			return 0;
sl@0
   300
 		memcpy(data.session_id,session_id,len);
sl@0
   301
		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
sl@0
   302
		ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
sl@0
   303
		if (ret != NULL)
sl@0
   304
		    /* don't allow other threads to steal it: */
sl@0
   305
		    CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   306
		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
sl@0
   307
		}
sl@0
   308
sl@0
   309
	if (ret == NULL)
sl@0
   310
		{
sl@0
   311
		int copy=1;
sl@0
   312
	
sl@0
   313
		s->ctx->stats.sess_miss++;
sl@0
   314
		ret=NULL;
sl@0
   315
		if (s->ctx->get_session_cb != NULL
sl@0
   316
		    && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
sl@0
   317
		       != NULL)
sl@0
   318
			{
sl@0
   319
			s->ctx->stats.sess_cb_hit++;
sl@0
   320
sl@0
   321
			/* Increment reference count now if the session callback
sl@0
   322
			 * asks us to do so (note that if the session structures
sl@0
   323
			 * returned by the callback are shared between threads,
sl@0
   324
			 * it must handle the reference count itself [i.e. copy == 0],
sl@0
   325
			 * or things won't be thread-safe). */
sl@0
   326
			if (copy)
sl@0
   327
				CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   328
sl@0
   329
			/* Add the externally cached session to the internal
sl@0
   330
			 * cache as well if and only if we are supposed to. */
sl@0
   331
			if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
sl@0
   332
				/* The following should not return 1, otherwise,
sl@0
   333
				 * things are very strange */
sl@0
   334
				SSL_CTX_add_session(s->ctx,ret);
sl@0
   335
			}
sl@0
   336
		if (ret == NULL)
sl@0
   337
			goto err;
sl@0
   338
		}
sl@0
   339
sl@0
   340
	/* Now ret is non-NULL, and we own one of its reference counts. */
sl@0
   341
sl@0
   342
	if (ret->sid_ctx_length != s->sid_ctx_length
sl@0
   343
	    || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
sl@0
   344
		{
sl@0
   345
		/* We've found the session named by the client, but we don't
sl@0
   346
		 * want to use it in this context. */
sl@0
   347
sl@0
   348
#if 0 /* The client cannot always know when a session is not appropriate,
sl@0
   349
       * so we shouldn't generate an error message. */
sl@0
   350
sl@0
   351
		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
sl@0
   352
#endif
sl@0
   353
		goto err; /* treat like cache miss */
sl@0
   354
		}
sl@0
   355
	
sl@0
   356
	if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
sl@0
   357
		{
sl@0
   358
		/* We can't be sure if this session is being used out of
sl@0
   359
		 * context, which is especially important for SSL_VERIFY_PEER.
sl@0
   360
		 * The application should have used SSL[_CTX]_set_session_id_context.
sl@0
   361
		 *
sl@0
   362
		 * For this error case, we generate an error instead of treating
sl@0
   363
		 * the event like a cache miss (otherwise it would be easy for
sl@0
   364
		 * applications to effectively disable the session cache by
sl@0
   365
		 * accident without anyone noticing).
sl@0
   366
		 */
sl@0
   367
		
sl@0
   368
		SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
sl@0
   369
		fatal = 1;
sl@0
   370
		goto err;
sl@0
   371
		}
sl@0
   372
sl@0
   373
	if (ret->cipher == NULL)
sl@0
   374
		{
sl@0
   375
		unsigned char buf[5],*p;
sl@0
   376
		unsigned long l;
sl@0
   377
sl@0
   378
		p=buf;
sl@0
   379
		l=ret->cipher_id;
sl@0
   380
		l2n(l,p);
sl@0
   381
		if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
sl@0
   382
			ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
sl@0
   383
		else 
sl@0
   384
			ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
sl@0
   385
		if (ret->cipher == NULL)
sl@0
   386
			goto err;
sl@0
   387
		}
sl@0
   388
sl@0
   389
sl@0
   390
#if 0 /* This is way too late. */
sl@0
   391
sl@0
   392
	/* If a thread got the session, then 'swaped', and another got
sl@0
   393
	 * it and then due to a time-out decided to 'OPENSSL_free' it we could
sl@0
   394
	 * be in trouble.  So I'll increment it now, then double decrement
sl@0
   395
	 * later - am I speaking rubbish?. */
sl@0
   396
	CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   397
#endif
sl@0
   398
sl@0
   399
	if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
sl@0
   400
		{
sl@0
   401
		s->ctx->stats.sess_timeout++;
sl@0
   402
		/* remove it from the cache */
sl@0
   403
		SSL_CTX_remove_session(s->ctx,ret);
sl@0
   404
		goto err;
sl@0
   405
		}
sl@0
   406
sl@0
   407
	s->ctx->stats.sess_hit++;
sl@0
   408
sl@0
   409
	/* ret->time=time(NULL); */ /* rezero timeout? */
sl@0
   410
	/* again, just leave the session 
sl@0
   411
	 * if it is the same session, we have just incremented and
sl@0
   412
	 * then decremented the reference count :-) */
sl@0
   413
	if (s->session != NULL)
sl@0
   414
		SSL_SESSION_free(s->session);
sl@0
   415
	s->session=ret;
sl@0
   416
	s->verify_result = s->session->verify_result;
sl@0
   417
	return(1);
sl@0
   418
sl@0
   419
 err:
sl@0
   420
	if (ret != NULL)
sl@0
   421
		SSL_SESSION_free(ret);
sl@0
   422
	if (fatal)
sl@0
   423
		return -1;
sl@0
   424
	else
sl@0
   425
		return 0;
sl@0
   426
	}
sl@0
   427
sl@0
   428
EXPORT_C int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
sl@0
   429
	{
sl@0
   430
	int ret=0;
sl@0
   431
	SSL_SESSION *s;
sl@0
   432
sl@0
   433
	/* add just 1 reference count for the SSL_CTX's session cache
sl@0
   434
	 * even though it has two ways of access: each session is in a
sl@0
   435
	 * doubly linked list and an lhash */
sl@0
   436
	CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   437
	/* if session c is in already in cache, we take back the increment later */
sl@0
   438
sl@0
   439
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
sl@0
   440
	s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
sl@0
   441
	
sl@0
   442
	/* s != NULL iff we already had a session with the given PID.
sl@0
   443
	 * In this case, s == c should hold (then we did not really modify
sl@0
   444
	 * ctx->sessions), or we're in trouble. */
sl@0
   445
	if (s != NULL && s != c)
sl@0
   446
		{
sl@0
   447
		/* We *are* in trouble ... */
sl@0
   448
		SSL_SESSION_list_remove(ctx,s);
sl@0
   449
		SSL_SESSION_free(s);
sl@0
   450
		/* ... so pretend the other session did not exist in cache
sl@0
   451
		 * (we cannot handle two SSL_SESSION structures with identical
sl@0
   452
		 * session ID in the same cache, which could happen e.g. when
sl@0
   453
		 * two threads concurrently obtain the same session from an external
sl@0
   454
		 * cache) */
sl@0
   455
		s = NULL;
sl@0
   456
		}
sl@0
   457
sl@0
   458
 	/* Put at the head of the queue unless it is already in the cache */
sl@0
   459
	if (s == NULL)
sl@0
   460
		SSL_SESSION_list_add(ctx,c);
sl@0
   461
sl@0
   462
	if (s != NULL)
sl@0
   463
		{
sl@0
   464
		/* existing cache entry -- decrement previously incremented reference
sl@0
   465
		 * count because it already takes into account the cache */
sl@0
   466
sl@0
   467
		SSL_SESSION_free(s); /* s == c */
sl@0
   468
		ret=0;
sl@0
   469
		}
sl@0
   470
	else
sl@0
   471
		{
sl@0
   472
		/* new cache entry -- remove old ones if cache has become too large */
sl@0
   473
		
sl@0
   474
		ret=1;
sl@0
   475
sl@0
   476
		if (SSL_CTX_sess_get_cache_size(ctx) > 0)
sl@0
   477
			{
sl@0
   478
			while (SSL_CTX_sess_number(ctx) >
sl@0
   479
				SSL_CTX_sess_get_cache_size(ctx))
sl@0
   480
				{
sl@0
   481
				if (!remove_session_lock(ctx,
sl@0
   482
					ctx->session_cache_tail, 0))
sl@0
   483
					break;
sl@0
   484
				else
sl@0
   485
					ctx->stats.sess_cache_full++;
sl@0
   486
				}
sl@0
   487
			}
sl@0
   488
		}
sl@0
   489
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
sl@0
   490
	return(ret);
sl@0
   491
	}
sl@0
   492
sl@0
   493
EXPORT_C int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
sl@0
   494
{
sl@0
   495
	return remove_session_lock(ctx, c, 1);
sl@0
   496
}
sl@0
   497
sl@0
   498
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
sl@0
   499
	{
sl@0
   500
	SSL_SESSION *r;
sl@0
   501
	int ret=0;
sl@0
   502
sl@0
   503
	if ((c != NULL) && (c->session_id_length != 0))
sl@0
   504
		{
sl@0
   505
		if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
sl@0
   506
		if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
sl@0
   507
			{
sl@0
   508
			ret=1;
sl@0
   509
			r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
sl@0
   510
			SSL_SESSION_list_remove(ctx,c);
sl@0
   511
			}
sl@0
   512
sl@0
   513
		if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
sl@0
   514
sl@0
   515
		if (ret)
sl@0
   516
			{
sl@0
   517
			r->not_resumable=1;
sl@0
   518
			if (ctx->remove_session_cb != NULL)
sl@0
   519
				ctx->remove_session_cb(ctx,r);
sl@0
   520
			SSL_SESSION_free(r);
sl@0
   521
			}
sl@0
   522
		}
sl@0
   523
	else
sl@0
   524
		ret=0;
sl@0
   525
	return(ret);
sl@0
   526
	}
sl@0
   527
sl@0
   528
EXPORT_C void SSL_SESSION_free(SSL_SESSION *ss)
sl@0
   529
	{
sl@0
   530
	int i;
sl@0
   531
sl@0
   532
	if(ss == NULL)
sl@0
   533
	    return;
sl@0
   534
sl@0
   535
	i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   536
#ifdef REF_PRINT
sl@0
   537
	REF_PRINT("SSL_SESSION",ss);
sl@0
   538
#endif
sl@0
   539
	if (i > 0) return;
sl@0
   540
#ifdef REF_CHECK
sl@0
   541
	if (i < 0)
sl@0
   542
		{
sl@0
   543
		fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
sl@0
   544
		abort(); /* ok */
sl@0
   545
		}
sl@0
   546
#endif
sl@0
   547
sl@0
   548
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
sl@0
   549
sl@0
   550
	OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
sl@0
   551
	OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
sl@0
   552
	OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
sl@0
   553
	if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
sl@0
   554
	if (ss->peer != NULL) X509_free(ss->peer);
sl@0
   555
	if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
sl@0
   556
sl@0
   557
	OPENSSL_cleanse(ss,sizeof(*ss));
sl@0
   558
	OPENSSL_free(ss);
sl@0
   559
	}
sl@0
   560
sl@0
   561
EXPORT_C int SSL_set_session(SSL *s, SSL_SESSION *session)
sl@0
   562
	{
sl@0
   563
	int ret=0;
sl@0
   564
	SSL_METHOD *meth;
sl@0
   565
sl@0
   566
	if (session != NULL)
sl@0
   567
		{
sl@0
   568
		meth=s->ctx->method->get_ssl_method(session->ssl_version);
sl@0
   569
		if (meth == NULL)
sl@0
   570
			meth=s->method->get_ssl_method(session->ssl_version);
sl@0
   571
		if (meth == NULL)
sl@0
   572
			{
sl@0
   573
			SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
sl@0
   574
			return(0);
sl@0
   575
			}
sl@0
   576
sl@0
   577
		if (meth != s->method)
sl@0
   578
			{
sl@0
   579
			if (!SSL_set_ssl_method(s,meth))
sl@0
   580
				return(0);
sl@0
   581
			if (s->ctx->session_timeout == 0)
sl@0
   582
				session->timeout=SSL_get_default_timeout(s);
sl@0
   583
			else
sl@0
   584
				session->timeout=s->ctx->session_timeout;
sl@0
   585
			}
sl@0
   586
sl@0
   587
#ifndef OPENSSL_NO_KRB5
sl@0
   588
                if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
sl@0
   589
                    session->krb5_client_princ_len > 0)
sl@0
   590
                {
sl@0
   591
                    s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
sl@0
   592
                    memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
sl@0
   593
                            session->krb5_client_princ_len);
sl@0
   594
                    s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
sl@0
   595
                }
sl@0
   596
#endif /* OPENSSL_NO_KRB5 */
sl@0
   597
sl@0
   598
		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
sl@0
   599
		CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
sl@0
   600
		if (s->session != NULL)
sl@0
   601
			SSL_SESSION_free(s->session);
sl@0
   602
		s->session=session;
sl@0
   603
		s->verify_result = s->session->verify_result;
sl@0
   604
		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
sl@0
   605
		ret=1;
sl@0
   606
		}
sl@0
   607
	else
sl@0
   608
		{
sl@0
   609
		if (s->session != NULL)
sl@0
   610
			{
sl@0
   611
			SSL_SESSION_free(s->session);
sl@0
   612
			s->session=NULL;
sl@0
   613
			}
sl@0
   614
sl@0
   615
		meth=s->ctx->method;
sl@0
   616
		if (meth != s->method)
sl@0
   617
			{
sl@0
   618
			if (!SSL_set_ssl_method(s,meth))
sl@0
   619
				return(0);
sl@0
   620
			}
sl@0
   621
		ret=1;
sl@0
   622
		}
sl@0
   623
	return(ret);
sl@0
   624
	}
sl@0
   625
sl@0
   626
EXPORT_C long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
sl@0
   627
	{
sl@0
   628
	if (s == NULL) return(0);
sl@0
   629
	s->timeout=t;
sl@0
   630
	return(1);
sl@0
   631
	}
sl@0
   632
sl@0
   633
EXPORT_C long SSL_SESSION_get_timeout(const SSL_SESSION *s)
sl@0
   634
	{
sl@0
   635
	if (s == NULL) return(0);
sl@0
   636
	return(s->timeout);
sl@0
   637
	}
sl@0
   638
sl@0
   639
EXPORT_C long SSL_SESSION_get_time(const SSL_SESSION *s)
sl@0
   640
	{
sl@0
   641
	if (s == NULL) return(0);
sl@0
   642
	return(s->time);
sl@0
   643
	}
sl@0
   644
sl@0
   645
EXPORT_C long SSL_SESSION_set_time(SSL_SESSION *s, long t)
sl@0
   646
	{
sl@0
   647
	if (s == NULL) return(0);
sl@0
   648
	s->time=t;
sl@0
   649
	return(t);
sl@0
   650
	}
sl@0
   651
sl@0
   652
EXPORT_C long SSL_CTX_set_timeout(SSL_CTX *s, long t)
sl@0
   653
	{
sl@0
   654
	long l;
sl@0
   655
	if (s == NULL) return(0);
sl@0
   656
	l=s->session_timeout;
sl@0
   657
	s->session_timeout=t;
sl@0
   658
	return(l);
sl@0
   659
	}
sl@0
   660
sl@0
   661
EXPORT_C long SSL_CTX_get_timeout(const SSL_CTX *s)
sl@0
   662
	{
sl@0
   663
	if (s == NULL) return(0);
sl@0
   664
	return(s->session_timeout);
sl@0
   665
	}
sl@0
   666
sl@0
   667
typedef struct timeout_param_st
sl@0
   668
	{
sl@0
   669
	SSL_CTX *ctx;
sl@0
   670
	long time;
sl@0
   671
	LHASH *cache;
sl@0
   672
	} TIMEOUT_PARAM;
sl@0
   673
sl@0
   674
static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
sl@0
   675
	{
sl@0
   676
	if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
sl@0
   677
		{
sl@0
   678
		/* The reason we don't call SSL_CTX_remove_session() is to
sl@0
   679
		 * save on locking overhead */
sl@0
   680
		lh_delete(p->cache,s);
sl@0
   681
		SSL_SESSION_list_remove(p->ctx,s);
sl@0
   682
		s->not_resumable=1;
sl@0
   683
		if (p->ctx->remove_session_cb != NULL)
sl@0
   684
			p->ctx->remove_session_cb(p->ctx,s);
sl@0
   685
		SSL_SESSION_free(s);
sl@0
   686
		}
sl@0
   687
	}
sl@0
   688
sl@0
   689
static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
sl@0
   690
sl@0
   691
EXPORT_C void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
sl@0
   692
	{
sl@0
   693
	unsigned long i;
sl@0
   694
	TIMEOUT_PARAM tp;
sl@0
   695
sl@0
   696
	tp.ctx=s;
sl@0
   697
	tp.cache=s->sessions;
sl@0
   698
	if (tp.cache == NULL) return;
sl@0
   699
	tp.time=t;
sl@0
   700
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
sl@0
   701
	i=tp.cache->down_load;
sl@0
   702
	tp.cache->down_load=0;
sl@0
   703
	lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
sl@0
   704
	tp.cache->down_load=i;
sl@0
   705
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
sl@0
   706
	}
sl@0
   707
sl@0
   708
int ssl_clear_bad_session(SSL *s)
sl@0
   709
	{
sl@0
   710
	if (	(s->session != NULL) &&
sl@0
   711
		!(s->shutdown & SSL_SENT_SHUTDOWN) &&
sl@0
   712
		!(SSL_in_init(s) || SSL_in_before(s)))
sl@0
   713
		{
sl@0
   714
		SSL_CTX_remove_session(s->ctx,s->session);
sl@0
   715
		return(1);
sl@0
   716
		}
sl@0
   717
	else
sl@0
   718
		return(0);
sl@0
   719
	}
sl@0
   720
sl@0
   721
/* locked by SSL_CTX in the calling function */
sl@0
   722
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
sl@0
   723
	{
sl@0
   724
	if ((s->next == NULL) || (s->prev == NULL)) return;
sl@0
   725
sl@0
   726
	if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
sl@0
   727
		{ /* last element in list */
sl@0
   728
		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
sl@0
   729
			{ /* only one element in list */
sl@0
   730
			ctx->session_cache_head=NULL;
sl@0
   731
			ctx->session_cache_tail=NULL;
sl@0
   732
			}
sl@0
   733
		else
sl@0
   734
			{
sl@0
   735
			ctx->session_cache_tail=s->prev;
sl@0
   736
			s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
sl@0
   737
			}
sl@0
   738
		}
sl@0
   739
	else
sl@0
   740
		{
sl@0
   741
		if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
sl@0
   742
			{ /* first element in list */
sl@0
   743
			ctx->session_cache_head=s->next;
sl@0
   744
			s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
sl@0
   745
			}
sl@0
   746
		else
sl@0
   747
			{ /* middle of list */
sl@0
   748
			s->next->prev=s->prev;
sl@0
   749
			s->prev->next=s->next;
sl@0
   750
			}
sl@0
   751
		}
sl@0
   752
	s->prev=s->next=NULL;
sl@0
   753
	}
sl@0
   754
sl@0
   755
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
sl@0
   756
	{
sl@0
   757
	if ((s->next != NULL) && (s->prev != NULL))
sl@0
   758
		SSL_SESSION_list_remove(ctx,s);
sl@0
   759
sl@0
   760
	if (ctx->session_cache_head == NULL)
sl@0
   761
		{
sl@0
   762
		ctx->session_cache_head=s;
sl@0
   763
		ctx->session_cache_tail=s;
sl@0
   764
		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
sl@0
   765
		s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
sl@0
   766
		}
sl@0
   767
	else
sl@0
   768
		{
sl@0
   769
		s->next=ctx->session_cache_head;
sl@0
   770
		s->next->prev=s;
sl@0
   771
		s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
sl@0
   772
		ctx->session_cache_head=s;
sl@0
   773
		}
sl@0
   774
	}
sl@0
   775
sl@0
   776
EXPORT_C void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
sl@0
   777
	int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
sl@0
   778
	{
sl@0
   779
	ctx->new_session_cb=cb;
sl@0
   780
	}
sl@0
   781
sl@0
   782
EXPORT_C int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
sl@0
   783
	{
sl@0
   784
	return ctx->new_session_cb;
sl@0
   785
	}
sl@0
   786
sl@0
   787
EXPORT_C void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
sl@0
   788
	void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
sl@0
   789
	{
sl@0
   790
	ctx->remove_session_cb=cb;
sl@0
   791
	}
sl@0
   792
sl@0
   793
EXPORT_C void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
sl@0
   794
	{
sl@0
   795
	return ctx->remove_session_cb;
sl@0
   796
	}
sl@0
   797
sl@0
   798
EXPORT_C void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
sl@0
   799
	SSL_SESSION *(*cb)(struct ssl_st *ssl,
sl@0
   800
	         unsigned char *data,int len,int *copy))
sl@0
   801
	{
sl@0
   802
	ctx->get_session_cb=cb;
sl@0
   803
	}
sl@0
   804
sl@0
   805
EXPORT_C SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
sl@0
   806
	         unsigned char *data,int len,int *copy)
sl@0
   807
	{
sl@0
   808
	return ctx->get_session_cb;
sl@0
   809
	}
sl@0
   810
sl@0
   811
EXPORT_C void SSL_CTX_set_info_callback(SSL_CTX *ctx, 
sl@0
   812
	void (*cb)(const SSL *ssl,int type,int val))
sl@0
   813
	{
sl@0
   814
	ctx->info_callback=cb;
sl@0
   815
	}
sl@0
   816
sl@0
   817
EXPORT_C void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
sl@0
   818
	{
sl@0
   819
	return ctx->info_callback;
sl@0
   820
	}
sl@0
   821
sl@0
   822
EXPORT_C void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
sl@0
   823
	int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
sl@0
   824
	{
sl@0
   825
	ctx->client_cert_cb=cb;
sl@0
   826
	}
sl@0
   827
sl@0
   828
EXPORT_C int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
sl@0
   829
	{
sl@0
   830
	return ctx->client_cert_cb;
sl@0
   831
	}
sl@0
   832
sl@0
   833
EXPORT_C void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
sl@0
   834
	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
sl@0
   835
	{
sl@0
   836
	ctx->app_gen_cookie_cb=cb;
sl@0
   837
	}
sl@0
   838
sl@0
   839
EXPORT_C void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
sl@0
   840
	int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
sl@0
   841
	{
sl@0
   842
	ctx->app_verify_cookie_cb=cb;
sl@0
   843
	}
sl@0
   844