Coverage Report

Created: 2021-01-13 07:05

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