Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::TLS::Client_Key_Exchange Class Referencefinal

#include <tls_messages.h>

Inheritance diagram for Botan::TLS::Client_Key_Exchange:
Botan::TLS::Handshake_Message

Public Member Functions

 Client_Key_Exchange (const std::vector< uint8_t > &buf, const Handshake_State &state, const Private_Key *server_rsa_kex_key, Credentials_Manager &creds, const Policy &policy, RandomNumberGenerator &rng)
 
 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)
 
const secure_vector< uint8_t > & pre_master_secret () const
 
const std::optional< std::string > & psk_identity () const
 
Handshake_Type type () const override
 
std::string type_string () const
 
virtual Handshake_Type wire_type () const
 

Detailed Description

Client Key Exchange Message

Definition at line 485 of file tls_messages.h.

Constructor & Destructor Documentation

◆ Client_Key_Exchange() [1/2]

Botan::TLS::Client_Key_Exchange::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 )

Definition at line 30 of file msg_client_kex.cpp.

36 {
37 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
38
39 if(kex_algo == Kex_Algo::PSK) {
40 std::string identity_hint;
41
42 if(state.server_kex()) {
43 TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
44 identity_hint = reader.get_string(2, 0, 65535);
45 }
46
47 m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
48
49 append_tls_length_value(m_key_material, to_byte_vector(m_psk_identity.value()), 2);
50
51 SymmetricKey psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
52
53 std::vector<uint8_t> zeros(psk.length());
54
55 append_tls_length_value(m_pre_master, zeros, 2);
56 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
57 } else if(state.server_kex()) {
58 TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
59
60 SymmetricKey psk;
61
62 if(kex_algo == Kex_Algo::ECDHE_PSK) {
63 std::string identity_hint = reader.get_string(2, 0, 65535);
64
65 m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
66
67 append_tls_length_value(m_key_material, to_byte_vector(m_psk_identity.value()), 2);
68
69 psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
70 }
71
72 if(kex_algo == Kex_Algo::DH) {
73 const auto modulus = BigInt::decode(reader.get_range<uint8_t>(2, 1, 65535));
74 const auto generator = BigInt::decode(reader.get_range<uint8_t>(2, 1, 65535));
75 const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(2, 1, 65535);
76
77 if(reader.remaining_bytes()) {
78 throw Decoding_Error("Bad params size for DH key exchange");
79 }
80
81 DL_Group group(modulus, generator);
82
83 if(!group.verify_group(rng, false)) {
84 throw TLS_Exception(Alert::InsufficientSecurity, "DH group validation failed");
85 }
86
87 const auto private_key = state.callbacks().tls_generate_ephemeral_key(group, rng);
88 auto shared_secret = CT::strip_leading_zeros(
89 state.callbacks().tls_ephemeral_key_agreement(group, *private_key, peer_public_value, rng, policy));
90
91 if(kex_algo == Kex_Algo::DH) {
92 m_pre_master = std::move(shared_secret);
93 } else {
94 append_tls_length_value(m_pre_master, shared_secret, 2);
95 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
96 }
97
98 append_tls_length_value(m_key_material, private_key->public_value(), 2);
99 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
100 const uint8_t curve_type = reader.get_byte();
101 if(curve_type != 3) {
102 throw Decoding_Error("Server sent non-named ECC curve");
103 }
104
105 const Group_Params curve_id = static_cast<Group_Params>(reader.get_uint16_t());
106 const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(1, 1, 255);
107
108 if(!curve_id.is_ecdh_named_curve() && !curve_id.is_x25519() && !curve_id.is_x448()) {
109 throw TLS_Exception(Alert::HandshakeFailure,
110 "Server selected a group that is not compatible with the negotiated ciphersuite");
111 }
112
113 if(policy.choose_key_exchange_group({curve_id}, {}) != curve_id) {
114 throw TLS_Exception(Alert::HandshakeFailure, "Server sent ECC curve prohibited by policy");
115 }
116
117 const auto private_key = state.callbacks().tls_generate_ephemeral_key(curve_id, rng);
118 auto shared_secret =
119 state.callbacks().tls_ephemeral_key_agreement(curve_id, *private_key, peer_public_value, rng, policy);
120
121 // RFC 8422 - 5.11.
122 // With X25519 and X448, a receiving party MUST check whether the
123 // computed premaster secret is the all-zero value and abort the
124 // handshake if so, as described in Section 6 of [RFC7748].
125 if((curve_id == Group_Params::X25519 || curve_id == Group_Params::X448) &&
126 CT::all_zeros(shared_secret.data(), shared_secret.size()).as_bool()) {
127 throw TLS_Exception(Alert::DecryptError, "Bad X25519 or X448 key exchange");
128 }
129
130 if(kex_algo == Kex_Algo::ECDH) {
131 m_pre_master = std::move(shared_secret);
132 } else {
133 append_tls_length_value(m_pre_master, shared_secret, 2);
134 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
135 }
136
137 if(curve_id.is_ecdh_named_curve()) {
138 auto ecdh_key = dynamic_cast<ECDH_PublicKey*>(private_key.get());
139 if(!ecdh_key) {
140 throw TLS_Exception(Alert::InternalError, "Application did not provide a ECDH_PublicKey");
141 }
142 append_tls_length_value(m_key_material,
143 ecdh_key->public_value(state.server_hello()->prefers_compressed_ec_points()
146 1);
147 } else {
148 append_tls_length_value(m_key_material, private_key->public_value(), 1);
149 }
150 } else {
151 throw Internal_Error("Client_Key_Exchange: Unknown key exchange method was negotiated");
152 }
153
154 reader.assert_done();
155 } else {
156 // No server key exchange msg better mean RSA kex + RSA key in cert
157
158 if(kex_algo != Kex_Algo::STATIC_RSA) {
159 throw Unexpected_Message("No server kex message, but negotiated a key exchange that required it");
160 }
161
162 if(!server_public_key) {
163 throw Internal_Error("No server public key for RSA exchange");
164 }
165
166 if(auto rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key)) {
167 const Protocol_Version offered_version = state.client_hello()->legacy_version();
168
169 rng.random_vec(m_pre_master, 48);
170 m_pre_master[0] = offered_version.major_version();
171 m_pre_master[1] = offered_version.minor_version();
172
173 PK_Encryptor_EME encryptor(*rsa_pub, rng, "PKCS1v15");
174
175 const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
176
177 append_tls_length_value(m_key_material, encrypted_key, 2);
178 } else {
179 throw TLS_Exception(Alert::HandshakeFailure,
180 "Expected a RSA key in server cert but got " + server_public_key->algo_name());
181 }
182 }
183
184 state.hash().update(io.send(*this));
185}
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:773
secure_vector< uint8_t > strip_leading_zeros(const uint8_t in[], size_t length)
Definition ct_utils.cpp:84
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:332
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:180
T to_byte_vector(std::string_view s)
Definition stl_util.h:30
OctetString SymmetricKey
Definition symkey.h:141
EC_Point_Format
Definition ec_point.h:19

