Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/ssl/ssl_ciph.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include <stdio.h>
13
#include <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
94.3k
{
111
94.3k
    size_t i;
112
1.07M
    for (i = 0; i < table_cnt; i++, table++) {
113
1.04M
        if (table->mask == mask)
114
63.3k
            return (int)i;
115
1.04M
    }
116
30.9k
    return -1;
117
94.3k
}
118
119
#define ssl_cipher_info_lookup(table, x) \
120
75.3k
    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
240M
#define CIPHER_ADD      1
139
11.3M
#define CIPHER_KILL     2
140
112M
#define CIPHER_DEL      3
141
146M
#define CIPHER_ORD      4
142
1.42M
#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
66.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,
289
                                         ameth) > 0)
290
        return pkey_id;
291
    return 0;
292
}
293
294
#else
295
296
static int get_optional_pkey_id(const char *pkey_name)
297
1.02M
{
298
1.02M
    const EVP_PKEY_ASN1_METHOD *ameth;
299
1.02M
    ENGINE *tmpeng = NULL;
300
1.02M
    int pkey_id = 0;
301
1.02M
    ameth = EVP_PKEY_asn1_find_str(&tmpeng, pkey_name, -1);
302
1.02M
    if (ameth) {
303
0
        if (EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
304
0
                                    ameth) <= 0)
305
0
            pkey_id = 0;
306
0
    }
307
1.02M
    tls_engine_finish(tmpeng);
308
1.02M
    return pkey_id;
309
1.02M
}
310
311
#endif
312
313
int ssl_load_ciphers(SSL_CTX *ctx)
314
86.0k
{
315
86.0k
    size_t i;
316
86.0k
    const ssl_cipher_table *t;
317
86.0k
    EVP_KEYEXCH *kex = NULL;
318
86.0k
    EVP_SIGNATURE *sig = NULL;
319
320
86.0k
    ctx->disabled_enc_mask = 0;
321
2.15M
    for (i = 0, t = ssl_cipher_table_cipher; i < SSL_ENC_NUM_IDX; i++, t++) {
322
2.06M
        if (t->nid != NID_undef) {
323
1.97M
            const EVP_CIPHER *cipher
324
1.97M
                = ssl_evp_cipher_fetch(ctx->libctx, t->nid, ctx->propq);
325
326
1.97M
            ctx->ssl_cipher_methods[i] = cipher;
327
1.97M
            if (cipher == NULL)
328
774k
                ctx->disabled_enc_mask |= t->mask;
329
1.97M
        }
330
2.06M
    }
331
86.0k
    ctx->disabled_mac_mask = 0;
332
1.29M
    for (i = 0, t = ssl_cipher_table_mac; i < SSL_MD_NUM_IDX; i++, t++) {
333
1.20M
        const EVP_MD *md
334
1.20M
            = ssl_evp_md_fetch(ctx->libctx, t->nid, ctx->propq);
335
336
1.20M
        ctx->ssl_digest_methods[i] = md;
337
1.20M
        if (md == NULL) {
338
602k
            ctx->disabled_mac_mask |= t->mask;
339
602k
        } else {
340
602k
            int tmpsize = EVP_MD_get_size(md);
341
342
602k
            if (!ossl_assert(tmpsize > 0))
343
0
                return 0;
344
602k
            ctx->ssl_mac_secret_size[i] = tmpsize;
345
602k
        }
346
1.20M
    }
347
348
86.0k
    ctx->disabled_mkey_mask = 0;
349
86.0k
    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
86.0k
    ERR_set_mark();
356
86.0k
    sig = EVP_SIGNATURE_fetch(ctx->libctx, "DSA", ctx->propq);
357
86.0k
    if (sig == NULL)
358
0
        ctx->disabled_auth_mask |= SSL_aDSS;
359
86.0k
    else
360
86.0k
        EVP_SIGNATURE_free(sig);
361
86.0k
    kex = EVP_KEYEXCH_fetch(ctx->libctx, "DH", ctx->propq);
362
86.0k
    if (kex == NULL)
363
0
        ctx->disabled_mkey_mask |= SSL_kDHE | SSL_kDHEPSK;
364
86.0k
    else
365
86.0k
        EVP_KEYEXCH_free(kex);
366
86.0k
    kex = EVP_KEYEXCH_fetch(ctx->libctx, "ECDH", ctx->propq);
367
86.0k
    if (kex == NULL)
368
0
        ctx->disabled_mkey_mask |= SSL_kECDHE | SSL_kECDHEPSK;
369
86.0k
    else
370
86.0k
        EVP_KEYEXCH_free(kex);
371
86.0k
    sig = EVP_SIGNATURE_fetch(ctx->libctx, "ECDSA", ctx->propq);
372
86.0k
    if (sig == NULL)
373
0
        ctx->disabled_auth_mask |= SSL_aECDSA;
374
86.0k
    else
375
86.0k
        EVP_SIGNATURE_free(sig);
376
86.0k
    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
86.0k
    memcpy(ctx->ssl_mac_pkey_id, default_mac_pkey_id,
391
86.0k
           sizeof(ctx->ssl_mac_pkey_id));
392
393
86.0k
    ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX] =
394
86.0k
        get_optional_pkey_id(SN_id_Gost28147_89_MAC);
395
86.0k
    if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX])
396
0
        ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
397
86.0k
    else
398
86.0k
        ctx->disabled_mac_mask |= SSL_GOST89MAC;
399
400
86.0k
    ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX] =
401
86.0k
        get_optional_pkey_id(SN_gost_mac_12);
402
86.0k
    if (ctx->ssl_mac_pkey_id[SSL_MD_GOST89MAC12_IDX])
403
0
        ctx->ssl_mac_secret_size[SSL_MD_GOST89MAC12_IDX] = 32;
404
86.0k
    else
405
86.0k
        ctx->disabled_mac_mask |= SSL_GOST89MAC12;
406
407
86.0k
    ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX] =
408
86.0k
        get_optional_pkey_id(SN_magma_mac);
409
86.0k
    if (ctx->ssl_mac_pkey_id[SSL_MD_MAGMAOMAC_IDX])
410
0
        ctx->ssl_mac_secret_size[SSL_MD_MAGMAOMAC_IDX] = 32;
411
86.0k
    else
412
86.0k
        ctx->disabled_mac_mask |= SSL_MAGMAOMAC;
413
414
86.0k
    ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX] =
415
86.0k
        get_optional_pkey_id(SN_kuznyechik_mac);
416
86.0k
    if (ctx->ssl_mac_pkey_id[SSL_MD_KUZNYECHIKOMAC_IDX])
417
0
        ctx->ssl_mac_secret_size[SSL_MD_KUZNYECHIKOMAC_IDX] = 32;
418
86.0k
    else
419
86.0k
        ctx->disabled_mac_mask |= SSL_KUZNYECHIKOMAC;
420
421
86.0k
    if (!get_optional_pkey_id(SN_id_GostR3410_2001))
422
86.0k
        ctx->disabled_auth_mask |= SSL_aGOST01 | SSL_aGOST12;
423
86.0k
    if (!get_optional_pkey_id(SN_id_GostR3410_2012_256))
424
86.0k
        ctx->disabled_auth_mask |= SSL_aGOST12;
425
86.0k
    if (!get_optional_pkey_id(SN_id_GostR3410_2012_512))
426
86.0k
        ctx->disabled_auth_mask |= SSL_aGOST12;
427
    /*
428
     * Disable GOST key exchange if no GOST signature algs are available *
429
     */
430
86.0k
    if ((ctx->disabled_auth_mask & (SSL_aGOST01 | SSL_aGOST12)) ==
431
86.0k
        (SSL_aGOST01 | SSL_aGOST12))
432
86.0k
        ctx->disabled_mkey_mask |= SSL_kGOST;
433
434
86.0k
    if ((ctx->disabled_auth_mask & SSL_aGOST12) ==  SSL_aGOST12)
435
86.0k
        ctx->disabled_mkey_mask |= SSL_kGOST18;
436
437
86.0k
    return 1;
438
86.0k
}
439
440
int ssl_cipher_get_evp_cipher(SSL_CTX *ctx, const SSL_CIPHER *sslc,
441
                              const EVP_CIPHER **enc)
442
47.1k
{
443
47.1k
    int i = ssl_cipher_info_lookup(ssl_cipher_table_cipher,
444
47.1k
                                   sslc->algorithm_enc);
445
446
47.1k
    if (i == -1) {
447
0
        *enc = NULL;
448
47.1k
    } else {
449
47.1k
        if (i == SSL_ENC_NULL_IDX) {
450
            /*
451
             * We assume we don't care about this coming from an ENGINE so
452
             * just do a normal EVP_CIPHER_fetch instead of
453
             * ssl_evp_cipher_fetch()
454
             */
455
6.38k
            *enc = EVP_CIPHER_fetch(ctx->libctx, "NULL", ctx->propq);
456
6.38k
            if (*enc == NULL)
457
0
                return 0;
458
40.7k
        } else {
459
40.7k
            const EVP_CIPHER *cipher = ctx->ssl_cipher_methods[i];
460
461
40.7k
            if (cipher == NULL
462
40.7k
                    || !ssl_evp_cipher_up_ref(cipher))
463
0
                return 0;
464
40.7k
            *enc = ctx->ssl_cipher_methods[i];
465
40.7k
        }
466
47.1k
    }
467
47.1k
    return 1;
468
47.1k
}
469
470
int ssl_cipher_get_evp_md_mac(SSL_CTX *ctx, const SSL_CIPHER *sslc,
471
                              const EVP_MD **md,
472
                              int *mac_pkey_type, size_t *mac_secret_size)
