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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

94.42
/src/lib/tls/tls_session.cpp
1
/*
2
* TLS Session State
3
* (C) 2011-2012,2015,2019 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/tls_session.h>
9

10
#include <botan/aead.h>
11
#include <botan/asn1_obj.h>
12
#include <botan/ber_dec.h>
13
#include <botan/der_enc.h>
14
#include <botan/mac.h>
15
#include <botan/pem.h>
16
#include <botan/rng.h>
17
#include <botan/tls_callbacks.h>
18
#include <botan/tls_messages.h>
19
#include <botan/x509_key.h>
20
#include <botan/internal/ct_utils.h>
21
#include <botan/internal/loadstor.h>
22
#include <botan/internal/stl_util.h>
23

24
#include <utility>
25

26
namespace Botan::TLS {
27

28
void Session_Handle::validate_constraints() const {
3,554✔
29
   std::visit(overloaded{
7,108✔
30
                 [](const Session_ID& id) {
698✔
31
                    // RFC 5246 7.4.1.2
32
                    //    opaque SessionID<0..32>;
33
                    BOTAN_ARG_CHECK(!id.empty(), "Session ID must not be empty");
698✔
34
                    BOTAN_ARG_CHECK(id.size() <= 32, "Session ID cannot be longer than 32 bytes");
698✔
35
                 },
698✔
36
                 [](const Session_Ticket& ticket) {
2,167✔
37
                    BOTAN_ARG_CHECK(!ticket.empty(), "Ticket most not be empty");
2,167✔
38
                    BOTAN_ARG_CHECK(ticket.size() <= std::numeric_limits<uint16_t>::max(),
2,167✔
39
                                    "Ticket cannot be longer than 64kB");
40
                 },
2,167✔
41
                 [](const Opaque_Session_Handle& handle) {
689✔
42
                    // RFC 8446 4.6.1
43
                    //    opaque ticket<1..2^16-1>;
44
                    BOTAN_ARG_CHECK(!handle.empty(), "Opaque session handle must not be empty");
689✔
45
                    BOTAN_ARG_CHECK(handle.size() <= std::numeric_limits<uint16_t>::max(),
689✔
46
                                    "Opaque session handle cannot be longer than 64kB");
47
                 },
689✔
48
              },
49
              m_handle);
3,554✔
50
}
3,554✔
51

52
Opaque_Session_Handle Session_Handle::opaque_handle() const {
367✔
53
   // both a Session_ID and a Session_Ticket could be an Opaque_Session_Handle
54
   return Opaque_Session_Handle(std::visit([](const auto& handle) { return handle.get(); }, m_handle));
1,101✔
55
}
56

57
std::optional<Session_ID> Session_Handle::id() const {
2,472✔
58
   if(is_id()) {
2,472✔
59
      return std::get<Session_ID>(m_handle);
638✔
60
   }
61

62
   // Opaque handles can mimic as a Session_ID if they are short enough
63
   if(is_opaque_handle()) {
1,834✔
64
      const auto& handle = std::get<Opaque_Session_Handle>(m_handle);
680✔
65
      if(handle.size() <= 32) {
680✔
66
         return Session_ID(handle.get());
27✔
67
      }
68
   }
69

70
   return std::nullopt;
1,807✔
71
}
72

73
std::optional<Session_Ticket> Session_Handle::ticket() const {
1,772✔
74
   if(is_ticket()) {
1,772✔
75
      return std::get<Session_Ticket>(m_handle);
1,220✔
76
   }
77

78
   // Opaque handles can mimic 'normal' Session_Tickets at any time
79
   if(is_opaque_handle()) {
552✔
80
      return Session_Ticket(std::get<Opaque_Session_Handle>(m_handle).get());
210✔
81
   }
82

83
   return std::nullopt;
342✔
84
}
85

86
Ciphersuite Session_Base::ciphersuite() const {
4,913✔
87
   auto suite = Ciphersuite::by_id(m_ciphersuite);
4,913✔
88
   if(!suite.has_value()) {
4,913✔
89
      throw Decoding_Error("Failed to find cipher suite for ID " + std::to_string(m_ciphersuite));
×
90
   }
91
   return suite.value();
4,913✔
92
}
93

94
Session_Summary::Session_Summary(const Session_Base& base,
1,785✔
95
                                 bool was_resumption,
96
                                 std::optional<std::string> psk_identity) :
1,785✔
97
      Session_Base(base), m_external_psk_identity(std::move(psk_identity)), m_was_resumption(was_resumption) {
1,900✔
98
   BOTAN_ARG_CHECK(version().is_pre_tls_13(), "Instantiated a TLS 1.2 session summary with an newer TLS version");
1,785✔
99

100
   const auto cs = ciphersuite();
1,785✔
101
   m_kex_algo = cs.kex_algo();
1,785✔
102
}
1,785✔
103

104
#if defined(BOTAN_HAS_TLS_13)
105

106
namespace {
107

108
std::string tls13_kex_to_string(bool psk, std::optional<Named_Group> group) {
610✔
109
   if(psk && group) {
610✔
110
      if(group->is_dh_named_group()) {
159✔
111
         return kex_method_to_string(Kex_Algo::DHE_PSK);
×
112
      } else if(group->is_ecdh_named_curve() || group->is_x25519() || group->is_x448()) {
159✔
113
         return kex_method_to_string(Kex_Algo::ECDHE_PSK);
157✔
114
      } else if(group->is_pure_ml_kem() || group->is_pure_frodokem()) {
2✔
115
         return kex_method_to_string(Kex_Algo::KEM_PSK);
×
116
      } else if(group->is_pqc_hybrid()) {
2✔
117
         return kex_method_to_string(Kex_Algo::HYBRID_PSK);
2✔
118
      } else if(auto s = group->to_string()) {
×
119
         return *s;
×
120
      }
×
121
   } else if(psk) {
451✔
122
      return kex_method_to_string(Kex_Algo::PSK);
×
123
   } else {
124
      BOTAN_ASSERT_NOMSG(group.has_value());
451✔
125
      if(group->is_dh_named_group()) {
451✔
126
         return kex_method_to_string(Kex_Algo::DH);
×
127
      } else if(group->is_ecdh_named_curve() || group->is_x25519() || group->is_x448()) {
451✔
128
         return kex_method_to_string(Kex_Algo::ECDH);
419✔
129
      } else if(group->is_pure_ml_kem() || group->is_pure_frodokem()) {
32✔
130
         return kex_method_to_string(Kex_Algo::KEM);
2✔
131
      } else if(group->is_pqc_hybrid()) {
30✔
132
         return kex_method_to_string(Kex_Algo::HYBRID);
30✔
133
      } else if(auto s = group->to_string()) {
×
134
         return *s;
×
135
      }
×
136
   }
137

138
   return kex_method_to_string(Kex_Algo::UNDEFINED);
×
139
}
140

141
}  // namespace
142

143
Session_Summary::Session_Summary(const Server_Hello_13& server_hello,
610✔
144
                                 Connection_Side side,
145
                                 std::vector<X509_Certificate> peer_certs,
146
                                 std::shared_ptr<const Public_Key> peer_raw_public_key,
147
                                 std::optional<std::string> psk_identity,
148
                                 bool session_was_resumed,
149
                                 Server_Information server_info,
150
                                 std::chrono::system_clock::time_point current_timestamp) :
610✔
151
      Session_Base(current_timestamp,
152
                   server_hello.selected_version(),
153
                   server_hello.ciphersuite(),
610✔
154
                   side,
155

156
                   // TODO: SRTP might become necessary when DTLS 1.3 is being implemented
157
                   0,
158

159
                   // RFC 8446 Appendix D
160
                   //    Because TLS 1.3 always hashes in the transcript up to the server
161
                   //    Finished, implementations which support both TLS 1.3 and earlier
162
                   //    versions SHOULD indicate the use of the Extended Master Secret
163
                   //    extension in their APIs whenever TLS 1.3 is used.
164
                   true,
165

166
                   // TLS 1.3 uses AEADs, so technically encrypt-then-MAC is not applicable.
167
                   false,
168
                   std::move(peer_certs),
169
                   std::move(peer_raw_public_key),
170
                   std::move(server_info)),
171
      m_external_psk_identity(std::move(psk_identity)),
610✔
172
      m_was_resumption(session_was_resumed) {
1,834✔
173
   BOTAN_ARG_CHECK(version().is_tls_13_or_later(), "Instantiated a TLS 1.3 session summary with an older TLS version");
610✔
174
   set_session_id(server_hello.session_id());
1,220✔
175

176
   // In TLS 1.3 the key exchange algorithm is not negotiated in the ciphersuite
177
   // anymore. This provides a compatible identifier for applications to use.
178

179
   std::optional<Named_Group> group = [&]() -> std::optional<Named_Group> {
1,830✔
180
      if(psk_used() || was_resumption()) {
610✔
181
         if(auto* const keyshare = server_hello.extensions().get<Key_Share>()) {
159✔
182
            return keyshare->selected_group();
159✔
183
         } else {
184
            return {};
×
185
         }
186
      } else {
187
         auto* const keyshare = server_hello.extensions().get<Key_Share>();
451✔
188
         BOTAN_ASSERT_NONNULL(keyshare);
451✔
189
         return keyshare->selected_group();
451✔
190
      }
191
   }();
610✔
192

193
   if(group.has_value()) {
610✔
194
      m_kex_parameters = group->to_string();
1,220✔
195
   }
196

197
   m_kex_algo = tls13_kex_to_string(psk_used() || was_resumption(), group);
610✔
198
}
610✔
199

200
#endif
201

202
Session::Session(const secure_vector<uint8_t>& master_secret,
1,663✔
203
                 Protocol_Version version,
204
                 uint16_t ciphersuite,
205
                 Connection_Side side,
206
                 bool extended_master_secret,
207
                 bool encrypt_then_mac,
208
                 const std::vector<X509_Certificate>& certs,
209
                 const Server_Information& server_info,
210
                 uint16_t srtp_profile,
211
                 std::chrono::system_clock::time_point current_timestamp,
212
                 std::chrono::seconds lifetime_hint) :
1,663✔
213
      Session_Base(current_timestamp,
214
                   version,
215
                   ciphersuite,
216
                   side,
217
                   srtp_profile,
218
                   extended_master_secret,
219
                   encrypt_then_mac,
220
                   certs,
221
                   nullptr,  // RFC 7250 (raw public keys) is NYI for TLS 1.2
222
                   server_info),
223
      m_master_secret(master_secret),
1,663✔
224
      m_early_data_allowed(false),
1,663✔
225
      m_max_early_data_bytes(0),
1,663✔
226
      m_ticket_age_add(0),
1,663✔
227
      m_lifetime_hint(lifetime_hint) {
3,326✔
228
   BOTAN_ARG_CHECK(version.is_pre_tls_13(), "Instantiated a TLS 1.2 session object with a TLS version newer than 1.2");
1,663✔
229
}
1,663✔
230

231
#if defined(BOTAN_HAS_TLS_13)
232

233
Session::Session(const secure_vector<uint8_t>& session_psk,
578✔
234
                 const std::optional<uint32_t>& max_early_data_bytes,
235
                 uint32_t ticket_age_add,
236
                 std::chrono::seconds lifetime_hint,
237
                 Protocol_Version version,
238
                 uint16_t ciphersuite,
239
                 Connection_Side side,
240
                 const std::vector<X509_Certificate>& peer_certs,
241
                 std::shared_ptr<const Public_Key> peer_raw_public_key,
242
                 const Server_Information& server_info,
243
                 std::chrono::system_clock::time_point current_timestamp) :
578✔
244
      Session_Base(current_timestamp,
245
                   version,
246
                   ciphersuite,
247
                   side,
248

249
                   // TODO: SRTP might become necessary when DTLS 1.3 is being implemented
250
                   0,
251

252
                   // RFC 8446 Appendix D
253
                   //    Because TLS 1.3 always hashes in the transcript up to the server
254
                   //    Finished, implementations which support both TLS 1.3 and earlier
255
                   //    versions SHOULD indicate the use of the Extended Master Secret
256
                   //    extension in their APIs whenever TLS 1.3 is used.
257
                   true,
258

259
                   // TLS 1.3 uses AEADs, so technically encrypt-then-MAC is not applicable.
260
                   false,
261
                   peer_certs,
262
                   std::move(peer_raw_public_key),
263
                   server_info),
264
      m_master_secret(session_psk),
578✔
265
      m_early_data_allowed(max_early_data_bytes.has_value()),
578✔
266
      m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
578✔
267
      m_ticket_age_add(ticket_age_add),
578✔
268
      m_lifetime_hint(lifetime_hint) {
1,156✔
269
   BOTAN_ARG_CHECK(!version.is_pre_tls_13(), "Instantiated a TLS 1.3 session object with a TLS version older than 1.3");
578✔
270
}
578✔
271

272
Session::Session(secure_vector<uint8_t>&& session_psk,
284✔
273
                 const std::optional<uint32_t>& max_early_data_bytes,
274
                 std::chrono::seconds lifetime_hint,
275
                 const std::vector<X509_Certificate>& peer_certs,
276
                 std::shared_ptr<const Public_Key> peer_raw_public_key,
277
                 const Client_Hello_13& client_hello,
278
                 const Server_Hello_13& server_hello,
279
                 Callbacks& callbacks,
280
                 RandomNumberGenerator& rng) :
284✔
281
      Session_Base(callbacks.tls_current_timestamp(),
284✔
282
                   server_hello.selected_version(),
283
                   server_hello.ciphersuite(),
284✔
284
                   Connection_Side::Server,
285
                   0,
286
                   true,
287
                   false,  // see constructor above for rationales
288
                   peer_certs,
289
                   std::move(peer_raw_public_key),
290
                   Server_Information(client_hello.sni_hostname())),
568✔
291
      m_master_secret(std::move(session_psk)),
284✔
292
      m_early_data_allowed(max_early_data_bytes.has_value()),
284✔
293
      m_max_early_data_bytes(max_early_data_bytes.value_or(0)),
284✔
294
      m_ticket_age_add(load_be<uint32_t>(rng.random_vec(4).data(), 0)),
284✔
295
      m_lifetime_hint(lifetime_hint) {
852✔
296
   BOTAN_ARG_CHECK(!m_version.is_pre_tls_13(),
284✔
297
                   "Instantiated a TLS 1.3 session object with a TLS version older than 1.3");
298
}
284✔
299

300
#endif
301

302
Session::Session(std::string_view pem) : Session(PEM_Code::decode_check_label(pem, "TLS SESSION")) {}
3✔
303

304
Session::Session(std::span<const uint8_t> ber_data) /* NOLINT(*-member-init) */ {
454✔
305
   uint8_t side_code = 0;
454✔
306

307
   std::vector<uint8_t> raw_pubkey_or_empty;
454✔
308

309
   ASN1_String server_hostname;
455✔
310
   ASN1_String server_service;
454✔
311
   size_t server_port = 0;
454✔
312

313
   uint8_t major_version = 0;
454✔
314
   uint8_t minor_version = 0;
454✔
315

316
   size_t start_time = 0;
454✔
317
   size_t srtp_profile = 0;
454✔
318
   uint16_t ciphersuite_code = 0;
454✔
319
   uint64_t lifetime_hint = 0;
454✔
320

321
   BER_Decoder(ber_data.data(), ber_data.size())
908✔
322
      .start_sequence()
454✔
323
      .decode_and_check(TLS_SESSION_PARAM_STRUCT_VERSION, "Unknown version in serialized TLS session")
908✔
324
      .decode_integer_type(start_time)
454✔
325
      .decode_integer_type(major_version)
454✔
326
      .decode_integer_type(minor_version)
454✔
327
      .decode_integer_type(ciphersuite_code)
454✔
328
      .decode_integer_type(side_code)
454✔
329
      .decode(m_extended_master_secret)
454✔
330
      .decode(m_encrypt_then_mac)
454✔
331
      .decode(m_master_secret, ASN1_Type::OctetString)
454✔
332
      .decode_list<X509_Certificate>(m_peer_certs)
454✔
333
      .decode(raw_pubkey_or_empty, ASN1_Type::OctetString)
454✔
334
      .decode(server_hostname)
454✔
335
      .decode(server_service)
454✔
336
      .decode(server_port)
454✔
337
      .decode(srtp_profile)
454✔
338
      .decode(m_early_data_allowed)
454✔
339
      .decode_integer_type(m_max_early_data_bytes)
454✔
340
      .decode_integer_type(m_ticket_age_add)
454✔
341
      .decode_integer_type(lifetime_hint)
454✔
342
      .end_cons()
454✔
343
      .verify_end();
454✔
344

345
   if(!Ciphersuite::by_id(ciphersuite_code)) {
454✔
346
      throw Decoding_Error(
1✔
347
         "Serialized TLS session contains unknown cipher suite "
348
         "(" +
2✔
349
         std::to_string(ciphersuite_code) + ")");
4✔
350
   }
351

352
   m_ciphersuite = ciphersuite_code;
453✔
353
   m_version = Protocol_Version(major_version, minor_version);
453✔
354
   m_start_time = std::chrono::system_clock::from_time_t(start_time);
453✔
355
   m_connection_side = static_cast<Connection_Side>(side_code);
453✔
356
   m_srtp_profile = static_cast<uint16_t>(srtp_profile);
453✔
357

358
   m_server_info =
453✔
359
      Server_Information(server_hostname.value(), server_service.value(), static_cast<uint16_t>(server_port));
906✔
360

361
   if(!raw_pubkey_or_empty.empty()) {
453✔
362
      m_peer_raw_public_key = X509::load_key(raw_pubkey_or_empty);
×
363
   }
364

365
   m_lifetime_hint = std::chrono::seconds(lifetime_hint);
453✔
366
}
457✔
367

