Coverage Report

Created: 2025-08-28 07:07

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