blob: 04a579911aec5c6033a70f1962ef981785bff629 [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,
273 SSL_HANDSHAKE_MAC_SHA256 |
274 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
275 128, 128,
276 },
277
278 /* Cipher 9D */
279 {
280 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
281 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
282 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
283 SSL_HANDSHAKE_MAC_SHA384 |
284 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
285 256, 256,
286 },
287
288 /* Cipher 9E */
289 {
290 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
291 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
292 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
293 SSL_HANDSHAKE_MAC_SHA256 |
294 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
295 128, 128,
296 },
297
298 /* Cipher 9F */
299 {
300 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
301 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
302 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
303 SSL_HANDSHAKE_MAC_SHA384 |
304 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
305 256, 256,
306 },
307
308 /* Cipher C007 */
309 {
310 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
311 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
312 SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
313 128,
314 },
315
316 /* Cipher C009 */
317 {
318 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
319 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
320 SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
321 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
322 },
323
324 /* Cipher C00A */
325 {
326 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
327 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
328 SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
329 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
330 },
331
332 /* Cipher C011 */
333 {
334 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
335 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
336 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
337 },
338
339 /* Cipher C013 */
340 {
341 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
342 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
343 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
344 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
345 },
346
347 /* Cipher C014 */
348 {
349 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
350 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
351 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
352 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
353 },
354
355
356 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
357
358 /* Cipher C023 */
359 {
360 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
361 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
362 SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
363 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
364 },
365
366 /* Cipher C024 */
367 {
368 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
369 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
370 SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
371 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
372 },
373
374 /* Cipher C027 */
375 {
376 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
377 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
378 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
379 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
380 },
381
382 /* Cipher C028 */
383 {
384 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
385 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
386 SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
387 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
388 },
389
390
391 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
392
393 /* Cipher C02B */
394 {
395 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
396 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
397 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
398 SSL_HANDSHAKE_MAC_SHA256 |
399 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
400 128, 128,
401 },
402
403 /* Cipher C02C */
404 {
405 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
406 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
407 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
408 SSL_HANDSHAKE_MAC_SHA384 |
409 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
410 256, 256,
411 },
412
413 /* Cipher C02F */
414 {
415 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
416 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
417 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
418 SSL_HANDSHAKE_MAC_SHA256 |
419 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
420 128, 128,
421 },
422
423 /* Cipher C030 */
424 {
425 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
426 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
427 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
428 SSL_HANDSHAKE_MAC_SHA384 |
429 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
430 256, 256,
431 },
432
Adam Langley85bc5602015-06-09 09:54:04 -0700433 /* ECDHE-PSK cipher suites. */
434
435 /* Cipher C035 */
436 {
437 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
438 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
439 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
440 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
441 },
442
443 /* Cipher C036 */
444 {
445 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
446 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
447 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
448 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
449 },
450
451 /* ChaCha20-Poly1305 cipher suites. */
452
David Benjamina1c90a52015-05-30 17:03:14 -0400453 {
454 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
455 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
456 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
457 SSL_HANDSHAKE_MAC_SHA256,
458 256, 0,
459 },
460
461 {
462 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
463 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
464 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
465 SSL_HANDSHAKE_MAC_SHA256,
466 256, 0,
467 },
468
469 {
470 TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
471 TLS1_CK_DHE_RSA_CHACHA20_POLY1305, SSL_kDHE, SSL_aRSA,
472 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
473 SSL_HANDSHAKE_MAC_SHA256,
474 256, 0,
475 },
476};
477
478static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400481 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400483};
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
486 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
487 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
488 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
489 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
490};
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492#define CIPHER_ADD 1
493#define CIPHER_KILL 2
494#define CIPHER_DEL 3
495#define CIPHER_ORD 4
496#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498typedef struct cipher_order_st {
499 const SSL_CIPHER *cipher;
500 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 int in_group;
502 struct cipher_order_st *next, *prev;
503} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin0344daf2015-04-08 02:08:01 -0400505typedef struct cipher_alias_st {
506 /* name is the name of the cipher alias. */
507 const char *name;
508
509 /* The following fields are bitmasks for the corresponding fields on
510 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
511 * bit corresponding to the cipher's value is set to 1. If any bitmask is
512 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
513 uint32_t algorithm_mkey;
514 uint32_t algorithm_auth;
515 uint32_t algorithm_enc;
516 uint32_t algorithm_mac;
517 uint32_t algorithm_ssl;
518 uint32_t algo_strength;
519} CIPHER_ALIAS;
520
David Benjamina1c90a52015-05-30 17:03:14 -0400521static const CIPHER_ALIAS kCipherAliases[] = {
522 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamina1c90a52015-05-30 17:03:14 -0400524 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700525
David Benjamina1c90a52015-05-30 17:03:14 -0400526 /* key exchange aliases
527 * (some of those using only a single bit here combine
528 * multiple key exchange algs according to the RFCs,
529 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
530 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamina1c90a52015-05-30 17:03:14 -0400532 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
533 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
534 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamina1c90a52015-05-30 17:03:14 -0400536 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
537 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
538 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamina1c90a52015-05-30 17:03:14 -0400540 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamina1c90a52015-05-30 17:03:14 -0400542 /* server authentication aliases */
543 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
544 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
545 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
546 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamina1c90a52015-05-30 17:03:14 -0400548 /* aliases combining key exchange and server authentication */
549 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
550 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
551 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
552 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
553 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
554 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamina1c90a52015-05-30 17:03:14 -0400556 /* symmetric encryption aliases */
557 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
558 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
559 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
560 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
561 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
562 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
563 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjamina1c90a52015-05-30 17:03:14 -0400565 /* MAC aliases */
566 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
567 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
568 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
569 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
570 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamina1c90a52015-05-30 17:03:14 -0400572 /* protocol version aliases */
573 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
574 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
575 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamina1c90a52015-05-30 17:03:14 -0400577 /* strength classes */
578 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
579 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
580 /* FIPS 140-2 approved ciphersuite */
581 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800582};
Adam Langley95c29f32014-06-20 12:00:00 -0700583
David Benjamina1c90a52015-05-30 17:03:14 -0400584static const size_t kCipherAliasesLen =
585 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
586
587static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
588 const SSL_CIPHER *a = in_a;
589 const SSL_CIPHER *b = in_b;
590
591 if (a->id > b->id) {
592 return 1;
593 } else if (a->id < b->id) {
594 return -1;
595 } else {
596 return 0;
597 }
598}
599
600static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
601 return ssl_cipher_id_cmp(*a, *b);
602}
603
604const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
605 SSL_CIPHER c;
606
607 c.id = 0x03000000L | value;
608 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
609 ssl_cipher_id_cmp);
610}
David Benjamin0344daf2015-04-08 02:08:01 -0400611
David Benjaminea72bd02014-12-21 21:27:41 -0500612int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
613 size_t *out_mac_secret_len,
614 size_t *out_fixed_iv_len,
615 const SSL_CIPHER *cipher, uint16_t version) {
616 *out_aead = NULL;
617 *out_mac_secret_len = 0;
618 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700619
David Benjaminea72bd02014-12-21 21:27:41 -0500620 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500622 *out_aead = EVP_aead_aes_128_gcm();
623 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 return 1;
625
626 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500627 *out_aead = EVP_aead_aes_256_gcm();
628 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 return 1;
630
631 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500632 *out_aead = EVP_aead_chacha20_poly1305();
633 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 return 1;
635
636 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500637 switch (cipher->algorithm_mac) {
638 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500639 if (version == SSL3_VERSION) {
640 *out_aead = EVP_aead_rc4_md5_ssl3();
641 } else {
642 *out_aead = EVP_aead_rc4_md5_tls();
643 }
David Benjaminea72bd02014-12-21 21:27:41 -0500644 *out_mac_secret_len = MD5_DIGEST_LENGTH;
645 return 1;
646 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500647 if (version == SSL3_VERSION) {
648 *out_aead = EVP_aead_rc4_sha1_ssl3();
649 } else {
650 *out_aead = EVP_aead_rc4_sha1_tls();
651 }
David Benjaminea72bd02014-12-21 21:27:41 -0500652 *out_mac_secret_len = SHA_DIGEST_LENGTH;
653 return 1;
654 default:
655 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800657
David Benjaminea72bd02014-12-21 21:27:41 -0500658 case SSL_AES128:
659 switch (cipher->algorithm_mac) {
660 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500661 if (version == SSL3_VERSION) {
662 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
663 *out_fixed_iv_len = 16;
664 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500665 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
666 *out_fixed_iv_len = 16;
667 } else {
668 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
669 }
670 *out_mac_secret_len = SHA_DIGEST_LENGTH;
671 return 1;
672 case SSL_SHA256:
673 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
674 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
675 return 1;
676 default:
677 return 0;
678 }
679
680 case SSL_AES256:
681 switch (cipher->algorithm_mac) {
682 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500683 if (version == SSL3_VERSION) {
684 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
685 *out_fixed_iv_len = 16;
686 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500687 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
688 *out_fixed_iv_len = 16;
689 } else {
690 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
691 }
692 *out_mac_secret_len = SHA_DIGEST_LENGTH;
693 return 1;
694 case SSL_SHA256:
695 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
696 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
697 return 1;
698 case SSL_SHA384:
699 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
700 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
701 return 1;
702 default:
703 return 0;
704 }
705
706 case SSL_3DES:
707 switch (cipher->algorithm_mac) {
708 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500709 if (version == SSL3_VERSION) {
710 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
711 *out_fixed_iv_len = 8;
712 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500713 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
714 *out_fixed_iv_len = 8;
715 } else {
716 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
717 }
718 *out_mac_secret_len = SHA_DIGEST_LENGTH;
719 return 1;
720 default:
721 return 0;
722 }
723
724 default:
725 return 0;
726 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800727}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700728
David Benjamin71f07942015-04-08 02:36:59 -0400729int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
730 size_t idx) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500731 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 return 0;
733 }
David Benjamin71f07942015-04-08 02:36:59 -0400734 *out_mask = ssl_handshake_digests[idx].mask;
735 *out_md = ssl_handshake_digests[idx].md_func();
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700737}
738
739#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700741
David Benjamin0344daf2015-04-08 02:08:01 -0400742/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
743 * |buf_len| bytes at |buf|. */
744static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
745 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
746 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
747}
748
Adam Langley95c29f32014-06-20 12:00:00 -0700749static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 CIPHER_ORDER **tail) {
751 if (curr == *tail) {
752 return;
753 }
754 if (curr == *head) {
755 *head = curr->next;
756 }
757 if (curr->prev != NULL) {
758 curr->prev->next = curr->next;
759 }
760 if (curr->next != NULL) {
761 curr->next->prev = curr->prev;
762 }
763 (*tail)->next = curr;
764 curr->prev = *tail;
765 curr->next = NULL;
766 *tail = curr;
767}
Adam Langley95c29f32014-06-20 12:00:00 -0700768
769static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 CIPHER_ORDER **tail) {
771 if (curr == *head) {
772 return;
773 }
774 if (curr == *tail) {
775 *tail = curr->prev;
776 }
777 if (curr->next != NULL) {
778 curr->next->prev = curr->prev;
779 }
780 if (curr->prev != NULL) {
781 curr->prev->next = curr->next;
782 }
783 (*head)->prev = curr;
784 curr->next = *head;
785 curr->prev = NULL;
786 *head = curr;
787}
Adam Langley95c29f32014-06-20 12:00:00 -0700788
David Benjamin82c9e902014-12-12 15:55:27 -0500789static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 CIPHER_ORDER *co_list,
791 CIPHER_ORDER **head_p,
792 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400793 /* The set of ciphers is static, but some subset may be unsupported by
794 * |ssl_method|, so the list may be smaller. */
795 size_t co_list_num = 0;
796 size_t i;
797 for (i = 0; i < kCiphersLen; i++) {
798 const SSL_CIPHER *cipher = &kCiphers[i];
799 if (ssl_method->supports_cipher(cipher)) {
800 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 co_list[co_list_num].next = NULL;
802 co_list[co_list_num].prev = NULL;
803 co_list[co_list_num].active = 0;
804 co_list[co_list_num].in_group = 0;
805 co_list_num++;
806 }
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 /* Prepare linked list from list entries. */
810 if (co_list_num > 0) {
811 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 if (co_list_num > 1) {
814 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 for (i = 1; i < co_list_num - 1; i++) {
817 co_list[i].prev = &co_list[i - 1];
818 co_list[i].next = &co_list[i + 1];
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
822 }
823
824 co_list[co_list_num - 1].next = NULL;
825
826 *head_p = &co_list[0];
827 *tail_p = &co_list[co_list_num - 1];
828 }
829}
Adam Langley95c29f32014-06-20 12:00:00 -0700830
David Benjamin0344daf2015-04-08 02:08:01 -0400831/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
832 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
833 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
834 *
835 * - If |cipher_id| is non-zero, only that cipher is selected.
836 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
837 * of that strength.
838 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
839 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800840static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400841 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
842 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
843 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
845 CIPHER_ORDER *head, *tail, *curr, *next, *last;
846 const SSL_CIPHER *cp;
847 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700848
David Benjamin0344daf2015-04-08 02:08:01 -0400849 if (cipher_id == 0 && strength_bits == -1 &&
850 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
851 alg_ssl == 0 || algo_strength == 0)) {
852 /* The rule matches nothing, so bail early. */
853 return;
854 }
855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (rule == CIPHER_DEL) {
857 /* needed to maintain sorting between currently deleted ciphers */
858 reverse = 1;
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 head = *head_p;
862 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 if (reverse) {
865 next = tail;
866 last = head;
867 } else {
868 next = head;
869 last = tail;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 curr = NULL;
873 for (;;) {
874 if (curr == last) {
875 break;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 curr = next;
879 if (curr == NULL) {
880 break;
881 }
Adam Langleye3142a72014-07-24 17:56:48 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 next = reverse ? curr->prev : curr->next;
884 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700885
David Benjamin0344daf2015-04-08 02:08:01 -0400886 /* Selection criteria is either a specific cipher, the value of
887 * |strength_bits|, or the algorithms used. */
888 if (cipher_id != 0) {
889 if (cipher_id != cp->id) {
890 continue;
891 }
892 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 if (strength_bits != cp->strength_bits) {
894 continue;
895 }
David Benjamin0344daf2015-04-08 02:08:01 -0400896 } else if (!(alg_mkey & cp->algorithm_mkey) ||
897 !(alg_auth & cp->algorithm_auth) ||
898 !(alg_enc & cp->algorithm_enc) ||
899 !(alg_mac & cp->algorithm_mac) ||
900 !(alg_ssl & cp->algorithm_ssl) ||
901 !(algo_strength & cp->algo_strength)) {
902 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 }
Adam Langleye3142a72014-07-24 17:56:48 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* add the cipher if it has not been added yet. */
906 if (rule == CIPHER_ADD) {
907 /* reverse == 0 */
908 if (!curr->active) {
909 ll_append_tail(&head, curr, &tail);
910 curr->active = 1;
911 curr->in_group = in_group;
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 /* Move the added cipher to this location */
916 else if (rule == CIPHER_ORD) {
917 /* reverse == 0 */
918 if (curr->active) {
919 ll_append_tail(&head, curr, &tail);
920 curr->in_group = 0;
921 }
922 } else if (rule == CIPHER_DEL) {
923 /* reverse == 1 */
924 if (curr->active) {
925 /* most recently deleted ciphersuites get best positions
926 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
927 * works in reverse to maintain the order) */
928 ll_append_head(&head, curr, &tail);
929 curr->active = 0;
930 curr->in_group = 0;
931 }
932 } else if (rule == CIPHER_KILL) {
933 /* reverse == 0 */
934 if (head == curr) {
935 head = curr->next;
936 } else {
937 curr->prev->next = curr->next;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 if (tail == curr) {
941 tail = curr->prev;
942 }
943 curr->active = 0;
944 if (curr->next != NULL) {
945 curr->next->prev = curr->prev;
946 }
947 if (curr->prev != NULL) {
948 curr->prev->next = curr->next;
949 }
950 curr->next = NULL;
951 curr->prev = NULL;
952 }
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 *head_p = head;
956 *tail_p = tail;
957}
Adam Langley95c29f32014-06-20 12:00:00 -0700958
959static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 CIPHER_ORDER **tail_p) {
961 int max_strength_bits, i, *number_uses;
962 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 /* This routine sorts the ciphers with descending strength. The sorting must
965 * keep the pre-sorted sequence, so we apply the normal sorting routine as
966 * '+' movement to the end of the list. */
967 max_strength_bits = 0;
968 curr = *head_p;
969 while (curr != NULL) {
970 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
971 max_strength_bits = curr->cipher->strength_bits;
972 }
973 curr = curr->next;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
977 if (!number_uses) {
978 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
979 return 0;
980 }
981 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 /* Now find the strength_bits values actually used. */
984 curr = *head_p;
985 while (curr != NULL) {
986 if (curr->active) {
987 number_uses[curr->cipher->strength_bits]++;
988 }
989 curr = curr->next;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 /* Go through the list of used strength_bits values in descending order. */
993 for (i = max_strength_bits; i >= 0; i--) {
994 if (number_uses[i] > 0) {
995 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
996 tail_p);
997 }
998 }
999
1000 OPENSSL_free(number_uses);
1001 return 1;
1002}
Adam Langley95c29f32014-06-20 12:00:00 -07001003
David Benjamin0344daf2015-04-08 02:08:01 -04001004static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1005 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001007 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -04001008 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -04001010 int multi, rule, retval, ok, in_group = 0, has_group = 0;
1011 size_t j, buf_len;
1012 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 retval = 1;
1016 l = rule_str;
1017 for (;;) {
1018 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 if (ch == '\0') {
1021 break; /* done */
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 if (in_group) {
1025 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (*tail_p) {
1027 (*tail_p)->in_group = 0;
1028 }
1029 in_group = 0;
1030 l++;
1031 continue;
1032 }
David Benjamin37d92462014-09-20 17:54:24 -04001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 if (ch == '|') {
1035 rule = CIPHER_ADD;
1036 l++;
1037 continue;
1038 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1039 !(ch >= '0' && ch <= '9')) {
1040 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
1041 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001042 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 break;
1044 } else {
1045 rule = CIPHER_ADD;
1046 }
1047 } else if (ch == '-') {
1048 rule = CIPHER_DEL;
1049 l++;
1050 } else if (ch == '+') {
1051 rule = CIPHER_ORD;
1052 l++;
1053 } else if (ch == '!') {
1054 rule = CIPHER_KILL;
1055 l++;
1056 } else if (ch == '@') {
1057 rule = CIPHER_SPECIAL;
1058 l++;
1059 } else if (ch == '[') {
1060 if (in_group) {
1061 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001062 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 break;
1064 }
1065 in_group = 1;
1066 has_group = 1;
1067 l++;
1068 continue;
1069 } else {
1070 rule = CIPHER_ADD;
1071 }
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 /* If preference groups are enabled, the only legal operator is +.
1074 * Otherwise the in_group bits will get mixed up. */
1075 if (has_group && rule != CIPHER_ADD) {
1076 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
1077 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001078 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 break;
1080 }
Adam Langley95c29f32014-06-20 12:00:00 -07001081
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 if (ITEM_SEP(ch)) {
1083 l++;
1084 continue;
1085 }
Adam Langley95c29f32014-06-20 12:00:00 -07001086
David Benjamin0344daf2015-04-08 02:08:01 -04001087 multi = 0;
1088 cipher_id = 0;
1089 alg_mkey = ~0u;
1090 alg_auth = ~0u;
1091 alg_enc = ~0u;
1092 alg_mac = ~0u;
1093 alg_ssl = ~0u;
1094 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 for (;;) {
1097 ch = *l;
1098 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001099 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1101 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1102 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001103 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 }
Adam Langley95c29f32014-06-20 12:00:00 -07001105
David Benjamin0344daf2015-04-08 02:08:01 -04001106 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 /* We hit something we cannot deal with, it is no command or separator
1108 * nor alphanumeric, so we call this an error. */
1109 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
1110 SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001111 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 l++;
1113 break;
1114 }
Adam Langley95c29f32014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 break;
1118 }
David Benjamin0344daf2015-04-08 02:08:01 -04001119
1120 /* Look for a matching exact cipher. These aren't allowed in multipart
1121 * rules. */
1122 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001123 for (j = 0; j < kCiphersLen; j++) {
1124 const SSL_CIPHER *cipher = &kCiphers[j];
1125 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001126 cipher_id = cipher->id;
1127 break;
1128 }
1129 }
1130 }
1131 if (cipher_id == 0) {
1132 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001133 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001134 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1135 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1136 alg_auth &= kCipherAliases[j].algorithm_auth;
1137 alg_enc &= kCipherAliases[j].algorithm_enc;
1138 alg_mac &= kCipherAliases[j].algorithm_mac;
1139 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1140 algo_strength &= kCipherAliases[j].algo_strength;
1141 break;
1142 }
1143 }
David Benjamina1c90a52015-05-30 17:03:14 -04001144 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001145 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1146 }
1147 }
1148
1149 /* Check for a multipart rule. */
1150 if (ch != '+') {
1151 break;
1152 }
1153 l++;
1154 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 /* Ok, we have the rule, now apply it. */
1158 if (rule == CIPHER_SPECIAL) {
1159 /* special command */
1160 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001161 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 ok = ssl_cipher_strength_sort(head_p, tail_p);
1163 } else {
1164 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
1165 SSL_R_INVALID_COMMAND);
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 if (ok == 0) {
1169 retval = 0;
1170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 /* We do not support any "multi" options together with "@", so throw away
1173 * the rest of the command, if any left, until end or ':' is found. */
1174 while (*l != '\0' && !ITEM_SEP(*l)) {
1175 l++;
1176 }
David Benjamin0344daf2015-04-08 02:08:01 -04001177 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1179 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1180 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 }
1182 }
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 if (in_group) {
1185 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1186 retval = 0;
1187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 return retval;
1190}
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192STACK_OF(SSL_CIPHER) *
1193ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001194 struct ssl_cipher_preference_list_st **out_cipher_list,
1195 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1196 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001197 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1199 const char *rule_p;
1200 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 uint8_t *in_group_flags = NULL;
1202 unsigned int num_in_group_flags = 0;
1203 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001206 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 return NULL;
1208 }
David Benjamin5213df42014-08-20 14:19:54 -04001209
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 /* Now we have to collect the available ciphers from the compiled in ciphers.
1211 * We cannot get more than the number compiled in, so it is used for
1212 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001213 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 if (co_list == NULL) {
1215 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1216 return NULL;
1217 }
Adam Langley95c29f32014-06-20 12:00:00 -07001218
David Benjamina1c90a52015-05-30 17:03:14 -04001219 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 /* Now arrange all ciphers by preference:
1222 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1225 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001226 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1227 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001228 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001230 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1231 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1234 * CHACHA20 unless there is hardware support for fast and constant-time
1235 * AES_GCM. */
1236 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001237 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001239 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1240 -1, 0, &head, &tail);
1241 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1242 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001244 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1245 CIPHER_ADD, -1, 0, &head, &tail);
1246 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001248 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1249 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 }
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1253 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001254 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1255 0, &head, &tail);
1256 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1257 0, &head, &tail);
1258 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1259 -1, 0, &head, &tail);
1260 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1261 0, &head, &tail);
1262 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001266 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1267 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001270 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1271 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001274 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1275 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001276
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 /* If the rule_string begins with DEFAULT, apply the default rule before
1278 * using the (possibly available) additional rules. */
1279 ok = 1;
1280 rule_p = rule_str;
1281 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001282 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1283 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 rule_p += 7;
1285 if (*rule_p == ':') {
1286 rule_p++;
1287 }
1288 }
Adam Langley858a88d2014-06-20 12:00:00 -07001289
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001291 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 }
Adam Langley95c29f32014-06-20 12:00:00 -07001293
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 if (!ok) {
1295 goto err;
1296 }
1297
1298 /* Allocate new "cipherstack" for the result, return with error
1299 * if we cannot get one. */
1300 cipherstack = sk_SSL_CIPHER_new_null();
1301 if (cipherstack == NULL) {
1302 goto err;
1303 }
1304
David Benjamina1c90a52015-05-30 17:03:14 -04001305 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 if (!in_group_flags) {
1307 goto err;
1308 }
1309
1310 /* The cipher selection for the list is done. The ciphers are added
1311 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1312 for (curr = head; curr != NULL; curr = curr->next) {
1313 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001314 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1315 goto err;
1316 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001317 in_group_flags[num_in_group_flags++] = curr->in_group;
1318 }
1319 }
1320 OPENSSL_free(co_list); /* Not needed any longer */
1321 co_list = NULL;
1322
1323 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1324 if (tmp_cipher_list == NULL) {
1325 goto err;
1326 }
1327 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1328 if (!pref_list) {
1329 goto err;
1330 }
1331 pref_list->ciphers = cipherstack;
1332 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1333 if (!pref_list->in_group_flags) {
1334 goto err;
1335 }
1336 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1337 OPENSSL_free(in_group_flags);
1338 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001339 if (*out_cipher_list != NULL) {
1340 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 }
David Benjamin71f07942015-04-08 02:36:59 -04001342 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001343 pref_list = NULL;
1344
David Benjamin71f07942015-04-08 02:36:59 -04001345 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001346 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001347 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001349 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1350 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001351
David Benjamin71f07942015-04-08 02:36:59 -04001352 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 } else {
1354 sk_SSL_CIPHER_free(tmp_cipher_list);
1355 tmp_cipher_list = NULL;
1356 }
1357
1358 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001359
1360err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001361 OPENSSL_free(co_list);
1362 OPENSSL_free(in_group_flags);
1363 sk_SSL_CIPHER_free(cipherstack);
1364 sk_SSL_CIPHER_free(tmp_cipher_list);
1365 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 OPENSSL_free(pref_list->in_group_flags);
1367 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001368 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 return NULL;
1370}
Adam Langley95c29f32014-06-20 12:00:00 -07001371
David Benjamin71f07942015-04-08 02:36:59 -04001372uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1373
David Benjamina1c90a52015-05-30 17:03:14 -04001374uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1375 uint32_t id = cipher->id;
1376 /* All ciphers are SSLv3. */
1377 assert((id & 0xff000000) == 0x03000000);
1378 return id & 0xffff;
1379}
1380
David Benjamin71f07942015-04-08 02:36:59 -04001381int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1382 return (cipher->algorithm_enc & SSL_AES) != 0;
1383}
1384
1385int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1386 return (cipher->algorithm_mac & SSL_MD5) != 0;
1387}
1388
1389int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1390 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1391}
1392
1393int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1394 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1395}
1396
1397/* return the actual cipher being used */
1398const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1399 if (cipher != NULL) {
1400 return cipher->name;
1401 }
1402
1403 return "(NONE)";
1404}
1405
1406const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1407 if (cipher == NULL) {
1408 return "";
1409 }
1410
1411 switch (cipher->algorithm_mkey) {
1412 case SSL_kRSA:
1413 return "RSA";
1414
1415 case SSL_kDHE:
1416 switch (cipher->algorithm_auth) {
1417 case SSL_aRSA:
1418 return "DHE_RSA";
1419 default:
1420 assert(0);
1421 return "UNKNOWN";
1422 }
1423
1424 case SSL_kECDHE:
1425 switch (cipher->algorithm_auth) {
1426 case SSL_aECDSA:
1427 return "ECDHE_ECDSA";
1428 case SSL_aRSA:
1429 return "ECDHE_RSA";
1430 case SSL_aPSK:
1431 return "ECDHE_PSK";
1432 default:
1433 assert(0);
1434 return "UNKNOWN";
1435 }
1436
1437 case SSL_kPSK:
1438 assert(cipher->algorithm_auth == SSL_aPSK);
1439 return "PSK";
1440
1441 default:
1442 assert(0);
1443 return "UNKNOWN";
1444 }
1445}
1446
1447static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1448 switch (cipher->algorithm_enc) {
1449 case SSL_3DES:
1450 return "3DES_EDE_CBC";
1451 case SSL_RC4:
1452 return "RC4";
1453 case SSL_AES128:
1454 return "AES_128_CBC";
1455 case SSL_AES256:
1456 return "AES_256_CBC";
1457 case SSL_AES128GCM:
1458 return "AES_128_GCM";
1459 case SSL_AES256GCM:
1460 return "AES_256_GCM";
1461 case SSL_CHACHA20POLY1305:
1462 return "CHACHA20_POLY1305";
1463 break;
1464 default:
1465 assert(0);
1466 return "UNKNOWN";
1467 }
1468}
1469
1470static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjamin9a980ab2015-05-27 00:36:11 -04001471 if ((cipher->algorithm2 & SSL_HANDSHAKE_MAC_DEFAULT) ==
1472 SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjamin71f07942015-04-08 02:36:59 -04001473 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1474 * only ever MD5 or SHA-1. */
1475 switch (cipher->algorithm_mac) {
1476 case SSL_MD5:
1477 return "MD5";
1478 case SSL_SHA1:
1479 return "SHA";
1480 default:
1481 assert(0);
1482 return "UNKNOWN";
1483 }
David Benjamin9a980ab2015-05-27 00:36:11 -04001484 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA256) {
David Benjamin71f07942015-04-08 02:36:59 -04001485 return "SHA256";
David Benjamin9a980ab2015-05-27 00:36:11 -04001486 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA384) {
David Benjamin71f07942015-04-08 02:36:59 -04001487 return "SHA384";
1488 } else {
1489 assert(0);
1490 return "UNKNOWN";
1491 }
1492}
1493
1494char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1495 if (cipher == NULL) {
1496 return NULL;
1497 }
1498
1499 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1500 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1501 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1502
1503 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1504 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1505 strlen(prf_name) + 1;
1506 char *ret = OPENSSL_malloc(len);
1507 if (ret == NULL) {
1508 return NULL;
1509 }
1510 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1511 BUF_strlcat(ret, kx_name, len) >= len ||
1512 BUF_strlcat(ret, "_WITH_", len) >= len ||
1513 BUF_strlcat(ret, enc_name, len) >= len ||
1514 BUF_strlcat(ret, "_", len) >= len ||
1515 BUF_strlcat(ret, prf_name, len) >= len) {
1516 assert(0);
1517 OPENSSL_free(ret);
1518 return NULL;
1519 }
1520 assert(strlen(ret) + 1 == len);
1521 return ret;
1522}
1523
1524int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1525 if (cipher == NULL) {
1526 return 0;
1527 }
1528
1529 if (out_alg_bits != NULL) {
1530 *out_alg_bits = cipher->alg_bits;
1531 }
1532 return cipher->strength_bits;
1533}
1534
Adam Langleyfcf25832014-12-18 17:42:32 -08001535const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1536 int len) {
1537 const char *ver;
1538 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001539 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 alg_mkey = cipher->algorithm_mkey;
1543 alg_auth = cipher->algorithm_auth;
1544 alg_enc = cipher->algorithm_enc;
1545 alg_mac = cipher->algorithm_mac;
1546 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001547
Adam Langleyfcf25832014-12-18 17:42:32 -08001548 if (alg_ssl & SSL_SSLV3) {
1549 ver = "SSLv3";
1550 } else if (alg_ssl & SSL_TLSV1_2) {
1551 ver = "TLSv1.2";
1552 } else {
1553 ver = "unknown";
1554 }
Adam Langley95c29f32014-06-20 12:00:00 -07001555
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 switch (alg_mkey) {
1557 case SSL_kRSA:
1558 kx = "RSA";
1559 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
David Benjamin7061e282015-03-19 11:10:48 -04001561 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 kx = "DH";
1563 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001564
David Benjamin7061e282015-03-19 11:10:48 -04001565 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 kx = "ECDH";
1567 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 case SSL_kPSK:
1570 kx = "PSK";
1571 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 default:
1574 kx = "unknown";
1575 }
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 switch (alg_auth) {
1578 case SSL_aRSA:
1579 au = "RSA";
1580 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 case SSL_aECDSA:
1583 au = "ECDSA";
1584 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 case SSL_aPSK:
1587 au = "PSK";
1588 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 default:
1591 au = "unknown";
1592 break;
1593 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 switch (alg_enc) {
1596 case SSL_3DES:
1597 enc = "3DES(168)";
1598 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 case SSL_RC4:
1601 enc = "RC4(128)";
1602 break;
1603
1604 case SSL_AES128:
1605 enc = "AES(128)";
1606 break;
1607
1608 case SSL_AES256:
1609 enc = "AES(256)";
1610 break;
1611
1612 case SSL_AES128GCM:
1613 enc = "AESGCM(128)";
1614 break;
1615
1616 case SSL_AES256GCM:
1617 enc = "AESGCM(256)";
1618 break;
1619
1620 case SSL_CHACHA20POLY1305:
1621 enc = "ChaCha20-Poly1305";
1622 break;
1623
1624 default:
1625 enc = "unknown";
1626 break;
1627 }
1628
1629 switch (alg_mac) {
1630 case SSL_MD5:
1631 mac = "MD5";
1632 break;
1633
1634 case SSL_SHA1:
1635 mac = "SHA1";
1636 break;
1637
1638 case SSL_SHA256:
1639 mac = "SHA256";
1640 break;
1641
1642 case SSL_SHA384:
1643 mac = "SHA384";
1644 break;
1645
1646 case SSL_AEAD:
1647 mac = "AEAD";
1648 break;
1649
1650 default:
1651 mac = "unknown";
1652 break;
1653 }
1654
1655 if (buf == NULL) {
1656 len = 128;
1657 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001658 if (buf == NULL) {
1659 return NULL;
1660 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 } else if (len < 128) {
1662 return "Buffer too small";
1663 }
1664
1665 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1666 return buf;
1667}
1668
David Benjamin71f07942015-04-08 02:36:59 -04001669const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1670 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001671}
1672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673void *SSL_COMP_get_compression_methods(void) { return NULL; }
1674
1675int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1676
1677const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001678
David Benjamin71f07942015-04-08 02:36:59 -04001679int ssl_cipher_get_cert_index(const SSL_CIPHER *cipher) {
1680 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001681
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 if (alg_a & SSL_aECDSA) {
1683 return SSL_PKEY_ECC;
1684 } else if (alg_a & SSL_aRSA) {
1685 return SSL_PKEY_RSA_ENC;
1686 }
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 return -1;
1689}
David Benjamin9c651c92014-07-12 13:27:45 -04001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001692 /* PSK-authenticated ciphers do not use a public key, except for
1693 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 if ((cipher->algorithm_auth & SSL_aPSK) &&
1695 !(cipher->algorithm_mkey & SSL_kRSA)) {
1696 return 0;
1697 }
1698
1699 /* All other ciphers include it. */
1700 return 1;
1701}
1702
Adam Langleyfcf25832014-12-18 17:42:32 -08001703int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1704 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001705 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 return 1;
1707 }
1708
1709 /* It is optional in all others. */
1710 return 0;
1711}