Coverage Report

Created: 2025-12-31 06:58

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