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