blob: 92a848996600108b82e22d568c0e09b6ee9d8c2e [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 <stdio.h>
140#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700141
Adam Langley95c29f32014-06-20 12:00:00 -0700142#include <openssl/err.h>
143#include <openssl/evp.h>
144#include <openssl/hmac.h>
145#include <openssl/md5.h>
146#include <openssl/mem.h>
147#include <openssl/obj.h>
148#include <openssl/rand.h>
149
David Benjamin2ee94aa2015-04-07 22:38:30 -0400150#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjamin1f5e1152014-12-23 09:23:32 -0500153/* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
David Benjaminb0883312015-08-06 09:54:13 -0400154 * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
David Benjamin1f5e1152014-12-23 09:23:32 -0500155 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
156 * seed parameter. It returns one on success and zero on failure. */
157static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
158 const uint8_t *secret, size_t secret_len,
159 const uint8_t *seed1, size_t seed1_len,
160 const uint8_t *seed2, size_t seed2_len,
161 const uint8_t *seed3, size_t seed3_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500162 HMAC_CTX ctx, ctx_tmp, ctx_init;
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 uint8_t A1[EVP_MAX_MD_SIZE];
David Benjamin1f5e1152014-12-23 09:23:32 -0500164 unsigned A1_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin871fff02015-12-06 21:04:36 -0500167 size_t chunk = EVP_MD_size(md);
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin1f5e1152014-12-23 09:23:32 -0500169 HMAC_CTX_init(&ctx);
170 HMAC_CTX_init(&ctx_tmp);
171 HMAC_CTX_init(&ctx_init);
172 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
173 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
David Benjamin871fff02015-12-06 21:04:36 -0500174 !HMAC_Update(&ctx, seed1, seed1_len) ||
175 !HMAC_Update(&ctx, seed2, seed2_len) ||
176 !HMAC_Update(&ctx, seed3, seed3_len) ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500177 !HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 goto err;
179 }
180
181 for (;;) {
David Benjaminb0883312015-08-06 09:54:13 -0400182 unsigned len;
183 uint8_t hmac[EVP_MAX_MD_SIZE];
David Benjamin871fff02015-12-06 21:04:36 -0500184 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
185 !HMAC_Update(&ctx, A1, A1_len) ||
186 /* Save a copy of |ctx| to compute the next A1 value below. */
187 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
188 !HMAC_Update(&ctx, seed1, seed1_len) ||
189 !HMAC_Update(&ctx, seed2, seed2_len) ||
190 !HMAC_Update(&ctx, seed3, seed3_len) ||
191 !HMAC_Final(&ctx, hmac, &len)) {
David Benjaminb0883312015-08-06 09:54:13 -0400192 goto err;
193 }
194 assert(len == chunk);
195
196 /* XOR the result into |out|. */
197 if (len > out_len) {
198 len = out_len;
199 }
200 unsigned i;
201 for (i = 0; i < len; i++) {
202 out[i] ^= hmac[i];
203 }
204 out += len;
205 out_len -= len;
206
207 if (out_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 break;
209 }
David Benjaminb0883312015-08-06 09:54:13 -0400210
211 /* Calculate the next A1 value. */
212 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
213 goto err;
214 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 }
216
217 ret = 1;
218
Adam Langley95c29f32014-06-20 12:00:00 -0700219err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500220 HMAC_CTX_cleanup(&ctx);
221 HMAC_CTX_cleanup(&ctx_tmp);
222 HMAC_CTX_cleanup(&ctx_init);
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 OPENSSL_cleanse(A1, sizeof(A1));
224 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700225}
Adam Langley9447dff2014-06-24 17:29:06 -0700226
David Benjamin0d56f882015-12-19 17:05:56 -0500227int tls1_prf(SSL *ssl, uint8_t *out, size_t out_len, const uint8_t *secret,
David Benjamin41ac9792014-12-23 10:41:06 -0500228 size_t secret_len, const char *label, size_t label_len,
229 const uint8_t *seed1, size_t seed1_len,
230 const uint8_t *seed2, size_t seed2_len) {
David Benjaminaf032d62014-12-22 10:42:51 -0500231
David Benjamin1f5e1152014-12-23 09:23:32 -0500232 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500233 return 1;
234 }
235
David Benjaminb0883312015-08-06 09:54:13 -0400236 memset(out, 0, out_len);
237
David Benjamin0d56f882015-12-19 17:05:56 -0500238 uint32_t algorithm_prf = ssl_get_algorithm_prf(ssl);
David Benjaminb0883312015-08-06 09:54:13 -0400239 if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
240 /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
241 * MD5, MD5 first. */
242 size_t secret_half = secret_len - (secret_len / 2);
243 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
244 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
245 seed2_len)) {
246 return 0;
247 }
248
249 /* Note that, if |secret_len| is odd, the two halves share a byte. */
250 secret = secret + (secret_len - secret_half);
251 secret_len = secret_half;
252 }
253
254 if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf),
255 secret, secret_len, (const uint8_t *)label, label_len,
256 seed1, seed1_len, seed2, seed2_len)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500257 return 0;
258 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800259
David Benjaminb0883312015-08-06 09:54:13 -0400260 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800261}
262
David Benjamin0d56f882015-12-19 17:05:56 -0500263static int tls1_generate_key_block(SSL *ssl, uint8_t *out, size_t out_len) {
264 return ssl->enc_method->prf(
265 ssl, out, out_len, ssl->session->master_key,
266 ssl->session->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
267 TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE,
268 ssl->s3->client_random, SSL3_RANDOM_SIZE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800269}
Adam Langley95c29f32014-06-20 12:00:00 -0700270
David Benjamin0d56f882015-12-19 17:05:56 -0500271int tls1_change_cipher_state(SSL *ssl, int which) {
David Benjamin57997da2015-12-25 15:48:39 -0500272 /* Ensure the key block is set up. */
273 if (!tls1_setup_key_block(ssl)) {
274 return 0;
275 }
276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
278 * need to update the read cipherspec. Otherwise we have just written one. */
279 const char is_read = (which & SSL3_CC_READ) != 0;
280 /* use_client_keys is true if we wish to use the keys for the "client write"
281 * direction. This is the case if we're a client sending a ChangeCipherSpec,
282 * or a server reading a client's ChangeCipherSpec. */
283 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
284 which == SSL3_CHANGE_CIPHER_SERVER_READ;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700285
David Benjamin4119d422015-12-25 15:34:23 -0500286 size_t mac_secret_len = ssl->s3->tmp.new_mac_secret_len;
287 size_t key_len = ssl->s3->tmp.new_key_len;
288 size_t iv_len = ssl->s3->tmp.new_fixed_iv_len;
289 assert((mac_secret_len + key_len + iv_len) * 2 ==
290 ssl->s3->tmp.key_block_length);
Adam Langleya5fa5b72014-06-20 12:00:00 -0700291
David Benjamin4119d422015-12-25 15:34:23 -0500292 const uint8_t *key_data = ssl->s3->tmp.key_block;
293 const uint8_t *client_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500295 const uint8_t *server_write_mac_secret = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 key_data += mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500297 const uint8_t *client_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500299 const uint8_t *server_write_key = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 key_data += key_len;
David Benjamin4119d422015-12-25 15:34:23 -0500301 const uint8_t *client_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 key_data += iv_len;
David Benjamin4119d422015-12-25 15:34:23 -0500303 const uint8_t *server_write_iv = key_data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700305
David Benjamin4119d422015-12-25 15:34:23 -0500306 const uint8_t *mac_secret, *key, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 if (use_client_keys) {
308 mac_secret = client_write_mac_secret;
309 key = client_write_key;
310 iv = client_write_iv;
311 } else {
312 mac_secret = server_write_mac_secret;
313 key = server_write_key;
314 iv = server_write_iv;
315 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700316
David Benjamin4119d422015-12-25 15:34:23 -0500317 SSL_AEAD_CTX *aead_ctx = SSL_AEAD_CTX_new(
318 is_read ? evp_aead_open : evp_aead_seal,
319 ssl3_version_from_wire(ssl, ssl->version), ssl->s3->tmp.new_cipher, key,
320 key_len, mac_secret, mac_secret_len, iv, iv_len);
321 if (aead_ctx == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 return 0;
323 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700324
David Benjamin31a07792015-03-03 14:20:26 -0500325 if (is_read) {
David Benjamin4119d422015-12-25 15:34:23 -0500326 ssl_set_read_state(ssl, aead_ctx);
327 } else {
328 ssl_set_write_state(ssl, aead_ctx);
David Benjamin31a07792015-03-03 14:20:26 -0500329 }
David Benjamin4119d422015-12-25 15:34:23 -0500330 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800331}
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamin0d56f882015-12-19 17:05:56 -0500333int tls1_setup_key_block(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500334 if (ssl->s3->tmp.key_block_length != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 return 1;
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin4119d422015-12-25 15:34:23 -0500338 const EVP_AEAD *aead = NULL;
339 size_t mac_secret_len, fixed_iv_len;
340 if (ssl->session->cipher == NULL ||
341 !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ssl->session->cipher,
343 ssl3_version_from_wire(ssl, ssl->version))) {
David Benjamin4119d422015-12-25 15:34:23 -0500344 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
345 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500346 }
David Benjamin4119d422015-12-25 15:34:23 -0500347 size_t key_len = EVP_AEAD_key_length(aead);
David Benjaminb8a56f12014-12-23 11:41:02 -0500348 if (mac_secret_len > 0) {
349 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
350 * key length reported by |EVP_AEAD_key_length| will include the MAC key
351 * bytes and initial implicit IV. */
352 if (key_len < mac_secret_len + fixed_iv_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400353 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb8a56f12014-12-23 11:41:02 -0500354 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500356 key_len -= mac_secret_len + fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 }
Adam Langley95c29f32014-06-20 12:00:00 -0700358
David Benjaminea72bd02014-12-21 21:27:41 -0500359 assert(mac_secret_len < 256);
David Benjamin4119d422015-12-25 15:34:23 -0500360 assert(key_len < 256);
David Benjaminea72bd02014-12-21 21:27:41 -0500361 assert(fixed_iv_len < 256);
David Benjaminea72bd02014-12-21 21:27:41 -0500362
David Benjamin0d56f882015-12-19 17:05:56 -0500363 ssl->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
David Benjamin4119d422015-12-25 15:34:23 -0500364 ssl->s3->tmp.new_key_len = (uint8_t)key_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500365 ssl->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700366
David Benjamin4119d422015-12-25 15:34:23 -0500367 size_t key_block_len = mac_secret_len + key_len + fixed_iv_len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500368 key_block_len *= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700369
David Benjamin0d56f882015-12-19 17:05:56 -0500370 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700371
David Benjamin4119d422015-12-25 15:34:23 -0500372 uint8_t *keyblock = (uint8_t *)OPENSSL_malloc(key_block_len);
373 if (keyblock == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400374 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin4119d422015-12-25 15:34:23 -0500375 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
David Benjamin4119d422015-12-25 15:34:23 -0500378 if (!tls1_generate_key_block(ssl, keyblock, key_block_len)) {
379 OPENSSL_free(keyblock);
380 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
David Benjamin4119d422015-12-25 15:34:23 -0500383 assert(key_block_len < 256);
384 ssl->s3->tmp.key_block_length = (uint8_t)key_block_len;
385 ssl->s3->tmp.key_block = keyblock;
386 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
David Benjamin0d56f882015-12-19 17:05:56 -0500389int tls1_cert_verify_mac(SSL *ssl, int md_nid, uint8_t *out) {
David Benjaminb0883312015-08-06 09:54:13 -0400390 const EVP_MD_CTX *ctx_template;
391 if (md_nid == NID_md5) {
David Benjamin0d56f882015-12-19 17:05:56 -0500392 ctx_template = &ssl->s3->handshake_md5;
393 } else if (md_nid == EVP_MD_CTX_type(&ssl->s3->handshake_hash)) {
394 ctx_template = &ssl->s3->handshake_hash;
David Benjaminb0883312015-08-06 09:54:13 -0400395 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400396 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 return 0;
398 }
399
David Benjaminb0883312015-08-06 09:54:13 -0400400 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 EVP_MD_CTX_init(&ctx);
David Benjaminb0883312015-08-06 09:54:13 -0400402 if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
David Benjamin9d0847a2015-02-16 03:57:55 -0500403 EVP_MD_CTX_cleanup(&ctx);
404 return 0;
405 }
David Benjaminb0883312015-08-06 09:54:13 -0400406 unsigned ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 EVP_DigestFinal_ex(&ctx, out, &ret);
408 EVP_MD_CTX_cleanup(&ctx);
David Benjaminb0883312015-08-06 09:54:13 -0400409 return ret;
410}
Adam Langleyfcf25832014-12-18 17:42:32 -0800411
David Benjaminb0883312015-08-06 09:54:13 -0400412static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
413 size_t max_out) {
414 int ret = 0;
415 EVP_MD_CTX ctx_copy;
416 EVP_MD_CTX_init(&ctx_copy);
417
418 if (EVP_MD_CTX_size(ctx) > max_out) {
419 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
420 goto err;
421 }
422 unsigned len;
423 if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
424 !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
425 goto err;
426 }
427 assert(len == EVP_MD_CTX_size(ctx));
428
429 *out_len = len;
430 ret = 1;
431
432err:
433 EVP_MD_CTX_cleanup(&ctx_copy);
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 return ret;
435}
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langley1258b6a2014-06-20 12:00:00 -0700437/* tls1_handshake_digest calculates the current handshake hash and writes it to
438 * |out|, which has space for |out_len| bytes. It returns the number of bytes
439 * written or -1 in the event of an error. This function works on a copy of the
440 * underlying digests so can be called multiple times and prior to the final
441 * update etc. */
David Benjamin0d56f882015-12-19 17:05:56 -0500442int tls1_handshake_digest(SSL *ssl, uint8_t *out, size_t out_len) {
David Benjaminb0883312015-08-06 09:54:13 -0400443 size_t md5_len = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500444 if (EVP_MD_CTX_md(&ssl->s3->handshake_md5) != NULL &&
445 !append_digest(&ssl->s3->handshake_md5, out, &md5_len, out_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 return -1;
447 }
David Benjaminb0883312015-08-06 09:54:13 -0400448
449 size_t len;
David Benjamin0d56f882015-12-19 17:05:56 -0500450 if (!append_digest(&ssl->s3->handshake_hash, out + md5_len, &len,
David Benjaminb0883312015-08-06 09:54:13 -0400451 out_len - md5_len)) {
452 return -1;
453 }
454
455 return (int)(md5_len + len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800456}
Adam Langley1258b6a2014-06-20 12:00:00 -0700457
David Benjamin0d56f882015-12-19 17:05:56 -0500458int tls1_final_finish_mac(SSL *ssl, const char *str, int slen, uint8_t *out) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 uint8_t buf[2 * EVP_MAX_MD_SIZE];
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 int err = 0;
461 int digests_len;
Adam Langley1258b6a2014-06-20 12:00:00 -0700462
David Benjamine76ccae2015-05-26 17:41:50 -0400463 /* At this point, the handshake should have released the handshake buffer on
David Benjamin9550c3a2015-08-05 08:50:34 -0400464 * its own. */
David Benjamin0d56f882015-12-19 17:05:56 -0500465 assert(ssl->s3->handshake_buffer == NULL);
Adam Langley1258b6a2014-06-20 12:00:00 -0700466
David Benjamin0d56f882015-12-19 17:05:56 -0500467 digests_len = tls1_handshake_digest(ssl, buf, sizeof(buf));
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (digests_len < 0) {
469 err = 1;
470 digests_len = 0;
471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin0d56f882015-12-19 17:05:56 -0500473 if (!ssl->enc_method->prf(ssl, out, 12, ssl->session->master_key,
474 ssl->session->master_key_length, str, slen, buf,
475 digests_len, NULL, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 err = 1;
477 }
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 if (err) {
480 return 0;
481 } else {
David Benjaminaf032d62014-12-22 10:42:51 -0500482 return 12;
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 }
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
David Benjamin0d56f882015-12-19 17:05:56 -0500486int tls1_generate_master_secret(SSL *ssl, uint8_t *out,
487 const uint8_t *premaster,
David Benjamin31b1d812014-12-23 10:01:09 -0500488 size_t premaster_len) {
David Benjamin0d56f882015-12-19 17:05:56 -0500489 if (ssl->s3->tmp.extended_master_secret) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 uint8_t digests[2 * EVP_MAX_MD_SIZE];
David Benjamin0d56f882015-12-19 17:05:56 -0500491 int digests_len = tls1_handshake_digest(ssl, digests, sizeof(digests));
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 if (digests_len == -1) {
493 return 0;
494 }
Adam Langley75712922014-10-10 16:23:43 -0700495
David Benjamin0d56f882015-12-19 17:05:56 -0500496 if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster,
497 premaster_len,
498 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
499 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
500 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500501 return 0;
502 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500504 if (!ssl->enc_method->prf(ssl, out, SSL3_MASTER_SECRET_SIZE, premaster,
505 premaster_len, TLS_MD_MASTER_SECRET_CONST,
506 TLS_MD_MASTER_SECRET_CONST_SIZE,
507 ssl->s3->client_random, SSL3_RANDOM_SIZE,
508 ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500509 return 0;
510 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 return SSL3_MASTER_SECRET_SIZE;
514}
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjaminf8d80712015-12-29 18:56:28 -0500516int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
517 const char *label, size_t label_len,
518 const uint8_t *context, size_t context_len,
519 int use_context) {
David Benjamin0d56f882015-12-19 17:05:56 -0500520 if (!ssl->s3->have_version || ssl->version == SSL3_VERSION) {
David Benjamind6e95ee2015-05-03 15:34:35 -0400521 return 0;
522 }
523
David Benjamincfd248b2015-04-03 11:02:24 -0400524 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400526 if (context_len >= 1u << 16) {
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamincfd248b2015-04-03 11:02:24 -0400528 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 }
David Benjamincfd248b2015-04-03 11:02:24 -0400530 seed_len += 2 + context_len;
531 }
532 uint8_t *seed = OPENSSL_malloc(seed_len);
533 if (seed == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400534 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfd248b2015-04-03 11:02:24 -0400535 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin0d56f882015-12-19 17:05:56 -0500538 memcpy(seed, ssl->s3->client_random, SSL3_RANDOM_SIZE);
539 memcpy(seed + SSL3_RANDOM_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE);
David Benjamincfd248b2015-04-03 11:02:24 -0400540 if (use_context) {
541 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
542 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
543 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin0d56f882015-12-19 17:05:56 -0500546 int ret = ssl->enc_method->prf(ssl, out, out_len, ssl->session->master_key,
547 ssl->session->master_key_length, label,
548 label_len, seed, seed_len, NULL, 0);
David Benjamincfd248b2015-04-03 11:02:24 -0400549 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 return ret;
551}
552
553int tls1_alert_code(int code) {
554 switch (code) {
555 case SSL_AD_CLOSE_NOTIFY:
556 return SSL3_AD_CLOSE_NOTIFY;
557
558 case SSL_AD_UNEXPECTED_MESSAGE:
559 return SSL3_AD_UNEXPECTED_MESSAGE;
560
561 case SSL_AD_BAD_RECORD_MAC:
562 return SSL3_AD_BAD_RECORD_MAC;
563
564 case SSL_AD_DECRYPTION_FAILED:
565 return TLS1_AD_DECRYPTION_FAILED;
566
567 case SSL_AD_RECORD_OVERFLOW:
568 return TLS1_AD_RECORD_OVERFLOW;
569
570 case SSL_AD_DECOMPRESSION_FAILURE:
571 return SSL3_AD_DECOMPRESSION_FAILURE;
572
573 case SSL_AD_HANDSHAKE_FAILURE:
574 return SSL3_AD_HANDSHAKE_FAILURE;
575
576 case SSL_AD_NO_CERTIFICATE:
577 return -1;
578
579 case SSL_AD_BAD_CERTIFICATE:
580 return SSL3_AD_BAD_CERTIFICATE;
581
582 case SSL_AD_UNSUPPORTED_CERTIFICATE:
583 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
584
585 case SSL_AD_CERTIFICATE_REVOKED:
586 return SSL3_AD_CERTIFICATE_REVOKED;
587
588 case SSL_AD_CERTIFICATE_EXPIRED:
589 return SSL3_AD_CERTIFICATE_EXPIRED;
590
591 case SSL_AD_CERTIFICATE_UNKNOWN:
592 return SSL3_AD_CERTIFICATE_UNKNOWN;
593
594 case SSL_AD_ILLEGAL_PARAMETER:
595 return SSL3_AD_ILLEGAL_PARAMETER;
596
597 case SSL_AD_UNKNOWN_CA:
598 return TLS1_AD_UNKNOWN_CA;
599
600 case SSL_AD_ACCESS_DENIED:
601 return TLS1_AD_ACCESS_DENIED;
602
603 case SSL_AD_DECODE_ERROR:
604 return TLS1_AD_DECODE_ERROR;
605
606 case SSL_AD_DECRYPT_ERROR:
607 return TLS1_AD_DECRYPT_ERROR;
608 case SSL_AD_EXPORT_RESTRICTION:
609 return TLS1_AD_EXPORT_RESTRICTION;
610
611 case SSL_AD_PROTOCOL_VERSION:
612 return TLS1_AD_PROTOCOL_VERSION;
613
614 case SSL_AD_INSUFFICIENT_SECURITY:
615 return TLS1_AD_INSUFFICIENT_SECURITY;
616
617 case SSL_AD_INTERNAL_ERROR:
618 return TLS1_AD_INTERNAL_ERROR;
619
620 case SSL_AD_USER_CANCELLED:
621 return TLS1_AD_USER_CANCELLED;
622
623 case SSL_AD_NO_RENEGOTIATION:
624 return TLS1_AD_NO_RENEGOTIATION;
625
626 case SSL_AD_UNSUPPORTED_EXTENSION:
627 return TLS1_AD_UNSUPPORTED_EXTENSION;
628
629 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
630 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
631
632 case SSL_AD_UNRECOGNIZED_NAME:
633 return TLS1_AD_UNRECOGNIZED_NAME;
634
635 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
636 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
637
638 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
639 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
640
641 case SSL_AD_UNKNOWN_PSK_IDENTITY:
642 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
643
644 case SSL_AD_INAPPROPRIATE_FALLBACK:
645 return SSL3_AD_INAPPROPRIATE_FALLBACK;
646
647 default:
648 return -1;
649 }
650}