blob: fc8d2a98c42e5fb8de2c6ace829c960903feb397 [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 Benjamindcb6ef02015-11-06 15:35:54 -0500163 SSL_eNULL, SSL_SHA1, SSL_FIPS, SSL_HANDSHAKE_MAC_DEFAULT, 0, 0,
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 Benjamindcb6ef02015-11-06 15:35:54 -0500169 SSL_RC4, SSL_MD5, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
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 Benjamindcb6ef02015-11-06 15:35:54 -0500175 SSL_RC4, SSL_SHA1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
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 Benjamindcb6ef02015-11-06 15:35:54 -0500181 SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400182 SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
183 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500191 SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400192 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
193 },
194
195 /* Cipher 33 */
196 {
197 TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500198 SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400199 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
200 },
201
202 /* Cipher 35 */
203 {
204 TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500205 SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400206 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
207 },
208
209 /* Cipher 39 */
210 {
211 TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500212 SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400213 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
214 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500222 SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_HIGH | SSL_FIPS,
223 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
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 Benjamina1c90a52015-05-30 17:03:14 -0400230 SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
231 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500237 SSL_SHA256, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400238 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
239 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500245 SSL_SHA256, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400246 SSL_HANDSHAKE_MAC_SHA256, 256, 256,
247 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500254 SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_MEDIUM,
David Benjamina1c90a52015-05-30 17:03:14 -0400255 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
256 },
257
258 /* Cipher 8C */
259 {
260 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500261 SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400262 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
263 },
264
265 /* Cipher 8D */
266 {
267 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500268 SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400269 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
270 },
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 Benjamindcb6ef02015-11-06 15:35:54 -0500278 SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400279 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400280 128, 128,
281 },
282
283 /* Cipher 9D */
284 {
285 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
286 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
David Benjamindcb6ef02015-11-06 15:35:54 -0500287 SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400288 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400289 256, 256,
290 },
291
292 /* Cipher 9E */
293 {
294 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
295 TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
David Benjamindcb6ef02015-11-06 15:35:54 -0500296 SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400297 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400298 128, 128,
299 },
300
301 /* Cipher 9F */
302 {
303 TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
304 TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
David Benjamindcb6ef02015-11-06 15:35:54 -0500305 SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400306 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400307 256, 256,
308 },
309
310 /* Cipher C007 */
311 {
312 TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
313 TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
David Benjamindcb6ef02015-11-06 15:35:54 -0500314 SSL_SHA1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
David Benjamina1c90a52015-05-30 17:03:14 -0400315 128,
316 },
317
318 /* Cipher C009 */
319 {
320 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
321 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500322 SSL_AES128, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400323 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
324 },
325
326 /* Cipher C00A */
327 {
328 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
329 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500330 SSL_AES256, SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400331 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
332 },
333
334 /* Cipher C011 */
335 {
336 TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500337 SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_MEDIUM,
David Benjamina1c90a52015-05-30 17:03:14 -0400338 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
339 },
340
341 /* Cipher C013 */
342 {
343 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
344 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
David Benjamindcb6ef02015-11-06 15:35:54 -0500345 SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400346 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
347 },
348
349 /* Cipher C014 */
350 {
351 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
352 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
David Benjamindcb6ef02015-11-06 15:35:54 -0500353 SSL_SHA1, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400354 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
355 },
356
357
358 /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
359
360 /* Cipher C023 */
361 {
362 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
363 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500364 SSL_AES128, SSL_SHA256, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400365 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
366 },
367
368 /* Cipher C024 */
369 {
370 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
371 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500372 SSL_AES256, SSL_SHA384, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400373 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
374 },
375
376 /* Cipher C027 */
377 {
378 TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
379 TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
David Benjamindcb6ef02015-11-06 15:35:54 -0500380 SSL_SHA256, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400381 SSL_HANDSHAKE_MAC_SHA256, 128, 128,
382 },
383
384 /* Cipher C028 */
385 {
386 TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
387 TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
David Benjamindcb6ef02015-11-06 15:35:54 -0500388 SSL_SHA384, SSL_HIGH | SSL_FIPS,
David Benjamina1c90a52015-05-30 17:03:14 -0400389 SSL_HANDSHAKE_MAC_SHA384, 256, 256,
390 },
391
392
393 /* GCM based TLS v1.2 ciphersuites from RFC5289 */
394
395 /* Cipher C02B */
396 {
397 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
398 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500399 SSL_AES128GCM, SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400400 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400401 128, 128,
402 },
403
404 /* Cipher C02C */
405 {
406 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
407 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500408 SSL_AES256GCM, SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400409 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400410 256, 256,
411 },
412
413 /* Cipher C02F */
414 {
415 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
416 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500417 SSL_AES128GCM, SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400418 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400419 128, 128,
420 },
421
422 /* Cipher C030 */
423 {
424 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
425 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500426 SSL_AES256GCM, SSL_AEAD, SSL_HIGH | SSL_FIPS,
David Benjaminb2a985b2015-06-21 15:13:57 -0400427 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400428 256, 256,
429 },
430
Adam Langley85bc5602015-06-09 09:54:04 -0700431 /* ECDHE-PSK cipher suites. */
432
433 /* Cipher C035 */
434 {
435 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
436 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500437 SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_HIGH | SSL_FIPS,
Adam Langley85bc5602015-06-09 09:54:04 -0700438 SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
439 },
440
441 /* Cipher C036 */
442 {
443 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
444 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500445 SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_HIGH | SSL_FIPS,
Adam Langley85bc5602015-06-09 09:54:04 -0700446 SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
447 },
448
Adam Langleyd98dc132015-09-23 16:41:33 -0700449#if !defined(BORINGSSL_ANDROID_SYSTEM)
Adam Langley85bc5602015-06-09 09:54:04 -0700450 /* ChaCha20-Poly1305 cipher suites. */
451
David Benjamina1c90a52015-05-30 17:03:14 -0400452 {
Brian Smith271777f2015-10-03 13:53:33 -1000453 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD,
454 TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aRSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500455 SSL_CHACHA20POLY1305_OLD, SSL_AEAD, SSL_HIGH,
David Benjamina1c90a52015-05-30 17:03:14 -0400456 SSL_HANDSHAKE_MAC_SHA256,
David Benjaminbe8a8692015-08-21 13:51:56 -0400457 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400458 },
459
460 {
Brian Smith271777f2015-10-03 13:53:33 -1000461 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD,
462 TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aECDSA,
David Benjamindcb6ef02015-11-06 15:35:54 -0500463 SSL_CHACHA20POLY1305_OLD, SSL_AEAD, SSL_HIGH,
David Benjamina1c90a52015-05-30 17:03:14 -0400464 SSL_HANDSHAKE_MAC_SHA256,
David Benjaminbe8a8692015-08-21 13:51:56 -0400465 256, 256,
David Benjamina1c90a52015-05-30 17:03:14 -0400466 },
Adam Langleyd98dc132015-09-23 16:41:33 -0700467#endif
David Benjamina1c90a52015-05-30 17:03:14 -0400468};
469
470static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472#define CIPHER_ADD 1
473#define CIPHER_KILL 2
474#define CIPHER_DEL 3
475#define CIPHER_ORD 4
476#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478typedef struct cipher_order_st {
479 const SSL_CIPHER *cipher;
480 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 int in_group;
482 struct cipher_order_st *next, *prev;
483} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin0344daf2015-04-08 02:08:01 -0400485typedef struct cipher_alias_st {
486 /* name is the name of the cipher alias. */
487 const char *name;
488
489 /* The following fields are bitmasks for the corresponding fields on
490 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
491 * bit corresponding to the cipher's value is set to 1. If any bitmask is
492 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
493 uint32_t algorithm_mkey;
494 uint32_t algorithm_auth;
495 uint32_t algorithm_enc;
496 uint32_t algorithm_mac;
David Benjamin0344daf2015-04-08 02:08:01 -0400497 uint32_t algo_strength;
David Benjamindcb6ef02015-11-06 15:35:54 -0500498
499 /* min_version, if non-zero, matches all ciphers which were added in that
500 * particular protocol version. */
501 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400502} CIPHER_ALIAS;
503
David Benjamina1c90a52015-05-30 17:03:14 -0400504static const CIPHER_ALIAS kCipherAliases[] = {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700505 /* "ALL" doesn't include eNULL (must be specifically enabled) */
David Benjamindcb6ef02015-11-06 15:35:54 -0500506 {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamina1c90a52015-05-30 17:03:14 -0400508 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700509
David Benjamina1c90a52015-05-30 17:03:14 -0400510 /* key exchange aliases
511 * (some of those using only a single bit here combine
512 * multiple key exchange algs according to the RFCs,
513 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamindcb6ef02015-11-06 15:35:54 -0500514 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamindcb6ef02015-11-06 15:35:54 -0500516 {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, ~0u, 0},
517 {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, ~0u, 0},
518 {"DH", SSL_kDHE, ~0u, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamindcb6ef02015-11-06 15:35:54 -0500520 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0},
521 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0},
522 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700523
David Benjamindcb6ef02015-11-06 15:35:54 -0500524 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700525
David Benjamina1c90a52015-05-30 17:03:14 -0400526 /* server authentication aliases */
David Benjamindcb6ef02015-11-06 15:35:54 -0500527 {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, 0},
528 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, 0},
529 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, 0},
530 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamina1c90a52015-05-30 17:03:14 -0400532 /* aliases combining key exchange and server authentication */
David Benjamindcb6ef02015-11-06 15:35:54 -0500533 {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, ~0u, 0},
534 {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, ~0u, 0},
535 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0},
536 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0},
537 {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, 0},
538 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamina1c90a52015-05-30 17:03:14 -0400540 /* symmetric encryption aliases */
David Benjamindcb6ef02015-11-06 15:35:54 -0500541 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, ~0u, 0},
542 {"RC4", ~0u, ~0u, SSL_RC4, ~0u, ~0u, 0},
543 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, 0},
544 {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, 0},
545 {"AES", ~0u, ~0u, SSL_AES, ~0u, ~0u, 0},
546 {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, 0},
547 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamina1c90a52015-05-30 17:03:14 -0400549 /* MAC aliases */
David Benjamindcb6ef02015-11-06 15:35:54 -0500550 {"MD5", ~0u, ~0u, ~0u, SSL_MD5, ~0u, 0},
551 {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, 0},
552 {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, 0},
553 {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, ~0u, 0},
554 {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamindcb6ef02015-11-06 15:35:54 -0500556 /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the
557 * same as "SSLv3". */
558 {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, SSL3_VERSION},
559 {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, SSL3_VERSION},
560 {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700561
David Benjamina1c90a52015-05-30 17:03:14 -0400562 /* strength classes */
David Benjamindcb6ef02015-11-06 15:35:54 -0500563 {"MEDIUM", ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM, 0},
564 {"HIGH", ~0u, ~0u, ~0u, ~0u, SSL_HIGH, 0},
David Benjamina1c90a52015-05-30 17:03:14 -0400565 /* FIPS 140-2 approved ciphersuite */
David Benjamindcb6ef02015-11-06 15:35:54 -0500566 {"FIPS", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_FIPS, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800567};
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamina1c90a52015-05-30 17:03:14 -0400569static const size_t kCipherAliasesLen =
570 sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
571
572static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
573 const SSL_CIPHER *a = in_a;
574 const SSL_CIPHER *b = in_b;
575
576 if (a->id > b->id) {
577 return 1;
578 } else if (a->id < b->id) {
579 return -1;
580 } else {
581 return 0;
582 }
583}
584
585static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
586 return ssl_cipher_id_cmp(*a, *b);
587}
588
589const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
590 SSL_CIPHER c;
591
592 c.id = 0x03000000L | value;
593 return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
594 ssl_cipher_id_cmp);
595}
David Benjamin0344daf2015-04-08 02:08:01 -0400596
David Benjaminea72bd02014-12-21 21:27:41 -0500597int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
598 size_t *out_mac_secret_len,
599 size_t *out_fixed_iv_len,
600 const SSL_CIPHER *cipher, uint16_t version) {
601 *out_aead = NULL;
602 *out_mac_secret_len = 0;
603 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700604
David Benjaminea72bd02014-12-21 21:27:41 -0500605 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500607 *out_aead = EVP_aead_aes_128_gcm();
608 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 return 1;
610
611 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500612 *out_aead = EVP_aead_aes_256_gcm();
613 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 return 1;
615
Adam Langleyd98dc132015-09-23 16:41:33 -0700616#if !defined(BORINGSSL_ANDROID_SYSTEM)
Brian Smith271777f2015-10-03 13:53:33 -1000617 case SSL_CHACHA20POLY1305_OLD:
Brian Smith3e23e4c2015-10-03 11:38:58 -1000618 *out_aead = EVP_aead_chacha20_poly1305_old();
David Benjaminea72bd02014-12-21 21:27:41 -0500619 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 return 1;
Adam Langleyd98dc132015-09-23 16:41:33 -0700621#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800622
623 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500624 switch (cipher->algorithm_mac) {
625 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500626 if (version == SSL3_VERSION) {
627 *out_aead = EVP_aead_rc4_md5_ssl3();
628 } else {
629 *out_aead = EVP_aead_rc4_md5_tls();
630 }
David Benjaminea72bd02014-12-21 21:27:41 -0500631 *out_mac_secret_len = MD5_DIGEST_LENGTH;
632 return 1;
633 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500634 if (version == SSL3_VERSION) {
635 *out_aead = EVP_aead_rc4_sha1_ssl3();
636 } else {
637 *out_aead = EVP_aead_rc4_sha1_tls();
638 }
David Benjaminea72bd02014-12-21 21:27:41 -0500639 *out_mac_secret_len = SHA_DIGEST_LENGTH;
640 return 1;
641 default:
642 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800644
David Benjaminea72bd02014-12-21 21:27:41 -0500645 case SSL_AES128:
646 switch (cipher->algorithm_mac) {
647 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500648 if (version == SSL3_VERSION) {
649 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
650 *out_fixed_iv_len = 16;
651 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500652 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
653 *out_fixed_iv_len = 16;
654 } else {
655 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
656 }
657 *out_mac_secret_len = SHA_DIGEST_LENGTH;
658 return 1;
659 case SSL_SHA256:
660 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
661 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
662 return 1;
663 default:
664 return 0;
665 }
666
667 case SSL_AES256:
668 switch (cipher->algorithm_mac) {
669 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500670 if (version == SSL3_VERSION) {
671 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
672 *out_fixed_iv_len = 16;
673 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500674 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
675 *out_fixed_iv_len = 16;
676 } else {
677 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
678 }
679 *out_mac_secret_len = SHA_DIGEST_LENGTH;
680 return 1;
681 case SSL_SHA256:
682 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
683 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
684 return 1;
685 case SSL_SHA384:
686 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
687 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
688 return 1;
689 default:
690 return 0;
691 }
692
693 case SSL_3DES:
694 switch (cipher->algorithm_mac) {
695 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500696 if (version == SSL3_VERSION) {
697 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
698 *out_fixed_iv_len = 8;
699 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500700 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
701 *out_fixed_iv_len = 8;
702 } else {
703 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
704 }
705 *out_mac_secret_len = SHA_DIGEST_LENGTH;
706 return 1;
707 default:
708 return 0;
709 }
710
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700711 case SSL_eNULL:
712 switch (cipher->algorithm_mac) {
713 case SSL_SHA1:
714 if (version == SSL3_VERSION) {
715 *out_aead = EVP_aead_null_sha1_ssl3();
716 } else {
717 *out_aead = EVP_aead_null_sha1_tls();
718 }
719 *out_mac_secret_len = SHA_DIGEST_LENGTH;
720 return 1;
721 default:
722 return 0;
723 }
724
David Benjaminea72bd02014-12-21 21:27:41 -0500725 default:
726 return 0;
727 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800728}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700729
David Benjaminb0883312015-08-06 09:54:13 -0400730const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
731 switch (algorithm_prf) {
732 case SSL_HANDSHAKE_MAC_DEFAULT:
733 return EVP_sha1();
734 case SSL_HANDSHAKE_MAC_SHA256:
735 return EVP_sha256();
736 case SSL_HANDSHAKE_MAC_SHA384:
737 return EVP_sha384();
738 default:
739 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 }
Adam Langley95c29f32014-06-20 12:00:00 -0700741}
742
743#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700745
David Benjamin0344daf2015-04-08 02:08:01 -0400746/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
747 * |buf_len| bytes at |buf|. */
748static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
749 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
750 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
751}
752
Adam Langley95c29f32014-06-20 12:00:00 -0700753static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 CIPHER_ORDER **tail) {
755 if (curr == *tail) {
756 return;
757 }
758 if (curr == *head) {
759 *head = curr->next;
760 }
761 if (curr->prev != NULL) {
762 curr->prev->next = curr->next;
763 }
764 if (curr->next != NULL) {
765 curr->next->prev = curr->prev;
766 }
767 (*tail)->next = curr;
768 curr->prev = *tail;
769 curr->next = NULL;
770 *tail = curr;
771}
Adam Langley95c29f32014-06-20 12:00:00 -0700772
773static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 CIPHER_ORDER **tail) {
775 if (curr == *head) {
776 return;
777 }
778 if (curr == *tail) {
779 *tail = curr->prev;
780 }
781 if (curr->next != NULL) {
782 curr->next->prev = curr->prev;
783 }
784 if (curr->prev != NULL) {
785 curr->prev->next = curr->next;
786 }
787 (*head)->prev = curr;
788 curr->next = *head;
789 curr->prev = NULL;
790 *head = curr;
791}
Adam Langley95c29f32014-06-20 12:00:00 -0700792
David Benjamin82c9e902014-12-12 15:55:27 -0500793static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 CIPHER_ORDER *co_list,
795 CIPHER_ORDER **head_p,
796 CIPHER_ORDER **tail_p) {
David Benjamina1c90a52015-05-30 17:03:14 -0400797 /* The set of ciphers is static, but some subset may be unsupported by
798 * |ssl_method|, so the list may be smaller. */
799 size_t co_list_num = 0;
800 size_t i;
801 for (i = 0; i < kCiphersLen; i++) {
802 const SSL_CIPHER *cipher = &kCiphers[i];
803 if (ssl_method->supports_cipher(cipher)) {
804 co_list[co_list_num].cipher = cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 co_list[co_list_num].next = NULL;
806 co_list[co_list_num].prev = NULL;
807 co_list[co_list_num].active = 0;
808 co_list[co_list_num].in_group = 0;
809 co_list_num++;
810 }
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 /* Prepare linked list from list entries. */
814 if (co_list_num > 0) {
815 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (co_list_num > 1) {
818 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 for (i = 1; i < co_list_num - 1; i++) {
821 co_list[i].prev = &co_list[i - 1];
822 co_list[i].next = &co_list[i + 1];
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
826 }
827
828 co_list[co_list_num - 1].next = NULL;
829
830 *head_p = &co_list[0];
831 *tail_p = &co_list[co_list_num - 1];
832 }
833}
Adam Langley95c29f32014-06-20 12:00:00 -0700834
David Benjamin0344daf2015-04-08 02:08:01 -0400835/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
836 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
837 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
838 *
839 * - If |cipher_id| is non-zero, only that cipher is selected.
840 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
841 * of that strength.
David Benjamindcb6ef02015-11-06 15:35:54 -0500842 * - Otherwise, it selects ciphers that match each bitmasks in |alg*| and
843 * |min_version|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800844static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400845 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamindcb6ef02015-11-06 15:35:54 -0500846 uint32_t alg_enc, uint32_t alg_mac, uint32_t algo_strength,
847 uint16_t min_version, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
849 CIPHER_ORDER *head, *tail, *curr, *next, *last;
850 const SSL_CIPHER *cp;
851 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700852
David Benjamindcb6ef02015-11-06 15:35:54 -0500853 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamin0344daf2015-04-08 02:08:01 -0400854 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
David Benjamindcb6ef02015-11-06 15:35:54 -0500855 algo_strength == 0)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400856 /* The rule matches nothing, so bail early. */
857 return;
858 }
859
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 if (rule == CIPHER_DEL) {
861 /* needed to maintain sorting between currently deleted ciphers */
862 reverse = 1;
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 head = *head_p;
866 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 if (reverse) {
869 next = tail;
870 last = head;
871 } else {
872 next = head;
873 last = tail;
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 curr = NULL;
877 for (;;) {
878 if (curr == last) {
879 break;
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 curr = next;
883 if (curr == NULL) {
884 break;
885 }
Adam Langleye3142a72014-07-24 17:56:48 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 next = reverse ? curr->prev : curr->next;
888 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700889
David Benjamin0344daf2015-04-08 02:08:01 -0400890 /* Selection criteria is either a specific cipher, the value of
891 * |strength_bits|, or the algorithms used. */
892 if (cipher_id != 0) {
893 if (cipher_id != cp->id) {
894 continue;
895 }
896 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 if (strength_bits != cp->strength_bits) {
898 continue;
899 }
David Benjamin0344daf2015-04-08 02:08:01 -0400900 } else if (!(alg_mkey & cp->algorithm_mkey) ||
901 !(alg_auth & cp->algorithm_auth) ||
902 !(alg_enc & cp->algorithm_enc) ||
903 !(alg_mac & cp->algorithm_mac) ||
David Benjamindcb6ef02015-11-06 15:35:54 -0500904 !(algo_strength & cp->algo_strength) ||
905 (min_version != 0 &&
906 SSL_CIPHER_get_min_version(cp) != min_version)) {
David Benjamin0344daf2015-04-08 02:08:01 -0400907 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 }
Adam Langleye3142a72014-07-24 17:56:48 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 /* add the cipher if it has not been added yet. */
911 if (rule == CIPHER_ADD) {
912 /* reverse == 0 */
913 if (!curr->active) {
914 ll_append_tail(&head, curr, &tail);
915 curr->active = 1;
916 curr->in_group = in_group;
917 }
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 /* Move the added cipher to this location */
921 else if (rule == CIPHER_ORD) {
922 /* reverse == 0 */
923 if (curr->active) {
924 ll_append_tail(&head, curr, &tail);
925 curr->in_group = 0;
926 }
927 } else if (rule == CIPHER_DEL) {
928 /* reverse == 1 */
929 if (curr->active) {
930 /* most recently deleted ciphersuites get best positions
931 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
932 * works in reverse to maintain the order) */
933 ll_append_head(&head, curr, &tail);
934 curr->active = 0;
935 curr->in_group = 0;
936 }
937 } else if (rule == CIPHER_KILL) {
938 /* reverse == 0 */
939 if (head == curr) {
940 head = curr->next;
941 } else {
942 curr->prev->next = curr->next;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 if (tail == curr) {
946 tail = curr->prev;
947 }
948 curr->active = 0;
949 if (curr->next != NULL) {
950 curr->next->prev = curr->prev;
951 }
952 if (curr->prev != NULL) {
953 curr->prev->next = curr->next;
954 }
955 curr->next = NULL;
956 curr->prev = NULL;
957 }
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 *head_p = head;
961 *tail_p = tail;
962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
964static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 CIPHER_ORDER **tail_p) {
966 int max_strength_bits, i, *number_uses;
967 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 /* This routine sorts the ciphers with descending strength. The sorting must
970 * keep the pre-sorted sequence, so we apply the normal sorting routine as
971 * '+' movement to the end of the list. */
972 max_strength_bits = 0;
973 curr = *head_p;
974 while (curr != NULL) {
975 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
976 max_strength_bits = curr->cipher->strength_bits;
977 }
978 curr = curr->next;
979 }
Adam Langley95c29f32014-06-20 12:00:00 -0700980
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
982 if (!number_uses) {
David Benjamin3570d732015-06-29 00:28:17 -0400983 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 return 0;
985 }
986 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 /* Now find the strength_bits values actually used. */
989 curr = *head_p;
990 while (curr != NULL) {
991 if (curr->active) {
992 number_uses[curr->cipher->strength_bits]++;
993 }
994 curr = curr->next;
995 }
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 /* Go through the list of used strength_bits values in descending order. */
998 for (i = max_strength_bits; i >= 0; i--) {
999 if (number_uses[i] > 0) {
1000 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
1001 tail_p);
1002 }
1003 }
1004
1005 OPENSSL_free(number_uses);
1006 return 1;
1007}
Adam Langley95c29f32014-06-20 12:00:00 -07001008
David Benjamin0344daf2015-04-08 02:08:01 -04001009static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
1010 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -04001012 CIPHER_ORDER **tail_p) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001013 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, algo_strength;
1014 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 const char *l, *buf;
David Benjamindcb6ef02015-11-06 15:35:54 -05001016 int multi, skip_rule, rule, retval, ok, in_group = 0, has_group = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001017 size_t j, buf_len;
1018 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 retval = 1;
1022 l = rule_str;
1023 for (;;) {
1024 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -07001025
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (ch == '\0') {
1027 break; /* done */
1028 }
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 if (in_group) {
1031 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 if (*tail_p) {
1033 (*tail_p)->in_group = 0;
1034 }
1035 in_group = 0;
1036 l++;
1037 continue;
1038 }
David Benjamin37d92462014-09-20 17:54:24 -04001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 if (ch == '|') {
1041 rule = CIPHER_ADD;
1042 l++;
1043 continue;
1044 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1045 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001046 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001047 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 break;
1049 } else {
1050 rule = CIPHER_ADD;
1051 }
1052 } else if (ch == '-') {
1053 rule = CIPHER_DEL;
1054 l++;
1055 } else if (ch == '+') {
1056 rule = CIPHER_ORD;
1057 l++;
1058 } else if (ch == '!') {
1059 rule = CIPHER_KILL;
1060 l++;
1061 } else if (ch == '@') {
1062 rule = CIPHER_SPECIAL;
1063 l++;
1064 } else if (ch == '[') {
1065 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -04001067 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 break;
1069 }
1070 in_group = 1;
1071 has_group = 1;
1072 l++;
1073 continue;
1074 } else {
1075 rule = CIPHER_ADD;
1076 }
Adam Langley95c29f32014-06-20 12:00:00 -07001077
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 /* If preference groups are enabled, the only legal operator is +.
1079 * Otherwise the in_group bits will get mixed up. */
1080 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001081 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -04001082 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 break;
1084 }
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 if (ITEM_SEP(ch)) {
1087 l++;
1088 continue;
1089 }
Adam Langley95c29f32014-06-20 12:00:00 -07001090
David Benjamin0344daf2015-04-08 02:08:01 -04001091 multi = 0;
1092 cipher_id = 0;
1093 alg_mkey = ~0u;
1094 alg_auth = ~0u;
1095 alg_enc = ~0u;
1096 alg_mac = ~0u;
David Benjamin0344daf2015-04-08 02:08:01 -04001097 algo_strength = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001098 min_version = 0;
1099 skip_rule = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 for (;;) {
1102 ch = *l;
1103 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001104 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
1106 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
1107 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001108 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 }
Adam Langley95c29f32014-06-20 12:00:00 -07001110
David Benjamin0344daf2015-04-08 02:08:01 -04001111 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 /* We hit something we cannot deal with, it is no command or separator
1113 * nor alphanumeric, so we call this an error. */
David Benjamin3570d732015-06-29 00:28:17 -04001114 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -04001115 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 l++;
1117 break;
1118 }
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 break;
1122 }
David Benjamin0344daf2015-04-08 02:08:01 -04001123
1124 /* Look for a matching exact cipher. These aren't allowed in multipart
1125 * rules. */
1126 if (!multi && ch != '+') {
David Benjamina1c90a52015-05-30 17:03:14 -04001127 for (j = 0; j < kCiphersLen; j++) {
1128 const SSL_CIPHER *cipher = &kCiphers[j];
1129 if (rule_equals(cipher->name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001130 cipher_id = cipher->id;
1131 break;
1132 }
1133 }
1134 }
1135 if (cipher_id == 0) {
1136 /* If not an exact cipher, look for a matching cipher alias. */
David Benjamina1c90a52015-05-30 17:03:14 -04001137 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001138 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1139 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1140 alg_auth &= kCipherAliases[j].algorithm_auth;
1141 alg_enc &= kCipherAliases[j].algorithm_enc;
1142 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamin0344daf2015-04-08 02:08:01 -04001143 algo_strength &= kCipherAliases[j].algo_strength;
David Benjamindcb6ef02015-11-06 15:35:54 -05001144
1145 if (min_version != 0 &&
1146 min_version != kCipherAliases[j].min_version) {
1147 skip_rule = 1;
1148 } else {
1149 min_version = kCipherAliases[j].min_version;
1150 }
David Benjamin0344daf2015-04-08 02:08:01 -04001151 break;
1152 }
1153 }
David Benjamina1c90a52015-05-30 17:03:14 -04001154 if (j == kCipherAliasesLen) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001155 skip_rule = 1;
David Benjamin0344daf2015-04-08 02:08:01 -04001156 }
1157 }
1158
1159 /* Check for a multipart rule. */
1160 if (ch != '+') {
1161 break;
1162 }
1163 l++;
1164 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 }
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 /* Ok, we have the rule, now apply it. */
1168 if (rule == CIPHER_SPECIAL) {
1169 /* special command */
1170 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -04001171 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 ok = ssl_cipher_strength_sort(head_p, tail_p);
1173 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001174 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 }
Adam Langley95c29f32014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 if (ok == 0) {
1178 retval = 0;
1179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 /* We do not support any "multi" options together with "@", so throw away
1182 * the rest of the command, if any left, until end or ':' is found. */
1183 while (*l != '\0' && !ITEM_SEP(*l)) {
1184 l++;
1185 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001186 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamindcb6ef02015-11-06 15:35:54 -05001188 algo_strength, min_version, rule, -1, in_group,
1189 head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 }
1191 }
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001194 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 retval = 0;
1196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 return retval;
1199}
Adam Langley95c29f32014-06-20 12:00:00 -07001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201STACK_OF(SSL_CIPHER) *
1202ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -04001203 struct ssl_cipher_preference_list_st **out_cipher_list,
1204 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
1205 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -04001206 int ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1208 const char *rule_p;
1209 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 uint8_t *in_group_flags = NULL;
1211 unsigned int num_in_group_flags = 0;
1212 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -04001215 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 return NULL;
1217 }
David Benjamin5213df42014-08-20 14:19:54 -04001218
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 /* Now we have to collect the available ciphers from the compiled in ciphers.
1220 * We cannot get more than the number compiled in, so it is used for
1221 * allocation. */
David Benjamina1c90a52015-05-30 17:03:14 -04001222 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 if (co_list == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001224 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 return NULL;
1226 }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
David Benjamina1c90a52015-05-30 17:03:14 -04001228 ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001229
Adam Langleyfcf25832014-12-18 17:42:32 -08001230 /* Now arrange all ciphers by preference:
1231 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1234 * key exchange mechanisms */
David Benjamindcb6ef02015-11-06 15:35:54 -05001235 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, 0, CIPHER_ADD,
1236 -1, 0, &head, &tail);
1237 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001239 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1,
David Benjamin0344daf2015-04-08 02:08:01 -04001240 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1243 * CHACHA20 unless there is hardware support for fast and constant-time
1244 * AES_GCM. */
1245 if (EVP_has_aes_hardware()) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001246 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, 0, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 -1, 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001248 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, 0, CIPHER_ADD,
David Benjamin0344daf2015-04-08 02:08:01 -04001249 -1, 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001250 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001251 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 } else {
David Benjamindcb6ef02015-11-06 15:35:54 -05001253 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001254 CIPHER_ADD, -1, 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001255 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, 0, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 -1, 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001257 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, 0, CIPHER_ADD,
David Benjamin0344daf2015-04-08 02:08:01 -04001258 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001259 }
Adam Langley95c29f32014-06-20 12:00:00 -07001260
Adam Langleyfcf25832014-12-18 17:42:32 -08001261 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1262 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamindcb6ef02015-11-06 15:35:54 -05001263 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, 0, CIPHER_ADD, -1,
David Benjamin0344daf2015-04-08 02:08:01 -04001264 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001265 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, 0, CIPHER_ADD, -1,
David Benjamin0344daf2015-04-08 02:08:01 -04001266 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001267 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, 0, CIPHER_ADD,
David Benjamin0344daf2015-04-08 02:08:01 -04001268 -1, 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001269 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, 0, CIPHER_ADD, -1,
David Benjamin0344daf2015-04-08 02:08:01 -04001270 0, &head, &tail);
David Benjamindcb6ef02015-11-06 15:35:54 -05001271 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001273
Adam Langleyfcf25832014-12-18 17:42:32 -08001274 /* Temporarily enable everything else for sorting */
David Benjamindcb6ef02015-11-06 15:35:54 -05001275 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001276 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001277
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 /* Move ciphers without forward secrecy to the end. */
David Benjamindcb6ef02015-11-06 15:35:54 -05001279 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001280 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001281
Adam Langleyfcf25832014-12-18 17:42:32 -08001282 /* Now disable everything (maintaining the ordering!) */
David Benjamindcb6ef02015-11-06 15:35:54 -05001283 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0,
David Benjamin0344daf2015-04-08 02:08:01 -04001284 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001285
Adam Langleyfcf25832014-12-18 17:42:32 -08001286 /* If the rule_string begins with DEFAULT, apply the default rule before
1287 * using the (possibly available) additional rules. */
1288 ok = 1;
1289 rule_p = rule_str;
1290 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001291 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
1292 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 rule_p += 7;
1294 if (*rule_p == ':') {
1295 rule_p++;
1296 }
1297 }
Adam Langley858a88d2014-06-20 12:00:00 -07001298
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -04001300 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 }
Adam Langley95c29f32014-06-20 12:00:00 -07001302
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 if (!ok) {
1304 goto err;
1305 }
1306
1307 /* Allocate new "cipherstack" for the result, return with error
1308 * if we cannot get one. */
1309 cipherstack = sk_SSL_CIPHER_new_null();
1310 if (cipherstack == NULL) {
1311 goto err;
1312 }
1313
David Benjamina1c90a52015-05-30 17:03:14 -04001314 in_group_flags = OPENSSL_malloc(kCiphersLen);
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 if (!in_group_flags) {
1316 goto err;
1317 }
1318
1319 /* The cipher selection for the list is done. The ciphers are added
1320 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1321 for (curr = head; curr != NULL; curr = curr->next) {
1322 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001323 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1324 goto err;
1325 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 in_group_flags[num_in_group_flags++] = curr->in_group;
1327 }
1328 }
1329 OPENSSL_free(co_list); /* Not needed any longer */
1330 co_list = NULL;
1331
1332 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1333 if (tmp_cipher_list == NULL) {
1334 goto err;
1335 }
1336 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1337 if (!pref_list) {
1338 goto err;
1339 }
1340 pref_list->ciphers = cipherstack;
1341 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1342 if (!pref_list->in_group_flags) {
1343 goto err;
1344 }
1345 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1346 OPENSSL_free(in_group_flags);
1347 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001348 if (*out_cipher_list != NULL) {
1349 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 }
David Benjamin71f07942015-04-08 02:36:59 -04001351 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 pref_list = NULL;
1353
David Benjamin71f07942015-04-08 02:36:59 -04001354 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001355 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001356 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001358 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1359 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001360
David Benjamin71f07942015-04-08 02:36:59 -04001361 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 } else {
1363 sk_SSL_CIPHER_free(tmp_cipher_list);
1364 tmp_cipher_list = NULL;
1365 }
1366
1367 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001368
1369err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001370 OPENSSL_free(co_list);
1371 OPENSSL_free(in_group_flags);
1372 sk_SSL_CIPHER_free(cipherstack);
1373 sk_SSL_CIPHER_free(tmp_cipher_list);
1374 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 OPENSSL_free(pref_list->in_group_flags);
1376 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001377 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001378 return NULL;
1379}
Adam Langley95c29f32014-06-20 12:00:00 -07001380
David Benjamin71f07942015-04-08 02:36:59 -04001381uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1382
David Benjamina1c90a52015-05-30 17:03:14 -04001383uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
1384 uint32_t id = cipher->id;
1385 /* All ciphers are SSLv3. */
1386 assert((id & 0xff000000) == 0x03000000);
1387 return id & 0xffff;
1388}
1389
David Benjamin71f07942015-04-08 02:36:59 -04001390int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1391 return (cipher->algorithm_enc & SSL_AES) != 0;
1392}
1393
1394int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1395 return (cipher->algorithm_mac & SSL_MD5) != 0;
1396}
1397
David Benjaminef793f42015-11-05 18:16:27 -05001398int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) {
1399 return (cipher->algorithm_mac & SSL_SHA1) != 0;
1400}
1401
David Benjamin71f07942015-04-08 02:36:59 -04001402int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
David Benjaminc0125ef2015-09-09 09:11:07 -04001403 return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001404}
1405
David Benjaminef793f42015-11-05 18:16:27 -05001406int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) {
1407 return (cipher->algorithm_enc & SSL_AES128GCM) != 0;
1408}
1409
Adam Langleyb00061c2015-11-16 17:44:52 -08001410int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) {
1411 return (cipher->algorithm_enc & SSL_AES128) != 0;
1412}
1413
1414int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) {
1415 return (cipher->algorithm_enc & SSL_AES256) != 0;
1416}
1417
David Benjamin51a01a52015-10-29 13:19:56 -04001418int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
Brian Smith271777f2015-10-03 13:53:33 -10001419 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305_OLD) != 0;
David Benjamin71f07942015-04-08 02:36:59 -04001420}
1421
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001422int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) {
1423 return (cipher->algorithm_enc & SSL_eNULL) != 0;
1424}
1425
Adam Langleyd7fe75c2015-09-18 15:40:48 -07001426int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) {
1427 return (cipher->algorithm_enc & SSL_RC4) != 0;
1428}
1429
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001430int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
1431 /* Neither stream cipher nor AEAD. */
1432 return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 &&
1433 cipher->algorithm_mac != SSL_AEAD;
1434}
1435
David Benjaminef793f42015-11-05 18:16:27 -05001436int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) {
1437 return (cipher->algorithm_auth & SSL_aECDSA) != 0;
1438}
1439
1440uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
David Benjamindcb6ef02015-11-06 15:35:54 -05001441 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
1442 /* Cipher suites before TLS 1.2 use the default PRF, while all those added
1443 * afterwards specify a particular hash. */
David Benjaminef793f42015-11-05 18:16:27 -05001444 return TLS1_2_VERSION;
1445 }
1446 return SSL3_VERSION;
1447}
1448
David Benjamin71f07942015-04-08 02:36:59 -04001449/* return the actual cipher being used */
1450const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1451 if (cipher != NULL) {
1452 return cipher->name;
1453 }
1454
1455 return "(NONE)";
1456}
1457
1458const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1459 if (cipher == NULL) {
1460 return "";
1461 }
1462
1463 switch (cipher->algorithm_mkey) {
1464 case SSL_kRSA:
1465 return "RSA";
1466
1467 case SSL_kDHE:
1468 switch (cipher->algorithm_auth) {
1469 case SSL_aRSA:
1470 return "DHE_RSA";
1471 default:
1472 assert(0);
1473 return "UNKNOWN";
1474 }
1475
1476 case SSL_kECDHE:
1477 switch (cipher->algorithm_auth) {
1478 case SSL_aECDSA:
1479 return "ECDHE_ECDSA";
1480 case SSL_aRSA:
1481 return "ECDHE_RSA";
1482 case SSL_aPSK:
1483 return "ECDHE_PSK";
1484 default:
1485 assert(0);
1486 return "UNKNOWN";
1487 }
1488
1489 case SSL_kPSK:
1490 assert(cipher->algorithm_auth == SSL_aPSK);
1491 return "PSK";
1492
1493 default:
1494 assert(0);
1495 return "UNKNOWN";
1496 }
1497}
1498
1499static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1500 switch (cipher->algorithm_enc) {
1501 case SSL_3DES:
1502 return "3DES_EDE_CBC";
1503 case SSL_RC4:
1504 return "RC4";
1505 case SSL_AES128:
1506 return "AES_128_CBC";
1507 case SSL_AES256:
1508 return "AES_256_CBC";
1509 case SSL_AES128GCM:
1510 return "AES_128_GCM";
1511 case SSL_AES256GCM:
1512 return "AES_256_GCM";
Brian Smith271777f2015-10-03 13:53:33 -10001513 case SSL_CHACHA20POLY1305_OLD:
David Benjamin71f07942015-04-08 02:36:59 -04001514 return "CHACHA20_POLY1305";
1515 break;
1516 default:
1517 assert(0);
1518 return "UNKNOWN";
1519 }
1520}
1521
1522static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
David Benjaminb0883312015-08-06 09:54:13 -04001523 switch (cipher->algorithm_prf) {
1524 case SSL_HANDSHAKE_MAC_DEFAULT:
1525 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1526 * only ever MD5 or SHA-1. */
1527 switch (cipher->algorithm_mac) {
1528 case SSL_MD5:
1529 return "MD5";
1530 case SSL_SHA1:
1531 return "SHA";
1532 }
1533 break;
1534 case SSL_HANDSHAKE_MAC_SHA256:
1535 return "SHA256";
1536 case SSL_HANDSHAKE_MAC_SHA384:
1537 return "SHA384";
David Benjamin71f07942015-04-08 02:36:59 -04001538 }
David Benjaminb0883312015-08-06 09:54:13 -04001539 assert(0);
1540 return "UNKNOWN";
David Benjamin71f07942015-04-08 02:36:59 -04001541}
1542
1543char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1544 if (cipher == NULL) {
1545 return NULL;
1546 }
1547
1548 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1549 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1550 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1551
1552 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1553 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1554 strlen(prf_name) + 1;
1555 char *ret = OPENSSL_malloc(len);
1556 if (ret == NULL) {
1557 return NULL;
1558 }
1559 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1560 BUF_strlcat(ret, kx_name, len) >= len ||
1561 BUF_strlcat(ret, "_WITH_", len) >= len ||
1562 BUF_strlcat(ret, enc_name, len) >= len ||
1563 BUF_strlcat(ret, "_", len) >= len ||
1564 BUF_strlcat(ret, prf_name, len) >= len) {
1565 assert(0);
1566 OPENSSL_free(ret);
1567 return NULL;
1568 }
1569 assert(strlen(ret) + 1 == len);
1570 return ret;
1571}
1572
1573int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1574 if (cipher == NULL) {
1575 return 0;
1576 }
1577
1578 if (out_alg_bits != NULL) {
1579 *out_alg_bits = cipher->alg_bits;
1580 }
1581 return cipher->strength_bits;
1582}
1583
Adam Langleyfcf25832014-12-18 17:42:32 -08001584const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1585 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001587 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
1588 static const char *format = "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001589
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 alg_mkey = cipher->algorithm_mkey;
1591 alg_auth = cipher->algorithm_auth;
1592 alg_enc = cipher->algorithm_enc;
1593 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 switch (alg_mkey) {
1596 case SSL_kRSA:
1597 kx = "RSA";
1598 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001599
David Benjamin7061e282015-03-19 11:10:48 -04001600 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 kx = "DH";
1602 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
David Benjamin7061e282015-03-19 11:10:48 -04001604 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 kx = "ECDH";
1606 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001607
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 case SSL_kPSK:
1609 kx = "PSK";
1610 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001611
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 default:
1613 kx = "unknown";
1614 }
Adam Langley95c29f32014-06-20 12:00:00 -07001615
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 switch (alg_auth) {
1617 case SSL_aRSA:
1618 au = "RSA";
1619 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langleyfcf25832014-12-18 17:42:32 -08001621 case SSL_aECDSA:
1622 au = "ECDSA";
1623 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 case SSL_aPSK:
1626 au = "PSK";
1627 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001628
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 default:
1630 au = "unknown";
1631 break;
1632 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001633
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 switch (alg_enc) {
1635 case SSL_3DES:
1636 enc = "3DES(168)";
1637 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001638
Adam Langleyfcf25832014-12-18 17:42:32 -08001639 case SSL_RC4:
1640 enc = "RC4(128)";
1641 break;
1642
1643 case SSL_AES128:
1644 enc = "AES(128)";
1645 break;
1646
1647 case SSL_AES256:
1648 enc = "AES(256)";
1649 break;
1650
1651 case SSL_AES128GCM:
1652 enc = "AESGCM(128)";
1653 break;
1654
1655 case SSL_AES256GCM:
1656 enc = "AESGCM(256)";
1657 break;
1658
Brian Smith271777f2015-10-03 13:53:33 -10001659 case SSL_CHACHA20POLY1305_OLD:
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 enc = "ChaCha20-Poly1305";
1661 break;
1662
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001663 case SSL_eNULL:
1664 enc="None";
1665 break;
1666
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 default:
1668 enc = "unknown";
1669 break;
1670 }
1671
1672 switch (alg_mac) {
1673 case SSL_MD5:
1674 mac = "MD5";
1675 break;
1676
1677 case SSL_SHA1:
1678 mac = "SHA1";
1679 break;
1680
1681 case SSL_SHA256:
1682 mac = "SHA256";
1683 break;
1684
1685 case SSL_SHA384:
1686 mac = "SHA384";
1687 break;
1688
1689 case SSL_AEAD:
1690 mac = "AEAD";
1691 break;
1692
1693 default:
1694 mac = "unknown";
1695 break;
1696 }
1697
1698 if (buf == NULL) {
1699 len = 128;
1700 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001701 if (buf == NULL) {
1702 return NULL;
1703 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 } else if (len < 128) {
1705 return "Buffer too small";
1706 }
1707
David Benjamindcb6ef02015-11-06 15:35:54 -05001708 BIO_snprintf(buf, len, format, cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 return buf;
1710}
1711
David Benjamin71f07942015-04-08 02:36:59 -04001712const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1713 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001714}
1715
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001716COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001717
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001718int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001719
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001720const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
David Benjamind1d80782015-07-05 11:54:09 -04001722int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
David Benjamin71f07942015-04-08 02:36:59 -04001723 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 if (alg_a & SSL_aECDSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001726 return EVP_PKEY_EC;
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 } else if (alg_a & SSL_aRSA) {
David Benjamind1d80782015-07-05 11:54:09 -04001728 return EVP_PKEY_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 }
Adam Langley95c29f32014-06-20 12:00:00 -07001730
David Benjamind1d80782015-07-05 11:54:09 -04001731 return EVP_PKEY_NONE;
Adam Langleyfcf25832014-12-18 17:42:32 -08001732}
David Benjamin9c651c92014-07-12 13:27:45 -04001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamindf0905a2015-09-19 09:58:31 -04001735 /* PSK-authenticated ciphers do not use a certificate. (RSA_PSK is not
1736 * supported.) */
1737 if (cipher->algorithm_auth & SSL_aPSK) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 return 0;
1739 }
1740
1741 /* All other ciphers include it. */
1742 return 1;
1743}
1744
Adam Langleyfcf25832014-12-18 17:42:32 -08001745int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1746 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001747 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 return 1;
1749 }
1750
1751 /* It is optional in all others. */
1752 return 0;
1753}
David Benjaminb8d28cf2015-07-28 21:34:45 -04001754
1755size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1756 size_t block_size;
1757 switch (cipher->algorithm_enc) {
1758 case SSL_3DES:
1759 block_size = 8;
1760 break;
1761 case SSL_AES128:
1762 case SSL_AES256:
1763 block_size = 16;
1764 break;
1765 default:
1766 return 0;
1767 }
1768
1769 size_t mac_len;
1770 switch (cipher->algorithm_mac) {
1771 case SSL_MD5:
1772 mac_len = MD5_DIGEST_LENGTH;
1773 break;
1774 case SSL_SHA1:
1775 mac_len = SHA_DIGEST_LENGTH;
1776 break;
1777 default:
1778 return 0;
1779 }
1780
1781 size_t ret = 1 + mac_len;
1782 ret += block_size - (ret % block_size);
1783 return ret;
1784}