Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/ssl/ssl_ciph.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
/*
13
 * Because of *asn1_*
14
 */
15
#define OPENSSL_SUPPRESS_DEPRECATED
16
17
#include <stdio.h>
18
#include <ctype.h>
19
#include <openssl/objects.h>
20
#include <openssl/comp.h>
21
#include <openssl/engine.h>
22
#include <openssl/crypto.h>
23
#include <openssl/conf.h>
24
#include <openssl/trace.h>
25
#include "internal/nelem.h"
26
#include "ssl_local.h"
27
#include "internal/thread_once.h"
28
#include "internal/cryptlib.h"
29
#include "internal/comp.h"
30
#include "internal/ssl_unwrap.h"
31
32
/* NB: make sure indices in these tables match values above */
33
34
typedef struct {
35
    uint32_t mask;
36
    int nid;
37
} ssl_cipher_table;
38
39
/* Table of NIDs for each cipher */
40
static const ssl_cipher_table ssl_cipher_table_cipher[SSL_ENC_NUM_IDX] = {
41
    { SSL_DES, NID_des_cbc }, /* SSL_ENC_DES_IDX 0 */
42
    { SSL_3DES, NID_des_ede3_cbc }, /* SSL_ENC_3DES_IDX 1 */
43
    { SSL_RC4, NID_rc4 }, /* SSL_ENC_RC4_IDX 2 */
44
    { SSL_RC2, NID_rc2_cbc }, /* SSL_ENC_RC2_IDX 3 */
45
    { SSL_IDEA, NID_idea_cbc }, /* SSL_ENC_IDEA_IDX 4 */
46
    { SSL_eNULL, NID_undef }, /* SSL_ENC_NULL_IDX 5 */
47
    { SSL_AES128, NID_aes_128_cbc }, /* SSL_ENC_AES128_IDX 6 */
48
    { SSL_AES256, NID_aes_256_cbc }, /* SSL_ENC_AES256_IDX 7 */
49
    { SSL_CAMELLIA128, NID_camellia_128_cbc }, /* SSL_ENC_CAMELLIA128_IDX 8 */
50
    { SSL_CAMELLIA256, NID_camellia_256_cbc }, /* SSL_ENC_CAMELLIA256_IDX 9 */
51
    { SSL_eGOST2814789CNT, NID_gost89_cnt }, /* SSL_ENC_GOST89_IDX 10 */
52
    { SSL_SEED, NID_seed_cbc }, /* SSL_ENC_SEED_IDX 11 */
53
    { SSL_AES128GCM, NID_aes_128_gcm }, /* SSL_ENC_AES128GCM_IDX 12 */
54
    { SSL_AES256GCM, NID_aes_256_gcm }, /* SSL_ENC_AES256GCM_IDX 13 */
55
    { SSL_AES128CCM, NID_aes_128_ccm }, /* SSL_ENC_AES128CCM_IDX 14 */
56
    { SSL_AES256CCM, NID_aes_256_ccm }, /* SSL_ENC_AES256CCM_IDX 15 */
57
    { SSL_AES128CCM8, NID_aes_128_ccm }, /* SSL_ENC_AES128CCM8_IDX 16 */
58
    { SSL_AES256CCM8, NID_aes_256_ccm }, /* SSL_ENC_AES256CCM8_IDX 17 */
59
    { SSL_eGOST2814789CNT12, NID_gost89_cnt_12 }, /* SSL_ENC_GOST8912_IDX 18 */
60
    { SSL_CHACHA20POLY1305, NID_chacha20_poly1305 }, /* SSL_ENC_CHACHA_IDX 19 */
61
    { SSL_ARIA128GCM, NID_aria_128_gcm }, /* SSL_ENC_ARIA128GCM_IDX 20 */
62
    { SSL_ARIA256GCM, NID_aria_256_gcm }, /* SSL_ENC_ARIA256GCM_IDX 21 */
63
    { SSL_MAGMA, NID_magma_ctr_acpkm }, /* SSL_ENC_MAGMA_IDX */
64
    { SSL_KUZNYECHIK, NID_kuznyechik_ctr_acpkm }, /* SSL_ENC_KUZNYECHIK_IDX */
65
};
66
67
/* NB: make sure indices in this table matches values above */
68
static const ssl_cipher_table ssl_cipher_table_mac[SSL_MD_NUM_IDX] = {
69
    { SSL_MD5, NID_md5 }, /* SSL_MD_MD5_IDX 0 */
70
    { SSL_SHA1, NID_sha1 }, /* SSL_MD_SHA1_IDX 1 */
71
    { SSL_GOST94, NID_id_GostR3411_94 }, /* SSL_MD_GOST94_IDX 2 */
72
    { SSL_GOST89MAC, NID_id_Gost28147_89_MAC }, /* SSL_MD_GOST89MAC_IDX 3 */
73
    { SSL_SHA256, NID_sha256 }, /* SSL_MD_SHA256_IDX 4 */
74
    { SSL_SHA384, NID_sha384 }, /* SSL_MD_SHA384_IDX 5 */
75
    { SSL_GOST12_256, NID_id_GostR3411_2012_256 }, /* SSL_MD_GOST12_256_IDX 6 */
76
    { SSL_GOST89MAC12, NID_gost_mac_12 }, /* SSL_MD_GOST89MAC12_IDX 7 */
77
    { SSL_GOST12_512, NID_id_GostR3411_2012_512 }, /* SSL_MD_GOST12_512_IDX 8 */
78
    { 0, NID_md5_sha1 }, /* SSL_MD_MD5_SHA1_IDX 9 */
79
    { 0, NID_sha224 }, /* SSL_MD_SHA224_IDX 10 */
80
    { 0, NID_sha512 }, /* SSL_MD_SHA512_IDX 11 */
81
    { SSL_MAGMAOMAC, NID_magma_mac }, /* sSL_MD_MAGMAOMAC_IDX */
82
    { SSL_KUZNYECHIKOMAC, NID_kuznyechik_mac } /* SSL_MD_KUZNYECHIKOMAC_IDX */
83
};
84
85
/* *INDENT-OFF* */
86
static const ssl_cipher_table ssl_cipher_table_kx[] = {
87
    { SSL_kRSA, NID_kx_rsa },
88
    { SSL_kECDHE, NID_kx_ecdhe },
89
    { SSL_kDHE, NID_kx_dhe },
90
    { SSL_kECDHEPSK, NID_kx_ecdhe_psk },
91
    { SSL_kDHEPSK, NID_kx_dhe_psk },
92
    { SSL_kRSAPSK, NID_kx_rsa_psk },
93
    { SSL_kPSK, NID_kx_psk },
94
    { SSL_kSRP, NID_kx_srp },
95
    { SSL_kGOST, NID_kx_gost },
96
    { SSL_kGOST18, NID_kx_gost18 },
97
    { SSL_kANY, NID_kx_any }
98
};
99
100
static const ssl_cipher_table ssl_cipher_table_auth[] = {
101
    { SSL_aRSA, NID_auth_rsa },
102
    { SSL_aECDSA, NID_auth_ecdsa },
103
    { SSL_aPSK, NID_auth_psk },
104
    { SSL_aDSS, NID_auth_dss },
105
    { SSL_aGOST01, NID_auth_gost01 },
106
    { SSL_aGOST12, NID_auth_gost12 },
107
    { SSL_aSRP, NID_auth_srp },
108
    { SSL_aNULL, NID_auth_null },
109
    { SSL_aANY, NID_auth_any }
110
};
111
/* *INDENT-ON* */
112
113
/* Utility function for table lookup */
114
static int ssl_cipher_info_find(const ssl_cipher_table *table,
115
    size_t table_cnt, uint32_t mask)
116
102k
{
117
102k
    size_t i;
118
1.15M
    for (i = 0; i < table_cnt; i++, table++) {
119
1.12M
        if (table->mask == mask)
120
69.5k
            return (int)i;
121
1.12M
    }
122
33.0k
    return -1;
123
102k
}
124
125
#define ssl_cipher_info_lookup(table, x) \
126
89.8k
    ssl_cipher_info_find(table, OSSL_NELEM(table), x)
127
128
/*
129
 * PKEY_TYPE for GOST89MAC is known in advance, but, because implementation
130
 * is engine-provided, we'll fill it only if corresponding EVP_PKEY_METHOD is
131
 * found
132
 */
133
static const int default_mac_pkey_id[SSL_MD_NUM_IDX] = {
134
    /* MD5, SHA, GOST94, MAC89 */
135
    EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
136
    /* SHA256, SHA384, GOST2012_256, MAC89-12 */
137
    EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, NID_undef,
138
    /* GOST2012_512 */
139
    EVP_PKEY_HMAC,
140
    /* MD5/SHA1, SHA224, SHA512, MAGMAOMAC, KUZNYECHIKOMAC */
141
    NID_undef, NID_undef, NID_undef, NID_undef, NID_undef
142
};
143
144
275M
#define CIPHER_ADD 1
145
12.9M
#define CIPHER_KILL 2
146
128M
#define CIPHER_DEL 3
147
168M
#define CIPHER_ORD 4
148
1.62M
#define CIPHER_SPECIAL 5
149
/*
150
 * Bump the ciphers to the top of the list.
151
 * This rule isn't currently supported by the public cipherstring API.
152
 */
