Coverage Report

Created: 2026-08-14 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/ssl/ssl_cipher.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
// Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3
// Copyright 2005 Nokia. All rights reserved.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     https://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include <openssl/ssl.h>
18
19
#include <assert.h>
20
#include <string.h>
21
22
#include <iterator>
23
24
#include <openssl/err.h>
25
#include <openssl/md5.h>
26
#include <openssl/mem.h>
27
#include <openssl/sha.h>
28
#include <openssl/stack.h>
29
30
#include "../crypto/internal.h"
31
#include "internal.h"
32
33
34
BSSL_NAMESPACE_BEGIN
35
36
static constexpr SSL_CIPHER kCiphers[] = {
37
    // The RSA ciphers
38
39
    // Cipher 0A
40
    {
41
        SSL3_TXT_RSA_DES_192_CBC3_SHA,
42
        "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
43
        SSL_CIPHER_RSA_WITH_3DES_EDE_CBC_SHA,
44
        SSL_kRSA,
45
        SSL_aRSA_DECRYPT,
46
        SSL_3DES,
47
        SSL_SHA1,
48
        SSL_HANDSHAKE_MAC_DEFAULT,
49
    },
50
51
52
    // New AES ciphersuites
53
54
    // Cipher 2F
55
    {
56
        TLS1_TXT_RSA_WITH_AES_128_SHA,
57
        "TLS_RSA_WITH_AES_128_CBC_SHA",
58
        SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA,
59
        SSL_kRSA,
60
        SSL_aRSA_DECRYPT,
61
        SSL_AES128,
62
        SSL_SHA1,
63
        SSL_HANDSHAKE_MAC_DEFAULT,
64
    },
65
66
    // Cipher 35
67
    {
68
        TLS1_TXT_RSA_WITH_AES_256_SHA,
69
        "TLS_RSA_WITH_AES_256_CBC_SHA",
70
        SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA,
71
        SSL_kRSA,
72
        SSL_aRSA_DECRYPT,
73
        SSL_AES256,
74
        SSL_SHA1,
75
        SSL_HANDSHAKE_MAC_DEFAULT,
76
    },
77
78
    // PSK cipher suites.
79
80
    // Cipher 8C
81
    {
82
        TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
83
        "TLS_PSK_WITH_AES_128_CBC_SHA",
84
        SSL_CIPHER_PSK_WITH_AES_128_CBC_SHA,
85
        SSL_kPSK,
86
        SSL_aPSK,
87
        SSL_AES128,
88
        SSL_SHA1,
89
        SSL_HANDSHAKE_MAC_DEFAULT,
90
    },
91
92
    // Cipher 8D
93
    {
94
        TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
95
        "TLS_PSK_WITH_AES_256_CBC_SHA",
96
        SSL_CIPHER_PSK_WITH_AES_256_CBC_SHA,
97
        SSL_kPSK,
98
        SSL_aPSK,
99
        SSL_AES256,
100
        SSL_SHA1,
101
        SSL_HANDSHAKE_MAC_DEFAULT,
102
    },
103
104
    // GCM ciphersuites from RFC 5288
105
106
    // Cipher 9C
107
    {
108
        TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
109
        "TLS_RSA_WITH_AES_128_GCM_SHA256",
110
        SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256,
111
        SSL_kRSA,
112
        SSL_aRSA_DECRYPT,
113
        SSL_AES128GCM,
114
        SSL_AEAD,
115
        SSL_HANDSHAKE_MAC_SHA256,
116
    },
117
118
    // Cipher 9D
119
    {
120
        TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
121
        "TLS_RSA_WITH_AES_256_GCM_SHA384",
122
        SSL_CIPHER_RSA_WITH_AES_256_GCM_SHA384,
123
        SSL_kRSA,
124
        SSL_aRSA_DECRYPT,
125
        SSL_AES256GCM,
126
        SSL_AEAD,
127
        SSL_HANDSHAKE_MAC_SHA384,
128
    },
129
130
    // TLS 1.3 suites.
131
132
    // Cipher 1301
133
    {
134
        TLS1_3_RFC_AES_128_GCM_SHA256,
135
        "TLS_AES_128_GCM_SHA256",
136
        SSL_CIPHER_AES_128_GCM_SHA256,
137
        SSL_kGENERIC,
138
        SSL_aGENERIC,
139
        SSL_AES128GCM,
140
        SSL_AEAD,
141
        SSL_HANDSHAKE_MAC_SHA256,
142
    },
143
144
    // Cipher 1302
145
    {
146
        TLS1_3_RFC_AES_256_GCM_SHA384,
147
        "TLS_AES_256_GCM_SHA384",
148
        SSL_CIPHER_AES_256_GCM_SHA384,
149
        SSL_kGENERIC,
150
        SSL_aGENERIC,
151
        SSL_AES256GCM,
152
        SSL_AEAD,
153
        SSL_HANDSHAKE_MAC_SHA384,
154
    },
155
156
    // Cipher 1303
157
    {
158
        TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
159
        "TLS_CHACHA20_POLY1305_SHA256",
160
        SSL_CIPHER_CHACHA20_POLY1305_SHA256,
161
        SSL_kGENERIC,
162
        SSL_aGENERIC,
163
        SSL_CHACHA20POLY1305,
164
        SSL_AEAD,
165
        SSL_HANDSHAKE_MAC_SHA256,
166
    },
167
168
    // Cipher C009
169
    {
170
        TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
171
        "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
172
        SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
173
        SSL_kECDHE,
174
        SSL_aECDSA,
175
        SSL_AES128,
176
        SSL_SHA1,
177
        SSL_HANDSHAKE_MAC_DEFAULT,
178
    },
179
180
    // Cipher C00A
181
    {
182
        TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
183
        "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
184
        SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
185
        SSL_kECDHE,
186
        SSL_aECDSA,
187
        SSL_AES256,
188
        SSL_SHA1,
189
        SSL_HANDSHAKE_MAC_DEFAULT,
190
    },
191
192
    // Cipher C013
193
    {
194
        TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
195
        "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
196
        SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA,
197
        SSL_kECDHE,
198
        SSL_aRSA_SIGN,
199
        SSL_AES128,
200
        SSL_SHA1,
201
        SSL_HANDSHAKE_MAC_DEFAULT,
202
    },
203
204
    // Cipher C014
205
    {
206
        TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
207
        "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
208
        SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA,
209
        SSL_kECDHE,
210
        SSL_aRSA_SIGN,
211
        SSL_AES256,
212
        SSL_SHA1,
213
        SSL_HANDSHAKE_MAC_DEFAULT,
214
    },
215
216
    // HMAC based TLS v1.2 ciphersuites from RFC5289
217
218
    // Cipher C023 (deprecated)
219
    {
220
        TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
221
        "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
222
        SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
223
        SSL_kECDHE,
224
        SSL_aECDSA,
225
        SSL_AES128,
226
        SSL_SHA256,
227
        SSL_HANDSHAKE_MAC_SHA256,
228
    },
229
230
    // Cipher C027 (deprecated)
231
    {
232
        TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
233
        "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
234
        SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
235
        SSL_kECDHE,
236
        SSL_aRSA_SIGN,
237
        SSL_AES128,
238
        SSL_SHA256,
239
        SSL_HANDSHAKE_MAC_SHA256,
240
    },
241
242
    // GCM based TLS v1.2 ciphersuites from RFC 5289
243
244
    // Cipher C02B
245
    {
246
        TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
247
        "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
248
        SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
249
        SSL_kECDHE,
250
        SSL_aECDSA,
251
        SSL_AES128GCM,
252
        SSL_AEAD,
253
        SSL_HANDSHAKE_MAC_SHA256,
254
    },
255
256
    // Cipher C02C
257
    {
258
        TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
259
        "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
260
        SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
261
        SSL_kECDHE,
262
        SSL_aECDSA,
263
        SSL_AES256GCM,
264
        SSL_AEAD,
265
        SSL_HANDSHAKE_MAC_SHA384,
266
    },
267
268
    // Cipher C02F
269
    {
270
        TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
271
        "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
272
        SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
273
        SSL_kECDHE,
274
        SSL_aRSA_SIGN,
275
        SSL_AES128GCM,
276
        SSL_AEAD,
277
        SSL_HANDSHAKE_MAC_SHA256,
278
    },
279
280
    // Cipher C030
281
    {
282
        TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
283
        "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
284
        SSL_CIPHER_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
285
        SSL_kECDHE,
286
        SSL_aRSA_SIGN,
287
        SSL_AES256GCM,
288
        SSL_AEAD,
289
        SSL_HANDSHAKE_MAC_SHA384,
290
    },
291
292
    // ECDHE-PSK cipher suites.
293
294
    // Cipher C035
295
    {
296
        TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
297
        "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
298
        SSL_CIPHER_ECDHE_PSK_WITH_AES_128_CBC_SHA,
299
        SSL_kECDHE,
300
        SSL_aPSK,
301
        SSL_AES128,
302
        SSL_SHA1,
303
        SSL_HANDSHAKE_MAC_DEFAULT,
304
    },
305
306
    // Cipher C036
307
    {
308
        TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
309
        "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
310
        SSL_CIPHER_ECDHE_PSK_WITH_AES_256_CBC_SHA,
311
        SSL_kECDHE,
312
        SSL_aPSK,
313
        SSL_AES256,
314
        SSL_SHA1,
315
        SSL_HANDSHAKE_MAC_DEFAULT,
316
    },
317
318
    // ChaCha20-Poly1305 cipher suites.
319
320
    // Cipher CCA8
321
    {
322
        TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
323
        "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
324
        SSL_CIPHER_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
325
        SSL_kECDHE,
326
        SSL_aRSA_SIGN,
327
        SSL_CHACHA20POLY1305,
328
        SSL_AEAD,
329
        SSL_HANDSHAKE_MAC_SHA256,
330
    },
331
332
    // Cipher CCA9
333
    {
334
        TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
335
        "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
336
        SSL_CIPHER_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
337
        SSL_kECDHE,
338
        SSL_aECDSA,
339
        SSL_CHACHA20POLY1305,
340
        SSL_AEAD,
341
        SSL_HANDSHAKE_MAC_SHA256,
342
    },
343
344
    // Cipher CCAB
345
    {
346
        TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
347
        "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
348
        SSL_CIPHER_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
349
        SSL_kECDHE,
350
        SSL_aPSK,
351
        SSL_CHACHA20POLY1305,
352
        SSL_AEAD,
353
        SSL_HANDSHAKE_MAC_SHA256,
354
    },
355
356
};
357
358
0
Span<const SSL_CIPHER> AllCiphers() { return kCiphers; }
359
360
0
static constexpr size_t NumTLS13Ciphers() {
361
0
  size_t num = 0;
362
0
  for (const auto &cipher : kCiphers) {
363
0
    if (cipher.algorithm_mkey == SSL_kGENERIC) {
364
0
      num++;
365
0
    }
366
0
  }
367
0
  return num;
368
0
}
369
370
405k
#define CIPHER_ADD 1
371
42.3k
#define CIPHER_KILL 2
372
73.2k
#define CIPHER_DEL 3
373
122k
#define CIPHER_ORD 4
374
80.8k
#define CIPHER_SPECIAL 5
375
376
typedef struct cipher_order_st {
377
  const SSL_CIPHER *cipher;
378
  bool active;
379
  bool in_group;
380
  struct cipher_order_st *next, *prev;
381
} CIPHER_ORDER;
382
383
typedef struct cipher_alias_st {
384
  // name is the name of the cipher alias.
385
  const char *name = nullptr;
386
387
  // The following fields are bitmasks for the corresponding fields on
388
  // `SSL_CIPHER`. A cipher matches a cipher alias iff, for each bitmask, the
389
  // bit corresponding to the cipher's value is set to 1. If any bitmask is
390
  // all zeroes, the alias matches nothing. Use `~0u` for the default value.
391
  uint32_t algorithm_mkey = ~0u;
392
  uint32_t algorithm_auth = ~0u;
393
  uint32_t algorithm_enc = ~0u;
394
  uint32_t algorithm_mac = ~0u;
395
396
  // min_version, if non-zero, matches all ciphers which were added in that
397
  // particular protocol version.
398
  uint16_t min_version = 0;
399
400
  // include_deprecated, if true, means this alias includes deprecated ciphers.
401
  bool include_deprecated = false;
402
} CIPHER_ALIAS;
403
404
static const CIPHER_ALIAS kCipherAliases[] = {
405
    {"ALL", ~0u, ~0u, ~0u, ~0u, 0},
406
407
    // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing.
408
409
    // key exchange aliases
410
    // (some of those using only a single bit here combine
411
    // multiple key exchange algs according to the RFCs.
412
    {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
413
414
    {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
415
    {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
416
    {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
417
418
    {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
419
420
    // server authentication aliases
421
    {"aRSA", ~0u, SSL_aRSA_SIGN | SSL_aRSA_DECRYPT, ~0u, ~0u, 0},
422
    {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
423
    {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
424
    {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
425
426
    // aliases combining key exchange and server authentication
427
    {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
428
    {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
429
    {"RSA", SSL_kRSA, SSL_aRSA_SIGN | SSL_aRSA_DECRYPT, ~0u, ~0u, 0},
430
    {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
431
432
    // symmetric encryption aliases
433
    {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0, /*include_deprecated=*/true},
434
    {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0,
435
     /*include_deprecated=*/false},
436
    {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0,
437
     /*include_deprecated=*/false},
438
    {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
439
    {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0,
440
     /*include_deprecated=*/false},
441
    {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0,
442
     /*include_deprecated=*/false},
443
444
    // MAC aliases
445
    {"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0},
446
    {"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0},
447
448
    // Legacy protocol minimum version aliases. "TLSv1" is intentionally the
449
    // same as "SSLv3".
450
    {"SSLv3", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
451
    {"TLSv1", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
452
    {"TLSv1.2", ~0u, ~0u, ~0u, ~0u, TLS1_2_VERSION},
453
454
    // Legacy strength classes.
455
    {"HIGH", ~0u, ~0u, ~0u, ~0u, 0},
456
    {"FIPS", ~0u, ~0u, ~0u, ~0u, 0},
457
};
458
459
static const size_t kCipherAliasesLen = std::size(kCipherAliases);
460
461
bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
462
                             size_t *out_mac_secret_len,
463
                             size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
464
250k
                             uint16_t version) {
465
250k
  *out_aead = nullptr;
466
250k
  *out_mac_secret_len = 0;
467
250k
  *out_fixed_iv_len = 0;
468
469
250k
  if (cipher->algorithm_mac == SSL_AEAD) {
470
194k
    if (cipher->algorithm_enc == SSL_AES128GCM) {
471
105k
      if (version < TLS1_3_VERSION) {
472
68.9k
        *out_aead = EVP_aead_aes_128_gcm_tls12();
473
68.9k
      } else {
474
36.6k
        *out_aead = EVP_aead_aes_128_gcm_tls13();
475
36.6k
      }
476
105k
      *out_fixed_iv_len = 4;
477
105k
    } else if (cipher->algorithm_enc == SSL_AES256GCM) {
478
56.7k
      if (version < TLS1_3_VERSION) {
479
28.1k
        *out_aead = EVP_aead_aes_256_gcm_tls12();
480
28.5k
      } else {
481
28.5k
        *out_aead = EVP_aead_aes_256_gcm_tls13();
482
28.5k
      }
483
56.7k
      *out_fixed_iv_len = 4;
484
56.7k
    } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
485
32.4k
      *out_aead = EVP_aead_chacha20_poly1305();
486
32.4k
      *out_fixed_iv_len = 12;
487
32.4k
    } else {
488
0
      return false;
489
0
    }
490
491
    // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
492
    // above computes the TLS 1.2 construction.
493
194k
    if (version >= TLS1_3_VERSION) {
494
97.4k
      *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
495
97.4k
    }
496
194k
  } else if (cipher->algorithm_mac == SSL_SHA1) {
497
55.5k
    if (cipher->algorithm_enc == SSL_3DES) {
498
7.93k
      if (version == TLS1_VERSION) {
499
228
        *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
500
228
        *out_fixed_iv_len = 8;
501
7.70k
      } else {
502
7.70k
        *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
503
7.70k
      }
504
47.6k
    } else if (cipher->algorithm_enc == SSL_AES128) {
505
6.69k
      if (version == TLS1_VERSION) {
506
1.50k
        *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
507
1.50k
        *out_fixed_iv_len = 16;
508
5.18k
      } else {
509
5.18k
        *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
510
5.18k
      }
511
40.9k
    } else if (cipher->algorithm_enc == SSL_AES256) {
512
40.9k
      if (version == TLS1_VERSION) {
513
3.13k
        *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
514
3.13k
        *out_fixed_iv_len = 16;
515
37.8k
      } else {
516
37.8k
        *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
517
37.8k
      }
518
40.9k
    } else {
519
0
      return false;
520
0
    }
521
522
55.5k
    *out_mac_secret_len = SHA_DIGEST_LENGTH;
523
55.5k
  } else if (cipher->algorithm_mac == SSL_SHA256) {
524
0
    if (cipher->algorithm_enc == SSL_AES128) {
525
0
      *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
526
0
    } else {
527
0
      return false;
528
0
    }
529
530
0
    *out_mac_secret_len = SHA256_DIGEST_LENGTH;
531
0
  } else {
532
0
    return false;
533
0
  }
534
535
250k
  return true;
536
250k
}
537
538
const EVP_MD *ssl_get_handshake_digest(uint16_t version,
539
236k
                                       const SSL_CIPHER *cipher) {
540
236k
  switch (cipher->algorithm_prf) {
541
35.7k
    case SSL_HANDSHAKE_MAC_DEFAULT:
542
35.7k
      return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
543
145k
    case SSL_HANDSHAKE_MAC_SHA256:
544
145k
      return EVP_sha256();
545
55.3k
    case SSL_HANDSHAKE_MAC_SHA384:
546
55.3k
      return EVP_sha384();
547
0
    default:
548
0
      assert(0);
549
0
      return nullptr;
550
236k
  }
551
236k
}
552
553
63.1k
static bool is_cipher_list_separator(char c, bool is_strict) {
554
63.1k
  if (c == ':') {
555
11.2k
    return true;
556
11.2k
  }
557
51.9k
  return !is_strict && (c == ' ' || c == ';' || c == ',');
558
63.1k
}
559
560
// rule_equals returns whether the NUL-terminated string `rule` is equal to the
561
// `buf_len` bytes at `buf`.
562
1.88M
static bool rule_equals(const char *rule, const char *buf, size_t buf_len) {
563
  // `strncmp` alone only checks that `buf` is a prefix of `rule`.
564
1.88M
  return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
565
1.88M
}
566
567
static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
568
224k
                           CIPHER_ORDER **tail) {
569
224k
  if (curr == *tail) {
570
3.50k
    return;
571
3.50k
  }
572
220k
  if (curr == *head) {
573
115k
    *head = curr->next;
574
115k
  }
575
220k
  if (curr->prev != nullptr) {
576
105k
    curr->prev->next = curr->next;
577
105k
  }
578
220k
  if (curr->next != nullptr) {
579
220k
    curr->next->prev = curr->prev;
580
220k
  }
581
220k
  (*tail)->next = curr;
582
220k
  curr->prev = *tail;
583
220k
  curr->next = nullptr;
584
220k
  *tail = curr;
585
220k
}
586
587
static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
588
1.81k
                           CIPHER_ORDER **tail) {
589
1.81k
  if (curr == *head) {
590
859
    return;
591
859
  }
592
960
  if (curr == *tail) {
593
484
    *tail = curr->prev;
594
484
  }
595
960
  if (curr->next != nullptr) {
596
476
    curr->next->prev = curr->prev;
597
476
  }
598
960
  if (curr->prev != nullptr) {
599
960
    curr->prev->next = curr->next;
600
960
  }
601
960
  (*head)->prev = curr;
602
960
  curr->next = *head;
603
960
  curr->prev = nullptr;
604
960
  *head = curr;
605
960
}
606
607
18.2k
SSLCipherPreferenceList::~SSLCipherPreferenceList() {
608
18.2k
  OPENSSL_free(in_group_flags);
609
18.2k
}
610
611
bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers_arg,
612
18.2k
                                   Span<const bool> in_group_flags_arg) {
613
18.2k
  if (sk_SSL_CIPHER_num(ciphers_arg.get()) != in_group_flags_arg.size()) {
614
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
615
0
    return false;
616
0
  }
617
618
18.2k
  Array<bool> copy;
619
18.2k
  if (!copy.CopyFrom(in_group_flags_arg)) {
620
0
    return false;
621
0
  }
622
18.2k
  ciphers = std::move(ciphers_arg);
623
18.2k
  size_t unused_len;
624
18.2k
  copy.Release(&in_group_flags, &unused_len);
625
18.2k
  return true;
626
18.2k
}
627
628
0
bool SSLCipherPreferenceList::Init(const SSLCipherPreferenceList &other) {
629
0
  size_t size = sk_SSL_CIPHER_num(other.ciphers.get());
630
0
  Span<const bool> other_flags(other.in_group_flags, size);
631
0
  UniquePtr<STACK_OF(SSL_CIPHER)> other_ciphers(
632
0
      sk_SSL_CIPHER_dup(other.ciphers.get()));
633
0
  if (!other_ciphers) {
634
0
    return false;
635
0
  }
636
0
  return Init(std::move(other_ciphers), other_flags);
637
0
}
638
639
0
void SSLCipherPreferenceList::Remove(const SSL_CIPHER *cipher) {
640
0
  size_t index;
641
0
  if (!sk_SSL_CIPHER_find(ciphers.get(), &index, cipher)) {
642
0
    return;
643
0
  }
644
0
  if (!in_group_flags[index] /* last element of group */ && index > 0) {
645
0
    in_group_flags[index - 1] = false;
646
0
  }
647
0
  for (size_t i = index; i < sk_SSL_CIPHER_num(ciphers.get()) - 1; ++i) {
648
0
    in_group_flags[i] = in_group_flags[i + 1];
649
0
  }
650
0
  sk_SSL_CIPHER_delete(ciphers.get(), index);
651
0
}
652
653
228k
bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher) {
654
228k
  return cipher->protocol_id ==
655
228k
             SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 ||
656
219k
         cipher->protocol_id == SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256 ||
657
209k
         cipher->algorithm_enc == SSL_3DES;
658
228k
}
659
660
// ssl_cipher_apply_rule applies the rule type `rule` to ciphers matching its
661
// parameters in the linked list from `*head_p` to `*tail_p`. It writes the new
662
// head and tail of the list to `*head_p` and `*tail_p`, respectively.
663
//
664
// - If `cipher_id` is non-zero, only that cipher is selected.
665
// - Otherwise, if `strength_bits` is non-negative, it selects ciphers
666
//   of that strength.
667
// - Otherwise, `alias` must be non-null. It selects ciphers that matches
668
//   `*alias`.
669
static void ssl_cipher_apply_rule(uint16_t cipher_id, const CIPHER_ALIAS *alias,
670
                                  int rule, int strength_bits, bool in_group,
671
                                  CIPHER_ORDER **head_p,
672
27.6k
                                  CIPHER_ORDER **tail_p) {
673
27.6k
  CIPHER_ORDER *head, *tail, *curr, *next, *last;
674
27.6k
  const SSL_CIPHER *cp;
675
27.6k
  bool reverse = false;
676
677
27.6k
  if (cipher_id == 0 && strength_bits == -1 && alias->min_version == 0 &&
678
17.9k
      (alias->algorithm_mkey == 0 || alias->algorithm_auth == 0 ||
679
17.5k
       alias->algorithm_enc == 0 || alias->algorithm_mac == 0)) {
680
    // The rule matches nothing, so bail early.
681
603
    return;
682
603
  }
683
684
27.0k
  if (rule == CIPHER_DEL) {
685
    // needed to maintain sorting between currently deleted ciphers
686
1.15k
    reverse = true;
687
1.15k
  }
688
689
27.0k
  head = *head_p;
690
27.0k
  tail = *tail_p;
691
692
27.0k
  if (reverse) {
693
1.15k
    next = tail;
694
1.15k
    last = head;
695
25.8k
  } else {
696
25.8k
    next = head;
697
25.8k
    last = tail;
698
25.8k
  }
699
700
27.0k
  curr = nullptr;
701
559k
  for (;;) {
702
559k
    if (curr == last) {
703
27.0k
      break;
704
27.0k
    }
705
706
532k
    curr = next;
707
532k
    if (curr == nullptr) {
708
0
      break;
709
0
    }
710
711
532k
    next = reverse ? curr->prev : curr->next;
712
532k
    cp = curr->cipher;
713
714
    // Selection criteria is either a specific cipher, the value of
715
    // `strength_bits`, or the algorithms used.
716
532k
    if (cipher_id != 0) {
717
9.74k
      if (cipher_id != cp->protocol_id) {
718
9.30k
        continue;
719
9.30k
      }
720
522k
    } else if (strength_bits >= 0) {
721
136k
      if (strength_bits != SSL_CIPHER_get_bits(cp, nullptr)) {
722
77.3k
        continue;
723
77.3k
      }
724
385k
    } else {
725
385k
      if (!(alias->algorithm_mkey & cp->algorithm_mkey) ||
726
364k
          !(alias->algorithm_auth & cp->algorithm_auth) ||
727
360k
          !(alias->algorithm_enc & cp->algorithm_enc) ||
728
319k
          !(alias->algorithm_mac & cp->algorithm_mac) ||
729
309k
          (alias->min_version != 0 &&
730
42.0k
           SSL_CIPHER_get_min_version(cp) != alias->min_version) ||
731
285k
          (!alias->include_deprecated && ssl_cipher_is_deprecated(cp))) {
732
131k
        continue;
733
131k
      }
734
385k
    }
735
736
    // add the cipher if it has not been added yet.
737
314k
    if (rule == CIPHER_ADD) {
738
      // reverse == false
739
200k
      if (!curr->active) {
740
180k
        ll_append_tail(&head, curr, &tail);
741
180k
        curr->active = true;
742
180k
        curr->in_group = in_group;
743
180k
      }
744
200k
    }
745
746
    // Move the added cipher to this location
747
114k
    else if (rule == CIPHER_ORD) {
748
      // reverse == false
749
69.4k
      if (curr->active) {
750
43.6k
        ll_append_tail(&head, curr, &tail);
751
43.6k
        curr->in_group = false;
752
43.6k
      }
753
69.4k
    } else if (rule == CIPHER_DEL) {
754
      // reverse == true
755
6.33k
      if (curr->active) {
756
        // most recently deleted ciphersuites get best positions
757
        // for any future CIPHER_ADD (note that the CIPHER_DEL loop
758
        // works in reverse to maintain the order)
759
1.81k
        ll_append_head(&head, curr, &tail);
760
1.81k
        curr->active = false;
761
1.81k
        curr->in_group = false;
762
1.81k
      }
763
38.5k
    } else if (rule == CIPHER_KILL) {
764
      // reverse == false
765
38.5k
      if (head == curr) {
766
16.2k
        head = curr->next;
767
22.3k
      } else {
768
22.3k
        curr->prev->next = curr->next;
769
22.3k
      }
770
771
38.5k
      if (tail == curr) {
772
1.45k
        tail = curr->prev;
773
1.45k
      }
774
38.5k
      curr->active = false;
775
38.5k
      if (curr->next != nullptr) {
776
37.1k
        curr->next->prev = curr->prev;
777
37.1k
      }
778
38.5k
      if (curr->prev != nullptr) {
779
22.3k
        curr->prev->next = curr->next;
780
22.3k
      }
781
38.5k
      curr->next = nullptr;
782
38.5k
      curr->prev = nullptr;
783
38.5k
    }
784
314k
  }
785
786
27.0k
  *head_p = head;
787
27.0k
  *tail_p = tail;
788
27.0k
}
789
790
static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
791
5.51k
                                     CIPHER_ORDER **tail_p) {
792
  // This routine sorts the ciphers with descending strength. The sorting must
793
  // keep the pre-sorted sequence, so we apply the normal sorting routine as
794
  // '+' movement to the end of the list.
795
5.51k
  int max_strength_bits = 0;
796
5.51k
  CIPHER_ORDER *curr = *head_p;
797
104k
  while (curr != nullptr) {
798
99.4k
    if (curr->active &&
799
37.8k
        SSL_CIPHER_get_bits(curr->cipher, nullptr) > max_strength_bits) {
800
5.77k
      max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, nullptr);
801
5.77k
    }
802
99.4k
    curr = curr->next;
803
99.4k
  }
804
805
5.51k
  Array<int> number_uses;
806
5.51k
  if (!number_uses.Init(max_strength_bits + 1)) {
807
0
    return false;
808
0
  }
809
810
  // Now find the strength_bits values actually used.
811
5.51k
  curr = *head_p;
812
104k
  while (curr != nullptr) {
813
99.4k
    if (curr->active) {
814
37.8k
      number_uses[SSL_CIPHER_get_bits(curr->cipher, nullptr)]++;
815
37.8k
    }
816
99.4k
    curr = curr->next;
817
99.4k
  }
818
819
  // Go through the list of used strength_bits values in descending order.
820
901k
  for (int i = max_strength_bits; i >= 0; i--) {
821
896k
    if (number_uses[i] > 0) {
822
7.02k
      ssl_cipher_apply_rule(/*cipher_id=*/0, /*alias=*/nullptr, CIPHER_ORD, i,
823
7.02k
                            false, head_p, tail_p);
824
7.02k
    }
825
896k
  }
826
827
5.51k
  return true;
828
5.51k
}
829
830
static bool ssl_cipher_process_rulestr(const char *rule_str,
831
                                       CIPHER_ORDER **head_p,
832
25.2k
                                       CIPHER_ORDER **tail_p, bool strict) {
833
25.2k
  const char *l, *buf;
834
25.2k
  bool in_group = false, has_group = false;
835
25.2k
  size_t j, buf_len;
836
25.2k
  char ch;
837
838
25.2k
  l = rule_str;
839
74.9k
  for (;;) {
840
74.9k
    ch = *l;
841
842
74.9k
    if (ch == '\0') {
843
13.5k
      break;  // done
844
13.5k
    }
845
846
61.4k
    int rule;
847
61.4k
    if (in_group) {
848
3.09k
      if (ch == ']') {
849
1.07k
        if (*tail_p) {
850
878
          (*tail_p)->in_group = false;
851
878
        }
852
1.07k
        in_group = false;
853
1.07k
        l++;
854
1.07k
        continue;
855
1.07k
      }
856
857
2.02k
      if (ch == '|') {
858
789
        rule = CIPHER_ADD;
859
789
        l++;
860
789
        continue;
861
1.23k
      } else if (!OPENSSL_isalnum(ch)) {
862
218
        OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
863
218
        return false;
864
1.01k
      } else {
865
1.01k
        rule = CIPHER_ADD;
866
1.01k
      }
867
58.3k
    } else if (ch == '-') {
868
1.39k
      rule = CIPHER_DEL;
869
1.39k
      l++;
870
56.9k
    } else if (ch == '+') {
871
1.03k
      rule = CIPHER_ORD;
872
1.03k
      l++;
873
55.9k
    } else if (ch == '!') {
874
3.82k
      rule = CIPHER_KILL;
875
3.82k
      l++;
876
52.0k
    } else if (ch == '@') {
877
7.47k
      rule = CIPHER_SPECIAL;
878
7.47k
      l++;
879
44.6k
    } else if (ch == '[') {
880
1.96k
      assert(!in_group);
881
1.96k
      in_group = true;
882
1.96k
      has_group = true;
883
1.96k
      l++;
884
1.96k
      continue;
885
42.6k
    } else {
886
42.6k
      rule = CIPHER_ADD;
887
42.6k
    }
888
889
    // If preference groups are enabled, the only legal operator is +.
890
    // Otherwise the in_group bits will get mixed up.
891
57.3k
    if (has_group && rule != CIPHER_ADD) {
892
195
      OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
893
195
      return false;
894
195
    }
895
896
57.1k
    if (is_cipher_list_separator(ch, strict)) {
897
12.1k
      l++;
898
12.1k
      continue;
899
12.1k
    }
900
901
45.0k
    bool multi = false;
902
45.0k
    uint16_t cipher_id = 0;
903
45.0k
    CIPHER_ALIAS alias;
904
45.0k
    bool skip_rule = false;
905
906
    // When adding, exclude deprecated ciphers by default.
907
45.0k
    alias.include_deprecated = rule != CIPHER_ADD;
908
909
47.1k
    for (;;) {
910
47.1k
      ch = *l;
911
47.1k
      buf = l;
912
47.1k
      buf_len = 0;
913
212k
      while (OPENSSL_isalnum(ch) || ch == '-' || ch == '.' || ch == '_') {
914
165k
        ch = *(++l);
915
165k
        buf_len++;
916
165k
      }
917
918
47.1k
      if (buf_len == 0) {
919
        // We hit something we cannot deal with, it is no command or separator
920
        // nor alphanumeric, so we call this an error.
921
8.55k
        OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
922
8.55k
        return false;
923
8.55k
      }
924
925
38.6k
      if (rule == CIPHER_SPECIAL) {
926
6.58k
        break;
927
6.58k
      }
928
929
      // Look for a matching exact cipher. These aren't allowed in multipart
930
      // rules.
931
32.0k
      if (!multi && ch != '+') {
932
702k
        for (const SSL_CIPHER &cipher : kCiphers) {
933
702k
          if (rule_equals(cipher.name, buf, buf_len) ||
934
702k
              rule_equals(cipher.standard_name, buf, buf_len)) {
935
447
            cipher_id = cipher.protocol_id;
936
447
            break;
937
447
          }
938
702k
        }
939
28.5k
      }
940
32.0k
      if (cipher_id == 0) {
941
        // If not an exact cipher, look for a matching cipher alias.
942
494k
        for (j = 0; j < kCipherAliasesLen; j++) {
943
483k
          if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
944
21.3k
            alias.algorithm_mkey &= kCipherAliases[j].algorithm_mkey;
945
21.3k
            alias.algorithm_auth &= kCipherAliases[j].algorithm_auth;
946
21.3k
            alias.algorithm_enc &= kCipherAliases[j].algorithm_enc;
947
21.3k
            alias.algorithm_mac &= kCipherAliases[j].algorithm_mac;
948
949
            // When specifying a combination of aliases, if any aliases
950
            // enables deprecated ciphers, deprecated ciphers are included. This
951
            // is slightly different from the bitmasks in that adding aliases
952
            // can increase the set of matched ciphers. This is so that an alias
953
            // like "RSA" will only specify AES-based RSA ciphers, but
954
            // "RSA+3DES" will still specify 3DES.
955
21.3k
            alias.include_deprecated |= kCipherAliases[j].include_deprecated;
956
957
21.3k
            if (alias.min_version != 0 &&
958
389
                alias.min_version != kCipherAliases[j].min_version) {
959
195
              skip_rule = true;
960
21.1k
            } else {
961
21.1k
              alias.min_version = kCipherAliases[j].min_version;
962
21.1k
            }
963
21.3k
            break;
964
21.3k
          }
965
483k
        }
966
31.6k
        if (j == kCipherAliasesLen) {
967
10.2k
          skip_rule = true;
968
10.2k
          if (strict) {
969
1.69k
            OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
970
1.69k
            return false;
971
1.69k
          }
972
10.2k
        }
973
31.6k
      }
974
975
      // Check for a multipart rule.
976
30.3k
      if (ch != '+') {
977
28.1k
        break;
978
28.1k
      }
979
2.17k
      l++;
980
2.17k
      multi = true;
981
2.17k
    }
982
983
    // Ok, we have the rule, now apply it.
984
34.7k
    if (rule == CIPHER_SPECIAL) {
985
6.58k
      if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) {
986
1.06k
        OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
987
1.06k
        return false;
988
1.06k
      }
989
5.51k
      if (!ssl_cipher_strength_sort(head_p, tail_p)) {
990
0
        return false;
991
0
      }
992
993
      // We do not support any "multi" options together with "@", so throw away
994
      // the rest of the command, if any left, until end or ':' is found.
995
7.58k
      while (*l != '\0' && !is_cipher_list_separator(*l, strict)) {
996
2.06k
        l++;
997
2.06k
      }
998
28.1k
    } else if (!skip_rule) {
999
20.5k
      ssl_cipher_apply_rule(cipher_id, &alias, rule, -1, in_group, head_p,
1000
20.5k
                            tail_p);
1001
20.5k
    }
1002
34.7k
  }
1003
1004
13.5k
  if (in_group) {
1005
669
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1006
669
    return false;
1007
669
  }
1008
1009
12.8k
  return true;
1010
13.5k
}
1011
1012
bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
1013
                            const bool has_aes_hw, const char *rule_str,
1014
30.6k
                            bool strict) {
1015
  // Return with error if nothing to do.
1016
30.6k
  if (rule_str == nullptr || out_cipher_list == nullptr) {
1017
0
    return false;
1018
0
  }
1019
1020
  // We prefer ECDHE ciphers over non-PFS ciphers. Then we prefer AEAD over
1021
  // non-AEAD.
1022
30.6k
  static const uint16_t kAESCiphers[] = {
1023
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
1024
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1025
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
1026
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
1027
30.6k
  };
1028
30.6k
  static const uint16_t kChaChaCiphers[] = {
1029
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
1030
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
1031
30.6k
      SSL_CIPHER_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
1032
30.6k
  };
1033
30.6k
  static const uint16_t kLegacyCiphers[] = {
1034
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
1035
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA,
1036
30.6k
      SSL_CIPHER_ECDHE_PSK_WITH_AES_128_CBC_SHA,
1037
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
1038
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_AES_256_CBC_SHA,
1039
30.6k
      SSL_CIPHER_ECDHE_PSK_WITH_AES_256_CBC_SHA,
1040
30.6k
      SSL_CIPHER_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
1041
30.6k
      SSL_CIPHER_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
1042
30.6k
      SSL_CIPHER_RSA_WITH_AES_128_GCM_SHA256,
1043
30.6k
      SSL_CIPHER_RSA_WITH_AES_256_GCM_SHA384,
1044
30.6k
      SSL_CIPHER_RSA_WITH_AES_128_CBC_SHA,
1045
30.6k
      SSL_CIPHER_PSK_WITH_AES_128_CBC_SHA,
1046
30.6k
      SSL_CIPHER_RSA_WITH_AES_256_CBC_SHA,
1047
30.6k
      SSL_CIPHER_PSK_WITH_AES_256_CBC_SHA,
1048
30.6k
      SSL_CIPHER_RSA_WITH_3DES_EDE_CBC_SHA,
1049
30.6k
  };
1050
1051
  // Set up a linked list of ciphers.
1052
30.6k
  CIPHER_ORDER co_list[std::size(kAESCiphers) + std::size(kChaChaCiphers) +
1053
30.6k
                       std::size(kLegacyCiphers)];
1054
705k
  for (size_t i = 0; i < std::size(co_list); i++) {
1055
674k
    co_list[i].next = i + 1 < std::size(co_list) ? &co_list[i + 1] : nullptr;
1056
674k
    co_list[i].prev = i == 0 ? nullptr : &co_list[i - 1];
1057
674k
    co_list[i].active = false;
1058
674k
    co_list[i].in_group = false;
1059
674k
  }
1060
30.6k
  CIPHER_ORDER *head = &co_list[0];
1061
30.6k
  CIPHER_ORDER *tail = &co_list[std::size(co_list) - 1];
1062
1063
  // Order AES ciphers vs ChaCha ciphers based on whether we have AES hardware.
1064
  //
1065
  // TODO(crbug.com/boringssl/29): We should also set up equipreference groups
1066
  // as a server.
1067
30.6k
  size_t num = 0;
1068
30.6k
  if (has_aes_hw) {
1069
122k
    for (uint16_t id : kAESCiphers) {
1070
122k
      co_list[num++].cipher = SSL_get_cipher_by_value(id);
1071
122k
      assert(co_list[num - 1].cipher != nullptr);
1072
122k
    }
1073
30.6k
  }
1074
91.9k
  for (uint16_t id : kChaChaCiphers) {
1075
91.9k
    co_list[num++].cipher = SSL_get_cipher_by_value(id);
1076
91.9k
    assert(co_list[num - 1].cipher != nullptr);
1077
91.9k
  }
1078
30.6k
  if (!has_aes_hw) {
1079
0
    for (uint16_t id : kAESCiphers) {
1080
0
      co_list[num++].cipher = SSL_get_cipher_by_value(id);
1081
0
      assert(co_list[num - 1].cipher != nullptr);
1082
0
    }
1083
0
  }
1084
459k
  for (uint16_t id : kLegacyCiphers) {
1085
459k
    co_list[num++].cipher = SSL_get_cipher_by_value(id);
1086
459k
    assert(co_list[num - 1].cipher != nullptr);
1087
459k
  }
1088
30.6k
  assert(num == std::size(co_list));
1089
30.6k
  static_assert(std::size(co_list) + NumTLS13Ciphers() == std::size(kCiphers),
1090
30.6k
                "Not all ciphers are included in the cipher order");
1091
1092
  // If the rule_string begins with DEFAULT, apply the default rule before
1093
  // using the (possibly available) additional rules.
1094
30.6k
  const char *rule_p = rule_str;
1095
30.6k
  if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1096
1.02k
    if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1097
1.02k
                                    strict)) {
1098
0
      return false;
1099
0
    }
1100
1.02k
    rule_p += 7;
1101
1.02k
    if (*rule_p == ':') {
1102
211
      rule_p++;
1103
211
    }
1104
1.02k
  }
1105
1106
30.6k
  if (*rule_p != '\0' &&
1107
24.2k
      !ssl_cipher_process_rulestr(rule_p, &head, &tail, strict)) {
1108
12.3k
    return false;
1109
12.3k
  }
1110
1111
  // Allocate new "cipherstack" for the result, return with error
1112
  // if we cannot get one.
1113
18.2k
  UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null());
1114
18.2k
  Array<bool> in_group_flags;
1115
18.2k
  if (cipherstack == nullptr ||
1116
18.2k
      !in_group_flags.InitForOverwrite(std::size(kCiphers))) {
1117
0
    return false;
1118
0
  }
1119
1120
  // The cipher selection for the list is done. The ciphers are added
1121
  // to the resulting precedence to the STACK_OF(SSL_CIPHER).
1122
18.2k
  size_t num_in_group_flags = 0;
1123
398k
  for (CIPHER_ORDER *curr = head; curr != nullptr; curr = curr->next) {
1124
380k
    if (curr->active) {
1125
142k
      if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) {
1126
0
        return false;
1127
0
      }
1128
142k
      in_group_flags[num_in_group_flags++] = curr->in_group;
1129
142k
    }
1130
380k
  }
1131
18.2k
  in_group_flags.Shrink(num_in_group_flags);
1132
1133
18.2k
  UniquePtr<SSLCipherPreferenceList> pref_list =
1134
18.2k
      MakeUnique<SSLCipherPreferenceList>();
1135
18.2k
  if (!pref_list || !pref_list->Init(std::move(cipherstack), in_group_flags)) {
1136
0
    return false;
1137
0
  }
1138
1139
18.2k
  *out_cipher_list = std::move(pref_list);
1140
1141
  // Configuring an empty cipher list is an error but still updates the
1142
  // output.
1143
18.2k
  if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers.get()) == 0) {
1144
8.99k
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1145
8.99k
    return false;
1146
8.99k
  }
