blob: ffe16be8d0bea630fd919befd9b0e9f6eb5fd0d6 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700136#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400137#include <stdio.h>
138#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700139
Adam Langley95c29f32014-06-20 12:00:00 -0700140#include <openssl/err.h>
141#include <openssl/evp.h>
142#include <openssl/hmac.h>
143#include <openssl/md5.h>
144#include <openssl/mem.h>
145#include <openssl/obj.h>
146#include <openssl/rand.h>
147
David Benjamin2ee94aa2015-04-07 22:38:30 -0400148#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin1f5e1152014-12-23 09:23:32 -0500151/* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
152 * section 5. It writes |out_len| bytes to |out|, using |md| as the hash and
153 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
154 * seed parameter. It returns one on success and zero on failure. */
155static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
156 const uint8_t *secret, size_t secret_len,
157 const uint8_t *seed1, size_t seed1_len,
158 const uint8_t *seed2, size_t seed2_len,
159 const uint8_t *seed3, size_t seed3_len) {
160 size_t chunk;
161 HMAC_CTX ctx, ctx_tmp, ctx_init;
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 uint8_t A1[EVP_MAX_MD_SIZE];
David Benjamin1f5e1152014-12-23 09:23:32 -0500163 unsigned A1_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langleyfcf25832014-12-18 17:42:32 -0800166 chunk = EVP_MD_size(md);
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin1f5e1152014-12-23 09:23:32 -0500168 HMAC_CTX_init(&ctx);
169 HMAC_CTX_init(&ctx_tmp);
170 HMAC_CTX_init(&ctx_init);
171 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
172 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
173 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
174 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
175 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) ||
176 !HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 goto err;
178 }
179
180 for (;;) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500181 /* Reinit mac contexts. */
182 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
183 !HMAC_Update(&ctx, A1, A1_len) ||
184 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
185 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
186 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
187 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800188 goto err;
189 }
190
David Benjamin1f5e1152014-12-23 09:23:32 -0500191 if (out_len > chunk) {
192 unsigned len;
193 if (!HMAC_Final(&ctx, out, &len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 goto err;
195 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500196 assert(len == chunk);
197 out += len;
198 out_len -= len;
199 /* Calculate the next A1 value. */
200 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 goto err;
202 }
203 } else {
David Benjamin1f5e1152014-12-23 09:23:32 -0500204 /* Last chunk. */
205 if (!HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 goto err;
207 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500208 memcpy(out, A1, out_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 break;
210 }
211 }
212
213 ret = 1;
214
Adam Langley95c29f32014-06-20 12:00:00 -0700215err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500216 HMAC_CTX_cleanup(&ctx);
217 HMAC_CTX_cleanup(&ctx_tmp);
218 HMAC_CTX_cleanup(&ctx_init);
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 OPENSSL_cleanse(A1, sizeof(A1));
220 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700221}
Adam Langley9447dff2014-06-24 17:29:06 -0700222
David Benjamin41ac9792014-12-23 10:41:06 -0500223int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
224 size_t secret_len, const char *label, size_t label_len,
225 const uint8_t *seed1, size_t seed1_len,
226 const uint8_t *seed2, size_t seed2_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500227 size_t idx, len, count, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 const uint8_t *S1;
David Benjamin107db582015-04-08 00:41:59 -0400229 uint32_t m;
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 const EVP_MD *md;
231 int ret = 0;
David Benjaminaf032d62014-12-22 10:42:51 -0500232 uint8_t *tmp;
233
David Benjamin1f5e1152014-12-23 09:23:32 -0500234 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500235 return 1;
236 }
237
238 /* Allocate a temporary buffer. */
David Benjamin1f5e1152014-12-23 09:23:32 -0500239 tmp = OPENSSL_malloc(out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500240 if (tmp == NULL) {
David Benjamin41ac9792014-12-23 10:41:06 -0500241 OPENSSL_PUT_ERROR(SSL, tls1_prf, ERR_R_MALLOC_FAILURE);
David Benjaminaf032d62014-12-22 10:42:51 -0500242 return 0;
243 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800244
David Benjamin1f5e1152014-12-23 09:23:32 -0500245 /* Count number of digests and partition |secret| evenly. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 count = 0;
David Benjamin71f07942015-04-08 02:36:59 -0400247 for (idx = 0; ssl_get_handshake_digest(&m, &md, idx); idx++) {
David Benjamin41ac9792014-12-23 10:41:06 -0500248 if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 count++;
250 }
251 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500252 /* TODO(davidben): The only case where count isn't 1 is the old MD5/SHA-1
253 * combination. The logic around multiple handshake digests can probably be
254 * simplified. */
255 assert(count == 1 || count == 2);
256 len = secret_len / count;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 if (count == 1) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500258 secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500260 S1 = secret;
261 memset(out, 0, out_len);
David Benjamin71f07942015-04-08 02:36:59 -0400262 for (idx = 0; ssl_get_handshake_digest(&m, &md, idx); idx++) {
David Benjamin41ac9792014-12-23 10:41:06 -0500263 if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500264 /* If |count| is 2 and |secret_len| is odd, |secret| is partitioned into
265 * two halves with an overlapping byte. */
266 if (!tls1_P_hash(tmp, out_len, md, S1, len + (secret_len & 1),
267 (const uint8_t *)label, label_len, seed1, seed1_len,
268 seed2, seed2_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 goto err;
270 }
271 S1 += len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500272 for (i = 0; i < out_len; i++) {
David Benjaminaf032d62014-12-22 10:42:51 -0500273 out[i] ^= tmp[i];
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 }
275 }
276 }
277 ret = 1;
278
279err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500280 OPENSSL_cleanse(tmp, out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500281 OPENSSL_free(tmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 return ret;
283}
284
David Benjamin1f5e1152014-12-23 09:23:32 -0500285static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
David Benjamin41ac9792014-12-23 10:41:06 -0500286 return s->enc_method->prf(s, out, out_len, s->session->master_key,
287 s->session->master_key_length,
288 TLS_MD_KEY_EXPANSION_CONST,
289 TLS_MD_KEY_EXPANSION_CONST_SIZE,
290 s->s3->server_random, SSL3_RANDOM_SIZE,
291 s->s3->client_random,
292 SSL3_RANDOM_SIZE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800293}
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langleyc9fb3752014-06-20 12:00:00 -0700295/* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It
296 * returns 0 on malloc error. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800297static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx) {
298 if (*aead_ctx != NULL) {
299 EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
300 } else {
301 *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
302 if (*aead_ctx == NULL) {
303 OPENSSL_PUT_ERROR(SSL, tls1_aead_ctx_init, ERR_R_MALLOC_FAILURE);
304 return 0;
305 }
306 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 return 1;
309}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700310
311static int tls1_change_cipher_state_aead(SSL *s, char is_read,
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 const uint8_t *key, unsigned key_len,
313 const uint8_t *iv, unsigned iv_len,
314 const uint8_t *mac_secret,
315 unsigned mac_secret_len) {
316 const EVP_AEAD *aead = s->s3->tmp.new_aead;
317 SSL_AEAD_CTX *aead_ctx;
David Benjaminea72bd02014-12-21 21:27:41 -0500318 /* merged_key is used to merge the MAC, cipher, and IV keys for an AEAD which
319 * simulates pre-AEAD cipher suites. */
320 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
Adam Langley9447dff2014-06-24 17:29:06 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 if (mac_secret_len > 0) {
323 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
324 * suites). */
David Benjaminea72bd02014-12-21 21:27:41 -0500325 if (mac_secret_len + key_len + iv_len > sizeof(merged_key)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead,
327 ERR_R_INTERNAL_ERROR);
328 return 0;
329 }
David Benjaminea72bd02014-12-21 21:27:41 -0500330 memcpy(merged_key, mac_secret, mac_secret_len);
331 memcpy(merged_key + mac_secret_len, key, key_len);
332 memcpy(merged_key + mac_secret_len + key_len, iv, iv_len);
333 key = merged_key;
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 key_len += mac_secret_len;
David Benjaminea72bd02014-12-21 21:27:41 -0500335 key_len += iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 if (is_read) {
339 if (!tls1_aead_ctx_init(&s->aead_read_ctx)) {
340 return 0;
341 }
342 aead_ctx = s->aead_read_ctx;
343 } else {
David Benjaminafbc63f2015-02-01 02:33:59 -0500344 if (SSL_IS_DTLS(s) && s->aead_write_ctx != NULL) {
345 /* DTLS renegotiation is unsupported, so a CCS can only switch away from
346 * the NULL cipher. This simplifies renegotiation. */
347 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead,
348 ERR_R_INTERNAL_ERROR);
349 return 0;
David Benjamine95d20d2014-12-23 11:16:01 -0500350 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 if (!tls1_aead_ctx_init(&s->aead_write_ctx)) {
352 return 0;
353 }
354 aead_ctx = s->aead_write_ctx;
355 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700356
David Benjamina07c0fc2015-05-13 13:19:42 -0400357 aead_ctx->cipher = s->session->cipher;
358
David Benjaminb34f5102015-02-28 03:59:33 -0500359 if (!EVP_AEAD_CTX_init_with_direction(
360 &aead_ctx->ctx, aead, key, key_len, EVP_AEAD_DEFAULT_TAG_LENGTH,
361 is_read ? evp_aead_open : evp_aead_seal)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 OPENSSL_free(aead_ctx);
363 if (is_read) {
364 s->aead_read_ctx = NULL;
365 } else {
366 s->aead_write_ctx = NULL;
367 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700371
David Benjaminea72bd02014-12-21 21:27:41 -0500372 if (mac_secret_len == 0) {
373 /* For a real AEAD, the IV is the fixed part of the nonce. */
374 if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
375 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
376 return 0;
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378
David Benjaminea72bd02014-12-21 21:27:41 -0500379 memcpy(aead_ctx->fixed_nonce, iv, iv_len);
380 aead_ctx->fixed_nonce_len = iv_len;
381 aead_ctx->variable_nonce_included_in_record =
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 (s->s3->tmp.new_cipher->algorithm2 &
383 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500384 aead_ctx->random_variable_nonce = 0;
385 aead_ctx->omit_length_in_ad = 0;
386 } else {
387 aead_ctx->fixed_nonce_len = 0;
388 aead_ctx->variable_nonce_included_in_record = 1;
389 aead_ctx->random_variable_nonce = 1;
390 aead_ctx->omit_length_in_ad = 1;
391 }
392 aead_ctx->variable_nonce_len = s->s3->tmp.new_variable_iv_len;
David Benjamin044abb02014-12-23 10:57:17 -0500393 aead_ctx->omit_version_in_ad = (s->version == SSL3_VERSION);
David Benjaminea72bd02014-12-21 21:27:41 -0500394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
396 EVP_AEAD_nonce_length(aead)) {
397 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
398 return 0;
399 }
400 aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
401
402 return 1;
403}
404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405int tls1_change_cipher_state(SSL *s, int which) {
406 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
407 * need to update the read cipherspec. Otherwise we have just written one. */
408 const char is_read = (which & SSL3_CC_READ) != 0;
409 /* use_client_keys is true if we wish to use the keys for the "client write"
410 * direction. This is the case if we're a client sending a ChangeCipherSpec,
411 * or a server reading a client's ChangeCipherSpec. */
412 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
413 which == SSL3_CHANGE_CIPHER_SERVER_READ;
414 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
415 const uint8_t *client_write_key, *server_write_key, *key;
416 const uint8_t *client_write_iv, *server_write_iv, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 const EVP_AEAD *aead = s->s3->tmp.new_aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500418 size_t key_len, iv_len, mac_secret_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 const uint8_t *key_data;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 /* Reset sequence number to zero. */
422 if (!SSL_IS_DTLS(s)) {
423 memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8);
424 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700425
David Benjaminea72bd02014-12-21 21:27:41 -0500426 mac_secret_len = s->s3->tmp.new_mac_secret_len;
427 iv_len = s->s3->tmp.new_fixed_iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700428
David Benjaminb8a56f12014-12-23 11:41:02 -0500429 if (aead == NULL) {
430 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
431 return 0;
432 }
433
434 key_len = EVP_AEAD_key_length(aead);
435 if (mac_secret_len > 0) {
436 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
437 * suites) the key length reported by |EVP_AEAD_key_length| will
438 * include the MAC and IV key bytes. */
439 if (key_len < mac_secret_len + iv_len) {
440 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
441 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500443 key_len -= mac_secret_len + iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 key_data = s->s3->tmp.key_block;
447 client_write_mac_secret = key_data;
448 key_data += mac_secret_len;
449 server_write_mac_secret = key_data;
450 key_data += mac_secret_len;
451 client_write_key = key_data;
452 key_data += key_len;
453 server_write_key = key_data;
454 key_data += key_len;
455 client_write_iv = key_data;
456 key_data += iv_len;
457 server_write_iv = key_data;
458 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700459
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 if (use_client_keys) {
461 mac_secret = client_write_mac_secret;
462 key = client_write_key;
463 iv = client_write_iv;
464 } else {
465 mac_secret = server_write_mac_secret;
466 key = server_write_key;
467 iv = server_write_iv;
468 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
471 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
472 return 0;
473 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700474
David Benjaminb8a56f12014-12-23 11:41:02 -0500475 return tls1_change_cipher_state_aead(s, is_read, key, key_len, iv, iv_len,
476 mac_secret, mac_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800477}
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479int tls1_setup_key_block(SSL *s) {
David Benjaminaf032d62014-12-22 10:42:51 -0500480 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 const EVP_AEAD *aead = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 int ret = 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500483 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500484 size_t key_block_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 if (s->s3->tmp.key_block_length != 0) {
487 return 1;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjaminea72bd02014-12-21 21:27:41 -0500490 if (s->session->cipher == NULL) {
491 goto cipher_unavailable_err;
492 }
493
David Benjaminb8a56f12014-12-23 11:41:02 -0500494 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
495 s->session->cipher,
496 ssl3_version_from_wire(s, s->version))) {
497 goto cipher_unavailable_err;
498 }
499 key_len = EVP_AEAD_key_length(aead);
500 variable_iv_len = EVP_AEAD_nonce_length(aead);
501 if (mac_secret_len > 0) {
502 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
503 * key length reported by |EVP_AEAD_key_length| will include the MAC key
504 * bytes and initial implicit IV. */
505 if (key_len < mac_secret_len + fixed_iv_len) {
506 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
507 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500509 key_len -= mac_secret_len + fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 } else {
David Benjaminb8a56f12014-12-23 11:41:02 -0500511 /* The nonce is split into a fixed portion and a variable portion. */
512 if (variable_iv_len < fixed_iv_len) {
513 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
514 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500516 variable_iv_len -= fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 }
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjaminea72bd02014-12-21 21:27:41 -0500519 assert(mac_secret_len < 256);
520 assert(fixed_iv_len < 256);
521 assert(variable_iv_len < 256);
522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 s->s3->tmp.new_aead = aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500524 s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
525 s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
526 s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700527
David Benjamin1f5e1152014-12-23 09:23:32 -0500528 key_block_len = key_len + mac_secret_len + fixed_iv_len;
529 key_block_len *= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin1f5e1152014-12-23 09:23:32 -0500533 p = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500534 if (p == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE);
536 goto err;
537 }
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin1f5e1152014-12-23 09:23:32 -0500539 s->s3->tmp.key_block_length = key_block_len;
David Benjaminaf032d62014-12-22 10:42:51 -0500540 s->s3->tmp.key_block = p;
Adam Langley95c29f32014-06-20 12:00:00 -0700541
David Benjamin1f5e1152014-12-23 09:23:32 -0500542 if (!tls1_generate_key_block(s, p, key_block_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 goto err;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 if (!SSL_USE_EXPLICIT_IV(s) &&
547 (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) {
548 /* enable vulnerability countermeasure for CBC ciphers with known-IV
549 * problem (http://www.openssl.org/~bodo/tls-cbc.txt). */
550 s->s3->need_record_splitting = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 if (s->session->cipher != NULL &&
553 s->session->cipher->algorithm_enc == SSL_RC4) {
554 s->s3->need_record_splitting = 0;
555 }
556 }
557
558 ret = 1;
559
Adam Langley95c29f32014-06-20 12:00:00 -0700560err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 return ret;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700562
563cipher_unavailable_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block,
565 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
566 return 0;
567}
Adam Langley95c29f32014-06-20 12:00:00 -0700568
Adam Langleyfcf25832014-12-18 17:42:32 -0800569/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|,
David Benjamin1e52eca2015-01-22 15:33:51 -0500570 * respectively. It returns one on success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800571int tls1_enc(SSL *s, int send) {
572 SSL3_RECORD *rec;
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 const SSL_AEAD_CTX *aead;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 if (send) {
576 rec = &s->s3->wrec;
577 aead = s->aead_write_ctx;
578 } else {
579 rec = &s->s3->rrec;
580 aead = s->aead_read_ctx;
581 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700582
David Benjaminafbc63f2015-02-01 02:33:59 -0500583 if (aead == NULL) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500584 /* Handle the initial NULL cipher. */
David Benjaminb8a56f12014-12-23 11:41:02 -0500585 memmove(rec->data, rec->input, rec->length);
586 rec->input = rec->data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 return 1;
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjaminb8a56f12014-12-23 11:41:02 -0500590 uint8_t ad[13], *seq, *in, *out, nonce[EVP_AEAD_MAX_NONCE_LENGTH];
591 unsigned nonce_used;
592 size_t n, ad_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700593
David Benjaminb8a56f12014-12-23 11:41:02 -0500594 seq = send ? s->s3->write_sequence : s->s3->read_sequence;
595
596 if (SSL_IS_DTLS(s)) {
597 uint8_t dtlsseq[9], *p = dtlsseq;
598
599 s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
600 memcpy(p, &seq[2], 6);
601 memcpy(ad, dtlsseq, 8);
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 } else {
David Benjaminb8a56f12014-12-23 11:41:02 -0500603 memcpy(ad, seq, 8);
David Benjamind81e73d2015-04-05 00:21:39 -0400604 if (!ssl3_record_sequence_update(seq, 8)) {
605 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 }
607 }
Adam Langley95c29f32014-06-20 12:00:00 -0700608
David Benjaminb8a56f12014-12-23 11:41:02 -0500609 ad[8] = rec->type;
610 ad_len = 9;
611 if (!aead->omit_version_in_ad) {
612 ad[ad_len++] = (uint8_t)(s->version >> 8);
613 ad[ad_len++] = (uint8_t)(s->version);
614 }
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjaminb8a56f12014-12-23 11:41:02 -0500616 if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce)) {
617 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500618 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
David Benjaminb8a56f12014-12-23 11:41:02 -0500621 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
622 nonce_used = aead->fixed_nonce_len;
623
624 if (send) {
625 size_t len = rec->length;
626 size_t eivlen = 0;
627 in = rec->input;
628 out = rec->data;
629
630 uint8_t *variable_nonce = nonce + nonce_used;
631 if (aead->random_variable_nonce) {
632 assert(aead->variable_nonce_included_in_record);
633 if (!RAND_bytes(nonce + nonce_used, aead->variable_nonce_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500634 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500636 } else {
637 /* When sending we use the sequence number as the variable part of the
638 * nonce. */
639 if (aead->variable_nonce_len != 8) {
640 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500641 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500642 }
643 memcpy(nonce + nonce_used, ad, aead->variable_nonce_len);
644 }
645 nonce_used += aead->variable_nonce_len;
646
647 /* in do_ssl3_write, rec->input is moved forward by variable_nonce_len in
648 * order to leave space for the variable nonce. Thus we can copy the
649 * sequence number bytes into place without overwriting any of the
650 * plaintext. */
651 if (aead->variable_nonce_included_in_record) {
652 memcpy(out, variable_nonce, aead->variable_nonce_len);
653 len -= aead->variable_nonce_len;
654 eivlen = aead->variable_nonce_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjaminb8a56f12014-12-23 11:41:02 -0500657 if (!aead->omit_length_in_ad) {
658 ad[ad_len++] = len >> 8;
659 ad[ad_len++] = len & 0xff;
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 }
Adam Langley95c29f32014-06-20 12:00:00 -0700661
David Benjaminb8a56f12014-12-23 11:41:02 -0500662 if (!EVP_AEAD_CTX_seal(&aead->ctx, out + eivlen, &n, len + aead->tag_len,
663 nonce, nonce_used, in + eivlen, len, ad, ad_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500664 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
David Benjaminb8a56f12014-12-23 11:41:02 -0500667 if (aead->variable_nonce_included_in_record) {
668 n += aead->variable_nonce_len;
669 }
670 } else {
671 /* receive */
672 size_t len = rec->length;
673
674 if (rec->data != rec->input) {
675 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500676 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500677 }
678 out = in = rec->input;
679
680 if (len < aead->variable_nonce_len) {
681 return 0;
682 }
683 memcpy(nonce + nonce_used,
684 aead->variable_nonce_included_in_record ? in : ad,
685 aead->variable_nonce_len);
686 nonce_used += aead->variable_nonce_len;
687
688 if (aead->variable_nonce_included_in_record) {
689 in += aead->variable_nonce_len;
690 len -= aead->variable_nonce_len;
691 out += aead->variable_nonce_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 }
Adam Langley95c29f32014-06-20 12:00:00 -0700693
David Benjaminb8a56f12014-12-23 11:41:02 -0500694 if (!aead->omit_length_in_ad) {
695 if (len < aead->tag_len) {
696 return 0;
697 }
698 size_t plaintext_len = len - aead->tag_len;
699
700 ad[ad_len++] = plaintext_len >> 8;
701 ad[ad_len++] = plaintext_len & 0xff;
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500703
704 if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, rec->length, nonce, nonce_used, in,
705 len, ad, ad_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500706 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500708
709 rec->data = rec->input = out;
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500711
712 rec->length = n;
713 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800714}
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
717 unsigned int ret;
718 EVP_MD_CTX ctx, *d = NULL;
719 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Adam Langleyfcf25832014-12-18 17:42:32 -0800721 if (s->s3->handshake_buffer &&
722 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
723 return 0;
724 }
725
726 for (i = 0; i < SSL_MAX_DIGEST; i++) {
727 if (s->s3->handshake_dgst[i] &&
728 EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
729 d = s->s3->handshake_dgst[i];
730 break;
731 }
732 }
733
734 if (!d) {
735 OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST);
736 return 0;
737 }
738
739 EVP_MD_CTX_init(&ctx);
David Benjamin9d0847a2015-02-16 03:57:55 -0500740 if (!EVP_MD_CTX_copy_ex(&ctx, d)) {
741 EVP_MD_CTX_cleanup(&ctx);
742 return 0;
743 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 EVP_DigestFinal_ex(&ctx, out, &ret);
745 EVP_MD_CTX_cleanup(&ctx);
746
747 return ret;
748}
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langley1258b6a2014-06-20 12:00:00 -0700750/* tls1_handshake_digest calculates the current handshake hash and writes it to
751 * |out|, which has space for |out_len| bytes. It returns the number of bytes
752 * written or -1 in the event of an error. This function works on a copy of the
753 * underlying digests so can be called multiple times and prior to the final
754 * update etc. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800755int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
756 const EVP_MD *md;
757 EVP_MD_CTX ctx;
David Benjamin1f5e1152014-12-23 09:23:32 -0500758 int err = 0, len = 0;
759 size_t i;
David Benjamin107db582015-04-08 00:41:59 -0400760 uint32_t mask;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 EVP_MD_CTX_init(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700763
David Benjamin71f07942015-04-08 02:36:59 -0400764 for (i = 0; ssl_get_handshake_digest(&mask, &md, i); i++) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500765 size_t hash_size;
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 unsigned int digest_len;
767 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i];
Adam Langley1258b6a2014-06-20 12:00:00 -0700768
Adam Langleyfcf25832014-12-18 17:42:32 -0800769 if ((mask & ssl_get_algorithm2(s)) == 0) {
770 continue;
771 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 hash_size = EVP_MD_size(md);
774 if (!hdgst ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500775 hash_size > out_len ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 !EVP_MD_CTX_copy_ex(&ctx, hdgst) ||
777 !EVP_DigestFinal_ex(&ctx, out, &digest_len) ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500778 digest_len != hash_size /* internal error */) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 err = 1;
780 break;
781 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 out += digest_len;
784 out_len -= digest_len;
785 len += digest_len;
786 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 EVP_MD_CTX_cleanup(&ctx);
Adam Langley1258b6a2014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 if (err != 0) {
791 return -1;
792 }
793 return len;
794}
Adam Langley1258b6a2014-06-20 12:00:00 -0700795
Adam Langleyfcf25832014-12-18 17:42:32 -0800796int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
797 uint8_t buf[2 * EVP_MAX_MD_SIZE];
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 int err = 0;
799 int digests_len;
Adam Langley1258b6a2014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 if (s->s3->handshake_buffer &&
802 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
803 return 0;
804 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700805
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
807 if (digests_len < 0) {
808 err = 1;
809 digests_len = 0;
810 }
Adam Langley95c29f32014-06-20 12:00:00 -0700811
David Benjamin41ac9792014-12-23 10:41:06 -0500812 if (!s->enc_method->prf(s, out, 12, s->session->master_key,
813 s->session->master_key_length, str, slen, buf,
814 digests_len, NULL, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 err = 1;
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (err) {
819 return 0;
820 } else {
David Benjaminaf032d62014-12-22 10:42:51 -0500821 return 12;
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 }
823}
Adam Langley95c29f32014-06-20 12:00:00 -0700824
David Benjamin31b1d812014-12-23 10:01:09 -0500825int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
826 size_t premaster_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 if (s->s3->tmp.extended_master_secret) {
828 uint8_t digests[2 * EVP_MAX_MD_SIZE];
829 int digests_len;
Adam Langley75712922014-10-10 16:23:43 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 /* The master secret is based on the handshake hash just after sending the
832 * ClientKeyExchange. However, we might have a client certificate to send,
833 * in which case we might need different hashes for the verification and
834 * thus still need the handshake buffer around. Keeping both a handshake
835 * buffer *and* running hashes isn't yet supported so, when it comes to
836 * calculating the Finished hash, we'll have to hash the handshake buffer
837 * again. */
838 if (s->s3->handshake_buffer &&
839 !ssl3_digest_cached_records(s, dont_free_handshake_buffer)) {
840 return 0;
841 }
Adam Langley75712922014-10-10 16:23:43 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (digests_len == -1) {
845 return 0;
846 }
Adam Langley75712922014-10-10 16:23:43 -0700847
David Benjamin41ac9792014-12-23 10:41:06 -0500848 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
849 premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
850 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
851 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500852 return 0;
853 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800854 } else {
David Benjamin41ac9792014-12-23 10:41:06 -0500855 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
856 premaster_len, TLS_MD_MASTER_SECRET_CONST,
857 TLS_MD_MASTER_SECRET_CONST_SIZE,
858 s->s3->client_random, SSL3_RANDOM_SIZE,
859 s->s3->server_random, SSL3_RANDOM_SIZE)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500860 return 0;
861 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 return SSL3_MASTER_SECRET_SIZE;
865}
Adam Langley95c29f32014-06-20 12:00:00 -0700866
David Benjamincfd248b2015-04-03 11:02:24 -0400867int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
868 const char *label, size_t label_len,
869 const uint8_t *context, size_t context_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 int use_context) {
David Benjamind6e95ee2015-05-03 15:34:35 -0400871 if (!s->s3->have_version || s->version == SSL3_VERSION) {
872 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material,
873 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
874 return 0;
875 }
876
David Benjamincfd248b2015-04-03 11:02:24 -0400877 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400879 if (context_len >= 1u << 16) {
880 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_OVERFLOW);
881 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 }
David Benjamincfd248b2015-04-03 11:02:24 -0400883 seed_len += 2 + context_len;
884 }
885 uint8_t *seed = OPENSSL_malloc(seed_len);
886 if (seed == NULL) {
887 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE);
888 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
David Benjamincfd248b2015-04-03 11:02:24 -0400891 memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
892 memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
893 if (use_context) {
894 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
895 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
896 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
David Benjamincfd248b2015-04-03 11:02:24 -0400899 int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
900 s->session->master_key_length, label, label_len,
901 seed, seed_len, NULL, 0);
902 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 return ret;
904}
905
906int tls1_alert_code(int code) {
907 switch (code) {
908 case SSL_AD_CLOSE_NOTIFY:
909 return SSL3_AD_CLOSE_NOTIFY;
910
911 case SSL_AD_UNEXPECTED_MESSAGE:
912 return SSL3_AD_UNEXPECTED_MESSAGE;
913
914 case SSL_AD_BAD_RECORD_MAC:
915 return SSL3_AD_BAD_RECORD_MAC;
916
917 case SSL_AD_DECRYPTION_FAILED:
918 return TLS1_AD_DECRYPTION_FAILED;
919
920 case SSL_AD_RECORD_OVERFLOW:
921 return TLS1_AD_RECORD_OVERFLOW;
922
923 case SSL_AD_DECOMPRESSION_FAILURE:
924 return SSL3_AD_DECOMPRESSION_FAILURE;
925
926 case SSL_AD_HANDSHAKE_FAILURE:
927 return SSL3_AD_HANDSHAKE_FAILURE;
928
929 case SSL_AD_NO_CERTIFICATE:
930 return -1;
931
932 case SSL_AD_BAD_CERTIFICATE:
933 return SSL3_AD_BAD_CERTIFICATE;
934
935 case SSL_AD_UNSUPPORTED_CERTIFICATE:
936 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
937
938 case SSL_AD_CERTIFICATE_REVOKED:
939 return SSL3_AD_CERTIFICATE_REVOKED;
940
941 case SSL_AD_CERTIFICATE_EXPIRED:
942 return SSL3_AD_CERTIFICATE_EXPIRED;
943
944 case SSL_AD_CERTIFICATE_UNKNOWN:
945 return SSL3_AD_CERTIFICATE_UNKNOWN;
946
947 case SSL_AD_ILLEGAL_PARAMETER:
948 return SSL3_AD_ILLEGAL_PARAMETER;
949
950 case SSL_AD_UNKNOWN_CA:
951 return TLS1_AD_UNKNOWN_CA;
952
953 case SSL_AD_ACCESS_DENIED:
954 return TLS1_AD_ACCESS_DENIED;
955
956 case SSL_AD_DECODE_ERROR:
957 return TLS1_AD_DECODE_ERROR;
958
959 case SSL_AD_DECRYPT_ERROR:
960 return TLS1_AD_DECRYPT_ERROR;
961 case SSL_AD_EXPORT_RESTRICTION:
962 return TLS1_AD_EXPORT_RESTRICTION;
963
964 case SSL_AD_PROTOCOL_VERSION:
965 return TLS1_AD_PROTOCOL_VERSION;
966
967 case SSL_AD_INSUFFICIENT_SECURITY:
968 return TLS1_AD_INSUFFICIENT_SECURITY;
969
970 case SSL_AD_INTERNAL_ERROR:
971 return TLS1_AD_INTERNAL_ERROR;
972
973 case SSL_AD_USER_CANCELLED:
974 return TLS1_AD_USER_CANCELLED;
975
976 case SSL_AD_NO_RENEGOTIATION:
977 return TLS1_AD_NO_RENEGOTIATION;
978
979 case SSL_AD_UNSUPPORTED_EXTENSION:
980 return TLS1_AD_UNSUPPORTED_EXTENSION;
981
982 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
983 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
984
985 case SSL_AD_UNRECOGNIZED_NAME:
986 return TLS1_AD_UNRECOGNIZED_NAME;
987
988 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
989 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
990
991 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
992 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
993
994 case SSL_AD_UNKNOWN_PSK_IDENTITY:
995 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
996
997 case SSL_AD_INAPPROPRIATE_FALLBACK:
998 return SSL3_AD_INAPPROPRIATE_FALLBACK;
999
1000 default:
1001 return -1;
1002 }
1003}