Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/ssl/ssl_sess.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2005 Nokia. All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#if defined(__TANDEM) && defined(_SPT_MODEL_)
12
#include <spthread.h>
13
#include <spt_extensions.h> /* timeval */
14
#endif
15
#include <stdio.h>
16
#include <openssl/rand.h>
17
#include <openssl/engine.h>
18
#include "internal/refcount.h"
19
#include "internal/cryptlib.h"
20
#include "ssl_local.h"
21
#include "statem/statem_local.h"
22
23
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
24
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
25
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
26
27
DEFINE_STACK_OF(SSL_SESSION)
28
29
__owur static ossl_inline int sess_timedout(OSSL_TIME t, SSL_SESSION *ss)
30
243
{
31
243
    return ossl_time_compare(t, ss->calc_timeout) > 0;
32
243
}
33
34
/*
35
 * Returns -1/0/+1 as other XXXcmp-type functions
36
 * Takes calculated timeout into consideration
37
 */
38
__owur static ossl_inline int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b)
39
0
{
40
0
    return ossl_time_compare(a->calc_timeout, b->calc_timeout);
41
0
}
42
43
/*
44
 * Calculates effective timeout
45
 * Locking must be done by the caller of this function
46
 */
47
void ssl_session_calculate_timeout(SSL_SESSION *ss)
48
300k
{
49
300k
    ss->calc_timeout = ossl_time_add(ss->time, ss->timeout);
50
300k
}
51
52
/*
53
 * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
54
 * unlike in earlier protocol versions, the session ticket may not have been
55
 * sent yet even though a handshake has finished. The session ticket data could
56
 * come in sometime later...or even change if multiple session ticket messages
57
 * are sent from the server. The preferred way for applications to obtain
58
 * a resumable session is to use SSL_CTX_sess_set_new_cb().
59
 */
60
61
SSL_SESSION *SSL_get_session(const SSL *ssl)
62
/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
63
7
{
64
7
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
65
66
7
    if (sc == NULL)
67
0
        return NULL;
68
69
7
    return sc->session;
70
7
}
71
72
SSL_SESSION *SSL_get1_session(SSL *ssl)
73
/* variant of SSL_get_session: caller really gets something */
74
0
{
75
0
    SSL_SESSION *sess;
76
77
    /*
78
     * Need to lock this all up rather than just use CRYPTO_add so that
79
     * somebody doesn't free ssl->session between when we check it's non-null
80
     * and when we up the reference count.
81
     */
82
0
    if (!CRYPTO_THREAD_read_lock(ssl->lock))
83
0
        return NULL;
84
0
    sess = SSL_get_session(ssl);
85
0
    if (sess != NULL)
86
0
        SSL_SESSION_up_ref(sess);
87
0
    CRYPTO_THREAD_unlock(ssl->lock);
88
0
    return sess;
89
0
}
90
91
int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
92
0
{
93
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
94
0
}
95
96
void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
97
0
{
98
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
99
0
}
100
101
SSL_SESSION *SSL_SESSION_new(void)
102
149k
{
103
149k
    SSL_SESSION *ss;
104
105
149k
    if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
106
0
        return NULL;
107
108
149k
    ss = OPENSSL_zalloc(sizeof(*ss));
109
149k
    if (ss == NULL)
110
0
        return NULL;
111
112
149k
    ss->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_UNSPECIFIED;
113
149k
    ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
114
    /* 5 minute timeout by default */
115
149k
    ss->timeout = ossl_seconds2time(60 * 5 + 4);
116
149k
    ss->time = ossl_time_now();
117
149k
    ssl_session_calculate_timeout(ss);
118
149k
    if (!CRYPTO_NEW_REF(&ss->references, 1)) {
119
0
        OPENSSL_free(ss);
120
0
        return NULL;
121
0
    }
122
123
149k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
124
0
        CRYPTO_FREE_REF(&ss->references);
125
0
        OPENSSL_free(ss);
126
0
        return NULL;
127
0
    }
128
149k
    return ss;
129
149k
}
130
131
/*
132
 * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
133
 * ticket == 0 then no ticket information is duplicated, otherwise it is.
134
 */
135
static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket)
136
2.22k
{
137
2.22k
    SSL_SESSION *dest;
138
139
2.22k
    dest = OPENSSL_malloc(sizeof(*dest));
140
2.22k
    if (dest == NULL)
141
0
        return NULL;
142
143
    /*
144
     * src is logically read-only but the prev/next pointers are not, they are
145
     * part of the session cache and can be modified concurrently.
146
     */
147
2.22k
    memcpy(dest, src, offsetof(SSL_SESSION, prev));
148
149
    /*
150
     * Set the various pointers to NULL so that we can call SSL_SESSION_free in
151
     * the case of an error whilst halfway through constructing dest
152
     */
153
2.22k
#ifndef OPENSSL_NO_PSK
154
2.22k
    dest->psk_identity_hint = NULL;
155
2.22k
    dest->psk_identity = NULL;
156
2.22k
#endif
157
2.22k
    dest->ext.hostname = NULL;
158
2.22k
    dest->ext.tick = NULL;
159
2.22k
    dest->ext.alpn_selected = NULL;
160
2.22k
#ifndef OPENSSL_NO_SRP
161
2.22k
    dest->srp_username = NULL;
162
2.22k
#endif
163
2.22k
    dest->peer_chain = NULL;
164
2.22k
    dest->peer = NULL;
165
2.22k
    dest->peer_rpk = NULL;
166
2.22k
    dest->ticket_appdata = NULL;
167
2.22k
    memset(&dest->ex_data, 0, sizeof(dest->ex_data));
168
169
    /* As the copy is not in the cache, we remove the associated pointers */
170
2.22k
    dest->prev = NULL;
171
2.22k
    dest->next = NULL;
172
2.22k
    dest->owner = NULL;
173
174
2.22k
    if (!CRYPTO_NEW_REF(&dest->references, 1)) {
175
0
        OPENSSL_free(dest);
176
0
        return NULL;
177
0
    }
178
179
2.22k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) {
180
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
181
0
        goto err;
182
0
    }
183
184
2.22k
    if (src->peer != NULL) {
185
2.22k
        if (!X509_up_ref(src->peer)) {
186
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
187
0
            goto err;
188
0
        }
189
2.22k
        dest->peer = src->peer;
190
2.22k
    }