153
76.5M
#define CIPHER_BUMP 6
154
155
typedef struct cipher_order_st {
156
    const SSL_CIPHER *cipher;
157
    int active;
158
    int dead;
159
    struct cipher_order_st *next, *prev;
160
} CIPHER_ORDER;
161
162
static const SSL_CIPHER cipher_aliases[] = {
163
    /* "ALL" doesn't include eNULL (must be specifically enabled) */
164
    { 0, SSL_TXT_ALL, NULL, 0, 0, 0, ~SSL_eNULL },
165
    /* "COMPLEMENTOFALL" */
166
    { 0, SSL_TXT_CMPALL, NULL, 0, 0, 0, SSL_eNULL },
167
168
    /*
169
     * "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
170
     * ALL!)
171
     */
172
    { 0, SSL_TXT_CMPDEF, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_NOT_DEFAULT },
173
174
    /*
175
     * key exchange aliases (some of those using only a single bit here
176
     * combine multiple key exchange algs according to the RFCs, e.g. kDHE
177
     * combines DHE_DSS and DHE_RSA)
178
     */
179
    { 0, SSL_TXT_kRSA, NULL, 0, SSL_kRSA },
180
181
    { 0, SSL_TXT_kEDH, NULL, 0, SSL_kDHE },
182
    { 0, SSL_TXT_kDHE, NULL, 0, SSL_kDHE },
183
    { 0, SSL_TXT_DH, NULL, 0, SSL_kDHE },
184
185
    { 0, SSL_TXT_kEECDH, NULL, 0, SSL_kECDHE },
186
    { 0, SSL_TXT_kECDHE, NULL, 0, SSL_kECDHE },
187
    { 0, SSL_TXT_ECDH, NULL, 0, SSL_kECDHE },
188
189
    { 0, SSL_TXT_kPSK, NULL, 0, SSL_kPSK },
190
    { 0, SSL_TXT_kRSAPSK, NULL, 0, SSL_kRSAPSK },
191
    { 0, SSL_TXT_kECDHEPSK, NULL, 0, SSL_kECDHEPSK },
192
    { 0, SSL_TXT_kDHEPSK, NULL, 0, SSL_kDHEPSK },
193
    { 0, SSL_TXT_kSRP, NULL, 0, SSL_kSRP },
194
    { 0, SSL_TXT_kGOST, NULL, 0, SSL_kGOST },
195
    { 0, SSL_TXT_kGOST18, NULL, 0, SSL_kGOST18 },
196
197
    /* server authentication aliases */
198
    { 0, SSL_TXT_aRSA, NULL, 0, 0, SSL_aRSA },
199
    { 0, SSL_TXT_aDSS, NULL, 0, 0, SSL_aDSS },
200
    { 0, SSL_TXT_DSS, NULL, 0, 0, SSL_aDSS },
201
    { 0, SSL_TXT_aNULL, NULL, 0, 0, SSL_aNULL },
202
    { 0, SSL_TXT_aECDSA, NULL, 0, 0, SSL_aECDSA },
203
    { 0, SSL_TXT_ECDSA, NULL, 0, 0, SSL_aECDSA },
204
    { 0, SSL_TXT_aPSK, NULL, 0, 0, SSL_aPSK },
205
    { 0, SSL_TXT_aGOST01, NULL, 0, 0, SSL_aGOST01 },
206
    { 0, SSL_TXT_aGOST12, NULL, 0, 0, SSL_aGOST12 },
207
    { 0, SSL_TXT_aGOST, NULL, 0, 0, SSL_aGOST01 | SSL_aGOST12 },
208
    { 0, SSL_TXT_aSRP, NULL, 0, 0, SSL_aSRP },
209
210
    /* aliases combining key exchange and server authentication */
211
    { 0, SSL_TXT_EDH, NULL, 0, SSL_kDHE, ~SSL_aNULL },
212
    { 0, SSL_TXT_DHE, NULL, 0, SSL_kDHE, ~SSL_aNULL },
213
    { 0, SSL_TXT_EECDH, NULL, 0, SSL_kECDHE, ~SSL_aNULL },
214
    { 0, SSL_TXT_ECDHE, NULL, 0, SSL_kECDHE, ~SSL_aNULL },
215
    { 0, SSL_TXT_NULL, NULL, 0, 0, 0, SSL_eNULL },
216
    { 0, SSL_TXT_RSA, NULL, 0, SSL_kRSA, SSL_aRSA },
217
    { 0, SSL_TXT_ADH, NULL, 0, SSL_kDHE, SSL_aNULL },
218
    { 0, SSL_TXT_AECDH, NULL, 0, SSL_kECDHE, SSL_aNULL },
219
    { 0, SSL_TXT_PSK, NULL, 0, SSL_PSK },
220
    { 0, SSL_TXT_SRP, NULL, 0, SSL_kSRP },
221
222
    /* symmetric encryption aliases */
223
    { 0, SSL_TXT_3DES, NULL, 0, 0, 0, SSL_3DES },
224
    { 0, SSL_TXT_RC4, NULL, 0, 0, 0, SSL_RC4 },
225
    { 0, SSL_TXT_RC2, NULL, 0, 0, 0, SSL_RC2 },
226
    { 0, SSL_TXT_IDEA, NULL, 0, 0, 0, SSL_IDEA },
227
    { 0, SSL_TXT_SEED, NULL, 0, 0, 0, SSL_SEED },
228
    { 0, SSL_TXT_eNULL, NULL, 0, 0, 0, SSL_eNULL },
229
    { 0, SSL_TXT_GOST, NULL, 0, 0, 0,
230
        SSL_eGOST2814789CNT | SSL_eGOST2814789CNT12 | SSL_MAGMA | SSL_KUZNYECHIK },
231
    { 0, SSL_TXT_AES128, NULL, 0, 0, 0,
232
        SSL_AES128 | SSL_AES128GCM | SSL_AES128CCM | SSL_AES128CCM8 },
233
    { 0, SSL_TXT_AES256, NULL, 0, 0, 0,
234
        SSL_AES256 | SSL_AES256GCM | SSL_AES256CCM | SSL_AES256CCM8 },
235
    { 0, SSL_TXT_AES, NULL, 0, 0, 0, SSL_AES },
236
    { 0, SSL_TXT_AES_GCM, NULL, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM },
237
    { 0, SSL_TXT_AES_CCM, NULL, 0, 0, 0,
238
        SSL_AES128CCM | SSL_AES256CCM | SSL_AES128CCM8 | SSL_AES256CCM8 },
239
    { 0, SSL_TXT_AES_CCM_8, NULL, 0, 0, 0, SSL_AES128CCM8 | SSL_AES256CCM8 },
240
    { 0, SSL_TXT_CAMELLIA128, NULL, 0, 0, 0, SSL_CAMELLIA128 },
241
    { 0, SSL_TXT_CAMELLIA256, NULL, 0, 0, 0, SSL_CAMELLIA256 },
242
    { 0, SSL_TXT_CAMELLIA, NULL, 0, 0, 0, SSL_CAMELLIA },
243
    { 0, SSL_TXT_CHACHA20, NULL, 0, 0, 0, SSL_CHACHA20 },
244
    { 0, SSL_TXT_GOST2012_GOST8912_GOST8912, NULL, 0, 0, 0, SSL_eGOST2814789CNT12 },
245
246
    { 0, SSL_TXT_ARIA, NULL, 0, 0, 0, SSL_ARIA },
247
    { 0, SSL_TXT_ARIA_GCM, NULL, 0, 0, 0, SSL_ARIA128GCM | SSL_ARIA256GCM },
248
    { 0, SSL_TXT_ARIA128, NULL, 0, 0, 0, SSL_ARIA128GCM },
249
    { 0, SSL_TXT_ARIA256, NULL, 0, 0, 0, SSL_ARIA256GCM },
250
    { 0, SSL_TXT_CBC, NULL, 0, 0, 0, SSL_CBC },
251
252
    /* MAC aliases */
253
    { 0, SSL_TXT_MD5, NULL, 0, 0, 0, 0, SSL_MD5 },
254
    { 0, SSL_TXT_SHA1, NULL, 0, 0, 0, 0, SSL_SHA1 },
255
    { 0, SSL_TXT_SHA, NULL, 0, 0, 0, 0, SSL_SHA1 },
256
    { 0, SSL_TXT_GOST94, NULL, 0, 0, 0, 0, SSL_GOST94 },
257
    { 0, SSL_TXT_GOST89MAC, NULL, 0, 0, 0, 0, SSL_GOST89MAC | SSL_GOST89MAC12 },
258
    { 0, SSL_TXT_SHA256, NULL, 0, 0, 0, 0, SSL_SHA256 },
259
    { 0, SSL_TXT_SHA384, NULL, 0, 0, 0, 0, SSL_SHA384 },
260
    { 0, SSL_TXT_GOST12, NULL, 0, 0, 0, 0, SSL_GOST12_256 },
261
262
    /* protocol version aliases */
263
    { 0, SSL_TXT_SSLV3, NULL, 0, 0, 0, 0, 0, SSL3_VERSION },
264
    { 0, SSL_TXT_TLSV1, NULL, 0, 0, 0, 0, 0, TLS1_VERSION },
265
    { 0, "TLSv1.0", NULL, 0, 0, 0, 0, 0, TLS1_VERSION },
266
    { 0, SSL_TXT_TLSV1_2, NULL, 0, 0, 0, 0, 0, TLS1_2_VERSION },
267
268
    /* strength classes */
269
    { 0, SSL_TXT_LOW, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_LOW },
270
    { 0, SSL_TXT_MEDIUM, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_MEDIUM },
271
    { 0, SSL_TXT_HIGH, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, SSL_HIGH },
272
    /* FIPS 140-2 approved ciphersuite */
273
    { 0, SSL_TXT_FIPS, NULL, 0, 0, 0, ~SSL_eNULL, 0, 0, 0, 0, 0, SSL_FIPS },
274
275
    /* "EDH-" aliases to "DHE-" labels (for backward compatibility) */
276
    { 0, SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA, NULL, 0,
277
        SSL_kDHE, SSL_aDSS, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS },
278
    { 0, SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA, NULL, 0,
279
        SSL_kDHE, SSL_aRSA, SSL_3DES, SSL_SHA1, 0, 0, 0, 0, SSL_HIGH | SSL_FIPS },
280
281
};
282
283
#ifndef OPENSSL_NO_DEPRECATED_3_6
284
/*
285
 * Search for public key algorithm with given name and return its pkey_id if
286
 * it is available. Otherwise return 0
287
 */
288
#ifdef OPENSSL_NO_ENGINE
289
290
static int get_optional_pkey_id(const char *pkey_name)
291
{
292
    const EVP_PKEY_ASN1_METHOD *ameth;
293
    int pkey_id = 0;
294
    ameth = EVP_PKEY_asn1_find_str(NULL, pkey_name, -1);
295
    if (ameth && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth) > 0)
296
        return pkey_id;
297
    return 0;
298
}
299
300
#else
301
302
static int get_optional_pkey_id(const char *pkey_name)
303
937k
{
304
937k
    const EVP_PKEY_ASN1_METHOD *ameth;
305
937k
    ENGINE *tmpeng = NULL;
306
937k
    int pkey_id = 0;
307
937k
    ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
308
937k
    if (ameth) {
309
0
        if (EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
310
0
                ameth)
311
0
            <= 0)
312
0
            pkey_id = 0;
313
0
    }
314
937k
    tls_engine_finish(tmpeng);
315
937k
    return pkey_id;
316
937k
}
317
#endif
318
#else
319
static int get_optional_pkey_id(const char *pkey_name)
320
{
321
    (void)pkey_name;
322
    return 0;
323
}
324
#endif
325
326
int ssl_load_ciphers(SSL_CTX *ctx)
327
120k
{
328
120k
    size_t i;
329
120k
    const ssl_cipher_table *t;
330
120k
    EVP_KEYEXCH *kex = NULL;
331
120k
    EVP_SIGNATURE *sig = NULL;
332
333
120k
    ctx->disabled_enc_mask = 0;
334
3.01M
    for (i = 0, t = ssl_cipher_table_cipher; i < SSL_ENC_NUM_IDX; i++, t++) {
335
2.89M
        if (t->nid != NID_undef) {
336
2.77M
            const EVP_CIPHER *cipher
337
2.77M
                = ssl_evp_cipher_fetch(ctx->libctx, t->nid, ctx->propq);
338
339
2.77M
            ctx->ssl_cipher_methods[i] = cipher;
340
2.77M
            if (cipher == NULL)
341
1.08M
                ctx->disabled_enc_mask |= t->mask;
342
2.77M
        }
343
2.89M
    }
344
120k
    ctx->disabled_mac_mask = 0;
345
1.80M
    for (i = 0, t = ssl_cipher_table_mac; i < SSL_MD_NUM_IDX; i++, t++) {
346
1.68M
        const EVP_MD *md
347
1.68M
            = ssl_evp_md_fetch(ctx->libctx, t->nid, ctx->propq);
348
349
1.68M
        ctx->ssl_digest_methods[i] = md;
350
1.68M
        if (md == NULL) {
351
843k
            ctx->disabled_mac_mask |= t->mask;
352
843k
        } else {
353
843k
            int tmpsize = EVP_MD_get_size(md);
354
355
843k
            if (!ossl_assert(tmpsize > 0))
356
0
                return 0;
357
843k
            ctx->ssl_mac_secret_size[i] = tmpsize;
358
843k
        }
359
1.68M
    }
360
361
120k
    ctx->disabled_mkey_mask = 0;
362
120k
    ctx->disabled_auth_mask = 0;
363
364
    /*
365
     * We ignore any errors from the fetches below. They are expected to fail
366
     * if these algorithms are not available.
367
     */
368
120k
    ERR_set_mark();
369
120k
    sig = EVP_SIGNATURE_fetch(ctx->libctx, "DSA", ctx->propq);
370
120k
    if (sig == NULL)
371
0
        ctx->disabled_auth_mask |= SSL_aDSS;
372
120k
    else
373
120k
        EVP_SIGNATURE_free(sig);
374
120k
    kex = EVP_KEYEXCH_fetch(ctx->libctx, "DH", ctx->propq);
375
120k
    if (kex == NULL)
376
0
        ctx->disabled_mkey_mask |= SSL_kDHE | SSL_kDHEPSK;
377
120k
    else
378
120k
        EVP_KEYEXCH_free(kex);
379
120k
    kex = EVP_KEYEXCH_fetch(ctx->libctx, "ECDH", ctx->propq);
380
120k
    if (kex == NULL)
381
0
        ctx->disabled_mkey_mask |= SSL_kECDHE | SSL_kECDHEPSK;
382
120k
    else
383
120k
        EVP_KEYEXCH_free(kex);
384
120k
    sig = EVP_SIGNATURE_fetch(ctx->libctx, "ECDSA", ctx->propq);
385
120k
    if (sig == NULL)
386
0
        ctx->disabled_auth_mask |= SSL_aECDSA;
387
120k
    else
388
120k
        EVP_SIGNATURE_free(sig);
389
120k
    ERR_pop_to_mark();
390
391
#ifdef OPENSSL_NO_PSK
392
    ctx->disabled_mkey_mask |= SSL_PSK;
393
    ctx->disabled_auth_mask |= SSL_aPSK;
394
#endif
395
#ifdef OPENSSL_NO_SRP
396
    ctx->disabled_mkey_mask |= SSL_kSRP;
397
#endif
398
399
    /*
400
     * Check for presence of GOST 34.10 algorithms, and if they are not
401
     * present, disable appropriate auth and key exchange
402
     */
403
120k
    memcpy(ctx->ssl_mac_pkey_id, default_mac_pkey_id,
404
120k
        sizeof(ctx->ssl_mac_pkey_id));
405
406
120k
    ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] = get_optional_pkey_id(SN_id_Gost28147_89_MAC);
