blob: deb463ef3cf8b9ff7ee0f943c4dcabcb1f93dcb5 [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
Steven Valdez3cbdc342016-10-06 14:59:23 -0400741 /* AEAD-only ciphers for TLS 1.3. */
742 {"GENERIC", SSL_kGENERIC, SSL_aGENERIC, ~0u, ~0u, 0},
743
David Benjamind6e9eec2015-11-18 09:48:55 -0500744 /* Legacy strength classes. */
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -0700745 {"HIGH", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, 0},
746 {"FIPS", ~SSL_kCECPQ1, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800747};
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Steven Valdezcb966542016-08-17 16:56:14 -0400749static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
David Benjamina1c90a52015-05-30 17:03:14 -0400750
751static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
752 const SSL_CIPHER *a = in_a;
753 const SSL_CIPHER *b = in_b;
754
755 if (a->id > b->id) {
756 return 1;
757 } else if (a->id < b->id) {
758 return -1;
759 } else {
760 return 0;
761 }
762}
763
764static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
765 return ssl_cipher_id_cmp(*a, *b);
766}
767
768const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
769 SSL_CIPHER c;
770
771 c.id = 0x03000000L | value;
772 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
773 ssl_cipher_id_cmp);
774}
David Benjamin0344daf2015-04-08 02:08:01 -0400775
David Benjaminea72bd02014-12-21 21:27:41 -0500776int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
777 size_t *out_mac_secret_len,
778 size_t *out_fixed_iv_len,
779 const SSL_CIPHER *cipher, uint16_t version) {
780 *out_aead = NULL;
781 *out_mac_secret_len = 0;
782 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700783
David Benjamin305e6fb2016-10-27 18:19:00 -0400784 if (cipher->algorithm_mac == SSL_AEAD) {
785 if (cipher->algorithm_enc == SSL_AES128GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500786 *out_aead = EVP_aead_aes_128_gcm();
787 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400788 } else if (cipher->algorithm_enc == SSL_AES256GCM) {
David Benjaminea72bd02014-12-21 21:27:41 -0500789 *out_aead = EVP_aead_aes_256_gcm();
790 *out_fixed_iv_len = 4;
Adam Langleyd98dc132015-09-23 16:41:33 -0700791#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin305e6fb2016-10-27 18:19:00 -0400792 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305_OLD) {
Brian Smith3e23e4c2015-10-03 11:38:58 -1000793 *out_aead = EVP_aead_chacha20_poly1305_old();
David Benjaminea72bd02014-12-21 21:27:41 -0500794 *out_fixed_iv_len = 0;
Adam Langleyd98dc132015-09-23 16:41:33 -0700795#endif
David Benjamin305e6fb2016-10-27 18:19:00 -0400796 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
David Benjamin13414b32015-12-09 23:02:39 -0500797 *out_aead = EVP_aead_chacha20_poly1305();
798 *out_fixed_iv_len = 12;
David Benjamin305e6fb2016-10-27 18:19:00 -0400799 } else {
David Benjaminea72bd02014-12-21 21:27:41 -0500800 return 0;
David Benjamin305e6fb2016-10-27 18:19:00 -0400801 }
802
803 /* In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
804 * above computes the TLS 1.2 construction. */
805 if (version >= TLS1_3_VERSION) {
806 *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
807 }
808 } else if (cipher->algorithm_mac == SSL_SHA1) {
809 if (cipher->algorithm_enc == SSL_eNULL) {
810 if (version == SSL3_VERSION) {
811 *out_aead = EVP_aead_null_sha1_ssl3();
812 } else {
813 *out_aead = EVP_aead_null_sha1_tls();
814 }
815 } else if (cipher->algorithm_enc == SSL_3DES) {
816 if (version == SSL3_VERSION) {
817 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
818 *out_fixed_iv_len = 8;
819 } else if (version == TLS1_VERSION) {
820 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
821 *out_fixed_iv_len = 8;
822 } else {
823 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
824 }
825 } else if (cipher->algorithm_enc == SSL_AES128) {
826 if (version == SSL3_VERSION) {
827 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
828 *out_fixed_iv_len = 16;
829 } else if (version == TLS1_VERSION) {
830 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
831 *out_fixed_iv_len = 16;
832 } else {
833 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
834 }
835 } else if (cipher->algorithm_enc == SSL_AES256) {
836 if (version == SSL3_VERSION) {
837 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
838 *out_fixed_iv_len = 16;
839 } else if (version == TLS1_VERSION) {
840 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
841 *out_fixed_iv_len = 16;
842 } else {
843 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
844 }
845 } else {
846 return 0;
847 }
848
849 *out_mac_secret_len = SHA_DIGEST_LENGTH;
850 } else if (cipher->algorithm_mac == SSL_SHA256) {
851 if (cipher->algorithm_enc == SSL_AES128) {
852 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
853 } else if (cipher->algorithm_enc == SSL_AES256) {
854 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
855 } else {
856 return 0;
857 }
858
859 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
860 } else if (cipher->algorithm_mac == SSL_SHA384) {
861 if (cipher->algorithm_enc != SSL_AES256) {
862 return 0;
863 }
864
865 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
866 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
867 } else {
868 return 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500869 }
Steven Valdez79750562016-06-16 06:38:04 -0400870
Steven Valdez79750562016-06-16 06:38:04 -0400871 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800872}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700873
David Benjaminb0883312015-08-06 09:54:13 -0400874const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
875 switch (algorithm_prf) {
876 case SSL_HANDSHAKE_MAC_DEFAULT:
877 return EVP_sha1();
878 case SSL_HANDSHAKE_MAC_SHA256:
879 return EVP_sha256();
880 case SSL_HANDSHAKE_MAC_SHA384:
881 return EVP_sha384();
882 default:
883 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885}
886
887#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamin0344daf2015-04-08 02:08:01 -0400890/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
891 * |buf_len| bytes at |buf|. */
892static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
893 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
894 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
895}
896
Adam Langley95c29f32014-06-20 12:00:00 -0700897static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 CIPHER_ORDER **tail) {
899 if (curr == *tail) {
900 return;
901 }
902 if (curr == *head) {
903 *head = curr->next;
904 }
905 if (curr->prev != NULL) {
906 curr->prev->next = curr->next;
907 }
908 if (curr->next != NULL) {
909 curr->next->prev = curr->prev;
910 }
911 (*tail)->next = curr;
912 curr->prev = *tail;
913 curr->next = NULL;
914 *tail = curr;
915}
Adam Langley95c29f32014-06-20 12:00:00 -0700916
917static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 CIPHER_ORDER **tail) {
919 if (curr == *head) {
920 return;
921 }
922 if (curr == *tail) {
923 *tail = curr->prev;
924 }
925 if (curr->next != NULL) {
926 curr->next->prev = curr->prev;
927 }
928 if (curr->prev != NULL) {
929 curr->prev->next = curr->next;
930 }
931 (*head)->prev = curr;
932 curr->next = *head;
933 curr->prev = NULL;
934 *head = curr;
935}
Adam Langley95c29f32014-06-20 12:00:00 -0700936
David Benjamin82c9e902014-12-12 15:55:27 -0500937static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 CIPHER_ORDER *co_list,
939 CIPHER_ORDER **head_p,
940 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400941 /* The set of ciphers is static, but some subset may be unsupported by
942 * |ssl_method|, so the list may be smaller. */
943 size_t co_list_num = 0;
David Benjamin54091232016-09-05 12:47:25 -0400944 for (size_t i = 0; i < kCiphersLen; i++) {
David Benjamina1c90a52015-05-30 17:03:14 -0400945 const SSL_CIPHER *cipher = &kCiphers[i];
946 if (ssl_method->supports_cipher(cipher)) {
947 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 co_list[co_list_num].next = NULL;
949 co_list[co_list_num].prev = NULL;
950 co_list[co_list_num].active = 0;
951 co_list[co_list_num].in_group = 0;
952 co_list_num++;
953 }
954 }
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 /* Prepare linked list from list entries. */
957 if (co_list_num > 0) {
958 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (co_list_num > 1) {
961 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700962
David Benjamin54091232016-09-05 12:47:25 -0400963 for (size_t i = 1; i < co_list_num - 1; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 co_list[i].prev = &co_list[i - 1];
965 co_list[i].next = &co_list[i + 1];
966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
969 }
970
971 co_list[co_list_num - 1].next = NULL;
972
973 *head_p = &co_list[0];
974 *tail_p = &co_list[co_list_num - 1];
975 }
976}
Adam Langley95c29f32014-06-20 12:00:00 -0700977
David Benjamin0344daf2015-04-08 02:08:01 -0400978/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
979 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
980 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
981 *
982 * - If |cipher_id| is non-zero, only that cipher is selected.
983 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
984 * of that strength.
David Benjamind6e9eec2015-11-18 09:48:55 -0500985 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
David Benjamindcb6ef02015-11-06 15:35:54 -0500986 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800987static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400988 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500989 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
990 int strength_bits, int in_group, CIPHER_ORDER **head_p,
991 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 CIPHER_ORDER *head, *tail, *curr, *next, *last;
993 const SSL_CIPHER *cp;
994 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700995
David Benjamindcb6ef02015-11-06 15:35:54 -0500996 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500997 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400998 /* The rule matches nothing, so bail early. */
999 return;
1000 }
1001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (rule == CIPHER_DEL) {
1003 /* needed to maintain sorting between currently deleted ciphers */
1004 reverse = 1;
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 head = *head_p;
1008 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 if (reverse) {
1011 next = tail;
1012 last = head;
1013 } else {
1014 next = head;
1015 last = tail;
1016 }
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 curr = NULL;
1019 for (;;) {
1020 if (curr == last) {
1021 break;
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 curr = next;
1025 if (curr == NULL) {
1026 break;
1027 }
Adam Langleye3142a72014-07-24 17:56:48 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 next = reverse ? curr->prev : curr->next;
1030 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -07001031
David Benjamin0344daf2015-04-08 02:08:01 -04001032 /* Selection criteria is either a specific cipher, the value of
1033 * |strength_bits|, or the algorithms used. */
1034 if (cipher_id != 0) {
1035 if (cipher_id != cp->id) {
1036 continue;
1037 }
1038 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001039 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 continue;
1041 }
David Benjamin881f1962016-08-10 18:29:12 -04001042 } else {
1043 if (!(alg_mkey & cp->algorithm_mkey) ||
1044 !(alg_auth & cp->algorithm_auth) ||
1045 !(alg_enc & cp->algorithm_enc) ||
1046 !(alg_mac & cp->algorithm_mac) ||
1047 (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
1048 continue;
1049 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 }
Adam Langleye3142a72014-07-24 17:56:48 -07001051
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 /* add the cipher if it has not been added yet. */
1053 if (rule == CIPHER_ADD) {
1054 /* reverse == 0 */
1055 if (!curr->active) {
1056 ll_append_tail(&head, curr, &tail);
1057 curr->active = 1;
1058 curr->in_group = in_group;
1059 }
1060 }
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 /* Move the added cipher to this location */
1063 else if (rule == CIPHER_ORD) {
1064 /* reverse == 0 */
1065 if (curr->active) {
1066 ll_append_tail(&head, curr, &tail);
1067 curr->in_group = 0;
1068 }
1069 } else if (rule == CIPHER_DEL) {
1070 /* reverse == 1 */
1071 if (curr->active) {
1072 /* most recently deleted ciphersuites get best positions
1073 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1074 * works in reverse to maintain the order) */
1075 ll_append_head(&head, curr, &tail);
1076 curr->active = 0;
1077 curr->in_group = 0;
1078 }
1079 } else if (rule == CIPHER_KILL) {
1080 /* reverse == 0 */
1081 if (head == curr) {
1082 head = curr->next;
1083 } else {
1084 curr->prev->next = curr->next;
1085 }
Adam Langley95c29f32014-06-20 12:00:00 -07001086
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 if (tail == curr) {
1088 tail = curr->prev;
1089 }
1090 curr->active = 0;
1091 if (curr->next != NULL) {
1092 curr->next->prev = curr->prev;
1093 }
1094 if (curr->prev != NULL) {
1095 curr->prev->next = curr->next;
1096 }
1097 curr->next = NULL;
1098 curr->prev = NULL;
1099 }
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 *head_p = head;
1103 *tail_p = tail;
1104}
Adam Langley95c29f32014-06-20 12:00:00 -07001105
1106static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 CIPHER_ORDER **tail_p) {
1108 int max_strength_bits, i, *number_uses;
1109 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 /* This routine sorts the ciphers with descending strength. The sorting must
1112 * keep the pre-sorted sequence, so we apply the normal sorting routine as
1113 * '+' movement to the end of the list. */
1114 max_strength_bits = 0;
1115 curr = *head_p;
1116 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001117 if (curr->active &&
1118 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
1119 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 }
1121 curr = curr->next;
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
1125 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -04001126 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 return 0;
1128 }
1129 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 /* Now find the strength_bits values actually used. */
1132 curr = *head_p;
1133 while (curr != NULL) {
1134 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001135 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 }
1137 curr = curr->next;
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 /* Go through the list of used strength_bits values in descending order. */
1141 for (i = max_strength_bits; i >= 0; i--) {
1142 if (number_uses[i] > 0) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001143 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 -08001144 }
1145 }
1146
1147 OPENSSL_free(number_uses);
1148 return 1;
1149}
Adam Langley95c29f32014-06-20 12:00:00 -07001150
David Benjamin0344daf2015-04-08 02:08:01 -04001151static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1152 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001154 CIPHER_ORDER **tail_p) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001155 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001156 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 const char *l, *buf;
Adam Langleyf139c992016-10-02 09:56:09 -07001158 int multi, skip_rule, rule, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001159 size_t j, buf_len;
1160 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 l = rule_str;
1164 for (;;) {
1165 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 if (ch == '\0') {
1168 break; /* done */
1169 }
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 if (in_group) {
1172 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 if (*tail_p) {
1174 (*tail_p)->in_group = 0;
1175 }
1176 in_group = 0;
1177 l++;
1178 continue;
1179 }
David Benjamin37d92462014-09-20 17:54:24 -04001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 if (ch == '|') {
1182 rule = CIPHER_ADD;
1183 l++;
1184 continue;
1185 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1186 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001187 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001188 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 } else {
1190 rule = CIPHER_ADD;
1191 }
1192 } else if (ch == '-') {
1193 rule = CIPHER_DEL;
1194 l++;
1195 } else if (ch == '+') {
1196 rule = CIPHER_ORD;
1197 l++;
1198 } else if (ch == '!') {
1199 rule = CIPHER_KILL;
1200 l++;
1201 } else if (ch == '@') {
1202 rule = CIPHER_SPECIAL;
1203 l++;
1204 } else if (ch == '[') {
1205 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001206 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
Adam Langleyf139c992016-10-02 09:56:09 -07001207 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 }
1209 in_group = 1;
1210 has_group = 1;
1211 l++;
1212 continue;
1213 } else {
1214 rule = CIPHER_ADD;
1215 }
Adam Langley95c29f32014-06-20 12:00:00 -07001216
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 /* If preference groups are enabled, the only legal operator is +.
1218 * Otherwise the in_group bits will get mixed up. */
1219 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001220 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
Adam Langleyf139c992016-10-02 09:56:09 -07001221 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 }
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 if (ITEM_SEP(ch)) {
1225 l++;
1226 continue;
1227 }
Adam Langley95c29f32014-06-20 12:00:00 -07001228
David Benjamin0344daf2015-04-08 02:08:01 -04001229 multi = 0;
1230 cipher_id = 0;
1231 alg_mkey = ~0u;
1232 alg_auth = ~0u;
1233 alg_enc = ~0u;
1234 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001235 min_version = 0;
1236 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 for (;;) {
1239 ch = *l;
1240 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001241 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1243 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1244 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001245 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 }
Adam Langley95c29f32014-06-20 12:00:00 -07001247
David Benjamin0344daf2015-04-08 02:08:01 -04001248 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001249 /* We hit something we cannot deal with, it is no command or separator
1250 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001251 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf99f2442016-10-02 09:53:38 -07001252 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 }
Adam Langley95c29f32014-06-20 12:00:00 -07001254
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 break;
1257 }
David Benjamin0344daf2015-04-08 02:08:01 -04001258
1259 /* Look for a matching exact cipher. These aren't allowed in multipart
1260 * rules. */
1261 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001262 for (j = 0; j < kCiphersLen; j++) {
1263 const SSL_CIPHER *cipher = &kCiphers[j];
1264 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001265 cipher_id = cipher->id;
1266 break;
1267 }
1268 }
1269 }
1270 if (cipher_id == 0) {
1271 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001272 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001273 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1274 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1275 alg_auth &= kCipherAliases[j].algorithm_auth;
1276 alg_enc &= kCipherAliases[j].algorithm_enc;
1277 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001278
1279 if (min_version != 0 &&
1280 min_version != kCipherAliases[j].min_version) {
1281 skip_rule = 1;
1282 } else {
1283 min_version = kCipherAliases[j].min_version;
1284 }
David Benjamin0344daf2015-04-08 02:08:01 -04001285 break;
1286 }
1287 }
David Benjamina1c90a52015-05-30 17:03:14 -04001288 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001289 skip_rule = 1;
David Benjamin0344daf2015-04-08 02:08:01 -04001290 }
1291 }
1292
1293 /* Check for a multipart rule. */
1294 if (ch != '+') {
1295 break;
1296 }
1297 l++;
1298 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 }
Adam Langley95c29f32014-06-20 12:00:00 -07001300
David Benjamin13414b32015-12-09 23:02:39 -05001301 /* If one of the CHACHA20_POLY1305 variants is selected, include the other
1302 * as well. They have the same name to avoid requiring changes in
1303 * configuration. Apply this transformation late so that the cipher name
1304 * still behaves as an exact name and not an alias in multipart rules.
1305 *
1306 * This is temporary and will be removed when the pre-standard construction
1307 * is removed. */
1308 if (cipher_id == TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD ||
1309 cipher_id == TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) {
1310 cipher_id = 0;
1311 alg_mkey = SSL_kECDHE;
1312 alg_auth = SSL_aRSA;
1313 alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
1314 alg_mac = SSL_AEAD;
1315 } else if (cipher_id == TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD ||
1316 cipher_id == TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) {
1317 cipher_id = 0;
1318 alg_mkey = SSL_kECDHE;
1319 alg_auth = SSL_aECDSA;
1320 alg_enc = SSL_CHACHA20POLY1305|SSL_CHACHA20POLY1305_OLD;
1321 alg_mac = SSL_AEAD;
1322 }
1323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 /* Ok, we have the rule, now apply it. */
1325 if (rule == CIPHER_SPECIAL) {
1326 /* special command */
1327 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001328 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 ok = ssl_cipher_strength_sort(head_p, tail_p);
1330 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001331 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 }
Adam Langley95c29f32014-06-20 12:00:00 -07001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 if (ok == 0) {
Adam Langleyf139c992016-10-02 09:56:09 -07001335 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 }
Adam Langley95c29f32014-06-20 12:00:00 -07001337
Adam Langleyfcf25832014-12-18 17:42:32 -08001338 /* We do not support any "multi" options together with "@", so throw away
1339 * the rest of the command, if any left, until end or ':' is found. */
1340 while (*l != '\0' && !ITEM_SEP(*l)) {
1341 l++;
1342 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001343 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001345 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 }
1347 }
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001350 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyf139c992016-10-02 09:56:09 -07001351 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 }
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyf139c992016-10-02 09:56:09 -07001354 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001355}
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357STACK_OF(SSL_CIPHER) *
1358ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001359 struct ssl_cipher_preference_list_st **out_cipher_list,
1360 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1361 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001362 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1364 const char *rule_p;
1365 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 uint8_t *in_group_flags = NULL;
1367 unsigned int num_in_group_flags = 0;
1368 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001371 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 return NULL;
1373 }
David Benjamin5213df42014-08-20 14:19:54 -04001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 /* Now we have to collect the available ciphers from the compiled in ciphers.
1376 * We cannot get more than the number compiled in, so it is used for
1377 * allocation. */
Brian Smith5ba06892016-02-07 09:36:04 -10001378 co_list = OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001380 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 return NULL;
1382 }
Adam Langley95c29f32014-06-20 12:00:00 -07001383
David Benjamina1c90a52015-05-30 17:03:14 -04001384 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 /* Now arrange all ciphers by preference:
1387 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Steven Valdez803c77a2016-09-06 14:13:43 -04001389 /* Everything else being equal, prefer TLS 1.3 ciphers then ECDHE_ECDSA then
1390 * ECDHE_RSA over other key exchange mechanisms */
Matt Braithwaite053931e2016-05-25 12:06:05 -07001391
Steven Valdez803c77a2016-09-06 14:13:43 -04001392 ssl_cipher_apply_rule(0, SSL_kGENERIC, SSL_aGENERIC, ~0u, ~0u, 0, CIPHER_ADD,
1393 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001394 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001396 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
1397 &head, &tail);
Steven Valdez803c77a2016-09-06 14:13:43 -04001398 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1399 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1402 * CHACHA20 unless there is hardware support for fast and constant-time
David Benjamin13414b32015-12-09 23:02:39 -05001403 * AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
1404 * old one. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 if (EVP_has_aes_hardware()) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001406 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1407 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001408 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1409 &head, &tail);
David Benjamin13414b32015-12-09 23:02:39 -05001410 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1411 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001412 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001413 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 } else {
David Benjamin13414b32015-12-09 23:02:39 -05001415 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
1416 -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001417 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001418 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001419 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1420 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001421 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1422 &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 }
Adam Langley95c29f32014-06-20 12:00:00 -07001424
David Benjamin43336652016-03-03 15:32:29 -05001425 /* Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001426 * 3DES_EDE_CBC_SHA. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001427 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
1428 &head, &tail);
David Benjamin43336652016-03-03 15:32:29 -05001429 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
1430 &head, &tail);
1431 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~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 /* Temporarily enable everything else for sorting */
David Benjamind6e9eec2015-11-18 09:48:55 -05001435 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1436 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001437
Adam Langleyfcf25832014-12-18 17:42:32 -08001438 /* Move ciphers without forward secrecy to the end. */
Steven Valdez803c77a2016-09-06 14:13:43 -04001439 ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001440 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 /* Now disable everything (maintaining the ordering!) */
David Benjamind6e9eec2015-11-18 09:48:55 -05001443 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1444 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 /* If the rule_string begins with DEFAULT, apply the default rule before
1447 * using the (possibly available) additional rules. */
1448 ok = 1;
1449 rule_p = rule_str;
1450 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001451 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1452 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 rule_p += 7;
1454 if (*rule_p == ':') {
1455 rule_p++;
1456 }
1457 }
Adam Langley858a88d2014-06-20 12:00:00 -07001458
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001460 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001461 }
Adam Langley95c29f32014-06-20 12:00:00 -07001462
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 if (!ok) {
1464 goto err;
1465 }
1466
1467 /* Allocate new "cipherstack" for the result, return with error
1468 * if we cannot get one. */
1469 cipherstack = sk_SSL_CIPHER_new_null();
1470 if (cipherstack == NULL) {
1471 goto err;
1472 }
1473
David Benjamina1c90a52015-05-30 17:03:14 -04001474 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 if (!in_group_flags) {
1476 goto err;
1477 }
1478
1479 /* The cipher selection for the list is done. The ciphers are added
1480 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1481 for (curr = head; curr != NULL; curr = curr->next) {
1482 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001483 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1484 goto err;
1485 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 in_group_flags[num_in_group_flags++] = curr->in_group;
1487 }
1488 }
1489 OPENSSL_free(co_list); /* Not needed any longer */
1490 co_list = NULL;
1491
1492 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1493 if (tmp_cipher_list == NULL) {
1494 goto err;
1495 }
1496 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1497 if (!pref_list) {
1498 goto err;
1499 }
1500 pref_list->ciphers = cipherstack;
1501 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1502 if (!pref_list->in_group_flags) {
1503 goto err;
1504 }
1505 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1506 OPENSSL_free(in_group_flags);
1507 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001508 if (*out_cipher_list != NULL) {
1509 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 }
David Benjamin71f07942015-04-08 02:36:59 -04001511 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 pref_list = NULL;
1513
David Benjamin71f07942015-04-08 02:36:59 -04001514 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001515 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001516 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001518 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1519 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001520
David Benjamin71f07942015-04-08 02:36:59 -04001521 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 } else {
1523 sk_SSL_CIPHER_free(tmp_cipher_list);
1524 tmp_cipher_list = NULL;
1525 }
1526
1527 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001528
1529err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001530 OPENSSL_free(co_list);
1531 OPENSSL_free(in_group_flags);
1532 sk_SSL_CIPHER_free(cipherstack);
1533 sk_SSL_CIPHER_free(tmp_cipher_list);
1534 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 OPENSSL_free(pref_list->in_group_flags);
1536 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001537 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 return NULL;
1539}
Adam Langley95c29f32014-06-20 12:00:00 -07001540
David Benjamin71f07942015-04-08 02:36:59 -04001541uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1542
David Benjamina1c90a52015-05-30 17:03:14 -04001543uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1544 uint32_t id = cipher->id;
1545 /* All ciphers are SSLv3. */
1546 assert((id & 0xff000000) == 0x03000000);
1547 return id & 0xffff;
1548}
1549
David Benjamin71f07942015-04-08 02:36:59 -04001550int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1551 return (cipher->algorithm_enc & SSL_AES) != 0;
1552}
1553
1554int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1555 return (cipher->algorithm_mac & SSL_MD5) != 0;
1556}
1557
David Benjaminef793f42015-11-05 18:16:27 -05001558int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1559 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1560}
1561
David Benjamina211aee2016-02-24 17:18:44 -05001562int SSL_CIPHER_has_SHA256_HMAC(const SSL_CIPHER *cipher) {
1563 return (cipher->algorithm_mac & SSL_SHA256) != 0;
1564}
1565
David Benjamin71f07942015-04-08 02:36:59 -04001566int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001567 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001568}
1569
David Benjaminef793f42015-11-05 18:16:27 -05001570int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1571 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1572}
1573
Adam Langleyb00061c2015-11-16 17:44:52 -08001574int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1575 return (cipher->algorithm_enc & SSL_AES128) != 0;
1576}
1577
1578int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1579 return (cipher->algorithm_enc & SSL_AES256) != 0;
1580}
1581
David Benjamin51a01a52015-10-29 13:19:56 -04001582int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
David Benjamin13414b32015-12-09 23:02:39 -05001583 return (cipher->algorithm_enc &
1584 (SSL_CHACHA20POLY1305 | SSL_CHACHA20POLY1305_OLD)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001585}
1586
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001587int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1588 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1589}
1590
1591int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001592 return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001593 cipher->algorithm_mac != SSL_AEAD;
1594}
1595
David Benjaminef793f42015-11-05 18:16:27 -05001596int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1597 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1598}
1599
David Benjamin0fc7df52016-06-02 18:36:33 -04001600int SSL_CIPHER_is_DHE(const SSL_CIPHER *cipher) {
1601 return (cipher->algorithm_mkey & SSL_kDHE) != 0;
1602}
1603
David Benjamin4cc36ad2015-12-19 14:23:26 -05001604int SSL_CIPHER_is_ECDHE(const SSL_CIPHER *cipher) {
1605 return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
1606}
1607
Matt Braithwaite053931e2016-05-25 12:06:05 -07001608int SSL_CIPHER_is_CECPQ1(const SSL_CIPHER *cipher) {
1609 return (cipher->algorithm_mkey & SSL_kCECPQ1) != 0;
1610}
1611
David Benjaminef793f42015-11-05 18:16:27 -05001612uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001613 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1614 cipher->algorithm_auth == SSL_aGENERIC) {
1615 return TLS1_3_VERSION;
1616 }
1617
David Benjamindcb6ef02015-11-06 15:35:54 -05001618 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1619 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1620 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001621 return TLS1_2_VERSION;
1622 }
1623 return SSL3_VERSION;
1624}
1625
Nick Harper1fd39d82016-06-14 18:14:35 -07001626uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001627 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1628 cipher->algorithm_auth == SSL_aGENERIC) {
Nick Harper1fd39d82016-06-14 18:14:35 -07001629 return TLS1_3_VERSION;
1630 }
1631 return TLS1_2_VERSION;
1632}
1633
David Benjamin71f07942015-04-08 02:36:59 -04001634/* return the actual cipher being used */
1635const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1636 if (cipher != NULL) {
1637 return cipher->name;
1638 }
1639
1640 return "(NONE)";
1641}
1642
1643const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1644 if (cipher == NULL) {
1645 return "";
1646 }
1647
1648 switch (cipher->algorithm_mkey) {
1649 case SSL_kRSA:
1650 return "RSA";
1651
1652 case SSL_kDHE:
1653 switch (cipher->algorithm_auth) {
1654 case SSL_aRSA:
1655 return "DHE_RSA";
1656 default:
1657 assert(0);
1658 return "UNKNOWN";
1659 }
1660
1661 case SSL_kECDHE:
1662 switch (cipher->algorithm_auth) {
1663 case SSL_aECDSA:
1664 return "ECDHE_ECDSA";
1665 case SSL_aRSA:
1666 return "ECDHE_RSA";
1667 case SSL_aPSK:
1668 return "ECDHE_PSK";
1669 default:
1670 assert(0);
1671 return "UNKNOWN";
1672 }
1673
Matt Braithwaite053931e2016-05-25 12:06:05 -07001674 case SSL_kCECPQ1:
1675 switch (cipher->algorithm_auth) {
1676 case SSL_aECDSA:
1677 return "CECPQ1_ECDSA";
1678 case SSL_aRSA:
1679 return "CECPQ1_RSA";
1680 default:
1681 assert(0);
1682 return "UNKNOWN";
1683 }
1684
David Benjamin71f07942015-04-08 02:36:59 -04001685 case SSL_kPSK:
1686 assert(cipher->algorithm_auth == SSL_aPSK);
1687 return "PSK";
1688
Steven Valdez803c77a2016-09-06 14:13:43 -04001689 case SSL_kGENERIC:
1690 assert(cipher->algorithm_auth == SSL_aGENERIC);
1691 return "GENERIC";
1692
David Benjamin71f07942015-04-08 02:36:59 -04001693 default:
1694 assert(0);
1695 return "UNKNOWN";
1696 }
1697}
1698
1699static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1700 switch (cipher->algorithm_enc) {
1701 case SSL_3DES:
1702 return "3DES_EDE_CBC";
David Benjamin71f07942015-04-08 02:36:59 -04001703 case SSL_AES128:
1704 return "AES_128_CBC";
1705 case SSL_AES256:
1706 return "AES_256_CBC";
1707 case SSL_AES128GCM:
1708 return "AES_128_GCM";
1709 case SSL_AES256GCM:
1710 return "AES_256_GCM";
David Benjamin13414b32015-12-09 23:02:39 -05001711 case SSL_CHACHA20POLY1305:
Brian Smith271777f2015-10-03 13:53:33 -10001712 case SSL_CHACHA20POLY1305_OLD:
David Benjamin71f07942015-04-08 02:36:59 -04001713 return "CHACHA20_POLY1305";
1714 break;
1715 default:
1716 assert(0);
1717 return "UNKNOWN";
1718 }
1719}
1720
1721static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001722 switch (cipher->algorithm_prf) {
1723 case SSL_HANDSHAKE_MAC_DEFAULT:
1724 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1725 * only ever MD5 or SHA-1. */
1726 switch (cipher->algorithm_mac) {
1727 case SSL_MD5:
1728 return "MD5";
1729 case SSL_SHA1:
1730 return "SHA";
1731 }
1732 break;
1733 case SSL_HANDSHAKE_MAC_SHA256:
1734 return "SHA256";
1735 case SSL_HANDSHAKE_MAC_SHA384:
1736 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001737 }
David Benjaminb0883312015-08-06 09:54:13 -04001738 assert(0);
1739 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001740}
1741
1742char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1743 if (cipher == NULL) {
1744 return NULL;
1745 }
1746
1747 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1748 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1749 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1750
Steven Valdez803c77a2016-09-06 14:13:43 -04001751 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name} or
1752 * TLS_{enc_name}_{prf_name} depending on whether the cipher is AEAD-only. */
1753 size_t len = 4 + strlen(enc_name) + 1 + strlen(prf_name) + 1;
1754
1755 if (cipher->algorithm_mkey != SSL_kGENERIC) {
1756 len += strlen(kx_name) + 6;
1757 }
1758
David Benjamin71f07942015-04-08 02:36:59 -04001759 char *ret = OPENSSL_malloc(len);
1760 if (ret == NULL) {
1761 return NULL;
1762 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001763
David Benjamin71f07942015-04-08 02:36:59 -04001764 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001765 (cipher->algorithm_mkey != SSL_kGENERIC &&
1766 (BUF_strlcat(ret, kx_name, len) >= len ||
1767 BUF_strlcat(ret, "_WITH_", len) >= len)) ||
David Benjamin71f07942015-04-08 02:36:59 -04001768 BUF_strlcat(ret, enc_name, len) >= len ||
1769 BUF_strlcat(ret, "_", len) >= len ||
1770 BUF_strlcat(ret, prf_name, len) >= len) {
1771 assert(0);
1772 OPENSSL_free(ret);
1773 return NULL;
1774 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001775
David Benjamin71f07942015-04-08 02:36:59 -04001776 assert(strlen(ret) + 1 == len);
1777 return ret;
1778}
1779
1780int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1781 if (cipher == NULL) {
1782 return 0;
1783 }
1784
David Benjamin9f2e2772015-11-18 09:59:43 -05001785 int alg_bits, strength_bits;
1786 switch (cipher->algorithm_enc) {
1787 case SSL_AES128:
1788 case SSL_AES128GCM:
David Benjamin9f2e2772015-11-18 09:59:43 -05001789 alg_bits = 128;
1790 strength_bits = 128;
1791 break;
1792
1793 case SSL_AES256:
1794 case SSL_AES256GCM:
1795#if !defined(BORINGSSL_ANDROID_SYSTEM)
1796 case SSL_CHACHA20POLY1305_OLD:
1797#endif
David Benjamin13414b32015-12-09 23:02:39 -05001798 case SSL_CHACHA20POLY1305:
David Benjamin9f2e2772015-11-18 09:59:43 -05001799 alg_bits = 256;
1800 strength_bits = 256;
1801 break;
1802
1803 case SSL_3DES:
1804 alg_bits = 168;
1805 strength_bits = 112;
1806 break;
1807
1808 case SSL_eNULL:
1809 alg_bits = 0;
1810 strength_bits = 0;
1811 break;
1812
1813 default:
1814 assert(0);
1815 alg_bits = 0;
1816 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001817 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001818
1819 if (out_alg_bits != NULL) {
1820 *out_alg_bits = alg_bits;
1821 }
1822 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001823}
1824
Adam Langleyfcf25832014-12-18 17:42:32 -08001825const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1826 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001828 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 alg_mkey = cipher->algorithm_mkey;
1831 alg_auth = cipher->algorithm_auth;
1832 alg_enc = cipher->algorithm_enc;
1833 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 switch (alg_mkey) {
1836 case SSL_kRSA:
1837 kx = "RSA";
1838 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
David Benjamin7061e282015-03-19 11:10:48 -04001840 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 kx = "DH";
1842 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001843
David Benjamin7061e282015-03-19 11:10:48 -04001844 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 kx = "ECDH";
1846 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Matt Braithwaite053931e2016-05-25 12:06:05 -07001848 case SSL_kCECPQ1:
1849 kx = "CECPQ1";
1850 break;
1851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 case SSL_kPSK:
1853 kx = "PSK";
1854 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001855
Steven Valdez803c77a2016-09-06 14:13:43 -04001856 case SSL_kGENERIC:
1857 kx = "GENERIC";
1858 break;
1859
Adam Langleyfcf25832014-12-18 17:42:32 -08001860 default:
1861 kx = "unknown";
1862 }
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 switch (alg_auth) {
1865 case SSL_aRSA:
1866 au = "RSA";
1867 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 case SSL_aECDSA:
1870 au = "ECDSA";
1871 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001872
Adam Langleyfcf25832014-12-18 17:42:32 -08001873 case SSL_aPSK:
1874 au = "PSK";
1875 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001876
Steven Valdez803c77a2016-09-06 14:13:43 -04001877 case SSL_aGENERIC:
1878 au = "GENERIC";
1879 break;
1880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 default:
1882 au = "unknown";
1883 break;
1884 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001885
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 switch (alg_enc) {
1887 case SSL_3DES:
1888 enc = "3DES(168)";
1889 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001890
Adam Langleyfcf25832014-12-18 17:42:32 -08001891 case SSL_AES128:
1892 enc = "AES(128)";
1893 break;
1894
1895 case SSL_AES256:
1896 enc = "AES(256)";
1897 break;
1898
1899 case SSL_AES128GCM:
1900 enc = "AESGCM(128)";
1901 break;
1902
1903 case SSL_AES256GCM:
1904 enc = "AESGCM(256)";
1905 break;
1906
Brian Smith271777f2015-10-03 13:53:33 -10001907 case SSL_CHACHA20POLY1305_OLD:
David Benjamin13414b32015-12-09 23:02:39 -05001908 enc = "ChaCha20-Poly1305-Old";
1909 break;
1910
1911 case SSL_CHACHA20POLY1305:
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 enc = "ChaCha20-Poly1305";
1913 break;
1914
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001915 case SSL_eNULL:
1916 enc="None";
1917 break;
1918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 default:
1920 enc = "unknown";
1921 break;
1922 }
1923
1924 switch (alg_mac) {
1925 case SSL_MD5:
1926 mac = "MD5";
1927 break;
1928
1929 case SSL_SHA1:
1930 mac = "SHA1";
1931 break;
1932
1933 case SSL_SHA256:
1934 mac = "SHA256";
1935 break;
1936
1937 case SSL_SHA384:
1938 mac = "SHA384";
1939 break;
1940
1941 case SSL_AEAD:
1942 mac = "AEAD";
1943 break;
1944
1945 default:
1946 mac = "unknown";
1947 break;
1948 }
1949
1950 if (buf == NULL) {
1951 len = 128;
1952 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001953 if (buf == NULL) {
1954 return NULL;
1955 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 } else if (len < 128) {
1957 return "Buffer too small";
1958 }
1959
Brian Smith0687bdf2016-01-17 09:18:26 -10001960 BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1961 cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 return buf;
1963}
1964
David Benjamin71f07942015-04-08 02:36:59 -04001965const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1966 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001967}
1968
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001969COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001970
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001971int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001972
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001973const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langley3e9e0432016-10-03 15:58:07 -07001975void SSL_COMP_free_compression_methods(void) {}
1976
David Benjamind1d80782015-07-05 11:54:09 -04001977int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001978 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001979
Adam Langleyfcf25832014-12-18 17:42:32 -08001980 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001981 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001983 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 }
Adam Langley95c29f32014-06-20 12:00:00 -07001985
David Benjamind1d80782015-07-05 11:54:09 -04001986 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001987}
David Benjamin9c651c92014-07-12 13:27:45 -04001988
David Benjaminc032dfa2016-05-12 14:54:57 -04001989int ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
1990 return (cipher->algorithm_auth & SSL_aCERT) != 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001991}
1992
Adam Langleyfcf25832014-12-18 17:42:32 -08001993int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1994 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
Matt Braithwaite053931e2016-05-25 12:06:05 -07001995 if (cipher->algorithm_mkey & SSL_kDHE ||
1996 cipher->algorithm_mkey & SSL_kECDHE ||
1997 cipher->algorithm_mkey & SSL_kCECPQ1) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 return 1;
1999 }
2000
2001 /* It is optional in all others. */
2002 return 0;
2003}
David Benjaminb8d28cf2015-07-28 21:34:45 -04002004
2005size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
2006 size_t block_size;
2007 switch (cipher->algorithm_enc) {
2008 case SSL_3DES:
2009 block_size = 8;
2010 break;
2011 case SSL_AES128:
2012 case SSL_AES256:
2013 block_size = 16;
2014 break;
2015 default:
2016 return 0;
2017 }
2018
2019 size_t mac_len;
2020 switch (cipher->algorithm_mac) {
2021 case SSL_MD5:
2022 mac_len = MD5_DIGEST_LENGTH;
2023 break;
2024 case SSL_SHA1:
2025 mac_len = SHA_DIGEST_LENGTH;
2026 break;
2027 default:
2028 return 0;
2029 }
2030
2031 size_t ret = 1 + mac_len;
2032 ret += block_size - (ret % block_size);
2033 return ret;
2034}