blob: a10aa8f4bdd6fbb792067889d98244ffe0ebdd49 [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
144#include <openssl/comp.h>
145#include <openssl/engine.h>
146#include <openssl/mem.h>
147#include <openssl/obj.h>
148
149#include "ssl_locl.h"
150
151#define SSL_ENC_DES_IDX 0
152#define SSL_ENC_3DES_IDX 1
153#define SSL_ENC_RC4_IDX 2
154#define SSL_ENC_RC2_IDX 3
155#define SSL_ENC_IDEA_IDX 4
156#define SSL_ENC_NULL_IDX 5
157#define SSL_ENC_AES128_IDX 6
158#define SSL_ENC_AES256_IDX 7
159#define SSL_ENC_CAMELLIA128_IDX 8
160#define SSL_ENC_CAMELLIA256_IDX 9
161#define SSL_ENC_GOST89_IDX 10
162#define SSL_ENC_SEED_IDX 11
163#define SSL_ENC_AES128GCM_IDX 12
164#define SSL_ENC_AES256GCM_IDX 13
165#define SSL_ENC_NUM_IDX 14
166
167
168static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]={
169 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
170 };
171
172#define SSL_COMP_NULL_IDX 0
173#define SSL_COMP_ZLIB_IDX 1
174#define SSL_COMP_NUM_IDX 2
175
176static STACK_OF(SSL_COMP) *ssl_comp_methods=NULL;
177
178#define SSL_MD_MD5_IDX 0
179#define SSL_MD_SHA1_IDX 1
180#define SSL_MD_GOST94_IDX 2
181#define SSL_MD_GOST89MAC_IDX 3
182#define SSL_MD_SHA256_IDX 4
183#define SSL_MD_SHA384_IDX 5
184/*Constant SSL_MAX_DIGEST equal to size of digests array should be
185 * defined in the
186 * ssl_locl.h */
187#define SSL_MD_NUM_IDX SSL_MAX_DIGEST
188static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]={
189 NULL,NULL,NULL,NULL,NULL,NULL
190 };
191/* PKEY_TYPE for GOST89MAC is known in advance, but, because
192 * implementation is engine-provided, we'll fill it only if
193 * corresponding EVP_PKEY_METHOD is found
194 */
195static int ssl_mac_pkey_id[SSL_MD_NUM_IDX]={
196 EVP_PKEY_HMAC,EVP_PKEY_HMAC,EVP_PKEY_HMAC,NID_undef,
197 EVP_PKEY_HMAC,EVP_PKEY_HMAC
198 };
199
200static int ssl_mac_secret_size[SSL_MD_NUM_IDX]={
201 0,0,0,0,0,0
202 };
203
204static int ssl_handshake_digest_flag[SSL_MD_NUM_IDX]={
205 SSL_HANDSHAKE_MAC_MD5,SSL_HANDSHAKE_MAC_SHA,
206 SSL_HANDSHAKE_MAC_GOST94, 0, SSL_HANDSHAKE_MAC_SHA256,
207 SSL_HANDSHAKE_MAC_SHA384
208 };
209
210#define CIPHER_ADD 1
211#define CIPHER_KILL 2
212#define CIPHER_DEL 3
213#define CIPHER_ORD 4
214#define CIPHER_SPECIAL 5
215
216typedef struct cipher_order_st
217 {
218 const SSL_CIPHER *cipher;
219 int active;
220 int dead;
Adam Langley858a88d2014-06-20 12:00:00 -0700221 int in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700222 struct cipher_order_st *next,*prev;
223 } CIPHER_ORDER;
224
225static const SSL_CIPHER cipher_aliases[]={
226 /* "ALL" doesn't include eNULL (must be specifically enabled) */
227 {0,SSL_TXT_ALL,0, 0,0,~SSL_eNULL,0,0,0,0,0,0},
228 /* "COMPLEMENTOFALL" */
229 {0,SSL_TXT_CMPALL,0, 0,0,SSL_eNULL,0,0,0,0,0,0},
230
231 /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
232 {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,~SSL_eNULL,0,0,0,0,0,0},
233
234 /* key exchange aliases
235 * (some of those using only a single bit here combine
236 * multiple key exchange algs according to the RFCs,
237 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
238 {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
239
240 {0,SSL_TXT_kDHr,0, SSL_kDHr, 0,0,0,0,0,0,0,0},
241 {0,SSL_TXT_kDHd,0, SSL_kDHd, 0,0,0,0,0,0,0,0},
242 {0,SSL_TXT_kDH,0, SSL_kDHr|SSL_kDHd,0,0,0,0,0,0,0,0},
243 {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
244 {0,SSL_TXT_DH,0, SSL_kDHr|SSL_kDHd|SSL_kEDH,0,0,0,0,0,0,0,0},
245
246 {0,SSL_TXT_kKRB5,0, SSL_kKRB5, 0,0,0,0,0,0,0,0},
247
248 {0,SSL_TXT_kECDHr,0, SSL_kECDHr,0,0,0,0,0,0,0,0},
249 {0,SSL_TXT_kECDHe,0, SSL_kECDHe,0,0,0,0,0,0,0,0},
250 {0,SSL_TXT_kECDH,0, SSL_kECDHr|SSL_kECDHe,0,0,0,0,0,0,0,0},
251 {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
252 {0,SSL_TXT_ECDH,0, SSL_kECDHr|SSL_kECDHe|SSL_kEECDH,0,0,0,0,0,0,0,0},
253
254 {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
255 {0,SSL_TXT_kSRP,0, SSL_kSRP, 0,0,0,0,0,0,0,0},
256 {0,SSL_TXT_kGOST,0, SSL_kGOST,0,0,0,0,0,0,0,0},
257
258 /* server authentication aliases */
259 {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
260 {0,SSL_TXT_aDSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
261 {0,SSL_TXT_DSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
262 {0,SSL_TXT_aKRB5,0, 0,SSL_aKRB5, 0,0,0,0,0,0,0},
263 {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
264 {0,SSL_TXT_aDH,0, 0,SSL_aDH, 0,0,0,0,0,0,0}, /* no such ciphersuites supported! */
265 {0,SSL_TXT_aECDH,0, 0,SSL_aECDH, 0,0,0,0,0,0,0},
266 {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
267 {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
268 {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
269 {0,SSL_TXT_aGOST94,0,0,SSL_aGOST94,0,0,0,0,0,0,0},
270 {0,SSL_TXT_aGOST01,0,0,SSL_aGOST01,0,0,0,0,0,0,0},
271 {0,SSL_TXT_aGOST,0,0,SSL_aGOST94|SSL_aGOST01,0,0,0,0,0,0,0},
272
273 /* aliases combining key exchange and server authentication */
274 {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
275 {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
276 {0,SSL_TXT_NULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
277 {0,SSL_TXT_KRB5,0, SSL_kKRB5,SSL_aKRB5,0,0,0,0,0,0,0},
278 {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
279 {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
280 {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
281 {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
282 {0,SSL_TXT_SRP,0, SSL_kSRP,0,0,0,0,0,0,0,0},
283
284
285 /* symmetric encryption aliases */
286 {0,SSL_TXT_DES,0, 0,0,SSL_DES, 0,0,0,0,0,0},
287 {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
288 {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
289 {0,SSL_TXT_RC2,0, 0,0,SSL_RC2, 0,0,0,0,0,0},
290 {0,SSL_TXT_IDEA,0, 0,0,SSL_IDEA, 0,0,0,0,0,0},
291 {0,SSL_TXT_SEED,0, 0,0,SSL_SEED, 0,0,0,0,0,0},
292 {0,SSL_TXT_eNULL,0, 0,0,SSL_eNULL, 0,0,0,0,0,0},
293 {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
294 {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
295 {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
296 {0,SSL_TXT_AES_GCM,0, 0,0,SSL_AES128GCM|SSL_AES256GCM,0,0,0,0,0,0},
297 {0,SSL_TXT_CAMELLIA128,0,0,0,SSL_CAMELLIA128,0,0,0,0,0,0},
298 {0,SSL_TXT_CAMELLIA256,0,0,0,SSL_CAMELLIA256,0,0,0,0,0,0},
299 {0,SSL_TXT_CAMELLIA ,0,0,0,SSL_CAMELLIA128|SSL_CAMELLIA256,0,0,0,0,0,0},
Adam Langleyde0b2022014-06-20 12:00:00 -0700300 {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700301
302 /* MAC aliases */
303 {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
304 {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
305 {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
306 {0,SSL_TXT_GOST94,0, 0,0,0,SSL_GOST94, 0,0,0,0,0},
307 {0,SSL_TXT_GOST89MAC,0, 0,0,0,SSL_GOST89MAC, 0,0,0,0,0},
308 {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
309 {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
310
311 /* protocol version aliases */
312 {0,SSL_TXT_SSLV2,0, 0,0,0,0,SSL_SSLV2, 0,0,0,0},
313 {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
314 {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
315 {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
316
317 /* export flag */
318 {0,SSL_TXT_EXP,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
319 {0,SSL_TXT_EXPORT,0, 0,0,0,0,0,SSL_EXPORT,0,0,0},
320
321 /* strength classes */
322 {0,SSL_TXT_EXP40,0, 0,0,0,0,0,SSL_EXP40, 0,0,0},
323 {0,SSL_TXT_EXP56,0, 0,0,0,0,0,SSL_EXP56, 0,0,0},
324 {0,SSL_TXT_LOW,0, 0,0,0,0,0,SSL_LOW, 0,0,0},
325 {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
326 {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
327 /* FIPS 140-2 approved ciphersuite */
328 {0,SSL_TXT_FIPS,0, 0,0,~SSL_eNULL,0,0,SSL_FIPS, 0,0,0},
329 };
330
331void ssl_load_ciphers(void)
332 {
333 ssl_cipher_methods[SSL_ENC_DES_IDX]= EVP_des_cbc();
334 ssl_cipher_methods[SSL_ENC_3DES_IDX]= EVP_des_ede3_cbc();
335 ssl_cipher_methods[SSL_ENC_RC4_IDX]= EVP_rc4();
336 ssl_cipher_methods[SSL_ENC_AES128_IDX]= EVP_aes_128_cbc();
337 ssl_cipher_methods[SSL_ENC_AES256_IDX]= EVP_aes_256_cbc();
338
339 ssl_cipher_methods[SSL_ENC_AES128GCM_IDX]= EVP_aes_128_gcm();
340 ssl_cipher_methods[SSL_ENC_AES256GCM_IDX]= EVP_aes_256_gcm();
341
342 ssl_digest_methods[SSL_MD_MD5_IDX]= EVP_md5();
343 ssl_mac_secret_size[SSL_MD_MD5_IDX]= EVP_MD_size(EVP_md5());
344 assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
345 ssl_digest_methods[SSL_MD_SHA1_IDX]=EVP_sha1();
346 ssl_mac_secret_size[SSL_MD_SHA1_IDX]= EVP_MD_size(EVP_sha1());
347 assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
348
349 ssl_digest_methods[SSL_MD_SHA256_IDX]= EVP_sha256();
350 ssl_mac_secret_size[SSL_MD_SHA256_IDX]= EVP_MD_size(EVP_sha256());
351 ssl_digest_methods[SSL_MD_SHA384_IDX]= EVP_sha384();
352 ssl_mac_secret_size[SSL_MD_SHA384_IDX]= EVP_MD_size(EVP_sha384());
353 }
354
Adam Langleyc9fb3752014-06-20 12:00:00 -0700355/* ssl_cipher_get_comp sets |comp| to the correct SSL_COMP for the given
356 * session and returns 1. On error it returns 0. */
357int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp)
Adam Langley95c29f32014-06-20 12:00:00 -0700358 {
Adam Langleyc9fb3752014-06-20 12:00:00 -0700359 size_t index;
360
361 SSL_COMP ctmp;
362
363 *comp=NULL;
364 ctmp.id=s->compress_meth;
365 if (ssl_comp_methods != NULL)
366 {
367 if (sk_SSL_COMP_find(ssl_comp_methods, &index, &ctmp))
368 *comp=sk_SSL_COMP_value(ssl_comp_methods,index);
369 else
370 *comp=NULL;
371 }
372
373 return 1;
374 }
375
376/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
377 * for |s->cipher|. It returns 1 on success and 0 on error. */
378int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
379 {
380 const SSL_CIPHER *c = s->cipher;
381
382 *aead = NULL;
383
384 if (c == NULL)
385 return 0;
386 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
387 return 0;
388
389#ifndef OPENSSL_NO_AES
Adam Langleyde0b2022014-06-20 12:00:00 -0700390 switch (c->algorithm_enc)
391 {
392 case SSL_AES128GCM:
393 *aead = EVP_aead_aes_128_gcm();
394 return 1;
Adam Langley5c270c52014-06-20 12:00:00 -0700395 case SSL_AES256GCM:
396 *aead = EVP_aead_aes_256_gcm();
397 return 1;
Adam Langleyde0b2022014-06-20 12:00:00 -0700398 case SSL_CHACHA20POLY1305:
399 *aead = EVP_aead_chacha20_poly1305();
400 return 1;
401 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700402#endif
403
404 return 0;
405 }
406
407int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
408 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
409 {
Adam Langley95c29f32014-06-20 12:00:00 -0700410 int i;
411 const SSL_CIPHER *c;
412
413 c=s->cipher;
414 if (c == NULL) return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyc9fb3752014-06-20 12:00:00 -0700416 /* This function doesn't deal with EVP_AEAD. See
417 * |ssl_cipher_get_aead_evp|. */
418 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
419 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700420
421 if ((enc == NULL) || (md == NULL)) return(0);
422
423 switch (c->algorithm_enc)
424 {
425 case SSL_DES:
426 i=SSL_ENC_DES_IDX;
427 break;
428 case SSL_3DES:
429 i=SSL_ENC_3DES_IDX;
430 break;
431 case SSL_RC4:
432 i=SSL_ENC_RC4_IDX;
433 break;
434 case SSL_RC2:
435 i=SSL_ENC_RC2_IDX;
436 break;
437 case SSL_IDEA:
438 i=SSL_ENC_IDEA_IDX;
439 break;
440 case SSL_eNULL:
441 i=SSL_ENC_NULL_IDX;
442 break;
443 case SSL_AES128:
444 i=SSL_ENC_AES128_IDX;
445 break;
446 case SSL_AES256:
447 i=SSL_ENC_AES256_IDX;
448 break;
449 case SSL_CAMELLIA128:
450 i=SSL_ENC_CAMELLIA128_IDX;
451 break;
452 case SSL_CAMELLIA256:
453 i=SSL_ENC_CAMELLIA256_IDX;
454 break;
455 case SSL_eGOST2814789CNT:
456 i=SSL_ENC_GOST89_IDX;
457 break;
458 case SSL_SEED:
459 i=SSL_ENC_SEED_IDX;
460 break;
461 case SSL_AES128GCM:
462 i=SSL_ENC_AES128GCM_IDX;
463 break;
464 case SSL_AES256GCM:
465 i=SSL_ENC_AES256GCM_IDX;
466 break;
467 default:
468 i= -1;
469 break;
470 }
471
472 if ((i < 0) || (i > SSL_ENC_NUM_IDX))
473 *enc=NULL;
474 else
475 {
476 if (i == SSL_ENC_NULL_IDX)
477 *enc = EVP_enc_null();
478
479 *enc=ssl_cipher_methods[i];
480 }
481
482 switch (c->algorithm_mac)
483 {
484 case SSL_MD5:
485 i=SSL_MD_MD5_IDX;
486 break;
487 case SSL_SHA1:
488 i=SSL_MD_SHA1_IDX;
489 break;
490 case SSL_SHA256:
491 i=SSL_MD_SHA256_IDX;
492 break;
493 case SSL_SHA384:
494 i=SSL_MD_SHA384_IDX;
495 break;
496 case SSL_GOST94:
497 i = SSL_MD_GOST94_IDX;
498 break;
499 case SSL_GOST89MAC:
500 i = SSL_MD_GOST89MAC_IDX;
501 break;
502 default:
503 i= -1;
504 break;
505 }
506 if ((i < 0) || (i > SSL_MD_NUM_IDX))
507 {
508 *md=NULL;
509 if (mac_pkey_type!=NULL) *mac_pkey_type = NID_undef;
510 if (mac_secret_size!=NULL) *mac_secret_size = 0;
511 if (c->algorithm_mac == SSL_AEAD)
512 mac_pkey_type = NULL;
513 }
514 else
515 {
516 *md=ssl_digest_methods[i];
517 if (mac_pkey_type!=NULL) *mac_pkey_type = ssl_mac_pkey_id[i];
518 if (mac_secret_size!=NULL) *mac_secret_size = ssl_mac_secret_size[i];
519 }
520
521 if ((*enc != NULL) &&
522 (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
523 (!mac_pkey_type||*mac_pkey_type != NID_undef))
524 {
525 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
526 s->ssl_version < TLS1_VERSION)
527 return 1;
528
529#ifdef OPENSSL_FIPS
530 if (FIPS_mode())
531 return 1;
532#endif
533
534 /* TODO(fork): enable the stitched cipher modes. */
535#if 0
536 if (c->algorithm_enc == SSL_RC4 &&
537 c->algorithm_mac == SSL_MD5 &&
538 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
539 *enc = evp, *md = NULL;
540 else if (c->algorithm_enc == SSL_AES128 &&
541 c->algorithm_mac == SSL_SHA1 &&
542 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
543 *enc = evp, *md = NULL;
544 else if (c->algorithm_enc == SSL_AES256 &&
545 c->algorithm_mac == SSL_SHA1 &&
546 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
547 *enc = evp, *md = NULL;
548#endif
549 return(1);
550 }
551 else
552 return(0);
553 }
554
555int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
556{
557 if (idx <0||idx>=SSL_MD_NUM_IDX)
558 {
559 return 0;
560 }
561 *mask = ssl_handshake_digest_flag[idx];
562 if (*mask)
563 *md = ssl_digest_methods[idx];
564 else
565 *md = NULL;
566 return 1;
567}
568
569#define ITEM_SEP(a) \
570 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
571
572static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
573 CIPHER_ORDER **tail)
574 {
575 if (curr == *tail) return;
576 if (curr == *head)
577 *head=curr->next;
578 if (curr->prev != NULL)
579 curr->prev->next=curr->next;
580 if (curr->next != NULL)
581 curr->next->prev=curr->prev;
582 (*tail)->next=curr;
583 curr->prev= *tail;
584 curr->next=NULL;
585 *tail=curr;
586 }
587
588static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
589 CIPHER_ORDER **tail)
590 {
591 if (curr == *head) return;
592 if (curr == *tail)
593 *tail=curr->prev;
594 if (curr->next != NULL)
595 curr->next->prev=curr->prev;
596 if (curr->prev != NULL)
597 curr->prev->next=curr->next;
598 (*head)->prev=curr;
599 curr->next= *head;
600 curr->prev=NULL;
601 *head=curr;
602 }
603
604static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth, unsigned long *enc, unsigned long *mac, unsigned long *ssl)
605 {
606 *mkey = 0;
607 *auth = 0;
608 *enc = 0;
609 *mac = 0;
610 *ssl = 0;
611
612#ifdef OPENSSL_NO_RSA
613 *mkey |= SSL_kRSA;
614 *auth |= SSL_aRSA;
615#endif
616#ifdef OPENSSL_NO_DSA
617 *auth |= SSL_aDSS;
618#endif
619#ifdef OPENSSL_NO_DH
620 *mkey |= SSL_kDHr|SSL_kDHd|SSL_kEDH;
621 *auth |= SSL_aDH;
622#endif
623#ifdef OPENSSL_NO_ECDSA
624 *auth |= SSL_aECDSA;
625#endif
626#ifdef OPENSSL_NO_ECDH
627 *mkey |= SSL_kECDHe|SSL_kECDHr;
628 *auth |= SSL_aECDH;
629#endif
630#ifdef OPENSSL_NO_PSK
631 *mkey |= SSL_kPSK;
632 *auth |= SSL_aPSK;
633#endif
634#ifdef SSL_FORBID_ENULL
635 *enc |= SSL_eNULL;
636#endif
637
638
639
640 *enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES :0;
641 *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
642 *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
643 *enc |= (ssl_cipher_methods[SSL_ENC_RC2_IDX ] == NULL) ? SSL_RC2 :0;
644 *enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA:0;
645 *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128:0;
646 *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256:0;
647 *enc |= (ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] == NULL) ? SSL_AES128GCM:0;
648 *enc |= (ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] == NULL) ? SSL_AES256GCM:0;
649 *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA128:0;
650 *enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA256:0;
651 *enc |= (ssl_cipher_methods[SSL_ENC_GOST89_IDX] == NULL) ? SSL_eGOST2814789CNT:0;
652 *enc |= (ssl_cipher_methods[SSL_ENC_SEED_IDX] == NULL) ? SSL_SEED:0;
653
654 *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
655 *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
656 *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256:0;
657 *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384:0;
658 *mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94:0;
659 *mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL || ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]==NID_undef)? SSL_GOST89MAC:0;
660
661 }
662
663static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
664 int num_of_ciphers,
665 unsigned long disabled_mkey, unsigned long disabled_auth,
666 unsigned long disabled_enc, unsigned long disabled_mac,
667 unsigned long disabled_ssl,
668 CIPHER_ORDER *co_list,
669 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
670 {
671 int i, co_list_num;
672 const SSL_CIPHER *c;
673
674 /*
675 * We have num_of_ciphers descriptions compiled in, depending on the
676 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
677 * These will later be sorted in a linked list with at most num
678 * entries.
679 */
680
681 /* Get the initial list of ciphers */
682 co_list_num = 0; /* actual count of ciphers */
683 for (i = 0; i < num_of_ciphers; i++)
684 {
685 c = ssl_method->get_cipher(i);
686 /* drop those that use any of that is not available */
687 if ((c != NULL) && c->valid &&
688#ifdef OPENSSL_FIPS
689 (!FIPS_mode() || (c->algo_strength & SSL_FIPS)) &&
690#endif
691 !(c->algorithm_mkey & disabled_mkey) &&
692 !(c->algorithm_auth & disabled_auth) &&
693 !(c->algorithm_enc & disabled_enc) &&
694 !(c->algorithm_mac & disabled_mac) &&
695 !(c->algorithm_ssl & disabled_ssl))
696 {
697 co_list[co_list_num].cipher = c;
698 co_list[co_list_num].next = NULL;
699 co_list[co_list_num].prev = NULL;
700 co_list[co_list_num].active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700701 co_list[co_list_num].in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700702 co_list_num++;
703#ifdef KSSL_DEBUG
704 printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
705#endif /* KSSL_DEBUG */
706 /*
707 if (!sk_push(ca_list,(char *)c)) goto err;
708 */
709 }
710 }
711
712 /*
713 * Prepare linked list from list entries
714 */
715 if (co_list_num > 0)
716 {
717 co_list[0].prev = NULL;
718
719 if (co_list_num > 1)
720 {
721 co_list[0].next = &co_list[1];
722
723 for (i = 1; i < co_list_num - 1; i++)
724 {
725 co_list[i].prev = &co_list[i - 1];
726 co_list[i].next = &co_list[i + 1];
727 }
728
729 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
730 }
731
732 co_list[co_list_num - 1].next = NULL;
733
734 *head_p = &co_list[0];
735 *tail_p = &co_list[co_list_num - 1];
736 }
737 }
738
739static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
740 int num_of_group_aliases,
741 unsigned long disabled_mkey, unsigned long disabled_auth,
742 unsigned long disabled_enc, unsigned long disabled_mac,
743 unsigned long disabled_ssl,
744 CIPHER_ORDER *head)
745 {
746 CIPHER_ORDER *ciph_curr;
747 const SSL_CIPHER **ca_curr;
748 int i;
749 unsigned long mask_mkey = ~disabled_mkey;
750 unsigned long mask_auth = ~disabled_auth;
751 unsigned long mask_enc = ~disabled_enc;
752 unsigned long mask_mac = ~disabled_mac;
753 unsigned long mask_ssl = ~disabled_ssl;
754
755 /*
756 * First, add the real ciphers as already collected
757 */
758 ciph_curr = head;
759 ca_curr = ca_list;
760 while (ciph_curr != NULL)
761 {
762 *ca_curr = ciph_curr->cipher;
763 ca_curr++;
764 ciph_curr = ciph_curr->next;
765 }
766
767 /*
768 * Now we add the available ones from the cipher_aliases[] table.
769 * They represent either one or more algorithms, some of which
770 * in any affected category must be supported (set in enabled_mask),
771 * or represent a cipher strength value (will be added in any case because algorithms=0).
772 */
773 for (i = 0; i < num_of_group_aliases; i++)
774 {
775 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
776 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
777 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
778 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
779 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
780
781 if (algorithm_mkey)
782 if ((algorithm_mkey & mask_mkey) == 0)
783 continue;
784
785 if (algorithm_auth)
786 if ((algorithm_auth & mask_auth) == 0)
787 continue;
788
789 if (algorithm_enc)
790 if ((algorithm_enc & mask_enc) == 0)
791 continue;
792
793 if (algorithm_mac)
794 if ((algorithm_mac & mask_mac) == 0)
795 continue;
796
797 if (algorithm_ssl)
798 if ((algorithm_ssl & mask_ssl) == 0)
799 continue;
800
801 *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
802 ca_curr++;
803 }
804
805 *ca_curr = NULL; /* end of list */
806 }
807
808static void ssl_cipher_apply_rule(unsigned long cipher_id,
809 unsigned long alg_mkey, unsigned long alg_auth,
810 unsigned long alg_enc, unsigned long alg_mac,
811 unsigned long alg_ssl,
812 unsigned long algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700813 int rule, int strength_bits, int in_group,
Adam Langley95c29f32014-06-20 12:00:00 -0700814 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
815 {
816 CIPHER_ORDER *head, *tail, *curr, *curr2, *last;
817 const SSL_CIPHER *cp;
818 int reverse = 0;
819
820#ifdef CIPHER_DEBUG
Adam Langley858a88d2014-06-20 12:00:00 -0700821 printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
822 rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
Adam Langley95c29f32014-06-20 12:00:00 -0700823#endif
824
825 if (rule == CIPHER_DEL)
826 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
827
828 head = *head_p;
829 tail = *tail_p;
830
831 if (reverse)
832 {
833 curr = tail;
834 last = head;
835 }
836 else
837 {
838 curr = head;
839 last = tail;
840 }
841
842 curr2 = curr;
843 for (;;)
844 {
845 if ((curr == NULL) || (curr == last)) break;
846 curr = curr2;
847 curr2 = reverse ? curr->prev : curr->next;
848
849 cp = curr->cipher;
850
851 /*
852 * Selection criteria is either the value of strength_bits
853 * or the algorithms used.
854 */
855 if (strength_bits >= 0)
856 {
857 if (strength_bits != cp->strength_bits)
858 continue;
859 }
860 else
861 {
862#ifdef CIPHER_DEBUG
863 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);
864#endif
865#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
866 if (cipher_id && cipher_id != cp->id)
867 continue;
868#endif
869 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
870 continue;
871 if (alg_auth && !(alg_auth & cp->algorithm_auth))
872 continue;
873 if (alg_enc && !(alg_enc & cp->algorithm_enc))
874 continue;
875 if (alg_mac && !(alg_mac & cp->algorithm_mac))
876 continue;
877 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
878 continue;
879 if ((algo_strength & SSL_EXP_MASK) && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
880 continue;
881 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
882 continue;
883 }
884
885#ifdef CIPHER_DEBUG
886 printf("Action = %d\n", rule);
887#endif
888
889 /* add the cipher if it has not been added yet. */
890 if (rule == CIPHER_ADD)
891 {
892 /* reverse == 0 */
893 if (!curr->active)
894 {
895 ll_append_tail(&head, curr, &tail);
896 curr->active = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700897 curr->in_group = in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700898 }
899 }
900 /* Move the added cipher to this location */
901 else if (rule == CIPHER_ORD)
902 {
903 /* reverse == 0 */
904 if (curr->active)
905 {
906 ll_append_tail(&head, curr, &tail);
Adam Langley858a88d2014-06-20 12:00:00 -0700907 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700908 }
909 }
910 else if (rule == CIPHER_DEL)
911 {
912 /* reverse == 1 */
913 if (curr->active)
914 {
915 /* most recently deleted ciphersuites get best positions
916 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
917 * works in reverse to maintain the order) */
918 ll_append_head(&head, curr, &tail);
919 curr->active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700920 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700921 }
922 }
923 else if (rule == CIPHER_KILL)
924 {
925 /* reverse == 0 */
926 if (head == curr)
927 head = curr->next;
928 else
929 curr->prev->next = curr->next;
930 if (tail == curr)
931 tail = curr->prev;
932 curr->active = 0;
933 if (curr->next != NULL)
934 curr->next->prev = curr->prev;
935 if (curr->prev != NULL)
936 curr->prev->next = curr->next;
937 curr->next = NULL;
938 curr->prev = NULL;
939 }
940 }
941
942 *head_p = head;
943 *tail_p = tail;
944 }
945
946static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
947 CIPHER_ORDER **tail_p)
948 {
949 int max_strength_bits, i, *number_uses;
950 CIPHER_ORDER *curr;
951
952 /*
953 * This routine sorts the ciphers with descending strength. The sorting
954 * must keep the pre-sorted sequence, so we apply the normal sorting
955 * routine as '+' movement to the end of the list.
956 */
957 max_strength_bits = 0;
958 curr = *head_p;
959 while (curr != NULL)
960 {
961 if (curr->active &&
962 (curr->cipher->strength_bits > max_strength_bits))
963 max_strength_bits = curr->cipher->strength_bits;
964 curr = curr->next;
965 }
966
967 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
968 if (!number_uses)
969 {
970 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
971 return(0);
972 }
973 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
974
975 /*
976 * Now find the strength_bits values actually used
977 */
978 curr = *head_p;
979 while (curr != NULL)
980 {
981 if (curr->active)
982 number_uses[curr->cipher->strength_bits]++;
983 curr = curr->next;
984 }
985 /*
986 * Go through the list of used strength_bits values in descending
987 * order.
988 */
989 for (i = max_strength_bits; i >= 0; i--)
990 if (number_uses[i] > 0)
Adam Langley858a88d2014-06-20 12:00:00 -0700991 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 -0700992
993 OPENSSL_free(number_uses);
994 return(1);
995 }
996
997static int ssl_cipher_process_rulestr(const char *rule_str,
998 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
999 const SSL_CIPHER **ca_list)
1000 {
1001 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
1002 const char *l, *buf;
Adam Langley858a88d2014-06-20 12:00:00 -07001003 int j, multi, found, rule, retval, ok, buflen, in_group = 0,
1004 has_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001005 unsigned long cipher_id = 0;
1006 char ch;
1007
1008 retval = 1;
1009 l = rule_str;
1010 for (;;)
1011 {
1012 ch = *l;
1013
1014 if (ch == '\0')
1015 break; /* done */
Adam Langley858a88d2014-06-20 12:00:00 -07001016 if (in_group)
1017 {
1018 if (ch == ']')
1019 {
1020 if (!in_group)
1021 {
1022 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
1023 retval = found = in_group = 0;
1024 break;
1025 }
1026 if (*tail_p)
1027 (*tail_p)->in_group = 0;
1028 in_group = 0;
1029 l++;
1030 continue;
1031 }
1032 if (ch == '|')
1033 { rule = CIPHER_ADD; l++; continue; }
1034 else if (!(ch >= 'a' && ch <= 'z') &&
1035 !(ch >= 'A' && ch <= 'Z') &&
1036 !(ch >= '0' && ch <= '9'))
1037 {
1038 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
1039 retval = found = in_group = 0;
1040 break;
1041 }
1042 else
1043 rule = CIPHER_ADD;
1044 }
1045 else if (ch == '-')
Adam Langley95c29f32014-06-20 12:00:00 -07001046 { rule = CIPHER_DEL; l++; }
1047 else if (ch == '+')
1048 { rule = CIPHER_ORD; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -07001049 else if (ch == '!' && has_group)
1050 {
1051 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
1052 retval = found = in_group = 0;
1053 break;
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055 else if (ch == '!')
1056 { rule = CIPHER_KILL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -07001057 else if (ch == '@' && has_group)
1058 {
1059 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
1060 retval = found = in_group = 0;
1061 break;
1062 }
Adam Langley95c29f32014-06-20 12:00:00 -07001063 else if (ch == '@')
1064 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -07001065 else if (ch == '[')
1066 {
1067 if (in_group)
1068 {
1069 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
1070 retval = found = in_group = 0;
1071 break;
1072 }
1073 in_group = 1;
1074 has_group = 1;
1075 l++;
1076 continue;
1077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078 else
1079 { rule = CIPHER_ADD; }
1080
1081 if (ITEM_SEP(ch))
1082 {
1083 l++;
1084 continue;
1085 }
1086
1087 alg_mkey = 0;
1088 alg_auth = 0;
1089 alg_enc = 0;
1090 alg_mac = 0;
1091 alg_ssl = 0;
1092 algo_strength = 0;
1093
1094 for (;;)
1095 {
1096 ch = *l;
1097 buf = l;
1098 buflen = 0;
1099#ifndef CHARSET_EBCDIC
1100 while ( ((ch >= 'A') && (ch <= 'Z')) ||
1101 ((ch >= '0') && (ch <= '9')) ||
1102 ((ch >= 'a') && (ch <= 'z')) ||
1103 (ch == '-') || (ch == '.'))
1104#else
1105 while ( isalnum(ch) || (ch == '-') || (ch == '.'))
1106#endif
1107 {
1108 ch = *(++l);
1109 buflen++;
1110 }
1111
1112 if (buflen == 0)
1113 {
1114 /*
1115 * We hit something we cannot deal with,
1116 * it is no command or separator nor
1117 * alphanumeric, so we call this an error.
1118 */
1119 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -07001120 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001121 l++;
1122 break;
1123 }
1124
1125 if (rule == CIPHER_SPECIAL)
1126 {
1127 found = 0; /* unused -- avoid compiler warning */
1128 break; /* special treatment */
1129 }
1130
1131 /* check for multi-part specification */
1132 if (ch == '+')
1133 {
1134 multi=1;
1135 l++;
1136 }
1137 else
1138 multi=0;
1139
1140 /*
1141 * Now search for the cipher alias in the ca_list. Be careful
1142 * with the strncmp, because the "buflen" limitation
1143 * will make the rule "ADH:SOME" and the cipher
1144 * "ADH-MY-CIPHER" look like a match for buflen=3.
1145 * So additionally check whether the cipher name found
1146 * has the correct length. We can save a strlen() call:
1147 * just checking for the '\0' at the right place is
1148 * sufficient, we have to strncmp() anyway. (We cannot
1149 * use strcmp(), because buf is not '\0' terminated.)
1150 */
1151 j = found = 0;
1152 cipher_id = 0;
1153 while (ca_list[j])
1154 {
1155 if (!strncmp(buf, ca_list[j]->name, buflen) &&
1156 (ca_list[j]->name[buflen] == '\0'))
1157 {
1158 found = 1;
1159 break;
1160 }
1161 else
1162 j++;
1163 }
1164
1165 if (!found)
1166 break; /* ignore this entry */
1167
1168 if (ca_list[j]->algorithm_mkey)
1169 {
1170 if (alg_mkey)
1171 {
1172 alg_mkey &= ca_list[j]->algorithm_mkey;
1173 if (!alg_mkey) { found = 0; break; }
1174 }
1175 else
1176 alg_mkey = ca_list[j]->algorithm_mkey;
1177 }
1178
1179 if (ca_list[j]->algorithm_auth)
1180 {
1181 if (alg_auth)
1182 {
1183 alg_auth &= ca_list[j]->algorithm_auth;
1184 if (!alg_auth) { found = 0; break; }
1185 }
1186 else
1187 alg_auth = ca_list[j]->algorithm_auth;
1188 }
1189
1190 if (ca_list[j]->algorithm_enc)
1191 {
1192 if (alg_enc)
1193 {
1194 alg_enc &= ca_list[j]->algorithm_enc;
1195 if (!alg_enc) { found = 0; break; }
1196 }
1197 else
1198 alg_enc = ca_list[j]->algorithm_enc;
1199 }
1200
1201 if (ca_list[j]->algorithm_mac)
1202 {
1203 if (alg_mac)
1204 {
1205 alg_mac &= ca_list[j]->algorithm_mac;
1206 if (!alg_mac) { found = 0; break; }
1207 }
1208 else
1209 alg_mac = ca_list[j]->algorithm_mac;
1210 }
1211
1212 if (ca_list[j]->algo_strength & SSL_EXP_MASK)
1213 {
1214 if (algo_strength & SSL_EXP_MASK)
1215 {
1216 algo_strength &= (ca_list[j]->algo_strength & SSL_EXP_MASK) | ~SSL_EXP_MASK;
1217 if (!(algo_strength & SSL_EXP_MASK)) { found = 0; break; }
1218 }
1219 else
1220 algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
1221 }
1222
1223 if (ca_list[j]->algo_strength & SSL_STRONG_MASK)
1224 {
1225 if (algo_strength & SSL_STRONG_MASK)
1226 {
1227 algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1228 if (!(algo_strength & SSL_STRONG_MASK)) { found = 0; break; }
1229 }
1230 else
1231 algo_strength |= ca_list[j]->algo_strength & SSL_STRONG_MASK;
1232 }
1233
1234 if (ca_list[j]->valid)
1235 {
1236 /* explicit ciphersuite found; its protocol version
1237 * does not become part of the search pattern!*/
1238
1239 cipher_id = ca_list[j]->id;
1240 }
1241 else
1242 {
1243 /* not an explicit ciphersuite; only in this case, the
1244 * protocol version is considered part of the search pattern */
1245
1246 if (ca_list[j]->algorithm_ssl)
1247 {
1248 if (alg_ssl)
1249 {
1250 alg_ssl &= ca_list[j]->algorithm_ssl;
1251 if (!alg_ssl) { found = 0; break; }
1252 }
1253 else
1254 alg_ssl = ca_list[j]->algorithm_ssl;
1255 }
1256 }
1257
1258 if (!multi) break;
1259 }
1260
1261 /*
1262 * Ok, we have the rule, now apply it
1263 */
1264 if (rule == CIPHER_SPECIAL)
1265 { /* special command */
1266 ok = 0;
1267 if ((buflen == 8) &&
1268 !strncmp(buf, "STRENGTH", 8))
1269 ok = ssl_cipher_strength_sort(head_p, tail_p);
1270 else
1271 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1272 if (ok == 0)
1273 retval = 0;
1274 /*
1275 * We do not support any "multi" options
1276 * together with "@", so throw away the
1277 * rest of the command, if any left, until
1278 * end or ':' is found.
1279 */
1280 while ((*l != '\0') && !ITEM_SEP(*l))
1281 l++;
1282 }
1283 else if (found)
1284 {
1285 ssl_cipher_apply_rule(cipher_id,
1286 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -07001287 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -07001288 }
1289 else
1290 {
1291 while ((*l != '\0') && !ITEM_SEP(*l))
1292 l++;
1293 }
1294 if (*l == '\0') break; /* done */
1295 }
1296
Adam Langley858a88d2014-06-20 12:00:00 -07001297 if (in_group)
1298 {
1299 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1300 retval = 0;
1301 }
1302
Adam Langley95c29f32014-06-20 12:00:00 -07001303 return(retval);
1304 }
1305#ifndef OPENSSL_NO_EC
1306static int check_suiteb_cipher_list(const SSL_METHOD *meth, CERT *c,
1307 const char **prule_str)
1308 {
1309 unsigned int suiteb_flags = 0, suiteb_comb2 = 0;
1310 if (!strcmp(*prule_str, "SUITEB128"))
1311 suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1312 else if (!strcmp(*prule_str, "SUITEB128ONLY"))
1313 suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS_ONLY;
1314 else if (!strcmp(*prule_str, "SUITEB128C2"))
1315 {
1316 suiteb_comb2 = 1;
1317 suiteb_flags = SSL_CERT_FLAG_SUITEB_128_LOS;
1318 }
1319 else if (!strcmp(*prule_str, "SUITEB192"))
1320 suiteb_flags = SSL_CERT_FLAG_SUITEB_192_LOS;
1321
1322 if (suiteb_flags)
1323 {
1324 c->cert_flags &= ~SSL_CERT_FLAG_SUITEB_128_LOS;
1325 c->cert_flags |= suiteb_flags;
1326 }
1327 else
1328 suiteb_flags = c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS;
1329
1330 if (!suiteb_flags)
1331 return 1;
1332 /* Check version: if TLS 1.2 ciphers allowed we can use Suite B */
1333
1334 if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS))
1335 {
1336 if (meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)
1337 OPENSSL_PUT_ERROR(SSL, check_suiteb_cipher_list, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1338 else
1339 OPENSSL_PUT_ERROR(SSL, check_suiteb_cipher_list, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
1340 return 0;
1341 }
1342
1343 switch(suiteb_flags)
1344 {
1345 case SSL_CERT_FLAG_SUITEB_128_LOS:
1346 if (suiteb_comb2)
1347 *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1348 else
1349 *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384";
1350 break;
1351 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1352 *prule_str = "ECDHE-ECDSA-AES128-GCM-SHA256";
1353 break;
1354 case SSL_CERT_FLAG_SUITEB_192_LOS:
1355 *prule_str = "ECDHE-ECDSA-AES256-GCM-SHA384";
1356 break;
1357 }
1358 /* Set auto ECDH parameter determination */
1359 c->ecdh_tmp_auto = 1;
1360 return 1;
1361 }
1362#endif
1363
1364
1365STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -07001366 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -07001367 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1368 const char *rule_str, CERT *c)
1369 {
1370 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1371 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
Adam Langley858a88d2014-06-20 12:00:00 -07001372 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001373 const char *rule_p;
1374 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1375 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001376 unsigned char *in_group_flags = NULL;
1377 unsigned int num_in_group_flags = 0;
1378 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001379
1380 /*
1381 * Return with error if nothing to do.
1382 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001383 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001384 return NULL;
1385#ifndef OPENSSL_NO_EC
1386 if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
1387 return NULL;
1388#endif
1389
1390 /*
1391 * To reduce the work to do we only want to process the compiled
1392 * in algorithms, so we first get the mask of disabled ciphers.
1393 */
1394 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1395
1396 /*
1397 * Now we have to collect the available ciphers from the compiled
1398 * in ciphers. We cannot get more than the number compiled in, so
1399 * it is used for allocation.
1400 */
1401 num_of_ciphers = ssl_method->num_ciphers();
1402#ifdef KSSL_DEBUG
1403 printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
1404#endif /* KSSL_DEBUG */
1405 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1406 if (co_list == NULL)
1407 {
1408 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1409 return(NULL); /* Failure */
1410 }
1411
1412 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1413 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1414 co_list, &head, &tail);
1415
1416
1417 /* Now arrange all ciphers by preference: */
1418
1419 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001420 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1421 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 -07001422
1423 /* AES is our preferred symmetric cipher */
Adam Langley858a88d2014-06-20 12:00:00 -07001424 ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001425
1426 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001427 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001428
1429 /* Low priority for MD5 */
Adam Langley858a88d2014-06-20 12:00:00 -07001430 ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001431
1432 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1433 * (For applications that allow them, they aren't too bad, but we prefer
1434 * authenticated ciphers.) */
Adam Langley858a88d2014-06-20 12:00:00 -07001435 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 -07001436
1437 /* Move ciphers without forward secrecy to the end */
Adam Langley858a88d2014-06-20 12:00:00 -07001438 ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
1439 /* ssl_cipher_apply_rule(0, 0, SSL_aDH, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail); */
1440 ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
1441 ssl_cipher_apply_rule(0, SSL_kPSK, 0,0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
1442 ssl_cipher_apply_rule(0, SSL_kKRB5, 0,0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001443
1444 /* RC4 is sort-of broken -- move the the end */
Adam Langley858a88d2014-06-20 12:00:00 -07001445 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001446
1447 /* Now sort by symmetric encryption strength. The above ordering remains
1448 * in force within each class */
1449 if (!ssl_cipher_strength_sort(&head, &tail))
1450 {
1451 OPENSSL_free(co_list);
1452 return NULL;
1453 }
1454
1455 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001456 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001457
1458
1459 /*
1460 * We also need cipher aliases for selecting based on the rule_str.
1461 * There might be two types of entries in the rule_str: 1) names
1462 * of ciphers themselves 2) aliases for groups of ciphers.
1463 * For 1) we need the available ciphers and for 2) the cipher
1464 * groups of cipher_aliases added together in one list (otherwise
1465 * we would be happy with just the cipher_aliases table).
1466 */
1467 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1468 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1469 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1470 if (ca_list == NULL)
1471 {
1472 OPENSSL_free(co_list);
1473 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1474 return(NULL); /* Failure */
1475 }
1476 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1477 disabled_mkey, disabled_auth, disabled_enc,
1478 disabled_mac, disabled_ssl, head);
1479
1480 /*
1481 * If the rule_string begins with DEFAULT, apply the default rule
1482 * before using the (possibly available) additional rules.
1483 */
1484 ok = 1;
1485 rule_p = rule_str;
1486 if (strncmp(rule_str,"DEFAULT",7) == 0)
1487 {
1488 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1489 &head, &tail, ca_list);
1490 rule_p += 7;
1491 if (*rule_p == ':')
1492 rule_p++;
1493 }
1494
1495 if (ok && (strlen(rule_p) > 0))
1496 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1497
1498 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1499
1500 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001501 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001502
1503 /*
1504 * Allocate new "cipherstack" for the result, return with error
1505 * if we cannot get one.
1506 */
1507 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001508 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001509
Adam Langley858a88d2014-06-20 12:00:00 -07001510 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1511 if (!in_group_flags)
1512 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001513 /*
1514 * The cipher selection for the list is done. The ciphers are added
1515 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1516 */
1517 for (curr = head; curr != NULL; curr = curr->next)
1518 {
1519#ifdef OPENSSL_FIPS
1520 if (curr->active && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS))
1521#else
1522 if (curr->active)
1523#endif
1524 {
1525 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001526 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001527#ifdef CIPHER_DEBUG
1528 printf("<%s>\n",curr->cipher->name);
1529#endif
1530 }
1531 }
1532 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001533 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001534
1535 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1536 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001537 goto err;
1538 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1539 if (!pref_list)
1540 goto err;
1541 pref_list->ciphers = cipherstack;
1542 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1543 if (!pref_list->in_group_flags)
1544 goto err;
1545 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1546 OPENSSL_free(in_group_flags);
1547 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001548 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001549 ssl_cipher_preference_list_free(*cipher_list);
1550 *cipher_list = pref_list;
1551 pref_list = NULL;
1552
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001553 if (cipher_list_by_id != NULL)
1554 {
1555 if (*cipher_list_by_id != NULL)
1556 sk_SSL_CIPHER_free(*cipher_list_by_id);
1557 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001558 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001559 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001560
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001561 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1562 }
1563 else
Adam Langley858a88d2014-06-20 12:00:00 -07001564 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001565 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001566 tmp_cipher_list = NULL;
1567 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001568
Adam Langley95c29f32014-06-20 12:00:00 -07001569 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001570
1571err:
1572 if (co_list)
1573 OPENSSL_free(co_list);
1574 if (in_group_flags)
1575 OPENSSL_free(in_group_flags);
1576 if (cipherstack)
1577 sk_SSL_CIPHER_free(cipherstack);
1578 if (tmp_cipher_list)
1579 sk_SSL_CIPHER_free(tmp_cipher_list);
1580 if (pref_list && pref_list->in_group_flags)
1581 OPENSSL_free(pref_list->in_group_flags);
1582 if (pref_list)
1583 OPENSSL_free(pref_list);
1584 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001585 }
1586
1587char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1588 {
1589 int is_export,pkl,kl;
1590 const char *ver,*exp_str;
1591 const char *kx,*au,*enc,*mac;
1592 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl,alg2;
1593#ifdef KSSL_DEBUG
1594 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s AL=%lx/%lx/%lx/%lx/%lx\n";
1595#else
1596 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s%s\n";
1597#endif /* KSSL_DEBUG */
1598
1599 alg_mkey = cipher->algorithm_mkey;
1600 alg_auth = cipher->algorithm_auth;
1601 alg_enc = cipher->algorithm_enc;
1602 alg_mac = cipher->algorithm_mac;
1603 alg_ssl = cipher->algorithm_ssl;
1604
1605 alg2=cipher->algorithm2;
1606
1607 is_export=SSL_C_IS_EXPORT(cipher);
1608 pkl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1609 kl=SSL_C_EXPORT_KEYLENGTH(cipher);
1610 exp_str=is_export?" export":"";
1611
1612 if (alg_ssl & SSL_SSLV2)
1613 ver="SSLv2";
1614 else if (alg_ssl & SSL_SSLV3)
1615 ver="SSLv3";
1616 else if (alg_ssl & SSL_TLSV1_2)
1617 ver="TLSv1.2";
1618 else
1619 ver="unknown";
1620
1621 switch (alg_mkey)
1622 {
1623 case SSL_kRSA:
1624 kx=is_export?(pkl == 512 ? "RSA(512)" : "RSA(1024)"):"RSA";
1625 break;
1626 case SSL_kDHr:
1627 kx="DH/RSA";
1628 break;
1629 case SSL_kDHd:
1630 kx="DH/DSS";
1631 break;
1632 case SSL_kKRB5:
1633 kx="KRB5";
1634 break;
1635 case SSL_kEDH:
1636 kx=is_export?(pkl == 512 ? "DH(512)" : "DH(1024)"):"DH";
1637 break;
1638 case SSL_kECDHr:
1639 kx="ECDH/RSA";
1640 break;
1641 case SSL_kECDHe:
1642 kx="ECDH/ECDSA";
1643 break;
1644 case SSL_kEECDH:
1645 kx="ECDH";
1646 break;
1647 case SSL_kPSK:
1648 kx="PSK";
1649 break;
1650 case SSL_kSRP:
1651 kx="SRP";
1652 break;
1653 default:
1654 kx="unknown";
1655 }
1656
1657 switch (alg_auth)
1658 {
1659 case SSL_aRSA:
1660 au="RSA";
1661 break;
1662 case SSL_aDSS:
1663 au="DSS";
1664 break;
1665 case SSL_aDH:
1666 au="DH";
1667 break;
1668 case SSL_aKRB5:
1669 au="KRB5";
1670 break;
1671 case SSL_aECDH:
1672 au="ECDH";
1673 break;
1674 case SSL_aNULL:
1675 au="None";
1676 break;
1677 case SSL_aECDSA:
1678 au="ECDSA";
1679 break;
1680 case SSL_aPSK:
1681 au="PSK";
1682 break;
1683 default:
1684 au="unknown";
1685 break;
1686 }
1687
1688 switch (alg_enc)
1689 {
1690 case SSL_DES:
1691 enc=(is_export && kl == 5)?"DES(40)":"DES(56)";
1692 break;
1693 case SSL_3DES:
1694 enc="3DES(168)";
1695 break;
1696 case SSL_RC4:
1697 enc=is_export?(kl == 5 ? "RC4(40)" : "RC4(56)")
1698 :((alg2&SSL2_CF_8_BYTE_ENC)?"RC4(64)":"RC4(128)");
1699 break;
1700 case SSL_RC2:
1701 enc=is_export?(kl == 5 ? "RC2(40)" : "RC2(56)"):"RC2(128)";
1702 break;
1703 case SSL_IDEA:
1704 enc="IDEA(128)";
1705 break;
1706 case SSL_eNULL:
1707 enc="None";
1708 break;
1709 case SSL_AES128:
1710 enc="AES(128)";
1711 break;
1712 case SSL_AES256:
1713 enc="AES(256)";
1714 break;
1715 case SSL_AES128GCM:
1716 enc="AESGCM(128)";
1717 break;
1718 case SSL_AES256GCM:
1719 enc="AESGCM(256)";
1720 break;
1721 case SSL_CAMELLIA128:
1722 enc="Camellia(128)";
1723 break;
1724 case SSL_CAMELLIA256:
1725 enc="Camellia(256)";
1726 break;
1727 case SSL_SEED:
1728 enc="SEED(128)";
1729 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001730 case SSL_CHACHA20POLY1305:
1731 enc="ChaCha20-Poly1305";
1732 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001733 default:
1734 enc="unknown";
1735 break;
1736 }
1737
1738 switch (alg_mac)
1739 {
1740 case SSL_MD5:
1741 mac="MD5";
1742 break;
1743 case SSL_SHA1:
1744 mac="SHA1";
1745 break;
1746 case SSL_SHA256:
1747 mac="SHA256";
1748 break;
1749 case SSL_SHA384:
1750 mac="SHA384";
1751 break;
1752 case SSL_AEAD:
1753 mac="AEAD";
1754 break;
1755 default:
1756 mac="unknown";
1757 break;
1758 }
1759
1760 if (buf == NULL)
1761 {
1762 len=128;
1763 buf=OPENSSL_malloc(len);
1764 if (buf == NULL) return("OPENSSL_malloc Error");
1765 }
1766 else if (len < 128)
1767 return("Buffer too small");
1768
1769#ifdef KSSL_DEBUG
1770 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str,alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl);
1771#else
1772 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,exp_str);
1773#endif /* KSSL_DEBUG */
1774 return(buf);
1775 }
1776
Adam Langley4d4bff82014-06-20 12:00:00 -07001777/* Next three functions require non-null cipher */
1778int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1779 {
1780 return (c->algorithm_enc & SSL_AES) != 0;
1781 }
1782
1783int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1784 {
1785 return (c->algorithm_mac & SSL_MD5) != 0;
1786 }
1787
1788int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1789 {
1790 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1791 }
1792
Adam Langleyde0b2022014-06-20 12:00:00 -07001793int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1794 {
1795 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1796 }
1797
Adam Langley95c29f32014-06-20 12:00:00 -07001798char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
1799 {
1800 int i;
1801
1802 if (c == NULL) return("(NONE)");
1803 i=(int)(c->id>>24L);
1804 if (i == 3)
1805 return("TLSv1/SSLv3");
1806 else if (i == 2)
1807 return("SSLv2");
1808 else
1809 return("unknown");
1810 }
1811
1812/* return the actual cipher being used */
1813const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1814 {
1815 if (c != NULL)
1816 return(c->name);
1817 return("(NONE)");
1818 }
1819
1820/* number of bits for symmetric cipher */
1821int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1822 {
1823 int ret=0;
1824
1825 if (c != NULL)
1826 {
1827 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1828 ret = c->strength_bits;
1829 }
1830 return(ret);
1831 }
1832
1833unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1834 {
1835 return c->id;
1836 }
1837
1838SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n)
1839 {
1840 SSL_COMP *ctmp;
1841 int i,nn;
1842
1843 if ((n == 0) || (sk == NULL)) return(NULL);
1844 nn=sk_SSL_COMP_num(sk);
1845 for (i=0; i<nn; i++)
1846 {
1847 ctmp=sk_SSL_COMP_value(sk,i);
1848 if (ctmp->id == n)
1849 return(ctmp);
1850 }
1851 return(NULL);
1852 }
1853
1854void *SSL_COMP_get_compression_methods(void)
1855 {
1856 return NULL;
1857 }
1858int SSL_COMP_add_compression_method(int id, void *cm)
1859 {
1860 return 1;
1861 }
1862
1863const char *SSL_COMP_get_name(const void *comp)
1864 {
1865 return NULL;
1866 }
1867
1868/* For a cipher return the index corresponding to the certificate type */
1869int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1870 {
1871 unsigned long alg_k, alg_a;
1872
1873 alg_k = c->algorithm_mkey;
1874 alg_a = c->algorithm_auth;
1875
1876 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
1877 {
1878 /* we don't need to look at SSL_kEECDH
1879 * since no certificate is needed for
1880 * anon ECDH and for authenticated
1881 * EECDH, the check for the auth
1882 * algorithm will set i correctly
1883 * NOTE: For ECDH-RSA, we need an ECC
1884 * not an RSA cert but for EECDH-RSA
1885 * we need an RSA cert. Placing the
1886 * checks for SSL_kECDH before RSA
1887 * checks ensures the correct cert is chosen.
1888 */
1889 return SSL_PKEY_ECC;
1890 }
1891 else if (alg_a & SSL_aECDSA)
1892 return SSL_PKEY_ECC;
1893 else if (alg_k & SSL_kDHr)
1894 return SSL_PKEY_DH_RSA;
1895 else if (alg_k & SSL_kDHd)
1896 return SSL_PKEY_DH_DSA;
1897 else if (alg_a & SSL_aDSS)
1898 return SSL_PKEY_DSA_SIGN;
1899 else if (alg_a & SSL_aRSA)
1900 return SSL_PKEY_RSA_ENC;
1901 else if (alg_a & SSL_aKRB5)
1902 /* VRS something else here? */
1903 return -1;
1904 else if (alg_a & SSL_aGOST94)
1905 return SSL_PKEY_GOST94;
1906 else if (alg_a & SSL_aGOST01)
1907 return SSL_PKEY_GOST01;
1908 return -1;
1909 }
1910
1911const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr)
1912 {
1913 const SSL_CIPHER *c;
1914 c = ssl->method->get_cipher_by_char(ptr);
1915 if (c == NULL || c->valid == 0)
1916 return NULL;
1917 return c;
1918 }
1919
1920const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
1921 {
1922 return ssl->method->get_cipher_by_char(ptr);
1923 }