Coverage Report

Created: 2025-12-31 06:58

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