sl@0: /* ssl/ssl_sess.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "ssl_locl.h" sl@0: sl@0: static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); sl@0: static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s); sl@0: static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); sl@0: sl@0: EXPORT_C SSL_SESSION *SSL_get_session(const SSL *ssl) sl@0: /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ sl@0: { sl@0: return(ssl->session); sl@0: } sl@0: sl@0: EXPORT_C SSL_SESSION *SSL_get1_session(SSL *ssl) sl@0: /* variant of SSL_get_session: caller really gets something */ sl@0: { sl@0: SSL_SESSION *sess; sl@0: /* Need to lock this all up rather than just use CRYPTO_add so that sl@0: * somebody doesn't free ssl->session between when we check it's sl@0: * non-null and when we up the reference count. */ sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION); sl@0: sess = ssl->session; sl@0: if(sess) sl@0: sess->references++; sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION); sl@0: return(sess); sl@0: } sl@0: sl@0: EXPORT_C int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) sl@0: { sl@0: return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); sl@0: } sl@0: sl@0: EXPORT_C void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&s->ex_data,idx)); sl@0: } sl@0: sl@0: EXPORT_C SSL_SESSION *SSL_SESSION_new(void) sl@0: { sl@0: SSL_SESSION *ss; sl@0: sl@0: ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); sl@0: if (ss == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: memset(ss,0,sizeof(SSL_SESSION)); sl@0: sl@0: ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ sl@0: ss->references=1; sl@0: ss->timeout=60*5+4; /* 5 minute timeout by default */ sl@0: ss->time=(unsigned long)time(NULL); sl@0: ss->prev=NULL; sl@0: ss->next=NULL; sl@0: ss->compress_meth=0; sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); sl@0: return(ss); sl@0: } sl@0: sl@0: EXPORT_C const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) sl@0: { sl@0: if(len) sl@0: *len = s->session_id_length; sl@0: return s->session_id; sl@0: } sl@0: sl@0: /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1 sl@0: * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly sl@0: * until we have no conflict is going to complete in one iteration pretty much sl@0: * "most" of the time (btw: understatement). So, if it takes us 10 iterations sl@0: * and we still can't avoid a conflict - well that's a reasonable point to call sl@0: * it quits. Either the RAND code is broken or someone is trying to open roughly sl@0: * very close to 2^128 (or 2^256) SSL sessions to our server. How you might sl@0: * store that many sessions is perhaps a more interesting question ... */ sl@0: sl@0: #define MAX_SESS_ID_ATTEMPTS 10 sl@0: static int def_generate_session_id(const SSL *ssl, unsigned char *id, sl@0: unsigned int *id_len) sl@0: { sl@0: unsigned int retry = 0; sl@0: do sl@0: if (RAND_pseudo_bytes(id, *id_len) <= 0) sl@0: return 0; sl@0: while(SSL_has_matching_session_id(ssl, id, *id_len) && sl@0: (++retry < MAX_SESS_ID_ATTEMPTS)); sl@0: if(retry < MAX_SESS_ID_ATTEMPTS) sl@0: return 1; sl@0: /* else - woops a session_id match */ sl@0: /* XXX We should also check the external cache -- sl@0: * but the probability of a collision is negligible, and sl@0: * we could not prevent the concurrent creation of sessions sl@0: * with identical IDs since we currently don't have means sl@0: * to atomically check whether a session ID already exists sl@0: * and make a reservation for it if it does not sl@0: * (this problem applies to the internal cache as well). sl@0: */ sl@0: return 0; sl@0: } sl@0: sl@0: int ssl_get_new_session(SSL *s, int session) sl@0: { sl@0: /* This gets used by clients and servers. */ sl@0: sl@0: unsigned int tmp; sl@0: SSL_SESSION *ss=NULL; sl@0: GEN_SESSION_CB cb = def_generate_session_id; sl@0: sl@0: if ((ss=SSL_SESSION_new()) == NULL) return(0); sl@0: sl@0: /* If the context has a default timeout, use it */ sl@0: if (s->ctx->session_timeout == 0) sl@0: ss->timeout=SSL_get_default_timeout(s); sl@0: else sl@0: ss->timeout=s->ctx->session_timeout; sl@0: sl@0: if (s->session != NULL) sl@0: { sl@0: SSL_SESSION_free(s->session); sl@0: s->session=NULL; sl@0: } sl@0: sl@0: if (session) sl@0: { sl@0: if (s->version == SSL2_VERSION) sl@0: { sl@0: ss->ssl_version=SSL2_VERSION; sl@0: ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH; sl@0: } sl@0: else if (s->version == SSL3_VERSION) sl@0: { sl@0: ss->ssl_version=SSL3_VERSION; sl@0: ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH; sl@0: } sl@0: else if (s->version == TLS1_VERSION) sl@0: { sl@0: ss->ssl_version=TLS1_VERSION; sl@0: ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH; sl@0: } sl@0: else if (s->version == DTLS1_VERSION) sl@0: { sl@0: ss->ssl_version=DTLS1_VERSION; sl@0: ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH; sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION); sl@0: SSL_SESSION_free(ss); sl@0: return(0); sl@0: } sl@0: /* Choose which callback will set the session ID */ sl@0: CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); sl@0: if(s->generate_session_id) sl@0: cb = s->generate_session_id; sl@0: else if(s->ctx->generate_session_id) sl@0: cb = s->ctx->generate_session_id; sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: /* Choose a session ID */ sl@0: tmp = ss->session_id_length; sl@0: if(!cb(s, ss->session_id, &tmp)) sl@0: { sl@0: /* The callback failed */ sl@0: SSLerr(SSL_F_SSL_GET_NEW_SESSION, sl@0: SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); sl@0: SSL_SESSION_free(ss); sl@0: return(0); sl@0: } sl@0: /* Don't allow the callback to set the session length to zero. sl@0: * nor set it higher than it was. */ sl@0: if(!tmp || (tmp > ss->session_id_length)) sl@0: { sl@0: /* The callback set an illegal length */ sl@0: SSLerr(SSL_F_SSL_GET_NEW_SESSION, sl@0: SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); sl@0: SSL_SESSION_free(ss); sl@0: return(0); sl@0: } sl@0: /* If the session length was shrunk and we're SSLv2, pad it */ sl@0: if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION)) sl@0: memset(ss->session_id + tmp, 0, ss->session_id_length - tmp); sl@0: else sl@0: ss->session_id_length = tmp; sl@0: /* Finally, check for a conflict */ sl@0: if(SSL_has_matching_session_id(s, ss->session_id, sl@0: ss->session_id_length)) sl@0: { sl@0: SSLerr(SSL_F_SSL_GET_NEW_SESSION, sl@0: SSL_R_SSL_SESSION_ID_CONFLICT); sl@0: SSL_SESSION_free(ss); sl@0: return(0); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: ss->session_id_length=0; sl@0: } sl@0: sl@0: if (s->sid_ctx_length > sizeof ss->sid_ctx) sl@0: { sl@0: SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); sl@0: SSL_SESSION_free(ss); sl@0: return 0; sl@0: } sl@0: memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length); sl@0: ss->sid_ctx_length=s->sid_ctx_length; sl@0: s->session=ss; sl@0: ss->ssl_version=s->version; sl@0: ss->verify_result = X509_V_OK; sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len, sl@0: const unsigned char *limit) sl@0: { sl@0: /* This is used only by servers. */ sl@0: sl@0: SSL_SESSION *ret=NULL; sl@0: int fatal = 0; sl@0: sl@0: if (len > SSL_MAX_SSL_SESSION_ID_LENGTH) sl@0: goto err; sl@0: sl@0: if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) sl@0: sl@0: { sl@0: SSL_SESSION data; sl@0: data.ssl_version=s->version; sl@0: data.session_id_length=len; sl@0: if (len == 0) sl@0: return 0; sl@0: memcpy(data.session_id,session_id,len); sl@0: CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); sl@0: ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data); sl@0: if (ret != NULL) sl@0: /* don't allow other threads to steal it: */ sl@0: CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: } sl@0: sl@0: if (ret == NULL) sl@0: { sl@0: int copy=1; sl@0: sl@0: s->ctx->stats.sess_miss++; sl@0: ret=NULL; sl@0: if (s->ctx->get_session_cb != NULL sl@0: && (ret=s->ctx->get_session_cb(s,session_id,len,©)) sl@0: != NULL) sl@0: { sl@0: s->ctx->stats.sess_cb_hit++; sl@0: sl@0: /* Increment reference count now if the session callback sl@0: * asks us to do so (note that if the session structures sl@0: * returned by the callback are shared between threads, sl@0: * it must handle the reference count itself [i.e. copy == 0], sl@0: * or things won't be thread-safe). */ sl@0: if (copy) sl@0: CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: sl@0: /* Add the externally cached session to the internal sl@0: * cache as well if and only if we are supposed to. */ sl@0: if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE)) sl@0: /* The following should not return 1, otherwise, sl@0: * things are very strange */ sl@0: SSL_CTX_add_session(s->ctx,ret); sl@0: } sl@0: if (ret == NULL) sl@0: goto err; sl@0: } sl@0: sl@0: /* Now ret is non-NULL, and we own one of its reference counts. */ sl@0: sl@0: if (ret->sid_ctx_length != s->sid_ctx_length sl@0: || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length)) sl@0: { sl@0: /* We've found the session named by the client, but we don't sl@0: * want to use it in this context. */ sl@0: sl@0: #if 0 /* The client cannot always know when a session is not appropriate, sl@0: * so we shouldn't generate an error message. */ sl@0: sl@0: SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); sl@0: #endif sl@0: goto err; /* treat like cache miss */ sl@0: } sl@0: sl@0: if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) sl@0: { sl@0: /* We can't be sure if this session is being used out of sl@0: * context, which is especially important for SSL_VERIFY_PEER. sl@0: * The application should have used SSL[_CTX]_set_session_id_context. sl@0: * sl@0: * For this error case, we generate an error instead of treating sl@0: * the event like a cache miss (otherwise it would be easy for sl@0: * applications to effectively disable the session cache by sl@0: * accident without anyone noticing). sl@0: */ sl@0: sl@0: SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); sl@0: fatal = 1; sl@0: goto err; sl@0: } sl@0: sl@0: if (ret->cipher == NULL) sl@0: { sl@0: unsigned char buf[5],*p; sl@0: unsigned long l; sl@0: sl@0: p=buf; sl@0: l=ret->cipher_id; sl@0: l2n(l,p); sl@0: if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR) sl@0: ret->cipher=ssl_get_cipher_by_char(s,&(buf[2])); sl@0: else sl@0: ret->cipher=ssl_get_cipher_by_char(s,&(buf[1])); sl@0: if (ret->cipher == NULL) sl@0: goto err; sl@0: } sl@0: sl@0: sl@0: #if 0 /* This is way too late. */ sl@0: sl@0: /* If a thread got the session, then 'swaped', and another got sl@0: * it and then due to a time-out decided to 'OPENSSL_free' it we could sl@0: * be in trouble. So I'll increment it now, then double decrement sl@0: * later - am I speaking rubbish?. */ sl@0: CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: #endif sl@0: sl@0: if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */ sl@0: { sl@0: s->ctx->stats.sess_timeout++; sl@0: /* remove it from the cache */ sl@0: SSL_CTX_remove_session(s->ctx,ret); sl@0: goto err; sl@0: } sl@0: sl@0: s->ctx->stats.sess_hit++; sl@0: sl@0: /* ret->time=time(NULL); */ /* rezero timeout? */ sl@0: /* again, just leave the session sl@0: * if it is the same session, we have just incremented and sl@0: * then decremented the reference count :-) */ sl@0: if (s->session != NULL) sl@0: SSL_SESSION_free(s->session); sl@0: s->session=ret; sl@0: s->verify_result = s->session->verify_result; sl@0: return(1); sl@0: sl@0: err: sl@0: if (ret != NULL) sl@0: SSL_SESSION_free(ret); sl@0: if (fatal) sl@0: return -1; sl@0: else sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) sl@0: { sl@0: int ret=0; sl@0: SSL_SESSION *s; sl@0: sl@0: /* add just 1 reference count for the SSL_CTX's session cache sl@0: * even though it has two ways of access: each session is in a sl@0: * doubly linked list and an lhash */ sl@0: CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: /* if session c is in already in cache, we take back the increment later */ sl@0: sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); sl@0: s=(SSL_SESSION *)lh_insert(ctx->sessions,c); sl@0: sl@0: /* s != NULL iff we already had a session with the given PID. sl@0: * In this case, s == c should hold (then we did not really modify sl@0: * ctx->sessions), or we're in trouble. */ sl@0: if (s != NULL && s != c) sl@0: { sl@0: /* We *are* in trouble ... */ sl@0: SSL_SESSION_list_remove(ctx,s); sl@0: SSL_SESSION_free(s); sl@0: /* ... so pretend the other session did not exist in cache sl@0: * (we cannot handle two SSL_SESSION structures with identical sl@0: * session ID in the same cache, which could happen e.g. when sl@0: * two threads concurrently obtain the same session from an external sl@0: * cache) */ sl@0: s = NULL; sl@0: } sl@0: sl@0: /* Put at the head of the queue unless it is already in the cache */ sl@0: if (s == NULL) sl@0: SSL_SESSION_list_add(ctx,c); sl@0: sl@0: if (s != NULL) sl@0: { sl@0: /* existing cache entry -- decrement previously incremented reference sl@0: * count because it already takes into account the cache */ sl@0: sl@0: SSL_SESSION_free(s); /* s == c */ sl@0: ret=0; sl@0: } sl@0: else sl@0: { sl@0: /* new cache entry -- remove old ones if cache has become too large */ sl@0: sl@0: ret=1; sl@0: sl@0: if (SSL_CTX_sess_get_cache_size(ctx) > 0) sl@0: { sl@0: while (SSL_CTX_sess_number(ctx) > sl@0: SSL_CTX_sess_get_cache_size(ctx)) sl@0: { sl@0: if (!remove_session_lock(ctx, sl@0: ctx->session_cache_tail, 0)) sl@0: break; sl@0: else sl@0: ctx->stats.sess_cache_full++; sl@0: } sl@0: } sl@0: } sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) sl@0: { sl@0: return remove_session_lock(ctx, c, 1); sl@0: } sl@0: sl@0: static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) sl@0: { sl@0: SSL_SESSION *r; sl@0: int ret=0; sl@0: sl@0: if ((c != NULL) && (c->session_id_length != 0)) sl@0: { sl@0: if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); sl@0: if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c) sl@0: { sl@0: ret=1; sl@0: r=(SSL_SESSION *)lh_delete(ctx->sessions,c); sl@0: SSL_SESSION_list_remove(ctx,c); sl@0: } sl@0: sl@0: if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: sl@0: if (ret) sl@0: { sl@0: r->not_resumable=1; sl@0: if (ctx->remove_session_cb != NULL) sl@0: ctx->remove_session_cb(ctx,r); sl@0: SSL_SESSION_free(r); sl@0: } sl@0: } sl@0: else sl@0: ret=0; sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C void SSL_SESSION_free(SSL_SESSION *ss) sl@0: { sl@0: int i; sl@0: sl@0: if(ss == NULL) sl@0: return; sl@0: sl@0: i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("SSL_SESSION",ss); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"SSL_SESSION_free, bad reference count\n"); sl@0: abort(); /* ok */ sl@0: } sl@0: #endif sl@0: sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); sl@0: sl@0: OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg); sl@0: OPENSSL_cleanse(ss->master_key,sizeof ss->master_key); sl@0: OPENSSL_cleanse(ss->session_id,sizeof ss->session_id); sl@0: if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert); sl@0: if (ss->peer != NULL) X509_free(ss->peer); sl@0: if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers); sl@0: sl@0: OPENSSL_cleanse(ss,sizeof(*ss)); sl@0: OPENSSL_free(ss); sl@0: } sl@0: sl@0: EXPORT_C int SSL_set_session(SSL *s, SSL_SESSION *session) sl@0: { sl@0: int ret=0; sl@0: SSL_METHOD *meth; sl@0: sl@0: if (session != NULL) sl@0: { sl@0: meth=s->ctx->method->get_ssl_method(session->ssl_version); sl@0: if (meth == NULL) sl@0: meth=s->method->get_ssl_method(session->ssl_version); sl@0: if (meth == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD); sl@0: return(0); sl@0: } sl@0: sl@0: if (meth != s->method) sl@0: { sl@0: if (!SSL_set_ssl_method(s,meth)) sl@0: return(0); sl@0: if (s->ctx->session_timeout == 0) sl@0: session->timeout=SSL_get_default_timeout(s); sl@0: else sl@0: session->timeout=s->ctx->session_timeout; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: if (s->kssl_ctx && !s->kssl_ctx->client_princ && sl@0: session->krb5_client_princ_len > 0) sl@0: { sl@0: s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1); sl@0: memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ, sl@0: session->krb5_client_princ_len); sl@0: s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0'; sl@0: } sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: sl@0: /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/ sl@0: CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION); sl@0: if (s->session != NULL) sl@0: SSL_SESSION_free(s->session); sl@0: s->session=session; sl@0: s->verify_result = s->session->verify_result; sl@0: /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/ sl@0: ret=1; sl@0: } sl@0: else sl@0: { sl@0: if (s->session != NULL) sl@0: { sl@0: SSL_SESSION_free(s->session); sl@0: s->session=NULL; sl@0: } sl@0: sl@0: meth=s->ctx->method; sl@0: if (meth != s->method) sl@0: { sl@0: if (!SSL_set_ssl_method(s,meth)) sl@0: return(0); sl@0: } sl@0: ret=1; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) sl@0: { sl@0: if (s == NULL) return(0); sl@0: s->timeout=t; sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C long SSL_SESSION_get_timeout(const SSL_SESSION *s) sl@0: { sl@0: if (s == NULL) return(0); sl@0: return(s->timeout); sl@0: } sl@0: sl@0: EXPORT_C long SSL_SESSION_get_time(const SSL_SESSION *s) sl@0: { sl@0: if (s == NULL) return(0); sl@0: return(s->time); sl@0: } sl@0: sl@0: EXPORT_C long SSL_SESSION_set_time(SSL_SESSION *s, long t) sl@0: { sl@0: if (s == NULL) return(0); sl@0: s->time=t; sl@0: return(t); sl@0: } sl@0: sl@0: EXPORT_C long SSL_CTX_set_timeout(SSL_CTX *s, long t) sl@0: { sl@0: long l; sl@0: if (s == NULL) return(0); sl@0: l=s->session_timeout; sl@0: s->session_timeout=t; sl@0: return(l); sl@0: } sl@0: sl@0: EXPORT_C long SSL_CTX_get_timeout(const SSL_CTX *s) sl@0: { sl@0: if (s == NULL) return(0); sl@0: return(s->session_timeout); sl@0: } sl@0: sl@0: typedef struct timeout_param_st sl@0: { sl@0: SSL_CTX *ctx; sl@0: long time; sl@0: LHASH *cache; sl@0: } TIMEOUT_PARAM; sl@0: sl@0: static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p) sl@0: { sl@0: if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */ sl@0: { sl@0: /* The reason we don't call SSL_CTX_remove_session() is to sl@0: * save on locking overhead */ sl@0: lh_delete(p->cache,s); sl@0: SSL_SESSION_list_remove(p->ctx,s); sl@0: s->not_resumable=1; sl@0: if (p->ctx->remove_session_cb != NULL) sl@0: p->ctx->remove_session_cb(p->ctx,s); sl@0: SSL_SESSION_free(s); sl@0: } sl@0: } sl@0: sl@0: static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *) sl@0: sl@0: EXPORT_C void SSL_CTX_flush_sessions(SSL_CTX *s, long t) sl@0: { sl@0: unsigned long i; sl@0: TIMEOUT_PARAM tp; sl@0: sl@0: tp.ctx=s; sl@0: tp.cache=s->sessions; sl@0: if (tp.cache == NULL) return; sl@0: tp.time=t; sl@0: CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); sl@0: i=tp.cache->down_load; sl@0: tp.cache->down_load=0; sl@0: lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp); sl@0: tp.cache->down_load=i; sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); sl@0: } sl@0: sl@0: int ssl_clear_bad_session(SSL *s) sl@0: { sl@0: if ( (s->session != NULL) && sl@0: !(s->shutdown & SSL_SENT_SHUTDOWN) && sl@0: !(SSL_in_init(s) || SSL_in_before(s))) sl@0: { sl@0: SSL_CTX_remove_session(s->ctx,s->session); sl@0: return(1); sl@0: } sl@0: else sl@0: return(0); sl@0: } sl@0: sl@0: /* locked by SSL_CTX in the calling function */ sl@0: static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) sl@0: { sl@0: if ((s->next == NULL) || (s->prev == NULL)) return; sl@0: sl@0: if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) sl@0: { /* last element in list */ sl@0: if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) sl@0: { /* only one element in list */ sl@0: ctx->session_cache_head=NULL; sl@0: ctx->session_cache_tail=NULL; sl@0: } sl@0: else sl@0: { sl@0: ctx->session_cache_tail=s->prev; sl@0: s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) sl@0: { /* first element in list */ sl@0: ctx->session_cache_head=s->next; sl@0: s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head); sl@0: } sl@0: else sl@0: { /* middle of list */ sl@0: s->next->prev=s->prev; sl@0: s->prev->next=s->next; sl@0: } sl@0: } sl@0: s->prev=s->next=NULL; sl@0: } sl@0: sl@0: static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) sl@0: { sl@0: if ((s->next != NULL) && (s->prev != NULL)) sl@0: SSL_SESSION_list_remove(ctx,s); sl@0: sl@0: if (ctx->session_cache_head == NULL) sl@0: { sl@0: ctx->session_cache_head=s; sl@0: ctx->session_cache_tail=s; sl@0: s->prev=(SSL_SESSION *)&(ctx->session_cache_head); sl@0: s->next=(SSL_SESSION *)&(ctx->session_cache_tail); sl@0: } sl@0: else sl@0: { sl@0: s->next=ctx->session_cache_head; sl@0: s->next->prev=s; sl@0: s->prev=(SSL_SESSION *)&(ctx->session_cache_head); sl@0: ctx->session_cache_head=s; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, sl@0: int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess)) sl@0: { sl@0: ctx->new_session_cb=cb; sl@0: } sl@0: sl@0: EXPORT_C int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) sl@0: { sl@0: return ctx->new_session_cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, sl@0: void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess)) sl@0: { sl@0: ctx->remove_session_cb=cb; sl@0: } sl@0: sl@0: EXPORT_C void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess) sl@0: { sl@0: return ctx->remove_session_cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, sl@0: SSL_SESSION *(*cb)(struct ssl_st *ssl, sl@0: unsigned char *data,int len,int *copy)) sl@0: { sl@0: ctx->get_session_cb=cb; sl@0: } sl@0: sl@0: EXPORT_C SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, sl@0: unsigned char *data,int len,int *copy) sl@0: { sl@0: return ctx->get_session_cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_info_callback(SSL_CTX *ctx, sl@0: void (*cb)(const SSL *ssl,int type,int val)) sl@0: { sl@0: ctx->info_callback=cb; sl@0: } sl@0: sl@0: EXPORT_C void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val) sl@0: { sl@0: return ctx->info_callback; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, sl@0: int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey)) sl@0: { sl@0: ctx->client_cert_cb=cb; sl@0: } sl@0: sl@0: EXPORT_C int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey) sl@0: { sl@0: return ctx->client_cert_cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, sl@0: int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)) sl@0: { sl@0: ctx->app_gen_cookie_cb=cb; sl@0: } sl@0: sl@0: EXPORT_C void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, sl@0: int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len)) sl@0: { sl@0: ctx->app_verify_cookie_cb=cb; sl@0: } sl@0: