Coverage Report

Created: 2023-06-29 07:23

/src/boringssl/ssl/ssl_cipher.cc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.]
56
 */
57
/* ====================================================================
58
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
59
 *
60
 * Redistribution and use in source and binary forms, with or without
61
 * modification, are permitted provided that the following conditions
62
 * are met:
63
 *
64
 * 1. Redistributions of source code must retain the above copyright
65
 *    notice, this list of conditions and the following disclaimer.
66
 *
67
 * 2. Redistributions in binary form must reproduce the above copyright
68
 *    notice, this list of conditions and the following disclaimer in
69
 *    the documentation and/or other materials provided with the
70
 *    distribution.
71
 *
72
 * 3. All advertising materials mentioning features or use of this
73
 *    software must display the following acknowledgment:
74
 *    "This product includes software developed by the OpenSSL Project
75
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
 *
77
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
 *    endorse or promote products derived from this software without
79
 *    prior written permission. For written permission, please contact
80
 *    openssl-core@openssl.org.
81
 *
82
 * 5. Products derived from this software may not be called "OpenSSL"
83
 *    nor may "OpenSSL" appear in their names without prior written
84
 *    permission of the OpenSSL Project.
85
 *
86
 * 6. Redistributions of any form whatsoever must retain the following
87
 *    acknowledgment:
88
 *    "This product includes software developed by the OpenSSL Project
89
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
 *
91
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
 * OF THE POSSIBILITY OF SUCH DAMAGE.
103
 * ====================================================================
104
 *
105
 * This product includes cryptographic software written by Eric Young
106
 * (eay@cryptsoft.com).  This product includes software written by Tim
107
 * Hudson (tjh@cryptsoft.com).
108
 *
109
 */
110
/* ====================================================================
111
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112
 * ECC cipher suite support in OpenSSL originally developed by
113
 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114
 */
115
/* ====================================================================
116
 * Copyright 2005 Nokia. All rights reserved.
117
 *
118
 * The portions of the attached software ("Contribution") is developed by
119
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120
 * license.
121
 *
122
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124
 * support (see RFC 4279) to OpenSSL.
125
 *
126
 * No patent licenses or other rights except those expressly stated in
127
 * the OpenSSL open source license shall be deemed granted or received
128
 * expressly, by implication, estoppel, or otherwise.
129
 *
130
 * No assurances are provided by Nokia that the Contribution does not
131
 * infringe the patent or other intellectual property rights of any third
132
 * party or that the license provides you with all the necessary rights
133
 * to make use of the Contribution.
134
 *
135
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139
 * OTHERWISE. */
