Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/ssl/ssl_lib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include <stdio.h>
13
#include "ssl_local.h"
14
#include "e_os.h"
15
#include <openssl/objects.h>
16
#include <openssl/x509v3.h>
17
#include <openssl/rand.h>
18
#include <openssl/ocsp.h>
19
#include <openssl/dh.h>
20
#include <openssl/engine.h>
21
#include <openssl/async.h>
22
#include <openssl/ct.h>
23
#include <openssl/trace.h>
24
#include "internal/cryptlib.h"
25
#include "internal/refcount.h"
26
#include "internal/ktls.h"
27
28
static int ssl_undefined_function_1(SSL *ssl, SSL3_RECORD *r, size_t s, int t,
29
    SSL_MAC_BUF *mac, size_t macsize)
30
0
{
31
0
    return ssl_undefined_function(ssl);
32
0
}
33
34
static int ssl_undefined_function_2(SSL *ssl, SSL3_RECORD *r, unsigned char *s,
35
    int t)
36
0
{
37
0
    return ssl_undefined_function(ssl);
38
0
}
39
40
static int ssl_undefined_function_3(SSL *ssl, unsigned char *r,
41
    unsigned char *s, size_t t, size_t *u)
42
0
{
43
0
    return ssl_undefined_function(ssl);
44
0
}
45
46
static int ssl_undefined_function_4(SSL *ssl, int r)
47
0
{
48
0
    return ssl_undefined_function(ssl);
49
0
}
50
51
static size_t ssl_undefined_function_5(SSL *ssl, const char *r, size_t s,
52
    unsigned char *t)
53
0
{
54
0
    return ssl_undefined_function(ssl);
55
0
}
56
57
static int ssl_undefined_function_6(int r)
58
0
{
59
0
    return ssl_undefined_function(NULL);
60
0
}
61
62
static int ssl_undefined_function_7(SSL *ssl, unsigned char *r, size_t s,
63
    const char *t, size_t u,
64
    const unsigned char *v, size_t w, int x)
65
0
{
66
0
    return ssl_undefined_function(ssl);
67
0
}
68
69
SSL3_ENC_METHOD ssl3_undef_enc_method = {
70
    ssl_undefined_function_1,
71
    ssl_undefined_function_2,
72
    ssl_undefined_function,
73
    ssl_undefined_function_3,
74
    ssl_undefined_function_4,
75
    ssl_undefined_function_5,
76
    NULL, /* client_finished_label */
77
    0, /* client_finished_label_len */
78
    NULL, /* server_finished_label */
79
    0, /* server_finished_label_len */
80
    ssl_undefined_function_6,
81
    ssl_undefined_function_7,
82
};
83
84
struct ssl_async_args {
85
    SSL *s;
86
    void *buf;
87
    size_t num;
88
    enum { READFUNC,
89
        WRITEFUNC,
90
        OTHERFUNC } type;
91
    union {
92
        int (*func_read)(SSL *, void *, size_t, size_t *);
93
        int (*func_write)(SSL *, const void *, size_t, size_t *);
94
        int (*func_other)(SSL *);
95
    } f;
96
};
97
98
static const struct {
99
    uint8_t mtype;
100
    uint8_t ord;
101
    int nid;
102
} dane_mds[] = {
103
    { DANETLS_MATCHING_FULL, 0, NID_undef },
104
    { DANETLS_MATCHING_2256, 1, NID_sha256 },
105
    { DANETLS_MATCHING_2512, 2, NID_sha512 },
106
};
107
108
static int dane_ctx_enable(struct dane_ctx_st *dctx)
109
0
{
110
0
    const EVP_MD **mdevp;
111
0
    uint8_t *mdord;
112
0
    uint8_t mdmax = DANETLS_MATCHING_LAST;
113
0
    int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
114
0
    size_t i;
115
116
0
    if (dctx->mdevp != NULL)
117
0
        return 1;
118
119
0
    mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
120
0
    mdord = OPENSSL_zalloc(n * sizeof(*mdord));
121
122
0
    if (mdord == NULL || mdevp == NULL) {
123
0
        OPENSSL_free(mdord);
124
0
        OPENSSL_free(mdevp);
125
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
126
0
        return 0;
127
0
    }
128
129
    /* Install default entries */
130
0
    for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
131
0
        const EVP_MD *md;
132
133
0
        if (dane_mds[i].nid == NID_undef || (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
134
0
            continue;
135
0
        mdevp[dane_mds[i].mtype] = md;
136
0
        mdord[dane_mds[i].mtype] = dane_mds[i].ord;
137
0
    }
138
139
0
    dctx->mdevp = mdevp;
140
0
    dctx->mdord = mdord;
141
0
    dctx->mdmax = mdmax;
142
143
0
    return 1;
144
0
}
145
146
static void dane_ctx_final(struct dane_ctx_st *dctx)
147
163k
{
148
163k
    OPENSSL_free(dctx->mdevp);
149
163k
    dctx->mdevp = NULL;
150
151
163k
    OPENSSL_free(dctx->mdord);
152
163k
    dctx->mdord = NULL;
153
163k
    dctx->mdmax = 0;
154
163k
}
155
156
static void tlsa_free(danetls_record *t)
157
0
{
158
0
    if (t == NULL)
159
0
        return;
160
0
    OPENSSL_free(t->data);
161
0
    EVP_PKEY_free(t->spki);
162
0
    OPENSSL_free(t);
163
0
}
164
165
static void dane_final(SSL_DANE *dane)
166
163k
{
167
163k
    sk_danetls_record_pop_free(dane->trecs, tlsa_free);
168
163k
    dane->trecs = NULL;
169
170
163k
    sk_X509_pop_free(dane->certs, X509_free);
171
163k
    dane->certs = NULL;
172
173
163k
    X509_free(dane->mcert);
174
163k
    dane->mcert = NULL;
175
163k
    dane->mtlsa = NULL;
176
163k
    dane->mdpth = -1;
177
163k
    dane->pdpth = -1;
178
163k
}
179
180
/*
181
 * dane_copy - Copy dane configuration, sans verification state.
182
 */
183
static int ssl_dane_dup(SSL *to, SSL *from)
184
0
{
185
0
    int num;
186
0
    int i;
187
188
0
    if (!DANETLS_ENABLED(&from->dane))
189
0
        return 1;
190
191
0
    num = sk_danetls_record_num(from->dane.trecs);
192
0
    dane_final(&to->dane);
193
0
    to->dane.flags = from->dane.flags;
194
0
    to->dane.dctx = &to->ctx->dane;
195
0
    to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
196
197
0
    if (to->dane.trecs == NULL) {
198
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
199
0
        return 0;
200
0
    }
201
202
0
    for (i = 0; i < num; ++i) {
203
0
        danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
204
205
0
        if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
206
0
                t->data, t->dlen)
207
0
            <= 0)
208
0
            return 0;
209
0
    }
210
0
    return 1;
211
0
}
212
213
static int dane_mtype_set(struct dane_ctx_st *dctx,
214
    const EVP_MD *md, uint8_t mtype, uint8_t ord)
215
0
{
216
0
    int i;
217
218
0
    if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
219
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
220
0
        return 0;
221
0
    }
222
223
0
    if (mtype > dctx->mdmax) {
224
0
        const EVP_MD **mdevp;
225
0
        uint8_t *mdord;
226
0
        int n = ((int)mtype) + 1;
227
228
0
        mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
229
0
        if (mdevp == NULL) {
230
0
            ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
231
0
            return -1;
232
0
        }
233
0
        dctx->mdevp = mdevp;
234
235
0
        mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
236
0
        if (mdord == NULL) {
237
0
            ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
238
0
            return -1;
239
0
        }
240
0
        dctx->mdord = mdord;
241
242
        /* Zero-fill any gaps */
243
0
        for (i = dctx->mdmax + 1; i < mtype; ++i) {
244
0
            mdevp[i] = NULL;
245
0
            mdord[i] = 0;
246
0
        }
247
248
0
        dctx->mdmax = mtype;
249
0
    }
250
251
0
    dctx->mdevp[mtype] = md;
252
    /* Coerce ordinal of disabled matching types to 0 */
253
0
    dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
254
255
0
    return 1;
256
0
}
257
258
static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
259
0
{
260
0
    if (mtype > dane->dctx->mdmax)
261
0
        return NULL;
262
0
    return dane->dctx->mdevp[mtype];
263
0
}
264
265
static int dane_tlsa_add(SSL_DANE *dane,
266
    uint8_t usage,
267
    uint8_t selector,
268
    uint8_t mtype, const unsigned char *data, size_t dlen)
269
0
{
270
0
    danetls_record *t;
271
0
    const EVP_MD *md = NULL;
272
0
    int ilen = (int)dlen;
273
0
    int i;
274
0
    int num;
275
276
0
    if (dane->trecs == NULL) {
277
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
278
0
        return -1;
279
0
    }
280
281
0
    if (ilen < 0 || dlen != (size_t)ilen) {
282
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
283
0
        return 0;
284
0
    }
285
286
0
    if (usage > DANETLS_USAGE_LAST) {
287
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
288
0
        return 0;
289
0
    }
290
291
0
    if (selector > DANETLS_SELECTOR_LAST) {
292
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
293
0
        return 0;
294
0
    }
295
296
0
    if (mtype != DANETLS_MATCHING_FULL) {
297
0
        md = tlsa_md_get(dane, mtype);
298
0
        if (md == NULL) {
299
0
            ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
300
0
            return 0;
301
0
        }
302
0
    }
303
304
0
    if (md != NULL && dlen != (size_t)EVP_MD_get_size(md)) {
305
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
306
0
        return 0;
307
0
    }
308
0
    if (!data) {
309
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
310
0
        return 0;
311
0
    }
312
313
0
    if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
314
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
315
0
        return -1;
316
0
    }
317
318
0
    t->usage = usage;
319
0
    t->selector = selector;
320
0
    t->mtype = mtype;
321
0
    t->data = OPENSSL_malloc(dlen);
322
0
    if (t->data == NULL) {
323
0
        tlsa_free(t);
324
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
325
0
        return -1;
326
0
    }
327
0
    memcpy(t->data, data, dlen);
328
0
    t->dlen = dlen;
329
330
    /* Validate and cache full certificate or public key */
331
0
    if (mtype == DANETLS_MATCHING_FULL) {
332
0
        const unsigned char *p = data;
333
0
        X509 *cert = NULL;
334
0
        EVP_PKEY *pkey = NULL;
335
336
0
        switch (selector) {
337
0
        case DANETLS_SELECTOR_CERT:
338
0
            if (!d2i_X509(&cert, &p, ilen) || p < data || dlen != (size_t)(p - data)) {
339
0
                X509_free(cert);
340
0
                tlsa_free(t);
341
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
342
0
                return 0;
343
0
            }
344
0
            if (X509_get0_pubkey(cert) == NULL) {
345
0
                X509_free(cert);
346
0
                tlsa_free(t);
347
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
348
0
                return 0;
349
0
            }
350
351
0
            if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
352
                /*
353
                 * The Full(0) certificate decodes to a seemingly valid X.509
354
                 * object with a plausible key, so the TLSA record is well
355
                 * formed.  However, we don't actually need the certifiate for
356
                 * usages PKIX-EE(1) or DANE-EE(3), because at least the EE
357
                 * certificate is always presented by the peer.  We discard the
358
                 * certificate, and just use the TLSA data as an opaque blob
359
                 * for matching the raw presented DER octets.
360
                 *
361
                 * DO NOT FREE `t` here, it will be added to the TLSA record
362
                 * list below!
363
                 */
364
0
                X509_free(cert);
365
0
                break;
366
0
            }
367
368
            /*
369
             * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
370
             * records that contain full certificates of trust-anchors that are
371
             * not present in the wire chain.  For usage PKIX-TA(0), we augment
372
             * the chain with untrusted Full(0) certificates from DNS, in case
373
             * they are missing from the chain.
374
             */
375
0
            if ((dane->certs == NULL && (dane->certs = sk_X509_new_null()) == NULL) || !sk_X509_push(dane->certs, cert)) {
376
0
                ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
377
0
                X509_free(cert);
378
0
                tlsa_free(t);
379
0
                return -1;
380
0
            }
381
0
            break;
382
383
0
        case DANETLS_SELECTOR_SPKI:
384
0
            if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data || dlen != (size_t)(p - data)) {
385
0
                EVP_PKEY_free(pkey);
386
0
                tlsa_free(t);
387
0
                ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
388
0
                return 0;
389
0
            }
390
391
            /*
392
             * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
393
             * records that contain full bare keys of trust-anchors that are
394
             * not present in the wire chain.
395
             */
396
0
            if (usage == DANETLS_USAGE_DANE_TA)
397
0
                t->spki = pkey;
398
0
            else
399
0
                EVP_PKEY_free(pkey);
400
0
            break;
401
0
        }
402
0
    }
403
404
    /*-
405
     * Find the right insertion point for the new record.
406
     *
407
     * See crypto/x509/x509_vfy.c.  We sort DANE-EE(3) records first, so that
408
     * they can be processed first, as they require no chain building, and no
409
     * expiration or hostname checks.  Because DANE-EE(3) is numerically
410
     * largest, this is accomplished via descending sort by "usage".
411
     *
412
     * We also sort in descending order by matching ordinal to simplify
413
     * the implementation of digest agility in the verification code.
414
     *
415
     * The choice of order for the selector is not significant, so we
416
     * use the same descending order for consistency.
417
     */
418
0
    num = sk_danetls_record_num(dane->trecs);
419
0
    for (i = 0; i < num; ++i) {
420
0
        danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
421
422
0
        if (rec->usage > usage)
423
0
            continue;
424
0
        if (rec->usage < usage)
425
0
            break;
426
0
        if (rec->selector > selector)
427
0
            continue;
428
0
        if (rec->selector < selector)
429
0
            break;
430
0
        if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
431
0
            continue;
432
0
        break;
433
0
    }
434
435
0
    if (!sk_danetls_record_insert(dane->trecs, t, i)) {
436
0
        tlsa_free(t);
437
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
438
0
        return -1;
439
0
    }
440
0
    dane->umask |= DANETLS_USAGE_BIT(usage);
441
442
0
    return 1;
443
0
}
444
445
/*
446
 * Return 0 if there is only one version configured and it was disabled
447
 * at configure time.  Return 1 otherwise.
448
 */
449
static int ssl_check_allowed_versions(int min_version, int max_version)
450
133k
{
451
133k
    int minisdtls = 0, maxisdtls = 0;
452
453
    /* Figure out if we're doing DTLS versions or TLS versions */
454
133k
    if (min_version == DTLS1_BAD_VER
455
133k
        || min_version >> 8 == DTLS1_VERSION_MAJOR)
456
0
        minisdtls = 1;
457
133k
    if (max_version == DTLS1_BAD_VER
458
133k
        || max_version >> 8 == DTLS1_VERSION_MAJOR)
459
0
        maxisdtls = 1;
460
    /* A wildcard version of 0 could be DTLS or TLS. */
461
133k
    if ((minisdtls && !maxisdtls && max_version != 0)
462
133k
        || (maxisdtls && !minisdtls && min_version != 0)) {
463
        /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
464
0
        return 0;
465
0
    }
466
467
133k
    if (minisdtls || maxisdtls) {
468
        /* Do DTLS version checks. */
469
0
        if (min_version == 0)
470
            /* Ignore DTLS1_BAD_VER */
471
0
            min_version = DTLS1_VERSION;
472
0
        if (max_version == 0)
473
0
            max_version = DTLS1_2_VERSION;
474
#ifdef OPENSSL_NO_DTLS1_2
475
        if (max_version == DTLS1_2_VERSION)
476
            max_version = DTLS1_VERSION;
477
#endif
478
#ifdef OPENSSL_NO_DTLS1
479
        if (min_version == DTLS1_VERSION)
480
            min_version = DTLS1_2_VERSION;
481
#endif
482
        /* Done massaging versions; do the check. */
483
0
        if (0
484
#ifdef OPENSSL_NO_DTLS1
485
            || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
486
                && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
487
#endif
488
#ifdef OPENSSL_NO_DTLS1_2
489
            || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
490
                && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
491
#endif
492
0
        )
493
0
            return 0;
494
133k
    } else {
495
        /* Regular TLS version checks. */
496
133k
        if (min_version == 0)
497
93.3k
            min_version = SSL3_VERSION;
498
133k
        if (max_version == 0)
499
133k
            max_version = TLS1_3_VERSION;
500
#ifdef OPENSSL_NO_TLS1_3
501
        if (max_version == TLS1_3_VERSION)
502
            max_version = TLS1_2_VERSION;
503
#endif
504
#ifdef OPENSSL_NO_TLS1_2
505
        if (max_version == TLS1_2_VERSION)
506
            max_version = TLS1_1_VERSION;
507
#endif
508
#ifdef OPENSSL_NO_TLS1_1
509
        if (max_version == TLS1_1_VERSION)
510
            max_version = TLS1_VERSION;
511
#endif
512
#ifdef OPENSSL_NO_TLS1
513
        if (max_version == TLS1_VERSION)
514
            max_version = SSL3_VERSION;
515
#endif
516
133k
#ifdef OPENSSL_NO_SSL3
517
133k
        if (min_version == SSL3_VERSION)
518
93.3k
            min_version = TLS1_VERSION;
519
133k
#endif
520
#ifdef OPENSSL_NO_TLS1
521
        if (min_version == TLS1_VERSION)
522
            min_version = TLS1_1_VERSION;
523
#endif
524
#ifdef OPENSSL_NO_TLS1_1
525
        if (min_version == TLS1_1_VERSION)
526
            min_version = TLS1_2_VERSION;
527
#endif
528
#ifdef OPENSSL_NO_TLS1_2
529
        if (min_version == TLS1_2_VERSION)
530
            min_version = TLS1_3_VERSION;
531
#endif
532
        /* Done massaging versions; do the check. */
533
133k
        if (0
534
133k
#ifdef OPENSSL_NO_SSL3
535
133k
            || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
536
133k
#endif
537
#ifdef OPENSSL_NO_TLS1
538
            || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
539
#endif
540
#ifdef OPENSSL_NO_TLS1_1
541
            || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
542
#endif
543
#ifdef OPENSSL_NO_TLS1_2
544
            || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
545
#endif
546
#ifdef OPENSSL_NO_TLS1_3
547
            || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
548
#endif
549
133k
        )
550
0
            return 0;
551
133k
    }
552
133k
    return 1;
553
133k
}
554
555
#if defined(__TANDEM) && defined(OPENSSL_VPROC)
556
/*
557
 * Define a VPROC function for HP NonStop build ssl library.
558
 * This is used by platform version identification tools.
559
 * Do not inline this procedure or make it static.
560
 */
561
#define OPENSSL_VPROC_STRING_(x) x##_SSL
562
#define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
563
#define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
564
void OPENSSL_VPROC_FUNC(void) { }
565
#endif
566
567
static void clear_ciphers(SSL *s)
568
45.8k
{
569
    /* clear the current cipher */
570
45.8k
    ssl_clear_cipher_ctx(s);
571
45.8k
    ssl_clear_hash_ctx(&s->read_hash);
572
45.8k
    ssl_clear_hash_ctx(&s->write_hash);
573
45.8k
}
574
575
int SSL_clear(SSL *s)
576
22.9k
{
577
22.9k
    if (s->method == NULL) {
578
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
579
0
        return 0;
580
0
    }
581
582
22.9k
    if (ssl_clear_bad_session(s)) {
583
0
        SSL_SESSION_free(s->session);
584
0
        s->session = NULL;
585
0
    }
586
22.9k
    SSL_SESSION_free(s->psksession);
587
22.9k
    s->psksession = NULL;
588
22.9k
    OPENSSL_free(s->psksession_id);
589
22.9k
    s->psksession_id = NULL;
590
22.9k
    s->psksession_id_len = 0;
591
22.9k
    s->hello_retry_request = SSL_HRR_NONE;
592
22.9k
    s->sent_tickets = 0;
593
594
22.9k
    s->error = 0;
595
22.9k
    s->hit = 0;
596
22.9k
    s->shutdown = 0;
597
598
22.9k
    if (s->renegotiate) {
599
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
600
0
        return 0;
601
0
    }
602
603
22.9k
    ossl_statem_clear(s);
604
605
22.9k
    s->version = s->method->version;
606
22.9k
    s->client_version = s->version;
607
22.9k
    s->rwstate = SSL_NOTHING;
608
609
22.9k
    BUF_MEM_free(s->init_buf);
610
22.9k
    s->init_buf = NULL;
611
22.9k
    clear_ciphers(s);
612
22.9k
    s->first_packet = 0;
613
614
22.9k
    s->key_update = SSL_KEY_UPDATE_NONE;
615
616
22.9k
    EVP_MD_CTX_free(s->pha_dgst);
617
22.9k
    s->pha_dgst = NULL;
618
619
    /* Reset DANE verification result state */
620
22.9k
    s->dane.mdpth = -1;
621
22.9k
    s->dane.pdpth = -1;
622
22.9k
    X509_free(s->dane.mcert);
623
22.9k
    s->dane.mcert = NULL;
624
22.9k
    s->dane.mtlsa = NULL;
625
626
    /* Clear the verification result peername */
627
22.9k
    X509_VERIFY_PARAM_move_peername(s->param, NULL);
628
629
    /* Clear any shared connection state */
630
22.9k
    OPENSSL_free(s->shared_sigalgs);
631
22.9k
    s->shared_sigalgs = NULL;
632
22.9k
    s->shared_sigalgslen = 0;
633
634
    /*
635
     * Check to see if we were changed into a different method, if so, revert
636
     * back.
637
     */
638
22.9k
    if (s->method != s->ctx->method) {
639
0
        s->method->ssl_free(s);
640
0
        s->method = s->ctx->method;
641
0
        if (!s->method->ssl_new(s))
642
0
            return 0;
643
22.9k
    } else {
644
22.9k
        if (!s->method->ssl_clear(s))
645
0
            return 0;
646
22.9k
    }
647
648
22.9k
    RECORD_LAYER_clear(&s->rlayer);
649
650
22.9k
    return 1;
651
22.9k
}
652
653
#ifndef OPENSSL_NO_DEPRECATED_3_0
654
/** Used to change an SSL_CTXs default SSL method type */
655
int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
656
0
{
657
0
    STACK_OF(SSL_CIPHER) *sk;
658
659
0
    ctx->method = meth;
660
661
0
    if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
662
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
663
0
        return 0;
664
0
    }
665
0
    sk = ssl_create_cipher_list(ctx,
666
0
        ctx->tls13_ciphersuites,
667
0
        &(ctx->cipher_list),
668
0
        &(ctx->cipher_list_by_id),
669
0
        OSSL_default_cipher_list(), ctx->cert);
670
0
    if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
671
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
672
0
        return 0;
673
0
    }
674
0
    return 1;
