Coverage Report

Created: 2025-06-13 06:58

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