Coverage Report

Created: 2026-08-18 07:24

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