140
141
#include <openssl/ssl.h>
142
143
#include <assert.h>
144
#include <string.h>
145
146
#include <openssl/err.h>
147
#include <openssl/md5.h>
148
#include <openssl/mem.h>
149
#include <openssl/sha.h>
150
#include <openssl/stack.h>
151
152
#include "internal.h"
153
#include "../crypto/internal.h"
154
155
156
BSSL_NAMESPACE_BEGIN
157
158
static constexpr SSL_CIPHER kCiphers[] = {
159
    // The RSA ciphers
160
161
    // Cipher 0A
162
    {
163
     SSL3_TXT_RSA_DES_192_CBC3_SHA,
164
     "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
165
     SSL3_CK_RSA_DES_192_CBC3_SHA,
166
     SSL_kRSA,
167
     SSL_aRSA,
168
     SSL_3DES,
169
     SSL_SHA1,
170
     SSL_HANDSHAKE_MAC_DEFAULT,
171
    },
172
173
174
    // New AES ciphersuites
175
176
    // Cipher 2F
177
    {
178
     TLS1_TXT_RSA_WITH_AES_128_SHA,
179
     "TLS_RSA_WITH_AES_128_CBC_SHA",
180
     TLS1_CK_RSA_WITH_AES_128_SHA,
181
     SSL_kRSA,
182
     SSL_aRSA,
183
     SSL_AES128,
184
     SSL_SHA1,
185
     SSL_HANDSHAKE_MAC_DEFAULT,
186
    },
187
188
    // Cipher 35
189
    {
190
     TLS1_TXT_RSA_WITH_AES_256_SHA,
191
     "TLS_RSA_WITH_AES_256_CBC_SHA",
192
     TLS1_CK_RSA_WITH_AES_256_SHA,
193
     SSL_kRSA,
194
     SSL_aRSA,
195
     SSL_AES256,
196
     SSL_SHA1,
197
     SSL_HANDSHAKE_MAC_DEFAULT,
198
    },
199
200
    // PSK cipher suites.
201
202
    // Cipher 8C
203
    {
204
     TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
205
     "TLS_PSK_WITH_AES_128_CBC_SHA",
206
     TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
207
     SSL_kPSK,
208
     SSL_aPSK,
209
     SSL_AES128,
210
     SSL_SHA1,
211
     SSL_HANDSHAKE_MAC_DEFAULT,
212
    },
213
214
    // Cipher 8D
215
    {
216
     TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
217
     "TLS_PSK_WITH_AES_256_CBC_SHA",
218
     TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
219
     SSL_kPSK,
220
     SSL_aPSK,
221
     SSL_AES256,
222
     SSL_SHA1,
223
     SSL_HANDSHAKE_MAC_DEFAULT,
224
    },
225
226
    // GCM ciphersuites from RFC 5288
227
228
    // Cipher 9C
229
    {
230
     TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
231
     "TLS_RSA_WITH_AES_128_GCM_SHA256",
232
     TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
233
     SSL_kRSA,
234
     SSL_aRSA,
235
     SSL_AES128GCM,
236
     SSL_AEAD,
237
     SSL_HANDSHAKE_MAC_SHA256,
238
    },
239
240
    // Cipher 9D
241
    {
242
     TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
243
     "TLS_RSA_WITH_AES_256_GCM_SHA384",
244
     TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
245
     SSL_kRSA,
246
     SSL_aRSA,
247
     SSL_AES256GCM,
248
     SSL_AEAD,
249
     SSL_HANDSHAKE_MAC_SHA384,
250
    },
251
252
    // TLS 1.3 suites.
253
254
    // Cipher 1301
255
    {
256
      TLS1_3_RFC_AES_128_GCM_SHA256,
257
      "TLS_AES_128_GCM_SHA256",
258
      TLS1_3_CK_AES_128_GCM_SHA256,
259
      SSL_kGENERIC,
260
      SSL_aGENERIC,
261
      SSL_AES128GCM,
262
      SSL_AEAD,
263
      SSL_HANDSHAKE_MAC_SHA256,
264
    },
265
266
    // Cipher 1302
267
    {
268
      TLS1_3_RFC_AES_256_GCM_SHA384,
269
      "TLS_AES_256_GCM_SHA384",
270
      TLS1_3_CK_AES_256_GCM_SHA384,
271
      SSL_kGENERIC,
272
      SSL_aGENERIC,
273
      SSL_AES256GCM,
274
      SSL_AEAD,
275
      SSL_HANDSHAKE_MAC_SHA384,
276
    },
277
278
    // Cipher 1303
279
    {
280
      TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
281
      "TLS_CHACHA20_POLY1305_SHA256",
282
      TLS1_3_CK_CHACHA20_POLY1305_SHA256,
283
      SSL_kGENERIC,
284
      SSL_aGENERIC,
285
      SSL_CHACHA20POLY1305,
286
      SSL_AEAD,
287
      SSL_HANDSHAKE_MAC_SHA256,
288
    },
289
290
    // Cipher C009
291
    {
292
     TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
293
     "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
294
     TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
295
     SSL_kECDHE,
296
     SSL_aECDSA,
297
     SSL_AES128,
298
     SSL_SHA1,
299
     SSL_HANDSHAKE_MAC_DEFAULT,
300
    },
301
302
    // Cipher C00A
303
    {
304
     TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
305
     "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
306
     TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
307
     SSL_kECDHE,
308
     SSL_aECDSA,
309
     SSL_AES256,
310
     SSL_SHA1,
311
     SSL_HANDSHAKE_MAC_DEFAULT,
312
    },
313
314
    // Cipher C013
315
    {
316
     TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
317
     "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
318
     TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
319
     SSL_kECDHE,
320
     SSL_aRSA,
321
     SSL_AES128,
322
     SSL_SHA1,
323
     SSL_HANDSHAKE_MAC_DEFAULT,
324
    },
325
326
    // Cipher C014
327
    {
328
     TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
329
     "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
330
     TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
331
     SSL_kECDHE,
332
     SSL_aRSA,
333
     SSL_AES256,
334
     SSL_SHA1,
335
     SSL_HANDSHAKE_MAC_DEFAULT,
336
    },
337
338
    // Cipher C027
339
    {
340
     TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
341
     "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
342
     TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
343
     SSL_kECDHE,
344
     SSL_aRSA,
345
     SSL_AES128,
346
     SSL_SHA256,
347
     SSL_HANDSHAKE_MAC_SHA256,
348
    },
349
350
    // GCM based TLS v1.2 ciphersuites from RFC 5289
351
352
    // Cipher C02B
353
    {
354
     TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
355
     "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
356
     TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
357
     SSL_kECDHE,
358
     SSL_aECDSA,
359
     SSL_AES128GCM,
360
     SSL_AEAD,
361
     SSL_HANDSHAKE_MAC_SHA256,
362
    },
363
364
    // Cipher C02C
365
    {
366
     TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
367
     "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
368
     TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
369
     SSL_kECDHE,
370
     SSL_aECDSA,
371
     SSL_AES256GCM,
372
     SSL_AEAD,
373
     SSL_HANDSHAKE_MAC_SHA384,
374
    },
375
376
    // Cipher C02F
377
    {
378
     TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
379
     "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
380
     TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
381
     SSL_kECDHE,
382
     SSL_aRSA,
383
     SSL_AES128GCM,
384
     SSL_AEAD,
385
     SSL_HANDSHAKE_MAC_SHA256,
386
    },
387
388
    // Cipher C030
389
    {
390
     TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
391
     "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
392
     TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
393
     SSL_kECDHE,
394
     SSL_aRSA,
395
     SSL_AES256GCM,
396
     SSL_AEAD,
397
     SSL_HANDSHAKE_MAC_SHA384,
398
    },
399
400
    // ECDHE-PSK cipher suites.
401
402
    // Cipher C035
403
    {
404
     TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
405
     "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
406
     TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
407
     SSL_kECDHE,
408
     SSL_aPSK,
409
     SSL_AES128,
410
     SSL_SHA1,
411
     SSL_HANDSHAKE_MAC_DEFAULT,
412
    },
413
414
    // Cipher C036
415
    {
416
     TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
417
     "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
418
     TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
419
     SSL_kECDHE,
420
     SSL_aPSK,
421
     SSL_AES256,
422
     SSL_SHA1,
423
     SSL_HANDSHAKE_MAC_DEFAULT,
424
    },
425
426
    // ChaCha20-Poly1305 cipher suites.
427
428
    // Cipher CCA8
429
    {
430
     TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
431
     "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
432
     TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
433
     SSL_kECDHE,
434
     SSL_aRSA,
435
     SSL_CHACHA20POLY1305,
436
     SSL_AEAD,
437
     SSL_HANDSHAKE_MAC_SHA256,
438
    },
439
440
    // Cipher CCA9
441
    {
442
     TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
443
     "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
444
     TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
445
     SSL_kECDHE,
446
     SSL_aECDSA,
447
     SSL_CHACHA20POLY1305,
448
     SSL_AEAD,
449
     SSL_HANDSHAKE_MAC_SHA256,
450
    },
451
452
    // Cipher CCAB
453
    {
454
     TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
455
     "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
456
     TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
457
     SSL_kECDHE,
458
     SSL_aPSK,
459
     SSL_CHACHA20POLY1305,
460
     SSL_AEAD,
461
     SSL_HANDSHAKE_MAC_SHA256,
462
    },
463
464
};
465
466
0
Span<const SSL_CIPHER> AllCiphers() {
467
0
  return MakeConstSpan(kCiphers, OPENSSL_ARRAY_SIZE(kCiphers));
468
0
}
469
470
0
static constexpr size_t NumTLS13Ciphers() {
471
0
  size_t num = 0;
472
0
  for (const auto &cipher : kCiphers) {
473
0
    if (cipher.algorithm_mkey == SSL_kGENERIC) {
474
0
      num++;
475
0
    }
476
0
  }
477
0
  return num;
478
0
}
479
480
42
#define CIPHER_ADD 1
481
0
#define CIPHER_KILL 2
482
2
#define CIPHER_DEL 3
483
0
#define CIPHER_ORD 4
484
4
#define CIPHER_SPECIAL 5
485
486
typedef struct cipher_order_st {
487
  const SSL_CIPHER *cipher;
488
  bool active;
489
  bool in_group;
490
  struct cipher_order_st *next, *prev;
491
} CIPHER_ORDER;
492
493
typedef struct cipher_alias_st {
494
  // name is the name of the cipher alias.
495
  const char *name = nullptr;
496
497
  // The following fields are bitmasks for the corresponding fields on
498
  // |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
499
  // bit corresponding to the cipher's value is set to 1. If any bitmask is
500
  // all zeroes, the alias matches nothing. Use |~0u| for the default value.
501
  uint32_t algorithm_mkey = ~0u;
502
  uint32_t algorithm_auth = ~0u;
503
  uint32_t algorithm_enc = ~0u;
504
  uint32_t algorithm_mac = ~0u;
505
506
  // min_version, if non-zero, matches all ciphers which were added in that
507
  // particular protocol version.
508
  uint16_t min_version = 0;
509
510
  // include_deprecated, if true, means this alias includes deprecated ciphers.
511
  bool include_deprecated = false;
512
} CIPHER_ALIAS;
513
514
static const CIPHER_ALIAS kCipherAliases[] = {
515
    {"ALL", ~0u, ~0u, ~0u, ~0u, 0},
516
517
    // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing.
518
519
    // key exchange aliases
520
    // (some of those using only a single bit here combine
521
    // multiple key exchange algs according to the RFCs.
522
    {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
523
524
    {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
525
    {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
526
    {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
527
528
    {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
529
530
    // server authentication aliases
531
    {"aRSA", ~0u, SSL_aRSA, ~0u, ~0u, 0},
532
    {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
533
    {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
534
    {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
535
536
    // aliases combining key exchange and server authentication
537
    {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
538
    {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
539
    {"RSA", SSL_kRSA, SSL_aRSA, ~0u, ~0u, 0},
540
    {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
541
542
    // symmetric encryption aliases
543
    {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0, /*include_deprecated=*/true},
544
    {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0,
545
     /*include_deprecated=*/false},
546
    {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0,
547
     /*include_deprecated=*/false},
548
    {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
549
    {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0,
550
     /*include_deprecated=*/false},
551
    {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0,
552
     /*include_deprecated=*/false},
553
554
    // MAC aliases
555
    {"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0},
556
    {"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0},
557
558
    // Legacy protocol minimum version aliases. "TLSv1" is intentionally the
559
    // same as "SSLv3".
560
    {"SSLv3", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
561
    {"TLSv1", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
562
    {"TLSv1.2", ~0u, ~0u, ~0u, ~0u, TLS1_2_VERSION},
563
564
    // Legacy strength classes.
565
    {"HIGH", ~0u, ~0u, ~0u, ~0u, 0},
566
    {"FIPS", ~0u, ~0u, ~0u, ~0u, 0},
567
568
    // Temporary no-op aliases corresponding to removed SHA-2 legacy CBC
569
    // ciphers. These should be removed after 2018-05-14.
570
    {"SHA256", 0, 0, 0, 0, 0},
571
    {"SHA384", 0, 0, 0, 0, 0},
572
};
573
574
static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
575
576
bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
577
                             size_t *out_mac_secret_len,
578
                             size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
579
0
                             uint16_t version, bool is_dtls) {
580
0
  *out_aead = NULL;
581
0
  *out_mac_secret_len = 0;
582
0
  *out_fixed_iv_len = 0;
583
584
0
  const bool is_tls12 = version == TLS1_2_VERSION && !is_dtls;
585
0
  const bool is_tls13 = version == TLS1_3_VERSION && !is_dtls;
586
587
0
  if (cipher->algorithm_mac == SSL_AEAD) {
588
0
    if (cipher->algorithm_enc == SSL_AES128GCM) {
589
0
      if (is_tls12) {
590
0
        *out_aead = EVP_aead_aes_128_gcm_tls12();
591
0
      } else if (is_tls13) {
592
0
        *out_aead = EVP_aead_aes_128_gcm_tls13();
593
0
      } else {
594
0
        *out_aead = EVP_aead_aes_128_gcm();
595
0
      }
596
0
      *out_fixed_iv_len = 4;
597
0
    } else if (cipher->algorithm_enc == SSL_AES256GCM) {
598
0
      if (is_tls12) {
599
0
        *out_aead = EVP_aead_aes_256_gcm_tls12();
600
0
      } else if (is_tls13) {
601
0
        *out_aead = EVP_aead_aes_256_gcm_tls13();
602
0
      } else {
603
0
        *out_aead = EVP_aead_aes_256_gcm();
604
0
      }
605
0
      *out_fixed_iv_len = 4;
606
0
    } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
607
0
      *out_aead = EVP_aead_chacha20_poly1305();
608
0
      *out_fixed_iv_len = 12;
609
0
    } else {
610
0
      return false;
611
0
    }
612
613
    // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
614
    // above computes the TLS 1.2 construction.
615
0
    if (version >= TLS1_3_VERSION) {
616
0
      *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
617
0
    }
618
0
  } else if (cipher->algorithm_mac == SSL_SHA1) {
619
0
    if (cipher->algorithm_enc == SSL_3DES) {
620
0
      if (version == TLS1_VERSION) {
621
0
        *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
622
0
        *out_fixed_iv_len = 8;
623
0
      } else {
624
0
        *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
625
0
      }
626
0
    } else if (cipher->algorithm_enc == SSL_AES128) {
627
0
      if (version == TLS1_VERSION) {
628
0
        *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
629
0
        *out_fixed_iv_len = 16;
630
0
      } else {
631
0
        *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
632
0
      }
633
0
    } else if (cipher->algorithm_enc == SSL_AES256) {
634
0
      if (version == TLS1_VERSION) {
635
0
        *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
636
0
        *out_fixed_iv_len = 16;
637
0
      } else {
638
0
        *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
639
0
      }
640
0
    } else {
641
0
      return false;
642
0
    }
643
644
0
    *out_mac_secret_len = SHA_DIGEST_LENGTH;
645
0
  } else if (cipher->algorithm_mac == SSL_SHA256) {
646
0
    if (cipher->algorithm_enc == SSL_AES128) {
647
0
      *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
648
0
    } else {
649
0
      return false;
650
0
    }
651
652
0
    *out_mac_secret_len = SHA256_DIGEST_LENGTH;
653
0
  } else {
654
0
    return false;
655
0
  }
656
657
0
  return true;
658
0
}
659
660
const EVP_MD *ssl_get_handshake_digest(uint16_t version,
661
0
                                       const SSL_CIPHER *cipher) {
662
0
  switch (cipher->algorithm_prf) {
663
0
    case SSL_HANDSHAKE_MAC_DEFAULT:
664
0
      return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
665
0
    case SSL_HANDSHAKE_MAC_SHA256:
666
0
      return EVP_sha256();
667
0
    case SSL_HANDSHAKE_MAC_SHA384:
668
0
      return EVP_sha384();
669
0
    default:
670
0
      assert(0);
671
0
      return NULL;
672
0
  }
673
0
}
674
675
2
static bool is_cipher_list_separator(char c, bool is_strict) {
676
2
  if (c == ':') {
677
0
    return true;
678
0
  }
679
2
  return !is_strict && (c == ' ' || c == ';' || c == ',');
680
2
}
681
682
// rule_equals returns whether the NUL-terminated string |rule| is equal to the
683
// |buf_len| bytes at |buf|.
684
98
static bool rule_equals(const char *rule, const char *buf, size_t buf_len) {
685
  // |strncmp| alone only checks that |buf| is a prefix of |rule|.
686
98
  return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
687
98
}
688
689
static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
690
38
                           CIPHER_ORDER **tail) {
691
38
  if (curr == *tail) {
692
0
    return;
693
0
  }
694
38
  if (curr == *head) {
695
26
    *head = curr->next;
696
26
  }
697
38
  if (curr->prev != NULL) {
698
12
    curr->prev->next = curr->next;
699
12
  }
700
38
  if (curr->next != NULL) {
701
38
    curr->next->prev = curr->prev;
702
38
  }
703
38
  (*tail)->next = curr;
704
38
  curr->prev = *tail;
705
38
  curr->next = NULL;
706
38
  *tail = curr;
707
38
}
708
709
static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
710
0
                           CIPHER_ORDER **tail) {
711
0
  if (curr == *head) {
712
0
    return;
713
0
  }
714
0
  if (curr == *tail) {
715
0
    *tail = curr->prev;
716
0
  }
717
0
  if (curr->next != NULL) {
718
0
    curr->next->prev = curr->prev;
719
0
  }
720
0
  if (curr->prev != NULL) {
721
0
    curr->prev->next = curr->next;
722
0
  }
723
0
  (*head)->prev = curr;
724
0
  curr->next = *head;
725
0
  curr->prev = NULL;
726
0
  *head = curr;
727
0
}
728
729
0
SSLCipherPreferenceList::~SSLCipherPreferenceList() {
730
0
  OPENSSL_free(in_group_flags);
731
0
}
732
733
bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers_arg,
734
2
                                   Span<const bool> in_group_flags_arg) {
735
2
  if (sk_SSL_CIPHER_num(ciphers_arg.get()) != in_group_flags_arg.size()) {
736
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
737
0
    return false;
738
0
  }
739
740
2
  Array<bool> copy;
741
2
  if (!copy.CopyFrom(in_group_flags_arg)) {
742
0
    return false;
743
0
  }
744
2
  ciphers = std::move(ciphers_arg);
745
2
  size_t unused_len;
746
2
  copy.Release(&in_group_flags, &unused_len);
747
2
  return true;
748
2
}
749
750
0
bool SSLCipherPreferenceList::Init(const SSLCipherPreferenceList& other) {
751
0
  size_t size = sk_SSL_CIPHER_num(other.ciphers.get());
752
0
  Span<const bool> other_flags(other.in_group_flags, size);
753
0
  UniquePtr<STACK_OF(SSL_CIPHER)> other_ciphers(sk_SSL_CIPHER_dup(
754
0
      other.ciphers.get()));
755
0
  if (!other_ciphers) {
756
0
    return false;
757
0
  }
758
0
  return Init(std::move(other_ciphers), other_flags);
759
0
}
760
761
0
void SSLCipherPreferenceList::Remove(const SSL_CIPHER *cipher) {
762
0
  size_t index;
763
0
  if (!sk_SSL_CIPHER_find(ciphers.get(), &index, cipher)) {
764
0
    return;
765
0
  }
766
0
  if (!in_group_flags[index] /* last element of group */ && index > 0) {
767
0
    in_group_flags[index-1] = false;
768
0
  }
769
0
  for (size_t i = index; i < sk_SSL_CIPHER_num(ciphers.get()) - 1; ++i) {
770
0
    in_group_flags[i] = in_group_flags[i+1];
771
0
  }
772
0
  sk_SSL_CIPHER_delete(ciphers.get(), index);
773
0
}
774
775
42
bool ssl_cipher_is_deprecated(const SSL_CIPHER *cipher) {
776
42
  return cipher->id == TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA256 ||
777
42
         cipher->algorithm_enc == SSL_3DES;
778
42
}
779
780
// ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
781
// parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
782
// head and tail of the list to |*head_p| and |*tail_p|, respectively.
783
//
784
// - If |cipher_id| is non-zero, only that cipher is selected.
785
// - Otherwise, if |strength_bits| is non-negative, it selects ciphers
786
//   of that strength.
787
// - Otherwise, |alias| must be non-null. It selects ciphers that matches
788
//   |*alias|.
789
static void ssl_cipher_apply_rule(uint32_t cipher_id, const CIPHER_ALIAS *alias,
790
                                  int rule, int strength_bits, bool in_group,
791
                                  CIPHER_ORDER **head_p,
792
2
                                  CIPHER_ORDER **tail_p) {
793
2
  CIPHER_ORDER *head, *tail, *curr, *next, *last;
794
2
  const SSL_CIPHER *cp;
795
2
  bool reverse = false;
796
797
2
  if (cipher_id == 0 && strength_bits == -1 && alias->min_version == 0 &&
798
2
      (alias->algorithm_mkey == 0 || alias->algorithm_auth == 0 ||
799
2
       alias->algorithm_enc == 0 || alias->algorithm_mac == 0)) {
800
    // The rule matches nothing, so bail early.
801
0
    return;
802
0
  }
803
804
2
  if (rule == CIPHER_DEL) {
805
    // needed to maintain sorting between currently deleted ciphers
806
0
    reverse = true;
807
0
  }
808
809
2
  head = *head_p;
810
2
  tail = *tail_p;
811
812
2
  if (reverse) {
813
0
    next = tail;
814
0
    last = head;
815
2
  } else {
816
2
    next = head;
817
2
    last = tail;
818
2
  }
819
820
2
  curr = NULL;
821
44
  for (;;) {
822
44
    if (curr == last) {
823
2
      break;
824
2
    }
825
826
42
    curr = next;
827
42
    if (curr == NULL) {
828
0
      break;
829
0
    }
830
831
42
    next = reverse ? curr->prev : curr->next;
832
42
    cp = curr->cipher;
833
834
    // Selection criteria is either a specific cipher, the value of
835
    // |strength_bits|, or the algorithms used.
836
42
    if (cipher_id != 0) {
837
0
      if (cipher_id != cp->id) {
838
0
        continue;
839
0
      }
840
42
    } else if (strength_bits >= 0) {
841
0
      if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
842
0
        continue;
843
0
      }
844
42
    } else {
845
42
      if (!(alias->algorithm_mkey & cp->algorithm_mkey) ||
846
42
          !(alias->algorithm_auth & cp->algorithm_auth) ||
847
42
          !(alias->algorithm_enc & cp->algorithm_enc) ||
848
42
          !(alias->algorithm_mac & cp->algorithm_mac) ||
849
42
          (alias->min_version != 0 &&
850
42
           SSL_CIPHER_get_min_version(cp) != alias->min_version) ||
851
42
          (!alias->include_deprecated && ssl_cipher_is_deprecated(cp))) {
852
4
        continue;
853
4
      }
854
42
    }
855
856
    // add the cipher if it has not been added yet.
857
38
    if (rule == CIPHER_ADD) {
858
      // reverse == false
859
38
      if (!curr->active) {
860
38
        ll_append_tail(&head, curr, &tail);
861
38
        curr->active = true;
862
38
        curr->in_group = in_group;
863
38
      }
864
38
    }
865
866
    // Move the added cipher to this location
867
0
    else if (rule == CIPHER_ORD) {
868
      // reverse == false
869
0
      if (curr->active) {
870
0
        ll_append_tail(&head, curr, &tail);
871
0
        curr->in_group = false;
872
0
      }
873
0
    } else if (rule == CIPHER_DEL) {
874
      // reverse == true
875
0
      if (curr->active) {
876
        // most recently deleted ciphersuites get best positions
877
        // for any future CIPHER_ADD (note that the CIPHER_DEL loop
878
        // works in reverse to maintain the order)
879
0
        ll_append_head(&head, curr, &tail);
880
0
        curr->active = false;
881
0
        curr->in_group = false;
882
0
      }
883
0
    } else if (rule == CIPHER_KILL) {
884
      // reverse == false
885
0
      if (head == curr) {
886
0
        head = curr->next;
887
0
      } else {
888
0
        curr->prev->next = curr->next;
889
0
      }
890
891
0
      if (tail == curr) {
892
0
        tail = curr->prev;
893
0
      }
894
0
      curr->active = false;
895
0
      if (curr->next != NULL) {
896
0
        curr->next->prev = curr->prev;
897
0
      }
898
0
      if (curr->prev != NULL) {
899
0
        curr->prev->next = curr->next;
900
0
      }
901
0
      curr->next = NULL;
902
0
      curr->prev = NULL;
903
0
    }
904
38
  }
905
906
2
  *head_p = head;
907
2
  *tail_p = tail;
908
2
}
909
910
static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
911
0
                                     CIPHER_ORDER **tail_p) {
912
  // This routine sorts the ciphers with descending strength. The sorting must
913
  // keep the pre-sorted sequence, so we apply the normal sorting routine as
914
  // '+' movement to the end of the list.
915
0
  int max_strength_bits = 0;
916
0
  CIPHER_ORDER *curr = *head_p;
917
0
  while (curr != NULL) {
918
0
    if (curr->active &&
919
0
        SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
920
0
      max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
921
0
    }
922
0
    curr = curr->next;
923
0
  }
924
925
0
  Array<int> number_uses;
926
0
  if (!number_uses.Init(max_strength_bits + 1)) {
927
0
    return false;
928
0
  }
929
0
  OPENSSL_memset(number_uses.data(), 0, (max_strength_bits + 1) * sizeof(int));
930
931
  // Now find the strength_bits values actually used.
932
0
  curr = *head_p;
933
0
  while (curr != NULL) {
934
0
    if (curr->active) {
935
0
      number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
936
0
    }
937
0
    curr = curr->next;
938
0
  }
939
940
  // Go through the list of used strength_bits values in descending order.
941
0
  for (int i = max_strength_bits; i >= 0; i--) {
942
0
    if (number_uses[i] > 0) {
943
0
      ssl_cipher_apply_rule(/*cipher_id=*/0, /*alias=*/nullptr, CIPHER_ORD, i,
944
0
                            false, head_p, tail_p);
945
0
    }
946
0
  }
947
948
0
  return true;
949
0
}
950
951
static bool ssl_cipher_process_rulestr(const char *rule_str,
952
                                       CIPHER_ORDER **head_p,
953
2
                                       CIPHER_ORDER **tail_p, bool strict) {
954
2
  const char *l, *buf;
955
2
  bool in_group = false, has_group = false;
956
2
  size_t j, buf_len;
957
2
  char ch;
958
959
2
  l = rule_str;
960
4
  for (;;) {
961
4
    ch = *l;
962
963
4
    if (ch == '\0') {
964
2
      break;  // done
965
2
    }
966
967
2
    int rule;
968
2
    if (in_group) {
969
0
      if (ch == ']') {
970
0
        if (*tail_p) {
971
0
          (*tail_p)->in_group = false;
972
0
        }
973
0
        in_group = false;
974
0
        l++;
975
0
        continue;
976
0
      }
977
978
0
      if (ch == '|') {
979
0
        rule = CIPHER_ADD;
980
0
        l++;
981
0
        continue;
982
0
      } else if (!OPENSSL_isalnum(ch)) {
983
0
        OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
984
0
        return false;
985
0
      } else {
986
0
        rule = CIPHER_ADD;
987
0
      }
988
2
    } else if (ch == '-') {
989
0
      rule = CIPHER_DEL;
990
0
      l++;
991
2
    } else if (ch == '+') {
992
0
      rule = CIPHER_ORD;
993
0
      l++;
994
2
    } else if (ch == '!') {
995
0
      rule = CIPHER_KILL;
996
0
      l++;
997
2
    } else if (ch == '@') {
998
0
      rule = CIPHER_SPECIAL;
999
0
      l++;
1000
2
    } else if (ch == '[') {
1001
0
      assert(!in_group);
1002
0
      in_group = true;
1003
0
      has_group = true;
1004
0
      l++;
1005
0
      continue;
1006
2
    } else {
1007
2
      rule = CIPHER_ADD;
1008
2
    }
1009
1010
    // If preference groups are enabled, the only legal operator is +.
1011
    // Otherwise the in_group bits will get mixed up.
1012
2
    if (has_group && rule != CIPHER_ADD) {
1013
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
1014
0
      return false;
1015
0
    }
1016
1017
2
    if (is_cipher_list_separator(ch, strict)) {
1018
0
      l++;
1019
0
      continue;
1020
0
    }
1021
1022
2
    bool multi = false;
1023
2
    uint32_t cipher_id = 0;
1024
2
    CIPHER_ALIAS alias;
1025
2
    bool skip_rule = false;
1026
1027
    // When adding, exclude deprecated ciphers by default.
1028
2
    alias.include_deprecated = rule != CIPHER_ADD;
1029
1030
2
    for (;;) {
1031
2
      ch = *l;
1032
2
      buf = l;
1033
2
      buf_len = 0;
1034
8
      while (OPENSSL_isalnum(ch) || ch == '-' || ch == '.' || ch == '_') {
1035
6
        ch = *(++l);
1036
6
        buf_len++;
1037
6
      }
1038
1039
2
      if (buf_len == 0) {
1040
        // We hit something we cannot deal with, it is no command or separator
1041
        // nor alphanumeric, so we call this an error.
1042
0
        OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1043
0
        return false;
1044
0
      }
1045
1046
2
      if (rule == CIPHER_SPECIAL) {
1047
0
        break;
1048
0
      }
1049
1050
      // Look for a matching exact cipher. These aren't allowed in multipart
1051
      // rules.
1052
2
      if (!multi && ch != '+') {
1053
50
        for (j = 0; j < OPENSSL_ARRAY_SIZE(kCiphers); j++) {
1054
48
          const SSL_CIPHER *cipher = &kCiphers[j];
1055
48
          if (rule_equals(cipher->name, buf, buf_len) ||
1056
48
              rule_equals(cipher->standard_name, buf, buf_len)) {
1057
0
            cipher_id = cipher->id;
1058
0
            break;
1059
0
          }
1060
48
        }
1061
2
      }
1062
2
      if (cipher_id == 0) {
1063
        // If not an exact cipher, look for a matching cipher alias.
1064
2
        for (j = 0; j < kCipherAliasesLen; j++) {
1065
2
          if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1066
2
            alias.algorithm_mkey &= kCipherAliases[j].algorithm_mkey;
1067
2
            alias.algorithm_auth &= kCipherAliases[j].algorithm_auth;
1068
2
            alias.algorithm_enc &= kCipherAliases[j].algorithm_enc;
1069
2
            alias.algorithm_mac &= kCipherAliases[j].algorithm_mac;
1070
1071
            // When specifying a combination of aliases, if any aliases
1072
            // enables deprecated ciphers, deprecated ciphers are included. This
1073
            // is slightly different from the bitmasks in that adding aliases
1074
            // can increase the set of matched ciphers. This is so that an alias
1075
            // like "RSA" will only specifiy AES-based RSA ciphers, but
1076
            // "RSA+3DES" will still specify 3DES.
1077
2
            alias.include_deprecated |= kCipherAliases[j].include_deprecated;
1078
1079
2
            if (alias.min_version != 0 &&
1080
2
                alias.min_version != kCipherAliases[j].min_version) {
1081
0
              skip_rule = true;
1082
2
            } else {
1083
2
              alias.min_version = kCipherAliases[j].min_version;
1084
2
            }
1085
2
            break;
1086
2
          }
1087
2
        }
1088
2
        if (j == kCipherAliasesLen) {
1089
0
          skip_rule = true;
1090
0
          if (strict) {
1091
0
            OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1092
0
            return false;
1093
0
          }
1094
0
        }
1095
2
      }
1096
1097
      // Check for a multipart rule.
1098
2
      if (ch != '+') {
1099
2
        break;
1100
2
      }
1101
0
      l++;
1102
0
      multi = true;
1103
0
    }
1104
1105
    // Ok, we have the rule, now apply it.
1106
2
    if (rule == CIPHER_SPECIAL) {
1107
0
      if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) {
1108
0
        OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1109
0
        return false;
1110
0
      }
1111
0
      if (!ssl_cipher_strength_sort(head_p, tail_p)) {
1112
0
        return false;
1113
0
      }
1114
1115
      // We do not support any "multi" options together with "@", so throw away
1116
      // the rest of the command, if any left, until end or ':' is found.
1117
0
      while (*l != '\0' && !is_cipher_list_separator(*l, strict)) {
1118
0
        l++;
1119
0
      }
1120
2
    } else if (!skip_rule) {
1121
2
      ssl_cipher_apply_rule(cipher_id, &alias, rule, -1, in_group, head_p,
1122
2
                            tail_p);
1123
2
    }
1124
2
  }
1125
1126
2
  if (in_group) {
1127
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1128
0
    return false;
1129
0
  }
1130
1131
2
  return true;
1132
2
}
1133
1134
bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
1135
                            const bool has_aes_hw, const char *rule_str,
1136
2
                            bool strict) {
1137
  // Return with error if nothing to do.
1138
2
  if (rule_str == NULL || out_cipher_list == NULL) {
1139
0
    return false;
1140
0
  }
1141
1142
  // We prefer ECDHE ciphers over non-PFS ciphers. Then we prefer AEAD over
1143
  // non-AEAD. The constants are masked by 0xffff to remove the vestigial 0x03
1144
  // byte from SSL 2.0.
1145
2
  static const uint16_t kAESCiphers[] = {
1146
2
      TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 & 0xffff,
1147
2
      TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256 & 0xffff,
1148
2
      TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 & 0xffff,
1149
2
      TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384 & 0xffff,
1150
2
  };
1151
2
  static const uint16_t kChaChaCiphers[] = {
1152
2
      TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 & 0xffff,
1153
2
      TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 & 0xffff,
1154
2
      TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 & 0xffff,
1155
2
  };
1156
2
  static const uint16_t kLegacyCiphers[] = {
1157
2
      TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA & 0xffff,
1158
2
      TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA & 0xffff,
1159
2
      TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA & 0xffff,
1160
2
      TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA & 0xffff,
1161
2
      TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA & 0xffff,
1162
2
      TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA & 0xffff,
1163
2
      TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA256 & 0xffff,
1164
2
      TLS1_CK_RSA_WITH_AES_128_GCM_SHA256 & 0xffff,
1165
2
      TLS1_CK_RSA_WITH_AES_256_GCM_SHA384 & 0xffff,
1166
2
      TLS1_CK_RSA_WITH_AES_128_SHA & 0xffff,
1167
2
      TLS1_CK_PSK_WITH_AES_128_CBC_SHA & 0xffff,
1168
2
      TLS1_CK_RSA_WITH_AES_256_SHA & 0xffff,
1169
2
      TLS1_CK_PSK_WITH_AES_256_CBC_SHA & 0xffff,
1170
2
      SSL3_CK_RSA_DES_192_CBC3_SHA & 0xffff,
1171
2
  };
1172
1173
  // Set up a linked list of ciphers.
1174
2
  CIPHER_ORDER co_list[OPENSSL_ARRAY_SIZE(kAESCiphers) +
1175
2
                       OPENSSL_ARRAY_SIZE(kChaChaCiphers) +
1176
2
                       OPENSSL_ARRAY_SIZE(kLegacyCiphers)];
1177
44
  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(co_list); i++) {
1178
42
    co_list[i].next =
1179
42
        i + 1 < OPENSSL_ARRAY_SIZE(co_list) ? &co_list[i + 1] : nullptr;
1180
42
    co_list[i].prev = i == 0 ? nullptr : &co_list[i - 1];
1181
42
    co_list[i].active = false;
1182
42
    co_list[i].in_group = false;
1183
42
  }
1184
2
  CIPHER_ORDER *head = &co_list[0];
1185
2
  CIPHER_ORDER *tail = &co_list[OPENSSL_ARRAY_SIZE(co_list) - 1];
1186
1187
  // Order AES ciphers vs ChaCha ciphers based on whether we have AES hardware.
1188
  //
1189
  // TODO(crbug.com/boringssl/29): We should also set up equipreference groups
1190
  // as a server.
1191
2
  size_t num = 0;
1192
2
  if (has_aes_hw) {
1193
0
    for (uint16_t id : kAESCiphers) {
1194
0
      co_list[num++].cipher = SSL_get_cipher_by_value(id);
1195
0
      assert(co_list[num - 1].cipher != nullptr);
1196
0
    }
1197
0
  }
1198
6
  for (uint16_t id : kChaChaCiphers) {
1199
6
    co_list[num++].cipher = SSL_get_cipher_by_value(id);
1200
6
    assert(co_list[num - 1].cipher != nullptr);
1201
6
  }
1202
2
  if (!has_aes_hw) {
1203
8
    for (uint16_t id : kAESCiphers) {
1204
8
      co_list[num++].cipher = SSL_get_cipher_by_value(id);
1205
8
      assert(co_list[num - 1].cipher != nullptr);
1206
8
    }
1207
2
  }
1208
28
  for (uint16_t id : kLegacyCiphers) {
1209
28
    co_list[num++].cipher = SSL_get_cipher_by_value(id);
1210
28
    assert(co_list[num - 1].cipher != nullptr);
1211
28
  }
1212
2
  assert(num == OPENSSL_ARRAY_SIZE(co_list));
1213
0
  static_assert(OPENSSL_ARRAY_SIZE(co_list) + NumTLS13Ciphers() ==
1214
2
                    OPENSSL_ARRAY_SIZE(kCiphers),
1215
2
                "Not all ciphers are included in the cipher order");
1216
1217
  // If the rule_string begins with DEFAULT, apply the default rule before
1218
  // using the (possibly available) additional rules.
1219
2
  const char *rule_p = rule_str;
1220
2
  if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1221
0
    if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1222
0
                                    strict)) {
1223
0
      return false;
1224
0
    }
1225
0
    rule_p += 7;
1226
0
    if (*rule_p == ':') {
1227
0
      rule_p++;
1228
0
    }
1229
0
  }
1230
1231
2
  if (*rule_p != '\0' &&
1232
2
      !ssl_cipher_process_rulestr(rule_p, &head, &tail, strict)) {
1233
0
    return false;
1234
0
  }
1235
1236
  // Allocate new "cipherstack" for the result, return with error
1237
  // if we cannot get one.
1238
2
  UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null());
1239
2
  Array<bool> in_group_flags;
1240
2
  if (cipherstack == nullptr ||
1241
2
      !in_group_flags.Init(OPENSSL_ARRAY_SIZE(kCiphers))) {
1242
0
    return false;
1243
0
  }
1244
1245
  // The cipher selection for the list is done. The ciphers are added
1246
  // to the resulting precedence to the STACK_OF(SSL_CIPHER).
1247
2
  size_t num_in_group_flags = 0;
1248
44
  for (CIPHER_ORDER *curr = head; curr != NULL; curr = curr->next) {
1249
42
    if (curr->active) {
1250
38
      if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) {
1251
0
        return false;
1252
0
      }
1253
38
      in_group_flags[num_in_group_flags++] = curr->in_group;
1254
38
    }
1255
42
  }
1256
1257
2
  UniquePtr<SSLCipherPreferenceList> pref_list =
1258
2
      MakeUnique<SSLCipherPreferenceList>();
1259
2
  if (!pref_list ||
1260
2
      !pref_list->Init(
1261
2
          std::move(cipherstack),
1262
2
          MakeConstSpan(in_group_flags).subspan(0, num_in_group_flags))) {
1263
0
    return false;
1264
0
  }
1265
1266
2
  *out_cipher_list = std::move(pref_list);
1267
1268
  // Configuring an empty cipher list is an error but still updates the
1269
  // output.
1270
2
  if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers.get()) == 0) {
1271
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1272
0
    return false;
1273
0
  }
