Coverage Report

Created: 2026-07-23 06:28

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