blob: eb28656610a995a0c85377396fe73d752749c71a [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 Benjamind633d632014-08-30 14:43:27 -0400150struct handshake_digest
151 {
152 long mask;
153 const EVP_MD *(*md_func)(void);
Adam Langley95c29f32014-06-20 12:00:00 -0700154 };
155
David Benjamind633d632014-08-30 14:43:27 -0400156static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = {
157 { SSL_HANDSHAKE_MAC_MD5, EVP_md5 },
158 { SSL_HANDSHAKE_MAC_SHA, EVP_sha1 },
159 { SSL_HANDSHAKE_MAC_SHA256, EVP_sha256 },
160 { SSL_HANDSHAKE_MAC_SHA384, EVP_sha384 },
161};
Adam Langley95c29f32014-06-20 12:00:00 -0700162
163#define CIPHER_ADD 1
164#define CIPHER_KILL 2
165#define CIPHER_DEL 3
166#define CIPHER_ORD 4
167#define CIPHER_SPECIAL 5
168
169typedef struct cipher_order_st
170 {
171 const SSL_CIPHER *cipher;
172 int active;
173 int dead;
Adam Langley858a88d2014-06-20 12:00:00 -0700174 int in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700175 struct cipher_order_st *next,*prev;
176 } CIPHER_ORDER;
177
178static const SSL_CIPHER cipher_aliases[]={
David Benjaminebf42b52014-08-19 15:18:42 -0400179 {0,SSL_TXT_ALL,0, 0,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700180
181 /* "COMPLEMENTOFDEFAULT" (does *not* include ciphersuites not found in ALL!) */
David Benjaminebf42b52014-08-19 15:18:42 -0400182 {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 -0700183
184 /* key exchange aliases
185 * (some of those using only a single bit here combine
186 * multiple key exchange algs according to the RFCs,
187 * e.g. kEDH combines DHE_DSS and DHE_RSA) */
188 {0,SSL_TXT_kRSA,0, SSL_kRSA, 0,0,0,0,0,0,0,0},
189
Adam Langley95c29f32014-06-20 12:00:00 -0700190 {0,SSL_TXT_kEDH,0, SSL_kEDH, 0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400191 {0,SSL_TXT_DH,0, SSL_kEDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley95c29f32014-06-20 12:00:00 -0700193 {0,SSL_TXT_kEECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
David Benjamin0da0e182014-08-19 16:20:28 -0400194 {0,SSL_TXT_ECDH,0, SSL_kEECDH,0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700195
196 {0,SSL_TXT_kPSK,0, SSL_kPSK, 0,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700197
198 /* server authentication aliases */
199 {0,SSL_TXT_aRSA,0, 0,SSL_aRSA, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700200 {0,SSL_TXT_aNULL,0, 0,SSL_aNULL, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700201 {0,SSL_TXT_aECDSA,0, 0,SSL_aECDSA,0,0,0,0,0,0,0},
202 {0,SSL_TXT_ECDSA,0, 0,SSL_aECDSA, 0,0,0,0,0,0,0},
203 {0,SSL_TXT_aPSK,0, 0,SSL_aPSK, 0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700204
205 /* aliases combining key exchange and server authentication */
206 {0,SSL_TXT_EDH,0, SSL_kEDH,~SSL_aNULL,0,0,0,0,0,0,0},
207 {0,SSL_TXT_EECDH,0, SSL_kEECDH,~SSL_aNULL,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700208 {0,SSL_TXT_RSA,0, SSL_kRSA,SSL_aRSA,0,0,0,0,0,0,0},
209 {0,SSL_TXT_ADH,0, SSL_kEDH,SSL_aNULL,0,0,0,0,0,0,0},
210 {0,SSL_TXT_AECDH,0, SSL_kEECDH,SSL_aNULL,0,0,0,0,0,0,0},
211 {0,SSL_TXT_PSK,0, SSL_kPSK,SSL_aPSK,0,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700212
213
214 /* symmetric encryption aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700215 {0,SSL_TXT_3DES,0, 0,0,SSL_3DES, 0,0,0,0,0,0},
216 {0,SSL_TXT_RC4,0, 0,0,SSL_RC4, 0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700217 {0,SSL_TXT_AES128,0, 0,0,SSL_AES128|SSL_AES128GCM,0,0,0,0,0,0},
218 {0,SSL_TXT_AES256,0, 0,0,SSL_AES256|SSL_AES256GCM,0,0,0,0,0,0},
219 {0,SSL_TXT_AES,0, 0,0,SSL_AES,0,0,0,0,0,0},
220 {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 -0700221 {0,SSL_TXT_CHACHA20 ,0,0,0,SSL_CHACHA20POLY1305,0,0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700222
223 /* MAC aliases */
224 {0,SSL_TXT_MD5,0, 0,0,0,SSL_MD5, 0,0,0,0,0},
225 {0,SSL_TXT_SHA1,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
226 {0,SSL_TXT_SHA,0, 0,0,0,SSL_SHA1, 0,0,0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700227 {0,SSL_TXT_SHA256,0, 0,0,0,SSL_SHA256, 0,0,0,0,0},
228 {0,SSL_TXT_SHA384,0, 0,0,0,SSL_SHA384, 0,0,0,0,0},
229
230 /* protocol version aliases */
Adam Langley95c29f32014-06-20 12:00:00 -0700231 {0,SSL_TXT_SSLV3,0, 0,0,0,0,SSL_SSLV3, 0,0,0,0},
232 {0,SSL_TXT_TLSV1,0, 0,0,0,0,SSL_TLSV1, 0,0,0,0},
233 {0,SSL_TXT_TLSV1_2,0, 0,0,0,0,SSL_TLSV1_2, 0,0,0,0},
234
Adam Langley95c29f32014-06-20 12:00:00 -0700235 /* strength classes */
Adam Langley95c29f32014-06-20 12:00:00 -0700236 {0,SSL_TXT_MEDIUM,0, 0,0,0,0,0,SSL_MEDIUM,0,0,0},
237 {0,SSL_TXT_HIGH,0, 0,0,0,0,0,SSL_HIGH, 0,0,0},
238 /* FIPS 140-2 approved ciphersuite */
David Benjaminebf42b52014-08-19 15:18:42 -0400239 {0,SSL_TXT_FIPS,0, 0,0,0,0,0,SSL_FIPS, 0,0,0},
Adam Langley95c29f32014-06-20 12:00:00 -0700240 };
241
Adam Langleyc9fb3752014-06-20 12:00:00 -0700242/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
243 * for |s->cipher|. It returns 1 on success and 0 on error. */
244int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
245 {
246 const SSL_CIPHER *c = s->cipher;
247
248 *aead = NULL;
249
250 if (c == NULL)
251 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700252 if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0 &&
253 (c->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) == 0)
Adam Langleyc9fb3752014-06-20 12:00:00 -0700254 return 0;
255
Adam Langleyde0b2022014-06-20 12:00:00 -0700256 switch (c->algorithm_enc)
257 {
258 case SSL_AES128GCM:
259 *aead = EVP_aead_aes_128_gcm();
260 return 1;
Adam Langley5c270c52014-06-20 12:00:00 -0700261 case SSL_AES256GCM:
262 *aead = EVP_aead_aes_256_gcm();
263 return 1;
Adam Langleyde0b2022014-06-20 12:00:00 -0700264 case SSL_CHACHA20POLY1305:
265 *aead = EVP_aead_chacha20_poly1305();
266 return 1;
Adam Langley9447dff2014-06-24 17:29:06 -0700267 case SSL_RC4:
268 if (c->algorithm_mac == SSL_MD5)
269 *aead = EVP_aead_rc4_md5_tls();
270 else
271 return 0;
272 return 1;
273 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700274
275 return 0;
276 }
277
278int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
279 const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
280 {
Adam Langley95c29f32014-06-20 12:00:00 -0700281 const SSL_CIPHER *c;
282
283 c=s->cipher;
284 if (c == NULL) return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langleyc9fb3752014-06-20 12:00:00 -0700286 /* This function doesn't deal with EVP_AEAD. See
287 * |ssl_cipher_get_aead_evp|. */
288 if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
289 return(0);
Adam Langley95c29f32014-06-20 12:00:00 -0700290
291 if ((enc == NULL) || (md == NULL)) return(0);
292
293 switch (c->algorithm_enc)
294 {
Adam Langley95c29f32014-06-20 12:00:00 -0700295 case SSL_3DES:
David Benjamind633d632014-08-30 14:43:27 -0400296 *enc = EVP_des_ede3_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700297 break;
298 case SSL_RC4:
David Benjamind633d632014-08-30 14:43:27 -0400299 *enc = EVP_rc4();
Adam Langley95c29f32014-06-20 12:00:00 -0700300 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700301 case SSL_AES128:
David Benjamind633d632014-08-30 14:43:27 -0400302 *enc = EVP_aes_128_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700303 break;
304 case SSL_AES256:
David Benjamind633d632014-08-30 14:43:27 -0400305 *enc = EVP_aes_256_cbc();
Adam Langley95c29f32014-06-20 12:00:00 -0700306 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700307 default:
David Benjamind633d632014-08-30 14:43:27 -0400308 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700309 }
310
Adam Langley9447dff2014-06-24 17:29:06 -0700311 if (!ssl_cipher_get_mac(s, md, mac_pkey_type, mac_secret_size))
312 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
David Benjamind633d632014-08-30 14:43:27 -0400314 assert(*enc != NULL && *md != NULL);
315
316 /* TODO(fork): enable the stitched cipher modes. */
317#if 0
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (s->ssl_version>>8 != TLS1_VERSION_MAJOR ||
319 s->ssl_version < TLS1_VERSION)
320 return 1;
321
Adam Langley95c29f32014-06-20 12:00:00 -0700322 if (c->algorithm_enc == SSL_RC4 &&
323 c->algorithm_mac == SSL_MD5 &&
324 (evp=EVP_get_cipherbyname("RC4-HMAC-MD5")))
325 *enc = evp, *md = NULL;
326 else if (c->algorithm_enc == SSL_AES128 &&
327 c->algorithm_mac == SSL_SHA1 &&
328 (evp=EVP_get_cipherbyname("AES-128-CBC-HMAC-SHA1")))
329 *enc = evp, *md = NULL;
330 else if (c->algorithm_enc == SSL_AES256 &&
331 c->algorithm_mac == SSL_SHA1 &&
332 (evp=EVP_get_cipherbyname("AES-256-CBC-HMAC-SHA1")))
333 *enc = evp, *md = NULL;
334#endif
David Benjamind633d632014-08-30 14:43:27 -0400335
336 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700337 }
338
Adam Langley9447dff2014-06-24 17:29:06 -0700339int ssl_cipher_get_mac(const SSL_SESSION *s, const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
340 {
Adam Langley9447dff2014-06-24 17:29:06 -0700341 const SSL_CIPHER *c;
342
343 c=s->cipher;
344 if (c == NULL) return(0);
345
346 switch (c->algorithm_mac)
347 {
348 case SSL_MD5:
David Benjamind633d632014-08-30 14:43:27 -0400349 *md = EVP_md5();
Adam Langley9447dff2014-06-24 17:29:06 -0700350 break;
351 case SSL_SHA1:
David Benjamind633d632014-08-30 14:43:27 -0400352 *md = EVP_sha1();
Adam Langley9447dff2014-06-24 17:29:06 -0700353 break;
354 case SSL_SHA256:
David Benjamind633d632014-08-30 14:43:27 -0400355 *md = EVP_sha256();
Adam Langley9447dff2014-06-24 17:29:06 -0700356 break;
357 case SSL_SHA384:
David Benjamind633d632014-08-30 14:43:27 -0400358 *md = EVP_sha384();
Adam Langley9447dff2014-06-24 17:29:06 -0700359 break;
Adam Langley9447dff2014-06-24 17:29:06 -0700360 default:
David Benjamind633d632014-08-30 14:43:27 -0400361 return 0;
Adam Langley9447dff2014-06-24 17:29:06 -0700362 }
363
David Benjamind633d632014-08-30 14:43:27 -0400364 if (mac_pkey_type != NULL)
Adam Langley9447dff2014-06-24 17:29:06 -0700365 {
David Benjamind633d632014-08-30 14:43:27 -0400366 *mac_pkey_type = EVP_PKEY_HMAC;
Adam Langley9447dff2014-06-24 17:29:06 -0700367 }
David Benjamind633d632014-08-30 14:43:27 -0400368 if (mac_secret_size!=NULL)
Adam Langley9447dff2014-06-24 17:29:06 -0700369 {
David Benjamind633d632014-08-30 14:43:27 -0400370 *mac_secret_size = EVP_MD_size(*md);
Adam Langley9447dff2014-06-24 17:29:06 -0700371 }
Adam Langley9447dff2014-06-24 17:29:06 -0700372 return 1;
373 }
374
Adam Langley95c29f32014-06-20 12:00:00 -0700375int ssl_get_handshake_digest(int idx, long *mask, const EVP_MD **md)
376{
David Benjamind633d632014-08-30 14:43:27 -0400377 if (idx < 0 || idx >= SSL_MAX_DIGEST)
Adam Langley95c29f32014-06-20 12:00:00 -0700378 {
379 return 0;
380 }
David Benjamind633d632014-08-30 14:43:27 -0400381 *mask = ssl_handshake_digests[idx].mask;
382 *md = ssl_handshake_digests[idx].md_func();
Adam Langley95c29f32014-06-20 12:00:00 -0700383 return 1;
384}
385
386#define ITEM_SEP(a) \
387 (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
388
389static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
390 CIPHER_ORDER **tail)
391 {
392 if (curr == *tail) return;
393 if (curr == *head)
394 *head=curr->next;
395 if (curr->prev != NULL)
396 curr->prev->next=curr->next;
397 if (curr->next != NULL)
398 curr->next->prev=curr->prev;
399 (*tail)->next=curr;
400 curr->prev= *tail;
401 curr->next=NULL;
402 *tail=curr;
403 }
404
405static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
406 CIPHER_ORDER **tail)
407 {
408 if (curr == *head) return;
409 if (curr == *tail)
410 *tail=curr->prev;
411 if (curr->next != NULL)
412 curr->next->prev=curr->prev;
413 if (curr->prev != NULL)
414 curr->prev->next=curr->next;
415 (*head)->prev=curr;
416 curr->next= *head;
417 curr->prev=NULL;
418 *head=curr;
419 }
420
Adam Langley95c29f32014-06-20 12:00:00 -0700421static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,
422 int num_of_ciphers,
Adam Langley95c29f32014-06-20 12:00:00 -0700423 CIPHER_ORDER *co_list,
424 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
425 {
426 int i, co_list_num;
427 const SSL_CIPHER *c;
428
429 /*
430 * We have num_of_ciphers descriptions compiled in, depending on the
431 * method selected (SSLv2 and/or SSLv3, TLSv1 etc).
432 * These will later be sorted in a linked list with at most num
433 * entries.
434 */
435
436 /* Get the initial list of ciphers */
437 co_list_num = 0; /* actual count of ciphers */
438 for (i = 0; i < num_of_ciphers; i++)
439 {
440 c = ssl_method->get_cipher(i);
441 /* drop those that use any of that is not available */
David Benjamind633d632014-08-30 14:43:27 -0400442 if ((c != NULL) && c->valid)
Adam Langley95c29f32014-06-20 12:00:00 -0700443 {
444 co_list[co_list_num].cipher = c;
445 co_list[co_list_num].next = NULL;
446 co_list[co_list_num].prev = NULL;
447 co_list[co_list_num].active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700448 co_list[co_list_num].in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700449 co_list_num++;
450#ifdef KSSL_DEBUG
451 printf("\t%d: %s %lx %lx %lx\n",i,c->name,c->id,c->algorithm_mkey,c->algorithm_auth);
452#endif /* KSSL_DEBUG */
453 /*
454 if (!sk_push(ca_list,(char *)c)) goto err;
455 */
456 }
457 }
458
459 /*
460 * Prepare linked list from list entries
461 */
462 if (co_list_num > 0)
463 {
464 co_list[0].prev = NULL;
465
466 if (co_list_num > 1)
467 {
468 co_list[0].next = &co_list[1];
469
470 for (i = 1; i < co_list_num - 1; i++)
471 {
472 co_list[i].prev = &co_list[i - 1];
473 co_list[i].next = &co_list[i + 1];
474 }
475
476 co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
477 }
478
479 co_list[co_list_num - 1].next = NULL;
480
481 *head_p = &co_list[0];
482 *tail_p = &co_list[co_list_num - 1];
483 }
484 }
485
486static void ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list,
487 int num_of_group_aliases,
Adam Langley95c29f32014-06-20 12:00:00 -0700488 CIPHER_ORDER *head)
489 {
490 CIPHER_ORDER *ciph_curr;
491 const SSL_CIPHER **ca_curr;
492 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700493
494 /*
495 * First, add the real ciphers as already collected
496 */
497 ciph_curr = head;
498 ca_curr = ca_list;
499 while (ciph_curr != NULL)
500 {
501 *ca_curr = ciph_curr->cipher;
502 ca_curr++;
503 ciph_curr = ciph_curr->next;
504 }
505
506 /*
507 * Now we add the available ones from the cipher_aliases[] table.
508 * They represent either one or more algorithms, some of which
509 * in any affected category must be supported (set in enabled_mask),
510 * or represent a cipher strength value (will be added in any case because algorithms=0).
511 */
512 for (i = 0; i < num_of_group_aliases; i++)
513 {
David Benjamin6f260012014-08-15 13:49:12 -0400514 *ca_curr = cipher_aliases + i;
Adam Langley95c29f32014-06-20 12:00:00 -0700515 ca_curr++;
516 }
517
518 *ca_curr = NULL; /* end of list */
519 }
520
521static void ssl_cipher_apply_rule(unsigned long cipher_id,
522 unsigned long alg_mkey, unsigned long alg_auth,
523 unsigned long alg_enc, unsigned long alg_mac,
524 unsigned long alg_ssl,
525 unsigned long algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700526 int rule, int strength_bits, int in_group,
Adam Langley95c29f32014-06-20 12:00:00 -0700527 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
528 {
Adam Langleye3142a72014-07-24 17:56:48 -0700529 CIPHER_ORDER *head, *tail, *curr, *next, *last;
Adam Langley95c29f32014-06-20 12:00:00 -0700530 const SSL_CIPHER *cp;
531 int reverse = 0;
532
533#ifdef CIPHER_DEBUG
Adam Langley858a88d2014-06-20 12:00:00 -0700534 printf("Applying rule %d with %08lx/%08lx/%08lx/%08lx/%08lx %08lx (%d) in_group:%d\n",
535 rule, alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength, strength_bits, in_group);
Adam Langley95c29f32014-06-20 12:00:00 -0700536#endif
537
538 if (rule == CIPHER_DEL)
539 reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
540
541 head = *head_p;
542 tail = *tail_p;
543
544 if (reverse)
545 {
Adam Langleye3142a72014-07-24 17:56:48 -0700546 next = tail;
Adam Langley95c29f32014-06-20 12:00:00 -0700547 last = head;
548 }
549 else
550 {
Adam Langleye3142a72014-07-24 17:56:48 -0700551 next = head;
Adam Langley95c29f32014-06-20 12:00:00 -0700552 last = tail;
553 }
554
Adam Langleye3142a72014-07-24 17:56:48 -0700555 curr = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700556 for (;;)
557 {
Adam Langleye3142a72014-07-24 17:56:48 -0700558 if (curr == last) break;
559
560 curr = next;
561
562 if (curr == NULL) break;
563
564 next = reverse ? curr->prev : curr->next;
Adam Langley95c29f32014-06-20 12:00:00 -0700565
566 cp = curr->cipher;
567
568 /*
569 * Selection criteria is either the value of strength_bits
570 * or the algorithms used.
571 */
572 if (strength_bits >= 0)
573 {
574 if (strength_bits != cp->strength_bits)
575 continue;
576 }
577 else
578 {
579#ifdef CIPHER_DEBUG
580 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);
581#endif
582#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
583 if (cipher_id && cipher_id != cp->id)
584 continue;
585#endif
586 if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
587 continue;
588 if (alg_auth && !(alg_auth & cp->algorithm_auth))
589 continue;
590 if (alg_enc && !(alg_enc & cp->algorithm_enc))
591 continue;
592 if (alg_mac && !(alg_mac & cp->algorithm_mac))
593 continue;
594 if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
595 continue;
David Benjamin594a58e2014-09-07 13:51:08 -0400596 if (algo_strength && !(algo_strength & cp->algo_strength))
Adam Langley95c29f32014-06-20 12:00:00 -0700597 continue;
598 }
599
600#ifdef CIPHER_DEBUG
601 printf("Action = %d\n", rule);
602#endif
603
604 /* add the cipher if it has not been added yet. */
605 if (rule == CIPHER_ADD)
606 {
607 /* reverse == 0 */
608 if (!curr->active)
609 {
610 ll_append_tail(&head, curr, &tail);
611 curr->active = 1;
Adam Langley858a88d2014-06-20 12:00:00 -0700612 curr->in_group = in_group;
Adam Langley95c29f32014-06-20 12:00:00 -0700613 }
614 }
615 /* Move the added cipher to this location */
616 else if (rule == CIPHER_ORD)
617 {
618 /* reverse == 0 */
619 if (curr->active)
620 {
621 ll_append_tail(&head, curr, &tail);
Adam Langley858a88d2014-06-20 12:00:00 -0700622 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700623 }
624 }
625 else if (rule == CIPHER_DEL)
626 {
627 /* reverse == 1 */
628 if (curr->active)
629 {
630 /* most recently deleted ciphersuites get best positions
631 * for any future CIPHER_ADD (note that the CIPHER_DEL loop
632 * works in reverse to maintain the order) */
633 ll_append_head(&head, curr, &tail);
634 curr->active = 0;
Adam Langley858a88d2014-06-20 12:00:00 -0700635 curr->in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700636 }
637 }
638 else if (rule == CIPHER_KILL)
639 {
640 /* reverse == 0 */
641 if (head == curr)
642 head = curr->next;
643 else
644 curr->prev->next = curr->next;
645 if (tail == curr)
646 tail = curr->prev;
647 curr->active = 0;
648 if (curr->next != NULL)
649 curr->next->prev = curr->prev;
650 if (curr->prev != NULL)
651 curr->prev->next = curr->next;
652 curr->next = NULL;
653 curr->prev = NULL;
654 }
655 }
656
657 *head_p = head;
658 *tail_p = tail;
659 }
660
661static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
662 CIPHER_ORDER **tail_p)
663 {
664 int max_strength_bits, i, *number_uses;
665 CIPHER_ORDER *curr;
666
667 /*
668 * This routine sorts the ciphers with descending strength. The sorting
669 * must keep the pre-sorted sequence, so we apply the normal sorting
670 * routine as '+' movement to the end of the list.
671 */
672 max_strength_bits = 0;
673 curr = *head_p;
674 while (curr != NULL)
675 {
676 if (curr->active &&
677 (curr->cipher->strength_bits > max_strength_bits))
678 max_strength_bits = curr->cipher->strength_bits;
679 curr = curr->next;
680 }
681
682 number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
683 if (!number_uses)
684 {
685 OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE);
686 return(0);
687 }
688 memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
689
690 /*
691 * Now find the strength_bits values actually used
692 */
693 curr = *head_p;
694 while (curr != NULL)
695 {
696 if (curr->active)
697 number_uses[curr->cipher->strength_bits]++;
698 curr = curr->next;
699 }
700 /*
701 * Go through the list of used strength_bits values in descending
702 * order.
703 */
704 for (i = max_strength_bits; i >= 0; i--)
705 if (number_uses[i] > 0)
Adam Langley858a88d2014-06-20 12:00:00 -0700706 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 -0700707
708 OPENSSL_free(number_uses);
709 return(1);
710 }
711
712static int ssl_cipher_process_rulestr(const char *rule_str,
713 CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p,
714 const SSL_CIPHER **ca_list)
715 {
716 unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
717 const char *l, *buf;
Adam Langley858a88d2014-06-20 12:00:00 -0700718 int j, multi, found, rule, retval, ok, buflen, in_group = 0,
719 has_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700720 unsigned long cipher_id = 0;
721 char ch;
722
723 retval = 1;
724 l = rule_str;
725 for (;;)
726 {
727 ch = *l;
728
729 if (ch == '\0')
730 break; /* done */
Adam Langley858a88d2014-06-20 12:00:00 -0700731 if (in_group)
732 {
733 if (ch == ']')
734 {
735 if (!in_group)
736 {
737 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_GROUP_CLOSE);
738 retval = found = in_group = 0;
739 break;
740 }
741 if (*tail_p)
742 (*tail_p)->in_group = 0;
743 in_group = 0;
744 l++;
745 continue;
746 }
747 if (ch == '|')
748 { rule = CIPHER_ADD; l++; continue; }
749 else if (!(ch >= 'a' && ch <= 'z') &&
750 !(ch >= 'A' && ch <= 'Z') &&
751 !(ch >= '0' && ch <= '9'))
752 {
753 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
754 retval = found = in_group = 0;
755 break;
756 }
757 else
758 rule = CIPHER_ADD;
759 }
760 else if (ch == '-')
Adam Langley95c29f32014-06-20 12:00:00 -0700761 { rule = CIPHER_DEL; l++; }
762 else if (ch == '+')
763 { rule = CIPHER_ORD; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700764 else if (ch == '!' && has_group)
765 {
766 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
767 retval = found = in_group = 0;
768 break;
769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770 else if (ch == '!')
771 { rule = CIPHER_KILL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700772 else if (ch == '@' && has_group)
773 {
774 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
775 retval = found = in_group = 0;
776 break;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778 else if (ch == '@')
779 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700780 else if (ch == '[')
781 {
782 if (in_group)
783 {
784 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
785 retval = found = in_group = 0;
786 break;
787 }
788 in_group = 1;
789 has_group = 1;
790 l++;
791 continue;
792 }
Adam Langley95c29f32014-06-20 12:00:00 -0700793 else
794 { rule = CIPHER_ADD; }
795
796 if (ITEM_SEP(ch))
797 {
798 l++;
799 continue;
800 }
801
802 alg_mkey = 0;
803 alg_auth = 0;
804 alg_enc = 0;
805 alg_mac = 0;
806 alg_ssl = 0;
807 algo_strength = 0;
808
809 for (;;)
810 {
811 ch = *l;
812 buf = l;
813 buflen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700814 while ( ((ch >= 'A') && (ch <= 'Z')) ||
815 ((ch >= '0') && (ch <= '9')) ||
816 ((ch >= 'a') && (ch <= 'z')) ||
817 (ch == '-') || (ch == '.'))
Adam Langley95c29f32014-06-20 12:00:00 -0700818 {
819 ch = *(++l);
820 buflen++;
821 }
822
823 if (buflen == 0)
824 {
825 /*
826 * We hit something we cannot deal with,
827 * it is no command or separator nor
828 * alphanumeric, so we call this an error.
829 */
830 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -0700831 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700832 l++;
833 break;
834 }
835
836 if (rule == CIPHER_SPECIAL)
837 {
838 found = 0; /* unused -- avoid compiler warning */
839 break; /* special treatment */
840 }
841
842 /* check for multi-part specification */
843 if (ch == '+')
844 {
845 multi=1;
846 l++;
847 }
848 else
849 multi=0;
850
851 /*
852 * Now search for the cipher alias in the ca_list. Be careful
853 * with the strncmp, because the "buflen" limitation
854 * will make the rule "ADH:SOME" and the cipher
855 * "ADH-MY-CIPHER" look like a match for buflen=3.
856 * So additionally check whether the cipher name found
857 * has the correct length. We can save a strlen() call:
858 * just checking for the '\0' at the right place is
859 * sufficient, we have to strncmp() anyway. (We cannot
860 * use strcmp(), because buf is not '\0' terminated.)
861 */
862 j = found = 0;
863 cipher_id = 0;
864 while (ca_list[j])
865 {
866 if (!strncmp(buf, ca_list[j]->name, buflen) &&
867 (ca_list[j]->name[buflen] == '\0'))
868 {
869 found = 1;
870 break;
871 }
872 else
873 j++;
874 }
875
876 if (!found)
877 break; /* ignore this entry */
878
879 if (ca_list[j]->algorithm_mkey)
880 {
881 if (alg_mkey)
882 {
883 alg_mkey &= ca_list[j]->algorithm_mkey;
884 if (!alg_mkey) { found = 0; break; }
885 }
886 else
887 alg_mkey = ca_list[j]->algorithm_mkey;
888 }
889
890 if (ca_list[j]->algorithm_auth)
891 {
892 if (alg_auth)
893 {
894 alg_auth &= ca_list[j]->algorithm_auth;
895 if (!alg_auth) { found = 0; break; }
896 }
897 else
898 alg_auth = ca_list[j]->algorithm_auth;
899 }
900
901 if (ca_list[j]->algorithm_enc)
902 {
903 if (alg_enc)
904 {
905 alg_enc &= ca_list[j]->algorithm_enc;
906 if (!alg_enc) { found = 0; break; }
907 }
908 else
909 alg_enc = ca_list[j]->algorithm_enc;
910 }
911
912 if (ca_list[j]->algorithm_mac)
913 {
914 if (alg_mac)
915 {
916 alg_mac &= ca_list[j]->algorithm_mac;
917 if (!alg_mac) { found = 0; break; }
918 }
919 else
920 alg_mac = ca_list[j]->algorithm_mac;
921 }
922
David Benjamin594a58e2014-09-07 13:51:08 -0400923 if (ca_list[j]->algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700924 {
David Benjamin594a58e2014-09-07 13:51:08 -0400925 if (algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700926 {
David Benjamin594a58e2014-09-07 13:51:08 -0400927 algo_strength &= ca_list[j]->algo_strength;
928 if (!algo_strength) { found = 0; break; }
Adam Langley95c29f32014-06-20 12:00:00 -0700929 }
930 else
David Benjamin594a58e2014-09-07 13:51:08 -0400931 algo_strength |= ca_list[j]->algo_strength;
Adam Langley95c29f32014-06-20 12:00:00 -0700932 }
933
934 if (ca_list[j]->valid)
935 {
936 /* explicit ciphersuite found; its protocol version
937 * does not become part of the search pattern!*/
938
939 cipher_id = ca_list[j]->id;
940 }
941 else
942 {
943 /* not an explicit ciphersuite; only in this case, the
944 * protocol version is considered part of the search pattern */
945
946 if (ca_list[j]->algorithm_ssl)
947 {
948 if (alg_ssl)
949 {
950 alg_ssl &= ca_list[j]->algorithm_ssl;
951 if (!alg_ssl) { found = 0; break; }
952 }
953 else
954 alg_ssl = ca_list[j]->algorithm_ssl;
955 }
956 }
957
958 if (!multi) break;
959 }
960
961 /*
962 * Ok, we have the rule, now apply it
963 */
964 if (rule == CIPHER_SPECIAL)
965 { /* special command */
966 ok = 0;
967 if ((buflen == 8) &&
968 !strncmp(buf, "STRENGTH", 8))
969 ok = ssl_cipher_strength_sort(head_p, tail_p);
970 else
971 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
972 if (ok == 0)
973 retval = 0;
974 /*
975 * We do not support any "multi" options
976 * together with "@", so throw away the
977 * rest of the command, if any left, until
978 * end or ':' is found.
979 */
980 while ((*l != '\0') && !ITEM_SEP(*l))
981 l++;
982 }
983 else if (found)
984 {
985 ssl_cipher_apply_rule(cipher_id,
986 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700987 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -0700988 }
989 else
990 {
991 while ((*l != '\0') && !ITEM_SEP(*l))
992 l++;
993 }
Adam Langley95c29f32014-06-20 12:00:00 -0700994 }
995
Adam Langley858a88d2014-06-20 12:00:00 -0700996 if (in_group)
997 {
998 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
999 retval = 0;
1000 }
1001
Adam Langley95c29f32014-06-20 12:00:00 -07001002 return(retval);
1003 }
Adam Langley95c29f32014-06-20 12:00:00 -07001004
1005
1006STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -07001007 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -07001008 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1009 const char *rule_str, CERT *c)
1010 {
1011 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
Adam Langley858a88d2014-06-20 12:00:00 -07001012 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001013 const char *rule_p;
1014 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1015 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001016 unsigned char *in_group_flags = NULL;
1017 unsigned int num_in_group_flags = 0;
1018 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
1020 /*
1021 * Return with error if nothing to do.
1022 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001023 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001024 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001025
1026 /*
Adam Langley95c29f32014-06-20 12:00:00 -07001027 * Now we have to collect the available ciphers from the compiled
1028 * in ciphers. We cannot get more than the number compiled in, so
1029 * it is used for allocation.
1030 */
1031 num_of_ciphers = ssl_method->num_ciphers();
1032#ifdef KSSL_DEBUG
1033 printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
1034#endif /* KSSL_DEBUG */
1035 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1036 if (co_list == NULL)
1037 {
1038 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1039 return(NULL); /* Failure */
1040 }
1041
1042 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
Adam Langley95c29f32014-06-20 12:00:00 -07001043 co_list, &head, &tail);
1044
1045
David Benjaminede973a2014-08-19 14:12:39 -04001046 /* Now arrange all ciphers by preference:
1047 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001048
1049 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001050 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1051 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 -07001052
David Benjamin5213df42014-08-20 14:19:54 -04001053 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1054 * CHACHA20 unless there is hardware support for fast and constant-time
1055 * AES_GCM. */
1056 if (EVP_has_aes_hardware())
1057 {
1058 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1059 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1060 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1061 }
1062 else
1063 {
1064 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1065 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1066 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1067 }
1068
1069 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
1070 * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjaminede973a2014-08-19 14:12:39 -04001071 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1072 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1073 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1074 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1075 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 -07001076
1077 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001078 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001079
David Benjaminede973a2014-08-19 14:12:39 -04001080 /* Move ciphers without forward secrecy to the end. */
1081 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 -07001082
1083 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1084 * (For applications that allow them, they aren't too bad, but we prefer
David Benjaminede973a2014-08-19 14:12:39 -04001085 * authenticated ciphers.)
1086 * TODO(davidben): Remove them altogether? */
Adam Langley858a88d2014-06-20 12:00:00 -07001087 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 -07001088
Adam Langley95c29f32014-06-20 12:00:00 -07001089 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001090 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langley95c29f32014-06-20 12:00:00 -07001092 /*
1093 * We also need cipher aliases for selecting based on the rule_str.
1094 * There might be two types of entries in the rule_str: 1) names
1095 * of ciphers themselves 2) aliases for groups of ciphers.
1096 * For 1) we need the available ciphers and for 2) the cipher
1097 * groups of cipher_aliases added together in one list (otherwise
1098 * we would be happy with just the cipher_aliases table).
1099 */
1100 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1101 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1102 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1103 if (ca_list == NULL)
1104 {
Adam Langley95c29f32014-06-20 12:00:00 -07001105 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
David Benjamind633d632014-08-30 14:43:27 -04001106 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001107 }
David Benjamind633d632014-08-30 14:43:27 -04001108 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001109
1110 /*
1111 * If the rule_string begins with DEFAULT, apply the default rule
1112 * before using the (possibly available) additional rules.
1113 */
1114 ok = 1;
1115 rule_p = rule_str;
1116 if (strncmp(rule_str,"DEFAULT",7) == 0)
1117 {
1118 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1119 &head, &tail, ca_list);
1120 rule_p += 7;
1121 if (*rule_p == ':')
1122 rule_p++;
1123 }
1124
1125 if (ok && (strlen(rule_p) > 0))
1126 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1127
1128 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1129
1130 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001131 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001132
1133 /*
1134 * Allocate new "cipherstack" for the result, return with error
1135 * if we cannot get one.
1136 */
1137 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001138 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langley858a88d2014-06-20 12:00:00 -07001140 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1141 if (!in_group_flags)
1142 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001143 /*
1144 * The cipher selection for the list is done. The ciphers are added
1145 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1146 */
1147 for (curr = head; curr != NULL; curr = curr->next)
1148 {
Adam Langley95c29f32014-06-20 12:00:00 -07001149 if (curr->active)
Adam Langley95c29f32014-06-20 12:00:00 -07001150 {
1151 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001152 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001153#ifdef CIPHER_DEBUG
1154 printf("<%s>\n",curr->cipher->name);
1155#endif
1156 }
1157 }
1158 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001159 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001160
1161 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1162 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001163 goto err;
1164 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1165 if (!pref_list)
1166 goto err;
1167 pref_list->ciphers = cipherstack;
1168 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1169 if (!pref_list->in_group_flags)
1170 goto err;
1171 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1172 OPENSSL_free(in_group_flags);
1173 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001174 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001175 ssl_cipher_preference_list_free(*cipher_list);
1176 *cipher_list = pref_list;
1177 pref_list = NULL;
1178
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001179 if (cipher_list_by_id != NULL)
1180 {
1181 if (*cipher_list_by_id != NULL)
1182 sk_SSL_CIPHER_free(*cipher_list_by_id);
1183 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001184 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001185 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001187 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1188 }
1189 else
Adam Langley858a88d2014-06-20 12:00:00 -07001190 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001191 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001192 tmp_cipher_list = NULL;
1193 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001194
Adam Langley95c29f32014-06-20 12:00:00 -07001195 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001196
1197err:
1198 if (co_list)
1199 OPENSSL_free(co_list);
1200 if (in_group_flags)
1201 OPENSSL_free(in_group_flags);
1202 if (cipherstack)
1203 sk_SSL_CIPHER_free(cipherstack);
1204 if (tmp_cipher_list)
1205 sk_SSL_CIPHER_free(tmp_cipher_list);
1206 if (pref_list && pref_list->in_group_flags)
1207 OPENSSL_free(pref_list->in_group_flags);
1208 if (pref_list)
1209 OPENSSL_free(pref_list);
1210 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001211 }
1212
Adam Langley73510762014-06-20 12:00:00 -07001213const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
Adam Langley95c29f32014-06-20 12:00:00 -07001214 {
David Benjamin060d9d22014-07-15 00:54:26 -04001215 const char *ver;
Adam Langley95c29f32014-06-20 12:00:00 -07001216 const char *kx,*au,*enc,*mac;
David Benjaminebf42b52014-08-19 15:18:42 -04001217 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001218#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001219 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 -07001220#else
David Benjamin060d9d22014-07-15 00:54:26 -04001221 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001222#endif /* KSSL_DEBUG */
1223
1224 alg_mkey = cipher->algorithm_mkey;
1225 alg_auth = cipher->algorithm_auth;
1226 alg_enc = cipher->algorithm_enc;
1227 alg_mac = cipher->algorithm_mac;
1228 alg_ssl = cipher->algorithm_ssl;
1229
David Benjaminebf42b52014-08-19 15:18:42 -04001230 if (alg_ssl & SSL_SSLV3)
Adam Langley95c29f32014-06-20 12:00:00 -07001231 ver="SSLv3";
1232 else if (alg_ssl & SSL_TLSV1_2)
1233 ver="TLSv1.2";
1234 else
1235 ver="unknown";
1236
1237 switch (alg_mkey)
1238 {
1239 case SSL_kRSA:
David Benjamin060d9d22014-07-15 00:54:26 -04001240 kx="RSA";
Adam Langley95c29f32014-06-20 12:00:00 -07001241 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001242 case SSL_kEDH:
David Benjamin060d9d22014-07-15 00:54:26 -04001243 kx="DH";
Adam Langley95c29f32014-06-20 12:00:00 -07001244 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001245 case SSL_kEECDH:
1246 kx="ECDH";
1247 break;
1248 case SSL_kPSK:
1249 kx="PSK";
1250 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001251 default:
1252 kx="unknown";
1253 }
1254
1255 switch (alg_auth)
1256 {
1257 case SSL_aRSA:
1258 au="RSA";
1259 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001260 case SSL_aNULL:
1261 au="None";
1262 break;
1263 case SSL_aECDSA:
1264 au="ECDSA";
1265 break;
1266 case SSL_aPSK:
1267 au="PSK";
1268 break;
1269 default:
1270 au="unknown";
1271 break;
1272 }
1273
1274 switch (alg_enc)
1275 {
Adam Langley95c29f32014-06-20 12:00:00 -07001276 case SSL_3DES:
1277 enc="3DES(168)";
1278 break;
1279 case SSL_RC4:
David Benjaminebf42b52014-08-19 15:18:42 -04001280 enc="RC4(128)";
Adam Langley95c29f32014-06-20 12:00:00 -07001281 break;
1282 case SSL_AES128:
1283 enc="AES(128)";
1284 break;
1285 case SSL_AES256:
1286 enc="AES(256)";
1287 break;
1288 case SSL_AES128GCM:
1289 enc="AESGCM(128)";
1290 break;
1291 case SSL_AES256GCM:
1292 enc="AESGCM(256)";
1293 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001294 case SSL_CHACHA20POLY1305:
1295 enc="ChaCha20-Poly1305";
1296 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001297 default:
1298 enc="unknown";
1299 break;
1300 }
1301
1302 switch (alg_mac)
1303 {
1304 case SSL_MD5:
1305 mac="MD5";
1306 break;
1307 case SSL_SHA1:
1308 mac="SHA1";
1309 break;
1310 case SSL_SHA256:
1311 mac="SHA256";
1312 break;
1313 case SSL_SHA384:
1314 mac="SHA384";
1315 break;
1316 case SSL_AEAD:
1317 mac="AEAD";
1318 break;
1319 default:
1320 mac="unknown";
1321 break;
1322 }
1323
1324 if (buf == NULL)
1325 {
1326 len=128;
1327 buf=OPENSSL_malloc(len);
1328 if (buf == NULL) return("OPENSSL_malloc Error");
1329 }
1330 else if (len < 128)
1331 return("Buffer too small");
1332
1333#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001334 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 -07001335#else
David Benjamin060d9d22014-07-15 00:54:26 -04001336 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
Adam Langley95c29f32014-06-20 12:00:00 -07001337#endif /* KSSL_DEBUG */
1338 return(buf);
1339 }
1340
Adam Langley4d4bff82014-06-20 12:00:00 -07001341/* Next three functions require non-null cipher */
1342int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1343 {
1344 return (c->algorithm_enc & SSL_AES) != 0;
1345 }
1346
1347int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1348 {
1349 return (c->algorithm_mac & SSL_MD5) != 0;
1350 }
1351
1352int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1353 {
1354 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1355 }
1356
Adam Langleyde0b2022014-06-20 12:00:00 -07001357int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1358 {
1359 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1360 }
1361
Adam Langley73510762014-06-20 12:00:00 -07001362const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
Adam Langley95c29f32014-06-20 12:00:00 -07001363 {
1364 int i;
1365
1366 if (c == NULL) return("(NONE)");
1367 i=(int)(c->id>>24L);
1368 if (i == 3)
1369 return("TLSv1/SSLv3");
1370 else if (i == 2)
1371 return("SSLv2");
1372 else
1373 return("unknown");
1374 }
1375
1376/* return the actual cipher being used */
1377const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1378 {
1379 if (c != NULL)
1380 return(c->name);
1381 return("(NONE)");
1382 }
1383
Adam Langleyf1df2b32014-08-11 17:37:53 -07001384const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1385 if (cipher == NULL) {
1386 return "";
1387 }
1388
1389 switch (cipher->algorithm_mkey) {
1390 case SSL_kRSA:
1391 return SSL_TXT_RSA;
Adam Langleyf1df2b32014-08-11 17:37:53 -07001392 case SSL_kEDH:
1393 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001394 case SSL_aRSA:
1395 return "DHE_" SSL_TXT_RSA;
1396 case SSL_aNULL:
1397 return SSL_TXT_DH "_anon";
1398 default:
1399 return "UNKNOWN";
1400 }
Adam Langleyf1df2b32014-08-11 17:37:53 -07001401 case SSL_kEECDH:
1402 switch (cipher->algorithm_auth) {
1403 case SSL_aECDSA:
1404 return "ECDHE_" SSL_TXT_ECDSA;
1405 case SSL_aRSA:
1406 return "ECDHE_" SSL_TXT_RSA;
1407 case SSL_aNULL:
1408 return SSL_TXT_ECDH "_anon";
1409 default:
1410 return "UNKNOWN";
1411 }
1412 default:
1413 return "UNKNOWN";
1414 }
1415}
1416
Adam Langley95c29f32014-06-20 12:00:00 -07001417/* number of bits for symmetric cipher */
1418int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1419 {
1420 int ret=0;
1421
1422 if (c != NULL)
1423 {
1424 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1425 ret = c->strength_bits;
1426 }
1427 return(ret);
1428 }
1429
1430unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1431 {
1432 return c->id;
1433 }
1434
Adam Langley95c29f32014-06-20 12:00:00 -07001435void *SSL_COMP_get_compression_methods(void)
1436 {
1437 return NULL;
1438 }
1439int SSL_COMP_add_compression_method(int id, void *cm)
1440 {
1441 return 1;
1442 }
1443
1444const char *SSL_COMP_get_name(const void *comp)
1445 {
1446 return NULL;
1447 }
1448
1449/* For a cipher return the index corresponding to the certificate type */
1450int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1451 {
David Benjamin0da0e182014-08-19 16:20:28 -04001452 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001453
David Benjamin0da0e182014-08-19 16:20:28 -04001454 if (alg_a & SSL_aECDSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001455 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07001456 else if (alg_a & SSL_aRSA)
1457 return SSL_PKEY_RSA_ENC;
Adam Langley95c29f32014-06-20 12:00:00 -07001458 return -1;
1459 }
1460
David Benjamine8f3d662014-07-12 01:10:19 -04001461/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
1462 * server public key in the key exchange, sent in a server Certificate
1463 * message. Otherwise it returns 0. */
1464int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
1465 {
1466 /* Anonymous ciphers do not include a server certificate. */
1467 if (cipher->algorithm_auth & SSL_aNULL)
1468 return 0;
1469 /* Neither do PSK ciphers, except for RSA_PSK. */
1470 if ((cipher->algorithm_auth & SSL_aPSK) &&
1471 !(cipher->algorithm_mkey & SSL_kRSA))
1472 return 0;
1473 /* All other ciphers include it. */
1474 return 1;
1475 }
David Benjamin9c651c92014-07-12 13:27:45 -04001476
1477/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
1478 * requires a ServerKeyExchange message. Otherwise it returns 0.
1479 *
1480 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
1481 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
1482 * to communicate a psk_identity_hint, so it is optional.
1483 *
1484 * Also, as implemented, the RSA key exchange takes an optional
1485 * ServerKeyExchange containing a signed ephemeral RSA encryption key.
1486 *
1487 * TODO(davidben): Can we remove the RSA one? This is a remnant of
1488 * RSA_EXPORT ciphers which required this (it was used to generate an
1489 * ephemeral 512-bit RSA encryption key), but it's allowed for all RSA
David Benjaminb9cc33a2014-07-15 00:09:48 -04001490 * ciphers. */
David Benjamin9c651c92014-07-12 13:27:45 -04001491int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
1492 {
1493 /* Ephemeral Diffie-Hellman key exchanges require a
1494 * ServerKeyExchange. */
1495 if (cipher->algorithm_mkey & SSL_kEDH ||
1496 cipher->algorithm_mkey & SSL_kEECDH)
1497 return 1;
1498 /* It is optional in all others. */
1499 return 0;
1500 }