407
120k
    if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
408
0
        ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
409
120k
    else
410
120k
        ctx->disabled_mac_mask |= SSL_GOST89MAC;
411
412
120k
    ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] = get_optional_pkey_id(SN_gost_mac_12);
413
120k
    if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
414
0
        ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
415
120k
    else
416
120k
        ctx->disabled_mac_mask |= SSL_GOST89MAC12;
417
418
120k
    ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX] = get_optional_pkey_id(SN_magma_mac);
419
120k
    if (ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX])
420
0
        ctx->ssl_mac_secret_size[SSL_MD_MAGMAOMAC_IDX] = 32;
421
120k
    else
422
120k
        ctx->disabled_mac_mask |= SSL_MAGMAOMAC;
423
424
120k
    ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX] = get_optional_pkey_id(SN_kuznyechik_mac);
425
120k
    if (ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX])
426
0
        ctx->ssl_mac_secret_size[SSL_MD_KUZNYECHIKOMAC_IDX] = 32;
427
120k
    else
428
120k
        ctx->disabled_mac_mask |= SSL_KUZNYECHIKOMAC;
429
430
120k
    if (!get_optional_pkey_id(SN_id_GostR3410_2001))
431
120k
        ctx->disabled_auth_mask |= SSL_aGOST01 | SSL_aGOST12;
432
120k
    if (!get_optional_pkey_id(SN_id_GostR3410_2012_256))
433
120k
        ctx->disabled_auth_mask |= SSL_aGOST12;
434
120k
    if (!get_optional_pkey_id(SN_id_GostR3410_2012_512))
435
120k
        ctx->disabled_auth_mask |= SSL_aGOST12;
436
    /*
437
     * Disable GOST key exchange if no GOST signature algs are available *
438
     */
439
120k
    if ((ctx->disabled_auth_mask & (SSL_aGOST01 | SSL_aGOST12)) == (SSL_aGOST01 | SSL_aGOST12))
440
120k
        ctx->disabled_mkey_mask |= SSL_kGOST;
441
442
120k
    if ((ctx->disabled_auth_mask & SSL_aGOST12) == SSL_aGOST12)
443
120k
        ctx->disabled_mkey_mask |= SSL_kGOST18;
444
445
120k
    return 1;
446
120k
}
447
448
int ssl_cipher_get_evp_cipher(SSL_CTX *ctx, const SSL_CIPHER *sslc,
449
    const EVP_CIPHER **enc)
450
51.2k
{
451
51.2k
    int i = ssl_cipher_info_lookup(ssl_cipher_table_cipher,
452
51.2k
        sslc->algorithm_enc);
453
454
51.2k
    if (i == -1) {
455
0
        *enc = NULL;
456
51.2k
    } else {
457
51.2k
        if (i == SSL_ENC_NULL_IDX) {
458
            /*
459
             * We assume we don't care about this coming from an ENGINE so
460
             * just do a normal EVP_CIPHER_fetch instead of
461
             * ssl_evp_cipher_fetch()
462
             */
463
6.86k
            *enc = EVP_CIPHER_fetch(ctx->libctx, "NULL", ctx->propq);
464
6.86k
            if (*enc == NULL)
465
0
                return 0;
466
44.3k
        } else {
467
44.3k
            const EVP_CIPHER *cipher = ctx->ssl_cipher_methods[i];
468
469
44.3k
            if (cipher == NULL
470
44.3k
                || !ssl_evp_cipher_up_ref(cipher))
471
0
                return 0;
472
44.3k
            *enc = ctx->ssl_cipher_methods[i];
473
44.3k
        }
474
51.2k
    }
475
51.2k
    return 1;
476
51.2k
}
477
478
int ssl_cipher_get_evp_md_mac(SSL_CTX *ctx, const SSL_CIPHER *sslc,
479
    const EVP_MD **md,
480
    int *mac_pkey_type, size_t *mac_secret_size)
481
38.6k
{
482
38.6k
    int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, sslc->algorithm_mac);
483
484
38.6k
    if (i == -1) {
485
25.7k
        *md = NULL;
486
25.7k
        if (mac_pkey_type != NULL)
487
25.7k
            *mac_pkey_type = NID_undef;
488
25.7k
        if (mac_secret_size != NULL)
489
25.7k
            *mac_secret_size = 0;
490
25.7k
    } else {
491
12.8k
        const EVP_MD *digest = ctx->ssl_digest_methods[i];
492
493
12.8k
        if (digest == NULL || !ssl_evp_md_up_ref(digest))
494
0
            return 0;
495
496
12.8k
        *md = digest;
497
12.8k
        if (mac_pkey_type != NULL)
498
12.8k
            *mac_pkey_type = ctx->ssl_mac_pkey_id[i];
499
12.8k
        if (mac_secret_size != NULL)
500
12.8k
            *mac_secret_size = ctx->ssl_mac_secret_size[i];
501
12.8k
    }
502
38.6k
    return 1;
503
38.6k
}
504
505
int ssl_cipher_get_evp(SSL_CTX *ctx, const SSL_SESSION *s,
506
    const EVP_CIPHER **enc, const EVP_MD **md,
507
    int *mac_pkey_type, size_t *mac_secret_size,
508
    SSL_COMP **comp, int use_etm)
509
38.6k
{
510
38.6k
    int i;
511
38.6k
    const SSL_CIPHER *c;
512
513
38.6k
    c = s->cipher;
514
38.6k
    if (c == NULL)
515
0
        return 0;
516
38.6k
    if (comp != NULL) {
517
16.5k
        SSL_COMP ctmp;
518
16.5k
        STACK_OF(SSL_COMP) *comp_methods;
519
520
16.5k
        *comp = NULL;
521
16.5k
        ctmp.id = s->compress_meth;
522
16.5k
        comp_methods = SSL_COMP_get_compression_methods();
523
16.5k
        if (comp_methods != NULL) {
524
16.5k
            i = sk_SSL_COMP_find(comp_methods, &ctmp);
525
16.5k
            if (i >= 0)
526
0
                *comp = sk_SSL_COMP_value(comp_methods, i);
527
16.5k
        }
528
        /* If were only interested in comp then return success */
529
16.5k
        if ((enc == NULL) && (md == NULL))
530
4
            return 1;
531
16.5k
    }
532
533
38.6k
    if ((enc == NULL) || (md == NULL))
534
0
        return 0;
535
536
38.6k
    if (!ssl_cipher_get_evp_cipher(ctx, c, enc))
537
0
        return 0;
538
539
38.6k
    if (!ssl_cipher_get_evp_md_mac(ctx, c, md, mac_pkey_type,
540
38.6k
            mac_secret_size)) {
541
0
        ssl_evp_cipher_free(*enc);
542
0
        return 0;
543
0
    }
544
545
38.6k
    if ((*enc != NULL)
546
38.6k
        && (*md != NULL
547
25.7k
            || (EVP_CIPHER_get_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
548
38.6k
        && (c->algorithm_mac == SSL_AEAD
549
38.6k
            || mac_pkey_type == NULL || *mac_pkey_type != NID_undef)) {
550
38.6k
        const EVP_CIPHER *evp = NULL;
551
552
38.6k
        if (use_etm
553
37.7k
            || s->ssl_version >> 8 != TLS1_VERSION_MAJOR
554
30.6k
            || s->ssl_version < TLS1_VERSION)
555
8.01k
            return 1;
556
557
30.6k
        if (c->algorithm_enc == SSL_RC4
558
0
            && c->algorithm_mac == SSL_MD5)
559
0
            evp = ssl_evp_cipher_fetch(ctx->libctx, NID_rc4_hmac_md5,
560
0
                ctx->propq);
561
30.6k
        else if (c->algorithm_enc == SSL_AES128
562
1.88k
            && c->algorithm_mac == SSL_SHA1)
563
594
            evp = ssl_evp_cipher_fetch(ctx->libctx,
564
594
                NID_aes_128_cbc_hmac_sha1,
565
594
                ctx->propq);
566
30.0k
        else if (c->algorithm_enc == SSL_AES256
567
1.02k
            && c->algorithm_mac == SSL_SHA1)
568
836
            evp = ssl_evp_cipher_fetch(ctx->libctx,
569
836
                NID_aes_256_cbc_hmac_sha1,
570
836
                ctx->propq);
571
29.1k
        else if (c->algorithm_enc == SSL_AES128
572
1.29k
            && c->algorithm_mac == SSL_SHA256)
573
1.29k
            evp = ssl_evp_cipher_fetch(ctx->libctx,
574
1.29k
                NID_aes_128_cbc_hmac_sha256,
575
1.29k
                ctx->propq);
576
27.8k
        else if (c->algorithm_enc == SSL_AES256
577
186
            && c->algorithm_mac == SSL_SHA256)
578
120
            evp = ssl_evp_cipher_fetch(ctx->libctx,
579
120
                NID_aes_256_cbc_hmac_sha256,
580
120
                ctx->propq);
581
582
30.6k
        if (evp != NULL) {
583
2.84k
            ssl_evp_cipher_free(*enc);
584
2.84k
            ssl_evp_md_free(*md);
585
2.84k
            *enc = evp;
586
2.84k
            *md = NULL;
587
2.84k
        }
588
30.6k
        return 1;
589
38.6k
    }
590
591
0
    return 0;
592
38.6k
}
593
594
const EVP_MD *ssl_md(SSL_CTX *ctx, int idx)
595
4.16M
{
596
4.16M
    idx &= SSL_HANDSHAKE_MAC_MASK;
597
4.16M
    if (idx < 0 || idx >= SSL_MD_NUM_IDX)
598
0
        return NULL;
599
4.16M
    return ctx->ssl_digest_methods[idx];
600
4.16M
}
601
602
const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s)
603
392k
{
604
392k
    return ssl_md(SSL_CONNECTION_GET_CTX(s), ssl_get_algorithm2(s));
605
392k
}
606
607
const EVP_MD *ssl_prf_md(SSL_CONNECTION *s)
608
68.0k
{
609
68.0k
    return ssl_md(SSL_CONNECTION_GET_CTX(s),
610
68.0k
        ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
611
68.0k
}
612
613
#define ITEM_SEP(a) \
614
1.26M
    (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
615
616
static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
617
    CIPHER_ORDER **tail)
618
132M
{
619
132M
    if (curr == *tail)
620
225k
        return;
621
132M
    if (curr == *head)
622
44.2M
        *head = curr->next;
623
132M
    if (curr->prev != NULL)
624
88.0M
        curr->prev->next = curr->next;
625
132M
    if (curr->next != NULL)
626
132M
        curr->next->prev = curr->prev;
627
132M
    (*tail)->next = curr;
628
132M
    curr->prev = *tail;
629
132M
    curr->next = NULL;
630
132M
    *tail = curr;
631
132M
}
632
633
static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
634
    CIPHER_ORDER **tail)
635
102M
{
636
102M
    if (curr == *head)
637
0
        return;
638
102M
    if (curr == *tail)
639
45.8M
        *tail = curr->prev;
640
102M
    if (curr->next != NULL)
641
56.9M
        curr->next->prev = curr->prev;
642
102M
    if (curr->prev != NULL)
643
102M
        curr->prev->next = curr->next;
644
102M
    (*head)->prev = curr;
645
102M
    curr->next = *head;
646
102M
    curr->prev = NULL;
647
102M
    *head = curr;
648
102M
}
649
650
static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
651
    int num_of_ciphers,
652
    uint32_t disabled_mkey,
653
    uint32_t disabled_auth,
654
    uint32_t disabled_enc,
655
    uint32_t disabled_mac,
656
    CIPHER_ORDER *co_list,
657
    CIPHER_ORDER **head_p,
658
    CIPHER_ORDER **tail_p)
