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)
106Session_Summary::Session_Summary(
const Server_Hello_13& server_hello,
108 std::vector<X509_Certificate> peer_certs,
109 std::shared_ptr<const Public_Key> peer_raw_public_key,
110 std::optional<std::string> psk_identity,
111 bool session_was_resumed,
112 Server_Information server_info,
113 std::chrono::system_clock::time_point current_timestamp) :
114 Session_Base(current_timestamp,
115 server_hello.selected_version(),
116 server_hello.ciphersuite(),
131 std::move(peer_certs),
132 std::move(peer_raw_public_key),
133 std::move(server_info)),
134 m_external_psk_identity(std::move(psk_identity)),
135 m_was_resumption(session_was_resumed) {
136 BOTAN_ARG_CHECK(
version().is_tls_13_or_later(),
"Instantiated a TLS 1.3 session summary with an older TLS version");
137 set_session_id(server_hello.session_id());
142 if(psk_used() || was_resumption()) {
143 if(
const auto keyshare = server_hello.extensions().get<Key_Share>()) {
144 const auto group = keyshare->selected_group();
145 if(group.is_dh_named_group()) {
147 }
else if(group.is_ecdh_named_curve() || group.is_x25519() || group.is_x448()) {
149 }
else if(group.is_pure_kyber()) {
151 }
else if(group.is_pqc_hybrid()) {
158 const auto keyshare = server_hello.extensions().get<Key_Share>();
160 const auto group = keyshare->selected_group();
161 if(group.is_dh_named_group()) {
163 }
else if(group.is_ecdh_named_curve() || group.is_x25519() || group.is_x448()) {
165 }
else if(group.is_pure_kyber()) {
167 }
else if(group.is_pqc_hybrid()) {
180 uint16_t ciphersuite,
182 bool extended_master_secret,
183 bool encrypt_then_mac,
184 const std::vector<X509_Certificate>& certs,
186 uint16_t srtp_profile,
187 std::chrono::system_clock::time_point current_timestamp,
188 std::chrono::seconds lifetime_hint) :
194 extended_master_secret,
199 m_master_secret(master_secret),
200 m_early_data_allowed(false),
201 m_max_early_data_bytes(0),
203 m_lifetime_hint(lifetime_hint) {
207#if defined(BOTAN_HAS_TLS_13)
210 const std::optional<uint32_t>& max_early_data_bytes,
211 uint32_t ticket_age_add,
212 std::chrono::seconds lifetime_hint,
214 uint16_t ciphersuite,
216 const std::vector<X509_Certificate>& peer_certs,
217 std::shared_ptr<const Public_Key> peer_raw_public_key,
219 std::chrono::system_clock::time_point current_timestamp) :
238 std::move(peer_raw_public_key),
240 m_master_secret(session_psk),
241 m_early_data_allowed(max_early_data_bytes.has_value()),
242 m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
243 m_ticket_age_add(ticket_age_add),
244 m_lifetime_hint(lifetime_hint) {
249 const std::optional<uint32_t>& max_early_data_bytes,
250 std::chrono::seconds lifetime_hint,
251 const std::vector<X509_Certificate>& peer_certs,
252 std::shared_ptr<const Public_Key> peer_raw_public_key,
258 server_hello.selected_version(),
259 server_hello.ciphersuite(),
265 std::move(peer_raw_public_key),
267 m_master_secret(std::move(session_psk)),
268 m_early_data_allowed(max_early_data_bytes.has_value()),
269 m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
270 m_ticket_age_add(
load_be<uint32_t>(rng.random_vec(4).data(), 0)),
271 m_lifetime_hint(lifetime_hint) {
273 "Instantiated a TLS 1.3 session object with a TLS version older than 1.3");
281 uint8_t side_code = 0;
283 std::vector<uint8_t> raw_pubkey_or_empty;
289 uint8_t major_version = 0, minor_version = 0;
292 size_t srtp_profile = 0;
299 "Unknown version in serialized TLS session")
310 .decode(server_hostname)
311 .decode(server_service)
313 .decode(srtp_profile)
314 .decode(m_early_data_allowed)
315 .decode_integer_type(m_max_early_data_bytes)
316 .decode_integer_type(m_ticket_age_add)
323 "Serialized TLS session contains unknown cipher suite "
337 if(!raw_pubkey_or_empty.empty()) {
345 const auto raw_pubkey_or_empty =
350 .
encode(
static_cast<size_t>(TLS_SESSION_PARAM_STRUCT_VERSION))
369 .
encode(m_early_data_allowed)
370 .
encode(
static_cast<size_t>(m_max_early_data_bytes))
371 .
encode(
static_cast<size_t>(m_ticket_age_add))
372 .
encode(
static_cast<size_t>(m_lifetime_hint.count()))
383 return std::exchange(m_master_secret, {});
389const char*
const TLS_SESSION_CRYPT_HMAC =
"HMAC(SHA-512-256)";
391const char*
const TLS_SESSION_CRYPT_AEAD =
"AES-256/GCM";
392const char*
const TLS_SESSION_CRYPT_KEY_NAME =
"BOTAN TLS SESSION KEY NAME";
393const uint64_t TLS_SESSION_CRYPT_MAGIC = 0x068B5A9D396C0000;
394const size_t TLS_SESSION_CRYPT_MAGIC_LEN = 8;
395const size_t TLS_SESSION_CRYPT_KEY_NAME_LEN = 4;
396const size_t TLS_SESSION_CRYPT_AEAD_NONCE_LEN = 12;
397const size_t TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN = 16;
398const size_t TLS_SESSION_CRYPT_AEAD_TAG_SIZE = 16;
400const size_t TLS_SESSION_CRYPT_HDR_LEN = TLS_SESSION_CRYPT_MAGIC_LEN + TLS_SESSION_CRYPT_KEY_NAME_LEN +
401 TLS_SESSION_CRYPT_AEAD_NONCE_LEN + TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN;
403const size_t TLS_SESSION_CRYPT_OVERHEAD = TLS_SESSION_CRYPT_HDR_LEN + TLS_SESSION_CRYPT_AEAD_TAG_SIZE;
412 std::vector<uint8_t> key_name(hmac->output_length());
413 hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
414 hmac->final(key_name.data());
415 key_name.resize(TLS_SESSION_CRYPT_KEY_NAME_LEN);
417 std::vector<uint8_t> aead_nonce;
418 std::vector<uint8_t> key_seed;
420 rng.
random_vec(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
421 rng.
random_vec(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
423 hmac->update(key_seed);
429 std::vector<uint8_t> buf;
430 buf.reserve(TLS_SESSION_CRYPT_OVERHEAD + bits.size());
431 buf.resize(TLS_SESSION_CRYPT_MAGIC_LEN);
432 store_be(TLS_SESSION_CRYPT_MAGIC, &buf[0]);
440 aead->set_key(aead_key);
441 aead->set_associated_data(buf);
442 aead->start(aead_nonce);
443 aead->finish(bits, 0);
452 const size_t min_session_size = 48 + 4;
453 if(in.size() < TLS_SESSION_CRYPT_OVERHEAD + min_session_size) {
458 const auto magic = sub.
take(TLS_SESSION_CRYPT_MAGIC_LEN).data();
459 const auto key_name = sub.
take(TLS_SESSION_CRYPT_KEY_NAME_LEN).data();
460 const auto key_seed = sub.
take(TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN).data();
461 const auto aead_nonce = sub.
take(TLS_SESSION_CRYPT_AEAD_NONCE_LEN).data();
472 std::vector<uint8_t> cmp_key_name(hmac->output_length());
473 hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
474 hmac->final(cmp_key_name.data());
476 if(
CT::is_equal(cmp_key_name.data(), key_name, TLS_SESSION_CRYPT_KEY_NAME_LEN).as_bool() ==
false) {
480 hmac->update(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
484 aead->set_key(aead_key);
485 aead->set_associated_data(in.data(), TLS_SESSION_CRYPT_HDR_LEN);
486 aead->start(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
487 aead->finish(ctext, 0);
489 }
catch(std::exception& e) {
490 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)