Coverage Report

Created: 2026-10-28 06:28

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