368
secure_vector<uint8_t> Session::DER_encode() const {
1,154✔
369
   const auto raw_pubkey_or_empty =
1,154✔
370
      m_peer_raw_public_key ? m_peer_raw_public_key->subject_public_key() : std::vector<uint8_t>{};
1,154✔
371

372
   return DER_Encoder()
2,308✔
373
      .start_sequence()
1,154✔
374
      .encode(TLS_SESSION_PARAM_STRUCT_VERSION)
1,154✔
375
      .encode(static_cast<size_t>(std::chrono::system_clock::to_time_t(m_start_time)))
1,154✔
376
      .encode(static_cast<size_t>(m_version.major_version()))
1,154✔
377
      .encode(static_cast<size_t>(m_version.minor_version()))
1,154✔
378
      .encode(static_cast<size_t>(m_ciphersuite))
1,154✔
379
      .encode(static_cast<size_t>(m_connection_side))
1,154✔
380
      .encode(m_extended_master_secret)
1,154✔
381
      .encode(m_encrypt_then_mac)
1,154✔
382
      .encode(m_master_secret, ASN1_Type::OctetString)
1,154✔
383
      .start_sequence()
1,570✔
384
      .encode_list(m_peer_certs)
1,154✔
385
      .end_cons()
1,154✔
386
      .encode(raw_pubkey_or_empty, ASN1_Type::OctetString)
1,154✔
387
      .encode(ASN1_String(m_server_info.hostname(), ASN1_Type::Utf8String))
3,462✔
388
      .encode(ASN1_String(m_server_info.service(), ASN1_Type::Utf8String))
3,462✔
389
      .encode(static_cast<size_t>(m_server_info.port()))
1,154✔
390
      .encode(static_cast<size_t>(m_srtp_profile))
1,154✔
391

392
      // the fields below were introduced for TLS 1.3 session tickets
393
      .encode(m_early_data_allowed)
1,154✔
394
      .encode(static_cast<size_t>(m_max_early_data_bytes))
1,154✔
395
      .encode(static_cast<size_t>(m_ticket_age_add))
1,154✔
396
      .encode(static_cast<size_t>(m_lifetime_hint.count()))
1,154✔
397
      .end_cons()
1,154✔
398
      .get_contents();
1,154✔
399
}
1,154✔
400

