]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/t1_enc.c
Validate the ticket age for resumed sessions
[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 131 if (which & SSL3_CC_READ) {
28a31a0a
MC
132 if (s->ext.use_etm)
133 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
134 else
135 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
136
0f113f3e
MC
137 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
138 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
139 else
140 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
141
142 if (s->enc_read_ctx != NULL)
143 reuse_dd = 1;
846ec07d 144 else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
0f113f3e
MC
145 goto err;
146 else
147 /*
f430ba31 148 * make sure it's initialised in case we exit later with an error
0f113f3e 149 */
846ec07d 150 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
0f113f3e
MC
151 dd = s->enc_read_ctx;
152 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
5f3d93e4
MC
153 if (mac_ctx == NULL)
154 goto err;
09b6c2ef 155#ifndef OPENSSL_NO_COMP
efa7dd64
RS
156 COMP_CTX_free(s->expand);
157 s->expand = NULL;
0f113f3e
MC
158 if (comp != NULL) {
159 s->expand = COMP_CTX_new(comp->method);
160 if (s->expand == NULL) {
161 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
162 SSL_R_COMPRESSION_LIBRARY_ERROR);
163 goto err2;
164 }
0f113f3e 165 }
09b6c2ef 166#endif
0f113f3e 167 /*
d5d0a1cb 168 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 169 */
d5d0a1cb 170 if (!SSL_IS_DTLS(s))
de07f311 171 RECORD_LAYER_reset_read_sequence(&s->rlayer);
0f113f3e
MC
172 mac_secret = &(s->s3->read_mac_secret[0]);
173 mac_secret_size = &(s->s3->read_mac_secret_size);
174 } else {
28a31a0a
MC
175 if (s->ext.use_etm)
176 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
177 else
178 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
179
0f113f3e
MC
180 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
181 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
182 else
183 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
184 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s))
185 reuse_dd = 1;
186 else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
187 goto err;
188 dd = s->enc_write_ctx;
189 if (SSL_IS_DTLS(s)) {
bfb0641f 190 mac_ctx = EVP_MD_CTX_new();
5f3d93e4 191 if (mac_ctx == NULL)
0f113f3e
MC
192 goto err;
193 s->write_hash = mac_ctx;
5f3d93e4 194 } else {
0f113f3e 195 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
5f3d93e4
MC
196 if (mac_ctx == NULL)
197 goto err;
198 }
09b6c2ef 199#ifndef OPENSSL_NO_COMP
efa7dd64
RS
200 COMP_CTX_free(s->compress);
201 s->compress = NULL;
0f113f3e
MC
202 if (comp != NULL) {
203 s->compress = COMP_CTX_new(comp->method);
204 if (s->compress == NULL) {
205 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
206 SSL_R_COMPRESSION_LIBRARY_ERROR);
207 goto err2;
208 }
209 }
09b6c2ef 210#endif
0f113f3e 211 /*
d5d0a1cb 212 * this is done by dtls1_reset_seq_numbers for DTLS
0f113f3e 213 */
d5d0a1cb 214 if (!SSL_IS_DTLS(s))
de07f311 215 RECORD_LAYER_reset_write_sequence(&s->rlayer);
0f113f3e
MC
216 mac_secret = &(s->s3->write_mac_secret[0]);
217 mac_secret_size = &(s->s3->write_mac_secret_size);
218 }
219
220 if (reuse_dd)
846ec07d 221 EVP_CIPHER_CTX_reset(dd);
0f113f3e
MC
222
223 p = s->s3->tmp.key_block;
224 i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
225
b43d1cbb 226 /* TODO(size_t): convert me */
0f113f3e 227 cl = EVP_CIPHER_key_length(c);
361a1191 228 j = cl;
0f113f3e 229 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
e75c5a79 230 /* If GCM/CCM mode only part of IV comes from PRF */
0f113f3e
MC
231 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
232 k = EVP_GCM_TLS_FIXED_IV_LEN;
e75c5a79
DSH
233 else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
234 k = EVP_CCM_TLS_FIXED_IV_LEN;
0f113f3e
MC
235 else
236 k = EVP_CIPHER_iv_length(c);
237 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
238 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
239 ms = &(p[0]);
240 n = i + i;
241 key = &(p[n]);
242 n += j + j;
243 iv = &(p[n]);
244 n += k + k;
0f113f3e
MC
245 } else {
246 n = i;
247 ms = &(p[n]);
248 n += i + j;
249 key = &(p[n]);
250 n += j + k;
251 iv = &(p[n]);
252 n += k;
0f113f3e
MC
253 }
254
255 if (n > s->s3->tmp.key_block_length) {
256 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
257 goto err2;
258 }
259
260 memcpy(mac_secret, ms, i);
261
262 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
348240c6 263 /* TODO(size_t): Convert this function */
0f113f3e 264 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
348240c6 265 mac_secret, (int)*mac_secret_size);
5f3d93e4 266 if (mac_key == NULL
a230b26e 267 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
5f3d93e4
MC
268 EVP_PKEY_free(mac_key);
269 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
270 goto err2;
271 }
0f113f3e
MC
272 EVP_PKEY_free(mac_key);
273 }
d63a5e5e 274#ifdef SSL_DEBUG
0f113f3e
MC
275 printf("which = %04X\nmac key=", which);
276 {
2b7363ec 277 size_t z;
0f113f3e
MC
278 for (z = 0; z < i; z++)
279 printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
280 }
58964a49 281#endif
0f113f3e
MC
282
283 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
eadf70d2 284 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
348240c6
MC
285 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
286 iv)) {
eadf70d2
MC
287 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
288 goto err2;
289 }
e75c5a79 290 } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
3d3701ea 291 int taglen;
a230b26e
EK
292 if (s->s3->tmp.
293 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
ec07b1d8 294 taglen = EVP_CCM8_TLS_TAG_LEN;
3d3701ea 295 else
ec07b1d8 296 taglen = EVP_CCM_TLS_TAG_LEN;
e75c5a79
DSH
297 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
298 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
299 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
348240c6 300 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
e75c5a79
DSH
301 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
302 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
303 goto err2;
304 }
eadf70d2
MC
305 } else {
306 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
307 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
308 goto err2;
309 }
310 }
0f113f3e 311 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
eadf70d2
MC
312 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
313 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
348240c6 314 (int)*mac_secret_size, mac_secret)) {
eadf70d2
MC
315 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
316 goto err2;
317 }
1cf218bc 318#ifdef OPENSSL_SSL_TRACE_CRYPTO
0f113f3e
MC
319 if (s->msg_callback) {
320 int wh = which & SSL3_CC_WRITE ? TLS1_RT_CRYPTO_WRITE : 0;
321 if (*mac_secret_size)
322 s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
323 mac_secret, *mac_secret_size,
324 s, s->msg_callback_arg);
325 if (c->key_len)
326 s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
327 key, c->key_len, s, s->msg_callback_arg);
328 if (k) {
329 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
330 wh |= TLS1_RT_CRYPTO_FIXED_IV;
331 else
332 wh |= TLS1_RT_CRYPTO_IV;
333 s->msg_callback(2, s->version, wh, iv, k, s, s->msg_callback_arg);
334 }
335 }
1cf218bc
DSH
336#endif
337
d63a5e5e 338#ifdef SSL_DEBUG
0f113f3e
MC
339 printf("which = %04X\nkey=", which);
340 {
341 int z;
342 for (z = 0; z < EVP_CIPHER_key_length(c); z++)
343 printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
344 }
345 printf("\niv=");
346 {
2b7363ec 347 size_t z;
0f113f3e
MC
348 for (z = 0; z < k; z++)
349 printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
350 }
351 printf("\n");
58964a49
RE
352#endif
353
0f113f3e
MC
354 OPENSSL_cleanse(tmp1, sizeof(tmp1));
355 OPENSSL_cleanse(tmp2, sizeof(tmp1));
356 OPENSSL_cleanse(iv1, sizeof(iv1));
357 OPENSSL_cleanse(iv2, sizeof(iv2));
358 return (1);
359 err:
360 SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
361 err2:
e0f9bf1d
RS
362 OPENSSL_cleanse(tmp1, sizeof(tmp1));
363 OPENSSL_cleanse(tmp2, sizeof(tmp1));
364 OPENSSL_cleanse(iv1, sizeof(iv1));
365 OPENSSL_cleanse(iv2, sizeof(iv2));
0f113f3e
MC
366 return (0);
367}
58964a49 368
6b691a5c 369int tls1_setup_key_block(SSL *s)
0f113f3e 370{
b7d60e76 371 unsigned char *p;
0f113f3e
MC
372 const EVP_CIPHER *c;
373 const EVP_MD *hash;
0f113f3e 374 SSL_COMP *comp;
8c1a5343
MC
375 int mac_type = NID_undef;
376 size_t num, mac_secret_size = 0;
0f113f3e 377 int ret = 0;
58964a49 378
0f113f3e
MC
379 if (s->s3->tmp.key_block_length != 0)
380 return (1);
381
28a31a0a
MC
382 if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
383 &comp, s->ext.use_etm)) {
0f113f3e
MC
384 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
385 return (0);
386 }
387
388 s->s3->tmp.new_sym_enc = c;
389 s->s3->tmp.new_hash = hash;
390 s->s3->tmp.new_mac_pkey_type = mac_type;
391 s->s3->tmp.new_mac_secret_size = mac_secret_size;
a230b26e 392 num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
0f113f3e
MC
393 num *= 2;
394
395 ssl3_cleanup_key_block(s);
396
b7d60e76 397 if ((p = OPENSSL_malloc(num)) == NULL) {
0f113f3e
MC
398 SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
399 goto err;
400 }
401
402 s->s3->tmp.key_block_length = num;
b7d60e76 403 s->s3->tmp.key_block = p;
0f113f3e 404
d63a5e5e 405#ifdef SSL_DEBUG
0f113f3e
MC
406 printf("client random\n");
407 {
408 int z;
409 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
410 printf("%02X%c", s->s3->client_random[z],
411 ((z + 1) % 16) ? ' ' : '\n');
412 }
413 printf("server random\n");
414 {
415 int z;
416 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
417 printf("%02X%c", s->s3->server_random[z],
418 ((z + 1) % 16) ? ' ' : '\n');
419 }
420 printf("master key\n");
421 {
8c1a5343 422 size_t z;
0f113f3e
MC
423 for (z = 0; z < s->session->master_key_length; z++)
424 printf("%02X%c", s->session->master_key[z],
425 ((z + 1) % 16) ? ' ' : '\n');
426 }
58964a49 427#endif
b7d60e76 428 if (!tls1_generate_key_block(s, p, num))
0f113f3e 429 goto err;
d63a5e5e 430#ifdef SSL_DEBUG
0f113f3e
MC
431 printf("\nkey block\n");
432 {
8c1a5343 433 size_t z;
0f113f3e 434 for (z = 0; z < num; z++)
d1776fde 435 printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
0f113f3e 436 }
58964a49
RE
437#endif
438
0f113f3e
MC
439 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
440 && s->method->version <= TLS1_VERSION) {
441 /*
442 * enable vulnerability countermeasure for CBC ciphers with known-IV
443 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
444 */
445 s->s3->need_empty_fragments = 1;
446
447 if (s->session->cipher != NULL) {
448 if (s->session->cipher->algorithm_enc == SSL_eNULL)
449 s->s3->need_empty_fragments = 0;
450
d1d0be3c 451#ifndef OPENSSL_NO_RC4
0f113f3e
MC
452 if (s->session->cipher->algorithm_enc == SSL_RC4)
453 s->s3->need_empty_fragments = 0;
82b0bf0b 454#endif
0f113f3e
MC
455 }
456 }
457
458 ret = 1;
459 err:
0f113f3e
MC
460 return (ret);
461}
58964a49 462
6db6bc5a
MC
463size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
464 unsigned char *out)
0f113f3e 465{
8c1a5343 466 size_t hashlen;
28ba2541 467 unsigned char hash[EVP_MAX_MD_SIZE];
0f113f3e 468
124037fd
DSH
469 if (!ssl3_digest_cached_records(s, 0))
470 return 0;
0f113f3e 471
8c1a5343 472 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
48fbcbac 473 return 0;
0f113f3e 474
b7d60e76 475 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
0f113f3e 476 s->session->master_key, s->session->master_key_length,
b7d60e76 477 out, TLS1_FINISH_MAC_LENGTH))
0f113f3e 478 return 0;
c9dd49a7 479 OPENSSL_cleanse(hash, hashlen);
b7d60e76 480 return TLS1_FINISH_MAC_LENGTH;
0f113f3e 481}
58964a49 482
6b691a5c 483int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
8c1a5343 484 size_t len, size_t *secret_size)
0f113f3e 485{
0f1e51ea
MC
486 /*
487 * TODO(TLS1.3): We haven't implemented TLS1.3 key derivation yet. For now
488 * we will just force no use of EMS (which adds complications around the
395cc5cd 489 * handshake hash). This will need to be removed later
0f1e51ea
MC
490 */
491 if ((s->session->flags & SSL_SESS_FLAG_EXTMS)
82c9c030 492 && !SSL_IS_TLS13(s)) {
0cfb0e75 493 unsigned char hash[EVP_MAX_MD_SIZE * 2];
8c1a5343 494 size_t hashlen;
a230b26e
EK
495 /*
496 * Digest cached records keeping record buffer (if present): this wont
497 * affect client auth because we're freezing the buffer at the same
498 * point (after client key exchange and before certificate verify)
124037fd
DSH
499 */
500 if (!ssl3_digest_cached_records(s, 1))
8c1a5343
MC
501 return 0;
502 if(!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
503 return 0;
0cfb0e75
DSH
504#ifdef SSL_DEBUG
505 fprintf(stderr, "Handshake hashes:\n");
506 BIO_dump_fp(stderr, (char *)hash, hashlen);
507#endif
28ba2541 508 tls1_PRF(s,
0cfb0e75
DSH
509 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
510 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
511 hash, hashlen,
0cfb0e75 512 NULL, 0,
3d47c1d3 513 NULL, 0,
b7d60e76
DSH
514 NULL, 0, p, len, s->session->master_key,
515 SSL3_MASTER_SECRET_SIZE);
0cfb0e75
DSH
516 OPENSSL_cleanse(hash, hashlen);
517 } else {
28ba2541 518 tls1_PRF(s,
0cfb0e75
DSH
519 TLS_MD_MASTER_SECRET_CONST,
520 TLS_MD_MASTER_SECRET_CONST_SIZE,
521 s->s3->client_random, SSL3_RANDOM_SIZE,
3d47c1d3 522 NULL, 0,
0cfb0e75 523 s->s3->server_random, SSL3_RANDOM_SIZE,
b7d60e76
DSH
524 NULL, 0, p, len, s->session->master_key,
525 SSL3_MASTER_SECRET_SIZE);
0cfb0e75 526 }
a2f9200f 527#ifdef SSL_DEBUG
0f113f3e
MC
528 fprintf(stderr, "Premaster Secret:\n");
529 BIO_dump_fp(stderr, (char *)p, len);
530 fprintf(stderr, "Client Random:\n");
531 BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
532 fprintf(stderr, "Server Random:\n");
533 BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
534 fprintf(stderr, "Master Secret:\n");
535 BIO_dump_fp(stderr, (char *)s->session->master_key,
536 SSL3_MASTER_SECRET_SIZE);
a2f9200f 537#endif
761772d7 538
1cf218bc 539#ifdef OPENSSL_SSL_TRACE_CRYPTO
0f113f3e
MC
540 if (s->msg_callback) {
541 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
542 p, len, s, s->msg_callback_arg);
543 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
544 s->s3->client_random, SSL3_RANDOM_SIZE,
545 s, s->msg_callback_arg);
546 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
547 s->s3->server_random, SSL3_RANDOM_SIZE,
548 s, s->msg_callback_arg);
549 s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
550 s->session->master_key,
551 SSL3_MASTER_SECRET_SIZE, s, s->msg_callback_arg);
552 }
1cf218bc
DSH
553#endif
554
8c1a5343
MC
555 *secret_size = SSL3_MASTER_SECRET_SIZE;
556 return 1;
0f113f3e 557}
58964a49 558
74b4b494 559int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
0f113f3e
MC
560 const char *label, size_t llen,
561 const unsigned char *context,
562 size_t contextlen, int use_context)
563{
0f113f3e 564 unsigned char *val = NULL;
1c8a527c 565 size_t vallen = 0, currentvalpos;
0f113f3e 566 int rv;
e0af0405 567
0f113f3e
MC
568 /*
569 * construct PRF arguments we construct the PRF argument ourself rather
570 * than passing separate values into the TLS PRF to ensure that the
571 * concatenation of values does not create a prohibited label.
572 */
573 vallen = llen + SSL3_RANDOM_SIZE * 2;
574 if (use_context) {
575 vallen += 2 + contextlen;
576 }
577
578 val = OPENSSL_malloc(vallen);
579 if (val == NULL)
580 goto err2;
581 currentvalpos = 0;
582 memcpy(val + currentvalpos, (unsigned char *)label, llen);
583 currentvalpos += llen;
584 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
585 currentvalpos += SSL3_RANDOM_SIZE;
586 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
587 currentvalpos += SSL3_RANDOM_SIZE;
588
589 if (use_context) {
590 val[currentvalpos] = (contextlen >> 8) & 0xff;
591 currentvalpos++;
592 val[currentvalpos] = contextlen & 0xff;
593 currentvalpos++;
594 if ((contextlen > 0) || (context != NULL)) {
595 memcpy(val + currentvalpos, context, contextlen);
596 }
597 }
598
599 /*
600 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
601 * label len) = 15, so size of val > max(prohibited label len) = 15 and
602 * the comparisons won't have buffer overflow
603 */
604 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
605 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
606 goto err1;
607 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
608 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
609 goto err1;
610 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
611 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
612 goto err1;
0cfb0e75
DSH
613 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
614 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
615 goto err1;
0f113f3e
MC
616 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
617 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
618 goto err1;
619
28ba2541 620 rv = tls1_PRF(s,
0f113f3e
MC
621 val, vallen,
622 NULL, 0,
623 NULL, 0,
624 NULL, 0,
625 NULL, 0,
626 s->session->master_key, s->session->master_key_length,
b7d60e76 627 out, olen);
e0af0405 628
0f113f3e
MC
629 goto ret;
630 err1:
a230b26e 631 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
0f113f3e
MC
632 rv = 0;
633 goto ret;
634 err2:
635 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
636 rv = 0;
637 ret:
05c7b163 638 OPENSSL_clear_free(val, vallen);
0f113f3e
MC
639 return (rv);
640}
e0af0405 641
6b691a5c 642int tls1_alert_code(int code)
0f113f3e
MC
643{
644 switch (code) {
645 case SSL_AD_CLOSE_NOTIFY:
646 return (SSL3_AD_CLOSE_NOTIFY);
647 case SSL_AD_UNEXPECTED_MESSAGE:
648 return (SSL3_AD_UNEXPECTED_MESSAGE);
649 case SSL_AD_BAD_RECORD_MAC:
650 return (SSL3_AD_BAD_RECORD_MAC);
651 case SSL_AD_DECRYPTION_FAILED:
652 return (TLS1_AD_DECRYPTION_FAILED);
653 case SSL_AD_RECORD_OVERFLOW:
654 return (TLS1_AD_RECORD_OVERFLOW);
655 case SSL_AD_DECOMPRESSION_FAILURE:
656 return (SSL3_AD_DECOMPRESSION_FAILURE);
657 case SSL_AD_HANDSHAKE_FAILURE:
658 return (SSL3_AD_HANDSHAKE_FAILURE);
659 case SSL_AD_NO_CERTIFICATE:
660 return (-1);
661 case SSL_AD_BAD_CERTIFICATE:
662 return (SSL3_AD_BAD_CERTIFICATE);
663 case SSL_AD_UNSUPPORTED_CERTIFICATE:
664 return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
665 case SSL_AD_CERTIFICATE_REVOKED:
666 return (SSL3_AD_CERTIFICATE_REVOKED);
667 case SSL_AD_CERTIFICATE_EXPIRED:
668 return (SSL3_AD_CERTIFICATE_EXPIRED);
669 case SSL_AD_CERTIFICATE_UNKNOWN:
670 return (SSL3_AD_CERTIFICATE_UNKNOWN);
671 case SSL_AD_ILLEGAL_PARAMETER:
672 return (SSL3_AD_ILLEGAL_PARAMETER);
673 case SSL_AD_UNKNOWN_CA:
674 return (TLS1_AD_UNKNOWN_CA);
675 case SSL_AD_ACCESS_DENIED:
676 return (TLS1_AD_ACCESS_DENIED);
677 case SSL_AD_DECODE_ERROR:
678 return (TLS1_AD_DECODE_ERROR);
679 case SSL_AD_DECRYPT_ERROR:
680 return (TLS1_AD_DECRYPT_ERROR);
681 case SSL_AD_EXPORT_RESTRICTION:
682 return (TLS1_AD_EXPORT_RESTRICTION);
683 case SSL_AD_PROTOCOL_VERSION:
684 return (TLS1_AD_PROTOCOL_VERSION);
685 case SSL_AD_INSUFFICIENT_SECURITY:
686 return (TLS1_AD_INSUFFICIENT_SECURITY);
687 case SSL_AD_INTERNAL_ERROR:
688 return (TLS1_AD_INTERNAL_ERROR);
689 case SSL_AD_USER_CANCELLED:
690 return (TLS1_AD_USER_CANCELLED);
691 case SSL_AD_NO_RENEGOTIATION:
692 return (TLS1_AD_NO_RENEGOTIATION);
693 case SSL_AD_UNSUPPORTED_EXTENSION:
694 return (TLS1_AD_UNSUPPORTED_EXTENSION);
695 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
696 return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
697 case SSL_AD_UNRECOGNIZED_NAME:
698 return (TLS1_AD_UNRECOGNIZED_NAME);
699 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
700 return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
701 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
702 return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
703 case SSL_AD_UNKNOWN_PSK_IDENTITY:
704 return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
705 case SSL_AD_INAPPROPRIATE_FALLBACK:
706 return (TLS1_AD_INAPPROPRIATE_FALLBACK);
06217867
EK
707 case SSL_AD_NO_APPLICATION_PROTOCOL:
708 return (TLS1_AD_NO_APPLICATION_PROTOCOL);
0f113f3e
MC
709 default:
710 return (-1);
711 }
712}