blob: a715409257037d2d753dccc6a5d4267243ed0688 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin71f07942015-04-08 02:36:59 -0400145#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400146#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500147#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400150#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamina1c90a52015-05-30 17:03:14 -0400155/* kCiphers is an array of all supported ciphers, sorted by id. */
156const SSL_CIPHER kCiphers[] = {
157 /* The RSA ciphers */
158 /* Cipher 04 */
159 {
160 SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
161 SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
162 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
163 },
164
165 /* Cipher 05 */
166 {
167 SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
168 SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
169 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
170 },
171
172 /* Cipher 0A */
173 {
174 SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
175 SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
176 SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
177 },
178
179
180 /* New AES ciphersuites */
181
182 /* Cipher 2F */
183 {
184 TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
185 SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
186 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
187 },
188
189 /* Cipher 33 */
190 {
191 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
192 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
193 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
194 },
195
196 /* Cipher 35 */
197 {
198 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
199 SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
200 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
201 },
202
203 /* Cipher 39 */
204 {
205 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
206 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
207 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
208 },
209
210
211 /* TLS v1.2 ciphersuites */
212
213 /* Cipher 3C */
214 {
215 TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
216 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
217 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
218 },
219
220 /* Cipher 3D */
221 {
222 TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
223 SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
224 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
225 },
226
227 /* Cipher 67 */
228 {
229 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
230 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
231 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
232 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
233 },
234
235 /* Cipher 6B */
236 {
237 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
238 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
239 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
240 SSL_HANDSHAKE_MAC_SHA256, 256, 256,
241 },
242
Adam Langley85bc5602015-06-09 09:54:04 -0700243 /* PSK cipher suites. */
244
David Benjamina1c90a52015-05-30 17:03:14 -0400245 /* Cipher 8A */
246 {
247 TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
248 SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
249 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
250 },
251
252 /* Cipher 8C */
253 {
254 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
255 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
256 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
257 },
258
259 /* Cipher 8D */
260 {
261 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
262 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
263 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
264 },
265
David Benjamina1c90a52015-05-30 17:03:14 -0400266 /* GCM ciphersuites from RFC5288 */
267
268 /* Cipher 9C */
269 {
270 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
271 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
272 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400273 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400274 128, 128,
275 },
276
277 /* Cipher 9D */
278 {
279 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
280 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
281 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400282 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400283 256, 256,
284 },
285
286 /* Cipher 9E */
287 {
288 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
289 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
290 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400291 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400292 128, 128,
293 },
294
295 /* Cipher 9F */
296 {
297 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
298 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
299 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400300 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400301 256, 256,
302 },
303
304 /* Cipher C007 */
305 {
306 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
307 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
308 SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
309 128,
310 },
311
312 /* Cipher C009 */
313 {
314 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
315 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
316 SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
317 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
318 },
319
320 /* Cipher C00A */
321 {
322 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
323 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
324 SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
325 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
326 },
327
328 /* Cipher C011 */
329 {
330 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
331 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
332 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
333 },
334
335 /* Cipher C013 */
336 {
337 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
338 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
339 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
340 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
341 },
342
343 /* Cipher C014 */
344 {
345 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
346 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
347 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
348 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
349 },
350
351
352 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
353
354 /* Cipher C023 */
355 {
356 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
357 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
358 SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
359 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
360 },
361
362 /* Cipher C024 */
363 {
364 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
365 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
366 SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
367 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
368 },
369
370 /* Cipher C027 */
371 {
372 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
373 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
374 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
375 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
376 },
377
378 /* Cipher C028 */
379 {
380 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
381 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
382 SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
383 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
384 },
385
386
387 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
388
389 /* Cipher C02B */
390 {
391 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
392 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
393 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400394 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400395 128, 128,
396 },
397
398 /* Cipher C02C */
399 {
400 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
401 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
402 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400403 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400404 256, 256,
405 },
406
407 /* Cipher C02F */
408 {
409 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
410 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
411 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400412 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400413 128, 128,
414 },
415
416 /* Cipher C030 */
417 {
418 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
419 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
420 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400421 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400422 256, 256,
423 },
424
Adam Langley85bc5602015-06-09 09:54:04 -0700425 /* ECDHE-PSK cipher suites. */
426
427 /* Cipher C035 */
428 {
429 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
430 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
431 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
432 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
433 },
434
435 /* Cipher C036 */
436 {
437 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
438 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
439 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
440 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
441 },
442
443 /* ChaCha20-Poly1305 cipher suites. */
444
David Benjamina1c90a52015-05-30 17:03:14 -0400445 {
446 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
447 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
448 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
449 SSL_HANDSHAKE_MAC_SHA256,
450 256, 0,
451 },
452
453 {
454 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
455 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
456 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
457 SSL_HANDSHAKE_MAC_SHA256,
458 256, 0,
459 },
460
461 {
462 TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
463 TLS1_CK_DHE_RSA_CHACHA20_POLY1305, SSL_kDHE, SSL_aRSA,
464 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
465 SSL_HANDSHAKE_MAC_SHA256,
466 256, 0,
467 },
468};
469
470static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472#define CIPHER_ADD 1
473#define CIPHER_KILL 2
474#define CIPHER_DEL 3
475#define CIPHER_ORD 4
476#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478typedef struct cipher_order_st {
479 const SSL_CIPHER *cipher;
480 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 int in_group;
482 struct cipher_order_st *next, *prev;
483} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin0344daf2015-04-08 02:08:01 -0400485typedef struct cipher_alias_st {
486 /* name is the name of the cipher alias. */
487 const char *name;
488
489 /* The following fields are bitmasks for the corresponding fields on
490 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
491 * bit corresponding to the cipher's value is set to 1. If any bitmask is
492 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
493 uint32_t algorithm_mkey;
494 uint32_t algorithm_auth;
495 uint32_t algorithm_enc;
496 uint32_t algorithm_mac;
497 uint32_t algorithm_ssl;
498 uint32_t algo_strength;
499} CIPHER_ALIAS;
500
David Benjamina1c90a52015-05-30 17:03:14 -0400501static const CIPHER_ALIAS kCipherAliases[] = {
502 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamina1c90a52015-05-30 17:03:14 -0400504 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700505
David Benjamina1c90a52015-05-30 17:03:14 -0400506 /* key exchange aliases
507 * (some of those using only a single bit here combine
508 * multiple key exchange algs according to the RFCs,
509 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
510 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamina1c90a52015-05-30 17:03:14 -0400512 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
513 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
514 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamina1c90a52015-05-30 17:03:14 -0400516 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
517 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
518 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamina1c90a52015-05-30 17:03:14 -0400520 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamina1c90a52015-05-30 17:03:14 -0400522 /* server authentication aliases */
523 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
524 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
525 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
526 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamina1c90a52015-05-30 17:03:14 -0400528 /* aliases combining key exchange and server authentication */
529 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
530 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
531 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
532 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
533 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
534 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamina1c90a52015-05-30 17:03:14 -0400536 /* symmetric encryption aliases */
537 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
538 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
539 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
540 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
541 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
542 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
543 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamina1c90a52015-05-30 17:03:14 -0400545 /* MAC aliases */
546 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
547 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
548 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
549 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
550 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700551
David Benjamina1c90a52015-05-30 17:03:14 -0400552 /* protocol version aliases */
553 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
554 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
555 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamina1c90a52015-05-30 17:03:14 -0400557 /* strength classes */
558 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
559 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
560 /* FIPS 140-2 approved ciphersuite */
561 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800562};
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamina1c90a52015-05-30 17:03:14 -0400564static const size_t kCipherAliasesLen =
565 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
566
567static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
568 const SSL_CIPHER *a = in_a;
569 const SSL_CIPHER *b = in_b;
570
571 if (a->id > b->id) {
572 return 1;
573 } else if (a->id < b->id) {
574 return -1;
575 } else {
576 return 0;
577 }
578}
579
580static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
581 return ssl_cipher_id_cmp(*a, *b);
582}
583
584const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
585 SSL_CIPHER c;
586
587 c.id = 0x03000000L | value;
588 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
589 ssl_cipher_id_cmp);
590}
David Benjamin0344daf2015-04-08 02:08:01 -0400591
David Benjaminea72bd02014-12-21 21:27:41 -0500592int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
593 size_t *out_mac_secret_len,
594 size_t *out_fixed_iv_len,
595 const SSL_CIPHER *cipher, uint16_t version) {
596 *out_aead = NULL;
597 *out_mac_secret_len = 0;
598 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700599
David Benjaminea72bd02014-12-21 21:27:41 -0500600 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500602 *out_aead = EVP_aead_aes_128_gcm();
603 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 return 1;
605
606 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500607 *out_aead = EVP_aead_aes_256_gcm();
608 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 return 1;
610
611 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500612 *out_aead = EVP_aead_chacha20_poly1305();
613 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 return 1;
615
616 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500617 switch (cipher->algorithm_mac) {
618 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500619 if (version == SSL3_VERSION) {
620 *out_aead = EVP_aead_rc4_md5_ssl3();
621 } else {
622 *out_aead = EVP_aead_rc4_md5_tls();
623 }
David Benjaminea72bd02014-12-21 21:27:41 -0500624 *out_mac_secret_len = MD5_DIGEST_LENGTH;
625 return 1;
626 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500627 if (version == SSL3_VERSION) {
628 *out_aead = EVP_aead_rc4_sha1_ssl3();
629 } else {
630 *out_aead = EVP_aead_rc4_sha1_tls();
631 }
David Benjaminea72bd02014-12-21 21:27:41 -0500632 *out_mac_secret_len = SHA_DIGEST_LENGTH;
633 return 1;
634 default:
635 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800637
David Benjaminea72bd02014-12-21 21:27:41 -0500638 case SSL_AES128:
639 switch (cipher->algorithm_mac) {
640 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500641 if (version == SSL3_VERSION) {
642 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
643 *out_fixed_iv_len = 16;
644 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500645 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
646 *out_fixed_iv_len = 16;
647 } else {
648 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
649 }
650 *out_mac_secret_len = SHA_DIGEST_LENGTH;
651 return 1;
652 case SSL_SHA256:
653 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
654 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
655 return 1;
656 default:
657 return 0;
658 }
659
660 case SSL_AES256:
661 switch (cipher->algorithm_mac) {
662 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500663 if (version == SSL3_VERSION) {
664 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
665 *out_fixed_iv_len = 16;
666 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500667 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
668 *out_fixed_iv_len = 16;
669 } else {
670 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
671 }
672 *out_mac_secret_len = SHA_DIGEST_LENGTH;
673 return 1;
674 case SSL_SHA256:
675 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
676 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
677 return 1;
678 case SSL_SHA384:
679 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
680 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
681 return 1;
682 default:
683 return 0;
684 }
685
686 case SSL_3DES:
687 switch (cipher->algorithm_mac) {
688 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500689 if (version == SSL3_VERSION) {
690 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
691 *out_fixed_iv_len = 8;
692 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500693 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
694 *out_fixed_iv_len = 8;
695 } else {
696 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
697 }
698 *out_mac_secret_len = SHA_DIGEST_LENGTH;
699 return 1;
700 default:
701 return 0;
702 }
703
704 default:
705 return 0;
706 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800707}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700708
David Benjaminb0883312015-08-06 09:54:13 -0400709const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
710 switch (algorithm_prf) {
711 case SSL_HANDSHAKE_MAC_DEFAULT:
712 return EVP_sha1();
713 case SSL_HANDSHAKE_MAC_SHA256:
714 return EVP_sha256();
715 case SSL_HANDSHAKE_MAC_SHA384:
716 return EVP_sha384();
717 default:
718 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 }
Adam Langley95c29f32014-06-20 12:00:00 -0700720}
721
722#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700724
David Benjamin0344daf2015-04-08 02:08:01 -0400725/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
726 * |buf_len| bytes at |buf|. */
727static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
728 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
729 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
730}
731
Adam Langley95c29f32014-06-20 12:00:00 -0700732static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800733 CIPHER_ORDER **tail) {
734 if (curr == *tail) {
735 return;
736 }
737 if (curr == *head) {
738 *head = curr->next;
739 }
740 if (curr->prev != NULL) {
741 curr->prev->next = curr->next;
742 }
743 if (curr->next != NULL) {
744 curr->next->prev = curr->prev;
745 }
746 (*tail)->next = curr;
747 curr->prev = *tail;
748 curr->next = NULL;
749 *tail = curr;
750}
Adam Langley95c29f32014-06-20 12:00:00 -0700751
752static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 CIPHER_ORDER **tail) {
754 if (curr == *head) {
755 return;
756 }
757 if (curr == *tail) {
758 *tail = curr->prev;
759 }
760 if (curr->next != NULL) {
761 curr->next->prev = curr->prev;
762 }
763 if (curr->prev != NULL) {
764 curr->prev->next = curr->next;
765 }
766 (*head)->prev = curr;
767 curr->next = *head;
768 curr->prev = NULL;
769 *head = curr;
770}
Adam Langley95c29f32014-06-20 12:00:00 -0700771
David Benjamin82c9e902014-12-12 15:55:27 -0500772static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 CIPHER_ORDER *co_list,
774 CIPHER_ORDER **head_p,
775 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400776 /* The set of ciphers is static, but some subset may be unsupported by
777 * |ssl_method|, so the list may be smaller. */
778 size_t co_list_num = 0;
779 size_t i;
780 for (i = 0; i < kCiphersLen; i++) {
781 const SSL_CIPHER *cipher = &kCiphers[i];
782 if (ssl_method->supports_cipher(cipher)) {
783 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800784 co_list[co_list_num].next = NULL;
785 co_list[co_list_num].prev = NULL;
786 co_list[co_list_num].active = 0;
787 co_list[co_list_num].in_group = 0;
788 co_list_num++;
789 }
790 }
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 /* Prepare linked list from list entries. */
793 if (co_list_num > 0) {
794 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 if (co_list_num > 1) {
797 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 for (i = 1; i < co_list_num - 1; i++) {
800 co_list[i].prev = &co_list[i - 1];
801 co_list[i].next = &co_list[i + 1];
802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
805 }
806
807 co_list[co_list_num - 1].next = NULL;
808
809 *head_p = &co_list[0];
810 *tail_p = &co_list[co_list_num - 1];
811 }
812}
Adam Langley95c29f32014-06-20 12:00:00 -0700813
David Benjamin0344daf2015-04-08 02:08:01 -0400814/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
815 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
816 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
817 *
818 * - If |cipher_id| is non-zero, only that cipher is selected.
819 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
820 * of that strength.
821 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
822 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800823static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400824 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
825 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
826 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
828 CIPHER_ORDER *head, *tail, *curr, *next, *last;
829 const SSL_CIPHER *cp;
830 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700831
David Benjamin0344daf2015-04-08 02:08:01 -0400832 if (cipher_id == 0 && strength_bits == -1 &&
833 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
834 alg_ssl == 0 || algo_strength == 0)) {
835 /* The rule matches nothing, so bail early. */
836 return;
837 }
838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (rule == CIPHER_DEL) {
840 /* needed to maintain sorting between currently deleted ciphers */
841 reverse = 1;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 head = *head_p;
845 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 if (reverse) {
848 next = tail;
849 last = head;
850 } else {
851 next = head;
852 last = tail;
853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 curr = NULL;
856 for (;;) {
857 if (curr == last) {
858 break;
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 curr = next;
862 if (curr == NULL) {
863 break;
864 }
Adam Langleye3142a72014-07-24 17:56:48 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 next = reverse ? curr->prev : curr->next;
867 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700868
David Benjamin0344daf2015-04-08 02:08:01 -0400869 /* Selection criteria is either a specific cipher, the value of
870 * |strength_bits|, or the algorithms used. */
871 if (cipher_id != 0) {
872 if (cipher_id != cp->id) {
873 continue;
874 }
875 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 if (strength_bits != cp->strength_bits) {
877 continue;
878 }
David Benjamin0344daf2015-04-08 02:08:01 -0400879 } else if (!(alg_mkey & cp->algorithm_mkey) ||
880 !(alg_auth & cp->algorithm_auth) ||
881 !(alg_enc & cp->algorithm_enc) ||
882 !(alg_mac & cp->algorithm_mac) ||
883 !(alg_ssl & cp->algorithm_ssl) ||
884 !(algo_strength & cp->algo_strength)) {
885 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 }
Adam Langleye3142a72014-07-24 17:56:48 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 /* add the cipher if it has not been added yet. */
889 if (rule == CIPHER_ADD) {
890 /* reverse == 0 */
891 if (!curr->active) {
892 ll_append_tail(&head, curr, &tail);
893 curr->active = 1;
894 curr->in_group = in_group;
895 }
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 /* Move the added cipher to this location */
899 else if (rule == CIPHER_ORD) {
900 /* reverse == 0 */
901 if (curr->active) {
902 ll_append_tail(&head, curr, &tail);
903 curr->in_group = 0;
904 }
905 } else if (rule == CIPHER_DEL) {
906 /* reverse == 1 */
907 if (curr->active) {
908 /* most recently deleted ciphersuites get best positions
909 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
910 * works in reverse to maintain the order) */
911 ll_append_head(&head, curr, &tail);
912 curr->active = 0;
913 curr->in_group = 0;
914 }
915 } else if (rule == CIPHER_KILL) {
916 /* reverse == 0 */
917 if (head == curr) {
918 head = curr->next;
919 } else {
920 curr->prev->next = curr->next;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 if (tail == curr) {
924 tail = curr->prev;
925 }
926 curr->active = 0;
927 if (curr->next != NULL) {
928 curr->next->prev = curr->prev;
929 }
930 if (curr->prev != NULL) {
931 curr->prev->next = curr->next;
932 }
933 curr->next = NULL;
934 curr->prev = NULL;
935 }
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 *head_p = head;
939 *tail_p = tail;
940}
Adam Langley95c29f32014-06-20 12:00:00 -0700941
942static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 CIPHER_ORDER **tail_p) {
944 int max_strength_bits, i, *number_uses;
945 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 /* This routine sorts the ciphers with descending strength. The sorting must
948 * keep the pre-sorted sequence, so we apply the normal sorting routine as
949 * '+' movement to the end of the list. */
950 max_strength_bits = 0;
951 curr = *head_p;
952 while (curr != NULL) {
953 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
954 max_strength_bits = curr->cipher->strength_bits;
955 }
956 curr = curr->next;
957 }
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
960 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400961 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 return 0;
963 }
964 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 /* Now find the strength_bits values actually used. */
967 curr = *head_p;
968 while (curr != NULL) {
969 if (curr->active) {
970 number_uses[curr->cipher->strength_bits]++;
971 }
972 curr = curr->next;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 /* Go through the list of used strength_bits values in descending order. */
976 for (i = max_strength_bits; i >= 0; i--) {
977 if (number_uses[i] > 0) {
978 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
979 tail_p);
980 }
981 }
982
983 OPENSSL_free(number_uses);
984 return 1;
985}
Adam Langley95c29f32014-06-20 12:00:00 -0700986
David Benjamin0344daf2015-04-08 02:08:01 -0400987static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
988 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400990 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -0400991 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -0400993 int multi, rule, retval, ok, in_group = 0, has_group = 0;
994 size_t j, buf_len;
995 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 retval = 1;
999 l = rule_str;
1000 for (;;) {
1001 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 if (ch == '\0') {
1004 break; /* done */
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 if (in_group) {
1008 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (*tail_p) {
1010 (*tail_p)->in_group = 0;
1011 }
1012 in_group = 0;
1013 l++;
1014 continue;
1015 }
David Benjamin37d92462014-09-20 17:54:24 -04001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 if (ch == '|') {
1018 rule = CIPHER_ADD;
1019 l++;
1020 continue;
1021 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1022 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001023 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001024 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 break;
1026 } else {
1027 rule = CIPHER_ADD;
1028 }
1029 } else if (ch == '-') {
1030 rule = CIPHER_DEL;
1031 l++;
1032 } else if (ch == '+') {
1033 rule = CIPHER_ORD;
1034 l++;
1035 } else if (ch == '!') {
1036 rule = CIPHER_KILL;
1037 l++;
1038 } else if (ch == '@') {
1039 rule = CIPHER_SPECIAL;
1040 l++;
1041 } else if (ch == '[') {
1042 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001043 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001044 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 break;
1046 }
1047 in_group = 1;
1048 has_group = 1;
1049 l++;
1050 continue;
1051 } else {
1052 rule = CIPHER_ADD;
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 /* If preference groups are enabled, the only legal operator is +.
1056 * Otherwise the in_group bits will get mixed up. */
1057 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001058 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001059 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 break;
1061 }
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 if (ITEM_SEP(ch)) {
1064 l++;
1065 continue;
1066 }
Adam Langley95c29f32014-06-20 12:00:00 -07001067
David Benjamin0344daf2015-04-08 02:08:01 -04001068 multi = 0;
1069 cipher_id = 0;
1070 alg_mkey = ~0u;
1071 alg_auth = ~0u;
1072 alg_enc = ~0u;
1073 alg_mac = ~0u;
1074 alg_ssl = ~0u;
1075 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001076
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 for (;;) {
1078 ch = *l;
1079 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001080 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1082 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1083 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001084 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 }
Adam Langley95c29f32014-06-20 12:00:00 -07001086
David Benjamin0344daf2015-04-08 02:08:01 -04001087 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 /* We hit something we cannot deal with, it is no command or separator
1089 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001090 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001091 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 l++;
1093 break;
1094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 break;
1098 }
David Benjamin0344daf2015-04-08 02:08:01 -04001099
1100 /* Look for a matching exact cipher. These aren't allowed in multipart
1101 * rules. */
1102 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001103 for (j = 0; j < kCiphersLen; j++) {
1104 const SSL_CIPHER *cipher = &kCiphers[j];
1105 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001106 cipher_id = cipher->id;
1107 break;
1108 }
1109 }
1110 }
1111 if (cipher_id == 0) {
1112 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001113 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001114 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1115 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1116 alg_auth &= kCipherAliases[j].algorithm_auth;
1117 alg_enc &= kCipherAliases[j].algorithm_enc;
1118 alg_mac &= kCipherAliases[j].algorithm_mac;
1119 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1120 algo_strength &= kCipherAliases[j].algo_strength;
1121 break;
1122 }
1123 }
David Benjamina1c90a52015-05-30 17:03:14 -04001124 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001125 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1126 }
1127 }
1128
1129 /* Check for a multipart rule. */
1130 if (ch != '+') {
1131 break;
1132 }
1133 l++;
1134 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 }
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 /* Ok, we have the rule, now apply it. */
1138 if (rule == CIPHER_SPECIAL) {
1139 /* special command */
1140 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001141 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 ok = ssl_cipher_strength_sort(head_p, tail_p);
1143 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001144 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 }
Adam Langley95c29f32014-06-20 12:00:00 -07001146
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (ok == 0) {
1148 retval = 0;
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 /* We do not support any "multi" options together with "@", so throw away
1152 * the rest of the command, if any left, until end or ':' is found. */
1153 while (*l != '\0' && !ITEM_SEP(*l)) {
1154 l++;
1155 }
David Benjamin0344daf2015-04-08 02:08:01 -04001156 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1158 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1159 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 }
1161 }
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001164 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 retval = 0;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 return retval;
1169}
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171STACK_OF(SSL_CIPHER) *
1172ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001173 struct ssl_cipher_preference_list_st **out_cipher_list,
1174 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1175 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001176 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1178 const char *rule_p;
1179 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 uint8_t *in_group_flags = NULL;
1181 unsigned int num_in_group_flags = 0;
1182 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001185 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 return NULL;
1187 }
David Benjamin5213df42014-08-20 14:19:54 -04001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 /* Now we have to collect the available ciphers from the compiled in ciphers.
1190 * We cannot get more than the number compiled in, so it is used for
1191 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001192 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001194 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 return NULL;
1196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197
David Benjamina1c90a52015-05-30 17:03:14 -04001198 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 /* Now arrange all ciphers by preference:
1201 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1204 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001205 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1206 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001207 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001209 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1210 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1213 * CHACHA20 unless there is hardware support for fast and constant-time
1214 * AES_GCM. */
1215 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001216 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001218 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1219 -1, 0, &head, &tail);
1220 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1221 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001223 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1224 CIPHER_ADD, -1, 0, &head, &tail);
1225 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001227 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1228 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 }
Adam Langley95c29f32014-06-20 12:00:00 -07001230
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1232 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001233 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1234 0, &head, &tail);
1235 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1236 0, &head, &tail);
1237 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1238 -1, 0, &head, &tail);
1239 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1240 0, &head, &tail);
1241 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001243
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001245 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1246 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001249 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1250 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001253 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1254 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 /* If the rule_string begins with DEFAULT, apply the default rule before
1257 * using the (possibly available) additional rules. */
1258 ok = 1;
1259 rule_p = rule_str;
1260 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001261 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1262 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 rule_p += 7;
1264 if (*rule_p == ':') {
1265 rule_p++;
1266 }
1267 }
Adam Langley858a88d2014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001270 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 }
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 if (!ok) {
1274 goto err;
1275 }
1276
1277 /* Allocate new "cipherstack" for the result, return with error
1278 * if we cannot get one. */
1279 cipherstack = sk_SSL_CIPHER_new_null();
1280 if (cipherstack == NULL) {
1281 goto err;
1282 }
1283
David Benjamina1c90a52015-05-30 17:03:14 -04001284 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 if (!in_group_flags) {
1286 goto err;
1287 }
1288
1289 /* The cipher selection for the list is done. The ciphers are added
1290 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1291 for (curr = head; curr != NULL; curr = curr->next) {
1292 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001293 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1294 goto err;
1295 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 in_group_flags[num_in_group_flags++] = curr->in_group;
1297 }
1298 }
1299 OPENSSL_free(co_list); /* Not needed any longer */
1300 co_list = NULL;
1301
1302 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1303 if (tmp_cipher_list == NULL) {
1304 goto err;
1305 }
1306 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1307 if (!pref_list) {
1308 goto err;
1309 }
1310 pref_list->ciphers = cipherstack;
1311 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1312 if (!pref_list->in_group_flags) {
1313 goto err;
1314 }
1315 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1316 OPENSSL_free(in_group_flags);
1317 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001318 if (*out_cipher_list != NULL) {
1319 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 }
David Benjamin71f07942015-04-08 02:36:59 -04001321 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 pref_list = NULL;
1323
David Benjamin71f07942015-04-08 02:36:59 -04001324 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001325 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001326 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001328 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1329 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001330
David Benjamin71f07942015-04-08 02:36:59 -04001331 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 } else {
1333 sk_SSL_CIPHER_free(tmp_cipher_list);
1334 tmp_cipher_list = NULL;
1335 }
1336
1337 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001338
1339err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001340 OPENSSL_free(co_list);
1341 OPENSSL_free(in_group_flags);
1342 sk_SSL_CIPHER_free(cipherstack);
1343 sk_SSL_CIPHER_free(tmp_cipher_list);
1344 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 OPENSSL_free(pref_list->in_group_flags);
1346 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001347 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 return NULL;
1349}
Adam Langley95c29f32014-06-20 12:00:00 -07001350
David Benjamin71f07942015-04-08 02:36:59 -04001351uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1352
David Benjamina1c90a52015-05-30 17:03:14 -04001353uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1354 uint32_t id = cipher->id;
1355 /* All ciphers are SSLv3. */
1356 assert((id & 0xff000000) == 0x03000000);
1357 return id & 0xffff;
1358}
1359
David Benjamin71f07942015-04-08 02:36:59 -04001360int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1361 return (cipher->algorithm_enc & SSL_AES) != 0;
1362}
1363
1364int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1365 return (cipher->algorithm_mac & SSL_MD5) != 0;
1366}
1367
1368int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1369 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1370}
1371
1372int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1373 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1374}
1375
1376/* return the actual cipher being used */
1377const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1378 if (cipher != NULL) {
1379 return cipher->name;
1380 }
1381
1382 return "(NONE)";
1383}
1384
1385const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1386 if (cipher == NULL) {
1387 return "";
1388 }
1389
1390 switch (cipher->algorithm_mkey) {
1391 case SSL_kRSA:
1392 return "RSA";
1393
1394 case SSL_kDHE:
1395 switch (cipher->algorithm_auth) {
1396 case SSL_aRSA:
1397 return "DHE_RSA";
1398 default:
1399 assert(0);
1400 return "UNKNOWN";
1401 }
1402
1403 case SSL_kECDHE:
1404 switch (cipher->algorithm_auth) {
1405 case SSL_aECDSA:
1406 return "ECDHE_ECDSA";
1407 case SSL_aRSA:
1408 return "ECDHE_RSA";
1409 case SSL_aPSK:
1410 return "ECDHE_PSK";
1411 default:
1412 assert(0);
1413 return "UNKNOWN";
1414 }
1415
1416 case SSL_kPSK:
1417 assert(cipher->algorithm_auth == SSL_aPSK);
1418 return "PSK";
1419
1420 default:
1421 assert(0);
1422 return "UNKNOWN";
1423 }
1424}
1425
1426static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1427 switch (cipher->algorithm_enc) {
1428 case SSL_3DES:
1429 return "3DES_EDE_CBC";
1430 case SSL_RC4:
1431 return "RC4";
1432 case SSL_AES128:
1433 return "AES_128_CBC";
1434 case SSL_AES256:
1435 return "AES_256_CBC";
1436 case SSL_AES128GCM:
1437 return "AES_128_GCM";
1438 case SSL_AES256GCM:
1439 return "AES_256_GCM";
1440 case SSL_CHACHA20POLY1305:
1441 return "CHACHA20_POLY1305";
1442 break;
1443 default:
1444 assert(0);
1445 return "UNKNOWN";
1446 }
1447}
1448
1449static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001450 switch (cipher->algorithm_prf) {
1451 case SSL_HANDSHAKE_MAC_DEFAULT:
1452 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1453 * only ever MD5 or SHA-1. */
1454 switch (cipher->algorithm_mac) {
1455 case SSL_MD5:
1456 return "MD5";
1457 case SSL_SHA1:
1458 return "SHA";
1459 }
1460 break;
1461 case SSL_HANDSHAKE_MAC_SHA256:
1462 return "SHA256";
1463 case SSL_HANDSHAKE_MAC_SHA384:
1464 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001465 }
David Benjaminb0883312015-08-06 09:54:13 -04001466 assert(0);
1467 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001468}
1469
1470char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1471 if (cipher == NULL) {
1472 return NULL;
1473 }
1474
1475 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1476 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1477 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1478
1479 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1480 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1481 strlen(prf_name) + 1;
1482 char *ret = OPENSSL_malloc(len);
1483 if (ret == NULL) {
1484 return NULL;
1485 }
1486 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1487 BUF_strlcat(ret, kx_name, len) >= len ||
1488 BUF_strlcat(ret, "_WITH_", len) >= len ||
1489 BUF_strlcat(ret, enc_name, len) >= len ||
1490 BUF_strlcat(ret, "_", len) >= len ||
1491 BUF_strlcat(ret, prf_name, len) >= len) {
1492 assert(0);
1493 OPENSSL_free(ret);
1494 return NULL;
1495 }
1496 assert(strlen(ret) + 1 == len);
1497 return ret;
1498}
1499
1500int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1501 if (cipher == NULL) {
1502 return 0;
1503 }
1504
1505 if (out_alg_bits != NULL) {
1506 *out_alg_bits = cipher->alg_bits;
1507 }
1508 return cipher->strength_bits;
1509}
1510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1512 int len) {
1513 const char *ver;
1514 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001515 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001517
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 alg_mkey = cipher->algorithm_mkey;
1519 alg_auth = cipher->algorithm_auth;
1520 alg_enc = cipher->algorithm_enc;
1521 alg_mac = cipher->algorithm_mac;
1522 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 if (alg_ssl & SSL_SSLV3) {
1525 ver = "SSLv3";
1526 } else if (alg_ssl & SSL_TLSV1_2) {
1527 ver = "TLSv1.2";
1528 } else {
1529 ver = "unknown";
1530 }
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 switch (alg_mkey) {
1533 case SSL_kRSA:
1534 kx = "RSA";
1535 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001536
David Benjamin7061e282015-03-19 11:10:48 -04001537 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 kx = "DH";
1539 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001540
David Benjamin7061e282015-03-19 11:10:48 -04001541 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 kx = "ECDH";
1543 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 case SSL_kPSK:
1546 kx = "PSK";
1547 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001548
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 default:
1550 kx = "unknown";
1551 }
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 switch (alg_auth) {
1554 case SSL_aRSA:
1555 au = "RSA";
1556 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 case SSL_aECDSA:
1559 au = "ECDSA";
1560 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 case SSL_aPSK:
1563 au = "PSK";
1564 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 default:
1567 au = "unknown";
1568 break;
1569 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 switch (alg_enc) {
1572 case SSL_3DES:
1573 enc = "3DES(168)";
1574 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 case SSL_RC4:
1577 enc = "RC4(128)";
1578 break;
1579
1580 case SSL_AES128:
1581 enc = "AES(128)";
1582 break;
1583
1584 case SSL_AES256:
1585 enc = "AES(256)";
1586 break;
1587
1588 case SSL_AES128GCM:
1589 enc = "AESGCM(128)";
1590 break;
1591
1592 case SSL_AES256GCM:
1593 enc = "AESGCM(256)";
1594 break;
1595
1596 case SSL_CHACHA20POLY1305:
1597 enc = "ChaCha20-Poly1305";
1598 break;
1599
1600 default:
1601 enc = "unknown";
1602 break;
1603 }
1604
1605 switch (alg_mac) {
1606 case SSL_MD5:
1607 mac = "MD5";
1608 break;
1609
1610 case SSL_SHA1:
1611 mac = "SHA1";
1612 break;
1613
1614 case SSL_SHA256:
1615 mac = "SHA256";
1616 break;
1617
1618 case SSL_SHA384:
1619 mac = "SHA384";
1620 break;
1621
1622 case SSL_AEAD:
1623 mac = "AEAD";
1624 break;
1625
1626 default:
1627 mac = "unknown";
1628 break;
1629 }
1630
1631 if (buf == NULL) {
1632 len = 128;
1633 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001634 if (buf == NULL) {
1635 return NULL;
1636 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 } else if (len < 128) {
1638 return "Buffer too small";
1639 }
1640
1641 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1642 return buf;
1643}
1644
David Benjamin71f07942015-04-08 02:36:59 -04001645const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1646 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001647}
1648
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001649COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001650
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001651int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001652
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001653const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001654
David Benjamind1d80782015-07-05 11:54:09 -04001655int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001656 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001659 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001661 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 }
Adam Langley95c29f32014-06-20 12:00:00 -07001663
David Benjamind1d80782015-07-05 11:54:09 -04001664 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001665}
David Benjamin9c651c92014-07-12 13:27:45 -04001666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001668 /* PSK-authenticated ciphers do not use a public key, except for
1669 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 if ((cipher->algorithm_auth & SSL_aPSK) &&
1671 !(cipher->algorithm_mkey & SSL_kRSA)) {
1672 return 0;
1673 }
1674
1675 /* All other ciphers include it. */
1676 return 1;
1677}
1678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1680 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001681 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 return 1;
1683 }
1684
1685 /* It is optional in all others. */
1686 return 0;
1687}