Coverage Report

Created: 2022-06-23 06:44

/src/botan/src/lib/tls/tls12/tls_session_key.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLS Session Key
3
* (C) 2004-2006,2011,2016,2019 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/tls_session_key.h>
9
#include <botan/internal/tls_handshake_state.h>
10
#include <botan/tls_messages.h>
11
#include <botan/kdf.h>
12
13
namespace Botan::TLS {
14
15
/**
16
* Session_Keys Constructor
17
*/
18
Session_Keys::Session_Keys(const Handshake_State* state,
19
                           const secure_vector<uint8_t>& pre_master_secret,
20
                           bool resuming)
21
13.5k
   {
22
13.5k
   const size_t cipher_keylen = state->ciphersuite().cipher_keylen();
23
13.5k
   const size_t mac_keylen = state->ciphersuite().mac_keylen();
24
13.5k
   const size_t cipher_nonce_bytes = state->ciphersuite().nonce_bytes_from_handshake();
25
26
13.5k
   const bool extended_master_secret = state->server_hello()->supports_extended_master_secret();
27
28
13.5k
   const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_nonce_bytes);
29
30
13.5k
   const uint8_t MASTER_SECRET_MAGIC[] = {
31
13.5k
      0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
32
33
13.5k
   const uint8_t EXT_MASTER_SECRET_MAGIC[] = {
34
13.5k
      0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x65, 0x64, 0x20,
35
13.5k
      0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
36
37
13.5k
   const uint8_t KEY_GEN_MAGIC[] = {
38
13.5k
      0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E };
39
40
13.5k
   auto prf = state->protocol_specific_prf();
41
42
13.5k
   if(resuming)
43
0
      {
44
      // This is actually the master secret saved as part of the session
45
0
      m_master_sec = pre_master_secret;
46
0
      }
47
13.5k
   else
48
13.5k
      {
49
13.5k
      std::vector<uint8_t> salt;
50
13.5k
      std::vector<uint8_t> label;
51
13.5k
      if(extended_master_secret)
52
815
         {
53
815
         label.assign(EXT_MASTER_SECRET_MAGIC, EXT_MASTER_SECRET_MAGIC + sizeof(EXT_MASTER_SECRET_MAGIC));
54
815
         salt += state->hash().final(state->ciphersuite().prf_algo());
55
815
         }
56
12.7k
      else
57
12.7k
         {
58
12.7k
         label.assign(MASTER_SECRET_MAGIC, MASTER_SECRET_MAGIC + sizeof(MASTER_SECRET_MAGIC));
59
12.7k
         salt += state->client_hello()->random();
60
12.7k
         salt += state->server_hello()->random();
61
12.7k
         }
62
63
13.5k
      m_master_sec = prf->derive_key(48, pre_master_secret, salt, label);
64
13.5k
      }
65
66
13.5k
   std::vector<uint8_t> salt;
67
13.5k
   std::vector<uint8_t> label;
68
13.5k
   label.assign(KEY_GEN_MAGIC, KEY_GEN_MAGIC + sizeof(KEY_GEN_MAGIC));
69
13.5k
   salt += state->server_hello()->random();
70
13.5k
   salt += state->client_hello()->random();
71
72
13.5k
   const secure_vector<uint8_t> prf_output = prf->derive_key(
73
13.5k
      prf_gen,
74
13.5k
      m_master_sec.data(), m_master_sec.size(),
75
13.5k
      salt.data(), salt.size(),
76
13.5k
      label.data(), label.size());
77
78
13.5k
   const uint8_t* key_data = prf_output.data();
79
80
13.5k
   m_c_aead.resize(mac_keylen + cipher_keylen);
81
13.5k
   m_s_aead.resize(mac_keylen + cipher_keylen);
82
83
13.5k
   copy_mem(&m_c_aead[0], key_data, mac_keylen);
84
13.5k
   copy_mem(&m_s_aead[0], key_data + mac_keylen, mac_keylen);
85
86
13.5k
   copy_mem(&m_c_aead[mac_keylen], key_data + 2*mac_keylen, cipher_keylen);
87
13.5k
   copy_mem(&m_s_aead[mac_keylen], key_data + 2*mac_keylen + cipher_keylen, cipher_keylen);
88
89
13.5k
   m_c_nonce.resize(cipher_nonce_bytes);
90
13.5k
   m_s_nonce.resize(cipher_nonce_bytes);
91
92
13.5k
   copy_mem(&m_c_nonce[0], key_data + 2*(mac_keylen + cipher_keylen), cipher_nonce_bytes);
93
13.5k
   copy_mem(&m_s_nonce[0], key_data + 2*(mac_keylen + cipher_keylen) + cipher_nonce_bytes, cipher_nonce_bytes);
94
13.5k
   }
95
96
}