1147
1148
9.27k
  return true;
1149
18.2k
}
1150
1151
48.4k
uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key, bool sign_ok) {
1152
48.4k
  switch (EVP_PKEY_id(key)) {
1153
30.6k
    case EVP_PKEY_RSA:
1154
30.6k
      return sign_ok ? (SSL_aRSA_SIGN | SSL_aRSA_DECRYPT) : SSL_aRSA_DECRYPT;
1155
17.7k
    case EVP_PKEY_EC:
1156
17.7k
    case EVP_PKEY_ED25519:
1157
      // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers.
1158
17.7k
      return sign_ok ? SSL_aECDSA : 0;
1159
0
    default:
1160
0
      return 0;
1161
48.4k
  }
1162
48.4k
}
1163
1164
383k
bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
1165
383k
  return (cipher->algorithm_auth & SSL_aCERT) != 0;
1166
383k
}
1167
1168
18.0k
bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1169
  // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is
1170
  // optional or omitted in all others.
1171
18.0k
  return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
1172
18.0k
}
1173
1174
0
size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1175
0
  size_t block_size;
1176
0
  switch (cipher->algorithm_enc) {
1177
0
    case SSL_3DES:
1178
0
      block_size = 8;
1179
0
      break;
1180
0
    case SSL_AES128:
1181
0
    case SSL_AES256:
1182
0
      block_size = 16;
1183
0
      break;
1184
0
    default:
1185
0
      return 0;
1186
0
  }
