Coverage Report

Created: 2025-11-16 06:40

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