Coverage Report

Created: 2025-11-16 06:40

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