blob: deecce73218a27ae39dea021491aa7705ed4ed7b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin71f07942015-04-08 02:36:59 -0400145#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400146#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500147#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400150#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamina1c90a52015-05-30 17:03:14 -0400155/* kCiphers is an array of all supported ciphers, sorted by id. */
156const SSL_CIPHER kCiphers[] = {
157 /* The RSA ciphers */
158 /* Cipher 04 */
159 {
160 SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
161 SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
162 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
163 },
164
165 /* Cipher 05 */
166 {
167 SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
168 SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
169 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
170 },
171
172 /* Cipher 0A */
173 {
174 SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
175 SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
176 SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
177 },
178
179
180 /* New AES ciphersuites */
181
182 /* Cipher 2F */
183 {
184 TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
185 SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
186 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
187 },
188
189 /* Cipher 33 */
190 {
191 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
192 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
193 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
194 },
195
196 /* Cipher 35 */
197 {
198 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
199 SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
200 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
201 },
202
203 /* Cipher 39 */
204 {
205 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
206 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
207 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
208 },
209
210
211 /* TLS v1.2 ciphersuites */
212
213 /* Cipher 3C */
214 {
215 TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
216 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
217 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
218 },
219
220 /* Cipher 3D */
221 {
222 TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
223 SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
224 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
225 },
226
227 /* Cipher 67 */
228 {
229 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
230 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
231 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
232 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
233 },
234
235 /* Cipher 6B */
236 {
237 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
238 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
239 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
240 SSL_HANDSHAKE_MAC_SHA256, 256, 256,
241 },
242
Adam Langley85bc5602015-06-09 09:54:04 -0700243 /* PSK cipher suites. */
244
David Benjamina1c90a52015-05-30 17:03:14 -0400245 /* Cipher 8A */
246 {
247 TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
248 SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
249 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
250 },
251
252 /* Cipher 8C */
253 {
254 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
255 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
256 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
257 },
258
259 /* Cipher 8D */
260 {
261 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
262 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
263 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
264 },
265
David Benjamina1c90a52015-05-30 17:03:14 -0400266 /* GCM ciphersuites from RFC5288 */
267
268 /* Cipher 9C */
269 {
270 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
271 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
272 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
273 SSL_HANDSHAKE_MAC_SHA256 |
274 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
275 128, 128,
276 },
277
278 /* Cipher 9D */
279 {
280 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
281 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
282 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
283 SSL_HANDSHAKE_MAC_SHA384 |
284 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
285 256, 256,
286 },
287
288 /* Cipher 9E */
289 {
290 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
291 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
292 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
293 SSL_HANDSHAKE_MAC_SHA256 |
294 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
295 128, 128,
296 },
297
298 /* Cipher 9F */
299 {
300 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
301 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
302 SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
303 SSL_HANDSHAKE_MAC_SHA384 |
304 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
305 256, 256,
306 },
307
308 /* Cipher C007 */
309 {
310 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
311 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
312 SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
313 128,
314 },
315
316 /* Cipher C009 */
317 {
318 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
319 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
320 SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
321 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
322 },
323
324 /* Cipher C00A */
325 {
326 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
327 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
328 SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
329 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
330 },
331
332 /* Cipher C011 */
333 {
334 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
335 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
336 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
337 },
338
339 /* Cipher C013 */
340 {
341 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
342 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
343 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
344 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
345 },
346
347 /* Cipher C014 */
348 {
349 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
350 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
351 SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
352 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
353 },
354
355
356 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
357
358 /* Cipher C023 */
359 {
360 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
361 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
362 SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
363 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
364 },
365
366 /* Cipher C024 */
367 {
368 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
369 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
370 SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
371 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
372 },
373
374 /* Cipher C027 */
375 {
376 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
377 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
378 SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
379 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
380 },
381
382 /* Cipher C028 */
383 {
384 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
385 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
386 SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
387 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
388 },
389
390
391 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
392
393 /* Cipher C02B */
394 {
395 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
396 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
397 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
398 SSL_HANDSHAKE_MAC_SHA256 |
399 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
400 128, 128,
401 },
402
403 /* Cipher C02C */
404 {
405 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
406 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
407 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
408 SSL_HANDSHAKE_MAC_SHA384 |
409 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
410 256, 256,
411 },
412
413 /* Cipher C02F */
414 {
415 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
416 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
417 SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
418 SSL_HANDSHAKE_MAC_SHA256 |
419 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
420 128, 128,
421 },
422
423 /* Cipher C030 */
424 {
425 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
426 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
427 SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
428 SSL_HANDSHAKE_MAC_SHA384 |
429 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
430 256, 256,
431 },
432
Adam Langley85bc5602015-06-09 09:54:04 -0700433 /* ECDHE-PSK cipher suites. */
434
435 /* Cipher C035 */
436 {
437 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
438 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
439 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
440 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
441 },
442
443 /* Cipher C036 */
444 {
445 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
446 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
447 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
448 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
449 },
450
451 /* ChaCha20-Poly1305 cipher suites. */
452
David Benjamina1c90a52015-05-30 17:03:14 -0400453 {
454 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
455 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
456 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
457 SSL_HANDSHAKE_MAC_SHA256,
458 256, 0,
459 },
460
461 {
462 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
463 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
464 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
465 SSL_HANDSHAKE_MAC_SHA256,
466 256, 0,
467 },
468
469 {
470 TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
471 TLS1_CK_DHE_RSA_CHACHA20_POLY1305, SSL_kDHE, SSL_aRSA,
472 SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
473 SSL_HANDSHAKE_MAC_SHA256,
474 256, 0,
475 },
476};
477
478static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400481 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400483};
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
486 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
487 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
488 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
489 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
490};
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492#define CIPHER_ADD 1
493#define CIPHER_KILL 2
494#define CIPHER_DEL 3
495#define CIPHER_ORD 4
496#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498typedef struct cipher_order_st {
499 const SSL_CIPHER *cipher;
500 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 int in_group;
502 struct cipher_order_st *next, *prev;
503} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin0344daf2015-04-08 02:08:01 -0400505typedef struct cipher_alias_st {
506 /* name is the name of the cipher alias. */
507 const char *name;
508
509 /* The following fields are bitmasks for the corresponding fields on
510 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
511 * bit corresponding to the cipher's value is set to 1. If any bitmask is
512 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
513 uint32_t algorithm_mkey;
514 uint32_t algorithm_auth;
515 uint32_t algorithm_enc;
516 uint32_t algorithm_mac;
517 uint32_t algorithm_ssl;
518 uint32_t algo_strength;
519} CIPHER_ALIAS;
520
David Benjamina1c90a52015-05-30 17:03:14 -0400521static const CIPHER_ALIAS kCipherAliases[] = {
522 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamina1c90a52015-05-30 17:03:14 -0400524 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700525
David Benjamina1c90a52015-05-30 17:03:14 -0400526 /* key exchange aliases
527 * (some of those using only a single bit here combine
528 * multiple key exchange algs according to the RFCs,
529 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
530 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamina1c90a52015-05-30 17:03:14 -0400532 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
533 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
534 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamina1c90a52015-05-30 17:03:14 -0400536 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
537 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
538 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamina1c90a52015-05-30 17:03:14 -0400540 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamina1c90a52015-05-30 17:03:14 -0400542 /* server authentication aliases */
543 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
544 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
545 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
546 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamina1c90a52015-05-30 17:03:14 -0400548 /* aliases combining key exchange and server authentication */
549 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
550 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
551 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
552 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
553 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
554 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamina1c90a52015-05-30 17:03:14 -0400556 /* symmetric encryption aliases */
557 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
558 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
559 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
560 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
561 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
562 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
563 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjamina1c90a52015-05-30 17:03:14 -0400565 /* MAC aliases */
566 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
567 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
568 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
569 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
570 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamina1c90a52015-05-30 17:03:14 -0400572 /* protocol version aliases */
573 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
574 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
575 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamina1c90a52015-05-30 17:03:14 -0400577 /* strength classes */
578 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
579 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
580 /* FIPS 140-2 approved ciphersuite */
581 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800582};
Adam Langley95c29f32014-06-20 12:00:00 -0700583
David Benjamina1c90a52015-05-30 17:03:14 -0400584static const size_t kCipherAliasesLen =
585 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
586
587static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
588 const SSL_CIPHER *a = in_a;
589 const SSL_CIPHER *b = in_b;
590
591 if (a->id > b->id) {
592 return 1;
593 } else if (a->id < b->id) {
594 return -1;
595 } else {
596 return 0;
597 }
598}
599
600static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
601 return ssl_cipher_id_cmp(*a, *b);
602}
603
604const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
605 SSL_CIPHER c;
606
607 c.id = 0x03000000L | value;
608 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
609 ssl_cipher_id_cmp);
610}
David Benjamin0344daf2015-04-08 02:08:01 -0400611
David Benjaminea72bd02014-12-21 21:27:41 -0500612int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
613 size_t *out_mac_secret_len,
614 size_t *out_fixed_iv_len,
615 const SSL_CIPHER *cipher, uint16_t version) {
616 *out_aead = NULL;
617 *out_mac_secret_len = 0;
618 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700619
David Benjaminea72bd02014-12-21 21:27:41 -0500620 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500622 *out_aead = EVP_aead_aes_128_gcm();
623 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 return 1;
625
626 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500627 *out_aead = EVP_aead_aes_256_gcm();
628 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 return 1;
630
631 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500632 *out_aead = EVP_aead_chacha20_poly1305();
633 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 return 1;
635
636 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500637 switch (cipher->algorithm_mac) {
638 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500639 if (version == SSL3_VERSION) {
640 *out_aead = EVP_aead_rc4_md5_ssl3();
641 } else {
642 *out_aead = EVP_aead_rc4_md5_tls();
643 }
David Benjaminea72bd02014-12-21 21:27:41 -0500644 *out_mac_secret_len = MD5_DIGEST_LENGTH;
645 return 1;
646 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500647 if (version == SSL3_VERSION) {
648 *out_aead = EVP_aead_rc4_sha1_ssl3();
649 } else {
650 *out_aead = EVP_aead_rc4_sha1_tls();
651 }
David Benjaminea72bd02014-12-21 21:27:41 -0500652 *out_mac_secret_len = SHA_DIGEST_LENGTH;
653 return 1;
654 default:
655 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800657
David Benjaminea72bd02014-12-21 21:27:41 -0500658 case SSL_AES128:
659 switch (cipher->algorithm_mac) {
660 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500661 if (version == SSL3_VERSION) {
662 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
663 *out_fixed_iv_len = 16;
664 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500665 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
666 *out_fixed_iv_len = 16;
667 } else {
668 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
669 }
670 *out_mac_secret_len = SHA_DIGEST_LENGTH;
671 return 1;
672 case SSL_SHA256:
673 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
674 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
675 return 1;
676 default:
677 return 0;
678 }
679
680 case SSL_AES256:
681 switch (cipher->algorithm_mac) {
682 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500683 if (version == SSL3_VERSION) {
684 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
685 *out_fixed_iv_len = 16;
686 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500687 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
688 *out_fixed_iv_len = 16;
689 } else {
690 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
691 }
692 *out_mac_secret_len = SHA_DIGEST_LENGTH;
693 return 1;
694 case SSL_SHA256:
695 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
696 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
697 return 1;
698 case SSL_SHA384:
699 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
700 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
701 return 1;
702 default:
703 return 0;
704 }
705
706 case SSL_3DES:
707 switch (cipher->algorithm_mac) {
708 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500709 if (version == SSL3_VERSION) {
710 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
711 *out_fixed_iv_len = 8;
712 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500713 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
714 *out_fixed_iv_len = 8;
715 } else {
716 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
717 }
718 *out_mac_secret_len = SHA_DIGEST_LENGTH;
719 return 1;
720 default:
721 return 0;
722 }
723
724 default:
725 return 0;
726 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800727}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700728
David Benjamin71f07942015-04-08 02:36:59 -0400729int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
730 size_t idx) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500731 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 return 0;
733 }
David Benjamin71f07942015-04-08 02:36:59 -0400734 *out_mask = ssl_handshake_digests[idx].mask;
735 *out_md = ssl_handshake_digests[idx].md_func();
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700737}
738
739#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700741
David Benjamin0344daf2015-04-08 02:08:01 -0400742/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
743 * |buf_len| bytes at |buf|. */
744static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
745 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
746 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
747}
748
Adam Langley95c29f32014-06-20 12:00:00 -0700749static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 CIPHER_ORDER **tail) {
751 if (curr == *tail) {
752 return;
753 }
754 if (curr == *head) {
755 *head = curr->next;
756 }
757 if (curr->prev != NULL) {
758 curr->prev->next = curr->next;
759 }
760 if (curr->next != NULL) {
761 curr->next->prev = curr->prev;
762 }
763 (*tail)->next = curr;
764 curr->prev = *tail;
765 curr->next = NULL;
766 *tail = curr;
767}
Adam Langley95c29f32014-06-20 12:00:00 -0700768
769static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 CIPHER_ORDER **tail) {
771 if (curr == *head) {
772 return;
773 }
774 if (curr == *tail) {
775 *tail = curr->prev;
776 }
777 if (curr->next != NULL) {
778 curr->next->prev = curr->prev;
779 }
780 if (curr->prev != NULL) {
781 curr->prev->next = curr->next;
782 }
783 (*head)->prev = curr;
784 curr->next = *head;
785 curr->prev = NULL;
786 *head = curr;
787}
Adam Langley95c29f32014-06-20 12:00:00 -0700788
David Benjamin82c9e902014-12-12 15:55:27 -0500789static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 CIPHER_ORDER *co_list,
791 CIPHER_ORDER **head_p,
792 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400793 /* The set of ciphers is static, but some subset may be unsupported by
794 * |ssl_method|, so the list may be smaller. */
795 size_t co_list_num = 0;
796 size_t i;
797 for (i = 0; i < kCiphersLen; i++) {
798 const SSL_CIPHER *cipher = &kCiphers[i];
799 if (ssl_method->supports_cipher(cipher)) {
800 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 co_list[co_list_num].next = NULL;
802 co_list[co_list_num].prev = NULL;
803 co_list[co_list_num].active = 0;
804 co_list[co_list_num].in_group = 0;
805 co_list_num++;
806 }
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 /* Prepare linked list from list entries. */
810 if (co_list_num > 0) {
811 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 if (co_list_num > 1) {
814 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 for (i = 1; i < co_list_num - 1; i++) {
817 co_list[i].prev = &co_list[i - 1];
818 co_list[i].next = &co_list[i + 1];
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
822 }
823
824 co_list[co_list_num - 1].next = NULL;
825
826 *head_p = &co_list[0];
827 *tail_p = &co_list[co_list_num - 1];
828 }
829}
Adam Langley95c29f32014-06-20 12:00:00 -0700830
David Benjamin0344daf2015-04-08 02:08:01 -0400831/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
832 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
833 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
834 *
835 * - If |cipher_id| is non-zero, only that cipher is selected.
836 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
837 * of that strength.
838 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
839 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800840static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400841 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
842 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
843 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
845 CIPHER_ORDER *head, *tail, *curr, *next, *last;
846 const SSL_CIPHER *cp;
847 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700848
David Benjamin0344daf2015-04-08 02:08:01 -0400849 if (cipher_id == 0 && strength_bits == -1 &&
850 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
851 alg_ssl == 0 || algo_strength == 0)) {
852 /* The rule matches nothing, so bail early. */
853 return;
854 }
855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (rule == CIPHER_DEL) {
857 /* needed to maintain sorting between currently deleted ciphers */
858 reverse = 1;
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 head = *head_p;
862 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 if (reverse) {
865 next = tail;
866 last = head;
867 } else {
868 next = head;
869 last = tail;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 curr = NULL;
873 for (;;) {
874 if (curr == last) {
875 break;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 curr = next;
879 if (curr == NULL) {
880 break;
881 }
Adam Langleye3142a72014-07-24 17:56:48 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 next = reverse ? curr->prev : curr->next;
884 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700885
David Benjamin0344daf2015-04-08 02:08:01 -0400886 /* Selection criteria is either a specific cipher, the value of
887 * |strength_bits|, or the algorithms used. */
888 if (cipher_id != 0) {
889 if (cipher_id != cp->id) {
890 continue;
891 }
892 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 if (strength_bits != cp->strength_bits) {
894 continue;
895 }
David Benjamin0344daf2015-04-08 02:08:01 -0400896 } else if (!(alg_mkey & cp->algorithm_mkey) ||
897 !(alg_auth & cp->algorithm_auth) ||
898 !(alg_enc & cp->algorithm_enc) ||
899 !(alg_mac & cp->algorithm_mac) ||
900 !(alg_ssl & cp->algorithm_ssl) ||
901 !(algo_strength & cp->algo_strength)) {
902 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 }
Adam Langleye3142a72014-07-24 17:56:48 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* add the cipher if it has not been added yet. */
906 if (rule == CIPHER_ADD) {
907 /* reverse == 0 */
908 if (!curr->active) {
909 ll_append_tail(&head, curr, &tail);
910 curr->active = 1;
911 curr->in_group = in_group;
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 /* Move the added cipher to this location */
916 else if (rule == CIPHER_ORD) {
917 /* reverse == 0 */
918 if (curr->active) {
919 ll_append_tail(&head, curr, &tail);
920 curr->in_group = 0;
921 }
922 } else if (rule == CIPHER_DEL) {
923 /* reverse == 1 */
924 if (curr->active) {
925 /* most recently deleted ciphersuites get best positions
926 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
927 * works in reverse to maintain the order) */
928 ll_append_head(&head, curr, &tail);
929 curr->active = 0;
930 curr->in_group = 0;
931 }
932 } else if (rule == CIPHER_KILL) {
933 /* reverse == 0 */
934 if (head == curr) {
935 head = curr->next;
936 } else {
937 curr->prev->next = curr->next;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 if (tail == curr) {
941 tail = curr->prev;
942 }
943 curr->active = 0;
944 if (curr->next != NULL) {
945 curr->next->prev = curr->prev;
946 }
947 if (curr->prev != NULL) {
948 curr->prev->next = curr->next;
949 }
950 curr->next = NULL;
951 curr->prev = NULL;
952 }
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 *head_p = head;
956 *tail_p = tail;
957}
Adam Langley95c29f32014-06-20 12:00:00 -0700958
959static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 CIPHER_ORDER **tail_p) {
961 int max_strength_bits, i, *number_uses;
962 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 /* This routine sorts the ciphers with descending strength. The sorting must
965 * keep the pre-sorted sequence, so we apply the normal sorting routine as
966 * '+' movement to the end of the list. */
967 max_strength_bits = 0;
968 curr = *head_p;
969 while (curr != NULL) {
970 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
971 max_strength_bits = curr->cipher->strength_bits;
972 }
973 curr = curr->next;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
977 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400978 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 return 0;
980 }
981 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 /* Now find the strength_bits values actually used. */
984 curr = *head_p;
985 while (curr != NULL) {
986 if (curr->active) {
987 number_uses[curr->cipher->strength_bits]++;
988 }
989 curr = curr->next;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 /* Go through the list of used strength_bits values in descending order. */
993 for (i = max_strength_bits; i >= 0; i--) {
994 if (number_uses[i] > 0) {
995 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
996 tail_p);
997 }
998 }
999
1000 OPENSSL_free(number_uses);
1001 return 1;
1002}
Adam Langley95c29f32014-06-20 12:00:00 -07001003
David Benjamin0344daf2015-04-08 02:08:01 -04001004static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1005 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001007 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -04001008 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -04001010 int multi, rule, retval, ok, in_group = 0, has_group = 0;
1011 size_t j, buf_len;
1012 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 retval = 1;
1016 l = rule_str;
1017 for (;;) {
1018 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 if (ch == '\0') {
1021 break; /* done */
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 if (in_group) {
1025 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (*tail_p) {
1027 (*tail_p)->in_group = 0;
1028 }
1029 in_group = 0;
1030 l++;
1031 continue;
1032 }
David Benjamin37d92462014-09-20 17:54:24 -04001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 if (ch == '|') {
1035 rule = CIPHER_ADD;
1036 l++;
1037 continue;
1038 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1039 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001040 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001041 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 break;
1043 } else {
1044 rule = CIPHER_ADD;
1045 }
1046 } else if (ch == '-') {
1047 rule = CIPHER_DEL;
1048 l++;
1049 } else if (ch == '+') {
1050 rule = CIPHER_ORD;
1051 l++;
1052 } else if (ch == '!') {
1053 rule = CIPHER_KILL;
1054 l++;
1055 } else if (ch == '@') {
1056 rule = CIPHER_SPECIAL;
1057 l++;
1058 } else if (ch == '[') {
1059 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001060 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001061 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 break;
1063 }
1064 in_group = 1;
1065 has_group = 1;
1066 l++;
1067 continue;
1068 } else {
1069 rule = CIPHER_ADD;
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 /* If preference groups are enabled, the only legal operator is +.
1073 * Otherwise the in_group bits will get mixed up. */
1074 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001075 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001076 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 break;
1078 }
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 if (ITEM_SEP(ch)) {
1081 l++;
1082 continue;
1083 }
Adam Langley95c29f32014-06-20 12:00:00 -07001084
David Benjamin0344daf2015-04-08 02:08:01 -04001085 multi = 0;
1086 cipher_id = 0;
1087 alg_mkey = ~0u;
1088 alg_auth = ~0u;
1089 alg_enc = ~0u;
1090 alg_mac = ~0u;
1091 alg_ssl = ~0u;
1092 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -07001093
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 for (;;) {
1095 ch = *l;
1096 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001097 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1099 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1100 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001101 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
David Benjamin0344daf2015-04-08 02:08:01 -04001104 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 /* We hit something we cannot deal with, it is no command or separator
1106 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001107 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001108 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 l++;
1110 break;
1111 }
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 break;
1115 }
David Benjamin0344daf2015-04-08 02:08:01 -04001116
1117 /* Look for a matching exact cipher. These aren't allowed in multipart
1118 * rules. */
1119 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001120 for (j = 0; j < kCiphersLen; j++) {
1121 const SSL_CIPHER *cipher = &kCiphers[j];
1122 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001123 cipher_id = cipher->id;
1124 break;
1125 }
1126 }
1127 }
1128 if (cipher_id == 0) {
1129 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001130 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001131 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1132 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1133 alg_auth &= kCipherAliases[j].algorithm_auth;
1134 alg_enc &= kCipherAliases[j].algorithm_enc;
1135 alg_mac &= kCipherAliases[j].algorithm_mac;
1136 alg_ssl &= kCipherAliases[j].algorithm_ssl;
1137 algo_strength &= kCipherAliases[j].algo_strength;
1138 break;
1139 }
1140 }
David Benjamina1c90a52015-05-30 17:03:14 -04001141 if (j == kCipherAliasesLen) {
David Benjamin0344daf2015-04-08 02:08:01 -04001142 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
1143 }
1144 }
1145
1146 /* Check for a multipart rule. */
1147 if (ch != '+') {
1148 break;
1149 }
1150 l++;
1151 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 /* Ok, we have the rule, now apply it. */
1155 if (rule == CIPHER_SPECIAL) {
1156 /* special command */
1157 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001158 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 ok = ssl_cipher_strength_sort(head_p, tail_p);
1160 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001161 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 }
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 if (ok == 0) {
1165 retval = 0;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 /* We do not support any "multi" options together with "@", so throw away
1169 * the rest of the command, if any left, until end or ':' is found. */
1170 while (*l != '\0' && !ITEM_SEP(*l)) {
1171 l++;
1172 }
David Benjamin0344daf2015-04-08 02:08:01 -04001173 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1175 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1176 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 }
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001181 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 retval = 0;
1183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 return retval;
1186}
Adam Langley95c29f32014-06-20 12:00:00 -07001187
Adam Langleyfcf25832014-12-18 17:42:32 -08001188STACK_OF(SSL_CIPHER) *
1189ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001190 struct ssl_cipher_preference_list_st **out_cipher_list,
1191 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1192 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001193 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1195 const char *rule_p;
1196 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 uint8_t *in_group_flags = NULL;
1198 unsigned int num_in_group_flags = 0;
1199 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001202 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 return NULL;
1204 }
David Benjamin5213df42014-08-20 14:19:54 -04001205
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 /* Now we have to collect the available ciphers from the compiled in ciphers.
1207 * We cannot get more than the number compiled in, so it is used for
1208 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001209 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001211 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 return NULL;
1213 }
Adam Langley95c29f32014-06-20 12:00:00 -07001214
David Benjamina1c90a52015-05-30 17:03:14 -04001215 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001216
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 /* Now arrange all ciphers by preference:
1218 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1221 * key exchange mechanisms */
David Benjamina1c90a52015-05-30 17:03:14 -04001222 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
1223 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001224 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001226 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
1227 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1230 * CHACHA20 unless there is hardware support for fast and constant-time
1231 * AES_GCM. */
1232 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -04001233 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001235 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1236 -1, 0, &head, &tail);
1237 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1238 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 } else {
David Benjamin0344daf2015-04-08 02:08:01 -04001240 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
1241 CIPHER_ADD, -1, 0, &head, &tail);
1242 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -04001244 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
1245 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 }
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1249 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -04001250 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1251 0, &head, &tail);
1252 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
1253 0, &head, &tail);
1254 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
1255 -1, 0, &head, &tail);
1256 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
1257 0, &head, &tail);
1258 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -04001262 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
1263 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001264
Adam Langleyfcf25832014-12-18 17:42:32 -08001265 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -04001266 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
1267 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001268
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -04001270 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
1271 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 /* If the rule_string begins with DEFAULT, apply the default rule before
1274 * using the (possibly available) additional rules. */
1275 ok = 1;
1276 rule_p = rule_str;
1277 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001278 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1279 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 rule_p += 7;
1281 if (*rule_p == ':') {
1282 rule_p++;
1283 }
1284 }
Adam Langley858a88d2014-06-20 12:00:00 -07001285
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001287 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 }
Adam Langley95c29f32014-06-20 12:00:00 -07001289
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 if (!ok) {
1291 goto err;
1292 }
1293
1294 /* Allocate new "cipherstack" for the result, return with error
1295 * if we cannot get one. */
1296 cipherstack = sk_SSL_CIPHER_new_null();
1297 if (cipherstack == NULL) {
1298 goto err;
1299 }
1300
David Benjamina1c90a52015-05-30 17:03:14 -04001301 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 if (!in_group_flags) {
1303 goto err;
1304 }
1305
1306 /* The cipher selection for the list is done. The ciphers are added
1307 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1308 for (curr = head; curr != NULL; curr = curr->next) {
1309 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001310 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1311 goto err;
1312 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 in_group_flags[num_in_group_flags++] = curr->in_group;
1314 }
1315 }
1316 OPENSSL_free(co_list); /* Not needed any longer */
1317 co_list = NULL;
1318
1319 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1320 if (tmp_cipher_list == NULL) {
1321 goto err;
1322 }
1323 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1324 if (!pref_list) {
1325 goto err;
1326 }
1327 pref_list->ciphers = cipherstack;
1328 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1329 if (!pref_list->in_group_flags) {
1330 goto err;
1331 }
1332 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1333 OPENSSL_free(in_group_flags);
1334 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001335 if (*out_cipher_list != NULL) {
1336 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 }
David Benjamin71f07942015-04-08 02:36:59 -04001338 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 pref_list = NULL;
1340
David Benjamin71f07942015-04-08 02:36:59 -04001341 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001342 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001343 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001345 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1346 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001347
David Benjamin71f07942015-04-08 02:36:59 -04001348 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 } else {
1350 sk_SSL_CIPHER_free(tmp_cipher_list);
1351 tmp_cipher_list = NULL;
1352 }
1353
1354 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001355
1356err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001357 OPENSSL_free(co_list);
1358 OPENSSL_free(in_group_flags);
1359 sk_SSL_CIPHER_free(cipherstack);
1360 sk_SSL_CIPHER_free(tmp_cipher_list);
1361 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 OPENSSL_free(pref_list->in_group_flags);
1363 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001364 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 return NULL;
1366}
Adam Langley95c29f32014-06-20 12:00:00 -07001367
David Benjamin71f07942015-04-08 02:36:59 -04001368uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1369
David Benjamina1c90a52015-05-30 17:03:14 -04001370uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1371 uint32_t id = cipher->id;
1372 /* All ciphers are SSLv3. */
1373 assert((id & 0xff000000) == 0x03000000);
1374 return id & 0xffff;
1375}
1376
David Benjamin71f07942015-04-08 02:36:59 -04001377int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1378 return (cipher->algorithm_enc & SSL_AES) != 0;
1379}
1380
1381int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1382 return (cipher->algorithm_mac & SSL_MD5) != 0;
1383}
1384
1385int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1386 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1387}
1388
1389int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1390 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1391}
1392
1393/* return the actual cipher being used */
1394const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1395 if (cipher != NULL) {
1396 return cipher->name;
1397 }
1398
1399 return "(NONE)";
1400}
1401
1402const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1403 if (cipher == NULL) {
1404 return "";
1405 }
1406
1407 switch (cipher->algorithm_mkey) {
1408 case SSL_kRSA:
1409 return "RSA";
1410
1411 case SSL_kDHE:
1412 switch (cipher->algorithm_auth) {
1413 case SSL_aRSA:
1414 return "DHE_RSA";
1415 default:
1416 assert(0);
1417 return "UNKNOWN";
1418 }
1419
1420 case SSL_kECDHE:
1421 switch (cipher->algorithm_auth) {
1422 case SSL_aECDSA:
1423 return "ECDHE_ECDSA";
1424 case SSL_aRSA:
1425 return "ECDHE_RSA";
1426 case SSL_aPSK:
1427 return "ECDHE_PSK";
1428 default:
1429 assert(0);
1430 return "UNKNOWN";
1431 }
1432
1433 case SSL_kPSK:
1434 assert(cipher->algorithm_auth == SSL_aPSK);
1435 return "PSK";
1436
1437 default:
1438 assert(0);
1439 return "UNKNOWN";
1440 }
1441}
1442
1443static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1444 switch (cipher->algorithm_enc) {
1445 case SSL_3DES:
1446 return "3DES_EDE_CBC";
1447 case SSL_RC4:
1448 return "RC4";
1449 case SSL_AES128:
1450 return "AES_128_CBC";
1451 case SSL_AES256:
1452 return "AES_256_CBC";
1453 case SSL_AES128GCM:
1454 return "AES_128_GCM";
1455 case SSL_AES256GCM:
1456 return "AES_256_GCM";
1457 case SSL_CHACHA20POLY1305:
1458 return "CHACHA20_POLY1305";
1459 break;
1460 default:
1461 assert(0);
1462 return "UNKNOWN";
1463 }
1464}
1465
1466static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjamin9a980ab2015-05-27 00:36:11 -04001467 if ((cipher->algorithm2 & SSL_HANDSHAKE_MAC_DEFAULT) ==
1468 SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjamin71f07942015-04-08 02:36:59 -04001469 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1470 * only ever MD5 or SHA-1. */
1471 switch (cipher->algorithm_mac) {
1472 case SSL_MD5:
1473 return "MD5";
1474 case SSL_SHA1:
1475 return "SHA";
1476 default:
1477 assert(0);
1478 return "UNKNOWN";
1479 }
David Benjamin9a980ab2015-05-27 00:36:11 -04001480 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA256) {
David Benjamin71f07942015-04-08 02:36:59 -04001481 return "SHA256";
David Benjamin9a980ab2015-05-27 00:36:11 -04001482 } else if (cipher->algorithm2 & SSL_HANDSHAKE_MAC_SHA384) {
David Benjamin71f07942015-04-08 02:36:59 -04001483 return "SHA384";
1484 } else {
1485 assert(0);
1486 return "UNKNOWN";
1487 }
1488}
1489
1490char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1491 if (cipher == NULL) {
1492 return NULL;
1493 }
1494
1495 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1496 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1497 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1498
1499 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1500 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1501 strlen(prf_name) + 1;
1502 char *ret = OPENSSL_malloc(len);
1503 if (ret == NULL) {
1504 return NULL;
1505 }
1506 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1507 BUF_strlcat(ret, kx_name, len) >= len ||
1508 BUF_strlcat(ret, "_WITH_", len) >= len ||
1509 BUF_strlcat(ret, enc_name, len) >= len ||
1510 BUF_strlcat(ret, "_", len) >= len ||
1511 BUF_strlcat(ret, prf_name, len) >= len) {
1512 assert(0);
1513 OPENSSL_free(ret);
1514 return NULL;
1515 }
1516 assert(strlen(ret) + 1 == len);
1517 return ret;
1518}
1519
1520int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1521 if (cipher == NULL) {
1522 return 0;
1523 }
1524
1525 if (out_alg_bits != NULL) {
1526 *out_alg_bits = cipher->alg_bits;
1527 }
1528 return cipher->strength_bits;
1529}
1530
Adam Langleyfcf25832014-12-18 17:42:32 -08001531const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1532 int len) {
1533 const char *ver;
1534 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001535 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001537
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 alg_mkey = cipher->algorithm_mkey;
1539 alg_auth = cipher->algorithm_auth;
1540 alg_enc = cipher->algorithm_enc;
1541 alg_mac = cipher->algorithm_mac;
1542 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001543
Adam Langleyfcf25832014-12-18 17:42:32 -08001544 if (alg_ssl & SSL_SSLV3) {
1545 ver = "SSLv3";
1546 } else if (alg_ssl & SSL_TLSV1_2) {
1547 ver = "TLSv1.2";
1548 } else {
1549 ver = "unknown";
1550 }
Adam Langley95c29f32014-06-20 12:00:00 -07001551
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 switch (alg_mkey) {
1553 case SSL_kRSA:
1554 kx = "RSA";
1555 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001556
David Benjamin7061e282015-03-19 11:10:48 -04001557 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 kx = "DH";
1559 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
David Benjamin7061e282015-03-19 11:10:48 -04001561 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 kx = "ECDH";
1563 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001564
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 case SSL_kPSK:
1566 kx = "PSK";
1567 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 default:
1570 kx = "unknown";
1571 }
Adam Langley95c29f32014-06-20 12:00:00 -07001572
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 switch (alg_auth) {
1574 case SSL_aRSA:
1575 au = "RSA";
1576 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001577
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 case SSL_aECDSA:
1579 au = "ECDSA";
1580 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 case SSL_aPSK:
1583 au = "PSK";
1584 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001585
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 default:
1587 au = "unknown";
1588 break;
1589 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001590
Adam Langleyfcf25832014-12-18 17:42:32 -08001591 switch (alg_enc) {
1592 case SSL_3DES:
1593 enc = "3DES(168)";
1594 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 case SSL_RC4:
1597 enc = "RC4(128)";
1598 break;
1599
1600 case SSL_AES128:
1601 enc = "AES(128)";
1602 break;
1603
1604 case SSL_AES256:
1605 enc = "AES(256)";
1606 break;
1607
1608 case SSL_AES128GCM:
1609 enc = "AESGCM(128)";
1610 break;
1611
1612 case SSL_AES256GCM:
1613 enc = "AESGCM(256)";
1614 break;
1615
1616 case SSL_CHACHA20POLY1305:
1617 enc = "ChaCha20-Poly1305";
1618 break;
1619
1620 default:
1621 enc = "unknown";
1622 break;
1623 }
1624
1625 switch (alg_mac) {
1626 case SSL_MD5:
1627 mac = "MD5";
1628 break;
1629
1630 case SSL_SHA1:
1631 mac = "SHA1";
1632 break;
1633
1634 case SSL_SHA256:
1635 mac = "SHA256";
1636 break;
1637
1638 case SSL_SHA384:
1639 mac = "SHA384";
1640 break;
1641
1642 case SSL_AEAD:
1643 mac = "AEAD";
1644 break;
1645
1646 default:
1647 mac = "unknown";
1648 break;
1649 }
1650
1651 if (buf == NULL) {
1652 len = 128;
1653 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001654 if (buf == NULL) {
1655 return NULL;
1656 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 } else if (len < 128) {
1658 return "Buffer too small";
1659 }
1660
1661 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1662 return buf;
1663}
1664
David Benjamin71f07942015-04-08 02:36:59 -04001665const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1666 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001667}
1668
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001669COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001670
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001671int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001672
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001673const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001674
David Benjamind1d80782015-07-05 11:54:09 -04001675int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001676 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langleyfcf25832014-12-18 17:42:32 -08001678 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001679 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001681 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 }
Adam Langley95c29f32014-06-20 12:00:00 -07001683
David Benjamind1d80782015-07-05 11:54:09 -04001684 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001685}
David Benjamin9c651c92014-07-12 13:27:45 -04001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001688 /* PSK-authenticated ciphers do not use a public key, except for
1689 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 if ((cipher->algorithm_auth & SSL_aPSK) &&
1691 !(cipher->algorithm_mkey & SSL_kRSA)) {
1692 return 0;
1693 }
1694
1695 /* All other ciphers include it. */
1696 return 1;
1697}
1698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1700 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001701 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 return 1;
1703 }
1704
1705 /* It is optional in all others. */
1706 return 0;
1707}