blob: 5ab43e79eb8cb44c21fda1782737c23884a5a9db [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
141#include <stdio.h>
142#include <assert.h>
143
Adam Langley95c29f32014-06-20 12:00:00 -0700144#include <openssl/engine.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500145#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700146#include <openssl/mem.h>
147#include <openssl/obj.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500148#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149
150#include "ssl_locl.h"
151
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153struct handshake_digest {
154 long mask;
155 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400156};
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
159 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
160 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
161 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
162 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
163};
Adam Langley95c29f32014-06-20 12:00:00 -0700164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165#define CIPHER_ADD 1
166#define CIPHER_KILL 2
167#define CIPHER_DEL 3
168#define CIPHER_ORD 4
169#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langleyfcf25832014-12-18 17:42:32 -0800171typedef struct cipher_order_st {
172 const SSL_CIPHER *cipher;
173 int active;
174 int dead;
175 int in_group;
176 struct cipher_order_st *next, *prev;
177} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700178
Adam Langleyfcf25832014-12-18 17:42:32 -0800179static const SSL_CIPHER cipher_aliases[] =
180 {
181 {0, SSL_TXT_ALL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in
184 ALL!) */
David Benjamin7061e282015-03-19 11:10:48 -0400185 {0, SSL_TXT_CMPDEF, 0, SSL_kDHE | SSL_kECDHE, SSL_aNULL, 0, 0, 0, 0, 0, 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langleyfcf25832014-12-18 17:42:32 -0800188 /* key exchange aliases
189 * (some of those using only a single bit here combine
190 * multiple key exchange algs according to the RFCs,
191 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
192 {0, SSL_TXT_kRSA, 0, SSL_kRSA, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin7061e282015-03-19 11:10:48 -0400194 {0, SSL_TXT_kDHE, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
195 {0, SSL_TXT_kEDH, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
196 {0, SSL_TXT_DH, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamin7061e282015-03-19 11:10:48 -0400198 {0, SSL_TXT_kECDHE, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
199 {0, SSL_TXT_kEECDH, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
200 {0, SSL_TXT_ECDH, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 {0, SSL_TXT_kPSK, 0, SSL_kPSK, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 /* server authentication aliases */
205 {0, SSL_TXT_aRSA, 0, 0, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
206 {0, SSL_TXT_aNULL, 0, 0, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
207 {0, SSL_TXT_aECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
208 {0, SSL_TXT_ECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
209 {0, SSL_TXT_aPSK, 0, 0, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 /* aliases combining key exchange and server authentication */
David Benjamin7061e282015-03-19 11:10:48 -0400212 {0, SSL_TXT_DHE, 0, SSL_kDHE, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
213 {0, SSL_TXT_EDH, 0, SSL_kDHE, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
214 {0, SSL_TXT_ECDHE, 0, SSL_kECDHE, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
215 {0, SSL_TXT_EECDH, 0, SSL_kECDHE, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 {0, SSL_TXT_RSA, 0, SSL_kRSA, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
David Benjamin7061e282015-03-19 11:10:48 -0400217 {0, SSL_TXT_ADH, 0, SSL_kDHE, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
218 {0, SSL_TXT_AECDH, 0, SSL_kECDHE, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 {0, SSL_TXT_PSK, 0, SSL_kPSK, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 /* symmetric encryption aliases */
222 {0, SSL_TXT_3DES, 0, 0, 0, SSL_3DES, 0, 0, 0, 0, 0, 0},
223 {0, SSL_TXT_RC4, 0, 0, 0, SSL_RC4, 0, 0, 0, 0, 0, 0},
224 {0, SSL_TXT_AES128, 0, 0, 0, SSL_AES128 | SSL_AES128GCM, 0, 0, 0, 0, 0, 0},
225 {0, SSL_TXT_AES256, 0, 0, 0, SSL_AES256 | SSL_AES256GCM, 0, 0, 0, 0, 0, 0},
226 {0, SSL_TXT_AES, 0, 0, 0, SSL_AES, 0, 0, 0, 0, 0, 0},
227 {0, SSL_TXT_AES_GCM, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM, 0, 0, 0, 0, 0,
228 0},
229 {0, SSL_TXT_CHACHA20, 0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* MAC aliases */
232 {0, SSL_TXT_MD5, 0, 0, 0, 0, SSL_MD5, 0, 0, 0, 0, 0},
233 {0, SSL_TXT_SHA1, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
234 {0, SSL_TXT_SHA, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
235 {0, SSL_TXT_SHA256, 0, 0, 0, 0, SSL_SHA256, 0, 0, 0, 0, 0},
236 {0, SSL_TXT_SHA384, 0, 0, 0, 0, SSL_SHA384, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 /* protocol version aliases */
239 {0, SSL_TXT_SSLV3, 0, 0, 0, 0, 0, SSL_SSLV3, 0, 0, 0, 0},
240 {0, SSL_TXT_TLSV1, 0, 0, 0, 0, 0, SSL_TLSV1, 0, 0, 0, 0},
241 {0, SSL_TXT_TLSV1_2, 0, 0, 0, 0, 0, SSL_TLSV1_2, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 /* strength classes */
244 {0, SSL_TXT_MEDIUM, 0, 0, 0, 0, 0, 0, SSL_MEDIUM, 0, 0, 0},
245 {0, SSL_TXT_HIGH, 0, 0, 0, 0, 0, 0, SSL_HIGH, 0, 0, 0},
246 /* FIPS 140-2 approved ciphersuite */
247 {0, SSL_TXT_FIPS, 0, 0, 0, 0, 0, 0, SSL_FIPS, 0, 0, 0},
248};
Adam Langley95c29f32014-06-20 12:00:00 -0700249
David Benjaminea72bd02014-12-21 21:27:41 -0500250int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
251 size_t *out_mac_secret_len,
252 size_t *out_fixed_iv_len,
253 const SSL_CIPHER *cipher, uint16_t version) {
254 *out_aead = NULL;
255 *out_mac_secret_len = 0;
256 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700257
David Benjaminea72bd02014-12-21 21:27:41 -0500258 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500260 *out_aead = EVP_aead_aes_128_gcm();
261 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 return 1;
263
264 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500265 *out_aead = EVP_aead_aes_256_gcm();
266 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268
269 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500270 *out_aead = EVP_aead_chacha20_poly1305();
271 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 return 1;
273
274 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500275 switch (cipher->algorithm_mac) {
276 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500277 if (version == SSL3_VERSION) {
278 *out_aead = EVP_aead_rc4_md5_ssl3();
279 } else {
280 *out_aead = EVP_aead_rc4_md5_tls();
281 }
David Benjaminea72bd02014-12-21 21:27:41 -0500282 *out_mac_secret_len = MD5_DIGEST_LENGTH;
283 return 1;
284 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500285 if (version == SSL3_VERSION) {
286 *out_aead = EVP_aead_rc4_sha1_ssl3();
287 } else {
288 *out_aead = EVP_aead_rc4_sha1_tls();
289 }
David Benjaminea72bd02014-12-21 21:27:41 -0500290 *out_mac_secret_len = SHA_DIGEST_LENGTH;
291 return 1;
292 default:
293 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800295
David Benjaminea72bd02014-12-21 21:27:41 -0500296 case SSL_AES128:
297 switch (cipher->algorithm_mac) {
298 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500299 if (version == SSL3_VERSION) {
300 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
301 *out_fixed_iv_len = 16;
302 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500303 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
304 *out_fixed_iv_len = 16;
305 } else {
306 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
307 }
308 *out_mac_secret_len = SHA_DIGEST_LENGTH;
309 return 1;
310 case SSL_SHA256:
311 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
312 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
313 return 1;
314 default:
315 return 0;
316 }
317
318 case SSL_AES256:
319 switch (cipher->algorithm_mac) {
320 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500321 if (version == SSL3_VERSION) {
322 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
323 *out_fixed_iv_len = 16;
324 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500325 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
326 *out_fixed_iv_len = 16;
327 } else {
328 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
329 }
330 *out_mac_secret_len = SHA_DIGEST_LENGTH;
331 return 1;
332 case SSL_SHA256:
333 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
334 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
335 return 1;
336 case SSL_SHA384:
337 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
338 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
339 return 1;
340 default:
341 return 0;
342 }
343
344 case SSL_3DES:
345 switch (cipher->algorithm_mac) {
346 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500347 if (version == SSL3_VERSION) {
348 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
349 *out_fixed_iv_len = 8;
350 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500351 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
352 *out_fixed_iv_len = 8;
353 } else {
354 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
355 }
356 *out_mac_secret_len = SHA_DIGEST_LENGTH;
357 return 1;
358 default:
359 return 0;
360 }
361
362 default:
363 return 0;
364 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700366
David Benjamin1f5e1152014-12-23 09:23:32 -0500367int ssl_get_handshake_digest(size_t idx, long *mask, const EVP_MD **md) {
368 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371 *mask = ssl_handshake_digests[idx].mask;
372 *md = ssl_handshake_digests[idx].md_func();
373 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700374}
375
376#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700378
379static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 CIPHER_ORDER **tail) {
381 if (curr == *tail) {
382 return;
383 }
384 if (curr == *head) {
385 *head = curr->next;
386 }
387 if (curr->prev != NULL) {
388 curr->prev->next = curr->next;
389 }
390 if (curr->next != NULL) {
391 curr->next->prev = curr->prev;
392 }
393 (*tail)->next = curr;
394 curr->prev = *tail;
395 curr->next = NULL;
396 *tail = curr;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
399static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 CIPHER_ORDER **tail) {
401 if (curr == *head) {
402 return;
403 }
404 if (curr == *tail) {
405 *tail = curr->prev;
406 }
407 if (curr->next != NULL) {
408 curr->next->prev = curr->prev;
409 }
410 if (curr->prev != NULL) {
411 curr->prev->next = curr->next;
412 }
413 (*head)->prev = curr;
414 curr->next = *head;
415 curr->prev = NULL;
416 *head = curr;
417}
Adam Langley95c29f32014-06-20 12:00:00 -0700418
David Benjamin82c9e902014-12-12 15:55:27 -0500419static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 int num_of_ciphers,
421 CIPHER_ORDER *co_list,
422 CIPHER_ORDER **head_p,
423 CIPHER_ORDER **tail_p) {
424 int i, co_list_num;
425 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 /* We have num_of_ciphers descriptions compiled in, depending on the method
428 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
429 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 /* Get the initial list of ciphers */
432 co_list_num = 0; /* actual count of ciphers */
433 for (i = 0; i < num_of_ciphers; i++) {
434 c = ssl_method->get_cipher(i);
435 /* drop those that use any of that is not available */
436 if (c != NULL && c->valid) {
437 co_list[co_list_num].cipher = c;
438 co_list[co_list_num].next = NULL;
439 co_list[co_list_num].prev = NULL;
440 co_list[co_list_num].active = 0;
441 co_list[co_list_num].in_group = 0;
442 co_list_num++;
443 }
444 }
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 /* Prepare linked list from list entries. */
447 if (co_list_num > 0) {
448 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 if (co_list_num > 1) {
451 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 for (i = 1; i < co_list_num - 1; i++) {
454 co_list[i].prev = &co_list[i - 1];
455 co_list[i].next = &co_list[i + 1];
456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
459 }
460
461 co_list[co_list_num - 1].next = NULL;
462
463 *head_p = &co_list[0];
464 *tail_p = &co_list[co_list_num - 1];
465 }
466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
468static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 int num_of_group_aliases,
470 CIPHER_ORDER *head) {
471 CIPHER_ORDER *ciph_curr;
472 const SSL_CIPHER **ca_curr;
473 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 /* First, add the real ciphers as already collected. */
476 ciph_curr = head;
477 ca_curr = ca_list;
478 while (ciph_curr != NULL) {
479 *ca_curr = ciph_curr->cipher;
480 ca_curr++;
481 ciph_curr = ciph_curr->next;
482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 /* Now we add the available ones from the cipher_aliases[] table. They
485 * represent either one or more algorithms, some of which in any affected
486 * category must be supported (set in enabled_mask), or represent a cipher
487 * strength value (will be added in any case because algorithms=0). */
488 for (i = 0; i < num_of_group_aliases; i++) {
489 *ca_curr = cipher_aliases + i;
490 ca_curr++;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 *ca_curr = NULL; /* end of list */
494}
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496static void ssl_cipher_apply_rule(
497 unsigned long cipher_id, unsigned long alg_mkey, unsigned long alg_auth,
498 unsigned long alg_enc, unsigned long alg_mac, unsigned long alg_ssl,
499 unsigned long algo_strength, int rule, int strength_bits, int in_group,
500 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 if (rule == CIPHER_DEL) {
506 /* needed to maintain sorting between currently deleted ciphers */
507 reverse = 1;
508 }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 head = *head_p;
511 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 if (reverse) {
514 next = tail;
515 last = head;
516 } else {
517 next = head;
518 last = tail;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 curr = NULL;
522 for (;;) {
523 if (curr == last) {
524 break;
525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 curr = next;
528 if (curr == NULL) {
529 break;
530 }
Adam Langleye3142a72014-07-24 17:56:48 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 next = reverse ? curr->prev : curr->next;
533 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* Selection criteria is either the value of strength_bits
536 * or the algorithms used. */
537 if (strength_bits >= 0) {
538 if (strength_bits != cp->strength_bits) {
539 continue;
540 }
541 } else {
542 if ((alg_mkey && !(alg_mkey & cp->algorithm_mkey)) ||
543 (alg_auth && !(alg_auth & cp->algorithm_auth)) ||
544 (alg_enc && !(alg_enc & cp->algorithm_enc)) ||
545 (alg_mac && !(alg_mac & cp->algorithm_mac)) ||
546 (alg_ssl && !(alg_ssl & cp->algorithm_ssl)) ||
547 (algo_strength && !(algo_strength & cp->algo_strength))) {
548 continue;
549 }
550 }
Adam Langleye3142a72014-07-24 17:56:48 -0700551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 /* add the cipher if it has not been added yet. */
553 if (rule == CIPHER_ADD) {
554 /* reverse == 0 */
555 if (!curr->active) {
556 ll_append_tail(&head, curr, &tail);
557 curr->active = 1;
558 curr->in_group = in_group;
559 }
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 /* Move the added cipher to this location */
563 else if (rule == CIPHER_ORD) {
564 /* reverse == 0 */
565 if (curr->active) {
566 ll_append_tail(&head, curr, &tail);
567 curr->in_group = 0;
568 }
569 } else if (rule == CIPHER_DEL) {
570 /* reverse == 1 */
571 if (curr->active) {
572 /* most recently deleted ciphersuites get best positions
573 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
574 * works in reverse to maintain the order) */
575 ll_append_head(&head, curr, &tail);
576 curr->active = 0;
577 curr->in_group = 0;
578 }
579 } else if (rule == CIPHER_KILL) {
580 /* reverse == 0 */
581 if (head == curr) {
582 head = curr->next;
583 } else {
584 curr->prev->next = curr->next;
585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 if (tail == curr) {
588 tail = curr->prev;
589 }
590 curr->active = 0;
591 if (curr->next != NULL) {
592 curr->next->prev = curr->prev;
593 }
594 if (curr->prev != NULL) {
595 curr->prev->next = curr->next;
596 }
597 curr->next = NULL;
598 curr->prev = NULL;
599 }
600 }
Adam Langley95c29f32014-06-20 12:00:00 -0700601
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 *head_p = head;
603 *tail_p = tail;
604}
Adam Langley95c29f32014-06-20 12:00:00 -0700605
606static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 CIPHER_ORDER **tail_p) {
608 int max_strength_bits, i, *number_uses;
609 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 /* This routine sorts the ciphers with descending strength. The sorting must
612 * keep the pre-sorted sequence, so we apply the normal sorting routine as
613 * '+' movement to the end of the list. */
614 max_strength_bits = 0;
615 curr = *head_p;
616 while (curr != NULL) {
617 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
618 max_strength_bits = curr->cipher->strength_bits;
619 }
620 curr = curr->next;
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
624 if (!number_uses) {
625 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
626 return 0;
627 }
628 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 /* Now find the strength_bits values actually used. */
631 curr = *head_p;
632 while (curr != NULL) {
633 if (curr->active) {
634 number_uses[curr->cipher->strength_bits]++;
635 }
636 curr = curr->next;
637 }
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 /* Go through the list of used strength_bits values in descending order. */
640 for (i = max_strength_bits; i >= 0; i--) {
641 if (number_uses[i] > 0) {
642 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
643 tail_p);
644 }
645 }
646
647 OPENSSL_free(number_uses);
648 return 1;
649}
Adam Langley95c29f32014-06-20 12:00:00 -0700650
651static int ssl_cipher_process_rulestr(const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 CIPHER_ORDER **head_p,
653 CIPHER_ORDER **tail_p,
654 const SSL_CIPHER **ca_list) {
655 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
656 const char *l, *buf;
657 int j, multi, found, rule, retval, ok, buflen, in_group = 0, has_group = 0;
658 unsigned long cipher_id = 0;
659 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700660
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 retval = 1;
662 l = rule_str;
663 for (;;) {
664 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 if (ch == '\0') {
667 break; /* done */
668 }
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 if (in_group) {
671 if (ch == ']') {
672 if (!in_group) {
673 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
674 SSL_R_UNEXPECTED_GROUP_CLOSE);
675 retval = found = in_group = 0;
676 break;
677 }
678 if (*tail_p) {
679 (*tail_p)->in_group = 0;
680 }
681 in_group = 0;
682 l++;
683 continue;
684 }
David Benjamin37d92462014-09-20 17:54:24 -0400685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686 if (ch == '|') {
687 rule = CIPHER_ADD;
688 l++;
689 continue;
690 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
691 !(ch >= '0' && ch <= '9')) {
692 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
693 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
694 retval = found = in_group = 0;
695 break;
696 } else {
697 rule = CIPHER_ADD;
698 }
699 } else if (ch == '-') {
700 rule = CIPHER_DEL;
701 l++;
702 } else if (ch == '+') {
703 rule = CIPHER_ORD;
704 l++;
705 } else if (ch == '!') {
706 rule = CIPHER_KILL;
707 l++;
708 } else if (ch == '@') {
709 rule = CIPHER_SPECIAL;
710 l++;
711 } else if (ch == '[') {
712 if (in_group) {
713 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
714 retval = found = in_group = 0;
715 break;
716 }
717 in_group = 1;
718 has_group = 1;
719 l++;
720 continue;
721 } else {
722 rule = CIPHER_ADD;
723 }
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 /* If preference groups are enabled, the only legal operator is +.
726 * Otherwise the in_group bits will get mixed up. */
727 if (has_group && rule != CIPHER_ADD) {
728 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
729 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
730 retval = found = in_group = 0;
731 break;
732 }
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 if (ITEM_SEP(ch)) {
735 l++;
736 continue;
737 }
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 alg_mkey = 0;
740 alg_auth = 0;
741 alg_enc = 0;
742 alg_mac = 0;
743 alg_ssl = 0;
744 algo_strength = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 for (;;) {
747 ch = *l;
748 buf = l;
749 buflen = 0;
750 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
751 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
752 ch = *(++l);
753 buflen++;
754 }
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 if (buflen == 0) {
757 /* We hit something we cannot deal with, it is no command or separator
758 * nor alphanumeric, so we call this an error. */
759 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
760 SSL_R_INVALID_COMMAND);
761 retval = found = in_group = 0;
762 l++;
763 break;
764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 if (rule == CIPHER_SPECIAL) {
767 found = 0; /* unused -- avoid compiler warning */
768 break; /* special treatment */
769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 /* check for multi-part specification */
772 if (ch == '+') {
773 multi = 1;
774 l++;
775 } else {
776 multi = 0;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 /* Now search for the cipher alias in the ca_list. Be careful with the
780 * strncmp, because the "buflen" limitation will make the rule "ADH:SOME"
781 * and the cipher "ADH-MY-CIPHER" look like a match for buflen=3. So
782 * additionally check whether the cipher name found has the correct
783 * length. We can save a strlen() call: just checking for the '\0' at the
784 * right place is sufficient, we have to strncmp() anyway. (We cannot use
785 * strcmp(), because buf is not '\0' terminated.) */
786 j = found = 0;
787 cipher_id = 0;
788 while (ca_list[j]) {
789 if (!strncmp(buf, ca_list[j]->name, buflen) &&
790 (ca_list[j]->name[buflen] == '\0')) {
791 found = 1;
792 break;
793 } else {
794 j++;
795 }
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 if (!found) {
799 break; /* ignore this entry */
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 if (ca_list[j]->algorithm_mkey) {
803 if (alg_mkey) {
804 alg_mkey &= ca_list[j]->algorithm_mkey;
805 if (!alg_mkey) {
806 found = 0;
807 break;
808 }
809 } else {
810 alg_mkey = ca_list[j]->algorithm_mkey;
811 }
812 }
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 if (ca_list[j]->algorithm_auth) {
815 if (alg_auth) {
816 alg_auth &= ca_list[j]->algorithm_auth;
817 if (!alg_auth) {
818 found = 0;
819 break;
820 }
821 } else {
822 alg_auth = ca_list[j]->algorithm_auth;
823 }
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 if (ca_list[j]->algorithm_enc) {
827 if (alg_enc) {
828 alg_enc &= ca_list[j]->algorithm_enc;
829 if (!alg_enc) {
830 found = 0;
831 break;
832 }
833 } else {
834 alg_enc = ca_list[j]->algorithm_enc;
835 }
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 if (ca_list[j]->algorithm_mac) {
839 if (alg_mac) {
840 alg_mac &= ca_list[j]->algorithm_mac;
841 if (!alg_mac) {
842 found = 0;
843 break;
844 }
845 } else {
846 alg_mac = ca_list[j]->algorithm_mac;
847 }
848 }
Adam Langley858a88d2014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 if (ca_list[j]->algo_strength) {
851 if (algo_strength) {
852 algo_strength &= ca_list[j]->algo_strength;
853 if (!algo_strength) {
854 found = 0;
855 break;
856 }
857 } else {
858 algo_strength |= ca_list[j]->algo_strength;
859 }
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 if (ca_list[j]->valid) {
863 /* explicit ciphersuite found; its protocol version does not become
864 * part of the search pattern! */
865 cipher_id = ca_list[j]->id;
866 } else {
867 /* not an explicit ciphersuite; only in this case, the protocol version
868 * is considered part of the search pattern. */
869 if (ca_list[j]->algorithm_ssl) {
870 if (alg_ssl) {
871 alg_ssl &= ca_list[j]->algorithm_ssl;
872 if (!alg_ssl) {
873 found = 0;
874 break;
875 }
876 } else {
877 alg_ssl = ca_list[j]->algorithm_ssl;
878 }
879 }
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (!multi) {
883 break;
884 }
885 }
Adam Langley95c29f32014-06-20 12:00:00 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 /* Ok, we have the rule, now apply it. */
888 if (rule == CIPHER_SPECIAL) {
889 /* special command */
890 ok = 0;
891 if (buflen == 8 && !strncmp(buf, "STRENGTH", 8)) {
892 ok = ssl_cipher_strength_sort(head_p, tail_p);
893 } else {
894 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
895 SSL_R_INVALID_COMMAND);
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (ok == 0) {
899 retval = 0;
900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 /* We do not support any "multi" options together with "@", so throw away
903 * the rest of the command, if any left, until end or ':' is found. */
904 while (*l != '\0' && !ITEM_SEP(*l)) {
905 l++;
906 }
907 } else if (found) {
908 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
909 alg_ssl, algo_strength, rule, -1, in_group, head_p,
910 tail_p);
911 } else {
912 while (*l != '\0' && !ITEM_SEP(*l)) {
913 l++;
914 }
915 }
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 if (in_group) {
919 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
920 retval = 0;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 return retval;
924}
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926STACK_OF(SSL_CIPHER) *
927ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
928 struct ssl_cipher_preference_list_st **cipher_list,
929 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
930 const char *rule_str, CERT *c) {
931 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
932 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
933 const char *rule_p;
934 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
935 const SSL_CIPHER **ca_list = NULL;
936 uint8_t *in_group_flags = NULL;
937 unsigned int num_in_group_flags = 0;
938 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 /* Return with error if nothing to do. */
941 if (rule_str == NULL || cipher_list == NULL) {
942 return NULL;
943 }
David Benjamin5213df42014-08-20 14:19:54 -0400944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 /* Now we have to collect the available ciphers from the compiled in ciphers.
946 * We cannot get more than the number compiled in, so it is used for
947 * allocation. */
948 num_of_ciphers = ssl_method->num_ciphers();
949 co_list =
950 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
951 if (co_list == NULL) {
952 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
953 return NULL;
954 }
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 /* Now arrange all ciphers by preference:
959 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
962 * key exchange mechanisms */
David Benjamin7061e282015-03-19 11:10:48 -0400963 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 0, &head, &tail);
David Benjamin7061e282015-03-19 11:10:48 -0400965 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 &tail);
David Benjamin7061e282015-03-19 11:10:48 -0400967 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head,
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700969
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
971 * CHACHA20 unless there is hardware support for fast and constant-time
972 * AES_GCM. */
973 if (EVP_has_aes_hardware()) {
974 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
975 &head, &tail);
976 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
977 &head, &tail);
978 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
979 -1, 0, &head, &tail);
980 } else {
981 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
982 -1, 0, &head, &tail);
983 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
984 &head, &tail);
985 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
986 &head, &tail);
987 }
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
990 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
991 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
992 &tail);
993 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
994 &tail);
995 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
996 &head, &tail);
997 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
998 &head, &tail);
999 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1000 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 /* Temporarily enable everything else for sorting */
1003 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 /* Move ciphers without forward secrecy to the end. */
David Benjamin7061e282015-03-19 11:10:48 -04001006 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), 0, 0, 0, 0, 0, CIPHER_ORD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1010 * (For applications that allow them, they aren't too bad, but we prefer
1011 * authenticated ciphers.)
1012 * TODO(davidben): Remove them altogether? */
1013 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head,
1014 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 /* Now disable everything (maintaining the ordering!) */
1017 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 /* We also need cipher aliases for selecting based on the rule_str. There
1020 * might be two types of entries in the rule_str: 1) names of ciphers
1021 * themselves 2) aliases for groups of ciphers. For 1) we need the available
1022 * ciphers and for 2) the cipher groups of cipher_aliases added together in
1023 * one list (otherwise we would be happy with just the cipher_aliases
1024 * table). */
1025 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1026 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1027 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1028 if (ca_list == NULL) {
1029 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1030 goto err;
1031 }
1032 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001033
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 /* If the rule_string begins with DEFAULT, apply the default rule before
1035 * using the (possibly available) additional rules. */
1036 ok = 1;
1037 rule_p = rule_str;
1038 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1039 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1040 ca_list);
1041 rule_p += 7;
1042 if (*rule_p == ':') {
1043 rule_p++;
1044 }
1045 }
Adam Langley858a88d2014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 if (ok && strlen(rule_p) > 0) {
1048 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Nick Harper4dd053e2014-12-22 14:58:54 -08001051 OPENSSL_free((void *)ca_list); /* Not needed anymore */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 if (!ok) {
1054 goto err;
1055 }
1056
1057 /* Allocate new "cipherstack" for the result, return with error
1058 * if we cannot get one. */
1059 cipherstack = sk_SSL_CIPHER_new_null();
1060 if (cipherstack == NULL) {
1061 goto err;
1062 }
1063
1064 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1065 if (!in_group_flags) {
1066 goto err;
1067 }
1068
1069 /* The cipher selection for the list is done. The ciphers are added
1070 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1071 for (curr = head; curr != NULL; curr = curr->next) {
1072 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001073 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1074 goto err;
1075 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001076 in_group_flags[num_in_group_flags++] = curr->in_group;
1077 }
1078 }
1079 OPENSSL_free(co_list); /* Not needed any longer */
1080 co_list = NULL;
1081
1082 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1083 if (tmp_cipher_list == NULL) {
1084 goto err;
1085 }
1086 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1087 if (!pref_list) {
1088 goto err;
1089 }
1090 pref_list->ciphers = cipherstack;
1091 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1092 if (!pref_list->in_group_flags) {
1093 goto err;
1094 }
1095 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1096 OPENSSL_free(in_group_flags);
1097 in_group_flags = NULL;
1098 if (*cipher_list != NULL) {
1099 ssl_cipher_preference_list_free(*cipher_list);
1100 }
1101 *cipher_list = pref_list;
1102 pref_list = NULL;
1103
1104 if (cipher_list_by_id != NULL) {
1105 if (*cipher_list_by_id != NULL) {
1106 sk_SSL_CIPHER_free(*cipher_list_by_id);
1107 }
1108 *cipher_list_by_id = tmp_cipher_list;
1109 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001110 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001111
1112 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1113 } else {
1114 sk_SSL_CIPHER_free(tmp_cipher_list);
1115 tmp_cipher_list = NULL;
1116 }
1117
1118 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001119
1120err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 if (co_list) {
1122 OPENSSL_free(co_list);
1123 }
1124 if (in_group_flags) {
1125 OPENSSL_free(in_group_flags);
1126 }
1127 if (cipherstack) {
1128 sk_SSL_CIPHER_free(cipherstack);
1129 }
1130 if (tmp_cipher_list) {
1131 sk_SSL_CIPHER_free(tmp_cipher_list);
1132 }
1133 if (pref_list && pref_list->in_group_flags) {
1134 OPENSSL_free(pref_list->in_group_flags);
1135 }
1136 if (pref_list) {
1137 OPENSSL_free(pref_list);
1138 }
1139 return NULL;
1140}
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1143 int len) {
1144 const char *ver;
1145 const char *kx, *au, *enc, *mac;
1146 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1147 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 alg_mkey = cipher->algorithm_mkey;
1150 alg_auth = cipher->algorithm_auth;
1151 alg_enc = cipher->algorithm_enc;
1152 alg_mac = cipher->algorithm_mac;
1153 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (alg_ssl & SSL_SSLV3) {
1156 ver = "SSLv3";
1157 } else if (alg_ssl & SSL_TLSV1_2) {
1158 ver = "TLSv1.2";
1159 } else {
1160 ver = "unknown";
1161 }
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 switch (alg_mkey) {
1164 case SSL_kRSA:
1165 kx = "RSA";
1166 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001167
David Benjamin7061e282015-03-19 11:10:48 -04001168 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 kx = "DH";
1170 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001171
David Benjamin7061e282015-03-19 11:10:48 -04001172 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 kx = "ECDH";
1174 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 case SSL_kPSK:
1177 kx = "PSK";
1178 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 default:
1181 kx = "unknown";
1182 }
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 switch (alg_auth) {
1185 case SSL_aRSA:
1186 au = "RSA";
1187 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 case SSL_aNULL:
1190 au = "None";
1191 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 case SSL_aECDSA:
1194 au = "ECDSA";
1195 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 case SSL_aPSK:
1198 au = "PSK";
1199 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001200
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 default:
1202 au = "unknown";
1203 break;
1204 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001205
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 switch (alg_enc) {
1207 case SSL_3DES:
1208 enc = "3DES(168)";
1209 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001210
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 case SSL_RC4:
1212 enc = "RC4(128)";
1213 break;
1214
1215 case SSL_AES128:
1216 enc = "AES(128)";
1217 break;
1218
1219 case SSL_AES256:
1220 enc = "AES(256)";
1221 break;
1222
1223 case SSL_AES128GCM:
1224 enc = "AESGCM(128)";
1225 break;
1226
1227 case SSL_AES256GCM:
1228 enc = "AESGCM(256)";
1229 break;
1230
1231 case SSL_CHACHA20POLY1305:
1232 enc = "ChaCha20-Poly1305";
1233 break;
1234
1235 default:
1236 enc = "unknown";
1237 break;
1238 }
1239
1240 switch (alg_mac) {
1241 case SSL_MD5:
1242 mac = "MD5";
1243 break;
1244
1245 case SSL_SHA1:
1246 mac = "SHA1";
1247 break;
1248
1249 case SSL_SHA256:
1250 mac = "SHA256";
1251 break;
1252
1253 case SSL_SHA384:
1254 mac = "SHA384";
1255 break;
1256
1257 case SSL_AEAD:
1258 mac = "AEAD";
1259 break;
1260
1261 default:
1262 mac = "unknown";
1263 break;
1264 }
1265
1266 if (buf == NULL) {
1267 len = 128;
1268 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001269 if (buf == NULL) {
1270 return NULL;
1271 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001272 } else if (len < 128) {
1273 return "Buffer too small";
1274 }
1275
1276 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1277 return buf;
1278}
1279
1280int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1281 return (c->algorithm_enc & SSL_AES) != 0;
1282}
1283
1284int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1285 return (c->algorithm_mac & SSL_MD5) != 0;
1286}
1287
1288int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1289 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1290}
1291
1292int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1293 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1294}
1295
1296const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1297 int i;
1298
1299 if (c == NULL) {
1300 return "(NONE)";
1301 }
1302
1303 i = (int)(c->id >> 24L);
1304 if (i == 3) {
1305 return "TLSv1/SSLv3";
1306 } else if (i == 2) {
1307 return "SSLv2";
1308 } else {
1309 return "unknown";
1310 }
1311}
Adam Langley95c29f32014-06-20 12:00:00 -07001312
1313/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001314const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1315 if (c != NULL) {
1316 return c->name;
1317 }
1318
1319 return "(NONE)";
1320}
Adam Langley95c29f32014-06-20 12:00:00 -07001321
Adam Langleyf1df2b32014-08-11 17:37:53 -07001322const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1323 if (cipher == NULL) {
1324 return "";
1325 }
1326
1327 switch (cipher->algorithm_mkey) {
1328 case SSL_kRSA:
David Benjamin65226252015-02-05 16:49:47 -05001329 return "RSA";
Adam Langleyfcf25832014-12-18 17:42:32 -08001330
David Benjamin7061e282015-03-19 11:10:48 -04001331 case SSL_kDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001332 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001333 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001334 return "DHE_RSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001335 case SSL_aNULL:
David Benjamin65226252015-02-05 16:49:47 -05001336 return "DH_anon";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001337 default:
David Benjamin65226252015-02-05 16:49:47 -05001338 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001339 return "UNKNOWN";
1340 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001341
David Benjamin7061e282015-03-19 11:10:48 -04001342 case SSL_kECDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001343 switch (cipher->algorithm_auth) {
1344 case SSL_aECDSA:
David Benjamin65226252015-02-05 16:49:47 -05001345 return "ECDHE_ECDSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001346 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001347 return "ECDHE_RSA";
1348 case SSL_aPSK:
1349 return "ECDHE_PSK";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001350 case SSL_aNULL:
David Benjamin65226252015-02-05 16:49:47 -05001351 return "ECDH_anon";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001352 default:
David Benjamin65226252015-02-05 16:49:47 -05001353 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001354 return "UNKNOWN";
1355 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001356
David Benjamin65226252015-02-05 16:49:47 -05001357 case SSL_kPSK:
1358 assert(cipher->algorithm_auth == SSL_aPSK);
1359 return "PSK";
1360
Adam Langleyf1df2b32014-08-11 17:37:53 -07001361 default:
David Benjamin65226252015-02-05 16:49:47 -05001362 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001363 return "UNKNOWN";
1364 }
1365}
1366
David Benjamin65226252015-02-05 16:49:47 -05001367static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1368 switch (cipher->algorithm_enc) {
1369 case SSL_3DES:
1370 return "3DES_EDE_CBC";
1371 case SSL_RC4:
1372 return "RC4";
1373 case SSL_AES128:
1374 return "AES_128_CBC";
1375 case SSL_AES256:
1376 return "AES_256_CBC";
1377 case SSL_AES128GCM:
1378 return "AES_128_GCM";
1379 case SSL_AES256GCM:
1380 return "AES_256_GCM";
1381 case SSL_CHACHA20POLY1305:
1382 return "CHACHA20_POLY1305";
1383 break;
1384 default:
1385 assert(0);
1386 return "UNKNOWN";
1387 }
1388}
1389
1390static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
1391 if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) {
1392 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1393 * only ever MD5 or SHA-1. */
1394 switch (cipher->algorithm_mac) {
1395 case SSL_MD5:
1396 return "MD5";
1397 case SSL_SHA1:
1398 return "SHA";
1399 default:
1400 assert(0);
1401 return "UNKNOWN";
1402 }
1403 } else if (cipher->algorithm2 & TLS1_PRF_SHA256) {
1404 return "SHA256";
1405 } else if (cipher->algorithm2 & TLS1_PRF_SHA384) {
1406 return "SHA384";
1407 } else {
1408 assert(0);
1409 return "UNKNOWN";
1410 }
1411}
1412
1413char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1414 if (cipher == NULL) {
1415 return NULL;
1416 }
1417
1418 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1419 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1420 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1421
1422 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1423 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1424 strlen(prf_name) + 1;
1425 char *ret = OPENSSL_malloc(len);
1426 if (ret == NULL) {
1427 return NULL;
1428 }
1429 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1430 BUF_strlcat(ret, kx_name, len) >= len ||
1431 BUF_strlcat(ret, "_WITH_", len) >= len ||
1432 BUF_strlcat(ret, enc_name, len) >= len ||
1433 BUF_strlcat(ret, "_", len) >= len ||
1434 BUF_strlcat(ret, prf_name, len) >= len) {
1435 assert(0);
1436 OPENSSL_free(ret);
1437 return NULL;
1438 }
1439 assert(strlen(ret) + 1 == len);
1440 return ret;
1441}
1442
Adam Langley95c29f32014-06-20 12:00:00 -07001443/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001444int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1445 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 if (c != NULL) {
1448 if (alg_bits != NULL) {
1449 *alg_bits = c->alg_bits;
1450 }
1451 ret = c->strength_bits;
1452 }
Adam Langley95c29f32014-06-20 12:00:00 -07001453
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 return ret;
1455}
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langleyfcf25832014-12-18 17:42:32 -08001459void *SSL_COMP_get_compression_methods(void) { return NULL; }
1460
1461int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1462
1463const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
1465/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001466int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
1467 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 if (alg_a & SSL_aECDSA) {
1470 return SSL_PKEY_ECC;
1471 } else if (alg_a & SSL_aRSA) {
1472 return SSL_PKEY_RSA_ENC;
1473 }
Adam Langley95c29f32014-06-20 12:00:00 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 return -1;
1476}
David Benjamin9c651c92014-07-12 13:27:45 -04001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1479 * public key in the key exchange, sent in a server Certificate message.
1480 * Otherwise it returns 0. */
1481int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
1482 /* Anonymous ciphers do not include a server certificate. */
1483 if (cipher->algorithm_auth & SSL_aNULL) {
1484 return 0;
1485 }
1486
1487 /* Neither do PSK ciphers, except for RSA_PSK. */
1488 if ((cipher->algorithm_auth & SSL_aPSK) &&
1489 !(cipher->algorithm_mkey & SSL_kRSA)) {
1490 return 0;
1491 }
1492
1493 /* All other ciphers include it. */
1494 return 1;
1495}
1496
1497/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1498 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001499 *
1500 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001501 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1502 * communicate a psk_identity_hint, so it is optional. */
1503int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1504 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001505 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 return 1;
1507 }
1508
1509 /* It is optional in all others. */
1510 return 0;
1511}