blob: a9dc0b4274549b4f62753a760e833e57ec04a46c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
David Benjamin9e4e01e2015-09-15 01:48:04 -0400141#include <openssl/ssl.h>
142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400144#include <stdio.h>
145#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin71f07942015-04-08 02:36:59 -0400147#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400148#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500151#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400152#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin2ee94aa2015-04-07 22:38:30 -0400154#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamina1c90a52015-05-30 17:03:14 -0400157/* kCiphers is an array of all supported ciphers, sorted by id. */
David Benjamin20c37312015-11-11 21:33:18 -0800158static const SSL_CIPHER kCiphers[] = {
David Benjamina1c90a52015-05-30 17:03:14 -0400159 /* The RSA ciphers */
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700160 /* Cipher 02 */
161 {
David Benjaminff2df332015-11-18 10:01:16 -0500162 SSL3_TXT_RSA_NULL_SHA,
163 SSL3_CK_RSA_NULL_SHA,
164 SSL_kRSA,
165 SSL_aRSA,
166 SSL_eNULL,
167 SSL_SHA1,
168 SSL_HANDSHAKE_MAC_DEFAULT,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700169 },
170
David Benjamina1c90a52015-05-30 17:03:14 -0400171 /* Cipher 04 */
172 {
David Benjaminff2df332015-11-18 10:01:16 -0500173 SSL3_TXT_RSA_RC4_128_MD5,
174 SSL3_CK_RSA_RC4_128_MD5,
175 SSL_kRSA,
176 SSL_aRSA,
177 SSL_RC4,
178 SSL_MD5,
179 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400180 },
181
182 /* Cipher 05 */
183 {
David Benjaminff2df332015-11-18 10:01:16 -0500184 SSL3_TXT_RSA_RC4_128_SHA,
185 SSL3_CK_RSA_RC4_128_SHA,
186 SSL_kRSA,
187 SSL_aRSA,
188 SSL_RC4,
189 SSL_SHA1,
190 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400191 },
192
193 /* Cipher 0A */
194 {
David Benjaminff2df332015-11-18 10:01:16 -0500195 SSL3_TXT_RSA_DES_192_CBC3_SHA,
196 SSL3_CK_RSA_DES_192_CBC3_SHA,
197 SSL_kRSA,
198 SSL_aRSA,
199 SSL_3DES,
200 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500201 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400202 },
203
204
205 /* New AES ciphersuites */
206
207 /* Cipher 2F */
208 {
David Benjaminff2df332015-11-18 10:01:16 -0500209 TLS1_TXT_RSA_WITH_AES_128_SHA,
210 TLS1_CK_RSA_WITH_AES_128_SHA,
211 SSL_kRSA,
212 SSL_aRSA,
213 SSL_AES128,
214 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500215 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400216 },
217
218 /* Cipher 33 */
219 {
David Benjaminff2df332015-11-18 10:01:16 -0500220 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
221 TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
222 SSL_kDHE,
223 SSL_aRSA,
224 SSL_AES128,
225 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500226 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400227 },
228
229 /* Cipher 35 */
230 {
David Benjaminff2df332015-11-18 10:01:16 -0500231 TLS1_TXT_RSA_WITH_AES_256_SHA,
232 TLS1_CK_RSA_WITH_AES_256_SHA,
233 SSL_kRSA,
234 SSL_aRSA,
235 SSL_AES256,
236 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500237 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400238 },
239
240 /* Cipher 39 */
241 {
David Benjaminff2df332015-11-18 10:01:16 -0500242 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
243 TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
244 SSL_kDHE,
245 SSL_aRSA,
246 SSL_AES256,
247 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500248 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400249 },
250
251
252 /* TLS v1.2 ciphersuites */
253
254 /* Cipher 3C */
255 {
David Benjaminff2df332015-11-18 10:01:16 -0500256 TLS1_TXT_RSA_WITH_AES_128_SHA256,
257 TLS1_CK_RSA_WITH_AES_128_SHA256,
258 SSL_kRSA,
259 SSL_aRSA,
260 SSL_AES128,
261 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500262 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400263 },
264
265 /* Cipher 3D */
266 {
David Benjaminff2df332015-11-18 10:01:16 -0500267 TLS1_TXT_RSA_WITH_AES_256_SHA256,
268 TLS1_CK_RSA_WITH_AES_256_SHA256,
269 SSL_kRSA,
270 SSL_aRSA,
271 SSL_AES256,
272 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500273 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400274 },
275
276 /* Cipher 67 */
277 {
278 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500279 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
280 SSL_kDHE,
281 SSL_aRSA,
282 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500283 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500284 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400285 },
286
287 /* Cipher 6B */
288 {
289 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500290 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
291 SSL_kDHE,
292 SSL_aRSA,
293 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500294 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500295 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400296 },
297
Adam Langley85bc5602015-06-09 09:54:04 -0700298 /* PSK cipher suites. */
299
David Benjamina1c90a52015-05-30 17:03:14 -0400300 /* Cipher 8A */
301 {
David Benjaminff2df332015-11-18 10:01:16 -0500302 TLS1_TXT_PSK_WITH_RC4_128_SHA,
303 TLS1_CK_PSK_WITH_RC4_128_SHA,
304 SSL_kPSK,
305 SSL_aPSK,
306 SSL_RC4,
307 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500308 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400309 },
310
311 /* Cipher 8C */
312 {
David Benjaminff2df332015-11-18 10:01:16 -0500313 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
314 TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
315 SSL_kPSK,
316 SSL_aPSK,
317 SSL_AES128,
318 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500319 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400320 },
321
322 /* Cipher 8D */
323 {
David Benjaminff2df332015-11-18 10:01:16 -0500324 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
325 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
326 SSL_kPSK,
327 SSL_aPSK,
328 SSL_AES256,
329 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500330 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400331 },
332
David Benjamina1c90a52015-05-30 17:03:14 -0400333 /* GCM ciphersuites from RFC5288 */
334
335 /* Cipher 9C */
336 {
337 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500338 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
339 SSL_kRSA,
340 SSL_aRSA,
341 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500342 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400343 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400344 },
345
346 /* Cipher 9D */
347 {
348 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500349 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
350 SSL_kRSA,
351 SSL_aRSA,
352 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500353 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400354 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400355 },
356
357 /* Cipher 9E */
358 {
359 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500360 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
361 SSL_kDHE,
362 SSL_aRSA,
363 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500364 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400365 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400366 },
367
368 /* Cipher 9F */
369 {
370 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500371 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
372 SSL_kDHE,
373 SSL_aRSA,
374 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500375 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400376 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400377 },
378
379 /* Cipher C007 */
380 {
381 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500382 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA,
383 SSL_kECDHE,
384 SSL_aECDSA,
385 SSL_RC4,
386 SSL_SHA1,
387 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400388 },
389
390 /* Cipher C009 */
391 {
392 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500393 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
394 SSL_kECDHE,
395 SSL_aECDSA,
396 SSL_AES128,
397 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500398 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400399 },
400
401 /* Cipher C00A */
402 {
403 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500404 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
405 SSL_kECDHE,
406 SSL_aECDSA,
407 SSL_AES256,
408 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500409 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400410 },
411
412 /* Cipher C011 */
413 {
David Benjaminff2df332015-11-18 10:01:16 -0500414 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA,
415 TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
416 SSL_kECDHE,
417 SSL_aRSA,
418 SSL_RC4,
419 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500420 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400421 },
422
423 /* Cipher C013 */
424 {
425 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500426 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
427 SSL_kECDHE,
428 SSL_aRSA,
429 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500430 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500431 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400432 },
433
434 /* Cipher C014 */
435 {
436 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500437 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
438 SSL_kECDHE,
439 SSL_aRSA,
440 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500441 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500442 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400443 },
444
445
446 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
447
448 /* Cipher C023 */
449 {
450 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500451 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
452 SSL_kECDHE,
453 SSL_aECDSA,
454 SSL_AES128,
455 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500456 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400457 },
458
459 /* Cipher C024 */
460 {
461 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500462 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
463 SSL_kECDHE,
464 SSL_aECDSA,
465 SSL_AES256,
466 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500467 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400468 },
469
470 /* Cipher C027 */
471 {
472 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500473 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
474 SSL_kECDHE,
475 SSL_aRSA,
476 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500477 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500478 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400479 },
480
481 /* Cipher C028 */
482 {
483 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500484 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
485 SSL_kECDHE,
486 SSL_aRSA,
487 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500488 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500489 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400490 },
491
492
493 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
494
495 /* Cipher C02B */
496 {
497 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500498 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
499 SSL_kECDHE,
500 SSL_aECDSA,
501 SSL_AES128GCM,
502 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400503 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400504 },
505
506 /* Cipher C02C */
507 {
508 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500509 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
510 SSL_kECDHE,
511 SSL_aECDSA,
512 SSL_AES256GCM,
513 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400514 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400515 },
516
517 /* Cipher C02F */
518 {
519 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjaminff2df332015-11-18 10:01:16 -0500520 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
521 SSL_kECDHE,
522 SSL_aRSA,
523 SSL_AES128GCM,
524 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400525 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400526 },
527
528 /* Cipher C030 */
529 {
530 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjaminff2df332015-11-18 10:01:16 -0500531 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
532 SSL_kECDHE,
533 SSL_aRSA,
534 SSL_AES256GCM,
535 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400536 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400537 },
538
Adam Langley85bc5602015-06-09 09:54:04 -0700539 /* ECDHE-PSK cipher suites. */
540
541 /* Cipher C035 */
542 {
543 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
544 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500545 SSL_kECDHE,
546 SSL_aPSK,
547 SSL_AES128,
548 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500549 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700550 },
551
552 /* Cipher C036 */
553 {
554 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
555 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500556 SSL_kECDHE,
557 SSL_aPSK,
558 SSL_AES256,
559 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500560 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700561 },
562
Adam Langleyd98dc132015-09-23 16:41:33 -0700563#if !defined(BORINGSSL_ANDROID_SYSTEM)
Adam Langley85bc5602015-06-09 09:54:04 -0700564 /* ChaCha20-Poly1305 cipher suites. */
565
David Benjamina1c90a52015-05-30 17:03:14 -0400566 {
Brian Smith271777f2015-10-03 13:53:33 -1000567 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
David Benjaminff2df332015-11-18 10:01:16 -0500568 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD,
569 SSL_kECDHE,
570 SSL_aRSA,
571 SSL_CHACHA20POLY1305_OLD,
572 SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400573 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400574 },
575
576 {
Brian Smith271777f2015-10-03 13:53:33 -1000577 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
David Benjaminff2df332015-11-18 10:01:16 -0500578 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD,
579 SSL_kECDHE,
580 SSL_aECDSA,
581 SSL_CHACHA20POLY1305_OLD,
582 SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400583 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400584 },
Adam Langleyd98dc132015-09-23 16:41:33 -0700585#endif
David Benjamina1c90a52015-05-30 17:03:14 -0400586};
587
588static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
589
Adam Langleyfcf25832014-12-18 17:42:32 -0800590#define CIPHER_ADD 1
591#define CIPHER_KILL 2
592#define CIPHER_DEL 3
593#define CIPHER_ORD 4
594#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596typedef struct cipher_order_st {
597 const SSL_CIPHER *cipher;
598 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 int in_group;
600 struct cipher_order_st *next, *prev;
601} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700602
David Benjamin0344daf2015-04-08 02:08:01 -0400603typedef struct cipher_alias_st {
604 /* name is the name of the cipher alias. */
605 const char *name;
606
607 /* The following fields are bitmasks for the corresponding fields on
608 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
609 * bit corresponding to the cipher's value is set to 1. If any bitmask is
610 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
611 uint32_t algorithm_mkey;
612 uint32_t algorithm_auth;
613 uint32_t algorithm_enc;
614 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500615
616 /* min_version, if non-zero, matches all ciphers which were added in that
617 * particular protocol version. */
618 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400619} CIPHER_ALIAS;
620
David Benjamina1c90a52015-05-30 17:03:14 -0400621static const CIPHER_ALIAS kCipherAliases[] = {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700622 /* "ALL" doesn't include eNULL (must be specifically enabled) */
David Benjamind6e9eec2015-11-18 09:48:55 -0500623 {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700624
David Benjamina1c90a52015-05-30 17:03:14 -0400625 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700626
David Benjamina1c90a52015-05-30 17:03:14 -0400627 /* key exchange aliases
628 * (some of those using only a single bit here combine
629 * multiple key exchange algs according to the RFCs,
630 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamind6e9eec2015-11-18 09:48:55 -0500631 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700632
David Benjamind6e9eec2015-11-18 09:48:55 -0500633 {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
634 {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
635 {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700636
David Benjamind6e9eec2015-11-18 09:48:55 -0500637 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
638 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
639 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700640
David Benjamind6e9eec2015-11-18 09:48:55 -0500641 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700642
David Benjamina1c90a52015-05-30 17:03:14 -0400643 /* server authentication aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500644 {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
645 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
646 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
647 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamina1c90a52015-05-30 17:03:14 -0400649 /* aliases combining key exchange and server authentication */
David Benjamind6e9eec2015-11-18 09:48:55 -0500650 {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
651 {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
652 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
653 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
654 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
655 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamina1c90a52015-05-30 17:03:14 -0400657 /* symmetric encryption aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500658 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
659 {"RC4", ~0u, ~0u, SSL_RC4, ~0u, 0},
660 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
661 {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
662 {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
663 {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
664 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700665
David Benjamina1c90a52015-05-30 17:03:14 -0400666 /* MAC aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500667 {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0},
668 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
669 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
670 {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
671 {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700672
David Benjamindcb6ef02015-11-06 15:35:54 -0500673 /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
674 * same as "SSLv3". */
David Benjamind6e9eec2015-11-18 09:48:55 -0500675 {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
676 {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
677 {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700678
David Benjamind6e9eec2015-11-18 09:48:55 -0500679 /* Legacy strength classes. */
680 {"MEDIUM", ~0u, ~0u, SSL_RC4, ~0u, 0},
681 {"HIGH", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
682 {"FIPS", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800683};
Adam Langley95c29f32014-06-20 12:00:00 -0700684
David Benjamina1c90a52015-05-30 17:03:14 -0400685static const size_t kCipherAliasesLen =
686 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
687
688static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
689 const SSL_CIPHER *a = in_a;
690 const SSL_CIPHER *b = in_b;
691
692 if (a->id > b->id) {
693 return 1;
694 } else if (a->id < b->id) {
695 return -1;
696 } else {
697 return 0;
698 }
699}
700
701static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
702 return ssl_cipher_id_cmp(*a, *b);
703}
704
705const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
706 SSL_CIPHER c;
707
708 c.id = 0x03000000L | value;
709 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
710 ssl_cipher_id_cmp);
711}
David Benjamin0344daf2015-04-08 02:08:01 -0400712
David Benjaminea72bd02014-12-21 21:27:41 -0500713int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
714 size_t *out_mac_secret_len,
715 size_t *out_fixed_iv_len,
716 const SSL_CIPHER *cipher, uint16_t version) {
717 *out_aead = NULL;
718 *out_mac_secret_len = 0;
719 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700720
David Benjaminea72bd02014-12-21 21:27:41 -0500721 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500723 *out_aead = EVP_aead_aes_128_gcm();
724 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 return 1;
726
727 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500728 *out_aead = EVP_aead_aes_256_gcm();
729 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 return 1;
731
Adam Langleyd98dc132015-09-23 16:41:33 -0700732#if !defined(BORINGSSL_ANDROID_SYSTEM)
Brian Smith271777f2015-10-03 13:53:33 -1000733 case SSL_CHACHA20POLY1305_OLD:
Brian Smith3e23e4c2015-10-03 11:38:58 -1000734 *out_aead = EVP_aead_chacha20_poly1305_old();
David Benjaminea72bd02014-12-21 21:27:41 -0500735 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 return 1;
Adam Langleyd98dc132015-09-23 16:41:33 -0700737#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800738
739 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500740 switch (cipher->algorithm_mac) {
741 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500742 if (version == SSL3_VERSION) {
743 *out_aead = EVP_aead_rc4_md5_ssl3();
744 } else {
745 *out_aead = EVP_aead_rc4_md5_tls();
746 }
David Benjaminea72bd02014-12-21 21:27:41 -0500747 *out_mac_secret_len = MD5_DIGEST_LENGTH;
748 return 1;
749 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500750 if (version == SSL3_VERSION) {
751 *out_aead = EVP_aead_rc4_sha1_ssl3();
752 } else {
753 *out_aead = EVP_aead_rc4_sha1_tls();
754 }
David Benjaminea72bd02014-12-21 21:27:41 -0500755 *out_mac_secret_len = SHA_DIGEST_LENGTH;
756 return 1;
757 default:
758 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800760
David Benjaminea72bd02014-12-21 21:27:41 -0500761 case SSL_AES128:
762 switch (cipher->algorithm_mac) {
763 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500764 if (version == SSL3_VERSION) {
765 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
766 *out_fixed_iv_len = 16;
767 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500768 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
769 *out_fixed_iv_len = 16;
770 } else {
771 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
772 }
773 *out_mac_secret_len = SHA_DIGEST_LENGTH;
774 return 1;
775 case SSL_SHA256:
776 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
777 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
778 return 1;
779 default:
780 return 0;
781 }
782
783 case SSL_AES256:
784 switch (cipher->algorithm_mac) {
785 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500786 if (version == SSL3_VERSION) {
787 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
788 *out_fixed_iv_len = 16;
789 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500790 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
791 *out_fixed_iv_len = 16;
792 } else {
793 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
794 }
795 *out_mac_secret_len = SHA_DIGEST_LENGTH;
796 return 1;
797 case SSL_SHA256:
798 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
799 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
800 return 1;
801 case SSL_SHA384:
802 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
803 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
804 return 1;
805 default:
806 return 0;
807 }
808
809 case SSL_3DES:
810 switch (cipher->algorithm_mac) {
811 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500812 if (version == SSL3_VERSION) {
813 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
814 *out_fixed_iv_len = 8;
815 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500816 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
817 *out_fixed_iv_len = 8;
818 } else {
819 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
820 }
821 *out_mac_secret_len = SHA_DIGEST_LENGTH;
822 return 1;
823 default:
824 return 0;
825 }
826
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700827 case SSL_eNULL:
828 switch (cipher->algorithm_mac) {
829 case SSL_SHA1:
830 if (version == SSL3_VERSION) {
831 *out_aead = EVP_aead_null_sha1_ssl3();
832 } else {
833 *out_aead = EVP_aead_null_sha1_tls();
834 }
835 *out_mac_secret_len = SHA_DIGEST_LENGTH;
836 return 1;
837 default:
838 return 0;
839 }
840
David Benjaminea72bd02014-12-21 21:27:41 -0500841 default:
842 return 0;
843 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800844}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700845
David Benjaminb0883312015-08-06 09:54:13 -0400846const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
847 switch (algorithm_prf) {
848 case SSL_HANDSHAKE_MAC_DEFAULT:
849 return EVP_sha1();
850 case SSL_HANDSHAKE_MAC_SHA256:
851 return EVP_sha256();
852 case SSL_HANDSHAKE_MAC_SHA384:
853 return EVP_sha384();
854 default:
855 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857}
858
859#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700861
David Benjamin0344daf2015-04-08 02:08:01 -0400862/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
863 * |buf_len| bytes at |buf|. */
864static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
865 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
866 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
867}
868
Adam Langley95c29f32014-06-20 12:00:00 -0700869static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 CIPHER_ORDER **tail) {
871 if (curr == *tail) {
872 return;
873 }
874 if (curr == *head) {
875 *head = curr->next;
876 }
877 if (curr->prev != NULL) {
878 curr->prev->next = curr->next;
879 }
880 if (curr->next != NULL) {
881 curr->next->prev = curr->prev;
882 }
883 (*tail)->next = curr;
884 curr->prev = *tail;
885 curr->next = NULL;
886 *tail = curr;
887}
Adam Langley95c29f32014-06-20 12:00:00 -0700888
889static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 CIPHER_ORDER **tail) {
891 if (curr == *head) {
892 return;
893 }
894 if (curr == *tail) {
895 *tail = curr->prev;
896 }
897 if (curr->next != NULL) {
898 curr->next->prev = curr->prev;
899 }
900 if (curr->prev != NULL) {
901 curr->prev->next = curr->next;
902 }
903 (*head)->prev = curr;
904 curr->next = *head;
905 curr->prev = NULL;
906 *head = curr;
907}
Adam Langley95c29f32014-06-20 12:00:00 -0700908
David Benjamin82c9e902014-12-12 15:55:27 -0500909static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 CIPHER_ORDER *co_list,
911 CIPHER_ORDER **head_p,
912 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400913 /* The set of ciphers is static, but some subset may be unsupported by
914 * |ssl_method|, so the list may be smaller. */
915 size_t co_list_num = 0;
916 size_t i;
917 for (i = 0; i < kCiphersLen; i++) {
918 const SSL_CIPHER *cipher = &kCiphers[i];
919 if (ssl_method->supports_cipher(cipher)) {
920 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 co_list[co_list_num].next = NULL;
922 co_list[co_list_num].prev = NULL;
923 co_list[co_list_num].active = 0;
924 co_list[co_list_num].in_group = 0;
925 co_list_num++;
926 }
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 /* Prepare linked list from list entries. */
930 if (co_list_num > 0) {
931 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 if (co_list_num > 1) {
934 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 for (i = 1; i < co_list_num - 1; i++) {
937 co_list[i].prev = &co_list[i - 1];
938 co_list[i].next = &co_list[i + 1];
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
942 }
943
944 co_list[co_list_num - 1].next = NULL;
945
946 *head_p = &co_list[0];
947 *tail_p = &co_list[co_list_num - 1];
948 }
949}
Adam Langley95c29f32014-06-20 12:00:00 -0700950
David Benjamin0344daf2015-04-08 02:08:01 -0400951/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
952 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
953 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
954 *
955 * - If |cipher_id| is non-zero, only that cipher is selected.
956 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
957 * of that strength.
David Benjamind6e9eec2015-11-18 09:48:55 -0500958 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
David Benjamindcb6ef02015-11-06 15:35:54 -0500959 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800960static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400961 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500962 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
963 int strength_bits, int in_group, CIPHER_ORDER **head_p,
964 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 CIPHER_ORDER *head, *tail, *curr, *next, *last;
966 const SSL_CIPHER *cp;
967 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
David Benjamindcb6ef02015-11-06 15:35:54 -0500969 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500970 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400971 /* The rule matches nothing, so bail early. */
972 return;
973 }
974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 if (rule == CIPHER_DEL) {
976 /* needed to maintain sorting between currently deleted ciphers */
977 reverse = 1;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 head = *head_p;
981 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 if (reverse) {
984 next = tail;
985 last = head;
986 } else {
987 next = head;
988 last = tail;
989 }
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 curr = NULL;
992 for (;;) {
993 if (curr == last) {
994 break;
995 }
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 curr = next;
998 if (curr == NULL) {
999 break;
1000 }
Adam Langleye3142a72014-07-24 17:56:48 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 next = reverse ? curr->prev : curr->next;
1003 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -07001004
David Benjamin0344daf2015-04-08 02:08:01 -04001005 /* Selection criteria is either a specific cipher, the value of
1006 * |strength_bits|, or the algorithms used. */
1007 if (cipher_id != 0) {
1008 if (cipher_id != cp->id) {
1009 continue;
1010 }
1011 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001012 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 continue;
1014 }
David Benjamin0344daf2015-04-08 02:08:01 -04001015 } else if (!(alg_mkey & cp->algorithm_mkey) ||
1016 !(alg_auth & cp->algorithm_auth) ||
1017 !(alg_enc & cp->algorithm_enc) ||
1018 !(alg_mac & cp->algorithm_mac) ||
David Benjamindcb6ef02015-11-06 15:35:54 -05001019 (min_version != 0 &&
1020 SSL_CIPHER_get_min_version(cp) != min_version)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001021 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 }
Adam Langleye3142a72014-07-24 17:56:48 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 /* add the cipher if it has not been added yet. */
1025 if (rule == CIPHER_ADD) {
1026 /* reverse == 0 */
1027 if (!curr->active) {
1028 ll_append_tail(&head, curr, &tail);
1029 curr->active = 1;
1030 curr->in_group = in_group;
1031 }
1032 }
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 /* Move the added cipher to this location */
1035 else if (rule == CIPHER_ORD) {
1036 /* reverse == 0 */
1037 if (curr->active) {
1038 ll_append_tail(&head, curr, &tail);
1039 curr->in_group = 0;
1040 }
1041 } else if (rule == CIPHER_DEL) {
1042 /* reverse == 1 */
1043 if (curr->active) {
1044 /* most recently deleted ciphersuites get best positions
1045 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1046 * works in reverse to maintain the order) */
1047 ll_append_head(&head, curr, &tail);
1048 curr->active = 0;
1049 curr->in_group = 0;
1050 }
1051 } else if (rule == CIPHER_KILL) {
1052 /* reverse == 0 */
1053 if (head == curr) {
1054 head = curr->next;
1055 } else {
1056 curr->prev->next = curr->next;
1057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 if (tail == curr) {
1060 tail = curr->prev;
1061 }
1062 curr->active = 0;
1063 if (curr->next != NULL) {
1064 curr->next->prev = curr->prev;
1065 }
1066 if (curr->prev != NULL) {
1067 curr->prev->next = curr->next;
1068 }
1069 curr->next = NULL;
1070 curr->prev = NULL;
1071 }
1072 }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 *head_p = head;
1075 *tail_p = tail;
1076}
Adam Langley95c29f32014-06-20 12:00:00 -07001077
1078static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 CIPHER_ORDER **tail_p) {
1080 int max_strength_bits, i, *number_uses;
1081 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 /* This routine sorts the ciphers with descending strength. The sorting must
1084 * keep the pre-sorted sequence, so we apply the normal sorting routine as
1085 * '+' movement to the end of the list. */
1086 max_strength_bits = 0;
1087 curr = *head_p;
1088 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001089 if (curr->active &&
1090 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
1091 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 }
1093 curr = curr->next;
1094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
1097 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -04001098 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001099 return 0;
1100 }
1101 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 /* Now find the strength_bits values actually used. */
1104 curr = *head_p;
1105 while (curr != NULL) {
1106 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -05001107 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 }
1109 curr = curr->next;
1110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 /* Go through the list of used strength_bits values in descending order. */
1113 for (i = max_strength_bits; i >= 0; i--) {
1114 if (number_uses[i] > 0) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001115 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 }
1117 }
1118
1119 OPENSSL_free(number_uses);
1120 return 1;
1121}
Adam Langley95c29f32014-06-20 12:00:00 -07001122
David Benjamin0344daf2015-04-08 02:08:01 -04001123static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1124 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001126 CIPHER_ORDER **tail_p) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001127 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001128 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 const char *l, *buf;
David Benjamindcb6ef02015-11-06 15:35:54 -05001130 int multi, skip_rule, rule, retval, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001131 size_t j, buf_len;
1132 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 retval = 1;
1136 l = rule_str;
1137 for (;;) {
1138 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 if (ch == '\0') {
1141 break; /* done */
1142 }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 if (in_group) {
1145 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001146 if (*tail_p) {
1147 (*tail_p)->in_group = 0;
1148 }
1149 in_group = 0;
1150 l++;
1151 continue;
1152 }
David Benjamin37d92462014-09-20 17:54:24 -04001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 if (ch == '|') {
1155 rule = CIPHER_ADD;
1156 l++;
1157 continue;
1158 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1159 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001160 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001161 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 break;
1163 } else {
1164 rule = CIPHER_ADD;
1165 }
1166 } else if (ch == '-') {
1167 rule = CIPHER_DEL;
1168 l++;
1169 } else if (ch == '+') {
1170 rule = CIPHER_ORD;
1171 l++;
1172 } else if (ch == '!') {
1173 rule = CIPHER_KILL;
1174 l++;
1175 } else if (ch == '@') {
1176 rule = CIPHER_SPECIAL;
1177 l++;
1178 } else if (ch == '[') {
1179 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001180 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001181 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 break;
1183 }
1184 in_group = 1;
1185 has_group = 1;
1186 l++;
1187 continue;
1188 } else {
1189 rule = CIPHER_ADD;
1190 }
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 /* If preference groups are enabled, the only legal operator is +.
1193 * Otherwise the in_group bits will get mixed up. */
1194 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001195 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001196 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 break;
1198 }
Adam Langley95c29f32014-06-20 12:00:00 -07001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 if (ITEM_SEP(ch)) {
1201 l++;
1202 continue;
1203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
David Benjamin0344daf2015-04-08 02:08:01 -04001205 multi = 0;
1206 cipher_id = 0;
1207 alg_mkey = ~0u;
1208 alg_auth = ~0u;
1209 alg_enc = ~0u;
1210 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001211 min_version = 0;
1212 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 for (;;) {
1215 ch = *l;
1216 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001217 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1219 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1220 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001221 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 }
Adam Langley95c29f32014-06-20 12:00:00 -07001223
David Benjamin0344daf2015-04-08 02:08:01 -04001224 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 /* We hit something we cannot deal with, it is no command or separator
1226 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001227 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001228 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 l++;
1230 break;
1231 }
Adam Langley95c29f32014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 break;
1235 }
David Benjamin0344daf2015-04-08 02:08:01 -04001236
1237 /* Look for a matching exact cipher. These aren't allowed in multipart
1238 * rules. */
1239 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001240 for (j = 0; j < kCiphersLen; j++) {
1241 const SSL_CIPHER *cipher = &kCiphers[j];
1242 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001243 cipher_id = cipher->id;
1244 break;
1245 }
1246 }
1247 }
1248 if (cipher_id == 0) {
1249 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001250 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001251 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1252 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1253 alg_auth &= kCipherAliases[j].algorithm_auth;
1254 alg_enc &= kCipherAliases[j].algorithm_enc;
1255 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001256
1257 if (min_version != 0 &&
1258 min_version != kCipherAliases[j].min_version) {
1259 skip_rule = 1;
1260 } else {
1261 min_version = kCipherAliases[j].min_version;
1262 }
David Benjamin0344daf2015-04-08 02:08:01 -04001263 break;
1264 }
1265 }
David Benjamina1c90a52015-05-30 17:03:14 -04001266 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001267 skip_rule = 1;
David Benjamin0344daf2015-04-08 02:08:01 -04001268 }
1269 }
1270
1271 /* Check for a multipart rule. */
1272 if (ch != '+') {
1273 break;
1274 }
1275 l++;
1276 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 }
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 /* Ok, we have the rule, now apply it. */
1280 if (rule == CIPHER_SPECIAL) {
1281 /* special command */
1282 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001283 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001284 ok = ssl_cipher_strength_sort(head_p, tail_p);
1285 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001286 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001287 }
Adam Langley95c29f32014-06-20 12:00:00 -07001288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 if (ok == 0) {
1290 retval = 0;
1291 }
Adam Langley95c29f32014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 /* We do not support any "multi" options together with "@", so throw away
1294 * the rest of the command, if any left, until end or ':' is found. */
1295 while (*l != '\0' && !ITEM_SEP(*l)) {
1296 l++;
1297 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001298 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001300 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 }
1302 }
Adam Langley95c29f32014-06-20 12:00:00 -07001303
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001305 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 retval = 0;
1307 }
Adam Langley95c29f32014-06-20 12:00:00 -07001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 return retval;
1310}
Adam Langley95c29f32014-06-20 12:00:00 -07001311
Adam Langleyfcf25832014-12-18 17:42:32 -08001312STACK_OF(SSL_CIPHER) *
1313ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001314 struct ssl_cipher_preference_list_st **out_cipher_list,
1315 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1316 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001317 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001318 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1319 const char *rule_p;
1320 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 uint8_t *in_group_flags = NULL;
1322 unsigned int num_in_group_flags = 0;
1323 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001326 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 return NULL;
1328 }
David Benjamin5213df42014-08-20 14:19:54 -04001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 /* Now we have to collect the available ciphers from the compiled in ciphers.
1331 * We cannot get more than the number compiled in, so it is used for
1332 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001333 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001335 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 return NULL;
1337 }
Adam Langley95c29f32014-06-20 12:00:00 -07001338
David Benjamina1c90a52015-05-30 17:03:14 -04001339 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 /* Now arrange all ciphers by preference:
1342 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1345 * key exchange mechanisms */
David Benjamind6e9eec2015-11-18 09:48:55 -05001346 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001348 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
1349 &head, &tail);
1350 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0,
1351 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001352
Adam Langleyfcf25832014-12-18 17:42:32 -08001353 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1354 * CHACHA20 unless there is hardware support for fast and constant-time
1355 * AES_GCM. */
1356 if (EVP_has_aes_hardware()) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001357 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1358 &head, &tail);
1359 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1360 &head, &tail);
1361 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001362 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 } else {
David Benjamind6e9eec2015-11-18 09:48:55 -05001364 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001365 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001366 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1367 &head, &tail);
1368 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1369 &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 }
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1373 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001374 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001376 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
1377 &head, &tail);
1378 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, 0, CIPHER_ADD, -1, 0,
1379 &head, &tail);
1380 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, 0, CIPHER_ADD, -1, 0,
1381 &head, &tail);
1382 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1383 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001384
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 /* Temporarily enable everything else for sorting */
David Benjamind6e9eec2015-11-18 09:48:55 -05001386 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1387 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 /* Move ciphers without forward secrecy to the end. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001390 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001391 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 /* Now disable everything (maintaining the ordering!) */
David Benjamind6e9eec2015-11-18 09:48:55 -05001394 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1395 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001396
Adam Langleyfcf25832014-12-18 17:42:32 -08001397 /* If the rule_string begins with DEFAULT, apply the default rule before
1398 * using the (possibly available) additional rules. */
1399 ok = 1;
1400 rule_p = rule_str;
1401 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001402 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1403 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 rule_p += 7;
1405 if (*rule_p == ':') {
1406 rule_p++;
1407 }
1408 }
Adam Langley858a88d2014-06-20 12:00:00 -07001409
Adam Langleyfcf25832014-12-18 17:42:32 -08001410 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001411 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 if (!ok) {
1415 goto err;
1416 }
1417
1418 /* Allocate new "cipherstack" for the result, return with error
1419 * if we cannot get one. */
1420 cipherstack = sk_SSL_CIPHER_new_null();
1421 if (cipherstack == NULL) {
1422 goto err;
1423 }
1424
David Benjamina1c90a52015-05-30 17:03:14 -04001425 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 if (!in_group_flags) {
1427 goto err;
1428 }
1429
1430 /* The cipher selection for the list is done. The ciphers are added
1431 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1432 for (curr = head; curr != NULL; curr = curr->next) {
1433 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001434 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1435 goto err;
1436 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 in_group_flags[num_in_group_flags++] = curr->in_group;
1438 }
1439 }
1440 OPENSSL_free(co_list); /* Not needed any longer */
1441 co_list = NULL;
1442
1443 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1444 if (tmp_cipher_list == NULL) {
1445 goto err;
1446 }
1447 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1448 if (!pref_list) {
1449 goto err;
1450 }
1451 pref_list->ciphers = cipherstack;
1452 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1453 if (!pref_list->in_group_flags) {
1454 goto err;
1455 }
1456 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1457 OPENSSL_free(in_group_flags);
1458 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001459 if (*out_cipher_list != NULL) {
1460 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001461 }
David Benjamin71f07942015-04-08 02:36:59 -04001462 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 pref_list = NULL;
1464
David Benjamin71f07942015-04-08 02:36:59 -04001465 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001466 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001467 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001468 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001469 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1470 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001471
David Benjamin71f07942015-04-08 02:36:59 -04001472 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001473 } else {
1474 sk_SSL_CIPHER_free(tmp_cipher_list);
1475 tmp_cipher_list = NULL;
1476 }
1477
1478 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001479
1480err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001481 OPENSSL_free(co_list);
1482 OPENSSL_free(in_group_flags);
1483 sk_SSL_CIPHER_free(cipherstack);
1484 sk_SSL_CIPHER_free(tmp_cipher_list);
1485 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001486 OPENSSL_free(pref_list->in_group_flags);
1487 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001488 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 return NULL;
1490}
Adam Langley95c29f32014-06-20 12:00:00 -07001491
David Benjamin71f07942015-04-08 02:36:59 -04001492uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1493
David Benjamina1c90a52015-05-30 17:03:14 -04001494uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1495 uint32_t id = cipher->id;
1496 /* All ciphers are SSLv3. */
1497 assert((id & 0xff000000) == 0x03000000);
1498 return id & 0xffff;
1499}
1500
David Benjamin71f07942015-04-08 02:36:59 -04001501int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1502 return (cipher->algorithm_enc & SSL_AES) != 0;
1503}
1504
1505int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1506 return (cipher->algorithm_mac & SSL_MD5) != 0;
1507}
1508
David Benjaminef793f42015-11-05 18:16:27 -05001509int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1510 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1511}
1512
David Benjamin71f07942015-04-08 02:36:59 -04001513int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001514 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001515}
1516
David Benjaminef793f42015-11-05 18:16:27 -05001517int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1518 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1519}
1520
Adam Langleyb00061c2015-11-16 17:44:52 -08001521int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1522 return (cipher->algorithm_enc & SSL_AES128) != 0;
1523}
1524
1525int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1526 return (cipher->algorithm_enc & SSL_AES256) != 0;
1527}
1528
David Benjamin51a01a52015-10-29 13:19:56 -04001529int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
Brian Smith271777f2015-10-03 13:53:33 -10001530 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305_OLD) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001531}
1532
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001533int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1534 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1535}
1536
Adam Langleyd7fe75c2015-09-18 15:40:48 -07001537int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) {
1538 return (cipher->algorithm_enc & SSL_RC4) != 0;
1539}
1540
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001541int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1542 /* Neither stream cipher nor AEAD. */
1543 return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 &&
1544 cipher->algorithm_mac != SSL_AEAD;
1545}
1546
David Benjaminef793f42015-11-05 18:16:27 -05001547int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1548 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1549}
1550
1551uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001552 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1553 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1554 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001555 return TLS1_2_VERSION;
1556 }
1557 return SSL3_VERSION;
1558}
1559
David Benjamin71f07942015-04-08 02:36:59 -04001560/* return the actual cipher being used */
1561const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1562 if (cipher != NULL) {
1563 return cipher->name;
1564 }
1565
1566 return "(NONE)";
1567}
1568
1569const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1570 if (cipher == NULL) {
1571 return "";
1572 }
1573
1574 switch (cipher->algorithm_mkey) {
1575 case SSL_kRSA:
1576 return "RSA";
1577
1578 case SSL_kDHE:
1579 switch (cipher->algorithm_auth) {
1580 case SSL_aRSA:
1581 return "DHE_RSA";
1582 default:
1583 assert(0);
1584 return "UNKNOWN";
1585 }
1586
1587 case SSL_kECDHE:
1588 switch (cipher->algorithm_auth) {
1589 case SSL_aECDSA:
1590 return "ECDHE_ECDSA";
1591 case SSL_aRSA:
1592 return "ECDHE_RSA";
1593 case SSL_aPSK:
1594 return "ECDHE_PSK";
1595 default:
1596 assert(0);
1597 return "UNKNOWN";
1598 }
1599
1600 case SSL_kPSK:
1601 assert(cipher->algorithm_auth == SSL_aPSK);
1602 return "PSK";
1603
1604 default:
1605 assert(0);
1606 return "UNKNOWN";
1607 }
1608}
1609
1610static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1611 switch (cipher->algorithm_enc) {
1612 case SSL_3DES:
1613 return "3DES_EDE_CBC";
1614 case SSL_RC4:
1615 return "RC4";
1616 case SSL_AES128:
1617 return "AES_128_CBC";
1618 case SSL_AES256:
1619 return "AES_256_CBC";
1620 case SSL_AES128GCM:
1621 return "AES_128_GCM";
1622 case SSL_AES256GCM:
1623 return "AES_256_GCM";
Brian Smith271777f2015-10-03 13:53:33 -10001624 case SSL_CHACHA20POLY1305_OLD:
David Benjamin71f07942015-04-08 02:36:59 -04001625 return "CHACHA20_POLY1305";
1626 break;
1627 default:
1628 assert(0);
1629 return "UNKNOWN";
1630 }
1631}
1632
1633static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001634 switch (cipher->algorithm_prf) {
1635 case SSL_HANDSHAKE_MAC_DEFAULT:
1636 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1637 * only ever MD5 or SHA-1. */
1638 switch (cipher->algorithm_mac) {
1639 case SSL_MD5:
1640 return "MD5";
1641 case SSL_SHA1:
1642 return "SHA";
1643 }
1644 break;
1645 case SSL_HANDSHAKE_MAC_SHA256:
1646 return "SHA256";
1647 case SSL_HANDSHAKE_MAC_SHA384:
1648 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001649 }
David Benjaminb0883312015-08-06 09:54:13 -04001650 assert(0);
1651 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001652}
1653
1654char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1655 if (cipher == NULL) {
1656 return NULL;
1657 }
1658
1659 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1660 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1661 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1662
1663 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1664 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1665 strlen(prf_name) + 1;
1666 char *ret = OPENSSL_malloc(len);
1667 if (ret == NULL) {
1668 return NULL;
1669 }
1670 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1671 BUF_strlcat(ret, kx_name, len) >= len ||
1672 BUF_strlcat(ret, "_WITH_", len) >= len ||
1673 BUF_strlcat(ret, enc_name, len) >= len ||
1674 BUF_strlcat(ret, "_", len) >= len ||
1675 BUF_strlcat(ret, prf_name, len) >= len) {
1676 assert(0);
1677 OPENSSL_free(ret);
1678 return NULL;
1679 }
1680 assert(strlen(ret) + 1 == len);
1681 return ret;
1682}
1683
1684int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1685 if (cipher == NULL) {
1686 return 0;
1687 }
1688
David Benjamin9f2e2772015-11-18 09:59:43 -05001689 int alg_bits, strength_bits;
1690 switch (cipher->algorithm_enc) {
1691 case SSL_AES128:
1692 case SSL_AES128GCM:
1693 case SSL_RC4:
1694 alg_bits = 128;
1695 strength_bits = 128;
1696 break;
1697
1698 case SSL_AES256:
1699 case SSL_AES256GCM:
1700#if !defined(BORINGSSL_ANDROID_SYSTEM)
1701 case SSL_CHACHA20POLY1305_OLD:
1702#endif
1703 alg_bits = 256;
1704 strength_bits = 256;
1705 break;
1706
1707 case SSL_3DES:
1708 alg_bits = 168;
1709 strength_bits = 112;
1710 break;
1711
1712 case SSL_eNULL:
1713 alg_bits = 0;
1714 strength_bits = 0;
1715 break;
1716
1717 default:
1718 assert(0);
1719 alg_bits = 0;
1720 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001721 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001722
1723 if (out_alg_bits != NULL) {
1724 *out_alg_bits = alg_bits;
1725 }
1726 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001727}
1728
Adam Langleyfcf25832014-12-18 17:42:32 -08001729const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1730 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001732 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1733 static const char *format = "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 alg_mkey = cipher->algorithm_mkey;
1736 alg_auth = cipher->algorithm_auth;
1737 alg_enc = cipher->algorithm_enc;
1738 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 switch (alg_mkey) {
1741 case SSL_kRSA:
1742 kx = "RSA";
1743 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001744
David Benjamin7061e282015-03-19 11:10:48 -04001745 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 kx = "DH";
1747 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001748
David Benjamin7061e282015-03-19 11:10:48 -04001749 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001750 kx = "ECDH";
1751 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 case SSL_kPSK:
1754 kx = "PSK";
1755 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 default:
1758 kx = "unknown";
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 switch (alg_auth) {
1762 case SSL_aRSA:
1763 au = "RSA";
1764 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001765
Adam Langleyfcf25832014-12-18 17:42:32 -08001766 case SSL_aECDSA:
1767 au = "ECDSA";
1768 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 case SSL_aPSK:
1771 au = "PSK";
1772 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 default:
1775 au = "unknown";
1776 break;
1777 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 switch (alg_enc) {
1780 case SSL_3DES:
1781 enc = "3DES(168)";
1782 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001783
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 case SSL_RC4:
1785 enc = "RC4(128)";
1786 break;
1787
1788 case SSL_AES128:
1789 enc = "AES(128)";
1790 break;
1791
1792 case SSL_AES256:
1793 enc = "AES(256)";
1794 break;
1795
1796 case SSL_AES128GCM:
1797 enc = "AESGCM(128)";
1798 break;
1799
1800 case SSL_AES256GCM:
1801 enc = "AESGCM(256)";
1802 break;
1803
Brian Smith271777f2015-10-03 13:53:33 -10001804 case SSL_CHACHA20POLY1305_OLD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 enc = "ChaCha20-Poly1305";
1806 break;
1807
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001808 case SSL_eNULL:
1809 enc="None";
1810 break;
1811
Adam Langleyfcf25832014-12-18 17:42:32 -08001812 default:
1813 enc = "unknown";
1814 break;
1815 }
1816
1817 switch (alg_mac) {
1818 case SSL_MD5:
1819 mac = "MD5";
1820 break;
1821
1822 case SSL_SHA1:
1823 mac = "SHA1";
1824 break;
1825
1826 case SSL_SHA256:
1827 mac = "SHA256";
1828 break;
1829
1830 case SSL_SHA384:
1831 mac = "SHA384";
1832 break;
1833
1834 case SSL_AEAD:
1835 mac = "AEAD";
1836 break;
1837
1838 default:
1839 mac = "unknown";
1840 break;
1841 }
1842
1843 if (buf == NULL) {
1844 len = 128;
1845 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001846 if (buf == NULL) {
1847 return NULL;
1848 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 } else if (len < 128) {
1850 return "Buffer too small";
1851 }
1852
David Benjamindcb6ef02015-11-06 15:35:54 -05001853 BIO_snprintf(buf, len, format, cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 return buf;
1855}
1856
David Benjamin71f07942015-04-08 02:36:59 -04001857const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1858 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001859}
1860
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001861COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001862
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001863int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001864
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001865const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001866
David Benjamind1d80782015-07-05 11:54:09 -04001867int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001868 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001869
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001871 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001873 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 }
Adam Langley95c29f32014-06-20 12:00:00 -07001875
David Benjamind1d80782015-07-05 11:54:09 -04001876 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001877}
David Benjamin9c651c92014-07-12 13:27:45 -04001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamindf0905a2015-09-19 09:58:31 -04001880 /* PSK-authenticated ciphers do not use a certificate. (RSA_PSK is not
1881 * supported.) */
1882 if (cipher->algorithm_auth & SSL_aPSK) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 return 0;
1884 }
1885
1886 /* All other ciphers include it. */
1887 return 1;
1888}
1889
Adam Langleyfcf25832014-12-18 17:42:32 -08001890int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1891 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001892 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001893 return 1;
1894 }
1895
1896 /* It is optional in all others. */
1897 return 0;
1898}
David Benjaminb8d28cf2015-07-28 21:34:45 -04001899
1900size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1901 size_t block_size;
1902 switch (cipher->algorithm_enc) {
1903 case SSL_3DES:
1904 block_size = 8;
1905 break;
1906 case SSL_AES128:
1907 case SSL_AES256:
1908 block_size = 16;
1909 break;
1910 default:
1911 return 0;
1912 }
1913
1914 size_t mac_len;
1915 switch (cipher->algorithm_mac) {
1916 case SSL_MD5:
1917 mac_len = MD5_DIGEST_LENGTH;
1918 break;
1919 case SSL_SHA1:
1920 mac_len = SHA_DIGEST_LENGTH;
1921 break;
1922 default:
1923 return 0;
1924 }
1925
1926 size_t ret = 1 + mac_len;
1927 ret += block_size - (ret % block_size);
1928 return ret;
1929}