blob: 551cc528d9d15abe78b9f178e884aac909052a89 [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,
David Benjaminbe8a8692015-08-21 13:51:56 -0400450 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400451 },
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,
David Benjaminbe8a8692015-08-21 13:51:56 -0400458 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400459 },
David Benjamina1c90a52015-05-30 17:03:14 -0400460};
461
462static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464#define CIPHER_ADD 1
465#define CIPHER_KILL 2
466#define CIPHER_DEL 3
467#define CIPHER_ORD 4
468#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470typedef struct cipher_order_st {
471 const SSL_CIPHER *cipher;
472 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 int in_group;
474 struct cipher_order_st *next, *prev;
475} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjamin0344daf2015-04-08 02:08:01 -0400477typedef struct cipher_alias_st {
478 /* name is the name of the cipher alias. */
479 const char *name;
480
481 /* The following fields are bitmasks for the corresponding fields on
482 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
483 * bit corresponding to the cipher's value is set to 1. If any bitmask is
484 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
485 uint32_t algorithm_mkey;
486 uint32_t algorithm_auth;
487 uint32_t algorithm_enc;
488 uint32_t algorithm_mac;
489 uint32_t algorithm_ssl;
490 uint32_t algo_strength;
491} CIPHER_ALIAS;
492
David Benjamina1c90a52015-05-30 17:03:14 -0400493static const CIPHER_ALIAS kCipherAliases[] = {
494 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700495
David Benjamina1c90a52015-05-30 17:03:14 -0400496 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700497
David Benjamina1c90a52015-05-30 17:03:14 -0400498 /* key exchange aliases
499 * (some of those using only a single bit here combine
500 * multiple key exchange algs according to the RFCs,
501 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
502 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamina1c90a52015-05-30 17:03:14 -0400504 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
505 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
506 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamina1c90a52015-05-30 17:03:14 -0400508 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
509 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
510 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamina1c90a52015-05-30 17:03:14 -0400512 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700513
David Benjamina1c90a52015-05-30 17:03:14 -0400514 /* server authentication aliases */
515 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
516 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
517 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
518 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamina1c90a52015-05-30 17:03:14 -0400520 /* aliases combining key exchange and server authentication */
521 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
522 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
523 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
524 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
525 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
526 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamina1c90a52015-05-30 17:03:14 -0400528 /* symmetric encryption aliases */
529 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
530 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
531 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
532 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
533 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
534 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
535 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamina1c90a52015-05-30 17:03:14 -0400537 /* MAC aliases */
538 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
539 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
540 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
541 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
542 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamina1c90a52015-05-30 17:03:14 -0400544 /* protocol version aliases */
545 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
546 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
547 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamina1c90a52015-05-30 17:03:14 -0400549 /* strength classes */
550 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
551 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
552 /* FIPS 140-2 approved ciphersuite */
553 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800554};
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamina1c90a52015-05-30 17:03:14 -0400556static const size_t kCipherAliasesLen =
557 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
558
559static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
560 const SSL_CIPHER *a = in_a;
561 const SSL_CIPHER *b = in_b;
562
563 if (a->id > b->id) {
564 return 1;
565 } else if (a->id < b->id) {
566 return -1;
567 } else {
568 return 0;
569 }
570}
571
572static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
573 return ssl_cipher_id_cmp(*a, *b);
574}
575
576const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
577 SSL_CIPHER c;
578
579 c.id = 0x03000000L | value;
580 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
581 ssl_cipher_id_cmp);
582}
David Benjamin0344daf2015-04-08 02:08:01 -0400583
David Benjaminea72bd02014-12-21 21:27:41 -0500584int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
585 size_t *out_mac_secret_len,
586 size_t *out_fixed_iv_len,
587 const SSL_CIPHER *cipher, uint16_t version) {
588 *out_aead = NULL;
589 *out_mac_secret_len = 0;
590 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700591
David Benjaminea72bd02014-12-21 21:27:41 -0500592 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500594 *out_aead = EVP_aead_aes_128_gcm();
595 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 return 1;
597
598 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500599 *out_aead = EVP_aead_aes_256_gcm();
600 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 return 1;
602
603 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500604 *out_aead = EVP_aead_chacha20_poly1305();
605 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 return 1;
607
608 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500609 switch (cipher->algorithm_mac) {
610 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500611 if (version == SSL3_VERSION) {
612 *out_aead = EVP_aead_rc4_md5_ssl3();
613 } else {
614 *out_aead = EVP_aead_rc4_md5_tls();
615 }
David Benjaminea72bd02014-12-21 21:27:41 -0500616 *out_mac_secret_len = MD5_DIGEST_LENGTH;
617 return 1;
618 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500619 if (version == SSL3_VERSION) {
620 *out_aead = EVP_aead_rc4_sha1_ssl3();
621 } else {
622 *out_aead = EVP_aead_rc4_sha1_tls();
623 }
David Benjaminea72bd02014-12-21 21:27:41 -0500624 *out_mac_secret_len = SHA_DIGEST_LENGTH;
625 return 1;
626 default:
627 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800629
David Benjaminea72bd02014-12-21 21:27:41 -0500630 case SSL_AES128:
631 switch (cipher->algorithm_mac) {
632 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500633 if (version == SSL3_VERSION) {
634 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
635 *out_fixed_iv_len = 16;
636 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500637 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
638 *out_fixed_iv_len = 16;
639 } else {
640 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
641 }
642 *out_mac_secret_len = SHA_DIGEST_LENGTH;
643 return 1;
644 case SSL_SHA256:
645 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
646 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
647 return 1;
648 default:
649 return 0;
650 }
651
652 case SSL_AES256:
653 switch (cipher->algorithm_mac) {
654 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500655 if (version == SSL3_VERSION) {
656 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
657 *out_fixed_iv_len = 16;
658 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500659 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
660 *out_fixed_iv_len = 16;
661 } else {
662 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
663 }
664 *out_mac_secret_len = SHA_DIGEST_LENGTH;
665 return 1;
666 case SSL_SHA256:
667 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
668 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
669 return 1;
670 case SSL_SHA384:
671 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
672 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
673 return 1;
674 default:
675 return 0;
676 }
677
678 case SSL_3DES:
679 switch (cipher->algorithm_mac) {
680 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500681 if (version == SSL3_VERSION) {
682 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
683 *out_fixed_iv_len = 8;
684 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500685 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
686 *out_fixed_iv_len = 8;
687 } else {
688 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
689 }
690 *out_mac_secret_len = SHA_DIGEST_LENGTH;
691 return 1;
692 default:
693 return 0;
694 }
695
696 default:
697 return 0;
698 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800699}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700700
David Benjaminb0883312015-08-06 09:54:13 -0400701const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
702 switch (algorithm_prf) {
703 case SSL_HANDSHAKE_MAC_DEFAULT:
704 return EVP_sha1();
705 case SSL_HANDSHAKE_MAC_SHA256:
706 return EVP_sha256();
707 case SSL_HANDSHAKE_MAC_SHA384:
708 return EVP_sha384();
709 default:
710 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800711 }
Adam Langley95c29f32014-06-20 12:00:00 -0700712}
713
714#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700716
David Benjamin0344daf2015-04-08 02:08:01 -0400717/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
718 * |buf_len| bytes at |buf|. */
719static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
720 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
721 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
722}
723
Adam Langley95c29f32014-06-20 12:00:00 -0700724static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 CIPHER_ORDER **tail) {
726 if (curr == *tail) {
727 return;
728 }
729 if (curr == *head) {
730 *head = curr->next;
731 }
732 if (curr->prev != NULL) {
733 curr->prev->next = curr->next;
734 }
735 if (curr->next != NULL) {
736 curr->next->prev = curr->prev;
737 }
738 (*tail)->next = curr;
739 curr->prev = *tail;
740 curr->next = NULL;
741 *tail = curr;
742}
Adam Langley95c29f32014-06-20 12:00:00 -0700743
744static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800745 CIPHER_ORDER **tail) {
746 if (curr == *head) {
747 return;
748 }
749 if (curr == *tail) {
750 *tail = curr->prev;
751 }
752 if (curr->next != NULL) {
753 curr->next->prev = curr->prev;
754 }
755 if (curr->prev != NULL) {
756 curr->prev->next = curr->next;
757 }
758 (*head)->prev = curr;
759 curr->next = *head;
760 curr->prev = NULL;
761 *head = curr;
762}
Adam Langley95c29f32014-06-20 12:00:00 -0700763
David Benjamin82c9e902014-12-12 15:55:27 -0500764static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800765 CIPHER_ORDER *co_list,
766 CIPHER_ORDER **head_p,
767 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400768 /* The set of ciphers is static, but some subset may be unsupported by
769 * |ssl_method|, so the list may be smaller. */
770 size_t co_list_num = 0;
771 size_t i;
772 for (i = 0; i < kCiphersLen; i++) {
773 const SSL_CIPHER *cipher = &kCiphers[i];
774 if (ssl_method->supports_cipher(cipher)) {
775 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 co_list[co_list_num].next = NULL;
777 co_list[co_list_num].prev = NULL;
778 co_list[co_list_num].active = 0;
779 co_list[co_list_num].in_group = 0;
780 co_list_num++;
781 }
782 }
Adam Langley95c29f32014-06-20 12:00:00 -0700783
Adam Langleyfcf25832014-12-18 17:42:32 -0800784 /* Prepare linked list from list entries. */
785 if (co_list_num > 0) {
786 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 if (co_list_num > 1) {
789 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langleyfcf25832014-12-18 17:42:32 -0800791 for (i = 1; i < co_list_num - 1; i++) {
792 co_list[i].prev = &co_list[i - 1];
793 co_list[i].next = &co_list[i + 1];
794 }
Adam Langley95c29f32014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
797 }
798
799 co_list[co_list_num - 1].next = NULL;
800
801 *head_p = &co_list[0];
802 *tail_p = &co_list[co_list_num - 1];
803 }
804}
Adam Langley95c29f32014-06-20 12:00:00 -0700805
David Benjamin0344daf2015-04-08 02:08:01 -0400806/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
807 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
808 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
809 *
810 * - If |cipher_id| is non-zero, only that cipher is selected.
811 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
812 * of that strength.
813 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
814 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800815static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400816 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
817 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
818 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
820 CIPHER_ORDER *head, *tail, *curr, *next, *last;
821 const SSL_CIPHER *cp;
822 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700823
David Benjamin0344daf2015-04-08 02:08:01 -0400824 if (cipher_id == 0 && strength_bits == -1 &&
825 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
826 alg_ssl == 0 || algo_strength == 0)) {
827 /* The rule matches nothing, so bail early. */
828 return;
829 }
830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 if (rule == CIPHER_DEL) {
832 /* needed to maintain sorting between currently deleted ciphers */
833 reverse = 1;
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 head = *head_p;
837 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (reverse) {
840 next = tail;
841 last = head;
842 } else {
843 next = head;
844 last = tail;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 curr = NULL;
848 for (;;) {
849 if (curr == last) {
850 break;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 curr = next;
854 if (curr == NULL) {
855 break;
856 }
Adam Langleye3142a72014-07-24 17:56:48 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 next = reverse ? curr->prev : curr->next;
859 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700860
David Benjamin0344daf2015-04-08 02:08:01 -0400861 /* Selection criteria is either a specific cipher, the value of
862 * |strength_bits|, or the algorithms used. */
863 if (cipher_id != 0) {
864 if (cipher_id != cp->id) {
865 continue;
866 }
867 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 if (strength_bits != cp->strength_bits) {
869 continue;
870 }
David Benjamin0344daf2015-04-08 02:08:01 -0400871 } else if (!(alg_mkey & cp->algorithm_mkey) ||
872 !(alg_auth & cp->algorithm_auth) ||
873 !(alg_enc & cp->algorithm_enc) ||
874 !(alg_mac & cp->algorithm_mac) ||
875 !(alg_ssl & cp->algorithm_ssl) ||
876 !(algo_strength & cp->algo_strength)) {
877 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 }
Adam Langleye3142a72014-07-24 17:56:48 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 /* add the cipher if it has not been added yet. */
881 if (rule == CIPHER_ADD) {
882 /* reverse == 0 */
883 if (!curr->active) {
884 ll_append_tail(&head, curr, &tail);
885 curr->active = 1;
886 curr->in_group = in_group;
887 }
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 /* Move the added cipher to this location */
891 else if (rule == CIPHER_ORD) {
892 /* reverse == 0 */
893 if (curr->active) {
894 ll_append_tail(&head, curr, &tail);
895 curr->in_group = 0;
896 }
897 } else if (rule == CIPHER_DEL) {
898 /* reverse == 1 */
899 if (curr->active) {
900 /* most recently deleted ciphersuites get best positions
901 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
902 * works in reverse to maintain the order) */
903 ll_append_head(&head, curr, &tail);
904 curr->active = 0;
905 curr->in_group = 0;
906 }
907 } else if (rule == CIPHER_KILL) {
908 /* reverse == 0 */
909 if (head == curr) {
910 head = curr->next;
911 } else {
912 curr->prev->next = curr->next;
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 if (tail == curr) {
916 tail = curr->prev;
917 }
918 curr->active = 0;
919 if (curr->next != NULL) {
920 curr->next->prev = curr->prev;
921 }
922 if (curr->prev != NULL) {
923 curr->prev->next = curr->next;
924 }
925 curr->next = NULL;
926 curr->prev = NULL;
927 }
928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 *head_p = head;
931 *tail_p = tail;
932}
Adam Langley95c29f32014-06-20 12:00:00 -0700933
934static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 CIPHER_ORDER **tail_p) {
936 int max_strength_bits, i, *number_uses;
937 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 /* This routine sorts the ciphers with descending strength. The sorting must
940 * keep the pre-sorted sequence, so we apply the normal sorting routine as
941 * '+' movement to the end of the list. */
942 max_strength_bits = 0;
943 curr = *head_p;
944 while (curr != NULL) {
945 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
946 max_strength_bits = curr->cipher->strength_bits;
947 }
948 curr = curr->next;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
952 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400953 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 return 0;
955 }
956 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 /* Now find the strength_bits values actually used. */
959 curr = *head_p;
960 while (curr != NULL) {
961 if (curr->active) {
962 number_uses[curr->cipher->strength_bits]++;
963 }
964 curr = curr->next;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 /* Go through the list of used strength_bits values in descending order. */
968 for (i = max_strength_bits; i >= 0; i--) {
969 if (number_uses[i] > 0) {
970 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
971 tail_p);
972 }
973 }
974
975 OPENSSL_free(number_uses);
976 return 1;
977}
Adam Langley95c29f32014-06-20 12:00:00 -0700978
David Benjamin0344daf2015-04-08 02:08:01 -0400979static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
980 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400982 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -0400983 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -0400985 int multi, rule, retval, ok, in_group = 0, has_group = 0;
986 size_t j, buf_len;
987 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 retval = 1;
991 l = rule_str;
992 for (;;) {
993 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 if (ch == '\0') {
996 break; /* done */
997 }
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 if (in_group) {
1000 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 if (*tail_p) {
1002 (*tail_p)->in_group = 0;
1003 }
1004 in_group = 0;
1005 l++;
1006 continue;
1007 }
David Benjamin37d92462014-09-20 17:54:24 -04001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (ch == '|') {
1010 rule = CIPHER_ADD;
1011 l++;
1012 continue;
1013 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1014 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001015 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001016 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 break;
1018 } else {
1019 rule = CIPHER_ADD;
1020 }
1021 } else if (ch == '-') {
1022 rule = CIPHER_DEL;
1023 l++;
1024 } else if (ch == '+') {
1025 rule = CIPHER_ORD;
1026 l++;
1027 } else if (ch == '!') {
1028 rule = CIPHER_KILL;
1029 l++;
1030 } else if (ch == '@') {
1031 rule = CIPHER_SPECIAL;
1032 l++;
1033 } else if (ch == '[') {
1034 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001035 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001036 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 break;
1038 }
1039 in_group = 1;
1040 has_group = 1;
1041 l++;
1042 continue;
1043 } else {
1044 rule = CIPHER_ADD;
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 /* If preference groups are enabled, the only legal operator is +.
1048 * Otherwise the in_group bits will get mixed up. */
1049 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001050 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001051 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 break;
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 if (ITEM_SEP(ch)) {
1056 l++;
1057 continue;
1058 }
Adam Langley95c29f32014-06-20 12:00:00 -07001059
David Benjamin0344daf2015-04-08 02:08:01 -04001060 multi = 0;
1061 cipher_id = 0;
1062 alg_mkey = ~0u;
1063 alg_auth = ~0u;
1064 alg_enc = ~0u;
1065 alg_mac = ~0u;
1066 alg_ssl = ~0u;
1067 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001068
Adam Langleyfcf25832014-12-18 17:42:32 -08001069 for (;;) {
1070 ch = *l;
1071 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001072 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1074 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1075 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001076 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078
David Benjamin0344daf2015-04-08 02:08:01 -04001079 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 /* We hit something we cannot deal with, it is no command or separator
1081 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001082 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001083 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 l++;
1085 break;
1086 }
Adam Langley95c29f32014-06-20 12:00:00 -07001087
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 break;
1090 }
David Benjamin0344daf2015-04-08 02:08:01 -04001091
1092 /* Look for a matching exact cipher. These aren't allowed in multipart
1093 * rules. */
1094 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001095 for (j = 0; j < kCiphersLen; j++) {
1096 const SSL_CIPHER *cipher = &kCiphers[j];
1097 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001098 cipher_id = cipher->id;
1099 break;
1100 }
1101 }
1102 }
1103 if (cipher_id == 0) {
1104 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001105 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001106 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1107 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1108 alg_auth &= kCipherAliases[j].algorithm_auth;
1109 alg_enc &= kCipherAliases[j].algorithm_enc;
1110 alg_mac &= kCipherAliases[j].algorithm_mac;
1111 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1112 algo_strength &= kCipherAliases[j].algo_strength;
1113 break;
1114 }
1115 }
David Benjamina1c90a52015-05-30 17:03:14 -04001116 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001117 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1118 }
1119 }
1120
1121 /* Check for a multipart rule. */
1122 if (ch != '+') {
1123 break;
1124 }
1125 l++;
1126 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 }
Adam Langley95c29f32014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 /* Ok, we have the rule, now apply it. */
1130 if (rule == CIPHER_SPECIAL) {
1131 /* special command */
1132 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001133 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 ok = ssl_cipher_strength_sort(head_p, tail_p);
1135 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001136 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 }
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 if (ok == 0) {
1140 retval = 0;
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 /* We do not support any "multi" options together with "@", so throw away
1144 * the rest of the command, if any left, until end or ':' is found. */
1145 while (*l != '\0' && !ITEM_SEP(*l)) {
1146 l++;
1147 }
David Benjamin0344daf2015-04-08 02:08:01 -04001148 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1150 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1151 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 }
1153 }
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001156 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 retval = 0;
1158 }
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 return retval;
1161}
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163STACK_OF(SSL_CIPHER) *
1164ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001165 struct ssl_cipher_preference_list_st **out_cipher_list,
1166 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1167 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001168 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1170 const char *rule_p;
1171 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 uint8_t *in_group_flags = NULL;
1173 unsigned int num_in_group_flags = 0;
1174 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001177 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 return NULL;
1179 }
David Benjamin5213df42014-08-20 14:19:54 -04001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 /* Now we have to collect the available ciphers from the compiled in ciphers.
1182 * We cannot get more than the number compiled in, so it is used for
1183 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001184 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001186 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 return NULL;
1188 }
Adam Langley95c29f32014-06-20 12:00:00 -07001189
David Benjamina1c90a52015-05-30 17:03:14 -04001190 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 /* Now arrange all ciphers by preference:
1193 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1196 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001197 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1198 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001199 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001201 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1202 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1205 * CHACHA20 unless there is hardware support for fast and constant-time
1206 * AES_GCM. */
1207 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001208 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001210 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1211 -1, 0, &head, &tail);
1212 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1213 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001215 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1216 CIPHER_ADD, -1, 0, &head, &tail);
1217 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001219 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1220 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 }
Adam Langley95c29f32014-06-20 12:00:00 -07001222
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1224 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001225 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1226 0, &head, &tail);
1227 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1228 0, &head, &tail);
1229 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1230 -1, 0, &head, &tail);
1231 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1232 0, &head, &tail);
1233 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001235
Adam Langleyfcf25832014-12-18 17:42:32 -08001236 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001237 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1238 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001241 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1242 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001243
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001245 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1246 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 /* If the rule_string begins with DEFAULT, apply the default rule before
1249 * using the (possibly available) additional rules. */
1250 ok = 1;
1251 rule_p = rule_str;
1252 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001253 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1254 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 rule_p += 7;
1256 if (*rule_p == ':') {
1257 rule_p++;
1258 }
1259 }
Adam Langley858a88d2014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001262 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 }
Adam Langley95c29f32014-06-20 12:00:00 -07001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 if (!ok) {
1266 goto err;
1267 }
1268
1269 /* Allocate new "cipherstack" for the result, return with error
1270 * if we cannot get one. */
1271 cipherstack = sk_SSL_CIPHER_new_null();
1272 if (cipherstack == NULL) {
1273 goto err;
1274 }
1275
David Benjamina1c90a52015-05-30 17:03:14 -04001276 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 if (!in_group_flags) {
1278 goto err;
1279 }
1280
1281 /* The cipher selection for the list is done. The ciphers are added
1282 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1283 for (curr = head; curr != NULL; curr = curr->next) {
1284 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001285 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1286 goto err;
1287 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 in_group_flags[num_in_group_flags++] = curr->in_group;
1289 }
1290 }
1291 OPENSSL_free(co_list); /* Not needed any longer */
1292 co_list = NULL;
1293
1294 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1295 if (tmp_cipher_list == NULL) {
1296 goto err;
1297 }
1298 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1299 if (!pref_list) {
1300 goto err;
1301 }
1302 pref_list->ciphers = cipherstack;
1303 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1304 if (!pref_list->in_group_flags) {
1305 goto err;
1306 }
1307 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1308 OPENSSL_free(in_group_flags);
1309 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001310 if (*out_cipher_list != NULL) {
1311 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 }
David Benjamin71f07942015-04-08 02:36:59 -04001313 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 pref_list = NULL;
1315
David Benjamin71f07942015-04-08 02:36:59 -04001316 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001317 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001318 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001320 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1321 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001322
David Benjamin71f07942015-04-08 02:36:59 -04001323 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 } else {
1325 sk_SSL_CIPHER_free(tmp_cipher_list);
1326 tmp_cipher_list = NULL;
1327 }
1328
1329 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001330
1331err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001332 OPENSSL_free(co_list);
1333 OPENSSL_free(in_group_flags);
1334 sk_SSL_CIPHER_free(cipherstack);
1335 sk_SSL_CIPHER_free(tmp_cipher_list);
1336 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 OPENSSL_free(pref_list->in_group_flags);
1338 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001339 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 return NULL;
1341}
Adam Langley95c29f32014-06-20 12:00:00 -07001342
David Benjamin71f07942015-04-08 02:36:59 -04001343uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1344
David Benjamina1c90a52015-05-30 17:03:14 -04001345uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1346 uint32_t id = cipher->id;
1347 /* All ciphers are SSLv3. */
1348 assert((id & 0xff000000) == 0x03000000);
1349 return id & 0xffff;
1350}
1351
David Benjamin71f07942015-04-08 02:36:59 -04001352int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1353 return (cipher->algorithm_enc & SSL_AES) != 0;
1354}
1355
1356int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1357 return (cipher->algorithm_mac & SSL_MD5) != 0;
1358}
1359
1360int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1361 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1362}
1363
1364int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1365 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1366}
1367
1368/* return the actual cipher being used */
1369const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1370 if (cipher != NULL) {
1371 return cipher->name;
1372 }
1373
1374 return "(NONE)";
1375}
1376
1377const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1378 if (cipher == NULL) {
1379 return "";
1380 }
1381
1382 switch (cipher->algorithm_mkey) {
1383 case SSL_kRSA:
1384 return "RSA";
1385
1386 case SSL_kDHE:
1387 switch (cipher->algorithm_auth) {
1388 case SSL_aRSA:
1389 return "DHE_RSA";
1390 default:
1391 assert(0);
1392 return "UNKNOWN";
1393 }
1394
1395 case SSL_kECDHE:
1396 switch (cipher->algorithm_auth) {
1397 case SSL_aECDSA:
1398 return "ECDHE_ECDSA";
1399 case SSL_aRSA:
1400 return "ECDHE_RSA";
1401 case SSL_aPSK:
1402 return "ECDHE_PSK";
1403 default:
1404 assert(0);
1405 return "UNKNOWN";
1406 }
1407
1408 case SSL_kPSK:
1409 assert(cipher->algorithm_auth == SSL_aPSK);
1410 return "PSK";
1411
1412 default:
1413 assert(0);
1414 return "UNKNOWN";
1415 }
1416}
1417
1418static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1419 switch (cipher->algorithm_enc) {
1420 case SSL_3DES:
1421 return "3DES_EDE_CBC";
1422 case SSL_RC4:
1423 return "RC4";
1424 case SSL_AES128:
1425 return "AES_128_CBC";
1426 case SSL_AES256:
1427 return "AES_256_CBC";
1428 case SSL_AES128GCM:
1429 return "AES_128_GCM";
1430 case SSL_AES256GCM:
1431 return "AES_256_GCM";
1432 case SSL_CHACHA20POLY1305:
1433 return "CHACHA20_POLY1305";
1434 break;
1435 default:
1436 assert(0);
1437 return "UNKNOWN";
1438 }
1439}
1440
1441static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001442 switch (cipher->algorithm_prf) {
1443 case SSL_HANDSHAKE_MAC_DEFAULT:
1444 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1445 * only ever MD5 or SHA-1. */
1446 switch (cipher->algorithm_mac) {
1447 case SSL_MD5:
1448 return "MD5";
1449 case SSL_SHA1:
1450 return "SHA";
1451 }
1452 break;
1453 case SSL_HANDSHAKE_MAC_SHA256:
1454 return "SHA256";
1455 case SSL_HANDSHAKE_MAC_SHA384:
1456 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001457 }
David Benjaminb0883312015-08-06 09:54:13 -04001458 assert(0);
1459 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001460}
1461
1462char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1463 if (cipher == NULL) {
1464 return NULL;
1465 }
1466
1467 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1468 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1469 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1470
1471 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1472 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1473 strlen(prf_name) + 1;
1474 char *ret = OPENSSL_malloc(len);
1475 if (ret == NULL) {
1476 return NULL;
1477 }
1478 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1479 BUF_strlcat(ret, kx_name, len) >= len ||
1480 BUF_strlcat(ret, "_WITH_", len) >= len ||
1481 BUF_strlcat(ret, enc_name, len) >= len ||
1482 BUF_strlcat(ret, "_", len) >= len ||
1483 BUF_strlcat(ret, prf_name, len) >= len) {
1484 assert(0);
1485 OPENSSL_free(ret);
1486 return NULL;
1487 }
1488 assert(strlen(ret) + 1 == len);
1489 return ret;
1490}
1491
1492int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1493 if (cipher == NULL) {
1494 return 0;
1495 }
1496
1497 if (out_alg_bits != NULL) {
1498 *out_alg_bits = cipher->alg_bits;
1499 }
1500 return cipher->strength_bits;
1501}
1502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1504 int len) {
1505 const char *ver;
1506 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001507 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001508 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 alg_mkey = cipher->algorithm_mkey;
1511 alg_auth = cipher->algorithm_auth;
1512 alg_enc = cipher->algorithm_enc;
1513 alg_mac = cipher->algorithm_mac;
1514 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 if (alg_ssl & SSL_SSLV3) {
1517 ver = "SSLv3";
1518 } else if (alg_ssl & SSL_TLSV1_2) {
1519 ver = "TLSv1.2";
1520 } else {
1521 ver = "unknown";
1522 }
Adam Langley95c29f32014-06-20 12:00:00 -07001523
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 switch (alg_mkey) {
1525 case SSL_kRSA:
1526 kx = "RSA";
1527 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001528
David Benjamin7061e282015-03-19 11:10:48 -04001529 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 kx = "DH";
1531 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001532
David Benjamin7061e282015-03-19 11:10:48 -04001533 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 kx = "ECDH";
1535 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 case SSL_kPSK:
1538 kx = "PSK";
1539 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 default:
1542 kx = "unknown";
1543 }
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 switch (alg_auth) {
1546 case SSL_aRSA:
1547 au = "RSA";
1548 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001549
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 case SSL_aECDSA:
1551 au = "ECDSA";
1552 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001553
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 case SSL_aPSK:
1555 au = "PSK";
1556 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 default:
1559 au = "unknown";
1560 break;
1561 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001562
Adam Langleyfcf25832014-12-18 17:42:32 -08001563 switch (alg_enc) {
1564 case SSL_3DES:
1565 enc = "3DES(168)";
1566 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001567
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 case SSL_RC4:
1569 enc = "RC4(128)";
1570 break;
1571
1572 case SSL_AES128:
1573 enc = "AES(128)";
1574 break;
1575
1576 case SSL_AES256:
1577 enc = "AES(256)";
1578 break;
1579
1580 case SSL_AES128GCM:
1581 enc = "AESGCM(128)";
1582 break;
1583
1584 case SSL_AES256GCM:
1585 enc = "AESGCM(256)";
1586 break;
1587
1588 case SSL_CHACHA20POLY1305:
1589 enc = "ChaCha20-Poly1305";
1590 break;
1591
1592 default:
1593 enc = "unknown";
1594 break;
1595 }
1596
1597 switch (alg_mac) {
1598 case SSL_MD5:
1599 mac = "MD5";
1600 break;
1601
1602 case SSL_SHA1:
1603 mac = "SHA1";
1604 break;
1605
1606 case SSL_SHA256:
1607 mac = "SHA256";
1608 break;
1609
1610 case SSL_SHA384:
1611 mac = "SHA384";
1612 break;
1613
1614 case SSL_AEAD:
1615 mac = "AEAD";
1616 break;
1617
1618 default:
1619 mac = "unknown";
1620 break;
1621 }
1622
1623 if (buf == NULL) {
1624 len = 128;
1625 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001626 if (buf == NULL) {
1627 return NULL;
1628 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 } else if (len < 128) {
1630 return "Buffer too small";
1631 }
1632
1633 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1634 return buf;
1635}
1636
David Benjamin71f07942015-04-08 02:36:59 -04001637const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1638 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001639}
1640
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001641COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001642
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001643int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001644
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001645const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001646
David Benjamind1d80782015-07-05 11:54:09 -04001647int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001648 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001651 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001653 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 }
Adam Langley95c29f32014-06-20 12:00:00 -07001655
David Benjamind1d80782015-07-05 11:54:09 -04001656 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001657}
David Benjamin9c651c92014-07-12 13:27:45 -04001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001660 /* PSK-authenticated ciphers do not use a public key, except for
1661 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 if ((cipher->algorithm_auth & SSL_aPSK) &&
1663 !(cipher->algorithm_mkey & SSL_kRSA)) {
1664 return 0;
1665 }
1666
1667 /* All other ciphers include it. */
1668 return 1;
1669}
1670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1672 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001673 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 return 1;
1675 }
1676
1677 /* It is optional in all others. */
1678 return 0;
1679}