Coverage Report

Created: 2025-10-28 06:56

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