Coverage Report

Created: 2025-08-28 07:07

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