1274
1275
2
  return true;
1276
2
}
1277
1278
0
uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
1279
0
  switch (EVP_PKEY_id(key)) {
1280
0
    case EVP_PKEY_RSA:
1281
0
      return SSL_aRSA;
1282
0
    case EVP_PKEY_EC:
1283
0
    case EVP_PKEY_ED25519:
1284
      // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers.
1285
0
      return SSL_aECDSA;
1286
0
    default:
1287
0
      return 0;
1288
0
  }
1289
0
}
1290
1291
0
bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
1292
0
  return (cipher->algorithm_auth & SSL_aCERT) != 0;
1293
0
}
1294
1295
0
bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1296
  // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is
1297
  // optional or omitted in all others.
1298
0
  return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
1299
0
}
1300
1301
0
size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1302
0
  size_t block_size;
1303
0
  switch (cipher->algorithm_enc) {
1304
0
    case SSL_3DES:
1305
0
      block_size = 8;
1306
0
      break;
1307
0
    case SSL_AES128:
1308
0
    case SSL_AES256:
1309
0
      block_size = 16;
1310
0
      break;
1311
0
    default:
1312
0
      return 0;
1313
0
  }
1314
1315
  // All supported TLS 1.0 ciphers use SHA-1.
1316
0
  assert(cipher->algorithm_mac == SSL_SHA1);
