Botan  1.11.10
tls_session_key.cpp
Go to the documentation of this file.
1 /*
2 * TLS Session Key
3 * (C) 2004-2006,2011 Jack Lloyd
4 *
5 * Released under the terms of the Botan license
6 */
7 
8 #include <botan/internal/tls_session_key.h>
9 #include <botan/internal/tls_handshake_state.h>
10 #include <botan/internal/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<byte>& 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_ivlen = state->ciphersuite().cipher_ivlen();
26 
27  const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_ivlen);
28 
29  const byte MASTER_SECRET_MAGIC[] = {
30  0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
31 
32  const byte KEY_GEN_MAGIC[] = {
33  0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E };
34 
35  std::unique_ptr<KDF> prf(state->protocol_specific_prf());
36 
37  if(resuming)
38  {
39  master_sec = pre_master_secret;
40  }
41  else
42  {
43  secure_vector<byte> salt;
44 
45  if(state->version() != Protocol_Version::SSL_V3)
46  salt += std::make_pair(MASTER_SECRET_MAGIC, sizeof(MASTER_SECRET_MAGIC));
47 
48  salt += state->client_hello()->random();
49  salt += state->server_hello()->random();
50 
51  master_sec = prf->derive_key(48, pre_master_secret, salt);
52  }
53 
54  secure_vector<byte> salt;
55  if(state->version() != Protocol_Version::SSL_V3)
56  salt += std::make_pair(KEY_GEN_MAGIC, sizeof(KEY_GEN_MAGIC));
57  salt += state->server_hello()->random();
58  salt += state->client_hello()->random();
59 
60  SymmetricKey keyblock = prf->derive_key(prf_gen, master_sec, salt);
61 
62  const byte* key_data = keyblock.begin();
63 
64  c_mac = SymmetricKey(key_data, mac_keylen);
65  key_data += mac_keylen;
66 
67  s_mac = SymmetricKey(key_data, mac_keylen);
68  key_data += mac_keylen;
69 
70  c_cipher = SymmetricKey(key_data, cipher_keylen);
71  key_data += cipher_keylen;
72 
73  s_cipher = SymmetricKey(key_data, cipher_keylen);
74  key_data += cipher_keylen;
75 
76  c_iv = InitializationVector(key_data, cipher_ivlen);
77  key_data += cipher_ivlen;
78 
79  s_iv = InitializationVector(key_data, cipher_ivlen);
80  }
81 
82 }
83 
84 }
RC4_KEY state
Definition: ossl_arc4.cpp:40
OctetString SymmetricKey
Definition: symkey.h:134
uint8_t byte
Definition: types.h:30
OctetString InitializationVector
Definition: symkey.h:139