xref: /dragonfly/crypto/libressl/ssl/t1_enc.c (revision 72c33676)
1 /* $OpenBSD: t1_enc.c,v 1.117 2019/02/09 15:26:15 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 2005 Nokia. All rights reserved.
113  *
114  * The portions of the attached software ("Contribution") is developed by
115  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116  * license.
117  *
118  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120  * support (see RFC 4279) to OpenSSL.
121  *
122  * No patent licenses or other rights except those expressly stated in
123  * the OpenSSL open source license shall be deemed granted or received
124  * expressly, by implication, estoppel, or otherwise.
125  *
126  * No assurances are provided by Nokia that the Contribution does not
127  * infringe the patent or other intellectual property rights of any third
128  * party or that the license provides you with all the necessary rights
129  * to make use of the Contribution.
130  *
131  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135  * OTHERWISE.
136  */
137 
138 #include <limits.h>
139 #include <stdio.h>
140 
141 #include "ssl_locl.h"
142 
143 #include <openssl/evp.h>
144 #include <openssl/hmac.h>
145 #include <openssl/md5.h>
146 
147 int tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
148     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
149     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
150     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len);
151 
152 void
153 tls1_cleanup_key_block(SSL *s)
154 {
155 	freezero(S3I(s)->hs.key_block, S3I(s)->hs.key_block_len);
156 	S3I(s)->hs.key_block = NULL;
157 	S3I(s)->hs.key_block_len = 0;
158 }
159 
160 void
161 tls1_record_sequence_increment(unsigned char *seq)
162 {
163 	int i;
164 
165 	for (i = SSL3_SEQUENCE_SIZE - 1; i >= 0; i--) {
166 		if (++seq[i] != 0)
167 			break;
168 	}
169 }
170 
171 /*
172  * TLS P_hash() data expansion function - see RFC 5246, section 5.
173  */
174 static int
175 tls1_P_hash(const EVP_MD *md, const unsigned char *secret, size_t secret_len,
176     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
177     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
178     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len)
179 {
180 	unsigned char A1[EVP_MAX_MD_SIZE], hmac[EVP_MAX_MD_SIZE];
181 	size_t A1_len, hmac_len;
182 	EVP_MD_CTX ctx;
183 	EVP_PKEY *mac_key;
184 	int ret = 0;
185 	int chunk;
186 	size_t i;
187 
188 	chunk = EVP_MD_size(md);
189 	OPENSSL_assert(chunk >= 0);
190 
191 	EVP_MD_CTX_init(&ctx);
192 
193 	mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
194 	if (!mac_key)
195 		goto err;
196 	if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
197 		goto err;
198 	if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
199 		goto err;
200 	if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
201 		goto err;
202 	if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
203 		goto err;
204 	if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
205 		goto err;
206 	if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
207 		goto err;
208 	if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
209 		goto err;
210 
211 	for (;;) {
212 		if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
213 			goto err;
214 		if (!EVP_DigestSignUpdate(&ctx, A1, A1_len))
215 			goto err;
216 		if (seed1 && !EVP_DigestSignUpdate(&ctx, seed1, seed1_len))
217 			goto err;
218 		if (seed2 && !EVP_DigestSignUpdate(&ctx, seed2, seed2_len))
219 			goto err;
220 		if (seed3 && !EVP_DigestSignUpdate(&ctx, seed3, seed3_len))
221 			goto err;
222 		if (seed4 && !EVP_DigestSignUpdate(&ctx, seed4, seed4_len))
223 			goto err;
224 		if (seed5 && !EVP_DigestSignUpdate(&ctx, seed5, seed5_len))
225 			goto err;
226 		if (!EVP_DigestSignFinal(&ctx, hmac, &hmac_len))
227 			goto err;
228 
229 		if (hmac_len > out_len)
230 			hmac_len = out_len;
231 
232 		for (i = 0; i < hmac_len; i++)
233 			out[i] ^= hmac[i];
234 
235 		out += hmac_len;
236 		out_len -= hmac_len;
237 
238 		if (out_len == 0)
239 			break;
240 
241 		if (!EVP_DigestSignInit(&ctx, NULL, md, NULL, mac_key))
242 			goto err;
243 		if (!EVP_DigestSignUpdate(&ctx, A1, A1_len))
244 			goto err;
245 		if (!EVP_DigestSignFinal(&ctx, A1, &A1_len))
246 			goto err;
247 	}
248 	ret = 1;
249 
250  err:
251 	EVP_PKEY_free(mac_key);
252 	EVP_MD_CTX_cleanup(&ctx);
253 
254 	explicit_bzero(A1, sizeof(A1));
255 	explicit_bzero(hmac, sizeof(hmac));
256 
257 	return ret;
258 }
259 
260 int
261 tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
262     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
263     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
264     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len)
265 {
266 	const EVP_MD *md;
267 	size_t half_len;
268 
269 	memset(out, 0, out_len);
270 
271 	if (!ssl_get_handshake_evp_md(s, &md))
272 		return (0);
273 
274 	if (md->type == NID_md5_sha1) {
275 		/*
276 		 * Partition secret between MD5 and SHA1, then XOR result.
277 		 * If the secret length is odd, a one byte overlap is used.
278 		 */
279 		half_len = secret_len - (secret_len / 2);
280 		if (!tls1_P_hash(EVP_md5(), secret, half_len, seed1, seed1_len,
281 		    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
282 		    seed5, seed5_len, out, out_len))
283 			return (0);
284 
285 		secret += secret_len - half_len;
286 		if (!tls1_P_hash(EVP_sha1(), secret, half_len, seed1, seed1_len,
287 		    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
288 		    seed5, seed5_len, out, out_len))
289 			return (0);
290 
291 		return (1);
292 	}
293 
294 	if (!tls1_P_hash(md, secret, secret_len, seed1, seed1_len,
295 	    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
296 	    seed5, seed5_len, out, out_len))
297 		return (0);
298 
299 	return (1);
300 }
301 
302 static int
303 tls1_generate_key_block(SSL *s, unsigned char *km, int num)
304 {
305 	if (num < 0)
306 		return (0);
307 
308 	return tls1_PRF(s,
309 	    s->session->master_key, s->session->master_key_length,
310 	    TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
311 	    s->s3->server_random, SSL3_RANDOM_SIZE,
312 	    s->s3->client_random, SSL3_RANDOM_SIZE,
313 	    NULL, 0, NULL, 0, km, num);
314 }
315 
316 /*
317  * tls1_aead_ctx_init allocates aead_ctx, if needed. It returns 1 on success
318  * and 0 on failure.
319  */
320 static int
321 tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx)
322 {
323 	if (*aead_ctx != NULL) {
324 		EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
325 		return (1);
326 	}
327 
328 	*aead_ctx = malloc(sizeof(SSL_AEAD_CTX));
329 	if (*aead_ctx == NULL) {
330 		SSLerrorx(ERR_R_MALLOC_FAILURE);
331 		return (0);
332 	}
333 
334 	return (1);
335 }
336 
337 static int
338 tls1_change_cipher_state_aead(SSL *s, char is_read, const unsigned char *key,
339     unsigned key_len, const unsigned char *iv, unsigned iv_len)
340 {
341 	const EVP_AEAD *aead = S3I(s)->tmp.new_aead;
342 	SSL_AEAD_CTX *aead_ctx;
343 
344 	if (is_read) {
345 		ssl_clear_cipher_read_state(s);
346 		if (!tls1_aead_ctx_init(&s->internal->aead_read_ctx))
347 			return 0;
348 		aead_ctx = s->internal->aead_read_ctx;
349 	} else {
350 		/* XXX - Need to correctly handle DTLS. */
351 		ssl_clear_cipher_write_state(s);
352 		if (!tls1_aead_ctx_init(&s->internal->aead_write_ctx))
353 			return 0;
354 		aead_ctx = s->internal->aead_write_ctx;
355 	}
356 
357 	if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
358 	    EVP_AEAD_DEFAULT_TAG_LENGTH, NULL))
359 		return (0);
360 	if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
361 		SSLerrorx(ERR_R_INTERNAL_ERROR);
362 		return (0);
363 	}
364 	memcpy(aead_ctx->fixed_nonce, iv, iv_len);
365 	aead_ctx->fixed_nonce_len = iv_len;
366 	aead_ctx->variable_nonce_len = 8;  /* always the case, currently. */
367 	aead_ctx->variable_nonce_in_record =
368 	    (S3I(s)->hs.new_cipher->algorithm2 &
369 	    SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD) != 0;
370 	aead_ctx->xor_fixed_nonce =
371 	    S3I(s)->hs.new_cipher->algorithm_enc == SSL_CHACHA20POLY1305;
372 	aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
373 
374 	if (aead_ctx->xor_fixed_nonce) {
375 		if (aead_ctx->fixed_nonce_len != EVP_AEAD_nonce_length(aead) ||
376 		    aead_ctx->variable_nonce_len > EVP_AEAD_nonce_length(aead)) {
377 			SSLerrorx(ERR_R_INTERNAL_ERROR);
378 			return (0);
379 		}
380 	} else {
381 		if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
382 		    EVP_AEAD_nonce_length(aead)) {
383 			SSLerrorx(ERR_R_INTERNAL_ERROR);
384 			return (0);
385 		}
386 	}
387 
388 	return (1);
389 }
390 
391 /*
392  * tls1_change_cipher_state_cipher performs the work needed to switch cipher
393  * states when using EVP_CIPHER. The argument is_read is true iff this function
394  * is being called due to reading, as opposed to writing, a ChangeCipherSpec
395  * message.
396  */
397 static int
398 tls1_change_cipher_state_cipher(SSL *s, char is_read,
399     const unsigned char *mac_secret, unsigned int mac_secret_size,
400     const unsigned char *key, unsigned int key_len, const unsigned char *iv,
401     unsigned int iv_len)
402 {
403 	EVP_CIPHER_CTX *cipher_ctx;
404 	const EVP_CIPHER *cipher;
405 	EVP_MD_CTX *mac_ctx;
406 	EVP_PKEY *mac_key;
407 	const EVP_MD *mac;
408 	int mac_type;
409 
410 	cipher = S3I(s)->tmp.new_sym_enc;
411 	mac = S3I(s)->tmp.new_hash;
412 	mac_type = S3I(s)->tmp.new_mac_pkey_type;
413 
414 	if (is_read) {
415 		if (S3I(s)->hs.new_cipher->algorithm2 & TLS1_STREAM_MAC)
416 			s->internal->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
417 		else
418 			s->internal->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
419 
420 		ssl_clear_cipher_read_state(s);
421 
422 		if ((cipher_ctx = EVP_CIPHER_CTX_new()) == NULL)
423 			goto err;
424 		s->enc_read_ctx = cipher_ctx;
425 		if ((mac_ctx = EVP_MD_CTX_new()) == NULL)
426 			goto err;
427 		s->read_hash = mac_ctx;
428 	} else {
429 		if (S3I(s)->hs.new_cipher->algorithm2 & TLS1_STREAM_MAC)
430 			s->internal->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
431 		else
432 			s->internal->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
433 
434 		/*
435 		 * DTLS fragments retain a pointer to the compression, cipher
436 		 * and hash contexts, so that it can restore state in order
437 		 * to perform retransmissions. As such, we cannot free write
438 		 * contexts that are used for DTLS - these are instead freed
439 		 * by DTLS when its frees a ChangeCipherSpec fragment.
440 		 */
441 		if (!SSL_IS_DTLS(s))
442 			ssl_clear_cipher_write_state(s);
443 
444 		if ((cipher_ctx = EVP_CIPHER_CTX_new()) == NULL)
445 			goto err;
446 		s->internal->enc_write_ctx = cipher_ctx;
447 		if ((mac_ctx = EVP_MD_CTX_new()) == NULL)
448 			goto err;
449 		s->internal->write_hash = mac_ctx;
450 	}
451 
452 	EVP_CipherInit_ex(cipher_ctx, cipher, NULL, key, iv, !is_read);
453 
454 	if ((mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
455 	    mac_secret_size)) == NULL)
456 		goto err;
457 	EVP_DigestSignInit(mac_ctx, NULL, mac, NULL, mac_key);
458 	EVP_PKEY_free(mac_key);
459 
460 	if (S3I(s)->hs.new_cipher->algorithm_enc == SSL_eGOST2814789CNT) {
461 		int nid;
462 		if (S3I(s)->hs.new_cipher->algorithm2 & SSL_HANDSHAKE_MAC_GOST94)
463 			nid = NID_id_Gost28147_89_CryptoPro_A_ParamSet;
464 		else
465 			nid = NID_id_tc26_gost_28147_param_Z;
466 
467 		EVP_CIPHER_CTX_ctrl(cipher_ctx, EVP_CTRL_GOST_SET_SBOX, nid, 0);
468 		if (S3I(s)->hs.new_cipher->algorithm_mac == SSL_GOST89MAC)
469 			EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_GOST_SET_SBOX, nid, 0);
470 	}
471 
472 	return (1);
473 
474 err:
475 	SSLerrorx(ERR_R_MALLOC_FAILURE);
476 	return (0);
477 }
478 
479 int
480 tls1_change_cipher_state(SSL *s, int which)
481 {
482 	const unsigned char *client_write_mac_secret, *server_write_mac_secret;
483 	const unsigned char *client_write_key, *server_write_key;
484 	const unsigned char *client_write_iv, *server_write_iv;
485 	const unsigned char *mac_secret, *key, *iv;
486 	int mac_secret_size, key_len, iv_len;
487 	unsigned char *key_block, *seq;
488 	const EVP_CIPHER *cipher;
489 	const EVP_AEAD *aead;
490 	char is_read, use_client_keys;
491 
492 	cipher = S3I(s)->tmp.new_sym_enc;
493 	aead = S3I(s)->tmp.new_aead;
494 
495 	/*
496 	 * is_read is true if we have just read a ChangeCipherSpec message,
497 	 * that is we need to update the read cipherspec. Otherwise we have
498 	 * just written one.
499 	 */
500 	is_read = (which & SSL3_CC_READ) != 0;
501 
502 	/*
503 	 * use_client_keys is true if we wish to use the keys for the "client
504 	 * write" direction. This is the case if we're a client sending a
505 	 * ChangeCipherSpec, or a server reading a client's ChangeCipherSpec.
506 	 */
507 	use_client_keys = ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
508 	    (which == SSL3_CHANGE_CIPHER_SERVER_READ));
509 
510 	/*
511 	 * Reset sequence number to zero - for DTLS this is handled in
512 	 * dtls1_reset_seq_numbers().
513 	 */
514 	if (!SSL_IS_DTLS(s)) {
515 		seq = is_read ? S3I(s)->read_sequence : S3I(s)->write_sequence;
516 		memset(seq, 0, SSL3_SEQUENCE_SIZE);
517 	}
518 
519 	if (aead != NULL) {
520 		key_len = EVP_AEAD_key_length(aead);
521 		iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(S3I(s)->hs.new_cipher);
522 	} else {
523 		key_len = EVP_CIPHER_key_length(cipher);
524 		iv_len = EVP_CIPHER_iv_length(cipher);
525 	}
526 
527 	mac_secret_size = S3I(s)->tmp.new_mac_secret_size;
528 
529 	key_block = S3I(s)->hs.key_block;
530 	client_write_mac_secret = key_block;
531 	key_block += mac_secret_size;
532 	server_write_mac_secret = key_block;
533 	key_block += mac_secret_size;
534 	client_write_key = key_block;
535 	key_block += key_len;
536 	server_write_key = key_block;
537 	key_block += key_len;
538 	client_write_iv = key_block;
539 	key_block += iv_len;
540 	server_write_iv = key_block;
541 	key_block += iv_len;
542 
543 	if (use_client_keys) {
544 		mac_secret = client_write_mac_secret;
545 		key = client_write_key;
546 		iv = client_write_iv;
547 	} else {
548 		mac_secret = server_write_mac_secret;
549 		key = server_write_key;
550 		iv = server_write_iv;
551 	}
552 
553 	if (key_block - S3I(s)->hs.key_block != S3I(s)->hs.key_block_len) {
554 		SSLerror(s, ERR_R_INTERNAL_ERROR);
555 		goto err2;
556 	}
557 
558 	if (is_read) {
559 		memcpy(S3I(s)->read_mac_secret, mac_secret, mac_secret_size);
560 		S3I(s)->read_mac_secret_size = mac_secret_size;
561 	} else {
562 		memcpy(S3I(s)->write_mac_secret, mac_secret, mac_secret_size);
563 		S3I(s)->write_mac_secret_size = mac_secret_size;
564 	}
565 
566 	if (aead != NULL) {
567 		return tls1_change_cipher_state_aead(s, is_read, key, key_len,
568 		    iv, iv_len);
569 	}
570 
571 	return tls1_change_cipher_state_cipher(s, is_read,
572 	    mac_secret, mac_secret_size, key, key_len, iv, iv_len);
573 
574 err2:
575 	return (0);
576 }
577 
578 int
579 tls1_setup_key_block(SSL *s)
580 {
581 	unsigned char *key_block;
582 	int mac_type = NID_undef, mac_secret_size = 0;
583 	int key_block_len, key_len, iv_len;
584 	const EVP_CIPHER *cipher = NULL;
585 	const EVP_AEAD *aead = NULL;
586 	const EVP_MD *mac = NULL;
587 	int ret = 0;
588 
589 	if (S3I(s)->hs.key_block_len != 0)
590 		return (1);
591 
592 	if (s->session->cipher &&
593 	    (s->session->cipher->algorithm_mac & SSL_AEAD)) {
594 		if (!ssl_cipher_get_evp_aead(s->session, &aead)) {
595 			SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
596 			return (0);
597 		}
598 		key_len = EVP_AEAD_key_length(aead);
599 		iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher);
600 	} else {
601 		if (!ssl_cipher_get_evp(s->session, &cipher, &mac, &mac_type,
602 		    &mac_secret_size)) {
603 			SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
604 			return (0);
605 		}
606 		key_len = EVP_CIPHER_key_length(cipher);
607 		iv_len = EVP_CIPHER_iv_length(cipher);
608 	}
609 
610 	S3I(s)->tmp.new_aead = aead;
611 	S3I(s)->tmp.new_sym_enc = cipher;
612 	S3I(s)->tmp.new_hash = mac;
613 	S3I(s)->tmp.new_mac_pkey_type = mac_type;
614 	S3I(s)->tmp.new_mac_secret_size = mac_secret_size;
615 
616 	tls1_cleanup_key_block(s);
617 
618 	if ((key_block = reallocarray(NULL, mac_secret_size + key_len + iv_len,
619 	    2)) == NULL) {
620 		SSLerror(s, ERR_R_MALLOC_FAILURE);
621 		goto err;
622 	}
623 	key_block_len = (mac_secret_size + key_len + iv_len) * 2;
624 
625 	S3I(s)->hs.key_block_len = key_block_len;
626 	S3I(s)->hs.key_block = key_block;
627 
628 	if (!tls1_generate_key_block(s, key_block, key_block_len))
629 		goto err;
630 
631 	if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) &&
632 	    s->method->internal->version <= TLS1_VERSION) {
633 		/*
634 		 * Enable vulnerability countermeasure for CBC ciphers with
635 		 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
636 		 */
637 		S3I(s)->need_empty_fragments = 1;
638 
639 		if (s->session->cipher != NULL) {
640 			if (s->session->cipher->algorithm_enc == SSL_eNULL)
641 				S3I(s)->need_empty_fragments = 0;
642 
643 #ifndef OPENSSL_NO_RC4
644 			if (s->session->cipher->algorithm_enc == SSL_RC4)
645 				S3I(s)->need_empty_fragments = 0;
646 #endif
647 		}
648 	}
649 
650 	ret = 1;
651 
652  err:
653 	return (ret);
654 }
655 
656 /* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
657  *
658  * Returns:
659  *   0: (in non-constant time) if the record is publically invalid (i.e. too
660  *       short etc).
661  *   1: if the record's padding is valid / the encryption was successful.
662  *   -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
663  *       an internal error occured.
664  */
665 int
666 tls1_enc(SSL *s, int send)
667 {
668 	const SSL_AEAD_CTX *aead;
669 	const EVP_CIPHER *enc;
670 	EVP_CIPHER_CTX *ds;
671 	SSL3_RECORD *rec;
672 	unsigned char *seq;
673 	unsigned long l;
674 	int bs, i, j, k, pad = 0, ret, mac_size = 0;
675 
676 	if (send) {
677 		aead = s->internal->aead_write_ctx;
678 		rec = &S3I(s)->wrec;
679 		seq = S3I(s)->write_sequence;
680 	} else {
681 		aead = s->internal->aead_read_ctx;
682 		rec = &S3I(s)->rrec;
683 		seq = S3I(s)->read_sequence;
684 	}
685 
686 	if (aead) {
687 		unsigned char ad[13], *in, *out, nonce[16];
688 		size_t out_len, pad_len = 0;
689 		unsigned int nonce_used;
690 
691 		if (SSL_IS_DTLS(s)) {
692 			dtls1_build_sequence_number(ad, seq,
693 			    send ? D1I(s)->w_epoch : D1I(s)->r_epoch);
694 		} else {
695 			memcpy(ad, seq, SSL3_SEQUENCE_SIZE);
696 			tls1_record_sequence_increment(seq);
697 		}
698 
699 		ad[8] = rec->type;
700 		ad[9] = (unsigned char)(s->version >> 8);
701 		ad[10] = (unsigned char)(s->version);
702 
703 		if (aead->variable_nonce_len > 8 ||
704 		    aead->variable_nonce_len > sizeof(nonce))
705 			return -1;
706 
707 		if (aead->xor_fixed_nonce) {
708 			if (aead->fixed_nonce_len > sizeof(nonce) ||
709 			    aead->variable_nonce_len > aead->fixed_nonce_len)
710 				return -1;  /* Should never happen. */
711 			pad_len = aead->fixed_nonce_len - aead->variable_nonce_len;
712 		} else {
713 			if (aead->fixed_nonce_len +
714 			    aead->variable_nonce_len > sizeof(nonce))
715 				return -1;  /* Should never happen. */
716 		}
717 
718 		if (send) {
719 			size_t len = rec->length;
720 			size_t eivlen = 0;
721 			in = rec->input;
722 			out = rec->data;
723 
724 			if (aead->xor_fixed_nonce) {
725 				/*
726 				 * The sequence number is left zero
727 				 * padded, then xored with the fixed
728 				 * nonce.
729 				 */
730 				memset(nonce, 0, pad_len);
731 				memcpy(nonce + pad_len, ad,
732 				    aead->variable_nonce_len);
733 				for (i = 0; i < aead->fixed_nonce_len; i++)
734 					nonce[i] ^= aead->fixed_nonce[i];
735 				nonce_used = aead->fixed_nonce_len;
736 			} else {
737 				/*
738 				 * When sending we use the sequence number as
739 				 * the variable part of the nonce.
740 				 */
741 				memcpy(nonce, aead->fixed_nonce,
742 				    aead->fixed_nonce_len);
743 				nonce_used = aead->fixed_nonce_len;
744 				memcpy(nonce + nonce_used, ad,
745 				    aead->variable_nonce_len);
746 				nonce_used += aead->variable_nonce_len;
747 			}
748 
749 			/*
750 			 * In do_ssl3_write, rec->input is moved forward by
751 			 * variable_nonce_len in order to leave space for the
752 			 * variable nonce. Thus we can copy the sequence number
753 			 * bytes into place without overwriting any of the
754 			 * plaintext.
755 			 */
756 			if (aead->variable_nonce_in_record) {
757 				memcpy(out, ad, aead->variable_nonce_len);
758 				len -= aead->variable_nonce_len;
759 				eivlen = aead->variable_nonce_len;
760 			}
761 
762 			ad[11] = len >> 8;
763 			ad[12] = len & 0xff;
764 
765 			if (!EVP_AEAD_CTX_seal(&aead->ctx,
766 			    out + eivlen, &out_len, len + aead->tag_len, nonce,
767 			    nonce_used, in + eivlen, len, ad, sizeof(ad)))
768 				return -1;
769 			if (aead->variable_nonce_in_record)
770 				out_len += aead->variable_nonce_len;
771 		} else {
772 			/* receive */
773 			size_t len = rec->length;
774 
775 			if (rec->data != rec->input)
776 				return -1;  /* internal error - should never happen. */
777 			out = in = rec->input;
778 
779 			if (len < aead->variable_nonce_len)
780 				return 0;
781 
782 			if (aead->xor_fixed_nonce) {
783 				/*
784 				 * The sequence number is left zero
785 				 * padded, then xored with the fixed
786 				 * nonce.
787 				 */
788 				memset(nonce, 0, pad_len);
789 				memcpy(nonce + pad_len, ad,
790 				    aead->variable_nonce_len);
791 				for (i = 0; i < aead->fixed_nonce_len; i++)
792 					nonce[i] ^= aead->fixed_nonce[i];
793 				nonce_used = aead->fixed_nonce_len;
794 			} else {
795 				memcpy(nonce, aead->fixed_nonce,
796 				    aead->fixed_nonce_len);
797 				nonce_used = aead->fixed_nonce_len;
798 
799 				memcpy(nonce + nonce_used,
800 				    aead->variable_nonce_in_record ? in : ad,
801 				    aead->variable_nonce_len);
802 				nonce_used += aead->variable_nonce_len;
803 			}
804 
805 			if (aead->variable_nonce_in_record) {
806 				in += aead->variable_nonce_len;
807 				len -= aead->variable_nonce_len;
808 				out += aead->variable_nonce_len;
809 			}
810 
811 			if (len < aead->tag_len)
812 				return 0;
813 			len -= aead->tag_len;
814 
815 			ad[11] = len >> 8;
816 			ad[12] = len & 0xff;
817 
818 			if (!EVP_AEAD_CTX_open(&aead->ctx, out, &out_len, len,
819 			    nonce, nonce_used, in, len + aead->tag_len, ad,
820 			    sizeof(ad)))
821 				return -1;
822 
823 			rec->data = rec->input = out;
824 		}
825 
826 		rec->length = out_len;
827 
828 		return 1;
829 	}
830 
831 	if (send) {
832 		if (EVP_MD_CTX_md(s->internal->write_hash)) {
833 			int n = EVP_MD_CTX_size(s->internal->write_hash);
834 			OPENSSL_assert(n >= 0);
835 		}
836 		ds = s->internal->enc_write_ctx;
837 		if (s->internal->enc_write_ctx == NULL)
838 			enc = NULL;
839 		else {
840 			int ivlen = 0;
841 			enc = EVP_CIPHER_CTX_cipher(s->internal->enc_write_ctx);
842 			if (SSL_USE_EXPLICIT_IV(s) &&
843 			    EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
844 				ivlen = EVP_CIPHER_iv_length(enc);
845 			if (ivlen > 1) {
846 				if (rec->data != rec->input) {
847 #ifdef DEBUG
848 					/* we can't write into the input stream:
849 					 * Can this ever happen?? (steve)
850 					 */
851 					fprintf(stderr,
852 					    "%s:%d: rec->data != rec->input\n",
853 					    __FILE__, __LINE__);
854 #endif
855 				} else
856 					arc4random_buf(rec->input, ivlen);
857 			}
858 		}
859 	} else {
860 		if (EVP_MD_CTX_md(s->read_hash)) {
861 			int n = EVP_MD_CTX_size(s->read_hash);
862 			OPENSSL_assert(n >= 0);
863 		}
864 		ds = s->enc_read_ctx;
865 		if (s->enc_read_ctx == NULL)
866 			enc = NULL;
867 		else
868 			enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
869 	}
870 
871 	if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
872 		memmove(rec->data, rec->input, rec->length);
873 		rec->input = rec->data;
874 		ret = 1;
875 	} else {
876 		l = rec->length;
877 		bs = EVP_CIPHER_block_size(ds->cipher);
878 
879 		if (bs != 1 && send) {
880 			i = bs - ((int)l % bs);
881 
882 			/* Add weird padding of upto 256 bytes */
883 
884 			/* we need to add 'i' padding bytes of value j */
885 			j = i - 1;
886 			for (k = (int)l; k < (int)(l + i); k++)
887 				rec->input[k] = j;
888 			l += i;
889 			rec->length += i;
890 		}
891 
892 		if (!send) {
893 			if (l == 0 || l % bs != 0)
894 				return 0;
895 		}
896 
897 		i = EVP_Cipher(ds, rec->data, rec->input, l);
898 		if ((EVP_CIPHER_flags(ds->cipher) &
899 		    EVP_CIPH_FLAG_CUSTOM_CIPHER) ? (i < 0) : (i == 0))
900 			return -1;	/* AEAD can fail to verify MAC */
901 
902 		ret = 1;
903 		if (EVP_MD_CTX_md(s->read_hash) != NULL)
904 			mac_size = EVP_MD_CTX_size(s->read_hash);
905 		if ((bs != 1) && !send)
906 			ret = tls1_cbc_remove_padding(s, rec, bs, mac_size);
907 		if (pad && !send)
908 			rec->length -= pad;
909 	}
910 	return ret;
911 }
912 
913 int
914 tls1_final_finish_mac(SSL *s, const char *str, int str_len, unsigned char *out)
915 {
916 	unsigned char buf[EVP_MAX_MD_SIZE];
917 	size_t hash_len;
918 
919 	if (str_len < 0)
920 		return 0;
921 
922 	if (!tls1_transcript_hash_value(s, buf, sizeof(buf), &hash_len))
923 		return 0;
924 
925 	if (!tls1_PRF(s, s->session->master_key, s->session->master_key_length,
926 	    str, str_len, buf, hash_len, NULL, 0, NULL, 0, NULL, 0,
927 	    out, TLS1_FINISH_MAC_LENGTH))
928 		return 0;
929 
930 	return TLS1_FINISH_MAC_LENGTH;
931 }
932 
933 int
934 tls1_mac(SSL *ssl, unsigned char *md, int send)
935 {
936 	SSL3_RECORD *rec;
937 	unsigned char *seq;
938 	EVP_MD_CTX *hash;
939 	size_t md_size, orig_len;
940 	EVP_MD_CTX hmac, *mac_ctx;
941 	unsigned char header[13];
942 	int stream_mac = (send ?
943 	    (ssl->internal->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) :
944 	    (ssl->internal->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
945 	int t;
946 
947 	if (send) {
948 		rec = &(ssl->s3->internal->wrec);
949 		seq = &(ssl->s3->internal->write_sequence[0]);
950 		hash = ssl->internal->write_hash;
951 	} else {
952 		rec = &(ssl->s3->internal->rrec);
953 		seq = &(ssl->s3->internal->read_sequence[0]);
954 		hash = ssl->read_hash;
955 	}
956 
957 	t = EVP_MD_CTX_size(hash);
958 	OPENSSL_assert(t >= 0);
959 	md_size = t;
960 
961 	/* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
962 	if (stream_mac) {
963 		mac_ctx = hash;
964 	} else {
965 		if (!EVP_MD_CTX_copy(&hmac, hash))
966 			return -1;
967 		mac_ctx = &hmac;
968 	}
969 
970 	if (SSL_IS_DTLS(ssl))
971 		dtls1_build_sequence_number(header, seq,
972 		    send ? D1I(ssl)->w_epoch : D1I(ssl)->r_epoch);
973 	else
974 		memcpy(header, seq, SSL3_SEQUENCE_SIZE);
975 
976 	/* kludge: tls1_cbc_remove_padding passes padding length in rec->type */
977 	orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8);
978 	rec->type &= 0xff;
979 
980 	header[8] = rec->type;
981 	header[9] = (unsigned char)(ssl->version >> 8);
982 	header[10] = (unsigned char)(ssl->version);
983 	header[11] = (rec->length) >> 8;
984 	header[12] = (rec->length) & 0xff;
985 
986 	if (!send &&
987 	    EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
988 	    ssl3_cbc_record_digest_supported(mac_ctx)) {
989 		/* This is a CBC-encrypted record. We must avoid leaking any
990 		 * timing-side channel information about how many blocks of
991 		 * data we are hashing because that gives an attacker a
992 		 * timing-oracle. */
993 		if (!ssl3_cbc_digest_record(mac_ctx,
994 		    md, &md_size, header, rec->input,
995 		    rec->length + md_size, orig_len,
996 		    ssl->s3->internal->read_mac_secret,
997 		    ssl->s3->internal->read_mac_secret_size))
998 			return -1;
999 	} else {
1000 		EVP_DigestSignUpdate(mac_ctx, header, sizeof(header));
1001 		EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length);
1002 		t = EVP_DigestSignFinal(mac_ctx, md, &md_size);
1003 		OPENSSL_assert(t > 0);
1004 	}
1005 
1006 	if (!stream_mac)
1007 		EVP_MD_CTX_cleanup(&hmac);
1008 
1009 	if (!SSL_IS_DTLS(ssl))
1010 		tls1_record_sequence_increment(seq);
1011 
1012 	return (md_size);
1013 }
1014 
1015 int
1016 tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
1017     int len)
1018 {
1019 	if (len < 0)
1020 		return 0;
1021 
1022 	if (!tls1_PRF(s, p, len,
1023 	    TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
1024 	    s->s3->client_random, SSL3_RANDOM_SIZE, NULL, 0,
1025 	    s->s3->server_random, SSL3_RANDOM_SIZE, NULL, 0,
1026 	    s->session->master_key, SSL_MAX_MASTER_KEY_LENGTH))
1027 		return 0;
1028 
1029 	return (SSL_MAX_MASTER_KEY_LENGTH);
1030 }
1031 
1032 int
1033 tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1034     const char *label, size_t llen, const unsigned char *context,
1035     size_t contextlen, int use_context)
1036 {
1037 	unsigned char *val = NULL;
1038 	size_t vallen, currentvalpos;
1039 	int rv;
1040 
1041 	/* construct PRF arguments
1042 	 * we construct the PRF argument ourself rather than passing separate
1043 	 * values into the TLS PRF to ensure that the concatenation of values
1044 	 * does not create a prohibited label.
1045 	 */
1046 	vallen = llen + SSL3_RANDOM_SIZE * 2;
1047 	if (use_context) {
1048 		vallen += 2 + contextlen;
1049 	}
1050 
1051 	val = malloc(vallen);
1052 	if (val == NULL)
1053 		goto err2;
1054 	currentvalpos = 0;
1055 	memcpy(val + currentvalpos, (unsigned char *) label, llen);
1056 	currentvalpos += llen;
1057 	memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
1058 	currentvalpos += SSL3_RANDOM_SIZE;
1059 	memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
1060 	currentvalpos += SSL3_RANDOM_SIZE;
1061 
1062 	if (use_context) {
1063 		val[currentvalpos] = (contextlen >> 8) & 0xff;
1064 		currentvalpos++;
1065 		val[currentvalpos] = contextlen & 0xff;
1066 		currentvalpos++;
1067 		if ((contextlen > 0) || (context != NULL)) {
1068 			memcpy(val + currentvalpos, context, contextlen);
1069 		}
1070 	}
1071 
1072 	/* disallow prohibited labels
1073 	 * note that SSL3_RANDOM_SIZE > max(prohibited label len) =
1074 	 * 15, so size of val > max(prohibited label len) = 15 and the
1075 	 * comparisons won't have buffer overflow
1076 	 */
1077 	if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
1078 	    TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
1079 		goto err1;
1080 	if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
1081 	    TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
1082 		goto err1;
1083 	if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
1084 	    TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
1085 		goto err1;
1086 	if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
1087 	    TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
1088 		goto err1;
1089 
1090 	rv = tls1_PRF(s, s->session->master_key, s->session->master_key_length,
1091 	    val, vallen, NULL, 0, NULL, 0, NULL, 0, NULL, 0, out, olen);
1092 
1093 	goto ret;
1094 err1:
1095 	SSLerror(s, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
1096 	rv = 0;
1097 	goto ret;
1098 err2:
1099 	SSLerror(s, ERR_R_MALLOC_FAILURE);
1100 	rv = 0;
1101 ret:
1102 	free(val);
1103 
1104 	return (rv);
1105 }
1106 
1107 int
1108 tls1_alert_code(int code)
1109 {
1110 	switch (code) {
1111 	case SSL_AD_CLOSE_NOTIFY:
1112 		return (SSL3_AD_CLOSE_NOTIFY);
1113 	case SSL_AD_UNEXPECTED_MESSAGE:
1114 		return (SSL3_AD_UNEXPECTED_MESSAGE);
1115 	case SSL_AD_BAD_RECORD_MAC:
1116 		return (SSL3_AD_BAD_RECORD_MAC);
1117 	case SSL_AD_DECRYPTION_FAILED:
1118 		return (TLS1_AD_DECRYPTION_FAILED);
1119 	case SSL_AD_RECORD_OVERFLOW:
1120 		return (TLS1_AD_RECORD_OVERFLOW);
1121 	case SSL_AD_DECOMPRESSION_FAILURE:
1122 		return (SSL3_AD_DECOMPRESSION_FAILURE);
1123 	case SSL_AD_HANDSHAKE_FAILURE:
1124 		return (SSL3_AD_HANDSHAKE_FAILURE);
1125 	case SSL_AD_NO_CERTIFICATE:
1126 		return (-1);
1127 	case SSL_AD_BAD_CERTIFICATE:
1128 		return (SSL3_AD_BAD_CERTIFICATE);
1129 	case SSL_AD_UNSUPPORTED_CERTIFICATE:
1130 		return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
1131 	case SSL_AD_CERTIFICATE_REVOKED:
1132 		return (SSL3_AD_CERTIFICATE_REVOKED);
1133 	case SSL_AD_CERTIFICATE_EXPIRED:
1134 		return (SSL3_AD_CERTIFICATE_EXPIRED);
1135 	case SSL_AD_CERTIFICATE_UNKNOWN:
1136 		return (SSL3_AD_CERTIFICATE_UNKNOWN);
1137 	case SSL_AD_ILLEGAL_PARAMETER:
1138 		return (SSL3_AD_ILLEGAL_PARAMETER);
1139 	case SSL_AD_UNKNOWN_CA:
1140 		return (TLS1_AD_UNKNOWN_CA);
1141 	case SSL_AD_ACCESS_DENIED:
1142 		return (TLS1_AD_ACCESS_DENIED);
1143 	case SSL_AD_DECODE_ERROR:
1144 		return (TLS1_AD_DECODE_ERROR);
1145 	case SSL_AD_DECRYPT_ERROR:
1146 		return (TLS1_AD_DECRYPT_ERROR);
1147 	case SSL_AD_EXPORT_RESTRICTION:
1148 		return (TLS1_AD_EXPORT_RESTRICTION);
1149 	case SSL_AD_PROTOCOL_VERSION:
1150 		return (TLS1_AD_PROTOCOL_VERSION);
1151 	case SSL_AD_INSUFFICIENT_SECURITY:
1152 		return (TLS1_AD_INSUFFICIENT_SECURITY);
1153 	case SSL_AD_INTERNAL_ERROR:
1154 		return (TLS1_AD_INTERNAL_ERROR);
1155 	case SSL_AD_INAPPROPRIATE_FALLBACK:
1156 		return(TLS1_AD_INAPPROPRIATE_FALLBACK);
1157 	case SSL_AD_USER_CANCELLED:
1158 		return (TLS1_AD_USER_CANCELLED);
1159 	case SSL_AD_NO_RENEGOTIATION:
1160 		return (TLS1_AD_NO_RENEGOTIATION);
1161 	case SSL_AD_UNSUPPORTED_EXTENSION:
1162 		return (TLS1_AD_UNSUPPORTED_EXTENSION);
1163 	case SSL_AD_CERTIFICATE_UNOBTAINABLE:
1164 		return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
1165 	case SSL_AD_UNRECOGNIZED_NAME:
1166 		return (TLS1_AD_UNRECOGNIZED_NAME);
1167 	case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
1168 		return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
1169 	case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
1170 		return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
1171 	case SSL_AD_UNKNOWN_PSK_IDENTITY:
1172 		return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
1173 	default:
1174 		return (-1);
1175 	}
1176 }
1177