1317
0
  size_t ret = 1 + SHA_DIGEST_LENGTH;
1318
0
  ret += block_size - (ret % block_size);
1319
0
  return ret;
1320
0
}
1321
1322
BSSL_NAMESPACE_END
1323
1324
using namespace bssl;
1325
1326
static constexpr int ssl_cipher_id_cmp(const SSL_CIPHER *a,
1327
11.9k
                                       const SSL_CIPHER *b) {
1328
11.9k
  if (a->id > b->id) {
1329
2.64k
    return 1;
1330
2.64k
  }
1331
9.31k
  if (a->id < b->id) {
1332
6.51k
    return -1;
1333
6.51k
  }
1334
2.79k
  return 0;
1335
9.31k
}
1336
1337
11.9k
static int ssl_cipher_id_cmp_void(const void *in_a, const void *in_b) {
1338
11.9k
  return ssl_cipher_id_cmp(reinterpret_cast<const SSL_CIPHER *>(in_a),
1339
11.9k
                           reinterpret_cast<const SSL_CIPHER *>(in_b));
1340
11.9k
}
1341
1342
template <size_t N>
1343
0
static constexpr bool ssl_ciphers_sorted(const SSL_CIPHER (&ciphers)[N]) {
1344
0
  for (size_t i = 1; i < N; i++) {
1345
0
    if (ssl_cipher_id_cmp(&ciphers[i - 1], &ciphers[i]) >= 0) {
1346
0
      return false;
1347
0
    }
1348
0
  }
1349
0
  return true;
1350
0
}
1351
1352
static_assert(ssl_ciphers_sorted(kCiphers),
1353
              "Ciphers are not sorted, bsearch won't work");
