Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::TLS::Session Class Referencefinal

#include <tls_session.h>

Public Member Functions

Ciphersuite ciphersuite () const
 
uint16_t ciphersuite_code () const
 
secure_vector< uint8_t > DER_encode () const
 
uint16_t dtls_srtp_profile () const
 
std::vector< uint8_t > encrypt (const SymmetricKey &key, RandomNumberGenerator &rng) const
 
const secure_vector< uint8_t > & master_secret () const
 
const std::vector< X509_Certificate > & peer_certs () const
 
std::string PEM_encode () const
 
const Server_Informationserver_info () const
 
 Session ()
 
 Session (const std::vector< uint8_t > &session_id, const secure_vector< uint8_t > &master_secret, Protocol_Version version, uint16_t ciphersuite, Connection_Side side, bool supports_extended_master_secret, bool supports_encrypt_then_mac, const std::vector< X509_Certificate > &peer_certs, const std::vector< uint8_t > &session_ticket, const Server_Information &server_info, const std::string &srp_identifier, uint16_t srtp_profile)
 
 Session (const uint8_t ber[], size_t ber_len)
 
 Session (const std::string &pem)
 
std::chrono::seconds session_age () const
 
const std::vector< uint8_t > & session_id () const
 
const std::vector< uint8_t > & session_ticket () const
 
Connection_Side side () const
 
const std::string & srp_identifier () const
 
std::chrono::system_clock::time_point start_time () const
 
bool supports_encrypt_then_mac () const
 
bool supports_extended_master_secret () const
 
Protocol_Version version () const
 

Static Public Member Functions

static Session decrypt (const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)
 
static Session decrypt (const std::vector< uint8_t > &ctext, const SymmetricKey &key)
 

Detailed Description

Class representing a TLS session state

Definition at line 27 of file tls_session.h.

Constructor & Destructor Documentation

◆ Session() [1/4]

Botan::TLS::Session::Session ( )
inline

Uninitialized session

Definition at line 34 of file tls_session.h.

Referenced by decrypt(), and Session().

34  :
35  m_start_time(std::chrono::system_clock::time_point::min()),
36  m_version(),
37  m_ciphersuite(0),
38  m_connection_side(static_cast<Connection_Side>(0)),
39  m_srtp_profile(0),
40  m_extended_master_secret(false),
41  m_encrypt_then_mac(false)
42  {}

◆ Session() [2/4]

Botan::TLS::Session::Session ( const std::vector< uint8_t > &  session_id,
const secure_vector< uint8_t > &  master_secret,
Protocol_Version  version,
uint16_t  ciphersuite,
Connection_Side  side,
bool  supports_extended_master_secret,
bool  supports_encrypt_then_mac,
const std::vector< X509_Certificate > &  peer_certs,
const std::vector< uint8_t > &  session_ticket,
const Server_Information server_info,
const std::string &  srp_identifier,
uint16_t  srtp_profile 
)

New session (sets session start time)

Definition at line 21 of file tls_session.cpp.

32  :
33  m_start_time(std::chrono::system_clock::now()),
34  m_identifier(session_identifier),
35  m_session_ticket(ticket),
36  m_master_secret(master_secret),
37  m_version(version),
38  m_ciphersuite(ciphersuite),
39  m_connection_side(side),
40  m_srtp_profile(srtp_profile),
41  m_extended_master_secret(extended_master_secret),
42  m_encrypt_then_mac(encrypt_then_mac),
43  m_peer_certs(certs),
44  m_server_info(server_info),
45  m_srp_identifier(srp_identifier)
46  {
47  }
const Server_Information & server_info() const
Definition: tls_session.h:183
Connection_Side side() const
Definition: tls_session.h:134
Ciphersuite ciphersuite() const
Definition: tls_session.h:128
const secure_vector< uint8_t > & master_secret() const
Definition: tls_session.h:144
Protocol_Version version() const
Definition: tls_session.h:118
const std::string & srp_identifier() const
Definition: tls_session.h:139

◆ Session() [3/4]

Botan::TLS::Session::Session ( const uint8_t  ber[],
size_t  ber_len 
)

Load a session from DER representation (created by DER_encode)

Parameters
berDER representation buffer
ber_lensize of buffer in bytes

Definition at line 56 of file tls_session.cpp.

References Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_and_check(), Botan::BER_Decoder::decode_integer_type(), Botan::BER_Decoder::end_cons(), Botan::OCTET_STRING, Botan::SEQUENCE, Botan::BER_Decoder::start_cons(), start_time(), Botan::ASN1_String::value(), and Botan::BER_Decoder::verify_end().

