blob: 3f164bd7877255ad0607ace27f87a73c0d71c207 [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!) */
185 {0, SSL_TXT_CMPDEF, 0, SSL_kEDH | SSL_kEECDH, SSL_aNULL, 0, 0, 0, 0, 0, 0,
186 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 {0, SSL_TXT_kEDH, 0, SSL_kEDH, 0, 0, 0, 0, 0, 0, 0, 0},
195 {0, SSL_TXT_DH, 0, SSL_kEDH, 0, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 {0, SSL_TXT_kEECDH, 0, SSL_kEECDH, 0, 0, 0, 0, 0, 0, 0, 0},
198 {0, SSL_TXT_ECDH, 0, SSL_kEECDH, 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},
204 {0, SSL_TXT_aNULL, 0, 0, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
205 {0, SSL_TXT_aECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
206 {0, SSL_TXT_ECDSA, 0, 0, SSL_aECDSA, 0, 0, 0, 0, 0, 0, 0},
207 {0, SSL_TXT_aPSK, 0, 0, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 /* aliases combining key exchange and server authentication */
210 {0, SSL_TXT_EDH, 0, SSL_kEDH, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
211 {0, SSL_TXT_EECDH, 0, SSL_kEECDH, ~SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
212 {0, SSL_TXT_RSA, 0, SSL_kRSA, SSL_aRSA, 0, 0, 0, 0, 0, 0, 0},
213 {0, SSL_TXT_ADH, 0, SSL_kEDH, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
214 {0, SSL_TXT_AECDH, 0, SSL_kEECDH, SSL_aNULL, 0, 0, 0, 0, 0, 0, 0},
215 {0, SSL_TXT_PSK, 0, SSL_kPSK, SSL_aPSK, 0, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 /* symmetric encryption aliases */
218 {0, SSL_TXT_3DES, 0, 0, 0, SSL_3DES, 0, 0, 0, 0, 0, 0},
219 {0, SSL_TXT_RC4, 0, 0, 0, SSL_RC4, 0, 0, 0, 0, 0, 0},
220 {0, SSL_TXT_AES128, 0, 0, 0, SSL_AES128 | SSL_AES128GCM, 0, 0, 0, 0, 0, 0},
221 {0, SSL_TXT_AES256, 0, 0, 0, SSL_AES256 | SSL_AES256GCM, 0, 0, 0, 0, 0, 0},
222 {0, SSL_TXT_AES, 0, 0, 0, SSL_AES, 0, 0, 0, 0, 0, 0},
223 {0, SSL_TXT_AES_GCM, 0, 0, 0, SSL_AES128GCM | SSL_AES256GCM, 0, 0, 0, 0, 0,
224 0},
225 {0, SSL_TXT_CHACHA20, 0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* MAC aliases */
228 {0, SSL_TXT_MD5, 0, 0, 0, 0, SSL_MD5, 0, 0, 0, 0, 0},
229 {0, SSL_TXT_SHA1, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
230 {0, SSL_TXT_SHA, 0, 0, 0, 0, SSL_SHA1, 0, 0, 0, 0, 0},
231 {0, SSL_TXT_SHA256, 0, 0, 0, 0, SSL_SHA256, 0, 0, 0, 0, 0},
232 {0, SSL_TXT_SHA384, 0, 0, 0, 0, SSL_SHA384, 0, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* protocol version aliases */
235 {0, SSL_TXT_SSLV3, 0, 0, 0, 0, 0, SSL_SSLV3, 0, 0, 0, 0},
236 {0, SSL_TXT_TLSV1, 0, 0, 0, 0, 0, SSL_TLSV1, 0, 0, 0, 0},
237 {0, SSL_TXT_TLSV1_2, 0, 0, 0, 0, 0, SSL_TLSV1_2, 0, 0, 0, 0},
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 /* strength classes */
240 {0, SSL_TXT_MEDIUM, 0, 0, 0, 0, 0, 0, SSL_MEDIUM, 0, 0, 0},
241 {0, SSL_TXT_HIGH, 0, 0, 0, 0, 0, 0, SSL_HIGH, 0, 0, 0},
242 /* FIPS 140-2 approved ciphersuite */
243 {0, SSL_TXT_FIPS, 0, 0, 0, 0, 0, 0, SSL_FIPS, 0, 0, 0},
244};
Adam Langley95c29f32014-06-20 12:00:00 -0700245
David Benjaminea72bd02014-12-21 21:27:41 -0500246int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
247 size_t *out_mac_secret_len,
248 size_t *out_fixed_iv_len,
249 const SSL_CIPHER *cipher, uint16_t version) {
250 *out_aead = NULL;
251 *out_mac_secret_len = 0;
252 *out_fixed_iv_len = 0;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700253
David Benjaminea72bd02014-12-21 21:27:41 -0500254 switch (cipher->algorithm_enc) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 case SSL_AES128GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500256 *out_aead = EVP_aead_aes_128_gcm();
257 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 return 1;
259
260 case SSL_AES256GCM:
David Benjaminea72bd02014-12-21 21:27:41 -0500261 *out_aead = EVP_aead_aes_256_gcm();
262 *out_fixed_iv_len = 4;
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 return 1;
264
265 case SSL_CHACHA20POLY1305:
David Benjaminea72bd02014-12-21 21:27:41 -0500266 *out_aead = EVP_aead_chacha20_poly1305();
267 *out_fixed_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269
270 case SSL_RC4:
David Benjaminea72bd02014-12-21 21:27:41 -0500271 switch (cipher->algorithm_mac) {
272 case SSL_MD5:
David Benjamin044abb02014-12-23 10:57:17 -0500273 if (version == SSL3_VERSION) {
274 *out_aead = EVP_aead_rc4_md5_ssl3();
275 } else {
276 *out_aead = EVP_aead_rc4_md5_tls();
277 }
David Benjaminea72bd02014-12-21 21:27:41 -0500278 *out_mac_secret_len = MD5_DIGEST_LENGTH;
279 return 1;
280 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500281 if (version == SSL3_VERSION) {
282 *out_aead = EVP_aead_rc4_sha1_ssl3();
283 } else {
284 *out_aead = EVP_aead_rc4_sha1_tls();
285 }
David Benjaminea72bd02014-12-21 21:27:41 -0500286 *out_mac_secret_len = SHA_DIGEST_LENGTH;
287 return 1;
288 default:
289 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800291
David Benjaminea72bd02014-12-21 21:27:41 -0500292 case SSL_AES128:
293 switch (cipher->algorithm_mac) {
294 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500295 if (version == SSL3_VERSION) {
296 *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
297 *out_fixed_iv_len = 16;
298 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500299 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
300 *out_fixed_iv_len = 16;
301 } else {
302 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
303 }
304 *out_mac_secret_len = SHA_DIGEST_LENGTH;
305 return 1;
306 case SSL_SHA256:
307 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
308 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
309 return 1;
310 default:
311 return 0;
312 }
313
314 case SSL_AES256:
315 switch (cipher->algorithm_mac) {
316 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500317 if (version == SSL3_VERSION) {
318 *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
319 *out_fixed_iv_len = 16;
320 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500321 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
322 *out_fixed_iv_len = 16;
323 } else {
324 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
325 }
326 *out_mac_secret_len = SHA_DIGEST_LENGTH;
327 return 1;
328 case SSL_SHA256:
329 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
330 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
331 return 1;
332 case SSL_SHA384:
333 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
334 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
335 return 1;
336 default:
337 return 0;
338 }
339
340 case SSL_3DES:
341 switch (cipher->algorithm_mac) {
342 case SSL_SHA1:
David Benjamin044abb02014-12-23 10:57:17 -0500343 if (version == SSL3_VERSION) {
344 *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
345 *out_fixed_iv_len = 8;
346 } else if (version == TLS1_VERSION) {
David Benjaminea72bd02014-12-21 21:27:41 -0500347 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
348 *out_fixed_iv_len = 8;
349 } else {
350 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
351 }
352 *out_mac_secret_len = SHA_DIGEST_LENGTH;
353 return 1;
354 default:
355 return 0;
356 }
357
358 default:
359 return 0;
360 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700362
363int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 const EVP_MD **md, int *mac_pkey_type,
David Benjaminea72bd02014-12-21 21:27:41 -0500365 size_t *mac_secret_size) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 c = s->cipher;
369 if (c == NULL ||
370 /* This function doesn't deal with EVP_AEAD. See
371 * |ssl_cipher_get_aead_evp|. */
372 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) ||
373 enc == NULL ||
374 md == NULL) {
375 return 0;
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 switch (c->algorithm_enc) {
379 case SSL_3DES:
380 *enc = EVP_des_ede3_cbc();
381 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 case SSL_RC4:
384 *enc = EVP_rc4();
385 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 case SSL_AES128:
388 *enc = EVP_aes_128_cbc();
389 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 case SSL_AES256:
392 *enc = EVP_aes_256_cbc();
393 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 default:
396 return 0;
397 }
David Benjamind633d632014-08-30 14:43:27 -0400398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size)) {
400 return 0;
401 }
402
403 assert(*enc != NULL && *md != NULL);
404
405/* TODO(fork): enable the stitched cipher modes. */
David Benjamind633d632014-08-30 14:43:27 -0400406#if 0
Adam Langley95c29f32014-06-20 12:00:00 -0700407 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
408 s->ssl_version < TLS1_VERSION)
409 return 1;
410
Adam Langley95c29f32014-06-20 12:00:00 -0700411 if (c->algorithm_enc == SSL_RC4 &&
412 c->algorithm_mac == SSL_MD5 &&
413 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
414 *enc = evp, *md = NULL;
415 else if (c->algorithm_enc == SSL_AES128 &&
416 c->algorithm_mac == SSL_SHA1 &&
417 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
418 *enc = evp, *md = NULL;
419 else if (c->algorithm_enc == SSL_AES256 &&
420 c->algorithm_mac == SSL_SHA1 &&
421 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
422 *enc = evp, *md = NULL;
423#endif
David Benjamind633d632014-08-30 14:43:27 -0400424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 return 1;
426}
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md,
David Benjaminea72bd02014-12-21 21:27:41 -0500429 int *mac_pkey_type, size_t *mac_secret_size) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 const SSL_CIPHER *c;
Adam Langley9447dff2014-06-24 17:29:06 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 c = s->cipher;
433 if (c == NULL) {
434 return 0;
435 }
Adam Langley9447dff2014-06-24 17:29:06 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 switch (c->algorithm_mac) {
438 case SSL_MD5:
439 *md = EVP_md5();
440 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 case SSL_SHA1:
443 *md = EVP_sha1();
444 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 case SSL_SHA256:
447 *md = EVP_sha256();
448 break;
449
450 case SSL_SHA384:
451 *md = EVP_sha384();
452 break;
453
454 default:
455 return 0;
456 }
457
458 if (mac_pkey_type != NULL) {
459 *mac_pkey_type = EVP_PKEY_HMAC;
460 }
461 if (mac_secret_size != NULL) {
462 *mac_secret_size = EVP_MD_size(*md);
463 }
464
465 return 1;
466}
467
David Benjamin1f5e1152014-12-23 09:23:32 -0500468int ssl_get_handshake_digest(size_t idx, long *mask, const EVP_MD **md) {
469 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 return 0;
471 }
472 *mask = ssl_handshake_digests[idx].mask;
473 *md = ssl_handshake_digests[idx].md_func();
474 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700475}
476
477#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700479
480static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 CIPHER_ORDER **tail) {
482 if (curr == *tail) {
483 return;
484 }
485 if (curr == *head) {
486 *head = curr->next;
487 }
488 if (curr->prev != NULL) {
489 curr->prev->next = curr->next;
490 }
491 if (curr->next != NULL) {
492 curr->next->prev = curr->prev;
493 }
494 (*tail)->next = curr;
495 curr->prev = *tail;
496 curr->next = NULL;
497 *tail = curr;
498}
Adam Langley95c29f32014-06-20 12:00:00 -0700499
500static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 CIPHER_ORDER **tail) {
502 if (curr == *head) {
503 return;
504 }
505 if (curr == *tail) {
506 *tail = curr->prev;
507 }
508 if (curr->next != NULL) {
509 curr->next->prev = curr->prev;
510 }
511 if (curr->prev != NULL) {
512 curr->prev->next = curr->next;
513 }
514 (*head)->prev = curr;
515 curr->next = *head;
516 curr->prev = NULL;
517 *head = curr;
518}
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin82c9e902014-12-12 15:55:27 -0500520static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 int num_of_ciphers,
522 CIPHER_ORDER *co_list,
523 CIPHER_ORDER **head_p,
524 CIPHER_ORDER **tail_p) {
525 int i, co_list_num;
526 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* We have num_of_ciphers descriptions compiled in, depending on the method
529 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
530 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 /* Get the initial list of ciphers */
533 co_list_num = 0; /* actual count of ciphers */
534 for (i = 0; i < num_of_ciphers; i++) {
535 c = ssl_method->get_cipher(i);
536 /* drop those that use any of that is not available */
537 if (c != NULL && c->valid) {
538 co_list[co_list_num].cipher = c;
539 co_list[co_list_num].next = NULL;
540 co_list[co_list_num].prev = NULL;
541 co_list[co_list_num].active = 0;
542 co_list[co_list_num].in_group = 0;
543 co_list_num++;
544 }
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 /* Prepare linked list from list entries. */
548 if (co_list_num > 0) {
549 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 if (co_list_num > 1) {
552 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 for (i = 1; i < co_list_num - 1; i++) {
555 co_list[i].prev = &co_list[i - 1];
556 co_list[i].next = &co_list[i + 1];
557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
560 }
561
562 co_list[co_list_num - 1].next = NULL;
563
564 *head_p = &co_list[0];
565 *tail_p = &co_list[co_list_num - 1];
566 }
567}
Adam Langley95c29f32014-06-20 12:00:00 -0700568
569static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 int num_of_group_aliases,
571 CIPHER_ORDER *head) {
572 CIPHER_ORDER *ciph_curr;
573 const SSL_CIPHER **ca_curr;
574 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 /* First, add the real ciphers as already collected. */
577 ciph_curr = head;
578 ca_curr = ca_list;
579 while (ciph_curr != NULL) {
580 *ca_curr = ciph_curr->cipher;
581 ca_curr++;
582 ciph_curr = ciph_curr->next;
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 /* Now we add the available ones from the cipher_aliases[] table. They
586 * represent either one or more algorithms, some of which in any affected
587 * category must be supported (set in enabled_mask), or represent a cipher
588 * strength value (will be added in any case because algorithms=0). */
589 for (i = 0; i < num_of_group_aliases; i++) {
590 *ca_curr = cipher_aliases + i;
591 ca_curr++;
592 }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 *ca_curr = NULL; /* end of list */
595}
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597static void ssl_cipher_apply_rule(
598 unsigned long cipher_id, unsigned long alg_mkey, unsigned long alg_auth,
599 unsigned long alg_enc, unsigned long alg_mac, unsigned long alg_ssl,
600 unsigned long algo_strength, int rule, int strength_bits, int in_group,
601 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
602 CIPHER_ORDER *head, *tail, *curr, *next, *last;
603 const SSL_CIPHER *cp;
604 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 if (rule == CIPHER_DEL) {
607 /* needed to maintain sorting between currently deleted ciphers */
608 reverse = 1;
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 head = *head_p;
612 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 if (reverse) {
615 next = tail;
616 last = head;
617 } else {
618 next = head;
619 last = tail;
620 }
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 curr = NULL;
623 for (;;) {
624 if (curr == last) {
625 break;
626 }
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 curr = next;
629 if (curr == NULL) {
630 break;
631 }
Adam Langleye3142a72014-07-24 17:56:48 -0700632
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 next = reverse ? curr->prev : curr->next;
634 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 /* Selection criteria is either the value of strength_bits
637 * or the algorithms used. */
638 if (strength_bits >= 0) {
639 if (strength_bits != cp->strength_bits) {
640 continue;
641 }
642 } else {
643 if ((alg_mkey && !(alg_mkey & cp->algorithm_mkey)) ||
644 (alg_auth && !(alg_auth & cp->algorithm_auth)) ||
645 (alg_enc && !(alg_enc & cp->algorithm_enc)) ||
646 (alg_mac && !(alg_mac & cp->algorithm_mac)) ||
647 (alg_ssl && !(alg_ssl & cp->algorithm_ssl)) ||
648 (algo_strength && !(algo_strength & cp->algo_strength))) {
649 continue;
650 }
651 }
Adam Langleye3142a72014-07-24 17:56:48 -0700652
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 /* add the cipher if it has not been added yet. */
654 if (rule == CIPHER_ADD) {
655 /* reverse == 0 */
656 if (!curr->active) {
657 ll_append_tail(&head, curr, &tail);
658 curr->active = 1;
659 curr->in_group = in_group;
660 }
661 }
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 /* Move the added cipher to this location */
664 else if (rule == CIPHER_ORD) {
665 /* reverse == 0 */
666 if (curr->active) {
667 ll_append_tail(&head, curr, &tail);
668 curr->in_group = 0;
669 }
670 } else if (rule == CIPHER_DEL) {
671 /* reverse == 1 */
672 if (curr->active) {
673 /* most recently deleted ciphersuites get best positions
674 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
675 * works in reverse to maintain the order) */
676 ll_append_head(&head, curr, &tail);
677 curr->active = 0;
678 curr->in_group = 0;
679 }
680 } else if (rule == CIPHER_KILL) {
681 /* reverse == 0 */
682 if (head == curr) {
683 head = curr->next;
684 } else {
685 curr->prev->next = curr->next;
686 }
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 if (tail == curr) {
689 tail = curr->prev;
690 }
691 curr->active = 0;
692 if (curr->next != NULL) {
693 curr->next->prev = curr->prev;
694 }
695 if (curr->prev != NULL) {
696 curr->prev->next = curr->next;
697 }
698 curr->next = NULL;
699 curr->prev = NULL;
700 }
701 }
Adam Langley95c29f32014-06-20 12:00:00 -0700702
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 *head_p = head;
704 *tail_p = tail;
705}
Adam Langley95c29f32014-06-20 12:00:00 -0700706
707static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 CIPHER_ORDER **tail_p) {
709 int max_strength_bits, i, *number_uses;
710 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langleyfcf25832014-12-18 17:42:32 -0800712 /* This routine sorts the ciphers with descending strength. The sorting must
713 * keep the pre-sorted sequence, so we apply the normal sorting routine as
714 * '+' movement to the end of the list. */
715 max_strength_bits = 0;
716 curr = *head_p;
717 while (curr != NULL) {
718 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
719 max_strength_bits = curr->cipher->strength_bits;
720 }
721 curr = curr->next;
722 }
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
725 if (!number_uses) {
726 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
727 return 0;
728 }
729 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 /* Now find the strength_bits values actually used. */
732 curr = *head_p;
733 while (curr != NULL) {
734 if (curr->active) {
735 number_uses[curr->cipher->strength_bits]++;
736 }
737 curr = curr->next;
738 }
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 /* Go through the list of used strength_bits values in descending order. */
741 for (i = max_strength_bits; i >= 0; i--) {
742 if (number_uses[i] > 0) {
743 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
744 tail_p);
745 }
746 }
747
748 OPENSSL_free(number_uses);
749 return 1;
750}
Adam Langley95c29f32014-06-20 12:00:00 -0700751
752static int ssl_cipher_process_rulestr(const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 CIPHER_ORDER **head_p,
754 CIPHER_ORDER **tail_p,
755 const SSL_CIPHER **ca_list) {
756 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
757 const char *l, *buf;
758 int j, multi, found, rule, retval, ok, buflen, in_group = 0, has_group = 0;
759 unsigned long cipher_id = 0;
760 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 retval = 1;
763 l = rule_str;
764 for (;;) {
765 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 if (ch == '\0') {
768 break; /* done */
769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 if (in_group) {
772 if (ch == ']') {
773 if (!in_group) {
774 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
775 SSL_R_UNEXPECTED_GROUP_CLOSE);
776 retval = found = in_group = 0;
777 break;
778 }
779 if (*tail_p) {
780 (*tail_p)->in_group = 0;
781 }
782 in_group = 0;
783 l++;
784 continue;
785 }
David Benjamin37d92462014-09-20 17:54:24 -0400786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 if (ch == '|') {
788 rule = CIPHER_ADD;
789 l++;
790 continue;
791 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
792 !(ch >= '0' && ch <= '9')) {
793 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
794 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
795 retval = found = in_group = 0;
796 break;
797 } else {
798 rule = CIPHER_ADD;
799 }
800 } else if (ch == '-') {
801 rule = CIPHER_DEL;
802 l++;
803 } else if (ch == '+') {
804 rule = CIPHER_ORD;
805 l++;
806 } else if (ch == '!') {
807 rule = CIPHER_KILL;
808 l++;
809 } else if (ch == '@') {
810 rule = CIPHER_SPECIAL;
811 l++;
812 } else if (ch == '[') {
813 if (in_group) {
814 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
815 retval = found = in_group = 0;
816 break;
817 }
818 in_group = 1;
819 has_group = 1;
820 l++;
821 continue;
822 } else {
823 rule = CIPHER_ADD;
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 /* If preference groups are enabled, the only legal operator is +.
827 * Otherwise the in_group bits will get mixed up. */
828 if (has_group && rule != CIPHER_ADD) {
829 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
830 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
831 retval = found = in_group = 0;
832 break;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 if (ITEM_SEP(ch)) {
836 l++;
837 continue;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 alg_mkey = 0;
841 alg_auth = 0;
842 alg_enc = 0;
843 alg_mac = 0;
844 alg_ssl = 0;
845 algo_strength = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 for (;;) {
848 ch = *l;
849 buf = l;
850 buflen = 0;
851 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
852 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
853 ch = *(++l);
854 buflen++;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (buflen == 0) {
858 /* We hit something we cannot deal with, it is no command or separator
859 * nor alphanumeric, so we call this an error. */
860 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
861 SSL_R_INVALID_COMMAND);
862 retval = found = in_group = 0;
863 l++;
864 break;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if (rule == CIPHER_SPECIAL) {
868 found = 0; /* unused -- avoid compiler warning */
869 break; /* special treatment */
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 /* check for multi-part specification */
873 if (ch == '+') {
874 multi = 1;
875 l++;
876 } else {
877 multi = 0;
878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 /* Now search for the cipher alias in the ca_list. Be careful with the
881 * strncmp, because the "buflen" limitation will make the rule "ADH:SOME"
882 * and the cipher "ADH-MY-CIPHER" look like a match for buflen=3. So
883 * additionally check whether the cipher name found has the correct
884 * length. We can save a strlen() call: just checking for the '\0' at the
885 * right place is sufficient, we have to strncmp() anyway. (We cannot use
886 * strcmp(), because buf is not '\0' terminated.) */
887 j = found = 0;
888 cipher_id = 0;
889 while (ca_list[j]) {
890 if (!strncmp(buf, ca_list[j]->name, buflen) &&
891 (ca_list[j]->name[buflen] == '\0')) {
892 found = 1;
893 break;
894 } else {
895 j++;
896 }
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 if (!found) {
900 break; /* ignore this entry */
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 if (ca_list[j]->algorithm_mkey) {
904 if (alg_mkey) {
905 alg_mkey &= ca_list[j]->algorithm_mkey;
906 if (!alg_mkey) {
907 found = 0;
908 break;
909 }
910 } else {
911 alg_mkey = ca_list[j]->algorithm_mkey;
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 if (ca_list[j]->algorithm_auth) {
916 if (alg_auth) {
917 alg_auth &= ca_list[j]->algorithm_auth;
918 if (!alg_auth) {
919 found = 0;
920 break;
921 }
922 } else {
923 alg_auth = ca_list[j]->algorithm_auth;
924 }
925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (ca_list[j]->algorithm_enc) {
928 if (alg_enc) {
929 alg_enc &= ca_list[j]->algorithm_enc;
930 if (!alg_enc) {
931 found = 0;
932 break;
933 }
934 } else {
935 alg_enc = ca_list[j]->algorithm_enc;
936 }
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (ca_list[j]->algorithm_mac) {
940 if (alg_mac) {
941 alg_mac &= ca_list[j]->algorithm_mac;
942 if (!alg_mac) {
943 found = 0;
944 break;
945 }
946 } else {
947 alg_mac = ca_list[j]->algorithm_mac;
948 }
949 }
Adam Langley858a88d2014-06-20 12:00:00 -0700950
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 if (ca_list[j]->algo_strength) {
952 if (algo_strength) {
953 algo_strength &= ca_list[j]->algo_strength;
954 if (!algo_strength) {
955 found = 0;
956 break;
957 }
958 } else {
959 algo_strength |= ca_list[j]->algo_strength;
960 }
961 }
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 if (ca_list[j]->valid) {
964 /* explicit ciphersuite found; its protocol version does not become
965 * part of the search pattern! */
966 cipher_id = ca_list[j]->id;
967 } else {
968 /* not an explicit ciphersuite; only in this case, the protocol version
969 * is considered part of the search pattern. */
970 if (ca_list[j]->algorithm_ssl) {
971 if (alg_ssl) {
972 alg_ssl &= ca_list[j]->algorithm_ssl;
973 if (!alg_ssl) {
974 found = 0;
975 break;
976 }
977 } else {
978 alg_ssl = ca_list[j]->algorithm_ssl;
979 }
980 }
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 if (!multi) {
984 break;
985 }
986 }
Adam Langley95c29f32014-06-20 12:00:00 -0700987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 /* Ok, we have the rule, now apply it. */
989 if (rule == CIPHER_SPECIAL) {
990 /* special command */
991 ok = 0;
992 if (buflen == 8 && !strncmp(buf, "STRENGTH", 8)) {
993 ok = ssl_cipher_strength_sort(head_p, tail_p);
994 } else {
995 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
996 SSL_R_INVALID_COMMAND);
997 }
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 if (ok == 0) {
1000 retval = 0;
1001 }
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 /* We do not support any "multi" options together with "@", so throw away
1004 * the rest of the command, if any left, until end or ':' is found. */
1005 while (*l != '\0' && !ITEM_SEP(*l)) {
1006 l++;
1007 }
1008 } else if (found) {
1009 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
1010 alg_ssl, algo_strength, rule, -1, in_group, head_p,
1011 tail_p);
1012 } else {
1013 while (*l != '\0' && !ITEM_SEP(*l)) {
1014 l++;
1015 }
1016 }
1017 }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 if (in_group) {
1020 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1021 retval = 0;
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 return retval;
1025}
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027STACK_OF(SSL_CIPHER) *
1028ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
1029 struct ssl_cipher_preference_list_st **cipher_list,
1030 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
1031 const char *rule_str, CERT *c) {
1032 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1033 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1034 const char *rule_p;
1035 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1036 const SSL_CIPHER **ca_list = NULL;
1037 uint8_t *in_group_flags = NULL;
1038 unsigned int num_in_group_flags = 0;
1039 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 /* Return with error if nothing to do. */
1042 if (rule_str == NULL || cipher_list == NULL) {
1043 return NULL;
1044 }
David Benjamin5213df42014-08-20 14:19:54 -04001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 /* Now we have to collect the available ciphers from the compiled in ciphers.
1047 * We cannot get more than the number compiled in, so it is used for
1048 * allocation. */
1049 num_of_ciphers = ssl_method->num_ciphers();
1050 co_list =
1051 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1052 if (co_list == NULL) {
1053 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1054 return NULL;
1055 }
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 /* Now arrange all ciphers by preference:
1060 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1063 * key exchange mechanisms */
1064 ssl_cipher_apply_rule(0, SSL_kEECDH, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD, -1,
1065 0, &head, &tail);
1066 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1067 &tail);
1068 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head,
1069 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001070
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1072 * CHACHA20 unless there is hardware support for fast and constant-time
1073 * AES_GCM. */
1074 if (EVP_has_aes_hardware()) {
1075 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1076 &head, &tail);
1077 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1078 &head, &tail);
1079 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
1080 -1, 0, &head, &tail);
1081 } else {
1082 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
1083 -1, 0, &head, &tail);
1084 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1085 &head, &tail);
1086 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1087 &head, &tail);
1088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1091 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
1092 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1093 &tail);
1094 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1095 &tail);
1096 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
1097 &head, &tail);
1098 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
1099 &head, &tail);
1100 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1101 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 /* Temporarily enable everything else for sorting */
1104 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 /* Move ciphers without forward secrecy to the end. */
1107 ssl_cipher_apply_rule(0, ~(SSL_kEDH | SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD,
1108 -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1111 * (For applications that allow them, they aren't too bad, but we prefer
1112 * authenticated ciphers.)
1113 * TODO(davidben): Remove them altogether? */
1114 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head,
1115 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001116
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 /* Now disable everything (maintaining the ordering!) */
1118 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 /* We also need cipher aliases for selecting based on the rule_str. There
1121 * might be two types of entries in the rule_str: 1) names of ciphers
1122 * themselves 2) aliases for groups of ciphers. For 1) we need the available
1123 * ciphers and for 2) the cipher groups of cipher_aliases added together in
1124 * one list (otherwise we would be happy with just the cipher_aliases
1125 * table). */
1126 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1127 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1128 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1129 if (ca_list == NULL) {
1130 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1131 goto err;
1132 }
1133 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langleyfcf25832014-12-18 17:42:32 -08001135 /* If the rule_string begins with DEFAULT, apply the default rule before
1136 * using the (possibly available) additional rules. */
1137 ok = 1;
1138 rule_p = rule_str;
1139 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1140 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1141 ca_list);
1142 rule_p += 7;
1143 if (*rule_p == ':') {
1144 rule_p++;
1145 }
1146 }
Adam Langley858a88d2014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 if (ok && strlen(rule_p) > 0) {
1149 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1150 }
Adam Langley95c29f32014-06-20 12:00:00 -07001151
Nick Harper4dd053e2014-12-22 14:58:54 -08001152 OPENSSL_free((void *)ca_list); /* Not needed anymore */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 if (!ok) {
1155 goto err;
1156 }
1157
1158 /* Allocate new "cipherstack" for the result, return with error
1159 * if we cannot get one. */
1160 cipherstack = sk_SSL_CIPHER_new_null();
1161 if (cipherstack == NULL) {
1162 goto err;
1163 }
1164
1165 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1166 if (!in_group_flags) {
1167 goto err;
1168 }
1169
1170 /* The cipher selection for the list is done. The ciphers are added
1171 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1172 for (curr = head; curr != NULL; curr = curr->next) {
1173 if (curr->active) {
1174 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1175 in_group_flags[num_in_group_flags++] = curr->in_group;
1176 }
1177 }
1178 OPENSSL_free(co_list); /* Not needed any longer */
1179 co_list = NULL;
1180
1181 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1182 if (tmp_cipher_list == NULL) {
1183 goto err;
1184 }
1185 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1186 if (!pref_list) {
1187 goto err;
1188 }
1189 pref_list->ciphers = cipherstack;
1190 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1191 if (!pref_list->in_group_flags) {
1192 goto err;
1193 }
1194 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1195 OPENSSL_free(in_group_flags);
1196 in_group_flags = NULL;
1197 if (*cipher_list != NULL) {
1198 ssl_cipher_preference_list_free(*cipher_list);
1199 }
1200 *cipher_list = pref_list;
1201 pref_list = NULL;
1202
1203 if (cipher_list_by_id != NULL) {
1204 if (*cipher_list_by_id != NULL) {
1205 sk_SSL_CIPHER_free(*cipher_list_by_id);
1206 }
1207 *cipher_list_by_id = tmp_cipher_list;
1208 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001209 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001210
1211 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1212 } else {
1213 sk_SSL_CIPHER_free(tmp_cipher_list);
1214 tmp_cipher_list = NULL;
1215 }
1216
1217 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001218
1219err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 if (co_list) {
1221 OPENSSL_free(co_list);
1222 }
1223 if (in_group_flags) {
1224 OPENSSL_free(in_group_flags);
1225 }
1226 if (cipherstack) {
1227 sk_SSL_CIPHER_free(cipherstack);
1228 }
1229 if (tmp_cipher_list) {
1230 sk_SSL_CIPHER_free(tmp_cipher_list);
1231 }
1232 if (pref_list && pref_list->in_group_flags) {
1233 OPENSSL_free(pref_list->in_group_flags);
1234 }
1235 if (pref_list) {
1236 OPENSSL_free(pref_list);
1237 }
1238 return NULL;
1239}
Adam Langley95c29f32014-06-20 12:00:00 -07001240
Adam Langleyfcf25832014-12-18 17:42:32 -08001241const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1242 int len) {
1243 const char *ver;
1244 const char *kx, *au, *enc, *mac;
1245 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1246 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001247
Adam Langleyfcf25832014-12-18 17:42:32 -08001248 alg_mkey = cipher->algorithm_mkey;
1249 alg_auth = cipher->algorithm_auth;
1250 alg_enc = cipher->algorithm_enc;
1251 alg_mac = cipher->algorithm_mac;
1252 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001253
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 if (alg_ssl & SSL_SSLV3) {
1255 ver = "SSLv3";
1256 } else if (alg_ssl & SSL_TLSV1_2) {
1257 ver = "TLSv1.2";
1258 } else {
1259 ver = "unknown";
1260 }
Adam Langley95c29f32014-06-20 12:00:00 -07001261
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 switch (alg_mkey) {
1263 case SSL_kRSA:
1264 kx = "RSA";
1265 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001266
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 case SSL_kEDH:
1268 kx = "DH";
1269 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 case SSL_kEECDH:
1272 kx = "ECDH";
1273 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001274
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 case SSL_kPSK:
1276 kx = "PSK";
1277 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 default:
1280 kx = "unknown";
1281 }
Adam Langley95c29f32014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 switch (alg_auth) {
1284 case SSL_aRSA:
1285 au = "RSA";
1286 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 case SSL_aNULL:
1289 au = "None";
1290 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001291
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 case SSL_aECDSA:
1293 au = "ECDSA";
1294 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001295
Adam Langleyfcf25832014-12-18 17:42:32 -08001296 case SSL_aPSK:
1297 au = "PSK";
1298 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 default:
1301 au = "unknown";
1302 break;
1303 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001304
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 switch (alg_enc) {
1306 case SSL_3DES:
1307 enc = "3DES(168)";
1308 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 case SSL_RC4:
1311 enc = "RC4(128)";
1312 break;
1313
1314 case SSL_AES128:
1315 enc = "AES(128)";
1316 break;
1317
1318 case SSL_AES256:
1319 enc = "AES(256)";
1320 break;
1321
1322 case SSL_AES128GCM:
1323 enc = "AESGCM(128)";
1324 break;
1325
1326 case SSL_AES256GCM:
1327 enc = "AESGCM(256)";
1328 break;
1329
1330 case SSL_CHACHA20POLY1305:
1331 enc = "ChaCha20-Poly1305";
1332 break;
1333
1334 default:
1335 enc = "unknown";
1336 break;
1337 }
1338
1339 switch (alg_mac) {
1340 case SSL_MD5:
1341 mac = "MD5";
1342 break;
1343
1344 case SSL_SHA1:
1345 mac = "SHA1";
1346 break;
1347
1348 case SSL_SHA256:
1349 mac = "SHA256";
1350 break;
1351
1352 case SSL_SHA384:
1353 mac = "SHA384";
1354 break;
1355
1356 case SSL_AEAD:
1357 mac = "AEAD";
1358 break;
1359
1360 default:
1361 mac = "unknown";
1362 break;
1363 }
1364
1365 if (buf == NULL) {
1366 len = 128;
1367 buf = OPENSSL_malloc(len);
1368 if (buf == NULL)
1369 return "OPENSSL_malloc Error";
1370 } else if (len < 128) {
1371 return "Buffer too small";
1372 }
1373
1374 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1375 return buf;
1376}
1377
1378int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1379 return (c->algorithm_enc & SSL_AES) != 0;
1380}
1381
1382int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1383 return (c->algorithm_mac & SSL_MD5) != 0;
1384}
1385
1386int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1387 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1388}
1389
1390int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1391 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1392}
1393
1394const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1395 int i;
1396
1397 if (c == NULL) {
1398 return "(NONE)";
1399 }
1400
1401 i = (int)(c->id >> 24L);
1402 if (i == 3) {
1403 return "TLSv1/SSLv3";
1404 } else if (i == 2) {
1405 return "SSLv2";
1406 } else {
1407 return "unknown";
1408 }
1409}
Adam Langley95c29f32014-06-20 12:00:00 -07001410
1411/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001412const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1413 if (c != NULL) {
1414 return c->name;
1415 }
1416
1417 return "(NONE)";
1418}
Adam Langley95c29f32014-06-20 12:00:00 -07001419
Adam Langleyf1df2b32014-08-11 17:37:53 -07001420const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1421 if (cipher == NULL) {
1422 return "";
1423 }
1424
1425 switch (cipher->algorithm_mkey) {
1426 case SSL_kRSA:
1427 return SSL_TXT_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001428
Adam Langleyf1df2b32014-08-11 17:37:53 -07001429 case SSL_kEDH:
1430 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001431 case SSL_aRSA:
1432 return "DHE_" SSL_TXT_RSA;
1433 case SSL_aNULL:
1434 return SSL_TXT_DH "_anon";
1435 default:
1436 return "UNKNOWN";
1437 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001438
Adam Langleyf1df2b32014-08-11 17:37:53 -07001439 case SSL_kEECDH:
1440 switch (cipher->algorithm_auth) {
1441 case SSL_aECDSA:
1442 return "ECDHE_" SSL_TXT_ECDSA;
1443 case SSL_aRSA:
1444 return "ECDHE_" SSL_TXT_RSA;
1445 case SSL_aNULL:
1446 return SSL_TXT_ECDH "_anon";
1447 default:
1448 return "UNKNOWN";
1449 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001450
Adam Langleyf1df2b32014-08-11 17:37:53 -07001451 default:
1452 return "UNKNOWN";
1453 }
1454}
1455
Adam Langley95c29f32014-06-20 12:00:00 -07001456/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001457int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1458 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001459
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 if (c != NULL) {
1461 if (alg_bits != NULL) {
1462 *alg_bits = c->alg_bits;
1463 }
1464 ret = c->strength_bits;
1465 }
Adam Langley95c29f32014-06-20 12:00:00 -07001466
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 return ret;
1468}
Adam Langley95c29f32014-06-20 12:00:00 -07001469
Adam Langleyfcf25832014-12-18 17:42:32 -08001470unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001471
Adam Langleyfcf25832014-12-18 17:42:32 -08001472void *SSL_COMP_get_compression_methods(void) { return NULL; }
1473
1474int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1475
1476const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
1478/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001479int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
1480 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001481
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 if (alg_a & SSL_aECDSA) {
1483 return SSL_PKEY_ECC;
1484 } else if (alg_a & SSL_aRSA) {
1485 return SSL_PKEY_RSA_ENC;
1486 }
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 return -1;
1489}
David Benjamin9c651c92014-07-12 13:27:45 -04001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1492 * public key in the key exchange, sent in a server Certificate message.
1493 * Otherwise it returns 0. */
1494int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
1495 /* Anonymous ciphers do not include a server certificate. */
1496 if (cipher->algorithm_auth & SSL_aNULL) {
1497 return 0;
1498 }
1499
1500 /* Neither do PSK ciphers, except for RSA_PSK. */
1501 if ((cipher->algorithm_auth & SSL_aPSK) &&
1502 !(cipher->algorithm_mkey & SSL_kRSA)) {
1503 return 0;
1504 }
1505
1506 /* All other ciphers include it. */
1507 return 1;
1508}
1509
1510/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1511 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001512 *
1513 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001514 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1515 * communicate a psk_identity_hint, so it is optional. */
1516int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1517 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
1518 if (cipher->algorithm_mkey & SSL_kEDH || cipher->algorithm_mkey & SSL_kEECDH) {
1519 return 1;
1520 }
1521
1522 /* It is optional in all others. */
1523 return 0;
1524}