blob: 4f5049c607c0af83c3bd2c01f327655975f60af5 [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.
Adam Langleye745b252018-02-26 14:02:17 -08007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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).
Adam Langleye745b252018-02-26 14:02:17 -080014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 :-).
Adam Langleye745b252018-02-26 14:02:17 -080036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Adam Langleye745b252018-02-26 14:02:17 -080039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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
Adam Langleye745b252018-02-26 14:02:17 -080065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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.
Adam Langleye745b252018-02-26 14:02:17 -0800112 * ECC cipher suite support in OpenSSL originally developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700113 * 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 <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjaminf0ae1702015-04-07 23:05:04 -0400146#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500147#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400150#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400153#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700156BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400157
MichaƂ Janiszewskic4f3b8a2018-07-30 22:50:40 +0200158static constexpr SSL_CIPHER kCiphers[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400159 // The RSA ciphers
160 // Cipher 02
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700161 {
David Benjaminff2df332015-11-18 10:01:16 -0500162 SSL3_TXT_RSA_NULL_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400163 "TLS_RSA_WITH_NULL_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500164 SSL3_CK_RSA_NULL_SHA,
165 SSL_kRSA,
166 SSL_aRSA,
167 SSL_eNULL,
168 SSL_SHA1,
169 SSL_HANDSHAKE_MAC_DEFAULT,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700170 },
171
David Benjaminc11ea9422017-08-29 16:33:21 -0400172 // Cipher 0A
David Benjamina1c90a52015-05-30 17:03:14 -0400173 {
David Benjaminff2df332015-11-18 10:01:16 -0500174 SSL3_TXT_RSA_DES_192_CBC3_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400175 "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500176 SSL3_CK_RSA_DES_192_CBC3_SHA,
177 SSL_kRSA,
178 SSL_aRSA,
179 SSL_3DES,
180 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500181 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400182 },
183
184
David Benjaminc11ea9422017-08-29 16:33:21 -0400185 // New AES ciphersuites
David Benjamina1c90a52015-05-30 17:03:14 -0400186
David Benjaminc11ea9422017-08-29 16:33:21 -0400187 // Cipher 2F
David Benjamina1c90a52015-05-30 17:03:14 -0400188 {
David Benjaminff2df332015-11-18 10:01:16 -0500189 TLS1_TXT_RSA_WITH_AES_128_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400190 "TLS_RSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500191 TLS1_CK_RSA_WITH_AES_128_SHA,
192 SSL_kRSA,
193 SSL_aRSA,
194 SSL_AES128,
195 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500196 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400197 },
198
David Benjaminc11ea9422017-08-29 16:33:21 -0400199 // Cipher 35
David Benjamina1c90a52015-05-30 17:03:14 -0400200 {
David Benjaminff2df332015-11-18 10:01:16 -0500201 TLS1_TXT_RSA_WITH_AES_256_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400202 "TLS_RSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500203 TLS1_CK_RSA_WITH_AES_256_SHA,
204 SSL_kRSA,
205 SSL_aRSA,
206 SSL_AES256,
207 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500208 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400209 },
210
David Benjaminc11ea9422017-08-29 16:33:21 -0400211 // PSK cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700212
David Benjaminc11ea9422017-08-29 16:33:21 -0400213 // Cipher 8C
David Benjamina1c90a52015-05-30 17:03:14 -0400214 {
David Benjaminff2df332015-11-18 10:01:16 -0500215 TLS1_TXT_PSK_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400216 "TLS_PSK_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500217 TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
218 SSL_kPSK,
219 SSL_aPSK,
220 SSL_AES128,
221 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500222 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400223 },
224
David Benjaminc11ea9422017-08-29 16:33:21 -0400225 // Cipher 8D
David Benjamina1c90a52015-05-30 17:03:14 -0400226 {
David Benjaminff2df332015-11-18 10:01:16 -0500227 TLS1_TXT_PSK_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400228 "TLS_PSK_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500229 TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
230 SSL_kPSK,
231 SSL_aPSK,
232 SSL_AES256,
233 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500234 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400235 },
236
David Benjaminc11ea9422017-08-29 16:33:21 -0400237 // GCM ciphersuites from RFC5288
David Benjamina1c90a52015-05-30 17:03:14 -0400238
David Benjaminc11ea9422017-08-29 16:33:21 -0400239 // Cipher 9C
David Benjamina1c90a52015-05-30 17:03:14 -0400240 {
241 TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400242 "TLS_RSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500243 TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
244 SSL_kRSA,
245 SSL_aRSA,
246 SSL_AES128GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500247 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400248 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400249 },
250
David Benjaminc11ea9422017-08-29 16:33:21 -0400251 // Cipher 9D
David Benjamina1c90a52015-05-30 17:03:14 -0400252 {
253 TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400254 "TLS_RSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500255 TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
256 SSL_kRSA,
257 SSL_aRSA,
258 SSL_AES256GCM,
David Benjamind6e9eec2015-11-18 09:48:55 -0500259 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400260 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400261 },
262
David Benjaminc11ea9422017-08-29 16:33:21 -0400263 // TLS 1.3 suites.
Steven Valdez803c77a2016-09-06 14:13:43 -0400264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // Cipher 1301
Steven Valdez803c77a2016-09-06 14:13:43 -0400266 {
267 TLS1_TXT_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400268 "TLS_AES_128_GCM_SHA256",
Steven Valdez803c77a2016-09-06 14:13:43 -0400269 TLS1_CK_AES_128_GCM_SHA256,
270 SSL_kGENERIC,
271 SSL_aGENERIC,
272 SSL_AES128GCM,
273 SSL_AEAD,
274 SSL_HANDSHAKE_MAC_SHA256,
275 },
276
David Benjaminc11ea9422017-08-29 16:33:21 -0400277 // Cipher 1302
Steven Valdez803c77a2016-09-06 14:13:43 -0400278 {
279 TLS1_TXT_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400280 "TLS_AES_256_GCM_SHA384",
Steven Valdez803c77a2016-09-06 14:13:43 -0400281 TLS1_CK_AES_256_GCM_SHA384,
282 SSL_kGENERIC,
283 SSL_aGENERIC,
284 SSL_AES256GCM,
285 SSL_AEAD,
286 SSL_HANDSHAKE_MAC_SHA384,
287 },
288
David Benjaminc11ea9422017-08-29 16:33:21 -0400289 // Cipher 1303
Steven Valdez803c77a2016-09-06 14:13:43 -0400290 {
291 TLS1_TXT_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400292 "TLS_CHACHA20_POLY1305_SHA256",
Steven Valdez803c77a2016-09-06 14:13:43 -0400293 TLS1_CK_CHACHA20_POLY1305_SHA256,
294 SSL_kGENERIC,
295 SSL_aGENERIC,
296 SSL_CHACHA20POLY1305,
297 SSL_AEAD,
298 SSL_HANDSHAKE_MAC_SHA256,
299 },
300
David Benjaminc11ea9422017-08-29 16:33:21 -0400301 // Cipher C009
David Benjamina1c90a52015-05-30 17:03:14 -0400302 {
303 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400304 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500305 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
306 SSL_kECDHE,
307 SSL_aECDSA,
308 SSL_AES128,
309 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500310 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400311 },
312
David Benjaminc11ea9422017-08-29 16:33:21 -0400313 // Cipher C00A
David Benjamina1c90a52015-05-30 17:03:14 -0400314 {
315 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400316 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500317 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
318 SSL_kECDHE,
319 SSL_aECDSA,
320 SSL_AES256,
321 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500322 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400323 },
324
David Benjaminc11ea9422017-08-29 16:33:21 -0400325 // Cipher C013
David Benjamina1c90a52015-05-30 17:03:14 -0400326 {
327 TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400328 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500329 TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
330 SSL_kECDHE,
331 SSL_aRSA,
332 SSL_AES128,
David Benjamind6e9eec2015-11-18 09:48:55 -0500333 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500334 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400335 },
336
David Benjaminc11ea9422017-08-29 16:33:21 -0400337 // Cipher C014
David Benjamina1c90a52015-05-30 17:03:14 -0400338 {
339 TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400340 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
David Benjaminff2df332015-11-18 10:01:16 -0500341 TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
342 SSL_kECDHE,
343 SSL_aRSA,
344 SSL_AES256,
David Benjamind6e9eec2015-11-18 09:48:55 -0500345 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500346 SSL_HANDSHAKE_MAC_DEFAULT,
David Benjamina1c90a52015-05-30 17:03:14 -0400347 },
348
David Benjaminc11ea9422017-08-29 16:33:21 -0400349 // GCM based TLS v1.2 ciphersuites from RFC5289
David Benjamina1c90a52015-05-30 17:03:14 -0400350
David Benjaminc11ea9422017-08-29 16:33:21 -0400351 // Cipher C02B
David Benjamina1c90a52015-05-30 17:03:14 -0400352 {
353 TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400354 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500355 TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
356 SSL_kECDHE,
357 SSL_aECDSA,
358 SSL_AES128GCM,
359 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400360 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400361 },
362
David Benjaminc11ea9422017-08-29 16:33:21 -0400363 // Cipher C02C
David Benjamina1c90a52015-05-30 17:03:14 -0400364 {
365 TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400366 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500367 TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
368 SSL_kECDHE,
369 SSL_aECDSA,
370 SSL_AES256GCM,
371 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400372 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400373 },
374
David Benjaminc11ea9422017-08-29 16:33:21 -0400375 // Cipher C02F
David Benjamina1c90a52015-05-30 17:03:14 -0400376 {
377 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400378 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
David Benjaminff2df332015-11-18 10:01:16 -0500379 TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
380 SSL_kECDHE,
381 SSL_aRSA,
382 SSL_AES128GCM,
383 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400384 SSL_HANDSHAKE_MAC_SHA256,
David Benjamina1c90a52015-05-30 17:03:14 -0400385 },
386
David Benjaminc11ea9422017-08-29 16:33:21 -0400387 // Cipher C030
David Benjamina1c90a52015-05-30 17:03:14 -0400388 {
389 TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
David Benjamin6fff3862017-06-21 21:07:04 -0400390 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
David Benjaminff2df332015-11-18 10:01:16 -0500391 TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
392 SSL_kECDHE,
393 SSL_aRSA,
394 SSL_AES256GCM,
395 SSL_AEAD,
David Benjaminb2a985b2015-06-21 15:13:57 -0400396 SSL_HANDSHAKE_MAC_SHA384,
David Benjamina1c90a52015-05-30 17:03:14 -0400397 },
398
David Benjaminc11ea9422017-08-29 16:33:21 -0400399 // ECDHE-PSK cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700400
David Benjaminc11ea9422017-08-29 16:33:21 -0400401 // Cipher C035
Adam Langley85bc5602015-06-09 09:54:04 -0700402 {
403 TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400404 "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA",
Adam Langley85bc5602015-06-09 09:54:04 -0700405 TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500406 SSL_kECDHE,
407 SSL_aPSK,
408 SSL_AES128,
409 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500410 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700411 },
412
David Benjaminc11ea9422017-08-29 16:33:21 -0400413 // Cipher C036
Adam Langley85bc5602015-06-09 09:54:04 -0700414 {
415 TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjamin6fff3862017-06-21 21:07:04 -0400416 "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA",
Adam Langley85bc5602015-06-09 09:54:04 -0700417 TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
David Benjaminff2df332015-11-18 10:01:16 -0500418 SSL_kECDHE,
419 SSL_aPSK,
420 SSL_AES256,
421 SSL_SHA1,
David Benjamin9f2e2772015-11-18 09:59:43 -0500422 SSL_HANDSHAKE_MAC_DEFAULT,
Adam Langley85bc5602015-06-09 09:54:04 -0700423 },
424
David Benjaminc11ea9422017-08-29 16:33:21 -0400425 // ChaCha20-Poly1305 cipher suites.
Adam Langley85bc5602015-06-09 09:54:04 -0700426
David Benjaminc11ea9422017-08-29 16:33:21 -0400427 // Cipher CCA8
David Benjamin13414b32015-12-09 23:02:39 -0500428 {
429 TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400430 "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500431 TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
432 SSL_kECDHE,
433 SSL_aRSA,
434 SSL_CHACHA20POLY1305,
435 SSL_AEAD,
436 SSL_HANDSHAKE_MAC_SHA256,
437 },
438
David Benjaminc11ea9422017-08-29 16:33:21 -0400439 // Cipher CCA9
David Benjamin13414b32015-12-09 23:02:39 -0500440 {
441 TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400442 "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500443 TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
444 SSL_kECDHE,
445 SSL_aECDSA,
446 SSL_CHACHA20POLY1305,
447 SSL_AEAD,
448 SSL_HANDSHAKE_MAC_SHA256,
449 },
450
David Benjaminc11ea9422017-08-29 16:33:21 -0400451 // Cipher CCAB
David Benjamin13414b32015-12-09 23:02:39 -0500452 {
453 TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
David Benjamin6fff3862017-06-21 21:07:04 -0400454 "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
David Benjamin13414b32015-12-09 23:02:39 -0500455 TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,
456 SSL_kECDHE,
457 SSL_aPSK,
458 SSL_CHACHA20POLY1305,
459 SSL_AEAD,
460 SSL_HANDSHAKE_MAC_SHA256,
461 },
Matt Braithwaite053931e2016-05-25 12:06:05 -0700462
David Benjamina1c90a52015-05-30 17:03:14 -0400463};
464
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700465Span<const SSL_CIPHER> AllCiphers() {
466 return MakeConstSpan(kCiphers, OPENSSL_ARRAY_SIZE(kCiphers));
467}
David Benjamina1c90a52015-05-30 17:03:14 -0400468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469#define CIPHER_ADD 1
470#define CIPHER_KILL 2
471#define CIPHER_DEL 3
472#define CIPHER_ORD 4
473#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475typedef struct cipher_order_st {
476 const SSL_CIPHER *cipher;
David Benjaminf4962492017-09-27 19:34:32 -0400477 bool active;
478 bool in_group;
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 struct cipher_order_st *next, *prev;
480} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin0344daf2015-04-08 02:08:01 -0400482typedef struct cipher_alias_st {
David Benjaminc11ea9422017-08-29 16:33:21 -0400483 // name is the name of the cipher alias.
David Benjamin0344daf2015-04-08 02:08:01 -0400484 const char *name;
485
David Benjaminc11ea9422017-08-29 16:33:21 -0400486 // The following fields are bitmasks for the corresponding fields on
487 // |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
488 // bit corresponding to the cipher's value is set to 1. If any bitmask is
489 // all zeroes, the alias matches nothing. Use |~0u| for the default value.
David Benjamin0344daf2015-04-08 02:08:01 -0400490 uint32_t algorithm_mkey;
491 uint32_t algorithm_auth;
492 uint32_t algorithm_enc;
493 uint32_t algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500494
David Benjaminc11ea9422017-08-29 16:33:21 -0400495 // min_version, if non-zero, matches all ciphers which were added in that
496 // particular protocol version.
David Benjamindcb6ef02015-11-06 15:35:54 -0500497 uint16_t min_version;
David Benjamin0344daf2015-04-08 02:08:01 -0400498} CIPHER_ALIAS;
499
David Benjamina1c90a52015-05-30 17:03:14 -0400500static const CIPHER_ALIAS kCipherAliases[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400501 // "ALL" doesn't include eNULL. It must be explicitly enabled.
David Benjamin3b903f22017-09-29 18:44:56 -0400502 {"ALL", ~0u, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjaminc11ea9422017-08-29 16:33:21 -0400504 // The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing.
Adam Langley95c29f32014-06-20 12:00:00 -0700505
David Benjaminc11ea9422017-08-29 16:33:21 -0400506 // key exchange aliases
507 // (some of those using only a single bit here combine
508 // multiple key exchange algs according to the RFCs.
David Benjamind6e9eec2015-11-18 09:48:55 -0500509 {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700510
David Benjamind6e9eec2015-11-18 09:48:55 -0500511 {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
512 {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
513 {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700514
David Benjamind6e9eec2015-11-18 09:48:55 -0500515 {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjaminc11ea9422017-08-29 16:33:21 -0400517 // server authentication aliases
David Benjamin3b903f22017-09-29 18:44:56 -0400518 {"aRSA", ~0u, SSL_aRSA, ~0u, ~0u, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800519 {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
520 {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500521 {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjaminc11ea9422017-08-29 16:33:21 -0400523 // aliases combining key exchange and server authentication
David Benjamind6e9eec2015-11-18 09:48:55 -0500524 {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
525 {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0},
David Benjamin3b903f22017-09-29 18:44:56 -0400526 {"RSA", SSL_kRSA, SSL_aRSA, ~0u, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500527 {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700528
David Benjaminc11ea9422017-08-29 16:33:21 -0400529 // symmetric encryption aliases
David Benjamind6e9eec2015-11-18 09:48:55 -0500530 {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0},
David Benjamind6e9eec2015-11-18 09:48:55 -0500531 {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0},
Matthew Braithwaite651aaef2016-12-08 16:14:36 -0800532 {"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},
Adam Langley2e839242017-01-19 15:12:44 -0800535 {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjaminc11ea9422017-08-29 16:33:21 -0400537 // MAC aliases
David Benjamin3b903f22017-09-29 18:44:56 -0400538 {"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0},
539 {"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700540
David Benjaminc11ea9422017-08-29 16:33:21 -0400541 // Legacy protocol minimum version aliases. "TLSv1" is intentionally the
542 // same as "SSLv3".
David Benjamin3b903f22017-09-29 18:44:56 -0400543 {"SSLv3", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
544 {"TLSv1", ~0u, ~0u, ~0u, ~0u, SSL3_VERSION},
545 {"TLSv1.2", ~0u, ~0u, ~0u, ~0u, TLS1_2_VERSION},
Adam Langley95c29f32014-06-20 12:00:00 -0700546
David Benjaminc11ea9422017-08-29 16:33:21 -0400547 // Legacy strength classes.
David Benjamin3b903f22017-09-29 18:44:56 -0400548 {"HIGH", ~0u, ~0u, ~0u, ~0u, 0},
549 {"FIPS", ~0u, ~0u, ~0u, ~0u, 0},
David Benjamin0ca92142018-05-03 11:35:10 -0400550
551 // Temporary no-op aliases corresponding to removed SHA-2 legacy CBC
552 // ciphers. These should be removed after 2018-05-14.
553 {"SHA256", 0, 0, 0, 0, 0},
554 {"SHA384", 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800555};
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Steven Valdezcb966542016-08-17 16:56:14 -0400557static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases);
David Benjamina1c90a52015-05-30 17:03:14 -0400558
David Benjaminf4962492017-09-27 19:34:32 -0400559bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
560 size_t *out_mac_secret_len,
561 size_t *out_fixed_iv_len, const SSL_CIPHER *cipher,
David Benjamin8525ff32018-09-05 18:44:15 -0500562 uint16_t version, bool is_dtls) {
David Benjaminea72bd02014-12-21 21:27:41 -0500563 *out_aead = NULL;
564 *out_mac_secret_len = 0;
565 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700566
David Benjamin8525ff32018-09-05 18:44:15 -0500567 const bool is_tls12 = version == TLS1_2_VERSION && !is_dtls;
568 const bool is_tls13 = version == TLS1_3_VERSION && !is_dtls;
Steven Valdez2f3404b2017-05-24 16:54:35 -0400569
David Benjamin305e6fb2016-10-27 18:19:00 -0400570 if (cipher->algorithm_mac == SSL_AEAD) {
571 if (cipher->algorithm_enc == SSL_AES128GCM) {
Adam Langleybcfb4992018-06-18 14:04:41 -0700572 if (is_tls12) {
573 *out_aead = EVP_aead_aes_128_gcm_tls12();
574 } else if (is_tls13) {
575 *out_aead = EVP_aead_aes_128_gcm_tls13();
576 } else {
577 *out_aead = EVP_aead_aes_128_gcm();
578 }
David Benjaminea72bd02014-12-21 21:27:41 -0500579 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400580 } else if (cipher->algorithm_enc == SSL_AES256GCM) {
Adam Langleybcfb4992018-06-18 14:04:41 -0700581 if (is_tls12) {
582 *out_aead = EVP_aead_aes_256_gcm_tls12();
583 } else if (is_tls13) {
584 *out_aead = EVP_aead_aes_256_gcm_tls13();
585 } else {
586 *out_aead = EVP_aead_aes_256_gcm();
587 }
David Benjaminea72bd02014-12-21 21:27:41 -0500588 *out_fixed_iv_len = 4;
David Benjamin305e6fb2016-10-27 18:19:00 -0400589 } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) {
David Benjamin13414b32015-12-09 23:02:39 -0500590 *out_aead = EVP_aead_chacha20_poly1305();
591 *out_fixed_iv_len = 12;
David Benjamin305e6fb2016-10-27 18:19:00 -0400592 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400593 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400594 }
595
David Benjaminc11ea9422017-08-29 16:33:21 -0400596 // In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code
597 // above computes the TLS 1.2 construction.
David Benjamin305e6fb2016-10-27 18:19:00 -0400598 if (version >= TLS1_3_VERSION) {
599 *out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead);
600 }
601 } else if (cipher->algorithm_mac == SSL_SHA1) {
602 if (cipher->algorithm_enc == SSL_eNULL) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400603 *out_aead = EVP_aead_null_sha1_tls();
David Benjamin305e6fb2016-10-27 18:19:00 -0400604 } else if (cipher->algorithm_enc == SSL_3DES) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400605 if (version == TLS1_VERSION) {
David Benjamin305e6fb2016-10-27 18:19:00 -0400606 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
607 *out_fixed_iv_len = 8;
608 } else {
609 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
610 }
611 } else if (cipher->algorithm_enc == SSL_AES128) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400612 if (version == TLS1_VERSION) {
David Benjamin305e6fb2016-10-27 18:19:00 -0400613 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
614 *out_fixed_iv_len = 16;
615 } else {
616 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
617 }
618 } else if (cipher->algorithm_enc == SSL_AES256) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400619 if (version == TLS1_VERSION) {
David Benjamin305e6fb2016-10-27 18:19:00 -0400620 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
621 *out_fixed_iv_len = 16;
622 } else {
623 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
624 }
625 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400626 return false;
David Benjamin305e6fb2016-10-27 18:19:00 -0400627 }
628
629 *out_mac_secret_len = SHA_DIGEST_LENGTH;
David Benjamin305e6fb2016-10-27 18:19:00 -0400630 } else {
David Benjaminf4962492017-09-27 19:34:32 -0400631 return false;
David Benjaminea72bd02014-12-21 21:27:41 -0500632 }
Steven Valdez79750562016-06-16 06:38:04 -0400633
David Benjaminf4962492017-09-27 19:34:32 -0400634 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800635}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700636
David Benjaminca9e8f52017-08-09 15:02:34 -0400637const EVP_MD *ssl_get_handshake_digest(uint16_t version,
638 const SSL_CIPHER *cipher) {
639 switch (cipher->algorithm_prf) {
David Benjaminb0883312015-08-06 09:54:13 -0400640 case SSL_HANDSHAKE_MAC_DEFAULT:
Steven Valdez908ac192017-01-12 13:17:07 -0500641 return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1();
David Benjaminb0883312015-08-06 09:54:13 -0400642 case SSL_HANDSHAKE_MAC_SHA256:
643 return EVP_sha256();
644 case SSL_HANDSHAKE_MAC_SHA384:
645 return EVP_sha384();
646 default:
David Benjaminca9e8f52017-08-09 15:02:34 -0400647 assert(0);
David Benjaminb0883312015-08-06 09:54:13 -0400648 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650}
651
David Benjamin8525ff32018-09-05 18:44:15 -0500652static bool is_cipher_list_separator(char c, bool is_strict) {
Adam Langley22df6912017-07-25 12:27:37 -0700653 if (c == ':') {
654 return true;
655 }
656 return !is_strict && (c == ' ' || c == ';' || c == ',');
657}
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjaminf4962492017-09-27 19:34:32 -0400659// rule_equals returns whether the NUL-terminated string |rule| is equal to the
David Benjaminc11ea9422017-08-29 16:33:21 -0400660// |buf_len| bytes at |buf|.
David Benjaminf4962492017-09-27 19:34:32 -0400661static bool rule_equals(const char *rule, const char *buf, size_t buf_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400662 // |strncmp| alone only checks that |buf| is a prefix of |rule|.
David Benjamin0344daf2015-04-08 02:08:01 -0400663 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
664}
665
Adam Langley95c29f32014-06-20 12:00:00 -0700666static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 CIPHER_ORDER **tail) {
668 if (curr == *tail) {
669 return;
670 }
671 if (curr == *head) {
672 *head = curr->next;
673 }
674 if (curr->prev != NULL) {
675 curr->prev->next = curr->next;
676 }
677 if (curr->next != NULL) {
678 curr->next->prev = curr->prev;
679 }
680 (*tail)->next = curr;
681 curr->prev = *tail;
682 curr->next = NULL;
683 *tail = curr;
684}
Adam Langley95c29f32014-06-20 12:00:00 -0700685
686static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800687 CIPHER_ORDER **tail) {
688 if (curr == *head) {
689 return;
690 }
691 if (curr == *tail) {
692 *tail = curr->prev;
693 }
694 if (curr->next != NULL) {
695 curr->next->prev = curr->prev;
696 }
697 if (curr->prev != NULL) {
698 curr->prev->next = curr->next;
699 }
700 (*head)->prev = curr;
701 curr->next = *head;
702 curr->prev = NULL;
703 *head = curr;
704}
Adam Langley95c29f32014-06-20 12:00:00 -0700705
David Benjamin48b276d2018-04-12 17:37:32 -0400706static bool ssl_cipher_collect_ciphers(Array<CIPHER_ORDER> *out_co_list,
707 CIPHER_ORDER **out_head,
708 CIPHER_ORDER **out_tail) {
709 Array<CIPHER_ORDER> co_list;
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700710 if (!co_list.Init(OPENSSL_ARRAY_SIZE(kCiphers))) {
David Benjamin48b276d2018-04-12 17:37:32 -0400711 return false;
712 }
713
David Benjamina1c90a52015-05-30 17:03:14 -0400714 size_t co_list_num = 0;
David Benjamin5be3a742017-10-28 16:08:58 -0400715 for (const SSL_CIPHER &cipher : kCiphers) {
716 // TLS 1.3 ciphers do not participate in this mechanism.
717 if (cipher.algorithm_mkey != SSL_kGENERIC) {
718 co_list[co_list_num].cipher = &cipher;
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 co_list[co_list_num].next = NULL;
720 co_list[co_list_num].prev = NULL;
David Benjaminf4962492017-09-27 19:34:32 -0400721 co_list[co_list_num].active = false;
722 co_list[co_list_num].in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 co_list_num++;
724 }
725 }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
David Benjaminc11ea9422017-08-29 16:33:21 -0400727 // Prepare linked list from list entries.
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 if (co_list_num > 0) {
729 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 if (co_list_num > 1) {
732 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700733
David Benjamin54091232016-09-05 12:47:25 -0400734 for (size_t i = 1; i < co_list_num - 1; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 co_list[i].prev = &co_list[i - 1];
736 co_list[i].next = &co_list[i + 1];
737 }
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
740 }
741
742 co_list[co_list_num - 1].next = NULL;
743
David Benjamin48b276d2018-04-12 17:37:32 -0400744 *out_head = &co_list[0];
745 *out_tail = &co_list[co_list_num - 1];
746 } else {
747 *out_head = nullptr;
748 *out_tail = nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 }
David Benjamin48b276d2018-04-12 17:37:32 -0400750 *out_co_list = std::move(co_list);
751 return true;
752}
753
754SSLCipherPreferenceList::~SSLCipherPreferenceList() {
755 OPENSSL_free(in_group_flags);
756}
757
758bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers_arg,
759 Span<const bool> in_group_flags_arg) {
760 if (sk_SSL_CIPHER_num(ciphers_arg.get()) != in_group_flags_arg.size()) {
761 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
762 return false;
763 }
764
765 Array<bool> copy;
766 if (!copy.CopyFrom(in_group_flags_arg)) {
767 return false;
768 }
769 ciphers = std::move(ciphers_arg);
770 size_t unused_len;
771 copy.Release(&in_group_flags, &unused_len);
772 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800773}
Adam Langley95c29f32014-06-20 12:00:00 -0700774
Matthew Braithwaited2ed3822018-07-10 16:27:22 -0700775bool SSLCipherPreferenceList::Init(const SSLCipherPreferenceList& other) {
776 size_t size = sk_SSL_CIPHER_num(other.ciphers.get());
777 Span<const bool> other_flags(other.in_group_flags, size);
778 UniquePtr<STACK_OF(SSL_CIPHER)> other_ciphers(sk_SSL_CIPHER_dup(
779 other.ciphers.get()));
780 if (!other_ciphers) {
781 return false;
782 }
783 return Init(std::move(other_ciphers), other_flags);
784}
785
786void SSLCipherPreferenceList::Remove(const SSL_CIPHER *cipher) {
787 size_t index;
788 if (!sk_SSL_CIPHER_find(ciphers.get(), &index, cipher)) {
789 return;
790 }
791 if (!in_group_flags[index] /* last element of group */ && index > 0) {
792 in_group_flags[index-1] = false;
793 }
794 for (size_t i = index; i < sk_SSL_CIPHER_num(ciphers.get()) - 1; ++i) {
795 in_group_flags[i] = in_group_flags[i+1];
796 }
797 sk_SSL_CIPHER_delete(ciphers.get(), index);
798}
799
David Benjaminc11ea9422017-08-29 16:33:21 -0400800// ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
801// parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
802// head and tail of the list to |*head_p| and |*tail_p|, respectively.
803//
804// - If |cipher_id| is non-zero, only that cipher is selected.
805// - Otherwise, if |strength_bits| is non-negative, it selects ciphers
806// of that strength.
807// - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
808// |min_version|.
Adam Langleyfcf25832014-12-18 17:42:32 -0800809static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400810 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
David Benjamind6e9eec2015-11-18 09:48:55 -0500811 uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule,
David Benjaminf4962492017-09-27 19:34:32 -0400812 int strength_bits, bool in_group, CIPHER_ORDER **head_p,
David Benjamind6e9eec2015-11-18 09:48:55 -0500813 CIPHER_ORDER **tail_p) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 CIPHER_ORDER *head, *tail, *curr, *next, *last;
815 const SSL_CIPHER *cp;
David Benjaminf4962492017-09-27 19:34:32 -0400816 bool reverse = false;
Adam Langley95c29f32014-06-20 12:00:00 -0700817
David Benjamindcb6ef02015-11-06 15:35:54 -0500818 if (cipher_id == 0 && strength_bits == -1 && min_version == 0 &&
David Benjamind6e9eec2015-11-18 09:48:55 -0500819 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400820 // The rule matches nothing, so bail early.
David Benjamin0344daf2015-04-08 02:08:01 -0400821 return;
822 }
823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 if (rule == CIPHER_DEL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400825 // needed to maintain sorting between currently deleted ciphers
David Benjaminf4962492017-09-27 19:34:32 -0400826 reverse = true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 head = *head_p;
830 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 if (reverse) {
833 next = tail;
834 last = head;
835 } else {
836 next = head;
837 last = tail;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 curr = NULL;
841 for (;;) {
842 if (curr == last) {
843 break;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 curr = next;
847 if (curr == NULL) {
848 break;
849 }
Adam Langleye3142a72014-07-24 17:56:48 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 next = reverse ? curr->prev : curr->next;
852 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700853
David Benjaminc11ea9422017-08-29 16:33:21 -0400854 // Selection criteria is either a specific cipher, the value of
855 // |strength_bits|, or the algorithms used.
David Benjamin0344daf2015-04-08 02:08:01 -0400856 if (cipher_id != 0) {
857 if (cipher_id != cp->id) {
858 continue;
859 }
860 } else if (strength_bits >= 0) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500861 if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 continue;
863 }
David Benjamin881f1962016-08-10 18:29:12 -0400864 } else {
865 if (!(alg_mkey & cp->algorithm_mkey) ||
866 !(alg_auth & cp->algorithm_auth) ||
867 !(alg_enc & cp->algorithm_enc) ||
868 !(alg_mac & cp->algorithm_mac) ||
David Benjamin3b903f22017-09-29 18:44:56 -0400869 (min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version) ||
870 // The NULL cipher must be selected explicitly.
871 cp->algorithm_enc == SSL_eNULL) {
David Benjamin881f1962016-08-10 18:29:12 -0400872 continue;
873 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 }
Adam Langleye3142a72014-07-24 17:56:48 -0700875
David Benjaminc11ea9422017-08-29 16:33:21 -0400876 // add the cipher if it has not been added yet.
Adam Langleyfcf25832014-12-18 17:42:32 -0800877 if (rule == CIPHER_ADD) {
David Benjaminf4962492017-09-27 19:34:32 -0400878 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 if (!curr->active) {
880 ll_append_tail(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400881 curr->active = true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 curr->in_group = in_group;
883 }
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
David Benjaminc11ea9422017-08-29 16:33:21 -0400886 // Move the added cipher to this location
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 else if (rule == CIPHER_ORD) {
David Benjaminf4962492017-09-27 19:34:32 -0400888 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (curr->active) {
890 ll_append_tail(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400891 curr->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 }
893 } else if (rule == CIPHER_DEL) {
David Benjaminf4962492017-09-27 19:34:32 -0400894 // reverse == true
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 if (curr->active) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400896 // most recently deleted ciphersuites get best positions
897 // for any future CIPHER_ADD (note that the CIPHER_DEL loop
898 // works in reverse to maintain the order)
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 ll_append_head(&head, curr, &tail);
David Benjaminf4962492017-09-27 19:34:32 -0400900 curr->active = false;
901 curr->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 }
903 } else if (rule == CIPHER_KILL) {
David Benjaminf4962492017-09-27 19:34:32 -0400904 // reverse == false
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 if (head == curr) {
906 head = curr->next;
907 } else {
908 curr->prev->next = curr->next;
909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 if (tail == curr) {
912 tail = curr->prev;
913 }
David Benjaminf4962492017-09-27 19:34:32 -0400914 curr->active = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 if (curr->next != NULL) {
916 curr->next->prev = curr->prev;
917 }
918 if (curr->prev != NULL) {
919 curr->prev->next = curr->next;
920 }
921 curr->next = NULL;
922 curr->prev = NULL;
923 }
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 *head_p = head;
927 *tail_p = tail;
928}
Adam Langley95c29f32014-06-20 12:00:00 -0700929
David Benjaminf4962492017-09-27 19:34:32 -0400930static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
931 CIPHER_ORDER **tail_p) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400932 // This routine sorts the ciphers with descending strength. The sorting must
933 // keep the pre-sorted sequence, so we apply the normal sorting routine as
934 // '+' movement to the end of the list.
David Benjaminf4962492017-09-27 19:34:32 -0400935 int max_strength_bits = 0;
936 CIPHER_ORDER *curr = *head_p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 while (curr != NULL) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500938 if (curr->active &&
939 SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) {
940 max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 }
942 curr = curr->next;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
David Benjaminf4962492017-09-27 19:34:32 -0400945 Array<int> number_uses;
946 if (!number_uses.Init(max_strength_bits + 1)) {
947 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 }
David Benjaminf4962492017-09-27 19:34:32 -0400949 OPENSSL_memset(number_uses.data(), 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700950
David Benjaminc11ea9422017-08-29 16:33:21 -0400951 // Now find the strength_bits values actually used.
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 curr = *head_p;
953 while (curr != NULL) {
954 if (curr->active) {
David Benjamin9f2e2772015-11-18 09:59:43 -0500955 number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 }
957 curr = curr->next;
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
David Benjaminc11ea9422017-08-29 16:33:21 -0400960 // Go through the list of used strength_bits values in descending order.
David Benjaminf4962492017-09-27 19:34:32 -0400961 for (int i = max_strength_bits; i >= 0; i--) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 if (number_uses[i] > 0) {
David Benjaminf4962492017-09-27 19:34:32 -0400963 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, false, head_p,
964 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 }
966 }
967
David Benjaminf4962492017-09-27 19:34:32 -0400968 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800969}
Adam Langley95c29f32014-06-20 12:00:00 -0700970
David Benjamin5be3a742017-10-28 16:08:58 -0400971static bool ssl_cipher_process_rulestr(const char *rule_str,
David Benjaminf4962492017-09-27 19:34:32 -0400972 CIPHER_ORDER **head_p,
973 CIPHER_ORDER **tail_p, bool strict) {
David Benjamind6e9eec2015-11-18 09:48:55 -0500974 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -0500975 uint16_t min_version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 const char *l, *buf;
David Benjaminf4962492017-09-27 19:34:32 -0400977 int rule;
978 bool multi, skip_rule, in_group = false, has_group = false;
David Benjamin0344daf2015-04-08 02:08:01 -0400979 size_t j, buf_len;
980 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 l = rule_str;
984 for (;;) {
985 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langleyfcf25832014-12-18 17:42:32 -0800987 if (ch == '\0') {
David Benjaminc11ea9422017-08-29 16:33:21 -0400988 break; // done
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 }
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 if (in_group) {
992 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 if (*tail_p) {
David Benjaminf4962492017-09-27 19:34:32 -0400994 (*tail_p)->in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 }
David Benjaminf4962492017-09-27 19:34:32 -0400996 in_group = false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 l++;
998 continue;
999 }
David Benjamin37d92462014-09-20 17:54:24 -04001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 if (ch == '|') {
1002 rule = CIPHER_ADD;
1003 l++;
1004 continue;
1005 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
1006 !(ch >= '0' && ch <= '9')) {
David Benjamin3570d732015-06-29 00:28:17 -04001007 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjaminf4962492017-09-27 19:34:32 -04001008 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 } else {
1010 rule = CIPHER_ADD;
1011 }
1012 } else if (ch == '-') {
1013 rule = CIPHER_DEL;
1014 l++;
1015 } else if (ch == '+') {
1016 rule = CIPHER_ORD;
1017 l++;
1018 } else if (ch == '!') {
1019 rule = CIPHER_KILL;
1020 l++;
1021 } else if (ch == '@') {
1022 rule = CIPHER_SPECIAL;
1023 l++;
1024 } else if (ch == '[') {
David Benjaminbf5f1922017-07-01 11:13:53 -04001025 assert(!in_group);
David Benjaminf4962492017-09-27 19:34:32 -04001026 in_group = true;
1027 has_group = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 l++;
1029 continue;
1030 } else {
1031 rule = CIPHER_ADD;
1032 }
Adam Langley95c29f32014-06-20 12:00:00 -07001033
David Benjaminc11ea9422017-08-29 16:33:21 -04001034 // If preference groups are enabled, the only legal operator is +.
1035 // Otherwise the in_group bits will get mixed up.
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 if (has_group && rule != CIPHER_ADD) {
David Benjamin3570d732015-06-29 00:28:17 -04001037 OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjaminf4962492017-09-27 19:34:32 -04001038 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langley22df6912017-07-25 12:27:37 -07001041 if (is_cipher_list_separator(ch, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 l++;
1043 continue;
1044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjaminf4962492017-09-27 19:34:32 -04001046 multi = false;
David Benjamin0344daf2015-04-08 02:08:01 -04001047 cipher_id = 0;
1048 alg_mkey = ~0u;
1049 alg_auth = ~0u;
1050 alg_enc = ~0u;
1051 alg_mac = ~0u;
David Benjamindcb6ef02015-11-06 15:35:54 -05001052 min_version = 0;
David Benjaminf4962492017-09-27 19:34:32 -04001053 skip_rule = false;
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 for (;;) {
1056 ch = *l;
1057 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -04001058 buf_len = 0;
David Benjamin6fff3862017-06-21 21:07:04 -04001059 while ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') ||
1060 (ch >= 'a' && ch <= 'z') || ch == '-' || ch == '.' || ch == '_') {
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -04001062 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
David Benjamin0344daf2015-04-08 02:08:01 -04001065 if (buf_len == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001066 // We hit something we cannot deal with, it is no command or separator
1067 // nor alphanumeric, so we call this an error.
David Benjamin3570d732015-06-29 00:28:17 -04001068 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001069 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 break;
1074 }
David Benjamin0344daf2015-04-08 02:08:01 -04001075
David Benjaminc11ea9422017-08-29 16:33:21 -04001076 // Look for a matching exact cipher. These aren't allowed in multipart
1077 // rules.
David Benjamin0344daf2015-04-08 02:08:01 -04001078 if (!multi && ch != '+') {
Matthew Braithwaited2ed3822018-07-10 16:27:22 -07001079 for (j = 0; j < OPENSSL_ARRAY_SIZE(kCiphers); j++) {
David Benjamina1c90a52015-05-30 17:03:14 -04001080 const SSL_CIPHER *cipher = &kCiphers[j];
David Benjamin6fff3862017-06-21 21:07:04 -04001081 if (rule_equals(cipher->name, buf, buf_len) ||
1082 rule_equals(cipher->standard_name, buf, buf_len)) {
David Benjamin0344daf2015-04-08 02:08:01 -04001083 cipher_id = cipher->id;
1084 break;
1085 }
1086 }
1087 }
1088 if (cipher_id == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001089 // If not an exact cipher, look for a matching cipher alias.
David Benjamina1c90a52015-05-30 17:03:14 -04001090 for (j = 0; j < kCipherAliasesLen; j++) {
David Benjamin0344daf2015-04-08 02:08:01 -04001091 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
1092 alg_mkey &= kCipherAliases[j].algorithm_mkey;
1093 alg_auth &= kCipherAliases[j].algorithm_auth;
1094 alg_enc &= kCipherAliases[j].algorithm_enc;
1095 alg_mac &= kCipherAliases[j].algorithm_mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001096
1097 if (min_version != 0 &&
1098 min_version != kCipherAliases[j].min_version) {
David Benjaminf4962492017-09-27 19:34:32 -04001099 skip_rule = true;
David Benjamindcb6ef02015-11-06 15:35:54 -05001100 } else {
1101 min_version = kCipherAliases[j].min_version;
1102 }
David Benjamin0344daf2015-04-08 02:08:01 -04001103 break;
1104 }
1105 }
David Benjamina1c90a52015-05-30 17:03:14 -04001106 if (j == kCipherAliasesLen) {
David Benjaminf4962492017-09-27 19:34:32 -04001107 skip_rule = true;
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001108 if (strict) {
1109 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001110 return false;
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001111 }
David Benjamin0344daf2015-04-08 02:08:01 -04001112 }
1113 }
1114
David Benjaminc11ea9422017-08-29 16:33:21 -04001115 // Check for a multipart rule.
David Benjamin0344daf2015-04-08 02:08:01 -04001116 if (ch != '+') {
1117 break;
1118 }
1119 l++;
David Benjaminf4962492017-09-27 19:34:32 -04001120 multi = true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 }
Adam Langley95c29f32014-06-20 12:00:00 -07001122
David Benjaminc11ea9422017-08-29 16:33:21 -04001123 // Ok, we have the rule, now apply it.
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (rule == CIPHER_SPECIAL) {
David Benjaminbf5f1922017-07-01 11:13:53 -04001125 if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001126 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001127 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 }
David Benjaminbf5f1922017-07-01 11:13:53 -04001129 if (!ssl_cipher_strength_sort(head_p, tail_p)) {
David Benjaminf4962492017-09-27 19:34:32 -04001130 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
David Benjaminc11ea9422017-08-29 16:33:21 -04001133 // We do not support any "multi" options together with "@", so throw away
1134 // the rest of the command, if any left, until end or ':' is found.
Adam Langley22df6912017-07-25 12:27:37 -07001135 while (*l != '\0' && !is_cipher_list_separator(*l, strict)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 l++;
1137 }
David Benjamindcb6ef02015-11-06 15:35:54 -05001138 } else if (!skip_rule) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
David Benjamind6e9eec2015-11-18 09:48:55 -05001140 min_version, rule, -1, in_group, head_p, tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 }
1142 }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 if (in_group) {
David Benjamin3570d732015-06-29 00:28:17 -04001145 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
David Benjaminf4962492017-09-27 19:34:32 -04001146 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
David Benjaminf4962492017-09-27 19:34:32 -04001149 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001150}
Adam Langley95c29f32014-06-20 12:00:00 -07001151
David Benjamin0ce090a2018-07-02 20:24:40 -04001152bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list,
David Benjamin48b276d2018-04-12 17:37:32 -04001153 const char *rule_str, bool strict) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001154 // Return with error if nothing to do.
David Benjamin71f07942015-04-08 02:36:59 -04001155 if (rule_str == NULL || out_cipher_list == NULL) {
David Benjaminf4962492017-09-27 19:34:32 -04001156 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 }
David Benjamin5213df42014-08-20 14:19:54 -04001158
David Benjaminc11ea9422017-08-29 16:33:21 -04001159 // Now we have to collect the available ciphers from the compiled in ciphers.
1160 // We cannot get more than the number compiled in, so it is used for
1161 // allocation.
David Benjamin48b276d2018-04-12 17:37:32 -04001162 Array<CIPHER_ORDER> co_list;
1163 CIPHER_ORDER *head = nullptr, *tail = nullptr;
1164 if (!ssl_cipher_collect_ciphers(&co_list, &head, &tail)) {
David Benjaminf4962492017-09-27 19:34:32 -04001165 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
David Benjaminc11ea9422017-08-29 16:33:21 -04001168 // Now arrange all ciphers by preference:
1169 // TODO(davidben): Compute this order once and copy it.
Adam Langley95c29f32014-06-20 12:00:00 -07001170
David Benjaminc11ea9422017-08-29 16:33:21 -04001171 // Everything else being equal, prefer ECDHE_ECDSA and ECDHE_RSA over other
1172 // key exchange mechanisms
David Benjamind6e9eec2015-11-18 09:48:55 -05001173 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1,
David Benjaminf4962492017-09-27 19:34:32 -04001174 false, &head, &tail);
1175 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamind6e9eec2015-11-18 09:48:55 -05001176 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001177 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
Steven Valdez803c77a2016-09-06 14:13:43 -04001178 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001179
David Benjaminc11ea9422017-08-29 16:33:21 -04001180 // Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1181 // CHACHA20 unless there is hardware support for fast and constant-time
1182 // AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the
1183 // old one.
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 if (EVP_has_aes_hardware()) {
David Benjaminf4962492017-09-27 19:34:32 -04001185 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
1186 false, &head, &tail);
1187 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
1188 false, &head, &tail);
David Benjamin13414b32015-12-09 23:02:39 -05001189 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
David Benjaminf4962492017-09-27 19:34:32 -04001190 -1, false, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 } else {
David Benjamin13414b32015-12-09 23:02:39 -05001192 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD,
David Benjaminf4962492017-09-27 19:34:32 -04001193 -1, false, &head, &tail);
1194 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1,
1195 false, &head, &tail);
1196 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1,
1197 false, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 }
Adam Langley95c29f32014-06-20 12:00:00 -07001199
David Benjaminc11ea9422017-08-29 16:33:21 -04001200 // Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC,
1201 // 3DES_EDE_CBC_SHA.
David Benjaminf4962492017-09-27 19:34:32 -04001202 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamind6e9eec2015-11-18 09:48:55 -05001203 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001204 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, false,
David Benjamin43336652016-03-03 15:32:29 -05001205 &head, &tail);
David Benjaminf4962492017-09-27 19:34:32 -04001206 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, false,
1207 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001208
David Benjaminc11ea9422017-08-29 16:33:21 -04001209 // Temporarily enable everything else for sorting
David Benjaminf4962492017-09-27 19:34:32 -04001210 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false, &head,
David Benjamind6e9eec2015-11-18 09:48:55 -05001211 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001212
David Benjaminc11ea9422017-08-29 16:33:21 -04001213 // Move ciphers without forward secrecy to the end.
David Benjaminf4962492017-09-27 19:34:32 -04001214 ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0, CIPHER_ORD,
1215 -1, false, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001216
David Benjaminc11ea9422017-08-29 16:33:21 -04001217 // Now disable everything (maintaining the ordering!)
David Benjaminf4962492017-09-27 19:34:32 -04001218 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head,
David Benjamind6e9eec2015-11-18 09:48:55 -05001219 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001220
David Benjaminc11ea9422017-08-29 16:33:21 -04001221 // If the rule_string begins with DEFAULT, apply the default rule before
1222 // using the (possibly available) additional rules.
David Benjamin11a7b3c2016-11-03 17:03:48 -04001223 const char *rule_p = rule_str;
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin5be3a742017-10-28 16:08:58 -04001225 if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1226 strict)) {
David Benjamin48b276d2018-04-12 17:37:32 -04001227 return false;
David Benjamin11a7b3c2016-11-03 17:03:48 -04001228 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 rule_p += 7;
1230 if (*rule_p == ':') {
1231 rule_p++;
1232 }
1233 }
Adam Langley858a88d2014-06-20 12:00:00 -07001234
David Benjamin11a7b3c2016-11-03 17:03:48 -04001235 if (*rule_p != '\0' &&
David Benjamin5be3a742017-10-28 16:08:58 -04001236 !ssl_cipher_process_rulestr(rule_p, &head, &tail, strict)) {
David Benjamin48b276d2018-04-12 17:37:32 -04001237 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 }
1239
David Benjaminc11ea9422017-08-29 16:33:21 -04001240 // Allocate new "cipherstack" for the result, return with error
1241 // if we cannot get one.
David Benjamin48b276d2018-04-12 17:37:32 -04001242 UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null());
1243 Array<bool> in_group_flags;
1244 if (cipherstack == nullptr ||
Matthew Braithwaited2ed3822018-07-10 16:27:22 -07001245 !in_group_flags.Init(OPENSSL_ARRAY_SIZE(kCiphers))) {
David Benjamin48b276d2018-04-12 17:37:32 -04001246 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 }
1248
David Benjaminc11ea9422017-08-29 16:33:21 -04001249 // The cipher selection for the list is done. The ciphers are added
1250 // to the resulting precedence to the STACK_OF(SSL_CIPHER).
David Benjamin48b276d2018-04-12 17:37:32 -04001251 size_t num_in_group_flags = 0;
1252 for (CIPHER_ORDER *curr = head; curr != NULL; curr = curr->next) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001253 if (curr->active) {
David Benjamin48b276d2018-04-12 17:37:32 -04001254 if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) {
1255 return false;
David Benjamin2adb7ec2015-01-11 19:59:06 -05001256 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 in_group_flags[num_in_group_flags++] = curr->in_group;
1258 }
1259 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001260
David Benjamin48b276d2018-04-12 17:37:32 -04001261 UniquePtr<SSLCipherPreferenceList> pref_list =
1262 MakeUnique<SSLCipherPreferenceList>();
1263 if (!pref_list ||
1264 !pref_list->Init(
1265 std::move(cipherstack),
1266 MakeConstSpan(in_group_flags).subspan(0, num_in_group_flags))) {
1267 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 }
David Benjamin48b276d2018-04-12 17:37:32 -04001269
David Benjamin0ce090a2018-07-02 20:24:40 -04001270 *out_cipher_list = std::move(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001271
David Benjaminc11ea9422017-08-29 16:33:21 -04001272 // Configuring an empty cipher list is an error but still updates the
1273 // output.
David Benjamin48b276d2018-04-12 17:37:32 -04001274 if (sk_SSL_CIPHER_num((*out_cipher_list)->ciphers.get()) == 0) {
David Benjamin91222b82017-03-09 20:10:56 -05001275 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
David Benjaminf4962492017-09-27 19:34:32 -04001276 return false;
David Benjamin91222b82017-03-09 20:10:56 -05001277 }
1278
David Benjaminf4962492017-09-27 19:34:32 -04001279 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08001280}
Adam Langley95c29f32014-06-20 12:00:00 -07001281
David Benjamin86e95b82017-07-18 16:34:25 -04001282uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) {
1283 switch (EVP_PKEY_id(key)) {
1284 case EVP_PKEY_RSA:
1285 return SSL_aRSA;
1286 case EVP_PKEY_EC:
1287 case EVP_PKEY_ED25519:
David Benjaminc11ea9422017-08-29 16:33:21 -04001288 // Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers.
David Benjamin86e95b82017-07-18 16:34:25 -04001289 return SSL_aECDSA;
1290 default:
1291 return 0;
1292 }
1293}
1294
David Benjaminf4962492017-09-27 19:34:32 -04001295bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) {
David Benjamin86e95b82017-07-18 16:34:25 -04001296 return (cipher->algorithm_auth & SSL_aCERT) != 0;
1297}
1298
David Benjaminf4962492017-09-27 19:34:32 -04001299bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1300 // Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is
1301 // optional or omitted in all others.
1302 return (cipher->algorithm_mkey & SSL_kECDHE) != 0;
David Benjamin86e95b82017-07-18 16:34:25 -04001303}
1304
1305size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
1306 size_t block_size;
1307 switch (cipher->algorithm_enc) {
1308 case SSL_3DES:
1309 block_size = 8;
1310 break;
1311 case SSL_AES128:
1312 case SSL_AES256:
1313 block_size = 16;
1314 break;
1315 default:
1316 return 0;
1317 }
1318
David Benjaminc11ea9422017-08-29 16:33:21 -04001319 // All supported TLS 1.0 ciphers use SHA-1.
David Benjamin86e95b82017-07-18 16:34:25 -04001320 assert(cipher->algorithm_mac == SSL_SHA1);
1321 size_t ret = 1 + SHA_DIGEST_LENGTH;
1322 ret += block_size - (ret % block_size);
1323 return ret;
1324}
1325
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07001326BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -04001327
1328using namespace bssl;
1329
MichaƂ Janiszewskic4f3b8a2018-07-30 22:50:40 +02001330static constexpr int ssl_cipher_id_cmp_inner(const SSL_CIPHER *a,
1331 const SSL_CIPHER *b) {
1332 // C++11's constexpr functions must have a body consisting of just a
1333 // return-statement.
1334 return (a->id > b->id) ? 1 : ((a->id < b->id) ? -1 : 0);
1335}
1336
1337static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
1338 return ssl_cipher_id_cmp_inner(reinterpret_cast<const SSL_CIPHER *>(in_a),
1339 reinterpret_cast<const SSL_CIPHER *>(in_b));
1340}
1341
1342template <typename T, size_t N>
1343static constexpr size_t countof(T const (&)[N]) {
1344 return N;
1345}
1346
1347template <typename T, size_t I>
1348static constexpr int check_order(const T (&arr)[I], size_t N) {
1349 // C++11's constexpr functions must have a body consisting of just a
1350 // return-statement.
1351 return N > 1 ? ((ssl_cipher_id_cmp_inner(&arr[N - 2], &arr[N - 1]) < 0)
1352 ? check_order(arr, N - 1)
1353 : 0)
1354 : 1;
1355}
1356
1357static_assert(check_order(kCiphers, countof(kCiphers)) == 1,
1358 "Ciphers are not sorted, bsearch won't work");
1359
David Benjamin86e95b82017-07-18 16:34:25 -04001360const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
1361 SSL_CIPHER c;
1362
1363 c.id = 0x03000000L | value;
1364 return reinterpret_cast<const SSL_CIPHER *>(bsearch(
Matthew Braithwaited2ed3822018-07-10 16:27:22 -07001365 &c, kCiphers, OPENSSL_ARRAY_SIZE(kCiphers), sizeof(SSL_CIPHER),
1366 ssl_cipher_id_cmp));
David Benjamin86e95b82017-07-18 16:34:25 -04001367}
1368
1369uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1370
David Benjamin3743aaf2020-09-21 13:55:16 -04001371uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *cipher) {
1372 // All OpenSSL cipher IDs are prefaced with 0x03. Historically this referred
1373 // to SSLv2 vs SSLv3.
1374 assert((cipher->id & 0xff000000) == 0x03000000);
Adam Langley04a89c82019-09-18 14:31:00 -07001375 return static_cast<uint16_t>(cipher->id);
1376}
1377
David Benjamin3743aaf2020-09-21 13:55:16 -04001378uint16_t SSL_CIPHER_get_value(const SSL_CIPHER *cipher) {
1379 return SSL_CIPHER_get_protocol_id(cipher);
1380}
1381
David Benjamin348f0d82017-08-10 16:06:27 -04001382int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) {
Alessandro Ghedini0726fb72017-01-17 13:27:08 +00001383 return (cipher->algorithm_mac & SSL_AEAD) != 0;
1384}
1385
David Benjamin348f0d82017-08-10 16:06:27 -04001386int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) {
1387 switch (cipher->algorithm_enc) {
1388 case SSL_eNULL:
1389 return NID_undef;
1390 case SSL_3DES:
1391 return NID_des_ede3_cbc;
1392 case SSL_AES128:
1393 return NID_aes_128_cbc;
1394 case SSL_AES256:
1395 return NID_aes_256_cbc;
1396 case SSL_AES128GCM:
1397 return NID_aes_128_gcm;
1398 case SSL_AES256GCM:
1399 return NID_aes_256_gcm;
1400 case SSL_CHACHA20POLY1305:
1401 return NID_chacha20_poly1305;
1402 }
1403 assert(0);
1404 return NID_undef;
1405}
1406
1407int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) {
1408 switch (cipher->algorithm_mac) {
1409 case SSL_AEAD:
1410 return NID_undef;
1411 case SSL_SHA1:
1412 return NID_sha1;
David Benjamin348f0d82017-08-10 16:06:27 -04001413 }
1414 assert(0);
1415 return NID_undef;
1416}
1417
1418int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) {
1419 switch (cipher->algorithm_mkey) {
1420 case SSL_kRSA:
1421 return NID_kx_rsa;
1422 case SSL_kECDHE:
1423 return NID_kx_ecdhe;
1424 case SSL_kPSK:
1425 return NID_kx_psk;
1426 case SSL_kGENERIC:
1427 return NID_kx_any;
1428 }
1429 assert(0);
1430 return NID_undef;
1431}
1432
1433int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) {
1434 switch (cipher->algorithm_auth) {
1435 case SSL_aRSA:
1436 return NID_auth_rsa;
1437 case SSL_aECDSA:
1438 return NID_auth_ecdsa;
1439 case SSL_aPSK:
1440 return NID_auth_psk;
1441 case SSL_aGENERIC:
1442 return NID_auth_any;
1443 }
1444 assert(0);
1445 return NID_undef;
1446}
1447
David Benjaminb1b76ae2017-09-21 17:03:34 -04001448int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) {
1449 switch (cipher->algorithm_prf) {
1450 case SSL_HANDSHAKE_MAC_DEFAULT:
1451 return NID_md5_sha1;
1452 case SSL_HANDSHAKE_MAC_SHA256:
1453 return NID_sha256;
1454 case SSL_HANDSHAKE_MAC_SHA384:
1455 return NID_sha384;
1456 }
1457 assert(0);
1458 return NID_undef;
1459}
1460
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001461int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) {
Matthew Braithwaite8aaa9e12016-09-07 15:09:58 -07001462 return (cipher->algorithm_enc & SSL_eNULL) == 0 &&
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001463 cipher->algorithm_mac != SSL_AEAD;
1464}
1465
David Benjaminef793f42015-11-05 18:16:27 -05001466uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001467 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1468 cipher->algorithm_auth == SSL_aGENERIC) {
1469 return TLS1_3_VERSION;
1470 }
1471
David Benjamindcb6ef02015-11-06 15:35:54 -05001472 if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001473 // Cipher suites before TLS 1.2 use the default PRF, while all those added
1474 // afterwards specify a particular hash.
David Benjaminef793f42015-11-05 18:16:27 -05001475 return TLS1_2_VERSION;
1476 }
1477 return SSL3_VERSION;
1478}
1479
Nick Harper1fd39d82016-06-14 18:14:35 -07001480uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001481 if (cipher->algorithm_mkey == SSL_kGENERIC ||
1482 cipher->algorithm_auth == SSL_aGENERIC) {
Nick Harper1fd39d82016-06-14 18:14:35 -07001483 return TLS1_3_VERSION;
1484 }
1485 return TLS1_2_VERSION;
1486}
1487
David Benjaminc11ea9422017-08-29 16:33:21 -04001488// return the actual cipher being used
David Benjamin71f07942015-04-08 02:36:59 -04001489const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1490 if (cipher != NULL) {
1491 return cipher->name;
1492 }
1493
1494 return "(NONE)";
1495}
1496
David Benjamin6fff3862017-06-21 21:07:04 -04001497const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) {
1498 return cipher->standard_name;
1499}
1500
David Benjamin71f07942015-04-08 02:36:59 -04001501const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1502 if (cipher == NULL) {
1503 return "";
1504 }
1505
1506 switch (cipher->algorithm_mkey) {
1507 case SSL_kRSA:
1508 return "RSA";
1509
David Benjamin71f07942015-04-08 02:36:59 -04001510 case SSL_kECDHE:
1511 switch (cipher->algorithm_auth) {
1512 case SSL_aECDSA:
1513 return "ECDHE_ECDSA";
1514 case SSL_aRSA:
1515 return "ECDHE_RSA";
1516 case SSL_aPSK:
1517 return "ECDHE_PSK";
1518 default:
1519 assert(0);
1520 return "UNKNOWN";
1521 }
1522
1523 case SSL_kPSK:
1524 assert(cipher->algorithm_auth == SSL_aPSK);
1525 return "PSK";
1526
Steven Valdez803c77a2016-09-06 14:13:43 -04001527 case SSL_kGENERIC:
1528 assert(cipher->algorithm_auth == SSL_aGENERIC);
1529 return "GENERIC";
1530
David Benjamin71f07942015-04-08 02:36:59 -04001531 default:
1532 assert(0);
1533 return "UNKNOWN";
1534 }
1535}
1536
David Benjamin71f07942015-04-08 02:36:59 -04001537char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1538 if (cipher == NULL) {
1539 return NULL;
1540 }
1541
David Benjamin6fff3862017-06-21 21:07:04 -04001542 return OPENSSL_strdup(SSL_CIPHER_standard_name(cipher));
David Benjamin71f07942015-04-08 02:36:59 -04001543}
1544
1545int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1546 if (cipher == NULL) {
1547 return 0;
1548 }
1549
David Benjamin9f2e2772015-11-18 09:59:43 -05001550 int alg_bits, strength_bits;
1551 switch (cipher->algorithm_enc) {
1552 case SSL_AES128:
1553 case SSL_AES128GCM:
David Benjamin9f2e2772015-11-18 09:59:43 -05001554 alg_bits = 128;
1555 strength_bits = 128;
1556 break;
1557
1558 case SSL_AES256:
1559 case SSL_AES256GCM:
David Benjamin13414b32015-12-09 23:02:39 -05001560 case SSL_CHACHA20POLY1305:
David Benjamin9f2e2772015-11-18 09:59:43 -05001561 alg_bits = 256;
1562 strength_bits = 256;
1563 break;
1564
1565 case SSL_3DES:
1566 alg_bits = 168;
1567 strength_bits = 112;
1568 break;
1569
1570 case SSL_eNULL:
1571 alg_bits = 0;
1572 strength_bits = 0;
1573 break;
1574
1575 default:
1576 assert(0);
1577 alg_bits = 0;
1578 strength_bits = 0;
David Benjamin71f07942015-04-08 02:36:59 -04001579 }
David Benjamin9f2e2772015-11-18 09:59:43 -05001580
1581 if (out_alg_bits != NULL) {
1582 *out_alg_bits = alg_bits;
1583 }
1584 return strength_bits;
David Benjamin71f07942015-04-08 02:36:59 -04001585}
1586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1588 int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 const char *kx, *au, *enc, *mac;
David Benjamindcb6ef02015-11-06 15:35:54 -05001590 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 alg_mkey = cipher->algorithm_mkey;
1593 alg_auth = cipher->algorithm_auth;
1594 alg_enc = cipher->algorithm_enc;
1595 alg_mac = cipher->algorithm_mac;
Adam Langley95c29f32014-06-20 12:00:00 -07001596
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 switch (alg_mkey) {
1598 case SSL_kRSA:
1599 kx = "RSA";
1600 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001601
David Benjamin7061e282015-03-19 11:10:48 -04001602 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 kx = "ECDH";
1604 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001605
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 case SSL_kPSK:
1607 kx = "PSK";
1608 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Steven Valdez803c77a2016-09-06 14:13:43 -04001610 case SSL_kGENERIC:
1611 kx = "GENERIC";
1612 break;
1613
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 default:
1615 kx = "unknown";
1616 }
Adam Langley95c29f32014-06-20 12:00:00 -07001617
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 switch (alg_auth) {
1619 case SSL_aRSA:
1620 au = "RSA";
1621 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 case SSL_aECDSA:
1624 au = "ECDSA";
1625 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001626
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 case SSL_aPSK:
1628 au = "PSK";
1629 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001630
Steven Valdez803c77a2016-09-06 14:13:43 -04001631 case SSL_aGENERIC:
1632 au = "GENERIC";
1633 break;
1634
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 default:
1636 au = "unknown";
1637 break;
1638 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 switch (alg_enc) {
1641 case SSL_3DES:
1642 enc = "3DES(168)";
1643 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 case SSL_AES128:
1646 enc = "AES(128)";
1647 break;
1648
1649 case SSL_AES256:
1650 enc = "AES(256)";
1651 break;
1652
1653 case SSL_AES128GCM:
1654 enc = "AESGCM(128)";
1655 break;
1656
1657 case SSL_AES256GCM:
1658 enc = "AESGCM(256)";
1659 break;
1660
David Benjamin13414b32015-12-09 23:02:39 -05001661 case SSL_CHACHA20POLY1305:
Adam Langleyfcf25832014-12-18 17:42:32 -08001662 enc = "ChaCha20-Poly1305";
1663 break;
1664
Matt Braithwaiteaf096752015-09-02 19:48:16 -07001665 case SSL_eNULL:
1666 enc="None";
1667 break;
1668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 default:
1670 enc = "unknown";
1671 break;
1672 }
1673
1674 switch (alg_mac) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 case SSL_SHA1:
1676 mac = "SHA1";
1677 break;
1678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 case SSL_AEAD:
1680 mac = "AEAD";
1681 break;
1682
1683 default:
1684 mac = "unknown";
1685 break;
1686 }
1687
1688 if (buf == NULL) {
1689 len = 128;
David Benjamine64d2c72017-07-12 16:31:08 -04001690 buf = (char *)OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001691 if (buf == NULL) {
1692 return NULL;
1693 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 } else if (len < 128) {
1695 return "Buffer too small";
1696 }
1697
Brian Smith0687bdf2016-01-17 09:18:26 -10001698 BIO_snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1699 cipher->name, kx, au, enc, mac);
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 return buf;
1701}
1702
David Benjamin71f07942015-04-08 02:36:59 -04001703const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1704 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001705}
1706
David Benjamind55bd792017-05-18 11:33:08 -04001707STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001708
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001709int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
Adam Langleyfcf25832014-12-18 17:42:32 -08001710
Matt Braithwaite6a1275b2015-06-26 12:09:10 -07001711const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001712
Adam Langleyfa3e9c32018-03-14 17:32:04 -07001713const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; }
1714
1715int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; }
1716
Adam Langley3e9e0432016-10-03 15:58:07 -07001717void SSL_COMP_free_compression_methods(void) {}