675
0
}
676
#endif
677
678
SSL *SSL_new(SSL_CTX *ctx)
679
11.4k
{
680
11.4k
    SSL *s;
681
682
11.4k
    if (ctx == NULL) {
683
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
684
0
        return NULL;
685
0
    }
686
11.4k
    if (ctx->method == NULL) {
687
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
688
0
        return NULL;
689
0
    }
690
691
11.4k
    s = OPENSSL_zalloc(sizeof(*s));
692
11.4k
    if (s == NULL)
693
0
        goto err;
694
695
11.4k
    s->references = 1;
696
11.4k
    s->lock = CRYPTO_THREAD_lock_new();
697
11.4k
    if (s->lock == NULL) {
698
0
        OPENSSL_free(s);
699
0
        s = NULL;
700
0
        goto err;
701
0
    }
702
703
11.4k
    RECORD_LAYER_init(&s->rlayer, s);
704
705
11.4k
    s->options = ctx->options;
706
11.4k
    s->dane.flags = ctx->dane.flags;
707
11.4k
    s->min_proto_version = ctx->min_proto_version;
708
11.4k
    s->max_proto_version = ctx->max_proto_version;
709
11.4k
    s->mode = ctx->mode;
710
11.4k
    s->max_cert_list = ctx->max_cert_list;
711
11.4k
    s->max_early_data = ctx->max_early_data;
712
11.4k
    s->recv_max_early_data = ctx->recv_max_early_data;
713
11.4k
    s->num_tickets = ctx->num_tickets;
714
11.4k
    s->pha_enabled = ctx->pha_enabled;
715
716
    /* Shallow copy of the ciphersuites stack */
717
11.4k
    s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
718
11.4k
    if (s->tls13_ciphersuites == NULL)
719
0
        goto err;
720
721
    /*
722
     * Earlier library versions used to copy the pointer to the CERT, not
723
     * its contents; only when setting new parameters for the per-SSL
724
     * copy, ssl_cert_new would be called (and the direct reference to
725
     * the per-SSL_CTX settings would be lost, but those still were
726
     * indirectly accessed for various purposes, and for that reason they
727
     * used to be known as s->ctx->default_cert). Now we don't look at the
728
     * SSL_CTX's CERT after having duplicated it once.
729
     */
730
11.4k
    s->cert = ssl_cert_dup(ctx->cert);
731
11.4k
    if (s->cert == NULL)
732
0
        goto err;
733
734
11.4k
    RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
735
11.4k
    s->msg_callback = ctx->msg_callback;
736
11.4k
    s->msg_callback_arg = ctx->msg_callback_arg;
737
11.4k
    s->verify_mode = ctx->verify_mode;
738
11.4k
    s->not_resumable_session_cb = ctx->not_resumable_session_cb;
739
11.4k
    s->record_padding_cb = ctx->record_padding_cb;
740
11.4k
    s->record_padding_arg = ctx->record_padding_arg;
741
11.4k
    s->block_padding = ctx->block_padding;
742
11.4k
    s->sid_ctx_length = ctx->sid_ctx_length;
743
11.4k
    if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
744
0
        goto err;
745
11.4k
    memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
746
11.4k
    s->verify_callback = ctx->default_verify_callback;
747
11.4k
    s->generate_session_id = ctx->generate_session_id;
748
749
11.4k
    s->param = X509_VERIFY_PARAM_new();
750
11.4k
    if (s->param == NULL)
751
0
        goto err;
752
11.4k
    X509_VERIFY_PARAM_inherit(s->param, ctx->param);
753
11.4k
    s->quiet_shutdown = ctx->quiet_shutdown;
754
755
11.4k
    s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
756
11.4k
    s->max_send_fragment = ctx->max_send_fragment;
757
11.4k
    s->split_send_fragment = ctx->split_send_fragment;
758
11.4k
    s->max_pipelines = ctx->max_pipelines;
759
11.4k
    if (s->max_pipelines > 1)
760
0
        RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
761
11.4k
    if (ctx->default_read_buf_len > 0)
762
0
        SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
763
764
11.4k
    SSL_CTX_up_ref(ctx);
765
11.4k
    s->ctx = ctx;
766
11.4k
    s->ext.debug_cb = 0;
767
11.4k
    s->ext.debug_arg = NULL;
768
11.4k
    s->ext.ticket_expected = 0;
769
11.4k
    s->ext.status_type = ctx->ext.status_type;
770
11.4k
    s->ext.status_expected = 0;
771
11.4k
    s->ext.ocsp.ids = NULL;
772
11.4k
    s->ext.ocsp.exts = NULL;
773
11.4k
    s->ext.ocsp.resp = NULL;
774
11.4k
    s->ext.ocsp.resp_len = 0;
775
11.4k
    SSL_CTX_up_ref(ctx);
776
11.4k
    s->session_ctx = ctx;
777
11.4k
    if (ctx->ext.ecpointformats) {
778
0
        s->ext.ecpointformats = OPENSSL_memdup(ctx->ext.ecpointformats,
779
0
            ctx->ext.ecpointformats_len);
780
0
        if (!s->ext.ecpointformats) {
781
0
            s->ext.ecpointformats_len = 0;
782
0
            goto err;
783
0
        }
784
0
        s->ext.ecpointformats_len = ctx->ext.ecpointformats_len;
785
0
    }
786
11.4k
    if (ctx->ext.supportedgroups) {
787
0
        s->ext.supportedgroups = OPENSSL_memdup(ctx->ext.supportedgroups,
788
0
            ctx->ext.supportedgroups_len
789
0
                * sizeof(*ctx->ext.supportedgroups));
790
0
        if (!s->ext.supportedgroups) {
791
0
            s->ext.supportedgroups_len = 0;
792
0
            goto err;
793
0
        }
794
0
        s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
795
0
    }
796
797
11.4k
#ifndef OPENSSL_NO_NEXTPROTONEG
798
11.4k
    s->ext.npn = NULL;
799
11.4k
#endif
800
801
11.4k
    if (s->ctx->ext.alpn) {
802
0
        s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
803
0
        if (s->ext.alpn == NULL) {
804
0
            s->ext.alpn_len = 0;
805
0
            goto err;
806
0
        }
807
0
        memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
808
0
        s->ext.alpn_len = s->ctx->ext.alpn_len;
809
0
    }
810
811
11.4k
    s->verified_chain = NULL;
812
11.4k
    s->verify_result = X509_V_OK;
813
814
11.4k
    s->default_passwd_callback = ctx->default_passwd_callback;
815
11.4k
    s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
816
817
11.4k
    s->method = ctx->method;
818
819
11.4k
    s->key_update = SSL_KEY_UPDATE_NONE;
820
821
11.4k
    s->allow_early_data_cb = ctx->allow_early_data_cb;
822
11.4k
    s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
823
824
11.4k
    if (!s->method->ssl_new(s))
825
0
        goto err;
826
827
11.4k
    s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
828
829
11.4k
    if (!SSL_clear(s))
830
0
        goto err;
831
832
11.4k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
833
0
        goto err;
834
835
11.4k
#ifndef OPENSSL_NO_PSK
836
11.4k
    s->psk_client_callback = ctx->psk_client_callback;
837
11.4k
    s->psk_server_callback = ctx->psk_server_callback;
838
11.4k
#endif
839
11.4k
    s->psk_find_session_cb = ctx->psk_find_session_cb;
840
11.4k
    s->psk_use_session_cb = ctx->psk_use_session_cb;
841
842
11.4k
    s->async_cb = ctx->async_cb;
843
11.4k
    s->async_cb_arg = ctx->async_cb_arg;
844
845
11.4k
    s->job = NULL;
846
847
11.4k
#ifndef OPENSSL_NO_CT
848
11.4k
    if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
849
11.4k
            ctx->ct_validation_callback_arg))
850
0
        goto err;
851
11.4k
#endif
852
853
11.4k
    return s;
854
0
err:
855
0
    SSL_free(s);
856
0
    ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
857
0
    return NULL;
858
11.4k
}
859
860
int SSL_is_dtls(const SSL *s)
861
{
862
    return SSL_IS_DTLS(s) ? 1 : 0;
863
}
864
865
int SSL_up_ref(SSL *s)
866
12.4k
{
867
12.4k
    int i;
868
869
12.4k
    if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0)
870
0
        return 0;
871
872
12.4k
    REF_PRINT_COUNT("SSL", s);
873
12.4k
    REF_ASSERT_ISNT(i < 2);
874
12.4k
    return ((i > 1) ? 1 : 0);
875
12.4k
}
876
877
int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
878
    unsigned int sid_ctx_len)
879
0
{
880
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
881
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
882
0
        return 0;
883
0
    }
884
0
    ctx->sid_ctx_length = sid_ctx_len;
885
0
    memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
886
887
0
    return 1;
888
0
}
889
890
int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
891
    unsigned int sid_ctx_len)
892
0
{
893
0
    if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
894
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
895
0
        return 0;
896
0
    }
897
0
    ssl->sid_ctx_length = sid_ctx_len;
898
0
    memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
899
900
0
    return 1;
901
0
}
902
903
int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
904
0
{
905
0
    if (!CRYPTO_THREAD_write_lock(ctx->lock))
906
0
        return 0;
907
0
    ctx->generate_session_id = cb;
908
0
    CRYPTO_THREAD_unlock(ctx->lock);
909
0
    return 1;
910
0
}
911
912
int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
913
0
{
914
0
    if (!CRYPTO_THREAD_write_lock(ssl->lock))
915
0
        return 0;
916
0
    ssl->generate_session_id = cb;
917
0
    CRYPTO_THREAD_unlock(ssl->lock);
918
0
    return 1;
919
0
}
920
921
int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
922
    unsigned int id_len)
923
5.55k
{
924
    /*
925
     * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
926
     * we can "construct" a session to give us the desired check - i.e. to
927
     * find if there's a session in the hash table that would conflict with
928
     * any new session built out of this id/id_len and the ssl_version in use
929
     * by this SSL.
930
     */
931
5.55k
    SSL_SESSION r, *p;
932
933
5.55k
    if (id_len > sizeof(r.session_id))
934
0
        return 0;
935
936
5.55k
    r.ssl_version = ssl->version;
937
5.55k
    r.session_id_length = id_len;
938
5.55k
    memcpy(r.session_id, id, id_len);
939
940
5.55k
    if (!CRYPTO_THREAD_read_lock(ssl->session_ctx->lock))
941
0
        return 0;
942
5.55k
    p = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &r);
943
5.55k
    CRYPTO_THREAD_unlock(ssl->session_ctx->lock);
944
5.55k
    return (p != NULL);
945
5.55k
}
946
947
int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
948
0
{
949
0
    return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
950
0
}
951
952
int SSL_set_purpose(SSL *s, int purpose)
953
0
{
954
0
    return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
955
0
}
956
957
int SSL_CTX_set_trust(SSL_CTX *s, int trust)
958
0
{
959
0
    return X509_VERIFY_PARAM_set_trust(s->param, trust);
960
0
}
961
962
int SSL_set_trust(SSL *s, int trust)
963
0
{
964
0
    return X509_VERIFY_PARAM_set_trust(s->param, trust);
965
0
}
966
967
int SSL_set1_host(SSL *s, const char *hostname)
968
0
{
969
    /* If a hostname is provided and parses as an IP address,
970
     * treat it as such. */
971
0
    if (hostname && X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname) == 1)
972
0
        return 1;
973
974
0
    return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
975
0
}
976
977
int SSL_add1_host(SSL *s, const char *hostname)
978
0
{
979
    /* If a hostname is provided and parses as an IP address,
980
     * treat it as such. */
981
0
    if (hostname) {
982
0
        ASN1_OCTET_STRING *ip;
983
0
        char *old_ip;
984
985
0
        ip = a2i_IPADDRESS(hostname);
986
0
        if (ip) {
987
            /* We didn't want it; only to check if it *is* an IP address */
988
0
            ASN1_OCTET_STRING_free(ip);
989
990
0
            old_ip = X509_VERIFY_PARAM_get1_ip_asc(s->param);
991
0
            if (old_ip) {
992
0
                OPENSSL_free(old_ip);
993
                /* There can be only one IP address */
994
0
                return 0;
995
0
            }
996
997
0
            return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
998
0
        }
999
0
    }
1000
1001
0
    return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
1002
0
}
1003
1004
void SSL_set_hostflags(SSL *s, unsigned int flags)
1005
0
{
1006
0
    X509_VERIFY_PARAM_set_hostflags(s->param, flags);
1007
0
}
1008
1009
const char *SSL_get0_peername(SSL *s)
1010
0
{
1011
0
    return X509_VERIFY_PARAM_get0_peername(s->param);
1012
0
}
1013
1014
int SSL_CTX_dane_enable(SSL_CTX *ctx)
1015
0
{
1016
0
    return dane_ctx_enable(&ctx->dane);
1017
0
}
1018
1019
unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
1020
0
{
1021
0
    unsigned long orig = ctx->dane.flags;
1022
1023
0
    ctx->dane.flags |= flags;
1024
0
    return orig;
1025
0
}
1026
1027
unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
1028
0
{
1029
0
    unsigned long orig = ctx->dane.flags;
1030
1031
0
    ctx->dane.flags &= ~flags;
1032
0
    return orig;
1033
0
}
1034
1035
int SSL_dane_enable(SSL *s, const char *basedomain)
1036
0
{
1037
0
    SSL_DANE *dane = &s->dane;
1038
1039
0
    if (s->ctx->dane.mdmax == 0) {
1040
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
1041
0
        return 0;
1042
0
    }
1043
0
    if (dane->trecs != NULL) {
1044
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
1045
0
        return 0;
1046
0
    }
1047
1048
    /*
1049
     * Default SNI name.  This rejects empty names, while set1_host below
1050
     * accepts them and disables host name checks.  To avoid side-effects with
1051
     * invalid input, set the SNI name first.
1052
     */
1053
0
    if (s->ext.hostname == NULL) {
1054
0
        if (!SSL_set_tlsext_host_name(s, basedomain)) {
1055
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1056
0
            return -1;
1057
0
        }
1058
0
    }
1059
1060
    /* Primary RFC6125 reference identifier */
1061
0
    if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
1062
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1063
0
        return -1;
1064
0
    }
1065
1066
0
    dane->mdpth = -1;
1067
0
    dane->pdpth = -1;
1068
0
    dane->dctx = &s->ctx->dane;
1069
0
    dane->trecs = sk_danetls_record_new_null();
1070
1071
0
    if (dane->trecs == NULL) {
1072
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1073
0
        return -1;
1074
0
    }
1075
0
    return 1;
1076
0
}
1077
1078
unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
1079
0
{
1080
0
    unsigned long orig = ssl->dane.flags;
1081
1082
0
    ssl->dane.flags |= flags;
1083
0
    return orig;
1084
0
}
1085
1086
unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
1087
0
{
1088
0
    unsigned long orig = ssl->dane.flags;
1089
1090
0
    ssl->dane.flags &= ~flags;
1091
0
    return orig;
1092
0
}
1093
1094
int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
1095
0
{
1096
0
    SSL_DANE *dane = &s->dane;
1097
1098
0
    if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
1099
0
        return -1;
1100
0
    if (dane->mtlsa) {
1101
0
        if (mcert)
1102
0
            *mcert = dane->mcert;
1103
0
        if (mspki)
1104
0
            *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
1105
0
    }
1106
0
    return dane->mdpth;
1107
0
}
1108
1109
int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
1110
    uint8_t *mtype, const unsigned char **data, size_t *dlen)
1111
0
{
1112
0
    SSL_DANE *dane = &s->dane;
1113
1114
0
    if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
1115
0
        return -1;
1116
0
    if (dane->mtlsa) {
1117
0
        if (usage)
1118
0
            *usage = dane->mtlsa->usage;
1119
0
        if (selector)
1120
0
            *selector = dane->mtlsa->selector;
1121
0
        if (mtype)
1122
0
            *mtype = dane->mtlsa->mtype;
1123
0
        if (data)
1124
0
            *data = dane->mtlsa->data;
1125
0
        if (dlen)
1126
0
            *dlen = dane->mtlsa->dlen;
1127
0
    }
1128
0
    return dane->mdpth;
1129
0
}
1130
1131
SSL_DANE *SSL_get0_dane(SSL *s)
1132
0
{
1133
0
    return &s->dane;
1134
0
}
1135
1136
int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
1137
    uint8_t mtype, const unsigned char *data, size_t dlen)
1138
0
{
1139
0
    return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
1140
0
}
1141
1142
int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
1143
    uint8_t ord)
1144
0
{
1145
0
    return dane_mtype_set(&ctx->dane, md, mtype, ord);
1146
0
}
1147
1148
int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
1149
0
{
1150
0
    return X509_VERIFY_PARAM_set1(ctx->param, vpm);
1151
0
}
1152
1153
int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
1154
0
{
1155
0
    return X509_VERIFY_PARAM_set1(ssl->param, vpm);
1156
0
}
1157
1158
X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
1159
0
{
1160
0
    return ctx->param;
1161
0
}
1162
1163
X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
1164
0
{
1165
0
    return ssl->param;
1166
0
}
1167
1168
void SSL_certs_clear(SSL *s)
1169
0
{
1170
0
    ssl_cert_clear_certs(s->cert);
1171
0
}
1172
1173
void SSL_free(SSL *s)
1174
11.4k
{
1175
11.4k
    int i;
1176
1177
11.4k
    if (s == NULL)
1178
0
        return;
1179
11.4k
    CRYPTO_DOWN_REF(&s->references, &i, s->lock);
1180
11.4k
    REF_PRINT_COUNT("SSL", s);
1181
11.4k
    if (i > 0)
1182
0
        return;
1183
11.4k
    REF_ASSERT_ISNT(i < 0);
1184
1185
11.4k
    X509_VERIFY_PARAM_free(s->param);
1186
11.4k
    dane_final(&s->dane);
1187
11.4k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
1188
1189
11.4k
    RECORD_LAYER_release(&s->rlayer);
1190
1191
    /* Ignore return value */
1192
11.4k
    ssl_free_wbio_buffer(s);
1193
1194
11.4k
    BIO_free_all(s->wbio);
1195
11.4k
    s->wbio = NULL;
1196
11.4k
    BIO_free_all(s->rbio);
1197
11.4k
    s->rbio = NULL;
1198
1199
11.4k
    BUF_MEM_free(s->init_buf);
1200
1201
    /* add extra stuff */
1202
11.4k
    sk_SSL_CIPHER_free(s->cipher_list);
1203
11.4k
    sk_SSL_CIPHER_free(s->cipher_list_by_id);
1204
11.4k
    sk_SSL_CIPHER_free(s->tls13_ciphersuites);
1205
11.4k
    sk_SSL_CIPHER_free(s->peer_ciphers);
1206
1207
    /* Make the next call work :-) */
1208
11.4k
    if (s->session != NULL) {
1209
10.5k
        ssl_clear_bad_session(s);
1210
10.5k
        SSL_SESSION_free(s->session);
1211
10.5k
    }
1212
11.4k
    SSL_SESSION_free(s->psksession);
1213
11.4k
    OPENSSL_free(s->psksession_id);
1214
1215
11.4k
    ssl_cert_free(s->cert);
1216
11.4k
    OPENSSL_free(s->shared_sigalgs);
1217
    /* Free up if allocated */
1218
1219
11.4k
    OPENSSL_free(s->ext.hostname);
1220
11.4k
    SSL_CTX_free(s->session_ctx);
1221
11.4k
    OPENSSL_free(s->ext.ecpointformats);
1222
11.4k
    OPENSSL_free(s->ext.peer_ecpointformats);
1223
11.4k
    OPENSSL_free(s->ext.supportedgroups);
1224
11.4k
    OPENSSL_free(s->ext.peer_supportedgroups);
1225
11.4k
    sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
1226
11.4k
#ifndef OPENSSL_NO_OCSP
1227
11.4k
    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
1228
11.4k
#endif
1229
11.4k
#ifndef OPENSSL_NO_CT
1230
11.4k
    SCT_LIST_free(s->scts);
1231
11.4k
    OPENSSL_free(s->ext.scts);
1232
11.4k
#endif
1233
11.4k
    OPENSSL_free(s->ext.ocsp.resp);
1234
11.4k
    OPENSSL_free(s->ext.alpn);
1235
11.4k
    OPENSSL_free(s->ext.tls13_cookie);
1236
11.4k
    if (s->clienthello != NULL)
1237
0
        OPENSSL_free(s->clienthello->pre_proc_exts);
1238
11.4k
    OPENSSL_free(s->clienthello);
1239
11.4k
    OPENSSL_free(s->pha_context);
1240
11.4k
    EVP_MD_CTX_free(s->pha_dgst);
1241
1242
11.4k
    sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
1243
11.4k
    sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
1244
1245
11.4k
    sk_X509_pop_free(s->verified_chain, X509_free);
1246
1247
11.4k
    if (s->method != NULL)
1248
11.4k
        s->method->ssl_free(s);
1249
1250
    /*
1251
     * Must occur after s->method->ssl_free(). The DTLS sent_messages queue
1252
     * may reference the EVP_CIPHER_CTX/EVP_MD_CTX that are freed here.
1253
     */
1254
11.4k
    clear_ciphers(s);
1255
1256
11.4k
    SSL_CTX_free(s->ctx);
1257
1258
11.4k
    ASYNC_WAIT_CTX_free(s->waitctx);
1259
1260
11.4k
#if !defined(OPENSSL_NO_NEXTPROTONEG)
1261
11.4k
    OPENSSL_free(s->ext.npn);
1262
11.4k
#endif
1263
1264
11.4k
#ifndef OPENSSL_NO_SRTP
1265
11.4k
    sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
1266
11.4k
#endif
1267
1268
11.4k
    CRYPTO_THREAD_lock_free(s->lock);
1269
1270
11.4k
    OPENSSL_free(s);
1271
11.4k
}
1272
1273
void SSL_set0_rbio(SSL *s, BIO *rbio)
1274
11.4k
{
1275
11.4k
    BIO_free_all(s->rbio);
1276
11.4k
    s->rbio = rbio;
1277
11.4k
}
1278
1279
void SSL_set0_wbio(SSL *s, BIO *wbio)
1280
11.4k
{
1281
    /*
1282
     * If the output buffering BIO is still in place, remove it
1283
     */
1284
11.4k
    if (s->bbio != NULL)
1285
0
        s->wbio = BIO_pop(s->wbio);
1286
1287
11.4k
    BIO_free_all(s->wbio);
1288
11.4k
    s->wbio = wbio;
1289
1290
    /* Re-attach |bbio| to the new |wbio|. */
1291
11.4k
    if (s->bbio != NULL)
1292
0
        s->wbio = BIO_push(s->bbio, s->wbio);
1293
11.4k
}
1294
1295
void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
1296
93.8k
{
1297
    /*
1298
     * For historical reasons, this function has many different cases in
1299
     * ownership handling.
1300
     */
1301
1302
    /* If nothing has changed, do nothing */
1303
93.8k
    if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
1304
0
        return;
1305
1306
    /*
1307
     * If the two arguments are equal then one fewer reference is granted by the
1308
     * caller than we want to take
1309
     */
1310
93.8k
    if (rbio != NULL && rbio == wbio)
1311
20.9k
        BIO_up_ref(rbio);
1312
1313
    /*
1314
     * If only the wbio is changed only adopt one reference.
1315
     */
1316
93.8k
    if (rbio == SSL_get_rbio(s)) {
1317
0
        SSL_set0_wbio(s, wbio);
1318
0
        return;
1319
0
    }
1320
    /*
1321
     * There is an asymmetry here for historical reasons. If only the rbio is
1322
     * changed AND the rbio and wbio were originally different, then we only
1323
     * adopt one reference.
1324
     */
1325
93.8k
    if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
1326
0
        SSL_set0_rbio(s, rbio);
1327
0
        return;
1328
0
    }
1329
1330
    /* Otherwise, adopt both references. */
1331
93.8k
    SSL_set0_rbio(s, rbio);
1332
93.8k
    SSL_set0_wbio(s, wbio);
1333
93.8k
}
1334
1335
BIO *SSL_get_rbio(const SSL *s)
1336
22.9k
{
1337
22.9k
    return s->rbio;
1338
22.9k
}
1339
1340
BIO *SSL_get_wbio(const SSL *s)
1341
11.4k
{
1342
11.4k
    if (s->bbio != NULL) {
1343
        /*
1344
         * If |bbio| is active, the true caller-configured BIO is its
1345
         * |next_bio|.
1346
         */
1347
0
        return BIO_next(s->bbio);
1348
0
    }
1349
11.4k
    return s->wbio;
1350
11.4k
}
1351
1352
int SSL_get_fd(const SSL *s)
1353
0
{
1354
0
    return SSL_get_rfd(s);
1355
0
}
1356
1357
int SSL_get_rfd(const SSL *s)
1358
0
{
1359
0
    int ret = -1;
1360
0
    BIO *b, *r;
1361
1362
0
    b = SSL_get_rbio(s);
1363
0
    r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1364
0
    if (r != NULL)
1365
0
        BIO_get_fd(r, &ret);
1366
0
    return ret;
1367
0
}
1368
1369
int SSL_get_wfd(const SSL *s)
1370
0
{
1371
0
    int ret = -1;
1372
0
    BIO *b, *r;
1373
1374
0
    b = SSL_get_wbio(s);
1375
0
    r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1376
0
    if (r != NULL)
1377
0
        BIO_get_fd(r, &ret);
1378
0
    return ret;
1379
0
}
1380
1381
#ifndef OPENSSL_NO_SOCK
1382
int SSL_set_fd(SSL *s, int fd)
1383
0
{
1384
0
    int ret = 0;
1385
0
    BIO *bio = NULL;
1386
1387
0
    bio = BIO_new(BIO_s_socket());
1388
1389
0
    if (bio == NULL) {
1390
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1391
0
        goto err;
1392
0
    }
1393
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
1394
0
    SSL_set_bio(s, bio, bio);
1395
0
    ret = 1;
1396
0
err:
1397
0
    return ret;
1398
0
}
1399
1400
int SSL_set_wfd(SSL *s, int fd)
1401
0
{
1402
0
    BIO *rbio = SSL_get_rbio(s);
1403
1404
0
    if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET
1405
0
        || (int)BIO_get_fd(rbio, NULL) != fd) {
1406
0
        BIO *bio = BIO_new(BIO_s_socket());
1407
1408
0
        if (bio == NULL) {
1409
0
            ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1410
0
            return 0;
1411
0
        }
1412
0
        BIO_set_fd(bio, fd, BIO_NOCLOSE);
1413
0
        SSL_set0_wbio(s, bio);
1414
0
    } else {
1415
0
        BIO_up_ref(rbio);
1416
0
        SSL_set0_wbio(s, rbio);
1417
0
    }
1418
0
    return 1;
1419
0
}
1420
1421
int SSL_set_rfd(SSL *s, int fd)
1422
0
{
1423
0
    BIO *wbio = SSL_get_wbio(s);
1424
1425
0
    if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET
1426
0
        || ((int)BIO_get_fd(wbio, NULL) != fd)) {
1427
0
        BIO *bio = BIO_new(BIO_s_socket());
1428
1429
0
        if (bio == NULL) {
1430
0
            ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1431
0
            return 0;
1432
0
        }
1433
0
        BIO_set_fd(bio, fd, BIO_NOCLOSE);
1434
0
        SSL_set0_rbio(s, bio);
1435
0
    } else {
1436
0
        BIO_up_ref(wbio);
1437
0
        SSL_set0_rbio(s, wbio);
1438
0
    }
1439
1440
0
    return 1;
1441
0
}
1442
#endif
1443
1444
/* return length of latest Finished message we sent, copy to 'buf' */
1445
size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
1446
0
{
1447
0
    size_t ret = 0;
1448
1449
0
    ret = s->s3.tmp.finish_md_len;
1450
0
    if (count > ret)
1451
0
        count = ret;
1452
0
    memcpy(buf, s->s3.tmp.finish_md, count);
1453
0
    return ret;
1454
0
}
1455
1456
/* return length of latest Finished message we expected, copy to 'buf' */
1457
size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
1458
0
{
1459
0
    size_t ret = 0;
1460
1461
0
    ret = s->s3.tmp.peer_finish_md_len;
1462
0
    if (count > ret)
1463
0
        count = ret;
1464
0
    memcpy(buf, s->s3.tmp.peer_finish_md, count);
1465
0
    return ret;
1466
0
}
1467
1468
int SSL_get_verify_mode(const SSL *s)
1469
0
{
1470
0
    return s->verify_mode;
1471
0
}
1472
1473
int SSL_get_verify_depth(const SSL *s)
1474
0
{
1475
0
    return X509_VERIFY_PARAM_get_depth(s->param);
1476
0
}
1477
1478
int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
1479
0
{
1480
0
    return s->verify_callback;
1481
0
}
1482
1483
int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
1484
0
{
1485
0
    return ctx->verify_mode;
1486
0
}
1487
1488
int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
1489
0
{
1490
0
    return X509_VERIFY_PARAM_get_depth(ctx->param);
1491
0
}
1492
1493
int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
1494
0
{
1495
0
    return ctx->default_verify_callback;
1496
0
}
1497
1498
void SSL_set_verify(SSL *s, int mode,
1499
    int (*callback)(int ok, X509_STORE_CTX *ctx))
