2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
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
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 #include <openssl/trace.h>
23 /* seed1 through seed5 are concatenated */
24 static int tls1_PRF(SSL *s,
25 const void *seed1, size_t seed1_len,
26 const void *seed2, size_t seed2_len,
27 const void *seed3, size_t seed3_len,
28 const void *seed4, size_t seed4_len,
29 const void *seed5, size_t seed5_len,
30 const unsigned char *sec, size_t slen,
31 unsigned char *out, size_t olen, int fatal)
33 const EVP_MD *md = ssl_prf_md(s);
34 EVP_PKEY_CTX *pctx = NULL;
38 /* Should never happen */
40 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
41 ERR_R_INTERNAL_ERROR);
43 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
46 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
47 if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
48 || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
49 || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0
50 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0
51 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0
52 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0
53 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0
54 || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0
55 || EVP_PKEY_derive(pctx, out, &olen) <= 0) {
57 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
58 ERR_R_INTERNAL_ERROR);
60 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
67 EVP_PKEY_CTX_free(pctx);
71 static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
75 /* Calls SSLfatal() as required */
77 TLS_MD_KEY_EXPANSION_CONST,
78 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,
79 SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,
80 NULL, 0, NULL, 0, s->session->master_key,
81 s->session->master_key_length, km, num, 1);
86 #ifndef OPENSSL_NO_KTLS
88 * Count the number of records that were not processed yet from record boundary.
90 * This function assumes that there are only fully formed records read in the
91 * record layer. If read_ahead is enabled, then this might be false and this
94 static int count_unprocessed_records(SSL *s)
96 SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
100 if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
103 while (PACKET_remaining(&pkt) > 0) {
104 /* Skip record type and version */
105 if (!PACKET_forward(&pkt, 3))
108 /* Read until next record */
109 if (PACKET_get_length_prefixed_2(&pkt, &subpkt))
119 int tls1_change_cipher_state(SSL *s, int which)
121 unsigned char *p, *mac_secret;
122 unsigned char *ms, *key, *iv;
125 #ifndef OPENSSL_NO_COMP
126 const SSL_COMP *comp;
130 size_t *mac_secret_size;
133 size_t n, i, j, k, cl;
135 #ifndef OPENSSL_NO_KTLS
136 struct tls12_crypto_info_aes_gcm_128 crypto_info;
138 unsigned char geniv[12];
139 int count_unprocessed;
143 c = s->s3.tmp.new_sym_enc;
144 m = s->s3.tmp.new_hash;
145 mac_type = s->s3.tmp.new_mac_pkey_type;
146 #ifndef OPENSSL_NO_COMP
147 comp = s->s3.tmp.new_compression;
150 if (which & SSL3_CC_READ) {
152 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
154 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
156 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
157 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
159 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
161 if (s->enc_read_ctx != NULL) {
163 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
164 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
165 ERR_R_MALLOC_FAILURE);
169 * make sure it's initialised in case we exit later with an error
171 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
173 dd = s->enc_read_ctx;
174 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
175 if (mac_ctx == NULL) {
176 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
177 ERR_R_INTERNAL_ERROR);
180 #ifndef OPENSSL_NO_COMP
181 COMP_CTX_free(s->expand);
184 s->expand = COMP_CTX_new(comp->method);
185 if (s->expand == NULL) {
186 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
187 SSL_F_TLS1_CHANGE_CIPHER_STATE,
188 SSL_R_COMPRESSION_LIBRARY_ERROR);
194 * this is done by dtls1_reset_seq_numbers for DTLS
197 RECORD_LAYER_reset_read_sequence(&s->rlayer);
198 mac_secret = &(s->s3.read_mac_secret[0]);
199 mac_secret_size = &(s->s3.read_mac_secret_size);
201 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
203 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
205 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
207 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
208 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
210 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
211 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
213 } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
214 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
215 ERR_R_MALLOC_FAILURE);
218 dd = s->enc_write_ctx;
219 if (SSL_IS_DTLS(s)) {
220 mac_ctx = EVP_MD_CTX_new();
221 if (mac_ctx == NULL) {
222 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
223 SSL_F_TLS1_CHANGE_CIPHER_STATE,
224 ERR_R_MALLOC_FAILURE);
227 s->write_hash = mac_ctx;
229 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
230 if (mac_ctx == NULL) {
231 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
232 SSL_F_TLS1_CHANGE_CIPHER_STATE,
233 ERR_R_MALLOC_FAILURE);
237 #ifndef OPENSSL_NO_COMP
238 COMP_CTX_free(s->compress);
241 s->compress = COMP_CTX_new(comp->method);
242 if (s->compress == NULL) {
243 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
244 SSL_F_TLS1_CHANGE_CIPHER_STATE,
245 SSL_R_COMPRESSION_LIBRARY_ERROR);
251 * this is done by dtls1_reset_seq_numbers for DTLS
254 RECORD_LAYER_reset_write_sequence(&s->rlayer);
255 mac_secret = &(s->s3.write_mac_secret[0]);
256 mac_secret_size = &(s->s3.write_mac_secret_size);
260 EVP_CIPHER_CTX_reset(dd);
262 p = s->s3.tmp.key_block;
263 i = *mac_secret_size = s->s3.tmp.new_mac_secret_size;
265 /* TODO(size_t): convert me */
266 cl = EVP_CIPHER_key_length(c);
268 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
269 /* If GCM/CCM mode only part of IV comes from PRF */
270 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
271 k = EVP_GCM_TLS_FIXED_IV_LEN;
272 else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
273 k = EVP_CCM_TLS_FIXED_IV_LEN;
275 k = EVP_CIPHER_iv_length(c);
276 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
277 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
294 if (n > s->s3.tmp.key_block_length) {
295 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
296 ERR_R_INTERNAL_ERROR);
300 memcpy(mac_secret, ms, i);
302 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
303 /* TODO(size_t): Convert this function */
304 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
305 (int)*mac_secret_size);
307 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
308 EVP_PKEY_free(mac_key);
309 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
310 ERR_R_INTERNAL_ERROR);
313 EVP_PKEY_free(mac_key);
316 OSSL_TRACE_BEGIN(TLS) {
317 BIO_printf(trc_out, "which = %04X, mac key:\n", which);
318 BIO_dump_indent(trc_out, ms, i, 4);
319 } OSSL_TRACE_END(TLS);
321 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
322 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
323 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
325 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
326 ERR_R_INTERNAL_ERROR);
329 } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
332 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
333 taglen = EVP_CCM8_TLS_TAG_LEN;
335 taglen = EVP_CCM_TLS_TAG_LEN;
336 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
337 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
338 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
339 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
340 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
341 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
342 ERR_R_INTERNAL_ERROR);
346 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
347 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
348 ERR_R_INTERNAL_ERROR);
352 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
353 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
354 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
355 (int)*mac_secret_size, mac_secret)) {
356 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
357 ERR_R_INTERNAL_ERROR);
360 #ifndef OPENSSL_NO_KTLS
364 if (((which & SSL3_CC_READ) && (s->mode & SSL_MODE_NO_KTLS_RX))
365 || ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX)))
368 /* ktls supports only the maximum fragment size */
369 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
372 /* check that cipher is AES_GCM_128 */
373 if (EVP_CIPHER_nid(c) != NID_aes_128_gcm
374 || EVP_CIPHER_mode(c) != EVP_CIPH_GCM_MODE
375 || EVP_CIPHER_key_length(c) != TLS_CIPHER_AES_GCM_128_KEY_SIZE)
378 /* check version is 1.2 */
379 if (s->version != TLS1_2_VERSION)
382 if (which & SSL3_CC_WRITE)
387 if (!ossl_assert(bio != NULL)) {
388 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
389 ERR_R_INTERNAL_ERROR);
393 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
394 if (which & SSL3_CC_WRITE) {
395 if (BIO_flush(bio) <= 0)
399 /* ktls doesn't support renegotiation */
400 if ((BIO_get_ktls_send(s->wbio) && (which & SSL3_CC_WRITE)) ||
401 (BIO_get_ktls_recv(s->rbio) && (which & SSL3_CC_READ))) {
402 SSLfatal(s, SSL_AD_NO_RENEGOTIATION, SSL_F_TLS1_CHANGE_CIPHER_STATE,
403 ERR_R_INTERNAL_ERROR);
407 memset(&crypto_info, 0, sizeof(crypto_info));
408 crypto_info.info.cipher_type = TLS_CIPHER_AES_GCM_128;
409 crypto_info.info.version = s->version;
411 EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GET_IV,
412 EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN,
414 memcpy(crypto_info.iv, geniv + EVP_GCM_TLS_FIXED_IV_LEN,
415 TLS_CIPHER_AES_GCM_128_IV_SIZE);
416 memcpy(crypto_info.salt, geniv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
417 memcpy(crypto_info.key, key, EVP_CIPHER_key_length(c));
418 if (which & SSL3_CC_WRITE)
419 memcpy(crypto_info.rec_seq, &s->rlayer.write_sequence,
420 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
422 memcpy(crypto_info.rec_seq, &s->rlayer.read_sequence,
423 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
425 if (which & SSL3_CC_READ) {
426 count_unprocessed = count_unprocessed_records(s);
427 if (count_unprocessed < 0)
430 /* increment the crypto_info record sequence */
431 while (count_unprocessed) {
432 for (bit = 7; bit >= 0; bit--) { /* increment */
433 ++crypto_info.rec_seq[bit];
434 if (crypto_info.rec_seq[bit] != 0)
441 /* ktls works with user provided buffers directly */
442 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
443 if (which & SSL3_CC_WRITE)
444 ssl3_release_write_buffer(s);
445 SSL_set_options(s, SSL_OP_NO_RENEGOTIATION);
449 #endif /* OPENSSL_NO_KTLS */
450 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
452 OSSL_TRACE_BEGIN(TLS) {
453 BIO_printf(trc_out, "which = %04X, key:\n", which);
454 BIO_dump_indent(trc_out, key, EVP_CIPHER_key_length(c), 4);
455 BIO_printf(trc_out, "iv:\n");
456 BIO_dump_indent(trc_out, iv, k, 4);
457 } OSSL_TRACE_END(TLS);
464 int tls1_setup_key_block(SSL *s)
470 int mac_type = NID_undef;
471 size_t num, mac_secret_size = 0;
474 if (s->s3.tmp.key_block_length != 0)
477 if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
478 &comp, s->ext.use_etm)) {
479 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
480 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
484 s->s3.tmp.new_sym_enc = c;
485 s->s3.tmp.new_hash = hash;
486 s->s3.tmp.new_mac_pkey_type = mac_type;
487 s->s3.tmp.new_mac_secret_size = mac_secret_size;
488 num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
491 ssl3_cleanup_key_block(s);
493 if ((p = OPENSSL_malloc(num)) == NULL) {
494 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
495 ERR_R_MALLOC_FAILURE);
499 s->s3.tmp.key_block_length = num;
500 s->s3.tmp.key_block = p;
502 OSSL_TRACE_BEGIN(TLS) {
503 BIO_printf(trc_out, "client random\n");
504 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
505 BIO_printf(trc_out, "server random\n");
506 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
507 BIO_printf(trc_out, "master key\n");
508 BIO_dump_indent(trc_out,
509 s->session->master_key,
510 s->session->master_key_length, 4);
511 } OSSL_TRACE_END(TLS);
513 if (!tls1_generate_key_block(s, p, num)) {
514 /* SSLfatal() already called */
518 OSSL_TRACE_BEGIN(TLS) {
519 BIO_printf(trc_out, "key block\n");
520 BIO_dump_indent(trc_out, p, num, 4);
521 } OSSL_TRACE_END(TLS);
523 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
524 && s->method->version <= TLS1_VERSION) {
526 * enable vulnerability countermeasure for CBC ciphers with known-IV
527 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
529 s->s3.need_empty_fragments = 1;
531 if (s->session->cipher != NULL) {
532 if (s->session->cipher->algorithm_enc == SSL_eNULL)
533 s->s3.need_empty_fragments = 0;
535 #ifndef OPENSSL_NO_RC4
536 if (s->session->cipher->algorithm_enc == SSL_RC4)
537 s->s3.need_empty_fragments = 0;
547 size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
551 unsigned char hash[EVP_MAX_MD_SIZE];
553 if (!ssl3_digest_cached_records(s, 0)) {
554 /* SSLfatal() already called */
558 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
559 /* SSLfatal() already called */
563 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
564 s->session->master_key, s->session->master_key_length,
565 out, TLS1_FINISH_MAC_LENGTH, 1)) {
566 /* SSLfatal() already called */
569 OPENSSL_cleanse(hash, hashlen);
570 return TLS1_FINISH_MAC_LENGTH;
573 int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
574 size_t len, size_t *secret_size)
576 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
577 unsigned char hash[EVP_MAX_MD_SIZE * 2];
580 * Digest cached records keeping record buffer (if present): this wont
581 * affect client auth because we're freezing the buffer at the same
582 * point (after client key exchange and before certificate verify)
584 if (!ssl3_digest_cached_records(s, 1)
585 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
586 /* SSLfatal() already called */
589 OSSL_TRACE_BEGIN(TLS) {
590 BIO_printf(trc_out, "Handshake hashes:\n");
591 BIO_dump(trc_out, (char *)hash, hashlen);
592 } OSSL_TRACE_END(TLS);
594 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
595 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
599 NULL, 0, p, len, out,
600 SSL3_MASTER_SECRET_SIZE, 1)) {
601 /* SSLfatal() already called */
604 OPENSSL_cleanse(hash, hashlen);
607 TLS_MD_MASTER_SECRET_CONST,
608 TLS_MD_MASTER_SECRET_CONST_SIZE,
609 s->s3.client_random, SSL3_RANDOM_SIZE,
611 s->s3.server_random, SSL3_RANDOM_SIZE,
612 NULL, 0, p, len, out,
613 SSL3_MASTER_SECRET_SIZE, 1)) {
614 /* SSLfatal() already called */
619 OSSL_TRACE_BEGIN(TLS) {
620 BIO_printf(trc_out, "Premaster Secret:\n");
621 BIO_dump_indent(trc_out, p, len, 4);
622 BIO_printf(trc_out, "Client Random:\n");
623 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
624 BIO_printf(trc_out, "Server Random:\n");
625 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
626 BIO_printf(trc_out, "Master Secret:\n");
627 BIO_dump_indent(trc_out,
628 s->session->master_key,
629 SSL3_MASTER_SECRET_SIZE, 4);
630 } OSSL_TRACE_END(TLS);
632 *secret_size = SSL3_MASTER_SECRET_SIZE;
636 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
637 const char *label, size_t llen,
638 const unsigned char *context,
639 size_t contextlen, int use_context)
641 unsigned char *val = NULL;
642 size_t vallen = 0, currentvalpos;
646 * construct PRF arguments we construct the PRF argument ourself rather
647 * than passing separate values into the TLS PRF to ensure that the
648 * concatenation of values does not create a prohibited label.
650 vallen = llen + SSL3_RANDOM_SIZE * 2;
652 vallen += 2 + contextlen;
655 val = OPENSSL_malloc(vallen);
659 memcpy(val + currentvalpos, (unsigned char *)label, llen);
660 currentvalpos += llen;
661 memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
662 currentvalpos += SSL3_RANDOM_SIZE;
663 memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
664 currentvalpos += SSL3_RANDOM_SIZE;
667 val[currentvalpos] = (contextlen >> 8) & 0xff;
669 val[currentvalpos] = contextlen & 0xff;
671 if ((contextlen > 0) || (context != NULL)) {
672 memcpy(val + currentvalpos, context, contextlen);
677 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
678 * label len) = 15, so size of val > max(prohibited label len) = 15 and
679 * the comparisons won't have buffer overflow
681 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
682 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
684 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
685 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
687 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
688 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
690 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
691 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
693 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
694 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
703 s->session->master_key, s->session->master_key_length,
708 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
712 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
715 OPENSSL_clear_free(val, vallen);
719 int tls1_alert_code(int code)
722 case SSL_AD_CLOSE_NOTIFY:
723 return SSL3_AD_CLOSE_NOTIFY;
724 case SSL_AD_UNEXPECTED_MESSAGE:
725 return SSL3_AD_UNEXPECTED_MESSAGE;
726 case SSL_AD_BAD_RECORD_MAC:
727 return SSL3_AD_BAD_RECORD_MAC;
728 case SSL_AD_DECRYPTION_FAILED:
729 return TLS1_AD_DECRYPTION_FAILED;
730 case SSL_AD_RECORD_OVERFLOW:
731 return TLS1_AD_RECORD_OVERFLOW;
732 case SSL_AD_DECOMPRESSION_FAILURE:
733 return SSL3_AD_DECOMPRESSION_FAILURE;
734 case SSL_AD_HANDSHAKE_FAILURE:
735 return SSL3_AD_HANDSHAKE_FAILURE;
736 case SSL_AD_NO_CERTIFICATE:
738 case SSL_AD_BAD_CERTIFICATE:
739 return SSL3_AD_BAD_CERTIFICATE;
740 case SSL_AD_UNSUPPORTED_CERTIFICATE:
741 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
742 case SSL_AD_CERTIFICATE_REVOKED:
743 return SSL3_AD_CERTIFICATE_REVOKED;
744 case SSL_AD_CERTIFICATE_EXPIRED:
745 return SSL3_AD_CERTIFICATE_EXPIRED;
746 case SSL_AD_CERTIFICATE_UNKNOWN:
747 return SSL3_AD_CERTIFICATE_UNKNOWN;
748 case SSL_AD_ILLEGAL_PARAMETER:
749 return SSL3_AD_ILLEGAL_PARAMETER;
750 case SSL_AD_UNKNOWN_CA:
751 return TLS1_AD_UNKNOWN_CA;
752 case SSL_AD_ACCESS_DENIED:
753 return TLS1_AD_ACCESS_DENIED;
754 case SSL_AD_DECODE_ERROR:
755 return TLS1_AD_DECODE_ERROR;
756 case SSL_AD_DECRYPT_ERROR:
757 return TLS1_AD_DECRYPT_ERROR;
758 case SSL_AD_EXPORT_RESTRICTION:
759 return TLS1_AD_EXPORT_RESTRICTION;
760 case SSL_AD_PROTOCOL_VERSION:
761 return TLS1_AD_PROTOCOL_VERSION;
762 case SSL_AD_INSUFFICIENT_SECURITY:
763 return TLS1_AD_INSUFFICIENT_SECURITY;
764 case SSL_AD_INTERNAL_ERROR:
765 return TLS1_AD_INTERNAL_ERROR;
766 case SSL_AD_USER_CANCELLED:
767 return TLS1_AD_USER_CANCELLED;
768 case SSL_AD_NO_RENEGOTIATION:
769 return TLS1_AD_NO_RENEGOTIATION;
770 case SSL_AD_UNSUPPORTED_EXTENSION:
771 return TLS1_AD_UNSUPPORTED_EXTENSION;
772 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
773 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
774 case SSL_AD_UNRECOGNIZED_NAME:
775 return TLS1_AD_UNRECOGNIZED_NAME;
776 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
777 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
778 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
779 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
780 case SSL_AD_UNKNOWN_PSK_IDENTITY:
781 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
782 case SSL_AD_INAPPROPRIATE_FALLBACK:
783 return TLS1_AD_INAPPROPRIATE_FALLBACK;
784 case SSL_AD_NO_APPLICATION_PROTOCOL:
785 return TLS1_AD_NO_APPLICATION_PROTOCOL;
786 case SSL_AD_CERTIFICATE_REQUIRED:
787 return SSL_AD_HANDSHAKE_FAILURE;