Coverage Report

Created: 2025-11-16 06:40

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