1187
1188
  // All supported TLS 1.0 ciphers use SHA-1.
1189
0
  assert(cipher->algorithm_mac == SSL_SHA1);
1190
0
  size_t ret = 1 + SHA_DIGEST_LENGTH;
1191
0
  ret += block_size - (ret % block_size);
1192
0
  return ret;
1193
0
}
1194
1195
BSSL_NAMESPACE_END
1196
1197
using namespace bssl;
1198
1199
static constexpr int ssl_cipher_id_cmp(const SSL_CIPHER *a,
1200
4.22M
                                       const SSL_CIPHER *b) {
1201
4.22M
  if (a->protocol_id > b->protocol_id) {
1202
1.38M
    return 1;
1203
1.38M
  }
1204
2.83M
  if (a->protocol_id < b->protocol_id) {
1205
1.89M
    return -1;
1206
1.89M
  }
1207
940k
  return 0;
1208
2.83M
}
1209
1210
4.22M
static int ssl_cipher_id_cmp_void(const void *in_a, const void *in_b) {
1211
4.22M
  return ssl_cipher_id_cmp(reinterpret_cast<const SSL_CIPHER *>(in_a),
1212
4.22M
                           reinterpret_cast<const SSL_CIPHER *>(in_b));
1213
4.22M
}
1214
1215
template <size_t N>
1216
0
static constexpr bool ssl_ciphers_sorted(const SSL_CIPHER (&ciphers)[N]) {
1217
0
  for (size_t i = 1; i < N; i++) {
1218
0
    if (ssl_cipher_id_cmp(&ciphers[i - 1], &ciphers[i]) >= 0) {
1219
0
      return false;
1220
0
    }
1221
0
  }
1222
0
  return true;
1223
0
}
1224
1225
static_assert(ssl_ciphers_sorted(kCiphers),
1226
              "Ciphers are not sorted, bsearch won't work");
