xref: /dflybsd-src/crypto/libressl/ssl/t1_enc.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: t1_enc.c,v 1.154 2022/02/05 14:54:10 jsing Exp $ */
2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3f5b1c8a1SJohn Marino  * All rights reserved.
4f5b1c8a1SJohn Marino  *
5f5b1c8a1SJohn Marino  * This package is an SSL implementation written
6f5b1c8a1SJohn Marino  * by Eric Young (eay@cryptsoft.com).
7f5b1c8a1SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
8f5b1c8a1SJohn Marino  *
9f5b1c8a1SJohn Marino  * This library is free for commercial and non-commercial use as long as
10f5b1c8a1SJohn Marino  * the following conditions are aheared to.  The following conditions
11f5b1c8a1SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
12f5b1c8a1SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13f5b1c8a1SJohn Marino  * included with this distribution is covered by the same copyright terms
14f5b1c8a1SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15f5b1c8a1SJohn Marino  *
16f5b1c8a1SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
17f5b1c8a1SJohn Marino  * the code are not to be removed.
18f5b1c8a1SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
19f5b1c8a1SJohn Marino  * as the author of the parts of the library used.
20f5b1c8a1SJohn Marino  * This can be in the form of a textual message at program startup or
21f5b1c8a1SJohn Marino  * in documentation (online or textual) provided with the package.
22f5b1c8a1SJohn Marino  *
23f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
24f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
25f5b1c8a1SJohn Marino  * are met:
26f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the copyright
27f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
28f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
29f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
30f5b1c8a1SJohn Marino  *    documentation and/or other materials provided with the distribution.
31f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this software
32f5b1c8a1SJohn Marino  *    must display the following acknowledgement:
33f5b1c8a1SJohn Marino  *    "This product includes cryptographic software written by
34f5b1c8a1SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
35f5b1c8a1SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
36f5b1c8a1SJohn Marino  *    being used are not cryptographic related :-).
37f5b1c8a1SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
38f5b1c8a1SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
39f5b1c8a1SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40f5b1c8a1SJohn Marino  *
41f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42f5b1c8a1SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44f5b1c8a1SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45f5b1c8a1SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46f5b1c8a1SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47f5b1c8a1SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49f5b1c8a1SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50f5b1c8a1SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51f5b1c8a1SJohn Marino  * SUCH DAMAGE.
52f5b1c8a1SJohn Marino  *
53f5b1c8a1SJohn Marino  * The licence and distribution terms for any publically available version or
54f5b1c8a1SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55f5b1c8a1SJohn Marino  * copied and put under another distribution licence
56f5b1c8a1SJohn Marino  * [including the GNU Public Licence.]
57f5b1c8a1SJohn Marino  */
58f5b1c8a1SJohn Marino /* ====================================================================
59f5b1c8a1SJohn Marino  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60f5b1c8a1SJohn Marino  *
61f5b1c8a1SJohn Marino  * Redistribution and use in source and binary forms, with or without
62f5b1c8a1SJohn Marino  * modification, are permitted provided that the following conditions
63f5b1c8a1SJohn Marino  * are met:
64f5b1c8a1SJohn Marino  *
65f5b1c8a1SJohn Marino  * 1. Redistributions of source code must retain the above copyright
66f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer.
67f5b1c8a1SJohn Marino  *
68f5b1c8a1SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
69f5b1c8a1SJohn Marino  *    notice, this list of conditions and the following disclaimer in
70f5b1c8a1SJohn Marino  *    the documentation and/or other materials provided with the
71f5b1c8a1SJohn Marino  *    distribution.
72f5b1c8a1SJohn Marino  *
73f5b1c8a1SJohn Marino  * 3. All advertising materials mentioning features or use of this
74f5b1c8a1SJohn Marino  *    software must display the following acknowledgment:
75f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
76f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77f5b1c8a1SJohn Marino  *
78f5b1c8a1SJohn Marino  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79f5b1c8a1SJohn Marino  *    endorse or promote products derived from this software without
80f5b1c8a1SJohn Marino  *    prior written permission. For written permission, please contact
81f5b1c8a1SJohn Marino  *    openssl-core@openssl.org.
82f5b1c8a1SJohn Marino  *
83f5b1c8a1SJohn Marino  * 5. Products derived from this software may not be called "OpenSSL"
84f5b1c8a1SJohn Marino  *    nor may "OpenSSL" appear in their names without prior written
85f5b1c8a1SJohn Marino  *    permission of the OpenSSL Project.
86f5b1c8a1SJohn Marino  *
87f5b1c8a1SJohn Marino  * 6. Redistributions of any form whatsoever must retain the following
88f5b1c8a1SJohn Marino  *    acknowledgment:
89f5b1c8a1SJohn Marino  *    "This product includes software developed by the OpenSSL Project
90f5b1c8a1SJohn Marino  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91f5b1c8a1SJohn Marino  *
92f5b1c8a1SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93f5b1c8a1SJohn Marino  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94f5b1c8a1SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95f5b1c8a1SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96f5b1c8a1SJohn Marino  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97f5b1c8a1SJohn Marino  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98f5b1c8a1SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99f5b1c8a1SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100f5b1c8a1SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101f5b1c8a1SJohn Marino  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102f5b1c8a1SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103f5b1c8a1SJohn Marino  * OF THE POSSIBILITY OF SUCH DAMAGE.
104f5b1c8a1SJohn Marino  * ====================================================================
105f5b1c8a1SJohn Marino  *
106f5b1c8a1SJohn Marino  * This product includes cryptographic software written by Eric Young
107f5b1c8a1SJohn Marino  * (eay@cryptsoft.com).  This product includes software written by Tim
108f5b1c8a1SJohn Marino  * Hudson (tjh@cryptsoft.com).
109f5b1c8a1SJohn Marino  *
110f5b1c8a1SJohn Marino  */
111f5b1c8a1SJohn Marino /* ====================================================================
112f5b1c8a1SJohn Marino  * Copyright 2005 Nokia. All rights reserved.
113f5b1c8a1SJohn Marino  *
114f5b1c8a1SJohn Marino  * The portions of the attached software ("Contribution") is developed by
115f5b1c8a1SJohn Marino  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116f5b1c8a1SJohn Marino  * license.
117f5b1c8a1SJohn Marino  *
118f5b1c8a1SJohn Marino  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119f5b1c8a1SJohn Marino  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120f5b1c8a1SJohn Marino  * support (see RFC 4279) to OpenSSL.
121f5b1c8a1SJohn Marino  *
122f5b1c8a1SJohn Marino  * No patent licenses or other rights except those expressly stated in
123f5b1c8a1SJohn Marino  * the OpenSSL open source license shall be deemed granted or received
124f5b1c8a1SJohn Marino  * expressly, by implication, estoppel, or otherwise.
125f5b1c8a1SJohn Marino  *
126f5b1c8a1SJohn Marino  * No assurances are provided by Nokia that the Contribution does not
127f5b1c8a1SJohn Marino  * infringe the patent or other intellectual property rights of any third
128f5b1c8a1SJohn Marino  * party or that the license provides you with all the necessary rights
129f5b1c8a1SJohn Marino  * to make use of the Contribution.
130f5b1c8a1SJohn Marino  *
131f5b1c8a1SJohn Marino  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132f5b1c8a1SJohn Marino  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133f5b1c8a1SJohn Marino  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134f5b1c8a1SJohn Marino  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135f5b1c8a1SJohn Marino  * OTHERWISE.
136f5b1c8a1SJohn Marino  */
137f5b1c8a1SJohn Marino 
13872c33676SMaxim Ag #include <limits.h>
139f5b1c8a1SJohn Marino #include <stdio.h>
140f5b1c8a1SJohn Marino 
141f5b1c8a1SJohn Marino #include <openssl/evp.h>
142f5b1c8a1SJohn Marino #include <openssl/hmac.h>
143f5b1c8a1SJohn Marino #include <openssl/md5.h>
144*de0e0e4dSAntonio Huete Jimenez #include <openssl/opensslconf.h>
145f5b1c8a1SJohn Marino 
146*de0e0e4dSAntonio Huete Jimenez #include "dtls_locl.h"
147*de0e0e4dSAntonio Huete Jimenez #include "ssl_locl.h"
14872c33676SMaxim Ag 
149f5b1c8a1SJohn Marino void
tls1_cleanup_key_block(SSL * s)150f5b1c8a1SJohn Marino tls1_cleanup_key_block(SSL *s)
151f5b1c8a1SJohn Marino {
152*de0e0e4dSAntonio Huete Jimenez 	tls12_key_block_free(s->s3->hs.tls12.key_block);
153*de0e0e4dSAntonio Huete Jimenez 	s->s3->hs.tls12.key_block = NULL;
154f5b1c8a1SJohn Marino }
155f5b1c8a1SJohn Marino 
15672c33676SMaxim Ag /*
15772c33676SMaxim Ag  * TLS P_hash() data expansion function - see RFC 5246, section 5.
15872c33676SMaxim Ag  */
159f5b1c8a1SJohn Marino static int
tls1_P_hash(const EVP_MD * md,const unsigned char * secret,size_t secret_len,const void * seed1,size_t seed1_len,const void * seed2,size_t seed2_len,const void * seed3,size_t seed3_len,const void * seed4,size_t seed4_len,const void * seed5,size_t seed5_len,unsigned char * out,size_t out_len)16072c33676SMaxim Ag tls1_P_hash(const EVP_MD *md, const unsigned char *secret, size_t secret_len,
16172c33676SMaxim Ag     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
16272c33676SMaxim Ag     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
16372c33676SMaxim Ag     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len)
164f5b1c8a1SJohn Marino {
16572c33676SMaxim Ag 	unsigned char A1[EVP_MAX_MD_SIZE], hmac[EVP_MAX_MD_SIZE];
16672c33676SMaxim Ag 	size_t A1_len, hmac_len;
167*de0e0e4dSAntonio Huete Jimenez 	EVP_MD_CTX *ctx = NULL;
168*de0e0e4dSAntonio Huete Jimenez 	EVP_PKEY *mac_key = NULL;
169f5b1c8a1SJohn Marino 	int ret = 0;
17072c33676SMaxim Ag 	int chunk;
17172c33676SMaxim Ag 	size_t i;
172f5b1c8a1SJohn Marino 
173f5b1c8a1SJohn Marino 	chunk = EVP_MD_size(md);
174f5b1c8a1SJohn Marino 	OPENSSL_assert(chunk >= 0);
175f5b1c8a1SJohn Marino 
176*de0e0e4dSAntonio Huete Jimenez 	if ((ctx = EVP_MD_CTX_new()) == NULL)
177*de0e0e4dSAntonio Huete Jimenez 		goto err;
17872c33676SMaxim Ag 
17972c33676SMaxim Ag 	mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
180*de0e0e4dSAntonio Huete Jimenez 	if (mac_key == NULL)
181f5b1c8a1SJohn Marino 		goto err;
182*de0e0e4dSAntonio Huete Jimenez 	if (!EVP_DigestSignInit(ctx, NULL, md, NULL, mac_key))
183f5b1c8a1SJohn Marino 		goto err;
184*de0e0e4dSAntonio Huete Jimenez 	if (seed1 && !EVP_DigestSignUpdate(ctx, seed1, seed1_len))
185f5b1c8a1SJohn Marino 		goto err;
186*de0e0e4dSAntonio Huete Jimenez 	if (seed2 && !EVP_DigestSignUpdate(ctx, seed2, seed2_len))
187f5b1c8a1SJohn Marino 		goto err;
188*de0e0e4dSAntonio Huete Jimenez 	if (seed3 && !EVP_DigestSignUpdate(ctx, seed3, seed3_len))
189f5b1c8a1SJohn Marino 		goto err;
190*de0e0e4dSAntonio Huete Jimenez 	if (seed4 && !EVP_DigestSignUpdate(ctx, seed4, seed4_len))
191f5b1c8a1SJohn Marino 		goto err;
192*de0e0e4dSAntonio Huete Jimenez 	if (seed5 && !EVP_DigestSignUpdate(ctx, seed5, seed5_len))
193f5b1c8a1SJohn Marino 		goto err;
194*de0e0e4dSAntonio Huete Jimenez 	if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
195f5b1c8a1SJohn Marino 		goto err;
196f5b1c8a1SJohn Marino 
197f5b1c8a1SJohn Marino 	for (;;) {
198*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignInit(ctx, NULL, md, NULL, mac_key))
199f5b1c8a1SJohn Marino 			goto err;
200*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
201f5b1c8a1SJohn Marino 			goto err;
202*de0e0e4dSAntonio Huete Jimenez 		if (seed1 && !EVP_DigestSignUpdate(ctx, seed1, seed1_len))
203f5b1c8a1SJohn Marino 			goto err;
204*de0e0e4dSAntonio Huete Jimenez 		if (seed2 && !EVP_DigestSignUpdate(ctx, seed2, seed2_len))
205f5b1c8a1SJohn Marino 			goto err;
206*de0e0e4dSAntonio Huete Jimenez 		if (seed3 && !EVP_DigestSignUpdate(ctx, seed3, seed3_len))
207f5b1c8a1SJohn Marino 			goto err;
208*de0e0e4dSAntonio Huete Jimenez 		if (seed4 && !EVP_DigestSignUpdate(ctx, seed4, seed4_len))
209f5b1c8a1SJohn Marino 			goto err;
210*de0e0e4dSAntonio Huete Jimenez 		if (seed5 && !EVP_DigestSignUpdate(ctx, seed5, seed5_len))
211f5b1c8a1SJohn Marino 			goto err;
212*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignFinal(ctx, hmac, &hmac_len))
21372c33676SMaxim Ag 			goto err;
214f5b1c8a1SJohn Marino 
21572c33676SMaxim Ag 		if (hmac_len > out_len)
21672c33676SMaxim Ag 			hmac_len = out_len;
21772c33676SMaxim Ag 
21872c33676SMaxim Ag 		for (i = 0; i < hmac_len; i++)
21972c33676SMaxim Ag 			out[i] ^= hmac[i];
22072c33676SMaxim Ag 
22172c33676SMaxim Ag 		out += hmac_len;
22272c33676SMaxim Ag 		out_len -= hmac_len;
22372c33676SMaxim Ag 
22472c33676SMaxim Ag 		if (out_len == 0)
22572c33676SMaxim Ag 			break;
22672c33676SMaxim Ag 
227*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignInit(ctx, NULL, md, NULL, mac_key))
228f5b1c8a1SJohn Marino 			goto err;
229*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
230f5b1c8a1SJohn Marino 			goto err;
231*de0e0e4dSAntonio Huete Jimenez 		if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
232f5b1c8a1SJohn Marino 			goto err;
233f5b1c8a1SJohn Marino 	}
234f5b1c8a1SJohn Marino 	ret = 1;
235f5b1c8a1SJohn Marino 
236f5b1c8a1SJohn Marino  err:
237f5b1c8a1SJohn Marino 	EVP_PKEY_free(mac_key);
238*de0e0e4dSAntonio Huete Jimenez 	EVP_MD_CTX_free(ctx);
23972c33676SMaxim Ag 
240f5b1c8a1SJohn Marino 	explicit_bzero(A1, sizeof(A1));
24172c33676SMaxim Ag 	explicit_bzero(hmac, sizeof(hmac));
24272c33676SMaxim Ag 
243f5b1c8a1SJohn Marino 	return ret;
244f5b1c8a1SJohn Marino }
245f5b1c8a1SJohn Marino 
24672c33676SMaxim Ag int
tls1_PRF(SSL * s,const unsigned char * secret,size_t secret_len,const void * seed1,size_t seed1_len,const void * seed2,size_t seed2_len,const void * seed3,size_t seed3_len,const void * seed4,size_t seed4_len,const void * seed5,size_t seed5_len,unsigned char * out,size_t out_len)24772c33676SMaxim Ag tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
24872c33676SMaxim Ag     const void *seed1, size_t seed1_len, const void *seed2, size_t seed2_len,
24972c33676SMaxim Ag     const void *seed3, size_t seed3_len, const void *seed4, size_t seed4_len,
25072c33676SMaxim Ag     const void *seed5, size_t seed5_len, unsigned char *out, size_t out_len)
251f5b1c8a1SJohn Marino {
252f5b1c8a1SJohn Marino 	const EVP_MD *md;
25372c33676SMaxim Ag 	size_t half_len;
254f5b1c8a1SJohn Marino 
25572c33676SMaxim Ag 	memset(out, 0, out_len);
256f5b1c8a1SJohn Marino 
25772c33676SMaxim Ag 	if (!ssl_get_handshake_evp_md(s, &md))
25872c33676SMaxim Ag 		return (0);
25972c33676SMaxim Ag 
260*de0e0e4dSAntonio Huete Jimenez 	if (EVP_MD_type(md) == NID_md5_sha1) {
26172c33676SMaxim Ag 		/*
26272c33676SMaxim Ag 		 * Partition secret between MD5 and SHA1, then XOR result.
26372c33676SMaxim Ag 		 * If the secret length is odd, a one byte overlap is used.
26472c33676SMaxim Ag 		 */
26572c33676SMaxim Ag 		half_len = secret_len - (secret_len / 2);
26672c33676SMaxim Ag 		if (!tls1_P_hash(EVP_md5(), secret, half_len, seed1, seed1_len,
26772c33676SMaxim Ag 		    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
26872c33676SMaxim Ag 		    seed5, seed5_len, out, out_len))
26972c33676SMaxim Ag 			return (0);
27072c33676SMaxim Ag 
27172c33676SMaxim Ag 		secret += secret_len - half_len;
27272c33676SMaxim Ag 		if (!tls1_P_hash(EVP_sha1(), secret, half_len, seed1, seed1_len,
27372c33676SMaxim Ag 		    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
27472c33676SMaxim Ag 		    seed5, seed5_len, out, out_len))
27572c33676SMaxim Ag 			return (0);
27672c33676SMaxim Ag 
27772c33676SMaxim Ag 		return (1);
27872c33676SMaxim Ag 	}
27972c33676SMaxim Ag 
28072c33676SMaxim Ag 	if (!tls1_P_hash(md, secret, secret_len, seed1, seed1_len,
28172c33676SMaxim Ag 	    seed2, seed2_len, seed3, seed3_len, seed4, seed4_len,
28272c33676SMaxim Ag 	    seed5, seed5_len, out, out_len))
28372c33676SMaxim Ag 		return (0);
28472c33676SMaxim Ag 
28572c33676SMaxim Ag 	return (1);
286f5b1c8a1SJohn Marino }
287f5b1c8a1SJohn Marino 
288*de0e0e4dSAntonio Huete Jimenez int
tls1_generate_key_block(SSL * s,uint8_t * key_block,size_t key_block_len)289*de0e0e4dSAntonio Huete Jimenez tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len)
290f5b1c8a1SJohn Marino {
29172c33676SMaxim Ag 	return tls1_PRF(s,
29272c33676SMaxim Ag 	    s->session->master_key, s->session->master_key_length,
293f5b1c8a1SJohn Marino 	    TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
294f5b1c8a1SJohn Marino 	    s->s3->server_random, SSL3_RANDOM_SIZE,
295f5b1c8a1SJohn Marino 	    s->s3->client_random, SSL3_RANDOM_SIZE,
296*de0e0e4dSAntonio Huete Jimenez 	    NULL, 0, NULL, 0, key_block, key_block_len);
297f5b1c8a1SJohn Marino }
298f5b1c8a1SJohn Marino 
299f5b1c8a1SJohn Marino static int
tls1_change_cipher_state(SSL * s,int is_write)300*de0e0e4dSAntonio Huete Jimenez tls1_change_cipher_state(SSL *s, int is_write)
301f5b1c8a1SJohn Marino {
302*de0e0e4dSAntonio Huete Jimenez 	CBS mac_key, key, iv;
303f5b1c8a1SJohn Marino 
304*de0e0e4dSAntonio Huete Jimenez 	/* Use client write keys on client write and server read. */
305*de0e0e4dSAntonio Huete Jimenez 	if ((!s->server && is_write) || (s->server && !is_write)) {
306*de0e0e4dSAntonio Huete Jimenez 		tls12_key_block_client_write(s->s3->hs.tls12.key_block,
307*de0e0e4dSAntonio Huete Jimenez 		    &mac_key, &key, &iv);
308f5b1c8a1SJohn Marino 	} else {
309*de0e0e4dSAntonio Huete Jimenez 		tls12_key_block_server_write(s->s3->hs.tls12.key_block,
310*de0e0e4dSAntonio Huete Jimenez 		    &mac_key, &key, &iv);
311f5b1c8a1SJohn Marino 	}
312f5b1c8a1SJohn Marino 
313*de0e0e4dSAntonio Huete Jimenez 	if (!is_write) {
314*de0e0e4dSAntonio Huete Jimenez 		if (!tls12_record_layer_change_read_cipher_state(s->internal->rl,
315*de0e0e4dSAntonio Huete Jimenez 		    &mac_key, &key, &iv))
316*de0e0e4dSAntonio Huete Jimenez 			goto err;
317*de0e0e4dSAntonio Huete Jimenez 		if (SSL_is_dtls(s))
318*de0e0e4dSAntonio Huete Jimenez 			dtls1_reset_read_seq_numbers(s);
319f5b1c8a1SJohn Marino 	} else {
320*de0e0e4dSAntonio Huete Jimenez 		if (!tls12_record_layer_change_write_cipher_state(s->internal->rl,
321*de0e0e4dSAntonio Huete Jimenez 		    &mac_key, &key, &iv))
3228edacedfSDaniel Fojt 			goto err;
323f5b1c8a1SJohn Marino 	}
324f5b1c8a1SJohn Marino 	return (1);
325f5b1c8a1SJohn Marino 
326f5b1c8a1SJohn Marino  err:
327f5b1c8a1SJohn Marino 	return (0);
328f5b1c8a1SJohn Marino }
329f5b1c8a1SJohn Marino 
330f5b1c8a1SJohn Marino int
tls1_change_read_cipher_state(SSL * s)331*de0e0e4dSAntonio Huete Jimenez tls1_change_read_cipher_state(SSL *s)
332f5b1c8a1SJohn Marino {
333*de0e0e4dSAntonio Huete Jimenez 	return tls1_change_cipher_state(s, 0);
334f5b1c8a1SJohn Marino }
335f5b1c8a1SJohn Marino 
336*de0e0e4dSAntonio Huete Jimenez int
tls1_change_write_cipher_state(SSL * s)337*de0e0e4dSAntonio Huete Jimenez tls1_change_write_cipher_state(SSL *s)
338*de0e0e4dSAntonio Huete Jimenez {
339*de0e0e4dSAntonio Huete Jimenez 	return tls1_change_cipher_state(s, 1);
340f5b1c8a1SJohn Marino }
341f5b1c8a1SJohn Marino 
342f5b1c8a1SJohn Marino int
tls1_setup_key_block(SSL * s)343f5b1c8a1SJohn Marino tls1_setup_key_block(SSL *s)
344f5b1c8a1SJohn Marino {
345*de0e0e4dSAntonio Huete Jimenez 	struct tls12_key_block *key_block;
346f5b1c8a1SJohn Marino 	int mac_type = NID_undef, mac_secret_size = 0;
347f5b1c8a1SJohn Marino 	const EVP_CIPHER *cipher = NULL;
348f5b1c8a1SJohn Marino 	const EVP_AEAD *aead = NULL;
349*de0e0e4dSAntonio Huete Jimenez 	const EVP_MD *handshake_hash = NULL;
350*de0e0e4dSAntonio Huete Jimenez 	const EVP_MD *mac_hash = NULL;
351f5b1c8a1SJohn Marino 	int ret = 0;
352f5b1c8a1SJohn Marino 
353*de0e0e4dSAntonio Huete Jimenez 	/*
354*de0e0e4dSAntonio Huete Jimenez 	 * XXX - callers should be changed so that they only call this
355*de0e0e4dSAntonio Huete Jimenez 	 * function once.
356*de0e0e4dSAntonio Huete Jimenez 	 */
357*de0e0e4dSAntonio Huete Jimenez 	if (s->s3->hs.tls12.key_block != NULL)
358f5b1c8a1SJohn Marino 		return (1);
359f5b1c8a1SJohn Marino 
360f5b1c8a1SJohn Marino 	if (s->session->cipher &&
36172c33676SMaxim Ag 	    (s->session->cipher->algorithm_mac & SSL_AEAD)) {
362f5b1c8a1SJohn Marino 		if (!ssl_cipher_get_evp_aead(s->session, &aead)) {
36372c33676SMaxim Ag 			SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
364f5b1c8a1SJohn Marino 			return (0);
365f5b1c8a1SJohn Marino 		}
366f5b1c8a1SJohn Marino 	} else {
367*de0e0e4dSAntonio Huete Jimenez 		/* XXX - mac_type and mac_secret_size are now unused. */
368*de0e0e4dSAntonio Huete Jimenez 		if (!ssl_cipher_get_evp(s->session, &cipher, &mac_hash,
369*de0e0e4dSAntonio Huete Jimenez 		    &mac_type, &mac_secret_size)) {
37072c33676SMaxim Ag 			SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
371f5b1c8a1SJohn Marino 			return (0);
372f5b1c8a1SJohn Marino 		}
373f5b1c8a1SJohn Marino 	}
374f5b1c8a1SJohn Marino 
375*de0e0e4dSAntonio Huete Jimenez 	if (!ssl_get_handshake_evp_md(s, &handshake_hash))
376*de0e0e4dSAntonio Huete Jimenez 		return (0);
377f5b1c8a1SJohn Marino 
378*de0e0e4dSAntonio Huete Jimenez 	tls12_record_layer_set_aead(s->internal->rl, aead);
379*de0e0e4dSAntonio Huete Jimenez 	tls12_record_layer_set_cipher_hash(s->internal->rl, cipher,
380*de0e0e4dSAntonio Huete Jimenez 	    handshake_hash, mac_hash);
381f5b1c8a1SJohn Marino 
382*de0e0e4dSAntonio Huete Jimenez 	if ((key_block = tls12_key_block_new()) == NULL)
383f5b1c8a1SJohn Marino 		goto err;
384*de0e0e4dSAntonio Huete Jimenez 	if (!tls12_key_block_generate(key_block, s, aead, cipher, mac_hash))
385f5b1c8a1SJohn Marino 		goto err;
386f5b1c8a1SJohn Marino 
387*de0e0e4dSAntonio Huete Jimenez 	s->s3->hs.tls12.key_block = key_block;
388*de0e0e4dSAntonio Huete Jimenez 	key_block = NULL;
389*de0e0e4dSAntonio Huete Jimenez 
39072c33676SMaxim Ag 	if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) &&
391*de0e0e4dSAntonio Huete Jimenez 	    s->method->version <= TLS1_VERSION) {
392f5b1c8a1SJohn Marino 		/*
393f5b1c8a1SJohn Marino 		 * Enable vulnerability countermeasure for CBC ciphers with
394f5b1c8a1SJohn Marino 		 * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
395f5b1c8a1SJohn Marino 		 */
396*de0e0e4dSAntonio Huete Jimenez 		s->s3->need_empty_fragments = 1;
397f5b1c8a1SJohn Marino 
398f5b1c8a1SJohn Marino 		if (s->session->cipher != NULL) {
399f5b1c8a1SJohn Marino 			if (s->session->cipher->algorithm_enc == SSL_eNULL)
400*de0e0e4dSAntonio Huete Jimenez 				s->s3->need_empty_fragments = 0;
401f5b1c8a1SJohn Marino 
402f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_RC4
403f5b1c8a1SJohn Marino 			if (s->session->cipher->algorithm_enc == SSL_RC4)
404*de0e0e4dSAntonio Huete Jimenez 				s->s3->need_empty_fragments = 0;
405f5b1c8a1SJohn Marino #endif
406f5b1c8a1SJohn Marino 		}
407f5b1c8a1SJohn Marino 	}
408f5b1c8a1SJohn Marino 
409f5b1c8a1SJohn Marino 	ret = 1;
410f5b1c8a1SJohn Marino 
411f5b1c8a1SJohn Marino  err:
412*de0e0e4dSAntonio Huete Jimenez 	tls12_key_block_free(key_block);
413*de0e0e4dSAntonio Huete Jimenez 
414f5b1c8a1SJohn Marino 	return (ret);
415f5b1c8a1SJohn Marino }
416f5b1c8a1SJohn Marino 
417f5b1c8a1SJohn Marino int
tls1_export_keying_material(SSL * s,unsigned char * out,size_t olen,const char * label,size_t llen,const unsigned char * context,size_t contextlen,int use_context)418f5b1c8a1SJohn Marino tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
419f5b1c8a1SJohn Marino     const char *label, size_t llen, const unsigned char *context,
420f5b1c8a1SJohn Marino     size_t contextlen, int use_context)
421f5b1c8a1SJohn Marino {
422f5b1c8a1SJohn Marino 	unsigned char *val = NULL;
423f5b1c8a1SJohn Marino 	size_t vallen, currentvalpos;
424f5b1c8a1SJohn Marino 	int rv;
425f5b1c8a1SJohn Marino 
426*de0e0e4dSAntonio Huete Jimenez 	if (!SSL_is_init_finished(s)) {
427*de0e0e4dSAntonio Huete Jimenez 		SSLerror(s, SSL_R_BAD_STATE);
428*de0e0e4dSAntonio Huete Jimenez 		return 0;
429*de0e0e4dSAntonio Huete Jimenez 	}
430*de0e0e4dSAntonio Huete Jimenez 
431f5b1c8a1SJohn Marino 	/* construct PRF arguments
432f5b1c8a1SJohn Marino 	 * we construct the PRF argument ourself rather than passing separate
433f5b1c8a1SJohn Marino 	 * values into the TLS PRF to ensure that the concatenation of values
434f5b1c8a1SJohn Marino 	 * does not create a prohibited label.
435f5b1c8a1SJohn Marino 	 */
436f5b1c8a1SJohn Marino 	vallen = llen + SSL3_RANDOM_SIZE * 2;
437f5b1c8a1SJohn Marino 	if (use_context) {
438f5b1c8a1SJohn Marino 		vallen += 2 + contextlen;
439f5b1c8a1SJohn Marino 	}
440f5b1c8a1SJohn Marino 
441f5b1c8a1SJohn Marino 	val = malloc(vallen);
442f5b1c8a1SJohn Marino 	if (val == NULL)
443f5b1c8a1SJohn Marino 		goto err2;
444f5b1c8a1SJohn Marino 	currentvalpos = 0;
445f5b1c8a1SJohn Marino 	memcpy(val + currentvalpos, (unsigned char *) label, llen);
446f5b1c8a1SJohn Marino 	currentvalpos += llen;
447f5b1c8a1SJohn Marino 	memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
448f5b1c8a1SJohn Marino 	currentvalpos += SSL3_RANDOM_SIZE;
449f5b1c8a1SJohn Marino 	memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
450f5b1c8a1SJohn Marino 	currentvalpos += SSL3_RANDOM_SIZE;
451f5b1c8a1SJohn Marino 
452f5b1c8a1SJohn Marino 	if (use_context) {
453f5b1c8a1SJohn Marino 		val[currentvalpos] = (contextlen >> 8) & 0xff;
454f5b1c8a1SJohn Marino 		currentvalpos++;
455f5b1c8a1SJohn Marino 		val[currentvalpos] = contextlen & 0xff;
456f5b1c8a1SJohn Marino 		currentvalpos++;
457f5b1c8a1SJohn Marino 		if ((contextlen > 0) || (context != NULL)) {
458f5b1c8a1SJohn Marino 			memcpy(val + currentvalpos, context, contextlen);
459f5b1c8a1SJohn Marino 		}
460f5b1c8a1SJohn Marino 	}
461f5b1c8a1SJohn Marino 
462f5b1c8a1SJohn Marino 	/* disallow prohibited labels
463f5b1c8a1SJohn Marino 	 * note that SSL3_RANDOM_SIZE > max(prohibited label len) =
464f5b1c8a1SJohn Marino 	 * 15, so size of val > max(prohibited label len) = 15 and the
465f5b1c8a1SJohn Marino 	 * comparisons won't have buffer overflow
466f5b1c8a1SJohn Marino 	 */
467f5b1c8a1SJohn Marino 	if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
468f5b1c8a1SJohn Marino 	    TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
469f5b1c8a1SJohn Marino 		goto err1;
470f5b1c8a1SJohn Marino 	if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
471f5b1c8a1SJohn Marino 	    TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
472f5b1c8a1SJohn Marino 		goto err1;
473f5b1c8a1SJohn Marino 	if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
474f5b1c8a1SJohn Marino 	    TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
475f5b1c8a1SJohn Marino 		goto err1;
476f5b1c8a1SJohn Marino 	if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
477f5b1c8a1SJohn Marino 	    TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
478f5b1c8a1SJohn Marino 		goto err1;
479f5b1c8a1SJohn Marino 
48072c33676SMaxim Ag 	rv = tls1_PRF(s, s->session->master_key, s->session->master_key_length,
48172c33676SMaxim Ag 	    val, vallen, NULL, 0, NULL, 0, NULL, 0, NULL, 0, out, olen);
482f5b1c8a1SJohn Marino 
483f5b1c8a1SJohn Marino 	goto ret;
484f5b1c8a1SJohn Marino  err1:
48572c33676SMaxim Ag 	SSLerror(s, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
486f5b1c8a1SJohn Marino 	rv = 0;
487f5b1c8a1SJohn Marino 	goto ret;
488f5b1c8a1SJohn Marino  err2:
48972c33676SMaxim Ag 	SSLerror(s, ERR_R_MALLOC_FAILURE);
490f5b1c8a1SJohn Marino 	rv = 0;
491f5b1c8a1SJohn Marino  ret:
492f5b1c8a1SJohn Marino 	free(val);
493f5b1c8a1SJohn Marino 
494f5b1c8a1SJohn Marino 	return (rv);
495f5b1c8a1SJohn Marino }
496