/src/botan/src/lib/tls/tls_channel.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * TLS Channels |
3 | | * (C) 2011,2012,2014,2015,2016 Jack Lloyd |
4 | | * 2016 Matthias Gierlings |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #include <botan/tls_channel.h> |
10 | | #include <botan/tls_policy.h> |
11 | | #include <botan/tls_messages.h> |
12 | | #include <botan/kdf.h> |
13 | | #include <botan/internal/tls_handshake_state.h> |
14 | | #include <botan/internal/tls_record.h> |
15 | | #include <botan/internal/tls_seq_numbers.h> |
16 | | #include <botan/internal/rounding.h> |
17 | | #include <botan/internal/stl_util.h> |
18 | | #include <botan/loadstor.h> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | namespace TLS { |
23 | | |
24 | | size_t TLS::Channel::IO_BUF_DEFAULT_SIZE = 10*1024; |
25 | | |
26 | | Channel::Channel(Callbacks& callbacks, |
27 | | Session_Manager& session_manager, |
28 | | RandomNumberGenerator& rng, |
29 | | const Policy& policy, |
30 | | bool is_server, |
31 | | bool is_datagram, |
32 | | size_t reserved_io_buffer_size) : |
33 | | m_is_server(is_server), |
34 | | m_is_datagram(is_datagram), |
35 | | m_callbacks(callbacks), |
36 | | m_session_manager(session_manager), |
37 | | m_policy(policy), |
38 | | m_rng(rng), |
39 | | m_has_been_closed(false) |
40 | 11.3k | { |
41 | 11.3k | init(reserved_io_buffer_size); |
42 | 11.3k | } |
43 | | |
44 | | Channel::Channel(output_fn out, |
45 | | data_cb app_data_cb, |
46 | | alert_cb recv_alert_cb, |
47 | | handshake_cb hs_cb, |
48 | | handshake_msg_cb hs_msg_cb, |
49 | | Session_Manager& session_manager, |
50 | | RandomNumberGenerator& rng, |
51 | | const Policy& policy, |
52 | | bool is_server, |
53 | | bool is_datagram, |
54 | | size_t io_buf_sz) : |
55 | | m_is_server(is_server), |
56 | | m_is_datagram(is_datagram), |
57 | | m_compat_callbacks(new Compat_Callbacks( |
58 | | /* |
59 | | this Channel constructor is also deprecated so its ok that it |
60 | | relies on a deprecated API |
61 | | */ |
62 | | Compat_Callbacks::SILENCE_DEPRECATION_WARNING::PLEASE, |
63 | | out, app_data_cb, recv_alert_cb, hs_cb, hs_msg_cb)), |
64 | | m_callbacks(*m_compat_callbacks.get()), |
65 | | m_session_manager(session_manager), |
66 | | m_policy(policy), |
67 | | m_rng(rng), |
68 | | m_has_been_closed(false) |
69 | 0 | { |
70 | 0 | init(io_buf_sz); |
71 | 0 | } |
72 | | |
73 | | void Channel::init(size_t io_buf_sz) |
74 | 11.3k | { |
75 | 11.3k | /* epoch 0 is plaintext, thus null cipher state */ |
76 | 11.3k | m_write_cipher_states[0] = nullptr; |
77 | 11.3k | m_read_cipher_states[0] = nullptr; |
78 | 11.3k | |
79 | 11.3k | m_writebuf.reserve(io_buf_sz); |
80 | 11.3k | m_readbuf.reserve(io_buf_sz); |
81 | 11.3k | } |
82 | | |
83 | | void Channel::reset_state() |
84 | 8.02k | { |
85 | 8.02k | m_active_state.reset(); |
86 | 8.02k | m_pending_state.reset(); |
87 | 8.02k | m_readbuf.clear(); |
88 | 8.02k | m_write_cipher_states.clear(); |
89 | 8.02k | m_read_cipher_states.clear(); |
90 | 8.02k | } |
91 | | |
92 | | void Channel::reset_active_association_state() |
93 | 0 | { |
94 | 0 | // This operation only makes sense for DTLS |
95 | 0 | BOTAN_ASSERT_NOMSG(m_is_datagram); |
96 | 0 | m_active_state.reset(); |
97 | 0 | m_read_cipher_states.clear(); |
98 | 0 | m_write_cipher_states.clear(); |
99 | 0 |
|
100 | 0 | m_write_cipher_states[0] = nullptr; |
101 | 0 | m_read_cipher_states[0] = nullptr; |
102 | 0 |
|
103 | 0 | if(m_sequence_numbers) |
104 | 0 | m_sequence_numbers->reset(); |
105 | 0 | } |
106 | | |
107 | | Channel::~Channel() |
108 | 11.3k | { |
109 | 11.3k | // So unique_ptr destructors run correctly |
110 | 11.3k | } |
111 | | |
112 | | Connection_Sequence_Numbers& Channel::sequence_numbers() const |
113 | 191k | { |
114 | 191k | BOTAN_ASSERT(m_sequence_numbers, "Have a sequence numbers object"); |
115 | 191k | return *m_sequence_numbers; |
116 | 191k | } |
117 | | |
118 | | std::shared_ptr<Connection_Cipher_State> Channel::read_cipher_state_epoch(uint16_t epoch) const |
119 | 1.11k | { |
120 | 1.11k | auto i = m_read_cipher_states.find(epoch); |
121 | 1.11k | if(i == m_read_cipher_states.end()) |
122 | 79 | throw Internal_Error("TLS::Channel No read cipherstate for epoch " + std::to_string(epoch)); |
123 | 1.03k | return i->second; |
124 | 1.03k | } |
125 | | |
126 | | std::shared_ptr<Connection_Cipher_State> Channel::write_cipher_state_epoch(uint16_t epoch) const |
127 | 92.2k | { |
128 | 92.2k | auto i = m_write_cipher_states.find(epoch); |
129 | 92.2k | if(i == m_write_cipher_states.end()) |
130 | 0 | throw Internal_Error("TLS::Channel No write cipherstate for epoch " + std::to_string(epoch)); |
131 | 92.2k | return i->second; |
132 | 92.2k | } |
133 | | |
134 | | std::vector<X509_Certificate> Channel::peer_cert_chain() const |
135 | 0 | { |
136 | 0 | if(auto active = active_state()) |
137 | 0 | return get_peer_cert_chain(*active); |
138 | 0 | return std::vector<X509_Certificate>(); |
139 | 0 | } |
140 | | |
141 | | bool Channel::save_session(const Session& session) |
142 | 574 | { |
143 | 574 | return callbacks().tls_session_established(session); |
144 | 574 | } |
145 | | |
146 | | Handshake_State& Channel::create_handshake_state(Protocol_Version version) |
147 | 45.3k | { |
148 | 45.3k | if(pending_state()) |
149 | 0 | throw Internal_Error("create_handshake_state called during handshake"); |
150 | 45.3k | |
151 | 45.3k | if(auto active = active_state()) |
152 | 0 | { |
153 | 0 | Protocol_Version active_version = active->version(); |
154 | 0 |
|
155 | 0 | if(active_version.is_datagram_protocol() != version.is_datagram_protocol()) |
156 | 0 | { |
157 | 0 | throw TLS_Exception(Alert::PROTOCOL_VERSION, |
158 | 0 | "Active state using version " + active_version.to_string() + |
159 | 0 | " cannot change to " + version.to_string() + " in pending"); |
160 | 0 | } |
161 | 45.3k | } |
162 | 45.3k | |
163 | 45.3k | if(!m_sequence_numbers) |
164 | 11.1k | { |
165 | 11.1k | if(version.is_datagram_protocol()) |
166 | 607 | m_sequence_numbers.reset(new Datagram_Sequence_Numbers); |
167 | 10.4k | else |
168 | 10.4k | m_sequence_numbers.reset(new Stream_Sequence_Numbers); |
169 | 11.1k | } |
170 | 45.3k | |
171 | 45.3k | using namespace std::placeholders; |
172 | 45.3k | |
173 | 45.3k | std::unique_ptr<Handshake_IO> io; |
174 | 45.3k | if(version.is_datagram_protocol()) |
175 | 607 | { |
176 | 607 | io.reset(new Datagram_Handshake_IO( |
177 | 607 | std::bind(&Channel::send_record_under_epoch, this, _1, _2, _3), |
178 | 607 | sequence_numbers(), |
179 | 607 | static_cast<uint16_t>(m_policy.dtls_default_mtu()), |
180 | 607 | m_policy.dtls_initial_timeout(), |
181 | 607 | m_policy.dtls_maximum_timeout())); |
182 | 607 | } |
183 | 44.7k | else |
184 | 44.7k | { |
185 | 44.7k | io.reset(new Stream_Handshake_IO(std::bind(&Channel::send_record, this, _1, _2))); |
186 | 44.7k | } |
187 | 45.3k | |
188 | 45.3k | m_pending_state.reset(new_handshake_state(io.release())); |
189 | 45.3k | |
190 | 45.3k | if(auto active = active_state()) |
191 | 0 | m_pending_state->set_version(active->version()); |
192 | 45.3k | |
193 | 45.3k | return *m_pending_state.get(); |
194 | 45.3k | } |
195 | | |
196 | | bool Channel::timeout_check() |
197 | 0 | { |
198 | 0 | if(m_pending_state) |
199 | 0 | return m_pending_state->handshake_io().timeout_check(); |
200 | 0 | |
201 | 0 | //FIXME: scan cipher suites and remove epochs older than 2*MSL |
202 | 0 | return false; |
203 | 0 | } |
204 | | |
205 | | void Channel::renegotiate(bool force_full_renegotiation) |
206 | 0 | { |
207 | 0 | if(pending_state()) // currently in handshake? |
208 | 0 | return; |
209 | 0 | |
210 | 0 | if(auto active = active_state()) |
211 | 0 | { |
212 | 0 | if(force_full_renegotiation == false) |
213 | 0 | force_full_renegotiation = !policy().allow_resumption_for_renegotiation(); |
214 | 0 |
|
215 | 0 | initiate_handshake(create_handshake_state(active->version()), |
216 | 0 | force_full_renegotiation); |
217 | 0 | } |
218 | 0 | else |
219 | 0 | throw Invalid_State("Cannot renegotiate on inactive connection"); |
220 | 0 | } |
221 | | |
222 | | void Channel::change_cipher_spec_reader(Connection_Side side) |
223 | 1.52k | { |
224 | 1.52k | auto pending = pending_state(); |
225 | 1.52k | |
226 | 1.52k | BOTAN_ASSERT(pending && pending->server_hello(), |
227 | 1.52k | "Have received server hello"); |
228 | 1.52k | |
229 | 1.52k | if(pending->server_hello()->compression_method() != 0) |
230 | 0 | throw Internal_Error("Negotiated unknown compression algorithm"); |
231 | 1.52k | |
232 | 1.52k | sequence_numbers().new_read_cipher_state(); |
233 | 1.52k | |
234 | 1.52k | const uint16_t epoch = sequence_numbers().current_read_epoch(); |
235 | 1.52k | |
236 | 1.52k | BOTAN_ASSERT(m_read_cipher_states.count(epoch) == 0, |
237 | 1.52k | "No read cipher state currently set for next epoch"); |
238 | 1.52k | |
239 | 1.52k | // flip side as we are reading |
240 | 1.52k | std::shared_ptr<Connection_Cipher_State> read_state( |
241 | 1.52k | new Connection_Cipher_State(pending->version(), |
242 | 1.52k | (side == CLIENT) ? SERVER : CLIENT, |
243 | 1.52k | false, |
244 | 1.52k | pending->ciphersuite(), |
245 | 1.52k | pending->session_keys(), |
246 | 1.52k | pending->server_hello()->supports_encrypt_then_mac())); |
247 | 1.52k | |
248 | 1.52k | m_read_cipher_states[epoch] = read_state; |
249 | 1.52k | } |
250 | | |
251 | | void Channel::change_cipher_spec_writer(Connection_Side side) |
252 | 1.44k | { |
253 | 1.44k | auto pending = pending_state(); |
254 | 1.44k | |
255 | 1.44k | BOTAN_ASSERT(pending && pending->server_hello(), |
256 | 1.44k | "Have received server hello"); |
257 | 1.44k | |
258 | 1.44k | if(pending->server_hello()->compression_method() != 0) |
259 | 0 | throw Internal_Error("Negotiated unknown compression algorithm"); |
260 | 1.44k | |
261 | 1.44k | sequence_numbers().new_write_cipher_state(); |
262 | 1.44k | |
263 | 1.44k | const uint16_t epoch = sequence_numbers().current_write_epoch(); |
264 | 1.44k | |
265 | 1.44k | BOTAN_ASSERT(m_write_cipher_states.count(epoch) == 0, |
266 | 1.44k | "No write cipher state currently set for next epoch"); |
267 | 1.44k | |
268 | 1.44k | std::shared_ptr<Connection_Cipher_State> write_state( |
269 | 1.44k | new Connection_Cipher_State(pending->version(), |
270 | 1.44k | side, |
271 | 1.44k | true, |
272 | 1.44k | pending->ciphersuite(), |
273 | 1.44k | pending->session_keys(), |
274 | 1.44k | pending->server_hello()->supports_encrypt_then_mac())); |
275 | 1.44k | |
276 | 1.44k | m_write_cipher_states[epoch] = write_state; |
277 | 1.44k | } |
278 | | |
279 | | bool Channel::is_active() const |
280 | 0 | { |
281 | 0 | if(is_closed()) |
282 | 0 | return false; |
283 | 0 | return (active_state() != nullptr); |
284 | 0 | } |
285 | | |
286 | | bool Channel::is_closed() const |
287 | 10.8k | { |
288 | 10.8k | return m_has_been_closed; |
289 | 10.8k | } |
290 | | |
291 | | void Channel::activate_session() |
292 | 574 | { |
293 | 574 | std::swap(m_active_state, m_pending_state); |
294 | 574 | m_pending_state.reset(); |
295 | 574 | |
296 | 574 | if(!m_active_state->version().is_datagram_protocol()) |
297 | 574 | { |
298 | 574 | // TLS is easy just remove all but the current state |
299 | 574 | const uint16_t current_epoch = sequence_numbers().current_write_epoch(); |
300 | 574 | |
301 | 574 | const auto not_current_epoch = |
302 | 2.29k | [current_epoch](uint16_t epoch) { return (epoch != current_epoch); }; |
303 | 574 | |
304 | 574 | map_remove_if(not_current_epoch, m_write_cipher_states); |
305 | 574 | map_remove_if(not_current_epoch, m_read_cipher_states); |
306 | 574 | } |
307 | 574 | |
308 | 574 | callbacks().tls_session_activated(); |
309 | 574 | } |
310 | | |
311 | | size_t Channel::received_data(const std::vector<uint8_t>& buf) |
312 | 0 | { |
313 | 0 | return this->received_data(buf.data(), buf.size()); |
314 | 0 | } |
315 | | |
316 | | size_t Channel::received_data(const uint8_t input[], size_t input_size) |
317 | 11.3k | { |
318 | 11.3k | const bool allow_epoch0_restart = m_is_datagram && m_is_server && policy().allow_dtls_epoch0_restart(); |
319 | 11.3k | |
320 | 11.3k | try |
321 | 11.3k | { |
322 | 163k | while(input_size) |
323 | 153k | { |
324 | 153k | size_t consumed = 0; |
325 | 153k | |
326 | 153k | auto get_epoch = [this](uint16_t epoch) { return read_cipher_state_epoch(epoch); }; |
327 | 153k | |
328 | 153k | const Record_Header record = |
329 | 153k | read_record(m_is_datagram, |
330 | 153k | m_readbuf, |
331 | 153k | input, |
332 | 153k | input_size, |
333 | 153k | consumed, |
334 | 153k | m_record_buf, |
335 | 153k | m_sequence_numbers.get(), |
336 | 153k | get_epoch, |
337 | 153k | allow_epoch0_restart); |
338 | 153k | |
339 | 153k | const size_t needed = record.needed(); |
340 | 153k | |
341 | 153k | BOTAN_ASSERT(consumed > 0, "Got to eat something"); |
342 | 153k | |
343 | 153k | BOTAN_ASSERT(consumed <= input_size, |
344 | 153k | "Record reader consumed sane amount"); |
345 | 153k | |
346 | 153k | input += consumed; |
347 | 153k | input_size -= consumed; |
348 | 153k | |
349 | 153k | BOTAN_ASSERT(input_size == 0 || needed == 0, |
350 | 153k | "Got a full record or consumed all input"); |
351 | 153k | |
352 | 153k | if(input_size == 0 && needed != 0) |
353 | 1.16k | return needed; // need more data to complete record |
354 | 152k | |
355 | 152k | // Ignore invalid records in DTLS |
356 | 152k | if(m_is_datagram && record.type() == NO_RECORD) |
357 | 173 | { |
358 | 173 | return 0; |
359 | 173 | } |
360 | 152k | |
361 | 152k | if(m_record_buf.size() > MAX_PLAINTEXT_SIZE) |
362 | 0 | throw TLS_Exception(Alert::RECORD_OVERFLOW, |
363 | 0 | "TLS plaintext record is larger than allowed maximum"); |
364 | 152k | |
365 | 152k | |
366 | 152k | const bool epoch0_restart = m_is_datagram && record.epoch() == 0 && active_state(); |
367 | 152k | BOTAN_ASSERT_IMPLICATION(epoch0_restart, allow_epoch0_restart, "Allowed state"); |
368 | 152k | |
369 | 152k | const bool initial_record = epoch0_restart || (!pending_state() && !active_state()); |
370 | 152k | |
371 | 152k | if(record.type() != ALERT) |
372 | 82.6k | { |
373 | 82.6k | if(initial_record) |
374 | 39.5k | { |
375 | 39.5k | // For initial records just check for basic sanity |
376 | 39.5k | if(record.version().major_version() != 3 && |
377 | 39.5k | record.version().major_version() != 0xFE) |
378 | 27 | { |
379 | 27 | throw TLS_Exception(Alert::PROTOCOL_VERSION, |
380 | 27 | "Received unexpected record version in initial record"); |
381 | 27 | } |
382 | 43.1k | } |
383 | 43.1k | else if(auto pending = pending_state()) |
384 | 43.1k | { |
385 | 43.1k | if(pending->server_hello() != nullptr && record.version() != pending->version()) |
386 | 115 | { |
387 | 115 | if(record.version() != pending->version()) |
388 | 115 | { |
389 | 115 | throw TLS_Exception(Alert::PROTOCOL_VERSION, |
390 | 115 | "Received unexpected record version"); |
391 | 115 | } |
392 | 0 | } |
393 | 0 | } |
394 | 0 | else if(auto active = active_state()) |
395 | 0 | { |
396 | 0 | if(record.version() != active->version()) |
397 | 0 | { |
398 | 0 | throw TLS_Exception(Alert::PROTOCOL_VERSION, |
399 | 0 | "Received unexpected record version"); |
400 | 0 | } |
401 | 152k | } |
402 | 82.6k | } |
403 | 152k | |
404 | 152k | if(record.type() == HANDSHAKE || record.type() == CHANGE_CIPHER_SPEC) |
405 | 82.4k | { |
406 | 82.4k | if(m_has_been_closed) |
407 | 2 | throw TLS_Exception(Alert::UNEXPECTED_MESSAGE, "Received handshake data after connection closure"); |
408 | 82.4k | process_handshake_ccs(m_record_buf, record.sequence(), record.type(), record.version(), epoch0_restart); |
409 | 82.4k | } |
410 | 69.8k | else if(record.type() == APPLICATION_DATA) |
411 | 13 | { |
412 | 13 | if(m_has_been_closed) |
413 | 2 | throw TLS_Exception(Alert::UNEXPECTED_MESSAGE, "Received application data after connection closure"); |
414 | 11 | if(pending_state() != nullptr) |
415 | 7 | throw TLS_Exception(Alert::UNEXPECTED_MESSAGE, "Can't interleave application and handshake data"); |
416 | 4 | process_application_data(record.sequence(), m_record_buf); |
417 | 4 | } |
418 | 69.7k | else if(record.type() == ALERT) |
419 | 68.0k | { |
420 | 68.0k | process_alert(m_record_buf); |
421 | 68.0k | } |
422 | 1.75k | else if(record.type() != NO_RECORD) |
423 | 115 | throw Unexpected_Message("Unexpected record type " + |
424 | 115 | std::to_string(record.type()) + |
425 | 115 | " from counterparty"); |
426 | 152k | } |
427 | 11.3k | |
428 | 11.3k | return 0; // on a record boundary |
429 | 2.92k | } |
430 | 2.92k | catch(TLS_Exception& e) |
431 | 2.92k | { |
432 | 2.92k | send_fatal_alert(e.type()); |
433 | 2.92k | throw; |
434 | 2.92k | } |
435 | 645 | catch(Invalid_Authentication_Tag&) |
436 | 645 | { |
437 | 645 | send_fatal_alert(Alert::BAD_RECORD_MAC); |
438 | 645 | throw; |
439 | 645 | } |
440 | 4.06k | catch(Decoding_Error&) |
441 | 4.06k | { |
442 | 4.06k | send_fatal_alert(Alert::DECODE_ERROR); |
443 | 4.06k | throw; |
444 | 4.06k | } |
445 | 387 | catch(...) |
446 | 387 | { |
447 | 387 | send_fatal_alert(Alert::INTERNAL_ERROR); |
448 | 387 | throw; |
449 | 387 | } |
450 | 11.3k | } |
451 | | |
452 | | void Channel::process_handshake_ccs(const secure_vector<uint8_t>& record, |
453 | | uint64_t record_sequence, |
454 | | Record_Type record_type, |
455 | | Protocol_Version record_version, |
456 | | bool epoch0_restart) |
457 | 82.4k | { |
458 | 82.4k | if(!m_pending_state) |
459 | 39.4k | { |
460 | 39.4k | // No pending handshake, possibly new: |
461 | 39.4k | if(record_version.is_datagram_protocol() && !epoch0_restart) |
462 | 607 | { |
463 | 607 | if(m_sequence_numbers) |
464 | 0 | { |
465 | 0 | /* |
466 | 0 | * Might be a peer retransmit under epoch - 1 in which |
467 | 0 | * case we must retransmit last flight |
468 | 0 | */ |
469 | 0 | sequence_numbers().read_accept(record_sequence); |
470 | 0 |
|
471 | 0 | const uint16_t epoch = record_sequence >> 48; |
472 | 0 |
|
473 | 0 | if(epoch == sequence_numbers().current_read_epoch()) |
474 | 0 | { |
475 | 0 | create_handshake_state(record_version); |
476 | 0 | } |
477 | 0 | else if(epoch == sequence_numbers().current_read_epoch() - 1) |
478 | 0 | { |
479 | 0 | BOTAN_ASSERT(m_active_state, "Have active state here"); |
480 | 0 | m_active_state->handshake_io().add_record(record.data(), |
481 | 0 | record.size(), |
482 | 0 | record_type, |
483 | 0 | record_sequence); |
484 | 0 | } |
485 | 0 | } |
486 | 607 | else |
487 | 607 | { |
488 | 607 | create_handshake_state(record_version); |
489 | 607 | } |
490 | 607 | } |
491 | 38.8k | else |
492 | 38.8k | { |
493 | 38.8k | create_handshake_state(record_version); |
494 | 38.8k | } |
495 | 39.4k | } |
496 | 82.4k | |
497 | 82.4k | // May have been created in above conditional |
498 | 82.4k | if(m_pending_state) |
499 | 82.4k | { |
500 | 82.4k | m_pending_state->handshake_io().add_record(record.data(), |
501 | 82.4k | record.size(), |
502 | 82.4k | record_type, |
503 | 82.4k | record_sequence); |
504 | 82.4k | |
505 | 139k | while(auto pending = m_pending_state.get()) |
506 | 133k | { |
507 | 133k | auto msg = pending->get_next_handshake_msg(); |
508 | 133k | |
509 | 133k | if(msg.first == HANDSHAKE_NONE) // no full handshake yet |
510 | 75.8k | break; |
511 | 57.4k | |
512 | 57.4k | process_handshake_msg(active_state(), *pending, |
513 | 57.4k | msg.first, msg.second, epoch0_restart); |
514 | 57.4k | |
515 | 57.4k | if(!m_pending_state) |
516 | 574 | break; |
517 | 57.4k | } |
518 | 82.4k | } |
519 | 82.4k | } |
520 | | |
521 | | void Channel::process_application_data(uint64_t seq_no, const secure_vector<uint8_t>& record) |
522 | 4 | { |
523 | 4 | if(!active_state()) |
524 | 4 | throw Unexpected_Message("Application data before handshake done"); |
525 | 0 | |
526 | 0 | callbacks().tls_record_received(seq_no, record.data(), record.size()); |
527 | 0 | } |
528 | | |
529 | | void Channel::process_alert(const secure_vector<uint8_t>& record) |
530 | 68.0k | { |
531 | 68.0k | Alert alert_msg(record); |
532 | 68.0k | |
533 | 68.0k | if(alert_msg.type() == Alert::NO_RENEGOTIATION) |
534 | 43.5k | m_pending_state.reset(); |
535 | 68.0k | |
536 | 68.0k | callbacks().tls_alert(alert_msg); |
537 | 68.0k | |
538 | 68.0k | if(alert_msg.is_fatal()) |
539 | 2.44k | { |
540 | 2.44k | if(auto active = active_state()) |
541 | 0 | m_session_manager.remove_entry(active->server_hello()->session_id()); |
542 | 2.44k | } |
543 | 68.0k | |
544 | 68.0k | if(alert_msg.type() == Alert::CLOSE_NOTIFY) |
545 | 2.80k | send_warning_alert(Alert::CLOSE_NOTIFY); // reply in kind |
546 | 68.0k | |
547 | 68.0k | if(alert_msg.type() == Alert::CLOSE_NOTIFY || alert_msg.is_fatal()) |
548 | 3.30k | { |
549 | 3.30k | m_has_been_closed = true; |
550 | 3.30k | } |
551 | 68.0k | } |
552 | | |
553 | | void Channel::write_record(Connection_Cipher_State* cipher_state, uint16_t epoch, |
554 | | uint8_t record_type, const uint8_t input[], size_t length) |
555 | 92.2k | { |
556 | 92.2k | BOTAN_ASSERT(m_pending_state || m_active_state, "Some connection state exists"); |
557 | 92.2k | |
558 | 92.2k | const Protocol_Version record_version = |
559 | 92.1k | (m_pending_state) ? (m_pending_state->version()) : (m_active_state->version()); |
560 | 92.1k | |
561 | 92.1k | TLS::write_record(m_writebuf, |
562 | 92.1k | record_type, |
563 | 92.1k | record_version, |
564 | 92.1k | sequence_numbers().next_write_sequence(epoch), |
565 | 92.1k | input, |
566 | 92.1k | length, |
567 | 92.1k | cipher_state, |
568 | 92.1k | m_rng); |
569 | 92.1k | |
570 | 92.1k | callbacks().tls_emit_data(m_writebuf.data(), m_writebuf.size()); |
571 | 92.1k | } |
572 | | |
573 | | void Channel::send_record_array(uint16_t epoch, uint8_t type, const uint8_t input[], size_t length) |
574 | 92.2k | { |
575 | 92.2k | if(length == 0) |
576 | 0 | return; |
577 | 92.2k | |
578 | 92.2k | /* |
579 | 92.2k | * In versions without an explicit IV field (only TLS v1.0 now that |
580 | 92.2k | * SSLv3 has been removed) send a single byte record first to randomize |
581 | 92.2k | * the following (implicit) IV of the following record. |
582 | 92.2k | * |
583 | 92.2k | * This isn't needed in TLS v1.1 or higher. |
584 | 92.2k | * |
585 | 92.2k | * An empty record also works but apparently some implementations do |
586 | 92.2k | * not like this (https://bugzilla.mozilla.org/show_bug.cgi?id=665814) |
587 | 92.2k | * |
588 | 92.2k | * See https://www.openssl.org/~bodo/tls-cbc.txt for background. |
589 | 92.2k | */ |
590 | 92.2k | |
591 | 92.2k | auto cipher_state = write_cipher_state_epoch(epoch); |
592 | 92.2k | |
593 | 92.2k | if(type == APPLICATION_DATA && m_active_state->version().supports_explicit_cbc_ivs() == false) |
594 | 0 | { |
595 | 0 | while(length) |
596 | 0 | { |
597 | 0 | write_record(cipher_state.get(), epoch, type, input, 1); |
598 | 0 | input += 1; |
599 | 0 | length -= 1; |
600 | 0 |
|
601 | 0 | const size_t sending = std::min<size_t>(length, MAX_PLAINTEXT_SIZE); |
602 | 0 | write_record(cipher_state.get(), epoch, type, input, sending); |
603 | 0 |
|
604 | 0 | input += sending; |
605 | 0 | length -= sending; |
606 | 0 | } |
607 | 0 | } |
608 | 92.2k | else |
609 | 92.2k | { |
610 | 184k | while(length) |
611 | 92.2k | { |
612 | 92.2k | const size_t sending = std::min<size_t>(length, MAX_PLAINTEXT_SIZE); |
613 | 92.2k | write_record(cipher_state.get(), epoch, type, input, sending); |
614 | 92.2k | |
615 | 92.2k | input += sending; |
616 | 92.2k | length -= sending; |
617 | 92.2k | } |
618 | 92.2k | } |
619 | 92.2k | } |
620 | | |
621 | | void Channel::send_record(uint8_t record_type, const std::vector<uint8_t>& record) |
622 | 92.3k | { |
623 | 92.3k | send_record_array(sequence_numbers().current_write_epoch(), |
624 | 92.3k | record_type, record.data(), record.size()); |
625 | 92.3k | } |
626 | | |
627 | | void Channel::send_record_under_epoch(uint16_t epoch, uint8_t record_type, |
628 | | const std::vector<uint8_t>& record) |
629 | 0 | { |
630 | 0 | send_record_array(epoch, record_type, record.data(), record.size()); |
631 | 0 | } |
632 | | |
633 | | void Channel::send(const uint8_t buf[], size_t buf_size) |
634 | 0 | { |
635 | 0 | if(!is_active()) |
636 | 0 | throw Invalid_State("Data cannot be sent on inactive TLS connection"); |
637 | 0 | |
638 | 0 | send_record_array(sequence_numbers().current_write_epoch(), |
639 | 0 | APPLICATION_DATA, buf, buf_size); |
640 | 0 | } |
641 | | |
642 | | void Channel::send(const std::string& string) |
643 | 0 | { |
644 | 0 | this->send(cast_char_ptr_to_uint8(string.data()), string.size()); |
645 | 0 | } |
646 | | |
647 | | void Channel::send_alert(const Alert& alert) |
648 | 10.8k | { |
649 | 10.8k | if(alert.is_valid() && !is_closed()) |
650 | 8.04k | { |
651 | 8.04k | try |
652 | 8.04k | { |
653 | 8.04k | send_record(ALERT, alert.serialize()); |
654 | 8.04k | } |
655 | 8.04k | catch(...) { /* swallow it */ } |
656 | 8.04k | } |
657 | 10.8k | |
658 | 10.8k | if(alert.type() == Alert::NO_RENEGOTIATION) |
659 | 0 | m_pending_state.reset(); |
660 | 10.8k | |
661 | 10.8k | if(alert.is_fatal()) |
662 | 8.02k | if(auto active = active_state()) |
663 | 324 | m_session_manager.remove_entry(active->server_hello()->session_id()); |
664 | 10.8k | |
665 | 10.8k | if(alert.is_fatal()) |
666 | 8.02k | reset_state(); |
667 | 10.8k | |
668 | 10.8k | if(alert.type() == Alert::CLOSE_NOTIFY || alert.is_fatal()) |
669 | 10.8k | { |
670 | 10.8k | m_has_been_closed = true; |
671 | 10.8k | } |
672 | 10.8k | } |
673 | | |
674 | | void Channel::secure_renegotiation_check(const Client_Hello* client_hello) |
675 | 28.5k | { |
676 | 28.5k | const bool secure_renegotiation = client_hello->secure_renegotiation(); |
677 | 28.5k | |
678 | 28.5k | if(auto active = active_state()) |
679 | 0 | { |
680 | 0 | const bool active_sr = active->client_hello()->secure_renegotiation(); |
681 | 0 |
|
682 | 0 | if(active_sr != secure_renegotiation) |
683 | 0 | throw TLS_Exception(Alert::HANDSHAKE_FAILURE, |
684 | 0 | "Client changed its mind about secure renegotiation"); |
685 | 28.5k | } |
686 | 28.5k | |
687 | 28.5k | if(secure_renegotiation) |
688 | 8.46k | { |
689 | 8.46k | const std::vector<uint8_t>& data = client_hello->renegotiation_info(); |
690 | 8.46k | |
691 | 8.46k | if(data != secure_renegotiation_data_for_client_hello()) |
692 | 1 | throw TLS_Exception(Alert::HANDSHAKE_FAILURE, |
693 | 1 | "Client sent bad values for secure renegotiation"); |
694 | 8.46k | } |
695 | 28.5k | } |
696 | | |
697 | | void Channel::secure_renegotiation_check(const Server_Hello* server_hello) |
698 | 26.8k | { |
699 | 26.8k | const bool secure_renegotiation = server_hello->secure_renegotiation(); |
700 | 26.8k | |
701 | 26.8k | if(auto active = active_state()) |
702 | 0 | { |
703 | 0 | const bool active_sr = active->server_hello()->secure_renegotiation(); |
704 | 0 |
|
705 | 0 | if(active_sr != secure_renegotiation) |
706 | 0 | throw TLS_Exception(Alert::HANDSHAKE_FAILURE, |
707 | 0 | "Server changed its mind about secure renegotiation"); |
708 | 26.8k | } |
709 | 26.8k | |
710 | 26.8k | if(secure_renegotiation) |
711 | 5.87k | { |
712 | 5.87k | const std::vector<uint8_t>& data = server_hello->renegotiation_info(); |
713 | 5.87k | |
714 | 5.87k | if(data != secure_renegotiation_data_for_server_hello()) |
715 | 1 | throw TLS_Exception(Alert::HANDSHAKE_FAILURE, |
716 | 1 | "Server sent bad values for secure renegotiation"); |
717 | 5.87k | } |
718 | 26.8k | } |
719 | | |
720 | | std::vector<uint8_t> Channel::secure_renegotiation_data_for_client_hello() const |
721 | 14.3k | { |
722 | 14.3k | if(auto active = active_state()) |
723 | 0 | return active->client_finished()->verify_data(); |
724 | 14.3k | return std::vector<uint8_t>(); |
725 | 14.3k | } |
726 | | |
727 | | std::vector<uint8_t> Channel::secure_renegotiation_data_for_server_hello() const |
728 | 28.2k | { |
729 | 28.2k | if(auto active = active_state()) |
730 | 0 | { |
731 | 0 | std::vector<uint8_t> buf = active->client_finished()->verify_data(); |
732 | 0 | buf += active->server_finished()->verify_data(); |
733 | 0 | return buf; |
734 | 0 | } |
735 | 28.2k | |
736 | 28.2k | return std::vector<uint8_t>(); |
737 | 28.2k | } |
738 | | |
739 | | bool Channel::secure_renegotiation_supported() const |
740 | 0 | { |
741 | 0 | if(auto active = active_state()) |
742 | 0 | return active->server_hello()->secure_renegotiation(); |
743 | 0 | |
744 | 0 | if(auto pending = pending_state()) |
745 | 0 | if(auto hello = pending->server_hello()) |
746 | 0 | return hello->secure_renegotiation(); |
747 | 0 | |
748 | 0 | return false; |
749 | 0 | } |
750 | | |
751 | | SymmetricKey Channel::key_material_export(const std::string& label, |
752 | | const std::string& context, |
753 | | size_t length) const |
754 | 0 | { |
755 | 0 | if(auto active = active_state()) |
756 | 0 | { |
757 | 0 | if(pending_state() != nullptr) |
758 | 0 | throw Invalid_State("Channel::key_material_export cannot export during renegotiation"); |
759 | 0 | |
760 | 0 | std::unique_ptr<KDF> prf(active->protocol_specific_prf()); |
761 | 0 |
|
762 | 0 | const secure_vector<uint8_t>& master_secret = |
763 | 0 | active->session_keys().master_secret(); |
764 | 0 |
|
765 | 0 | std::vector<uint8_t> salt; |
766 | 0 | salt += active->client_hello()->random(); |
767 | 0 | salt += active->server_hello()->random(); |
768 | 0 |
|
769 | 0 | if(context != "") |
770 | 0 | { |
771 | 0 | size_t context_size = context.length(); |
772 | 0 | if(context_size > 0xFFFF) |
773 | 0 | throw Invalid_Argument("key_material_export context is too long"); |
774 | 0 | salt.push_back(get_byte(0, static_cast<uint16_t>(context_size))); |
775 | 0 | salt.push_back(get_byte(1, static_cast<uint16_t>(context_size))); |
776 | 0 | salt += to_byte_vector(context); |
777 | 0 | } |
778 | 0 |
|
779 | 0 | return prf->derive_key(length, master_secret, salt, to_byte_vector(label)); |
780 | 0 | } |
781 | 0 | else |
782 | 0 | { |
783 | 0 | throw Invalid_State("Channel::key_material_export connection not active"); |
784 | 0 | } |
785 | 0 | } |
786 | | |
787 | | } |
788 | | |
789 | | } |