1227
1228
1.04M
const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
1229
1.04M
  SSL_CIPHER c;
1230
1.04M
  c.protocol_id = value;
1231
1.04M
  return reinterpret_cast<const SSL_CIPHER *>(
1232
1.04M
      bsearch(&c, kCiphers, std::size(kCiphers), sizeof(SSL_CIPHER),
1233
1.04M
              ssl_cipher_id_cmp_void));
1234
1.04M
}
1235
1236
0
uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) {
1237
  // Historically, OpenSSL added a leading 0x03 byte to cipher IDs, to
1238
  // distinguish between SSL 2.0 and SSL 3.0.
1239
0
  return cipher->protocol_id | 0x03000000;
1240
0
}
1241
1242
784k
uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *cipher) {
1243
784k
  return cipher->protocol_id;
1244
784k
}
1245
1246
0
int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) {
1247
0
  return (cipher->algorithm_mac & SSL_AEAD) != 0;
1248
0
}
1249
1250
0
int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) {
1251
0
  switch (cipher->algorithm_enc) {
1252
0
    case SSL_3DES:
1253
0
      return NID_des_ede3_cbc;
1254
0
    case SSL_AES128:
1255
0
      return NID_aes_128_cbc;
1256
0
    case SSL_AES256:
1257
0
      return NID_aes_256_cbc;
1258
0
    case SSL_AES128GCM:
1259
0
      return NID_aes_128_gcm;
1260
0
    case SSL_AES256GCM:
1261
0
      return NID_aes_256_gcm;
1262
0
    case SSL_CHACHA20POLY1305:
1263
0
      return NID_chacha20_poly1305;
1264
0
  }
1265
0
  assert(0);
1266
0
  return NID_undef;
1267
0
}
1268
1269
0
int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) {
1270
0
  switch (cipher->algorithm_mac) {
1271
0
    case SSL_AEAD:
1272
0
      return NID_undef;
1273
0
    case SSL_SHA1:
1274
0
      return NID_sha1;
1275
0
    case SSL_SHA256:
1276
0
      return NID_sha256;
1277
0
  }
1278
0
  assert(0);
1279
0
  return NID_undef;
1280
0
}
1281
1282
0
int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) {
1283
0
  switch (cipher->algorithm_mkey) {
1284
0
    case SSL_kRSA:
1285
0
      return NID_kx_rsa;
1286
0
    case SSL_kECDHE:
1287
0
      return NID_kx_ecdhe;
1288
0
    case SSL_kPSK:
1289
0
      return NID_kx_psk;
1290
0
    case SSL_kGENERIC:
1291
0
      return NID_kx_any;
1292
0
  }
1293
0
  assert(0);
1294
0
  return NID_undef;
1295
0
}
1296
1297
0
int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) {
1298
0
  switch (cipher->algorithm_auth) {
1299
0
    case SSL_aRSA_DECRYPT:
1300
0
    case SSL_aRSA_SIGN:
1301
0
      return NID_auth_rsa;
1302
0
    case SSL_aECDSA:
1303
0
      return NID_auth_ecdsa;
1304
0
    case SSL_aPSK:
1305
0
      return NID_auth_psk;
1306
0
    case SSL_aGENERIC:
1307
0
      return NID_auth_any;
1308
0
  }
1309
0
  assert(0);
1310
0
  return NID_undef;
1311
0
}
1312
1313
590
const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *cipher) {
1314
590
  switch (cipher->algorithm_prf) {
1315
0
    case SSL_HANDSHAKE_MAC_DEFAULT:
1316
0
      return EVP_md5_sha1();
1317
573
    case SSL_HANDSHAKE_MAC_SHA256:
1318
573
      return EVP_sha256();
1319
17
    case SSL_HANDSHAKE_MAC_SHA384:
1320
17
      return EVP_sha384();
1321
590
  }
1322
590
  assert(0);
1323
0
  return nullptr;
1324
0
}
1325
1326
0
int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) {
1327
0
  const EVP_MD *md = SSL_CIPHER_get_handshake_digest(cipher);
1328
0
  if (md == nullptr) {
1329
0
    return NID_undef;
1330
0
  }
1331
0
  return EVP_MD_nid(md);
1332
0
}
1333
1334
0
int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1335
0
  return cipher->algorithm_mac != SSL_AEAD;