191
192
2.22k
    if (src->peer_chain != NULL) {
193
2.22k
        dest->peer_chain = X509_chain_up_ref(src->peer_chain);
194
2.22k
        if (dest->peer_chain == NULL) {
195
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
196
0
            goto err;
197
0
        }
198
2.22k
    }
199
200
2.22k
    if (src->peer_rpk != NULL) {
201
0
        if (!EVP_PKEY_up_ref(src->peer_rpk))
202
0
            goto err;
203
0
        dest->peer_rpk = src->peer_rpk;
204
0
    }
205
206
2.22k
#ifndef OPENSSL_NO_PSK
207
2.22k
    if (src->psk_identity_hint) {
208
0
        dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
209
0
        if (dest->psk_identity_hint == NULL)
210
0
            goto err;
211
0
    }
212
2.22k
    if (src->psk_identity) {
213
0
        dest->psk_identity = OPENSSL_strdup(src->psk_identity);
214
0
        if (dest->psk_identity == NULL)
215
0
            goto err;
216
0
    }
217
2.22k
#endif
218
219
2.22k
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
220
2.22k
            &dest->ex_data, &src->ex_data)) {
221
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
222
0
        goto err;
223
0
    }
224
225
2.22k
    if (src->ext.hostname) {
226
4
        dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
227
4
        if (dest->ext.hostname == NULL)
228
0
            goto err;
229
4
    }
230
231
2.22k
    if (ticket != 0 && src->ext.tick != NULL) {
232
0
        dest->ext.tick = OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
233
0
        if (dest->ext.tick == NULL)
234
0
            goto err;
235
2.22k
    } else {
236
2.22k
        dest->ext.tick_lifetime_hint = 0;
237
2.22k
        dest->ext.ticklen = 0;
238
2.22k
    }
239
240
2.22k
    if (src->ext.alpn_selected != NULL) {
241
2.21k
        dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
242
2.21k
            src->ext.alpn_selected_len);
243
2.21k
        if (dest->ext.alpn_selected == NULL)
244
0
            goto err;
245
2.21k
    }
246
247
2.22k
#ifndef OPENSSL_NO_SRP
248
2.22k
    if (src->srp_username) {
249
0
        dest->srp_username = OPENSSL_strdup(src->srp_username);
250
0
        if (dest->srp_username == NULL)
251
0
            goto err;
252
0
    }
253
2.22k
#endif
254
255
2.22k
    if (src->ticket_appdata != NULL) {
256
0
        dest->ticket_appdata = OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
257
0
        if (dest->ticket_appdata == NULL)
258
0
            goto err;
259
0
    }
260
261
2.22k
    return dest;
262
0
err:
263
0
    SSL_SESSION_free(dest);
264
0
    return NULL;
265
2.22k
}
266
267
SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
268
0
{
269
0
    return ssl_session_dup_intern(src, 1);
270
0
}
271
272
/*
273
 * Used internally when duplicating a session which might be already shared.
274
 * We will have resumed the original session. Subsequently we might have marked
275
 * it as non-resumable (e.g. in another thread) - but this copy should be ok to
276
 * resume from.
277
 */
278
SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
279
2.22k
{
280
2.22k
    SSL_SESSION *sess = ssl_session_dup_intern(src, ticket);
281
282
2.22k
    if (sess != NULL)
283
2.22k
        sess->not_resumable = 0;
284
285
2.22k
    return sess;
286
2.22k
}
287
288
const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
289
0
{
290
0
    if (len)
291
0
        *len = (unsigned int)s->session_id_length;
292
0
    return s->session_id;
293
0
}
294
295
const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
296
    unsigned int *len)
297
0
{
298
0
    if (len != NULL)
299
0
        *len = (unsigned int)s->sid_ctx_length;
300
0
    return s->sid_ctx;
301
0
}
302
303
unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
304
0
{
305
0
    return s->compress_meth;
306
0
}
307
308
/*
309
 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
310
 * the ID with random junk repeatedly until we have no conflict is going to
311
 * complete in one iteration pretty much "most" of the time (btw:
312
 * understatement). So, if it takes us 10 iterations and we still can't avoid
313
 * a conflict - well that's a reasonable point to call it quits. Either the
314
 * RAND code is broken or someone is trying to open roughly very close to
315
 * 2^256 SSL sessions to our server. How you might store that many sessions
316
 * is perhaps a more interesting question ...
317
 */
318
319
26.4k
#define MAX_SESS_ID_ATTEMPTS 10
320
static int def_generate_session_id(SSL *ssl, unsigned char *id,
321
    unsigned int *id_len)
322
26.4k
{
323
26.4k
    unsigned int retry = 0;
324
26.4k
    do {
325
26.4k
        if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0)
326
0
            return 0;
327
26.4k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
328
26.4k
        if (retry > 0) {
329
0
            id[0]++;
330
0
        }
331
26.4k
#endif
332
26.4k
    } while (SSL_has_matching_session_id(ssl, id, *id_len) && (++retry < MAX_SESS_ID_ATTEMPTS));
333
26.4k
    if (retry < MAX_SESS_ID_ATTEMPTS)
334
26.4k
        return 1;
335
    /* else - woops a session_id match */
336
    /*
337
     * XXX We should also check the external cache -- but the probability of
338
     * a collision is negligible, and we could not prevent the concurrent
339
     * creation of sessions with identical IDs since we currently don't have
340
     * means to atomically check whether a session ID already exists and make
341
     * a reservation for it if it does not (this problem applies to the
342
     * internal cache as well).
343
     */
344
0
    return 0;
