10#include <botan/tls_extensions_13.h>
12#include <botan/credentials_manager.h>
13#include <botan/hash.h>
14#include <botan/tls_callbacks.h>
15#include <botan/tls_exceptn.h>
16#include <botan/tls_policy.h>
17#include <botan/tls_psk_identity_13.h>
18#include <botan/tls_session.h>
19#include <botan/tls_session_manager.h>
20#include <botan/internal/ct_utils.h>
21#include <botan/internal/stl_util.h>
22#include <botan/internal/tls_cipher_state.h>
23#include <botan/internal/tls_reader.h>
31decltype(
auto) calculate_age(std::chrono::system_clock::time_point then, std::chrono::system_clock::time_point now) {
36 return std::chrono::duration_cast<std::chrono::milliseconds>(now - then);
41 Client_PSK(Session_with_Handle& session_to_resume, std::chrono::system_clock::time_point timestamp) :
42 Client_PSK(PskIdentity(session_to_resume.handle.opaque_handle(),
43 calculate_age(session_to_resume.session.start_time(), timestamp),
44 session_to_resume.session.session_age_add()),
45 session_to_resume.session.ciphersuite().prf_algo(),
46 session_to_resume.session.extract_master_secret(),
47 Cipher_State::PSK_Type::Resumption) {}
50 explicit Client_PSK(ExternalPSK&& psk) :
53 psk.extract_master_secret(),
54 psk.is_imported() ? Cipher_State::PSK_Type::Imported : Cipher_State::PSK_Type::External) {}
56 Client_PSK(PskIdentity
id, std::vector<uint8_t> bndr) :
57 m_identity(std::move(id)), m_binder(std::move(bndr)), m_is_resumption(false) {}
59 Client_PSK(PskIdentity
id,
60 std::string_view prf_algo,
63 m_identity(std::move(id)),
78 m_binder(HashFunction::create_or_throw(prf_algo)->output_length()),
79 m_is_resumption(psk_type == Cipher_State::PSK_Type::Resumption),
80 m_cipher_state(Cipher_State::init_with_psk(
83 const PskIdentity& identity()
const {
return m_identity; }
85 const std::vector<uint8_t>& binder()
const {
return m_binder; }
87 bool is_resumption()
const {
return m_is_resumption; }
89 void set_binder(std::vector<uint8_t> binder) { m_binder = std::move(binder); }
91 const Cipher_State& cipher_state()
const {
93 return *m_cipher_state;
96 std::unique_ptr<Cipher_State> take_cipher_state() {
return std::exchange(m_cipher_state,
nullptr); }
99 PskIdentity m_identity;
100 std::vector<uint8_t> m_binder;
101 bool m_is_resumption;
105 std::unique_ptr<Cipher_State> m_cipher_state;
110 explicit Server_PSK(uint16_t
id) : m_selected_identity(id), m_session_to_resume_or_psk(std::monostate()) {}
112 Server_PSK(uint16_t
id, Session session) :
113 m_selected_identity(id), m_session_to_resume_or_psk(std::move(session)) {}
115 Server_PSK(uint16_t
id, ExternalPSK psk) : m_selected_identity(id), m_session_to_resume_or_psk(std::move(psk)) {}
117 uint16_t selected_identity()
const {
return m_selected_identity; }
119 std::variant<std::monostate, Session, ExternalPSK> take_session_to_resume_or_psk() {
120 BOTAN_STATE_CHECK(!std::holds_alternative<std::monostate>(m_session_to_resume_or_psk));
121 return std::exchange(m_session_to_resume_or_psk, std::monostate());
125 uint16_t m_selected_identity;
128 std::variant<std::monostate, Session, ExternalPSK> m_session_to_resume_or_psk;
133class PSK::PSK_Internal {
135 explicit PSK_Internal(Server_PSK srv_psk) : psk(std::move(srv_psk)) {}
137 explicit PSK_Internal(std::vector<Client_PSK> clt_psks) : psk(std::move(clt_psks)) {}
140 std::variant<std::vector<Client_PSK>, Server_PSK> psk;
145 if(extension_size != 2) {
146 throw TLS_Exception(Alert::DecodeError,
"Server provided a malformed PSK extension");
150 m_impl = std::make_unique<PSK_Internal>(Server_PSK(selected_id));
153 const auto identities_offset = reader.
read_so_far();
155 std::vector<PskIdentity> psk_identities;
163 if(identity.empty()) {
164 throw TLS_Exception(Alert::DecodeError,
"Empty PSK identity");
166 const auto obfuscated_ticket_age = reader.
get_uint32_t();
167 psk_identities.emplace_back(std::move(identity), obfuscated_ticket_age);
170 if(psk_identities.empty()) {
174 if(reader.
read_so_far() - identities_offset != identities_length) {
175 throw TLS_Exception(Alert::DecodeError,
"Inconsistent PSK identity list");
181 if(binders_length == 0) {
182 throw TLS_Exception(Alert::DecodeError,
"Empty PSK binders list");
185 std::vector<Client_PSK> psks;
186 for(
auto& psk_identity : psk_identities) {
188 throw TLS_Exception(Alert::IllegalParameter,
"Not enough PSK binders");
199 if(reader.
read_so_far() - binders_offset != binders_length) {
200 throw TLS_Exception(Alert::IllegalParameter,
"Too many PSK binders");
203 m_impl = std::make_unique<PSK_Internal>(std::move(psks));
205 throw TLS_Exception(Alert::DecodeError,
"Found a PSK extension in an unexpected handshake message");
209PSK::PSK(std::optional<Session_with_Handle>& session_to_resume, std::vector<ExternalPSK> psks,
Callbacks& callbacks) {
210 std::vector<Client_PSK> cpsk;
212 if(session_to_resume.has_value()) {
216 for(
auto&& psk : psks) {
217 cpsk.emplace_back(std::move(psk));
220 m_impl = std::make_unique<PSK_Internal>(std::move(cpsk));
224 m_impl(std::make_unique<PSK_Internal>(Server_PSK(psk_index, std::move(session_to_resume)))) {}
226PSK::PSK(ExternalPSK psk, uint16_t psk_index) :
227 m_impl(std::make_unique<PSK_Internal>(Server_PSK(psk_index, std::move(psk)))) {}
232 if(std::holds_alternative<Server_PSK>(m_impl->psk)) {
237 return std::get<std::vector<Client_PSK>>(m_impl->psk).
empty();
245 const auto id = std::get<Server_PSK>(server_psk.m_impl->psk).selected_identity();
246 auto& ids = std::get<std::vector<Client_PSK>>(m_impl->psk);
253 if(
id >= ids.size()) {
254 throw TLS_Exception(Alert::IllegalParameter,
"PSK identity selected by server is out of bounds");
257 auto& selected_psk = ids.at(
id);
258 auto cipher_state = selected_psk.take_cipher_state();
262 auto psk_id = [&]() -> std::optional<std::string> {
263 if(selected_psk.is_resumption()) {
266 return selected_psk.identity().identity_as_string();
277 if(!cipher_state->is_compatible_with(cipher)) {
278 throw TLS_Exception(Alert::IllegalParameter,
"PSK and ciphersuite selected by server are not compatible");
281 return {std::move(psk_id), std::move(cipher_state)};
292 auto& psks = std::get<std::vector<Client_PSK>>(m_impl->psk);
297 std::vector<PskIdentity> psk_identities;
299 psks.begin(), psks.end(), std::back_inserter(psk_identities), [&](
const auto& psk) { return psk.identity(); });
300 if(
auto selected_session =
302 auto& [session, psk_index] = selected_session.value();
310 "Session manager chose a session stored by a TLS client for server-side resumption");
317 if(session.server_info().hostname() == host) {
322 if(session.ciphersuite().prf_algo() != cipher.
prf_algo()) {
324 "Application chose a ticket that is not compatible with the negotiated ciphersuite");
327 return std::unique_ptr<PSK>(
new PSK(std::move(session), psk_index));
345 std::vector<std::string> psk_ids;
346 std::transform(psks.begin(), psks.end(), std::back_inserter(psk_ids), [&](
const auto& psk) {
347 return psk.identity().identity_as_string();
349 if(
auto selected_psk =
351 auto& psk = selected_psk.value();
357 if(psk.prf_algo() != cipher.
prf_algo()) {
359 "Application chose a PSK that is not compatible with the negotiated ciphersuite");
362 const auto selected_itr =
363 std::find_if(psk_identities.begin(), psk_identities.end(), [&](
const auto& offered_psk) {
364 return offered_psk.identity_as_string() == psk.identity();
366 if(selected_itr == psk_identities.end()) {
368 "Application provided a PSK with an identity that was not offered by the client");
382 return std::unique_ptr<PSK>(
383 new PSK(std::move(psk),
static_cast<uint16_t
>(std::distance(psk_identities.begin(), selected_itr))));
391 auto& psks = std::get<std::vector<Client_PSK>>(m_impl->psk);
393 const auto r = std::remove_if(psks.begin(), psks.end(), [&](
const auto& psk) {
394 const auto& cipher_state = psk.cipher_state();
395 return !cipher_state.is_compatible_with(cipher);
397 psks.erase(r, psks.end());
402 BOTAN_STATE_CHECK(std::holds_alternative<std::vector<Client_PSK>>(new_psk.m_impl->psk));
404 const auto& old_psks = std::get<std::vector<Client_PSK>>(m_impl->psk);
405 const auto& new_psks = std::get<std::vector<Client_PSK>>(new_psk.m_impl->psk);
407 auto old_itr = old_psks.begin();
408 for(
const auto& psk : new_psks) {
409 old_itr = std::find_if(old_itr, old_psks.end(), [&](
const Client_PSK& old_psk) {
410 return old_psk.identity().identity() == psk.identity().identity();
412 if(old_itr == old_psks.end()) {
413 throw TLS_Exception(Alert::IllegalParameter,
"PSK identities changed in updated Client Hello");
423 [](
auto out) -> std::variant<Session, ExternalPSK> {
424 if constexpr(std::is_same_v<
decltype(out), std::monostate>) {
430 std::get<Server_PSK>(m_impl->psk).take_session_to_resume_or_psk());
434 std::vector<uint8_t> result;
437 [&](
const Server_PSK& psk) {
440 const uint16_t
id = psk.selected_identity();
444 [&](
const std::vector<Client_PSK>& psks) {
447 std::vector<uint8_t> identities;
448 std::vector<uint8_t> binders;
449 for(
const auto& psk : psks) {
450 const auto& psk_identity = psk.identity();
453 const uint32_t obfuscated_ticket_age = psk_identity.obfuscated_age();
454 identities.push_back(
get_byte<0>(obfuscated_ticket_age));
455 identities.push_back(
get_byte<1>(obfuscated_ticket_age));
456 identities.push_back(
get_byte<2>(obfuscated_ticket_age));
457 identities.push_back(
get_byte<3>(obfuscated_ticket_age));
474 for(
auto& psk : std::get<std::vector<Client_PSK>>(m_impl->psk)) {
475 auto tth = truncated_transcript_hash.
clone();
476 const auto& cipher_state = psk.cipher_state();
478 psk.set_binder(cipher_state.psk_binder_mac(tth.truncated()));
486 const uint16_t index = std::get<Server_PSK>(server_psk.m_impl->psk).selected_identity();
487 const auto& psks = std::get<std::vector<Client_PSK>>(m_impl->psk);
490 const auto& expected_binder = psks[index].binder();
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
virtual std::optional< TLS::ExternalPSK > choose_preshared_key(std::string_view host, TLS::Connection_Side whoami, const std::vector< std::string > &identities, const std::optional< std::string > &prf=std::nullopt)
virtual std::chrono::system_clock::time_point tls_current_timestamp()
std::string prf_algo() const
std::pair< std::optional< std::string >, std::unique_ptr< Cipher_State > > take_selected_psk_info(const PSK &server_psk, const Ciphersuite &cipher)
std::variant< Session, ExternalPSK > take_session_to_resume_or_psk()
void filter(const Ciphersuite &cipher)
void validate_updates(const PSK &new_psk) const
std::vector< uint8_t > serialize(Connection_Side side) const override
void calculate_binders(const Transcript_Hash_State &truncated_transcript_hash)
bool validate_binder(const PSK &server_psk, std::span< const uint8_t > binder) const
std::unique_ptr< PSK > select_offered_psk(std::string_view host, const Ciphersuite &cipher, Session_Manager &session_mgr, Credentials_Manager &credentials_mgr, Callbacks &callbacks, const Policy &policy)
bool empty() const override
PSK(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type)
virtual bool require_client_certificate_authentication() const
virtual std::optional< std::pair< Session, uint16_t > > choose_from_offered_tickets(const std::vector< PskIdentity > &tickets, std::string_view hash_function, Callbacks &callbacks, const Policy &policy)
bool has_remaining() const
size_t read_so_far() const
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
Transcript_Hash_State clone() const
void set_algorithm(std::string_view algo_spec)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Strong< std::string, struct PresharedKeyID_ > PresharedKeyID
holds a PSK identity as used in TLS 1.3
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
constexpr uint8_t get_byte(T input)
std::vector< T, secure_allocator< T > > secure_vector