blob: d4a6ee9fa99f0410858a87bef97a7e4470b0fac8 [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 Benjamin9e4e01e2015-09-15 01:48:04 -0400136#include <openssl/ssl.h>
137
Adam Langley95c29f32014-06-20 12:00:00 -0700138#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400139#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700140
David Benjamincfc11c22017-07-18 22:45:18 -0400141#include <utility>
142
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 Benjamin1386aad2017-07-19 23:57:40 -0400166 ScopedHMAC_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 Benjamin1386aad2017-07-19 23:57:40 -0400173 if (!HMAC_Init_ex(ctx_init.get(), secret, secret_len, md, NULL) ||
174 !HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) ||
175 !HMAC_Update(ctx.get(), seed1, seed1_len) ||
176 !HMAC_Update(ctx.get(), seed2, seed2_len) ||
177 !HMAC_Update(ctx.get(), seed3, seed3_len) ||
178 !HMAC_Final(ctx.get(), A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 goto err;
180 }
181
182 for (;;) {
David Benjaminb0883312015-08-06 09:54:13 -0400183 unsigned len;
184 uint8_t hmac[EVP_MAX_MD_SIZE];
David Benjamin1386aad2017-07-19 23:57:40 -0400185 if (!HMAC_CTX_copy_ex(ctx.get(), ctx_init.get()) ||
186 !HMAC_Update(ctx.get(), A1, A1_len) ||
David Benjamin871fff02015-12-06 21:04:36 -0500187 /* Save a copy of |ctx| to compute the next A1 value below. */
David Benjamin1386aad2017-07-19 23:57:40 -0400188 (out_len > chunk && !HMAC_CTX_copy_ex(ctx_tmp.get(), ctx.get())) ||
189 !HMAC_Update(ctx.get(), seed1, seed1_len) ||
190 !HMAC_Update(ctx.get(), seed2, seed2_len) ||
191 !HMAC_Update(ctx.get(), seed3, seed3_len) ||
192 !HMAC_Final(ctx.get(), hmac, &len)) {
David Benjaminb0883312015-08-06 09:54:13 -0400193 goto err;
194 }
195 assert(len == chunk);
196
197 /* XOR the result into |out|. */
198 if (len > out_len) {
199 len = out_len;
200 }
201 unsigned i;
202 for (i = 0; i < len; i++) {
203 out[i] ^= hmac[i];
204 }
205 out += len;
206 out_len -= len;
207
208 if (out_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 break;
210 }
David Benjaminb0883312015-08-06 09:54:13 -0400211
212 /* Calculate the next A1 value. */
David Benjamin1386aad2017-07-19 23:57:40 -0400213 if (!HMAC_Final(ctx_tmp.get(), A1, &A1_len)) {
David Benjaminb0883312015-08-06 09:54:13 -0400214 goto err;
215 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 }
217
218 ret = 1;
219
Adam Langley95c29f32014-06-20 12:00:00 -0700220err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 OPENSSL_cleanse(A1, sizeof(A1));
222 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700223}
Adam Langley9447dff2014-06-24 17:29:06 -0700224
Steven Valdez908ac192017-01-12 13:17:07 -0500225int tls1_prf(const EVP_MD *digest, uint8_t *out, size_t out_len,
226 const uint8_t *secret, size_t secret_len, const char *label,
227 size_t label_len, const uint8_t *seed1, size_t seed1_len,
228 const uint8_t *seed2, size_t seed2_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500229 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500230 return 1;
231 }
232
David Benjamin17cf2cb2016-12-13 01:07:13 -0500233 OPENSSL_memset(out, 0, out_len);
David Benjaminb0883312015-08-06 09:54:13 -0400234
Steven Valdez908ac192017-01-12 13:17:07 -0500235 if (digest == EVP_md5_sha1()) {
David Benjaminb0883312015-08-06 09:54:13 -0400236 /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
237 * MD5, MD5 first. */
238 size_t secret_half = secret_len - (secret_len / 2);
239 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
240 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
241 seed2_len)) {
242 return 0;
243 }
244
245 /* Note that, if |secret_len| is odd, the two halves share a byte. */
246 secret = secret + (secret_len - secret_half);
247 secret_len = secret_half;
Steven Valdez908ac192017-01-12 13:17:07 -0500248
249 digest = EVP_sha1();
David Benjaminb0883312015-08-06 09:54:13 -0400250 }
251
Steven Valdez908ac192017-01-12 13:17:07 -0500252 if (!tls1_P_hash(out, out_len, digest, secret, secret_len,
253 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
254 seed2_len)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500255 return 0;
256 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800257
David Benjaminb0883312015-08-06 09:54:13 -0400258 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259}
260
Steven Valdez908ac192017-01-12 13:17:07 -0500261static int ssl3_prf(uint8_t *out, size_t out_len, const uint8_t *secret,
262 size_t secret_len, const char *label, size_t label_len,
263 const uint8_t *seed1, size_t seed1_len,
264 const uint8_t *seed2, size_t seed2_len) {
David Benjamin1386aad2017-07-19 23:57:40 -0400265 ScopedEVP_MD_CTX md5;
266 ScopedEVP_MD_CTX sha1;
Steven Valdez908ac192017-01-12 13:17:07 -0500267 uint8_t buf[16], smd[SHA_DIGEST_LENGTH];
268 uint8_t c = 'A';
269 size_t i, j, k;
270
271 k = 0;
Steven Valdez908ac192017-01-12 13:17:07 -0500272 for (i = 0; i < out_len; i += MD5_DIGEST_LENGTH) {
273 k++;
274 if (k > sizeof(buf)) {
275 /* bug: 'buf' is too small for this ciphersuite */
276 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
277 return 0;
278 }
279
280 for (j = 0; j < k; j++) {
281 buf[j] = c;
282 }
283 c++;
David Benjamin1386aad2017-07-19 23:57:40 -0400284 if (!EVP_DigestInit_ex(sha1.get(), EVP_sha1(), NULL)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500285 OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
286 return 0;
287 }
David Benjamin1386aad2017-07-19 23:57:40 -0400288 EVP_DigestUpdate(sha1.get(), buf, k);
289 EVP_DigestUpdate(sha1.get(), secret, secret_len);
Steven Valdez908ac192017-01-12 13:17:07 -0500290 /* |label| is ignored for SSLv3. */
291 if (seed1_len) {
David Benjamin1386aad2017-07-19 23:57:40 -0400292 EVP_DigestUpdate(sha1.get(), seed1, seed1_len);
Steven Valdez908ac192017-01-12 13:17:07 -0500293 }
294 if (seed2_len) {
David Benjamin1386aad2017-07-19 23:57:40 -0400295 EVP_DigestUpdate(sha1.get(), seed2, seed2_len);
Steven Valdez908ac192017-01-12 13:17:07 -0500296 }
David Benjamin1386aad2017-07-19 23:57:40 -0400297 EVP_DigestFinal_ex(sha1.get(), smd, NULL);
Steven Valdez908ac192017-01-12 13:17:07 -0500298
David Benjamin1386aad2017-07-19 23:57:40 -0400299 if (!EVP_DigestInit_ex(md5.get(), EVP_md5(), NULL)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500300 OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP);
301 return 0;
302 }
David Benjamin1386aad2017-07-19 23:57:40 -0400303 EVP_DigestUpdate(md5.get(), secret, secret_len);
304 EVP_DigestUpdate(md5.get(), smd, SHA_DIGEST_LENGTH);
Steven Valdez908ac192017-01-12 13:17:07 -0500305 if (i + MD5_DIGEST_LENGTH > out_len) {
David Benjamin1386aad2017-07-19 23:57:40 -0400306 EVP_DigestFinal_ex(md5.get(), smd, NULL);
Steven Valdez908ac192017-01-12 13:17:07 -0500307 OPENSSL_memcpy(out, smd, out_len - i);
308 } else {
David Benjamin1386aad2017-07-19 23:57:40 -0400309 EVP_DigestFinal_ex(md5.get(), out, NULL);
Steven Valdez908ac192017-01-12 13:17:07 -0500310 }
311
312 out += MD5_DIGEST_LENGTH;
313 }
314
315 OPENSSL_cleanse(smd, SHA_DIGEST_LENGTH);
Steven Valdez908ac192017-01-12 13:17:07 -0500316 return 1;
317}
318
David Benjamin67739722016-11-17 17:03:59 +0900319static int tls1_setup_key_block(SSL_HANDSHAKE *hs) {
320 SSL *const ssl = hs->ssl;
321 if (hs->key_block_len != 0) {
David Benjaminb5172a72016-11-17 17:01:01 +0900322 return 1;
323 }
324
325 SSL_SESSION *session = ssl->session;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400326 if (hs->new_session) {
327 session = hs->new_session.get();
David Benjaminb5172a72016-11-17 17:01:01 +0900328 }
329
330 const EVP_AEAD *aead = NULL;
331 size_t mac_secret_len, fixed_iv_len;
332 if (session->cipher == NULL ||
333 !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
Steven Valdez2f3404b2017-05-24 16:54:35 -0400334 session->cipher, ssl3_protocol_version(ssl),
335 SSL_is_dtls(ssl))) {
David Benjaminb5172a72016-11-17 17:01:01 +0900336 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
337 return 0;
338 }
339 size_t key_len = EVP_AEAD_key_length(aead);
340 if (mac_secret_len > 0) {
341 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
342 * key length reported by |EVP_AEAD_key_length| will include the MAC key
343 * bytes and initial implicit IV. */
344 if (key_len < mac_secret_len + fixed_iv_len) {
345 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
346 return 0;
347 }
348 key_len -= mac_secret_len + fixed_iv_len;
349 }
350
351 assert(mac_secret_len < 256);
352 assert(key_len < 256);
353 assert(fixed_iv_len < 256);
354
355 ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
356 ssl->s3->tmp.new_key_len = (uint8_t)key_len;
357 ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
358
359 size_t key_block_len = SSL_get_key_block_len(ssl);
360
David Benjamin0238d8f2017-07-12 17:35:14 -0400361 uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminb5172a72016-11-17 17:01:01 +0900362 if (keyblock == NULL) {
363 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
364 return 0;
365 }
366
367 if (!SSL_generate_key_block(ssl, keyblock, key_block_len)) {
368 OPENSSL_free(keyblock);
369 return 0;
370 }
371
372 assert(key_block_len < 256);
David Benjamin67739722016-11-17 17:03:59 +0900373 hs->key_block_len = (uint8_t)key_block_len;
374 hs->key_block = keyblock;
David Benjaminb5172a72016-11-17 17:01:01 +0900375 return 1;
376}
377
David Benjamin67739722016-11-17 17:03:59 +0900378int tls1_change_cipher_state(SSL_HANDSHAKE *hs, int which) {
379 SSL *const ssl = hs->ssl;
David Benjamin57997da2015-12-25 15:48:39 -0500380 /* Ensure the key block is set up. */
David Benjamin67739722016-11-17 17:03:59 +0900381 if (!tls1_setup_key_block(hs)) {
David Benjamin57997da2015-12-25 15:48:39 -0500382 return 0;
383 }
384
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
386 * need to update the read cipherspec. Otherwise we have just written one. */
387 const char is_read = (which & SSL3_CC_READ) != 0;
388 /* use_client_keys is true if we wish to use the keys for the "client write"
389 * direction. This is the case if we're a client sending a ChangeCipherSpec,
390 * or a server reading a client's ChangeCipherSpec. */
391 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
392 which == SSL3_CHANGE_CIPHER_SERVER_READ;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700393
David Benjamin4119d422015-12-25 15:34:23 -0500394 size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len;
395 size_t key_len = ssl->s3->tmp.new_key_len;
396 size_t iv_len = ssl->s3->tmp.new_fixed_iv_len;
David Benjamin67739722016-11-17 17:03:59 +0900397 assert((mac_secret_len + key_len + iv_len) * 2 == hs->key_block_len);
Adam Langleya5fa5b72014-06-20 12:00:00 -0700398
David Benjamin67739722016-11-17 17:03:59 +0900399 const uint8_t *key_data = hs->key_block;
David Benjamin4119d422015-12-25 15:34:23 -0500400 const uint8_t *client_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500402 const uint8_t *server_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500404 const uint8_t *client_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500406 const uint8_t *server_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500408 const uint8_t *client_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 key_data += iv_len;
David Benjamin4119d422015-12-25 15:34:23 -0500410 const uint8_t *server_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700412
David Benjamin4119d422015-12-25 15:34:23 -0500413 const uint8_t *mac_secret, *key, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 if (use_client_keys) {
415 mac_secret = client_write_mac_secret;
416 key = client_write_key;
417 iv = client_write_iv;
418 } else {
419 mac_secret = server_write_mac_secret;
420 key = server_write_key;
421 iv = server_write_iv;
422 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700423
David Benjamincfc11c22017-07-18 22:45:18 -0400424 UniquePtr<SSLAEADContext> aead_ctx = SSLAEADContext::Create(
425 is_read ? evp_aead_open : evp_aead_seal, ssl3_protocol_version(ssl),
426 SSL_is_dtls(ssl), hs->new_cipher, key, key_len, mac_secret,
427 mac_secret_len, iv, iv_len);
428 if (!aead_ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 return 0;
430 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700431
David Benjamin31a07792015-03-03 14:20:26 -0500432 if (is_read) {
David Benjamincfc11c22017-07-18 22:45:18 -0400433 return ssl->method->set_read_state(ssl, std::move(aead_ctx));
David Benjamin31a07792015-03-03 14:20:26 -0500434 }
David Benjamin61672812016-07-14 23:10:43 -0400435
David Benjamincfc11c22017-07-18 22:45:18 -0400436 return ssl->method->set_write_state(ssl, std::move(aead_ctx));
Adam Langleyfcf25832014-12-18 17:42:32 -0800437}
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Steven Valdez908ac192017-01-12 13:17:07 -0500439int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
David Benjamin0d56f882015-12-19 17:05:56 -0500440 const uint8_t *premaster,
David Benjamin31b1d812014-12-23 10:01:09 -0500441 size_t premaster_len) {
Steven Valdez908ac192017-01-12 13:17:07 -0500442 const SSL *ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500443 if (hs->extended_master_secret) {
David Benjaminbaa12162015-12-29 19:13:58 -0500444 uint8_t digests[EVP_MAX_MD_SIZE];
Steven Valdez908ac192017-01-12 13:17:07 -0500445 size_t digests_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -0400446 if (!hs->transcript.GetHash(digests, &digests_len) ||
447 !tls1_prf(hs->transcript.Digest(), out, SSL3_MASTER_SECRET_SIZE,
448 premaster, premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
Steven Valdez908ac192017-01-12 13:17:07 -0500449 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
450 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500451 return 0;
452 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 } else {
Steven Valdez908ac192017-01-12 13:17:07 -0500454 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
455 if (!ssl3_prf(out, SSL3_MASTER_SECRET_SIZE, premaster, premaster_len,
456 TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE,
457 ssl->s3->client_random, SSL3_RANDOM_SIZE,
458 ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
459 return 0;
460 }
461 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400462 if (!tls1_prf(hs->transcript.Digest(), out, SSL3_MASTER_SECRET_SIZE,
463 premaster, premaster_len, TLS_MD_MASTER_SECRET_CONST,
464 TLS_MD_MASTER_SECRET_CONST_SIZE, ssl->s3->client_random,
465 SSL3_RANDOM_SIZE, ssl->s3->server_random,
466 SSL3_RANDOM_SIZE)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500467 return 0;
468 }
David Benjaminaf032d62014-12-22 10:42:51 -0500469 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 }
Adam Langley95c29f32014-06-20 12:00:00 -0700471
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 return SSL3_MASTER_SECRET_SIZE;
473}
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin86e95b82017-07-18 16:34:25 -0400475} // namespace bssl
476
477using namespace bssl;
478
479size_t SSL_get_key_block_len(const SSL *ssl) {
480 return 2 * ((size_t)ssl->s3->tmp.new_mac_secret_len +
481 (size_t)ssl->s3->tmp.new_key_len +
482 (size_t)ssl->s3->tmp.new_fixed_iv_len);
483}
484
485int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
David Benjaminca9e8f52017-08-09 15:02:34 -0400486 const SSL_SESSION *session = SSL_get_session(ssl);
David Benjamin86e95b82017-07-18 16:34:25 -0400487 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjaminca9e8f52017-08-09 15:02:34 -0400488 return ssl3_prf(out, out_len, session->master_key,
489 session->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
490 TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random,
491 SSL3_RANDOM_SIZE, ssl->s3->client_random, SSL3_RANDOM_SIZE);
David Benjamin86e95b82017-07-18 16:34:25 -0400492 }
493
David Benjaminca9e8f52017-08-09 15:02:34 -0400494 const EVP_MD *digest = SSL_SESSION_get_digest(session);
495 return tls1_prf(digest, out, out_len, session->master_key,
496 session->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
497 TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random,
498 SSL3_RANDOM_SIZE, ssl->s3->client_random, SSL3_RANDOM_SIZE);
David Benjamin86e95b82017-07-18 16:34:25 -0400499}
500
David Benjaminf8d80712015-12-29 18:56:28 -0500501int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
502 const char *label, size_t label_len,
503 const uint8_t *context, size_t context_len,
504 int use_context) {
David Benjamin0d56f882015-12-19 17:05:56 -0500505 if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) {
David Benjamind6e95ee2015-05-03 15:34:35 -0400506 return 0;
507 }
508
David Benjamin7bb1d292016-11-01 19:45:06 -0400509 /* Exporters may not be used in the middle of a renegotiation. */
510 if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
511 return 0;
512 }
513
Steven Valdez143e8b32016-07-11 13:19:03 -0400514 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
515 return tls13_export_keying_material(ssl, out, out_len, label, label_len,
516 context, context_len, use_context);
517 }
518
David Benjamincfd248b2015-04-03 11:02:24 -0400519 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400521 if (context_len >= 1u << 16) {
David Benjamin3570d732015-06-29 00:28:17 -0400522 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamincfd248b2015-04-03 11:02:24 -0400523 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 }
David Benjamincfd248b2015-04-03 11:02:24 -0400525 seed_len += 2 + context_len;
526 }
David Benjamin0238d8f2017-07-12 17:35:14 -0400527 uint8_t *seed = (uint8_t *)OPENSSL_malloc(seed_len);
David Benjamincfd248b2015-04-03 11:02:24 -0400528 if (seed == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400529 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfd248b2015-04-03 11:02:24 -0400530 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin17cf2cb2016-12-13 01:07:13 -0500533 OPENSSL_memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE);
534 OPENSSL_memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random,
535 SSL3_RANDOM_SIZE);
David Benjamincfd248b2015-04-03 11:02:24 -0400536 if (use_context) {
537 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
538 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500539 OPENSSL_memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjaminca9e8f52017-08-09 15:02:34 -0400542 const SSL_SESSION *session = SSL_get_session(ssl);
543 const EVP_MD *digest = SSL_SESSION_get_digest(session);
544 int ret = tls1_prf(digest, out, out_len, session->master_key,
545 session->master_key_length, label, label_len, seed,
546 seed_len, NULL, 0);
David Benjamincfd248b2015-04-03 11:02:24 -0400547 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 return ret;
549}