]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/t1_enc.c
Use (D)TLS_MAX_VERSION_INTERNAL internally
[thirdparty/openssl.git] / ssl / t1_enc.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <stdio.h>
12 #include "ssl_locl.h"
13 #include "record/record_locl.h"
14 #include "internal/ktls.h"
15 #include "internal/cryptlib.h"
16 #include <openssl/comp.h>
17 #include <openssl/evp.h>
18 #include <openssl/kdf.h>
19 #include <openssl/rand.h>
20 #include <openssl/obj_mac.h>
21
22 /* seed1 through seed5 are concatenated */
23 static int tls1_PRF(SSL *s,
24 const void *seed1, size_t seed1_len,
25 const void *seed2, size_t seed2_len,
26 const void *seed3, size_t seed3_len,
27 const void *seed4, size_t seed4_len,
28 const void *seed5, size_t seed5_len,
29 const unsigned char *sec, size_t slen,
30 unsigned char *out, size_t olen, int fatal)
31 {
32 const EVP_MD *md = ssl_prf_md(s);
33 EVP_PKEY_CTX *pctx = NULL;
34 int ret = 0;
35
36 if (md == NULL) {
37 /* Should never happen */
38 if (fatal)
39 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
40 ERR_R_INTERNAL_ERROR);
41 else
42 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
43 return 0;
44 }
45 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
46 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
47 || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
48 || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0
49 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0
50 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0
51 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0
52 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0
53 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0
54 || EVP_PKEY_derive(pctx, out, &olen) <= 0) {
55 if (fatal)
56 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
57 ERR_R_INTERNAL_ERROR);
58 else
59 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
60 goto err;
61 }
62
63 ret = 1;
64
65 err:
66 EVP_PKEY_CTX_free(pctx);
67 return ret;
68 }
69
70 static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
71 {
72 int ret;
73
74 /* Calls SSLfatal() as required */
75 ret = tls1_PRF(s,
76 TLS_MD_KEY_EXPANSION_CONST,
77 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
78 SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
79 NULL, 0, NULL, 0, s->session->master_key,
80 s->session->master_key_length, km, num, 1);
81
82 return ret;
83 }
84
85 int tls1_change_cipher_state(SSL *s, int which)
86 {
87 unsigned char *p, *mac_secret;
88 unsigned char tmp1[EVP_MAX_KEY_LENGTH];
89 unsigned char tmp2[EVP_MAX_KEY_LENGTH];
90 unsigned char iv1[EVP_MAX_IV_LENGTH * 2];
91 unsigned char iv2[EVP_MAX_IV_LENGTH * 2];
92 unsigned char *ms, *key, *iv;
93 EVP_CIPHER_CTX *dd;
94 const EVP_CIPHER *c;
95 #ifndef OPENSSL_NO_COMP
96 const SSL_COMP *comp;
97 #endif
98 const EVP_MD *m;
99 int mac_type;
100 size_t *mac_secret_size;
101 EVP_MD_CTX *mac_ctx;
102 EVP_PKEY *mac_key;
103 size_t n, i, j, k, cl;
104 int reuse_dd = 0;
105 #ifndef OPENSSL_NO_KTLS
106 struct tls12_crypto_info_aes_gcm_128 crypto_info;
107 BIO *wbio;
108 unsigned char geniv[12];
109 #endif
110
111 c = s->s3->tmp.new_sym_enc;
112 m = s->s3->tmp.new_hash;
113 mac_type = s->s3->tmp.new_mac_pkey_type;
114 #ifndef OPENSSL_NO_COMP
115 comp = s->s3->tmp.new_compression;
116 #endif
117
118 if (which & SSL3_CC_READ) {
119 if (s->ext.use_etm)
120 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
121 else
122 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
123
124 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
125 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
126 else
127 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
128
129 if (s->enc_read_ctx != NULL) {
130 reuse_dd = 1;
131 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
132 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
133 ERR_R_MALLOC_FAILURE);
134 goto err;
135 } else {
136 /*
137 * make sure it's initialised in case we exit later with an error
138 */
139 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
140 }
141 dd = s->enc_read_ctx;
142 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
143 if (mac_ctx == NULL) {
144 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
145 ERR_R_INTERNAL_ERROR);
146 goto err;
147 }
148 #ifndef OPENSSL_NO_COMP
149 COMP_CTX_free(s->expand);
150 s->expand = NULL;
151 if (comp != NULL) {
152 s->expand = COMP_CTX_new(comp->method);
153 if (s->expand == NULL) {
154 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
155 SSL_F_TLS1_CHANGE_CIPHER_STATE,
156 SSL_R_COMPRESSION_LIBRARY_ERROR);
157 goto err;
158 }
159 }
160 #endif
161 /*
162 * this is done by dtls1_reset_seq_numbers for DTLS
163 */
164 if (!SSL_IS_DTLS(s))
165 RECORD_LAYER_reset_read_sequence(&s->rlayer);
166 mac_secret = &(s->s3->read_mac_secret[0]);
167 mac_secret_size = &(s->s3->read_mac_secret_size);
168 } else {
169 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
170 if (s->ext.use_etm)
171 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
172 else
173 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
174
175 if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
176 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
177 else
178 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
179 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
180 reuse_dd = 1;
181 } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
182 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
183 ERR_R_MALLOC_FAILURE);
184 goto err;
185 }
186 dd = s->enc_write_ctx;
187 if (SSL_IS_DTLS(s)) {
188 mac_ctx = EVP_MD_CTX_new();
189 if (mac_ctx == NULL) {
190 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
191 SSL_F_TLS1_CHANGE_CIPHER_STATE,
192 ERR_R_MALLOC_FAILURE);
193 goto err;
194 }
195 s->write_hash = mac_ctx;
196 } else {
197 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
198 if (mac_ctx == NULL) {
199 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
200 SSL_F_TLS1_CHANGE_CIPHER_STATE,
201 ERR_R_MALLOC_FAILURE);
202 goto err;
203 }
204 }
205 #ifndef OPENSSL_NO_COMP
206 COMP_CTX_free(s->compress);
207 s->compress = NULL;
208 if (comp != NULL) {
209 s->compress = COMP_CTX_new(comp->method);
210 if (s->compress == NULL) {
211 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
212 SSL_F_TLS1_CHANGE_CIPHER_STATE,
213 SSL_R_COMPRESSION_LIBRARY_ERROR);
214 goto err;
215 }
216 }
217 #endif
218 /*
219 * this is done by dtls1_reset_seq_numbers for DTLS
220 */
221 if (!SSL_IS_DTLS(s))
222 RECORD_LAYER_reset_write_sequence(&s->rlayer);
223 mac_secret = &(s->s3->write_mac_secret[0]);
224 mac_secret_size = &(s->s3->write_mac_secret_size);
225 }
226
227 if (reuse_dd)
228 EVP_CIPHER_CTX_reset(dd);
229
230 p = s->s3->tmp.key_block;
231 i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
232
233 /* TODO(size_t): convert me */
234 cl = EVP_CIPHER_key_length(c);
235 j = cl;
236 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
237 /* If GCM/CCM mode only part of IV comes from PRF */
238 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
239 k = EVP_GCM_TLS_FIXED_IV_LEN;
240 else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
241 k = EVP_CCM_TLS_FIXED_IV_LEN;
242 else
243 k = EVP_CIPHER_iv_length(c);
244 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
245 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
246 ms = &(p[0]);
247 n = i + i;
248 key = &(p[n]);
249 n += j + j;
250 iv = &(p[n]);
251 n += k + k;
252 } else {
253 n = i;
254 ms = &(p[n]);
255 n += i + j;
256 key = &(p[n]);
257 n += j + k;
258 iv = &(p[n]);
259 n += k;
260 }
261
262 if (n > s->s3->tmp.key_block_length) {
263 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
264 ERR_R_INTERNAL_ERROR);
265 goto err;
266 }
267
268 memcpy(mac_secret, ms, i);
269
270 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
271 /* TODO(size_t): Convert this function */
272 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
273 (int)*mac_secret_size);
274 if (mac_key == NULL
275 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
276 EVP_PKEY_free(mac_key);
277 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
278 ERR_R_INTERNAL_ERROR);
279 goto err;
280 }
281 EVP_PKEY_free(mac_key);
282 }
283 #ifdef SSL_DEBUG
284 printf("which = %04X\nmac key=", which);
285 {
286 size_t z;
287 for (z = 0; z < i; z++)
288 printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
289 }
290 #endif
291
292 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
293 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
294 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
295 iv)) {
296 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
297 ERR_R_INTERNAL_ERROR);
298 goto err;
299 }
300 } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
301 int taglen;
302 if (s->s3->tmp.
303 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
304 taglen = EVP_CCM8_TLS_TAG_LEN;
305 else
306 taglen = EVP_CCM_TLS_TAG_LEN;
307 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
308 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
309 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
310 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
311 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
312 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
313 ERR_R_INTERNAL_ERROR);
314 goto err;
315 }
316 } else {
317 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
318 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
319 ERR_R_INTERNAL_ERROR);
320 goto err;
321 }
322 }
323 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
324 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
325 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
326 (int)*mac_secret_size, mac_secret)) {
327 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
328 ERR_R_INTERNAL_ERROR);
329 goto err;
330 }
331 #ifndef OPENSSL_NO_KTLS
332 if (s->compress)
333 goto skip_ktls;
334
335 if ((which & SSL3_CC_READ) ||
336 ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX)))
337 goto skip_ktls;
338
339 /* ktls supports only the maximum fragment size */
340 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
341 goto skip_ktls;
342
343 /* check that cipher is AES_GCM_128 */
344 if (EVP_CIPHER_nid(c) != NID_aes_128_gcm
345 || EVP_CIPHER_mode(c) != EVP_CIPH_GCM_MODE
346 || EVP_CIPHER_key_length(c) != TLS_CIPHER_AES_GCM_128_KEY_SIZE)
347 goto skip_ktls;
348
349 /* check version is 1.2 */
350 if (s->version != TLS1_2_VERSION)
351 goto skip_ktls;
352
353 wbio = s->wbio;
354 if (!ossl_assert(wbio != NULL)) {
355 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
356 ERR_R_INTERNAL_ERROR);
357 goto err;
358 }
359
360 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
361 if (BIO_flush(wbio) <= 0)
362 goto skip_ktls;
363
364 /* ktls doesn't support renegotiation */
365 if (BIO_get_ktls_send(s->wbio)) {
366 SSLfatal(s, SSL_AD_NO_RENEGOTIATION, SSL_F_TLS1_CHANGE_CIPHER_STATE,
367 ERR_R_INTERNAL_ERROR);
368 goto err;
369 }
370
371 memset(&crypto_info, 0, sizeof(crypto_info));
372 crypto_info.info.cipher_type = TLS_CIPHER_AES_GCM_128;
373 crypto_info.info.version = s->version;
374
375 EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GET_IV,
376 EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN,
377 geniv);
378 memcpy(crypto_info.iv, geniv + EVP_GCM_TLS_FIXED_IV_LEN,
379 TLS_CIPHER_AES_GCM_128_IV_SIZE);
380 memcpy(crypto_info.salt, geniv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
381 memcpy(crypto_info.key, key, EVP_CIPHER_key_length(c));
382 memcpy(crypto_info.rec_seq, &s->rlayer.write_sequence,
383 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
384
385 /* ktls works with user provided buffers directly */
386 if (BIO_set_ktls(wbio, &crypto_info, which & SSL3_CC_WRITE)) {
387 ssl3_release_write_buffer(s);
388 SSL_set_options(s, SSL_OP_NO_RENEGOTIATION);
389 }
390
391 skip_ktls:
392 #endif /* OPENSSL_NO_KTLS */
393 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
394
395 #ifdef SSL_DEBUG
396 printf("which = %04X\nkey=", which);
397 {
398 int z;
399 for (z = 0; z < EVP_CIPHER_key_length(c); z++)
400 printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
401 }
402 printf("\niv=");
403 {
404 size_t z;
405 for (z = 0; z < k; z++)
406 printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
407 }
408 printf("\n");
409 #endif
410
411 OPENSSL_cleanse(tmp1, sizeof(tmp1));
412 OPENSSL_cleanse(tmp2, sizeof(tmp1));
413 OPENSSL_cleanse(iv1, sizeof(iv1));
414 OPENSSL_cleanse(iv2, sizeof(iv2));
415 return 1;
416 err:
417 OPENSSL_cleanse(tmp1, sizeof(tmp1));
418 OPENSSL_cleanse(tmp2, sizeof(tmp1));
419 OPENSSL_cleanse(iv1, sizeof(iv1));
420 OPENSSL_cleanse(iv2, sizeof(iv2));
421 return 0;
422 }
423
424 int tls1_setup_key_block(SSL *s)
425 {
426 unsigned char *p;
427 const EVP_CIPHER *c;
428 const EVP_MD *hash;
429 SSL_COMP *comp;
430 int mac_type = NID_undef;
431 size_t num, mac_secret_size = 0;
432 int ret = 0;
433
434 if (s->s3->tmp.key_block_length != 0)
435 return 1;
436
437 if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
438 &comp, s->ext.use_etm)) {
439 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
440 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
441 return 0;
442 }
443
444 s->s3->tmp.new_sym_enc = c;
445 s->s3->tmp.new_hash = hash;
446 s->s3->tmp.new_mac_pkey_type = mac_type;
447 s->s3->tmp.new_mac_secret_size = mac_secret_size;
448 num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
449 num *= 2;
450
451 ssl3_cleanup_key_block(s);
452
453 if ((p = OPENSSL_malloc(num)) == NULL) {
454 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
455 ERR_R_MALLOC_FAILURE);
456 goto err;
457 }
458
459 s->s3->tmp.key_block_length = num;
460 s->s3->tmp.key_block = p;
461
462 #ifdef SSL_DEBUG
463 printf("client random\n");
464 {
465 int z;
466 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
467 printf("%02X%c", s->s3->client_random[z],
468 ((z + 1) % 16) ? ' ' : '\n');
469 }
470 printf("server random\n");
471 {
472 int z;
473 for (z = 0; z < SSL3_RANDOM_SIZE; z++)
474 printf("%02X%c", s->s3->server_random[z],
475 ((z + 1) % 16) ? ' ' : '\n');
476 }
477 printf("master key\n");
478 {
479 size_t z;
480 for (z = 0; z < s->session->master_key_length; z++)
481 printf("%02X%c", s->session->master_key[z],
482 ((z + 1) % 16) ? ' ' : '\n');
483 }
484 #endif
485 if (!tls1_generate_key_block(s, p, num)) {
486 /* SSLfatal() already called */
487 goto err;
488 }
489 #ifdef SSL_DEBUG
490 printf("\nkey block\n");
491 {
492 size_t z;
493 for (z = 0; z < num; z++)
494 printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
495 }
496 #endif
497
498 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
499 && s->method->version <= TLS1_VERSION) {
500 /*
501 * enable vulnerability countermeasure for CBC ciphers with known-IV
502 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
503 */
504 s->s3->need_empty_fragments = 1;
505
506 if (s->session->cipher != NULL) {
507 if (s->session->cipher->algorithm_enc == SSL_eNULL)
508 s->s3->need_empty_fragments = 0;
509
510 #ifndef OPENSSL_NO_RC4
511 if (s->session->cipher->algorithm_enc == SSL_RC4)
512 s->s3->need_empty_fragments = 0;
513 #endif
514 }
515 }
516
517 ret = 1;
518 err:
519 return ret;
520 }
521
522 size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
523 unsigned char *out)
524 {
525 size_t hashlen;
526 unsigned char hash[EVP_MAX_MD_SIZE];
527
528 if (!ssl3_digest_cached_records(s, 0)) {
529 /* SSLfatal() already called */
530 return 0;
531 }
532
533 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
534 /* SSLfatal() already called */
535 return 0;
536 }
537
538 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
539 s->session->master_key, s->session->master_key_length,
540 out, TLS1_FINISH_MAC_LENGTH, 1)) {
541 /* SSLfatal() already called */
542 return 0;
543 }
544 OPENSSL_cleanse(hash, hashlen);
545 return TLS1_FINISH_MAC_LENGTH;
546 }
547
548 int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
549 size_t len, size_t *secret_size)
550 {
551 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
552 unsigned char hash[EVP_MAX_MD_SIZE * 2];
553 size_t hashlen;
554 /*
555 * Digest cached records keeping record buffer (if present): this wont
556 * affect client auth because we're freezing the buffer at the same
557 * point (after client key exchange and before certificate verify)
558 */
559 if (!ssl3_digest_cached_records(s, 1)
560 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
561 /* SSLfatal() already called */
562 return 0;
563 }
564 #ifdef SSL_DEBUG
565 fprintf(stderr, "Handshake hashes:\n");
566 BIO_dump_fp(stderr, (char *)hash, hashlen);
567 #endif
568 if (!tls1_PRF(s,
569 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
570 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
571 hash, hashlen,
572 NULL, 0,
573 NULL, 0,
574 NULL, 0, p, len, out,
575 SSL3_MASTER_SECRET_SIZE, 1)) {
576 /* SSLfatal() already called */
577 return 0;
578 }
579 OPENSSL_cleanse(hash, hashlen);
580 } else {
581 if (!tls1_PRF(s,
582 TLS_MD_MASTER_SECRET_CONST,
583 TLS_MD_MASTER_SECRET_CONST_SIZE,
584 s->s3->client_random, SSL3_RANDOM_SIZE,
585 NULL, 0,
586 s->s3->server_random, SSL3_RANDOM_SIZE,
587 NULL, 0, p, len, out,
588 SSL3_MASTER_SECRET_SIZE, 1)) {
589 /* SSLfatal() already called */
590 return 0;
591 }
592 }
593 #ifdef SSL_DEBUG
594 fprintf(stderr, "Premaster Secret:\n");
595 BIO_dump_fp(stderr, (char *)p, len);
596 fprintf(stderr, "Client Random:\n");
597 BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
598 fprintf(stderr, "Server Random:\n");
599 BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
600 fprintf(stderr, "Master Secret:\n");
601 BIO_dump_fp(stderr, (char *)s->session->master_key,
602 SSL3_MASTER_SECRET_SIZE);
603 #endif
604
605 *secret_size = SSL3_MASTER_SECRET_SIZE;
606 return 1;
607 }
608
609 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
610 const char *label, size_t llen,
611 const unsigned char *context,
612 size_t contextlen, int use_context)
613 {
614 unsigned char *val = NULL;
615 size_t vallen = 0, currentvalpos;
616 int rv;
617
618 /*
619 * construct PRF arguments we construct the PRF argument ourself rather
620 * than passing separate values into the TLS PRF to ensure that the
621 * concatenation of values does not create a prohibited label.
622 */
623 vallen = llen + SSL3_RANDOM_SIZE * 2;
624 if (use_context) {
625 vallen += 2 + contextlen;
626 }
627
628 val = OPENSSL_malloc(vallen);
629 if (val == NULL)
630 goto err2;
631 currentvalpos = 0;
632 memcpy(val + currentvalpos, (unsigned char *)label, llen);
633 currentvalpos += llen;
634 memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
635 currentvalpos += SSL3_RANDOM_SIZE;
636 memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
637 currentvalpos += SSL3_RANDOM_SIZE;
638
639 if (use_context) {
640 val[currentvalpos] = (contextlen >> 8) & 0xff;
641 currentvalpos++;
642 val[currentvalpos] = contextlen & 0xff;
643 currentvalpos++;
644 if ((contextlen > 0) || (context != NULL)) {
645 memcpy(val + currentvalpos, context, contextlen);
646 }
647 }
648
649 /*
650 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
651 * label len) = 15, so size of val > max(prohibited label len) = 15 and
652 * the comparisons won't have buffer overflow
653 */
654 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
655 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
656 goto err1;
657 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
658 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
659 goto err1;
660 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
661 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
662 goto err1;
663 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
664 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
665 goto err1;
666 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
667 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
668 goto err1;
669
670 rv = tls1_PRF(s,
671 val, vallen,
672 NULL, 0,
673 NULL, 0,
674 NULL, 0,
675 NULL, 0,
676 s->session->master_key, s->session->master_key_length,
677 out, olen, 0);
678
679 goto ret;
680 err1:
681 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
682 rv = 0;
683 goto ret;
684 err2:
685 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
686 rv = 0;
687 ret:
688 OPENSSL_clear_free(val, vallen);
689 return rv;
690 }
691
692 int tls1_alert_code(int code)
693 {
694 switch (code) {
695 case SSL_AD_CLOSE_NOTIFY:
696 return SSL3_AD_CLOSE_NOTIFY;
697 case SSL_AD_UNEXPECTED_MESSAGE:
698 return SSL3_AD_UNEXPECTED_MESSAGE;
699 case SSL_AD_BAD_RECORD_MAC:
700 return SSL3_AD_BAD_RECORD_MAC;
701 case SSL_AD_DECRYPTION_FAILED:
702 return TLS1_AD_DECRYPTION_FAILED;
703 case SSL_AD_RECORD_OVERFLOW:
704 return TLS1_AD_RECORD_OVERFLOW;
705 case SSL_AD_DECOMPRESSION_FAILURE:
706 return SSL3_AD_DECOMPRESSION_FAILURE;
707 case SSL_AD_HANDSHAKE_FAILURE:
708 return SSL3_AD_HANDSHAKE_FAILURE;
709 case SSL_AD_NO_CERTIFICATE:
710 return -1;
711 case SSL_AD_BAD_CERTIFICATE:
712 return SSL3_AD_BAD_CERTIFICATE;
713 case SSL_AD_UNSUPPORTED_CERTIFICATE:
714 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
715 case SSL_AD_CERTIFICATE_REVOKED:
716 return SSL3_AD_CERTIFICATE_REVOKED;
717 case SSL_AD_CERTIFICATE_EXPIRED:
718 return SSL3_AD_CERTIFICATE_EXPIRED;
719 case SSL_AD_CERTIFICATE_UNKNOWN:
720 return SSL3_AD_CERTIFICATE_UNKNOWN;
721 case SSL_AD_ILLEGAL_PARAMETER:
722 return SSL3_AD_ILLEGAL_PARAMETER;
723 case SSL_AD_UNKNOWN_CA:
724 return TLS1_AD_UNKNOWN_CA;
725 case SSL_AD_ACCESS_DENIED:
726 return TLS1_AD_ACCESS_DENIED;
727 case SSL_AD_DECODE_ERROR:
728 return TLS1_AD_DECODE_ERROR;
729 case SSL_AD_DECRYPT_ERROR:
730 return TLS1_AD_DECRYPT_ERROR;
731 case SSL_AD_EXPORT_RESTRICTION:
732 return TLS1_AD_EXPORT_RESTRICTION;
733 case SSL_AD_PROTOCOL_VERSION:
734 return TLS1_AD_PROTOCOL_VERSION;
735 case SSL_AD_INSUFFICIENT_SECURITY:
736 return TLS1_AD_INSUFFICIENT_SECURITY;
737 case SSL_AD_INTERNAL_ERROR:
738 return TLS1_AD_INTERNAL_ERROR;
739 case SSL_AD_USER_CANCELLED:
740 return TLS1_AD_USER_CANCELLED;
741 case SSL_AD_NO_RENEGOTIATION:
742 return TLS1_AD_NO_RENEGOTIATION;
743 case SSL_AD_UNSUPPORTED_EXTENSION:
744 return TLS1_AD_UNSUPPORTED_EXTENSION;
745 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
746 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
747 case SSL_AD_UNRECOGNIZED_NAME:
748 return TLS1_AD_UNRECOGNIZED_NAME;
749 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
750 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
751 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
752 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
753 case SSL_AD_UNKNOWN_PSK_IDENTITY:
754 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
755 case SSL_AD_INAPPROPRIATE_FALLBACK:
756 return TLS1_AD_INAPPROPRIATE_FALLBACK;
757 case SSL_AD_NO_APPLICATION_PROTOCOL:
758 return TLS1_AD_NO_APPLICATION_PROTOCOL;
759 case SSL_AD_CERTIFICATE_REQUIRED:
760 return SSL_AD_HANDSHAKE_FAILURE;
761 default:
762 return -1;
763 }
764 }