References Botan::Asymmetric_Key::algo_name(), Botan::CT::all_zeros(), Botan::TLS::append_tls_length_value(), Botan::TLS::TLS_Data_Reader::assert_done(), Botan::OctetString::bits_of(), Botan::TLS::Handshake_State::callbacks(), Botan::TLS::Policy::choose_key_exchange_group(), Botan::TLS::Handshake_State::ciphersuite(), Botan::TLS::Handshake_State::client_hello(), Botan::Compressed, Botan::BigInt::decode(), Botan::TLS::DH, Botan::TLS::ECDH, Botan::TLS::ECDHE_PSK, Botan::PK_Encryptor::encrypt(), Botan::TLS::TLS_Data_Reader::get_byte(), Botan::TLS::TLS_Data_Reader::get_range(), Botan::TLS::TLS_Data_Reader::get_string(), Botan::TLS::TLS_Data_Reader::get_uint16_t(), Botan::TLS::Handshake_State::hash(), Botan::TLS::Group_Params::is_ecdh_named_curve(), Botan::TLS::Group_Params::is_x25519(), Botan::TLS::Group_Params::is_x448(), Botan::TLS::Ciphersuite::kex_method(), Botan::OctetString::length(), Botan::TLS::Protocol_Version::major_version(), Botan::TLS::Protocol_Version::minor_version(), Botan::Credentials_Manager::psk(), Botan::TLS::PSK, Botan::Credentials_Manager::psk_identity(), Botan::RandomNumberGenerator::random_vec(), Botan::TLS::TLS_Data_Reader::remaining_bytes(), Botan::TLS::Handshake_IO::send(), Botan::TLS::Handshake_State::server_hello(), Botan::TLS::Handshake_State::server_kex(), Botan::TLS::STATIC_RSA, Botan::CT::strip_leading_zeros(), Botan::TLS::Callbacks::tls_ephemeral_key_agreement(), Botan::TLS::Callbacks::tls_generate_ephemeral_key(), Botan::to_byte_vector(), Botan::Uncompressed, Botan::TLS::Handshake_Hash::update(), and Botan::DL_Group::verify_group().