1354
1355
2.81k
const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
1356
2.81k
  SSL_CIPHER c;
1357
1358
2.81k
  c.id = 0x03000000L | value;
1359
2.81k
  return reinterpret_cast<const SSL_CIPHER *>(bsearch(
1360
2.81k
      &c, kCiphers, OPENSSL_ARRAY_SIZE(kCiphers), sizeof(SSL_CIPHER),
1361
2.81k
      ssl_cipher_id_cmp_void));
1362
2.81k
}
1363
1364
0
uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1365
1366
0
uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *cipher) {
1367
  // All OpenSSL cipher IDs are prefaced with 0x03. Historically this referred
1368
  // to SSLv2 vs SSLv3.
1369
0
  assert((cipher->id & 0xff000000) == 0x03000000);
1370
0
  return static_cast<uint16_t>(cipher->id);
1371
0
}
1372
1373
0
int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) {
1374
0
  return (cipher->algorithm_mac & SSL_AEAD) != 0;
1375
0
}
1376
1377
0
int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) {
1378
0
  switch (cipher->algorithm_enc) {
1379
0
    case SSL_3DES:
1380
0
      return NID_des_ede3_cbc;
1381
0
    case SSL_AES128:
1382
0
      return NID_aes_128_cbc;
1383
0
    case SSL_AES256:
1384
0
      return NID_aes_256_cbc;
1385
0
    case SSL_AES128GCM:
1386
0
      return NID_aes_128_gcm;
1387
0
    case SSL_AES256GCM:
1388
0
      return NID_aes_256_gcm;
1389
0
    case SSL_CHACHA20POLY1305:
1390
0
      return NID_chacha20_poly1305;
1391
0
  }
1392
0
  assert(0);
1393
0
  return NID_undef;
1394
0
}
1395
1396
0
int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) {
1397
0
  switch (cipher->algorithm_mac) {
1398
0
    case SSL_AEAD:
1399
0
      return NID_undef;
1400
0
    case SSL_SHA1:
1401
0
      return NID_sha1;
1402
0
    case SSL_SHA256:
1403
0
      return NID_sha256;
1404
0
  }
1405
0
  assert(0);
1406
0
  return NID_undef;
1407
0
}
1408
1409
0
int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) {
1410
0
  switch (cipher->algorithm_mkey) {
1411
0
    case SSL_kRSA:
1412
0
      return NID_kx_rsa;
1413
0
    case SSL_kECDHE:
1414
0
      return NID_kx_ecdhe;
1415
0
    case SSL_kPSK:
1416
0
      return NID_kx_psk;
1417
0
    case SSL_kGENERIC:
1418
0
      return NID_kx_any;
1419
0
  }
1420
0
  assert(0);
1421
0
  return NID_undef;
1422
0
}
1423
1424
0
int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) {
1425
0
  switch (cipher->algorithm_auth) {
1426
0
    case SSL_aRSA:
1427
0
      return NID_auth_rsa;
1428
0
    case SSL_aECDSA:
1429
0
      return NID_auth_ecdsa;
1430
0
    case SSL_aPSK:
1431
0
      return NID_auth_psk;
1432
0
    case SSL_aGENERIC:
1433
0
      return NID_auth_any;
1434
0
  }
1435
0
  assert(0);
1436
0
  return NID_undef;
1437
0
}
1438
1439
0
const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *cipher) {
1440
0
  switch (cipher->algorithm_prf) {
1441
0
    case SSL_HANDSHAKE_MAC_DEFAULT:
1442
0
      return EVP_md5_sha1();
1443
0
    case SSL_HANDSHAKE_MAC_SHA256:
1444
0
      return EVP_sha256();
1445
0
    case SSL_HANDSHAKE_MAC_SHA384:
1446
0
      return EVP_sha384();
1447
0
  }
1448
0
  assert(0);
1449
0
  return NULL;
1450
0
}
1451
1452
0
int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) {
1453
0
  const EVP_MD *md = SSL_CIPHER_get_handshake_digest(cipher);
1454
0
  if (md == NULL) {
1455
0
    return NID_undef;
1456
0
  }
1457
0
  return EVP_MD_nid(md);
1458
0
}
1459
1460
0
int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1461
0
  return cipher->algorithm_mac != SSL_AEAD;