473
28.1k
{
474
28.1k
    int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, sslc->algorithm_mac);
475
476
28.1k
    if (i == -1) {
477
18.4k
        *md = NULL;
478
18.4k
        if (mac_pkey_type != NULL)
479
18.4k
            *mac_pkey_type = NID_undef;
480
18.4k
        if (mac_secret_size != NULL)
481
18.4k
            *mac_secret_size = 0;
482
18.4k
    } else {
483
9.71k
        const EVP_MD *digest = ctx->ssl_digest_methods[i];
484
485
9.71k
        if (digest == NULL || !ssl_evp_md_up_ref(digest)) 
486
0
            return 0;
487
488
9.71k
        *md = digest;
489
9.71k
        if (mac_pkey_type != NULL)
490
8.86k
            *mac_pkey_type = ctx->ssl_mac_pkey_id[i];
491
9.71k
        if (mac_secret_size != NULL)
492
8.86k
            *mac_secret_size = ctx->ssl_mac_secret_size[i];
493
9.71k
    }
494
28.1k
    return 1;
495
28.1k
}
496
497
int ssl_cipher_get_evp(SSL_CTX *ctx, const SSL_SESSION *s,
498
                       const EVP_CIPHER **enc, const EVP_MD **md,
499
                       int *mac_pkey_type, size_t *mac_secret_size,
500
                       SSL_COMP **comp, int use_etm)
501
28.1k
{
502
28.1k
    int i;
503
28.1k
    const SSL_CIPHER *c;
504
505
28.1k
    c = s->cipher;
506
28.1k
    if (c == NULL)
507
0
        return 0;
508
28.1k
    if (comp != NULL) {
509
12.6k
        SSL_COMP ctmp;
510
12.6k
        STACK_OF(SSL_COMP) *comp_methods;
511
512
12.6k
        *comp = NULL;
513
12.6k
        ctmp.id = s->compress_meth;
514
12.6k
        comp_methods = SSL_COMP_get_compression_methods();
515
12.6k
        if (comp_methods != NULL) {
516
12.6k
            i = sk_SSL_COMP_find(comp_methods, &ctmp);
517
12.6k
            if (i >= 0)
518
0
                *comp = sk_SSL_COMP_value(comp_methods, i);
519
12.6k
        }
520
        /* If were only interested in comp then return success */
521
12.6k
        if ((enc == NULL) && (md == NULL))
522
3
            return 1;
523
12.6k
    }
524
525
28.1k
    if ((enc == NULL) || (md == NULL))
526
0
        return 0;
527
528
28.1k
    if (!ssl_cipher_get_evp_cipher(ctx, c, enc))
529
0
        return 0;
530
531
28.1k
    if (!ssl_cipher_get_evp_md_mac(ctx, c, md, mac_pkey_type,
532
28.1k
                                   mac_secret_size)) {
533
0
        ssl_evp_cipher_free(*enc);
534
0
        return 0;
535
0
    }
536
537
28.1k
    if ((*enc != NULL)
538
28.1k
        && (*md != NULL
539
28.1k
            || (EVP_CIPHER_get_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER))
540
28.1k
        && (c->algorithm_mac == SSL_AEAD
541
28.1k
            || mac_pkey_type == NULL || *mac_pkey_type != NID_undef)) {
542
28.1k
        const EVP_CIPHER *evp = NULL;
543
544
28.1k
        if (use_etm
545
28.1k
                || s->ssl_version >> 8 != TLS1_VERSION_MAJOR
546
28.1k
                || s->ssl_version < TLS1_VERSION)
547
7.41k
            return 1;
548
549
20.7k
        if (c->algorithm_enc == SSL_RC4
550
20.7k
                && c->algorithm_mac == SSL_MD5)
551
0
            evp = ssl_evp_cipher_fetch(ctx->libctx, NID_rc4_hmac_md5,
552
0
                                       ctx->propq);
553
20.7k
        else if (c->algorithm_enc == SSL_AES128
554
20.7k
                    && c->algorithm_mac == SSL_SHA1)
555
183
            evp = ssl_evp_cipher_fetch(ctx->libctx,
556
183
                                       NID_aes_128_cbc_hmac_sha1,
557
183
                                       ctx->propq);
558
20.5k
        else if (c->algorithm_enc == SSL_AES256
559
20.5k
                    && c->algorithm_mac == SSL_SHA1)
560
488
             evp = ssl_evp_cipher_fetch(ctx->libctx,
561
488
                                        NID_aes_256_cbc_hmac_sha1,
562
488
                                        ctx->propq);
563
20.0k
        else if (c->algorithm_enc == SSL_AES128
564
20.0k
                    && c->algorithm_mac == SSL_SHA256)
565
869
            evp = ssl_evp_cipher_fetch(ctx->libctx,
566
869
                                       NID_aes_128_cbc_hmac_sha256,
567
869
                                       ctx->propq);
568
19.2k
        else if (c->algorithm_enc == SSL_AES256
569
19.2k
                    && c->algorithm_mac == SSL_SHA256)
570
109
            evp = ssl_evp_cipher_fetch(ctx->libctx,
571
109
                                       NID_aes_256_cbc_hmac_sha256,
572
109
                                       ctx->propq);
573
574
20.7k
        if (evp != NULL) {
575
1.64k
            ssl_evp_cipher_free(*enc);
576
1.64k
            ssl_evp_md_free(*md);
577
1.64k
            *enc = evp;
578
1.64k
            *md = NULL;
579
1.64k
        }
580
20.7k
        return 1;
581
28.1k
    }
582
583
0
    return 0;
584
28.1k
}
585
586
const EVP_MD *ssl_md(SSL_CTX *ctx, int idx)
587
3.78M
{
588
3.78M
    idx &= SSL_HANDSHAKE_MAC_MASK;
589
3.78M
    if (idx < 0 || idx >= SSL_MD_NUM_IDX)
590
0
        return NULL;
591
3.78M
    return ctx->ssl_digest_methods[idx];
592
3.78M
}
593
594
const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s)
595
369k
{
596
369k
    return ssl_md(SSL_CONNECTION_GET_CTX(s), ssl_get_algorithm2(s));
597
369k
}
598
599
const EVP_MD *ssl_prf_md(SSL_CONNECTION *s)
600
52.8k
{
601
52.8k
    return ssl_md(SSL_CONNECTION_GET_CTX(s),
602
52.8k
                  ssl_get_algorithm2(s) >> TLS1_PRF_DGST_SHIFT);
603
52.8k
}
604
605
606
#define ITEM_SEP(a) \
607
1.11M
        (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
608
609
static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
610
                           CIPHER_ORDER **tail)
611
115M
{
612
115M
    if (curr == *tail)
613
196k
        return;
614
115M
    if (curr == *head)
615
38.6M
        *head = curr->next;
616
115M
    if (curr->prev != NULL)
617
76.7M
        curr->prev->next = curr->next;
618
115M
    if (curr->next != NULL)
619
115M
        curr->next->prev = curr->prev;
620
115M
    (*tail)->next = curr;
621
115M
    curr->prev = *tail;
622
115M
    curr->next = NULL;
623
115M
    *tail = curr;
624
115M
}
625
626
static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
627
                           CIPHER_ORDER **tail)
628
89.6M
{
629
89.6M
    if (curr == *head)
630
0
        return;
631
89.6M
    if (curr == *tail)
632
39.9M
        *tail = curr->prev;
633
89.6M
    if (curr->next != NULL)
634
49.6M
        curr->next->prev = curr->prev;
635
89.6M
    if (curr->prev != NULL)
636
89.6M
        curr->prev->next = curr->next;
637
89.6M
    (*head)->prev = curr;
638
89.6M
    curr->next = *head;
639
89.6M
    curr->prev = NULL;
640
89.6M
    *head = curr;
641
89.6M
}
642
643
static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
644
                                       int num_of_ciphers,
645
                                       uint32_t disabled_mkey,
646
                                       uint32_t disabled_auth,
647
                                       uint32_t disabled_enc,
648
                                       uint32_t disabled_mac,
649
                                       CIPHER_ORDER *co_list,
650
                                       CIPHER_ORDER **head_p,
651
                                       CIPHER_ORDER **tail_p)
652
245k
{
653
245k
    int i, co_list_num;
654
245k
    const SSL_CIPHER *c;
655
656
    /*
657
     * We have num_of_ciphers descriptions compiled in, depending on the
658
     * method selected (SSLv3, TLSv1 etc).
659
     * These will later be sorted in a linked list with at most num
660
     * entries.
661
     */
662
663
    /* Get the initial list of ciphers */
664
245k
    co_list_num = 0;            /* actual count of ciphers */
665
37.8M
    for (i = 0; i < num_of_ciphers; i++) {
666
37.6M
        c = ssl_method->get_cipher(i);
667
        /* drop those that use any of that is not available */
668
37.6M
        if (c == NULL || !c->valid)
669
0
            continue;
670
37.6M
        if ((c->algorithm_mkey & disabled_mkey) ||
671
37.6M
            (c->algorithm_auth & disabled_auth) ||
672
37.6M
            (c->algorithm_enc & disabled_enc) ||
673
37.6M
            (c->algorithm_mac & disabled_mac))
674
4.33M
            continue;
675
33.2M
        if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) == 0) &&
676
33.2M
            c->min_tls == 0)
677
0
            continue;
