blob: 4df96aabe1ce2950ede4dfa0d84b6a2ba0d926f6 [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},
216 {0,SSL_TXT_aDSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
217 {0,SSL_TXT_DSS,0, 0,SSL_aDSS, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700218 {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700219 {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
220 {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
221 {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700222
223 /* aliases combining key exchange and server authentication */
224 {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
225 {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700226 {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
227 {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
228 {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
229 {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700230
231
232 /* symmetric encryption aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700233 {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
234 {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700235 {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
236 {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
237 {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
238 {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 -0700239 {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700240
241 /* MAC aliases */
242 {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
243 {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
244 {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700245 {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
246 {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
247
248 /* protocol version aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700249 {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
250 {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
251 {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
252
Adam Langley95c29f32014-06-20 12:00:00 -0700253 /* strength classes */
Adam Langley95c29f32014-06-20 12:00:00 -0700254 {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
255 {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
256 /* FIPS 140-2 approved ciphersuite */
David Benjaminebf42b52014-08-19 15:18:42 -0400257 {0,SSL_TXT_FIPS,0, 0,0,0,0,0,SSL_FIPS, 0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700258 };
259
260void ssl_load_ciphers(void)
261 {
Adam Langley95c29f32014-06-20 12:00:00 -0700262 ssl_cipher_methods[SSL_ENC_3DES_IDX]= EVP_des_ede3_cbc();
263 ssl_cipher_methods[SSL_ENC_RC4_IDX]= EVP_rc4();
264 ssl_cipher_methods[SSL_ENC_AES128_IDX]= EVP_aes_128_cbc();
265 ssl_cipher_methods[SSL_ENC_AES256_IDX]= EVP_aes_256_cbc();
266
Adam Langley95c29f32014-06-20 12:00:00 -0700267 ssl_digest_methods[SSL_MD_MD5_IDX]= EVP_md5();
268 ssl_mac_secret_size[SSL_MD_MD5_IDX]= EVP_MD_size(EVP_md5());
269 assert(ssl_mac_secret_size[SSL_MD_MD5_IDX] >= 0);
270 ssl_digest_methods[SSL_MD_SHA1_IDX]=EVP_sha1();
271 ssl_mac_secret_size[SSL_MD_SHA1_IDX]= EVP_MD_size(EVP_sha1());
272 assert(ssl_mac_secret_size[SSL_MD_SHA1_IDX] >= 0);
273
274 ssl_digest_methods[SSL_MD_SHA256_IDX]= EVP_sha256();
275 ssl_mac_secret_size[SSL_MD_SHA256_IDX]= EVP_MD_size(EVP_sha256());
276 ssl_digest_methods[SSL_MD_SHA384_IDX]= EVP_sha384();
277 ssl_mac_secret_size[SSL_MD_SHA384_IDX]= EVP_MD_size(EVP_sha384());
278 }
279
Adam Langleyc9fb3752014-06-20 12:00:00 -0700280/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
281 * for |s->cipher|. It returns 1 on success and 0 on error. */
282int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
283 {
284 const SSL_CIPHER *c = s->cipher;
285
286 *aead = NULL;
287
288 if (c == NULL)
289 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700290 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0 &&
291 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) == 0)
Adam Langleyc9fb3752014-06-20 12:00:00 -0700292 return 0;
293
294#ifndef OPENSSL_NO_AES
Adam Langleyde0b2022014-06-20 12:00:00 -0700295 switch (c->algorithm_enc)
296 {
297 case SSL_AES128GCM:
298 *aead = EVP_aead_aes_128_gcm();
299 return 1;
Adam Langley5c270c52014-06-20 12:00:00 -0700300 case SSL_AES256GCM:
301 *aead = EVP_aead_aes_256_gcm();
302 return 1;
Adam Langleyde0b2022014-06-20 12:00:00 -0700303 case SSL_CHACHA20POLY1305:
304 *aead = EVP_aead_chacha20_poly1305();
305 return 1;
Adam Langley9447dff2014-06-24 17:29:06 -0700306 case SSL_RC4:
307 if (c->algorithm_mac == SSL_MD5)
308 *aead = EVP_aead_rc4_md5_tls();
309 else
310 return 0;
311 return 1;
312 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700313#endif
314
315 return 0;
316 }
317
318int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
319 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
320 {
Adam Langley95c29f32014-06-20 12:00:00 -0700321 int i;
322 const SSL_CIPHER *c;
323
324 c=s->cipher;
325 if (c == NULL) return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyc9fb3752014-06-20 12:00:00 -0700327 /* This function doesn't deal with EVP_AEAD. See
328 * |ssl_cipher_get_aead_evp|. */
329 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
330 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700331
332 if ((enc == NULL) || (md == NULL)) return(0);
333
334 switch (c->algorithm_enc)
335 {
Adam Langley95c29f32014-06-20 12:00:00 -0700336 case SSL_3DES:
337 i=SSL_ENC_3DES_IDX;
338 break;
339 case SSL_RC4:
340 i=SSL_ENC_RC4_IDX;
341 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700342 case SSL_AES128:
343 i=SSL_ENC_AES128_IDX;
344 break;
345 case SSL_AES256:
346 i=SSL_ENC_AES256_IDX;
347 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700348 default:
349 i= -1;
350 break;
351 }
352
Adam Langley4c65f3a2014-06-20 12:00:00 -0700353 if ((i < 0) || (i >= SSL_ENC_NUM_IDX))
Adam Langley95c29f32014-06-20 12:00:00 -0700354 *enc=NULL;
355 else
Adam Langley95c29f32014-06-20 12:00:00 -0700356 *enc=ssl_cipher_methods[i];
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langley9447dff2014-06-24 17:29:06 -0700358 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size))
359 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700360
361 if ((*enc != NULL) &&
362 (*md != NULL || (EVP_CIPHER_flags(*enc)&EVP_CIPH_FLAG_AEAD_CIPHER)) &&
363 (!mac_pkey_type||*mac_pkey_type != NID_undef))
364 {
365 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
366 s->ssl_version < TLS1_VERSION)
367 return 1;
368
Adam Langley95c29f32014-06-20 12:00:00 -0700369 /* TODO(fork): enable the stitched cipher modes. */
370#if 0
371 if (c->algorithm_enc == SSL_RC4 &&
372 c->algorithm_mac == SSL_MD5 &&
373 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
374 *enc = evp, *md = NULL;
375 else if (c->algorithm_enc == SSL_AES128 &&
376 c->algorithm_mac == SSL_SHA1 &&
377 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
378 *enc = evp, *md = NULL;
379 else if (c->algorithm_enc == SSL_AES256 &&
380 c->algorithm_mac == SSL_SHA1 &&
381 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
382 *enc = evp, *md = NULL;
383#endif
384 return(1);
385 }
386 else
387 return(0);
388 }
389
Adam Langley9447dff2014-06-24 17:29:06 -0700390int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
391 {
392 int i;
393 const SSL_CIPHER *c;
394
395 c=s->cipher;
396 if (c == NULL) return(0);
397
398 switch (c->algorithm_mac)
399 {
400 case SSL_MD5:
401 i=SSL_MD_MD5_IDX;
402 break;
403 case SSL_SHA1:
404 i=SSL_MD_SHA1_IDX;
405 break;
406 case SSL_SHA256:
407 i=SSL_MD_SHA256_IDX;
408 break;
409 case SSL_SHA384:
410 i=SSL_MD_SHA384_IDX;
411 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700412 default:
413 i= -1;
414 break;
415 }
416
417 if ((i < 0) || (i >= SSL_MD_NUM_IDX))
418 {
419 *md=NULL;
420 if (mac_pkey_type!=NULL) *mac_pkey_type = NID_undef;
421 if (mac_secret_size!=NULL) *mac_secret_size = 0;
422 }
423 else
424 {
425 *md=ssl_digest_methods[i];
426 if (mac_pkey_type!=NULL) *mac_pkey_type = ssl_mac_pkey_id[i];
427 if (mac_secret_size!=NULL) *mac_secret_size = ssl_mac_secret_size[i];
428 }
429
430 return 1;
431 }
432
Adam Langley95c29f32014-06-20 12:00:00 -0700433int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
434{
435 if (idx <0||idx>=SSL_MD_NUM_IDX)
436 {
437 return 0;
438 }
439 *mask = ssl_handshake_digest_flag[idx];
440 if (*mask)
441 *md = ssl_digest_methods[idx];
442 else
443 *md = NULL;
444 return 1;
445}
446
447#define ITEM_SEP(a) \
448 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
449
450static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
451 CIPHER_ORDER **tail)
452 {
453 if (curr == *tail) return;
454 if (curr == *head)
455 *head=curr->next;
456 if (curr->prev != NULL)
457 curr->prev->next=curr->next;
458 if (curr->next != NULL)
459 curr->next->prev=curr->prev;
460 (*tail)->next=curr;
461 curr->prev= *tail;
462 curr->next=NULL;
463 *tail=curr;
464 }
465
466static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
467 CIPHER_ORDER **tail)
468 {
469 if (curr == *head) return;
470 if (curr == *tail)
471 *tail=curr->prev;
472 if (curr->next != NULL)
473 curr->next->prev=curr->prev;
474 if (curr->prev != NULL)
475 curr->prev->next=curr->next;
476 (*head)->prev=curr;
477 curr->next= *head;
478 curr->prev=NULL;
479 *head=curr;
480 }
481
482static void ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth, unsigned long *enc, unsigned long *mac, unsigned long *ssl)
483 {
484 *mkey = 0;
485 *auth = 0;
486 *enc = 0;
487 *mac = 0;
488 *ssl = 0;
489
Adam Langley95c29f32014-06-20 12:00:00 -0700490#ifdef OPENSSL_NO_DSA
491 *auth |= SSL_aDSS;
492#endif
493#ifdef OPENSSL_NO_DH
494 *mkey |= SSL_kDHr|SSL_kDHd|SSL_kEDH;
495 *auth |= SSL_aDH;
496#endif
497#ifdef OPENSSL_NO_ECDSA
498 *auth |= SSL_aECDSA;
499#endif
500#ifdef OPENSSL_NO_ECDH
501 *mkey |= SSL_kECDHe|SSL_kECDHr;
502 *auth |= SSL_aECDH;
503#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700504
505
506
Adam Langley95c29f32014-06-20 12:00:00 -0700507 *enc |= (ssl_cipher_methods[SSL_ENC_3DES_IDX] == NULL) ? SSL_3DES:0;
508 *enc |= (ssl_cipher_methods[SSL_ENC_RC4_IDX ] == NULL) ? SSL_RC4 :0;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 *enc |= (ssl_cipher_methods[SSL_ENC_AES128_IDX] == NULL) ? SSL_AES128:0;
510 *enc |= (ssl_cipher_methods[SSL_ENC_AES256_IDX] == NULL) ? SSL_AES256:0;
Adam Langley95c29f32014-06-20 12:00:00 -0700511
512 *mac |= (ssl_digest_methods[SSL_MD_MD5_IDX ] == NULL) ? SSL_MD5 :0;
513 *mac |= (ssl_digest_methods[SSL_MD_SHA1_IDX] == NULL) ? SSL_SHA1:0;
514 *mac |= (ssl_digest_methods[SSL_MD_SHA256_IDX] == NULL) ? SSL_SHA256:0;
515 *mac |= (ssl_digest_methods[SSL_MD_SHA384_IDX] == NULL) ? SSL_SHA384:0;
Adam Langley95c29f32014-06-20 12:00:00 -0700516
517 }
518
519static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
520 int num_of_ciphers,
521 unsigned long disabled_mkey, unsigned long disabled_auth,
522 unsigned long disabled_enc, unsigned long disabled_mac,
523 unsigned long disabled_ssl,
524 CIPHER_ORDER *co_list,
525 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
526 {
527 int i, co_list_num;
528 const SSL_CIPHER *c;
529
530 /*
531 * We have num_of_ciphers descriptions compiled in, depending on the
532 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
533 * These will later be sorted in a linked list with at most num
534 * entries.
535 */
536
537 /* Get the initial list of ciphers */
538 co_list_num = 0; /* actual count of ciphers */
539 for (i = 0; i < num_of_ciphers; i++)
540 {
541 c = ssl_method->get_cipher(i);
542 /* drop those that use any of that is not available */
543 if ((c != NULL) && c->valid &&
Adam Langley95c29f32014-06-20 12:00:00 -0700544 !(c->algorithm_mkey & disabled_mkey) &&
545 !(c->algorithm_auth & disabled_auth) &&
546 !(c->algorithm_enc & disabled_enc) &&
547 !(c->algorithm_mac & disabled_mac) &&
548 !(c->algorithm_ssl & disabled_ssl))
549 {
550 co_list[co_list_num].cipher = c;
551 co_list[co_list_num].next = NULL;
552 co_list[co_list_num].prev = NULL;
553 co_list[co_list_num].active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700554 co_list[co_list_num].in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700555 co_list_num++;
556#ifdef KSSL_DEBUG
557 printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
558#endif /* KSSL_DEBUG */
559 /*
560 if (!sk_push(ca_list,(char *)c)) goto err;
561 */
562 }
563 }
564
565 /*
566 * Prepare linked list from list entries
567 */
568 if (co_list_num > 0)
569 {
570 co_list[0].prev = NULL;
571
572 if (co_list_num > 1)
573 {
574 co_list[0].next = &co_list[1];
575
576 for (i = 1; i < co_list_num - 1; i++)
577 {
578 co_list[i].prev = &co_list[i - 1];
579 co_list[i].next = &co_list[i + 1];
580 }
581
582 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
583 }
584
585 co_list[co_list_num - 1].next = NULL;
586
587 *head_p = &co_list[0];
588 *tail_p = &co_list[co_list_num - 1];
589 }
590 }
591
592static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
593 int num_of_group_aliases,
594 unsigned long disabled_mkey, unsigned long disabled_auth,
595 unsigned long disabled_enc, unsigned long disabled_mac,
596 unsigned long disabled_ssl,
597 CIPHER_ORDER *head)
598 {
599 CIPHER_ORDER *ciph_curr;
600 const SSL_CIPHER **ca_curr;
601 int i;
602 unsigned long mask_mkey = ~disabled_mkey;
603 unsigned long mask_auth = ~disabled_auth;
604 unsigned long mask_enc = ~disabled_enc;
605 unsigned long mask_mac = ~disabled_mac;
606 unsigned long mask_ssl = ~disabled_ssl;
607
608 /*
609 * First, add the real ciphers as already collected
610 */
611 ciph_curr = head;
612 ca_curr = ca_list;
613 while (ciph_curr != NULL)
614 {
615 *ca_curr = ciph_curr->cipher;
616 ca_curr++;
617 ciph_curr = ciph_curr->next;
618 }
619
620 /*
621 * Now we add the available ones from the cipher_aliases[] table.
622 * They represent either one or more algorithms, some of which
623 * in any affected category must be supported (set in enabled_mask),
624 * or represent a cipher strength value (will be added in any case because algorithms=0).
625 */
626 for (i = 0; i < num_of_group_aliases; i++)
627 {
628 unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
629 unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
630 unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
631 unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
632 unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
633
634 if (algorithm_mkey)
635 if ((algorithm_mkey & mask_mkey) == 0)
636 continue;
637
638 if (algorithm_auth)
639 if ((algorithm_auth & mask_auth) == 0)
640 continue;
641
642 if (algorithm_enc)
643 if ((algorithm_enc & mask_enc) == 0)
644 continue;
645
646 if (algorithm_mac)
647 if ((algorithm_mac & mask_mac) == 0)
648 continue;
649
650 if (algorithm_ssl)
651 if ((algorithm_ssl & mask_ssl) == 0)
652 continue;
653
David Benjamin6f260012014-08-15 13:49:12 -0400654 *ca_curr = cipher_aliases + i;
Adam Langley95c29f32014-06-20 12:00:00 -0700655 ca_curr++;
656 }
657
658 *ca_curr = NULL; /* end of list */
659 }
660
661static void ssl_cipher_apply_rule(unsigned long cipher_id,
662 unsigned long alg_mkey, unsigned long alg_auth,
663 unsigned long alg_enc, unsigned long alg_mac,
664 unsigned long alg_ssl,
665 unsigned long algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700666 int rule, int strength_bits, int in_group,
Adam Langley95c29f32014-06-20 12:00:00 -0700667 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
668 {
Adam Langleye3142a72014-07-24 17:56:48 -0700669 CIPHER_ORDER *head, *tail, *curr, *next, *last;
Adam Langley95c29f32014-06-20 12:00:00 -0700670 const SSL_CIPHER *cp;
671 int reverse = 0;
672
673#ifdef CIPHER_DEBUG
Adam Langley858a88d2014-06-20 12:00:00 -0700674 printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
675 rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
Adam Langley95c29f32014-06-20 12:00:00 -0700676#endif
677
678 if (rule == CIPHER_DEL)
679 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
680
681 head = *head_p;
682 tail = *tail_p;
683
684 if (reverse)
685 {
Adam Langleye3142a72014-07-24 17:56:48 -0700686 next = tail;
Adam Langley95c29f32014-06-20 12:00:00 -0700687 last = head;
688 }
689 else
690 {
Adam Langleye3142a72014-07-24 17:56:48 -0700691 next = head;
Adam Langley95c29f32014-06-20 12:00:00 -0700692 last = tail;
693 }
694
Adam Langleye3142a72014-07-24 17:56:48 -0700695 curr = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700696 for (;;)
697 {
Adam Langleye3142a72014-07-24 17:56:48 -0700698 if (curr == last) break;
699
700 curr = next;
701
702 if (curr == NULL) break;
703
704 next = reverse ? curr->prev : curr->next;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
706 cp = curr->cipher;
707
708 /*
709 * Selection criteria is either the value of strength_bits
710 * or the algorithms used.
711 */
712 if (strength_bits >= 0)
713 {
714 if (strength_bits != cp->strength_bits)
715 continue;
716 }
717 else
718 {
719#ifdef CIPHER_DEBUG
720 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);
721#endif
722#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
723 if (cipher_id && cipher_id != cp->id)
724 continue;
725#endif
726 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
727 continue;
728 if (alg_auth && !(alg_auth & cp->algorithm_auth))
729 continue;
730 if (alg_enc && !(alg_enc & cp->algorithm_enc))
731 continue;
732 if (alg_mac && !(alg_mac & cp->algorithm_mac))
733 continue;
734 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
735 continue;
736 if ((algo_strength & SSL_EXP_MASK) && !(algo_strength & SSL_EXP_MASK & cp->algo_strength))
737 continue;
738 if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
739 continue;
740 }
741
742#ifdef CIPHER_DEBUG
743 printf("Action = %d\n", rule);
744#endif
745
746 /* add the cipher if it has not been added yet. */
747 if (rule == CIPHER_ADD)
748 {
749 /* reverse == 0 */
750 if (!curr->active)
751 {
752 ll_append_tail(&head, curr, &tail);
753 curr->active = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700754 curr->in_group = in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700755 }
756 }
757 /* Move the added cipher to this location */
758 else if (rule == CIPHER_ORD)
759 {
760 /* reverse == 0 */
761 if (curr->active)
762 {
763 ll_append_tail(&head, curr, &tail);
Adam Langley858a88d2014-06-20 12:00:00 -0700764 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700765 }
766 }
767 else if (rule == CIPHER_DEL)
768 {
769 /* reverse == 1 */
770 if (curr->active)
771 {
772 /* most recently deleted ciphersuites get best positions
773 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
774 * works in reverse to maintain the order) */
775 ll_append_head(&head, curr, &tail);
776 curr->active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700777 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700778 }
779 }
780 else if (rule == CIPHER_KILL)
781 {
782 /* reverse == 0 */
783 if (head == curr)
784 head = curr->next;
785 else
786 curr->prev->next = curr->next;
787 if (tail == curr)
788 tail = curr->prev;
789 curr->active = 0;
790 if (curr->next != NULL)
791 curr->next->prev = curr->prev;
792 if (curr->prev != NULL)
793 curr->prev->next = curr->next;
794 curr->next = NULL;
795 curr->prev = NULL;
796 }
797 }
798
799 *head_p = head;
800 *tail_p = tail;
801 }
802
803static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
804 CIPHER_ORDER **tail_p)
805 {
806 int max_strength_bits, i, *number_uses;
807 CIPHER_ORDER *curr;
808
809 /*
810 * This routine sorts the ciphers with descending strength. The sorting
811 * must keep the pre-sorted sequence, so we apply the normal sorting
812 * routine as '+' movement to the end of the list.
813 */
814 max_strength_bits = 0;
815 curr = *head_p;
816 while (curr != NULL)
817 {
818 if (curr->active &&
819 (curr->cipher->strength_bits > max_strength_bits))
820 max_strength_bits = curr->cipher->strength_bits;
821 curr = curr->next;
822 }
823
824 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
825 if (!number_uses)
826 {
827 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
828 return(0);
829 }
830 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
831
832 /*
833 * Now find the strength_bits values actually used
834 */
835 curr = *head_p;
836 while (curr != NULL)
837 {
838 if (curr->active)
839 number_uses[curr->cipher->strength_bits]++;
840 curr = curr->next;
841 }
842 /*
843 * Go through the list of used strength_bits values in descending
844 * order.
845 */
846 for (i = max_strength_bits; i >= 0; i--)
847 if (number_uses[i] > 0)
Adam Langley858a88d2014-06-20 12:00:00 -0700848 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 -0700849
850 OPENSSL_free(number_uses);
851 return(1);
852 }
853
854static int ssl_cipher_process_rulestr(const char *rule_str,
855 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
856 const SSL_CIPHER **ca_list)
857 {
858 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
859 const char *l, *buf;
Adam Langley858a88d2014-06-20 12:00:00 -0700860 int j, multi, found, rule, retval, ok, buflen, in_group = 0,
861 has_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700862 unsigned long cipher_id = 0;
863 char ch;
864
865 retval = 1;
866 l = rule_str;
867 for (;;)
868 {
869 ch = *l;
870
871 if (ch == '\0')
872 break; /* done */
Adam Langley858a88d2014-06-20 12:00:00 -0700873 if (in_group)
874 {
875 if (ch == ']')
876 {
877 if (!in_group)
878 {
879 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
880 retval = found = in_group = 0;
881 break;
882 }
883 if (*tail_p)
884 (*tail_p)->in_group = 0;
885 in_group = 0;
886 l++;
887 continue;
888 }
889 if (ch == '|')
890 { rule = CIPHER_ADD; l++; continue; }
891 else if (!(ch >= 'a' && ch <= 'z') &&
892 !(ch >= 'A' && ch <= 'Z') &&
893 !(ch >= '0' && ch <= '9'))
894 {
895 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
896 retval = found = in_group = 0;
897 break;
898 }
899 else
900 rule = CIPHER_ADD;
901 }
902 else if (ch == '-')
Adam Langley95c29f32014-06-20 12:00:00 -0700903 { rule = CIPHER_DEL; l++; }
904 else if (ch == '+')
905 { rule = CIPHER_ORD; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700906 else if (ch == '!' && has_group)
907 {
908 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
909 retval = found = in_group = 0;
910 break;
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912 else if (ch == '!')
913 { rule = CIPHER_KILL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700914 else if (ch == '@' && has_group)
915 {
916 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
917 retval = found = in_group = 0;
918 break;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920 else if (ch == '@')
921 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700922 else if (ch == '[')
923 {
924 if (in_group)
925 {
926 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
927 retval = found = in_group = 0;
928 break;
929 }
930 in_group = 1;
931 has_group = 1;
932 l++;
933 continue;
934 }
Adam Langley95c29f32014-06-20 12:00:00 -0700935 else
936 { rule = CIPHER_ADD; }
937
938 if (ITEM_SEP(ch))
939 {
940 l++;
941 continue;
942 }
943
944 alg_mkey = 0;
945 alg_auth = 0;
946 alg_enc = 0;
947 alg_mac = 0;
948 alg_ssl = 0;
949 algo_strength = 0;
950
951 for (;;)
952 {
953 ch = *l;
954 buf = l;
955 buflen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700956 while ( ((ch >= 'A') && (ch <= 'Z')) ||
957 ((ch >= '0') && (ch <= '9')) ||
958 ((ch >= 'a') && (ch <= 'z')) ||
959 (ch == '-') || (ch == '.'))
Adam Langley95c29f32014-06-20 12:00:00 -0700960 {
961 ch = *(++l);
962 buflen++;
963 }
964
965 if (buflen == 0)
966 {
967 /*
968 * We hit something we cannot deal with,
969 * it is no command or separator nor
970 * alphanumeric, so we call this an error.
971 */
972 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -0700973 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700974 l++;
975 break;
976 }
977
978 if (rule == CIPHER_SPECIAL)
979 {
980 found = 0; /* unused -- avoid compiler warning */
981 break; /* special treatment */
982 }
983
984 /* check for multi-part specification */
985 if (ch == '+')
986 {
987 multi=1;
988 l++;
989 }
990 else
991 multi=0;
992
993 /*
994 * Now search for the cipher alias in the ca_list. Be careful
995 * with the strncmp, because the "buflen" limitation
996 * will make the rule "ADH:SOME" and the cipher
997 * "ADH-MY-CIPHER" look like a match for buflen=3.
998 * So additionally check whether the cipher name found
999 * has the correct length. We can save a strlen() call:
1000 * just checking for the '\0' at the right place is
1001 * sufficient, we have to strncmp() anyway. (We cannot
1002 * use strcmp(), because buf is not '\0' terminated.)
1003 */
1004 j = found = 0;
1005 cipher_id = 0;
1006 while (ca_list[j])
1007 {
1008 if (!strncmp(buf, ca_list[j]->name, buflen) &&
1009 (ca_list[j]->name[buflen] == '\0'))
1010 {
1011 found = 1;
1012 break;
1013 }
1014 else
1015 j++;
1016 }
1017
1018 if (!found)
1019 break; /* ignore this entry */
1020
1021 if (ca_list[j]->algorithm_mkey)
1022 {
1023 if (alg_mkey)
1024 {
1025 alg_mkey &= ca_list[j]->algorithm_mkey;
1026 if (!alg_mkey) { found = 0; break; }
1027 }
1028 else
1029 alg_mkey = ca_list[j]->algorithm_mkey;
1030 }
1031
1032 if (ca_list[j]->algorithm_auth)
1033 {
1034 if (alg_auth)
1035 {
1036 alg_auth &= ca_list[j]->algorithm_auth;
1037 if (!alg_auth) { found = 0; break; }
1038 }
1039 else
1040 alg_auth = ca_list[j]->algorithm_auth;
1041 }
1042
1043 if (ca_list[j]->algorithm_enc)
1044 {
1045 if (alg_enc)
1046 {
1047 alg_enc &= ca_list[j]->algorithm_enc;
1048 if (!alg_enc) { found = 0; break; }
1049 }
1050 else
1051 alg_enc = ca_list[j]->algorithm_enc;
1052 }
1053
1054 if (ca_list[j]->algorithm_mac)
1055 {
1056 if (alg_mac)
1057 {
1058 alg_mac &= ca_list[j]->algorithm_mac;
1059 if (!alg_mac) { found = 0; break; }
1060 }
1061 else
1062 alg_mac = ca_list[j]->algorithm_mac;
1063 }
1064
1065 if (ca_list[j]->algo_strength & SSL_EXP_MASK)
1066 {
1067 if (algo_strength & SSL_EXP_MASK)
1068 {
1069 algo_strength &= (ca_list[j]->algo_strength & SSL_EXP_MASK) | ~SSL_EXP_MASK;
1070 if (!(algo_strength & SSL_EXP_MASK)) { found = 0; break; }
1071 }
1072 else
1073 algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
1074 }
1075
1076 if (ca_list[j]->algo_strength & SSL_STRONG_MASK)
1077 {
1078 if (algo_strength & SSL_STRONG_MASK)
1079 {
1080 algo_strength &= (ca_list[j]->algo_strength & SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1081 if (!(algo_strength & SSL_STRONG_MASK)) { found = 0; break; }
1082 }
1083 else
1084 algo_strength |= ca_list[j]->algo_strength & SSL_STRONG_MASK;
1085 }
1086
1087 if (ca_list[j]->valid)
1088 {
1089 /* explicit ciphersuite found; its protocol version
1090 * does not become part of the search pattern!*/
1091
1092 cipher_id = ca_list[j]->id;
1093 }
1094 else
1095 {
1096 /* not an explicit ciphersuite; only in this case, the
1097 * protocol version is considered part of the search pattern */
1098
1099 if (ca_list[j]->algorithm_ssl)
1100 {
1101 if (alg_ssl)
1102 {
1103 alg_ssl &= ca_list[j]->algorithm_ssl;
1104 if (!alg_ssl) { found = 0; break; }
1105 }
1106 else
1107 alg_ssl = ca_list[j]->algorithm_ssl;
1108 }
1109 }
1110
1111 if (!multi) break;
1112 }
1113
1114 /*
1115 * Ok, we have the rule, now apply it
1116 */
1117 if (rule == CIPHER_SPECIAL)
1118 { /* special command */
1119 ok = 0;
1120 if ((buflen == 8) &&
1121 !strncmp(buf, "STRENGTH", 8))
1122 ok = ssl_cipher_strength_sort(head_p, tail_p);
1123 else
1124 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1125 if (ok == 0)
1126 retval = 0;
1127 /*
1128 * We do not support any "multi" options
1129 * together with "@", so throw away the
1130 * rest of the command, if any left, until
1131 * end or ':' is found.
1132 */
1133 while ((*l != '\0') && !ITEM_SEP(*l))
1134 l++;
1135 }
1136 else if (found)
1137 {
1138 ssl_cipher_apply_rule(cipher_id,
1139 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -07001140 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -07001141 }
1142 else
1143 {
1144 while ((*l != '\0') && !ITEM_SEP(*l))
1145 l++;
1146 }
1147 if (*l == '\0') break; /* done */
1148 }
1149
Adam Langley858a88d2014-06-20 12:00:00 -07001150 if (in_group)
1151 {
1152 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
1153 retval = 0;
1154 }
1155
Adam Langley95c29f32014-06-20 12:00:00 -07001156 return(retval);
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
1159
1160STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -07001161 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -07001162 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1163 const char *rule_str, CERT *c)
1164 {
1165 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1166 unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
Adam Langley858a88d2014-06-20 12:00:00 -07001167 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001168 const char *rule_p;
1169 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1170 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001171 unsigned char *in_group_flags = NULL;
1172 unsigned int num_in_group_flags = 0;
1173 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001174
1175 /*
1176 * Return with error if nothing to do.
1177 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001178 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001179 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001180
1181 /*
1182 * To reduce the work to do we only want to process the compiled
1183 * in algorithms, so we first get the mask of disabled ciphers.
1184 */
1185 ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1186
1187 /*
1188 * Now we have to collect the available ciphers from the compiled
1189 * in ciphers. We cannot get more than the number compiled in, so
1190 * it is used for allocation.
1191 */
1192 num_of_ciphers = ssl_method->num_ciphers();
1193#ifdef KSSL_DEBUG
1194 printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
1195#endif /* KSSL_DEBUG */
1196 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1197 if (co_list == NULL)
1198 {
1199 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1200 return(NULL); /* Failure */
1201 }
1202
1203 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1204 disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1205 co_list, &head, &tail);
1206
1207
David Benjaminede973a2014-08-19 14:12:39 -04001208 /* Now arrange all ciphers by preference:
1209 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001210
1211 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001212 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1213 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 -07001214
David Benjaminede973a2014-08-19 14:12:39 -04001215 /* Order the bulk ciphers.
1216 * 1. CHACHA20_POLY1305.
1217 * 2. AES_256_GCM and AES_128_GCM.
1218 * 3. Legacy non-AEAD ciphers. AES_256_CBC, AES-128_CBC, RC4_128_SHA,
1219 * RC4_128_MD5, 3DES_EDE_CBC_SHA.
1220 * TODO(davidben): Prefer AES_GCM over CHACHA20 if there is hardware
1221 * support. */
1222 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1223 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1224 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1225 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1226 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1227 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1228 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1229 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 -07001230
1231 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001232 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001233
David Benjaminede973a2014-08-19 14:12:39 -04001234 /* Move ciphers without forward secrecy to the end. */
1235 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 -07001236
1237 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1238 * (For applications that allow them, they aren't too bad, but we prefer
David Benjaminede973a2014-08-19 14:12:39 -04001239 * authenticated ciphers.)
1240 * TODO(davidben): Remove them altogether? */
Adam Langley858a88d2014-06-20 12:00:00 -07001241 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 -07001242
Adam Langley95c29f32014-06-20 12:00:00 -07001243 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001244 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001245
Adam Langley95c29f32014-06-20 12:00:00 -07001246 /*
1247 * We also need cipher aliases for selecting based on the rule_str.
1248 * There might be two types of entries in the rule_str: 1) names
1249 * of ciphers themselves 2) aliases for groups of ciphers.
1250 * For 1) we need the available ciphers and for 2) the cipher
1251 * groups of cipher_aliases added together in one list (otherwise
1252 * we would be happy with just the cipher_aliases table).
1253 */
1254 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1255 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1256 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1257 if (ca_list == NULL)
1258 {
1259 OPENSSL_free(co_list);
1260 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1261 return(NULL); /* Failure */
1262 }
1263 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
1264 disabled_mkey, disabled_auth, disabled_enc,
1265 disabled_mac, disabled_ssl, head);
1266
1267 /*
1268 * If the rule_string begins with DEFAULT, apply the default rule
1269 * before using the (possibly available) additional rules.
1270 */
1271 ok = 1;
1272 rule_p = rule_str;
1273 if (strncmp(rule_str,"DEFAULT",7) == 0)
1274 {
1275 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1276 &head, &tail, ca_list);
1277 rule_p += 7;
1278 if (*rule_p == ':')
1279 rule_p++;
1280 }
1281
1282 if (ok && (strlen(rule_p) > 0))
1283 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1284
1285 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1286
1287 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001288 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001289
1290 /*
1291 * Allocate new "cipherstack" for the result, return with error
1292 * if we cannot get one.
1293 */
1294 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001295 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001296
Adam Langley858a88d2014-06-20 12:00:00 -07001297 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1298 if (!in_group_flags)
1299 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001300 /*
1301 * The cipher selection for the list is done. The ciphers are added
1302 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1303 */
1304 for (curr = head; curr != NULL; curr = curr->next)
1305 {
Adam Langley95c29f32014-06-20 12:00:00 -07001306 if (curr->active)
Adam Langley95c29f32014-06-20 12:00:00 -07001307 {
1308 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001309 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001310#ifdef CIPHER_DEBUG
1311 printf("<%s>\n",curr->cipher->name);
1312#endif
1313 }
1314 }
1315 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001316 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001317
1318 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1319 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001320 goto err;
1321 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1322 if (!pref_list)
1323 goto err;
1324 pref_list->ciphers = cipherstack;
1325 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1326 if (!pref_list->in_group_flags)
1327 goto err;
1328 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1329 OPENSSL_free(in_group_flags);
1330 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001331 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001332 ssl_cipher_preference_list_free(*cipher_list);
1333 *cipher_list = pref_list;
1334 pref_list = NULL;
1335
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001336 if (cipher_list_by_id != NULL)
1337 {
1338 if (*cipher_list_by_id != NULL)
1339 sk_SSL_CIPHER_free(*cipher_list_by_id);
1340 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001341 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001342 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001343
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001344 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1345 }
1346 else
Adam Langley858a88d2014-06-20 12:00:00 -07001347 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001348 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001349 tmp_cipher_list = NULL;
1350 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001351
Adam Langley95c29f32014-06-20 12:00:00 -07001352 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001353
1354err:
1355 if (co_list)
1356 OPENSSL_free(co_list);
1357 if (in_group_flags)
1358 OPENSSL_free(in_group_flags);
1359 if (cipherstack)
1360 sk_SSL_CIPHER_free(cipherstack);
1361 if (tmp_cipher_list)
1362 sk_SSL_CIPHER_free(tmp_cipher_list);
1363 if (pref_list && pref_list->in_group_flags)
1364 OPENSSL_free(pref_list->in_group_flags);
1365 if (pref_list)
1366 OPENSSL_free(pref_list);
1367 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001368 }
1369
Adam Langley73510762014-06-20 12:00:00 -07001370const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
Adam Langley95c29f32014-06-20 12:00:00 -07001371 {
David Benjamin060d9d22014-07-15 00:54:26 -04001372 const char *ver;
Adam Langley95c29f32014-06-20 12:00:00 -07001373 const char *kx,*au,*enc,*mac;
David Benjaminebf42b52014-08-19 15:18:42 -04001374 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001375#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001376 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 -07001377#else
David Benjamin060d9d22014-07-15 00:54:26 -04001378 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001379#endif /* KSSL_DEBUG */
1380
1381 alg_mkey = cipher->algorithm_mkey;
1382 alg_auth = cipher->algorithm_auth;
1383 alg_enc = cipher->algorithm_enc;
1384 alg_mac = cipher->algorithm_mac;
1385 alg_ssl = cipher->algorithm_ssl;
1386
David Benjaminebf42b52014-08-19 15:18:42 -04001387 if (alg_ssl & SSL_SSLV3)
Adam Langley95c29f32014-06-20 12:00:00 -07001388 ver="SSLv3";
1389 else if (alg_ssl & SSL_TLSV1_2)
1390 ver="TLSv1.2";
1391 else
1392 ver="unknown";
1393
1394 switch (alg_mkey)
1395 {
1396 case SSL_kRSA:
David Benjamin060d9d22014-07-15 00:54:26 -04001397 kx="RSA";
Adam Langley95c29f32014-06-20 12:00:00 -07001398 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001399 case SSL_kEDH:
David Benjamin060d9d22014-07-15 00:54:26 -04001400 kx="DH";
Adam Langley95c29f32014-06-20 12:00:00 -07001401 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001402 case SSL_kEECDH:
1403 kx="ECDH";
1404 break;
1405 case SSL_kPSK:
1406 kx="PSK";
1407 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001408 default:
1409 kx="unknown";
1410 }
1411
1412 switch (alg_auth)
1413 {
1414 case SSL_aRSA:
1415 au="RSA";
1416 break;
1417 case SSL_aDSS:
1418 au="DSS";
1419 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001420 case SSL_aNULL:
1421 au="None";
1422 break;
1423 case SSL_aECDSA:
1424 au="ECDSA";
1425 break;
1426 case SSL_aPSK:
1427 au="PSK";
1428 break;
1429 default:
1430 au="unknown";
1431 break;
1432 }
1433
1434 switch (alg_enc)
1435 {
Adam Langley95c29f32014-06-20 12:00:00 -07001436 case SSL_3DES:
1437 enc="3DES(168)";
1438 break;
1439 case SSL_RC4:
David Benjaminebf42b52014-08-19 15:18:42 -04001440 enc="RC4(128)";
Adam Langley95c29f32014-06-20 12:00:00 -07001441 break;
1442 case SSL_AES128:
1443 enc="AES(128)";
1444 break;
1445 case SSL_AES256:
1446 enc="AES(256)";
1447 break;
1448 case SSL_AES128GCM:
1449 enc="AESGCM(128)";
1450 break;
1451 case SSL_AES256GCM:
1452 enc="AESGCM(256)";
1453 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001454 case SSL_CHACHA20POLY1305:
1455 enc="ChaCha20-Poly1305";
1456 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001457 default:
1458 enc="unknown";
1459 break;
1460 }
1461
1462 switch (alg_mac)
1463 {
1464 case SSL_MD5:
1465 mac="MD5";
1466 break;
1467 case SSL_SHA1:
1468 mac="SHA1";
1469 break;
1470 case SSL_SHA256:
1471 mac="SHA256";
1472 break;
1473 case SSL_SHA384:
1474 mac="SHA384";
1475 break;
1476 case SSL_AEAD:
1477 mac="AEAD";
1478 break;
1479 default:
1480 mac="unknown";
1481 break;
1482 }
1483
1484 if (buf == NULL)
1485 {
1486 len=128;
1487 buf=OPENSSL_malloc(len);
1488 if (buf == NULL) return("OPENSSL_malloc Error");
1489 }
1490 else if (len < 128)
1491 return("Buffer too small");
1492
1493#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001494 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 -07001495#else
David Benjamin060d9d22014-07-15 00:54:26 -04001496 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
Adam Langley95c29f32014-06-20 12:00:00 -07001497#endif /* KSSL_DEBUG */
1498 return(buf);
1499 }
1500
Adam Langley4d4bff82014-06-20 12:00:00 -07001501/* Next three functions require non-null cipher */
1502int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1503 {
1504 return (c->algorithm_enc & SSL_AES) != 0;
1505 }
1506
1507int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1508 {
1509 return (c->algorithm_mac & SSL_MD5) != 0;
1510 }
1511
1512int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1513 {
1514 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1515 }
1516
Adam Langleyde0b2022014-06-20 12:00:00 -07001517int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1518 {
1519 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1520 }
1521
Adam Langley73510762014-06-20 12:00:00 -07001522const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
Adam Langley95c29f32014-06-20 12:00:00 -07001523 {
1524 int i;
1525
1526 if (c == NULL) return("(NONE)");
1527 i=(int)(c->id>>24L);
1528 if (i == 3)
1529 return("TLSv1/SSLv3");
1530 else if (i == 2)
1531 return("SSLv2");
1532 else
1533 return("unknown");
1534 }
1535
1536/* return the actual cipher being used */
1537const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1538 {
1539 if (c != NULL)
1540 return(c->name);
1541 return("(NONE)");
1542 }
1543
Adam Langleyf1df2b32014-08-11 17:37:53 -07001544const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1545 if (cipher == NULL) {
1546 return "";
1547 }
1548
1549 switch (cipher->algorithm_mkey) {
1550 case SSL_kRSA:
1551 return SSL_TXT_RSA;
Adam Langleyf1df2b32014-08-11 17:37:53 -07001552 case SSL_kEDH:
1553 switch (cipher->algorithm_auth) {
1554 case SSL_aDSS:
1555 return "DHE_" SSL_TXT_DSS;
1556 case SSL_aRSA:
1557 return "DHE_" SSL_TXT_RSA;
1558 case SSL_aNULL:
1559 return SSL_TXT_DH "_anon";
1560 default:
1561 return "UNKNOWN";
1562 }
Adam Langleyf1df2b32014-08-11 17:37:53 -07001563 case SSL_kEECDH:
1564 switch (cipher->algorithm_auth) {
1565 case SSL_aECDSA:
1566 return "ECDHE_" SSL_TXT_ECDSA;
1567 case SSL_aRSA:
1568 return "ECDHE_" SSL_TXT_RSA;
1569 case SSL_aNULL:
1570 return SSL_TXT_ECDH "_anon";
1571 default:
1572 return "UNKNOWN";
1573 }
1574 default:
1575 return "UNKNOWN";
1576 }
1577}
1578
Adam Langley95c29f32014-06-20 12:00:00 -07001579/* number of bits for symmetric cipher */
1580int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1581 {
1582 int ret=0;
1583
1584 if (c != NULL)
1585 {
1586 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1587 ret = c->strength_bits;
1588 }
1589 return(ret);
1590 }
1591
1592unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1593 {
1594 return c->id;
1595 }
1596
Adam Langley95c29f32014-06-20 12:00:00 -07001597void *SSL_COMP_get_compression_methods(void)
1598 {
1599 return NULL;
1600 }
1601int SSL_COMP_add_compression_method(int id, void *cm)
1602 {
1603 return 1;
1604 }
1605
1606const char *SSL_COMP_get_name(const void *comp)
1607 {
1608 return NULL;
1609 }
1610
1611/* For a cipher return the index corresponding to the certificate type */
1612int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1613 {
David Benjamin0da0e182014-08-19 16:20:28 -04001614 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001615
David Benjamin0da0e182014-08-19 16:20:28 -04001616 if (alg_a & SSL_aECDSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001617 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07001618 else if (alg_a & SSL_aDSS)
1619 return SSL_PKEY_DSA_SIGN;
1620 else if (alg_a & SSL_aRSA)
1621 return SSL_PKEY_RSA_ENC;
Adam Langley95c29f32014-06-20 12:00:00 -07001622 return -1;
1623 }
1624
David Benjamine8f3d662014-07-12 01:10:19 -04001625/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
1626 * server public key in the key exchange, sent in a server Certificate
1627 * message. Otherwise it returns 0. */
1628int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
1629 {
1630 /* Anonymous ciphers do not include a server certificate. */
1631 if (cipher->algorithm_auth & SSL_aNULL)
1632 return 0;
1633 /* Neither do PSK ciphers, except for RSA_PSK. */
1634 if ((cipher->algorithm_auth & SSL_aPSK) &&
1635 !(cipher->algorithm_mkey & SSL_kRSA))
1636 return 0;
1637 /* All other ciphers include it. */
1638 return 1;
1639 }
David Benjamin9c651c92014-07-12 13:27:45 -04001640
1641/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
1642 * requires a ServerKeyExchange message. Otherwise it returns 0.
1643 *
1644 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
1645 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
1646 * to communicate a psk_identity_hint, so it is optional.
1647 *
1648 * Also, as implemented, the RSA key exchange takes an optional
1649 * ServerKeyExchange containing a signed ephemeral RSA encryption key.
1650 *
1651 * TODO(davidben): Can we remove the RSA one? This is a remnant of
1652 * RSA_EXPORT ciphers which required this (it was used to generate an
1653 * ephemeral 512-bit RSA encryption key), but it's allowed for all RSA
David Benjaminb9cc33a2014-07-15 00:09:48 -04001654 * ciphers. */
David Benjamin9c651c92014-07-12 13:27:45 -04001655int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
1656 {
1657 /* Ephemeral Diffie-Hellman key exchanges require a
1658 * ServerKeyExchange. */
1659 if (cipher->algorithm_mkey & SSL_kEDH ||
1660 cipher->algorithm_mkey & SSL_kEECDH)
1661 return 1;
1662 /* It is optional in all others. */
1663 return 0;
1664 }