174664626SKris Kennaway /* ssl/ssl_sess.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 5874664626SKris Kennaway 5974664626SKris Kennaway #include <stdio.h> 6074664626SKris Kennaway #include <openssl/lhash.h> 6174664626SKris Kennaway #include <openssl/rand.h> 6274664626SKris Kennaway #include "ssl_locl.h" 634f20a5a2SJacques Vidrine #include "cryptlib.h" 6474664626SKris Kennaway 6574664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 6674664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s); 6774664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 6874664626SKris Kennaway static int ssl_session_num=0; 69f579bf8eSKris Kennaway static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ssl_session_meth=NULL; 7074664626SKris Kennaway 7174664626SKris Kennaway SSL_SESSION *SSL_get_session(SSL *ssl) 72f579bf8eSKris Kennaway /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 7374664626SKris Kennaway { 7474664626SKris Kennaway return(ssl->session); 7574664626SKris Kennaway } 7674664626SKris Kennaway 77f579bf8eSKris Kennaway SSL_SESSION *SSL_get1_session(SSL *ssl) 78f579bf8eSKris Kennaway /* variant of SSL_get_session: caller really gets something */ 79f579bf8eSKris Kennaway { 80f579bf8eSKris Kennaway SSL_SESSION *sess; 81f579bf8eSKris Kennaway /* Need to lock this all up rather than just use CRYPTO_add so that 82f579bf8eSKris Kennaway * somebody doesn't free ssl->session between when we check it's 83f579bf8eSKris Kennaway * non-null and when we up the reference count. */ 84f579bf8eSKris Kennaway CRYPTO_r_lock(CRYPTO_LOCK_SSL_SESSION); 85f579bf8eSKris Kennaway sess = ssl->session; 86f579bf8eSKris Kennaway if(sess) 87f579bf8eSKris Kennaway sess->references++; 88f579bf8eSKris Kennaway CRYPTO_r_unlock(CRYPTO_LOCK_SSL_SESSION); 89f579bf8eSKris Kennaway return(sess); 90f579bf8eSKris Kennaway } 91f579bf8eSKris Kennaway 92f579bf8eSKris Kennaway int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 93f579bf8eSKris Kennaway CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 9474664626SKris Kennaway { 9574664626SKris Kennaway ssl_session_num++; 9674664626SKris Kennaway return(CRYPTO_get_ex_new_index(ssl_session_num-1, 9774664626SKris Kennaway &ssl_session_meth, 9874664626SKris Kennaway argl,argp,new_func,dup_func,free_func)); 9974664626SKris Kennaway } 10074664626SKris Kennaway 10174664626SKris Kennaway int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) 10274664626SKris Kennaway { 10374664626SKris Kennaway return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); 10474664626SKris Kennaway } 10574664626SKris Kennaway 10674664626SKris Kennaway void *SSL_SESSION_get_ex_data(SSL_SESSION *s, int idx) 10774664626SKris Kennaway { 10874664626SKris Kennaway return(CRYPTO_get_ex_data(&s->ex_data,idx)); 10974664626SKris Kennaway } 11074664626SKris Kennaway 11174664626SKris Kennaway SSL_SESSION *SSL_SESSION_new(void) 11274664626SKris Kennaway { 11374664626SKris Kennaway SSL_SESSION *ss; 11474664626SKris Kennaway 115ddd58736SKris Kennaway ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); 11674664626SKris Kennaway if (ss == NULL) 11774664626SKris Kennaway { 11874664626SKris Kennaway SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE); 11974664626SKris Kennaway return(0); 12074664626SKris Kennaway } 12174664626SKris Kennaway memset(ss,0,sizeof(SSL_SESSION)); 12274664626SKris Kennaway 123f579bf8eSKris Kennaway ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 12474664626SKris Kennaway ss->references=1; 12574664626SKris Kennaway ss->timeout=60*5+4; /* 5 minute timeout by default */ 12674664626SKris Kennaway ss->time=time(NULL); 12774664626SKris Kennaway ss->prev=NULL; 12874664626SKris Kennaway ss->next=NULL; 12974664626SKris Kennaway ss->compress_meth=0; 130f579bf8eSKris Kennaway CRYPTO_new_ex_data(ssl_session_meth,ss,&ss->ex_data); 13174664626SKris Kennaway return(ss); 13274664626SKris Kennaway } 13374664626SKris Kennaway 13474664626SKris Kennaway int ssl_get_new_session(SSL *s, int session) 13574664626SKris Kennaway { 13674664626SKris Kennaway /* This gets used by clients and servers. */ 13774664626SKris Kennaway 13874664626SKris Kennaway SSL_SESSION *ss=NULL; 13974664626SKris Kennaway 14074664626SKris Kennaway if ((ss=SSL_SESSION_new()) == NULL) return(0); 14174664626SKris Kennaway 14274664626SKris Kennaway /* If the context has a default timeout, use it */ 14374664626SKris Kennaway if (s->ctx->session_timeout == 0) 14474664626SKris Kennaway ss->timeout=SSL_get_default_timeout(s); 14574664626SKris Kennaway else 14674664626SKris Kennaway ss->timeout=s->ctx->session_timeout; 14774664626SKris Kennaway 14874664626SKris Kennaway if (s->session != NULL) 14974664626SKris Kennaway { 15074664626SKris Kennaway SSL_SESSION_free(s->session); 15174664626SKris Kennaway s->session=NULL; 15274664626SKris Kennaway } 15374664626SKris Kennaway 15474664626SKris Kennaway if (session) 15574664626SKris Kennaway { 15674664626SKris Kennaway if (s->version == SSL2_VERSION) 15774664626SKris Kennaway { 15874664626SKris Kennaway ss->ssl_version=SSL2_VERSION; 15974664626SKris Kennaway ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH; 16074664626SKris Kennaway } 16174664626SKris Kennaway else if (s->version == SSL3_VERSION) 16274664626SKris Kennaway { 16374664626SKris Kennaway ss->ssl_version=SSL3_VERSION; 16474664626SKris Kennaway ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH; 16574664626SKris Kennaway } 16674664626SKris Kennaway else if (s->version == TLS1_VERSION) 16774664626SKris Kennaway { 16874664626SKris Kennaway ss->ssl_version=TLS1_VERSION; 16974664626SKris Kennaway ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH; 17074664626SKris Kennaway } 17174664626SKris Kennaway else 17274664626SKris Kennaway { 17374664626SKris Kennaway SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION); 17474664626SKris Kennaway SSL_SESSION_free(ss); 17574664626SKris Kennaway return(0); 17674664626SKris Kennaway } 17774664626SKris Kennaway 17874664626SKris Kennaway for (;;) 17974664626SKris Kennaway { 18074664626SKris Kennaway SSL_SESSION *r; 18174664626SKris Kennaway 182f579bf8eSKris Kennaway RAND_pseudo_bytes(ss->session_id,ss->session_id_length); 18374664626SKris Kennaway CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 184f579bf8eSKris Kennaway r=(SSL_SESSION *)lh_retrieve(s->ctx->sessions, ss); 18574664626SKris Kennaway CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 18674664626SKris Kennaway if (r == NULL) break; 18774664626SKris Kennaway /* else - woops a session_id match */ 188f579bf8eSKris Kennaway /* XXX We should also check the external cache -- 189f579bf8eSKris Kennaway * but the probability of a collision is negligible, and 190f579bf8eSKris Kennaway * we could not prevent the concurrent creation of sessions 191f579bf8eSKris Kennaway * with identical IDs since we currently don't have means 192f579bf8eSKris Kennaway * to atomically check whether a session ID already exists 193f579bf8eSKris Kennaway * and make a reservation for it if it does not 194f579bf8eSKris Kennaway * (this problem applies to the internal cache as well). 195f579bf8eSKris Kennaway */ 19674664626SKris Kennaway } 19774664626SKris Kennaway } 19874664626SKris Kennaway else 19974664626SKris Kennaway { 20074664626SKris Kennaway ss->session_id_length=0; 20174664626SKris Kennaway } 20274664626SKris Kennaway 20348454956SJacques Vidrine if (s->sid_ctx_length > sizeof ss->sid_ctx) 20448454956SJacques Vidrine { 20548454956SJacques Vidrine SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_INTERNAL_ERROR); 20648454956SJacques Vidrine SSL_SESSION_free(ss); 20748454956SJacques Vidrine return 0; 20848454956SJacques Vidrine } 20974664626SKris Kennaway memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length); 21074664626SKris Kennaway ss->sid_ctx_length=s->sid_ctx_length; 21174664626SKris Kennaway s->session=ss; 21274664626SKris Kennaway ss->ssl_version=s->version; 213f579bf8eSKris Kennaway ss->verify_result = X509_V_OK; 21474664626SKris Kennaway 21574664626SKris Kennaway return(1); 21674664626SKris Kennaway } 21774664626SKris Kennaway 21874664626SKris Kennaway int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len) 21974664626SKris Kennaway { 22074664626SKris Kennaway /* This is used only by servers. */ 22174664626SKris Kennaway 22274664626SKris Kennaway SSL_SESSION *ret=NULL,data; 22374664626SKris Kennaway int fatal = 0; 22474664626SKris Kennaway 22574664626SKris Kennaway data.ssl_version=s->version; 22674664626SKris Kennaway data.session_id_length=len; 22774664626SKris Kennaway if (len > SSL_MAX_SSL_SESSION_ID_LENGTH) 22874664626SKris Kennaway goto err; 22974664626SKris Kennaway memcpy(data.session_id,session_id,len); 23074664626SKris Kennaway 23174664626SKris Kennaway if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) 23274664626SKris Kennaway { 23374664626SKris Kennaway CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 234f579bf8eSKris Kennaway ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data); 23574664626SKris Kennaway if (ret != NULL) 23674664626SKris Kennaway /* don't allow other threads to steal it: */ 23774664626SKris Kennaway CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); 23874664626SKris Kennaway CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 23974664626SKris Kennaway } 24074664626SKris Kennaway 24174664626SKris Kennaway if (ret == NULL) 24274664626SKris Kennaway { 24374664626SKris Kennaway int copy=1; 24474664626SKris Kennaway 24574664626SKris Kennaway s->ctx->stats.sess_miss++; 24674664626SKris Kennaway ret=NULL; 24774664626SKris Kennaway if (s->ctx->get_session_cb != NULL 24874664626SKris Kennaway && (ret=s->ctx->get_session_cb(s,session_id,len,©)) 24974664626SKris Kennaway != NULL) 25074664626SKris Kennaway { 25174664626SKris Kennaway s->ctx->stats.sess_cb_hit++; 25274664626SKris Kennaway 25374664626SKris Kennaway /* Increment reference count now if the session callback 25474664626SKris Kennaway * asks us to do so (note that if the session structures 25574664626SKris Kennaway * returned by the callback are shared between threads, 25674664626SKris Kennaway * it must handle the reference count itself [i.e. copy == 0], 25774664626SKris Kennaway * or things won't be thread-safe). */ 25874664626SKris Kennaway if (copy) 25974664626SKris Kennaway CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); 26074664626SKris Kennaway 26174664626SKris Kennaway /* The following should not return 1, otherwise, 26274664626SKris Kennaway * things are very strange */ 26374664626SKris Kennaway SSL_CTX_add_session(s->ctx,ret); 26474664626SKris Kennaway } 26574664626SKris Kennaway if (ret == NULL) 26674664626SKris Kennaway goto err; 26774664626SKris Kennaway } 26874664626SKris Kennaway 26974664626SKris Kennaway /* Now ret is non-NULL, and we own one of its reference counts. */ 27074664626SKris Kennaway 27174664626SKris Kennaway if((s->verify_mode&SSL_VERIFY_PEER) 27274664626SKris Kennaway && (!s->sid_ctx_length || ret->sid_ctx_length != s->sid_ctx_length 27374664626SKris Kennaway || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))) 27474664626SKris Kennaway { 27574664626SKris Kennaway /* We've found the session named by the client, but we don't 27674664626SKris Kennaway * want to use it in this context. */ 27774664626SKris Kennaway 27874664626SKris Kennaway if (s->sid_ctx_length == 0) 27974664626SKris Kennaway { 28074664626SKris Kennaway /* application should have used SSL[_CTX]_set_session_id_context 28174664626SKris Kennaway * -- we could tolerate this and just pretend we never heard 28274664626SKris Kennaway * of this session, but then applications could effectively 28374664626SKris Kennaway * disable the session cache by accident without anyone noticing */ 28474664626SKris Kennaway 28574664626SKris Kennaway SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 28674664626SKris Kennaway fatal = 1; 28774664626SKris Kennaway goto err; 28874664626SKris Kennaway } 28974664626SKris Kennaway else 29074664626SKris Kennaway { 29174664626SKris Kennaway #if 0 /* The client cannot always know when a session is not appropriate, 29274664626SKris Kennaway * so we shouldn't generate an error message. */ 29374664626SKris Kennaway 29474664626SKris Kennaway SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 29574664626SKris Kennaway #endif 29674664626SKris Kennaway goto err; /* treat like cache miss */ 29774664626SKris Kennaway } 29874664626SKris Kennaway } 29974664626SKris Kennaway 30074664626SKris Kennaway if (ret->cipher == NULL) 30174664626SKris Kennaway { 30274664626SKris Kennaway unsigned char buf[5],*p; 30374664626SKris Kennaway unsigned long l; 30474664626SKris Kennaway 30574664626SKris Kennaway p=buf; 30674664626SKris Kennaway l=ret->cipher_id; 30774664626SKris Kennaway l2n(l,p); 30874664626SKris Kennaway if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR) 30974664626SKris Kennaway ret->cipher=ssl_get_cipher_by_char(s,&(buf[2])); 31074664626SKris Kennaway else 31174664626SKris Kennaway ret->cipher=ssl_get_cipher_by_char(s,&(buf[1])); 31274664626SKris Kennaway if (ret->cipher == NULL) 31374664626SKris Kennaway goto err; 31474664626SKris Kennaway } 31574664626SKris Kennaway 31674664626SKris Kennaway 31774664626SKris Kennaway #if 0 /* This is way too late. */ 31874664626SKris Kennaway 31974664626SKris Kennaway /* If a thread got the session, then 'swaped', and another got 320ddd58736SKris Kennaway * it and then due to a time-out decided to 'OPENSSL_free' it we could 32174664626SKris Kennaway * be in trouble. So I'll increment it now, then double decrement 32274664626SKris Kennaway * later - am I speaking rubbish?. */ 32374664626SKris Kennaway CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); 32474664626SKris Kennaway #endif 32574664626SKris Kennaway 32674664626SKris Kennaway if ((long)(ret->time+ret->timeout) < (long)time(NULL)) /* timeout */ 32774664626SKris Kennaway { 32874664626SKris Kennaway s->ctx->stats.sess_timeout++; 32974664626SKris Kennaway /* remove it from the cache */ 33074664626SKris Kennaway SSL_CTX_remove_session(s->ctx,ret); 33174664626SKris Kennaway goto err; 33274664626SKris Kennaway } 33374664626SKris Kennaway 33474664626SKris Kennaway s->ctx->stats.sess_hit++; 33574664626SKris Kennaway 33674664626SKris Kennaway /* ret->time=time(NULL); */ /* rezero timeout? */ 33774664626SKris Kennaway /* again, just leave the session 33874664626SKris Kennaway * if it is the same session, we have just incremented and 33974664626SKris Kennaway * then decremented the reference count :-) */ 34074664626SKris Kennaway if (s->session != NULL) 34174664626SKris Kennaway SSL_SESSION_free(s->session); 34274664626SKris Kennaway s->session=ret; 343f579bf8eSKris Kennaway s->verify_result = s->session->verify_result; 34474664626SKris Kennaway return(1); 34574664626SKris Kennaway 34674664626SKris Kennaway err: 34774664626SKris Kennaway if (ret != NULL) 34874664626SKris Kennaway SSL_SESSION_free(ret); 34974664626SKris Kennaway if (fatal) 35074664626SKris Kennaway return -1; 35174664626SKris Kennaway else 35274664626SKris Kennaway return 0; 35374664626SKris Kennaway } 35474664626SKris Kennaway 35574664626SKris Kennaway int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) 35674664626SKris Kennaway { 35774664626SKris Kennaway int ret=0; 35874664626SKris Kennaway SSL_SESSION *s; 35974664626SKris Kennaway 360f579bf8eSKris Kennaway /* add just 1 reference count for the SSL_CTX's session cache 361f579bf8eSKris Kennaway * even though it has two ways of access: each session is in a 362f579bf8eSKris Kennaway * doubly linked list and an lhash */ 36374664626SKris Kennaway CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION); 364f579bf8eSKris Kennaway /* if session c is in already in cache, we take back the increment later */ 36574664626SKris Kennaway 36674664626SKris Kennaway CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 367f579bf8eSKris Kennaway s=(SSL_SESSION *)lh_insert(ctx->sessions,c); 36874664626SKris Kennaway 369f579bf8eSKris Kennaway /* s != NULL iff we already had a session with the given PID. 370f579bf8eSKris Kennaway * In this case, s == c should hold (then we did not really modify 371f579bf8eSKris Kennaway * ctx->sessions), or we're in trouble. */ 372f579bf8eSKris Kennaway if (s != NULL && s != c) 373f579bf8eSKris Kennaway { 374f579bf8eSKris Kennaway /* We *are* in trouble ... */ 375f579bf8eSKris Kennaway SSL_SESSION_list_remove(ctx,s); 376f579bf8eSKris Kennaway SSL_SESSION_free(s); 377f579bf8eSKris Kennaway /* ... so pretend the other session did not exist in cache 378f579bf8eSKris Kennaway * (we cannot handle two SSL_SESSION structures with identical 379f579bf8eSKris Kennaway * session ID in the same cache, which could happen e.g. when 380f579bf8eSKris Kennaway * two threads concurrently obtain the same session from an external 381f579bf8eSKris Kennaway * cache) */ 382f579bf8eSKris Kennaway s = NULL; 383f579bf8eSKris Kennaway } 384f579bf8eSKris Kennaway 385f579bf8eSKris Kennaway /* Put at the head of the queue unless it is already in the cache */ 38674664626SKris Kennaway if (s == NULL) 38774664626SKris Kennaway SSL_SESSION_list_add(ctx,c); 38874664626SKris Kennaway 38974664626SKris Kennaway if (s != NULL) 39074664626SKris Kennaway { 391f579bf8eSKris Kennaway /* existing cache entry -- decrement previously incremented reference 392f579bf8eSKris Kennaway * count because it already takes into account the cache */ 393f579bf8eSKris Kennaway 394f579bf8eSKris Kennaway SSL_SESSION_free(s); /* s == c */ 39574664626SKris Kennaway ret=0; 39674664626SKris Kennaway } 39774664626SKris Kennaway else 39874664626SKris Kennaway { 399f579bf8eSKris Kennaway /* new cache entry -- remove old ones if cache has become too large */ 400f579bf8eSKris Kennaway 40174664626SKris Kennaway ret=1; 40274664626SKris Kennaway 40374664626SKris Kennaway if (SSL_CTX_sess_get_cache_size(ctx) > 0) 40474664626SKris Kennaway { 40574664626SKris Kennaway while (SSL_CTX_sess_number(ctx) > 40674664626SKris Kennaway SSL_CTX_sess_get_cache_size(ctx)) 40774664626SKris Kennaway { 40874664626SKris Kennaway if (!remove_session_lock(ctx, 40974664626SKris Kennaway ctx->session_cache_tail, 0)) 41074664626SKris Kennaway break; 41174664626SKris Kennaway else 41274664626SKris Kennaway ctx->stats.sess_cache_full++; 41374664626SKris Kennaway } 41474664626SKris Kennaway } 41574664626SKris Kennaway } 41674664626SKris Kennaway CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 41774664626SKris Kennaway return(ret); 41874664626SKris Kennaway } 41974664626SKris Kennaway 42074664626SKris Kennaway int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) 42174664626SKris Kennaway { 42274664626SKris Kennaway return remove_session_lock(ctx, c, 1); 42374664626SKris Kennaway } 42474664626SKris Kennaway 42574664626SKris Kennaway static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) 42674664626SKris Kennaway { 42774664626SKris Kennaway SSL_SESSION *r; 42874664626SKris Kennaway int ret=0; 42974664626SKris Kennaway 43074664626SKris Kennaway if ((c != NULL) && (c->session_id_length != 0)) 43174664626SKris Kennaway { 43274664626SKris Kennaway if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 433c1803d78SJacques Vidrine if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c) 43474664626SKris Kennaway { 43574664626SKris Kennaway ret=1; 436c1803d78SJacques Vidrine r=(SSL_SESSION *)lh_delete(ctx->sessions,c); 43774664626SKris Kennaway SSL_SESSION_list_remove(ctx,c); 43874664626SKris Kennaway } 43974664626SKris Kennaway 44074664626SKris Kennaway if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 44174664626SKris Kennaway 44274664626SKris Kennaway if (ret) 44374664626SKris Kennaway { 44474664626SKris Kennaway r->not_resumable=1; 44574664626SKris Kennaway if (ctx->remove_session_cb != NULL) 44674664626SKris Kennaway ctx->remove_session_cb(ctx,r); 44774664626SKris Kennaway SSL_SESSION_free(r); 44874664626SKris Kennaway } 44974664626SKris Kennaway } 45074664626SKris Kennaway else 45174664626SKris Kennaway ret=0; 45274664626SKris Kennaway return(ret); 45374664626SKris Kennaway } 45474664626SKris Kennaway 45574664626SKris Kennaway void SSL_SESSION_free(SSL_SESSION *ss) 45674664626SKris Kennaway { 45774664626SKris Kennaway int i; 45874664626SKris Kennaway 45974664626SKris Kennaway if(ss == NULL) 46074664626SKris Kennaway return; 46174664626SKris Kennaway 46274664626SKris Kennaway i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION); 46374664626SKris Kennaway #ifdef REF_PRINT 46474664626SKris Kennaway REF_PRINT("SSL_SESSION",ss); 46574664626SKris Kennaway #endif 46674664626SKris Kennaway if (i > 0) return; 46774664626SKris Kennaway #ifdef REF_CHECK 46874664626SKris Kennaway if (i < 0) 46974664626SKris Kennaway { 47074664626SKris Kennaway fprintf(stderr,"SSL_SESSION_free, bad reference count\n"); 47174664626SKris Kennaway abort(); /* ok */ 47274664626SKris Kennaway } 47374664626SKris Kennaway #endif 47474664626SKris Kennaway 475f579bf8eSKris Kennaway CRYPTO_free_ex_data(ssl_session_meth,ss,&ss->ex_data); 47674664626SKris Kennaway 47774664626SKris Kennaway memset(ss->key_arg,0,SSL_MAX_KEY_ARG_LENGTH); 47874664626SKris Kennaway memset(ss->master_key,0,SSL_MAX_MASTER_KEY_LENGTH); 47974664626SKris Kennaway memset(ss->session_id,0,SSL_MAX_SSL_SESSION_ID_LENGTH); 48074664626SKris Kennaway if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert); 48174664626SKris Kennaway if (ss->peer != NULL) X509_free(ss->peer); 48274664626SKris Kennaway if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers); 48374664626SKris Kennaway memset(ss,0,sizeof(*ss)); 484ddd58736SKris Kennaway OPENSSL_free(ss); 48574664626SKris Kennaway } 48674664626SKris Kennaway 48774664626SKris Kennaway int SSL_set_session(SSL *s, SSL_SESSION *session) 48874664626SKris Kennaway { 48974664626SKris Kennaway int ret=0; 49074664626SKris Kennaway SSL_METHOD *meth; 49174664626SKris Kennaway 49274664626SKris Kennaway if (session != NULL) 49374664626SKris Kennaway { 49474664626SKris Kennaway meth=s->ctx->method->get_ssl_method(session->ssl_version); 49574664626SKris Kennaway if (meth == NULL) 49674664626SKris Kennaway meth=s->method->get_ssl_method(session->ssl_version); 49774664626SKris Kennaway if (meth == NULL) 49874664626SKris Kennaway { 49974664626SKris Kennaway SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD); 50074664626SKris Kennaway return(0); 50174664626SKris Kennaway } 50274664626SKris Kennaway 50374664626SKris Kennaway if (meth != s->method) 50474664626SKris Kennaway { 50574664626SKris Kennaway if (!SSL_set_ssl_method(s,meth)) 50674664626SKris Kennaway return(0); 50774664626SKris Kennaway if (s->ctx->session_timeout == 0) 50874664626SKris Kennaway session->timeout=SSL_get_default_timeout(s); 50974664626SKris Kennaway else 51074664626SKris Kennaway session->timeout=s->ctx->session_timeout; 51174664626SKris Kennaway } 51274664626SKris Kennaway 51374664626SKris Kennaway /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/ 51474664626SKris Kennaway CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION); 51574664626SKris Kennaway if (s->session != NULL) 51674664626SKris Kennaway SSL_SESSION_free(s->session); 51774664626SKris Kennaway s->session=session; 518de7cdddaSKris Kennaway s->verify_result = s->session->verify_result; 51974664626SKris Kennaway /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/ 52074664626SKris Kennaway ret=1; 52174664626SKris Kennaway } 52274664626SKris Kennaway else 52374664626SKris Kennaway { 52474664626SKris Kennaway if (s->session != NULL) 52574664626SKris Kennaway { 52674664626SKris Kennaway SSL_SESSION_free(s->session); 52774664626SKris Kennaway s->session=NULL; 52874664626SKris Kennaway } 52974664626SKris Kennaway 53074664626SKris Kennaway meth=s->ctx->method; 53174664626SKris Kennaway if (meth != s->method) 53274664626SKris Kennaway { 53374664626SKris Kennaway if (!SSL_set_ssl_method(s,meth)) 53474664626SKris Kennaway return(0); 53574664626SKris Kennaway } 53674664626SKris Kennaway ret=1; 53774664626SKris Kennaway } 53874664626SKris Kennaway return(ret); 53974664626SKris Kennaway } 54074664626SKris Kennaway 54174664626SKris Kennaway long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) 54274664626SKris Kennaway { 54374664626SKris Kennaway if (s == NULL) return(0); 54474664626SKris Kennaway s->timeout=t; 54574664626SKris Kennaway return(1); 54674664626SKris Kennaway } 54774664626SKris Kennaway 54874664626SKris Kennaway long SSL_SESSION_get_timeout(SSL_SESSION *s) 54974664626SKris Kennaway { 55074664626SKris Kennaway if (s == NULL) return(0); 55174664626SKris Kennaway return(s->timeout); 55274664626SKris Kennaway } 55374664626SKris Kennaway 55474664626SKris Kennaway long SSL_SESSION_get_time(SSL_SESSION *s) 55574664626SKris Kennaway { 55674664626SKris Kennaway if (s == NULL) return(0); 55774664626SKris Kennaway return(s->time); 55874664626SKris Kennaway } 55974664626SKris Kennaway 56074664626SKris Kennaway long SSL_SESSION_set_time(SSL_SESSION *s, long t) 56174664626SKris Kennaway { 56274664626SKris Kennaway if (s == NULL) return(0); 56374664626SKris Kennaway s->time=t; 56474664626SKris Kennaway return(t); 56574664626SKris Kennaway } 56674664626SKris Kennaway 56774664626SKris Kennaway long SSL_CTX_set_timeout(SSL_CTX *s, long t) 56874664626SKris Kennaway { 56974664626SKris Kennaway long l; 57074664626SKris Kennaway if (s == NULL) return(0); 57174664626SKris Kennaway l=s->session_timeout; 57274664626SKris Kennaway s->session_timeout=t; 57374664626SKris Kennaway return(l); 57474664626SKris Kennaway } 57574664626SKris Kennaway 57674664626SKris Kennaway long SSL_CTX_get_timeout(SSL_CTX *s) 57774664626SKris Kennaway { 57874664626SKris Kennaway if (s == NULL) return(0); 57974664626SKris Kennaway return(s->session_timeout); 58074664626SKris Kennaway } 58174664626SKris Kennaway 58274664626SKris Kennaway typedef struct timeout_param_st 58374664626SKris Kennaway { 58474664626SKris Kennaway SSL_CTX *ctx; 58574664626SKris Kennaway long time; 58674664626SKris Kennaway LHASH *cache; 58774664626SKris Kennaway } TIMEOUT_PARAM; 58874664626SKris Kennaway 58974664626SKris Kennaway static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p) 59074664626SKris Kennaway { 59174664626SKris Kennaway if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */ 59274664626SKris Kennaway { 59374664626SKris Kennaway /* The reason we don't call SSL_CTX_remove_session() is to 59474664626SKris Kennaway * save on locking overhead */ 595f579bf8eSKris Kennaway lh_delete(p->cache,s); 59674664626SKris Kennaway SSL_SESSION_list_remove(p->ctx,s); 59774664626SKris Kennaway s->not_resumable=1; 59874664626SKris Kennaway if (p->ctx->remove_session_cb != NULL) 59974664626SKris Kennaway p->ctx->remove_session_cb(p->ctx,s); 60074664626SKris Kennaway SSL_SESSION_free(s); 60174664626SKris Kennaway } 60274664626SKris Kennaway } 60374664626SKris Kennaway 60474664626SKris Kennaway void SSL_CTX_flush_sessions(SSL_CTX *s, long t) 60574664626SKris Kennaway { 60674664626SKris Kennaway unsigned long i; 60774664626SKris Kennaway TIMEOUT_PARAM tp; 60874664626SKris Kennaway 60974664626SKris Kennaway tp.ctx=s; 61074664626SKris Kennaway tp.cache=s->sessions; 61174664626SKris Kennaway if (tp.cache == NULL) return; 61274664626SKris Kennaway tp.time=t; 61374664626SKris Kennaway CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 61474664626SKris Kennaway i=tp.cache->down_load; 61574664626SKris Kennaway tp.cache->down_load=0; 616f579bf8eSKris Kennaway lh_doall_arg(tp.cache,(void (*)())timeout,&tp); 61774664626SKris Kennaway tp.cache->down_load=i; 61874664626SKris Kennaway CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 61974664626SKris Kennaway } 62074664626SKris Kennaway 62174664626SKris Kennaway int ssl_clear_bad_session(SSL *s) 62274664626SKris Kennaway { 62374664626SKris Kennaway if ( (s->session != NULL) && 62474664626SKris Kennaway !(s->shutdown & SSL_SENT_SHUTDOWN) && 62574664626SKris Kennaway !(SSL_in_init(s) || SSL_in_before(s))) 62674664626SKris Kennaway { 62774664626SKris Kennaway SSL_CTX_remove_session(s->ctx,s->session); 62874664626SKris Kennaway return(1); 62974664626SKris Kennaway } 63074664626SKris Kennaway else 63174664626SKris Kennaway return(0); 63274664626SKris Kennaway } 63374664626SKris Kennaway 63474664626SKris Kennaway /* locked by SSL_CTX in the calling function */ 63574664626SKris Kennaway static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) 63674664626SKris Kennaway { 63774664626SKris Kennaway if ((s->next == NULL) || (s->prev == NULL)) return; 63874664626SKris Kennaway 63974664626SKris Kennaway if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) 64074664626SKris Kennaway { /* last element in list */ 64174664626SKris Kennaway if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) 64274664626SKris Kennaway { /* only one element in list */ 64374664626SKris Kennaway ctx->session_cache_head=NULL; 64474664626SKris Kennaway ctx->session_cache_tail=NULL; 64574664626SKris Kennaway } 64674664626SKris Kennaway else 64774664626SKris Kennaway { 64874664626SKris Kennaway ctx->session_cache_tail=s->prev; 64974664626SKris Kennaway s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail); 65074664626SKris Kennaway } 65174664626SKris Kennaway } 65274664626SKris Kennaway else 65374664626SKris Kennaway { 65474664626SKris Kennaway if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) 65574664626SKris Kennaway { /* first element in list */ 65674664626SKris Kennaway ctx->session_cache_head=s->next; 65774664626SKris Kennaway s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head); 65874664626SKris Kennaway } 65974664626SKris Kennaway else 66074664626SKris Kennaway { /* middle of list */ 66174664626SKris Kennaway s->next->prev=s->prev; 66274664626SKris Kennaway s->prev->next=s->next; 66374664626SKris Kennaway } 66474664626SKris Kennaway } 66574664626SKris Kennaway s->prev=s->next=NULL; 66674664626SKris Kennaway } 66774664626SKris Kennaway 66874664626SKris Kennaway static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) 66974664626SKris Kennaway { 67074664626SKris Kennaway if ((s->next != NULL) && (s->prev != NULL)) 67174664626SKris Kennaway SSL_SESSION_list_remove(ctx,s); 67274664626SKris Kennaway 67374664626SKris Kennaway if (ctx->session_cache_head == NULL) 67474664626SKris Kennaway { 67574664626SKris Kennaway ctx->session_cache_head=s; 67674664626SKris Kennaway ctx->session_cache_tail=s; 67774664626SKris Kennaway s->prev=(SSL_SESSION *)&(ctx->session_cache_head); 67874664626SKris Kennaway s->next=(SSL_SESSION *)&(ctx->session_cache_tail); 67974664626SKris Kennaway } 68074664626SKris Kennaway else 68174664626SKris Kennaway { 68274664626SKris Kennaway s->next=ctx->session_cache_head; 68374664626SKris Kennaway s->next->prev=s; 68474664626SKris Kennaway s->prev=(SSL_SESSION *)&(ctx->session_cache_head); 68574664626SKris Kennaway ctx->session_cache_head=s; 68674664626SKris Kennaway } 68774664626SKris Kennaway } 68874664626SKris Kennaway 689