678
33.2M
        if (((ssl_method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS) != 0) &&
679
33.2M
            c->min_dtls == 0)
680
0
            continue;
681
682
33.2M
        co_list[co_list_num].cipher = c;
683
33.2M
        co_list[co_list_num].next = NULL;
684
33.2M
        co_list[co_list_num].prev = NULL;
685
33.2M
        co_list[co_list_num].active = 0;
686
33.2M
        co_list_num++;
687
33.2M
    }
688
689
    /*
690
     * Prepare linked list from list entries
691
     */
692
245k
    if (co_list_num > 0) {
693
196k
        co_list[0].prev = NULL;
694
695
196k
        if (co_list_num > 1) {
696
196k
            co_list[0].next = &co_list[1];
697
698
33.0M
            for (i = 1; i < co_list_num - 1; i++) {
699
32.8M
                co_list[i].prev = &co_list[i - 1];
700
32.8M
                co_list[i].next = &co_list[i + 1];
701
32.8M
            }
702
703
196k
            co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
704
196k
        }
705
706
196k
        co_list[co_list_num - 1].next = NULL;
707
708
196k
        *head_p = &co_list[0];
709
196k
        *tail_p = &co_list[co_list_num - 1];
710
196k
    }
711
245k
}
712
713
static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
714
                                       int num_of_group_aliases,
715
                                       uint32_t disabled_mkey,
716
                                       uint32_t disabled_auth,
717
                                       uint32_t disabled_enc,
718
                                       uint32_t disabled_mac,
719
                                       CIPHER_ORDER *head)
720
245k
{
721
245k
    CIPHER_ORDER *ciph_curr;
722
245k
    const SSL_CIPHER **ca_curr;
723
245k
    int i;
724
245k
    uint32_t mask_mkey = ~disabled_mkey;
725
245k
    uint32_t mask_auth = ~disabled_auth;
726
245k
    uint32_t mask_enc = ~disabled_enc;
727
245k
    uint32_t mask_mac = ~disabled_mac;
728
729
    /*
730
     * First, add the real ciphers as already collected
731
     */
732
245k
    ciph_curr = head;
733
245k
    ca_curr = ca_list;
734
33.5M
    while (ciph_curr != NULL) {
735
33.2M
        *ca_curr = ciph_curr->cipher;
736
33.2M
        ca_curr++;
737
33.2M
        ciph_curr = ciph_curr->next;
738
33.2M
    }
739
740
    /*
741
     * Now we add the available ones from the cipher_aliases[] table.
742
     * They represent either one or more algorithms, some of which
743
     * in any affected category must be supported (set in enabled_mask),
744
     * or represent a cipher strength value (will be added in any case because algorithms=0).
745
     */
746
19.6M
    for (i = 0; i < num_of_group_aliases; i++) {
747
19.3M
        uint32_t algorithm_mkey = cipher_aliases[i].algorithm_mkey;
748
19.3M
        uint32_t algorithm_auth = cipher_aliases[i].algorithm_auth;
749
19.3M
        uint32_t algorithm_enc = cipher_aliases[i].algorithm_enc;
750
19.3M
        uint32_t algorithm_mac = cipher_aliases[i].algorithm_mac;
751
752
19.3M
        if (algorithm_mkey)
753
6.13M
            if ((algorithm_mkey & mask_mkey) == 0)
754
490k
                continue;
755
756
18.8M
        if (algorithm_auth)
757
4.90M
            if ((algorithm_auth & mask_auth) == 0)
758
735k
                continue;
759
760
18.1M
        if (algorithm_enc)
761
7.11M
            if ((algorithm_enc & mask_enc) == 0)
762
1.47M
                continue;
763
764
16.6M
        if (algorithm_mac)
765
2.45M
            if ((algorithm_mac & mask_mac) == 0)
766
735k
                continue;
767
768
15.9M
        *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
769
15.9M
        ca_curr++;
770
15.9M
    }
771
772
245k
    *ca_curr = NULL;            /* end of list */
773
245k
}
774
775
static void ssl_cipher_apply_rule(uint32_t cipher_id, uint32_t alg_mkey,
776
                                  uint32_t alg_auth, uint32_t alg_enc,
777
                                  uint32_t alg_mac, int min_tls,
778
                                  uint32_t algo_strength, int rule,
779
                                  int32_t strength_bits, CIPHER_ORDER **head_p,
780
                                  CIPHER_ORDER **tail_p)
781
5.76M
{
782
5.76M
    CIPHER_ORDER *head, *tail, *curr, *next, *last;
783
5.76M
    const SSL_CIPHER *cp;
784
5.76M
    int reverse = 0;
785
786
5.76M
    OSSL_TRACE_BEGIN(TLS_CIPHER) {
787
0
        BIO_printf(trc_out,
788
0
                   "Applying rule %d with %08x/%08x/%08x/%08x/%08x %08x (%d)\n",
789
0
                   rule, (unsigned int)alg_mkey, (unsigned int)alg_auth,
790
0
                   (unsigned int)alg_enc, (unsigned int)alg_mac, min_tls,
791
0
                   (unsigned int)algo_strength, (int)strength_bits);
792
0
    }
793
794
5.76M
    if (rule == CIPHER_DEL || rule == CIPHER_BUMP)
795
1.47M
        reverse = 1;            /* needed to maintain sorting between currently
796
                                 * deleted ciphers */
797
798
5.76M
    head = *head_p;
799
5.76M
    tail = *tail_p;
800
801
5.76M
    if (reverse) {
802
1.47M
        next = tail;
803
1.47M
        last = head;
804
4.29M
    } else {
805
4.29M
        next = head;
806
4.29M
        last = tail;
807
4.29M
    }
808
809
5.76M
    curr = NULL;
810
808M
    for (;;) {
811
808M
        if (curr == last)
812
5.76M
            break;
813
814
802M
        curr = next;
815
816
802M
        if (curr == NULL)
817
0
            break;
818
819
802M
        next = reverse ? curr->prev : curr->next;
820
821
802M
        cp = curr->cipher;
822
823
        /*
824
         * Selection criteria is either the value of strength_bits
825
         * or the algorithms used.
826
         */
827
802M
        if (strength_bits >= 0) {
828
162M
            if (strength_bits != cp->strength_bits)
829
129M
                continue;
830
639M
        } else {
831
639M
            if (trc_out != NULL) {
832
0
                BIO_printf(trc_out,
833
0
                           "\nName: %s:"
834
0
                           "\nAlgo = %08x/%08x/%08x/%08x/%08x Algo_strength = %08x\n",
835
0
                           cp->name,
836
0
                           (unsigned int)cp->algorithm_mkey,
837
0
                           (unsigned int)cp->algorithm_auth,
838
0
                           (unsigned int)cp->algorithm_enc,
839
0
                           (unsigned int)cp->algorithm_mac,
840
0
                           cp->min_tls,
841
0
                           (unsigned int)cp->algo_strength);
842
0
            }
843
639M
            if (cipher_id != 0 && (cipher_id != cp->id))
844
0
                continue;
845
639M
            if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
846
175M
                continue;
847
464M
            if (alg_auth && !(alg_auth & cp->algorithm_auth))
848
33.6M
                continue;
849
430M
            if (alg_enc && !(alg_enc & cp->algorithm_enc))
850
138M
                continue;
851
291M
            if (alg_mac && !(alg_mac & cp->algorithm_mac))
852
63.6M
                continue;
853
228M
            if (min_tls && (min_tls != cp->min_tls))
854
16.3M
                continue;
855
212M
            if ((algo_strength & SSL_STRONG_MASK)
856
212M
                && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
857
0
                continue;
858
212M
            if ((algo_strength & SSL_DEFAULT_MASK)
859
212M
                && !(algo_strength & SSL_DEFAULT_MASK & cp->algo_strength))
860
7.38M
                continue;
861
212M
        }
862
863
237M
        if (trc_out != NULL)
864
0
            BIO_printf(trc_out, "Action = %d\n", rule);
865
866
        /* add the cipher if it has not been added yet. */
867
237M
        if (rule == CIPHER_ADD) {
868
            /* reverse == 0 */
869
93.1M
            if (!curr->active) {
870
71.4M
                ll_append_tail(&head, curr, &tail);
871
71.4M
                curr->active = 1;
872
71.4M
            }
873
93.1M
        }
874
        /* Move the added cipher to this location */
875
144M
        else if (rule == CIPHER_ORD) {
876
            /* reverse == 0 */
877
44.1M
            if (curr->active) {
878
44.1M
                ll_append_tail(&head, curr, &tail);
879
44.1M
            }
880
100M
        } else if (rule == CIPHER_DEL) {
881
            /* reverse == 1 */
882
39.9M
            if (curr->active) {
883
                /*
884
                 * most recently deleted ciphersuites get best positions for
885
                 * any future CIPHER_ADD (note that the CIPHER_DEL loop works
886
                 * in reverse to maintain the order)
887
                 */
888
39.9M
                ll_append_head(&head, curr, &tail);
889
39.9M
                curr->active = 0;
890
39.9M
            }
891
60.6M
        } else if (rule == CIPHER_BUMP) {
892
49.6M
            if (curr->active)
893
49.6M
                ll_append_head(&head, curr, &tail);
894
49.6M
        } else if (rule == CIPHER_KILL) {
895
            /* reverse == 0 */
896
11.0M
            if (head == curr)
897
1.77M
                head = curr->next;
898
9.25M
            else
899
9.25M
                curr->prev->next = curr->next;
900
11.0M
            if (tail == curr)
901
98.4k
                tail = curr->prev;
902
11.0M
            curr->active = 0;
903
11.0M
            if (curr->next != NULL)
904
10.9M
                curr->next->prev = curr->prev;
905
11.0M
            if (curr->prev != NULL)
906
9.25M
                curr->prev->next = curr->next;
907
11.0M
            curr->next = NULL;
908
11.0M
            curr->prev = NULL;
909
11.0M
        }
910
237M
    }
911
912
5.76M
    *head_p = head;
913
5.76M
    *tail_p = tail;
914
915
5.76M
    OSSL_TRACE_END(TLS_CIPHER);
916
5.76M
}
917
918
static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
919
                                    CIPHER_ORDER **tail_p)
