Coverage Report

Created: 2025-11-24 06:26

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