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