920
245k
{
921
245k
    int32_t max_strength_bits;
922
245k
    int i, *number_uses;
923
245k
    CIPHER_ORDER *curr;
924
925
    /*
926
     * This routine sorts the ciphers with descending strength. The sorting
927
     * must keep the pre-sorted sequence, so we apply the normal sorting
928
     * routine as '+' movement to the end of the list.
929
     */
930
245k
    max_strength_bits = 0;
931
245k
    curr = *head_p;
932
33.5M
    while (curr != NULL) {
933
33.2M
        if (curr->active && (curr->cipher->strength_bits > max_strength_bits))
934
196k
            max_strength_bits = curr->cipher->strength_bits;
935
33.2M
        curr = curr->next;
936
33.2M
    }
937
938
245k
    number_uses = OPENSSL_zalloc(sizeof(int) * (max_strength_bits + 1));
939
245k
    if (number_uses == NULL)
940
0
        return 0;
941
942
    /*
943
     * Now find the strength_bits values actually used
944
     */
945
245k
    curr = *head_p;
946
33.5M
    while (curr != NULL) {
947
33.2M
        if (curr->active)
948
33.2M
            number_uses[curr->cipher->strength_bits]++;
949
33.2M
        curr = curr->next;
950
33.2M
    }
951
    /*
952
     * Go through the list of used strength_bits values in descending
953
     * order.
954
     */
955
50.9M
    for (i = max_strength_bits; i >= 0; i--)
956
50.6M
        if (number_uses[i] > 0)
957
961k
            ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p,
958
961k
                                  tail_p);
959
960
245k
    OPENSSL_free(number_uses);
961
245k
    return 1;
962
245k
}
963
964
static int ssl_cipher_process_rulestr(const char *rule_str,
965
                                      CIPHER_ORDER **head_p,
966
                                      CIPHER_ORDER **tail_p,
967
                                      const SSL_CIPHER **ca_list, CERT *c)
968
222k
{
969
222k
    uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
970
222k
    int min_tls;
971
222k
    const char *l, *buf;
972
222k
    int j, multi, found, rule, retval, ok, buflen;
973
222k
    uint32_t cipher_id = 0;
974
222k
    char ch;
975
976
222k
    retval = 1;
977
222k
    l = rule_str;
978
1.11M
    for (;;) {
979
1.11M
        ch = *l;
980
981
1.11M
        if (ch == '\0')
982
0
            break;              /* done */
983
1.11M
        if (ch == '-') {
984
0
            rule = CIPHER_DEL;
985
0
            l++;
986
1.11M
        } else if (ch == '+') {
987
0
            rule = CIPHER_ORD;
988
0
            l++;
989
1.11M
        } else if (ch == '!') {
990
270k
            rule = CIPHER_KILL;
991
270k
            l++;
992
840k
        } else if (ch == '@') {
993
86.9k
            rule = CIPHER_SPECIAL;
994
86.9k
            l++;
995
753k
        } else {
996
753k
            rule = CIPHER_ADD;
997
753k
        }
998
999
1.11M
        if (ITEM_SEP(ch)) {
1000
444k
            l++;
1001
444k
            continue;
1002
444k
        }
1003
1004
666k
        alg_mkey = 0;
1005
666k
        alg_auth = 0;
1006
666k
        alg_enc = 0;
1007
666k
        alg_mac = 0;
1008
666k
        min_tls = 0;
1009
666k
        algo_strength = 0;
1010
1011
666k
        for (;;) {
1012
666k
            ch = *l;
1013
666k
            buf = l;
1014
666k
            buflen = 0;
1015
666k
#ifndef CHARSET_EBCDIC
1016
5.88M
            while (((ch >= 'A') && (ch <= 'Z')) ||
1017
5.88M
                   ((ch >= '0') && (ch <= '9')) ||
1018
5.88M
                   ((ch >= 'a') && (ch <= 'z')) ||
1019
5.88M
                   (ch == '-') || (ch == '_') || (ch == '.') || (ch == '='))
1020
#else
1021
            while (isalnum((unsigned char)ch) || (ch == '-') || (ch == '_') || (ch == '.')
1022
                   || (ch == '='))
1023
#endif
1024
5.21M
            {
1025
5.21M
                ch = *(++l);
1026
5.21M
                buflen++;
1027
5.21M
            }
1028
1029
666k
            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
666k
            if (rule == CIPHER_SPECIAL) {
1040
86.9k
                found = 0;      /* unused -- avoid compiler warning */
1041
86.9k
                break;          /* special treatment */
1042
86.9k
            }
1043
1044
            /* check for multi-part specification */
1045
579k
            if (ch == '+') {
1046
0
                multi = 1;
1047
0
                l++;
1048
579k
            } else {
1049
579k
                multi = 0;
1050
579k
            }
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
579k
            j = found = 0;
1064
579k
            cipher_id = 0;
1065
81.8M
            while (ca_list[j]) {
1066
81.8M
                if (strncmp(buf, ca_list[j]->name, buflen) == 0
1067
81.8M
                    && (ca_list[j]->name[buflen] == '\0')) {
1068
579k
                    found = 1;
1069
579k
                    break;
1070
81.3M
                } else if (ca_list[j]->stdname != NULL
1071
81.3M
                           && strncmp(buf, ca_list[j]->stdname, buflen) == 0
1072
81.3M
                           && ca_list[j]->stdname[buflen] == '\0') {
1073
0
                    found = 1;
1074
0
                    break;
1075
0
                } else
1076
81.3M
                    j++;
1077
81.8M
            }
1078
1079
579k
            if (!found)
1080
0
                break;          /* ignore this entry */
1081
1082
579k
            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
579k
            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
579k
            if (ca_list[j]->algorithm_enc) {
1107
444k
                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
444k
                } else {
1114
444k
                    alg_enc = ca_list[j]->algorithm_enc;
1115
444k
                }
1116
444k
            }
1117
1118
579k
            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
579k
            if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1131
0
                if (algo_strength & SSL_STRONG_MASK) {
1132
0
                    algo_strength &=
1133
0
                        (ca_list[j]->algo_strength & SSL_STRONG_MASK) |
1134
0
                        ~SSL_STRONG_MASK;
1135
0
                    if (!(algo_strength & SSL_STRONG_MASK)) {
1136
0
                        found = 0;
1137
0
                        break;
1138
0
                    }
1139
0
                } else {
1140
0
                    algo_strength = ca_list[j]->algo_strength & SSL_STRONG_MASK;
1141
0
                }
1142
0
            }
1143
1144
579k
            if (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) {
1145
135k
                if (algo_strength & SSL_DEFAULT_MASK) {
1146
0
                    algo_strength &=
1147
0
                        (ca_list[j]->algo_strength & SSL_DEFAULT_MASK) |
1148
0
                        ~SSL_DEFAULT_MASK;
1149
0
                    if (!(algo_strength & SSL_DEFAULT_MASK)) {
1150
0
                        found = 0;
1151
0
                        break;
1152
0
                    }
1153
135k
                } else {
1154
135k
                    algo_strength |=
1155
135k
                        ca_list[j]->algo_strength & SSL_DEFAULT_MASK;
1156
135k
                }
1157
135k
            }
1158
1159
579k
            if (ca_list[j]->valid) {
1160
                /*
1161
                 * explicit ciphersuite found; its protocol version does not
1162
                 * become part of the search pattern!
1163
                 */
1164
1165
0
                cipher_id = ca_list[j]->id;
1166
579k
            } else {
1167
                /*
1168
                 * not an explicit ciphersuite; only in this case, the
1169
                 * protocol version is considered part of the search pattern
1170
                 */
1171
1172
579k
                if (ca_list[j]->min_tls) {
1173
0
                    if (min_tls != 0 && min_tls != ca_list[j]->min_tls) {
1174
0
                        found = 0;
1175
0
                        break;
1176
0
                    } else {
1177
0
                        min_tls = ca_list[j]->min_tls;
1178
0
                    }
1179
0
                }
1180
579k
            }
1181
1182
579k
            if (!multi)
1183
579k
                break;
1184
579k
        }
1185
1186
        /*
1187
         * Ok, we have the rule, now apply it
1188
         */
1189
666k
        if (rule == CIPHER_SPECIAL) { /* special command */
1190
86.9k
            ok = 0;
1191
86.9k
            if ((buflen == 8) && HAS_PREFIX(buf, "STRENGTH")) {
1192
0
                ok = ssl_cipher_strength_sort(head_p, tail_p);
1193
86.9k
            } else if (buflen == 10 && CHECK_AND_SKIP_PREFIX(buf, "SECLEVEL=")) {
1194
86.9k
                int level = *buf - '0';
1195
86.9k
                if (level < 0 || level > 5) {
1196
0
                    ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
1197
86.9k
                } else {
1198
86.9k
                    c->sec_level = level;
1199
86.9k
                    ok = 1;
1200
86.9k
                }
1201
86.9k
            } else {
1202
0
                ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_COMMAND);
1203
0
            }
