1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* Client Key Exchange Message
* (C) 2004-2010,2016 Jack Lloyd
* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/tls_messages.h>
#include <botan/tls_extensions.h>
#include <botan/rng.h>
#include <botan/internal/tls_reader.h>
#include <botan/internal/tls_handshake_io.h>
#include <botan/internal/tls_handshake_state.h>
#include <botan/internal/tls_handshake_hash.h>
#include <botan/credentials_manager.h>
#include <botan/internal/ct_utils.h>
#include <botan/rsa.h>
#if defined(BOTAN_HAS_CECPQ1)
#include <botan/cecpq1.h>
#endif
namespace Botan::TLS {
/*
* Create a new Client Key Exchange message
*/
Client_Key_Exchange::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)
{
const Kex_Algo kex_algo = state.ciphersuite().kex_method();
if(kex_algo == Kex_Algo::PSK)
{
std::string identity_hint = "";
if(state.server_kex())
{
TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
identity_hint = reader.get_string(2, 0, 65535);
}
const std::string psk_identity =
creds.psk_identity("tls-client", hostname, identity_hint);
append_tls_length_value(m_key_material, psk_identity, 2);
SymmetricKey psk = creds.psk("tls-client", hostname, psk_identity);
std::vector<uint8_t> zeros(psk.length());
append_tls_length_value(m_pre_master, zeros, 2);
append_tls_length_value(m_pre_master, psk.bits_of(), 2);
}
else if(state.server_kex())
{
TLS_Data_Reader reader("ClientKeyExchange", state.server_kex()->params());
SymmetricKey psk;
if(kex_algo == Kex_Algo::ECDHE_PSK)
{
std::string identity_hint = reader.get_string(2, 0, 65535);
const std::string psk_identity =
creds.psk_identity("tls-client", hostname, identity_hint);
append_tls_length_value(m_key_material, psk_identity, 2);
psk = creds.psk("tls-client", hostname, psk_identity);
}
if(kex_algo == Kex_Algo::DH)
{
const std::vector<uint8_t> modulus = reader.get_range<uint8_t>(2, 1, 65535);
const std::vector<uint8_t> generator = reader.get_range<uint8_t>(2, 1, 65535);
const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(2, 1, 65535);
if(reader.remaining_bytes())
throw Decoding_Error("Bad params size for DH key exchange");
const std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> dh_result =
state.callbacks().tls_dh_agree(modulus, generator, peer_public_value, policy, rng);
if(kex_algo == Kex_Algo::DH)
m_pre_master = dh_result.first;
else
{
append_tls_length_value(m_pre_master, dh_result.first, 2);
append_tls_length_value(m_pre_master, psk.bits_of(), 2);
}
append_tls_length_value(m_key_material, dh_result.second, 2);
}
else if(kex_algo == Kex_Algo::ECDH ||
kex_algo == Kex_Algo::ECDHE_PSK)
{
const uint8_t curve_type = reader.get_byte();
if(curve_type != 3)
throw Decoding_Error("Server sent non-named ECC curve");
const Group_Params curve_id = static_cast<Group_Params>(reader.get_uint16_t());
const std::vector<uint8_t> peer_public_value = reader.get_range<uint8_t>(1, 1, 255);
if(policy.choose_key_exchange_group({curve_id}) != curve_id)
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server sent ECC curve prohibited by policy");
}
const std::string curve_name = state.callbacks().tls_decode_group_param(curve_id);
if(curve_name.empty())
throw Decoding_Error("Server sent unknown named curve " +
std::to_string(static_cast<uint16_t>(curve_id)));
const std::pair<secure_vector<uint8_t>, std::vector<uint8_t>> ecdh_result =
state.callbacks().tls_ecdh_agree(curve_name, peer_public_value, policy, rng,
state.server_hello()->prefers_compressed_ec_points());
if(kex_algo == Kex_Algo::ECDH)
{
m_pre_master = ecdh_result.first;
}
else
{
append_tls_length_value(m_pre_master, ecdh_result.first, 2);
append_tls_length_value(m_pre_master, psk.bits_of(), 2);
}
append_tls_length_value(m_key_material, ecdh_result.second, 1);
}
#if defined(BOTAN_HAS_CECPQ1)
else if(kex_algo == Kex_Algo::CECPQ1)
{
const std::vector<uint8_t> cecpq1_offer = reader.get_range<uint8_t>(2, 1, 65535);
if(cecpq1_offer.size() != CECPQ1_OFFER_BYTES)
throw TLS_Exception(Alert::HANDSHAKE_FAILURE, "Invalid CECPQ1 key size");
std::vector<uint8_t> newhope_accept(CECPQ1_ACCEPT_BYTES);
secure_vector<uint8_t> shared_secret(CECPQ1_SHARED_KEY_BYTES);
CECPQ1_accept(shared_secret.data(), newhope_accept.data(), cecpq1_offer.data(), rng);
append_tls_length_value(m_key_material, newhope_accept, 2);
m_pre_master = shared_secret;
}
#endif
else
{
throw Internal_Error("Client_Key_Exchange: Unknown key exchange method was negotiated");
}
reader.assert_done();
}
else
{
// No server key exchange msg better mean RSA kex + RSA key in cert
if(kex_algo != Kex_Algo::STATIC_RSA)
throw Unexpected_Message("No server kex message, but negotiated a key exchange that required it");
if(!server_public_key)
throw Internal_Error("No server public key for RSA exchange");
if(auto rsa_pub = dynamic_cast<const RSA_PublicKey*>(server_public_key))
{
const Protocol_Version offered_version = state.client_hello()->version();
rng.random_vec(m_pre_master, 48);
m_pre_master[0] = offered_version.major_version();
m_pre_master[1] = offered_version.minor_version();
PK_Encryptor_EME encryptor(*rsa_pub, rng, "PKCS1v15");
const std::vector<uint8_t> encrypted_key = encryptor.encrypt(m_pre_master, rng);
append_tls_length_value(m_key_material, encrypted_key, 2);
}
else
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Expected a RSA key in server cert but got " +
server_public_key->algo_name());
}
state.hash().update(io.send(*this));
}
/*
* Read a Client Key Exchange message
*/
Client_Key_Exchange::Client_Key_Exchange(const std::vector<uint8_t>& contents,
const Handshake_State& state,
const Private_Key* server_rsa_kex_key,
Credentials_Manager& creds,
const Policy& policy,
RandomNumberGenerator& rng)
{
const Kex_Algo kex_algo = state.ciphersuite().kex_method();
if(kex_algo == Kex_Algo::STATIC_RSA)
{
BOTAN_ASSERT(state.server_certs() && !state.server_certs()->cert_chain().empty(),
"RSA key exchange negotiated so server sent a certificate");
if(!server_rsa_kex_key)
throw Internal_Error("Expected RSA kex but no server kex key set");
if(server_rsa_kex_key->algo_name() != "RSA")
throw Internal_Error("Expected RSA key but got " + server_rsa_kex_key->algo_name());
TLS_Data_Reader reader("ClientKeyExchange", contents);
const std::vector<uint8_t> encrypted_pre_master = reader.get_range<uint8_t>(2, 0, 65535);
reader.assert_done();
PK_Decryptor_EME decryptor(*server_rsa_kex_key, rng, "PKCS1v15");
const uint8_t client_major = state.client_hello()->version().major_version();
const uint8_t client_minor = state.client_hello()->version().minor_version();
/*
* PK_Decryptor::decrypt_or_random will return a random value if
* either the length does not match the expected value or if the
* version number embedded in the PMS does not match the one sent
* in the client hello.
*/
const size_t expected_plaintext_size = 48;
const size_t expected_content_size = 2;
const uint8_t expected_content_bytes[expected_content_size] = { client_major, client_minor };
const uint8_t expected_content_pos[expected_content_size] = { 0, 1 };
m_pre_master =
decryptor.decrypt_or_random(encrypted_pre_master.data(),
encrypted_pre_master.size(),
expected_plaintext_size,
rng,
expected_content_bytes,
expected_content_pos,
expected_content_size);
}
else
{
TLS_Data_Reader reader("ClientKeyExchange", contents);
SymmetricKey psk;
if(key_exchange_is_psk(kex_algo))
{
const std::string psk_identity = reader.get_string(2, 0, 65535);
psk = creds.psk("tls-server",
state.client_hello()->sni_hostname(),
psk_identity);
if(psk.length() == 0)
{
if(policy.hide_unknown_users())
psk = SymmetricKey(rng, 16);
else
throw TLS_Exception(Alert::UNKNOWN_PSK_IDENTITY,
"No PSK for identifier " + psk_identity);
}
}
if(kex_algo == Kex_Algo::PSK)
{
std::vector<uint8_t> zeros(psk.length());
append_tls_length_value(m_pre_master, zeros, 2);
append_tls_length_value(m_pre_master, psk.bits_of(), 2);
}
#if defined(BOTAN_HAS_CECPQ1)
else if(kex_algo == Kex_Algo::CECPQ1)
{
const CECPQ1_key& cecpq1_offer = state.server_kex()->cecpq1_key();
const std::vector<uint8_t> cecpq1_accept = reader.get_range<uint8_t>(2, 0, 65535);
if(cecpq1_accept.size() != CECPQ1_ACCEPT_BYTES)
throw Decoding_Error("Invalid size for CECPQ1 accept message");
m_pre_master.resize(CECPQ1_SHARED_KEY_BYTES);
CECPQ1_finish(m_pre_master.data(), cecpq1_offer, cecpq1_accept.data());
}
#endif
else if(kex_algo == Kex_Algo::DH ||
kex_algo == Kex_Algo::ECDH ||
kex_algo == Kex_Algo::ECDHE_PSK)
{
const Private_Key& private_key = state.server_kex()->server_kex_key();
const PK_Key_Agreement_Key* ka_key =
dynamic_cast<const PK_Key_Agreement_Key*>(&private_key);
if(!ka_key)
throw Internal_Error("Expected key agreement key type but got " +
private_key.algo_name());
std::vector<uint8_t> client_pubkey;
if(ka_key->algo_name() == "DH")
{
client_pubkey = reader.get_range<uint8_t>(2, 0, 65535);
}
else
{
client_pubkey = reader.get_range<uint8_t>(1, 1, 255);
}
try
{
PK_Key_Agreement ka(*ka_key, rng, "Raw");
secure_vector<uint8_t> shared_secret = ka.derive_key(0, client_pubkey).bits_of();
if(ka_key->algo_name() == "DH")
shared_secret = CT::strip_leading_zeros(shared_secret);
if(kex_algo == Kex_Algo::ECDHE_PSK)
{
append_tls_length_value(m_pre_master, shared_secret, 2);
append_tls_length_value(m_pre_master, psk.bits_of(), 2);
}
else
m_pre_master = shared_secret;
}
catch(Invalid_Argument& e)
{
throw TLS_Exception(Alert::ILLEGAL_PARAMETER, e.what());
}
catch(std::exception&)
{
/*
* Something failed in the DH/ECDH computation. To avoid possible
* attacks which are based on triggering and detecting some edge
* failure condition, randomize the pre-master output and carry on,
* allowing the protocol to fail later in the finished checks.
*/
rng.random_vec(m_pre_master, ka_key->public_value().size());
}
reader.assert_done();
}
else
throw Internal_Error("Client_Key_Exchange: Unknown key exchange negotiated");
}
}
}
|