1500
0
{
1501
0
    s->verify_mode = mode;
1502
0
    if (callback != NULL)
1503
0
        s->verify_callback = callback;
1504
0
}
1505
1506
void SSL_set_verify_depth(SSL *s, int depth)
1507
0
{
1508
0
    X509_VERIFY_PARAM_set_depth(s->param, depth);
1509
0
}
1510
1511
void SSL_set_read_ahead(SSL *s, int yes)
1512
0
{
1513
0
    RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
1514
0
}
1515
1516
int SSL_get_read_ahead(const SSL *s)
1517
0
{
1518
0
    return RECORD_LAYER_get_read_ahead(&s->rlayer);
1519
0
}
1520
1521
int SSL_pending(const SSL *s)
1522
0
{
1523
0
    size_t pending = s->method->ssl_pending(s);
1524
1525
    /*
1526
     * SSL_pending cannot work properly if read-ahead is enabled
1527
     * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1528
     * impossible to fix since SSL_pending cannot report errors that may be
1529
     * observed while scanning the new data. (Note that SSL_pending() is
1530
     * often used as a boolean value, so we'd better not return -1.)
1531
     *
1532
     * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1533
     * we just return INT_MAX.
1534
     */
1535
0
    return pending < INT_MAX ? (int)pending : INT_MAX;
1536
0
}
1537
1538
int SSL_has_pending(const SSL *s)
1539
0
{
1540
    /*
1541
     * Similar to SSL_pending() but returns a 1 to indicate that we have
1542
     * processed or unprocessed data available or 0 otherwise (as opposed to the
1543
     * number of bytes available). Unlike SSL_pending() this will take into
1544
     * account read_ahead data. A 1 return simply indicates that we have data.
1545
     * That data may not result in any application data, or we may fail to parse
1546
     * the records for some reason.
1547
     */
1548
1549
    /* Check buffered app data if any first */
1550
0
    if (SSL_IS_DTLS(s)) {
1551
0
        DTLS1_RECORD_DATA *rdata;
1552
0
        pitem *item, *iter;
1553
1554
0
        iter = pqueue_iterator(s->rlayer.d->buffered_app_data.q);
1555
0
        while ((item = pqueue_next(&iter)) != NULL) {
1556
0
            rdata = item->data;
1557
0
            if (rdata->rrec.length > 0)
1558
0
                return 1;
1559
0
        }
1560
0
    }
1561
1562
0
    if (RECORD_LAYER_processed_read_pending(&s->rlayer))
1563
0
        return 1;
1564
1565
0
    return RECORD_LAYER_read_pending(&s->rlayer);
1566
0
}
1567
1568
X509 *SSL_get1_peer_certificate(const SSL *s)
1569
0
{
1570
0
    X509 *r = SSL_get0_peer_certificate(s);
1571
1572
0
    if (r != NULL)
1573
0
        X509_up_ref(r);
1574
1575
0
    return r;
1576
0
}
1577
1578
X509 *SSL_get0_peer_certificate(const SSL *s)
1579
0
{
1580
0
    if ((s == NULL) || (s->session == NULL))
1581
0
        return NULL;
1582
0
    else
1583
0
        return s->session->peer;
1584
0
}
1585
1586
STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
1587
0
{
1588
0
    STACK_OF(X509) *r;
1589
1590
0
    if ((s == NULL) || (s->session == NULL))
1591
0
        r = NULL;
1592
0
    else
1593
0
        r = s->session->peer_chain;
1594
1595
    /*
1596
     * If we are a client, cert_chain includes the peer's own certificate; if
1597
     * we are a server, it does not.
1598
     */
1599
1600
0
    return r;
1601
0
}
1602
1603
/*
1604
 * Now in theory, since the calling process own 't' it should be safe to
1605
 * modify.  We need to be able to read f without being hassled
1606
 */
1607
int SSL_copy_session_id(SSL *t, const SSL *f)
1608
0
{
1609
0
    int i;
1610
    /* Do we need to do SSL locking? */
1611
0
    if (!SSL_set_session(t, SSL_get_session(f))) {
1612
0
        return 0;
1613
0
    }
1614
1615
    /*
1616
     * what if we are setup for one protocol version but want to talk another
1617
     */
1618
0
    if (t->method != f->method) {
1619
0
        t->method->ssl_free(t);
1620
0
        t->method = f->method;
1621
0
        if (t->method->ssl_new(t) == 0)
1622
0
            return 0;
1623
0
    }
1624
1625
0
    CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
1626
0
    ssl_cert_free(t->cert);
1627
0
    t->cert = f->cert;
1628
0
    if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
1629
0
        return 0;
1630
0
    }
1631
1632
0
    return 1;
1633
0
}
1634
1635
/* Fix this so it checks all the valid key/cert options */
1636
int SSL_CTX_check_private_key(const SSL_CTX *ctx)
1637
0
{
1638
0
    if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
1639
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
1640
0
        return 0;
1641
0
    }
1642
0
    if (ctx->cert->key->privatekey == NULL) {
1643
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1644
0
        return 0;
1645
0
    }
1646
0
    return X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey);
1647
0
}
1648
1649
/* Fix this function so that it takes an optional type parameter */
1650
int SSL_check_private_key(const SSL *ssl)
1651
0
{
1652
0
    if (ssl == NULL) {
1653
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1654
0
        return 0;
1655
0
    }
1656
0
    if (ssl->cert->key->x509 == NULL) {
1657
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
1658
0
        return 0;
1659
0
    }
1660
0
    if (ssl->cert->key->privatekey == NULL) {
1661
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1662
0
        return 0;
1663
0
    }
1664
0
    return X509_check_private_key(ssl->cert->key->x509,
1665
0
        ssl->cert->key->privatekey);
1666
0
}
1667
1668
int SSL_waiting_for_async(SSL *s)
1669
0
{
1670
0
    if (s->job)
1671
0
        return 1;
1672
1673
0
    return 0;
1674
0
}
1675
1676
int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
1677
0
{
1678
0
    ASYNC_WAIT_CTX *ctx = s->waitctx;
1679
1680
0
    if (ctx == NULL)
1681
0
        return 0;
1682
0
    return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
1683
0
}
1684
1685
int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
1686
    OSSL_ASYNC_FD *delfd, size_t *numdelfds)
1687
0
{
1688
0
    ASYNC_WAIT_CTX *ctx = s->waitctx;
1689
1690
0
    if (ctx == NULL)
1691
0
        return 0;
1692
0
    return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
1693
0
        numdelfds);
1694
0
}
1695
1696
int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
1697
0
{
1698
0
    ctx->async_cb = callback;
1699
0
    return 1;
1700
0
}
1701
1702
int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
1703
0
{
1704
0
    ctx->async_cb_arg = arg;
1705
0
    return 1;
1706
0
}
1707
1708
int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
1709
0
{
1710
0
    s->async_cb = callback;
1711
0
    return 1;
1712
0
}
1713
1714
int SSL_set_async_callback_arg(SSL *s, void *arg)
1715
0
{
1716
0
    s->async_cb_arg = arg;
1717
0
    return 1;
1718
0
}
1719
1720
int SSL_get_async_status(SSL *s, int *status)
1721
0
{
1722
0
    ASYNC_WAIT_CTX *ctx = s->waitctx;
1723
1724
0
    if (ctx == NULL)
1725
0
        return 0;
1726
0
    *status = ASYNC_WAIT_CTX_get_status(ctx);
1727
0
    return 1;
1728
0
}
1729
1730
int SSL_accept(SSL *s)
1731
2.12k
{
1732
2.12k
    if (s->handshake_func == NULL) {
1733
        /* Not properly initialized yet */
1734
0
        SSL_set_accept_state(s);
1735
0
    }
1736
1737
2.12k
    return SSL_do_handshake(s);
1738
2.12k
}
1739
1740
int SSL_connect(SSL *s)
1741
0
{
1742
0
    if (s->handshake_func == NULL) {
1743
        /* Not properly initialized yet */
1744
0
        SSL_set_connect_state(s);
1745
0
    }
1746
1747
0
    return SSL_do_handshake(s);
1748
0
}
1749
1750
long SSL_get_default_timeout(const SSL *s)
1751
0
{
1752
0
    return s->method->get_timeout();
1753
0
}
1754
1755
static int ssl_async_wait_ctx_cb(void *arg)
1756
0
{
1757
0
    SSL *s = (SSL *)arg;
1758
1759
0
    return s->async_cb(s, s->async_cb_arg);
1760
0
}
1761
1762
static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
1763
    int (*func)(void *))
1764
0
{
1765
0
    int ret;
1766
0
    if (s->waitctx == NULL) {
1767
0
        s->waitctx = ASYNC_WAIT_CTX_new();
1768
0
        if (s->waitctx == NULL)
1769
0
            return -1;
1770
0
        if (s->async_cb != NULL
1771
0
            && !ASYNC_WAIT_CTX_set_callback(s->waitctx, ssl_async_wait_ctx_cb, s))
1772
0
            return -1;
1773
0
    }
1774
1775
0
    s->rwstate = SSL_NOTHING;
1776
0
    switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
1777
0
        sizeof(struct ssl_async_args))) {
1778
0
    case ASYNC_ERR:
1779
0
        s->rwstate = SSL_NOTHING;
1780
0
        ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
1781
0
        return -1;
1782
0
    case ASYNC_PAUSE:
1783
0
        s->rwstate = SSL_ASYNC_PAUSED;
1784
0
        return -1;
1785
0
    case ASYNC_NO_JOBS:
1786
0
        s->rwstate = SSL_ASYNC_NO_JOBS;
1787
0
        return -1;
1788
0
    case ASYNC_FINISH:
1789
0
        s->job = NULL;
1790
0
        return ret;
1791
0
    default:
1792
0
        s->rwstate = SSL_NOTHING;
1793
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1794
        /* Shouldn't happen */
1795
0
        return -1;
1796
0
    }
1797
0
}
1798
1799
static int ssl_io_intern(void *vargs)
1800
0
{
1801
0
    struct ssl_async_args *args;
1802
0
    SSL *s;
1803
0
    void *buf;
1804
0
    size_t num;
1805
1806
0
    args = (struct ssl_async_args *)vargs;
1807
0
    s = args->s;
1808
0
    buf = args->buf;
1809
0
    num = args->num;
1810
0
    switch (args->type) {
1811
0
    case READFUNC:
1812
0
        return args->f.func_read(s, buf, num, &s->asyncrw);
1813
0
    case WRITEFUNC:
1814
0
        return args->f.func_write(s, buf, num, &s->asyncrw);
1815
0
    case OTHERFUNC:
1816
0
        return args->f.func_other(s);
1817
0
    }
1818
0
    return -1;
1819
0
}
1820
1821
int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
1822
0
{
1823
0
    if (s->handshake_func == NULL) {
1824
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
1825
0
        return -1;
1826
0
    }
1827
1828
0
    if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1829
0
        s->rwstate = SSL_NOTHING;
1830
0
        return 0;
1831
0
    }
1832
1833
0
    if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
1834
0
        || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
1835
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1836
0
        return 0;
1837
0
    }
1838
    /*
1839
     * If we are a client and haven't received the ServerHello etc then we
1840
     * better do that
1841
     */
1842
0
    ossl_statem_check_finish_init(s, 0);
1843
1844
0
    if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
1845
0
        struct ssl_async_args args;
1846
0
        int ret;
1847
1848
0
        args.s = s;
1849
0
        args.buf = buf;
1850
0
        args.num = num;
1851
0
        args.type = READFUNC;
1852
0
        args.f.func_read = s->method->ssl_read;
1853
1854
0
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
1855
0
        *readbytes = s->asyncrw;
1856
0
        return ret;
1857
0
    } else {
1858
0
        return s->method->ssl_read(s, buf, num, readbytes);
1859
0
    }
1860
0
}
1861
1862
int SSL_read(SSL *s, void *buf, int num)
1863
45.0M
{
1864
45.0M
    int ret;
1865
45.0M
    size_t readbytes;
1866
1867
45.0M
    if (num < 0) {
1868
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
1869
0
        return -1;
1870
0
    }
1871
1872
45.0M
    ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
1873
1874
    /*
1875
     * The cast is safe here because ret should be <= INT_MAX because num is
1876
     * <= INT_MAX
1877
     */
1878
45.0M
    if (ret > 0)
1879
61.9k
        ret = (int)readbytes;
1880
1881
45.0M
    return ret;
1882
45.0M
}
1883
1884
int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
1885
0
{
1886
0
    int ret = ssl_read_internal(s, buf, num, readbytes);
1887
1888
0
    if (ret < 0)
1889
0
        ret = 0;
1890
0
    return ret;
1891
0
}
1892
1893
int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
1894
2.12k
{
1895
2.12k
    int ret;
1896
1897
2.12k
    if (!s->server) {
1898
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1899
0
        return SSL_READ_EARLY_DATA_ERROR;
1900
0
    }
1901
1902
2.12k
    switch (s->early_data_state) {
1903
2.12k
    case SSL_EARLY_DATA_NONE:
1904
2.12k
        if (!SSL_in_before(s)) {
1905
0
            ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1906
0
            return SSL_READ_EARLY_DATA_ERROR;
1907
0
        }
1908
        /* fall through */
1909
1910
2.12k
    case SSL_EARLY_DATA_ACCEPT_RETRY:
1911
2.12k
        s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
1912
2.12k
        ret = SSL_accept(s);
1913
2.12k
        if (ret <= 0) {
1914
            /* NBIO or error */
1915
1.85k
            s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
1916
1.85k
            return SSL_READ_EARLY_DATA_ERROR;
1917
1.85k
        }
1918
        /* fall through */
1919
1920
269
    case SSL_EARLY_DATA_READ_RETRY:
1921
269
        if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
1922
0
            s->early_data_state = SSL_EARLY_DATA_READING;
1923
0
            ret = SSL_read_ex(s, buf, num, readbytes);
1924
            /*
1925
             * State machine will update early_data_state to
1926
             * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
1927
             * message
1928
             */
1929
0
            if (ret > 0 || (ret <= 0 && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING)) {
1930
0
                s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
1931
0
                return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
1932
0
                               : SSL_READ_EARLY_DATA_ERROR;
1933
0
            }
1934
269
        } else {
1935
269
            s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
1936
269
        }
1937
269
        *readbytes = 0;
1938
269
        return SSL_READ_EARLY_DATA_FINISH;
1939
1940
0
    default:
1941
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1942
0
        return SSL_READ_EARLY_DATA_ERROR;
1943
2.12k
    }
1944
2.12k
}
1945
1946
int SSL_get_early_data_status(const SSL *s)
1947
0
{
1948
0
    return s->ext.early_data;
1949
0
}
1950
1951
static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
1952
0
{
1953
0
    if (s->handshake_func == NULL) {
1954
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
1955
0
        return -1;
1956
0
    }
1957
1958
0
    if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1959
0
        return 0;
1960
0
    }
1961
0
    if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
1962
0
        struct ssl_async_args args;
1963
0
        int ret;
1964
1965
0
        args.s = s;
1966
0
        args.buf = buf;
1967
0
        args.num = num;
1968
0
        args.type = READFUNC;
1969
0
        args.f.func_read = s->method->ssl_peek;
1970
1971
0
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
1972
0
        *readbytes = s->asyncrw;
1973
0
        return ret;
1974
0
    } else {
1975
0
        return s->method->ssl_peek(s, buf, num, readbytes);
1976
0
    }
1977
0
}
1978
1979
int SSL_peek(SSL *s, void *buf, int num)
1980
0
{
1981
0
    int ret;
1982
0
    size_t readbytes;
1983
1984
0
    if (num < 0) {
1985
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
1986
0
        return -1;
1987
0
    }
1988
1989
0
    ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
1990
1991
    /*
1992
     * The cast is safe here because ret should be <= INT_MAX because num is
1993
     * <= INT_MAX
1994
     */
1995
0
    if (ret > 0)
1996
0
        ret = (int)readbytes;
1997
1998
0
    return ret;
1999
0
}
2000
2001
int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2002
0
{
2003
0
    int ret = ssl_peek_internal(s, buf, num, readbytes);
2004
2005
0
    if (ret < 0)
2006
0
        ret = 0;
2007
0
    return ret;
2008
0
}
2009
2010
int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
2011
{
2012
    if (s->handshake_func == NULL) {
2013
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2014
        return -1;
2015
    }
2016
2017
    if (s->shutdown & SSL_SENT_SHUTDOWN) {
2018
        s->rwstate = SSL_NOTHING;
2019
        ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2020
        return -1;
2021
    }
2022
2023
    if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2024
        || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
2025
        || s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
2026
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2027
        return 0;
2028
    }
2029
    /* If we are a client and haven't sent the Finished we better do that */
2030
    ossl_statem_check_finish_init(s, 1);
2031
2032
    if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2033
        int ret;
2034
        struct ssl_async_args args;
2035
2036
        args.s = s;
2037
        args.buf = (void *)buf;
2038
        args.num = num;
2039
        args.type = WRITEFUNC;
2040
        args.f.func_write = s->method->ssl_write;
2041
2042
        ret = ssl_start_async_job(s, &args, ssl_io_intern);
2043
        *written = s->asyncrw;
2044
        return ret;
2045
    } else {
2046
        return s->method->ssl_write(s, buf, num, written);
2047
    }
2048
}
2049
2050
ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
2051
0
{
2052
0
    ossl_ssize_t ret;
2053
2054
0
    if (s->handshake_func == NULL) {
2055
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2056
0
        return -1;
2057
0
    }
2058
2059
0
    if (s->shutdown & SSL_SENT_SHUTDOWN) {
2060
0
        s->rwstate = SSL_NOTHING;
2061
0
        ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2062
0
        return -1;
2063
0
    }
2064
2065
0
    if (!BIO_get_ktls_send(s->wbio)) {
2066
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2067
0
        return -1;
2068
0
    }
2069
2070
    /* If we have an alert to send, lets send it */
2071
0
    if (s->s3.alert_dispatch) {
2072
0
        ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
2073
0
        if (ret <= 0) {
2074
            /* SSLfatal() already called if appropriate */
2075
0
            return ret;
2076
0
        }
2077
        /* if it went, fall through and send more stuff */
2078
0
    }
2079
2080
0
    s->rwstate = SSL_WRITING;
2081
0
    if (BIO_flush(s->wbio) <= 0) {
2082
0
        if (!BIO_should_retry(s->wbio)) {
2083
0
            s->rwstate = SSL_NOTHING;
2084
0
        } else {
2085
0
#ifdef EAGAIN
2086
0
            set_sys_error(EAGAIN);
2087
0
#endif
2088
0
        }
2089
0
        return -1;
2090
0
    }
2091
2092
0
#ifdef OPENSSL_NO_KTLS
2093
0
    ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
2094
0
        "can't call ktls_sendfile(), ktls disabled");
2095
0
    return -1;
2096
#else
2097
    ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
2098
    if (ret < 0) {
2099
#if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
2100
        if ((get_last_sys_error() == EAGAIN) || (get_last_sys_error() == EINTR) || (get_last_sys_error() == EBUSY))
2101
            BIO_set_retry_write(s->wbio);
2102
        else
2103
#endif
2104
            ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2105
        return ret;
2106
    }
2107
    s->rwstate = SSL_NOTHING;
2108
    return ret;
2109
#endif
2110
0
}
2111
2112
int SSL_write(SSL *s, const void *buf, int num)
2113
125k
{
2114
125k
    int ret;
2115
125k
    size_t written;
2116
2117
125k
    if (num < 0) {
2118
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2119
0
        return -1;
2120
0
    }
2121
2122
125k
    ret = ssl_write_internal(s, buf, (size_t)num, &written);
2123
2124
    /*
2125
     * The cast is safe here because ret should be <= INT_MAX because num is
2126
     * <= INT_MAX
2127
     */
2128
125k
    if (ret > 0)
2129
4.97k
        ret = (int)written;
2130
2131
125k
    return ret;
2132
125k
}
2133
2134
int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
2135
0
{
2136
0
    int ret = ssl_write_internal(s, buf, num, written);
2137
2138
0
    if (ret < 0)
2139
0
        ret = 0;
2140
0
    return ret;
2141
0
}
2142
2143
int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
2144
0
{
2145
0
    int ret, early_data_state;
2146
0
    size_t writtmp;
2147
0
    uint32_t partialwrite;
2148
2149
0
    switch (s->early_data_state) {
2150
0
    case SSL_EARLY_DATA_NONE:
2151
0
        if (s->server
2152
0
            || !SSL_in_before(s)
2153
0
            || ((s->session == NULL || s->session->ext.max_early_data == 0)
2154
0
                && (s->psk_use_session_cb == NULL))) {
2155
0
            ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2156
0
            return 0;
2157
0
        }
2158
        /* fall through */
2159
2160
0
    case SSL_EARLY_DATA_CONNECT_RETRY:
2161
0
        s->early_data_state = SSL_EARLY_DATA_CONNECTING;
2162
0
        ret = SSL_connect(s);
2163
0
        if (ret <= 0) {
2164
            /* NBIO or error */
2165
0
            s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
2166
0
            return 0;
2167
0
        }
2168
        /* fall through */
2169
2170
0
    case SSL_EARLY_DATA_WRITE_RETRY:
2171
0
        s->early_data_state = SSL_EARLY_DATA_WRITING;
2172
        /*
2173
         * We disable partial write for early data because we don't keep track
2174
         * of how many bytes we've written between the SSL_write_ex() call and
2175
         * the flush if the flush needs to be retried)
2176
         */
2177
0
        partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
2178
0
        s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
2179
0
        ret = SSL_write_ex(s, buf, num, &writtmp);
2180
0
        s->mode |= partialwrite;
2181
0
        if (!ret) {
2182
0
            s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2183
0
            return ret;
2184
0
        }
2185
0
        s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
2186
        /* fall through */
2187
2188
0
    case SSL_EARLY_DATA_WRITE_FLUSH:
2189
        /* The buffering BIO is still in place so we need to flush it */
2190
0
        if (statem_flush(s) != 1)
2191
0
            return 0;
2192
0
        *written = num;
2193
0
        s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2194
0
        return 1;
2195
2196
0
    case SSL_EARLY_DATA_FINISHED_READING:
2197
0
    case SSL_EARLY_DATA_READ_RETRY:
2198
0
        early_data_state = s->early_data_state;
2199
        /* We are a server writing to an unauthenticated client */
2200
0
        s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
2201
0
        ret = SSL_write_ex(s, buf, num, written);
2202
        /* The buffering BIO is still in place */
2203
0
        if (ret)
2204
0
            (void)BIO_flush(s->wbio);
2205
0
        s->early_data_state = early_data_state;
2206
0
        return ret;
2207
2208
0
    default:
2209
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2210
0
        return 0;
2211
0
    }
2212
0
}
2213
2214
int SSL_shutdown(SSL *s)
2215
0
{
2216
    /*
2217
     * Note that this function behaves differently from what one might
2218
     * expect.  Return values are 0 for no success (yet), 1 for success; but
2219
     * calling it once is usually not enough, even if blocking I/O is used
2220
     * (see ssl3_shutdown).
2221
     */
2222
2223
0
    if (s->handshake_func == NULL) {
2224
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2225
0
        return -1;
2226
0
    }
2227
2228
0
    if (!SSL_in_init(s)) {
2229
0
        if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2230
0
            struct ssl_async_args args;
2231
2232
0
            memset(&args, 0, sizeof(args));
2233
0
            args.s = s;
2234
0
            args.type = OTHERFUNC;
2235
0
            args.f.func_other = s->method->ssl_shutdown;
2236
2237
0
            return ssl_start_async_job(s, &args, ssl_io_intern);
2238
0
        } else {
2239
0
            return s->method->ssl_shutdown(s);
2240
0
        }
2241
0
    } else {
2242
0
        ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
2243
0
        return -1;
2244
0
    }
2245
0
}
2246
2247
int SSL_key_update(SSL *s, int updatetype)
2248
0
{
2249
0
    if (!SSL_IS_TLS13(s)) {
2250
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
2251
0
        return 0;
2252
0
    }
2253
2254
0
    if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
2255
0
        && updatetype != SSL_KEY_UPDATE_REQUESTED) {
2256
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
2257
0
        return 0;
2258
0
    }
2259
2260
0
    if (!SSL_is_init_finished(s)) {
2261
0
        ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
2262
0
        return 0;
2263
0
    }
2264
2265
0
    if (RECORD_LAYER_write_pending(&s->rlayer)) {
2266
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
2267
0
        return 0;
2268
0
    }
2269
2270
0
    ossl_statem_set_in_init(s, 1);
2271
0
    s->key_update = updatetype;
2272
0
    return 1;
2273
0
}
2274
2275
int SSL_get_key_update_type(const SSL *s)
2276
0
{
2277
0
    return s->key_update;
2278
0
}
2279
2280
/*
2281
 * Can we accept a renegotiation request?  If yes, set the flag and
2282
 * return 1 if yes. If not, raise error and return 0.
2283
 */
2284
static int can_renegotiate(const SSL *s)
2285
1.23k
{
2286
1.23k
    if (SSL_IS_TLS13(s)) {
2287
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
2288
0
        return 0;
2289
0
    }
2290
2291
1.23k
    if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0) {
2292
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
2293
0
        return 0;
2294
0
    }
2295
2296
1.23k
    return 1;