1204
86.9k
            if (ok == 0)
1205
0
                retval = 0;
1206
            /*
1207
             * We do not support any "multi" options
1208
             * together with "@", so throw away the
1209
             * rest of the command, if any left, until
1210
             * end or ':' is found.
1211
             */
1212
86.9k
            while ((*l != '\0') && !ITEM_SEP(*l))
1213
0
                l++;
1214
579k
        } else if (found) {
1215
579k
            ssl_cipher_apply_rule(cipher_id,
1216
579k
                                  alg_mkey, alg_auth, alg_enc, alg_mac,
1217
579k
                                  min_tls, algo_strength, rule, -1, head_p,
1218
579k
                                  tail_p);
1219
579k
        } else {
1220
0
            while ((*l != '\0') && !ITEM_SEP(*l))
1221
0
                l++;
1222
0
        }
1223
666k
        if (*l == '\0')
1224
222k
            break;              /* done */
1225
666k
    }
1226
1227
222k
    return retval;
1228
222k
}
1229
1230
static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
1231
                                    const char **prule_str)
1232
245k
{
1233
245k
    unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
1234
245k
    if (HAS_PREFIX(*prule_str, "SUITEB128ONLY")) {
1235
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
1236
245k
    } else if (HAS_PREFIX(*prule_str, "SUITEB128C2")) {
1237
0
        suiteb_comb2 = 1;
1238
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1239
245k
    } else if (HAS_PREFIX(*prule_str, "SUITEB128")) {
1240
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1241
245k
    } else if (HAS_PREFIX(*prule_str, "SUITEB192")) {
1242
0
        suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
1243
0
    }
1244
1245
245k
    if (suiteb_flags) {
1246
0
        c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
1247
0
        c->cert_flags |= suiteb_flags;
1248
245k
    } else {
1249
245k
        suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
1250
245k
    }
1251
1252
245k
    if (!suiteb_flags)
1253
245k
        return 1;
1254
    /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
1255
1256
0
    if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)) {
1257
0
        ERR_raise(ERR_LIB_SSL, SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE);
1258
0
        return 0;
1259
0
    }
1260
1261
0
    switch (suiteb_flags) {
1262
0
    case SSL_CERT_FLAG_SUITEB_128_LOS:
1263
0
        if (suiteb_comb2)
1264
0
            *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1265
0
        else
1266
0
            *prule_str =
1267
0
                "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
1268
0
        break;
1269
0
    case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1270
0
        *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
1271
0
        break;
1272
0
    case SSL_CERT_FLAG_SUITEB_192_LOS:
1273
0
        *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1274
0
        break;
1275
0
    }
1276
0
    return 1;
1277
0
}
1278
1279
static int ciphersuite_cb(const char *elem, int len, void *arg)
1280
440k
{
1281
440k
    STACK_OF(SSL_CIPHER) *ciphersuites = (STACK_OF(SSL_CIPHER) *)arg;
1282
440k
    const SSL_CIPHER *cipher;
1283
    /* Arbitrary sized temp buffer for the cipher name. Should be big enough */
1284
440k
    char name[80];
1285
1286
440k
    if (len > (int)(sizeof(name) - 1))
1287
        /* Anyway return 1 so we can parse rest of the list */
1288
0
        return 1;
1289
1290
440k
    memcpy(name, elem, len);
1291
440k
    name[len] = '\0';
1292
1293
440k
    cipher = ssl3_get_cipher_by_std_name(name);
1294
440k
    if (cipher == NULL)
1295
        /* Ciphersuite not found but return 1 to parse rest of the list */
1296
0
        return 1;
1297
1298
440k
    if (!sk_SSL_CIPHER_push(ciphersuites, cipher)) {
1299
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1300
0
        return 0;
1301
0
    }
1302
1303
440k
    return 1;
1304
440k
}
1305
1306
static __owur int set_ciphersuites(STACK_OF(SSL_CIPHER) **currciphers, const char *str)
1307
146k
{
1308
146k
    STACK_OF(SSL_CIPHER) *newciphers = sk_SSL_CIPHER_new_null();
1309
1310
146k
    if (newciphers == NULL)
1311
0
        return 0;
1312
1313
    /* Parse the list. We explicitly allow an empty list */
1314
146k
    if (*str != '\0'
1315
146k
            && (CONF_parse_list(str, ':', 1, ciphersuite_cb, newciphers) <= 0
1316
146k
                || sk_SSL_CIPHER_num(newciphers) == 0)) {
1317
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
1318
0
        sk_SSL_CIPHER_free(newciphers);
1319
0
        return 0;
1320
0
    }
1321
146k
    sk_SSL_CIPHER_free(*currciphers);
1322
146k
    *currciphers = newciphers;
1323
1324
146k
    return 1;
1325
146k
}
1326
1327
static int update_cipher_list_by_id(STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1328
                                    STACK_OF(SSL_CIPHER) *cipherstack)
1329
245k
{
1330
245k
    STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1331
1332
245k
    if (tmp_cipher_list == NULL) {
1333
0
        return 0;
1334
0
    }
1335
1336
245k
    sk_SSL_CIPHER_free(*cipher_list_by_id);
1337
245k
    *cipher_list_by_id = tmp_cipher_list;
1338
1339
245k
    (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
1340
245k
    sk_SSL_CIPHER_sort(*cipher_list_by_id);
1341
1342
245k
    return 1;
1343
245k
}
1344
1345
static int update_cipher_list(SSL_CTX *ctx,
1346
                              STACK_OF(SSL_CIPHER) **cipher_list,
1347
                              STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1348
                              STACK_OF(SSL_CIPHER) *tls13_ciphersuites)
1349
0
{
1350
0
    int i;
1351
0
    STACK_OF(SSL_CIPHER) *tmp_cipher_list = sk_SSL_CIPHER_dup(*cipher_list);
1352
1353
0
    if (tmp_cipher_list == NULL)
1354
0
        return 0;
1355
1356
    /*
1357
     * Delete any existing TLSv1.3 ciphersuites. These are always first in the
1358
     * list.
1359
     */
1360
0
    while (sk_SSL_CIPHER_num(tmp_cipher_list) > 0
1361
0
           && sk_SSL_CIPHER_value(tmp_cipher_list, 0)->min_tls
1362
0
              == TLS1_3_VERSION)
1363
0
        (void)sk_SSL_CIPHER_delete(tmp_cipher_list, 0);
1364
1365
    /* Insert the new TLSv1.3 ciphersuites */
1366
0
    for (i = sk_SSL_CIPHER_num(tls13_ciphersuites) - 1; i >= 0; i--) {
1367
0
        const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
1368
1369
        /* Don't include any TLSv1.3 ciphersuites that are disabled */
1370
0
        if ((sslc->algorithm_enc & ctx->disabled_enc_mask) == 0
1371
0
                && (ssl_cipher_table_mac[sslc->algorithm2
1372
0
                                         & SSL_HANDSHAKE_MAC_MASK].mask
1373
0
                    & ctx->disabled_mac_mask) == 0) {
1374
0
            sk_SSL_CIPHER_unshift(tmp_cipher_list, sslc);
1375
0
        }
1376
0
    }
1377
1378
0
    if (!update_cipher_list_by_id(cipher_list_by_id, tmp_cipher_list)) {
1379
0
        sk_SSL_CIPHER_free(tmp_cipher_list);
1380
0
        return 0;
1381
0
    }
1382
1383
0
    sk_SSL_CIPHER_free(*cipher_list);
1384
0
    *cipher_list = tmp_cipher_list;
1385
1386
0
    return 1;
1387
0
}
1388
1389
int SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1390
146k
{
1391
146k
    int ret = set_ciphersuites(&(ctx->tls13_ciphersuites), str);
1392
1393
146k
    if (ret && ctx->cipher_list != NULL)
1394
0
        return update_cipher_list(ctx, &ctx->cipher_list, &ctx->cipher_list_by_id,
1395
0
                                  ctx->tls13_ciphersuites);
1396
1397
146k
    return ret;
1398
146k
}
1399
1400
int SSL_set_ciphersuites(SSL *s, const char *str)
1401
0
{
1402
0
    STACK_OF(SSL_CIPHER) *cipher_list;
1403
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1404
0
    int ret;
1405
1406
0
    if (sc == NULL)
1407
0
        return 0;
1408
1409
0
    ret = set_ciphersuites(&(sc->tls13_ciphersuites), str);
1410
1411
0
    if (sc->cipher_list == NULL) {
1412
0
        if ((cipher_list = SSL_get_ciphers(s)) != NULL)
1413
0
            sc->cipher_list = sk_SSL_CIPHER_dup(cipher_list);
1414
0
    }
1415
0
    if (ret && sc->cipher_list != NULL)
1416
0
        return update_cipher_list(s->ctx, &sc->cipher_list,
1417
0
                                  &sc->cipher_list_by_id,
1418
0
                                  sc->tls13_ciphersuites);
1419
1420
0
    return ret;
1421
0
}
1422
1423
STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
1424
                                             STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
1425
                                             STACK_OF(SSL_CIPHER) **cipher_list,
1426
                                             STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1427
                                             const char *rule_str,
1428
                                             CERT *c)