659
276k
{
660
276k
    int i, co_list_num;
661
276k
    const SSL_CIPHER *c;
662
663
    /*
664
     * We have num_of_ciphers descriptions compiled in, depending on the
665
     * method selected (SSLv3, TLSv1 etc).
666
     * These will later be sorted in a linked list with at most num
667
     * entries.
668
     */
669
670
    /* Get the initial list of ciphers */
671
276k
    co_list_num = 0; /* actual count of ciphers */
672
43.4M
    for (i = 0; i < num_of_ciphers; i++) {
673
43.1M
        c = ssl_method->get_cipher(i);
674
        /* drop those that use any of that is not available */
675
43.1M
        if (c == NULL || !c->valid)
676
0
            continue;
677
43.1M
        if ((c->algorithm_mkey & disabled_mkey) || (c->algorithm_auth & disabled_auth) || (c->algorithm_enc & disabled_enc) || (c->algorithm_mac & disabled_mac))
678
4.96M
            continue;
679
38.1M
        if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) == 0) && c->min_tls == 0)
680
0
            continue;
681
38.1M
        if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) != 0) && c->min_dtls == 0)
682
0
            continue;
683
684
38.1M
        co_list[co_list_num].cipher = c;
685
38.1M
        co_list[co_list_num].next = NULL;
686
38.1M
        co_list[co_list_num].prev = NULL;
687
38.1M
        co_list[co_list_num].active = 0;
688
38.1M
        co_list_num++;
689
38.1M
    }
690
691
    /*
692
     * Prepare linked list from list entries
693
     */
694
276k
    if (co_list_num > 0) {
695
225k
        co_list[0].prev = NULL;
696
697
225k
        if (co_list_num > 1) {
698
225k
            co_list[0].next = &co_list[1];
699
700
37.9M
            for (i = 1; i < co_list_num - 1; i++) {
701
37.7M
                co_list[i].prev = &co_list[i - 1];
702
37.7M
                co_list[i].next = &co_list[i + 1];
703
37.7M
            }
704
705
225k
            co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
706
225k
        }
707
708
225k
        co_list[co_list_num - 1].next = NULL;
709
710
225k
        *head_p = &co_list[0];
711
225k
        *tail_p = &co_list[co_list_num - 1];
712
225k
    }
713
276k
}
714
715
static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
716
    int num_of_group_aliases,
717
    uint32_t disabled_mkey,
718
    uint32_t disabled_auth,
719
    uint32_t disabled_enc,
720
    uint32_t disabled_mac,
721
    CIPHER_ORDER *head)
722
276k
{
723
276k
    CIPHER_ORDER *ciph_curr;
724
276k
    const SSL_CIPHER **ca_curr;
725
276k
    int i;
726
276k
    uint32_t mask_mkey = ~disabled_mkey;
727
276k
    uint32_t mask_auth = ~disabled_auth;
728
276k
    uint32_t mask_enc = ~disabled_enc;
729
276k
    uint32_t mask_mac = ~disabled_mac;
730
731
    /*
732
     * First, add the real ciphers as already collected
733
     */
734
276k
    ciph_curr = head;
735
276k
    ca_curr = ca_list;
736
38.4M
    while (ciph_curr != NULL) {
737
38.1M
        *ca_curr = ciph_curr->cipher;
738
38.1M
        ca_curr++;
739
38.1M
        ciph_curr = ciph_curr->next;
740
38.1M
    }
741
742
    /*
743
     * Now we add the available ones from the cipher_aliases[] table.
744
     * They represent either one or more algorithms, some of which
745
     * in any affected category must be supported (set in enabled_mask),
746
     * or represent a cipher strength value (will be added in any case because algorithms=0).
747
     */
748
22.0M
    for (i = 0; i < num_of_group_aliases; i++) {
749
21.7M
        uint32_t algorithm_mkey = cipher_aliases[i].algorithm_mkey;
750
21.7M
        uint32_t algorithm_auth = cipher_aliases[i].algorithm_auth;
751
21.7M
        uint32_t algorithm_enc = cipher_aliases[i].algorithm_enc;
752
21.7M
        uint32_t algorithm_mac = cipher_aliases[i].algorithm_mac;
753
754
21.7M
        if (algorithm_mkey)
755
6.91M
            if ((algorithm_mkey & mask_mkey) == 0)
756
553k
                continue;
757
758
21.2M
        if (algorithm_auth)
759
5.53M
            if ((algorithm_auth & mask_auth) == 0)
760
829k
                continue;
761
762
20.4M
        if (algorithm_enc)
763
8.01M
            if ((algorithm_enc & mask_enc) == 0)
764
1.65M
                continue;
765
766
18.7M
        if (algorithm_mac)
767
2.76M
            if ((algorithm_mac & mask_mac) == 0)
768
829k
                continue;
769
770
17.9M
        *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
771
17.9M
        ca_curr++;
772
17.9M
    }
773
774
276k
    *ca_curr = NULL; /* end of list */
775
276k
}
776
777
static void ssl_cipher_apply_rule(uint32_t cipher_id, uint32_t alg_mkey,
778
    uint32_t alg_auth, uint32_t alg_enc,
779
    uint32_t alg_mac, int min_tls,
780
    uint32_t algo_strength, int rule,
781
    int32_t strength_bits, CIPHER_ORDER **head_p,
782
    CIPHER_ORDER **tail_p)
783
6.52M
{
784
6.52M
    CIPHER_ORDER *head, *tail, *curr, *next, *last;
785
6.52M
    const SSL_CIPHER *cp;
786
6.52M
    int reverse = 0;
787
788
6.52M
    OSSL_TRACE_BEGIN(TLS_CIPHER)
789
0
    {
790
0
        BIO_printf(trc_out,
791
0
            "Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
792
0
            rule, (unsigned int)alg_mkey, (unsigned int)alg_auth,
793
0
            (unsigned int)alg_enc, (unsigned int)alg_mac, min_tls,
794
0
            (unsigned int)algo_strength, (int)strength_bits);
795
0
    }
796
797
6.52M
    if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
798
1.65M
        reverse = 1; /* needed to maintain sorting between currently
799
                      * deleted ciphers */
800
801
6.52M
    head = *head_p;
802
6.52M
    tail = *tail_p;
803
804
6.52M
    if (reverse) {
805
1.65M
        next = tail;
806
1.65M
        last = head;
807
4.86M
    } else {
808
4.86M
        next = head;
809
4.86M
        last = tail;
810
4.86M
    }
811
812
6.52M
    curr = NULL;
813
927M
    for (;;) {
814
927M
        if (curr == last)
815
6.52M
            break;
816
817
920M
        curr = next;
818
819
920M
        if (curr == NULL)
820
0
            break;
821
822
920M
        next = reverse ? curr->prev : curr->next;
823
824
920M
        cp = curr->cipher;
825
826
        /*
827
         * Selection criteria is either the value of strength_bits
828
         * or the algorithms used.
829
         */
830
920M
        if (strength_bits >= 0) {
831
186M
            if (strength_bits != cp->strength_bits)
832
148M
                continue;
833
733M
        } else {
834
733M
            if (trc_out != NULL) {
835
0
                BIO_printf(trc_out,
836
0
                    "\nName: %s:"
837
0
                    "\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
838
0
                    cp->name,
839
0
                    (unsigned int)cp->algorithm_mkey,
840
0
                    (unsigned int)cp->algorithm_auth,
841
0
                    (unsigned int)cp->algorithm_enc,
842
0
                    (unsigned int)cp->algorithm_mac,
843
0
                    cp->min_tls,
844
0
                    (unsigned int)cp->algo_strength);
845
0
            }
846
733M
            if (cipher_id != 0 && (cipher_id != cp->id))
847
0
                continue;
848
733M
            if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
849
201M
                continue;
850
532M
            if (alg_auth && !(alg_auth & cp->algorithm_auth))
851
38.6M
                continue;
852
494M
            if (alg_enc && !(alg_enc & cp->algorithm_enc))
853
159M
                continue;
854
334M
            if (alg_mac && !(alg_mac & cp->algorithm_mac))
855
72.9M
                continue;
856
261M
            if (min_tls && (min_tls != cp->min_tls))
857
18.7M
                continue;
858
243M
            if ((algo_strength & SSL_STRONG_MASK)
859
0
                && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
860
0
                continue;
861
243M
            if ((algo_strength & SSL_DEFAULT_MASK)
862
19.0M
                && !(algo_strength & SSL_DEFAULT_MASK & cp->algo_strength))
863
8.46M
                continue;
864
243M
        }
865
866
272M
        if (trc_out != NULL)
867
0
            BIO_printf(trc_out, "Action = %d\n", rule);
868
869
        /* add the cipher if it has not been added yet. */
870
272M
        if (rule == CIPHER_ADD) {
871
            /* reverse == 0 */
872
106M
            if (!curr->active) {
873
81.9M
                ll_append_tail(&head, curr, &tail);
874
81.9M
                curr->active = 1;
875
81.9M
            }
876
106M
        }
877
        /* Move the added cipher to this location */
878
165M
        else if (rule == CIPHER_ORD) {
879
            /* reverse == 0 */
880
50.5M
            if (curr->active) {
881
50.5M
                ll_append_tail(&head, curr, &tail);
882
50.5M
            }
883
115M
        } else if (rule == CIPHER_DEL) {
884
            /* reverse == 1 */
885
45.8M
            if (curr->active) {
886
                /*
887
                 * most recently deleted ciphersuites get best positions for
888
                 * any future CIPHER_ADD (note that the CIPHER_DEL loop works
889
                 * in reverse to maintain the order)
890
                 */
891
45.8M
                ll_append_head(&head, curr, &tail);
892
45.8M
                curr->active = 0;
893
45.8M
            }
894
69.5M
        } else if (rule == CIPHER_BUMP) {
895
56.9M
            if (curr->active)
896
56.9M
                ll_append_head(&head, curr, &tail);
897
56.9M
        } else if (rule == CIPHER_KILL) {
898
            /* reverse == 0 */
899
12.6M
            if (head == curr)
900
2.03M
                head = curr->next;
901
10.6M
            else
902
10.6M
                curr->prev->next = curr->next;
903
12.6M
            if (tail == curr)
904
112k
                tail = curr->prev;
905
12.6M
            curr->active = 0;
906
12.6M
            if (curr->next != NULL)
907
12.5M
                curr->next->prev = curr->prev;
908
12.6M
            if (curr->prev != NULL)
909
10.6M
                curr->prev->next = curr->next;
910
12.6M
            curr->next = NULL;
911
12.6M
            curr->prev = NULL;
912
12.6M
        }
913
272M
    }
914
915
6.52M
    *head_p = head;
916
6.52M
    *tail_p = tail;
917
918
6.52M
    OSSL_TRACE_END(TLS_CIPHER);
919
6.52M
}
920
921
static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
922
    CIPHER_ORDER **tail_p)
923
276k
{
924
276k
    int32_t max_strength_bits;
925
276k
    int i, *number_uses;
926
276k
    CIPHER_ORDER *curr;
927
928
    /*
929
     * This routine sorts the ciphers with descending strength. The sorting
930
     * must keep the pre-sorted sequence, so we apply the normal sorting
931
     * routine as '+' movement to the end of the list.
932
     */
933
276k
    max_strength_bits = 0;
934
276k
    curr = *head_p;
935
38.4M
    while (curr != NULL) {
936
38.1M
        if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
937
225k
            max_strength_bits = curr->cipher->strength_bits;
938
38.1M
        curr = curr->next;
939
38.1M
    }
940
941
276k
    number_uses = OPENSSL_calloc(max_strength_bits + 1, sizeof(int));
942
276k
    if (number_uses == NULL)
943
0
        return 0;
944
945
    /*
946
     * Now find the strength_bits values actually used
947
     */
948
276k
    curr = *head_p;
949
38.4M
    while (curr != NULL) {
950
38.1M
        if (curr->active)
951
38.1M
            number_uses[curr->cipher->strength_bits]++;
952
38.1M
        curr = curr->next;
953
38.1M
    }
954
    /*
955
     * Go through the list of used strength_bits values in descending
956
     * order.
957
     */
958
58.3M
    for (i = max_strength_bits; i >= 0; i--)
959
58.0M
        if (number_uses[i] > 0)
960
1.10M
            ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
961
1.10M
                tail_p);