57  {
58  uint8_t side_code = 0;
59 
60  ASN1_String server_hostname;
61  ASN1_String server_service;
62  size_t server_port;
63 
64  ASN1_String srp_identifier_str;
65 
66  uint8_t major_version = 0, minor_version = 0;
67  std::vector<uint8_t> peer_cert_bits;
68 
69  size_t start_time = 0;
70  size_t srtp_profile = 0;
71  size_t fragment_size = 0;
72  size_t compression_method = 0;
73 
74  BER_Decoder(ber, ber_len)
75  .start_cons(SEQUENCE)
76  .decode_and_check(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION),
77  "Unknown version in serialized TLS session")
78  .decode_integer_type(start_time)
79  .decode_integer_type(major_version)
80  .decode_integer_type(minor_version)
81  .decode(m_identifier, OCTET_STRING)
82  .decode(m_session_ticket, OCTET_STRING)
83  .decode_integer_type(m_ciphersuite)
84  .decode_integer_type(compression_method)
85  .decode_integer_type(side_code)
86  .decode_integer_type(fragment_size)
87  .decode(m_extended_master_secret)
88  .decode(m_encrypt_then_mac)
89  .decode(m_master_secret, OCTET_STRING)
90  .decode(peer_cert_bits, OCTET_STRING)
91  .decode(server_hostname)
92  .decode(server_service)
93  .decode(server_port)
94  .decode(srp_identifier_str)
95  .decode(srtp_profile)
96  .end_cons()
97  .verify_end();
98 
99  /*
100  * Compression is not supported and must be zero
101  */
102  if(compression_method != 0)
103  {
104  throw Decoding_Error("Serialized TLS session contains non-null compression method");
105  }
106 
107  /*
108  Fragment size is not supported anymore, but the field is still
109  set in the session object.
110  */
111  if(fragment_size != 0)
112  {
113  throw Decoding_Error("Serialized TLS session used maximum fragment length which is "
114  " no longer supported");
115  }
116 
117  m_version = Protocol_Version(major_version, minor_version);
118  m_start_time = std::chrono::system_clock::from_time_t(start_time);
119  m_connection_side = static_cast<Connection_Side>(side_code);
120  m_srtp_profile = static_cast<uint16_t>(srtp_profile);
121 
122  m_server_info = Server_Information(server_hostname.value(),
123  server_service.value(),
124  static_cast<uint16_t>(server_port));
125 
126  m_srp_identifier = srp_identifier_str.value();
127 
128  if(!peer_cert_bits.empty())
129  {
130  DataSource_Memory certs(peer_cert_bits.data(), peer_cert_bits.size());
131 
132  while(!certs.end_of_data())
133  m_peer_certs.push_back(X509_Certificate(certs));
134  }
135  }
std::chrono::system_clock::time_point start_time() const
Definition: tls_session.h:168

◆ Session() [4/4]

Botan::TLS::Session::Session ( const std::string &  pem)
explicit

Load a session from PEM representation (created by PEM_encode)

Parameters
pemPEM representation

Definition at line 49 of file tls_session.cpp.

References Botan::PEM_Code::decode_check_label(), and Session().

50  {
51  secure_vector<uint8_t> der = PEM_Code::decode_check_label(pem, "TLS SESSION");
52 
53  *this = Session(der.data(), der.size());
54  }
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54

Member Function Documentation

◆ ciphersuite()

Ciphersuite Botan::TLS::Session::ciphersuite ( ) const
inline

Get the ciphersuite info of the saved session

Definition at line 128 of file tls_session.h.

References Botan::TLS::Ciphersuite::by_id().

Referenced by Botan::TLS::Server_Hello::Server_Hello().

128 { return Ciphersuite::by_id(m_ciphersuite); }
static Ciphersuite by_id(uint16_t suite)

◆ ciphersuite_code()

uint16_t Botan::TLS::Session::ciphersuite_code ( ) const
inline

Get the ciphersuite code of the saved session

Definition at line 123 of file tls_session.h.

Referenced by Botan::TLS::Client_Hello::Client_Hello().

123 { return m_ciphersuite; }

◆ decrypt() [1/2]

Session Botan::TLS::Session::decrypt ( const uint8_t  ctext[],
size_t  ctext_size,
const SymmetricKey key 
)
static

Decrypt a session created by encrypt

Parameters
ctextthe ciphertext returned by encrypt
ctext_sizethe size of ctext in bytes
keythe same key used by the encrypting side

Definition at line 201 of file tls_session.cpp.

References Botan::MessageAuthenticationCode::create(), Botan::AEAD_Mode::create_or_throw(), Botan::DECRYPTION, and Session().

Referenced by decrypt(), Botan::TLS::Session_Manager_SQL::load_from_server_info(), and Botan::TLS::Session_Manager_SQL::load_from_session_id().

