Coverage Report

Created: 2025-12-31 06:58

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