1336
0
}
1337
1338
920k
uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
1339
920k
  if (cipher->algorithm_mkey == SSL_kGENERIC ||
1340
899k
      cipher->algorithm_auth == SSL_aGENERIC) {
1341
21.1k
    return TLS1_3_VERSION;
1342
21.1k
  }
1343
1344
899k
  if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1345
    // Cipher suites before TLS 1.2 use the default PRF, while all those added
1346
    // afterwards specify a particular hash.
1347
487k
    return TLS1_2_VERSION;
1348
487k
  }
1349
411k
  return SSL3_VERSION;
1350
899k
}
1351
1352
868k
uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
1353
868k
  if (cipher->algorithm_mkey == SSL_kGENERIC ||
1354
856k
      cipher->algorithm_auth == SSL_aGENERIC) {
1355
11.8k
    return TLS1_3_VERSION;
1356
11.8k
  }
1357
856k
  return TLS1_2_VERSION;
1358
868k
}
1359
1360
static const char *const kUnknownCipher = "(NONE)";
1361
1362
// return the actual cipher being used
1363
0
const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1364
0
  if (cipher != nullptr) {
1365
0
    return cipher->name;
1366
0
  }
1367
1368
0
  return kUnknownCipher;
1369
0
}
1370
1371
0
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
1372
0
  return cipher->standard_name;
