9#include <botan/tls_messages_12.h>
11#include <botan/credentials_manager.h>
12#include <botan/dl_group.h>
15#include <botan/tls_callbacks.h>
16#include <botan/tls_extensions.h>
17#include <botan/tls_policy.h>
18#include <botan/internal/ct_utils.h>
19#include <botan/internal/fmt.h>
20#include <botan/internal/stl_util.h>
21#include <botan/internal/tls_handshake_hash.h>
22#include <botan/internal/tls_handshake_io.h>
23#include <botan/internal/tls_handshake_state.h>
24#include <botan/internal/tls_messages_internal.h>
25#include <botan/internal/tls_reader.h>
41 z.erase(z.begin(), z.begin() + leading_zeros);
48std::optional<DL_Group> match_well_known_dh_group(
const BigInt& p,
const BigInt& g) {
49 const size_t p_bits = p.bits();
51 if(p_bits != 2048 && p_bits != 3072 && p_bits != 4096 && p_bits != 6144 && p_bits != 8192) {
55 for(
const char* prefix : {
"ffdhe/ietf",
"modp/ietf"}) {
56 const std::string
name =
fmt(
"{}/{}", prefix, p_bits);
58 if(candidate.get_p() == p && candidate.get_g() == g) {
76 std::string_view hostname,
81 std::string identity_hint;
85 identity_hint = reader.
get_string(2, 0, 65535);
88 m_psk_identity = creds.
psk_identity(
"tls-client", std::string(hostname), identity_hint);
92 const SymmetricKey psk = creds.
psk(
"tls-client", std::string(hostname), m_psk_identity.value());
95 throw TLS_Exception(Alert::InternalError,
"Application did not provide a PSK for the negotiated identity");
98 const std::vector<uint8_t> zeros(psk.
length());
108 const std::string identity_hint = reader.
get_string(2, 0, 65535);
110 m_psk_identity = creds.
psk_identity(
"tls-client", std::string(hostname), identity_hint);
114 psk = creds.
psk(
"tls-client", std::string(hostname), m_psk_identity.value());
117 throw TLS_Exception(Alert::InternalError,
"Application did not provide a PSK for the negotiated identity");
124 const std::vector<uint8_t> peer_public_value = reader.
get_range<uint8_t>(2, 1, 65535);
131 throw TLS_Exception(Alert::InsufficientSecurity,
"DH prime too small for policy");
134 throw TLS_Exception(Alert::IllegalParameter,
"DH prime too large for policy");
137 const auto group = [&] {
138 if(
auto matched = match_well_known_dh_group(modulus, generator)) {
139 return std::move(*matched);
145 DL_Group ad_hoc(modulus, generator);
147 throw TLS_Exception(Alert::InsufficientSecurity,
"DH group validation failed");
156 strip_leading_zeros(m_pre_master);
159 const uint8_t curve_type = reader.
get_byte();
160 if(curve_type != 3) {
165 const std::vector<uint8_t> peer_public_value = reader.
get_range<uint8_t>(1, 1, 255);
169 "Server selected a group that is not compatible with the negotiated ciphersuite");
178 throw TLS_Exception(Alert::IllegalParameter,
"Server selected a curve we did not offer");
182 throw TLS_Exception(Alert::HandshakeFailure,
"Server sent ECC curve prohibited by policy");
185 const auto private_key = [&] {
187 const auto pubkey_point_format = state.
server_hello().prefers_compressed_ec_points()
197 throw TLS_Exception(Alert::InternalError,
"Application did not provide an EC key");
204 m_pre_master = std::move(shared_secret);
215 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange method was negotiated");
223 throw Unexpected_Message(
"No server kex message, but negotiated a key exchange that required it");
226 if(server_public_key ==
nullptr) {
230 if(
const auto* rsa_pub =
dynamic_cast<const RSA_PublicKey*
>(server_public_key)) {
239 const std::vector<uint8_t> encrypted_key = encryptor.
encrypt(m_pre_master, rng);
244 "Expected a RSA key in server cert but got " + server_public_key->
algo_name());
264 "RSA key exchange negotiated so server sent a certificate");
266 if(server_rsa_kex_key ==
nullptr) {
267 throw Internal_Error(
"Expected RSA kex but no server kex key set");
270 if(server_rsa_kex_key->
algo_name() !=
"RSA") {
276 const std::vector<uint8_t> encrypted_pre_master = reader.
get_range<uint8_t>(2, 1, 65535);
281 const uint8_t client_major = state.
client_hello().legacy_version().major_version();
282 const uint8_t client_minor = state.
client_hello().legacy_version().minor_version();
290 const size_t expected_plaintext_size = 48;
291 const size_t expected_content_size = 2;
292 const uint8_t expected_content_bytes[expected_content_size] = {client_major, client_minor};
293 const uint8_t expected_content_pos[expected_content_size] = {0, 1};
305 encrypted_pre_master.size(),
306 expected_plaintext_size,
308 expected_content_bytes,
309 expected_content_pos,
310 expected_content_size);
312 m_pre_master = rng.
random_vec(expected_plaintext_size);
320 m_psk_identity = reader.
get_string(2, 0, 65535);
323 psk = creds.
psk(
"tls-server", state.
client_hello().sni_hostname(), m_psk_identity.value());
333 throw TLS_Exception(Alert::UnknownPSKIdentity,
"No PSK for identifier " + m_psk_identity.value());
340 const std::vector<uint8_t> zeros(psk.
length());
346 const std::vector<uint8_t> client_pubkey = (ka_key.
algo_name() ==
"DH")
350 const auto shared_group = state.
server_kex().shared_group();
358 strip_leading_zeros(shared_secret);
365 m_pre_master = shared_secret;
371 }
catch(std::exception&) {
383 throw Internal_Error(
"Client_Key_Exchange: Unknown key exchange negotiated");
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT(expr, assertion_made)
virtual std::string algo_name() const =0
static BigInt from_bytes(std::span< const uint8_t > bytes)
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)
static DL_Group from_name(std::string_view name)
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
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
virtual std::vector< uint8_t > public_value() const =0
void random_vec(std::span< uint8_t > v)
virtual std::unique_ptr< PK_Key_Agreement_Key > tls12_generate_ephemeral_ecdh_key(TLS::Group_Params group, RandomNumberGenerator &rng, EC_Point_Format tls12_ecc_pubkey_encoding_format)
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)
constexpr bool is_ecdh_named_curve() const
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
void client_hello(std::unique_ptr< Client_Hello_12 > client_hello)
bool has_server_kex() const
Callbacks & callbacks() const
void server_hello(std::unique_ptr< Server_Hello_12 > server_hello)
const Ciphersuite & ciphersuite() const
void server_kex(std::unique_ptr< Server_Key_Exchange > server_kex)
void server_certs(std::unique_ptr< Certificate_12 > server_certs)
virtual size_t maximum_dh_group_size() const
virtual bool hide_unknown_users() const
virtual size_t minimum_dh_group_size() 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
size_t count_leading_zero_bytes(std::span< const uint8_t > input)
bool group_usable_in_version(Group_Params group, Protocol_Version version)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
bool group_may_be_ecc(Group_Params group)
bool key_exchange_is_psk(Kex_Algo m)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
std::string fmt(std::string_view format, const T &... args)
bool value_exists(const std::vector< T > &vec, const V &val)
std::vector< T, secure_allocator< T > > secure_vector