blob: 9df81d9cecda75dac298d78ff9caccf2105880a4 [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 {
162 SSL3_TXT_RSA_NULL_SHA, SSL3_CK_RSA_NULL_SHA, SSL_kRSA, SSL_aRSA,
David Benjamin9f2e2772015-11-18 09:59:43 -0500163 SSL_eNULL, SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700164 },
165
David Benjamina1c90a52015-05-30 17:03:14 -0400166 /* Cipher 04 */
167 {
168 SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
David Benjamin9f2e2772015-11-18 09:59:43 -0500169 SSL_RC4, SSL_MD5, SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400170 },
171
172 /* Cipher 05 */
173 {
174 SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
David Benjamin9f2e2772015-11-18 09:59:43 -0500175 SSL_RC4, SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400176 },
177
178 /* Cipher 0A */
179 {
180 SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500181 SSL_aRSA, SSL_3DES, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500182 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400183 },
184
185
186 /* New AES ciphersuites */
187
188 /* Cipher 2F */
189 {
190 TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500191 SSL_aRSA, SSL_AES128, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500192 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400193 },
194
195 /* Cipher 33 */
196 {
197 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500198 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500199 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400200 },
201
202 /* Cipher 35 */
203 {
204 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500205 SSL_aRSA, SSL_AES256, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500206 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400207 },
208
209 /* Cipher 39 */
210 {
211 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500212 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500213 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400214 },
215
216
217 /* TLS v1.2 ciphersuites */
218
219 /* Cipher 3C */
220 {
221 TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500222 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500223 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400224 },
225
226 /* Cipher 3D */
227 {
228 TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
David Benjamindcb6ef02015-11-06 15:35:54 -0500229 SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500230 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400231 },
232
233 /* Cipher 67 */
234 {
235 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
236 TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500237 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500238 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400239 },
240
241 /* Cipher 6B */
242 {
243 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
244 TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500245 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500246 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400247 },
248
Adam Langley85bc5602015-06-09 09:54:04 -0700249 /* PSK cipher suites. */
250
David Benjamina1c90a52015-05-30 17:03:14 -0400251 /* Cipher 8A */
252 {
253 TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
David Benjamind6e9eec2015-11-18 09:48:55 -0500254 SSL_aPSK, SSL_RC4, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500255 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400256 },
257
258 /* Cipher 8C */
259 {
260 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500261 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500262 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400263 },
264
265 /* Cipher 8D */
266 {
267 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500268 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500269 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400270 },
271
David Benjamina1c90a52015-05-30 17:03:14 -0400272 /* GCM ciphersuites from RFC5288 */
273
274 /* Cipher 9C */
275 {
276 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
277 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500278 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400279 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400280 },
281
282 /* Cipher 9D */
283 {
284 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
285 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500286 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400287 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400288 },
289
290 /* Cipher 9E */
291 {
292 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
293 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500294 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400295 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400296 },
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,
David Benjamind6e9eec2015-11-18 09:48:55 -0500302 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400303 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400304 },
305
306 /* Cipher C007 */
307 {
308 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
309 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
David Benjamin9f2e2772015-11-18 09:59:43 -0500310 SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400311 },
312
313 /* Cipher C009 */
314 {
315 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
316 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500317 SSL_AES128, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500318 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400319 },
320
321 /* Cipher C00A */
322 {
323 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
324 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500325 SSL_AES256, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500326 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400327 },
328
329 /* Cipher C011 */
330 {
331 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500332 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500333 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400334 },
335
336 /* Cipher C013 */
337 {
338 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
339 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500340 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500341 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400342 },
343
344 /* Cipher C014 */
345 {
346 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
347 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500348 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500349 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400350 },
351
352
353 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
354
355 /* Cipher C023 */
356 {
357 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
358 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500359 SSL_AES128, SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500360 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400361 },
362
363 /* Cipher C024 */
364 {
365 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
366 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500367 SSL_AES256, SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500368 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400369 },
370
371 /* Cipher C027 */
372 {
373 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
374 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500375 SSL_SHA256,
David Benjamin9f2e2772015-11-18 09:59:43 -0500376 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400377 },
378
379 /* Cipher C028 */
380 {
381 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
382 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500383 SSL_SHA384,
David Benjamin9f2e2772015-11-18 09:59:43 -0500384 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400385 },
386
387
388 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
389
390 /* Cipher C02B */
391 {
392 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
393 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500394 SSL_AES128GCM, SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400395 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400396 },
397
398 /* Cipher C02C */
399 {
400 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
401 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500402 SSL_AES256GCM, SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400403 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400404 },
405
406 /* Cipher C02F */
407 {
408 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
409 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500410 SSL_AES128GCM, SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400411 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400412 },
413
414 /* Cipher C030 */
415 {
416 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
417 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500418 SSL_AES256GCM, SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400419 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400420 },
421
Adam Langley85bc5602015-06-09 09:54:04 -0700422 /* ECDHE-PSK cipher suites. */
423
424 /* Cipher C035 */
425 {
426 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
427 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500428 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500429 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700430 },
431
432 /* Cipher C036 */
433 {
434 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
435 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500436 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500437 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700438 },
439
Adam Langleyd98dc132015-09-23 16:41:33 -0700440#if !defined(BORINGSSL_ANDROID_SYSTEM)
Adam Langley85bc5602015-06-09 09:54:04 -0700441 /* ChaCha20-Poly1305 cipher suites. */
442
David Benjamina1c90a52015-05-30 17:03:14 -0400443 {
Brian Smith271777f2015-10-03 13:53:33 -1000444 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
445 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aRSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500446 SSL_CHACHA20POLY1305_OLD, SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400447 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400448 },
449
450 {
Brian Smith271777f2015-10-03 13:53:33 -1000451 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
452 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aECDSA,
David Benjamind6e9eec2015-11-18 09:48:55 -0500453 SSL_CHACHA20POLY1305_OLD, SSL_AEAD,
David Benjamina1c90a52015-05-30 17:03:14 -0400454 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400455 },
Adam Langleyd98dc132015-09-23 16:41:33 -0700456#endif
David Benjamina1c90a52015-05-30 17:03:14 -0400457};
458
459static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461#define CIPHER_ADD 1
462#define CIPHER_KILL 2
463#define CIPHER_DEL 3
464#define CIPHER_ORD 4
465#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467typedef struct cipher_order_st {
468 const SSL_CIPHER *cipher;
469 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 int in_group;
471 struct cipher_order_st *next, *prev;
472} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700473
David Benjamin0344daf2015-04-08 02:08:01 -0400474typedef struct cipher_alias_st {
475 /* name is the name of the cipher alias. */
476 const char *name;
477
478 /* The following fields are bitmasks for the corresponding fields on
479 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
480 * bit corresponding to the cipher's value is set to 1. If any bitmask is
481 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
482 uint32_t algorithm_mkey;
483 uint32_t algorithm_auth;
484 uint32_t algorithm_enc;
485 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500486
487 /* min_version, if non-zero, matches all ciphers which were added in that
488 * particular protocol version. */
489 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400490} CIPHER_ALIAS;
491
David Benjamina1c90a52015-05-30 17:03:14 -0400492static const CIPHER_ALIAS kCipherAliases[] = {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700493 /* "ALL" doesn't include eNULL (must be specifically enabled) */
David Benjamind6e9eec2015-11-18 09:48:55 -0500494 {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700495
David Benjamina1c90a52015-05-30 17:03:14 -0400496 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700497
David Benjamina1c90a52015-05-30 17:03:14 -0400498 /* key exchange aliases
499 * (some of those using only a single bit here combine
500 * multiple key exchange algs according to the RFCs,
501 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamind6e9eec2015-11-18 09:48:55 -0500502 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamind6e9eec2015-11-18 09:48:55 -0500504 {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
505 {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
506 {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamind6e9eec2015-11-18 09:48:55 -0500508 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
509 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
510 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamind6e9eec2015-11-18 09:48:55 -0500512 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700513
David Benjamina1c90a52015-05-30 17:03:14 -0400514 /* server authentication aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500515 {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
516 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
517 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
518 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamina1c90a52015-05-30 17:03:14 -0400520 /* aliases combining key exchange and server authentication */
David Benjamind6e9eec2015-11-18 09:48:55 -0500521 {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0},
522 {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0},
523 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
524 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
525 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0},
526 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamina1c90a52015-05-30 17:03:14 -0400528 /* symmetric encryption aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500529 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
530 {"RC4", ~0u, ~0u, SSL_RC4, ~0u, 0},
531 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
532 {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0},
533 {"AES", ~0u, ~0u, SSL_AES, ~0u, 0},
534 {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0},
535 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamina1c90a52015-05-30 17:03:14 -0400537 /* MAC aliases */
David Benjamind6e9eec2015-11-18 09:48:55 -0500538 {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0},
539 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
540 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0},
541 {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0},
542 {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamindcb6ef02015-11-06 15:35:54 -0500544 /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
545 * same as "SSLv3". */
David Benjamind6e9eec2015-11-18 09:48:55 -0500546 {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
547 {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION},
548 {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamind6e9eec2015-11-18 09:48:55 -0500550 /* Legacy strength classes. */
551 {"MEDIUM", ~0u, ~0u, SSL_RC4, ~0u, 0},
552 {"HIGH", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
553 {"FIPS", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800554};
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamina1c90a52015-05-30 17:03:14 -0400556static const size_t kCipherAliasesLen =
557 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
558
559static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
560 const SSL_CIPHER *a = in_a;
561 const SSL_CIPHER *b = in_b;
562
563 if (a->id > b->id) {
564 return 1;
565 } else if (a->id < b->id) {
566 return -1;
567 } else {
568 return 0;
569 }
570}
571
572static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
573 return ssl_cipher_id_cmp(*a, *b);
574}
575
576const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
577 SSL_CIPHER c;
578
579 c.id = 0x03000000L | value;
580 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
581 ssl_cipher_id_cmp);
582}
David Benjamin0344daf2015-04-08 02:08:01 -0400583
David Benjaminea72bd02014-12-21 21:27:41 -0500584int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
585 size_t *out_mac_secret_len,
586 size_t *out_fixed_iv_len,
587 const SSL_CIPHER *cipher, uint16_t version) {
588 *out_aead = NULL;
589 *out_mac_secret_len = 0;
590 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700591
David Benjaminea72bd02014-12-21 21:27:41 -0500592 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500594 *out_aead = EVP_aead_aes_128_gcm();
595 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 return 1;
597
598 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500599 *out_aead = EVP_aead_aes_256_gcm();
600 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 return 1;
602
Adam Langleyd98dc132015-09-23 16:41:33 -0700603#if !defined(BORINGSSL_ANDROID_SYSTEM)
Brian Smith271777f2015-10-03 13:53:33 -1000604 case SSL_CHACHA20POLY1305_OLD:
Brian Smith3e23e4c2015-10-03 11:38:58 -1000605 *out_aead = EVP_aead_chacha20_poly1305_old();
David Benjaminea72bd02014-12-21 21:27:41 -0500606 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 return 1;
Adam Langleyd98dc132015-09-23 16:41:33 -0700608#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800609
610 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500611 switch (cipher->algorithm_mac) {
612 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500613 if (version == SSL3_VERSION) {
614 *out_aead = EVP_aead_rc4_md5_ssl3();
615 } else {
616 *out_aead = EVP_aead_rc4_md5_tls();
617 }
David Benjaminea72bd02014-12-21 21:27:41 -0500618 *out_mac_secret_len = MD5_DIGEST_LENGTH;
619 return 1;
620 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500621 if (version == SSL3_VERSION) {
622 *out_aead = EVP_aead_rc4_sha1_ssl3();
623 } else {
624 *out_aead = EVP_aead_rc4_sha1_tls();
625 }
David Benjaminea72bd02014-12-21 21:27:41 -0500626 *out_mac_secret_len = SHA_DIGEST_LENGTH;
627 return 1;
628 default:
629 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800631
David Benjaminea72bd02014-12-21 21:27:41 -0500632 case SSL_AES128:
633 switch (cipher->algorithm_mac) {
634 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500635 if (version == SSL3_VERSION) {
636 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
637 *out_fixed_iv_len = 16;
638 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500639 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
640 *out_fixed_iv_len = 16;
641 } else {
642 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
643 }
644 *out_mac_secret_len = SHA_DIGEST_LENGTH;
645 return 1;
646 case SSL_SHA256:
647 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
648 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
649 return 1;
650 default:
651 return 0;
652 }
653
654 case SSL_AES256:
655 switch (cipher->algorithm_mac) {
656 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500657 if (version == SSL3_VERSION) {
658 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
659 *out_fixed_iv_len = 16;
660 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500661 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
662 *out_fixed_iv_len = 16;
663 } else {
664 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
665 }
666 *out_mac_secret_len = SHA_DIGEST_LENGTH;
667 return 1;
668 case SSL_SHA256:
669 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
670 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
671 return 1;
672 case SSL_SHA384:
673 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
674 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
675 return 1;
676 default:
677 return 0;
678 }
679
680 case SSL_3DES:
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_des_ede3_cbc_sha1_ssl3();
685 *out_fixed_iv_len = 8;
686 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500687 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
688 *out_fixed_iv_len = 8;
689 } else {
690 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
691 }
692 *out_mac_secret_len = SHA_DIGEST_LENGTH;
693 return 1;
694 default:
695 return 0;
696 }
697
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700698 case SSL_eNULL:
699 switch (cipher->algorithm_mac) {
700 case SSL_SHA1:
701 if (version == SSL3_VERSION) {
702 *out_aead = EVP_aead_null_sha1_ssl3();
703 } else {
704 *out_aead = EVP_aead_null_sha1_tls();
705 }
706 *out_mac_secret_len = SHA_DIGEST_LENGTH;
707 return 1;
708 default:
709 return 0;
710 }
711
David Benjaminea72bd02014-12-21 21:27:41 -0500712 default:
713 return 0;
714 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800715}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700716
David Benjaminb0883312015-08-06 09:54:13 -0400717const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
718 switch (algorithm_prf) {
719 case SSL_HANDSHAKE_MAC_DEFAULT:
720 return EVP_sha1();
721 case SSL_HANDSHAKE_MAC_SHA256:
722 return EVP_sha256();
723 case SSL_HANDSHAKE_MAC_SHA384:
724 return EVP_sha384();
725 default:
726 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800727 }
Adam Langley95c29f32014-06-20 12:00:00 -0700728}
729
730#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700732
David Benjamin0344daf2015-04-08 02:08:01 -0400733/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
734 * |buf_len| bytes at |buf|. */
735static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
736 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
737 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
738}
739
Adam Langley95c29f32014-06-20 12:00:00 -0700740static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800741 CIPHER_ORDER **tail) {
742 if (curr == *tail) {
743 return;
744 }
745 if (curr == *head) {
746 *head = curr->next;
747 }
748 if (curr->prev != NULL) {
749 curr->prev->next = curr->next;
750 }
751 if (curr->next != NULL) {
752 curr->next->prev = curr->prev;
753 }
754 (*tail)->next = curr;
755 curr->prev = *tail;
756 curr->next = NULL;
757 *tail = curr;
758}
Adam Langley95c29f32014-06-20 12:00:00 -0700759
760static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 CIPHER_ORDER **tail) {
762 if (curr == *head) {
763 return;
764 }
765 if (curr == *tail) {
766 *tail = curr->prev;
767 }
768 if (curr->next != NULL) {
769 curr->next->prev = curr->prev;
770 }
771 if (curr->prev != NULL) {
772 curr->prev->next = curr->next;
773 }
774 (*head)->prev = curr;
775 curr->next = *head;
776 curr->prev = NULL;
777 *head = curr;
778}
Adam Langley95c29f32014-06-20 12:00:00 -0700779
David Benjamin82c9e902014-12-12 15:55:27 -0500780static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 CIPHER_ORDER *co_list,
782 CIPHER_ORDER **head_p,
783 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400784 /* The set of ciphers is static, but some subset may be unsupported by
785 * |ssl_method|, so the list may be smaller. */
786 size_t co_list_num = 0;
787 size_t i;
788 for (i = 0; i < kCiphersLen; i++) {
789 const SSL_CIPHER *cipher = &kCiphers[i];
790 if (ssl_method->supports_cipher(cipher)) {
791 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 co_list[co_list_num].next = NULL;
793 co_list[co_list_num].prev = NULL;
794 co_list[co_list_num].active = 0;
795 co_list[co_list_num].in_group = 0;
796 co_list_num++;
797 }
798 }
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 /* Prepare linked list from list entries. */
801 if (co_list_num > 0) {
802 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 if (co_list_num > 1) {
805 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 for (i = 1; i < co_list_num - 1; i++) {
808 co_list[i].prev = &co_list[i - 1];
809 co_list[i].next = &co_list[i + 1];
810 }
Adam Langley95c29f32014-06-20 12:00:00 -0700811
Adam Langleyfcf25832014-12-18 17:42:32 -0800812 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
813 }
814
815 co_list[co_list_num - 1].next = NULL;
816
817 *head_p = &co_list[0];
818 *tail_p = &co_list[co_list_num - 1];
819 }
820}
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamin0344daf2015-04-08 02:08:01 -0400822/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
823 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
824 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
825 *
826 * - If |cipher_id| is non-zero, only that cipher is selected.
827 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
828 * of that strength.
David Benjamind6e9eec2015-11-18 09:48:55 -0500829 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
David Benjamindcb6ef02015-11-06 15:35:54 -0500830 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800831static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400832 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500833 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
834 int strength_bits, int in_group, CIPHER_ORDER **head_p,
835 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 CIPHER_ORDER *head, *tail, *curr, *next, *last;
837 const SSL_CIPHER *cp;
838 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700839
David Benjamindcb6ef02015-11-06 15:35:54 -0500840 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500841 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400842 /* The rule matches nothing, so bail early. */
843 return;
844 }
845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 if (rule == CIPHER_DEL) {
847 /* needed to maintain sorting between currently deleted ciphers */
848 reverse = 1;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 head = *head_p;
852 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700853
Adam Langleyfcf25832014-12-18 17:42:32 -0800854 if (reverse) {
855 next = tail;
856 last = head;
857 } else {
858 next = head;
859 last = tail;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 curr = NULL;
863 for (;;) {
864 if (curr == last) {
865 break;
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 curr = next;
869 if (curr == NULL) {
870 break;
871 }
Adam Langleye3142a72014-07-24 17:56:48 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 next = reverse ? curr->prev : curr->next;
874 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700875
David Benjamin0344daf2015-04-08 02:08:01 -0400876 /* Selection criteria is either a specific cipher, the value of
877 * |strength_bits|, or the algorithms used. */
878 if (cipher_id != 0) {
879 if (cipher_id != cp->id) {
880 continue;
881 }
882 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500883 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 continue;
885 }
David Benjamin0344daf2015-04-08 02:08:01 -0400886 } else if (!(alg_mkey & cp->algorithm_mkey) ||
887 !(alg_auth & cp->algorithm_auth) ||
888 !(alg_enc & cp->algorithm_enc) ||
889 !(alg_mac & cp->algorithm_mac) ||
David Benjamindcb6ef02015-11-06 15:35:54 -0500890 (min_version != 0 &&
891 SSL_CIPHER_get_min_version(cp) != min_version)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400892 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 }
Adam Langleye3142a72014-07-24 17:56:48 -0700894
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 /* add the cipher if it has not been added yet. */
896 if (rule == CIPHER_ADD) {
897 /* reverse == 0 */
898 if (!curr->active) {
899 ll_append_tail(&head, curr, &tail);
900 curr->active = 1;
901 curr->in_group = in_group;
902 }
903 }
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 /* Move the added cipher to this location */
906 else if (rule == CIPHER_ORD) {
907 /* reverse == 0 */
908 if (curr->active) {
909 ll_append_tail(&head, curr, &tail);
910 curr->in_group = 0;
911 }
912 } else if (rule == CIPHER_DEL) {
913 /* reverse == 1 */
914 if (curr->active) {
915 /* most recently deleted ciphersuites get best positions
916 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
917 * works in reverse to maintain the order) */
918 ll_append_head(&head, curr, &tail);
919 curr->active = 0;
920 curr->in_group = 0;
921 }
922 } else if (rule == CIPHER_KILL) {
923 /* reverse == 0 */
924 if (head == curr) {
925 head = curr->next;
926 } else {
927 curr->prev->next = curr->next;
928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 if (tail == curr) {
931 tail = curr->prev;
932 }
933 curr->active = 0;
934 if (curr->next != NULL) {
935 curr->next->prev = curr->prev;
936 }
937 if (curr->prev != NULL) {
938 curr->prev->next = curr->next;
939 }
940 curr->next = NULL;
941 curr->prev = NULL;
942 }
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 *head_p = head;
946 *tail_p = tail;
947}
Adam Langley95c29f32014-06-20 12:00:00 -0700948
949static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 CIPHER_ORDER **tail_p) {
951 int max_strength_bits, i, *number_uses;
952 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 /* This routine sorts the ciphers with descending strength. The sorting must
955 * keep the pre-sorted sequence, so we apply the normal sorting routine as
956 * '+' movement to the end of the list. */
957 max_strength_bits = 0;
958 curr = *head_p;
959 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500960 if (curr->active &&
961 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
962 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 }
964 curr = curr->next;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
968 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400969 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 return 0;
971 }
972 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 /* Now find the strength_bits values actually used. */
975 curr = *head_p;
976 while (curr != NULL) {
977 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500978 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 }
980 curr = curr->next;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 /* Go through the list of used strength_bits values in descending order. */
984 for (i = max_strength_bits; i >= 0; i--) {
985 if (number_uses[i] > 0) {
David Benjamind6e9eec2015-11-18 09:48:55 -0500986 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 -0800987 }
988 }
989
990 OPENSSL_free(number_uses);
991 return 1;
992}
Adam Langley95c29f32014-06-20 12:00:00 -0700993
David Benjamin0344daf2015-04-08 02:08:01 -0400994static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
995 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400997 CIPHER_ORDER **tail_p) {
David Benjamind6e9eec2015-11-18 09:48:55 -0500998 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500999 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 const char *l, *buf;
David Benjamindcb6ef02015-11-06 15:35:54 -05001001 int multi, skip_rule, rule, retval, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001002 size_t j, buf_len;
1003 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 retval = 1;
1007 l = rule_str;
1008 for (;;) {
1009 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 if (ch == '\0') {
1012 break; /* done */
1013 }
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (in_group) {
1016 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 if (*tail_p) {
1018 (*tail_p)->in_group = 0;
1019 }
1020 in_group = 0;
1021 l++;
1022 continue;
1023 }
David Benjamin37d92462014-09-20 17:54:24 -04001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 if (ch == '|') {
1026 rule = CIPHER_ADD;
1027 l++;
1028 continue;
1029 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1030 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001031 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001032 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 break;
1034 } else {
1035 rule = CIPHER_ADD;
1036 }
1037 } else if (ch == '-') {
1038 rule = CIPHER_DEL;
1039 l++;
1040 } else if (ch == '+') {
1041 rule = CIPHER_ORD;
1042 l++;
1043 } else if (ch == '!') {
1044 rule = CIPHER_KILL;
1045 l++;
1046 } else if (ch == '@') {
1047 rule = CIPHER_SPECIAL;
1048 l++;
1049 } else if (ch == '[') {
1050 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001051 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001052 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 break;
1054 }
1055 in_group = 1;
1056 has_group = 1;
1057 l++;
1058 continue;
1059 } else {
1060 rule = CIPHER_ADD;
1061 }
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 /* If preference groups are enabled, the only legal operator is +.
1064 * Otherwise the in_group bits will get mixed up. */
1065 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001067 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 break;
1069 }
Adam Langley95c29f32014-06-20 12:00:00 -07001070
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 if (ITEM_SEP(ch)) {
1072 l++;
1073 continue;
1074 }
Adam Langley95c29f32014-06-20 12:00:00 -07001075
David Benjamin0344daf2015-04-08 02:08:01 -04001076 multi = 0;
1077 cipher_id = 0;
1078 alg_mkey = ~0u;
1079 alg_auth = ~0u;
1080 alg_enc = ~0u;
1081 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001082 min_version = 0;
1083 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001084
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 for (;;) {
1086 ch = *l;
1087 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001088 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1090 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1091 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001092 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
David Benjamin0344daf2015-04-08 02:08:01 -04001095 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 /* We hit something we cannot deal with, it is no command or separator
1097 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001098 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001099 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 l++;
1101 break;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 break;
1106 }
David Benjamin0344daf2015-04-08 02:08:01 -04001107
1108 /* Look for a matching exact cipher. These aren't allowed in multipart
1109 * rules. */
1110 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001111 for (j = 0; j < kCiphersLen; j++) {
1112 const SSL_CIPHER *cipher = &kCiphers[j];
1113 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001114 cipher_id = cipher->id;
1115 break;
1116 }
1117 }
1118 }
1119 if (cipher_id == 0) {
1120 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001121 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001122 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1123 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1124 alg_auth &= kCipherAliases[j].algorithm_auth;
1125 alg_enc &= kCipherAliases[j].algorithm_enc;
1126 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001127
1128 if (min_version != 0 &&
1129 min_version != kCipherAliases[j].min_version) {
1130 skip_rule = 1;
1131 } else {
1132 min_version = kCipherAliases[j].min_version;
1133 }
David Benjamin0344daf2015-04-08 02:08:01 -04001134 break;
1135 }
1136 }
David Benjamina1c90a52015-05-30 17:03:14 -04001137 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001138 skip_rule = 1;
David Benjamin0344daf2015-04-08 02:08:01 -04001139 }
1140 }
1141
1142 /* Check for a multipart rule. */
1143 if (ch != '+') {
1144 break;
1145 }
1146 l++;
1147 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 /* Ok, we have the rule, now apply it. */
1151 if (rule == CIPHER_SPECIAL) {
1152 /* special command */
1153 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001154 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 ok = ssl_cipher_strength_sort(head_p, tail_p);
1156 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001157 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 }
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (ok == 0) {
1161 retval = 0;
1162 }
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 /* We do not support any "multi" options together with "@", so throw away
1165 * the rest of the command, if any left, until end or ':' is found. */
1166 while (*l != '\0' && !ITEM_SEP(*l)) {
1167 l++;
1168 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001169 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001171 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 }
1173 }
Adam Langley95c29f32014-06-20 12:00:00 -07001174
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001176 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 retval = 0;
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 return retval;
1181}
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183STACK_OF(SSL_CIPHER) *
1184ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001185 struct ssl_cipher_preference_list_st **out_cipher_list,
1186 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1187 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001188 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1190 const char *rule_p;
1191 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 uint8_t *in_group_flags = NULL;
1193 unsigned int num_in_group_flags = 0;
1194 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyfcf25832014-12-18 17:42:32 -08001196 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001197 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 return NULL;
1199 }
David Benjamin5213df42014-08-20 14:19:54 -04001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 /* Now we have to collect the available ciphers from the compiled in ciphers.
1202 * We cannot get more than the number compiled in, so it is used for
1203 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001204 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001206 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 return NULL;
1208 }
Adam Langley95c29f32014-06-20 12:00:00 -07001209
David Benjamina1c90a52015-05-30 17:03:14 -04001210 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001211
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 /* Now arrange all ciphers by preference:
1213 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1216 * key exchange mechanisms */
David Benjamind6e9eec2015-11-18 09:48:55 -05001217 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001219 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
1220 &head, &tail);
1221 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0,
1222 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1225 * CHACHA20 unless there is hardware support for fast and constant-time
1226 * AES_GCM. */
1227 if (EVP_has_aes_hardware()) {
David Benjamind6e9eec2015-11-18 09:48:55 -05001228 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1229 &head, &tail);
1230 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1231 &head, &tail);
1232 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001233 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 } else {
David Benjamind6e9eec2015-11-18 09:48:55 -05001235 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001236 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001237 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1238 &head, &tail);
1239 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0,
1240 &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 }
Adam Langley95c29f32014-06-20 12:00:00 -07001242
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1244 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001245 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 &head, &tail);
David Benjamind6e9eec2015-11-18 09:48:55 -05001247 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0,
1248 &head, &tail);
1249 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, 0, CIPHER_ADD, -1, 0,
1250 &head, &tail);
1251 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, 0, CIPHER_ADD, -1, 0,
1252 &head, &tail);
1253 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1254 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001255
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 /* Temporarily enable everything else for sorting */
David Benjamind6e9eec2015-11-18 09:48:55 -05001257 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head,
1258 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001259
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 /* Move ciphers without forward secrecy to the end. */
David Benjamind6e9eec2015-11-18 09:48:55 -05001261 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001262 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001263
Adam Langleyfcf25832014-12-18 17:42:32 -08001264 /* Now disable everything (maintaining the ordering!) */
David Benjamind6e9eec2015-11-18 09:48:55 -05001265 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head,
1266 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001267
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 /* If the rule_string begins with DEFAULT, apply the default rule before
1269 * using the (possibly available) additional rules. */
1270 ok = 1;
1271 rule_p = rule_str;
1272 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001273 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1274 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 rule_p += 7;
1276 if (*rule_p == ':') {
1277 rule_p++;
1278 }
1279 }
Adam Langley858a88d2014-06-20 12:00:00 -07001280
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001282 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 }
Adam Langley95c29f32014-06-20 12:00:00 -07001284
Adam Langleyfcf25832014-12-18 17:42:32 -08001285 if (!ok) {
1286 goto err;
1287 }
1288
1289 /* Allocate new "cipherstack" for the result, return with error
1290 * if we cannot get one. */
1291 cipherstack = sk_SSL_CIPHER_new_null();
1292 if (cipherstack == NULL) {
1293 goto err;
1294 }
1295
David Benjamina1c90a52015-05-30 17:03:14 -04001296 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001297 if (!in_group_flags) {
1298 goto err;
1299 }
1300
1301 /* The cipher selection for the list is done. The ciphers are added
1302 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1303 for (curr = head; curr != NULL; curr = curr->next) {
1304 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001305 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1306 goto err;
1307 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001308 in_group_flags[num_in_group_flags++] = curr->in_group;
1309 }
1310 }
1311 OPENSSL_free(co_list); /* Not needed any longer */
1312 co_list = NULL;
1313
1314 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1315 if (tmp_cipher_list == NULL) {
1316 goto err;
1317 }
1318 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1319 if (!pref_list) {
1320 goto err;
1321 }
1322 pref_list->ciphers = cipherstack;
1323 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1324 if (!pref_list->in_group_flags) {
1325 goto err;
1326 }
1327 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1328 OPENSSL_free(in_group_flags);
1329 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001330 if (*out_cipher_list != NULL) {
1331 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001332 }
David Benjamin71f07942015-04-08 02:36:59 -04001333 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 pref_list = NULL;
1335
David Benjamin71f07942015-04-08 02:36:59 -04001336 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001337 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001338 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001340 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1341 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001342
David Benjamin71f07942015-04-08 02:36:59 -04001343 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 } else {
1345 sk_SSL_CIPHER_free(tmp_cipher_list);
1346 tmp_cipher_list = NULL;
1347 }
1348
1349 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001350
1351err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001352 OPENSSL_free(co_list);
1353 OPENSSL_free(in_group_flags);
1354 sk_SSL_CIPHER_free(cipherstack);
1355 sk_SSL_CIPHER_free(tmp_cipher_list);
1356 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 OPENSSL_free(pref_list->in_group_flags);
1358 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001359 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001360 return NULL;
1361}
Adam Langley95c29f32014-06-20 12:00:00 -07001362
David Benjamin71f07942015-04-08 02:36:59 -04001363uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1364
David Benjamina1c90a52015-05-30 17:03:14 -04001365uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1366 uint32_t id = cipher->id;
1367 /* All ciphers are SSLv3. */
1368 assert((id & 0xff000000) == 0x03000000);
1369 return id & 0xffff;
1370}
1371
David Benjamin71f07942015-04-08 02:36:59 -04001372int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1373 return (cipher->algorithm_enc & SSL_AES) != 0;
1374}
1375
1376int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1377 return (cipher->algorithm_mac & SSL_MD5) != 0;
1378}
1379
David Benjaminef793f42015-11-05 18:16:27 -05001380int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1381 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1382}
1383
David Benjamin71f07942015-04-08 02:36:59 -04001384int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001385 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001386}
1387
David Benjaminef793f42015-11-05 18:16:27 -05001388int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1389 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1390}
1391
Adam Langleyb00061c2015-11-16 17:44:52 -08001392int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1393 return (cipher->algorithm_enc & SSL_AES128) != 0;
1394}
1395
1396int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1397 return (cipher->algorithm_enc & SSL_AES256) != 0;
1398}
1399
David Benjamin51a01a52015-10-29 13:19:56 -04001400int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
Brian Smith271777f2015-10-03 13:53:33 -10001401 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305_OLD) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001402}
1403
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001404int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1405 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1406}
1407
Adam Langleyd7fe75c2015-09-18 15:40:48 -07001408int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) {
1409 return (cipher->algorithm_enc & SSL_RC4) != 0;
1410}
1411
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001412int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1413 /* Neither stream cipher nor AEAD. */
1414 return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 &&
1415 cipher->algorithm_mac != SSL_AEAD;
1416}
1417
David Benjaminef793f42015-11-05 18:16:27 -05001418int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1419 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1420}
1421
1422uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001423 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1424 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1425 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001426 return TLS1_2_VERSION;
1427 }
1428 return SSL3_VERSION;
1429}
1430
David Benjamin71f07942015-04-08 02:36:59 -04001431/* return the actual cipher being used */
1432const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1433 if (cipher != NULL) {
1434 return cipher->name;
1435 }
1436
1437 return "(NONE)";
1438}
1439
1440const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1441 if (cipher == NULL) {
1442 return "";
1443 }
1444
1445 switch (cipher->algorithm_mkey) {
1446 case SSL_kRSA:
1447 return "RSA";
1448
1449 case SSL_kDHE:
1450 switch (cipher->algorithm_auth) {
1451 case SSL_aRSA:
1452 return "DHE_RSA";
1453 default:
1454 assert(0);
1455 return "UNKNOWN";
1456 }
1457
1458 case SSL_kECDHE:
1459 switch (cipher->algorithm_auth) {
1460 case SSL_aECDSA:
1461 return "ECDHE_ECDSA";
1462 case SSL_aRSA:
1463 return "ECDHE_RSA";
1464 case SSL_aPSK:
1465 return "ECDHE_PSK";
1466 default:
1467 assert(0);
1468 return "UNKNOWN";
1469 }
1470
1471 case SSL_kPSK:
1472 assert(cipher->algorithm_auth == SSL_aPSK);
1473 return "PSK";
1474
1475 default:
1476 assert(0);
1477 return "UNKNOWN";
1478 }
1479}
1480
1481static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1482 switch (cipher->algorithm_enc) {
1483 case SSL_3DES:
1484 return "3DES_EDE_CBC";
1485 case SSL_RC4:
1486 return "RC4";
1487 case SSL_AES128:
1488 return "AES_128_CBC";
1489 case SSL_AES256:
1490 return "AES_256_CBC";
1491 case SSL_AES128GCM:
1492 return "AES_128_GCM";
1493 case SSL_AES256GCM:
1494 return "AES_256_GCM";
Brian Smith271777f2015-10-03 13:53:33 -10001495 case SSL_CHACHA20POLY1305_OLD:
David Benjamin71f07942015-04-08 02:36:59 -04001496 return "CHACHA20_POLY1305";
1497 break;
1498 default:
1499 assert(0);
1500 return "UNKNOWN";
1501 }
1502}
1503
1504static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001505 switch (cipher->algorithm_prf) {
1506 case SSL_HANDSHAKE_MAC_DEFAULT:
1507 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1508 * only ever MD5 or SHA-1. */
1509 switch (cipher->algorithm_mac) {
1510 case SSL_MD5:
1511 return "MD5";
1512 case SSL_SHA1:
1513 return "SHA";
1514 }
1515 break;
1516 case SSL_HANDSHAKE_MAC_SHA256:
1517 return "SHA256";
1518 case SSL_HANDSHAKE_MAC_SHA384:
1519 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001520 }
David Benjaminb0883312015-08-06 09:54:13 -04001521 assert(0);
1522 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001523}
1524
1525char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1526 if (cipher == NULL) {
1527 return NULL;
1528 }
1529
1530 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1531 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1532 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1533
1534 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1535 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1536 strlen(prf_name) + 1;
1537 char *ret = OPENSSL_malloc(len);
1538 if (ret == NULL) {
1539 return NULL;
1540 }
1541 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1542 BUF_strlcat(ret, kx_name, len) >= len ||
1543 BUF_strlcat(ret, "_WITH_", len) >= len ||
1544 BUF_strlcat(ret, enc_name, len) >= len ||
1545 BUF_strlcat(ret, "_", len) >= len ||
1546 BUF_strlcat(ret, prf_name, len) >= len) {
1547 assert(0);
1548 OPENSSL_free(ret);
1549 return NULL;
1550 }
1551 assert(strlen(ret) + 1 == len);
1552 return ret;
1553}
1554
1555int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1556 if (cipher == NULL) {
1557 return 0;
1558 }
1559
David Benjamin9f2e2772015-11-18 09:59:43 -05001560 int alg_bits, strength_bits;
1561 switch (cipher->algorithm_enc) {
1562 case SSL_AES128:
1563 case SSL_AES128GCM:
1564 case SSL_RC4:
1565 alg_bits = 128;
1566 strength_bits = 128;
1567 break;
1568
1569 case SSL_AES256:
1570 case SSL_AES256GCM:
1571#if !defined(BORINGSSL_ANDROID_SYSTEM)
1572 case SSL_CHACHA20POLY1305_OLD:
1573#endif
1574 alg_bits = 256;
1575 strength_bits = 256;
1576 break;
1577
1578 case SSL_3DES:
1579 alg_bits = 168;
1580 strength_bits = 112;
1581 break;
1582
1583 case SSL_eNULL:
1584 alg_bits = 0;
1585 strength_bits = 0;
1586 break;
1587
1588 default:
1589 assert(0);
1590 alg_bits = 0;
1591 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001592 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001593
1594 if (out_alg_bits != NULL) {
1595 *out_alg_bits = alg_bits;
1596 }
1597 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001598}
1599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1601 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001603 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1604 static const char *format = "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 alg_mkey = cipher->algorithm_mkey;
1607 alg_auth = cipher->algorithm_auth;
1608 alg_enc = cipher->algorithm_enc;
1609 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001610
Adam Langleyfcf25832014-12-18 17:42:32 -08001611 switch (alg_mkey) {
1612 case SSL_kRSA:
1613 kx = "RSA";
1614 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamin7061e282015-03-19 11:10:48 -04001616 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 kx = "DH";
1618 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001619
David Benjamin7061e282015-03-19 11:10:48 -04001620 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 kx = "ECDH";
1622 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001623
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 case SSL_kPSK:
1625 kx = "PSK";
1626 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001627
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 default:
1629 kx = "unknown";
1630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 switch (alg_auth) {
1633 case SSL_aRSA:
1634 au = "RSA";
1635 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001636
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 case SSL_aECDSA:
1638 au = "ECDSA";
1639 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 case SSL_aPSK:
1642 au = "PSK";
1643 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 default:
1646 au = "unknown";
1647 break;
1648 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 switch (alg_enc) {
1651 case SSL_3DES:
1652 enc = "3DES(168)";
1653 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 case SSL_RC4:
1656 enc = "RC4(128)";
1657 break;
1658
1659 case SSL_AES128:
1660 enc = "AES(128)";
1661 break;
1662
1663 case SSL_AES256:
1664 enc = "AES(256)";
1665 break;
1666
1667 case SSL_AES128GCM:
1668 enc = "AESGCM(128)";
1669 break;
1670
1671 case SSL_AES256GCM:
1672 enc = "AESGCM(256)";
1673 break;
1674
Brian Smith271777f2015-10-03 13:53:33 -10001675 case SSL_CHACHA20POLY1305_OLD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 enc = "ChaCha20-Poly1305";
1677 break;
1678
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001679 case SSL_eNULL:
1680 enc="None";
1681 break;
1682
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 default:
1684 enc = "unknown";
1685 break;
1686 }
1687
1688 switch (alg_mac) {
1689 case SSL_MD5:
1690 mac = "MD5";
1691 break;
1692
1693 case SSL_SHA1:
1694 mac = "SHA1";
1695 break;
1696
1697 case SSL_SHA256:
1698 mac = "SHA256";
1699 break;
1700
1701 case SSL_SHA384:
1702 mac = "SHA384";
1703 break;
1704
1705 case SSL_AEAD:
1706 mac = "AEAD";
1707 break;
1708
1709 default:
1710 mac = "unknown";
1711 break;
1712 }
1713
1714 if (buf == NULL) {
1715 len = 128;
1716 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001717 if (buf == NULL) {
1718 return NULL;
1719 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 } else if (len < 128) {
1721 return "Buffer too small";
1722 }
1723
David Benjamindcb6ef02015-11-06 15:35:54 -05001724 BIO_snprintf(buf, len, format, cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 return buf;
1726}
1727
David Benjamin71f07942015-04-08 02:36:59 -04001728const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1729 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001730}
1731
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001732COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001733
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001734int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001735
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001736const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001737
David Benjamind1d80782015-07-05 11:54:09 -04001738int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001739 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001742 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001744 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 }
Adam Langley95c29f32014-06-20 12:00:00 -07001746
David Benjamind1d80782015-07-05 11:54:09 -04001747 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001748}
David Benjamin9c651c92014-07-12 13:27:45 -04001749
Adam Langleyfcf25832014-12-18 17:42:32 -08001750int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamindf0905a2015-09-19 09:58:31 -04001751 /* PSK-authenticated ciphers do not use a certificate. (RSA_PSK is not
1752 * supported.) */
1753 if (cipher->algorithm_auth & SSL_aPSK) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 return 0;
1755 }
1756
1757 /* All other ciphers include it. */
1758 return 1;
1759}
1760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1762 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001763 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001764 return 1;
1765 }
1766
1767 /* It is optional in all others. */
1768 return 0;
1769}
David Benjaminb8d28cf2015-07-28 21:34:45 -04001770
1771size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1772 size_t block_size;
1773 switch (cipher->algorithm_enc) {
1774 case SSL_3DES:
1775 block_size = 8;
1776 break;
1777 case SSL_AES128:
1778 case SSL_AES256:
1779 block_size = 16;
1780 break;
1781 default:
1782 return 0;
1783 }
1784
1785 size_t mac_len;
1786 switch (cipher->algorithm_mac) {
1787 case SSL_MD5:
1788 mac_len = MD5_DIGEST_LENGTH;
1789 break;
1790 case SSL_SHA1:
1791 mac_len = SHA_DIGEST_LENGTH;
1792 break;
1793 default:
1794 return 0;
1795 }
1796
1797 size_t ret = 1 + mac_len;
1798 ret += block_size - (ret % block_size);
1799 return ret;
1800}