345
26.4k
}
346
347
int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss)
348
28.4k
{
349
28.4k
    unsigned int tmp;
350
28.4k
    GEN_SESSION_CB cb = def_generate_session_id;
351
28.4k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
352
353
28.4k
    switch (s->version) {
354
0
    case SSL3_VERSION:
355
1.78k
    case TLS1_VERSION:
356
2.84k
    case TLS1_1_VERSION:
357
15.9k
    case TLS1_2_VERSION:
358
15.9k
    case TLS1_3_VERSION:
359
15.9k
    case DTLS1_BAD_VER:
360
19.0k
    case DTLS1_VERSION:
361
28.4k
    case DTLS1_2_VERSION:
362
28.4k
        ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
363
28.4k
        break;
364
0
    default:
365
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
366
0
        return 0;
367
28.4k
    }
368
369
    /*-
370
     * If RFC5077 ticket, use empty session ID (as server).
371
     * Note that:
372
     * (a) ssl_get_prev_session() does lookahead into the
373
     *     ClientHello extensions to find the session ticket.
374
     *     When ssl_get_prev_session() fails, statem_srvr.c calls
375
     *     ssl_get_new_session() in tls_process_client_hello().
376
     *     At that point, it has not yet parsed the extensions,
377
     *     however, because of the lookahead, it already knows
378
     *     whether a ticket is expected or not.
379
     *
380
     * (b) statem_clnt.c calls ssl_get_new_session() before parsing
381
     *     ServerHello extensions, and before recording the session
382
     *     ID received from the server, so this block is a noop.
383
     */
384
28.4k
    if (s->ext.ticket_expected) {
385
5.65k
        ss->session_id_length = 0;
386
5.65k
        return 1;
387
5.65k
    }
388
389
    /* Choose which callback will set the session ID */
390
22.7k
    if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock))
391
0
        return 0;
392
22.7k
    if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
393
0
        CRYPTO_THREAD_unlock(ssl->lock);
394
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
395
0
            SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
396
0
        return 0;
397
0
    }
398
22.7k
    if (s->generate_session_id)
399
0
        cb = s->generate_session_id;
400
22.7k
    else if (s->session_ctx->generate_session_id)
401
0
        cb = s->session_ctx->generate_session_id;
402
22.7k
    CRYPTO_THREAD_unlock(s->session_ctx->lock);
403
22.7k
    CRYPTO_THREAD_unlock(ssl->lock);
404
    /* Choose a session ID */
405
22.7k
    memset(ss->session_id, 0, ss->session_id_length);
406
22.7k
    tmp = (int)ss->session_id_length;
407
22.7k
    if (!cb(ssl, ss->session_id, &tmp)) {
408
        /* The callback failed */
409
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
410
0
            SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
411
0
        return 0;
412
0
    }
413
    /*
414
     * Don't allow the callback to set the session length to zero. nor
415
     * set it higher than it was.
416
     */
417
22.7k
    if (tmp == 0 || tmp > ss->session_id_length) {
418
        /* The callback set an illegal length */
419
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
420
0
            SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
421
0
        return 0;
422
0
    }
423
22.7k
    ss->session_id_length = tmp;
424
    /* Finally, check for a conflict */
425
22.7k
    if (SSL_has_matching_session_id(ssl, ss->session_id,
426
22.7k
            (unsigned int)ss->session_id_length)) {
427
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
428
0
        return 0;
429
0
    }
430
431
22.7k
    return 1;
432
22.7k
}
433
434
int ssl_get_new_session(SSL_CONNECTION *s, int session)
435
146k
{
436
    /* This gets used by clients and servers. */
437
438
146k
    SSL_SESSION *ss = NULL;
439
440
146k
    if ((ss = SSL_SESSION_new()) == NULL) {
441
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
442
0
        return 0;
443
0
    }
444
445
    /* If the context has a default timeout, use it */
446
146k
    if (ossl_time_is_zero(s->session_ctx->session_timeout))
447
0
        ss->timeout = SSL_CONNECTION_GET_SSL(s)->method->get_timeout();
448
146k
    else
449
146k
        ss->timeout = s->session_ctx->session_timeout;
450
146k
    ssl_session_calculate_timeout(ss);
451
452
146k
    SSL_SESSION_free(s->session);
453
146k
    s->session = NULL;
454
455
146k
    if (session) {
456
36.6k
        if (SSL_CONNECTION_IS_TLS13(s)) {
457
            /*
458
             * We generate the session id while constructing the
459
             * NewSessionTicket in TLSv1.3.
460
             */
461
4.27k
            ss->session_id_length = 0;
462
32.3k
        } else if (!ssl_generate_session_id(s, ss)) {
463
            /* SSLfatal() already called */
464
0
            SSL_SESSION_free(ss);
465
0
            return 0;
466
0
        }
467
468
109k
    } else {
469
109k
        ss->session_id_length = 0;
470
109k
    }
471
472
146k
    if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
473
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
474
0
        SSL_SESSION_free(ss);
475
0
        return 0;
476
0
    }
477
146k
    memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
478
146k
    ss->sid_ctx_length = s->sid_ctx_length;
479
146k
    s->session = ss;
480
146k
    ss->ssl_version = s->version;
481
146k
    ss->verify_result = X509_V_OK;
482
483
    /* If client supports extended master secret set it in session */
484
146k
    if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
485
4.64k
        ss->flags |= SSL_SESS_FLAG_EXTMS;
486
487
146k
    return 1;
488
146k
}
489
490
SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
491
    const unsigned char *sess_id,
492
    size_t sess_id_len)
493
514
{
494
514
    SSL_SESSION *ret = NULL;
495
496
514
    if ((s->session_ctx->session_cache_mode
497
514
            & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)
498
514
        == 0) {
499
514
        SSL_SESSION data;
500
501
514
        data.ssl_version = s->version;
502
514
        if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
503
0
            return NULL;
504
505
514
        memcpy(data.session_id, sess_id, sess_id_len);
506
514
        data.session_id_length = sess_id_len;
507
508
514
        if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
509
0
            return NULL;
510
514
        ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
511
514
        if (ret != NULL) {
512
            /* don't allow other threads to steal it: */
513
0
            SSL_SESSION_up_ref(ret);
514
0
        }
515
514
        CRYPTO_THREAD_unlock(s->session_ctx->lock);
516
514
        if (ret == NULL)
517
514
            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
518
514
    }
519
520
514
    if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
521
0
        int copy = 1;
522
523
0
        ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_USER_SSL(s),
524
0
            sess_id, sess_id_len, &copy);