1429
222k
{
1430
222k
    int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases, i;
1431
222k
    uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
1432
222k
    STACK_OF(SSL_CIPHER) *cipherstack;
1433
222k
    const char *rule_p;
1434
222k
    CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1435
222k
    const SSL_CIPHER **ca_list = NULL;
1436
222k
    const SSL_METHOD *ssl_method = ctx->method;
1437
1438
    /*
1439
     * Return with error if nothing to do.
1440
     */
1441
222k
    if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1442
0
        return NULL;
1443
1444
222k
    if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
1445
0
        return NULL;
1446
1447
    /*
1448
     * To reduce the work to do we only want to process the compiled
1449
     * in algorithms, so we first get the mask of disabled ciphers.
1450
     */
1451
1452
222k
    disabled_mkey = ctx->disabled_mkey_mask;
1453
222k
    disabled_auth = ctx->disabled_auth_mask;
1454
222k
    disabled_enc = ctx->disabled_enc_mask;
1455
222k
    disabled_mac = ctx->disabled_mac_mask;
1456
1457
    /*
1458
     * Now we have to collect the available ciphers from the compiled
1459
     * in ciphers. We cannot get more than the number compiled in, so
1460
     * it is used for allocation.
1461
     */
1462
222k
    num_of_ciphers = ssl_method->num_ciphers();
1463
1464
222k
    if (num_of_ciphers > 0) {
1465
173k
        co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
1466
173k
        if (co_list == NULL)
1467
0
            return NULL;          /* Failure */
1468
173k
    }
1469
1470
222k
    ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1471
222k
                               disabled_mkey, disabled_auth, disabled_enc,
1472
222k
                               disabled_mac, co_list, &head, &tail);
1473
1474
    /* Now arrange all ciphers by preference. */
1475
1476
    /*
1477
     * Everything else being equal, prefer ephemeral ECDH over other key
1478
     * exchange mechanisms.
1479
     * For consistency, prefer ECDSA over RSA (though this only matters if the
1480
     * server has both certificates, and is using the DEFAULT, or a client
1481
     * preference).
1482
     */
1483
222k
    ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD,
1484
222k
                          -1, &head, &tail);
1485
222k
    ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head,
1486
222k
                          &tail);
1487
222k
    ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
1488
222k
                          &tail);
1489
1490
    /* Within each strength group, we prefer GCM over CHACHA... */
1491
222k
    ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
1492
222k
                          &head, &tail);
1493
222k
    ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
1494
222k
                          &head, &tail);
1495
1496
    /*
1497
     * ...and generally, our preferred cipher is AES.
1498
     * Note that AEADs will be bumped to take preference after sorting by
1499
     * strength.
1500
     */
1501
222k
    ssl_cipher_apply_rule(0, 0, 0, SSL_AES ^ SSL_AESGCM, 0, 0, 0, CIPHER_ADD,
1502
222k
                          -1, &head, &tail);
1503
1504
    /* Temporarily enable everything else for sorting */
1505
222k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1506
1507
    /* Low priority for MD5 */
1508
222k
    ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head,
1509
222k
                          &tail);
1510
1511
    /*
1512
     * Move anonymous ciphers to the end.  Usually, these will remain
1513
     * disabled. (For applications that allow them, they aren't too bad, but
1514
     * we prefer authenticated ciphers.)
1515
     */
1516
222k
    ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1517
222k
                          &tail);
1518
1519
222k
    ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1520
222k
                          &tail);
1521
222k
    ssl_cipher_apply_rule(0, SSL_kPSK, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head,
1522
222k
                          &tail);
1523
1524
    /* RC4 is sort-of broken -- move to the end */
1525
222k
    ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head,
1526
222k
                          &tail);
1527
1528
    /*
1529
     * Now sort by symmetric encryption strength.  The above ordering remains
1530
     * in force within each class
1531
     */
1532
222k
    if (!ssl_cipher_strength_sort(&head, &tail)) {
1533
0
        OPENSSL_free(co_list);
1534
0
        return NULL;
1535
0
    }
1536
1537
    /*
1538
     * Partially overrule strength sort to prefer TLS 1.2 ciphers/PRFs.
1539
     */
1540
222k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, TLS1_2_VERSION, 0, CIPHER_BUMP, -1,
1541
222k
                          &head, &tail);
1542
1543
    /*
1544
     * Irrespective of strength, enforce the following order:
1545
     * (EC)DHE + AEAD > (EC)DHE > rest of AEAD > rest.
1546
     * Within each group, ciphers remain sorted by strength and previous
1547
     * preference, i.e.,
1548
     * 1) ECDHE > DHE
1549
     * 2) GCM > CHACHA
1550
     * 3) AES > rest
1551
     * 4) TLS 1.2 > legacy
1552
     *
1553
     * Because we now bump ciphers to the top of the list, we proceed in
1554
     * reverse order of preference.
1555
     */
1556
222k
    ssl_cipher_apply_rule(0, 0, 0, 0, SSL_AEAD, 0, 0, CIPHER_BUMP, -1,
1557
222k
                          &head, &tail);
1558
222k
    ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, 0, 0, 0,
1559
222k
                          CIPHER_BUMP, -1, &head, &tail);
1560
222k
    ssl_cipher_apply_rule(0, SSL_kDHE | SSL_kECDHE, 0, 0, SSL_AEAD, 0, 0,
1561
222k
                          CIPHER_BUMP, -1, &head, &tail);
1562
1563
    /* Now disable everything (maintaining the ordering!) */
1564
222k
    ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1565
1566
    /*
1567
     * We also need cipher aliases for selecting based on the rule_str.
1568
     * There might be two types of entries in the rule_str: 1) names
1569
     * of ciphers themselves 2) aliases for groups of ciphers.
1570
     * For 1) we need the available ciphers and for 2) the cipher
1571
     * groups of cipher_aliases added together in one list (otherwise
1572
     * we would be happy with just the cipher_aliases table).
1573
     */
1574
222k
    num_of_group_aliases = OSSL_NELEM(cipher_aliases);
1575
222k
    num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1576
222k
    ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
1577
222k
    if (ca_list == NULL) {
1578
0
        OPENSSL_free(co_list);
1579
0
        return NULL;          /* Failure */
1580
0
    }
1581
222k
    ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1582
222k
                               disabled_mkey, disabled_auth, disabled_enc,
1583
222k
                               disabled_mac, head);
1584
1585
    /*
1586
     * If the rule_string begins with DEFAULT, apply the default rule
1587
     * before using the (possibly available) additional rules.
1588
     */
1589
222k
    ok = 1;
1590
222k
    rule_p = rule_str;
1591
222k
    if (HAS_PREFIX(rule_str, "DEFAULT")) {
1592
0
        ok = ssl_cipher_process_rulestr(OSSL_default_cipher_list(),
1593
0
                                        &head, &tail, ca_list, c);
1594
0
        rule_p += 7;
1595
0
        if (*rule_p == ':')
1596
0
            rule_p++;
1597
0
    }
1598
1599
222k
    if (ok && (rule_p[0] != '\0'))
1600
222k
        ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list, c);
1601
1602
222k
    OPENSSL_free(ca_list);      /* Not needed anymore */
1603
1604
222k
    if (!ok) {                  /* Rule processing failure */
1605
0
        OPENSSL_free(co_list);
1606
0
        return NULL;
1607
0
    }
1608
1609
    /*
1610
     * Allocate new "cipherstack" for the result, return with error
1611
     * if we cannot get one.
1612
     */
1613
222k
    if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1614
0
        OPENSSL_free(co_list);
1615
0
        return NULL;
1616
0
    }
1617
1618
    /* Add TLSv1.3 ciphers first - we always prefer those if possible */
1619
889k
    for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
1620
666k
        const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
1621
1622
        /* Don't include any TLSv1.3 ciphers that are disabled */
1623
666k
        if ((sslc->algorithm_enc & disabled_enc) != 0
1624
666k
                || (ssl_cipher_table_mac[sslc->algorithm2
1625
666k
                                         & SSL_HANDSHAKE_MAC_MASK].mask
1626
666k
                    & ctx->disabled_mac_mask) != 0) {
1627
0
            sk_SSL_CIPHER_delete(tls13_ciphersuites, i);
1628
0
            i--;
1629
0
            continue;
1630
0
        }
1631
1632
666k
        if (!sk_SSL_CIPHER_push(cipherstack, sslc)) {
1633
0
            OPENSSL_free(co_list);
1634
0
            sk_SSL_CIPHER_free(cipherstack);
1635
0
            return NULL;
1636
0
        }
1637
666k
    }
1638
1639
222k
    OSSL_TRACE_BEGIN(TLS_CIPHER) {
1640
0
        BIO_printf(trc_out, "cipher selection:\n");
1641
0
    }
1642
    /*
1643
     * The cipher selection for the list is done. The ciphers are added
1644
     * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1645
     */
1646
19.8M
    for (curr = head; curr != NULL; curr = curr->next) {
1647
19.6M
        if (curr->active) {
1648
19.6M
            if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1649
0
                OPENSSL_free(co_list);
1650
0
                sk_SSL_CIPHER_free(cipherstack);
1651
0
                OSSL_TRACE_CANCEL(TLS_CIPHER);
1652
0
                return NULL;
1653
0
            }
1654
19.6M
            if (trc_out != NULL)
1655
0
                BIO_printf(trc_out, "<%s>\n", curr->cipher->name);
1656
19.6M
        }
1657
19.6M
    }
1658
222k
    OPENSSL_free(co_list);      /* Not needed any longer */
1659
222k
    OSSL_TRACE_END(TLS_CIPHER);
1660
1661
222k
    if (!update_cipher_list_by_id(cipher_list_by_id, cipherstack)) {
1662
0
        sk_SSL_CIPHER_free(cipherstack);
1663
0
        return NULL;
1664
0
    }
