Coverage Report

Created: 2026-08-31 06:56

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