Coverage Report

Created: 2025-12-14 06:48

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