Coverage Report

Created: 2023-09-25 06:45

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