Coverage Report

Created: 2023-09-25 06:41

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