blob: 0c02389a2b3158cfdcc3f25d95867e7f56772ac2 [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 Benjamin86e95b82017-07-18 16:34:25 -0400157namespace bssl {
158
David Benjaminc11ea9422017-08-29 16:33:21 -0400159// kCiphers is an array of all supported ciphers, sorted by id.
David Benjamin20c37312015-11-11 21:33:18 -0800160static const SSL_CIPHER kCiphers[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400161 // The RSA ciphers
162 // Cipher 02
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700163 {
David Benjaminff2df332015-11-18 10:01:16 -0500164 SSL3_TXT_RSA_NULL_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400165 "TLS_RSA_WITH_NULL_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500166 SSL3_CK_RSA_NULL_SHA,
167 SSL_kRSA,
168 SSL_aRSA,
169 SSL_eNULL,
170 SSL_SHA1,
171 SSL_HANDSHAKE_MAC_DEFAULT,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700172 },
173
David Benjaminc11ea9422017-08-29 16:33:21 -0400174 // Cipher 0A
David Benjamina1c90a52015-05-30 17:03:14 -0400175 {
David Benjaminff2df332015-11-18 10:01:16 -0500176 SSL3_TXT_RSA_DES_192_CBC3_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400177 "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500178 SSL3_CK_RSA_DES_192_CBC3_SHA,
179 SSL_kRSA,
180 SSL_aRSA,
181 SSL_3DES,
182 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500183 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400184 },
185
186
David Benjaminc11ea9422017-08-29 16:33:21 -0400187 // New AES ciphersuites
David Benjamina1c90a52015-05-30 17:03:14 -0400188
David Benjaminc11ea9422017-08-29 16:33:21 -0400189 // Cipher 2F
David Benjamina1c90a52015-05-30 17:03:14 -0400190 {
David Benjaminff2df332015-11-18 10:01:16 -0500191 TLS1_TXT_RSA_WITH_AES_128_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400192 "TLS_RSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500193 TLS1_CK_RSA_WITH_AES_128_SHA,
194 SSL_kRSA,
195 SSL_aRSA,
196 SSL_AES128,
197 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500198 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400199 },
200
David Benjaminc11ea9422017-08-29 16:33:21 -0400201 // Cipher 35
David Benjamina1c90a52015-05-30 17:03:14 -0400202 {
David Benjaminff2df332015-11-18 10:01:16 -0500203 TLS1_TXT_RSA_WITH_AES_256_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400204 "TLS_RSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500205 TLS1_CK_RSA_WITH_AES_256_SHA,
206 SSL_kRSA,
207 SSL_aRSA,
208 SSL_AES256,
209 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500210 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400211 },
212
David Benjamina1c90a52015-05-30 17:03:14 -0400213
David Benjaminc11ea9422017-08-29 16:33:21 -0400214 // TLS v1.2 ciphersuites
David Benjamina1c90a52015-05-30 17:03:14 -0400215
David Benjaminc11ea9422017-08-29 16:33:21 -0400216 // Cipher 3C
David Benjamina1c90a52015-05-30 17:03:14 -0400217 {
David Benjaminff2df332015-11-18 10:01:16 -0500218 TLS1_TXT_RSA_WITH_AES_128_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400219 "TLS_RSA_WITH_AES_128_CBC_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500220 TLS1_CK_RSA_WITH_AES_128_SHA256,
221 SSL_kRSA,
222 SSL_aRSA,
223 SSL_AES128,
224 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500225 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400226 },
227
David Benjaminc11ea9422017-08-29 16:33:21 -0400228 // Cipher 3D
David Benjamina1c90a52015-05-30 17:03:14 -0400229 {
David Benjaminff2df332015-11-18 10:01:16 -0500230 TLS1_TXT_RSA_WITH_AES_256_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400231 "TLS_RSA_WITH_AES_256_CBC_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500232 TLS1_CK_RSA_WITH_AES_256_SHA256,
233 SSL_kRSA,
234 SSL_aRSA,
235 SSL_AES256,
236 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500237 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400238 },
239
David Benjaminc11ea9422017-08-29 16:33:21 -0400240 // PSK cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700241
David Benjaminc11ea9422017-08-29 16:33:21 -0400242 // Cipher 8C
David Benjamina1c90a52015-05-30 17:03:14 -0400243 {
David Benjaminff2df332015-11-18 10:01:16 -0500244 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400245 "TLS_PSK_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500246 TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
247 SSL_kPSK,
248 SSL_aPSK,
249 SSL_AES128,
250 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500251 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400252 },
253
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // Cipher 8D
David Benjamina1c90a52015-05-30 17:03:14 -0400255 {
David Benjaminff2df332015-11-18 10:01:16 -0500256 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400257 "TLS_PSK_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500258 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
259 SSL_kPSK,
260 SSL_aPSK,
261 SSL_AES256,
262 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500263 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400264 },
265
David Benjaminc11ea9422017-08-29 16:33:21 -0400266 // GCM ciphersuites from RFC5288
David Benjamina1c90a52015-05-30 17:03:14 -0400267
David Benjaminc11ea9422017-08-29 16:33:21 -0400268 // Cipher 9C
David Benjamina1c90a52015-05-30 17:03:14 -0400269 {
270 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400271 "TLS_RSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500272 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
273 SSL_kRSA,
274 SSL_aRSA,
275 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500276 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400277 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400278 },
279
David Benjaminc11ea9422017-08-29 16:33:21 -0400280 // Cipher 9D
David Benjamina1c90a52015-05-30 17:03:14 -0400281 {
282 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400283 "TLS_RSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500284 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
285 SSL_kRSA,
286 SSL_aRSA,
287 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500288 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400289 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400290 },
291
David Benjaminc11ea9422017-08-29 16:33:21 -0400292 // TLS 1.3 suites.
Steven Valdez803c77a2016-09-06 14:13:43 -0400293
David Benjaminc11ea9422017-08-29 16:33:21 -0400294 // Cipher 1301
Steven Valdez803c77a2016-09-06 14:13:43 -0400295 {
296 TLS1_TXT_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400297 "TLS_AES_128_GCM_SHA256",
Steven Valdez803c77a2016-09-06 14:13:43 -0400298 TLS1_CK_AES_128_GCM_SHA256,
299 SSL_kGENERIC,
300 SSL_aGENERIC,
301 SSL_AES128GCM,
302 SSL_AEAD,
303 SSL_HANDSHAKE_MAC_SHA256,
304 },
305
David Benjaminc11ea9422017-08-29 16:33:21 -0400306 // Cipher 1302
Steven Valdez803c77a2016-09-06 14:13:43 -0400307 {
308 TLS1_TXT_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400309 "TLS_AES_256_GCM_SHA384",
Steven Valdez803c77a2016-09-06 14:13:43 -0400310 TLS1_CK_AES_256_GCM_SHA384,
311 SSL_kGENERIC,
312 SSL_aGENERIC,
313 SSL_AES256GCM,
314 SSL_AEAD,
315 SSL_HANDSHAKE_MAC_SHA384,
316 },
317
David Benjaminc11ea9422017-08-29 16:33:21 -0400318 // Cipher 1303
Steven Valdez803c77a2016-09-06 14:13:43 -0400319 {
320 TLS1_TXT_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400321 "TLS_CHACHA20_POLY1305_SHA256",
Steven Valdez803c77a2016-09-06 14:13:43 -0400322 TLS1_CK_CHACHA20_POLY1305_SHA256,
323 SSL_kGENERIC,
324 SSL_aGENERIC,
325 SSL_CHACHA20POLY1305,
326 SSL_AEAD,
327 SSL_HANDSHAKE_MAC_SHA256,
328 },
329
David Benjaminc11ea9422017-08-29 16:33:21 -0400330 // Cipher C009
David Benjamina1c90a52015-05-30 17:03:14 -0400331 {
332 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400333 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500334 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
335 SSL_kECDHE,
336 SSL_aECDSA,
337 SSL_AES128,
338 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500339 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400340 },
341
David Benjaminc11ea9422017-08-29 16:33:21 -0400342 // Cipher C00A
David Benjamina1c90a52015-05-30 17:03:14 -0400343 {
344 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400345 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500346 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
347 SSL_kECDHE,
348 SSL_aECDSA,
349 SSL_AES256,
350 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500351 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400352 },
353
David Benjaminc11ea9422017-08-29 16:33:21 -0400354 // Cipher C013
David Benjamina1c90a52015-05-30 17:03:14 -0400355 {
356 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400357 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500358 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
359 SSL_kECDHE,
360 SSL_aRSA,
361 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500362 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500363 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400364 },
365
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // Cipher C014
David Benjamina1c90a52015-05-30 17:03:14 -0400367 {
368 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400369 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500370 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
371 SSL_kECDHE,
372 SSL_aRSA,
373 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500374 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500375 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400376 },
377
378
David Benjaminc11ea9422017-08-29 16:33:21 -0400379 // HMAC based TLS v1.2 ciphersuites from RFC5289
David Benjamina1c90a52015-05-30 17:03:14 -0400380
David Benjaminc11ea9422017-08-29 16:33:21 -0400381 // Cipher C023
David Benjamina1c90a52015-05-30 17:03:14 -0400382 {
383 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400384 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500385 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
386 SSL_kECDHE,
387 SSL_aECDSA,
388 SSL_AES128,
389 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500390 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400391 },
392
David Benjaminc11ea9422017-08-29 16:33:21 -0400393 // Cipher C024
David Benjamina1c90a52015-05-30 17:03:14 -0400394 {
395 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400396 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500397 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
398 SSL_kECDHE,
399 SSL_aECDSA,
400 SSL_AES256,
401 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500402 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400403 },
404
David Benjaminc11ea9422017-08-29 16:33:21 -0400405 // Cipher C027
David Benjamina1c90a52015-05-30 17:03:14 -0400406 {
407 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400408 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500409 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
410 SSL_kECDHE,
411 SSL_aRSA,
412 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500413 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500414 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400415 },
416
David Benjaminc11ea9422017-08-29 16:33:21 -0400417 // Cipher C028
David Benjamina1c90a52015-05-30 17:03:14 -0400418 {
419 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400420 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500421 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
422 SSL_kECDHE,
423 SSL_aRSA,
424 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500425 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500426 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400427 },
428
429
David Benjaminc11ea9422017-08-29 16:33:21 -0400430 // GCM based TLS v1.2 ciphersuites from RFC5289
David Benjamina1c90a52015-05-30 17:03:14 -0400431
David Benjaminc11ea9422017-08-29 16:33:21 -0400432 // Cipher C02B
David Benjamina1c90a52015-05-30 17:03:14 -0400433 {
434 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400435 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500436 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
437 SSL_kECDHE,
438 SSL_aECDSA,
439 SSL_AES128GCM,
440 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400441 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400442 },
443
David Benjaminc11ea9422017-08-29 16:33:21 -0400444 // Cipher C02C
David Benjamina1c90a52015-05-30 17:03:14 -0400445 {
446 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400447 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500448 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
449 SSL_kECDHE,
450 SSL_aECDSA,
451 SSL_AES256GCM,
452 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400453 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400454 },
455
David Benjaminc11ea9422017-08-29 16:33:21 -0400456 // Cipher C02F
David Benjamina1c90a52015-05-30 17:03:14 -0400457 {
458 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400459 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500460 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
461 SSL_kECDHE,
462 SSL_aRSA,
463 SSL_AES128GCM,
464 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400465 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400466 },
467
David Benjaminc11ea9422017-08-29 16:33:21 -0400468 // Cipher C030
David Benjamina1c90a52015-05-30 17:03:14 -0400469 {
470 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400471 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500472 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
473 SSL_kECDHE,
474 SSL_aRSA,
475 SSL_AES256GCM,
476 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400477 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400478 },
479
David Benjaminc11ea9422017-08-29 16:33:21 -0400480 // ECDHE-PSK cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700481
David Benjaminc11ea9422017-08-29 16:33:21 -0400482 // Cipher C035
Adam Langley85bc5602015-06-09 09:54:04 -0700483 {
484 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400485 "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
Adam Langley85bc5602015-06-09 09:54:04 -0700486 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500487 SSL_kECDHE,
488 SSL_aPSK,
489 SSL_AES128,
490 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500491 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700492 },
493
David Benjaminc11ea9422017-08-29 16:33:21 -0400494 // Cipher C036
Adam Langley85bc5602015-06-09 09:54:04 -0700495 {
496 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400497 "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
Adam Langley85bc5602015-06-09 09:54:04 -0700498 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500499 SSL_kECDHE,
500 SSL_aPSK,
501 SSL_AES256,
502 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500503 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700504 },
505
David Benjaminc11ea9422017-08-29 16:33:21 -0400506 // ChaCha20-Poly1305 cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700507
David Benjaminc11ea9422017-08-29 16:33:21 -0400508 // Cipher CCA8
David Benjamin13414b32015-12-09 23:02:39 -0500509 {
510 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400511 "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500512 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
513 SSL_kECDHE,
514 SSL_aRSA,
515 SSL_CHACHA20POLY1305,
516 SSL_AEAD,
517 SSL_HANDSHAKE_MAC_SHA256,
518 },
519
David Benjaminc11ea9422017-08-29 16:33:21 -0400520 // Cipher CCA9
David Benjamin13414b32015-12-09 23:02:39 -0500521 {
522 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400523 "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500524 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
525 SSL_kECDHE,
526 SSL_aECDSA,
527 SSL_CHACHA20POLY1305,
528 SSL_AEAD,
529 SSL_HANDSHAKE_MAC_SHA256,
530 },
531
David Benjaminc11ea9422017-08-29 16:33:21 -0400532 // Cipher CCAB
David Benjamin13414b32015-12-09 23:02:39 -0500533 {
534 TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400535 "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500536 TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
537 SSL_kECDHE,
538 SSL_aPSK,
539 SSL_CHACHA20POLY1305,
540 SSL_AEAD,
541 SSL_HANDSHAKE_MAC_SHA256,
542 },
Matt Braithwaite053931e2016-05-25 12:06:05 -0700543
David Benjamina1c90a52015-05-30 17:03:14 -0400544};
545
Steven Valdezcb966542016-08-17 16:56:14 -0400546static const size_t kCiphersLen = OPENSSL_ARRAY_SIZE(kCiphers);
David Benjamina1c90a52015-05-30 17:03:14 -0400547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548#define CIPHER_ADD 1
549#define CIPHER_KILL 2
550#define CIPHER_DEL 3
551#define CIPHER_ORD 4
552#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554typedef struct cipher_order_st {
555 const SSL_CIPHER *cipher;
David Benjaminf4962492017-09-27 19:34:32 -0400556 bool active;
557 bool in_group;
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 struct cipher_order_st *next, *prev;
559} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700560
David Benjamin0344daf2015-04-08 02:08:01 -0400561typedef struct cipher_alias_st {
David Benjaminc11ea9422017-08-29 16:33:21 -0400562 // name is the name of the cipher alias.
David Benjamin0344daf2015-04-08 02:08:01 -0400563 const char *name;
564
David Benjaminc11ea9422017-08-29 16:33:21 -0400565 // The following fields are bitmasks for the corresponding fields on
566 // |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
567 // bit corresponding to the cipher's value is set to 1. If any bitmask is
568 // all zeroes, the alias matches nothing. Use |~0u| for the default value.
David Benjamin0344daf2015-04-08 02:08:01 -0400569 uint32_t algorithm_mkey;
570 uint32_t algorithm_auth;
571 uint32_t algorithm_enc;
572 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500573
David Benjaminc11ea9422017-08-29 16:33:21 -0400574 // min_version, if non-zero, matches all ciphers which were added in that
575 // particular protocol version.
David Benjamindcb6ef02015-11-06 15:35:54 -0500576 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400577} CIPHER_ALIAS;
578
David Benjamina1c90a52015-05-30 17:03:14 -0400579static const CIPHER_ALIAS kCipherAliases[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400580 // "ALL" doesn't include eNULL. It must be explicitly enabled.
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800581 {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700582
David Benjaminc11ea9422017-08-29 16:33:21 -0400583 // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing.
Adam Langley95c29f32014-06-20 12:00:00 -0700584
David Benjaminc11ea9422017-08-29 16:33:21 -0400585 // key exchange aliases
586 // (some of those using only a single bit here combine
587 // multiple key exchange algs according to the RFCs.
David Benjamind6e9eec2015-11-18 09:48:55 -0500588 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjamind6e9eec2015-11-18 09:48:55 -0500590 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
591 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
592 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700593
David Benjamind6e9eec2015-11-18 09:48:55 -0500594 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700595
David Benjaminc11ea9422017-08-29 16:33:21 -0400596 // server authentication aliases
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800597 {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
598 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
599 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500600 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700601
David Benjaminc11ea9422017-08-29 16:33:21 -0400602 // aliases combining key exchange and server authentication
David Benjamind6e9eec2015-11-18 09:48:55 -0500603 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
604 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
605 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
606 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjaminc11ea9422017-08-29 16:33:21 -0400608 // symmetric encryption aliases
David Benjamind6e9eec2015-11-18 09:48:55 -0500609 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500610 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800611 {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
612 {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
613 {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
Adam Langley2e839242017-01-19 15:12:44 -0800614 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjaminc11ea9422017-08-29 16:33:21 -0400616 // MAC aliases
David Benjamind6e9eec2015-11-18 09:48:55 -0500617 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
618 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800619 {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
620 {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjaminc11ea9422017-08-29 16:33:21 -0400622 // Legacy protocol minimum version aliases. "TLSv1" is intentionally the
623 // same as "SSLv3".
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800624 {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
625 {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
626 {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700627
David Benjaminc11ea9422017-08-29 16:33:21 -0400628 // Legacy strength classes.
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800629 {"HIGH", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
630 {"FIPS", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800631};
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Steven Valdezcb966542016-08-17 16:56:14 -0400633static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
David Benjamina1c90a52015-05-30 17:03:14 -0400634
635static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
David Benjamine64d2c72017-07-12 16:31:08 -0400636 const SSL_CIPHER *a = reinterpret_cast<const SSL_CIPHER *>(in_a);
637 const SSL_CIPHER *b = reinterpret_cast<const SSL_CIPHER *>(in_b);
David Benjamina1c90a52015-05-30 17:03:14 -0400638
639 if (a->id > b->id) {
640 return 1;
641 } else if (a->id < b->id) {
642 return -1;
643 } else {
644 return 0;
645 }
646}
647
David Benjaminf4962492017-09-27 19:34:32 -0400648bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
649 size_t *out_mac_secret_len,
650 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
651 uint16_t version, int is_dtls) {
David Benjaminea72bd02014-12-21 21:27:41 -0500652 *out_aead = NULL;
653 *out_mac_secret_len = 0;
654 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700655
Steven Valdez2f3404b2017-05-24 16:54:35 -0400656 const int is_tls12 = version == TLS1_2_VERSION && !is_dtls;
657
David Benjamin305e6fb2016-10-27 18:19:00 -0400658 if (cipher->algorithm_mac == SSL_AEAD) {
659 if (cipher->algorithm_enc == SSL_AES128GCM) {
Steven Valdez2f3404b2017-05-24 16:54:35 -0400660 *out_aead =
661 is_tls12 ? EVP_aead_aes_128_gcm_tls12() : EVP_aead_aes_128_gcm();
David Benjaminea72bd02014-12-21 21:27:41 -0500662 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400663 } else if (cipher->algorithm_enc == SSL_AES256GCM) {
Steven Valdez2f3404b2017-05-24 16:54:35 -0400664 *out_aead =
665 is_tls12 ? EVP_aead_aes_256_gcm_tls12() : EVP_aead_aes_256_gcm();
David Benjaminea72bd02014-12-21 21:27:41 -0500666 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400667 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
David Benjamin13414b32015-12-09 23:02:39 -0500668 *out_aead = EVP_aead_chacha20_poly1305();
669 *out_fixed_iv_len = 12;
David Benjamin305e6fb2016-10-27 18:19:00 -0400670 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400671 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400672 }
673
David Benjaminc11ea9422017-08-29 16:33:21 -0400674 // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
675 // above computes the TLS 1.2 construction.
David Benjamin305e6fb2016-10-27 18:19:00 -0400676 if (version >= TLS1_3_VERSION) {
677 *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
678 }
679 } else if (cipher->algorithm_mac == SSL_SHA1) {
680 if (cipher->algorithm_enc == SSL_eNULL) {
681 if (version == SSL3_VERSION) {
682 *out_aead = EVP_aead_null_sha1_ssl3();
683 } else {
684 *out_aead = EVP_aead_null_sha1_tls();
685 }
686 } else if (cipher->algorithm_enc == SSL_3DES) {
687 if (version == SSL3_VERSION) {
688 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
689 *out_fixed_iv_len = 8;
690 } else if (version == TLS1_VERSION) {
691 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
692 *out_fixed_iv_len = 8;
693 } else {
694 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
695 }
696 } else if (cipher->algorithm_enc == SSL_AES128) {
697 if (version == SSL3_VERSION) {
698 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
699 *out_fixed_iv_len = 16;
700 } else if (version == TLS1_VERSION) {
701 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
702 *out_fixed_iv_len = 16;
703 } else {
704 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
705 }
706 } else if (cipher->algorithm_enc == SSL_AES256) {
707 if (version == SSL3_VERSION) {
708 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
709 *out_fixed_iv_len = 16;
710 } else if (version == TLS1_VERSION) {
711 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
712 *out_fixed_iv_len = 16;
713 } else {
714 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
715 }
716 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400717 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400718 }
719
720 *out_mac_secret_len = SHA_DIGEST_LENGTH;
721 } else if (cipher->algorithm_mac == SSL_SHA256) {
722 if (cipher->algorithm_enc == SSL_AES128) {
723 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
724 } else if (cipher->algorithm_enc == SSL_AES256) {
725 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
726 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400727 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400728 }
729
730 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
731 } else if (cipher->algorithm_mac == SSL_SHA384) {
732 if (cipher->algorithm_enc != SSL_AES256) {
David Benjaminf4962492017-09-27 19:34:32 -0400733 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400734 }
735
736 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
737 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
738 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400739 return false;
David Benjaminea72bd02014-12-21 21:27:41 -0500740 }
Steven Valdez79750562016-06-16 06:38:04 -0400741
David Benjaminf4962492017-09-27 19:34:32 -0400742 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800743}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700744
David Benjaminca9e8f52017-08-09 15:02:34 -0400745const EVP_MD *ssl_get_handshake_digest(uint16_t version,
746 const SSL_CIPHER *cipher) {
747 switch (cipher->algorithm_prf) {
David Benjaminb0883312015-08-06 09:54:13 -0400748 case SSL_HANDSHAKE_MAC_DEFAULT:
Steven Valdez908ac192017-01-12 13:17:07 -0500749 return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
David Benjaminb0883312015-08-06 09:54:13 -0400750 case SSL_HANDSHAKE_MAC_SHA256:
751 return EVP_sha256();
752 case SSL_HANDSHAKE_MAC_SHA384:
753 return EVP_sha384();
754 default:
David Benjaminca9e8f52017-08-09 15:02:34 -0400755 assert(0);
David Benjaminb0883312015-08-06 09:54:13 -0400756 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800757 }
Adam Langley95c29f32014-06-20 12:00:00 -0700758}
759
Adam Langley22df6912017-07-25 12:27:37 -0700760static bool is_cipher_list_separator(char c, int is_strict) {
761 if (c == ':') {
762 return true;
763 }
764 return !is_strict && (c == ' ' || c == ';' || c == ',');
765}
Adam Langley95c29f32014-06-20 12:00:00 -0700766
David Benjaminf4962492017-09-27 19:34:32 -0400767// rule_equals returns whether the NUL-terminated string |rule| is equal to the
David Benjaminc11ea9422017-08-29 16:33:21 -0400768// |buf_len| bytes at |buf|.
David Benjaminf4962492017-09-27 19:34:32 -0400769static bool rule_equals(const char *rule, const char *buf, size_t buf_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400770 // |strncmp| alone only checks that |buf| is a prefix of |rule|.
David Benjamin0344daf2015-04-08 02:08:01 -0400771 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
772}
773
Adam Langley95c29f32014-06-20 12:00:00 -0700774static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800775 CIPHER_ORDER **tail) {
776 if (curr == *tail) {
777 return;
778 }
779 if (curr == *head) {
780 *head = curr->next;
781 }
782 if (curr->prev != NULL) {
783 curr->prev->next = curr->next;
784 }
785 if (curr->next != NULL) {
786 curr->next->prev = curr->prev;
787 }
788 (*tail)->next = curr;
789 curr->prev = *tail;
790 curr->next = NULL;
791 *tail = curr;
792}
Adam Langley95c29f32014-06-20 12:00:00 -0700793
794static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 CIPHER_ORDER **tail) {
796 if (curr == *head) {
797 return;
798 }
799 if (curr == *tail) {
800 *tail = curr->prev;
801 }
802 if (curr->next != NULL) {
803 curr->next->prev = curr->prev;
804 }
805 if (curr->prev != NULL) {
806 curr->prev->next = curr->next;
807 }
808 (*head)->prev = curr;
809 curr->next = *head;
810 curr->prev = NULL;
811 *head = curr;
812}
Adam Langley95c29f32014-06-20 12:00:00 -0700813
David Benjamin82c9e902014-12-12 15:55:27 -0500814static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 CIPHER_ORDER *co_list,
816 CIPHER_ORDER **head_p,
817 CIPHER_ORDER **tail_p) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400818 // The set of ciphers is static, but some subset may be unsupported by
819 // |ssl_method|, so the list may be smaller.
David Benjamina1c90a52015-05-30 17:03:14 -0400820 size_t co_list_num = 0;
David Benjamin54091232016-09-05 12:47:25 -0400821 for (size_t i = 0; i < kCiphersLen; i++) {
David Benjamina1c90a52015-05-30 17:03:14 -0400822 const SSL_CIPHER *cipher = &kCiphers[i];
David Benjaminabbbee12016-10-31 19:20:42 -0400823 if (ssl_method->supports_cipher(cipher) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400824 // TLS 1.3 ciphers do not participate in this mechanism.
David Benjaminabbbee12016-10-31 19:20:42 -0400825 cipher->algorithm_mkey != SSL_kGENERIC) {
David Benjamina1c90a52015-05-30 17:03:14 -0400826 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 co_list[co_list_num].next = NULL;
828 co_list[co_list_num].prev = NULL;
David Benjaminf4962492017-09-27 19:34:32 -0400829 co_list[co_list_num].active = false;
830 co_list[co_list_num].in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 co_list_num++;
832 }
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
David Benjaminc11ea9422017-08-29 16:33:21 -0400835 // Prepare linked list from list entries.
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (co_list_num > 0) {
837 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (co_list_num > 1) {
840 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700841
David Benjamin54091232016-09-05 12:47:25 -0400842 for (size_t i = 1; i < co_list_num - 1; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 co_list[i].prev = &co_list[i - 1];
844 co_list[i].next = &co_list[i + 1];
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
848 }
849
850 co_list[co_list_num - 1].next = NULL;
851
852 *head_p = &co_list[0];
853 *tail_p = &co_list[co_list_num - 1];
854 }
855}
Adam Langley95c29f32014-06-20 12:00:00 -0700856
David Benjaminc11ea9422017-08-29 16:33:21 -0400857// ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
858// parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
859// head and tail of the list to |*head_p| and |*tail_p|, respectively.
860//
861// - If |cipher_id| is non-zero, only that cipher is selected.
862// - Otherwise, if |strength_bits| is non-negative, it selects ciphers
863// of that strength.
864// - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
865// |min_version|.
Adam Langleyfcf25832014-12-18 17:42:32 -0800866static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400867 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500868 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
David Benjaminf4962492017-09-27 19:34:32 -0400869 int strength_bits, bool in_group, CIPHER_ORDER **head_p,
David Benjamind6e9eec2015-11-18 09:48:55 -0500870 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 CIPHER_ORDER *head, *tail, *curr, *next, *last;
872 const SSL_CIPHER *cp;
David Benjaminf4962492017-09-27 19:34:32 -0400873 bool reverse = false;
Adam Langley95c29f32014-06-20 12:00:00 -0700874
David Benjamindcb6ef02015-11-06 15:35:54 -0500875 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500876 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400877 // The rule matches nothing, so bail early.
David Benjamin0344daf2015-04-08 02:08:01 -0400878 return;
879 }
880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 if (rule == CIPHER_DEL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400882 // needed to maintain sorting between currently deleted ciphers
David Benjaminf4962492017-09-27 19:34:32 -0400883 reverse = true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 head = *head_p;
887 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (reverse) {
890 next = tail;
891 last = head;
892 } else {
893 next = head;
894 last = tail;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 curr = NULL;
898 for (;;) {
899 if (curr == last) {
900 break;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 curr = next;
904 if (curr == NULL) {
905 break;
906 }
Adam Langleye3142a72014-07-24 17:56:48 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 next = reverse ? curr->prev : curr->next;
909 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700910
David Benjaminc11ea9422017-08-29 16:33:21 -0400911 // Selection criteria is either a specific cipher, the value of
912 // |strength_bits|, or the algorithms used.
David Benjamin0344daf2015-04-08 02:08:01 -0400913 if (cipher_id != 0) {
914 if (cipher_id != cp->id) {
915 continue;
916 }
917 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500918 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 continue;
920 }
David Benjamin881f1962016-08-10 18:29:12 -0400921 } else {
922 if (!(alg_mkey & cp->algorithm_mkey) ||
923 !(alg_auth & cp->algorithm_auth) ||
924 !(alg_enc & cp->algorithm_enc) ||
925 !(alg_mac & cp->algorithm_mac) ||
926 (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version)) {
927 continue;
928 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 }
Adam Langleye3142a72014-07-24 17:56:48 -0700930
David Benjaminc11ea9422017-08-29 16:33:21 -0400931 // add the cipher if it has not been added yet.
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (rule == CIPHER_ADD) {
David Benjaminf4962492017-09-27 19:34:32 -0400933 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (!curr->active) {
935 ll_append_tail(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400936 curr->active = true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 curr->in_group = in_group;
938 }
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
David Benjaminc11ea9422017-08-29 16:33:21 -0400941 // Move the added cipher to this location
Adam Langleyfcf25832014-12-18 17:42:32 -0800942 else if (rule == CIPHER_ORD) {
David Benjaminf4962492017-09-27 19:34:32 -0400943 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 if (curr->active) {
945 ll_append_tail(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400946 curr->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 }
948 } else if (rule == CIPHER_DEL) {
David Benjaminf4962492017-09-27 19:34:32 -0400949 // reverse == true
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (curr->active) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400951 // most recently deleted ciphersuites get best positions
952 // for any future CIPHER_ADD (note that the CIPHER_DEL loop
953 // works in reverse to maintain the order)
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 ll_append_head(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400955 curr->active = false;
956 curr->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 }
958 } else if (rule == CIPHER_KILL) {
David Benjaminf4962492017-09-27 19:34:32 -0400959 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (head == curr) {
961 head = curr->next;
962 } else {
963 curr->prev->next = curr->next;
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 if (tail == curr) {
967 tail = curr->prev;
968 }
David Benjaminf4962492017-09-27 19:34:32 -0400969 curr->active = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 if (curr->next != NULL) {
971 curr->next->prev = curr->prev;
972 }
973 if (curr->prev != NULL) {
974 curr->prev->next = curr->next;
975 }
976 curr->next = NULL;
977 curr->prev = NULL;
978 }
979 }
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 *head_p = head;
982 *tail_p = tail;
983}
Adam Langley95c29f32014-06-20 12:00:00 -0700984
David Benjaminf4962492017-09-27 19:34:32 -0400985static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
986 CIPHER_ORDER **tail_p) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400987 // This routine sorts the ciphers with descending strength. The sorting must
988 // keep the pre-sorted sequence, so we apply the normal sorting routine as
989 // '+' movement to the end of the list.
David Benjaminf4962492017-09-27 19:34:32 -0400990 int max_strength_bits = 0;
991 CIPHER_ORDER *curr = *head_p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500993 if (curr->active &&
994 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
995 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 }
997 curr = curr->next;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
David Benjaminf4962492017-09-27 19:34:32 -04001000 Array<int> number_uses;
1001 if (!number_uses.Init(max_strength_bits + 1)) {
1002 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 }
David Benjaminf4962492017-09-27 19:34:32 -04001004 OPENSSL_memset(number_uses.data(), 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -07001005
David Benjaminc11ea9422017-08-29 16:33:21 -04001006 // Now find the strength_bits values actually used.
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 curr = *head_p;
1008 while (curr != NULL) {
1009 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001010 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 }
1012 curr = curr->next;
1013 }
Adam Langley95c29f32014-06-20 12:00:00 -07001014
David Benjaminc11ea9422017-08-29 16:33:21 -04001015 // Go through the list of used strength_bits values in descending order.
David Benjaminf4962492017-09-27 19:34:32 -04001016 for (int i = max_strength_bits; i >= 0; i--) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 if (number_uses[i] > 0) {
David Benjaminf4962492017-09-27 19:34:32 -04001018 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, false, head_p,
1019 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 }
1021 }
1022
David Benjaminf4962492017-09-27 19:34:32 -04001023 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001024}
Adam Langley95c29f32014-06-20 12:00:00 -07001025
David Benjaminf4962492017-09-27 19:34:32 -04001026static bool ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1027 const char *rule_str,
1028 CIPHER_ORDER **head_p,
1029 CIPHER_ORDER **tail_p, bool strict) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001030 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001031 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 const char *l, *buf;
David Benjaminf4962492017-09-27 19:34:32 -04001033 int rule;
1034 bool multi, skip_rule, in_group = false, has_group = false;
David Benjamin0344daf2015-04-08 02:08:01 -04001035 size_t j, buf_len;
1036 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 l = rule_str;
1040 for (;;) {
1041 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 if (ch == '\0') {
David Benjaminc11ea9422017-08-29 16:33:21 -04001044 break; // done
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 if (in_group) {
1048 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 if (*tail_p) {
David Benjaminf4962492017-09-27 19:34:32 -04001050 (*tail_p)->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 }
David Benjaminf4962492017-09-27 19:34:32 -04001052 in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 l++;
1054 continue;
1055 }
David Benjamin37d92462014-09-20 17:54:24 -04001056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 if (ch == '|') {
1058 rule = CIPHER_ADD;
1059 l++;
1060 continue;
1061 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1062 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001063 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjaminf4962492017-09-27 19:34:32 -04001064 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 } else {
1066 rule = CIPHER_ADD;
1067 }
1068 } else if (ch == '-') {
1069 rule = CIPHER_DEL;
1070 l++;
1071 } else if (ch == '+') {
1072 rule = CIPHER_ORD;
1073 l++;
1074 } else if (ch == '!') {
1075 rule = CIPHER_KILL;
1076 l++;
1077 } else if (ch == '@') {
1078 rule = CIPHER_SPECIAL;
1079 l++;
1080 } else if (ch == '[') {
David Benjaminbf5f1922017-07-01 11:13:53 -04001081 assert(!in_group);
David Benjaminf4962492017-09-27 19:34:32 -04001082 in_group = true;
1083 has_group = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 l++;
1085 continue;
1086 } else {
1087 rule = CIPHER_ADD;
1088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
David Benjaminc11ea9422017-08-29 16:33:21 -04001090 // If preference groups are enabled, the only legal operator is +.
1091 // Otherwise the in_group bits will get mixed up.
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001093 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjaminf4962492017-09-27 19:34:32 -04001094 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 }
Adam Langley95c29f32014-06-20 12:00:00 -07001096
Adam Langley22df6912017-07-25 12:27:37 -07001097 if (is_cipher_list_separator(ch, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 l++;
1099 continue;
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
David Benjaminf4962492017-09-27 19:34:32 -04001102 multi = false;
David Benjamin0344daf2015-04-08 02:08:01 -04001103 cipher_id = 0;
1104 alg_mkey = ~0u;
1105 alg_auth = ~0u;
1106 alg_enc = ~0u;
1107 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001108 min_version = 0;
David Benjaminf4962492017-09-27 19:34:32 -04001109 skip_rule = false;
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 for (;;) {
1112 ch = *l;
1113 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001114 buf_len = 0;
David Benjamin6fff3862017-06-21 21:07:04 -04001115 while ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') ||
1116 (ch >= 'a' && ch <= 'z') || ch == '-' || ch == '.' || ch == '_') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001118 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 }
Adam Langley95c29f32014-06-20 12:00:00 -07001120
David Benjamin0344daf2015-04-08 02:08:01 -04001121 if (buf_len == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001122 // We hit something we cannot deal with, it is no command or separator
1123 // nor alphanumeric, so we call this an error.
David Benjamin3570d732015-06-29 00:28:17 -04001124 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001125 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001126 }
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 break;
1130 }
David Benjamin0344daf2015-04-08 02:08:01 -04001131
David Benjaminc11ea9422017-08-29 16:33:21 -04001132 // Look for a matching exact cipher. These aren't allowed in multipart
1133 // rules.
David Benjamin0344daf2015-04-08 02:08:01 -04001134 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001135 for (j = 0; j < kCiphersLen; j++) {
1136 const SSL_CIPHER *cipher = &kCiphers[j];
David Benjamin6fff3862017-06-21 21:07:04 -04001137 if (rule_equals(cipher->name, buf, buf_len) ||
1138 rule_equals(cipher->standard_name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001139 cipher_id = cipher->id;
1140 break;
1141 }
1142 }
1143 }
1144 if (cipher_id == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001145 // If not an exact cipher, look for a matching cipher alias.
David Benjamina1c90a52015-05-30 17:03:14 -04001146 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001147 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1148 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1149 alg_auth &= kCipherAliases[j].algorithm_auth;
1150 alg_enc &= kCipherAliases[j].algorithm_enc;
1151 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001152
1153 if (min_version != 0 &&
1154 min_version != kCipherAliases[j].min_version) {
David Benjaminf4962492017-09-27 19:34:32 -04001155 skip_rule = true;
David Benjamindcb6ef02015-11-06 15:35:54 -05001156 } else {
1157 min_version = kCipherAliases[j].min_version;
1158 }
David Benjamin0344daf2015-04-08 02:08:01 -04001159 break;
1160 }
1161 }
David Benjamina1c90a52015-05-30 17:03:14 -04001162 if (j == kCipherAliasesLen) {
David Benjaminf4962492017-09-27 19:34:32 -04001163 skip_rule = true;
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001164 if (strict) {
1165 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001166 return false;
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001167 }
David Benjamin0344daf2015-04-08 02:08:01 -04001168 }
1169 }
1170
David Benjaminc11ea9422017-08-29 16:33:21 -04001171 // Check for a multipart rule.
David Benjamin0344daf2015-04-08 02:08:01 -04001172 if (ch != '+') {
1173 break;
1174 }
1175 l++;
David Benjaminf4962492017-09-27 19:34:32 -04001176 multi = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 }
Adam Langley95c29f32014-06-20 12:00:00 -07001178
David Benjaminc11ea9422017-08-29 16:33:21 -04001179 // Ok, we have the rule, now apply it.
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 if (rule == CIPHER_SPECIAL) {
David Benjaminbf5f1922017-07-01 11:13:53 -04001181 if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001182 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001183 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 }
David Benjaminbf5f1922017-07-01 11:13:53 -04001185 if (!ssl_cipher_strength_sort(head_p, tail_p)) {
David Benjaminf4962492017-09-27 19:34:32 -04001186 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
David Benjaminc11ea9422017-08-29 16:33:21 -04001189 // We do not support any "multi" options together with "@", so throw away
1190 // the rest of the command, if any left, until end or ':' is found.
Adam Langley22df6912017-07-25 12:27:37 -07001191 while (*l != '\0' && !is_cipher_list_separator(*l, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 l++;
1193 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001194 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001196 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 }
1198 }
Adam Langley95c29f32014-06-20 12:00:00 -07001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001201 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001202 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
David Benjaminf4962492017-09-27 19:34:32 -04001205 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001206}
Adam Langley95c29f32014-06-20 12:00:00 -07001207
David Benjaminf4962492017-09-27 19:34:32 -04001208bool ssl_create_cipher_list(
Matthew Braithwaite6ad20dc2017-02-21 16:41:33 -08001209 const SSL_PROTOCOL_METHOD *ssl_method,
1210 struct ssl_cipher_preference_list_st **out_cipher_list,
David Benjaminf4962492017-09-27 19:34:32 -04001211 const char *rule_str, bool strict) {
David Benjamind2cb1c12016-11-02 17:49:09 -04001212 STACK_OF(SSL_CIPHER) *cipherstack = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 uint8_t *in_group_flags = NULL;
1215 unsigned int num_in_group_flags = 0;
1216 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001217
David Benjaminc11ea9422017-08-29 16:33:21 -04001218 // Return with error if nothing to do.
David Benjamin71f07942015-04-08 02:36:59 -04001219 if (rule_str == NULL || out_cipher_list == NULL) {
David Benjaminf4962492017-09-27 19:34:32 -04001220 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 }
David Benjamin5213df42014-08-20 14:19:54 -04001222
David Benjaminc11ea9422017-08-29 16:33:21 -04001223 // Now we have to collect the available ciphers from the compiled in ciphers.
1224 // We cannot get more than the number compiled in, so it is used for
1225 // allocation.
David Benjamine64d2c72017-07-12 16:31:08 -04001226 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001227 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001228 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminf4962492017-09-27 19:34:32 -04001229 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001230 }
Adam Langley95c29f32014-06-20 12:00:00 -07001231
David Benjamina1c90a52015-05-30 17:03:14 -04001232 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001233
David Benjaminc11ea9422017-08-29 16:33:21 -04001234 // Now arrange all ciphers by preference:
1235 // TODO(davidben): Compute this order once and copy it.
Adam Langley95c29f32014-06-20 12:00:00 -07001236
David Benjaminc11ea9422017-08-29 16:33:21 -04001237 // Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
1238 // key exchange mechanisms
David Benjamind6e9eec2015-11-18 09:48:55 -05001239 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
David Benjaminf4962492017-09-27 19:34:32 -04001240 false, &head, &tail);
1241 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamind6e9eec2015-11-18 09:48:55 -05001242 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001243 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
Steven Valdez803c77a2016-09-06 14:13:43 -04001244 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001245
David Benjaminc11ea9422017-08-29 16:33:21 -04001246 // Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1247 // CHACHA20 unless there is hardware support for fast and constant-time
1248 // AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
1249 // old one.
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 if (EVP_has_aes_hardware()) {
David Benjaminf4962492017-09-27 19:34:32 -04001251 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
1252 false, &head, &tail);
1253 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
1254 false, &head, &tail);
David Benjamin13414b32015-12-09 23:02:39 -05001255 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
David Benjaminf4962492017-09-27 19:34:32 -04001256 -1, false, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 } else {
David Benjamin13414b32015-12-09 23:02:39 -05001258 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
David Benjaminf4962492017-09-27 19:34:32 -04001259 -1, false, &head, &tail);
1260 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
1261 false, &head, &tail);
1262 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
1263 false, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 }
Adam Langley95c29f32014-06-20 12:00:00 -07001265
David Benjaminc11ea9422017-08-29 16:33:21 -04001266 // Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
1267 // 3DES_EDE_CBC_SHA.
David Benjaminf4962492017-09-27 19:34:32 -04001268 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamind6e9eec2015-11-18 09:48:55 -05001269 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001270 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamin43336652016-03-03 15:32:29 -05001271 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001272 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, false,
1273 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001274
David Benjaminc11ea9422017-08-29 16:33:21 -04001275 // Temporarily enable everything else for sorting
David Benjaminf4962492017-09-27 19:34:32 -04001276 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false, &head,
David Benjamind6e9eec2015-11-18 09:48:55 -05001277 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001278
David Benjaminc11ea9422017-08-29 16:33:21 -04001279 // Move ciphers without forward secrecy to the end.
David Benjaminf4962492017-09-27 19:34:32 -04001280 ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0, CIPHER_ORD,
1281 -1, false, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001282
David Benjaminc11ea9422017-08-29 16:33:21 -04001283 // Now disable everything (maintaining the ordering!)
David Benjaminf4962492017-09-27 19:34:32 -04001284 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
David Benjamind6e9eec2015-11-18 09:48:55 -05001285 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001286
David Benjaminc11ea9422017-08-29 16:33:21 -04001287 // If the rule_string begins with DEFAULT, apply the default rule before
1288 // using the (possibly available) additional rules.
David Benjamin11a7b3c2016-11-03 17:03:48 -04001289 const char *rule_p = rule_str;
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin11a7b3c2016-11-03 17:03:48 -04001291 if (!ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001292 &tail, strict)) {
David Benjamin11a7b3c2016-11-03 17:03:48 -04001293 goto err;
1294 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001295 rule_p += 7;
1296 if (*rule_p == ':') {
1297 rule_p++;
1298 }
1299 }
Adam Langley858a88d2014-06-20 12:00:00 -07001300
David Benjamin11a7b3c2016-11-03 17:03:48 -04001301 if (*rule_p != '\0' &&
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001302 !ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 goto err;
1304 }
1305
David Benjaminc11ea9422017-08-29 16:33:21 -04001306 // Allocate new "cipherstack" for the result, return with error
1307 // if we cannot get one.
Adam Langleyfcf25832014-12-18 17:42:32 -08001308 cipherstack = sk_SSL_CIPHER_new_null();
1309 if (cipherstack == NULL) {
1310 goto err;
1311 }
1312
David Benjamine64d2c72017-07-12 16:31:08 -04001313 in_group_flags = (uint8_t *)OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 if (!in_group_flags) {
1315 goto err;
1316 }
1317
David Benjaminc11ea9422017-08-29 16:33:21 -04001318 // The cipher selection for the list is done. The ciphers are added
1319 // to the resulting precedence to the STACK_OF(SSL_CIPHER).
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 for (curr = head; curr != NULL; curr = curr->next) {
1321 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001322 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1323 goto err;
1324 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 in_group_flags[num_in_group_flags++] = curr->in_group;
1326 }
1327 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001328 OPENSSL_free(co_list); // Not needed any longer
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 co_list = NULL;
1330
David Benjamine64d2c72017-07-12 16:31:08 -04001331 pref_list = (ssl_cipher_preference_list_st *)OPENSSL_malloc(
1332 sizeof(struct ssl_cipher_preference_list_st));
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 if (!pref_list) {
1334 goto err;
1335 }
1336 pref_list->ciphers = cipherstack;
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -07001337 pref_list->in_group_flags = NULL;
1338 if (num_in_group_flags) {
1339 pref_list->in_group_flags = (uint8_t *)OPENSSL_malloc(num_in_group_flags);
1340 if (!pref_list->in_group_flags) {
1341 goto err;
1342 }
1343 OPENSSL_memcpy(pref_list->in_group_flags, in_group_flags,
1344 num_in_group_flags);
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 OPENSSL_free(in_group_flags);
1347 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001348 if (*out_cipher_list != NULL) {
1349 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 }
David Benjamin71f07942015-04-08 02:36:59 -04001351 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 pref_list = NULL;
1353
David Benjaminc11ea9422017-08-29 16:33:21 -04001354 // Configuring an empty cipher list is an error but still updates the
1355 // output.
David Benjamin91222b82017-03-09 20:10:56 -05001356 if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers) == 0) {
1357 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
David Benjaminf4962492017-09-27 19:34:32 -04001358 return false;
David Benjamin91222b82017-03-09 20:10:56 -05001359 }
1360
David Benjaminf4962492017-09-27 19:34:32 -04001361 return true;
Adam Langley858a88d2014-06-20 12:00:00 -07001362
1363err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001364 OPENSSL_free(co_list);
1365 OPENSSL_free(in_group_flags);
1366 sk_SSL_CIPHER_free(cipherstack);
David Benjamin2755a3e2015-04-22 16:17:58 -04001367 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 OPENSSL_free(pref_list->in_group_flags);
1369 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001370 OPENSSL_free(pref_list);
David Benjaminf4962492017-09-27 19:34:32 -04001371 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001372}
Adam Langley95c29f32014-06-20 12:00:00 -07001373
David Benjamina1c90a52015-05-30 17:03:14 -04001374uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1375 uint32_t id = cipher->id;
David Benjaminc11ea9422017-08-29 16:33:21 -04001376 // All ciphers are SSLv3.
David Benjamina1c90a52015-05-30 17:03:14 -04001377 assert((id & 0xff000000) == 0x03000000);
1378 return id & 0xffff;
1379}
1380
David Benjamin86e95b82017-07-18 16:34:25 -04001381uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
1382 switch (EVP_PKEY_id(key)) {
1383 case EVP_PKEY_RSA:
1384 return SSL_aRSA;
1385 case EVP_PKEY_EC:
1386 case EVP_PKEY_ED25519:
David Benjaminc11ea9422017-08-29 16:33:21 -04001387 // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers.
David Benjamin86e95b82017-07-18 16:34:25 -04001388 return SSL_aECDSA;
1389 default:
1390 return 0;
1391 }
1392}
1393
David Benjaminf4962492017-09-27 19:34:32 -04001394bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
David Benjamin86e95b82017-07-18 16:34:25 -04001395 return (cipher->algorithm_auth & SSL_aCERT) != 0;
1396}
1397
David Benjaminf4962492017-09-27 19:34:32 -04001398bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1399 // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is
1400 // optional or omitted in all others.
1401 return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
David Benjamin86e95b82017-07-18 16:34:25 -04001402}
1403
1404size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1405 size_t block_size;
1406 switch (cipher->algorithm_enc) {
1407 case SSL_3DES:
1408 block_size = 8;
1409 break;
1410 case SSL_AES128:
1411 case SSL_AES256:
1412 block_size = 16;
1413 break;
1414 default:
1415 return 0;
1416 }
1417
David Benjaminc11ea9422017-08-29 16:33:21 -04001418 // All supported TLS 1.0 ciphers use SHA-1.
David Benjamin86e95b82017-07-18 16:34:25 -04001419 assert(cipher->algorithm_mac == SSL_SHA1);
1420 size_t ret = 1 + SHA_DIGEST_LENGTH;
1421 ret += block_size - (ret % block_size);
1422 return ret;
1423}
1424
1425} // namespace bssl
1426
1427using namespace bssl;
1428
1429const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
1430 SSL_CIPHER c;
1431
1432 c.id = 0x03000000L | value;
1433 return reinterpret_cast<const SSL_CIPHER *>(bsearch(
1434 &c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER), ssl_cipher_id_cmp));
1435}
1436
1437uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1438
David Benjamin348f0d82017-08-10 16:06:27 -04001439int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) {
Alessandro Ghedini0726fb72017-01-17 13:27:08 +00001440 return (cipher->algorithm_mac & SSL_AEAD) != 0;
1441}
1442
David Benjamin348f0d82017-08-10 16:06:27 -04001443int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) {
1444 switch (cipher->algorithm_enc) {
1445 case SSL_eNULL:
1446 return NID_undef;
1447 case SSL_3DES:
1448 return NID_des_ede3_cbc;
1449 case SSL_AES128:
1450 return NID_aes_128_cbc;
1451 case SSL_AES256:
1452 return NID_aes_256_cbc;
1453 case SSL_AES128GCM:
1454 return NID_aes_128_gcm;
1455 case SSL_AES256GCM:
1456 return NID_aes_256_gcm;
1457 case SSL_CHACHA20POLY1305:
1458 return NID_chacha20_poly1305;
1459 }
1460 assert(0);
1461 return NID_undef;
1462}
1463
1464int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) {
1465 switch (cipher->algorithm_mac) {
1466 case SSL_AEAD:
1467 return NID_undef;
1468 case SSL_SHA1:
1469 return NID_sha1;
1470 case SSL_SHA256:
1471 return NID_sha256;
1472 case SSL_SHA384:
1473 return NID_sha384;
1474 }
1475 assert(0);
1476 return NID_undef;
1477}
1478
1479int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) {
1480 switch (cipher->algorithm_mkey) {
1481 case SSL_kRSA:
1482 return NID_kx_rsa;
1483 case SSL_kECDHE:
1484 return NID_kx_ecdhe;
1485 case SSL_kPSK:
1486 return NID_kx_psk;
1487 case SSL_kGENERIC:
1488 return NID_kx_any;
1489 }
1490 assert(0);
1491 return NID_undef;
1492}
1493
1494int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) {
1495 switch (cipher->algorithm_auth) {
1496 case SSL_aRSA:
1497 return NID_auth_rsa;
1498 case SSL_aECDSA:
1499 return NID_auth_ecdsa;
1500 case SSL_aPSK:
1501 return NID_auth_psk;
1502 case SSL_aGENERIC:
1503 return NID_auth_any;
1504 }
1505 assert(0);
1506 return NID_undef;
1507}
1508
David Benjaminb1b76ae2017-09-21 17:03:34 -04001509int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) {
1510 switch (cipher->algorithm_prf) {
1511 case SSL_HANDSHAKE_MAC_DEFAULT:
1512 return NID_md5_sha1;
1513 case SSL_HANDSHAKE_MAC_SHA256:
1514 return NID_sha256;
1515 case SSL_HANDSHAKE_MAC_SHA384:
1516 return NID_sha384;
1517 }
1518 assert(0);
1519 return NID_undef;
1520}
1521
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001522int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001523 return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001524 cipher->algorithm_mac != SSL_AEAD;
1525}
1526
David Benjaminef793f42015-11-05 18:16:27 -05001527uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001528 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1529 cipher->algorithm_auth == SSL_aGENERIC) {
1530 return TLS1_3_VERSION;
1531 }
1532
David Benjamindcb6ef02015-11-06 15:35:54 -05001533 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001534 // Cipher suites before TLS 1.2 use the default PRF, while all those added
1535 // afterwards specify a particular hash.
David Benjaminef793f42015-11-05 18:16:27 -05001536 return TLS1_2_VERSION;
1537 }
1538 return SSL3_VERSION;
1539}
1540
Nick Harper1fd39d82016-06-14 18:14:35 -07001541uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001542 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1543 cipher->algorithm_auth == SSL_aGENERIC) {
Nick Harper1fd39d82016-06-14 18:14:35 -07001544 return TLS1_3_VERSION;
1545 }
1546 return TLS1_2_VERSION;
1547}
1548
David Benjaminc11ea9422017-08-29 16:33:21 -04001549// return the actual cipher being used
David Benjamin71f07942015-04-08 02:36:59 -04001550const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1551 if (cipher != NULL) {
1552 return cipher->name;
1553 }
1554
1555 return "(NONE)";
1556}
1557
David Benjamin6fff3862017-06-21 21:07:04 -04001558const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
1559 return cipher->standard_name;
1560}
1561
David Benjamin71f07942015-04-08 02:36:59 -04001562const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1563 if (cipher == NULL) {
1564 return "";
1565 }
1566
1567 switch (cipher->algorithm_mkey) {
1568 case SSL_kRSA:
1569 return "RSA";
1570
David Benjamin71f07942015-04-08 02:36:59 -04001571 case SSL_kECDHE:
1572 switch (cipher->algorithm_auth) {
1573 case SSL_aECDSA:
1574 return "ECDHE_ECDSA";
1575 case SSL_aRSA:
1576 return "ECDHE_RSA";
1577 case SSL_aPSK:
1578 return "ECDHE_PSK";
1579 default:
1580 assert(0);
1581 return "UNKNOWN";
1582 }
1583
1584 case SSL_kPSK:
1585 assert(cipher->algorithm_auth == SSL_aPSK);
1586 return "PSK";
1587
Steven Valdez803c77a2016-09-06 14:13:43 -04001588 case SSL_kGENERIC:
1589 assert(cipher->algorithm_auth == SSL_aGENERIC);
1590 return "GENERIC";
1591
David Benjamin71f07942015-04-08 02:36:59 -04001592 default:
1593 assert(0);
1594 return "UNKNOWN";
1595 }
1596}
1597
David Benjamin71f07942015-04-08 02:36:59 -04001598char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1599 if (cipher == NULL) {
1600 return NULL;
1601 }
1602
David Benjamin6fff3862017-06-21 21:07:04 -04001603 return OPENSSL_strdup(SSL_CIPHER_standard_name(cipher));
David Benjamin71f07942015-04-08 02:36:59 -04001604}
1605
1606int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1607 if (cipher == NULL) {
1608 return 0;
1609 }
1610
David Benjamin9f2e2772015-11-18 09:59:43 -05001611 int alg_bits, strength_bits;
1612 switch (cipher->algorithm_enc) {
1613 case SSL_AES128:
1614 case SSL_AES128GCM:
David Benjamin9f2e2772015-11-18 09:59:43 -05001615 alg_bits = 128;
1616 strength_bits = 128;
1617 break;
1618
1619 case SSL_AES256:
1620 case SSL_AES256GCM:
David Benjamin13414b32015-12-09 23:02:39 -05001621 case SSL_CHACHA20POLY1305:
David Benjamin9f2e2772015-11-18 09:59:43 -05001622 alg_bits = 256;
1623 strength_bits = 256;
1624 break;
1625
1626 case SSL_3DES:
1627 alg_bits = 168;
1628 strength_bits = 112;
1629 break;
1630
1631 case SSL_eNULL:
1632 alg_bits = 0;
1633 strength_bits = 0;
1634 break;
1635
1636 default:
1637 assert(0);
1638 alg_bits = 0;
1639 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001640 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001641
1642 if (out_alg_bits != NULL) {
1643 *out_alg_bits = alg_bits;
1644 }
1645 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001646}
1647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1649 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001651 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001652
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 alg_mkey = cipher->algorithm_mkey;
1654 alg_auth = cipher->algorithm_auth;
1655 alg_enc = cipher->algorithm_enc;
1656 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 switch (alg_mkey) {
1659 case SSL_kRSA:
1660 kx = "RSA";
1661 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001662
David Benjamin7061e282015-03-19 11:10:48 -04001663 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 kx = "ECDH";
1665 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 case SSL_kPSK:
1668 kx = "PSK";
1669 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001670
Steven Valdez803c77a2016-09-06 14:13:43 -04001671 case SSL_kGENERIC:
1672 kx = "GENERIC";
1673 break;
1674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 default:
1676 kx = "unknown";
1677 }
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 switch (alg_auth) {
1680 case SSL_aRSA:
1681 au = "RSA";
1682 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 case SSL_aECDSA:
1685 au = "ECDSA";
1686 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 case SSL_aPSK:
1689 au = "PSK";
1690 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001691
Steven Valdez803c77a2016-09-06 14:13:43 -04001692 case SSL_aGENERIC:
1693 au = "GENERIC";
1694 break;
1695
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 default:
1697 au = "unknown";
1698 break;
1699 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 switch (alg_enc) {
1702 case SSL_3DES:
1703 enc = "3DES(168)";
1704 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 case SSL_AES128:
1707 enc = "AES(128)";
1708 break;
1709
1710 case SSL_AES256:
1711 enc = "AES(256)";
1712 break;
1713
1714 case SSL_AES128GCM:
1715 enc = "AESGCM(128)";
1716 break;
1717
1718 case SSL_AES256GCM:
1719 enc = "AESGCM(256)";
1720 break;
1721
David Benjamin13414b32015-12-09 23:02:39 -05001722 case SSL_CHACHA20POLY1305:
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 enc = "ChaCha20-Poly1305";
1724 break;
1725
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001726 case SSL_eNULL:
1727 enc="None";
1728 break;
1729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 default:
1731 enc = "unknown";
1732 break;
1733 }
1734
1735 switch (alg_mac) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 case SSL_SHA1:
1737 mac = "SHA1";
1738 break;
1739
1740 case SSL_SHA256:
1741 mac = "SHA256";
1742 break;
1743
1744 case SSL_SHA384:
1745 mac = "SHA384";
1746 break;
1747
1748 case SSL_AEAD:
1749 mac = "AEAD";
1750 break;
1751
1752 default:
1753 mac = "unknown";
1754 break;
1755 }
1756
1757 if (buf == NULL) {
1758 len = 128;
David Benjamine64d2c72017-07-12 16:31:08 -04001759 buf = (char *)OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001760 if (buf == NULL) {
1761 return NULL;
1762 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 } else if (len < 128) {
1764 return "Buffer too small";
1765 }
1766
Brian Smith0687bdf2016-01-17 09:18:26 -10001767 BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1768 cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 return buf;
1770}
1771
David Benjamin71f07942015-04-08 02:36:59 -04001772const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1773 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001774}
1775
David Benjamind55bd792017-05-18 11:33:08 -04001776STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001777
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001778int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001779
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001780const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001781
Adam Langley3e9e0432016-10-03 15:58:07 -07001782void SSL_COMP_free_compression_methods(void) {}