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