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