Coverage Report

Created: 2025-12-31 06:58

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