xref: /openbsd/lib/libssl/ssl_ciph.c (revision 8529ddd3)
1 /* $OpenBSD: ssl_ciph.c,v 1.81 2015/02/07 04:17:11 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  * ECC cipher suite support in OpenSSL originally developed by
114  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115  */
116 /* ====================================================================
117  * Copyright 2005 Nokia. All rights reserved.
118  *
119  * The portions of the attached software ("Contribution") is developed by
120  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121  * license.
122  *
123  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125  * support (see RFC 4279) to OpenSSL.
126  *
127  * No patent licenses or other rights except those expressly stated in
128  * the OpenSSL open source license shall be deemed granted or received
129  * expressly, by implication, estoppel, or otherwise.
130  *
131  * No assurances are provided by Nokia that the Contribution does not
132  * infringe the patent or other intellectual property rights of any third
133  * party or that the license provides you with all the necessary rights
134  * to make use of the Contribution.
135  *
136  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140  * OTHERWISE.
141  */
142 
143 #include <stdio.h>
144 
145 #include <openssl/objects.h>
146 
147 #ifndef OPENSSL_NO_ENGINE
148 #include <openssl/engine.h>
149 #endif
150 
151 #include "ssl_locl.h"
152 
153 #define SSL_ENC_DES_IDX		0
154 #define SSL_ENC_3DES_IDX	1
155 #define SSL_ENC_RC4_IDX		2
156 #define SSL_ENC_IDEA_IDX	3
157 #define SSL_ENC_NULL_IDX	4
158 #define SSL_ENC_AES128_IDX	5
159 #define SSL_ENC_AES256_IDX	6
160 #define SSL_ENC_CAMELLIA128_IDX	7
161 #define SSL_ENC_CAMELLIA256_IDX	8
162 #define SSL_ENC_GOST89_IDX	9
163 #define SSL_ENC_AES128GCM_IDX	10
164 #define SSL_ENC_AES256GCM_IDX	11
165 #define SSL_ENC_NUM_IDX		12
166 
167 
168 static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX] = {
169 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
170 };
171 
172 #define SSL_MD_MD5_IDX	0
173 #define SSL_MD_SHA1_IDX	1
174 #define SSL_MD_GOST94_IDX 2
175 #define SSL_MD_GOST89MAC_IDX 3
176 #define SSL_MD_SHA256_IDX 4
177 #define SSL_MD_SHA384_IDX 5
178 #define SSL_MD_STREEBOG256_IDX 6
179 #define SSL_MD_STREEBOG512_IDX 7
180 /*Constant SSL_MAX_DIGEST equal to size of digests array should be
181  * defined in the
182  * ssl_locl.h */
183 #define SSL_MD_NUM_IDX	SSL_MAX_DIGEST
184 static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = {
185 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
186 };
187 
188 static int  ssl_mac_pkey_id[SSL_MD_NUM_IDX] = {
189 	EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_GOSTIMIT,
190 	EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC,
191 };
192 
193 static int ssl_mac_secret_size[SSL_MD_NUM_IDX] = {
194 	0, 0, 0, 0, 0, 0, 0, 0
195 };
196 
197 static int ssl_handshake_digest_flag[SSL_MD_NUM_IDX] = {
198 	SSL_HANDSHAKE_MAC_MD5, SSL_HANDSHAKE_MAC_SHA,
199 	SSL_HANDSHAKE_MAC_GOST94, 0, SSL_HANDSHAKE_MAC_SHA256,
200 	SSL_HANDSHAKE_MAC_SHA384, SSL_HANDSHAKE_MAC_STREEBOG256,
201 	SSL_HANDSHAKE_MAC_STREEBOG512
202 };
203 
204 #define CIPHER_ADD	1
205 #define CIPHER_KILL	2
206 #define CIPHER_DEL	3
207 #define CIPHER_ORD	4
208 #define CIPHER_SPECIAL	5
209 
210 typedef struct cipher_order_st {
211 	const SSL_CIPHER *cipher;
212 	int active;
213 	int dead;
214 	struct cipher_order_st *next, *prev;
215 } CIPHER_ORDER;
216 
217 static const SSL_CIPHER cipher_aliases[] = {
218 
219 	/* "ALL" doesn't include eNULL (must be specifically enabled) */
220 	{
221 		.name = SSL_TXT_ALL,
222 		.algorithm_enc = ~SSL_eNULL,
223 	},
224 
225 	/* "COMPLEMENTOFALL" */
226 	{
227 		.name = SSL_TXT_CMPALL,
228 		.algorithm_enc = SSL_eNULL,
229 	},
230 
231 	/*
232 	 * "COMPLEMENTOFDEFAULT"
233 	 * (does *not* include ciphersuites not found in ALL!)
234 	 */
235 	{
236 		.name = SSL_TXT_CMPDEF,
237 		.algorithm_mkey = SSL_kDHE|SSL_kECDHE,
238 		.algorithm_auth = SSL_aNULL,
239 		.algorithm_enc = ~SSL_eNULL,
240 	},
241 
242 	/*
243 	 * key exchange aliases
244 	 * (some of those using only a single bit here combine multiple key
245 	 * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
246 	 * and DHE_RSA)
247 	 */
248 	{
249 		.name = SSL_TXT_kRSA,
250 		.algorithm_mkey = SSL_kRSA,
251 	},
252 	{
253 		.name = SSL_TXT_kEDH,
254 		.algorithm_mkey = SSL_kDHE,
255 	},
256 	{
257 		.name = SSL_TXT_DH,
258 		.algorithm_mkey = SSL_kDHE,
259 	},
260 
261 	{
262 		.name = SSL_TXT_kECDHr,
263 		.algorithm_mkey = SSL_kECDHr,
264 	},
265 	{
266 		.name = SSL_TXT_kECDHe,
267 		.algorithm_mkey = SSL_kECDHe,
268 	},
269 	{
270 		.name = SSL_TXT_kECDH,
271 		.algorithm_mkey = SSL_kECDHr|SSL_kECDHe,
272 	},
273 	{
274 		.name = SSL_TXT_kEECDH,
275 		.algorithm_mkey = SSL_kECDHE,
276 	},
277 	{
278 		.name = SSL_TXT_ECDH,
279 		.algorithm_mkey = SSL_kECDHr|SSL_kECDHe|SSL_kECDHE,
280 	},
281 
282 	{
283 		.name = SSL_TXT_kGOST,
284 		.algorithm_mkey = SSL_kGOST,
285 	},
286 
287 	/* server authentication aliases */
288 	{
289 		.name = SSL_TXT_aRSA,
290 		.algorithm_auth = SSL_aRSA,
291 	},
292 	{
293 		.name = SSL_TXT_aDSS,
294 		.algorithm_auth = SSL_aDSS,
295 	},
296 	{
297 		.name = SSL_TXT_DSS,
298 		.algorithm_auth = SSL_aDSS,
299 	},
300 	{
301 		.name = SSL_TXT_aNULL,
302 		.algorithm_auth = SSL_aNULL,
303 	},
304 	{
305 		.name = SSL_TXT_aECDH,
306 		.algorithm_auth = SSL_aECDH,
307 	},
308 	{
309 		.name = SSL_TXT_aECDSA,
310 		.algorithm_auth = SSL_aECDSA,
311 	},
312 	{
313 		.name = SSL_TXT_ECDSA,
314 		.algorithm_auth = SSL_aECDSA,
315 	},
316 	{
317 		.name = SSL_TXT_aGOST01,
318 		.algorithm_auth = SSL_aGOST01,
319 	},
320 	{
321 		.name = SSL_TXT_aGOST,
322 		.algorithm_auth = SSL_aGOST01,
323 	},
324 
325 	/* aliases combining key exchange and server authentication */
326 	{
327 		.name = SSL_TXT_DHE,
328 		.algorithm_mkey = SSL_kDHE,
329 		.algorithm_auth = ~SSL_aNULL,
330 	},
331 	{
332 		.name = SSL_TXT_EDH,
333 		.algorithm_mkey = SSL_kDHE,
334 		.algorithm_auth = ~SSL_aNULL,
335 	},
336 	{
337 		.name = SSL_TXT_ECDHE,
338 		.algorithm_mkey = SSL_kECDHE,
339 		.algorithm_auth = ~SSL_aNULL,
340 	},
341 	{
342 		.name = SSL_TXT_EECDH,
343 		.algorithm_mkey = SSL_kECDHE,
344 		.algorithm_auth = ~SSL_aNULL,
345 	},
346 	{
347 		.name = SSL_TXT_NULL,
348 		.algorithm_enc = SSL_eNULL,
349 	},
350 	{
351 		.name = SSL_TXT_RSA,
352 		.algorithm_mkey = SSL_kRSA,
353 		.algorithm_auth = SSL_aRSA,
354 	},
355 	{
356 		.name = SSL_TXT_ADH,
357 		.algorithm_mkey = SSL_kDHE,
358 		.algorithm_auth = SSL_aNULL,
359 	},
360 	{
361 		.name = SSL_TXT_AECDH,
362 		.algorithm_mkey = SSL_kECDHE,
363 		.algorithm_auth = SSL_aNULL,
364 	},
365 
366 	/* symmetric encryption aliases */
367 	{
368 		.name = SSL_TXT_DES,
369 		.algorithm_enc = SSL_DES,
370 	},
371 	{
372 		.name = SSL_TXT_3DES,
373 		.algorithm_enc = SSL_3DES,
374 	},
375 	{
376 		.name = SSL_TXT_RC4,
377 		.algorithm_enc = SSL_RC4,
378 	},
379 	{
380 		.name = SSL_TXT_IDEA,
381 		.algorithm_enc = SSL_IDEA,
382 	},
383 	{
384 		.name = SSL_TXT_eNULL,
385 		.algorithm_enc = SSL_eNULL,
386 	},
387 	{
388 		.name = SSL_TXT_AES128,
389 		.algorithm_enc = SSL_AES128|SSL_AES128GCM,
390 	},
391 	{
392 		.name = SSL_TXT_AES256,
393 		.algorithm_enc = SSL_AES256|SSL_AES256GCM,
394 	},
395 	{
396 		.name = SSL_TXT_AES,
397 		.algorithm_enc = SSL_AES,
398 	},
399 	{
400 		.name = SSL_TXT_AES_GCM,
401 		.algorithm_enc = SSL_AES128GCM|SSL_AES256GCM,
402 	},
403 	{
404 		.name = SSL_TXT_CAMELLIA128,
405 		.algorithm_enc = SSL_CAMELLIA128,
406 	},
407 	{
408 		.name = SSL_TXT_CAMELLIA256,
409 		.algorithm_enc = SSL_CAMELLIA256,
410 	},
411 	{
412 		.name = SSL_TXT_CAMELLIA,
413 		.algorithm_enc = SSL_CAMELLIA128|SSL_CAMELLIA256,
414 	},
415 	{
416 		.name = SSL_TXT_CHACHA20,
417 		.algorithm_enc = SSL_CHACHA20POLY1305,
418 	},
419 
420 	/* MAC aliases */
421 	{
422 		.name = SSL_TXT_AEAD,
423 		.algorithm_mac = SSL_AEAD,
424 	},
425 	{
426 		.name = SSL_TXT_MD5,
427 		.algorithm_mac = SSL_MD5,
428 	},
429 	{
430 		.name = SSL_TXT_SHA1,
431 		.algorithm_mac = SSL_SHA1,
432 	},
433 	{
434 		.name = SSL_TXT_SHA,
435 		.algorithm_mac = SSL_SHA1,
436 	},
437 	{
438 		.name = SSL_TXT_GOST94,
439 		.algorithm_mac = SSL_GOST94,
440 	},
441 	{
442 		.name = SSL_TXT_GOST89MAC,
443 		.algorithm_mac = SSL_GOST89MAC,
444 	},
445 	{
446 		.name = SSL_TXT_SHA256,
447 		.algorithm_mac = SSL_SHA256,
448 	},
449 	{
450 		.name = SSL_TXT_SHA384,
451 		.algorithm_mac = SSL_SHA384,
452 	},
453 	{
454 		.name = SSL_TXT_STREEBOG256,
455 		.algorithm_mac = SSL_STREEBOG256,
456 	},
457 	{
458 		.name = SSL_TXT_STREEBOG512,
459 		.algorithm_mac = SSL_STREEBOG512,
460 	},
461 
462 	/* protocol version aliases */
463 	{
464 		.name = SSL_TXT_SSLV3,
465 		.algorithm_ssl = SSL_SSLV3,
466 	},
467 	{
468 		.name = SSL_TXT_TLSV1,
469 		.algorithm_ssl = SSL_TLSV1,
470 	},
471 	{
472 		.name = SSL_TXT_TLSV1_2,
473 		.algorithm_ssl = SSL_TLSV1_2,
474 	},
475 
476 	/* strength classes */
477 	{
478 		.name = SSL_TXT_LOW,
479 		.algo_strength = SSL_LOW,
480 	},
481 	{
482 		.name = SSL_TXT_MEDIUM,
483 		.algo_strength = SSL_MEDIUM,
484 	},
485 	{
486 		.name = SSL_TXT_HIGH,
487 		.algo_strength = SSL_HIGH,
488 	},
489 };
490 
491 void
492 ssl_load_ciphers(void)
493 {
494 	ssl_cipher_methods[SSL_ENC_DES_IDX] =
495 	    EVP_get_cipherbyname(SN_des_cbc);
496 	ssl_cipher_methods[SSL_ENC_3DES_IDX] =
497 	    EVP_get_cipherbyname(SN_des_ede3_cbc);
498 	ssl_cipher_methods[SSL_ENC_RC4_IDX] =
499 	    EVP_get_cipherbyname(SN_rc4);
500 #ifndef OPENSSL_NO_IDEA
501 	ssl_cipher_methods[SSL_ENC_IDEA_IDX] =
502 	    EVP_get_cipherbyname(SN_idea_cbc);
503 #else
504 	ssl_cipher_methods[SSL_ENC_IDEA_IDX] = NULL;
505 #endif
506 	ssl_cipher_methods[SSL_ENC_AES128_IDX] =
507 	    EVP_get_cipherbyname(SN_aes_128_cbc);
508 	ssl_cipher_methods[SSL_ENC_AES256_IDX] =
509 	    EVP_get_cipherbyname(SN_aes_256_cbc);
510 	ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] =
511 	    EVP_get_cipherbyname(SN_camellia_128_cbc);
512 	ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] =
513 	    EVP_get_cipherbyname(SN_camellia_256_cbc);
514 	ssl_cipher_methods[SSL_ENC_GOST89_IDX] =
515 	    EVP_get_cipherbyname(SN_gost89_cnt);
516 
517 	ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] =
518 	    EVP_get_cipherbyname(SN_aes_128_gcm);
519 	ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] =
520 	    EVP_get_cipherbyname(SN_aes_256_gcm);
521 
522 	ssl_digest_methods[SSL_MD_MD5_IDX] =
523 	    EVP_get_digestbyname(SN_md5);
524 	ssl_mac_secret_size[SSL_MD_MD5_IDX] =
525 	    EVP_MD_size(ssl_digest_methods[SSL_MD_MD5_IDX]);
526 	OPENSSL_assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
527 	ssl_digest_methods[SSL_MD_SHA1_IDX] =
528 	    EVP_get_digestbyname(SN_sha1);
529 	ssl_mac_secret_size[SSL_MD_SHA1_IDX] =
530 	    EVP_MD_size(ssl_digest_methods[SSL_MD_SHA1_IDX]);
531 	OPENSSL_assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
532 	ssl_digest_methods[SSL_MD_GOST94_IDX] =
533 	    EVP_get_digestbyname(SN_id_GostR3411_94);
534 	if (ssl_digest_methods[SSL_MD_GOST94_IDX]) {
535 		ssl_mac_secret_size[SSL_MD_GOST94_IDX] =
536 		    EVP_MD_size(ssl_digest_methods[SSL_MD_GOST94_IDX]);
537 		OPENSSL_assert(ssl_mac_secret_size[SSL_MD_GOST94_IDX] >= 0);
538 	}
539 	ssl_digest_methods[SSL_MD_GOST89MAC_IDX] =
540 	    EVP_get_digestbyname(SN_id_Gost28147_89_MAC);
541 	if (ssl_mac_pkey_id[SSL_MD_GOST89MAC_IDX]) {
542 		ssl_mac_secret_size[SSL_MD_GOST89MAC_IDX] = 32;
543 	}
544 
545 	ssl_digest_methods[SSL_MD_SHA256_IDX] =
546 	    EVP_get_digestbyname(SN_sha256);
547 	ssl_mac_secret_size[SSL_MD_SHA256_IDX] =
548 	    EVP_MD_size(ssl_digest_methods[SSL_MD_SHA256_IDX]);
549 	ssl_digest_methods[SSL_MD_SHA384_IDX] =
550 	    EVP_get_digestbyname(SN_sha384);
551 	ssl_mac_secret_size[SSL_MD_SHA384_IDX] =
552 	    EVP_MD_size(ssl_digest_methods[SSL_MD_SHA384_IDX]);
553 	ssl_digest_methods[SSL_MD_STREEBOG256_IDX] =
554 	    EVP_get_digestbyname(SN_id_tc26_gost3411_2012_256);
555 	ssl_mac_secret_size[SSL_MD_STREEBOG256_IDX] =
556 	    EVP_MD_size(ssl_digest_methods[SSL_MD_STREEBOG256_IDX]);
557 	ssl_digest_methods[SSL_MD_STREEBOG512_IDX] =
558 	    EVP_get_digestbyname(SN_id_tc26_gost3411_2012_512);
559 	ssl_mac_secret_size[SSL_MD_STREEBOG512_IDX] =
560 	    EVP_MD_size(ssl_digest_methods[SSL_MD_STREEBOG512_IDX]);
561 }
562 
563 int
564 ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
565     const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
566 {
567 	const SSL_CIPHER *c;
568 	int i;
569 
570 	c = s->cipher;
571 	if (c == NULL)
572 		return (0);
573 
574 	/*
575 	 * This function does not handle EVP_AEAD.
576 	 * See ssl_cipher_get_aead_evp instead.
577 	 */
578 	if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
579 		return(0);
580 
581 	if ((enc == NULL) || (md == NULL))
582 		return (0);
583 
584 	switch (c->algorithm_enc) {
585 	case SSL_DES:
586 		i = SSL_ENC_DES_IDX;
587 		break;
588 	case SSL_3DES:
589 		i = SSL_ENC_3DES_IDX;
590 		break;
591 	case SSL_RC4:
592 		i = SSL_ENC_RC4_IDX;
593 		break;
594 	case SSL_IDEA:
595 		i = SSL_ENC_IDEA_IDX;
596 		break;
597 	case SSL_eNULL:
598 		i = SSL_ENC_NULL_IDX;
599 		break;
600 	case SSL_AES128:
601 		i = SSL_ENC_AES128_IDX;
602 		break;
603 	case SSL_AES256:
604 		i = SSL_ENC_AES256_IDX;
605 		break;
606 	case SSL_CAMELLIA128:
607 		i = SSL_ENC_CAMELLIA128_IDX;
608 		break;
609 	case SSL_CAMELLIA256:
610 		i = SSL_ENC_CAMELLIA256_IDX;
611 		break;
612 	case SSL_eGOST2814789CNT:
613 		i = SSL_ENC_GOST89_IDX;
614 		break;
615 	case SSL_AES128GCM:
616 		i = SSL_ENC_AES128GCM_IDX;
617 		break;
618 	case SSL_AES256GCM:
619 		i = SSL_ENC_AES256GCM_IDX;
620 		break;
621 	default:
622 		i = -1;
623 		break;
624 	}
625 
626 	if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
627 		*enc = NULL;
628 	else {
629 		if (i == SSL_ENC_NULL_IDX)
630 			*enc = EVP_enc_null();
631 		else
632 			*enc = ssl_cipher_methods[i];
633 	}
634 
635 	switch (c->algorithm_mac) {
636 	case SSL_MD5:
637 		i = SSL_MD_MD5_IDX;
638 		break;
639 	case SSL_SHA1:
640 		i = SSL_MD_SHA1_IDX;
641 		break;
642 	case SSL_SHA256:
643 		i = SSL_MD_SHA256_IDX;
644 		break;
645 	case SSL_SHA384:
646 		i = SSL_MD_SHA384_IDX;
647 		break;
648 	case SSL_GOST94:
649 		i = SSL_MD_GOST94_IDX;
650 		break;
651 	case SSL_GOST89MAC:
652 		i = SSL_MD_GOST89MAC_IDX;
653 		break;
654 	case SSL_STREEBOG256:
655 		i = SSL_MD_STREEBOG256_IDX;
656 		break;
657 	case SSL_STREEBOG512:
658 		i = SSL_MD_STREEBOG512_IDX;
659 		break;
660 	default:
661 		i = -1;
662 		break;
663 	}
664 	if ((i < 0) || (i >= SSL_MD_NUM_IDX)) {
665 		*md = NULL;
666 
667 		if (mac_pkey_type != NULL)
668 			*mac_pkey_type = NID_undef;
669 		if (mac_secret_size != NULL)
670 			*mac_secret_size = 0;
671 		if (c->algorithm_mac == SSL_AEAD)
672 			mac_pkey_type = NULL;
673 	} else {
674 		*md = ssl_digest_methods[i];
675 		if (mac_pkey_type != NULL)
676 			*mac_pkey_type = ssl_mac_pkey_id[i];
677 		if (mac_secret_size != NULL)
678 			*mac_secret_size = ssl_mac_secret_size[i];
679 	}
680 
681 	if ((*enc != NULL) &&
682 	    (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
683 	    (!mac_pkey_type || *mac_pkey_type != NID_undef)) {
684 		const EVP_CIPHER *evp;
685 
686 		if (s->ssl_version >> 8 != TLS1_VERSION_MAJOR ||
687 		    s->ssl_version < TLS1_VERSION)
688 			return 1;
689 
690 		if (c->algorithm_enc == SSL_RC4 &&
691 		    c->algorithm_mac == SSL_MD5 &&
692 		    (evp = EVP_get_cipherbyname("RC4-HMAC-MD5")))
693 			*enc = evp, *md = NULL;
694 		else if (c->algorithm_enc == SSL_AES128 &&
695 		    c->algorithm_mac == SSL_SHA1 &&
696 		    (evp = EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
697 			*enc = evp, *md = NULL;
698 		else if (c->algorithm_enc == SSL_AES256 &&
699 		    c->algorithm_mac == SSL_SHA1 &&
700 		    (evp = EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
701 			*enc = evp, *md = NULL;
702 		return (1);
703 	} else
704 		return (0);
705 }
706 
707 /*
708  * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
709  * for s->cipher. It returns 1 on success and 0 on error.
710  */
711 int
712 ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
713 {
714 	const SSL_CIPHER *c = s->cipher;
715 
716 	*aead = NULL;
717 
718 	if (c == NULL)
719 		return 0;
720 	if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
721 		return 0;
722 
723 	switch (c->algorithm_enc) {
724 #ifndef OPENSSL_NO_AES
725 	case SSL_AES128GCM:
726 		*aead = EVP_aead_aes_128_gcm();
727 		return 1;
728 	case SSL_AES256GCM:
729 		*aead = EVP_aead_aes_256_gcm();
730 		return 1;
731 #endif
732 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
733 	case SSL_CHACHA20POLY1305:
734 		*aead = EVP_aead_chacha20_poly1305();
735 		return 1;
736 #endif
737 	default:
738 		break;
739 	}
740 	return 0;
741 }
742 
743 int
744 ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
745 {
746 	if (idx < 0 || idx >= SSL_MD_NUM_IDX) {
747 		return 0;
748 	}
749 	*mask = ssl_handshake_digest_flag[idx];
750 	if (*mask)
751 		*md = ssl_digest_methods[idx];
752 	else
753 		*md = NULL;
754 	return 1;
755 }
756 
757 #define ITEM_SEP(a) \
758 	(((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
759 
760 static void
761 ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
762     CIPHER_ORDER **tail)
763 {
764 	if (curr == *tail)
765 		return;
766 	if (curr == *head)
767 		*head = curr->next;
768 	if (curr->prev != NULL)
769 		curr->prev->next = curr->next;
770 	if (curr->next != NULL)
771 		curr->next->prev = curr->prev;
772 	(*tail)->next = curr;
773 	curr->prev= *tail;
774 	curr->next = NULL;
775 	*tail = curr;
776 }
777 
778 static void
779 ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
780     CIPHER_ORDER **tail)
781 {
782 	if (curr == *head)
783 		return;
784 	if (curr == *tail)
785 		*tail = curr->prev;
786 	if (curr->next != NULL)
787 		curr->next->prev = curr->prev;
788 	if (curr->prev != NULL)
789 		curr->prev->next = curr->next;
790 	(*head)->prev = curr;
791 	curr->next= *head;
792 	curr->prev = NULL;
793 	*head = curr;
794 }
795 
796 static void
797 ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
798     unsigned long *enc, unsigned long *mac, unsigned long *ssl)
799 {
800 	*mkey = 0;
801 	*auth = 0;
802 	*enc = 0;
803 	*mac = 0;
804 	*ssl = 0;
805 
806 	/*
807 	 * Check for the availability of GOST 34.10 public/private key
808 	 * algorithms. If they are not available disable the associated
809 	 * authentication and key exchange algorithms.
810 	 */
811 	if (EVP_PKEY_meth_find(NID_id_GostR3410_2001) == NULL) {
812 		*auth |= SSL_aGOST01;
813 		*mkey |= SSL_kGOST;
814 	}
815 
816 #ifdef SSL_FORBID_ENULL
817 	*enc |= SSL_eNULL;
818 #endif
819 
820 	*enc |= (ssl_cipher_methods[SSL_ENC_DES_IDX ] == NULL) ? SSL_DES : 0;
821 	*enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES : 0;
822 	*enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 : 0;
823 	*enc |= (ssl_cipher_methods[SSL_ENC_IDEA_IDX] == NULL) ? SSL_IDEA : 0;
824 	*enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128 : 0;
825 	*enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256 : 0;
826 	*enc |= (ssl_cipher_methods[SSL_ENC_AES128GCM_IDX] == NULL) ? SSL_AES128GCM : 0;
827 	*enc |= (ssl_cipher_methods[SSL_ENC_AES256GCM_IDX] == NULL) ? SSL_AES256GCM : 0;
828 	*enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA128_IDX] == NULL) ? SSL_CAMELLIA128 : 0;
829 	*enc |= (ssl_cipher_methods[SSL_ENC_CAMELLIA256_IDX] == NULL) ? SSL_CAMELLIA256 : 0;
830 	*enc |= (ssl_cipher_methods[SSL_ENC_GOST89_IDX] == NULL) ? SSL_eGOST2814789CNT : 0;
831 
832 	*mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 : 0;
833 	*mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1 : 0;
834 	*mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256 : 0;
835 	*mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384 : 0;
836 	*mac |= (ssl_digest_methods[SSL_MD_GOST94_IDX] == NULL) ? SSL_GOST94 : 0;
837 	*mac |= (ssl_digest_methods[SSL_MD_GOST89MAC_IDX] == NULL) ? SSL_GOST89MAC : 0;
838 	*mac |= (ssl_digest_methods[SSL_MD_STREEBOG256_IDX] == NULL) ? SSL_STREEBOG256 : 0;
839 	*mac |= (ssl_digest_methods[SSL_MD_STREEBOG512_IDX] == NULL) ? SSL_STREEBOG512 : 0;
840 
841 }
842 
843 static void
844 ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers,
845     unsigned long disabled_mkey, unsigned long disabled_auth,
846     unsigned long disabled_enc, unsigned long disabled_mac,
847     unsigned long disabled_ssl, CIPHER_ORDER *co_list,
848     CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
849 {
850 	int i, co_list_num;
851 	const SSL_CIPHER *c;
852 
853 	/*
854 	 * We have num_of_ciphers descriptions compiled in, depending on the
855 	 * method selected (SSLv3, TLSv1, etc). These will later be sorted in
856 	 * a linked list with at most num entries.
857 	 */
858 
859 	/* Get the initial list of ciphers */
860 	co_list_num = 0;	/* actual count of ciphers */
861 	for (i = 0; i < num_of_ciphers; i++) {
862 		c = ssl_method->get_cipher(i);
863 		/* drop those that use any of that is not available */
864 		if ((c != NULL) && c->valid &&
865 		    !(c->algorithm_mkey & disabled_mkey) &&
866 		    !(c->algorithm_auth & disabled_auth) &&
867 		    !(c->algorithm_enc & disabled_enc) &&
868 		    !(c->algorithm_mac & disabled_mac) &&
869 		    !(c->algorithm_ssl & disabled_ssl)) {
870 			co_list[co_list_num].cipher = c;
871 			co_list[co_list_num].next = NULL;
872 			co_list[co_list_num].prev = NULL;
873 			co_list[co_list_num].active = 0;
874 			co_list_num++;
875 			/*
876 			if (!sk_push(ca_list,(char *)c)) goto err;
877 			*/
878 		}
879 	}
880 
881 	/*
882 	 * Prepare linked list from list entries
883 	 */
884 	if (co_list_num > 0) {
885 		co_list[0].prev = NULL;
886 
887 		if (co_list_num > 1) {
888 			co_list[0].next = &co_list[1];
889 
890 			for (i = 1; i < co_list_num - 1; i++) {
891 				co_list[i].prev = &co_list[i - 1];
892 				co_list[i].next = &co_list[i + 1];
893 			}
894 
895 			co_list[co_list_num - 1].prev =
896 			    &co_list[co_list_num - 2];
897 		}
898 
899 		co_list[co_list_num - 1].next = NULL;
900 
901 		*head_p = &co_list[0];
902 		*tail_p = &co_list[co_list_num - 1];
903 	}
904 }
905 
906 static void
907 ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list, int num_of_group_aliases,
908     unsigned long disabled_mkey, unsigned long disabled_auth,
909     unsigned long disabled_enc, unsigned long disabled_mac,
910     unsigned long disabled_ssl, CIPHER_ORDER *head)
911 {
912 	CIPHER_ORDER *ciph_curr;
913 	const SSL_CIPHER **ca_curr;
914 	int i;
915 	unsigned long mask_mkey = ~disabled_mkey;
916 	unsigned long mask_auth = ~disabled_auth;
917 	unsigned long mask_enc = ~disabled_enc;
918 	unsigned long mask_mac = ~disabled_mac;
919 	unsigned long mask_ssl = ~disabled_ssl;
920 
921 	/*
922 	 * First, add the real ciphers as already collected
923 	 */
924 	ciph_curr = head;
925 	ca_curr = ca_list;
926 	while (ciph_curr != NULL) {
927 		*ca_curr = ciph_curr->cipher;
928 		ca_curr++;
929 		ciph_curr = ciph_curr->next;
930 	}
931 
932 	/*
933 	 * Now we add the available ones from the cipher_aliases[] table.
934 	 * They represent either one or more algorithms, some of which
935 	 * in any affected category must be supported (set in enabled_mask),
936 	 * or represent a cipher strength value (will be added in any case because algorithms=0).
937 	 */
938 	for (i = 0; i < num_of_group_aliases; i++) {
939 		unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
940 		unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
941 		unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
942 		unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
943 		unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
944 
945 		if (algorithm_mkey)
946 			if ((algorithm_mkey & mask_mkey) == 0)
947 				continue;
948 
949 		if (algorithm_auth)
950 			if ((algorithm_auth & mask_auth) == 0)
951 				continue;
952 
953 		if (algorithm_enc)
954 			if ((algorithm_enc & mask_enc) == 0)
955 				continue;
956 
957 		if (algorithm_mac)
958 			if ((algorithm_mac & mask_mac) == 0)
959 				continue;
960 
961 		if (algorithm_ssl)
962 			if ((algorithm_ssl & mask_ssl) == 0)
963 				continue;
964 
965 		*ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
966 		ca_curr++;
967 	}
968 
969 	*ca_curr = NULL;	/* end of list */
970 }
971 
972 static void
973 ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long alg_mkey,
974     unsigned long alg_auth, unsigned long alg_enc, unsigned long alg_mac,
975     unsigned long alg_ssl, unsigned long algo_strength,
976     int rule, int strength_bits, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
977 {
978 	CIPHER_ORDER *head, *tail, *curr, *next, *last;
979 	const SSL_CIPHER *cp;
980 	int reverse = 0;
981 
982 
983 	if (rule == CIPHER_DEL)
984 		reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
985 
986 	head = *head_p;
987 	tail = *tail_p;
988 
989 	if (reverse) {
990 		next = tail;
991 		last = head;
992 	} else {
993 		next = head;
994 		last = tail;
995 	}
996 
997 	curr = NULL;
998 	for (;;) {
999 		if (curr == last)
1000 			break;
1001 		curr = next;
1002 		next = reverse ? curr->prev : curr->next;
1003 
1004 		cp = curr->cipher;
1005 
1006 		/*
1007 		 * Selection criteria is either the value of strength_bits
1008 		 * or the algorithms used.
1009 		 */
1010 		if (strength_bits >= 0) {
1011 			if (strength_bits != cp->strength_bits)
1012 				continue;
1013 		} else {
1014 
1015 			if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
1016 				continue;
1017 			if (alg_auth && !(alg_auth & cp->algorithm_auth))
1018 				continue;
1019 			if (alg_enc && !(alg_enc & cp->algorithm_enc))
1020 				continue;
1021 			if (alg_mac && !(alg_mac & cp->algorithm_mac))
1022 				continue;
1023 			if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
1024 				continue;
1025 			if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
1026 				continue;
1027 		}
1028 
1029 
1030 		/* add the cipher if it has not been added yet. */
1031 		if (rule == CIPHER_ADD) {
1032 			/* reverse == 0 */
1033 			if (!curr->active) {
1034 				ll_append_tail(&head, curr, &tail);
1035 				curr->active = 1;
1036 			}
1037 		}
1038 		/* Move the added cipher to this location */
1039 		else if (rule == CIPHER_ORD) {
1040 			/* reverse == 0 */
1041 			if (curr->active) {
1042 				ll_append_tail(&head, curr, &tail);
1043 			}
1044 		} else if (rule == CIPHER_DEL) {
1045 			/* reverse == 1 */
1046 			if (curr->active) {
1047 				/* most recently deleted ciphersuites get best positions
1048 				 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
1049 				 * works in reverse to maintain the order) */
1050 				ll_append_head(&head, curr, &tail);
1051 				curr->active = 0;
1052 			}
1053 		} else if (rule == CIPHER_KILL) {
1054 			/* reverse == 0 */
1055 			if (head == curr)
1056 				head = curr->next;
1057 			else
1058 				curr->prev->next = curr->next;
1059 			if (tail == curr)
1060 				tail = curr->prev;
1061 			curr->active = 0;
1062 			if (curr->next != NULL)
1063 				curr->next->prev = curr->prev;
1064 			if (curr->prev != NULL)
1065 				curr->prev->next = curr->next;
1066 			curr->next = NULL;
1067 			curr->prev = NULL;
1068 		}
1069 	}
1070 
1071 	*head_p = head;
1072 	*tail_p = tail;
1073 }
1074 
1075 static int
1076 ssl_cipher_strength_sort(CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
1077 {
1078 	int max_strength_bits, i, *number_uses;
1079 	CIPHER_ORDER *curr;
1080 
1081 	/*
1082 	 * This routine sorts the ciphers with descending strength. The sorting
1083 	 * must keep the pre-sorted sequence, so we apply the normal sorting
1084 	 * routine as '+' movement to the end of the list.
1085 	 */
1086 	max_strength_bits = 0;
1087 	curr = *head_p;
1088 	while (curr != NULL) {
1089 		if (curr->active &&
1090 		    (curr->cipher->strength_bits > max_strength_bits))
1091 			max_strength_bits = curr->cipher->strength_bits;
1092 		curr = curr->next;
1093 	}
1094 
1095 	number_uses = calloc((max_strength_bits + 1), sizeof(int));
1096 	if (!number_uses) {
1097 		SSLerr(SSL_F_SSL_CIPHER_STRENGTH_SORT, ERR_R_MALLOC_FAILURE);
1098 		return (0);
1099 	}
1100 
1101 	/*
1102 	 * Now find the strength_bits values actually used
1103 	 */
1104 	curr = *head_p;
1105 	while (curr != NULL) {
1106 		if (curr->active)
1107 			number_uses[curr->cipher->strength_bits]++;
1108 		curr = curr->next;
1109 	}
1110 	/*
1111 	 * Go through the list of used strength_bits values in descending
1112 	 * order.
1113 	 */
1114 	for (i = max_strength_bits; i >= 0; i--)
1115 		if (number_uses[i] > 0)
1116 			ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
1117 
1118 	free(number_uses);
1119 	return (1);
1120 }
1121 
1122 static int
1123 ssl_cipher_process_rulestr(const char *rule_str, CIPHER_ORDER **head_p,
1124     CIPHER_ORDER **tail_p, const SSL_CIPHER **ca_list)
1125 {
1126 	unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
1127 	unsigned long algo_strength;
1128 	int j, multi, found, rule, retval, ok, buflen;
1129 	unsigned long cipher_id = 0;
1130 	const char *l, *buf;
1131 	char ch;
1132 
1133 	retval = 1;
1134 	l = rule_str;
1135 	for (;;) {
1136 		ch = *l;
1137 
1138 		if (ch == '\0')
1139 			break;
1140 
1141 		if (ch == '-') {
1142 			rule = CIPHER_DEL;
1143 			l++;
1144 		} else if (ch == '+') {
1145 			rule = CIPHER_ORD;
1146 			l++;
1147 		} else if (ch == '!') {
1148 			rule = CIPHER_KILL;
1149 			l++;
1150 		} else if (ch == '@') {
1151 			rule = CIPHER_SPECIAL;
1152 			l++;
1153 		} else {
1154 			rule = CIPHER_ADD;
1155 		}
1156 
1157 		if (ITEM_SEP(ch)) {
1158 			l++;
1159 			continue;
1160 		}
1161 
1162 		alg_mkey = 0;
1163 		alg_auth = 0;
1164 		alg_enc = 0;
1165 		alg_mac = 0;
1166 		alg_ssl = 0;
1167 		algo_strength = 0;
1168 
1169 		for (;;) {
1170 			ch = *l;
1171 			buf = l;
1172 			buflen = 0;
1173 			while (((ch >= 'A') && (ch <= 'Z')) ||
1174 			    ((ch >= '0') && (ch <= '9')) ||
1175 			    ((ch >= 'a') && (ch <= 'z')) ||
1176 			    (ch == '-') || (ch == '.')) {
1177 				ch = *(++l);
1178 				buflen++;
1179 			}
1180 
1181 			if (buflen == 0) {
1182 				/*
1183 				 * We hit something we cannot deal with,
1184 				 * it is no command or separator nor
1185 				 * alphanumeric, so we call this an error.
1186 				 */
1187 				SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
1188 				    SSL_R_INVALID_COMMAND);
1189 				retval = found = 0;
1190 				l++;
1191 				break;
1192 			}
1193 
1194 			if (rule == CIPHER_SPECIAL) {
1195 				 /* unused -- avoid compiler warning */
1196 				found = 0;
1197 				/* special treatment */
1198 				break;
1199 			}
1200 
1201 			/* check for multi-part specification */
1202 			if (ch == '+') {
1203 				multi = 1;
1204 				l++;
1205 			} else
1206 				multi = 0;
1207 
1208 			/*
1209 			 * Now search for the cipher alias in the ca_list.
1210 			 * Be careful with the strncmp, because the "buflen"
1211 			 * limitation will make the rule "ADH:SOME" and the
1212 			 * cipher "ADH-MY-CIPHER" look like a match for
1213 			 * buflen=3. So additionally check whether the cipher
1214 			 * name found has the correct length. We can save a
1215 			 * strlen() call: just checking for the '\0' at the
1216 			 * right place is sufficient, we have to strncmp()
1217 			 * anyway (we cannot use strcmp(), because buf is not
1218 			 * '\0' terminated.)
1219 			 */
1220 			j = found = 0;
1221 			cipher_id = 0;
1222 			while (ca_list[j]) {
1223 				if (!strncmp(buf, ca_list[j]->name, buflen) &&
1224 				    (ca_list[j]->name[buflen] == '\0')) {
1225 					found = 1;
1226 					break;
1227 				} else
1228 					j++;
1229 			}
1230 
1231 			if (!found)
1232 				break;	/* ignore this entry */
1233 
1234 			if (ca_list[j]->algorithm_mkey) {
1235 				if (alg_mkey) {
1236 					alg_mkey &= ca_list[j]->algorithm_mkey;
1237 					if (!alg_mkey) {
1238 						found = 0;
1239 						break;
1240 					}
1241 				} else
1242 					alg_mkey = ca_list[j]->algorithm_mkey;
1243 			}
1244 
1245 			if (ca_list[j]->algorithm_auth) {
1246 				if (alg_auth) {
1247 					alg_auth &= ca_list[j]->algorithm_auth;
1248 					if (!alg_auth) {
1249 						found = 0;
1250 						break;
1251 					}
1252 				} else
1253 					alg_auth = ca_list[j]->algorithm_auth;
1254 			}
1255 
1256 			if (ca_list[j]->algorithm_enc) {
1257 				if (alg_enc) {
1258 					alg_enc &= ca_list[j]->algorithm_enc;
1259 					if (!alg_enc) {
1260 						found = 0;
1261 						break;
1262 					}
1263 				} else
1264 					alg_enc = ca_list[j]->algorithm_enc;
1265 			}
1266 
1267 			if (ca_list[j]->algorithm_mac) {
1268 				if (alg_mac) {
1269 					alg_mac &= ca_list[j]->algorithm_mac;
1270 					if (!alg_mac) {
1271 						found = 0;
1272 						break;
1273 					}
1274 				} else
1275 					alg_mac = ca_list[j]->algorithm_mac;
1276 			}
1277 
1278 			if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1279 				if (algo_strength & SSL_STRONG_MASK) {
1280 					algo_strength &=
1281 					    (ca_list[j]->algo_strength &
1282 					    SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1283 					if (!(algo_strength &
1284 					    SSL_STRONG_MASK)) {
1285 						found = 0;
1286 						break;
1287 					}
1288 				} else
1289 					algo_strength |=
1290 					    ca_list[j]->algo_strength &
1291 					    SSL_STRONG_MASK;
1292 			}
1293 
1294 			if (ca_list[j]->valid) {
1295 				/*
1296 				 * explicit ciphersuite found; its protocol
1297 				 * version does not become part of the search
1298 				 * pattern!
1299 				 */
1300 				cipher_id = ca_list[j]->id;
1301 			} else {
1302 				/*
1303 				 * not an explicit ciphersuite; only in this
1304 				 * case, the protocol version is considered
1305 				 * part of the search pattern
1306 				 */
1307 				if (ca_list[j]->algorithm_ssl) {
1308 					if (alg_ssl) {
1309 						alg_ssl &=
1310 						    ca_list[j]->algorithm_ssl;
1311 						if (!alg_ssl) {
1312 							found = 0;
1313 							break;
1314 						}
1315 					} else
1316 						alg_ssl =
1317 						    ca_list[j]->algorithm_ssl;
1318 				}
1319 			}
1320 
1321 			if (!multi)
1322 				break;
1323 		}
1324 
1325 		/*
1326 		 * Ok, we have the rule, now apply it
1327 		 */
1328 		if (rule == CIPHER_SPECIAL) {
1329 			/* special command */
1330 			ok = 0;
1331 			if ((buflen == 8) && !strncmp(buf, "STRENGTH", 8))
1332 				ok = ssl_cipher_strength_sort(head_p, tail_p);
1333 			else
1334 				SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
1335 				    SSL_R_INVALID_COMMAND);
1336 			if (ok == 0)
1337 				retval = 0;
1338 			/*
1339 			 * We do not support any "multi" options
1340 			 * together with "@", so throw away the
1341 			 * rest of the command, if any left, until
1342 			 * end or ':' is found.
1343 			 */
1344 			while ((*l != '\0') && !ITEM_SEP(*l))
1345 				l++;
1346 		} else if (found) {
1347 			ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth,
1348 			    alg_enc, alg_mac, alg_ssl, algo_strength, rule,
1349 			    -1, head_p, tail_p);
1350 		} else {
1351 			while ((*l != '\0') && !ITEM_SEP(*l))
1352 				l++;
1353 		}
1354 		if (*l == '\0')
1355 			break; /* done */
1356 	}
1357 
1358 	return (retval);
1359 }
1360 
1361 STACK_OF(SSL_CIPHER) *
1362 ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1363     STACK_OF(SSL_CIPHER) **cipher_list,
1364     STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1365     const char *rule_str)
1366 {
1367 	int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1368 	unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
1369 	STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
1370 	const char *rule_p;
1371 	CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1372 	const SSL_CIPHER **ca_list = NULL;
1373 
1374 	/*
1375 	 * Return with error if nothing to do.
1376 	 */
1377 	if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
1378 		return NULL;
1379 
1380 	/*
1381 	 * To reduce the work to do we only want to process the compiled
1382 	 * in algorithms, so we first get the mask of disabled ciphers.
1383 	 */
1384 	ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1385 
1386 	/*
1387 	 * Now we have to collect the available ciphers from the compiled
1388 	 * in ciphers. We cannot get more than the number compiled in, so
1389 	 * it is used for allocation.
1390 	 */
1391 	num_of_ciphers = ssl_method->num_ciphers();
1392 	co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER));
1393 	if (co_list == NULL) {
1394 		SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1395 		return(NULL);	/* Failure */
1396 	}
1397 
1398 	ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1399 	disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1400 	co_list, &head, &tail);
1401 
1402 
1403 	/* Now arrange all ciphers by preference: */
1404 
1405 	/* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1406 	ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1407 	ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1408 
1409 	/*
1410 	 * CHACHA20 is fast and safe on all hardware and is thus our preferred
1411 	 * symmetric cipher, with AES second.
1412 	 */
1413 	ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1414 	ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1415 
1416 	/* Temporarily enable everything else for sorting */
1417 	ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1418 
1419 	/* Low priority for MD5 */
1420 	ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
1421 
1422 	/* Move anonymous ciphers to the end.  Usually, these will remain disabled.
1423 	 * (For applications that allow them, they aren't too bad, but we prefer
1424 	 * authenticated ciphers.) */
1425 	ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1426 
1427 	/* Move ciphers without forward secrecy to the end */
1428 	ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1429 	ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1430 
1431 	/* RC4 is sort-of broken -- move the the end */
1432 	ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1433 
1434 	/* Now sort by symmetric encryption strength.  The above ordering remains
1435 	 * in force within each class */
1436 	if (!ssl_cipher_strength_sort(&head, &tail)) {
1437 		free(co_list);
1438 		return NULL;
1439 	}
1440 
1441 	/* Now disable everything (maintaining the ordering!) */
1442 	ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1443 
1444 
1445 	/*
1446 	 * We also need cipher aliases for selecting based on the rule_str.
1447 	 * There might be two types of entries in the rule_str: 1) names
1448 	 * of ciphers themselves 2) aliases for groups of ciphers.
1449 	 * For 1) we need the available ciphers and for 2) the cipher
1450 	 * groups of cipher_aliases added together in one list (otherwise
1451 	 * we would be happy with just the cipher_aliases table).
1452 	 */
1453 	num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1454 	num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1455 	ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *));
1456 	if (ca_list == NULL) {
1457 		free(co_list);
1458 		SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1459 		return(NULL);	/* Failure */
1460 	}
1461 	ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1462 	disabled_mkey, disabled_auth, disabled_enc,
1463 	disabled_mac, disabled_ssl, head);
1464 
1465 	/*
1466 	 * If the rule_string begins with DEFAULT, apply the default rule
1467 	 * before using the (possibly available) additional rules.
1468 	 */
1469 	ok = 1;
1470 	rule_p = rule_str;
1471 	if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1472 		ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1473 		&head, &tail, ca_list);
1474 		rule_p += 7;
1475 		if (*rule_p == ':')
1476 			rule_p++;
1477 	}
1478 
1479 	if (ok && (strlen(rule_p) > 0))
1480 		ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1481 
1482 	free((void *)ca_list);	/* Not needed anymore */
1483 
1484 	if (!ok) {
1485 		/* Rule processing failure */
1486 		free(co_list);
1487 		return (NULL);
1488 	}
1489 
1490 	/*
1491 	 * Allocate new "cipherstack" for the result, return with error
1492 	 * if we cannot get one.
1493 	 */
1494 	if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1495 		free(co_list);
1496 		return (NULL);
1497 	}
1498 
1499 	/*
1500 	 * The cipher selection for the list is done. The ciphers are added
1501 	 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1502 	 */
1503 	for (curr = head; curr != NULL; curr = curr->next) {
1504 		if (curr->active) {
1505 			sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1506 		}
1507 	}
1508 	free(co_list);	/* Not needed any longer */
1509 
1510 	tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1511 	if (tmp_cipher_list == NULL) {
1512 		sk_SSL_CIPHER_free(cipherstack);
1513 		return NULL;
1514 	}
1515 	if (*cipher_list != NULL)
1516 		sk_SSL_CIPHER_free(*cipher_list);
1517 	*cipher_list = cipherstack;
1518 	if (*cipher_list_by_id != NULL)
1519 		sk_SSL_CIPHER_free(*cipher_list_by_id);
1520 	*cipher_list_by_id = tmp_cipher_list;
1521 	(void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,
1522 	    ssl_cipher_ptr_id_cmp);
1523 
1524 	sk_SSL_CIPHER_sort(*cipher_list_by_id);
1525 	return (cipherstack);
1526 }
1527 
1528 const SSL_CIPHER *
1529 SSL_CIPHER_get_by_id(unsigned int id)
1530 {
1531 	return ssl3_get_cipher_by_id(id);
1532 }
1533 
1534 const SSL_CIPHER *
1535 SSL_CIPHER_get_by_value(uint16_t value)
1536 {
1537 	return ssl3_get_cipher_by_value(value);
1538 }
1539 
1540 char *
1541 SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1542 {
1543 	unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, alg2;
1544 	const char *ver, *kx, *au, *enc, *mac;
1545 	char *ret;
1546 	int l;
1547 
1548 	alg_mkey = cipher->algorithm_mkey;
1549 	alg_auth = cipher->algorithm_auth;
1550 	alg_enc = cipher->algorithm_enc;
1551 	alg_mac = cipher->algorithm_mac;
1552 	alg_ssl = cipher->algorithm_ssl;
1553 
1554 	alg2 = cipher->algorithm2;
1555 
1556 	if (alg_ssl & SSL_SSLV3)
1557 		ver = "SSLv3";
1558 	else if (alg_ssl & SSL_TLSV1_2)
1559 		ver = "TLSv1.2";
1560 	else
1561 		ver = "unknown";
1562 
1563 	switch (alg_mkey) {
1564 	case SSL_kRSA:
1565 		kx = "RSA";
1566 		break;
1567 	case SSL_kDHE:
1568 		kx = "DH";
1569 		break;
1570 	case SSL_kECDHr:
1571 		kx = "ECDH/RSA";
1572 		break;
1573 	case SSL_kECDHe:
1574 		kx = "ECDH/ECDSA";
1575 		break;
1576 	case SSL_kECDHE:
1577 		kx = "ECDH";
1578 		break;
1579 	case SSL_kGOST:
1580 		kx = "GOST";
1581 		break;
1582 	default:
1583 		kx = "unknown";
1584 	}
1585 
1586 	switch (alg_auth) {
1587 	case SSL_aRSA:
1588 		au = "RSA";
1589 		break;
1590 	case SSL_aDSS:
1591 		au = "DSS";
1592 		break;
1593 	case SSL_aECDH:
1594 		au = "ECDH";
1595 		break;
1596 	case SSL_aNULL:
1597 		au = "None";
1598 		break;
1599 	case SSL_aECDSA:
1600 		au = "ECDSA";
1601 		break;
1602 	case SSL_aGOST01:
1603 		au = "GOST01";
1604 		break;
1605 	default:
1606 		au = "unknown";
1607 		break;
1608 	}
1609 
1610 	switch (alg_enc) {
1611 	case SSL_DES:
1612 		enc = "DES(56)";
1613 		break;
1614 	case SSL_3DES:
1615 		enc = "3DES(168)";
1616 		break;
1617 	case SSL_RC4:
1618 		enc = alg2 & SSL2_CF_8_BYTE_ENC ? "RC4(64)" : "RC4(128)";
1619 		break;
1620 	case SSL_IDEA:
1621 		enc = "IDEA(128)";
1622 		break;
1623 	case SSL_eNULL:
1624 		enc = "None";
1625 		break;
1626 	case SSL_AES128:
1627 		enc = "AES(128)";
1628 		break;
1629 	case SSL_AES256:
1630 		enc = "AES(256)";
1631 		break;
1632 	case SSL_AES128GCM:
1633 		enc = "AESGCM(128)";
1634 		break;
1635 	case SSL_AES256GCM:
1636 		enc = "AESGCM(256)";
1637 		break;
1638 	case SSL_CAMELLIA128:
1639 		enc = "Camellia(128)";
1640 		break;
1641 	case SSL_CAMELLIA256:
1642 		enc = "Camellia(256)";
1643 		break;
1644 	case SSL_CHACHA20POLY1305:
1645 		enc = "ChaCha20-Poly1305";
1646 		break;
1647 	case SSL_eGOST2814789CNT:
1648 		enc = "GOST-28178-89-CNT";
1649 		break;
1650 	default:
1651 		enc = "unknown";
1652 		break;
1653 	}
1654 
1655 	switch (alg_mac) {
1656 	case SSL_MD5:
1657 		mac = "MD5";
1658 		break;
1659 	case SSL_SHA1:
1660 		mac = "SHA1";
1661 		break;
1662 	case SSL_SHA256:
1663 		mac = "SHA256";
1664 		break;
1665 	case SSL_SHA384:
1666 		mac = "SHA384";
1667 		break;
1668 	case SSL_AEAD:
1669 		mac = "AEAD";
1670 		break;
1671 	case SSL_GOST94:
1672 		mac = "GOST94";
1673 		break;
1674 	case SSL_GOST89MAC:
1675 		mac = "GOST89IMIT";
1676 		break;
1677 	case SSL_STREEBOG256:
1678 		mac = "STREEBOG256";
1679 		break;
1680 	case SSL_STREEBOG512:
1681 		mac = "STREEBOG512";
1682 		break;
1683 	default:
1684 		mac = "unknown";
1685 		break;
1686 	}
1687 
1688 	if (asprintf(&ret, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1689 	    cipher->name, ver, kx, au, enc, mac) == -1)
1690 		return "OPENSSL_malloc Error";
1691 
1692 	if (buf != NULL) {
1693 		l = strlcpy(buf, ret, len);
1694 		free(ret);
1695 		ret = buf;
1696 		if (l >= len)
1697 			ret = "Buffer too small";
1698 	}
1699 
1700 	return (ret);
1701 }
1702 
1703 char *
1704 SSL_CIPHER_get_version(const SSL_CIPHER *c)
1705 {
1706 	if (c == NULL)
1707 		return("(NONE)");
1708 	if ((c->id >> 24) == 3)
1709 		return("TLSv1/SSLv3");
1710 	else
1711 		return("unknown");
1712 }
1713 
1714 /* return the actual cipher being used */
1715 const char *
1716 SSL_CIPHER_get_name(const SSL_CIPHER *c)
1717 {
1718 	if (c != NULL)
1719 		return (c->name);
1720 	return("(NONE)");
1721 }
1722 
1723 /* number of bits for symmetric cipher */
1724 int
1725 SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1726 {
1727 	int ret = 0;
1728 
1729 	if (c != NULL) {
1730 		if (alg_bits != NULL)
1731 			*alg_bits = c->alg_bits;
1732 		ret = c->strength_bits;
1733 	}
1734 	return (ret);
1735 }
1736 
1737 unsigned long
1738 SSL_CIPHER_get_id(const SSL_CIPHER *c)
1739 {
1740 	return c->id;
1741 }
1742 
1743 uint16_t
1744 SSL_CIPHER_get_value(const SSL_CIPHER *c)
1745 {
1746 	return ssl3_cipher_get_value(c);
1747 }
1748 
1749 void *
1750 SSL_COMP_get_compression_methods(void)
1751 {
1752 	return NULL;
1753 }
1754 
1755 int
1756 SSL_COMP_add_compression_method(int id, void *cm)
1757 {
1758 	return 1;
1759 }
1760 
1761 const char *
1762 SSL_COMP_get_name(const void *comp)
1763 {
1764 	return NULL;
1765 }
1766