202  {
203  try
204  {
205  std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw("AES-256/GCM", DECRYPTION);
206  const size_t nonce_len = aead->default_nonce_length();
207 
208  if(in_len < nonce_len + aead->tag_size())
209  throw Decoding_Error("Encrypted session too short to be valid");
210 
211  // Support any length key for input
212  std::unique_ptr<MessageAuthenticationCode> hmac(MessageAuthenticationCode::create("HMAC(SHA-256)"));
213  hmac->set_key(key);
214  hmac->update(in, nonce_len); // nonce bytes
215  aead->set_key(hmac->final());
216 
217  aead->start(in, nonce_len);
218  secure_vector<uint8_t> buf(in + nonce_len, in + in_len);
219  aead->finish(buf, 0);
220 
221  return Session(buf.data(), buf.size());
222  }
223  catch(std::exception& e)
224  {
225  throw Decoding_Error("Failed to decrypt serialized TLS session: " +
226  std::string(e.what()));
227  }
228  }
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:46
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42

◆ decrypt() [2/2]

static Session Botan::TLS::Session::decrypt ( const std::vector< uint8_t > &  ctext,
const SymmetricKey key 
)
inlinestatic

Decrypt a session created by encrypt

Parameters
ctextthe ciphertext returned by encrypt
keythe same key used by the encrypting side

Definition at line 102 of file tls_session.h.

References decrypt().

104  {
105  return Session::decrypt(ctext.data(), ctext.size(), key);
106  }
static Session decrypt(const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)

◆ DER_encode()

secure_vector< uint8_t > Botan::TLS::Session::DER_encode ( ) const

Encode this session data for storage

Warning
if the master secret is compromised so is the session traffic

Definition at line 137 of file tls_session.cpp.

References Botan::PKCS8::BER_encode(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents(), Botan::TLS::Server_Information::hostname(), Botan::TLS::Protocol_Version::major_version(), Botan::TLS::Protocol_Version::minor_version(), Botan::OCTET_STRING, Botan::TLS::Server_Information::port(), Botan::SEQUENCE, Botan::TLS::Server_Information::service(), Botan::DER_Encoder::start_cons(), and Botan::UTF8_STRING.

Referenced by encrypt(), and PEM_encode().

138  {
139  std::vector<uint8_t> peer_cert_bits;
140  for(size_t i = 0; i != m_peer_certs.size(); ++i)
141  peer_cert_bits += m_peer_certs[i].BER_encode();
142 
143  return DER_Encoder()
144  .start_cons(SEQUENCE)
145  .encode(static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION))
146  .encode(static_cast<size_t>(std::chrono::system_clock::to_time_t(m_start_time)))
147  .encode(static_cast<size_t>(m_version.major_version()))
148  .encode(static_cast<size_t>(m_version.minor_version()))
149  .encode(m_identifier, OCTET_STRING)
150  .encode(m_session_ticket, OCTET_STRING)
151  .encode(static_cast<size_t>(m_ciphersuite))
152  .encode(static_cast<size_t>(/*old compression method*/0))
153  .encode(static_cast<size_t>(m_connection_side))
154  .encode(static_cast<size_t>(/*old fragment size*/0))
155  .encode(m_extended_master_secret)
156  .encode(m_encrypt_then_mac)
157  .encode(m_master_secret, OCTET_STRING)
158  .encode(peer_cert_bits, OCTET_STRING)
159  .encode(ASN1_String(m_server_info.hostname(), UTF8_STRING))
160  .encode(ASN1_String(m_server_info.service(), UTF8_STRING))
161  .encode(static_cast<size_t>(m_server_info.port()))
162  .encode(ASN1_String(m_srp_identifier, UTF8_STRING))
163  .encode(static_cast<size_t>(m_srtp_profile))
164  .end_cons()
165  .get_contents();
166  }
uint8_t minor_version() const
Definition: tls_version.h:82
std::string hostname() const
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
uint8_t major_version() const
Definition: tls_version.h:77

◆ dtls_srtp_profile()

uint16_t Botan::TLS::Session::dtls_srtp_profile ( ) const
inline

Get the negotiated DTLS-SRTP algorithm (RFC 5764)

Definition at line 154 of file tls_session.h.

154 { return m_srtp_profile; }

◆ encrypt()

std::vector< uint8_t > Botan::TLS::Session::encrypt ( const SymmetricKey key,
RandomNumberGenerator rng 
) const

Encrypt a session (useful for serialization or session tickets)

Definition at line 180 of file tls_session.cpp.

References Botan::MessageAuthenticationCode::create(), Botan::AEAD_Mode::create_or_throw(), DER_encode(), Botan::ENCRYPTION, Botan::RandomNumberGenerator::random_vec(), and Botan::unlock().

Referenced by Botan::TLS::Session_Manager_SQL::save(), and Botan::TLS::Session_Manager_In_Memory::save().

