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