blob: 076f8bd24c4fc87f922ec7f0dec9b753e49ab9d6 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000136#include <openssl/ssl.h>
137
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138#include <assert.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700139#include <stdio.h>
140#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800141
142#include <openssl/err.h>
143#include <openssl/evp.h>
144#include <openssl/hmac.h>
145#include <openssl/md5.h>
146#include <openssl/mem.h>
147#include <openssl/obj.h>
148#include <openssl/rand.h>
149
Adam Langleye9ada862015-05-11 17:20:37 -0700150#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151
152
153/* tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
Kenny Rootb8494592015-09-25 02:29:14 +0000154 * section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155 * |secret| as the secret. |seed1| through |seed3| are concatenated to form the
156 * seed parameter. It returns one on success and zero on failure. */
157static int tls1_P_hash(uint8_t *out, size_t out_len, const EVP_MD *md,
158 const uint8_t *secret, size_t secret_len,
159 const uint8_t *seed1, size_t seed1_len,
160 const uint8_t *seed2, size_t seed2_len,
161 const uint8_t *seed3, size_t seed3_len) {
162 size_t chunk;
163 HMAC_CTX ctx, ctx_tmp, ctx_init;
164 uint8_t A1[EVP_MAX_MD_SIZE];
165 unsigned A1_len;
166 int ret = 0;
167
168 chunk = EVP_MD_size(md);
169
170 HMAC_CTX_init(&ctx);
171 HMAC_CTX_init(&ctx_tmp);
172 HMAC_CTX_init(&ctx_init);
173 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
174 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
175 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
176 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
177 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len)) ||
178 !HMAC_Final(&ctx, A1, &A1_len)) {
179 goto err;
180 }
181
182 for (;;) {
183 /* Reinit mac contexts. */
184 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
185 !HMAC_Update(&ctx, A1, A1_len) ||
186 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
187 (seed1_len && !HMAC_Update(&ctx, seed1, seed1_len)) ||
188 (seed2_len && !HMAC_Update(&ctx, seed2, seed2_len)) ||
189 (seed3_len && !HMAC_Update(&ctx, seed3, seed3_len))) {
190 goto err;
191 }
192
Kenny Rootb8494592015-09-25 02:29:14 +0000193 unsigned len;
194 uint8_t hmac[EVP_MAX_MD_SIZE];
195 if (!HMAC_Final(&ctx, hmac, &len)) {
196 goto err;
197 }
198 assert(len == chunk);
199
200 /* XOR the result into |out|. */
201 if (len > out_len) {
202 len = out_len;
203 }
204 unsigned i;
205 for (i = 0; i < len; i++) {
206 out[i] ^= hmac[i];
207 }
208 out += len;
209 out_len -= len;
210
211 if (out_len == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 break;
213 }
Kenny Rootb8494592015-09-25 02:29:14 +0000214
215 /* Calculate the next A1 value. */
216 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
217 goto err;
218 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 }
220
221 ret = 1;
222
223err:
224 HMAC_CTX_cleanup(&ctx);
225 HMAC_CTX_cleanup(&ctx_tmp);
226 HMAC_CTX_cleanup(&ctx_init);
227 OPENSSL_cleanse(A1, sizeof(A1));
228 return ret;
229}
230
231int tls1_prf(SSL *s, uint8_t *out, size_t out_len, const uint8_t *secret,
232 size_t secret_len, const char *label, size_t label_len,
233 const uint8_t *seed1, size_t seed1_len,
234 const uint8_t *seed2, size_t seed2_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235
236 if (out_len == 0) {
237 return 1;
238 }
239
Kenny Rootb8494592015-09-25 02:29:14 +0000240 memset(out, 0, out_len);
241
242 uint32_t algorithm_prf = ssl_get_algorithm_prf(s);
243 if (algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) {
244 /* If using the MD5/SHA1 PRF, |secret| is partitioned between SHA-1 and
245 * MD5, MD5 first. */
246 size_t secret_half = secret_len - (secret_len / 2);
247 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half,
248 (const uint8_t *)label, label_len, seed1, seed1_len, seed2,
249 seed2_len)) {
250 return 0;
251 }
252
253 /* Note that, if |secret_len| is odd, the two halves share a byte. */
254 secret = secret + (secret_len - secret_half);
255 secret_len = secret_half;
256 }
257
258 if (!tls1_P_hash(out, out_len, ssl_get_handshake_digest(algorithm_prf),
259 secret, secret_len, (const uint8_t *)label, label_len,
260 seed1, seed1_len, seed2, seed2_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800261 return 0;
262 }
263
Kenny Rootb8494592015-09-25 02:29:14 +0000264 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265}
266
267static int tls1_generate_key_block(SSL *s, uint8_t *out, size_t out_len) {
268 return s->enc_method->prf(s, out, out_len, s->session->master_key,
269 s->session->master_key_length,
270 TLS_MD_KEY_EXPANSION_CONST,
271 TLS_MD_KEY_EXPANSION_CONST_SIZE,
272 s->s3->server_random, SSL3_RANDOM_SIZE,
273 s->s3->client_random,
274 SSL3_RANDOM_SIZE);
275}
276
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277int tls1_change_cipher_state(SSL *s, int which) {
278 /* is_read is true if we have just read a ChangeCipherSpec message - i.e. we
279 * need to update the read cipherspec. Otherwise we have just written one. */
280 const char is_read = (which & SSL3_CC_READ) != 0;
281 /* use_client_keys is true if we wish to use the keys for the "client write"
282 * direction. This is the case if we're a client sending a ChangeCipherSpec,
283 * or a server reading a client's ChangeCipherSpec. */
284 const char use_client_keys = which == SSL3_CHANGE_CIPHER_CLIENT_WRITE ||
285 which == SSL3_CHANGE_CIPHER_SERVER_READ;
286 const uint8_t *client_write_mac_secret, *server_write_mac_secret, *mac_secret;
287 const uint8_t *client_write_key, *server_write_key, *key;
288 const uint8_t *client_write_iv, *server_write_iv, *iv;
289 const EVP_AEAD *aead = s->s3->tmp.new_aead;
290 size_t key_len, iv_len, mac_secret_len;
291 const uint8_t *key_data;
292
293 /* Reset sequence number to zero. */
294 if (!SSL_IS_DTLS(s)) {
295 memset(is_read ? s->s3->read_sequence : s->s3->write_sequence, 0, 8);
296 }
297
298 mac_secret_len = s->s3->tmp.new_mac_secret_len;
299 iv_len = s->s3->tmp.new_fixed_iv_len;
300
301 if (aead == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000302 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303 return 0;
304 }
305
306 key_len = EVP_AEAD_key_length(aead);
307 if (mac_secret_len > 0) {
308 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher
309 * suites) the key length reported by |EVP_AEAD_key_length| will
310 * include the MAC and IV key bytes. */
311 if (key_len < mac_secret_len + iv_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000312 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800313 return 0;
314 }
315 key_len -= mac_secret_len + iv_len;
316 }
317
318 key_data = s->s3->tmp.key_block;
319 client_write_mac_secret = key_data;
320 key_data += mac_secret_len;
321 server_write_mac_secret = key_data;
322 key_data += mac_secret_len;
323 client_write_key = key_data;
324 key_data += key_len;
325 server_write_key = key_data;
326 key_data += key_len;
327 client_write_iv = key_data;
328 key_data += iv_len;
329 server_write_iv = key_data;
330 key_data += iv_len;
331
332 if (use_client_keys) {
333 mac_secret = client_write_mac_secret;
334 key = client_write_key;
335 iv = client_write_iv;
336 } else {
337 mac_secret = server_write_mac_secret;
338 key = server_write_key;
339 iv = server_write_iv;
340 }
341
342 if (key_data - s->s3->tmp.key_block != s->s3->tmp.key_block_length) {
Kenny Rootb8494592015-09-25 02:29:14 +0000343 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800344 return 0;
345 }
346
Adam Langleyf4e42722015-06-04 17:45:09 -0700347 if (is_read) {
348 SSL_AEAD_CTX_free(s->aead_read_ctx);
349 s->aead_read_ctx = SSL_AEAD_CTX_new(
350 evp_aead_open, ssl3_version_from_wire(s, s->version),
351 s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
352 iv_len);
353 return s->aead_read_ctx != NULL;
Adam Langleyf4e42722015-06-04 17:45:09 -0700354 }
Kenny Rootb8494592015-09-25 02:29:14 +0000355
356 SSL_AEAD_CTX_free(s->aead_write_ctx);
357 s->aead_write_ctx = SSL_AEAD_CTX_new(
358 evp_aead_seal, ssl3_version_from_wire(s, s->version),
359 s->s3->tmp.new_cipher, key, key_len, mac_secret, mac_secret_len, iv,
360 iv_len);
361 if (s->aead_write_ctx == NULL) {
362 return 0;
363 }
364
365 s->s3->need_record_splitting = 0;
366 if (!SSL_USE_EXPLICIT_IV(s) &&
367 (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
368 SSL_CIPHER_is_block_cipher(s->s3->tmp.new_cipher)) {
369 /* Enable 1/n-1 record-splitting to randomize the IV. See
370 * https://www.openssl.org/~bodo/tls-cbc.txt and the BEAST attack. */
371 s->s3->need_record_splitting = 1;
372 }
373 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374}
375
376int tls1_setup_key_block(SSL *s) {
377 uint8_t *p;
378 const EVP_AEAD *aead = NULL;
379 int ret = 0;
380 size_t mac_secret_len, fixed_iv_len, variable_iv_len, key_len;
381 size_t key_block_len;
382
383 if (s->s3->tmp.key_block_length != 0) {
384 return 1;
385 }
386
387 if (s->session->cipher == NULL) {
388 goto cipher_unavailable_err;
389 }
390
391 if (!ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
392 s->session->cipher,
393 ssl3_version_from_wire(s, s->version))) {
394 goto cipher_unavailable_err;
395 }
396 key_len = EVP_AEAD_key_length(aead);
397 variable_iv_len = EVP_AEAD_nonce_length(aead);
398 if (mac_secret_len > 0) {
399 /* For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
400 * key length reported by |EVP_AEAD_key_length| will include the MAC key
401 * bytes and initial implicit IV. */
402 if (key_len < mac_secret_len + fixed_iv_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000403 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800404 return 0;
405 }
406 key_len -= mac_secret_len + fixed_iv_len;
407 } else {
408 /* The nonce is split into a fixed portion and a variable portion. */
409 if (variable_iv_len < fixed_iv_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000410 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800411 return 0;
412 }
413 variable_iv_len -= fixed_iv_len;
414 }
415
416 assert(mac_secret_len < 256);
417 assert(fixed_iv_len < 256);
418 assert(variable_iv_len < 256);
419
420 s->s3->tmp.new_aead = aead;
421 s->s3->tmp.new_mac_secret_len = (uint8_t)mac_secret_len;
422 s->s3->tmp.new_fixed_iv_len = (uint8_t)fixed_iv_len;
423 s->s3->tmp.new_variable_iv_len = (uint8_t)variable_iv_len;
424
425 key_block_len = key_len + mac_secret_len + fixed_iv_len;
426 key_block_len *= 2;
427
428 ssl3_cleanup_key_block(s);
429
430 p = (uint8_t *)OPENSSL_malloc(key_block_len);
431 if (p == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000432 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800433 goto err;
434 }
435
436 s->s3->tmp.key_block_length = key_block_len;
437 s->s3->tmp.key_block = p;
438
439 if (!tls1_generate_key_block(s, p, key_block_len)) {
440 goto err;
441 }
442
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443 ret = 1;
444
445err:
446 return ret;
447
448cipher_unavailable_err:
Kenny Rootb8494592015-09-25 02:29:14 +0000449 OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800450 return 0;
451}
452
Adam Langleyd9e397b2015-01-22 14:27:53 -0800453int tls1_cert_verify_mac(SSL *s, int md_nid, uint8_t *out) {
Kenny Rootb8494592015-09-25 02:29:14 +0000454 const EVP_MD_CTX *ctx_template;
455 if (md_nid == NID_md5) {
456 ctx_template = &s->s3->handshake_md5;
457 } else if (md_nid == EVP_MD_CTX_type(&s->s3->handshake_hash)) {
458 ctx_template = &s->s3->handshake_hash;
459 } else {
460 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800461 return 0;
462 }
463
Kenny Rootb8494592015-09-25 02:29:14 +0000464 EVP_MD_CTX ctx;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800465 EVP_MD_CTX_init(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000466 if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700467 EVP_MD_CTX_cleanup(&ctx);
468 return 0;
469 }
Kenny Rootb8494592015-09-25 02:29:14 +0000470 unsigned ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800471 EVP_DigestFinal_ex(&ctx, out, &ret);
472 EVP_MD_CTX_cleanup(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000473 return ret;
474}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800475
Kenny Rootb8494592015-09-25 02:29:14 +0000476static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
477 size_t max_out) {
478 int ret = 0;
479 EVP_MD_CTX ctx_copy;
480 EVP_MD_CTX_init(&ctx_copy);
481
482 if (EVP_MD_CTX_size(ctx) > max_out) {
483 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
484 goto err;
485 }
486 unsigned len;
487 if (!EVP_MD_CTX_copy_ex(&ctx_copy, ctx) ||
488 !EVP_DigestFinal_ex(&ctx_copy, out, &len)) {
489 goto err;
490 }
491 assert(len == EVP_MD_CTX_size(ctx));
492
493 *out_len = len;
494 ret = 1;
495
496err:
497 EVP_MD_CTX_cleanup(&ctx_copy);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800498 return ret;
499}
500
501/* tls1_handshake_digest calculates the current handshake hash and writes it to
502 * |out|, which has space for |out_len| bytes. It returns the number of bytes
503 * written or -1 in the event of an error. This function works on a copy of the
504 * underlying digests so can be called multiple times and prior to the final
505 * update etc. */
506int tls1_handshake_digest(SSL *s, uint8_t *out, size_t out_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000507 size_t md5_len = 0;
508 if (EVP_MD_CTX_md(&s->s3->handshake_md5) != NULL &&
509 !append_digest(&s->s3->handshake_md5, out, &md5_len, out_len)) {
Adam Langley1e4884f2015-09-24 10:57:52 -0700510 return -1;
511 }
Kenny Rootb8494592015-09-25 02:29:14 +0000512
513 size_t len;
514 if (!append_digest(&s->s3->handshake_hash, out + md5_len, &len,
515 out_len - md5_len)) {
516 return -1;
517 }
518
519 return (int)(md5_len + len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800520}
521
522int tls1_final_finish_mac(SSL *s, const char *str, int slen, uint8_t *out) {
523 uint8_t buf[2 * EVP_MAX_MD_SIZE];
524 int err = 0;
525 int digests_len;
526
Adam Langleyf4e42722015-06-04 17:45:09 -0700527 /* At this point, the handshake should have released the handshake buffer on
Kenny Rootb8494592015-09-25 02:29:14 +0000528 * its own. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700529 assert(s->s3->handshake_buffer == NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800530
531 digests_len = tls1_handshake_digest(s, buf, sizeof(buf));
532 if (digests_len < 0) {
533 err = 1;
534 digests_len = 0;
535 }
536
537 if (!s->enc_method->prf(s, out, 12, s->session->master_key,
538 s->session->master_key_length, str, slen, buf,
539 digests_len, NULL, 0)) {
540 err = 1;
541 }
542
543 if (err) {
544 return 0;
545 } else {
546 return 12;
547 }
548}
549
550int tls1_generate_master_secret(SSL *s, uint8_t *out, const uint8_t *premaster,
551 size_t premaster_len) {
552 if (s->s3->tmp.extended_master_secret) {
553 uint8_t digests[2 * EVP_MAX_MD_SIZE];
Kenny Rootb8494592015-09-25 02:29:14 +0000554 int digests_len = tls1_handshake_digest(s, digests, sizeof(digests));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800555 if (digests_len == -1) {
556 return 0;
557 }
558
559 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
560 premaster_len, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
561 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE, digests,
562 digests_len, NULL, 0)) {
563 return 0;
564 }
565 } else {
566 if (!s->enc_method->prf(s, out, SSL3_MASTER_SECRET_SIZE, premaster,
567 premaster_len, TLS_MD_MASTER_SECRET_CONST,
568 TLS_MD_MASTER_SECRET_CONST_SIZE,
569 s->s3->client_random, SSL3_RANDOM_SIZE,
570 s->s3->server_random, SSL3_RANDOM_SIZE)) {
571 return 0;
572 }
573 }
574
575 return SSL3_MASTER_SECRET_SIZE;
576}
577
Adam Langleye9ada862015-05-11 17:20:37 -0700578int tls1_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
579 const char *label, size_t label_len,
580 const uint8_t *context, size_t context_len,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800581 int use_context) {
Adam Langleye9ada862015-05-11 17:20:37 -0700582 if (!s->s3->have_version || s->version == SSL3_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +0000583 OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleye9ada862015-05-11 17:20:37 -0700584 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800585 }
586
Adam Langleye9ada862015-05-11 17:20:37 -0700587 size_t seed_len = 2 * SSL3_RANDOM_SIZE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800588 if (use_context) {
Adam Langleye9ada862015-05-11 17:20:37 -0700589 if (context_len >= 1u << 16) {
Kenny Rootb8494592015-09-25 02:29:14 +0000590 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
Adam Langleye9ada862015-05-11 17:20:37 -0700591 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800592 }
Adam Langleye9ada862015-05-11 17:20:37 -0700593 seed_len += 2 + context_len;
594 }
595 uint8_t *seed = OPENSSL_malloc(seed_len);
596 if (seed == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000597 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleye9ada862015-05-11 17:20:37 -0700598 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800599 }
600
Adam Langleye9ada862015-05-11 17:20:37 -0700601 memcpy(seed, s->s3->client_random, SSL3_RANDOM_SIZE);
602 memcpy(seed + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
603 if (use_context) {
604 seed[2 * SSL3_RANDOM_SIZE] = (uint8_t)(context_len >> 8);
605 seed[2 * SSL3_RANDOM_SIZE + 1] = (uint8_t)context_len;
606 memcpy(seed + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800607 }
608
Adam Langleye9ada862015-05-11 17:20:37 -0700609 int ret = s->enc_method->prf(s, out, out_len, s->session->master_key,
610 s->session->master_key_length, label, label_len,
611 seed, seed_len, NULL, 0);
612 OPENSSL_free(seed);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613 return ret;
614}
615
616int tls1_alert_code(int code) {
617 switch (code) {
618 case SSL_AD_CLOSE_NOTIFY:
619 return SSL3_AD_CLOSE_NOTIFY;
620
621 case SSL_AD_UNEXPECTED_MESSAGE:
622 return SSL3_AD_UNEXPECTED_MESSAGE;
623
624 case SSL_AD_BAD_RECORD_MAC:
625 return SSL3_AD_BAD_RECORD_MAC;
626
627 case SSL_AD_DECRYPTION_FAILED:
628 return TLS1_AD_DECRYPTION_FAILED;
629
630 case SSL_AD_RECORD_OVERFLOW:
631 return TLS1_AD_RECORD_OVERFLOW;
632
633 case SSL_AD_DECOMPRESSION_FAILURE:
634 return SSL3_AD_DECOMPRESSION_FAILURE;
635
636 case SSL_AD_HANDSHAKE_FAILURE:
637 return SSL3_AD_HANDSHAKE_FAILURE;
638
639 case SSL_AD_NO_CERTIFICATE:
640 return -1;
641
642 case SSL_AD_BAD_CERTIFICATE:
643 return SSL3_AD_BAD_CERTIFICATE;
644
645 case SSL_AD_UNSUPPORTED_CERTIFICATE:
646 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
647
648 case SSL_AD_CERTIFICATE_REVOKED:
649 return SSL3_AD_CERTIFICATE_REVOKED;
650
651 case SSL_AD_CERTIFICATE_EXPIRED:
652 return SSL3_AD_CERTIFICATE_EXPIRED;
653
654 case SSL_AD_CERTIFICATE_UNKNOWN:
655 return SSL3_AD_CERTIFICATE_UNKNOWN;
656
657 case SSL_AD_ILLEGAL_PARAMETER:
658 return SSL3_AD_ILLEGAL_PARAMETER;
659
660 case SSL_AD_UNKNOWN_CA:
661 return TLS1_AD_UNKNOWN_CA;
662
663 case SSL_AD_ACCESS_DENIED:
664 return TLS1_AD_ACCESS_DENIED;
665
666 case SSL_AD_DECODE_ERROR:
667 return TLS1_AD_DECODE_ERROR;
668
669 case SSL_AD_DECRYPT_ERROR:
670 return TLS1_AD_DECRYPT_ERROR;
671 case SSL_AD_EXPORT_RESTRICTION:
672 return TLS1_AD_EXPORT_RESTRICTION;
673
674 case SSL_AD_PROTOCOL_VERSION:
675 return TLS1_AD_PROTOCOL_VERSION;
676
677 case SSL_AD_INSUFFICIENT_SECURITY:
678 return TLS1_AD_INSUFFICIENT_SECURITY;
679
680 case SSL_AD_INTERNAL_ERROR:
681 return TLS1_AD_INTERNAL_ERROR;
682
683 case SSL_AD_USER_CANCELLED:
684 return TLS1_AD_USER_CANCELLED;
685
686 case SSL_AD_NO_RENEGOTIATION:
687 return TLS1_AD_NO_RENEGOTIATION;
688
689 case SSL_AD_UNSUPPORTED_EXTENSION:
690 return TLS1_AD_UNSUPPORTED_EXTENSION;
691
692 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
693 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
694
695 case SSL_AD_UNRECOGNIZED_NAME:
696 return TLS1_AD_UNRECOGNIZED_NAME;
697
698 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
699 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
700
701 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
702 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
703
704 case SSL_AD_UNKNOWN_PSK_IDENTITY:
705 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
706
707 case SSL_AD_INAPPROPRIATE_FALLBACK:
708 return SSL3_AD_INAPPROPRIATE_FALLBACK;
709
710 default:
711 return -1;
712 }
713}