blob: a46aa7a96a5833727a1cd850777665ee4ed04696 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400142#include <stdio.h>
143#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjaminf0ae1702015-04-07 23:05:04 -0400145#include <openssl/err.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500146#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700147#include <openssl/mem.h>
148#include <openssl/obj.h>
David Benjaminea72bd02014-12-21 21:27:41 -0500149#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin2ee94aa2015-04-07 22:38:30 -0400151#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154struct handshake_digest {
David Benjamin107db582015-04-08 00:41:59 -0400155 uint32_t mask;
Adam Langleyfcf25832014-12-18 17:42:32 -0800156 const EVP_MD *(*md_func)(void);
David Benjamind633d632014-08-30 14:43:27 -0400157};
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
160 {SSL_HANDSHAKE_MAC_MD5, EVP_md5},
161 {SSL_HANDSHAKE_MAC_SHA, EVP_sha1},
162 {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256},
163 {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384},
164};
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langleyfcf25832014-12-18 17:42:32 -0800166#define CIPHER_ADD 1
167#define CIPHER_KILL 2
168#define CIPHER_DEL 3
169#define CIPHER_ORD 4
170#define CIPHER_SPECIAL 5
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172typedef struct cipher_order_st {
173 const SSL_CIPHER *cipher;
174 int active;
175 int dead;
176 int in_group;
177 struct cipher_order_st *next, *prev;
178} CIPHER_ORDER;
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langleyfcf25832014-12-18 17:42:32 -0800180static const SSL_CIPHER cipher_aliases[] =
181 {
182 {0, SSL_TXT_ALL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin32fbdf22015-04-07 01:14:06 -0400184 /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 /* key exchange aliases
187 * (some of those using only a single bit here combine
188 * multiple key exchange algs according to the RFCs,
189 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
190 {0, SSL_TXT_kRSA, 0, SSL_kRSA, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjamin7061e282015-03-19 11:10:48 -0400192 {0, SSL_TXT_kDHE, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
193 {0, SSL_TXT_kEDH, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
194 {0, SSL_TXT_DH, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin7061e282015-03-19 11:10:48 -0400196 {0, SSL_TXT_kECDHE, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
197 {0, SSL_TXT_kEECDH, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
198 {0, SSL_TXT_ECDH, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 {0, SSL_TXT_kPSK, 0, SSL_kPSK, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 /* server authentication aliases */
203 {0, SSL_TXT_aRSA, 0, 0, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 {0, SSL_TXT_aECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
205 {0, SSL_TXT_ECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
206 {0, SSL_TXT_aPSK, 0, 0, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 /* aliases combining key exchange and server authentication */
David Benjamin32fbdf22015-04-07 01:14:06 -0400209 {0, SSL_TXT_DHE, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
210 {0, SSL_TXT_EDH, 0, SSL_kDHE, 0, 0, 0, 0, 0, 0, 0, 0},
211 {0, SSL_TXT_ECDHE, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
212 {0, SSL_TXT_EECDH, 0, SSL_kECDHE, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 {0, SSL_TXT_RSA, 0, SSL_kRSA, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 {0, SSL_TXT_PSK, 0, SSL_kPSK, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700215
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 /* symmetric encryption aliases */
217 {0, SSL_TXT_3DES, 0, 0, 0, SSL_3DES, 0, 0, 0, 0, 0, 0},
218 {0, SSL_TXT_RC4, 0, 0, 0, SSL_RC4, 0, 0, 0, 0, 0, 0},
219 {0, SSL_TXT_AES128, 0, 0, 0, SSL_AES128 | SSL_AES128GCM, 0, 0, 0, 0, 0, 0},
220 {0, SSL_TXT_AES256, 0, 0, 0, SSL_AES256 | SSL_AES256GCM, 0, 0, 0, 0, 0, 0},
221 {0, SSL_TXT_AES, 0, 0, 0, SSL_AES, 0, 0, 0, 0, 0, 0},
222 {0, SSL_TXT_AES_GCM, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM, 0, 0, 0, 0, 0,
223 0},
224 {0, SSL_TXT_CHACHA20, 0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* MAC aliases */
227 {0, SSL_TXT_MD5, 0, 0, 0, 0, SSL_MD5, 0, 0, 0, 0, 0},
228 {0, SSL_TXT_SHA1, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
229 {0, SSL_TXT_SHA, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
230 {0, SSL_TXT_SHA256, 0, 0, 0, 0, SSL_SHA256, 0, 0, 0, 0, 0},
231 {0, SSL_TXT_SHA384, 0, 0, 0, 0, SSL_SHA384, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 /* protocol version aliases */
234 {0, SSL_TXT_SSLV3, 0, 0, 0, 0, 0, SSL_SSLV3, 0, 0, 0, 0},
235 {0, SSL_TXT_TLSV1, 0, 0, 0, 0, 0, SSL_TLSV1, 0, 0, 0, 0},
236 {0, SSL_TXT_TLSV1_2, 0, 0, 0, 0, 0, SSL_TLSV1_2, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 /* strength classes */
239 {0, SSL_TXT_MEDIUM, 0, 0, 0, 0, 0, 0, SSL_MEDIUM, 0, 0, 0},
240 {0, SSL_TXT_HIGH, 0, 0, 0, 0, 0, 0, SSL_HIGH, 0, 0, 0},
241 /* FIPS 140-2 approved ciphersuite */
242 {0, SSL_TXT_FIPS, 0, 0, 0, 0, 0, 0, SSL_FIPS, 0, 0, 0},
243};
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjaminea72bd02014-12-21 21:27:41 -0500245int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
246 size_t *out_mac_secret_len,
247 size_t *out_fixed_iv_len,
248 const SSL_CIPHER *cipher, uint16_t version) {
249 *out_aead = NULL;
250 *out_mac_secret_len = 0;
251 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700252
David Benjaminea72bd02014-12-21 21:27:41 -0500253 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500255 *out_aead = EVP_aead_aes_128_gcm();
256 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 return 1;
258
259 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500260 *out_aead = EVP_aead_aes_256_gcm();
261 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 return 1;
263
264 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500265 *out_aead = EVP_aead_chacha20_poly1305();
266 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268
269 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500270 switch (cipher->algorithm_mac) {
271 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500272 if (version == SSL3_VERSION) {
273 *out_aead = EVP_aead_rc4_md5_ssl3();
274 } else {
275 *out_aead = EVP_aead_rc4_md5_tls();
276 }
David Benjaminea72bd02014-12-21 21:27:41 -0500277 *out_mac_secret_len = MD5_DIGEST_LENGTH;
278 return 1;
279 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500280 if (version == SSL3_VERSION) {
281 *out_aead = EVP_aead_rc4_sha1_ssl3();
282 } else {
283 *out_aead = EVP_aead_rc4_sha1_tls();
284 }
David Benjaminea72bd02014-12-21 21:27:41 -0500285 *out_mac_secret_len = SHA_DIGEST_LENGTH;
286 return 1;
287 default:
288 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800290
David Benjaminea72bd02014-12-21 21:27:41 -0500291 case SSL_AES128:
292 switch (cipher->algorithm_mac) {
293 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500294 if (version == SSL3_VERSION) {
295 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
296 *out_fixed_iv_len = 16;
297 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500298 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
299 *out_fixed_iv_len = 16;
300 } else {
301 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
302 }
303 *out_mac_secret_len = SHA_DIGEST_LENGTH;
304 return 1;
305 case SSL_SHA256:
306 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
307 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
308 return 1;
309 default:
310 return 0;
311 }
312
313 case SSL_AES256:
314 switch (cipher->algorithm_mac) {
315 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500316 if (version == SSL3_VERSION) {
317 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
318 *out_fixed_iv_len = 16;
319 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500320 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
321 *out_fixed_iv_len = 16;
322 } else {
323 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
324 }
325 *out_mac_secret_len = SHA_DIGEST_LENGTH;
326 return 1;
327 case SSL_SHA256:
328 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
329 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
330 return 1;
331 case SSL_SHA384:
332 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
333 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
334 return 1;
335 default:
336 return 0;
337 }
338
339 case SSL_3DES:
340 switch (cipher->algorithm_mac) {
341 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500342 if (version == SSL3_VERSION) {
343 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
344 *out_fixed_iv_len = 8;
345 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500346 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
347 *out_fixed_iv_len = 8;
348 } else {
349 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
350 }
351 *out_mac_secret_len = SHA_DIGEST_LENGTH;
352 return 1;
353 default:
354 return 0;
355 }
356
357 default:
358 return 0;
359 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800360}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700361
David Benjamin107db582015-04-08 00:41:59 -0400362int ssl_get_handshake_digest(size_t idx, uint32_t *mask, const EVP_MD **md) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500363 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 return 0;
365 }
366 *mask = ssl_handshake_digests[idx].mask;
367 *md = ssl_handshake_digests[idx].md_func();
368 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700369}
370
371#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700373
374static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 CIPHER_ORDER **tail) {
376 if (curr == *tail) {
377 return;
378 }
379 if (curr == *head) {
380 *head = curr->next;
381 }
382 if (curr->prev != NULL) {
383 curr->prev->next = curr->next;
384 }
385 if (curr->next != NULL) {
386 curr->next->prev = curr->prev;
387 }
388 (*tail)->next = curr;
389 curr->prev = *tail;
390 curr->next = NULL;
391 *tail = curr;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
394static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 CIPHER_ORDER **tail) {
396 if (curr == *head) {
397 return;
398 }
399 if (curr == *tail) {
400 *tail = curr->prev;
401 }
402 if (curr->next != NULL) {
403 curr->next->prev = curr->prev;
404 }
405 if (curr->prev != NULL) {
406 curr->prev->next = curr->next;
407 }
408 (*head)->prev = curr;
409 curr->next = *head;
410 curr->prev = NULL;
411 *head = curr;
412}
Adam Langley95c29f32014-06-20 12:00:00 -0700413
David Benjamin82c9e902014-12-12 15:55:27 -0500414static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 int num_of_ciphers,
416 CIPHER_ORDER *co_list,
417 CIPHER_ORDER **head_p,
418 CIPHER_ORDER **tail_p) {
419 int i, co_list_num;
420 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 /* We have num_of_ciphers descriptions compiled in, depending on the method
423 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
424 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 /* Get the initial list of ciphers */
427 co_list_num = 0; /* actual count of ciphers */
428 for (i = 0; i < num_of_ciphers; i++) {
429 c = ssl_method->get_cipher(i);
430 /* drop those that use any of that is not available */
431 if (c != NULL && c->valid) {
432 co_list[co_list_num].cipher = c;
433 co_list[co_list_num].next = NULL;
434 co_list[co_list_num].prev = NULL;
435 co_list[co_list_num].active = 0;
436 co_list[co_list_num].in_group = 0;
437 co_list_num++;
438 }
439 }
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 /* Prepare linked list from list entries. */
442 if (co_list_num > 0) {
443 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 if (co_list_num > 1) {
446 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 for (i = 1; i < co_list_num - 1; i++) {
449 co_list[i].prev = &co_list[i - 1];
450 co_list[i].next = &co_list[i + 1];
451 }
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
454 }
455
456 co_list[co_list_num - 1].next = NULL;
457
458 *head_p = &co_list[0];
459 *tail_p = &co_list[co_list_num - 1];
460 }
461}
Adam Langley95c29f32014-06-20 12:00:00 -0700462
463static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 int num_of_group_aliases,
465 CIPHER_ORDER *head) {
466 CIPHER_ORDER *ciph_curr;
467 const SSL_CIPHER **ca_curr;
468 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 /* First, add the real ciphers as already collected. */
471 ciph_curr = head;
472 ca_curr = ca_list;
473 while (ciph_curr != NULL) {
474 *ca_curr = ciph_curr->cipher;
475 ca_curr++;
476 ciph_curr = ciph_curr->next;
477 }
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 /* Now we add the available ones from the cipher_aliases[] table. They
480 * represent either one or more algorithms, some of which in any affected
481 * category must be supported (set in enabled_mask), or represent a cipher
482 * strength value (will be added in any case because algorithms=0). */
483 for (i = 0; i < num_of_group_aliases; i++) {
484 *ca_curr = cipher_aliases + i;
485 ca_curr++;
486 }
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 *ca_curr = NULL; /* end of list */
489}
Adam Langley95c29f32014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491static void ssl_cipher_apply_rule(
David Benjamin107db582015-04-08 00:41:59 -0400492 uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
493 uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
494 uint32_t algo_strength, int rule, int strength_bits, int in_group,
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
496 CIPHER_ORDER *head, *tail, *curr, *next, *last;
497 const SSL_CIPHER *cp;
498 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 if (rule == CIPHER_DEL) {
501 /* needed to maintain sorting between currently deleted ciphers */
502 reverse = 1;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 head = *head_p;
506 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 if (reverse) {
509 next = tail;
510 last = head;
511 } else {
512 next = head;
513 last = tail;
514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 curr = NULL;
517 for (;;) {
518 if (curr == last) {
519 break;
520 }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 curr = next;
523 if (curr == NULL) {
524 break;
525 }
Adam Langleye3142a72014-07-24 17:56:48 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 next = reverse ? curr->prev : curr->next;
528 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 /* Selection criteria is either the value of strength_bits
531 * or the algorithms used. */
532 if (strength_bits >= 0) {
533 if (strength_bits != cp->strength_bits) {
534 continue;
535 }
536 } else {
537 if ((alg_mkey && !(alg_mkey & cp->algorithm_mkey)) ||
538 (alg_auth && !(alg_auth & cp->algorithm_auth)) ||
539 (alg_enc && !(alg_enc & cp->algorithm_enc)) ||
540 (alg_mac && !(alg_mac & cp->algorithm_mac)) ||
541 (alg_ssl && !(alg_ssl & cp->algorithm_ssl)) ||
542 (algo_strength && !(algo_strength & cp->algo_strength))) {
543 continue;
544 }
545 }
Adam Langleye3142a72014-07-24 17:56:48 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 /* add the cipher if it has not been added yet. */
548 if (rule == CIPHER_ADD) {
549 /* reverse == 0 */
550 if (!curr->active) {
551 ll_append_tail(&head, curr, &tail);
552 curr->active = 1;
553 curr->in_group = in_group;
554 }
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 /* Move the added cipher to this location */
558 else if (rule == CIPHER_ORD) {
559 /* reverse == 0 */
560 if (curr->active) {
561 ll_append_tail(&head, curr, &tail);
562 curr->in_group = 0;
563 }
564 } else if (rule == CIPHER_DEL) {
565 /* reverse == 1 */
566 if (curr->active) {
567 /* most recently deleted ciphersuites get best positions
568 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
569 * works in reverse to maintain the order) */
570 ll_append_head(&head, curr, &tail);
571 curr->active = 0;
572 curr->in_group = 0;
573 }
574 } else if (rule == CIPHER_KILL) {
575 /* reverse == 0 */
576 if (head == curr) {
577 head = curr->next;
578 } else {
579 curr->prev->next = curr->next;
580 }
Adam Langley95c29f32014-06-20 12:00:00 -0700581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 if (tail == curr) {
583 tail = curr->prev;
584 }
585 curr->active = 0;
586 if (curr->next != NULL) {
587 curr->next->prev = curr->prev;
588 }
589 if (curr->prev != NULL) {
590 curr->prev->next = curr->next;
591 }
592 curr->next = NULL;
593 curr->prev = NULL;
594 }
595 }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 *head_p = head;
598 *tail_p = tail;
599}
Adam Langley95c29f32014-06-20 12:00:00 -0700600
601static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 CIPHER_ORDER **tail_p) {
603 int max_strength_bits, i, *number_uses;
604 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 /* This routine sorts the ciphers with descending strength. The sorting must
607 * keep the pre-sorted sequence, so we apply the normal sorting routine as
608 * '+' movement to the end of the list. */
609 max_strength_bits = 0;
610 curr = *head_p;
611 while (curr != NULL) {
612 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
613 max_strength_bits = curr->cipher->strength_bits;
614 }
615 curr = curr->next;
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
619 if (!number_uses) {
620 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
621 return 0;
622 }
623 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 /* Now find the strength_bits values actually used. */
626 curr = *head_p;
627 while (curr != NULL) {
628 if (curr->active) {
629 number_uses[curr->cipher->strength_bits]++;
630 }
631 curr = curr->next;
632 }
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 /* Go through the list of used strength_bits values in descending order. */
635 for (i = max_strength_bits; i >= 0; i--) {
636 if (number_uses[i] > 0) {
637 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
638 tail_p);
639 }
640 }
641
642 OPENSSL_free(number_uses);
643 return 1;
644}
Adam Langley95c29f32014-06-20 12:00:00 -0700645
646static int ssl_cipher_process_rulestr(const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 CIPHER_ORDER **head_p,
648 CIPHER_ORDER **tail_p,
649 const SSL_CIPHER **ca_list) {
David Benjamin107db582015-04-08 00:41:59 -0400650 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 const char *l, *buf;
652 int j, multi, found, rule, retval, ok, buflen, in_group = 0, has_group = 0;
David Benjamin107db582015-04-08 00:41:59 -0400653 uint32_t cipher_id = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 retval = 1;
657 l = rule_str;
658 for (;;) {
659 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700660
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 if (ch == '\0') {
662 break; /* done */
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 if (in_group) {
666 if (ch == ']') {
667 if (!in_group) {
668 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
669 SSL_R_UNEXPECTED_GROUP_CLOSE);
670 retval = found = in_group = 0;
671 break;
672 }
673 if (*tail_p) {
674 (*tail_p)->in_group = 0;
675 }
676 in_group = 0;
677 l++;
678 continue;
679 }
David Benjamin37d92462014-09-20 17:54:24 -0400680
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 if (ch == '|') {
682 rule = CIPHER_ADD;
683 l++;
684 continue;
685 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
686 !(ch >= '0' && ch <= '9')) {
687 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
688 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
689 retval = found = in_group = 0;
690 break;
691 } else {
692 rule = CIPHER_ADD;
693 }
694 } else if (ch == '-') {
695 rule = CIPHER_DEL;
696 l++;
697 } else if (ch == '+') {
698 rule = CIPHER_ORD;
699 l++;
700 } else if (ch == '!') {
701 rule = CIPHER_KILL;
702 l++;
703 } else if (ch == '@') {
704 rule = CIPHER_SPECIAL;
705 l++;
706 } else if (ch == '[') {
707 if (in_group) {
708 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
709 retval = found = in_group = 0;
710 break;
711 }
712 in_group = 1;
713 has_group = 1;
714 l++;
715 continue;
716 } else {
717 rule = CIPHER_ADD;
718 }
Adam Langley95c29f32014-06-20 12:00:00 -0700719
Adam Langleyfcf25832014-12-18 17:42:32 -0800720 /* If preference groups are enabled, the only legal operator is +.
721 * Otherwise the in_group bits will get mixed up. */
722 if (has_group && rule != CIPHER_ADD) {
723 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
724 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
725 retval = found = in_group = 0;
726 break;
727 }
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langleyfcf25832014-12-18 17:42:32 -0800729 if (ITEM_SEP(ch)) {
730 l++;
731 continue;
732 }
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 alg_mkey = 0;
735 alg_auth = 0;
736 alg_enc = 0;
737 alg_mac = 0;
738 alg_ssl = 0;
739 algo_strength = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langleyfcf25832014-12-18 17:42:32 -0800741 for (;;) {
742 ch = *l;
743 buf = l;
744 buflen = 0;
745 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
746 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
747 ch = *(++l);
748 buflen++;
749 }
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 if (buflen == 0) {
752 /* We hit something we cannot deal with, it is no command or separator
753 * nor alphanumeric, so we call this an error. */
754 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
755 SSL_R_INVALID_COMMAND);
756 retval = found = in_group = 0;
757 l++;
758 break;
759 }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 if (rule == CIPHER_SPECIAL) {
762 found = 0; /* unused -- avoid compiler warning */
763 break; /* special treatment */
764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 /* check for multi-part specification */
767 if (ch == '+') {
768 multi = 1;
769 l++;
770 } else {
771 multi = 0;
772 }
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 /* Now search for the cipher alias in the ca_list. Be careful with the
775 * strncmp, because the "buflen" limitation will make the rule "ADH:SOME"
776 * and the cipher "ADH-MY-CIPHER" look like a match for buflen=3. So
777 * additionally check whether the cipher name found has the correct
778 * length. We can save a strlen() call: just checking for the '\0' at the
779 * right place is sufficient, we have to strncmp() anyway. (We cannot use
780 * strcmp(), because buf is not '\0' terminated.) */
781 j = found = 0;
782 cipher_id = 0;
783 while (ca_list[j]) {
784 if (!strncmp(buf, ca_list[j]->name, buflen) &&
785 (ca_list[j]->name[buflen] == '\0')) {
786 found = 1;
787 break;
788 } else {
789 j++;
790 }
791 }
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langleyfcf25832014-12-18 17:42:32 -0800793 if (!found) {
794 break; /* ignore this entry */
795 }
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 if (ca_list[j]->algorithm_mkey) {
798 if (alg_mkey) {
799 alg_mkey &= ca_list[j]->algorithm_mkey;
800 if (!alg_mkey) {
801 found = 0;
802 break;
803 }
804 } else {
805 alg_mkey = ca_list[j]->algorithm_mkey;
806 }
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 if (ca_list[j]->algorithm_auth) {
810 if (alg_auth) {
811 alg_auth &= ca_list[j]->algorithm_auth;
812 if (!alg_auth) {
813 found = 0;
814 break;
815 }
816 } else {
817 alg_auth = ca_list[j]->algorithm_auth;
818 }
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 if (ca_list[j]->algorithm_enc) {
822 if (alg_enc) {
823 alg_enc &= ca_list[j]->algorithm_enc;
824 if (!alg_enc) {
825 found = 0;
826 break;
827 }
828 } else {
829 alg_enc = ca_list[j]->algorithm_enc;
830 }
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (ca_list[j]->algorithm_mac) {
834 if (alg_mac) {
835 alg_mac &= ca_list[j]->algorithm_mac;
836 if (!alg_mac) {
837 found = 0;
838 break;
839 }
840 } else {
841 alg_mac = ca_list[j]->algorithm_mac;
842 }
843 }
Adam Langley858a88d2014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 if (ca_list[j]->algo_strength) {
846 if (algo_strength) {
847 algo_strength &= ca_list[j]->algo_strength;
848 if (!algo_strength) {
849 found = 0;
850 break;
851 }
852 } else {
853 algo_strength |= ca_list[j]->algo_strength;
854 }
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (ca_list[j]->valid) {
858 /* explicit ciphersuite found; its protocol version does not become
859 * part of the search pattern! */
860 cipher_id = ca_list[j]->id;
861 } else {
862 /* not an explicit ciphersuite; only in this case, the protocol version
863 * is considered part of the search pattern. */
864 if (ca_list[j]->algorithm_ssl) {
865 if (alg_ssl) {
866 alg_ssl &= ca_list[j]->algorithm_ssl;
867 if (!alg_ssl) {
868 found = 0;
869 break;
870 }
871 } else {
872 alg_ssl = ca_list[j]->algorithm_ssl;
873 }
874 }
875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877 if (!multi) {
878 break;
879 }
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 /* Ok, we have the rule, now apply it. */
883 if (rule == CIPHER_SPECIAL) {
884 /* special command */
885 ok = 0;
886 if (buflen == 8 && !strncmp(buf, "STRENGTH", 8)) {
887 ok = ssl_cipher_strength_sort(head_p, tail_p);
888 } else {
889 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
890 SSL_R_INVALID_COMMAND);
891 }
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 if (ok == 0) {
894 retval = 0;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* We do not support any "multi" options together with "@", so throw away
898 * the rest of the command, if any left, until end or ':' is found. */
899 while (*l != '\0' && !ITEM_SEP(*l)) {
900 l++;
901 }
902 } else if (found) {
903 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
904 alg_ssl, algo_strength, rule, -1, in_group, head_p,
905 tail_p);
906 } else {
907 while (*l != '\0' && !ITEM_SEP(*l)) {
908 l++;
909 }
910 }
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 if (in_group) {
914 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
915 retval = 0;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 return retval;
919}
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921STACK_OF(SSL_CIPHER) *
922ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
923 struct ssl_cipher_preference_list_st **cipher_list,
924 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
925 const char *rule_str, CERT *c) {
926 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
927 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
928 const char *rule_p;
929 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
930 const SSL_CIPHER **ca_list = NULL;
931 uint8_t *in_group_flags = NULL;
932 unsigned int num_in_group_flags = 0;
933 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 /* Return with error if nothing to do. */
936 if (rule_str == NULL || cipher_list == NULL) {
937 return NULL;
938 }
David Benjamin5213df42014-08-20 14:19:54 -0400939
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 /* Now we have to collect the available ciphers from the compiled in ciphers.
941 * We cannot get more than the number compiled in, so it is used for
942 * allocation. */
943 num_of_ciphers = ssl_method->num_ciphers();
944 co_list =
945 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
946 if (co_list == NULL) {
947 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
948 return NULL;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 /* Now arrange all ciphers by preference:
954 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
957 * key exchange mechanisms */
David Benjamin7061e282015-03-19 11:10:48 -0400958 ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD, -1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 0, &head, &tail);
David Benjamin7061e282015-03-19 11:10:48 -0400960 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 &tail);
David Benjamin7061e282015-03-19 11:10:48 -0400962 ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head,
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700964
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
966 * CHACHA20 unless there is hardware support for fast and constant-time
967 * AES_GCM. */
968 if (EVP_has_aes_hardware()) {
969 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
970 &head, &tail);
971 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
972 &head, &tail);
973 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
974 -1, 0, &head, &tail);
975 } else {
976 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
977 -1, 0, &head, &tail);
978 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
979 &head, &tail);
980 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
981 &head, &tail);
982 }
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
985 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
986 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
987 &tail);
988 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
989 &tail);
990 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
991 &head, &tail);
992 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
993 &head, &tail);
994 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
995 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 /* Temporarily enable everything else for sorting */
998 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 /* Move ciphers without forward secrecy to the end. */
David Benjamin7061e282015-03-19 11:10:48 -04001001 ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), 0, 0, 0, 0, 0, CIPHER_ORD,
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001003
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 /* Now disable everything (maintaining the ordering!) */
1005 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 /* We also need cipher aliases for selecting based on the rule_str. There
1008 * might be two types of entries in the rule_str: 1) names of ciphers
1009 * themselves 2) aliases for groups of ciphers. For 1) we need the available
1010 * ciphers and for 2) the cipher groups of cipher_aliases added together in
1011 * one list (otherwise we would be happy with just the cipher_aliases
1012 * table). */
1013 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1014 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1015 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1016 if (ca_list == NULL) {
1017 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1018 goto err;
1019 }
1020 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 /* If the rule_string begins with DEFAULT, apply the default rule before
1023 * using the (possibly available) additional rules. */
1024 ok = 1;
1025 rule_p = rule_str;
1026 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1027 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1028 ca_list);
1029 rule_p += 7;
1030 if (*rule_p == ':') {
1031 rule_p++;
1032 }
1033 }
Adam Langley858a88d2014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 if (ok && strlen(rule_p) > 0) {
1036 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1037 }
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Nick Harper4dd053e2014-12-22 14:58:54 -08001039 OPENSSL_free((void *)ca_list); /* Not needed anymore */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (!ok) {
1042 goto err;
1043 }
1044
1045 /* Allocate new "cipherstack" for the result, return with error
1046 * if we cannot get one. */
1047 cipherstack = sk_SSL_CIPHER_new_null();
1048 if (cipherstack == NULL) {
1049 goto err;
1050 }
1051
1052 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1053 if (!in_group_flags) {
1054 goto err;
1055 }
1056
1057 /* The cipher selection for the list is done. The ciphers are added
1058 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1059 for (curr = head; curr != NULL; curr = curr->next) {
1060 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001061 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1062 goto err;
1063 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 in_group_flags[num_in_group_flags++] = curr->in_group;
1065 }
1066 }
1067 OPENSSL_free(co_list); /* Not needed any longer */
1068 co_list = NULL;
1069
1070 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1071 if (tmp_cipher_list == NULL) {
1072 goto err;
1073 }
1074 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1075 if (!pref_list) {
1076 goto err;
1077 }
1078 pref_list->ciphers = cipherstack;
1079 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1080 if (!pref_list->in_group_flags) {
1081 goto err;
1082 }
1083 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1084 OPENSSL_free(in_group_flags);
1085 in_group_flags = NULL;
1086 if (*cipher_list != NULL) {
1087 ssl_cipher_preference_list_free(*cipher_list);
1088 }
1089 *cipher_list = pref_list;
1090 pref_list = NULL;
1091
1092 if (cipher_list_by_id != NULL) {
1093 if (*cipher_list_by_id != NULL) {
1094 sk_SSL_CIPHER_free(*cipher_list_by_id);
1095 }
1096 *cipher_list_by_id = tmp_cipher_list;
1097 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001098 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001099
1100 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1101 } else {
1102 sk_SSL_CIPHER_free(tmp_cipher_list);
1103 tmp_cipher_list = NULL;
1104 }
1105
1106 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001107
1108err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 if (co_list) {
1110 OPENSSL_free(co_list);
1111 }
1112 if (in_group_flags) {
1113 OPENSSL_free(in_group_flags);
1114 }
1115 if (cipherstack) {
1116 sk_SSL_CIPHER_free(cipherstack);
1117 }
1118 if (tmp_cipher_list) {
1119 sk_SSL_CIPHER_free(tmp_cipher_list);
1120 }
1121 if (pref_list && pref_list->in_group_flags) {
1122 OPENSSL_free(pref_list->in_group_flags);
1123 }
1124 if (pref_list) {
1125 OPENSSL_free(pref_list);
1126 }
1127 return NULL;
1128}
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1131 int len) {
1132 const char *ver;
1133 const char *kx, *au, *enc, *mac;
David Benjamin107db582015-04-08 00:41:59 -04001134 uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 alg_mkey = cipher->algorithm_mkey;
1138 alg_auth = cipher->algorithm_auth;
1139 alg_enc = cipher->algorithm_enc;
1140 alg_mac = cipher->algorithm_mac;
1141 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (alg_ssl & SSL_SSLV3) {
1144 ver = "SSLv3";
1145 } else if (alg_ssl & SSL_TLSV1_2) {
1146 ver = "TLSv1.2";
1147 } else {
1148 ver = "unknown";
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 switch (alg_mkey) {
1152 case SSL_kRSA:
1153 kx = "RSA";
1154 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001155
David Benjamin7061e282015-03-19 11:10:48 -04001156 case SSL_kDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 kx = "DH";
1158 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001159
David Benjamin7061e282015-03-19 11:10:48 -04001160 case SSL_kECDHE:
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 kx = "ECDH";
1162 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 case SSL_kPSK:
1165 kx = "PSK";
1166 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 default:
1169 kx = "unknown";
1170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 switch (alg_auth) {
1173 case SSL_aRSA:
1174 au = "RSA";
1175 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 case SSL_aECDSA:
1178 au = "ECDSA";
1179 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 case SSL_aPSK:
1182 au = "PSK";
1183 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 default:
1186 au = "unknown";
1187 break;
1188 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 switch (alg_enc) {
1191 case SSL_3DES:
1192 enc = "3DES(168)";
1193 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 case SSL_RC4:
1196 enc = "RC4(128)";
1197 break;
1198
1199 case SSL_AES128:
1200 enc = "AES(128)";
1201 break;
1202
1203 case SSL_AES256:
1204 enc = "AES(256)";
1205 break;
1206
1207 case SSL_AES128GCM:
1208 enc = "AESGCM(128)";
1209 break;
1210
1211 case SSL_AES256GCM:
1212 enc = "AESGCM(256)";
1213 break;
1214
1215 case SSL_CHACHA20POLY1305:
1216 enc = "ChaCha20-Poly1305";
1217 break;
1218
1219 default:
1220 enc = "unknown";
1221 break;
1222 }
1223
1224 switch (alg_mac) {
1225 case SSL_MD5:
1226 mac = "MD5";
1227 break;
1228
1229 case SSL_SHA1:
1230 mac = "SHA1";
1231 break;
1232
1233 case SSL_SHA256:
1234 mac = "SHA256";
1235 break;
1236
1237 case SSL_SHA384:
1238 mac = "SHA384";
1239 break;
1240
1241 case SSL_AEAD:
1242 mac = "AEAD";
1243 break;
1244
1245 default:
1246 mac = "unknown";
1247 break;
1248 }
1249
1250 if (buf == NULL) {
1251 len = 128;
1252 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001253 if (buf == NULL) {
1254 return NULL;
1255 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 } else if (len < 128) {
1257 return "Buffer too small";
1258 }
1259
1260 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1261 return buf;
1262}
1263
1264int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1265 return (c->algorithm_enc & SSL_AES) != 0;
1266}
1267
1268int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1269 return (c->algorithm_mac & SSL_MD5) != 0;
1270}
1271
1272int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1273 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1274}
1275
1276int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1277 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1278}
1279
1280const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1281 int i;
1282
1283 if (c == NULL) {
1284 return "(NONE)";
1285 }
1286
1287 i = (int)(c->id >> 24L);
1288 if (i == 3) {
1289 return "TLSv1/SSLv3";
1290 } else if (i == 2) {
1291 return "SSLv2";
1292 } else {
1293 return "unknown";
1294 }
1295}
Adam Langley95c29f32014-06-20 12:00:00 -07001296
1297/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001298const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1299 if (c != NULL) {
1300 return c->name;
1301 }
1302
1303 return "(NONE)";
1304}
Adam Langley95c29f32014-06-20 12:00:00 -07001305
Adam Langleyf1df2b32014-08-11 17:37:53 -07001306const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1307 if (cipher == NULL) {
1308 return "";
1309 }
1310
1311 switch (cipher->algorithm_mkey) {
1312 case SSL_kRSA:
David Benjamin65226252015-02-05 16:49:47 -05001313 return "RSA";
Adam Langleyfcf25832014-12-18 17:42:32 -08001314
David Benjamin7061e282015-03-19 11:10:48 -04001315 case SSL_kDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001316 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001317 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001318 return "DHE_RSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001319 default:
David Benjamin65226252015-02-05 16:49:47 -05001320 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001321 return "UNKNOWN";
1322 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001323
David Benjamin7061e282015-03-19 11:10:48 -04001324 case SSL_kECDHE:
Adam Langleyf1df2b32014-08-11 17:37:53 -07001325 switch (cipher->algorithm_auth) {
1326 case SSL_aECDSA:
David Benjamin65226252015-02-05 16:49:47 -05001327 return "ECDHE_ECDSA";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001328 case SSL_aRSA:
David Benjamin65226252015-02-05 16:49:47 -05001329 return "ECDHE_RSA";
1330 case SSL_aPSK:
1331 return "ECDHE_PSK";
Adam Langleyf1df2b32014-08-11 17:37:53 -07001332 default:
David Benjamin65226252015-02-05 16:49:47 -05001333 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001334 return "UNKNOWN";
1335 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001336
David Benjamin65226252015-02-05 16:49:47 -05001337 case SSL_kPSK:
1338 assert(cipher->algorithm_auth == SSL_aPSK);
1339 return "PSK";
1340
Adam Langleyf1df2b32014-08-11 17:37:53 -07001341 default:
David Benjamin65226252015-02-05 16:49:47 -05001342 assert(0);
Adam Langleyf1df2b32014-08-11 17:37:53 -07001343 return "UNKNOWN";
1344 }
1345}
1346
David Benjamin65226252015-02-05 16:49:47 -05001347static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
1348 switch (cipher->algorithm_enc) {
1349 case SSL_3DES:
1350 return "3DES_EDE_CBC";
1351 case SSL_RC4:
1352 return "RC4";
1353 case SSL_AES128:
1354 return "AES_128_CBC";
1355 case SSL_AES256:
1356 return "AES_256_CBC";
1357 case SSL_AES128GCM:
1358 return "AES_128_GCM";
1359 case SSL_AES256GCM:
1360 return "AES_256_GCM";
1361 case SSL_CHACHA20POLY1305:
1362 return "CHACHA20_POLY1305";
1363 break;
1364 default:
1365 assert(0);
1366 return "UNKNOWN";
1367 }
1368}
1369
1370static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
1371 if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) {
1372 /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
1373 * only ever MD5 or SHA-1. */
1374 switch (cipher->algorithm_mac) {
1375 case SSL_MD5:
1376 return "MD5";
1377 case SSL_SHA1:
1378 return "SHA";
1379 default:
1380 assert(0);
1381 return "UNKNOWN";
1382 }
1383 } else if (cipher->algorithm2 & TLS1_PRF_SHA256) {
1384 return "SHA256";
1385 } else if (cipher->algorithm2 & TLS1_PRF_SHA384) {
1386 return "SHA384";
1387 } else {
1388 assert(0);
1389 return "UNKNOWN";
1390 }
1391}
1392
1393char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
1394 if (cipher == NULL) {
1395 return NULL;
1396 }
1397
1398 const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
1399 const char *enc_name = ssl_cipher_get_enc_name(cipher);
1400 const char *prf_name = ssl_cipher_get_prf_name(cipher);
1401
1402 /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
1403 size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
1404 strlen(prf_name) + 1;
1405 char *ret = OPENSSL_malloc(len);
1406 if (ret == NULL) {
1407 return NULL;
1408 }
1409 if (BUF_strlcpy(ret, "TLS_", len) >= len ||
1410 BUF_strlcat(ret, kx_name, len) >= len ||
1411 BUF_strlcat(ret, "_WITH_", len) >= len ||
1412 BUF_strlcat(ret, enc_name, len) >= len ||
1413 BUF_strlcat(ret, "_", len) >= len ||
1414 BUF_strlcat(ret, prf_name, len) >= len) {
1415 assert(0);
1416 OPENSSL_free(ret);
1417 return NULL;
1418 }
1419 assert(strlen(ret) + 1 == len);
1420 return ret;
1421}
1422
Adam Langley95c29f32014-06-20 12:00:00 -07001423/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001424int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1425 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001426
Adam Langleyfcf25832014-12-18 17:42:32 -08001427 if (c != NULL) {
1428 if (alg_bits != NULL) {
1429 *alg_bits = c->alg_bits;
1430 }
1431 ret = c->strength_bits;
1432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 return ret;
1435}
Adam Langley95c29f32014-06-20 12:00:00 -07001436
David Benjamin107db582015-04-08 00:41:59 -04001437uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
Adam Langleyfcf25832014-12-18 17:42:32 -08001439void *SSL_COMP_get_compression_methods(void) { return NULL; }
1440
1441int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1442
1443const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001444
1445/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001446int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
David Benjamin107db582015-04-08 00:41:59 -04001447 uint32_t alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001448
Adam Langleyfcf25832014-12-18 17:42:32 -08001449 if (alg_a & SSL_aECDSA) {
1450 return SSL_PKEY_ECC;
1451 } else if (alg_a & SSL_aRSA) {
1452 return SSL_PKEY_RSA_ENC;
1453 }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 return -1;
1456}
David Benjamin9c651c92014-07-12 13:27:45 -04001457
Adam Langleyfcf25832014-12-18 17:42:32 -08001458/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1459 * public key in the key exchange, sent in a server Certificate message.
1460 * Otherwise it returns 0. */
1461int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
David Benjamin32fbdf22015-04-07 01:14:06 -04001462 /* PSK-authenticated ciphers do not use a public key, except for
1463 * RSA_PSK. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 if ((cipher->algorithm_auth & SSL_aPSK) &&
1465 !(cipher->algorithm_mkey & SSL_kRSA)) {
1466 return 0;
1467 }
1468
1469 /* All other ciphers include it. */
1470 return 1;
1471}
1472
1473/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1474 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001475 *
1476 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1478 * communicate a psk_identity_hint, so it is optional. */
1479int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1480 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
David Benjamin7061e282015-03-19 11:10:48 -04001481 if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 return 1;
1483 }
1484
1485 /* It is optional in all others. */
1486 return 0;
1487}