962
963
276k
    OPENSSL_free(number_uses);
964
276k
    return 1;
965
276k
}
966
967
static int ssl_cipher_process_rulestr(const char *rule_str,
968
    CIPHER_ORDER **head_p,
969
    CIPHER_ORDER **tail_p,
970
    const SSL_CIPHER **ca_list, CERT *c)
971
253k
{
972
253k
    uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
973
253k
    int min_tls;
974
253k
    const char *l, *buf;
975
253k
    int j, multi, found, rule, retval, ok, buflen;
976
253k
    uint32_t cipher_id = 0;
977
253k
    char ch;
978
979
253k
    retval = 1;
980
253k
    l = rule_str;
981
1.26M
    for (;;) {
982
1.26M
        ch = *l;
983
984
1.26M
        if (ch == '\0')
985
0
            break; /* done */
986
1.26M
        if (ch == '-') {
987
0
            rule = CIPHER_DEL;
988
0
            l++;
989
1.26M
        } else if (ch == '+') {
990
0
            rule = CIPHER_ORD;
991
0
            l++;
992
1.26M
        } else if (ch == '!') {
993
304k
            rule = CIPHER_KILL;
994
304k
            l++;
995
963k
        } else if (ch == '@') {
996
101k
            rule = CIPHER_SPECIAL;
997
101k
            l++;
998
862k
        } else {
999
862k
            rule = CIPHER_ADD;
1000
862k
        }
1001
1002
1.26M
        if (ITEM_SEP(ch)) {
1003
507k
            l++;
1004
507k
            continue;
1005
507k
        }
1006
1007
760k
        alg_mkey = 0;
1008
760k
        alg_auth = 0;
1009
760k
        alg_enc = 0;
1010
760k
        alg_mac = 0;
1011
760k
        min_tls = 0;
1012
760k
        algo_strength = 0;
1013
1014
760k
        for (;;) {
1015
760k
            ch = *l;
1016
760k
            buf = l;
1017
760k
            buflen = 0;
1018
760k
#ifndef CHARSET_EBCDIC
1019
6.69M
            while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '_') || (ch == '.') || (ch == '='))
1020
#else
1021
            while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '_') || (ch == '.')
1022
                || (ch == '='))
1023
#endif
1024
5.93M
            {
1025
5.93M
                ch = *(++l);
1026
5.93M
                buflen++;
1027
5.93M
            }
1028
1029
760k
            if (buflen == 0) {
1030
                /*
1031
                 * We hit something we cannot deal with,
1032
                 * it is no command or separator nor
1033
                 * alphanumeric, so we call this an error.
1034
                 */
1035
0
                ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
1036
0
                return 0;
1037
0
            }
1038
1039
760k
            if (rule == CIPHER_SPECIAL) {
1040
101k
                found = 0; /* unused -- avoid compiler warning */
1041
101k
                break; /* special treatment */
1042
101k
            }
1043
1044
            /* check for multi-part specification */
1045
659k
            if (ch == '+') {
1046
0
                multi = 1;
1047
0
                l++;
1048
659k
            } else {
1049
659k
                multi = 0;
1050
659k
            }
1051
1052
            /*
1053
             * Now search for the cipher alias in the ca_list. Be careful
1054
             * with the strncmp, because the "buflen" limitation
1055
             * will make the rule "ADH:SOME" and the cipher
1056
             * "ADH-MY-CIPHER" look like a match for buflen=3.
1057
             * So additionally check whether the cipher name found
1058
             * has the correct length. We can save a strlen() call:
1059
             * just checking for the '\0' at the right place is
1060
             * sufficient, we have to strncmp() anyway. (We cannot
1061
             * use strcmp(), because buf is not '\0' terminated.)
1062
             */
1063
659k
            j = found = 0;
1064
659k
            cipher_id = 0;
1065
95.3M
            while (ca_list[j]) {
1066
95.3M
                if (strncmp(buf, ca_list[j]->name, buflen) == 0
1067
659k
                    && (ca_list[j]->name[buflen] == '\0')) {
1068
659k
                    found = 1;
1069
659k
                    break;
1070
94.6M
                } else if (ca_list[j]->stdname != NULL
1071
85.7M
                    && strncmp(buf, ca_list[j]->stdname, buflen) == 0
1072
0
                    && ca_list[j]->stdname[buflen] == '\0') {
1073
0
                    found = 1;
1074
0
                    break;
1075
0
                } else
1076
94.6M
                    j++;
1077
95.3M
            }
1078
1079
659k
            if (!found)
1080
0
                break; /* ignore this entry */
1081
1082
659k
            if (ca_list[j]->algorithm_mkey) {
1083
0
                if (alg_mkey) {
1084
0
                    alg_mkey &= ca_list[j]->algorithm_mkey;
1085
0
                    if (!alg_mkey) {
1086
0
                        found = 0;
1087
0
                        break;
1088
0
                    }
1089
0
                } else {
1090
0
                    alg_mkey = ca_list[j]->algorithm_mkey;
1091
0
                }
1092
0
            }
1093
1094
659k
            if (ca_list[j]->algorithm_auth) {
1095
0
                if (alg_auth) {
1096
0
                    alg_auth &= ca_list[j]->algorithm_auth;
1097
0
                    if (!alg_auth) {
1098
0
                        found = 0;
1099
0
                        break;
1100
0
                    }
1101
0
                } else {
1102
0
                    alg_auth = ca_list[j]->algorithm_auth;
1103
0
                }
1104
0
            }
1105
1106
659k
            if (ca_list[j]->algorithm_enc) {
1107
507k
                if (alg_enc) {
1108
0
                    alg_enc &= ca_list[j]->algorithm_enc;
1109
0
                    if (!alg_enc) {
1110
0
                        found = 0;
1111
0
                        break;
1112
0
                    }
1113
507k
                } else {
1114
507k
                    alg_enc = ca_list[j]->algorithm_enc;
1115
507k
                }
1116
507k
            }
1117
1118
659k
            if (ca_list[j]->algorithm_mac) {
1119
0
                if (alg_mac) {
1120
0
                    alg_mac &= ca_list[j]->algorithm_mac;
1121
0
                    if (!alg_mac) {
1122
0
                        found = 0;
1123
0
                        break;
1124
0
                    }
1125
0
                } else {
1126
0
                    alg_mac = ca_list[j]->algorithm_mac;
1127
0
                }
1128
0
            }
1129
1130
659k
            if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1131
0
                if (algo_strength & SSL_STRONG_MASK) {
1132
0
                    algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1133
0
                    if (!(algo_strength & SSL_STRONG_MASK)) {
1134
0
                        found = 0;
1135
0
                        break;
1136
0
                    }
1137
0
                } else {
1138
0
                    algo_strength = ca_list[j]->algo_strength & SSL_STRONG_MASK;
1139
0
                }
1140
0
            }
1141
1142
659k
            if (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) {
1143
152k
                if (algo_strength & SSL_DEFAULT_MASK) {
1144
0
                    algo_strength &= (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) | ~SSL_DEFAULT_MASK;
1145
0
                    if (!(algo_strength & SSL_DEFAULT_MASK)) {
1146
0
                        found = 0;
1147
0
                        break;
1148
0
                    }
1149
152k
                } else {
1150
152k
                    algo_strength |= ca_list[j]->algo_strength & SSL_DEFAULT_MASK;
1151
152k
                }
1152
152k
            }
1153
1154
659k
            if (ca_list[j]->valid) {
1155
                /*
1156
                 * explicit ciphersuite found; its protocol version does not
1157
                 * become part of the search pattern!
1158
                 */
1159
1160
0
                cipher_id = ca_list[j]->id;
1161
659k
            } else {
1162
                /*
1163
                 * not an explicit ciphersuite; only in this case, the
1164
                 * protocol version is considered part of the search pattern
1165
                 */
1166
1167
659k
                if (ca_list[j]->min_tls) {
1168
0
                    if (min_tls != 0 && min_tls != ca_list[j]->min_tls) {
1169
0
                        found = 0;
1170
0
                        break;
1171
0
                    } else {
1172
0
                        min_tls = ca_list[j]->min_tls;
1173
0
                    }
1174
0
                }
1175
659k
            }
1176
1177
659k
            if (!multi)
1178
659k
                break;
1179
659k
        }
1180
1181
        /*
1182
         * Ok, we have the rule, now apply it
1183
         */
1184
760k
        if (rule == CIPHER_SPECIAL) { /* special command */
1185
101k
            ok = 0;
1186
101k
            if ((buflen == 8) && HAS_PREFIX(buf, "STRENGTH")) {
1187
0
                ok = ssl_cipher_strength_sort(head_p, tail_p);
1188
101k
            } else if (buflen == 10 && CHECK_AND_SKIP_PREFIX(buf, "SECLEVEL=")) {
1189
101k
                int level = *buf - '0';
1190
101k
                if (level < 0 || level > 5) {
1191
0
                    ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
1192
101k
                } else {
1193
101k
                    c->sec_level = level;
1194
101k
                    ok = 1;
1195
101k
                }
1196
101k
            } else {
1197
0
                ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
1198
0
            }
1199
101k
            if (ok == 0)
1200
0
                retval = 0;
1201
            /*
1202
             * We do not support any "multi" options
1203
             * together with "@", so throw away the
1204
             * rest of the command, if any left, until
1205
             * end or ':' is found.
1206
             */
1207
101k
            while ((*l != '\0') && !ITEM_SEP(*l))
1208
0
                l++;
1209
659k
        } else if (found) {
1210
659k
            ssl_cipher_apply_rule(cipher_id,
1211
659k
                alg_mkey, alg_auth, alg_enc, alg_mac,
1212
659k
                min_tls, algo_strength, rule, -1, head_p,
1213
659k
                tail_p);
1214
659k
        } else {
1215
0
            while ((*l != '\0') && !ITEM_SEP(*l))
1216
0
                l++;
1217
0
        }
1218
760k
        if (*l == '\0')
1219
253k
            break; /* done */
1220
760k
    }
1221
1222
253k
    return retval;
1223
253k
}
1224
1225
static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
1226
    const char **prule_str)
1227
276k
{
1228
276k
    unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
1229
276k
    if (HAS_PREFIX(*prule_str, "SUITEB128ONLY")) {
1230
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
1231
276k
    } else if (HAS_PREFIX(*prule_str, "SUITEB128C2")) {
1232
0
        suiteb_comb2 = 1;
1233
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1234
276k
    } else if (HAS_PREFIX(*prule_str, "SUITEB128")) {
1235
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1236
276k
    } else if (HAS_PREFIX(*prule_str, "SUITEB192")) {
1237
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
1238
0
    }
1239
1240
276k
    if (suiteb_flags) {
1241
0
        c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
1242
0
        c->cert_flags |= suiteb_flags;
1243
276k
    } else {
1244
276k
        suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
1245
276k
    }
1246
1247
276k
    if (!suiteb_flags)
1248
276k
        return 1;
1249
    /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
1250
1251
0
    if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)) {
1252
0
        ERR_raise(ERR_LIB_SSL, SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE);
1253
0
        return 0;
1254
0
    }
1255
1256
0
    switch (suiteb_flags) {
1257
0
    case SSL_CERT_FLAG_SUITEB_128_LOS:
1258
0
        if (suiteb_comb2)
1259
0
            *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1260
0
        else
1261
0
            *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
1262
0
        break;
1263
0
    case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1264
0
        *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
1265
0
        break;
1266
0
    case SSL_CERT_FLAG_SUITEB_192_LOS:
1267
0
        *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1268
0
        break;
1269
0
    }
1270
0
    return 1;
