xref: /openbsd-src/lib/libssl/s3_lib.c (revision af97903fb555373f4af4abf3f3808148e699bb6f)
1 /* $OpenBSD: s3_lib.c,v 1.224 2022/01/08 12:43:44 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  *
114  * Portions of the attached software ("Contribution") are developed by
115  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116  *
117  * The Contribution is licensed pursuant to the OpenSSL open source
118  * license provided above.
119  *
120  * ECC cipher suite support in OpenSSL originally written by
121  * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122  *
123  */
124 /* ====================================================================
125  * Copyright 2005 Nokia. All rights reserved.
126  *
127  * The portions of the attached software ("Contribution") is developed by
128  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129  * license.
130  *
131  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133  * support (see RFC 4279) to OpenSSL.
134  *
135  * No patent licenses or other rights except those expressly stated in
136  * the OpenSSL open source license shall be deemed granted or received
137  * expressly, by implication, estoppel, or otherwise.
138  *
139  * No assurances are provided by Nokia that the Contribution does not
140  * infringe the patent or other intellectual property rights of any third
141  * party or that the license provides you with all the necessary rights
142  * to make use of the Contribution.
143  *
144  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148  * OTHERWISE.
149  */
150 
151 #include <limits.h>
152 #include <stdio.h>
153 
154 #include <openssl/bn.h>
155 #include <openssl/curve25519.h>
156 #include <openssl/dh.h>
157 #include <openssl/md5.h>
158 #include <openssl/objects.h>
159 #include <openssl/opensslconf.h>
160 
161 #include "bytestring.h"
162 #include "dtls_locl.h"
163 #include "ssl_locl.h"
164 #include "ssl_sigalgs.h"
165 #include "ssl_tlsext.h"
166 
167 #define SSL3_NUM_CIPHERS	(sizeof(ssl3_ciphers) / sizeof(SSL_CIPHER))
168 
169 /*
170  * FIXED_NONCE_LEN is a macro that provides in the correct value to set the
171  * fixed nonce length in algorithms2. It is the inverse of the
172  * SSL_CIPHER_AEAD_FIXED_NONCE_LEN macro.
173  */
174 #define FIXED_NONCE_LEN(x) (((x / 2) & 0xf) << 24)
175 
176 /* list of available SSLv3 ciphers (sorted by id) */
177 const SSL_CIPHER ssl3_ciphers[] = {
178 
179 	/* The RSA ciphers */
180 	/* Cipher 01 */
181 	{
182 		.valid = 1,
183 		.name = SSL3_TXT_RSA_NULL_MD5,
184 		.id = SSL3_CK_RSA_NULL_MD5,
185 		.algorithm_mkey = SSL_kRSA,
186 		.algorithm_auth = SSL_aRSA,
187 		.algorithm_enc = SSL_eNULL,
188 		.algorithm_mac = SSL_MD5,
189 		.algorithm_ssl = SSL_SSLV3,
190 		.algo_strength = SSL_STRONG_NONE,
191 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
192 		.strength_bits = 0,
193 		.alg_bits = 0,
194 	},
195 
196 	/* Cipher 02 */
197 	{
198 		.valid = 1,
199 		.name = SSL3_TXT_RSA_NULL_SHA,
200 		.id = SSL3_CK_RSA_NULL_SHA,
201 		.algorithm_mkey = SSL_kRSA,
202 		.algorithm_auth = SSL_aRSA,
203 		.algorithm_enc = SSL_eNULL,
204 		.algorithm_mac = SSL_SHA1,
205 		.algorithm_ssl = SSL_SSLV3,
206 		.algo_strength = SSL_STRONG_NONE,
207 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
208 		.strength_bits = 0,
209 		.alg_bits = 0,
210 	},
211 
212 	/* Cipher 04 */
213 	{
214 		.valid = 1,
215 		.name = SSL3_TXT_RSA_RC4_128_MD5,
216 		.id = SSL3_CK_RSA_RC4_128_MD5,
217 		.algorithm_mkey = SSL_kRSA,
218 		.algorithm_auth = SSL_aRSA,
219 		.algorithm_enc = SSL_RC4,
220 		.algorithm_mac = SSL_MD5,
221 		.algorithm_ssl = SSL_SSLV3,
222 		.algo_strength = SSL_LOW,
223 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
224 		.strength_bits = 128,
225 		.alg_bits = 128,
226 	},
227 
228 	/* Cipher 05 */
229 	{
230 		.valid = 1,
231 		.name = SSL3_TXT_RSA_RC4_128_SHA,
232 		.id = SSL3_CK_RSA_RC4_128_SHA,
233 		.algorithm_mkey = SSL_kRSA,
234 		.algorithm_auth = SSL_aRSA,
235 		.algorithm_enc = SSL_RC4,
236 		.algorithm_mac = SSL_SHA1,
237 		.algorithm_ssl = SSL_SSLV3,
238 		.algo_strength = SSL_LOW,
239 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
240 		.strength_bits = 128,
241 		.alg_bits = 128,
242 	},
243 
244 	/* Cipher 0A */
245 	{
246 		.valid = 1,
247 		.name = SSL3_TXT_RSA_DES_192_CBC3_SHA,
248 		.id = SSL3_CK_RSA_DES_192_CBC3_SHA,
249 		.algorithm_mkey = SSL_kRSA,
250 		.algorithm_auth = SSL_aRSA,
251 		.algorithm_enc = SSL_3DES,
252 		.algorithm_mac = SSL_SHA1,
253 		.algorithm_ssl = SSL_SSLV3,
254 		.algo_strength = SSL_MEDIUM,
255 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
256 		.strength_bits = 112,
257 		.alg_bits = 168,
258 	},
259 
260 	/*
261 	 * Ephemeral DH (DHE) ciphers.
262 	 */
263 
264 	/* Cipher 16 */
265 	{
266 		.valid = 1,
267 		.name = SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
268 		.id = SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
269 		.algorithm_mkey = SSL_kDHE,
270 		.algorithm_auth = SSL_aRSA,
271 		.algorithm_enc = SSL_3DES,
272 		.algorithm_mac = SSL_SHA1,
273 		.algorithm_ssl = SSL_SSLV3,
274 		.algo_strength = SSL_MEDIUM,
275 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
276 		.strength_bits = 112,
277 		.alg_bits = 168,
278 	},
279 
280 	/* Cipher 18 */
281 	{
282 		.valid = 1,
283 		.name = SSL3_TXT_ADH_RC4_128_MD5,
284 		.id = SSL3_CK_ADH_RC4_128_MD5,
285 		.algorithm_mkey = SSL_kDHE,
286 		.algorithm_auth = SSL_aNULL,
287 		.algorithm_enc = SSL_RC4,
288 		.algorithm_mac = SSL_MD5,
289 		.algorithm_ssl = SSL_SSLV3,
290 		.algo_strength = SSL_LOW,
291 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
292 		.strength_bits = 128,
293 		.alg_bits = 128,
294 	},
295 
296 	/* Cipher 1B */
297 	{
298 		.valid = 1,
299 		.name = SSL3_TXT_ADH_DES_192_CBC_SHA,
300 		.id = SSL3_CK_ADH_DES_192_CBC_SHA,
301 		.algorithm_mkey = SSL_kDHE,
302 		.algorithm_auth = SSL_aNULL,
303 		.algorithm_enc = SSL_3DES,
304 		.algorithm_mac = SSL_SHA1,
305 		.algorithm_ssl = SSL_SSLV3,
306 		.algo_strength = SSL_MEDIUM,
307 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
308 		.strength_bits = 112,
309 		.alg_bits = 168,
310 	},
311 
312 	/*
313 	 * AES ciphersuites.
314 	 */
315 
316 	/* Cipher 2F */
317 	{
318 		.valid = 1,
319 		.name = TLS1_TXT_RSA_WITH_AES_128_SHA,
320 		.id = TLS1_CK_RSA_WITH_AES_128_SHA,
321 		.algorithm_mkey = SSL_kRSA,
322 		.algorithm_auth = SSL_aRSA,
323 		.algorithm_enc = SSL_AES128,
324 		.algorithm_mac = SSL_SHA1,
325 		.algorithm_ssl = SSL_TLSV1,
326 		.algo_strength = SSL_HIGH,
327 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
328 		.strength_bits = 128,
329 		.alg_bits = 128,
330 	},
331 
332 	/* Cipher 33 */
333 	{
334 		.valid = 1,
335 		.name = TLS1_TXT_DHE_RSA_WITH_AES_128_SHA,
336 		.id = TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
337 		.algorithm_mkey = SSL_kDHE,
338 		.algorithm_auth = SSL_aRSA,
339 		.algorithm_enc = SSL_AES128,
340 		.algorithm_mac = SSL_SHA1,
341 		.algorithm_ssl = SSL_TLSV1,
342 		.algo_strength = SSL_HIGH,
343 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
344 		.strength_bits = 128,
345 		.alg_bits = 128,
346 	},
347 
348 	/* Cipher 34 */
349 	{
350 		.valid = 1,
351 		.name = TLS1_TXT_ADH_WITH_AES_128_SHA,
352 		.id = TLS1_CK_ADH_WITH_AES_128_SHA,
353 		.algorithm_mkey = SSL_kDHE,
354 		.algorithm_auth = SSL_aNULL,
355 		.algorithm_enc = SSL_AES128,
356 		.algorithm_mac = SSL_SHA1,
357 		.algorithm_ssl = SSL_TLSV1,
358 		.algo_strength = SSL_HIGH,
359 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
360 		.strength_bits = 128,
361 		.alg_bits = 128,
362 	},
363 
364 	/* Cipher 35 */
365 	{
366 		.valid = 1,
367 		.name = TLS1_TXT_RSA_WITH_AES_256_SHA,
368 		.id = TLS1_CK_RSA_WITH_AES_256_SHA,
369 		.algorithm_mkey = SSL_kRSA,
370 		.algorithm_auth = SSL_aRSA,
371 		.algorithm_enc = SSL_AES256,
372 		.algorithm_mac = SSL_SHA1,
373 		.algorithm_ssl = SSL_TLSV1,
374 		.algo_strength = SSL_HIGH,
375 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
376 		.strength_bits = 256,
377 		.alg_bits = 256,
378 	},
379 
380 	/* Cipher 39 */
381 	{
382 		.valid = 1,
383 		.name = TLS1_TXT_DHE_RSA_WITH_AES_256_SHA,
384 		.id = TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
385 		.algorithm_mkey = SSL_kDHE,
386 		.algorithm_auth = SSL_aRSA,
387 		.algorithm_enc = SSL_AES256,
388 		.algorithm_mac = SSL_SHA1,
389 		.algorithm_ssl = SSL_TLSV1,
390 		.algo_strength = SSL_HIGH,
391 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
392 		.strength_bits = 256,
393 		.alg_bits = 256,
394 	},
395 
396 	/* Cipher 3A */
397 	{
398 		.valid = 1,
399 		.name = TLS1_TXT_ADH_WITH_AES_256_SHA,
400 		.id = TLS1_CK_ADH_WITH_AES_256_SHA,
401 		.algorithm_mkey = SSL_kDHE,
402 		.algorithm_auth = SSL_aNULL,
403 		.algorithm_enc = SSL_AES256,
404 		.algorithm_mac = SSL_SHA1,
405 		.algorithm_ssl = SSL_TLSV1,
406 		.algo_strength = SSL_HIGH,
407 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
408 		.strength_bits = 256,
409 		.alg_bits = 256,
410 	},
411 
412 	/* TLS v1.2 ciphersuites */
413 	/* Cipher 3B */
414 	{
415 		.valid = 1,
416 		.name = TLS1_TXT_RSA_WITH_NULL_SHA256,
417 		.id = TLS1_CK_RSA_WITH_NULL_SHA256,
418 		.algorithm_mkey = SSL_kRSA,
419 		.algorithm_auth = SSL_aRSA,
420 		.algorithm_enc = SSL_eNULL,
421 		.algorithm_mac = SSL_SHA256,
422 		.algorithm_ssl = SSL_TLSV1_2,
423 		.algo_strength = SSL_STRONG_NONE,
424 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
425 		.strength_bits = 0,
426 		.alg_bits = 0,
427 	},
428 
429 	/* Cipher 3C */
430 	{
431 		.valid = 1,
432 		.name = TLS1_TXT_RSA_WITH_AES_128_SHA256,
433 		.id = TLS1_CK_RSA_WITH_AES_128_SHA256,
434 		.algorithm_mkey = SSL_kRSA,
435 		.algorithm_auth = SSL_aRSA,
436 		.algorithm_enc = SSL_AES128,
437 		.algorithm_mac = SSL_SHA256,
438 		.algorithm_ssl = SSL_TLSV1_2,
439 		.algo_strength = SSL_HIGH,
440 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
441 		.strength_bits = 128,
442 		.alg_bits = 128,
443 	},
444 
445 	/* Cipher 3D */
446 	{
447 		.valid = 1,
448 		.name = TLS1_TXT_RSA_WITH_AES_256_SHA256,
449 		.id = TLS1_CK_RSA_WITH_AES_256_SHA256,
450 		.algorithm_mkey = SSL_kRSA,
451 		.algorithm_auth = SSL_aRSA,
452 		.algorithm_enc = SSL_AES256,
453 		.algorithm_mac = SSL_SHA256,
454 		.algorithm_ssl = SSL_TLSV1_2,
455 		.algo_strength = SSL_HIGH,
456 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
457 		.strength_bits = 256,
458 		.alg_bits = 256,
459 	},
460 
461 #ifndef OPENSSL_NO_CAMELLIA
462 	/* Camellia ciphersuites from RFC4132 (128-bit portion) */
463 
464 	/* Cipher 41 */
465 	{
466 		.valid = 1,
467 		.name = TLS1_TXT_RSA_WITH_CAMELLIA_128_CBC_SHA,
468 		.id = TLS1_CK_RSA_WITH_CAMELLIA_128_CBC_SHA,
469 		.algorithm_mkey = SSL_kRSA,
470 		.algorithm_auth = SSL_aRSA,
471 		.algorithm_enc = SSL_CAMELLIA128,
472 		.algorithm_mac = SSL_SHA1,
473 		.algorithm_ssl = SSL_TLSV1,
474 		.algo_strength = SSL_HIGH,
475 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
476 		.strength_bits = 128,
477 		.alg_bits = 128,
478 	},
479 
480 	/* Cipher 45 */
481 	{
482 		.valid = 1,
483 		.name = TLS1_TXT_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
484 		.id = TLS1_CK_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
485 		.algorithm_mkey = SSL_kDHE,
486 		.algorithm_auth = SSL_aRSA,
487 		.algorithm_enc = SSL_CAMELLIA128,
488 		.algorithm_mac = SSL_SHA1,
489 		.algorithm_ssl = SSL_TLSV1,
490 		.algo_strength = SSL_HIGH,
491 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
492 		.strength_bits = 128,
493 		.alg_bits = 128,
494 	},
495 
496 	/* Cipher 46 */
497 	{
498 		.valid = 1,
499 		.name = TLS1_TXT_ADH_WITH_CAMELLIA_128_CBC_SHA,
500 		.id = TLS1_CK_ADH_WITH_CAMELLIA_128_CBC_SHA,
501 		.algorithm_mkey = SSL_kDHE,
502 		.algorithm_auth = SSL_aNULL,
503 		.algorithm_enc = SSL_CAMELLIA128,
504 		.algorithm_mac = SSL_SHA1,
505 		.algorithm_ssl = SSL_TLSV1,
506 		.algo_strength = SSL_HIGH,
507 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
508 		.strength_bits = 128,
509 		.alg_bits = 128,
510 	},
511 #endif /* OPENSSL_NO_CAMELLIA */
512 
513 	/* TLS v1.2 ciphersuites */
514 	/* Cipher 67 */
515 	{
516 		.valid = 1,
517 		.name = TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
518 		.id = TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
519 		.algorithm_mkey = SSL_kDHE,
520 		.algorithm_auth = SSL_aRSA,
521 		.algorithm_enc = SSL_AES128,
522 		.algorithm_mac = SSL_SHA256,
523 		.algorithm_ssl = SSL_TLSV1_2,
524 		.algo_strength = SSL_HIGH,
525 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
526 		.strength_bits = 128,
527 		.alg_bits = 128,
528 	},
529 
530 	/* Cipher 6B */
531 	{
532 		.valid = 1,
533 		.name = TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
534 		.id = TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
535 		.algorithm_mkey = SSL_kDHE,
536 		.algorithm_auth = SSL_aRSA,
537 		.algorithm_enc = SSL_AES256,
538 		.algorithm_mac = SSL_SHA256,
539 		.algorithm_ssl = SSL_TLSV1_2,
540 		.algo_strength = SSL_HIGH,
541 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
542 		.strength_bits = 256,
543 		.alg_bits = 256,
544 	},
545 
546 	/* Cipher 6C */
547 	{
548 		.valid = 1,
549 		.name = TLS1_TXT_ADH_WITH_AES_128_SHA256,
550 		.id = TLS1_CK_ADH_WITH_AES_128_SHA256,
551 		.algorithm_mkey = SSL_kDHE,
552 		.algorithm_auth = SSL_aNULL,
553 		.algorithm_enc = SSL_AES128,
554 		.algorithm_mac = SSL_SHA256,
555 		.algorithm_ssl = SSL_TLSV1_2,
556 		.algo_strength = SSL_HIGH,
557 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
558 		.strength_bits = 128,
559 		.alg_bits = 128,
560 	},
561 
562 	/* Cipher 6D */
563 	{
564 		.valid = 1,
565 		.name = TLS1_TXT_ADH_WITH_AES_256_SHA256,
566 		.id = TLS1_CK_ADH_WITH_AES_256_SHA256,
567 		.algorithm_mkey = SSL_kDHE,
568 		.algorithm_auth = SSL_aNULL,
569 		.algorithm_enc = SSL_AES256,
570 		.algorithm_mac = SSL_SHA256,
571 		.algorithm_ssl = SSL_TLSV1_2,
572 		.algo_strength = SSL_HIGH,
573 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
574 		.strength_bits = 256,
575 		.alg_bits = 256,
576 	},
577 
578 	/* GOST Ciphersuites */
579 
580 	/* Cipher 81 */
581 	{
582 		.valid = 1,
583 		.name = "GOST2001-GOST89-GOST89",
584 		.id = 0x3000081,
585 		.algorithm_mkey = SSL_kGOST,
586 		.algorithm_auth = SSL_aGOST01,
587 		.algorithm_enc = SSL_eGOST2814789CNT,
588 		.algorithm_mac = SSL_GOST89MAC,
589 		.algorithm_ssl = SSL_TLSV1,
590 		.algo_strength = SSL_HIGH,
591 		.algorithm2 = SSL_HANDSHAKE_MAC_GOST94|TLS1_PRF_GOST94|
592 		    TLS1_STREAM_MAC,
593 		.strength_bits = 256,
594 		.alg_bits = 256
595 	},
596 
597 	/* Cipher 83 */
598 	{
599 		.valid = 1,
600 		.name = "GOST2001-NULL-GOST94",
601 		.id = 0x3000083,
602 		.algorithm_mkey = SSL_kGOST,
603 		.algorithm_auth = SSL_aGOST01,
604 		.algorithm_enc = SSL_eNULL,
605 		.algorithm_mac = SSL_GOST94,
606 		.algorithm_ssl = SSL_TLSV1,
607 		.algo_strength = SSL_STRONG_NONE,
608 		.algorithm2 = SSL_HANDSHAKE_MAC_GOST94|TLS1_PRF_GOST94,
609 		.strength_bits = 0,
610 		.alg_bits = 0
611 	},
612 
613 #ifndef OPENSSL_NO_CAMELLIA
614 	/* Camellia ciphersuites from RFC4132 (256-bit portion) */
615 
616 	/* Cipher 84 */
617 	{
618 		.valid = 1,
619 		.name = TLS1_TXT_RSA_WITH_CAMELLIA_256_CBC_SHA,
620 		.id = TLS1_CK_RSA_WITH_CAMELLIA_256_CBC_SHA,
621 		.algorithm_mkey = SSL_kRSA,
622 		.algorithm_auth = SSL_aRSA,
623 		.algorithm_enc = SSL_CAMELLIA256,
624 		.algorithm_mac = SSL_SHA1,
625 		.algorithm_ssl = SSL_TLSV1,
626 		.algo_strength = SSL_HIGH,
627 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
628 		.strength_bits = 256,
629 		.alg_bits = 256,
630 	},
631 
632 	/* Cipher 88 */
633 	{
634 		.valid = 1,
635 		.name = TLS1_TXT_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
636 		.id = TLS1_CK_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
637 		.algorithm_mkey = SSL_kDHE,
638 		.algorithm_auth = SSL_aRSA,
639 		.algorithm_enc = SSL_CAMELLIA256,
640 		.algorithm_mac = SSL_SHA1,
641 		.algorithm_ssl = SSL_TLSV1,
642 		.algo_strength = SSL_HIGH,
643 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
644 		.strength_bits = 256,
645 		.alg_bits = 256,
646 	},
647 
648 	/* Cipher 89 */
649 	{
650 		.valid = 1,
651 		.name = TLS1_TXT_ADH_WITH_CAMELLIA_256_CBC_SHA,
652 		.id = TLS1_CK_ADH_WITH_CAMELLIA_256_CBC_SHA,
653 		.algorithm_mkey = SSL_kDHE,
654 		.algorithm_auth = SSL_aNULL,
655 		.algorithm_enc = SSL_CAMELLIA256,
656 		.algorithm_mac = SSL_SHA1,
657 		.algorithm_ssl = SSL_TLSV1,
658 		.algo_strength = SSL_HIGH,
659 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
660 		.strength_bits = 256,
661 		.alg_bits = 256,
662 	},
663 #endif /* OPENSSL_NO_CAMELLIA */
664 
665 	/*
666 	 * GCM ciphersuites from RFC5288.
667 	 */
668 
669 	/* Cipher 9C */
670 	{
671 		.valid = 1,
672 		.name = TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
673 		.id = TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
674 		.algorithm_mkey = SSL_kRSA,
675 		.algorithm_auth = SSL_aRSA,
676 		.algorithm_enc = SSL_AES128GCM,
677 		.algorithm_mac = SSL_AEAD,
678 		.algorithm_ssl = SSL_TLSV1_2,
679 		.algo_strength = SSL_HIGH,
680 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
681 		    FIXED_NONCE_LEN(4)|
682 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
683 		.strength_bits = 128,
684 		.alg_bits = 128,
685 	},
686 
687 	/* Cipher 9D */
688 	{
689 		.valid = 1,
690 		.name = TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
691 		.id = TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
692 		.algorithm_mkey = SSL_kRSA,
693 		.algorithm_auth = SSL_aRSA,
694 		.algorithm_enc = SSL_AES256GCM,
695 		.algorithm_mac = SSL_AEAD,
696 		.algorithm_ssl = SSL_TLSV1_2,
697 		.algo_strength = SSL_HIGH,
698 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|
699 		    FIXED_NONCE_LEN(4)|
700 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
701 		.strength_bits = 256,
702 		.alg_bits = 256,
703 	},
704 
705 	/* Cipher 9E */
706 	{
707 		.valid = 1,
708 		.name = TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
709 		.id = TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
710 		.algorithm_mkey = SSL_kDHE,
711 		.algorithm_auth = SSL_aRSA,
712 		.algorithm_enc = SSL_AES128GCM,
713 		.algorithm_mac = SSL_AEAD,
714 		.algorithm_ssl = SSL_TLSV1_2,
715 		.algo_strength = SSL_HIGH,
716 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
717 		    FIXED_NONCE_LEN(4)|
718 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
719 		.strength_bits = 128,
720 		.alg_bits = 128,
721 	},
722 
723 	/* Cipher 9F */
724 	{
725 		.valid = 1,
726 		.name = TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
727 		.id = TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
728 		.algorithm_mkey = SSL_kDHE,
729 		.algorithm_auth = SSL_aRSA,
730 		.algorithm_enc = SSL_AES256GCM,
731 		.algorithm_mac = SSL_AEAD,
732 		.algorithm_ssl = SSL_TLSV1_2,
733 		.algo_strength = SSL_HIGH,
734 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|
735 		    FIXED_NONCE_LEN(4)|
736 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
737 		.strength_bits = 256,
738 		.alg_bits = 256,
739 	},
740 
741 	/* Cipher A6 */
742 	{
743 		.valid = 1,
744 		.name = TLS1_TXT_ADH_WITH_AES_128_GCM_SHA256,
745 		.id = TLS1_CK_ADH_WITH_AES_128_GCM_SHA256,
746 		.algorithm_mkey = SSL_kDHE,
747 		.algorithm_auth = SSL_aNULL,
748 		.algorithm_enc = SSL_AES128GCM,
749 		.algorithm_mac = SSL_AEAD,
750 		.algorithm_ssl = SSL_TLSV1_2,
751 		.algo_strength = SSL_HIGH,
752 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
753 		    FIXED_NONCE_LEN(4)|
754 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
755 		.strength_bits = 128,
756 		.alg_bits = 128,
757 	},
758 
759 	/* Cipher A7 */
760 	{
761 		.valid = 1,
762 		.name = TLS1_TXT_ADH_WITH_AES_256_GCM_SHA384,
763 		.id = TLS1_CK_ADH_WITH_AES_256_GCM_SHA384,
764 		.algorithm_mkey = SSL_kDHE,
765 		.algorithm_auth = SSL_aNULL,
766 		.algorithm_enc = SSL_AES256GCM,
767 		.algorithm_mac = SSL_AEAD,
768 		.algorithm_ssl = SSL_TLSV1_2,
769 		.algo_strength = SSL_HIGH,
770 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|
771 		    FIXED_NONCE_LEN(4)|
772 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
773 		.strength_bits = 256,
774 		.alg_bits = 256,
775 	},
776 
777 #ifndef OPENSSL_NO_CAMELLIA
778 	/* TLS 1.2 Camellia SHA-256 ciphersuites from RFC5932 */
779 
780 	/* Cipher BA */
781 	{
782 		.valid = 1,
783 		.name = TLS1_TXT_RSA_WITH_CAMELLIA_128_CBC_SHA256,
784 		.id = TLS1_CK_RSA_WITH_CAMELLIA_128_CBC_SHA256,
785 		.algorithm_mkey = SSL_kRSA,
786 		.algorithm_auth = SSL_aRSA,
787 		.algorithm_enc = SSL_CAMELLIA128,
788 		.algorithm_mac = SSL_SHA256,
789 		.algorithm_ssl = SSL_TLSV1_2,
790 		.algo_strength = SSL_HIGH,
791 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
792 		.strength_bits = 128,
793 		.alg_bits = 128,
794 	},
795 
796 	/* Cipher BE */
797 	{
798 		.valid = 1,
799 		.name = TLS1_TXT_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
800 		.id = TLS1_CK_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
801 		.algorithm_mkey = SSL_kDHE,
802 		.algorithm_auth = SSL_aRSA,
803 		.algorithm_enc = SSL_CAMELLIA128,
804 		.algorithm_mac = SSL_SHA256,
805 		.algorithm_ssl = SSL_TLSV1_2,
806 		.algo_strength = SSL_HIGH,
807 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
808 		.strength_bits = 128,
809 		.alg_bits = 128,
810 	},
811 
812 	/* Cipher BF */
813 	{
814 		.valid = 1,
815 		.name = TLS1_TXT_ADH_WITH_CAMELLIA_128_CBC_SHA256,
816 		.id = TLS1_CK_ADH_WITH_CAMELLIA_128_CBC_SHA256,
817 		.algorithm_mkey = SSL_kDHE,
818 		.algorithm_auth = SSL_aNULL,
819 		.algorithm_enc = SSL_CAMELLIA128,
820 		.algorithm_mac = SSL_SHA256,
821 		.algorithm_ssl = SSL_TLSV1_2,
822 		.algo_strength = SSL_HIGH,
823 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
824 		.strength_bits = 128,
825 		.alg_bits = 128,
826 	},
827 
828 	/* Cipher C0 */
829 	{
830 		.valid = 1,
831 		.name = TLS1_TXT_RSA_WITH_CAMELLIA_256_CBC_SHA256,
832 		.id = TLS1_CK_RSA_WITH_CAMELLIA_256_CBC_SHA256,
833 		.algorithm_mkey = SSL_kRSA,
834 		.algorithm_auth = SSL_aRSA,
835 		.algorithm_enc = SSL_CAMELLIA256,
836 		.algorithm_mac = SSL_SHA256,
837 		.algorithm_ssl = SSL_TLSV1_2,
838 		.algo_strength = SSL_HIGH,
839 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
840 		.strength_bits = 256,
841 		.alg_bits = 256,
842 	},
843 
844 	/* Cipher C4 */
845 	{
846 		.valid = 1,
847 		.name = TLS1_TXT_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
848 		.id = TLS1_CK_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
849 		.algorithm_mkey = SSL_kDHE,
850 		.algorithm_auth = SSL_aRSA,
851 		.algorithm_enc = SSL_CAMELLIA256,
852 		.algorithm_mac = SSL_SHA256,
853 		.algorithm_ssl = SSL_TLSV1_2,
854 		.algo_strength = SSL_HIGH,
855 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
856 		.strength_bits = 256,
857 		.alg_bits = 256,
858 	},
859 
860 	/* Cipher C5 */
861 	{
862 		.valid = 1,
863 		.name = TLS1_TXT_ADH_WITH_CAMELLIA_256_CBC_SHA256,
864 		.id = TLS1_CK_ADH_WITH_CAMELLIA_256_CBC_SHA256,
865 		.algorithm_mkey = SSL_kDHE,
866 		.algorithm_auth = SSL_aNULL,
867 		.algorithm_enc = SSL_CAMELLIA256,
868 		.algorithm_mac = SSL_SHA256,
869 		.algorithm_ssl = SSL_TLSV1_2,
870 		.algo_strength = SSL_HIGH,
871 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
872 		.strength_bits = 256,
873 		.alg_bits = 256,
874 	},
875 #endif /* OPENSSL_NO_CAMELLIA */
876 
877 	/*
878 	 * TLSv1.3 cipher suites.
879 	 */
880 
881 #ifdef LIBRESSL_HAS_TLS1_3
882 	/* Cipher 1301 */
883 	{
884 		.valid = 1,
885 		.name = TLS1_3_TXT_AES_128_GCM_SHA256,
886 		.id = TLS1_3_CK_AES_128_GCM_SHA256,
887 		.algorithm_mkey = SSL_kTLS1_3,
888 		.algorithm_auth = SSL_aTLS1_3,
889 		.algorithm_enc = SSL_AES128GCM,
890 		.algorithm_mac = SSL_AEAD,
891 		.algorithm_ssl = SSL_TLSV1_3,
892 		.algo_strength = SSL_HIGH,
893 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256, /* XXX */
894 		.strength_bits = 128,
895 		.alg_bits = 128,
896 	},
897 
898 	/* Cipher 1302 */
899 	{
900 		.valid = 1,
901 		.name = TLS1_3_TXT_AES_256_GCM_SHA384,
902 		.id = TLS1_3_CK_AES_256_GCM_SHA384,
903 		.algorithm_mkey = SSL_kTLS1_3,
904 		.algorithm_auth = SSL_aTLS1_3,
905 		.algorithm_enc = SSL_AES256GCM,
906 		.algorithm_mac = SSL_AEAD,
907 		.algorithm_ssl = SSL_TLSV1_3,
908 		.algo_strength = SSL_HIGH,
909 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384, /* XXX */
910 		.strength_bits = 256,
911 		.alg_bits = 256,
912 	},
913 
914 	/* Cipher 1303 */
915 	{
916 		.valid = 1,
917 		.name = TLS1_3_TXT_CHACHA20_POLY1305_SHA256,
918 		.id = TLS1_3_CK_CHACHA20_POLY1305_SHA256,
919 		.algorithm_mkey = SSL_kTLS1_3,
920 		.algorithm_auth = SSL_aTLS1_3,
921 		.algorithm_enc = SSL_CHACHA20POLY1305,
922 		.algorithm_mac = SSL_AEAD,
923 		.algorithm_ssl = SSL_TLSV1_3,
924 		.algo_strength = SSL_HIGH,
925 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256, /* XXX */
926 		.strength_bits = 256,
927 		.alg_bits = 256,
928 	},
929 #endif
930 
931 	/* Cipher C006 */
932 	{
933 		.valid = 1,
934 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_NULL_SHA,
935 		.id = TLS1_CK_ECDHE_ECDSA_WITH_NULL_SHA,
936 		.algorithm_mkey = SSL_kECDHE,
937 		.algorithm_auth = SSL_aECDSA,
938 		.algorithm_enc = SSL_eNULL,
939 		.algorithm_mac = SSL_SHA1,
940 		.algorithm_ssl = SSL_TLSV1,
941 		.algo_strength = SSL_STRONG_NONE,
942 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
943 		.strength_bits = 0,
944 		.alg_bits = 0,
945 	},
946 
947 	/* Cipher C007 */
948 	{
949 		.valid = 1,
950 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
951 		.id = TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA,
952 		.algorithm_mkey = SSL_kECDHE,
953 		.algorithm_auth = SSL_aECDSA,
954 		.algorithm_enc = SSL_RC4,
955 		.algorithm_mac = SSL_SHA1,
956 		.algorithm_ssl = SSL_TLSV1,
957 		.algo_strength = SSL_LOW,
958 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
959 		.strength_bits = 128,
960 		.alg_bits = 128,
961 	},
962 
963 	/* Cipher C008 */
964 	{
965 		.valid = 1,
966 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA,
967 		.id = TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA,
968 		.algorithm_mkey = SSL_kECDHE,
969 		.algorithm_auth = SSL_aECDSA,
970 		.algorithm_enc = SSL_3DES,
971 		.algorithm_mac = SSL_SHA1,
972 		.algorithm_ssl = SSL_TLSV1,
973 		.algo_strength = SSL_MEDIUM,
974 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
975 		.strength_bits = 112,
976 		.alg_bits = 168,
977 	},
978 
979 	/* Cipher C009 */
980 	{
981 		.valid = 1,
982 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
983 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
984 		.algorithm_mkey = SSL_kECDHE,
985 		.algorithm_auth = SSL_aECDSA,
986 		.algorithm_enc = SSL_AES128,
987 		.algorithm_mac = SSL_SHA1,
988 		.algorithm_ssl = SSL_TLSV1,
989 		.algo_strength = SSL_HIGH,
990 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
991 		.strength_bits = 128,
992 		.alg_bits = 128,
993 	},
994 
995 	/* Cipher C00A */
996 	{
997 		.valid = 1,
998 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
999 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
1000 		.algorithm_mkey = SSL_kECDHE,
1001 		.algorithm_auth = SSL_aECDSA,
1002 		.algorithm_enc = SSL_AES256,
1003 		.algorithm_mac = SSL_SHA1,
1004 		.algorithm_ssl = SSL_TLSV1,
1005 		.algo_strength = SSL_HIGH,
1006 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1007 		.strength_bits = 256,
1008 		.alg_bits = 256,
1009 	},
1010 
1011 	/* Cipher C010 */
1012 	{
1013 		.valid = 1,
1014 		.name = TLS1_TXT_ECDHE_RSA_WITH_NULL_SHA,
1015 		.id = TLS1_CK_ECDHE_RSA_WITH_NULL_SHA,
1016 		.algorithm_mkey = SSL_kECDHE,
1017 		.algorithm_auth = SSL_aRSA,
1018 		.algorithm_enc = SSL_eNULL,
1019 		.algorithm_mac = SSL_SHA1,
1020 		.algorithm_ssl = SSL_TLSV1,
1021 		.algo_strength = SSL_STRONG_NONE,
1022 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1023 		.strength_bits = 0,
1024 		.alg_bits = 0,
1025 	},
1026 
1027 	/* Cipher C011 */
1028 	{
1029 		.valid = 1,
1030 		.name = TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA,
1031 		.id = TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
1032 		.algorithm_mkey = SSL_kECDHE,
1033 		.algorithm_auth = SSL_aRSA,
1034 		.algorithm_enc = SSL_RC4,
1035 		.algorithm_mac = SSL_SHA1,
1036 		.algorithm_ssl = SSL_TLSV1,
1037 		.algo_strength = SSL_LOW,
1038 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1039 		.strength_bits = 128,
1040 		.alg_bits = 128,
1041 	},
1042 
1043 	/* Cipher C012 */
1044 	{
1045 		.valid = 1,
1046 		.name = TLS1_TXT_ECDHE_RSA_WITH_DES_192_CBC3_SHA,
1047 		.id = TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA,
1048 		.algorithm_mkey = SSL_kECDHE,
1049 		.algorithm_auth = SSL_aRSA,
1050 		.algorithm_enc = SSL_3DES,
1051 		.algorithm_mac = SSL_SHA1,
1052 		.algorithm_ssl = SSL_TLSV1,
1053 		.algo_strength = SSL_MEDIUM,
1054 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1055 		.strength_bits = 112,
1056 		.alg_bits = 168,
1057 	},
1058 
1059 	/* Cipher C013 */
1060 	{
1061 		.valid = 1,
1062 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
1063 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
1064 		.algorithm_mkey = SSL_kECDHE,
1065 		.algorithm_auth = SSL_aRSA,
1066 		.algorithm_enc = SSL_AES128,
1067 		.algorithm_mac = SSL_SHA1,
1068 		.algorithm_ssl = SSL_TLSV1,
1069 		.algo_strength = SSL_HIGH,
1070 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1071 		.strength_bits = 128,
1072 		.alg_bits = 128,
1073 	},
1074 
1075 	/* Cipher C014 */
1076 	{
1077 		.valid = 1,
1078 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
1079 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
1080 		.algorithm_mkey = SSL_kECDHE,
1081 		.algorithm_auth = SSL_aRSA,
1082 		.algorithm_enc = SSL_AES256,
1083 		.algorithm_mac = SSL_SHA1,
1084 		.algorithm_ssl = SSL_TLSV1,
1085 		.algo_strength = SSL_HIGH,
1086 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1087 		.strength_bits = 256,
1088 		.alg_bits = 256,
1089 	},
1090 
1091 	/* Cipher C015 */
1092 	{
1093 		.valid = 1,
1094 		.name = TLS1_TXT_ECDH_anon_WITH_NULL_SHA,
1095 		.id = TLS1_CK_ECDH_anon_WITH_NULL_SHA,
1096 		.algorithm_mkey = SSL_kECDHE,
1097 		.algorithm_auth = SSL_aNULL,
1098 		.algorithm_enc = SSL_eNULL,
1099 		.algorithm_mac = SSL_SHA1,
1100 		.algorithm_ssl = SSL_TLSV1,
1101 		.algo_strength = SSL_STRONG_NONE,
1102 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1103 		.strength_bits = 0,
1104 		.alg_bits = 0,
1105 	},
1106 
1107 	/* Cipher C016 */
1108 	{
1109 		.valid = 1,
1110 		.name = TLS1_TXT_ECDH_anon_WITH_RC4_128_SHA,
1111 		.id = TLS1_CK_ECDH_anon_WITH_RC4_128_SHA,
1112 		.algorithm_mkey = SSL_kECDHE,
1113 		.algorithm_auth = SSL_aNULL,
1114 		.algorithm_enc = SSL_RC4,
1115 		.algorithm_mac = SSL_SHA1,
1116 		.algorithm_ssl = SSL_TLSV1,
1117 		.algo_strength = SSL_LOW,
1118 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1119 		.strength_bits = 128,
1120 		.alg_bits = 128,
1121 	},
1122 
1123 	/* Cipher C017 */
1124 	{
1125 		.valid = 1,
1126 		.name = TLS1_TXT_ECDH_anon_WITH_DES_192_CBC3_SHA,
1127 		.id = TLS1_CK_ECDH_anon_WITH_DES_192_CBC3_SHA,
1128 		.algorithm_mkey = SSL_kECDHE,
1129 		.algorithm_auth = SSL_aNULL,
1130 		.algorithm_enc = SSL_3DES,
1131 		.algorithm_mac = SSL_SHA1,
1132 		.algorithm_ssl = SSL_TLSV1,
1133 		.algo_strength = SSL_MEDIUM,
1134 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1135 		.strength_bits = 112,
1136 		.alg_bits = 168,
1137 	},
1138 
1139 	/* Cipher C018 */
1140 	{
1141 		.valid = 1,
1142 		.name = TLS1_TXT_ECDH_anon_WITH_AES_128_CBC_SHA,
1143 		.id = TLS1_CK_ECDH_anon_WITH_AES_128_CBC_SHA,
1144 		.algorithm_mkey = SSL_kECDHE,
1145 		.algorithm_auth = SSL_aNULL,
1146 		.algorithm_enc = SSL_AES128,
1147 		.algorithm_mac = SSL_SHA1,
1148 		.algorithm_ssl = SSL_TLSV1,
1149 		.algo_strength = SSL_HIGH,
1150 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1151 		.strength_bits = 128,
1152 		.alg_bits = 128,
1153 	},
1154 
1155 	/* Cipher C019 */
1156 	{
1157 		.valid = 1,
1158 		.name = TLS1_TXT_ECDH_anon_WITH_AES_256_CBC_SHA,
1159 		.id = TLS1_CK_ECDH_anon_WITH_AES_256_CBC_SHA,
1160 		.algorithm_mkey = SSL_kECDHE,
1161 		.algorithm_auth = SSL_aNULL,
1162 		.algorithm_enc = SSL_AES256,
1163 		.algorithm_mac = SSL_SHA1,
1164 		.algorithm_ssl = SSL_TLSV1,
1165 		.algo_strength = SSL_HIGH,
1166 		.algorithm2 = SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF,
1167 		.strength_bits = 256,
1168 		.alg_bits = 256,
1169 	},
1170 
1171 
1172 	/* HMAC based TLS v1.2 ciphersuites from RFC5289 */
1173 
1174 	/* Cipher C023 */
1175 	{
1176 		.valid = 1,
1177 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
1178 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
1179 		.algorithm_mkey = SSL_kECDHE,
1180 		.algorithm_auth = SSL_aECDSA,
1181 		.algorithm_enc = SSL_AES128,
1182 		.algorithm_mac = SSL_SHA256,
1183 		.algorithm_ssl = SSL_TLSV1_2,
1184 		.algo_strength = SSL_HIGH,
1185 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
1186 		.strength_bits = 128,
1187 		.alg_bits = 128,
1188 	},
1189 
1190 	/* Cipher C024 */
1191 	{
1192 		.valid = 1,
1193 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
1194 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
1195 		.algorithm_mkey = SSL_kECDHE,
1196 		.algorithm_auth = SSL_aECDSA,
1197 		.algorithm_enc = SSL_AES256,
1198 		.algorithm_mac = SSL_SHA384,
1199 		.algorithm_ssl = SSL_TLSV1_2,
1200 		.algo_strength = SSL_HIGH,
1201 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
1202 		.strength_bits = 256,
1203 		.alg_bits = 256,
1204 	},
1205 
1206 	/* Cipher C027 */
1207 	{
1208 		.valid = 1,
1209 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
1210 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
1211 		.algorithm_mkey = SSL_kECDHE,
1212 		.algorithm_auth = SSL_aRSA,
1213 		.algorithm_enc = SSL_AES128,
1214 		.algorithm_mac = SSL_SHA256,
1215 		.algorithm_ssl = SSL_TLSV1_2,
1216 		.algo_strength = SSL_HIGH,
1217 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
1218 		.strength_bits = 128,
1219 		.alg_bits = 128,
1220 	},
1221 
1222 	/* Cipher C028 */
1223 	{
1224 		.valid = 1,
1225 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
1226 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
1227 		.algorithm_mkey = SSL_kECDHE,
1228 		.algorithm_auth = SSL_aRSA,
1229 		.algorithm_enc = SSL_AES256,
1230 		.algorithm_mac = SSL_SHA384,
1231 		.algorithm_ssl = SSL_TLSV1_2,
1232 		.algo_strength = SSL_HIGH,
1233 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
1234 		.strength_bits = 256,
1235 		.alg_bits = 256,
1236 	},
1237 
1238 	/* GCM based TLS v1.2 ciphersuites from RFC5289 */
1239 
1240 	/* Cipher C02B */
1241 	{
1242 		.valid = 1,
1243 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
1244 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
1245 		.algorithm_mkey = SSL_kECDHE,
1246 		.algorithm_auth = SSL_aECDSA,
1247 		.algorithm_enc = SSL_AES128GCM,
1248 		.algorithm_mac = SSL_AEAD,
1249 		.algorithm_ssl = SSL_TLSV1_2,
1250 		.algo_strength = SSL_HIGH,
1251 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
1252 		    FIXED_NONCE_LEN(4)|
1253 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
1254 		.strength_bits = 128,
1255 		.alg_bits = 128,
1256 	},
1257 
1258 	/* Cipher C02C */
1259 	{
1260 		.valid = 1,
1261 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
1262 		.id = TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
1263 		.algorithm_mkey = SSL_kECDHE,
1264 		.algorithm_auth = SSL_aECDSA,
1265 		.algorithm_enc = SSL_AES256GCM,
1266 		.algorithm_mac = SSL_AEAD,
1267 		.algorithm_ssl = SSL_TLSV1_2,
1268 		.algo_strength = SSL_HIGH,
1269 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|
1270 		    FIXED_NONCE_LEN(4)|
1271 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
1272 		.strength_bits = 256,
1273 		.alg_bits = 256,
1274 	},
1275 
1276 	/* Cipher C02F */
1277 	{
1278 		.valid = 1,
1279 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1280 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1281 		.algorithm_mkey = SSL_kECDHE,
1282 		.algorithm_auth = SSL_aRSA,
1283 		.algorithm_enc = SSL_AES128GCM,
1284 		.algorithm_mac = SSL_AEAD,
1285 		.algorithm_ssl = SSL_TLSV1_2,
1286 		.algo_strength = SSL_HIGH,
1287 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
1288 		    FIXED_NONCE_LEN(4)|
1289 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
1290 		.strength_bits = 128,
1291 		.alg_bits = 128,
1292 	},
1293 
1294 	/* Cipher C030 */
1295 	{
1296 		.valid = 1,
1297 		.name = TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
1298 		.id = TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
1299 		.algorithm_mkey = SSL_kECDHE,
1300 		.algorithm_auth = SSL_aRSA,
1301 		.algorithm_enc = SSL_AES256GCM,
1302 		.algorithm_mac = SSL_AEAD,
1303 		.algorithm_ssl = SSL_TLSV1_2,
1304 		.algo_strength = SSL_HIGH,
1305 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|
1306 		    FIXED_NONCE_LEN(4)|
1307 		    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD,
1308 		.strength_bits = 256,
1309 		.alg_bits = 256,
1310 	},
1311 
1312 	/* Cipher CCA8 */
1313 	{
1314 		.valid = 1,
1315 		.name = TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
1316 		.id = TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305,
1317 		.algorithm_mkey = SSL_kECDHE,
1318 		.algorithm_auth = SSL_aRSA,
1319 		.algorithm_enc = SSL_CHACHA20POLY1305,
1320 		.algorithm_mac = SSL_AEAD,
1321 		.algorithm_ssl = SSL_TLSV1_2,
1322 		.algo_strength = SSL_HIGH,
1323 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
1324 		    FIXED_NONCE_LEN(12),
1325 		.strength_bits = 256,
1326 		.alg_bits = 256,
1327 	},
1328 
1329 	/* Cipher CCA9 */
1330 	{
1331 		.valid = 1,
1332 		.name = TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
1333 		.id = TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305,
1334 		.algorithm_mkey = SSL_kECDHE,
1335 		.algorithm_auth = SSL_aECDSA,
1336 		.algorithm_enc = SSL_CHACHA20POLY1305,
1337 		.algorithm_mac = SSL_AEAD,
1338 		.algorithm_ssl = SSL_TLSV1_2,
1339 		.algo_strength = SSL_HIGH,
1340 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
1341 		    FIXED_NONCE_LEN(12),
1342 		.strength_bits = 256,
1343 		.alg_bits = 256,
1344 	},
1345 
1346 	/* Cipher CCAA */
1347 	{
1348 		.valid = 1,
1349 		.name = TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
1350 		.id = TLS1_CK_DHE_RSA_CHACHA20_POLY1305,
1351 		.algorithm_mkey = SSL_kDHE,
1352 		.algorithm_auth = SSL_aRSA,
1353 		.algorithm_enc = SSL_CHACHA20POLY1305,
1354 		.algorithm_mac = SSL_AEAD,
1355 		.algorithm_ssl = SSL_TLSV1_2,
1356 		.algo_strength = SSL_HIGH,
1357 		.algorithm2 = SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|
1358 		    FIXED_NONCE_LEN(12),
1359 		.strength_bits = 256,
1360 		.alg_bits = 256,
1361 	},
1362 
1363 	/* Cipher FF85 FIXME IANA */
1364 	{
1365 		.valid = 1,
1366 		.name = "GOST2012256-GOST89-GOST89",
1367 		.id = 0x300ff85, /* FIXME IANA */
1368 		.algorithm_mkey = SSL_kGOST,
1369 		.algorithm_auth = SSL_aGOST01,
1370 		.algorithm_enc = SSL_eGOST2814789CNT,
1371 		.algorithm_mac = SSL_GOST89MAC,
1372 		.algorithm_ssl = SSL_TLSV1,
1373 		.algo_strength = SSL_HIGH,
1374 		.algorithm2 = SSL_HANDSHAKE_MAC_STREEBOG256|TLS1_PRF_STREEBOG256|
1375 		    TLS1_STREAM_MAC,
1376 		.strength_bits = 256,
1377 		.alg_bits = 256
1378 	},
1379 
1380 	/* Cipher FF87 FIXME IANA */
1381 	{
1382 		.valid = 1,
1383 		.name = "GOST2012256-NULL-STREEBOG256",
1384 		.id = 0x300ff87, /* FIXME IANA */
1385 		.algorithm_mkey = SSL_kGOST,
1386 		.algorithm_auth = SSL_aGOST01,
1387 		.algorithm_enc = SSL_eNULL,
1388 		.algorithm_mac = SSL_STREEBOG256,
1389 		.algorithm_ssl = SSL_TLSV1,
1390 		.algo_strength = SSL_STRONG_NONE,
1391 		.algorithm2 = SSL_HANDSHAKE_MAC_STREEBOG256|TLS1_PRF_STREEBOG256,
1392 		.strength_bits = 0,
1393 		.alg_bits = 0
1394 	},
1395 
1396 
1397 	/* end of list */
1398 };
1399 
1400 int
1401 ssl3_num_ciphers(void)
1402 {
1403 	return (SSL3_NUM_CIPHERS);
1404 }
1405 
1406 const SSL_CIPHER *
1407 ssl3_get_cipher(unsigned int u)
1408 {
1409 	if (u < SSL3_NUM_CIPHERS)
1410 		return (&(ssl3_ciphers[SSL3_NUM_CIPHERS - 1 - u]));
1411 	else
1412 		return (NULL);
1413 }
1414 
1415 const SSL_CIPHER *
1416 ssl3_get_cipher_by_id(unsigned int id)
1417 {
1418 	const SSL_CIPHER *cp;
1419 	SSL_CIPHER c;
1420 
1421 	c.id = id;
1422 	cp = OBJ_bsearch_ssl_cipher_id(&c, ssl3_ciphers, SSL3_NUM_CIPHERS);
1423 	if (cp != NULL && cp->valid == 1)
1424 		return (cp);
1425 
1426 	return (NULL);
1427 }
1428 
1429 const SSL_CIPHER *
1430 ssl3_get_cipher_by_value(uint16_t value)
1431 {
1432 	return ssl3_get_cipher_by_id(SSL3_CK_ID | value);
1433 }
1434 
1435 uint16_t
1436 ssl3_cipher_get_value(const SSL_CIPHER *c)
1437 {
1438 	return (c->id & SSL3_CK_VALUE_MASK);
1439 }
1440 
1441 int
1442 ssl3_pending(const SSL *s)
1443 {
1444 	if (s->internal->rstate == SSL_ST_READ_BODY)
1445 		return 0;
1446 
1447 	return (S3I(s)->rrec.type == SSL3_RT_APPLICATION_DATA) ?
1448 	    S3I(s)->rrec.length : 0;
1449 }
1450 
1451 int
1452 ssl3_handshake_msg_hdr_len(SSL *s)
1453 {
1454 	return (SSL_is_dtls(s) ? DTLS1_HM_HEADER_LENGTH :
1455             SSL3_HM_HEADER_LENGTH);
1456 }
1457 
1458 int
1459 ssl3_handshake_msg_start(SSL *s, CBB *handshake, CBB *body, uint8_t msg_type)
1460 {
1461 	int ret = 0;
1462 
1463 	if (!CBB_init(handshake, SSL3_RT_MAX_PLAIN_LENGTH))
1464 		goto err;
1465 	if (!CBB_add_u8(handshake, msg_type))
1466 		goto err;
1467 	if (SSL_is_dtls(s)) {
1468 		unsigned char *data;
1469 
1470 		if (!CBB_add_space(handshake, &data, DTLS1_HM_HEADER_LENGTH -
1471 		    SSL3_HM_HEADER_LENGTH))
1472 			goto err;
1473 	}
1474 	if (!CBB_add_u24_length_prefixed(handshake, body))
1475 		goto err;
1476 
1477 	ret = 1;
1478 
1479  err:
1480 	return (ret);
1481 }
1482 
1483 int
1484 ssl3_handshake_msg_finish(SSL *s, CBB *handshake)
1485 {
1486 	unsigned char *data = NULL;
1487 	size_t outlen;
1488 	int ret = 0;
1489 
1490 	if (!CBB_finish(handshake, &data, &outlen))
1491 		goto err;
1492 
1493 	if (outlen > INT_MAX)
1494 		goto err;
1495 
1496 	if (!BUF_MEM_grow_clean(s->internal->init_buf, outlen))
1497 		goto err;
1498 
1499 	memcpy(s->internal->init_buf->data, data, outlen);
1500 
1501 	s->internal->init_num = (int)outlen;
1502 	s->internal->init_off = 0;
1503 
1504 	if (SSL_is_dtls(s)) {
1505 		unsigned long len;
1506 		uint8_t msg_type;
1507 		CBS cbs;
1508 
1509 		CBS_init(&cbs, data, outlen);
1510 		if (!CBS_get_u8(&cbs, &msg_type))
1511 			goto err;
1512 
1513 		len = outlen - ssl3_handshake_msg_hdr_len(s);
1514 
1515 		dtls1_set_message_header(s, msg_type, len, 0, len);
1516 		dtls1_buffer_message(s, 0);
1517 	}
1518 
1519 	ret = 1;
1520 
1521  err:
1522 	free(data);
1523 
1524 	return (ret);
1525 }
1526 
1527 int
1528 ssl3_handshake_write(SSL *s)
1529 {
1530 	return ssl3_record_write(s, SSL3_RT_HANDSHAKE);
1531 }
1532 
1533 int
1534 ssl3_record_write(SSL *s, int type)
1535 {
1536 	if (SSL_is_dtls(s))
1537 		return dtls1_do_write(s, type);
1538 
1539 	return ssl3_do_write(s, type);
1540 }
1541 
1542 int
1543 ssl3_new(SSL *s)
1544 {
1545 	if ((s->s3 = calloc(1, sizeof(*s->s3))) == NULL)
1546 		return (0);
1547 	if ((S3I(s) = calloc(1, sizeof(*S3I(s)))) == NULL) {
1548 		free(s->s3);
1549 		return (0);
1550 	}
1551 
1552 	s->method->ssl_clear(s);
1553 
1554 	return (1);
1555 }
1556 
1557 void
1558 ssl3_free(SSL *s)
1559 {
1560 	if (s == NULL)
1561 		return;
1562 
1563 	tls1_cleanup_key_block(s);
1564 	ssl3_release_read_buffer(s);
1565 	ssl3_release_write_buffer(s);
1566 	freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len);
1567 
1568 	tls_key_share_free(S3I(s)->hs.key_share);
1569 
1570 	tls13_secrets_destroy(S3I(s)->hs.tls13.secrets);
1571 	freezero(S3I(s)->hs.tls13.cookie, S3I(s)->hs.tls13.cookie_len);
1572 	tls13_clienthello_hash_clear(&S3I(s)->hs.tls13);
1573 
1574 	sk_X509_NAME_pop_free(S3I(s)->hs.tls12.ca_names, X509_NAME_free);
1575 	sk_X509_pop_free(s->internal->verified_chain, X509_free);
1576 
1577 	tls1_transcript_free(s);
1578 	tls1_transcript_hash_free(s);
1579 
1580 	free(S3I(s)->alpn_selected);
1581 
1582 	freezero(S3I(s), sizeof(*S3I(s)));
1583 	freezero(s->s3, sizeof(*s->s3));
1584 
1585 	s->s3 = NULL;
1586 }
1587 
1588 void
1589 ssl3_clear(SSL *s)
1590 {
1591 	struct ssl3_state_internal_st *internal;
1592 	unsigned char	*rp, *wp;
1593 	size_t		 rlen, wlen;
1594 
1595 	tls1_cleanup_key_block(s);
1596 	sk_X509_NAME_pop_free(S3I(s)->hs.tls12.ca_names, X509_NAME_free);
1597 	sk_X509_pop_free(s->internal->verified_chain, X509_free);
1598 	s->internal->verified_chain = NULL;
1599 
1600 	freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len);
1601 	S3I(s)->hs.sigalgs = NULL;
1602 	S3I(s)->hs.sigalgs_len = 0;
1603 
1604 	tls_key_share_free(S3I(s)->hs.key_share);
1605 	S3I(s)->hs.key_share = NULL;
1606 
1607 	tls13_secrets_destroy(S3I(s)->hs.tls13.secrets);
1608 	S3I(s)->hs.tls13.secrets = NULL;
1609 	freezero(S3I(s)->hs.tls13.cookie, S3I(s)->hs.tls13.cookie_len);
1610 	S3I(s)->hs.tls13.cookie = NULL;
1611 	S3I(s)->hs.tls13.cookie_len = 0;
1612 	tls13_clienthello_hash_clear(&S3I(s)->hs.tls13);
1613 
1614 	S3I(s)->hs.extensions_seen = 0;
1615 
1616 	rp = S3I(s)->rbuf.buf;
1617 	wp = S3I(s)->wbuf.buf;
1618 	rlen = S3I(s)->rbuf.len;
1619 	wlen = S3I(s)->wbuf.len;
1620 
1621 	tls1_transcript_free(s);
1622 	tls1_transcript_hash_free(s);
1623 
1624 	free(S3I(s)->alpn_selected);
1625 	S3I(s)->alpn_selected = NULL;
1626 	S3I(s)->alpn_selected_len = 0;
1627 
1628 	memset(S3I(s), 0, sizeof(*S3I(s)));
1629 	internal = S3I(s);
1630 	memset(s->s3, 0, sizeof(*s->s3));
1631 	S3I(s) = internal;
1632 
1633 	S3I(s)->rbuf.buf = rp;
1634 	S3I(s)->wbuf.buf = wp;
1635 	S3I(s)->rbuf.len = rlen;
1636 	S3I(s)->wbuf.len = wlen;
1637 
1638 	ssl_free_wbio_buffer(s);
1639 
1640 	/* Not needed... */
1641 	S3I(s)->renegotiate = 0;
1642 	S3I(s)->total_renegotiations = 0;
1643 	S3I(s)->num_renegotiations = 0;
1644 	S3I(s)->in_read_app_data = 0;
1645 
1646 	s->internal->packet_length = 0;
1647 	s->version = TLS1_VERSION;
1648 
1649 	S3I(s)->hs.state = SSL_ST_BEFORE|((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);
1650 }
1651 
1652 long
1653 _SSL_get_peer_tmp_key(SSL *s, EVP_PKEY **key)
1654 {
1655 	EVP_PKEY *pkey = NULL;
1656 	int ret = 0;
1657 
1658 	*key = NULL;
1659 
1660 	if (S3I(s)->hs.key_share == NULL)
1661 		goto err;
1662 
1663 	if ((pkey = EVP_PKEY_new()) == NULL)
1664 		goto err;
1665 	if (!tls_key_share_peer_pkey(S3I(s)->hs.key_share, pkey))
1666 		goto err;
1667 
1668 	*key = pkey;
1669 	pkey = NULL;
1670 
1671 	ret = 1;
1672 
1673  err:
1674 	EVP_PKEY_free(pkey);
1675 
1676 	return (ret);
1677 }
1678 
1679 static int
1680 _SSL_session_reused(SSL *s)
1681 {
1682 	return s->internal->hit;
1683 }
1684 
1685 static int
1686 _SSL_num_renegotiations(SSL *s)
1687 {
1688 	return S3I(s)->num_renegotiations;
1689 }
1690 
1691 static int
1692 _SSL_clear_num_renegotiations(SSL *s)
1693 {
1694 	int renegs;
1695 
1696 	renegs = S3I(s)->num_renegotiations;
1697 	S3I(s)->num_renegotiations = 0;
1698 
1699 	return renegs;
1700 }
1701 
1702 static int
1703 _SSL_total_renegotiations(SSL *s)
1704 {
1705 	return S3I(s)->total_renegotiations;
1706 }
1707 
1708 static int
1709 _SSL_set_tmp_dh(SSL *s, DH *dh)
1710 {
1711 	DH *dhe_params;
1712 
1713 	if (dh == NULL) {
1714 		SSLerror(s, ERR_R_PASSED_NULL_PARAMETER);
1715 		return 0;
1716 	}
1717 
1718 	if ((dhe_params = DHparams_dup(dh)) == NULL) {
1719 		SSLerror(s, ERR_R_DH_LIB);
1720 		return 0;
1721 	}
1722 
1723 	DH_free(s->cert->dhe_params);
1724 	s->cert->dhe_params = dhe_params;
1725 
1726 	return 1;
1727 }
1728 
1729 static int
1730 _SSL_set_dh_auto(SSL *s, int state)
1731 {
1732 	s->cert->dhe_params_auto = state;
1733 	return 1;
1734 }
1735 
1736 static int
1737 _SSL_set_tmp_ecdh(SSL *s, EC_KEY *ecdh)
1738 {
1739 	const EC_GROUP *group;
1740 	int nid;
1741 
1742 	if (ecdh == NULL)
1743 		return 0;
1744 	if ((group = EC_KEY_get0_group(ecdh)) == NULL)
1745 		return 0;
1746 
1747 	nid = EC_GROUP_get_curve_name(group);
1748 	return SSL_set1_groups(s, &nid, 1);
1749 }
1750 
1751 static int
1752 _SSL_set_ecdh_auto(SSL *s, int state)
1753 {
1754 	return 1;
1755 }
1756 
1757 static int
1758 _SSL_set_tlsext_host_name(SSL *s, const char *name)
1759 {
1760 	int is_ip;
1761 	CBS cbs;
1762 
1763 	free(s->tlsext_hostname);
1764 	s->tlsext_hostname = NULL;
1765 
1766 	if (name == NULL)
1767 		return 1;
1768 
1769 	CBS_init(&cbs, name, strlen(name));
1770 
1771 	if (!tlsext_sni_is_valid_hostname(&cbs, &is_ip)) {
1772 		SSLerror(s, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1773 		return 0;
1774 	}
1775 	if ((s->tlsext_hostname = strdup(name)) == NULL) {
1776 		SSLerror(s, ERR_R_INTERNAL_ERROR);
1777 		return 0;
1778 	}
1779 
1780 	return 1;
1781 }
1782 
1783 static int
1784 _SSL_set_tlsext_debug_arg(SSL *s, void *arg)
1785 {
1786 	s->internal->tlsext_debug_arg = arg;
1787 	return 1;
1788 }
1789 
1790 static int
1791 _SSL_get_tlsext_status_type(SSL *s)
1792 {
1793 	return s->tlsext_status_type;
1794 }
1795 
1796 static int
1797 _SSL_set_tlsext_status_type(SSL *s, int type)
1798 {
1799 	s->tlsext_status_type = type;
1800 	return 1;
1801 }
1802 
1803 static int
1804 _SSL_get_tlsext_status_exts(SSL *s, STACK_OF(X509_EXTENSION) **exts)
1805 {
1806 	*exts = s->internal->tlsext_ocsp_exts;
1807 	return 1;
1808 }
1809 
1810 static int
1811 _SSL_set_tlsext_status_exts(SSL *s, STACK_OF(X509_EXTENSION) *exts)
1812 {
1813 	/* XXX - leak... */
1814 	s->internal->tlsext_ocsp_exts = exts;
1815 	return 1;
1816 }
1817 
1818 static int
1819 _SSL_get_tlsext_status_ids(SSL *s, STACK_OF(OCSP_RESPID) **ids)
1820 {
1821 	*ids = s->internal->tlsext_ocsp_ids;
1822 	return 1;
1823 }
1824 
1825 static int
1826 _SSL_set_tlsext_status_ids(SSL *s, STACK_OF(OCSP_RESPID) *ids)
1827 {
1828 	/* XXX - leak... */
1829 	s->internal->tlsext_ocsp_ids = ids;
1830 	return 1;
1831 }
1832 
1833 static int
1834 _SSL_get_tlsext_status_ocsp_resp(SSL *s, unsigned char **resp)
1835 {
1836 	if (s->internal->tlsext_ocsp_resp != NULL &&
1837 	    s->internal->tlsext_ocsp_resp_len < INT_MAX) {
1838 		*resp = s->internal->tlsext_ocsp_resp;
1839 		return (int)s->internal->tlsext_ocsp_resp_len;
1840 	}
1841 
1842 	*resp = NULL;
1843 
1844 	return -1;
1845 }
1846 
1847 static int
1848 _SSL_set_tlsext_status_ocsp_resp(SSL *s, unsigned char *resp, int resp_len)
1849 {
1850 	free(s->internal->tlsext_ocsp_resp);
1851 	s->internal->tlsext_ocsp_resp = NULL;
1852 	s->internal->tlsext_ocsp_resp_len = 0;
1853 
1854 	if (resp_len < 0)
1855 		return 0;
1856 
1857 	s->internal->tlsext_ocsp_resp = resp;
1858 	s->internal->tlsext_ocsp_resp_len = (size_t)resp_len;
1859 
1860 	return 1;
1861 }
1862 
1863 int
1864 SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain)
1865 {
1866 	return ssl_cert_set0_chain(ssl->cert, chain);
1867 }
1868 
1869 int
1870 SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain)
1871 {
1872 	return ssl_cert_set1_chain(ssl->cert, chain);
1873 }
1874 
1875 int
1876 SSL_add0_chain_cert(SSL *ssl, X509 *x509)
1877 {
1878 	return ssl_cert_add0_chain_cert(ssl->cert, x509);
1879 }
1880 
1881 int
1882 SSL_add1_chain_cert(SSL *ssl, X509 *x509)
1883 {
1884 	return ssl_cert_add1_chain_cert(ssl->cert, x509);
1885 }
1886 
1887 int
1888 SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain)
1889 {
1890 	*out_chain = NULL;
1891 
1892 	if (ssl->cert->key != NULL)
1893 		*out_chain = ssl->cert->key->chain;
1894 
1895 	return 1;
1896 }
1897 
1898 int
1899 SSL_clear_chain_certs(SSL *ssl)
1900 {
1901 	return ssl_cert_set0_chain(ssl->cert, NULL);
1902 }
1903 
1904 int
1905 SSL_set1_groups(SSL *s, const int *groups, size_t groups_len)
1906 {
1907 	return tls1_set_groups(&s->internal->tlsext_supportedgroups,
1908 	    &s->internal->tlsext_supportedgroups_length, groups, groups_len);
1909 }
1910 
1911 int
1912 SSL_set1_groups_list(SSL *s, const char *groups)
1913 {
1914 	return tls1_set_group_list(&s->internal->tlsext_supportedgroups,
1915 	    &s->internal->tlsext_supportedgroups_length, groups);
1916 }
1917 
1918 static int
1919 _SSL_get_signature_nid(SSL *s, int *nid)
1920 {
1921 	const struct ssl_sigalg *sigalg;
1922 
1923 	if ((sigalg = S3I(s)->hs.our_sigalg) == NULL)
1924 		return 0;
1925 
1926 	*nid = EVP_MD_type(sigalg->md());
1927 
1928 	return 1;
1929 }
1930 
1931 static int
1932 _SSL_get_peer_signature_nid(SSL *s, int *nid)
1933 {
1934 	const struct ssl_sigalg *sigalg;
1935 
1936 	if ((sigalg = S3I(s)->hs.peer_sigalg) == NULL)
1937 		return 0;
1938 
1939 	*nid = EVP_MD_type(sigalg->md());
1940 
1941 	return 1;
1942 }
1943 
1944 int
1945 SSL_get_signature_type_nid(const SSL *s, int *nid)
1946 {
1947 	const struct ssl_sigalg *sigalg;
1948 
1949 	if ((sigalg = S3I(s)->hs.our_sigalg) == NULL)
1950 		return 0;
1951 
1952 	*nid = sigalg->key_type;
1953 	if (sigalg->key_type == EVP_PKEY_RSA &&
1954 	    (sigalg->flags & SIGALG_FLAG_RSA_PSS))
1955 		*nid = EVP_PKEY_RSA_PSS;
1956 
1957 	return 1;
1958 }
1959 
1960 int
1961 SSL_get_peer_signature_type_nid(const SSL *s, int *nid)
1962 {
1963 	const struct ssl_sigalg *sigalg;
1964 
1965 	if ((sigalg = S3I(s)->hs.peer_sigalg) == NULL)
1966 		return 0;
1967 
1968 	*nid = sigalg->key_type;
1969 	if (sigalg->key_type == EVP_PKEY_RSA &&
1970 	    (sigalg->flags & SIGALG_FLAG_RSA_PSS))
1971 		*nid = EVP_PKEY_RSA_PSS;
1972 
1973 	return 1;
1974 }
1975 
1976 long
1977 ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
1978 {
1979 	switch (cmd) {
1980 	case SSL_CTRL_GET_SESSION_REUSED:
1981 		return _SSL_session_reused(s);
1982 
1983 	case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
1984 		return _SSL_num_renegotiations(s);
1985 
1986 	case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
1987 		return _SSL_clear_num_renegotiations(s);
1988 
1989 	case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
1990 		return _SSL_total_renegotiations(s);
1991 
1992 	case SSL_CTRL_SET_TMP_DH:
1993 		return _SSL_set_tmp_dh(s, parg);
1994 
1995 	case SSL_CTRL_SET_TMP_DH_CB:
1996 		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1997 		return 0;
1998 
1999 	case SSL_CTRL_SET_DH_AUTO:
2000 		return _SSL_set_dh_auto(s, larg);
2001 
2002 	case SSL_CTRL_SET_TMP_ECDH:
2003 		return _SSL_set_tmp_ecdh(s, parg);
2004 
2005 	case SSL_CTRL_SET_TMP_ECDH_CB:
2006 		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2007 		return 0;
2008 
2009 	case SSL_CTRL_SET_ECDH_AUTO:
2010 		return _SSL_set_ecdh_auto(s, larg);
2011 
2012 	case SSL_CTRL_SET_TLSEXT_HOSTNAME:
2013 		if (larg != TLSEXT_NAMETYPE_host_name) {
2014 			SSLerror(s, SSL_R_SSL3_EXT_INVALID_SERVERNAME_TYPE);
2015 			return 0;
2016 		}
2017 		return _SSL_set_tlsext_host_name(s, parg);
2018 
2019 	case SSL_CTRL_SET_TLSEXT_DEBUG_ARG:
2020 		return _SSL_set_tlsext_debug_arg(s, parg);
2021 
2022 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_TYPE:
2023 		return _SSL_get_tlsext_status_type(s);
2024 
2025 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_TYPE:
2026 		return _SSL_set_tlsext_status_type(s, larg);
2027 
2028 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_EXTS:
2029 		return _SSL_get_tlsext_status_exts(s, parg);
2030 
2031 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_EXTS:
2032 		return _SSL_set_tlsext_status_exts(s, parg);
2033 
2034 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_IDS:
2035 		return _SSL_get_tlsext_status_ids(s, parg);
2036 
2037 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_IDS:
2038 		return _SSL_set_tlsext_status_ids(s, parg);
2039 
2040 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_OCSP_RESP:
2041 		return _SSL_get_tlsext_status_ocsp_resp(s, parg);
2042 
2043 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_OCSP_RESP:
2044 		return _SSL_set_tlsext_status_ocsp_resp(s, parg, larg);
2045 
2046 	case SSL_CTRL_CHAIN:
2047 		if (larg == 0)
2048 			return SSL_set0_chain(s, (STACK_OF(X509) *)parg);
2049 		else
2050 			return SSL_set1_chain(s, (STACK_OF(X509) *)parg);
2051 
2052 	case SSL_CTRL_CHAIN_CERT:
2053 		if (larg == 0)
2054 			return SSL_add0_chain_cert(s, (X509 *)parg);
2055 		else
2056 			return SSL_add1_chain_cert(s, (X509 *)parg);
2057 
2058 	case SSL_CTRL_GET_CHAIN_CERTS:
2059 		return SSL_get0_chain_certs(s, (STACK_OF(X509) **)parg);
2060 
2061 	case SSL_CTRL_SET_GROUPS:
2062 		return SSL_set1_groups(s, parg, larg);
2063 
2064 	case SSL_CTRL_SET_GROUPS_LIST:
2065 		return SSL_set1_groups_list(s, parg);
2066 
2067 	/* XXX - rename to SSL_CTRL_GET_PEER_TMP_KEY and remove server check. */
2068 	case SSL_CTRL_GET_SERVER_TMP_KEY:
2069 		if (s->server != 0)
2070 			return 0;
2071 		return _SSL_get_peer_tmp_key(s, parg);
2072 
2073 	case SSL_CTRL_GET_MIN_PROTO_VERSION:
2074 		return SSL_get_min_proto_version(s);
2075 
2076 	case SSL_CTRL_GET_MAX_PROTO_VERSION:
2077 		return SSL_get_max_proto_version(s);
2078 
2079 	case SSL_CTRL_SET_MIN_PROTO_VERSION:
2080 		if (larg < 0 || larg > UINT16_MAX)
2081 			return 0;
2082 		return SSL_set_min_proto_version(s, larg);
2083 
2084 	case SSL_CTRL_SET_MAX_PROTO_VERSION:
2085 		if (larg < 0 || larg > UINT16_MAX)
2086 			return 0;
2087 		return SSL_set_max_proto_version(s, larg);
2088 
2089 	case SSL_CTRL_GET_SIGNATURE_NID:
2090 		return _SSL_get_signature_nid(s, parg);
2091 
2092 	case SSL_CTRL_GET_PEER_SIGNATURE_NID:
2093 		return _SSL_get_peer_signature_nid(s, parg);
2094 
2095 	/*
2096 	 * Legacy controls that should eventually be removed.
2097 	 */
2098 	case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
2099 		return 0;
2100 
2101 	case SSL_CTRL_GET_FLAGS:
2102 		return (int)(s->s3->flags);
2103 
2104 	case SSL_CTRL_NEED_TMP_RSA:
2105 		return 0;
2106 
2107 	case SSL_CTRL_SET_TMP_RSA:
2108 	case SSL_CTRL_SET_TMP_RSA_CB:
2109 		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2110 		return 0;
2111 	}
2112 
2113 	return 0;
2114 }
2115 
2116 long
2117 ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
2118 {
2119 	switch (cmd) {
2120 	case SSL_CTRL_SET_TMP_RSA_CB:
2121 		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2122 		return 0;
2123 
2124 	case SSL_CTRL_SET_TMP_DH_CB:
2125 		s->cert->dhe_params_cb = (DH *(*)(SSL *, int, int))fp;
2126 		return 1;
2127 
2128 	case SSL_CTRL_SET_TMP_ECDH_CB:
2129 		return 1;
2130 
2131 	case SSL_CTRL_SET_TLSEXT_DEBUG_CB:
2132 		s->internal->tlsext_debug_cb = (void (*)(SSL *, int , int,
2133 		    unsigned char *, int, void *))fp;
2134 		return 1;
2135 	}
2136 
2137 	return 0;
2138 }
2139 
2140 static int
2141 _SSL_CTX_set_tmp_dh(SSL_CTX *ctx, DH *dh)
2142 {
2143 	DH *dhe_params;
2144 
2145 	if (dh == NULL) {
2146 		SSLerrorx(ERR_R_PASSED_NULL_PARAMETER);
2147 		return 0;
2148 	}
2149 
2150 	if ((dhe_params = DHparams_dup(dh)) == NULL) {
2151 		SSLerrorx(ERR_R_DH_LIB);
2152 		return 0;
2153 	}
2154 
2155 	DH_free(ctx->internal->cert->dhe_params);
2156 	ctx->internal->cert->dhe_params = dhe_params;
2157 
2158 	return 1;
2159 }
2160 
2161 static int
2162 _SSL_CTX_set_dh_auto(SSL_CTX *ctx, int state)
2163 {
2164 	ctx->internal->cert->dhe_params_auto = state;
2165 	return 1;
2166 }
2167 
2168 static int
2169 _SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, EC_KEY *ecdh)
2170 {
2171 	const EC_GROUP *group;
2172 	int nid;
2173 
2174 	if (ecdh == NULL)
2175 		return 0;
2176 	if ((group = EC_KEY_get0_group(ecdh)) == NULL)
2177 		return 0;
2178 
2179 	nid = EC_GROUP_get_curve_name(group);
2180 	return SSL_CTX_set1_groups(ctx, &nid, 1);
2181 }
2182 
2183 static int
2184 _SSL_CTX_set_ecdh_auto(SSL_CTX *ctx, int state)
2185 {
2186 	return 1;
2187 }
2188 
2189 static int
2190 _SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg)
2191 {
2192 	ctx->internal->tlsext_servername_arg = arg;
2193 	return 1;
2194 }
2195 
2196 static int
2197 _SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, unsigned char *keys, int keys_len)
2198 {
2199 	if (keys == NULL)
2200 		return 48;
2201 
2202 	if (keys_len != 48) {
2203 		SSLerrorx(SSL_R_INVALID_TICKET_KEYS_LENGTH);
2204 		return 0;
2205 	}
2206 
2207 	memcpy(keys, ctx->internal->tlsext_tick_key_name, 16);
2208 	memcpy(keys + 16, ctx->internal->tlsext_tick_hmac_key, 16);
2209 	memcpy(keys + 32, ctx->internal->tlsext_tick_aes_key, 16);
2210 
2211 	return 1;
2212 }
2213 
2214 static int
2215 _SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, unsigned char *keys, int keys_len)
2216 {
2217 	if (keys == NULL)
2218 		return 48;
2219 
2220 	if (keys_len != 48) {
2221 		SSLerrorx(SSL_R_INVALID_TICKET_KEYS_LENGTH);
2222 		return 0;
2223 	}
2224 
2225 	memcpy(ctx->internal->tlsext_tick_key_name, keys, 16);
2226 	memcpy(ctx->internal->tlsext_tick_hmac_key, keys + 16, 16);
2227 	memcpy(ctx->internal->tlsext_tick_aes_key, keys + 32, 16);
2228 
2229 	return 1;
2230 }
2231 
2232 static int
2233 _SSL_CTX_get_tlsext_status_arg(SSL_CTX *ctx, void **arg)
2234 {
2235 	*arg = ctx->internal->tlsext_status_arg;
2236 	return 1;
2237 }
2238 
2239 static int
2240 _SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg)
2241 {
2242 	ctx->internal->tlsext_status_arg = arg;
2243 	return 1;
2244 }
2245 
2246 int
2247 SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain)
2248 {
2249 	return ssl_cert_set0_chain(ctx->internal->cert, chain);
2250 }
2251 
2252 int
2253 SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain)
2254 {
2255 	return ssl_cert_set1_chain(ctx->internal->cert, chain);
2256 }
2257 
2258 int
2259 SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509)
2260 {
2261 	return ssl_cert_add0_chain_cert(ctx->internal->cert, x509);
2262 }
2263 
2264 int
2265 SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509)
2266 {
2267 	return ssl_cert_add1_chain_cert(ctx->internal->cert, x509);
2268 }
2269 
2270 int
2271 SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain)
2272 {
2273 	*out_chain = NULL;
2274 
2275 	if (ctx->internal->cert->key != NULL)
2276 		*out_chain = ctx->internal->cert->key->chain;
2277 
2278 	return 1;
2279 }
2280 
2281 int
2282 SSL_CTX_clear_chain_certs(SSL_CTX *ctx)
2283 {
2284 	return ssl_cert_set0_chain(ctx->internal->cert, NULL);
2285 }
2286 
2287 static int
2288 _SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *cert)
2289 {
2290 	if (ctx->extra_certs == NULL) {
2291 		if ((ctx->extra_certs = sk_X509_new_null()) == NULL)
2292 			return 0;
2293 	}
2294 	if (sk_X509_push(ctx->extra_certs, cert) == 0)
2295 		return 0;
2296 
2297 	return 1;
2298 }
2299 
2300 static int
2301 _SSL_CTX_get_extra_chain_certs(SSL_CTX *ctx, STACK_OF(X509) **certs)
2302 {
2303 	*certs = ctx->extra_certs;
2304 	if (*certs == NULL)
2305 		*certs = ctx->internal->cert->key->chain;
2306 
2307 	return 1;
2308 }
2309 
2310 static int
2311 _SSL_CTX_get_extra_chain_certs_only(SSL_CTX *ctx, STACK_OF(X509) **certs)
2312 {
2313 	*certs = ctx->extra_certs;
2314 	return 1;
2315 }
2316 
2317 static int
2318 _SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx)
2319 {
2320 	sk_X509_pop_free(ctx->extra_certs, X509_free);
2321 	ctx->extra_certs = NULL;
2322 	return 1;
2323 }
2324 
2325 int
2326 SSL_CTX_set1_groups(SSL_CTX *ctx, const int *groups, size_t groups_len)
2327 {
2328 	return tls1_set_groups(&ctx->internal->tlsext_supportedgroups,
2329 	    &ctx->internal->tlsext_supportedgroups_length, groups, groups_len);
2330 }
2331 
2332 int
2333 SSL_CTX_set1_groups_list(SSL_CTX *ctx, const char *groups)
2334 {
2335 	return tls1_set_group_list(&ctx->internal->tlsext_supportedgroups,
2336 	    &ctx->internal->tlsext_supportedgroups_length, groups);
2337 }
2338 
2339 long
2340 ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
2341 {
2342 	switch (cmd) {
2343 	case SSL_CTRL_SET_TMP_DH:
2344 		return _SSL_CTX_set_tmp_dh(ctx, parg);
2345 
2346 	case SSL_CTRL_SET_TMP_DH_CB:
2347 		SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2348 		return 0;
2349 
2350 	case SSL_CTRL_SET_DH_AUTO:
2351 		return _SSL_CTX_set_dh_auto(ctx, larg);
2352 
2353 	case SSL_CTRL_SET_TMP_ECDH:
2354 		return _SSL_CTX_set_tmp_ecdh(ctx, parg);
2355 
2356 	case SSL_CTRL_SET_TMP_ECDH_CB:
2357 		SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2358 		return 0;
2359 
2360 	case SSL_CTRL_SET_ECDH_AUTO:
2361 		return _SSL_CTX_set_ecdh_auto(ctx, larg);
2362 
2363 	case SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG:
2364 		return _SSL_CTX_set_tlsext_servername_arg(ctx, parg);
2365 
2366 	case SSL_CTRL_GET_TLSEXT_TICKET_KEYS:
2367 		return _SSL_CTX_get_tlsext_ticket_keys(ctx, parg, larg);
2368 
2369 	case SSL_CTRL_SET_TLSEXT_TICKET_KEYS:
2370 		return _SSL_CTX_set_tlsext_ticket_keys(ctx, parg, larg);
2371 
2372 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG:
2373 		return _SSL_CTX_get_tlsext_status_arg(ctx, parg);
2374 
2375 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB_ARG:
2376 		return _SSL_CTX_set_tlsext_status_arg(ctx, parg);
2377 
2378 	case SSL_CTRL_CHAIN:
2379 		if (larg == 0)
2380 			return SSL_CTX_set0_chain(ctx, (STACK_OF(X509) *)parg);
2381 		else
2382 			return SSL_CTX_set1_chain(ctx, (STACK_OF(X509) *)parg);
2383 
2384 	case SSL_CTRL_CHAIN_CERT:
2385 		if (larg == 0)
2386 			return SSL_CTX_add0_chain_cert(ctx, (X509 *)parg);
2387 		else
2388 			return SSL_CTX_add1_chain_cert(ctx, (X509 *)parg);
2389 
2390 	case SSL_CTRL_GET_CHAIN_CERTS:
2391 		return SSL_CTX_get0_chain_certs(ctx, (STACK_OF(X509) **)parg);
2392 
2393 	case SSL_CTRL_EXTRA_CHAIN_CERT:
2394 		return _SSL_CTX_add_extra_chain_cert(ctx, parg);
2395 
2396 	case SSL_CTRL_GET_EXTRA_CHAIN_CERTS:
2397 		if (larg == 0)
2398 			return _SSL_CTX_get_extra_chain_certs(ctx, parg);
2399 		else
2400 			return _SSL_CTX_get_extra_chain_certs_only(ctx, parg);
2401 
2402 	case SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS:
2403 		return _SSL_CTX_clear_extra_chain_certs(ctx);
2404 
2405 	case SSL_CTRL_SET_GROUPS:
2406 		return SSL_CTX_set1_groups(ctx, parg, larg);
2407 
2408 	case SSL_CTRL_SET_GROUPS_LIST:
2409 		return SSL_CTX_set1_groups_list(ctx, parg);
2410 
2411 	case SSL_CTRL_GET_MIN_PROTO_VERSION:
2412 		return SSL_CTX_get_min_proto_version(ctx);
2413 
2414 	case SSL_CTRL_GET_MAX_PROTO_VERSION:
2415 		return SSL_CTX_get_max_proto_version(ctx);
2416 
2417 	case SSL_CTRL_SET_MIN_PROTO_VERSION:
2418 		if (larg < 0 || larg > UINT16_MAX)
2419 			return 0;
2420 		return SSL_CTX_set_min_proto_version(ctx, larg);
2421 
2422 	case SSL_CTRL_SET_MAX_PROTO_VERSION:
2423 		if (larg < 0 || larg > UINT16_MAX)
2424 			return 0;
2425 		return SSL_CTX_set_max_proto_version(ctx, larg);
2426 
2427 	/*
2428 	 * Legacy controls that should eventually be removed.
2429 	 */
2430 	case SSL_CTRL_NEED_TMP_RSA:
2431 		return 0;
2432 
2433 	case SSL_CTRL_SET_TMP_RSA:
2434 	case SSL_CTRL_SET_TMP_RSA_CB:
2435 		SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2436 		return 0;
2437 	}
2438 
2439 	return 0;
2440 }
2441 
2442 long
2443 ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
2444 {
2445 	switch (cmd) {
2446 	case SSL_CTRL_SET_TMP_RSA_CB:
2447 		SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2448 		return 0;
2449 
2450 	case SSL_CTRL_SET_TMP_DH_CB:
2451 		ctx->internal->cert->dhe_params_cb =
2452 		    (DH *(*)(SSL *, int, int))fp;
2453 		return 1;
2454 
2455 	case SSL_CTRL_SET_TMP_ECDH_CB:
2456 		return 1;
2457 
2458 	case SSL_CTRL_SET_TLSEXT_SERVERNAME_CB:
2459 		ctx->internal->tlsext_servername_callback =
2460 		    (int (*)(SSL *, int *, void *))fp;
2461 		return 1;
2462 
2463 	case SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB:
2464 		*(int (**)(SSL *, void *))fp = ctx->internal->tlsext_status_cb;
2465 		return 1;
2466 
2467 	case SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB:
2468 		ctx->internal->tlsext_status_cb = (int (*)(SSL *, void *))fp;
2469 		return 1;
2470 
2471 	case SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB:
2472 		ctx->internal->tlsext_ticket_key_cb = (int (*)(SSL *, unsigned char  *,
2473 		    unsigned char *, EVP_CIPHER_CTX *, HMAC_CTX *, int))fp;
2474 		return 1;
2475 	}
2476 
2477 	return 0;
2478 }
2479 
2480 SSL_CIPHER *
2481 ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *clnt,
2482     STACK_OF(SSL_CIPHER) *srvr)
2483 {
2484 	unsigned long alg_k, alg_a, mask_k, mask_a;
2485 	STACK_OF(SSL_CIPHER) *prio, *allow;
2486 	SSL_CIPHER *c, *ret = NULL;
2487 	int can_use_ecc;
2488 	int i, ii, ok;
2489 	SSL_CERT *cert;
2490 
2491 	/* Let's see which ciphers we can support */
2492 	cert = s->cert;
2493 
2494 	can_use_ecc = (tls1_get_shared_curve(s) != NID_undef);
2495 
2496 	/*
2497 	 * Do not set the compare functions, because this may lead to a
2498 	 * reordering by "id". We want to keep the original ordering.
2499 	 * We may pay a price in performance during sk_SSL_CIPHER_find(),
2500 	 * but would have to pay with the price of sk_SSL_CIPHER_dup().
2501 	 */
2502 
2503 	if (s->internal->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2504 		prio = srvr;
2505 		allow = clnt;
2506 	} else {
2507 		prio = clnt;
2508 		allow = srvr;
2509 	}
2510 
2511 	for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
2512 		c = sk_SSL_CIPHER_value(prio, i);
2513 
2514 		/* Skip TLS v1.2 only ciphersuites if not supported. */
2515 		if ((c->algorithm_ssl & SSL_TLSV1_2) &&
2516 		    !SSL_USE_TLS1_2_CIPHERS(s))
2517 			continue;
2518 
2519 		/* Skip TLS v1.3 only ciphersuites if not supported. */
2520 		if ((c->algorithm_ssl & SSL_TLSV1_3) &&
2521 		    !SSL_USE_TLS1_3_CIPHERS(s))
2522 			continue;
2523 
2524 		/* If TLS v1.3, only allow TLS v1.3 ciphersuites. */
2525 		if (SSL_USE_TLS1_3_CIPHERS(s) &&
2526 		    !(c->algorithm_ssl & SSL_TLSV1_3))
2527 			continue;
2528 
2529 		ssl_set_cert_masks(cert, c);
2530 		mask_k = cert->mask_k;
2531 		mask_a = cert->mask_a;
2532 
2533 		alg_k = c->algorithm_mkey;
2534 		alg_a = c->algorithm_auth;
2535 
2536 		ok = (alg_k & mask_k) && (alg_a & mask_a);
2537 
2538 		/*
2539 		 * If we are considering an ECC cipher suite that uses our
2540 		 * certificate check it.
2541 		 */
2542 		if (alg_a & SSL_aECDSA)
2543 			ok = ok && tls1_check_ec_server_key(s);
2544 		/*
2545 		 * If we are considering an ECC cipher suite that uses
2546 		 * an ephemeral EC key check it.
2547 		 */
2548 		if (alg_k & SSL_kECDHE)
2549 			ok = ok && can_use_ecc;
2550 
2551 		if (!ok)
2552 			continue;
2553 		ii = sk_SSL_CIPHER_find(allow, c);
2554 		if (ii >= 0) {
2555 			ret = sk_SSL_CIPHER_value(allow, ii);
2556 			break;
2557 		}
2558 	}
2559 	return (ret);
2560 }
2561 
2562 int
2563 ssl3_get_req_cert_types(SSL *s, CBB *cbb)
2564 {
2565 	unsigned long alg_k;
2566 
2567 	alg_k = S3I(s)->hs.cipher->algorithm_mkey;
2568 
2569 #ifndef OPENSSL_NO_GOST
2570 	if ((alg_k & SSL_kGOST) != 0) {
2571 		if (!CBB_add_u8(cbb, TLS_CT_GOST01_SIGN))
2572 			return 0;
2573 		if (!CBB_add_u8(cbb, TLS_CT_GOST12_256_SIGN))
2574 			return 0;
2575 		if (!CBB_add_u8(cbb, TLS_CT_GOST12_512_SIGN))
2576 			return 0;
2577 		if (!CBB_add_u8(cbb, TLS_CT_GOST12_256_SIGN_COMPAT))
2578 			return 0;
2579 		if (!CBB_add_u8(cbb, TLS_CT_GOST12_512_SIGN_COMPAT))
2580 			return 0;
2581 	}
2582 #endif
2583 
2584 	if ((alg_k & SSL_kDHE) != 0) {
2585 		if (!CBB_add_u8(cbb, SSL3_CT_RSA_FIXED_DH))
2586 			return 0;
2587 	}
2588 
2589 	if (!CBB_add_u8(cbb, SSL3_CT_RSA_SIGN))
2590 		return 0;
2591 
2592 	/*
2593 	 * ECDSA certs can be used with RSA cipher suites as well
2594 	 * so we don't need to check for SSL_kECDH or SSL_kECDHE.
2595 	 */
2596 	if (!CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN))
2597 		return 0;
2598 
2599 	return 1;
2600 }
2601 
2602 int
2603 ssl3_shutdown(SSL *s)
2604 {
2605 	int	ret;
2606 
2607 	/*
2608 	 * Don't do anything much if we have not done the handshake or
2609 	 * we don't want to send messages :-)
2610 	 */
2611 	if ((s->internal->quiet_shutdown) || (S3I(s)->hs.state == SSL_ST_BEFORE)) {
2612 		s->internal->shutdown = (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
2613 		return (1);
2614 	}
2615 
2616 	if (!(s->internal->shutdown & SSL_SENT_SHUTDOWN)) {
2617 		s->internal->shutdown|=SSL_SENT_SHUTDOWN;
2618 		ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
2619 		/*
2620 		 * Our shutdown alert has been sent now, and if it still needs
2621 	 	 * to be written, S3I(s)->alert_dispatch will be true
2622 		 */
2623 		if (S3I(s)->alert_dispatch)
2624 			return(-1);	/* return WANT_WRITE */
2625 	} else if (S3I(s)->alert_dispatch) {
2626 		/* resend it if not sent */
2627 		ret = ssl3_dispatch_alert(s);
2628 		if (ret == -1) {
2629 			/*
2630 			 * We only get to return -1 here the 2nd/Nth
2631 			 * invocation, we must  have already signalled
2632 			 * return 0 upon a previous invoation,
2633 			 * return WANT_WRITE
2634 			 */
2635 			return (ret);
2636 		}
2637 	} else if (!(s->internal->shutdown & SSL_RECEIVED_SHUTDOWN)) {
2638 		/* If we are waiting for a close from our peer, we are closed */
2639 		s->method->ssl_read_bytes(s, 0, NULL, 0, 0);
2640 		if (!(s->internal->shutdown & SSL_RECEIVED_SHUTDOWN)) {
2641 			return(-1);	/* return WANT_READ */
2642 		}
2643 	}
2644 
2645 	if ((s->internal->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
2646 	    !S3I(s)->alert_dispatch)
2647 		return (1);
2648 	else
2649 		return (0);
2650 }
2651 
2652 int
2653 ssl3_write(SSL *s, const void *buf, int len)
2654 {
2655 	errno = 0;
2656 
2657 	if (S3I(s)->renegotiate)
2658 		ssl3_renegotiate_check(s);
2659 
2660 	return s->method->ssl_write_bytes(s, SSL3_RT_APPLICATION_DATA,
2661 	    buf, len);
2662 }
2663 
2664 static int
2665 ssl3_read_internal(SSL *s, void *buf, int len, int peek)
2666 {
2667 	int	ret;
2668 
2669 	errno = 0;
2670 	if (S3I(s)->renegotiate)
2671 		ssl3_renegotiate_check(s);
2672 	S3I(s)->in_read_app_data = 1;
2673 
2674 	ret = s->method->ssl_read_bytes(s, SSL3_RT_APPLICATION_DATA, buf, len,
2675 	    peek);
2676 	if ((ret == -1) && (S3I(s)->in_read_app_data == 2)) {
2677 		/*
2678 		 * ssl3_read_bytes decided to call s->internal->handshake_func,
2679 		 * which called ssl3_read_bytes to read handshake data.
2680 		 * However, ssl3_read_bytes actually found application data
2681 		 * and thinks that application data makes sense here; so disable
2682 		 * handshake processing and try to read application data again.
2683 		 */
2684 		s->internal->in_handshake++;
2685 		ret = s->method->ssl_read_bytes(s, SSL3_RT_APPLICATION_DATA,
2686 		    buf, len, peek);
2687 		s->internal->in_handshake--;
2688 	} else
2689 		S3I(s)->in_read_app_data = 0;
2690 
2691 	return (ret);
2692 }
2693 
2694 int
2695 ssl3_read(SSL *s, void *buf, int len)
2696 {
2697 	return ssl3_read_internal(s, buf, len, 0);
2698 }
2699 
2700 int
2701 ssl3_peek(SSL *s, void *buf, int len)
2702 {
2703 	return ssl3_read_internal(s, buf, len, 1);
2704 }
2705 
2706 int
2707 ssl3_renegotiate(SSL *s)
2708 {
2709 	if (s->internal->handshake_func == NULL)
2710 		return (1);
2711 
2712 	if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
2713 		return (0);
2714 
2715 	S3I(s)->renegotiate = 1;
2716 	return (1);
2717 }
2718 
2719 int
2720 ssl3_renegotiate_check(SSL *s)
2721 {
2722 	int	ret = 0;
2723 
2724 	if (S3I(s)->renegotiate) {
2725 		if ((S3I(s)->rbuf.left == 0) && (S3I(s)->wbuf.left == 0) &&
2726 		    !SSL_in_init(s)) {
2727 			/*
2728 			 * If we are the server, and we have sent
2729 			 * a 'RENEGOTIATE' message, we need to go
2730 			 * to SSL_ST_ACCEPT.
2731 			 */
2732 			/* SSL_ST_ACCEPT */
2733 			S3I(s)->hs.state = SSL_ST_RENEGOTIATE;
2734 			S3I(s)->renegotiate = 0;
2735 			S3I(s)->num_renegotiations++;
2736 			S3I(s)->total_renegotiations++;
2737 			ret = 1;
2738 		}
2739 	}
2740 	return (ret);
2741 }
2742