525
526
0
        if (ret != NULL) {
527
0
            if (ret->not_resumable) {
528
                /* If its not resumable then ignore this session */
529
0
                if (!copy)
530
0
                    SSL_SESSION_free(ret);
531
0
                return NULL;
532
0
            }
533
0
            ssl_tsan_counter(s->session_ctx,
534
0
                &s->session_ctx->stats.sess_cb_hit);
535
536
            /*
537
             * Increment reference count now if the session callback asks us
538
             * to do so (note that if the session structures returned by the
539
             * callback are shared between threads, it must handle the
540
             * reference count itself [i.e. copy == 0], or things won't be
541
             * thread-safe).
542
             */
543
0
            if (copy)
544
0
                SSL_SESSION_up_ref(ret);
545
546
            /*
547
             * Add the externally cached session to the internal cache as
548
             * well if and only if we are supposed to.
549
             */
550
0
            if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
551
                /*
552
                 * Either return value of SSL_CTX_add_session should not
553
                 * interrupt the session resumption process. The return
554
                 * value is intentionally ignored.
555
                 */
556
0
                (void)SSL_CTX_add_session(s->session_ctx, ret);
557
0
            }
558
0
        }
559
0
    }
560
561
514
    return ret;
562
514
}
563
564
/*-
565
 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
566
 * connection. It is only called by servers.
567
 *
568
 *   hello: The parsed ClientHello data
569
 *
570
 * Returns:
571
 *   -1: fatal error
572
 *    0: no session found
573
 *    1: a session may have been found.
574
 *
575
 * Side effects:
576
 *   - If a session is found then s->session is pointed at it (after freeing an
577
 *     existing session if need be) and s->verify_result is set from the session.
578
 *   - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
579
 *     if the server should issue a new session ticket (to 0 otherwise).
580
 */
581
int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello)
582
36.0k
{
583
    /* This is used only by servers. */
584
585
36.0k
    SSL_SESSION *ret = NULL;
586
36.0k
    int fatal = 0;
587
36.0k
    int try_session_cache = 0;
588
36.0k
    SSL_TICKET_STATUS r;
589
590
36.0k
    if (SSL_CONNECTION_IS_TLS13(s)) {
591
5.68k
        SSL_SESSION_free(s->session);
592
5.68k
        s->session = NULL;
593
        /*
594
         * By default we will send a new ticket. This can be overridden in the
595
         * ticket processing.
596
         */
597
5.68k
        s->ext.ticket_expected = 1;
598
5.68k
        if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
599
5.68k
                SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
600
5.68k
                NULL, 0)
601
5.62k
            || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
602
5.62k
                hello->pre_proc_exts, NULL, 0))
603
531
            return -1;
604
605
        /* If we resumed, s->session will now be set */
606
5.14k
        ret = s->session;
607
30.3k
    } else {
608
        /* sets s->ext.ticket_expected */
609
30.3k
        r = tls_get_ticket_from_client(s, hello, &ret);
610
30.3k
        switch (r) {
611
0
        case SSL_TICKET_FATAL_ERR_MALLOC:
612
0
        case SSL_TICKET_FATAL_ERR_OTHER:
613
0
            fatal = 1;
614
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
615
0
            goto err;
616
23.9k
        case SSL_TICKET_NONE:
617
27.9k
        case SSL_TICKET_EMPTY:
618
27.9k
            if (hello->session_id_len > 0) {
619
3.69k
                try_session_cache = 1;
620
3.69k
                ret = lookup_sess_in_cache(s, hello->session_id,
621
3.69k
                    hello->session_id_len);
622
3.69k
            }
623
27.9k
            break;
624
2.14k
        case SSL_TICKET_NO_DECRYPT:
625
2.45k
        case SSL_TICKET_SUCCESS:
626
2.45k
        case SSL_TICKET_SUCCESS_RENEW:
627
2.45k
            break;
628
30.3k
        }
629
30.3k
    }
630
631
35.5k
    if (ret == NULL)
632
35.2k
        goto err;
633
634
    /* Now ret is non-NULL and we own one of its reference counts. */
635
636
    /* Check TLS version consistency */
637
319
    if (ret->ssl_version != s->version)
638
15
        goto err;
639
640
304
    if (ret->sid_ctx_length != s->sid_ctx_length
641
296
        || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
642
        /*
643
         * We have the session requested by the client, but we don't want to
644
         * use it in this context.
645
         */
646
8
        goto err; /* treat like cache miss */
647
8
    }
648
649
296
    if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
650
        /*
651
         * We can't be sure if this session is being used out of context,
652
         * which is especially important for SSL_VERIFY_PEER. The application
653
         * should have used SSL[_CTX]_set_session_id_context. For this error
654
         * case, we generate an error instead of treating the event like a
655
         * cache miss (otherwise it would be easy for applications to
656
         * effectively disable the session cache by accident without anyone
657
         * noticing).
658
         */
659
660
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
661
0
            SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
662
0
        fatal = 1;
663
0
        goto err;
664
0
    }
665
666
296
    if (sess_timedout(ossl_time_now(), ret)) {
667
7
        ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout);
668
7
        if (try_session_cache) {
669
            /* session was from the cache, so remove it */
670
0
            SSL_CTX_remove_session(s->session_ctx, ret);
671
0
        }
672
7
        goto err;
673
7
    }
674
675
    /* Check extended master secret extension consistency */
676
289
    if (ret->flags & SSL_SESS_FLAG_EXTMS) {
677
        /* If old session includes extms, but new does not: abort handshake */
678
252
        if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
679
14
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS);
680
14
            fatal = 1;
681
14
            goto err;
682
14
        }
683
252
    } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
684
        /* If new session includes extms, but old does not: do not resume */
685
15
        goto err;
686
15
    }
687
688
260
    if (!SSL_CONNECTION_IS_TLS13(s)) {
689
        /* We already did this for TLS1.3 */
690
259
        SSL_SESSION_free(s->session);
691
259
        s->session = ret;
692
259
    }
693
694
260
    ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit);
695
260
    s->verify_result = s->session->verify_result;
696
260
    return 1;
697
698
35.2k
err:
699
35.2k
    if (ret != NULL) {
700
59
        SSL_SESSION_free(ret);
701
        /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
702
59
        if (SSL_CONNECTION_IS_TLS13(s))
703
7
            s->session = NULL;
704
705
59
        if (!try_session_cache) {
706
            /*
707
             * The session was from a ticket, so we should issue a ticket for
708
             * the new session
709
             */
710
59
            s->ext.ticket_expected = 1;
711
59
        }
712
59
    }
713
35.2k
    if (fatal)
714
14
        return -1;
715
716
35.2k
    return 0;
