Coverage Report

Created: 2025-12-31 06:58

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