blob: 37c7e433c0d3d85a6d62a089805ff2d72d0f0168 [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>
145#include <openssl/mem.h>
146#include <openssl/obj.h>
147
148#include "ssl_locl.h"
149
David Benjamind633d632014-08-30 14:43:27 -0400150struct handshake_digest
151 {
152 long mask;
153 const EVP_MD *(*md_func)(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700154 };
155
David Benjamind633d632014-08-30 14:43:27 -0400156static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
157 { SSL_HANDSHAKE_MAC_MD5, EVP_md5 },
158 { SSL_HANDSHAKE_MAC_SHA, EVP_sha1 },
159 { SSL_HANDSHAKE_MAC_SHA256, EVP_sha256 },
160 { SSL_HANDSHAKE_MAC_SHA384, EVP_sha384 },
161};
Adam Langley95c29f32014-06-20 12:00:00 -0700162
163#define CIPHER_ADD 1
164#define CIPHER_KILL 2
165#define CIPHER_DEL 3
166#define CIPHER_ORD 4
167#define CIPHER_SPECIAL 5
168
169typedef struct cipher_order_st
170 {
171 const SSL_CIPHER *cipher;
172 int active;
173 int dead;
Adam Langley858a88d2014-06-20 12:00:00 -0700174 int in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700175 struct cipher_order_st *next,*prev;
176 } CIPHER_ORDER;
177
178static const SSL_CIPHER cipher_aliases[]={
David Benjaminebf42b52014-08-19 15:18:42 -0400179 {0,SSL_TXT_ALL,0, 0,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700180
181 /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
David Benjaminebf42b52014-08-19 15:18:42 -0400182 {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700183
184 /* key exchange aliases
185 * (some of those using only a single bit here combine
186 * multiple key exchange algs according to the RFCs,
187 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
188 {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
189
Adam Langley95c29f32014-06-20 12:00:00 -0700190 {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400191 {0,SSL_TXT_DH,0, SSL_kEDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400194 {0,SSL_TXT_ECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700195
196 {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700197
198 /* server authentication aliases */
199 {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700200 {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700201 {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
202 {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
203 {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700204
205 /* aliases combining key exchange and server authentication */
206 {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
207 {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700208 {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
209 {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
210 {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
211 {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700212
213
214 /* symmetric encryption aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700215 {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
216 {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700217 {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
218 {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
219 {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
220 {0,SSL_TXT_AES_GCM,0, 0,0,SSL_AES128GCM|SSL_AES256GCM,0,0,0,0,0,0},
Adam Langleyde0b2022014-06-20 12:00:00 -0700221 {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700222
223 /* MAC aliases */
224 {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
225 {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
226 {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700227 {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
228 {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
229
230 /* protocol version aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700231 {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
232 {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
233 {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
234
Adam Langley95c29f32014-06-20 12:00:00 -0700235 /* strength classes */
Adam Langley95c29f32014-06-20 12:00:00 -0700236 {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
237 {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
238 /* FIPS 140-2 approved ciphersuite */
David Benjaminebf42b52014-08-19 15:18:42 -0400239 {0,SSL_TXT_FIPS,0, 0,0,0,0,0,SSL_FIPS, 0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700240 };
241
Adam Langleyc9fb3752014-06-20 12:00:00 -0700242/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
243 * for |s->cipher|. It returns 1 on success and 0 on error. */
244int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
245 {
246 const SSL_CIPHER *c = s->cipher;
247
248 *aead = NULL;
249
250 if (c == NULL)
251 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700252 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0 &&
253 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) == 0)
Adam Langleyc9fb3752014-06-20 12:00:00 -0700254 return 0;
255
Adam Langleyde0b2022014-06-20 12:00:00 -0700256 switch (c->algorithm_enc)
257 {
258 case SSL_AES128GCM:
259 *aead = EVP_aead_aes_128_gcm();
260 return 1;
Adam Langley5c270c52014-06-20 12:00:00 -0700261 case SSL_AES256GCM:
262 *aead = EVP_aead_aes_256_gcm();
263 return 1;
Adam Langleyde0b2022014-06-20 12:00:00 -0700264 case SSL_CHACHA20POLY1305:
265 *aead = EVP_aead_chacha20_poly1305();
266 return 1;
Adam Langley9447dff2014-06-24 17:29:06 -0700267 case SSL_RC4:
268 if (c->algorithm_mac == SSL_MD5)
269 *aead = EVP_aead_rc4_md5_tls();
270 else
271 return 0;
272 return 1;
273 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700274
275 return 0;
276 }
277
278int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
279 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
280 {
Adam Langley95c29f32014-06-20 12:00:00 -0700281 const SSL_CIPHER *c;
282
283 c=s->cipher;
284 if (c == NULL) return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langleyc9fb3752014-06-20 12:00:00 -0700286 /* This function doesn't deal with EVP_AEAD. See
287 * |ssl_cipher_get_aead_evp|. */
288 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
289 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700290
291 if ((enc == NULL) || (md == NULL)) return(0);
292
293 switch (c->algorithm_enc)
294 {
Adam Langley95c29f32014-06-20 12:00:00 -0700295 case SSL_3DES:
David Benjamind633d632014-08-30 14:43:27 -0400296 *enc = EVP_des_ede3_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700297 break;
298 case SSL_RC4:
David Benjamind633d632014-08-30 14:43:27 -0400299 *enc = EVP_rc4();
Adam Langley95c29f32014-06-20 12:00:00 -0700300 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700301 case SSL_AES128:
David Benjamind633d632014-08-30 14:43:27 -0400302 *enc = EVP_aes_128_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700303 break;
304 case SSL_AES256:
David Benjamind633d632014-08-30 14:43:27 -0400305 *enc = EVP_aes_256_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700306 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700307 default:
David Benjamind633d632014-08-30 14:43:27 -0400308 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700309 }
310
Adam Langley9447dff2014-06-24 17:29:06 -0700311 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size))
312 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
David Benjamind633d632014-08-30 14:43:27 -0400314 assert(*enc != NULL && *md != NULL);
315
316 /* TODO(fork): enable the stitched cipher modes. */
317#if 0
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
319 s->ssl_version < TLS1_VERSION)
320 return 1;
321
Adam Langley95c29f32014-06-20 12:00:00 -0700322 if (c->algorithm_enc == SSL_RC4 &&
323 c->algorithm_mac == SSL_MD5 &&
324 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
325 *enc = evp, *md = NULL;
326 else if (c->algorithm_enc == SSL_AES128 &&
327 c->algorithm_mac == SSL_SHA1 &&
328 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
329 *enc = evp, *md = NULL;
330 else if (c->algorithm_enc == SSL_AES256 &&
331 c->algorithm_mac == SSL_SHA1 &&
332 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
333 *enc = evp, *md = NULL;
334#endif
David Benjamind633d632014-08-30 14:43:27 -0400335
336 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700337 }
338
Adam Langley9447dff2014-06-24 17:29:06 -0700339int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
340 {
Adam Langley9447dff2014-06-24 17:29:06 -0700341 const SSL_CIPHER *c;
342
343 c=s->cipher;
344 if (c == NULL) return(0);
345
346 switch (c->algorithm_mac)
347 {
348 case SSL_MD5:
David Benjamind633d632014-08-30 14:43:27 -0400349 *md = EVP_md5();
Adam Langley9447dff2014-06-24 17:29:06 -0700350 break;
351 case SSL_SHA1:
David Benjamind633d632014-08-30 14:43:27 -0400352 *md = EVP_sha1();
Adam Langley9447dff2014-06-24 17:29:06 -0700353 break;
354 case SSL_SHA256:
David Benjamind633d632014-08-30 14:43:27 -0400355 *md = EVP_sha256();
Adam Langley9447dff2014-06-24 17:29:06 -0700356 break;
357 case SSL_SHA384:
David Benjamind633d632014-08-30 14:43:27 -0400358 *md = EVP_sha384();
Adam Langley9447dff2014-06-24 17:29:06 -0700359 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700360 default:
David Benjamind633d632014-08-30 14:43:27 -0400361 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700362 }
363
David Benjamind633d632014-08-30 14:43:27 -0400364 if (mac_pkey_type != NULL)
Adam Langley9447dff2014-06-24 17:29:06 -0700365 {
David Benjamind633d632014-08-30 14:43:27 -0400366 *mac_pkey_type = EVP_PKEY_HMAC;
Adam Langley9447dff2014-06-24 17:29:06 -0700367 }
David Benjamind633d632014-08-30 14:43:27 -0400368 if (mac_secret_size!=NULL)
Adam Langley9447dff2014-06-24 17:29:06 -0700369 {
David Benjamind633d632014-08-30 14:43:27 -0400370 *mac_secret_size = EVP_MD_size(*md);
Adam Langley9447dff2014-06-24 17:29:06 -0700371 }
Adam Langley9447dff2014-06-24 17:29:06 -0700372 return 1;
373 }
374
Adam Langley95c29f32014-06-20 12:00:00 -0700375int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
376{
David Benjamind633d632014-08-30 14:43:27 -0400377 if (idx < 0 || idx >= SSL_MAX_DIGEST)
Adam Langley95c29f32014-06-20 12:00:00 -0700378 {
379 return 0;
380 }
David Benjamind633d632014-08-30 14:43:27 -0400381 *mask = ssl_handshake_digests[idx].mask;
382 *md = ssl_handshake_digests[idx].md_func();
Adam Langley95c29f32014-06-20 12:00:00 -0700383 return 1;
384}
385
386#define ITEM_SEP(a) \
387 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
388
389static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
390 CIPHER_ORDER **tail)
391 {
392 if (curr == *tail) return;
393 if (curr == *head)
394 *head=curr->next;
395 if (curr->prev != NULL)
396 curr->prev->next=curr->next;
397 if (curr->next != NULL)
398 curr->next->prev=curr->prev;
399 (*tail)->next=curr;
400 curr->prev= *tail;
401 curr->next=NULL;
402 *tail=curr;
403 }
404
405static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
406 CIPHER_ORDER **tail)
407 {
408 if (curr == *head) return;
409 if (curr == *tail)
410 *tail=curr->prev;
411 if (curr->next != NULL)
412 curr->next->prev=curr->prev;
413 if (curr->prev != NULL)
414 curr->prev->next=curr->next;
415 (*head)->prev=curr;
416 curr->next= *head;
417 curr->prev=NULL;
418 *head=curr;
419 }
420
David Benjamin82c9e902014-12-12 15:55:27 -0500421static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langley95c29f32014-06-20 12:00:00 -0700422 int num_of_ciphers,
Adam Langley95c29f32014-06-20 12:00:00 -0700423 CIPHER_ORDER *co_list,
424 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
425 {
426 int i, co_list_num;
427 const SSL_CIPHER *c;
428
429 /*
430 * We have num_of_ciphers descriptions compiled in, depending on the
431 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
432 * These will later be sorted in a linked list with at most num
433 * entries.
434 */
435
436 /* Get the initial list of ciphers */
437 co_list_num = 0; /* actual count of ciphers */
438 for (i = 0; i < num_of_ciphers; i++)
439 {
440 c = ssl_method->get_cipher(i);
441 /* drop those that use any of that is not available */
David Benjamind633d632014-08-30 14:43:27 -0400442 if ((c != NULL) && c->valid)
Adam Langley95c29f32014-06-20 12:00:00 -0700443 {
444 co_list[co_list_num].cipher = c;
445 co_list[co_list_num].next = NULL;
446 co_list[co_list_num].prev = NULL;
447 co_list[co_list_num].active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700448 co_list[co_list_num].in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700449 co_list_num++;
Adam Langley95c29f32014-06-20 12:00:00 -0700450 }
451 }
452
453 /*
454 * Prepare linked list from list entries
455 */
456 if (co_list_num > 0)
457 {
458 co_list[0].prev = NULL;
459
460 if (co_list_num > 1)
461 {
462 co_list[0].next = &co_list[1];
463
464 for (i = 1; i < co_list_num - 1; i++)
465 {
466 co_list[i].prev = &co_list[i - 1];
467 co_list[i].next = &co_list[i + 1];
468 }
469
470 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
471 }
472
473 co_list[co_list_num - 1].next = NULL;
474
475 *head_p = &co_list[0];
476 *tail_p = &co_list[co_list_num - 1];
477 }
478 }
479
480static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
481 int num_of_group_aliases,
Adam Langley95c29f32014-06-20 12:00:00 -0700482 CIPHER_ORDER *head)
483 {
484 CIPHER_ORDER *ciph_curr;
485 const SSL_CIPHER **ca_curr;
486 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
488 /*
489 * First, add the real ciphers as already collected
490 */
491 ciph_curr = head;
492 ca_curr = ca_list;
493 while (ciph_curr != NULL)
494 {
495 *ca_curr = ciph_curr->cipher;
496 ca_curr++;
497 ciph_curr = ciph_curr->next;
498 }
499
500 /*
501 * Now we add the available ones from the cipher_aliases[] table.
502 * They represent either one or more algorithms, some of which
503 * in any affected category must be supported (set in enabled_mask),
504 * or represent a cipher strength value (will be added in any case because algorithms=0).
505 */
506 for (i = 0; i < num_of_group_aliases; i++)
507 {
David Benjamin6f260012014-08-15 13:49:12 -0400508 *ca_curr = cipher_aliases + i;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 ca_curr++;
510 }
511
512 *ca_curr = NULL; /* end of list */
513 }
514
515static void ssl_cipher_apply_rule(unsigned long cipher_id,
516 unsigned long alg_mkey, unsigned long alg_auth,
517 unsigned long alg_enc, unsigned long alg_mac,
518 unsigned long alg_ssl,
519 unsigned long algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700520 int rule, int strength_bits, int in_group,
Adam Langley95c29f32014-06-20 12:00:00 -0700521 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
522 {
Adam Langleye3142a72014-07-24 17:56:48 -0700523 CIPHER_ORDER *head, *tail, *curr, *next, *last;
Adam Langley95c29f32014-06-20 12:00:00 -0700524 const SSL_CIPHER *cp;
525 int reverse = 0;
526
527#ifdef CIPHER_DEBUG
Adam Langley858a88d2014-06-20 12:00:00 -0700528 printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
529 rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
Adam Langley95c29f32014-06-20 12:00:00 -0700530#endif
531
532 if (rule == CIPHER_DEL)
533 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
534
535 head = *head_p;
536 tail = *tail_p;
537
538 if (reverse)
539 {
Adam Langleye3142a72014-07-24 17:56:48 -0700540 next = tail;
Adam Langley95c29f32014-06-20 12:00:00 -0700541 last = head;
542 }
543 else
544 {
Adam Langleye3142a72014-07-24 17:56:48 -0700545 next = head;
Adam Langley95c29f32014-06-20 12:00:00 -0700546 last = tail;
547 }
548
Adam Langleye3142a72014-07-24 17:56:48 -0700549 curr = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700550 for (;;)
551 {
Adam Langleye3142a72014-07-24 17:56:48 -0700552 if (curr == last) break;
553
554 curr = next;
555
556 if (curr == NULL) break;
557
558 next = reverse ? curr->prev : curr->next;
Adam Langley95c29f32014-06-20 12:00:00 -0700559
560 cp = curr->cipher;
561
562 /*
563 * Selection criteria is either the value of strength_bits
564 * or the algorithms used.
565 */
566 if (strength_bits >= 0)
567 {
568 if (strength_bits != cp->strength_bits)
569 continue;
570 }
571 else
572 {
573#ifdef CIPHER_DEBUG
574 printf("\nName: %s:\nAlgo = %08lx/%08lx/%08lx/%08lx/%08lx Algo_strength = %08lx\n", cp->name, cp->algorithm_mkey, cp->algorithm_auth, cp->algorithm_enc, cp->algorithm_mac, cp->algorithm_ssl, cp->algo_strength);
575#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700576 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
577 continue;
578 if (alg_auth && !(alg_auth & cp->algorithm_auth))
579 continue;
580 if (alg_enc && !(alg_enc & cp->algorithm_enc))
581 continue;
582 if (alg_mac && !(alg_mac & cp->algorithm_mac))
583 continue;
584 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
585 continue;
David Benjamin594a58e2014-09-07 13:51:08 -0400586 if (algo_strength && !(algo_strength & cp->algo_strength))
Adam Langley95c29f32014-06-20 12:00:00 -0700587 continue;
588 }
589
590#ifdef CIPHER_DEBUG
591 printf("Action = %d\n", rule);
592#endif
593
594 /* add the cipher if it has not been added yet. */
595 if (rule == CIPHER_ADD)
596 {
597 /* reverse == 0 */
598 if (!curr->active)
599 {
600 ll_append_tail(&head, curr, &tail);
601 curr->active = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700602 curr->in_group = in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700603 }
604 }
605 /* Move the added cipher to this location */
606 else if (rule == CIPHER_ORD)
607 {
608 /* reverse == 0 */
609 if (curr->active)
610 {
611 ll_append_tail(&head, curr, &tail);
Adam Langley858a88d2014-06-20 12:00:00 -0700612 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700613 }
614 }
615 else if (rule == CIPHER_DEL)
616 {
617 /* reverse == 1 */
618 if (curr->active)
619 {
620 /* most recently deleted ciphersuites get best positions
621 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
622 * works in reverse to maintain the order) */
623 ll_append_head(&head, curr, &tail);
624 curr->active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700625 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700626 }
627 }
628 else if (rule == CIPHER_KILL)
629 {
630 /* reverse == 0 */
631 if (head == curr)
632 head = curr->next;
633 else
634 curr->prev->next = curr->next;
635 if (tail == curr)
636 tail = curr->prev;
637 curr->active = 0;
638 if (curr->next != NULL)
639 curr->next->prev = curr->prev;
640 if (curr->prev != NULL)
641 curr->prev->next = curr->next;
642 curr->next = NULL;
643 curr->prev = NULL;
644 }
645 }
646
647 *head_p = head;
648 *tail_p = tail;
649 }
650
651static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
652 CIPHER_ORDER **tail_p)
653 {
654 int max_strength_bits, i, *number_uses;
655 CIPHER_ORDER *curr;
656
657 /*
658 * This routine sorts the ciphers with descending strength. The sorting
659 * must keep the pre-sorted sequence, so we apply the normal sorting
660 * routine as '+' movement to the end of the list.
661 */
662 max_strength_bits = 0;
663 curr = *head_p;
664 while (curr != NULL)
665 {
666 if (curr->active &&
667 (curr->cipher->strength_bits > max_strength_bits))
668 max_strength_bits = curr->cipher->strength_bits;
669 curr = curr->next;
670 }
671
672 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
673 if (!number_uses)
674 {
675 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
676 return(0);
677 }
678 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
679
680 /*
681 * Now find the strength_bits values actually used
682 */
683 curr = *head_p;
684 while (curr != NULL)
685 {
686 if (curr->active)
687 number_uses[curr->cipher->strength_bits]++;
688 curr = curr->next;
689 }
690 /*
691 * Go through the list of used strength_bits values in descending
692 * order.
693 */
694 for (i = max_strength_bits; i >= 0; i--)
695 if (number_uses[i] > 0)
Adam Langley858a88d2014-06-20 12:00:00 -0700696 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -0700697
698 OPENSSL_free(number_uses);
699 return(1);
700 }
701
702static int ssl_cipher_process_rulestr(const char *rule_str,
703 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
704 const SSL_CIPHER **ca_list)
705 {
706 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
707 const char *l, *buf;
Adam Langley858a88d2014-06-20 12:00:00 -0700708 int j, multi, found, rule, retval, ok, buflen, in_group = 0,
709 has_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700710 unsigned long cipher_id = 0;
711 char ch;
712
713 retval = 1;
714 l = rule_str;
715 for (;;)
716 {
717 ch = *l;
718
719 if (ch == '\0')
720 break; /* done */
Adam Langley858a88d2014-06-20 12:00:00 -0700721 if (in_group)
722 {
723 if (ch == ']')
724 {
725 if (!in_group)
726 {
727 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
728 retval = found = in_group = 0;
729 break;
730 }
731 if (*tail_p)
732 (*tail_p)->in_group = 0;
733 in_group = 0;
734 l++;
735 continue;
736 }
737 if (ch == '|')
738 { rule = CIPHER_ADD; l++; continue; }
739 else if (!(ch >= 'a' && ch <= 'z') &&
740 !(ch >= 'A' && ch <= 'Z') &&
741 !(ch >= '0' && ch <= '9'))
742 {
743 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
744 retval = found = in_group = 0;
745 break;
746 }
747 else
748 rule = CIPHER_ADD;
749 }
750 else if (ch == '-')
Adam Langley95c29f32014-06-20 12:00:00 -0700751 { rule = CIPHER_DEL; l++; }
752 else if (ch == '+')
753 { rule = CIPHER_ORD; l++; }
754 else if (ch == '!')
755 { rule = CIPHER_KILL; l++; }
756 else if (ch == '@')
757 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700758 else if (ch == '[')
759 {
760 if (in_group)
761 {
762 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
763 retval = found = in_group = 0;
764 break;
765 }
766 in_group = 1;
767 has_group = 1;
768 l++;
769 continue;
770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771 else
772 { rule = CIPHER_ADD; }
773
David Benjamin37d92462014-09-20 17:54:24 -0400774 /* If preference groups are enabled, the only legal
775 * operator is +. Otherwise the in_group bits will get
776 * mixed up. */
777 if (has_group && rule != CIPHER_ADD)
778 {
779 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
780 retval = found = in_group = 0;
781 break;
782 }
783
Adam Langley95c29f32014-06-20 12:00:00 -0700784 if (ITEM_SEP(ch))
785 {
786 l++;
787 continue;
788 }
789
790 alg_mkey = 0;
791 alg_auth = 0;
792 alg_enc = 0;
793 alg_mac = 0;
794 alg_ssl = 0;
795 algo_strength = 0;
796
797 for (;;)
798 {
799 ch = *l;
800 buf = l;
801 buflen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700802 while ( ((ch >= 'A') && (ch <= 'Z')) ||
803 ((ch >= '0') && (ch <= '9')) ||
804 ((ch >= 'a') && (ch <= 'z')) ||
805 (ch == '-') || (ch == '.'))
Adam Langley95c29f32014-06-20 12:00:00 -0700806 {
807 ch = *(++l);
808 buflen++;
809 }
810
811 if (buflen == 0)
812 {
813 /*
814 * We hit something we cannot deal with,
815 * it is no command or separator nor
816 * alphanumeric, so we call this an error.
817 */
818 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -0700819 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700820 l++;
821 break;
822 }
823
824 if (rule == CIPHER_SPECIAL)
825 {
826 found = 0; /* unused -- avoid compiler warning */
827 break; /* special treatment */
828 }
829
830 /* check for multi-part specification */
831 if (ch == '+')
832 {
833 multi=1;
834 l++;
835 }
836 else
837 multi=0;
838
839 /*
840 * Now search for the cipher alias in the ca_list. Be careful
841 * with the strncmp, because the "buflen" limitation
842 * will make the rule "ADH:SOME" and the cipher
843 * "ADH-MY-CIPHER" look like a match for buflen=3.
844 * So additionally check whether the cipher name found
845 * has the correct length. We can save a strlen() call:
846 * just checking for the '\0' at the right place is
847 * sufficient, we have to strncmp() anyway. (We cannot
848 * use strcmp(), because buf is not '\0' terminated.)
849 */
850 j = found = 0;
851 cipher_id = 0;
852 while (ca_list[j])
853 {
854 if (!strncmp(buf, ca_list[j]->name, buflen) &&
855 (ca_list[j]->name[buflen] == '\0'))
856 {
857 found = 1;
858 break;
859 }
860 else
861 j++;
862 }
863
864 if (!found)
865 break; /* ignore this entry */
866
867 if (ca_list[j]->algorithm_mkey)
868 {
869 if (alg_mkey)
870 {
871 alg_mkey &= ca_list[j]->algorithm_mkey;
872 if (!alg_mkey) { found = 0; break; }
873 }
874 else
875 alg_mkey = ca_list[j]->algorithm_mkey;
876 }
877
878 if (ca_list[j]->algorithm_auth)
879 {
880 if (alg_auth)
881 {
882 alg_auth &= ca_list[j]->algorithm_auth;
883 if (!alg_auth) { found = 0; break; }
884 }
885 else
886 alg_auth = ca_list[j]->algorithm_auth;
887 }
888
889 if (ca_list[j]->algorithm_enc)
890 {
891 if (alg_enc)
892 {
893 alg_enc &= ca_list[j]->algorithm_enc;
894 if (!alg_enc) { found = 0; break; }
895 }
896 else
897 alg_enc = ca_list[j]->algorithm_enc;
898 }
899
900 if (ca_list[j]->algorithm_mac)
901 {
902 if (alg_mac)
903 {
904 alg_mac &= ca_list[j]->algorithm_mac;
905 if (!alg_mac) { found = 0; break; }
906 }
907 else
908 alg_mac = ca_list[j]->algorithm_mac;
909 }
910
David Benjamin594a58e2014-09-07 13:51:08 -0400911 if (ca_list[j]->algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700912 {
David Benjamin594a58e2014-09-07 13:51:08 -0400913 if (algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700914 {
David Benjamin594a58e2014-09-07 13:51:08 -0400915 algo_strength &= ca_list[j]->algo_strength;
916 if (!algo_strength) { found = 0; break; }
Adam Langley95c29f32014-06-20 12:00:00 -0700917 }
918 else
David Benjamin594a58e2014-09-07 13:51:08 -0400919 algo_strength |= ca_list[j]->algo_strength;
Adam Langley95c29f32014-06-20 12:00:00 -0700920 }
921
922 if (ca_list[j]->valid)
923 {
924 /* explicit ciphersuite found; its protocol version
925 * does not become part of the search pattern!*/
926
927 cipher_id = ca_list[j]->id;
928 }
929 else
930 {
931 /* not an explicit ciphersuite; only in this case, the
932 * protocol version is considered part of the search pattern */
933
934 if (ca_list[j]->algorithm_ssl)
935 {
936 if (alg_ssl)
937 {
938 alg_ssl &= ca_list[j]->algorithm_ssl;
939 if (!alg_ssl) { found = 0; break; }
940 }
941 else
942 alg_ssl = ca_list[j]->algorithm_ssl;
943 }
944 }
945
946 if (!multi) break;
947 }
948
949 /*
950 * Ok, we have the rule, now apply it
951 */
952 if (rule == CIPHER_SPECIAL)
953 { /* special command */
954 ok = 0;
955 if ((buflen == 8) &&
956 !strncmp(buf, "STRENGTH", 8))
957 ok = ssl_cipher_strength_sort(head_p, tail_p);
958 else
959 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
960 if (ok == 0)
961 retval = 0;
962 /*
963 * We do not support any "multi" options
964 * together with "@", so throw away the
965 * rest of the command, if any left, until
966 * end or ':' is found.
967 */
968 while ((*l != '\0') && !ITEM_SEP(*l))
969 l++;
970 }
971 else if (found)
972 {
973 ssl_cipher_apply_rule(cipher_id,
974 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700975 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -0700976 }
977 else
978 {
979 while ((*l != '\0') && !ITEM_SEP(*l))
980 l++;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982 }
983
Adam Langley858a88d2014-06-20 12:00:00 -0700984 if (in_group)
985 {
986 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
987 retval = 0;
988 }
989
Adam Langley95c29f32014-06-20 12:00:00 -0700990 return(retval);
991 }
Adam Langley95c29f32014-06-20 12:00:00 -0700992
993
David Benjamin82c9e902014-12-12 15:55:27 -0500994STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -0700995 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -0700996 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
997 const char *rule_str, CERT *c)
998 {
999 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
Adam Langley858a88d2014-06-20 12:00:00 -07001000 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001001 const char *rule_p;
1002 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1003 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001004 unsigned char *in_group_flags = NULL;
1005 unsigned int num_in_group_flags = 0;
1006 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001007
1008 /*
1009 * Return with error if nothing to do.
1010 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001011 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001012 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001013
1014 /*
Adam Langley95c29f32014-06-20 12:00:00 -07001015 * Now we have to collect the available ciphers from the compiled
1016 * in ciphers. We cannot get more than the number compiled in, so
1017 * it is used for allocation.
1018 */
1019 num_of_ciphers = ssl_method->num_ciphers();
Adam Langley95c29f32014-06-20 12:00:00 -07001020 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1021 if (co_list == NULL)
1022 {
1023 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1024 return(NULL); /* Failure */
1025 }
1026
1027 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
Adam Langley95c29f32014-06-20 12:00:00 -07001028 co_list, &head, &tail);
1029
1030
David Benjaminede973a2014-08-19 14:12:39 -04001031 /* Now arrange all ciphers by preference:
1032 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001033
1034 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001035 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1036 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001037
David Benjamin5213df42014-08-20 14:19:54 -04001038 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1039 * CHACHA20 unless there is hardware support for fast and constant-time
1040 * AES_GCM. */
1041 if (EVP_has_aes_hardware())
1042 {
1043 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1044 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1045 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1046 }
1047 else
1048 {
1049 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1050 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1051 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1052 }
1053
1054 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
1055 * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjaminede973a2014-08-19 14:12:39 -04001056 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1057 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1058 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1059 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1060 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001061
1062 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001063 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001064
David Benjaminede973a2014-08-19 14:12:39 -04001065 /* Move ciphers without forward secrecy to the end. */
1066 ssl_cipher_apply_rule(0, ~(SSL_kEDH|SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001067
1068 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1069 * (For applications that allow them, they aren't too bad, but we prefer
David Benjaminede973a2014-08-19 14:12:39 -04001070 * authenticated ciphers.)
1071 * TODO(davidben): Remove them altogether? */
Adam Langley858a88d2014-06-20 12:00:00 -07001072 ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langley95c29f32014-06-20 12:00:00 -07001074 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001075 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001076
Adam Langley95c29f32014-06-20 12:00:00 -07001077 /*
1078 * We also need cipher aliases for selecting based on the rule_str.
1079 * There might be two types of entries in the rule_str: 1) names
1080 * of ciphers themselves 2) aliases for groups of ciphers.
1081 * For 1) we need the available ciphers and for 2) the cipher
1082 * groups of cipher_aliases added together in one list (otherwise
1083 * we would be happy with just the cipher_aliases table).
1084 */
1085 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1086 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1087 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1088 if (ca_list == NULL)
1089 {
Adam Langley95c29f32014-06-20 12:00:00 -07001090 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
David Benjamind633d632014-08-30 14:43:27 -04001091 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001092 }
David Benjamind633d632014-08-30 14:43:27 -04001093 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001094
1095 /*
1096 * If the rule_string begins with DEFAULT, apply the default rule
1097 * before using the (possibly available) additional rules.
1098 */
1099 ok = 1;
1100 rule_p = rule_str;
1101 if (strncmp(rule_str,"DEFAULT",7) == 0)
1102 {
1103 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1104 &head, &tail, ca_list);
1105 rule_p += 7;
1106 if (*rule_p == ':')
1107 rule_p++;
1108 }
1109
1110 if (ok && (strlen(rule_p) > 0))
1111 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1112
1113 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1114
1115 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001116 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001117
1118 /*
1119 * Allocate new "cipherstack" for the result, return with error
1120 * if we cannot get one.
1121 */
1122 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001123 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langley858a88d2014-06-20 12:00:00 -07001125 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1126 if (!in_group_flags)
1127 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001128 /*
1129 * The cipher selection for the list is done. The ciphers are added
1130 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1131 */
1132 for (curr = head; curr != NULL; curr = curr->next)
1133 {
Adam Langley95c29f32014-06-20 12:00:00 -07001134 if (curr->active)
Adam Langley95c29f32014-06-20 12:00:00 -07001135 {
1136 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001137 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001138#ifdef CIPHER_DEBUG
1139 printf("<%s>\n",curr->cipher->name);
1140#endif
1141 }
1142 }
1143 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001144 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001145
1146 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1147 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001148 goto err;
1149 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1150 if (!pref_list)
1151 goto err;
1152 pref_list->ciphers = cipherstack;
1153 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1154 if (!pref_list->in_group_flags)
1155 goto err;
1156 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1157 OPENSSL_free(in_group_flags);
1158 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001159 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001160 ssl_cipher_preference_list_free(*cipher_list);
1161 *cipher_list = pref_list;
1162 pref_list = NULL;
1163
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001164 if (cipher_list_by_id != NULL)
1165 {
1166 if (*cipher_list_by_id != NULL)
1167 sk_SSL_CIPHER_free(*cipher_list_by_id);
1168 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001169 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001170 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001171
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001172 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1173 }
1174 else
Adam Langley858a88d2014-06-20 12:00:00 -07001175 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001176 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001177 tmp_cipher_list = NULL;
1178 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001179
Adam Langley95c29f32014-06-20 12:00:00 -07001180 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001181
1182err:
1183 if (co_list)
1184 OPENSSL_free(co_list);
1185 if (in_group_flags)
1186 OPENSSL_free(in_group_flags);
1187 if (cipherstack)
1188 sk_SSL_CIPHER_free(cipherstack);
1189 if (tmp_cipher_list)
1190 sk_SSL_CIPHER_free(tmp_cipher_list);
1191 if (pref_list && pref_list->in_group_flags)
1192 OPENSSL_free(pref_list->in_group_flags);
1193 if (pref_list)
1194 OPENSSL_free(pref_list);
1195 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001196 }
1197
Adam Langley73510762014-06-20 12:00:00 -07001198const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
Adam Langley95c29f32014-06-20 12:00:00 -07001199 {
David Benjamin060d9d22014-07-15 00:54:26 -04001200 const char *ver;
Adam Langley95c29f32014-06-20 12:00:00 -07001201 const char *kx,*au,*enc,*mac;
David Benjaminebf42b52014-08-19 15:18:42 -04001202 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
David Benjamin060d9d22014-07-15 00:54:26 -04001203 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001204
1205 alg_mkey = cipher->algorithm_mkey;
1206 alg_auth = cipher->algorithm_auth;
1207 alg_enc = cipher->algorithm_enc;
1208 alg_mac = cipher->algorithm_mac;
1209 alg_ssl = cipher->algorithm_ssl;
1210
David Benjaminebf42b52014-08-19 15:18:42 -04001211 if (alg_ssl & SSL_SSLV3)
Adam Langley95c29f32014-06-20 12:00:00 -07001212 ver="SSLv3";
1213 else if (alg_ssl & SSL_TLSV1_2)
1214 ver="TLSv1.2";
1215 else
1216 ver="unknown";
1217
1218 switch (alg_mkey)
1219 {
1220 case SSL_kRSA:
David Benjamin060d9d22014-07-15 00:54:26 -04001221 kx="RSA";
Adam Langley95c29f32014-06-20 12:00:00 -07001222 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001223 case SSL_kEDH:
David Benjamin060d9d22014-07-15 00:54:26 -04001224 kx="DH";
Adam Langley95c29f32014-06-20 12:00:00 -07001225 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001226 case SSL_kEECDH:
1227 kx="ECDH";
1228 break;
1229 case SSL_kPSK:
1230 kx="PSK";
1231 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001232 default:
1233 kx="unknown";
1234 }
1235
1236 switch (alg_auth)
1237 {
1238 case SSL_aRSA:
1239 au="RSA";
1240 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001241 case SSL_aNULL:
1242 au="None";
1243 break;
1244 case SSL_aECDSA:
1245 au="ECDSA";
1246 break;
1247 case SSL_aPSK:
1248 au="PSK";
1249 break;
1250 default:
1251 au="unknown";
1252 break;
1253 }
1254
1255 switch (alg_enc)
1256 {
Adam Langley95c29f32014-06-20 12:00:00 -07001257 case SSL_3DES:
1258 enc="3DES(168)";
1259 break;
1260 case SSL_RC4:
David Benjaminebf42b52014-08-19 15:18:42 -04001261 enc="RC4(128)";
Adam Langley95c29f32014-06-20 12:00:00 -07001262 break;
1263 case SSL_AES128:
1264 enc="AES(128)";
1265 break;
1266 case SSL_AES256:
1267 enc="AES(256)";
1268 break;
1269 case SSL_AES128GCM:
1270 enc="AESGCM(128)";
1271 break;
1272 case SSL_AES256GCM:
1273 enc="AESGCM(256)";
1274 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001275 case SSL_CHACHA20POLY1305:
1276 enc="ChaCha20-Poly1305";
1277 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001278 default:
1279 enc="unknown";
1280 break;
1281 }
1282
1283 switch (alg_mac)
1284 {
1285 case SSL_MD5:
1286 mac="MD5";
1287 break;
1288 case SSL_SHA1:
1289 mac="SHA1";
1290 break;
1291 case SSL_SHA256:
1292 mac="SHA256";
1293 break;
1294 case SSL_SHA384:
1295 mac="SHA384";
1296 break;
1297 case SSL_AEAD:
1298 mac="AEAD";
1299 break;
1300 default:
1301 mac="unknown";
1302 break;
1303 }
1304
1305 if (buf == NULL)
1306 {
1307 len=128;
1308 buf=OPENSSL_malloc(len);
1309 if (buf == NULL) return("OPENSSL_malloc Error");
1310 }
1311 else if (len < 128)
1312 return("Buffer too small");
1313
David Benjamin060d9d22014-07-15 00:54:26 -04001314 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
Adam Langley95c29f32014-06-20 12:00:00 -07001315 return(buf);
1316 }
1317
Adam Langley4d4bff82014-06-20 12:00:00 -07001318/* Next three functions require non-null cipher */
1319int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1320 {
1321 return (c->algorithm_enc & SSL_AES) != 0;
1322 }
1323
1324int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1325 {
1326 return (c->algorithm_mac & SSL_MD5) != 0;
1327 }
1328
1329int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1330 {
1331 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1332 }
1333
Adam Langleyde0b2022014-06-20 12:00:00 -07001334int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1335 {
1336 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1337 }
1338
Adam Langley73510762014-06-20 12:00:00 -07001339const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
Adam Langley95c29f32014-06-20 12:00:00 -07001340 {
1341 int i;
1342
1343 if (c == NULL) return("(NONE)");
1344 i=(int)(c->id>>24L);
1345 if (i == 3)
1346 return("TLSv1/SSLv3");
1347 else if (i == 2)
1348 return("SSLv2");
1349 else
1350 return("unknown");
1351 }
1352
1353/* return the actual cipher being used */
1354const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1355 {
1356 if (c != NULL)
1357 return(c->name);
1358 return("(NONE)");
1359 }
1360
Adam Langleyf1df2b32014-08-11 17:37:53 -07001361const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1362 if (cipher == NULL) {
1363 return "";
1364 }
1365
1366 switch (cipher->algorithm_mkey) {
1367 case SSL_kRSA:
1368 return SSL_TXT_RSA;
Adam Langleyf1df2b32014-08-11 17:37:53 -07001369 case SSL_kEDH:
1370 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001371 case SSL_aRSA:
1372 return "DHE_" SSL_TXT_RSA;
1373 case SSL_aNULL:
1374 return SSL_TXT_DH "_anon";
1375 default:
1376 return "UNKNOWN";
1377 }
Adam Langleyf1df2b32014-08-11 17:37:53 -07001378 case SSL_kEECDH:
1379 switch (cipher->algorithm_auth) {
1380 case SSL_aECDSA:
1381 return "ECDHE_" SSL_TXT_ECDSA;
1382 case SSL_aRSA:
1383 return "ECDHE_" SSL_TXT_RSA;
1384 case SSL_aNULL:
1385 return SSL_TXT_ECDH "_anon";
1386 default:
1387 return "UNKNOWN";
1388 }
1389 default:
1390 return "UNKNOWN";
1391 }
1392}
1393
Adam Langley95c29f32014-06-20 12:00:00 -07001394/* number of bits for symmetric cipher */
1395int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1396 {
1397 int ret=0;
1398
1399 if (c != NULL)
1400 {
1401 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1402 ret = c->strength_bits;
1403 }
1404 return(ret);
1405 }
1406
1407unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1408 {
1409 return c->id;
1410 }
1411
Adam Langley95c29f32014-06-20 12:00:00 -07001412void *SSL_COMP_get_compression_methods(void)
1413 {
1414 return NULL;
1415 }
1416int SSL_COMP_add_compression_method(int id, void *cm)
1417 {
1418 return 1;
1419 }
1420
1421const char *SSL_COMP_get_name(const void *comp)
1422 {
1423 return NULL;
1424 }
1425
1426/* For a cipher return the index corresponding to the certificate type */
1427int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1428 {
David Benjamin0da0e182014-08-19 16:20:28 -04001429 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001430
David Benjamin0da0e182014-08-19 16:20:28 -04001431 if (alg_a & SSL_aECDSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001432 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07001433 else if (alg_a & SSL_aRSA)
1434 return SSL_PKEY_RSA_ENC;
Adam Langley95c29f32014-06-20 12:00:00 -07001435 return -1;
1436 }
1437
David Benjamine8f3d662014-07-12 01:10:19 -04001438/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
1439 * server public key in the key exchange, sent in a server Certificate
1440 * message. Otherwise it returns 0. */
1441int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
1442 {
1443 /* Anonymous ciphers do not include a server certificate. */
1444 if (cipher->algorithm_auth & SSL_aNULL)
1445 return 0;
1446 /* Neither do PSK ciphers, except for RSA_PSK. */
1447 if ((cipher->algorithm_auth & SSL_aPSK) &&
1448 !(cipher->algorithm_mkey & SSL_kRSA))
1449 return 0;
1450 /* All other ciphers include it. */
1451 return 1;
1452 }
David Benjamin9c651c92014-07-12 13:27:45 -04001453
1454/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
1455 * requires a ServerKeyExchange message. Otherwise it returns 0.
1456 *
1457 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
1458 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
David Benjamin525a0fe2014-11-08 11:41:12 -05001459 * to communicate a psk_identity_hint, so it is optional. */
David Benjamin9c651c92014-07-12 13:27:45 -04001460int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
1461 {
1462 /* Ephemeral Diffie-Hellman key exchanges require a
1463 * ServerKeyExchange. */
1464 if (cipher->algorithm_mkey & SSL_kEDH ||
1465 cipher->algorithm_mkey & SSL_kEECDH)
1466 return 1;
1467 /* It is optional in all others. */
1468 return 0;
1469 }