717
35.2k
}
718
719
int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
720
540
{
721
540
    int ret = 0;
722
540
    SSL_SESSION *s;
723
724
    /*
725
     * add just 1 reference count for the SSL_CTX's session cache even though
726
     * it has two ways of access: each session is in a doubly linked list and
727
     * an lhash
728
     */
729
540
    SSL_SESSION_up_ref(c);
730
    /*
731
     * if session c is in already in cache, we take back the increment later
732
     */
733
734
540
    if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
735
0
        SSL_SESSION_free(c);
736
0
        return 0;
737
0
    }
738
540
    s = lh_SSL_SESSION_insert(ctx->sessions, c);
739
740
    /*
741
     * s != NULL iff we already had a session with the given PID. In this
742
     * case, s == c should hold (then we did not really modify
743
     * ctx->sessions), or we're in trouble.
744
     */
745
540
    if (s != NULL && s != c) {
746
        /* We *are* in trouble ... */
747
0
        SSL_SESSION_list_remove(ctx, s);
748
0
        SSL_SESSION_free(s);
749
        /*
750
         * ... so pretend the other session did not exist in cache (we cannot
751
         * handle two SSL_SESSION structures with identical session ID in the
752
         * same cache, which could happen e.g. when two threads concurrently
753
         * obtain the same session from an external cache)
754
         */
755
0
        s = NULL;
756
540
    } else if (s == NULL && lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
757
        /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
758
759
        /*
760
         * ... so take back the extra reference and also don't add
761
         * the session to the SSL_SESSION_list at this time
762
         */
763
0
        s = c;
764
0
    }
765
766
    /* Adjust last used time, and add back into the cache at the appropriate spot */
767
540
    if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) {
768
0
        c->time = ossl_time_now();
769
0
        ssl_session_calculate_timeout(c);
770
0
    }
771
772
540
    if (s == NULL) {
773
        /*
774
         * new cache entry -- remove old ones if cache has become too large
775
         * delete cache entry *before* add, so we don't remove the one we're adding!
776
         */
777
778
540
        ret = 1;
779
780
540
        if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
781
540
            while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) {
782
0
                if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
783
0
                    break;
784
0
                else
785
0
                    ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full);
786
0
            }
787
540
        }
788
540
    }
789
790
540
    SSL_SESSION_list_add(ctx, c);
791
792
540
    if (s != NULL) {
793
        /*
794
         * existing cache entry -- decrement previously incremented reference
795
         * count because it already takes into account the cache
796
         */
797
798
0
        SSL_SESSION_free(s); /* s == c */
799
0
        ret = 0;
800
0
    }
801
540
    CRYPTO_THREAD_unlock(ctx->lock);
802
540
    return ret;
803
540
}
804
805
int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
806
87.0k
{
807
87.0k
    return remove_session_lock(ctx, c, 1);
808
87.0k
}
809
810
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
811
87.0k
{
812
87.0k
    SSL_SESSION *r;
813
87.0k
    int ret = 0;
814
815
87.0k
    if ((c != NULL) && (c->session_id_length != 0)) {
816
29.2k
        if (lck) {
817
29.2k
            if (!CRYPTO_THREAD_write_lock(ctx->lock))
818
0
                return 0;
819
29.2k
        }
820
29.2k
        if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
821
1.40k
            ret = 1;
822
1.40k
            r = lh_SSL_SESSION_delete(ctx->sessions, r);
823
1.40k
            SSL_SESSION_list_remove(ctx, r);
824
1.40k
        }
825
29.2k
        c->not_resumable = 1;
826
827
29.2k
        if (lck)
828
29.2k
            CRYPTO_THREAD_unlock(ctx->lock);
829
830
29.2k
        if (ctx->remove_session_cb != NULL)
831
0
            ctx->remove_session_cb(ctx, c);
832
833
29.2k
        if (ret)
834
1.40k
            SSL_SESSION_free(r);
835
29.2k
    }
836
87.0k
    return ret;
837
87.0k
}
838
839
void SSL_SESSION_free(SSL_SESSION *ss)
840
972k
{
841
972k
    int i;
842
843
972k
    if (ss == NULL)
844
807k
        return;
845
164k
    CRYPTO_DOWN_REF(&ss->references, &i);
846
164k
    REF_PRINT_COUNT("SSL_SESSION", i, ss);
847
164k
    if (i > 0)
848
1.46k
        return;
849
162k
    REF_ASSERT_ISNT(i < 0);
850
851
162k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
852
853
162k
    OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
854
162k
    OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
855
162k
    X509_free(ss->peer);
856
162k
    EVP_PKEY_free(ss->peer_rpk);
857
162k
    OSSL_STACK_OF_X509_free(ss->peer_chain);
858
162k
    OPENSSL_free(ss->ext.hostname);
859
162k
    OPENSSL_free(ss->ext.tick);
860
162k
#ifndef OPENSSL_NO_PSK
861
162k
    OPENSSL_free(ss->psk_identity_hint);
862
162k
    OPENSSL_free(ss->psk_identity);
863
162k
#endif
864
162k
#ifndef OPENSSL_NO_SRP
865
162k
    OPENSSL_free(ss->srp_username);
866
162k
#endif
867
162k
    OPENSSL_free(ss->ext.alpn_selected);
868
162k
    OPENSSL_free(ss->ticket_appdata);
869
162k
    CRYPTO_FREE_REF(&ss->references);
870
162k
    OPENSSL_clear_free(ss, sizeof(*ss));
871
162k
}
872
873
int SSL_SESSION_up_ref(SSL_SESSION *ss)
874
1.46k
{
875
1.46k
    int i;
876
877
1.46k
    if (CRYPTO_UP_REF(&ss->references, &i) <= 0)
878
0
        return 0;
879
880
1.46k
    REF_PRINT_COUNT("SSL_SESSION", i, ss);
881
1.46k
    REF_ASSERT_ISNT(i < 2);
882
1.46k
    return ((i > 1) ? 1 : 0);
883
1.46k
}
884
885
int SSL_set_session(SSL *s, SSL_SESSION *session)
886
0
{
887
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
888
889
0
    if (sc == NULL)
890
0
        return 0;
891
892
0
    ssl_clear_bad_session(sc);
893
0
    if (s->defltmeth != s->method) {
894
0
        if (!SSL_set_ssl_method(s, s->defltmeth))
895
0
            return 0;
896
0
    }
897
898
0
    if (session != NULL) {
899
0
        SSL_SESSION_up_ref(session);
900
0
        sc->verify_result = session->verify_result;
901
0
    }
902
0
    SSL_SESSION_free(sc->session);
903
0
    sc->session = session;
904
905
0
    return 1;
906
0
}
907
908
int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
909
    unsigned int sid_len)
