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>
22#if defined(BOTAN_HAS_CECPQ1)
23 #include <botan/cecpq1.h>
26#if defined(BOTAN_HAS_SRP6)
27 #include <botan/srp6.h>
42 const std::string& hostname,
49 std::string identity_hint =
"";
54 identity_hint = reader.
get_string(2, 0, 65535);
57 const std::string psk_identity =
58 creds.
psk_identity(
"tls-client", hostname, identity_hint);
64 std::vector<uint8_t> zeros(psk.
length());
78 std::string identity_hint = reader.
get_string(2, 0, 65535);
80 const std::string psk_identity =
81 creds.
psk_identity(
"tls-client", hostname, identity_hint);
85 psk = creds.
psk(
"tls-client", hostname, psk_identity);
91 const std::vector<uint8_t> modulus = reader.
get_range<uint8_t>(2, 1, 65535);
92 const std::vector<uint8_t> generator = reader.
get_range<uint8_t>(2, 1, 65535);
93 const std::vector<uint8_t> peer_public_value = reader.
get_range<uint8_t>(2, 1, 65535);
98 const std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> dh_result =
102 m_pre_master = dh_result.first;
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");
133 const std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> ecdh_result =
139 m_pre_master = ecdh_result.first;
149#if defined(BOTAN_HAS_SRP6)
154 std::vector<uint8_t> salt = reader.get_range<uint8_t>(1, 1, 255);
159 const std::string srp_identifier =
162 const std::string srp_password =
163 creds.
srp_password(
"tls-client", hostname, srp_identifier);
165 std::pair<BigInt, SymmetricKey> srp_vals =
175 m_pre_master = srp_vals.second.bits_of();
179#if defined(BOTAN_HAS_CECPQ1)
182 const std::vector<uint8_t> cecpq1_offer = reader.get_range<uint8_t>(2, 1, 65535);
189 CECPQ1_accept(shared_secret.data(), newhope_accept.data(), cecpq1_offer.data(), rng);
191 m_pre_master = shared_secret;
196 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange method was negotiated");
199 reader.assert_done();
206 throw Unexpected_Message(
"No server kex message, but negotiated a key exchange that required it");
215 rng.random_vec(m_pre_master, 48);
221 const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
227 "Expected a RSA key in server cert but got " +
231 state.hash().update(io.send(*
this));
237Client_Key_Exchange::Client_Key_Exchange(
const std::vector<uint8_t>& contents,
246 if(kex_algo == Kex_Algo::STATIC_RSA)
249 "RSA key exchange negotiated so server sent a certificate");
251 if(!server_rsa_kex_key)
252 throw Internal_Error(
"Expected RSA kex but no server kex key set");
258 const std::vector<uint8_t> encrypted_pre_master = reader.
get_range<uint8_t>(2, 0, 65535);
263 const uint8_t client_major = state.
client_hello()->version().major_version();
264 const uint8_t client_minor = state.
client_hello()->version().minor_version();
272 const size_t expected_plaintext_size = 48;
273 const size_t expected_content_size = 2;
274 const uint8_t expected_content_bytes[expected_content_size] = { client_major, client_minor };
275 const uint8_t expected_content_pos[expected_content_size] = { 0, 1 };
279 encrypted_pre_master.size(),
280 expected_plaintext_size,
282 expected_content_bytes,
283 expected_content_pos,
284 expected_content_size);
294 const std::string psk_identity = reader.
get_string(2, 0, 65535);
296 psk = creds.
psk(
"tls-server",
306 "No PSK for identifier " + psk_identity);
310 if(kex_algo == Kex_Algo::PSK)
312 std::vector<uint8_t> zeros(psk.
length());
316#if defined(BOTAN_HAS_SRP6)
317 else if(kex_algo == Kex_Algo::SRP_SHA)
324#if defined(BOTAN_HAS_CECPQ1)
325 else if(kex_algo == Kex_Algo::CECPQ1)
329 const std::vector<uint8_t> cecpq1_accept = reader.
get_range<uint8_t>(2, 0, 65535);
334 CECPQ1_finish(m_pre_master.data(), cecpq1_offer, cecpq1_accept.data());
337 else if(kex_algo == Kex_Algo::DH ||
338 kex_algo == Kex_Algo::DHE_PSK ||
339 kex_algo == Kex_Algo::ECDH ||
340 kex_algo == Kex_Algo::ECDHE_PSK)
351 std::vector<uint8_t> client_pubkey;
355 client_pubkey = reader.
get_range<uint8_t>(2, 0, 65535);
359 client_pubkey = reader.
get_range<uint8_t>(1, 1, 255);
371 if(kex_algo == Kex_Algo::DHE_PSK ||
372 kex_algo == Kex_Algo::ECDHE_PSK)
378 m_pre_master = shared_secret;
384 catch(std::exception&)
398 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange negotiated");
#define BOTAN_ASSERT(expr, assertion_made)
static BigInt decode(const uint8_t buf[], size_t length)
static std::vector< uint8_t > encode(const BigInt &n)
virtual std::string srp_password(const std::string &type, const std::string &context, const std::string &identifier)
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)
virtual std::string srp_identifier(const std::string &type, const std::string &context)
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
virtual std::string algo_name() const =0
secure_vector< uint8_t > random_vec(size_t bytes)
SymmetricKey step2(const BigInt &A)
virtual std::string tls_decode_group_param(Group_Params group_param)
virtual std::pair< secure_vector< uint8_t >, std::vector< uint8_t > > tls_dh_agree(const std::vector< uint8_t > &modulus, const std::vector< uint8_t > &generator, const std::vector< uint8_t > &peer_public_value, const Policy &policy, RandomNumberGenerator &rng)
virtual std::pair< secure_vector< uint8_t >, std::vector< uint8_t > > tls_ecdh_agree(const std::string &curve_name, const std::vector< uint8_t > &peer_public_value, const Policy &policy, RandomNumberGenerator &rng, bool compressed)
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, const std::string &hostname, RandomNumberGenerator &rng)
void server_certs(Certificate *server_certs)
void server_kex(Server_Key_Exchange *server_kex)
Callbacks & callbacks() const
void client_hello(Client_Hello *client_hello)
const Ciphersuite & ciphersuite() const
void server_hello(Server_Hello *server_hello)
virtual bool hide_unknown_users() const
virtual Group_Params choose_key_exchange_group(const std::vector< Group_Params > &peer_groups) 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
std::string to_string(const BER_Object &obj)
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)
bool key_exchange_is_psk(Kex_Algo m)
std::pair< BigInt, SymmetricKey > srp6_client_agree(const std::string &identifier, const std::string &password, const std::string &group_id, const std::string &hash_id, const std::vector< uint8_t > &salt, const BigInt &B, RandomNumberGenerator &rng)
void CECPQ1_finish(uint8_t shared_key[CECPQ1_SHARED_KEY_BYTES], const CECPQ1_key &offer_key, const uint8_t received[CECPQ1_ACCEPT_BYTES])
@ CECPQ1_SHARED_KEY_BYTES
std::string srp6_group_identifier(const BigInt &N, const BigInt &g)
void CECPQ1_accept(uint8_t shared_key[CECPQ1_SHARED_KEY_BYTES], uint8_t send[CECPQ1_ACCEPT_BYTES], const uint8_t received[CECPQ1_OFFER_BYTES], RandomNumberGenerator &rng)
std::vector< T, secure_allocator< T > > secure_vector
std::unique_ptr< Public_Key > server_public_key