Coverage Report

Created: 2025-06-13 06:58

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