Botan 3.14.1
Crypto and TLS for C&
tls_session_key.cpp
Go to the documentation of this file.
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
10#include <botan/kdf.h>
11#include <botan/mem_ops.h>
12#include <botan/tls_messages_12.h>
13#include <botan/internal/tls_handshake_state.h>
14
15namespace Botan::TLS {
16
17/**
18* Session_Keys Constructor
19*/
21 const secure_vector<uint8_t>& pre_master_secret,
22 bool resuming) {
24
25 const auto& suite = state->ciphersuite();
26 BOTAN_STATE_CHECK(suite.valid());
27
28 const size_t cipher_keylen = suite.cipher_keylen();
29 const size_t mac_keylen = suite.mac_keylen();
30 const size_t cipher_nonce_bytes = suite.nonce_bytes_from_handshake();
31
32 const bool extended_master_secret = state->server_hello().supports_extended_master_secret();
33
34 const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_nonce_bytes);
35
36 const uint8_t MASTER_SECRET_MAGIC[] = {0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74};
37
38 const uint8_t EXT_MASTER_SECRET_MAGIC[] = {0x65, 0x78, 0x74, 0x65, 0x6E, 0x64, 0x65, 0x64, 0x20, 0x6D, 0x61,
39 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74};
40
41 const uint8_t KEY_GEN_MAGIC[] = {0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E};
42
43 auto prf = state->protocol_specific_prf();
44
45 if(resuming) {
46 // This is actually the master secret saved as part of the session
47 m_master_sec = pre_master_secret;
48 } else {
49 std::vector<uint8_t> salt;
50 std::vector<uint8_t> label;
51 if(extended_master_secret) {
52 label.assign(EXT_MASTER_SECRET_MAGIC, EXT_MASTER_SECRET_MAGIC + sizeof(EXT_MASTER_SECRET_MAGIC));
53 salt += state->hash().final(suite.prf_algo());
54 } else {
55 label.assign(MASTER_SECRET_MAGIC, MASTER_SECRET_MAGIC + sizeof(MASTER_SECRET_MAGIC));
56 salt += state->client_hello().random();
57 salt += state->server_hello().random();
58 }
59
60 m_master_sec = prf->derive_key(48, pre_master_secret, salt, label);
61 }
62
63 std::vector<uint8_t> salt;
64 std::vector<uint8_t> label;
65 label.assign(KEY_GEN_MAGIC, KEY_GEN_MAGIC + sizeof(KEY_GEN_MAGIC));
66 salt += state->server_hello().random();
67 salt += state->client_hello().random();
68
69 const secure_vector<uint8_t> prf_output = prf->derive_key(
70 prf_gen, m_master_sec.data(), m_master_sec.size(), salt.data(), salt.size(), label.data(), label.size());
71
72 const uint8_t* key_data = prf_output.data();
73
74 m_c_aead.resize(mac_keylen + cipher_keylen);
75 m_s_aead.resize(mac_keylen + cipher_keylen);
76
77 // NOLINTBEGIN(readability-container-data-pointer)
78 copy_mem(&m_c_aead[0], key_data, mac_keylen);
79 copy_mem(&m_s_aead[0], key_data + mac_keylen, mac_keylen);
80 // NOLINTEND(readability-container-data-pointer)
81
82 // Key is not used for NULL suites
83 if(cipher_keylen > 0) {
84 copy_mem(&m_c_aead[mac_keylen], key_data + 2 * mac_keylen, cipher_keylen);
85 copy_mem(&m_s_aead[mac_keylen], key_data + 2 * mac_keylen + cipher_keylen, cipher_keylen);
86 } else {
87 BOTAN_STATE_CHECK(suite.null_ciphersuite());
88 }
89
90 if(cipher_nonce_bytes > 0) {
91 const uint8_t* c_nonce_bytes = key_data + 2 * (mac_keylen + cipher_keylen);
92 m_c_nonce.assign(c_nonce_bytes, c_nonce_bytes + cipher_nonce_bytes);
93
94 const uint8_t* s_nonce_bytes = key_data + 2 * (mac_keylen + cipher_keylen) + cipher_nonce_bytes;
95 m_s_nonce.assign(s_nonce_bytes, s_nonce_bytes + cipher_nonce_bytes);
96 }
97}
98
99} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
secure_vector< uint8_t > final(std::string_view mac_algo) const
void client_hello(std::unique_ptr< Client_Hello_12 > client_hello)
void server_hello(std::unique_ptr< Server_Hello_12 > server_hello)
const Ciphersuite & ciphersuite() const
std::unique_ptr< KDF > protocol_specific_prf() const
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128