blob: d5f9c6801369569ad6f2841e76c731168ec62a96 [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:
273 *out_aead = EVP_aead_rc4_md5_tls();
274 *out_mac_secret_len = MD5_DIGEST_LENGTH;
275 return 1;
276 case SSL_SHA1:
277 *out_aead = EVP_aead_rc4_sha1_tls();
278 *out_mac_secret_len = SHA_DIGEST_LENGTH;
279 return 1;
280 default:
281 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800283
David Benjaminea72bd02014-12-21 21:27:41 -0500284 case SSL_AES128:
285 switch (cipher->algorithm_mac) {
286 case SSL_SHA1:
287 if (version <= TLS1_VERSION) {
288 *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
289 *out_fixed_iv_len = 16;
290 } else {
291 *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
292 }
293 *out_mac_secret_len = SHA_DIGEST_LENGTH;
294 return 1;
295 case SSL_SHA256:
296 *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
297 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
298 return 1;
299 default:
300 return 0;
301 }
302
303 case SSL_AES256:
304 switch (cipher->algorithm_mac) {
305 case SSL_SHA1:
306 if (version <= TLS1_VERSION) {
307 *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
308 *out_fixed_iv_len = 16;
309 } else {
310 *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
311 }
312 *out_mac_secret_len = SHA_DIGEST_LENGTH;
313 return 1;
314 case SSL_SHA256:
315 *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
316 *out_mac_secret_len = SHA256_DIGEST_LENGTH;
317 return 1;
318 case SSL_SHA384:
319 *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
320 *out_mac_secret_len = SHA384_DIGEST_LENGTH;
321 return 1;
322 default:
323 return 0;
324 }
325
326 case SSL_3DES:
327 switch (cipher->algorithm_mac) {
328 case SSL_SHA1:
329 if (version <= TLS1_VERSION) {
330 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
331 *out_fixed_iv_len = 8;
332 } else {
333 *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
334 }
335 *out_mac_secret_len = SHA_DIGEST_LENGTH;
336 return 1;
337 default:
338 return 0;
339 }
340
341 default:
342 return 0;
343 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800344}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700345
346int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 const EVP_MD **md, int *mac_pkey_type,
David Benjaminea72bd02014-12-21 21:27:41 -0500348 size_t *mac_secret_size) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 c = s->cipher;
352 if (c == NULL ||
353 /* This function doesn't deal with EVP_AEAD. See
354 * |ssl_cipher_get_aead_evp|. */
355 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) ||
356 enc == NULL ||
357 md == NULL) {
358 return 0;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 switch (c->algorithm_enc) {
362 case SSL_3DES:
363 *enc = EVP_des_ede3_cbc();
364 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 case SSL_RC4:
367 *enc = EVP_rc4();
368 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 case SSL_AES128:
371 *enc = EVP_aes_128_cbc();
372 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 case SSL_AES256:
375 *enc = EVP_aes_256_cbc();
376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 default:
379 return 0;
380 }
David Benjamind633d632014-08-30 14:43:27 -0400381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size)) {
383 return 0;
384 }
385
386 assert(*enc != NULL && *md != NULL);
387
388/* TODO(fork): enable the stitched cipher modes. */
David Benjamind633d632014-08-30 14:43:27 -0400389#if 0
Adam Langley95c29f32014-06-20 12:00:00 -0700390 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
391 s->ssl_version < TLS1_VERSION)
392 return 1;
393
Adam Langley95c29f32014-06-20 12:00:00 -0700394 if (c->algorithm_enc == SSL_RC4 &&
395 c->algorithm_mac == SSL_MD5 &&
396 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
397 *enc = evp, *md = NULL;
398 else if (c->algorithm_enc == SSL_AES128 &&
399 c->algorithm_mac == SSL_SHA1 &&
400 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
401 *enc = evp, *md = NULL;
402 else if (c->algorithm_enc == SSL_AES256 &&
403 c->algorithm_mac == SSL_SHA1 &&
404 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
405 *enc = evp, *md = NULL;
406#endif
David Benjamind633d632014-08-30 14:43:27 -0400407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 return 1;
409}
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md,
David Benjaminea72bd02014-12-21 21:27:41 -0500412 int *mac_pkey_type, size_t *mac_secret_size) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 const SSL_CIPHER *c;
Adam Langley9447dff2014-06-24 17:29:06 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 c = s->cipher;
416 if (c == NULL) {
417 return 0;
418 }
Adam Langley9447dff2014-06-24 17:29:06 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 switch (c->algorithm_mac) {
421 case SSL_MD5:
422 *md = EVP_md5();
423 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 case SSL_SHA1:
426 *md = EVP_sha1();
427 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 case SSL_SHA256:
430 *md = EVP_sha256();
431 break;
432
433 case SSL_SHA384:
434 *md = EVP_sha384();
435 break;
436
437 default:
438 return 0;
439 }
440
441 if (mac_pkey_type != NULL) {
442 *mac_pkey_type = EVP_PKEY_HMAC;
443 }
444 if (mac_secret_size != NULL) {
445 *mac_secret_size = EVP_MD_size(*md);
446 }
447
448 return 1;
449}
450
451int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md) {
452 if (idx < 0 || idx >= SSL_MAX_DIGEST) {
453 return 0;
454 }
455 *mask = ssl_handshake_digests[idx].mask;
456 *md = ssl_handshake_digests[idx].md_func();
457 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700458}
459
460#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700462
463static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 CIPHER_ORDER **tail) {
465 if (curr == *tail) {
466 return;
467 }
468 if (curr == *head) {
469 *head = curr->next;
470 }
471 if (curr->prev != NULL) {
472 curr->prev->next = curr->next;
473 }
474 if (curr->next != NULL) {
475 curr->next->prev = curr->prev;
476 }
477 (*tail)->next = curr;
478 curr->prev = *tail;
479 curr->next = NULL;
480 *tail = curr;
481}
Adam Langley95c29f32014-06-20 12:00:00 -0700482
483static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 CIPHER_ORDER **tail) {
485 if (curr == *head) {
486 return;
487 }
488 if (curr == *tail) {
489 *tail = curr->prev;
490 }
491 if (curr->next != NULL) {
492 curr->next->prev = curr->prev;
493 }
494 if (curr->prev != NULL) {
495 curr->prev->next = curr->next;
496 }
497 (*head)->prev = curr;
498 curr->next = *head;
499 curr->prev = NULL;
500 *head = curr;
501}
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjamin82c9e902014-12-12 15:55:27 -0500503static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 int num_of_ciphers,
505 CIPHER_ORDER *co_list,
506 CIPHER_ORDER **head_p,
507 CIPHER_ORDER **tail_p) {
508 int i, co_list_num;
509 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 /* We have num_of_ciphers descriptions compiled in, depending on the method
512 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
513 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* Get the initial list of ciphers */
516 co_list_num = 0; /* actual count of ciphers */
517 for (i = 0; i < num_of_ciphers; i++) {
518 c = ssl_method->get_cipher(i);
519 /* drop those that use any of that is not available */
520 if (c != NULL && c->valid) {
521 co_list[co_list_num].cipher = c;
522 co_list[co_list_num].next = NULL;
523 co_list[co_list_num].prev = NULL;
524 co_list[co_list_num].active = 0;
525 co_list[co_list_num].in_group = 0;
526 co_list_num++;
527 }
528 }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 /* Prepare linked list from list entries. */
531 if (co_list_num > 0) {
532 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (co_list_num > 1) {
535 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 for (i = 1; i < co_list_num - 1; i++) {
538 co_list[i].prev = &co_list[i - 1];
539 co_list[i].next = &co_list[i + 1];
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
543 }
544
545 co_list[co_list_num - 1].next = NULL;
546
547 *head_p = &co_list[0];
548 *tail_p = &co_list[co_list_num - 1];
549 }
550}
Adam Langley95c29f32014-06-20 12:00:00 -0700551
552static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 int num_of_group_aliases,
554 CIPHER_ORDER *head) {
555 CIPHER_ORDER *ciph_curr;
556 const SSL_CIPHER **ca_curr;
557 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 /* First, add the real ciphers as already collected. */
560 ciph_curr = head;
561 ca_curr = ca_list;
562 while (ciph_curr != NULL) {
563 *ca_curr = ciph_curr->cipher;
564 ca_curr++;
565 ciph_curr = ciph_curr->next;
566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 /* Now we add the available ones from the cipher_aliases[] table. They
569 * represent either one or more algorithms, some of which in any affected
570 * category must be supported (set in enabled_mask), or represent a cipher
571 * strength value (will be added in any case because algorithms=0). */
572 for (i = 0; i < num_of_group_aliases; i++) {
573 *ca_curr = cipher_aliases + i;
574 ca_curr++;
575 }
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 *ca_curr = NULL; /* end of list */
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langleyfcf25832014-12-18 17:42:32 -0800580static void ssl_cipher_apply_rule(
581 unsigned long cipher_id, unsigned long alg_mkey, unsigned long alg_auth,
582 unsigned long alg_enc, unsigned long alg_mac, unsigned long alg_ssl,
583 unsigned long algo_strength, int rule, int strength_bits, int in_group,
584 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
585 CIPHER_ORDER *head, *tail, *curr, *next, *last;
586 const SSL_CIPHER *cp;
587 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 if (rule == CIPHER_DEL) {
590 /* needed to maintain sorting between currently deleted ciphers */
591 reverse = 1;
592 }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 head = *head_p;
595 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 if (reverse) {
598 next = tail;
599 last = head;
600 } else {
601 next = head;
602 last = tail;
603 }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 curr = NULL;
606 for (;;) {
607 if (curr == last) {
608 break;
609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 curr = next;
612 if (curr == NULL) {
613 break;
614 }
Adam Langleye3142a72014-07-24 17:56:48 -0700615
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 next = reverse ? curr->prev : curr->next;
617 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 /* Selection criteria is either the value of strength_bits
620 * or the algorithms used. */
621 if (strength_bits >= 0) {
622 if (strength_bits != cp->strength_bits) {
623 continue;
624 }
625 } else {
626 if ((alg_mkey && !(alg_mkey & cp->algorithm_mkey)) ||
627 (alg_auth && !(alg_auth & cp->algorithm_auth)) ||
628 (alg_enc && !(alg_enc & cp->algorithm_enc)) ||
629 (alg_mac && !(alg_mac & cp->algorithm_mac)) ||
630 (alg_ssl && !(alg_ssl & cp->algorithm_ssl)) ||
631 (algo_strength && !(algo_strength & cp->algo_strength))) {
632 continue;
633 }
634 }
Adam Langleye3142a72014-07-24 17:56:48 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 /* add the cipher if it has not been added yet. */
637 if (rule == CIPHER_ADD) {
638 /* reverse == 0 */
639 if (!curr->active) {
640 ll_append_tail(&head, curr, &tail);
641 curr->active = 1;
642 curr->in_group = in_group;
643 }
644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 /* Move the added cipher to this location */
647 else if (rule == CIPHER_ORD) {
648 /* reverse == 0 */
649 if (curr->active) {
650 ll_append_tail(&head, curr, &tail);
651 curr->in_group = 0;
652 }
653 } else if (rule == CIPHER_DEL) {
654 /* reverse == 1 */
655 if (curr->active) {
656 /* most recently deleted ciphersuites get best positions
657 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
658 * works in reverse to maintain the order) */
659 ll_append_head(&head, curr, &tail);
660 curr->active = 0;
661 curr->in_group = 0;
662 }
663 } else if (rule == CIPHER_KILL) {
664 /* reverse == 0 */
665 if (head == curr) {
666 head = curr->next;
667 } else {
668 curr->prev->next = curr->next;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 if (tail == curr) {
672 tail = curr->prev;
673 }
674 curr->active = 0;
675 if (curr->next != NULL) {
676 curr->next->prev = curr->prev;
677 }
678 if (curr->prev != NULL) {
679 curr->prev->next = curr->next;
680 }
681 curr->next = NULL;
682 curr->prev = NULL;
683 }
684 }
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langleyfcf25832014-12-18 17:42:32 -0800686 *head_p = head;
687 *tail_p = tail;
688}
Adam Langley95c29f32014-06-20 12:00:00 -0700689
690static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 CIPHER_ORDER **tail_p) {
692 int max_strength_bits, i, *number_uses;
693 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 /* This routine sorts the ciphers with descending strength. The sorting must
696 * keep the pre-sorted sequence, so we apply the normal sorting routine as
697 * '+' movement to the end of the list. */
698 max_strength_bits = 0;
699 curr = *head_p;
700 while (curr != NULL) {
701 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
702 max_strength_bits = curr->cipher->strength_bits;
703 }
704 curr = curr->next;
705 }
Adam Langley95c29f32014-06-20 12:00:00 -0700706
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
708 if (!number_uses) {
709 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
710 return 0;
711 }
712 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 /* Now find the strength_bits values actually used. */
715 curr = *head_p;
716 while (curr != NULL) {
717 if (curr->active) {
718 number_uses[curr->cipher->strength_bits]++;
719 }
720 curr = curr->next;
721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 /* Go through the list of used strength_bits values in descending order. */
724 for (i = max_strength_bits; i >= 0; i--) {
725 if (number_uses[i] > 0) {
726 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
727 tail_p);
728 }
729 }
730
731 OPENSSL_free(number_uses);
732 return 1;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
735static int ssl_cipher_process_rulestr(const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 CIPHER_ORDER **head_p,
737 CIPHER_ORDER **tail_p,
738 const SSL_CIPHER **ca_list) {
739 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
740 const char *l, *buf;
741 int j, multi, found, rule, retval, ok, buflen, in_group = 0, has_group = 0;
742 unsigned long cipher_id = 0;
743 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745 retval = 1;
746 l = rule_str;
747 for (;;) {
748 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 if (ch == '\0') {
751 break; /* done */
752 }
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 if (in_group) {
755 if (ch == ']') {
756 if (!in_group) {
757 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
758 SSL_R_UNEXPECTED_GROUP_CLOSE);
759 retval = found = in_group = 0;
760 break;
761 }
762 if (*tail_p) {
763 (*tail_p)->in_group = 0;
764 }
765 in_group = 0;
766 l++;
767 continue;
768 }
David Benjamin37d92462014-09-20 17:54:24 -0400769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 if (ch == '|') {
771 rule = CIPHER_ADD;
772 l++;
773 continue;
774 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
775 !(ch >= '0' && ch <= '9')) {
776 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
777 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
778 retval = found = in_group = 0;
779 break;
780 } else {
781 rule = CIPHER_ADD;
782 }
783 } else if (ch == '-') {
784 rule = CIPHER_DEL;
785 l++;
786 } else if (ch == '+') {
787 rule = CIPHER_ORD;
788 l++;
789 } else if (ch == '!') {
790 rule = CIPHER_KILL;
791 l++;
792 } else if (ch == '@') {
793 rule = CIPHER_SPECIAL;
794 l++;
795 } else if (ch == '[') {
796 if (in_group) {
797 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
798 retval = found = in_group = 0;
799 break;
800 }
801 in_group = 1;
802 has_group = 1;
803 l++;
804 continue;
805 } else {
806 rule = CIPHER_ADD;
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 /* If preference groups are enabled, the only legal operator is +.
810 * Otherwise the in_group bits will get mixed up. */
811 if (has_group && rule != CIPHER_ADD) {
812 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
813 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
814 retval = found = in_group = 0;
815 break;
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (ITEM_SEP(ch)) {
819 l++;
820 continue;
821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 alg_mkey = 0;
824 alg_auth = 0;
825 alg_enc = 0;
826 alg_mac = 0;
827 alg_ssl = 0;
828 algo_strength = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 for (;;) {
831 ch = *l;
832 buf = l;
833 buflen = 0;
834 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
835 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
836 ch = *(++l);
837 buflen++;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 if (buflen == 0) {
841 /* We hit something we cannot deal with, it is no command or separator
842 * nor alphanumeric, so we call this an error. */
843 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
844 SSL_R_INVALID_COMMAND);
845 retval = found = in_group = 0;
846 l++;
847 break;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 if (rule == CIPHER_SPECIAL) {
851 found = 0; /* unused -- avoid compiler warning */
852 break; /* special treatment */
853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 /* check for multi-part specification */
856 if (ch == '+') {
857 multi = 1;
858 l++;
859 } else {
860 multi = 0;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 /* Now search for the cipher alias in the ca_list. Be careful with the
864 * strncmp, because the "buflen" limitation will make the rule "ADH:SOME"
865 * and the cipher "ADH-MY-CIPHER" look like a match for buflen=3. So
866 * additionally check whether the cipher name found has the correct
867 * length. We can save a strlen() call: just checking for the '\0' at the
868 * right place is sufficient, we have to strncmp() anyway. (We cannot use
869 * strcmp(), because buf is not '\0' terminated.) */
870 j = found = 0;
871 cipher_id = 0;
872 while (ca_list[j]) {
873 if (!strncmp(buf, ca_list[j]->name, buflen) &&
874 (ca_list[j]->name[buflen] == '\0')) {
875 found = 1;
876 break;
877 } else {
878 j++;
879 }
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if (!found) {
883 break; /* ignore this entry */
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 if (ca_list[j]->algorithm_mkey) {
887 if (alg_mkey) {
888 alg_mkey &= ca_list[j]->algorithm_mkey;
889 if (!alg_mkey) {
890 found = 0;
891 break;
892 }
893 } else {
894 alg_mkey = ca_list[j]->algorithm_mkey;
895 }
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (ca_list[j]->algorithm_auth) {
899 if (alg_auth) {
900 alg_auth &= ca_list[j]->algorithm_auth;
901 if (!alg_auth) {
902 found = 0;
903 break;
904 }
905 } else {
906 alg_auth = ca_list[j]->algorithm_auth;
907 }
908 }
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 if (ca_list[j]->algorithm_enc) {
911 if (alg_enc) {
912 alg_enc &= ca_list[j]->algorithm_enc;
913 if (!alg_enc) {
914 found = 0;
915 break;
916 }
917 } else {
918 alg_enc = ca_list[j]->algorithm_enc;
919 }
920 }
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 if (ca_list[j]->algorithm_mac) {
923 if (alg_mac) {
924 alg_mac &= ca_list[j]->algorithm_mac;
925 if (!alg_mac) {
926 found = 0;
927 break;
928 }
929 } else {
930 alg_mac = ca_list[j]->algorithm_mac;
931 }
932 }
Adam Langley858a88d2014-06-20 12:00:00 -0700933
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (ca_list[j]->algo_strength) {
935 if (algo_strength) {
936 algo_strength &= ca_list[j]->algo_strength;
937 if (!algo_strength) {
938 found = 0;
939 break;
940 }
941 } else {
942 algo_strength |= ca_list[j]->algo_strength;
943 }
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (ca_list[j]->valid) {
947 /* explicit ciphersuite found; its protocol version does not become
948 * part of the search pattern! */
949 cipher_id = ca_list[j]->id;
950 } else {
951 /* not an explicit ciphersuite; only in this case, the protocol version
952 * is considered part of the search pattern. */
953 if (ca_list[j]->algorithm_ssl) {
954 if (alg_ssl) {
955 alg_ssl &= ca_list[j]->algorithm_ssl;
956 if (!alg_ssl) {
957 found = 0;
958 break;
959 }
960 } else {
961 alg_ssl = ca_list[j]->algorithm_ssl;
962 }
963 }
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 if (!multi) {
967 break;
968 }
969 }
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 /* Ok, we have the rule, now apply it. */
972 if (rule == CIPHER_SPECIAL) {
973 /* special command */
974 ok = 0;
975 if (buflen == 8 && !strncmp(buf, "STRENGTH", 8)) {
976 ok = ssl_cipher_strength_sort(head_p, tail_p);
977 } else {
978 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
979 SSL_R_INVALID_COMMAND);
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 if (ok == 0) {
983 retval = 0;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 /* We do not support any "multi" options together with "@", so throw away
987 * the rest of the command, if any left, until end or ':' is found. */
988 while (*l != '\0' && !ITEM_SEP(*l)) {
989 l++;
990 }
991 } else if (found) {
992 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
993 alg_ssl, algo_strength, rule, -1, in_group, head_p,
994 tail_p);
995 } else {
996 while (*l != '\0' && !ITEM_SEP(*l)) {
997 l++;
998 }
999 }
1000 }
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (in_group) {
1003 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1004 retval = 0;
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 return retval;
1008}
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010STACK_OF(SSL_CIPHER) *
1011ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
1012 struct ssl_cipher_preference_list_st **cipher_list,
1013 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
1014 const char *rule_str, CERT *c) {
1015 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1016 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
1017 const char *rule_p;
1018 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1019 const SSL_CIPHER **ca_list = NULL;
1020 uint8_t *in_group_flags = NULL;
1021 unsigned int num_in_group_flags = 0;
1022 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 /* Return with error if nothing to do. */
1025 if (rule_str == NULL || cipher_list == NULL) {
1026 return NULL;
1027 }
David Benjamin5213df42014-08-20 14:19:54 -04001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 /* Now we have to collect the available ciphers from the compiled in ciphers.
1030 * We cannot get more than the number compiled in, so it is used for
1031 * allocation. */
1032 num_of_ciphers = ssl_method->num_ciphers();
1033 co_list =
1034 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1035 if (co_list == NULL) {
1036 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1037 return NULL;
1038 }
Adam Langley95c29f32014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 /* Now arrange all ciphers by preference:
1043 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langleyfcf25832014-12-18 17:42:32 -08001045 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
1046 * key exchange mechanisms */
1047 ssl_cipher_apply_rule(0, SSL_kEECDH, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD, -1,
1048 0, &head, &tail);
1049 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1050 &tail);
1051 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head,
1052 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1055 * CHACHA20 unless there is hardware support for fast and constant-time
1056 * AES_GCM. */
1057 if (EVP_has_aes_hardware()) {
1058 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1059 &head, &tail);
1060 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1061 &head, &tail);
1062 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
1063 -1, 0, &head, &tail);
1064 } else {
1065 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
1066 -1, 0, &head, &tail);
1067 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1068 &head, &tail);
1069 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
1070 &head, &tail);
1071 }
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1074 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
1075 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1076 &tail);
1077 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1078 &tail);
1079 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
1080 &head, &tail);
1081 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
1082 &head, &tail);
1083 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
1084 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 /* Temporarily enable everything else for sorting */
1087 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 /* Move ciphers without forward secrecy to the end. */
1090 ssl_cipher_apply_rule(0, ~(SSL_kEDH | SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD,
1091 -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001092
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1094 * (For applications that allow them, they aren't too bad, but we prefer
1095 * authenticated ciphers.)
1096 * TODO(davidben): Remove them altogether? */
1097 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head,
1098 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 /* Now disable everything (maintaining the ordering!) */
1101 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 /* We also need cipher aliases for selecting based on the rule_str. There
1104 * might be two types of entries in the rule_str: 1) names of ciphers
1105 * themselves 2) aliases for groups of ciphers. For 1) we need the available
1106 * ciphers and for 2) the cipher groups of cipher_aliases added together in
1107 * one list (otherwise we would be happy with just the cipher_aliases
1108 * table). */
1109 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1110 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1111 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1112 if (ca_list == NULL) {
1113 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1114 goto err;
1115 }
1116 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001117
Adam Langleyfcf25832014-12-18 17:42:32 -08001118 /* If the rule_string begins with DEFAULT, apply the default rule before
1119 * using the (possibly available) additional rules. */
1120 ok = 1;
1121 rule_p = rule_str;
1122 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1123 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1124 ca_list);
1125 rule_p += 7;
1126 if (*rule_p == ':') {
1127 rule_p++;
1128 }
1129 }
Adam Langley858a88d2014-06-20 12:00:00 -07001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 if (ok && strlen(rule_p) > 0) {
1132 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1133 }
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Nick Harper4dd053e2014-12-22 14:58:54 -08001135 OPENSSL_free((void *)ca_list); /* Not needed anymore */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 if (!ok) {
1138 goto err;
1139 }
1140
1141 /* Allocate new "cipherstack" for the result, return with error
1142 * if we cannot get one. */
1143 cipherstack = sk_SSL_CIPHER_new_null();
1144 if (cipherstack == NULL) {
1145 goto err;
1146 }
1147
1148 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1149 if (!in_group_flags) {
1150 goto err;
1151 }
1152
1153 /* The cipher selection for the list is done. The ciphers are added
1154 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1155 for (curr = head; curr != NULL; curr = curr->next) {
1156 if (curr->active) {
1157 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1158 in_group_flags[num_in_group_flags++] = curr->in_group;
1159 }
1160 }
1161 OPENSSL_free(co_list); /* Not needed any longer */
1162 co_list = NULL;
1163
1164 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1165 if (tmp_cipher_list == NULL) {
1166 goto err;
1167 }
1168 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1169 if (!pref_list) {
1170 goto err;
1171 }
1172 pref_list->ciphers = cipherstack;
1173 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1174 if (!pref_list->in_group_flags) {
1175 goto err;
1176 }
1177 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1178 OPENSSL_free(in_group_flags);
1179 in_group_flags = NULL;
1180 if (*cipher_list != NULL) {
1181 ssl_cipher_preference_list_free(*cipher_list);
1182 }
1183 *cipher_list = pref_list;
1184 pref_list = NULL;
1185
1186 if (cipher_list_by_id != NULL) {
1187 if (*cipher_list_by_id != NULL) {
1188 sk_SSL_CIPHER_free(*cipher_list_by_id);
1189 }
1190 *cipher_list_by_id = tmp_cipher_list;
1191 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001192 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001193
1194 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1195 } else {
1196 sk_SSL_CIPHER_free(tmp_cipher_list);
1197 tmp_cipher_list = NULL;
1198 }
1199
1200 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001201
1202err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 if (co_list) {
1204 OPENSSL_free(co_list);
1205 }
1206 if (in_group_flags) {
1207 OPENSSL_free(in_group_flags);
1208 }
1209 if (cipherstack) {
1210 sk_SSL_CIPHER_free(cipherstack);
1211 }
1212 if (tmp_cipher_list) {
1213 sk_SSL_CIPHER_free(tmp_cipher_list);
1214 }
1215 if (pref_list && pref_list->in_group_flags) {
1216 OPENSSL_free(pref_list->in_group_flags);
1217 }
1218 if (pref_list) {
1219 OPENSSL_free(pref_list);
1220 }
1221 return NULL;
1222}
Adam Langley95c29f32014-06-20 12:00:00 -07001223
Adam Langleyfcf25832014-12-18 17:42:32 -08001224const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1225 int len) {
1226 const char *ver;
1227 const char *kx, *au, *enc, *mac;
1228 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1229 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001230
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 alg_mkey = cipher->algorithm_mkey;
1232 alg_auth = cipher->algorithm_auth;
1233 alg_enc = cipher->algorithm_enc;
1234 alg_mac = cipher->algorithm_mac;
1235 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001236
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 if (alg_ssl & SSL_SSLV3) {
1238 ver = "SSLv3";
1239 } else if (alg_ssl & SSL_TLSV1_2) {
1240 ver = "TLSv1.2";
1241 } else {
1242 ver = "unknown";
1243 }
Adam Langley95c29f32014-06-20 12:00:00 -07001244
Adam Langleyfcf25832014-12-18 17:42:32 -08001245 switch (alg_mkey) {
1246 case SSL_kRSA:
1247 kx = "RSA";
1248 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001249
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 case SSL_kEDH:
1251 kx = "DH";
1252 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001253
Adam Langleyfcf25832014-12-18 17:42:32 -08001254 case SSL_kEECDH:
1255 kx = "ECDH";
1256 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001257
Adam Langleyfcf25832014-12-18 17:42:32 -08001258 case SSL_kPSK:
1259 kx = "PSK";
1260 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001261
Adam Langleyfcf25832014-12-18 17:42:32 -08001262 default:
1263 kx = "unknown";
1264 }
Adam Langley95c29f32014-06-20 12:00:00 -07001265
Adam Langleyfcf25832014-12-18 17:42:32 -08001266 switch (alg_auth) {
1267 case SSL_aRSA:
1268 au = "RSA";
1269 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001270
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 case SSL_aNULL:
1272 au = "None";
1273 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001274
Adam Langleyfcf25832014-12-18 17:42:32 -08001275 case SSL_aECDSA:
1276 au = "ECDSA";
1277 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 case SSL_aPSK:
1280 au = "PSK";
1281 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 default:
1284 au = "unknown";
1285 break;
1286 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001287
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 switch (alg_enc) {
1289 case SSL_3DES:
1290 enc = "3DES(168)";
1291 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001292
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 case SSL_RC4:
1294 enc = "RC4(128)";
1295 break;
1296
1297 case SSL_AES128:
1298 enc = "AES(128)";
1299 break;
1300
1301 case SSL_AES256:
1302 enc = "AES(256)";
1303 break;
1304
1305 case SSL_AES128GCM:
1306 enc = "AESGCM(128)";
1307 break;
1308
1309 case SSL_AES256GCM:
1310 enc = "AESGCM(256)";
1311 break;
1312
1313 case SSL_CHACHA20POLY1305:
1314 enc = "ChaCha20-Poly1305";
1315 break;
1316
1317 default:
1318 enc = "unknown";
1319 break;
1320 }
1321
1322 switch (alg_mac) {
1323 case SSL_MD5:
1324 mac = "MD5";
1325 break;
1326
1327 case SSL_SHA1:
1328 mac = "SHA1";
1329 break;
1330
1331 case SSL_SHA256:
1332 mac = "SHA256";
1333 break;
1334
1335 case SSL_SHA384:
1336 mac = "SHA384";
1337 break;
1338
1339 case SSL_AEAD:
1340 mac = "AEAD";
1341 break;
1342
1343 default:
1344 mac = "unknown";
1345 break;
1346 }
1347
1348 if (buf == NULL) {
1349 len = 128;
1350 buf = OPENSSL_malloc(len);
1351 if (buf == NULL)
1352 return "OPENSSL_malloc Error";
1353 } else if (len < 128) {
1354 return "Buffer too small";
1355 }
1356
1357 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1358 return buf;
1359}
1360
1361int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1362 return (c->algorithm_enc & SSL_AES) != 0;
1363}
1364
1365int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1366 return (c->algorithm_mac & SSL_MD5) != 0;
1367}
1368
1369int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1370 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1371}
1372
1373int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1374 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1375}
1376
1377const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1378 int i;
1379
1380 if (c == NULL) {
1381 return "(NONE)";
1382 }
1383
1384 i = (int)(c->id >> 24L);
1385 if (i == 3) {
1386 return "TLSv1/SSLv3";
1387 } else if (i == 2) {
1388 return "SSLv2";
1389 } else {
1390 return "unknown";
1391 }
1392}
Adam Langley95c29f32014-06-20 12:00:00 -07001393
1394/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001395const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1396 if (c != NULL) {
1397 return c->name;
1398 }
1399
1400 return "(NONE)";
1401}
Adam Langley95c29f32014-06-20 12:00:00 -07001402
Adam Langleyf1df2b32014-08-11 17:37:53 -07001403const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1404 if (cipher == NULL) {
1405 return "";
1406 }
1407
1408 switch (cipher->algorithm_mkey) {
1409 case SSL_kRSA:
1410 return SSL_TXT_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001411
Adam Langleyf1df2b32014-08-11 17:37:53 -07001412 case SSL_kEDH:
1413 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001414 case SSL_aRSA:
1415 return "DHE_" SSL_TXT_RSA;
1416 case SSL_aNULL:
1417 return SSL_TXT_DH "_anon";
1418 default:
1419 return "UNKNOWN";
1420 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001421
Adam Langleyf1df2b32014-08-11 17:37:53 -07001422 case SSL_kEECDH:
1423 switch (cipher->algorithm_auth) {
1424 case SSL_aECDSA:
1425 return "ECDHE_" SSL_TXT_ECDSA;
1426 case SSL_aRSA:
1427 return "ECDHE_" SSL_TXT_RSA;
1428 case SSL_aNULL:
1429 return SSL_TXT_ECDH "_anon";
1430 default:
1431 return "UNKNOWN";
1432 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001433
Adam Langleyf1df2b32014-08-11 17:37:53 -07001434 default:
1435 return "UNKNOWN";
1436 }
1437}
1438
Adam Langley95c29f32014-06-20 12:00:00 -07001439/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001440int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1441 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 if (c != NULL) {
1444 if (alg_bits != NULL) {
1445 *alg_bits = c->alg_bits;
1446 }
1447 ret = c->strength_bits;
1448 }
Adam Langley95c29f32014-06-20 12:00:00 -07001449
Adam Langleyfcf25832014-12-18 17:42:32 -08001450 return ret;
1451}
Adam Langley95c29f32014-06-20 12:00:00 -07001452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Adam Langleyfcf25832014-12-18 17:42:32 -08001455void *SSL_COMP_get_compression_methods(void) { return NULL; }
1456
1457int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1458
1459const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001460
1461/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001462int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
1463 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001464
Adam Langleyfcf25832014-12-18 17:42:32 -08001465 if (alg_a & SSL_aECDSA) {
1466 return SSL_PKEY_ECC;
1467 } else if (alg_a & SSL_aRSA) {
1468 return SSL_PKEY_RSA_ENC;
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 return -1;
1472}
David Benjamin9c651c92014-07-12 13:27:45 -04001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1475 * public key in the key exchange, sent in a server Certificate message.
1476 * Otherwise it returns 0. */
1477int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
1478 /* Anonymous ciphers do not include a server certificate. */
1479 if (cipher->algorithm_auth & SSL_aNULL) {
1480 return 0;
1481 }
1482
1483 /* Neither do PSK ciphers, except for RSA_PSK. */
1484 if ((cipher->algorithm_auth & SSL_aPSK) &&
1485 !(cipher->algorithm_mkey & SSL_kRSA)) {
1486 return 0;
1487 }
1488
1489 /* All other ciphers include it. */
1490 return 1;
1491}
1492
1493/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1494 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001495 *
1496 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1498 * communicate a psk_identity_hint, so it is optional. */
1499int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1500 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
1501 if (cipher->algorithm_mkey & SSL_kEDH || cipher->algorithm_mkey & SSL_kEECDH) {
1502 return 1;
1503 }
1504
1505 /* It is optional in all others. */
1506 return 0;
1507}