9 #include <botan/tls_messages.h>
10 #include <botan/tls_extensions.h>
11 #include <botan/rng.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>
20 #include <botan/rsa.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);
95 if(reader.remaining_bytes())
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");
208 if(!server_public_key)
211 if(
auto rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key))
221 const std::vector<uint8_t> encrypted_key = encryptor.
encrypt(m_pre_master, rng);
227 "Expected a RSA key in server cert but got " +
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");
254 if(!dynamic_cast<const RSA_PrivateKey*>(server_rsa_kex_key))
258 const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
259 reader.assert_done();
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);
312 std::vector<uint8_t> zeros(psk.
length());
316 #if defined(BOTAN_HAS_SRP6)
324 #if defined(BOTAN_HAS_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());
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);
378 m_pre_master = shared_secret;
384 catch(std::exception&)
398 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange negotiated");
secure_vector< uint8_t > bits_of() const
virtual std::string tls_decode_group_param(Group_Params group_param)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
uint8_t minor_version() const
void CECPQ1_finish(uint8_t shared_key[CECPQ1_SHARED_KEY_BYTES], const CECPQ1_key &offer_key, const uint8_t received[CECPQ1_ACCEPT_BYTES])
void server_hello(Server_Hello *server_hello)
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)
secure_vector< uint8_t > random_vec(size_t bytes)
virtual std::string srp_identifier(const std::string &type, const std::string &context)
virtual std::string srp_password(const std::string &type, const std::string &context, const std::string &identifier)
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)
static std::vector< uint8_t > encode(const BigInt &n)
bool key_exchange_is_psk(Kex_Algo m)
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 server_kex(Server_Key_Exchange *server_kex)
virtual std::string algo_name() const =0
virtual std::vector< uint8_t > public_value() const =0
std::string to_string(const BER_Object &obj)
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
virtual bool hide_unknown_users() const
SymmetricKey step2(const BigInt &A)
#define BOTAN_ASSERT(expr, assertion_made)
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
std::vector< T, secure_allocator< T >> secure_vector
Callbacks & callbacks() const
void client_hello(Client_Hello *client_hello)
virtual std::string psk_identity(const std::string &type, const std::string &context, const std::string &identity_hint)
secure_vector< uint8_t > decrypt_or_random(const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
void update(const uint8_t in[], size_t length)
uint8_t major_version() const
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)
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)
virtual SymmetricKey psk(const std::string &type, const std::string &context, const std::string &identity)
std::unique_ptr< Public_Key > server_public_key
static BigInt decode(const uint8_t buf[], size_t length)
const char * what() const noexceptoverride
void server_certs(Certificate *server_certs)
Kex_Algo kex_method() const
const Ciphersuite & ciphersuite() const
virtual Group_Params choose_key_exchange_group(const std::vector< Group_Params > &peer_groups) const
std::string srp6_group_identifier(const BigInt &N, const BigInt &g)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)