blob: b24781a88030834712b674d15176f4816a912d73 [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
147#include "ssl_locl.h"
148
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 Benjamin1f5e1152014-12-23 09:23:32 -0500222/* tls1_PRF computes the TLS PRF function as described in RFC 5246, section 5
223 * and RFC 2246 section 5. It writes |out_len| bytes to |out|, using
224 * |digest_mask| to select the hash functions, |secret| as the secret, and
225 * |label| as the label. |seed1| and |seed2| are concatenated to form the seed
226 * parameter. It returns one on success and zero on failure. */
227static int tls1_PRF(uint8_t *out, size_t out_len, long digest_mask,
228 const uint8_t *secret, size_t secret_len,
229 const char *label, size_t label_len,
230 const uint8_t *seed1, size_t seed1_len,
231 const uint8_t *seed2, size_t seed2_len) {
232 size_t idx, len, count, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 const uint8_t *S1;
234 long m;
235 const EVP_MD *md;
236 int ret = 0;
David Benjaminaf032d62014-12-22 10:42:51 -0500237 uint8_t *tmp;
238
David Benjamin1f5e1152014-12-23 09:23:32 -0500239 if (out_len == 0) {
David Benjaminaf032d62014-12-22 10:42:51 -0500240 return 1;
241 }
242
243 /* Allocate a temporary buffer. */
David Benjamin1f5e1152014-12-23 09:23:32 -0500244 tmp = OPENSSL_malloc(out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500245 if (tmp == NULL) {
246 OPENSSL_PUT_ERROR(SSL, tls1_PRF, ERR_R_MALLOC_FAILURE);
247 return 0;
248 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800249
David Benjamin1f5e1152014-12-23 09:23:32 -0500250 /* Count number of digests and partition |secret| evenly. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 count = 0;
252 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
253 if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) {
254 count++;
255 }
256 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500257 /* TODO(davidben): The only case where count isn't 1 is the old MD5/SHA-1
258 * combination. The logic around multiple handshake digests can probably be
259 * simplified. */
260 assert(count == 1 || count == 2);
261 len = secret_len / count;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 if (count == 1) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500263 secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 }
David Benjamin1f5e1152014-12-23 09:23:32 -0500265 S1 = secret;
266 memset(out, 0, out_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 for (idx = 0; ssl_get_handshake_digest(idx, &m, &md); idx++) {
268 if ((m << TLS1_PRF_DGST_SHIFT) & digest_mask) {
David Benjamin1f5e1152014-12-23 09:23:32 -0500269 /* If |count| is 2 and |secret_len| is odd, |secret| is partitioned into
270 * two halves with an overlapping byte. */
271 if (!tls1_P_hash(tmp, out_len, md, S1, len + (secret_len & 1),
272 (const uint8_t *)label, label_len, seed1, seed1_len,
273 seed2, seed2_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 goto err;
275 }
276 S1 += len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500277 for (i = 0; i < out_len; i++) {
David Benjaminaf032d62014-12-22 10:42:51 -0500278 out[i] ^= tmp[i];
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 }
280 }
281 }
282 ret = 1;
283
284err:
David Benjamin1f5e1152014-12-23 09:23:32 -0500285 OPENSSL_cleanse(tmp, out_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500286 OPENSSL_free(tmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 return ret;
288}
289
David Benjamin1f5e1152014-12-23 09:23:32 -0500290static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
291 return tls1_PRF(out, out_len, ssl_get_algorithm2(s),
292 s->session->master_key, s->session->master_key_length,
293 TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
294 s->s3->server_random, SSL3_RANDOM_SIZE, s->s3->client_random,
295 SSL3_RANDOM_SIZE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800296}
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyc9fb3752014-06-20 12:00:00 -0700298/* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It
299 * returns 0 on malloc error. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800300static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx) {
301 if (*aead_ctx != NULL) {
302 EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
303 } else {
304 *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
305 if (*aead_ctx == NULL) {
306 OPENSSL_PUT_ERROR(SSL, tls1_aead_ctx_init, ERR_R_MALLOC_FAILURE);
307 return 0;
308 }
309 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 return 1;
312}
Adam Langleyc9fb3752014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314static void tls1_cleanup_enc_ctx(EVP_CIPHER_CTX **ctx) {
315 if (*ctx != NULL) {
316 EVP_CIPHER_CTX_free(*ctx);
317 }
318 *ctx = NULL;
319}
Adam Langley88333ef2014-10-15 20:13:35 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321static void tls1_cleanup_hash_ctx(EVP_MD_CTX **ctx) {
322 if (*ctx != NULL) {
323 EVP_MD_CTX_destroy(*ctx);
324 }
325 *ctx = NULL;
326}
Adam Langley88333ef2014-10-15 20:13:35 -0700327
Adam Langleyc9fb3752014-06-20 12:00:00 -0700328static int tls1_change_cipher_state_aead(SSL *s, char is_read,
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 const uint8_t *key, unsigned key_len,
330 const uint8_t *iv, unsigned iv_len,
331 const uint8_t *mac_secret,
332 unsigned mac_secret_len) {
333 const EVP_AEAD *aead = s->s3->tmp.new_aead;
334 SSL_AEAD_CTX *aead_ctx;
David Benjaminea72bd02014-12-21 21:27:41 -0500335 /* merged_key is used to merge the MAC, cipher, and IV keys for an AEAD which
336 * simulates pre-AEAD cipher suites. */
337 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
Adam Langley9447dff2014-06-24 17:29:06 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 if (is_read) {
340 tls1_cleanup_enc_ctx(&s->enc_read_ctx);
341 tls1_cleanup_hash_ctx(&s->read_hash);
342 } else {
343 tls1_cleanup_enc_ctx(&s->enc_write_ctx);
344 tls1_cleanup_hash_ctx(&s->write_hash);
345 }
Adam Langley88333ef2014-10-15 20:13:35 -0700346
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 if (mac_secret_len > 0) {
348 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
349 * suites). */
David Benjaminea72bd02014-12-21 21:27:41 -0500350 if (mac_secret_len + key_len + iv_len > sizeof(merged_key)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead,
352 ERR_R_INTERNAL_ERROR);
353 return 0;
354 }
David Benjaminea72bd02014-12-21 21:27:41 -0500355 memcpy(merged_key, mac_secret, mac_secret_len);
356 memcpy(merged_key + mac_secret_len, key, key_len);
357 memcpy(merged_key + mac_secret_len + key_len, iv, iv_len);
358 key = merged_key;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 key_len += mac_secret_len;
David Benjaminea72bd02014-12-21 21:27:41 -0500360 key_len += iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (is_read) {
364 if (!tls1_aead_ctx_init(&s->aead_read_ctx)) {
365 return 0;
366 }
367 aead_ctx = s->aead_read_ctx;
368 } else {
369 if (!tls1_aead_ctx_init(&s->aead_write_ctx)) {
370 return 0;
371 }
372 aead_ctx = s->aead_write_ctx;
373 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
376 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL /* engine */)) {
377 OPENSSL_free(aead_ctx);
378 if (is_read) {
379 s->aead_read_ctx = NULL;
380 } else {
381 s->aead_write_ctx = NULL;
382 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 return 0;
385 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700386
David Benjaminea72bd02014-12-21 21:27:41 -0500387 if (mac_secret_len == 0) {
388 /* For a real AEAD, the IV is the fixed part of the nonce. */
389 if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
390 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
391 return 0;
392 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800393
David Benjaminea72bd02014-12-21 21:27:41 -0500394 memcpy(aead_ctx->fixed_nonce, iv, iv_len);
395 aead_ctx->fixed_nonce_len = iv_len;
396 aead_ctx->variable_nonce_included_in_record =
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 (s->s3->tmp.new_cipher->algorithm2 &
398 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500399 aead_ctx->random_variable_nonce = 0;
400 aead_ctx->omit_length_in_ad = 0;
401 } else {
402 aead_ctx->fixed_nonce_len = 0;
403 aead_ctx->variable_nonce_included_in_record = 1;
404 aead_ctx->random_variable_nonce = 1;
405 aead_ctx->omit_length_in_ad = 1;
406 }
407 aead_ctx->variable_nonce_len = s->s3->tmp.new_variable_iv_len;
408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
410 EVP_AEAD_nonce_length(aead)) {
411 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_aead, ERR_R_INTERNAL_ERROR);
412 return 0;
413 }
414 aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
415
416 return 1;
417}
418
419static void tls1_cleanup_aead_ctx(SSL_AEAD_CTX **ctx) {
420 if (*ctx != NULL) {
421 EVP_AEAD_CTX_cleanup(&(*ctx)->ctx);
422 OPENSSL_free(*ctx);
423 }
424 *ctx = NULL;
425}
Adam Langley88333ef2014-10-15 20:13:35 -0700426
Adam Langleya5fa5b72014-06-20 12:00:00 -0700427/* tls1_change_cipher_state_cipher performs the work needed to switch cipher
428 * states when using EVP_CIPHER. The argument |is_read| is true iff this
429 * function is being called due to reading, as opposed to writing, a
430 * ChangeCipherSpec message. In order to support export ciphersuites,
431 * use_client_keys indicates whether the key material provided is in the
432 * "client write" direction. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800433static int tls1_change_cipher_state_cipher(SSL *s, char is_read,
434 char use_client_keys,
435 const uint8_t *mac_secret,
436 unsigned mac_secret_len,
437 const uint8_t *key, unsigned key_len,
438 const uint8_t *iv, unsigned iv_len) {
439 const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc;
440 EVP_CIPHER_CTX *cipher_ctx;
441 EVP_MD_CTX *mac_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 if (is_read) {
444 tls1_cleanup_aead_ctx(&s->aead_read_ctx);
445 } else {
446 tls1_cleanup_aead_ctx(&s->aead_write_ctx);
447 }
Adam Langley88333ef2014-10-15 20:13:35 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 if (is_read) {
450 if (s->enc_read_ctx != NULL && !SSL_IS_DTLS(s)) {
451 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
452 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
453 goto err;
454 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 cipher_ctx = s->enc_read_ctx;
457 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
458 if (mac_ctx == NULL) {
459 goto err;
460 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 memcpy(s->s3->read_mac_secret, mac_secret, mac_secret_len);
463 s->s3->read_mac_secret_size = mac_secret_len;
464 } else {
465 /* When updating the write contexts for DTLS, we do not wish to free the
466 * old ones because DTLS stores pointers to them in order to implement
467 * retransmission. */
Adam Langleya5fa5b72014-06-20 12:00:00 -0700468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
470 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
471 } else {
472 s->enc_write_ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
473 if (s->enc_write_ctx == NULL) {
474 goto err;
475 }
476 }
477 EVP_CIPHER_CTX_init(s->enc_write_ctx);
Adam Langleya5fa5b72014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 cipher_ctx = s->enc_write_ctx;
480 if (SSL_IS_DTLS(s)) {
481 /* This is the same as ssl_replace_hash, but doesn't
482 * free the old |s->write_hash|. */
483 mac_ctx = EVP_MD_CTX_create();
484 if (!mac_ctx) {
485 goto err;
486 }
487 s->write_hash = mac_ctx;
488 } else {
489 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
490 if (mac_ctx == NULL) {
491 goto err;
492 }
493 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 memcpy(s->s3->write_mac_secret, mac_secret, mac_secret_len);
496 s->s3->write_mac_secret_size = mac_secret_len;
497 }
Adam Langley95c29f32014-06-20 12:00:00 -0700498
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 EVP_PKEY *mac_key = EVP_PKEY_new_mac_key(s->s3->tmp.new_mac_pkey_type, NULL,
500 mac_secret, mac_secret_len);
501 if (!mac_key) {
502 return 0;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 if (!EVP_DigestSignInit(mac_ctx, NULL, s->s3->tmp.new_hash, NULL, mac_key)) {
506 EVP_PKEY_free(mac_key);
507 goto err;
508 }
509 EVP_PKEY_free(mac_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 if (!EVP_CipherInit_ex(cipher_ctx, cipher, NULL /* engine */, key, iv,
512 !is_read)) {
513 goto err;
514 }
515
516 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langley95c29f32014-06-20 12:00:00 -0700518err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state_cipher, ERR_R_MALLOC_FAILURE);
520 return 0;
521}
Adam Langleya5fa5b72014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523int tls1_change_cipher_state(SSL *s, int which) {
524 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
525 * need to update the read cipherspec. Otherwise we have just written one. */
526 const char is_read = (which & SSL3_CC_READ) != 0;
527 /* use_client_keys is true if we wish to use the keys for the "client write"
528 * direction. This is the case if we're a client sending a ChangeCipherSpec,
529 * or a server reading a client's ChangeCipherSpec. */
530 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
531 which == SSL3_CHANGE_CIPHER_SERVER_READ;
532 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
533 const uint8_t *client_write_key, *server_write_key, *key;
534 const uint8_t *client_write_iv, *server_write_iv, *iv;
535 const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc;
536 const EVP_AEAD *aead = s->s3->tmp.new_aead;
David Benjaminea72bd02014-12-21 21:27:41 -0500537 size_t key_len, iv_len, mac_secret_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 const uint8_t *key_data;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 /* Reset sequence number to zero. */
541 if (!SSL_IS_DTLS(s)) {
542 memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8);
543 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700544
David Benjaminea72bd02014-12-21 21:27:41 -0500545 mac_secret_len = s->s3->tmp.new_mac_secret_len;
546 iv_len = s->s3->tmp.new_fixed_iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700547
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 if (aead != NULL) {
549 key_len = EVP_AEAD_key_length(aead);
David Benjaminea72bd02014-12-21 21:27:41 -0500550 if (mac_secret_len > 0) {
551 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites)
552 * the key length reported by |EVP_AEAD_key_length| will include the MAC
553 * and IV key bytes. */
554 if (key_len < mac_secret_len + iv_len) {
555 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
556 return 0;
557 }
558 key_len -= mac_secret_len + iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 } else {
561 key_len = EVP_CIPHER_key_length(cipher);
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 key_data = s->s3->tmp.key_block;
565 client_write_mac_secret = key_data;
566 key_data += mac_secret_len;
567 server_write_mac_secret = key_data;
568 key_data += mac_secret_len;
569 client_write_key = key_data;
570 key_data += key_len;
571 server_write_key = key_data;
572 key_data += key_len;
573 client_write_iv = key_data;
574 key_data += iv_len;
575 server_write_iv = key_data;
576 key_data += iv_len;
Adam Langleya5fa5b72014-06-20 12:00:00 -0700577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 if (use_client_keys) {
579 mac_secret = client_write_mac_secret;
580 key = client_write_key;
581 iv = client_write_iv;
582 } else {
583 mac_secret = server_write_mac_secret;
584 key = server_write_key;
585 iv = server_write_iv;
586 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
589 OPENSSL_PUT_ERROR(SSL, tls1_change_cipher_state, ERR_R_INTERNAL_ERROR);
590 return 0;
591 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 if (aead != NULL) {
594 if (!tls1_change_cipher_state_aead(s, is_read, key, key_len, iv, iv_len,
595 mac_secret, mac_secret_len)) {
596 return 0;
597 }
598 } else {
599 if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys,
600 mac_secret, mac_secret_len, key,
601 key_len, iv, iv_len)) {
602 return 0;
603 }
604 }
Adam Langleya5fa5b72014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 return 1;
607}
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609int tls1_setup_key_block(SSL *s) {
David Benjaminaf032d62014-12-22 10:42:51 -0500610 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 const EVP_CIPHER *c = NULL;
612 const EVP_MD *hash = NULL;
613 const EVP_AEAD *aead = NULL;
David Benjaminea72bd02014-12-21 21:27:41 -0500614 int mac_type = NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 int ret = 0;
David Benjaminea72bd02014-12-21 21:27:41 -0500616 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
David Benjamin1f5e1152014-12-23 09:23:32 -0500617 size_t key_block_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 if (s->s3->tmp.key_block_length != 0) {
621 return 1;
622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
David Benjaminea72bd02014-12-21 21:27:41 -0500624 if (s->session->cipher == NULL) {
625 goto cipher_unavailable_err;
626 }
627
628 /* TODO(davidben): Make DTLS record-layer code EVP_AEAD-aware. */
629 if (!SSL_IS_DTLS(s)) {
630 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
631 s->session->cipher,
632 ssl3_version_from_wire(s, s->version))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 goto cipher_unavailable_err;
634 }
635 key_len = EVP_AEAD_key_length(aead);
David Benjaminea72bd02014-12-21 21:27:41 -0500636 variable_iv_len = EVP_AEAD_nonce_length(aead);
637 if (mac_secret_len > 0) {
638 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites)
639 * the key length reported by |EVP_AEAD_key_length| will include the MAC
640 * key bytes and initial implicit IV. */
641 if (key_len < mac_secret_len + fixed_iv_len) {
642 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
643 return 0;
644 }
645 key_len -= mac_secret_len + fixed_iv_len;
646 } else {
647 /* The nonce is split into a fixed portion and a variable portion. */
648 if (variable_iv_len < fixed_iv_len) {
649 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_INTERNAL_ERROR);
650 return 0;
651 }
652 variable_iv_len -= fixed_iv_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 } else {
655 if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type,
David Benjaminea72bd02014-12-21 21:27:41 -0500656 &mac_secret_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 goto cipher_unavailable_err;
658 }
659 key_len = EVP_CIPHER_key_length(c);
David Benjaminea72bd02014-12-21 21:27:41 -0500660 fixed_iv_len = EVP_CIPHER_iv_length(c);
661 variable_iv_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 }
Adam Langley95c29f32014-06-20 12:00:00 -0700663
David Benjaminea72bd02014-12-21 21:27:41 -0500664 assert(mac_secret_len < 256);
665 assert(fixed_iv_len < 256);
666 assert(variable_iv_len < 256);
667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 s->s3->tmp.new_aead = aead;
669 s->s3->tmp.new_sym_enc = c;
670 s->s3->tmp.new_hash = hash;
671 s->s3->tmp.new_mac_pkey_type = mac_type;
David Benjaminea72bd02014-12-21 21:27:41 -0500672 s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
673 s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
674 s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700675
David Benjamin1f5e1152014-12-23 09:23:32 -0500676 key_block_len = key_len + mac_secret_len + fixed_iv_len;
677 key_block_len *= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700680
David Benjamin1f5e1152014-12-23 09:23:32 -0500681 p = (uint8_t *)OPENSSL_malloc(key_block_len);
David Benjaminaf032d62014-12-22 10:42:51 -0500682 if (p == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, ERR_R_MALLOC_FAILURE);
684 goto err;
685 }
Adam Langley95c29f32014-06-20 12:00:00 -0700686
David Benjamin1f5e1152014-12-23 09:23:32 -0500687 s->s3->tmp.key_block_length = key_block_len;
David Benjaminaf032d62014-12-22 10:42:51 -0500688 s->s3->tmp.key_block = p;
Adam Langley95c29f32014-06-20 12:00:00 -0700689
David Benjamin1f5e1152014-12-23 09:23:32 -0500690 if (!tls1_generate_key_block(s, p, key_block_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 goto err;
692 }
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 if (!SSL_USE_EXPLICIT_IV(s) &&
695 (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0) {
696 /* enable vulnerability countermeasure for CBC ciphers with known-IV
697 * problem (http://www.openssl.org/~bodo/tls-cbc.txt). */
698 s->s3->need_record_splitting = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 if (s->session->cipher != NULL &&
701 s->session->cipher->algorithm_enc == SSL_RC4) {
702 s->s3->need_record_splitting = 0;
703 }
704 }
705
706 ret = 1;
707
Adam Langley95c29f32014-06-20 12:00:00 -0700708err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800709 return ret;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700710
711cipher_unavailable_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800712 OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block,
713 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
714 return 0;
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langleyfcf25832014-12-18 17:42:32 -0800717/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|,
718 * respectively.
Adam Langley95c29f32014-06-20 12:00:00 -0700719 *
720 * Returns:
721 * 0: (in non-constant time) if the record is publically invalid (i.e. too
722 * short etc).
723 * 1: if the record's padding is valid / the encryption was successful.
724 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 * an internal error occured. */
726int tls1_enc(SSL *s, int send) {
727 SSL3_RECORD *rec;
728 EVP_CIPHER_CTX *ds;
729 unsigned long l;
730 int bs, i, j, k, pad = 0, ret, mac_size = 0;
731 const EVP_CIPHER *enc;
732 const SSL_AEAD_CTX *aead;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 if (send) {
735 rec = &s->s3->wrec;
736 aead = s->aead_write_ctx;
737 } else {
738 rec = &s->s3->rrec;
739 aead = s->aead_read_ctx;
740 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700741
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 if (aead) {
David Benjaminea72bd02014-12-21 21:27:41 -0500743 uint8_t ad[13], *seq, *in, *out, nonce[EVP_AEAD_MAX_NONCE_LENGTH];
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 unsigned nonce_used;
David Benjaminea72bd02014-12-21 21:27:41 -0500745 size_t n, ad_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700746
Adam Langleyfcf25832014-12-18 17:42:32 -0800747 seq = send ? s->s3->write_sequence : s->s3->read_sequence;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 if (SSL_IS_DTLS(s)) {
750 uint8_t dtlsseq[9], *p = dtlsseq;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700751
Adam Langleyfcf25832014-12-18 17:42:32 -0800752 s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
753 memcpy(p, &seq[2], 6);
754 memcpy(ad, dtlsseq, 8);
755 } else {
756 memcpy(ad, seq, 8);
757 for (i = 7; i >= 0; i--) {
758 ++seq[i];
759 if (seq[i] != 0) {
760 break;
761 }
762 }
763 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700764
Adam Langleyfcf25832014-12-18 17:42:32 -0800765 ad[8] = rec->type;
766 ad[9] = (uint8_t)(s->version >> 8);
767 ad[10] = (uint8_t)(s->version);
Adam Langleyc9fb3752014-06-20 12:00:00 -0700768
David Benjaminea72bd02014-12-21 21:27:41 -0500769 if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce)) {
770 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800771 return -1; /* internal error - should never happen. */
772 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700773
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
775 nonce_used = aead->fixed_nonce_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 if (send) {
778 size_t len = rec->length;
779 size_t eivlen = 0;
780 in = rec->input;
781 out = rec->data;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700782
David Benjaminea72bd02014-12-21 21:27:41 -0500783 uint8_t *variable_nonce = nonce + nonce_used;
784 if (aead->random_variable_nonce) {
785 assert(aead->variable_nonce_included_in_record);
786 if (!RAND_bytes(nonce + nonce_used, aead->variable_nonce_len)) {
787 return -1;
788 }
789 } else {
790 /* When sending we use the sequence number as the variable part of the
791 * nonce. */
792 if (aead->variable_nonce_len != 8) {
793 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
794 return -1;
795 }
796 memcpy(nonce + nonce_used, ad, aead->variable_nonce_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 nonce_used += aead->variable_nonce_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 /* in do_ssl3_write, rec->input is moved forward by variable_nonce_len in
801 * order to leave space for the variable nonce. Thus we can copy the
802 * sequence number bytes into place without overwriting any of the
803 * plaintext. */
804 if (aead->variable_nonce_included_in_record) {
David Benjaminea72bd02014-12-21 21:27:41 -0500805 memcpy(out, variable_nonce, aead->variable_nonce_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 len -= aead->variable_nonce_len;
807 eivlen = aead->variable_nonce_len;
808 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700809
David Benjaminea72bd02014-12-21 21:27:41 -0500810 if (aead->omit_length_in_ad) {
811 ad_len = 11;
812 } else {
813 ad[11] = len >> 8;
814 ad[12] = len & 0xff;
815 ad_len = 13;
816 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 if (!EVP_AEAD_CTX_seal(&aead->ctx, out + eivlen, &n, len + aead->tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500819 nonce, nonce_used, in + eivlen, len, ad, ad_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 return -1;
821 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 if (aead->variable_nonce_included_in_record) {
824 n += aead->variable_nonce_len;
825 }
826 } else {
827 /* receive */
828 size_t len = rec->length;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 if (rec->data != rec->input) {
David Benjaminea72bd02014-12-21 21:27:41 -0500831 OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 return -1; /* internal error - should never happen. */
833 }
834 out = in = rec->input;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (len < aead->variable_nonce_len) {
837 return 0;
838 }
839 memcpy(nonce + nonce_used,
840 aead->variable_nonce_included_in_record ? in : ad,
841 aead->variable_nonce_len);
842 nonce_used += aead->variable_nonce_len;
Adam Langleyc9fb3752014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (aead->variable_nonce_included_in_record) {
845 in += aead->variable_nonce_len;
846 len -= aead->variable_nonce_len;
847 out += aead->variable_nonce_len;
848 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700849
David Benjaminea72bd02014-12-21 21:27:41 -0500850 if (aead->omit_length_in_ad) {
851 ad_len = 11;
852 } else {
853 if (len < aead->tag_len) {
854 return 0;
855 }
856 size_t plaintext_len = len - aead->tag_len;
857
858 ad[11] = plaintext_len >> 8;
859 ad[12] = plaintext_len & 0xff;
860 ad_len = 13;
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700862
David Benjaminea72bd02014-12-21 21:27:41 -0500863 if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, rec->length, nonce, nonce_used, in,
864 len, ad, ad_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 return -1;
866 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700867
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 rec->data = rec->input = out;
869 }
Adam Langleyc9fb3752014-06-20 12:00:00 -0700870
Adam Langleyfcf25832014-12-18 17:42:32 -0800871 rec->length = n;
872 return 1;
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 if (send) {
876 ds = s->enc_write_ctx;
877 rec = &(s->s3->wrec);
878 if (s->enc_write_ctx == NULL) {
879 enc = NULL;
880 } else {
881 int ivlen;
882 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
883 /* For TLSv1.1 and later explicit IV */
884 if (SSL_USE_EXPLICIT_IV(s) && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE) {
885 ivlen = EVP_CIPHER_iv_length(enc);
886 } else {
887 ivlen = 0;
888 }
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 if (ivlen > 1) {
891 if (rec->data != rec->input) {
892 /* we can't write into the input stream:
893 * Can this ever happen?? (steve)
894 */
895 fprintf(stderr, "%s:%d: rec->data != rec->input\n", __FILE__,
896 __LINE__);
897 } else if (!RAND_bytes(rec->input, ivlen)) {
898 return -1;
899 }
900 }
901 }
902 } else {
903 ds = s->enc_read_ctx;
904 rec = &(s->s3->rrec);
905 if (s->enc_read_ctx == NULL) {
906 enc = NULL;
907 } else {
908 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
909 }
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (s->session == NULL || ds == NULL || enc == NULL) {
913 memmove(rec->data, rec->input, rec->length);
914 rec->input = rec->data;
915 ret = 1;
916 } else {
917 l = rec->length;
918 bs = EVP_CIPHER_block_size(ds->cipher);
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 if (bs != 1 && send) {
921 i = bs - ((int)l % bs);
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 /* Add weird padding of upto 256 bytes */
924 /* we need to add 'i' padding bytes of value j */
925 j = i - 1;
926 for (k = (int)l; k < (int)(l + i); k++) {
927 rec->input[k] = j;
928 }
929 l += i;
930 rec->length += i;
931 }
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 if (!send && (l == 0 || l % bs != 0)) {
934 return 0;
935 }
Adam Langley95c29f32014-06-20 12:00:00 -0700936
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 if (!EVP_Cipher(ds, rec->data, rec->input, l)) {
938 return -1;
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 ret = 1;
942 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
943 mac_size = EVP_MD_CTX_size(s->read_hash);
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (bs != 1 && !send) {
947 ret = tls1_cbc_remove_padding(s, rec, bs, mac_size);
948 }
949 if (pad && !send) {
950 rec->length -= pad;
951 }
952 }
953 return ret;
954}
Adam Langley95c29f32014-06-20 12:00:00 -0700955
Adam Langleyfcf25832014-12-18 17:42:32 -0800956int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
957 unsigned int ret;
958 EVP_MD_CTX ctx, *d = NULL;
959 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 if (s->s3->handshake_buffer &&
962 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
963 return 0;
964 }
965
966 for (i = 0; i < SSL_MAX_DIGEST; i++) {
967 if (s->s3->handshake_dgst[i] &&
968 EVP_MD_CTX_type(s->s3->handshake_dgst[i]) == md_nid) {
969 d = s->s3->handshake_dgst[i];
970 break;
971 }
972 }
973
974 if (!d) {
975 OPENSSL_PUT_ERROR(SSL, tls1_cert_verify_mac, SSL_R_NO_REQUIRED_DIGEST);
976 return 0;
977 }
978
979 EVP_MD_CTX_init(&ctx);
980 EVP_MD_CTX_copy_ex(&ctx, d);
981 EVP_DigestFinal_ex(&ctx, out, &ret);
982 EVP_MD_CTX_cleanup(&ctx);
983
984 return ret;
985}
Adam Langley95c29f32014-06-20 12:00:00 -0700986
Adam Langley1258b6a2014-06-20 12:00:00 -0700987/* tls1_handshake_digest calculates the current handshake hash and writes it to
988 * |out|, which has space for |out_len| bytes. It returns the number of bytes
989 * written or -1 in the event of an error. This function works on a copy of the
990 * underlying digests so can be called multiple times and prior to the final
991 * update etc. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800992int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
993 const EVP_MD *md;
994 EVP_MD_CTX ctx;
David Benjamin1f5e1152014-12-23 09:23:32 -0500995 int err = 0, len = 0;
996 size_t i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 long mask;
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 EVP_MD_CTX_init(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 for (i = 0; ssl_get_handshake_digest(i, &mask, &md); i++) {
David Benjamin1f5e1152014-12-23 09:23:32 -05001002 size_t hash_size;
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 unsigned int digest_len;
1004 EVP_MD_CTX *hdgst = s->s3->handshake_dgst[i];
Adam Langley1258b6a2014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 if ((mask & ssl_get_algorithm2(s)) == 0) {
1007 continue;
1008 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 hash_size = EVP_MD_size(md);
1011 if (!hdgst ||
David Benjamin1f5e1152014-12-23 09:23:32 -05001012 hash_size > out_len ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 !EVP_MD_CTX_copy_ex(&ctx, hdgst) ||
1014 !EVP_DigestFinal_ex(&ctx, out, &digest_len) ||
David Benjamin1f5e1152014-12-23 09:23:32 -05001015 digest_len != hash_size /* internal error */) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 err = 1;
1017 break;
1018 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 out += digest_len;
1021 out_len -= digest_len;
1022 len += digest_len;
1023 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 EVP_MD_CTX_cleanup(&ctx);
Adam Langley1258b6a2014-06-20 12:00:00 -07001026
Adam Langleyfcf25832014-12-18 17:42:32 -08001027 if (err != 0) {
1028 return -1;
1029 }
1030 return len;
1031}
Adam Langley1258b6a2014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
1034 uint8_t buf[2 * EVP_MAX_MD_SIZE];
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 int err = 0;
1036 int digests_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07001037
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 if (s->s3->handshake_buffer &&
1039 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
1040 return 0;
1041 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
1044 if (digests_len < 0) {
1045 err = 1;
1046 digests_len = 0;
1047 }
Adam Langley95c29f32014-06-20 12:00:00 -07001048
David Benjamin1f5e1152014-12-23 09:23:32 -05001049 if (!tls1_PRF(out, 12, ssl_get_algorithm2(s), s->session->master_key,
1050 s->session->master_key_length, str, slen, buf, digests_len,
1051 NULL, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 err = 1;
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 if (err) {
1056 return 0;
1057 } else {
David Benjaminaf032d62014-12-22 10:42:51 -05001058 return 12;
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 }
1060}
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062int tls1_mac(SSL *ssl, uint8_t *md, int send) {
1063 SSL3_RECORD *rec;
1064 uint8_t *seq;
1065 EVP_MD_CTX *hash;
1066 size_t md_size, orig_len;
1067 int i, ok;
1068 EVP_MD_CTX hmac, *mac_ctx;
1069 uint8_t header[13];
1070 int t;
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 if (send) {
1073 rec = &ssl->s3->wrec;
1074 seq = &ssl->s3->write_sequence[0];
1075 hash = ssl->write_hash;
1076 } else {
1077 rec = &ssl->s3->rrec;
1078 seq = &ssl->s3->read_sequence[0];
1079 hash = ssl->read_hash;
1080 }
Adam Langley95c29f32014-06-20 12:00:00 -07001081
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 t = EVP_MD_CTX_size(hash);
1083 assert(t >= 0);
1084 md_size = t;
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 mac_ctx = &hmac;
1087 if (!EVP_MD_CTX_copy(mac_ctx, hash)) {
1088 return -1;
1089 }
Adam Langley95c29f32014-06-20 12:00:00 -07001090
Adam Langleyfcf25832014-12-18 17:42:32 -08001091 if (SSL_IS_DTLS(ssl)) {
1092 uint8_t dtlsseq[8], *p = dtlsseq;
Adam Langley95c29f32014-06-20 12:00:00 -07001093
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 s2n(send ? ssl->d1->w_epoch : ssl->d1->r_epoch, p);
1095 memcpy(p, &seq[2], 6);
Adam Langley95c29f32014-06-20 12:00:00 -07001096
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 memcpy(header, dtlsseq, 8);
1098 } else {
1099 memcpy(header, seq, 8);
1100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 /* kludge: tls1_cbc_remove_padding passes padding length in rec->type */
1103 orig_len = rec->length + md_size + ((unsigned int)rec->type >> 8);
1104 rec->type &= 0xff;
Adam Langley95c29f32014-06-20 12:00:00 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 header[8] = rec->type;
1107 header[9] = (uint8_t)(ssl->version >> 8);
1108 header[10] = (uint8_t)(ssl->version);
1109 header[11] = (rec->length) >> 8;
1110 header[12] = (rec->length) & 0xff;
Adam Langley69a01602014-11-17 17:26:55 -08001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 if (!send && EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1113 ssl3_cbc_record_digest_supported(mac_ctx)) {
1114 /* This is a CBC-encrypted record. We must avoid leaking any timing-side
1115 * channel information about how many blocks of data we are hashing because
1116 * that gives an attacker a timing-oracle. */
1117 ok = ssl3_cbc_digest_record(
1118 mac_ctx, md, &md_size, header, rec->input, rec->length + md_size,
1119 orig_len, ssl->s3->read_mac_secret, ssl->s3->read_mac_secret_size,
1120 0 /* not SSLv3 */);
1121 } else {
1122 EVP_DigestSignUpdate(mac_ctx, header, sizeof(header));
1123 EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length);
1124 ok = EVP_DigestSignFinal(mac_ctx, md, &md_size);
1125 }
Adam Langley69a01602014-11-17 17:26:55 -08001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 EVP_MD_CTX_cleanup(mac_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 if (!ok) {
1130 return -1;
1131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 if (!SSL_IS_DTLS(ssl)) {
1134 for (i = 7; i >= 0; i--) {
1135 ++seq[i];
1136 if (seq[i] != 0) {
1137 break;
1138 }
1139 }
1140 }
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 return md_size;
1143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
David Benjamin1f5e1152014-12-23 09:23:32 -05001145int tls1_generate_master_secret(SSL *s, uint8_t *out, uint8_t *premaster,
1146 int premaster_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 if (s->s3->tmp.extended_master_secret) {
1148 uint8_t digests[2 * EVP_MAX_MD_SIZE];
1149 int digests_len;
Adam Langley75712922014-10-10 16:23:43 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 /* The master secret is based on the handshake hash just after sending the
1152 * ClientKeyExchange. However, we might have a client certificate to send,
1153 * in which case we might need different hashes for the verification and
1154 * thus still need the handshake buffer around. Keeping both a handshake
1155 * buffer *and* running hashes isn't yet supported so, when it comes to
1156 * calculating the Finished hash, we'll have to hash the handshake buffer
1157 * again. */
1158 if (s->s3->handshake_buffer &&
1159 !ssl3_digest_cached_records(s, dont_free_handshake_buffer)) {
1160 return 0;
1161 }
Adam Langley75712922014-10-10 16:23:43 -07001162
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 if (digests_len == -1) {
1165 return 0;
1166 }
Adam Langley75712922014-10-10 16:23:43 -07001167
David Benjamin1f5e1152014-12-23 09:23:32 -05001168 if (!tls1_PRF(out, SSL3_MASTER_SECRET_SIZE, ssl_get_algorithm2(s),
1169 premaster, premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
David Benjaminaf032d62014-12-22 10:42:51 -05001170 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
David Benjamin1f5e1152014-12-23 09:23:32 -05001171 digests_len, NULL, 0)) {
David Benjaminaf032d62014-12-22 10:42:51 -05001172 return 0;
1173 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 } else {
David Benjamin1f5e1152014-12-23 09:23:32 -05001175 if (!tls1_PRF(out, SSL3_MASTER_SECRET_SIZE, ssl_get_algorithm2(s),
1176 premaster, premaster_len, TLS_MD_MASTER_SECRET_CONST,
David Benjaminaf032d62014-12-22 10:42:51 -05001177 TLS_MD_MASTER_SECRET_CONST_SIZE, s->s3->client_random,
David Benjamin1f5e1152014-12-23 09:23:32 -05001178 SSL3_RANDOM_SIZE, s->s3->server_random,
1179 SSL3_RANDOM_SIZE)) {
David Benjaminaf032d62014-12-22 10:42:51 -05001180 return 0;
1181 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 }
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 return SSL3_MASTER_SECRET_SIZE;
1185}
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187int tls1_export_keying_material(SSL *s, uint8_t *out, size_t olen,
1188 const char *label, size_t llen,
1189 const uint8_t *context, size_t contextlen,
1190 int use_context) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 uint8_t *val = NULL;
1192 size_t vallen, currentvalpos;
1193 int ret;
Adam Langley95c29f32014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 /* construct PRF arguments we construct the PRF argument ourself rather than
1196 * passing separate values into the TLS PRF to ensure that the concatenation
1197 * of values does not create a prohibited label. */
1198 vallen = llen + SSL3_RANDOM_SIZE * 2;
1199 if (use_context) {
1200 vallen += 2 + contextlen;
1201 }
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 val = OPENSSL_malloc(vallen);
1204 if (val == NULL) {
1205 goto err2;
1206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyfcf25832014-12-18 17:42:32 -08001208 currentvalpos = 0;
1209 memcpy(val + currentvalpos, (uint8_t *)label, llen);
1210 currentvalpos += llen;
1211 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
1212 currentvalpos += SSL3_RANDOM_SIZE;
1213 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
1214 currentvalpos += SSL3_RANDOM_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 if (use_context) {
1217 val[currentvalpos] = (contextlen >> 8) & 0xff;
1218 currentvalpos++;
1219 val[currentvalpos] = contextlen & 0xff;
1220 currentvalpos++;
1221 if (contextlen > 0 || context != NULL) {
1222 memcpy(val + currentvalpos, context, contextlen);
1223 }
1224 }
Adam Langley95c29f32014-06-20 12:00:00 -07001225
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 /* disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
1227 * label len) = 15, so size of val > max(prohibited label len) = 15 and the
1228 * comparisons won't have buffer overflow. */
1229 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
1230 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0 ||
1231 memcmp(val, TLS_MD_SERVER_FINISH_CONST,
1232 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0 ||
1233 memcmp(val, TLS_MD_MASTER_SECRET_CONST,
1234 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0 ||
1235 memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
1236 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0) {
1237 goto err1;
1238 }
Adam Langley95c29f32014-06-20 12:00:00 -07001239
David Benjamin1f5e1152014-12-23 09:23:32 -05001240 ret = tls1_PRF(out, olen, ssl_get_algorithm2(s), s->session->master_key,
1241 s->session->master_key_length, (const char *)val, vallen, NULL,
1242 0, NULL, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 goto out;
1244
Adam Langley95c29f32014-06-20 12:00:00 -07001245err1:
Adam Langleyfcf25832014-12-18 17:42:32 -08001246 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material,
1247 SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
1248 ret = 0;
1249 goto out;
Adam Langley95c29f32014-06-20 12:00:00 -07001250
Adam Langleyfcf25832014-12-18 17:42:32 -08001251err2:
1252 OPENSSL_PUT_ERROR(SSL, tls1_export_keying_material, ERR_R_MALLOC_FAILURE);
1253 ret = 0;
1254
1255out:
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 if (val != NULL) {
1257 OPENSSL_free(val);
1258 }
1259
1260 return ret;
1261}
1262
1263int tls1_alert_code(int code) {
1264 switch (code) {
1265 case SSL_AD_CLOSE_NOTIFY:
1266 return SSL3_AD_CLOSE_NOTIFY;
1267
1268 case SSL_AD_UNEXPECTED_MESSAGE:
1269 return SSL3_AD_UNEXPECTED_MESSAGE;
1270
1271 case SSL_AD_BAD_RECORD_MAC:
1272 return SSL3_AD_BAD_RECORD_MAC;
1273
1274 case SSL_AD_DECRYPTION_FAILED:
1275 return TLS1_AD_DECRYPTION_FAILED;
1276
1277 case SSL_AD_RECORD_OVERFLOW:
1278 return TLS1_AD_RECORD_OVERFLOW;
1279
1280 case SSL_AD_DECOMPRESSION_FAILURE:
1281 return SSL3_AD_DECOMPRESSION_FAILURE;
1282
1283 case SSL_AD_HANDSHAKE_FAILURE:
1284 return SSL3_AD_HANDSHAKE_FAILURE;
1285
1286 case SSL_AD_NO_CERTIFICATE:
1287 return -1;
1288
1289 case SSL_AD_BAD_CERTIFICATE:
1290 return SSL3_AD_BAD_CERTIFICATE;
1291
1292 case SSL_AD_UNSUPPORTED_CERTIFICATE:
1293 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
1294
1295 case SSL_AD_CERTIFICATE_REVOKED:
1296 return SSL3_AD_CERTIFICATE_REVOKED;
1297
1298 case SSL_AD_CERTIFICATE_EXPIRED:
1299 return SSL3_AD_CERTIFICATE_EXPIRED;
1300
1301 case SSL_AD_CERTIFICATE_UNKNOWN:
1302 return SSL3_AD_CERTIFICATE_UNKNOWN;
1303
1304 case SSL_AD_ILLEGAL_PARAMETER:
1305 return SSL3_AD_ILLEGAL_PARAMETER;
1306
1307 case SSL_AD_UNKNOWN_CA:
1308 return TLS1_AD_UNKNOWN_CA;
1309
1310 case SSL_AD_ACCESS_DENIED:
1311 return TLS1_AD_ACCESS_DENIED;
1312
1313 case SSL_AD_DECODE_ERROR:
1314 return TLS1_AD_DECODE_ERROR;
1315
1316 case SSL_AD_DECRYPT_ERROR:
1317 return TLS1_AD_DECRYPT_ERROR;
1318 case SSL_AD_EXPORT_RESTRICTION:
1319 return TLS1_AD_EXPORT_RESTRICTION;
1320
1321 case SSL_AD_PROTOCOL_VERSION:
1322 return TLS1_AD_PROTOCOL_VERSION;
1323
1324 case SSL_AD_INSUFFICIENT_SECURITY:
1325 return TLS1_AD_INSUFFICIENT_SECURITY;
1326
1327 case SSL_AD_INTERNAL_ERROR:
1328 return TLS1_AD_INTERNAL_ERROR;
1329
1330 case SSL_AD_USER_CANCELLED:
1331 return TLS1_AD_USER_CANCELLED;
1332
1333 case SSL_AD_NO_RENEGOTIATION:
1334 return TLS1_AD_NO_RENEGOTIATION;
1335
1336 case SSL_AD_UNSUPPORTED_EXTENSION:
1337 return TLS1_AD_UNSUPPORTED_EXTENSION;
1338
1339 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
1340 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
1341
1342 case SSL_AD_UNRECOGNIZED_NAME:
1343 return TLS1_AD_UNRECOGNIZED_NAME;
1344
1345 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
1346 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1347
1348 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
1349 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
1350
1351 case SSL_AD_UNKNOWN_PSK_IDENTITY:
1352 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
1353
1354 case SSL_AD_INAPPROPRIATE_FALLBACK:
1355 return SSL3_AD_INAPPROPRIATE_FALLBACK;
1356
1357 default:
1358 return -1;
1359 }
1360}