]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/t1_enc.c
Make asn1 fuzzer more reproducible
[thirdparty/openssl.git] / ssl / t1_enc.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
82b0bf0b 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
82b0bf0b 8 */
846e33c7 9
ddac1974
NL
10/* ====================================================================
11 * Copyright 2005 Nokia. All rights reserved.
12 *
13 * The portions of the attached software ("Contribution") is developed by
14 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
15 * license.
16 *
17 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
18 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
19 * support (see RFC 4279) to OpenSSL.
20 *
21 * No patent licenses or other rights except those expressly stated in
22 * the OpenSSL open source license shall be deemed granted or received
23 * expressly, by implication, estoppel, or otherwise.
24 *
25 * No assurances are provided by Nokia that the Contribution does not
26 * infringe the patent or other intellectual property rights of any third
27 * party or that the license provides you with all the necessary rights
28 * to make use of the Contribution.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
31 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
32 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
33 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
34 * OTHERWISE.
35 */
58964a49
RE
36
37#include <stdio.h>
7b63c0fa 38#include "ssl_locl.h"
3c27208f 39#include <openssl/comp.h>
ec577822 40#include <openssl/evp.h>
b7d60e76 41#include <openssl/kdf.h>
637f374a 42#include <openssl/rand.h>
58964a49 43
b7d60e76 44/* seed1 through seed5 are concatenated */
28ba2541 45static int tls1_PRF(SSL *s,
6db6bc5a
MC
46 const void *seed1, size_t seed1_len,
47 const void *seed2, size_t seed2_len,
48 const void *seed3, size_t seed3_len,
49 const void *seed4, size_t seed4_len,
50 const void *seed5, size_t seed5_len,
51 const unsigned char *sec, size_t slen,
52 unsigned char *out, size_t olen)
0f113f3e 53{
28ba2541 54 const EVP_MD *md = ssl_prf_md(s);
b7d60e76
DSH
55 EVP_PKEY_CTX *pctx = NULL;
56
57 int ret = 0;
0f113f3e 58
28ba2541 59 if (md == NULL) {
668f6f08
MC
60 /* Should never happen */
61 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
28ba2541 62 return 0;
668f6f08 63 }
b7d60e76
DSH
64 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
65 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
66 || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
348240c6 67 || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0)
b7d60e76 68 goto err;
28ba2541 69
348240c6 70 if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0)
b7d60e76 71 goto err;
348240c6 72 if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0)
b7d60e76 73 goto err;
348240c6 74 if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0)
b7d60e76 75 goto err;
348240c6 76 if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0)
b7d60e76 77 goto err;
348240c6 78 if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0)
b7d60e76
DSH
79 goto err;
80
6db6bc5a 81 if (EVP_PKEY_derive(pctx, out, &olen) <= 0)
b7d60e76
DSH
82 goto err;
83 ret = 1;
84
a230b26e 85 err:
b7d60e76
DSH
86 EVP_PKEY_CTX_free(pctx);
87 return ret;
81025661 88}
0f113f3e 89
8c1a5343 90static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
0f113f3e
MC
91{
92 int ret;
28ba2541 93 ret = tls1_PRF(s,
0f113f3e
MC
94 TLS_MD_KEY_EXPANSION_CONST,
95 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
96 SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
97 NULL, 0, NULL, 0, s->session->master_key,
b7d60e76 98 s->session->master_key_length, km, num);
55a9a16f 99
0f113f3e
MC
100 return ret;
101}
58964a49 102
6b691a5c 103int tls1_change_cipher_state(SSL *s, int which)
0f113f3e 104{
0f113f3e 105 unsigned char *p, *mac_secret;
0f113f3e
MC
106 unsigned char tmp1[EVP_MAX_KEY_LENGTH];
107 unsigned char tmp2[EVP_MAX_KEY_LENGTH];
108 unsigned char iv1[EVP_MAX_IV_LENGTH * 2];
109 unsigned char iv2[EVP_MAX_IV_LENGTH * 2];
110 unsigned char *ms, *key, *iv;
0f113f3e
MC
111 EVP_CIPHER_CTX *dd;
112 const EVP_CIPHER *c;
09b6c2ef 113#ifndef OPENSSL_NO_COMP
0f113f3e 114 const SSL_COMP *comp;
09b6c2ef 115#endif
0f113f3e
MC
116 const EVP_MD *m;
117 int mac_type;
b43d1cbb 118 size_t *mac_secret_size;
0f113f3e
MC
119 EVP_MD_CTX *mac_ctx;
120 EVP_PKEY *mac_key;
b43d1cbb 121 size_t n, i, j, k, cl;
0f113f3e
MC
122 int reuse_dd = 0;
123
0f113f3e
MC
124 c = s->s3->tmp.new_sym_enc;
125 m = s->s3->tmp.new_hash;
126 mac_type = s->s3->tmp.new_mac_pkey_type;
09b6c2ef 127#ifndef OPENSSL_NO_COMP
0f113f3e 128 comp = s->s3->tmp.new_compression;
09b6c2ef 129#endif
58964a49 130
0f113f3e
MC
131 if (which & SSL3_CC_READ) {
132 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
133 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
134 else
135 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
136
137 if (s->enc_read_ctx != NULL)
138 reuse_dd = 1;
846ec07d 139 else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
0f113f3e
MC
140 goto err;
141 else
142 /*
f430ba31 143 * make sure it's initialised in case we exit later with an error
0f113f3e 144 */
846ec07d 145 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
0f113f3e
MC
146 dd = s->enc_read_ctx;
147 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
5f3d93e4
MC
148 if (mac_ctx == NULL)
149 goto err;
09b6c2ef 150#ifndef OPENSSL_NO_COMP
efa7dd64
RS
151 COMP_CTX_free(s->expand);
152 s->expand = NULL;
0f113f3e
MC
153 if (comp != NULL) {
154 s->expand = COMP_CTX_new(comp->method);
155 if (s->expand == NULL) {
156 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
157 SSL_R_COMPRESSION_LIBRARY_ERROR);
158 goto err2;
159 }
0f113f3e 160 }
09b6c2ef 161#endif
0f113f3e 162 /*
d5d0a1cb 163 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 164 */
d5d0a1cb 165 if (!SSL_IS_DTLS(s))
de07f311 166 RECORD_LAYER_reset_read_sequence(&s->rlayer);
0f113f3e
MC
167 mac_secret = &(s->s3->read_mac_secret[0]);
168 mac_secret_size = &(s->s3->read_mac_secret_size);
169 } else {
170 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
171 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
172 else
173 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
174 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s))
175 reuse_dd = 1;
176 else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
177 goto err;
178 dd = s->enc_write_ctx;
179 if (SSL_IS_DTLS(s)) {
bfb0641f 180 mac_ctx = EVP_MD_CTX_new();
5f3d93e4 181 if (mac_ctx == NULL)
0f113f3e
MC
182 goto err;
183 s->write_hash = mac_ctx;
5f3d93e4 184 } else {
0f113f3e 185 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
5f3d93e4
MC
186 if (mac_ctx == NULL)
187 goto err;
188 }
09b6c2ef 189#ifndef OPENSSL_NO_COMP
efa7dd64
RS
190 COMP_CTX_free(s->compress);
191 s->compress = NULL;
0f113f3e
MC
192 if (comp != NULL) {
193 s->compress = COMP_CTX_new(comp->method);
194 if (s->compress == NULL) {
195 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
196 SSL_R_COMPRESSION_LIBRARY_ERROR);
197 goto err2;
198 }
199 }
09b6c2ef 200#endif
0f113f3e 201 /*
d5d0a1cb 202 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 203 */
d5d0a1cb 204 if (!SSL_IS_DTLS(s))
de07f311 205 RECORD_LAYER_reset_write_sequence(&s->rlayer);
0f113f3e
MC
206 mac_secret = &(s->s3->write_mac_secret[0]);
207 mac_secret_size = &(s->s3->write_mac_secret_size);
208 }
209
210 if (reuse_dd)
846ec07d 211 EVP_CIPHER_CTX_reset(dd);
0f113f3e
MC
212
213 p = s->s3->tmp.key_block;
214 i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
215
b43d1cbb 216 /* TODO(size_t): convert me */
0f113f3e 217 cl = EVP_CIPHER_key_length(c);
361a1191 218 j = cl;
0f113f3e 219 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
e75c5a79 220 /* If GCM/CCM mode only part of IV comes from PRF */
0f113f3e
MC
221 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
222 k = EVP_GCM_TLS_FIXED_IV_LEN;
e75c5a79
DSH
223 else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
224 k = EVP_CCM_TLS_FIXED_IV_LEN;
0f113f3e
MC
225 else
226 k = EVP_CIPHER_iv_length(c);
227 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
228 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
229 ms = &(p[0]);
230 n = i + i;
231 key = &(p[n]);
232 n += j + j;
233 iv = &(p[n]);
234 n += k + k;
0f113f3e
MC
235 } else {
236 n = i;
237 ms = &(p[n]);
238 n += i + j;
239 key = &(p[n]);
240 n += j + k;
241 iv = &(p[n]);
242 n += k;
0f113f3e
MC
243 }
244
245 if (n > s->s3->tmp.key_block_length) {
246 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
247 goto err2;
248 }
249
250 memcpy(mac_secret, ms, i);
251
252 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
348240c6 253 /* TODO(size_t): Convert this function */
0f113f3e 254 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
348240c6 255 mac_secret, (int)*mac_secret_size);
5f3d93e4 256 if (mac_key == NULL
a230b26e 257 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
5f3d93e4
MC
258 EVP_PKEY_free(mac_key);
259 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
260 goto err2;
261 }
0f113f3e
MC
262 EVP_PKEY_free(mac_key);
263 }
d63a5e5e 264#ifdef SSL_DEBUG
0f113f3e
MC
265 printf("which = %04X\nmac key=", which);
266 {
2b7363ec 267 size_t z;
0f113f3e
MC
268 for (z = 0; z < i; z++)
269 printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
270 }
58964a49 271#endif
0f113f3e
MC
272
273 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
eadf70d2 274 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
348240c6
MC
275 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
276 iv)) {
eadf70d2
MC
277 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
278 goto err2;
279 }
e75c5a79 280 } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
3d3701ea 281 int taglen;
a230b26e
EK
282 if (s->s3->tmp.
283 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
3d3701ea
DSH
284 taglen = 8;
285 else
286 taglen = 16;
e75c5a79
DSH
287 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
288 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
289 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
348240c6 290 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
e75c5a79
DSH
291 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
292 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
293 goto err2;
294 }
eadf70d2
MC
295 } else {
296 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
297 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
298 goto err2;
299 }
300 }
0f113f3e 301 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
eadf70d2
MC
302 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
303 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
348240c6 304 (int)*mac_secret_size, mac_secret)) {
eadf70d2
MC
305 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
306 goto err2;
307 }
1cf218bc 308#ifdef OPENSSL_SSL_TRACE_CRYPTO
0f113f3e
MC
309 if (s->msg_callback) {
310 int wh = which & SSL3_CC_WRITE ? TLS1_RT_CRYPTO_WRITE : 0;
311 if (*mac_secret_size)
312 s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
313 mac_secret, *mac_secret_size,
314 s, s->msg_callback_arg);
315 if (c->key_len)
316 s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
317 key, c->key_len, s, s->msg_callback_arg);
318 if (k) {
319 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
320 wh |= TLS1_RT_CRYPTO_FIXED_IV;
321 else
322 wh |= TLS1_RT_CRYPTO_IV;
323 s->msg_callback(2, s->version, wh, iv, k, s, s->msg_callback_arg);
324 }
325 }
1cf218bc
DSH
326#endif
327
d63a5e5e 328#ifdef SSL_DEBUG
0f113f3e
MC
329 printf("which = %04X\nkey=", which);
330 {
331 int z;
332 for (z = 0; z < EVP_CIPHER_key_length(c); z++)
333 printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
334 }
335 printf("\niv=");
336 {
2b7363ec 337 size_t z;
0f113f3e
MC
338 for (z = 0; z < k; z++)
339 printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
340 }
341 printf("\n");
58964a49
RE
342#endif
343
0f113f3e
MC
344 OPENSSL_cleanse(tmp1, sizeof(tmp1));
345 OPENSSL_cleanse(tmp2, sizeof(tmp1));
346 OPENSSL_cleanse(iv1, sizeof(iv1));
347 OPENSSL_cleanse(iv2, sizeof(iv2));
348 return (1);
349 err:
350 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
351 err2:
e0f9bf1d
RS
352 OPENSSL_cleanse(tmp1, sizeof(tmp1));
353 OPENSSL_cleanse(tmp2, sizeof(tmp1));
354 OPENSSL_cleanse(iv1, sizeof(iv1));
355 OPENSSL_cleanse(iv2, sizeof(iv2));
0f113f3e
MC
356 return (0);
357}
58964a49 358
6b691a5c 359int tls1_setup_key_block(SSL *s)
0f113f3e 360{
b7d60e76 361 unsigned char *p;
0f113f3e
MC
362 const EVP_CIPHER *c;
363 const EVP_MD *hash;
0f113f3e 364 SSL_COMP *comp;
8c1a5343
MC
365 int mac_type = NID_undef;
366 size_t num, mac_secret_size = 0;
0f113f3e 367 int ret = 0;
58964a49 368
0f113f3e
MC
369 if (s->s3->tmp.key_block_length != 0)
370 return (1);
371
372 if (!ssl_cipher_get_evp
373 (s->session, &c, &hash, &mac_type, &mac_secret_size, &comp,
374 SSL_USE_ETM(s))) {
375 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
376 return (0);
377 }
378
379 s->s3->tmp.new_sym_enc = c;
380 s->s3->tmp.new_hash = hash;
381 s->s3->tmp.new_mac_pkey_type = mac_type;
382 s->s3->tmp.new_mac_secret_size = mac_secret_size;
a230b26e 383 num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
0f113f3e
MC
384 num *= 2;
385
386 ssl3_cleanup_key_block(s);
387
b7d60e76 388 if ((p = OPENSSL_malloc(num)) == NULL) {
0f113f3e
MC
389 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
390 goto err;
391 }
392
393 s->s3->tmp.key_block_length = num;
b7d60e76 394 s->s3->tmp.key_block = p;
0f113f3e 395
d63a5e5e 396#ifdef SSL_DEBUG
0f113f3e
MC
397 printf("client random\n");
398 {
399 int z;
400 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
401 printf("%02X%c", s->s3->client_random[z],
402 ((z + 1) % 16) ? ' ' : '\n');
403 }
404 printf("server random\n");
405 {
406 int z;
407 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
408 printf("%02X%c", s->s3->server_random[z],
409 ((z + 1) % 16) ? ' ' : '\n');
410 }
411 printf("master key\n");
412 {
8c1a5343 413 size_t z;
0f113f3e
MC
414 for (z = 0; z < s->session->master_key_length; z++)
415 printf("%02X%c", s->session->master_key[z],
416 ((z + 1) % 16) ? ' ' : '\n');
417 }
58964a49 418#endif
b7d60e76 419 if (!tls1_generate_key_block(s, p, num))
0f113f3e 420 goto err;
d63a5e5e 421#ifdef SSL_DEBUG
0f113f3e
MC
422 printf("\nkey block\n");
423 {
8c1a5343 424 size_t z;
0f113f3e 425 for (z = 0; z < num; z++)
d1776fde 426 printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
0f113f3e 427 }
58964a49
RE
428#endif
429
0f113f3e
MC
430 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
431 && s->method->version <= TLS1_VERSION) {
432 /*
433 * enable vulnerability countermeasure for CBC ciphers with known-IV
434 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
435 */
436 s->s3->need_empty_fragments = 1;
437
438 if (s->session->cipher != NULL) {
439 if (s->session->cipher->algorithm_enc == SSL_eNULL)
440 s->s3->need_empty_fragments = 0;
441
d1d0be3c 442#ifndef OPENSSL_NO_RC4
0f113f3e
MC
443 if (s->session->cipher->algorithm_enc == SSL_RC4)
444 s->s3->need_empty_fragments = 0;
82b0bf0b 445#endif
0f113f3e
MC
446 }
447 }
448
449 ret = 1;
450 err:
0f113f3e
MC
451 return (ret);
452}
58964a49 453
6db6bc5a
MC
454size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
455 unsigned char *out)
0f113f3e 456{
8c1a5343 457 size_t hashlen;
28ba2541 458 unsigned char hash[EVP_MAX_MD_SIZE];
0f113f3e 459
124037fd
DSH
460 if (!ssl3_digest_cached_records(s, 0))
461 return 0;
0f113f3e 462
8c1a5343 463 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
48fbcbac 464 return 0;
0f113f3e 465
b7d60e76 466 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
0f113f3e 467 s->session->master_key, s->session->master_key_length,
b7d60e76 468 out, TLS1_FINISH_MAC_LENGTH))
0f113f3e 469 return 0;
c9dd49a7 470 OPENSSL_cleanse(hash, hashlen);
b7d60e76 471 return TLS1_FINISH_MAC_LENGTH;
0f113f3e 472}
58964a49 473
6b691a5c 474int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
8c1a5343 475 size_t len, size_t *secret_size)
0f113f3e 476{
0f1e51ea
MC
477 /*
478 * TODO(TLS1.3): We haven't implemented TLS1.3 key derivation yet. For now
479 * we will just force no use of EMS (which adds complications around the
395cc5cd 480 * handshake hash). This will need to be removed later
0f1e51ea
MC
481 */
482 if ((s->session->flags & SSL_SESS_FLAG_EXTMS)
82c9c030 483 && !SSL_IS_TLS13(s)) {
0cfb0e75 484 unsigned char hash[EVP_MAX_MD_SIZE * 2];
8c1a5343 485 size_t hashlen;
a230b26e
EK
486 /*
487 * Digest cached records keeping record buffer (if present): this wont
488 * affect client auth because we're freezing the buffer at the same
489 * point (after client key exchange and before certificate verify)
124037fd
DSH
490 */
491 if (!ssl3_digest_cached_records(s, 1))
8c1a5343
MC
492 return 0;
493 if(!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
494 return 0;
0cfb0e75
DSH
495#ifdef SSL_DEBUG
496 fprintf(stderr, "Handshake hashes:\n");
497 BIO_dump_fp(stderr, (char *)hash, hashlen);
498#endif
28ba2541 499 tls1_PRF(s,
0cfb0e75
DSH
500 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
501 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
502 hash, hashlen,
0cfb0e75 503 NULL, 0,
3d47c1d3 504 NULL, 0,
b7d60e76
DSH
505 NULL, 0, p, len, s->session->master_key,
506 SSL3_MASTER_SECRET_SIZE);
0cfb0e75
DSH
507 OPENSSL_cleanse(hash, hashlen);
508 } else {
28ba2541 509 tls1_PRF(s,
0cfb0e75
DSH
510 TLS_MD_MASTER_SECRET_CONST,
511 TLS_MD_MASTER_SECRET_CONST_SIZE,
512 s->s3->client_random, SSL3_RANDOM_SIZE,
3d47c1d3 513 NULL, 0,
0cfb0e75 514 s->s3->server_random, SSL3_RANDOM_SIZE,
b7d60e76
DSH
515 NULL, 0, p, len, s->session->master_key,
516 SSL3_MASTER_SECRET_SIZE);
0cfb0e75 517 }
a2f9200f 518#ifdef SSL_DEBUG
0f113f3e
MC
519 fprintf(stderr, "Premaster Secret:\n");
520 BIO_dump_fp(stderr, (char *)p, len);
521 fprintf(stderr, "Client Random:\n");
522 BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
523 fprintf(stderr, "Server Random:\n");
524 BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
525 fprintf(stderr, "Master Secret:\n");
526 BIO_dump_fp(stderr, (char *)s->session->master_key,
527 SSL3_MASTER_SECRET_SIZE);
a2f9200f 528#endif
761772d7 529
1cf218bc 530#ifdef OPENSSL_SSL_TRACE_CRYPTO
0f113f3e
MC
531 if (s->msg_callback) {
532 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
533 p, len, s, s->msg_callback_arg);
534 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
535 s->s3->client_random, SSL3_RANDOM_SIZE,
536 s, s->msg_callback_arg);
537 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
538 s->s3->server_random, SSL3_RANDOM_SIZE,
539 s, s->msg_callback_arg);
540 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
541 s->session->master_key,
542 SSL3_MASTER_SECRET_SIZE, s, s->msg_callback_arg);
543 }
1cf218bc
DSH
544#endif
545
8c1a5343
MC
546 *secret_size = SSL3_MASTER_SECRET_SIZE;
547 return 1;
0f113f3e 548}
58964a49 549
74b4b494 550int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
0f113f3e
MC
551 const char *label, size_t llen,
552 const unsigned char *context,
553 size_t contextlen, int use_context)
554{
0f113f3e 555 unsigned char *val = NULL;
1c8a527c 556 size_t vallen = 0, currentvalpos;
0f113f3e 557 int rv;
e0af0405 558
0f113f3e
MC
559 /*
560 * construct PRF arguments we construct the PRF argument ourself rather
561 * than passing separate values into the TLS PRF to ensure that the
562 * concatenation of values does not create a prohibited label.
563 */
564 vallen = llen + SSL3_RANDOM_SIZE * 2;
565 if (use_context) {
566 vallen += 2 + contextlen;
567 }
568
569 val = OPENSSL_malloc(vallen);
570 if (val == NULL)
571 goto err2;
572 currentvalpos = 0;
573 memcpy(val + currentvalpos, (unsigned char *)label, llen);
574 currentvalpos += llen;
575 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
576 currentvalpos += SSL3_RANDOM_SIZE;
577 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
578 currentvalpos += SSL3_RANDOM_SIZE;
579
580 if (use_context) {
581 val[currentvalpos] = (contextlen >> 8) & 0xff;
582 currentvalpos++;
583 val[currentvalpos] = contextlen & 0xff;
584 currentvalpos++;
585 if ((contextlen > 0) || (context != NULL)) {
586 memcpy(val + currentvalpos, context, contextlen);
587 }
588 }
589
590 /*
591 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
592 * label len) = 15, so size of val > max(prohibited label len) = 15 and
593 * the comparisons won't have buffer overflow
594 */
595 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
596 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
597 goto err1;
598 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
599 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
600 goto err1;
601 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
602 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
603 goto err1;
0cfb0e75
DSH
604 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
605 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
606 goto err1;
0f113f3e
MC
607 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
608 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
609 goto err1;
610
28ba2541 611 rv = tls1_PRF(s,
0f113f3e
MC
612 val, vallen,
613 NULL, 0,
614 NULL, 0,
615 NULL, 0,
616 NULL, 0,
617 s->session->master_key, s->session->master_key_length,
b7d60e76 618 out, olen);
e0af0405 619
0f113f3e
MC
620 goto ret;
621 err1:
a230b26e 622 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
0f113f3e
MC
623 rv = 0;
624 goto ret;
625 err2:
626 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
627 rv = 0;
628 ret:
05c7b163 629 OPENSSL_clear_free(val, vallen);
0f113f3e
MC
630 return (rv);
631}
e0af0405 632
6b691a5c 633int tls1_alert_code(int code)
0f113f3e
MC
634{
635 switch (code) {
636 case SSL_AD_CLOSE_NOTIFY:
637 return (SSL3_AD_CLOSE_NOTIFY);
638 case SSL_AD_UNEXPECTED_MESSAGE:
639 return (SSL3_AD_UNEXPECTED_MESSAGE);
640 case SSL_AD_BAD_RECORD_MAC:
641 return (SSL3_AD_BAD_RECORD_MAC);
642 case SSL_AD_DECRYPTION_FAILED:
643 return (TLS1_AD_DECRYPTION_FAILED);
644 case SSL_AD_RECORD_OVERFLOW:
645 return (TLS1_AD_RECORD_OVERFLOW);
646 case SSL_AD_DECOMPRESSION_FAILURE:
647 return (SSL3_AD_DECOMPRESSION_FAILURE);
648 case SSL_AD_HANDSHAKE_FAILURE:
649 return (SSL3_AD_HANDSHAKE_FAILURE);
650 case SSL_AD_NO_CERTIFICATE:
651 return (-1);
652 case SSL_AD_BAD_CERTIFICATE:
653 return (SSL3_AD_BAD_CERTIFICATE);
654 case SSL_AD_UNSUPPORTED_CERTIFICATE:
655 return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
656 case SSL_AD_CERTIFICATE_REVOKED:
657 return (SSL3_AD_CERTIFICATE_REVOKED);
658 case SSL_AD_CERTIFICATE_EXPIRED:
659 return (SSL3_AD_CERTIFICATE_EXPIRED);
660 case SSL_AD_CERTIFICATE_UNKNOWN:
661 return (SSL3_AD_CERTIFICATE_UNKNOWN);
662 case SSL_AD_ILLEGAL_PARAMETER:
663 return (SSL3_AD_ILLEGAL_PARAMETER);
664 case SSL_AD_UNKNOWN_CA:
665 return (TLS1_AD_UNKNOWN_CA);
666 case SSL_AD_ACCESS_DENIED:
667 return (TLS1_AD_ACCESS_DENIED);
668 case SSL_AD_DECODE_ERROR:
669 return (TLS1_AD_DECODE_ERROR);
670 case SSL_AD_DECRYPT_ERROR:
671 return (TLS1_AD_DECRYPT_ERROR);
672 case SSL_AD_EXPORT_RESTRICTION:
673 return (TLS1_AD_EXPORT_RESTRICTION);
674 case SSL_AD_PROTOCOL_VERSION:
675 return (TLS1_AD_PROTOCOL_VERSION);
676 case SSL_AD_INSUFFICIENT_SECURITY:
677 return (TLS1_AD_INSUFFICIENT_SECURITY);
678 case SSL_AD_INTERNAL_ERROR:
679 return (TLS1_AD_INTERNAL_ERROR);
680 case SSL_AD_USER_CANCELLED:
681 return (TLS1_AD_USER_CANCELLED);
682 case SSL_AD_NO_RENEGOTIATION:
683 return (TLS1_AD_NO_RENEGOTIATION);
684 case SSL_AD_UNSUPPORTED_EXTENSION:
685 return (TLS1_AD_UNSUPPORTED_EXTENSION);
686 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
687 return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
688 case SSL_AD_UNRECOGNIZED_NAME:
689 return (TLS1_AD_UNRECOGNIZED_NAME);
690 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
691 return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
692 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
693 return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
694 case SSL_AD_UNKNOWN_PSK_IDENTITY:
695 return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
696 case SSL_AD_INAPPROPRIATE_FALLBACK:
697 return (TLS1_AD_INAPPROPRIATE_FALLBACK);
06217867
EK
698 case SSL_AD_NO_APPLICATION_PROTOCOL:
699 return (TLS1_AD_NO_APPLICATION_PROTOCOL);
0f113f3e
MC
700 default:
701 return (-1);
702 }
703}