2297
1.23k
}
2298
2299
int SSL_renegotiate(SSL *s)
2300
0
{
2301
0
    if (!can_renegotiate(s))
2302
0
        return 0;
2303
2304
0
    s->renegotiate = 1;
2305
0
    s->new_session = 1;
2306
0
    return s->method->ssl_renegotiate(s);
2307
0
}
2308
2309
int SSL_renegotiate_abbreviated(SSL *s)
2310
{
2311
    if (!can_renegotiate(s))
2312
        return 0;
2313
2314
    s->renegotiate = 1;
2315
    s->new_session = 0;
2316
    return s->method->ssl_renegotiate(s);
2317
}
2318
2319
int SSL_renegotiate_pending(const SSL *s)
2320
0
{
2321
    /*
2322
     * becomes true when negotiation is requested; false again once a
2323
     * handshake has finished
2324
     */
2325
0
    return (s->renegotiate != 0);
2326
0
}
2327
2328
int SSL_new_session_ticket(SSL *s)
2329
0
{
2330
    /* If we are in init because we're sending tickets, okay to send more. */
2331
0
    if ((SSL_in_init(s) && s->ext.extra_tickets_expected == 0)
2332
0
        || SSL_IS_FIRST_HANDSHAKE(s) || !s->server
2333
0
        || !SSL_IS_TLS13(s))
2334
0
        return 0;
2335
0
    s->ext.extra_tickets_expected++;
2336
0
    if (!RECORD_LAYER_write_pending(&s->rlayer) && !SSL_in_init(s))
2337
0
        ossl_statem_set_in_init(s, 1);
2338
0
    return 1;
2339
0
}
2340
2341
long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
2342
13.3k
{
2343
13.3k
    long l;
2344
2345
13.3k
    switch (cmd) {
2346
0
    case SSL_CTRL_GET_READ_AHEAD:
2347
0
        return RECORD_LAYER_get_read_ahead(&s->rlayer);
2348
0
    case SSL_CTRL_SET_READ_AHEAD:
2349
0
        l = RECORD_LAYER_get_read_ahead(&s->rlayer);
2350
0
        RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
2351
0
        return l;
2352
2353
0
    case SSL_CTRL_SET_MSG_CALLBACK_ARG:
2354
0
        s->msg_callback_arg = parg;
2355
0
        return 1;
2356
2357
0
    case SSL_CTRL_MODE:
2358
0
        return (s->mode |= larg);
2359
0
    case SSL_CTRL_CLEAR_MODE:
2360
0
        return (s->mode &= ~larg);
2361
0
    case SSL_CTRL_GET_MAX_CERT_LIST:
2362
0
        return (long)s->max_cert_list;
2363
0
    case SSL_CTRL_SET_MAX_CERT_LIST:
2364
0
        if (larg < 0)
2365
0
            return 0;
2366
0
        l = (long)s->max_cert_list;
2367
0
        s->max_cert_list = (size_t)larg;
2368
0
        return l;
2369
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
2370
0
        if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
2371
0
            return 0;
2372
#ifndef OPENSSL_NO_KTLS
2373
        if (s->wbio != NULL && BIO_get_ktls_send(s->wbio))
2374
            return 0;
2375
#endif /* OPENSSL_NO_KTLS */
2376
0
        s->max_send_fragment = larg;
2377
0
        if (s->max_send_fragment < s->split_send_fragment)
2378
0
            s->split_send_fragment = s->max_send_fragment;
2379
0
        return 1;
2380
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
2381
0
        if ((size_t)larg > s->max_send_fragment || larg == 0)
2382
0
            return 0;
2383
0
        s->split_send_fragment = larg;
2384
0
        return 1;
2385
0
    case SSL_CTRL_SET_MAX_PIPELINES:
2386
0
        if (larg < 1 || larg > SSL_MAX_PIPELINES)
2387
0
            return 0;
2388
0
        s->max_pipelines = larg;
2389
0
        if (larg > 1)
2390
0
            RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
2391
0
        return 1;
2392
0
    case SSL_CTRL_GET_RI_SUPPORT:
2393
0
        return s->s3.send_connection_binding;
2394
0
    case SSL_CTRL_SET_RETRY_VERIFY:
2395
0
        s->rwstate = SSL_RETRY_VERIFY;
2396
0
        return 1;
2397
0
    case SSL_CTRL_CERT_FLAGS:
2398
0
        return (s->cert->cert_flags |= larg);
2399
0
    case SSL_CTRL_CLEAR_CERT_FLAGS:
2400
0
        return (s->cert->cert_flags &= ~larg);
2401
2402
0
    case SSL_CTRL_GET_RAW_CIPHERLIST:
2403
0
        if (parg) {
2404
0
            if (s->s3.tmp.ciphers_raw == NULL)
2405
0
                return 0;
2406
0
            *(unsigned char **)parg = s->s3.tmp.ciphers_raw;
2407
0
            return (int)s->s3.tmp.ciphers_rawlen;
2408
0
        } else {
2409
0
            return TLS_CIPHER_LEN;
2410
0
        }
2411
0
    case SSL_CTRL_GET_EXTMS_SUPPORT:
2412
0
        if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
2413
0
            return -1;
2414
0
        if (s->session->flags & SSL_SESS_FLAG_EXTMS)
2415
0
            return 1;
2416
0
        else
2417
0
            return 0;
2418
6.69k
    case SSL_CTRL_SET_MIN_PROTO_VERSION:
2419
6.69k
        return ssl_check_allowed_versions(larg, s->max_proto_version)
2420
6.69k
            && ssl_set_version_bound(s->ctx->method->version, (int)larg,
2421
6.69k
                &s->min_proto_version);
2422
0
    case SSL_CTRL_GET_MIN_PROTO_VERSION:
2423
0
        return s->min_proto_version;
2424
0
    case SSL_CTRL_SET_MAX_PROTO_VERSION:
2425
0
        return ssl_check_allowed_versions(s->min_proto_version, larg)
2426
0
            && ssl_set_version_bound(s->ctx->method->version, (int)larg,
2427
0
                &s->max_proto_version);
2428
0
    case SSL_CTRL_GET_MAX_PROTO_VERSION:
2429
0
        return s->max_proto_version;
2430
6.69k
    default:
2431
6.69k
        return s->method->ssl_ctrl(s, cmd, larg, parg);
2432
13.3k
    }
2433
13.3k
}
2434
2435
long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
2436
0
{
2437
0
    switch (cmd) {
2438
0
    case SSL_CTRL_SET_MSG_CALLBACK:
2439
0
        s->msg_callback = (void (*)(int write_p, int version, int content_type,
2440
0
            const void *buf, size_t len, SSL *ssl,
2441
0
            void *arg))(fp);
2442
0
        return 1;
2443
2444
0
    default:
2445
0
        return s->method->ssl_callback_ctrl(s, cmd, fp);
2446
0
    }
2447
0
}
2448
2449
LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
2450
0
{
2451
0
    return ctx->sessions;
2452
0
}
2453
2454
static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
2455
1.46k
{
2456
1.46k
    int res = 0;
2457
2458
1.46k
    if (ssl_tsan_lock(ctx)) {
2459
1.46k
        res = tsan_load(stat);
2460
1.46k
        ssl_tsan_unlock(ctx);
2461
1.46k
    }
2462
1.46k
    return res;
2463
1.46k
}
2464
2465
long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2466
25.3k
{
2467
25.3k
    long l;
2468
    /* For some cases with ctx == NULL perform syntax checks */
2469
25.3k
    if (ctx == NULL) {
2470
0
        switch (cmd) {
2471
0
        case SSL_CTRL_SET_GROUPS_LIST:
2472
0
            return tls1_set_groups_list(ctx, NULL, NULL, parg);
2473
0
        case SSL_CTRL_SET_SIGALGS_LIST:
2474
0
        case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
2475
0
            return tls1_set_sigalgs_list(NULL, parg, 0);
2476
0
        default:
2477
0
            return 0;
2478
0
        }
2479
0
    }
2480
2481
25.3k
    switch (cmd) {
2482
0
    case SSL_CTRL_GET_READ_AHEAD:
2483
0
        return ctx->read_ahead;
2484
0
    case SSL_CTRL_SET_READ_AHEAD:
2485
0
        l = ctx->read_ahead;
2486
0
        ctx->read_ahead = larg;
2487
0
        return l;
2488
2489
0
    case SSL_CTRL_SET_MSG_CALLBACK_ARG:
2490
0
        ctx->msg_callback_arg = parg;
2491
0
        return 1;
2492
2493
0
    case SSL_CTRL_GET_MAX_CERT_LIST:
2494
0
        return (long)ctx->max_cert_list;
2495
0
    case SSL_CTRL_SET_MAX_CERT_LIST:
2496
0
        if (larg < 0)
2497
0
            return 0;
2498
0
        l = (long)ctx->max_cert_list;
2499
0
        ctx->max_cert_list = (size_t)larg;
2500
0
        return l;
2501
2502
0
    case SSL_CTRL_SET_SESS_CACHE_SIZE:
2503
0
        if (larg < 0)
2504
0
            return 0;
2505
0
        l = (long)ctx->session_cache_size;
2506
0
        ctx->session_cache_size = (size_t)larg;
2507
0
        return l;
2508
1.08k
    case SSL_CTRL_GET_SESS_CACHE_SIZE:
2509
1.08k
        return (long)ctx->session_cache_size;
2510
0
    case SSL_CTRL_SET_SESS_CACHE_MODE:
2511
0
        l = ctx->session_cache_mode;
2512
0
        ctx->session_cache_mode = larg;
2513
0
        return l;
2514
0
    case SSL_CTRL_GET_SESS_CACHE_MODE:
2515
0
        return ctx->session_cache_mode;
2516
2517
540
    case SSL_CTRL_SESS_NUMBER:
2518
540
        return lh_SSL_SESSION_num_items(ctx->sessions);
2519
0
    case SSL_CTRL_SESS_CONNECT:
2520
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
2521
0
    case SSL_CTRL_SESS_CONNECT_GOOD:
2522
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
2523
0
    case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
2524
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
2525
0
    case SSL_CTRL_SESS_ACCEPT:
2526
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
2527
0
    case SSL_CTRL_SESS_ACCEPT_GOOD:
2528
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
2529
0
    case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
2530
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
2531
0
    case SSL_CTRL_SESS_HIT:
2532
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
2533
0
    case SSL_CTRL_SESS_CB_HIT:
2534
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
2535
0
    case SSL_CTRL_SESS_MISSES:
2536
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
2537
0
    case SSL_CTRL_SESS_TIMEOUTS:
2538
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
2539
0
    case SSL_CTRL_SESS_CACHE_FULL:
2540
0
        return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
2541
0
    case SSL_CTRL_MODE:
2542
0
        return (ctx->mode |= larg);
2543
0
    case SSL_CTRL_CLEAR_MODE:
2544
0
        return (ctx->mode &= ~larg);
2545
0
    case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
2546
0
        if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
2547
0
            return 0;
2548
0
        ctx->max_send_fragment = larg;
2549
0
        if (ctx->max_send_fragment < ctx->split_send_fragment)
2550
0
            ctx->split_send_fragment = ctx->max_send_fragment;
2551
0
        return 1;
2552
0
    case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
2553
0
        if ((size_t)larg > ctx->max_send_fragment || larg == 0)
2554
0
            return 0;
2555
0
        ctx->split_send_fragment = larg;
2556
0
        return 1;
2557
0
    case SSL_CTRL_SET_MAX_PIPELINES:
2558
0
        if (larg < 1 || larg > SSL_MAX_PIPELINES)
2559
0
            return 0;
2560
0
        ctx->max_pipelines = larg;
2561
0
        return 1;
2562
0
    case SSL_CTRL_CERT_FLAGS:
2563
0
        return (ctx->cert->cert_flags |= larg);
2564
0
    case SSL_CTRL_CLEAR_CERT_FLAGS:
2565
0
        return (ctx->cert->cert_flags &= ~larg);
2566
23.7k
    case SSL_CTRL_SET_MIN_PROTO_VERSION:
2567
23.7k
        return ssl_check_allowed_versions(larg, ctx->max_proto_version)
2568
23.7k
            && ssl_set_version_bound(ctx->method->version, (int)larg,
2569
23.7k
                &ctx->min_proto_version);
2570
0
    case SSL_CTRL_GET_MIN_PROTO_VERSION:
2571
0
        return ctx->min_proto_version;
2572
0
    case SSL_CTRL_SET_MAX_PROTO_VERSION:
2573
0
        return ssl_check_allowed_versions(ctx->min_proto_version, larg)
2574
0
            && ssl_set_version_bound(ctx->method->version, (int)larg,
2575
0
                &ctx->max_proto_version);
2576
0
    case SSL_CTRL_GET_MAX_PROTO_VERSION:
2577
0
        return ctx->max_proto_version;
2578
0
    default:
2579
0
        return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
2580
25.3k
    }
2581
25.3k
}
2582
2583
long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
2584
0
{
2585
0
    switch (cmd) {
2586
0
    case SSL_CTRL_SET_MSG_CALLBACK:
2587
0
        ctx->msg_callback = (void (*)(int write_p, int version, int content_type,
2588
0
            const void *buf, size_t len, SSL *ssl,
2589
0
            void *arg))(fp);
2590
0
        return 1;
2591
2592
0
    default:
2593
0
        return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
2594
0
    }
2595
0
}
2596
2597
int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
2598
7.81M
{
2599
7.81M
    if (a->id > b->id)
2600
3.10M
        return 1;
2601
4.71M
    if (a->id < b->id)
2602
4.36M
        return -1;
2603
351k
    return 0;
2604
4.71M
}
2605
2606
int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
2607
    const SSL_CIPHER *const *bp)
2608
145M
{
2609
145M
    if ((*ap)->id > (*bp)->id)
2610
80.9M
        return 1;
2611
64.4M
    if ((*ap)->id < (*bp)->id)
2612
64.3M
        return -1;
2613
73.5k
    return 0;
2614
64.4M
}
2615
2616
/** return a STACK of the ciphers available for the SSL and in order of
2617
 * preference */
2618
STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
2619
28.3k
{
2620
28.3k
    if (s != NULL) {
2621
28.3k
        if (s->cipher_list != NULL) {
2622
20.2k
            return s->cipher_list;
2623
20.2k
        } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
2624
8.07k
            return s->ctx->cipher_list;
2625
8.07k
        }
2626
28.3k
    }
2627
0
    return NULL;
2628
28.3k
}
2629
2630
STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
2631
0
{
2632
0
    if ((s == NULL) || !s->server)
2633
0
        return NULL;
2634
0
    return s->peer_ciphers;
2635
0
}
2636
2637
STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
2638
13.5k
{
2639
13.5k
    STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
2640
13.5k
    int i;
2641
2642
13.5k
    ciphers = SSL_get_ciphers(s);
2643
13.5k
    if (!ciphers)
2644
0
        return NULL;
2645
13.5k
    if (!ssl_set_client_disabled(s))
2646
0
        return NULL;
2647
2.34M
    for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
2648
2.32M
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
2649
2.32M
        if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
2650
1.33M
            if (!sk)
2651
13.5k
                sk = sk_SSL_CIPHER_new_null();
2652
1.33M
            if (!sk)
2653
0
                return NULL;
2654
1.33M
            if (!sk_SSL_CIPHER_push(sk, c)) {
2655
0
                sk_SSL_CIPHER_free(sk);
2656
0
                return NULL;
2657
0
            }
2658
1.33M
        }
2659
2.32M
    }
2660
13.5k
    return sk;
2661
13.5k
}
2662
2663
/** return a STACK of the ciphers available for the SSL and in order of
2664
 * algorithm id */
2665
STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
2666
73.6k
{
2667
73.6k
    if (s != NULL) {
2668
73.6k
        if (s->cipher_list_by_id != NULL) {
2669
48.5k
            return s->cipher_list_by_id;
2670
48.5k
        } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
2671
25.0k
            return s->ctx->cipher_list_by_id;
2672
25.0k
        }
2673
73.6k
    }
2674
0
    return NULL;
2675
73.6k
}
2676
2677
/** The old interface to get the same thing as SSL_get_ciphers() */
2678
const char *SSL_get_cipher_list(const SSL *s, int n)
2679
0
{
2680
0
    const SSL_CIPHER *c;
2681
0
    STACK_OF(SSL_CIPHER) *sk;
2682
2683
0
    if (s == NULL)
2684
0
        return NULL;
2685
0
    sk = SSL_get_ciphers(s);
2686
0
    if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
2687
0
        return NULL;
2688
0
    c = sk_SSL_CIPHER_value(sk, n);
2689
0
    if (c == NULL)
2690
0
        return NULL;
2691
0
    return c->name;
2692
0
}
2693
2694
/** return a STACK of the ciphers available for the SSL_CTX and in order of
2695
 * preference */
2696
STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
2697
0
{
2698
0
    if (ctx != NULL)
2699
0
        return ctx->cipher_list;
2700
0
    return NULL;
2701
0
}
2702
2703
/*
2704
 * Distinguish between ciphers controlled by set_ciphersuite() and
2705
 * set_cipher_list() when counting.
2706
 */
2707
static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
2708
112k
{
2709
112k
    int i, num = 0;
2710
112k
    const SSL_CIPHER *c;
2711
2712
112k
    if (sk == NULL)
2713
0
        return 0;
2714
19.5M
    for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
2715
19.4M
        c = sk_SSL_CIPHER_value(sk, i);
2716
19.4M
        if (c->min_tls >= TLS1_3_VERSION)
2717
338k
            continue;
2718
19.0M
        num++;
2719
19.0M
    }
2720
112k
    return num;
2721
112k
}
2722
2723
/** specify the ciphers to be used by default by the SSL_CTX */
2724
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
2725
4.77k
{
2726
4.77k
    STACK_OF(SSL_CIPHER) *sk;
2727
2728
4.77k
    sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
2729
4.77k
        &ctx->cipher_list, &ctx->cipher_list_by_id, str,
2730
4.77k
        ctx->cert);
2731
    /*
2732
     * ssl_create_cipher_list may return an empty stack if it was unable to
2733
     * find a cipher matching the given rule string (for example if the rule
2734
     * string specifies a cipher which has been disabled). This is not an
2735
     * error as far as ssl_create_cipher_list is concerned, and hence
2736
     * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
2737
     */
2738
4.77k
    if (sk == NULL)
2739
0
        return 0;
2740
4.77k
    else if (cipher_list_tls12_num(sk) == 0) {
2741
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
2742
0
        return 0;
2743
0
    }
2744
4.77k
    return 1;
2745
4.77k
}
2746
2747
/** specify the ciphers to be used by the SSL */
2748
int SSL_set_cipher_list(SSL *s, const char *str)
2749
6.69k
{
2750
6.69k
    STACK_OF(SSL_CIPHER) *sk;
2751
2752
6.69k
    sk = ssl_create_cipher_list(s->ctx, s->tls13_ciphersuites,
2753
6.69k
        &s->cipher_list, &s->cipher_list_by_id, str,
2754
6.69k
        s->cert);
2755
    /* see comment in SSL_CTX_set_cipher_list */
2756
6.69k
    if (sk == NULL)
2757
0
        return 0;
2758
6.69k
    else if (cipher_list_tls12_num(sk) == 0) {
2759
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
2760
0
        return 0;
2761
0
    }
2762
6.69k
    return 1;
2763
6.69k
}
2764
2765
char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
2766
0
{
2767
0
    char *p;
2768
0
    STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
2769
0
    const SSL_CIPHER *c;
2770
0
    int i;
2771
2772
0
    if (!s->server
2773
0
        || s->peer_ciphers == NULL
2774
0
        || size < 2)
2775
0
        return NULL;
2776
2777
0
    p = buf;
2778
0
    clntsk = s->peer_ciphers;
2779
0
    srvrsk = SSL_get_ciphers(s);
2780
0
    if (clntsk == NULL || srvrsk == NULL)
2781
0
        return NULL;
2782
2783
0
    if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
2784
0
        return NULL;
2785
2786
0
    for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
2787
0
        int n;
2788
2789
0
        c = sk_SSL_CIPHER_value(clntsk, i);
2790
0
        if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
2791
0
            continue;
2792
2793
0
        n = (int)OPENSSL_strnlen(c->name, size);
2794
0
        if (n >= size)
2795
0
            break;
2796
2797
0
        memcpy(p, c->name, n);
2798
0
        p += n;
2799
0
        *(p++) = ':';
2800
0
        size -= n + 1;
2801
0
    }
2802
2803
    /* No overlap */
2804
0
    if (p == buf)
2805
0
        return NULL;
2806
2807
0
    p[-1] = '\0';
2808
0
    return buf;
2809
0
}
2810
2811
/**
2812
 * Return the requested servername (SNI) value. Note that the behaviour varies
2813
 * depending on:
2814
 * - whether this is called by the client or the server,
2815
 * - if we are before or during/after the handshake,
2816
 * - if a resumption or normal handshake is being attempted/has occurred
2817
 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
2818
 *
2819
 * Note that only the host_name type is defined (RFC 3546).
2820
 */
2821
const char *SSL_get_servername(const SSL *s, const int type)
2822
0
{
2823
    /*
2824
     * If we don't know if we are the client or the server yet then we assume
2825
     * client.
2826
     */
2827
0
    int server = s->handshake_func == NULL ? 0 : s->server;
2828
0
    if (type != TLSEXT_NAMETYPE_host_name)
2829
0
        return NULL;
2830
2831
0
    if (server) {
2832
        /**
2833
         * Server side
2834
         * In TLSv1.3 on the server SNI is not associated with the session
2835
         * but in TLSv1.2 or below it is.
2836
         *
2837
         * Before the handshake:
2838
         *  - return NULL
2839
         *
2840
         * During/after the handshake (TLSv1.2 or below resumption occurred):
2841
         * - If a servername was accepted by the server in the original
2842
         *   handshake then it will return that servername, or NULL otherwise.
2843
         *
2844
         * During/after the handshake (TLSv1.2 or below resumption did not occur):
2845
         * - The function will return the servername requested by the client in
2846
         *   this handshake or NULL if none was requested.
2847
         */
2848
0
        if (s->hit && !SSL_IS_TLS13(s))
2849
0
            return s->session->ext.hostname;
2850
0
    } else {
2851
        /**
2852
         * Client side
2853
         *
2854
         * Before the handshake:
2855
         *  - If a servername has been set via a call to
2856
         *    SSL_set_tlsext_host_name() then it will return that servername
2857
         *  - If one has not been set, but a TLSv1.2 resumption is being
2858
         *    attempted and the session from the original handshake had a
2859
         *    servername accepted by the server then it will return that
2860
         *    servername
2861
         *  - Otherwise it returns NULL
2862
         *
2863
         * During/after the handshake (TLSv1.2 or below resumption occurred):
2864
         * - If the session from the original handshake had a servername accepted
2865
         *   by the server then it will return that servername.
2866
         * - Otherwise it returns the servername set via
2867
         *   SSL_set_tlsext_host_name() (or NULL if it was not called).
2868
         *
2869
         * During/after the handshake (TLSv1.2 or below resumption did not occur):
2870
         * - It will return the servername set via SSL_set_tlsext_host_name()
2871
         *   (or NULL if it was not called).
2872
         */
2873
0
        if (SSL_in_before(s)) {
2874
0
            if (s->ext.hostname == NULL
2875
0
                && s->session != NULL
2876
0
                && s->session->ssl_version != TLS1_3_VERSION)
2877
0
                return s->session->ext.hostname;
2878
0
        } else {
2879
0
            if (!SSL_IS_TLS13(s) && s->hit && s->session->ext.hostname != NULL)
2880
0
                return s->session->ext.hostname;
2881
0
        }
2882
0
    }
2883
2884
0
    return s->ext.hostname;
2885
0
}
2886
2887
int SSL_get_servername_type(const SSL *s)
2888
0
{
2889
0
    if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
2890
0
        return TLSEXT_NAMETYPE_host_name;
2891
0
    return -1;
2892
0
}
2893
2894
/*
2895
 * SSL_select_next_proto implements the standard protocol selection. It is
2896
 * expected that this function is called from the callback set by
2897
 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
2898
 * vector of 8-bit, length prefixed byte strings. The length byte itself is
2899
 * not included in the length. A byte string of length 0 is invalid. No byte
2900
 * string may be truncated. The current, but experimental algorithm for
2901
 * selecting the protocol is: 1) If the server doesn't support NPN then this
2902
 * is indicated to the callback. In this case, the client application has to
2903
 * abort the connection or have a default application level protocol. 2) If
2904
 * the server supports NPN, but advertises an empty list then the client
2905
 * selects the first protocol in its list, but indicates via the API that this
2906
 * fallback case was enacted. 3) Otherwise, the client finds the first
2907
 * protocol in the server's list that it supports and selects this protocol.
2908
 * This is because it's assumed that the server has better information about
2909
 * which protocol a client should use. 4) If the client doesn't support any
2910
 * of the server's advertised protocols, then this is treated the same as
2911
 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
2912
 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
2913
 */
2914
int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
2915
    const unsigned char *server,
2916
    unsigned int server_len,
2917
    const unsigned char *client, unsigned int client_len)
2918
0
{
2919
0
    PACKET cpkt, csubpkt, spkt, ssubpkt;
2920
2921
0
    if (!PACKET_buf_init(&cpkt, client, client_len)
2922
0
        || !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
2923
0
        || PACKET_remaining(&csubpkt) == 0) {
2924
0
        *out = NULL;
2925
0
        *outlen = 0;
2926
0
        return OPENSSL_NPN_NO_OVERLAP;
2927
0
    }
2928
2929
    /*
2930
     * Set the default opportunistic protocol. Will be overwritten if we find
2931
     * a match.
2932
     */
2933
0
    *out = (unsigned char *)PACKET_data(&csubpkt);
2934
0
    *outlen = (unsigned char)PACKET_remaining(&csubpkt);
2935
2936
    /*
2937
     * For each protocol in server preference order, see if we support it.
2938
     */
2939
0
    if (PACKET_buf_init(&spkt, server, server_len)) {
2940
0
        while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
2941
0
            if (PACKET_remaining(&ssubpkt) == 0)
2942
0
                continue; /* Invalid - ignore it */
2943
0
            if (PACKET_buf_init(&cpkt, client, client_len)) {
2944
0
                while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
2945
0
                    if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
2946
0
                            PACKET_remaining(&ssubpkt))) {
2947
                        /* We found a match */
2948
0
                        *out = (unsigned char *)PACKET_data(&ssubpkt);
2949
0
                        *outlen = (unsigned char)PACKET_remaining(&ssubpkt);
2950
0
                        return OPENSSL_NPN_NEGOTIATED;
2951
0
                    }
2952
0
                }