◆ Client_Key_Exchange() [2/2]

Botan::TLS::Client_Key_Exchange::Client_Key_Exchange ( const std::vector< uint8_t > & buf,
const Handshake_State & state,
const Private_Key * server_rsa_kex_key,
Credentials_Manager & creds,
const Policy & policy,
RandomNumberGenerator & rng )

Definition at line 190 of file msg_client_kex.cpp.

195 {
196 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
197
198 if(kex_algo == Kex_Algo::STATIC_RSA) {
199 BOTAN_ASSERT(state.server_certs() && !state.server_certs()->cert_chain().empty(),
200 "RSA key exchange negotiated so server sent a certificate");
201
202 if(!server_rsa_kex_key) {
203 throw Internal_Error("Expected RSA kex but no server kex key set");
204 }
205
206 if(server_rsa_kex_key->algo_name() != "RSA") {
207 throw Internal_Error("Expected RSA key but got " + server_rsa_kex_key->algo_name());
208 }
209
210 TLS_Data_Reader reader("ClientKeyExchange", contents);
211 const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
212 reader.assert_done();
213
214 PK_Decryptor_EME decryptor(*server_rsa_kex_key, rng, "PKCS1v15");
215
216 const uint8_t client_major = state.client_hello()->legacy_version().major_version();
217 const uint8_t client_minor = state.client_hello()->legacy_version().minor_version();
218
219 /*
220 * PK_Decryptor::decrypt_or_random will return a random value if
221 * either the length does not match the expected value or if the
222 * version number embedded in the PMS does not match the one sent
223 * in the client hello.
224 */
225 const size_t expected_plaintext_size = 48;
226 const size_t expected_content_size = 2;
227 const uint8_t expected_content_bytes[expected_content_size] = {client_major, client_minor};
228 const uint8_t expected_content_pos[expected_content_size] = {0, 1};
229
230 m_pre_master = decryptor.decrypt_or_random(encrypted_pre_master.data(),
231 encrypted_pre_master.size(),
232 expected_plaintext_size,
233 rng,
234 expected_content_bytes,
235 expected_content_pos,
236 expected_content_size);
237 } else {
238 TLS_Data_Reader reader("ClientKeyExchange", contents);
239
240 SymmetricKey psk;
241
242 if(key_exchange_is_psk(kex_algo)) {
243 m_psk_identity = reader.get_string(2, 0, 65535);
244
245 psk = creds.psk("tls-server", state.client_hello()->sni_hostname(), m_psk_identity.value());
246
247 if(psk.length() == 0) {
248 if(policy.hide_unknown_users()) {
249 psk = SymmetricKey(rng, 16);
250 } else {
251 throw TLS_Exception(Alert::UnknownPSKIdentity, "No PSK for identifier " + m_psk_identity.value());
252 }
253 }
254 }
255
256 if(kex_algo == Kex_Algo::PSK) {
257 std::vector<uint8_t> zeros(psk.length());
258 append_tls_length_value(m_pre_master, zeros, 2);
259 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
260 } else if(kex_algo == Kex_Algo::DH || kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
261 const PK_Key_Agreement_Key& ka_key = state.server_kex()->server_kex_key();
262
263 const std::vector<uint8_t> client_pubkey = (ka_key.algo_name() == "DH")
264 ? reader.get_range<uint8_t>(2, 0, 65535)
265 : reader.get_range<uint8_t>(1, 1, 255);
266
267 const auto shared_group = state.server_kex()->shared_group();
268 BOTAN_STATE_CHECK(shared_group && shared_group.value() != Group_Params::NONE);
269
270 try {
271 auto shared_secret =
272 state.callbacks().tls_ephemeral_key_agreement(shared_group.value(), ka_key, client_pubkey, rng, policy);
273
274 if(ka_key.algo_name() == "DH") {
275 shared_secret = CT::strip_leading_zeros(shared_secret);
276 }
277
278 if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
279 // RFC 8422 - 5.11.
280 // With X25519 and X448, a receiving party MUST check whether the
281 // computed premaster secret is the all-zero value and abort the
282 // handshake if so, as described in Section 6 of [RFC7748].
283 BOTAN_ASSERT_NOMSG(state.server_kex()->params().size() >= 3);
284 Group_Params group = static_cast<Group_Params>(state.server_kex()->params().at(2));
285 if((group == Group_Params::X25519 || group == Group_Params::X448) &&
286 CT::all_zeros(shared_secret.data(), shared_secret.size()).as_bool()) {
287 throw TLS_Exception(Alert::DecryptError, "Bad X25519 or X448 key exchange");
288 }
289 }
290
291 if(kex_algo == Kex_Algo::ECDHE_PSK) {
292 append_tls_length_value(m_pre_master, shared_secret, 2);
293 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
294 } else {
295 m_pre_master = shared_secret;
296 }
297 } catch(Invalid_Argument& e) {
298 throw TLS_Exception(Alert::IllegalParameter, e.what());
299 } catch(TLS_Exception& e) {
300 // NOLINTNEXTLINE(cert-err60-cpp)
301 throw e;
302 } catch(std::exception&) {
303 /*
304 * Something failed in the DH/ECDH computation. To avoid possible
305 * attacks which are based on triggering and detecting some edge
306 * failure condition, randomize the pre-master output and carry on,
307 * allowing the protocol to fail later in the finished checks.
308 */
309 rng.random_vec(m_pre_master, ka_key.public_value().size());
310 }
311
312 reader.assert_done();
313 } else {
314 throw Internal_Error("Client_Key_Exchange: Unknown key exchange negotiated");
315 }
316 }
317}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
bool key_exchange_is_psk(Kex_Algo m)
Definition tls_algos.h:264

