Coverage Report

Created: 2026-04-29 06:57

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