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