155714Skris/* ssl/ssl_sess.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 58238405Sjkim/* ==================================================================== 59238405Sjkim * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60238405Sjkim * 61238405Sjkim * Redistribution and use in source and binary forms, with or without 62238405Sjkim * modification, are permitted provided that the following conditions 63238405Sjkim * are met: 64238405Sjkim * 65238405Sjkim * 1. Redistributions of source code must retain the above copyright 66296341Sdelphij * notice, this list of conditions and the following disclaimer. 67238405Sjkim * 68238405Sjkim * 2. Redistributions in binary form must reproduce the above copyright 69238405Sjkim * notice, this list of conditions and the following disclaimer in 70238405Sjkim * the documentation and/or other materials provided with the 71238405Sjkim * distribution. 72238405Sjkim * 73238405Sjkim * 3. All advertising materials mentioning features or use of this 74238405Sjkim * software must display the following acknowledgment: 75238405Sjkim * "This product includes software developed by the OpenSSL Project 76238405Sjkim * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77238405Sjkim * 78238405Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79238405Sjkim * endorse or promote products derived from this software without 80238405Sjkim * prior written permission. For written permission, please contact 81238405Sjkim * openssl-core@openssl.org. 82238405Sjkim * 83238405Sjkim * 5. Products derived from this software may not be called "OpenSSL" 84238405Sjkim * nor may "OpenSSL" appear in their names without prior written 85238405Sjkim * permission of the OpenSSL Project. 86238405Sjkim * 87238405Sjkim * 6. Redistributions of any form whatsoever must retain the following 88238405Sjkim * acknowledgment: 89238405Sjkim * "This product includes software developed by the OpenSSL Project 90238405Sjkim * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91238405Sjkim * 92238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93238405Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94238405Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95238405Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96238405Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97238405Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98238405Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99238405Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100238405Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101238405Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102238405Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103238405Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 104238405Sjkim * ==================================================================== 105238405Sjkim * 106238405Sjkim * This product includes cryptographic software written by Eric Young 107238405Sjkim * (eay@cryptsoft.com). This product includes software written by Tim 108238405Sjkim * Hudson (tjh@cryptsoft.com). 109238405Sjkim * 110238405Sjkim */ 111238405Sjkim/* ==================================================================== 112238405Sjkim * Copyright 2005 Nokia. All rights reserved. 113238405Sjkim * 114238405Sjkim * The portions of the attached software ("Contribution") is developed by 115238405Sjkim * Nokia Corporation and is licensed pursuant to the OpenSSL open source 116238405Sjkim * license. 117238405Sjkim * 118238405Sjkim * The Contribution, originally written by Mika Kousa and Pasi Eronen of 119238405Sjkim * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 120238405Sjkim * support (see RFC 4279) to OpenSSL. 121238405Sjkim * 122238405Sjkim * No patent licenses or other rights except those expressly stated in 123238405Sjkim * the OpenSSL open source license shall be deemed granted or received 124238405Sjkim * expressly, by implication, estoppel, or otherwise. 125238405Sjkim * 126238405Sjkim * No assurances are provided by Nokia that the Contribution does not 127238405Sjkim * infringe the patent or other intellectual property rights of any third 128238405Sjkim * party or that the license provides you with all the necessary rights 129238405Sjkim * to make use of the Contribution. 130238405Sjkim * 131238405Sjkim * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 132238405Sjkim * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 133238405Sjkim * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 134238405Sjkim * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 135238405Sjkim * OTHERWISE. 136238405Sjkim */ 13755714Skris 13855714Skris#include <stdio.h> 13955714Skris#include <openssl/lhash.h> 14055714Skris#include <openssl/rand.h> 141194206Ssimon#ifndef OPENSSL_NO_ENGINE 142296341Sdelphij# include <openssl/engine.h> 143194206Ssimon#endif 14455714Skris#include "ssl_locl.h" 14555714Skris 14655714Skrisstatic void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 147296341Sdelphijstatic void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); 14855714Skrisstatic int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 14955714Skris 150160814SsimonSSL_SESSION *SSL_get_session(const SSL *ssl) 15159191Skris/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 152296341Sdelphij{ 153296341Sdelphij return (ssl->session); 154296341Sdelphij} 15555714Skris 15659191SkrisSSL_SESSION *SSL_get1_session(SSL *ssl) 15759191Skris/* variant of SSL_get_session: caller really gets something */ 158296341Sdelphij{ 159296341Sdelphij SSL_SESSION *sess; 160296341Sdelphij /* 161296341Sdelphij * Need to lock this all up rather than just use CRYPTO_add so that 162296341Sdelphij * somebody doesn't free ssl->session between when we check it's non-null 163296341Sdelphij * and when we up the reference count. 164296341Sdelphij */ 165296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION); 166296341Sdelphij sess = ssl->session; 167296341Sdelphij if (sess) 168296341Sdelphij sess->references++; 169296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION); 170296341Sdelphij return (sess); 171296341Sdelphij} 17259191Skris 173296341Sdelphijint SSL_SESSION_get_ex_new_index(long argl, void *argp, 174296341Sdelphij CRYPTO_EX_new *new_func, 175296341Sdelphij CRYPTO_EX_dup *dup_func, 176296341Sdelphij CRYPTO_EX_free *free_func) 177296341Sdelphij{ 178296341Sdelphij return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp, 179296341Sdelphij new_func, dup_func, free_func); 180296341Sdelphij} 18155714Skris 18255714Skrisint SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) 183296341Sdelphij{ 184296341Sdelphij return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); 185296341Sdelphij} 18655714Skris 187160814Ssimonvoid *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) 188296341Sdelphij{ 189296341Sdelphij return (CRYPTO_get_ex_data(&s->ex_data, idx)); 190296341Sdelphij} 19155714Skris 19255714SkrisSSL_SESSION *SSL_SESSION_new(void) 193296341Sdelphij{ 194296341Sdelphij SSL_SESSION *ss; 19555714Skris 196296341Sdelphij ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); 197296341Sdelphij if (ss == NULL) { 198296341Sdelphij SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE); 199296341Sdelphij return (0); 200296341Sdelphij } 201296341Sdelphij memset(ss, 0, sizeof(SSL_SESSION)); 20255714Skris 203296341Sdelphij ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 204296341Sdelphij ss->references = 1; 205296341Sdelphij ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */ 206296341Sdelphij ss->time = (unsigned long)time(NULL); 207296341Sdelphij ss->prev = NULL; 208296341Sdelphij ss->next = NULL; 209296341Sdelphij ss->compress_meth = 0; 210194206Ssimon#ifndef OPENSSL_NO_TLSEXT 211296341Sdelphij ss->tlsext_hostname = NULL; 212296341Sdelphij# ifndef OPENSSL_NO_EC 213296341Sdelphij ss->tlsext_ecpointformatlist_length = 0; 214296341Sdelphij ss->tlsext_ecpointformatlist = NULL; 215296341Sdelphij ss->tlsext_ellipticcurvelist_length = 0; 216296341Sdelphij ss->tlsext_ellipticcurvelist = NULL; 217296341Sdelphij# endif 218194206Ssimon#endif 219296341Sdelphij CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 220238405Sjkim#ifndef OPENSSL_NO_PSK 221296341Sdelphij ss->psk_identity_hint = NULL; 222296341Sdelphij ss->psk_identity = NULL; 223238405Sjkim#endif 224238405Sjkim#ifndef OPENSSL_NO_SRP 225296341Sdelphij ss->srp_username = NULL; 226238405Sjkim#endif 227296341Sdelphij return (ss); 228296341Sdelphij} 22955714Skris 230284295Sdelphij/* 231284295Sdelphij * Create a new SSL_SESSION and duplicate the contents of |src| into it. If 232284295Sdelphij * ticket == 0 then no ticket information is duplicated, otherwise it is. 233284295Sdelphij */ 234284295SdelphijSSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket) 235284295Sdelphij{ 236284295Sdelphij SSL_SESSION *dest; 237284295Sdelphij 238284295Sdelphij dest = OPENSSL_malloc(sizeof(*src)); 239284295Sdelphij if (dest == NULL) { 240284295Sdelphij goto err; 241284295Sdelphij } 242284295Sdelphij memcpy(dest, src, sizeof(*dest)); 243284295Sdelphij 244284295Sdelphij /* 245284295Sdelphij * Set the various pointers to NULL so that we can call SSL_SESSION_free in 246284295Sdelphij * the case of an error whilst halfway through constructing dest 247284295Sdelphij */ 248284295Sdelphij#ifndef OPENSSL_NO_PSK 249284295Sdelphij dest->psk_identity_hint = NULL; 250284295Sdelphij dest->psk_identity = NULL; 251284295Sdelphij#endif 252284295Sdelphij dest->ciphers = NULL; 253284295Sdelphij#ifndef OPENSSL_NO_TLSEXT 254284295Sdelphij dest->tlsext_hostname = NULL; 255284295Sdelphij# ifndef OPENSSL_NO_EC 256284295Sdelphij dest->tlsext_ecpointformatlist = NULL; 257284295Sdelphij dest->tlsext_ellipticcurvelist = NULL; 258284295Sdelphij# endif 259284295Sdelphij#endif 260284295Sdelphij dest->tlsext_tick = NULL; 261284295Sdelphij#ifndef OPENSSL_NO_SRP 262284295Sdelphij dest->srp_username = NULL; 263284295Sdelphij#endif 264284295Sdelphij memset(&dest->ex_data, 0, sizeof(dest->ex_data)); 265284295Sdelphij 266284295Sdelphij /* We deliberately don't copy the prev and next pointers */ 267284295Sdelphij dest->prev = NULL; 268284295Sdelphij dest->next = NULL; 269284295Sdelphij 270284295Sdelphij dest->references = 1; 271284295Sdelphij 272284295Sdelphij if (src->sess_cert != NULL) 273284295Sdelphij CRYPTO_add(&src->sess_cert->references, 1, CRYPTO_LOCK_SSL_SESS_CERT); 274284295Sdelphij 275284295Sdelphij if (src->peer != NULL) 276284295Sdelphij CRYPTO_add(&src->peer->references, 1, CRYPTO_LOCK_X509); 277284295Sdelphij 278284295Sdelphij#ifndef OPENSSL_NO_PSK 279284295Sdelphij if (src->psk_identity_hint) { 280284295Sdelphij dest->psk_identity_hint = BUF_strdup(src->psk_identity_hint); 281284295Sdelphij if (dest->psk_identity_hint == NULL) { 282284295Sdelphij goto err; 283284295Sdelphij } 284284295Sdelphij } 285284295Sdelphij if (src->psk_identity) { 286284295Sdelphij dest->psk_identity = BUF_strdup(src->psk_identity); 287284295Sdelphij if (dest->psk_identity == NULL) { 288284295Sdelphij goto err; 289284295Sdelphij } 290284295Sdelphij } 291284295Sdelphij#endif 292284295Sdelphij 293284295Sdelphij if(src->ciphers != NULL) { 294284295Sdelphij dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers); 295284295Sdelphij if (dest->ciphers == NULL) 296284295Sdelphij goto err; 297284295Sdelphij } 298284295Sdelphij 299284295Sdelphij if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, 300284295Sdelphij &dest->ex_data, &src->ex_data)) { 301284295Sdelphij goto err; 302284295Sdelphij } 303284295Sdelphij 304284295Sdelphij#ifndef OPENSSL_NO_TLSEXT 305284295Sdelphij if (src->tlsext_hostname) { 306284295Sdelphij dest->tlsext_hostname = BUF_strdup(src->tlsext_hostname); 307284295Sdelphij if (dest->tlsext_hostname == NULL) { 308284295Sdelphij goto err; 309284295Sdelphij } 310284295Sdelphij } 311284295Sdelphij# ifndef OPENSSL_NO_EC 312284295Sdelphij if (src->tlsext_ecpointformatlist) { 313284295Sdelphij dest->tlsext_ecpointformatlist = 314284295Sdelphij BUF_memdup(src->tlsext_ecpointformatlist, 315284295Sdelphij src->tlsext_ecpointformatlist_length); 316284295Sdelphij if (dest->tlsext_ecpointformatlist == NULL) 317284295Sdelphij goto err; 318284295Sdelphij } 319284295Sdelphij if (src->tlsext_ellipticcurvelist) { 320284295Sdelphij dest->tlsext_ellipticcurvelist = 321284295Sdelphij BUF_memdup(src->tlsext_ellipticcurvelist, 322284295Sdelphij src->tlsext_ellipticcurvelist_length); 323284295Sdelphij if (dest->tlsext_ellipticcurvelist == NULL) 324284295Sdelphij goto err; 325284295Sdelphij } 326284295Sdelphij# endif 327284295Sdelphij#endif 328284295Sdelphij 329284295Sdelphij if (ticket != 0) { 330284295Sdelphij dest->tlsext_tick = BUF_memdup(src->tlsext_tick, src->tlsext_ticklen); 331284295Sdelphij if(dest->tlsext_tick == NULL) 332284295Sdelphij goto err; 333284295Sdelphij } else { 334284295Sdelphij dest->tlsext_tick_lifetime_hint = 0; 335284295Sdelphij dest->tlsext_ticklen = 0; 336284295Sdelphij } 337284295Sdelphij 338284295Sdelphij#ifndef OPENSSL_NO_SRP 339284295Sdelphij if (src->srp_username) { 340284295Sdelphij dest->srp_username = BUF_strdup(src->srp_username); 341284295Sdelphij if (dest->srp_username == NULL) { 342284295Sdelphij goto err; 343284295Sdelphij } 344284295Sdelphij } 345284295Sdelphij#endif 346284295Sdelphij 347284295Sdelphij return dest; 348284295Sdelphijerr: 349284295Sdelphij SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE); 350284295Sdelphij SSL_SESSION_free(dest); 351284295Sdelphij return NULL; 352284295Sdelphij} 353284295Sdelphij 354296341Sdelphijconst unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, 355296341Sdelphij unsigned int *len) 356296341Sdelphij{ 357296341Sdelphij if (len) 358296341Sdelphij *len = s->session_id_length; 359296341Sdelphij return s->session_id; 360296341Sdelphij} 361160814Ssimon 362238405Sjkimunsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) 363296341Sdelphij{ 364296341Sdelphij return s->compress_meth; 365296341Sdelphij} 366238405Sjkim 367296341Sdelphij/* 368296341Sdelphij * Even with SSLv2, we have 16 bytes (128 bits) of session ID space. 369296341Sdelphij * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random 370296341Sdelphij * gunk repeatedly until we have no conflict is going to complete in one 371296341Sdelphij * iteration pretty much "most" of the time (btw: understatement). So, if it 372296341Sdelphij * takes us 10 iterations and we still can't avoid a conflict - well that's a 373296341Sdelphij * reasonable point to call it quits. Either the RAND code is broken or 374296341Sdelphij * someone is trying to open roughly very close to 2^128 (or 2^256) SSL 375296341Sdelphij * sessions to our server. How you might store that many sessions is perhaps 376296341Sdelphij * a more interesting question ... 377296341Sdelphij */ 378109998Smarkm 379109998Smarkm#define MAX_SESS_ID_ATTEMPTS 10 380109998Smarkmstatic int def_generate_session_id(const SSL *ssl, unsigned char *id, 381296341Sdelphij unsigned int *id_len) 382109998Smarkm{ 383296341Sdelphij unsigned int retry = 0; 384296341Sdelphij do 385296341Sdelphij if (RAND_pseudo_bytes(id, *id_len) <= 0) 386296341Sdelphij return 0; 387296341Sdelphij while (SSL_has_matching_session_id(ssl, id, *id_len) && 388296341Sdelphij (++retry < MAX_SESS_ID_ATTEMPTS)) ; 389296341Sdelphij if (retry < MAX_SESS_ID_ATTEMPTS) 390296341Sdelphij return 1; 391296341Sdelphij /* else - woops a session_id match */ 392296341Sdelphij /* 393296341Sdelphij * XXX We should also check the external cache -- but the probability of 394296341Sdelphij * a collision is negligible, and we could not prevent the concurrent 395296341Sdelphij * creation of sessions with identical IDs since we currently don't have 396296341Sdelphij * means to atomically check whether a session ID already exists and make 397296341Sdelphij * a reservation for it if it does not (this problem applies to the 398296341Sdelphij * internal cache as well). 399296341Sdelphij */ 400296341Sdelphij return 0; 401109998Smarkm} 402109998Smarkm 40355714Skrisint ssl_get_new_session(SSL *s, int session) 404296341Sdelphij{ 405296341Sdelphij /* This gets used by clients and servers. */ 40655714Skris 407296341Sdelphij unsigned int tmp; 408296341Sdelphij SSL_SESSION *ss = NULL; 409296341Sdelphij GEN_SESSION_CB cb = def_generate_session_id; 41055714Skris 411296341Sdelphij if ((ss = SSL_SESSION_new()) == NULL) 412296341Sdelphij return (0); 41355714Skris 414296341Sdelphij /* If the context has a default timeout, use it */ 415296341Sdelphij if (s->session_ctx->session_timeout == 0) 416296341Sdelphij ss->timeout = SSL_get_default_timeout(s); 417296341Sdelphij else 418296341Sdelphij ss->timeout = s->session_ctx->session_timeout; 41955714Skris 420296341Sdelphij if (s->session != NULL) { 421296341Sdelphij SSL_SESSION_free(s->session); 422296341Sdelphij s->session = NULL; 423296341Sdelphij } 42455714Skris 425296341Sdelphij if (session) { 426296341Sdelphij if (s->version == SSL2_VERSION) { 427296341Sdelphij ss->ssl_version = SSL2_VERSION; 428296341Sdelphij ss->session_id_length = SSL2_SSL_SESSION_ID_LENGTH; 429296341Sdelphij } else if (s->version == SSL3_VERSION) { 430296341Sdelphij ss->ssl_version = SSL3_VERSION; 431296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 432296341Sdelphij } else if (s->version == TLS1_VERSION) { 433296341Sdelphij ss->ssl_version = TLS1_VERSION; 434296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 435296341Sdelphij } else if (s->version == TLS1_1_VERSION) { 436296341Sdelphij ss->ssl_version = TLS1_1_VERSION; 437296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 438296341Sdelphij } else if (s->version == TLS1_2_VERSION) { 439296341Sdelphij ss->ssl_version = TLS1_2_VERSION; 440296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 441296341Sdelphij } else if (s->version == DTLS1_BAD_VER) { 442296341Sdelphij ss->ssl_version = DTLS1_BAD_VER; 443296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 444296341Sdelphij } else if (s->version == DTLS1_VERSION) { 445296341Sdelphij ss->ssl_version = DTLS1_VERSION; 446296341Sdelphij ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 447296341Sdelphij } else { 448296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION); 449296341Sdelphij SSL_SESSION_free(ss); 450296341Sdelphij return (0); 451296341Sdelphij } 452194206Ssimon#ifndef OPENSSL_NO_TLSEXT 453296341Sdelphij /*- 454296341Sdelphij * If RFC5077 ticket, use empty session ID (as server). 455296341Sdelphij * Note that: 456296341Sdelphij * (a) ssl_get_prev_session() does lookahead into the 457296341Sdelphij * ClientHello extensions to find the session ticket. 458296341Sdelphij * When ssl_get_prev_session() fails, s3_srvr.c calls 459296341Sdelphij * ssl_get_new_session() in ssl3_get_client_hello(). 460296341Sdelphij * At that point, it has not yet parsed the extensions, 461296341Sdelphij * however, because of the lookahead, it already knows 462296341Sdelphij * whether a ticket is expected or not. 463296341Sdelphij * 464296341Sdelphij * (b) s3_clnt.c calls ssl_get_new_session() before parsing 465296341Sdelphij * ServerHello extensions, and before recording the session 466296341Sdelphij * ID received from the server, so this block is a noop. 467296341Sdelphij */ 468296341Sdelphij if (s->tlsext_ticket_expected) { 469296341Sdelphij ss->session_id_length = 0; 470296341Sdelphij goto sess_id_done; 471296341Sdelphij } 472194206Ssimon#endif 473296341Sdelphij /* Choose which callback will set the session ID */ 474296341Sdelphij CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 475296341Sdelphij if (s->generate_session_id) 476296341Sdelphij cb = s->generate_session_id; 477296341Sdelphij else if (s->session_ctx->generate_session_id) 478296341Sdelphij cb = s->session_ctx->generate_session_id; 479296341Sdelphij CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 480296341Sdelphij /* Choose a session ID */ 481296341Sdelphij tmp = ss->session_id_length; 482296341Sdelphij if (!cb(s, ss->session_id, &tmp)) { 483296341Sdelphij /* The callback failed */ 484296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, 485296341Sdelphij SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); 486296341Sdelphij SSL_SESSION_free(ss); 487296341Sdelphij return (0); 488296341Sdelphij } 489296341Sdelphij /* 490296341Sdelphij * Don't allow the callback to set the session length to zero. nor 491296341Sdelphij * set it higher than it was. 492296341Sdelphij */ 493296341Sdelphij if (!tmp || (tmp > ss->session_id_length)) { 494296341Sdelphij /* The callback set an illegal length */ 495296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, 496296341Sdelphij SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); 497296341Sdelphij SSL_SESSION_free(ss); 498296341Sdelphij return (0); 499296341Sdelphij } 500296341Sdelphij /* If the session length was shrunk and we're SSLv2, pad it */ 501296341Sdelphij if ((tmp < ss->session_id_length) && (s->version == SSL2_VERSION)) 502296341Sdelphij memset(ss->session_id + tmp, 0, ss->session_id_length - tmp); 503296341Sdelphij else 504296341Sdelphij ss->session_id_length = tmp; 505296341Sdelphij /* Finally, check for a conflict */ 506296341Sdelphij if (SSL_has_matching_session_id(s, ss->session_id, 507296341Sdelphij ss->session_id_length)) { 508296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT); 509296341Sdelphij SSL_SESSION_free(ss); 510296341Sdelphij return (0); 511296341Sdelphij } 512194206Ssimon#ifndef OPENSSL_NO_TLSEXT 513296341Sdelphij sess_id_done: 514296341Sdelphij if (s->tlsext_hostname) { 515296341Sdelphij ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname); 516296341Sdelphij if (ss->tlsext_hostname == NULL) { 517296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); 518296341Sdelphij SSL_SESSION_free(ss); 519296341Sdelphij return 0; 520296341Sdelphij } 521296341Sdelphij } 522296341Sdelphij# ifndef OPENSSL_NO_EC 523296341Sdelphij if (s->tlsext_ecpointformatlist) { 524296341Sdelphij if (ss->tlsext_ecpointformatlist != NULL) 525296341Sdelphij OPENSSL_free(ss->tlsext_ecpointformatlist); 526296341Sdelphij if ((ss->tlsext_ecpointformatlist = 527296341Sdelphij OPENSSL_malloc(s->tlsext_ecpointformatlist_length)) == 528296341Sdelphij NULL) { 529296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE); 530296341Sdelphij SSL_SESSION_free(ss); 531296341Sdelphij return 0; 532296341Sdelphij } 533296341Sdelphij ss->tlsext_ecpointformatlist_length = 534296341Sdelphij s->tlsext_ecpointformatlist_length; 535296341Sdelphij memcpy(ss->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist, 536296341Sdelphij s->tlsext_ecpointformatlist_length); 537296341Sdelphij } 538296341Sdelphij if (s->tlsext_ellipticcurvelist) { 539296341Sdelphij if (ss->tlsext_ellipticcurvelist != NULL) 540296341Sdelphij OPENSSL_free(ss->tlsext_ellipticcurvelist); 541296341Sdelphij if ((ss->tlsext_ellipticcurvelist = 542296341Sdelphij OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == 543296341Sdelphij NULL) { 544296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE); 545296341Sdelphij SSL_SESSION_free(ss); 546296341Sdelphij return 0; 547296341Sdelphij } 548296341Sdelphij ss->tlsext_ellipticcurvelist_length = 549296341Sdelphij s->tlsext_ellipticcurvelist_length; 550296341Sdelphij memcpy(ss->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist, 551296341Sdelphij s->tlsext_ellipticcurvelist_length); 552296341Sdelphij } 553296341Sdelphij# endif 554194206Ssimon#endif 555296341Sdelphij } else { 556296341Sdelphij ss->session_id_length = 0; 557296341Sdelphij } 55855714Skris 559296341Sdelphij if (s->sid_ctx_length > sizeof ss->sid_ctx) { 560296341Sdelphij SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); 561296341Sdelphij SSL_SESSION_free(ss); 562296341Sdelphij return 0; 563296341Sdelphij } 564296341Sdelphij memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); 565296341Sdelphij ss->sid_ctx_length = s->sid_ctx_length; 566296341Sdelphij s->session = ss; 567296341Sdelphij ss->ssl_version = s->version; 568296341Sdelphij ss->verify_result = X509_V_OK; 56955714Skris 570296341Sdelphij return (1); 571296341Sdelphij} 57255714Skris 573296341Sdelphij/*- 574296341Sdelphij * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this 575238405Sjkim * connection. It is only called by servers. 576238405Sjkim * 577238405Sjkim * session_id: points at the session ID in the ClientHello. This code will 578238405Sjkim * read past the end of this in order to parse out the session ticket 579238405Sjkim * extension, if any. 580238405Sjkim * len: the length of the session ID. 581238405Sjkim * limit: a pointer to the first byte after the ClientHello. 582238405Sjkim * 583238405Sjkim * Returns: 584238405Sjkim * -1: error 585238405Sjkim * 0: a session may have been found. 586238405Sjkim * 587238405Sjkim * Side effects: 588238405Sjkim * - If a session is found then s->session is pointed at it (after freeing an 589238405Sjkim * existing session if need be) and s->verify_result is set from the session. 590238405Sjkim * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 591238405Sjkim * if the server should issue a new session ticket (to 0 otherwise). 592238405Sjkim */ 593194206Ssimonint ssl_get_prev_session(SSL *s, unsigned char *session_id, int len, 594296341Sdelphij const unsigned char *limit) 595296341Sdelphij{ 596296341Sdelphij /* This is used only by servers. */ 59755714Skris 598296341Sdelphij SSL_SESSION *ret = NULL; 599296341Sdelphij int fatal = 0; 600296341Sdelphij int try_session_cache = 1; 601194206Ssimon#ifndef OPENSSL_NO_TLSEXT 602296341Sdelphij int r; 603194206Ssimon#endif 604238405Sjkim 605296341Sdelphij if (len < 0 || len > SSL_MAX_SSL_SESSION_ID_LENGTH) 606296341Sdelphij goto err; 607238405Sjkim 608306230Sdelphij if (limit - session_id < len) { 609296341Sdelphij fatal = 1; 610296341Sdelphij goto err; 611296341Sdelphij } 612238405Sjkim 613296341Sdelphij if (len == 0) 614296341Sdelphij try_session_cache = 0; 615296341Sdelphij 616194206Ssimon#ifndef OPENSSL_NO_TLSEXT 617296341Sdelphij /* sets s->tlsext_ticket_expected */ 618296341Sdelphij r = tls1_process_ticket(s, session_id, len, limit, &ret); 619296341Sdelphij switch (r) { 620296341Sdelphij case -1: /* Error during processing */ 621296341Sdelphij fatal = 1; 622296341Sdelphij goto err; 623296341Sdelphij case 0: /* No ticket found */ 624296341Sdelphij case 1: /* Zero length ticket found */ 625296341Sdelphij break; /* Ok to carry on processing session id. */ 626296341Sdelphij case 2: /* Ticket found but not decrypted. */ 627296341Sdelphij case 3: /* Ticket decrypted, *ret has been set. */ 628296341Sdelphij try_session_cache = 0; 629296341Sdelphij break; 630296341Sdelphij default: 631296341Sdelphij abort(); 632296341Sdelphij } 633194206Ssimon#endif 634238405Sjkim 635296341Sdelphij if (try_session_cache && 636296341Sdelphij ret == NULL && 637296341Sdelphij !(s->session_ctx->session_cache_mode & 638296341Sdelphij SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { 639296341Sdelphij SSL_SESSION data; 640296341Sdelphij data.ssl_version = s->version; 641296341Sdelphij data.session_id_length = len; 642296341Sdelphij if (len == 0) 643296341Sdelphij return 0; 644296341Sdelphij memcpy(data.session_id, session_id, len); 645296341Sdelphij CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); 646296341Sdelphij ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); 647296341Sdelphij if (ret != NULL) { 648296341Sdelphij /* don't allow other threads to steal it: */ 649296341Sdelphij CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION); 650296341Sdelphij } 651296341Sdelphij CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); 652296341Sdelphij if (ret == NULL) 653296341Sdelphij s->session_ctx->stats.sess_miss++; 654296341Sdelphij } 65555714Skris 656296341Sdelphij if (try_session_cache && 657296341Sdelphij ret == NULL && s->session_ctx->get_session_cb != NULL) { 658296341Sdelphij int copy = 1; 65955714Skris 660296341Sdelphij if ((ret = s->session_ctx->get_session_cb(s, session_id, len, ©))) { 661296341Sdelphij s->session_ctx->stats.sess_cb_hit++; 66255714Skris 663296341Sdelphij /* 664296341Sdelphij * Increment reference count now if the session callback asks us 665296341Sdelphij * to do so (note that if the session structures returned by the 666296341Sdelphij * callback are shared between threads, it must handle the 667296341Sdelphij * reference count itself [i.e. copy == 0], or things won't be 668296341Sdelphij * thread-safe). 669296341Sdelphij */ 670296341Sdelphij if (copy) 671296341Sdelphij CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION); 67255714Skris 673296341Sdelphij /* 674296341Sdelphij * Add the externally cached session to the internal cache as 675296341Sdelphij * well if and only if we are supposed to. 676296341Sdelphij */ 677296341Sdelphij if (! 678296341Sdelphij (s->session_ctx->session_cache_mode & 679296341Sdelphij SSL_SESS_CACHE_NO_INTERNAL_STORE)) 680296341Sdelphij /* 681296341Sdelphij * The following should not return 1, otherwise, things are 682296341Sdelphij * very strange 683296341Sdelphij */ 684296341Sdelphij SSL_CTX_add_session(s->session_ctx, ret); 685296341Sdelphij } 686296341Sdelphij } 68755714Skris 688296341Sdelphij if (ret == NULL) 689296341Sdelphij goto err; 690238405Sjkim 691296341Sdelphij /* Now ret is non-NULL and we own one of its reference counts. */ 69255714Skris 693296341Sdelphij if (ret->sid_ctx_length != s->sid_ctx_length 694296341Sdelphij || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { 695296341Sdelphij /* 696296341Sdelphij * We have the session requested by the client, but we don't want to 697296341Sdelphij * use it in this context. 698296341Sdelphij */ 699296341Sdelphij goto err; /* treat like cache miss */ 700296341Sdelphij } 70155714Skris 702296341Sdelphij if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { 703296341Sdelphij /* 704296341Sdelphij * We can't be sure if this session is being used out of context, 705296341Sdelphij * which is especially important for SSL_VERIFY_PEER. The application 706296341Sdelphij * should have used SSL[_CTX]_set_session_id_context. For this error 707296341Sdelphij * case, we generate an error instead of treating the event like a 708296341Sdelphij * cache miss (otherwise it would be easy for applications to 709296341Sdelphij * effectively disable the session cache by accident without anyone 710296341Sdelphij * noticing). 711296341Sdelphij */ 71255714Skris 713296341Sdelphij SSLerr(SSL_F_SSL_GET_PREV_SESSION, 714296341Sdelphij SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 715296341Sdelphij fatal = 1; 716296341Sdelphij goto err; 717296341Sdelphij } 71855714Skris 719296341Sdelphij if (ret->cipher == NULL) { 720296341Sdelphij unsigned char buf[5], *p; 721296341Sdelphij unsigned long l; 72255714Skris 723296341Sdelphij p = buf; 724296341Sdelphij l = ret->cipher_id; 725296341Sdelphij l2n(l, p); 726296341Sdelphij if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR) 727296341Sdelphij ret->cipher = ssl_get_cipher_by_char(s, &(buf[2])); 728296341Sdelphij else 729296341Sdelphij ret->cipher = ssl_get_cipher_by_char(s, &(buf[1])); 730296341Sdelphij if (ret->cipher == NULL) 731296341Sdelphij goto err; 732296341Sdelphij } 73355714Skris 734296341Sdelphij if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */ 735296341Sdelphij s->session_ctx->stats.sess_timeout++; 736296341Sdelphij if (try_session_cache) { 737296341Sdelphij /* session was from the cache, so remove it */ 738296341Sdelphij SSL_CTX_remove_session(s->session_ctx, ret); 739296341Sdelphij } 740296341Sdelphij goto err; 741296341Sdelphij } 742296341Sdelphij 743296341Sdelphij s->session_ctx->stats.sess_hit++; 744296341Sdelphij 745296341Sdelphij if (s->session != NULL) 746296341Sdelphij SSL_SESSION_free(s->session); 747296341Sdelphij s->session = ret; 748296341Sdelphij s->verify_result = s->session->verify_result; 749296341Sdelphij return 1; 750296341Sdelphij 75155714Skris err: 752296341Sdelphij if (ret != NULL) { 753296341Sdelphij SSL_SESSION_free(ret); 754238405Sjkim#ifndef OPENSSL_NO_TLSEXT 755296341Sdelphij if (!try_session_cache) { 756296341Sdelphij /* 757296341Sdelphij * The session was from a ticket, so we should issue a ticket for 758296341Sdelphij * the new session 759296341Sdelphij */ 760296341Sdelphij s->tlsext_ticket_expected = 1; 761296341Sdelphij } 762238405Sjkim#endif 763296341Sdelphij } 764296341Sdelphij if (fatal) 765296341Sdelphij return -1; 766296341Sdelphij else 767296341Sdelphij return 0; 768296341Sdelphij} 76955714Skris 77055714Skrisint SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) 771296341Sdelphij{ 772296341Sdelphij int ret = 0; 773296341Sdelphij SSL_SESSION *s; 77455714Skris 775296341Sdelphij /* 776296341Sdelphij * add just 1 reference count for the SSL_CTX's session cache even though 777296341Sdelphij * it has two ways of access: each session is in a doubly linked list and 778296341Sdelphij * an lhash 779296341Sdelphij */ 780296341Sdelphij CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION); 781296341Sdelphij /* 782296341Sdelphij * if session c is in already in cache, we take back the increment later 783296341Sdelphij */ 78455714Skris 785296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 786296341Sdelphij s = lh_SSL_SESSION_insert(ctx->sessions, c); 78759191Skris 788296341Sdelphij /* 789296341Sdelphij * s != NULL iff we already had a session with the given PID. In this 790296341Sdelphij * case, s == c should hold (then we did not really modify 791296341Sdelphij * ctx->sessions), or we're in trouble. 792296341Sdelphij */ 793296341Sdelphij if (s != NULL && s != c) { 794296341Sdelphij /* We *are* in trouble ... */ 795296341Sdelphij SSL_SESSION_list_remove(ctx, s); 796296341Sdelphij SSL_SESSION_free(s); 797296341Sdelphij /* 798296341Sdelphij * ... so pretend the other session did not exist in cache (we cannot 799296341Sdelphij * handle two SSL_SESSION structures with identical session ID in the 800296341Sdelphij * same cache, which could happen e.g. when two threads concurrently 801296341Sdelphij * obtain the same session from an external cache) 802296341Sdelphij */ 803296341Sdelphij s = NULL; 804296341Sdelphij } 80555714Skris 806296341Sdelphij /* Put at the head of the queue unless it is already in the cache */ 807296341Sdelphij if (s == NULL) 808296341Sdelphij SSL_SESSION_list_add(ctx, c); 80959191Skris 810296341Sdelphij if (s != NULL) { 811296341Sdelphij /* 812296341Sdelphij * existing cache entry -- decrement previously incremented reference 813296341Sdelphij * count because it already takes into account the cache 814296341Sdelphij */ 81555714Skris 816296341Sdelphij SSL_SESSION_free(s); /* s == c */ 817296341Sdelphij ret = 0; 818296341Sdelphij } else { 819296341Sdelphij /* 820296341Sdelphij * new cache entry -- remove old ones if cache has become too large 821296341Sdelphij */ 82255714Skris 823296341Sdelphij ret = 1; 824296341Sdelphij 825296341Sdelphij if (SSL_CTX_sess_get_cache_size(ctx) > 0) { 826296341Sdelphij while (SSL_CTX_sess_number(ctx) > 827296341Sdelphij SSL_CTX_sess_get_cache_size(ctx)) { 828296341Sdelphij if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) 829296341Sdelphij break; 830296341Sdelphij else 831296341Sdelphij ctx->stats.sess_cache_full++; 832296341Sdelphij } 833296341Sdelphij } 834296341Sdelphij } 835296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 836296341Sdelphij return (ret); 837296341Sdelphij} 838296341Sdelphij 83955714Skrisint SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) 84055714Skris{ 841296341Sdelphij return remove_session_lock(ctx, c, 1); 84255714Skris} 84355714Skris 84455714Skrisstatic int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) 845296341Sdelphij{ 846296341Sdelphij SSL_SESSION *r; 847296341Sdelphij int ret = 0; 84855714Skris 849296341Sdelphij if ((c != NULL) && (c->session_id_length != 0)) { 850296341Sdelphij if (lck) 851296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 852296341Sdelphij if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) { 853296341Sdelphij ret = 1; 854296341Sdelphij r = lh_SSL_SESSION_delete(ctx->sessions, c); 855296341Sdelphij SSL_SESSION_list_remove(ctx, c); 856296341Sdelphij } 85755714Skris 858296341Sdelphij if (lck) 859296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 86055714Skris 861296341Sdelphij if (ret) { 862296341Sdelphij r->not_resumable = 1; 863296341Sdelphij if (ctx->remove_session_cb != NULL) 864296341Sdelphij ctx->remove_session_cb(ctx, r); 865296341Sdelphij SSL_SESSION_free(r); 866296341Sdelphij } 867296341Sdelphij } else 868296341Sdelphij ret = 0; 869296341Sdelphij return (ret); 870296341Sdelphij} 87155714Skris 87255714Skrisvoid SSL_SESSION_free(SSL_SESSION *ss) 873296341Sdelphij{ 874296341Sdelphij int i; 87555714Skris 876296341Sdelphij if (ss == NULL) 877296341Sdelphij return; 87855714Skris 879296341Sdelphij i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION); 88055714Skris#ifdef REF_PRINT 881296341Sdelphij REF_PRINT("SSL_SESSION", ss); 88255714Skris#endif 883296341Sdelphij if (i > 0) 884296341Sdelphij return; 88555714Skris#ifdef REF_CHECK 886296341Sdelphij if (i < 0) { 887296341Sdelphij fprintf(stderr, "SSL_SESSION_free, bad reference count\n"); 888296341Sdelphij abort(); /* ok */ 889296341Sdelphij } 89055714Skris#endif 89155714Skris 892296341Sdelphij CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 89355714Skris 894296341Sdelphij OPENSSL_cleanse(ss->key_arg, sizeof ss->key_arg); 895296341Sdelphij OPENSSL_cleanse(ss->master_key, sizeof ss->master_key); 896296341Sdelphij OPENSSL_cleanse(ss->session_id, sizeof ss->session_id); 897296341Sdelphij if (ss->sess_cert != NULL) 898296341Sdelphij ssl_sess_cert_free(ss->sess_cert); 899296341Sdelphij if (ss->peer != NULL) 900296341Sdelphij X509_free(ss->peer); 901296341Sdelphij if (ss->ciphers != NULL) 902296341Sdelphij sk_SSL_CIPHER_free(ss->ciphers); 903194206Ssimon#ifndef OPENSSL_NO_TLSEXT 904296341Sdelphij if (ss->tlsext_hostname != NULL) 905296341Sdelphij OPENSSL_free(ss->tlsext_hostname); 906296341Sdelphij if (ss->tlsext_tick != NULL) 907296341Sdelphij OPENSSL_free(ss->tlsext_tick); 908296341Sdelphij# ifndef OPENSSL_NO_EC 909296341Sdelphij ss->tlsext_ecpointformatlist_length = 0; 910296341Sdelphij if (ss->tlsext_ecpointformatlist != NULL) 911296341Sdelphij OPENSSL_free(ss->tlsext_ecpointformatlist); 912296341Sdelphij ss->tlsext_ellipticcurvelist_length = 0; 913296341Sdelphij if (ss->tlsext_ellipticcurvelist != NULL) 914296341Sdelphij OPENSSL_free(ss->tlsext_ellipticcurvelist); 915296341Sdelphij# endif /* OPENSSL_NO_EC */ 916194206Ssimon#endif 917238405Sjkim#ifndef OPENSSL_NO_PSK 918296341Sdelphij if (ss->psk_identity_hint != NULL) 919296341Sdelphij OPENSSL_free(ss->psk_identity_hint); 920296341Sdelphij if (ss->psk_identity != NULL) 921296341Sdelphij OPENSSL_free(ss->psk_identity); 922238405Sjkim#endif 923238405Sjkim#ifndef OPENSSL_NO_SRP 924296341Sdelphij if (ss->srp_username != NULL) 925296341Sdelphij OPENSSL_free(ss->srp_username); 926238405Sjkim#endif 927296341Sdelphij OPENSSL_cleanse(ss, sizeof(*ss)); 928296341Sdelphij OPENSSL_free(ss); 929296341Sdelphij} 93055714Skris 93155714Skrisint SSL_set_session(SSL *s, SSL_SESSION *session) 932296341Sdelphij{ 933296341Sdelphij int ret = 0; 934296341Sdelphij const SSL_METHOD *meth; 93555714Skris 936296341Sdelphij if (session != NULL) { 937296341Sdelphij meth = s->ctx->method->get_ssl_method(session->ssl_version); 938296341Sdelphij if (meth == NULL) 939296341Sdelphij meth = s->method->get_ssl_method(session->ssl_version); 940296341Sdelphij if (meth == NULL) { 941296341Sdelphij SSLerr(SSL_F_SSL_SET_SESSION, SSL_R_UNABLE_TO_FIND_SSL_METHOD); 942296341Sdelphij return (0); 943296341Sdelphij } 94455714Skris 945296341Sdelphij if (meth != s->method) { 946296341Sdelphij if (!SSL_set_ssl_method(s, meth)) 947296341Sdelphij return (0); 948296341Sdelphij } 949109998Smarkm#ifndef OPENSSL_NO_KRB5 950296341Sdelphij if (s->kssl_ctx && !s->kssl_ctx->client_princ && 951296341Sdelphij session->krb5_client_princ_len > 0) { 952296341Sdelphij s->kssl_ctx->client_princ = 953296341Sdelphij (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1); 954296341Sdelphij memcpy(s->kssl_ctx->client_princ, session->krb5_client_princ, 955296341Sdelphij session->krb5_client_princ_len); 956296341Sdelphij s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0'; 957296341Sdelphij } 958296341Sdelphij#endif /* OPENSSL_NO_KRB5 */ 959109998Smarkm 960296341Sdelphij /* CRYPTO_w_lock(CRYPTO_LOCK_SSL); */ 961296341Sdelphij CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION); 962296341Sdelphij if (s->session != NULL) 963296341Sdelphij SSL_SESSION_free(s->session); 964296341Sdelphij s->session = session; 965296341Sdelphij s->verify_result = s->session->verify_result; 966296341Sdelphij /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL); */ 967296341Sdelphij ret = 1; 968296341Sdelphij } else { 969296341Sdelphij if (s->session != NULL) { 970296341Sdelphij SSL_SESSION_free(s->session); 971296341Sdelphij s->session = NULL; 972296341Sdelphij } 97355714Skris 974296341Sdelphij meth = s->ctx->method; 975296341Sdelphij if (meth != s->method) { 976296341Sdelphij if (!SSL_set_ssl_method(s, meth)) 977296341Sdelphij return (0); 978296341Sdelphij } 979296341Sdelphij ret = 1; 980296341Sdelphij } 981296341Sdelphij return (ret); 982296341Sdelphij} 98355714Skris 98455714Skrislong SSL_SESSION_set_timeout(SSL_SESSION *s, long t) 985296341Sdelphij{ 986296341Sdelphij if (s == NULL) 987296341Sdelphij return (0); 988296341Sdelphij s->timeout = t; 989296341Sdelphij return (1); 990296341Sdelphij} 99155714Skris 992160814Ssimonlong SSL_SESSION_get_timeout(const SSL_SESSION *s) 993296341Sdelphij{ 994296341Sdelphij if (s == NULL) 995296341Sdelphij return (0); 996296341Sdelphij return (s->timeout); 997296341Sdelphij} 99855714Skris 999160814Ssimonlong SSL_SESSION_get_time(const SSL_SESSION *s) 1000296341Sdelphij{ 1001296341Sdelphij if (s == NULL) 1002296341Sdelphij return (0); 1003296341Sdelphij return (s->time); 1004296341Sdelphij} 100555714Skris 100655714Skrislong SSL_SESSION_set_time(SSL_SESSION *s, long t) 1007296341Sdelphij{ 1008296341Sdelphij if (s == NULL) 1009296341Sdelphij return (0); 1010296341Sdelphij s->time = t; 1011296341Sdelphij return (t); 1012296341Sdelphij} 101355714Skris 1014238405SjkimX509 *SSL_SESSION_get0_peer(SSL_SESSION *s) 1015296341Sdelphij{ 1016296341Sdelphij return s->peer; 1017296341Sdelphij} 1018238405Sjkim 1019296341Sdelphijint SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, 1020296341Sdelphij unsigned int sid_ctx_len) 1021296341Sdelphij{ 1022296341Sdelphij if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 1023296341Sdelphij SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT, 1024296341Sdelphij SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 1025296341Sdelphij return 0; 1026296341Sdelphij } 1027296341Sdelphij s->sid_ctx_length = sid_ctx_len; 1028296341Sdelphij memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); 1029238405Sjkim 1030296341Sdelphij return 1; 1031296341Sdelphij} 1032238405Sjkim 103355714Skrislong SSL_CTX_set_timeout(SSL_CTX *s, long t) 1034296341Sdelphij{ 1035296341Sdelphij long l; 1036296341Sdelphij if (s == NULL) 1037296341Sdelphij return (0); 1038296341Sdelphij l = s->session_timeout; 1039296341Sdelphij s->session_timeout = t; 1040296341Sdelphij return (l); 1041296341Sdelphij} 104255714Skris 1043160814Ssimonlong SSL_CTX_get_timeout(const SSL_CTX *s) 1044296341Sdelphij{ 1045296341Sdelphij if (s == NULL) 1046296341Sdelphij return (0); 1047296341Sdelphij return (s->session_timeout); 1048296341Sdelphij} 104955714Skris 1050238405Sjkim#ifndef OPENSSL_NO_TLSEXT 1051296341Sdelphijint SSL_set_session_secret_cb(SSL *s, 1052296341Sdelphij int (*tls_session_secret_cb) (SSL *s, 1053296341Sdelphij void *secret, 1054296341Sdelphij int *secret_len, 1055296341Sdelphij STACK_OF(SSL_CIPHER) 1056296341Sdelphij *peer_ciphers, 1057296341Sdelphij SSL_CIPHER 1058296341Sdelphij **cipher, 1059296341Sdelphij void *arg), 1060296341Sdelphij void *arg) 1061296341Sdelphij{ 1062296341Sdelphij if (s == NULL) 1063296341Sdelphij return (0); 1064296341Sdelphij s->tls_session_secret_cb = tls_session_secret_cb; 1065296341Sdelphij s->tls_session_secret_cb_arg = arg; 1066296341Sdelphij return (1); 1067296341Sdelphij} 1068238405Sjkim 1069238405Sjkimint SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 1070296341Sdelphij void *arg) 1071296341Sdelphij{ 1072296341Sdelphij if (s == NULL) 1073296341Sdelphij return (0); 1074296341Sdelphij s->tls_session_ticket_ext_cb = cb; 1075296341Sdelphij s->tls_session_ticket_ext_cb_arg = arg; 1076296341Sdelphij return (1); 1077296341Sdelphij} 1078238405Sjkim 1079238405Sjkimint SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) 1080296341Sdelphij{ 1081296341Sdelphij if (s->version >= TLS1_VERSION) { 1082296341Sdelphij if (s->tlsext_session_ticket) { 1083296341Sdelphij OPENSSL_free(s->tlsext_session_ticket); 1084296341Sdelphij s->tlsext_session_ticket = NULL; 1085296341Sdelphij } 1086238405Sjkim 1087296341Sdelphij s->tlsext_session_ticket = 1088296341Sdelphij OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); 1089296341Sdelphij if (!s->tlsext_session_ticket) { 1090296341Sdelphij SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE); 1091296341Sdelphij return 0; 1092296341Sdelphij } 1093238405Sjkim 1094296341Sdelphij if (ext_data) { 1095296341Sdelphij s->tlsext_session_ticket->length = ext_len; 1096296341Sdelphij s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1; 1097296341Sdelphij memcpy(s->tlsext_session_ticket->data, ext_data, ext_len); 1098296341Sdelphij } else { 1099296341Sdelphij s->tlsext_session_ticket->length = 0; 1100296341Sdelphij s->tlsext_session_ticket->data = NULL; 1101296341Sdelphij } 1102238405Sjkim 1103296341Sdelphij return 1; 1104296341Sdelphij } 1105238405Sjkim 1106296341Sdelphij return 0; 1107296341Sdelphij} 1108296341Sdelphij#endif /* OPENSSL_NO_TLSEXT */ 1109238405Sjkim 1110296341Sdelphijtypedef struct timeout_param_st { 1111296341Sdelphij SSL_CTX *ctx; 1112296341Sdelphij long time; 1113296341Sdelphij LHASH_OF(SSL_SESSION) *cache; 1114296341Sdelphij} TIMEOUT_PARAM; 111555714Skris 1116238405Sjkimstatic void timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p) 1117296341Sdelphij{ 1118296341Sdelphij if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */ 1119296341Sdelphij /* 1120296341Sdelphij * The reason we don't call SSL_CTX_remove_session() is to save on 1121296341Sdelphij * locking overhead 1122296341Sdelphij */ 1123296341Sdelphij (void)lh_SSL_SESSION_delete(p->cache, s); 1124296341Sdelphij SSL_SESSION_list_remove(p->ctx, s); 1125296341Sdelphij s->not_resumable = 1; 1126296341Sdelphij if (p->ctx->remove_session_cb != NULL) 1127296341Sdelphij p->ctx->remove_session_cb(p->ctx, s); 1128296341Sdelphij SSL_SESSION_free(s); 1129296341Sdelphij } 1130296341Sdelphij} 113155714Skris 1132238405Sjkimstatic IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION, TIMEOUT_PARAM) 1133109998Smarkm 113455714Skrisvoid SSL_CTX_flush_sessions(SSL_CTX *s, long t) 1135296341Sdelphij{ 1136296341Sdelphij unsigned long i; 1137296341Sdelphij TIMEOUT_PARAM tp; 113855714Skris 1139296341Sdelphij tp.ctx = s; 1140296341Sdelphij tp.cache = s->sessions; 1141296341Sdelphij if (tp.cache == NULL) 1142296341Sdelphij return; 1143296341Sdelphij tp.time = t; 1144296341Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); 1145296341Sdelphij i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load; 1146296341Sdelphij CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0; 1147296341Sdelphij lh_SSL_SESSION_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), 1148296341Sdelphij TIMEOUT_PARAM, &tp); 1149296341Sdelphij CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i; 1150296341Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); 1151296341Sdelphij} 115255714Skris 115355714Skrisint ssl_clear_bad_session(SSL *s) 1154296341Sdelphij{ 1155296341Sdelphij if ((s->session != NULL) && 1156296341Sdelphij !(s->shutdown & SSL_SENT_SHUTDOWN) && 1157296341Sdelphij !(SSL_in_init(s) || SSL_in_before(s))) { 1158296341Sdelphij SSL_CTX_remove_session(s->ctx, s->session); 1159296341Sdelphij return (1); 1160296341Sdelphij } else 1161296341Sdelphij return (0); 1162296341Sdelphij} 116355714Skris 116455714Skris/* locked by SSL_CTX in the calling function */ 116555714Skrisstatic void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) 1166296341Sdelphij{ 1167296341Sdelphij if ((s->next == NULL) || (s->prev == NULL)) 1168296341Sdelphij return; 116955714Skris 1170296341Sdelphij if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { 1171296341Sdelphij /* last element in list */ 1172296341Sdelphij if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1173296341Sdelphij /* only one element in list */ 1174296341Sdelphij ctx->session_cache_head = NULL; 1175296341Sdelphij ctx->session_cache_tail = NULL; 1176296341Sdelphij } else { 1177296341Sdelphij ctx->session_cache_tail = s->prev; 1178296341Sdelphij s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1179296341Sdelphij } 1180296341Sdelphij } else { 1181296341Sdelphij if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1182296341Sdelphij /* first element in list */ 1183296341Sdelphij ctx->session_cache_head = s->next; 1184296341Sdelphij s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1185296341Sdelphij } else { 1186296341Sdelphij /* middle of list */ 1187296341Sdelphij s->next->prev = s->prev; 1188296341Sdelphij s->prev->next = s->next; 1189296341Sdelphij } 1190296341Sdelphij } 1191296341Sdelphij s->prev = s->next = NULL; 1192296341Sdelphij} 119355714Skris 119455714Skrisstatic void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) 1195296341Sdelphij{ 1196296341Sdelphij if ((s->next != NULL) && (s->prev != NULL)) 1197296341Sdelphij SSL_SESSION_list_remove(ctx, s); 119855714Skris 1199296341Sdelphij if (ctx->session_cache_head == NULL) { 1200296341Sdelphij ctx->session_cache_head = s; 1201296341Sdelphij ctx->session_cache_tail = s; 1202296341Sdelphij s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1203296341Sdelphij s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1204296341Sdelphij } else { 1205296341Sdelphij s->next = ctx->session_cache_head; 1206296341Sdelphij s->next->prev = s; 1207296341Sdelphij s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1208296341Sdelphij ctx->session_cache_head = s; 1209296341Sdelphij } 1210296341Sdelphij} 121155714Skris 1212167612Ssimonvoid SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, 1213296341Sdelphij int (*cb) (struct ssl_st *ssl, 1214296341Sdelphij SSL_SESSION *sess)) 1215296341Sdelphij{ 1216296341Sdelphij ctx->new_session_cb = cb; 1217296341Sdelphij} 1218167612Ssimon 1219296341Sdelphijint (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { 1220296341Sdelphij return ctx->new_session_cb; 1221296341Sdelphij} 1222167612Ssimon 1223167612Ssimonvoid SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, 1224296341Sdelphij void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) 1225296341Sdelphij{ 1226296341Sdelphij ctx->remove_session_cb = cb; 1227296341Sdelphij} 1228167612Ssimon 1229296341Sdelphijvoid (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, 1230296341Sdelphij SSL_SESSION *sess) { 1231296341Sdelphij return ctx->remove_session_cb; 1232296341Sdelphij} 1233167612Ssimon 1234167612Ssimonvoid SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, 1235296341Sdelphij SSL_SESSION *(*cb) (struct ssl_st *ssl, 1236296341Sdelphij unsigned char *data, int len, 1237296341Sdelphij int *copy)) 1238296341Sdelphij{ 1239296341Sdelphij ctx->get_session_cb = cb; 1240296341Sdelphij} 1241167612Ssimon 1242296341SdelphijSSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, 1243296341Sdelphij unsigned char *data, 1244296341Sdelphij int len, int *copy) { 1245296341Sdelphij return ctx->get_session_cb; 1246296341Sdelphij} 1247167612Ssimon 1248296341Sdelphijvoid SSL_CTX_set_info_callback(SSL_CTX *ctx, 1249296341Sdelphij void (*cb) (const SSL *ssl, int type, int val)) 1250296341Sdelphij{ 1251296341Sdelphij ctx->info_callback = cb; 1252296341Sdelphij} 1253167612Ssimon 1254296341Sdelphijvoid (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, 1255296341Sdelphij int val) { 1256296341Sdelphij return ctx->info_callback; 1257296341Sdelphij} 1258167612Ssimon 1259167612Ssimonvoid SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, 1260296341Sdelphij int (*cb) (SSL *ssl, X509 **x509, 1261296341Sdelphij EVP_PKEY **pkey)) 1262296341Sdelphij{ 1263296341Sdelphij ctx->client_cert_cb = cb; 1264296341Sdelphij} 1265167612Ssimon 1266296341Sdelphijint (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, 1267296341Sdelphij EVP_PKEY **pkey) { 1268296341Sdelphij return ctx->client_cert_cb; 1269296341Sdelphij} 1270167612Ssimon 1271194206Ssimon#ifndef OPENSSL_NO_ENGINE 1272194206Ssimonint SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e) 1273296341Sdelphij{ 1274296341Sdelphij if (!ENGINE_init(e)) { 1275296341Sdelphij SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB); 1276296341Sdelphij return 0; 1277296341Sdelphij } 1278296341Sdelphij if (!ENGINE_get_ssl_client_cert_function(e)) { 1279296341Sdelphij SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, 1280296341Sdelphij SSL_R_NO_CLIENT_CERT_METHOD); 1281296341Sdelphij ENGINE_finish(e); 1282296341Sdelphij return 0; 1283296341Sdelphij } 1284296341Sdelphij ctx->client_cert_engine = e; 1285296341Sdelphij return 1; 1286296341Sdelphij} 1287194206Ssimon#endif 1288194206Ssimon 1289167612Ssimonvoid SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, 1290296341Sdelphij int (*cb) (SSL *ssl, 1291296341Sdelphij unsigned char *cookie, 1292296341Sdelphij unsigned int *cookie_len)) 1293296341Sdelphij{ 1294296341Sdelphij ctx->app_gen_cookie_cb = cb; 1295296341Sdelphij} 1296167612Ssimon 1297167612Ssimonvoid SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, 1298296341Sdelphij int (*cb) (SSL *ssl, unsigned char *cookie, 1299296341Sdelphij unsigned int cookie_len)) 1300296341Sdelphij{ 1301296341Sdelphij ctx->app_verify_cookie_cb = cb; 1302296341Sdelphij} 1303167612Ssimon 1304296341SdelphijIMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, 1305296341Sdelphij SSL_SESSION) 1306