Botan  2.4.0
Crypto and TLS for C++11
tls_session.cpp
Go to the documentation of this file.
1 /*
2 * TLS Session State
3 * (C) 2011-2012,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/tls_session.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/asn1_str.h>
12 #include <botan/pem.h>
13 #include <botan/aead.h>
14 #include <botan/mac.h>
15 #include <botan/rng.h>
16 
17 namespace Botan {
18 
19 namespace TLS {
20 
21 Session::Session(const std::vector<uint8_t>& session_identifier,
22  const secure_vector<uint8_t>& master_secret,
23  Protocol_Version version,
24  uint16_t ciphersuite,
25  uint8_t compression_method,
26  Connection_Side side,
27  bool extended_master_secret,
28  bool encrypt_then_mac,
29  const std::vector<X509_Certificate>& certs,
30  const std::vector<uint8_t>& ticket,
31  const Server_Information& server_info,
32  const std::string& srp_identifier,
33  uint16_t srtp_profile) :
34  m_start_time(std::chrono::system_clock::now()),
35  m_identifier(session_identifier),
36  m_session_ticket(ticket),
37  m_master_secret(master_secret),
38  m_version(version),
39  m_ciphersuite(ciphersuite),
40  m_compression_method(compression_method),
41  m_connection_side(side),
42  m_srtp_profile(srtp_profile),
43  m_extended_master_secret(extended_master_secret),
44  m_encrypt_then_mac(encrypt_then_mac),
45  m_peer_certs(certs),
46  m_server_info(server_info),
47  m_srp_identifier(srp_identifier)
48  {
49  }
50 
51 Session::Session(const std::string& pem)
52  {
53  secure_vector<uint8_t> der = PEM_Code::decode_check_label(pem, "TLS SESSION");
54 
55  *this = Session(der.data(), der.size());
56  }
57 
58 Session::Session(const uint8_t ber[], size_t ber_len)
59  {
60  uint8_t side_code = 0;
61 
62  ASN1_String server_hostname;
63  ASN1_String server_service;
64  size_t server_port;
65 
66  ASN1_String srp_identifier_str;
67 
68  uint8_t major_version = 0, minor_version = 0;
69  std::vector<uint8_t> peer_cert_bits;
70 
71  size_t start_time = 0;
72  size_t srtp_profile = 0;
73  size_t fragment_size = 0;
74 
75  BER_Decoder(ber, ber_len)
77  .decode_and_check(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION),
78  "Unknown version in serialized TLS session")
79  .decode_integer_type(start_time)
80  .decode_integer_type(major_version)
81  .decode_integer_type(minor_version)
82  .decode(m_identifier, OCTET_STRING)
83  .decode(m_session_ticket, OCTET_STRING)
84  .decode_integer_type(m_ciphersuite)
85  .decode_integer_type(m_compression_method)
86  .decode_integer_type(side_code)
87  .decode_integer_type(fragment_size)
88  .decode(m_extended_master_secret)
89  .decode(m_encrypt_then_mac)
90  .decode(m_master_secret, OCTET_STRING)
91  .decode(peer_cert_bits, OCTET_STRING)
92  .decode(server_hostname)
93  .decode(server_service)
94  .decode(server_port)
95  .decode(srp_identifier_str)
96  .decode(srtp_profile)
97  .end_cons()
98  .verify_end();
99 
100  /*
101  Fragment size is not supported anymore, but the field is still
102  set in the session object.
103  */
104  if(fragment_size != 0)
105  {
106  throw Decoding_Error("Serialized TLS session used maximum fragment length which is "
107  " no longer supported");
108  }
109 
110  m_version = Protocol_Version(major_version, minor_version);
111  m_start_time = std::chrono::system_clock::from_time_t(start_time);
112  m_connection_side = static_cast<Connection_Side>(side_code);
113  m_srtp_profile = static_cast<uint16_t>(srtp_profile);
114 
115  m_server_info = Server_Information(server_hostname.value(),
116  server_service.value(),
117  static_cast<uint16_t>(server_port));
118 
119  m_srp_identifier = srp_identifier_str.value();
120 
121  if(!peer_cert_bits.empty())
122  {
123  DataSource_Memory certs(peer_cert_bits.data(), peer_cert_bits.size());
124 
125  while(!certs.end_of_data())
126  m_peer_certs.push_back(X509_Certificate(certs));
127  }
128  }
129 
131  {
132  std::vector<uint8_t> peer_cert_bits;
133  for(size_t i = 0; i != m_peer_certs.size(); ++i)
134  peer_cert_bits += m_peer_certs[i].BER_encode();
135 
136  return DER_Encoder()
138  .encode(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION))
139  .encode(static_cast<size_t>(std::chrono::system_clock::to_time_t(m_start_time)))
140  .encode(static_cast<size_t>(m_version.major_version()))
141  .encode(static_cast<size_t>(m_version.minor_version()))
142  .encode(m_identifier, OCTET_STRING)
143  .encode(m_session_ticket, OCTET_STRING)
144  .encode(static_cast<size_t>(m_ciphersuite))
145  .encode(static_cast<size_t>(m_compression_method))
146  .encode(static_cast<size_t>(m_connection_side))
147  .encode(static_cast<size_t>(/*old fragment size*/0))
148  .encode(m_extended_master_secret)
149  .encode(m_encrypt_then_mac)
150  .encode(m_master_secret, OCTET_STRING)
151  .encode(peer_cert_bits, OCTET_STRING)
152  .encode(ASN1_String(m_server_info.hostname(), UTF8_STRING))
153  .encode(ASN1_String(m_server_info.service(), UTF8_STRING))
154  .encode(static_cast<size_t>(m_server_info.port()))
155  .encode(ASN1_String(m_srp_identifier, UTF8_STRING))
156  .encode(static_cast<size_t>(m_srtp_profile))
157  .end_cons()
158  .get_contents();
159  }
160 
161 std::string Session::PEM_encode() const
162  {
163  return PEM_Code::encode(this->DER_encode(), "TLS SESSION");
164  }
165 
166 std::chrono::seconds Session::session_age() const
167  {
168  return std::chrono::duration_cast<std::chrono::seconds>(
169  std::chrono::system_clock::now() - m_start_time);
170  }
171 
172 std::vector<uint8_t>
174  {
175  std::unique_ptr<AEAD_Mode> aead(get_aead("AES-256/GCM", ENCRYPTION));
176  const size_t nonce_len = aead->default_nonce_length();
177 
178  const secure_vector<uint8_t> nonce = rng.random_vec(nonce_len);
179  const secure_vector<uint8_t> bits = this->DER_encode();
180 
181  // Support any length key for input
182  std::unique_ptr<MessageAuthenticationCode> hmac(MessageAuthenticationCode::create("HMAC(SHA-256)"));
183  hmac->set_key(key);
184  hmac->update(nonce);
185  aead->set_key(hmac->final());
186 
187  secure_vector<uint8_t> buf = nonce;
188  buf += bits;
189  aead->start(buf.data(), nonce_len);
190  aead->finish(buf, nonce_len);
191  return unlock(buf);
192  }
193 
194 Session Session::decrypt(const uint8_t in[], size_t in_len, const SymmetricKey& key)
195  {
196  try
197  {
198  std::unique_ptr<AEAD_Mode> aead(get_aead("AES-256/GCM", DECRYPTION));
199  const size_t nonce_len = aead->default_nonce_length();
200 
201  if(in_len < nonce_len + aead->tag_size())
202  throw Decoding_Error("Encrypted session too short to be valid");
203 
204  // Support any length key for input
205  std::unique_ptr<MessageAuthenticationCode> hmac(MessageAuthenticationCode::create("HMAC(SHA-256)"));
206  hmac->set_key(key);
207  hmac->update(in, nonce_len); // nonce bytes
208  aead->set_key(hmac->final());
209 
210  aead->start(in, nonce_len);
211  secure_vector<uint8_t> buf(in + nonce_len, in + in_len);
212  aead->finish(buf, 0);
213 
214  return Session(buf.data(), buf.size());
215  }
216  catch(std::exception& e)
217  {
218  throw Decoding_Error("Failed to decrypt serialized TLS session: " +
219  std::string(e.what()));
220  }
221  }
222 
223 }
224 
225 }
static Session decrypt(const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:45
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:132
BER_Decoder & decode_integer_type(T &out)
Definition: ber_dec.h:128
uint8_t minor_version() const
Definition: tls_version.h:82
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:163
std::chrono::system_clock::time_point start_time() const
Definition: tls_session.h:175
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:123
Definition: bigint.h:635
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:355
std::string hostname() const
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
BER_Decoder & end_cons()
Definition: ber_dec.cpp:265
std::vector< uint8_t > encrypt(const SymmetricKey &key, RandomNumberGenerator &rng) const
std::string PEM_encode() const
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:251
Definition: alg_id.cpp:13
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:131
secure_vector< uint8_t > DER_encode() const
std::chrono::seconds session_age() const
const std::string & value() const
Definition: asn1_str.h:27
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
BER_Decoder & verify_end()
Definition: ber_dec.cpp:176
uint8_t major_version() const
Definition: tls_version.h:77
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
AEAD_Mode * get_aead(const std::string &algo, Cipher_Dir dir)
Definition: aead.cpp:42
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88