401
std::string Session::PEM_encode() const {
2✔
402
   return PEM_Code::encode(this->DER_encode(), "TLS SESSION");
6✔
403
}
404

405
secure_vector<uint8_t> Session::extract_master_secret() {
171✔
406
   BOTAN_STATE_CHECK(!m_master_secret.empty());
171✔
407
   return std::exchange(m_master_secret, {});
171✔
408
}
409

410
namespace {
411

412
// The output length of the HMAC must be a valid keylength for the AEAD
413
const char* const TLS_SESSION_CRYPT_HMAC = "HMAC(SHA-512-256)";
414
// SIV would be better, but we can't assume it is available
415
const char* const TLS_SESSION_CRYPT_AEAD = "AES-256/GCM";
416
const char* const TLS_SESSION_CRYPT_KEY_NAME = "BOTAN TLS SESSION KEY NAME";
417
const uint64_t TLS_SESSION_CRYPT_MAGIC = 0x068B5A9D396C0000;
418
const size_t TLS_SESSION_CRYPT_MAGIC_LEN = 8;
419
const size_t TLS_SESSION_CRYPT_KEY_NAME_LEN = 4;
420
const size_t TLS_SESSION_CRYPT_AEAD_NONCE_LEN = 12;
421
const size_t TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN = 16;
422
const size_t TLS_SESSION_CRYPT_AEAD_TAG_SIZE = 16;
423

424
const size_t TLS_SESSION_CRYPT_HDR_LEN = TLS_SESSION_CRYPT_MAGIC_LEN + TLS_SESSION_CRYPT_KEY_NAME_LEN +
425
                                         TLS_SESSION_CRYPT_AEAD_NONCE_LEN + TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN;
426

427
const size_t TLS_SESSION_CRYPT_OVERHEAD = TLS_SESSION_CRYPT_HDR_LEN + TLS_SESSION_CRYPT_AEAD_TAG_SIZE;
428

429
}  // namespace
430

