Coverage Report

Created: 2025-06-13 06:58

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