blob: bda7c5b84be37e0d33ebc0359d1edb9820f100ae [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 Benjamin41ac9792014-12-23 10:41:06 -0500227int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
228 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
238 uint32_t algorithm_prf = ssl_get_algorithm_prf(s);
239 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 Benjamin1f5e1152014-12-23 09:23:32 -0500263static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
David Benjamin41ac9792014-12-23 10:41:06 -0500264 return s->enc_method->prf(s, out, out_len, s->session->master_key,
265 s->session->master_key_length,
266 TLS_MD_KEY_EXPANSION_CONST,
267 TLS_MD_KEY_EXPANSION_CONST_SIZE,
268 s->s3->server_random, SSL3_RANDOM_SIZE,
269 s->s3->client_random,
270 SSL3_RANDOM_SIZE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800271}
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273int tls1_change_cipher_state(SSL *s, int which) {
274 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
275 * need to update the read cipherspec. Otherwise we have just written one. */
276 const char is_read = (which & SSL3_CC_READ) != 0;
277 /* use_client_keys is true if we wish to use the keys for the "client write"
278 * direction. This is the case if we're a client sending a ChangeCipherSpec,
279 * or a server reading a client's ChangeCipherSpec. */
280 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
281 which == SSL3_CHANGE_CIPHER_SERVER_READ;
282 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
283 const uint8_t *client_write_key, *server_write_key, *key;
284 const uint8_t *client_write_iv, *server_write_iv, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 const EVP_AEAD *aead = s->s3->tmp.new_aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500286 size_t key_len, iv_len, mac_secret_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 const uint8_t *key_data;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 /* Reset sequence number to zero. */
David Benjaminf584a5a2015-11-01 14:08:42 -0500290 if (is_read) {
291 if (SSL_IS_DTLS(s)) {
292 s->d1->r_epoch++;
293 memset(&s->d1->bitmap, 0, sizeof(s->d1->bitmap));
294 }
295 memset(s->s3->read_sequence, 0, sizeof(s->s3->read_sequence));
296 } else {
297 if (SSL_IS_DTLS(s)) {
298 s->d1->w_epoch++;
299 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
300 sizeof(s->s3->write_sequence));
301 }
302 memset(s->s3->write_sequence, 0, sizeof(s->s3->write_sequence));
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700304
David Benjaminea72bd02014-12-21 21:27:41 -0500305 mac_secret_len = s->s3->tmp.new_mac_secret_len;
306 iv_len = s->s3->tmp.new_fixed_iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700307
David Benjaminb8a56f12014-12-23 11:41:02 -0500308 if (aead == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400309 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb8a56f12014-12-23 11:41:02 -0500310 return 0;
311 }
312
313 key_len = EVP_AEAD_key_length(aead);
314 if (mac_secret_len > 0) {
315 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
316 * suites) the key length reported by |EVP_AEAD_key_length| will
317 * include the MAC and IV key bytes. */
318 if (key_len < mac_secret_len + iv_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400319 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb8a56f12014-12-23 11:41:02 -0500320 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500322 key_len -= mac_secret_len + iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 key_data = s->s3->tmp.key_block;
326 client_write_mac_secret = key_data;
327 key_data += mac_secret_len;
328 server_write_mac_secret = key_data;
329 key_data += mac_secret_len;
330 client_write_key = key_data;
331 key_data += key_len;
332 server_write_key = key_data;
333 key_data += key_len;
334 client_write_iv = key_data;
335 key_data += iv_len;
336 server_write_iv = key_data;
337 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 if (use_client_keys) {
340 mac_secret = client_write_mac_secret;
341 key = client_write_key;
342 iv = client_write_iv;
343 } else {
344 mac_secret = server_write_mac_secret;
345 key = server_write_key;
346 iv = server_write_iv;
347 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
David Benjamin3570d732015-06-29 00:28:17 -0400350 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 return 0;
352 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700353
David Benjamin31a07792015-03-03 14:20:26 -0500354 if (is_read) {
355 SSL_AEAD_CTX_free(s->aead_read_ctx);
356 s->aead_read_ctx = SSL_AEAD_CTX_new(
357 evp_aead_open, ssl3_version_from_wire(s, s->version),
358 s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
359 iv_len);
360 return s->aead_read_ctx != NULL;
David Benjamin31a07792015-03-03 14:20:26 -0500361 }
David Benjaminac8302a2015-09-01 17:18:15 -0400362
363 SSL_AEAD_CTX_free(s->aead_write_ctx);
364 s->aead_write_ctx = SSL_AEAD_CTX_new(
365 evp_aead_seal, ssl3_version_from_wire(s, s->version),
366 s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
367 iv_len);
David Benjamind9f06712015-12-06 16:07:47 -0500368 return s->aead_write_ctx != NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371int tls1_setup_key_block(SSL *s) {
David Benjaminaf032d62014-12-22 10:42:51 -0500372 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 const EVP_AEAD *aead = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 int ret = 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500375 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500376 size_t key_block_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 if (s->s3->tmp.key_block_length != 0) {
379 return 1;
380 }
Adam Langley95c29f32014-06-20 12:00:00 -0700381
David Benjaminea72bd02014-12-21 21:27:41 -0500382 if (s->session->cipher == NULL) {
383 goto cipher_unavailable_err;
384 }
385
David Benjaminb8a56f12014-12-23 11:41:02 -0500386 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
387 s->session->cipher,
388 ssl3_version_from_wire(s, s->version))) {
389 goto cipher_unavailable_err;
390 }
391 key_len = EVP_AEAD_key_length(aead);
392 variable_iv_len = EVP_AEAD_nonce_length(aead);
393 if (mac_secret_len > 0) {
394 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
395 * key length reported by |EVP_AEAD_key_length| will include the MAC key
396 * bytes and initial implicit IV. */
397 if (key_len < mac_secret_len + fixed_iv_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400398 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb8a56f12014-12-23 11:41:02 -0500399 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500401 key_len -= mac_secret_len + fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 } else {
David Benjaminb8a56f12014-12-23 11:41:02 -0500403 /* The nonce is split into a fixed portion and a variable portion. */
404 if (variable_iv_len < fixed_iv_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400405 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminb8a56f12014-12-23 11:41:02 -0500406 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500408 variable_iv_len -= fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 }
Adam Langley95c29f32014-06-20 12:00:00 -0700410
David Benjaminea72bd02014-12-21 21:27:41 -0500411 assert(mac_secret_len < 256);
412 assert(fixed_iv_len < 256);
413 assert(variable_iv_len < 256);
414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 s->s3->tmp.new_aead = aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500416 s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
417 s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
418 s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700419
David Benjamin1f5e1152014-12-23 09:23:32 -0500420 key_block_len = key_len + mac_secret_len + fixed_iv_len;
421 key_block_len *= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin1f5e1152014-12-23 09:23:32 -0500425 p = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500426 if (p == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400427 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 goto err;
429 }
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin1f5e1152014-12-23 09:23:32 -0500431 s->s3->tmp.key_block_length = key_block_len;
David Benjaminaf032d62014-12-22 10:42:51 -0500432 s->s3->tmp.key_block = p;
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamin1f5e1152014-12-23 09:23:32 -0500434 if (!tls1_generate_key_block(s, p, key_block_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 goto err;
436 }
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 ret = 1;
439
Adam Langley95c29f32014-06-20 12:00:00 -0700440err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 return ret;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700442
443cipher_unavailable_err:
David Benjamin3570d732015-06-29 00:28:17 -0400444 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 return 0;
446}
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langleyfcf25832014-12-18 17:42:32 -0800448int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
David Benjaminb0883312015-08-06 09:54:13 -0400449 const EVP_MD_CTX *ctx_template;
450 if (md_nid == NID_md5) {
451 ctx_template = &s->s3->handshake_md5;
452 } else if (md_nid == EVP_MD_CTX_type(&s->s3->handshake_hash)) {
453 ctx_template = &s->s3->handshake_hash;
454 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400455 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 return 0;
457 }
458
David Benjaminb0883312015-08-06 09:54:13 -0400459 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 EVP_MD_CTX_init(&ctx);
David Benjaminb0883312015-08-06 09:54:13 -0400461 if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
David Benjamin9d0847a2015-02-16 03:57:55 -0500462 EVP_MD_CTX_cleanup(&ctx);
463 return 0;
464 }
David Benjaminb0883312015-08-06 09:54:13 -0400465 unsigned ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 EVP_DigestFinal_ex(&ctx, out, &ret);
467 EVP_MD_CTX_cleanup(&ctx);
David Benjaminb0883312015-08-06 09:54:13 -0400468 return ret;
469}
Adam Langleyfcf25832014-12-18 17:42:32 -0800470
David Benjaminb0883312015-08-06 09:54:13 -0400471static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
472 size_t max_out) {
473 int ret = 0;
474 EVP_MD_CTX ctx_copy;
475 EVP_MD_CTX_init(&ctx_copy);
476
477 if (EVP_MD_CTX_size(ctx) > max_out) {
478 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
479 goto err;
480 }
481 unsigned len;
482 if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
483 !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
484 goto err;
485 }
486 assert(len == EVP_MD_CTX_size(ctx));
487
488 *out_len = len;
489 ret = 1;
490
491err:
492 EVP_MD_CTX_cleanup(&ctx_copy);
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 return ret;
494}
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langley1258b6a2014-06-20 12:00:00 -0700496/* tls1_handshake_digest calculates the current handshake hash and writes it to
497 * |out|, which has space for |out_len| bytes. It returns the number of bytes
498 * written or -1 in the event of an error. This function works on a copy of the
499 * underlying digests so can be called multiple times and prior to the final
500 * update etc. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800501int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
David Benjaminb0883312015-08-06 09:54:13 -0400502 size_t md5_len = 0;
503 if (EVP_MD_CTX_md(&s->s3->handshake_md5) != NULL &&
504 !append_digest(&s->s3->handshake_md5, out, &md5_len, out_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 return -1;
506 }
David Benjaminb0883312015-08-06 09:54:13 -0400507
508 size_t len;
509 if (!append_digest(&s->s3->handshake_hash, out + md5_len, &len,
510 out_len - md5_len)) {
511 return -1;
512 }
513
514 return (int)(md5_len + len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515}
Adam Langley1258b6a2014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
518 uint8_t buf[2 * EVP_MAX_MD_SIZE];
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 int err = 0;
520 int digests_len;
Adam Langley1258b6a2014-06-20 12:00:00 -0700521
David Benjamine76ccae2015-05-26 17:41:50 -0400522 /* At this point, the handshake should have released the handshake buffer on
David Benjamin9550c3a2015-08-05 08:50:34 -0400523 * its own. */
David Benjamine76ccae2015-05-26 17:41:50 -0400524 assert(s->s3->handshake_buffer == NULL);
Adam Langley1258b6a2014-06-20 12:00:00 -0700525
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
527 if (digests_len < 0) {
528 err = 1;
529 digests_len = 0;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamin41ac9792014-12-23 10:41:06 -0500532 if (!s->enc_method->prf(s, out, 12, s->session->master_key,
533 s->session->master_key_length, str, slen, buf,
534 digests_len, NULL, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 err = 1;
536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 if (err) {
539 return 0;
540 } else {
David Benjaminaf032d62014-12-22 10:42:51 -0500541 return 12;
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 }
543}
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin31b1d812014-12-23 10:01:09 -0500545int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
546 size_t premaster_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 if (s->s3->tmp.extended_master_secret) {
548 uint8_t digests[2 * EVP_MAX_MD_SIZE];
David Benjamin9550c3a2015-08-05 08:50:34 -0400549 int digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 if (digests_len == -1) {
551 return 0;
552 }
Adam Langley75712922014-10-10 16:23:43 -0700553
David Benjamin41ac9792014-12-23 10:41:06 -0500554 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
555 premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
556 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
557 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500558 return 0;
559 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 } else {
David Benjamin41ac9792014-12-23 10:41:06 -0500561 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
562 premaster_len, TLS_MD_MASTER_SECRET_CONST,
563 TLS_MD_MASTER_SECRET_CONST_SIZE,
564 s->s3->client_random, SSL3_RANDOM_SIZE,
565 s->s3->server_random, SSL3_RANDOM_SIZE)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500566 return 0;
567 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 return SSL3_MASTER_SECRET_SIZE;
571}
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamincfd248b2015-04-03 11:02:24 -0400573int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
574 const char *label, size_t label_len,
575 const uint8_t *context, size_t context_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 int use_context) {
David Benjamind6e95ee2015-05-03 15:34:35 -0400577 if (!s->s3->have_version || s->version == SSL3_VERSION) {
David Benjamin3570d732015-06-29 00:28:17 -0400578 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
David Benjamind6e95ee2015-05-03 15:34:35 -0400579 return 0;
580 }
581
David Benjamincfd248b2015-04-03 11:02:24 -0400582 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400584 if (context_len >= 1u << 16) {
David Benjamin3570d732015-06-29 00:28:17 -0400585 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamincfd248b2015-04-03 11:02:24 -0400586 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 }
David Benjamincfd248b2015-04-03 11:02:24 -0400588 seed_len += 2 + context_len;
589 }
590 uint8_t *seed = OPENSSL_malloc(seed_len);
591 if (seed == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400592 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfd248b2015-04-03 11:02:24 -0400593 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
David Benjamincfd248b2015-04-03 11:02:24 -0400596 memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
597 memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
598 if (use_context) {
599 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
600 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
601 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 }
Adam Langley95c29f32014-06-20 12:00:00 -0700603
David Benjamincfd248b2015-04-03 11:02:24 -0400604 int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
605 s->session->master_key_length, label, label_len,
606 seed, seed_len, NULL, 0);
607 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800608 return ret;
609}
610
611int tls1_alert_code(int code) {
612 switch (code) {
613 case SSL_AD_CLOSE_NOTIFY:
614 return SSL3_AD_CLOSE_NOTIFY;
615
616 case SSL_AD_UNEXPECTED_MESSAGE:
617 return SSL3_AD_UNEXPECTED_MESSAGE;
618
619 case SSL_AD_BAD_RECORD_MAC:
620 return SSL3_AD_BAD_RECORD_MAC;
621
622 case SSL_AD_DECRYPTION_FAILED:
623 return TLS1_AD_DECRYPTION_FAILED;
624
625 case SSL_AD_RECORD_OVERFLOW:
626 return TLS1_AD_RECORD_OVERFLOW;
627
628 case SSL_AD_DECOMPRESSION_FAILURE:
629 return SSL3_AD_DECOMPRESSION_FAILURE;
630
631 case SSL_AD_HANDSHAKE_FAILURE:
632 return SSL3_AD_HANDSHAKE_FAILURE;
633
634 case SSL_AD_NO_CERTIFICATE:
635 return -1;
636
637 case SSL_AD_BAD_CERTIFICATE:
638 return SSL3_AD_BAD_CERTIFICATE;
639
640 case SSL_AD_UNSUPPORTED_CERTIFICATE:
641 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
642
643 case SSL_AD_CERTIFICATE_REVOKED:
644 return SSL3_AD_CERTIFICATE_REVOKED;
645
646 case SSL_AD_CERTIFICATE_EXPIRED:
647 return SSL3_AD_CERTIFICATE_EXPIRED;
648
649 case SSL_AD_CERTIFICATE_UNKNOWN:
650 return SSL3_AD_CERTIFICATE_UNKNOWN;
651
652 case SSL_AD_ILLEGAL_PARAMETER:
653 return SSL3_AD_ILLEGAL_PARAMETER;
654
655 case SSL_AD_UNKNOWN_CA:
656 return TLS1_AD_UNKNOWN_CA;
657
658 case SSL_AD_ACCESS_DENIED:
659 return TLS1_AD_ACCESS_DENIED;
660
661 case SSL_AD_DECODE_ERROR:
662 return TLS1_AD_DECODE_ERROR;
663
664 case SSL_AD_DECRYPT_ERROR:
665 return TLS1_AD_DECRYPT_ERROR;
666 case SSL_AD_EXPORT_RESTRICTION:
667 return TLS1_AD_EXPORT_RESTRICTION;
668
669 case SSL_AD_PROTOCOL_VERSION:
670 return TLS1_AD_PROTOCOL_VERSION;
671
672 case SSL_AD_INSUFFICIENT_SECURITY:
673 return TLS1_AD_INSUFFICIENT_SECURITY;
674
675 case SSL_AD_INTERNAL_ERROR:
676 return TLS1_AD_INTERNAL_ERROR;
677
678 case SSL_AD_USER_CANCELLED:
679 return TLS1_AD_USER_CANCELLED;
680
681 case SSL_AD_NO_RENEGOTIATION:
682 return TLS1_AD_NO_RENEGOTIATION;
683
684 case SSL_AD_UNSUPPORTED_EXTENSION:
685 return TLS1_AD_UNSUPPORTED_EXTENSION;
686
687 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
688 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
689
690 case SSL_AD_UNRECOGNIZED_NAME:
691 return TLS1_AD_UNRECOGNIZED_NAME;
692
693 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
694 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
695
696 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
697 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
698
699 case SSL_AD_UNKNOWN_PSK_IDENTITY:
700 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
701
702 case SSL_AD_INAPPROPRIATE_FALLBACK:
703 return SSL3_AD_INAPPROPRIATE_FALLBACK;
704
705 default:
706 return -1;
707 }
708}