blob: cbdf7ca085a6c4f9033cfbc67fd5d68a0ccaa0f2 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin71f07942015-04-08 02:36:59 -0400145#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400146#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500147#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400150#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamina1c90a52015-05-30 17:03:14 -0400155/* kCiphers is an array of all supported ciphers, sorted by id. */
156const SSL_CIPHER kCiphers[] = {
157 /* The RSA ciphers */
158 /* Cipher 04 */
159 {
160 SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
161 SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
162 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
163 },
164
165 /* Cipher 05 */
166 {
167 SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
168 SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
169 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
170 },
171
172 /* Cipher 0A */
173 {
174 SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
175 SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
176 SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
177 },
178
179
180 /* New AES ciphersuites */
181
182 /* Cipher 2F */
183 {
184 TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
185 SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
186 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
187 },
188
189 /* Cipher 33 */
190 {
191 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
192 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
193 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
194 },
195
196 /* Cipher 35 */
197 {
198 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
199 SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
200 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
201 },
202
203 /* Cipher 39 */
204 {
205 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
206 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
207 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
208 },
209
210
211 /* TLS v1.2 ciphersuites */
212
213 /* Cipher 3C */
214 {
215 TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
216 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
217 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
218 },
219
220 /* Cipher 3D */
221 {
222 TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
223 SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
224 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
225 },
226
227 /* Cipher 67 */
228 {
229 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
230 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
231 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
232 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
233 },
234
235 /* Cipher 6B */
236 {
237 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
238 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
239 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
240 SSL_HANDSHAKE_MAC_SHA256, 256, 256,
241 },
242
Adam Langley85bc5602015-06-09 09:54:04 -0700243 /* PSK cipher suites. */
244
David Benjamina1c90a52015-05-30 17:03:14 -0400245 /* Cipher 8A */
246 {
247 TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
248 SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
249 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
250 },
251
252 /* Cipher 8C */
253 {
254 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
255 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
256 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
257 },
258
259 /* Cipher 8D */
260 {
261 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
262 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
263 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
264 },
265
David Benjamina1c90a52015-05-30 17:03:14 -0400266 /* GCM ciphersuites from RFC5288 */
267
268 /* Cipher 9C */
269 {
270 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
271 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
272 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400273 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400274 128, 128,
275 },
276
277 /* Cipher 9D */
278 {
279 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
280 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
281 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400282 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400283 256, 256,
284 },
285
286 /* Cipher 9E */
287 {
288 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
289 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
290 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400291 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400292 128, 128,
293 },
294
295 /* Cipher 9F */
296 {
297 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
298 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
299 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400300 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400301 256, 256,
302 },
303
304 /* Cipher C007 */
305 {
306 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
307 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
308 SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
309 128,
310 },
311
312 /* Cipher C009 */
313 {
314 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
315 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
316 SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
317 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
318 },
319
320 /* Cipher C00A */
321 {
322 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
323 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
324 SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
325 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
326 },
327
328 /* Cipher C011 */
329 {
330 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
331 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
332 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
333 },
334
335 /* Cipher C013 */
336 {
337 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
338 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
339 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
340 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
341 },
342
343 /* Cipher C014 */
344 {
345 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
346 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
347 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
348 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
349 },
350
351
352 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
353
354 /* Cipher C023 */
355 {
356 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
357 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
358 SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
359 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
360 },
361
362 /* Cipher C024 */
363 {
364 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
365 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
366 SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
367 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
368 },
369
370 /* Cipher C027 */
371 {
372 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
373 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
374 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
375 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
376 },
377
378 /* Cipher C028 */
379 {
380 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
381 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
382 SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
383 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
384 },
385
386
387 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
388
389 /* Cipher C02B */
390 {
391 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
392 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
393 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400394 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400395 128, 128,
396 },
397
398 /* Cipher C02C */
399 {
400 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
401 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
402 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400403 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400404 256, 256,
405 },
406
407 /* Cipher C02F */
408 {
409 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
410 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
411 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400412 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400413 128, 128,
414 },
415
416 /* Cipher C030 */
417 {
418 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
419 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
420 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400421 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400422 256, 256,
423 },
424
Adam Langley85bc5602015-06-09 09:54:04 -0700425 /* ECDHE-PSK cipher suites. */
426
427 /* Cipher C035 */
428 {
429 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
430 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
431 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
432 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
433 },
434
435 /* Cipher C036 */
436 {
437 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
438 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
439 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
440 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
441 },
442
443 /* ChaCha20-Poly1305 cipher suites. */
444
David Benjamina1c90a52015-05-30 17:03:14 -0400445 {
446 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
447 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
448 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
449 SSL_HANDSHAKE_MAC_SHA256,
450 256, 0,
451 },
452
453 {
454 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
455 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
456 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
457 SSL_HANDSHAKE_MAC_SHA256,
458 256, 0,
459 },
460
461 {
462 TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
463 TLS1_CK_DHE_RSA_CHACHA20_POLY1305, SSL_kDHE, SSL_aRSA,
464 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
465 SSL_HANDSHAKE_MAC_SHA256,
466 256, 0,
467 },
468};
469
470static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400473 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400475};
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langleyfcf25832014-12-18 17:42:32 -0800477static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
478 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
479 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
480 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
481 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
482};
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484#define CIPHER_ADD 1
485#define CIPHER_KILL 2
486#define CIPHER_DEL 3
487#define CIPHER_ORD 4
488#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langleyfcf25832014-12-18 17:42:32 -0800490typedef struct cipher_order_st {
491 const SSL_CIPHER *cipher;
492 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 int in_group;
494 struct cipher_order_st *next, *prev;
495} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
David Benjamin0344daf2015-04-08 02:08:01 -0400497typedef struct cipher_alias_st {
498 /* name is the name of the cipher alias. */
499 const char *name;
500
501 /* The following fields are bitmasks for the corresponding fields on
502 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
503 * bit corresponding to the cipher's value is set to 1. If any bitmask is
504 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
505 uint32_t algorithm_mkey;
506 uint32_t algorithm_auth;
507 uint32_t algorithm_enc;
508 uint32_t algorithm_mac;
509 uint32_t algorithm_ssl;
510 uint32_t algo_strength;
511} CIPHER_ALIAS;
512
David Benjamina1c90a52015-05-30 17:03:14 -0400513static const CIPHER_ALIAS kCipherAliases[] = {
514 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamina1c90a52015-05-30 17:03:14 -0400516 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700517
David Benjamina1c90a52015-05-30 17:03:14 -0400518 /* key exchange aliases
519 * (some of those using only a single bit here combine
520 * multiple key exchange algs according to the RFCs,
521 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
522 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamina1c90a52015-05-30 17:03:14 -0400524 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
525 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
526 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamina1c90a52015-05-30 17:03:14 -0400528 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
529 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
530 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamina1c90a52015-05-30 17:03:14 -0400532 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamina1c90a52015-05-30 17:03:14 -0400534 /* server authentication aliases */
535 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
536 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
537 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
538 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamina1c90a52015-05-30 17:03:14 -0400540 /* aliases combining key exchange and server authentication */
541 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
542 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
543 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
544 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
545 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
546 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamina1c90a52015-05-30 17:03:14 -0400548 /* symmetric encryption aliases */
549 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
550 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
551 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
552 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
553 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
554 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
555 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamina1c90a52015-05-30 17:03:14 -0400557 /* MAC aliases */
558 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
559 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
560 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
561 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
562 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamina1c90a52015-05-30 17:03:14 -0400564 /* protocol version aliases */
565 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
566 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
567 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamina1c90a52015-05-30 17:03:14 -0400569 /* strength classes */
570 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
571 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
572 /* FIPS 140-2 approved ciphersuite */
573 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800574};
Adam Langley95c29f32014-06-20 12:00:00 -0700575
David Benjamina1c90a52015-05-30 17:03:14 -0400576static const size_t kCipherAliasesLen =
577 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
578
579static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
580 const SSL_CIPHER *a = in_a;
581 const SSL_CIPHER *b = in_b;
582
583 if (a->id > b->id) {
584 return 1;
585 } else if (a->id < b->id) {
586 return -1;
587 } else {
588 return 0;
589 }
590}
591
592static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
593 return ssl_cipher_id_cmp(*a, *b);
594}
595
596const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
597 SSL_CIPHER c;
598
599 c.id = 0x03000000L | value;
600 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
601 ssl_cipher_id_cmp);
602}
David Benjamin0344daf2015-04-08 02:08:01 -0400603
David Benjaminea72bd02014-12-21 21:27:41 -0500604int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
605 size_t *out_mac_secret_len,
606 size_t *out_fixed_iv_len,
607 const SSL_CIPHER *cipher, uint16_t version) {
608 *out_aead = NULL;
609 *out_mac_secret_len = 0;
610 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700611
David Benjaminea72bd02014-12-21 21:27:41 -0500612 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500614 *out_aead = EVP_aead_aes_128_gcm();
615 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 return 1;
617
618 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500619 *out_aead = EVP_aead_aes_256_gcm();
620 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 return 1;
622
623 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500624 *out_aead = EVP_aead_chacha20_poly1305();
625 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 return 1;
627
628 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500629 switch (cipher->algorithm_mac) {
630 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500631 if (version == SSL3_VERSION) {
632 *out_aead = EVP_aead_rc4_md5_ssl3();
633 } else {
634 *out_aead = EVP_aead_rc4_md5_tls();
635 }
David Benjaminea72bd02014-12-21 21:27:41 -0500636 *out_mac_secret_len = MD5_DIGEST_LENGTH;
637 return 1;
638 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500639 if (version == SSL3_VERSION) {
640 *out_aead = EVP_aead_rc4_sha1_ssl3();
641 } else {
642 *out_aead = EVP_aead_rc4_sha1_tls();
643 }
David Benjaminea72bd02014-12-21 21:27:41 -0500644 *out_mac_secret_len = SHA_DIGEST_LENGTH;
645 return 1;
646 default:
647 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800649
David Benjaminea72bd02014-12-21 21:27:41 -0500650 case SSL_AES128:
651 switch (cipher->algorithm_mac) {
652 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500653 if (version == SSL3_VERSION) {
654 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
655 *out_fixed_iv_len = 16;
656 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500657 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
658 *out_fixed_iv_len = 16;
659 } else {
660 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
661 }
662 *out_mac_secret_len = SHA_DIGEST_LENGTH;
663 return 1;
664 case SSL_SHA256:
665 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
666 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
667 return 1;
668 default:
669 return 0;
670 }
671
672 case SSL_AES256:
673 switch (cipher->algorithm_mac) {
674 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500675 if (version == SSL3_VERSION) {
676 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
677 *out_fixed_iv_len = 16;
678 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500679 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
680 *out_fixed_iv_len = 16;
681 } else {
682 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
683 }
684 *out_mac_secret_len = SHA_DIGEST_LENGTH;
685 return 1;
686 case SSL_SHA256:
687 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
688 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
689 return 1;
690 case SSL_SHA384:
691 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
692 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
693 return 1;
694 default:
695 return 0;
696 }
697
698 case SSL_3DES:
699 switch (cipher->algorithm_mac) {
700 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500701 if (version == SSL3_VERSION) {
702 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
703 *out_fixed_iv_len = 8;
704 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500705 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
706 *out_fixed_iv_len = 8;
707 } else {
708 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
709 }
710 *out_mac_secret_len = SHA_DIGEST_LENGTH;
711 return 1;
712 default:
713 return 0;
714 }
715
716 default:
717 return 0;
718 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800719}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700720
David Benjamin71f07942015-04-08 02:36:59 -0400721int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
722 size_t idx) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500723 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 return 0;
725 }
David Benjamin71f07942015-04-08 02:36:59 -0400726 *out_mask = ssl_handshake_digests[idx].mask;
727 *out_md = ssl_handshake_digests[idx].md_func();
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700729}
730
731#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700733
David Benjamin0344daf2015-04-08 02:08:01 -0400734/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
735 * |buf_len| bytes at |buf|. */
736static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
737 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
738 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
739}
740
Adam Langley95c29f32014-06-20 12:00:00 -0700741static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 CIPHER_ORDER **tail) {
743 if (curr == *tail) {
744 return;
745 }
746 if (curr == *head) {
747 *head = curr->next;
748 }
749 if (curr->prev != NULL) {
750 curr->prev->next = curr->next;
751 }
752 if (curr->next != NULL) {
753 curr->next->prev = curr->prev;
754 }
755 (*tail)->next = curr;
756 curr->prev = *tail;
757 curr->next = NULL;
758 *tail = curr;
759}
Adam Langley95c29f32014-06-20 12:00:00 -0700760
761static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 CIPHER_ORDER **tail) {
763 if (curr == *head) {
764 return;
765 }
766 if (curr == *tail) {
767 *tail = curr->prev;
768 }
769 if (curr->next != NULL) {
770 curr->next->prev = curr->prev;
771 }
772 if (curr->prev != NULL) {
773 curr->prev->next = curr->next;
774 }
775 (*head)->prev = curr;
776 curr->next = *head;
777 curr->prev = NULL;
778 *head = curr;
779}
Adam Langley95c29f32014-06-20 12:00:00 -0700780
David Benjamin82c9e902014-12-12 15:55:27 -0500781static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 CIPHER_ORDER *co_list,
783 CIPHER_ORDER **head_p,
784 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400785 /* The set of ciphers is static, but some subset may be unsupported by
786 * |ssl_method|, so the list may be smaller. */
787 size_t co_list_num = 0;
788 size_t i;
789 for (i = 0; i < kCiphersLen; i++) {
790 const SSL_CIPHER *cipher = &kCiphers[i];
791 if (ssl_method->supports_cipher(cipher)) {
792 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800793 co_list[co_list_num].next = NULL;
794 co_list[co_list_num].prev = NULL;
795 co_list[co_list_num].active = 0;
796 co_list[co_list_num].in_group = 0;
797 co_list_num++;
798 }
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 /* Prepare linked list from list entries. */
802 if (co_list_num > 0) {
803 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 if (co_list_num > 1) {
806 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 for (i = 1; i < co_list_num - 1; i++) {
809 co_list[i].prev = &co_list[i - 1];
810 co_list[i].next = &co_list[i + 1];
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
814 }
815
816 co_list[co_list_num - 1].next = NULL;
817
818 *head_p = &co_list[0];
819 *tail_p = &co_list[co_list_num - 1];
820 }
821}
Adam Langley95c29f32014-06-20 12:00:00 -0700822
David Benjamin0344daf2015-04-08 02:08:01 -0400823/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
824 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
825 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
826 *
827 * - If |cipher_id| is non-zero, only that cipher is selected.
828 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
829 * of that strength.
830 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
831 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800832static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400833 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
834 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
835 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
837 CIPHER_ORDER *head, *tail, *curr, *next, *last;
838 const SSL_CIPHER *cp;
839 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700840
David Benjamin0344daf2015-04-08 02:08:01 -0400841 if (cipher_id == 0 && strength_bits == -1 &&
842 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
843 alg_ssl == 0 || algo_strength == 0)) {
844 /* The rule matches nothing, so bail early. */
845 return;
846 }
847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 if (rule == CIPHER_DEL) {
849 /* needed to maintain sorting between currently deleted ciphers */
850 reverse = 1;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 head = *head_p;
854 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (reverse) {
857 next = tail;
858 last = head;
859 } else {
860 next = head;
861 last = tail;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 curr = NULL;
865 for (;;) {
866 if (curr == last) {
867 break;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 curr = next;
871 if (curr == NULL) {
872 break;
873 }
Adam Langleye3142a72014-07-24 17:56:48 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 next = reverse ? curr->prev : curr->next;
876 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700877
David Benjamin0344daf2015-04-08 02:08:01 -0400878 /* Selection criteria is either a specific cipher, the value of
879 * |strength_bits|, or the algorithms used. */
880 if (cipher_id != 0) {
881 if (cipher_id != cp->id) {
882 continue;
883 }
884 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (strength_bits != cp->strength_bits) {
886 continue;
887 }
David Benjamin0344daf2015-04-08 02:08:01 -0400888 } else if (!(alg_mkey & cp->algorithm_mkey) ||
889 !(alg_auth & cp->algorithm_auth) ||
890 !(alg_enc & cp->algorithm_enc) ||
891 !(alg_mac & cp->algorithm_mac) ||
892 !(alg_ssl & cp->algorithm_ssl) ||
893 !(algo_strength & cp->algo_strength)) {
894 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 }
Adam Langleye3142a72014-07-24 17:56:48 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* add the cipher if it has not been added yet. */
898 if (rule == CIPHER_ADD) {
899 /* reverse == 0 */
900 if (!curr->active) {
901 ll_append_tail(&head, curr, &tail);
902 curr->active = 1;
903 curr->in_group = in_group;
904 }
905 }
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 /* Move the added cipher to this location */
908 else if (rule == CIPHER_ORD) {
909 /* reverse == 0 */
910 if (curr->active) {
911 ll_append_tail(&head, curr, &tail);
912 curr->in_group = 0;
913 }
914 } else if (rule == CIPHER_DEL) {
915 /* reverse == 1 */
916 if (curr->active) {
917 /* most recently deleted ciphersuites get best positions
918 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
919 * works in reverse to maintain the order) */
920 ll_append_head(&head, curr, &tail);
921 curr->active = 0;
922 curr->in_group = 0;
923 }
924 } else if (rule == CIPHER_KILL) {
925 /* reverse == 0 */
926 if (head == curr) {
927 head = curr->next;
928 } else {
929 curr->prev->next = curr->next;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (tail == curr) {
933 tail = curr->prev;
934 }
935 curr->active = 0;
936 if (curr->next != NULL) {
937 curr->next->prev = curr->prev;
938 }
939 if (curr->prev != NULL) {
940 curr->prev->next = curr->next;
941 }
942 curr->next = NULL;
943 curr->prev = NULL;
944 }
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 *head_p = head;
948 *tail_p = tail;
949}
Adam Langley95c29f32014-06-20 12:00:00 -0700950
951static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 CIPHER_ORDER **tail_p) {
953 int max_strength_bits, i, *number_uses;
954 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 /* This routine sorts the ciphers with descending strength. The sorting must
957 * keep the pre-sorted sequence, so we apply the normal sorting routine as
958 * '+' movement to the end of the list. */
959 max_strength_bits = 0;
960 curr = *head_p;
961 while (curr != NULL) {
962 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
963 max_strength_bits = curr->cipher->strength_bits;
964 }
965 curr = curr->next;
966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
969 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400970 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 return 0;
972 }
973 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 /* Now find the strength_bits values actually used. */
976 curr = *head_p;
977 while (curr != NULL) {
978 if (curr->active) {
979 number_uses[curr->cipher->strength_bits]++;
980 }
981 curr = curr->next;
982 }
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 /* Go through the list of used strength_bits values in descending order. */
985 for (i = max_strength_bits; i >= 0; i--) {
986 if (number_uses[i] > 0) {
987 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
988 tail_p);
989 }
990 }
991
992 OPENSSL_free(number_uses);
993 return 1;
994}
Adam Langley95c29f32014-06-20 12:00:00 -0700995
David Benjamin0344daf2015-04-08 02:08:01 -0400996static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
997 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400999 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -04001000 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -04001002 int multi, rule, retval, ok, in_group = 0, has_group = 0;
1003 size_t j, buf_len;
1004 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 retval = 1;
1008 l = rule_str;
1009 for (;;) {
1010 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 if (ch == '\0') {
1013 break; /* done */
1014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 if (in_group) {
1017 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 if (*tail_p) {
1019 (*tail_p)->in_group = 0;
1020 }
1021 in_group = 0;
1022 l++;
1023 continue;
1024 }
David Benjamin37d92462014-09-20 17:54:24 -04001025
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (ch == '|') {
1027 rule = CIPHER_ADD;
1028 l++;
1029 continue;
1030 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1031 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001032 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001033 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 break;
1035 } else {
1036 rule = CIPHER_ADD;
1037 }
1038 } else if (ch == '-') {
1039 rule = CIPHER_DEL;
1040 l++;
1041 } else if (ch == '+') {
1042 rule = CIPHER_ORD;
1043 l++;
1044 } else if (ch == '!') {
1045 rule = CIPHER_KILL;
1046 l++;
1047 } else if (ch == '@') {
1048 rule = CIPHER_SPECIAL;
1049 l++;
1050 } else if (ch == '[') {
1051 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001053 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 break;
1055 }
1056 in_group = 1;
1057 has_group = 1;
1058 l++;
1059 continue;
1060 } else {
1061 rule = CIPHER_ADD;
1062 }
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 /* If preference groups are enabled, the only legal operator is +.
1065 * Otherwise the in_group bits will get mixed up. */
1066 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001067 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001068 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001069 break;
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 if (ITEM_SEP(ch)) {
1073 l++;
1074 continue;
1075 }
Adam Langley95c29f32014-06-20 12:00:00 -07001076
David Benjamin0344daf2015-04-08 02:08:01 -04001077 multi = 0;
1078 cipher_id = 0;
1079 alg_mkey = ~0u;
1080 alg_auth = ~0u;
1081 alg_enc = ~0u;
1082 alg_mac = ~0u;
1083 alg_ssl = ~0u;
1084 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 for (;;) {
1087 ch = *l;
1088 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001089 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1091 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1092 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001093 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
David Benjamin0344daf2015-04-08 02:08:01 -04001096 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 /* We hit something we cannot deal with, it is no command or separator
1098 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001099 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001100 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 l++;
1102 break;
1103 }
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 break;
1107 }
David Benjamin0344daf2015-04-08 02:08:01 -04001108
1109 /* Look for a matching exact cipher. These aren't allowed in multipart
1110 * rules. */
1111 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001112 for (j = 0; j < kCiphersLen; j++) {
1113 const SSL_CIPHER *cipher = &kCiphers[j];
1114 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001115 cipher_id = cipher->id;
1116 break;
1117 }
1118 }
1119 }
1120 if (cipher_id == 0) {
1121 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001122 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001123 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1124 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1125 alg_auth &= kCipherAliases[j].algorithm_auth;
1126 alg_enc &= kCipherAliases[j].algorithm_enc;
1127 alg_mac &= kCipherAliases[j].algorithm_mac;
1128 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1129 algo_strength &= kCipherAliases[j].algo_strength;
1130 break;
1131 }
1132 }
David Benjamina1c90a52015-05-30 17:03:14 -04001133 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001134 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1135 }
1136 }
1137
1138 /* Check for a multipart rule. */
1139 if (ch != '+') {
1140 break;
1141 }
1142 l++;
1143 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 }
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 /* Ok, we have the rule, now apply it. */
1147 if (rule == CIPHER_SPECIAL) {
1148 /* special command */
1149 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001150 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 ok = ssl_cipher_strength_sort(head_p, tail_p);
1152 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001153 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 }
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (ok == 0) {
1157 retval = 0;
1158 }
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 /* We do not support any "multi" options together with "@", so throw away
1161 * the rest of the command, if any left, until end or ':' is found. */
1162 while (*l != '\0' && !ITEM_SEP(*l)) {
1163 l++;
1164 }
David Benjamin0344daf2015-04-08 02:08:01 -04001165 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1167 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1168 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 }
1170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001173 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 retval = 0;
1175 }
Adam Langley95c29f32014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 return retval;
1178}
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180STACK_OF(SSL_CIPHER) *
1181ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001182 struct ssl_cipher_preference_list_st **out_cipher_list,
1183 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1184 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001185 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1187 const char *rule_p;
1188 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 uint8_t *in_group_flags = NULL;
1190 unsigned int num_in_group_flags = 0;
1191 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001194 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 return NULL;
1196 }
David Benjamin5213df42014-08-20 14:19:54 -04001197
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 /* Now we have to collect the available ciphers from the compiled in ciphers.
1199 * We cannot get more than the number compiled in, so it is used for
1200 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001201 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001202 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001203 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 return NULL;
1205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
David Benjamina1c90a52015-05-30 17:03:14 -04001207 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001208
Adam Langleyfcf25832014-12-18 17:42:32 -08001209 /* Now arrange all ciphers by preference:
1210 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1213 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001214 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1215 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001216 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001218 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1219 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1222 * CHACHA20 unless there is hardware support for fast and constant-time
1223 * AES_GCM. */
1224 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001225 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001227 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1228 -1, 0, &head, &tail);
1229 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1230 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001232 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1233 CIPHER_ADD, -1, 0, &head, &tail);
1234 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001235 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001236 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1237 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 }
Adam Langley95c29f32014-06-20 12:00:00 -07001239
Adam Langleyfcf25832014-12-18 17:42:32 -08001240 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1241 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001242 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1243 0, &head, &tail);
1244 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1245 0, &head, &tail);
1246 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1247 -1, 0, &head, &tail);
1248 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1249 0, &head, &tail);
1250 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001254 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1255 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001256
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001258 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1259 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001262 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1263 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 /* If the rule_string begins with DEFAULT, apply the default rule before
1266 * using the (possibly available) additional rules. */
1267 ok = 1;
1268 rule_p = rule_str;
1269 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001270 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1271 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 rule_p += 7;
1273 if (*rule_p == ':') {
1274 rule_p++;
1275 }
1276 }
Adam Langley858a88d2014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001279 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 }
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 if (!ok) {
1283 goto err;
1284 }
1285
1286 /* Allocate new "cipherstack" for the result, return with error
1287 * if we cannot get one. */
1288 cipherstack = sk_SSL_CIPHER_new_null();
1289 if (cipherstack == NULL) {
1290 goto err;
1291 }
1292
David Benjamina1c90a52015-05-30 17:03:14 -04001293 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001294 if (!in_group_flags) {
1295 goto err;
1296 }
1297
1298 /* The cipher selection for the list is done. The ciphers are added
1299 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1300 for (curr = head; curr != NULL; curr = curr->next) {
1301 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001302 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1303 goto err;
1304 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 in_group_flags[num_in_group_flags++] = curr->in_group;
1306 }
1307 }
1308 OPENSSL_free(co_list); /* Not needed any longer */
1309 co_list = NULL;
1310
1311 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1312 if (tmp_cipher_list == NULL) {
1313 goto err;
1314 }
1315 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1316 if (!pref_list) {
1317 goto err;
1318 }
1319 pref_list->ciphers = cipherstack;
1320 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1321 if (!pref_list->in_group_flags) {
1322 goto err;
1323 }
1324 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1325 OPENSSL_free(in_group_flags);
1326 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001327 if (*out_cipher_list != NULL) {
1328 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 }
David Benjamin71f07942015-04-08 02:36:59 -04001330 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 pref_list = NULL;
1332
David Benjamin71f07942015-04-08 02:36:59 -04001333 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001334 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001335 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001337 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1338 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001339
David Benjamin71f07942015-04-08 02:36:59 -04001340 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 } else {
1342 sk_SSL_CIPHER_free(tmp_cipher_list);
1343 tmp_cipher_list = NULL;
1344 }
1345
1346 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001347
1348err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001349 OPENSSL_free(co_list);
1350 OPENSSL_free(in_group_flags);
1351 sk_SSL_CIPHER_free(cipherstack);
1352 sk_SSL_CIPHER_free(tmp_cipher_list);
1353 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 OPENSSL_free(pref_list->in_group_flags);
1355 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001356 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 return NULL;
1358}
Adam Langley95c29f32014-06-20 12:00:00 -07001359
David Benjamin71f07942015-04-08 02:36:59 -04001360uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1361
David Benjamina1c90a52015-05-30 17:03:14 -04001362uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1363 uint32_t id = cipher->id;
1364 /* All ciphers are SSLv3. */
1365 assert((id & 0xff000000) == 0x03000000);
1366 return id & 0xffff;
1367}
1368
David Benjamin71f07942015-04-08 02:36:59 -04001369int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1370 return (cipher->algorithm_enc & SSL_AES) != 0;
1371}
1372
1373int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1374 return (cipher->algorithm_mac & SSL_MD5) != 0;
1375}
1376
1377int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1378 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1379}
1380
1381int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1382 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1383}
1384
1385/* return the actual cipher being used */
1386const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1387 if (cipher != NULL) {
1388 return cipher->name;
1389 }
1390
1391 return "(NONE)";
1392}
1393
1394const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1395 if (cipher == NULL) {
1396 return "";
1397 }
1398
1399 switch (cipher->algorithm_mkey) {
1400 case SSL_kRSA:
1401 return "RSA";
1402
1403 case SSL_kDHE:
1404 switch (cipher->algorithm_auth) {
1405 case SSL_aRSA:
1406 return "DHE_RSA";
1407 default:
1408 assert(0);
1409 return "UNKNOWN";
1410 }
1411
1412 case SSL_kECDHE:
1413 switch (cipher->algorithm_auth) {
1414 case SSL_aECDSA:
1415 return "ECDHE_ECDSA";
1416 case SSL_aRSA:
1417 return "ECDHE_RSA";
1418 case SSL_aPSK:
1419 return "ECDHE_PSK";
1420 default:
1421 assert(0);
1422 return "UNKNOWN";
1423 }
1424
1425 case SSL_kPSK:
1426 assert(cipher->algorithm_auth == SSL_aPSK);
1427 return "PSK";
1428
1429 default:
1430 assert(0);
1431 return "UNKNOWN";
1432 }
1433}
1434
1435static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1436 switch (cipher->algorithm_enc) {
1437 case SSL_3DES:
1438 return "3DES_EDE_CBC";
1439 case SSL_RC4:
1440 return "RC4";
1441 case SSL_AES128:
1442 return "AES_128_CBC";
1443 case SSL_AES256:
1444 return "AES_256_CBC";
1445 case SSL_AES128GCM:
1446 return "AES_128_GCM";
1447 case SSL_AES256GCM:
1448 return "AES_256_GCM";
1449 case SSL_CHACHA20POLY1305:
1450 return "CHACHA20_POLY1305";
1451 break;
1452 default:
1453 assert(0);
1454 return "UNKNOWN";
1455 }
1456}
1457
1458static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjamin9a980ab2015-05-27 00:36:11 -04001459 if ((cipher->algorithm2 & SSL_HANDSHAKE_MAC_DEFAULT) ==
1460 SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjamin71f07942015-04-08 02:36:59 -04001461 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1462 * only ever MD5 or SHA-1. */
1463 switch (cipher->algorithm_mac) {
1464 case SSL_MD5:
1465 return "MD5";
1466 case SSL_SHA1:
1467 return "SHA";
1468 default:
1469 assert(0);
1470 return "UNKNOWN";
1471 }
David Benjamin9a980ab2015-05-27 00:36:11 -04001472 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA256) {
David Benjamin71f07942015-04-08 02:36:59 -04001473 return "SHA256";
David Benjamin9a980ab2015-05-27 00:36:11 -04001474 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA384) {
David Benjamin71f07942015-04-08 02:36:59 -04001475 return "SHA384";
1476 } else {
1477 assert(0);
1478 return "UNKNOWN";
1479 }
1480}
1481
1482char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1483 if (cipher == NULL) {
1484 return NULL;
1485 }
1486
1487 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1488 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1489 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1490
1491 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1492 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1493 strlen(prf_name) + 1;
1494 char *ret = OPENSSL_malloc(len);
1495 if (ret == NULL) {
1496 return NULL;
1497 }
1498 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1499 BUF_strlcat(ret, kx_name, len) >= len ||
1500 BUF_strlcat(ret, "_WITH_", len) >= len ||
1501 BUF_strlcat(ret, enc_name, len) >= len ||
1502 BUF_strlcat(ret, "_", len) >= len ||
1503 BUF_strlcat(ret, prf_name, len) >= len) {
1504 assert(0);
1505 OPENSSL_free(ret);
1506 return NULL;
1507 }
1508 assert(strlen(ret) + 1 == len);
1509 return ret;
1510}
1511
1512int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1513 if (cipher == NULL) {
1514 return 0;
1515 }
1516
1517 if (out_alg_bits != NULL) {
1518 *out_alg_bits = cipher->alg_bits;
1519 }
1520 return cipher->strength_bits;
1521}
1522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1524 int len) {
1525 const char *ver;
1526 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001527 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 alg_mkey = cipher->algorithm_mkey;
1531 alg_auth = cipher->algorithm_auth;
1532 alg_enc = cipher->algorithm_enc;
1533 alg_mac = cipher->algorithm_mac;
1534 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 if (alg_ssl & SSL_SSLV3) {
1537 ver = "SSLv3";
1538 } else if (alg_ssl & SSL_TLSV1_2) {
1539 ver = "TLSv1.2";
1540 } else {
1541 ver = "unknown";
1542 }
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 switch (alg_mkey) {
1545 case SSL_kRSA:
1546 kx = "RSA";
1547 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001548
David Benjamin7061e282015-03-19 11:10:48 -04001549 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 kx = "DH";
1551 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamin7061e282015-03-19 11:10:48 -04001553 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 kx = "ECDH";
1555 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 case SSL_kPSK:
1558 kx = "PSK";
1559 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 default:
1562 kx = "unknown";
1563 }
Adam Langley95c29f32014-06-20 12:00:00 -07001564
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 switch (alg_auth) {
1566 case SSL_aRSA:
1567 au = "RSA";
1568 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001569
Adam Langleyfcf25832014-12-18 17:42:32 -08001570 case SSL_aECDSA:
1571 au = "ECDSA";
1572 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574 case SSL_aPSK:
1575 au = "PSK";
1576 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001577
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 default:
1579 au = "unknown";
1580 break;
1581 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001582
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 switch (alg_enc) {
1584 case SSL_3DES:
1585 enc = "3DES(168)";
1586 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001587
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 case SSL_RC4:
1589 enc = "RC4(128)";
1590 break;
1591
1592 case SSL_AES128:
1593 enc = "AES(128)";
1594 break;
1595
1596 case SSL_AES256:
1597 enc = "AES(256)";
1598 break;
1599
1600 case SSL_AES128GCM:
1601 enc = "AESGCM(128)";
1602 break;
1603
1604 case SSL_AES256GCM:
1605 enc = "AESGCM(256)";
1606 break;
1607
1608 case SSL_CHACHA20POLY1305:
1609 enc = "ChaCha20-Poly1305";
1610 break;
1611
1612 default:
1613 enc = "unknown";
1614 break;
1615 }
1616
1617 switch (alg_mac) {
1618 case SSL_MD5:
1619 mac = "MD5";
1620 break;
1621
1622 case SSL_SHA1:
1623 mac = "SHA1";
1624 break;
1625
1626 case SSL_SHA256:
1627 mac = "SHA256";
1628 break;
1629
1630 case SSL_SHA384:
1631 mac = "SHA384";
1632 break;
1633
1634 case SSL_AEAD:
1635 mac = "AEAD";
1636 break;
1637
1638 default:
1639 mac = "unknown";
1640 break;
1641 }
1642
1643 if (buf == NULL) {
1644 len = 128;
1645 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001646 if (buf == NULL) {
1647 return NULL;
1648 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001649 } else if (len < 128) {
1650 return "Buffer too small";
1651 }
1652
1653 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1654 return buf;
1655}
1656
David Benjamin71f07942015-04-08 02:36:59 -04001657const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1658 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001659}
1660
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001661COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001662
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001663int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001664
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001665const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001666
David Benjamind1d80782015-07-05 11:54:09 -04001667int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001668 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001671 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001673 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675
David Benjamind1d80782015-07-05 11:54:09 -04001676 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001677}
David Benjamin9c651c92014-07-12 13:27:45 -04001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001680 /* PSK-authenticated ciphers do not use a public key, except for
1681 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 if ((cipher->algorithm_auth & SSL_aPSK) &&
1683 !(cipher->algorithm_mkey & SSL_kRSA)) {
1684 return 0;
1685 }
1686
1687 /* All other ciphers include it. */
1688 return 1;
1689}
1690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1692 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001693 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 return 1;
1695 }
1696
1697 /* It is optional in all others. */
1698 return 0;
1699}