blob: 9ca7f24a86afbfec6130afa04b47c0f5fa6b5b5a [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
196 /* Cipher 33 */
197 {
David Benjaminff2df332015-11-18 10:01:16 -0500198 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
199 TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
200 SSL_kDHE,
201 SSL_aRSA,
202 SSL_AES128,
203 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500204 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400205 },
206
207 /* Cipher 35 */
208 {
David Benjaminff2df332015-11-18 10:01:16 -0500209 TLS1_TXT_RSA_WITH_AES_256_SHA,
210 TLS1_CK_RSA_WITH_AES_256_SHA,
211 SSL_kRSA,
212 SSL_aRSA,
213 SSL_AES256,
214 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500215 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400216 },
217
218 /* Cipher 39 */
219 {
David Benjaminff2df332015-11-18 10:01:16 -0500220 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
221 TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
222 SSL_kDHE,
223 SSL_aRSA,
224 SSL_AES256,
225 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500226 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400227 },
228
229
230 /* TLS v1.2 ciphersuites */
231
232 /* Cipher 3C */
233 {
David Benjaminff2df332015-11-18 10:01:16 -0500234 TLS1_TXT_RSA_WITH_AES_128_SHA256,
235 TLS1_CK_RSA_WITH_AES_128_SHA256,
236 SSL_kRSA,
237 SSL_aRSA,
238 SSL_AES128,
239 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500240 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400241 },
242
243 /* Cipher 3D */
244 {
David Benjaminff2df332015-11-18 10:01:16 -0500245 TLS1_TXT_RSA_WITH_AES_256_SHA256,
246 TLS1_CK_RSA_WITH_AES_256_SHA256,
247 SSL_kRSA,
248 SSL_aRSA,
249 SSL_AES256,
250 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500251 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400252 },
253
254 /* Cipher 67 */
255 {
256 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500257 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
258 SSL_kDHE,
259 SSL_aRSA,
260 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500261 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500262 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400263 },
264
265 /* Cipher 6B */
266 {
267 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500268 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
269 SSL_kDHE,
270 SSL_aRSA,
271 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500272 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500273 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400274 },
275
Adam Langley85bc5602015-06-09 09:54:04 -0700276 /* PSK cipher suites. */
277
David Benjamina1c90a52015-05-30 17:03:14 -0400278 /* Cipher 8C */
279 {
David Benjaminff2df332015-11-18 10:01:16 -0500280 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
281 TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
282 SSL_kPSK,
283 SSL_aPSK,
284 SSL_AES128,
285 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500286 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400287 },
288
289 /* Cipher 8D */
290 {
David Benjaminff2df332015-11-18 10:01:16 -0500291 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
292 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
293 SSL_kPSK,
294 SSL_aPSK,
295 SSL_AES256,
296 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500297 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400298 },
299
David Benjamina1c90a52015-05-30 17:03:14 -0400300 /* GCM ciphersuites from RFC5288 */
301
302 /* Cipher 9C */
303 {
304 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500305 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
306 SSL_kRSA,
307 SSL_aRSA,
308 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500309 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400310 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400311 },
312
313 /* Cipher 9D */
314 {
315 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500316 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
317 SSL_kRSA,
318 SSL_aRSA,
319 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500320 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400321 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400322 },
323
324 /* Cipher 9E */
325 {
326 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500327 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
328 SSL_kDHE,
329 SSL_aRSA,
330 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500331 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400332 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400333 },
334
335 /* Cipher 9F */
336 {
337 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500338 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
339 SSL_kDHE,
340 SSL_aRSA,
341 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500342 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400343 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400344 },
345
Steven Valdez803c77a2016-09-06 14:13:43 -0400346 /* TLS 1.3 suites. */
347
348 /* Cipher 1301 */
349 {
350 TLS1_TXT_AES_128_GCM_SHA256,
351 TLS1_CK_AES_128_GCM_SHA256,
352 SSL_kGENERIC,
353 SSL_aGENERIC,
354 SSL_AES128GCM,
355 SSL_AEAD,
356 SSL_HANDSHAKE_MAC_SHA256,
357 },
358
359 /* Cipher 1302 */
360 {
361 TLS1_TXT_AES_256_GCM_SHA384,
362 TLS1_CK_AES_256_GCM_SHA384,
363 SSL_kGENERIC,
364 SSL_aGENERIC,
365 SSL_AES256GCM,
366 SSL_AEAD,
367 SSL_HANDSHAKE_MAC_SHA384,
368 },
369
370 /* Cipher 1303 */
371 {
372 TLS1_TXT_CHACHA20_POLY1305_SHA256,
373 TLS1_CK_CHACHA20_POLY1305_SHA256,
374 SSL_kGENERIC,
375 SSL_aGENERIC,
376 SSL_CHACHA20POLY1305,
377 SSL_AEAD,
378 SSL_HANDSHAKE_MAC_SHA256,
379 },
380
Matt Braithwaite053931e2016-05-25 12:06:05 -0700381 /* CECPQ1 (combined elliptic curve + post-quantum) suites. */
382
383 /* Cipher 16B7 */
384 {
385 TLS1_TXT_CECPQ1_RSA_WITH_CHACHA20_POLY1305_SHA256,
386 TLS1_CK_CECPQ1_RSA_WITH_CHACHA20_POLY1305_SHA256,
387 SSL_kCECPQ1,
388 SSL_aRSA,
389 SSL_CHACHA20POLY1305,
390 SSL_AEAD,
391 SSL_HANDSHAKE_MAC_SHA256,
392 },
393
394 /* Cipher 16B8 */
395 {
396 TLS1_TXT_CECPQ1_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
397 TLS1_CK_CECPQ1_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
398 SSL_kCECPQ1,
399 SSL_aECDSA,
400 SSL_CHACHA20POLY1305,
401 SSL_AEAD,
402 SSL_HANDSHAKE_MAC_SHA256,
403 },
404
405 /* Cipher 16B9 */
406 {
407 TLS1_TXT_CECPQ1_RSA_WITH_AES_256_GCM_SHA384,
408 TLS1_CK_CECPQ1_RSA_WITH_AES_256_GCM_SHA384,
409 SSL_kCECPQ1,
410 SSL_aRSA,
411 SSL_AES256GCM,
412 SSL_AEAD,
413 SSL_HANDSHAKE_MAC_SHA384,
414 },
415
416 /* Cipher 16BA */
417 {
418 TLS1_TXT_CECPQ1_ECDSA_WITH_AES_256_GCM_SHA384,
419 TLS1_CK_CECPQ1_ECDSA_WITH_AES_256_GCM_SHA384,
420 SSL_kCECPQ1,
421 SSL_aECDSA,
422 SSL_AES256GCM,
423 SSL_AEAD,
424 SSL_HANDSHAKE_MAC_SHA384,
425 },
426
David Benjamina1c90a52015-05-30 17:03:14 -0400427 /* Cipher C009 */
428 {
429 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500430 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
431 SSL_kECDHE,
432 SSL_aECDSA,
433 SSL_AES128,
434 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500435 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400436 },
437
438 /* Cipher C00A */
439 {
440 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500441 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
442 SSL_kECDHE,
443 SSL_aECDSA,
444 SSL_AES256,
445 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500446 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400447 },
448
David Benjamina1c90a52015-05-30 17:03:14 -0400449 /* Cipher C013 */
450 {
451 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500452 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
453 SSL_kECDHE,
454 SSL_aRSA,
455 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500456 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500457 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400458 },
459
460 /* Cipher C014 */
461 {
462 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500463 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
464 SSL_kECDHE,
465 SSL_aRSA,
466 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500467 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500468 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400469 },
470
471
472 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
473
474 /* Cipher C023 */
475 {
476 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500477 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
478 SSL_kECDHE,
479 SSL_aECDSA,
480 SSL_AES128,
481 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500482 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400483 },
484
485 /* Cipher C024 */
486 {
487 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500488 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
489 SSL_kECDHE,
490 SSL_aECDSA,
491 SSL_AES256,
492 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500493 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400494 },
495
496 /* Cipher C027 */
497 {
498 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500499 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
500 SSL_kECDHE,
501 SSL_aRSA,
502 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500503 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500504 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400505 },
506
507 /* Cipher C028 */
508 {
509 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500510 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
511 SSL_kECDHE,
512 SSL_aRSA,
513 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500514 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500515 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400516 },
517
518
519 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
520
521 /* Cipher C02B */
522 {
523 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500524 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
525 SSL_kECDHE,
526 SSL_aECDSA,
527 SSL_AES128GCM,
528 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400529 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400530 },
531
532 /* Cipher C02C */
533 {
534 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500535 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
536 SSL_kECDHE,
537 SSL_aECDSA,
538 SSL_AES256GCM,
539 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400540 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400541 },
542
543 /* Cipher C02F */
544 {
545 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500546 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
547 SSL_kECDHE,
548 SSL_aRSA,
549 SSL_AES128GCM,
550 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400551 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400552 },
553
554 /* Cipher C030 */
555 {
556 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500557 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
558 SSL_kECDHE,
559 SSL_aRSA,
560 SSL_AES256GCM,
561 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400562 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400563 },
564
Adam Langley85bc5602015-06-09 09:54:04 -0700565 /* ECDHE-PSK cipher suites. */
566
567 /* Cipher C035 */
568 {
569 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
570 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500571 SSL_kECDHE,
572 SSL_aPSK,
573 SSL_AES128,
574 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500575 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700576 },
577
578 /* Cipher C036 */
579 {
580 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
581 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500582 SSL_kECDHE,
583 SSL_aPSK,
584 SSL_AES256,
585 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500586 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700587 },
588
589 /* ChaCha20-Poly1305 cipher suites. */
590
David Benjamin13414b32015-12-09 23:02:39 -0500591#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamina1c90a52015-05-30 17:03:14 -0400592 {
Brian Smith271777f2015-10-03 13:53:33 -1000593 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
David Benjaminff2df332015-11-18 10:01:16 -0500594 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD,
595 SSL_kECDHE,
596 SSL_aRSA,
597 SSL_CHACHA20POLY1305_OLD,
598 SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400599 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400600 },
601
602 {
Brian Smith271777f2015-10-03 13:53:33 -1000603 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
David Benjaminff2df332015-11-18 10:01:16 -0500604 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD,
605 SSL_kECDHE,
606 SSL_aECDSA,
607 SSL_CHACHA20POLY1305_OLD,
608 SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400609 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400610 },
Adam Langleyd98dc132015-09-23 16:41:33 -0700611#endif
David Benjamin13414b32015-12-09 23:02:39 -0500612
613 /* Cipher CCA8 */
614 {
615 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
616 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
617 SSL_kECDHE,
618 SSL_aRSA,
619 SSL_CHACHA20POLY1305,
620 SSL_AEAD,
621 SSL_HANDSHAKE_MAC_SHA256,
622 },
623
624 /* Cipher CCA9 */
625 {
626 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
627 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
628 SSL_kECDHE,
629 SSL_aECDSA,
630 SSL_CHACHA20POLY1305,
631 SSL_AEAD,
632 SSL_HANDSHAKE_MAC_SHA256,
633 },
634
635 /* Cipher CCAB */
636 {
637 TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
638 TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
639 SSL_kECDHE,
640 SSL_aPSK,
641 SSL_CHACHA20POLY1305,
642 SSL_AEAD,
643 SSL_HANDSHAKE_MAC_SHA256,
644 },
Matt Braithwaite053931e2016-05-25 12:06:05 -0700645
David Benjamina1c90a52015-05-30 17:03:14 -0400646};
647
Steven Valdezcb966542016-08-17 16:56:14 -0400648static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
David Benjamina1c90a52015-05-30 17:03:14 -0400649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650#define CIPHER_ADD 1
651#define CIPHER_KILL 2
652#define CIPHER_DEL 3
653#define CIPHER_ORD 4
654#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656typedef struct cipher_order_st {
657 const SSL_CIPHER *cipher;
658 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 int in_group;
660 struct cipher_order_st *next, *prev;
661} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
David Benjamin0344daf2015-04-08 02:08:01 -0400663typedef struct cipher_alias_st {
664 /* name is the name of the cipher alias. */
665 const char *name;
666
667 /* The following fields are bitmasks for the corresponding fields on
668 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
669 * bit corresponding to the cipher's value is set to 1. If any bitmask is
670 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
671 uint32_t algorithm_mkey;
672 uint32_t algorithm_auth;
673 uint32_t algorithm_enc;
674 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500675
676 /* min_version, if non-zero, matches all ciphers which were added in that
677 * particular protocol version. */
678 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400679} CIPHER_ALIAS;
680
David Benjamina1c90a52015-05-30 17:03:14 -0400681static const CIPHER_ALIAS kCipherAliases[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -0700682 /* "ALL" doesn't include eNULL nor kCECPQ1. These must be explicitly
683 * enabled. */
684 {"ALL", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700685
David Benjamina1c90a52015-05-30 17:03:14 -0400686 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700687
David Benjamina1c90a52015-05-30 17:03:14 -0400688 /* key exchange aliases
689 * (some of those using only a single bit here combine
690 * multiple key exchange algs according to the RFCs,
691 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamind6e9eec2015-11-18 09:48:55 -0500692 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700693
David Benjamind6e9eec2015-11-18 09:48:55 -0500694 {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
695 {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
696 {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700697
David Benjamind6e9eec2015-11-18 09:48:55 -0500698 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Matt Braithwaite053931e2016-05-25 12:06:05 -0700699 {"kCECPQ1", SSL_kCECPQ1, ~0u, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500700 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
701 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700702
David Benjamind6e9eec2015-11-18 09:48:55 -0500703 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700704
David Benjamina1c90a52015-05-30 17:03:14 -0400705 /* server authentication aliases */
Matt Braithwaite053931e2016-05-25 12:06:05 -0700706 {"aRSA", ~SSL_kCECPQ1, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
707 {"aECDSA", ~SSL_kCECPQ1, SSL_aECDSA, ~0u, ~0u, 0},
708 {"ECDSA", ~SSL_kCECPQ1, SSL_aECDSA, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500709 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700710
David Benjamina1c90a52015-05-30 17:03:14 -0400711 /* aliases combining key exchange and server authentication */
David Benjamind6e9eec2015-11-18 09:48:55 -0500712 {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
713 {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
714 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
715 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
716 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
717 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700718
David Benjamina1c90a52015-05-30 17:03:14 -0400719 /* symmetric encryption aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500720 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500721 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
Matt Braithwaite053931e2016-05-25 12:06:05 -0700722 {"AES256", ~SSL_kCECPQ1, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
723 {"AES", ~SSL_kCECPQ1, ~0u, SSL_AES, ~0u, 0},
724 {"AESGCM", ~SSL_kCECPQ1, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
725 {"CHACHA20", ~SSL_kCECPQ1, ~0u, SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD, ~0u,
David Benjamin13414b32015-12-09 23:02:39 -0500726 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700727
David Benjamina1c90a52015-05-30 17:03:14 -0400728 /* MAC aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500729 {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0},
730 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
731 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
Matt Braithwaite053931e2016-05-25 12:06:05 -0700732 {"SHA256", ~SSL_kCECPQ1, ~0u, ~0u, SSL_SHA256, 0},
733 {"SHA384", ~SSL_kCECPQ1, ~0u, ~0u, SSL_SHA384, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700734
David Benjamindcb6ef02015-11-06 15:35:54 -0500735 /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
736 * same as "SSLv3". */
Matt Braithwaite053931e2016-05-25 12:06:05 -0700737 {"SSLv3", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
738 {"TLSv1", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
739 {"TLSv1.2", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700740
David Benjamind6e9eec2015-11-18 09:48:55 -0500741 /* Legacy strength classes. */
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -0700742 {"HIGH", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, 0},
743 {"FIPS", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800744};
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Steven Valdezcb966542016-08-17 16:56:14 -0400746static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
David Benjamina1c90a52015-05-30 17:03:14 -0400747
748static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
749 const SSL_CIPHER *a = in_a;
750 const SSL_CIPHER *b = in_b;
751
752 if (a->id > b->id) {
753 return 1;
754 } else if (a->id < b->id) {
755 return -1;
756 } else {
757 return 0;
758 }
759}
760
761static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
762 return ssl_cipher_id_cmp(*a, *b);
763}
764
765const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
766 SSL_CIPHER c;
767
768 c.id = 0x03000000L | value;
769 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
770 ssl_cipher_id_cmp);
771}
David Benjamin0344daf2015-04-08 02:08:01 -0400772
David Benjaminea72bd02014-12-21 21:27:41 -0500773int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
774 size_t *out_mac_secret_len,
775 size_t *out_fixed_iv_len,
776 const SSL_CIPHER *cipher, uint16_t version) {
777 *out_aead = NULL;
778 *out_mac_secret_len = 0;
779 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700780
David Benjamin305e6fb2016-10-27 18:19:00 -0400781 if (cipher->algorithm_mac == SSL_AEAD) {
782 if (cipher->algorithm_enc == SSL_AES128GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500783 *out_aead = EVP_aead_aes_128_gcm();
784 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400785 } else if (cipher->algorithm_enc == SSL_AES256GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500786 *out_aead = EVP_aead_aes_256_gcm();
787 *out_fixed_iv_len = 4;
Adam Langleyd98dc132015-09-23 16:41:33 -0700788#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin305e6fb2016-10-27 18:19:00 -0400789 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD) {
Brian Smith3e23e4c2015-10-03 11:38:58 -1000790 *out_aead = EVP_aead_chacha20_poly1305_old();
David Benjaminea72bd02014-12-21 21:27:41 -0500791 *out_fixed_iv_len = 0;
Adam Langleyd98dc132015-09-23 16:41:33 -0700792#endif
David Benjamin305e6fb2016-10-27 18:19:00 -0400793 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
David Benjamin13414b32015-12-09 23:02:39 -0500794 *out_aead = EVP_aead_chacha20_poly1305();
795 *out_fixed_iv_len = 12;
David Benjamin305e6fb2016-10-27 18:19:00 -0400796 } else {
David Benjaminea72bd02014-12-21 21:27:41 -0500797 return 0;
David Benjamin305e6fb2016-10-27 18:19:00 -0400798 }
799
800 /* In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
801 * above computes the TLS 1.2 construction. */
802 if (version >= TLS1_3_VERSION) {
803 *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
804 }
805 } else if (cipher->algorithm_mac == SSL_SHA1) {
806 if (cipher->algorithm_enc == SSL_eNULL) {
807 if (version == SSL3_VERSION) {
808 *out_aead = EVP_aead_null_sha1_ssl3();
809 } else {
810 *out_aead = EVP_aead_null_sha1_tls();
811 }
812 } else if (cipher->algorithm_enc == SSL_3DES) {
813 if (version == SSL3_VERSION) {
814 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
815 *out_fixed_iv_len = 8;
816 } else if (version == TLS1_VERSION) {
817 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
818 *out_fixed_iv_len = 8;
819 } else {
820 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
821 }
822 } else if (cipher->algorithm_enc == SSL_AES128) {
823 if (version == SSL3_VERSION) {
824 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
825 *out_fixed_iv_len = 16;
826 } else if (version == TLS1_VERSION) {
827 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
828 *out_fixed_iv_len = 16;
829 } else {
830 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
831 }
832 } else if (cipher->algorithm_enc == SSL_AES256) {
833 if (version == SSL3_VERSION) {
834 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
835 *out_fixed_iv_len = 16;
836 } else if (version == TLS1_VERSION) {
837 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
838 *out_fixed_iv_len = 16;
839 } else {
840 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
841 }
842 } else {
843 return 0;
844 }
845
846 *out_mac_secret_len = SHA_DIGEST_LENGTH;
847 } else if (cipher->algorithm_mac == SSL_SHA256) {
848 if (cipher->algorithm_enc == SSL_AES128) {
849 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
850 } else if (cipher->algorithm_enc == SSL_AES256) {
851 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
852 } else {
853 return 0;
854 }
855
856 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
857 } else if (cipher->algorithm_mac == SSL_SHA384) {
858 if (cipher->algorithm_enc != SSL_AES256) {
859 return 0;
860 }
861
862 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
863 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
864 } else {
865 return 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500866 }
Steven Valdez79750562016-06-16 06:38:04 -0400867
Steven Valdez79750562016-06-16 06:38:04 -0400868 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800869}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700870
David Benjaminb0883312015-08-06 09:54:13 -0400871const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
872 switch (algorithm_prf) {
873 case SSL_HANDSHAKE_MAC_DEFAULT:
874 return EVP_sha1();
875 case SSL_HANDSHAKE_MAC_SHA256:
876 return EVP_sha256();
877 case SSL_HANDSHAKE_MAC_SHA384:
878 return EVP_sha384();
879 default:
880 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882}
883
884#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700886
David Benjamin0344daf2015-04-08 02:08:01 -0400887/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
888 * |buf_len| bytes at |buf|. */
889static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
890 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
891 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
892}
893
Adam Langley95c29f32014-06-20 12:00:00 -0700894static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 CIPHER_ORDER **tail) {
896 if (curr == *tail) {
897 return;
898 }
899 if (curr == *head) {
900 *head = curr->next;
901 }
902 if (curr->prev != NULL) {
903 curr->prev->next = curr->next;
904 }
905 if (curr->next != NULL) {
906 curr->next->prev = curr->prev;
907 }
908 (*tail)->next = curr;
909 curr->prev = *tail;
910 curr->next = NULL;
911 *tail = curr;
912}
Adam Langley95c29f32014-06-20 12:00:00 -0700913
914static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 CIPHER_ORDER **tail) {
916 if (curr == *head) {
917 return;
918 }
919 if (curr == *tail) {
920 *tail = curr->prev;
921 }
922 if (curr->next != NULL) {
923 curr->next->prev = curr->prev;
924 }
925 if (curr->prev != NULL) {
926 curr->prev->next = curr->next;
927 }
928 (*head)->prev = curr;
929 curr->next = *head;
930 curr->prev = NULL;
931 *head = curr;
932}
Adam Langley95c29f32014-06-20 12:00:00 -0700933
David Benjamin82c9e902014-12-12 15:55:27 -0500934static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 CIPHER_ORDER *co_list,
936 CIPHER_ORDER **head_p,
937 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400938 /* The set of ciphers is static, but some subset may be unsupported by
939 * |ssl_method|, so the list may be smaller. */
940 size_t co_list_num = 0;
David Benjamin54091232016-09-05 12:47:25 -0400941 for (size_t i = 0; i < kCiphersLen; i++) {
David Benjamina1c90a52015-05-30 17:03:14 -0400942 const SSL_CIPHER *cipher = &kCiphers[i];
David Benjaminabbbee12016-10-31 19:20:42 -0400943 if (ssl_method->supports_cipher(cipher) &&
944 /* TLS 1.3 ciphers do not participate in this mechanism. */
945 cipher->algorithm_mkey != SSL_kGENERIC) {
David Benjamina1c90a52015-05-30 17:03:14 -0400946 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 co_list[co_list_num].next = NULL;
948 co_list[co_list_num].prev = NULL;
949 co_list[co_list_num].active = 0;
950 co_list[co_list_num].in_group = 0;
951 co_list_num++;
952 }
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 /* Prepare linked list from list entries. */
956 if (co_list_num > 0) {
957 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 if (co_list_num > 1) {
960 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700961
David Benjamin54091232016-09-05 12:47:25 -0400962 for (size_t i = 1; i < co_list_num - 1; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 co_list[i].prev = &co_list[i - 1];
964 co_list[i].next = &co_list[i + 1];
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
968 }
969
970 co_list[co_list_num - 1].next = NULL;
971
972 *head_p = &co_list[0];
973 *tail_p = &co_list[co_list_num - 1];
974 }
975}
Adam Langley95c29f32014-06-20 12:00:00 -0700976
David Benjamin0344daf2015-04-08 02:08:01 -0400977/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
978 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
979 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
980 *
981 * - If |cipher_id| is non-zero, only that cipher is selected.
982 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
983 * of that strength.
David Benjamind6e9eec2015-11-18 09:48:55 -0500984 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
David Benjamindcb6ef02015-11-06 15:35:54 -0500985 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800986static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400987 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500988 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
989 int strength_bits, int in_group, CIPHER_ORDER **head_p,
990 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 CIPHER_ORDER *head, *tail, *curr, *next, *last;
992 const SSL_CIPHER *cp;
993 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700994
David Benjamindcb6ef02015-11-06 15:35:54 -0500995 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500996 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400997 /* The rule matches nothing, so bail early. */
998 return;
999 }
1000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 if (rule == CIPHER_DEL) {
1002 /* needed to maintain sorting between currently deleted ciphers */
1003 reverse = 1;
1004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 head = *head_p;
1007 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (reverse) {
1010 next = tail;
1011 last = head;
1012 } else {
1013 next = head;
1014 last = tail;
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 curr = NULL;
1018 for (;;) {
1019 if (curr == last) {
1020 break;
1021 }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 curr = next;
1024 if (curr == NULL) {
1025 break;
1026 }
Adam Langleye3142a72014-07-24 17:56:48 -07001027
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 next = reverse ? curr->prev : curr->next;
1029 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -07001030
David Benjamin0344daf2015-04-08 02:08:01 -04001031 /* Selection criteria is either a specific cipher, the value of
1032 * |strength_bits|, or the algorithms used. */
1033 if (cipher_id != 0) {
1034 if (cipher_id != cp->id) {
1035 continue;
1036 }
1037 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001038 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 continue;
1040 }
David Benjamin881f1962016-08-10 18:29:12 -04001041 } else {
1042 if (!(alg_mkey & cp->algorithm_mkey) ||
1043 !(alg_auth & cp->algorithm_auth) ||
1044 !(alg_enc & cp->algorithm_enc) ||
1045 !(alg_mac & cp->algorithm_mac) ||
1046 (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
1047 continue;
1048 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 }
Adam Langleye3142a72014-07-24 17:56:48 -07001050
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 /* add the cipher if it has not been added yet. */
1052 if (rule == CIPHER_ADD) {
1053 /* reverse == 0 */
1054 if (!curr->active) {
1055 ll_append_tail(&head, curr, &tail);
1056 curr->active = 1;
1057 curr->in_group = in_group;
1058 }
1059 }
Adam Langley95c29f32014-06-20 12:00:00 -07001060
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 /* Move the added cipher to this location */
1062 else if (rule == CIPHER_ORD) {
1063 /* reverse == 0 */
1064 if (curr->active) {
1065 ll_append_tail(&head, curr, &tail);
1066 curr->in_group = 0;
1067 }
1068 } else if (rule == CIPHER_DEL) {
1069 /* reverse == 1 */
1070 if (curr->active) {
1071 /* most recently deleted ciphersuites get best positions
1072 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1073 * works in reverse to maintain the order) */
1074 ll_append_head(&head, curr, &tail);
1075 curr->active = 0;
1076 curr->in_group = 0;
1077 }
1078 } else if (rule == CIPHER_KILL) {
1079 /* reverse == 0 */
1080 if (head == curr) {
1081 head = curr->next;
1082 } else {
1083 curr->prev->next = curr->next;
1084 }
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 if (tail == curr) {
1087 tail = curr->prev;
1088 }
1089 curr->active = 0;
1090 if (curr->next != NULL) {
1091 curr->next->prev = curr->prev;
1092 }
1093 if (curr->prev != NULL) {
1094 curr->prev->next = curr->next;
1095 }
1096 curr->next = NULL;
1097 curr->prev = NULL;
1098 }
1099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 *head_p = head;
1102 *tail_p = tail;
1103}
Adam Langley95c29f32014-06-20 12:00:00 -07001104
1105static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 CIPHER_ORDER **tail_p) {
1107 int max_strength_bits, i, *number_uses;
1108 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 /* This routine sorts the ciphers with descending strength. The sorting must
1111 * keep the pre-sorted sequence, so we apply the normal sorting routine as
1112 * '+' movement to the end of the list. */
1113 max_strength_bits = 0;
1114 curr = *head_p;
1115 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001116 if (curr->active &&
1117 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
1118 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 }
1120 curr = curr->next;
1121 }
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langleyfcf25832014-12-18 17:42:32 -08001123 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
1124 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -04001125 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001126 return 0;
1127 }
1128 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130 /* Now find the strength_bits values actually used. */
1131 curr = *head_p;
1132 while (curr != NULL) {
1133 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001134 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 }
1136 curr = curr->next;
1137 }
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 /* Go through the list of used strength_bits values in descending order. */
1140 for (i = max_strength_bits; i >= 0; i--) {
1141 if (number_uses[i] > 0) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001142 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 -08001143 }
1144 }
1145
1146 OPENSSL_free(number_uses);
1147 return 1;
1148}
Adam Langley95c29f32014-06-20 12:00:00 -07001149
David Benjamin0344daf2015-04-08 02:08:01 -04001150static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1151 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001153 CIPHER_ORDER **tail_p) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001154 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001155 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 const char *l, *buf;
Adam Langleyf139c992016-10-02 09:56:09 -07001157 int multi, skip_rule, rule, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001158 size_t j, buf_len;
1159 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 l = rule_str;
1163 for (;;) {
1164 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 if (ch == '\0') {
1167 break; /* done */
1168 }
Adam Langley95c29f32014-06-20 12:00:00 -07001169
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 if (in_group) {
1171 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 if (*tail_p) {
1173 (*tail_p)->in_group = 0;
1174 }
1175 in_group = 0;
1176 l++;
1177 continue;
1178 }
David Benjamin37d92462014-09-20 17:54:24 -04001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 if (ch == '|') {
1181 rule = CIPHER_ADD;
1182 l++;
1183 continue;
1184 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1185 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001186 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001187 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001188 } else {
1189 rule = CIPHER_ADD;
1190 }
1191 } else if (ch == '-') {
1192 rule = CIPHER_DEL;
1193 l++;
1194 } else if (ch == '+') {
1195 rule = CIPHER_ORD;
1196 l++;
1197 } else if (ch == '!') {
1198 rule = CIPHER_KILL;
1199 l++;
1200 } else if (ch == '@') {
1201 rule = CIPHER_SPECIAL;
1202 l++;
1203 } else if (ch == '[') {
1204 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001205 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001206 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 }
1208 in_group = 1;
1209 has_group = 1;
1210 l++;
1211 continue;
1212 } else {
1213 rule = CIPHER_ADD;
1214 }
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 /* If preference groups are enabled, the only legal operator is +.
1217 * Otherwise the in_group bits will get mixed up. */
1218 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001219 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
Adam Langleyf139c992016-10-02 09:56:09 -07001220 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 }
Adam Langley95c29f32014-06-20 12:00:00 -07001222
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 if (ITEM_SEP(ch)) {
1224 l++;
1225 continue;
1226 }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
David Benjamin0344daf2015-04-08 02:08:01 -04001228 multi = 0;
1229 cipher_id = 0;
1230 alg_mkey = ~0u;
1231 alg_auth = ~0u;
1232 alg_enc = ~0u;
1233 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001234 min_version = 0;
1235 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 for (;;) {
1238 ch = *l;
1239 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001240 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1242 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1243 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001244 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 }
Adam Langley95c29f32014-06-20 12:00:00 -07001246
David Benjamin0344daf2015-04-08 02:08:01 -04001247 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 /* We hit something we cannot deal with, it is no command or separator
1249 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001250 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf99f2442016-10-02 09:53:38 -07001251 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 }
Adam Langley95c29f32014-06-20 12:00:00 -07001253
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 break;
1256 }
David Benjamin0344daf2015-04-08 02:08:01 -04001257
1258 /* Look for a matching exact cipher. These aren't allowed in multipart
1259 * rules. */
1260 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001261 for (j = 0; j < kCiphersLen; j++) {
1262 const SSL_CIPHER *cipher = &kCiphers[j];
1263 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001264 cipher_id = cipher->id;
1265 break;
1266 }
1267 }
1268 }
1269 if (cipher_id == 0) {
1270 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001271 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001272 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1273 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1274 alg_auth &= kCipherAliases[j].algorithm_auth;
1275 alg_enc &= kCipherAliases[j].algorithm_enc;
1276 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001277
1278 if (min_version != 0 &&
1279 min_version != kCipherAliases[j].min_version) {
1280 skip_rule = 1;
1281 } else {
1282 min_version = kCipherAliases[j].min_version;
1283 }
David Benjamin0344daf2015-04-08 02:08:01 -04001284 break;
1285 }
1286 }
David Benjamina1c90a52015-05-30 17:03:14 -04001287 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001288 skip_rule = 1;
David Benjamin0344daf2015-04-08 02:08:01 -04001289 }
1290 }
1291
1292 /* Check for a multipart rule. */
1293 if (ch != '+') {
1294 break;
1295 }
1296 l++;
1297 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 }
Adam Langley95c29f32014-06-20 12:00:00 -07001299
David Benjamin13414b32015-12-09 23:02:39 -05001300 /* If one of the CHACHA20_POLY1305 variants is selected, include the other
1301 * as well. They have the same name to avoid requiring changes in
1302 * configuration. Apply this transformation late so that the cipher name
1303 * still behaves as an exact name and not an alias in multipart rules.
1304 *
1305 * This is temporary and will be removed when the pre-standard construction
1306 * is removed. */
1307 if (cipher_id == TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD ||
1308 cipher_id == TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) {
1309 cipher_id = 0;
1310 alg_mkey = SSL_kECDHE;
1311 alg_auth = SSL_aRSA;
1312 alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
1313 alg_mac = SSL_AEAD;
1314 } else if (cipher_id == TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD ||
1315 cipher_id == TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) {
1316 cipher_id = 0;
1317 alg_mkey = SSL_kECDHE;
1318 alg_auth = SSL_aECDSA;
1319 alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
1320 alg_mac = SSL_AEAD;
1321 }
1322
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 /* Ok, we have the rule, now apply it. */
1324 if (rule == CIPHER_SPECIAL) {
1325 /* special command */
1326 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001327 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001328 ok = ssl_cipher_strength_sort(head_p, tail_p);
1329 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001330 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 }
Adam Langley95c29f32014-06-20 12:00:00 -07001332
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 if (ok == 0) {
Adam Langleyf139c992016-10-02 09:56:09 -07001334 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 }
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 /* We do not support any "multi" options together with "@", so throw away
1338 * the rest of the command, if any left, until end or ':' is found. */
1339 while (*l != '\0' && !ITEM_SEP(*l)) {
1340 l++;
1341 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001342 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001344 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 }
1346 }
Adam Langley95c29f32014-06-20 12:00:00 -07001347
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001349 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf139c992016-10-02 09:56:09 -07001350 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 }
Adam Langley95c29f32014-06-20 12:00:00 -07001352
Adam Langleyf139c992016-10-02 09:56:09 -07001353 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001354}
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356STACK_OF(SSL_CIPHER) *
1357ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001358 struct ssl_cipher_preference_list_st **out_cipher_list,
1359 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1360 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001361 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1363 const char *rule_p;
1364 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 uint8_t *in_group_flags = NULL;
1366 unsigned int num_in_group_flags = 0;
1367 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001368
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001370 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 return NULL;
1372 }
David Benjamin5213df42014-08-20 14:19:54 -04001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 /* Now we have to collect the available ciphers from the compiled in ciphers.
1375 * We cannot get more than the number compiled in, so it is used for
1376 * allocation. */
Brian Smith5ba06892016-02-07 09:36:04 -10001377 co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001379 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 return NULL;
1381 }
Adam Langley95c29f32014-06-20 12:00:00 -07001382
David Benjamina1c90a52015-05-30 17:03:14 -04001383 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 /* Now arrange all ciphers by preference:
1386 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001387
David Benjaminabbbee12016-10-31 19:20:42 -04001388 /* Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
1389 * key exchange mechanisms */
David Benjamind6e9eec2015-11-18 09:48:55 -05001390 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001392 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
1393 &head, &tail);
Steven Valdez803c77a2016-09-06 14:13:43 -04001394 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1395 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langleyfcf25832014-12-18 17:42:32 -08001397 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1398 * CHACHA20 unless there is hardware support for fast and constant-time
David Benjamin13414b32015-12-09 23:02:39 -05001399 * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
1400 * old one. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 if (EVP_has_aes_hardware()) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001402 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1403 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001404 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1405 &head, &tail);
David Benjamin13414b32015-12-09 23:02:39 -05001406 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1407 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001408 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001409 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001410 } else {
David Benjamin13414b32015-12-09 23:02:39 -05001411 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1412 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001413 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001414 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001415 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1416 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001417 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1418 &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001419 }
Adam Langley95c29f32014-06-20 12:00:00 -07001420
David Benjamin43336652016-03-03 15:32:29 -05001421 /* Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001422 * 3DES_EDE_CBC_SHA. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001423 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
1424 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001425 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
1426 &head, &tail);
1427 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1428 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001429
Adam Langleyfcf25832014-12-18 17:42:32 -08001430 /* Temporarily enable everything else for sorting */
David Benjamind6e9eec2015-11-18 09:48:55 -05001431 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1432 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 /* Move ciphers without forward secrecy to the end. */
Steven Valdez803c77a2016-09-06 14:13:43 -04001435 ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001436 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438 /* Now disable everything (maintaining the ordering!) */
David Benjamind6e9eec2015-11-18 09:48:55 -05001439 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1440 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 /* If the rule_string begins with DEFAULT, apply the default rule before
1443 * using the (possibly available) additional rules. */
1444 ok = 1;
1445 rule_p = rule_str;
1446 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001447 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1448 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 rule_p += 7;
1450 if (*rule_p == ':') {
1451 rule_p++;
1452 }
1453 }
Adam Langley858a88d2014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001456 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 }
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 if (!ok) {
1460 goto err;
1461 }
1462
1463 /* Allocate new "cipherstack" for the result, return with error
1464 * if we cannot get one. */
1465 cipherstack = sk_SSL_CIPHER_new_null();
1466 if (cipherstack == NULL) {
1467 goto err;
1468 }
1469
David Benjamina1c90a52015-05-30 17:03:14 -04001470 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 if (!in_group_flags) {
1472 goto err;
1473 }
1474
1475 /* The cipher selection for the list is done. The ciphers are added
1476 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1477 for (curr = head; curr != NULL; curr = curr->next) {
1478 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001479 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1480 goto err;
1481 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 in_group_flags[num_in_group_flags++] = curr->in_group;
1483 }
1484 }
1485 OPENSSL_free(co_list); /* Not needed any longer */
1486 co_list = NULL;
1487
1488 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1489 if (tmp_cipher_list == NULL) {
1490 goto err;
1491 }
1492 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1493 if (!pref_list) {
1494 goto err;
1495 }
1496 pref_list->ciphers = cipherstack;
1497 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1498 if (!pref_list->in_group_flags) {
1499 goto err;
1500 }
1501 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1502 OPENSSL_free(in_group_flags);
1503 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001504 if (*out_cipher_list != NULL) {
1505 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 }
David Benjamin71f07942015-04-08 02:36:59 -04001507 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001508 pref_list = NULL;
1509
David Benjamin71f07942015-04-08 02:36:59 -04001510 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001511 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001512 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001513 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001514 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1515 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001516
David Benjamin71f07942015-04-08 02:36:59 -04001517 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 } else {
1519 sk_SSL_CIPHER_free(tmp_cipher_list);
1520 tmp_cipher_list = NULL;
1521 }
1522
1523 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001524
1525err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001526 OPENSSL_free(co_list);
1527 OPENSSL_free(in_group_flags);
1528 sk_SSL_CIPHER_free(cipherstack);
1529 sk_SSL_CIPHER_free(tmp_cipher_list);
1530 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 OPENSSL_free(pref_list->in_group_flags);
1532 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001533 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 return NULL;
1535}
Adam Langley95c29f32014-06-20 12:00:00 -07001536
David Benjamin71f07942015-04-08 02:36:59 -04001537uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1538
David Benjamina1c90a52015-05-30 17:03:14 -04001539uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1540 uint32_t id = cipher->id;
1541 /* All ciphers are SSLv3. */
1542 assert((id & 0xff000000) == 0x03000000);
1543 return id & 0xffff;
1544}
1545
David Benjamin71f07942015-04-08 02:36:59 -04001546int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1547 return (cipher->algorithm_enc & SSL_AES) != 0;
1548}
1549
1550int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1551 return (cipher->algorithm_mac & SSL_MD5) != 0;
1552}
1553
David Benjaminef793f42015-11-05 18:16:27 -05001554int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1555 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1556}
1557
David Benjamina211aee2016-02-24 17:18:44 -05001558int SSL_CIPHER_has_SHA256_HMAC(const SSL_CIPHER *cipher) {
1559 return (cipher->algorithm_mac & SSL_SHA256) != 0;
1560}
1561
David Benjamin71f07942015-04-08 02:36:59 -04001562int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001563 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001564}
1565
David Benjaminef793f42015-11-05 18:16:27 -05001566int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1567 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1568}
1569
Adam Langleyb00061c2015-11-16 17:44:52 -08001570int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1571 return (cipher->algorithm_enc & SSL_AES128) != 0;
1572}
1573
1574int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1575 return (cipher->algorithm_enc & SSL_AES256) != 0;
1576}
1577
David Benjamin51a01a52015-10-29 13:19:56 -04001578int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
David Benjamin13414b32015-12-09 23:02:39 -05001579 return (cipher->algorithm_enc &
1580 (SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001581}
1582
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001583int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1584 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1585}
1586
1587int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001588 return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001589 cipher->algorithm_mac != SSL_AEAD;
1590}
1591
David Benjaminef793f42015-11-05 18:16:27 -05001592int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1593 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1594}
1595
David Benjamin0fc7df52016-06-02 18:36:33 -04001596int SSL_CIPHER_is_DHE(const SSL_CIPHER *cipher) {
1597 return (cipher->algorithm_mkey & SSL_kDHE) != 0;
1598}
1599
David Benjamin4cc36ad2015-12-19 14:23:26 -05001600int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
1601 return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
1602}
1603
Matt Braithwaite053931e2016-05-25 12:06:05 -07001604int SSL_CIPHER_is_CECPQ1(const SSL_CIPHER *cipher) {
1605 return (cipher->algorithm_mkey & SSL_kCECPQ1) != 0;
1606}
1607
David Benjaminef793f42015-11-05 18:16:27 -05001608uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001609 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1610 cipher->algorithm_auth == SSL_aGENERIC) {
1611 return TLS1_3_VERSION;
1612 }
1613
David Benjamindcb6ef02015-11-06 15:35:54 -05001614 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1615 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1616 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001617 return TLS1_2_VERSION;
1618 }
1619 return SSL3_VERSION;
1620}
1621
Nick Harper1fd39d82016-06-14 18:14:35 -07001622uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001623 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1624 cipher->algorithm_auth == SSL_aGENERIC) {
Nick Harper1fd39d82016-06-14 18:14:35 -07001625 return TLS1_3_VERSION;
1626 }
1627 return TLS1_2_VERSION;
1628}
1629
David Benjamin71f07942015-04-08 02:36:59 -04001630/* return the actual cipher being used */
1631const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1632 if (cipher != NULL) {
1633 return cipher->name;
1634 }
1635
1636 return "(NONE)";
1637}
1638
1639const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1640 if (cipher == NULL) {
1641 return "";
1642 }
1643
1644 switch (cipher->algorithm_mkey) {
1645 case SSL_kRSA:
1646 return "RSA";
1647
1648 case SSL_kDHE:
1649 switch (cipher->algorithm_auth) {
1650 case SSL_aRSA:
1651 return "DHE_RSA";
1652 default:
1653 assert(0);
1654 return "UNKNOWN";
1655 }
1656
1657 case SSL_kECDHE:
1658 switch (cipher->algorithm_auth) {
1659 case SSL_aECDSA:
1660 return "ECDHE_ECDSA";
1661 case SSL_aRSA:
1662 return "ECDHE_RSA";
1663 case SSL_aPSK:
1664 return "ECDHE_PSK";
1665 default:
1666 assert(0);
1667 return "UNKNOWN";
1668 }
1669
Matt Braithwaite053931e2016-05-25 12:06:05 -07001670 case SSL_kCECPQ1:
1671 switch (cipher->algorithm_auth) {
1672 case SSL_aECDSA:
1673 return "CECPQ1_ECDSA";
1674 case SSL_aRSA:
1675 return "CECPQ1_RSA";
1676 default:
1677 assert(0);
1678 return "UNKNOWN";
1679 }
1680
David Benjamin71f07942015-04-08 02:36:59 -04001681 case SSL_kPSK:
1682 assert(cipher->algorithm_auth == SSL_aPSK);
1683 return "PSK";
1684
Steven Valdez803c77a2016-09-06 14:13:43 -04001685 case SSL_kGENERIC:
1686 assert(cipher->algorithm_auth == SSL_aGENERIC);
1687 return "GENERIC";
1688
David Benjamin71f07942015-04-08 02:36:59 -04001689 default:
1690 assert(0);
1691 return "UNKNOWN";
1692 }
1693}
1694
1695static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1696 switch (cipher->algorithm_enc) {
1697 case SSL_3DES:
1698 return "3DES_EDE_CBC";
David Benjamin71f07942015-04-08 02:36:59 -04001699 case SSL_AES128:
1700 return "AES_128_CBC";
1701 case SSL_AES256:
1702 return "AES_256_CBC";
1703 case SSL_AES128GCM:
1704 return "AES_128_GCM";
1705 case SSL_AES256GCM:
1706 return "AES_256_GCM";
David Benjamin13414b32015-12-09 23:02:39 -05001707 case SSL_CHACHA20POLY1305:
Brian Smith271777f2015-10-03 13:53:33 -10001708 case SSL_CHACHA20POLY1305_OLD:
David Benjamin71f07942015-04-08 02:36:59 -04001709 return "CHACHA20_POLY1305";
1710 break;
1711 default:
1712 assert(0);
1713 return "UNKNOWN";
1714 }
1715}
1716
1717static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001718 switch (cipher->algorithm_prf) {
1719 case SSL_HANDSHAKE_MAC_DEFAULT:
1720 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1721 * only ever MD5 or SHA-1. */
1722 switch (cipher->algorithm_mac) {
1723 case SSL_MD5:
1724 return "MD5";
1725 case SSL_SHA1:
1726 return "SHA";
1727 }
1728 break;
1729 case SSL_HANDSHAKE_MAC_SHA256:
1730 return "SHA256";
1731 case SSL_HANDSHAKE_MAC_SHA384:
1732 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001733 }
David Benjaminb0883312015-08-06 09:54:13 -04001734 assert(0);
1735 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001736}
1737
1738char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1739 if (cipher == NULL) {
1740 return NULL;
1741 }
1742
1743 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1744 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1745 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1746
Steven Valdez803c77a2016-09-06 14:13:43 -04001747 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name} or
1748 * TLS_{enc_name}_{prf_name} depending on whether the cipher is AEAD-only. */
1749 size_t len = 4 + strlen(enc_name) + 1 + strlen(prf_name) + 1;
1750
1751 if (cipher->algorithm_mkey != SSL_kGENERIC) {
1752 len += strlen(kx_name) + 6;
1753 }
1754
David Benjamin71f07942015-04-08 02:36:59 -04001755 char *ret = OPENSSL_malloc(len);
1756 if (ret == NULL) {
1757 return NULL;
1758 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001759
David Benjamin71f07942015-04-08 02:36:59 -04001760 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001761 (cipher->algorithm_mkey != SSL_kGENERIC &&
1762 (BUF_strlcat(ret, kx_name, len) >= len ||
1763 BUF_strlcat(ret, "_WITH_", len) >= len)) ||
David Benjamin71f07942015-04-08 02:36:59 -04001764 BUF_strlcat(ret, enc_name, len) >= len ||
1765 BUF_strlcat(ret, "_", len) >= len ||
1766 BUF_strlcat(ret, prf_name, len) >= len) {
1767 assert(0);
1768 OPENSSL_free(ret);
1769 return NULL;
1770 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001771
David Benjamin71f07942015-04-08 02:36:59 -04001772 assert(strlen(ret) + 1 == len);
1773 return ret;
1774}
1775
1776int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1777 if (cipher == NULL) {
1778 return 0;
1779 }
1780
David Benjamin9f2e2772015-11-18 09:59:43 -05001781 int alg_bits, strength_bits;
1782 switch (cipher->algorithm_enc) {
1783 case SSL_AES128:
1784 case SSL_AES128GCM:
David Benjamin9f2e2772015-11-18 09:59:43 -05001785 alg_bits = 128;
1786 strength_bits = 128;
1787 break;
1788
1789 case SSL_AES256:
1790 case SSL_AES256GCM:
1791#if !defined(BORINGSSL_ANDROID_SYSTEM)
1792 case SSL_CHACHA20POLY1305_OLD:
1793#endif
David Benjamin13414b32015-12-09 23:02:39 -05001794 case SSL_CHACHA20POLY1305:
David Benjamin9f2e2772015-11-18 09:59:43 -05001795 alg_bits = 256;
1796 strength_bits = 256;
1797 break;
1798
1799 case SSL_3DES:
1800 alg_bits = 168;
1801 strength_bits = 112;
1802 break;
1803
1804 case SSL_eNULL:
1805 alg_bits = 0;
1806 strength_bits = 0;
1807 break;
1808
1809 default:
1810 assert(0);
1811 alg_bits = 0;
1812 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001813 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001814
1815 if (out_alg_bits != NULL) {
1816 *out_alg_bits = alg_bits;
1817 }
1818 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001819}
1820
Adam Langleyfcf25832014-12-18 17:42:32 -08001821const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1822 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001823 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001824 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 alg_mkey = cipher->algorithm_mkey;
1827 alg_auth = cipher->algorithm_auth;
1828 alg_enc = cipher->algorithm_enc;
1829 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 switch (alg_mkey) {
1832 case SSL_kRSA:
1833 kx = "RSA";
1834 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001835
David Benjamin7061e282015-03-19 11:10:48 -04001836 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 kx = "DH";
1838 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
David Benjamin7061e282015-03-19 11:10:48 -04001840 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 kx = "ECDH";
1842 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Matt Braithwaite053931e2016-05-25 12:06:05 -07001844 case SSL_kCECPQ1:
1845 kx = "CECPQ1";
1846 break;
1847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 case SSL_kPSK:
1849 kx = "PSK";
1850 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001851
Steven Valdez803c77a2016-09-06 14:13:43 -04001852 case SSL_kGENERIC:
1853 kx = "GENERIC";
1854 break;
1855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 default:
1857 kx = "unknown";
1858 }
Adam Langley95c29f32014-06-20 12:00:00 -07001859
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 switch (alg_auth) {
1861 case SSL_aRSA:
1862 au = "RSA";
1863 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001864
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 case SSL_aECDSA:
1866 au = "ECDSA";
1867 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 case SSL_aPSK:
1870 au = "PSK";
1871 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001872
Steven Valdez803c77a2016-09-06 14:13:43 -04001873 case SSL_aGENERIC:
1874 au = "GENERIC";
1875 break;
1876
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 default:
1878 au = "unknown";
1879 break;
1880 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001881
Adam Langleyfcf25832014-12-18 17:42:32 -08001882 switch (alg_enc) {
1883 case SSL_3DES:
1884 enc = "3DES(168)";
1885 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 case SSL_AES128:
1888 enc = "AES(128)";
1889 break;
1890
1891 case SSL_AES256:
1892 enc = "AES(256)";
1893 break;
1894
1895 case SSL_AES128GCM:
1896 enc = "AESGCM(128)";
1897 break;
1898
1899 case SSL_AES256GCM:
1900 enc = "AESGCM(256)";
1901 break;
1902
Brian Smith271777f2015-10-03 13:53:33 -10001903 case SSL_CHACHA20POLY1305_OLD:
David Benjamin13414b32015-12-09 23:02:39 -05001904 enc = "ChaCha20-Poly1305-Old";
1905 break;
1906
1907 case SSL_CHACHA20POLY1305:
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 enc = "ChaCha20-Poly1305";
1909 break;
1910
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001911 case SSL_eNULL:
1912 enc="None";
1913 break;
1914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 default:
1916 enc = "unknown";
1917 break;
1918 }
1919
1920 switch (alg_mac) {
1921 case SSL_MD5:
1922 mac = "MD5";
1923 break;
1924
1925 case SSL_SHA1:
1926 mac = "SHA1";
1927 break;
1928
1929 case SSL_SHA256:
1930 mac = "SHA256";
1931 break;
1932
1933 case SSL_SHA384:
1934 mac = "SHA384";
1935 break;
1936
1937 case SSL_AEAD:
1938 mac = "AEAD";
1939 break;
1940
1941 default:
1942 mac = "unknown";
1943 break;
1944 }
1945
1946 if (buf == NULL) {
1947 len = 128;
1948 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001949 if (buf == NULL) {
1950 return NULL;
1951 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001952 } else if (len < 128) {
1953 return "Buffer too small";
1954 }
1955
Brian Smith0687bdf2016-01-17 09:18:26 -10001956 BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1957 cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001958 return buf;
1959}
1960
David Benjamin71f07942015-04-08 02:36:59 -04001961const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1962 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001963}
1964
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001965COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001966
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001967int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001968
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001969const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langley3e9e0432016-10-03 15:58:07 -07001971void SSL_COMP_free_compression_methods(void) {}
1972
David Benjamind1d80782015-07-05 11:54:09 -04001973int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001974 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001975
Adam Langleyfcf25832014-12-18 17:42:32 -08001976 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001977 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001978 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001979 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001980 }
Adam Langley95c29f32014-06-20 12:00:00 -07001981
David Benjamind1d80782015-07-05 11:54:09 -04001982 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001983}
David Benjamin9c651c92014-07-12 13:27:45 -04001984
David Benjaminc032dfa2016-05-12 14:54:57 -04001985int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
1986 return (cipher->algorithm_auth & SSL_aCERT) != 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001987}
1988
Adam Langleyfcf25832014-12-18 17:42:32 -08001989int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1990 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
Matt Braithwaite053931e2016-05-25 12:06:05 -07001991 if (cipher->algorithm_mkey & SSL_kDHE ||
1992 cipher->algorithm_mkey & SSL_kECDHE ||
1993 cipher->algorithm_mkey & SSL_kCECPQ1) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001994 return 1;
1995 }
1996
1997 /* It is optional in all others. */
1998 return 0;
1999}
David Benjaminb8d28cf2015-07-28 21:34:45 -04002000
2001size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
2002 size_t block_size;
2003 switch (cipher->algorithm_enc) {
2004 case SSL_3DES:
2005 block_size = 8;
2006 break;
2007 case SSL_AES128:
2008 case SSL_AES256:
2009 block_size = 16;
2010 break;
2011 default:
2012 return 0;
2013 }
2014
2015 size_t mac_len;
2016 switch (cipher->algorithm_mac) {
2017 case SSL_MD5:
2018 mac_len = MD5_DIGEST_LENGTH;
2019 break;
2020 case SSL_SHA1:
2021 mac_len = SHA_DIGEST_LENGTH;
2022 break;
2023 default:
2024 return 0;
2025 }
2026
2027 size_t ret = 1 + mac_len;
2028 ret += block_size - (ret % block_size);
2029 return ret;
2030}