Coverage Report

Created: 2025-08-28 07:07

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