Coverage Report

Created: 2026-08-12 07:21

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