2953
                /* Ignore spurious trailing bytes in the client list */
2954
0
            } else {
2955
                /* This should never happen */
2956
0
                return OPENSSL_NPN_NO_OVERLAP;
2957
0
            }
2958
0
        }
2959
        /* Ignore spurious trailing bytes in the server list */
2960
0
    }
2961
2962
    /*
2963
     * There's no overlap between our protocols and the server's list. We use
2964
     * the default opportunistic protocol selected earlier
2965
     */
2966
0
    return OPENSSL_NPN_NO_OVERLAP;
2967
0
}
2968
2969
#ifndef OPENSSL_NO_NEXTPROTONEG
2970
/*
2971
 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
2972
 * client's requested protocol for this connection and returns 0. If the
2973
 * client didn't request any protocol, then *data is set to NULL. Note that
2974
 * the client can request any protocol it chooses. The value returned from
2975
 * this function need not be a member of the list of supported protocols
2976
 * provided by the callback.
2977
 */
2978
void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
2979
    unsigned *len)
2980
0
{
2981
0
    *data = s->ext.npn;
2982
0
    if (*data == NULL) {
2983
0
        *len = 0;
2984
0
    } else {
2985
0
        *len = (unsigned int)s->ext.npn_len;
2986
0
    }
2987
0
}
2988
2989
/*
2990
 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
2991
 * a TLS server needs a list of supported protocols for Next Protocol
2992
 * Negotiation. The returned list must be in wire format.  The list is
2993
 * returned by setting |out| to point to it and |outlen| to its length. This
2994
 * memory will not be modified, but one should assume that the SSL* keeps a
2995
 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
2996
 * wishes to advertise. Otherwise, no such extension will be included in the
2997
 * ServerHello.
2998
 */
2999
void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
3000
    SSL_CTX_npn_advertised_cb_func cb,
3001
    void *arg)
3002
0
{
3003
0
    ctx->ext.npn_advertised_cb = cb;
3004
0
    ctx->ext.npn_advertised_cb_arg = arg;
3005
0
}
3006
3007
/*
3008
 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
3009
 * client needs to select a protocol from the server's provided list. |out|
3010
 * must be set to point to the selected protocol (which may be within |in|).
3011
 * The length of the protocol name must be written into |outlen|. The
3012
 * server's advertised protocols are provided in |in| and |inlen|. The
3013
 * callback can assume that |in| is syntactically valid. The client must
3014
 * select a protocol. It is fatal to the connection if this callback returns
3015
 * a value other than SSL_TLSEXT_ERR_OK.
3016
 */
3017
void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
3018
    SSL_CTX_npn_select_cb_func cb,
3019
    void *arg)
3020
0
{
3021
0
    ctx->ext.npn_select_cb = cb;
3022
0
    ctx->ext.npn_select_cb_arg = arg;
3023
0
}
3024
#endif
3025
3026
static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
3027
50.4k
{
3028
50.4k
    unsigned int idx;
3029
3030
50.4k
    if (protos_len < 2 || protos == NULL)
3031
0
        return 0;
3032
3033
100k
    for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
3034
50.4k
        if (protos[idx] == 0)
3035
0
            return 0;
3036
50.4k
    }
3037
50.4k
    return idx == protos_len;
3038
50.4k
}
3039
/*
3040
 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
3041
 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3042
 * length-prefixed strings). Returns 0 on success.
3043
 */
3044
int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
3045
    unsigned int protos_len)
3046
0
{
3047
0
    unsigned char *alpn;
3048
3049
0
    if (protos_len == 0 || protos == NULL) {
3050
0
        OPENSSL_free(ctx->ext.alpn);
3051
0
        ctx->ext.alpn = NULL;
3052
0
        ctx->ext.alpn_len = 0;
3053
0
        return 0;
3054
0
    }
3055
    /* Not valid per RFC */
3056
0
    if (!alpn_value_ok(protos, protos_len))
3057
0
        return 1;
3058
3059
0
    alpn = OPENSSL_memdup(protos, protos_len);
3060
0
    if (alpn == NULL) {
3061
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
3062
0
        return 1;
3063
0
    }
3064
0
    OPENSSL_free(ctx->ext.alpn);
3065
0
    ctx->ext.alpn = alpn;
3066
0
    ctx->ext.alpn_len = protos_len;
3067
3068
0
    return 0;
3069
0
}
3070
3071
/*
3072
 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
3073
 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3074
 * length-prefixed strings). Returns 0 on success.
3075
 */
3076
int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
3077
    unsigned int protos_len)
3078
{
3079
    unsigned char *alpn;
3080
3081
    if (protos_len == 0 || protos == NULL) {
3082
        OPENSSL_free(ssl->ext.alpn);
3083
        ssl->ext.alpn = NULL;
3084
        ssl->ext.alpn_len = 0;
3085
        return 0;
3086
    }
3087
    /* Not valid per RFC */
3088
    if (!alpn_value_ok(protos, protos_len))
3089
        return 1;
3090
3091
    alpn = OPENSSL_memdup(protos, protos_len);
3092
    if (alpn == NULL) {
3093
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
3094
        return 1;
3095
    }
3096
    OPENSSL_free(ssl->ext.alpn);
3097
    ssl->ext.alpn = alpn;
3098
    ssl->ext.alpn_len = protos_len;
3099
3100
    return 0;
3101
}
3102
3103
/*
3104
 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
3105
 * called during ClientHello processing in order to select an ALPN protocol
3106
 * from the client's list of offered protocols.
3107
 */
3108
void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
3109
    SSL_CTX_alpn_select_cb_func cb,
3110
    void *arg)
3111
245
{
3112
245
    ctx->ext.alpn_select_cb = cb;
3113
245
    ctx->ext.alpn_select_cb_arg = arg;
3114
245
}
3115
3116
/*
3117
 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
3118
 * On return it sets |*data| to point to |*len| bytes of protocol name
3119
 * (not including the leading length-prefix byte). If the server didn't
3120
 * respond with a negotiated protocol then |*len| will be zero.
3121
 */
3122
void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
3123
    unsigned int *len)
3124
{
3125
    *data = ssl->s3.alpn_selected;
3126
    if (*data == NULL)
3127
        *len = 0;
3128
    else
3129
        *len = (unsigned int)ssl->s3.alpn_selected_len;
3130
}
3131
3132
int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
3133
    const char *label, size_t llen,
3134
    const unsigned char *context, size_t contextlen,
3135
    int use_context)
3136
0
{
3137
0
    if (s->session == NULL
3138
0
        || (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER))
3139
0
        return -1;
3140
3141
0
    return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
3142
0
        llen, context,
3143
0
        contextlen, use_context);
3144
0
}
3145
3146
int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
3147
    const char *label, size_t llen,
3148
    const unsigned char *context,
3149
    size_t contextlen)
3150
0
{
3151
0
    if (s->version != TLS1_3_VERSION)
3152
0
        return 0;
3153
3154
0
    return tls13_export_keying_material_early(s, out, olen, label, llen,
3155
0
        context, contextlen);
3156
0
}
3157
3158
static unsigned long ssl_session_hash(const SSL_SESSION *a)
3159
95.8k
{
3160
95.8k
    const unsigned char *session_id = a->session_id;
3161
95.8k
    unsigned long l;
3162
95.8k
    unsigned char tmp_storage[4];
3163
3164
95.8k
    if (a->session_id_length < sizeof(tmp_storage)) {
3165
693
        memset(tmp_storage, 0, sizeof(tmp_storage));
3166
693
        memcpy(tmp_storage, a->session_id, a->session_id_length);
3167
693
        session_id = tmp_storage;
3168
693
    }
3169
3170
95.8k
    l = (unsigned long)((unsigned long)session_id[0]) | ((unsigned long)session_id[1] << 8L) | ((unsigned long)session_id[2] << 16L) | ((unsigned long)session_id[3] << 24L);
3171
95.8k
    return l;
3172
95.8k
}
3173
3174
/*
3175
 * NB: If this function (or indeed the hash function which uses a sort of
3176
 * coarser function than this one) is changed, ensure
3177
 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
3178
 * being able to construct an SSL_SESSION that will collide with any existing
3179
 * session with a matching session ID.
3180
 */
3181
static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
3182
4.33k
{
3183
4.33k
    if (a->ssl_version != b->ssl_version)
3184
0
        return 1;
3185
4.33k
    if (a->session_id_length != b->session_id_length)
3186
0
        return 1;
3187
4.33k
    return memcmp(a->session_id, b->session_id, a->session_id_length);
3188
4.33k
}
3189
3190
/*
3191
 * These wrapper functions should remain rather than redeclaring
3192
 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
3193
 * variable. The reason is that the functions aren't static, they're exposed
3194
 * via ssl.h.
3195
 */
3196
3197
SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
3198
    const SSL_METHOD *meth)
3199
11.4k
{
3200
11.4k
    SSL_CTX *ret = NULL;
3201
3202
11.4k
    if (meth == NULL) {
3203
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
3204
0
        return NULL;
3205
0
    }
3206
3207
11.4k
    if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
3208
0
        return NULL;
3209
3210
11.4k
    if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
3211
0
        ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
3212
0
        goto err;
3213
0
    }
3214
11.4k
    ret = OPENSSL_zalloc(sizeof(*ret));
3215
11.4k
    if (ret == NULL)
3216
0
        goto err;
3217
3218
    /* Init the reference counting before any call to SSL_CTX_free */
3219
11.4k
    ret->references = 1;
3220
11.4k
    ret->lock = CRYPTO_THREAD_lock_new();
3221
11.4k
    if (ret->lock == NULL) {
3222
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
3223
0
        OPENSSL_free(ret);
3224
0
        return NULL;
3225
0
    }
3226
3227
#ifdef TSAN_REQUIRES_LOCKING
3228
    ret->tsan_lock = CRYPTO_THREAD_lock_new();
3229
    if (ret->tsan_lock == NULL) {
3230
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
3231
        goto err;
3232
    }
3233
#endif
3234
3235
11.4k
    ret->libctx = libctx;
3236
11.4k
    if (propq != NULL) {
3237
0
        ret->propq = OPENSSL_strdup(propq);
3238
0
        if (ret->propq == NULL)
3239
0
            goto err;
3240
0
    }
3241
3242
11.4k
    ret->method = meth;
3243
11.4k
    ret->min_proto_version = 0;
3244
11.4k
    ret->max_proto_version = 0;
3245
11.4k
    ret->mode = SSL_MODE_AUTO_RETRY;
3246
11.4k
    ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
3247
11.4k
    ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
3248
    /* We take the system default. */
3249
11.4k
    ret->session_timeout = meth->get_timeout();
3250
11.4k
    ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
3251
11.4k
    ret->verify_mode = SSL_VERIFY_NONE;
3252
11.4k
    if ((ret->cert = ssl_cert_new()) == NULL)
3253
0
        goto err;
3254
3255
11.4k
    ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
3256
11.4k
    if (ret->sessions == NULL)
3257
0
        goto err;
3258
11.4k
    ret->cert_store = X509_STORE_new();
3259
11.4k
    if (ret->cert_store == NULL)
3260
0
        goto err;
3261
11.4k
#ifndef OPENSSL_NO_CT
3262
11.4k
    ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
3263
11.4k
    if (ret->ctlog_store == NULL)
3264
0
        goto err;
3265
11.4k
#endif
3266
3267
    /* initialize cipher/digest methods table */
3268
11.4k
    if (!ssl_load_ciphers(ret))
3269
0
        goto err2;
3270
    /* initialise sig algs */
3271
11.4k
    if (!ssl_setup_sig_algs(ret))
3272
0
        goto err2;
3273
3274
11.4k
    if (!ssl_load_groups(ret))
3275
0
        goto err2;
3276
3277
11.4k
    if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites()))
3278
0
        goto err;
3279
3280
11.4k
    if (!ssl_create_cipher_list(ret,
3281
11.4k
            ret->tls13_ciphersuites,
3282
11.4k
            &ret->cipher_list, &ret->cipher_list_by_id,
3283
11.4k
            OSSL_default_cipher_list(), ret->cert)
3284
11.4k
        || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
3285
0
        ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
3286
0
        goto err2;
3287
0
    }
3288
3289
11.4k
    ret->param = X509_VERIFY_PARAM_new();
3290
11.4k
    if (ret->param == NULL)
3291
0
        goto err;
3292
3293
    /*
3294
     * If these aren't available from the provider we'll get NULL returns.
3295
     * That's fine but will cause errors later if SSLv3 is negotiated
3296
     */
3297
11.4k
    ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
3298
11.4k
    ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
3299
3300
11.4k
    if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
3301
0
        goto err;
3302
3303
11.4k
    if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
3304
0
        goto err;
3305
3306
11.4k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
3307
0
        goto err;
3308
3309
11.4k
    if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
3310
0
        goto err;
3311
3312
    /* No compression for DTLS */
3313
11.4k
    if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
3314
11.4k
        ret->comp_methods = SSL_COMP_get_compression_methods();
3315
3316
11.4k
    ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
3317
11.4k
    ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
3318
3319
    /* Setup RFC5077 ticket keys */
3320
11.4k
    if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
3321
11.4k
             sizeof(ret->ext.tick_key_name), 0)
3322
11.4k
            <= 0)
3323
11.4k
        || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
3324
11.4k
                sizeof(ret->ext.secure->tick_hmac_key), 0)
3325
11.4k
            <= 0)
3326
11.4k
        || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
3327
11.4k
                sizeof(ret->ext.secure->tick_aes_key), 0)
3328
11.4k
            <= 0))
3329
0
        ret->options |= SSL_OP_NO_TICKET;
3330
3331
11.4k
    if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
3332
11.4k
            sizeof(ret->ext.cookie_hmac_key), 0)
3333
11.4k
        <= 0)
3334
0
        goto err;
3335
3336
11.4k
#ifndef OPENSSL_NO_SRP
3337
11.4k
    if (!ssl_ctx_srp_ctx_init_intern(ret))
3338
0
        goto err;
3339
11.4k
#endif
3340
11.4k
#ifndef OPENSSL_NO_ENGINE
3341
#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
3342
#define eng_strx(x) #x
3343
#define eng_str(x) eng_strx(x)
3344
    /* Use specific client engine automatically... ignore errors */
3345
    {
3346
        ENGINE *eng;
3347
        eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
3348
        if (!eng) {
3349
            ERR_clear_error();
3350
            ENGINE_load_builtin_engines();
3351
            eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
3352
        }
3353
        if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
3354
            ERR_clear_error();
3355
    }
3356
#endif
3357
11.4k
#endif
3358
    /*
3359
     * Disable compression by default to prevent CRIME. Applications can
3360
     * re-enable compression by configuring
3361
     * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
3362
     * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
3363
     * middlebox compatibility by default. This may be disabled by default in
3364
     * a later OpenSSL version.
3365
     */
3366
11.4k
    ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
3367
3368
11.4k
    ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
3369
3370
    /*
3371
     * We cannot usefully set a default max_early_data here (which gets
3372
     * propagated in SSL_new(), for the following reason: setting the
3373
     * SSL field causes tls_construct_stoc_early_data() to tell the
3374
     * client that early data will be accepted when constructing a TLS 1.3
3375
     * session ticket, and the client will accordingly send us early data
3376
     * when using that ticket (if the client has early data to send).
3377
     * However, in order for the early data to actually be consumed by
3378
     * the application, the application must also have calls to
3379
     * SSL_read_early_data(); otherwise we'll just skip past the early data
3380
     * and ignore it.  So, since the application must add calls to
3381
     * SSL_read_early_data(), we also require them to add
3382
     * calls to SSL_CTX_set_max_early_data() in order to use early data,
3383
     * eliminating the bandwidth-wasting early data in the case described
3384
     * above.
3385
     */
3386
11.4k
    ret->max_early_data = 0;
3387
3388
    /*
3389
     * Default recv_max_early_data is a fully loaded single record. Could be
3390
     * split across multiple records in practice. We set this differently to
3391
     * max_early_data so that, in the default case, we do not advertise any
3392
     * support for early_data, but if a client were to send us some (e.g.
3393
     * because of an old, stale ticket) then we will tolerate it and skip over
3394
     * it.
3395
     */
3396
11.4k
    ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
3397
3398
    /* By default we send two session tickets automatically in TLSv1.3 */
3399
11.4k
    ret->num_tickets = 2;
3400
3401
11.4k
    ssl_ctx_system_config(ret);
3402
3403
11.4k
    return ret;
3404
0
err:
3405
0
    ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
3406
0
err2:
3407
0
    SSL_CTX_free(ret);
3408
0
    return NULL;
3409
0
}
3410
3411
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
3412
163k
{
3413
163k
    return SSL_CTX_new_ex(NULL, NULL, meth);
3414
163k
}
3415
3416
int SSL_CTX_up_ref(SSL_CTX *ctx)
3417
389k
{
3418
389k
    int i;
3419
3420
389k
    if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
3421
0
        return 0;
3422
3423
389k
    REF_PRINT_COUNT("SSL_CTX", ctx);
3424
389k
    REF_ASSERT_ISNT(i < 2);
3425
389k
    return ((i > 1) ? 1 : 0);
3426
389k
}
3427
3428
void SSL_CTX_free(SSL_CTX *a)
3429
34.4k
{
3430
34.4k
    int i;
3431
34.4k
    size_t j;
3432
3433
34.4k
    if (a == NULL)
3434
0
        return;
3435
3436
34.4k
    CRYPTO_DOWN_REF(&a->references, &i, a->lock);
3437
34.4k
    REF_PRINT_COUNT("SSL_CTX", a);
3438
34.4k
    if (i > 0)
3439
22.9k
        return;
3440
11.4k
    REF_ASSERT_ISNT(i < 0);
3441
3442
11.4k
    X509_VERIFY_PARAM_free(a->param);
3443
11.4k
    dane_ctx_final(&a->dane);
3444
3445
    /*
3446
     * Free internal session cache. However: the remove_cb() may reference
3447
     * the ex_data of SSL_CTX, thus the ex_data store can only be removed
3448
     * after the sessions were flushed.
3449
     * As the ex_data handling routines might also touch the session cache,
3450
     * the most secure solution seems to be: empty (flush) the cache, then
3451
     * free ex_data, then finally free the cache.
3452
     * (See ticket [openssl.org #212].)
3453
     */
3454
11.4k
    if (a->sessions != NULL)
3455
11.4k
        SSL_CTX_flush_sessions(a, 0);
3456
3457
11.4k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
3458
11.4k
    lh_SSL_SESSION_free(a->sessions);
3459
11.4k
    X509_STORE_free(a->cert_store);
3460
11.4k
#ifndef OPENSSL_NO_CT
3461
11.4k
    CTLOG_STORE_free(a->ctlog_store);
3462
11.4k
#endif
3463
11.4k
    sk_SSL_CIPHER_free(a->cipher_list);
3464
11.4k
    sk_SSL_CIPHER_free(a->cipher_list_by_id);
3465
11.4k
    sk_SSL_CIPHER_free(a->tls13_ciphersuites);
3466
11.4k
    ssl_cert_free(a->cert);
3467
11.4k
    sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
3468
11.4k
    sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
3469
11.4k
    sk_X509_pop_free(a->extra_certs, X509_free);
3470
11.4k
    a->comp_methods = NULL;
3471
11.4k
#ifndef OPENSSL_NO_SRTP
3472
11.4k
    sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
3473
11.4k
#endif
3474
11.4k
#ifndef OPENSSL_NO_SRP
3475
11.4k
    ssl_ctx_srp_ctx_free_intern(a);
3476
11.4k
#endif
3477
11.4k
#ifndef OPENSSL_NO_ENGINE
3478
11.4k
    tls_engine_finish(a->client_cert_engine);
3479
11.4k
#endif
3480
3481
11.4k
    OPENSSL_free(a->ext.ecpointformats);
3482
11.4k
    OPENSSL_free(a->ext.supportedgroups);
3483
11.4k
    OPENSSL_free(a->ext.supported_groups_default);
3484
11.4k
    OPENSSL_free(a->ext.alpn);
3485
11.4k
    OPENSSL_secure_free(a->ext.secure);
3486
3487
11.4k
    ssl_evp_md_free(a->md5);
3488
11.4k
    ssl_evp_md_free(a->sha1);
3489
3490
286k
    for (j = 0; j < SSL_ENC_NUM_IDX; j++)
3491
275k
        ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
3492
172k
    for (j = 0; j < SSL_MD_NUM_IDX; j++)
3493
160k
        ssl_evp_md_free(a->ssl_digest_methods[j]);
3494
584k
    for (j = 0; j < a->group_list_len; j++) {
3495
573k
        OPENSSL_free(a->group_list[j].tlsname);
3496
573k
        OPENSSL_free(a->group_list[j].realname);
3497
573k
        OPENSSL_free(a->group_list[j].algorithm);
3498
573k
    }
3499
11.4k
    OPENSSL_free(a->group_list);
3500
3501
11.4k
    OPENSSL_free(a->sigalg_lookup_cache);
3502
3503
11.4k
    CRYPTO_THREAD_lock_free(a->lock);
3504
#ifdef TSAN_REQUIRES_LOCKING
3505
    CRYPTO_THREAD_lock_free(a->tsan_lock);
3506
#endif
3507
3508
11.4k
    OPENSSL_free(a->propq);
3509
3510
11.4k
    OPENSSL_free(a);
3511
11.4k
}
3512
3513
void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
3514
0
{
3515
0
    ctx->default_passwd_callback = cb;
3516
0
}
3517
3518
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
3519
0
{
3520
0
    ctx->default_passwd_callback_userdata = u;
3521
0
}
3522
3523
pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
3524
0
{
3525
0
    return ctx->default_passwd_callback;
3526
0
}
3527
3528
void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
3529
0
{
3530
0
    return ctx->default_passwd_callback_userdata;
3531
0
}
3532
3533
void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
3534
0
{
3535
0
    s->default_passwd_callback = cb;
3536
0
}
3537
3538
void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
3539
0
{
3540
0
    s->default_passwd_callback_userdata = u;
3541
0
}
3542
3543
pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
3544
0
{
3545
0
    return s->default_passwd_callback;
3546
0
}
3547
3548
void *SSL_get_default_passwd_cb_userdata(SSL *s)
3549
0
{
3550
0
    return s->default_passwd_callback_userdata;
3551
0
}
3552
3553
void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
3554
    int (*cb)(X509_STORE_CTX *, void *),
3555
    void *arg)
3556
0
{
3557
0
    ctx->app_verify_callback = cb;
3558
0
    ctx->app_verify_arg = arg;
3559
0
}
3560
3561
void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
3562
    int (*cb)(int, X509_STORE_CTX *))
3563
0
{
3564
0
    ctx->verify_mode = mode;
3565
0
    ctx->default_verify_callback = cb;
3566
0
}
3567
3568
void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
3569
0
{
3570
0
    X509_VERIFY_PARAM_set_depth(ctx->param, depth);
3571
0
}
3572
3573
void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
3574
0
{
3575
0
    ssl_cert_set_cert_cb(c->cert, cb, arg);
3576
0
}
3577
3578
void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
3579
0
{
3580
0
    ssl_cert_set_cert_cb(s->cert, cb, arg);
3581
0
}
3582
3583
void ssl_set_masks(SSL *s)
3584
2.35k
{
3585
2.35k
    CERT *c = s->cert;
3586
2.35k
    uint32_t *pvalid = s->s3.tmp.valid_flags;
3587
2.35k
    int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
3588
2.35k
    unsigned long mask_k, mask_a;
3589
2.35k
    int have_ecc_cert, ecdsa_ok;
3590
3591
2.35k
    if (c == NULL)
3592
0
        return;
3593
3594
2.35k
    dh_tmp = (c->dh_tmp != NULL
3595
2.35k
        || c->dh_tmp_cb != NULL
3596
2.35k
        || c->dh_tmp_auto);
3597
3598
2.35k
    rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
3599
2.35k
    rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
3600
2.35k
    dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
3601
2.35k
    have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
3602
2.35k
    mask_k = 0;
3603
2.35k
    mask_a = 0;
3604
3605
2.35k
    OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
3606
2.35k
        dh_tmp, rsa_enc, rsa_sign, dsa_sign);
3607
3608
2.35k
#ifndef OPENSSL_NO_GOST
3609
2.35k
    if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
3610
0
        mask_k |= SSL_kGOST | SSL_kGOST18;
3611
0
        mask_a |= SSL_aGOST12;
3612
0
    }
3613
2.35k
    if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
3614
0
        mask_k |= SSL_kGOST | SSL_kGOST18;
3615
0
        mask_a |= SSL_aGOST12;
3616
0
    }
3617
2.35k
    if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
3618
0
        mask_k |= SSL_kGOST;
3619
0
        mask_a |= SSL_aGOST01;
3620
0
    }
3621
2.35k
#endif
3622
3623
2.35k
    if (rsa_enc)
3624
2.35k
        mask_k |= SSL_kRSA;
3625
3626
2.35k
    if (dh_tmp)
3627
0
        mask_k |= SSL_kDHE;
3628
3629
    /*
3630
     * If we only have an RSA-PSS certificate allow RSA authentication
3631
     * if TLS 1.2 and peer supports it.
3632
     */
3633
3634
2.35k
    if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN) && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN && TLS1_get_version(s) == TLS1_2_VERSION))
3635
2.35k
        mask_a |= SSL_aRSA;
3636
3637
2.35k
    if (dsa_sign) {
3638
2.35k
        mask_a |= SSL_aDSS;
3639
2.35k
    }
3640
3641
2.35k
    mask_a |= SSL_aNULL;
3642
3643
    /*
3644
     * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
3645
     * depending on the key usage extension.
3646
     */
3647
2.35k
    if (have_ecc_cert) {
3648
1.73k
        uint32_t ex_kusage;
3649
1.73k
        ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
3650
1.73k
        ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
3651
1.73k
        if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
3652
141
            ecdsa_ok = 0;
3653
1.73k
        if (ecdsa_ok)
3654
1.59k
            mask_a |= SSL_aECDSA;
3655
1.73k
    }
