Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

tls_session_key.cpp
Go to the documentation of this file.
1 /*
2 * TLS Session Key
3 * (C) 2004-2006,2011,2016 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
12 namespace Botan {
13
14 namespace TLS {
15
16 /**
17 * Session_Keys Constructor
18 */
19 Session_Keys::Session_Keys(const Handshake_State* state,
20 const secure_vector<uint8_t>& pre_master_secret,
21 bool resuming)
22 {
23 const size_t cipher_keylen = state->ciphersuite().cipher_keylen();
24 const size_t mac_keylen = state->ciphersuite().mac_keylen();
25 const size_t cipher_nonce_bytes = state->ciphersuite().nonce_bytes_from_handshake();
26
27 const bool extended_master_secret = state->server_hello()->supports_extended_master_secret();
28
29 const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_nonce_bytes);
30
31 const uint8_t MASTER_SECRET_MAGIC[] = {
32 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
33
34 const uint8_t EXT_MASTER_SECRET_MAGIC[] = {
35 0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x65, 0x64, 0x20,
36 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
37
38 const uint8_t KEY_GEN_MAGIC[] = {
39 0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E };
40
41 std::unique_ptr<KDF> prf(state->protocol_specific_prf());
42
43 if(resuming)
44 {
45 // This is actually the master secret saved as part of the session
46 m_master_sec = pre_master_secret;
47 }
48 else
49 {
50 secure_vector<uint8_t> salt;
51 secure_vector<uint8_t> label;
52 if(extended_master_secret)
53 {
54 label += std::make_pair(EXT_MASTER_SECRET_MAGIC, sizeof(EXT_MASTER_SECRET_MAGIC));
55 salt += state->hash().final(state->version(),
56 state->ciphersuite().prf_algo());
57 }
58 else
59 {
60 label += std::make_pair(MASTER_SECRET_MAGIC, sizeof(MASTER_SECRET_MAGIC));
61 salt += state->client_hello()->random();
62 salt += state->server_hello()->random();
63 }
64
65 m_master_sec = prf->derive_key(48, pre_master_secret, salt, label);
66 }
67
68 secure_vector<uint8_t> salt;
69 secure_vector<uint8_t> label;
70 label += std::make_pair(KEY_GEN_MAGIC, sizeof(KEY_GEN_MAGIC));
71 salt += state->server_hello()->random();
72 salt += state->client_hello()->random();
73
74 SymmetricKey keyblock = prf->derive_key(prf_gen, m_master_sec, salt, label);
75
76 const uint8_t* key_data = keyblock.begin();
77
78 m_c_mac = SymmetricKey(key_data, mac_keylen);
79 key_data += mac_keylen;
80
81 m_s_mac = SymmetricKey(key_data, mac_keylen);
82 key_data += mac_keylen;
83
84 m_c_cipher = SymmetricKey(key_data, cipher_keylen);
85 key_data += cipher_keylen;
86
87 m_s_cipher = SymmetricKey(key_data, cipher_keylen);
88 key_data += cipher_keylen;
89
90 m_c_iv = InitializationVector(key_data, cipher_nonce_bytes);
91 key_data += cipher_nonce_bytes;
92
93 m_s_iv = InitializationVector(key_data, cipher_nonce_bytes);
94 }
95
96 }
97
98 }
Botan::TLS::Handshake_State::server_hello
void server_hello(Server_Hello *server_hello)
Definition: tls_handshake_state.cpp:206
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:141
Botan::OctetString
Definition: symkey.h:19
Botan::TLS::Session_Keys::Session_Keys
Session_Keys()=default
Botan::TLS::Handshake_State::protocol_specific_prf
KDF * protocol_specific_prf() const
Definition: tls_handshake_state.cpp:356
Botan::TLS::Handshake_State::hash
Handshake_Hash & hash()
Definition: tls_handshake_state.h:167
Botan::TLS::Ciphersuite::prf_algo
std::string prf_algo() const
Definition: tls_ciphersuite.h:90
Botan::TLS::Ciphersuite::mac_keylen
size_t mac_keylen() const
Definition: tls_ciphersuite.h:106
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::TLS::Handshake_State::client_hello
void client_hello(Client_Hello *client_hello)
Definition: tls_handshake_state.cpp:200
Botan
Definition: alg_id.cpp:13
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:136
Botan::TLS::Handshake_Hash::final
secure_vector< uint8_t > final(Protocol_Version version, const std::string &mac_algo) const
Definition: tls_handshake_hash.cpp:19
Botan::TLS::Ciphersuite::cipher_keylen
size_t cipher_keylen() const
Definition: tls_ciphersuite.h:100
Botan::TLS::Handshake_State
Definition: tls_handshake_state.h:47
Botan::TLS::Ciphersuite::nonce_bytes_from_handshake
size_t nonce_bytes_from_handshake() const
Definition: tls_ciphersuite.h:104
Botan::TLS::Handshake_State::ciphersuite
const Ciphersuite & ciphersuite() const
Definition: tls_handshake_state.h:159
Botan::OctetString::begin
const uint8_t * begin() const
Definition: symkey.h:36
Botan::TLS::Handshake_State::version
Protocol_Version version() const
Definition: tls_handshake_state.h:100