blob: fbed5482b3033c642446a860e2299d9832dc2248 [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++; }
764 else if (ch == '!')
765 { rule = CIPHER_KILL; l++; }
766 else if (ch == '@')
767 { rule = CIPHER_SPECIAL; l++; }
Adam Langley858a88d2014-06-20 12:00:00 -0700768 else if (ch == '[')
769 {
770 if (in_group)
771 {
772 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP);
773 retval = found = in_group = 0;
774 break;
775 }
776 in_group = 1;
777 has_group = 1;
778 l++;
779 continue;
780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781 else
782 { rule = CIPHER_ADD; }
783
David Benjamin37d92462014-09-20 17:54:24 -0400784 /* If preference groups are enabled, the only legal
785 * operator is +. Otherwise the in_group bits will get
786 * mixed up. */
787 if (has_group && rule != CIPHER_ADD)
788 {
789 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
790 retval = found = in_group = 0;
791 break;
792 }
793
Adam Langley95c29f32014-06-20 12:00:00 -0700794 if (ITEM_SEP(ch))
795 {
796 l++;
797 continue;
798 }
799
800 alg_mkey = 0;
801 alg_auth = 0;
802 alg_enc = 0;
803 alg_mac = 0;
804 alg_ssl = 0;
805 algo_strength = 0;
806
807 for (;;)
808 {
809 ch = *l;
810 buf = l;
811 buflen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700812 while ( ((ch >= 'A') && (ch <= 'Z')) ||
813 ((ch >= '0') && (ch <= '9')) ||
814 ((ch >= 'a') && (ch <= 'z')) ||
815 (ch == '-') || (ch == '.'))
Adam Langley95c29f32014-06-20 12:00:00 -0700816 {
817 ch = *(++l);
818 buflen++;
819 }
820
821 if (buflen == 0)
822 {
823 /*
824 * We hit something we cannot deal with,
825 * it is no command or separator nor
826 * alphanumeric, so we call this an error.
827 */
828 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
Adam Langley858a88d2014-06-20 12:00:00 -0700829 retval = found = in_group = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700830 l++;
831 break;
832 }
833
834 if (rule == CIPHER_SPECIAL)
835 {
836 found = 0; /* unused -- avoid compiler warning */
837 break; /* special treatment */
838 }
839
840 /* check for multi-part specification */
841 if (ch == '+')
842 {
843 multi=1;
844 l++;
845 }
846 else
847 multi=0;
848
849 /*
850 * Now search for the cipher alias in the ca_list. Be careful
851 * with the strncmp, because the "buflen" limitation
852 * will make the rule "ADH:SOME" and the cipher
853 * "ADH-MY-CIPHER" look like a match for buflen=3.
854 * So additionally check whether the cipher name found
855 * has the correct length. We can save a strlen() call:
856 * just checking for the '\0' at the right place is
857 * sufficient, we have to strncmp() anyway. (We cannot
858 * use strcmp(), because buf is not '\0' terminated.)
859 */
860 j = found = 0;
861 cipher_id = 0;
862 while (ca_list[j])
863 {
864 if (!strncmp(buf, ca_list[j]->name, buflen) &&
865 (ca_list[j]->name[buflen] == '\0'))
866 {
867 found = 1;
868 break;
869 }
870 else
871 j++;
872 }
873
874 if (!found)
875 break; /* ignore this entry */
876
877 if (ca_list[j]->algorithm_mkey)
878 {
879 if (alg_mkey)
880 {
881 alg_mkey &= ca_list[j]->algorithm_mkey;
882 if (!alg_mkey) { found = 0; break; }
883 }
884 else
885 alg_mkey = ca_list[j]->algorithm_mkey;
886 }
887
888 if (ca_list[j]->algorithm_auth)
889 {
890 if (alg_auth)
891 {
892 alg_auth &= ca_list[j]->algorithm_auth;
893 if (!alg_auth) { found = 0; break; }
894 }
895 else
896 alg_auth = ca_list[j]->algorithm_auth;
897 }
898
899 if (ca_list[j]->algorithm_enc)
900 {
901 if (alg_enc)
902 {
903 alg_enc &= ca_list[j]->algorithm_enc;
904 if (!alg_enc) { found = 0; break; }
905 }
906 else
907 alg_enc = ca_list[j]->algorithm_enc;
908 }
909
910 if (ca_list[j]->algorithm_mac)
911 {
912 if (alg_mac)
913 {
914 alg_mac &= ca_list[j]->algorithm_mac;
915 if (!alg_mac) { found = 0; break; }
916 }
917 else
918 alg_mac = ca_list[j]->algorithm_mac;
919 }
920
David Benjamin594a58e2014-09-07 13:51:08 -0400921 if (ca_list[j]->algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700922 {
David Benjamin594a58e2014-09-07 13:51:08 -0400923 if (algo_strength)
Adam Langley95c29f32014-06-20 12:00:00 -0700924 {
David Benjamin594a58e2014-09-07 13:51:08 -0400925 algo_strength &= ca_list[j]->algo_strength;
926 if (!algo_strength) { found = 0; break; }
Adam Langley95c29f32014-06-20 12:00:00 -0700927 }
928 else
David Benjamin594a58e2014-09-07 13:51:08 -0400929 algo_strength |= ca_list[j]->algo_strength;
Adam Langley95c29f32014-06-20 12:00:00 -0700930 }
931
932 if (ca_list[j]->valid)
933 {
934 /* explicit ciphersuite found; its protocol version
935 * does not become part of the search pattern!*/
936
937 cipher_id = ca_list[j]->id;
938 }
939 else
940 {
941 /* not an explicit ciphersuite; only in this case, the
942 * protocol version is considered part of the search pattern */
943
944 if (ca_list[j]->algorithm_ssl)
945 {
946 if (alg_ssl)
947 {
948 alg_ssl &= ca_list[j]->algorithm_ssl;
949 if (!alg_ssl) { found = 0; break; }
950 }
951 else
952 alg_ssl = ca_list[j]->algorithm_ssl;
953 }
954 }
955
956 if (!multi) break;
957 }
958
959 /*
960 * Ok, we have the rule, now apply it
961 */
962 if (rule == CIPHER_SPECIAL)
963 { /* special command */
964 ok = 0;
965 if ((buflen == 8) &&
966 !strncmp(buf, "STRENGTH", 8))
967 ok = ssl_cipher_strength_sort(head_p, tail_p);
968 else
969 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
970 if (ok == 0)
971 retval = 0;
972 /*
973 * We do not support any "multi" options
974 * together with "@", so throw away the
975 * rest of the command, if any left, until
976 * end or ':' is found.
977 */
978 while ((*l != '\0') && !ITEM_SEP(*l))
979 l++;
980 }
981 else if (found)
982 {
983 ssl_cipher_apply_rule(cipher_id,
984 alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength,
Adam Langley858a88d2014-06-20 12:00:00 -0700985 rule, -1, in_group, head_p, tail_p);
Adam Langley95c29f32014-06-20 12:00:00 -0700986 }
987 else
988 {
989 while ((*l != '\0') && !ITEM_SEP(*l))
990 l++;
991 }
Adam Langley95c29f32014-06-20 12:00:00 -0700992 }
993
Adam Langley858a88d2014-06-20 12:00:00 -0700994 if (in_group)
995 {
996 OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND);
997 retval = 0;
998 }
999
Adam Langley95c29f32014-06-20 12:00:00 -07001000 return(retval);
1001 }
Adam Langley95c29f32014-06-20 12:00:00 -07001002
1003
1004STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
Adam Langley858a88d2014-06-20 12:00:00 -07001005 struct ssl_cipher_preference_list_st **cipher_list,
Adam Langley95c29f32014-06-20 12:00:00 -07001006 STACK_OF(SSL_CIPHER) **cipher_list_by_id,
1007 const char *rule_str, CERT *c)
1008 {
1009 int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
Adam Langley858a88d2014-06-20 12:00:00 -07001010 STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001011 const char *rule_p;
1012 CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1013 const SSL_CIPHER **ca_list = NULL;
Adam Langley858a88d2014-06-20 12:00:00 -07001014 unsigned char *in_group_flags = NULL;
1015 unsigned int num_in_group_flags = 0;
1016 struct ssl_cipher_preference_list_st *pref_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001017
1018 /*
1019 * Return with error if nothing to do.
1020 */
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001021 if (rule_str == NULL || cipher_list == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001022 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001023
1024 /*
Adam Langley95c29f32014-06-20 12:00:00 -07001025 * Now we have to collect the available ciphers from the compiled
1026 * in ciphers. We cannot get more than the number compiled in, so
1027 * it is used for allocation.
1028 */
1029 num_of_ciphers = ssl_method->num_ciphers();
1030#ifdef KSSL_DEBUG
1031 printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
1032#endif /* KSSL_DEBUG */
1033 co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
1034 if (co_list == NULL)
1035 {
1036 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
1037 return(NULL); /* Failure */
1038 }
1039
1040 ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
Adam Langley95c29f32014-06-20 12:00:00 -07001041 co_list, &head, &tail);
1042
1043
David Benjaminede973a2014-08-19 14:12:39 -04001044 /* Now arrange all ciphers by preference:
1045 * TODO(davidben): Compute this order once and copy it. */
Adam Langley95c29f32014-06-20 12:00:00 -07001046
1047 /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
Adam Langley858a88d2014-06-20 12:00:00 -07001048 ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1049 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 -07001050
David Benjamin5213df42014-08-20 14:19:54 -04001051 /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
1052 * CHACHA20 unless there is hardware support for fast and constant-time
1053 * AES_GCM. */
1054 if (EVP_has_aes_hardware())
1055 {
1056 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1057 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1058 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1059 }
1060 else
1061 {
1062 ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1063 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1064 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128GCM, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1065 }
1066
1067 /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC,
1068 * RC4_128_SHA, RC4_128_MD5, 3DES_EDE_CBC_SHA. */
David Benjaminede973a2014-08-19 14:12:39 -04001069 ssl_cipher_apply_rule(0, 0, 0, SSL_AES256, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1070 ssl_cipher_apply_rule(0, 0, 0, SSL_AES128, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1071 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, ~SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1072 ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, SSL_MD5, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
1073 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 -07001074
1075 /* Temporarily enable everything else for sorting */
Adam Langley858a88d2014-06-20 12:00:00 -07001076 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001077
David Benjaminede973a2014-08-19 14:12:39 -04001078 /* Move ciphers without forward secrecy to the end. */
1079 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 -07001080
1081 /* Move anonymous ciphers to the end. Usually, these will remain disabled.
1082 * (For applications that allow them, they aren't too bad, but we prefer
David Benjaminede973a2014-08-19 14:12:39 -04001083 * authenticated ciphers.)
1084 * TODO(davidben): Remove them altogether? */
Adam Langley858a88d2014-06-20 12:00:00 -07001085 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 -07001086
Adam Langley95c29f32014-06-20 12:00:00 -07001087 /* Now disable everything (maintaining the ordering!) */
Adam Langley858a88d2014-06-20 12:00:00 -07001088 ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, 0, &head, &tail);
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langley95c29f32014-06-20 12:00:00 -07001090 /*
1091 * We also need cipher aliases for selecting based on the rule_str.
1092 * There might be two types of entries in the rule_str: 1) names
1093 * of ciphers themselves 2) aliases for groups of ciphers.
1094 * For 1) we need the available ciphers and for 2) the cipher
1095 * groups of cipher_aliases added together in one list (otherwise
1096 * we would be happy with just the cipher_aliases table).
1097 */
1098 num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1099 num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1100 ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
1101 if (ca_list == NULL)
1102 {
Adam Langley95c29f32014-06-20 12:00:00 -07001103 OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE);
David Benjamind633d632014-08-30 14:43:27 -04001104 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001105 }
David Benjamind633d632014-08-30 14:43:27 -04001106 ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, head);
Adam Langley95c29f32014-06-20 12:00:00 -07001107
1108 /*
1109 * If the rule_string begins with DEFAULT, apply the default rule
1110 * before using the (possibly available) additional rules.
1111 */
1112 ok = 1;
1113 rule_p = rule_str;
1114 if (strncmp(rule_str,"DEFAULT",7) == 0)
1115 {
1116 ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1117 &head, &tail, ca_list);
1118 rule_p += 7;
1119 if (*rule_p == ':')
1120 rule_p++;
1121 }
1122
1123 if (ok && (strlen(rule_p) > 0))
1124 ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
1125
1126 OPENSSL_free((void *)ca_list); /* Not needed anymore */
1127
1128 if (!ok)
Adam Langley858a88d2014-06-20 12:00:00 -07001129 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001130
1131 /*
1132 * Allocate new "cipherstack" for the result, return with error
1133 * if we cannot get one.
1134 */
1135 if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001136 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langley858a88d2014-06-20 12:00:00 -07001138 in_group_flags = OPENSSL_malloc(num_of_ciphers);
1139 if (!in_group_flags)
1140 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001141 /*
1142 * The cipher selection for the list is done. The ciphers are added
1143 * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1144 */
1145 for (curr = head; curr != NULL; curr = curr->next)
1146 {
Adam Langley95c29f32014-06-20 12:00:00 -07001147 if (curr->active)
Adam Langley95c29f32014-06-20 12:00:00 -07001148 {
1149 sk_SSL_CIPHER_push(cipherstack, curr->cipher);
Adam Langley858a88d2014-06-20 12:00:00 -07001150 in_group_flags[num_in_group_flags++] = curr->in_group;
Adam Langley95c29f32014-06-20 12:00:00 -07001151#ifdef CIPHER_DEBUG
1152 printf("<%s>\n",curr->cipher->name);
1153#endif
1154 }
1155 }
1156 OPENSSL_free(co_list); /* Not needed any longer */
Adam Langley858a88d2014-06-20 12:00:00 -07001157 co_list = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001158
1159 tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
1160 if (tmp_cipher_list == NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001161 goto err;
1162 pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
1163 if (!pref_list)
1164 goto err;
1165 pref_list->ciphers = cipherstack;
1166 pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
1167 if (!pref_list->in_group_flags)
1168 goto err;
1169 memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
1170 OPENSSL_free(in_group_flags);
1171 in_group_flags = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001172 if (*cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001173 ssl_cipher_preference_list_free(*cipher_list);
1174 *cipher_list = pref_list;
1175 pref_list = NULL;
1176
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001177 if (cipher_list_by_id != NULL)
1178 {
1179 if (*cipher_list_by_id != NULL)
1180 sk_SSL_CIPHER_free(*cipher_list_by_id);
1181 *cipher_list_by_id = tmp_cipher_list;
Adam Langley858a88d2014-06-20 12:00:00 -07001182 tmp_cipher_list = NULL;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001183 (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001185 sk_SSL_CIPHER_sort(*cipher_list_by_id);
1186 }
1187 else
Adam Langley858a88d2014-06-20 12:00:00 -07001188 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001189 sk_SSL_CIPHER_free(tmp_cipher_list);
Adam Langley858a88d2014-06-20 12:00:00 -07001190 tmp_cipher_list = NULL;
1191 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001192
Adam Langley95c29f32014-06-20 12:00:00 -07001193 return(cipherstack);
Adam Langley858a88d2014-06-20 12:00:00 -07001194
1195err:
1196 if (co_list)
1197 OPENSSL_free(co_list);
1198 if (in_group_flags)
1199 OPENSSL_free(in_group_flags);
1200 if (cipherstack)
1201 sk_SSL_CIPHER_free(cipherstack);
1202 if (tmp_cipher_list)
1203 sk_SSL_CIPHER_free(tmp_cipher_list);
1204 if (pref_list && pref_list->in_group_flags)
1205 OPENSSL_free(pref_list->in_group_flags);
1206 if (pref_list)
1207 OPENSSL_free(pref_list);
1208 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001209 }
1210
Adam Langley73510762014-06-20 12:00:00 -07001211const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
Adam Langley95c29f32014-06-20 12:00:00 -07001212 {
David Benjamin060d9d22014-07-15 00:54:26 -04001213 const char *ver;
Adam Langley95c29f32014-06-20 12:00:00 -07001214 const char *kx,*au,*enc,*mac;
David Benjaminebf42b52014-08-19 15:18:42 -04001215 unsigned long alg_mkey,alg_auth,alg_enc,alg_mac,alg_ssl;
Adam Langley95c29f32014-06-20 12:00:00 -07001216#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001217 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 -07001218#else
David Benjamin060d9d22014-07-15 00:54:26 -04001219 static const char *format="%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
Adam Langley95c29f32014-06-20 12:00:00 -07001220#endif /* KSSL_DEBUG */
1221
1222 alg_mkey = cipher->algorithm_mkey;
1223 alg_auth = cipher->algorithm_auth;
1224 alg_enc = cipher->algorithm_enc;
1225 alg_mac = cipher->algorithm_mac;
1226 alg_ssl = cipher->algorithm_ssl;
1227
David Benjaminebf42b52014-08-19 15:18:42 -04001228 if (alg_ssl & SSL_SSLV3)
Adam Langley95c29f32014-06-20 12:00:00 -07001229 ver="SSLv3";
1230 else if (alg_ssl & SSL_TLSV1_2)
1231 ver="TLSv1.2";
1232 else
1233 ver="unknown";
1234
1235 switch (alg_mkey)
1236 {
1237 case SSL_kRSA:
David Benjamin060d9d22014-07-15 00:54:26 -04001238 kx="RSA";
Adam Langley95c29f32014-06-20 12:00:00 -07001239 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001240 case SSL_kEDH:
David Benjamin060d9d22014-07-15 00:54:26 -04001241 kx="DH";
Adam Langley95c29f32014-06-20 12:00:00 -07001242 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001243 case SSL_kEECDH:
1244 kx="ECDH";
1245 break;
1246 case SSL_kPSK:
1247 kx="PSK";
1248 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001249 default:
1250 kx="unknown";
1251 }
1252
1253 switch (alg_auth)
1254 {
1255 case SSL_aRSA:
1256 au="RSA";
1257 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001258 case SSL_aNULL:
1259 au="None";
1260 break;
1261 case SSL_aECDSA:
1262 au="ECDSA";
1263 break;
1264 case SSL_aPSK:
1265 au="PSK";
1266 break;
1267 default:
1268 au="unknown";
1269 break;
1270 }
1271
1272 switch (alg_enc)
1273 {
Adam Langley95c29f32014-06-20 12:00:00 -07001274 case SSL_3DES:
1275 enc="3DES(168)";
1276 break;
1277 case SSL_RC4:
David Benjaminebf42b52014-08-19 15:18:42 -04001278 enc="RC4(128)";
Adam Langley95c29f32014-06-20 12:00:00 -07001279 break;
1280 case SSL_AES128:
1281 enc="AES(128)";
1282 break;
1283 case SSL_AES256:
1284 enc="AES(256)";
1285 break;
1286 case SSL_AES128GCM:
1287 enc="AESGCM(128)";
1288 break;
1289 case SSL_AES256GCM:
1290 enc="AESGCM(256)";
1291 break;
Adam Langleyde0b2022014-06-20 12:00:00 -07001292 case SSL_CHACHA20POLY1305:
1293 enc="ChaCha20-Poly1305";
1294 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001295 default:
1296 enc="unknown";
1297 break;
1298 }
1299
1300 switch (alg_mac)
1301 {
1302 case SSL_MD5:
1303 mac="MD5";
1304 break;
1305 case SSL_SHA1:
1306 mac="SHA1";
1307 break;
1308 case SSL_SHA256:
1309 mac="SHA256";
1310 break;
1311 case SSL_SHA384:
1312 mac="SHA384";
1313 break;
1314 case SSL_AEAD:
1315 mac="AEAD";
1316 break;
1317 default:
1318 mac="unknown";
1319 break;
1320 }
1321
1322 if (buf == NULL)
1323 {
1324 len=128;
1325 buf=OPENSSL_malloc(len);
1326 if (buf == NULL) return("OPENSSL_malloc Error");
1327 }
1328 else if (len < 128)
1329 return("Buffer too small");
1330
1331#ifdef KSSL_DEBUG
David Benjamin060d9d22014-07-15 00:54:26 -04001332 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 -07001333#else
David Benjamin060d9d22014-07-15 00:54:26 -04001334 BIO_snprintf(buf,len,format,cipher->name,ver,kx,au,enc,mac);
Adam Langley95c29f32014-06-20 12:00:00 -07001335#endif /* KSSL_DEBUG */
1336 return(buf);
1337 }
1338
Adam Langley4d4bff82014-06-20 12:00:00 -07001339/* Next three functions require non-null cipher */
1340int SSL_CIPHER_is_AES(const SSL_CIPHER *c)
1341 {
1342 return (c->algorithm_enc & SSL_AES) != 0;
1343 }
1344
1345int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c)
1346 {
1347 return (c->algorithm_mac & SSL_MD5) != 0;
1348 }
1349
1350int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c)
1351 {
1352 return (c->algorithm_mac & (SSL_AES128GCM|SSL_AES256GCM)) != 0;
1353 }
1354
Adam Langleyde0b2022014-06-20 12:00:00 -07001355int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c)
1356 {
1357 return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
1358 }
1359
Adam Langley73510762014-06-20 12:00:00 -07001360const char *SSL_CIPHER_get_version(const SSL_CIPHER *c)
Adam Langley95c29f32014-06-20 12:00:00 -07001361 {
1362 int i;
1363
1364 if (c == NULL) return("(NONE)");
1365 i=(int)(c->id>>24L);
1366 if (i == 3)
1367 return("TLSv1/SSLv3");
1368 else if (i == 2)
1369 return("SSLv2");
1370 else
1371 return("unknown");
1372 }
1373
1374/* return the actual cipher being used */
1375const char *SSL_CIPHER_get_name(const SSL_CIPHER *c)
1376 {
1377 if (c != NULL)
1378 return(c->name);
1379 return("(NONE)");
1380 }
1381
Adam Langleyf1df2b32014-08-11 17:37:53 -07001382const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
1383 if (cipher == NULL) {
1384 return "";
1385 }
1386
1387 switch (cipher->algorithm_mkey) {
1388 case SSL_kRSA:
1389 return SSL_TXT_RSA;
Adam Langleyf1df2b32014-08-11 17:37:53 -07001390 case SSL_kEDH:
1391 switch (cipher->algorithm_auth) {
Adam Langleyf1df2b32014-08-11 17:37:53 -07001392 case SSL_aRSA:
1393 return "DHE_" SSL_TXT_RSA;
1394 case SSL_aNULL:
1395 return SSL_TXT_DH "_anon";
1396 default:
1397 return "UNKNOWN";
1398 }
Adam Langleyf1df2b32014-08-11 17:37:53 -07001399 case SSL_kEECDH:
1400 switch (cipher->algorithm_auth) {
1401 case SSL_aECDSA:
1402 return "ECDHE_" SSL_TXT_ECDSA;
1403 case SSL_aRSA:
1404 return "ECDHE_" SSL_TXT_RSA;
1405 case SSL_aNULL:
1406 return SSL_TXT_ECDH "_anon";
1407 default:
1408 return "UNKNOWN";
1409 }
1410 default:
1411 return "UNKNOWN";
1412 }
1413}
1414
Adam Langley95c29f32014-06-20 12:00:00 -07001415/* number of bits for symmetric cipher */
1416int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1417 {
1418 int ret=0;
1419
1420 if (c != NULL)
1421 {
1422 if (alg_bits != NULL) *alg_bits = c->alg_bits;
1423 ret = c->strength_bits;
1424 }
1425 return(ret);
1426 }
1427
1428unsigned long SSL_CIPHER_get_id(const SSL_CIPHER *c)
1429 {
1430 return c->id;
1431 }
1432
Adam Langley95c29f32014-06-20 12:00:00 -07001433void *SSL_COMP_get_compression_methods(void)
1434 {
1435 return NULL;
1436 }
1437int SSL_COMP_add_compression_method(int id, void *cm)
1438 {
1439 return 1;
1440 }
1441
1442const char *SSL_COMP_get_name(const void *comp)
1443 {
1444 return NULL;
1445 }
1446
1447/* For a cipher return the index corresponding to the certificate type */
1448int ssl_cipher_get_cert_index(const SSL_CIPHER *c)
1449 {
David Benjamin0da0e182014-08-19 16:20:28 -04001450 unsigned long alg_a = c->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001451
David Benjamin0da0e182014-08-19 16:20:28 -04001452 if (alg_a & SSL_aECDSA)
Adam Langley95c29f32014-06-20 12:00:00 -07001453 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07001454 else if (alg_a & SSL_aRSA)
1455 return SSL_PKEY_RSA_ENC;
Adam Langley95c29f32014-06-20 12:00:00 -07001456 return -1;
1457 }
1458
David Benjamine8f3d662014-07-12 01:10:19 -04001459/* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a
1460 * server public key in the key exchange, sent in a server Certificate
1461 * message. Otherwise it returns 0. */
1462int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher)
1463 {
1464 /* Anonymous ciphers do not include a server certificate. */
1465 if (cipher->algorithm_auth & SSL_aNULL)
1466 return 0;
1467 /* Neither do PSK ciphers, except for RSA_PSK. */
1468 if ((cipher->algorithm_auth & SSL_aPSK) &&
1469 !(cipher->algorithm_mkey & SSL_kRSA))
1470 return 0;
1471 /* All other ciphers include it. */
1472 return 1;
1473 }
David Benjamin9c651c92014-07-12 13:27:45 -04001474
1475/* ssl_cipher_requires_server_key_exchange returns 1 if |cipher|
1476 * requires a ServerKeyExchange message. Otherwise it returns 0.
1477 *
1478 * Unlike ssl_cipher_has_server_public_key, some ciphers take optional
1479 * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange
1480 * to communicate a psk_identity_hint, so it is optional.
1481 *
1482 * Also, as implemented, the RSA key exchange takes an optional
1483 * ServerKeyExchange containing a signed ephemeral RSA encryption key.
1484 *
1485 * TODO(davidben): Can we remove the RSA one? This is a remnant of
1486 * RSA_EXPORT ciphers which required this (it was used to generate an
1487 * ephemeral 512-bit RSA encryption key), but it's allowed for all RSA
David Benjaminb9cc33a2014-07-15 00:09:48 -04001488 * ciphers. */
David Benjamin9c651c92014-07-12 13:27:45 -04001489int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher)
1490 {
1491 /* Ephemeral Diffie-Hellman key exchanges require a
1492 * ServerKeyExchange. */
1493 if (cipher->algorithm_mkey & SSL_kEDH ||
1494 cipher->algorithm_mkey & SSL_kEECDH)
1495 return 1;
1496 /* It is optional in all others. */
1497 return 0;
1498 }