blob: 83dea456ed7e5ea39f30a7289b4f98f5e449edca [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400144#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin71f07942015-04-08 02:36:59 -0400146#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400147#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500148#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500150#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400151#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjamin2ee94aa2015-04-07 22:38:30 -0400153#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400154#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamina1c90a52015-05-30 17:03:14 -0400157/* kCiphers is an array of all supported ciphers, sorted by id. */
David Benjamin20c37312015-11-11 21:33:18 -0800158static const SSL_CIPHER kCiphers[] = {
David Benjamina1c90a52015-05-30 17:03:14 -0400159 /* The RSA ciphers */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700160 /* Cipher 02 */
161 {
David Benjaminff2df332015-11-18 10:01:16 -0500162 SSL3_TXT_RSA_NULL_SHA,
163 SSL3_CK_RSA_NULL_SHA,
164 SSL_kRSA,
165 SSL_aRSA,
166 SSL_eNULL,
167 SSL_SHA1,
168 SSL_HANDSHAKE_MAC_DEFAULT,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700169 },
170
David Benjamina1c90a52015-05-30 17:03:14 -0400171 /* Cipher 0A */
172 {
David Benjaminff2df332015-11-18 10:01:16 -0500173 SSL3_TXT_RSA_DES_192_CBC3_SHA,
174 SSL3_CK_RSA_DES_192_CBC3_SHA,
175 SSL_kRSA,
176 SSL_aRSA,
177 SSL_3DES,
178 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500179 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400180 },
181
182
183 /* New AES ciphersuites */
184
185 /* Cipher 2F */
186 {
David Benjaminff2df332015-11-18 10:01:16 -0500187 TLS1_TXT_RSA_WITH_AES_128_SHA,
188 TLS1_CK_RSA_WITH_AES_128_SHA,
189 SSL_kRSA,
190 SSL_aRSA,
191 SSL_AES128,
192 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500193 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400194 },
195
David Benjamina1c90a52015-05-30 17:03:14 -0400196 /* Cipher 35 */
197 {
David Benjaminff2df332015-11-18 10:01:16 -0500198 TLS1_TXT_RSA_WITH_AES_256_SHA,
199 TLS1_CK_RSA_WITH_AES_256_SHA,
200 SSL_kRSA,
201 SSL_aRSA,
202 SSL_AES256,
203 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500204 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400205 },
206
David Benjamina1c90a52015-05-30 17:03:14 -0400207
208 /* TLS v1.2 ciphersuites */
209
210 /* Cipher 3C */
211 {
David Benjaminff2df332015-11-18 10:01:16 -0500212 TLS1_TXT_RSA_WITH_AES_128_SHA256,
213 TLS1_CK_RSA_WITH_AES_128_SHA256,
214 SSL_kRSA,
215 SSL_aRSA,
216 SSL_AES128,
217 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500218 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400219 },
220
221 /* Cipher 3D */
222 {
David Benjaminff2df332015-11-18 10:01:16 -0500223 TLS1_TXT_RSA_WITH_AES_256_SHA256,
224 TLS1_CK_RSA_WITH_AES_256_SHA256,
225 SSL_kRSA,
226 SSL_aRSA,
227 SSL_AES256,
228 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500229 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400230 },
231
Adam Langley85bc5602015-06-09 09:54:04 -0700232 /* PSK cipher suites. */
233
David Benjamina1c90a52015-05-30 17:03:14 -0400234 /* Cipher 8C */
235 {
David Benjaminff2df332015-11-18 10:01:16 -0500236 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
237 TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
238 SSL_kPSK,
239 SSL_aPSK,
240 SSL_AES128,
241 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500242 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400243 },
244
245 /* Cipher 8D */
246 {
David Benjaminff2df332015-11-18 10:01:16 -0500247 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
248 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
249 SSL_kPSK,
250 SSL_aPSK,
251 SSL_AES256,
252 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500253 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400254 },
255
David Benjamina1c90a52015-05-30 17:03:14 -0400256 /* GCM ciphersuites from RFC5288 */
257
258 /* Cipher 9C */
259 {
260 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500261 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
262 SSL_kRSA,
263 SSL_aRSA,
264 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500265 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400266 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400267 },
268
269 /* Cipher 9D */
270 {
271 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500272 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
273 SSL_kRSA,
274 SSL_aRSA,
275 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500276 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400277 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400278 },
279
Steven Valdez803c77a2016-09-06 14:13:43 -0400280 /* TLS 1.3 suites. */
281
282 /* Cipher 1301 */
283 {
284 TLS1_TXT_AES_128_GCM_SHA256,
285 TLS1_CK_AES_128_GCM_SHA256,
286 SSL_kGENERIC,
287 SSL_aGENERIC,
288 SSL_AES128GCM,
289 SSL_AEAD,
290 SSL_HANDSHAKE_MAC_SHA256,
291 },
292
293 /* Cipher 1302 */
294 {
295 TLS1_TXT_AES_256_GCM_SHA384,
296 TLS1_CK_AES_256_GCM_SHA384,
297 SSL_kGENERIC,
298 SSL_aGENERIC,
299 SSL_AES256GCM,
300 SSL_AEAD,
301 SSL_HANDSHAKE_MAC_SHA384,
302 },
303
304 /* Cipher 1303 */
305 {
306 TLS1_TXT_CHACHA20_POLY1305_SHA256,
307 TLS1_CK_CHACHA20_POLY1305_SHA256,
308 SSL_kGENERIC,
309 SSL_aGENERIC,
310 SSL_CHACHA20POLY1305,
311 SSL_AEAD,
312 SSL_HANDSHAKE_MAC_SHA256,
313 },
314
David Benjamina1c90a52015-05-30 17:03:14 -0400315 /* Cipher C009 */
316 {
317 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500318 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
319 SSL_kECDHE,
320 SSL_aECDSA,
321 SSL_AES128,
322 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500323 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400324 },
325
326 /* Cipher C00A */
327 {
328 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500329 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
330 SSL_kECDHE,
331 SSL_aECDSA,
332 SSL_AES256,
333 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500334 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400335 },
336
David Benjamina1c90a52015-05-30 17:03:14 -0400337 /* Cipher C013 */
338 {
339 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500340 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
341 SSL_kECDHE,
342 SSL_aRSA,
343 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500344 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500345 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400346 },
347
348 /* Cipher C014 */
349 {
350 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500351 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
352 SSL_kECDHE,
353 SSL_aRSA,
354 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500355 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500356 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400357 },
358
359
360 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
361
362 /* Cipher C023 */
363 {
364 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500365 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
366 SSL_kECDHE,
367 SSL_aECDSA,
368 SSL_AES128,
369 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500370 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400371 },
372
373 /* Cipher C024 */
374 {
375 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500376 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
377 SSL_kECDHE,
378 SSL_aECDSA,
379 SSL_AES256,
380 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500381 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400382 },
383
384 /* Cipher C027 */
385 {
386 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500387 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
388 SSL_kECDHE,
389 SSL_aRSA,
390 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500391 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500392 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400393 },
394
395 /* Cipher C028 */
396 {
397 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500398 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
399 SSL_kECDHE,
400 SSL_aRSA,
401 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500402 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500403 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400404 },
405
406
407 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
408
409 /* Cipher C02B */
410 {
411 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500412 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
413 SSL_kECDHE,
414 SSL_aECDSA,
415 SSL_AES128GCM,
416 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400417 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400418 },
419
420 /* Cipher C02C */
421 {
422 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500423 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
424 SSL_kECDHE,
425 SSL_aECDSA,
426 SSL_AES256GCM,
427 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400428 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400429 },
430
431 /* Cipher C02F */
432 {
433 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500434 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
435 SSL_kECDHE,
436 SSL_aRSA,
437 SSL_AES128GCM,
438 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400439 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400440 },
441
442 /* Cipher C030 */
443 {
444 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500445 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
446 SSL_kECDHE,
447 SSL_aRSA,
448 SSL_AES256GCM,
449 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400450 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400451 },
452
Adam Langley85bc5602015-06-09 09:54:04 -0700453 /* ECDHE-PSK cipher suites. */
454
455 /* Cipher C035 */
456 {
457 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
458 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500459 SSL_kECDHE,
460 SSL_aPSK,
461 SSL_AES128,
462 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500463 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700464 },
465
466 /* Cipher C036 */
467 {
468 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
469 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500470 SSL_kECDHE,
471 SSL_aPSK,
472 SSL_AES256,
473 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500474 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700475 },
476
477 /* ChaCha20-Poly1305 cipher suites. */
478
David Benjamin13414b32015-12-09 23:02:39 -0500479 /* Cipher CCA8 */
480 {
481 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
482 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
483 SSL_kECDHE,
484 SSL_aRSA,
485 SSL_CHACHA20POLY1305,
486 SSL_AEAD,
487 SSL_HANDSHAKE_MAC_SHA256,
488 },
489
490 /* Cipher CCA9 */
491 {
492 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
493 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
494 SSL_kECDHE,
495 SSL_aECDSA,
496 SSL_CHACHA20POLY1305,
497 SSL_AEAD,
498 SSL_HANDSHAKE_MAC_SHA256,
499 },
500
501 /* Cipher CCAB */
502 {
503 TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
504 TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
505 SSL_kECDHE,
506 SSL_aPSK,
507 SSL_CHACHA20POLY1305,
508 SSL_AEAD,
509 SSL_HANDSHAKE_MAC_SHA256,
510 },
Matt Braithwaite053931e2016-05-25 12:06:05 -0700511
David Benjamina1c90a52015-05-30 17:03:14 -0400512};
513
Steven Valdezcb966542016-08-17 16:56:14 -0400514static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
David Benjamina1c90a52015-05-30 17:03:14 -0400515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516#define CIPHER_ADD 1
517#define CIPHER_KILL 2
518#define CIPHER_DEL 3
519#define CIPHER_ORD 4
520#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522typedef struct cipher_order_st {
523 const SSL_CIPHER *cipher;
524 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 int in_group;
526 struct cipher_order_st *next, *prev;
527} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamin0344daf2015-04-08 02:08:01 -0400529typedef struct cipher_alias_st {
530 /* name is the name of the cipher alias. */
531 const char *name;
532
533 /* The following fields are bitmasks for the corresponding fields on
534 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
535 * bit corresponding to the cipher's value is set to 1. If any bitmask is
536 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
537 uint32_t algorithm_mkey;
538 uint32_t algorithm_auth;
539 uint32_t algorithm_enc;
540 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500541
542 /* min_version, if non-zero, matches all ciphers which were added in that
543 * particular protocol version. */
544 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400545} CIPHER_ALIAS;
546
David Benjamina1c90a52015-05-30 17:03:14 -0400547static const CIPHER_ALIAS kCipherAliases[] = {
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800548 /* "ALL" doesn't include eNULL. It must be explicitly enabled. */
549 {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamina1c90a52015-05-30 17:03:14 -0400551 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamina1c90a52015-05-30 17:03:14 -0400553 /* key exchange aliases
554 * (some of those using only a single bit here combine
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700555 * multiple key exchange algs according to the RFCs. */
David Benjamind6e9eec2015-11-18 09:48:55 -0500556 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamind6e9eec2015-11-18 09:48:55 -0500558 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
559 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
560 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700561
David Benjamind6e9eec2015-11-18 09:48:55 -0500562 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamina1c90a52015-05-30 17:03:14 -0400564 /* server authentication aliases */
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800565 {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
566 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
567 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500568 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamina1c90a52015-05-30 17:03:14 -0400570 /* aliases combining key exchange and server authentication */
David Benjamind6e9eec2015-11-18 09:48:55 -0500571 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
572 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
573 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
574 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700575
David Benjamina1c90a52015-05-30 17:03:14 -0400576 /* symmetric encryption aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500577 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500578 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800579 {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
580 {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
581 {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
Adam Langley2e839242017-01-19 15:12:44 -0800582 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700583
David Benjamina1c90a52015-05-30 17:03:14 -0400584 /* MAC aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500585 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
586 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800587 {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
588 {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjamindcb6ef02015-11-06 15:35:54 -0500590 /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
591 * same as "SSLv3". */
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800592 {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
593 {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
594 {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700595
David Benjamind6e9eec2015-11-18 09:48:55 -0500596 /* Legacy strength classes. */
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800597 {"HIGH", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
598 {"FIPS", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800599};
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Steven Valdezcb966542016-08-17 16:56:14 -0400601static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
David Benjamina1c90a52015-05-30 17:03:14 -0400602
603static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
604 const SSL_CIPHER *a = in_a;
605 const SSL_CIPHER *b = in_b;
606
607 if (a->id > b->id) {
608 return 1;
609 } else if (a->id < b->id) {
610 return -1;
611 } else {
612 return 0;
613 }
614}
615
David Benjamina1c90a52015-05-30 17:03:14 -0400616const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
617 SSL_CIPHER c;
618
619 c.id = 0x03000000L | value;
620 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
621 ssl_cipher_id_cmp);
622}
David Benjamin0344daf2015-04-08 02:08:01 -0400623
David Benjaminea72bd02014-12-21 21:27:41 -0500624int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
625 size_t *out_mac_secret_len,
626 size_t *out_fixed_iv_len,
627 const SSL_CIPHER *cipher, uint16_t version) {
628 *out_aead = NULL;
629 *out_mac_secret_len = 0;
630 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700631
David Benjamin305e6fb2016-10-27 18:19:00 -0400632 if (cipher->algorithm_mac == SSL_AEAD) {
633 if (cipher->algorithm_enc == SSL_AES128GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500634 *out_aead = EVP_aead_aes_128_gcm();
635 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400636 } else if (cipher->algorithm_enc == SSL_AES256GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500637 *out_aead = EVP_aead_aes_256_gcm();
638 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400639 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
David Benjamin13414b32015-12-09 23:02:39 -0500640 *out_aead = EVP_aead_chacha20_poly1305();
641 *out_fixed_iv_len = 12;
David Benjamin305e6fb2016-10-27 18:19:00 -0400642 } else {
David Benjaminea72bd02014-12-21 21:27:41 -0500643 return 0;
David Benjamin305e6fb2016-10-27 18:19:00 -0400644 }
645
646 /* In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
647 * above computes the TLS 1.2 construction. */
648 if (version >= TLS1_3_VERSION) {
649 *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
650 }
651 } else if (cipher->algorithm_mac == SSL_SHA1) {
652 if (cipher->algorithm_enc == SSL_eNULL) {
653 if (version == SSL3_VERSION) {
654 *out_aead = EVP_aead_null_sha1_ssl3();
655 } else {
656 *out_aead = EVP_aead_null_sha1_tls();
657 }
658 } else if (cipher->algorithm_enc == SSL_3DES) {
659 if (version == SSL3_VERSION) {
660 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
661 *out_fixed_iv_len = 8;
662 } else if (version == TLS1_VERSION) {
663 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
664 *out_fixed_iv_len = 8;
665 } else {
666 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
667 }
668 } else if (cipher->algorithm_enc == SSL_AES128) {
669 if (version == SSL3_VERSION) {
670 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
671 *out_fixed_iv_len = 16;
672 } else if (version == TLS1_VERSION) {
673 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
674 *out_fixed_iv_len = 16;
675 } else {
676 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
677 }
678 } else if (cipher->algorithm_enc == SSL_AES256) {
679 if (version == SSL3_VERSION) {
680 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
681 *out_fixed_iv_len = 16;
682 } else if (version == TLS1_VERSION) {
683 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
684 *out_fixed_iv_len = 16;
685 } else {
686 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
687 }
688 } else {
689 return 0;
690 }
691
692 *out_mac_secret_len = SHA_DIGEST_LENGTH;
693 } else if (cipher->algorithm_mac == SSL_SHA256) {
694 if (cipher->algorithm_enc == SSL_AES128) {
695 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
696 } else if (cipher->algorithm_enc == SSL_AES256) {
697 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
698 } else {
699 return 0;
700 }
701
702 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
703 } else if (cipher->algorithm_mac == SSL_SHA384) {
704 if (cipher->algorithm_enc != SSL_AES256) {
705 return 0;
706 }
707
708 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
709 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
710 } else {
711 return 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500712 }
Steven Valdez79750562016-06-16 06:38:04 -0400713
Steven Valdez79750562016-06-16 06:38:04 -0400714 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800715}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700716
Steven Valdez908ac192017-01-12 13:17:07 -0500717const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf,
718 uint16_t version) {
David Benjaminb0883312015-08-06 09:54:13 -0400719 switch (algorithm_prf) {
720 case SSL_HANDSHAKE_MAC_DEFAULT:
Steven Valdez908ac192017-01-12 13:17:07 -0500721 return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
David Benjaminb0883312015-08-06 09:54:13 -0400722 case SSL_HANDSHAKE_MAC_SHA256:
723 return EVP_sha256();
724 case SSL_HANDSHAKE_MAC_SHA384:
725 return EVP_sha384();
726 default:
727 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 }
Adam Langley95c29f32014-06-20 12:00:00 -0700729}
730
731#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700733
David Benjamin0344daf2015-04-08 02:08:01 -0400734/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
735 * |buf_len| bytes at |buf|. */
736static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
737 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
738 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
739}
740
Adam Langley95c29f32014-06-20 12:00:00 -0700741static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 CIPHER_ORDER **tail) {
743 if (curr == *tail) {
744 return;
745 }
746 if (curr == *head) {
747 *head = curr->next;
748 }
749 if (curr->prev != NULL) {
750 curr->prev->next = curr->next;
751 }
752 if (curr->next != NULL) {
753 curr->next->prev = curr->prev;
754 }
755 (*tail)->next = curr;
756 curr->prev = *tail;
757 curr->next = NULL;
758 *tail = curr;
759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
761static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 CIPHER_ORDER **tail) {
763 if (curr == *head) {
764 return;
765 }
766 if (curr == *tail) {
767 *tail = curr->prev;
768 }
769 if (curr->next != NULL) {
770 curr->next->prev = curr->prev;
771 }
772 if (curr->prev != NULL) {
773 curr->prev->next = curr->next;
774 }
775 (*head)->prev = curr;
776 curr->next = *head;
777 curr->prev = NULL;
778 *head = curr;
779}
Adam Langley95c29f32014-06-20 12:00:00 -0700780
David Benjamin82c9e902014-12-12 15:55:27 -0500781static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 CIPHER_ORDER *co_list,
783 CIPHER_ORDER **head_p,
784 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400785 /* The set of ciphers is static, but some subset may be unsupported by
786 * |ssl_method|, so the list may be smaller. */
787 size_t co_list_num = 0;
David Benjamin54091232016-09-05 12:47:25 -0400788 for (size_t i = 0; i < kCiphersLen; i++) {
David Benjamina1c90a52015-05-30 17:03:14 -0400789 const SSL_CIPHER *cipher = &kCiphers[i];
David Benjaminabbbee12016-10-31 19:20:42 -0400790 if (ssl_method->supports_cipher(cipher) &&
791 /* TLS 1.3 ciphers do not participate in this mechanism. */
792 cipher->algorithm_mkey != SSL_kGENERIC) {
David Benjamina1c90a52015-05-30 17:03:14 -0400793 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 co_list[co_list_num].next = NULL;
795 co_list[co_list_num].prev = NULL;
796 co_list[co_list_num].active = 0;
797 co_list[co_list_num].in_group = 0;
798 co_list_num++;
799 }
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 /* Prepare linked list from list entries. */
803 if (co_list_num > 0) {
804 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 if (co_list_num > 1) {
807 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700808
David Benjamin54091232016-09-05 12:47:25 -0400809 for (size_t i = 1; i < co_list_num - 1; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 co_list[i].prev = &co_list[i - 1];
811 co_list[i].next = &co_list[i + 1];
812 }
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
815 }
816
817 co_list[co_list_num - 1].next = NULL;
818
819 *head_p = &co_list[0];
820 *tail_p = &co_list[co_list_num - 1];
821 }
822}
Adam Langley95c29f32014-06-20 12:00:00 -0700823
David Benjamin0344daf2015-04-08 02:08:01 -0400824/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
825 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
826 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
827 *
828 * - If |cipher_id| is non-zero, only that cipher is selected.
829 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
830 * of that strength.
David Benjamind6e9eec2015-11-18 09:48:55 -0500831 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
David Benjamindcb6ef02015-11-06 15:35:54 -0500832 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800833static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400834 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500835 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
836 int strength_bits, int in_group, CIPHER_ORDER **head_p,
837 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 CIPHER_ORDER *head, *tail, *curr, *next, *last;
839 const SSL_CIPHER *cp;
840 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700841
David Benjamindcb6ef02015-11-06 15:35:54 -0500842 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500843 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400844 /* The rule matches nothing, so bail early. */
845 return;
846 }
847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 if (rule == CIPHER_DEL) {
849 /* needed to maintain sorting between currently deleted ciphers */
850 reverse = 1;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 head = *head_p;
854 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (reverse) {
857 next = tail;
858 last = head;
859 } else {
860 next = head;
861 last = tail;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 curr = NULL;
865 for (;;) {
866 if (curr == last) {
867 break;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 curr = next;
871 if (curr == NULL) {
872 break;
873 }
Adam Langleye3142a72014-07-24 17:56:48 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 next = reverse ? curr->prev : curr->next;
876 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700877
David Benjamin0344daf2015-04-08 02:08:01 -0400878 /* Selection criteria is either a specific cipher, the value of
879 * |strength_bits|, or the algorithms used. */
880 if (cipher_id != 0) {
881 if (cipher_id != cp->id) {
882 continue;
883 }
884 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500885 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 continue;
887 }
David Benjamin881f1962016-08-10 18:29:12 -0400888 } else {
889 if (!(alg_mkey & cp->algorithm_mkey) ||
890 !(alg_auth & cp->algorithm_auth) ||
891 !(alg_enc & cp->algorithm_enc) ||
892 !(alg_mac & cp->algorithm_mac) ||
893 (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
894 continue;
895 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800896 }
Adam Langleye3142a72014-07-24 17:56:48 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 /* add the cipher if it has not been added yet. */
899 if (rule == CIPHER_ADD) {
900 /* reverse == 0 */
901 if (!curr->active) {
902 ll_append_tail(&head, curr, &tail);
903 curr->active = 1;
904 curr->in_group = in_group;
905 }
906 }
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 /* Move the added cipher to this location */
909 else if (rule == CIPHER_ORD) {
910 /* reverse == 0 */
911 if (curr->active) {
912 ll_append_tail(&head, curr, &tail);
913 curr->in_group = 0;
914 }
915 } else if (rule == CIPHER_DEL) {
916 /* reverse == 1 */
917 if (curr->active) {
918 /* most recently deleted ciphersuites get best positions
919 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
920 * works in reverse to maintain the order) */
921 ll_append_head(&head, curr, &tail);
922 curr->active = 0;
923 curr->in_group = 0;
924 }
925 } else if (rule == CIPHER_KILL) {
926 /* reverse == 0 */
927 if (head == curr) {
928 head = curr->next;
929 } else {
930 curr->prev->next = curr->next;
931 }
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 if (tail == curr) {
934 tail = curr->prev;
935 }
936 curr->active = 0;
937 if (curr->next != NULL) {
938 curr->next->prev = curr->prev;
939 }
940 if (curr->prev != NULL) {
941 curr->prev->next = curr->next;
942 }
943 curr->next = NULL;
944 curr->prev = NULL;
945 }
946 }
Adam Langley95c29f32014-06-20 12:00:00 -0700947
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 *head_p = head;
949 *tail_p = tail;
950}
Adam Langley95c29f32014-06-20 12:00:00 -0700951
952static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 CIPHER_ORDER **tail_p) {
954 int max_strength_bits, i, *number_uses;
955 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 /* This routine sorts the ciphers with descending strength. The sorting must
958 * keep the pre-sorted sequence, so we apply the normal sorting routine as
959 * '+' movement to the end of the list. */
960 max_strength_bits = 0;
961 curr = *head_p;
962 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500963 if (curr->active &&
964 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
965 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 }
967 curr = curr->next;
968 }
Adam Langley95c29f32014-06-20 12:00:00 -0700969
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
971 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400972 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 return 0;
974 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500975 OPENSSL_memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 /* Now find the strength_bits values actually used. */
978 curr = *head_p;
979 while (curr != NULL) {
980 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500981 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 }
983 curr = curr->next;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 /* Go through the list of used strength_bits values in descending order. */
987 for (i = max_strength_bits; i >= 0; i--) {
988 if (number_uses[i] > 0) {
David Benjamind6e9eec2015-11-18 09:48:55 -0500989 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 }
991 }
992
993 OPENSSL_free(number_uses);
994 return 1;
995}
Adam Langley95c29f32014-06-20 12:00:00 -0700996
David Benjamin0344daf2015-04-08 02:08:01 -0400997static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
998 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 CIPHER_ORDER **head_p,
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001000 CIPHER_ORDER **tail_p, int strict) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001001 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001002 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 const char *l, *buf;
Adam Langleyf139c992016-10-02 09:56:09 -07001004 int multi, skip_rule, rule, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001005 size_t j, buf_len;
1006 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 l = rule_str;
1010 for (;;) {
1011 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 if (ch == '\0') {
1014 break; /* done */
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 if (in_group) {
1018 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 if (*tail_p) {
1020 (*tail_p)->in_group = 0;
1021 }
1022 in_group = 0;
1023 l++;
1024 continue;
1025 }
David Benjamin37d92462014-09-20 17:54:24 -04001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 if (ch == '|') {
1028 rule = CIPHER_ADD;
1029 l++;
1030 continue;
1031 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1032 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001033 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001034 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 } else {
1036 rule = CIPHER_ADD;
1037 }
1038 } else if (ch == '-') {
1039 rule = CIPHER_DEL;
1040 l++;
1041 } else if (ch == '+') {
1042 rule = CIPHER_ORD;
1043 l++;
1044 } else if (ch == '!') {
1045 rule = CIPHER_KILL;
1046 l++;
1047 } else if (ch == '@') {
1048 rule = CIPHER_SPECIAL;
1049 l++;
1050 } else if (ch == '[') {
1051 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001053 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 }
1055 in_group = 1;
1056 has_group = 1;
1057 l++;
1058 continue;
1059 } else {
1060 rule = CIPHER_ADD;
1061 }
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 /* If preference groups are enabled, the only legal operator is +.
1064 * Otherwise the in_group bits will get mixed up. */
1065 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
Adam Langleyf139c992016-10-02 09:56:09 -07001067 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 if (ITEM_SEP(ch)) {
1071 l++;
1072 continue;
1073 }
Adam Langley95c29f32014-06-20 12:00:00 -07001074
David Benjamin0344daf2015-04-08 02:08:01 -04001075 multi = 0;
1076 cipher_id = 0;
1077 alg_mkey = ~0u;
1078 alg_auth = ~0u;
1079 alg_enc = ~0u;
1080 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001081 min_version = 0;
1082 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 for (;;) {
1085 ch = *l;
1086 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001087 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1089 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1090 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001091 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 }
Adam Langley95c29f32014-06-20 12:00:00 -07001093
David Benjamin0344daf2015-04-08 02:08:01 -04001094 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 /* We hit something we cannot deal with, it is no command or separator
1096 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001097 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf99f2442016-10-02 09:53:38 -07001098 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 break;
1103 }
David Benjamin0344daf2015-04-08 02:08:01 -04001104
1105 /* Look for a matching exact cipher. These aren't allowed in multipart
1106 * rules. */
1107 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001108 for (j = 0; j < kCiphersLen; j++) {
1109 const SSL_CIPHER *cipher = &kCiphers[j];
1110 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001111 cipher_id = cipher->id;
1112 break;
1113 }
1114 }
1115 }
1116 if (cipher_id == 0) {
1117 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001118 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001119 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1120 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1121 alg_auth &= kCipherAliases[j].algorithm_auth;
1122 alg_enc &= kCipherAliases[j].algorithm_enc;
1123 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001124
1125 if (min_version != 0 &&
1126 min_version != kCipherAliases[j].min_version) {
1127 skip_rule = 1;
1128 } else {
1129 min_version = kCipherAliases[j].min_version;
1130 }
David Benjamin0344daf2015-04-08 02:08:01 -04001131 break;
1132 }
1133 }
David Benjamina1c90a52015-05-30 17:03:14 -04001134 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001135 skip_rule = 1;
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001136 if (strict) {
1137 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
1138 return 0;
1139 }
David Benjamin0344daf2015-04-08 02:08:01 -04001140 }
1141 }
1142
1143 /* Check for a multipart rule. */
1144 if (ch != '+') {
1145 break;
1146 }
1147 l++;
1148 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 /* Ok, we have the rule, now apply it. */
1152 if (rule == CIPHER_SPECIAL) {
1153 /* special command */
1154 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001155 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 ok = ssl_cipher_strength_sort(head_p, tail_p);
1157 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001158 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if (ok == 0) {
Adam Langleyf139c992016-10-02 09:56:09 -07001162 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 /* We do not support any "multi" options together with "@", so throw away
1166 * the rest of the command, if any left, until end or ':' is found. */
1167 while (*l != '\0' && !ITEM_SEP(*l)) {
1168 l++;
1169 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001170 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001172 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 }
1174 }
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001177 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf139c992016-10-02 09:56:09 -07001178 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langleyf139c992016-10-02 09:56:09 -07001181 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001182}
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001184int ssl_create_cipher_list(
1185 const SSL_PROTOCOL_METHOD *ssl_method,
1186 struct ssl_cipher_preference_list_st **out_cipher_list,
1187 const char *rule_str, int strict) {
David Benjamind2cb1c12016-11-02 17:49:09 -04001188 STACK_OF(SSL_CIPHER) *cipherstack = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 uint8_t *in_group_flags = NULL;
1191 unsigned int num_in_group_flags = 0;
1192 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001195 if (rule_str == NULL || out_cipher_list == NULL) {
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001196 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 }
David Benjamin5213df42014-08-20 14:19:54 -04001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 /* Now we have to collect the available ciphers from the compiled in ciphers.
1200 * We cannot get more than the number compiled in, so it is used for
1201 * allocation. */
Brian Smith5ba06892016-02-07 09:36:04 -10001202 co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001204 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001205 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
David Benjamina1c90a52015-05-30 17:03:14 -04001208 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001209
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 /* Now arrange all ciphers by preference:
1211 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001212
David Benjaminabbbee12016-10-31 19:20:42 -04001213 /* Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
1214 * key exchange mechanisms */
David Benjamind6e9eec2015-11-18 09:48:55 -05001215 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001217 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
1218 &head, &tail);
Steven Valdez803c77a2016-09-06 14:13:43 -04001219 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1220 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1223 * CHACHA20 unless there is hardware support for fast and constant-time
David Benjamin13414b32015-12-09 23:02:39 -05001224 * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
1225 * old one. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 if (EVP_has_aes_hardware()) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001227 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1228 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001229 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1230 &head, &tail);
David Benjamin13414b32015-12-09 23:02:39 -05001231 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1232 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 } else {
David Benjamin13414b32015-12-09 23:02:39 -05001234 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1235 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001236 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1237 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001238 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1239 &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 }
Adam Langley95c29f32014-06-20 12:00:00 -07001241
David Benjamin43336652016-03-03 15:32:29 -05001242 /* Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001243 * 3DES_EDE_CBC_SHA. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001244 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
1245 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001246 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
1247 &head, &tail);
1248 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1249 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001250
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 /* Temporarily enable everything else for sorting */
David Benjamind6e9eec2015-11-18 09:48:55 -05001252 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1253 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 /* Move ciphers without forward secrecy to the end. */
Steven Valdez803c77a2016-09-06 14:13:43 -04001256 ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001257 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 /* Now disable everything (maintaining the ordering!) */
David Benjamind6e9eec2015-11-18 09:48:55 -05001260 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1261 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 /* If the rule_string begins with DEFAULT, apply the default rule before
1264 * using the (possibly available) additional rules. */
David Benjamin11a7b3c2016-11-03 17:03:48 -04001265 const char *rule_p = rule_str;
Adam Langleyfcf25832014-12-18 17:42:32 -08001266 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin11a7b3c2016-11-03 17:03:48 -04001267 if (!ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001268 &tail, strict)) {
David Benjamin11a7b3c2016-11-03 17:03:48 -04001269 goto err;
1270 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 rule_p += 7;
1272 if (*rule_p == ':') {
1273 rule_p++;
1274 }
1275 }
Adam Langley858a88d2014-06-20 12:00:00 -07001276
David Benjamin11a7b3c2016-11-03 17:03:48 -04001277 if (*rule_p != '\0' &&
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001278 !ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 goto err;
1280 }
1281
1282 /* Allocate new "cipherstack" for the result, return with error
1283 * if we cannot get one. */
1284 cipherstack = sk_SSL_CIPHER_new_null();
1285 if (cipherstack == NULL) {
1286 goto err;
1287 }
1288
David Benjamina1c90a52015-05-30 17:03:14 -04001289 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 if (!in_group_flags) {
1291 goto err;
1292 }
1293
1294 /* The cipher selection for the list is done. The ciphers are added
1295 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1296 for (curr = head; curr != NULL; curr = curr->next) {
1297 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001298 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1299 goto err;
1300 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 in_group_flags[num_in_group_flags++] = curr->in_group;
1302 }
1303 }
1304 OPENSSL_free(co_list); /* Not needed any longer */
1305 co_list = NULL;
1306
Adam Langleyfcf25832014-12-18 17:42:32 -08001307 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1308 if (!pref_list) {
1309 goto err;
1310 }
1311 pref_list->ciphers = cipherstack;
1312 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1313 if (!pref_list->in_group_flags) {
1314 goto err;
1315 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001316 OPENSSL_memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 OPENSSL_free(in_group_flags);
1318 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001319 if (*out_cipher_list != NULL) {
1320 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 }
David Benjamin71f07942015-04-08 02:36:59 -04001322 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 pref_list = NULL;
1324
David Benjamin91222b82017-03-09 20:10:56 -05001325 /* Configuring an empty cipher list is an error but still updates the
1326 * output. */
1327 if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers) == 0) {
1328 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
1329 return 0;
1330 }
1331
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001332 return 1;
Adam Langley858a88d2014-06-20 12:00:00 -07001333
1334err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001335 OPENSSL_free(co_list);
1336 OPENSSL_free(in_group_flags);
1337 sk_SSL_CIPHER_free(cipherstack);
David Benjamin2755a3e2015-04-22 16:17:58 -04001338 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 OPENSSL_free(pref_list->in_group_flags);
1340 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001341 OPENSSL_free(pref_list);
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001342 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001343}
Adam Langley95c29f32014-06-20 12:00:00 -07001344
David Benjamin71f07942015-04-08 02:36:59 -04001345uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1346
David Benjamina1c90a52015-05-30 17:03:14 -04001347uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1348 uint32_t id = cipher->id;
1349 /* All ciphers are SSLv3. */
1350 assert((id & 0xff000000) == 0x03000000);
1351 return id & 0xffff;
1352}
1353
David Benjamin71f07942015-04-08 02:36:59 -04001354int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1355 return (cipher->algorithm_enc & SSL_AES) != 0;
1356}
1357
David Benjaminef793f42015-11-05 18:16:27 -05001358int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1359 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1360}
1361
David Benjamina211aee2016-02-24 17:18:44 -05001362int SSL_CIPHER_has_SHA256_HMAC(const SSL_CIPHER *cipher) {
1363 return (cipher->algorithm_mac & SSL_SHA256) != 0;
1364}
1365
Alessandro Ghedini48b6b8f2017-05-12 12:53:31 +01001366int SSL_CIPHER_has_SHA384_HMAC(const SSL_CIPHER *cipher) {
1367 return (cipher->algorithm_mac & SSL_SHA384) != 0;
1368}
1369
Alessandro Ghedini0726fb72017-01-17 13:27:08 +00001370int SSL_CIPHER_is_AEAD(const SSL_CIPHER *cipher) {
1371 return (cipher->algorithm_mac & SSL_AEAD) != 0;
1372}
1373
David Benjamin71f07942015-04-08 02:36:59 -04001374int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001375 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001376}
1377
David Benjaminef793f42015-11-05 18:16:27 -05001378int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1379 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1380}
1381
Adam Langleyb00061c2015-11-16 17:44:52 -08001382int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1383 return (cipher->algorithm_enc & SSL_AES128) != 0;
1384}
1385
1386int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1387 return (cipher->algorithm_enc & SSL_AES256) != 0;
1388}
1389
David Benjamin51a01a52015-10-29 13:19:56 -04001390int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
Adam Langley2e839242017-01-19 15:12:44 -08001391 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001392}
1393
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001394int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1395 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1396}
1397
1398int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001399 return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001400 cipher->algorithm_mac != SSL_AEAD;
1401}
1402
David Benjaminef793f42015-11-05 18:16:27 -05001403int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1404 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1405}
1406
David Benjamin4cc36ad2015-12-19 14:23:26 -05001407int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
1408 return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
1409}
1410
David Benjamin745745d2017-01-10 08:34:14 -05001411int SSL_CIPHER_is_static_RSA(const SSL_CIPHER *cipher) {
1412 return (cipher->algorithm_mkey & SSL_kRSA) != 0;
1413}
1414
David Benjaminef793f42015-11-05 18:16:27 -05001415uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001416 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1417 cipher->algorithm_auth == SSL_aGENERIC) {
1418 return TLS1_3_VERSION;
1419 }
1420
David Benjamindcb6ef02015-11-06 15:35:54 -05001421 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1422 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1423 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001424 return TLS1_2_VERSION;
1425 }
1426 return SSL3_VERSION;
1427}
1428
Nick Harper1fd39d82016-06-14 18:14:35 -07001429uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001430 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1431 cipher->algorithm_auth == SSL_aGENERIC) {
Nick Harper1fd39d82016-06-14 18:14:35 -07001432 return TLS1_3_VERSION;
1433 }
1434 return TLS1_2_VERSION;
1435}
1436
David Benjamin71f07942015-04-08 02:36:59 -04001437/* return the actual cipher being used */
1438const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1439 if (cipher != NULL) {
1440 return cipher->name;
1441 }
1442
1443 return "(NONE)";
1444}
1445
1446const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1447 if (cipher == NULL) {
1448 return "";
1449 }
1450
1451 switch (cipher->algorithm_mkey) {
1452 case SSL_kRSA:
1453 return "RSA";
1454
David Benjamin71f07942015-04-08 02:36:59 -04001455 case SSL_kECDHE:
1456 switch (cipher->algorithm_auth) {
1457 case SSL_aECDSA:
1458 return "ECDHE_ECDSA";
1459 case SSL_aRSA:
1460 return "ECDHE_RSA";
1461 case SSL_aPSK:
1462 return "ECDHE_PSK";
1463 default:
1464 assert(0);
1465 return "UNKNOWN";
1466 }
1467
1468 case SSL_kPSK:
1469 assert(cipher->algorithm_auth == SSL_aPSK);
1470 return "PSK";
1471
Steven Valdez803c77a2016-09-06 14:13:43 -04001472 case SSL_kGENERIC:
1473 assert(cipher->algorithm_auth == SSL_aGENERIC);
1474 return "GENERIC";
1475
David Benjamin71f07942015-04-08 02:36:59 -04001476 default:
1477 assert(0);
1478 return "UNKNOWN";
1479 }
1480}
1481
1482static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1483 switch (cipher->algorithm_enc) {
1484 case SSL_3DES:
1485 return "3DES_EDE_CBC";
David Benjamin71f07942015-04-08 02:36:59 -04001486 case SSL_AES128:
1487 return "AES_128_CBC";
1488 case SSL_AES256:
1489 return "AES_256_CBC";
1490 case SSL_AES128GCM:
1491 return "AES_128_GCM";
1492 case SSL_AES256GCM:
1493 return "AES_256_GCM";
David Benjamin13414b32015-12-09 23:02:39 -05001494 case SSL_CHACHA20POLY1305:
David Benjamin71f07942015-04-08 02:36:59 -04001495 return "CHACHA20_POLY1305";
1496 break;
1497 default:
1498 assert(0);
1499 return "UNKNOWN";
1500 }
1501}
1502
1503static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001504 switch (cipher->algorithm_prf) {
1505 case SSL_HANDSHAKE_MAC_DEFAULT:
David Benjamin5fc99c62017-01-10 08:19:12 -05001506 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which
1507 * is SHA-1 for all supported ciphers. */
1508 assert(cipher->algorithm_mac == SSL_SHA1);
1509 return "SHA";
David Benjaminb0883312015-08-06 09:54:13 -04001510 case SSL_HANDSHAKE_MAC_SHA256:
1511 return "SHA256";
1512 case SSL_HANDSHAKE_MAC_SHA384:
1513 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001514 }
David Benjaminb0883312015-08-06 09:54:13 -04001515 assert(0);
1516 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001517}
1518
1519char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1520 if (cipher == NULL) {
1521 return NULL;
1522 }
1523
1524 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1525 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1526 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1527
Steven Valdez803c77a2016-09-06 14:13:43 -04001528 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name} or
1529 * TLS_{enc_name}_{prf_name} depending on whether the cipher is AEAD-only. */
1530 size_t len = 4 + strlen(enc_name) + 1 + strlen(prf_name) + 1;
1531
1532 if (cipher->algorithm_mkey != SSL_kGENERIC) {
1533 len += strlen(kx_name) + 6;
1534 }
1535
David Benjamin71f07942015-04-08 02:36:59 -04001536 char *ret = OPENSSL_malloc(len);
1537 if (ret == NULL) {
1538 return NULL;
1539 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001540
David Benjamin71f07942015-04-08 02:36:59 -04001541 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001542 (cipher->algorithm_mkey != SSL_kGENERIC &&
1543 (BUF_strlcat(ret, kx_name, len) >= len ||
1544 BUF_strlcat(ret, "_WITH_", len) >= len)) ||
David Benjamin71f07942015-04-08 02:36:59 -04001545 BUF_strlcat(ret, enc_name, len) >= len ||
1546 BUF_strlcat(ret, "_", len) >= len ||
1547 BUF_strlcat(ret, prf_name, len) >= len) {
1548 assert(0);
1549 OPENSSL_free(ret);
1550 return NULL;
1551 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001552
David Benjamin71f07942015-04-08 02:36:59 -04001553 assert(strlen(ret) + 1 == len);
1554 return ret;
1555}
1556
1557int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1558 if (cipher == NULL) {
1559 return 0;
1560 }
1561
David Benjamin9f2e2772015-11-18 09:59:43 -05001562 int alg_bits, strength_bits;
1563 switch (cipher->algorithm_enc) {
1564 case SSL_AES128:
1565 case SSL_AES128GCM:
David Benjamin9f2e2772015-11-18 09:59:43 -05001566 alg_bits = 128;
1567 strength_bits = 128;
1568 break;
1569
1570 case SSL_AES256:
1571 case SSL_AES256GCM:
David Benjamin13414b32015-12-09 23:02:39 -05001572 case SSL_CHACHA20POLY1305:
David Benjamin9f2e2772015-11-18 09:59:43 -05001573 alg_bits = 256;
1574 strength_bits = 256;
1575 break;
1576
1577 case SSL_3DES:
1578 alg_bits = 168;
1579 strength_bits = 112;
1580 break;
1581
1582 case SSL_eNULL:
1583 alg_bits = 0;
1584 strength_bits = 0;
1585 break;
1586
1587 default:
1588 assert(0);
1589 alg_bits = 0;
1590 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001591 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001592
1593 if (out_alg_bits != NULL) {
1594 *out_alg_bits = alg_bits;
1595 }
1596 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001597}
1598
Adam Langleyfcf25832014-12-18 17:42:32 -08001599const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1600 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001602 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 alg_mkey = cipher->algorithm_mkey;
1605 alg_auth = cipher->algorithm_auth;
1606 alg_enc = cipher->algorithm_enc;
1607 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001608
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 switch (alg_mkey) {
1610 case SSL_kRSA:
1611 kx = "RSA";
1612 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001613
David Benjamin7061e282015-03-19 11:10:48 -04001614 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 kx = "ECDH";
1616 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 case SSL_kPSK:
1619 kx = "PSK";
1620 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
Steven Valdez803c77a2016-09-06 14:13:43 -04001622 case SSL_kGENERIC:
1623 kx = "GENERIC";
1624 break;
1625
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 default:
1627 kx = "unknown";
1628 }
Adam Langley95c29f32014-06-20 12:00:00 -07001629
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 switch (alg_auth) {
1631 case SSL_aRSA:
1632 au = "RSA";
1633 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001634
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 case SSL_aECDSA:
1636 au = "ECDSA";
1637 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639 case SSL_aPSK:
1640 au = "PSK";
1641 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001642
Steven Valdez803c77a2016-09-06 14:13:43 -04001643 case SSL_aGENERIC:
1644 au = "GENERIC";
1645 break;
1646
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 default:
1648 au = "unknown";
1649 break;
1650 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 switch (alg_enc) {
1653 case SSL_3DES:
1654 enc = "3DES(168)";
1655 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 case SSL_AES128:
1658 enc = "AES(128)";
1659 break;
1660
1661 case SSL_AES256:
1662 enc = "AES(256)";
1663 break;
1664
1665 case SSL_AES128GCM:
1666 enc = "AESGCM(128)";
1667 break;
1668
1669 case SSL_AES256GCM:
1670 enc = "AESGCM(256)";
1671 break;
1672
David Benjamin13414b32015-12-09 23:02:39 -05001673 case SSL_CHACHA20POLY1305:
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 enc = "ChaCha20-Poly1305";
1675 break;
1676
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001677 case SSL_eNULL:
1678 enc="None";
1679 break;
1680
Adam Langleyfcf25832014-12-18 17:42:32 -08001681 default:
1682 enc = "unknown";
1683 break;
1684 }
1685
1686 switch (alg_mac) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 case SSL_SHA1:
1688 mac = "SHA1";
1689 break;
1690
1691 case SSL_SHA256:
1692 mac = "SHA256";
1693 break;
1694
1695 case SSL_SHA384:
1696 mac = "SHA384";
1697 break;
1698
1699 case SSL_AEAD:
1700 mac = "AEAD";
1701 break;
1702
1703 default:
1704 mac = "unknown";
1705 break;
1706 }
1707
1708 if (buf == NULL) {
1709 len = 128;
1710 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001711 if (buf == NULL) {
1712 return NULL;
1713 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 } else if (len < 128) {
1715 return "Buffer too small";
1716 }
1717
Brian Smith0687bdf2016-01-17 09:18:26 -10001718 BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1719 cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 return buf;
1721}
1722
David Benjamin71f07942015-04-08 02:36:59 -04001723const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1724 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001725}
1726
David Benjamind55bd792017-05-18 11:33:08 -04001727STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001728
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001729int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001730
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001731const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langley3e9e0432016-10-03 15:58:07 -07001733void SSL_COMP_free_compression_methods(void) {}
1734
David Benjamin69522112017-03-28 15:38:29 -05001735uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
1736 switch (EVP_PKEY_id(key)) {
1737 case EVP_PKEY_RSA:
1738 return SSL_aRSA;
1739 case EVP_PKEY_EC:
1740 case EVP_PKEY_ED25519:
1741 /* Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers. */
1742 return SSL_aECDSA;
1743 default:
1744 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001746}
David Benjamin9c651c92014-07-12 13:27:45 -04001747
David Benjaminc032dfa2016-05-12 14:54:57 -04001748int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
1749 return (cipher->algorithm_auth & SSL_aCERT) != 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001750}
1751
Adam Langleyfcf25832014-12-18 17:42:32 -08001752int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1753 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001754 if (cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 return 1;
1756 }
1757
1758 /* It is optional in all others. */
1759 return 0;
1760}
David Benjaminb8d28cf2015-07-28 21:34:45 -04001761
1762size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1763 size_t block_size;
1764 switch (cipher->algorithm_enc) {
1765 case SSL_3DES:
1766 block_size = 8;
1767 break;
1768 case SSL_AES128:
1769 case SSL_AES256:
1770 block_size = 16;
1771 break;
1772 default:
1773 return 0;
1774 }
1775
David Benjamin5fc99c62017-01-10 08:19:12 -05001776 /* All supported TLS 1.0 ciphers use SHA-1. */
1777 assert(cipher->algorithm_mac == SSL_SHA1);
1778 size_t ret = 1 + SHA_DIGEST_LENGTH;
David Benjaminb8d28cf2015-07-28 21:34:45 -04001779 ret += block_size - (ret % block_size);
1780 return ret;
1781}