1271
0
}
1272
1273
static int ciphersuite_cb(const char *elem, int len, void *arg)
1274
490k
{
1275
490k
    STACK_OF(SSL_CIPHER) *ciphersuites = (STACK_OF(SSL_CIPHER) *)arg;
1276
490k
    const SSL_CIPHER *cipher;
1277
    /* Arbitrary sized temp buffer for the cipher name. Should be big enough */
1278
490k
    char name[80];
1279
1280
490k
    if (len > (int)(sizeof(name) - 1))
1281
        /* Anyway return 1 so we can parse rest of the list */
1282
0
        return 1;
1283
1284
490k
    memcpy(name, elem, len);
1285
490k
    name[len] = '\0';
1286
1287
490k
    cipher = ssl3_get_cipher_by_std_name(name);
1288
490k
    if (cipher == NULL)
1289
        /* Ciphersuite not found but return 1 to parse rest of the list */
1290
0
        return 1;
1291
1292
490k
    if (!sk_SSL_CIPHER_push(ciphersuites, cipher)) {
1293
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1294
0
        return 0;
1295
0
    }
1296
1297
490k
    return 1;
1298
490k
}
1299
1300
static __owur int set_ciphersuites(STACK_OF(SSL_CIPHER) **currciphers, const char *str)
1301
163k
{
1302
163k
    STACK_OF(SSL_CIPHER) *newciphers = sk_SSL_CIPHER_new_null();
1303
1304
163k
    if (newciphers == NULL)
1305
0
        return 0;
1306
1307
    /* Parse the list. We explicitly allow an empty list */
1308
163k
    if (*str != '\0'
1309
163k
        && (CONF_parse_list(str, ':', 1, ciphersuite_cb, newciphers) <= 0
1310
163k
            || sk_SSL_CIPHER_num(newciphers) == 0)) {
1311
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
1312
0
        sk_SSL_CIPHER_free(newciphers);
1313
0
        return 0;
1314
0
    }
1315
163k
    sk_SSL_CIPHER_free(*currciphers);
1316
163k
    *currciphers = newciphers;
1317
1318
163k
    return 1;
1319
163k
}
1320
1321
static int update_cipher_list_by_id(STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1322
    STACK_OF(SSL_CIPHER) *cipherstack)
1323
276k
{
1324
276k
    STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1325
1326
276k
    if (tmp_cipher_list == NULL) {
1327
0
        return 0;
1328
0
    }
1329
1330
276k
    sk_SSL_CIPHER_free(*cipher_list_by_id);
1331
276k
    *cipher_list_by_id = tmp_cipher_list;
1332
1333
276k
    (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
1334
276k
    sk_SSL_CIPHER_sort(*cipher_list_by_id);
1335
1336
276k
    return 1;
1337
276k
}
1338
1339
static int update_cipher_list(SSL_CTX *ctx,
1340
    STACK_OF(SSL_CIPHER) **cipher_list,
1341
    STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1342
    STACK_OF(SSL_CIPHER) *tls13_ciphersuites)
1343
0
{
1344
0
    int i;
1345
0
    STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(*cipher_list);
1346
1347
0
    if (tmp_cipher_list == NULL)
1348
0
        return 0;
1349
1350
    /*
1351
     * Delete any existing TLSv1.3 ciphersuites. These are always first in the
1352
     * list.
1353
     */
1354
0
    while (sk_SSL_CIPHER_num(tmp_cipher_list) > 0
1355
0
        && sk_SSL_CIPHER_value(tmp_cipher_list, 0)->min_tls
1356
0
            == TLS1_3_VERSION)
1357
0
        (void)sk_SSL_CIPHER_delete(tmp_cipher_list, 0);
1358
1359
    /* Insert the new TLSv1.3 ciphersuites */
1360
0
    for (i = sk_SSL_CIPHER_num(tls13_ciphersuites) - 1; i >= 0; i--) {
1361
0
        const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
1362
1363
        /* Don't include any TLSv1.3 ciphersuites that are disabled */
1364
0
        if ((sslc->algorithm_enc & ctx->disabled_enc_mask) == 0
1365
0
            && (ssl_cipher_table_mac[sslc->algorithm2
1366
0
                    & SSL_HANDSHAKE_MAC_MASK]
1367
0
                       .mask
1368
0
                   & ctx->disabled_mac_mask)
1369
0
                == 0) {
1370
0
            sk_SSL_CIPHER_unshift(tmp_cipher_list, sslc);
1371
0
        }
1372
0
    }
1373
1374
0
    if (!update_cipher_list_by_id(cipher_list_by_id, tmp_cipher_list)) {
1375
0
        sk_SSL_CIPHER_free(tmp_cipher_list);
1376
0
        return 0;
1377
0
    }
1378
1379
0
    sk_SSL_CIPHER_free(*cipher_list);
1380
0
    *cipher_list = tmp_cipher_list;
1381
1382
0
    return 1;
1383
0
}
1384
1385
int SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1386
163k
{
1387
163k
    int ret = set_ciphersuites(&(ctx->tls13_ciphersuites), str);
1388
1389
163k
    if (ret && ctx->cipher_list != NULL)
1390
0
        return update_cipher_list(ctx, &ctx->cipher_list, &ctx->cipher_list_by_id,
1391
0
            ctx->tls13_ciphersuites);
1392
1393
163k
    return ret;
1394
163k
}
1395
1396
int SSL_set_ciphersuites(SSL *s, const char *str)
1397
0
{
1398
0
    STACK_OF(SSL_CIPHER) *cipher_list;
1399
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1400
0
    int ret;
1401
1402
0
    if (sc == NULL)
1403
0
        return 0;
1404
1405
0
    ret = set_ciphersuites(&(sc->tls13_ciphersuites), str);
1406
1407
0
    if (sc->cipher_list == NULL) {
1408
0
        if ((cipher_list = SSL_get_ciphers(s)) != NULL)
1409
0
            sc->cipher_list = sk_SSL_CIPHER_dup(cipher_list);
1410
0
    }
1411
0
    if (ret && sc->cipher_list != NULL)
1412
0
        return update_cipher_list(s->ctx, &sc->cipher_list,
1413
0
            &sc->cipher_list_by_id,
1414
0
            sc->tls13_ciphersuites);
1415
1416
0
    return ret;
1417
0
}
1418
1419
STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
1420
    STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
1421
    STACK_OF(SSL_CIPHER) **cipher_list,
1422
    STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1423
    const char *rule_str,
1424
    CERT *c)
1425
253k
{
1426
253k
    int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases, i;
1427
253k
    uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
1428
253k
    STACK_OF(SSL_CIPHER) *cipherstack;
1429
253k
    const char *rule_p;
1430
253k
    CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1431
253k
    const SSL_CIPHER **ca_list = NULL;
1432
253k
    const SSL_METHOD *ssl_method = ctx->method;
1433
1434
    /*
1435
     * Return with error if nothing to do.
1436
     */
1437
253k
    if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1438
0
        return NULL;
1439
1440
253k
    if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
1441
0
        return NULL;
1442
1443
    /*
1444
     * To reduce the work to do we only want to process the compiled
1445
     * in algorithms, so we first get the mask of disabled ciphers.
1446
     */
1447
1448
253k
    disabled_mkey = ctx->disabled_mkey_mask;
1449
253k
    disabled_auth = ctx->disabled_auth_mask;
1450
253k
    disabled_enc = ctx->disabled_enc_mask;
1451
253k
    disabled_mac = ctx->disabled_mac_mask;
1452
1453
    /*
1454
     * Now we have to collect the available ciphers from the compiled
1455
     * in ciphers. We cannot get more than the number compiled in, so
1456
     * it is used for allocation.
1457
     */
1458
253k
    num_of_ciphers = ssl_method->num_ciphers();
1459
1460
253k
    if (num_of_ciphers > 0) {
1461
202k
        co_list = OPENSSL_malloc_array(num_of_ciphers, sizeof(*co_list));
1462
202k
        if (co_list == NULL)
1463
0
            return NULL; /* Failure */
1464
202k
    }
1465
1466
253k
    ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1467
253k
        disabled_mkey, disabled_auth, disabled_enc,
1468
253k
        disabled_mac, co_list, &head, &tail);
1469
1470
    /* Now arrange all ciphers by preference. */
1471
1472
    /*
1473
     * Everything else being equal, prefer ephemeral ECDH over other key
1474
     * exchange mechanisms.
1475
     * For consistency, prefer ECDSA over RSA (though this only matters if the
1476
     * server has both certificates, and is using the DEFAULT, or a client
1477
     * preference).
1478
     */
1479
253k
    ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD,
1480
253k
        -1, &head, &tail);
1481
253k
    ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
1482
253k
        &tail);
1483
253k
    ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
1484
253k
        &tail);
1485
1486
    /* Within each strength group, we prefer GCM over CHACHA... */
1487
253k
    ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
1488
253k
        &head, &tail);
1489
253k
    ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
1490
253k
        &head, &tail);
1491
1492
    /*
1493
     * ...and generally, our preferred cipher is AES.
1494
     * Note that AEADs will be bumped to take preference after sorting by
1495
     * strength.
1496
     */
1497
253k
    ssl_cipher_apply_rule(0, 0, 0, SSL_AES ^ SSL_AESGCM, 0, 0, 0, CIPHER_ADD,
1498
253k
        -1, &head, &tail);
1499
1500
    /* Temporarily enable everything else for sorting */
1501
253k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1502
1503
    /* Low priority for MD5 */
1504
253k
    ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
1505
253k
        &tail);
1506
1507
    /*
1508
     * Move anonymous ciphers to the end.  Usually, these will remain
1509
     * disabled. (For applications that allow them, they aren't too bad, but
1510
     * we prefer authenticated ciphers.)
1511
     */
1512
253k
    ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1513
253k
        &tail);
1514
1515
253k
    ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1516
253k
        &tail);
1517
253k
    ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1518
253k
        &tail);
1519
1520
    /* RC4 is sort-of broken -- move to the end */
1521
253k
    ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
1522
253k
        &tail);
1523
1524
    /*
1525
     * Now sort by symmetric encryption strength.  The above ordering remains
1526
     * in force within each class
1527
     */
1528
253k
    if (!ssl_cipher_strength_sort(&head, &tail)) {
1529
0
        OPENSSL_free(co_list);
1530
0
        return NULL;
1531
0
    }
1532
1533
    /*
1534
     * Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
1535
     */
1536
253k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
1537
253k
        &head, &tail);
1538
1539
    /*
1540
     * Irrespective of strength, enforce the following order:
1541
     * (EC)DHE + AEAD > (EC)DHE > rest of AEAD > rest.
1542
     * Within each group, ciphers remain sorted by strength and previous
1543
     * preference, i.e.,
1544
     * 1) ECDHE > DHE
1545
     * 2) GCM > CHACHA
1546
     * 3) AES > rest
1547
     * 4) TLS 1.2 > legacy
1548
     *
1549
     * Because we now bump ciphers to the top of the list, we proceed in
1550
     * reverse order of preference.
1551
     */
1552
253k
    ssl_cipher_apply_rule(0, 0, 0, 0, SSL_AEAD, 0, 0, CIPHER_BUMP, -1,
1553
253k
        &head, &tail);
1554
253k
    ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, 0, 0, 0,
1555
253k
        CIPHER_BUMP, -1, &head, &tail);
1556
253k
    ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, SSL_AEAD, 0, 0,
1557
253k
        CIPHER_BUMP, -1, &head, &tail);
1558
1559
    /* Now disable everything (maintaining the ordering!) */
1560
253k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1561
1562
    /*
1563
     * We also need cipher aliases for selecting based on the rule_str.
1564
     * There might be two types of entries in the rule_str: 1) names
1565
     * of ciphers themselves 2) aliases for groups of ciphers.
1566
     * For 1) we need the available ciphers and for 2) the cipher
1567
     * groups of cipher_aliases added together in one list (otherwise
1568
     * we would be happy with just the cipher_aliases table).
1569
     */
1570
253k
    num_of_group_aliases = OSSL_NELEM(cipher_aliases);
1571
253k
    num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1572
253k
    ca_list = OPENSSL_malloc_array(num_of_alias_max, sizeof(*ca_list));
1573
253k
    if (ca_list == NULL) {
1574
0
        OPENSSL_free(co_list);
1575
0
        return NULL; /* Failure */
1576
0
    }
1577
253k
    ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1578
253k
        disabled_mkey, disabled_auth, disabled_enc,
1579
253k
        disabled_mac, head);
1580
1581
    /*
1582
     * If the rule_string begins with DEFAULT, apply the default rule
1583
     * before using the (possibly available) additional rules.
1584
     */
