blob: 1efd834474921d310f5f52d76e673c05538446c1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2005 Nokia. All rights reserved.
112 *
113 * The portions of the attached software ("Contribution") is developed by
114 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
115 * license.
116 *
117 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
118 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
119 * support (see RFC 4279) to OpenSSL.
120 *
121 * No patent licenses or other rights except those expressly stated in
122 * the OpenSSL open source license shall be deemed granted or received
123 * expressly, by implication, estoppel, or otherwise.
124 *
125 * No assurances are provided by Nokia that the Contribution does not
126 * infringe the patent or other intellectual property rights of any third
127 * party or that the license provides you with all the necessary rights
128 * to make use of the Contribution.
129 *
130 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
131 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
132 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
133 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
134 * OTHERWISE. */
135
David Benjamin86e95b82017-07-18 16:34:25 -0400136#define BORINGSSL_INTERNAL_CXX_TYPES
137
David Benjamin9e4e01e2015-09-15 01:48:04 -0400138#include <openssl/ssl.h>
139
Adam Langley95c29f32014-06-20 12:00:00 -0700140#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400141#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700142
Adam Langley95c29f32014-06-20 12:00:00 -0700143#include <openssl/err.h>
144#include <openssl/evp.h>
145#include <openssl/hmac.h>
146#include <openssl/md5.h>
147#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400148#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700149#include <openssl/rand.h>
150
David Benjamin17cf2cb2016-12-13 01:07:13 -0500151#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400152#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin86e95b82017-07-18 16:34:25 -0400155namespace bssl {
156
David Benjamin1f5e1152014-12-23 09:23:32 -0500157/* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
David Benjaminb0883312015-08-06 09:54:13 -0400158 * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
David Benjamin1f5e1152014-12-23 09:23:32 -0500159 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
160 * seed parameter. It returns one on success and zero on failure. */
161static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
162 const uint8_t *secret, size_t secret_len,
163 const uint8_t *seed1, size_t seed1_len,
164 const uint8_t *seed2, size_t seed2_len,
165 const uint8_t *seed3, size_t seed3_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500166 HMAC_CTX ctx, ctx_tmp, ctx_init;
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 uint8_t A1[EVP_MAX_MD_SIZE];
David Benjamin1f5e1152014-12-23 09:23:32 -0500168 unsigned A1_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800169 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjamin871fff02015-12-06 21:04:36 -0500171 size_t chunk = EVP_MD_size(md);
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamin1f5e1152014-12-23 09:23:32 -0500173 HMAC_CTX_init(&ctx);
174 HMAC_CTX_init(&ctx_tmp);
175 HMAC_CTX_init(&ctx_init);
176 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
177 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
David Benjamin871fff02015-12-06 21:04:36 -0500178 !HMAC_Update(&ctx, seed1, seed1_len) ||
179 !HMAC_Update(&ctx, seed2, seed2_len) ||
180 !HMAC_Update(&ctx, seed3, seed3_len) ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500181 !HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 goto err;
183 }
184
185 for (;;) {
David Benjaminb0883312015-08-06 09:54:13 -0400186 unsigned len;
187 uint8_t hmac[EVP_MAX_MD_SIZE];
David Benjamin871fff02015-12-06 21:04:36 -0500188 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
189 !HMAC_Update(&ctx, A1, A1_len) ||
190 /* Save a copy of |ctx| to compute the next A1 value below. */
191 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
192 !HMAC_Update(&ctx, seed1, seed1_len) ||
193 !HMAC_Update(&ctx, seed2, seed2_len) ||
194 !HMAC_Update(&ctx, seed3, seed3_len) ||
195 !HMAC_Final(&ctx, hmac, &len)) {
David Benjaminb0883312015-08-06 09:54:13 -0400196 goto err;
197 }
198 assert(len == chunk);
199
200 /* XOR the result into |out|. */
201 if (len > out_len) {
202 len = out_len;
203 }
204 unsigned i;
205 for (i = 0; i < len; i++) {
206 out[i] ^= hmac[i];
207 }
208 out += len;
209 out_len -= len;
210
211 if (out_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 break;
213 }
David Benjaminb0883312015-08-06 09:54:13 -0400214
215 /* Calculate the next A1 value. */
216 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
217 goto err;
218 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 }
220
221 ret = 1;
222
Adam Langley95c29f32014-06-20 12:00:00 -0700223err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500224 HMAC_CTX_cleanup(&ctx);
225 HMAC_CTX_cleanup(&ctx_tmp);
226 HMAC_CTX_cleanup(&ctx_init);
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 OPENSSL_cleanse(A1, sizeof(A1));
228 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700229}
Adam Langley9447dff2014-06-24 17:29:06 -0700230
Steven Valdez908ac192017-01-12 13:17:07 -0500231int tls1_prf(const EVP_MD *digest, uint8_t *out, size_t out_len,
232 const uint8_t *secret, size_t secret_len, const char *label,
233 size_t label_len, const uint8_t *seed1, size_t seed1_len,
234 const uint8_t *seed2, size_t seed2_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500235 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500236 return 1;
237 }
238
David Benjamin17cf2cb2016-12-13 01:07:13 -0500239 OPENSSL_memset(out, 0, out_len);
David Benjaminb0883312015-08-06 09:54:13 -0400240
Steven Valdez908ac192017-01-12 13:17:07 -0500241 if (digest == EVP_md5_sha1()) {
David Benjaminb0883312015-08-06 09:54:13 -0400242 /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
243 * MD5, MD5 first. */
244 size_t secret_half = secret_len - (secret_len / 2);
245 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
246 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
247 seed2_len)) {
248 return 0;
249 }
250
251 /* Note that, if |secret_len| is odd, the two halves share a byte. */
252 secret = secret + (secret_len - secret_half);
253 secret_len = secret_half;
Steven Valdez908ac192017-01-12 13:17:07 -0500254
255 digest = EVP_sha1();
David Benjaminb0883312015-08-06 09:54:13 -0400256 }
257
Steven Valdez908ac192017-01-12 13:17:07 -0500258 if (!tls1_P_hash(out, out_len, digest, secret, secret_len,
259 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
260 seed2_len)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500261 return 0;
262 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800263
David Benjaminb0883312015-08-06 09:54:13 -0400264 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800265}
266
Steven Valdez908ac192017-01-12 13:17:07 -0500267static int ssl3_prf(uint8_t *out, size_t out_len, const uint8_t *secret,
268 size_t secret_len, const char *label, size_t label_len,
269 const uint8_t *seed1, size_t seed1_len,
270 const uint8_t *seed2, size_t seed2_len) {
271 EVP_MD_CTX md5;
272 EVP_MD_CTX sha1;
273 uint8_t buf[16], smd[SHA_DIGEST_LENGTH];
274 uint8_t c = 'A';
275 size_t i, j, k;
276
277 k = 0;
278 EVP_MD_CTX_init(&md5);
279 EVP_MD_CTX_init(&sha1);
280 for (i = 0; i < out_len; i += MD5_DIGEST_LENGTH) {
281 k++;
282 if (k > sizeof(buf)) {
283 /* bug: 'buf' is too small for this ciphersuite */
284 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
285 return 0;
286 }
287
288 for (j = 0; j < k; j++) {
289 buf[j] = c;
290 }
291 c++;
292 if (!EVP_DigestInit_ex(&sha1, EVP_sha1(), NULL)) {
293 OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
294 return 0;
295 }
296 EVP_DigestUpdate(&sha1, buf, k);
297 EVP_DigestUpdate(&sha1, secret, secret_len);
298 /* |label| is ignored for SSLv3. */
299 if (seed1_len) {
300 EVP_DigestUpdate(&sha1, seed1, seed1_len);
301 }
302 if (seed2_len) {
303 EVP_DigestUpdate(&sha1, seed2, seed2_len);
304 }
305 EVP_DigestFinal_ex(&sha1, smd, NULL);
306
307 if (!EVP_DigestInit_ex(&md5, EVP_md5(), NULL)) {
308 OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
309 return 0;
310 }
311 EVP_DigestUpdate(&md5, secret, secret_len);
312 EVP_DigestUpdate(&md5, smd, SHA_DIGEST_LENGTH);
313 if (i + MD5_DIGEST_LENGTH > out_len) {
314 EVP_DigestFinal_ex(&md5, smd, NULL);
315 OPENSSL_memcpy(out, smd, out_len - i);
316 } else {
317 EVP_DigestFinal_ex(&md5, out, NULL);
318 }
319
320 out += MD5_DIGEST_LENGTH;
321 }
322
323 OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH);
324 EVP_MD_CTX_cleanup(&md5);
325 EVP_MD_CTX_cleanup(&sha1);
326
327 return 1;
328}
329
David Benjamin67739722016-11-17 17:03:59 +0900330static int tls1_setup_key_block(SSL_HANDSHAKE *hs) {
331 SSL *const ssl = hs->ssl;
332 if (hs->key_block_len != 0) {
David Benjaminb5172a72016-11-17 17:01:01 +0900333 return 1;
334 }
335
336 SSL_SESSION *session = ssl->session;
David Benjamin45738dd2017-02-09 20:01:26 -0500337 if (hs->new_session != NULL) {
338 session = hs->new_session;
David Benjaminb5172a72016-11-17 17:01:01 +0900339 }
340
341 const EVP_AEAD *aead = NULL;
342 size_t mac_secret_len, fixed_iv_len;
343 if (session->cipher == NULL ||
344 !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
Steven Valdez2f3404b2017-05-24 16:54:35 -0400345 session->cipher, ssl3_protocol_version(ssl),
346 SSL_is_dtls(ssl))) {
David Benjaminb5172a72016-11-17 17:01:01 +0900347 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
348 return 0;
349 }
350 size_t key_len = EVP_AEAD_key_length(aead);
351 if (mac_secret_len > 0) {
352 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
353 * key length reported by |EVP_AEAD_key_length| will include the MAC key
354 * bytes and initial implicit IV. */
355 if (key_len < mac_secret_len + fixed_iv_len) {
356 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
357 return 0;
358 }
359 key_len -= mac_secret_len + fixed_iv_len;
360 }
361
362 assert(mac_secret_len < 256);
363 assert(key_len < 256);
364 assert(fixed_iv_len < 256);
365
366 ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
367 ssl->s3->tmp.new_key_len = (uint8_t)key_len;
368 ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
369
370 size_t key_block_len = SSL_get_key_block_len(ssl);
371
David Benjamin0238d8f2017-07-12 17:35:14 -0400372 uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminb5172a72016-11-17 17:01:01 +0900373 if (keyblock == NULL) {
374 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
375 return 0;
376 }
377
378 if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) {
379 OPENSSL_free(keyblock);
380 return 0;
381 }
382
383 assert(key_block_len < 256);
David Benjamin67739722016-11-17 17:03:59 +0900384 hs->key_block_len = (uint8_t)key_block_len;
385 hs->key_block = keyblock;
David Benjaminb5172a72016-11-17 17:01:01 +0900386 return 1;
387}
388
David Benjamin67739722016-11-17 17:03:59 +0900389int tls1_change_cipher_state(SSL_HANDSHAKE *hs, int which) {
390 SSL *const ssl = hs->ssl;
David Benjamin57997da2015-12-25 15:48:39 -0500391 /* Ensure the key block is set up. */
David Benjamin67739722016-11-17 17:03:59 +0900392 if (!tls1_setup_key_block(hs)) {
David Benjamin57997da2015-12-25 15:48:39 -0500393 return 0;
394 }
395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
397 * need to update the read cipherspec. Otherwise we have just written one. */
398 const char is_read = (which & SSL3_CC_READ) != 0;
399 /* use_client_keys is true if we wish to use the keys for the "client write"
400 * direction. This is the case if we're a client sending a ChangeCipherSpec,
401 * or a server reading a client's ChangeCipherSpec. */
402 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
403 which == SSL3_CHANGE_CIPHER_SERVER_READ;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700404
David Benjamin4119d422015-12-25 15:34:23 -0500405 size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len;
406 size_t key_len = ssl->s3->tmp.new_key_len;
407 size_t iv_len = ssl->s3->tmp.new_fixed_iv_len;
David Benjamin67739722016-11-17 17:03:59 +0900408 assert((mac_secret_len + key_len + iv_len) * 2 == hs->key_block_len);
Adam Langleya5fa5b72014-06-20 12:00:00 -0700409
David Benjamin67739722016-11-17 17:03:59 +0900410 const uint8_t *key_data = hs->key_block;
David Benjamin4119d422015-12-25 15:34:23 -0500411 const uint8_t *client_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500413 const uint8_t *server_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500415 const uint8_t *client_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500417 const uint8_t *server_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500419 const uint8_t *client_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 key_data += iv_len;
David Benjamin4119d422015-12-25 15:34:23 -0500421 const uint8_t *server_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700423
David Benjamin4119d422015-12-25 15:34:23 -0500424 const uint8_t *mac_secret, *key, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 if (use_client_keys) {
426 mac_secret = client_write_mac_secret;
427 key = client_write_key;
428 iv = client_write_iv;
429 } else {
430 mac_secret = server_write_mac_secret;
431 key = server_write_key;
432 iv = server_write_iv;
433 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700434
David Benjamin45738dd2017-02-09 20:01:26 -0500435 SSL_AEAD_CTX *aead_ctx = SSL_AEAD_CTX_new(
Steven Valdez2f3404b2017-05-24 16:54:35 -0400436 is_read ? evp_aead_open : evp_aead_seal, ssl3_protocol_version(ssl), SSL_is_dtls(ssl),
David Benjamin45738dd2017-02-09 20:01:26 -0500437 hs->new_cipher, key, key_len, mac_secret, mac_secret_len, iv, iv_len);
David Benjamin4119d422015-12-25 15:34:23 -0500438 if (aead_ctx == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 return 0;
440 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700441
David Benjamin31a07792015-03-03 14:20:26 -0500442 if (is_read) {
David Benjamin61672812016-07-14 23:10:43 -0400443 return ssl->method->set_read_state(ssl, aead_ctx);
David Benjamin31a07792015-03-03 14:20:26 -0500444 }
David Benjamin61672812016-07-14 23:10:43 -0400445
446 return ssl->method->set_write_state(ssl, aead_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800447}
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Steven Valdez908ac192017-01-12 13:17:07 -0500449int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin0d56f882015-12-19 17:05:56 -0500450 const uint8_t *premaster,
David Benjamin31b1d812014-12-23 10:01:09 -0500451 size_t premaster_len) {
Steven Valdez908ac192017-01-12 13:17:07 -0500452 const SSL *ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500453 if (hs->extended_master_secret) {
David Benjaminbaa12162015-12-29 19:13:58 -0500454 uint8_t digests[EVP_MAX_MD_SIZE];
Steven Valdez908ac192017-01-12 13:17:07 -0500455 size_t digests_len;
456 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, digests, &digests_len) ||
457 !tls1_prf(SSL_TRANSCRIPT_md(&hs->transcript), out,
458 SSL3_MASTER_SECRET_SIZE, premaster, premaster_len,
459 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
460 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
461 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500462 return 0;
463 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 } else {
Steven Valdez908ac192017-01-12 13:17:07 -0500465 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
466 if (!ssl3_prf(out, SSL3_MASTER_SECRET_SIZE, premaster, premaster_len,
467 TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
468 ssl->s3->client_random, SSL3_RANDOM_SIZE,
469 ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
470 return 0;
471 }
472 } else {
473 if (!tls1_prf(SSL_TRANSCRIPT_md(&hs->transcript), out,
474 SSL3_MASTER_SECRET_SIZE, premaster, premaster_len,
475 TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
476 ssl->s3->client_random, SSL3_RANDOM_SIZE,
477 ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
478 return 0;
479 }
David Benjaminaf032d62014-12-22 10:42:51 -0500480 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 return SSL3_MASTER_SECRET_SIZE;
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
David Benjamin86e95b82017-07-18 16:34:25 -0400486} // namespace bssl
487
488using namespace bssl;
489
490size_t SSL_get_key_block_len(const SSL *ssl) {
491 return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len +
492 (size_t)ssl->s3->tmp.new_key_len +
493 (size_t)ssl->s3->tmp.new_fixed_iv_len);
494}
495
496int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
497 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
498 return ssl3_prf(out, out_len, SSL_get_session(ssl)->master_key,
499 SSL_get_session(ssl)->master_key_length,
500 TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
501 ssl->s3->server_random, SSL3_RANDOM_SIZE,
502 ssl->s3->client_random, SSL3_RANDOM_SIZE);
503 }
504
505 const EVP_MD *digest = ssl_get_handshake_digest(
506 SSL_get_session(ssl)->cipher->algorithm_prf, ssl3_protocol_version(ssl));
507 if (digest == NULL) {
508 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
509 return 0;
510 }
511 return tls1_prf(digest, out, out_len, SSL_get_session(ssl)->master_key,
512 SSL_get_session(ssl)->master_key_length,
513 TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
514 ssl->s3->server_random, SSL3_RANDOM_SIZE,
515 ssl->s3->client_random, SSL3_RANDOM_SIZE);
516}
517
David Benjaminf8d80712015-12-29 18:56:28 -0500518int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
519 const char *label, size_t label_len,
520 const uint8_t *context, size_t context_len,
521 int use_context) {
David Benjamin0d56f882015-12-19 17:05:56 -0500522 if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) {
David Benjamind6e95ee2015-05-03 15:34:35 -0400523 return 0;
524 }
525
David Benjamin7bb1d292016-11-01 19:45:06 -0400526 /* Exporters may not be used in the middle of a renegotiation. */
527 if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
528 return 0;
529 }
530
Steven Valdez143e8b32016-07-11 13:19:03 -0400531 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
532 return tls13_export_keying_material(ssl, out, out_len, label, label_len,
533 context, context_len, use_context);
534 }
535
David Benjamincfd248b2015-04-03 11:02:24 -0400536 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400538 if (context_len >= 1u << 16) {
David Benjamin3570d732015-06-29 00:28:17 -0400539 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamincfd248b2015-04-03 11:02:24 -0400540 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 }
David Benjamincfd248b2015-04-03 11:02:24 -0400542 seed_len += 2 + context_len;
543 }
David Benjamin0238d8f2017-07-12 17:35:14 -0400544 uint8_t *seed = (uint8_t *)OPENSSL_malloc(seed_len);
David Benjamincfd248b2015-04-03 11:02:24 -0400545 if (seed == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400546 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfd248b2015-04-03 11:02:24 -0400547 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 }
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin17cf2cb2016-12-13 01:07:13 -0500550 OPENSSL_memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE);
551 OPENSSL_memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random,
552 SSL3_RANDOM_SIZE);
David Benjamincfd248b2015-04-03 11:02:24 -0400553 if (use_context) {
554 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
555 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500556 OPENSSL_memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Steven Valdez908ac192017-01-12 13:17:07 -0500559 const EVP_MD *digest = ssl_get_handshake_digest(
560 SSL_get_session(ssl)->cipher->algorithm_prf, ssl3_protocol_version(ssl));
561 if (digest == NULL) {
562 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
563 return 0;
564 }
565 int ret = tls1_prf(digest, out, out_len, SSL_get_session(ssl)->master_key,
566 SSL_get_session(ssl)->master_key_length, label, label_len,
567 seed, seed_len, NULL, 0);
David Benjamincfd248b2015-04-03 11:02:24 -0400568 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 return ret;
570}