3656
    /* Allow Ed25519 for TLS 1.2 if peer supports it */
3657
2.35k
    if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
3658
0
        && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
3659
0
        && TLS1_get_version(s) == TLS1_2_VERSION)
3660
0
        mask_a |= SSL_aECDSA;
3661
3662
    /* Allow Ed448 for TLS 1.2 if peer supports it */
3663
2.35k
    if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
3664
0
        && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
3665
0
        && TLS1_get_version(s) == TLS1_2_VERSION)
3666
0
        mask_a |= SSL_aECDSA;
3667
3668
2.35k
    mask_k |= SSL_kECDHE;
3669
3670
2.35k
#ifndef OPENSSL_NO_PSK
3671
2.35k
    mask_k |= SSL_kPSK;
3672
2.35k
    mask_a |= SSL_aPSK;
3673
2.35k
    if (mask_k & SSL_kRSA)
3674
2.35k
        mask_k |= SSL_kRSAPSK;
3675
2.35k
    if (mask_k & SSL_kDHE)
3676
0
        mask_k |= SSL_kDHEPSK;
3677
2.35k
    if (mask_k & SSL_kECDHE)
3678
2.35k
        mask_k |= SSL_kECDHEPSK;
3679
2.35k
#endif
3680
3681
2.35k
    s->s3.tmp.mask_k = mask_k;
3682
2.35k
    s->s3.tmp.mask_a = mask_a;
3683
2.35k
}
3684
3685
int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
3686
0
{
3687
0
    if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
3688
        /* key usage, if present, must allow signing */
3689
0
        if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
3690
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
3691
0
            return 0;
3692
0
        }
3693
0
    }
3694
0
    return 1; /* all checks are ok */
3695
0
}
3696
3697
int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
3698
    size_t *serverinfo_length)
3699
0
{
3700
0
    CERT_PKEY *cpk = s->s3.tmp.cert;
3701
0
    *serverinfo_length = 0;
3702
3703
0
    if (cpk == NULL || cpk->serverinfo == NULL)
3704
0
        return 0;
3705
3706
0
    *serverinfo = cpk->serverinfo;
3707
0
    *serverinfo_length = cpk->serverinfo_length;
3708
0
    return 1;
3709
0
}
3710
3711
void ssl_update_cache(SSL *s, int mode)
3712
2.02k
{
3713
2.02k
    int i;
3714
3715
    /*
3716
     * If the session_id_length is 0, we are not supposed to cache it, and it
3717
     * would be rather hard to do anyway :-). Also if the session has already
3718
     * been marked as not_resumable we should not cache it for later reuse.
3719
     */
3720
2.02k
    if (s->session->session_id_length == 0 || s->session->not_resumable)
3721
165
        return;
3722
3723
    /*
3724
     * If sid_ctx_length is 0 there is no specific application context
3725
     * associated with this session, so when we try to resume it and
3726
     * SSL_VERIFY_PEER is requested to verify the client identity, we have no
3727
     * indication that this is actually a session for the proper application
3728
     * context, and the *handshake* will fail, not just the resumption attempt.
3729
     * Do not cache (on the server) these sessions that are not resumable
3730
     * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
3731
     */
3732
1.85k
    if (s->server && s->session->sid_ctx_length == 0
3733
540
        && (s->verify_mode & SSL_VERIFY_PEER) != 0)
3734
0
        return;
3735
3736
1.85k
    i = s->session_ctx->session_cache_mode;
3737
1.85k
    if ((i & mode) != 0
3738
540
        && (!s->hit || SSL_IS_TLS13(s))) {
3739
        /*
3740
         * Add the session to the internal cache. In server side TLSv1.3 we
3741
         * normally don't do this because by default it's a full stateless ticket
3742
         * with only a dummy session id so there is no reason to cache it,
3743
         * unless:
3744
         * - we are doing early_data, in which case we cache so that we can
3745
         *   detect replays
3746
         * - the application has set a remove_session_cb so needs to know about
3747
         *   session timeout events
3748
         * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
3749
         */
3750
540
        if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
3751
540
            && (!SSL_IS_TLS13(s)
3752
0
                || !s->server
3753
0
                || (s->max_early_data > 0
3754
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
3755
0
                || s->session_ctx->remove_session_cb != NULL
3756
0
                || (s->options & SSL_OP_NO_TICKET) != 0))
3757
540
            SSL_CTX_add_session(s->session_ctx, s->session);
3758
3759
        /*
3760
         * Add the session to the external cache. We do this even in server side
3761
         * TLSv1.3 without early data because some applications just want to
3762
         * know about the creation of a session and aren't doing a full cache.
3763
         */
3764
540
        if (s->session_ctx->new_session_cb != NULL) {
3765
0
            SSL_SESSION_up_ref(s->session);
3766
0
            if (!s->session_ctx->new_session_cb(s, s->session))
3767
0
                SSL_SESSION_free(s->session);
3768
0
        }
3769
540
    }
3770
3771
    /* auto flush every 255 connections */
3772
1.85k
    if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
3773
540
        TSAN_QUALIFIER int *stat;
3774
3775
540
        if (mode & SSL_SESS_CACHE_CLIENT)
3776
0
            stat = &s->session_ctx->stats.sess_connect_good;
3777
540
        else
3778
540
            stat = &s->session_ctx->stats.sess_accept_good;
3779
540
        if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
3780
0
            SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
3781
540
    }
3782
1.85k
}
3783
3784
const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3785
0
{
3786
0
    return ctx->method;
3787
0
}
3788
3789
const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
3790
0
{
3791
0
    return s->method;
3792
0
}
3793
3794
int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
3795
0
{
3796
0
    int ret = 1;
3797
3798
0
    if (s->method != meth) {
3799
0
        const SSL_METHOD *sm = s->method;
3800
0
        int (*hf)(SSL *) = s->handshake_func;
3801
3802
0
        if (sm->version == meth->version)
3803
0
            s->method = meth;
3804
0
        else {
3805
0
            sm->ssl_free(s);
3806
0
            s->method = meth;
3807
0
            ret = s->method->ssl_new(s);
3808
0
        }
3809
3810
0
        if (hf == sm->ssl_connect)
3811
0
            s->handshake_func = meth->ssl_connect;
3812
0
        else if (hf == sm->ssl_accept)
3813
0
            s->handshake_func = meth->ssl_accept;
3814
0
    }
3815
0
    return ret;
3816
0
}
3817
3818
int SSL_get_error(const SSL *s, int i)
3819
0
{
3820
0
    int reason;
3821
0
    unsigned long l;
3822
0
    BIO *bio;
3823
3824
0
    if (i > 0)
3825
0
        return SSL_ERROR_NONE;
3826
3827
    /*
3828
     * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
3829
     * where we do encode the error
3830
     */
3831
0
    if ((l = ERR_peek_error()) != 0) {
3832
0
        if (ERR_GET_LIB(l) == ERR_LIB_SYS)
3833
0
            return SSL_ERROR_SYSCALL;
3834
0
        else
3835
0
            return SSL_ERROR_SSL;
3836
0
    }
3837
3838
0
    if (SSL_want_read(s)) {
3839
0
        bio = SSL_get_rbio(s);
3840
0
        if (BIO_should_read(bio))
3841
0
            return SSL_ERROR_WANT_READ;
3842
0
        else if (BIO_should_write(bio))
3843
            /*
3844
             * This one doesn't make too much sense ... We never try to write
3845
             * to the rbio, and an application program where rbio and wbio
3846
             * are separate couldn't even know what it should wait for.
3847
             * However if we ever set s->rwstate incorrectly (so that we have
3848
             * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
3849
             * wbio *are* the same, this test works around that bug; so it
3850
             * might be safer to keep it.
3851
             */
3852
0
            return SSL_ERROR_WANT_WRITE;
3853
0
        else if (BIO_should_io_special(bio)) {
3854
0
            reason = BIO_get_retry_reason(bio);
3855
0
            if (reason == BIO_RR_CONNECT)
3856
0
                return SSL_ERROR_WANT_CONNECT;
3857
0
            else if (reason == BIO_RR_ACCEPT)
3858
0
                return SSL_ERROR_WANT_ACCEPT;
3859
0
            else
3860
0
                return SSL_ERROR_SYSCALL; /* unknown */
3861
0
        }
3862
0
    }
3863
3864
0
    if (SSL_want_write(s)) {
3865
        /* Access wbio directly - in order to use the buffered bio if present */
3866
0
        bio = s->wbio;
3867
0
        if (BIO_should_write(bio))
3868
0
            return SSL_ERROR_WANT_WRITE;
3869
0
        else if (BIO_should_read(bio))
3870
            /*
3871
             * See above (SSL_want_read(s) with BIO_should_write(bio))
3872
             */
3873
0
            return SSL_ERROR_WANT_READ;
3874
0
        else if (BIO_should_io_special(bio)) {
3875
0
            reason = BIO_get_retry_reason(bio);
3876
0
            if (reason == BIO_RR_CONNECT)
3877
0
                return SSL_ERROR_WANT_CONNECT;
3878
0
            else if (reason == BIO_RR_ACCEPT)
3879
0
                return SSL_ERROR_WANT_ACCEPT;
3880
0
            else
3881
0
                return SSL_ERROR_SYSCALL;
3882
0
        }
3883
0
    }
3884
0
    if (SSL_want_x509_lookup(s))
3885
0
        return SSL_ERROR_WANT_X509_LOOKUP;
3886
0
    if (SSL_want_retry_verify(s))
3887
0
        return SSL_ERROR_WANT_RETRY_VERIFY;
3888
0
    if (SSL_want_async(s))
3889
0
        return SSL_ERROR_WANT_ASYNC;
3890
0
    if (SSL_want_async_job(s))
3891
0
        return SSL_ERROR_WANT_ASYNC_JOB;
3892
0
    if (SSL_want_client_hello_cb(s))
3893
0
        return SSL_ERROR_WANT_CLIENT_HELLO_CB;
3894
3895
0
    if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && (s->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
3896
0
        return SSL_ERROR_ZERO_RETURN;
3897
3898
0
    return SSL_ERROR_SYSCALL;
3899
0
}
3900
3901
static int ssl_do_handshake_intern(void *vargs)
3902
0
{
3903
0
    struct ssl_async_args *args;
3904
0
    SSL *s;
3905
3906
0
    args = (struct ssl_async_args *)vargs;
3907
0
    s = args->s;
3908
3909
0
    return s->handshake_func(s);
3910
0
}
3911
3912
int SSL_do_handshake(SSL *s)
3913
13.5k
{
3914
13.5k
    int ret = 1;
3915
3916
13.5k
    if (s->handshake_func == NULL) {
3917
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
3918
0
        return -1;
3919
0
    }
3920
3921
13.5k
    ossl_statem_check_finish_init(s, -1);
3922
3923
13.5k
    s->method->ssl_renegotiate_check(s, 0);
3924
3925
13.5k
    if (SSL_in_init(s) || SSL_in_before(s)) {
3926
13.5k
        if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
3927
0
            struct ssl_async_args args;
3928
3929
0
            memset(&args, 0, sizeof(args));
3930
0
            args.s = s;
3931
3932
0
            ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
3933
13.5k
        } else {
3934
13.5k
            ret = s->handshake_func(s);
3935
13.5k
        }
3936
13.5k
    }
3937
13.5k
    return ret;
3938
13.5k
}
3939
3940
void SSL_set_accept_state(SSL *s)
3941
4.77k
{
3942
4.77k
    s->server = 1;
3943
4.77k
    s->shutdown = 0;
3944
4.77k
    ossl_statem_clear(s);
3945
4.77k
    s->handshake_func = s->method->ssl_accept;
3946
4.77k
    clear_ciphers(s);
3947
4.77k
}
3948
3949
void SSL_set_connect_state(SSL *s)
3950
6.69k
{
3951
6.69k
    s->server = 0;
3952
6.69k
    s->shutdown = 0;
3953
6.69k
    ossl_statem_clear(s);
3954
6.69k
    s->handshake_func = s->method->ssl_connect;
3955
6.69k
    clear_ciphers(s);
3956
6.69k
}
3957
3958
int ssl_undefined_function(SSL *s)
3959
0
{
3960
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3961
0
    return 0;
3962
0
}
3963
3964
int ssl_undefined_void_function(void)
3965
0
{
3966
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3967
0
    return 0;
3968
0
}
3969
3970
int ssl_undefined_const_function(const SSL *s)
3971
0
{
3972
0
    return 0;
3973
0
}
3974
3975
const SSL_METHOD *ssl_bad_method(int ver)
3976
0
{
3977
0
    ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
3978
0
    return NULL;
3979
0
}
3980
3981
const char *ssl_protocol_to_string(int version)
3982
2.18k
{
3983
2.18k
    switch (version) {
3984
129
    case TLS1_3_VERSION:
3985
129
        return "TLSv1.3";
3986
3987
346
    case TLS1_2_VERSION:
3988
346
        return "TLSv1.2";
3989
3990
53
    case TLS1_1_VERSION:
3991
53
        return "TLSv1.1";
3992
3993
14
    case TLS1_VERSION:
3994
14
        return "TLSv1";
3995
3996
62
    case SSL3_VERSION:
3997
62
        return "SSLv3";
3998
3999
14
    case DTLS1_BAD_VER:
4000
14
        return "DTLSv0.9";
4001
4002
25
    case DTLS1_VERSION:
4003
25
        return "DTLSv1";
4004
4005
31
    case DTLS1_2_VERSION:
4006
31
        return "DTLSv1.2";
4007
4008
1.51k
    default:
4009
1.51k
        return "unknown";
4010
2.18k
    }
4011
2.18k
}
4012
4013
const char *SSL_get_version(const SSL *s)
4014
0
{
4015
0
    return ssl_protocol_to_string(s->version);
4016
0
}
4017
4018
static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
4019
0
{
4020
0
    STACK_OF(X509_NAME) *sk;
4021
0
    X509_NAME *xn;
4022
0
    int i;
4023
4024
0
    if (src == NULL) {
4025
0
        *dst = NULL;
4026
0
        return 1;
4027
0
    }
4028
4029
0
    if ((sk = sk_X509_NAME_new_null()) == NULL)
4030
0
        return 0;
4031
0
    for (i = 0; i < sk_X509_NAME_num(src); i++) {
4032
0
        xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
4033
0
        if (xn == NULL) {
4034
0
            sk_X509_NAME_pop_free(sk, X509_NAME_free);
4035
0
            return 0;
4036
0
        }
4037
0
        if (sk_X509_NAME_insert(sk, xn, i) == 0) {
4038
0
            X509_NAME_free(xn);
4039
0
            sk_X509_NAME_pop_free(sk, X509_NAME_free);
4040
0
            return 0;
4041
0
        }
4042
0
    }
4043
0
    *dst = sk;
4044
4045
0
    return 1;
4046
0
}
4047
4048
SSL *SSL_dup(SSL *s)
4049
0
{
4050
0
    SSL *ret;
4051
0
    int i;
4052
4053
    /* If we're not quiescent, just up_ref! */
4054
0
    if (!SSL_in_init(s) || !SSL_in_before(s)) {
4055
0
        CRYPTO_UP_REF(&s->references, &i, s->lock);
4056
0
        return s;
4057
0
    }
4058
4059
    /*
4060
     * Otherwise, copy configuration state, and session if set.
4061
     */
4062
0
    if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
4063
0
        return NULL;
4064
4065
0
    if (s->session != NULL) {
4066
        /*
4067
         * Arranges to share the same session via up_ref.  This "copies"
4068
         * session-id, SSL_METHOD, sid_ctx, and 'cert'
4069
         */
4070
0
        if (!SSL_copy_session_id(ret, s))
4071
0
            goto err;
4072
0
    } else {
4073
        /*
4074
         * No session has been established yet, so we have to expect that
4075
         * s->cert or ret->cert will be changed later -- they should not both
4076
         * point to the same object, and thus we can't use
4077
         * SSL_copy_session_id.
4078
         */
4079
0
        if (!SSL_set_ssl_method(ret, s->method))
4080
0
            goto err;
4081
4082
0
        if (s->cert != NULL) {
4083
0
            ssl_cert_free(ret->cert);
4084
0
            ret->cert = ssl_cert_dup(s->cert);
4085
0
            if (ret->cert == NULL)
4086
0
                goto err;
4087
0
        }
4088
4089
0
        if (!SSL_set_session_id_context(ret, s->sid_ctx,
4090
0
                (int)s->sid_ctx_length))
4091
0
            goto err;
4092
0
    }
4093
4094
0
    if (!ssl_dane_dup(ret, s))
4095
0
        goto err;
4096
0
    ret->version = s->version;
4097
0
    ret->options = s->options;
4098
0
    ret->min_proto_version = s->min_proto_version;
4099
0
    ret->max_proto_version = s->max_proto_version;
4100
0
    ret->mode = s->mode;
4101
0
    SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
4102
0
    SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
4103
0
    ret->msg_callback = s->msg_callback;
4104
0
    ret->msg_callback_arg = s->msg_callback_arg;
4105
0
    SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
4106
0
    SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
4107
0
    ret->generate_session_id = s->generate_session_id;
4108
4109
0
    SSL_set_info_callback(ret, SSL_get_info_callback(s));
4110
4111
    /* copy app data, a little dangerous perhaps */
4112
0
    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
4113
0
        goto err;
4114
4115
0
    ret->server = s->server;
4116
0
    if (s->handshake_func) {
4117
0
        if (s->server)
4118
0
            SSL_set_accept_state(ret);
4119
0
        else
4120
0
            SSL_set_connect_state(ret);
4121
0
    }
4122
0
    ret->shutdown = s->shutdown;
4123
0
    ret->hit = s->hit;
4124
4125
0
    ret->default_passwd_callback = s->default_passwd_callback;
4126
0
    ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
4127
4128
0
    X509_VERIFY_PARAM_inherit(ret->param, s->param);
4129
4130
    /* dup the cipher_list and cipher_list_by_id stacks */
4131
0
    if (s->cipher_list != NULL) {
4132
0
        if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
4133
0
            goto err;
4134
0
    }
4135
0
    if (s->cipher_list_by_id != NULL)
4136
0
        if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
4137
0
            == NULL)
4138
0
            goto err;
4139
4140
    /* Dup the client_CA list */
4141
0
    if (!dup_ca_names(&ret->ca_names, s->ca_names)
4142
0
        || !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
4143
0
        goto err;
4144
4145
0
    return ret;
4146
4147
0
err:
4148
0
    SSL_free(ret);
4149
0
    return NULL;
4150
0
}
4151
4152
void ssl_clear_cipher_ctx(SSL *s)
4153
45.8k
{
4154
45.8k
    if (s->enc_read_ctx != NULL) {
4155
1.42k
        EVP_CIPHER_CTX_free(s->enc_read_ctx);
4156
1.42k
        s->enc_read_ctx = NULL;
4157
1.42k
    }
4158
45.8k
    if (s->enc_write_ctx != NULL) {
4159
1.73k
        EVP_CIPHER_CTX_free(s->enc_write_ctx);
4160
1.73k
        s->enc_write_ctx = NULL;
4161
1.73k
    }
4162
45.8k
#ifndef OPENSSL_NO_COMP
4163
45.8k
    COMP_CTX_free(s->expand);
4164
45.8k
    s->expand = NULL;
4165
45.8k
    COMP_CTX_free(s->compress);
4166
45.8k
    s->compress = NULL;
4167
45.8k
#endif
4168
45.8k
}
4169
4170
X509 *SSL_get_certificate(const SSL *s)
4171
0
{
4172
0
    if (s->cert != NULL)
4173
0
        return s->cert->key->x509;
4174
0
    else
4175
0
        return NULL;
4176
0
}
4177
4178
EVP_PKEY *SSL_get_privatekey(const SSL *s)
4179
0
{
4180
0
    if (s->cert != NULL)
4181
0
        return s->cert->key->privatekey;
4182
0
    else
4183
0
        return NULL;
4184
0
}
4185
4186
X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
4187
0
{
4188
0
    if (ctx->cert != NULL)
4189
0
        return ctx->cert->key->x509;
4190
0
    else
4191
0
        return NULL;
4192
0
}
4193
4194
EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
4195
0
{
4196
0
    if (ctx->cert != NULL)
4197
0
        return ctx->cert->key->privatekey;
4198
0
    else
4199
0
        return NULL;
4200
0
}
4201
4202
const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
4203
0
{
4204
0
    if ((s->session != NULL) && (s->session->cipher != NULL))
4205
0
        return s->session->cipher;
4206
0
    return NULL;
4207
0
}
4208
4209
const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
4210
0
{
4211
0
    return s->s3.tmp.new_cipher;
4212
0
}
4213
4214
const COMP_METHOD *SSL_get_current_compression(const SSL *s)
4215
0
{
4216
0
#ifndef OPENSSL_NO_COMP
4217
0
    return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
4218
#else
4219
    return NULL;
4220
#endif
4221
0
}
4222
4223
const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
4224
0
{
4225
0
#ifndef OPENSSL_NO_COMP
4226
0
    return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
4227
#else
4228
    return NULL;
4229
#endif
4230
0
}
4231
4232
int ssl_init_wbio_buffer(SSL *s)
4233
202k
{
4234
202k
    BIO *bbio;
4235
4236
202k
    if (s->bbio != NULL) {
4237
        /* Already buffered. */
4238
0
        return 1;
4239
0
    }
4240
4241
202k
    bbio = BIO_new(BIO_f_buffer());
4242
202k
    if (bbio == NULL || BIO_set_read_buffer_size(bbio, 1) <= 0) {
4243
0
        BIO_free(bbio);
4244
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
4245
0
        return 0;
4246
0
    }
4247
202k
    s->bbio = bbio;
4248
202k
    s->wbio = BIO_push(bbio, s->wbio);
4249
4250
202k
    return 1;
4251
202k
}
4252
4253
int ssl_free_wbio_buffer(SSL *s)
4254
863k
{
4255
    /* callers ensure s is never null */
4256
863k
    if (s->bbio == NULL)
4257
661k
        return 1;
4258
4259
202k
    s->wbio = BIO_pop(s->wbio);
4260
202k
    BIO_free(s->bbio);
4261
202k
    s->bbio = NULL;
4262
4263
202k
    return 1;
4264
863k
}
4265
4266
void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
4267
0
{
4268
0
    ctx->quiet_shutdown = mode;
4269
0
}
4270
4271
int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
4272
0
{
4273
0
    return ctx->quiet_shutdown;
4274
0
}
4275
4276
void SSL_set_quiet_shutdown(SSL *s, int mode)
4277
0
{
4278
0
    s->quiet_shutdown = mode;
4279
0
}
4280
4281
int SSL_get_quiet_shutdown(const SSL *s)
4282
0
{
4283
0
    return s->quiet_shutdown;
4284
0
}
4285
4286
void SSL_set_shutdown(SSL *s, int mode)
4287
0
{
4288
0
    s->shutdown = mode;
4289
0
}
4290
4291
int SSL_get_shutdown(const SSL *s)
4292
0
{
4293
0
    return s->shutdown;
4294
0
}
4295
4296
int SSL_version(const SSL *s)
4297
70.8k
{
4298
70.8k
    return s->version;
4299
70.8k
}
4300
4301
int SSL_client_version(const SSL *s)
4302
0
{
4303
0
    return s->client_version;
4304
0
}
4305
4306
SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
4307
0
{
4308
0
    return ssl->ctx;
4309
0
}
4310
4311
SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
4312
0
{
4313
0
    CERT *new_cert;
4314
0
    if (ssl->ctx == ctx)
4315
0
        return ssl->ctx;
4316
0
    if (ctx == NULL)
4317
0
        ctx = ssl->session_ctx;
4318
0
    new_cert = ssl_cert_dup(ctx->cert);
4319
0
    if (new_cert == NULL) {
4320
0
        return NULL;
4321
0
    }
4322
4323
0
    if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
4324
0
        ssl_cert_free(new_cert);
4325
0
        return NULL;
4326
0
    }
4327
4328
0
    ssl_cert_free(ssl->cert);
4329
0
    ssl->cert = new_cert;
4330
4331
    /*
4332
     * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
4333
     * so setter APIs must prevent invalid lengths from entering the system.
4334
     */
4335
0
    if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
4336
0
        return NULL;
4337
4338
    /*
4339
     * If the session ID context matches that of the parent SSL_CTX,
4340
     * inherit it from the new SSL_CTX as well. If however the context does
4341
     * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
4342
     * leave it unchanged.
4343
     */
4344
0
    if ((ssl->ctx != NULL) && (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) && (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
4345
0
        ssl->sid_ctx_length = ctx->sid_ctx_length;
4346
0
        memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
4347
0
    }
4348
4349
0
    SSL_CTX_up_ref(ctx);
4350
0
    SSL_CTX_free(ssl->ctx); /* decrement reference count */
4351
0
    ssl->ctx = ctx;
4352
4353
0
    return ssl->ctx;
4354
0
}
4355
4356
int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
4357
0
{
4358
0
    return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
4359
0
        ctx->propq);
4360
0
}
4361
4362
int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
4363
0
{
4364
0
    X509_LOOKUP *lookup;
4365
4366
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
4367
0
    if (lookup == NULL)
4368
0
        return 0;
4369
4370
    /* We ignore errors, in case the directory doesn't exist */
4371
0
    ERR_set_mark();
4372
4373
0
    X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
4374
4375
0
    ERR_pop_to_mark();
4376
4377
0
    return 1;
4378
0
}
4379
4380
int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
4381
0
{
4382
0
    X509_LOOKUP *lookup;
4383
4384
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
4385
0
    if (lookup == NULL)
4386
0
        return 0;
4387
4388
    /* We ignore errors, in case the file doesn't exist */
4389
0
    ERR_set_mark();
4390
4391
0
    X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
4392
0
        ctx->propq);
4393
4394
0
    ERR_pop_to_mark();
4395
4396
0
    return 1;