1373
0
}
1374
1375
0
const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1376
0
  if (cipher == nullptr) {
1377
0
    return "";
1378
0
  }
1379
1380
0
  switch (cipher->algorithm_mkey) {
1381
0
    case SSL_kRSA:
1382
0
      return "RSA";
1383
1384
0
    case SSL_kECDHE:
1385
0
      switch (cipher->algorithm_auth) {
1386
0
        case SSL_aECDSA:
1387
0
          return "ECDHE_ECDSA";
1388
0
        case SSL_aRSA_SIGN:
1389
0
          return "ECDHE_RSA";
1390
0
        case SSL_aPSK:
1391
0
          return "ECDHE_PSK";
1392
0
        default:
1393
0
          assert(0);
1394
0
          return "UNKNOWN";
1395
0
      }
1396
1397
0
    case SSL_kPSK:
1398
0
      assert(cipher->algorithm_auth == SSL_aPSK);
1399
0
      return "PSK";
1400
1401
0
    case SSL_kGENERIC:
1402
0
      assert(cipher->algorithm_auth == SSL_aGENERIC);
1403
0
      return "GENERIC";
1404
1405
0
    default:
1406
0
      assert(0);
1407
0
      return "UNKNOWN";
1408
0
  }
1409
0
}
1410
1411
217k
int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1412
217k
  if (cipher == nullptr) {
1413
0
    return 0;
1414
0
  }
