15af53050Schristos /* 2b46c97feSchristos * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. 3e0ea3921Schristos * Copyright 2005 Nokia. All rights reserved. 4a89c9211Schristos * 58fbed61eSchristos * Licensed under the Apache License 2.0 (the "License"). You may not use 65af53050Schristos * this file except in compliance with the License. You can obtain a copy 75af53050Schristos * in the file LICENSE in the source distribution or at 85af53050Schristos * https://www.openssl.org/source/license.html 9a89c9211Schristos */ 105af53050Schristos 118fbed61eSchristos #if defined(__TANDEM) && defined(_SPT_MODEL_) 128fbed61eSchristos # include <spthread.h> 138fbed61eSchristos # include <spt_extensions.h> /* timeval */ 148fbed61eSchristos #endif 15a89c9211Schristos #include <stdio.h> 16a89c9211Schristos #include <openssl/rand.h> 17a89c9211Schristos #include <openssl/engine.h> 18e0ea3921Schristos #include "internal/refcount.h" 19e0ea3921Schristos #include "internal/cryptlib.h" 2052629741Schristos #include "ssl_local.h" 2152629741Schristos #include "statem/statem_local.h" 22a89c9211Schristos 23a89c9211Schristos static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); 24a89c9211Schristos static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); 25a89c9211Schristos static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); 26a89c9211Schristos 278fbed61eSchristos DEFINE_STACK_OF(SSL_SESSION) 288fbed61eSchristos 298fbed61eSchristos __owur static int sess_timedout(time_t t, SSL_SESSION *ss) 308fbed61eSchristos { 318fbed61eSchristos /* if timeout overflowed, it can never timeout! */ 328fbed61eSchristos if (ss->timeout_ovf) 338fbed61eSchristos return 0; 348fbed61eSchristos return t > ss->calc_timeout; 358fbed61eSchristos } 368fbed61eSchristos 378fbed61eSchristos /* 388fbed61eSchristos * Returns -1/0/+1 as other XXXcmp-type functions 398fbed61eSchristos * Takes overflow of calculated timeout into consideration 408fbed61eSchristos */ 418fbed61eSchristos __owur static int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b) 428fbed61eSchristos { 438fbed61eSchristos /* if only one overflowed, then it is greater */ 448fbed61eSchristos if (a->timeout_ovf && !b->timeout_ovf) 458fbed61eSchristos return 1; 468fbed61eSchristos if (!a->timeout_ovf && b->timeout_ovf) 478fbed61eSchristos return -1; 488fbed61eSchristos /* No overflow, or both overflowed, so straight compare is safe */ 498fbed61eSchristos if (a->calc_timeout < b->calc_timeout) 508fbed61eSchristos return -1; 518fbed61eSchristos if (a->calc_timeout > b->calc_timeout) 528fbed61eSchristos return 1; 538fbed61eSchristos return 0; 548fbed61eSchristos } 558fbed61eSchristos 56*7d9ffdb3Schristos #ifdef __DJGPP__ /* time_t is unsigned on djgpp, it's signed anywhere else */ 57*7d9ffdb3Schristos # define TMAX(_type_) ((time_t)-1) 58*7d9ffdb3Schristos #else 59*7d9ffdb3Schristos # define TMAX(_type_) ((time_t)(((_type_)-1) >> 1)) 60*7d9ffdb3Schristos #endif 61*7d9ffdb3Schristos 62*7d9ffdb3Schristos #define CALCULATE_TIMEOUT(_ss_, _type_) do { \ 63*7d9ffdb3Schristos _type_ overflow; \ 64*7d9ffdb3Schristos time_t tmax = TMAX(_type_); \ 65*7d9ffdb3Schristos overflow = (_type_)tmax - (_type_)(_ss_)->time; \ 66*7d9ffdb3Schristos if ((_ss_)->timeout > (time_t)overflow) { \ 67*7d9ffdb3Schristos (_ss_)->timeout_ovf = 1; \ 68*7d9ffdb3Schristos (_ss_)->calc_timeout = (_ss_)->timeout - (time_t)overflow; \ 69*7d9ffdb3Schristos } else { \ 70*7d9ffdb3Schristos (_ss_)->timeout_ovf = 0; \ 71*7d9ffdb3Schristos (_ss_)->calc_timeout = (_ss_)->time + (_ss_)->timeout; \ 72*7d9ffdb3Schristos } \ 73*7d9ffdb3Schristos } while (0) 748fbed61eSchristos /* 758fbed61eSchristos * Calculates effective timeout, saving overflow state 768fbed61eSchristos * Locking must be done by the caller of this function 778fbed61eSchristos */ 788fbed61eSchristos void ssl_session_calculate_timeout(SSL_SESSION *ss) 798fbed61eSchristos { 80*7d9ffdb3Schristos 81*7d9ffdb3Schristos if (sizeof(time_t) == 8) 82*7d9ffdb3Schristos CALCULATE_TIMEOUT(ss, uint64_t); 83*7d9ffdb3Schristos else 84*7d9ffdb3Schristos CALCULATE_TIMEOUT(ss, uint32_t); 85*7d9ffdb3Schristos 868fbed61eSchristos /* 878fbed61eSchristos * N.B. Realistic overflow can only occur in our lifetimes on a 888fbed61eSchristos * 32-bit machine in January 2038. 898fbed61eSchristos * However, There are no controls to limit the |timeout| 908fbed61eSchristos * value, except to keep it positive. 918fbed61eSchristos */ 928fbed61eSchristos } 938fbed61eSchristos 94e0ea3921Schristos /* 95e0ea3921Schristos * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because, 96e0ea3921Schristos * unlike in earlier protocol versions, the session ticket may not have been 97e0ea3921Schristos * sent yet even though a handshake has finished. The session ticket data could 98e0ea3921Schristos * come in sometime later...or even change if multiple session ticket messages 99e0ea3921Schristos * are sent from the server. The preferred way for applications to obtain 100e0ea3921Schristos * a resumable session is to use SSL_CTX_sess_set_new_cb(). 101e0ea3921Schristos */ 102e0ea3921Schristos 103a89c9211Schristos SSL_SESSION *SSL_get_session(const SSL *ssl) 104a89c9211Schristos /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ 105a89c9211Schristos { 106e0ea3921Schristos return ssl->session; 107a89c9211Schristos } 108a89c9211Schristos 109a89c9211Schristos SSL_SESSION *SSL_get1_session(SSL *ssl) 110a89c9211Schristos /* variant of SSL_get_session: caller really gets something */ 111a89c9211Schristos { 112a89c9211Schristos SSL_SESSION *sess; 113635165faSspz /* 114635165faSspz * Need to lock this all up rather than just use CRYPTO_add so that 115635165faSspz * somebody doesn't free ssl->session between when we check it's non-null 116635165faSspz * and when we up the reference count. 117635165faSspz */ 1188fbed61eSchristos if (!CRYPTO_THREAD_read_lock(ssl->lock)) 1198fbed61eSchristos return NULL; 120a89c9211Schristos sess = ssl->session; 121a89c9211Schristos if (sess) 1225af53050Schristos SSL_SESSION_up_ref(sess); 1235af53050Schristos CRYPTO_THREAD_unlock(ssl->lock); 1245af53050Schristos return sess; 125a89c9211Schristos } 126a89c9211Schristos 127a89c9211Schristos int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) 128a89c9211Schristos { 129e0ea3921Schristos return CRYPTO_set_ex_data(&s->ex_data, idx, arg); 130a89c9211Schristos } 131a89c9211Schristos 132a89c9211Schristos void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) 133a89c9211Schristos { 134e0ea3921Schristos return CRYPTO_get_ex_data(&s->ex_data, idx); 135a89c9211Schristos } 136a89c9211Schristos 137a89c9211Schristos SSL_SESSION *SSL_SESSION_new(void) 138a89c9211Schristos { 139a89c9211Schristos SSL_SESSION *ss; 140a89c9211Schristos 141e0ea3921Schristos if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL)) 142e0ea3921Schristos return NULL; 143e0ea3921Schristos 1445af53050Schristos ss = OPENSSL_zalloc(sizeof(*ss)); 145635165faSspz if (ss == NULL) { 1468fbed61eSchristos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 1475af53050Schristos return NULL; 148a89c9211Schristos } 149a89c9211Schristos 150*7d9ffdb3Schristos ss->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_UNSPECIFIED; 151a89c9211Schristos ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ 152a89c9211Schristos ss->references = 1; 153a89c9211Schristos ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */ 1548fbed61eSchristos ss->time = time(NULL); 1558fbed61eSchristos ssl_session_calculate_timeout(ss); 1565af53050Schristos ss->lock = CRYPTO_THREAD_lock_new(); 1575af53050Schristos if (ss->lock == NULL) { 1588fbed61eSchristos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 1595af53050Schristos OPENSSL_free(ss); 1605af53050Schristos return NULL; 1615af53050Schristos } 1625af53050Schristos 1635af53050Schristos if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) { 1645af53050Schristos CRYPTO_THREAD_lock_free(ss->lock); 1655af53050Schristos OPENSSL_free(ss); 1665af53050Schristos return NULL; 1675af53050Schristos } 1685af53050Schristos return ss; 169a89c9211Schristos } 170a89c9211Schristos 1719cae6e10Schristos /* 1729cae6e10Schristos * Create a new SSL_SESSION and duplicate the contents of |src| into it. If 1739cae6e10Schristos * ticket == 0 then no ticket information is duplicated, otherwise it is. 1749cae6e10Schristos */ 175b46c97feSchristos static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket) 1769cae6e10Schristos { 1779cae6e10Schristos SSL_SESSION *dest; 1789cae6e10Schristos 1794a7cf967Schristos dest = OPENSSL_malloc(sizeof(*dest)); 1809cae6e10Schristos if (dest == NULL) { 1819cae6e10Schristos goto err; 1829cae6e10Schristos } 1839cae6e10Schristos memcpy(dest, src, sizeof(*dest)); 1849cae6e10Schristos 1859cae6e10Schristos /* 1869cae6e10Schristos * Set the various pointers to NULL so that we can call SSL_SESSION_free in 1879cae6e10Schristos * the case of an error whilst halfway through constructing dest 1889cae6e10Schristos */ 1899cae6e10Schristos #ifndef OPENSSL_NO_PSK 1909cae6e10Schristos dest->psk_identity_hint = NULL; 1919cae6e10Schristos dest->psk_identity = NULL; 1929cae6e10Schristos #endif 193e0ea3921Schristos dest->ext.hostname = NULL; 194e0ea3921Schristos dest->ext.tick = NULL; 195e0ea3921Schristos dest->ext.alpn_selected = NULL; 1969cae6e10Schristos #ifndef OPENSSL_NO_SRP 1979cae6e10Schristos dest->srp_username = NULL; 1989cae6e10Schristos #endif 1995af53050Schristos dest->peer_chain = NULL; 2005af53050Schristos dest->peer = NULL; 201e0ea3921Schristos dest->ticket_appdata = NULL; 2029cae6e10Schristos memset(&dest->ex_data, 0, sizeof(dest->ex_data)); 2039cae6e10Schristos 204d6e24a89Schristos /* As the copy is not in the cache, we remove the associated pointers */ 2059cae6e10Schristos dest->prev = NULL; 2069cae6e10Schristos dest->next = NULL; 207d6e24a89Schristos dest->owner = NULL; 2089cae6e10Schristos 2099cae6e10Schristos dest->references = 1; 2109cae6e10Schristos 2115af53050Schristos dest->lock = CRYPTO_THREAD_lock_new(); 2128dcce544Schristos if (dest->lock == NULL) { 2138dcce544Schristos OPENSSL_free(dest); 2148dcce544Schristos dest = NULL; 2155af53050Schristos goto err; 2168dcce544Schristos } 2179cae6e10Schristos 2185af53050Schristos if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) 2195af53050Schristos goto err; 2209cae6e10Schristos 2215af53050Schristos if (src->peer != NULL) { 2225af53050Schristos if (!X509_up_ref(src->peer)) 2235af53050Schristos goto err; 2245af53050Schristos dest->peer = src->peer; 2255af53050Schristos } 2265af53050Schristos 2275af53050Schristos if (src->peer_chain != NULL) { 2285af53050Schristos dest->peer_chain = X509_chain_up_ref(src->peer_chain); 2295af53050Schristos if (dest->peer_chain == NULL) 2305af53050Schristos goto err; 2315af53050Schristos } 2329cae6e10Schristos #ifndef OPENSSL_NO_PSK 2339cae6e10Schristos if (src->psk_identity_hint) { 2345af53050Schristos dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint); 2359cae6e10Schristos if (dest->psk_identity_hint == NULL) { 2369cae6e10Schristos goto err; 2379cae6e10Schristos } 2389cae6e10Schristos } 2399cae6e10Schristos if (src->psk_identity) { 2405af53050Schristos dest->psk_identity = OPENSSL_strdup(src->psk_identity); 2419cae6e10Schristos if (dest->psk_identity == NULL) { 2429cae6e10Schristos goto err; 2439cae6e10Schristos } 2449cae6e10Schristos } 2459cae6e10Schristos #endif 2469cae6e10Schristos 2479cae6e10Schristos if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, 2489cae6e10Schristos &dest->ex_data, &src->ex_data)) { 2499cae6e10Schristos goto err; 2509cae6e10Schristos } 2519cae6e10Schristos 252e0ea3921Schristos if (src->ext.hostname) { 253e0ea3921Schristos dest->ext.hostname = OPENSSL_strdup(src->ext.hostname); 254e0ea3921Schristos if (dest->ext.hostname == NULL) { 2559cae6e10Schristos goto err; 2569cae6e10Schristos } 2579cae6e10Schristos } 2589cae6e10Schristos 259e0ea3921Schristos if (ticket != 0 && src->ext.tick != NULL) { 260e0ea3921Schristos dest->ext.tick = 261e0ea3921Schristos OPENSSL_memdup(src->ext.tick, src->ext.ticklen); 262e0ea3921Schristos if (dest->ext.tick == NULL) 2639cae6e10Schristos goto err; 2649cae6e10Schristos } else { 265e0ea3921Schristos dest->ext.tick_lifetime_hint = 0; 266e0ea3921Schristos dest->ext.ticklen = 0; 267e0ea3921Schristos } 268e0ea3921Schristos 269e0ea3921Schristos if (src->ext.alpn_selected != NULL) { 270e0ea3921Schristos dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected, 271e0ea3921Schristos src->ext.alpn_selected_len); 272e0ea3921Schristos if (dest->ext.alpn_selected == NULL) 273e0ea3921Schristos goto err; 2749cae6e10Schristos } 2759cae6e10Schristos 2769cae6e10Schristos #ifndef OPENSSL_NO_SRP 2779cae6e10Schristos if (src->srp_username) { 2785af53050Schristos dest->srp_username = OPENSSL_strdup(src->srp_username); 2799cae6e10Schristos if (dest->srp_username == NULL) { 2809cae6e10Schristos goto err; 2819cae6e10Schristos } 2829cae6e10Schristos } 2839cae6e10Schristos #endif 2849cae6e10Schristos 285e0ea3921Schristos if (src->ticket_appdata != NULL) { 286e0ea3921Schristos dest->ticket_appdata = 287e0ea3921Schristos OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len); 288e0ea3921Schristos if (dest->ticket_appdata == NULL) 289e0ea3921Schristos goto err; 290e0ea3921Schristos } 291e0ea3921Schristos 2929cae6e10Schristos return dest; 2939cae6e10Schristos err: 2948fbed61eSchristos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 2959cae6e10Schristos SSL_SESSION_free(dest); 2969cae6e10Schristos return NULL; 2979cae6e10Schristos } 2989cae6e10Schristos 299b46c97feSchristos SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src) 300b46c97feSchristos { 301b46c97feSchristos return ssl_session_dup_intern(src, 1); 302b46c97feSchristos } 303b46c97feSchristos 304b46c97feSchristos /* 305b46c97feSchristos * Used internally when duplicating a session which might be already shared. 306b46c97feSchristos * We will have resumed the original session. Subsequently we might have marked 307b46c97feSchristos * it as non-resumable (e.g. in another thread) - but this copy should be ok to 308b46c97feSchristos * resume from. 309b46c97feSchristos */ 310b46c97feSchristos SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket) 311b46c97feSchristos { 312b46c97feSchristos SSL_SESSION *sess = ssl_session_dup_intern(src, ticket); 313b46c97feSchristos 314b46c97feSchristos if (sess != NULL) 315b46c97feSchristos sess->not_resumable = 0; 316b46c97feSchristos 317b46c97feSchristos return sess; 318b46c97feSchristos } 319b46c97feSchristos 3205af53050Schristos const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) 321a89c9211Schristos { 322a89c9211Schristos if (len) 323e0ea3921Schristos *len = (unsigned int)s->session_id_length; 324a89c9211Schristos return s->session_id; 325a89c9211Schristos } 3265af53050Schristos const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, 3275af53050Schristos unsigned int *len) 3285af53050Schristos { 3295af53050Schristos if (len != NULL) 330e0ea3921Schristos *len = (unsigned int)s->sid_ctx_length; 3315af53050Schristos return s->sid_ctx; 3325af53050Schristos } 333a89c9211Schristos 33432daad53Schristos unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) 33532daad53Schristos { 33632daad53Schristos return s->compress_meth; 33732daad53Schristos } 33832daad53Schristos 339635165faSspz /* 3405af53050Schristos * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling 3415af53050Schristos * the ID with random junk repeatedly until we have no conflict is going to 3425af53050Schristos * complete in one iteration pretty much "most" of the time (btw: 3435af53050Schristos * understatement). So, if it takes us 10 iterations and we still can't avoid 3445af53050Schristos * a conflict - well that's a reasonable point to call it quits. Either the 3455af53050Schristos * RAND code is broken or someone is trying to open roughly very close to 3465af53050Schristos * 2^256 SSL sessions to our server. How you might store that many sessions 3475af53050Schristos * is perhaps a more interesting question ... 348635165faSspz */ 349a89c9211Schristos 350a89c9211Schristos #define MAX_SESS_ID_ATTEMPTS 10 351e0ea3921Schristos static int def_generate_session_id(SSL *ssl, unsigned char *id, 352a89c9211Schristos unsigned int *id_len) 353a89c9211Schristos { 354a89c9211Schristos unsigned int retry = 0; 355a89c9211Schristos do 3568fbed61eSchristos if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0) 357a89c9211Schristos return 0; 358a89c9211Schristos while (SSL_has_matching_session_id(ssl, id, *id_len) && 359a89c9211Schristos (++retry < MAX_SESS_ID_ATTEMPTS)) ; 360a89c9211Schristos if (retry < MAX_SESS_ID_ATTEMPTS) 361a89c9211Schristos return 1; 362a89c9211Schristos /* else - woops a session_id match */ 363635165faSspz /* 364635165faSspz * XXX We should also check the external cache -- but the probability of 365635165faSspz * a collision is negligible, and we could not prevent the concurrent 366635165faSspz * creation of sessions with identical IDs since we currently don't have 367635165faSspz * means to atomically check whether a session ID already exists and make 368635165faSspz * a reservation for it if it does not (this problem applies to the 369635165faSspz * internal cache as well). 370a89c9211Schristos */ 371a89c9211Schristos return 0; 372a89c9211Schristos } 373a89c9211Schristos 374e0ea3921Schristos int ssl_generate_session_id(SSL *s, SSL_SESSION *ss) 375a89c9211Schristos { 376a89c9211Schristos unsigned int tmp; 377a89c9211Schristos GEN_SESSION_CB cb = def_generate_session_id; 378a89c9211Schristos 379e0ea3921Schristos switch (s->version) { 380e0ea3921Schristos case SSL3_VERSION: 381e0ea3921Schristos case TLS1_VERSION: 382e0ea3921Schristos case TLS1_1_VERSION: 383e0ea3921Schristos case TLS1_2_VERSION: 384e0ea3921Schristos case TLS1_3_VERSION: 385e0ea3921Schristos case DTLS1_BAD_VER: 386e0ea3921Schristos case DTLS1_VERSION: 387e0ea3921Schristos case DTLS1_2_VERSION: 388a89c9211Schristos ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; 389e0ea3921Schristos break; 390e0ea3921Schristos default: 3918fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION); 392e0ea3921Schristos return 0; 393a89c9211Schristos } 3945af53050Schristos 395635165faSspz /*- 39605304d43Sspz * If RFC5077 ticket, use empty session ID (as server). 39705304d43Sspz * Note that: 39805304d43Sspz * (a) ssl_get_prev_session() does lookahead into the 39905304d43Sspz * ClientHello extensions to find the session ticket. 4005af53050Schristos * When ssl_get_prev_session() fails, statem_srvr.c calls 4015af53050Schristos * ssl_get_new_session() in tls_process_client_hello(). 40205304d43Sspz * At that point, it has not yet parsed the extensions, 40305304d43Sspz * however, because of the lookahead, it already knows 40405304d43Sspz * whether a ticket is expected or not. 40505304d43Sspz * 4065af53050Schristos * (b) statem_clnt.c calls ssl_get_new_session() before parsing 40705304d43Sspz * ServerHello extensions, and before recording the session 40805304d43Sspz * ID received from the server, so this block is a noop. 40905304d43Sspz */ 410e0ea3921Schristos if (s->ext.ticket_expected) { 411a89c9211Schristos ss->session_id_length = 0; 412e0ea3921Schristos return 1; 413a89c9211Schristos } 4145af53050Schristos 415a89c9211Schristos /* Choose which callback will set the session ID */ 4168fbed61eSchristos if (!CRYPTO_THREAD_read_lock(s->lock)) 4178fbed61eSchristos return 0; 4188fbed61eSchristos if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) { 4198fbed61eSchristos CRYPTO_THREAD_unlock(s->lock); 4208fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 4218fbed61eSchristos SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 4228fbed61eSchristos return 0; 4238fbed61eSchristos } 424a89c9211Schristos if (s->generate_session_id) 425a89c9211Schristos cb = s->generate_session_id; 426a89c9211Schristos else if (s->session_ctx->generate_session_id) 427a89c9211Schristos cb = s->session_ctx->generate_session_id; 4285af53050Schristos CRYPTO_THREAD_unlock(s->session_ctx->lock); 4295af53050Schristos CRYPTO_THREAD_unlock(s->lock); 430a89c9211Schristos /* Choose a session ID */ 4315af53050Schristos memset(ss->session_id, 0, ss->session_id_length); 432e0ea3921Schristos tmp = (int)ss->session_id_length; 433635165faSspz if (!cb(s, ss->session_id, &tmp)) { 434a89c9211Schristos /* The callback failed */ 4358fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 436a89c9211Schristos SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); 437e0ea3921Schristos return 0; 438a89c9211Schristos } 439635165faSspz /* 440635165faSspz * Don't allow the callback to set the session length to zero. nor 441635165faSspz * set it higher than it was. 442635165faSspz */ 4435af53050Schristos if (tmp == 0 || tmp > ss->session_id_length) { 444a89c9211Schristos /* The callback set an illegal length */ 4458fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 446a89c9211Schristos SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); 447e0ea3921Schristos return 0; 448a89c9211Schristos } 449a89c9211Schristos ss->session_id_length = tmp; 450a89c9211Schristos /* Finally, check for a conflict */ 451a89c9211Schristos if (SSL_has_matching_session_id(s, ss->session_id, 452e0ea3921Schristos (unsigned int)ss->session_id_length)) { 4538fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT); 454e0ea3921Schristos return 0; 455a89c9211Schristos } 4565af53050Schristos 457e0ea3921Schristos return 1; 458e0ea3921Schristos } 459e0ea3921Schristos 460e0ea3921Schristos int ssl_get_new_session(SSL *s, int session) 461e0ea3921Schristos { 462e0ea3921Schristos /* This gets used by clients and servers. */ 463e0ea3921Schristos 464e0ea3921Schristos SSL_SESSION *ss = NULL; 465e0ea3921Schristos 466e0ea3921Schristos if ((ss = SSL_SESSION_new()) == NULL) { 4678fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); 468e0ea3921Schristos return 0; 469e0ea3921Schristos } 470e0ea3921Schristos 471e0ea3921Schristos /* If the context has a default timeout, use it */ 472e0ea3921Schristos if (s->session_ctx->session_timeout == 0) 473e0ea3921Schristos ss->timeout = SSL_get_default_timeout(s); 474e0ea3921Schristos else 475e0ea3921Schristos ss->timeout = s->session_ctx->session_timeout; 4768fbed61eSchristos ssl_session_calculate_timeout(ss); 477e0ea3921Schristos 478e0ea3921Schristos SSL_SESSION_free(s->session); 479e0ea3921Schristos s->session = NULL; 480e0ea3921Schristos 481e0ea3921Schristos if (session) { 482e0ea3921Schristos if (SSL_IS_TLS13(s)) { 483e0ea3921Schristos /* 484e0ea3921Schristos * We generate the session id while constructing the 485e0ea3921Schristos * NewSessionTicket in TLSv1.3. 486e0ea3921Schristos */ 487e0ea3921Schristos ss->session_id_length = 0; 488e0ea3921Schristos } else if (!ssl_generate_session_id(s, ss)) { 489e0ea3921Schristos /* SSLfatal() already called */ 490a89c9211Schristos SSL_SESSION_free(ss); 491a89c9211Schristos return 0; 492a89c9211Schristos } 493e0ea3921Schristos 494635165faSspz } else { 495a89c9211Schristos ss->session_id_length = 0; 496a89c9211Schristos } 497a89c9211Schristos 49878327f04Schristos if (s->sid_ctx_length > sizeof(ss->sid_ctx)) { 4998fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 500a89c9211Schristos SSL_SESSION_free(ss); 501a89c9211Schristos return 0; 502a89c9211Schristos } 503a89c9211Schristos memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); 504a89c9211Schristos ss->sid_ctx_length = s->sid_ctx_length; 505a89c9211Schristos s->session = ss; 506a89c9211Schristos ss->ssl_version = s->version; 507a89c9211Schristos ss->verify_result = X509_V_OK; 508a89c9211Schristos 5095af53050Schristos /* If client supports extended master secret set it in session */ 5108fbed61eSchristos if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) 5115af53050Schristos ss->flags |= SSL_SESS_FLAG_EXTMS; 5125af53050Schristos 513e0ea3921Schristos return 1; 514a89c9211Schristos } 515a89c9211Schristos 516e0ea3921Schristos SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id, 517e0ea3921Schristos size_t sess_id_len) 518a89c9211Schristos { 519a89c9211Schristos SSL_SESSION *ret = NULL; 520a89c9211Schristos 521e0ea3921Schristos if ((s->session_ctx->session_cache_mode 522e0ea3921Schristos & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) { 523a89c9211Schristos SSL_SESSION data; 524e0ea3921Schristos 525a89c9211Schristos data.ssl_version = s->version; 526e0ea3921Schristos if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH)) 527e0ea3921Schristos return NULL; 528e0ea3921Schristos 529e0ea3921Schristos memcpy(data.session_id, sess_id, sess_id_len); 530e0ea3921Schristos data.session_id_length = sess_id_len; 531e0ea3921Schristos 5328fbed61eSchristos if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) 5338fbed61eSchristos return NULL; 534a89c9211Schristos ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); 535635165faSspz if (ret != NULL) { 536a89c9211Schristos /* don't allow other threads to steal it: */ 5375af53050Schristos SSL_SESSION_up_ref(ret); 53832daad53Schristos } 5395af53050Schristos CRYPTO_THREAD_unlock(s->session_ctx->lock); 54032daad53Schristos if (ret == NULL) 5418fbed61eSchristos ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss); 542a89c9211Schristos } 543a89c9211Schristos 544e0ea3921Schristos if (ret == NULL && s->session_ctx->get_session_cb != NULL) { 545a89c9211Schristos int copy = 1; 546e0ea3921Schristos 547e0ea3921Schristos ret = s->session_ctx->get_session_cb(s, sess_id, sess_id_len, ©); 548a89c9211Schristos 5495af53050Schristos if (ret != NULL) { 550b46c97feSchristos if (ret->not_resumable) { 551b46c97feSchristos /* If its not resumable then ignore this session */ 552b46c97feSchristos if (!copy) 553b46c97feSchristos SSL_SESSION_free(ret); 554b46c97feSchristos return NULL; 555b46c97feSchristos } 5568fbed61eSchristos ssl_tsan_counter(s->session_ctx, 5578fbed61eSchristos &s->session_ctx->stats.sess_cb_hit); 558a89c9211Schristos 559635165faSspz /* 560635165faSspz * Increment reference count now if the session callback asks us 561635165faSspz * to do so (note that if the session structures returned by the 562635165faSspz * callback are shared between threads, it must handle the 563635165faSspz * reference count itself [i.e. copy == 0], or things won't be 564635165faSspz * thread-safe). 565635165faSspz */ 566a89c9211Schristos if (copy) 5675af53050Schristos SSL_SESSION_up_ref(ret); 568a89c9211Schristos 569635165faSspz /* 570635165faSspz * Add the externally cached session to the internal cache as 571635165faSspz * well if and only if we are supposed to. 572635165faSspz */ 573e0ea3921Schristos if ((s->session_ctx->session_cache_mode & 574e0ea3921Schristos SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) { 575635165faSspz /* 5765af53050Schristos * Either return value of SSL_CTX_add_session should not 5775af53050Schristos * interrupt the session resumption process. The return 5785af53050Schristos * value is intentionally ignored. 579635165faSspz */ 580e0ea3921Schristos (void)SSL_CTX_add_session(s->session_ctx, ret); 581a89c9211Schristos } 582a89c9211Schristos } 5835af53050Schristos } 584a89c9211Schristos 585e0ea3921Schristos return ret; 586e0ea3921Schristos } 587e0ea3921Schristos 588e0ea3921Schristos /*- 589e0ea3921Schristos * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this 590e0ea3921Schristos * connection. It is only called by servers. 591e0ea3921Schristos * 592e0ea3921Schristos * hello: The parsed ClientHello data 593e0ea3921Schristos * 594e0ea3921Schristos * Returns: 595e0ea3921Schristos * -1: fatal error 596e0ea3921Schristos * 0: no session found 597e0ea3921Schristos * 1: a session may have been found. 598e0ea3921Schristos * 599e0ea3921Schristos * Side effects: 600e0ea3921Schristos * - If a session is found then s->session is pointed at it (after freeing an 601e0ea3921Schristos * existing session if need be) and s->verify_result is set from the session. 602e0ea3921Schristos * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1 603e0ea3921Schristos * if the server should issue a new session ticket (to 0 otherwise). 604e0ea3921Schristos */ 605e0ea3921Schristos int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello) 606e0ea3921Schristos { 607e0ea3921Schristos /* This is used only by servers. */ 608e0ea3921Schristos 609e0ea3921Schristos SSL_SESSION *ret = NULL; 610e0ea3921Schristos int fatal = 0; 611e0ea3921Schristos int try_session_cache = 0; 612e0ea3921Schristos SSL_TICKET_STATUS r; 613e0ea3921Schristos 614e0ea3921Schristos if (SSL_IS_TLS13(s)) { 615e0ea3921Schristos /* 616e0ea3921Schristos * By default we will send a new ticket. This can be overridden in the 617e0ea3921Schristos * ticket processing. 618e0ea3921Schristos */ 619e0ea3921Schristos s->ext.ticket_expected = 1; 620e0ea3921Schristos if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes, 621e0ea3921Schristos SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts, 622e0ea3921Schristos NULL, 0) 623e0ea3921Schristos || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO, 624e0ea3921Schristos hello->pre_proc_exts, NULL, 0)) 625e0ea3921Schristos return -1; 626e0ea3921Schristos 627e0ea3921Schristos ret = s->session; 628e0ea3921Schristos } else { 629e0ea3921Schristos /* sets s->ext.ticket_expected */ 630e0ea3921Schristos r = tls_get_ticket_from_client(s, hello, &ret); 631e0ea3921Schristos switch (r) { 632e0ea3921Schristos case SSL_TICKET_FATAL_ERR_MALLOC: 633e0ea3921Schristos case SSL_TICKET_FATAL_ERR_OTHER: 634e0ea3921Schristos fatal = 1; 6358fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 636e0ea3921Schristos goto err; 637e0ea3921Schristos case SSL_TICKET_NONE: 638e0ea3921Schristos case SSL_TICKET_EMPTY: 639e0ea3921Schristos if (hello->session_id_len > 0) { 640e0ea3921Schristos try_session_cache = 1; 641e0ea3921Schristos ret = lookup_sess_in_cache(s, hello->session_id, 642e0ea3921Schristos hello->session_id_len); 643e0ea3921Schristos } 644e0ea3921Schristos break; 645e0ea3921Schristos case SSL_TICKET_NO_DECRYPT: 646e0ea3921Schristos case SSL_TICKET_SUCCESS: 647e0ea3921Schristos case SSL_TICKET_SUCCESS_RENEW: 648e0ea3921Schristos break; 649e0ea3921Schristos } 650e0ea3921Schristos } 651e0ea3921Schristos 65232daad53Schristos if (ret == NULL) 65332daad53Schristos goto err; 65432daad53Schristos 65532daad53Schristos /* Now ret is non-NULL and we own one of its reference counts. */ 656a89c9211Schristos 657e0ea3921Schristos /* Check TLS version consistency */ 658e0ea3921Schristos if (ret->ssl_version != s->version) 659e0ea3921Schristos goto err; 660e0ea3921Schristos 661a89c9211Schristos if (ret->sid_ctx_length != s->sid_ctx_length 662635165faSspz || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { 663635165faSspz /* 664635165faSspz * We have the session requested by the client, but we don't want to 665635165faSspz * use it in this context. 666635165faSspz */ 667a89c9211Schristos goto err; /* treat like cache miss */ 668a89c9211Schristos } 669a89c9211Schristos 670635165faSspz if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { 671635165faSspz /* 672635165faSspz * We can't be sure if this session is being used out of context, 673635165faSspz * which is especially important for SSL_VERIFY_PEER. The application 674635165faSspz * should have used SSL[_CTX]_set_session_id_context. For this error 675635165faSspz * case, we generate an error instead of treating the event like a 676635165faSspz * cache miss (otherwise it would be easy for applications to 677635165faSspz * effectively disable the session cache by accident without anyone 678635165faSspz * noticing). 679a89c9211Schristos */ 680a89c9211Schristos 6818fbed61eSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 682635165faSspz SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); 683a89c9211Schristos fatal = 1; 684a89c9211Schristos goto err; 685a89c9211Schristos } 686a89c9211Schristos 6878fbed61eSchristos if (sess_timedout(time(NULL), ret)) { 6888fbed61eSchristos ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout); 689635165faSspz if (try_session_cache) { 69032daad53Schristos /* session was from the cache, so remove it */ 691a89c9211Schristos SSL_CTX_remove_session(s->session_ctx, ret); 69232daad53Schristos } 693a89c9211Schristos goto err; 694a89c9211Schristos } 695a89c9211Schristos 6965af53050Schristos /* Check extended master secret extension consistency */ 6975af53050Schristos if (ret->flags & SSL_SESS_FLAG_EXTMS) { 6985af53050Schristos /* If old session includes extms, but new does not: abort handshake */ 6998fbed61eSchristos if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) { 7008fbed61eSchristos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS); 7015af53050Schristos fatal = 1; 7025af53050Schristos goto err; 7035af53050Schristos } 7048fbed61eSchristos } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) { 7055af53050Schristos /* If new session includes extms, but old does not: do not resume */ 7065af53050Schristos goto err; 7075af53050Schristos } 7085af53050Schristos 709e0ea3921Schristos if (!SSL_IS_TLS13(s)) { 710e0ea3921Schristos /* We already did this for TLS1.3 */ 711a89c9211Schristos SSL_SESSION_free(s->session); 712a89c9211Schristos s->session = ret; 713e0ea3921Schristos } 714e0ea3921Schristos 7158fbed61eSchristos ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit); 716a89c9211Schristos s->verify_result = s->session->verify_result; 71732daad53Schristos return 1; 718a89c9211Schristos 719a89c9211Schristos err: 720635165faSspz if (ret != NULL) { 721a89c9211Schristos SSL_SESSION_free(ret); 722e0ea3921Schristos /* In TLSv1.3 s->session was already set to ret, so we NULL it out */ 723e0ea3921Schristos if (SSL_IS_TLS13(s)) 724e0ea3921Schristos s->session = NULL; 7255af53050Schristos 726635165faSspz if (!try_session_cache) { 727635165faSspz /* 728635165faSspz * The session was from a ticket, so we should issue a ticket for 729635165faSspz * the new session 730635165faSspz */ 731e0ea3921Schristos s->ext.ticket_expected = 1; 73232daad53Schristos } 73332daad53Schristos } 734a89c9211Schristos if (fatal) 735a89c9211Schristos return -1; 736e0ea3921Schristos 737a89c9211Schristos return 0; 738a89c9211Schristos } 739a89c9211Schristos 740a89c9211Schristos int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) 741a89c9211Schristos { 742a89c9211Schristos int ret = 0; 743a89c9211Schristos SSL_SESSION *s; 744a89c9211Schristos 745635165faSspz /* 746635165faSspz * add just 1 reference count for the SSL_CTX's session cache even though 747635165faSspz * it has two ways of access: each session is in a doubly linked list and 748635165faSspz * an lhash 749635165faSspz */ 7505af53050Schristos SSL_SESSION_up_ref(c); 751635165faSspz /* 752635165faSspz * if session c is in already in cache, we take back the increment later 753635165faSspz */ 754a89c9211Schristos 7558fbed61eSchristos if (!CRYPTO_THREAD_write_lock(ctx->lock)) { 7568fbed61eSchristos SSL_SESSION_free(c); 7578fbed61eSchristos return 0; 7588fbed61eSchristos } 759a89c9211Schristos s = lh_SSL_SESSION_insert(ctx->sessions, c); 760a89c9211Schristos 761635165faSspz /* 762635165faSspz * s != NULL iff we already had a session with the given PID. In this 763635165faSspz * case, s == c should hold (then we did not really modify 764635165faSspz * ctx->sessions), or we're in trouble. 765635165faSspz */ 766635165faSspz if (s != NULL && s != c) { 767a89c9211Schristos /* We *are* in trouble ... */ 768a89c9211Schristos SSL_SESSION_list_remove(ctx, s); 769a89c9211Schristos SSL_SESSION_free(s); 770635165faSspz /* 771635165faSspz * ... so pretend the other session did not exist in cache (we cannot 772635165faSspz * handle two SSL_SESSION structures with identical session ID in the 773635165faSspz * same cache, which could happen e.g. when two threads concurrently 774635165faSspz * obtain the same session from an external cache) 775635165faSspz */ 776a89c9211Schristos s = NULL; 777b367ed38Sspz } else if (s == NULL && 778b367ed38Sspz lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) { 779b367ed38Sspz /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */ 780b367ed38Sspz 781b367ed38Sspz /* 782b367ed38Sspz * ... so take back the extra reference and also don't add 783b367ed38Sspz * the session to the SSL_SESSION_list at this time 784b367ed38Sspz */ 785b367ed38Sspz s = c; 786a89c9211Schristos } 787a89c9211Schristos 7888fbed61eSchristos /* Adjust last used time, and add back into the cache at the appropriate spot */ 7898fbed61eSchristos if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) { 7908fbed61eSchristos c->time = time(NULL); 7918fbed61eSchristos ssl_session_calculate_timeout(c); 7928fbed61eSchristos } 7938fbed61eSchristos 7948fbed61eSchristos if (s == NULL) { 7958fbed61eSchristos /* 7968fbed61eSchristos * new cache entry -- remove old ones if cache has become too large 7978fbed61eSchristos * delete cache entry *before* add, so we don't remove the one we're adding! 7988fbed61eSchristos */ 7998fbed61eSchristos 8008fbed61eSchristos ret = 1; 8018fbed61eSchristos 8028fbed61eSchristos if (SSL_CTX_sess_get_cache_size(ctx) > 0) { 8038fbed61eSchristos while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) { 8048fbed61eSchristos if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) 8058fbed61eSchristos break; 8068fbed61eSchristos else 8078fbed61eSchristos ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full); 8088fbed61eSchristos } 8098fbed61eSchristos } 8108fbed61eSchristos } 8118fbed61eSchristos 812a89c9211Schristos SSL_SESSION_list_add(ctx, c); 813a89c9211Schristos 814635165faSspz if (s != NULL) { 815635165faSspz /* 816635165faSspz * existing cache entry -- decrement previously incremented reference 817635165faSspz * count because it already takes into account the cache 818635165faSspz */ 819a89c9211Schristos 820a89c9211Schristos SSL_SESSION_free(s); /* s == c */ 821a89c9211Schristos ret = 0; 822a89c9211Schristos } 8235af53050Schristos CRYPTO_THREAD_unlock(ctx->lock); 8245af53050Schristos return ret; 825a89c9211Schristos } 826a89c9211Schristos 827a89c9211Schristos int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) 828a89c9211Schristos { 829a89c9211Schristos return remove_session_lock(ctx, c, 1); 830a89c9211Schristos } 831a89c9211Schristos 832a89c9211Schristos static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) 833a89c9211Schristos { 834a89c9211Schristos SSL_SESSION *r; 835a89c9211Schristos int ret = 0; 836a89c9211Schristos 837635165faSspz if ((c != NULL) && (c->session_id_length != 0)) { 8388fbed61eSchristos if (lck) { 8398fbed61eSchristos if (!CRYPTO_THREAD_write_lock(ctx->lock)) 8408fbed61eSchristos return 0; 8418fbed61eSchristos } 842e0ea3921Schristos if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) { 843a89c9211Schristos ret = 1; 844e0ea3921Schristos r = lh_SSL_SESSION_delete(ctx->sessions, r); 845e0ea3921Schristos SSL_SESSION_list_remove(ctx, r); 846a89c9211Schristos } 8475af53050Schristos c->not_resumable = 1; 848a89c9211Schristos 849635165faSspz if (lck) 8505af53050Schristos CRYPTO_THREAD_unlock(ctx->lock); 851a89c9211Schristos 8525af53050Schristos if (ctx->remove_session_cb != NULL) 8535af53050Schristos ctx->remove_session_cb(ctx, c); 8542500041cSchristos 8552500041cSchristos if (ret) 8562500041cSchristos SSL_SESSION_free(r); 8578fbed61eSchristos } 858e0ea3921Schristos return ret; 859a89c9211Schristos } 860a89c9211Schristos 861a89c9211Schristos void SSL_SESSION_free(SSL_SESSION *ss) 862a89c9211Schristos { 863a89c9211Schristos int i; 864a89c9211Schristos 865a89c9211Schristos if (ss == NULL) 866a89c9211Schristos return; 867e0ea3921Schristos CRYPTO_DOWN_REF(&ss->references, &i, ss->lock); 8685af53050Schristos REF_PRINT_COUNT("SSL_SESSION", ss); 869635165faSspz if (i > 0) 870635165faSspz return; 8715af53050Schristos REF_ASSERT_ISNT(i < 0); 872a89c9211Schristos 873a89c9211Schristos CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); 874a89c9211Schristos 87578327f04Schristos OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key)); 87678327f04Schristos OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id)); 877635165faSspz X509_free(ss->peer); 8785af53050Schristos sk_X509_pop_free(ss->peer_chain, X509_free); 879e0ea3921Schristos OPENSSL_free(ss->ext.hostname); 880e0ea3921Schristos OPENSSL_free(ss->ext.tick); 881a89c9211Schristos #ifndef OPENSSL_NO_PSK 882a89c9211Schristos OPENSSL_free(ss->psk_identity_hint); 883a89c9211Schristos OPENSSL_free(ss->psk_identity); 884a89c9211Schristos #endif 8854e3dcb23Sspz #ifndef OPENSSL_NO_SRP 8864e3dcb23Sspz OPENSSL_free(ss->srp_username); 8874e3dcb23Sspz #endif 888e0ea3921Schristos OPENSSL_free(ss->ext.alpn_selected); 889e0ea3921Schristos OPENSSL_free(ss->ticket_appdata); 8905af53050Schristos CRYPTO_THREAD_lock_free(ss->lock); 8915af53050Schristos OPENSSL_clear_free(ss, sizeof(*ss)); 8925af53050Schristos } 8935af53050Schristos 8945af53050Schristos int SSL_SESSION_up_ref(SSL_SESSION *ss) 8955af53050Schristos { 8965af53050Schristos int i; 8975af53050Schristos 898e0ea3921Schristos if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0) 8995af53050Schristos return 0; 9005af53050Schristos 9015af53050Schristos REF_PRINT_COUNT("SSL_SESSION", ss); 9025af53050Schristos REF_ASSERT_ISNT(i < 2); 9035af53050Schristos return ((i > 1) ? 1 : 0); 904a89c9211Schristos } 905a89c9211Schristos 906a89c9211Schristos int SSL_set_session(SSL *s, SSL_SESSION *session) 907a89c9211Schristos { 9085af53050Schristos ssl_clear_bad_session(s); 9095af53050Schristos if (s->ctx->method != s->method) { 9105af53050Schristos if (!SSL_set_ssl_method(s, s->ctx->method)) 911a6054fbfSspz return 0; 912a6054fbfSspz } 913a89c9211Schristos 9145af53050Schristos if (session != NULL) { 9155af53050Schristos SSL_SESSION_up_ref(session); 9165af53050Schristos s->verify_result = session->verify_result; 9175af53050Schristos } 918a89c9211Schristos SSL_SESSION_free(s->session); 919a89c9211Schristos s->session = session; 9205af53050Schristos 9215af53050Schristos return 1; 922a89c9211Schristos } 923a89c9211Schristos 9245af53050Schristos int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, 9255af53050Schristos unsigned int sid_len) 9265af53050Schristos { 9275af53050Schristos if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { 9288fbed61eSchristos ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); 9295af53050Schristos return 0; 930a89c9211Schristos } 9315af53050Schristos s->session_id_length = sid_len; 9325af53050Schristos if (sid != s->session_id) 9335af53050Schristos memcpy(s->session_id, sid, sid_len); 9345af53050Schristos return 1; 935a89c9211Schristos } 936a89c9211Schristos 937a89c9211Schristos long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) 938a89c9211Schristos { 9398fbed61eSchristos time_t new_timeout = (time_t)t; 9408fbed61eSchristos 9418fbed61eSchristos if (s == NULL || t < 0) 942e0ea3921Schristos return 0; 9438fbed61eSchristos if (s->owner != NULL) { 9448fbed61eSchristos if (!CRYPTO_THREAD_write_lock(s->owner->lock)) 9458fbed61eSchristos return 0; 9468fbed61eSchristos s->timeout = new_timeout; 9478fbed61eSchristos ssl_session_calculate_timeout(s); 9488fbed61eSchristos SSL_SESSION_list_add(s->owner, s); 9498fbed61eSchristos CRYPTO_THREAD_unlock(s->owner->lock); 9508fbed61eSchristos } else { 9518fbed61eSchristos s->timeout = new_timeout; 9528fbed61eSchristos ssl_session_calculate_timeout(s); 9538fbed61eSchristos } 954e0ea3921Schristos return 1; 955a89c9211Schristos } 956a89c9211Schristos 957a89c9211Schristos long SSL_SESSION_get_timeout(const SSL_SESSION *s) 958a89c9211Schristos { 959635165faSspz if (s == NULL) 960e0ea3921Schristos return 0; 9618fbed61eSchristos return (long)s->timeout; 962a89c9211Schristos } 963a89c9211Schristos 964a89c9211Schristos long SSL_SESSION_get_time(const SSL_SESSION *s) 965a89c9211Schristos { 966635165faSspz if (s == NULL) 967e0ea3921Schristos return 0; 9688fbed61eSchristos return (long)s->time; 969a89c9211Schristos } 970a89c9211Schristos 971a89c9211Schristos long SSL_SESSION_set_time(SSL_SESSION *s, long t) 972a89c9211Schristos { 9738fbed61eSchristos time_t new_time = (time_t)t; 9748fbed61eSchristos 975635165faSspz if (s == NULL) 976e0ea3921Schristos return 0; 9778fbed61eSchristos if (s->owner != NULL) { 9788fbed61eSchristos if (!CRYPTO_THREAD_write_lock(s->owner->lock)) 9798fbed61eSchristos return 0; 9808fbed61eSchristos s->time = new_time; 9818fbed61eSchristos ssl_session_calculate_timeout(s); 9828fbed61eSchristos SSL_SESSION_list_add(s->owner, s); 9838fbed61eSchristos CRYPTO_THREAD_unlock(s->owner->lock); 9848fbed61eSchristos } else { 9858fbed61eSchristos s->time = new_time; 9868fbed61eSchristos ssl_session_calculate_timeout(s); 9878fbed61eSchristos } 988e0ea3921Schristos return t; 989a89c9211Schristos } 990a89c9211Schristos 9915af53050Schristos int SSL_SESSION_get_protocol_version(const SSL_SESSION *s) 9925af53050Schristos { 9935af53050Schristos return s->ssl_version; 9945af53050Schristos } 9955af53050Schristos 996e0ea3921Schristos int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version) 997e0ea3921Schristos { 998e0ea3921Schristos s->ssl_version = version; 999e0ea3921Schristos return 1; 1000e0ea3921Schristos } 1001e0ea3921Schristos 10025af53050Schristos const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s) 10035af53050Schristos { 10045af53050Schristos return s->cipher; 10055af53050Schristos } 10065af53050Schristos 1007e0ea3921Schristos int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher) 1008e0ea3921Schristos { 1009e0ea3921Schristos s->cipher = cipher; 1010e0ea3921Schristos return 1; 1011e0ea3921Schristos } 1012e0ea3921Schristos 10135af53050Schristos const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s) 10145af53050Schristos { 1015e0ea3921Schristos return s->ext.hostname; 1016e0ea3921Schristos } 1017e0ea3921Schristos 1018e0ea3921Schristos int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname) 1019e0ea3921Schristos { 1020e0ea3921Schristos OPENSSL_free(s->ext.hostname); 1021e0ea3921Schristos if (hostname == NULL) { 1022e0ea3921Schristos s->ext.hostname = NULL; 1023e0ea3921Schristos return 1; 1024e0ea3921Schristos } 1025e0ea3921Schristos s->ext.hostname = OPENSSL_strdup(hostname); 1026e0ea3921Schristos 1027e0ea3921Schristos return s->ext.hostname != NULL; 10285af53050Schristos } 10295af53050Schristos 10305af53050Schristos int SSL_SESSION_has_ticket(const SSL_SESSION *s) 10315af53050Schristos { 1032e0ea3921Schristos return (s->ext.ticklen > 0) ? 1 : 0; 10335af53050Schristos } 10345af53050Schristos 10355af53050Schristos unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) 10365af53050Schristos { 1037e0ea3921Schristos return s->ext.tick_lifetime_hint; 10385af53050Schristos } 10395af53050Schristos 10405af53050Schristos void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick, 10415af53050Schristos size_t *len) 10425af53050Schristos { 1043e0ea3921Schristos *len = s->ext.ticklen; 10445af53050Schristos if (tick != NULL) 1045e0ea3921Schristos *tick = s->ext.tick; 1046e0ea3921Schristos } 1047e0ea3921Schristos 1048e0ea3921Schristos uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s) 1049e0ea3921Schristos { 1050e0ea3921Schristos return s->ext.max_early_data; 1051e0ea3921Schristos } 1052e0ea3921Schristos 1053e0ea3921Schristos int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data) 1054e0ea3921Schristos { 1055e0ea3921Schristos s->ext.max_early_data = max_early_data; 1056e0ea3921Schristos 1057e0ea3921Schristos return 1; 1058e0ea3921Schristos } 1059e0ea3921Schristos 1060e0ea3921Schristos void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s, 1061e0ea3921Schristos const unsigned char **alpn, 1062e0ea3921Schristos size_t *len) 1063e0ea3921Schristos { 1064e0ea3921Schristos *alpn = s->ext.alpn_selected; 1065e0ea3921Schristos *len = s->ext.alpn_selected_len; 1066e0ea3921Schristos } 1067e0ea3921Schristos 1068e0ea3921Schristos int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn, 1069e0ea3921Schristos size_t len) 1070e0ea3921Schristos { 1071e0ea3921Schristos OPENSSL_free(s->ext.alpn_selected); 1072e0ea3921Schristos if (alpn == NULL || len == 0) { 1073e0ea3921Schristos s->ext.alpn_selected = NULL; 1074e0ea3921Schristos s->ext.alpn_selected_len = 0; 1075e0ea3921Schristos return 1; 1076e0ea3921Schristos } 1077e0ea3921Schristos s->ext.alpn_selected = OPENSSL_memdup(alpn, len); 1078e0ea3921Schristos if (s->ext.alpn_selected == NULL) { 1079e0ea3921Schristos s->ext.alpn_selected_len = 0; 1080e0ea3921Schristos return 0; 1081e0ea3921Schristos } 1082e0ea3921Schristos s->ext.alpn_selected_len = len; 1083e0ea3921Schristos 1084e0ea3921Schristos return 1; 10855af53050Schristos } 10865af53050Schristos 10874e3dcb23Sspz X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) 10884e3dcb23Sspz { 10894e3dcb23Sspz return s->peer; 10904e3dcb23Sspz } 10914e3dcb23Sspz 10924e3dcb23Sspz int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, 10934e3dcb23Sspz unsigned int sid_ctx_len) 10944e3dcb23Sspz { 1095635165faSspz if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { 10968fbed61eSchristos ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); 10974e3dcb23Sspz return 0; 10984e3dcb23Sspz } 10994e3dcb23Sspz s->sid_ctx_length = sid_ctx_len; 11005af53050Schristos if (sid_ctx != s->sid_ctx) 11014e3dcb23Sspz memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); 11024e3dcb23Sspz 11034e3dcb23Sspz return 1; 11044e3dcb23Sspz } 11054e3dcb23Sspz 1106e0ea3921Schristos int SSL_SESSION_is_resumable(const SSL_SESSION *s) 1107e0ea3921Schristos { 1108e0ea3921Schristos /* 1109e0ea3921Schristos * In the case of EAP-FAST, we can have a pre-shared "ticket" without a 1110e0ea3921Schristos * session ID. 1111e0ea3921Schristos */ 1112e0ea3921Schristos return !s->not_resumable 1113e0ea3921Schristos && (s->session_id_length > 0 || s->ext.ticklen > 0); 1114e0ea3921Schristos } 1115e0ea3921Schristos 1116a89c9211Schristos long SSL_CTX_set_timeout(SSL_CTX *s, long t) 1117a89c9211Schristos { 1118a89c9211Schristos long l; 1119635165faSspz if (s == NULL) 1120e0ea3921Schristos return 0; 1121a89c9211Schristos l = s->session_timeout; 1122a89c9211Schristos s->session_timeout = t; 1123e0ea3921Schristos return l; 1124a89c9211Schristos } 1125a89c9211Schristos 1126a89c9211Schristos long SSL_CTX_get_timeout(const SSL_CTX *s) 1127a89c9211Schristos { 1128635165faSspz if (s == NULL) 1129e0ea3921Schristos return 0; 1130e0ea3921Schristos return s->session_timeout; 1131a89c9211Schristos } 1132a89c9211Schristos 1133635165faSspz int SSL_set_session_secret_cb(SSL *s, 1134e0ea3921Schristos tls_session_secret_cb_fn tls_session_secret_cb, 1135635165faSspz void *arg) 1136a89c9211Schristos { 1137635165faSspz if (s == NULL) 1138e0ea3921Schristos return 0; 1139e0ea3921Schristos s->ext.session_secret_cb = tls_session_secret_cb; 1140e0ea3921Schristos s->ext.session_secret_cb_arg = arg; 1141e0ea3921Schristos return 1; 1142a89c9211Schristos } 1143a89c9211Schristos 1144a89c9211Schristos int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 1145a89c9211Schristos void *arg) 1146a89c9211Schristos { 1147635165faSspz if (s == NULL) 1148e0ea3921Schristos return 0; 1149e0ea3921Schristos s->ext.session_ticket_cb = cb; 1150e0ea3921Schristos s->ext.session_ticket_cb_arg = arg; 1151e0ea3921Schristos return 1; 1152a89c9211Schristos } 1153a89c9211Schristos 1154a89c9211Schristos int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) 1155a89c9211Schristos { 1156635165faSspz if (s->version >= TLS1_VERSION) { 1157e0ea3921Schristos OPENSSL_free(s->ext.session_ticket); 1158e0ea3921Schristos s->ext.session_ticket = NULL; 1159e0ea3921Schristos s->ext.session_ticket = 1160635165faSspz OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); 1161e0ea3921Schristos if (s->ext.session_ticket == NULL) { 11628fbed61eSchristos ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE); 1163a89c9211Schristos return 0; 1164a89c9211Schristos } 1165a89c9211Schristos 1166e0ea3921Schristos if (ext_data != NULL) { 1167e0ea3921Schristos s->ext.session_ticket->length = ext_len; 1168e0ea3921Schristos s->ext.session_ticket->data = s->ext.session_ticket + 1; 1169e0ea3921Schristos memcpy(s->ext.session_ticket->data, ext_data, ext_len); 1170635165faSspz } else { 1171e0ea3921Schristos s->ext.session_ticket->length = 0; 1172e0ea3921Schristos s->ext.session_ticket->data = NULL; 1173a89c9211Schristos } 1174a89c9211Schristos 1175a89c9211Schristos return 1; 1176a89c9211Schristos } 1177a89c9211Schristos 1178a89c9211Schristos return 0; 1179a89c9211Schristos } 1180a89c9211Schristos 1181a89c9211Schristos void SSL_CTX_flush_sessions(SSL_CTX *s, long t) 1182a89c9211Schristos { 11838fbed61eSchristos STACK_OF(SSL_SESSION) *sk; 11848fbed61eSchristos SSL_SESSION *current; 1185a89c9211Schristos unsigned long i; 1186a89c9211Schristos 11878fbed61eSchristos if (!CRYPTO_THREAD_write_lock(s->lock)) 1188635165faSspz return; 11898fbed61eSchristos 11908fbed61eSchristos sk = sk_SSL_SESSION_new_null(); 11915af53050Schristos i = lh_SSL_SESSION_get_down_load(s->sessions); 11925af53050Schristos lh_SSL_SESSION_set_down_load(s->sessions, 0); 11938fbed61eSchristos 11948fbed61eSchristos /* 11958fbed61eSchristos * Iterate over the list from the back (oldest), and stop 11968fbed61eSchristos * when a session can no longer be removed. 11978fbed61eSchristos * Add the session to a temporary list to be freed outside 11988fbed61eSchristos * the SSL_CTX lock. 11998fbed61eSchristos * But still do the remove_session_cb() within the lock. 12008fbed61eSchristos */ 12018fbed61eSchristos while (s->session_cache_tail != NULL) { 12028fbed61eSchristos current = s->session_cache_tail; 12038fbed61eSchristos if (t == 0 || sess_timedout((time_t)t, current)) { 12048fbed61eSchristos lh_SSL_SESSION_delete(s->sessions, current); 12058fbed61eSchristos SSL_SESSION_list_remove(s, current); 12068fbed61eSchristos current->not_resumable = 1; 12078fbed61eSchristos if (s->remove_session_cb != NULL) 12088fbed61eSchristos s->remove_session_cb(s, current); 12098fbed61eSchristos /* 12108fbed61eSchristos * Throw the session on a stack, it's entirely plausible 12118fbed61eSchristos * that while freeing outside the critical section, the 12128fbed61eSchristos * session could be re-added, so avoid using the next/prev 12138fbed61eSchristos * pointers. If the stack failed to create, or the session 12148fbed61eSchristos * couldn't be put on the stack, just free it here 12158fbed61eSchristos */ 12168fbed61eSchristos if (sk == NULL || !sk_SSL_SESSION_push(sk, current)) 12178fbed61eSchristos SSL_SESSION_free(current); 12188fbed61eSchristos } else { 12198fbed61eSchristos break; 12208fbed61eSchristos } 12218fbed61eSchristos } 12228fbed61eSchristos 12235af53050Schristos lh_SSL_SESSION_set_down_load(s->sessions, i); 12245af53050Schristos CRYPTO_THREAD_unlock(s->lock); 12258fbed61eSchristos 12268fbed61eSchristos sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free); 1227a89c9211Schristos } 1228a89c9211Schristos 1229a89c9211Schristos int ssl_clear_bad_session(SSL *s) 1230a89c9211Schristos { 1231a89c9211Schristos if ((s->session != NULL) && 1232a89c9211Schristos !(s->shutdown & SSL_SENT_SHUTDOWN) && 1233635165faSspz !(SSL_in_init(s) || SSL_in_before(s))) { 1234a6054fbfSspz SSL_CTX_remove_session(s->session_ctx, s->session); 1235e0ea3921Schristos return 1; 1236635165faSspz } else 1237e0ea3921Schristos return 0; 1238a89c9211Schristos } 1239a89c9211Schristos 1240a89c9211Schristos /* locked by SSL_CTX in the calling function */ 1241a89c9211Schristos static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) 1242a89c9211Schristos { 1243635165faSspz if ((s->next == NULL) || (s->prev == NULL)) 1244635165faSspz return; 1245a89c9211Schristos 1246635165faSspz if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { 1247635165faSspz /* last element in list */ 1248635165faSspz if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1249635165faSspz /* only one element in list */ 1250a89c9211Schristos ctx->session_cache_head = NULL; 1251a89c9211Schristos ctx->session_cache_tail = NULL; 1252635165faSspz } else { 1253a89c9211Schristos ctx->session_cache_tail = s->prev; 1254a89c9211Schristos s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1255a89c9211Schristos } 1256635165faSspz } else { 1257635165faSspz if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { 1258635165faSspz /* first element in list */ 1259a89c9211Schristos ctx->session_cache_head = s->next; 1260a89c9211Schristos s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1261635165faSspz } else { 1262635165faSspz /* middle of list */ 1263a89c9211Schristos s->next->prev = s->prev; 1264a89c9211Schristos s->prev->next = s->next; 1265a89c9211Schristos } 1266a89c9211Schristos } 1267a89c9211Schristos s->prev = s->next = NULL; 12688fbed61eSchristos s->owner = NULL; 1269a89c9211Schristos } 1270a89c9211Schristos 1271a89c9211Schristos static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) 1272a89c9211Schristos { 12738fbed61eSchristos SSL_SESSION *next; 12748fbed61eSchristos 1275a89c9211Schristos if ((s->next != NULL) && (s->prev != NULL)) 1276a89c9211Schristos SSL_SESSION_list_remove(ctx, s); 1277a89c9211Schristos 1278635165faSspz if (ctx->session_cache_head == NULL) { 1279a89c9211Schristos ctx->session_cache_head = s; 1280a89c9211Schristos ctx->session_cache_tail = s; 1281a89c9211Schristos s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1282a89c9211Schristos s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 1283635165faSspz } else { 12848fbed61eSchristos if (timeoutcmp(s, ctx->session_cache_head) >= 0) { 12858fbed61eSchristos /* 12868fbed61eSchristos * if we timeout after (or the same time as) the first 12878fbed61eSchristos * session, put us first - usual case 12888fbed61eSchristos */ 1289a89c9211Schristos s->next = ctx->session_cache_head; 1290a89c9211Schristos s->next->prev = s; 1291a89c9211Schristos s->prev = (SSL_SESSION *)&(ctx->session_cache_head); 1292a89c9211Schristos ctx->session_cache_head = s; 12938fbed61eSchristos } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) { 12948fbed61eSchristos /* if we timeout before the last session, put us last */ 12958fbed61eSchristos s->prev = ctx->session_cache_tail; 12968fbed61eSchristos s->prev->next = s; 12978fbed61eSchristos s->next = (SSL_SESSION *)&(ctx->session_cache_tail); 12988fbed61eSchristos ctx->session_cache_tail = s; 12998fbed61eSchristos } else { 13008fbed61eSchristos /* 13018fbed61eSchristos * we timeout somewhere in-between - if there is only 13028fbed61eSchristos * one session in the cache it will be caught above 13038fbed61eSchristos */ 13048fbed61eSchristos next = ctx->session_cache_head->next; 13058fbed61eSchristos while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) { 13068fbed61eSchristos if (timeoutcmp(s, next) >= 0) { 13078fbed61eSchristos s->next = next; 13088fbed61eSchristos s->prev = next->prev; 13098fbed61eSchristos next->prev->next = s; 13108fbed61eSchristos next->prev = s; 13118fbed61eSchristos break; 1312a89c9211Schristos } 13138fbed61eSchristos next = next->next; 13148fbed61eSchristos } 13158fbed61eSchristos } 13168fbed61eSchristos } 13178fbed61eSchristos s->owner = ctx; 1318a89c9211Schristos } 1319a89c9211Schristos 1320a89c9211Schristos void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, 13215af53050Schristos int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess)) 1322a89c9211Schristos { 1323a89c9211Schristos ctx->new_session_cb = cb; 1324a89c9211Schristos } 1325a89c9211Schristos 1326635165faSspz int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { 1327a89c9211Schristos return ctx->new_session_cb; 1328a89c9211Schristos } 1329a89c9211Schristos 1330a89c9211Schristos void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, 1331a89c9211Schristos void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) 1332a89c9211Schristos { 1333a89c9211Schristos ctx->remove_session_cb = cb; 1334a89c9211Schristos } 1335a89c9211Schristos 1336635165faSspz void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, 1337635165faSspz SSL_SESSION *sess) { 1338a89c9211Schristos return ctx->remove_session_cb; 1339a89c9211Schristos } 1340a89c9211Schristos 1341a89c9211Schristos void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, 1342a89c9211Schristos SSL_SESSION *(*cb) (struct ssl_st *ssl, 13435af53050Schristos const unsigned char *data, 13445af53050Schristos int len, int *copy)) 1345a89c9211Schristos { 1346a89c9211Schristos ctx->get_session_cb = cb; 1347a89c9211Schristos } 1348a89c9211Schristos 1349a89c9211Schristos SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, 13505af53050Schristos const unsigned char 13515af53050Schristos *data, int len, 13525af53050Schristos int *copy) { 1353a89c9211Schristos return ctx->get_session_cb; 1354a89c9211Schristos } 1355a89c9211Schristos 1356a89c9211Schristos void SSL_CTX_set_info_callback(SSL_CTX *ctx, 1357a89c9211Schristos void (*cb) (const SSL *ssl, int type, int val)) 1358a89c9211Schristos { 1359a89c9211Schristos ctx->info_callback = cb; 1360a89c9211Schristos } 1361a89c9211Schristos 1362635165faSspz void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, 1363635165faSspz int val) { 1364a89c9211Schristos return ctx->info_callback; 1365a89c9211Schristos } 1366a89c9211Schristos 1367a89c9211Schristos void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, 1368635165faSspz int (*cb) (SSL *ssl, X509 **x509, 1369635165faSspz EVP_PKEY **pkey)) 1370a89c9211Schristos { 1371a89c9211Schristos ctx->client_cert_cb = cb; 1372a89c9211Schristos } 1373a89c9211Schristos 1374635165faSspz int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, 1375635165faSspz EVP_PKEY **pkey) { 1376a89c9211Schristos return ctx->client_cert_cb; 1377a89c9211Schristos } 1378a89c9211Schristos 1379a89c9211Schristos void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, 1380635165faSspz int (*cb) (SSL *ssl, 1381635165faSspz unsigned char *cookie, 1382635165faSspz unsigned int *cookie_len)) 1383a89c9211Schristos { 1384a89c9211Schristos ctx->app_gen_cookie_cb = cb; 1385a89c9211Schristos } 1386a89c9211Schristos 1387a89c9211Schristos void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, 13885af53050Schristos int (*cb) (SSL *ssl, 13895af53050Schristos const unsigned char *cookie, 1390635165faSspz unsigned int cookie_len)) 1391a89c9211Schristos { 1392a89c9211Schristos ctx->app_verify_cookie_cb = cb; 1393a89c9211Schristos } 1394a89c9211Schristos 1395e0ea3921Schristos int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len) 1396e0ea3921Schristos { 1397e0ea3921Schristos OPENSSL_free(ss->ticket_appdata); 1398e0ea3921Schristos ss->ticket_appdata_len = 0; 1399e0ea3921Schristos if (data == NULL || len == 0) { 1400e0ea3921Schristos ss->ticket_appdata = NULL; 1401e0ea3921Schristos return 1; 1402e0ea3921Schristos } 1403e0ea3921Schristos ss->ticket_appdata = OPENSSL_memdup(data, len); 1404e0ea3921Schristos if (ss->ticket_appdata != NULL) { 1405e0ea3921Schristos ss->ticket_appdata_len = len; 1406e0ea3921Schristos return 1; 1407e0ea3921Schristos } 1408e0ea3921Schristos return 0; 1409e0ea3921Schristos } 1410e0ea3921Schristos 1411e0ea3921Schristos int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len) 1412e0ea3921Schristos { 1413e0ea3921Schristos *data = ss->ticket_appdata; 1414e0ea3921Schristos *len = ss->ticket_appdata_len; 1415e0ea3921Schristos return 1; 1416e0ea3921Schristos } 1417e0ea3921Schristos 1418e0ea3921Schristos void SSL_CTX_set_stateless_cookie_generate_cb( 1419e0ea3921Schristos SSL_CTX *ctx, 1420e0ea3921Schristos int (*cb) (SSL *ssl, 1421e0ea3921Schristos unsigned char *cookie, 1422e0ea3921Schristos size_t *cookie_len)) 1423e0ea3921Schristos { 1424e0ea3921Schristos ctx->gen_stateless_cookie_cb = cb; 1425e0ea3921Schristos } 1426e0ea3921Schristos 1427e0ea3921Schristos void SSL_CTX_set_stateless_cookie_verify_cb( 1428e0ea3921Schristos SSL_CTX *ctx, 1429e0ea3921Schristos int (*cb) (SSL *ssl, 1430e0ea3921Schristos const unsigned char *cookie, 1431e0ea3921Schristos size_t cookie_len)) 1432e0ea3921Schristos { 1433e0ea3921Schristos ctx->verify_stateless_cookie_cb = cb; 1434e0ea3921Schristos } 1435e0ea3921Schristos 14365af53050Schristos IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) 1437