Coverage Report

Created: 2026-10-28 06:28

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