blob: 252a9a4c5df34d303b7932ad902a6b2bc10391b1 [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 Benjaminf0ae1702015-04-07 23:05:04 -0400145#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500146#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700147#include <openssl/mem.h>
148#include <openssl/obj.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin2ee94aa2015-04-07 22:38:30 -0400151#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400155 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800156 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400157};
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
160 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
161 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
162 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
163 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
164};
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langleyfcf25832014-12-18 17:42:32 -0800166#define CIPHER_ADD 1
167#define CIPHER_KILL 2
168#define CIPHER_DEL 3
169#define CIPHER_ORD 4
170#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172typedef struct cipher_order_st {
173 const SSL_CIPHER *cipher;
174 int active;
175 int dead;
176 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 Benjamin107db582015-04-08 00:41:59 -0400379int ssl_get_handshake_digest(size_t idx, uint32_t *mask, const EVP_MD **md) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500380 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 0;
382 }
383 *mask = ssl_handshake_digests[idx].mask;
384 *md = ssl_handshake_digests[idx].md_func();
385 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700386}
387
388#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700390
David Benjamin0344daf2015-04-08 02:08:01 -0400391/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
392 * |buf_len| bytes at |buf|. */
393static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
394 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
395 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
396}
397
Adam Langley95c29f32014-06-20 12:00:00 -0700398static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 CIPHER_ORDER **tail) {
400 if (curr == *tail) {
401 return;
402 }
403 if (curr == *head) {
404 *head = curr->next;
405 }
406 if (curr->prev != NULL) {
407 curr->prev->next = curr->next;
408 }
409 if (curr->next != NULL) {
410 curr->next->prev = curr->prev;
411 }
412 (*tail)->next = curr;
413 curr->prev = *tail;
414 curr->next = NULL;
415 *tail = curr;
416}
Adam Langley95c29f32014-06-20 12:00:00 -0700417
418static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 CIPHER_ORDER **tail) {
420 if (curr == *head) {
421 return;
422 }
423 if (curr == *tail) {
424 *tail = curr->prev;
425 }
426 if (curr->next != NULL) {
427 curr->next->prev = curr->prev;
428 }
429 if (curr->prev != NULL) {
430 curr->prev->next = curr->next;
431 }
432 (*head)->prev = curr;
433 curr->next = *head;
434 curr->prev = NULL;
435 *head = curr;
436}
Adam Langley95c29f32014-06-20 12:00:00 -0700437
David Benjamin82c9e902014-12-12 15:55:27 -0500438static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin0344daf2015-04-08 02:08:01 -0400439 size_t num_of_ciphers,
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 CIPHER_ORDER *co_list,
441 CIPHER_ORDER **head_p,
442 CIPHER_ORDER **tail_p) {
David Benjamin0344daf2015-04-08 02:08:01 -0400443 size_t i, co_list_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 /* We have num_of_ciphers descriptions compiled in, depending on the method
446 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
447 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 /* Get the initial list of ciphers */
450 co_list_num = 0; /* actual count of ciphers */
451 for (i = 0; i < num_of_ciphers; i++) {
David Benjamin0344daf2015-04-08 02:08:01 -0400452 const SSL_CIPHER *c = ssl_method->get_cipher(i);
453 if (c != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 co_list[co_list_num].cipher = c;
455 co_list[co_list_num].next = NULL;
456 co_list[co_list_num].prev = NULL;
457 co_list[co_list_num].active = 0;
458 co_list[co_list_num].in_group = 0;
459 co_list_num++;
460 }
461 }
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 /* Prepare linked list from list entries. */
464 if (co_list_num > 0) {
465 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 if (co_list_num > 1) {
468 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 for (i = 1; i < co_list_num - 1; i++) {
471 co_list[i].prev = &co_list[i - 1];
472 co_list[i].next = &co_list[i + 1];
473 }
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
476 }
477
478 co_list[co_list_num - 1].next = NULL;
479
480 *head_p = &co_list[0];
481 *tail_p = &co_list[co_list_num - 1];
482 }
483}
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin0344daf2015-04-08 02:08:01 -0400485/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
486 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
487 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
488 *
489 * - If |cipher_id| is non-zero, only that cipher is selected.
490 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
491 * of that strength.
492 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
493 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800494static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400495 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
496 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
497 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
499 CIPHER_ORDER *head, *tail, *curr, *next, *last;
500 const SSL_CIPHER *cp;
501 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjamin0344daf2015-04-08 02:08:01 -0400503 if (cipher_id == 0 && strength_bits == -1 &&
504 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
505 alg_ssl == 0 || algo_strength == 0)) {
506 /* The rule matches nothing, so bail early. */
507 return;
508 }
509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 if (rule == CIPHER_DEL) {
511 /* needed to maintain sorting between currently deleted ciphers */
512 reverse = 1;
513 }
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 head = *head_p;
516 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 if (reverse) {
519 next = tail;
520 last = head;
521 } else {
522 next = head;
523 last = tail;
524 }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 curr = NULL;
527 for (;;) {
528 if (curr == last) {
529 break;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 curr = next;
533 if (curr == NULL) {
534 break;
535 }
Adam Langleye3142a72014-07-24 17:56:48 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 next = reverse ? curr->prev : curr->next;
538 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700539
David Benjamin0344daf2015-04-08 02:08:01 -0400540 /* Selection criteria is either a specific cipher, the value of
541 * |strength_bits|, or the algorithms used. */
542 if (cipher_id != 0) {
543 if (cipher_id != cp->id) {
544 continue;
545 }
546 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 if (strength_bits != cp->strength_bits) {
548 continue;
549 }
David Benjamin0344daf2015-04-08 02:08:01 -0400550 } else if (!(alg_mkey & cp->algorithm_mkey) ||
551 !(alg_auth & cp->algorithm_auth) ||
552 !(alg_enc & cp->algorithm_enc) ||
553 !(alg_mac & cp->algorithm_mac) ||
554 !(alg_ssl & cp->algorithm_ssl) ||
555 !(algo_strength & cp->algo_strength)) {
556 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 }
Adam Langleye3142a72014-07-24 17:56:48 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 /* add the cipher if it has not been added yet. */
560 if (rule == CIPHER_ADD) {
561 /* reverse == 0 */
562 if (!curr->active) {
563 ll_append_tail(&head, curr, &tail);
564 curr->active = 1;
565 curr->in_group = in_group;
566 }
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 /* Move the added cipher to this location */
570 else if (rule == CIPHER_ORD) {
571 /* reverse == 0 */
572 if (curr->active) {
573 ll_append_tail(&head, curr, &tail);
574 curr->in_group = 0;
575 }
576 } else if (rule == CIPHER_DEL) {
577 /* reverse == 1 */
578 if (curr->active) {
579 /* most recently deleted ciphersuites get best positions
580 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
581 * works in reverse to maintain the order) */
582 ll_append_head(&head, curr, &tail);
583 curr->active = 0;
584 curr->in_group = 0;
585 }
586 } else if (rule == CIPHER_KILL) {
587 /* reverse == 0 */
588 if (head == curr) {
589 head = curr->next;
590 } else {
591 curr->prev->next = curr->next;
592 }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 if (tail == curr) {
595 tail = curr->prev;
596 }
597 curr->active = 0;
598 if (curr->next != NULL) {
599 curr->next->prev = curr->prev;
600 }
601 if (curr->prev != NULL) {
602 curr->prev->next = curr->next;
603 }
604 curr->next = NULL;
605 curr->prev = NULL;
606 }
607 }
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 *head_p = head;
610 *tail_p = tail;
611}
Adam Langley95c29f32014-06-20 12:00:00 -0700612
613static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 CIPHER_ORDER **tail_p) {
615 int max_strength_bits, i, *number_uses;
616 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 /* This routine sorts the ciphers with descending strength. The sorting must
619 * keep the pre-sorted sequence, so we apply the normal sorting routine as
620 * '+' movement to the end of the list. */
621 max_strength_bits = 0;
622 curr = *head_p;
623 while (curr != NULL) {
624 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
625 max_strength_bits = curr->cipher->strength_bits;
626 }
627 curr = curr->next;
628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
631 if (!number_uses) {
632 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
633 return 0;
634 }
635 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 /* Now find the strength_bits values actually used. */
638 curr = *head_p;
639 while (curr != NULL) {
640 if (curr->active) {
641 number_uses[curr->cipher->strength_bits]++;
642 }
643 curr = curr->next;
644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 /* Go through the list of used strength_bits values in descending order. */
647 for (i = max_strength_bits; i >= 0; i--) {
648 if (number_uses[i] > 0) {
649 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
650 tail_p);
651 }
652 }
653
654 OPENSSL_free(number_uses);
655 return 1;
656}
Adam Langley95c29f32014-06-20 12:00:00 -0700657
David Benjamin0344daf2015-04-08 02:08:01 -0400658static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
659 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400661 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -0400662 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -0400664 int multi, rule, retval, ok, in_group = 0, has_group = 0;
665 size_t j, buf_len;
666 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 retval = 1;
670 l = rule_str;
671 for (;;) {
672 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 if (ch == '\0') {
675 break; /* done */
676 }
Adam Langley95c29f32014-06-20 12:00:00 -0700677
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 if (in_group) {
679 if (ch == ']') {
680 if (!in_group) {
681 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
682 SSL_R_UNEXPECTED_GROUP_CLOSE);
David Benjamin0344daf2015-04-08 02:08:01 -0400683 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 break;
685 }
686 if (*tail_p) {
687 (*tail_p)->in_group = 0;
688 }
689 in_group = 0;
690 l++;
691 continue;
692 }
David Benjamin37d92462014-09-20 17:54:24 -0400693
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 if (ch == '|') {
695 rule = CIPHER_ADD;
696 l++;
697 continue;
698 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
699 !(ch >= '0' && ch <= '9')) {
700 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
701 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400702 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 break;
704 } else {
705 rule = CIPHER_ADD;
706 }
707 } else if (ch == '-') {
708 rule = CIPHER_DEL;
709 l++;
710 } else if (ch == '+') {
711 rule = CIPHER_ORD;
712 l++;
713 } else if (ch == '!') {
714 rule = CIPHER_KILL;
715 l++;
716 } else if (ch == '@') {
717 rule = CIPHER_SPECIAL;
718 l++;
719 } else if (ch == '[') {
720 if (in_group) {
721 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400722 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 break;
724 }
725 in_group = 1;
726 has_group = 1;
727 l++;
728 continue;
729 } else {
730 rule = CIPHER_ADD;
731 }
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langleyfcf25832014-12-18 17:42:32 -0800733 /* If preference groups are enabled, the only legal operator is +.
734 * Otherwise the in_group bits will get mixed up. */
735 if (has_group && rule != CIPHER_ADD) {
736 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
737 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -0400738 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 break;
740 }
Adam Langley95c29f32014-06-20 12:00:00 -0700741
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 if (ITEM_SEP(ch)) {
743 l++;
744 continue;
745 }
Adam Langley95c29f32014-06-20 12:00:00 -0700746
David Benjamin0344daf2015-04-08 02:08:01 -0400747 multi = 0;
748 cipher_id = 0;
749 alg_mkey = ~0u;
750 alg_auth = ~0u;
751 alg_enc = ~0u;
752 alg_mac = ~0u;
753 alg_ssl = ~0u;
754 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 for (;;) {
757 ch = *l;
758 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -0400759 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
761 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
762 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -0400763 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765
David Benjamin0344daf2015-04-08 02:08:01 -0400766 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 /* We hit something we cannot deal with, it is no command or separator
768 * nor alphanumeric, so we call this an error. */
769 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
770 SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -0400771 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 l++;
773 break;
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 break;
778 }
David Benjamin0344daf2015-04-08 02:08:01 -0400779
780 /* Look for a matching exact cipher. These aren't allowed in multipart
781 * rules. */
782 if (!multi && ch != '+') {
783 size_t num_ciphers = ssl_method->num_ciphers();
784 for (j = 0; j < num_ciphers; j++) {
785 const SSL_CIPHER *cipher = ssl_method->get_cipher(j);
786 if (cipher != NULL && rule_equals(cipher->name, buf, buf_len)) {
787 cipher_id = cipher->id;
788 break;
789 }
790 }
791 }
792 if (cipher_id == 0) {
793 /* If not an exact cipher, look for a matching cipher alias. */
794 for (j = 0; j < NUM_CIPHER_ALIASES; j++) {
795 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
796 alg_mkey &= kCipherAliases[j].algorithm_mkey;
797 alg_auth &= kCipherAliases[j].algorithm_auth;
798 alg_enc &= kCipherAliases[j].algorithm_enc;
799 alg_mac &= kCipherAliases[j].algorithm_mac;
800 alg_ssl &= kCipherAliases[j].algorithm_ssl;
801 algo_strength &= kCipherAliases[j].algo_strength;
802 break;
803 }
804 }
805 if (j == NUM_CIPHER_ALIASES) {
806 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
807 }
808 }
809
810 /* Check for a multipart rule. */
811 if (ch != '+') {
812 break;
813 }
814 l++;
815 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 /* Ok, we have the rule, now apply it. */
819 if (rule == CIPHER_SPECIAL) {
820 /* special command */
821 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -0400822 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 ok = ssl_cipher_strength_sort(head_p, tail_p);
824 } else {
825 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
826 SSL_R_INVALID_COMMAND);
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 if (ok == 0) {
830 retval = 0;
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 /* We do not support any "multi" options together with "@", so throw away
834 * the rest of the command, if any left, until end or ':' is found. */
835 while (*l != '\0' && !ITEM_SEP(*l)) {
836 l++;
837 }
David Benjamin0344daf2015-04-08 02:08:01 -0400838 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
840 alg_ssl, algo_strength, rule, -1, in_group, head_p,
841 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 }
843 }
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 if (in_group) {
846 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
847 retval = 0;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 return retval;
851}
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853STACK_OF(SSL_CIPHER) *
854ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
855 struct ssl_cipher_preference_list_st **cipher_list,
856 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
857 const char *rule_str, CERT *c) {
David Benjamin0344daf2015-04-08 02:08:01 -0400858 int ok;
859 size_t num_of_ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
861 const char *rule_p;
862 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 uint8_t *in_group_flags = NULL;
864 unsigned int num_in_group_flags = 0;
865 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 /* Return with error if nothing to do. */
868 if (rule_str == NULL || cipher_list == NULL) {
869 return NULL;
870 }
David Benjamin5213df42014-08-20 14:19:54 -0400871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 /* Now we have to collect the available ciphers from the compiled in ciphers.
873 * We cannot get more than the number compiled in, so it is used for
874 * allocation. */
875 num_of_ciphers = ssl_method->num_ciphers();
876 co_list =
877 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
878 if (co_list == NULL) {
879 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
880 return NULL;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 /* Now arrange all ciphers by preference:
886 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
889 * key exchange mechanisms */
David Benjamin0344daf2015-04-08 02:08:01 -0400890 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
891 CIPHER_ADD, -1, 0, &head, &tail);
892 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400894 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
895 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
898 * CHACHA20 unless there is hardware support for fast and constant-time
899 * AES_GCM. */
900 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -0400901 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400903 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
904 -1, 0, &head, &tail);
905 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
906 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 } else {
David Benjamin0344daf2015-04-08 02:08:01 -0400908 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
909 CIPHER_ADD, -1, 0, &head, &tail);
910 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400912 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
913 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
917 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -0400918 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
919 0, &head, &tail);
920 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
921 0, &head, &tail);
922 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
923 -1, 0, &head, &tail);
924 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
925 0, &head, &tail);
926 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -0400930 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
931 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -0400934 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
935 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -0400938 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
939 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 /* If the rule_string begins with DEFAULT, apply the default rule before
942 * using the (possibly available) additional rules. */
943 ok = 1;
944 rule_p = rule_str;
945 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400946 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
947 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 rule_p += 7;
949 if (*rule_p == ':') {
950 rule_p++;
951 }
952 }
Adam Langley858a88d2014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400955 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 if (!ok) {
959 goto err;
960 }
961
962 /* Allocate new "cipherstack" for the result, return with error
963 * if we cannot get one. */
964 cipherstack = sk_SSL_CIPHER_new_null();
965 if (cipherstack == NULL) {
966 goto err;
967 }
968
969 in_group_flags = OPENSSL_malloc(num_of_ciphers);
970 if (!in_group_flags) {
971 goto err;
972 }
973
974 /* The cipher selection for the list is done. The ciphers are added
975 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
976 for (curr = head; curr != NULL; curr = curr->next) {
977 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -0500978 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
979 goto err;
980 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 in_group_flags[num_in_group_flags++] = curr->in_group;
982 }
983 }
984 OPENSSL_free(co_list); /* Not needed any longer */
985 co_list = NULL;
986
987 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
988 if (tmp_cipher_list == NULL) {
989 goto err;
990 }
991 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
992 if (!pref_list) {
993 goto err;
994 }
995 pref_list->ciphers = cipherstack;
996 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
997 if (!pref_list->in_group_flags) {
998 goto err;
999 }
1000 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1001 OPENSSL_free(in_group_flags);
1002 in_group_flags = NULL;
1003 if (*cipher_list != NULL) {
1004 ssl_cipher_preference_list_free(*cipher_list);
1005 }
1006 *cipher_list = pref_list;
1007 pref_list = NULL;
1008
1009 if (cipher_list_by_id != NULL) {
1010 if (*cipher_list_by_id != NULL) {
1011 sk_SSL_CIPHER_free(*cipher_list_by_id);
1012 }
1013 *cipher_list_by_id = tmp_cipher_list;
1014 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001015 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001016
1017 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1018 } else {
1019 sk_SSL_CIPHER_free(tmp_cipher_list);
1020 tmp_cipher_list = NULL;
1021 }
1022
1023 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001024
1025err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (co_list) {
1027 OPENSSL_free(co_list);
1028 }
1029 if (in_group_flags) {
1030 OPENSSL_free(in_group_flags);
1031 }
1032 if (cipherstack) {
1033 sk_SSL_CIPHER_free(cipherstack);
1034 }
1035 if (tmp_cipher_list) {
1036 sk_SSL_CIPHER_free(tmp_cipher_list);
1037 }
1038 if (pref_list && pref_list->in_group_flags) {
1039 OPENSSL_free(pref_list->in_group_flags);
1040 }
1041 if (pref_list) {
1042 OPENSSL_free(pref_list);
1043 }
1044 return NULL;
1045}
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1048 int len) {
1049 const char *ver;
1050 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001051 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 alg_mkey = cipher->algorithm_mkey;
1055 alg_auth = cipher->algorithm_auth;
1056 alg_enc = cipher->algorithm_enc;
1057 alg_mac = cipher->algorithm_mac;
1058 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Adam Langleyfcf25832014-12-18 17:42:32 -08001060 if (alg_ssl & SSL_SSLV3) {
1061 ver = "SSLv3";
1062 } else if (alg_ssl & SSL_TLSV1_2) {
1063 ver = "TLSv1.2";
1064 } else {
1065 ver = "unknown";
1066 }
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 switch (alg_mkey) {
1069 case SSL_kRSA:
1070 kx = "RSA";
1071 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001072
David Benjamin7061e282015-03-19 11:10:48 -04001073 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 kx = "DH";
1075 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001076
David Benjamin7061e282015-03-19 11:10:48 -04001077 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 kx = "ECDH";
1079 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001080
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 case SSL_kPSK:
1082 kx = "PSK";
1083 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001084
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 default:
1086 kx = "unknown";
1087 }
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 switch (alg_auth) {
1090 case SSL_aRSA:
1091 au = "RSA";
1092 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001093
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 case SSL_aECDSA:
1095 au = "ECDSA";
1096 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 case SSL_aPSK:
1099 au = "PSK";
1100 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001101
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 default:
1103 au = "unknown";
1104 break;
1105 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 switch (alg_enc) {
1108 case SSL_3DES:
1109 enc = "3DES(168)";
1110 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 case SSL_RC4:
1113 enc = "RC4(128)";
1114 break;
1115
1116 case SSL_AES128:
1117 enc = "AES(128)";
1118 break;
1119
1120 case SSL_AES256:
1121 enc = "AES(256)";
1122 break;
1123
1124 case SSL_AES128GCM:
1125 enc = "AESGCM(128)";
1126 break;
1127
1128 case SSL_AES256GCM:
1129 enc = "AESGCM(256)";
1130 break;
1131
1132 case SSL_CHACHA20POLY1305:
1133 enc = "ChaCha20-Poly1305";
1134 break;
1135
1136 default:
1137 enc = "unknown";
1138 break;
1139 }
1140
1141 switch (alg_mac) {
1142 case SSL_MD5:
1143 mac = "MD5";
1144 break;
1145
1146 case SSL_SHA1:
1147 mac = "SHA1";
1148 break;
1149
1150 case SSL_SHA256:
1151 mac = "SHA256";
1152 break;
1153
1154 case SSL_SHA384:
1155 mac = "SHA384";
1156 break;
1157
1158 case SSL_AEAD:
1159 mac = "AEAD";
1160 break;
1161
1162 default:
1163 mac = "unknown";
1164 break;
1165 }
1166
1167 if (buf == NULL) {
1168 len = 128;
1169 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001170 if (buf == NULL) {
1171 return NULL;
1172 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 } else if (len < 128) {
1174 return "Buffer too small";
1175 }
1176
1177 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1178 return buf;
1179}
1180
1181int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1182 return (c->algorithm_enc & SSL_AES) != 0;
1183}
1184
1185int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1186 return (c->algorithm_mac & SSL_MD5) != 0;
1187}
1188
1189int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1190 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1191}
1192
1193int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1194 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1195}
1196
1197const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1198 int i;
1199
1200 if (c == NULL) {
1201 return "(NONE)";
1202 }
1203
1204 i = (int)(c->id >> 24L);
1205 if (i == 3) {
1206 return "TLSv1/SSLv3";
1207 } else if (i == 2) {
1208 return "SSLv2";
1209 } else {
1210 return "unknown";
1211 }
1212}
Adam Langley95c29f32014-06-20 12:00:00 -07001213
1214/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001215const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1216 if (c != NULL) {
1217 return c->name;
1218 }
1219
1220 return "(NONE)";
1221}
Adam Langley95c29f32014-06-20 12:00:00 -07001222
Adam Langleyf1df2b32014-08-11 17:37:53 -07001223const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1224 if (cipher == NULL) {
1225 return "";
1226 }
1227
1228 switch (cipher->algorithm_mkey) {
1229 case SSL_kRSA:
David Benjamin65226252015-02-05 16:49:47 -05001230 return "RSA";
Adam Langleyfcf25832014-12-18 17:42:32 -08001231
David Benjamin7061e282015-03-19 11:10:48 -04001232 case SSL_kDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001233 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001234 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001235 return "DHE_RSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001236 default:
David Benjamin65226252015-02-05 16:49:47 -05001237 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001238 return "UNKNOWN";
1239 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001240
David Benjamin7061e282015-03-19 11:10:48 -04001241 case SSL_kECDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001242 switch (cipher->algorithm_auth) {
1243 case SSL_aECDSA:
David Benjamin65226252015-02-05 16:49:47 -05001244 return "ECDHE_ECDSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001245 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001246 return "ECDHE_RSA";
1247 case SSL_aPSK:
1248 return "ECDHE_PSK";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001249 default:
David Benjamin65226252015-02-05 16:49:47 -05001250 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001251 return "UNKNOWN";
1252 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001253
David Benjamin65226252015-02-05 16:49:47 -05001254 case SSL_kPSK:
1255 assert(cipher->algorithm_auth == SSL_aPSK);
1256 return "PSK";
1257
Adam Langleyf1df2b32014-08-11 17:37:53 -07001258 default:
David Benjamin65226252015-02-05 16:49:47 -05001259 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001260 return "UNKNOWN";
1261 }
1262}
1263
David Benjamin65226252015-02-05 16:49:47 -05001264static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1265 switch (cipher->algorithm_enc) {
1266 case SSL_3DES:
1267 return "3DES_EDE_CBC";
1268 case SSL_RC4:
1269 return "RC4";
1270 case SSL_AES128:
1271 return "AES_128_CBC";
1272 case SSL_AES256:
1273 return "AES_256_CBC";
1274 case SSL_AES128GCM:
1275 return "AES_128_GCM";
1276 case SSL_AES256GCM:
1277 return "AES_256_GCM";
1278 case SSL_CHACHA20POLY1305:
1279 return "CHACHA20_POLY1305";
1280 break;
1281 default:
1282 assert(0);
1283 return "UNKNOWN";
1284 }
1285}
1286
1287static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
1288 if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) {
1289 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1290 * only ever MD5 or SHA-1. */
1291 switch (cipher->algorithm_mac) {
1292 case SSL_MD5:
1293 return "MD5";
1294 case SSL_SHA1:
1295 return "SHA";
1296 default:
1297 assert(0);
1298 return "UNKNOWN";
1299 }
1300 } else if (cipher->algorithm2 & TLS1_PRF_SHA256) {
1301 return "SHA256";
1302 } else if (cipher->algorithm2 & TLS1_PRF_SHA384) {
1303 return "SHA384";
1304 } else {
1305 assert(0);
1306 return "UNKNOWN";
1307 }
1308}
1309
1310char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1311 if (cipher == NULL) {
1312 return NULL;
1313 }
1314
1315 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1316 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1317 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1318
1319 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1320 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1321 strlen(prf_name) + 1;
1322 char *ret = OPENSSL_malloc(len);
1323 if (ret == NULL) {
1324 return NULL;
1325 }
1326 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1327 BUF_strlcat(ret, kx_name, len) >= len ||
1328 BUF_strlcat(ret, "_WITH_", len) >= len ||
1329 BUF_strlcat(ret, enc_name, len) >= len ||
1330 BUF_strlcat(ret, "_", len) >= len ||
1331 BUF_strlcat(ret, prf_name, len) >= len) {
1332 assert(0);
1333 OPENSSL_free(ret);
1334 return NULL;
1335 }
1336 assert(strlen(ret) + 1 == len);
1337 return ret;
1338}
1339
Adam Langley95c29f32014-06-20 12:00:00 -07001340/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001341int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1342 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001343
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 if (c != NULL) {
1345 if (alg_bits != NULL) {
1346 *alg_bits = c->alg_bits;
1347 }
1348 ret = c->strength_bits;
1349 }
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 return ret;
1352}
Adam Langley95c29f32014-06-20 12:00:00 -07001353
David Benjamin107db582015-04-08 00:41:59 -04001354uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356void *SSL_COMP_get_compression_methods(void) { return NULL; }
1357
1358int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1359
1360const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001361
1362/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001363int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
David Benjamin107db582015-04-08 00:41:59 -04001364 uint32_t alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001365
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 if (alg_a & SSL_aECDSA) {
1367 return SSL_PKEY_ECC;
1368 } else if (alg_a & SSL_aRSA) {
1369 return SSL_PKEY_RSA_ENC;
1370 }
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 return -1;
1373}
David Benjamin9c651c92014-07-12 13:27:45 -04001374
Adam Langleyfcf25832014-12-18 17:42:32 -08001375/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1376 * public key in the key exchange, sent in a server Certificate message.
1377 * Otherwise it returns 0. */
1378int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001379 /* PSK-authenticated ciphers do not use a public key, except for
1380 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 if ((cipher->algorithm_auth & SSL_aPSK) &&
1382 !(cipher->algorithm_mkey & SSL_kRSA)) {
1383 return 0;
1384 }
1385
1386 /* All other ciphers include it. */
1387 return 1;
1388}
1389
1390/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1391 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001392 *
1393 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1395 * communicate a psk_identity_hint, so it is optional. */
1396int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1397 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001398 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 return 1;
1400 }
1401
1402 /* It is optional in all others. */
1403 return 0;
1404}