Coverage Report

Created: 2026-11-16 07:12

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