1585
253k
    ok = 1;
1586
253k
    rule_p = rule_str;
1587
253k
    if (HAS_PREFIX(rule_str, "DEFAULT")) {
1588
0
        ok = ssl_cipher_process_rulestr(OSSL_default_cipher_list(),
1589
0
            &head, &tail, ca_list, c);
1590
0
        rule_p += 7;
1591
0
        if (*rule_p == ':')
1592
0
            rule_p++;
1593
0
    }
1594
1595
253k
    if (ok && (rule_p[0] != '\0'))
1596
253k
        ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, c);
1597
1598
253k
    OPENSSL_free(ca_list); /* Not needed anymore */
1599
1600
253k
    if (!ok) { /* Rule processing failure */
1601
0
        OPENSSL_free(co_list);
1602
0
        return NULL;
1603
0
    }
1604
1605
    /*
1606
     * Allocate new "cipherstack" for the result, return with error
1607
     * if we cannot get one.
1608
     */
1609
253k
    if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1610
0
        OPENSSL_free(co_list);
1611
0
        return NULL;
1612
0
    }
1613
1614
    /* Add TLSv1.3 ciphers first - we always prefer those if possible */
1615
1.01M
    for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
1616
760k
        const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
1617
1618
        /* Don't include any TLSv1.3 ciphers that are disabled */
1619
760k
        if ((sslc->algorithm_enc & disabled_enc) != 0
1620
760k
            || (ssl_cipher_table_mac[sslc->algorithm2
1621
760k
                    & SSL_HANDSHAKE_MAC_MASK]
1622
760k
                       .mask
1623
760k
                   & ctx->disabled_mac_mask)
1624
760k
                != 0) {
1625
0
            sk_SSL_CIPHER_delete(tls13_ciphersuites, i);
1626
0
            i--;
1627
0
            continue;
1628
0
        }
1629
1630
760k
        if (!sk_SSL_CIPHER_push(cipherstack, sslc)) {
1631
0
            OPENSSL_free(co_list);
1632
0
            sk_SSL_CIPHER_free(cipherstack);
1633
0
            return NULL;
1634
0
        }
1635
760k
    }
1636
1637
253k
    OSSL_TRACE_BEGIN(TLS_CIPHER)
1638
0
    {
1639
0
        BIO_printf(trc_out, "cipher selection:\n");
1640
0
    }
1641
    /*
1642
     * The cipher selection for the list is done. The ciphers are added
1643
     * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1644
     */
1645
23.1M
    for (curr = head; curr != NULL; curr = curr->next) {
1646
22.9M
        if (curr->active) {
1647
22.9M
            if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1648
0
                OPENSSL_free(co_list);
1649
0
                sk_SSL_CIPHER_free(cipherstack);
1650
0
                OSSL_TRACE_CANCEL(TLS_CIPHER);
1651
0
                return NULL;
1652
0
            }
1653
22.9M
            if (trc_out != NULL)
1654
0
                BIO_printf(trc_out, "<%s>\n", curr->cipher->name);
1655
22.9M
        }
1656
22.9M
    }
1657
253k
    OPENSSL_free(co_list); /* Not needed any longer */
1658
253k
    OSSL_TRACE_END(TLS_CIPHER);
1659
1660
253k
    if (!update_cipher_list_by_id(cipher_list_by_id, cipherstack)) {
1661
0
        sk_SSL_CIPHER_free(cipherstack);
1662
0
        return NULL;
1663
0
    }
1664
253k
    sk_SSL_CIPHER_free(*cipher_list);
1665
253k
    *cipher_list = cipherstack;
1666
1667
253k
    return cipherstack;
1668
253k
}
1669
1670
char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1671
0
{
1672
0
    const char *ver;
1673
0
    const char *kx, *au, *enc, *mac;
1674
0
    uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1675
0
    static const char *const format = "%-30s %-7s Kx=%-8s Au=%-5s Enc=%-22s Mac=%-4s\n";
1676
1677
0
    if (buf == NULL) {
1678
0
        len = 128;
1679
0
        if ((buf = OPENSSL_malloc(len)) == NULL)
1680
0
            return NULL;
1681
0
    } else if (len < 128) {
1682
0
        return NULL;
1683
0
    }
1684
1685
0
    alg_mkey = cipher->algorithm_mkey;
1686
0
    alg_auth = cipher->algorithm_auth;
1687
0
    alg_enc = cipher->algorithm_enc;
1688
0
    alg_mac = cipher->algorithm_mac;
1689
1690
0
    ver = ssl_protocol_to_string(cipher->min_tls);
1691
1692
0
    switch (alg_mkey) {
1693
0
    case SSL_kRSA:
1694
0
        kx = "RSA";
1695
0
        break;
1696
0
    case SSL_kDHE:
1697
0
        kx = "DH";
1698
0
        break;
1699
0
    case SSL_kECDHE:
1700
0
        kx = "ECDH";
1701
0
        break;
1702
0
    case SSL_kPSK:
1703
0
        kx = "PSK";
1704
0
        break;
1705
0
    case SSL_kRSAPSK:
1706
0
        kx = "RSAPSK";
1707
0
        break;
1708
0
    case SSL_kECDHEPSK:
1709
0
        kx = "ECDHEPSK";
1710
0
        break;
1711
0
    case SSL_kDHEPSK:
1712
0
        kx = "DHEPSK";
1713
0
        break;
1714
0
    case SSL_kSRP:
1715
0
        kx = "SRP";
1716
0
        break;
1717
0
    case SSL_kGOST:
1718
0
        kx = "GOST";
1719
0
        break;
1720
0
    case SSL_kGOST18:
1721
0
        kx = "GOST18";
1722
0
        break;
1723
0
    case SSL_kANY:
1724
0
        kx = "any";
1725
0
        break;
1726
0
    default:
1727
0
        kx = "unknown";
1728
0
    }
1729
1730
0
    switch (alg_auth) {
1731
0
    case SSL_aRSA:
1732
0
        au = "RSA";
1733
0
        break;
1734
0
    case SSL_aDSS:
1735
0
        au = "DSS";
1736
0
        break;
1737
0
    case SSL_aNULL:
1738
0
        au = "None";
1739
0
        break;
1740
0
    case SSL_aECDSA:
1741
0
        au = "ECDSA";
1742
0
        break;
1743
0
    case SSL_aPSK:
1744
0
        au = "PSK";
1745
0
        break;
1746
0
    case SSL_aSRP:
1747
0
        au = "SRP";
1748
0
        break;
1749
0
    case SSL_aGOST01:
1750
0
        au = "GOST01";
1751
0
        break;
1752
    /* New GOST ciphersuites have both SSL_aGOST12 and SSL_aGOST01 bits */
1753
0
    case (SSL_aGOST12 | SSL_aGOST01):
1754
0
        au = "GOST12";
1755
0
        break;
1756
0
    case SSL_aANY:
1757
0
        au = "any";
1758
0
        break;
1759
0
    default:
1760
0
        au = "unknown";
1761
0
        break;
1762
0
    }
1763
1764
0
    switch (alg_enc) {
1765
0
    case SSL_DES:
1766
0
        enc = "DES(56)";
1767
0
        break;
1768
0
    case SSL_3DES:
1769
0
        enc = "3DES(168)";
1770
0
        break;
1771
0
    case SSL_RC4:
1772
0
        enc = "RC4(128)";
1773
0
        break;
1774
0
    case SSL_RC2:
1775
0
        enc = "RC2(128)";
1776
0
        break;
1777
0
    case SSL_IDEA:
1778
0
        enc = "IDEA(128)";
1779
0
        break;
1780
0
    case SSL_eNULL:
1781
0
        enc = "None";
1782
0
        break;
1783
0
    case SSL_AES128:
1784
0
        enc = "AES(128)";
1785
0
        break;
1786
0
    case SSL_AES256:
1787
0
        enc = "AES(256)";
1788
0
        break;
1789
0
    case SSL_AES128GCM:
1790
0
        enc = "AESGCM(128)";
1791
0
        break;
1792
0
    case SSL_AES256GCM:
1793
0
        enc = "AESGCM(256)";
1794
0
        break;
1795
0
    case SSL_AES128CCM:
1796
0
        enc = "AESCCM(128)";
1797
0
        break;
1798
0
    case SSL_AES256CCM:
1799
0
        enc = "AESCCM(256)";
1800
0
        break;
1801
0
    case SSL_AES128CCM8:
1802
0
        enc = "AESCCM8(128)";
1803
0
        break;
1804
0
    case SSL_AES256CCM8:
1805
0
        enc = "AESCCM8(256)";
1806
0
        break;
1807
0
    case SSL_CAMELLIA128:
1808
0
        enc = "Camellia(128)";
1809
0
        break;
1810
0
    case SSL_CAMELLIA256:
1811
0
        enc = "Camellia(256)";
1812
0
        break;
1813
0
    case SSL_ARIA128GCM:
1814
0
        enc = "ARIAGCM(128)";
1815
0
        break;
1816
0
    case SSL_ARIA256GCM:
1817
0
        enc = "ARIAGCM(256)";
1818
0
        break;
1819
0
    case SSL_SEED:
1820
0
        enc = "SEED(128)";
1821
0
        break;
1822
0
    case SSL_eGOST2814789CNT:
1823
0
    case SSL_eGOST2814789CNT12:
1824
0
        enc = "GOST89(256)";
1825
0
        break;
1826
0
    case SSL_MAGMA:
1827
0
        enc = "MAGMA";
1828
0
        break;
1829
0
    case SSL_KUZNYECHIK:
1830
0
        enc = "KUZNYECHIK";
1831
0
        break;
1832
0
    case SSL_CHACHA20POLY1305:
1833
0
        enc = "CHACHA20/POLY1305(256)";
1834
0
        break;
1835
0
    default:
1836
0
        enc = "unknown";
1837
0
        break;
1838
0
    }
1839
1840
0
    switch (alg_mac) {
1841
0
    case SSL_MD5:
1842
0
        mac = "MD5";
1843
0
        break;
1844
0
    case SSL_SHA1:
1845
0
        mac = "SHA1";
1846
0
        break;
1847
0
    case SSL_SHA256:
1848
0
        mac = "SHA256";
1849
0
        break;
1850
0
    case SSL_SHA384:
1851
0
        mac = "SHA384";
1852
0
        break;
1853
0
    case SSL_AEAD:
1854
0
        mac = "AEAD";
1855
0
        break;
1856
0
    case SSL_GOST89MAC:
1857
0
    case SSL_GOST89MAC12:
1858
0
        mac = "GOST89";
1859
0
        break;
1860
0
    case SSL_GOST94:
1861
0
        mac = "GOST94";
1862
0
        break;
1863
0
    case SSL_GOST12_256:
1864
0
    case SSL_GOST12_512:
1865
0
        mac = "GOST2012";
1866
0
        break;
1867
0
    default:
1868
0
        mac = "unknown";
1869
0
        break;
1870
0
    }
1871
1872
0
    BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1873
1874
0
    return buf;
1875
0
}
1876
1877
const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1878
0
{
1879
0
    if (c == NULL)
1880
0
        return "(NONE)";
1881
1882
    /*
1883
     * Backwards-compatibility crutch.  In almost all contexts we report TLS
1884
     * 1.0 as "TLSv1", but for ciphers we report "TLSv1.0".
1885
     */
1886
0
    if (c->min_tls == TLS1_VERSION)
1887
0
        return "TLSv1.0";
1888
0
    return ssl_protocol_to_string(c->min_tls);
1889
0
}
1890
1891
/* return the actual cipher being used */
1892
const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1893
0
{
1894
0
    if (c != NULL)
1895
0
        return c->name;
1896
0
    return "(NONE)";
1897
0
}
1898
1899
/* return the actual cipher being used in RFC standard name */
1900
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1901
0
{
1902
0
    if (c != NULL)
1903
0
        return c->stdname;
1904
0
    return "(NONE)";
1905
0
}
1906
1907
/* return the OpenSSL name based on given RFC standard name */
1908
const char *OPENSSL_cipher_name(const char *stdname)
1909
0
{
1910
0
    const SSL_CIPHER *c;
1911
1912
0
    if (stdname == NULL)
1913
0
        return "(NONE)";
1914
0
    c = ssl3_get_cipher_by_std_name(stdname);
1915
0
    return SSL_CIPHER_get_name(c);
1916
0
}
1917
1918
/* number of bits for symmetric cipher */
1919
int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1920
0
{
1921
0
    int ret = 0;
1922
1923
0
    if (c != NULL) {
1924
0
        if (alg_bits != NULL)
1925
0
            *alg_bits = (int)c->alg_bits;
1926
0
        ret = (int)c->strength_bits;
1927
0
    }
1928
0
    return ret;
1929
0
}
1930
1931
uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c)
1932
492k
{
1933
492k
    return c->id;
1934
492k
}
1935
1936
uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *c)
1937
0
{
1938
0
    return c->id & 0xFFFF;
1939
0
}
1940
1941
SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1942
0
{
1943
0
    SSL_COMP *ctmp;
1944
0
    SSL_COMP srch_key;
1945
0
    int i;
1946
1947
0
    if ((n == 0) || (sk == NULL))
1948
0
        return NULL;
1949
0
    srch_key.id = n;
1950
0
    i = sk_SSL_COMP_find(sk, &srch_key);
1951
0
    if (i >= 0)
1952
0
        ctmp = sk_SSL_COMP_value(sk, i);
1953
0
    else
1954
0
        ctmp = NULL;
1955
1956
0
    return ctmp;
1957
0
}
1958
1959
#ifdef OPENSSL_NO_COMP
1960
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1961
{
1962
    return NULL;
1963
}
1964
1965
STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1966
        *meths)