431
std::vector<uint8_t> Session::encrypt(const SymmetricKey& key, RandomNumberGenerator& rng) const {
1,146✔
432
   auto hmac = MessageAuthenticationCode::create_or_throw(TLS_SESSION_CRYPT_HMAC);
1,146✔
433
   hmac->set_key(key);
1,146✔
434

435
   // First derive the "key name"
436
   std::vector<uint8_t> key_name(hmac->output_length());
1,146✔
437
   hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
1,146✔
438
   hmac->final(key_name.data());
1,146✔
439
   key_name.resize(TLS_SESSION_CRYPT_KEY_NAME_LEN);
1,146✔
440

441
   std::vector<uint8_t> aead_nonce;
1,146✔
442
   std::vector<uint8_t> key_seed;
1,146✔
443

444
   rng.random_vec(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
1,146✔
445
   rng.random_vec(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
1,146✔
446

447
   hmac->update(key_seed);
1,146✔
448
   const secure_vector<uint8_t> aead_key = hmac->final();
1,146✔
449

450
   secure_vector<uint8_t> bits = this->DER_encode();
1,146✔
451

452
   // create the header
453
   std::vector<uint8_t> buf;
1,146✔
454
   buf.reserve(TLS_SESSION_CRYPT_OVERHEAD + bits.size());
1,146✔
455
   buf.resize(TLS_SESSION_CRYPT_MAGIC_LEN);
1,146✔
456
   store_be(TLS_SESSION_CRYPT_MAGIC, &buf[0]);  // NOLINT(*container-data-pointer)
1,146✔
457
   buf += key_name;
1,146✔
458
   buf += key_seed;
1,146✔
459
   buf += aead_nonce;
1,146✔
460

461
   auto aead = AEAD_Mode::create_or_throw(TLS_SESSION_CRYPT_AEAD, Cipher_Dir::Encryption);
1,146✔
462
   BOTAN_ASSERT_NOMSG(aead->valid_nonce_length(TLS_SESSION_CRYPT_AEAD_NONCE_LEN));
1,146✔
463
   BOTAN_ASSERT_NOMSG(aead->tag_size() == TLS_SESSION_CRYPT_AEAD_TAG_SIZE);
1,146✔
464
   aead->set_key(aead_key);
1,146✔
465
   aead->set_associated_data(buf);
1,146✔
466
   aead->start(aead_nonce);
1,146✔
467
   aead->finish(bits, 0);
1,146✔
468

469
   // append the ciphertext
470
   buf += bits;
1,146✔
471
   return buf;
1,146✔
472
}
8,022✔
473

474
Session Session::decrypt(std::span<const uint8_t> in, const SymmetricKey& key) {
455✔
475
   try {
455✔
476
      const size_t min_session_size = 48 + 4;  // serious under-estimate
455✔
477
      if(in.size() < TLS_SESSION_CRYPT_OVERHEAD + min_session_size) {
455✔
478
         throw Decoding_Error("Encrypted session too short to be valid");
3✔
479
      }
480

481
      BufferSlicer sub(in);
452✔
482
      const auto* const magic = sub.take(TLS_SESSION_CRYPT_MAGIC_LEN).data();
452✔
483
      const auto* const key_name = sub.take(TLS_SESSION_CRYPT_KEY_NAME_LEN).data();
452✔
484
      const auto* const key_seed = sub.take(TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN).data();
452✔
485
      const auto* const aead_nonce = sub.take(TLS_SESSION_CRYPT_AEAD_NONCE_LEN).data();
452✔
486
      auto ctext = sub.copy_as_secure_vector(sub.remaining());
452✔
487

488
      if(load_be<uint64_t>(magic, 0) != TLS_SESSION_CRYPT_MAGIC) {
452✔
489
         throw Decoding_Error("Missing expected magic numbers");
×
490
      }
491

492
      auto hmac = MessageAuthenticationCode::create_or_throw(TLS_SESSION_CRYPT_HMAC);
456✔
493
      hmac->set_key(key);
452✔
494

495
      // First derive and check the "key name"
496
      std::vector<uint8_t> cmp_key_name(hmac->output_length());
456✔
497
      hmac->update(TLS_SESSION_CRYPT_KEY_NAME);
456✔
498
      hmac->final(cmp_key_name.data());
452✔
499

500
      if(CT::is_equal(cmp_key_name.data(), key_name, TLS_SESSION_CRYPT_KEY_NAME_LEN).as_bool() == false) {
904✔
501
         throw Decoding_Error("Wrong key name for encrypted session");
1✔
502
      }
503

504
      hmac->update(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
451✔
505
      const secure_vector<uint8_t> aead_key = hmac->final();
451✔
506

507
      auto aead = AEAD_Mode::create_or_throw(TLS_SESSION_CRYPT_AEAD, Cipher_Dir::Decryption);
454✔
508
      aead->set_key(aead_key);
451✔
509
      aead->set_associated_data(in.data(), TLS_SESSION_CRYPT_HDR_LEN);
451✔
510
      aead->start(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
451✔
511
      aead->finish(ctext, 0);
451✔
512
      return Session(ctext);
451✔
513
   } catch(std::exception& e) {
2,261✔
514
      throw Decoding_Error("Failed to decrypt serialized TLS session: " + std::string(e.what()));
21✔
515
   }
7✔
516
}
517

518
}  // 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

© 2026 Coveralls, Inc