8 #include <botan/tls_messages.h>
9 #include <botan/tls_extensions.h>
10 #include <botan/internal/tls_reader.h>
11 #include <botan/internal/tls_handshake_io.h>
12 #include <botan/internal/tls_handshake_state.h>
13 #include <botan/internal/tls_handshake_hash.h>
14 #include <botan/credentials_manager.h>
15 #include <botan/rng.h>
16 #include <botan/loadstor.h>
17 #include <botan/internal/ct_utils.h>
19 #include <botan/pubkey.h>
22 #include <botan/ecdh.h>
23 #include <botan/rsa.h>
25 #if defined(BOTAN_HAS_CURVE_25519)
26 #include <botan/curve25519.h>
29 #if defined(BOTAN_HAS_CECPQ1)
30 #include <botan/cecpq1.h>
33 #if defined(BOTAN_HAS_SRP6)
34 #include <botan/srp6.h>
49 const std::string& hostname,
56 std::string identity_hint =
"";
61 identity_hint = reader.
get_string(2, 0, 65535);
64 const std::string psk_identity =
65 creds.
psk_identity(
"tls-client", hostname, identity_hint);
71 std::vector<uint8_t> zeros(psk.
length());
82 if(kex_algo ==
"DHE_PSK" || kex_algo ==
"ECDHE_PSK")
84 std::string identity_hint = reader.get_string(2, 0, 65535);
86 const std::string psk_identity =
87 creds.
psk_identity(
"tls-client", hostname, identity_hint);
91 psk = creds.
psk(
"tls-client", hostname, psk_identity);
94 if(kex_algo ==
"DH" || kex_algo ==
"DHE_PSK")
100 if(reader.remaining_bytes())
109 if(Y <= 1 || Y >= p - 1)
111 "Server sent bad DH key for DHE exchange");
117 "DH group validation failed");
131 m_pre_master = dh_secret;
140 else if(kex_algo ==
"ECDH" || kex_algo ==
"ECDHE_PSK")
142 const uint8_t curve_type = reader.get_byte();
147 const uint16_t curve_id = reader.get_uint16_t();
157 "Server sent ECC curve prohibited by policy");
160 const std::vector<uint8_t> ecdh_key = reader.get_range<uint8_t>(1, 1, 255);
161 std::vector<uint8_t> our_ecdh_public;
164 if(curve_name ==
"x25519")
166 #if defined(BOTAN_HAS_CURVE_25519)
167 if(ecdh_key.size() != 32)
179 throw Internal_Error(
"Negotiated X25519 somehow, but it is disabled");
196 if(kex_algo ==
"ECDH")
197 m_pre_master = ecdh_secret;
206 #if defined(BOTAN_HAS_SRP6)
207 else if(kex_algo ==
"SRP_SHA")
211 std::vector<uint8_t> salt = reader.get_range<uint8_t>(1, 1, 255);
216 const std::string srp_identifier =
219 const std::string srp_password =
220 creds.
srp_password(
"tls-client", hostname, srp_identifier);
222 std::pair<BigInt, SymmetricKey> srp_vals =
232 m_pre_master = srp_vals.second.bits_of();
236 #if defined(BOTAN_HAS_CECPQ1)
237 else if(kex_algo ==
"CECPQ1")
239 const std::vector<uint8_t> cecpq1_offer = reader.get_range<uint8_t>(2, 1, 65535);
246 CECPQ1_accept(shared_secret.data(), newhope_accept.data(), cecpq1_offer.data(), rng);
248 m_pre_master = shared_secret;
253 throw Internal_Error(
"Client_Key_Exchange: Unknown kex " + kex_algo);
256 reader.assert_done();
262 if(kex_algo !=
"RSA")
265 if(!server_public_key)
268 if(
auto rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key))
278 const std::vector<uint8_t> encrypted_key = encryptor.
encrypt(m_pre_master, rng);
284 "Expected a RSA key in server cert but got " +
303 if(kex_algo ==
"RSA")
306 "RSA key exchange negotiated so server sent a certificate");
308 if(!server_rsa_kex_key)
309 throw Internal_Error(
"Expected RSA kex but no server kex key set");
311 if(!dynamic_cast<const RSA_PrivateKey*>(server_rsa_kex_key))
315 const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
319 const uint8_t client_major = state.
client_hello()->version().major_version();
320 const uint8_t client_minor = state.
client_hello()->version().minor_version();
328 const size_t expected_plaintext_size = 48;
329 const size_t expected_content_size = 2;
330 const uint8_t expected_content_bytes[expected_content_size] = { client_major, client_minor };
331 const uint8_t expected_content_pos[expected_content_size] = { 0, 1 };
334 decryptor.decrypt_or_random(encrypted_pre_master.data(),
335 encrypted_pre_master.size(),
336 expected_plaintext_size,
338 expected_content_bytes,
339 expected_content_pos,
340 expected_content_size);
348 if(kex_algo ==
"PSK" || kex_algo ==
"DHE_PSK" || kex_algo ==
"ECDHE_PSK")
350 const std::string psk_identity = reader.
get_string(2, 0, 65535);
352 psk = creds.
psk(
"tls-server",
362 "No PSK for identifier " + psk_identity);
366 if(kex_algo ==
"PSK")
368 std::vector<uint8_t> zeros(psk.
length());
372 #if defined(BOTAN_HAS_SRP6)
373 else if(kex_algo ==
"SRP_SHA")
380 #if defined(BOTAN_HAS_CECPQ1)
381 else if(kex_algo ==
"CECPQ1")
385 const std::vector<uint8_t> cecpq1_accept = reader.
get_range<uint8_t>(2, 0, 65535);
390 CECPQ1_finish(m_pre_master.data(), cecpq1_offer, cecpq1_accept.data());
393 else if(kex_algo ==
"DH" || kex_algo ==
"DHE_PSK" ||
394 kex_algo ==
"ECDH" || kex_algo ==
"ECDHE_PSK")
409 std::vector<uint8_t> client_pubkey;
412 client_pubkey = reader.
get_range<uint8_t>(2, 0, 65535);
414 client_pubkey = reader.
get_range<uint8_t>(1, 0, 255);
421 if(kex_algo ==
"DHE_PSK" || kex_algo ==
"ECDHE_PSK")
427 m_pre_master = shared_secret;
429 catch(std::exception &)
441 throw Internal_Error(
"Client_Key_Exchange: Unknown kex type " + kex_algo);
secure_vector< uint8_t > bits_of() const
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)
std::vector< uint8_t > public_value() const
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)
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)
bool verify_group(RandomNumberGenerator &rng, bool strong) const
virtual std::string algo_name() const =0
virtual std::vector< uint8_t > public_value() const =0
std::vector< uint8_t > public_value() const
std::string to_string(const BER_Object &obj)
virtual bool hide_unknown_users() const
SymmetricKey step2(const BigInt &A)
#define BOTAN_ASSERT(expr, assertion_made)
static std::string curve_id_to_name(uint16_t id)
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
const CurveGFp & get_curve() const
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
std::vector< T, secure_allocator< T >> secure_vector
bool allowed_ecc_curve(const std::string &curve) const
std::vector< uint8_t > public_value() const override
void client_hello(Client_Hello *client_hello)
std::string kex_algo() const
virtual std::string psk_identity(const std::string &type, const std::string &context, const std::string &identity_hint)
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
PointGFp OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
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::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)
std::unique_ptr< Public_Key > server_public_key
std::vector< uint8_t > public_value() const
void server_certs(Certificate *server_certs)
std::vector< uint8_t > public_value() const override
virtual void check_peer_key_acceptable(const Public_Key &public_key) const
const Ciphersuite & ciphersuite() const
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
std::vector< uint8_t > public_value() const override
std::string srp6_group_identifier(const BigInt &N, const BigInt &g)
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
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)