1415
1416
217k
  int alg_bits, strength_bits;
1417
217k
  switch (cipher->algorithm_enc) {
1418
63.1k
    case SSL_AES128:
1419
93.5k
    case SSL_AES128GCM:
1420
93.5k
      alg_bits = 128;
1421
93.5k
      strength_bits = 128;
1422
93.5k
      break;
1423
1424
50.5k
    case SSL_AES256:
1425
81.1k
    case SSL_AES256GCM:
1426
113k
    case SSL_CHACHA20POLY1305:
1427
113k
      alg_bits = 256;
1428
113k
      strength_bits = 256;
1429
113k
      break;
1430
1431
11.2k
    case SSL_3DES:
1432
11.2k
      alg_bits = 168;
1433
11.2k
      strength_bits = 112;
1434
11.2k
      break;
1435
1436
0
    default:
1437
0
      assert(0);
1438
0
      alg_bits = 0;
1439
0
      strength_bits = 0;
1440
217k
  }
1441
1442
217k
  if (out_alg_bits != nullptr) {
1443
0
    *out_alg_bits = alg_bits;
1444
0
  }
1445
217k
  return strength_bits;
1446
217k
}
1447
1448
const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1449
0
                                   int len) {
1450
0
  const char *kx, *au, *enc, *mac;
1451
0
  uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1452
1453
0
  alg_mkey = cipher->algorithm_mkey;
1454
0
  alg_auth = cipher->algorithm_auth;
1455
0
  alg_enc = cipher->algorithm_enc;
1456
0
  alg_mac = cipher->algorithm_mac;
1457
1458
0
  switch (alg_mkey) {
1459
0
    case SSL_kRSA:
1460
0
      kx = "RSA";
1461
0
      break;
1462
1463
0
    case SSL_kECDHE:
1464
0
      kx = "ECDH";
1465
0
      break;
1466
1467
0
    case SSL_kPSK:
1468
0
      kx = "PSK";
1469
0
      break;
1470
1471
0
    case SSL_kGENERIC:
1472
0
      kx = "GENERIC";
1473
0
      break;
1474
1475
0
    default:
1476
0
      kx = "unknown";
1477
0
  }
1478
1479
0
  switch (alg_auth) {
1480
0
    case SSL_aRSA_DECRYPT:
1481
0
    case SSL_aRSA_SIGN:
1482
0
      au = "RSA";
1483
0
      break;
1484
1485
0
    case SSL_aECDSA:
1486
0
      au = "ECDSA";
1487
0
      break;
1488
1489
0
    case SSL_aPSK:
1490
0
      au = "PSK";
1491
0
      break;
1492
1493
0
    case SSL_aGENERIC:
1494
0
      au = "GENERIC";
1495
0
      break;
1496
1497
0
    default:
1498
0
      au = "unknown";
1499
0
      break;
1500
0
  }
1501
1502
0
  switch (alg_enc) {
1503
0
    case SSL_3DES:
1504
0
      enc = "3DES(168)";
1505
0
      break;
1506
1507
0
    case SSL_AES128:
1508
0
      enc = "AES(128)";
1509
0
      break;
1510
1511
0
    case SSL_AES256:
1512
0
      enc = "AES(256)";
1513
0
      break;
1514
1515
0
    case SSL_AES128GCM:
1516
0
      enc = "AESGCM(128)";
1517
0
      break;
1518
1519
0
    case SSL_AES256GCM:
1520
0
      enc = "AESGCM(256)";
1521
0
      break;
1522
1523
0
    case SSL_CHACHA20POLY1305:
1524
0
      enc = "ChaCha20-Poly1305";
1525
0
      break;
1526
1527
0
    default:
1528
0
      enc = "unknown";
1529
0
      break;
1530
0
  }
1531
1532
0
  switch (alg_mac) {
1533
0
    case SSL_SHA1:
1534
0
      mac = "SHA1";
1535
0
      break;
1536
1537
0
    case SSL_SHA256:
1538
0
      mac = "SHA256";
1539
0
      break;
1540
1541
0
    case SSL_AEAD:
1542
0
      mac = "AEAD";
1543
0
      break;
1544
1545
0
    default:
1546
0
      mac = "unknown";
1547
0
      break;
1548
0
  }
1549
1550
0
  if (buf == nullptr) {
1551
0
    len = 128;
1552
0
    buf = (char *)OPENSSL_malloc(len);
1553
0
    if (buf == nullptr) {
1554
0
      return nullptr;
1555
0
    }
1556
0
  } else if (len < 128) {
1557
0
    return "Buffer too small";
1558
0
  }
1559
1560
0
  snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n", cipher->name,
1561
0
           kx, au, enc, mac);
1562
0
  return buf;
1563
0
}
1564
1565
0
const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1566
0
  return "TLSv1/SSLv3";
1567
0
}
1568
1569
0
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods() { return nullptr; }
1570
1571
0
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
1572
1573
0
const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return nullptr; }
1574
1575
0
const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; }
1576
1577
0
int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; }
1578
1579
0
void SSL_COMP_free_compression_methods() {}
1580
1581
0
size_t SSL_get_all_cipher_names(const char **out, size_t max_out) {
1582
0
  return GetAllNames(out, max_out, Span(&kUnknownCipher, 1), &SSL_CIPHER::name,
1583
0
                     Span(kCiphers));
1584
0
}
1585
1586
0
size_t SSL_get_all_standard_cipher_names(const char **out, size_t max_out) {
1587
0
  return GetAllNames(out, max_out, Span<const char *const>(),
1588
0
                     &SSL_CIPHER::standard_name, Span(kCiphers));
1589
0
}