1462
0
}
1463
1464
0
uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
1465
0
  if (cipher->algorithm_mkey == SSL_kGENERIC ||
1466
0
      cipher->algorithm_auth == SSL_aGENERIC) {
1467
0
    return TLS1_3_VERSION;
1468
0
  }
1469
1470
0
  if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1471
    // Cipher suites before TLS 1.2 use the default PRF, while all those added
1472
    // afterwards specify a particular hash.
1473
0
    return TLS1_2_VERSION;
1474
0
  }
1475
0
  return SSL3_VERSION;
1476
0
}
1477
1478
0
uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
1479
0
  if (cipher->algorithm_mkey == SSL_kGENERIC ||
1480
0
      cipher->algorithm_auth == SSL_aGENERIC) {
1481
0
    return TLS1_3_VERSION;
1482
0
  }
1483
0
  return TLS1_2_VERSION;
1484
0
}
1485
1486
static const char* kUnknownCipher = "(NONE)";
1487
1488
// return the actual cipher being used
1489
0
const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1490
0
  if (cipher != NULL) {
1491
0
    return cipher->name;
1492
0
  }
1493
1494
0
  return kUnknownCipher;
1495
0
}
1496
1497
0
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
1498
0
  return cipher->standard_name;
1499
0
}
1500
1501
0
const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1502
0
  if (cipher == NULL) {
1503
0
    return "";
1504
0
  }
