Coverage Report

Created: 2025-06-13 06:57

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