Coverage Report

Created: 2025-11-16 06:40

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