1505
1506
0
  switch (cipher->algorithm_mkey) {
1507
0
    case SSL_kRSA:
1508
0
      return "RSA";
1509
1510
0
    case SSL_kECDHE:
1511
0
      switch (cipher->algorithm_auth) {
1512
0
        case SSL_aECDSA:
1513
0
          return "ECDHE_ECDSA";
1514
0
        case SSL_aRSA:
1515
0
          return "ECDHE_RSA";
1516
0
        case SSL_aPSK:
1517
0
          return "ECDHE_PSK";
1518
0
        default:
1519
0
          assert(0);
1520
0
          return "UNKNOWN";
1521
0
      }
1522
1523
0
    case SSL_kPSK:
1524
0
      assert(cipher->algorithm_auth == SSL_aPSK);
1525
0
      return "PSK";
1526
1527
0
    case SSL_kGENERIC:
1528
0
      assert(cipher->algorithm_auth == SSL_aGENERIC);
1529
0
      return "GENERIC";
1530
1531
0
    default:
1532
0
      assert(0);
1533
0
      return "UNKNOWN";
1534
0
  }
1535
0
}
1536
1537
0
int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1538
0
  if (cipher == NULL) {
1539
0
    return 0;
1540
0
  }
1541
1542
0
  int alg_bits, strength_bits;
