Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/ssl_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include "internal/e_os.h"
13
#include "internal/e_winsock.h"
14
#include "ssl_local.h"
15
16
#include <openssl/err.h>
17
#include <openssl/objects.h>
18
#include <openssl/x509v3.h>
19
#include <openssl/rand.h>
20
#include <openssl/ocsp.h>
21
#include <openssl/dh.h>
22
#include <openssl/async.h>
23
#include <openssl/kdf.h>
24
#include <openssl/ct.h>
25
#include <openssl/trace.h>
26
#include <openssl/core_names.h>
27
#include <openssl/provider.h>
28
#include "internal/cryptlib.h"
29
#include "internal/nelem.h"
30
#include "internal/refcount.h"
31
#include "internal/thread_once.h"
32
#include "internal/ktls.h"
33
#include "internal/to_hex.h"
34
#include "internal/ssl_unwrap.h"
35
#include "quic/quic_local.h"
36
37
#ifndef OPENSSL_NO_SSLKEYLOG
38
#include <sys/stat.h>
39
#include <fcntl.h>
40
#endif
41
42
static int ssl_undefined_function_3(SSL_CONNECTION *sc, unsigned char *r,
43
    unsigned char *s, size_t t, size_t *u)
44
0
{
45
0
    return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
46
0
}
47
48
static int ssl_undefined_function_4(SSL_CONNECTION *sc, int r)
49
0
{
50
0
    return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
51
0
}
52
53
static size_t ssl_undefined_function_5(SSL_CONNECTION *sc, const char *r,
54
    size_t s, unsigned char *t)
55
0
{
56
0
    return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
57
0
}
58
59
static int ssl_undefined_function_6(int r)
60
0
{
61
0
    return ssl_undefined_function(NULL);
62
0
}
63
64
static int ssl_undefined_function_7(SSL_CONNECTION *sc, unsigned char *r,
65
    size_t s, const char *t, size_t u,
66
    const unsigned char *v, size_t w, int x)
67
0
{
68
0
    return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
69
0
}
70
71
static int ssl_undefined_function_8(SSL_CONNECTION *sc)
72
0
{
73
0
    return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
74
0
}
75
76
const SSL3_ENC_METHOD ssl3_undef_enc_method = {
77
    ssl_undefined_function_8,
78
    ssl_undefined_function_3,
79
    ssl_undefined_function_4,
80
    ssl_undefined_function_5,
81
    NULL, /* client_finished_label */
82
    0, /* client_finished_label_len */
83
    NULL, /* server_finished_label */
84
    0, /* server_finished_label_len */
85
    ssl_undefined_function_6,
86
    ssl_undefined_function_7,
87
};
88
89
struct ssl_async_args {
90
    SSL *s;
91
    void *buf;
92
    size_t num;
93
    enum { READFUNC,
94
        WRITEFUNC,
95
        OTHERFUNC } type;
96
    union {
97
        int (*func_read)(SSL *, void *, size_t, size_t *);
98
        int (*func_write)(SSL *, const void *, size_t, size_t *);
99
        int (*func_other)(SSL *);
100
    } f;
101
};
102
103
static const struct {
104
    uint8_t mtype;
105
    uint8_t ord;
106
    int nid;
107
} dane_mds[] = {
108
    { DANETLS_MATCHING_FULL, 0, NID_undef },
109
    { DANETLS_MATCHING_2256, 1, NID_sha256 },
110
    { DANETLS_MATCHING_2512, 2, NID_sha512 },
111
};
112
113
static int dane_ctx_enable(struct dane_ctx_st *dctx)
114
0
{
115
0
    const EVP_MD **mdevp;
116
0
    uint8_t *mdord;
117
0
    uint8_t mdmax = DANETLS_MATCHING_LAST;
118
0
    int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
119
0
    size_t i;
120
121
0
    if (dctx->mdevp != NULL)
122
0
        return 1;
123
124
0
    mdevp = OPENSSL_calloc(n, sizeof(*mdevp));
125
0
    mdord = OPENSSL_calloc(n, sizeof(*mdord));
126
127
0
    if (mdord == NULL || mdevp == NULL) {
128
0
        OPENSSL_free(mdord);
129
0
        OPENSSL_free(mdevp);
130
0
        return 0;
131
0
    }
132
133
    /* Install default entries */
134
0
    for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
135
0
        const EVP_MD *md;
136
137
0
        if (dane_mds[i].nid == NID_undef || (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
138
0
            continue;
139
0
        mdevp[dane_mds[i].mtype] = md;
140
0
        mdord[dane_mds[i].mtype] = dane_mds[i].ord;
141
0
    }
142
143
0
    dctx->mdevp = mdevp;
144
0
    dctx->mdord = mdord;
145
0
    dctx->mdmax = mdmax;
146
147
0
    return 1;
148
0
}
149
150
static void dane_ctx_final(struct dane_ctx_st *dctx)
151
172k
{
152
172k
    OPENSSL_free(dctx->mdevp);
153
172k
    dctx->mdevp = NULL;
154
155
172k
    OPENSSL_free(dctx->mdord);
156
172k
    dctx->mdord = NULL;
157
172k
    dctx->mdmax = 0;
158
172k
}
159
160
static void tlsa_free(danetls_record *t)
161
0
{
162
0
    if (t == NULL)
163
0
        return;
164
0
    OPENSSL_free(t->data);
165
0
    EVP_PKEY_free(t->spki);
166
0
    OPENSSL_free(t);
167
0
}
168
169
static void dane_final(SSL_DANE *dane)
170
172k
{
171
172k
    sk_danetls_record_pop_free(dane->trecs, tlsa_free);
172
172k
    dane->trecs = NULL;
173
174
172k
    OSSL_STACK_OF_X509_free(dane->certs);
175
172k
    dane->certs = NULL;
176
177
172k
    X509_free(dane->mcert);
178
172k
    dane->mcert = NULL;
179
172k
    dane->mtlsa = NULL;
180
172k
    dane->mdpth = -1;
181
172k
    dane->pdpth = -1;
182
172k
}
183
184
/*
185
 * dane_copy - Copy dane configuration, sans verification state.
186
 */
187
static int ssl_dane_dup(SSL_CONNECTION *to, SSL_CONNECTION *from)
188
0
{
189
0
    int num;
190
0
    int i;
191
192
0
    if (!DANETLS_ENABLED(&from->dane))
193
0
        return 1;
194
195
0
    num = sk_danetls_record_num(from->dane.trecs);
196
0
    dane_final(&to->dane);
197
0
    to->dane.flags = from->dane.flags;
198
0
    to->dane.dctx = &SSL_CONNECTION_GET_CTX(to)->dane;
199
0
    to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
200
201
0
    if (to->dane.trecs == NULL) {
202
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
203
0
        return 0;
204
0
    }
205
206
0
    for (i = 0; i < num; ++i) {
207
0
        danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
208
209
0
        if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to), t->usage,
210
0
                t->selector, t->mtype, t->data, t->dlen)
211
0
            <= 0)
212
0
            return 0;
213
0
    }
214
0
    return 1;
215
0
}
216
217
static int dane_mtype_set(struct dane_ctx_st *dctx,
218
    const EVP_MD *md, uint8_t mtype, uint8_t ord)
219
0
{
220
0
    int i;
221
222
0
    if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
223
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
224
0
        return 0;
225
0
    }
226
227
0
    if (mtype > dctx->mdmax) {
228
0
        const EVP_MD **mdevp;
229
0
        uint8_t *mdord;
230
0
        int n = ((int)mtype) + 1;
231
232
0
        mdevp = OPENSSL_realloc_array(dctx->mdevp, n, sizeof(*mdevp));
233
0
        if (mdevp == NULL)
234
0
            return -1;
235
0
        dctx->mdevp = mdevp;
236
237
0
        mdord = OPENSSL_realloc_array(dctx->mdord, n, sizeof(*mdord));
238
0
        if (mdord == NULL)
239
0
            return -1;
240
0
        dctx->mdord = mdord;
241
242
        /* Zero-fill any gaps */
243
0
        for (i = dctx->mdmax + 1; i < mtype; ++i) {
244
0
            mdevp[i] = NULL;
245
0
            mdord[i] = 0;
246
0
        }
247
248
0
        dctx->mdmax = mtype;
249
0
    }
250
251
0
    dctx->mdevp[mtype] = md;
252
    /* Coerce ordinal of disabled matching types to 0 */
253
0
    dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
254
255
0
    return 1;
256
0
}
257
258
static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
259
0
{
260
0
    if (mtype > dane->dctx->mdmax)
261
0
        return NULL;
262
0
    return dane->dctx->mdevp[mtype];
263
0
}
264
265
static int dane_tlsa_add(SSL_DANE *dane,
266
    uint8_t usage,
267
    uint8_t selector,
268
    uint8_t mtype, const unsigned char *data, size_t dlen)
269
0
{
270
0
    danetls_record *t;
271
0
    const EVP_MD *md = NULL;
272
0
    int ilen = (int)dlen;
273
0
    int i;
274
0
    int num;
275
0
    int mdsize;
276
277
0
    if (dane->trecs == NULL) {
278
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
279
0
        return -1;
280
0
    }
281
282
0
    if (ilen < 0 || dlen != (size_t)ilen) {
283
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
284
0
        return 0;
285
0
    }
286
287
0
    if (usage > DANETLS_USAGE_LAST) {
288
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
289
0
        return 0;
290
0
    }
291
292
0
    if (selector > DANETLS_SELECTOR_LAST) {
293
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
294
0
        return 0;
295
0
    }
296
297
0
    if (mtype != DANETLS_MATCHING_FULL) {
298
0
        md = tlsa_md_get(dane, mtype);
299
0
        if (md == NULL) {
300
0
            ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
301
0
            return 0;
302
0
        }
303
0
    }
304
305
0
    if (md != NULL) {
306
0
        mdsize = EVP_MD_get_size(md);
307
0
        if (mdsize <= 0 || dlen != (size_t)mdsize) {
308
0
            ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
309
0
            return 0;
310
0
        }
311
0
    }
312
0
    if (!data) {
313
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
314
0
        return 0;
315
0
    }
316
317
0
    if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL)
318
0
        return -1;
319
320
0
    t->usage = usage;
321
0
    t->selector = selector;
322
0
    t->mtype = mtype;
323
0
    t->data = OPENSSL_malloc(dlen);
324
0
    if (t->data == NULL) {
325
0
        tlsa_free(t);
326
0
        return -1;
327
0
    }
328
0
    memcpy(t->data, data, dlen);
329
0
    t->dlen = dlen;
330
331
    /* Validate and cache full certificate or public key */
332
0
    if (mtype == DANETLS_MATCHING_FULL) {
333
0
        const unsigned char *p = data;
334
0
        X509 *cert = NULL;
335
0
        EVP_PKEY *pkey = NULL;
336
337
0
        switch (selector) {
338
0
        case DANETLS_SELECTOR_CERT:
339
0
            if (!d2i_X509(&cert, &p, ilen) || p < data || dlen != (size_t)(p - data)) {
340
0
                X509_free(cert);
341
0
                tlsa_free(t);
342
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
343
0
                return 0;
344
0
            }
345
0
            if (X509_get0_pubkey(cert) == NULL) {
346
0
                X509_free(cert);
347
0
                tlsa_free(t);
348
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
349
0
                return 0;
350
0
            }
351
352
0
            if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
353
                /*
354
                 * The Full(0) certificate decodes to a seemingly valid X.509
355
                 * object with a plausible key, so the TLSA record is well
356
                 * formed.  However, we don't actually need the certificate for
357
                 * usages PKIX-EE(1) or DANE-EE(3), because at least the EE
358
                 * certificate is always presented by the peer.  We discard the
359
                 * certificate, and just use the TLSA data as an opaque blob
360
                 * for matching the raw presented DER octets.
361
                 *
362
                 * DO NOT FREE `t` here, it will be added to the TLSA record
363
                 * list below!
364
                 */
365
0
                X509_free(cert);
366
0
                break;
367
0
            }
368
369
            /*
370
             * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
371
             * records that contain full certificates of trust-anchors that are
372
             * not present in the wire chain.  For usage PKIX-TA(0), we augment
373
             * the chain with untrusted Full(0) certificates from DNS, in case
374
             * they are missing from the chain.
375
             */
376
0
            if ((dane->certs == NULL && (dane->certs = sk_X509_new_null()) == NULL) || !sk_X509_push(dane->certs, cert)) {
377
0
                ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
378
0
                X509_free(cert);
379
0
                tlsa_free(t);
380
0
                return -1;
381
0
            }
382
0
            break;
383
384
0
        case DANETLS_SELECTOR_SPKI:
385
0
            if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data || dlen != (size_t)(p - data)) {
386
0
                EVP_PKEY_free(pkey);
387
0
                tlsa_free(t);
388
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
389
0
                return 0;
390
0
            }
391
392
            /*
393
             * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
394
             * records that contain full bare keys of trust-anchors that are
395
             * not present in the wire chain.
396
             */
397
0
            if (usage == DANETLS_USAGE_DANE_TA)
398
0
                t->spki = pkey;
399
0
            else
400
0
                EVP_PKEY_free(pkey);
401
0
            break;
402
0
        }
403
0
    }
404
405
    /*-
406
     * Find the right insertion point for the new record.
407
     *
408
     * See crypto/x509/x509_vfy.c.  We sort DANE-EE(3) records first, so that
409
     * they can be processed first, as they require no chain building, and no
410
     * expiration or hostname checks.  Because DANE-EE(3) is numerically
411
     * largest, this is accomplished via descending sort by "usage".
412
     *
413
     * We also sort in descending order by matching ordinal to simplify
414
     * the implementation of digest agility in the verification code.
415
     *
416
     * The choice of order for the selector is not significant, so we
417
     * use the same descending order for consistency.
418
     */
419
0
    num = sk_danetls_record_num(dane->trecs);
420
0
    for (i = 0; i < num; ++i) {
421
0
        danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
422
423
0
        if (rec->usage > usage)
424
0
            continue;
425
0
        if (rec->usage < usage)
426
0
            break;
427
0
        if (rec->selector > selector)
428
0
            continue;
429
0
        if (rec->selector < selector)
430
0
            break;
431
0
        if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
432
0
            continue;
433
0
        break;
434
0
    }
435
436
0
    if (!sk_danetls_record_insert(dane->trecs, t, i)) {
437
0
        tlsa_free(t);
438
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
439
0
        return -1;
440
0
    }
441
0
    dane->umask |= DANETLS_USAGE_BIT(usage);
442
443
0
    return 1;
444
0
}
445
446
/*
447
 * Return 0 if there is only one version configured and it was disabled
448
 * at configure time.  Return 1 otherwise.
449
 */
450
static int ssl_check_allowed_versions(int min_version, int max_version)
451
95.1k
{
452
95.1k
    int minisdtls = 0, maxisdtls = 0;
453
454
    /* Figure out if we're doing DTLS versions or TLS versions */
455
95.1k
    if (min_version == DTLS1_BAD_VER
456
95.1k
        || min_version >> 8 == DTLS1_VERSION_MAJOR)
457
0
        minisdtls = 1;
458
95.1k
    if (max_version == DTLS1_BAD_VER
459
95.1k
        || max_version >> 8 == DTLS1_VERSION_MAJOR)
460
0
        maxisdtls = 1;
461
    /* A wildcard version of 0 could be DTLS or TLS. */
462
95.1k
    if ((minisdtls && !maxisdtls && max_version != 0)
463
95.1k
        || (maxisdtls && !minisdtls && min_version != 0)) {
464
        /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
465
0
        return 0;
466
0
    }
467
468
95.1k
    if (minisdtls || maxisdtls) {
469
        /* Do DTLS version checks. */
470
0
        if (min_version == 0)
471
            /* Ignore DTLS1_BAD_VER */
472
0
            min_version = DTLS1_VERSION;
473
0
        if (max_version == 0)
474
0
            max_version = DTLS1_3_VERSION;
475
#ifdef OPENSSL_NO_DTLS1_3
476
        if (max_version == DTLS1_3_VERSION)
477
            max_version = DTLS1_2_VERSION;
478
#endif
479
#ifdef OPENSSL_NO_DTLS1_2
480
        if (max_version == DTLS1_2_VERSION)
481
            max_version = DTLS1_VERSION;
482
#endif
483
#ifdef OPENSSL_NO_DTLS1
484
        if (min_version == DTLS1_VERSION)
485
            min_version = DTLS1_2_VERSION;
486
#endif
487
        /* Done massaging versions; do the check. */
488
0
        if (0
489
#ifdef OPENSSL_NO_DTLS1
490
            || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
491
                && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
492
#endif
493
#ifdef OPENSSL_NO_DTLS1_2
494
            || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
495
                && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
496
#endif
497
#ifdef OPENSSL_NO_DTLS1_3
498
            || (DTLS_VERSION_GE(min_version, DTLS1_3_VERSION)
499
                && DTLS_VERSION_GE(DTLS1_3_VERSION, max_version))
500
#endif
501
0
        )
502
0
            return 0;
503
95.1k
    } else {
504
        /* Regular TLS version checks. */
505
95.1k
        if (min_version == 0)
506
70.5k
            min_version = TLS1_VERSION;
507
95.1k
        if (max_version == 0)
508
95.1k
            max_version = TLS1_3_VERSION;
509
#ifdef OPENSSL_NO_TLS1_3
510
        if (max_version == TLS1_3_VERSION)
511
            max_version = TLS1_2_VERSION;
512
#endif
513
#ifdef OPENSSL_NO_TLS1_2
514
        if (max_version == TLS1_2_VERSION)
515
            max_version = TLS1_1_VERSION;
516
#endif
517
#ifdef OPENSSL_NO_TLS1_1
518
        if (max_version == TLS1_1_VERSION)
519
            max_version = TLS1_VERSION;
520
#endif
521
#ifdef OPENSSL_NO_TLS1
522
        if (min_version == TLS1_VERSION)
523
            min_version = TLS1_1_VERSION;
524
#endif
525
#ifdef OPENSSL_NO_TLS1_1
526
        if (min_version == TLS1_1_VERSION)
527
            min_version = TLS1_2_VERSION;
528
#endif
529
#ifdef OPENSSL_NO_TLS1_2
530
        if (min_version == TLS1_2_VERSION)
531
            min_version = TLS1_3_VERSION;
532
#endif
533
        /* Done massaging versions; do the check. */
534
95.1k
        if (0
535
#ifdef OPENSSL_NO_TLS1
536
            || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
537
#endif
538
#ifdef OPENSSL_NO_TLS1_1
539
            || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
540
#endif
541
#ifdef OPENSSL_NO_TLS1_2
542
            || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
543
#endif
544
#ifdef OPENSSL_NO_TLS1_3
545
            || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
546
#endif
547
95.1k
        )
548
0
            return 0;
549
95.1k
    }
550
95.1k
    return 1;
551
95.1k
}
552
553
#if defined(__TANDEM) && defined(OPENSSL_VPROC)
554
/*
555
 * Define a VPROC function for HP NonStop build ssl library.
556
 * This is used by platform version identification tools.
557
 * Do not inline this procedure or make it static.
558
 */
559
#define OPENSSL_VPROC_STRING_(x) x##_SSL
560
#define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
561
#define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
562
void OPENSSL_VPROC_FUNC(void) { }
563
#endif
564
565
int SSL_clear(SSL *s)
566
172k
{
567
172k
    if (s->method == NULL) {
568
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
569
0
        return 0;
570
0
    }
571
572
172k
    return s->method->ssl_reset(s);
573
172k
}
574
575
int ossl_ssl_connection_reset(SSL *s)
576
115k
{
577
115k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
578
579
115k
    if (sc == NULL)
580
0
        return 0;
581
582
115k
    if (ssl_clear_bad_session(sc)) {
583
0
        SSL_SESSION_free(sc->session);
584
0
        sc->session = NULL;
585
0
    }
586
115k
    SSL_SESSION_free(sc->psksession);
587
115k
    sc->psksession = NULL;
588
115k
    OPENSSL_free(sc->psksession_id);
589
115k
    sc->psksession_id = NULL;
590
115k
    sc->psksession_id_len = 0;
591
115k
    sc->hello_retry_request = SSL_HRR_NONE;
592
115k
    sc->dtls13_process_hello = 0;
593
115k
    sc->sent_tickets = 0;
594
595
115k
    sc->error = 0;
596
115k
    sc->hit = 0;
597
115k
    sc->shutdown = 0;
598
115k
    sc->ext.early_data_suppressed = 0;
599
115k
    sc->ext.early_exporter_ready = 0;
600
115k
    SSL_SESSION_free(sc->ext.early_data_session);
601
115k
    sc->ext.early_data_session = NULL;
602
115k
    sc->ext.tick_age_checked = 0;
603
115k
    sc->ext.tick_age_ms = 0;
604
605
115k
    if (sc->renegotiate) {
606
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
607
0
        return 0;
608
0
    }
609
610
115k
    ossl_statem_clear(sc);
611
612
115k
    sc->version = s->method->version;
613
115k
    sc->client_version = sc->version;
614
115k
    sc->rwstate = SSL_NOTHING;
615
616
115k
    BUF_MEM_free(sc->init_buf);
617
115k
    sc->init_buf = NULL;
618
115k
    sc->first_packet = 0;
619
620
115k
    sc->key_update = SSL_KEY_UPDATE_NONE;
621
115k
    memset(sc->ext.compress_certificate_from_peer, 0,
622
115k
        sizeof(sc->ext.compress_certificate_from_peer));
623
115k
    sc->ext.compress_certificate_sent = 0;
624
625
115k
    EVP_MD_CTX_free(sc->pha_dgst);
626
115k
    sc->pha_dgst = NULL;
627
628
    /* Reset DANE verification result state */
629
115k
    sc->dane.mdpth = -1;
630
115k
    sc->dane.pdpth = -1;
631
115k
    X509_free(sc->dane.mcert);
632
115k
    sc->dane.mcert = NULL;
633
115k
    sc->dane.mtlsa = NULL;
634
635
    /* Clear the verification result peername */
636
115k
    X509_VERIFY_PARAM_move_peername(sc->param, NULL);
637
638
    /* Clear any shared connection state */
639
115k
    OPENSSL_free(sc->shared_sigalgs);
640
115k
    sc->shared_sigalgs = NULL;
641
115k
    sc->shared_sigalgslen = 0;
642
643
    /*
644
     * Check to see if we were changed into a different method, if so, revert
645
     * back.
646
     */
647
115k
    if (s->method != s->defltmeth) {
648
        /*
649
         * For DTLS listener-created connections, we need to preserve the
650
         * peer_addr, rx (DTLS_RX), listener, and created_at across method changes.
651
         * These are set during connection creation and must survive SSL_clear().
652
         * The ssl_deinit/ssl_init sequence would otherwise free the old d1
653
         * structure and allocate a new one, losing these values.
654
         */
655
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
656
0
        BIO_ADDR saved_peer_addr = { 0 };
657
0
        DTLS_RX *saved_rx = NULL;
658
0
        SSL *saved_listener = NULL;
659
0
        OSSL_TIME saved_created_at = ossl_time_zero();
660
0
        unsigned int saved_req_blocking_mode = DTLS_BLOCKING_MODE_INHERIT;
661
0
        int is_dtls_listener_conn = 0;
662
663
0
        if (SSL_CONNECTION_IS_DTLS(sc) && sc->d1 != NULL
664
0
            && sc->d1->listener != NULL) {
665
0
            is_dtls_listener_conn = 1;
666
0
            saved_peer_addr = sc->d1->peer_addr;
667
0
            saved_rx = sc->d1->rx;
668
0
            saved_listener = sc->d1->listener;
669
0
            saved_created_at = sc->d1->created_at;
670
0
            saved_req_blocking_mode = sc->d1->req_blocking_mode;
671
            /*
672
             * Prevent dtls1_free from freeing rx and releasing the listener
673
             * reference - we'll restore them after ssl_init.
674
             */
675
0
            sc->d1->rx = NULL;
676
0
            sc->d1->listener = NULL;
677
0
        }
678
0
#endif
679
680
0
        s->method->ssl_deinit(s);
681
0
        s->method = s->defltmeth;
682
0
        if (!s->method->ssl_init(s)) {
683
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
684
0
            if (is_dtls_listener_conn) {
685
0
                DTLS_LISTENER *dl = (DTLS_LISTENER *)saved_listener;
686
687
                /* The detached connection cannot unregister itself on free. */
688
0
                ossl_dtls_listener_unregister_established_conn(saved_listener,
689
0
                    &saved_peer_addr);
690
0
                ossl_crypto_mutex_lock(dl->mutex);
691
0
                sc->listener_rx = NULL;
692
0
                ossl_crypto_mutex_unlock(dl->mutex);
693
0
                ossl_dtls_rx_free(saved_rx);
694
0
                SSL_free(saved_listener);
695
0
            }
696
0
#endif
697
0
            return 0;
698
0
        }
699
700
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
701
        /* Restore DTLS listener connection state */
702
0
        if (is_dtls_listener_conn && sc->d1 != NULL) {
703
0
            sc->d1->peer_addr = saved_peer_addr;
704
0
            sc->d1->rx = saved_rx;
705
0
            sc->d1->listener = saved_listener;
706
0
            sc->d1->created_at = saved_created_at;
707
            /*
708
             * The blocking mode is how the application configured this
709
             * connection, not handshake state, so it survives a clear.
710
             */
711
0
            sc->d1->req_blocking_mode = saved_req_blocking_mode;
712
0
        }
713
0
#endif
714
115k
    } else {
715
115k
        if (!s->method->ssl_clear(s))
716
0
            return 0;
717
115k
    }
718
719
115k
    ossl_quic_tls_clear(sc->qtls);
720
721
115k
    if (!RECORD_LAYER_reset(&sc->rlayer))
722
0
        return 0;
723
724
115k
    return 1;
725
115k
}
726
727
#ifndef OPENSSL_NO_DEPRECATED_3_0
728
/** Used to change an SSL_CTXs default SSL method type */
729
int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
730
0
{
731
0
    STACK_OF(SSL_CIPHER) *sk;
732
733
0
    if (IS_QUIC_CTX(ctx)) {
734
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
735
0
        return 0;
736
0
    }
737
738
0
    ctx->method = meth;
739
740
0
    if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
741
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
742
0
        return 0;
743
0
    }
744
0
    sk = ssl_create_cipher_list(ctx,
745
0
        ctx->tls13_ciphersuites,
746
0
        &(ctx->cipher_list),
747
0
        &(ctx->cipher_list_by_id),
748
0
        OSSL_default_cipher_list(), ctx->cert);
749
0
    if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
750
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
751
0
        return 0;
752
0
    }
753
0
    return 1;
754
0
}
755
#endif
756
757
SSL *SSL_new(SSL_CTX *ctx)
758
172k
{
759
172k
    if (ctx == NULL) {
760
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
761
0
        return NULL;
762
0
    }
763
172k
    if (ctx->method == NULL) {
764
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
765
0
        return NULL;
766
0
    }
767
172k
    return ctx->method->ssl_new(ctx);
768
172k
}
769
770
int ossl_ssl_init(SSL *ssl, SSL_CTX *ctx, const SSL_METHOD *method, int type)
771
193k
{
772
193k
    if (!SSL_CTX_up_ref(ctx))
773
0
        return 0;
774
775
193k
    ssl->lock = CRYPTO_THREAD_lock_new();
776
777
193k
    if (ssl->lock == NULL || !CRYPTO_NEW_REF(&ssl->references, 1))
778
0
        goto err;
779
780
193k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, ssl, &ssl->ex_data)) {
781
0
        CRYPTO_FREE_REF(&ssl->references);
782
0
        goto err;
783
0
    }
784
785
193k
    ssl->ctx = ctx;
786
193k
    ssl->type = type;
787
193k
    ssl->defltmeth = ssl->method = method;
788
789
193k
    return 1;
790
791
0
err:
792
0
    CRYPTO_THREAD_lock_free(ssl->lock);
793
0
    ssl->lock = NULL;
794
0
    SSL_CTX_free(ctx);
795
0
    return 0;
796
193k
}
797
798
SSL *ossl_ssl_connection_new_int(SSL_CTX *ctx, SSL *user_ssl,
799
    const SSL_METHOD *method)
800
57.8k
{
801
57.8k
    SSL_CONNECTION *s;
802
57.8k
    SSL *ssl;
803
804
57.8k
    s = OPENSSL_zalloc(sizeof(*s));
805
57.8k
    if (s == NULL)
806
0
        return NULL;
807
808
57.8k
    ssl = &s->ssl;
809
57.8k
    s->user_ssl = (user_ssl == NULL) ? ssl : user_ssl;
810
811
57.8k
    if (!ossl_ssl_init(ssl, ctx, method, SSL_TYPE_SSL_CONNECTION)) {
812
0
        OPENSSL_free(s);
813
0
        s = NULL;
814
0
        ssl = NULL;
815
0
        goto sslerr;
816
0
    }
817
818
57.8k
    RECORD_LAYER_init(&s->rlayer, s);
819
820
57.8k
    s->options = ctx->options;
821
822
57.8k
    s->dane.flags = ctx->dane.flags;
823
57.8k
    if (method->version == ctx->method->version) {
824
41.7k
        s->min_proto_version = ctx->min_proto_version;
825
41.7k
        s->max_proto_version = ctx->max_proto_version;
826
41.7k
    }
827
828
57.8k
    s->mode = ctx->mode;
829
57.8k
    s->max_cert_list = ctx->max_cert_list;
830
57.8k
    s->max_early_data = ctx->max_early_data;
831
57.8k
    s->recv_max_early_data = ctx->recv_max_early_data;
832
833
57.8k
    s->num_tickets = ctx->num_tickets;
834
57.8k
    s->pha_enabled = ctx->pha_enabled;
835
836
    /* Shallow copy of the ciphersuites stack */
837
57.8k
    s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
838
57.8k
    if (s->tls13_ciphersuites == NULL)
839
0
        goto cerr;
840
841
    /*
842
     * Earlier library versions used to copy the pointer to the CERT, not
843
     * its contents; only when setting new parameters for the per-SSL
844
     * copy, ssl_cert_new would be called (and the direct reference to
845
     * the per-SSL_CTX settings would be lost, but those still were
846
     * indirectly accessed for various purposes, and for that reason they
847
     * used to be known as s->ctx->default_cert). Now we don't look at the
848
     * SSL_CTX's CERT after having duplicated it once.
849
     */
850
57.8k
    s->cert = ssl_cert_dup(ctx->cert);
851
57.8k
    if (s->cert == NULL)
852
0
        goto sslerr;
853
854
57.8k
    RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
855
57.8k
    s->msg_callback = ctx->msg_callback;
856
57.8k
    s->msg_callback_arg = ctx->msg_callback_arg;
857
57.8k
    s->verify_mode = ctx->verify_mode;
858
57.8k
    s->not_resumable_session_cb = ctx->not_resumable_session_cb;
859
57.8k
    s->rlayer.record_padding_cb = ctx->record_padding_cb;
860
57.8k
    s->rlayer.record_padding_arg = ctx->record_padding_arg;
861
57.8k
    s->rlayer.block_padding = ctx->block_padding;
862
57.8k
    s->rlayer.hs_padding = ctx->hs_padding;
863
57.8k
    s->sid_ctx_length = ctx->sid_ctx_length;
864
57.8k
    if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
865
0
        goto err;
866
57.8k
    memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
867
57.8k
    s->verify_callback = ctx->default_verify_callback;
868
57.8k
    s->generate_session_id = ctx->generate_session_id;
869
870
57.8k
    s->param = X509_VERIFY_PARAM_new();
871
57.8k
    if (s->param == NULL)
872
0
        goto asn1err;
873
57.8k
    X509_VERIFY_PARAM_inherit(s->param, ctx->param);
874
57.8k
    s->quiet_shutdown = IS_QUIC_CTX(ctx) ? 0 : ctx->quiet_shutdown;
875
876
57.8k
    if (!IS_QUIC_CTX(ctx))
877
41.7k
        s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
878
879
57.8k
    s->max_send_fragment = ctx->max_send_fragment;
880
57.8k
    s->split_send_fragment = ctx->split_send_fragment;
881
57.8k
    s->max_pipelines = ctx->max_pipelines;
882
57.8k
    s->rlayer.default_read_buf_len = ctx->default_read_buf_len;
883
884
57.8k
    s->ext.debug_cb = 0;
885
57.8k
    s->ext.debug_arg = NULL;
886
57.8k
    s->ext.ticket_expected = 0;
887
57.8k
    s->ext.status_type = ctx->ext.status_type;
888
57.8k
    s->ext.status_expected = 0;
889
57.8k
    s->ext.ocsp.ids = NULL;
890
57.8k
    s->ext.ocsp.exts = NULL;
891
57.8k
    s->ext.ocsp.resp = NULL;
892
57.8k
    s->ext.ocsp.resp_len = 0;
893
57.8k
    s->ext.ocsp.resp_ex = NULL;
894
895
57.8k
    if (!SSL_CTX_up_ref(ctx))
896
0
        goto err;
897
898
57.8k
    s->session_ctx = ctx;
899
57.8k
    if (ctx->ext.supportedgroups != NULL) {
900
57.8k
        size_t add = 0;
901
902
57.8k
        if (ctx->ext.supportedgroups_len == 0)
903
            /* Add 1 so allocation won't fail */
904
0
            add = 1;
905
57.8k
        s->ext.supportedgroups = OPENSSL_memdup(ctx->ext.supportedgroups,
906
57.8k
            (ctx->ext.supportedgroups_len + add)
907
57.8k
                * sizeof(*ctx->ext.supportedgroups));
908
57.8k
        if (s->ext.supportedgroups == NULL) {
909
0
            s->ext.supportedgroups_len = 0;
910
0
            goto err;
911
0
        }
912
57.8k
        s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
913
57.8k
    }
914
57.8k
    if (ctx->ext.keyshares != NULL) {
915
57.8k
        size_t add = 0;
916
917
57.8k
        if (ctx->ext.keyshares_len == 0)
918
            /* Add 1 so allocation won't fail */
919
0
            add = 1;
920
57.8k
        s->ext.keyshares = OPENSSL_memdup(ctx->ext.keyshares,
921
57.8k
            (ctx->ext.keyshares_len + add)
922
57.8k
                * sizeof(*ctx->ext.keyshares));
923
57.8k
        if (s->ext.keyshares == NULL) {
924
0
            s->ext.keyshares_len = 0;
925
0
            goto err;
926
0
        }
927
57.8k
        s->ext.keyshares_len = ctx->ext.keyshares_len;
928
57.8k
    }
929
57.8k
    if (ctx->ext.tuples != NULL) {
930
57.8k
        size_t add = 0;
931
932
57.8k
        if (ctx->ext.tuples_len == 0)
933
            /* Add 1 so allocation won't fail */
934
0
            add = 1;
935
57.8k
        s->ext.tuples = OPENSSL_memdup(ctx->ext.tuples,
936
57.8k
            (ctx->ext.tuples_len + add)
937
57.8k
                * sizeof(*ctx->ext.tuples));
938
57.8k
        if (s->ext.tuples == NULL) {
939
0
            s->ext.tuples_len = 0;
940
0
            goto err;
941
0
        }
942
57.8k
        s->ext.tuples_len = ctx->ext.tuples_len;
943
57.8k
    }
944
945
57.8k
#ifndef OPENSSL_NO_NEXTPROTONEG
946
57.8k
    s->ext.npn = NULL;
947
57.8k
#endif
948
949
57.8k
    if (ctx->ext.alpn != NULL) {
950
0
        s->ext.alpn = OPENSSL_malloc(ctx->ext.alpn_len);
951
0
        if (s->ext.alpn == NULL) {
952
0
            s->ext.alpn_len = 0;
953
0
            goto err;
954
0
        }
955
0
        memcpy(s->ext.alpn, ctx->ext.alpn, ctx->ext.alpn_len);
956
0
        s->ext.alpn_len = ctx->ext.alpn_len;
957
0
    }
958
959
57.8k
    s->verified_chain = NULL;
960
57.8k
    s->verify_result = X509_V_OK;
961
962
57.8k
    s->default_passwd_callback = ctx->default_passwd_callback;
963
57.8k
    s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
964
965
57.8k
    s->key_update = SSL_KEY_UPDATE_NONE;
966
967
57.8k
    if (!IS_QUIC_CTX(ctx)) {
968
41.7k
        s->allow_early_data_cb = ctx->allow_early_data_cb;
969
41.7k
        s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
970
41.7k
    }
971
972
57.8k
    if (!method->ssl_init(ssl))
973
0
        goto sslerr;
974
975
57.8k
    s->server = (method->ssl_accept == ssl_undefined_function) ? 0 : 1;
976
977
57.8k
    if (!method->ssl_reset(ssl))
978
0
        goto sslerr;
979
980
57.8k
#ifndef OPENSSL_NO_PSK
981
57.8k
    s->psk_client_callback = ctx->psk_client_callback;
982
57.8k
    s->psk_server_callback = ctx->psk_server_callback;
983
57.8k
#endif
984
57.8k
    s->psk_find_session_cb = ctx->psk_find_session_cb;
985
57.8k
    s->psk_use_session_cb = ctx->psk_use_session_cb;
986
987
57.8k
    s->async_cb = ctx->async_cb;
988
57.8k
    s->async_cb_arg = ctx->async_cb_arg;
989
990
57.8k
    s->job = NULL;
991
992
#ifndef OPENSSL_NO_COMP_ALG
993
    memcpy(s->cert_comp_prefs, ctx->cert_comp_prefs, sizeof(s->cert_comp_prefs));
994
#endif
995
57.8k
    if (ctx->client_cert_type != NULL) {
996
0
        s->client_cert_type = OPENSSL_memdup(ctx->client_cert_type,
997
0
            ctx->client_cert_type_len);
998
0
        if (s->client_cert_type == NULL)
999
0
            goto sslerr;
1000
0
        s->client_cert_type_len = ctx->client_cert_type_len;
1001
0
    }
1002
57.8k
    if (ctx->server_cert_type != NULL) {
1003
0
        s->server_cert_type = OPENSSL_memdup(ctx->server_cert_type,
1004
0
            ctx->server_cert_type_len);
1005
0
        if (s->server_cert_type == NULL)
1006
0
            goto sslerr;
1007
0
        s->server_cert_type_len = ctx->server_cert_type_len;
1008
0
    }
1009
1010
57.8k
#ifndef OPENSSL_NO_CT
1011
57.8k
    if (!SSL_set_ct_validation_callback(ssl, ctx->ct_validation_callback,
1012
57.8k
            ctx->ct_validation_callback_arg))
1013
0
        goto sslerr;
1014
57.8k
#endif
1015
1016
57.8k
#ifndef OPENSSL_NO_ECH
1017
57.8k
    if (!ossl_ech_conn_init(s, ctx, method))
1018
0
        goto sslerr;
1019
57.8k
#endif
1020
1021
57.8k
    s->ssl_pkey_num = SSL_PKEY_NUM + ctx->sigalg_list_len;
1022
57.8k
    s->dtls13_process_hello = 0;
1023
57.8k
    return ssl;
1024
0
cerr:
1025
0
    ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
1026
0
    goto err;
1027
0
asn1err:
1028
0
    ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
1029
0
    goto err;
1030
0
sslerr:
1031
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
1032
0
err:
1033
0
    SSL_free(ssl);
1034
0
    return NULL;
1035
0
}
1036
1037
SSL *ossl_ssl_connection_new(SSL_CTX *ctx)
1038
119k
{
1039
119k
    return ossl_ssl_connection_new_int(ctx, NULL, ctx->method);
1040
119k
}
1041
1042
int SSL_is_dtls(const SSL *s)
1043
30.9M
{
1044
30.9M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1045
1046
30.9M
    if (s == NULL)
1047
104k
        return 0;
1048
1049
30.8M
#ifndef OPENSSL_NO_QUIC
1050
30.8M
    if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
1051
2.92M
        return 0;
1052
27.8M
#endif
1053
1054
27.8M
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
1055
    /* DTLS listener is always DTLS */
1056
27.8M
    if (IS_DTLS_LISTENER(s))
1057
0
        return 1;
1058
27.8M
#endif
1059
1060
27.8M
    if (sc == NULL)
1061
0
        return 0;
1062
1063
27.8M
    return SSL_CONNECTION_IS_DTLS(sc) ? 1 : 0;
1064
27.8M
}
1065
1066
int SSL_is_tls(const SSL *s)
1067
0
{
1068
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1069
1070
0
#ifndef OPENSSL_NO_QUIC
1071
0
    if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
1072
0
        return 0;
1073
0
#endif
1074
1075
0
    if (sc == NULL)
1076
0
        return 0;
1077
1078
0
    return SSL_CONNECTION_IS_DTLS(sc) ? 0 : 1;
1079
0
}
1080
1081
int SSL_is_quic(const SSL *s)
1082
0
{
1083
0
    return IS_QUIC(s);
1084
0
}
1085
1086
int SSL_CTX_is_quic(const SSL_CTX *c)
1087
0
{
1088
0
    return IS_QUIC_CTX(c);
1089
0
}
1090
1091
int SSL_CTX_is_server(const SSL_CTX *c)
1092
30.1k
{
1093
30.1k
    if (c == NULL || c->method == NULL)
1094
0
        return 0;
1095
30.1k
    return (c->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
1096
30.1k
}
1097
1098
int SSL_up_ref(SSL *s)
1099
4.30k
{
1100
4.30k
    int i;
1101
1102
4.30k
    if (!CRYPTO_UP_REF(&s->references, &i))
1103
0
        return 0;
1104
1105
4.30k
    REF_PRINT_COUNT("SSL", i, s);
1106
4.30k
    REF_ASSERT_ISNT(i < 2);
1107
4.30k
    return ((i > 1) ? 1 : 0);
1108
4.30k
}
1109
1110
int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
1111
    unsigned int sid_ctx_len)
1112
0
{
1113
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1114
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1115
0
        return 0;
1116
0
    }
1117
0
    ctx->sid_ctx_length = sid_ctx_len;
1118
0
    memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
1119
1120
0
    return 1;
1121
0
}
1122
1123
int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
1124
    unsigned int sid_ctx_len)
1125
0
{
1126
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1127
1128
0
    if (sc == NULL)
1129
0
        return 0;
1130
1131
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1132
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1133
0
        return 0;
1134
0
    }
1135
0
    sc->sid_ctx_length = sid_ctx_len;
1136
0
    memcpy(sc->sid_ctx, sid_ctx, sid_ctx_len);
1137
1138
0
    return 1;
1139
0
}
1140
1141
int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
1142
0
{
1143
0
    if (!CRYPTO_THREAD_write_lock(ctx->lock))
1144
0
        return 0;
1145
0
    ctx->generate_session_id = cb;
1146
0
    CRYPTO_THREAD_unlock(ctx->lock);
1147
0
    return 1;
1148
0
}
1149
1150
int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
1151
0
{
1152
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1153
1154
0
    if (sc == NULL || !CRYPTO_THREAD_write_lock(ssl->lock))
1155
0
        return 0;
1156
0
    sc->generate_session_id = cb;
1157
0
    CRYPTO_THREAD_unlock(ssl->lock);
1158
0
    return 1;
1159
0
}
1160
1161
/*
1162
 * Returns -1 on error, 0 if there is no match and 1 if there is a match.
1163
 */
1164
int ssl_has_matching_session_id(const SSL_CONNECTION *sc,
1165
    const unsigned char *id, unsigned int id_len)
1166
10.2k
{
1167
    /*
1168
     * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
1169
     * we can "construct" a session to give us the desired check - i.e. to
1170
     * find if there's a session in the hash table that would conflict with
1171
     * any new session built out of this id/id_len and the ssl_version in use
1172
     * by this SSL.
1173
     */
1174
10.2k
    SSL_SESSION r, *p;
1175
1176
10.2k
    if (sc == NULL || id_len > sizeof(r.session_id))
1177
0
        return -1;
1178
1179
10.2k
    r.ssl_version = sc->version;
1180
10.2k
    r.session_id_length = id_len;
1181
10.2k
    memcpy(r.session_id, id, id_len);
1182
1183
10.2k
    if (!CRYPTO_THREAD_read_lock(sc->session_ctx->lock))
1184
0
        return -1;
1185
10.2k
    p = lh_SSL_SESSION_retrieve(sc->session_ctx->sessions, &r);
1186
10.2k
    CRYPTO_THREAD_unlock(sc->session_ctx->lock);
1187
10.2k
    return (p != NULL);
1188
10.2k
}
1189
1190
int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
1191
    unsigned int id_len)
1192
{
1193
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
1194
1195
    /* Preserve the public API's boolean return value on internal errors. */
1196
    return ssl_has_matching_session_id(sc, id, id_len) > 0;
1197
}
1198
1199
int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
1200
0
{
1201
0
    return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
1202
0
}
1203
1204
int SSL_set_purpose(SSL *s, int purpose)
1205
0
{
1206
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1207
1208
0
    if (sc == NULL)
1209
0
        return 0;
1210
1211
0
    return X509_VERIFY_PARAM_set_purpose(sc->param, purpose);
1212
0
}
1213
1214
int SSL_CTX_set_trust(SSL_CTX *s, int trust)
1215
0
{
1216
0
    return X509_VERIFY_PARAM_set_trust(s->param, trust);
1217
0
}
1218
1219
int SSL_set_trust(SSL *s, int trust)
1220
0
{
1221
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1222
1223
0
    if (sc == NULL)
1224
0
        return 0;
1225
1226
0
    return X509_VERIFY_PARAM_set_trust(sc->param, trust);
1227
0
}
1228
1229
int SSL_set1_dnsname(SSL *s, const char *host)
1230
0
{
1231
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1232
1233
0
    if (sc == NULL)
1234
0
        return 0;
1235
1236
0
    return X509_VERIFY_PARAM_set1_host(sc->param, host, 0);
1237
0
}
1238
1239
int SSL_add1_dnsname(SSL *s, const char *host)
1240
0
{
1241
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1242
1243
0
    if (sc == NULL)
1244
0
        return 0;
1245
1246
0
    return X509_VERIFY_PARAM_add1_host(sc->param, host, strlen(host));
1247
0
}
1248
1249
int SSL_set1_ipaddr(SSL *s, const char *ipaddr)
1250
0
{
1251
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1252
1253
0
    if (sc == NULL)
1254
0
        return 0;
1255
1256
0
    return X509_VERIFY_PARAM_set1_ip_asc(sc->param, ipaddr);
1257
0
}
1258
1259
int SSL_add1_ipaddr(SSL *s, const char *ipaddr)
1260
0
{
1261
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1262
1263
0
    if (sc == NULL)
1264
0
        return 0;
1265
1266
0
    return X509_VERIFY_PARAM_add1_ip_asc(sc->param, ipaddr);
1267
0
}
1268
1269
#if !defined(OPENSSL_NO_DEPRECATED_4_0)
1270
int SSL_set1_host(SSL *s, const char *host)
1271
0
{
1272
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1273
1274
0
    if (sc == NULL)
1275
0
        return 0;
1276
1277
    /* clear hostname(s) and IP address in any case, also if host parses as an IP address */
1278
0
    (void)X509_VERIFY_PARAM_set1_host(sc->param, NULL, 0);
1279
0
    (void)X509_VERIFY_PARAM_set1_ip(sc->param, NULL, 0);
1280
0
    if (host == NULL)
1281
0
        return 1;
1282
1283
    /* If a host is provided and parses as an IP address, treat it as such. */
1284
0
    return X509_VERIFY_PARAM_set1_ip_asc(sc->param, host)
1285
0
        || X509_VERIFY_PARAM_set1_host(sc->param, host, 0);
1286
0
}
1287
1288
int SSL_add1_host(SSL *s, const char *host)
1289
0
{
1290
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1291
1292
0
    if (sc == NULL)
1293
0
        return 0;
1294
1295
    /* If a host is provided and parses as an IP address, treat it as such. */
1296
0
    if (host != NULL) {
1297
0
        ASN1_OCTET_STRING *ip;
1298
0
        char *old_ip;
1299
1300
0
        ip = a2i_IPADDRESS(host);
1301
0
        if (ip != NULL) {
1302
            /* We didn't want it; only to check if it *is* an IP address */
1303
0
            ASN1_OCTET_STRING_free(ip);
1304
1305
0
            old_ip = X509_VERIFY_PARAM_get1_ip_asc(sc->param);
1306
0
            if (old_ip != NULL) {
1307
0
                OPENSSL_free(old_ip);
1308
                /* There can be only one IP address */
1309
0
                ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,
1310
0
                    "IP address was already set");
1311
0
                return 0;
1312
0
            }
1313
1314
0
            return X509_VERIFY_PARAM_set1_ip_asc(sc->param, host);
1315
0
        }
1316
0
    }
1317
1318
0
    return X509_VERIFY_PARAM_add1_host(sc->param, host, 0);
1319
0
}
1320
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_0) */
1321
1322
void SSL_set_hostflags(SSL *s, unsigned int flags)
1323
0
{
1324
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1325
1326
0
    if (sc == NULL)
1327
0
        return;
1328
1329
0
    X509_VERIFY_PARAM_set_hostflags(sc->param, flags);
1330
0
}
1331
1332
const char *SSL_get0_peername(SSL *s)
1333
0
{
1334
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1335
1336
0
    if (sc == NULL)
1337
0
        return NULL;
1338
1339
0
    return X509_VERIFY_PARAM_get0_peername(sc->param);
1340
0
}
1341
1342
int SSL_CTX_dane_enable(SSL_CTX *ctx)
1343
0
{
1344
0
    return dane_ctx_enable(&ctx->dane);
1345
0
}
1346
1347
unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
1348
0
{
1349
0
    unsigned long orig = ctx->dane.flags;
1350
1351
0
    ctx->dane.flags |= flags;
1352
0
    return orig;
1353
0
}
1354
1355
unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
1356
0
{
1357
0
    unsigned long orig = ctx->dane.flags;
1358
1359
0
    ctx->dane.flags &= ~flags;
1360
0
    return orig;
1361
0
}
1362
1363
int SSL_dane_enable(SSL *s, const char *basedomain)
1364
0
{
1365
0
    SSL_DANE *dane;
1366
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1367
1368
0
    if (sc == NULL)
1369
0
        return 0;
1370
1371
0
    dane = &sc->dane;
1372
0
    if (s->ctx->dane.mdmax == 0) {
1373
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
1374
0
        return 0;
1375
0
    }
1376
0
    if (dane->trecs != NULL) {
1377
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
1378
0
        return 0;
1379
0
    }
1380
1381
    /*
1382
     * Default SNI name.  This rejects empty names, while set1_host below
1383
     * accepts them and disables hostname checks.  To avoid side-effects with
1384
     * invalid input, set the SNI name first.
1385
     */
1386
0
    if (sc->ext.hostname == NULL) {
1387
0
        if (!SSL_set_tlsext_host_name(s, basedomain)) {
1388
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1389
0
            return -1;
1390
0
        }
1391
0
    }
1392
1393
    /* Primary RFC6125 reference identifier */
1394
0
    if (!X509_VERIFY_PARAM_set1_host(sc->param, basedomain, 0)) {
1395
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1396
0
        return -1;
1397
0
    }
1398
1399
0
    dane->mdpth = -1;
1400
0
    dane->pdpth = -1;
1401
0
    dane->dctx = &s->ctx->dane;
1402
0
    dane->trecs = sk_danetls_record_new_null();
1403
1404
0
    if (dane->trecs == NULL) {
1405
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
1406
0
        return -1;
1407
0
    }
1408
0
    return 1;
1409
0
}
1410
1411
unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
1412
0
{
1413
0
    unsigned long orig;
1414
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1415
1416
0
    if (sc == NULL)
1417
0
        return 0;
1418
1419
0
    orig = sc->dane.flags;
1420
1421
0
    sc->dane.flags |= flags;
1422
0
    return orig;
1423
0
}
1424
1425
unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
1426
0
{
1427
0
    unsigned long orig;
1428
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1429
1430
0
    if (sc == NULL)
1431
0
        return 0;
1432
1433
0
    orig = sc->dane.flags;
1434
1435
0
    sc->dane.flags &= ~flags;
1436
0
    return orig;
1437
0
}
1438
1439
int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
1440
0
{
1441
0
    SSL_DANE *dane;
1442
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1443
1444
0
    if (sc == NULL)
1445
0
        return -1;
1446
1447
0
    dane = &sc->dane;
1448
1449
0
    if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
1450
0
        return -1;
1451
0
    if (dane->mtlsa) {
1452
0
        if (mcert)
1453
0
            *mcert = dane->mcert;
1454
0
        if (mspki)
1455
0
            *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
1456
0
    }
1457
0
    return dane->mdpth;
1458
0
}
1459
1460
int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
1461
    uint8_t *mtype, const unsigned char **data, size_t *dlen)
1462
0
{
1463
0
    SSL_DANE *dane;
1464
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1465
1466
0
    if (sc == NULL)
1467
0
        return -1;
1468
1469
0
    dane = &sc->dane;
1470
1471
0
    if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
1472
0
        return -1;
1473
0
    if (dane->mtlsa) {
1474
0
        if (usage)
1475
0
            *usage = dane->mtlsa->usage;
1476
0
        if (selector)
1477
0
            *selector = dane->mtlsa->selector;
1478
0
        if (mtype)
1479
0
            *mtype = dane->mtlsa->mtype;
1480
0
        if (data)
1481
0
            *data = dane->mtlsa->data;
1482
0
        if (dlen)
1483
0
            *dlen = dane->mtlsa->dlen;
1484
0
    }
1485
0
    return dane->mdpth;
1486
0
}
1487
1488
SSL_DANE *SSL_get0_dane(SSL *s)
1489
0
{
1490
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1491
1492
0
    if (sc == NULL)
1493
0
        return NULL;
1494
1495
0
    return &sc->dane;
1496
0
}
1497
1498
int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
1499
    uint8_t mtype, const unsigned char *data, size_t dlen)
1500
0
{
1501
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1502
1503
0
    if (sc == NULL)
1504
0
        return 0;
1505
1506
0
    return dane_tlsa_add(&sc->dane, usage, selector, mtype, data, dlen);
1507
0
}
1508
1509
int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
1510
    uint8_t ord)
1511
0
{
1512
0
    return dane_mtype_set(&ctx->dane, md, mtype, ord);
1513
0
}
1514
1515
int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
1516
0
{
1517
0
    return X509_VERIFY_PARAM_set1(ctx->param, vpm);
1518
0
}
1519
1520
int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
1521
0
{
1522
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1523
1524
0
    if (sc == NULL)
1525
0
        return 0;
1526
1527
0
    return X509_VERIFY_PARAM_set1(sc->param, vpm);
1528
0
}
1529
1530
X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
1531
0
{
1532
0
    return ctx->param;
1533
0
}
1534
1535
X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
1536
0
{
1537
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1538
1539
0
    if (sc == NULL)
1540
0
        return NULL;
1541
1542
0
    return sc->param;
1543
0
}
1544
1545
void SSL_certs_clear(SSL *s)
1546
0
{
1547
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1548
1549
0
    if (sc == NULL)
1550
0
        return;
1551
1552
0
    ssl_cert_clear_certs(sc->cert);
1553
0
}
1554
1555
void SSL_free(SSL *s)
1556
243k
{
1557
243k
    int i;
1558
1559
243k
    if (s == NULL)
1560
410
        return;
1561
243k
    CRYPTO_DOWN_REF(&s->references, &i);
1562
243k
    REF_PRINT_COUNT("SSL", i, s);
1563
243k
    if (i > 0)
1564
5.05k
        return;
1565
238k
    REF_ASSERT_ISNT(i < 0);
1566
1567
238k
    if (s->method != NULL)
1568
238k
        s->method->ssl_free(s);
1569
1570
238k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
1571
238k
    SSL_CTX_free(s->ctx);
1572
238k
    CRYPTO_THREAD_lock_free(s->lock);
1573
238k
    CRYPTO_FREE_REF(&s->references);
1574
1575
238k
    OPENSSL_free(s);
1576
238k
}
1577
1578
void ossl_ssl_connection_free(SSL *ssl)
1579
57.8k
{
1580
57.8k
    SSL_CONNECTION *s;
1581
1582
57.8k
    s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
1583
57.8k
    if (s == NULL) {
1584
        /*
1585
         * This is not an SSL_CONNECTION (e.g., DTLS listener).
1586
         * Still need to call ssl_deinit which handles type-specific cleanup.
1587
         */
1588
0
        if (ssl != NULL && ssl->method != NULL)
1589
0
            ssl->method->ssl_deinit(ssl);
1590
0
        return;
1591
0
    }
1592
1593
57.8k
    if (s->d1 != NULL && s->rlayer.wrl != NULL)
1594
21.1k
        dtls1_clear_current_wrl_from_sent_buffer(s);
1595
1596
    /*
1597
     * Ignore return values. This could result in user callbacks being called
1598
     * e.g. for the QUIC TLS record layer. So we do this early before we have
1599
     * freed other things.
1600
     */
1601
57.8k
    ssl_free_wbio_buffer(s);
1602
57.8k
    RECORD_LAYER_clear(&s->rlayer);
1603
1604
57.8k
    X509_VERIFY_PARAM_free(s->param);
1605
57.8k
    dane_final(&s->dane);
1606
1607
57.8k
    BUF_MEM_free(s->init_buf);
1608
1609
    /* add extra stuff */
1610
57.8k
    sk_SSL_CIPHER_free(s->cipher_list);
1611
57.8k
    sk_SSL_CIPHER_free(s->cipher_list_by_id);
1612
57.8k
    sk_SSL_CIPHER_free(s->tls13_ciphersuites);
1613
57.8k
    sk_SSL_CIPHER_free(s->peer_ciphers);
1614
1615
    /* Make the next call work :-) */
1616
57.8k
    if (s->session != NULL) {
1617
53.5k
        ssl_clear_bad_session(s);
1618
53.5k
        SSL_SESSION_free(s->session);
1619
53.5k
    }
1620
57.8k
    SSL_SESSION_free(s->psksession);
1621
57.8k
    SSL_SESSION_free(s->ext.early_data_session);
1622
57.8k
    OPENSSL_free(s->psksession_id);
1623
1624
57.8k
    ssl_cert_free(s->cert);
1625
57.8k
    OPENSSL_free(s->shared_sigalgs);
1626
    /* Free up if allocated */
1627
1628
57.8k
    OPENSSL_free(s->ext.hostname);
1629
57.8k
    SSL_CTX_free(s->session_ctx);
1630
57.8k
    OPENSSL_free(s->ext.peer_ecpointformats);
1631
57.8k
    OPENSSL_free(s->ext.supportedgroups);
1632
57.8k
    OPENSSL_free(s->ext.keyshares);
1633
57.8k
    OPENSSL_free(s->ext.tuples);
1634
57.8k
    OPENSSL_free(s->ext.peer_supportedgroups);
1635
57.8k
    sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
1636
1637
57.8k
#ifndef OPENSSL_NO_OCSP
1638
57.8k
    OPENSSL_free(s->ext.ocsp.resp);
1639
57.8k
    s->ext.ocsp.resp = NULL;
1640
57.8k
    s->ext.ocsp.resp_len = 0;
1641
1642
57.8k
    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
1643
57.8k
    sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free);
1644
57.8k
    s->ext.ocsp.resp_ex = NULL;
1645
57.8k
#endif
1646
57.8k
#ifndef OPENSSL_NO_CT
1647
57.8k
    SCT_LIST_free(s->scts);
1648
57.8k
    OPENSSL_free(s->ext.scts);
1649
57.8k
#endif
1650
57.8k
    OPENSSL_free(s->ext.alpn);
1651
57.8k
    OPENSSL_free(s->ext.tls13_cookie);
1652
57.8k
    if (s->clienthello != NULL)
1653
0
        OPENSSL_free(s->clienthello->pre_proc_exts);
1654
57.8k
    OPENSSL_free(s->clienthello);
1655
57.8k
    OPENSSL_free(s->pha_context);
1656
57.8k
    EVP_MD_CTX_free(s->pha_dgst);
1657
1658
57.8k
    sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
1659
57.8k
    sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
1660
1661
57.8k
    OPENSSL_free(s->client_cert_type);
1662
57.8k
    OPENSSL_free(s->server_cert_type);
1663
1664
57.8k
    OSSL_STACK_OF_X509_free(s->verified_chain);
1665
1666
57.8k
    if (ssl->method != NULL)
1667
57.8k
        ssl->method->ssl_deinit(ssl);
1668
1669
57.8k
    ASYNC_WAIT_CTX_free(s->waitctx);
1670
1671
57.8k
#if !defined(OPENSSL_NO_NEXTPROTONEG)
1672
57.8k
    OPENSSL_free(s->ext.npn);
1673
57.8k
#endif
1674
1675
57.8k
#ifndef OPENSSL_NO_SRTP
1676
57.8k
    sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
1677
57.8k
#endif
1678
1679
    /*
1680
     * We do this late. We want to ensure that any other references we held to
1681
     * these BIOs are freed first *before* we call BIO_free_all(), because
1682
     * BIO_free_all() will only free each BIO in the chain if the number of
1683
     * references to the first BIO have dropped to 0
1684
     */
1685
57.8k
    BIO_free_all(s->wbio);
1686
57.8k
    s->wbio = NULL;
1687
57.8k
    BIO_free_all(s->rbio);
1688
57.8k
    s->rbio = NULL;
1689
57.8k
    OPENSSL_free(s->s3.tmp.valid_flags);
1690
57.8k
#ifndef OPENSSL_NO_ECH
1691
57.8k
    ossl_ech_conn_clear(&s->ext.ech);
1692
57.8k
#endif
1693
57.8k
}
1694
1695
void SSL_set0_rbio(SSL *s, BIO *rbio)
1696
74.1k
{
1697
74.1k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1698
1699
74.1k
#ifndef OPENSSL_NO_QUIC
1700
74.1k
    if (IS_QUIC(s)) {
1701
16.2k
        ossl_quic_conn_set0_net_rbio(s, rbio);
1702
16.2k
        return;
1703
16.2k
    }
1704
57.8k
#endif
1705
1706
57.8k
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
1707
57.8k
    if (IS_DTLS_LISTENER(s)) {
1708
0
        ossl_dtls_listener_set0_net_rbio(s, rbio);
1709
0
        return;
1710
0
    }
1711
57.8k
#endif
1712
1713
57.8k
    if (sc == NULL)
1714
0
        return;
1715
1716
57.8k
    BIO_free_all(sc->rbio);
1717
57.8k
    sc->rbio = rbio;
1718
57.8k
    sc->rlayer.rrlmethod->set1_bio(sc->rlayer.rrl, sc->rbio);
1719
57.8k
}
1720
1721
void SSL_set0_wbio(SSL *s, BIO *wbio)
1722
74.1k
{
1723
74.1k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1724
1725
74.1k
#ifndef OPENSSL_NO_QUIC
1726
74.1k
    if (IS_QUIC(s)) {
1727
16.2k
        ossl_quic_conn_set0_net_wbio(s, wbio);
1728
16.2k
        return;
1729
16.2k
    }
1730
57.8k
#endif
1731
1732
57.8k
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
1733
57.8k
    if (IS_DTLS_LISTENER(s)) {
1734
0
        ossl_dtls_listener_set0_net_wbio(s, wbio);
1735
0
        return;
1736
0
    }
1737
57.8k
#endif
1738
1739
57.8k
    if (sc == NULL)
1740
0
        return;
1741
1742
    /*
1743
     * If the output buffering BIO is still in place, remove it
1744
     */
1745
57.8k
    if (sc->bbio != NULL)
1746
0
        sc->wbio = BIO_pop(sc->wbio);
1747
1748
57.8k
    BIO_free_all(sc->wbio);
1749
57.8k
    sc->wbio = wbio;
1750
1751
    /* Re-attach |bbio| to the new |wbio|. */
1752
57.8k
    if (sc->bbio != NULL)
1753
0
        sc->wbio = BIO_push(sc->bbio, sc->wbio);
1754
1755
57.8k
    sc->rlayer.wrlmethod->set1_bio(sc->rlayer.wrl, sc->wbio);
1756
57.8k
}
1757
1758
void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
1759
184k
{
1760
    /*
1761
     * For historical reasons, this function has many different cases in
1762
     * ownership handling.
1763
     */
1764
1765
    /* If nothing has changed, do nothing */
1766
184k
    if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
1767
0
        return;
1768
1769
    /*
1770
     * If the two arguments are equal then one fewer reference is granted by the
1771
     * caller than we want to take
1772
     */
1773
184k
    if (rbio != NULL && rbio == wbio) {
1774
42.8k
        if (!BIO_up_ref(rbio))
1775
0
            return;
1776
42.8k
    }
1777
1778
    /*
1779
     * If only the wbio is changed only adopt one reference.
1780
     */
1781
184k
    if (rbio == SSL_get_rbio(s)) {
1782
0
        SSL_set0_wbio(s, wbio);
1783
0
        return;
1784
0
    }
1785
    /*
1786
     * There is an asymmetry here for historical reasons. If only the rbio is
1787
     * changed AND the rbio and wbio were originally different, then we only
1788
     * adopt one reference.
1789
     */
1790
184k
    if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
1791
0
        SSL_set0_rbio(s, rbio);
1792
0
        return;
1793
0
    }
1794
1795
    /* Otherwise, adopt both references. */
1796
184k
    SSL_set0_rbio(s, rbio);
1797
184k
    SSL_set0_wbio(s, wbio);
1798
184k
}
1799
1800
BIO *SSL_get_rbio(const SSL *s)
1801
19.6M
{
1802
19.6M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1803
1804
19.6M
#ifndef OPENSSL_NO_QUIC
1805
19.6M
    if (IS_QUIC(s))
1806
32.4k
        return ossl_quic_conn_get_net_rbio(s);
1807
19.6M
#endif
1808
1809
19.6M
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
1810
19.6M
    if (IS_DTLS_LISTENER(s))
1811
0
        return ossl_dtls_listener_get_net_rbio(s);
1812
19.6M
#endif
1813
1814
19.6M
    if (sc == NULL)
1815
0
        return NULL;
1816
1817
19.6M
    return sc->rbio;
1818
19.6M
}
1819
1820
BIO *SSL_get_wbio(const SSL *s)
1821
236k
{
1822
236k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1823
1824
236k
#ifndef OPENSSL_NO_QUIC
1825
236k
    if (IS_QUIC(s))
1826
16.2k
        return ossl_quic_conn_get_net_wbio(s);
1827
220k
#endif
1828
1829
220k
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
1830
220k
    if (IS_DTLS_LISTENER(s))
1831
0
        return ossl_dtls_listener_get_net_wbio(s);
1832
220k
#endif
1833
1834
220k
    if (sc == NULL)
1835
0
        return NULL;
1836
1837
220k
    if (sc->bbio != NULL) {
1838
        /*
1839
         * If |bbio| is active, the true caller-configured BIO is its
1840
         * |next_bio|.
1841
         */
1842
162k
        return BIO_next(sc->bbio);
1843
162k
    }
1844
57.8k
    return sc->wbio;
1845
220k
}
1846
1847
int SSL_get_fd(const SSL *s)
1848
0
{
1849
0
    return SSL_get_rfd(s);
1850
0
}
1851
1852
int SSL_get_rfd(const SSL *s)
1853
0
{
1854
0
    int ret = -1;
1855
0
    BIO *b, *r;
1856
1857
0
    b = SSL_get_rbio(s);
1858
0
    r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1859
0
    if (r != NULL)
1860
0
        BIO_get_fd(r, &ret);
1861
0
    return ret;
1862
0
}
1863
1864
int SSL_get_wfd(const SSL *s)
1865
0
{
1866
0
    int ret = -1;
1867
0
    BIO *b, *r;
1868
1869
0
    b = SSL_get_wbio(s);
1870
0
    r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1871
0
    if (r != NULL)
1872
0
        BIO_get_fd(r, &ret);
1873
0
    return ret;
1874
0
}
1875
1876
#ifndef OPENSSL_NO_SOCK
1877
static const BIO_METHOD *fd_method(SSL *s)
1878
0
{
1879
0
#ifndef OPENSSL_NO_DGRAM
1880
0
    if (IS_QUIC(s))
1881
0
        return BIO_s_datagram();
1882
0
#endif
1883
1884
0
    return BIO_s_socket();
1885
0
}
1886
1887
int SSL_set_fd(SSL *s, int fd)
1888
0
{
1889
0
    int ret = 0;
1890
0
    BIO *bio = NULL;
1891
1892
0
    if (s->type == SSL_TYPE_QUIC_XSO) {
1893
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1894
0
        goto err;
1895
0
    }
1896
1897
0
    bio = BIO_new(fd_method(s));
1898
1899
0
    if (bio == NULL) {
1900
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1901
0
        goto err;
1902
0
    }
1903
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
1904
0
    SSL_set_bio(s, bio, bio);
1905
0
    ret = 1;
1906
0
err:
1907
0
    return ret;
1908
0
}
1909
1910
int SSL_set_wfd(SSL *s, int fd)
1911
0
{
1912
0
    BIO *rbio = SSL_get_rbio(s);
1913
0
    int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1914
1915
0
    if (s->type == SSL_TYPE_QUIC_XSO) {
1916
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1917
0
        return 0;
1918
0
    }
1919
1920
0
    if (rbio == NULL || BIO_method_type(rbio) != desired_type
1921
0
        || (int)BIO_get_fd(rbio, NULL) != fd) {
1922
0
        BIO *bio = BIO_new(fd_method(s));
1923
1924
0
        if (bio == NULL) {
1925
0
            ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1926
0
            return 0;
1927
0
        }
1928
0
        BIO_set_fd(bio, fd, BIO_NOCLOSE);
1929
0
        SSL_set0_wbio(s, bio);
1930
0
    } else {
1931
0
        if (!BIO_up_ref(rbio))
1932
0
            return 0;
1933
0
        SSL_set0_wbio(s, rbio);
1934
0
    }
1935
0
    return 1;
1936
0
}
1937
1938
int SSL_set_rfd(SSL *s, int fd)
1939
0
{
1940
0
    BIO *wbio = SSL_get_wbio(s);
1941
0
    int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1942
1943
0
    if (s->type == SSL_TYPE_QUIC_XSO) {
1944
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1945
0
        return 0;
1946
0
    }
1947
1948
0
    if (wbio == NULL || BIO_method_type(wbio) != desired_type
1949
0
        || ((int)BIO_get_fd(wbio, NULL) != fd)) {
1950
0
        BIO *bio = BIO_new(fd_method(s));
1951
1952
0
        if (bio == NULL) {
1953
0
            ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1954
0
            return 0;
1955
0
        }
1956
0
        BIO_set_fd(bio, fd, BIO_NOCLOSE);
1957
0
        SSL_set0_rbio(s, bio);
1958
0
    } else {
1959
0
        if (!BIO_up_ref(wbio))
1960
0
            return 0;
1961
0
        SSL_set0_rbio(s, wbio);
1962
0
    }
1963
1964
0
    return 1;
1965
0
}
1966
#endif
1967
1968
/* return length of latest Finished message we sent, copy to 'buf' */
1969
size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
1970
0
{
1971
0
    size_t ret = 0;
1972
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1973
1974
0
    if (sc == NULL)
1975
0
        return 0;
1976
1977
0
    ret = sc->s3.tmp.finish_md_len;
1978
0
    if (count > ret)
1979
0
        count = ret;
1980
0
    memcpy(buf, sc->s3.tmp.finish_md, count);
1981
0
    return ret;
1982
0
}
1983
1984
/* return length of latest Finished message we expected, copy to 'buf' */
1985
size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
1986
0
{
1987
0
    size_t ret = 0;
1988
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1989
1990
0
    if (sc == NULL)
1991
0
        return 0;
1992
1993
0
    ret = sc->s3.tmp.peer_finish_md_len;
1994
0
    if (count > ret)
1995
0
        count = ret;
1996
0
    memcpy(buf, sc->s3.tmp.peer_finish_md, count);
1997
0
    return ret;
1998
0
}
1999
2000
int SSL_get_verify_mode(const SSL *s)
2001
0
{
2002
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2003
2004
0
    if (sc == NULL)
2005
0
        return 0;
2006
2007
0
    return sc->verify_mode;
2008
0
}
2009
2010
int SSL_get_verify_depth(const SSL *s)
2011
0
{
2012
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2013
2014
0
    if (sc == NULL)
2015
0
        return 0;
2016
2017
0
    return X509_VERIFY_PARAM_get_depth(sc->param);
2018
0
}
2019
2020
int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
2021
0
{
2022
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2023
2024
0
    if (sc == NULL)
2025
0
        return NULL;
2026
2027
0
    return sc->verify_callback;
2028
0
}
2029
2030
int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
2031
0
{
2032
0
    return ctx->verify_mode;
2033
0
}
2034
2035
int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
2036
0
{
2037
0
    return X509_VERIFY_PARAM_get_depth(ctx->param);
2038
0
}
2039
2040
int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
2041
0
{
2042
0
    return ctx->default_verify_callback;
2043
0
}
2044
2045
void SSL_set_verify(SSL *s, int mode,
2046
    int (*callback)(int ok, X509_STORE_CTX *ctx))
2047
0
{
2048
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2049
2050
0
    if (sc == NULL)
2051
0
        return;
2052
2053
0
    sc->verify_mode = mode;
2054
0
    if (callback != NULL)
2055
0
        sc->verify_callback = callback;
2056
0
}
2057
2058
void SSL_set_verify_depth(SSL *s, int depth)
2059
0
{
2060
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2061
2062
0
    if (sc == NULL)
2063
0
        return;
2064
2065
0
    X509_VERIFY_PARAM_set_depth(sc->param, depth);
2066
0
}
2067
2068
void SSL_set_read_ahead(SSL *s, int yes)
2069
0
{
2070
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2071
0
    OSSL_PARAM options[2], *opts = options;
2072
2073
0
    if (sc == NULL)
2074
0
        return;
2075
2076
0
    RECORD_LAYER_set_read_ahead(&sc->rlayer, yes);
2077
2078
0
    *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
2079
0
        &sc->rlayer.read_ahead);
2080
0
    *opts = OSSL_PARAM_construct_end();
2081
2082
    /* Ignore return value */
2083
0
    sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
2084
0
}
2085
2086
int SSL_get_read_ahead(const SSL *s)
2087
0
{
2088
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
2089
2090
0
    if (sc == NULL)
2091
0
        return 0;
2092
2093
0
    return RECORD_LAYER_get_read_ahead(&sc->rlayer);
2094
0
}
2095
2096
int SSL_pending(const SSL *s)
2097
0
{
2098
0
    size_t pending = s->method->ssl_pending(s);
2099
2100
    /*
2101
     * SSL_pending cannot work properly if read-ahead is enabled
2102
     * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
2103
     * impossible to fix since SSL_pending cannot report errors that may be
2104
     * observed while scanning the new data. (Note that SSL_pending() is
2105
     * often used as a boolean value, so we'd better not return -1.)
2106
     *
2107
     * SSL_pending also cannot work properly if the value >INT_MAX. In that case
2108
     * we just return INT_MAX.
2109
     */
2110
0
    return pending < INT_MAX ? (int)pending : INT_MAX;
2111
0
}
2112
2113
int SSL_has_pending(const SSL *s)
2114
0
{
2115
    /*
2116
     * Similar to SSL_pending() but returns a 1 to indicate that we have
2117
     * processed or unprocessed data available or 0 otherwise (as opposed to the
2118
     * number of bytes available). Unlike SSL_pending() this will take into
2119
     * account read_ahead data. A 1 return simply indicates that we have data.
2120
     * That data may not result in any application data, or we may fail to parse
2121
     * the records for some reason.
2122
     */
2123
0
    const SSL_CONNECTION *sc;
2124
2125
0
#ifndef OPENSSL_NO_QUIC
2126
0
    if (IS_QUIC(s))
2127
0
        return ossl_quic_has_pending(s);
2128
0
#endif
2129
2130
0
    sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2131
2132
    /* Check buffered app data if any first */
2133
0
    if (SSL_CONNECTION_IS_DTLS(sc)
2134
0
        && DTLS_RECORD_LAYER_buffered_app_data_pending(&sc->rlayer))
2135
0
        return 1;
2136
2137
0
    if (RECORD_LAYER_processed_read_pending(&sc->rlayer))
2138
0
        return 1;
2139
2140
0
    return RECORD_LAYER_read_pending(&sc->rlayer);
2141
0
}
2142
2143
X509 *SSL_get1_peer_certificate(const SSL *s)
2144
0
{
2145
0
    X509 *r = SSL_get0_peer_certificate(s);
2146
2147
0
    if (r != NULL && !X509_up_ref(r))
2148
0
        return NULL;
2149
2150
0
    return r;
2151
0
}
2152
2153
X509 *SSL_get0_peer_certificate(const SSL *s)
2154
0
{
2155
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2156
2157
0
    if (sc == NULL)
2158
0
        return NULL;
2159
2160
0
    if (sc->session == NULL)
2161
0
        return NULL;
2162
0
    else
2163
0
        return sc->session->peer;
2164
0
}
2165
2166
STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
2167
0
{
2168
0
    STACK_OF(X509) *r;
2169
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2170
2171
0
    if (sc == NULL)
2172
0
        return NULL;
2173
2174
0
    if (sc->session == NULL)
2175
0
        r = NULL;
2176
0
    else
2177
0
        r = sc->session->peer_chain;
2178
2179
    /*
2180
     * If we are a client, cert_chain includes the peer's own certificate; if
2181
     * we are a server, it does not.
2182
     */
2183
2184
0
    return r;
2185
0
}
2186
2187
/*
2188
 * Now in theory, since the calling process own 't' it should be safe to
2189
 * modify.  We need to be able to read f without being hassled
2190
 */
2191
int SSL_copy_session_id(SSL *t, const SSL *f)
2192
0
{
2193
0
    int i;
2194
    /* TODO(QUIC FUTURE): Not allowed for QUIC currently. */
2195
0
    SSL_CONNECTION *tsc = SSL_CONNECTION_FROM_SSL_ONLY(t);
2196
0
    const SSL_CONNECTION *fsc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(f);
2197
2198
0
    if (tsc == NULL || fsc == NULL)
2199
0
        return 0;
2200
2201
    /* Do we need to do SSL locking? */
2202
0
    if (!SSL_set_session(t, SSL_get_session(f))) {
2203
0
        return 0;
2204
0
    }
2205
2206
    /*
2207
     * what if we are setup for one protocol version but want to talk another
2208
     */
2209
0
    if (t->method != f->method) {
2210
0
        t->method->ssl_deinit(t);
2211
0
        t->method = f->method;
2212
0
        if (t->method->ssl_init(t) == 0)
2213
0
            return 0;
2214
0
    }
2215
2216
0
    if (!CRYPTO_UP_REF(&fsc->cert->references, &i))
2217
0
        return 0;
2218
2219
0
    ssl_cert_free(tsc->cert);
2220
0
    tsc->cert = fsc->cert;
2221
0
    if (!SSL_set_session_id_context(t, fsc->sid_ctx, (int)fsc->sid_ctx_length)) {
2222
0
        return 0;
2223
0
    }
2224
2225
0
    return 1;
2226
0
}
2227
2228
/* Fix this so it checks all the valid key/cert options */
2229
int SSL_CTX_check_private_key(const SSL_CTX *ctx)
2230
0
{
2231
0
    if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
2232
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
2233
0
        return 0;
2234
0
    }
2235
0
    if (ctx->cert->key->privatekey == NULL) {
2236
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
2237
0
        return 0;
2238
0
    }
2239
0
    return X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey);
2240
0
}
2241
2242
/* Fix this function so that it takes an optional type parameter */
2243
int SSL_check_private_key(const SSL *ssl)
2244
0
{
2245
0
    const SSL_CONNECTION *sc;
2246
2247
0
    if ((sc = SSL_CONNECTION_FROM_CONST_SSL(ssl)) == NULL) {
2248
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
2249
0
        return 0;
2250
0
    }
2251
0
    if (sc->cert->key->x509 == NULL) {
2252
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
2253
0
        return 0;
2254
0
    }
2255
0
    if (sc->cert->key->privatekey == NULL) {
2256
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
2257
0
        return 0;
2258
0
    }
2259
0
    return X509_check_private_key(sc->cert->key->x509,
2260
0
        sc->cert->key->privatekey);
2261
0
}
2262
2263
int SSL_waiting_for_async(SSL *s)
2264
0
{
2265
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2266
2267
0
    if (sc == NULL)
2268
0
        return 0;
2269
2270
0
    if (sc->job)
2271
0
        return 1;
2272
2273
0
    return 0;
2274
0
}
2275
2276
int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
2277
0
{
2278
0
    ASYNC_WAIT_CTX *ctx;
2279
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2280
2281
0
    if (sc == NULL)
2282
0
        return 0;
2283
2284
0
    if ((ctx = sc->waitctx) == NULL)
2285
0
        return 0;
2286
0
    return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
2287
0
}
2288
2289
int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
2290
    OSSL_ASYNC_FD *delfd, size_t *numdelfds)
2291
0
{
2292
0
    ASYNC_WAIT_CTX *ctx;
2293
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2294
2295
0
    if (sc == NULL)
2296
0
        return 0;
2297
2298
0
    if ((ctx = sc->waitctx) == NULL)
2299
0
        return 0;
2300
0
    return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
2301
0
        numdelfds);
2302
0
}
2303
2304
int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
2305
0
{
2306
0
    ctx->async_cb = callback;
2307
0
    return 1;
2308
0
}
2309
2310
int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
2311
0
{
2312
0
    ctx->async_cb_arg = arg;
2313
0
    return 1;
2314
0
}
2315
2316
int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
2317
0
{
2318
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2319
2320
0
    if (sc == NULL)
2321
0
        return 0;
2322
2323
0
    sc->async_cb = callback;
2324
0
    return 1;
2325
0
}
2326
2327
int SSL_set_async_callback_arg(SSL *s, void *arg)
2328
0
{
2329
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2330
2331
0
    if (sc == NULL)
2332
0
        return 0;
2333
2334
0
    sc->async_cb_arg = arg;
2335
0
    return 1;
2336
0
}
2337
2338
int SSL_get_async_status(SSL *s, int *status)
2339
0
{
2340
0
    ASYNC_WAIT_CTX *ctx;
2341
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2342
2343
0
    if (sc == NULL)
2344
0
        return 0;
2345
2346
0
    if ((ctx = sc->waitctx) == NULL)
2347
0
        return 0;
2348
0
    *status = ASYNC_WAIT_CTX_get_status(ctx);
2349
0
    return 1;
2350
0
}
2351
2352
/*
2353
 * Reset the statem error_state to ERROR_STATE_NOERROR to avoid the
2354
 * error sticking.
2355
 * Designed to be called at the beginning of any function that
2356
 * is expected to be followed by a call to SSL_get_error().
2357
 * It returns 0 if the state machine is in the MSG_FLOW_ERROR state,
2358
 * in this case the calling function can and should return immediately.
2359
 */
2360
static int ssl_reset_error_state(SSL_CONNECTION *sc)
2361
27.5M
{
2362
27.5M
    if (sc == NULL)
2363
0
        return 1;
2364
2365
27.5M
    if (sc->statem.state == MSG_FLOW_ERROR) {
2366
3.12k
        sc->statem.error_state = ERROR_STATE_SSL;
2367
3.12k
        return 0;
2368
3.12k
    }
2369
2370
27.4M
    sc->statem.error_state = ERROR_STATE_NOERROR;
2371
27.4M
    return 1;
2372
27.5M
}
2373
2374
/*
2375
 * Check if the connection failed into the MSG_FLOW_ERROR state during
2376
 * the operation, if so, check the error stack and set the error_state
2377
 * correspondently.
2378
 * Designed to be called at the end of any function that
2379
 * is expected to be followed by a call to SSL_get_error().
2380
 */
2381
static void ssl_update_error_state(SSL_CONNECTION *sc)
2382
27.4M
{
2383
27.4M
    unsigned long l;
2384
2385
27.4M
    if (sc == NULL)
2386
0
        return;
2387
2388
27.4M
    if (sc->statem.state == MSG_FLOW_ERROR
2389
42.5k
        && sc->statem.error_state == ERROR_STATE_NOERROR) {
2390
42.5k
        l = ERR_peek_error();
2391
42.5k
        if (l == 0 || ERR_GET_LIB(l) == ERR_LIB_SYS)
2392
4.55k
            sc->statem.error_state = ERROR_STATE_SYSCALL;
2393
37.9k
        else
2394
37.9k
            sc->statem.error_state = ERROR_STATE_SSL;
2395
42.5k
    }
2396
27.4M
}
2397
2398
int SSL_accept(SSL *s)
2399
9.32k
{
2400
9.32k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2401
2402
9.32k
#ifndef OPENSSL_NO_QUIC
2403
9.32k
    if (IS_QUIC(s)) {
2404
0
        return s->method->ssl_accept(s);
2405
0
    }
2406
9.32k
#endif
2407
2408
9.32k
    if (sc == NULL)
2409
0
        return 0;
2410
2411
9.32k
    if (sc->handshake_func == NULL) {
2412
        /* Not properly initialized yet */
2413
0
        SSL_set_accept_state(s);
2414
0
    }
2415
2416
9.32k
    return SSL_do_handshake(s);
2417
9.32k
}
2418
2419
int SSL_connect(SSL *s)
2420
0
{
2421
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2422
2423
0
#ifndef OPENSSL_NO_QUIC
2424
0
    if (IS_QUIC(s)) {
2425
0
        return s->method->ssl_connect(s);
2426
0
    }
2427
0
#endif
2428
2429
0
    if (sc == NULL)
2430
0
        return 0;
2431
2432
0
    if (sc->handshake_func == NULL) {
2433
        /* Not properly initialized yet */
2434
0
        SSL_set_connect_state(s);
2435
0
    }
2436
2437
0
    return SSL_do_handshake(s);
2438
0
}
2439
2440
long SSL_get_default_timeout(const SSL *s)
2441
0
{
2442
0
    return (long int)ossl_time2seconds(s->method->get_timeout());
2443
0
}
2444
2445
static int ssl_async_wait_ctx_cb(void *arg)
2446
0
{
2447
0
    SSL *s = (SSL *)arg;
2448
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2449
2450
0
    if (sc == NULL)
2451
0
        return 0;
2452
2453
0
    return sc->async_cb(s, sc->async_cb_arg);
2454
0
}
2455
2456
static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
2457
    int (*func)(void *))
2458
0
{
2459
0
    int ret;
2460
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2461
2462
0
    if (sc == NULL)
2463
0
        return 0;
2464
2465
0
    if (sc->waitctx == NULL) {
2466
0
        sc->waitctx = ASYNC_WAIT_CTX_new();
2467
0
        if (sc->waitctx == NULL)
2468
0
            return -1;
2469
0
        if (sc->async_cb != NULL
2470
0
            && !ASYNC_WAIT_CTX_set_callback(sc->waitctx, ssl_async_wait_ctx_cb, s))
2471
0
            return -1;
2472
0
    }
2473
2474
0
    sc->rwstate = SSL_NOTHING;
2475
0
    switch (ASYNC_start_job(&sc->job, sc->waitctx, &ret, func, args,
2476
0
        sizeof(struct ssl_async_args))) {
2477
0
    case ASYNC_ERR:
2478
0
        sc->rwstate = SSL_NOTHING;
2479
0
        ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
2480
0
        sc->statem.error_state = ERROR_STATE_SSL;
2481
0
        return -1;
2482
0
    case ASYNC_PAUSE:
2483
0
        sc->rwstate = SSL_ASYNC_PAUSED;
2484
0
        return -1;
2485
0
    case ASYNC_NO_JOBS:
2486
0
        sc->rwstate = SSL_ASYNC_NO_JOBS;
2487
0
        return -1;
2488
0
    case ASYNC_FINISH:
2489
0
        sc->job = NULL;
2490
0
        return ret;
2491
0
    default:
2492
0
        sc->rwstate = SSL_NOTHING;
2493
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
2494
0
        sc->statem.error_state = ERROR_STATE_SSL;
2495
        /* Shouldn't happen */
2496
0
        return -1;
2497
0
    }
2498
0
}
2499
2500
static int ssl_io_intern(void *vargs)
2501
0
{
2502
0
    struct ssl_async_args *args;
2503
0
    SSL *s;
2504
0
    void *buf;
2505
0
    size_t num;
2506
0
    SSL_CONNECTION *sc;
2507
2508
0
    args = (struct ssl_async_args *)vargs;
2509
0
    s = args->s;
2510
0
    buf = args->buf;
2511
0
    num = args->num;
2512
0
    if ((sc = SSL_CONNECTION_FROM_SSL(s)) == NULL)
2513
0
        return -1;
2514
2515
0
    switch (args->type) {
2516
0
    case READFUNC:
2517
0
        return args->f.func_read(s, buf, num, &sc->asyncrw);
2518
0
    case WRITEFUNC:
2519
0
        return args->f.func_write(s, buf, num, &sc->asyncrw);
2520
0
    case OTHERFUNC:
2521
0
        return args->f.func_other(s);
2522
0
    }
2523
0
    return -1;
2524
0
}
2525
2526
int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
2527
24.4M
{
2528
24.4M
    int ret;
2529
24.4M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2530
2531
24.4M
#ifndef OPENSSL_NO_QUIC
2532
24.4M
    if (IS_QUIC(s))
2533
12.1M
        return s->method->ssl_read(s, buf, num, readbytes);
2534
12.3M
#endif
2535
2536
12.3M
    if (sc == NULL)
2537
0
        return -1;
2538
2539
12.3M
    if (ssl_reset_error_state(sc) == 0)
2540
0
        return -1;
2541
2542
12.3M
    if (sc->handshake_func == NULL) {
2543
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2544
0
        sc->statem.error_state = ERROR_STATE_SSL;
2545
0
        return -1;
2546
0
    }
2547
2548
12.3M
    if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
2549
0
        sc->rwstate = SSL_NOTHING;
2550
0
        return 0;
2551
0
    }
2552
2553
12.3M
    if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2554
12.3M
        || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
2555
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2556
0
        sc->statem.error_state = ERROR_STATE_SSL;
2557
0
        return 0;
2558
0
    }
2559
    /*
2560
     * If we are a client and haven't received the ServerHello etc then we
2561
     * better do that
2562
     */
2563
12.3M
    if (!ossl_statem_check_finish_init(sc, 0))
2564
0
        return -1;
2565
2566
12.3M
    if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2567
0
        struct ssl_async_args args;
2568
2569
0
        args.s = s;
2570
0
        args.buf = buf;
2571
0
        args.num = num;
2572
0
        args.type = READFUNC;
2573
0
        args.f.func_read = s->method->ssl_read;
2574
2575
0
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
2576
0
        *readbytes = sc->asyncrw;
2577
0
        ssl_update_error_state(sc);
2578
0
        return ret;
2579
12.3M
    } else {
2580
12.3M
        ret = s->method->ssl_read(s, buf, num, readbytes);
2581
12.3M
        ssl_update_error_state(sc);
2582
12.3M
        return ret;
2583
12.3M
    }
2584
12.3M
}
2585
2586
int SSL_read(SSL *s, void *buf, int num)
2587
24.4M
{
2588
24.4M
    int ret;
2589
24.4M
    size_t readbytes;
2590
24.4M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2591
2592
24.4M
    if (num < 0) {
2593
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2594
0
        if (sc != NULL)
2595
0
            sc->statem.error_state = ERROR_STATE_SSL;
2596
0
        return -1;
2597
0
    }
2598
2599
24.4M
    ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
2600
2601
    /*
2602
     * The cast is safe here because ret should be <= INT_MAX because num is
2603
     * <= INT_MAX
2604
     */
2605
24.4M
    if (ret > 0)
2606
5.27k
        ret = (int)readbytes;
2607
2608
24.4M
    return ret;
2609
24.4M
}
2610
2611
int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2612
0
{
2613
0
    int ret;
2614
2615
0
    ret = ssl_read_internal(s, buf, num, readbytes);
2616
0
    if (ret < 0)
2617
0
        ret = 0;
2618
2619
0
    return ret;
2620
0
}
2621
2622
int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
2623
5.38k
{
2624
5.38k
    int ret;
2625
5.38k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2626
2627
    /* TODO(QUIC 0RTT): 0-RTT support */
2628
5.38k
    if (sc == NULL) {
2629
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2630
0
        return SSL_READ_EARLY_DATA_ERROR;
2631
0
    }
2632
2633
5.38k
    if (ssl_reset_error_state(sc) == 0)
2634
0
        return 0;
2635
2636
5.38k
    if (!sc->server) {
2637
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2638
0
        sc->statem.error_state = ERROR_STATE_SSL;
2639
0
        return SSL_READ_EARLY_DATA_ERROR;
2640
0
    }
2641
2642
5.38k
    switch (sc->early_data_state) {
2643
5.38k
    case SSL_EARLY_DATA_NONE:
2644
5.38k
        if (!SSL_in_before(s)) {
2645
0
            ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2646
0
            sc->statem.error_state = ERROR_STATE_SSL;
2647
0
            return SSL_READ_EARLY_DATA_ERROR;
2648
0
        }
2649
        /* fall through */
2650
2651
5.38k
    case SSL_EARLY_DATA_ACCEPT_RETRY:
2652
5.38k
        sc->early_data_state = SSL_EARLY_DATA_ACCEPTING;
2653
5.38k
        ret = SSL_accept(s);
2654
5.38k
        if (ret <= 0) {
2655
            /* NBIO or error */
2656
4.31k
            sc->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
2657
4.31k
            return SSL_READ_EARLY_DATA_ERROR;
2658
4.31k
        }
2659
        /* fall through */
2660
2661
1.07k
    case SSL_EARLY_DATA_READ_RETRY:
2662
1.07k
        if (sc->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
2663
0
            sc->early_data_state = SSL_EARLY_DATA_READING;
2664
0
            ret = SSL_read_ex(s, buf, num, readbytes);
2665
            /*
2666
             * State machine will update early_data_state to
2667
             * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
2668
             * message
2669
             */
2670
0
            if (ret > 0 || (ret <= 0 && sc->early_data_state != SSL_EARLY_DATA_FINISHED_READING)) {
2671
0
                sc->early_data_state = SSL_EARLY_DATA_READ_RETRY;
2672
0
                return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
2673
0
                               : SSL_READ_EARLY_DATA_ERROR;
2674
0
            }
2675
1.07k
        } else {
2676
1.07k
            sc->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
2677
1.07k
        }
2678
1.07k
        *readbytes = 0;
2679
1.07k
        return SSL_READ_EARLY_DATA_FINISH;
2680
2681
0
    default:
2682
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2683
0
        sc->statem.error_state = ERROR_STATE_SSL;
2684
0
        return SSL_READ_EARLY_DATA_ERROR;
2685
5.38k
    }
2686
5.38k
}
2687
2688
int SSL_get_early_data_status(const SSL *s)
2689
0
{
2690
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
2691
2692
    /* TODO(QUIC 0RTT): 0-RTT support */
2693
0
    if (sc == NULL)
2694
0
        return 0;
2695
2696
0
    return sc->ext.early_data;
2697
0
}
2698
2699
static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
2700
0
{
2701
0
    int ret;
2702
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2703
2704
0
#ifndef OPENSSL_NO_QUIC
2705
0
    if (IS_QUIC(s))
2706
0
        return s->method->ssl_peek(s, buf, num, readbytes);
2707
0
#endif
2708
2709
0
    if (sc == NULL)
2710
0
        return 0;
2711
2712
0
    if (ssl_reset_error_state(sc) == 0)
2713
0
        return -1;
2714
2715
0
    if (sc->handshake_func == NULL) {
2716
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2717
0
        sc->statem.error_state = ERROR_STATE_SSL;
2718
0
        return -1;
2719
0
    }
2720
2721
0
    if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
2722
0
        return 0;
2723
0
    }
2724
0
    if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2725
0
        struct ssl_async_args args;
2726
2727
0
        args.s = s;
2728
0
        args.buf = buf;
2729
0
        args.num = num;
2730
0
        args.type = READFUNC;
2731
0
        args.f.func_read = s->method->ssl_peek;
2732
2733
0
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
2734
0
        *readbytes = sc->asyncrw;
2735
0
        ssl_update_error_state(sc);
2736
0
        return ret;
2737
0
    } else {
2738
0
        ret = s->method->ssl_peek(s, buf, num, readbytes);
2739
0
        ssl_update_error_state(sc);
2740
0
        return ret;
2741
0
    }
2742
0
}
2743
2744
int SSL_peek(SSL *s, void *buf, int num)
2745
0
{
2746
0
    int ret;
2747
0
    size_t readbytes;
2748
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2749
2750
0
    if (num < 0) {
2751
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2752
0
        if (sc != NULL)
2753
0
            sc->statem.error_state = ERROR_STATE_SSL;
2754
0
        return -1;
2755
0
    }
2756
2757
0
    ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
2758
2759
    /*
2760
     * The cast is safe here because ret should be <= INT_MAX because num is
2761
     * <= INT_MAX
2762
     */
2763
0
    if (ret > 0)
2764
0
        ret = (int)readbytes;
2765
2766
0
    return ret;
2767
0
}
2768
2769
int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2770
0
{
2771
0
    int ret;
2772
2773
0
    ret = ssl_peek_internal(s, buf, num, readbytes);
2774
0
    if (ret < 0)
2775
0
        ret = 0;
2776
2777
0
    return ret;
2778
0
}
2779
2780
int ssl_write_internal(SSL *s, const void *buf, size_t num,
2781
    uint64_t flags, size_t *written)
2782
205k
{
2783
205k
    int ret;
2784
205k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2785
2786
205k
#ifndef OPENSSL_NO_QUIC
2787
205k
    if (IS_QUIC(s))
2788
205k
        return ossl_quic_write_flags(s, buf, num, flags, written);
2789
0
#endif
2790
2791
0
    if (sc == NULL)
2792
0
        return 0;
2793
2794
0
    if (ssl_reset_error_state(sc) == 0)
2795
0
        return -1;
2796
2797
0
    if (sc->handshake_func == NULL) {
2798
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2799
0
        sc->statem.error_state = ERROR_STATE_SSL;
2800
0
        return -1;
2801
0
    }
2802
2803
0
    if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2804
0
        sc->rwstate = SSL_NOTHING;
2805
0
        ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2806
0
        sc->statem.error_state = ERROR_STATE_SSL;
2807
0
        return -1;
2808
0
    }
2809
2810
0
    if (flags != 0) {
2811
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_WRITE_FLAG);
2812
0
        sc->statem.error_state = ERROR_STATE_SSL;
2813
0
        return -1;
2814
0
    }
2815
2816
0
    if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2817
0
        || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
2818
0
        || sc->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
2819
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2820
0
        sc->statem.error_state = ERROR_STATE_SSL;
2821
0
        return 0;
2822
0
    }
2823
    /* If we are a client and haven't sent the Finished we better do that */
2824
0
    if (!ossl_statem_check_finish_init(sc, 1))
2825
0
        return -1;
2826
2827
0
    if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2828
0
        struct ssl_async_args args;
2829
2830
0
        args.s = s;
2831
0
        args.buf = (void *)buf;
2832
0
        args.num = num;
2833
0
        args.type = WRITEFUNC;
2834
0
        args.f.func_write = s->method->ssl_write;
2835
2836
0
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
2837
0
        *written = sc->asyncrw;
2838
0
        ssl_update_error_state(sc);
2839
0
        return ret;
2840
0
    } else {
2841
0
        ret = s->method->ssl_write(s, buf, num, written);
2842
0
        ssl_update_error_state(sc);
2843
0
        return ret;
2844
0
    }
2845
0
}
2846
2847
ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
2848
0
{
2849
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2850
#ifndef OPENSSL_NO_KTLS
2851
    ossl_ssize_t sbytes;
2852
#endif
2853
0
    int ret;
2854
2855
0
    if (sc == NULL)
2856
0
        return 0;
2857
2858
0
    if (ssl_reset_error_state(sc) == 0)
2859
0
        return -1;
2860
2861
0
    if (sc->handshake_func == NULL) {
2862
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2863
0
        sc->statem.error_state = ERROR_STATE_SSL;
2864
0
        return -1;
2865
0
    }
2866
2867
0
    if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2868
0
        sc->rwstate = SSL_NOTHING;
2869
0
        ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2870
0
        sc->statem.error_state = ERROR_STATE_SSL;
2871
0
        return -1;
2872
0
    }
2873
2874
0
    if (!BIO_get_ktls_send(sc->wbio)) {
2875
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2876
0
        sc->statem.error_state = ERROR_STATE_SSL;
2877
0
        return -1;
2878
0
    }
2879
2880
    /* If we have an alert to send, lets send it */
2881
0
    if (sc->s3.alert_dispatch != SSL_ALERT_DISPATCH_NONE) {
2882
0
        ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
2883
0
        ssl_update_error_state(sc);
2884
0
        if (ret <= 0) {
2885
            /* SSLfatal() already called if appropriate */
2886
0
            return ret;
2887
0
        }
2888
        /* if it went, fall through and send more stuff */
2889
0
    }
2890
2891
0
    sc->rwstate = SSL_WRITING;
2892
0
    if (BIO_flush(sc->wbio) <= 0) {
2893
0
        if (!BIO_should_retry(sc->wbio)) {
2894
0
            sc->rwstate = SSL_NOTHING;
2895
0
        } else {
2896
0
#ifdef EAGAIN
2897
0
            set_sys_error(EAGAIN);
2898
0
#endif
2899
0
        }
2900
0
        return -1;
2901
0
    }
2902
2903
0
#ifdef OPENSSL_NO_KTLS
2904
0
    ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
2905
0
        "can't call ktls_sendfile(), ktls disabled");
2906
0
    sc->statem.error_state = ERROR_STATE_SSL;
2907
0
    return -1;
2908
#else
2909
    ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, &sbytes, flags);
2910
    ssl_update_error_state(sc);
2911
    BIO_clear_retry_flags(sc->wbio);
2912
    if (ret < 0) {
2913
        if (BIO_sock_should_retry(ret)) {
2914
            BIO_set_retry_write(sc->wbio);
2915
            return (sbytes > 0 ? sbytes : ret);
2916
        } else {
2917
            ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
2918
                "ktls_sendfile failure");
2919
            sc->statem.error_state = ERROR_STATE_SYSCALL;
2920
        }
2921
        return ret;
2922
    }
2923
    sc->rwstate = SSL_NOTHING;
2924
    return sbytes;
2925
#endif
2926
0
}
2927
2928
int SSL_write(SSL *s, const void *buf, int num)
2929
205k
{
2930
205k
    int ret;
2931
205k
    size_t written;
2932
205k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2933
2934
205k
    if (num < 0) {
2935
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2936
0
        if (sc != NULL)
2937
0
            sc->statem.error_state = ERROR_STATE_SSL;
2938
0
        return -1;
2939
0
    }
2940
2941
205k
    ret = ssl_write_internal(s, buf, (size_t)num, 0, &written);
2942
2943
    /*
2944
     * The cast is safe here because ret should be <= INT_MAX because num is
2945
     * <= INT_MAX
2946
     */
2947
205k
    if (ret > 0)
2948
1.84k
        ret = (int)written;
2949
2950
205k
    return ret;
2951
205k
}
2952
2953
int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
2954
0
{
2955
0
    return SSL_write_ex2(s, buf, num, 0, written);
2956
0
}
2957
2958
int SSL_write_ex2(SSL *s, const void *buf, size_t num, uint64_t flags,
2959
    size_t *written)
2960
0
{
2961
0
    int ret;
2962
2963
0
    ret = ssl_write_internal(s, buf, num, flags, written);
2964
0
    if (ret < 0)
2965
0
        ret = 0;
2966
2967
0
    return ret;
2968
0
}
2969
2970
int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
2971
0
{
2972
0
    int ret, early_data_state;
2973
0
    size_t writtmp;
2974
0
    uint32_t partialwrite;
2975
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2976
2977
    /* TODO(QUIC 0RTT): This will need special handling for QUIC */
2978
0
    if (sc == NULL)
2979
0
        return 0;
2980
2981
0
    if (ssl_reset_error_state(sc) == 0)
2982
0
        return 0;
2983
2984
0
    switch (sc->early_data_state) {
2985
0
    case SSL_EARLY_DATA_NONE:
2986
        /*
2987
         * tls_construct_ctos_early_data() decided not to advertise the
2988
         * early_data extension.
2989
         *
2990
         * Succeed and report num bytes as written so the handshake can
2991
         * continue without 0-RTT; early data rejection can be detected via
2992
         * SSL_get_early_data_status().
2993
         */
2994
0
        if (!sc->server && sc->ext.early_data_suppressed && !SSL_in_before(s)) {
2995
            /*
2996
             * 0-RTT was suppressed. Complete the handshake behind the scenes
2997
             * and report success so the application's early data write loop
2998
             * finishes. Run the internal SSL_connect() in the CONNECTING state,
2999
             * as the non-suppressed path does, so ossl_statem_check_finish_init()
3000
             * does not mistake it for the application leaving the early data
3001
             * write sequence.
3002
             */
3003
0
            sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
3004
0
            ret = SSL_connect(s);
3005
0
            sc->early_data_state = SSL_EARLY_DATA_NONE;
3006
0
            if (ret <= 0)
3007
0
                return 0;
3008
0
            *written = num;
3009
0
            return 1;
3010
0
        }
3011
3012
0
        if (sc->server
3013
0
            || !SSL_in_before(s)
3014
0
            || ((sc->session == NULL || sc->session->ext.max_early_data == 0)
3015
0
                && (sc->psk_use_session_cb == NULL))) {
3016
0
            ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3017
0
            sc->statem.error_state = ERROR_STATE_SSL;
3018
0
            return 0;
3019
0
        }
3020
        /* fall through */
3021
3022
0
    case SSL_EARLY_DATA_CONNECT_RETRY:
3023
0
        sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
3024
0
        ret = SSL_connect(s);
3025
0
        if (ret <= 0) {
3026
            /*
3027
             * NBIO or error. Normally we stamp the state back to
3028
             * SSL_EARLY_DATA_CONNECT_RETRY so the next SSL_write_early_data()
3029
             * call resumes here. However tls_construct_ctos_early_data() may
3030
             * have reset early_data_state to SSL_EARLY_DATA_NONE because it
3031
             * decided not to send the early_data extension.
3032
             *
3033
             * In that case leave the state alone so the handshake can complete
3034
             * normally without 0-RTT.
3035
             */
3036
0
            if (!sc->ext.early_data_suppressed)
3037
0
                sc->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
3038
0
            return 0;
3039
0
        }
3040
0
        if (sc->early_data_state == SSL_EARLY_DATA_NONE
3041
0
            && sc->ext.early_data_suppressed) {
3042
0
            *written = num;
3043
0
            return 1;
3044
0
        }
3045
        /* fall through */
3046
3047
0
    case SSL_EARLY_DATA_WRITE_RETRY:
3048
0
        sc->early_data_state = SSL_EARLY_DATA_WRITING;
3049
        /*
3050
         * We disable partial write for early data because we don't keep track
3051
         * of how many bytes we've written between the SSL_write_ex() call and
3052
         * the flush if the flush needs to be retried)
3053
         */
3054
0
        partialwrite = sc->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
3055
0
        sc->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
3056
0
        ret = SSL_write_ex(s, buf, num, &writtmp);
3057
0
        sc->mode |= partialwrite;
3058
0
        if (!ret) {
3059
0
            sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
3060
0
            return ret;
3061
0
        }
3062
0
        sc->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
3063
        /* fall through */
3064
3065
0
    case SSL_EARLY_DATA_WRITE_FLUSH:
3066
        /* The buffering BIO is still in place so we need to flush it */
3067
0
        if (statem_flush(sc) != 1)
3068
0
            return 0;
3069
0
        *written = num;
3070
0
        sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
3071
0
        return 1;
3072
3073
0
    case SSL_EARLY_DATA_FINISHED_READING:
3074
0
    case SSL_EARLY_DATA_READ_RETRY:
3075
0
        early_data_state = sc->early_data_state;
3076
        /* We are a server writing to an unauthenticated client */
3077
0
        sc->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
3078
0
        ret = SSL_write_ex(s, buf, num, written);
3079
        /* The buffering BIO is still in place */
3080
0
        if (ret)
3081
0
            (void)BIO_flush(sc->wbio);
3082
0
        sc->early_data_state = early_data_state;
3083
0
        return ret;
3084
3085
0
    default:
3086
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3087
0
        sc->statem.error_state = ERROR_STATE_SSL;
3088
0
        return 0;
3089
0
    }
3090
0
}
3091
3092
int SSL_shutdown(SSL *s)
3093
0
{
3094
    /*
3095
     * Note that this function behaves differently from what one might
3096
     * expect.  Return values are 0 for no success (yet), 1 for success; but
3097
     * calling it once is usually not enough, even if blocking I/O is used
3098
     * (see ssl3_shutdown).
3099
     */
3100
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3101
0
    int ret;
3102
3103
0
    if (ssl_reset_error_state(sc) == 0)
3104
0
        return -1;
3105
3106
0
#ifndef OPENSSL_NO_QUIC
3107
0
    if (IS_QUIC(s))
3108
0
        return ossl_quic_conn_shutdown(s, 0, NULL, 0);
3109
0
#endif
3110
3111
0
    if (sc == NULL)
3112
0
        return -1;
3113
3114
0
    if (sc->handshake_func == NULL) {
3115
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
3116
0
        sc->statem.error_state = ERROR_STATE_SSL;
3117
0
        return -1;
3118
0
    }
3119
3120
0
    if (!SSL_in_init(s)) {
3121
0
        if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
3122
0
            struct ssl_async_args args;
3123
3124
0
            memset(&args, 0, sizeof(args));
3125
0
            args.s = s;
3126
0
            args.type = OTHERFUNC;
3127
0
            args.f.func_other = s->method->ssl_shutdown;
3128
3129
0
            ret = ssl_start_async_job(s, &args, ssl_io_intern);
3130
0
        } else {
3131
0
            ret = s->method->ssl_shutdown(s);
3132
0
        }
3133
0
    } else {
3134
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
3135
0
        sc->statem.error_state = ERROR_STATE_SSL;
3136
0
        return -1;
3137
0
    }
3138
3139
0
    ssl_update_error_state(sc);
3140
0
    return ret;
3141
0
}
3142
3143
int SSL_key_update(SSL *s, int updatetype)
3144
0
{
3145
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3146
3147
0
#ifndef OPENSSL_NO_QUIC
3148
0
    if (IS_QUIC(s))
3149
0
        return ossl_quic_key_update(s, updatetype);
3150
0
#endif
3151
3152
0
    if (sc == NULL)
3153
0
        return 0;
3154
3155
0
    if (!SSL_CONNECTION_IS_VERSION13(sc)) {
3156
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
3157
0
        return 0;
3158
0
    }
3159
3160
0
    if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
3161
0
        && updatetype != SSL_KEY_UPDATE_REQUESTED) {
3162
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
3163
0
        return 0;
3164
0
    }
3165
3166
0
    if (!SSL_is_init_finished(s)) {
3167
0
        ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
3168
0
        return 0;
3169
0
    }
3170
3171
0
    if (RECORD_LAYER_write_pending(&sc->rlayer)) {
3172
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
3173
0
        return 0;
3174
0
    }
3175
3176
0
    ossl_statem_set_in_init(sc, 1);
3177
0
    sc->key_update = updatetype;
3178
0
    return 1;
3179
0
}
3180
3181
int SSL_get_key_update_type(const SSL *s)
3182
0
{
3183
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3184
3185
0
#ifndef OPENSSL_NO_QUIC
3186
0
    if (IS_QUIC(s))
3187
0
        return ossl_quic_get_key_update_type(s);
3188
0
#endif
3189
3190
0
    if (sc == NULL)
3191
0
        return 0;
3192
3193
0
    return sc->key_update;
3194
0
}
3195
3196
/*
3197
 * Can we accept a renegotiation request?  If yes, set the flag and
3198
 * return 1 if yes. If not, raise error and return 0.
3199
 */
3200
static int can_renegotiate(const SSL_CONNECTION *sc)
3201
365
{
3202
365
    if (SSL_CONNECTION_IS_VERSION13(sc)) {
3203
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
3204
0
        return 0;
3205
0
    }
3206
3207
365
    if ((sc->options & SSL_OP_NO_RENEGOTIATION) != 0) {
3208
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
3209
0
        return 0;
3210
0
    }
3211
3212
365
    return 1;
3213
365
}
3214
3215
int SSL_renegotiate(SSL *s)
3216
0
{
3217
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
3218
3219
0
    if (sc == NULL)
3220
0
        return 0;
3221
3222
0
    if (!can_renegotiate(sc))
3223
0
        return 0;
3224
3225
0
    sc->renegotiate = 1;
3226
0
    sc->new_session = 1;
3227
0
    return s->method->ssl_renegotiate(s);
3228
0
}
3229
3230
int SSL_renegotiate_abbreviated(SSL *s)
3231
1.49k
{
3232
1.49k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
3233
3234
1.49k
    if (sc == NULL)
3235
0
        return 0;
3236
3237
1.49k
    if (!can_renegotiate(sc))
3238
0
        return 0;
3239
3240
1.49k
    sc->renegotiate = 1;
3241
1.49k
    sc->new_session = 0;
3242
1.49k
    return s->method->ssl_renegotiate(s);
3243
1.49k
}
3244
3245
int SSL_renegotiate_pending(const SSL *s)
3246
0
{
3247
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
3248
3249
0
    if (sc == NULL)
3250
0
        return 0;
3251
3252
    /*
3253
     * becomes true when negotiation is requested; false again once a
3254
     * handshake has finished
3255
     */
3256
0
    return (sc->renegotiate != 0);
3257
0
}
3258
3259
int SSL_new_session_ticket(SSL *s)
3260
0
{
3261
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3262
3263
0
    if (sc == NULL)
3264
0
        return 0;
3265
3266
    /* If we are in init because we're sending tickets, okay to send more. */
3267
0
    if ((SSL_in_init(s) && sc->ext.extra_tickets_expected == 0)
3268
0
        || SSL_IS_FIRST_HANDSHAKE(sc) || !sc->server
3269
0
        || !SSL_CONNECTION_IS_VERSION13(sc))
3270
0
        return 0;
3271
0
    sc->ext.extra_tickets_expected++;
3272
0
    if (!RECORD_LAYER_write_pending(&sc->rlayer) && !SSL_in_init(s))
3273
0
        ossl_statem_set_in_init(sc, 1);
3274
0
    return 1;
3275
0
}
3276
3277
long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
3278
321k
{
3279
321k
    return ossl_ctrl_internal(s, cmd, larg, parg, /*no_quic=*/0);
3280
321k
}
3281
3282
long ossl_ctrl_internal(SSL *s, int cmd, long larg, void *parg, int no_quic)
3283
309k
{
3284
309k
    long l;
3285
309k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3286
3287
    /*
3288
     * Routing of ctrl calls for QUIC is a little counterintuitive:
3289
     *
3290
     *   - Firstly (no_quic=0), we pass the ctrl directly to our QUIC
3291
     *     implementation in case it wants to handle the ctrl specially.
3292
     *
3293
     *   - If our QUIC implementation does not care about the ctrl, it
3294
     *     will reenter this function with no_quic=1 and we will try to handle
3295
     *     it directly using the QCSO SSL object stub (not the handshake layer
3296
     *     SSL object). This is important for e.g. the version configuration
3297
     *     ctrls below, which must use s->defltmeth (and not sc->defltmeth).
3298
     *
3299
     *   - If we don't handle a ctrl here specially, then processing is
3300
     *     redirected to the handshake layer SSL object.
3301
     */
3302
309k
    if (!no_quic && IS_QUIC(s))
3303
42.8k
        return s->method->ssl_ctrl(s, cmd, larg, parg);
3304
3305
266k
    if (sc == NULL)
3306
0
        return 0;
3307
3308
266k
    switch (cmd) {
3309
0
    case SSL_CTRL_GET_READ_AHEAD:
3310
0
        return RECORD_LAYER_get_read_ahead(&sc->rlayer);
3311
0
    case SSL_CTRL_SET_READ_AHEAD:
3312
0
        l = RECORD_LAYER_get_read_ahead(&sc->rlayer);
3313
0
        RECORD_LAYER_set_read_ahead(&sc->rlayer, larg);
3314
0
        return l;
3315
3316
0
    case SSL_CTRL_MODE: {
3317
0
        OSSL_PARAM options[2], *opts = options;
3318
3319
0
        sc->mode |= larg;
3320
3321
0
        *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
3322
0
            &sc->mode);
3323
0
        *opts = OSSL_PARAM_construct_end();
3324
3325
        /* Ignore return value */
3326
0
        sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
3327
3328
0
        return sc->mode;
3329
0
    }
3330
0
    case SSL_CTRL_CLEAR_MODE:
3331
0
        return (sc->mode &= ~larg);
3332
0
    case SSL_CTRL_GET_MAX_CERT_LIST:
3333
0
        return (long)sc->max_cert_list;
3334
0
    case SSL_CTRL_SET_MAX_CERT_LIST:
3335
0
        if (larg < 0)
3336
0
            return 0;
3337
0
        l = (long)sc->max_cert_list;
3338
0
        sc->max_cert_list = (size_t)larg;
3339
0
        return l;
3340
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
3341
0
        if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
3342
0
            return 0;
3343
#ifndef OPENSSL_NO_KTLS
3344
        if (sc->wbio != NULL && BIO_get_ktls_send(sc->wbio))
3345
            return 0;
3346
#endif /* OPENSSL_NO_KTLS */
3347
0
        sc->max_send_fragment = larg;
3348
0
        if (sc->max_send_fragment < sc->split_send_fragment)
3349
0
            sc->split_send_fragment = sc->max_send_fragment;
3350
0
        sc->rlayer.wrlmethod->set_max_frag_len(sc->rlayer.wrl, larg);
3351
0
        return 1;
3352
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
3353
0
        if ((size_t)larg > sc->max_send_fragment || larg == 0)
3354
0
            return 0;
3355
0
        sc->split_send_fragment = larg;
3356
0
        return 1;
3357
0
    case SSL_CTRL_SET_MAX_PIPELINES:
3358
0
        if (larg < 1 || larg > SSL_MAX_PIPELINES)
3359
0
            return 0;
3360
0
        sc->max_pipelines = larg;
3361
0
        if (sc->rlayer.rrlmethod->set_max_pipelines != NULL)
3362
0
            sc->rlayer.rrlmethod->set_max_pipelines(sc->rlayer.rrl, (size_t)larg);
3363
0
        return 1;
3364
0
    case SSL_CTRL_GET_RI_SUPPORT:
3365
0
        return sc->s3.send_connection_binding;
3366
0
    case SSL_CTRL_SET_RETRY_VERIFY:
3367
0
        sc->rwstate = SSL_RETRY_VERIFY;
3368
0
        return 1;
3369
0
    case SSL_CTRL_CERT_FLAGS:
3370
0
        return (sc->cert->cert_flags |= larg);
3371
0
    case SSL_CTRL_CLEAR_CERT_FLAGS:
3372
0
        return (sc->cert->cert_flags &= ~larg);
3373
3374
0
    case SSL_CTRL_GET_RAW_CIPHERLIST:
3375
0
        if (parg) {
3376
0
            if (sc->s3.tmp.ciphers_raw == NULL)
3377
0
                return 0;
3378
0
            *(unsigned char **)parg = sc->s3.tmp.ciphers_raw;
3379
0
            return (int)sc->s3.tmp.ciphers_rawlen;
3380
0
        } else {
3381
0
            return TLS_CIPHER_LEN;
3382
0
        }
3383
0
    case SSL_CTRL_GET_EXTMS_SUPPORT:
3384
0
        if (!sc->session || SSL_in_init(s) || ossl_statem_get_in_handshake(sc))
3385
0
            return -1;
3386
0
        if (sc->session->flags & SSL_SESS_FLAG_EXTMS)
3387
0
            return 1;
3388
0
        else
3389
0
            return 0;
3390
99.2k
    case SSL_CTRL_SET_MIN_PROTO_VERSION:
3391
99.2k
        return ssl_check_allowed_versions(larg, sc->max_proto_version)
3392
99.2k
            && ssl_set_version_bound(s->defltmeth->version, (int)larg,
3393
99.2k
                &sc->min_proto_version);
3394
1.72k
    case SSL_CTRL_GET_MIN_PROTO_VERSION:
3395
1.72k
        return sc->min_proto_version;
3396
12.6k
    case SSL_CTRL_SET_MAX_PROTO_VERSION:
3397
12.6k
        return ssl_check_allowed_versions(sc->min_proto_version, larg)
3398
12.6k
            && ssl_set_version_bound(s->defltmeth->version, (int)larg,
3399
12.6k
                &sc->max_proto_version);
3400
0
    case SSL_CTRL_GET_MAX_PROTO_VERSION:
3401
0
        return sc->max_proto_version;
3402
153k
    default:
3403
153k
        if (IS_QUIC(s))
3404
42.8k
            return SSL_ctrl((SSL *)sc, cmd, larg, parg);
3405
110k
        else
3406
110k
            return s->method->ssl_ctrl(s, cmd, larg, parg);
3407
266k
    }
3408
266k
}
3409
3410
long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
3411
0
{
3412
0
    return s->method->ssl_callback_ctrl(s, cmd, fp);
3413
0
}
3414
3415
LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
3416
0
{
3417
0
    return ctx->sessions;
3418
0
}
3419
3420
static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
3421
1.28k
{
3422
1.28k
    int res = 0;
3423
3424
1.28k
    if (ssl_tsan_lock(ctx)) {
3425
1.28k
        res = tsan_load(stat);
3426
1.28k
        ssl_tsan_unlock(ctx);
3427
1.28k
    }
3428
1.28k
    return res;
3429
1.28k
}
3430
3431
long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3432
186k
{
3433
186k
    long l;
3434
3435
    /* For some cases with ctx == NULL or larg == 1 perform syntax checks */
3436
186k
    if (cmd == SSL_CTRL_SET_GROUPS_LIST && larg == 1)
3437
0
        return tls1_set_groups_list(ctx, NULL, NULL, NULL, NULL, NULL, NULL, parg);
3438
186k
    if (ctx == NULL) {
3439
0
        switch (cmd) {
3440
0
        case SSL_CTRL_SET_SIGALGS_LIST:
3441
0
        case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
3442
0
            return tls1_set_sigalgs_list(ctx, NULL, parg, 0);
3443
0
        default:
3444
0
            return 0;
3445
0
        }
3446
0
    }
3447
3448
186k
    switch (cmd) {
3449
0
    case SSL_CTRL_GET_READ_AHEAD:
3450
0
        return ctx->read_ahead;
3451
0
    case SSL_CTRL_SET_READ_AHEAD:
3452
0
        l = ctx->read_ahead;
3453
0
        ctx->read_ahead = larg;
3454
0
        return l;
3455
3456
0
    case SSL_CTRL_SET_MSG_CALLBACK_ARG:
3457
0
        ctx->msg_callback_arg = parg;
3458
0
        return 1;
3459
3460
0
    case SSL_CTRL_GET_MAX_CERT_LIST:
3461
0
        return (long)ctx->max_cert_list;
3462
0
    case SSL_CTRL_SET_MAX_CERT_LIST:
3463
0
        if (larg < 0)
3464
0
            return 0;
3465
0
        l = (long)ctx->max_cert_list;
3466
0
        ctx->max_cert_list = (size_t)larg;
3467
0
        return l;
3468
3469
0
    case SSL_CTRL_SET_SESS_CACHE_SIZE:
3470
0
        if (larg < 0)
3471
0
            return 0;
3472
0
        l = (long)ctx->session_cache_size;
3473
0
        ctx->session_cache_size = (size_t)larg;
3474
0
        return l;
3475
1.97k
    case SSL_CTRL_GET_SESS_CACHE_SIZE:
3476
1.97k
        return (long)ctx->session_cache_size;
3477
0
    case SSL_CTRL_SET_SESS_CACHE_MODE:
3478
0
        l = ctx->session_cache_mode;
3479
0
        ctx->session_cache_mode = larg;
3480
0
        return l;
3481
0
    case SSL_CTRL_GET_SESS_CACHE_MODE:
3482
0
        return ctx->session_cache_mode;
3483
3484
987
    case SSL_CTRL_SESS_NUMBER:
3485
987
        return lh_SSL_SESSION_num_items(ctx->sessions);
3486
0
    case SSL_CTRL_SESS_CONNECT:
3487
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
3488
0
    case SSL_CTRL_SESS_CONNECT_GOOD:
3489
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
3490
0
    case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
3491
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
3492
0
    case SSL_CTRL_SESS_ACCEPT:
3493
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
3494
0
    case SSL_CTRL_SESS_ACCEPT_GOOD:
3495
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
3496
0
    case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
3497
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
3498
0
    case SSL_CTRL_SESS_HIT:
3499
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
3500
0
    case SSL_CTRL_SESS_CB_HIT:
3501
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
3502
0
    case SSL_CTRL_SESS_MISSES:
3503
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
3504
0
    case SSL_CTRL_SESS_TIMEOUTS:
3505
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
3506
0
    case SSL_CTRL_SESS_CACHE_FULL:
3507
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
3508
0
    case SSL_CTRL_MODE:
3509
0
        return (ctx->mode |= larg);
3510
0
    case SSL_CTRL_CLEAR_MODE:
3511
0
        return (ctx->mode &= ~larg);
3512
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
3513
0
        if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
3514
0
            return 0;
3515
0
        ctx->max_send_fragment = larg;
3516
0
        if (ctx->max_send_fragment < ctx->split_send_fragment)
3517
0
            ctx->split_send_fragment = ctx->max_send_fragment;
3518
0
        return 1;
3519
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
3520
0
        if ((size_t)larg > ctx->max_send_fragment || larg == 0)
3521
0
            return 0;
3522
0
        ctx->split_send_fragment = larg;
3523
0
        return 1;
3524
0
    case SSL_CTRL_SET_MAX_PIPELINES:
3525
0
        if (larg < 1 || larg > SSL_MAX_PIPELINES)
3526
0
            return 0;
3527
0
        ctx->max_pipelines = larg;
3528
0
        return 1;
3529
0
    case SSL_CTRL_CERT_FLAGS:
3530
0
        return (ctx->cert->cert_flags |= larg);
3531
0
    case SSL_CTRL_CLEAR_CERT_FLAGS:
3532
0
        return (ctx->cert->cert_flags &= ~larg);
3533
41.7k
    case SSL_CTRL_SET_MIN_PROTO_VERSION:
3534
41.7k
        return ssl_check_allowed_versions(larg, ctx->max_proto_version)
3535
41.7k
            && ssl_set_version_bound(ctx->method->version, (int)larg,
3536
41.7k
                &ctx->min_proto_version);
3537
0
    case SSL_CTRL_GET_MIN_PROTO_VERSION:
3538
0
        return ctx->min_proto_version;
3539
0
    case SSL_CTRL_SET_MAX_PROTO_VERSION:
3540
0
        return ssl_check_allowed_versions(ctx->min_proto_version, larg)
3541
0
            && ssl_set_version_bound(ctx->method->version, (int)larg,
3542
0
                &ctx->max_proto_version);
3543
0
    case SSL_CTRL_GET_MAX_PROTO_VERSION:
3544
0
        return ctx->max_proto_version;
3545
141k
    default:
3546
141k
        return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
3547
186k
    }
3548
186k
}
3549
3550
long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
3551
0
{
3552
0
    switch (cmd) {
3553
0
    case SSL_CTRL_SET_MSG_CALLBACK:
3554
0
        ctx->msg_callback = (void (*)(int write_p, int version, int content_type,
3555
0
            const void *buf, size_t len, SSL *ssl,
3556
0
            void *arg))(fp);
3557
0
        return 1;
3558
3559
0
    default:
3560
0
        return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
3561
0
    }
3562
0
}
3563
3564
int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
3565
7.61M
{
3566
7.61M
    if (a->id > b->id)
3567
2.93M
        return 1;
3568
4.67M
    if (a->id < b->id)
3569
4.35M
        return -1;
3570
326k
    return 0;
3571
4.67M
}
3572
3573
int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
3574
    const SSL_CIPHER *const *bp)
3575
154M
{
3576
154M
    if ((*ap)->id > (*bp)->id)
3577
85.6M
        return 1;
3578
68.4M
    if ((*ap)->id < (*bp)->id)
3579
68.3M
        return -1;
3580
80.9k
    return 0;
3581
68.4M
}
3582
3583
/*
3584
 * return a STACK of the ciphers available for the SSL and in order of
3585
 * preference
3586
 */
3587
STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
3588
511k
{
3589
511k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3590
3591
511k
    if (sc != NULL) {
3592
511k
        if (sc->cipher_list != NULL) {
3593
217k
            return sc->cipher_list;
3594
294k
        } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
3595
294k
            return s->ctx->cipher_list;
3596
294k
        }
3597
511k
    }
3598
0
    return NULL;
3599
511k
}
3600
3601
STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
3602
0
{
3603
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3604
3605
0
    if (sc == NULL || !sc->server)
3606
0
        return NULL;
3607
0
    return sc->peer_ciphers;
3608
0
}
3609
3610
STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
3611
267k
{
3612
267k
    STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
3613
267k
    int i;
3614
267k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3615
3616
267k
    if (sc == NULL)
3617
0
        return NULL;
3618
3619
267k
    ciphers = SSL_get_ciphers(s);
3620
267k
    if (!ciphers)
3621
0
        return NULL;
3622
267k
    if (!ssl_set_client_disabled(sc))
3623
0
        return NULL;
3624
27.5M
    for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
3625
27.3M
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
3626
27.3M
        if (!ssl_cipher_disabled(sc, c, SSL_SECOP_CIPHER_SUPPORTED)) {
3627
15.5M
            if (!sk)
3628
267k
                sk = sk_SSL_CIPHER_new_null();
3629
15.5M
            if (!sk)
3630
0
                return NULL;
3631
15.5M
            if (!sk_SSL_CIPHER_push(sk, c)) {
3632
0
                sk_SSL_CIPHER_free(sk);
3633
0
                return NULL;
3634
0
            }
3635
15.5M
        }
3636
27.3M
    }
3637
267k
    return sk;
3638
267k
}
3639
3640
/** return a STACK of the ciphers available for the SSL and in order of
3641
 * algorithm id */
3642
STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *s)
3643
80.9k
{
3644
80.9k
    if (s != NULL) {
3645
80.9k
        if (s->cipher_list_by_id != NULL)
3646
54.4k
            return s->cipher_list_by_id;
3647
26.5k
        else if (s->ssl.ctx != NULL
3648
26.5k
            && s->ssl.ctx->cipher_list_by_id != NULL)
3649
26.5k
            return s->ssl.ctx->cipher_list_by_id;
3650
80.9k
    }
3651
0
    return NULL;
3652
80.9k
}
3653
3654
/** The old interface to get the same thing as SSL_get_ciphers() */
3655
const char *SSL_get_cipher_list(const SSL *s, int n)
3656
0
{
3657
0
    const SSL_CIPHER *c;
3658
0
    STACK_OF(SSL_CIPHER) *sk;
3659
3660
0
    if (s == NULL)
3661
0
        return NULL;
3662
0
    sk = SSL_get_ciphers(s);
3663
0
    if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
3664
0
        return NULL;
3665
0
    c = sk_SSL_CIPHER_value(sk, n);
3666
0
    if (c == NULL)
3667
0
        return NULL;
3668
0
    return c->name;
3669
0
}
3670
3671
/** return a STACK of the ciphers available for the SSL_CTX and in order of
3672
 * preference */
3673
STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
3674
0
{
3675
0
    if (ctx != NULL)
3676
0
        return ctx->cipher_list;
3677
0
    return NULL;
3678
0
}
3679
3680
/*
3681
 * Distinguish between ciphers controlled by set_ciphersuite() and
3682
 * set_cipher_list() when counting.
3683
 */
3684
static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk, int isdtls)
3685
41.7k
{
3686
41.7k
    int i, num = 0;
3687
41.7k
    const SSL_CIPHER *c;
3688
41.7k
    const int version1_3 = isdtls ? DTLS1_3_VERSION : TLS1_3_VERSION;
3689
3690
41.7k
    if (sk == NULL)
3691
0
        return 0;
3692
7.22M
    for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
3693
7.18M
        int minversion;
3694
3695
7.18M
        c = sk_SSL_CIPHER_value(sk, i);
3696
7.18M
        minversion = isdtls ? c->min_dtls : c->min_tls;
3697
3698
7.18M
        if (PROTOCOL_VERSION_CMP(isdtls, minversion, version1_3) >= 0)
3699
125k
            continue;
3700
7.05M
        num++;
3701
7.05M
    }
3702
41.7k
    return num;
3703
41.7k
}
3704
3705
/** specify the ciphers to be used by default by the SSL_CTX */
3706
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
3707
17.4k
{
3708
17.4k
    STACK_OF(SSL_CIPHER) *sk;
3709
3710
17.4k
    sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
3711
17.4k
        &ctx->cipher_list, &ctx->cipher_list_by_id, str,
3712
17.4k
        ctx->cert);
3713
    /*
3714
     * ssl_create_cipher_list may return an empty stack if it was unable to
3715
     * find a cipher matching the given rule string (for example if the rule
3716
     * string specifies a cipher which has been disabled). This is not an
3717
     * error as far as ssl_create_cipher_list is concerned, and hence
3718
     * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
3719
     */
3720
17.4k
    if (sk == NULL)
3721
0
        return 0;
3722
17.4k
    if (ctx->method->num_ciphers() > 0
3723
17.4k
        && cipher_list_tls12_num(sk, SSL_CTX_IS_DTLS(ctx)) == 0) {
3724
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
3725
0
        return 0;
3726
0
    }
3727
17.4k
    return 1;
3728
17.4k
}
3729
3730
/** specify the ciphers to be used by the SSL */
3731
int SSL_set_cipher_list(SSL *s, const char *str)
3732
56.3k
{
3733
56.3k
    STACK_OF(SSL_CIPHER) *sk;
3734
56.3k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3735
56.3k
    SSL_CTX *ctx;
3736
3737
56.3k
    if (sc == NULL)
3738
0
        return 0;
3739
3740
56.3k
    ctx = s->ctx;
3741
56.3k
    sk = ssl_create_cipher_list(ctx, sc->tls13_ciphersuites,
3742
56.3k
        &sc->cipher_list, &sc->cipher_list_by_id, str,
3743
56.3k
        sc->cert);
3744
    /* see comment in SSL_CTX_set_cipher_list */
3745
56.3k
    if (sk == NULL)
3746
0
        return 0;
3747
56.3k
    if (ctx->method->num_ciphers() > 0
3748
56.3k
        && cipher_list_tls12_num(sk, SSL_CONNECTION_IS_DTLS(sc)) == 0) {
3749
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
3750
0
        return 0;
3751
0
    }
3752
56.3k
    return 1;
3753
56.3k
}
3754
3755
char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
3756
0
{
3757
0
    char *p;
3758
0
    STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
3759
0
    const SSL_CIPHER *c;
3760
0
    int i;
3761
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3762
3763
0
    if (size < 2 || buf == NULL)
3764
0
        return NULL;
3765
3766
0
    buf[0] = '\0';
3767
3768
0
    if (sc == NULL || !sc->server)
3769
0
        return NULL;
3770
3771
0
    p = buf;
3772
0
    clntsk = sc->peer_ciphers;
3773
0
    srvrsk = SSL_get_ciphers(s);
3774
3775
0
    if (clntsk == NULL || sk_SSL_CIPHER_num(clntsk) == 0
3776
0
        || srvrsk == NULL || sk_SSL_CIPHER_num(srvrsk) == 0)
3777
0
        return buf;
3778
3779
0
    for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
3780
0
        int n;
3781
3782
0
        c = sk_SSL_CIPHER_value(clntsk, i);
3783
0
        if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
3784
0
            continue;
3785
3786
0
        n = (int)OPENSSL_strnlen(c->name, size);
3787
0
        if (n >= size)
3788
0
            break;
3789
3790
0
        memcpy(p, c->name, n);
3791
0
        p += n;
3792
0
        *(p++) = ':';
3793
0
        size -= n + 1;
3794
0
    }
3795
3796
    /* No overlap */
3797
0
    if (p != buf)
3798
0
        p[-1] = '\0';
3799
3800
0
    return buf;
3801
0
}
3802
3803
/**
3804
 * Return the requested servername (SNI) value. Note that the behaviour varies
3805
 * depending on:
3806
 * - whether this is called by the client or the server,
3807
 * - if we are before or during/after the handshake,
3808
 * - if a resumption or normal handshake is being attempted/has occurred
3809
 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
3810
 *
3811
 * Note that only the host_name type is defined (RFC 3546).
3812
 */
3813
const char *SSL_get_servername(const SSL *s, int type)
3814
0
{
3815
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3816
0
    int server;
3817
3818
0
    if (sc == NULL)
3819
0
        return NULL;
3820
3821
    /*
3822
     * If we don't know if we are the client or the server yet then we assume
3823
     * client.
3824
     */
3825
0
    server = sc->handshake_func == NULL ? 0 : sc->server;
3826
3827
0
    if (type != TLSEXT_NAMETYPE_host_name)
3828
0
        return NULL;
3829
3830
0
    if (server) {
3831
        /**
3832
         * Server side
3833
         * In (D)TLSv1.3 on the server SNI is not associated with the session
3834
         * but in (D)TLSv1.2 or below it is.
3835
         *
3836
         * Before the handshake:
3837
         *  - return NULL
3838
         *
3839
         * During/after the handshake ((D)TLSv1.2 or below resumption occurred):
3840
         * - If a servername was accepted by the server in the original
3841
         *   handshake then it will return that servername, or NULL otherwise.
3842
         *
3843
         * During/after the handshake ((D)TLSv1.2 or below resumption did not occur):
3844
         * - The function will return the servername requested by the client in
3845
         *   this handshake or NULL if none was requested.
3846
         */
3847
0
        if (sc->hit && !SSL_CONNECTION_IS_VERSION13(sc))
3848
0
            return sc->session->ext.hostname;
3849
0
    } else {
3850
        /**
3851
         * Client side
3852
         *
3853
         * Before the handshake:
3854
         *  - If a servername has been set via a call to
3855
         *    SSL_set_tlsext_host_name() then it will return that servername
3856
         *  - If one has not been set, but a (D)TLSv1.2 resumption is being
3857
         *    attempted and the session from the original handshake had a
3858
         *    servername accepted by the server then it will return that
3859
         *    servername
3860
         *  - Otherwise it returns NULL
3861
         *
3862
         * During/after the handshake ((D)TLSv1.2 or below resumption occurred):
3863
         * - If the session from the original handshake had a servername accepted
3864
         *   by the server then it will return that servername.
3865
         * - Otherwise it returns the servername set via
3866
         *   SSL_set_tlsext_host_name() (or NULL if it was not called).
3867
         *
3868
         * During/after the handshake ((D)TLSv1.2 or below resumption did not occur):
3869
         * - It will return the servername set via SSL_set_tlsext_host_name()
3870
         *   (or NULL if it was not called).
3871
         */
3872
0
        if (SSL_in_before(s)) {
3873
0
            const int version1_3 = SSL_CONNECTION_IS_DTLS(sc) ? DTLS1_3_VERSION
3874
0
                                                              : TLS1_3_VERSION;
3875
3876
0
            if (sc->ext.hostname == NULL
3877
0
                && sc->session != NULL
3878
0
                && sc->session->ssl_version != version1_3)
3879
0
                return sc->session->ext.hostname;
3880
0
        } else {
3881
0
            if (!SSL_CONNECTION_IS_VERSION13(sc) && sc->hit
3882
0
                && sc->session->ext.hostname != NULL)
3883
0
                return sc->session->ext.hostname;
3884
0
        }
3885
0
    }
3886
3887
0
    return sc->ext.hostname;
3888
0
}
3889
3890
int SSL_get_servername_type(const SSL *s)
3891
0
{
3892
0
    if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
3893
0
        return TLSEXT_NAMETYPE_host_name;
3894
0
    return -1;
3895
0
}
3896
3897
/*
3898
 * SSL_select_next_proto implements the standard protocol selection. It is
3899
 * expected that this function is called from the callback set by
3900
 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
3901
 * vector of 8-bit, length prefixed byte strings. The length byte itself is
3902
 * not included in the length. A byte string of length 0 is invalid. No byte
3903
 * string may be truncated. The current, but experimental algorithm for
3904
 * selecting the protocol is: 1) If the server doesn't support NPN then this
3905
 * is indicated to the callback. In this case, the client application has to
3906
 * abort the connection or have a default application level protocol. 2) If
3907
 * the server supports NPN, but advertises an empty list then the client
3908
 * selects the first protocol in its list, but indicates via the API that this
3909
 * fallback case was enacted. 3) Otherwise, the client finds the first
3910
 * protocol in the server's list that it supports and selects this protocol.
3911
 * This is because it's assumed that the server has better information about
3912
 * which protocol a client should use. 4) If the client doesn't support any
3913
 * of the server's advertised protocols, then this is treated the same as
3914
 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
3915
 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
3916
 */
3917
int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
3918
    const unsigned char *server,
3919
    unsigned int server_len,
3920
    const unsigned char *client, unsigned int client_len)
3921
0
{
3922
0
    PACKET cpkt, csubpkt, spkt, ssubpkt;
3923
3924
0
    if (!PACKET_buf_init(&cpkt, client, client_len)
3925
0
        || !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
3926
0
        || PACKET_remaining(&csubpkt) == 0) {
3927
0
        *out = NULL;
3928
0
        *outlen = 0;
3929
0
        return OPENSSL_NPN_NO_OVERLAP;
3930
0
    }
3931
3932
    /*
3933
     * Set the default opportunistic protocol. Will be overwritten if we find
3934
     * a match.
3935
     */
3936
0
    *out = (unsigned char *)PACKET_data(&csubpkt);
3937
0
    *outlen = (unsigned char)PACKET_remaining(&csubpkt);
3938
3939
    /*
3940
     * For each protocol in server preference order, see if we support it.
3941
     */
3942
0
    if (PACKET_buf_init(&spkt, server, server_len)) {
3943
0
        while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
3944
0
            if (PACKET_remaining(&ssubpkt) == 0)
3945
0
                continue; /* Invalid - ignore it */
3946
0
            if (PACKET_buf_init(&cpkt, client, client_len)) {
3947
0
                while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
3948
0
                    if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
3949
0
                            PACKET_remaining(&ssubpkt))) {
3950
                        /* We found a match */
3951
0
                        *out = (unsigned char *)PACKET_data(&ssubpkt);
3952
0
                        *outlen = (unsigned char)PACKET_remaining(&ssubpkt);
3953
0
                        return OPENSSL_NPN_NEGOTIATED;
3954
0
                    }
3955
0
                }
3956
                /* Ignore spurious trailing bytes in the client list */
3957
0
            } else {
3958
                /* This should never happen */
3959
0
                return OPENSSL_NPN_NO_OVERLAP;
3960
0
            }
3961
0
        }
3962
        /* Ignore spurious trailing bytes in the server list */
3963
0
    }
3964
3965
    /*
3966
     * There's no overlap between our protocols and the server's list. We use
3967
     * the default opportunistic protocol selected earlier
3968
     */
3969
0
    return OPENSSL_NPN_NO_OVERLAP;
3970
0
}
3971
3972
#ifndef OPENSSL_NO_NEXTPROTONEG
3973
/*
3974
 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
3975
 * client's requested protocol for this connection and returns 0. If the
3976
 * client didn't request any protocol, then *data is set to NULL. Note that
3977
 * the client can request any protocol it chooses. The value returned from
3978
 * this function need not be a member of the list of supported protocols
3979
 * provided by the callback.
3980
 */
3981
void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
3982
    unsigned *len)
3983
0
{
3984
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3985
3986
0
    if (sc == NULL) {
3987
        /* We have no other way to indicate error */
3988
0
        *data = NULL;
3989
0
        *len = 0;
3990
0
        return;
3991
0
    }
3992
3993
0
    *data = sc->ext.npn;
3994
0
    if (*data == NULL) {
3995
0
        *len = 0;
3996
0
    } else {
3997
0
        *len = (unsigned int)sc->ext.npn_len;
3998
0
    }
3999
0
}
4000
4001
/*
4002
 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
4003
 * a TLS server needs a list of supported protocols for Next Protocol
4004
 * Negotiation. The returned list must be in wire format.  The list is
4005
 * returned by setting |out| to point to it and |outlen| to its length. This
4006
 * memory will not be modified, but one should assume that the SSL* keeps a
4007
 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
4008
 * wishes to advertise. Otherwise, no such extension will be included in the
4009
 * ServerHello.
4010
 */
4011
void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
4012
    SSL_CTX_npn_advertised_cb_func cb,
4013
    void *arg)
4014
0
{
4015
0
    if (IS_QUIC_CTX(ctx))
4016
        /* NPN not allowed for QUIC */
4017
0
        return;
4018
4019
0
    ctx->ext.npn_advertised_cb = cb;
4020
0
    ctx->ext.npn_advertised_cb_arg = arg;
4021
0
}
4022
4023
/*
4024
 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
4025
 * client needs to select a protocol from the server's provided list. |out|
4026
 * must be set to point to the selected protocol (which may be within |in|).
4027
 * The length of the protocol name must be written into |outlen|. The
4028
 * server's advertised protocols are provided in |in| and |inlen|. The
4029
 * callback can assume that |in| is syntactically valid. The client must
4030
 * select a protocol. It is fatal to the connection if this callback returns
4031
 * a value other than SSL_TLSEXT_ERR_OK.
4032
 */
4033
void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
4034
    SSL_CTX_npn_select_cb_func cb,
4035
    void *arg)
4036
0
{
4037
0
    if (IS_QUIC_CTX(ctx))
4038
        /* NPN not allowed for QUIC */
4039
0
        return;
4040
4041
0
    ctx->ext.npn_select_cb = cb;
4042
0
    ctx->ext.npn_select_cb_arg = arg;
4043
0
}
4044
#endif
4045
4046
static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
4047
53.0k
{
4048
53.0k
    unsigned int idx;
4049
4050
53.0k
    if (protos_len < 2 || protos == NULL)
4051
0
        return 0;
4052
4053
106k
    for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
4054
53.0k
        if (protos[idx] == 0)
4055
0
            return 0;
4056
53.0k
    }
4057
53.0k
    return idx == protos_len;
4058
53.0k
}
4059
/*
4060
 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
4061
 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
4062
 * length-prefixed strings). Returns 0 on success.
4063
 */
4064
int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
4065
    unsigned int protos_len)
4066
0
{
4067
0
    unsigned char *alpn;
4068
4069
0
    if (protos_len == 0 || protos == NULL) {
4070
0
        OPENSSL_free(ctx->ext.alpn);
4071
0
        ctx->ext.alpn = NULL;
4072
0
        ctx->ext.alpn_len = 0;
4073
0
        return 0;
4074
0
    }
4075
    /* Not valid per RFC */
4076
0
    if (!alpn_value_ok(protos, protos_len))
4077
0
        return 1;
4078
4079
0
    alpn = OPENSSL_memdup(protos, protos_len);
4080
0
    if (alpn == NULL)
4081
0
        return 1;
4082
0
    OPENSSL_free(ctx->ext.alpn);
4083
0
    ctx->ext.alpn = alpn;
4084
0
    ctx->ext.alpn_len = protos_len;
4085
4086
0
    return 0;
4087
0
}
4088
4089
/*
4090
 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
4091
 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
4092
 * length-prefixed strings). Returns 0 on success.
4093
 */
4094
int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
4095
    unsigned int protos_len)
4096
42.8k
{
4097
42.8k
    unsigned char *alpn;
4098
42.8k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
4099
4100
42.8k
    if (sc == NULL)
4101
0
        return 1;
4102
4103
42.8k
    if (protos_len == 0 || protos == NULL) {
4104
0
        OPENSSL_free(sc->ext.alpn);
4105
0
        sc->ext.alpn = NULL;
4106
0
        sc->ext.alpn_len = 0;
4107
0
        return 0;
4108
0
    }
4109
    /* Not valid per RFC */
4110
42.8k
    if (!alpn_value_ok(protos, protos_len))
4111
0
        return 1;
4112
4113
42.8k
    alpn = OPENSSL_memdup(protos, protos_len);
4114
42.8k
    if (alpn == NULL)
4115
0
        return 1;
4116
42.8k
    OPENSSL_free(sc->ext.alpn);
4117
42.8k
    sc->ext.alpn = alpn;
4118
42.8k
    sc->ext.alpn_len = protos_len;
4119
4120
42.8k
    return 0;
4121
42.8k
}
4122
4123
/*
4124
 * SSL_CTX_set_get0_protos gets the ALPN protocol list on |ctx| to |protos|.
4125
 */
4126
void SSL_CTX_get0_alpn_protos(SSL_CTX *ctx, const unsigned char **protos,
4127
    unsigned int *protos_len)
4128
0
{
4129
0
    unsigned char *p = NULL;
4130
0
    unsigned int len = 0;
4131
4132
0
    if (ctx != NULL) {
4133
0
        p = ctx->ext.alpn;
4134
0
        len = (unsigned int)ctx->ext.alpn_len;
4135
0
    }
4136
4137
0
    if (protos != NULL)
4138
0
        *protos = p;
4139
0
    if (protos_len != NULL)
4140
0
        *protos_len = len;
4141
0
}
4142
4143
/*
4144
 * SSL_get0_alpn_protos gets the ALPN protocol list on |ssl| to |protos|.
4145
 */
4146
void SSL_get0_alpn_protos(SSL *ssl, const unsigned char **protos,
4147
    unsigned int *protos_len)
4148
0
{
4149
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
4150
0
    unsigned char *p = NULL;
4151
0
    unsigned int len = 0;
4152
4153
0
    if (sc != NULL) {
4154
0
        p = sc->ext.alpn;
4155
0
        len = (unsigned int)sc->ext.alpn_len;
4156
0
    }
4157
4158
0
    if (protos != NULL)
4159
0
        *protos = p;
4160
0
    if (protos_len != NULL)
4161
0
        *protos_len = len;
4162
0
}
4163
4164
/*
4165
 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
4166
 * called during ClientHello processing in order to select an ALPN protocol
4167
 * from the client's list of offered protocols.
4168
 */
4169
void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
4170
    SSL_CTX_alpn_select_cb_func cb,
4171
    void *arg)
4172
410
{
4173
410
    ctx->ext.alpn_select_cb = cb;
4174
410
    ctx->ext.alpn_select_cb_arg = arg;
4175
410
}
4176
4177
/*
4178
 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
4179
 * On return it sets |*data| to point to |*len| bytes of protocol name
4180
 * (not including the leading length-prefix byte). If the server didn't
4181
 * respond with a negotiated protocol then |*len| will be zero.
4182
 */
4183
void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
4184
    unsigned int *len)
4185
10.2k
{
4186
10.2k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
4187
4188
10.2k
    if (sc == NULL) {
4189
        /* We have no other way to indicate error */
4190
0
        *data = NULL;
4191
0
        *len = 0;
4192
0
        return;
4193
0
    }
4194
4195
10.2k
    *data = sc->s3.alpn_selected;
4196
10.2k
    if (*data == NULL)
4197
26
        *len = 0;
4198
10.2k
    else
4199
10.2k
        *len = (unsigned int)sc->s3.alpn_selected_len;
4200
10.2k
}
4201
4202
int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
4203
    const char *label, size_t llen,
4204
    const unsigned char *context, size_t contextlen,
4205
    int use_context)
4206
0
{
4207
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4208
4209
0
    if (sc == NULL || sc->session == NULL)
4210
0
        return -1;
4211
4212
0
    return sc->ssl.method->ssl3_enc->export_keying_material(sc, out, olen, label,
4213
0
        llen, context,
4214
0
        contextlen,
4215
0
        use_context);
4216
0
}
4217
4218
int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
4219
    const char *label, size_t llen,
4220
    const unsigned char *context,
4221
    size_t contextlen)
4222
0
{
4223
0
    int version1_3;
4224
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4225
4226
0
    if (sc == NULL)
4227
0
        return -1;
4228
4229
0
    version1_3 = SSL_CONNECTION_IS_DTLS(sc) ? DTLS1_3_VERSION : TLS1_3_VERSION;
4230
4231
0
    if (sc->version != version1_3)
4232
0
        return 0;
4233
4234
0
    return tls13_export_keying_material_early(sc, out, olen, label, llen,
4235
0
        context, contextlen);
4236
0
}
4237
4238
static unsigned long ssl_session_hash(const SSL_SESSION *a)
4239
91.0k
{
4240
91.0k
    const unsigned char *session_id = a->session_id;
4241
91.0k
    unsigned long l;
4242
91.0k
    unsigned char tmp_storage[4];
4243
4244
91.0k
    if (a->session_id_length < sizeof(tmp_storage)) {
4245
1.24k
        memset(tmp_storage, 0, sizeof(tmp_storage));
4246
1.24k
        memcpy(tmp_storage, a->session_id, a->session_id_length);
4247
1.24k
        session_id = tmp_storage;
4248
1.24k
    }
4249
4250
91.0k
    l = (unsigned long)((unsigned long)session_id[0]) | ((unsigned long)session_id[1] << 8L) | ((unsigned long)session_id[2] << 16L) | ((unsigned long)session_id[3] << 24L);
4251
91.0k
    return l;
4252
91.0k
}
4253
4254
/*
4255
 * NB: If this function (or indeed the hash function which uses a sort of
4256
 * coarser function than this one) is changed, ensure
4257
 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
4258
 * being able to construct an SSL_SESSION that will collide with any existing
4259
 * session with a matching session ID.
4260
 */
4261
static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
4262
3.74k
{
4263
3.74k
    if (a->ssl_version != b->ssl_version)
4264
0
        return 1;
4265
3.74k
    if (a->session_id_length != b->session_id_length)
4266
0
        return 1;
4267
3.74k
    return memcmp(a->session_id, b->session_id, a->session_id_length);
4268
3.74k
}
4269
4270
#ifndef OPENSSL_NO_SSLKEYLOG
4271
/**
4272
 * @brief Static initialization for a one-time action to initialize the SSL key log.
4273
 */
4274
static CRYPTO_ONCE ssl_keylog_once = CRYPTO_ONCE_STATIC_INIT;
4275
4276
/**
4277
 * @brief Pointer to a read-write lock used to protect access to the key log.
4278
 */
4279
static CRYPTO_RWLOCK *keylog_lock = NULL;
4280
4281
/**
4282
 * @brief Pointer to a BIO structure used for writing the key log information.
4283
 */
4284
static BIO *keylog_bio = NULL;
4285
4286
/**
4287
 * @brief Initializes the SSLKEYLOGFILE lock.
4288
 *
4289
 * @return 1 on success, 0 on failure.
4290
 */
4291
DEFINE_RUN_ONCE_STATIC(ssl_keylog_init)
4292
{
4293
    keylog_lock = CRYPTO_THREAD_lock_new();
4294
    if (keylog_lock == NULL)
4295
        return 0;
4296
    return 1;
4297
}
4298
4299
/**
4300
 * @brief checks when a BIO refcount has reached zero, and sets
4301
 * keylog_cb to NULL if it has
4302
 *
4303
 * @returns 1 always
4304
 */
4305
static long check_keylog_bio_free(BIO *b, int oper, const char *argp,
4306
    size_t len, int argi, long argl, int ret,
4307
    size_t *processed)
4308
{
4309
4310
    /*
4311
     * Note we _dont_ take the keylog_lock here
4312
     * This is intentional, because we only free the keylog lock
4313
     * During SSL_CTX_free, in which we already possess the lock, so
4314
     * There's no need to grab it again here
4315
     */
4316
    if (oper == BIO_CB_FREE)
4317
        keylog_bio = NULL;
4318
    return ret;
4319
}
4320
4321
/**
4322
 * @brief records ssl secrets to a file
4323
 */
4324
static void do_sslkeylogfile(const SSL *ssl, const char *line)
4325
{
4326
    if (keylog_lock == NULL)
4327
        return;
4328
4329
    if (!CRYPTO_THREAD_write_lock(keylog_lock))
4330
        return;
4331
    if (keylog_bio != NULL) {
4332
        BIO_printf(keylog_bio, "%s\n", line);
4333
        (void)BIO_flush(keylog_bio);
4334
    }
4335
    CRYPTO_THREAD_unlock(keylog_lock);
4336
}
4337
#endif
4338
4339
/*
4340
 * These wrapper functions should remain rather than redeclaring
4341
 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
4342
 * variable. The reason is that the functions aren't static, they're exposed
4343
 * via ssl.h.
4344
 */
4345
4346
#ifndef OPENSSL_NO_SSLKEYLOG
4347
static BIO *get_sslkeylog_bio(const char *keylogfile)
4348
{
4349
#ifdef _POSIX_C_SOURCE
4350
    BIO *b;
4351
    int fdno = -1;
4352
    FILE *fp = NULL;
4353
4354
    fdno = open(keylogfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
4355
    if (fdno < 0)
4356
        return NULL;
4357
4358
    fp = fdopen(fdno, "a");
4359
    if (fp == NULL) {
4360
        close(fdno);
4361
        return NULL;
4362
    }
4363
4364
    if ((b = BIO_new_fp(fp, BIO_CLOSE)) == NULL)
4365
        fclose(fp);
4366
    return b;
4367
#else
4368
    return BIO_new_file(keylogfile, "a");
4369
#endif
4370
}
4371
#endif
4372
4373
SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
4374
    const SSL_METHOD *meth)
4375
30.1k
{
4376
30.1k
    SSL_CTX *ret = NULL;
4377
#ifndef OPENSSL_NO_SSLKEYLOG
4378
    const char *keylogfile = ossl_safe_getenv("SSLKEYLOGFILE");
4379
#endif
4380
#ifndef OPENSSL_NO_COMP_ALG
4381
    int i;
4382
#endif
4383
4384
30.1k
    if (meth == NULL) {
4385
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
4386
0
        return NULL;
4387
0
    }
4388
4389
30.1k
    if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
4390
0
        return NULL;
4391
4392
    /* Doing this for the run once effect */
4393
30.1k
    if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
4394
0
        ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
4395
0
        goto err;
4396
0
    }
4397
4398
30.1k
    ret = OPENSSL_zalloc(sizeof(*ret));
4399
30.1k
    if (ret == NULL)
4400
0
        return NULL;
4401
4402
    /* Init the reference counting before any call to SSL_CTX_free */
4403
30.1k
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
4404
0
        OPENSSL_free(ret);
4405
0
        return NULL;
4406
0
    }
4407
4408
30.1k
    ret->lock = CRYPTO_THREAD_lock_new();
4409
30.1k
    if (ret->lock == NULL) {
4410
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4411
0
        goto err;
4412
0
    }
4413
4414
#ifdef TSAN_REQUIRES_LOCKING
4415
    ret->tsan_lock = CRYPTO_THREAD_lock_new();
4416
    if (ret->tsan_lock == NULL) {
4417
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4418
        goto err;
4419
    }
4420
#endif
4421
4422
30.1k
    ret->libctx = libctx;
4423
30.1k
    if (propq != NULL) {
4424
0
        ret->propq = OPENSSL_strdup(propq);
4425
0
        if (ret->propq == NULL)
4426
0
            goto err;
4427
0
    }
4428
4429
30.1k
    if ((ret->hmac = EVP_MAC_fetch(libctx, "HMAC", propq)) == NULL)
4430
0
        goto err;
4431
30.1k
    if ((ret->sha256 = EVP_MD_fetch(libctx, "SHA2-256", propq)) == NULL)
4432
0
        goto err;
4433
30.1k
    if ((ret->tktenc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq)) == NULL)
4434
0
        goto err;
4435
30.1k
    if ((ret->sha1 = EVP_MD_fetch(libctx, "SHA1", propq)) == NULL)
4436
0
        goto err;
4437
4438
30.1k
#if defined(OPENSSL_HAVE_TLS1PRF)
4439
30.1k
    if ((ret->tls1prf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_TLS1_PRF, propq)) == NULL)
4440
0
        goto err;
4441
30.1k
#endif
4442
4443
30.1k
    ret->method = meth;
4444
30.1k
    ret->min_proto_version = 0;
4445
30.1k
    ret->max_proto_version = 0;
4446
30.1k
    ret->mode = SSL_MODE_AUTO_RETRY;
4447
30.1k
    ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
4448
30.1k
    ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
4449
    /* We take the system default. */
4450
30.1k
    ret->session_timeout = meth->get_timeout();
4451
30.1k
    ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
4452
30.1k
    ret->verify_mode = SSL_VERIFY_NONE;
4453
4454
30.1k
    ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
4455
30.1k
    if (ret->sessions == NULL) {
4456
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4457
0
        goto err;
4458
0
    }
4459
30.1k
    ret->cert_store = X509_STORE_new();
4460
30.1k
    if (ret->cert_store == NULL) {
4461
0
        ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
4462
0
        goto err;
4463
0
    }
4464
4465
30.1k
    if (!SSL_CTX_is_server(ret)) {
4466
14.9k
        ret->handshake_certs = sk_X509_HS_CACHE_ENT_new(NULL);
4467
14.9k
        if (ret->handshake_certs == NULL) {
4468
0
            ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4469
0
            goto err;
4470
0
        }
4471
14.9k
    }
4472
30.1k
#ifndef OPENSSL_NO_CT
4473
30.1k
    ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
4474
30.1k
    if (ret->ctlog_store == NULL) {
4475
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CT_LIB);
4476
0
        goto err;
4477
0
    }
4478
30.1k
#endif
4479
4480
    /* initialize cipher/digest methods table */
4481
30.1k
    if (!ssl_load_ciphers(ret)) {
4482
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4483
0
        goto err;
4484
0
    }
4485
4486
30.1k
    if (!ssl_load_groups(ret)) {
4487
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4488
0
        goto err;
4489
0
    }
4490
4491
    /* load provider sigalgs */
4492
30.1k
    if (!ssl_load_sigalgs(ret)) {
4493
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4494
0
        goto err;
4495
0
    }
4496
4497
    /* initialise sig algs */
4498
30.1k
    if (!ssl_setup_sigalgs(ret)) {
4499
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4500
0
        goto err;
4501
0
    }
4502
4503
30.1k
    if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) {
4504
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4505
0
        goto err;
4506
0
    }
4507
4508
30.1k
    if ((ret->cert = ssl_cert_new(SSL_PKEY_NUM + ret->sigalg_list_len)) == NULL) {
4509
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4510
0
        goto err;
4511
0
    }
4512
4513
30.1k
    if (!ssl_create_cipher_list(ret,
4514
30.1k
            ret->tls13_ciphersuites,
4515
30.1k
            &ret->cipher_list, &ret->cipher_list_by_id,
4516
30.1k
            OSSL_default_cipher_list(), ret->cert)
4517
30.1k
        || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
4518
0
        ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
4519
0
        goto err;
4520
0
    }
4521
4522
30.1k
    ret->param = X509_VERIFY_PARAM_new();
4523
30.1k
    if (ret->param == NULL) {
4524
0
        ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
4525
0
        goto err;
4526
0
    }
4527
4528
30.1k
    if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL) {
4529
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4530
0
        goto err;
4531
0
    }
4532
4533
30.1k
    if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL) {
4534
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4535
0
        goto err;
4536
0
    }
4537
4538
30.1k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data)) {
4539
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4540
0
        goto err;
4541
0
    }
4542
4543
30.1k
    if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
4544
0
        goto err;
4545
4546
    /* No compression for DTLS */
4547
30.1k
    if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
4548
19.5k
        ret->comp_methods = SSL_COMP_get_compression_methods();
4549
4550
30.1k
    ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
4551
30.1k
    ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
4552
4553
    /* Setup RFC5077 ticket keys */
4554
30.1k
    if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
4555
30.1k
             sizeof(ret->ext.tick_key_name), 0)
4556
30.1k
            <= 0)
4557
30.1k
        || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
4558
30.1k
                sizeof(ret->ext.secure->tick_hmac_key), 0)
4559
30.1k
            <= 0)
4560
30.1k
        || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
4561
30.1k
                sizeof(ret->ext.secure->tick_aes_key), 0)
4562
30.1k
            <= 0))
4563
0
        ret->options |= SSL_OP_NO_TICKET;
4564
4565
30.1k
    if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
4566
30.1k
            sizeof(ret->ext.cookie_hmac_key), 0)
4567
30.1k
        <= 0) {
4568
0
        ERR_raise(ERR_LIB_SSL, ERR_R_RAND_LIB);
4569
0
        goto err;
4570
0
    }
4571
4572
30.1k
#ifndef OPENSSL_NO_SRP
4573
30.1k
    if (!ssl_ctx_srp_ctx_init_intern(ret)) {
4574
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4575
0
        goto err;
4576
0
    }
4577
30.1k
#endif
4578
4579
#ifndef OPENSSL_NO_COMP_ALG
4580
    /*
4581
     * Set the default order: brotli, zlib, zstd
4582
     * Including only those enabled algorithms
4583
     */
4584
    memset(ret->cert_comp_prefs, 0, sizeof(ret->cert_comp_prefs));
4585
    i = 0;
4586
    if (ossl_comp_has_alg(TLSEXT_comp_cert_brotli))
4587
        ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_brotli;
4588
    if (ossl_comp_has_alg(TLSEXT_comp_cert_zlib))
4589
        ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zlib;
4590
    if (ossl_comp_has_alg(TLSEXT_comp_cert_zstd))
4591
        ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zstd;
4592
#endif
4593
    /*
4594
     * Disable compression by default to prevent CRIME. Applications can
4595
     * re-enable compression by configuring
4596
     * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
4597
     * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
4598
     * middlebox compatibility by default. This may be disabled by default in
4599
     * a later OpenSSL version.
4600
     */
4601
30.1k
    ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
4602
4603
30.1k
    ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
4604
4605
    /*
4606
     * We cannot usefully set a default max_early_data here (which gets
4607
     * propagated in SSL_new(), for the following reason: setting the
4608
     * SSL field causes tls_construct_stoc_early_data() to tell the
4609
     * client that early data will be accepted when constructing a TLS 1.3
4610
     * session ticket, and the client will accordingly send us early data
4611
     * when using that ticket (if the client has early data to send).
4612
     * However, in order for the early data to actually be consumed by
4613
     * the application, the application must also have calls to
4614
     * SSL_read_early_data(); otherwise we'll just skip past the early data
4615
     * and ignore it.  So, since the application must add calls to
4616
     * SSL_read_early_data(), we also require them to add
4617
     * calls to SSL_CTX_set_max_early_data() in order to use early data,
4618
     * eliminating the bandwidth-wasting early data in the case described
4619
     * above.
4620
     */
4621
30.1k
    ret->max_early_data = 0;
4622
4623
    /*
4624
     * Default recv_max_early_data is a fully loaded single record. Could be
4625
     * split across multiple records in practice. We set this differently to
4626
     * max_early_data so that, in the default case, we do not advertise any
4627
     * support for early_data, but if a client were to send us some (e.g.
4628
     * because of an old, stale ticket) then we will tolerate it and skip over
4629
     * it.
4630
     */
4631
30.1k
    ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
4632
4633
    /* By default we send two session tickets automatically in TLSv1.3 */
4634
30.1k
    ret->num_tickets = 2;
4635
4636
30.1k
#ifndef OPENSSL_NO_QUIC
4637
    /* only create a cache for client CTX-es */
4638
30.1k
    if (meth == OSSL_QUIC_client_method())
4639
8.71k
        if ((ret->tokencache = ossl_quic_new_token_store()) == NULL)
4640
0
            goto err;
4641
30.1k
    ret->domain_flags = 0;
4642
30.1k
    if (IS_QUIC_METHOD(meth)) {
4643
8.78k
#if defined(OPENSSL_THREADS)
4644
8.78k
        if (meth == OSSL_QUIC_client_thread_method())
4645
0
            ret->domain_flags
4646
0
                = SSL_DOMAIN_FLAG_MULTI_THREAD
4647
0
                | SSL_DOMAIN_FLAG_THREAD_ASSISTED
4648
0
                | SSL_DOMAIN_FLAG_BLOCKING;
4649
8.78k
        else
4650
8.78k
            ret->domain_flags
4651
8.78k
                = SSL_DOMAIN_FLAG_MULTI_THREAD
4652
8.78k
                | SSL_DOMAIN_FLAG_LEGACY_BLOCKING;
4653
#else
4654
        ret->domain_flags
4655
            = SSL_DOMAIN_FLAG_SINGLE_THREAD
4656
            | SSL_DOMAIN_FLAG_LEGACY_BLOCKING;
4657
#endif
4658
8.78k
    }
4659
30.1k
#endif
4660
4661
30.1k
    if (!ssl_ctx_system_config(ret)) {
4662
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_SYSTEM_DEFAULT_CONFIG);
4663
0
        goto err;
4664
0
    }
4665
4666
#ifndef OPENSSL_NO_SSLKEYLOG
4667
    if (keylogfile != NULL && strlen(keylogfile) != 0) {
4668
        /* Make sure we have a global lock allocated */
4669
        if (!RUN_ONCE(&ssl_keylog_once, ssl_keylog_init)) {
4670
            /* use a trace message as a warning */
4671
            OSSL_TRACE(TLS, "Unable to initialize keylog data\n");
4672
            goto out;
4673
        }
4674
4675
        /* Grab our global lock */
4676
        if (!CRYPTO_THREAD_write_lock(keylog_lock)) {
4677
            OSSL_TRACE(TLS, "Unable to acquire keylog write lock\n");
4678
            goto out;
4679
        } else {
4680
            /*
4681
             * If the bio for the requested keylog file hasn't been
4682
             * created yet, go ahead and create it, and set it to append
4683
             * if its already there.
4684
             */
4685
            if (keylog_bio == NULL) {
4686
                keylog_bio = get_sslkeylog_bio(keylogfile);
4687
                if (keylog_bio == NULL) {
4688
                    OSSL_TRACE(TLS, "Unable to create keylog bio\n");
4689
                    goto out;
4690
                }
4691
                BIO_set_callback_ex(keylog_bio, check_keylog_bio_free);
4692
            } else {
4693
                /* up our refcount for the already-created case */
4694
                BIO_up_ref(keylog_bio);
4695
            }
4696
            /* If we have a bio now, assign the callback handler */
4697
            if (keylog_bio != NULL)
4698
                ret->do_sslkeylog = 1;
4699
            /* unlock, and we're done */
4700
            CRYPTO_THREAD_unlock(keylog_lock);
4701
        }
4702
    }
4703
out:
4704
#endif
4705
30.1k
    return ret;
4706
0
err:
4707
0
    SSL_CTX_free(ret);
4708
#ifndef OPENSSL_NO_SSLKEYLOG
4709
    BIO_free(keylog_bio);
4710
#endif
4711
0
    return NULL;
4712
30.1k
}
4713
4714
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
4715
172k
{
4716
172k
    return SSL_CTX_new_ex(NULL, NULL, meth);
4717
172k
}
4718
4719
int SSL_CTX_up_ref(SSL_CTX *ctx)
4720
136k
{
4721
136k
    int i;
4722
4723
136k
    if (!CRYPTO_UP_REF(&ctx->references, &i))
4724
0
        return 0;
4725
4726
136k
    REF_PRINT_COUNT("SSL_CTX", i, ctx);
4727
136k
    REF_ASSERT_ISNT(i < 2);
4728
136k
    return ((i > 1) ? 1 : 0);
4729
136k
}
4730
4731
static void free_hs_cache_ent(X509_HS_CACHE_ENT *entry)
4732
1.74k
{
4733
1.74k
    X509_free(entry->cert);
4734
1.74k
    OPENSSL_free(entry);
4735
1.74k
    return;
4736
1.74k
}
4737
4738
void SSL_CTX_free(SSL_CTX *a)
4739
583k
{
4740
583k
    int i;
4741
583k
    size_t j;
4742
4743
583k
    if (a == NULL)
4744
0
        return;
4745
4746
583k
    CRYPTO_DOWN_REF(&a->references, &i);
4747
583k
    REF_PRINT_COUNT("SSL_CTX", i, a);
4748
583k
    if (i > 0)
4749
410k
        return;
4750
172k
    REF_ASSERT_ISNT(i < 0);
4751
4752
#ifndef OPENSSL_NO_SSLKEYLOG
4753
    if (keylog_lock != NULL && CRYPTO_THREAD_write_lock(keylog_lock)) {
4754
        if (a->do_sslkeylog == 1)
4755
            BIO_free(keylog_bio);
4756
        a->do_sslkeylog = 0;
4757
        CRYPTO_THREAD_unlock(keylog_lock);
4758
    }
4759
#endif
4760
4761
172k
    X509_VERIFY_PARAM_free(a->param);
4762
172k
    dane_ctx_final(&a->dane);
4763
4764
    /*
4765
     * Free internal session cache. However: the remove_cb() may reference
4766
     * the ex_data of SSL_CTX, thus the ex_data store can only be removed
4767
     * after the sessions were flushed.
4768
     * As the ex_data handling routines might also touch the session cache,
4769
     * the most secure solution seems to be: empty (flush) the cache, then
4770
     * free ex_data, then finally free the cache.
4771
     * (See ticket [openssl.org #212].)
4772
     */
4773
172k
    if (a->sessions != NULL)
4774
172k
        SSL_CTX_flush_sessions_ex(a, 0);
4775
4776
172k
    EVP_MAC_free(a->hmac);
4777
172k
    EVP_MD_free(a->sha256);
4778
172k
    EVP_CIPHER_free(a->tktenc);
4779
172k
    EVP_MD_free(a->sha1);
4780
172k
#ifdef OPENSSL_HAVE_TLS1PRF
4781
172k
    EVP_KDF_free(a->tls1prf);
4782
172k
#endif
4783
4784
172k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
4785
172k
    lh_SSL_SESSION_free(a->sessions);
4786
172k
    X509_STORE_free(a->cert_store);
4787
172k
#ifndef OPENSSL_NO_CT
4788
172k
    CTLOG_STORE_free(a->ctlog_store);
4789
172k
#endif
4790
172k
    sk_SSL_CIPHER_free(a->cipher_list);
4791
172k
    sk_SSL_CIPHER_free(a->cipher_list_by_id);
4792
172k
    sk_SSL_CIPHER_free(a->tls13_ciphersuites);
4793
172k
    ssl_cert_free(a->cert);
4794
172k
    sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
4795
172k
    sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
4796
172k
    OSSL_STACK_OF_X509_free(a->extra_certs);
4797
172k
    a->comp_methods = NULL;
4798
172k
#ifndef OPENSSL_NO_SRTP
4799
172k
    sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
4800
172k
#endif
4801
172k
#ifndef OPENSSL_NO_SRP
4802
172k
    ssl_ctx_srp_ctx_free_intern(a);
4803
172k
#endif
4804
4805
172k
    OPENSSL_free(a->ext.supportedgroups);
4806
172k
    OPENSSL_free(a->ext.keyshares);
4807
172k
    OPENSSL_free(a->ext.tuples);
4808
172k
    OPENSSL_free(a->ext.alpn);
4809
172k
    OPENSSL_secure_clear_free(a->ext.secure, sizeof(*a->ext.secure));
4810
4811
4.42M
    for (j = 0; j < OSSL_NELEM(a->ssl_cipher_methods); j++)
4812
4.25M
        ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
4813
2.67M
    for (j = 0; j < SSL_MD_NUM_IDX; j++)
4814
2.50M
        ssl_evp_md_free(a->ssl_digest_methods[j]);
4815
7.59M
    for (j = 0; j < a->group_list_len; j++) {
4816
7.42M
        OPENSSL_free(a->group_list[j].tlsname);
4817
7.42M
        OPENSSL_free(a->group_list[j].realname);
4818
7.42M
        OPENSSL_free(a->group_list[j].algorithm);
4819
7.42M
    }
4820
172k
    OPENSSL_free(a->group_list);
4821
1.37M
    for (j = 0; j < a->sigalg_list_len; j++) {
4822
1.20M
        OPENSSL_free(a->sigalg_list[j].name);
4823
1.20M
        OPENSSL_free(a->sigalg_list[j].sigalg_name);
4824
1.20M
        OPENSSL_free(a->sigalg_list[j].sigalg_oid);
4825
1.20M
        OPENSSL_free(a->sigalg_list[j].sig_name);
4826
1.20M
        OPENSSL_free(a->sigalg_list[j].sig_oid);
4827
1.20M
        OPENSSL_free(a->sigalg_list[j].hash_name);
4828
1.20M
        OPENSSL_free(a->sigalg_list[j].hash_oid);
4829
1.20M
        OPENSSL_free(a->sigalg_list[j].keytype);
4830
1.20M
        OPENSSL_free(a->sigalg_list[j].keytype_oid);
4831
1.20M
    }
4832
172k
    OPENSSL_free(a->sigalg_list);
4833
172k
    OPENSSL_free(a->ssl_cert_info);
4834
4835
172k
    OPENSSL_free(a->sigalg_lookup_cache);
4836
172k
    OPENSSL_free(a->tls12_sigalgs);
4837
4838
172k
    OPENSSL_free(a->client_cert_type);
4839
172k
    OPENSSL_free(a->server_cert_type);
4840
4841
172k
    sk_X509_HS_CACHE_ENT_pop_free(a->handshake_certs, free_hs_cache_ent);
4842
172k
    CRYPTO_THREAD_lock_free(a->lock);
4843
172k
    CRYPTO_FREE_REF(&a->references);
4844
#ifdef TSAN_REQUIRES_LOCKING
4845
    CRYPTO_THREAD_lock_free(a->tsan_lock);
4846
#endif
4847
4848
172k
    OPENSSL_free(a->propq);
4849
172k
#ifndef OPENSSL_NO_QLOG
4850
172k
    OPENSSL_free(a->qlog_title);
4851
172k
#endif
4852
4853
172k
#ifndef OPENSSL_NO_QUIC
4854
172k
    ossl_quic_free_token_store(a->tokencache);
4855
172k
#endif
4856
4857
172k
#ifndef OPENSSL_NO_ECH
4858
172k
    ossl_ech_ctx_clear(&a->ext.ech);
4859
172k
#endif
4860
4861
172k
    OPENSSL_free(a);
4862
172k
}
4863
4864
X509 *ssl_ctx_find_handshake_cert(SSL_CTX *sctx, const unsigned char *certbytes,
4865
    size_t cert_len, unsigned char *sha1_hash)
4866
12.2k
{
4867
12.2k
    int idx;
4868
12.2k
    X509_HS_CACHE_ENT *find = NULL;
4869
12.2k
    X509 *ret = NULL;
4870
12.2k
    unsigned int len = SHA_DIGEST_LENGTH;
4871
12.2k
    int cache_len;
4872
4873
12.2k
    if (sctx->handshake_certs == NULL)
4874
4.75k
        return NULL;
4875
4876
7.53k
    if (!EVP_Digest(certbytes, cert_len, sha1_hash, &len, sctx->sha1, NULL))
4877
0
        return NULL;
4878
4879
7.53k
    if (len != SHA_DIGEST_LENGTH)
4880
0
        return NULL;
4881
4882
7.53k
    if (!CRYPTO_THREAD_read_lock(sctx->lock))
4883
0
        return NULL;
4884
4885
7.53k
    cache_len = sk_X509_HS_CACHE_ENT_num(sctx->handshake_certs);
4886
7.54k
    for (idx = 0; idx < cache_len; idx++) {
4887
8
        find = sk_X509_HS_CACHE_ENT_value(sctx->handshake_certs, idx);
4888
8
        if (!memcmp(find->sha1_hash, sha1_hash, SHA_DIGEST_LENGTH)) {
4889
0
            X509_up_ref(find->cert);
4890
0
            ret = find->cert;
4891
0
            break;
4892
0
        }
4893
8
    }
4894
7.53k
    CRYPTO_THREAD_unlock(sctx->lock);
4895
7.53k
    return ret;
4896
7.53k
}
4897
4898
X509 *ssl_ctx_add_handshake_cert(SSL_CTX *sctx, X509 *cert,
4899
    unsigned char *sha1_hash)
4900
8.46k
{
4901
8.46k
    X509_HS_CACHE_ENT *tmp, *del;
4902
8.46k
    X509 *ret = cert;
4903
4904
8.46k
    if (sctx->handshake_certs == NULL)
4905
3.11k
        return cert;
4906
4907
    /*
4908
     * Force extension caching on the X509 now, while we still "own" it
4909
     * exclusively. From this point on the cached cert is treated as
4910
     * immutable by all readers (they only X509_up_ref it).
4911
     */
4912
5.34k
    if (X509_check_purpose(cert, -1, 0) <= 0)
4913
3.60k
        return cert;
4914
4915
1.74k
    tmp = OPENSSL_malloc(sizeof(X509_HS_CACHE_ENT));
4916
1.74k
    if (tmp == NULL)
4917
0
        return cert;
4918
4919
    /*
4920
     * We had to compute the hash of the der string in the find routine, so
4921
     * we can reuse it here
4922
     */
4923
1.74k
    memcpy(tmp->sha1_hash, sha1_hash, SHA_DIGEST_LENGTH);
4924
1.74k
    tmp->cert = cert;
4925
1.74k
    if (!X509_up_ref(cert)) {
4926
        /*
4927
         * We didn't take a ref, so we can't cache this cert, but we still
4928
         * own the initial ref, so we can just return it for a single use
4929
         * Note: We use OPENSSL_free here instead of free_hs_cache_ent, as the
4930
         * latter drops the ref on the cert, which we don't want
4931
         */
4932
0
        OPENSSL_free(tmp);
4933
0
        return cert;
4934
0
    }
4935
4936
1.74k
    if (!CRYPTO_THREAD_write_lock(sctx->lock)) {
4937
0
        free_hs_cache_ent(tmp);
4938
0
        return cert;
4939
0
    }
4940
4941
1.74k
    if (sk_X509_HS_CACHE_ENT_num(sctx->handshake_certs) > 64) {
4942
        /*
4943
         * Evict the oldest entry (index 0). Deleting index 64 with
4944
         * num > 64 would remove the most-recently-pushed entry, which
4945
         * is the opposite of what we want.
4946
         */
4947
0
        del = sk_X509_HS_CACHE_ENT_delete(sctx->handshake_certs, 0);
4948
0
        if (del != NULL)
4949
0
            free_hs_cache_ent(del);
4950
0
    }
4951
1.74k
    if (!sk_X509_HS_CACHE_ENT_push(sctx->handshake_certs, tmp)) {
4952
0
        free_hs_cache_ent(tmp);
4953
0
        ret = cert;
4954
0
    }
4955
4956
1.74k
    CRYPTO_THREAD_unlock(sctx->lock);
4957
1.74k
    return ret;
4958
1.74k
}
4959
4960
void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
4961
0
{
4962
0
    ctx->default_passwd_callback = cb;
4963
0
}
4964
4965
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
4966
0
{
4967
0
    ctx->default_passwd_callback_userdata = u;
4968
0
}
4969
4970
pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
4971
0
{
4972
0
    return ctx->default_passwd_callback;
4973
0
}
4974
4975
void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
4976
0
{
4977
0
    return ctx->default_passwd_callback_userdata;
4978
0
}
4979
4980
void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
4981
0
{
4982
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4983
4984
0
    if (sc == NULL)
4985
0
        return;
4986
4987
0
    sc->default_passwd_callback = cb;
4988
0
}
4989
4990
void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
4991
0
{
4992
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4993
4994
0
    if (sc == NULL)
4995
0
        return;
4996
4997
0
    sc->default_passwd_callback_userdata = u;
4998
0
}
4999
5000
pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
5001
0
{
5002
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5003
5004
0
    if (sc == NULL)
5005
0
        return NULL;
5006
5007
0
    return sc->default_passwd_callback;
5008
0
}
5009
5010
void *SSL_get_default_passwd_cb_userdata(SSL *s)
5011
0
{
5012
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5013
5014
0
    if (sc == NULL)
5015
0
        return NULL;
5016
5017
0
    return sc->default_passwd_callback_userdata;
5018
0
}
5019
5020
void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
5021
    int (*cb)(X509_STORE_CTX *, void *),
5022
    void *arg)
5023
0
{
5024
0
    ctx->app_verify_callback = cb;
5025
0
    ctx->app_verify_arg = arg;
5026
0
}
5027
5028
void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
5029
    int (*cb)(int, X509_STORE_CTX *))
5030
0
{
5031
0
    ctx->verify_mode = mode;
5032
0
    ctx->default_verify_callback = cb;
5033
0
}
5034
5035
void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
5036
0
{
5037
0
    X509_VERIFY_PARAM_set_depth(ctx->param, depth);
5038
0
}
5039
5040
void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
5041
0
{
5042
0
    ssl_cert_set_cert_cb(c->cert, cb, arg);
5043
0
}
5044
5045
void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
5046
0
{
5047
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5048
5049
0
    if (sc == NULL)
5050
0
        return;
5051
5052
0
    ssl_cert_set_cert_cb(sc->cert, cb, arg);
5053
0
}
5054
5055
void ssl_set_masks(SSL_CONNECTION *s)
5056
10.4k
{
5057
10.4k
    CERT *c = s->cert;
5058
10.4k
    uint32_t *pvalid = s->s3.tmp.valid_flags;
5059
10.4k
    int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
5060
10.4k
    unsigned long mask_k, mask_a;
5061
10.4k
    int have_ecc_cert, ecdsa_ok;
5062
5063
10.4k
    if (c == NULL)
5064
0
        return;
5065
5066
10.4k
    dh_tmp = (c->dh_tmp != NULL
5067
10.4k
        || c->dh_tmp_cb != NULL
5068
10.4k
        || c->dh_tmp_auto
5069
10.4k
        || tls1_shared_group(s, TLS1_GROUPS_RETURN_TMP_ID,
5070
10.4k
               TLS1_GROUPS_FFDHE_GROUPS)
5071
10.4k
            != 0);
5072
5073
10.4k
    rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
5074
10.4k
    rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
5075
10.4k
    dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
5076
10.4k
    have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
5077
10.4k
    mask_k = 0;
5078
10.4k
    mask_a = 0;
5079
5080
10.4k
    OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
5081
10.4k
        dh_tmp, rsa_enc, rsa_sign, dsa_sign);
5082
5083
10.4k
#ifndef OPENSSL_NO_GOST
5084
10.4k
    if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
5085
0
        mask_k |= SSL_kGOST | SSL_kGOST18;
5086
0
        mask_a |= SSL_aGOST12;
5087
0
    }
5088
10.4k
    if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
5089
0
        mask_k |= SSL_kGOST | SSL_kGOST18;
5090
0
        mask_a |= SSL_aGOST12;
5091
0
    }
5092
10.4k
    if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
5093
0
        mask_k |= SSL_kGOST;
5094
0
        mask_a |= SSL_aGOST01;
5095
0
    }
5096
10.4k
#endif
5097
5098
10.4k
    if (rsa_enc)
5099
10.4k
        mask_k |= SSL_kRSA;
5100
5101
10.4k
    if (dh_tmp)
5102
2.28k
        mask_k |= SSL_kDHE;
5103
5104
    /*
5105
     * If we only have an RSA-PSS certificate allow RSA authentication
5106
     * if TLS 1.2 or DTLS 1.2 and peer supports it.
5107
     */
5108
5109
10.4k
    if (rsa_enc || rsa_sign
5110
0
        || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
5111
0
            && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
5112
0
            && (SSL_version(&s->ssl) == XTLS(&s->ssl, 1, 2))))
5113
10.4k
        mask_a |= SSL_aRSA;
5114
5115
10.4k
    if (dsa_sign) {
5116
10.4k
        mask_a |= SSL_aDSS;
5117
10.4k
    }
5118
5119
10.4k
    mask_a |= SSL_aNULL;
5120
5121
    /*
5122
     * You can do anything with an RPK key, since there's no cert to restrict it
5123
     * But we need to check for private keys
5124
     */
5125
10.4k
    if (pvalid[SSL_PKEY_RSA] & CERT_PKEY_RPK) {
5126
0
        mask_a |= SSL_aRSA;
5127
0
        mask_k |= SSL_kRSA;
5128
0
    }
5129
10.4k
    if (pvalid[SSL_PKEY_ECC] & CERT_PKEY_RPK)
5130
0
        mask_a |= SSL_aECDSA;
5131
10.4k
    if (SSL_version(&s->ssl) == XTLS(&s->ssl, 1, 2)) {
5132
7.59k
        if (pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_RPK)
5133
0
            mask_a |= SSL_aRSA;
5134
7.59k
        if (pvalid[SSL_PKEY_ED25519] & CERT_PKEY_RPK
5135
7.59k
            || pvalid[SSL_PKEY_ED448] & CERT_PKEY_RPK)
5136
0
            mask_a |= SSL_aECDSA;
5137
7.59k
    }
5138
5139
    /*
5140
     * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
5141
     * depending on the key usage extension.
5142
     */
5143
10.4k
    if (have_ecc_cert) {
5144
6.75k
        uint32_t ex_kusage;
5145
6.75k
        ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
5146
6.75k
        ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
5147
6.75k
        if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
5148
188
            ecdsa_ok = 0;
5149
6.75k
        if (ecdsa_ok)
5150
6.56k
            mask_a |= SSL_aECDSA;
5151
6.75k
    }
5152
    /* Allow Ed25519 for TLS 1.2 and DTLS 1.2 if peer supports it */
5153
10.4k
    if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
5154
0
        && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
5155
0
        && (SSL_version(&s->ssl) == XTLS(&s->ssl, 1, 2)))
5156
0
        mask_a |= SSL_aECDSA;
5157
5158
    /* Allow Ed448 for TLS 1.2 and DTLS 1.2 if peer supports it */
5159
10.4k
    if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
5160
0
        && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
5161
0
        && (SSL_version(&s->ssl) == XTLS(&s->ssl, 1, 2)))
5162
0
        mask_a |= SSL_aECDSA;
5163
5164
10.4k
    mask_k |= SSL_kECDHE;
5165
5166
10.4k
#ifndef OPENSSL_NO_PSK
5167
10.4k
    mask_k |= SSL_kPSK;
5168
10.4k
    mask_a |= SSL_aPSK;
5169
10.4k
    if (mask_k & SSL_kRSA)
5170
10.4k
        mask_k |= SSL_kRSAPSK;
5171
10.4k
    if (mask_k & SSL_kDHE)
5172
2.28k
        mask_k |= SSL_kDHEPSK;
5173
10.4k
    if (mask_k & SSL_kECDHE)
5174
10.4k
        mask_k |= SSL_kECDHEPSK;
5175
10.4k
#endif
5176
5177
10.4k
    s->s3.tmp.mask_k = mask_k;
5178
10.4k
    s->s3.tmp.mask_a = mask_a;
5179
10.4k
}
5180
5181
int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s)
5182
0
{
5183
0
    if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
5184
        /* key usage, if present, must allow signing */
5185
0
        if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
5186
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
5187
0
            return 0;
5188
0
        }
5189
0
    }
5190
0
    return 1; /* all checks are ok */
5191
0
}
5192
5193
int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s,
5194
    const unsigned char **serverinfo,
5195
    size_t *serverinfo_length)
5196
0
{
5197
0
    CERT_PKEY *cpk = s->s3.tmp.cert;
5198
0
    *serverinfo_length = 0;
5199
5200
0
    if (cpk == NULL || cpk->serverinfo == NULL)
5201
0
        return 0;
5202
5203
0
    *serverinfo = cpk->serverinfo;
5204
0
    *serverinfo_length = cpk->serverinfo_length;
5205
0
    return 1;
5206
0
}
5207
5208
void ssl_update_cache(SSL_CONNECTION *s, int mode)
5209
1.74k
{
5210
1.74k
    int i;
5211
5212
    /*
5213
     * If the session_id_length is 0, we are not supposed to cache it, and it
5214
     * would be rather hard to do anyway :-). Also if the session has already
5215
     * been marked as not_resumable we should not cache it for later reuse.
5216
     */
5217
1.74k
    if (s->session->session_id_length == 0 || s->session->not_resumable)
5218
579
        return;
5219
5220
    /*
5221
     * If sid_ctx_length is 0 there is no specific application context
5222
     * associated with this session, so when we try to resume it and
5223
     * SSL_VERIFY_PEER is requested to verify the client identity, we have no
5224
     * indication that this is actually a session for the proper application
5225
     * context, and the *handshake* will fail, not just the resumption attempt.
5226
     * Do not cache (on the server) these sessions that are not resumable
5227
     * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
5228
     */
5229
1.16k
    if (s->server && s->session->sid_ctx_length == 0
5230
427
        && (s->verify_mode & SSL_VERIFY_PEER) != 0)
5231
0
        return;
5232
5233
1.16k
    i = s->session_ctx->session_cache_mode;
5234
1.16k
    if ((i & mode) != 0
5235
427
        && (!s->hit || SSL_CONNECTION_IS_VERSION13(s))) {
5236
        /*
5237
         * Add the session to the internal cache. In server side TLSv1.3 we
5238
         * normally don't do this because by default it's a full stateless ticket
5239
         * with only a dummy session id so there is no reason to cache it,
5240
         * unless:
5241
         * - we are doing early_data, in which case we cache so that we can
5242
         *   detect replays
5243
         * - the application has set a remove_session_cb so needs to know about
5244
         *   session timeout events
5245
         * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
5246
         */
5247
427
        if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
5248
427
            && (!SSL_CONNECTION_IS_VERSION13(s)
5249
0
                || !s->server
5250
0
                || (s->max_early_data > 0
5251
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
5252
0
                || s->session_ctx->remove_session_cb != NULL
5253
0
                || (s->options & SSL_OP_NO_TICKET) != 0))
5254
427
            SSL_CTX_add_session(s->session_ctx, s->session);
5255
5256
        /*
5257
         * Add the session to the external cache. We do this even in server side
5258
         * TLSv1.3 without early data because some applications just want to
5259
         * know about the creation of a session and aren't doing a full cache.
5260
         */
5261
427
        if (s->session_ctx->new_session_cb != NULL && SSL_SESSION_up_ref(s->session)) {
5262
0
            if (!s->session_ctx->new_session_cb(SSL_CONNECTION_GET_USER_SSL(s),
5263
0
                    s->session))
5264
0
                SSL_SESSION_free(s->session);
5265
0
        }
5266
427
    }
5267
5268
    /* auto flush every 255 connections */
5269
1.16k
    if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
5270
427
        TSAN_QUALIFIER int *stat;
5271
5272
427
        if (mode & SSL_SESS_CACHE_CLIENT)
5273
0
            stat = &s->session_ctx->stats.sess_connect_good;
5274
427
        else
5275
427
            stat = &s->session_ctx->stats.sess_accept_good;
5276
427
        if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
5277
0
            SSL_CTX_flush_sessions_ex(s->session_ctx, time(NULL));
5278
427
    }
5279
1.16k
}
5280
5281
const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
5282
0
{
5283
0
    return ctx->method;
5284
0
}
5285
5286
const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
5287
0
{
5288
0
    return s->method;
5289
0
}
5290
5291
int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
5292
0
{
5293
0
    int ret = 1;
5294
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5295
5296
    /* Not allowed for QUIC */
5297
0
    if (sc == NULL
5298
0
        || (s->type != SSL_TYPE_SSL_CONNECTION && s->method != meth)
5299
0
        || (s->type == SSL_TYPE_SSL_CONNECTION && IS_QUIC_METHOD(meth)))
5300
0
        return 0;
5301
5302
0
    if (s->method != meth) {
5303
0
        const SSL_METHOD *sm = s->method;
5304
0
        int (*hf)(SSL *) = sc->handshake_func;
5305
5306
0
        if (sm->version == meth->version)
5307
0
            s->method = meth;
5308
0
        else {
5309
0
            sm->ssl_deinit(s);
5310
0
            s->method = meth;
5311
0
            ret = s->method->ssl_init(s);
5312
0
        }
5313
5314
0
        if (hf == sm->ssl_connect)
5315
0
            sc->handshake_func = meth->ssl_connect;
5316
0
        else if (hf == sm->ssl_accept)
5317
0
            sc->handshake_func = meth->ssl_accept;
5318
0
    }
5319
0
    return ret;
5320
0
}
5321
5322
int SSL_get_error(const SSL *s, int i)
5323
71.0M
{
5324
71.0M
    return ossl_ssl_get_error(s, i, /*check_err=*/1);
5325
71.0M
}
5326
5327
int ossl_ssl_get_error(const SSL *s, int i, int check_err)
5328
38.8M
{
5329
38.8M
    int reason;
5330
38.8M
    BIO *bio;
5331
38.8M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5332
5333
38.8M
    if (i > 0)
5334
0
        return SSL_ERROR_NONE;
5335
5336
38.8M
#ifndef OPENSSL_NO_QUIC
5337
38.8M
    if (IS_QUIC(s)) {
5338
19.4M
        reason = ossl_quic_get_error(s, i);
5339
19.4M
        if (reason != SSL_ERROR_NONE)
5340
19.4M
            return reason;
5341
19.4M
    }
5342
19.4M
#endif
5343
5344
19.4M
    if (sc == NULL)
5345
0
        return SSL_ERROR_SSL;
5346
5347
19.4M
    if (check_err != 0) {
5348
2.11k
        if (sc->statem.error_state == ERROR_STATE_SSL)
5349
638
            return SSL_ERROR_SSL;
5350
1.47k
        if (sc->statem.error_state == ERROR_STATE_SYSCALL)
5351
0
            return SSL_ERROR_SYSCALL;
5352
1.47k
    }
5353
5354
19.4M
#ifndef OPENSSL_NO_QUIC
5355
19.4M
    if (!IS_QUIC(s))
5356
19.4M
#endif
5357
19.4M
    {
5358
19.4M
        if (SSL_want_read(s)) {
5359
19.4M
            bio = SSL_get_rbio(s);
5360
            /*
5361
             * rbio can be NULL for DTLS listener-created connections that
5362
             * read from DTLS_RX queue instead of a BIO.
5363
             */
5364
19.4M
            if (bio == NULL) {
5365
0
#ifndef OPENSSL_NO_DTLS
5366
0
                if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
5367
0
                    && sc->d1 != NULL && sc->d1->listener != NULL)
5368
0
                    return SSL_ERROR_WANT_READ;
5369
0
#endif
5370
                /* Unexpected NULL BIO */
5371
0
                return SSL_ERROR_SYSCALL;
5372
0
            }
5373
19.4M
            if (BIO_should_read(bio))
5374
19.4M
                return SSL_ERROR_WANT_READ;
5375
0
            else if (BIO_should_write(bio))
5376
                /*
5377
                 * This one doesn't make too much sense ... We never try to
5378
                 * write to the rbio, and an application program where rbio and
5379
                 * wbio are separate couldn't even know what it should wait for.
5380
                 * However if we ever set s->rwstate incorrectly (so that we
5381
                 * have SSL_want_read(s) instead of SSL_want_write(s)) and rbio
5382
                 * and wbio *are* the same, this test works around that bug; so
5383
                 * it might be safer to keep it.
5384
                 */
5385
0
                return SSL_ERROR_WANT_WRITE;
5386
0
            else if (BIO_should_io_special(bio)) {
5387
0
                reason = BIO_get_retry_reason(bio);
5388
0
                if (reason == BIO_RR_CONNECT)
5389
0
                    return SSL_ERROR_WANT_CONNECT;
5390
0
                else if (reason == BIO_RR_ACCEPT)
5391
0
                    return SSL_ERROR_WANT_ACCEPT;
5392
0
                else
5393
0
                    return SSL_ERROR_SYSCALL; /* unknown */
5394
0
            }
5395
19.4M
        }
5396
5397
2.92k
        if (SSL_want_write(s)) {
5398
            /*
5399
             * Access wbio directly - in order to use the buffered bio if
5400
             * present
5401
             */
5402
0
            bio = sc->wbio;
5403
            /*
5404
             * wbio can be NULL for DTLS listener-created connections that
5405
             * use the listener's shared BIO via BIO_sendmmsg().
5406
             */
5407
0
            if (bio == NULL) {
5408
0
#ifndef OPENSSL_NO_DTLS
5409
0
                if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
5410
0
                    && sc->d1 != NULL && sc->d1->listener != NULL)
5411
0
                    return SSL_ERROR_WANT_WRITE;
5412
0
#endif
5413
                /* Unexpected NULL BIO */
5414
0
                return SSL_ERROR_SYSCALL;
5415
0
            }
5416
0
            if (BIO_should_write(bio))
5417
0
                return SSL_ERROR_WANT_WRITE;
5418
0
            else if (BIO_should_read(bio))
5419
                /*
5420
                 * See above (SSL_want_read(s) with BIO_should_write(bio))
5421
                 */
5422
0
                return SSL_ERROR_WANT_READ;
5423
0
            else if (BIO_should_io_special(bio)) {
5424
0
                reason = BIO_get_retry_reason(bio);
5425
0
                if (reason == BIO_RR_CONNECT)
5426
0
                    return SSL_ERROR_WANT_CONNECT;
5427
0
                else if (reason == BIO_RR_ACCEPT)
5428
0
                    return SSL_ERROR_WANT_ACCEPT;
5429
0
                else
5430
0
                    return SSL_ERROR_SYSCALL;
5431
0
            }
5432
0
        }
5433
2.92k
    }
5434
5435
2.92k
    if (SSL_want_x509_lookup(s))
5436
0
        return SSL_ERROR_WANT_X509_LOOKUP;
5437
2.92k
    if (SSL_want_retry_verify(s))
5438
0
        return SSL_ERROR_WANT_RETRY_VERIFY;
5439
2.92k
    if (SSL_want_async(s))
5440
0
        return SSL_ERROR_WANT_ASYNC;
5441
2.92k
    if (SSL_want_async_job(s))
5442
0
        return SSL_ERROR_WANT_ASYNC_JOB;
5443
2.92k
    if (SSL_want_client_hello_cb(s))
5444
0
        return SSL_ERROR_WANT_CLIENT_HELLO_CB;
5445
5446
2.92k
    if ((sc->shutdown & SSL_RECEIVED_SHUTDOWN) && (sc->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
5447
0
        return SSL_ERROR_ZERO_RETURN;
5448
5449
2.92k
    return SSL_ERROR_SYSCALL;
5450
2.92k
}
5451
5452
static int ssl_do_handshake_intern(void *vargs)
5453
0
{
5454
0
    struct ssl_async_args *args = (struct ssl_async_args *)vargs;
5455
0
    SSL *s = args->s;
5456
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5457
5458
0
    if (sc == NULL)
5459
0
        return -1;
5460
5461
0
    return sc->handshake_func(s);
5462
0
}
5463
5464
int SSL_do_handshake(SSL *s)
5465
30.1M
{
5466
30.1M
    int ret = 1;
5467
30.1M
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5468
5469
30.1M
#ifndef OPENSSL_NO_QUIC
5470
30.1M
    if (IS_QUIC(s))
5471
15.0M
        return ossl_quic_do_handshake(s);
5472
15.1M
#endif
5473
5474
15.1M
    if (sc == NULL)
5475
0
        return -1;
5476
5477
15.1M
    if (ssl_reset_error_state(sc) == 0)
5478
3.12k
        return -1;
5479
5480
15.1M
    sc->statem.error_state = ERROR_STATE_NOERROR;
5481
5482
15.1M
    if (sc->handshake_func == NULL) {
5483
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
5484
0
        sc->statem.error_state = ERROR_STATE_SSL;
5485
0
        return -1;
5486
0
    }
5487
5488
15.1M
    if (!ossl_statem_check_finish_init(sc, -1)) {
5489
0
        ssl_update_error_state(sc);
5490
0
        return -1;
5491
0
    }
5492
5493
15.1M
    s->method->ssl_renegotiate_check(s, 0);
5494
5495
15.1M
    if (SSL_in_init(s) || SSL_in_before(s)) {
5496
15.1M
        if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
5497
0
            struct ssl_async_args args;
5498
5499
0
            memset(&args, 0, sizeof(args));
5500
0
            args.s = s;
5501
5502
0
            ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
5503
15.1M
        } else {
5504
15.1M
            ret = sc->handshake_func(s);
5505
15.1M
        }
5506
15.1M
    }
5507
5508
15.1M
    ssl_update_error_state(sc);
5509
15.1M
    return ret;
5510
15.1M
}
5511
5512
void SSL_set_accept_state(SSL *s)
5513
8.98k
{
5514
8.98k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5515
5516
8.98k
#ifndef OPENSSL_NO_QUIC
5517
8.98k
    if (IS_QUIC(s)) {
5518
        /* We suppress errors because this is a void function */
5519
78
        (void)ossl_quic_set_accept_state(s, 0 /* suppress errors */);
5520
78
        return;
5521
78
    }
5522
8.91k
#endif
5523
5524
8.91k
    if (sc == NULL)
5525
0
        return;
5526
8.91k
    sc->server = 1;
5527
8.91k
    sc->shutdown = 0;
5528
8.91k
    ossl_statem_clear(sc);
5529
8.91k
    sc->handshake_func = s->method->ssl_accept;
5530
    /* Ignore return value. Its a void public API function */
5531
8.91k
    RECORD_LAYER_reset(&sc->rlayer);
5532
8.91k
}
5533
5534
void SSL_set_connect_state(SSL *s)
5535
29.8k
{
5536
29.8k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5537
5538
29.8k
#ifndef OPENSSL_NO_QUIC
5539
29.8k
    if (IS_QUIC(s)) {
5540
        /* We suppress errors because this is a void function */
5541
8.71k
        (void)ossl_quic_set_connect_state(s, 0 /* suppress errors */);
5542
8.71k
        return;
5543
8.71k
    }
5544
21.1k
#endif
5545
5546
21.1k
    if (sc == NULL)
5547
0
        return;
5548
21.1k
    sc->server = 0;
5549
21.1k
    sc->shutdown = 0;
5550
21.1k
    ossl_statem_clear(sc);
5551
21.1k
    sc->handshake_func = s->method->ssl_connect;
5552
    /* Ignore return value. Its a void public API function */
5553
21.1k
    RECORD_LAYER_reset(&sc->rlayer);
5554
21.1k
}
5555
5556
int ssl_undefined_function(SSL *s)
5557
0
{
5558
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5559
5560
0
    if (sc != NULL)
5561
0
        sc->statem.error_state = ERROR_STATE_SSL;
5562
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
5563
0
    return 0;
5564
0
}
5565
5566
int ssl_undefined_void_function(void)
5567
0
{
5568
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
5569
0
    return 0;
5570
0
}
5571
5572
const char *ssl_protocol_to_string(int version)
5573
519
{
5574
519
    switch (version) {
5575
22
    case TLS1_3_VERSION:
5576
22
        return "TLSv1.3";
5577
5578
119
    case TLS1_2_VERSION:
5579
119
        return "TLSv1.2";
5580
5581
24
    case TLS1_1_VERSION:
5582
24
        return "TLSv1.1";
5583
5584
4
    case TLS1_VERSION:
5585
4
        return "TLSv1";
5586
5587
11
    case SSL3_VERSION:
5588
11
        return "SSLv3";
5589
5590
8
    case DTLS1_BAD_VER:
5591
8
        return "DTLSv0.9";
5592
5593
10
    case DTLS1_VERSION:
5594
10
        return "DTLSv1";
5595
5596
2
    case DTLS1_2_VERSION:
5597
2
        return "DTLSv1.2";
5598
5599
2
    case DTLS1_3_VERSION:
5600
2
        return "DTLSv1.3";
5601
5602
317
    default:
5603
317
        return "unknown";
5604
519
    }
5605
519
}
5606
5607
const char *SSL_get_version(const SSL *s)
5608
0
{
5609
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5610
5611
0
#ifndef OPENSSL_NO_QUIC
5612
    /* We only support QUICv1 - so if its QUIC its QUICv1 */
5613
0
    if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
5614
0
        return "QUICv1";
5615
0
#endif
5616
5617
0
    if (sc == NULL)
5618
0
        return NULL;
5619
5620
0
    return ssl_protocol_to_string(sc->version);
5621
0
}
5622
5623
__owur int SSL_get_handshake_rtt(const SSL *s, uint64_t *rtt)
5624
0
{
5625
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5626
5627
0
    if (sc == NULL)
5628
0
        return -1;
5629
0
    if (sc->ts_msg_write.t <= 0 || sc->ts_msg_read.t <= 0)
5630
0
        return 0; /* data not (yet) available */
5631
0
    if (sc->ts_msg_read.t < sc->ts_msg_write.t)
5632
0
        return -1;
5633
5634
0
    *rtt = ossl_time2us(ossl_time_subtract(sc->ts_msg_read, sc->ts_msg_write));
5635
0
    return 1;
5636
0
}
5637
5638
static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
5639
0
{
5640
0
    STACK_OF(X509_NAME) *sk;
5641
0
    X509_NAME *xn;
5642
0
    int i;
5643
5644
0
    if (src == NULL) {
5645
0
        *dst = NULL;
5646
0
        return 1;
5647
0
    }
5648
5649
0
    if ((sk = sk_X509_NAME_new_null()) == NULL)
5650
0
        return 0;
5651
0
    for (i = 0; i < sk_X509_NAME_num(src); i++) {
5652
0
        xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
5653
0
        if (xn == NULL) {
5654
0
            sk_X509_NAME_pop_free(sk, X509_NAME_free);
5655
0
            return 0;
5656
0
        }
5657
0
        if (sk_X509_NAME_insert(sk, xn, i) == 0) {
5658
0
            X509_NAME_free(xn);
5659
0
            sk_X509_NAME_pop_free(sk, X509_NAME_free);
5660
0
            return 0;
5661
0
        }
5662
0
    }
5663
0
    *dst = sk;
5664
5665
0
    return 1;
5666
0
}
5667
5668
SSL *SSL_dup(SSL *s)
5669
0
{
5670
0
    SSL *ret;
5671
0
    int i;
5672
    /* TODO(QUIC FUTURE): Add an SSL_METHOD function for duplication */
5673
0
    SSL_CONNECTION *retsc;
5674
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5675
5676
0
    if (sc == NULL)
5677
0
        return NULL;
5678
5679
    /* If we're not quiescent, just up_ref! */
5680
0
    if (!SSL_in_init(s) || !SSL_in_before(s)) {
5681
0
        if (!CRYPTO_UP_REF(&s->references, &i))
5682
0
            return NULL;
5683
0
        return s;
5684
0
    }
5685
5686
    /*
5687
     * Otherwise, copy configuration state, and session if set.
5688
     */
5689
0
    if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
5690
0
        return NULL;
5691
0
    if ((retsc = SSL_CONNECTION_FROM_SSL_ONLY(ret)) == NULL)
5692
0
        goto err;
5693
5694
0
    if (sc->session != NULL) {
5695
        /*
5696
         * Arranges to share the same session via up_ref.  This "copies"
5697
         * session-id, SSL_METHOD, sid_ctx, and 'cert'
5698
         */
5699
0
        if (!SSL_copy_session_id(ret, s))
5700
0
            goto err;
5701
0
    } else {
5702
        /*
5703
         * No session has been established yet, so we have to expect that
5704
         * s->cert or ret->cert will be changed later -- they should not both
5705
         * point to the same object, and thus we can't use
5706
         * SSL_copy_session_id.
5707
         */
5708
0
        if (!SSL_set_ssl_method(ret, s->method))
5709
0
            goto err;
5710
5711
0
        if (sc->cert != NULL) {
5712
0
            ssl_cert_free(retsc->cert);
5713
0
            retsc->cert = ssl_cert_dup(sc->cert);
5714
0
            if (retsc->cert == NULL)
5715
0
                goto err;
5716
0
        }
5717
5718
0
        if (!SSL_set_session_id_context(ret, sc->sid_ctx,
5719
0
                (int)sc->sid_ctx_length))
5720
0
            goto err;
5721
0
    }
5722
5723
0
    if (!ssl_dane_dup(retsc, sc))
5724
0
        goto err;
5725
0
    retsc->version = sc->version;
5726
0
    retsc->options = sc->options;
5727
0
    retsc->min_proto_version = sc->min_proto_version;
5728
0
    retsc->max_proto_version = sc->max_proto_version;
5729
0
    retsc->mode = sc->mode;
5730
0
    SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
5731
0
    SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
5732
0
    retsc->msg_callback = sc->msg_callback;
5733
0
    retsc->msg_callback_arg = sc->msg_callback_arg;
5734
0
    SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
5735
0
    SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
5736
0
    retsc->generate_session_id = sc->generate_session_id;
5737
5738
0
    SSL_set_info_callback(ret, SSL_get_info_callback(s));
5739
5740
    /* copy app data, a little dangerous perhaps */
5741
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
5742
0
        goto err;
5743
5744
0
    retsc->server = sc->server;
5745
0
    if (sc->handshake_func) {
5746
0
        if (sc->server)
5747
0
            SSL_set_accept_state(ret);
5748
0
        else
5749
0
            SSL_set_connect_state(ret);
5750
0
    }
5751
0
    retsc->shutdown = sc->shutdown;
5752
0
    retsc->hit = sc->hit;
5753
5754
0
    retsc->default_passwd_callback = sc->default_passwd_callback;
5755
0
    retsc->default_passwd_callback_userdata = sc->default_passwd_callback_userdata;
5756
5757
0
    X509_VERIFY_PARAM_inherit(retsc->param, sc->param);
5758
5759
    /* dup the cipher_list and cipher_list_by_id stacks */
5760
0
    if (sc->cipher_list != NULL) {
5761
0
        if ((retsc->cipher_list = sk_SSL_CIPHER_dup(sc->cipher_list)) == NULL)
5762
0
            goto err;
5763
0
    }
5764
0
    if (sc->cipher_list_by_id != NULL)
5765
0
        if ((retsc->cipher_list_by_id = sk_SSL_CIPHER_dup(sc->cipher_list_by_id))
5766
0
            == NULL)
5767
0
            goto err;
5768
5769
    /* Dup the client_CA list */
5770
0
    if (!dup_ca_names(&retsc->ca_names, sc->ca_names)
5771
0
        || !dup_ca_names(&retsc->client_ca_names, sc->client_ca_names))
5772
0
        goto err;
5773
5774
0
    if (sc->server_cert_type != NULL) {
5775
0
        OPENSSL_free(retsc->server_cert_type);
5776
0
        retsc->server_cert_type = OPENSSL_memdup(sc->server_cert_type,
5777
0
            sc->server_cert_type_len);
5778
0
        if (retsc->server_cert_type == NULL)
5779
0
            goto err;
5780
0
        retsc->server_cert_type_len = sc->server_cert_type_len;
5781
0
    }
5782
5783
0
    if (sc->client_cert_type != NULL) {
5784
0
        OPENSSL_free(retsc->client_cert_type);
5785
0
        retsc->client_cert_type = OPENSSL_memdup(sc->client_cert_type,
5786
0
            sc->client_cert_type_len);
5787
0
        if (retsc->client_cert_type == NULL)
5788
0
            goto err;
5789
0
        retsc->client_cert_type_len = sc->client_cert_type_len;
5790
0
    }
5791
5792
0
#ifndef OPENSSL_NO_CT
5793
0
    retsc->ct_validation_callback = sc->ct_validation_callback;
5794
0
    retsc->ct_validation_callback_arg = sc->ct_validation_callback_arg;
5795
0
#endif
5796
5797
0
    retsc->ext.status_type = sc->ext.status_type;
5798
5799
0
    return ret;
5800
5801
0
err:
5802
0
    SSL_free(ret);
5803
0
    return NULL;
5804
0
}
5805
5806
X509 *SSL_get_certificate(const SSL *s)
5807
0
{
5808
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5809
5810
0
    if (sc == NULL)
5811
0
        return NULL;
5812
5813
0
    if (sc->cert != NULL)
5814
0
        return sc->cert->key->x509;
5815
0
    else
5816
0
        return NULL;
5817
0
}
5818
5819
EVP_PKEY *SSL_get_privatekey(const SSL *s)
5820
0
{
5821
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5822
5823
0
    if (sc == NULL)
5824
0
        return NULL;
5825
5826
0
    if (sc->cert != NULL)
5827
0
        return sc->cert->key->privatekey;
5828
0
    else
5829
0
        return NULL;
5830
0
}
5831
5832
X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
5833
0
{
5834
0
    if (ctx->cert != NULL)
5835
0
        return ctx->cert->key->x509;
5836
0
    else
5837
0
        return NULL;
5838
0
}
5839
5840
EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
5841
0
{
5842
0
    if (ctx->cert != NULL)
5843
0
        return ctx->cert->key->privatekey;
5844
0
    else
5845
0
        return NULL;
5846
0
}
5847
5848
const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
5849
0
{
5850
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5851
5852
0
    if (sc == NULL)
5853
0
        return NULL;
5854
5855
0
    if ((sc->session != NULL) && (sc->session->cipher != NULL))
5856
0
        return sc->session->cipher;
5857
0
    return NULL;
5858
0
}
5859
5860
const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
5861
0
{
5862
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5863
5864
0
    if (sc == NULL)
5865
0
        return NULL;
5866
5867
0
    return sc->s3.tmp.new_cipher;
5868
0
}
5869
5870
const COMP_METHOD *SSL_get_current_compression(const SSL *s)
5871
0
{
5872
0
#ifndef OPENSSL_NO_COMP
5873
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5874
5875
0
    if (sc == NULL)
5876
0
        return NULL;
5877
5878
0
    return sc->rlayer.wrlmethod->get_compression(sc->rlayer.wrl);
5879
#else
5880
    return NULL;
5881
#endif
5882
0
}
5883
5884
const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
5885
0
{
5886
0
#ifndef OPENSSL_NO_COMP
5887
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5888
5889
0
    if (sc == NULL)
5890
0
        return NULL;
5891
5892
0
    return sc->rlayer.rrlmethod->get_compression(sc->rlayer.rrl);
5893
#else
5894
    return NULL;
5895
#endif
5896
0
}
5897
5898
int ssl_init_wbio_buffer(SSL_CONNECTION *s)
5899
254k
{
5900
254k
    BIO *bbio;
5901
5902
254k
    if (s->bbio != NULL) {
5903
        /* Already buffered. */
5904
0
        return 1;
5905
0
    }
5906
5907
254k
    bbio = BIO_new(BIO_f_buffer());
5908
254k
    if (bbio == NULL || BIO_set_read_buffer_size(bbio, 1) <= 0) {
5909
0
        BIO_free(bbio);
5910
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
5911
0
        return 0;
5912
0
    }
5913
254k
    s->bbio = bbio;
5914
254k
    s->wbio = BIO_push(bbio, s->wbio);
5915
5916
254k
    s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5917
5918
254k
    return 1;
5919
254k
}
5920
5921
int ssl_free_wbio_buffer(SSL_CONNECTION *s)
5922
953k
{
5923
    /* callers ensure s is never null */
5924
953k
    if (s->bbio == NULL)
5925
699k
        return 1;
5926
5927
254k
    s->wbio = BIO_pop(s->wbio);
5928
254k
    s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5929
5930
254k
    BIO_free(s->bbio);
5931
254k
    s->bbio = NULL;
5932
5933
254k
    return 1;
5934
953k
}
5935
5936
void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
5937
0
{
5938
0
    ctx->quiet_shutdown = mode;
5939
0
}
5940
5941
int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
5942
0
{
5943
0
    return ctx->quiet_shutdown;
5944
0
}
5945
5946
void SSL_set_quiet_shutdown(SSL *s, int mode)
5947
0
{
5948
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5949
5950
    /* Not supported with QUIC */
5951
0
    if (sc == NULL)
5952
0
        return;
5953
5954
0
    sc->quiet_shutdown = mode;
5955
0
}
5956
5957
int SSL_get_quiet_shutdown(const SSL *s)
5958
0
{
5959
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5960
5961
    /* Not supported with QUIC */
5962
0
    if (sc == NULL)
5963
0
        return 0;
5964
5965
0
    return sc->quiet_shutdown;
5966
0
}
5967
5968
void SSL_set_shutdown(SSL *s, int mode)
5969
0
{
5970
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5971
5972
    /* Not supported with QUIC */
5973
0
    if (sc == NULL)
5974
0
        return;
5975
5976
0
    sc->shutdown = mode;
5977
0
}
5978
5979
int SSL_get_shutdown(const SSL *s)
5980
0
{
5981
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5982
5983
0
#ifndef OPENSSL_NO_QUIC
5984
    /* QUIC: Just indicate whether the connection was shutdown cleanly. */
5985
0
    if (IS_QUIC(s))
5986
0
        return ossl_quic_get_shutdown(s);
5987
0
#endif
5988
5989
0
    if (sc == NULL)
5990
0
        return 0;
5991
5992
0
    return sc->shutdown;
5993
0
}
5994
5995
int SSL_version(const SSL *s)
5996
617k
{
5997
617k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5998
5999
617k
#ifndef OPENSSL_NO_QUIC
6000
    /* We only support QUICv1 - so if its QUIC its QUICv1 */
6001
617k
    if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
6002
0
        return OSSL_QUIC1_VERSION;
6003
617k
#endif
6004
617k
    if (sc == NULL)
6005
0
        return 0;
6006
6007
617k
    return sc->version;
6008
617k
}
6009
6010
int SSL_client_version(const SSL *s)
6011
0
{
6012
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6013
6014
0
#ifndef OPENSSL_NO_QUIC
6015
    /* We only support QUICv1 - so if its QUIC its QUICv1 */
6016
0
    if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
6017
0
        return OSSL_QUIC1_VERSION;
6018
0
#endif
6019
0
    if (sc == NULL)
6020
0
        return 0;
6021
6022
0
    return sc->client_version;
6023
0
}
6024
6025
SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
6026
0
{
6027
0
    return ssl->ctx;
6028
0
}
6029
6030
SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
6031
0
{
6032
0
    CERT *new_cert;
6033
0
    uint32_t *new_valid_flags = NULL;
6034
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
6035
6036
    /* TODO(QUIC FUTURE): Add support for QUIC */
6037
0
    if (sc == NULL)
6038
0
        return NULL;
6039
6040
0
    if (ssl->ctx == ctx)
6041
0
        return ssl->ctx;
6042
0
    if (ctx == NULL)
6043
0
        ctx = sc->session_ctx;
6044
0
    new_cert = ssl_cert_dup(ctx->cert);
6045
0
    if (new_cert == NULL)
6046
0
        goto err;
6047
0
    if (!custom_exts_copy_conn(&new_cert->custext, &sc->cert->custext))
6048
0
        goto err;
6049
0
    if (!custom_exts_copy_flags(&new_cert->custext, &sc->cert->custext))
6050
0
        goto err;
6051
6052
    /*
6053
     * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
6054
     * so setter APIs must prevent invalid lengths from entering the system.
6055
     */
6056
0
    if (!ossl_assert(sc->sid_ctx_length <= sizeof(sc->sid_ctx)))
6057
0
        goto err;
6058
6059
    /*
6060
     * |valid_flags| is sized from the number of signature algorithm slots of
6061
     * the SSL_CTX the connection was created from, so it must be resized for
6062
     * the replacement context.
6063
     *
6064
     * The built-in slots are indexed by the fixed SSL_PKEY_* constants and so
6065
     * mean the same thing in either context. They are preserved because they
6066
     * may already hold peer signature algorithm state which does not depend
6067
     * on the SSL_CTX. A provider slot index is instead a position in one
6068
     * context's provider list, so the same index denotes a different
6069
     * algorithm here and the old value cannot be carried over. They are reset
6070
     * rather than recomputed: recomputing them means recomputing the shared
6071
     * signature algorithms against the replacement context, which would let
6072
     * its preferences take effect on an established connection.
6073
     */
6074
0
    if (sc->s3.tmp.valid_flags != NULL) {
6075
        /* Should never happen: ssl_cert_new() enforces this */
6076
0
        if (!ossl_assert(new_cert->ssl_pkey_num >= SSL_PKEY_NUM))
6077
0
            goto err;
6078
0
        new_valid_flags = OPENSSL_calloc(new_cert->ssl_pkey_num,
6079
0
            sizeof(*new_valid_flags));
6080
0
        if (new_valid_flags == NULL)
6081
0
            goto err;
6082
0
        memcpy(new_valid_flags, sc->s3.tmp.valid_flags,
6083
0
            SSL_PKEY_NUM * sizeof(*new_valid_flags));
6084
0
    }
6085
6086
0
    if (!SSL_CTX_up_ref(ctx))
6087
0
        goto err;
6088
6089
    /*
6090
     * If the session ID context matches that of the parent SSL_CTX,
6091
     * inherit it from the new SSL_CTX as well. If however the context does
6092
     * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
6093
     * leave it unchanged.
6094
     */
6095
0
    if ((ssl->ctx != NULL) && (sc->sid_ctx_length == ssl->ctx->sid_ctx_length) && (memcmp(sc->sid_ctx, ssl->ctx->sid_ctx, sc->sid_ctx_length) == 0)) {
6096
0
        sc->sid_ctx_length = ctx->sid_ctx_length;
6097
0
        memcpy(&sc->sid_ctx, &ctx->sid_ctx, sizeof(sc->sid_ctx));
6098
0
    }
6099
6100
0
    ssl_cert_free(sc->cert);
6101
0
    sc->cert = new_cert;
6102
0
    sc->ssl_pkey_num = new_cert->ssl_pkey_num;
6103
0
    if (new_valid_flags != NULL) {
6104
0
        OPENSSL_free(sc->s3.tmp.valid_flags);
6105
0
        sc->s3.tmp.valid_flags = new_valid_flags;
6106
0
    }
6107
0
    SSL_CTX_free(ssl->ctx); /* decrement reference count */
6108
0
    ssl->ctx = ctx;
6109
6110
0
    return ssl->ctx;
6111
6112
0
err:
6113
0
    OPENSSL_free(new_valid_flags);
6114
0
    ssl_cert_free(new_cert);
6115
0
    return NULL;
6116
0
}
6117
6118
int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
6119
0
{
6120
0
    return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
6121
0
        ctx->propq);
6122
0
}
6123
6124
int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
6125
0
{
6126
0
    X509_LOOKUP *lookup;
6127
6128
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
6129
0
    if (lookup == NULL)
6130
0
        return 0;
6131
6132
    /* We ignore errors, in case the directory doesn't exist */
6133
0
    ERR_set_mark();
6134
6135
0
    X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
6136
6137
0
    ERR_pop_to_mark();
6138
6139
0
    return 1;
6140
0
}
6141
6142
int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
6143
0
{
6144
0
    X509_LOOKUP *lookup;
6145
6146
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
6147
0
    if (lookup == NULL)
6148
0
        return 0;
6149
6150
    /* We ignore errors, in case the file doesn't exist */
6151
0
    ERR_set_mark();
6152
6153
0
    X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
6154
0
        ctx->propq);
6155
6156
0
    ERR_pop_to_mark();
6157
6158
0
    return 1;
6159
0
}
6160
6161
int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
6162
0
{
6163
0
    X509_LOOKUP *lookup;
6164
6165
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
6166
0
    if (lookup == NULL)
6167
0
        return 0;
6168
6169
    /* We ignore errors, in case the directory doesn't exist */
6170
0
    ERR_set_mark();
6171
6172
0
    X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
6173
6174
0
    ERR_pop_to_mark();
6175
6176
0
    return 1;
6177
0
}
6178
6179
int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
6180
0
{
6181
0
    return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
6182
0
        ctx->propq);
6183
0
}
6184
6185
int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
6186
0
{
6187
0
    return X509_STORE_load_path(ctx->cert_store, CApath);
6188
0
}
6189
6190
int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
6191
0
{
6192
0
    return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
6193
0
        ctx->propq);
6194
0
}
6195
6196
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
6197
    const char *CApath)
6198
0
{
6199
0
    if (CAfile == NULL && CApath == NULL)
6200
0
        return 0;
6201
0
    if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
6202
0
        return 0;
6203
0
    if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
6204
0
        return 0;
6205
0
    return 1;
6206
0
}
6207
6208
void SSL_set_info_callback(SSL *ssl,
6209
    void (*cb)(const SSL *ssl, int type, int val))
6210
0
{
6211
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6212
6213
0
    if (sc == NULL)
6214
0
        return;
6215
6216
0
    sc->info_callback = cb;
6217
0
}
6218
6219
/*
6220
 * One compiler (Diab DCC) doesn't like argument names in returned function
6221
 * pointer.
6222
 */
6223
void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /* ssl */,
6224
    int /* type */,
6225
    int /* val */)
6226
0
{
6227
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
6228
6229
0
    if (sc == NULL)
6230
0
        return NULL;
6231
6232
0
    return sc->info_callback;
6233
0
}
6234
6235
void SSL_set_verify_result(SSL *ssl, long arg)
6236
0
{
6237
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6238
6239
0
    if (sc == NULL)
6240
0
        return;
6241
6242
0
    sc->verify_result = arg;
6243
0
}
6244
6245
long SSL_get_verify_result(const SSL *ssl)
6246
0
{
6247
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
6248
6249
0
    if (sc == NULL)
6250
0
        return 0;
6251
6252
0
    return sc->verify_result;
6253
0
}
6254
6255
size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
6256
0
{
6257
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
6258
6259
0
    if (sc == NULL)
6260
0
        return 0;
6261
6262
0
    if (outlen == 0)
6263
0
        return sizeof(sc->s3.client_random);
6264
0
    if (outlen > sizeof(sc->s3.client_random))
6265
0
        outlen = sizeof(sc->s3.client_random);
6266
0
    memcpy(out, sc->s3.client_random, outlen);
6267
0
    return outlen;
6268
0
}
6269
6270
size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
6271
0
{
6272
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
6273
6274
0
    if (sc == NULL)
6275
0
        return 0;
6276
6277
0
    if (outlen == 0)
6278
0
        return sizeof(sc->s3.server_random);
6279
0
    if (outlen > sizeof(sc->s3.server_random))
6280
0
        outlen = sizeof(sc->s3.server_random);
6281
0
    memcpy(out, sc->s3.server_random, outlen);
6282
0
    return outlen;
6283
0
}
6284
6285
size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
6286
    unsigned char *out, size_t outlen)
6287
0
{
6288
0
    if (outlen == 0)
6289
0
        return session->master_key_length;
6290
0
    if (outlen > session->master_key_length)
6291
0
        outlen = session->master_key_length;
6292
0
    memcpy(out, session->master_key, outlen);
6293
0
    return outlen;
6294
0
}
6295
6296
int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
6297
    size_t len)
6298
0
{
6299
0
    if (len > sizeof(sess->master_key))
6300
0
        return 0;
6301
6302
0
    memcpy(sess->master_key, in, len);
6303
0
    sess->master_key_length = len;
6304
0
    return 1;
6305
0
}
6306
6307
int SSL_set_ex_data(SSL *s, int idx, void *arg)
6308
0
{
6309
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
6310
0
}
6311
6312
void *SSL_get_ex_data(const SSL *s, int idx)
6313
0
{
6314
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
6315
0
}
6316
6317
int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
6318
0
{
6319
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
6320
0
}
6321
6322
void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
6323
0
{
6324
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
6325
0
}
6326
6327
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
6328
0
{
6329
0
    return ctx->cert_store;
6330
0
}
6331
6332
void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
6333
0
{
6334
0
    X509_STORE_free(ctx->cert_store);
6335
0
    ctx->cert_store = store;
6336
0
}
6337
6338
void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
6339
0
{
6340
0
    if (store != NULL && !X509_STORE_up_ref(store))
6341
0
        return;
6342
6343
0
    SSL_CTX_set_cert_store(ctx, store);
6344
0
}
6345
6346
int SSL_want(const SSL *s)
6347
99.8M
{
6348
99.8M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6349
6350
99.8M
#ifndef OPENSSL_NO_QUIC
6351
99.8M
    if (IS_QUIC(s))
6352
0
        return ossl_quic_want(s);
6353
99.8M
#endif
6354
6355
99.8M
    if (sc == NULL)
6356
0
        return SSL_NOTHING;
6357
6358
99.8M
    return sc->rwstate;
6359
99.8M
}
6360
6361
#ifndef OPENSSL_NO_PSK
6362
int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
6363
0
{
6364
0
    if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
6365
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
6366
0
        return 0;
6367
0
    }
6368
0
    OPENSSL_free(ctx->cert->psk_identity_hint);
6369
0
    if (identity_hint != NULL) {
6370
0
        ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
6371
0
        if (ctx->cert->psk_identity_hint == NULL)
6372
0
            return 0;
6373
0
    } else
6374
0
        ctx->cert->psk_identity_hint = NULL;
6375
0
    return 1;
6376
0
}
6377
6378
int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
6379
0
{
6380
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6381
6382
0
    if (sc == NULL)
6383
0
        return 0;
6384
6385
0
    if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
6386
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
6387
0
        return 0;
6388
0
    }
6389
0
    OPENSSL_free(sc->cert->psk_identity_hint);
6390
0
    if (identity_hint != NULL) {
6391
0
        sc->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
6392
0
        if (sc->cert->psk_identity_hint == NULL)
6393
0
            return 0;
6394
0
    } else
6395
0
        sc->cert->psk_identity_hint = NULL;
6396
0
    return 1;
6397
0
}
6398
6399
const char *SSL_get_psk_identity_hint(const SSL *s)
6400
0
{
6401
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6402
6403
0
    if (sc == NULL || sc->session == NULL)
6404
0
        return NULL;
6405
6406
0
    return sc->session->psk_identity_hint;
6407
0
}
6408
6409
const char *SSL_get_psk_identity(const SSL *s)
6410
0
{
6411
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6412
6413
0
    if (sc == NULL || sc->session == NULL)
6414
0
        return NULL;
6415
6416
0
    return sc->session->psk_identity;
6417
0
}
6418
6419
void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
6420
0
{
6421
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6422
6423
0
    if (sc == NULL)
6424
0
        return;
6425
6426
0
    sc->psk_client_callback = cb;
6427
0
}
6428
6429
void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
6430
0
{
6431
0
    ctx->psk_client_callback = cb;
6432
0
}
6433
6434
void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
6435
0
{
6436
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6437
6438
0
    if (sc == NULL)
6439
0
        return;
6440
6441
0
    sc->psk_server_callback = cb;
6442
0
}
6443
6444
void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
6445
0
{
6446
0
    ctx->psk_server_callback = cb;
6447
0
}
6448
#endif
6449
6450
void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
6451
0
{
6452
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6453
6454
0
    if (sc == NULL)
6455
0
        return;
6456
6457
0
    sc->psk_find_session_cb = cb;
6458
0
}
6459
6460
void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
6461
    SSL_psk_find_session_cb_func cb)
6462
0
{
6463
0
    ctx->psk_find_session_cb = cb;
6464
0
}
6465
6466
void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
6467
0
{
6468
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6469
6470
0
    if (sc == NULL)
6471
0
        return;
6472
6473
0
    sc->psk_use_session_cb = cb;
6474
0
}
6475
6476
void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
6477
    SSL_psk_use_session_cb_func cb)
6478
0
{
6479
0
    ctx->psk_use_session_cb = cb;
6480
0
}
6481
6482
void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
6483
    void (*cb)(int write_p, int version,
6484
        int content_type, const void *buf,
6485
        size_t len, SSL *ssl, void *arg))
6486
0
{
6487
0
    SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
6488
0
}
6489
6490
void SSL_set_msg_callback(SSL *ssl,
6491
    void (*cb)(int write_p, int version,
6492
        int content_type, const void *buf,
6493
        size_t len, SSL *ssl, void *arg))
6494
0
{
6495
0
    SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
6496
0
}
6497
6498
void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
6499
    int (*cb)(SSL *ssl,
6500
        int
6501
            is_forward_secure))
6502
0
{
6503
0
    SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
6504
0
        (void (*)(void))cb);
6505
0
}
6506
6507
void SSL_set_not_resumable_session_callback(SSL *ssl,
6508
    int (*cb)(SSL *ssl,
6509
        int is_forward_secure))
6510
0
{
6511
0
    SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
6512
0
        (void (*)(void))cb);
6513
0
}
6514
6515
void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
6516
    size_t (*cb)(SSL *ssl, int type,
6517
        size_t len, void *arg))
6518
0
{
6519
0
    ctx->record_padding_cb = cb;
6520
0
}
6521
6522
void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
6523
0
{
6524
0
    ctx->record_padding_arg = arg;
6525
0
}
6526
6527
void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
6528
0
{
6529
0
    return ctx->record_padding_arg;
6530
0
}
6531
6532
int SSL_CTX_set_block_padding_ex(SSL_CTX *ctx, size_t app_block_size,
6533
    size_t hs_block_size)
6534
0
{
6535
0
    if (IS_QUIC_CTX(ctx) && (app_block_size > 1 || hs_block_size > 1))
6536
0
        return 0;
6537
6538
    /* block size of 0 or 1 is basically no padding */
6539
0
    if (app_block_size == 1) {
6540
0
        ctx->block_padding = 0;
6541
0
    } else if (app_block_size <= SSL3_RT_MAX_PLAIN_LENGTH) {
6542
0
        ctx->block_padding = app_block_size;
6543
0
    } else {
6544
0
        return 0;
6545
0
    }
6546
0
    if (hs_block_size == 1) {
6547
0
        ctx->hs_padding = 0;
6548
0
    } else if (hs_block_size <= SSL3_RT_MAX_PLAIN_LENGTH) {
6549
0
        ctx->hs_padding = hs_block_size;
6550
0
    } else {
6551
0
        return 0;
6552
0
    }
6553
0
    return 1;
6554
0
}
6555
6556
int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
6557
0
{
6558
0
    return SSL_CTX_set_block_padding_ex(ctx, block_size, block_size);
6559
0
}
6560
6561
int SSL_set_record_padding_callback(SSL *ssl,
6562
    size_t (*cb)(SSL *ssl, int type,
6563
        size_t len, void *arg))
6564
0
{
6565
0
    BIO *b;
6566
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
6567
6568
0
    if (sc == NULL)
6569
0
        return 0;
6570
6571
0
    b = SSL_get_wbio(ssl);
6572
0
    if (b == NULL || !BIO_get_ktls_send(b)) {
6573
0
        sc->rlayer.record_padding_cb = cb;
6574
0
        return 1;
6575
0
    }
6576
0
    return 0;
6577
0
}
6578
6579
void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
6580
0
{
6581
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6582
6583
0
    if (sc == NULL)
6584
0
        return;
6585
6586
0
    sc->rlayer.record_padding_arg = arg;
6587
0
}
6588
6589
void *SSL_get_record_padding_callback_arg(const SSL *ssl)
6590
0
{
6591
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
6592
6593
0
    if (sc == NULL)
6594
0
        return NULL;
6595
6596
0
    return sc->rlayer.record_padding_arg;
6597
0
}
6598
6599
int SSL_set_block_padding_ex(SSL *ssl, size_t app_block_size,
6600
    size_t hs_block_size)
6601
0
{
6602
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6603
6604
0
    if (sc == NULL
6605
0
        || (IS_QUIC(ssl)
6606
0
            && (app_block_size > 1 || hs_block_size > 1)))
6607
0
        return 0;
6608
6609
    /* block size of 0 or 1 is basically no padding */
6610
0
    if (app_block_size == 1) {
6611
0
        sc->rlayer.block_padding = 0;
6612
0
    } else if (app_block_size <= SSL3_RT_MAX_PLAIN_LENGTH) {
6613
0
        sc->rlayer.block_padding = app_block_size;
6614
0
    } else {
6615
0
        return 0;
6616
0
    }
6617
0
    if (hs_block_size == 1) {
6618
0
        sc->rlayer.hs_padding = 0;
6619
0
    } else if (hs_block_size <= SSL3_RT_MAX_PLAIN_LENGTH) {
6620
0
        sc->rlayer.hs_padding = hs_block_size;
6621
0
    } else {
6622
0
        return 0;
6623
0
    }
6624
0
    return 1;
6625
0
}
6626
6627
int SSL_set_block_padding(SSL *ssl, size_t block_size)
6628
0
{
6629
0
    return SSL_set_block_padding_ex(ssl, block_size, block_size);
6630
0
}
6631
6632
int SSL_set_num_tickets(SSL *s, size_t num_tickets)
6633
0
{
6634
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6635
6636
0
    if (sc == NULL)
6637
0
        return 0;
6638
6639
0
    sc->num_tickets = num_tickets;
6640
6641
0
    return 1;
6642
0
}
6643
6644
size_t SSL_get_num_tickets(const SSL *s)
6645
0
{
6646
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6647
6648
0
    if (sc == NULL)
6649
0
        return 0;
6650
6651
0
    return sc->num_tickets;
6652
0
}
6653
6654
int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
6655
0
{
6656
0
    ctx->num_tickets = num_tickets;
6657
6658
0
    return 1;
6659
0
}
6660
6661
size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
6662
0
{
6663
0
    return ctx->num_tickets;
6664
0
}
6665
6666
/* Retrieve handshake hashes */
6667
int ssl_handshake_hash(SSL_CONNECTION *s,
6668
    unsigned char *out, size_t outlen,
6669
    size_t *hashlen)
6670
201k
{
6671
201k
    EVP_MD_CTX *ctx = NULL;
6672
201k
    EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
6673
201k
    int hashleni = EVP_MD_CTX_get_size(hdgst);
6674
201k
    int ret = 0;
6675
6676
201k
    if (hashleni < 0 || (size_t)hashleni > outlen) {
6677
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6678
0
        goto err;
6679
0
    }
6680
6681
201k
    ctx = EVP_MD_CTX_new();
6682
201k
    if (ctx == NULL) {
6683
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6684
0
        goto err;
6685
0
    }
6686
6687
201k
    if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
6688
201k
        || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
6689
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6690
0
        goto err;
6691
0
    }
6692
6693
201k
    *hashlen = hashleni;
6694
6695
201k
    ret = 1;
6696
201k
err:
6697
201k
    EVP_MD_CTX_free(ctx);
6698
201k
    return ret;
6699
201k
}
6700
6701
int SSL_session_reused(const SSL *s)
6702
0
{
6703
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6704
6705
0
    if (sc == NULL)
6706
0
        return 0;
6707
6708
0
    return sc->hit;
6709
0
}
6710
6711
int SSL_is_server(const SSL *s)
6712
0
{
6713
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6714
6715
0
    if (sc == NULL)
6716
0
        return 0;
6717
6718
0
    return sc->server;
6719
0
}
6720
6721
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
6722
void SSL_set_debug(SSL *s, int debug)
6723
0
{
6724
    /* Old function was do-nothing anyway... */
6725
0
    (void)s;
6726
0
    (void)debug;
6727
0
}
6728
#endif
6729
6730
void SSL_set_security_level(SSL *s, int level)
6731
0
{
6732
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6733
6734
0
    if (sc == NULL)
6735
0
        return;
6736
6737
0
    sc->cert->sec_level = level;
6738
0
}
6739
6740
int SSL_get_security_level(const SSL *s)
6741
30.9M
{
6742
30.9M
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6743
6744
30.9M
    if (sc == NULL)
6745
0
        return 0;
6746
6747
30.9M
    return sc->cert->sec_level;
6748
30.9M
}
6749
6750
void SSL_set_security_callback(SSL *s,
6751
    int (*cb)(const SSL *s, const SSL_CTX *ctx,
6752
        int op, int bits, int nid,
6753
        void *other, void *ex))
6754
0
{
6755
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6756
6757
0
    if (sc == NULL)
6758
0
        return;
6759
6760
0
    sc->cert->sec_cb = cb;
6761
0
}
6762
6763
int (*SSL_get_security_callback(const SSL *s))(const SSL *s,
6764
    const SSL_CTX *ctx, int op,
6765
    int bits, int nid, void *other,
6766
    void *ex)
6767
0
{
6768
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6769
6770
0
    if (sc == NULL)
6771
0
        return NULL;
6772
6773
0
    return sc->cert->sec_cb;
6774
0
}
6775
6776
void SSL_set0_security_ex_data(SSL *s, void *ex)
6777
0
{
6778
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6779
6780
0
    if (sc == NULL)
6781
0
        return;
6782
6783
0
    sc->cert->sec_ex = ex;
6784
0
}
6785
6786
void *SSL_get0_security_ex_data(const SSL *s)
6787
0
{
6788
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6789
6790
0
    if (sc == NULL)
6791
0
        return NULL;
6792
6793
0
    return sc->cert->sec_ex;
6794
0
}
6795
6796
void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
6797
0
{
6798
0
    ctx->cert->sec_level = level;
6799
0
}
6800
6801
int SSL_CTX_get_security_level(const SSL_CTX *ctx)
6802
153k
{
6803
153k
    return ctx->cert->sec_level;
6804
153k
}
6805
6806
void SSL_CTX_set_security_callback(SSL_CTX *ctx,
6807
    int (*cb)(const SSL *s, const SSL_CTX *ctx,
6808
        int op, int bits, int nid,
6809
        void *other, void *ex))
6810
0
{
6811
0
    ctx->cert->sec_cb = cb;
6812
0
}
6813
6814
int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx))(const SSL *s,
6815
    const SSL_CTX *ctx,
6816
    int op, int bits,
6817
    int nid,
6818
    void *other,
6819
    void *ex)
6820
0
{
6821
0
    return ctx->cert->sec_cb;
6822
0
}
6823
6824
void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
6825
0
{
6826
0
    ctx->cert->sec_ex = ex;
6827
0
}
6828
6829
void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
6830
0
{
6831
0
    return ctx->cert->sec_ex;
6832
0
}
6833
6834
uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
6835
0
{
6836
0
    return ctx->options;
6837
0
}
6838
6839
uint64_t SSL_get_options(const SSL *s)
6840
299k
{
6841
299k
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6842
6843
299k
#ifndef OPENSSL_NO_QUIC
6844
299k
    if (IS_QUIC(s))
6845
0
        return ossl_quic_get_options(s);
6846
299k
#endif
6847
6848
299k
    if (sc == NULL)
6849
0
        return 0;
6850
6851
299k
    return sc->options;
6852
299k
}
6853
6854
uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
6855
0
{
6856
0
    return ctx->options |= op;
6857
0
}
6858
6859
uint64_t SSL_set_options(SSL *s, uint64_t op)
6860
0
{
6861
0
    SSL_CONNECTION *sc;
6862
0
    OSSL_PARAM options[2], *opts = options;
6863
6864
0
#ifndef OPENSSL_NO_QUIC
6865
0
    if (IS_QUIC(s))
6866
0
        return ossl_quic_set_options(s, op);
6867
0
#endif
6868
6869
0
    sc = SSL_CONNECTION_FROM_SSL(s);
6870
0
    if (sc == NULL)
6871
0
        return 0;
6872
6873
0
    sc->options |= op;
6874
6875
0
    *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6876
0
        &sc->options);
6877
0
    *opts = OSSL_PARAM_construct_end();
6878
6879
    /* Ignore return value */
6880
0
    sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
6881
0
    sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
6882
6883
0
    return sc->options;
6884
0
}
6885
6886
uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
6887
0
{
6888
0
    return ctx->options &= ~op;
6889
0
}
6890
6891
uint64_t SSL_clear_options(SSL *s, uint64_t op)
6892
42.8k
{
6893
42.8k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6894
42.8k
    OSSL_PARAM options[2], *opts = options;
6895
6896
42.8k
#ifndef OPENSSL_NO_QUIC
6897
42.8k
    if (IS_QUIC(s))
6898
0
        return ossl_quic_clear_options(s, op);
6899
42.8k
#endif
6900
6901
42.8k
    if (sc == NULL)
6902
0
        return 0;
6903
6904
42.8k
    sc->options &= ~op;
6905
6906
42.8k
    *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6907
42.8k
        &sc->options);
6908
42.8k
    *opts = OSSL_PARAM_construct_end();
6909
6910
    /* Ignore return value */
6911
42.8k
    sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
6912
42.8k
    sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
6913
6914
42.8k
    return sc->options;
6915
42.8k
}
6916
6917
STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
6918
0
{
6919
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6920
6921
0
    if (sc == NULL)
6922
0
        return NULL;
6923
6924
0
    return sc->verified_chain;
6925
0
}
6926
6927
IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
6928
6929
#ifndef OPENSSL_NO_CT
6930
6931
/*
6932
 * Moves SCTs from the |src| stack to the |dst| stack.
6933
 * The source of each SCT will be set to |origin|.
6934
 * If |dst| points to a NULL pointer, a new stack will be created and owned by
6935
 * the caller.
6936
 * Returns the number of SCTs moved, or a negative integer if an error occurs.
6937
 * The |dst| stack is created and possibly partially populated even in case
6938
 * of error, likewise the |src| stack may be left in an intermediate state.
6939
 */
6940
static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
6941
    sct_source_t origin)
6942
0
{
6943
0
    int scts_moved = 0;
6944
0
    SCT *sct = NULL;
6945
6946
0
    if (*dst == NULL) {
6947
0
        *dst = sk_SCT_new_null();
6948
0
        if (*dst == NULL) {
6949
0
            ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
6950
0
            goto err;
6951
0
        }
6952
0
    }
6953
6954
0
    while ((sct = sk_SCT_pop(src)) != NULL) {
6955
0
        if (SCT_set_source(sct, origin) != 1)
6956
0
            goto err;
6957
6958
0
        if (!sk_SCT_push(*dst, sct))
6959
0
            goto err;
6960
0
        scts_moved += 1;
6961
0
    }
6962
6963
0
    return scts_moved;
6964
0
err:
6965
0
    SCT_free(sct);
6966
0
    return -1;
6967
0
}
6968
6969
/*
6970
 * Look for data collected during ServerHello and parse if found.
6971
 * Returns the number of SCTs extracted.
6972
 */
6973
static int ct_extract_tls_extension_scts(SSL_CONNECTION *s)
6974
0
{
6975
0
    int scts_extracted = 0;
6976
6977
0
    if (s->ext.scts != NULL) {
6978
0
        const unsigned char *p = s->ext.scts;
6979
0
        STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
6980
6981
0
        scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
6982
6983
0
        SCT_LIST_free(scts);
6984
0
    }
6985
6986
0
    return scts_extracted;
6987
0
}
6988
6989
/*
6990
 * Checks for an OCSP response and then attempts to extract any SCTs found if it
6991
 * contains an SCT X509 extension. They will be stored in |s->scts|.
6992
 * Returns:
6993
 * - The number of SCTs extracted, assuming an OCSP response exists.
6994
 * - 0 if no OCSP response exists or it contains no SCTs.
6995
 * - A negative integer if an error occurs.
6996
 */
6997
static int ct_extract_ocsp_response_scts(SSL_CONNECTION *s)
6998
0
{
6999
0
#ifndef OPENSSL_NO_OCSP
7000
0
    int scts_extracted = 0;
7001
0
    OCSP_BASICRESP *br = NULL;
7002
0
    OCSP_RESPONSE *rsp = NULL;
7003
0
    STACK_OF(SCT) *scts = NULL;
7004
0
    int ret;
7005
0
    int i, j;
7006
7007
0
    if (s->ext.ocsp.resp_ex == NULL)
7008
0
        goto err;
7009
7010
0
    for (j = 0; j < sk_OCSP_RESPONSE_num(s->ext.ocsp.resp_ex); j++) {
7011
0
        rsp = sk_OCSP_RESPONSE_value(s->ext.ocsp.resp_ex, j);
7012
0
        if (rsp == NULL)
7013
0
            goto err;
7014
7015
0
        br = OCSP_response_get1_basic(rsp);
7016
0
        if (br == NULL)
7017
0
            goto err;
7018
7019
0
        for (i = 0; i < OCSP_resp_count(br); ++i) {
7020
0
            OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
7021
7022
0
            if (single == NULL)
7023
0
                continue;
7024
7025
0
            scts = OCSP_SINGLERESP_get1_ext_d2i(single,
7026
0
                NID_ct_cert_scts, NULL, NULL);
7027
0
            if (scts == NULL) {
7028
0
                scts_extracted = -1;
7029
0
                goto err;
7030
0
            }
7031
7032
0
            ret = ct_move_scts(&s->scts, scts,
7033
0
                SCT_SOURCE_OCSP_STAPLED_RESPONSE);
7034
7035
0
            SCT_LIST_free(scts);
7036
7037
0
            if (ret < 0) {
7038
0
                scts_extracted = -1;
7039
0
                goto err;
7040
0
            }
7041
7042
0
            scts_extracted += ret;
7043
0
        }
7044
7045
0
        OCSP_BASICRESP_free(br);
7046
        /* to assure that is not freed twice */
7047
0
        br = NULL;
7048
0
    }
7049
0
err:
7050
0
    OCSP_BASICRESP_free(br);
7051
0
    return scts_extracted;
7052
#else
7053
    /* Behave as if no OCSP response exists */
7054
    return 0;
7055
#endif
7056
0
}
7057
7058
/*
7059
 * Attempts to extract SCTs from the peer certificate.
7060
 * Return the number of SCTs extracted, or a negative integer if an error
7061
 * occurs.
7062
 */
7063
static int ct_extract_x509v3_extension_scts(SSL_CONNECTION *s)
7064
0
{
7065
0
    int scts_extracted = 0;
7066
0
    X509 *cert = s->session != NULL ? s->session->peer : NULL;
7067
7068
0
    if (cert != NULL) {
7069
0
        STACK_OF(SCT) *scts = X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
7070
7071
0
        scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
7072
7073
0
        SCT_LIST_free(scts);
7074
0
    }
7075
7076
0
    return scts_extracted;
7077
0
}
7078
7079
/*
7080
 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
7081
 * response (if it exists) and X509v3 extensions in the certificate.
7082
 * Returns NULL if an error occurs.
7083
 */
7084
const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
7085
0
{
7086
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7087
7088
0
    if (sc == NULL)
7089
0
        return NULL;
7090
7091
0
    if (!sc->scts_parsed) {
7092
0
        if (ct_extract_tls_extension_scts(sc) < 0 || ct_extract_ocsp_response_scts(sc) < 0 || ct_extract_x509v3_extension_scts(sc) < 0)
7093
0
            goto err;
7094
7095
0
        sc->scts_parsed = 1;
7096
0
    }
7097
0
    return sc->scts;
7098
0
err:
7099
0
    return NULL;
7100
0
}
7101
7102
static int ct_permissive(const CT_POLICY_EVAL_CTX *ctx,
7103
    const STACK_OF(SCT) *scts, void *unused_arg)
7104
0
{
7105
0
    return 1;
7106
0
}
7107
7108
static int ct_strict(const CT_POLICY_EVAL_CTX *ctx,
7109
    const STACK_OF(SCT) *scts, void *unused_arg)
7110
0
{
7111
0
    int count = scts != NULL ? sk_SCT_num(scts) : 0;
7112
0
    int i;
7113
7114
0
    for (i = 0; i < count; ++i) {
7115
0
        SCT *sct = sk_SCT_value(scts, i);
7116
0
        int status = SCT_get_validation_status(sct);
7117
7118
0
        if (status == SCT_VALIDATION_STATUS_VALID)
7119
0
            return 1;
7120
0
    }
7121
0
    ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
7122
0
    return 0;
7123
0
}
7124
7125
int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
7126
    void *arg)
7127
140k
{
7128
140k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7129
7130
140k
    if (sc == NULL)
7131
0
        return 0;
7132
7133
    /*
7134
     * Since code exists that uses the custom extension handler for CT, look
7135
     * for this and throw an error if they have already registered to use CT.
7136
     */
7137
140k
    if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx, TLSEXT_TYPE_signed_certificate_timestamp)) {
7138
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
7139
0
        return 0;
7140
0
    }
7141
7142
140k
    if (callback != NULL) {
7143
        /*
7144
         * If we are validating CT, then we MUST accept SCTs served via OCSP
7145
         */
7146
0
        if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
7147
0
            return 0;
7148
0
    }
7149
7150
140k
    sc->ct_validation_callback = callback;
7151
140k
    sc->ct_validation_callback_arg = arg;
7152
7153
140k
    return 1;
7154
140k
}
7155
7156
int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
7157
    ssl_ct_validation_cb callback, void *arg)
7158
0
{
7159
    /*
7160
     * Since code exists that uses the custom extension handler for CT, look for
7161
     * this and throw an error if they have already registered to use CT.
7162
     */
7163
0
    if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx, TLSEXT_TYPE_signed_certificate_timestamp)) {
7164
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
7165
0
        return 0;
7166
0
    }
7167
7168
0
    ctx->ct_validation_callback = callback;
7169
0
    ctx->ct_validation_callback_arg = arg;
7170
0
    return 1;
7171
0
}
7172
7173
int SSL_ct_is_enabled(const SSL *s)
7174
0
{
7175
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7176
7177
0
    if (sc == NULL)
7178
0
        return 0;
7179
7180
0
    return sc->ct_validation_callback != NULL;
7181
0
}
7182
7183
int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
7184
0
{
7185
0
    return ctx->ct_validation_callback != NULL;
7186
0
}
7187
7188
int ssl_validate_ct(SSL_CONNECTION *s)
7189
0
{
7190
0
    int ret = 0;
7191
0
    X509 *cert = s->session != NULL ? s->session->peer : NULL;
7192
0
    X509 *issuer;
7193
0
    SSL_DANE *dane = &s->dane;
7194
0
    CT_POLICY_EVAL_CTX *ctx = NULL;
7195
0
    const STACK_OF(SCT) *scts;
7196
7197
    /*
7198
     * If no callback is set, the peer is anonymous, or its chain is invalid,
7199
     * skip SCT validation - just return success.  Applications that continue
7200
     * handshakes without certificates, with unverified chains, or pinned leaf
7201
     * certificates are outside the scope of the WebPKI and CT.
7202
     *
7203
     * The above exclusions notwithstanding the vast majority of peers will
7204
     * have rather ordinary certificate chains validated by typical
7205
     * applications that perform certificate verification and therefore will
7206
     * process SCTs when enabled.
7207
     */
7208
0
    if (s->ct_validation_callback == NULL || cert == NULL || s->verify_result != X509_V_OK || s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
7209
0
        return 1;
7210
7211
    /*
7212
     * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
7213
     * trust-anchors.  See https://tools.ietf.org/html/rfc7671#section-4.2
7214
     */
7215
0
    if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
7216
0
        switch (dane->mtlsa->usage) {
7217
0
        case DANETLS_USAGE_DANE_TA:
7218
0
        case DANETLS_USAGE_DANE_EE:
7219
0
            return 1;
7220
0
        }
7221
0
    }
7222
7223
0
    ctx = CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
7224
0
        SSL_CONNECTION_GET_CTX(s)->propq);
7225
0
    if (ctx == NULL) {
7226
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CT_LIB);
7227
0
        goto end;
7228
0
    }
7229
7230
0
    issuer = sk_X509_value(s->verified_chain, 1);
7231
0
    CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
7232
0
    CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
7233
0
    CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx,
7234
0
        SSL_CONNECTION_GET_CTX(s)->ctlog_store);
7235
0
    CT_POLICY_EVAL_CTX_set_time(
7236
0
        ctx, (uint64_t)SSL_SESSION_get_time_ex(s->session) * 1000);
7237
7238
0
    scts = SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s));
7239
7240
    /*
7241
     * This function returns success (> 0) only when all the SCTs are valid, 0
7242
     * when some are invalid, and < 0 on various internal errors (out of
7243
     * memory, etc.).  Having some, or even all, invalid SCTs is not sufficient
7244
     * reason to abort the handshake, that decision is up to the callback.
7245
     * Therefore, we error out only in the unexpected case that the return
7246
     * value is negative.
7247
     *
7248
     * XXX: One might well argue that the return value of this function is an
7249
     * unfortunate design choice.  Its job is only to determine the validation
7250
     * status of each of the provided SCTs.  So long as it correctly separates
7251
     * the wheat from the chaff it should return success.  Failure in this case
7252
     * ought to correspond to an inability to carry out its duties.
7253
     */
7254
0
    if (SCT_LIST_validate(scts, ctx) < 0) {
7255
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
7256
0
        goto end;
7257
0
    }
7258
7259
0
    ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
7260
0
    if (ret < 0)
7261
0
        ret = 0; /* This function returns 0 on failure */
7262
0
    if (!ret)
7263
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
7264
7265
0
end:
7266
0
    CT_POLICY_EVAL_CTX_free(ctx);
7267
    /*
7268
     * With SSL_VERIFY_NONE the session may be cached and reused despite a
7269
     * failure return code here.  Also the application may wish the complete
7270
     * the handshake, and then disconnect cleanly at a higher layer, after
7271
     * checking the verification status of the completed connection.
7272
     *
7273
     * We therefore force a certificate verification failure which will be
7274
     * visible via SSL_get_verify_result() and cached as part of any resumed
7275
     * session.
7276
     *
7277
     * Note: the permissive callback is for information gathering only, always
7278
     * returns success, and does not affect verification status.  Only the
7279
     * strict callback or a custom application-specified callback can trigger
7280
     * connection failure or record a verification error.
7281
     */
7282
0
    if (ret <= 0)
7283
0
        s->verify_result = X509_V_ERR_NO_VALID_SCTS;
7284
0
    return ret;
7285
0
}
7286
7287
int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
7288
0
{
7289
0
    switch (validation_mode) {
7290
0
    default:
7291
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
7292
0
        return 0;
7293
0
    case SSL_CT_VALIDATION_PERMISSIVE:
7294
0
        return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
7295
0
    case SSL_CT_VALIDATION_STRICT:
7296
0
        return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
7297
0
    }
7298
0
}
7299
7300
int SSL_enable_ct(SSL *s, int validation_mode)
7301
0
{
7302
0
    switch (validation_mode) {
7303
0
    default:
7304
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
7305
0
        return 0;
7306
0
    case SSL_CT_VALIDATION_PERMISSIVE:
7307
0
        return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
7308
0
    case SSL_CT_VALIDATION_STRICT:
7309
0
        return SSL_set_ct_validation_callback(s, ct_strict, NULL);
7310
0
    }
7311
0
}
7312
7313
int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
7314
0
{
7315
0
    return CTLOG_STORE_load_default_file(ctx->ctlog_store);
7316
0
}
7317
7318
int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
7319
0
{
7320
0
    return CTLOG_STORE_load_file(ctx->ctlog_store, path);
7321
0
}
7322
7323
void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs)
7324
0
{
7325
0
    CTLOG_STORE_free(ctx->ctlog_store);
7326
0
    ctx->ctlog_store = logs;
7327
0
}
7328
7329
const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
7330
0
{
7331
0
    return ctx->ctlog_store;
7332
0
}
7333
7334
#endif /* OPENSSL_NO_CT */
7335
7336
void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
7337
    void *arg)
7338
0
{
7339
0
    c->client_hello_cb = cb;
7340
0
    c->client_hello_cb_arg = arg;
7341
0
}
7342
7343
void SSL_CTX_set_new_pending_conn_cb(SSL_CTX *c, SSL_new_pending_conn_cb_fn cb,
7344
    void *arg)
7345
0
{
7346
0
    c->new_pending_conn_cb = cb;
7347
0
    c->new_pending_conn_arg = arg;
7348
0
}
7349
7350
#ifndef OPENSSL_NO_DEPRECATED_4_0
7351
int SSL_client_hello_isv2(SSL *s)
7352
0
{
7353
0
    return 0;
7354
0
}
7355
#endif
7356
7357
unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
7358
0
{
7359
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7360
7361
0
    if (sc == NULL)
7362
0
        return 0;
7363
7364
0
    if (sc->clienthello == NULL)
7365
0
        return 0;
7366
0
    return sc->clienthello->legacy_version;
7367
0
}
7368
7369
size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
7370
0
{
7371
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7372
7373
0
    if (sc == NULL)
7374
0
        return 0;
7375
7376
0
    if (sc->clienthello == NULL)
7377
0
        return 0;
7378
0
    if (out != NULL)
7379
0
        *out = sc->clienthello->random;
7380
0
    return SSL3_RANDOM_SIZE;
7381
0
}
7382
7383
size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
7384
0
{
7385
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7386
7387
0
    if (sc == NULL)
7388
0
        return 0;
7389
7390
0
    if (sc->clienthello == NULL)
7391
0
        return 0;
7392
0
    if (out != NULL)
7393
0
        *out = sc->clienthello->session_id;
7394
0
    return sc->clienthello->session_id_len;
7395
0
}
7396
7397
size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
7398
0
{
7399
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7400
7401
0
    if (sc == NULL)
7402
0
        return 0;
7403
7404
0
    if (sc->clienthello == NULL)
7405
0
        return 0;
7406
0
    if (out != NULL)
7407
0
        *out = PACKET_data(&sc->clienthello->ciphersuites);
7408
0
    return PACKET_remaining(&sc->clienthello->ciphersuites);
7409
0
}
7410
7411
size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
7412
0
{
7413
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7414
7415
0
    if (sc == NULL)
7416
0
        return 0;
7417
7418
0
    if (sc->clienthello == NULL)
7419
0
        return 0;
7420
0
    if (out != NULL)
7421
0
        *out = sc->clienthello->compressions;
7422
0
    return sc->clienthello->compressions_len;
7423
0
}
7424
7425
int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
7426
0
{
7427
0
    RAW_EXTENSION *ext;
7428
0
    int *present;
7429
0
    size_t num = 0, i;
7430
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7431
7432
0
    if (sc == NULL)
7433
0
        return 0;
7434
7435
0
    if (sc->clienthello == NULL || out == NULL || outlen == NULL)
7436
0
        return 0;
7437
0
    for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
7438
0
        ext = sc->clienthello->pre_proc_exts + i;
7439
0
        if (ext->present)
7440
0
            num++;
7441
0
    }
7442
0
    if (num == 0) {
7443
0
        *out = NULL;
7444
0
        *outlen = 0;
7445
0
        return 1;
7446
0
    }
7447
0
    if ((present = OPENSSL_malloc_array(num, sizeof(*present))) == NULL)
7448
0
        return 0;
7449
0
    for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
7450
0
        ext = sc->clienthello->pre_proc_exts + i;
7451
0
        if (ext->present) {
7452
0
            if (ext->received_order >= num)
7453
0
                goto err;
7454
0
            present[ext->received_order] = ext->type;
7455
0
        }
7456
0
    }
7457
0
    *out = present;
7458
0
    *outlen = num;
7459
0
    return 1;
7460
0
err:
7461
0
    OPENSSL_free(present);
7462
0
    return 0;
7463
0
}
7464
7465
int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts)
7466
0
{
7467
0
    RAW_EXTENSION *ext;
7468
0
    size_t num = 0, i;
7469
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7470
7471
0
    if (sc == NULL)
7472
0
        return 0;
7473
7474
0
    if (sc->clienthello == NULL || num_exts == NULL)
7475
0
        return 0;
7476
0
    for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
7477
0
        ext = sc->clienthello->pre_proc_exts + i;
7478
0
        if (ext->present)
7479
0
            num++;
7480
0
    }
7481
0
    if (num == 0) {
7482
0
        *num_exts = 0;
7483
0
        return 1;
7484
0
    }
7485
0
    if (exts == NULL) {
7486
0
        *num_exts = num;
7487
0
        return 1;
7488
0
    }
7489
0
    if (*num_exts < num)
7490
0
        return 0;
7491
0
    for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
7492
0
        ext = sc->clienthello->pre_proc_exts + i;
7493
0
        if (ext->present) {
7494
0
            if (ext->received_order >= num)
7495
0
                return 0;
7496
0
            exts[ext->received_order] = ext->type;
7497
0
        }
7498
0
    }
7499
0
    *num_exts = num;
7500
0
    return 1;
7501
0
}
7502
7503
int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
7504
    size_t *outlen)
7505
0
{
7506
0
    size_t i;
7507
0
    RAW_EXTENSION *r;
7508
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7509
7510
0
    if (sc == NULL)
7511
0
        return 0;
7512
7513
0
    if (sc->clienthello == NULL)
7514
0
        return 0;
7515
0
    for (i = 0; i < sc->clienthello->pre_proc_exts_len; ++i) {
7516
0
        r = sc->clienthello->pre_proc_exts + i;
7517
0
        if (r->present && r->type == type) {
7518
0
            if (out != NULL)
7519
0
                *out = PACKET_data(&r->data);
7520
0
            if (outlen != NULL)
7521
0
                *outlen = PACKET_remaining(&r->data);
7522
0
            return 1;
7523
0
        }
7524
0
    }
7525
0
    return 0;
7526
0
}
7527
7528
int SSL_free_buffers(SSL *ssl)
7529
0
{
7530
0
    RECORD_LAYER *rl;
7531
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
7532
7533
0
    if (sc == NULL)
7534
0
        return 0;
7535
7536
0
    rl = &sc->rlayer;
7537
7538
0
    return rl->rrlmethod->free_buffers(rl->rrl)
7539
0
        && rl->wrlmethod->free_buffers(rl->wrl);
7540
0
}
7541
7542
int SSL_alloc_buffers(SSL *ssl)
7543
0
{
7544
0
    RECORD_LAYER *rl;
7545
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
7546
7547
0
    if (sc == NULL)
7548
0
        return 0;
7549
7550
    /* QUIC always has buffers allocated. */
7551
0
    if (IS_QUIC(ssl))
7552
0
        return 1;
7553
7554
0
    rl = &sc->rlayer;
7555
7556
0
    return rl->rrlmethod->alloc_buffers(rl->rrl)
7557
0
        && rl->wrlmethod->alloc_buffers(rl->wrl);
7558
0
}
7559
7560
void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
7561
0
{
7562
0
    ctx->keylog_callback = cb;
7563
0
}
7564
7565
SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
7566
0
{
7567
0
    return ctx->keylog_callback;
7568
0
}
7569
7570
static int nss_keylog_int(const char *prefix,
7571
    SSL_CONNECTION *sc,
7572
    const uint8_t *parameter_1,
7573
    size_t parameter_1_len,
7574
    const uint8_t *parameter_2,
7575
    size_t parameter_2_len)
7576
104k
{
7577
104k
    char *out = NULL;
7578
104k
    char *cursor = NULL;
7579
104k
    size_t out_len = 0, i, prefix_len;
7580
104k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
7581
7582
#ifndef OPENSSL_NO_SSLKEYLOG
7583
    if (sctx->keylog_callback == NULL && sctx->do_sslkeylog == 0)
7584
        return 1;
7585
#else
7586
104k
    if (sctx->keylog_callback == NULL)
7587
104k
        return 1;
7588
0
#endif
7589
7590
    /*
7591
     * Our output buffer will contain the following strings, rendered with
7592
     * space characters in between, terminated by a NULL character: first the
7593
     * prefix, then the first parameter, then the second parameter. The
7594
     * meaning of each parameter depends on the specific key material being
7595
     * logged. Note that the first and second parameters are encoded in
7596
     * hexadecimal, so we need a buffer that is twice their lengths.
7597
     */
7598
0
    prefix_len = strlen(prefix);
7599
0
    out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
7600
0
    if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
7601
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
7602
0
        return 0;
7603
0
    }
7604
7605
0
    memcpy(cursor, prefix, prefix_len);
7606
0
    cursor += prefix_len;
7607
0
    *cursor++ = ' ';
7608
7609
0
    for (i = 0; i < parameter_1_len; ++i)
7610
0
        cursor += ossl_to_lowerhex(cursor, parameter_1[i]);
7611
0
    *cursor++ = ' ';
7612
7613
0
    for (i = 0; i < parameter_2_len; ++i)
7614
0
        cursor += ossl_to_lowerhex(cursor, parameter_2[i]);
7615
0
    *cursor = '\0';
7616
7617
#ifndef OPENSSL_NO_SSLKEYLOG
7618
    if (sctx->do_sslkeylog == 1)
7619
        do_sslkeylogfile(SSL_CONNECTION_GET_SSL(sc), (const char *)out);
7620
#endif
7621
0
    if (sctx->keylog_callback != NULL)
7622
0
        sctx->keylog_callback(SSL_CONNECTION_GET_USER_SSL(sc), (const char *)out);
7623
0
    OPENSSL_clear_free(out, out_len);
7624
0
    return 1;
7625
0
}
7626
7627
int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *sc,
7628
    const uint8_t *encrypted_premaster,
7629
    size_t encrypted_premaster_len,
7630
    const uint8_t *premaster,
7631
    size_t premaster_len)
7632
8.99k
{
7633
8.99k
    if (encrypted_premaster_len < 8) {
7634
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
7635
0
        return 0;
7636
0
    }
7637
7638
    /* We only want the first 8 bytes of the encrypted premaster as a tag. */
7639
8.99k
    return nss_keylog_int("RSA",
7640
8.99k
        sc,
7641
8.99k
        encrypted_premaster,
7642
8.99k
        8,
7643
8.99k
        premaster,
7644
8.99k
        premaster_len);
7645
8.99k
}
7646
7647
int ssl_log_secret(SSL_CONNECTION *sc,
7648
    const char *label,
7649
    const uint8_t *secret,
7650
    size_t secret_len)
7651
57.1k
{
7652
57.1k
    const uint8_t *client_random = sc->s3.client_random;
7653
7654
    /*
7655
     * If the client attempted ECH, secrets are logged against the inner
7656
     * ClientHello random. CLIENT_EARLY_LABEL and EARLY_EXPORTER_SECRET_LABEL
7657
     * are special-cased because they are logged before the server's
7658
     * acceptance of ECH is known, but are always derived from the inner
7659
     * ClientHello (see tls13_change_cipher_state()), so they use the inner
7660
     * random regardless of ech.success. On rejection the early secrets
7661
     * therefore carry the inner random while later ones carry the outer:
7662
     * each line is tagged with the random of the ClientHello its secret
7663
     * was derived from, and since a rejecting server never derives the
7664
     * early secrets, both peers still log identical material for every label
7665
     * they both log.
7666
     */
7667
57.1k
#ifndef OPENSSL_NO_ECH
7668
57.1k
    if (!sc->server && sc->ext.ech.attempted == 1
7669
0
        && (sc->ext.ech.success == 1
7670
0
            || strcmp(label, CLIENT_EARLY_LABEL) == 0
7671
0
            || strcmp(label, EARLY_EXPORTER_SECRET_LABEL) == 0))
7672
0
        client_random = sc->ext.ech.client_random;
7673
57.1k
#endif
7674
7675
57.1k
    return nss_keylog_int(label,
7676
57.1k
        sc,
7677
57.1k
        client_random,
7678
57.1k
        SSL3_RANDOM_SIZE,
7679
57.1k
        secret,
7680
57.1k
        secret_len);
7681
57.1k
}
7682
7683
int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites)
7684
18.5k
{
7685
18.5k
    if (PACKET_remaining(cipher_suites) == 0) {
7686
21
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_CIPHERS_SPECIFIED);
7687
21
        return 0;
7688
21
    }
7689
7690
18.5k
    if (PACKET_remaining(cipher_suites) % TLS_CIPHER_LEN != 0) {
7691
9
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
7692
9
        return 0;
7693
9
    }
7694
7695
18.4k
    OPENSSL_free(s->s3.tmp.ciphers_raw);
7696
18.4k
    s->s3.tmp.ciphers_raw = NULL;
7697
18.4k
    s->s3.tmp.ciphers_rawlen = 0;
7698
7699
18.4k
    if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
7700
18.4k
            &s->s3.tmp.ciphers_rawlen)) {
7701
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
7702
0
        return 0;
7703
0
    }
7704
18.4k
    return 1;
7705
18.4k
}
7706
7707
int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
7708
    int isv2format, STACK_OF(SSL_CIPHER) **sk,
7709
    STACK_OF(SSL_CIPHER) **scsvs)
7710
0
{
7711
0
    PACKET pkt;
7712
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7713
7714
0
    if (sc == NULL || isv2format)
7715
0
        return 0;
7716
7717
0
    if (!PACKET_buf_init(&pkt, bytes, len))
7718
0
        return 0;
7719
0
    return ossl_bytes_to_cipher_list(sc, &pkt, sk, scsvs, 0);
7720
0
}
7721
7722
int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites,
7723
    STACK_OF(SSL_CIPHER) **skp,
7724
    STACK_OF(SSL_CIPHER) **scsvs_out,
7725
    int fatal)
7726
18.4k
{
7727
18.4k
    const SSL_CIPHER *c;
7728
18.4k
    STACK_OF(SSL_CIPHER) *sk = NULL;
7729
18.4k
    STACK_OF(SSL_CIPHER) *scsvs = NULL;
7730
18.4k
    int n = TLS_CIPHER_LEN;
7731
18.4k
    unsigned char cipher[TLS_CIPHER_LEN];
7732
7733
18.4k
    if (PACKET_remaining(cipher_suites) == 0) {
7734
0
        if (fatal)
7735
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
7736
0
        else
7737
0
            ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
7738
0
        return 0;
7739
0
    }
7740
7741
18.4k
    if (PACKET_remaining(cipher_suites) % n != 0) {
7742
0
        if (fatal)
7743
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
7744
0
                SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
7745
0
        else
7746
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
7747
0
        return 0;
7748
0
    }
7749
7750
18.4k
    sk = sk_SSL_CIPHER_new_null();
7751
18.4k
    scsvs = sk_SSL_CIPHER_new_null();
7752
18.4k
    if (sk == NULL || scsvs == NULL) {
7753
0
        if (fatal)
7754
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
7755
0
        else
7756
0
            ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
7757
0
        goto err;
7758
0
    }
7759
7760
315k
    while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
7761
296k
        c = ssl_get_cipher_by_char(s, cipher, 1);
7762
296k
        if (c != NULL) {
7763
112k
            if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) || (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
7764
0
                if (fatal)
7765
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
7766
0
                else
7767
0
                    ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
7768
0
                goto err;
7769
0
            }
7770
112k
        }
7771
296k
    }
7772
18.4k
    if (PACKET_remaining(cipher_suites) > 0) {
7773
0
        if (fatal)
7774
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
7775
0
        else
7776
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
7777
0
        goto err;
7778
0
    }
7779
7780
18.4k
    if (skp != NULL)
7781
18.4k
        *skp = sk;
7782
0
    else
7783
0
        sk_SSL_CIPHER_free(sk);
7784
18.4k
    if (scsvs_out != NULL)
7785
18.4k
        *scsvs_out = scsvs;
7786
0
    else
7787
0
        sk_SSL_CIPHER_free(scsvs);
7788
18.4k
    return 1;
7789
0
err:
7790
0
    sk_SSL_CIPHER_free(sk);
7791
0
    sk_SSL_CIPHER_free(scsvs);
7792
0
    return 0;
7793
18.4k
}
7794
7795
int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
7796
0
{
7797
0
    ctx->max_early_data = max_early_data;
7798
7799
0
    return 1;
7800
0
}
7801
7802
uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
7803
0
{
7804
0
    return ctx->max_early_data;
7805
0
}
7806
7807
int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
7808
0
{
7809
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7810
7811
0
    if (sc == NULL)
7812
0
        return 0;
7813
7814
0
    sc->max_early_data = max_early_data;
7815
7816
0
    return 1;
7817
0
}
7818
7819
uint32_t SSL_get_max_early_data(const SSL *s)
7820
0
{
7821
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7822
7823
0
    if (sc == NULL)
7824
0
        return 0;
7825
7826
0
    return sc->max_early_data;
7827
0
}
7828
7829
int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
7830
0
{
7831
0
    ctx->recv_max_early_data = recv_max_early_data;
7832
7833
0
    return 1;
7834
0
}
7835
7836
uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
7837
0
{
7838
0
    return ctx->recv_max_early_data;
7839
0
}
7840
7841
int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
7842
0
{
7843
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7844
7845
0
    if (sc == NULL)
7846
0
        return 0;
7847
7848
0
    sc->recv_max_early_data = recv_max_early_data;
7849
7850
0
    return 1;
7851
0
}
7852
7853
uint32_t SSL_get_recv_max_early_data(const SSL *s)
7854
0
{
7855
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7856
7857
0
    if (sc == NULL)
7858
0
        return 0;
7859
7860
0
    return sc->recv_max_early_data;
7861
0
}
7862
7863
__owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc)
7864
1.36M
{
7865
    /* Return any active Max Fragment Len extension */
7866
1.36M
    if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session))
7867
17.0k
        return GET_MAX_FRAGMENT_LENGTH(sc->session);
7868
7869
    /* return current SSL connection setting */
7870
1.34M
    return (unsigned int)sc->max_send_fragment;
7871
1.36M
}
7872
7873
__owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc)
7874
196k
{
7875
    /* Return a value regarding an active Max Fragment Len extension */
7876
196k
    if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session)
7877
1.12k
        && sc->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(sc->session))
7878
1.12k
        return GET_MAX_FRAGMENT_LENGTH(sc->session);
7879
7880
    /* else limit |split_send_fragment| to current |max_send_fragment| */
7881
195k
    if (sc->split_send_fragment > sc->max_send_fragment)
7882
0
        return (unsigned int)sc->max_send_fragment;
7883
7884
    /* return current SSL connection setting */
7885
195k
    return (unsigned int)sc->split_send_fragment;
7886
195k
}
7887
7888
int SSL_stateless(SSL *s)
7889
0
{
7890
0
    int ret;
7891
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7892
7893
0
    if (sc == NULL)
7894
0
        return 0;
7895
7896
    /* Ensure there is no state left over from a previous invocation */
7897
0
    if (!SSL_clear(s))
7898
0
        return 0;
7899
7900
0
    ERR_clear_error();
7901
7902
0
    sc->s3.flags |= TLS1_FLAGS_STATELESS;
7903
0
    ret = SSL_accept(s);
7904
0
    sc->s3.flags &= ~TLS1_FLAGS_STATELESS;
7905
7906
0
    if (ret > 0 && sc->ext.cookieok)
7907
0
        return 1;
7908
7909
0
    if (sc->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(sc))
7910
0
        return 0;
7911
7912
0
    return -1;
7913
0
}
7914
7915
void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
7916
0
{
7917
0
    ctx->pha_enabled = val;
7918
0
}
7919
7920
void SSL_set_post_handshake_auth(SSL *ssl, int val)
7921
0
{
7922
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
7923
7924
0
    if (sc == NULL)
7925
0
        return;
7926
7927
0
    sc->pha_enabled = val;
7928
0
}
7929
7930
int SSL_verify_client_post_handshake(SSL *ssl)
7931
0
{
7932
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
7933
7934
0
#ifndef OPENSSL_NO_QUIC
7935
0
    if (IS_QUIC(ssl)) {
7936
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
7937
0
        return 0;
7938
0
    }
7939
0
#endif
7940
7941
0
    if (sc == NULL)
7942
0
        return 0;
7943
7944
0
    if (!SSL_CONNECTION_IS_VERSION13(sc)) {
7945
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
7946
0
        return 0;
7947
0
    }
7948
0
    if (!sc->server) {
7949
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
7950
0
        return 0;
7951
0
    }
7952
7953
0
    if (!SSL_is_init_finished(ssl)) {
7954
0
        ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
7955
0
        return 0;
7956
0
    }
7957
7958
0
    switch (sc->post_handshake_auth) {
7959
0
    case SSL_PHA_NONE:
7960
0
        ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
7961
0
        return 0;
7962
0
    default:
7963
0
    case SSL_PHA_EXT_SENT:
7964
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
7965
0
        return 0;
7966
0
    case SSL_PHA_EXT_RECEIVED:
7967
0
        break;
7968
0
    case SSL_PHA_REQUEST_PENDING:
7969
0
        ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
7970
0
        return 0;
7971
0
    case SSL_PHA_REQUESTED:
7972
0
        ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
7973
0
        return 0;
7974
0
    }
7975
7976
0
    sc->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
7977
7978
    /* checks verify_mode and algorithm_auth */
7979
0
    if (!send_certificate_request(sc)) {
7980
0
        sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
7981
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
7982
0
        return 0;
7983
0
    }
7984
7985
0
    ossl_statem_set_in_init(sc, 1);
7986
0
    return 1;
7987
0
}
7988
7989
int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
7990
    SSL_CTX_generate_session_ticket_fn gen_cb,
7991
    SSL_CTX_decrypt_session_ticket_fn dec_cb,
7992
    void *arg)
7993
0
{
7994
0
    ctx->generate_ticket_cb = gen_cb;
7995
0
    ctx->decrypt_ticket_cb = dec_cb;
7996
0
    ctx->ticket_cb_data = arg;
7997
0
    return 1;
7998
0
}
7999
8000
void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
8001
    SSL_allow_early_data_cb_fn cb,
8002
    void *arg)
8003
0
{
8004
0
    ctx->allow_early_data_cb = cb;
8005
0
    ctx->allow_early_data_cb_data = arg;
8006
0
}
8007
8008
void SSL_set_allow_early_data_cb(SSL *s,
8009
    SSL_allow_early_data_cb_fn cb,
8010
    void *arg)
8011
0
{
8012
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
8013
8014
0
    if (sc == NULL)
8015
0
        return;
8016
8017
0
    sc->allow_early_data_cb = cb;
8018
0
    sc->allow_early_data_cb_data = arg;
8019
0
}
8020
8021
const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
8022
    const char *name,
8023
    const char *properties)
8024
2.04M
{
8025
2.04M
    const EVP_CIPHER *ciph;
8026
8027
2.04M
    ERR_set_mark();
8028
2.04M
    ciph = EVP_CIPHER_fetch(libctx, name, properties);
8029
2.04M
    if (ciph != NULL) {
8030
1.35M
        OSSL_PARAM params[2];
8031
1.35M
        int decrypt_only = 0;
8032
8033
1.35M
        params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_DECRYPT_ONLY,
8034
1.35M
            &decrypt_only);
8035
1.35M
        params[1] = OSSL_PARAM_construct_end();
8036
1.35M
        if (EVP_CIPHER_get_params((EVP_CIPHER *)ciph, params)
8037
1.35M
            && decrypt_only) {
8038
            /* If a cipher is decrypt-only, it is unusable */
8039
0
            EVP_CIPHER_free((EVP_CIPHER *)ciph);
8040
0
            ciph = NULL;
8041
0
        }
8042
1.35M
    }
8043
2.04M
    ERR_pop_to_mark();
8044
2.04M
    return ciph;
8045
2.04M
}
8046
8047
int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
8048
50.6k
{
8049
    /* Don't up-ref an implicit EVP_CIPHER */
8050
50.6k
    if (EVP_CIPHER_get0_provider(cipher) == NULL)
8051
0
        return 1;
8052
8053
    /*
8054
     * The cipher was explicitly fetched and therefore it is safe to cast
8055
     * away the const
8056
     */
8057
50.6k
    return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
8058
50.6k
}
8059
8060
void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
8061
4.55M
{
8062
4.55M
    if (cipher == NULL)
8063
1.87M
        return;
8064
8065
2.67M
    if (EVP_CIPHER_get0_provider(cipher) != NULL) {
8066
        /*
8067
         * The cipher was explicitly fetched and therefore it is safe to cast
8068
         * away the const
8069
         */
8070
2.67M
        EVP_CIPHER_free((EVP_CIPHER *)cipher);
8071
2.67M
    }
8072
2.67M
}
8073
8074
int ssl_evp_md_up_ref(const EVP_MD *md)
8075
22.5k
{
8076
    /* Don't up-ref an implicit EVP_MD */
8077
22.5k
    if (EVP_MD_get0_provider(md) == NULL)
8078
0
        return 1;
8079
8080
    /*
8081
     * The digest was explicitly fetched and therefore it is safe to cast
8082
     * away the const
8083
     */
8084
22.5k
    return EVP_MD_up_ref((EVP_MD *)md);
8085
22.5k
}
8086
8087
void ssl_evp_md_free(const EVP_MD *md)
8088
2.96M
{
8089
2.96M
    if (md == NULL)
8090
1.42M
        return;
8091
8092
1.54M
    if (EVP_MD_get0_provider(md) != NULL) {
8093
        /*
8094
         * The digest was explicitly fetched and therefore it is safe to cast
8095
         * away the const
8096
         */
8097
1.54M
        EVP_MD_free((EVP_MD *)md);
8098
1.54M
    }
8099
1.54M
}
8100
8101
int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
8102
0
{
8103
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
8104
8105
0
    if (sc == NULL)
8106
0
        return 0;
8107
8108
0
    if (!ssl_security(sc, SSL_SECOP_TMP_DH,
8109
0
            EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
8110
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
8111
0
        return 0;
8112
0
    }
8113
0
    EVP_PKEY_free(sc->cert->dh_tmp);
8114
0
    sc->cert->dh_tmp = dhpkey;
8115
0
    return 1;
8116
0
}
8117
8118
int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
8119
0
{
8120
0
    if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
8121
0
            EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
8122
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
8123
0
        return 0;
8124
0
    }
8125
0
    EVP_PKEY_free(ctx->cert->dh_tmp);
8126
0
    ctx->cert->dh_tmp = dhpkey;
8127
0
    return 1;
8128
0
}
8129
8130
/* QUIC-specific methods which are supported on QUIC connections only. */
8131
int SSL_handle_events(SSL *s)
8132
0
{
8133
0
    SSL_CONNECTION *sc;
8134
8135
0
#ifndef OPENSSL_NO_QUIC
8136
0
    if (IS_QUIC(s))
8137
0
        return ossl_quic_handle_events(s);
8138
0
#endif
8139
8140
0
    sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
8141
0
    if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc))
8142
        /*
8143
         * DTLSv1_handle_timeout returns 0 if the timer wasn't expired yet,
8144
         * which we consider a success case. Theoretically DTLSv1_handle_timeout
8145
         * can also return 0 if s is NULL or not a DTLS object, but we've
8146
         * already ruled out those possibilities above, so this is not possible
8147
         * here. Thus the only failure cases are where DTLSv1_handle_timeout
8148
         * returns -1.
8149
         */
8150
0
        return DTLSv1_handle_timeout(s) >= 0;
8151
8152
0
    return 1;
8153
0
}
8154
8155
int SSL_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
8156
63.4M
{
8157
63.4M
    SSL_CONNECTION *sc;
8158
8159
63.4M
#ifndef OPENSSL_NO_QUIC
8160
63.4M
    if (IS_QUIC(s))
8161
63.4M
        return ossl_quic_get_event_timeout(s, tv, is_infinite);
8162
0
#endif
8163
8164
0
    sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
8165
0
    if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
8166
0
        && DTLSv1_get_timeout(s, tv)) {
8167
0
        *is_infinite = 0;
8168
0
        return 1;
8169
0
    }
8170
8171
0
    tv->tv_sec = 1000000;
8172
0
    tv->tv_usec = 0;
8173
0
    *is_infinite = 1;
8174
0
    return 1;
8175
0
}
8176
8177
int SSL_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
8178
0
{
8179
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
8180
8181
0
#ifndef OPENSSL_NO_QUIC
8182
0
    if (IS_QUIC(s))
8183
0
        return ossl_quic_get_rpoll_descriptor(s, desc);
8184
0
#endif
8185
8186
0
    if (sc == NULL || sc->rbio == NULL)
8187
0
        return 0;
8188
8189
0
    return BIO_get_rpoll_descriptor(sc->rbio, desc);
8190
0
}
8191
8192
int SSL_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
8193
0
{
8194
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
8195
8196
0
#ifndef OPENSSL_NO_QUIC
8197
0
    if (IS_QUIC(s))
8198
0
        return ossl_quic_get_wpoll_descriptor(s, desc);
8199
0
#endif
8200
8201
0
    if (sc == NULL || sc->wbio == NULL)
8202
0
        return 0;
8203
8204
0
    return BIO_get_wpoll_descriptor(sc->wbio, desc);
8205
0
}
8206
8207
int SSL_net_read_desired(SSL *s)
8208
0
{
8209
0
#ifndef OPENSSL_NO_QUIC
8210
0
    if (!IS_QUIC(s))
8211
0
        return SSL_want_read(s);
8212
8213
0
    return ossl_quic_get_net_read_desired(s);
8214
#else
8215
    return SSL_want_read(s);
8216
#endif
8217
0
}
8218
8219
int SSL_net_write_desired(SSL *s)
8220
0
{
8221
0
#ifndef OPENSSL_NO_QUIC
8222
0
    if (!IS_QUIC(s))
8223
0
        return SSL_want_write(s);
8224
8225
0
    return ossl_quic_get_net_write_desired(s);
8226
#else
8227
    return SSL_want_write(s);
8228
#endif
8229
0
}
8230
8231
int SSL_set_blocking_mode(SSL *s, int blocking)
8232
0
{
8233
0
#ifndef OPENSSL_NO_QUIC
8234
0
    if (IS_QUIC(s))
8235
0
        return ossl_quic_conn_set_blocking_mode(s, blocking);
8236
0
#endif
8237
8238
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8239
0
    if (IS_DTLS(s))
8240
0
        return ossl_dtls_set_blocking_mode(s, blocking);
8241
0
#endif
8242
8243
0
    return 0;
8244
0
}
8245
8246
int SSL_get_blocking_mode(SSL *s)
8247
0
{
8248
0
#ifndef OPENSSL_NO_QUIC
8249
0
    if (IS_QUIC(s))
8250
0
        return ossl_quic_conn_get_blocking_mode(s);
8251
0
#endif
8252
8253
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8254
0
    if (IS_DTLS(s))
8255
0
        return ossl_dtls_get_blocking_mode(s);
8256
0
#endif
8257
8258
0
    return -1;
8259
0
}
8260
8261
int SSL_set1_initial_peer_addr(SSL *s, const BIO_ADDR *peer_addr)
8262
16.0k
{
8263
16.0k
#ifndef OPENSSL_NO_QUIC
8264
16.0k
    if (IS_QUIC(s))
8265
16.0k
        return ossl_quic_conn_set_initial_peer_addr(s, peer_addr);
8266
0
#endif
8267
8268
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8269
    /* Handle DTLS connections */
8270
0
    if (IS_DTLS(s)) {
8271
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
8272
8273
0
        if (sc == NULL || sc->d1 == NULL)
8274
0
            return 0;
8275
8276
0
        if (peer_addr != NULL) {
8277
0
            if (!BIO_ADDR_copy(&sc->d1->peer_addr, peer_addr))
8278
0
                return 0;
8279
0
        } else {
8280
0
            BIO_ADDR_clear(&sc->d1->peer_addr);
8281
0
        }
8282
8283
        /*
8284
         * Update the record layers' peer address if they exist.
8285
         * This is needed for listener-created connections where the record
8286
         * layer is created before the peer address is set.
8287
         */
8288
0
        if (sc->rlayer.wrlmethod != NULL && sc->rlayer.wrl != NULL)
8289
0
            sc->rlayer.wrlmethod->set1_peer(sc->rlayer.wrl, peer_addr);
8290
0
        if (sc->rlayer.rrlmethod != NULL && sc->rlayer.rrl != NULL)
8291
0
            sc->rlayer.rrlmethod->set1_peer(sc->rlayer.rrl, peer_addr);
8292
8293
0
        return 1;
8294
0
    }
8295
0
#endif
8296
8297
0
    return 0;
8298
0
}
8299
8300
int SSL_shutdown_ex(SSL *ssl, uint64_t flags,
8301
    const SSL_SHUTDOWN_EX_ARGS *args,
8302
    size_t args_len)
8303
0
{
8304
0
#ifndef OPENSSL_NO_QUIC
8305
0
    if (!IS_QUIC(ssl))
8306
0
        return SSL_shutdown(ssl);
8307
8308
0
    return ossl_quic_conn_shutdown(ssl, flags, args, args_len);
8309
#else
8310
    return SSL_shutdown(ssl);
8311
#endif
8312
0
}
8313
8314
int SSL_stream_conclude(SSL *ssl, uint64_t flags)
8315
0
{
8316
0
#ifndef OPENSSL_NO_QUIC
8317
0
    if (!IS_QUIC(ssl))
8318
0
        return 0;
8319
8320
0
    return ossl_quic_conn_stream_conclude(ssl);
8321
#else
8322
    return 0;
8323
#endif
8324
0
}
8325
8326
SSL *SSL_new_stream(SSL *s, uint64_t flags)
8327
11.6k
{
8328
11.6k
#ifndef OPENSSL_NO_QUIC
8329
11.6k
    if (!IS_QUIC(s))
8330
0
        return NULL;
8331
8332
11.6k
    return ossl_quic_conn_stream_new(s, flags);
8333
#else
8334
    return NULL;
8335
#endif
8336
11.6k
}
8337
8338
SSL *SSL_get0_connection(SSL *s)
8339
0
{
8340
0
#ifndef OPENSSL_NO_QUIC
8341
0
    if (!IS_QUIC(s))
8342
0
        return s;
8343
8344
0
    return ossl_quic_get0_connection(s);
8345
#else
8346
    return s;
8347
#endif
8348
0
}
8349
8350
int SSL_is_connection(SSL *s)
8351
0
{
8352
0
    return SSL_get0_connection(s) == s;
8353
0
}
8354
8355
SSL *SSL_get0_listener(SSL *s)
8356
0
{
8357
0
#ifndef OPENSSL_NO_QUIC
8358
0
    if (IS_QUIC(s))
8359
0
        return ossl_quic_get0_listener(s);
8360
0
#endif
8361
8362
0
#ifndef OPENSSL_NO_DTLS
8363
0
    if (IS_DTLS(s))
8364
0
        return ossl_dtls_get0_listener(s);
8365
0
#endif
8366
8367
0
    return NULL;
8368
0
}
8369
8370
SSL *SSL_get0_domain(SSL *s)
8371
0
{
8372
0
#ifndef OPENSSL_NO_QUIC
8373
0
    if (!IS_QUIC(s))
8374
0
        return NULL;
8375
8376
0
    return ossl_quic_get0_domain(s);
8377
#else
8378
    return NULL;
8379
#endif
8380
0
}
8381
8382
int SSL_is_listener(SSL *s)
8383
0
{
8384
0
    return SSL_get0_listener(s) == s;
8385
0
}
8386
8387
int SSL_is_domain(SSL *s)
8388
0
{
8389
0
    return SSL_get0_domain(s) == s;
8390
0
}
8391
8392
int SSL_get_stream_type(SSL *s)
8393
0
{
8394
0
#ifndef OPENSSL_NO_QUIC
8395
0
    if (!IS_QUIC(s))
8396
0
        return SSL_STREAM_TYPE_BIDI;
8397
8398
0
    return ossl_quic_get_stream_type(s);
8399
#else
8400
    return SSL_STREAM_TYPE_BIDI;
8401
#endif
8402
0
}
8403
8404
uint64_t SSL_get_stream_id(SSL *s)
8405
0
{
8406
0
#ifndef OPENSSL_NO_QUIC
8407
0
    if (!IS_QUIC(s))
8408
0
        return UINT64_MAX;
8409
8410
0
    return ossl_quic_get_stream_id(s);
8411
#else
8412
    return UINT64_MAX;
8413
#endif
8414
0
}
8415
8416
int SSL_is_stream_local(SSL *s)
8417
0
{
8418
0
#ifndef OPENSSL_NO_QUIC
8419
0
    if (!IS_QUIC(s))
8420
0
        return -1;
8421
8422
0
    return ossl_quic_is_stream_local(s);
8423
#else
8424
    return -1;
8425
#endif
8426
0
}
8427
8428
int SSL_set_default_stream_mode(SSL *s, uint32_t mode)
8429
0
{
8430
0
#ifndef OPENSSL_NO_QUIC
8431
0
    if (!IS_QUIC(s))
8432
0
        return 0;
8433
8434
0
    return ossl_quic_set_default_stream_mode(s, mode);
8435
#else
8436
    return 0;
8437
#endif
8438
0
}
8439
8440
int SSL_set_incoming_stream_policy(SSL *s, int policy, uint64_t aec)
8441
42.8k
{
8442
42.8k
#ifndef OPENSSL_NO_QUIC
8443
42.8k
    if (!IS_QUIC(s))
8444
0
        return 0;
8445
8446
42.8k
    return ossl_quic_set_incoming_stream_policy(s, policy, aec);
8447
#else
8448
    return 0;
8449
#endif
8450
42.8k
}
8451
8452
SSL *SSL_accept_stream(SSL *s, uint64_t flags)
8453
507
{
8454
507
#ifndef OPENSSL_NO_QUIC
8455
507
    if (!IS_QUIC(s))
8456
0
        return NULL;
8457
8458
507
    return ossl_quic_accept_stream(s, flags);
8459
#else
8460
    return NULL;
8461
#endif
8462
507
}
8463
8464
size_t SSL_get_accept_stream_queue_len(SSL *s)
8465
12.4k
{
8466
12.4k
#ifndef OPENSSL_NO_QUIC
8467
12.4k
    if (!IS_QUIC(s))
8468
0
        return 0;
8469
8470
12.4k
    return ossl_quic_get_accept_stream_queue_len(s);
8471
#else
8472
    return 0;
8473
#endif
8474
12.4k
}
8475
8476
int SSL_stream_reset(SSL *s,
8477
    const SSL_STREAM_RESET_ARGS *args,
8478
    size_t args_len)
8479
0
{
8480
0
#ifndef OPENSSL_NO_QUIC
8481
0
    if (!IS_QUIC(s))
8482
0
        return 0;
8483
8484
0
    return ossl_quic_stream_reset(s, args, args_len);
8485
#else
8486
    return 0;
8487
#endif
8488
0
}
8489
8490
int SSL_get_stream_read_state(SSL *s)
8491
0
{
8492
0
#ifndef OPENSSL_NO_QUIC
8493
0
    if (!IS_QUIC(s))
8494
0
        return SSL_STREAM_STATE_NONE;
8495
8496
0
    return ossl_quic_get_stream_read_state(s);
8497
#else
8498
    return SSL_STREAM_STATE_NONE;
8499
#endif
8500
0
}
8501
8502
int SSL_get_stream_write_state(SSL *s)
8503
0
{
8504
0
#ifndef OPENSSL_NO_QUIC
8505
0
    if (!IS_QUIC(s))
8506
0
        return SSL_STREAM_STATE_NONE;
8507
8508
0
    return ossl_quic_get_stream_write_state(s);
8509
#else
8510
    return SSL_STREAM_STATE_NONE;
8511
#endif
8512
0
}
8513
8514
int SSL_get_stream_read_error_code(SSL *s, uint64_t *app_error_code)
8515
0
{
8516
0
#ifndef OPENSSL_NO_QUIC
8517
0
    if (!IS_QUIC(s))
8518
0
        return -1;
8519
8520
0
    return ossl_quic_get_stream_read_error_code(s, app_error_code);
8521
#else
8522
    return -1;
8523
#endif
8524
0
}
8525
8526
int SSL_get_stream_write_error_code(SSL *s, uint64_t *app_error_code)
8527
0
{
8528
0
#ifndef OPENSSL_NO_QUIC
8529
0
    if (!IS_QUIC(s))
8530
0
        return -1;
8531
8532
0
    return ossl_quic_get_stream_write_error_code(s, app_error_code);
8533
#else
8534
    return -1;
8535
#endif
8536
0
}
8537
8538
int SSL_get_conn_close_info(SSL *s, SSL_CONN_CLOSE_INFO *info,
8539
    size_t info_len)
8540
0
{
8541
0
#ifndef OPENSSL_NO_QUIC
8542
0
    if (!IS_QUIC(s))
8543
0
        return -1;
8544
8545
0
    return ossl_quic_get_conn_close_info(s, info, info_len);
8546
#else
8547
    return -1;
8548
#endif
8549
0
}
8550
8551
int SSL_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
8552
    uint64_t *value)
8553
0
{
8554
0
#ifndef OPENSSL_NO_QUIC
8555
0
    if (IS_QUIC(s))
8556
0
        return ossl_quic_get_value_uint(s, class_, id, value);
8557
0
#endif
8558
8559
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8560
0
    if (IS_DTLS_LISTENER(s))
8561
0
        return ossl_dtls_get_value_uint(s, class_, id, value);
8562
0
#endif
8563
8564
0
    ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_PROTOCOL);
8565
0
    return 0;
8566
0
}
8567
8568
int SSL_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
8569
    uint64_t value)
8570
0
{
8571
0
#ifndef OPENSSL_NO_QUIC
8572
0
    if (IS_QUIC(s))
8573
0
        return ossl_quic_set_value_uint(s, class_, id, value);
8574
0
#endif
8575
8576
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8577
0
    if (IS_DTLS_LISTENER(s))
8578
0
        return ossl_dtls_set_value_uint(s, class_, id, value);
8579
0
#endif
8580
8581
0
    ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_PROTOCOL);
8582
0
    return 0;
8583
0
}
8584
8585
SSL *SSL_new_listener(SSL_CTX *ctx, uint64_t flags)
8586
158
{
8587
158
    if (ctx == NULL) {
8588
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
8589
0
        return NULL;
8590
0
    }
8591
8592
158
#ifndef OPENSSL_NO_QUIC
8593
158
    if (IS_QUIC_CTX(ctx))
8594
158
        return ossl_quic_new_listener(ctx, flags);
8595
0
#endif
8596
8597
0
#ifndef OPENSSL_NO_DTLS
8598
0
    if (SSL_CTX_IS_DTLS(ctx))
8599
0
        return ossl_dtls_new_listener(ctx, flags);
8600
0
#endif
8601
0
    return NULL;
8602
0
}
8603
8604
SSL *SSL_new_listener_from(SSL *ssl, uint64_t flags)
8605
0
{
8606
0
#ifndef OPENSSL_NO_QUIC
8607
0
    if (!IS_QUIC(ssl))
8608
0
        return NULL;
8609
0
    return ossl_quic_new_listener_from(ssl, flags);
8610
#else
8611
    return NULL;
8612
#endif
8613
0
}
8614
8615
SSL *SSL_new_from_listener(SSL *ssl, uint64_t flags)
8616
0
{
8617
0
#ifndef OPENSSL_NO_QUIC
8618
0
    if (!IS_QUIC(ssl))
8619
0
        return NULL;
8620
0
    return ossl_quic_new_from_listener(ssl, flags);
8621
#else
8622
    return NULL;
8623
#endif
8624
0
}
8625
8626
SSL *SSL_accept_connection(SSL *ssl, uint64_t flags)
8627
158
{
8628
158
#ifndef OPENSSL_NO_QUIC
8629
158
    if (IS_QUIC(ssl))
8630
158
        return ossl_quic_accept_connection(ssl, flags);
8631
0
#endif
8632
8633
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8634
0
    if (IS_DTLS(ssl))
8635
0
        return ossl_dtls_accept_connection(ssl, flags);
8636
0
#endif
8637
8638
0
    return NULL;
8639
0
}
8640
8641
size_t SSL_get_accept_connection_queue_len(SSL *ssl)
8642
0
{
8643
0
#ifndef OPENSSL_NO_QUIC
8644
0
    if (IS_QUIC(ssl))
8645
0
        return ossl_quic_get_accept_connection_queue_len(ssl);
8646
0
#endif
8647
8648
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8649
0
    if (IS_DTLS(ssl))
8650
0
        return ossl_dtls_get_accept_connection_queue_len(ssl);
8651
0
#endif
8652
8653
0
    return 0;
8654
0
}
8655
8656
int SSL_get_peer_addr(SSL *ssl, BIO_ADDR *peer_addr)
8657
0
{
8658
0
#ifndef OPENSSL_NO_QUIC
8659
0
    if (IS_QUIC(ssl))
8660
0
        return ossl_quic_get_peer_addr(ssl, peer_addr);
8661
0
#endif
8662
8663
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8664
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
8665
8666
0
    if (sc != NULL && sc->d1 != NULL
8667
0
        && BIO_ADDR_family(&sc->d1->peer_addr) != AF_UNSPEC) {
8668
0
        if (!BIO_ADDR_copy(peer_addr, &sc->d1->peer_addr))
8669
0
            return 0;
8670
0
        return 1;
8671
0
    }
8672
0
#endif
8673
8674
0
    return 0;
8675
0
}
8676
8677
int SSL_listen_ex(SSL *listener, SSL *new_conn)
8678
0
{
8679
0
#ifndef OPENSSL_NO_QUIC
8680
0
    if (IS_QUIC(listener) && IS_QUIC(new_conn))
8681
0
        return ossl_quic_peeloff_conn(listener, new_conn);
8682
0
    else
8683
0
#endif
8684
0
        ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,
8685
0
            "SSL_listen_ex only operates on QUIC SSL objects");
8686
0
    return -1;
8687
0
}
8688
8689
int SSL_listen(SSL *ssl)
8690
0
{
8691
0
#ifndef OPENSSL_NO_QUIC
8692
0
    if (IS_QUIC(ssl))
8693
0
        return ossl_quic_listen(ssl);
8694
0
#endif
8695
8696
0
#if !defined(OPENSSL_NO_DTLS) && !defined(OPENSSL_NO_SOCK)
8697
0
    if (IS_DTLS(ssl))
8698
0
        return ossl_dtls_listen(ssl);
8699
0
#endif
8700
8701
0
    return 0;
8702
0
}
8703
8704
SSL *SSL_new_domain(SSL_CTX *ctx, uint64_t flags)
8705
0
{
8706
0
#ifndef OPENSSL_NO_QUIC
8707
0
    if (!IS_QUIC_CTX(ctx))
8708
0
        return NULL;
8709
8710
0
    return ossl_quic_new_domain(ctx, flags);
8711
#else
8712
    return NULL;
8713
#endif
8714
0
}
8715
8716
int ossl_adjust_domain_flags(uint64_t domain_flags, uint64_t *p_domain_flags)
8717
0
{
8718
0
    if ((domain_flags & ~OSSL_QUIC_SUPPORTED_DOMAIN_FLAGS) != 0) {
8719
0
        ERR_raise_data(ERR_LIB_SSL, ERR_R_UNSUPPORTED,
8720
0
            "unsupported domain flag requested");
8721
0
        return 0;
8722
0
    }
8723
8724
0
    if ((domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0)
8725
0
        domain_flags |= SSL_DOMAIN_FLAG_MULTI_THREAD;
8726
8727
0
    if ((domain_flags & (SSL_DOMAIN_FLAG_MULTI_THREAD | SSL_DOMAIN_FLAG_SINGLE_THREAD)) == 0)
8728
0
        domain_flags |= SSL_DOMAIN_FLAG_MULTI_THREAD;
8729
8730
0
    if ((domain_flags & SSL_DOMAIN_FLAG_SINGLE_THREAD) != 0
8731
0
        && (domain_flags & SSL_DOMAIN_FLAG_MULTI_THREAD) != 0) {
8732
0
        ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT,
8733
0
            "mutually exclusive domain flags specified");
8734
0
        return 0;
8735
0
    }
8736
8737
    /*
8738
     * Note: We treat MULTI_THREAD as a no-op in non-threaded builds, but
8739
     * not THREAD_ASSISTED.
8740
     */
8741
#ifndef OPENSSL_THREADS
8742
    if ((domain_flags & SSL_DOMAIN_FLAG_THREAD_ASSISTED) != 0) {
8743
        ERR_raise_data(ERR_LIB_SSL, ERR_R_UNSUPPORTED,
8744
            "thread assisted mode not available in this build");
8745
        return 0;
8746
    }
8747
#endif
8748
8749
0
    *p_domain_flags = domain_flags;
8750
0
    return 1;
8751
0
}
8752
8753
int SSL_CTX_set_domain_flags(SSL_CTX *ctx, uint64_t domain_flags)
8754
0
{
8755
0
#ifndef OPENSSL_NO_QUIC
8756
0
    if (IS_QUIC_CTX(ctx)) {
8757
0
        if (!ossl_adjust_domain_flags(domain_flags, &domain_flags))
8758
0
            return 0;
8759
8760
0
        ctx->domain_flags = domain_flags;
8761
0
        return 1;
8762
0
    }
8763
0
#endif
8764
8765
0
    ERR_raise_data(ERR_LIB_SSL, ERR_R_UNSUPPORTED,
8766
0
        "domain flags unsupported on this kind of SSL_CTX");
8767
0
    return 0;
8768
0
}
8769
8770
int SSL_CTX_get_domain_flags(const SSL_CTX *ctx, uint64_t *domain_flags)
8771
0
{
8772
0
#ifndef OPENSSL_NO_QUIC
8773
0
    if (IS_QUIC_CTX(ctx)) {
8774
0
        if (domain_flags != NULL)
8775
0
            *domain_flags = ctx->domain_flags;
8776
8777
0
        return 1;
8778
0
    }
8779
0
#endif
8780
8781
0
    ERR_raise_data(ERR_LIB_SSL, ERR_R_UNSUPPORTED,
8782
0
        "domain flags unsupported on this kind of SSL_CTX");
8783
0
    return 0;
8784
0
}
8785
8786
int SSL_get_domain_flags(const SSL *ssl, uint64_t *domain_flags)
8787
0
{
8788
0
#ifndef OPENSSL_NO_QUIC
8789
0
    if (IS_QUIC(ssl))
8790
0
        return ossl_quic_get_domain_flags(ssl, domain_flags);
8791
0
#endif
8792
8793
0
    return 0;
8794
0
}
8795
8796
int SSL_add_expected_rpk(SSL *s, EVP_PKEY *rpk)
8797
0
{
8798
0
    unsigned char *data = NULL;
8799
0
    SSL_DANE *dane = SSL_get0_dane(s);
8800
0
    int ret;
8801
8802
0
    if (dane == NULL || dane->dctx == NULL)
8803
0
        return 0;
8804
0
    if ((ret = i2d_PUBKEY(rpk, &data)) <= 0)
8805
0
        return 0;
8806
8807
0
    ret = SSL_dane_tlsa_add(s, DANETLS_USAGE_DANE_EE,
8808
0
              DANETLS_SELECTOR_SPKI,
8809
0
              DANETLS_MATCHING_FULL,
8810
0
              data, (size_t)ret)
8811
0
        > 0;
8812
0
    OPENSSL_free(data);
8813
0
    return ret;
8814
0
}
8815
8816
EVP_PKEY *SSL_get0_peer_rpk(const SSL *s)
8817
0
{
8818
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
8819
8820
0
    if (sc == NULL || sc->session == NULL)
8821
0
        return NULL;
8822
0
    return sc->session->peer_rpk;
8823
0
}
8824
8825
int SSL_get_negotiated_client_cert_type(const SSL *s)
8826
0
{
8827
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
8828
8829
0
    if (sc == NULL)
8830
0
        return 0;
8831
8832
0
    return sc->ext.client_cert_type;
8833
0
}
8834
8835
int SSL_get_negotiated_server_cert_type(const SSL *s)
8836
0
{
8837
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
8838
8839
0
    if (sc == NULL)
8840
0
        return 0;
8841
8842
0
    return sc->ext.server_cert_type;
8843
0
}
8844
8845
static int validate_cert_type(const unsigned char *val, size_t len)
8846
0
{
8847
0
    size_t i;
8848
0
    int saw_rpk = 0;
8849
0
    int saw_x509 = 0;
8850
8851
0
    if (val == NULL && len == 0)
8852
0
        return 1;
8853
8854
0
    if (val == NULL || len == 0)
8855
0
        return 0;
8856
8857
0
    for (i = 0; i < len; i++) {
8858
0
        switch (val[i]) {
8859
0
        case TLSEXT_cert_type_rpk:
8860
0
            if (saw_rpk)
8861
0
                return 0;
8862
0
            saw_rpk = 1;
8863
0
            break;
8864
0
        case TLSEXT_cert_type_x509:
8865
0
            if (saw_x509)
8866
0
                return 0;
8867
0
            saw_x509 = 1;
8868
0
            break;
8869
0
        case TLSEXT_cert_type_pgp:
8870
0
        case TLSEXT_cert_type_1609dot2:
8871
0
        default:
8872
0
            return 0;
8873
0
        }
8874
0
    }
8875
0
    return 1;
8876
0
}
8877
8878
static int set_cert_type(unsigned char **cert_type,
8879
    size_t *cert_type_len,
8880
    const unsigned char *val,
8881
    size_t len)
8882
0
{
8883
0
    unsigned char *tmp = NULL;
8884
8885
0
    if (!validate_cert_type(val, len))
8886
0
        return 0;
8887
8888
0
    if (val != NULL && (tmp = OPENSSL_memdup(val, len)) == NULL)
8889
0
        return 0;
8890
8891
0
    OPENSSL_free(*cert_type);
8892
0
    *cert_type = tmp;
8893
0
    *cert_type_len = len;
8894
0
    return 1;
8895
0
}
8896
8897
int SSL_set1_client_cert_type(SSL *s, const unsigned char *val, size_t len)
8898
0
{
8899
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
8900
8901
0
    if (sc == NULL)
8902
0
        return 0;
8903
8904
0
    return set_cert_type(&sc->client_cert_type, &sc->client_cert_type_len,
8905
0
        val, len);
8906
0
}
8907
8908
int SSL_set1_server_cert_type(SSL *s, const unsigned char *val, size_t len)
8909
0
{
8910
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
8911
8912
0
    if (sc == NULL)
8913
0
        return 0;
8914
8915
0
    return set_cert_type(&sc->server_cert_type, &sc->server_cert_type_len,
8916
0
        val, len);
8917
0
}
8918
8919
int SSL_CTX_set1_client_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
8920
0
{
8921
0
    return set_cert_type(&ctx->client_cert_type, &ctx->client_cert_type_len,
8922
0
        val, len);
8923
0
}
8924
8925
int SSL_CTX_set1_server_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
8926
0
{
8927
0
    return set_cert_type(&ctx->server_cert_type, &ctx->server_cert_type_len,
8928
0
        val, len);
8929
0
}
8930
8931
int SSL_get0_client_cert_type(const SSL *s, unsigned char **t, size_t *len)
8932
0
{
8933
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
8934
8935
0
    if (t == NULL || len == NULL || sc == NULL)
8936
0
        return 0;
8937
8938
0
    *t = sc->client_cert_type;
8939
0
    *len = sc->client_cert_type_len;
8940
0
    return 1;
8941
0
}
8942
8943
int SSL_get0_server_cert_type(const SSL *s, unsigned char **t, size_t *len)
8944
0
{
8945
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
8946
8947
0
    if (t == NULL || len == NULL || sc == NULL)
8948
0
        return 0;
8949
8950
0
    *t = sc->server_cert_type;
8951
0
    *len = sc->server_cert_type_len;
8952
0
    return 1;
8953
0
}
8954
8955
int SSL_CTX_get0_client_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
8956
0
{
8957
0
    if (t == NULL || len == NULL)
8958
0
        return 0;
8959
8960
0
    *t = ctx->client_cert_type;
8961
0
    *len = ctx->client_cert_type_len;
8962
0
    return 1;
8963
0
}
8964
8965
int SSL_CTX_get0_server_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
8966
0
{
8967
0
    if (t == NULL || len == NULL)
8968
0
        return 0;
8969
8970
0
    *t = ctx->server_cert_type;
8971
0
    *len = ctx->server_cert_type_len;
8972
0
    return 1;
8973
0
}
8974
8975
/*
8976
 * RFC 8701 GREASE - returns a GREASE value (0x?A?A pattern) for the given
8977
 * index.  Seeds are generated lazily on first use and remain stable for the
8978
 * lifetime of the connection so that HelloRetryRequest replays get identical
8979
 * values.
8980
 */
8981
uint16_t ossl_grease_value(SSL_CONNECTION *s, int index)
8982
0
{
8983
0
    uint16_t ret;
8984
8985
0
    if (index < 0 || index > OSSL_GREASE_LAST_INDEX)
8986
0
        return 0x0A0A;
8987
8988
0
    if (!s->ext.grease_seeded) {
8989
0
        if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
8990
0
                s->ext.grease_seed,
8991
0
                sizeof(s->ext.grease_seed), 0)
8992
0
            <= 0)
8993
0
            memset(s->ext.grease_seed, 0x42, sizeof(s->ext.grease_seed));
8994
0
        s->ext.grease_seeded = 1;
8995
0
    }
8996
8997
    /* Map seed byte to 0x?A?A pattern */
8998
0
    ret = (s->ext.grease_seed[index] & 0xf0) | 0x0a;
8999
0
    ret |= ret << 8;
9000
9001
    /* Ensure EXT2 differs from EXT1 */
9002
0
    if (index == OSSL_GREASE_EXT2
9003
0
        && ret == ossl_grease_value(s, OSSL_GREASE_EXT1))
9004
0
        ret ^= 0x1010;
9005
9006
0
    return ret;
9007
0
}