Botan 3.10.0
Crypto and TLS for C&
msg_client_kex.cpp
Go to the documentation of this file.
1/*
2* Client Key Exchange Message
3* (C) 2004-2010,2016 Jack Lloyd
4* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/tls_messages.h>
10
11#include <botan/credentials_manager.h>
12#include <botan/rng.h>
13#include <botan/rsa.h>
14#include <botan/tls_extensions.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/tls_handshake_hash.h>
17#include <botan/internal/tls_handshake_io.h>
18#include <botan/internal/tls_handshake_state.h>
19#include <botan/internal/tls_reader.h>
20
21namespace Botan::TLS {
22
23/*
24* Create a new Client Key Exchange message
25*/
27 Handshake_State& state,
28 const Policy& policy,
30 const Public_Key* server_public_key,
31 std::string_view hostname,
33 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
34
35 if(kex_algo == Kex_Algo::PSK) {
36 std::string identity_hint;
37
38 if(state.server_kex() != nullptr) {
39 TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
40 identity_hint = reader.get_string(2, 0, 65535);
41 }
42
43 m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
44
45 append_tls_length_value(m_key_material, as_span_of_bytes(m_psk_identity.value()), 2);
46
47 SymmetricKey psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
48
49 std::vector<uint8_t> zeros(psk.length());
50
51 append_tls_length_value(m_pre_master, zeros, 2);
52 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
53 } else if(state.server_kex() != nullptr) {
54 TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
55
56 SymmetricKey psk;
57
58 if(kex_algo == Kex_Algo::ECDHE_PSK) {
59 std::string identity_hint = reader.get_string(2, 0, 65535);
60
61 m_psk_identity = creds.psk_identity("tls-client", std::string(hostname), identity_hint);
62
63 append_tls_length_value(m_key_material, as_span_of_bytes(m_psk_identity.value()), 2);
64
65 psk = creds.psk("tls-client", std::string(hostname), m_psk_identity.value());
66 }
67
68 if(kex_algo == Kex_Algo::DH) {
69 const auto modulus = BigInt::from_bytes(reader.get_range<uint8_t>(2, 1, 65535));
70 const auto generator = BigInt::from_bytes(reader.get_range<uint8_t>(2, 1, 65535));
71 const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(2, 1, 65535);
72
73 if(reader.remaining_bytes() > 0) {
74 throw Decoding_Error("Bad params size for DH key exchange");
75 }
76
77 DL_Group group(modulus, generator);
78
79 if(!group.verify_group(rng, false)) {
80 throw TLS_Exception(Alert::InsufficientSecurity, "DH group validation failed");
81 }
82
83 const auto private_key = state.callbacks().tls_generate_ephemeral_key(group, rng);
84 m_pre_master = CT::strip_leading_zeros(
85 state.callbacks().tls_ephemeral_key_agreement(group, *private_key, peer_public_value, rng, policy));
86 append_tls_length_value(m_key_material, private_key->public_value(), 2);
87 } else if(kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
88 const uint8_t curve_type = reader.get_byte();
89 if(curve_type != 3) {
90 throw Decoding_Error("Server sent non-named ECC curve");
91 }
92
93 const Group_Params curve_id = static_cast<Group_Params>(reader.get_uint16_t());
94 const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(1, 1, 255);
95
96 if(!curve_id.is_ecdh_named_curve() && !curve_id.is_x25519() && !curve_id.is_x448()) {
97 throw TLS_Exception(Alert::IllegalParameter,
98 "Server selected a group that is not compatible with the negotiated ciphersuite");
99 }
100
101 if(policy.choose_key_exchange_group({curve_id}, {}) != curve_id) {
102 throw TLS_Exception(Alert::HandshakeFailure, "Server sent ECC curve prohibited by policy");
103 }
104
105 const auto private_key = [&] {
106 if(curve_id.is_ecdh_named_curve()) {
107 const auto pubkey_point_format = state.server_hello()->prefers_compressed_ec_points()
110 return state.callbacks().tls12_generate_ephemeral_ecdh_key(curve_id, rng, pubkey_point_format);
111 } else {
112 return state.callbacks().tls_generate_ephemeral_key(curve_id, rng);
113 }
114 }();
115
116 if(!private_key) {
117 throw TLS_Exception(Alert::InternalError, "Application did not provide an EC key");
118 }
119
120 auto shared_secret =
121 state.callbacks().tls_ephemeral_key_agreement(curve_id, *private_key, peer_public_value, rng, policy);
122
123 if(kex_algo == Kex_Algo::ECDH) {
124 m_pre_master = std::move(shared_secret);
125 } else {
126 append_tls_length_value(m_pre_master, shared_secret, 2);
127 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
128 }
129
130 // Note: In contrast to public_value(), raw_public_key_bits() takes the
131 // point format (compressed vs. uncompressed) into account that was set
132 // in its construction within tls_generate_ephemeral_key().
133 append_tls_length_value(m_key_material, private_key->raw_public_key_bits(), 1);
134 } else {
135 throw Internal_Error("Client_Key_Exchange: Unknown key exchange method was negotiated");
136 }
137
138 reader.assert_done();
139 } else {
140 // No server key exchange msg better mean RSA kex + RSA key in cert
141
142 if(kex_algo != Kex_Algo::STATIC_RSA) {
143 throw Unexpected_Message("No server kex message, but negotiated a key exchange that required it");
144 }
145
146 if(server_public_key == nullptr) {
147 throw Internal_Error("No server public key for RSA exchange");
148 }
149
150 if(const auto* rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key)) {
151 const Protocol_Version offered_version = state.client_hello()->legacy_version();
152
153 rng.random_vec(m_pre_master, 48);
154 m_pre_master[0] = offered_version.major_version();
155 m_pre_master[1] = offered_version.minor_version();
156
157 PK_Encryptor_EME encryptor(*rsa_pub, rng, "PKCS1v15");
158
159 const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
160
161 append_tls_length_value(m_key_material, encrypted_key, 2);
162 } else {
163 throw TLS_Exception(Alert::HandshakeFailure,
164 "Expected a RSA key in server cert but got " + server_public_key->algo_name());
165 }
166 }
167
168 state.hash().update(io.send(*this));
169}
170
171/*
172* Read a Client Key Exchange message
173*/
174Client_Key_Exchange::Client_Key_Exchange(const std::vector<uint8_t>& contents,
175 const Handshake_State& state,
176 const Private_Key* server_rsa_kex_key,
177 Credentials_Manager& creds,
178 const Policy& policy,
180 const Kex_Algo kex_algo = state.ciphersuite().kex_method();
181
182 if(kex_algo == Kex_Algo::STATIC_RSA) {
183 BOTAN_ASSERT(state.server_certs() && !state.server_certs()->cert_chain().empty(),
184 "RSA key exchange negotiated so server sent a certificate");
185
186 if(server_rsa_kex_key == nullptr) {
187 throw Internal_Error("Expected RSA kex but no server kex key set");
188 }
189
190 if(server_rsa_kex_key->algo_name() != "RSA") {
191 throw Internal_Error("Expected RSA key but got " + server_rsa_kex_key->algo_name());
192 }
193
194 TLS_Data_Reader reader("ClientKeyExchange", contents);
195 const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
196 reader.assert_done();
197
198 PK_Decryptor_EME decryptor(*server_rsa_kex_key, rng, "PKCS1v15");
199
200 const uint8_t client_major = state.client_hello()->legacy_version().major_version();
201 const uint8_t client_minor = state.client_hello()->legacy_version().minor_version();
202
203 /*
204 * PK_Decryptor::decrypt_or_random will return a random value if
205 * either the length does not match the expected value or if the
206 * version number embedded in the PMS does not match the one sent
207 * in the client hello.
208 */
209 const size_t expected_plaintext_size = 48;
210 const size_t expected_content_size = 2;
211 const uint8_t expected_content_bytes[expected_content_size] = {client_major, client_minor};
212 const uint8_t expected_content_pos[expected_content_size] = {0, 1};
213
214 m_pre_master = decryptor.decrypt_or_random(encrypted_pre_master.data(),
215 encrypted_pre_master.size(),
216 expected_plaintext_size,
217 rng,
218 expected_content_bytes,
219 expected_content_pos,
220 expected_content_size);
221 } else {
222 TLS_Data_Reader reader("ClientKeyExchange", contents);
223
224 SymmetricKey psk;
225
226 if(key_exchange_is_psk(kex_algo)) {
227 m_psk_identity = reader.get_string(2, 0, 65535);
228
229 psk = creds.psk("tls-server", state.client_hello()->sni_hostname(), m_psk_identity.value());
230
231 if(psk.empty()) {
232 if(policy.hide_unknown_users()) {
233 psk = SymmetricKey(rng, 16);
234 } else {
235 throw TLS_Exception(Alert::UnknownPSKIdentity, "No PSK for identifier " + m_psk_identity.value());
236 }
237 }
238 }
239
240 if(kex_algo == Kex_Algo::PSK) {
241 std::vector<uint8_t> zeros(psk.length());
242 append_tls_length_value(m_pre_master, zeros, 2);
243 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
244 } else if(kex_algo == Kex_Algo::DH || kex_algo == Kex_Algo::ECDH || kex_algo == Kex_Algo::ECDHE_PSK) {
245 const PK_Key_Agreement_Key& ka_key = state.server_kex()->server_kex_key();
246
247 const std::vector<uint8_t> client_pubkey = (ka_key.algo_name() == "DH")
248 ? reader.get_range<uint8_t>(2, 0, 65535)
249 : reader.get_range<uint8_t>(1, 1, 255);
250
251 const auto shared_group = state.server_kex()->shared_group();
252 BOTAN_STATE_CHECK(shared_group && shared_group.value() != Group_Params::NONE);
253
254 try {
255 auto shared_secret =
256 state.callbacks().tls_ephemeral_key_agreement(shared_group.value(), ka_key, client_pubkey, rng, policy);
257
258 if(ka_key.algo_name() == "DH") {
259 shared_secret = CT::strip_leading_zeros(shared_secret);
260 }
261
262 if(kex_algo == Kex_Algo::ECDHE_PSK) {
263 append_tls_length_value(m_pre_master, shared_secret, 2);
264 append_tls_length_value(m_pre_master, psk.bits_of(), 2);
265 } else {
266 m_pre_master = shared_secret;
267 }
268 } catch(Invalid_Argument& e) {
269 throw TLS_Exception(Alert::IllegalParameter, e.what());
270 } catch(TLS_Exception& e) {
271 // NOLINTNEXTLINE(cert-err60-cpp)
272 throw e;
273 } catch(std::exception&) {
274 /*
275 * Something failed in the DH/ECDH computation. To avoid possible
276 * attacks which are based on triggering and detecting some edge
277 * failure condition, randomize the pre-master output and carry on,
278 * allowing the protocol to fail later in the finished checks.
279 */
280 rng.random_vec(m_pre_master, ka_key.public_value().size());
281 }
282
283 reader.assert_done();
284 } else {
285 throw Internal_Error("Client_Key_Exchange: Unknown key exchange negotiated");
286 }
287 }
288}
289
290} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
virtual std::string algo_name() const =0
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:87
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)
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
Definition dl_group.cpp:421
const char * what() const noexcept override
Definition exceptn.h:93
secure_vector< uint8_t > bits_of() const
Definition symkey.h:36
size_t length() const
Definition symkey.h:27
bool empty() const
Definition symkey.h:31
secure_vector< uint8_t > decrypt_or_random(const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
Definition pubkey.cpp:87
std::vector< uint8_t > encrypt(const uint8_t in[], size_t length, RandomNumberGenerator &rng) const
Definition pubkey.h:37
virtual std::vector< uint8_t > public_value() const =0
void random_vec(std::span< uint8_t > v)
Definition rng.h:199
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
Definition tls_algos.h:176
constexpr bool is_x448() const
Definition tls_algos.h:174
constexpr bool is_x25519() const
Definition tls_algos.h:172
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)
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 bool hide_unknown_users() 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
Definition tls_version.h:90
uint8_t minor_version() const
Definition tls_version.h:95
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
Definition tls_reader.h:123
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:110
size_t remaining_bytes() const
Definition tls_reader.h:38
secure_vector< uint8_t > strip_leading_zeros(std::span< const uint8_t > input)
Definition ct_utils.cpp:94
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:184
bool key_exchange_is_psk(Kex_Algo m)
Definition tls_algos.h:277
OctetString SymmetricKey
Definition symkey.h:140
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28