References Botan::Asymmetric_Key::algo_name(), Botan::CT::all_zeros(), Botan::TLS::append_tls_length_value(), Botan::TLS::TLS_Data_Reader::assert_done(), Botan::OctetString::bits_of(), BOTAN_ASSERT, BOTAN_ASSERT_NOMSG, BOTAN_STATE_CHECK, Botan::TLS::Handshake_State::callbacks(), Botan::TLS::Handshake_State::ciphersuite(), Botan::TLS::Handshake_State::client_hello(), Botan::PK_Decryptor::decrypt_or_random(), Botan::TLS::DH, Botan::TLS::ECDH, Botan::TLS::ECDHE_PSK, Botan::TLS::TLS_Data_Reader::get_range(), Botan::TLS::TLS_Data_Reader::get_string(), Botan::TLS::Policy::hide_unknown_users(), Botan::TLS::Ciphersuite::kex_method(), Botan::TLS::key_exchange_is_psk(), Botan::OctetString::length(), Botan::Credentials_Manager::psk(), Botan::TLS::PSK, Botan::PK_Key_Agreement_Key::public_value(), Botan::RandomNumberGenerator::random_vec(), Botan::TLS::Handshake_State::server_certs(), Botan::TLS::Handshake_State::server_kex(), Botan::TLS::STATIC_RSA, Botan::CT::strip_leading_zeros(), Botan::TLS::Callbacks::tls_ephemeral_key_agreement(), and Botan::Exception::what().

Member Function Documentation

◆ pre_master_secret()

const secure_vector< uint8_t > & Botan::TLS::Client_Key_Exchange::pre_master_secret ( ) const
inline

Definition at line 489 of file tls_messages.h.

489{ return m_pre_master; }

◆ psk_identity()

const std::optional< std::string > & Botan::TLS::Client_Key_Exchange::psk_identity ( ) const
inline
Returns
the agreed upon PSK identity or std::nullopt if not applicable

Definition at line 494 of file tls_messages.h.

494{ return m_psk_identity; }

◆ type()

Handshake_Type Botan::TLS::Client_Key_Exchange::type ( ) const
inlineoverridevirtual
Returns
the message type

Implements Botan::TLS::Handshake_Message.

Definition at line 487 of file tls_messages.h.

◆ type_string()

std::string Botan::TLS::Handshake_Message::type_string ( ) const
inherited
Returns
string representation of this message type

Definition at line 19 of file tls_handshake_state.cpp.

19 {
21}
virtual Handshake_Type type() const =0
const char * handshake_type_to_string(Handshake_Type type)

References Botan::TLS::handshake_type_to_string(), and Botan::TLS::Handshake_Message::type().

◆ wire_type()

virtual Handshake_Type Botan::TLS::Handshake_Message::wire_type ( ) const
inlinevirtualinherited
Returns
the wire representation of the message's type

Reimplemented in Botan::TLS::Hello_Retry_Request.

Definition at line 39 of file tls_handshake_msg.h.

39 {
40 // Usually equal to the Handshake_Type enum value,
41 // with the exception of TLS 1.3 Hello Retry Request.
42 return type();
43 }

Referenced by Botan::TLS::Stream_Handshake_IO::send().


The documentation for this class was generated from the following files: