blob: 0caed0b416629cf258def2facc3071b33c20ddb8 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
141#include <stdio.h>
142#include <assert.h>
143
Adam Langley95c29f32014-06-20 12:00:00 -0700144#include <openssl/engine.h>
145#include <openssl/mem.h>
146#include <openssl/obj.h>
147
148#include "ssl_locl.h"
149
David Benjamind0639af2014-08-19 15:25:49 -0400150#define SSL_ENC_3DES_IDX 0
151#define SSL_ENC_RC4_IDX 1
152#define SSL_ENC_AES128_IDX 2
153#define SSL_ENC_AES256_IDX 3
154#define SSL_ENC_NUM_IDX 4
Adam Langley95c29f32014-06-20 12:00:00 -0700155
156
David Benjamin019c3cc2014-07-14 23:13:22 -0400157static const EVP_CIPHER *ssl_cipher_methods[SSL_ENC_NUM_IDX]= { 0 };
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langley95c29f32014-06-20 12:00:00 -0700159#define SSL_MD_MD5_IDX 0
160#define SSL_MD_SHA1_IDX 1
David Benjamin019c3cc2014-07-14 23:13:22 -0400161#define SSL_MD_SHA256_IDX 2
162#define SSL_MD_SHA384_IDX 3
Adam Langley95c29f32014-06-20 12:00:00 -0700163/*Constant SSL_MAX_DIGEST equal to size of digests array should be
164 * defined in the
165 * ssl_locl.h */
166#define SSL_MD_NUM_IDX SSL_MAX_DIGEST
David Benjamin019c3cc2014-07-14 23:13:22 -0400167static const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX] = { 0 };
David Benjamincff64722014-08-19 19:54:46 -0400168static const int ssl_mac_pkey_id[SSL_MD_NUM_IDX]={
David Benjamin019c3cc2014-07-14 23:13:22 -0400169 EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC, EVP_PKEY_HMAC,
Adam Langley95c29f32014-06-20 12:00:00 -0700170 };
171
David Benjamin019c3cc2014-07-14 23:13:22 -0400172static int ssl_mac_secret_size[SSL_MD_NUM_IDX] = { 0 };
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamincff64722014-08-19 19:54:46 -0400174static const int ssl_handshake_digest_flag[SSL_MD_NUM_IDX]={
David Benjamin019c3cc2014-07-14 23:13:22 -0400175 SSL_HANDSHAKE_MAC_MD5, SSL_HANDSHAKE_MAC_SHA,
176 SSL_HANDSHAKE_MAC_SHA256, SSL_HANDSHAKE_MAC_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700177 };
178
179#define CIPHER_ADD 1
180#define CIPHER_KILL 2
181#define CIPHER_DEL 3
182#define CIPHER_ORD 4
183#define CIPHER_SPECIAL 5
184
185typedef struct cipher_order_st
186 {
187 const SSL_CIPHER *cipher;
188 int active;
189 int dead;
Adam Langley858a88d2014-06-20 12:00:00 -0700190 int in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700191 struct cipher_order_st *next,*prev;
192 } CIPHER_ORDER;
193
194static const SSL_CIPHER cipher_aliases[]={
David Benjaminebf42b52014-08-19 15:18:42 -0400195 {0,SSL_TXT_ALL,0, 0,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700196
197 /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
David Benjaminebf42b52014-08-19 15:18:42 -0400198 {0,SSL_TXT_CMPDEF,0, SSL_kEDH|SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700199
200 /* key exchange aliases
201 * (some of those using only a single bit here combine
202 * multiple key exchange algs according to the RFCs,
203 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
204 {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
205
Adam Langley95c29f32014-06-20 12:00:00 -0700206 {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400207 {0,SSL_TXT_DH,0, SSL_kEDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley95c29f32014-06-20 12:00:00 -0700209 {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400210 {0,SSL_TXT_ECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700211
212 {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700213
214 /* server authentication aliases */
215 {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700216 {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700217 {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
218 {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
219 {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700220
221 /* aliases combining key exchange and server authentication */
222 {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
223 {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700224 {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
225 {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
226 {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
227 {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700228
229
230 /* symmetric encryption aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700231 {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
232 {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700233 {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
234 {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
235 {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
236 {0,SSL_TXT_AES_GCM,0, 0,0,SSL_AES128GCM|SSL_AES256GCM,0,0,0,0,0,0},
Adam Langleyde0b2022014-06-20 12:00:00 -0700237 {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700238
239 /* MAC aliases */
240 {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
241 {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
242 {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700243 {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
244 {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
245
246 /* protocol version aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700247 {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
248 {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
249 {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
250
Adam Langley95c29f32014-06-20 12:00:00 -0700251 /* strength classes */
Adam Langley95c29f32014-06-20 12:00:00 -0700252 {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
253 {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
254 /* FIPS 140-2 approved ciphersuite */
David Benjaminebf42b52014-08-19 15:18:42 -0400255 {0,SSL_TXT_FIPS,0, 0,0,0,0,0,SSL_FIPS, 0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700256 };
257
258void ssl_load_ciphers(void)
259 {
Adam Langley95c29f32014-06-20 12:00:00 -0700260 ssl_cipher_methods[SSL_ENC_3DES_IDX]= EVP_des_ede3_cbc();
261 ssl_cipher_methods[SSL_ENC_RC4_IDX]= EVP_rc4();
262 ssl_cipher_methods[SSL_ENC_AES128_IDX]= EVP_aes_128_cbc();
263 ssl_cipher_methods[SSL_ENC_AES256_IDX]= EVP_aes_256_cbc();
264
Adam Langley95c29f32014-06-20 12:00:00 -0700265 ssl_digest_methods[SSL_MD_MD5_IDX]= EVP_md5();
266 ssl_mac_secret_size[SSL_MD_MD5_IDX]= EVP_MD_size(EVP_md5());
267 assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
268 ssl_digest_methods[SSL_MD_SHA1_IDX]=EVP_sha1();
269 ssl_mac_secret_size[SSL_MD_SHA1_IDX]= EVP_MD_size(EVP_sha1());
270 assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
271
272 ssl_digest_methods[SSL_MD_SHA256_IDX]= EVP_sha256();
273 ssl_mac_secret_size[SSL_MD_SHA256_IDX]= EVP_MD_size(EVP_sha256());
274 ssl_digest_methods[SSL_MD_SHA384_IDX]= EVP_sha384();
275 ssl_mac_secret_size[SSL_MD_SHA384_IDX]= EVP_MD_size(EVP_sha384());
276 }
277
Adam Langleyc9fb3752014-06-20 12:00:00 -0700278/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
279 * for |s->cipher|. It returns 1 on success and 0 on error. */
280int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
281 {
282 const SSL_CIPHER *c = s->cipher;
283
284 *aead = NULL;
285
286 if (c == NULL)
287 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700288 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0 &&
289 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) == 0)
Adam Langleyc9fb3752014-06-20 12:00:00 -0700290 return 0;
291
292#ifndef OPENSSL_NO_AES
Adam Langleyde0b2022014-06-20 12:00:00 -0700293 switch (c->algorithm_enc)
294 {
295 case SSL_AES128GCM:
296 *aead = EVP_aead_aes_128_gcm();
297 return 1;
Adam Langley5c270c52014-06-20 12:00:00 -0700298 case SSL_AES256GCM:
299 *aead = EVP_aead_aes_256_gcm();
300 return 1;
Adam Langleyde0b2022014-06-20 12:00:00 -0700301 case SSL_CHACHA20POLY1305:
302 *aead = EVP_aead_chacha20_poly1305();
303 return 1;
Adam Langley9447dff2014-06-24 17:29:06 -0700304 case SSL_RC4:
305 if (c->algorithm_mac == SSL_MD5)
306 *aead = EVP_aead_rc4_md5_tls();
307 else
308 return 0;
309 return 1;
310 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700311#endif
312
313 return 0;
314 }
315
316int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
317 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
318 {
Adam Langley95c29f32014-06-20 12:00:00 -0700319 int i;
320 const SSL_CIPHER *c;
321
322 c=s->cipher;
323 if (c == NULL) return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleyc9fb3752014-06-20 12:00:00 -0700325 /* This function doesn't deal with EVP_AEAD. See
326 * |ssl_cipher_get_aead_evp|. */
327 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
328 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700329
330 if ((enc == NULL) || (md == NULL)) return(0);
331
332 switch (c->algorithm_enc)
333 {
Adam Langley95c29f32014-06-20 12:00:00 -0700334 case SSL_3DES:
335 i=SSL_ENC_3DES_IDX;
336 break;
337 case SSL_RC4:
338 i=SSL_ENC_RC4_IDX;
339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340 case SSL_AES128:
341 i=SSL_ENC_AES128_IDX;
342 break;
343 case SSL_AES256:
344 i=SSL_ENC_AES256_IDX;
345 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700346 default:
347 i= -1;
348 break;
349 }
350
Adam Langley4c65f3a2014-06-20 12:00:00 -0700351 if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
Adam Langley95c29f32014-06-20 12:00:00 -0700352 *enc=NULL;
353 else
Adam Langley95c29f32014-06-20 12:00:00 -0700354 *enc=ssl_cipher_methods[i];
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langley9447dff2014-06-24 17:29:06 -0700356 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size))
357 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700358
359 if ((*enc != NULL) &&
360 (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
361 (!mac_pkey_type||*mac_pkey_type != NID_undef))
362 {
363 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
364 s->ssl_version < TLS1_VERSION)
365 return 1;
366
Adam Langley95c29f32014-06-20 12:00:00 -0700367 /* TODO(fork): enable the stitched cipher modes. */
368#if 0
369 if (c->algorithm_enc == SSL_RC4 &&
370 c->algorithm_mac == SSL_MD5 &&
371 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
372 *enc = evp, *md = NULL;
373 else if (c->algorithm_enc == SSL_AES128 &&
374 c->algorithm_mac == SSL_SHA1 &&
375 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
376 *enc = evp, *md = NULL;
377 else if (c->algorithm_enc == SSL_AES256 &&
378 c->algorithm_mac == SSL_SHA1 &&
379 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
380 *enc = evp, *md = NULL;
381#endif
382 return(1);
383 }
384 else
385 return(0);
386 }
387
Adam Langley9447dff2014-06-24 17:29:06 -0700388int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
389 {
390 int i;
391 const SSL_CIPHER *c;
392
393 c=s->cipher;
394 if (c == NULL) return(0);
395
396 switch (c->algorithm_mac)
397 {
398 case SSL_MD5:
399 i=SSL_MD_MD5_IDX;
400 break;
401 case SSL_SHA1:
402 i=SSL_MD_SHA1_IDX;
403 break;
404 case SSL_SHA256:
405 i=SSL_MD_SHA256_IDX;
406 break;
407 case SSL_SHA384:
408 i=SSL_MD_SHA384_IDX;
409 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700410 default:
411 i= -1;
412 break;
413 }
414
415 if ((i < 0) || (i >= SSL_MD_NUM_IDX))
416 {
417 *md=NULL;
418 if (mac_pkey_type!=NULL) *mac_pkey_type = NID_undef;
419 if (mac_secret_size!=NULL) *mac_secret_size = 0;
420 }
421 else
422 {
423 *md=ssl_digest_methods[i];
424 if (mac_pkey_type!=NULL) *mac_pkey_type = ssl_mac_pkey_id[i];
425 if (mac_secret_size!=NULL) *mac_secret_size = ssl_mac_secret_size[i];
426 }
427
428 return 1;
429 }
430
Adam Langley95c29f32014-06-20 12:00:00 -0700431int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
432{
433 if (idx <0||idx>=SSL_MD_NUM_IDX)
434 {
435 return 0;
436 }
437 *mask = ssl_handshake_digest_flag[idx];
438 if (*mask)
439 *md = ssl_digest_methods[idx];
440 else
441 *md = NULL;
442 return 1;
443}
444
445#define ITEM_SEP(a) \
446 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
447
448static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
449 CIPHER_ORDER **tail)
450 {
451 if (curr == *tail) return;
452 if (curr == *head)
453 *head=curr->next;
454 if (curr->prev != NULL)
455 curr->prev->next=curr->next;
456 if (curr->next != NULL)
457 curr->next->prev=curr->prev;
458 (*tail)->next=curr;
459 curr->prev= *tail;
460 curr->next=NULL;
461 *tail=curr;
462 }
463
464static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
465 CIPHER_ORDER **tail)
466 {
467 if (curr == *head) return;
468 if (curr == *tail)
469 *tail=curr->prev;
470 if (curr->next != NULL)
471 curr->next->prev=curr->prev;
472 if (curr->prev != NULL)
473 curr->prev->next=curr->next;
474 (*head)->prev=curr;
475 curr->next= *head;
476 curr->prev=NULL;
477 *head=curr;
478 }
479
480static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth, unsigned long *enc, unsigned long *mac, unsigned long *ssl)
481 {
482 *mkey = 0;
483 *auth = 0;
484 *enc = 0;
485 *mac = 0;
486 *ssl = 0;
487
Adam Langley95c29f32014-06-20 12:00:00 -0700488 *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
489 *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
Adam Langley95c29f32014-06-20 12:00:00 -0700490 *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128:0;
491 *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256:0;
Adam Langley95c29f32014-06-20 12:00:00 -0700492
493 *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
494 *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
495 *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256:0;
496 *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384:0;
Adam Langley95c29f32014-06-20 12:00:00 -0700497
498 }
499
500static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
501 int num_of_ciphers,
502 unsigned long disabled_mkey, unsigned long disabled_auth,
503 unsigned long disabled_enc, unsigned long disabled_mac,
504 unsigned long disabled_ssl,
505 CIPHER_ORDER *co_list,
506 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
507 {
508 int i, co_list_num;
509 const SSL_CIPHER *c;
510
511 /*
512 * We have num_of_ciphers descriptions compiled in, depending on the
513 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
514 * These will later be sorted in a linked list with at most num
515 * entries.
516 */
517
518 /* Get the initial list of ciphers */
519 co_list_num = 0; /* actual count of ciphers */
520 for (i = 0; i < num_of_ciphers; i++)
521 {
522 c = ssl_method->get_cipher(i);
523 /* drop those that use any of that is not available */
524 if ((c != NULL) && c->valid &&
Adam Langley95c29f32014-06-20 12:00:00 -0700525 !(c->algorithm_mkey & disabled_mkey) &&
526 !(c->algorithm_auth & disabled_auth) &&
527 !(c->algorithm_enc & disabled_enc) &&
528 !(c->algorithm_mac & disabled_mac) &&
529 !(c->algorithm_ssl & disabled_ssl))
530 {
531 co_list[co_list_num].cipher = c;
532 co_list[co_list_num].next = NULL;
533 co_list[co_list_num].prev = NULL;
534 co_list[co_list_num].active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700535 co_list[co_list_num].in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700536 co_list_num++;
537#ifdef KSSL_DEBUG
538 printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
539#endif /* KSSL_DEBUG */
540 /*
541 if (!sk_push(ca_list,(char *)c)) goto err;
542 */
543 }
544 }
545
546 /*
547 * Prepare linked list from list entries
548 */
549 if (co_list_num > 0)
550 {
551 co_list[0].prev = NULL;
552
553 if (co_list_num > 1)
554 {
555 co_list[0].next = &co_list[1];
556
557 for (i = 1; i < co_list_num - 1; i++)
558 {
559 co_list[i].prev = &co_list[i - 1];
560 co_list[i].next = &co_list[i + 1];
561 }
562
563 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
564 }
565
566 co_list[co_list_num - 1].next = NULL;
567
568 *head_p = &co_list[0];
569 *tail_p = &co_list[co_list_num - 1];
570 }
571 }
572
573static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
574 int num_of_group_aliases,
575 unsigned long disabled_mkey, unsigned long disabled_auth,
576 unsigned long disabled_enc, unsigned long disabled_mac,
577 unsigned long disabled_ssl,
578 CIPHER_ORDER *head)
579 {
580 CIPHER_ORDER *ciph_curr;
581 const SSL_CIPHER **ca_curr;
582 int i;
583 unsigned long mask_mkey = ~disabled_mkey;
584 unsigned long mask_auth = ~disabled_auth;
585 unsigned long mask_enc = ~disabled_enc;
586 unsigned long mask_mac = ~disabled_mac;
587 unsigned long mask_ssl = ~disabled_ssl;
588
589 /*
590 * First, add the real ciphers as already collected
591 */
592 ciph_curr = head;
593 ca_curr = ca_list;
594 while (ciph_curr != NULL)
595 {
596 *ca_curr = ciph_curr->cipher;
597 ca_curr++;
598 ciph_curr = ciph_curr->next;
599 }
600
601 /*
602 * Now we add the available ones from the cipher_aliases[] table.
603 * They represent either one or more algorithms, some of which
604 * in any affected category must be supported (set in enabled_mask),
605 * or represent a cipher strength value (will be added in any case because algorithms=0).
606 */
607 for (i = 0; i < num_of_group_aliases; i++)
608 {
609 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
610 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
611 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
612 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
613 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
614
615 if (algorithm_mkey)
616 if ((algorithm_mkey & mask_mkey) == 0)
617 continue;
618
619 if (algorithm_auth)
620 if ((algorithm_auth & mask_auth) == 0)
621 continue;
622
623 if (algorithm_enc)
624 if ((algorithm_enc & mask_enc) == 0)
625 continue;
626
627 if (algorithm_mac)
628 if ((algorithm_mac & mask_mac) == 0)
629 continue;
630
631 if (algorithm_ssl)
632 if ((algorithm_ssl & mask_ssl) == 0)
633 continue;
634
David Benjamin6f260012014-08-15 13:49:12 -0400635 *ca_curr = cipher_aliases + i;
Adam Langley95c29f32014-06-20 12:00:00 -0700636 ca_curr++;
637 }
638
639 *ca_curr = NULL; /* end of list */
640 }
641
642static void ssl_cipher_apply_rule(unsigned long cipher_id,
643 unsigned long alg_mkey, unsigned long alg_auth,
644 unsigned long alg_enc, unsigned long alg_mac,
645 unsigned long alg_ssl,
646 unsigned long algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700647 int rule, int strength_bits, int in_group,
Adam Langley95c29f32014-06-20 12:00:00 -0700648 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
649 {
Adam Langleye3142a72014-07-24 17:56:48 -0700650 CIPHER_ORDER *head, *tail, *curr, *next, *last;
Adam Langley95c29f32014-06-20 12:00:00 -0700651 const SSL_CIPHER *cp;
652 int reverse = 0;
653
654#ifdef CIPHER_DEBUG
Adam Langley858a88d2014-06-20 12:00:00 -0700655 printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
656 rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
Adam Langley95c29f32014-06-20 12:00:00 -0700657#endif
658
659 if (rule == CIPHER_DEL)
660 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
661
662 head = *head_p;
663 tail = *tail_p;
664
665 if (reverse)
666 {
Adam Langleye3142a72014-07-24 17:56:48 -0700667 next = tail;
Adam Langley95c29f32014-06-20 12:00:00 -0700668 last = head;
669 }
670 else
671 {
Adam Langleye3142a72014-07-24 17:56:48 -0700672 next = head;
Adam Langley95c29f32014-06-20 12:00:00 -0700673 last = tail;
674 }
675
Adam Langleye3142a72014-07-24 17:56:48 -0700676 curr = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700677 for (;;)
678 {
Adam Langleye3142a72014-07-24 17:56:48 -0700679 if (curr == last) break;
680
681 curr = next;
682
683 if (curr == NULL) break;
684
685 next = reverse ? curr->prev : curr->next;
Adam Langley95c29f32014-06-20 12:00:00 -0700686
687 cp = curr->cipher;
688
689 /*
690 * Selection criteria is either the value of strength_bits
691 * or the algorithms used.
692 */
693 if (strength_bits >= 0)
694 {
695 if (strength_bits != cp->strength_bits)
696 continue;
697 }
698 else
699 {
700#ifdef CIPHER_DEBUG
701 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);
702#endif
703#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
704 if (cipher_id && cipher_id != cp->id)
705 continue;
706#endif
707 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
708 continue;
709 if (alg_auth && !(alg_auth & cp->algorithm_auth))
710 continue;
711 if (alg_enc && !(alg_enc & cp->algorithm_enc))
712 continue;
713 if (alg_mac && !(alg_mac & cp->algorithm_mac))
714 continue;
715 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
716 continue;
717 if ((algo_strength & SSL_EXP_MASK) && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
718 continue;
719 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
720 continue;
721 }
722
723#ifdef CIPHER_DEBUG
724 printf("Action = %d\n", rule);
725#endif
726
727 /* add the cipher if it has not been added yet. */
728 if (rule == CIPHER_ADD)
729 {
730 /* reverse == 0 */
731 if (!curr->active)
732 {
733 ll_append_tail(&head, curr, &tail);
734 curr->active = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700735 curr->in_group = in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700736 }
737 }
738 /* Move the added cipher to this location */
739 else if (rule == CIPHER_ORD)
740 {
741 /* reverse == 0 */
742 if (curr->active)
743 {
744 ll_append_tail(&head, curr, &tail);
Adam Langley858a88d2014-06-20 12:00:00 -0700745 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700746 }
747 }
748 else if (rule == CIPHER_DEL)
749 {
750 /* reverse == 1 */
751 if (curr->active)
752 {
753 /* most recently deleted ciphersuites get best positions
754 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
755 * works in reverse to maintain the order) */
756 ll_append_head(&head, curr, &tail);
757 curr->active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700758 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700759 }
760 }
761 else if (rule == CIPHER_KILL)
762 {
763 /* reverse == 0 */
764 if (head == curr)
765 head = curr->next;
766 else
767 curr->prev->next = curr->next;
768 if (tail == curr)
769 tail = curr->prev;
770 curr->active = 0;
771 if (curr->next != NULL)
772 curr->next->prev = curr->prev;
773 if (curr->prev != NULL)
774 curr->prev->next = curr->next;
775 curr->next = NULL;
776 curr->prev = NULL;
777 }
778 }
779
780 *head_p = head;
781 *tail_p = tail;
782 }
783
784static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
785 CIPHER_ORDER **tail_p)
786 {
787 int max_strength_bits, i, *number_uses;
788 CIPHER_ORDER *curr;
789
790 /*
791 * This routine sorts the ciphers with descending strength. The sorting
792 * must keep the pre-sorted sequence, so we apply the normal sorting
793 * routine as '+' movement to the end of the list.
794 */
795 max_strength_bits = 0;
796 curr = *head_p;
797 while (curr != NULL)
798 {
799 if (curr->active &&
800 (curr->cipher->strength_bits > max_strength_bits))
801 max_strength_bits = curr->cipher->strength_bits;
802 curr = curr->next;
803 }
804
805 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
806 if (!number_uses)
807 {
808 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
809 return(0);
810 }
811 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
812
813 /*
814 * Now find the strength_bits values actually used
815 */
816 curr = *head_p;
817 while (curr != NULL)
818 {
819 if (curr->active)
820 number_uses[curr->cipher->strength_bits]++;
821 curr = curr->next;
822 }
823 /*
824 * Go through the list of used strength_bits values in descending
825 * order.
826 */
827 for (i = max_strength_bits; i >= 0; i--)
828 if (number_uses[i] > 0)
Adam Langley858a88d2014-06-20 12:00:00 -0700829 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 -0700830
831 OPENSSL_free(number_uses);
832 return(1);
833 }
834
835static int ssl_cipher_process_rulestr(const char *rule_str,
836 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
837 const SSL_CIPHER **ca_list)
838 {
839 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
840 const char *l, *buf;
Adam Langley858a88d2014-06-20 12:00:00 -0700841 int j, multi, found, rule, retval, ok, buflen, in_group = 0,
842 has_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700843 unsigned long cipher_id = 0;
844 char ch;
845
846 retval = 1;
847 l = rule_str;
848 for (;;)
849 {
850 ch = *l;
851
852 if (ch == '\0')
853 break; /* done */
Adam Langley858a88d2014-06-20 12:00:00 -0700854 if (in_group)
855 {
856 if (ch == ']')
857 {
858 if (!in_group)
859 {
860 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
861 retval = found = in_group = 0;
862 break;
863 }
864 if (*tail_p)
865 (*tail_p)->in_group = 0;
866 in_group = 0;
867 l++;
868 continue;
869 }
870 if (ch == '|')
871 { rule = CIPHER_ADD; l++; continue; }
872 else if (!(ch >= 'a' && ch <= 'z') &&
873 !(ch >= 'A' && ch <= 'Z') &&
874 !(ch >= '0' && ch <= '9'))
875 {
876 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
877 retval = found = in_group = 0;
878 break;
879 }
880 else
881 rule = CIPHER_ADD;
882 }
883 else if (ch == '-')
Adam Langley95c29f32014-06-20 12:00:00 -0700884 { rule = CIPHER_DEL; l++; }
885 else if (ch == '+')
886 { rule = CIPHER_ORD; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700887 else if (ch == '!' && has_group)
888 {
889 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
890 retval = found = in_group = 0;
891 break;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893 else if (ch == '!')
894 { rule = CIPHER_KILL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700895 else if (ch == '@' && has_group)
896 {
897 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
898 retval = found = in_group = 0;
899 break;
900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901 else if (ch == '@')
902 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700903 else if (ch == '[')
904 {
905 if (in_group)
906 {
907 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
908 retval = found = in_group = 0;
909 break;
910 }
911 in_group = 1;
912 has_group = 1;
913 l++;
914 continue;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916 else
917 { rule = CIPHER_ADD; }
918
919 if (ITEM_SEP(ch))
920 {
921 l++;
922 continue;
923 }
924
925 alg_mkey = 0;
926 alg_auth = 0;
927 alg_enc = 0;
928 alg_mac = 0;
929 alg_ssl = 0;
930 algo_strength = 0;
931
932 for (;;)
933 {
934 ch = *l;
935 buf = l;
936 buflen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700937 while ( ((ch >= 'A') && (ch <= 'Z')) ||
938 ((ch >= '0') && (ch <= '9')) ||
939 ((ch >= 'a') && (ch <= 'z')) ||
940 (ch == '-') || (ch == '.'))
Adam Langley95c29f32014-06-20 12:00:00 -0700941 {
942 ch = *(++l);
943 buflen++;
944 }
945
946 if (buflen == 0)
947 {
948 /*
949 * We hit something we cannot deal with,
950 * it is no command or separator nor
951 * alphanumeric, so we call this an error.
952 */
953 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -0700954 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700955 l++;
956 break;
957 }
958
959 if (rule == CIPHER_SPECIAL)
960 {
961 found = 0; /* unused -- avoid compiler warning */
962 break; /* special treatment */
963 }
964
965 /* check for multi-part specification */
966 if (ch == '+')
967 {
968 multi=1;
969 l++;
970 }
971 else
972 multi=0;
973
974 /*
975 * Now search for the cipher alias in the ca_list. Be careful
976 * with the strncmp, because the "buflen" limitation
977 * will make the rule "ADH:SOME" and the cipher
978 * "ADH-MY-CIPHER" look like a match for buflen=3.
979 * So additionally check whether the cipher name found
980 * has the correct length. We can save a strlen() call:
981 * just checking for the '\0' at the right place is
982 * sufficient, we have to strncmp() anyway. (We cannot
983 * use strcmp(), because buf is not '\0' terminated.)
984 */
985 j = found = 0;
986 cipher_id = 0;
987 while (ca_list[j])
988 {
989 if (!strncmp(buf, ca_list[j]->name, buflen) &&
990 (ca_list[j]->name[buflen] == '\0'))
991 {
992 found = 1;
993 break;
994 }
995 else
996 j++;
997 }
998
999 if (!found)
1000 break; /* ignore this entry */
1001
1002 if (ca_list[j]->algorithm_mkey)
1003 {
1004 if (alg_mkey)
1005 {
1006 alg_mkey &= ca_list[j]->algorithm_mkey;
1007 if (!alg_mkey) { found = 0; break; }
1008 }
1009 else
1010 alg_mkey = ca_list[j]->algorithm_mkey;
1011 }
1012
1013 if (ca_list[j]->algorithm_auth)
1014 {
1015 if (alg_auth)
1016 {
1017 alg_auth &= ca_list[j]->algorithm_auth;
1018 if (!alg_auth) { found = 0; break; }
1019 }
1020 else
1021 alg_auth = ca_list[j]->algorithm_auth;
1022 }
1023
1024 if (ca_list[j]->algorithm_enc)
1025 {
1026 if (alg_enc)
1027 {
1028 alg_enc &= ca_list[j]->algorithm_enc;
1029 if (!alg_enc) { found = 0; break; }
1030 }
1031 else
1032 alg_enc = ca_list[j]->algorithm_enc;
1033 }
1034
1035 if (ca_list[j]->algorithm_mac)
1036 {
1037 if (alg_mac)
1038 {
1039 alg_mac &= ca_list[j]->algorithm_mac;
1040 if (!alg_mac) { found = 0; break; }
1041 }
1042 else
1043 alg_mac = ca_list[j]->algorithm_mac;
1044 }
1045
1046 if (ca_list[j]->algo_strength & SSL_EXP_MASK)
1047 {
1048 if (algo_strength & SSL_EXP_MASK)
1049 {
1050 algo_strength &= (ca_list[j]->algo_strength & SSL_EXP_MASK) | ~SSL_EXP_MASK;
1051 if (!(algo_strength & SSL_EXP_MASK)) { found = 0; break; }
1052 }
1053 else
1054 algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
1055 }
1056
1057 if (ca_list[j]->algo_strength & SSL_STRONG_MASK)
1058 {
1059 if (algo_strength & SSL_STRONG_MASK)
1060 {
1061 algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1062 if (!(algo_strength & SSL_STRONG_MASK)) { found = 0; break; }
1063 }
1064 else
1065 algo_strength |= ca_list[j]->algo_strength & SSL_STRONG_MASK;
1066 }
1067
1068 if (ca_list[j]->valid)
1069 {
1070 /* explicit ciphersuite found; its protocol version
1071 * does not become part of the search pattern!*/
1072
1073 cipher_id = ca_list[j]->id;
1074 }
1075 else
1076 {
1077 /* not an explicit ciphersuite; only in this case, the
1078 * protocol version is considered part of the search pattern */
1079
1080 if (ca_list[j]->algorithm_ssl)
1081 {
1082 if (alg_ssl)
1083 {
1084 alg_ssl &= ca_list[j]->algorithm_ssl;
1085 if (!alg_ssl) { found = 0; break; }
1086 }
1087 else
1088 alg_ssl = ca_list[j]->algorithm_ssl;
1089 }
1090 }
1091
1092 if (!multi) break;
1093 }
1094
1095 /*
1096 * Ok, we have the rule, now apply it
1097 */
1098 if (rule == CIPHER_SPECIAL)
1099 { /* special command */
1100 ok = 0;
1101 if ((buflen == 8) &&
1102 !strncmp(buf, "STRENGTH", 8))
1103 ok = ssl_cipher_strength_sort(head_p, tail_p);
1104 else
1105 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1106 if (ok == 0)
1107 retval = 0;
1108 /*
1109 * We do not support any "multi" options
1110 * together with "@", so throw away the
1111 * rest of the command, if any left, until
1112 * end or ':' is found.
1113 */
1114 while ((*l != '\0') && !ITEM_SEP(*l))
1115 l++;
1116 }
1117 else if (found)
1118 {
1119 ssl_cipher_apply_rule(cipher_id,
1120 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -07001121 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -07001122 }
1123 else
1124 {
1125 while ((*l != '\0') && !ITEM_SEP(*l))
1126 l++;
1127 }
1128 if (*l == '\0') break; /* done */
1129 }
1130
Adam Langley858a88d2014-06-20 12:00:00 -07001131 if (in_group)
1132 {
1133 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1134 retval = 0;
1135 }
1136
Adam Langley95c29f32014-06-20 12:00:00 -07001137 return(retval);
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139
1140
1141STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -07001142 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -07001143 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1144 const char *rule_str, CERT *c)
1145 {
1146 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1147 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
Adam Langley858a88d2014-06-20 12:00:00 -07001148 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001149 const char *rule_p;
1150 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1151 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001152 unsigned char *in_group_flags = NULL;
1153 unsigned int num_in_group_flags = 0;
1154 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001155
1156 /*
1157 * Return with error if nothing to do.
1158 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001159 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001160 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001161
1162 /*
1163 * To reduce the work to do we only want to process the compiled
1164 * in algorithms, so we first get the mask of disabled ciphers.
1165 */
1166 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1167
1168 /*
1169 * Now we have to collect the available ciphers from the compiled
1170 * in ciphers. We cannot get more than the number compiled in, so
1171 * it is used for allocation.
1172 */
1173 num_of_ciphers = ssl_method->num_ciphers();
1174#ifdef KSSL_DEBUG
1175 printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
1176#endif /* KSSL_DEBUG */
1177 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1178 if (co_list == NULL)
1179 {
1180 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1181 return(NULL); /* Failure */
1182 }
1183
1184 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1185 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1186 co_list, &head, &tail);
1187
1188
David Benjaminede973a2014-08-19 14:12:39 -04001189 /* Now arrange all ciphers by preference:
1190 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001191
1192 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001193 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1194 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 -07001195
David Benjamin5213df42014-08-20 14:19:54 -04001196 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1197 * CHACHA20 unless there is hardware support for fast and constant-time
1198 * AES_GCM. */
1199 if (EVP_has_aes_hardware())
1200 {
1201 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1202 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1203 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1204 }
1205 else
1206 {
1207 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1208 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1209 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1210 }
1211
1212 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
1213 * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjaminede973a2014-08-19 14:12:39 -04001214 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1215 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1216 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1217 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1218 ssl_cipher_apply_rule(0, 0, 0, SSL_3DES, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001219
1220 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001221 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001222
David Benjaminede973a2014-08-19 14:12:39 -04001223 /* Move ciphers without forward secrecy to the end. */
1224 ssl_cipher_apply_rule(0, ~(SSL_kEDH|SSL_kEECDH), 0, 0, 0, 0, 0, CIPHER_ORD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001225
1226 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1227 * (For applications that allow them, they aren't too bad, but we prefer
David Benjaminede973a2014-08-19 14:12:39 -04001228 * authenticated ciphers.)
1229 * TODO(davidben): Remove them altogether? */
Adam Langley858a88d2014-06-20 12:00:00 -07001230 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 -07001231
Adam Langley95c29f32014-06-20 12:00:00 -07001232 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001233 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001234
Adam Langley95c29f32014-06-20 12:00:00 -07001235 /*
1236 * We also need cipher aliases for selecting based on the rule_str.
1237 * There might be two types of entries in the rule_str: 1) names
1238 * of ciphers themselves 2) aliases for groups of ciphers.
1239 * For 1) we need the available ciphers and for 2) the cipher
1240 * groups of cipher_aliases added together in one list (otherwise
1241 * we would be happy with just the cipher_aliases table).
1242 */
1243 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1244 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1245 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1246 if (ca_list == NULL)
1247 {
1248 OPENSSL_free(co_list);
1249 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1250 return(NULL); /* Failure */
1251 }
1252 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1253 disabled_mkey, disabled_auth, disabled_enc,
1254 disabled_mac, disabled_ssl, head);
1255
1256 /*
1257 * If the rule_string begins with DEFAULT, apply the default rule
1258 * before using the (possibly available) additional rules.
1259 */
1260 ok = 1;
1261 rule_p = rule_str;
1262 if (strncmp(rule_str,"DEFAULT",7) == 0)
1263 {
1264 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1265 &head, &tail, ca_list);
1266 rule_p += 7;
1267 if (*rule_p == ':')
1268 rule_p++;
1269 }
1270
1271 if (ok && (strlen(rule_p) > 0))
1272 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1273
1274 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1275
1276 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001277 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001278
1279 /*
1280 * Allocate new "cipherstack" for the result, return with error
1281 * if we cannot get one.
1282 */
1283 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001284 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001285
Adam Langley858a88d2014-06-20 12:00:00 -07001286 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1287 if (!in_group_flags)
1288 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001289 /*
1290 * The cipher selection for the list is done. The ciphers are added
1291 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1292 */
1293 for (curr = head; curr != NULL; curr = curr->next)
1294 {
Adam Langley95c29f32014-06-20 12:00:00 -07001295 if (curr->active)
Adam Langley95c29f32014-06-20 12:00:00 -07001296 {
1297 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001298 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001299#ifdef CIPHER_DEBUG
1300 printf("<%s>\n",curr->cipher->name);
1301#endif
1302 }
1303 }
1304 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001305 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001306
1307 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1308 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001309 goto err;
1310 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1311 if (!pref_list)
1312 goto err;
1313 pref_list->ciphers = cipherstack;
1314 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1315 if (!pref_list->in_group_flags)
1316 goto err;
1317 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1318 OPENSSL_free(in_group_flags);
1319 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001320 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001321 ssl_cipher_preference_list_free(*cipher_list);
1322 *cipher_list = pref_list;
1323 pref_list = NULL;
1324
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001325 if (cipher_list_by_id != NULL)
1326 {
1327 if (*cipher_list_by_id != NULL)
1328 sk_SSL_CIPHER_free(*cipher_list_by_id);
1329 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001330 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001331 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001332
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001333 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1334 }
1335 else
Adam Langley858a88d2014-06-20 12:00:00 -07001336 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001337 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001338 tmp_cipher_list = NULL;
1339 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001340
Adam Langley95c29f32014-06-20 12:00:00 -07001341 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001342
1343err:
1344 if (co_list)
1345 OPENSSL_free(co_list);
1346 if (in_group_flags)
1347 OPENSSL_free(in_group_flags);
1348 if (cipherstack)
1349 sk_SSL_CIPHER_free(cipherstack);
1350 if (tmp_cipher_list)
1351 sk_SSL_CIPHER_free(tmp_cipher_list);
1352 if (pref_list && pref_list->in_group_flags)
1353 OPENSSL_free(pref_list->in_group_flags);
1354 if (pref_list)
1355 OPENSSL_free(pref_list);
1356 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001357 }
1358
Adam Langley73510762014-06-20 12:00:00 -07001359const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
Adam Langley95c29f32014-06-20 12:00:00 -07001360 {
David Benjamin060d9d22014-07-15 00:54:26 -04001361 const char *ver;
Adam Langley95c29f32014-06-20 12:00:00 -07001362 const char *kx,*au,*enc,*mac;
David Benjaminebf42b52014-08-19 15:18:42 -04001363 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001364#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001365 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s AL=%lx/%lx/%lx/%lx/%lx\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001366#else
David Benjamin060d9d22014-07-15 00:54:26 -04001367 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001368#endif /* KSSL_DEBUG */
1369
1370 alg_mkey = cipher->algorithm_mkey;
1371 alg_auth = cipher->algorithm_auth;
1372 alg_enc = cipher->algorithm_enc;
1373 alg_mac = cipher->algorithm_mac;
1374 alg_ssl = cipher->algorithm_ssl;
1375
David Benjaminebf42b52014-08-19 15:18:42 -04001376 if (alg_ssl & SSL_SSLV3)
Adam Langley95c29f32014-06-20 12:00:00 -07001377 ver="SSLv3";
1378 else if (alg_ssl & SSL_TLSV1_2)
1379 ver="TLSv1.2";
1380 else
1381 ver="unknown";
1382
1383 switch (alg_mkey)
1384 {
1385 case SSL_kRSA:
David Benjamin060d9d22014-07-15 00:54:26 -04001386 kx="RSA";
Adam Langley95c29f32014-06-20 12:00:00 -07001387 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001388 case SSL_kEDH:
David Benjamin060d9d22014-07-15 00:54:26 -04001389 kx="DH";
Adam Langley95c29f32014-06-20 12:00:00 -07001390 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001391 case SSL_kEECDH:
1392 kx="ECDH";
1393 break;
1394 case SSL_kPSK:
1395 kx="PSK";
1396 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001397 default:
1398 kx="unknown";
1399 }
1400
1401 switch (alg_auth)
1402 {
1403 case SSL_aRSA:
1404 au="RSA";
1405 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001406 case SSL_aNULL:
1407 au="None";
1408 break;
1409 case SSL_aECDSA:
1410 au="ECDSA";
1411 break;
1412 case SSL_aPSK:
1413 au="PSK";
1414 break;
1415 default:
1416 au="unknown";
1417 break;
1418 }
1419
1420 switch (alg_enc)
1421 {
Adam Langley95c29f32014-06-20 12:00:00 -07001422 case SSL_3DES:
1423 enc="3DES(168)";
1424 break;
1425 case SSL_RC4:
David Benjaminebf42b52014-08-19 15:18:42 -04001426 enc="RC4(128)";
Adam Langley95c29f32014-06-20 12:00:00 -07001427 break;
1428 case SSL_AES128:
1429 enc="AES(128)";
1430 break;
1431 case SSL_AES256:
1432 enc="AES(256)";
1433 break;
1434 case SSL_AES128GCM:
1435 enc="AESGCM(128)";
1436 break;
1437 case SSL_AES256GCM:
1438 enc="AESGCM(256)";
1439 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001440 case SSL_CHACHA20POLY1305:
1441 enc="ChaCha20-Poly1305";
1442 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001443 default:
1444 enc="unknown";
1445 break;
1446 }
1447
1448 switch (alg_mac)
1449 {
1450 case SSL_MD5:
1451 mac="MD5";
1452 break;
1453 case SSL_SHA1:
1454 mac="SHA1";
1455 break;
1456 case SSL_SHA256:
1457 mac="SHA256";
1458 break;
1459 case SSL_SHA384:
1460 mac="SHA384";
1461 break;
1462 case SSL_AEAD:
1463 mac="AEAD";
1464 break;
1465 default:
1466 mac="unknown";
1467 break;
1468 }
1469
1470 if (buf == NULL)
1471 {
1472 len=128;
1473 buf=OPENSSL_malloc(len);
1474 if (buf == NULL) return("OPENSSL_malloc Error");
1475 }
1476 else if (len < 128)
1477 return("Buffer too small");
1478
1479#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001480 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac,alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07001481#else
David Benjamin060d9d22014-07-15 00:54:26 -04001482 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
Adam Langley95c29f32014-06-20 12:00:00 -07001483#endif /* KSSL_DEBUG */
1484 return(buf);
1485 }
1486
Adam Langley4d4bff82014-06-20 12:00:00 -07001487/* Next three functions require non-null cipher */
1488int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1489 {
1490 return (c->algorithm_enc & SSL_AES) != 0;
1491 }
1492
1493int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1494 {
1495 return (c->algorithm_mac & SSL_MD5) != 0;
1496 }
1497
1498int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1499 {
1500 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1501 }
1502
Adam Langleyde0b2022014-06-20 12:00:00 -07001503int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1504 {
1505 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1506 }
1507
Adam Langley73510762014-06-20 12:00:00 -07001508const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
Adam Langley95c29f32014-06-20 12:00:00 -07001509 {
1510 int i;
1511
1512 if (c == NULL) return("(NONE)");
1513 i=(int)(c->id>>24L);
1514 if (i == 3)
1515 return("TLSv1/SSLv3");
1516 else if (i == 2)
1517 return("SSLv2");
1518 else
1519 return("unknown");
1520 }
1521
1522/* return the actual cipher being used */
1523const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1524 {
1525 if (c != NULL)
1526 return(c->name);
1527 return("(NONE)");
1528 }
1529
Adam Langleyf1df2b32014-08-11 17:37:53 -07001530const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1531 if (cipher == NULL) {
1532 return "";
1533 }
1534
1535 switch (cipher->algorithm_mkey) {
1536 case SSL_kRSA:
1537 return SSL_TXT_RSA;
Adam Langleyf1df2b32014-08-11 17:37:53 -07001538 case SSL_kEDH:
1539 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001540 case SSL_aRSA:
1541 return "DHE_" SSL_TXT_RSA;
1542 case SSL_aNULL:
1543 return SSL_TXT_DH "_anon";
1544 default:
1545 return "UNKNOWN";
1546 }
Adam Langleyf1df2b32014-08-11 17:37:53 -07001547 case SSL_kEECDH:
1548 switch (cipher->algorithm_auth) {
1549 case SSL_aECDSA:
1550 return "ECDHE_" SSL_TXT_ECDSA;
1551 case SSL_aRSA:
1552 return "ECDHE_" SSL_TXT_RSA;
1553 case SSL_aNULL:
1554 return SSL_TXT_ECDH "_anon";
1555 default:
1556 return "UNKNOWN";
1557 }
1558 default:
1559 return "UNKNOWN";
1560 }
1561}
1562
Adam Langley95c29f32014-06-20 12:00:00 -07001563/* number of bits for symmetric cipher */
1564int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1565 {
1566 int ret=0;
1567
1568 if (c != NULL)
1569 {
1570 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1571 ret = c->strength_bits;
1572 }
1573 return(ret);
1574 }
1575
1576unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1577 {
1578 return c->id;
1579 }
1580
Adam Langley95c29f32014-06-20 12:00:00 -07001581void *SSL_COMP_get_compression_methods(void)
1582 {
1583 return NULL;
1584 }
1585int SSL_COMP_add_compression_method(int id, void *cm)
1586 {
1587 return 1;
1588 }
1589
1590const char *SSL_COMP_get_name(const void *comp)
1591 {
1592 return NULL;
1593 }
1594
1595/* For a cipher return the index corresponding to the certificate type */
1596int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1597 {
David Benjamin0da0e182014-08-19 16:20:28 -04001598 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001599
David Benjamin0da0e182014-08-19 16:20:28 -04001600 if (alg_a & SSL_aECDSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001601 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07001602 else if (alg_a & SSL_aRSA)
1603 return SSL_PKEY_RSA_ENC;
Adam Langley95c29f32014-06-20 12:00:00 -07001604 return -1;
1605 }
1606
David Benjamine8f3d662014-07-12 01:10:19 -04001607/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
1608 * server public key in the key exchange, sent in a server Certificate
1609 * message. Otherwise it returns 0. */
1610int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
1611 {
1612 /* Anonymous ciphers do not include a server certificate. */
1613 if (cipher->algorithm_auth & SSL_aNULL)
1614 return 0;
1615 /* Neither do PSK ciphers, except for RSA_PSK. */
1616 if ((cipher->algorithm_auth & SSL_aPSK) &&
1617 !(cipher->algorithm_mkey & SSL_kRSA))
1618 return 0;
1619 /* All other ciphers include it. */
1620 return 1;
1621 }
David Benjamin9c651c92014-07-12 13:27:45 -04001622
1623/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
1624 * requires a ServerKeyExchange message. Otherwise it returns 0.
1625 *
1626 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
1627 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
1628 * to communicate a psk_identity_hint, so it is optional.
1629 *
1630 * Also, as implemented, the RSA key exchange takes an optional
1631 * ServerKeyExchange containing a signed ephemeral RSA encryption key.
1632 *
1633 * TODO(davidben): Can we remove the RSA one? This is a remnant of
1634 * RSA_EXPORT ciphers which required this (it was used to generate an
1635 * ephemeral 512-bit RSA encryption key), but it's allowed for all RSA
David Benjaminb9cc33a2014-07-15 00:09:48 -04001636 * ciphers. */
David Benjamin9c651c92014-07-12 13:27:45 -04001637int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
1638 {
1639 /* Ephemeral Diffie-Hellman key exchanges require a
1640 * ServerKeyExchange. */
1641 if (cipher->algorithm_mkey & SSL_kEDH ||
1642 cipher->algorithm_mkey & SSL_kEECDH)
1643 return 1;
1644 /* It is optional in all others. */
1645 return 0;
1646 }