910
0
{
911
0
    if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
912
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
913
0
        return 0;
914
0
    }
915
0
    s->session_id_length = sid_len;
916
0
    if (sid != s->session_id && sid_len > 0)
917
0
        memcpy(s->session_id, sid, sid_len);
918
919
0
    return 1;
920
0
}
921
922
long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
923
0
{
924
0
    OSSL_TIME new_timeout = ossl_seconds2time(t);
925
926
0
    if (s == NULL || t < 0)
927
0
        return 0;
928
0
    if (s->owner != NULL) {
929
0
        if (!CRYPTO_THREAD_write_lock(s->owner->lock))
930
0
            return 0;
931
0
        s->timeout = new_timeout;
932
0
        ssl_session_calculate_timeout(s);
933
0
        SSL_SESSION_list_add(s->owner, s);
934
0
        CRYPTO_THREAD_unlock(s->owner->lock);
935
0
    } else {
936
0
        s->timeout = new_timeout;
937
0
        ssl_session_calculate_timeout(s);
938
0
    }
939
0
    return 1;
940
0
}
941
942
long SSL_SESSION_get_timeout(const SSL_SESSION *s)
943
0
{
944
0
    if (s == NULL)
945
0
        return 0;
946
0
    return (long)ossl_time_to_time_t(s->timeout);
947
0
}
948
949
#ifndef OPENSSL_NO_DEPRECATED_3_4
950
long SSL_SESSION_get_time(const SSL_SESSION *s)
951
0
{
952
0
    return (long)SSL_SESSION_get_time_ex(s);
953
0
}
954
#endif
955
956
time_t SSL_SESSION_get_time_ex(const SSL_SESSION *s)
957
0
{
958
0
    if (s == NULL)
959
0
        return 0;
960
0
    return ossl_time_to_time_t(s->time);
961
0
}
962
963
time_t SSL_SESSION_set_time_ex(SSL_SESSION *s, time_t t)
964
0
{
965
0
    OSSL_TIME new_time = ossl_time_from_time_t(t);
966
967
0
    if (s == NULL)
968
0
        return 0;
969
0
    if (s->owner != NULL) {
970
0
        if (!CRYPTO_THREAD_write_lock(s->owner->lock))
971
0
            return 0;
972
0
        s->time = new_time;
973
0
        ssl_session_calculate_timeout(s);
974
0
        SSL_SESSION_list_add(s->owner, s);
975
0
        CRYPTO_THREAD_unlock(s->owner->lock);
976
0
    } else {
977
0
        s->time = new_time;
978
0
        ssl_session_calculate_timeout(s);
979
0
    }
980
0
    return t;
981
0
}
982
983
#ifndef OPENSSL_NO_DEPRECATED_3_4
984
long SSL_SESSION_set_time(SSL_SESSION *s, long t)
985
0
{
986
0
    return (long)SSL_SESSION_set_time_ex(s, (time_t)t);
987
0
}
988
#endif
989
990
int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
991
0
{
992
0
    return s->ssl_version;
993
0
}
994
995
int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
996
0
{
997
0
    s->ssl_version = version;
998
0
    return 1;
999
0
}
1000
1001
const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
1002
0
{
1003
0
    return s->cipher;
1004
0
}
1005
1006
int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
1007
0
{
1008
0
    s->cipher = cipher;
1009
0
    return 1;
1010
0
}
1011
1012
const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
1013
0
{
1014
0
    return s->ext.hostname;
1015
0
}
1016
1017
int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
1018
0
{
1019
0
    OPENSSL_free(s->ext.hostname);
1020
0
    if (hostname == NULL) {
1021
0
        s->ext.hostname = NULL;
1022
0
        return 1;
1023
0
    }
1024
0
    s->ext.hostname = OPENSSL_strdup(hostname);
1025
1026
0
    return s->ext.hostname != NULL;
1027
0
}
1028
1029
int SSL_SESSION_has_ticket(const SSL_SESSION *s)
1030
0
{
1031
0
    return (s->ext.ticklen > 0) ? 1 : 0;
1032
0
}
1033
1034
unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
1035
0
{
1036
0
    return s->ext.tick_lifetime_hint;
1037
0
}
1038
1039
void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
1040
    size_t *len)
1041
0
{
1042
0
    *len = s->ext.ticklen;
1043
0
    if (tick != NULL)
1044
0
        *tick = s->ext.tick;
1045
0
}
1046
1047
uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
1048
0
{
1049
0
    return s->ext.max_early_data;
1050
0
}
1051
1052
int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
1053
0
{
1054
0
    s->ext.max_early_data = max_early_data;
1055
1056
0
    return 1;
1057
0
}
1058
1059
void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
1060
    const unsigned char **alpn,
1061
    size_t *len)
1062
0
{
1063
0
    *alpn = s->ext.alpn_selected;
1064
0
    *len = s->ext.alpn_selected_len;
1065
0
}
1066
1067
int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
1068
    size_t len)
1069
0
{
1070
0
    OPENSSL_free(s->ext.alpn_selected);
1071
0
    if (alpn == NULL || len == 0) {
1072
0
        s->ext.alpn_selected = NULL;
1073
0
        s->ext.alpn_selected_len = 0;
1074
0
        return 1;
1075
0
    }
1076
0
    s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
1077
0
    if (s->ext.alpn_selected == NULL) {
1078
0
        s->ext.alpn_selected_len = 0;
1079
0
        return 0;
1080
0
    }
1081
0
    s->ext.alpn_selected_len = len;
1082
1083
0
    return 1;
1084
0
}
1085
1086
X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
1087
0
{
1088
0
    return s->peer;
1089
0
}
1090
1091
EVP_PKEY *SSL_SESSION_get0_peer_rpk(SSL_SESSION *s)
1092
0
{
1093
0
    return s->peer_rpk;
1094
0
}
1095
1096
int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
1097
    unsigned int sid_ctx_len)
1098
0
{
1099
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1100
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1101
0
        return 0;
1102
0
    }
1103
0
    s->sid_ctx_length = sid_ctx_len;
