blob: e79253325ceec26c03bf33fc7215f0929b51f093 [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
David Benjamin1f5e1152014-12-23 09:23:32 -0500363int ssl_get_handshake_digest(size_t idx, long *mask, const EVP_MD **md) {
364 if (idx >= SSL_MAX_DIGEST) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 return 0;
366 }
367 *mask = ssl_handshake_digests[idx].mask;
368 *md = ssl_handshake_digests[idx].md_func();
369 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700370}
371
372#define ITEM_SEP(a) \
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
Adam Langley95c29f32014-06-20 12:00:00 -0700374
375static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 CIPHER_ORDER **tail) {
377 if (curr == *tail) {
378 return;
379 }
380 if (curr == *head) {
381 *head = curr->next;
382 }
383 if (curr->prev != NULL) {
384 curr->prev->next = curr->next;
385 }
386 if (curr->next != NULL) {
387 curr->next->prev = curr->prev;
388 }
389 (*tail)->next = curr;
390 curr->prev = *tail;
391 curr->next = NULL;
392 *tail = curr;
393}
Adam Langley95c29f32014-06-20 12:00:00 -0700394
395static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 CIPHER_ORDER **tail) {
397 if (curr == *head) {
398 return;
399 }
400 if (curr == *tail) {
401 *tail = curr->prev;
402 }
403 if (curr->next != NULL) {
404 curr->next->prev = curr->prev;
405 }
406 if (curr->prev != NULL) {
407 curr->prev->next = curr->next;
408 }
409 (*head)->prev = curr;
410 curr->next = *head;
411 curr->prev = NULL;
412 *head = curr;
413}
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjamin82c9e902014-12-12 15:55:27 -0500415static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 int num_of_ciphers,
417 CIPHER_ORDER *co_list,
418 CIPHER_ORDER **head_p,
419 CIPHER_ORDER **tail_p) {
420 int i, co_list_num;
421 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 /* We have num_of_ciphers descriptions compiled in, depending on the method
424 * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a
425 * linked list with at most num entries. */
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 /* Get the initial list of ciphers */
428 co_list_num = 0; /* actual count of ciphers */
429 for (i = 0; i < num_of_ciphers; i++) {
430 c = ssl_method->get_cipher(i);
431 /* drop those that use any of that is not available */
432 if (c != NULL && c->valid) {
433 co_list[co_list_num].cipher = c;
434 co_list[co_list_num].next = NULL;
435 co_list[co_list_num].prev = NULL;
436 co_list[co_list_num].active = 0;
437 co_list[co_list_num].in_group = 0;
438 co_list_num++;
439 }
440 }
Adam Langley95c29f32014-06-20 12:00:00 -0700441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 /* Prepare linked list from list entries. */
443 if (co_list_num > 0) {
444 co_list[0].prev = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 if (co_list_num > 1) {
447 co_list[0].next = &co_list[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 for (i = 1; i < co_list_num - 1; i++) {
450 co_list[i].prev = &co_list[i - 1];
451 co_list[i].next = &co_list[i + 1];
452 }
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
455 }
456
457 co_list[co_list_num - 1].next = NULL;
458
459 *head_p = &co_list[0];
460 *tail_p = &co_list[co_list_num - 1];
461 }
462}
Adam Langley95c29f32014-06-20 12:00:00 -0700463
464static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 int num_of_group_aliases,
466 CIPHER_ORDER *head) {
467 CIPHER_ORDER *ciph_curr;
468 const SSL_CIPHER **ca_curr;
469 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 /* First, add the real ciphers as already collected. */
472 ciph_curr = head;
473 ca_curr = ca_list;
474 while (ciph_curr != NULL) {
475 *ca_curr = ciph_curr->cipher;
476 ca_curr++;
477 ciph_curr = ciph_curr->next;
478 }
Adam Langley95c29f32014-06-20 12:00:00 -0700479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 /* Now we add the available ones from the cipher_aliases[] table. They
481 * represent either one or more algorithms, some of which in any affected
482 * category must be supported (set in enabled_mask), or represent a cipher
483 * strength value (will be added in any case because algorithms=0). */
484 for (i = 0; i < num_of_group_aliases; i++) {
485 *ca_curr = cipher_aliases + i;
486 ca_curr++;
487 }
Adam Langley95c29f32014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 *ca_curr = NULL; /* end of list */
490}
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492static void ssl_cipher_apply_rule(
493 unsigned long cipher_id, unsigned long alg_mkey, unsigned long alg_auth,
494 unsigned long alg_enc, unsigned long alg_mac, unsigned long alg_ssl,
495 unsigned long algo_strength, int rule, int strength_bits, int in_group,
496 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
497 CIPHER_ORDER *head, *tail, *curr, *next, *last;
498 const SSL_CIPHER *cp;
499 int reverse = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 if (rule == CIPHER_DEL) {
502 /* needed to maintain sorting between currently deleted ciphers */
503 reverse = 1;
504 }
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 head = *head_p;
507 tail = *tail_p;
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 if (reverse) {
510 next = tail;
511 last = head;
512 } else {
513 next = head;
514 last = tail;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 curr = NULL;
518 for (;;) {
519 if (curr == last) {
520 break;
521 }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 curr = next;
524 if (curr == NULL) {
525 break;
526 }
Adam Langleye3142a72014-07-24 17:56:48 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 next = reverse ? curr->prev : curr->next;
529 cp = curr->cipher;
Adam Langleye3142a72014-07-24 17:56:48 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 /* Selection criteria is either the value of strength_bits
532 * or the algorithms used. */
533 if (strength_bits >= 0) {
534 if (strength_bits != cp->strength_bits) {
535 continue;
536 }
537 } else {
538 if ((alg_mkey && !(alg_mkey & cp->algorithm_mkey)) ||
539 (alg_auth && !(alg_auth & cp->algorithm_auth)) ||
540 (alg_enc && !(alg_enc & cp->algorithm_enc)) ||
541 (alg_mac && !(alg_mac & cp->algorithm_mac)) ||
542 (alg_ssl && !(alg_ssl & cp->algorithm_ssl)) ||
543 (algo_strength && !(algo_strength & cp->algo_strength))) {
544 continue;
545 }
546 }
Adam Langleye3142a72014-07-24 17:56:48 -0700547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 /* add the cipher if it has not been added yet. */
549 if (rule == CIPHER_ADD) {
550 /* reverse == 0 */
551 if (!curr->active) {
552 ll_append_tail(&head, curr, &tail);
553 curr->active = 1;
554 curr->in_group = in_group;
555 }
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 /* Move the added cipher to this location */
559 else if (rule == CIPHER_ORD) {
560 /* reverse == 0 */
561 if (curr->active) {
562 ll_append_tail(&head, curr, &tail);
563 curr->in_group = 0;
564 }
565 } else if (rule == CIPHER_DEL) {
566 /* reverse == 1 */
567 if (curr->active) {
568 /* most recently deleted ciphersuites get best positions
569 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
570 * works in reverse to maintain the order) */
571 ll_append_head(&head, curr, &tail);
572 curr->active = 0;
573 curr->in_group = 0;
574 }
575 } else if (rule == CIPHER_KILL) {
576 /* reverse == 0 */
577 if (head == curr) {
578 head = curr->next;
579 } else {
580 curr->prev->next = curr->next;
581 }
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 if (tail == curr) {
584 tail = curr->prev;
585 }
586 curr->active = 0;
587 if (curr->next != NULL) {
588 curr->next->prev = curr->prev;
589 }
590 if (curr->prev != NULL) {
591 curr->prev->next = curr->next;
592 }
593 curr->next = NULL;
594 curr->prev = NULL;
595 }
596 }
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 *head_p = head;
599 *tail_p = tail;
600}
Adam Langley95c29f32014-06-20 12:00:00 -0700601
602static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 CIPHER_ORDER **tail_p) {
604 int max_strength_bits, i, *number_uses;
605 CIPHER_ORDER *curr;
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 /* This routine sorts the ciphers with descending strength. The sorting must
608 * keep the pre-sorted sequence, so we apply the normal sorting routine as
609 * '+' movement to the end of the list. */
610 max_strength_bits = 0;
611 curr = *head_p;
612 while (curr != NULL) {
613 if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
614 max_strength_bits = curr->cipher->strength_bits;
615 }
616 curr = curr->next;
617 }
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
620 if (!number_uses) {
621 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
622 return 0;
623 }
624 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 /* Now find the strength_bits values actually used. */
627 curr = *head_p;
628 while (curr != NULL) {
629 if (curr->active) {
630 number_uses[curr->cipher->strength_bits]++;
631 }
632 curr = curr->next;
633 }
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 /* Go through the list of used strength_bits values in descending order. */
636 for (i = max_strength_bits; i >= 0; i--) {
637 if (number_uses[i] > 0) {
638 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
639 tail_p);
640 }
641 }
642
643 OPENSSL_free(number_uses);
644 return 1;
645}
Adam Langley95c29f32014-06-20 12:00:00 -0700646
647static int ssl_cipher_process_rulestr(const char *rule_str,
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 CIPHER_ORDER **head_p,
649 CIPHER_ORDER **tail_p,
650 const SSL_CIPHER **ca_list) {
651 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
652 const char *l, *buf;
653 int j, multi, found, rule, retval, ok, buflen, in_group = 0, has_group = 0;
654 unsigned long cipher_id = 0;
655 char ch;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 retval = 1;
658 l = rule_str;
659 for (;;) {
660 ch = *l;
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 if (ch == '\0') {
663 break; /* done */
664 }
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 if (in_group) {
667 if (ch == ']') {
668 if (!in_group) {
669 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
670 SSL_R_UNEXPECTED_GROUP_CLOSE);
671 retval = found = in_group = 0;
672 break;
673 }
674 if (*tail_p) {
675 (*tail_p)->in_group = 0;
676 }
677 in_group = 0;
678 l++;
679 continue;
680 }
David Benjamin37d92462014-09-20 17:54:24 -0400681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 if (ch == '|') {
683 rule = CIPHER_ADD;
684 l++;
685 continue;
686 } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
687 !(ch >= '0' && ch <= '9')) {
688 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
689 SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
690 retval = found = in_group = 0;
691 break;
692 } else {
693 rule = CIPHER_ADD;
694 }
695 } else if (ch == '-') {
696 rule = CIPHER_DEL;
697 l++;
698 } else if (ch == '+') {
699 rule = CIPHER_ORD;
700 l++;
701 } else if (ch == '!') {
702 rule = CIPHER_KILL;
703 l++;
704 } else if (ch == '@') {
705 rule = CIPHER_SPECIAL;
706 l++;
707 } else if (ch == '[') {
708 if (in_group) {
709 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
710 retval = found = in_group = 0;
711 break;
712 }
713 in_group = 1;
714 has_group = 1;
715 l++;
716 continue;
717 } else {
718 rule = CIPHER_ADD;
719 }
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721 /* If preference groups are enabled, the only legal operator is +.
722 * Otherwise the in_group bits will get mixed up. */
723 if (has_group && rule != CIPHER_ADD) {
724 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
725 SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
726 retval = found = in_group = 0;
727 break;
728 }
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 if (ITEM_SEP(ch)) {
731 l++;
732 continue;
733 }
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 alg_mkey = 0;
736 alg_auth = 0;
737 alg_enc = 0;
738 alg_mac = 0;
739 alg_ssl = 0;
740 algo_strength = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700741
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 for (;;) {
743 ch = *l;
744 buf = l;
745 buflen = 0;
746 while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
747 ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
748 ch = *(++l);
749 buflen++;
750 }
Adam Langley95c29f32014-06-20 12:00:00 -0700751
Adam Langleyfcf25832014-12-18 17:42:32 -0800752 if (buflen == 0) {
753 /* We hit something we cannot deal with, it is no command or separator
754 * nor alphanumeric, so we call this an error. */
755 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
756 SSL_R_INVALID_COMMAND);
757 retval = found = in_group = 0;
758 l++;
759 break;
760 }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 if (rule == CIPHER_SPECIAL) {
763 found = 0; /* unused -- avoid compiler warning */
764 break; /* special treatment */
765 }
Adam Langley95c29f32014-06-20 12:00:00 -0700766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 /* check for multi-part specification */
768 if (ch == '+') {
769 multi = 1;
770 l++;
771 } else {
772 multi = 0;
773 }
Adam Langley95c29f32014-06-20 12:00:00 -0700774
Adam Langleyfcf25832014-12-18 17:42:32 -0800775 /* Now search for the cipher alias in the ca_list. Be careful with the
776 * strncmp, because the "buflen" limitation will make the rule "ADH:SOME"
777 * and the cipher "ADH-MY-CIPHER" look like a match for buflen=3. So
778 * additionally check whether the cipher name found has the correct
779 * length. We can save a strlen() call: just checking for the '\0' at the
780 * right place is sufficient, we have to strncmp() anyway. (We cannot use
781 * strcmp(), because buf is not '\0' terminated.) */
782 j = found = 0;
783 cipher_id = 0;
784 while (ca_list[j]) {
785 if (!strncmp(buf, ca_list[j]->name, buflen) &&
786 (ca_list[j]->name[buflen] == '\0')) {
787 found = 1;
788 break;
789 } else {
790 j++;
791 }
792 }
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 if (!found) {
795 break; /* ignore this entry */
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 if (ca_list[j]->algorithm_mkey) {
799 if (alg_mkey) {
800 alg_mkey &= ca_list[j]->algorithm_mkey;
801 if (!alg_mkey) {
802 found = 0;
803 break;
804 }
805 } else {
806 alg_mkey = ca_list[j]->algorithm_mkey;
807 }
808 }
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 if (ca_list[j]->algorithm_auth) {
811 if (alg_auth) {
812 alg_auth &= ca_list[j]->algorithm_auth;
813 if (!alg_auth) {
814 found = 0;
815 break;
816 }
817 } else {
818 alg_auth = ca_list[j]->algorithm_auth;
819 }
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 if (ca_list[j]->algorithm_enc) {
823 if (alg_enc) {
824 alg_enc &= ca_list[j]->algorithm_enc;
825 if (!alg_enc) {
826 found = 0;
827 break;
828 }
829 } else {
830 alg_enc = ca_list[j]->algorithm_enc;
831 }
832 }
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 if (ca_list[j]->algorithm_mac) {
835 if (alg_mac) {
836 alg_mac &= ca_list[j]->algorithm_mac;
837 if (!alg_mac) {
838 found = 0;
839 break;
840 }
841 } else {
842 alg_mac = ca_list[j]->algorithm_mac;
843 }
844 }
Adam Langley858a88d2014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 if (ca_list[j]->algo_strength) {
847 if (algo_strength) {
848 algo_strength &= ca_list[j]->algo_strength;
849 if (!algo_strength) {
850 found = 0;
851 break;
852 }
853 } else {
854 algo_strength |= ca_list[j]->algo_strength;
855 }
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 if (ca_list[j]->valid) {
859 /* explicit ciphersuite found; its protocol version does not become
860 * part of the search pattern! */
861 cipher_id = ca_list[j]->id;
862 } else {
863 /* not an explicit ciphersuite; only in this case, the protocol version
864 * is considered part of the search pattern. */
865 if (ca_list[j]->algorithm_ssl) {
866 if (alg_ssl) {
867 alg_ssl &= ca_list[j]->algorithm_ssl;
868 if (!alg_ssl) {
869 found = 0;
870 break;
871 }
872 } else {
873 alg_ssl = ca_list[j]->algorithm_ssl;
874 }
875 }
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 if (!multi) {
879 break;
880 }
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 /* Ok, we have the rule, now apply it. */
884 if (rule == CIPHER_SPECIAL) {
885 /* special command */
886 ok = 0;
887 if (buflen == 8 && !strncmp(buf, "STRENGTH", 8)) {
888 ok = ssl_cipher_strength_sort(head_p, tail_p);
889 } else {
890 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr,
891 SSL_R_INVALID_COMMAND);
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 if (ok == 0) {
895 retval = 0;
896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 /* We do not support any "multi" options together with "@", so throw away
899 * the rest of the command, if any left, until end or ':' is found. */
900 while (*l != '\0' && !ITEM_SEP(*l)) {
901 l++;
902 }
903 } else if (found) {
904 ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
905 alg_ssl, algo_strength, rule, -1, in_group, head_p,
906 tail_p);
907 } else {
908 while (*l != '\0' && !ITEM_SEP(*l)) {
909 l++;
910 }
911 }
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langleyfcf25832014-12-18 17:42:32 -0800914 if (in_group) {
915 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
916 retval = 0;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 return retval;
920}
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922STACK_OF(SSL_CIPHER) *
923ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
924 struct ssl_cipher_preference_list_st **cipher_list,
925 STACK_OF(SSL_CIPHER) * *cipher_list_by_id,
926 const char *rule_str, CERT *c) {
927 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
928 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
929 const char *rule_p;
930 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
931 const SSL_CIPHER **ca_list = NULL;
932 uint8_t *in_group_flags = NULL;
933 unsigned int num_in_group_flags = 0;
934 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleyfcf25832014-12-18 17:42:32 -0800936 /* Return with error if nothing to do. */
937 if (rule_str == NULL || cipher_list == NULL) {
938 return NULL;
939 }
David Benjamin5213df42014-08-20 14:19:54 -0400940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 /* Now we have to collect the available ciphers from the compiled in ciphers.
942 * We cannot get more than the number compiled in, so it is used for
943 * allocation. */
944 num_of_ciphers = ssl_method->num_ciphers();
945 co_list =
946 (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
947 if (co_list == NULL) {
948 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
949 return NULL;
950 }
Adam Langley95c29f32014-06-20 12:00:00 -0700951
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 /* Now arrange all ciphers by preference:
955 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
958 * key exchange mechanisms */
959 ssl_cipher_apply_rule(0, SSL_kEECDH, SSL_aECDSA, 0, 0, 0, 0, CIPHER_ADD, -1,
960 0, &head, &tail);
961 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
962 &tail);
963 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head,
964 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
967 * CHACHA20 unless there is hardware support for fast and constant-time
968 * AES_GCM. */
969 if (EVP_has_aes_hardware()) {
970 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
971 &head, &tail);
972 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
973 &head, &tail);
974 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
975 -1, 0, &head, &tail);
976 } else {
977 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD,
978 -1, 0, &head, &tail);
979 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
980 &head, &tail);
981 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0,
982 &head, &tail);
983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langleyfcf25832014-12-18 17:42:32 -0800985 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
986 * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
987 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
988 &tail);
989 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
990 &tail);
991 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
992 &head, &tail);
993 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0,
994 &head, &tail);
995 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head,
996 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 /* Temporarily enable everything else for sorting */
999 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 /* Move ciphers without forward secrecy to the end. */
1002 ssl_cipher_apply_rule(0, ~(SSL_kEDH | SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD,
1003 -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1006 * (For applications that allow them, they aren't too bad, but we prefer
1007 * authenticated ciphers.)
1008 * TODO(davidben): Remove them altogether? */
1009 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head,
1010 &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 /* Now disable everything (maintaining the ordering!) */
1013 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 /* We also need cipher aliases for selecting based on the rule_str. There
1016 * might be two types of entries in the rule_str: 1) names of ciphers
1017 * themselves 2) aliases for groups of ciphers. For 1) we need the available
1018 * ciphers and for 2) the cipher groups of cipher_aliases added together in
1019 * one list (otherwise we would be happy with just the cipher_aliases
1020 * table). */
1021 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1022 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1023 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1024 if (ca_list == NULL) {
1025 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1026 goto err;
1027 }
1028 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001029
Adam Langleyfcf25832014-12-18 17:42:32 -08001030 /* If the rule_string begins with DEFAULT, apply the default rule before
1031 * using the (possibly available) additional rules. */
1032 ok = 1;
1033 rule_p = rule_str;
1034 if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1035 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail,
1036 ca_list);
1037 rule_p += 7;
1038 if (*rule_p == ':') {
1039 rule_p++;
1040 }
1041 }
Adam Langley858a88d2014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 if (ok && strlen(rule_p) > 0) {
1044 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Nick Harper4dd053e2014-12-22 14:58:54 -08001047 OPENSSL_free((void *)ca_list); /* Not needed anymore */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001048
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 if (!ok) {
1050 goto err;
1051 }
1052
1053 /* Allocate new "cipherstack" for the result, return with error
1054 * if we cannot get one. */
1055 cipherstack = sk_SSL_CIPHER_new_null();
1056 if (cipherstack == NULL) {
1057 goto err;
1058 }
1059
1060 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1061 if (!in_group_flags) {
1062 goto err;
1063 }
1064
1065 /* The cipher selection for the list is done. The ciphers are added
1066 * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
1067 for (curr = head; curr != NULL; curr = curr->next) {
1068 if (curr->active) {
David Benjamin2adb7ec2015-01-11 19:59:06 -05001069 if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
1070 goto err;
1071 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 in_group_flags[num_in_group_flags++] = curr->in_group;
1073 }
1074 }
1075 OPENSSL_free(co_list); /* Not needed any longer */
1076 co_list = NULL;
1077
1078 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1079 if (tmp_cipher_list == NULL) {
1080 goto err;
1081 }
1082 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1083 if (!pref_list) {
1084 goto err;
1085 }
1086 pref_list->ciphers = cipherstack;
1087 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1088 if (!pref_list->in_group_flags) {
1089 goto err;
1090 }
1091 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1092 OPENSSL_free(in_group_flags);
1093 in_group_flags = NULL;
1094 if (*cipher_list != NULL) {
1095 ssl_cipher_preference_list_free(*cipher_list);
1096 }
1097 *cipher_list = pref_list;
1098 pref_list = NULL;
1099
1100 if (cipher_list_by_id != NULL) {
1101 if (*cipher_list_by_id != NULL) {
1102 sk_SSL_CIPHER_free(*cipher_list_by_id);
1103 }
1104 *cipher_list_by_id = tmp_cipher_list;
1105 tmp_cipher_list = NULL;
Adam Langleya307dfd2015-01-09 15:42:58 -08001106 (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
Adam Langleyfcf25832014-12-18 17:42:32 -08001107
1108 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1109 } else {
1110 sk_SSL_CIPHER_free(tmp_cipher_list);
1111 tmp_cipher_list = NULL;
1112 }
1113
1114 return cipherstack;
Adam Langley858a88d2014-06-20 12:00:00 -07001115
1116err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 if (co_list) {
1118 OPENSSL_free(co_list);
1119 }
1120 if (in_group_flags) {
1121 OPENSSL_free(in_group_flags);
1122 }
1123 if (cipherstack) {
1124 sk_SSL_CIPHER_free(cipherstack);
1125 }
1126 if (tmp_cipher_list) {
1127 sk_SSL_CIPHER_free(tmp_cipher_list);
1128 }
1129 if (pref_list && pref_list->in_group_flags) {
1130 OPENSSL_free(pref_list->in_group_flags);
1131 }
1132 if (pref_list) {
1133 OPENSSL_free(pref_list);
1134 }
1135 return NULL;
1136}
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langleyfcf25832014-12-18 17:42:32 -08001138const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
1139 int len) {
1140 const char *ver;
1141 const char *kx, *au, *enc, *mac;
1142 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1143 static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 alg_mkey = cipher->algorithm_mkey;
1146 alg_auth = cipher->algorithm_auth;
1147 alg_enc = cipher->algorithm_enc;
1148 alg_mac = cipher->algorithm_mac;
1149 alg_ssl = cipher->algorithm_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 if (alg_ssl & SSL_SSLV3) {
1152 ver = "SSLv3";
1153 } else if (alg_ssl & SSL_TLSV1_2) {
1154 ver = "TLSv1.2";
1155 } else {
1156 ver = "unknown";
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 switch (alg_mkey) {
1160 case SSL_kRSA:
1161 kx = "RSA";
1162 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001163
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 case SSL_kEDH:
1165 kx = "DH";
1166 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 case SSL_kEECDH:
1169 kx = "ECDH";
1170 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 case SSL_kPSK:
1173 kx = "PSK";
1174 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 default:
1177 kx = "unknown";
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 switch (alg_auth) {
1181 case SSL_aRSA:
1182 au = "RSA";
1183 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 case SSL_aNULL:
1186 au = "None";
1187 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001188
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 case SSL_aECDSA:
1190 au = "ECDSA";
1191 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001192
Adam Langleyfcf25832014-12-18 17:42:32 -08001193 case SSL_aPSK:
1194 au = "PSK";
1195 break;
Adam Langley4d4bff82014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 default:
1198 au = "unknown";
1199 break;
1200 }
Adam Langleyde0b2022014-06-20 12:00:00 -07001201
Adam Langleyfcf25832014-12-18 17:42:32 -08001202 switch (alg_enc) {
1203 case SSL_3DES:
1204 enc = "3DES(168)";
1205 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 case SSL_RC4:
1208 enc = "RC4(128)";
1209 break;
1210
1211 case SSL_AES128:
1212 enc = "AES(128)";
1213 break;
1214
1215 case SSL_AES256:
1216 enc = "AES(256)";
1217 break;
1218
1219 case SSL_AES128GCM:
1220 enc = "AESGCM(128)";
1221 break;
1222
1223 case SSL_AES256GCM:
1224 enc = "AESGCM(256)";
1225 break;
1226
1227 case SSL_CHACHA20POLY1305:
1228 enc = "ChaCha20-Poly1305";
1229 break;
1230
1231 default:
1232 enc = "unknown";
1233 break;
1234 }
1235
1236 switch (alg_mac) {
1237 case SSL_MD5:
1238 mac = "MD5";
1239 break;
1240
1241 case SSL_SHA1:
1242 mac = "SHA1";
1243 break;
1244
1245 case SSL_SHA256:
1246 mac = "SHA256";
1247 break;
1248
1249 case SSL_SHA384:
1250 mac = "SHA384";
1251 break;
1252
1253 case SSL_AEAD:
1254 mac = "AEAD";
1255 break;
1256
1257 default:
1258 mac = "unknown";
1259 break;
1260 }
1261
1262 if (buf == NULL) {
1263 len = 128;
1264 buf = OPENSSL_malloc(len);
David Benjamin1eed2c02015-02-08 23:20:06 -05001265 if (buf == NULL) {
1266 return NULL;
1267 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001268 } else if (len < 128) {
1269 return "Buffer too small";
1270 }
1271
1272 BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
1273 return buf;
1274}
1275
1276int SSL_CIPHER_is_AES(const SSL_CIPHER *c) {
1277 return (c->algorithm_enc & SSL_AES) != 0;
1278}
1279
1280int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) {
1281 return (c->algorithm_mac & SSL_MD5) != 0;
1282}
1283
1284int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) {
1285 return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
1286}
1287
1288int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) {
1289 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1290}
1291
1292const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) {
1293 int i;
1294
1295 if (c == NULL) {
1296 return "(NONE)";
1297 }
1298
1299 i = (int)(c->id >> 24L);
1300 if (i == 3) {
1301 return "TLSv1/SSLv3";
1302 } else if (i == 2) {
1303 return "SSLv2";
1304 } else {
1305 return "unknown";
1306 }
1307}
Adam Langley95c29f32014-06-20 12:00:00 -07001308
1309/* return the actual cipher being used */
Adam Langleyfcf25832014-12-18 17:42:32 -08001310const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) {
1311 if (c != NULL) {
1312 return c->name;
1313 }
1314
1315 return "(NONE)";
1316}
Adam Langley95c29f32014-06-20 12:00:00 -07001317
Adam Langleyf1df2b32014-08-11 17:37:53 -07001318const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1319 if (cipher == NULL) {
1320 return "";
1321 }
1322
1323 switch (cipher->algorithm_mkey) {
1324 case SSL_kRSA:
1325 return SSL_TXT_RSA;
Adam Langleyfcf25832014-12-18 17:42:32 -08001326
Adam Langleyf1df2b32014-08-11 17:37:53 -07001327 case SSL_kEDH:
1328 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001329 case SSL_aRSA:
1330 return "DHE_" SSL_TXT_RSA;
1331 case SSL_aNULL:
1332 return SSL_TXT_DH "_anon";
1333 default:
1334 return "UNKNOWN";
1335 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001336
Adam Langleyf1df2b32014-08-11 17:37:53 -07001337 case SSL_kEECDH:
1338 switch (cipher->algorithm_auth) {
1339 case SSL_aECDSA:
1340 return "ECDHE_" SSL_TXT_ECDSA;
1341 case SSL_aRSA:
1342 return "ECDHE_" SSL_TXT_RSA;
1343 case SSL_aNULL:
1344 return SSL_TXT_ECDH "_anon";
1345 default:
1346 return "UNKNOWN";
1347 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001348
Adam Langleyf1df2b32014-08-11 17:37:53 -07001349 default:
1350 return "UNKNOWN";
1351 }
1352}
1353
Adam Langley95c29f32014-06-20 12:00:00 -07001354/* number of bits for symmetric cipher */
Adam Langleyfcf25832014-12-18 17:42:32 -08001355int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) {
1356 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 if (c != NULL) {
1359 if (alg_bits != NULL) {
1360 *alg_bits = c->alg_bits;
1361 }
1362 ret = c->strength_bits;
1363 }
Adam Langley95c29f32014-06-20 12:00:00 -07001364
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 return ret;
1366}
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; }
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370void *SSL_COMP_get_compression_methods(void) { return NULL; }
1371
1372int SSL_COMP_add_compression_method(int id, void *cm) { return 1; }
1373
1374const char *SSL_COMP_get_name(const void *comp) { return NULL; }
Adam Langley95c29f32014-06-20 12:00:00 -07001375
1376/* For a cipher return the index corresponding to the certificate type */
Adam Langleyfcf25832014-12-18 17:42:32 -08001377int ssl_cipher_get_cert_index(const SSL_CIPHER *c) {
1378 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 if (alg_a & SSL_aECDSA) {
1381 return SSL_PKEY_ECC;
1382 } else if (alg_a & SSL_aRSA) {
1383 return SSL_PKEY_RSA_ENC;
1384 }
Adam Langley95c29f32014-06-20 12:00:00 -07001385
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 return -1;
1387}
David Benjamin9c651c92014-07-12 13:27:45 -04001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server
1390 * public key in the key exchange, sent in a server Certificate message.
1391 * Otherwise it returns 0. */
1392int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
1393 /* Anonymous ciphers do not include a server certificate. */
1394 if (cipher->algorithm_auth & SSL_aNULL) {
1395 return 0;
1396 }
1397
1398 /* Neither do PSK ciphers, except for RSA_PSK. */
1399 if ((cipher->algorithm_auth & SSL_aPSK) &&
1400 !(cipher->algorithm_mkey & SSL_kRSA)) {
1401 return 0;
1402 }
1403
1404 /* All other ciphers include it. */
1405 return 1;
1406}
1407
1408/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a
1409 * ServerKeyExchange message. Otherwise it returns 0.
David Benjamin9c651c92014-07-12 13:27:45 -04001410 *
1411 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to
1413 * communicate a psk_identity_hint, so it is optional. */
1414int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
1415 /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
1416 if (cipher->algorithm_mkey & SSL_kEDH || cipher->algorithm_mkey & SSL_kEECDH) {
1417 return 1;
1418 }
1419
1420 /* It is optional in all others. */
1421 return 0;
1422}