1665
222k
    sk_SSL_CIPHER_free(*cipher_list);
1666
222k
    *cipher_list = cipherstack;
1667
1668
222k
    return cipherstack;
1669
222k
}
1670
1671
char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1672
0
{
1673
0
    const char *ver;
1674
0
    const char *kx, *au, *enc, *mac;
1675
0
    uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1676
0
    static const char *const format = "%-30s %-7s Kx=%-8s Au=%-5s Enc=%-22s Mac=%-4s\n";
1677
1678
0
    if (buf == NULL) {
1679
0
        len = 128;
1680
0
        if ((buf = OPENSSL_malloc(len)) == NULL)
1681
0
            return NULL;
1682
0
    } else if (len < 128) {
1683
0
        return NULL;
1684
0
    }
1685
1686
0
    alg_mkey = cipher->algorithm_mkey;
1687
0
    alg_auth = cipher->algorithm_auth;
1688
0
    alg_enc = cipher->algorithm_enc;
1689
0
    alg_mac = cipher->algorithm_mac;
1690
1691
0
    ver = ssl_protocol_to_string(cipher->min_tls);
1692
1693
0
    switch (alg_mkey) {
1694
0
    case SSL_kRSA:
1695
0
        kx = "RSA";
1696
0
        break;
1697
0
    case SSL_kDHE:
1698
0
        kx = "DH";
1699
0
        break;
1700
0
    case SSL_kECDHE:
1701
0
        kx = "ECDH";
1702
0
        break;
1703
0
    case SSL_kPSK:
1704
0
        kx = "PSK";
1705
0
        break;
1706
0
    case SSL_kRSAPSK:
1707
0
        kx = "RSAPSK";
1708
0
        break;
1709
0
    case SSL_kECDHEPSK:
1710
0
        kx = "ECDHEPSK";
1711
0
        break;
1712
0
    case SSL_kDHEPSK:
1713
0
        kx = "DHEPSK";
1714
0
        break;
1715
0
    case SSL_kSRP:
1716
0
        kx = "SRP";
1717
0
        break;
1718
0
    case SSL_kGOST:
1719
0
        kx = "GOST";
1720
0
        break;
1721
0
    case SSL_kGOST18:
1722
0
        kx = "GOST18";
1723
0
        break;
1724
0
    case SSL_kANY:
1725
0
        kx = "any";
1726
0
        break;
1727
0
    default:
1728
0
        kx = "unknown";
1729
0
    }
1730
1731
0
    switch (alg_auth) {
1732
0
    case SSL_aRSA:
1733
0
        au = "RSA";
1734
0
        break;
1735
0
    case SSL_aDSS:
1736
0
        au = "DSS";
1737
0
        break;
1738
0
    case SSL_aNULL:
1739
0
        au = "None";
1740
0
        break;
1741
0
    case SSL_aECDSA:
1742
0
        au = "ECDSA";
1743
0
        break;
1744
0
    case SSL_aPSK:
1745
0
        au = "PSK";
1746
0
        break;
1747
0
    case SSL_aSRP:
1748
0
        au = "SRP";
1749
0
        break;
1750
0
    case SSL_aGOST01:
1751
0
        au = "GOST01";
1752
0
        break;
1753
    /* New GOST ciphersuites have both SSL_aGOST12 and SSL_aGOST01 bits */
1754
0
    case (SSL_aGOST12 | SSL_aGOST01):
1755
0
        au = "GOST12";
1756
0
        break;
1757
0
    case SSL_aANY:
1758
0
        au = "any";
1759
0
        break;
1760
0
    default:
1761
0
        au = "unknown";
1762
0
        break;
1763
0
    }
1764
1765
0
    switch (alg_enc) {
1766
0
    case SSL_DES:
1767
0
        enc = "DES(56)";
1768
0
        break;
1769
0
    case SSL_3DES:
1770
0
        enc = "3DES(168)";
1771
0
        break;
1772
0
    case SSL_RC4:
1773
0
        enc = "RC4(128)";
1774
0
        break;
1775
0
    case SSL_RC2:
1776
0
        enc = "RC2(128)";
1777
0
        break;
1778
0
    case SSL_IDEA:
1779
0
        enc = "IDEA(128)";
1780
0
        break;
1781
0
    case SSL_eNULL:
1782
0
        enc = "None";
1783
0
        break;
1784
0
    case SSL_AES128:
1785
0
        enc = "AES(128)";
1786
0
        break;
1787
0
    case SSL_AES256:
1788
0
        enc = "AES(256)";
1789
0
        break;
1790
0
    case SSL_AES128GCM:
1791
0
        enc = "AESGCM(128)";
1792
0
        break;
1793
0
    case SSL_AES256GCM:
1794
0
        enc = "AESGCM(256)";
1795
0
        break;
1796
0
    case SSL_AES128CCM:
1797
0
        enc = "AESCCM(128)";
1798
0
        break;
1799
0
    case SSL_AES256CCM:
1800
0
        enc = "AESCCM(256)";
1801
0
        break;
1802
0
    case SSL_AES128CCM8:
1803
0
        enc = "AESCCM8(128)";
1804
0
        break;
1805
0
    case SSL_AES256CCM8:
1806
0
        enc = "AESCCM8(256)";
1807
0
        break;
1808
0
    case SSL_CAMELLIA128:
1809
0
        enc = "Camellia(128)";
1810
0
        break;
1811
0
    case SSL_CAMELLIA256:
1812
0
        enc = "Camellia(256)";
1813
0
        break;
1814
0
    case SSL_ARIA128GCM:
1815
0
        enc = "ARIAGCM(128)";
1816
0
        break;
1817
0
    case SSL_ARIA256GCM:
1818
0
        enc = "ARIAGCM(256)";
1819
0
        break;
1820
0
    case SSL_SEED:
1821
0
        enc = "SEED(128)";
1822
0
        break;
1823
0
    case SSL_eGOST2814789CNT:
1824
0
    case SSL_eGOST2814789CNT12:
1825
0
        enc = "GOST89(256)";
1826
0
        break;
1827
0
    case SSL_MAGMA:
1828
0
        enc = "MAGMA";
1829
0
        break;
1830
0
    case SSL_KUZNYECHIK:
1831
0
        enc = "KUZNYECHIK";
1832
0
        break;
1833
0
    case SSL_CHACHA20POLY1305:
1834
0
        enc = "CHACHA20/POLY1305(256)";
1835
0
        break;
1836
0
    default:
1837
0
        enc = "unknown";
1838
0
        break;
1839
0
    }
1840
1841
0
    switch (alg_mac) {
1842
0
    case SSL_MD5:
1843
0
        mac = "MD5";
1844
0
        break;
1845
0
    case SSL_SHA1:
1846
0
        mac = "SHA1";
1847
0
        break;
1848
0
    case SSL_SHA256:
1849
0
        mac = "SHA256";
1850
0
        break;
1851
0
    case SSL_SHA384:
1852
0
        mac = "SHA384";
1853
0
        break;
1854
0
    case SSL_AEAD:
1855
0
        mac = "AEAD";
1856
0
        break;
1857
0
    case SSL_GOST89MAC:
1858
0
    case SSL_GOST89MAC12:
1859
0
        mac = "GOST89";
1860
0
        break;
1861
0
    case SSL_GOST94:
1862
0
        mac = "GOST94";
1863
0
        break;
1864
0
    case SSL_GOST12_256:
1865
0
    case SSL_GOST12_512:
1866
0
        mac = "GOST2012";
1867
0
        break;
1868
0
    default:
1869
0
        mac = "unknown";
1870
0
        break;
1871
0
    }
1872
1873
0
    BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1874
1875
0
    return buf;
1876
0
}
1877
1878
const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1879
0
{
1880
0
    if (c == NULL)
1881
0
        return "(NONE)";
1882
1883
    /*
1884
     * Backwards-compatibility crutch.  In almost all contexts we report TLS
1885
     * 1.0 as "TLSv1", but for ciphers we report "TLSv1.0".
1886
     */
1887
0
    if (c->min_tls == TLS1_VERSION)
1888
0
        return "TLSv1.0";
1889
0
    return ssl_protocol_to_string(c->min_tls);
1890
0
}
1891
1892
/* return the actual cipher being used */
1893
const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1894
0
{
1895
0
    if (c != NULL)
1896
0
        return c->name;
1897
0
    return "(NONE)";
1898
0
}
1899
1900
/* return the actual cipher being used in RFC standard name */
1901
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c)
1902
0
{
1903
0
    if (c != NULL)
1904
0
        return c->stdname;
1905
0
    return "(NONE)";
1906
0
}
1907
1908
/* return the OpenSSL name based on given RFC standard name */
1909
const char *OPENSSL_cipher_name(const char *stdname)
1910
0
{
1911
0
    const SSL_CIPHER *c;
1912
1913
0
    if (stdname == NULL)
1914
0
        return "(NONE)";
1915
0
    c = ssl3_get_cipher_by_std_name(stdname);
1916
0
    return SSL_CIPHER_get_name(c);
1917
0
}
1918
1919
/* number of bits for symmetric cipher */
1920
int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1921
0
{
1922
0
    int ret = 0;
1923
1924
0
    if (c != NULL) {
1925
0
        if (alg_bits != NULL)
1926
0
            *alg_bits = (int)c->alg_bits;
1927
0
        ret = (int)c->strength_bits;
1928
0
    }
1929
0
    return ret;
1930
0
}
1931
1932
uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c)
1933
483k
{
1934
483k
    return c->id;
1935
483k
}
1936
1937
uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *c)
1938
0
{
1939
0
    return c->id & 0xFFFF;
1940
0
}
1941
1942
SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1943
0
{
1944
0
    SSL_COMP *ctmp;
1945
0
    SSL_COMP srch_key;
1946
0
    int i;
1947
1948
0
    if ((n == 0) || (sk == NULL))
1949
0
        return NULL;
1950
0
    srch_key.id = n;
1951
0
    i = sk_SSL_COMP_find(sk, &srch_key);
1952
0
    if (i >= 0)
1953
0
        ctmp = sk_SSL_COMP_value(sk, i);
1954
0
    else
1955
0
        ctmp = NULL;
1956
1957
0
    return ctmp;
1958
0
}
1959
1960
#ifdef OPENSSL_NO_COMP
1961
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1962
{
1963
    return NULL;
1964
}
1965
1966
STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1967
                                                      *meths)
