8#include <botan/tls_session.h>
10#include <botan/aead.h>
11#include <botan/asn1_obj.h>
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
17#include <botan/tls_callbacks.h>
18#include <botan/tls_messages.h>
19#include <botan/x509_key.h>
20#include <botan/internal/ct_utils.h>
21#include <botan/internal/loadstor.h>
22#include <botan/internal/stl_util.h>
28void Session_Handle::validate_constraints()
const {
34 BOTAN_ARG_CHECK(
id.size() <= 32,
"Session ID cannot be longer than 32 bytes");
39 "Ticket cannot be longer than 64kB");
44 BOTAN_ARG_CHECK(!handle.empty(),
"Opaque session handle must not be empty");
46 "Opaque session handle cannot be longer than 64kB");
59 return std::get<Session_ID>(m_handle);
64 const auto& handle = std::get<Opaque_Session_Handle>(m_handle);
65 if(handle.size() <= 32) {
75 return std::get<Session_Ticket>(m_handle);
88 if(!suite.has_value()) {
94Session_Summary::Session_Summary(
const Session_Base& base,
96 std::optional<std::string> psk_identity) :
97 Session_Base(base), m_external_psk_identity(std::move(psk_identity)), m_was_resumption(was_resumption) {
98 BOTAN_ARG_CHECK(
version().is_pre_tls_13(),
"Instantiated a TLS 1.2 session summary with an newer TLS version");
101 m_kex_algo = cs.kex_algo();
104#if defined(BOTAN_HAS_TLS_13)
108std::string tls13_kex_to_string(
bool psk, std::optional<Named_Group> group) {
110 if(group->is_dh_named_group()) {
112 }
else if(group->is_ecdh_named_curve() || group->is_x25519() || group->is_x448()) {
114 }
else if(group->is_pure_ml_kem() || group->is_pure_frodokem()) {
116 }
else if(group->is_pqc_hybrid()) {
118 }
else if(
auto s = group->to_string()) {
125 if(group->is_dh_named_group()) {
127 }
else if(group->is_ecdh_named_curve() || group->is_x25519() || group->is_x448()) {
129 }
else if(group->is_pure_ml_kem() || group->is_pure_frodokem()) {
131 }
else if(group->is_pqc_hybrid()) {
133 }
else if(
auto s = group->to_string()) {
143Session_Summary::Session_Summary(
const Server_Hello_13& server_hello,
145 std::vector<X509_Certificate> peer_certs,
146 std::shared_ptr<const Public_Key> peer_raw_public_key,
147 std::optional<std::string> psk_identity,
148 bool session_was_resumed,
149 Server_Information server_info,
150 std::chrono::system_clock::time_point current_timestamp) :
151 Session_Base(current_timestamp,
152 server_hello.selected_version(),
153 server_hello.ciphersuite(),
168 std::move(peer_certs),
169 std::move(peer_raw_public_key),
170 std::move(server_info)),
171 m_external_psk_identity(std::move(psk_identity)),
172 m_was_resumption(session_was_resumed) {
173 BOTAN_ARG_CHECK(
version().is_tls_13_or_later(),
"Instantiated a TLS 1.3 session summary with an older TLS version");
174 set_session_id(server_hello.session_id());
179 std::optional<Named_Group> group = [&]() -> std::optional<Named_Group> {
180 if(psk_used() || was_resumption()) {
181 if(
const auto keyshare = server_hello.extensions().get<Key_Share>()) {
182 return keyshare->selected_group();
187 const auto keyshare = server_hello.extensions().get<Key_Share>();
189 return keyshare->selected_group();
193 if(group.has_value()) {
194 m_kex_parameters = group->to_string();
197 m_kex_algo = tls13_kex_to_string(psk_used() || was_resumption(), group);
204 uint16_t ciphersuite,
206 bool extended_master_secret,
207 bool encrypt_then_mac,
208 const std::vector<X509_Certificate>& certs,
210 uint16_t srtp_profile,
211 std::chrono::system_clock::time_point current_timestamp,
212 std::chrono::seconds lifetime_hint) :
218 extended_master_secret,
223 m_master_secret(master_secret),
224 m_early_data_allowed(false),
225 m_max_early_data_bytes(0),
227 m_lifetime_hint(lifetime_hint) {
231#if defined(BOTAN_HAS_TLS_13)
234 const std::optional<uint32_t>& max_early_data_bytes,
235 uint32_t ticket_age_add,
236 std::chrono::seconds lifetime_hint,
238 uint16_t ciphersuite,
240 const std::vector<X509_Certificate>& peer_certs,
241 std::shared_ptr<const Public_Key> peer_raw_public_key,
243 std::chrono::system_clock::time_point current_timestamp) :
262 std::move(peer_raw_public_key),
264 m_master_secret(session_psk),
265 m_early_data_allowed(max_early_data_bytes.has_value()),
266 m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
267 m_ticket_age_add(ticket_age_add),
268 m_lifetime_hint(lifetime_hint) {
273 const std::optional<uint32_t>& max_early_data_bytes,
274 std::chrono::seconds lifetime_hint,
275 const std::vector<X509_Certificate>& peer_certs,
276 std::shared_ptr<const Public_Key> peer_raw_public_key,
282 server_hello.selected_version(),
283 server_hello.ciphersuite(),
289 std::move(peer_raw_public_key),
291 m_master_secret(std::move(session_psk)),
292 m_early_data_allowed(max_early_data_bytes.has_value()),
293 m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
294 m_ticket_age_add(
load_be<uint32_t>(rng.random_vec(4).data(), 0)),
295 m_lifetime_hint(lifetime_hint) {
297 "Instantiated a TLS 1.3 session object with a TLS version older than 1.3");
305 uint8_t side_code = 0;
307 std::vector<uint8_t> raw_pubkey_or_empty;
313 uint8_t major_version = 0, minor_version = 0;
316 size_t srtp_profile = 0;
323 "Unknown version in serialized TLS session")
334 .decode(server_hostname)
335 .decode(server_service)
337 .decode(srtp_profile)
338 .decode(m_early_data_allowed)
339 .decode_integer_type(m_max_early_data_bytes)
340 .decode_integer_type(m_ticket_age_add)
347 "Serialized TLS session contains unknown cipher suite "
361 if(!raw_pubkey_or_empty.empty()) {
369 const auto raw_pubkey_or_empty =
374 .
encode(
static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION))
393 .
encode(m_early_data_allowed)
394 .
encode(
static_cast<size_t>(m_max_early_data_bytes))
395 .
encode(
static_cast<size_t>(m_ticket_age_add))
396 .
encode(
static_cast<size_t>(m_lifetime_hint.count()))
407 return std::exchange(m_master_secret, {});
413const char*
const TLS_SESSION_CRYPT_HMAC =
"HMAC(SHA-512-256)";
415const char*
const TLS_SESSION_CRYPT_AEAD =
"AES-256/GCM";
416const char*
const TLS_SESSION_CRYPT_KEY_NAME =
"BOTAN TLS SESSION KEY NAME";
417const uint64_t TLS_SESSION_CRYPT_MAGIC = 0x068B5A9D396C0000;
418const size_t TLS_SESSION_CRYPT_MAGIC_LEN = 8;
419const size_t TLS_SESSION_CRYPT_KEY_NAME_LEN = 4;
420const size_t TLS_SESSION_CRYPT_AEAD_NONCE_LEN = 12;
421const size_t TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN = 16;
422const size_t TLS_SESSION_CRYPT_AEAD_TAG_SIZE = 16;
424const size_t TLS_SESSION_CRYPT_HDR_LEN = TLS_SESSION_CRYPT_MAGIC_LEN + TLS_SESSION_CRYPT_KEY_NAME_LEN +
425 TLS_SESSION_CRYPT_AEAD_NONCE_LEN + TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN;
427const size_t TLS_SESSION_CRYPT_OVERHEAD = TLS_SESSION_CRYPT_HDR_LEN + TLS_SESSION_CRYPT_AEAD_TAG_SIZE;
436 std::vector<uint8_t> key_name(hmac->output_length());
437 hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
438 hmac->final(key_name.data());
439 key_name.resize(TLS_SESSION_CRYPT_KEY_NAME_LEN);
441 std::vector<uint8_t> aead_nonce;
442 std::vector<uint8_t> key_seed;
444 rng.
random_vec(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
445 rng.
random_vec(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
447 hmac->update(key_seed);
453 std::vector<uint8_t> buf;
454 buf.reserve(TLS_SESSION_CRYPT_OVERHEAD + bits.size());
455 buf.resize(TLS_SESSION_CRYPT_MAGIC_LEN);
456 store_be(TLS_SESSION_CRYPT_MAGIC, &buf[0]);
464 aead->set_key(aead_key);
465 aead->set_associated_data(buf);
466 aead->start(aead_nonce);
467 aead->finish(bits, 0);
476 const size_t min_session_size = 48 + 4;
477 if(in.size() < TLS_SESSION_CRYPT_OVERHEAD + min_session_size) {
482 const auto magic = sub.
take(TLS_SESSION_CRYPT_MAGIC_LEN).data();
483 const auto key_name = sub.
take(TLS_SESSION_CRYPT_KEY_NAME_LEN).data();
484 const auto key_seed = sub.
take(TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN).data();
485 const auto aead_nonce = sub.
take(TLS_SESSION_CRYPT_AEAD_NONCE_LEN).data();
496 std::vector<uint8_t> cmp_key_name(hmac->output_length());
497 hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
498 hmac->final(cmp_key_name.data());
500 if(
CT::is_equal(cmp_key_name.data(), key_name, TLS_SESSION_CRYPT_KEY_NAME_LEN).as_bool() ==
false) {
504 hmac->update(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
508 aead->set_key(aead_key);
509 aead->set_associated_data(in.data(), TLS_SESSION_CRYPT_HDR_LEN);
510 aead->start(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
511 aead->finish(ctext, 0);
513 }
catch(std::exception& e) {
514 throw Decoding_Error(
"Failed to decrypt serialized TLS session: " + std::string(e.what()));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ARG_CHECK(expr, msg)
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
const std::string & value() const
BER_Decoder & decode(bool &out)
BER_Decoder & decode_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder start_sequence()
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
BER_Decoder & decode_integer_type(T &out)
auto copy_as_secure_vector(const size_t count)
std::span< const uint8_t > take(const size_t count)
secure_vector< uint8_t > get_contents()
DER_Encoder & encode_list(const std::vector< T > &values)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
void random_vec(std::span< uint8_t > v)
static std::optional< Ciphersuite > by_id(uint16_t suite)
uint8_t major_version() const
uint8_t minor_version() const
bool is_pre_tls_13() const
std::vector< X509_Certificate > m_peer_certs
bool m_extended_master_secret
Protocol_Version version() const
Protocol_Version m_version
std::chrono::system_clock::time_point m_start_time
Server_Information m_server_info
std::chrono::system_clock::time_point start_time() const
uint16_t ciphersuite_code() const
Ciphersuite ciphersuite() const
std::shared_ptr< const Public_Key > m_peer_raw_public_key
Connection_Side m_connection_side
std::optional< Session_Ticket > ticket() const
decltype(auto) get() const
bool is_opaque_handle() const
Opaque_Session_Handle opaque_handle() const
std::optional< Session_ID > id() const
secure_vector< uint8_t > DER_encode() const
std::vector< uint8_t > encrypt(const SymmetricKey &key, RandomNumberGenerator &rng) const
std::chrono::seconds lifetime_hint() const
static Session decrypt(const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)
std::string PEM_encode() const
Session(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 Server_Information &server_info, uint16_t srtp_profile, std::chrono::system_clock::time_point current_timestamp, std::chrono::seconds lifetime_hint=std::chrono::seconds::max())
secure_vector< uint8_t > extract_master_secret()
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
std::string kex_method_to_string(Kex_Algo method)
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
Strong< std::vector< uint8_t >, struct Session_Ticket_ > Session_Ticket
holds a TLS 1.2 session ticket for stateless resumption
Strong< std::vector< uint8_t >, struct Opaque_Session_Handle_ > Opaque_Session_Handle
holds an opaque session handle as used in TLS 1.3 that could be either a ticket for stateless resumpt...
std::unique_ptr< Public_Key > load_key(DataSource &source)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto store_be(ParamTs &&... params)
overloaded(Ts...) -> overloaded< Ts... >
constexpr auto load_be(ParamTs &&... params)