1104
0
    if (sid_ctx != s->sid_ctx)
1105
0
        memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
1106
1107
0
    return 1;
1108
0
}
1109
1110
int SSL_SESSION_is_resumable(const SSL_SESSION *s)
1111
5.98k
{
1112
    /*
1113
     * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
1114
     * session ID.
1115
     */
1116
5.98k
    return !s->not_resumable
1117
5.98k
        && (s->session_id_length > 0 || s->ext.ticklen > 0);
1118
5.98k
}
1119
1120
long SSL_CTX_set_timeout(SSL_CTX *s, long t)
1121
0
{
1122
0
    long l;
1123
1124
0
    if (s == NULL)
1125
0
        return 0;
1126
0
    l = (long)ossl_time2seconds(s->session_timeout);
1127
0
    s->session_timeout = ossl_seconds2time(t);
1128
0
    return l;
1129
0
}
1130
1131
long SSL_CTX_get_timeout(const SSL_CTX *s)
1132
0
{
1133
0
    if (s == NULL)
1134
0
        return 0;
1135
0
    return (long)ossl_time2seconds(s->session_timeout);
1136
0
}
1137
1138
int SSL_set_session_secret_cb(SSL *s,
1139
    tls_session_secret_cb_fn tls_session_secret_cb,
1140
    void *arg)
1141
0
{
1142
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1143
1144
0
    if (sc == NULL)
1145
0
        return 0;
1146
1147
0
    sc->ext.session_secret_cb = tls_session_secret_cb;
1148
0
    sc->ext.session_secret_cb_arg = arg;
1149
0
    return 1;
1150
0
}
1151
1152
int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1153
    void *arg)
1154
0
{
1155
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1156
1157
0
    if (sc == NULL)
1158
0
        return 0;
1159
1160
0
    sc->ext.session_ticket_cb = cb;
1161
0
    sc->ext.session_ticket_cb_arg = arg;
1162
0
    return 1;
1163
0
}
1164
1165
int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1166
0
{
1167
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1168
1169
0
    if (sc == NULL)
1170
0
        return 0;
1171
1172
0
    if (sc->version >= TLS1_VERSION) {
1173
0
        OPENSSL_free(sc->ext.session_ticket);
1174
0
        sc->ext.session_ticket = NULL;
1175
0
        sc->ext.session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1176
0
        if (sc->ext.session_ticket == NULL)
1177
0
            return 0;
1178
1179
0
        if (ext_data != NULL) {
1180
0
            sc->ext.session_ticket->length = ext_len;
1181
0
            sc->ext.session_ticket->data = sc->ext.session_ticket + 1;
1182
0
            memcpy(sc->ext.session_ticket->data, ext_data, ext_len);
1183
0
        } else {
1184
0
            sc->ext.session_ticket->length = 0;
1185
0
            sc->ext.session_ticket->data = NULL;
1186
0
        }
1187
1188
0
        return 1;
1189
0
    }
1190
1191
0
    return 0;
1192
0
}
1193
1194
#ifndef OPENSSL_NO_DEPRECATED_3_4
1195
void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
1196
{
1197
    SSL_CTX_flush_sessions_ex(s, (time_t)t);
1198
}
1199
#endif
1200
1201
void SSL_CTX_flush_sessions_ex(SSL_CTX *s, time_t t)
1202
120k
{
1203
120k
    STACK_OF(SSL_SESSION) *sk;
1204
120k
    SSL_SESSION *current;
1205
120k
    unsigned long i;
1206
120k
    const OSSL_TIME timeout = ossl_time_from_time_t(t);
1207
1208
120k
    if (!CRYPTO_THREAD_write_lock(s->lock))
1209
0
        return;
1210
1211
120k
    sk = sk_SSL_SESSION_new_null();
1212
120k
    i = lh_SSL_SESSION_get_down_load(s->sessions);
1213
120k
    lh_SSL_SESSION_set_down_load(s->sessions, 0);
1214
1215
    /*
1216
     * Iterate over the list from the back (oldest), and stop
1217
     * when a session can no longer be removed.
1218
     * Add the session to a temporary list to be freed outside
1219
     * the SSL_CTX lock.
1220
     * But still do the remove_session_cb() within the lock.
1221
     */
1222
120k
    while (s->session_cache_tail != NULL) {
1223
47
        current = s->session_cache_tail;
1224
47
        if (t == 0 || sess_timedout(timeout, current)) {
1225
47
            lh_SSL_SESSION_delete(s->sessions, current);
1226
47
            SSL_SESSION_list_remove(s, current);
1227
47
            current->not_resumable = 1;
1228
47
            if (s->remove_session_cb != NULL)
1229
0
                s->remove_session_cb(s, current);
1230
            /*
1231
             * Throw the session on a stack, it's entirely plausible
1232
             * that while freeing outside the critical section, the
1233
             * session could be re-added, so avoid using the next/prev
1234
             * pointers. If the stack failed to create, or the session
1235
             * couldn't be put on the stack, just free it here
1236
             */
1237
47
            if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
1238
0
                SSL_SESSION_free(current);
1239
47
        } else {
1240
0
            break;
1241
0
        }
1242
47
    }
1243
1244
120k
    lh_SSL_SESSION_set_down_load(s->sessions, i);
1245
120k
    CRYPTO_THREAD_unlock(s->lock);
1246
1247
120k
    sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
1248
120k
}
1249
1250
int ssl_clear_bad_session(SSL_CONNECTION *s)
1251
478k
{
1252
478k
    if ((s->session != NULL) && !(s->shutdown & SSL_SENT_SHUTDOWN) && !(SSL_in_init(SSL_CONNECTION_GET_SSL(s)) || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) {
1253
7.84k
        SSL_CTX_remove_session(s->session_ctx, s->session);
1254
7.84k
        return 1;
1255
7.84k
    } else
1256
470k
        return 0;
1257
478k
}
1258
1259
/* locked by SSL_CTX in the calling function */
1260
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
1261
1.46k
{
1262
1.46k
    if ((s->next == NULL) || (s->prev == NULL))
1263
0
        return;
1264
1265
1.46k
    if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
1266
        /* last element in list */
1267
1.46k
        if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1268
            /* only one element in list */
1269
1.46k
            ctx->session_cache_head = NULL;
1270
1.46k
            ctx->session_cache_tail = NULL;
1271
1.46k
        } else {
1272
0
            ctx->session_cache_tail = s->prev;
1273
0
            s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1274
0
        }
1275
1.46k
    } else {
1276
0
        if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
1277
            /* first element in list */
1278
0
            ctx->session_cache_head = s->next;
1279
0
            s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1280
0
        } else {
1281
            /* middle of list */
1282
0
            s->next->prev = s->prev;
1283
0
            s->prev->next = s->next;
1284
0
        }
