Coverage Report

Created: 2026-09-12 06:55

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