4397
0
}
4398
4399
int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
4400
0
{
4401
0
    X509_LOOKUP *lookup;
4402
4403
0
    lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
4404
0
    if (lookup == NULL)
4405
0
        return 0;
4406
4407
    /* We ignore errors, in case the directory doesn't exist */
4408
0
    ERR_set_mark();
4409
4410
0
    X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
4411
4412
0
    ERR_pop_to_mark();
4413
4414
0
    return 1;
4415
0
}
4416
4417
int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
4418
0
{
4419
0
    return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
4420
0
        ctx->propq);
4421
0
}
4422
4423
int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
4424
0
{
4425
0
    return X509_STORE_load_path(ctx->cert_store, CApath);
4426
0
}
4427
4428
int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
4429
0
{
4430
0
    return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
4431
0
        ctx->propq);
4432
0
}
4433
4434
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
4435
    const char *CApath)
4436
0
{
4437
0
    if (CAfile == NULL && CApath == NULL)
4438
0
        return 0;
4439
0
    if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
4440
0
        return 0;
4441
0
    if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
4442
0
        return 0;
4443
0
    return 1;
4444
0
}
4445
4446
void SSL_set_info_callback(SSL *ssl,
4447
    void (*cb)(const SSL *ssl, int type, int val))
4448
0
{
4449
0
    ssl->info_callback = cb;
4450
0
}
4451
4452
/*
4453
 * One compiler (Diab DCC) doesn't like argument names in returned function
4454
 * pointer.
4455
 */
4456
void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /* ssl */,
4457
    int /* type */,
4458
    int /* val */)
4459
0
{
4460
0
    return ssl->info_callback;
4461
0
}
4462
4463
void SSL_set_verify_result(SSL *ssl, long arg)
4464
0
{
4465
0
    ssl->verify_result = arg;
4466
0
}
4467
4468
long SSL_get_verify_result(const SSL *ssl)
4469
0
{
4470
0
    return ssl->verify_result;
4471
0
}
4472
4473
size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
4474
0
{
4475
0
    if (outlen == 0)
4476
0
        return sizeof(ssl->s3.client_random);
4477
0
    if (outlen > sizeof(ssl->s3.client_random))
4478
0
        outlen = sizeof(ssl->s3.client_random);
4479
0
    memcpy(out, ssl->s3.client_random, outlen);
4480
0
    return outlen;
4481
0
}
4482
4483
size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
4484
0
{
4485
0
    if (outlen == 0)
4486
0
        return sizeof(ssl->s3.server_random);
4487
0
    if (outlen > sizeof(ssl->s3.server_random))
4488
0
        outlen = sizeof(ssl->s3.server_random);
4489
0
    memcpy(out, ssl->s3.server_random, outlen);
4490
0
    return outlen;
4491
0
}
4492
4493
size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
4494
    unsigned char *out, size_t outlen)
4495
0
{
4496
0
    if (outlen == 0)
4497
0
        return session->master_key_length;
4498
0
    if (outlen > session->master_key_length)
4499
0
        outlen = session->master_key_length;
4500
0
    memcpy(out, session->master_key, outlen);
4501
0
    return outlen;
4502
0
}
4503
4504
int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
4505
    size_t len)
4506
0
{
4507
0
    if (len > sizeof(sess->master_key))
4508
0
        return 0;
4509
4510
0
    memcpy(sess->master_key, in, len);
4511
0
    sess->master_key_length = len;
4512
0
    return 1;
4513
0
}
4514
4515
int SSL_set_ex_data(SSL *s, int idx, void *arg)
4516
0
{
4517
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
4518
0
}
4519
4520
void *SSL_get_ex_data(const SSL *s, int idx)
4521
0
{
4522
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
4523
0
}
4524
4525
int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
4526
0
{
4527
0
    return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
4528
0
}
4529
4530
void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
4531
0
{
4532
0
    return CRYPTO_get_ex_data(&s->ex_data, idx);
4533
0
}
4534
4535
X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
4536
0
{
4537
0
    return ctx->cert_store;
4538
0
}
4539
4540
void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
4541
0
{
4542
0
    X509_STORE_free(ctx->cert_store);
4543
0
    ctx->cert_store = store;
4544
0
}
4545
4546
void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
4547
0
{
4548
0
    if (store != NULL)
4549
0
        X509_STORE_up_ref(store);
4550
0
    SSL_CTX_set_cert_store(ctx, store);
4551
0
}
4552
4553
int SSL_want(const SSL *s)
4554
0
{
4555
0
    return s->rwstate;
4556
0
}
4557
4558
#ifndef OPENSSL_NO_PSK
4559
int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
4560
0
{
4561
0
    if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
4562
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
4563
0
        return 0;
4564
0
    }
4565
0
    OPENSSL_free(ctx->cert->psk_identity_hint);
4566
0
    if (identity_hint != NULL) {
4567
0
        ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
4568
0
        if (ctx->cert->psk_identity_hint == NULL)
4569
0
            return 0;
4570
0
    } else
4571
0
        ctx->cert->psk_identity_hint = NULL;
4572
0
    return 1;
4573
0
}
4574
4575
int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
4576
0
{
4577
0
    if (s == NULL)
4578
0
        return 0;
4579
4580
0
    if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
4581
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
4582
0
        return 0;
4583
0
    }
4584
0
    OPENSSL_free(s->cert->psk_identity_hint);
4585
0
    if (identity_hint != NULL) {
4586
0
        s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
4587
0
        if (s->cert->psk_identity_hint == NULL)
4588
0
            return 0;
4589
0
    } else
4590
0
        s->cert->psk_identity_hint = NULL;
4591
0
    return 1;
4592
0
}
4593
4594
const char *SSL_get_psk_identity_hint(const SSL *s)
4595
0
{
4596
0
    if (s == NULL || s->session == NULL)
4597
0
        return NULL;
4598
0
    return s->session->psk_identity_hint;
4599
0
}
4600
4601
const char *SSL_get_psk_identity(const SSL *s)
4602
0
{
4603
0
    if (s == NULL || s->session == NULL)
4604
0
        return NULL;
4605
0
    return s->session->psk_identity;
4606
0
}
4607
4608
void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
4609
0
{
4610
0
    s->psk_client_callback = cb;
4611
0
}
4612
4613
void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
4614
0
{
4615
0
    ctx->psk_client_callback = cb;
4616
0
}
4617
4618
void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
4619
0
{
4620
0
    s->psk_server_callback = cb;
4621
0
}
4622
4623
void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
4624
0
{
4625
0
    ctx->psk_server_callback = cb;
4626
0
}
4627
#endif
4628
4629
void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
4630
0
{
4631
0
    s->psk_find_session_cb = cb;
4632
0
}
4633
4634
void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
4635
    SSL_psk_find_session_cb_func cb)
4636
0
{
4637
0
    ctx->psk_find_session_cb = cb;
4638
0
}
4639
4640
void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
4641
0
{
4642
0
    s->psk_use_session_cb = cb;
4643
0
}
4644
4645
void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
4646
    SSL_psk_use_session_cb_func cb)
4647
0
{
4648
0
    ctx->psk_use_session_cb = cb;
4649
0
}
4650
4651
void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
4652
    void (*cb)(int write_p, int version,
4653
        int content_type, const void *buf,
4654
        size_t len, SSL *ssl, void *arg))
4655
0
{
4656
0
    SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
4657
0
}
4658
4659
void SSL_set_msg_callback(SSL *ssl,
4660
    void (*cb)(int write_p, int version,
4661
        int content_type, const void *buf,
4662
        size_t len, SSL *ssl, void *arg))
4663
0
{
4664
0
    SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
4665
0
}
4666
4667
void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
4668
    int (*cb)(SSL *ssl,
4669
        int
4670
            is_forward_secure))
4671
0
{
4672
0
    SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
4673
0
        (void (*)(void))cb);
4674
0
}
4675
4676
void SSL_set_not_resumable_session_callback(SSL *ssl,
4677
    int (*cb)(SSL *ssl,
4678
        int is_forward_secure))
4679
0
{
4680
0
    SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
4681
0
        (void (*)(void))cb);
4682
0
}
4683
4684
void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
4685
    size_t (*cb)(SSL *ssl, int type,
4686
        size_t len, void *arg))
4687
0
{
4688
0
    ctx->record_padding_cb = cb;
4689
0
}
4690
4691
void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
4692
0
{
4693
0
    ctx->record_padding_arg = arg;
4694
0
}
4695
4696
void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
4697
0
{
4698
0
    return ctx->record_padding_arg;
4699
0
}
4700
4701
int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
4702
0
{
4703
    /* block size of 0 or 1 is basically no padding */
4704
0
    if (block_size == 1)
4705
0
        ctx->block_padding = 0;
4706
0
    else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
4707
0
        ctx->block_padding = block_size;
4708
0
    else
4709
0
        return 0;
4710
0
    return 1;
4711
0
}
4712
4713
int SSL_set_record_padding_callback(SSL *ssl,
4714
    size_t (*cb)(SSL *ssl, int type,
4715
        size_t len, void *arg))
4716
0
{
4717
0
    BIO *b;
4718
4719
0
    b = SSL_get_wbio(ssl);
4720
0
    if (b == NULL || !BIO_get_ktls_send(b)) {
4721
0
        ssl->record_padding_cb = cb;
4722
0
        return 1;
4723
0
    }
4724
0
    return 0;
4725
0
}
4726
4727
void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
4728
0
{
4729
0
    ssl->record_padding_arg = arg;
4730
0
}
4731
4732
void *SSL_get_record_padding_callback_arg(const SSL *ssl)
4733
0
{
4734
0
    return ssl->record_padding_arg;
4735
0
}
4736
4737
int SSL_set_block_padding(SSL *ssl, size_t block_size)
4738
0
{
4739
    /* block size of 0 or 1 is basically no padding */
4740
0
    if (block_size == 1)
4741
0
        ssl->block_padding = 0;
4742
0
    else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
4743
0
        ssl->block_padding = block_size;
4744
0
    else
4745
0
        return 0;
4746
0
    return 1;
4747
0
}
4748
4749
int SSL_set_num_tickets(SSL *s, size_t num_tickets)
4750
0
{
4751
0
    s->num_tickets = num_tickets;
4752
4753
0
    return 1;
4754
0
}
4755
4756
size_t SSL_get_num_tickets(const SSL *s)
4757
0
{
4758
0
    return s->num_tickets;
4759
0
}
4760
4761
int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
4762
0
{
4763
0
    ctx->num_tickets = num_tickets;
4764
4765
0
    return 1;
4766
0
}
4767
4768
size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
4769
0
{
4770
0
    return ctx->num_tickets;
4771
0
}
4772
4773
/*
4774
 * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
4775
 * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
4776
 * If EVP_MD pointer is passed, initializes ctx with this |md|.
4777
 * Returns the newly allocated ctx;
4778
 */
4779
4780
EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
4781
2.08k
{
4782
2.08k
    ssl_clear_hash_ctx(hash);
4783
2.08k
    *hash = EVP_MD_CTX_new();
4784
2.08k
    if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
4785
0
        EVP_MD_CTX_free(*hash);
4786
0
        *hash = NULL;
4787
0
        return NULL;
4788
0
    }
4789
2.08k
    return *hash;
4790
2.08k
}
4791
4792
void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
4793
93.8k
{
4794
4795
93.8k
    EVP_MD_CTX_free(*hash);
4796
93.8k
    *hash = NULL;
4797
93.8k
}
4798
4799
/* Retrieve handshake hashes */
4800
int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
4801
    size_t *hashlen)
4802
177k
{
4803
177k
    EVP_MD_CTX *ctx = NULL;
4804
177k
    EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
4805
177k
    int hashleni = EVP_MD_CTX_get_size(hdgst);
4806
177k
    int ret = 0;
4807
4808
177k
    if (hashleni < 0 || (size_t)hashleni > outlen) {
4809
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4810
0
        goto err;
4811
0
    }
4812
4813
177k
    ctx = EVP_MD_CTX_new();
4814
177k
    if (ctx == NULL) {
4815
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4816
0
        goto err;
4817
0
    }
4818
4819
177k
    if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
4820
177k
        || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
4821
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4822
0
        goto err;
4823
0
    }
4824
4825
177k
    *hashlen = hashleni;
4826
4827
177k
    ret = 1;
4828
177k
err:
4829
177k
    EVP_MD_CTX_free(ctx);
4830
177k
    return ret;
4831
177k
}
4832
4833
int SSL_session_reused(const SSL *s)
4834
0
{
4835
0
    return s->hit;
4836
0
}
4837
4838
int SSL_is_server(const SSL *s)
4839
0
{
4840
0
    return s->server;
4841
0
}
4842
4843
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
4844
void SSL_set_debug(SSL *s, int debug)
4845
0
{
4846
    /* Old function was do-nothing anyway... */
4847
0
    (void)s;
4848
0
    (void)debug;
4849
0
}
4850
#endif
4851
4852
void SSL_set_security_level(SSL *s, int level)
4853
0
{
4854
0
    s->cert->sec_level = level;
4855
0
}
4856
4857
int SSL_get_security_level(const SSL *s)
4858
2.69M
{
4859
2.69M
    return s->cert->sec_level;
4860
2.69M
}
4861
4862
void SSL_set_security_callback(SSL *s,
4863
    int (*cb)(const SSL *s, const SSL_CTX *ctx,
4864
        int op, int bits, int nid,
4865
        void *other, void *ex))
4866
0
{
4867
0
    s->cert->sec_cb = cb;
4868
0
}
4869
4870
int (*SSL_get_security_callback(const SSL *s))(const SSL *s,
4871
    const SSL_CTX *ctx, int op,
4872
    int bits, int nid, void *other,
4873
    void *ex)
4874
0
{
4875
0
    return s->cert->sec_cb;
4876
0
}
4877
4878
void SSL_set0_security_ex_data(SSL *s, void *ex)
4879
0
{
4880
0
    s->cert->sec_ex = ex;
4881
0
}
4882
4883
void *SSL_get0_security_ex_data(const SSL *s)
4884
0
{
4885
0
    return s->cert->sec_ex;
4886
0
}
4887
4888
void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
4889
0
{
4890
0
    ctx->cert->sec_level = level;
4891
0
}
4892
4893
int SSL_CTX_get_security_level(const SSL_CTX *ctx)
4894
154k
{
4895
154k
    return ctx->cert->sec_level;
4896
154k
}
4897
4898
void SSL_CTX_set_security_callback(SSL_CTX *ctx,
4899
    int (*cb)(const SSL *s, const SSL_CTX *ctx,
4900
        int op, int bits, int nid,
4901
        void *other, void *ex))
4902
0
{
4903
0
    ctx->cert->sec_cb = cb;
4904
0
}
4905
4906
int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx))(const SSL *s,
4907
    const SSL_CTX *ctx,
4908
    int op, int bits,
4909
    int nid,
4910
    void *other,
4911
    void *ex)
4912
0
{
4913
0
    return ctx->cert->sec_cb;
4914
0
}
4915
4916
void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
4917
0
{
4918
0
    ctx->cert->sec_ex = ex;
4919
0
}
4920
4921
void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
4922
0
{
4923
0
    return ctx->cert->sec_ex;
4924
0
}
4925
4926
uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
4927
0
{
4928
0
    return ctx->options;
4929
0
}
4930
4931
uint64_t SSL_get_options(const SSL *s)
4932
8.60k
{
4933
8.60k
    return s->options;
4934
8.60k
}
4935
4936
uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
4937
0
{
4938
0
    return ctx->options |= op;
4939
0
}
4940
4941
uint64_t SSL_set_options(SSL *s, uint64_t op)
4942
0
{
4943
0
    return s->options |= op;
4944
0
}
4945
4946
uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
4947
0
{
4948
0
    return ctx->options &= ~op;
4949
0
}
4950
4951
uint64_t SSL_clear_options(SSL *s, uint64_t op)
4952
0
{
4953
0
    return s->options &= ~op;
4954
0
}
4955
4956
STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
4957
0
{
4958
0
    return s->verified_chain;
4959
0
}
4960
4961
IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
4962
4963
#ifndef OPENSSL_NO_CT
4964
4965
/*
4966
 * Moves SCTs from the |src| stack to the |dst| stack.
4967
 * The source of each SCT will be set to |origin|.
4968
 * If |dst| points to a NULL pointer, a new stack will be created and owned by
4969
 * the caller.
4970
 * Returns the number of SCTs moved, or a negative integer if an error occurs.
4971
 * The |dst| stack is created and possibly partially populated even in case
4972
 * of error, likewise the |src| stack may be left in an intermediate state.
4973
 */
4974
static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
4975
    sct_source_t origin)
4976
0
{
4977
0
    int scts_moved = 0;
4978
0
    SCT *sct = NULL;
4979
4980
0
    if (*dst == NULL) {
4981
0
        *dst = sk_SCT_new_null();
4982
0
        if (*dst == NULL) {
4983
0
            ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
4984
0
            goto err;
4985
0
        }
4986
0
    }
4987
4988
0
    while ((sct = sk_SCT_pop(src)) != NULL) {
4989
0
        if (SCT_set_source(sct, origin) != 1)
4990
0
            goto err;
4991
4992
0
        if (!sk_SCT_push(*dst, sct))
4993
0
            goto err;
4994
0
        scts_moved += 1;
4995
0
    }
4996
4997
0
    return scts_moved;
4998
0
err:
4999
0
    SCT_free(sct);
5000
0
    return -1;
5001
0
}
5002
5003
/*
5004
 * Look for data collected during ServerHello and parse if found.
5005
 * Returns the number of SCTs extracted.
5006
 */
5007
static int ct_extract_tls_extension_scts(SSL *s)
5008
0
{
5009
0
    int scts_extracted = 0;
5010
5011
0
    if (s->ext.scts != NULL) {
5012
0
        const unsigned char *p = s->ext.scts;
5013
0
        STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
5014
5015
0
        scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
5016
5017
0
        SCT_LIST_free(scts);
5018
0
    }
5019
5020
0
    return scts_extracted;
5021
0
}
5022
5023
/*
5024
 * Checks for an OCSP response and then attempts to extract any SCTs found if it
5025
 * contains an SCT X509 extension. They will be stored in |s->scts|.
5026
 * Returns:
5027
 * - The number of SCTs extracted, assuming an OCSP response exists.
5028
 * - 0 if no OCSP response exists or it contains no SCTs.
5029
 * - A negative integer if an error occurs.
5030
 */
5031
static int ct_extract_ocsp_response_scts(SSL *s)
5032
0
{
5033
0
#ifndef OPENSSL_NO_OCSP
5034
0
    int scts_extracted = 0;
5035
0
    const unsigned char *p;
5036
0
    OCSP_BASICRESP *br = NULL;
5037
0
    OCSP_RESPONSE *rsp = NULL;
5038
0
    STACK_OF(SCT) *scts = NULL;
5039
0
    int i;
5040
5041
0
    if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
5042
0
        goto err;
5043
5044
0
    p = s->ext.ocsp.resp;
5045
0
    rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
5046
0
    if (rsp == NULL)
5047
0
        goto err;
5048
5049
0
    br = OCSP_response_get1_basic(rsp);
5050
0
    if (br == NULL)
5051
0
        goto err;
5052
5053
0
    for (i = 0; i < OCSP_resp_count(br); ++i) {
5054
0
        OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
5055
5056
0
        if (single == NULL)
5057
0
            continue;
5058
5059
0
        scts = OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
5060
0
        scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
5061
0
        if (scts_extracted < 0)
5062
0
            goto err;
5063
0
    }
5064
0
err:
5065
0
    SCT_LIST_free(scts);
5066
0
    OCSP_BASICRESP_free(br);
5067
0
    OCSP_RESPONSE_free(rsp);
5068
0
    return scts_extracted;
5069
#else
5070
    /* Behave as if no OCSP response exists */
5071
    return 0;
5072
#endif
5073
0
}
5074
5075
/*
5076
 * Attempts to extract SCTs from the peer certificate.
5077
 * Return the number of SCTs extracted, or a negative integer if an error
5078
 * occurs.
5079
 */
5080
static int ct_extract_x509v3_extension_scts(SSL *s)
5081
0
{
5082
0
    int scts_extracted = 0;
5083
0
    X509 *cert = s->session != NULL ? s->session->peer : NULL;
5084
5085
0
    if (cert != NULL) {
5086
0
        STACK_OF(SCT) *scts = X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
5087
5088
0
        scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
5089
5090
0
        SCT_LIST_free(scts);
5091
0
    }
5092
5093
0
    return scts_extracted;
5094
0
}
5095
5096
/*
5097
 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
5098
 * response (if it exists) and X509v3 extensions in the certificate.
5099
 * Returns NULL if an error occurs.
5100
 */
5101
const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
5102
0
{
5103
0
    if (!s->scts_parsed) {
5104
0
        if (ct_extract_tls_extension_scts(s) < 0 || ct_extract_ocsp_response_scts(s) < 0 || ct_extract_x509v3_extension_scts(s) < 0)
5105
0
            goto err;
5106
5107
0
        s->scts_parsed = 1;
5108
0
    }
5109
0
    return s->scts;
5110
0
err:
5111
0
    return NULL;
5112
0
}
5113
5114
static int ct_permissive(const CT_POLICY_EVAL_CTX *ctx,
5115
    const STACK_OF(SCT) *scts, void *unused_arg)
5116
0
{
5117
0
    return 1;
5118
0
}
5119
5120
static int ct_strict(const CT_POLICY_EVAL_CTX *ctx,
5121
    const STACK_OF(SCT) *scts, void *unused_arg)
5122
0
{
5123
0
    int count = scts != NULL ? sk_SCT_num(scts) : 0;
5124
0
    int i;
5125
5126
0
    for (i = 0; i < count; ++i) {
5127
0
        SCT *sct = sk_SCT_value(scts, i);
5128
0
        int status = SCT_get_validation_status(sct);
5129
5130
0
        if (status == SCT_VALIDATION_STATUS_VALID)
5131
0
            return 1;
5132
0
    }
5133
0
    ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
5134
0
    return 0;
5135
0
}
5136
5137
int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
5138
    void *arg)
5139
11.4k
{
5140
    /*
5141
     * Since code exists that uses the custom extension handler for CT, look
5142
     * for this and throw an error if they have already registered to use CT.
5143
     */
5144
11.4k
    if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx, TLSEXT_TYPE_signed_certificate_timestamp)) {
5145
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
5146
0
        return 0;
5147
0
    }
5148
5149
11.4k
    if (callback != NULL) {
5150
        /*
5151
         * If we are validating CT, then we MUST accept SCTs served via OCSP
5152
         */
5153
0
        if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
5154
0
            return 0;
5155
0
    }
5156
5157
11.4k
    s->ct_validation_callback = callback;
5158
11.4k
    s->ct_validation_callback_arg = arg;
5159
5160
11.4k
    return 1;
5161
11.4k
}
5162
5163
int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
5164
    ssl_ct_validation_cb callback, void *arg)
5165
0
{
5166
    /*
5167
     * Since code exists that uses the custom extension handler for CT, look for
5168
     * this and throw an error if they have already registered to use CT.
5169
     */
5170
0
    if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx, TLSEXT_TYPE_signed_certificate_timestamp)) {
5171
0
        ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
5172
0
        return 0;
5173
0
    }
5174
5175
0
    ctx->ct_validation_callback = callback;
5176
0
    ctx->ct_validation_callback_arg = arg;
5177
0
    return 1;
