blob: 903020e6200ff0a8ada3dbb1f60dd434ddb6e301 [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
136#include <stdio.h>
137#include <assert.h>
138
Adam Langley95c29f32014-06-20 12:00:00 -0700139#include <openssl/err.h>
140#include <openssl/evp.h>
141#include <openssl/hmac.h>
142#include <openssl/md5.h>
143#include <openssl/mem.h>
144#include <openssl/obj.h>
145#include <openssl/rand.h>
146
David Benjamin2ee94aa2015-04-07 22:38:30 -0400147#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700148
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjamin1f5e1152014-12-23 09:23:32 -0500150/* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
151 * section 5. It writes |out_len| bytes to |out|, using |md| as the hash and
152 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
153 * seed parameter. It returns one on success and zero on failure. */
154static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
155 const uint8_t *secret, size_t secret_len,
156 const uint8_t *seed1, size_t seed1_len,
157 const uint8_t *seed2, size_t seed2_len,
158 const uint8_t *seed3, size_t seed3_len) {
159 size_t chunk;
160 HMAC_CTX ctx, ctx_tmp, ctx_init;
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 uint8_t A1[EVP_MAX_MD_SIZE];
David Benjamin1f5e1152014-12-23 09:23:32 -0500162 unsigned A1_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 chunk = EVP_MD_size(md);
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin1f5e1152014-12-23 09:23:32 -0500167 HMAC_CTX_init(&ctx);
168 HMAC_CTX_init(&ctx_tmp);
169 HMAC_CTX_init(&ctx_init);
170 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
171 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
172 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
173 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
174 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) ||
175 !HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 goto err;
177 }
178
179 for (;;) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500180 /* Reinit mac contexts. */
181 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
182 !HMAC_Update(&ctx, A1, A1_len) ||
183 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
184 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
185 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
186 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 goto err;
188 }
189
David Benjamin1f5e1152014-12-23 09:23:32 -0500190 if (out_len > chunk) {
191 unsigned len;
192 if (!HMAC_Final(&ctx, out, &len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 goto err;
194 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500195 assert(len == chunk);
196 out += len;
197 out_len -= len;
198 /* Calculate the next A1 value. */
199 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 goto err;
201 }
202 } else {
David Benjamin1f5e1152014-12-23 09:23:32 -0500203 /* Last chunk. */
204 if (!HMAC_Final(&ctx, A1, &A1_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 goto err;
206 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500207 memcpy(out, A1, out_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 break;
209 }
210 }
211
212 ret = 1;
213
Adam Langley95c29f32014-06-20 12:00:00 -0700214err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500215 HMAC_CTX_cleanup(&ctx);
216 HMAC_CTX_cleanup(&ctx_tmp);
217 HMAC_CTX_cleanup(&ctx_init);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 OPENSSL_cleanse(A1, sizeof(A1));
219 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700220}
Adam Langley9447dff2014-06-24 17:29:06 -0700221
David Benjamin41ac9792014-12-23 10:41:06 -0500222int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
223 size_t secret_len, const char *label, size_t label_len,
224 const uint8_t *seed1, size_t seed1_len,
225 const uint8_t *seed2, size_t seed2_len) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500226 size_t idx, len, count, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 const uint8_t *S1;
228 long m;
229 const EVP_MD *md;
230 int ret = 0;
David Benjaminaf032d62014-12-22 10:42:51 -0500231 uint8_t *tmp;
232
David Benjamin1f5e1152014-12-23 09:23:32 -0500233 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500234 return 1;
235 }
236
237 /* Allocate a temporary buffer. */
David Benjamin1f5e1152014-12-23 09:23:32 -0500238 tmp = OPENSSL_malloc(out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500239 if (tmp == NULL) {
David Benjamin41ac9792014-12-23 10:41:06 -0500240 OPENSSL_PUT_ERROR(SSL, tls1_prf, ERR_R_MALLOC_FAILURE);
David Benjaminaf032d62014-12-22 10:42:51 -0500241 return 0;
242 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800243
David Benjamin1f5e1152014-12-23 09:23:32 -0500244 /* Count number of digests and partition |secret| evenly. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 count = 0;
246 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
David Benjamin41ac9792014-12-23 10:41:06 -0500247 if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 count++;
249 }
250 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500251 /* TODO(davidben): The only case where count isn't 1 is the old MD5/SHA-1
252 * combination. The logic around multiple handshake digests can probably be
253 * simplified. */
254 assert(count == 1 || count == 2);
255 len = secret_len / count;
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 if (count == 1) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500257 secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500259 S1 = secret;
260 memset(out, 0, out_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
David Benjamin41ac9792014-12-23 10:41:06 -0500262 if ((m << TLS1_PRF_DGST_SHIFT) & ssl_get_algorithm2(s)) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500263 /* If |count| is 2 and |secret_len| is odd, |secret| is partitioned into
264 * two halves with an overlapping byte. */
265 if (!tls1_P_hash(tmp, out_len, md, S1, len + (secret_len & 1),
266 (const uint8_t *)label, label_len, seed1, seed1_len,
267 seed2, seed2_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 goto err;
269 }
270 S1 += len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500271 for (i = 0; i < out_len; i++) {
David Benjaminaf032d62014-12-22 10:42:51 -0500272 out[i] ^= tmp[i];
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
274 }
275 }
276 ret = 1;
277
278err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500279 OPENSSL_cleanse(tmp, out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500280 OPENSSL_free(tmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 return ret;
282}
283
David Benjamin1f5e1152014-12-23 09:23:32 -0500284static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
David Benjamin41ac9792014-12-23 10:41:06 -0500285 return s->enc_method->prf(s, out, out_len, s->session->master_key,
286 s->session->master_key_length,
287 TLS_MD_KEY_EXPANSION_CONST,
288 TLS_MD_KEY_EXPANSION_CONST_SIZE,
289 s->s3->server_random, SSL3_RANDOM_SIZE,
290 s->s3->client_random,
291 SSL3_RANDOM_SIZE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langleyc9fb3752014-06-20 12:00:00 -0700294/* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It
295 * returns 0 on malloc error. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800296static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx) {
297 if (*aead_ctx != NULL) {
298 EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
299 } else {
300 *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
301 if (*aead_ctx == NULL) {
302 OPENSSL_PUT_ERROR(SSL, tls1_aead_ctx_init, ERR_R_MALLOC_FAILURE);
303 return 0;
304 }
305 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 return 1;
308}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700309
310static int tls1_change_cipher_state_aead(SSL *s, char is_read,
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 const uint8_t *key, unsigned key_len,
312 const uint8_t *iv, unsigned iv_len,
313 const uint8_t *mac_secret,
314 unsigned mac_secret_len) {
315 const EVP_AEAD *aead = s->s3->tmp.new_aead;
316 SSL_AEAD_CTX *aead_ctx;
David Benjaminea72bd02014-12-21 21:27:41 -0500317 /* merged_key is used to merge the MAC, cipher, and IV keys for an AEAD which
318 * simulates pre-AEAD cipher suites. */
319 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
Adam Langley9447dff2014-06-24 17:29:06 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 if (mac_secret_len > 0) {
322 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
323 * suites). */
David Benjaminea72bd02014-12-21 21:27:41 -0500324 if (mac_secret_len + key_len + iv_len > sizeof(merged_key)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead,
326 ERR_R_INTERNAL_ERROR);
327 return 0;
328 }
David Benjaminea72bd02014-12-21 21:27:41 -0500329 memcpy(merged_key, mac_secret, mac_secret_len);
330 memcpy(merged_key + mac_secret_len, key, key_len);
331 memcpy(merged_key + mac_secret_len + key_len, iv, iv_len);
332 key = merged_key;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 key_len += mac_secret_len;
David Benjaminea72bd02014-12-21 21:27:41 -0500334 key_len += iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700336
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 if (is_read) {
338 if (!tls1_aead_ctx_init(&s->aead_read_ctx)) {
339 return 0;
340 }
341 aead_ctx = s->aead_read_ctx;
342 } else {
David Benjaminafbc63f2015-02-01 02:33:59 -0500343 if (SSL_IS_DTLS(s) && s->aead_write_ctx != NULL) {
344 /* DTLS renegotiation is unsupported, so a CCS can only switch away from
345 * the NULL cipher. This simplifies renegotiation. */
346 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead,
347 ERR_R_INTERNAL_ERROR);
348 return 0;
David Benjamine95d20d2014-12-23 11:16:01 -0500349 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 if (!tls1_aead_ctx_init(&s->aead_write_ctx)) {
351 return 0;
352 }
353 aead_ctx = s->aead_write_ctx;
354 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700355
David Benjaminb34f5102015-02-28 03:59:33 -0500356 if (!EVP_AEAD_CTX_init_with_direction(
357 &aead_ctx->ctx, aead, key, key_len, EVP_AEAD_DEFAULT_TAG_LENGTH,
358 is_read ? evp_aead_open : evp_aead_seal)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 OPENSSL_free(aead_ctx);
360 if (is_read) {
361 s->aead_read_ctx = NULL;
362 } else {
363 s->aead_write_ctx = NULL;
364 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 return 0;
367 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700368
David Benjaminea72bd02014-12-21 21:27:41 -0500369 if (mac_secret_len == 0) {
370 /* For a real AEAD, the IV is the fixed part of the nonce. */
371 if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
372 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
373 return 0;
374 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800375
David Benjaminea72bd02014-12-21 21:27:41 -0500376 memcpy(aead_ctx->fixed_nonce, iv, iv_len);
377 aead_ctx->fixed_nonce_len = iv_len;
378 aead_ctx->variable_nonce_included_in_record =
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 (s->s3->tmp.new_cipher->algorithm2 &
380 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500381 aead_ctx->random_variable_nonce = 0;
382 aead_ctx->omit_length_in_ad = 0;
383 } else {
384 aead_ctx->fixed_nonce_len = 0;
385 aead_ctx->variable_nonce_included_in_record = 1;
386 aead_ctx->random_variable_nonce = 1;
387 aead_ctx->omit_length_in_ad = 1;
388 }
389 aead_ctx->variable_nonce_len = s->s3->tmp.new_variable_iv_len;
David Benjamin044abb02014-12-23 10:57:17 -0500390 aead_ctx->omit_version_in_ad = (s->version == SSL3_VERSION);
David Benjaminea72bd02014-12-21 21:27:41 -0500391
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
393 EVP_AEAD_nonce_length(aead)) {
394 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
395 return 0;
396 }
397 aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
398
399 return 1;
400}
401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402int tls1_change_cipher_state(SSL *s, int which) {
403 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
404 * need to update the read cipherspec. Otherwise we have just written one. */
405 const char is_read = (which & SSL3_CC_READ) != 0;
406 /* use_client_keys is true if we wish to use the keys for the "client write"
407 * direction. This is the case if we're a client sending a ChangeCipherSpec,
408 * or a server reading a client's ChangeCipherSpec. */
409 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
410 which == SSL3_CHANGE_CIPHER_SERVER_READ;
411 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
412 const uint8_t *client_write_key, *server_write_key, *key;
413 const uint8_t *client_write_iv, *server_write_iv, *iv;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 const EVP_AEAD *aead = s->s3->tmp.new_aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500415 size_t key_len, iv_len, mac_secret_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 const uint8_t *key_data;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 /* Reset sequence number to zero. */
419 if (!SSL_IS_DTLS(s)) {
420 memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8);
421 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700422
David Benjaminea72bd02014-12-21 21:27:41 -0500423 mac_secret_len = s->s3->tmp.new_mac_secret_len;
424 iv_len = s->s3->tmp.new_fixed_iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700425
David Benjaminb8a56f12014-12-23 11:41:02 -0500426 if (aead == NULL) {
427 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
428 return 0;
429 }
430
431 key_len = EVP_AEAD_key_length(aead);
432 if (mac_secret_len > 0) {
433 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
434 * suites) the key length reported by |EVP_AEAD_key_length| will
435 * include the MAC and IV key bytes. */
436 if (key_len < mac_secret_len + iv_len) {
437 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
438 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500440 key_len -= mac_secret_len + iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 key_data = s->s3->tmp.key_block;
444 client_write_mac_secret = key_data;
445 key_data += mac_secret_len;
446 server_write_mac_secret = key_data;
447 key_data += mac_secret_len;
448 client_write_key = key_data;
449 key_data += key_len;
450 server_write_key = key_data;
451 key_data += key_len;
452 client_write_iv = key_data;
453 key_data += iv_len;
454 server_write_iv = key_data;
455 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700456
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 if (use_client_keys) {
458 mac_secret = client_write_mac_secret;
459 key = client_write_key;
460 iv = client_write_iv;
461 } else {
462 mac_secret = server_write_mac_secret;
463 key = server_write_key;
464 iv = server_write_iv;
465 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
468 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
469 return 0;
470 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700471
David Benjaminb8a56f12014-12-23 11:41:02 -0500472 return tls1_change_cipher_state_aead(s, is_read, key, key_len, iv, iv_len,
473 mac_secret, mac_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800474}
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476int tls1_setup_key_block(SSL *s) {
David Benjaminaf032d62014-12-22 10:42:51 -0500477 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 const EVP_AEAD *aead = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 int ret = 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500480 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500481 size_t key_block_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 if (s->s3->tmp.key_block_length != 0) {
484 return 1;
485 }
Adam Langley95c29f32014-06-20 12:00:00 -0700486
David Benjaminea72bd02014-12-21 21:27:41 -0500487 if (s->session->cipher == NULL) {
488 goto cipher_unavailable_err;
489 }
490
David Benjaminb8a56f12014-12-23 11:41:02 -0500491 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
492 s->session->cipher,
493 ssl3_version_from_wire(s, s->version))) {
494 goto cipher_unavailable_err;
495 }
496 key_len = EVP_AEAD_key_length(aead);
497 variable_iv_len = EVP_AEAD_nonce_length(aead);
498 if (mac_secret_len > 0) {
499 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
500 * key length reported by |EVP_AEAD_key_length| will include the MAC key
501 * bytes and initial implicit IV. */
502 if (key_len < mac_secret_len + fixed_iv_len) {
503 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
504 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500506 key_len -= mac_secret_len + fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 } else {
David Benjaminb8a56f12014-12-23 11:41:02 -0500508 /* The nonce is split into a fixed portion and a variable portion. */
509 if (variable_iv_len < fixed_iv_len) {
510 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
511 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500513 variable_iv_len -= fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjaminea72bd02014-12-21 21:27:41 -0500516 assert(mac_secret_len < 256);
517 assert(fixed_iv_len < 256);
518 assert(variable_iv_len < 256);
519
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 s->s3->tmp.new_aead = aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500521 s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
522 s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
523 s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700524
David Benjamin1f5e1152014-12-23 09:23:32 -0500525 key_block_len = key_len + mac_secret_len + fixed_iv_len;
526 key_block_len *= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700529
David Benjamin1f5e1152014-12-23 09:23:32 -0500530 p = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500531 if (p == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE);
533 goto err;
534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamin1f5e1152014-12-23 09:23:32 -0500536 s->s3->tmp.key_block_length = key_block_len;
David Benjaminaf032d62014-12-22 10:42:51 -0500537 s->s3->tmp.key_block = p;
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin1f5e1152014-12-23 09:23:32 -0500539 if (!tls1_generate_key_block(s, p, key_block_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 goto err;
541 }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 if (!SSL_USE_EXPLICIT_IV(s) &&
544 (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) {
545 /* enable vulnerability countermeasure for CBC ciphers with known-IV
546 * problem (http://www.openssl.org/~bodo/tls-cbc.txt). */
547 s->s3->need_record_splitting = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 if (s->session->cipher != NULL &&
550 s->session->cipher->algorithm_enc == SSL_RC4) {
551 s->s3->need_record_splitting = 0;
552 }
553 }
554
555 ret = 1;
556
Adam Langley95c29f32014-06-20 12:00:00 -0700557err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 return ret;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700559
560cipher_unavailable_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block,
562 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
563 return 0;
564}
Adam Langley95c29f32014-06-20 12:00:00 -0700565
Adam Langleyfcf25832014-12-18 17:42:32 -0800566/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|,
David Benjamin1e52eca2015-01-22 15:33:51 -0500567 * respectively. It returns one on success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800568int tls1_enc(SSL *s, int send) {
569 SSL3_RECORD *rec;
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 const SSL_AEAD_CTX *aead;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 if (send) {
573 rec = &s->s3->wrec;
574 aead = s->aead_write_ctx;
575 } else {
576 rec = &s->s3->rrec;
577 aead = s->aead_read_ctx;
578 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700579
David Benjaminafbc63f2015-02-01 02:33:59 -0500580 if (aead == NULL) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500581 /* Handle the initial NULL cipher. */
David Benjaminb8a56f12014-12-23 11:41:02 -0500582 memmove(rec->data, rec->input, rec->length);
583 rec->input = rec->data;
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 return 1;
585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
David Benjaminb8a56f12014-12-23 11:41:02 -0500587 uint8_t ad[13], *seq, *in, *out, nonce[EVP_AEAD_MAX_NONCE_LENGTH];
588 unsigned nonce_used;
589 size_t n, ad_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700590
David Benjaminb8a56f12014-12-23 11:41:02 -0500591 seq = send ? s->s3->write_sequence : s->s3->read_sequence;
592
593 if (SSL_IS_DTLS(s)) {
594 uint8_t dtlsseq[9], *p = dtlsseq;
595
596 s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
597 memcpy(p, &seq[2], 6);
598 memcpy(ad, dtlsseq, 8);
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 } else {
David Benjaminb8a56f12014-12-23 11:41:02 -0500600 memcpy(ad, seq, 8);
David Benjamind81e73d2015-04-05 00:21:39 -0400601 if (!ssl3_record_sequence_update(seq, 8)) {
602 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 }
604 }
Adam Langley95c29f32014-06-20 12:00:00 -0700605
David Benjaminb8a56f12014-12-23 11:41:02 -0500606 ad[8] = rec->type;
607 ad_len = 9;
608 if (!aead->omit_version_in_ad) {
609 ad[ad_len++] = (uint8_t)(s->version >> 8);
610 ad[ad_len++] = (uint8_t)(s->version);
611 }
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjaminb8a56f12014-12-23 11:41:02 -0500613 if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce)) {
614 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500615 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
David Benjaminb8a56f12014-12-23 11:41:02 -0500618 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
619 nonce_used = aead->fixed_nonce_len;
620
621 if (send) {
622 size_t len = rec->length;
623 size_t eivlen = 0;
624 in = rec->input;
625 out = rec->data;
626
627 uint8_t *variable_nonce = nonce + nonce_used;
628 if (aead->random_variable_nonce) {
629 assert(aead->variable_nonce_included_in_record);
630 if (!RAND_bytes(nonce + nonce_used, aead->variable_nonce_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500631 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500633 } else {
634 /* When sending we use the sequence number as the variable part of the
635 * nonce. */
636 if (aead->variable_nonce_len != 8) {
637 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500638 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500639 }
640 memcpy(nonce + nonce_used, ad, aead->variable_nonce_len);
641 }
642 nonce_used += aead->variable_nonce_len;
643
644 /* in do_ssl3_write, rec->input is moved forward by variable_nonce_len in
645 * order to leave space for the variable nonce. Thus we can copy the
646 * sequence number bytes into place without overwriting any of the
647 * plaintext. */
648 if (aead->variable_nonce_included_in_record) {
649 memcpy(out, variable_nonce, aead->variable_nonce_len);
650 len -= aead->variable_nonce_len;
651 eivlen = aead->variable_nonce_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 }
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjaminb8a56f12014-12-23 11:41:02 -0500654 if (!aead->omit_length_in_ad) {
655 ad[ad_len++] = len >> 8;
656 ad[ad_len++] = len & 0xff;
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 }
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjaminb8a56f12014-12-23 11:41:02 -0500659 if (!EVP_AEAD_CTX_seal(&aead->ctx, out + eivlen, &n, len + aead->tag_len,
660 nonce, nonce_used, in + eivlen, len, ad, ad_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500661 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 }
Adam Langley95c29f32014-06-20 12:00:00 -0700663
David Benjaminb8a56f12014-12-23 11:41:02 -0500664 if (aead->variable_nonce_included_in_record) {
665 n += aead->variable_nonce_len;
666 }
667 } else {
668 /* receive */
669 size_t len = rec->length;
670
671 if (rec->data != rec->input) {
672 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
David Benjamin1e52eca2015-01-22 15:33:51 -0500673 return 0;
David Benjaminb8a56f12014-12-23 11:41:02 -0500674 }
675 out = in = rec->input;
676
677 if (len < aead->variable_nonce_len) {
678 return 0;
679 }
680 memcpy(nonce + nonce_used,
681 aead->variable_nonce_included_in_record ? in : ad,
682 aead->variable_nonce_len);
683 nonce_used += aead->variable_nonce_len;
684
685 if (aead->variable_nonce_included_in_record) {
686 in += aead->variable_nonce_len;
687 len -= aead->variable_nonce_len;
688 out += aead->variable_nonce_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 }
Adam Langley95c29f32014-06-20 12:00:00 -0700690
David Benjaminb8a56f12014-12-23 11:41:02 -0500691 if (!aead->omit_length_in_ad) {
692 if (len < aead->tag_len) {
693 return 0;
694 }
695 size_t plaintext_len = len - aead->tag_len;
696
697 ad[ad_len++] = plaintext_len >> 8;
698 ad[ad_len++] = plaintext_len & 0xff;
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500700
701 if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, rec->length, nonce, nonce_used, in,
702 len, ad, ad_len)) {
David Benjamin1e52eca2015-01-22 15:33:51 -0500703 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500705
706 rec->data = rec->input = out;
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 }
David Benjaminb8a56f12014-12-23 11:41:02 -0500708
709 rec->length = n;
710 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800711}
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
714 unsigned int ret;
715 EVP_MD_CTX ctx, *d = NULL;
716 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 if (s->s3->handshake_buffer &&
719 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
720 return 0;
721 }
722
723 for (i = 0; i < SSL_MAX_DIGEST; i++) {
724 if (s->s3->handshake_dgst[i] &&
725 EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
726 d = s->s3->handshake_dgst[i];
727 break;
728 }
729 }
730
731 if (!d) {
732 OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST);
733 return 0;
734 }
735
736 EVP_MD_CTX_init(&ctx);
David Benjamin9d0847a2015-02-16 03:57:55 -0500737 if (!EVP_MD_CTX_copy_ex(&ctx, d)) {
738 EVP_MD_CTX_cleanup(&ctx);
739 return 0;
740 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800741 EVP_DigestFinal_ex(&ctx, out, &ret);
742 EVP_MD_CTX_cleanup(&ctx);
743
744 return ret;
745}
Adam Langley95c29f32014-06-20 12:00:00 -0700746
Adam Langley1258b6a2014-06-20 12:00:00 -0700747/* tls1_handshake_digest calculates the current handshake hash and writes it to
748 * |out|, which has space for |out_len| bytes. It returns the number of bytes
749 * written or -1 in the event of an error. This function works on a copy of the
750 * underlying digests so can be called multiple times and prior to the final
751 * update etc. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800752int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
753 const EVP_MD *md;
754 EVP_MD_CTX ctx;
David Benjamin1f5e1152014-12-23 09:23:32 -0500755 int err = 0, len = 0;
756 size_t i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800757 long mask;
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 EVP_MD_CTX_init(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500762 size_t hash_size;
Adam Langleyfcf25832014-12-18 17:42:32 -0800763 unsigned int digest_len;
764 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i];
Adam Langley1258b6a2014-06-20 12:00:00 -0700765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 if ((mask & ssl_get_algorithm2(s)) == 0) {
767 continue;
768 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 hash_size = EVP_MD_size(md);
771 if (!hdgst ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500772 hash_size > out_len ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 !EVP_MD_CTX_copy_ex(&ctx, hdgst) ||
774 !EVP_DigestFinal_ex(&ctx, out, &digest_len) ||
David Benjamin1f5e1152014-12-23 09:23:32 -0500775 digest_len != hash_size /* internal error */) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 err = 1;
777 break;
778 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700779
Adam Langleyfcf25832014-12-18 17:42:32 -0800780 out += digest_len;
781 out_len -= digest_len;
782 len += digest_len;
783 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700784
Adam Langleyfcf25832014-12-18 17:42:32 -0800785 EVP_MD_CTX_cleanup(&ctx);
Adam Langley1258b6a2014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 if (err != 0) {
788 return -1;
789 }
790 return len;
791}
Adam Langley1258b6a2014-06-20 12:00:00 -0700792
Adam Langleyfcf25832014-12-18 17:42:32 -0800793int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
794 uint8_t buf[2 * EVP_MAX_MD_SIZE];
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 int err = 0;
796 int digests_len;
Adam Langley1258b6a2014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 if (s->s3->handshake_buffer &&
799 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
800 return 0;
801 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
804 if (digests_len < 0) {
805 err = 1;
806 digests_len = 0;
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
David Benjamin41ac9792014-12-23 10:41:06 -0500809 if (!s->enc_method->prf(s, out, 12, s->session->master_key,
810 s->session->master_key_length, str, slen, buf,
811 digests_len, NULL, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800812 err = 1;
813 }
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 if (err) {
816 return 0;
817 } else {
David Benjaminaf032d62014-12-22 10:42:51 -0500818 return 12;
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 }
820}
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamin31b1d812014-12-23 10:01:09 -0500822int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
823 size_t premaster_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 if (s->s3->tmp.extended_master_secret) {
825 uint8_t digests[2 * EVP_MAX_MD_SIZE];
826 int digests_len;
Adam Langley75712922014-10-10 16:23:43 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 /* The master secret is based on the handshake hash just after sending the
829 * ClientKeyExchange. However, we might have a client certificate to send,
830 * in which case we might need different hashes for the verification and
831 * thus still need the handshake buffer around. Keeping both a handshake
832 * buffer *and* running hashes isn't yet supported so, when it comes to
833 * calculating the Finished hash, we'll have to hash the handshake buffer
834 * again. */
835 if (s->s3->handshake_buffer &&
836 !ssl3_digest_cached_records(s, dont_free_handshake_buffer)) {
837 return 0;
838 }
Adam Langley75712922014-10-10 16:23:43 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (digests_len == -1) {
842 return 0;
843 }
Adam Langley75712922014-10-10 16:23:43 -0700844
David Benjamin41ac9792014-12-23 10:41:06 -0500845 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
846 premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
847 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
848 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500849 return 0;
850 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 } else {
David Benjamin41ac9792014-12-23 10:41:06 -0500852 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
853 premaster_len, TLS_MD_MASTER_SECRET_CONST,
854 TLS_MD_MASTER_SECRET_CONST_SIZE,
855 s->s3->client_random, SSL3_RANDOM_SIZE,
856 s->s3->server_random, SSL3_RANDOM_SIZE)) {
David Benjaminaf032d62014-12-22 10:42:51 -0500857 return 0;
858 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 return SSL3_MASTER_SECRET_SIZE;
862}
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjamincfd248b2015-04-03 11:02:24 -0400864int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
865 const char *label, size_t label_len,
866 const uint8_t *context, size_t context_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 int use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400868 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (use_context) {
David Benjamincfd248b2015-04-03 11:02:24 -0400870 if (context_len >= 1u << 16) {
871 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_OVERFLOW);
872 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 }
David Benjamincfd248b2015-04-03 11:02:24 -0400874 seed_len += 2 + context_len;
875 }
876 uint8_t *seed = OPENSSL_malloc(seed_len);
877 if (seed == NULL) {
878 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE);
879 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
David Benjamincfd248b2015-04-03 11:02:24 -0400882 memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
883 memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
884 if (use_context) {
885 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
886 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
887 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamincfd248b2015-04-03 11:02:24 -0400890 int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
891 s->session->master_key_length, label, label_len,
892 seed, seed_len, NULL, 0);
893 OPENSSL_free(seed);
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 return ret;
895}
896
897int tls1_alert_code(int code) {
898 switch (code) {
899 case SSL_AD_CLOSE_NOTIFY:
900 return SSL3_AD_CLOSE_NOTIFY;
901
902 case SSL_AD_UNEXPECTED_MESSAGE:
903 return SSL3_AD_UNEXPECTED_MESSAGE;
904
905 case SSL_AD_BAD_RECORD_MAC:
906 return SSL3_AD_BAD_RECORD_MAC;
907
908 case SSL_AD_DECRYPTION_FAILED:
909 return TLS1_AD_DECRYPTION_FAILED;
910
911 case SSL_AD_RECORD_OVERFLOW:
912 return TLS1_AD_RECORD_OVERFLOW;
913
914 case SSL_AD_DECOMPRESSION_FAILURE:
915 return SSL3_AD_DECOMPRESSION_FAILURE;
916
917 case SSL_AD_HANDSHAKE_FAILURE:
918 return SSL3_AD_HANDSHAKE_FAILURE;
919
920 case SSL_AD_NO_CERTIFICATE:
921 return -1;
922
923 case SSL_AD_BAD_CERTIFICATE:
924 return SSL3_AD_BAD_CERTIFICATE;
925
926 case SSL_AD_UNSUPPORTED_CERTIFICATE:
927 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
928
929 case SSL_AD_CERTIFICATE_REVOKED:
930 return SSL3_AD_CERTIFICATE_REVOKED;
931
932 case SSL_AD_CERTIFICATE_EXPIRED:
933 return SSL3_AD_CERTIFICATE_EXPIRED;
934
935 case SSL_AD_CERTIFICATE_UNKNOWN:
936 return SSL3_AD_CERTIFICATE_UNKNOWN;
937
938 case SSL_AD_ILLEGAL_PARAMETER:
939 return SSL3_AD_ILLEGAL_PARAMETER;
940
941 case SSL_AD_UNKNOWN_CA:
942 return TLS1_AD_UNKNOWN_CA;
943
944 case SSL_AD_ACCESS_DENIED:
945 return TLS1_AD_ACCESS_DENIED;
946
947 case SSL_AD_DECODE_ERROR:
948 return TLS1_AD_DECODE_ERROR;
949
950 case SSL_AD_DECRYPT_ERROR:
951 return TLS1_AD_DECRYPT_ERROR;
952 case SSL_AD_EXPORT_RESTRICTION:
953 return TLS1_AD_EXPORT_RESTRICTION;
954
955 case SSL_AD_PROTOCOL_VERSION:
956 return TLS1_AD_PROTOCOL_VERSION;
957
958 case SSL_AD_INSUFFICIENT_SECURITY:
959 return TLS1_AD_INSUFFICIENT_SECURITY;
960
961 case SSL_AD_INTERNAL_ERROR:
962 return TLS1_AD_INTERNAL_ERROR;
963
964 case SSL_AD_USER_CANCELLED:
965 return TLS1_AD_USER_CANCELLED;
966
967 case SSL_AD_NO_RENEGOTIATION:
968 return TLS1_AD_NO_RENEGOTIATION;
969
970 case SSL_AD_UNSUPPORTED_EXTENSION:
971 return TLS1_AD_UNSUPPORTED_EXTENSION;
972
973 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
974 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
975
976 case SSL_AD_UNRECOGNIZED_NAME:
977 return TLS1_AD_UNRECOGNIZED_NAME;
978
979 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
980 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
981
982 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
983 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
984
985 case SSL_AD_UNKNOWN_PSK_IDENTITY:
986 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
987
988 case SSL_AD_INAPPROPRIATE_FALLBACK:
989 return SSL3_AD_INAPPROPRIATE_FALLBACK;
990
991 default:
992 return -1;
993 }
994}