1285
0
    }
1286
1.46k
    s->prev = s->next = NULL;
1287
1.46k
    s->owner = NULL;
1288
1.46k
}
1289
1290
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
1291
1.46k
{
1292
1.46k
    SSL_SESSION *next;
1293
1294
1.46k
    if ((s->next != NULL) && (s->prev != NULL))
1295
0
        SSL_SESSION_list_remove(ctx, s);
1296
1297
1.46k
    if (ctx->session_cache_head == NULL) {
1298
1.46k
        ctx->session_cache_head = s;
1299
1.46k
        ctx->session_cache_tail = s;
1300
1.46k
        s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1301
1.46k
        s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1302
1.46k
    } else {
1303
0
        if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
1304
            /*
1305
             * if we timeout after (or the same time as) the first
1306
             * session, put us first - usual case
1307
             */
1308
0
            s->next = ctx->session_cache_head;
1309
0
            s->next->prev = s;
1310
0
            s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
1311
0
            ctx->session_cache_head = s;
1312
0
        } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
1313
            /* if we timeout before the last session, put us last */
1314
0
            s->prev = ctx->session_cache_tail;
1315
0
            s->prev->next = s;
1316
0
            s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
1317
0
            ctx->session_cache_tail = s;
1318
0
        } else {
1319
            /*
1320
             * we timeout somewhere in-between - if there is only
1321
             * one session in the cache it will be caught above
1322
             */
1323
0
            next = ctx->session_cache_head->next;
1324
0
            while (next != (SSL_SESSION *)&(ctx->session_cache_tail)) {
1325
0
                if (timeoutcmp(s, next) >= 0) {
1326
0
                    s->next = next;
1327
0
                    s->prev = next->prev;
1328
0
                    next->prev->next = s;
1329
0
                    next->prev = s;
1330
0
                    break;
1331
0
                }
1332
0
                next = next->next;
1333
0
            }
1334
0
        }
1335
0
    }
1336
1.46k
    s->owner = ctx;
1337
1.46k
}
1338
1339
void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1340
    int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess))
1341
0
{
1342
0
    ctx->new_session_cb = cb;
1343
0
}
1344
1345
int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1346
0
{
1347
0
    return ctx->new_session_cb;
1348
0
}
1349
1350
void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1351
    void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess))
1352
0
{
1353
0
    ctx->remove_session_cb = cb;
1354
0
}
1355
1356
void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
1357
    SSL_SESSION *sess)
1358
0
{
1359
0
    return ctx->remove_session_cb;
1360
0
}
1361
1362
void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
1363
    SSL_SESSION *(*cb)(SSL *ssl,
1364
        const unsigned char *data,
1365
        int len, int *copy))
1366
0
{
1367
0
    ctx->get_session_cb = cb;
1368
0
}
1369
1370
SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
1371
    const unsigned char
1372
        *data,
1373
    int len,
1374
    int *copy)
1375
0
{
1376
0
    return ctx->get_session_cb;
1377
0
}
1378
1379
void SSL_CTX_set_info_callback(SSL_CTX *ctx,
1380
    void (*cb)(const SSL *ssl, int type, int val))
1381
0
{
1382
0
    ctx->info_callback = cb;
1383
0
}
1384
1385
void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
1386
    int val)
1387
0
{
1388
0
    return ctx->info_callback;
1389
0
}
1390
1391
void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1392
    int (*cb)(SSL *ssl, X509 **x509,
1393
        EVP_PKEY **pkey))
1394
0
{
1395
0
    ctx->client_cert_cb = cb;
1396
0
}
1397
1398
int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
1399
    EVP_PKEY **pkey)
1400
0
{
1401
0
    return ctx->client_cert_cb;
1402
0
}
1403
1404
void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1405
    int (*cb)(SSL *ssl,
1406
        unsigned char *cookie,
1407
        unsigned int *cookie_len))
1408
0
{
1409
0
    ctx->app_gen_cookie_cb = cb;
1410
0
}
1411
1412
void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1413
    int (*cb)(SSL *ssl,
1414
        const unsigned char *cookie,
1415
        unsigned int cookie_len))
1416
0
{
1417
0
    ctx->app_verify_cookie_cb = cb;
1418
0
}
1419
1420
int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
1421
0
{
1422
0
    OPENSSL_free(ss->ticket_appdata);
1423
0
    ss->ticket_appdata_len = 0;
1424
0
    if (data == NULL || len == 0) {
1425
0
        ss->ticket_appdata = NULL;
1426
0
        return 1;
1427
0
    }
1428
0
    ss->ticket_appdata = OPENSSL_memdup(data, len);
1429
0
    if (ss->ticket_appdata != NULL) {
1430
0
        ss->ticket_appdata_len = len;
1431
0
        return 1;
1432
0
    }
1433
0
    return 0;
1434
0
}
1435
1436
int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
1437
0
{
1438
0
    *data = ss->ticket_appdata;
1439
0
    *len = ss->ticket_appdata_len;
1440
0
    return 1;
1441
0
}
1442
1443
void SSL_CTX_set_stateless_cookie_generate_cb(
1444
    SSL_CTX *ctx,
1445
    int (*cb)(SSL *ssl,
1446
        unsigned char *cookie,
1447
        size_t *cookie_len))
1448
0
{
1449
0
    ctx->gen_stateless_cookie_cb = cb;
1450
0
}
1451
1452
void SSL_CTX_set_stateless_cookie_verify_cb(
1453
    SSL_CTX *ctx,
1454
    int (*cb)(SSL *ssl,
1455
        const unsigned char *cookie,
1456
        size_t cookie_len))
1457
0
{
1458
0
    ctx->verify_stateless_cookie_cb = cb;
1459
0
}
1460
1461
IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
Unexecuted instantiation: PEM_write_bio_SSL_SESSION
Unexecuted instantiation: PEM_write_SSL_SESSION