1967
{
1968
    return meths;
1969
}
1970
1971
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1972
{
1973
    return 1;
1974
}
1975
1976
#else
1977
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1978
103k
{
1979
103k
    STACK_OF(SSL_COMP) **rv;
1980
1981
103k
    rv = (STACK_OF(SSL_COMP) **)OSSL_LIB_CTX_get_data(NULL,
1982
103k
        OSSL_LIB_CTX_COMP_METHODS);
1983
103k
    if (rv != NULL)
1984
103k
        return *rv;
1985
0
    else
1986
0
        return NULL;
1987
103k
}
1988
1989
STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1990
        *meths)
1991
0
{
1992
0
    STACK_OF(SSL_COMP) **comp_methods;
1993
0
    STACK_OF(SSL_COMP) *old_meths;
1994
1995
0
    comp_methods = (STACK_OF(SSL_COMP) **)OSSL_LIB_CTX_get_data(NULL,
1996
0
        OSSL_LIB_CTX_COMP_METHODS);
1997
0
    if (comp_methods == NULL) {
1998
0
        old_meths = meths;
1999
0
    } else {
2000
0
        old_meths = *comp_methods;
2001
0
        *comp_methods = meths;
2002
0
    }
2003
2004
0
    return old_meths;
2005
0
}
2006
2007
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
2008
0
{
2009
0
    STACK_OF(SSL_COMP) *comp_methods;
2010
0
    SSL_COMP *comp;
2011
2012
0
    comp_methods = SSL_COMP_get_compression_methods();
2013
2014
0
    if (comp_methods == NULL)
2015
0
        return 1;
2016
2017
0
    if (cm == NULL || COMP_get_type(cm) == NID_undef)
2018
0
        return 1;
2019
2020
    /*-
2021
     * According to draft-ietf-tls-compression-04.txt, the
2022
     * compression number ranges should be the following:
2023
     *
2024
     *   0 to  63:  methods defined by the IETF
2025
     *  64 to 192:  external party methods assigned by IANA
2026
     * 193 to 255:  reserved for private use
2027
     */
2028
0
    if (id < 193 || id > 255) {
2029
0
        ERR_raise(ERR_LIB_SSL, SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
2030
0
        return 1;
2031
0
    }
2032
2033
0
    comp = OPENSSL_malloc(sizeof(*comp));
2034
0
    if (comp == NULL)
2035
0
        return 1;
2036
2037
0
    comp->id = id;
2038
0
    if (sk_SSL_COMP_find(comp_methods, comp) >= 0) {
2039
0
        OPENSSL_free(comp);
2040
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DUPLICATE_COMPRESSION_ID);
2041
0
        return 1;
2042
0
    }
2043
0
    if (!sk_SSL_COMP_push(comp_methods, comp)) {
2044
0
        OPENSSL_free(comp);
2045
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
2046
0
        return 1;
2047
0
    }
2048
2049
0
    return 0;
2050
0
}
2051
#endif
2052
2053
const char *SSL_COMP_get_name(const COMP_METHOD *comp)
2054
0
{
2055
0
#ifndef OPENSSL_NO_COMP
2056
0
    return comp ? COMP_get_name(comp) : NULL;
2057
#else
2058
    return NULL;
2059
#endif
2060
0
}
2061
2062
const char *SSL_COMP_get0_name(const SSL_COMP *comp)
2063
0
{
2064
0
#ifndef OPENSSL_NO_COMP
2065
0
    return comp->name;
2066
#else
2067
    return NULL;
2068
#endif
2069
0
}
2070
2071
int SSL_COMP_get_id(const SSL_COMP *comp)
2072
0
{
2073
0
#ifndef OPENSSL_NO_COMP
2074
0
    return comp->id;
2075
#else
2076
    return -1;
2077
#endif
2078
0
}
2079
2080
const SSL_CIPHER *ssl_get_cipher_by_char(SSL_CONNECTION *s,
2081
    const unsigned char *ptr,
2082
    int all)
2083
726k
{
2084
726k
    const SSL_CIPHER *c = SSL_CONNECTION_GET_SSL(s)->method->get_cipher_by_char(ptr);
2085
2086
726k
    if (c == NULL || (!all && c->valid == 0))
2087
378k
        return NULL;
2088
347k
    return c;
2089
726k
}
2090
2091
const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
2092
0
{
2093
0
    return ssl->method->get_cipher_by_char(ptr);
2094
0
}
2095
2096
int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
2097
0
{
2098
0
    int i;
2099
0
    if (c == NULL)
2100
0
        return NID_undef;
2101
0
    i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
2102
0
    if (i == -1)
2103
0
        return NID_undef;
2104
0
    return ssl_cipher_table_cipher[i].nid;
2105
0
}
2106
2107
int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
2108
0
{
2109
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
2110
2111
0
    if (i == -1)
2112
0
        return NID_undef;
2113
0
    return ssl_cipher_table_mac[i].nid;
2114
0
}
2115
2116
int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
2117
0
{
2118
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_kx, c->algorithm_mkey);
2119
2120
0
    if (i == -1)
2121
0
        return NID_undef;
2122
0
    return ssl_cipher_table_kx[i].nid;
2123
0
}
2124
2125
int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
2126
0
{
2127
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_auth, c->algorithm_auth);
2128
2129
0
    if (i == -1)
2130
0
        return NID_undef;
2131
0
    return ssl_cipher_table_auth[i].nid;
2132
0
}
2133
2134
int ssl_get_md_idx(int md_nid)
2135
272k
{
2136
272k
    int i;
2137
2138
4.08M
    for (i = 0; i < SSL_MD_NUM_IDX; i++) {
2139
3.80M
        if (md_nid == ssl_cipher_table_mac[i].nid)
2140
0
            return i;
2141
3.80M
    }
2142
272k
    return -1;
2143
272k
}
2144
2145
const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c)
2146
0
{
2147
0
    int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK;
2148
2149
0
    if (idx < 0 || idx >= SSL_MD_NUM_IDX)
2150
0
        return NULL;
2151
0
    return EVP_get_digestbynid(ssl_cipher_table_mac[idx].nid);
2152
0
}
2153
2154
int SSL_CIPHER_is_aead(const SSL_CIPHER *c)
2155
0
{
2156
0
    return (c->algorithm_mac & SSL_AEAD) ? 1 : 0;
2157
0
}
2158
2159
int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
2160
    size_t *int_overhead, size_t *blocksize,
2161
    size_t *ext_overhead)
2162
0
{
2163
0
    int mac = 0, in = 0, blk = 0, out = 0;
2164
2165
    /* Some hard-coded numbers for the CCM/Poly1305 MAC overhead
2166
     * because there are no handy #defines for those. */
2167
0
    if (c->algorithm_enc & (SSL_AESGCM | SSL_ARIAGCM)) {
2168
0
        out = EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
2169
0
    } else if (c->algorithm_enc & (SSL_AES128CCM | SSL_AES256CCM)) {
2170
0
        out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 16;
2171
0
    } else if (c->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8)) {
2172
0
        out = EVP_CCM_TLS_EXPLICIT_IV_LEN + 8;
2173
0
    } else if (c->algorithm_enc & SSL_CHACHA20POLY1305) {
2174
0
        out = 16;
2175
0
    } else if (c->algorithm_mac & SSL_AEAD) {
2176
        /* We're supposed to have handled all the AEAD modes above */
2177
0
        return 0;
2178
0
    } else {
2179
        /* Non-AEAD modes. Calculate MAC/cipher overhead separately */
2180
0
        int digest_nid = SSL_CIPHER_get_digest_nid(c);
2181
0
        const EVP_MD *e_md = EVP_get_digestbynid(digest_nid);
2182
2183
0
        if (e_md == NULL)
2184
0
            return 0;
2185
2186
0
        mac = EVP_MD_get_size(e_md);
2187
0
        if (mac <= 0)
2188
0
            return 0;
2189
0
        if (c->algorithm_enc != SSL_eNULL) {
2190
0
            int cipher_nid = SSL_CIPHER_get_cipher_nid(c);
2191
0
            const EVP_CIPHER *e_ciph = EVP_get_cipherbynid(cipher_nid);
2192
2193
            /* If it wasn't AEAD or SSL_eNULL, we expect it to be a
2194
               known CBC cipher. */
2195
0
            if (e_ciph == NULL || EVP_CIPHER_get_mode(e_ciph) != EVP_CIPH_CBC_MODE)
2196
0
                return 0;
2197
2198
0
            in = 1; /* padding length byte */
2199
0
            out = EVP_CIPHER_get_iv_length(e_ciph);
2200
0
            if (out < 0)
2201
0
                return 0;
2202
0
            blk = EVP_CIPHER_get_block_size(e_ciph);
2203
0
            if (blk <= 0)
2204
0
                return 0;
2205
0
        }
2206
0
    }
2207
2208
0
    *mac_overhead = (size_t)mac;
2209
0
    *int_overhead = (size_t)in;
2210
0
    *blocksize = (size_t)blk;
2211
0
    *ext_overhead = (size_t)out;
2212
2213
0
    return 1;
2214
0
}
2215
2216
int ssl_cert_is_disabled(SSL_CTX *ctx, size_t idx)
2217
3.69M
{
2218
3.69M
    const SSL_CERT_LOOKUP *cl;
2219
2220
    /* A provider-loaded key type is always enabled */
2221
3.69M
    if (idx >= SSL_PKEY_NUM)
2222
149k
        return 0;
2223
2224
3.54M
    cl = ssl_cert_lookup_by_idx(idx, ctx);
2225
3.54M
    if (cl == NULL || (cl->amask & ctx->disabled_auth_mask) != 0)
2226
0
        return 1;
2227
3.54M
    return 0;
2228
3.54M
}
2229
2230
/*
2231
 * Default list of TLSv1.2 (and earlier) ciphers
2232
 * SSL_DEFAULT_CIPHER_LIST deprecated in 3.0.0
2233
 * Update both macro and function simultaneously
2234
 */
2235
const char *OSSL_default_cipher_list(void)
2236
163k
{
2237
163k
    return "ALL:!COMPLEMENTOFDEFAULT:!eNULL";
2238
163k
}
2239
2240
/*
2241
 * Default list of TLSv1.3 (and later) ciphers
2242
 * TLS_DEFAULT_CIPHERSUITES deprecated in 3.0.0
2243
 * Update both macro and function simultaneously
2244
 */
2245
const char *OSSL_default_ciphersuites(void)
2246
163k
{
2247
163k
    return "TLS_AES_256_GCM_SHA384:"
2248
163k
           "TLS_CHACHA20_POLY1305_SHA256:"
2249
163k
           "TLS_AES_128_GCM_SHA256";
2250
163k
}