blob: 2cafeb9a90c1b667190be6b71ab2caf33db34d8c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin71f07942015-04-08 02:36:59 -0400145#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400146#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500147#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700148#include <openssl/mem.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
David Benjamin71f07942015-04-08 02:36:59 -0400150#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700151
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400156 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400158};
Adam Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
161 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
162 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
163 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
164 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
165};
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167#define CIPHER_ADD 1
168#define CIPHER_KILL 2
169#define CIPHER_DEL 3
170#define CIPHER_ORD 4
171#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173typedef struct cipher_order_st {
174 const SSL_CIPHER *cipher;
175 int active;
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 int in_group;
177 struct cipher_order_st *next, *prev;
178} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjamin0344daf2015-04-08 02:08:01 -0400180typedef struct cipher_alias_st {
181 /* name is the name of the cipher alias. */
182 const char *name;
183
184 /* The following fields are bitmasks for the corresponding fields on
185 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
186 * bit corresponding to the cipher's value is set to 1. If any bitmask is
187 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
188 uint32_t algorithm_mkey;
189 uint32_t algorithm_auth;
190 uint32_t algorithm_enc;
191 uint32_t algorithm_mac;
192 uint32_t algorithm_ssl;
193 uint32_t algo_strength;
194} CIPHER_ALIAS;
195
196static const CIPHER_ALIAS kCipherAliases[] =
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 {
David Benjamin0344daf2015-04-08 02:08:01 -0400198 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin32fbdf22015-04-07 01:14:06 -0400200 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 /* key exchange aliases
203 * (some of those using only a single bit here combine
204 * multiple key exchange algs according to the RFCs,
205 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamin0344daf2015-04-08 02:08:01 -0400206 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin0344daf2015-04-08 02:08:01 -0400208 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
209 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
210 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin0344daf2015-04-08 02:08:01 -0400212 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
213 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
214 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin0344daf2015-04-08 02:08:01 -0400216 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 /* server authentication aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400219 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
220 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
221 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
222 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 /* aliases combining key exchange and server authentication */
David Benjamin0344daf2015-04-08 02:08:01 -0400225 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
226 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
227 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
228 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
229 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
230 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 /* symmetric encryption aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400233 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
234 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
235 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
236 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
237 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
238 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
239 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 /* MAC aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400242 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
243 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
244 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
245 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
246 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* protocol version aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400249 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
250 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
251 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 /* strength classes */
David Benjamin0344daf2015-04-08 02:08:01 -0400254 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
255 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* FIPS 140-2 approved ciphersuite */
David Benjamin0344daf2015-04-08 02:08:01 -0400257 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800258};
Adam Langley95c29f32014-06-20 12:00:00 -0700259
David Benjamin0344daf2015-04-08 02:08:01 -0400260#define NUM_CIPHER_ALIASES (sizeof(kCipherAliases) / sizeof(kCipherAliases[0]))
261
David Benjaminea72bd02014-12-21 21:27:41 -0500262int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
263 size_t *out_mac_secret_len,
264 size_t *out_fixed_iv_len,
265 const SSL_CIPHER *cipher, uint16_t version) {
266 *out_aead = NULL;
267 *out_mac_secret_len = 0;
268 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700269
David Benjaminea72bd02014-12-21 21:27:41 -0500270 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500272 *out_aead = EVP_aead_aes_128_gcm();
273 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 return 1;
275
276 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500277 *out_aead = EVP_aead_aes_256_gcm();
278 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 return 1;
280
281 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500282 *out_aead = EVP_aead_chacha20_poly1305();
283 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 return 1;
285
286 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500287 switch (cipher->algorithm_mac) {
288 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500289 if (version == SSL3_VERSION) {
290 *out_aead = EVP_aead_rc4_md5_ssl3();
291 } else {
292 *out_aead = EVP_aead_rc4_md5_tls();
293 }
David Benjaminea72bd02014-12-21 21:27:41 -0500294 *out_mac_secret_len = MD5_DIGEST_LENGTH;
295 return 1;
296 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500297 if (version == SSL3_VERSION) {
298 *out_aead = EVP_aead_rc4_sha1_ssl3();
299 } else {
300 *out_aead = EVP_aead_rc4_sha1_tls();
301 }
David Benjaminea72bd02014-12-21 21:27:41 -0500302 *out_mac_secret_len = SHA_DIGEST_LENGTH;
303 return 1;
304 default:
305 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800307
David Benjaminea72bd02014-12-21 21:27:41 -0500308 case SSL_AES128:
309 switch (cipher->algorithm_mac) {
310 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500311 if (version == SSL3_VERSION) {
312 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
313 *out_fixed_iv_len = 16;
314 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500315 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
316 *out_fixed_iv_len = 16;
317 } else {
318 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
319 }
320 *out_mac_secret_len = SHA_DIGEST_LENGTH;
321 return 1;
322 case SSL_SHA256:
323 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
324 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
325 return 1;
326 default:
327 return 0;
328 }
329
330 case SSL_AES256:
331 switch (cipher->algorithm_mac) {
332 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500333 if (version == SSL3_VERSION) {
334 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
335 *out_fixed_iv_len = 16;
336 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500337 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
338 *out_fixed_iv_len = 16;
339 } else {
340 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
341 }
342 *out_mac_secret_len = SHA_DIGEST_LENGTH;
343 return 1;
344 case SSL_SHA256:
345 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
346 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
347 return 1;
348 case SSL_SHA384:
349 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
350 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
351 return 1;
352 default:
353 return 0;
354 }
355
356 case SSL_3DES:
357 switch (cipher->algorithm_mac) {
358 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500359 if (version == SSL3_VERSION) {
360 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
361 *out_fixed_iv_len = 8;
362 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500363 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
364 *out_fixed_iv_len = 8;
365 } else {
366 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
367 }
368 *out_mac_secret_len = SHA_DIGEST_LENGTH;
369 return 1;
370 default:
371 return 0;
372 }
373
374 default:
375 return 0;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700378
David Benjamin71f07942015-04-08 02:36:59 -0400379int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
380 size_t idx) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500381 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 return 0;
383 }
David Benjamin71f07942015-04-08 02:36:59 -0400384 *out_mask = ssl_handshake_digests[idx].mask;
385 *out_md = ssl_handshake_digests[idx].md_func();
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700387}
388
389#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700391
David Benjamin0344daf2015-04-08 02:08:01 -0400392/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
393 * |buf_len| bytes at |buf|. */
394static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
395 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
396 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
397}
398
Adam Langley95c29f32014-06-20 12:00:00 -0700399static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 CIPHER_ORDER **tail) {
401 if (curr == *tail) {
402 return;
403 }
404 if (curr == *head) {
405 *head = curr->next;
406 }
407 if (curr->prev != NULL) {
408 curr->prev->next = curr->next;
409 }
410 if (curr->next != NULL) {
411 curr->next->prev = curr->prev;
412 }
413 (*tail)->next = curr;
414 curr->prev = *tail;
415 curr->next = NULL;
416 *tail = curr;
417}
Adam Langley95c29f32014-06-20 12:00:00 -0700418
419static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 CIPHER_ORDER **tail) {
421 if (curr == *head) {
422 return;
423 }
424 if (curr == *tail) {
425 *tail = curr->prev;
426 }
427 if (curr->next != NULL) {
428 curr->next->prev = curr->prev;
429 }
430 if (curr->prev != NULL) {
431 curr->prev->next = curr->next;
432 }
433 (*head)->prev = curr;
434 curr->next = *head;
435 curr->prev = NULL;
436 *head = curr;
437}
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamin82c9e902014-12-12 15:55:27 -0500439static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin0344daf2015-04-08 02:08:01 -0400440 size_t num_of_ciphers,
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 CIPHER_ORDER *co_list,
442 CIPHER_ORDER **head_p,
443 CIPHER_ORDER **tail_p) {
David Benjamin0344daf2015-04-08 02:08:01 -0400444 size_t i, co_list_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 /* We have num_of_ciphers descriptions compiled in, depending on the method
447 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
448 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 /* Get the initial list of ciphers */
451 co_list_num = 0; /* actual count of ciphers */
452 for (i = 0; i < num_of_ciphers; i++) {
David Benjamin0344daf2015-04-08 02:08:01 -0400453 const SSL_CIPHER *c = ssl_method->get_cipher(i);
454 if (c != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 co_list[co_list_num].cipher = c;
456 co_list[co_list_num].next = NULL;
457 co_list[co_list_num].prev = NULL;
458 co_list[co_list_num].active = 0;
459 co_list[co_list_num].in_group = 0;
460 co_list_num++;
461 }
462 }
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 /* Prepare linked list from list entries. */
465 if (co_list_num > 0) {
466 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (co_list_num > 1) {
469 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 for (i = 1; i < co_list_num - 1; i++) {
472 co_list[i].prev = &co_list[i - 1];
473 co_list[i].next = &co_list[i + 1];
474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
477 }
478
479 co_list[co_list_num - 1].next = NULL;
480
481 *head_p = &co_list[0];
482 *tail_p = &co_list[co_list_num - 1];
483 }
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
David Benjamin0344daf2015-04-08 02:08:01 -0400486/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
487 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
488 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
489 *
490 * - If |cipher_id| is non-zero, only that cipher is selected.
491 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
492 * of that strength.
493 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
494 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800495static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400496 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
497 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
498 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
500 CIPHER_ORDER *head, *tail, *curr, *next, *last;
501 const SSL_CIPHER *cp;
502 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin0344daf2015-04-08 02:08:01 -0400504 if (cipher_id == 0 && strength_bits == -1 &&
505 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
506 alg_ssl == 0 || algo_strength == 0)) {
507 /* The rule matches nothing, so bail early. */
508 return;
509 }
510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 if (rule == CIPHER_DEL) {
512 /* needed to maintain sorting between currently deleted ciphers */
513 reverse = 1;
514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 head = *head_p;
517 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 if (reverse) {
520 next = tail;
521 last = head;
522 } else {
523 next = head;
524 last = tail;
525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 curr = NULL;
528 for (;;) {
529 if (curr == last) {
530 break;
531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 curr = next;
534 if (curr == NULL) {
535 break;
536 }
Adam Langleye3142a72014-07-24 17:56:48 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 next = reverse ? curr->prev : curr->next;
539 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700540
David Benjamin0344daf2015-04-08 02:08:01 -0400541 /* Selection criteria is either a specific cipher, the value of
542 * |strength_bits|, or the algorithms used. */
543 if (cipher_id != 0) {
544 if (cipher_id != cp->id) {
545 continue;
546 }
547 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 if (strength_bits != cp->strength_bits) {
549 continue;
550 }
David Benjamin0344daf2015-04-08 02:08:01 -0400551 } else if (!(alg_mkey & cp->algorithm_mkey) ||
552 !(alg_auth & cp->algorithm_auth) ||
553 !(alg_enc & cp->algorithm_enc) ||
554 !(alg_mac & cp->algorithm_mac) ||
555 !(alg_ssl & cp->algorithm_ssl) ||
556 !(algo_strength & cp->algo_strength)) {
557 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 }
Adam Langleye3142a72014-07-24 17:56:48 -0700559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 /* add the cipher if it has not been added yet. */
561 if (rule == CIPHER_ADD) {
562 /* reverse == 0 */
563 if (!curr->active) {
564 ll_append_tail(&head, curr, &tail);
565 curr->active = 1;
566 curr->in_group = in_group;
567 }
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 /* Move the added cipher to this location */
571 else if (rule == CIPHER_ORD) {
572 /* reverse == 0 */
573 if (curr->active) {
574 ll_append_tail(&head, curr, &tail);
575 curr->in_group = 0;
576 }
577 } else if (rule == CIPHER_DEL) {
578 /* reverse == 1 */
579 if (curr->active) {
580 /* most recently deleted ciphersuites get best positions
581 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
582 * works in reverse to maintain the order) */
583 ll_append_head(&head, curr, &tail);
584 curr->active = 0;
585 curr->in_group = 0;
586 }
587 } else if (rule == CIPHER_KILL) {
588 /* reverse == 0 */
589 if (head == curr) {
590 head = curr->next;
591 } else {
592 curr->prev->next = curr->next;
593 }
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 if (tail == curr) {
596 tail = curr->prev;
597 }
598 curr->active = 0;
599 if (curr->next != NULL) {
600 curr->next->prev = curr->prev;
601 }
602 if (curr->prev != NULL) {
603 curr->prev->next = curr->next;
604 }
605 curr->next = NULL;
606 curr->prev = NULL;
607 }
608 }
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 *head_p = head;
611 *tail_p = tail;
612}
Adam Langley95c29f32014-06-20 12:00:00 -0700613
614static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 CIPHER_ORDER **tail_p) {
616 int max_strength_bits, i, *number_uses;
617 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 /* This routine sorts the ciphers with descending strength. The sorting must
620 * keep the pre-sorted sequence, so we apply the normal sorting routine as
621 * '+' movement to the end of the list. */
622 max_strength_bits = 0;
623 curr = *head_p;
624 while (curr != NULL) {
625 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
626 max_strength_bits = curr->cipher->strength_bits;
627 }
628 curr = curr->next;
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
632 if (!number_uses) {
633 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
634 return 0;
635 }
636 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 /* Now find the strength_bits values actually used. */
639 curr = *head_p;
640 while (curr != NULL) {
641 if (curr->active) {
642 number_uses[curr->cipher->strength_bits]++;
643 }
644 curr = curr->next;
645 }
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 /* Go through the list of used strength_bits values in descending order. */
648 for (i = max_strength_bits; i >= 0; i--) {
649 if (number_uses[i] > 0) {
650 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
651 tail_p);
652 }
653 }
654
655 OPENSSL_free(number_uses);
656 return 1;
657}
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjamin0344daf2015-04-08 02:08:01 -0400659static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
660 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400662 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -0400663 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -0400665 int multi, rule, retval, ok, in_group = 0, has_group = 0;
666 size_t j, buf_len;
667 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 retval = 1;
671 l = rule_str;
672 for (;;) {
673 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 if (ch == '\0') {
676 break; /* done */
677 }
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 if (in_group) {
680 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 if (*tail_p) {
682 (*tail_p)->in_group = 0;
683 }
684 in_group = 0;
685 l++;
686 continue;
687 }
David Benjamin37d92462014-09-20 17:54:24 -0400688
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 if (ch == '|') {
690 rule = CIPHER_ADD;
691 l++;
692 continue;
693 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
694 !(ch >= '0' && ch <= '9')) {
695 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
696 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400697 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 break;
699 } else {
700 rule = CIPHER_ADD;
701 }
702 } else if (ch == '-') {
703 rule = CIPHER_DEL;
704 l++;
705 } else if (ch == '+') {
706 rule = CIPHER_ORD;
707 l++;
708 } else if (ch == '!') {
709 rule = CIPHER_KILL;
710 l++;
711 } else if (ch == '@') {
712 rule = CIPHER_SPECIAL;
713 l++;
714 } else if (ch == '[') {
715 if (in_group) {
716 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400717 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 break;
719 }
720 in_group = 1;
721 has_group = 1;
722 l++;
723 continue;
724 } else {
725 rule = CIPHER_ADD;
726 }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 /* If preference groups are enabled, the only legal operator is +.
729 * Otherwise the in_group bits will get mixed up. */
730 if (has_group && rule != CIPHER_ADD) {
731 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
732 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -0400733 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 break;
735 }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737 if (ITEM_SEP(ch)) {
738 l++;
739 continue;
740 }
Adam Langley95c29f32014-06-20 12:00:00 -0700741
David Benjamin0344daf2015-04-08 02:08:01 -0400742 multi = 0;
743 cipher_id = 0;
744 alg_mkey = ~0u;
745 alg_auth = ~0u;
746 alg_enc = ~0u;
747 alg_mac = ~0u;
748 alg_ssl = ~0u;
749 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 for (;;) {
752 ch = *l;
753 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -0400754 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
756 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
757 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -0400758 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin0344daf2015-04-08 02:08:01 -0400761 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 /* We hit something we cannot deal with, it is no command or separator
763 * nor alphanumeric, so we call this an error. */
764 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
765 SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -0400766 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 l++;
768 break;
769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 break;
773 }
David Benjamin0344daf2015-04-08 02:08:01 -0400774
775 /* Look for a matching exact cipher. These aren't allowed in multipart
776 * rules. */
777 if (!multi && ch != '+') {
778 size_t num_ciphers = ssl_method->num_ciphers();
779 for (j = 0; j < num_ciphers; j++) {
780 const SSL_CIPHER *cipher = ssl_method->get_cipher(j);
781 if (cipher != NULL && rule_equals(cipher->name, buf, buf_len)) {
782 cipher_id = cipher->id;
783 break;
784 }
785 }
786 }
787 if (cipher_id == 0) {
788 /* If not an exact cipher, look for a matching cipher alias. */
789 for (j = 0; j < NUM_CIPHER_ALIASES; j++) {
790 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
791 alg_mkey &= kCipherAliases[j].algorithm_mkey;
792 alg_auth &= kCipherAliases[j].algorithm_auth;
793 alg_enc &= kCipherAliases[j].algorithm_enc;
794 alg_mac &= kCipherAliases[j].algorithm_mac;
795 alg_ssl &= kCipherAliases[j].algorithm_ssl;
796 algo_strength &= kCipherAliases[j].algo_strength;
797 break;
798 }
799 }
800 if (j == NUM_CIPHER_ALIASES) {
801 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
802 }
803 }
804
805 /* Check for a multipart rule. */
806 if (ch != '+') {
807 break;
808 }
809 l++;
810 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 /* Ok, we have the rule, now apply it. */
814 if (rule == CIPHER_SPECIAL) {
815 /* special command */
816 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -0400817 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 ok = ssl_cipher_strength_sort(head_p, tail_p);
819 } else {
820 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
821 SSL_R_INVALID_COMMAND);
822 }
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 if (ok == 0) {
825 retval = 0;
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 /* We do not support any "multi" options together with "@", so throw away
829 * the rest of the command, if any left, until end or ':' is found. */
830 while (*l != '\0' && !ITEM_SEP(*l)) {
831 l++;
832 }
David Benjamin0344daf2015-04-08 02:08:01 -0400833 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
835 alg_ssl, algo_strength, rule, -1, in_group, head_p,
836 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 }
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 if (in_group) {
841 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
842 retval = 0;
843 }
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 return retval;
846}
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848STACK_OF(SSL_CIPHER) *
849ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -0400850 struct ssl_cipher_preference_list_st **out_cipher_list,
851 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
852 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -0400853 int ok;
854 size_t num_of_ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
856 const char *rule_p;
857 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 uint8_t *in_group_flags = NULL;
859 unsigned int num_in_group_flags = 0;
860 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -0400863 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 return NULL;
865 }
David Benjamin5213df42014-08-20 14:19:54 -0400866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 /* Now we have to collect the available ciphers from the compiled in ciphers.
868 * We cannot get more than the number compiled in, so it is used for
869 * allocation. */
870 num_of_ciphers = ssl_method->num_ciphers();
871 co_list =
872 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
873 if (co_list == NULL) {
874 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
875 return NULL;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 /* Now arrange all ciphers by preference:
881 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
884 * key exchange mechanisms */
David Benjamin0344daf2015-04-08 02:08:01 -0400885 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
886 CIPHER_ADD, -1, 0, &head, &tail);
887 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400889 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
890 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
893 * CHACHA20 unless there is hardware support for fast and constant-time
894 * AES_GCM. */
895 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -0400896 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400898 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
899 -1, 0, &head, &tail);
900 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
901 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 } else {
David Benjamin0344daf2015-04-08 02:08:01 -0400903 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
904 CIPHER_ADD, -1, 0, &head, &tail);
905 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400907 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
908 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
912 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -0400913 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
914 0, &head, &tail);
915 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
916 0, &head, &tail);
917 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
918 -1, 0, &head, &tail);
919 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
920 0, &head, &tail);
921 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -0400925 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
926 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -0400929 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
930 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -0400933 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
934 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 /* If the rule_string begins with DEFAULT, apply the default rule before
937 * using the (possibly available) additional rules. */
938 ok = 1;
939 rule_p = rule_str;
940 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400941 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
942 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 rule_p += 7;
944 if (*rule_p == ':') {
945 rule_p++;
946 }
947 }
Adam Langley858a88d2014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400950 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 }
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 if (!ok) {
954 goto err;
955 }
956
957 /* Allocate new "cipherstack" for the result, return with error
958 * if we cannot get one. */
959 cipherstack = sk_SSL_CIPHER_new_null();
960 if (cipherstack == NULL) {
961 goto err;
962 }
963
964 in_group_flags = OPENSSL_malloc(num_of_ciphers);
965 if (!in_group_flags) {
966 goto err;
967 }
968
969 /* The cipher selection for the list is done. The ciphers are added
970 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
971 for (curr = head; curr != NULL; curr = curr->next) {
972 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -0500973 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
974 goto err;
975 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 in_group_flags[num_in_group_flags++] = curr->in_group;
977 }
978 }
979 OPENSSL_free(co_list); /* Not needed any longer */
980 co_list = NULL;
981
982 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
983 if (tmp_cipher_list == NULL) {
984 goto err;
985 }
986 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
987 if (!pref_list) {
988 goto err;
989 }
990 pref_list->ciphers = cipherstack;
991 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
992 if (!pref_list->in_group_flags) {
993 goto err;
994 }
995 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
996 OPENSSL_free(in_group_flags);
997 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -0400998 if (*out_cipher_list != NULL) {
999 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 }
David Benjamin71f07942015-04-08 02:36:59 -04001001 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 pref_list = NULL;
1003
David Benjamin71f07942015-04-08 02:36:59 -04001004 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001005 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001006 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001008 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1009 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001010
David Benjamin71f07942015-04-08 02:36:59 -04001011 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 } else {
1013 sk_SSL_CIPHER_free(tmp_cipher_list);
1014 tmp_cipher_list = NULL;
1015 }
1016
1017 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001018
1019err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001020 OPENSSL_free(co_list);
1021 OPENSSL_free(in_group_flags);
1022 sk_SSL_CIPHER_free(cipherstack);
1023 sk_SSL_CIPHER_free(tmp_cipher_list);
1024 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 OPENSSL_free(pref_list->in_group_flags);
1026 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001027 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 return NULL;
1029}
Adam Langley95c29f32014-06-20 12:00:00 -07001030
David Benjamin71f07942015-04-08 02:36:59 -04001031uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1032
1033int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1034 return (cipher->algorithm_enc & SSL_AES) != 0;
1035}
1036
1037int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1038 return (cipher->algorithm_mac & SSL_MD5) != 0;
1039}
1040
1041int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1042 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1043}
1044
1045int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1046 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1047}
1048
1049/* return the actual cipher being used */
1050const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1051 if (cipher != NULL) {
1052 return cipher->name;
1053 }
1054
1055 return "(NONE)";
1056}
1057
1058const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1059 if (cipher == NULL) {
1060 return "";
1061 }
1062
1063 switch (cipher->algorithm_mkey) {
1064 case SSL_kRSA:
1065 return "RSA";
1066
1067 case SSL_kDHE:
1068 switch (cipher->algorithm_auth) {
1069 case SSL_aRSA:
1070 return "DHE_RSA";
1071 default:
1072 assert(0);
1073 return "UNKNOWN";
1074 }
1075
1076 case SSL_kECDHE:
1077 switch (cipher->algorithm_auth) {
1078 case SSL_aECDSA:
1079 return "ECDHE_ECDSA";
1080 case SSL_aRSA:
1081 return "ECDHE_RSA";
1082 case SSL_aPSK:
1083 return "ECDHE_PSK";
1084 default:
1085 assert(0);
1086 return "UNKNOWN";
1087 }
1088
1089 case SSL_kPSK:
1090 assert(cipher->algorithm_auth == SSL_aPSK);
1091 return "PSK";
1092
1093 default:
1094 assert(0);
1095 return "UNKNOWN";
1096 }
1097}
1098
1099static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1100 switch (cipher->algorithm_enc) {
1101 case SSL_3DES:
1102 return "3DES_EDE_CBC";
1103 case SSL_RC4:
1104 return "RC4";
1105 case SSL_AES128:
1106 return "AES_128_CBC";
1107 case SSL_AES256:
1108 return "AES_256_CBC";
1109 case SSL_AES128GCM:
1110 return "AES_128_GCM";
1111 case SSL_AES256GCM:
1112 return "AES_256_GCM";
1113 case SSL_CHACHA20POLY1305:
1114 return "CHACHA20_POLY1305";
1115 break;
1116 default:
1117 assert(0);
1118 return "UNKNOWN";
1119 }
1120}
1121
1122static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
1123 if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) {
1124 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1125 * only ever MD5 or SHA-1. */
1126 switch (cipher->algorithm_mac) {
1127 case SSL_MD5:
1128 return "MD5";
1129 case SSL_SHA1:
1130 return "SHA";
1131 default:
1132 assert(0);
1133 return "UNKNOWN";
1134 }
1135 } else if (cipher->algorithm2 & TLS1_PRF_SHA256) {
1136 return "SHA256";
1137 } else if (cipher->algorithm2 & TLS1_PRF_SHA384) {
1138 return "SHA384";
1139 } else {
1140 assert(0);
1141 return "UNKNOWN";
1142 }
1143}
1144
1145char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1146 if (cipher == NULL) {
1147 return NULL;
1148 }
1149
1150 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1151 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1152 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1153
1154 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1155 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1156 strlen(prf_name) + 1;
1157 char *ret = OPENSSL_malloc(len);
1158 if (ret == NULL) {
1159 return NULL;
1160 }
1161 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1162 BUF_strlcat(ret, kx_name, len) >= len ||
1163 BUF_strlcat(ret, "_WITH_", len) >= len ||
1164 BUF_strlcat(ret, enc_name, len) >= len ||
1165 BUF_strlcat(ret, "_", len) >= len ||
1166 BUF_strlcat(ret, prf_name, len) >= len) {
1167 assert(0);
1168 OPENSSL_free(ret);
1169 return NULL;
1170 }
1171 assert(strlen(ret) + 1 == len);
1172 return ret;
1173}
1174
1175int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1176 if (cipher == NULL) {
1177 return 0;
1178 }
1179
1180 if (out_alg_bits != NULL) {
1181 *out_alg_bits = cipher->alg_bits;
1182 }
1183 return cipher->strength_bits;
1184}
1185
Adam Langleyfcf25832014-12-18 17:42:32 -08001186const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1187 int len) {
1188 const char *ver;
1189 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001190 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 alg_mkey = cipher->algorithm_mkey;
1194 alg_auth = cipher->algorithm_auth;
1195 alg_enc = cipher->algorithm_enc;
1196 alg_mac = cipher->algorithm_mac;
1197 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 if (alg_ssl & SSL_SSLV3) {
1200 ver = "SSLv3";
1201 } else if (alg_ssl & SSL_TLSV1_2) {
1202 ver = "TLSv1.2";
1203 } else {
1204 ver = "unknown";
1205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 switch (alg_mkey) {
1208 case SSL_kRSA:
1209 kx = "RSA";
1210 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001211
David Benjamin7061e282015-03-19 11:10:48 -04001212 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001213 kx = "DH";
1214 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001215
David Benjamin7061e282015-03-19 11:10:48 -04001216 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001217 kx = "ECDH";
1218 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001219
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 case SSL_kPSK:
1221 kx = "PSK";
1222 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224 default:
1225 kx = "unknown";
1226 }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 switch (alg_auth) {
1229 case SSL_aRSA:
1230 au = "RSA";
1231 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001232
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 case SSL_aECDSA:
1234 au = "ECDSA";
1235 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 case SSL_aPSK:
1238 au = "PSK";
1239 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001240
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 default:
1242 au = "unknown";
1243 break;
1244 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001245
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 switch (alg_enc) {
1247 case SSL_3DES:
1248 enc = "3DES(168)";
1249 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001250
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 case SSL_RC4:
1252 enc = "RC4(128)";
1253 break;
1254
1255 case SSL_AES128:
1256 enc = "AES(128)";
1257 break;
1258
1259 case SSL_AES256:
1260 enc = "AES(256)";
1261 break;
1262
1263 case SSL_AES128GCM:
1264 enc = "AESGCM(128)";
1265 break;
1266
1267 case SSL_AES256GCM:
1268 enc = "AESGCM(256)";
1269 break;
1270
1271 case SSL_CHACHA20POLY1305:
1272 enc = "ChaCha20-Poly1305";
1273 break;
1274
1275 default:
1276 enc = "unknown";
1277 break;
1278 }
1279
1280 switch (alg_mac) {
1281 case SSL_MD5:
1282 mac = "MD5";
1283 break;
1284
1285 case SSL_SHA1:
1286 mac = "SHA1";
1287 break;
1288
1289 case SSL_SHA256:
1290 mac = "SHA256";
1291 break;
1292
1293 case SSL_SHA384:
1294 mac = "SHA384";
1295 break;
1296
1297 case SSL_AEAD:
1298 mac = "AEAD";
1299 break;
1300
1301 default:
1302 mac = "unknown";
1303 break;
1304 }
1305
1306 if (buf == NULL) {
1307 len = 128;
1308 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001309 if (buf == NULL) {
1310 return NULL;
1311 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 } else if (len < 128) {
1313 return "Buffer too small";
1314 }
1315
1316 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1317 return buf;
1318}
1319
David Benjamin71f07942015-04-08 02:36:59 -04001320const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1321 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001322}
1323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324void *SSL_COMP_get_compression_methods(void) { return NULL; }
1325
1326int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1327
1328const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
David Benjamin71f07942015-04-08 02:36:59 -04001330int ssl_cipher_get_cert_index(const SSL_CIPHER *cipher) {
1331 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001332
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 if (alg_a & SSL_aECDSA) {
1334 return SSL_PKEY_ECC;
1335 } else if (alg_a & SSL_aRSA) {
1336 return SSL_PKEY_RSA_ENC;
1337 }
Adam Langley95c29f32014-06-20 12:00:00 -07001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 return -1;
1340}
David Benjamin9c651c92014-07-12 13:27:45 -04001341
Adam Langleyfcf25832014-12-18 17:42:32 -08001342int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001343 /* PSK-authenticated ciphers do not use a public key, except for
1344 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 if ((cipher->algorithm_auth & SSL_aPSK) &&
1346 !(cipher->algorithm_mkey & SSL_kRSA)) {
1347 return 0;
1348 }
1349
1350 /* All other ciphers include it. */
1351 return 1;
1352}
1353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1355 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001356 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 return 1;
1358 }
1359
1360 /* It is optional in all others. */
1361 return 0;
1362}