Coverage Report

Created: 2026-10-16 07:31

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