Coverage Report

Created: 2018-08-29 13:53

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