blob: 632db30ad2cc3ff4311b91631eef41439e84bff1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400144#include <stdio.h>
145#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin71f07942015-04-08 02:36:59 -0400147#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400148#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500151#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400152#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamina1c90a52015-05-30 17:03:14 -0400157/* kCiphers is an array of all supported ciphers, sorted by id. */
158const SSL_CIPHER kCiphers[] = {
159 /* The RSA ciphers */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700160 /* Cipher 02 */
161 {
162 SSL3_TXT_RSA_NULL_SHA, SSL3_CK_RSA_NULL_SHA, SSL_kRSA, SSL_aRSA,
163 SSL_eNULL, SSL_SHA1, SSL_SSLV3, SSL_FIPS, SSL_HANDSHAKE_MAC_DEFAULT, 0, 0,
164 },
165
David Benjamina1c90a52015-05-30 17:03:14 -0400166 /* Cipher 04 */
167 {
168 SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
169 SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
170 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
171 },
172
173 /* Cipher 05 */
174 {
175 SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
176 SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
177 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
178 },
179
180 /* Cipher 0A */
181 {
182 SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
183 SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
184 SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
185 },
186
187
188 /* New AES ciphersuites */
189
190 /* Cipher 2F */
191 {
192 TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
193 SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
194 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
195 },
196
197 /* Cipher 33 */
198 {
199 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
200 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
201 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
202 },
203
204 /* Cipher 35 */
205 {
206 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
207 SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
208 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
209 },
210
211 /* Cipher 39 */
212 {
213 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
214 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
215 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
216 },
217
218
219 /* TLS v1.2 ciphersuites */
220
221 /* Cipher 3C */
222 {
223 TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
224 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
225 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
226 },
227
228 /* Cipher 3D */
229 {
230 TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
231 SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
232 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
233 },
234
235 /* Cipher 67 */
236 {
237 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
238 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
239 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
240 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
241 },
242
243 /* Cipher 6B */
244 {
245 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
246 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
247 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
248 SSL_HANDSHAKE_MAC_SHA256, 256, 256,
249 },
250
Adam Langley85bc5602015-06-09 09:54:04 -0700251 /* PSK cipher suites. */
252
David Benjamina1c90a52015-05-30 17:03:14 -0400253 /* Cipher 8A */
254 {
255 TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
256 SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
257 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
258 },
259
260 /* Cipher 8C */
261 {
262 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
263 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
264 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
265 },
266
267 /* Cipher 8D */
268 {
269 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
270 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
271 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
272 },
273
David Benjamina1c90a52015-05-30 17:03:14 -0400274 /* GCM ciphersuites from RFC5288 */
275
276 /* Cipher 9C */
277 {
278 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
279 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
280 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400281 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400282 128, 128,
283 },
284
285 /* Cipher 9D */
286 {
287 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
288 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
289 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400290 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400291 256, 256,
292 },
293
294 /* Cipher 9E */
295 {
296 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
297 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
298 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400299 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400300 128, 128,
301 },
302
303 /* Cipher 9F */
304 {
305 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
306 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
307 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400308 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400309 256, 256,
310 },
311
312 /* Cipher C007 */
313 {
314 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
315 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
316 SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
317 128,
318 },
319
320 /* Cipher C009 */
321 {
322 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
323 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
324 SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
325 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
326 },
327
328 /* Cipher C00A */
329 {
330 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
331 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
332 SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
333 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
334 },
335
336 /* Cipher C011 */
337 {
338 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
339 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
340 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
341 },
342
343 /* Cipher C013 */
344 {
345 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
346 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
347 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
348 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
349 },
350
351 /* Cipher C014 */
352 {
353 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
354 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
355 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
356 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
357 },
358
359
360 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
361
362 /* Cipher C023 */
363 {
364 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
365 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
366 SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
367 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
368 },
369
370 /* Cipher C024 */
371 {
372 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
373 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
374 SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
375 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
376 },
377
378 /* Cipher C027 */
379 {
380 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
381 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
382 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
383 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
384 },
385
386 /* Cipher C028 */
387 {
388 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
389 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
390 SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
391 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
392 },
393
394
395 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
396
397 /* Cipher C02B */
398 {
399 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
400 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
401 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400402 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400403 128, 128,
404 },
405
406 /* Cipher C02C */
407 {
408 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
409 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
410 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400411 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400412 256, 256,
413 },
414
415 /* Cipher C02F */
416 {
417 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
418 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
419 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400420 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400421 128, 128,
422 },
423
424 /* Cipher C030 */
425 {
426 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
427 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
428 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400429 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400430 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,
David Benjaminbe8a8692015-08-21 13:51:56 -0400458 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400459 },
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,
David Benjaminbe8a8692015-08-21 13:51:56 -0400466 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400467 },
David Benjamina1c90a52015-05-30 17:03:14 -0400468};
469
470static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472#define CIPHER_ADD 1
473#define CIPHER_KILL 2
474#define CIPHER_DEL 3
475#define CIPHER_ORD 4
476#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478typedef struct cipher_order_st {
479 const SSL_CIPHER *cipher;
480 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 int in_group;
482 struct cipher_order_st *next, *prev;
483} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin0344daf2015-04-08 02:08:01 -0400485typedef struct cipher_alias_st {
486 /* name is the name of the cipher alias. */
487 const char *name;
488
489 /* The following fields are bitmasks for the corresponding fields on
490 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
491 * bit corresponding to the cipher's value is set to 1. If any bitmask is
492 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
493 uint32_t algorithm_mkey;
494 uint32_t algorithm_auth;
495 uint32_t algorithm_enc;
496 uint32_t algorithm_mac;
497 uint32_t algorithm_ssl;
498 uint32_t algo_strength;
499} CIPHER_ALIAS;
500
David Benjamina1c90a52015-05-30 17:03:14 -0400501static const CIPHER_ALIAS kCipherAliases[] = {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700502 /* "ALL" doesn't include eNULL (must be specifically enabled) */
503 {SSL_TXT_ALL, ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamina1c90a52015-05-30 17:03:14 -0400505 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamina1c90a52015-05-30 17:03:14 -0400507 /* key exchange aliases
508 * (some of those using only a single bit here combine
509 * multiple key exchange algs according to the RFCs,
510 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
511 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamina1c90a52015-05-30 17:03:14 -0400513 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
514 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
515 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamina1c90a52015-05-30 17:03:14 -0400517 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
518 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
519 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamina1c90a52015-05-30 17:03:14 -0400521 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamina1c90a52015-05-30 17:03:14 -0400523 /* server authentication aliases */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700524 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, ~0u},
David Benjamina1c90a52015-05-30 17:03:14 -0400525 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
526 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
527 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjamina1c90a52015-05-30 17:03:14 -0400529 /* aliases combining key exchange and server authentication */
530 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
531 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
532 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
533 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700534 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, ~0u},
David Benjamina1c90a52015-05-30 17:03:14 -0400535 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamina1c90a52015-05-30 17:03:14 -0400537 /* symmetric encryption aliases */
538 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
539 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
540 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
541 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
542 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
543 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
544 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamina1c90a52015-05-30 17:03:14 -0400546 /* MAC aliases */
547 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700548 {SSL_TXT_SHA1, ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, ~0u},
549 {SSL_TXT_SHA, ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, ~0u},
David Benjamina1c90a52015-05-30 17:03:14 -0400550 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
551 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamina1c90a52015-05-30 17:03:14 -0400553 /* protocol version aliases */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700554 {SSL_TXT_SSLV3, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_SSLV3, ~0u},
555 {SSL_TXT_TLSV1, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_TLSV1, ~0u},
556 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamina1c90a52015-05-30 17:03:14 -0400558 /* strength classes */
559 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
560 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
561 /* FIPS 140-2 approved ciphersuite */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700562 {SSL_TXT_FIPS, ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800563};
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjamina1c90a52015-05-30 17:03:14 -0400565static const size_t kCipherAliasesLen =
566 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
567
568static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
569 const SSL_CIPHER *a = in_a;
570 const SSL_CIPHER *b = in_b;
571
572 if (a->id > b->id) {
573 return 1;
574 } else if (a->id < b->id) {
575 return -1;
576 } else {
577 return 0;
578 }
579}
580
581static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
582 return ssl_cipher_id_cmp(*a, *b);
583}
584
585const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
586 SSL_CIPHER c;
587
588 c.id = 0x03000000L | value;
589 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
590 ssl_cipher_id_cmp);
591}
David Benjamin0344daf2015-04-08 02:08:01 -0400592
David Benjaminea72bd02014-12-21 21:27:41 -0500593int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
594 size_t *out_mac_secret_len,
595 size_t *out_fixed_iv_len,
596 const SSL_CIPHER *cipher, uint16_t version) {
597 *out_aead = NULL;
598 *out_mac_secret_len = 0;
599 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700600
David Benjaminea72bd02014-12-21 21:27:41 -0500601 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500603 *out_aead = EVP_aead_aes_128_gcm();
604 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 return 1;
606
607 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500608 *out_aead = EVP_aead_aes_256_gcm();
609 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 return 1;
611
612 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500613 *out_aead = EVP_aead_chacha20_poly1305();
614 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 return 1;
616
617 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500618 switch (cipher->algorithm_mac) {
619 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500620 if (version == SSL3_VERSION) {
621 *out_aead = EVP_aead_rc4_md5_ssl3();
622 } else {
623 *out_aead = EVP_aead_rc4_md5_tls();
624 }
David Benjaminea72bd02014-12-21 21:27:41 -0500625 *out_mac_secret_len = MD5_DIGEST_LENGTH;
626 return 1;
627 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500628 if (version == SSL3_VERSION) {
629 *out_aead = EVP_aead_rc4_sha1_ssl3();
630 } else {
631 *out_aead = EVP_aead_rc4_sha1_tls();
632 }
David Benjaminea72bd02014-12-21 21:27:41 -0500633 *out_mac_secret_len = SHA_DIGEST_LENGTH;
634 return 1;
635 default:
636 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800638
David Benjaminea72bd02014-12-21 21:27:41 -0500639 case SSL_AES128:
640 switch (cipher->algorithm_mac) {
641 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500642 if (version == SSL3_VERSION) {
643 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
644 *out_fixed_iv_len = 16;
645 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500646 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
647 *out_fixed_iv_len = 16;
648 } else {
649 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
650 }
651 *out_mac_secret_len = SHA_DIGEST_LENGTH;
652 return 1;
653 case SSL_SHA256:
654 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
655 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
656 return 1;
657 default:
658 return 0;
659 }
660
661 case SSL_AES256:
662 switch (cipher->algorithm_mac) {
663 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500664 if (version == SSL3_VERSION) {
665 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
666 *out_fixed_iv_len = 16;
667 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500668 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
669 *out_fixed_iv_len = 16;
670 } else {
671 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
672 }
673 *out_mac_secret_len = SHA_DIGEST_LENGTH;
674 return 1;
675 case SSL_SHA256:
676 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
677 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
678 return 1;
679 case SSL_SHA384:
680 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
681 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
682 return 1;
683 default:
684 return 0;
685 }
686
687 case SSL_3DES:
688 switch (cipher->algorithm_mac) {
689 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500690 if (version == SSL3_VERSION) {
691 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
692 *out_fixed_iv_len = 8;
693 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500694 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
695 *out_fixed_iv_len = 8;
696 } else {
697 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
698 }
699 *out_mac_secret_len = SHA_DIGEST_LENGTH;
700 return 1;
701 default:
702 return 0;
703 }
704
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700705 case SSL_eNULL:
706 switch (cipher->algorithm_mac) {
707 case SSL_SHA1:
708 if (version == SSL3_VERSION) {
709 *out_aead = EVP_aead_null_sha1_ssl3();
710 } else {
711 *out_aead = EVP_aead_null_sha1_tls();
712 }
713 *out_mac_secret_len = SHA_DIGEST_LENGTH;
714 return 1;
715 default:
716 return 0;
717 }
718
David Benjaminea72bd02014-12-21 21:27:41 -0500719 default:
720 return 0;
721 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800722}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700723
David Benjaminb0883312015-08-06 09:54:13 -0400724const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
725 switch (algorithm_prf) {
726 case SSL_HANDSHAKE_MAC_DEFAULT:
727 return EVP_sha1();
728 case SSL_HANDSHAKE_MAC_SHA256:
729 return EVP_sha256();
730 case SSL_HANDSHAKE_MAC_SHA384:
731 return EVP_sha384();
732 default:
733 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 }
Adam Langley95c29f32014-06-20 12:00:00 -0700735}
736
737#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800738 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700739
David Benjamin0344daf2015-04-08 02:08:01 -0400740/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
741 * |buf_len| bytes at |buf|. */
742static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
743 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
744 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
745}
746
Adam Langley95c29f32014-06-20 12:00:00 -0700747static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800748 CIPHER_ORDER **tail) {
749 if (curr == *tail) {
750 return;
751 }
752 if (curr == *head) {
753 *head = curr->next;
754 }
755 if (curr->prev != NULL) {
756 curr->prev->next = curr->next;
757 }
758 if (curr->next != NULL) {
759 curr->next->prev = curr->prev;
760 }
761 (*tail)->next = curr;
762 curr->prev = *tail;
763 curr->next = NULL;
764 *tail = curr;
765}
Adam Langley95c29f32014-06-20 12:00:00 -0700766
767static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 CIPHER_ORDER **tail) {
769 if (curr == *head) {
770 return;
771 }
772 if (curr == *tail) {
773 *tail = curr->prev;
774 }
775 if (curr->next != NULL) {
776 curr->next->prev = curr->prev;
777 }
778 if (curr->prev != NULL) {
779 curr->prev->next = curr->next;
780 }
781 (*head)->prev = curr;
782 curr->next = *head;
783 curr->prev = NULL;
784 *head = curr;
785}
Adam Langley95c29f32014-06-20 12:00:00 -0700786
David Benjamin82c9e902014-12-12 15:55:27 -0500787static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 CIPHER_ORDER *co_list,
789 CIPHER_ORDER **head_p,
790 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400791 /* The set of ciphers is static, but some subset may be unsupported by
792 * |ssl_method|, so the list may be smaller. */
793 size_t co_list_num = 0;
794 size_t i;
795 for (i = 0; i < kCiphersLen; i++) {
796 const SSL_CIPHER *cipher = &kCiphers[i];
797 if (ssl_method->supports_cipher(cipher)) {
798 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 co_list[co_list_num].next = NULL;
800 co_list[co_list_num].prev = NULL;
801 co_list[co_list_num].active = 0;
802 co_list[co_list_num].in_group = 0;
803 co_list_num++;
804 }
805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 /* Prepare linked list from list entries. */
808 if (co_list_num > 0) {
809 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 if (co_list_num > 1) {
812 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 for (i = 1; i < co_list_num - 1; i++) {
815 co_list[i].prev = &co_list[i - 1];
816 co_list[i].next = &co_list[i + 1];
817 }
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
820 }
821
822 co_list[co_list_num - 1].next = NULL;
823
824 *head_p = &co_list[0];
825 *tail_p = &co_list[co_list_num - 1];
826 }
827}
Adam Langley95c29f32014-06-20 12:00:00 -0700828
David Benjamin0344daf2015-04-08 02:08:01 -0400829/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
830 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
831 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
832 *
833 * - If |cipher_id| is non-zero, only that cipher is selected.
834 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
835 * of that strength.
836 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
837 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800838static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400839 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
840 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
841 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
843 CIPHER_ORDER *head, *tail, *curr, *next, *last;
844 const SSL_CIPHER *cp;
845 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700846
David Benjamin0344daf2015-04-08 02:08:01 -0400847 if (cipher_id == 0 && strength_bits == -1 &&
848 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
849 alg_ssl == 0 || algo_strength == 0)) {
850 /* The rule matches nothing, so bail early. */
851 return;
852 }
853
Adam Langleyfcf25832014-12-18 17:42:32 -0800854 if (rule == CIPHER_DEL) {
855 /* needed to maintain sorting between currently deleted ciphers */
856 reverse = 1;
857 }
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 head = *head_p;
860 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 if (reverse) {
863 next = tail;
864 last = head;
865 } else {
866 next = head;
867 last = tail;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 curr = NULL;
871 for (;;) {
872 if (curr == last) {
873 break;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 curr = next;
877 if (curr == NULL) {
878 break;
879 }
Adam Langleye3142a72014-07-24 17:56:48 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 next = reverse ? curr->prev : curr->next;
882 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700883
David Benjamin0344daf2015-04-08 02:08:01 -0400884 /* Selection criteria is either a specific cipher, the value of
885 * |strength_bits|, or the algorithms used. */
886 if (cipher_id != 0) {
887 if (cipher_id != cp->id) {
888 continue;
889 }
890 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 if (strength_bits != cp->strength_bits) {
892 continue;
893 }
David Benjamin0344daf2015-04-08 02:08:01 -0400894 } else if (!(alg_mkey & cp->algorithm_mkey) ||
895 !(alg_auth & cp->algorithm_auth) ||
896 !(alg_enc & cp->algorithm_enc) ||
897 !(alg_mac & cp->algorithm_mac) ||
898 !(alg_ssl & cp->algorithm_ssl) ||
899 !(algo_strength & cp->algo_strength)) {
900 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 }
Adam Langleye3142a72014-07-24 17:56:48 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 /* add the cipher if it has not been added yet. */
904 if (rule == CIPHER_ADD) {
905 /* reverse == 0 */
906 if (!curr->active) {
907 ll_append_tail(&head, curr, &tail);
908 curr->active = 1;
909 curr->in_group = in_group;
910 }
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 /* Move the added cipher to this location */
914 else if (rule == CIPHER_ORD) {
915 /* reverse == 0 */
916 if (curr->active) {
917 ll_append_tail(&head, curr, &tail);
918 curr->in_group = 0;
919 }
920 } else if (rule == CIPHER_DEL) {
921 /* reverse == 1 */
922 if (curr->active) {
923 /* most recently deleted ciphersuites get best positions
924 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
925 * works in reverse to maintain the order) */
926 ll_append_head(&head, curr, &tail);
927 curr->active = 0;
928 curr->in_group = 0;
929 }
930 } else if (rule == CIPHER_KILL) {
931 /* reverse == 0 */
932 if (head == curr) {
933 head = curr->next;
934 } else {
935 curr->prev->next = curr->next;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 if (tail == curr) {
939 tail = curr->prev;
940 }
941 curr->active = 0;
942 if (curr->next != NULL) {
943 curr->next->prev = curr->prev;
944 }
945 if (curr->prev != NULL) {
946 curr->prev->next = curr->next;
947 }
948 curr->next = NULL;
949 curr->prev = NULL;
950 }
951 }
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 *head_p = head;
954 *tail_p = tail;
955}
Adam Langley95c29f32014-06-20 12:00:00 -0700956
957static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 CIPHER_ORDER **tail_p) {
959 int max_strength_bits, i, *number_uses;
960 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700961
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 /* This routine sorts the ciphers with descending strength. The sorting must
963 * keep the pre-sorted sequence, so we apply the normal sorting routine as
964 * '+' movement to the end of the list. */
965 max_strength_bits = 0;
966 curr = *head_p;
967 while (curr != NULL) {
968 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
969 max_strength_bits = curr->cipher->strength_bits;
970 }
971 curr = curr->next;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
975 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400976 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 return 0;
978 }
979 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 /* Now find the strength_bits values actually used. */
982 curr = *head_p;
983 while (curr != NULL) {
984 if (curr->active) {
985 number_uses[curr->cipher->strength_bits]++;
986 }
987 curr = curr->next;
988 }
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 /* Go through the list of used strength_bits values in descending order. */
991 for (i = max_strength_bits; i >= 0; i--) {
992 if (number_uses[i] > 0) {
993 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
994 tail_p);
995 }
996 }
997
998 OPENSSL_free(number_uses);
999 return 1;
1000}
Adam Langley95c29f32014-06-20 12:00:00 -07001001
David Benjamin0344daf2015-04-08 02:08:01 -04001002static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1003 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001005 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -04001006 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -04001008 int multi, rule, retval, ok, in_group = 0, has_group = 0;
1009 size_t j, buf_len;
1010 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 retval = 1;
1014 l = rule_str;
1015 for (;;) {
1016 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 if (ch == '\0') {
1019 break; /* done */
1020 }
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 if (in_group) {
1023 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 if (*tail_p) {
1025 (*tail_p)->in_group = 0;
1026 }
1027 in_group = 0;
1028 l++;
1029 continue;
1030 }
David Benjamin37d92462014-09-20 17:54:24 -04001031
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 if (ch == '|') {
1033 rule = CIPHER_ADD;
1034 l++;
1035 continue;
1036 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1037 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001038 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001039 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 break;
1041 } else {
1042 rule = CIPHER_ADD;
1043 }
1044 } else if (ch == '-') {
1045 rule = CIPHER_DEL;
1046 l++;
1047 } else if (ch == '+') {
1048 rule = CIPHER_ORD;
1049 l++;
1050 } else if (ch == '!') {
1051 rule = CIPHER_KILL;
1052 l++;
1053 } else if (ch == '@') {
1054 rule = CIPHER_SPECIAL;
1055 l++;
1056 } else if (ch == '[') {
1057 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001058 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001059 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 break;
1061 }
1062 in_group = 1;
1063 has_group = 1;
1064 l++;
1065 continue;
1066 } else {
1067 rule = CIPHER_ADD;
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 /* If preference groups are enabled, the only legal operator is +.
1071 * Otherwise the in_group bits will get mixed up. */
1072 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001073 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001074 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 break;
1076 }
Adam Langley95c29f32014-06-20 12:00:00 -07001077
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 if (ITEM_SEP(ch)) {
1079 l++;
1080 continue;
1081 }
Adam Langley95c29f32014-06-20 12:00:00 -07001082
David Benjamin0344daf2015-04-08 02:08:01 -04001083 multi = 0;
1084 cipher_id = 0;
1085 alg_mkey = ~0u;
1086 alg_auth = ~0u;
1087 alg_enc = ~0u;
1088 alg_mac = ~0u;
1089 alg_ssl = ~0u;
1090 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 for (;;) {
1093 ch = *l;
1094 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001095 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1097 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1098 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001099 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
David Benjamin0344daf2015-04-08 02:08:01 -04001102 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 /* We hit something we cannot deal with, it is no command or separator
1104 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001105 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001106 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 l++;
1108 break;
1109 }
Adam Langley95c29f32014-06-20 12:00:00 -07001110
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 break;
1113 }
David Benjamin0344daf2015-04-08 02:08:01 -04001114
1115 /* Look for a matching exact cipher. These aren't allowed in multipart
1116 * rules. */
1117 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001118 for (j = 0; j < kCiphersLen; j++) {
1119 const SSL_CIPHER *cipher = &kCiphers[j];
1120 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001121 cipher_id = cipher->id;
1122 break;
1123 }
1124 }
1125 }
1126 if (cipher_id == 0) {
1127 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001128 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001129 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1130 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1131 alg_auth &= kCipherAliases[j].algorithm_auth;
1132 alg_enc &= kCipherAliases[j].algorithm_enc;
1133 alg_mac &= kCipherAliases[j].algorithm_mac;
1134 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1135 algo_strength &= kCipherAliases[j].algo_strength;
1136 break;
1137 }
1138 }
David Benjamina1c90a52015-05-30 17:03:14 -04001139 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001140 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1141 }
1142 }
1143
1144 /* Check for a multipart rule. */
1145 if (ch != '+') {
1146 break;
1147 }
1148 l++;
1149 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 }
Adam Langley95c29f32014-06-20 12:00:00 -07001151
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 /* Ok, we have the rule, now apply it. */
1153 if (rule == CIPHER_SPECIAL) {
1154 /* special command */
1155 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001156 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 ok = ssl_cipher_strength_sort(head_p, tail_p);
1158 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001159 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 }
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (ok == 0) {
1163 retval = 0;
1164 }
Adam Langley95c29f32014-06-20 12:00:00 -07001165
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 /* We do not support any "multi" options together with "@", so throw away
1167 * the rest of the command, if any left, until end or ':' is found. */
1168 while (*l != '\0' && !ITEM_SEP(*l)) {
1169 l++;
1170 }
David Benjamin0344daf2015-04-08 02:08:01 -04001171 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1173 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1174 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 }
1176 }
Adam Langley95c29f32014-06-20 12:00:00 -07001177
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001179 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 retval = 0;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 return retval;
1184}
Adam Langley95c29f32014-06-20 12:00:00 -07001185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186STACK_OF(SSL_CIPHER) *
1187ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001188 struct ssl_cipher_preference_list_st **out_cipher_list,
1189 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1190 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001191 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1193 const char *rule_p;
1194 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 uint8_t *in_group_flags = NULL;
1196 unsigned int num_in_group_flags = 0;
1197 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001200 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 return NULL;
1202 }
David Benjamin5213df42014-08-20 14:19:54 -04001203
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 /* Now we have to collect the available ciphers from the compiled in ciphers.
1205 * We cannot get more than the number compiled in, so it is used for
1206 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001207 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001209 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 return NULL;
1211 }
Adam Langley95c29f32014-06-20 12:00:00 -07001212
David Benjamina1c90a52015-05-30 17:03:14 -04001213 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215 /* Now arrange all ciphers by preference:
1216 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001217
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1219 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001220 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1221 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001222 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001224 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1225 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001226
Adam Langleyfcf25832014-12-18 17:42:32 -08001227 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1228 * CHACHA20 unless there is hardware support for fast and constant-time
1229 * AES_GCM. */
1230 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001231 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001232 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001233 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1234 -1, 0, &head, &tail);
1235 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1236 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001238 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1239 CIPHER_ADD, -1, 0, &head, &tail);
1240 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001242 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1243 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 }
Adam Langley95c29f32014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1247 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001248 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1249 0, &head, &tail);
1250 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1251 0, &head, &tail);
1252 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1253 -1, 0, &head, &tail);
1254 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1255 0, &head, &tail);
1256 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001258
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001260 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1261 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001264 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1265 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001268 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1269 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 /* If the rule_string begins with DEFAULT, apply the default rule before
1272 * using the (possibly available) additional rules. */
1273 ok = 1;
1274 rule_p = rule_str;
1275 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001276 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1277 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 rule_p += 7;
1279 if (*rule_p == ':') {
1280 rule_p++;
1281 }
1282 }
Adam Langley858a88d2014-06-20 12:00:00 -07001283
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001285 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 }
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 if (!ok) {
1289 goto err;
1290 }
1291
1292 /* Allocate new "cipherstack" for the result, return with error
1293 * if we cannot get one. */
1294 cipherstack = sk_SSL_CIPHER_new_null();
1295 if (cipherstack == NULL) {
1296 goto err;
1297 }
1298
David Benjamina1c90a52015-05-30 17:03:14 -04001299 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 if (!in_group_flags) {
1301 goto err;
1302 }
1303
1304 /* The cipher selection for the list is done. The ciphers are added
1305 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1306 for (curr = head; curr != NULL; curr = curr->next) {
1307 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001308 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1309 goto err;
1310 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 in_group_flags[num_in_group_flags++] = curr->in_group;
1312 }
1313 }
1314 OPENSSL_free(co_list); /* Not needed any longer */
1315 co_list = NULL;
1316
1317 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1318 if (tmp_cipher_list == NULL) {
1319 goto err;
1320 }
1321 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1322 if (!pref_list) {
1323 goto err;
1324 }
1325 pref_list->ciphers = cipherstack;
1326 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1327 if (!pref_list->in_group_flags) {
1328 goto err;
1329 }
1330 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1331 OPENSSL_free(in_group_flags);
1332 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001333 if (*out_cipher_list != NULL) {
1334 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 }
David Benjamin71f07942015-04-08 02:36:59 -04001336 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 pref_list = NULL;
1338
David Benjamin71f07942015-04-08 02:36:59 -04001339 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001340 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001341 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001343 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1344 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001345
David Benjamin71f07942015-04-08 02:36:59 -04001346 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 } else {
1348 sk_SSL_CIPHER_free(tmp_cipher_list);
1349 tmp_cipher_list = NULL;
1350 }
1351
1352 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001353
1354err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001355 OPENSSL_free(co_list);
1356 OPENSSL_free(in_group_flags);
1357 sk_SSL_CIPHER_free(cipherstack);
1358 sk_SSL_CIPHER_free(tmp_cipher_list);
1359 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 OPENSSL_free(pref_list->in_group_flags);
1361 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001362 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 return NULL;
1364}
Adam Langley95c29f32014-06-20 12:00:00 -07001365
David Benjamin71f07942015-04-08 02:36:59 -04001366uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1367
David Benjamina1c90a52015-05-30 17:03:14 -04001368uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1369 uint32_t id = cipher->id;
1370 /* All ciphers are SSLv3. */
1371 assert((id & 0xff000000) == 0x03000000);
1372 return id & 0xffff;
1373}
1374
David Benjamin71f07942015-04-08 02:36:59 -04001375int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1376 return (cipher->algorithm_enc & SSL_AES) != 0;
1377}
1378
1379int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1380 return (cipher->algorithm_mac & SSL_MD5) != 0;
1381}
1382
1383int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001384 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001385}
1386
1387int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1388 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1389}
1390
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001391int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1392 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1393}
1394
Adam Langleyd7fe75c2015-09-18 15:40:48 -07001395int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) {
1396 return (cipher->algorithm_enc & SSL_RC4) != 0;
1397}
1398
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001399int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1400 /* Neither stream cipher nor AEAD. */
1401 return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 &&
1402 cipher->algorithm_mac != SSL_AEAD;
1403}
1404
David Benjamin71f07942015-04-08 02:36:59 -04001405/* return the actual cipher being used */
1406const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1407 if (cipher != NULL) {
1408 return cipher->name;
1409 }
1410
1411 return "(NONE)";
1412}
1413
1414const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1415 if (cipher == NULL) {
1416 return "";
1417 }
1418
1419 switch (cipher->algorithm_mkey) {
1420 case SSL_kRSA:
1421 return "RSA";
1422
1423 case SSL_kDHE:
1424 switch (cipher->algorithm_auth) {
1425 case SSL_aRSA:
1426 return "DHE_RSA";
1427 default:
1428 assert(0);
1429 return "UNKNOWN";
1430 }
1431
1432 case SSL_kECDHE:
1433 switch (cipher->algorithm_auth) {
1434 case SSL_aECDSA:
1435 return "ECDHE_ECDSA";
1436 case SSL_aRSA:
1437 return "ECDHE_RSA";
1438 case SSL_aPSK:
1439 return "ECDHE_PSK";
1440 default:
1441 assert(0);
1442 return "UNKNOWN";
1443 }
1444
1445 case SSL_kPSK:
1446 assert(cipher->algorithm_auth == SSL_aPSK);
1447 return "PSK";
1448
1449 default:
1450 assert(0);
1451 return "UNKNOWN";
1452 }
1453}
1454
1455static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1456 switch (cipher->algorithm_enc) {
1457 case SSL_3DES:
1458 return "3DES_EDE_CBC";
1459 case SSL_RC4:
1460 return "RC4";
1461 case SSL_AES128:
1462 return "AES_128_CBC";
1463 case SSL_AES256:
1464 return "AES_256_CBC";
1465 case SSL_AES128GCM:
1466 return "AES_128_GCM";
1467 case SSL_AES256GCM:
1468 return "AES_256_GCM";
1469 case SSL_CHACHA20POLY1305:
1470 return "CHACHA20_POLY1305";
1471 break;
1472 default:
1473 assert(0);
1474 return "UNKNOWN";
1475 }
1476}
1477
1478static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001479 switch (cipher->algorithm_prf) {
1480 case SSL_HANDSHAKE_MAC_DEFAULT:
1481 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1482 * only ever MD5 or SHA-1. */
1483 switch (cipher->algorithm_mac) {
1484 case SSL_MD5:
1485 return "MD5";
1486 case SSL_SHA1:
1487 return "SHA";
1488 }
1489 break;
1490 case SSL_HANDSHAKE_MAC_SHA256:
1491 return "SHA256";
1492 case SSL_HANDSHAKE_MAC_SHA384:
1493 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001494 }
David Benjaminb0883312015-08-06 09:54:13 -04001495 assert(0);
1496 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001497}
1498
1499char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1500 if (cipher == NULL) {
1501 return NULL;
1502 }
1503
1504 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1505 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1506 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1507
1508 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1509 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1510 strlen(prf_name) + 1;
1511 char *ret = OPENSSL_malloc(len);
1512 if (ret == NULL) {
1513 return NULL;
1514 }
1515 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1516 BUF_strlcat(ret, kx_name, len) >= len ||
1517 BUF_strlcat(ret, "_WITH_", len) >= len ||
1518 BUF_strlcat(ret, enc_name, len) >= len ||
1519 BUF_strlcat(ret, "_", len) >= len ||
1520 BUF_strlcat(ret, prf_name, len) >= len) {
1521 assert(0);
1522 OPENSSL_free(ret);
1523 return NULL;
1524 }
1525 assert(strlen(ret) + 1 == len);
1526 return ret;
1527}
1528
1529int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1530 if (cipher == NULL) {
1531 return 0;
1532 }
1533
1534 if (out_alg_bits != NULL) {
1535 *out_alg_bits = cipher->alg_bits;
1536 }
1537 return cipher->strength_bits;
1538}
1539
Adam Langleyfcf25832014-12-18 17:42:32 -08001540const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1541 int len) {
1542 const char *ver;
1543 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001544 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001546
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 alg_mkey = cipher->algorithm_mkey;
1548 alg_auth = cipher->algorithm_auth;
1549 alg_enc = cipher->algorithm_enc;
1550 alg_mac = cipher->algorithm_mac;
1551 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 if (alg_ssl & SSL_SSLV3) {
1554 ver = "SSLv3";
1555 } else if (alg_ssl & SSL_TLSV1_2) {
1556 ver = "TLSv1.2";
1557 } else {
1558 ver = "unknown";
1559 }
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 switch (alg_mkey) {
1562 case SSL_kRSA:
1563 kx = "RSA";
1564 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001565
David Benjamin7061e282015-03-19 11:10:48 -04001566 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 kx = "DH";
1568 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001569
David Benjamin7061e282015-03-19 11:10:48 -04001570 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 kx = "ECDH";
1572 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574 case SSL_kPSK:
1575 kx = "PSK";
1576 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001577
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 default:
1579 kx = "unknown";
1580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 switch (alg_auth) {
1583 case SSL_aRSA:
1584 au = "RSA";
1585 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 case SSL_aECDSA:
1588 au = "ECDSA";
1589 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001590
Adam Langleyfcf25832014-12-18 17:42:32 -08001591 case SSL_aPSK:
1592 au = "PSK";
1593 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 default:
1596 au = "unknown";
1597 break;
1598 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 switch (alg_enc) {
1601 case SSL_3DES:
1602 enc = "3DES(168)";
1603 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001604
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 case SSL_RC4:
1606 enc = "RC4(128)";
1607 break;
1608
1609 case SSL_AES128:
1610 enc = "AES(128)";
1611 break;
1612
1613 case SSL_AES256:
1614 enc = "AES(256)";
1615 break;
1616
1617 case SSL_AES128GCM:
1618 enc = "AESGCM(128)";
1619 break;
1620
1621 case SSL_AES256GCM:
1622 enc = "AESGCM(256)";
1623 break;
1624
1625 case SSL_CHACHA20POLY1305:
1626 enc = "ChaCha20-Poly1305";
1627 break;
1628
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001629 case SSL_eNULL:
1630 enc="None";
1631 break;
1632
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 default:
1634 enc = "unknown";
1635 break;
1636 }
1637
1638 switch (alg_mac) {
1639 case SSL_MD5:
1640 mac = "MD5";
1641 break;
1642
1643 case SSL_SHA1:
1644 mac = "SHA1";
1645 break;
1646
1647 case SSL_SHA256:
1648 mac = "SHA256";
1649 break;
1650
1651 case SSL_SHA384:
1652 mac = "SHA384";
1653 break;
1654
1655 case SSL_AEAD:
1656 mac = "AEAD";
1657 break;
1658
1659 default:
1660 mac = "unknown";
1661 break;
1662 }
1663
1664 if (buf == NULL) {
1665 len = 128;
1666 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001667 if (buf == NULL) {
1668 return NULL;
1669 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 } else if (len < 128) {
1671 return "Buffer too small";
1672 }
1673
1674 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1675 return buf;
1676}
1677
David Benjamin71f07942015-04-08 02:36:59 -04001678const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1679 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001680}
1681
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001682COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001683
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001684int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001685
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001686const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001687
David Benjamind1d80782015-07-05 11:54:09 -04001688int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001689 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001692 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001694 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 }
Adam Langley95c29f32014-06-20 12:00:00 -07001696
David Benjamind1d80782015-07-05 11:54:09 -04001697 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001698}
David Benjamin9c651c92014-07-12 13:27:45 -04001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001701 /* PSK-authenticated ciphers do not use a public key, except for
1702 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 if ((cipher->algorithm_auth & SSL_aPSK) &&
1704 !(cipher->algorithm_mkey & SSL_kRSA)) {
1705 return 0;
1706 }
1707
1708 /* All other ciphers include it. */
1709 return 1;
1710}
1711
Adam Langleyfcf25832014-12-18 17:42:32 -08001712int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1713 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001714 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001715 return 1;
1716 }
1717
1718 /* It is optional in all others. */
1719 return 0;
1720}
David Benjaminb8d28cf2015-07-28 21:34:45 -04001721
1722size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1723 size_t block_size;
1724 switch (cipher->algorithm_enc) {
1725 case SSL_3DES:
1726 block_size = 8;
1727 break;
1728 case SSL_AES128:
1729 case SSL_AES256:
1730 block_size = 16;
1731 break;
1732 default:
1733 return 0;
1734 }
1735
1736 size_t mac_len;
1737 switch (cipher->algorithm_mac) {
1738 case SSL_MD5:
1739 mac_len = MD5_DIGEST_LENGTH;
1740 break;
1741 case SSL_SHA1:
1742 mac_len = SHA_DIGEST_LENGTH;
1743 break;
1744 default:
1745 return 0;
1746 }
1747
1748 size_t ret = 1 + mac_len;
1749 ret += block_size - (ret % block_size);
1750 return ret;
1751}