Coverage Report

Created: 2025-12-04 06:33

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