/src/botan/src/lib/tls/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 { |
14 | | |
15 | | namespace TLS { |
16 | | |
17 | | /** |
18 | | * Session_Keys Constructor |
19 | | */ |
20 | | Session_Keys::Session_Keys(const Handshake_State* state, |
21 | | const secure_vector<uint8_t>& pre_master_secret, |
22 | | bool resuming) |
23 | 10.2k | { |
24 | 10.2k | const size_t cipher_keylen = state->ciphersuite().cipher_keylen(); |
25 | 10.2k | const size_t mac_keylen = state->ciphersuite().mac_keylen(); |
26 | 10.2k | const size_t cipher_nonce_bytes = state->ciphersuite().nonce_bytes_from_handshake(); |
27 | | |
28 | 10.2k | const bool extended_master_secret = state->server_hello()->supports_extended_master_secret(); |
29 | | |
30 | 10.2k | const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_nonce_bytes); |
31 | | |
32 | 10.2k | const uint8_t MASTER_SECRET_MAGIC[] = { |
33 | 10.2k | 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 }; |
34 | | |
35 | 10.2k | const uint8_t EXT_MASTER_SECRET_MAGIC[] = { |
36 | 10.2k | 0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x65, 0x64, 0x20, |
37 | 10.2k | 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 }; |
38 | | |
39 | 10.2k | const uint8_t KEY_GEN_MAGIC[] = { |
40 | 10.2k | 0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E }; |
41 | | |
42 | 10.2k | auto prf = state->protocol_specific_prf(); |
43 | | |
44 | 10.2k | if(resuming) |
45 | 0 | { |
46 | | // This is actually the master secret saved as part of the session |
47 | 0 | m_master_sec = pre_master_secret; |
48 | 0 | } |
49 | 10.2k | else |
50 | 10.2k | { |
51 | 10.2k | std::vector<uint8_t> salt; |
52 | 10.2k | std::vector<uint8_t> label; |
53 | 10.2k | if(extended_master_secret) |
54 | 1.22k | { |
55 | 1.22k | label.assign(EXT_MASTER_SECRET_MAGIC, EXT_MASTER_SECRET_MAGIC + sizeof(EXT_MASTER_SECRET_MAGIC)); |
56 | 1.22k | salt += state->hash().final(state->ciphersuite().prf_algo()); |
57 | 1.22k | } |
58 | 8.99k | else |
59 | 8.99k | { |
60 | 8.99k | label.assign(MASTER_SECRET_MAGIC, MASTER_SECRET_MAGIC + sizeof(MASTER_SECRET_MAGIC)); |
61 | 8.99k | salt += state->client_hello()->random(); |
62 | 8.99k | salt += state->server_hello()->random(); |
63 | 8.99k | } |
64 | | |
65 | 10.2k | m_master_sec = prf->derive_key(48, pre_master_secret, salt, label); |
66 | 10.2k | } |
67 | | |
68 | 10.2k | std::vector<uint8_t> salt; |
69 | 10.2k | std::vector<uint8_t> label; |
70 | 10.2k | label.assign(KEY_GEN_MAGIC, KEY_GEN_MAGIC + sizeof(KEY_GEN_MAGIC)); |
71 | 10.2k | salt += state->server_hello()->random(); |
72 | 10.2k | salt += state->client_hello()->random(); |
73 | | |
74 | 10.2k | const secure_vector<uint8_t> prf_output = prf->derive_key( |
75 | 10.2k | prf_gen, |
76 | 10.2k | m_master_sec.data(), m_master_sec.size(), |
77 | 10.2k | salt.data(), salt.size(), |
78 | 10.2k | label.data(), label.size()); |
79 | | |
80 | 10.2k | const uint8_t* key_data = prf_output.data(); |
81 | | |
82 | 10.2k | m_c_aead.resize(mac_keylen + cipher_keylen); |
83 | 10.2k | m_s_aead.resize(mac_keylen + cipher_keylen); |
84 | | |
85 | 10.2k | copy_mem(&m_c_aead[0], key_data, mac_keylen); |
86 | 10.2k | copy_mem(&m_s_aead[0], key_data + mac_keylen, mac_keylen); |
87 | | |
88 | 10.2k | copy_mem(&m_c_aead[mac_keylen], key_data + 2*mac_keylen, cipher_keylen); |
89 | 10.2k | copy_mem(&m_s_aead[mac_keylen], key_data + 2*mac_keylen + cipher_keylen, cipher_keylen); |
90 | | |
91 | 10.2k | m_c_nonce.resize(cipher_nonce_bytes); |
92 | 10.2k | m_s_nonce.resize(cipher_nonce_bytes); |
93 | | |
94 | 10.2k | copy_mem(&m_c_nonce[0], key_data + 2*(mac_keylen + cipher_keylen), cipher_nonce_bytes); |
95 | 10.2k | copy_mem(&m_s_nonce[0], key_data + 2*(mac_keylen + cipher_keylen) + cipher_nonce_bytes, cipher_nonce_bytes); |
96 | 10.2k | } |
97 | | |
98 | | } |
99 | | |
100 | | } |