9#include <botan/tls_messages.h>
10#include <botan/tls_extensions.h>
13#include <botan/internal/tls_reader.h>
14#include <botan/internal/tls_handshake_io.h>
15#include <botan/internal/tls_handshake_state.h>
16#include <botan/internal/tls_handshake_hash.h>
17#include <botan/credentials_manager.h>
18#include <botan/internal/ct_utils.h>
21#include <botan/ecdh.h>
33 std::string_view hostname,
40 std::string identity_hint;
45 identity_hint = reader.
get_string(2, 0, 65535);
48 const std::string psk_identity =
49 creds.
psk_identity(
"tls-client", std::string(hostname), identity_hint);
53 SymmetricKey psk = creds.
psk(
"tls-client", std::string(hostname), psk_identity);
55 std::vector<uint8_t> zeros(psk.
length());
68 std::string identity_hint = reader.
get_string(2, 0, 65535);
70 const std::string psk_identity =
71 creds.
psk_identity(
"tls-client", std::string(hostname), identity_hint);
75 psk = creds.
psk(
"tls-client", std::string(hostname), psk_identity);
82 const std::vector<uint8_t> peer_public_value = reader.
get_range<uint8_t>(2, 1, 65535);
90 {
throw TLS_Exception(Alert::InsufficientSecurity,
"DH group validation failed"); }
102 m_pre_master = std::move(shared_secret);
114 const uint8_t curve_type = reader.
get_byte();
119 const std::vector<uint8_t> peer_public_value = reader.
get_range<uint8_t>(1, 1, 255);
124 "Server sent ECC curve prohibited by policy");
137 m_pre_master = std::move(shared_secret);
151 "Application did not provide a ECDH_PublicKey");
154 ecdh_key->public_value(
167 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange method was negotiated");
170 reader.assert_done();
177 throw Unexpected_Message(
"No server kex message, but negotiated a key exchange that required it");
184 const Protocol_Version offered_version = state.client_hello()->legacy_version();
186 rng.random_vec(m_pre_master, 48);
192 const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
197 throw TLS_Exception(Alert::HandshakeFailure,
198 "Expected a RSA key in server cert but got " +
202 state.hash().update(io.send(*
this));
208Client_Key_Exchange::Client_Key_Exchange(
const std::vector<uint8_t>& contents,
217 if(kex_algo == Kex_Algo::STATIC_RSA)
220 "RSA key exchange negotiated so server sent a certificate");
222 if(!server_rsa_kex_key)
223 throw Internal_Error(
"Expected RSA kex but no server kex key set");
225 if(server_rsa_kex_key->
algo_name() !=
"RSA")
229 const std::vector<uint8_t> encrypted_pre_master = reader.
get_range<uint8_t>(2, 0, 65535);
234 const uint8_t client_major = state.
client_hello()->legacy_version().major_version();
235 const uint8_t client_minor = state.
client_hello()->legacy_version().minor_version();
243 const size_t expected_plaintext_size = 48;
244 const size_t expected_content_size = 2;
245 const uint8_t expected_content_bytes[expected_content_size] = { client_major, client_minor };
246 const uint8_t expected_content_pos[expected_content_size] = { 0, 1 };
250 encrypted_pre_master.size(),
251 expected_plaintext_size,
253 expected_content_bytes,
254 expected_content_pos,
255 expected_content_size);
265 const std::string psk_identity = reader.
get_string(2, 0, 65535);
267 psk = creds.
psk(
"tls-server",
277 "No PSK for identifier " + psk_identity);
281 if(kex_algo == Kex_Algo::PSK)
283 std::vector<uint8_t> zeros(psk.
length());
287 else if(kex_algo == Kex_Algo::DH ||
288 kex_algo == Kex_Algo::ECDH ||
289 kex_algo == Kex_Algo::ECDHE_PSK)
300 std::vector<uint8_t> client_pubkey;
304 client_pubkey = reader.
get_range<uint8_t>(2, 0, 65535);
308 client_pubkey = reader.
get_range<uint8_t>(1, 1, 255);
320 if(kex_algo == Kex_Algo::ECDHE_PSK)
326 m_pre_master = shared_secret;
332 catch(std::exception&)
346 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange negotiated");
#define BOTAN_ASSERT(expr, assertion_made)
virtual std::string algo_name() const =0
static BigInt decode(const uint8_t buf[], size_t length)
virtual std::string psk_identity(const std::string &type, const std::string &context, const std::string &identity_hint)
virtual SymmetricKey psk(const std::string &type, const std::string &context, const std::string &identity)
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
const char * what() const noexcept override
secure_vector< uint8_t > bits_of() const
secure_vector< uint8_t > decrypt_or_random(const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
virtual std::vector< uint8_t > public_value() const =0
SymmetricKey derive_key(size_t key_len, const uint8_t in[], size_t in_len, const uint8_t params[], size_t params_len) const
void random_vec(std::span< uint8_t > v)
virtual secure_vector< uint8_t > tls_ephemeral_key_agreement(const std::variant< TLS::Group_Params, DL_Group > &group, const PK_Key_Agreement_Key &private_key, const std::vector< uint8_t > &public_value, RandomNumberGenerator &rng, const Policy &policy)
virtual std::unique_ptr< PK_Key_Agreement_Key > tls_generate_ephemeral_key(const std::variant< TLS::Group_Params, DL_Group > &group, RandomNumberGenerator &rng)
Kex_Algo kex_method() const
Client_Key_Exchange(Handshake_IO &io, Handshake_State &state, const Policy &policy, Credentials_Manager &creds, const Public_Key *server_public_key, std::string_view hostname, RandomNumberGenerator &rng)
void server_certs(Certificate_12 *server_certs)
void server_hello(Server_Hello_12 *server_hello)
void server_kex(Server_Key_Exchange *server_kex)
Callbacks & callbacks() const
const Ciphersuite & ciphersuite() const
void client_hello(Client_Hello_12 *client_hello)
virtual bool hide_unknown_users() const
virtual Group_Params choose_key_exchange_group(const std::vector< Group_Params > &supported_by_peer, const std::vector< Group_Params > &offered_by_peer) const
uint8_t major_version() const
uint8_t minor_version() const
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
size_t remaining_bytes() const
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
constexpr bool is_ecdh(const Group_Params group)
bool key_exchange_is_psk(Kex_Algo m)
std::vector< T, secure_allocator< T > > secure_vector
std::unique_ptr< Public_Key > server_public_key