blob: 7e0ade8de05079170cd14f2bc720be4ba43b6630 [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;
176 int dead;
177 int in_group;
178 struct cipher_order_st *next, *prev;
179} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjamin0344daf2015-04-08 02:08:01 -0400181typedef struct cipher_alias_st {
182 /* name is the name of the cipher alias. */
183 const char *name;
184
185 /* The following fields are bitmasks for the corresponding fields on
186 * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
187 * bit corresponding to the cipher's value is set to 1. If any bitmask is
188 * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
189 uint32_t algorithm_mkey;
190 uint32_t algorithm_auth;
191 uint32_t algorithm_enc;
192 uint32_t algorithm_mac;
193 uint32_t algorithm_ssl;
194 uint32_t algo_strength;
195} CIPHER_ALIAS;
196
197static const CIPHER_ALIAS kCipherAliases[] =
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 {
David Benjamin0344daf2015-04-08 02:08:01 -0400199 {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamin32fbdf22015-04-07 01:14:06 -0400201 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 /* key exchange aliases
204 * (some of those using only a single bit here combine
205 * multiple key exchange algs according to the RFCs,
206 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
David Benjamin0344daf2015-04-08 02:08:01 -0400207 {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin0344daf2015-04-08 02:08:01 -0400209 {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
210 {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
211 {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin0344daf2015-04-08 02:08:01 -0400213 {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
214 {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
215 {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjamin0344daf2015-04-08 02:08:01 -0400217 {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 /* server authentication aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400220 {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
221 {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
222 {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
223 {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 /* aliases combining key exchange and server authentication */
David Benjamin0344daf2015-04-08 02:08:01 -0400226 {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
227 {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
228 {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
229 {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
230 {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
231 {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 /* symmetric encryption aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400234 {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
235 {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
236 {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
237 {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
238 {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
239 {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
240 {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 /* MAC aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400243 {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
244 {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
245 {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
246 {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
247 {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* protocol version aliases */
David Benjamin0344daf2015-04-08 02:08:01 -0400250 {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
251 {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
252 {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 /* strength classes */
David Benjamin0344daf2015-04-08 02:08:01 -0400255 {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
256 {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* FIPS 140-2 approved ciphersuite */
David Benjamin0344daf2015-04-08 02:08:01 -0400258 {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
Adam Langleyfcf25832014-12-18 17:42:32 -0800259};
Adam Langley95c29f32014-06-20 12:00:00 -0700260
David Benjamin0344daf2015-04-08 02:08:01 -0400261#define NUM_CIPHER_ALIASES (sizeof(kCipherAliases) / sizeof(kCipherAliases[0]))
262
David Benjaminea72bd02014-12-21 21:27:41 -0500263int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
264 size_t *out_mac_secret_len,
265 size_t *out_fixed_iv_len,
266 const SSL_CIPHER *cipher, uint16_t version) {
267 *out_aead = NULL;
268 *out_mac_secret_len = 0;
269 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700270
David Benjaminea72bd02014-12-21 21:27:41 -0500271 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500273 *out_aead = EVP_aead_aes_128_gcm();
274 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 return 1;
276
277 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500278 *out_aead = EVP_aead_aes_256_gcm();
279 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 return 1;
281
282 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500283 *out_aead = EVP_aead_chacha20_poly1305();
284 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286
287 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500288 switch (cipher->algorithm_mac) {
289 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500290 if (version == SSL3_VERSION) {
291 *out_aead = EVP_aead_rc4_md5_ssl3();
292 } else {
293 *out_aead = EVP_aead_rc4_md5_tls();
294 }
David Benjaminea72bd02014-12-21 21:27:41 -0500295 *out_mac_secret_len = MD5_DIGEST_LENGTH;
296 return 1;
297 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500298 if (version == SSL3_VERSION) {
299 *out_aead = EVP_aead_rc4_sha1_ssl3();
300 } else {
301 *out_aead = EVP_aead_rc4_sha1_tls();
302 }
David Benjaminea72bd02014-12-21 21:27:41 -0500303 *out_mac_secret_len = SHA_DIGEST_LENGTH;
304 return 1;
305 default:
306 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800308
David Benjaminea72bd02014-12-21 21:27:41 -0500309 case SSL_AES128:
310 switch (cipher->algorithm_mac) {
311 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500312 if (version == SSL3_VERSION) {
313 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
314 *out_fixed_iv_len = 16;
315 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500316 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
317 *out_fixed_iv_len = 16;
318 } else {
319 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
320 }
321 *out_mac_secret_len = SHA_DIGEST_LENGTH;
322 return 1;
323 case SSL_SHA256:
324 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
325 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
326 return 1;
327 default:
328 return 0;
329 }
330
331 case SSL_AES256:
332 switch (cipher->algorithm_mac) {
333 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500334 if (version == SSL3_VERSION) {
335 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
336 *out_fixed_iv_len = 16;
337 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500338 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
339 *out_fixed_iv_len = 16;
340 } else {
341 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
342 }
343 *out_mac_secret_len = SHA_DIGEST_LENGTH;
344 return 1;
345 case SSL_SHA256:
346 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
347 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
348 return 1;
349 case SSL_SHA384:
350 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
351 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
352 return 1;
353 default:
354 return 0;
355 }
356
357 case SSL_3DES:
358 switch (cipher->algorithm_mac) {
359 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500360 if (version == SSL3_VERSION) {
361 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
362 *out_fixed_iv_len = 8;
363 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500364 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
365 *out_fixed_iv_len = 8;
366 } else {
367 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
368 }
369 *out_mac_secret_len = SHA_DIGEST_LENGTH;
370 return 1;
371 default:
372 return 0;
373 }
374
375 default:
376 return 0;
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700379
David Benjamin71f07942015-04-08 02:36:59 -0400380int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md,
381 size_t idx) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500382 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 0;
384 }
David Benjamin71f07942015-04-08 02:36:59 -0400385 *out_mask = ssl_handshake_digests[idx].mask;
386 *out_md = ssl_handshake_digests[idx].md_func();
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700388}
389
390#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700392
David Benjamin0344daf2015-04-08 02:08:01 -0400393/* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
394 * |buf_len| bytes at |buf|. */
395static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
396 /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
397 return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
398}
399
Adam Langley95c29f32014-06-20 12:00:00 -0700400static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 CIPHER_ORDER **tail) {
402 if (curr == *tail) {
403 return;
404 }
405 if (curr == *head) {
406 *head = curr->next;
407 }
408 if (curr->prev != NULL) {
409 curr->prev->next = curr->next;
410 }
411 if (curr->next != NULL) {
412 curr->next->prev = curr->prev;
413 }
414 (*tail)->next = curr;
415 curr->prev = *tail;
416 curr->next = NULL;
417 *tail = curr;
418}
Adam Langley95c29f32014-06-20 12:00:00 -0700419
420static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 CIPHER_ORDER **tail) {
422 if (curr == *head) {
423 return;
424 }
425 if (curr == *tail) {
426 *tail = curr->prev;
427 }
428 if (curr->next != NULL) {
429 curr->next->prev = curr->prev;
430 }
431 if (curr->prev != NULL) {
432 curr->prev->next = curr->next;
433 }
434 (*head)->prev = curr;
435 curr->next = *head;
436 curr->prev = NULL;
437 *head = curr;
438}
Adam Langley95c29f32014-06-20 12:00:00 -0700439
David Benjamin82c9e902014-12-12 15:55:27 -0500440static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin0344daf2015-04-08 02:08:01 -0400441 size_t num_of_ciphers,
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 CIPHER_ORDER *co_list,
443 CIPHER_ORDER **head_p,
444 CIPHER_ORDER **tail_p) {
David Benjamin0344daf2015-04-08 02:08:01 -0400445 size_t i, co_list_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 /* We have num_of_ciphers descriptions compiled in, depending on the method
448 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
449 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 /* Get the initial list of ciphers */
452 co_list_num = 0; /* actual count of ciphers */
453 for (i = 0; i < num_of_ciphers; i++) {
David Benjamin0344daf2015-04-08 02:08:01 -0400454 const SSL_CIPHER *c = ssl_method->get_cipher(i);
455 if (c != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 co_list[co_list_num].cipher = c;
457 co_list[co_list_num].next = NULL;
458 co_list[co_list_num].prev = NULL;
459 co_list[co_list_num].active = 0;
460 co_list[co_list_num].in_group = 0;
461 co_list_num++;
462 }
463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 /* Prepare linked list from list entries. */
466 if (co_list_num > 0) {
467 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 if (co_list_num > 1) {
470 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 for (i = 1; i < co_list_num - 1; i++) {
473 co_list[i].prev = &co_list[i - 1];
474 co_list[i].next = &co_list[i + 1];
475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
478 }
479
480 co_list[co_list_num - 1].next = NULL;
481
482 *head_p = &co_list[0];
483 *tail_p = &co_list[co_list_num - 1];
484 }
485}
Adam Langley95c29f32014-06-20 12:00:00 -0700486
David Benjamin0344daf2015-04-08 02:08:01 -0400487/* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
488 * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
489 * head and tail of the list to |*head_p| and |*tail_p|, respectively.
490 *
491 * - If |cipher_id| is non-zero, only that cipher is selected.
492 * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
493 * of that strength.
494 * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
495 * |algo_strength|. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800496static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400497 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
498 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
499 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
501 CIPHER_ORDER *head, *tail, *curr, *next, *last;
502 const SSL_CIPHER *cp;
503 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin0344daf2015-04-08 02:08:01 -0400505 if (cipher_id == 0 && strength_bits == -1 &&
506 (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
507 alg_ssl == 0 || algo_strength == 0)) {
508 /* The rule matches nothing, so bail early. */
509 return;
510 }
511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 if (rule == CIPHER_DEL) {
513 /* needed to maintain sorting between currently deleted ciphers */
514 reverse = 1;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 head = *head_p;
518 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 if (reverse) {
521 next = tail;
522 last = head;
523 } else {
524 next = head;
525 last = tail;
526 }
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 curr = NULL;
529 for (;;) {
530 if (curr == last) {
531 break;
532 }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 curr = next;
535 if (curr == NULL) {
536 break;
537 }
Adam Langleye3142a72014-07-24 17:56:48 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 next = reverse ? curr->prev : curr->next;
540 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700541
David Benjamin0344daf2015-04-08 02:08:01 -0400542 /* Selection criteria is either a specific cipher, the value of
543 * |strength_bits|, or the algorithms used. */
544 if (cipher_id != 0) {
545 if (cipher_id != cp->id) {
546 continue;
547 }
548 } else if (strength_bits >= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 if (strength_bits != cp->strength_bits) {
550 continue;
551 }
David Benjamin0344daf2015-04-08 02:08:01 -0400552 } else if (!(alg_mkey & cp->algorithm_mkey) ||
553 !(alg_auth & cp->algorithm_auth) ||
554 !(alg_enc & cp->algorithm_enc) ||
555 !(alg_mac & cp->algorithm_mac) ||
556 !(alg_ssl & cp->algorithm_ssl) ||
557 !(algo_strength & cp->algo_strength)) {
558 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 }
Adam Langleye3142a72014-07-24 17:56:48 -0700560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 /* add the cipher if it has not been added yet. */
562 if (rule == CIPHER_ADD) {
563 /* reverse == 0 */
564 if (!curr->active) {
565 ll_append_tail(&head, curr, &tail);
566 curr->active = 1;
567 curr->in_group = in_group;
568 }
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 /* Move the added cipher to this location */
572 else if (rule == CIPHER_ORD) {
573 /* reverse == 0 */
574 if (curr->active) {
575 ll_append_tail(&head, curr, &tail);
576 curr->in_group = 0;
577 }
578 } else if (rule == CIPHER_DEL) {
579 /* reverse == 1 */
580 if (curr->active) {
581 /* most recently deleted ciphersuites get best positions
582 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
583 * works in reverse to maintain the order) */
584 ll_append_head(&head, curr, &tail);
585 curr->active = 0;
586 curr->in_group = 0;
587 }
588 } else if (rule == CIPHER_KILL) {
589 /* reverse == 0 */
590 if (head == curr) {
591 head = curr->next;
592 } else {
593 curr->prev->next = curr->next;
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (tail == curr) {
597 tail = curr->prev;
598 }
599 curr->active = 0;
600 if (curr->next != NULL) {
601 curr->next->prev = curr->prev;
602 }
603 if (curr->prev != NULL) {
604 curr->prev->next = curr->next;
605 }
606 curr->next = NULL;
607 curr->prev = NULL;
608 }
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 *head_p = head;
612 *tail_p = tail;
613}
Adam Langley95c29f32014-06-20 12:00:00 -0700614
615static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 CIPHER_ORDER **tail_p) {
617 int max_strength_bits, i, *number_uses;
618 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 /* This routine sorts the ciphers with descending strength. The sorting must
621 * keep the pre-sorted sequence, so we apply the normal sorting routine as
622 * '+' movement to the end of the list. */
623 max_strength_bits = 0;
624 curr = *head_p;
625 while (curr != NULL) {
626 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
627 max_strength_bits = curr->cipher->strength_bits;
628 }
629 curr = curr->next;
630 }
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
633 if (!number_uses) {
634 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
635 return 0;
636 }
637 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 /* Now find the strength_bits values actually used. */
640 curr = *head_p;
641 while (curr != NULL) {
642 if (curr->active) {
643 number_uses[curr->cipher->strength_bits]++;
644 }
645 curr = curr->next;
646 }
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 /* Go through the list of used strength_bits values in descending order. */
649 for (i = max_strength_bits; i >= 0; i--) {
650 if (number_uses[i] > 0) {
651 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
652 tail_p);
653 }
654 }
655
656 OPENSSL_free(number_uses);
657 return 1;
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
David Benjamin0344daf2015-04-08 02:08:01 -0400660static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
661 const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 CIPHER_ORDER **head_p,
David Benjamin0344daf2015-04-08 02:08:01 -0400663 CIPHER_ORDER **tail_p) {
David Benjamin107db582015-04-08 00:41:59 -0400664 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 const char *l, *buf;
David Benjamin0344daf2015-04-08 02:08:01 -0400666 int multi, rule, retval, ok, in_group = 0, has_group = 0;
667 size_t j, buf_len;
668 uint32_t cipher_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 retval = 1;
672 l = rule_str;
673 for (;;) {
674 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (ch == '\0') {
677 break; /* done */
678 }
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 if (in_group) {
681 if (ch == ']') {
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 if (*tail_p) {
683 (*tail_p)->in_group = 0;
684 }
685 in_group = 0;
686 l++;
687 continue;
688 }
David Benjamin37d92462014-09-20 17:54:24 -0400689
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 if (ch == '|') {
691 rule = CIPHER_ADD;
692 l++;
693 continue;
694 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
695 !(ch >= '0' && ch <= '9')) {
696 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
697 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400698 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 break;
700 } else {
701 rule = CIPHER_ADD;
702 }
703 } else if (ch == '-') {
704 rule = CIPHER_DEL;
705 l++;
706 } else if (ch == '+') {
707 rule = CIPHER_ORD;
708 l++;
709 } else if (ch == '!') {
710 rule = CIPHER_KILL;
711 l++;
712 } else if (ch == '@') {
713 rule = CIPHER_SPECIAL;
714 l++;
715 } else if (ch == '[') {
716 if (in_group) {
717 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
David Benjamin0344daf2015-04-08 02:08:01 -0400718 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 break;
720 }
721 in_group = 1;
722 has_group = 1;
723 l++;
724 continue;
725 } else {
726 rule = CIPHER_ADD;
727 }
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729 /* If preference groups are enabled, the only legal operator is +.
730 * Otherwise the in_group bits will get mixed up. */
731 if (has_group && rule != CIPHER_ADD) {
732 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
733 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
David Benjamin0344daf2015-04-08 02:08:01 -0400734 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 break;
736 }
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langleyfcf25832014-12-18 17:42:32 -0800738 if (ITEM_SEP(ch)) {
739 l++;
740 continue;
741 }
Adam Langley95c29f32014-06-20 12:00:00 -0700742
David Benjamin0344daf2015-04-08 02:08:01 -0400743 multi = 0;
744 cipher_id = 0;
745 alg_mkey = ~0u;
746 alg_auth = ~0u;
747 alg_enc = ~0u;
748 alg_mac = ~0u;
749 alg_ssl = ~0u;
750 algo_strength = ~0u;
Adam Langley95c29f32014-06-20 12:00:00 -0700751
Adam Langleyfcf25832014-12-18 17:42:32 -0800752 for (;;) {
753 ch = *l;
754 buf = l;
David Benjamin0344daf2015-04-08 02:08:01 -0400755 buf_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
757 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
758 ch = *(++l);
David Benjamin0344daf2015-04-08 02:08:01 -0400759 buf_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
David Benjamin0344daf2015-04-08 02:08:01 -0400762 if (buf_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800763 /* We hit something we cannot deal with, it is no command or separator
764 * nor alphanumeric, so we call this an error. */
765 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
766 SSL_R_INVALID_COMMAND);
David Benjamin0344daf2015-04-08 02:08:01 -0400767 retval = in_group = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 l++;
769 break;
770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 if (rule == CIPHER_SPECIAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 break;
774 }
David Benjamin0344daf2015-04-08 02:08:01 -0400775
776 /* Look for a matching exact cipher. These aren't allowed in multipart
777 * rules. */
778 if (!multi && ch != '+') {
779 size_t num_ciphers = ssl_method->num_ciphers();
780 for (j = 0; j < num_ciphers; j++) {
781 const SSL_CIPHER *cipher = ssl_method->get_cipher(j);
782 if (cipher != NULL && rule_equals(cipher->name, buf, buf_len)) {
783 cipher_id = cipher->id;
784 break;
785 }
786 }
787 }
788 if (cipher_id == 0) {
789 /* If not an exact cipher, look for a matching cipher alias. */
790 for (j = 0; j < NUM_CIPHER_ALIASES; j++) {
791 if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
792 alg_mkey &= kCipherAliases[j].algorithm_mkey;
793 alg_auth &= kCipherAliases[j].algorithm_auth;
794 alg_enc &= kCipherAliases[j].algorithm_enc;
795 alg_mac &= kCipherAliases[j].algorithm_mac;
796 alg_ssl &= kCipherAliases[j].algorithm_ssl;
797 algo_strength &= kCipherAliases[j].algo_strength;
798 break;
799 }
800 }
801 if (j == NUM_CIPHER_ALIASES) {
802 alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
803 }
804 }
805
806 /* Check for a multipart rule. */
807 if (ch != '+') {
808 break;
809 }
810 l++;
811 multi = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800812 }
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 /* Ok, we have the rule, now apply it. */
815 if (rule == CIPHER_SPECIAL) {
816 /* special command */
817 ok = 0;
David Benjamin0344daf2015-04-08 02:08:01 -0400818 if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 ok = ssl_cipher_strength_sort(head_p, tail_p);
820 } else {
821 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
822 SSL_R_INVALID_COMMAND);
823 }
Adam Langley95c29f32014-06-20 12:00:00 -0700824
Adam Langleyfcf25832014-12-18 17:42:32 -0800825 if (ok == 0) {
826 retval = 0;
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 /* We do not support any "multi" options together with "@", so throw away
830 * the rest of the command, if any left, until end or ':' is found. */
831 while (*l != '\0' && !ITEM_SEP(*l)) {
832 l++;
833 }
David Benjamin0344daf2015-04-08 02:08:01 -0400834 } else {
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
836 alg_ssl, algo_strength, rule, -1, in_group, head_p,
837 tail_p);
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 }
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (in_group) {
842 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
843 retval = 0;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 return retval;
847}
Adam Langley95c29f32014-06-20 12:00:00 -0700848
Adam Langleyfcf25832014-12-18 17:42:32 -0800849STACK_OF(SSL_CIPHER) *
850ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
David Benjamin71f07942015-04-08 02:36:59 -0400851 struct ssl_cipher_preference_list_st **out_cipher_list,
852 STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
853 const char *rule_str) {
David Benjamin0344daf2015-04-08 02:08:01 -0400854 int ok;
855 size_t num_of_ciphers;
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
857 const char *rule_p;
858 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 uint8_t *in_group_flags = NULL;
860 unsigned int num_in_group_flags = 0;
861 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 /* Return with error if nothing to do. */
David Benjamin71f07942015-04-08 02:36:59 -0400864 if (rule_str == NULL || out_cipher_list == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 return NULL;
866 }
David Benjamin5213df42014-08-20 14:19:54 -0400867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 /* Now we have to collect the available ciphers from the compiled in ciphers.
869 * We cannot get more than the number compiled in, so it is used for
870 * allocation. */
871 num_of_ciphers = ssl_method->num_ciphers();
872 co_list =
873 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
874 if (co_list == NULL) {
875 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
876 return NULL;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 /* Now arrange all ciphers by preference:
882 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
885 * key exchange mechanisms */
David Benjamin0344daf2015-04-08 02:08:01 -0400886 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
887 CIPHER_ADD, -1, 0, &head, &tail);
888 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400890 ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
891 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
894 * CHACHA20 unless there is hardware support for fast and constant-time
895 * AES_GCM. */
896 if (EVP_has_aes_hardware()) {
David Benjamin0344daf2015-04-08 02:08:01 -0400897 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400899 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
900 -1, 0, &head, &tail);
901 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
902 CIPHER_ADD, -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 } else {
David Benjamin0344daf2015-04-08 02:08:01 -0400904 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
905 CIPHER_ADD, -1, 0, &head, &tail);
906 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 -1, 0, &head, &tail);
David Benjamin0344daf2015-04-08 02:08:01 -0400908 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
909 -1, 0, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
913 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjamin0344daf2015-04-08 02:08:01 -0400914 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
915 0, &head, &tail);
916 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
917 0, &head, &tail);
918 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
919 -1, 0, &head, &tail);
920 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
921 0, &head, &tail);
922 ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 /* Temporarily enable everything else for sorting */
David Benjamin0344daf2015-04-08 02:08:01 -0400926 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
927 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 /* Move ciphers without forward secrecy to the end. */
David Benjamin0344daf2015-04-08 02:08:01 -0400930 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
931 CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 /* Now disable everything (maintaining the ordering!) */
David Benjamin0344daf2015-04-08 02:08:01 -0400934 ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
935 &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 /* If the rule_string begins with DEFAULT, apply the default rule before
938 * using the (possibly available) additional rules. */
939 ok = 1;
940 rule_p = rule_str;
941 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400942 ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
943 &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 rule_p += 7;
945 if (*rule_p == ':') {
946 rule_p++;
947 }
948 }
Adam Langley858a88d2014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (ok && strlen(rule_p) > 0) {
David Benjamin0344daf2015-04-08 02:08:01 -0400951 ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 }
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 if (!ok) {
955 goto err;
956 }
957
958 /* Allocate new "cipherstack" for the result, return with error
959 * if we cannot get one. */
960 cipherstack = sk_SSL_CIPHER_new_null();
961 if (cipherstack == NULL) {
962 goto err;
963 }
964
965 in_group_flags = OPENSSL_malloc(num_of_ciphers);
966 if (!in_group_flags) {
967 goto err;
968 }
969
970 /* The cipher selection for the list is done. The ciphers are added
971 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
972 for (curr = head; curr != NULL; curr = curr->next) {
973 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -0500974 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
975 goto err;
976 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 in_group_flags[num_in_group_flags++] = curr->in_group;
978 }
979 }
980 OPENSSL_free(co_list); /* Not needed any longer */
981 co_list = NULL;
982
983 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
984 if (tmp_cipher_list == NULL) {
985 goto err;
986 }
987 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
988 if (!pref_list) {
989 goto err;
990 }
991 pref_list->ciphers = cipherstack;
992 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
993 if (!pref_list->in_group_flags) {
994 goto err;
995 }
996 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
997 OPENSSL_free(in_group_flags);
998 in_group_flags = NULL;
David Benjamin71f07942015-04-08 02:36:59 -0400999 if (*out_cipher_list != NULL) {
1000 ssl_cipher_preference_list_free(*out_cipher_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 }
David Benjamin71f07942015-04-08 02:36:59 -04001002 *out_cipher_list = pref_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 pref_list = NULL;
1004
David Benjamin71f07942015-04-08 02:36:59 -04001005 if (out_cipher_list_by_id != NULL) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001006 sk_SSL_CIPHER_free(*out_cipher_list_by_id);
David Benjamin71f07942015-04-08 02:36:59 -04001007 *out_cipher_list_by_id = tmp_cipher_list;
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 tmp_cipher_list = NULL;
David Benjamin71f07942015-04-08 02:36:59 -04001009 (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
1010 ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001011
David Benjamin71f07942015-04-08 02:36:59 -04001012 sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 } else {
1014 sk_SSL_CIPHER_free(tmp_cipher_list);
1015 tmp_cipher_list = NULL;
1016 }
1017
1018 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001019
1020err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001021 OPENSSL_free(co_list);
1022 OPENSSL_free(in_group_flags);
1023 sk_SSL_CIPHER_free(cipherstack);
1024 sk_SSL_CIPHER_free(tmp_cipher_list);
1025 if (pref_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 OPENSSL_free(pref_list->in_group_flags);
1027 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001028 OPENSSL_free(pref_list);
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 return NULL;
1030}
Adam Langley95c29f32014-06-20 12:00:00 -07001031
David Benjamin71f07942015-04-08 02:36:59 -04001032uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
1033
1034int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
1035 return (cipher->algorithm_enc & SSL_AES) != 0;
1036}
1037
1038int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
1039 return (cipher->algorithm_mac & SSL_MD5) != 0;
1040}
1041
1042int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
1043 return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1044}
1045
1046int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
1047 return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1048}
1049
1050/* return the actual cipher being used */
1051const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
1052 if (cipher != NULL) {
1053 return cipher->name;
1054 }
1055
1056 return "(NONE)";
1057}
1058
1059const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1060 if (cipher == NULL) {
1061 return "";
1062 }
1063
1064 switch (cipher->algorithm_mkey) {
1065 case SSL_kRSA:
1066 return "RSA";
1067
1068 case SSL_kDHE:
1069 switch (cipher->algorithm_auth) {
1070 case SSL_aRSA:
1071 return "DHE_RSA";
1072 default:
1073 assert(0);
1074 return "UNKNOWN";
1075 }
1076
1077 case SSL_kECDHE:
1078 switch (cipher->algorithm_auth) {
1079 case SSL_aECDSA:
1080 return "ECDHE_ECDSA";
1081 case SSL_aRSA:
1082 return "ECDHE_RSA";
1083 case SSL_aPSK:
1084 return "ECDHE_PSK";
1085 default:
1086 assert(0);
1087 return "UNKNOWN";
1088 }
1089
1090 case SSL_kPSK:
1091 assert(cipher->algorithm_auth == SSL_aPSK);
1092 return "PSK";
1093
1094 default:
1095 assert(0);
1096 return "UNKNOWN";
1097 }
1098}
1099
1100static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1101 switch (cipher->algorithm_enc) {
1102 case SSL_3DES:
1103 return "3DES_EDE_CBC";
1104 case SSL_RC4:
1105 return "RC4";
1106 case SSL_AES128:
1107 return "AES_128_CBC";
1108 case SSL_AES256:
1109 return "AES_256_CBC";
1110 case SSL_AES128GCM:
1111 return "AES_128_GCM";
1112 case SSL_AES256GCM:
1113 return "AES_256_GCM";
1114 case SSL_CHACHA20POLY1305:
1115 return "CHACHA20_POLY1305";
1116 break;
1117 default:
1118 assert(0);
1119 return "UNKNOWN";
1120 }
1121}
1122
1123static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
1124 if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) {
1125 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1126 * only ever MD5 or SHA-1. */
1127 switch (cipher->algorithm_mac) {
1128 case SSL_MD5:
1129 return "MD5";
1130 case SSL_SHA1:
1131 return "SHA";
1132 default:
1133 assert(0);
1134 return "UNKNOWN";
1135 }
1136 } else if (cipher->algorithm2 & TLS1_PRF_SHA256) {
1137 return "SHA256";
1138 } else if (cipher->algorithm2 & TLS1_PRF_SHA384) {
1139 return "SHA384";
1140 } else {
1141 assert(0);
1142 return "UNKNOWN";
1143 }
1144}
1145
1146char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1147 if (cipher == NULL) {
1148 return NULL;
1149 }
1150
1151 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1152 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1153 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1154
1155 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1156 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1157 strlen(prf_name) + 1;
1158 char *ret = OPENSSL_malloc(len);
1159 if (ret == NULL) {
1160 return NULL;
1161 }
1162 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1163 BUF_strlcat(ret, kx_name, len) >= len ||
1164 BUF_strlcat(ret, "_WITH_", len) >= len ||
1165 BUF_strlcat(ret, enc_name, len) >= len ||
1166 BUF_strlcat(ret, "_", len) >= len ||
1167 BUF_strlcat(ret, prf_name, len) >= len) {
1168 assert(0);
1169 OPENSSL_free(ret);
1170 return NULL;
1171 }
1172 assert(strlen(ret) + 1 == len);
1173 return ret;
1174}
1175
1176int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
1177 if (cipher == NULL) {
1178 return 0;
1179 }
1180
1181 if (out_alg_bits != NULL) {
1182 *out_alg_bits = cipher->alg_bits;
1183 }
1184 return cipher->strength_bits;
1185}
1186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1188 int len) {
1189 const char *ver;
1190 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001191 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001193
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 alg_mkey = cipher->algorithm_mkey;
1195 alg_auth = cipher->algorithm_auth;
1196 alg_enc = cipher->algorithm_enc;
1197 alg_mac = cipher->algorithm_mac;
1198 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001199
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 if (alg_ssl & SSL_SSLV3) {
1201 ver = "SSLv3";
1202 } else if (alg_ssl & SSL_TLSV1_2) {
1203 ver = "TLSv1.2";
1204 } else {
1205 ver = "unknown";
1206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 switch (alg_mkey) {
1209 case SSL_kRSA:
1210 kx = "RSA";
1211 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001212
David Benjamin7061e282015-03-19 11:10:48 -04001213 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 kx = "DH";
1215 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001216
David Benjamin7061e282015-03-19 11:10:48 -04001217 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 kx = "ECDH";
1219 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001220
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 case SSL_kPSK:
1222 kx = "PSK";
1223 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001224
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 default:
1226 kx = "unknown";
1227 }
Adam Langley95c29f32014-06-20 12:00:00 -07001228
Adam Langleyfcf25832014-12-18 17:42:32 -08001229 switch (alg_auth) {
1230 case SSL_aRSA:
1231 au = "RSA";
1232 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001233
Adam Langleyfcf25832014-12-18 17:42:32 -08001234 case SSL_aECDSA:
1235 au = "ECDSA";
1236 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001237
Adam Langleyfcf25832014-12-18 17:42:32 -08001238 case SSL_aPSK:
1239 au = "PSK";
1240 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001241
Adam Langleyfcf25832014-12-18 17:42:32 -08001242 default:
1243 au = "unknown";
1244 break;
1245 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001246
Adam Langleyfcf25832014-12-18 17:42:32 -08001247 switch (alg_enc) {
1248 case SSL_3DES:
1249 enc = "3DES(168)";
1250 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001251
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 case SSL_RC4:
1253 enc = "RC4(128)";
1254 break;
1255
1256 case SSL_AES128:
1257 enc = "AES(128)";
1258 break;
1259
1260 case SSL_AES256:
1261 enc = "AES(256)";
1262 break;
1263
1264 case SSL_AES128GCM:
1265 enc = "AESGCM(128)";
1266 break;
1267
1268 case SSL_AES256GCM:
1269 enc = "AESGCM(256)";
1270 break;
1271
1272 case SSL_CHACHA20POLY1305:
1273 enc = "ChaCha20-Poly1305";
1274 break;
1275
1276 default:
1277 enc = "unknown";
1278 break;
1279 }
1280
1281 switch (alg_mac) {
1282 case SSL_MD5:
1283 mac = "MD5";
1284 break;
1285
1286 case SSL_SHA1:
1287 mac = "SHA1";
1288 break;
1289
1290 case SSL_SHA256:
1291 mac = "SHA256";
1292 break;
1293
1294 case SSL_SHA384:
1295 mac = "SHA384";
1296 break;
1297
1298 case SSL_AEAD:
1299 mac = "AEAD";
1300 break;
1301
1302 default:
1303 mac = "unknown";
1304 break;
1305 }
1306
1307 if (buf == NULL) {
1308 len = 128;
1309 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001310 if (buf == NULL) {
1311 return NULL;
1312 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 } else if (len < 128) {
1314 return "Buffer too small";
1315 }
1316
1317 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1318 return buf;
1319}
1320
David Benjamin71f07942015-04-08 02:36:59 -04001321const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
1322 return "TLSv1/SSLv3";
Adam Langleyfcf25832014-12-18 17:42:32 -08001323}
1324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325void *SSL_COMP_get_compression_methods(void) { return NULL; }
1326
1327int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1328
1329const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001330
David Benjamin71f07942015-04-08 02:36:59 -04001331int ssl_cipher_get_cert_index(const SSL_CIPHER *cipher) {
1332 uint32_t alg_a = cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 if (alg_a & SSL_aECDSA) {
1335 return SSL_PKEY_ECC;
1336 } else if (alg_a & SSL_aRSA) {
1337 return SSL_PKEY_RSA_ENC;
1338 }
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 return -1;
1341}
David Benjamin9c651c92014-07-12 13:27:45 -04001342
Adam Langleyfcf25832014-12-18 17:42:32 -08001343int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001344 /* PSK-authenticated ciphers do not use a public key, except for
1345 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 if ((cipher->algorithm_auth & SSL_aPSK) &&
1347 !(cipher->algorithm_mkey & SSL_kRSA)) {
1348 return 0;
1349 }
1350
1351 /* All other ciphers include it. */
1352 return 1;
1353}
1354
Adam Langleyfcf25832014-12-18 17:42:32 -08001355int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1356 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001357 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 return 1;
1359 }
1360
1361 /* It is optional in all others. */
1362 return 0;
1363}