• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5312210723

19 Jun 2023 01:02PM UTC coverage: 91.712% (+0.002%) from 91.71%
5312210723

Pull #3549

github

web-flow
Merge 3275783d5 into 5288e84e1
Pull Request #3549: SPHINCS+

78132 of 85193 relevant lines covered (91.71%)

11884653.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/lib/tls/tls12/tls_channel_impl_12.cpp
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/internal/tls_channel_impl_12.h>
10

11
#include <botan/kdf.h>
12
#include <botan/tls_messages.h>
13
#include <botan/tls_policy.h>
14
#include <botan/x509cert.h>
15
#include <botan/internal/loadstor.h>
16
#include <botan/internal/stl_util.h>
17
#include <botan/internal/tls_handshake_state.h>
18
#include <botan/internal/tls_record.h>
19
#include <botan/internal/tls_seq_numbers.h>
20

21
namespace Botan::TLS {
22

23
Channel_Impl_12::Channel_Impl_12(const std::shared_ptr<Callbacks>& callbacks,
5,119✔
24
                                 const std::shared_ptr<Session_Manager>& session_manager,
25
                                 const std::shared_ptr<RandomNumberGenerator>& rng,
26
                                 const std::shared_ptr<const Policy>& policy,
27
                                 bool is_server,
28
                                 bool is_datagram,
29
                                 size_t reserved_io_buffer_size) :
5,119✔
30
      m_is_server(is_server),
5,119✔
31
      m_is_datagram(is_datagram),
5,119✔
32
      m_callbacks(callbacks),
5,119✔
33
      m_session_manager(session_manager),
5,119✔
34
      m_policy(policy),
5,119✔
35
      m_rng(rng),
5,119✔
36
      m_has_been_closed(false) {
10,238✔
37
   BOTAN_ASSERT_NONNULL(m_callbacks);
5,119✔
38
   BOTAN_ASSERT_NONNULL(m_session_manager);
5,119✔
39
   BOTAN_ASSERT_NONNULL(m_rng);
5,119✔
40
   BOTAN_ASSERT_NONNULL(m_policy);
5,119✔
41

42
   /* epoch 0 is plaintext, thus null cipher state */
43
   m_write_cipher_states[0] = nullptr;
5,119✔
44
   m_read_cipher_states[0] = nullptr;
5,119✔
45

46
   m_writebuf.reserve(reserved_io_buffer_size);
5,119✔
47
   m_readbuf.reserve(reserved_io_buffer_size);
5,119✔
48
}
5,119✔
49

50
void Channel_Impl_12::reset_state() {
3,473✔
51
   m_active_state.reset();
3,473✔
52
   m_pending_state.reset();
3,473✔
53
   m_readbuf.clear();
3,473✔
54
   m_write_cipher_states.clear();
3,473✔
55
   m_read_cipher_states.clear();
3,473✔
56
}
3,473✔
57

58
void Channel_Impl_12::reset_active_association_state() {
1✔
59
   // This operation only makes sense for DTLS
60
   BOTAN_ASSERT_NOMSG(m_is_datagram);
1✔
61
   m_active_state.reset();
1✔
62
   m_read_cipher_states.clear();
1✔
63
   m_write_cipher_states.clear();
1✔
64

65
   m_write_cipher_states[0] = nullptr;
1✔
66
   m_read_cipher_states[0] = nullptr;
1✔
67

68
   if(m_sequence_numbers) {
1✔
69
      m_sequence_numbers->reset();
1✔
70
   }
71
}
1✔
72

73
Channel_Impl_12::~Channel_Impl_12() = default;
44,773✔
74

75
Connection_Sequence_Numbers& Channel_Impl_12::sequence_numbers() const {
41,027✔
76
   BOTAN_ASSERT(m_sequence_numbers, "Have a sequence numbers object");
13,693✔
77
   return *m_sequence_numbers;
41,029✔
78
}
79

80
std::shared_ptr<Connection_Cipher_State> Channel_Impl_12::read_cipher_state_epoch(uint16_t epoch) const {
12,245✔
81
   auto i = m_read_cipher_states.find(epoch);
12,245✔
82
   if(i == m_read_cipher_states.end()) {
12,245✔
83
      throw Internal_Error("TLS::Channel_Impl_12 No read cipherstate for epoch " + std::to_string(epoch));
32✔
84
   }
85
   return i->second;
12,229✔
86
}
87

88
std::shared_ptr<Connection_Cipher_State> Channel_Impl_12::write_cipher_state_epoch(uint16_t epoch) const {
18,479✔
89
   auto i = m_write_cipher_states.find(epoch);
18,479✔
90
   if(i == m_write_cipher_states.end()) {
18,479✔
91
      throw Internal_Error("TLS::Channel_Impl_12 No write cipherstate for epoch " + std::to_string(epoch));
×
92
   }
93
   return i->second;
18,479✔
94
}
95

96
std::vector<X509_Certificate> Channel_Impl_12::peer_cert_chain() const {
116✔
97
   if(auto active = active_state()) {
116✔
98
      return get_peer_cert_chain(*active);
116✔
99
   }
100
   return std::vector<X509_Certificate>();
×
101
}
102

103
Handshake_State& Channel_Impl_12::create_handshake_state(Protocol_Version version) {
4,042✔
104
   if(pending_state()) {
4,042✔
105
      throw Internal_Error("create_handshake_state called during handshake");
×
106
   }
107

108
   if(auto active = active_state()) {
4,042✔
109
      Protocol_Version active_version = active->version();
51✔
110

111
      if(active_version.is_datagram_protocol() != version.is_datagram_protocol()) {
51✔
112
         throw TLS_Exception(Alert::ProtocolVersion,
×
113
                             "Active state using version " + active_version.to_string() + " cannot change to " +
×
114
                                version.to_string() + " in pending");
×
115
      }
116
   }
117

118
   if(!m_sequence_numbers) {
4,042✔
119
      if(version.is_datagram_protocol()) {
3,991✔
120
         m_sequence_numbers = std::make_unique<Datagram_Sequence_Numbers>();
812✔
121
      } else {
122
         m_sequence_numbers = std::make_unique<Stream_Sequence_Numbers>();
3,179✔
123
      }
124
   }
125

126
   using namespace std::placeholders;
4,042✔
127

128
   std::unique_ptr<Handshake_IO> io;
4,042✔
129
   if(version.is_datagram_protocol()) {
4,042✔
130
      io =
815✔
131
         std::make_unique<Datagram_Handshake_IO>(std::bind(&Channel_Impl_12::send_record_under_epoch, this, _1, _2, _3),
815✔
132
                                                 sequence_numbers(),
133
                                                 static_cast<uint16_t>(policy().dtls_default_mtu()),
815✔
134
                                                 policy().dtls_initial_timeout(),
815✔
135
                                                 policy().dtls_maximum_timeout());
1,630✔
136
   } else {
137
      io = std::make_unique<Stream_Handshake_IO>(std::bind(&Channel_Impl_12::send_record, this, _1, _2));
3,227✔
138
   }
139

140
   m_pending_state = new_handshake_state(std::move(io));
4,042✔
141

142
   if(auto active = active_state()) {
4,042✔
143
      m_pending_state->set_version(active->version());
51✔
144
   }
145

146
   return *m_pending_state;
4,042✔
147
}
4,042✔
148

149
bool Channel_Impl_12::timeout_check() {
10✔
150
   if(m_pending_state) {
10✔
151
      return m_pending_state->handshake_io().timeout_check();
4✔
152
   }
153

154
   //FIXME: scan cipher suites and remove epochs older than 2*MSL
155
   return false;
156
}
157

158
void Channel_Impl_12::renegotiate(bool force_full_renegotiation) {
×
159
   if(pending_state()) {  // currently in handshake?
×
160
      return;
161
   }
162

163
   if(auto active = active_state()) {
×
164
      if(force_full_renegotiation == false) {
×
165
         force_full_renegotiation = !policy().allow_resumption_for_renegotiation();
×
166
      }
167

168
      initiate_handshake(create_handshake_state(active->version()), force_full_renegotiation);
×
169
   } else {
170
      throw Invalid_State("Cannot renegotiate on inactive connection");
×
171
   }
172
}
173

174
void Channel_Impl_12::update_traffic_keys(bool) {
×
175
   throw Invalid_Argument("cannot update traffic keys on a TLS 1.2 channel");
×
176
}
177

178
void Channel_Impl_12::change_cipher_spec_reader(Connection_Side side) {
1,754✔
179
   auto pending = pending_state();
1,754✔
180

181
   BOTAN_ASSERT(pending && pending->server_hello(), "Have received server hello");
1,754✔
182

183
   if(pending->server_hello()->compression_method() != 0) {
1,754✔
184
      throw Internal_Error("Negotiated unknown compression algorithm");
×
185
   }
186

187
   sequence_numbers().new_read_cipher_state();
1,754✔
188

189
   const uint16_t epoch = sequence_numbers().current_read_epoch();
1,754✔
190

191
   BOTAN_ASSERT(!m_read_cipher_states.contains(epoch), "No read cipher state currently set for next epoch");
3,508✔
192

193
   // flip side as we are reading
194
   std::shared_ptr<Connection_Cipher_State> read_state(
1,754✔
195
      new Connection_Cipher_State(pending->version(),
3,508✔
196
                                  (side == Connection_Side::Client) ? Connection_Side::Server : Connection_Side::Client,
197
                                  false,
198
                                  pending->ciphersuite(),
1,754✔
199
                                  pending->session_keys(),
1,754✔
200
                                  pending->server_hello()->supports_encrypt_then_mac()));
2,573✔
201

202
   m_read_cipher_states[epoch] = read_state;
1,754✔
203
}
1,754✔
204

205
void Channel_Impl_12::change_cipher_spec_writer(Connection_Side side) {
1,753✔
206
   auto pending = pending_state();
1,753✔
207

208
   BOTAN_ASSERT(pending && pending->server_hello(), "Have received server hello");
1,753✔
209

210
   if(pending->server_hello()->compression_method() != 0) {
1,753✔
211
      throw Internal_Error("Negotiated unknown compression algorithm");
×
212
   }
213

214
   sequence_numbers().new_write_cipher_state();
1,753✔
215

216
   const uint16_t epoch = sequence_numbers().current_write_epoch();
1,753✔
217

218
   BOTAN_ASSERT(!m_write_cipher_states.contains(epoch), "No write cipher state currently set for next epoch");
3,506✔
219

220
   std::shared_ptr<Connection_Cipher_State> write_state(
1,753✔
221
      new Connection_Cipher_State(pending->version(),
3,506✔
222
                                  side,
223
                                  true,
224
                                  pending->ciphersuite(),
1,753✔
225
                                  pending->session_keys(),
1,753✔
226
                                  pending->server_hello()->supports_encrypt_then_mac()));
1,753✔
227

228
   m_write_cipher_states[epoch] = write_state;
1,753✔
229
}
1,753✔
230

231
bool Channel_Impl_12::is_active() const {
6,874✔
232
   if(is_closed()) {
6,874✔
233
      return false;
234
   }
235
   return (active_state() != nullptr);
6,110✔
236
}
237

238
bool Channel_Impl_12::is_closed() const {
14,266✔
239
   return m_has_been_closed;
14,266✔
240
}
241

242
void Channel_Impl_12::activate_session() {
1,714✔
243
   std::swap(m_active_state, m_pending_state);
1,714✔
244
   m_pending_state.reset();
1,714✔
245

246
   if(!m_active_state->version().is_datagram_protocol()) {
1,714✔
247
      // TLS is easy just remove all but the current state
248
      const uint16_t current_epoch = sequence_numbers().current_write_epoch();
1,024✔
249

250
      const auto not_current_epoch = [current_epoch](uint16_t epoch) { return (epoch != current_epoch); };
4,096✔
251

252
      map_remove_if(not_current_epoch, m_write_cipher_states);
1,024✔
253
      map_remove_if(not_current_epoch, m_read_cipher_states);
1,024✔
254
   }
255

256
   callbacks().tls_session_activated();
1,714✔
257
}
1,712✔
258

259
size_t Channel_Impl_12::from_peer(std::span<const uint8_t> data) {
140,104✔
260
   const bool allow_epoch0_restart = m_is_datagram && m_is_server && policy().allow_dtls_epoch0_restart();
140,104✔
261

262
   auto input = data.data();
140,104✔
263
   auto input_size = data.size();
140,104✔
264

265
   try {
140,104✔
266
      while(input_size) {
433,158✔
267
         size_t consumed = 0;
297,221✔
268

269
         auto get_epoch = [this](uint16_t epoch) { return read_cipher_state_epoch(epoch); };
12,245✔
270

271
         const Record_Header record = read_record(m_is_datagram,
298,965✔
272
                                                  m_readbuf,
297,221✔
273
                                                  input,
274
                                                  input_size,
275
                                                  consumed,
276
                                                  m_record_buf,
297,221✔
277
                                                  m_sequence_numbers.get(),
278
                                                  get_epoch,
279
                                                  allow_epoch0_restart);
280

281
         const size_t needed = record.needed();
295,477✔
282

283
         BOTAN_ASSERT(consumed > 0, "Got to eat something");
295,477✔
284

285
         BOTAN_ASSERT(consumed <= input_size, "Record reader consumed sane amount");
295,477✔
286

287
         input += consumed;
295,477✔
288
         input_size -= consumed;
295,477✔
289

290
         BOTAN_ASSERT(input_size == 0 || needed == 0, "Got a full record or consumed all input");
295,477✔
291

292
         if(input_size == 0 && needed != 0) {
295,477✔
293
            return needed;  // need more data to complete record
696✔
294
         }
295

296
         // Ignore invalid records in DTLS
297
         if(m_is_datagram && record.type() == Record_Type::Invalid) {
477,558✔
298
            return 0;
299
         }
300

301
         if(m_record_buf.size() > MAX_PLAINTEXT_SIZE) {
294,781✔
302
            throw TLS_Exception(Alert::RecordOverflow, "TLS plaintext record is larger than allowed maximum");
1✔
303
         }
304

305
         const bool epoch0_restart = m_is_datagram && record.epoch() == 0 && active_state();
476,180✔
306
         BOTAN_ASSERT_IMPLICATION(epoch0_restart, allow_epoch0_restart, "Allowed state");
294,780✔
307

308
         const bool initial_record = epoch0_restart || (!pending_state() && !active_state());
294,780✔
309

310
         if(record.type() != Record_Type::Alert) {
294,780✔
311
            if(initial_record) {
292,940✔
312
               // For initial records just check for basic sanity
313
               if(record.version().major_version() != 3 && record.version().major_version() != 0xFE) {
933✔
314
                  throw TLS_Exception(Alert::ProtocolVersion, "Received unexpected record version in initial record");
×
315
               }
316
            } else if(auto pending = pending_state()) {
292,007✔
317
               if(pending->server_hello() != nullptr && record.version() != pending->version()) {
289,602✔
318
                  throw TLS_Exception(Alert::ProtocolVersion, "Received unexpected record version");
×
319
               }
320
            } else if(auto active = active_state()) {
2,405✔
321
               if(record.version() != active->version()) {
2,405✔
322
                  throw TLS_Exception(Alert::ProtocolVersion, "Received unexpected record version");
×
323
               }
324
            }
325
         }
326

327
         if(record.type() == Record_Type::Handshake || record.type() == Record_Type::ChangeCipherSpec) {
294,780✔
328
            if(m_has_been_closed) {
290,591✔
329
               throw TLS_Exception(Alert::UnexpectedMessage, "Received handshake data after connection closure");
18✔
330
            }
331
            process_handshake_ccs(m_record_buf, record.sequence(), record.type(), record.version(), epoch0_restart);
581,146✔
332
         } else if(record.type() == Record_Type::ApplicationData) {
4,189✔
333
            if(m_has_been_closed) {
2,348✔
334
               throw TLS_Exception(Alert::UnexpectedMessage, "Received application data after connection closure");
6✔
335
            }
336
            if(pending_state() != nullptr) {
2,342✔
337
               throw TLS_Exception(Alert::UnexpectedMessage, "Can't interleave application and handshake data");
12✔
338
            }
339
            process_application_data(record.sequence(), m_record_buf);
2,330✔
340
         } else if(record.type() == Record_Type::Alert) {
1,841✔
341
            process_alert(m_record_buf);
1,840✔
342
         } else if(record.type() != Record_Type::Invalid) {
1✔
343
            throw Unexpected_Message("Unexpected record type " + std::to_string(static_cast<size_t>(record.type())) +
2✔
344
                                     " from counterparty");
2✔
345
         }
346
      }
347

348
      return 0;  // on a record boundary
349
   } catch(TLS_Exception& e) {
3,465✔
350
      send_fatal_alert(e.type());
3,252✔
351
      throw;
3,252✔
352
   } catch(Invalid_Authentication_Tag&) {
3,265✔
353
      send_fatal_alert(Alert::BadRecordMac);
13✔
354
      throw;
13✔
355
   } catch(Decoding_Error&) {
187✔
356
      send_fatal_alert(Alert::DecodeError);
174✔
357
      throw;
174✔
358
   } catch(...) {
200✔
359
      send_fatal_alert(Alert::InternalError);
26✔
360
      throw;
26✔
361
   }
26✔
362
}
363

364
void Channel_Impl_12::process_handshake_ccs(const secure_vector<uint8_t>& record,
290,573✔
365
                                            uint64_t record_sequence,
366
                                            Record_Type record_type,
367
                                            Protocol_Version record_version,
368
                                            bool epoch0_restart) {
369
   if(!m_pending_state) {
290,573✔
370
      // No pending handshake, possibly new:
371
      if(record_version.is_datagram_protocol() && !epoch0_restart) {
982✔
372
         if(m_sequence_numbers) {
375✔
373
            /*
374
            * Might be a peer retransmit under epoch - 1 in which
375
            * case we must retransmit last flight
376
            */
377
            sequence_numbers().read_accept(record_sequence);
2✔
378

379
            const uint16_t epoch = record_sequence >> 48;
2✔
380

381
            if(epoch == sequence_numbers().current_read_epoch()) {
2✔
382
               create_handshake_state(record_version);
2✔
383
            } else if(epoch == sequence_numbers().current_read_epoch() - 1) {
×
384
               BOTAN_ASSERT(m_active_state, "Have active state here");
×
385
               m_active_state->handshake_io().add_record(record.data(), record.size(), record_type, record_sequence);
×
386
            }
387
         } else {
388
            create_handshake_state(record_version);
373✔
389
         }
390
      } else {
391
         create_handshake_state(record_version);
607✔
392
      }
393
   }
394

395
   // May have been created in above conditional
396
   if(m_pending_state) {
290,573✔
397
      m_pending_state->handshake_io().add_record(record.data(), record.size(), record_type, record_sequence);
290,573✔
398

399
      while(auto pending = m_pending_state.get()) {
299,130✔
400
         auto msg = pending->get_next_handshake_msg();
299,130✔
401

402
         if(msg.first == Handshake_Type::None) {  // no full handshake yet
299,130✔
403
            break;
404
         }
405

406
         process_handshake_msg(active_state(), *pending, msg.first, msg.second, epoch0_restart);
11,949✔
407

408
         if(!m_pending_state) {
10,279✔
409
            break;
410
         }
411
      }
297,460✔
412
   }
413
}
288,894✔
414

415
void Channel_Impl_12::process_application_data(uint64_t seq_no, const secure_vector<uint8_t>& record) {
2,330✔
416
   if(!active_state()) {
2,330✔
417
      throw Unexpected_Message("Application data before handshake done");
×
418
   }
419

420
   callbacks().tls_record_received(seq_no, record);
2,330✔
421
}
2,330✔
422

423
void Channel_Impl_12::process_alert(const secure_vector<uint8_t>& record) {
1,840✔
424
   Alert alert_msg(record);
1,840✔
425

426
   if(alert_msg.type() == Alert::NoRenegotiation) {
1,836✔
427
      m_pending_state.reset();
232✔
428
   }
429

430
   callbacks().tls_alert(alert_msg);
1,836✔
431

432
   if(alert_msg.is_fatal()) {
1,830✔
433
      if(auto active = active_state()) {
14✔
434
         const auto& session_id = active->server_hello()->session_id();
6✔
435
         if(!session_id.empty()) {
6✔
436
            session_manager().remove(session_id);
12✔
437
         }
438
      }
439
   }
440

441
   if(alert_msg.type() == Alert::CloseNotify) {
1,830✔
442
      // TLS 1.2 requires us to immediately react with our "close_notify",
443
      // the return value of the application's callback has no effect on that.
444
      callbacks().tls_peer_closed_connection();
1,563✔
445
      send_warning_alert(Alert::CloseNotify);  // reply in kind
1,563✔
446
   }
447

448
   if(alert_msg.type() == Alert::CloseNotify || alert_msg.is_fatal()) {
1,830✔
449
      m_has_been_closed = true;
1,577✔
450
   }
451
}
1,830✔
452

453
void Channel_Impl_12::write_record(Connection_Cipher_State* cipher_state,
18,479✔
454
                                   uint16_t epoch,
455
                                   Record_Type record_type,
456
                                   const uint8_t input[],
457
                                   size_t length) {
458
   BOTAN_ASSERT(m_pending_state || m_active_state, "Some connection state exists");
18,479✔
459

460
   const Protocol_Version record_version =
18,479✔
461
      (m_pending_state) ? (m_pending_state->version()) : (m_active_state->version());
18,479✔
462

463
   const uint64_t next_seq = sequence_numbers().next_write_sequence(epoch);
18,479✔
464

465
   if(cipher_state == nullptr) {
18,479✔
466
      TLS::write_unencrypted_record(m_writebuf, record_type, record_version, next_seq, input, length);
12,205✔
467
   } else {
468
      TLS::write_record(m_writebuf, record_type, record_version, next_seq, input, length, *cipher_state, rng());
6,274✔
469
   }
470

471
   callbacks().tls_emit_data(m_writebuf);
18,479✔
472
}
18,476✔
473

474
void Channel_Impl_12::send_record_array(uint16_t epoch, Record_Type type, const uint8_t input[], size_t length) {
18,479✔
475
   if(length == 0) {
18,479✔
476
      return;
×
477
   }
478

479
   auto cipher_state = write_cipher_state_epoch(epoch);
18,479✔
480

481
   while(length) {
36,955✔
482
      const size_t sending = std::min<size_t>(length, MAX_PLAINTEXT_SIZE);
18,479✔
483
      write_record(cipher_state.get(), epoch, type, input, sending);
18,479✔
484

485
      input += sending;
18,476✔
486
      length -= sending;
18,476✔
487
   }
488
}
18,476✔
489

490
void Channel_Impl_12::send_record(Record_Type record_type, const std::vector<uint8_t>& record) {
11,184✔
491
   send_record_array(sequence_numbers().current_write_epoch(), record_type, record.data(), record.size());
11,184✔
492
}
11,182✔
493

494
void Channel_Impl_12::send_record_under_epoch(uint16_t epoch,
4,786✔
495
                                              Record_Type record_type,
496
                                              const std::vector<uint8_t>& record) {
497
   send_record_array(epoch, record_type, record.data(), record.size());
4,786✔
498
}
4,785✔
499

500
void Channel_Impl_12::to_peer(std::span<const uint8_t> data) {
2,509✔
501
   if(!is_active()) {
2,509✔
502
      throw Invalid_State("Data cannot be sent on inactive TLS connection");
×
503
   }
504

505
   send_record_array(sequence_numbers().current_write_epoch(), Record_Type::ApplicationData, data.data(), data.size());
2,509✔
506
}
2,509✔
507

508
void Channel_Impl_12::send_alert(const Alert& alert) {
6,757✔
509
   const bool ready_to_send_anything = !is_closed() && m_sequence_numbers;
6,757✔
510
   if(alert.is_valid() && ready_to_send_anything) {
6,757✔
511
      try {
4,187✔
512
         send_record(Record_Type::Alert, alert.serialize());
8,374✔
513
      } catch(...) { /* swallow it */
×
514
      }
×
515
   }
516

517
   if(alert.type() == Alert::NoRenegotiation) {
6,757✔
518
      m_pending_state.reset();
244✔
519
   }
520

521
   if(alert.is_fatal()) {
6,757✔
522
      if(auto active = active_state()) {
3,473✔
523
         const auto& session_id = active->server_hello()->session_id();
86✔
524
         if(!session_id.empty()) {
86✔
525
            session_manager().remove(Session_ID(session_id));
18✔
526
         }
527
      }
528
      reset_state();
3,473✔
529
   }
530

531
   if(alert.type() == Alert::CloseNotify || alert.is_fatal()) {
6,757✔
532
      m_has_been_closed = true;
6,518✔
533
   }
534
}
6,757✔
535

536
void Channel_Impl_12::secure_renegotiation_check(const Client_Hello_12* client_hello) {
4,014✔
537
   const bool secure_renegotiation = client_hello->secure_renegotiation();
4,014✔
538

539
   if(auto active = active_state()) {
4,014✔
540
      const bool active_sr = active->client_hello()->secure_renegotiation();
33✔
541

542
      if(active_sr != secure_renegotiation) {
33✔
543
         throw TLS_Exception(Alert::HandshakeFailure, "Client changed its mind about secure renegotiation");
×
544
      }
545
   }
546

547
   if(secure_renegotiation) {
4,014✔
548
      const std::vector<uint8_t>& data = client_hello->renegotiation_info();
4,011✔
549

550
      if(data != secure_renegotiation_data_for_client_hello()) {
4,011✔
551
         throw TLS_Exception(Alert::HandshakeFailure, "Client sent bad values for secure renegotiation");
×
552
      }
553
   }
4,011✔
554
}
4,014✔
555

556
void Channel_Impl_12::secure_renegotiation_check(const Server_Hello_12* server_hello) {
2,007✔
557
   const bool secure_renegotiation = server_hello->secure_renegotiation();
2,007✔
558

559
   if(auto active = active_state()) {
2,007✔
560
      const bool active_sr = active->server_hello()->secure_renegotiation();
31✔
561

562
      if(active_sr != secure_renegotiation) {
31✔
563
         throw TLS_Exception(Alert::HandshakeFailure, "Server changed its mind about secure renegotiation");
1✔
564
      }
565
   }
566

567
   if(secure_renegotiation) {
2,006✔
568
      const std::vector<uint8_t>& data = server_hello->renegotiation_info();
1,998✔
569

570
      if(data != secure_renegotiation_data_for_server_hello()) {
2,027✔
571
         throw TLS_Exception(Alert::HandshakeFailure, "Server sent bad values for secure renegotiation");
3✔
572
      }
573
   }
1,995✔
574
}
2,003✔
575

576
std::vector<uint8_t> Channel_Impl_12::secure_renegotiation_data_for_client_hello() const {
6,649✔
577
   if(auto active = active_state()) {
6,649✔
578
      return active->client_finished()->verify_data();
66✔
579
   }
580
   return std::vector<uint8_t>();
6,616✔
581
}
582

583
std::vector<uint8_t> Channel_Impl_12::secure_renegotiation_data_for_server_hello() const {
2,906✔
584
   if(auto active = active_state()) {
2,906✔
585
      std::vector<uint8_t> buf = active->client_finished()->verify_data();
29✔
586
      buf += active->server_finished()->verify_data();
29✔
587
      return buf;
29✔
588
   }
29✔
589

590
   return std::vector<uint8_t>();
2,906✔
591
}
592

593
bool Channel_Impl_12::secure_renegotiation_supported() const {
44✔
594
   if(auto active = active_state()) {
44✔
595
      return active->server_hello()->secure_renegotiation();
39✔
596
   }
597

598
   if(auto pending = pending_state()) {
5✔
599
      if(auto hello = pending->server_hello()) {
5✔
600
         return hello->secure_renegotiation();
5✔
601
      }
602
   }
603

604
   return false;
605
}
606

607
SymmetricKey Channel_Impl_12::key_material_export(std::string_view label,
398✔
608
                                                  std::string_view context,
609
                                                  size_t length) const {
610
   if(auto active = active_state()) {
398✔
611
      if(pending_state() != nullptr) {
398✔
612
         throw Invalid_State("Channel_Impl_12::key_material_export cannot export during renegotiation");
1✔
613
      }
614

615
      auto prf = active->protocol_specific_prf();
397✔
616

617
      const secure_vector<uint8_t>& master_secret = active->session_keys().master_secret();
397✔
618

619
      std::vector<uint8_t> salt;
397✔
620
      salt += active->client_hello()->random();
397✔
621
      salt += active->server_hello()->random();
397✔
622

623
      if(!context.empty()) {
397✔
624
         size_t context_size = context.length();
236✔
625
         if(context_size > 0xFFFF) {
236✔
626
            throw Invalid_Argument("key_material_export context is too long");
×
627
         }
628
         salt.push_back(get_byte<0>(static_cast<uint16_t>(context_size)));
236✔
629
         salt.push_back(get_byte<1>(static_cast<uint16_t>(context_size)));
236✔
630
         salt += to_byte_vector(context);
472✔
631
      }
632

633
      return SymmetricKey(prf->derive_key(length, master_secret, salt, to_byte_vector(label)));
633✔
634
   } else {
794✔
635
      throw Invalid_State("Channel_Impl_12::key_material_export connection not active");
×
636
   }
637
}
638

639
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc