Botan  1.11.4
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 #include <botan/lookup.h>
12 #include <memory>
13 
14 namespace Botan {
15 
16 namespace TLS {
17 
18 /**
19 * Session_Keys Constructor
20 */
21 Session_Keys::Session_Keys(const Handshake_State* state,
22  const secure_vector<byte>& pre_master_secret,
23  bool resuming)
24  {
25  const size_t cipher_keylen = state->ciphersuite().cipher_keylen();
26  const size_t mac_keylen = state->ciphersuite().mac_keylen();
27  const size_t cipher_ivlen = state->ciphersuite().cipher_ivlen();
28 
29  const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_ivlen);
30 
31  const byte MASTER_SECRET_MAGIC[] = {
32  0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
33 
34  const byte KEY_GEN_MAGIC[] = {
35  0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E };
36 
37  std::unique_ptr<KDF> prf(state->protocol_specific_prf());
38 
39  if(resuming)
40  {
41  master_sec = pre_master_secret;
42  }
43  else
44  {
45  secure_vector<byte> salt;
46 
47  if(state->version() != Protocol_Version::SSL_V3)
48  salt += std::make_pair(MASTER_SECRET_MAGIC, sizeof(MASTER_SECRET_MAGIC));
49 
50  salt += state->client_hello()->random();
51  salt += state->server_hello()->random();
52 
53  master_sec = prf->derive_key(48, pre_master_secret, salt);
54  }
55 
56  secure_vector<byte> salt;
57  if(state->version() != Protocol_Version::SSL_V3)
58  salt += std::make_pair(KEY_GEN_MAGIC, sizeof(KEY_GEN_MAGIC));
59  salt += state->server_hello()->random();
60  salt += state->client_hello()->random();
61 
62  SymmetricKey keyblock = prf->derive_key(prf_gen, master_sec, salt);
63 
64  const byte* key_data = keyblock.begin();
65 
66  c_mac = SymmetricKey(key_data, mac_keylen);
67  key_data += mac_keylen;
68 
69  s_mac = SymmetricKey(key_data, mac_keylen);
70  key_data += mac_keylen;
71 
72  c_cipher = SymmetricKey(key_data, cipher_keylen);
73  key_data += cipher_keylen;
74 
75  s_cipher = SymmetricKey(key_data, cipher_keylen);
76  key_data += cipher_keylen;
77 
78  c_iv = InitializationVector(key_data, cipher_ivlen);
79  key_data += cipher_ivlen;
80 
81  s_iv = InitializationVector(key_data, cipher_ivlen);
82  }
83 
84 }
85 
86 }