1968
{
1969
    return meths;
1970
}
1971
1972
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
1973
{
1974
    return 1;
1975
}
1976
1977
#else
1978
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
1979
75.2k
{
1980
75.2k
    STACK_OF(SSL_COMP) **rv;
1981
1982
75.2k
    rv = (STACK_OF(SSL_COMP) **)OSSL_LIB_CTX_get_data(NULL,
1983
75.2k
                                     OSSL_LIB_CTX_COMP_METHODS);
1984
75.2k
    if (rv != NULL)
1985
75.2k
        return *rv;
1986
0
    else
1987
0
        return NULL;
1988
75.2k
}
1989
1990
STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
1991
                                                      *meths)
1992
0
{
1993
0
    STACK_OF(SSL_COMP) **comp_methods;
1994
0
    STACK_OF(SSL_COMP) *old_meths;
1995
1996
0
    comp_methods = (STACK_OF(SSL_COMP) **)OSSL_LIB_CTX_get_data(NULL,
1997
0
                                              OSSL_LIB_CTX_COMP_METHODS);
1998
0
    if (comp_methods == NULL) {
1999
0
        old_meths = meths;
2000
0
    } else {
2001
0
        old_meths = *comp_methods;
2002
0
        *comp_methods = meths;
2003
0
    }
2004
2005
0
    return old_meths;
2006
0
}
2007
2008
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
2009
0
{
2010
0
    STACK_OF(SSL_COMP) *comp_methods;
2011
0
    SSL_COMP *comp;
2012
2013
0
    comp_methods = SSL_COMP_get_compression_methods();
2014
2015
0
    if (comp_methods == NULL)
2016
0
        return 1;
2017
2018
0
    if (cm == NULL || COMP_get_type(cm) == NID_undef)
2019
0
        return 1;
2020
2021
    /*-
2022
     * According to draft-ietf-tls-compression-04.txt, the
2023
     * compression number ranges should be the following:
2024
     *
2025
     *   0 to  63:  methods defined by the IETF
2026
     *  64 to 192:  external party methods assigned by IANA
2027
     * 193 to 255:  reserved for private use
2028
     */
2029
0
    if (id < 193 || id > 255) {
2030
0
        ERR_raise(ERR_LIB_SSL, SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
2031
0
        return 1;
2032
0
    }
2033
2034
0
    comp = OPENSSL_malloc(sizeof(*comp));
2035
0
    if (comp == NULL)
2036
0
        return 1;
2037
2038
0
    comp->id = id;
2039
0
    if (sk_SSL_COMP_find(comp_methods, comp) >= 0) {
2040
0
        OPENSSL_free(comp);
2041
0
        ERR_raise(ERR_LIB_SSL, SSL_R_DUPLICATE_COMPRESSION_ID);
2042
0
        return 1;
2043
0
    }
2044
0
    if (!sk_SSL_COMP_push(comp_methods, comp)) {
2045
0
        OPENSSL_free(comp);
2046
0
        ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
2047
0
        return 1;
2048
0
    }
2049
2050
0
    return 0;
2051
0
}
2052
#endif
2053
2054
const char *SSL_COMP_get_name(const COMP_METHOD *comp)
2055
0
{
2056
0
#ifndef OPENSSL_NO_COMP
2057
0
    return comp ? COMP_get_name(comp) : NULL;
2058
#else
2059
    return NULL;
2060
#endif
2061
0
}
2062
2063
const char *SSL_COMP_get0_name(const SSL_COMP *comp)
2064
0
{
2065
0
#ifndef OPENSSL_NO_COMP
2066
0
    return comp->name;
2067
#else
2068
    return NULL;
2069
#endif
2070
0
}
2071
2072
int SSL_COMP_get_id(const SSL_COMP *comp)
2073
0
{
2074
0
#ifndef OPENSSL_NO_COMP
2075
0
    return comp->id;
2076
#else
2077
    return -1;
2078
#endif
2079
0
}
2080
2081
const SSL_CIPHER *ssl_get_cipher_by_char(SSL_CONNECTION *s,
2082
                                         const unsigned char *ptr,
2083
                                         int all)
2084
678k
{
2085
678k
    const SSL_CIPHER *c = SSL_CONNECTION_GET_SSL(s)->method->get_cipher_by_char(ptr);
2086
2087
678k
    if (c == NULL || (!all && c->valid == 0))
2088
364k
        return NULL;
2089
314k
    return c;
2090
678k
}
2091
2092
const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
2093
0
{
2094
0
    return ssl->method->get_cipher_by_char(ptr);
2095
0
}
2096
2097
int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
2098
0
{
2099
0
    int i;
2100
0
    if (c == NULL)
2101
0
        return NID_undef;
2102
0
    i = ssl_cipher_info_lookup(ssl_cipher_table_cipher, c->algorithm_enc);
2103
0
    if (i == -1)
2104
0
        return NID_undef;
2105
0
    return ssl_cipher_table_cipher[i].nid;
2106
0
}
2107
2108
int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
2109
0
{
2110
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_mac, c->algorithm_mac);
2111
2112
0
    if (i == -1)
2113
0
        return NID_undef;
2114
0
    return ssl_cipher_table_mac[i].nid;
2115
0
}
2116
2117
int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
2118
0
{
2119
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_kx, c->algorithm_mkey);
2120
2121
0
    if (i == -1)
2122
0
        return NID_undef;
2123
0
    return ssl_cipher_table_kx[i].nid;
2124
0
}
2125
2126
int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
2127
0
{
2128
0
    int i = ssl_cipher_info_lookup(ssl_cipher_table_auth, c->algorithm_auth);
2129
2130
0
    if (i == -1)
2131
0
        return NID_undef;
2132
0
    return ssl_cipher_table_auth[i].nid;
2133
0
}
2134
2135
177k
int ssl_get_md_idx(int md_nid) {
2136
177k
    int i;
2137
2138
2.65M
    for(i = 0; i < SSL_MD_NUM_IDX; i++) {
2139
2.47M
        if (md_nid == ssl_cipher_table_mac[i].nid)
2140
0
            return i;
2141
2.47M
    }
2142
177k
    return -1;
2143
177k
}
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 ||
2196
0
                EVP_CIPHER_get_mode(e_ciph) != EVP_CIPH_CBC_MODE)
2197
0
                return 0;
2198
2199
0
            in = 1; /* padding length byte */
2200
0
            out = EVP_CIPHER_get_iv_length(e_ciph);
2201
0
            if (out < 0)
2202
0
                return 0;
2203
0
            blk = EVP_CIPHER_get_block_size(e_ciph);
2204
0
            if (blk <= 0)
2205
0
                return 0;
2206
0
        }
2207
0
    }
2208
2209
0
    *mac_overhead = (size_t)mac;
2210
0
    *int_overhead = (size_t)in;
2211
0
    *blocksize = (size_t)blk;
2212
0
    *ext_overhead = (size_t)out;
2213
2214
0
    return 1;
2215
0
}
2216
2217
int ssl_cert_is_disabled(SSL_CTX *ctx, size_t idx)
2218
3.30M
{
2219
3.30M
    const SSL_CERT_LOOKUP *cl;
2220
2221
    /* A provider-loaded key type is always enabled */
2222
3.30M
    if (idx >= SSL_PKEY_NUM)
2223
97.5k
        return 0;
2224
2225
3.20M
    cl = ssl_cert_lookup_by_idx(idx, ctx);
2226
3.20M
    if (cl == NULL || (cl->amask & ctx->disabled_auth_mask) != 0)
2227
0
        return 1;
2228
3.20M
    return 0;
2229
3.20M
}
2230
2231
/*
2232
 * Default list of TLSv1.2 (and earlier) ciphers
2233
 * SSL_DEFAULT_CIPHER_LIST deprecated in 3.0.0
2234
 * Update both macro and function simultaneously
2235
 */
2236
const char *OSSL_default_cipher_list(void)
2237
146k
{
2238
146k
    return "ALL:!COMPLEMENTOFDEFAULT:!eNULL";
2239
146k
}
2240
2241
/*
2242
 * Default list of TLSv1.3 (and later) ciphers
2243
 * TLS_DEFAULT_CIPHERSUITES deprecated in 3.0.0
2244
 * Update both macro and function simultaneously
2245
 */
2246
const char *OSSL_default_ciphersuites(void)
2247
146k
{
2248
146k
    return "TLS_AES_256_GCM_SHA384:"
2249
146k
           "TLS_CHACHA20_POLY1305_SHA256:"
2250
146k
           "TLS_AES_128_GCM_SHA256";
2251
146k
}