Coverage Report

Created: 2025-11-16 06:40

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