5178
0
}
5179
5180
int SSL_ct_is_enabled(const SSL *s)
5181
0
{
5182
0
    return s->ct_validation_callback != NULL;
5183
0
}
5184
5185
int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
5186
0
{
5187
0
    return ctx->ct_validation_callback != NULL;
5188
0
}
5189
5190
int ssl_validate_ct(SSL *s)
5191
0
{
5192
0
    int ret = 0;
5193
0
    X509 *cert = s->session != NULL ? s->session->peer : NULL;
5194
0
    X509 *issuer;
5195
0
    SSL_DANE *dane = &s->dane;
5196
0
    CT_POLICY_EVAL_CTX *ctx = NULL;
5197
0
    const STACK_OF(SCT) *scts;
5198
5199
    /*
5200
     * If no callback is set, the peer is anonymous, or its chain is invalid,
5201
     * skip SCT validation - just return success.  Applications that continue
5202
     * handshakes without certificates, with unverified chains, or pinned leaf
5203
     * certificates are outside the scope of the WebPKI and CT.
5204
     *
5205
     * The above exclusions notwithstanding the vast majority of peers will
5206
     * have rather ordinary certificate chains validated by typical
5207
     * applications that perform certificate verification and therefore will
5208
     * process SCTs when enabled.
5209
     */
5210
0
    if (s->ct_validation_callback == NULL || cert == NULL || s->verify_result != X509_V_OK || s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
5211
0
        return 1;
5212
5213
    /*
5214
     * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
5215
     * trust-anchors.  See https://tools.ietf.org/html/rfc7671#section-4.2
5216
     */
5217
0
    if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
5218
0
        switch (dane->mtlsa->usage) {
5219
0
        case DANETLS_USAGE_DANE_TA:
5220
0
        case DANETLS_USAGE_DANE_EE:
5221
0
            return 1;
5222
0
        }
5223
0
    }
5224
5225
0
    ctx = CT_POLICY_EVAL_CTX_new_ex(s->ctx->libctx, s->ctx->propq);
5226
0
    if (ctx == NULL) {
5227
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5228
0
        goto end;
5229
0
    }
5230
5231
0
    issuer = sk_X509_value(s->verified_chain, 1);
5232
0
    CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
5233
0
    CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
5234
0
    CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
5235
0
    CT_POLICY_EVAL_CTX_set_time(
5236
0
        ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
5237
5238
0
    scts = SSL_get0_peer_scts(s);
5239
5240
    /*
5241
     * This function returns success (> 0) only when all the SCTs are valid, 0
5242
     * when some are invalid, and < 0 on various internal errors (out of
5243
     * memory, etc.).  Having some, or even all, invalid SCTs is not sufficient
5244
     * reason to abort the handshake, that decision is up to the callback.
5245
     * Therefore, we error out only in the unexpected case that the return
5246
     * value is negative.
5247
     *
5248
     * XXX: One might well argue that the return value of this function is an
5249
     * unfortunate design choice.  Its job is only to determine the validation
5250
     * status of each of the provided SCTs.  So long as it correctly separates
5251
     * the wheat from the chaff it should return success.  Failure in this case
5252
     * ought to correspond to an inability to carry out its duties.
5253
     */
5254
0
    if (SCT_LIST_validate(scts, ctx) < 0) {
5255
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
5256
0
        goto end;
5257
0
    }
5258
5259
0
    ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
5260
0
    if (ret < 0)
5261
0
        ret = 0; /* This function returns 0 on failure */
5262
0
    if (!ret)
5263
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
5264
5265
0
end:
5266
0
    CT_POLICY_EVAL_CTX_free(ctx);
5267
    /*
5268
     * With SSL_VERIFY_NONE the session may be cached and re-used despite a
5269
     * failure return code here.  Also the application may wish the complete
5270
     * the handshake, and then disconnect cleanly at a higher layer, after
5271
     * checking the verification status of the completed connection.
5272
     *
5273
     * We therefore force a certificate verification failure which will be
5274
     * visible via SSL_get_verify_result() and cached as part of any resumed
5275
     * session.
5276
     *
5277
     * Note: the permissive callback is for information gathering only, always
5278
     * returns success, and does not affect verification status.  Only the
5279
     * strict callback or a custom application-specified callback can trigger
5280
     * connection failure or record a verification error.
5281
     */
5282
0
    if (ret <= 0)
5283
0
        s->verify_result = X509_V_ERR_NO_VALID_SCTS;
5284
0
    return ret;
5285
0
}
5286
5287
int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
5288
0
{
5289
0
    switch (validation_mode) {
5290
0
    default:
5291
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
5292
0
        return 0;
5293
0
    case SSL_CT_VALIDATION_PERMISSIVE:
5294
0
        return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
5295
0
    case SSL_CT_VALIDATION_STRICT:
5296
0
        return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
5297
0
    }
5298
0
}
5299
5300
int SSL_enable_ct(SSL *s, int validation_mode)
5301
0
{
5302
0
    switch (validation_mode) {
5303
0
    default:
5304
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
5305
0
        return 0;
5306
0
    case SSL_CT_VALIDATION_PERMISSIVE:
5307
0
        return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
5308
0
    case SSL_CT_VALIDATION_STRICT:
5309
0
        return SSL_set_ct_validation_callback(s, ct_strict, NULL);
5310
0
    }
5311
0
}
5312
5313
int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
5314
0
{
5315
0
    return CTLOG_STORE_load_default_file(ctx->ctlog_store);
5316
0
}
5317
5318
int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
5319
0
{
5320
0
    return CTLOG_STORE_load_file(ctx->ctlog_store, path);
5321
0
}
5322
5323
void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs)
5324
0
{
5325
0
    CTLOG_STORE_free(ctx->ctlog_store);
5326
0
    ctx->ctlog_store = logs;
5327
0
}
5328
5329
const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
5330
0
{
5331
0
    return ctx->ctlog_store;
5332
0
}
5333
5334
#endif /* OPENSSL_NO_CT */
5335
5336
void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
5337
    void *arg)
5338
0
{
5339
0
    c->client_hello_cb = cb;
5340
0
    c->client_hello_cb_arg = arg;
5341
0
}
5342
5343
int SSL_client_hello_isv2(SSL *s)
5344
0
{
5345
0
    if (s->clienthello == NULL)
5346
0
        return 0;
5347
0
    return s->clienthello->isv2;
5348
0
}
5349
5350
unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
5351
0
{
5352
0
    if (s->clienthello == NULL)
5353
0
        return 0;
5354
0
    return s->clienthello->legacy_version;
5355
0
}
5356
5357
size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
5358
0
{
5359
0
    if (s->clienthello == NULL)
5360
0
        return 0;
5361
0
    if (out != NULL)
5362
0
        *out = s->clienthello->random;
5363
0
    return SSL3_RANDOM_SIZE;
5364
0
}
5365
5366
size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
5367
0
{
5368
0
    if (s->clienthello == NULL)
5369
0
        return 0;
5370
0
    if (out != NULL)
5371
0
        *out = s->clienthello->session_id;
5372
0
    return s->clienthello->session_id_len;
5373
0
}
5374
5375
size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
5376
0
{
5377
0
    if (s->clienthello == NULL)
5378
0
        return 0;
5379
0
    if (out != NULL)
5380
0
        *out = PACKET_data(&s->clienthello->ciphersuites);
5381
0
    return PACKET_remaining(&s->clienthello->ciphersuites);
5382
0
}
5383
5384
size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
5385
0
{
5386
0
    if (s->clienthello == NULL)
5387
0
        return 0;
5388
0
    if (out != NULL)
5389
0
        *out = s->clienthello->compressions;
5390
0
    return s->clienthello->compressions_len;
5391
0
}
5392
5393
int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
5394
0
{
5395
0
    RAW_EXTENSION *ext;
5396
0
    int *present;
5397
0
    size_t num = 0, i;
5398
5399
0
    if (s->clienthello == NULL || out == NULL || outlen == NULL)
5400
0
        return 0;
5401
0
    for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
5402
0
        ext = s->clienthello->pre_proc_exts + i;
5403
0
        if (ext->present)
5404
0
            num++;
5405
0
    }
5406
0
    if (num == 0) {
5407
0
        *out = NULL;
5408
0
        *outlen = 0;
5409
0
        return 1;
5410
0
    }
5411
0
    if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
5412
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
5413
0
        return 0;
5414
0
    }
5415
0
    for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
5416
0
        ext = s->clienthello->pre_proc_exts + i;
5417
0
        if (ext->present) {
5418
0
            if (ext->received_order >= num)
5419
0
                goto err;
5420
0
            present[ext->received_order] = ext->type;
5421
0
        }
5422
0
    }
5423
0
    *out = present;
5424
0
    *outlen = num;
5425
0
    return 1;
5426
0
err:
5427
0
    OPENSSL_free(present);
5428
0
    return 0;
5429
0
}
5430
5431
int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
5432
    size_t *outlen)
5433
0
{
5434
0
    size_t i;
5435
0
    RAW_EXTENSION *r;
5436
5437
0
    if (s->clienthello == NULL)
5438
0
        return 0;
5439
0
    for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
5440
0
        r = s->clienthello->pre_proc_exts + i;
5441
0
        if (r->present && r->type == type) {
5442
0
            if (out != NULL)
5443
0
                *out = PACKET_data(&r->data);
5444
0
            if (outlen != NULL)
5445
0
                *outlen = PACKET_remaining(&r->data);
5446
0
            return 1;
5447
0
        }
5448
0
    }
5449
0
    return 0;
5450
0
}
5451
5452
int SSL_free_buffers(SSL *ssl)
5453
0
{
5454
0
    RECORD_LAYER *rl = &ssl->rlayer;
5455
5456
0
    if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
5457
0
        return 0;
5458
5459
0
    if (RECORD_LAYER_data_present(rl))
5460
0
        return 0;
5461
5462
0
    RECORD_LAYER_release(rl);
5463
0
    return 1;
5464
0
}
5465
5466
int SSL_alloc_buffers(SSL *ssl)
5467
0
{
5468
0
    return ssl3_setup_buffers(ssl);
5469
0
}
5470
5471
void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
5472
0
{
5473
0
    ctx->keylog_callback = cb;
5474
0
}
5475
5476
SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
5477
0
{
5478
0
    return ctx->keylog_callback;
5479
0
}
5480
5481
static int nss_keylog_int(const char *prefix,
5482
    SSL *ssl,
5483
    const uint8_t *parameter_1,
5484
    size_t parameter_1_len,
5485
    const uint8_t *parameter_2,
5486
    size_t parameter_2_len)
5487
49.9k
{
5488
49.9k
    char *out = NULL;
5489
49.9k
    char *cursor = NULL;
5490
49.9k
    size_t out_len = 0;
5491
49.9k
    size_t i;
5492
49.9k
    size_t prefix_len;
5493
5494
49.9k
    if (ssl->ctx->keylog_callback == NULL)
5495
49.9k
        return 1;
5496
5497
    /*
5498
     * Our output buffer will contain the following strings, rendered with
5499
     * space characters in between, terminated by a NULL character: first the
5500
     * prefix, then the first parameter, then the second parameter. The
5501
     * meaning of each parameter depends on the specific key material being
5502
     * logged. Note that the first and second parameters are encoded in
5503
     * hexadecimal, so we need a buffer that is twice their lengths.
5504
     */
5505
0
    prefix_len = strlen(prefix);
5506
0
    out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
5507
0
    if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
5508
0
        SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5509
0
        return 0;
5510
0
    }
5511
5512
0
    strcpy(cursor, prefix);
5513
0
    cursor += prefix_len;
5514
0
    *cursor++ = ' ';
5515
5516
0
    for (i = 0; i < parameter_1_len; i++) {
5517
0
        sprintf(cursor, "%02x", parameter_1[i]);
5518
0
        cursor += 2;
5519
0
    }
5520
0
    *cursor++ = ' ';
5521
5522
0
    for (i = 0; i < parameter_2_len; i++) {
5523
0
        sprintf(cursor, "%02x", parameter_2[i]);
5524
0
        cursor += 2;
5525
0
    }
5526
0
    *cursor = '\0';
5527
5528
0
    ssl->ctx->keylog_callback(ssl, (const char *)out);
5529
0
    OPENSSL_clear_free(out, out_len);
5530
0
    return 1;
5531
0
}
5532
5533
int ssl_log_rsa_client_key_exchange(SSL *ssl,
5534
    const uint8_t *encrypted_premaster,
5535
    size_t encrypted_premaster_len,
5536
    const uint8_t *premaster,
5537
    size_t premaster_len)
5538
4.80k
{
5539
4.80k
    if (encrypted_premaster_len < 8) {
5540
0
        SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5541
0
        return 0;
5542
0
    }
5543
5544
    /* We only want the first 8 bytes of the encrypted premaster as a tag. */
5545
4.80k
    return nss_keylog_int("RSA",
5546
4.80k
        ssl,
5547
4.80k
        encrypted_premaster,
5548
4.80k
        8,
5549
4.80k
        premaster,
5550
4.80k
        premaster_len);
5551
4.80k
}
5552
5553
int ssl_log_secret(SSL *ssl,
5554
    const char *label,
5555
    const uint8_t *secret,
5556
    size_t secret_len)
5557
112k
{
5558
112k
    return nss_keylog_int(label,
5559
112k
        ssl,
5560
112k
        ssl->s3.client_random,
5561
112k
        SSL3_RANDOM_SIZE,
5562
112k
        secret,
5563
112k
        secret_len);
5564
112k
}
5565
5566
6.25k
#define SSLV2_CIPHER_LEN 3
5567
5568
int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
5569
41.6k
{
5570
41.6k
    int n;
5571
5572
41.6k
    n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
5573
5574
41.6k
    if (PACKET_remaining(cipher_suites) == 0) {
5575
48
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
5576
48
        return 0;
5577
48
    }
5578
5579
41.5k
    if (PACKET_remaining(cipher_suites) % n != 0) {
5580
33
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
5581
33
        return 0;
5582
33
    }
5583
5584
41.5k
    OPENSSL_free(s->s3.tmp.ciphers_raw);
5585
41.5k
    s->s3.tmp.ciphers_raw = NULL;
5586
41.5k
    s->s3.tmp.ciphers_rawlen = 0;
5587
5588
41.5k
    if (sslv2format) {
5589
5.37k
        size_t numciphers = PACKET_remaining(cipher_suites) / n;
5590
5.37k
        PACKET sslv2ciphers = *cipher_suites;
5591
5.37k
        unsigned int leadbyte;
5592
5.37k
        unsigned char *raw;
5593
5594
        /*
5595
         * We store the raw ciphers list in SSLv3+ format so we need to do some
5596
         * preprocessing to convert the list first. If there are any SSLv2 only
5597
         * ciphersuites with a non-zero leading byte then we are going to
5598
         * slightly over allocate because we won't store those. But that isn't a
5599
         * problem.
5600
         */
5601
5.37k
        raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
5602
5.37k
        s->s3.tmp.ciphers_raw = raw;
5603
5.37k
        if (raw == NULL) {
5604
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5605
0
            return 0;
5606
0
        }
5607
5.37k
        for (s->s3.tmp.ciphers_rawlen = 0;
5608
119k
            PACKET_remaining(&sslv2ciphers) > 0;
5609
113k
            raw += TLS_CIPHER_LEN) {
5610
113k
            if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
5611
113k
                || (leadbyte == 0
5612
59.5k
                    && !PACKET_copy_bytes(&sslv2ciphers, raw,
5613
59.5k
                        TLS_CIPHER_LEN))
5614
113k
                || (leadbyte != 0
5615
54.3k
                    && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
5616
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
5617
0
                OPENSSL_free(s->s3.tmp.ciphers_raw);
5618
0
                s->s3.tmp.ciphers_raw = NULL;
5619
0
                s->s3.tmp.ciphers_rawlen = 0;
5620
0
                return 0;
5621
0
            }
5622
113k
            if (leadbyte == 0)
5623
59.5k
                s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
5624
113k
        }
5625
36.1k
    } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
5626
36.1k
                   &s->s3.tmp.ciphers_rawlen)) {
5627
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5628
0
        return 0;
5629
0
    }
5630
41.5k
    return 1;
5631
41.5k
}
5632
5633
int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
5634
    int isv2format, STACK_OF(SSL_CIPHER) **sk,
5635
    STACK_OF(SSL_CIPHER) **scsvs)
5636
0
{
5637
0
    PACKET pkt;
5638
5639
0
    if (!PACKET_buf_init(&pkt, bytes, len))
5640
0
        return 0;
5641
0
    return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
5642
0
}
5643
5644
int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
5645
    STACK_OF(SSL_CIPHER) **skp,
5646
    STACK_OF(SSL_CIPHER) **scsvs_out,
5647
    int sslv2format, int fatal)
5648
4.08k
{
5649
4.08k
    const SSL_CIPHER *c;
5650
4.08k
    STACK_OF(SSL_CIPHER) *sk = NULL;
5651
4.08k
    STACK_OF(SSL_CIPHER) *scsvs = NULL;
5652
4.08k
    int n;
5653
    /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
5654
4.08k
    unsigned char cipher[SSLV2_CIPHER_LEN];
5655
5656
4.08k
    n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
5657
5658
4.08k
    if (PACKET_remaining(cipher_suites) == 0) {
5659
0
        if (fatal)
5660
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
5661
0
        else
5662
0
            ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
5663
0
        return 0;
5664
0
    }
5665
5666
4.08k
    if (PACKET_remaining(cipher_suites) % n != 0) {
5667
0
        if (fatal)
5668
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
5669
0
                SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
5670
0
        else
5671
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
5672
0
        return 0;
5673
0
    }
5674
5675
4.08k
    sk = sk_SSL_CIPHER_new_null();
5676
4.08k
    scsvs = sk_SSL_CIPHER_new_null();
5677
4.08k
    if (sk == NULL || scsvs == NULL) {
5678
0
        if (fatal)
5679
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5680
0
        else
5681
0
            ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
5682
0
        goto err;
5683
0
    }
5684
5685
85.9k
    while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
5686
        /*
5687
         * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
5688
         * first byte set to zero, while true SSLv2 ciphers have a non-zero
5689
         * first byte. We don't support any true SSLv2 ciphers, so skip them.
5690
         */
5691
81.8k
        if (sslv2format && cipher[0] != '\0')
5692
12.2k
            continue;
5693
5694
        /* For SSLv2-compat, ignore leading 0-byte. */
5695
69.5k
        c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
5696
69.5k
        if (c != NULL) {
5697
21.5k
            if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) || (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
5698
0
                if (fatal)
5699
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
5700
0
                else
5701
0
                    ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
5702
0
                goto err;
5703
0
            }
5704
21.5k
        }
5705
69.5k
    }
5706
4.08k
    if (PACKET_remaining(cipher_suites) > 0) {
5707
0
        if (fatal)
5708
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
5709
0
        else
5710
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
5711
0
        goto err;
5712
0
    }
5713
5714
4.08k
    if (skp != NULL)
5715
4.08k
        *skp = sk;
5716
0
    else
5717
0
        sk_SSL_CIPHER_free(sk);
5718
4.08k
    if (scsvs_out != NULL)
5719
4.08k
        *scsvs_out = scsvs;
5720
0
    else
5721
0
        sk_SSL_CIPHER_free(scsvs);
5722
4.08k
    return 1;
5723
0
err:
5724
0
    sk_SSL_CIPHER_free(sk);
5725
0
    sk_SSL_CIPHER_free(scsvs);
5726
0
    return 0;
5727
4.08k
}
5728
5729
int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
5730
0
{
5731
0
    ctx->max_early_data = max_early_data;
5732
5733
0
    return 1;
5734
0
}
5735
5736
uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
5737
0
{
5738
0
    return ctx->max_early_data;
5739
0
}
5740
5741
int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
5742
0
{
5743
0
    s->max_early_data = max_early_data;
5744
5745
0
    return 1;
5746
0
}
5747
5748
uint32_t SSL_get_max_early_data(const SSL *s)
5749
0
{
5750
0
    return s->max_early_data;
5751
0
}
5752
5753
int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
5754
0
{
5755
0
    ctx->recv_max_early_data = recv_max_early_data;
5756
5757
0
    return 1;
5758
0
}
5759
5760
uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
5761
0
{
5762
0
    return ctx->recv_max_early_data;
5763
0
}
5764
5765
int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
5766
0
{
5767
0
    s->recv_max_early_data = recv_max_early_data;
5768
5769
0
    return 1;
5770
0
}
5771
5772
uint32_t SSL_get_recv_max_early_data(const SSL *s)
5773
0
{
5774
0
    return s->recv_max_early_data;
5775
0
}
5776
5777
__owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
5778
1.05M
{
5779
    /* Return any active Max Fragment Len extension */
5780
1.05M
    if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
5781
15.2k
        return GET_MAX_FRAGMENT_LENGTH(ssl->session);
5782
5783
    /* return current SSL connection setting */
5784
1.03M
    return ssl->max_send_fragment;
5785
1.05M
}
5786
5787
__owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
5788
198k
{
5789
    /* Return a value regarding an active Max Fragment Len extension */
5790
198k
    if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
5791
592
        && ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
5792
592
        return GET_MAX_FRAGMENT_LENGTH(ssl->session);
5793
5794
    /* else limit |split_send_fragment| to current |max_send_fragment| */
5795
198k
    if (ssl->split_send_fragment > ssl->max_send_fragment)
5796
0
        return ssl->max_send_fragment;
5797
5798
    /* return current SSL connection setting */
5799
198k
    return ssl->split_send_fragment;
5800
198k
}
5801
5802
int SSL_stateless(SSL *s)
5803
0
{
5804
0
    int ret;
5805
5806
    /* Ensure there is no state left over from a previous invocation */
5807
0
    if (!SSL_clear(s))
5808
0
        return 0;
5809
5810
0
    ERR_clear_error();
5811
5812
0
    s->s3.flags |= TLS1_FLAGS_STATELESS;
5813
0
    ret = SSL_accept(s);
5814
0
    s->s3.flags &= ~TLS1_FLAGS_STATELESS;
5815
5816
0
    if (ret > 0 && s->ext.cookieok)
5817
0
        return 1;
5818
5819
0
    if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
5820
0
        return 0;
5821
5822
0
    return -1;
5823
0
}
5824
5825
void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
5826
0
{
5827
0
    ctx->pha_enabled = val;
5828
0
}
5829
5830
void SSL_set_post_handshake_auth(SSL *ssl, int val)
5831
0
{
5832
0
    ssl->pha_enabled = val;
5833
0
}
5834
5835
int SSL_verify_client_post_handshake(SSL *ssl)
5836
0
{
5837
0
    if (!SSL_IS_TLS13(ssl)) {
5838
0
        ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
5839
0
        return 0;
5840
0
    }
5841
0
    if (!ssl->server) {
5842
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
5843
0
        return 0;
5844
0
    }
5845
5846
0
    if (!SSL_is_init_finished(ssl)) {
5847
0
        ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
5848
0
        return 0;
5849
0
    }
5850
5851
0
    switch (ssl->post_handshake_auth) {
5852
0
    case SSL_PHA_NONE:
5853
0
        ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
5854
0
        return 0;
5855
0
    default:
5856
0
    case SSL_PHA_EXT_SENT:
5857
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
5858
0
        return 0;
5859
0
    case SSL_PHA_EXT_RECEIVED:
5860
0
        break;
5861
0
    case SSL_PHA_REQUEST_PENDING:
5862
0
        ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
5863
0
        return 0;
5864
0
    case SSL_PHA_REQUESTED:
5865
0
        ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
5866
0
        return 0;
5867
0
    }
5868
5869
0
    ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
5870
5871
    /* checks verify_mode and algorithm_auth */
5872
0
    if (!send_certificate_request(ssl)) {
5873
0
        ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
5874
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
5875
0
        return 0;
5876
0
    }
5877
5878
0
    ossl_statem_set_in_init(ssl, 1);
5879
0
    return 1;
5880
0
}
5881
5882
int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
5883
    SSL_CTX_generate_session_ticket_fn gen_cb,
5884
    SSL_CTX_decrypt_session_ticket_fn dec_cb,
5885
    void *arg)
5886
0
{
5887
0
    ctx->generate_ticket_cb = gen_cb;
5888
0
    ctx->decrypt_ticket_cb = dec_cb;
5889
0
    ctx->ticket_cb_data = arg;
5890
0
    return 1;
5891
0
}
5892
5893
void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
5894
    SSL_allow_early_data_cb_fn cb,
5895
    void *arg)
5896
0
{
5897
0
    ctx->allow_early_data_cb = cb;
5898
0
    ctx->allow_early_data_cb_data = arg;
5899
0
}
5900
5901
void SSL_set_allow_early_data_cb(SSL *s,
5902
    SSL_allow_early_data_cb_fn cb,
5903
    void *arg)
5904
0
{
5905
0
    s->allow_early_data_cb = cb;
5906
0
    s->allow_early_data_cb_data = arg;
5907
0
}
5908
5909
const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
5910
    int nid,
5911
    const char *properties)
5912
992k
{
5913
992k
    const EVP_CIPHER *ciph;
5914
5915
992k
    ciph = tls_get_cipher_from_engine(nid);
5916
992k
    if (ciph != NULL)
5917
0
        return ciph;
5918
5919
    /*
5920
     * If there is no engine cipher then we do an explicit fetch. This may fail
5921
     * and that could be ok
5922
     */
5923
992k
    ERR_set_mark();
5924
992k
    ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
5925
992k
    ERR_pop_to_mark();
5926
992k
    return ciph;
5927
992k
}
5928
5929
int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
5930
44.3k
{
5931
    /* Don't up-ref an implicit EVP_CIPHER */
5932
44.3k
    if (EVP_CIPHER_get0_provider(cipher) == NULL)
5933
0
        return 1;
5934
5935
    /*
5936
     * The cipher was explicitly fetched and therefore it is safe to cast
5937
     * away the const
5938
     */
5939
44.3k
    return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
5940
44.3k
}
5941
5942
void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
5943
4.14M
{
5944
4.14M
    if (cipher == NULL)
5945
1.79M
        return;
5946
5947
2.34M
    if (EVP_CIPHER_get0_provider(cipher) != NULL) {
5948
        /*
5949
         * The cipher was explicitly fetched and therefore it is safe to cast
5950
         * away the const
5951
         */
5952
2.34M
        EVP_CIPHER_free((EVP_CIPHER *)cipher);
5953
2.34M
    }
5954
2.34M
}
5955
5956
const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
5957
    int nid,
5958
    const char *properties)
5959
2.14M
{
5960
2.14M
    const EVP_MD *md;
5961
5962
2.14M
    md = tls_get_digest_from_engine(nid);
5963
2.14M
    if (md != NULL)
5964
0
        return md;
5965
5966
    /* Otherwise we do an explicit fetch */
5967
2.14M
    ERR_set_mark();
5968
2.14M
    md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
5969
2.14M
    ERR_pop_to_mark();
5970
2.14M
    return md;
5971
2.14M
}
5972
5973
int ssl_evp_md_up_ref(const EVP_MD *md)
5974
18.2k
{
5975
    /* Don't up-ref an implicit EVP_MD */
5976
18.2k
    if (EVP_MD_get0_provider(md) == NULL)
5977
0
        return 1;
5978
5979
    /*
5980
     * The digest was explicitly fetched and therefore it is safe to cast
5981
     * away the const
5982
     */
5983
18.2k
    return EVP_MD_up_ref((EVP_MD *)md);
5984
18.2k
}
5985
5986
void ssl_evp_md_free(const EVP_MD *md)
5987
2.83M
{
5988
2.83M
    if (md == NULL)
5989
1.34M
        return;
5990
5991
1.49M
    if (EVP_MD_get0_provider(md) != NULL) {
5992
        /*
5993
         * The digest was explicitly fetched and therefore it is safe to cast
5994
         * away the const
5995
         */
5996
1.49M
        EVP_MD_free((EVP_MD *)md);
5997
1.49M
    }
5998
1.49M
}
5999
6000
int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
6001
0
{
6002
0
    if (!ssl_security(s, SSL_SECOP_TMP_DH,
6003
0
            EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
6004
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
6005
0
        return 0;
6006
0
    }
6007
0
    EVP_PKEY_free(s->cert->dh_tmp);
6008
0
    s->cert->dh_tmp = dhpkey;
6009
0
    return 1;
6010
0
}
6011
6012
int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
6013
0
{
6014
0
    if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
6015
0
            EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
6016
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
6017
0
        return 0;
6018
0
    }
6019
0
    EVP_PKEY_free(ctx->cert->dh_tmp);
6020
0
    ctx->cert->dh_tmp = dhpkey;
6021
0
    return 1;
6022
0
}