1543
0
  switch (cipher->algorithm_enc) {
1544
0
    case SSL_AES128:
1545
0
    case SSL_AES128GCM:
1546
0
      alg_bits = 128;
1547
0
      strength_bits = 128;
1548
0
      break;
1549
1550
0
    case SSL_AES256:
1551
0
    case SSL_AES256GCM:
1552
0
    case SSL_CHACHA20POLY1305:
1553
0
      alg_bits = 256;
1554
0
      strength_bits = 256;
1555
0
      break;
1556
1557
0
    case SSL_3DES:
1558
0
      alg_bits = 168;
1559
0
      strength_bits = 112;
1560
0
      break;
1561
1562
0
    default:
1563
0
      assert(0);
1564
0
      alg_bits = 0;
1565
0
      strength_bits = 0;
1566
0
  }
1567
1568
0
  if (out_alg_bits != NULL) {
1569
0
    *out_alg_bits = alg_bits;
1570
0
  }
1571
0
  return strength_bits;
1572
0
}
1573
1574
const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1575
0
                                   int len) {
1576
0
  const char *kx, *au, *enc, *mac;
1577
0
  uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1578
1579
0
  alg_mkey = cipher->algorithm_mkey;
1580
0
  alg_auth = cipher->algorithm_auth;
1581
0
  alg_enc = cipher->algorithm_enc;
1582
0
  alg_mac = cipher->algorithm_mac;
1583
1584
0
  switch (alg_mkey) {
1585
0
    case SSL_kRSA:
1586
0
      kx = "RSA";
1587
0
      break;
1588
1589
0
    case SSL_kECDHE:
1590
0
      kx = "ECDH";
1591
0
      break;
1592
1593
0
    case SSL_kPSK:
1594
0
      kx = "PSK";
1595
0
      break;
1596
1597
0
    case SSL_kGENERIC:
1598
0
      kx = "GENERIC";
1599
0
      break;
1600
1601
0
    default:
1602
0
      kx = "unknown";
1603
0
  }
1604
1605
0
  switch (alg_auth) {
1606
0
    case SSL_aRSA:
1607
0
      au = "RSA";
1608
0
      break;
1609
1610
0
    case SSL_aECDSA:
1611
0
      au = "ECDSA";
1612
0
      break;
1613
1614
0
    case SSL_aPSK:
1615
0
      au = "PSK";
1616
0
      break;
1617
1618
0
    case SSL_aGENERIC:
1619
0
      au = "GENERIC";
1620
0
      break;
1621
1622
0
    default:
1623
0
      au = "unknown";
1624
0
      break;
1625
0
  }
1626
1627
0
  switch (alg_enc) {
1628
0
    case SSL_3DES:
1629
0
      enc = "3DES(168)";
1630
0
      break;
1631
1632
0
    case SSL_AES128:
1633
0
      enc = "AES(128)";
1634
0
      break;
1635
1636
0
    case SSL_AES256:
1637
0
      enc = "AES(256)";
1638
0
      break;
1639
1640
0
    case SSL_AES128GCM:
1641
0
      enc = "AESGCM(128)";
1642
0
      break;
1643
1644
0
    case SSL_AES256GCM:
1645
0
      enc = "AESGCM(256)";
1646
0
      break;
1647
1648
0
    case SSL_CHACHA20POLY1305:
1649
0
      enc = "ChaCha20-Poly1305";
1650
0
      break;
1651
1652
0
    default:
1653
0
      enc = "unknown";
1654
0
      break;
1655
0
  }
1656
1657
0
  switch (alg_mac) {
1658
0
    case SSL_SHA1:
1659
0
      mac = "SHA1";
1660
0
      break;
1661
1662
0
    case SSL_SHA256:
1663
0
      mac = "SHA256";
1664
0
      break;
1665
1666
0
    case SSL_AEAD:
1667
0
      mac = "AEAD";
1668
0
      break;
1669
1670
0
    default:
1671
0
      mac = "unknown";
1672
0
      break;
1673
0
  }
1674
1675
0
  if (buf == NULL) {
1676
0
    len = 128;
1677
0
    buf = (char *)OPENSSL_malloc(len);
1678
0
    if (buf == NULL) {
1679
0
      return NULL;
1680
0
    }
1681
0
  } else if (len < 128) {
1682
0
    return "Buffer too small";
1683
0
  }
1684
1685
0
  BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1686
0
               cipher->name, kx, au, enc, mac);
1687
0
  return buf;
1688
0
}
1689
1690
0
const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1691
0
  return "TLSv1/SSLv3";
1692
0
}
1693
1694
0
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; }
1695
1696
0
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
1697
1698
0
const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
1699
1700
0
const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; }
1701
1702
0
int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; }
1703
1704
0
void SSL_COMP_free_compression_methods(void) {}
1705
1706
0
size_t SSL_get_all_cipher_names(const char **out, size_t max_out) {
1707
0
  return GetAllNames(out, max_out, MakeConstSpan(&kUnknownCipher, 1),
1708
0
                     &SSL_CIPHER::name, MakeConstSpan(kCiphers));
1709
0
}
1710
1711
0
size_t SSL_get_all_standard_cipher_names(const char **out, size_t max_out) {
1712
0
  return GetAllNames(out, max_out, Span<const char *>(),
1713
0
                     &SSL_CIPHER::standard_name, MakeConstSpan(kCiphers));
1714
0
}