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

randombit / botan / 21712952425

05 Feb 2026 01:16PM UTC coverage: 90.076% (+0.005%) from 90.071%
21712952425

Pull #5287

github

web-flow
Merge 1b320c06e into 8c9623340
Pull Request #5287: Split out BufferSlicer and BufferStuffer to their own headers

102242 of 113507 relevant lines covered (90.08%)

11534589.25 hits per line

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

94.8
/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/buffer_slicer.h>
21
#include <botan/internal/ct_utils.h>
22
#include <botan/internal/loadstor.h>
23
#include <botan/internal/stl_util.h>
24

25
#include <utility>
26

27
namespace Botan::TLS {
28

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

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

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

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

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

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

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

84
   return std::nullopt;
341✔
85
}
86

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

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

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

105
#if defined(BOTAN_HAS_TLS_13)
106

107
namespace {
108

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

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

142
}  // namespace
143

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

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

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

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

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

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

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

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

201
#endif
202

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

232
#if defined(BOTAN_HAS_TLS_13)
233

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

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

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

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

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

301
#endif
302

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

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

308
   std::vector<uint8_t> raw_pubkey_or_empty;
451✔
309

310
   ASN1_String server_hostname;
451✔
311
   ASN1_String server_service;
452✔
312
   size_t server_port = 0;
451✔
313

314
   uint8_t major_version = 0;
451✔
315
   uint8_t minor_version = 0;
451✔
316

317
   size_t start_time = 0;
451✔
318
   size_t srtp_profile = 0;
451✔
319
   uint16_t ciphersuite_code = 0;
451✔
320
   uint64_t lifetime_hint = 0;
451✔
321

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

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

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

359
   m_server_info =
450✔
360
      Server_Information(server_hostname.value(), server_service.value(), static_cast<uint16_t>(server_port));
900✔
361

362
   if(!raw_pubkey_or_empty.empty()) {
450✔
363
      m_peer_raw_public_key = X509::load_key(raw_pubkey_or_empty);
1✔
364
   }
365

366
   m_lifetime_hint = std::chrono::seconds(lifetime_hint);
450✔
367
}
904✔
368

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

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

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

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

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

411
namespace {
412

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

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

428
const size_t TLS_SESSION_CRYPT_OVERHEAD = TLS_SESSION_CRYPT_HDR_LEN + TLS_SESSION_CRYPT_AEAD_TAG_SIZE;
429

430
}  // namespace
431

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

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

442
   std::vector<uint8_t> aead_nonce;
1,149✔
443
   std::vector<uint8_t> key_seed;
1,149✔
444

445
   rng.random_vec(aead_nonce, TLS_SESSION_CRYPT_AEAD_NONCE_LEN);
1,149✔
446
   rng.random_vec(key_seed, TLS_SESSION_CRYPT_AEAD_KEY_SEED_LEN);
1,149✔
447

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

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

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

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

470
   // append the ciphertext
471
   buf += bits;
1,149✔
472
   return buf;
1,149✔
473
}
4,596✔
474

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

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

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

493
      auto hmac = MessageAuthenticationCode::create_or_throw(TLS_SESSION_CRYPT_HMAC);
453✔
494
      hmac->set_key(key);
449✔
495

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

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

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

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

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