181  {
182  std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw("AES-256/GCM", ENCRYPTION);
183  const size_t nonce_len = aead->default_nonce_length();
184 
185  const secure_vector<uint8_t> nonce = rng.random_vec(nonce_len);
186  const secure_vector<uint8_t> bits = this->DER_encode();
187 
188  // Support any length key for input
189  std::unique_ptr<MessageAuthenticationCode> hmac(MessageAuthenticationCode::create("HMAC(SHA-256)"));
190  hmac->set_key(key);
191  hmac->update(nonce);
192  aead->set_key(hmac->final());
193 
194  secure_vector<uint8_t> buf = nonce;
195  buf += bits;
196  aead->start(buf.data(), nonce_len);
197  aead->finish(buf, nonce_len);
198  return unlock(buf);
199  }
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:46
secure_vector< uint8_t > DER_encode() const
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42

◆ master_secret()

const secure_vector<uint8_t>& Botan::TLS::Session::master_secret ( ) const
inline

Get the saved master secret

Definition at line 144 of file tls_session.h.

144 { return m_master_secret; }

◆ peer_certs()

const std::vector<X509_Certificate>& Botan::TLS::Session::peer_certs ( ) const
inline

Return the certificate chain of the peer (possibly empty)

Definition at line 163 of file tls_session.h.

163 { return m_peer_certs; }

◆ PEM_encode()

std::string Botan::TLS::Session::PEM_encode ( ) const

Encode this session data for storage

Warning
if the master secret is compromised so is the session traffic

Definition at line 168 of file tls_session.cpp.

References DER_encode(), and Botan::PEM_Code::encode().

169  {
170  return PEM_Code::encode(this->DER_encode(), "TLS SESSION");
171  }
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
secure_vector< uint8_t > DER_encode() const

◆ server_info()

const Server_Information& Botan::TLS::Session::server_info ( ) const
inline
Returns
information about the TLS server

Definition at line 183 of file tls_session.h.

Referenced by Botan::TLS::Client_Hello::Client_Hello(), Botan::TLS::Session_Manager_SQL::save(), and Botan::TLS::Session_Manager_In_Memory::save().

183 { return m_server_info; }

◆ session_age()

std::chrono::seconds Botan::TLS::Session::session_age ( ) const

Return how long this session has existed (in seconds)

Definition at line 173 of file tls_session.cpp.

174  {
175  return std::chrono::duration_cast<std::chrono::seconds>(
176  std::chrono::system_clock::now() - m_start_time);
177  }

◆ session_id()

const std::vector<uint8_t>& Botan::TLS::Session::session_id ( ) const
inline

Get the session identifier

Definition at line 149 of file tls_session.h.

Referenced by Botan::TLS::Session_Manager_SQL::save(), and Botan::TLS::Session_Manager_In_Memory::save().

149 { return m_identifier; }

◆ session_ticket()

const std::vector<uint8_t>& Botan::TLS::Session::session_ticket ( ) const
inline

Return the session ticket the server gave us

Definition at line 178 of file tls_session.h.

Referenced by Botan::TLS::Client_Hello::Client_Hello().

178 { return m_session_ticket; }

◆ side()

Connection_Side Botan::TLS::Session::side ( ) const
inline

Get which side of the connection the resumed session we are/were acting as.

Definition at line 134 of file tls_session.h.

Referenced by Botan::TLS::Session_Manager_In_Memory::save().

134 { return m_connection_side; }

◆ srp_identifier()

const std::string& Botan::TLS::Session::srp_identifier ( ) const
inline

Get the SRP identity (if sent by the client in the initial handshake)

Definition at line 139 of file tls_session.h.

Referenced by Botan::TLS::Client_Hello::Client_Hello().

139 { return m_srp_identifier; }

◆ start_time()

std::chrono::system_clock::time_point Botan::TLS::Session::start_time ( ) const
inline

Get the wall clock time this session began

Definition at line 168 of file tls_session.h.

Referenced by Botan::TLS::Session_Manager_SQL::save(), and Session().

168 { return m_start_time; }

◆ supports_encrypt_then_mac()

bool Botan::TLS::Session::supports_encrypt_then_mac ( ) const
inline

Definition at line 158 of file tls_session.h.

Referenced by Botan::TLS::Client_Hello::Client_Hello().

158 { return m_encrypt_then_mac; }

◆ supports_extended_master_secret()

bool Botan::TLS::Session::supports_extended_master_secret ( ) const
inline

Definition at line 156 of file tls_session.h.

156 { return m_extended_master_secret; }

◆ version()

Protocol_Version Botan::TLS::Session::version ( ) const
inline

Get the version of the saved session

Definition at line 118 of file tls_session.h.

118 { return m_version; }

The documentation for this class was generated from the following files: