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

randombit / botan / 22039041177

15 Feb 2026 04:24PM UTC coverage: 90.054% (-0.005%) from 90.059%
22039041177

push

github

web-flow
Merge pull request #5341 from randombit/jack/test-h-bin

Cleanup test predicates on binary strings

102341 of 113644 relevant lines covered (90.05%)

11487063.99 hits per line

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

97.27
/src/tests/test_tls_rfc8448.cpp
1
/*
2
* (C) 2021 Jack Lloyd
3
*     2021, 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
4
*     2022       René Meusel - Rohde & Schwarz Cybersecurity GmbH
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10
#include <fstream>
11
#include <memory>
12
#include <utility>
13

14
// Since RFC 8448 uses a specific set of cipher suites we can only run this
15
// test if all of them are enabled.
16
#if defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) && defined(BOTAN_HAS_AEAD_GCM) &&          \
17
   defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_X25519) && defined(BOTAN_HAS_SHA2_32) && defined(BOTAN_HAS_SHA2_64) && \
18
   defined(BOTAN_HAS_ECDSA) && defined(BOTAN_HAS_PSS)
19
   #define BOTAN_CAN_RUN_TEST_TLS_RFC8448
20
#endif
21

22
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
23
   #include "test_rng.h"
24

25
   #include <botan/assert.h>
26
   #include <botan/credentials_manager.h>
27
   #include <botan/data_src.h>
28
   #include <botan/dl_group.h>
29
   #include <botan/ec_group.h>
30
   #include <botan/pk_algs.h>
31
   #include <botan/pkcs8.h>
32
   #include <botan/tls_callbacks.h>
33
   #include <botan/tls_client.h>
34
   #include <botan/tls_extensions.h>
35
   #include <botan/tls_messages.h>
36
   #include <botan/tls_policy.h>
37
   #include <botan/tls_server.h>
38
   #include <botan/tls_session_manager.h>
39
   #include <botan/x509_key.h>
40
   #include <botan/internal/concat_util.h>
41
   #include <botan/internal/fmt.h>
42
   #include <botan/internal/stl_util.h>
43
#endif
44

45
namespace Botan_Tests {
46

47
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
48

49
namespace {
50

51
void add_entropy(Fixed_Output_RNG& rng, const std::vector<uint8_t>& bin) {
14✔
52
   rng.add_entropy(bin.data(), bin.size());
28✔
53
}
54

55
Botan::X509_Certificate server_certificate() {
4✔
56
   // self-signed certificate with an RSA1024 public key valid until:
57
   //   Jul 30 01:23:59 2026 GMT
58
   Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/server_certificate.pem"));
8✔
59
   return Botan::X509_Certificate(in);
4✔
60
}
4✔
61

62
Botan::X509_Certificate alternative_server_certificate() {
1✔
63
   // self-signed certificate with a P-256 public key valid until:
64
   //   Jul 30 01:24:00 2026 GMT
65
   //
66
   // This certificate is presented by the server in the "Client Authentication"
67
   // test case. Why the certificate differs in that case remains unclear.
68
   Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/server_certificate_client_auth.pem"));
2✔
69
   return Botan::X509_Certificate(in);
1✔
70
}
1✔
71

72
Botan::X509_Certificate client_certificate() {
2✔
73
   // self-signed certificate with an RSA1024 public key valid until:
74
   //   Jul 30 01:23:59 2026 GMT
75
   Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/client_certificate.pem"));
4✔
76
   return Botan::X509_Certificate(in);
2✔
77
}
2✔
78

79
std::unique_ptr<Botan::Private_Key> client_raw_public_key_pair() {
4✔
80
   // P-256 private key (independently generated)
81
   Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/client_raw_public_keypair.pem"));
8✔
82
   return Botan::PKCS8::load_key(in);
4✔
83
}
4✔
84

85
std::unique_ptr<Botan::Private_Key> server_raw_public_key_pair() {
4✔
86
   // P-256 private key (independently generated)
87
   Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/server_raw_public_keypair.pem"));
8✔
88
   return Botan::PKCS8::load_key(in);
4✔
89
}
4✔
90

91
/**
92
* Simple version of the Padding extension (RFC 7685) to reproduce the
93
* 2nd Client_Hello in RFC8448 Section 5 (HelloRetryRequest)
94
*/
95
class Padding final : public Botan::TLS::Extension {
96
   public:
97
      static Botan::TLS::Extension_Code static_type() {
98
         // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
99
         return Botan::TLS::Extension_Code(21);
100
      }
101

102
      Botan::TLS::Extension_Code type() const override { return static_type(); }
46✔
103

104
      explicit Padding(const size_t padding_bytes) : m_padding_bytes(padding_bytes) {}
2✔
105

106
      std::vector<uint8_t> serialize(Botan::TLS::Connection_Side /*whoami*/) const override {
5✔
107
         return std::vector<uint8_t>(m_padding_bytes, 0x00);
5✔
108
      }
109

110
      bool empty() const override { return m_padding_bytes == 0; }
5✔
111

112
   private:
113
      size_t m_padding_bytes;
114
};
115

116
using namespace Botan;
117
using namespace Botan::TLS;
118

119
std::chrono::system_clock::time_point from_milliseconds_since_epoch(uint64_t msecs) {
14✔
120
   const int64_t secs_since_epoch = msecs / 1000;
14✔
121
   const uint32_t additional_millis = msecs % 1000;
14✔
122

123
   BOTAN_ASSERT_NOMSG(secs_since_epoch <= std::numeric_limits<time_t>::max());
14✔
124
   return std::chrono::system_clock::from_time_t(static_cast<time_t>(secs_since_epoch)) +
14✔
125
          std::chrono::milliseconds(additional_millis);
14✔
126
}
127

128
using Modify_Exts_Fn =
129
   std::function<void(Botan::TLS::Extensions&, Botan::TLS::Connection_Side, Botan::TLS::Handshake_Type)>;
130

131
/**
132
 * We cannot actually reproduce the signatures stated in RFC 8448 as their
133
 * signature scheme is probabilistic and we're lacking the correct RNG
134
 * input. Hence, signatures are know beforehand and just reproduced by the
135
 * TLS callback when requested.
136
 */
137
struct MockSignature {
15✔
138
      std::vector<uint8_t> message_to_sign;
139
      std::vector<uint8_t> signature_to_produce;
140
};
141

142
/**
143
 * Subclass of the Botan::TLS::Callbacks instrumenting all available callbacks.
144
 * The invocation counts can be checked in the integration tests to make sure
145
 * all expected callbacks are hit. Furthermore collects the received application
146
 * data and sent record bytes for further inspection by the test cases.
147
 */
148
class Test_TLS_13_Callbacks : public Botan::TLS::Callbacks {
149
   public:
150
      Test_TLS_13_Callbacks(Modify_Exts_Fn modify_exts_cb,
14✔
151
                            std::vector<MockSignature> mock_signatures,
152
                            uint64_t timestamp) :
14✔
153
            session_activated_called(false),
14✔
154
            m_modify_exts(std::move(modify_exts_cb)),
14✔
155
            m_mock_signatures(std::move(mock_signatures)),
14✔
156
            m_timestamp(from_milliseconds_since_epoch(timestamp)) {}
14✔
157

158
      void tls_emit_data(std::span<const uint8_t> data) override {
47✔
159
         count_callback_invocation("tls_emit_data");
47✔
160
         send_buffer.insert(send_buffer.end(), data.begin(), data.end());
47✔
161
      }
47✔
162

163
      void tls_record_received(uint64_t seq_no, std::span<const uint8_t> data) override {
4✔
164
         count_callback_invocation("tls_record_received");
4✔
165
         received_seq_no = seq_no;
4✔
166
         receive_buffer.insert(receive_buffer.end(), data.begin(), data.end());
4✔
167
      }
4✔
168

169
      void tls_alert(Botan::TLS::Alert alert) override {
12✔
170
         count_callback_invocation("tls_alert");
12✔
171
         BOTAN_UNUSED(alert);
12✔
172
         // handle a tls alert received from the tls server
173
      }
12✔
174

175
      bool tls_peer_closed_connection() override {
12✔
176
         count_callback_invocation("tls_peer_closed_connection");
12✔
177
         // we want to handle the closure ourselves
178
         return false;
12✔
179
      }
180

181
      void tls_session_established(const Botan::TLS::Session_Summary& summary) override {
12✔
182
         if(const auto& psk_id = summary.external_psk_identity()) {
12✔
183
            negotiated_psk_identity = *psk_id;
2✔
184
         }
185
         count_callback_invocation("tls_session_established");
12✔
186
      }
12✔
187

188
      void tls_session_activated() override {
12✔
189
         count_callback_invocation("tls_session_activated");
12✔
190
         session_activated_called = true;
12✔
191
      }
12✔
192

193
      bool tls_should_persist_resumption_information(const Session& /*session*/) override {
2✔
194
         count_callback_invocation("tls_should_persist_resumption_information");
2✔
195
         return true;  // should always store the session
2✔
196
      }
197

198
      void tls_verify_cert_chain(const std::vector<Botan::X509_Certificate>& cert_chain,
5✔
199
                                 const std::vector<std::optional<Botan::OCSP::Response>>& /*ocsp*/,
200
                                 const std::vector<Botan::Certificate_Store*>& /*trusted*/,
201
                                 Botan::Usage_Type /*usage*/,
202
                                 std::string_view /*hostname*/,
203
                                 const Botan::TLS::Policy& /*policy*/) override {
204
         count_callback_invocation("tls_verify_cert_chain");
5✔
205
         certificate_chain = cert_chain;
5✔
206
      }
5✔
207

208
      void tls_verify_raw_public_key(const Public_Key& raw_pk,
2✔
209
                                     Usage_Type /*usage*/,
210
                                     std::string_view /*hostname*/,
211
                                     const TLS::Policy& /*policy*/) override {
212
         count_callback_invocation("tls_verify_raw_public_key");
2✔
213
         // TODO: is there a better way to copy a generic public key?
214
         raw_public_key = Botan::X509::load_key(raw_pk.subject_public_key());
2✔
215
      }
2✔
216

217
      std::chrono::milliseconds tls_verify_cert_chain_ocsp_timeout() const override {
×
218
         count_callback_invocation("tls_verify_cert_chain");
×
219
         return std::chrono::milliseconds(0);
×
220
      }
221

222
      std::vector<uint8_t> tls_provide_cert_status(const std::vector<X509_Certificate>& chain,
×
223
                                                   const Certificate_Status_Request& csr) override {
224
         count_callback_invocation("tls_provide_cert_status");
×
225
         return Callbacks::tls_provide_cert_status(chain, csr);
×
226
      }
227

228
      std::vector<uint8_t> tls_sign_message(const Private_Key& key,
7✔
229
                                            RandomNumberGenerator& rng,
230
                                            std::string_view padding,
231
                                            Signature_Format format,
232
                                            const std::vector<uint8_t>& msg) override {
233
         BOTAN_UNUSED(key, rng);
7✔
234
         count_callback_invocation("tls_sign_message");
7✔
235

236
         if(key.algo_name() == "RSA") {
7✔
237
            if(format != Signature_Format::Standard) {
4✔
238
               throw Test_Error("TLS implementation selected unexpected signature format for RSA");
×
239
            }
240

241
            if(padding != "PSS(SHA-256,MGF1,32)") {
8✔
242
               throw Test_Error("TLS implementation selected unexpected padding for RSA: " + std::string(padding));
×
243
            }
244
         } else if(key.algo_name() == "ECDSA") {
3✔
245
            if(format != Signature_Format::DerSequence) {
3✔
246
               throw Test_Error("TLS implementation selected unexpected signature format for ECDSA");
×
247
            }
248

249
            if(padding != "SHA-256") {
6✔
250
               throw Test_Error("TLS implementation selected unexpected padding for ECDSA: " + std::string(padding));
×
251
            }
252
         } else {
253
            throw Test_Error("TLS implementation trying to sign with unexpected algorithm (" + key.algo_name() + ")");
×
254
         }
255

256
         for(const auto& mock : m_mock_signatures) {
9✔
257
            if(mock.message_to_sign == msg) {
9✔
258
               return mock.signature_to_produce;
7✔
259
            }
260
         }
261

262
         throw Test_Error("TLS implementation produced an unexpected message to be signed: " + Botan::hex_encode(msg));
×
263
      }
×
264

265
      bool tls_verify_message(const Public_Key& key,
7✔
266
                              std::string_view padding,
267
                              Signature_Format format,
268
                              const std::vector<uint8_t>& msg,
269
                              const std::vector<uint8_t>& sig) override {
270
         count_callback_invocation("tls_verify_message");
7✔
271
         return Callbacks::tls_verify_message(key, padding, format, msg, sig);
7✔
272
      }
273

274
      std::unique_ptr<PK_Key_Agreement_Key> tls_generate_ephemeral_key(
15✔
275
         const std::variant<TLS::Group_Params, DL_Group>& group, RandomNumberGenerator& rng) override {
276
         count_callback_invocation("tls_generate_ephemeral_key");
15✔
277
         return Callbacks::tls_generate_ephemeral_key(group, rng);
15✔
278
      }
279

280
      secure_vector<uint8_t> tls_ephemeral_key_agreement(const std::variant<TLS::Group_Params, DL_Group>& group,
13✔
281
                                                         const PK_Key_Agreement_Key& private_key,
282
                                                         const std::vector<uint8_t>& public_value,
283
                                                         RandomNumberGenerator& rng,
284
                                                         const Policy& policy) override {
285
         count_callback_invocation("tls_ephemeral_key_agreement");
13✔
286
         return Callbacks::tls_ephemeral_key_agreement(group, private_key, public_value, rng, policy);
13✔
287
      }
288

289
      void tls_inspect_handshake_msg(const Handshake_Message& message) override {
102✔
290
         count_callback_invocation("tls_inspect_handshake_msg_" + message.type_string());
306✔
291

292
         try {
102✔
293
            auto serialized_message = message.serialize();
102✔
294

295
            serialized_messages.try_emplace(message.type_string())
204✔
296
               .first->second.emplace_back(std::move(serialized_message));
102✔
297
         } catch(const Not_Implemented&) {
102✔
298
            // TODO: Once the server implementation is finished, this crutch
299
            //       can likely be removed, as all message types will have a
300
            //       serialization method with actual business logic. :o)
301
         }
×
302

303
         return Callbacks::tls_inspect_handshake_msg(message);
102✔
304
      }
305

306
      std::string tls_server_choose_app_protocol(const std::vector<std::string>& client_protos) override {
×
307
         count_callback_invocation("tls_server_choose_app_protocol");
×
308
         return Callbacks::tls_server_choose_app_protocol(client_protos);
×
309
      }
310

311
      void tls_modify_extensions(Botan::TLS::Extensions& exts,
33✔
312
                                 Botan::TLS::Connection_Side side,
313
                                 Botan::TLS::Handshake_Type which_message) override {
314
         count_callback_invocation(std::string("tls_modify_extensions_") + handshake_type_to_string(which_message));
99✔
315
         m_modify_exts(exts, side, which_message);
33✔
316
         Callbacks::tls_modify_extensions(exts, side, which_message);
33✔
317
      }
33✔
318

319
      void tls_examine_extensions(const Botan::TLS::Extensions& extn,
31✔
320
                                  Connection_Side which_side,
321
                                  Botan::TLS::Handshake_Type which_message) override {
322
         count_callback_invocation(std::string("tls_examine_extensions_") + handshake_type_to_string(which_message));
93✔
323
         return Callbacks::tls_examine_extensions(extn, which_side, which_message);
31✔
324
      }
325

326
      std::string tls_peer_network_identity() override {
×
327
         count_callback_invocation("tls_peer_network_identity");
×
328
         return Callbacks::tls_peer_network_identity();
×
329
      }
330

331
      std::chrono::system_clock::time_point tls_current_timestamp() override {
23✔
332
         count_callback_invocation("tls_current_timestamp");
23✔
333
         return m_timestamp;
23✔
334
      }
335

336
      std::vector<uint8_t> pull_send_buffer() { return std::exchange(send_buffer, std::vector<uint8_t>()); }
39✔
337

338
      std::vector<uint8_t> pull_receive_buffer() { return std::exchange(receive_buffer, std::vector<uint8_t>()); }
4✔
339

340
      uint64_t last_received_seq_no() const { return received_seq_no; }
4✔
341

342
      const std::map<std::string, unsigned int>& callback_invocations() const { return m_callback_invocations; }
60✔
343

344
      void reset_callback_invocation_counters() { m_callback_invocations.clear(); }
60✔
345

346
   private:
347
      void count_callback_invocation(const std::string& callback_name) const {
339✔
348
         if(!m_callback_invocations.contains(callback_name)) {
339✔
349
            m_callback_invocations[callback_name] = 0;
320✔
350
         }
351

352
         m_callback_invocations[callback_name]++;
339✔
353
      }
339✔
354

355
   public:
356
      bool session_activated_called;                           // NOLINT(*-non-private-member-variable*)
357
      std::vector<Botan::X509_Certificate> certificate_chain;  // NOLINT(*-non-private-member-variable*)
358
      std::unique_ptr<Botan::Public_Key> raw_public_key;       // NOLINT(*-non-private-member-variable*)
359
      std::string negotiated_psk_identity;                     // NOLINT(*-non-private-member-variable*)
360
      std::map<std::string, std::vector<std::vector<uint8_t>>>
361
         serialized_messages;  // NOLINT(*-non-private-member-variable*)
362

363
   private:
364
      std::vector<uint8_t> send_buffer;
365
      std::vector<uint8_t> receive_buffer;
366
      uint64_t received_seq_no = 0;
367
      Modify_Exts_Fn m_modify_exts;
368
      std::vector<MockSignature> m_mock_signatures;
369
      std::chrono::system_clock::time_point m_timestamp;
370

371
      mutable std::map<std::string, unsigned int> m_callback_invocations;
372
};
373

374
class Test_Credentials : public Botan::Credentials_Manager {
375
   public:
376
      explicit Test_Credentials(bool use_alternative_server_certificate, std::optional<ExternalPSK> external_psk) :
14✔
377
            m_alternative_server_certificate(use_alternative_server_certificate),
14✔
378
            m_external_psk(std::move(external_psk)) {
16✔
379
         Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/server_key.pem"));
28✔
380
         m_server_private_key.reset(Botan::PKCS8::load_key(in).release());
14✔
381

382
         // RFC 8448 does not actually provide these keys. Hence we generate one on the
383
         // fly as a stand-in. Instead of actually using it, the signatures generated
384
         // by this private key must be hard-coded in `Callbacks::sign_message()`; see
385
         // `MockSignature_Fn` for more details.
386
         auto rng = Test::new_rng(__func__);
14✔
387
         m_bogus_alternative_server_private_key.reset(create_private_key("ECDSA", *rng, "secp256r1").release());
14✔
388

389
         m_client_private_key.reset(create_private_key("RSA", *rng, "1024").release());
28✔
390
      }
28✔
391

392
      std::vector<Botan::X509_Certificate> cert_chain(const std::vector<std::string>& cert_key_types,
5✔
393
                                                      const std::vector<AlgorithmIdentifier>& cert_signature_schemes,
394
                                                      const std::string& type,
395
                                                      const std::string& context) override {
396
         BOTAN_UNUSED(cert_key_types, cert_signature_schemes, context);
5✔
397
         if(type == "tls-client") {
5✔
398
            return {client_certificate()};
2✔
399
         } else if(m_alternative_server_certificate) {
4✔
400
            return {alternative_server_certificate()};
2✔
401
         } else {
402
            return {server_certificate()};
6✔
403
         }
404
      }
5✔
405

406
      std::shared_ptr<Public_Key> find_raw_public_key(const std::vector<std::string>& key_types,
2✔
407
                                                      const std::string& type,
408
                                                      const std::string& context) override {
409
         BOTAN_UNUSED(key_types, type, context);
2✔
410
         return (type == "tls-client") ? client_raw_public_key_pair()->public_key()
4✔
411
                                       : server_raw_public_key_pair()->public_key();
5✔
412
      }
413

414
      std::shared_ptr<Botan::Private_Key> private_key_for(const Botan::X509_Certificate& cert,
5✔
415
                                                          const std::string& type,
416
                                                          const std::string& context) override {
417
         BOTAN_UNUSED(cert, context);
5✔
418

419
         if(type == "tls-client") {
5✔
420
            return m_client_private_key;
1✔
421
         }
422

423
         if(m_alternative_server_certificate) {
4✔
424
            return m_bogus_alternative_server_private_key;
1✔
425
         }
426

427
         return m_server_private_key;
3✔
428
      }
429

430
      std::shared_ptr<Botan::Private_Key> private_key_for(const Public_Key& raw_public_key,
2✔
431
                                                          const std::string& type,
432
                                                          const std::string& context) override {
433
         BOTAN_UNUSED(type, context);
2✔
434
         std::vector<std::unique_ptr<Botan::Private_Key>> keys;
2✔
435
         keys.emplace_back(client_raw_public_key_pair());
2✔
436
         keys.emplace_back(server_raw_public_key_pair());
2✔
437
         for(auto& key : keys) {
3✔
438
            if(key->fingerprint_public() == raw_public_key.fingerprint_public()) {
3✔
439
               return std::move(key);
2✔
440
            }
441
         }
442
         return nullptr;
×
443
      }
2✔
444

445
      std::vector<TLS::ExternalPSK> find_preshared_keys(std::string_view /* host */,
8✔
446
                                                        TLS::Connection_Side /* whoami */,
447
                                                        const std::vector<std::string>& identities,
448
                                                        const std::optional<std::string>& prf) override {
449
         if(!m_external_psk.has_value()) {
8✔
450
            return {};
6✔
451
         }
452

453
         ExternalPSK& epsk = m_external_psk.value();
2✔
454
         const auto found = std::find(identities.begin(), identities.end(), epsk.identity());
2✔
455
         if(!identities.empty() && found == identities.end()) {
2✔
456
            return {};
×
457
         }
458

459
         if(prf && prf != epsk.prf_algo()) {
2✔
460
            return {};
×
461
         }
462

463
         // ExternalPSK has a deleted copy constructor. We need to do some gymnastics
464
         // to copy it and leave the data in m_external_psk intact
465
         const auto secret = epsk.extract_master_secret();
2✔
466
         m_external_psk = ExternalPSK(epsk.identity(), epsk.prf_algo(), secret);
2✔
467
         std::vector<ExternalPSK> psks;
2✔
468
         psks.emplace_back(epsk.identity(), epsk.prf_algo(), secret);
2✔
469
         return psks;
2✔
470
      }
4✔
471

472
   private:
473
      bool m_alternative_server_certificate;
474
      std::optional<ExternalPSK> m_external_psk;
475
      std::shared_ptr<Private_Key> m_client_private_key;
476
      std::shared_ptr<Private_Key> m_bogus_alternative_server_private_key;
477
      std::shared_ptr<Private_Key> m_server_private_key;
478
};
479

480
class RFC8448_Text_Policy : public Botan::TLS::Text_Policy {
14✔
481
   private:
482
      Botan::TLS::Text_Policy read_policy(const std::string& policy_file) {
14✔
483
         const std::string fspath = Test::data_file("tls-policy/" + policy_file + ".txt");
42✔
484

485
         std::ifstream is(fspath.c_str());
14✔
486
         if(!is.good()) {
14✔
487
            throw Test_Error("Missing policy file " + fspath);
×
488
         }
489

490
         return Botan::TLS::Text_Policy(is);
14✔
491
      }
14✔
492

493
   public:
494
      explicit RFC8448_Text_Policy(const std::string& policy_file, bool rfc8448 = true) :
14✔
495
            Botan::TLS::Text_Policy(read_policy(policy_file)), m_rfc8448(rfc8448) {}
14✔
496

497
      std::vector<Botan::TLS::Signature_Scheme> allowed_signature_schemes() const override {
16✔
498
         if(!m_rfc8448) {
16✔
499
            return Botan::TLS::Text_Policy::allowed_signature_schemes();
1✔
500
         }
501

502
         // We extend the allowed signature schemes with algorithms that we don't
503
         // actually support. The nature of the RFC 8448 test forces us to generate
504
         // bit-compatible TLS messages. Unfortunately, the test data offers all
505
         // those algorithms in its Client Hellos.
506
         return {
15✔
507
            Botan::TLS::Signature_Scheme::ECDSA_SHA256,
508
            Botan::TLS::Signature_Scheme::ECDSA_SHA384,
509
            Botan::TLS::Signature_Scheme::ECDSA_SHA512,
510
            Botan::TLS::Signature_Scheme::ECDSA_SHA1,  // not actually supported
511
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA256,
512
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA384,
513
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA512,
514
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA256,
515
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA384,
516
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA512,
517
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA1,  // not actually supported
518
            Botan::TLS::Signature_Scheme(0x0402),          // DSA_SHA256, not actually supported
519
            Botan::TLS::Signature_Scheme(0x0502),          // DSA_SHA384, not actually supported
520
            Botan::TLS::Signature_Scheme(0x0602),          // DSA_SHA512, not actually supported
521
            Botan::TLS::Signature_Scheme(0x0202),          // DSA_SHA1, not actually supported
522
         };
15✔
523
      }
524

525
      // Overriding the key exchange group selection to favour the server's key
526
      // exchange group preference. This is required to enforce a Hello Retry Request
527
      // when testing RFC 8448 5. from the server side.
528
      Named_Group choose_key_exchange_group(const std::vector<Group_Params>& supported_by_peer,
8✔
529
                                            const std::vector<Group_Params>& offered_by_peer) const override {
530
         BOTAN_UNUSED(offered_by_peer);
8✔
531

532
         const auto supported_by_us = key_exchange_groups();
8✔
533
         const auto selected_group =
8✔
534
            std::find_if(supported_by_us.begin(), supported_by_us.end(), [&](const auto group) {
8✔
535
               return value_exists(supported_by_peer, group);
16✔
536
            });
537

538
         return selected_group != supported_by_us.end() ? *selected_group : Named_Group::NONE;
8✔
539
      }
8✔
540

541
   private:
542
      bool m_rfc8448;
543
};
544

545
/**
546
 * In-Memory Session Manager that stores sessions verbatim, without encryption.
547
 * Therefor it is not dependent on a random number generator and can easily be
548
 * instrumented for test inspection.
549
 */
550
class RFC8448_Session_Manager : public Botan::TLS::Session_Manager {
551
   private:
552
      decltype(auto) find_by_handle(const Session_Handle& handle) {
3✔
553
         return [=](const Session_with_Handle& session) {
18✔
554
            if(session.handle.id().has_value() && handle.id().has_value() &&
4✔
555
               session.handle.id().value() == handle.id().value()) {
2✔
556
               return true;
557
            }
558
            if(session.handle.ticket().has_value() && handle.ticket().has_value() &&
8✔
559
               session.handle.ticket().value() == handle.ticket().value()) {
10✔
560
               return true;
561
            }
562
            return false;
563
         };
6✔
564
      }
565

566
   public:
567
      RFC8448_Session_Manager() : Session_Manager(std::make_shared<Botan::Null_RNG>()) {}
28✔
568

569
      const std::vector<Session_with_Handle>& all_sessions() const { return m_sessions; }
3✔
570

571
      void store(const Session& session, const Session_Handle& handle) override {
4✔
572
         m_sessions.push_back({session, handle});
4✔
573
      }
12✔
574

575
      std::optional<Session_Handle> establish(const Session& session,
1✔
576
                                              const std::optional<Session_ID>& /*session*/,
577
                                              bool /*no_ticket*/) override {
578
         // we assume that the 'mocked' session is already stored in the manager,
579
         // verify that it is equivalent to the one created by the testee and
580
         // return the associated handle stored with it
581

582
         if(m_sessions.size() != 1) {
1✔
583
            throw Test_Error("No mocked session handle available; Test bug?");
×
584
         }
585

586
         const auto& [mocked_session, handle] = m_sessions.front();
1✔
587
         if(mocked_session.master_secret() != session.master_secret()) {
1✔
588
            throw Test_Error("Generated session object does not match the expected mock");
×
589
         }
590

591
         return handle;
1✔
592
      }
593

594
      std::optional<Session> retrieve_one(const Session_Handle& handle) override {
2✔
595
         auto itr = std::find_if(m_sessions.begin(), m_sessions.end(), find_by_handle(handle));
2✔
596
         if(itr == m_sessions.end()) {
2✔
597
            return std::nullopt;
1✔
598
         } else {
599
            return itr->session;
1✔
600
         }
601
      }
602

603
      std::vector<Session_with_Handle> find_some(const Server_Information& info,
7✔
604
                                                 const size_t /*max_sessions_hint*/) override {
605
         std::vector<Session_with_Handle> found_sessions;
7✔
606
         for(const auto& [session, handle] : m_sessions) {
8✔
607
            if(session.server_info() == info) {
1✔
608
               found_sessions.emplace_back(Session_with_Handle{session, handle});
2✔
609
            }
610
         }
611

612
         return found_sessions;
7✔
613
      }
×
614

615
      size_t remove(const Session_Handle& handle) override { return std::erase_if(m_sessions, find_by_handle(handle)); }
2✔
616

617
      size_t remove_all() override {
×
618
         const auto sessions = m_sessions.size();
×
619
         m_sessions.clear();
×
620
         return sessions;
×
621
      }
622

623
   private:
624
      std::vector<Session_with_Handle> m_sessions;
625
};
626

627
/**
628
 * This steers the TLS client handle and is the central entry point for the
629
 * test cases to interact with the TLS 1.3 implementation.
630
 *
631
 * Note: This class is abstract to be subclassed for both client and server tests.
632
 */
633
class TLS_Context {
634
   protected:
635
      TLS_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng_in,
14✔
636
                  std::shared_ptr<const RFC8448_Text_Policy> policy,
637
                  Modify_Exts_Fn modify_exts_cb,
638
                  std::vector<MockSignature> mock_signatures,
639
                  uint64_t timestamp,
640
                  std::optional<std::pair<Session, Session_Ticket>> session_and_ticket,
641
                  std::optional<ExternalPSK> external_psk,
642
                  bool use_alternative_server_certificate) :
14✔
643
            m_callbacks(std::make_shared<Test_TLS_13_Callbacks>(
14✔
644
               std::move(modify_exts_cb), std::move(mock_signatures), timestamp)),
645
            m_creds(std::make_shared<Test_Credentials>(use_alternative_server_certificate, std::move(external_psk))),
14✔
646
            m_rng(std::move(rng_in)),
14✔
647
            m_session_mgr(std::make_shared<RFC8448_Session_Manager>()),
648
            m_policy(std::move(policy)) {
28✔
649
         if(session_and_ticket.has_value()) {
14✔
650
            m_session_mgr->store(std::get<Session>(session_and_ticket.value()),
6✔
651
                                 Botan::TLS::Session_Handle(std::get<Session_Ticket>(session_and_ticket.value())));
9✔
652
         }
653
      }
14✔
654

655
   public:
656
      virtual ~TLS_Context() = default;
70✔
657

658
      TLS_Context(TLS_Context&) = delete;
659
      TLS_Context& operator=(const TLS_Context&) = delete;
660

661
      TLS_Context(TLS_Context&&) = delete;
662
      TLS_Context& operator=(TLS_Context&&) = delete;
663

664
      std::vector<uint8_t> pull_send_buffer() { return m_callbacks->pull_send_buffer(); }
39✔
665

666
      std::vector<uint8_t> pull_receive_buffer() { return m_callbacks->pull_receive_buffer(); }
4✔
667

668
      uint64_t last_received_seq_no() const { return m_callbacks->last_received_seq_no(); }
4✔
669

670
      /**
671
       * Checks that all of the listed callbacks were called at least once, no other
672
       * callbacks were called in addition to the expected ones. After the checks are
673
       * done, the callback invocation counters are reset.
674
       */
675
      void check_callback_invocations(Test::Result& result,
60✔
676
                                      const std::string& context,
677
                                      const std::vector<std::string>& callback_names) {
678
         const auto& invokes = m_callbacks->callback_invocations();
60✔
679
         for(const auto& cbn : callback_names) {
371✔
680
            result.test_is_true(Botan::fmt("{} was invoked (Context: {})", cbn, context),
933✔
681
                                invokes.contains(cbn) && invokes.at(cbn) > 0);
311✔
682
         }
683

684
         for(const auto& invoke : invokes) {
371✔
685
            if(invoke.second == 0) {
311✔
686
               continue;
×
687
            }
688
            result.test_is_true(
622✔
689
               invoke.first + " was expected (Context: " + context + ")",
1,244✔
690
               std::find(callback_names.cbegin(), callback_names.cend(), invoke.first) != callback_names.cend());
622✔
691
         }
692

693
         m_callbacks->reset_callback_invocation_counters();
60✔
694
      }
60✔
695

696
      const std::vector<Session_with_Handle>& stored_sessions() const { return m_session_mgr->all_sessions(); }
3✔
697

698
      const std::vector<Botan::X509_Certificate>& certs_verified() const { return m_callbacks->certificate_chain; }
2✔
699

700
      const std::string& psk_identity_negotiated() const { return m_callbacks->negotiated_psk_identity; }
2✔
701

702
      decltype(auto) observed_handshake_messages() const { return m_callbacks->serialized_messages; }
7✔
703

704
      /**
705
       * Send application data through the secure channel
706
       */
707
      virtual void send(const std::vector<uint8_t>& data) = 0;
708

709
   protected:
710
      std::shared_ptr<Test_TLS_13_Callbacks> m_callbacks;  // NOLINT(*-non-private-member-variable*)
711
      std::shared_ptr<Test_Credentials> m_creds;           // NOLINT(*-non-private-member-variable*)
712

713
      std::shared_ptr<Botan::RandomNumberGenerator> m_rng;     // NOLINT(*-non-private-member-variable*)
714
      std::shared_ptr<RFC8448_Session_Manager> m_session_mgr;  // NOLINT(*-non-private-member-variable*)
715
      std::shared_ptr<const RFC8448_Text_Policy> m_policy;     // NOLINT(*-non-private-member-variable*)
716
};
717

718
class Client_Context : public TLS_Context {
719
   public:
720
      Client_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng_in,
7✔
721
                     std::shared_ptr<const RFC8448_Text_Policy> policy,
722
                     uint64_t timestamp,
723
                     Modify_Exts_Fn modify_exts_cb,
724
                     std::optional<std::pair<Session, Session_Ticket>> session_and_ticket = std::nullopt,
725
                     std::optional<ExternalPSK> external_psk = std::nullopt,
726
                     std::vector<MockSignature> mock_signatures = {}) :
7✔
727
            TLS_Context(std::move(rng_in),
728
                        std::move(policy),
729
                        std::move(modify_exts_cb),
730
                        std::move(mock_signatures),
731
                        timestamp,
732
                        std::move(session_and_ticket),
733
                        std::move(external_psk),
734
                        false),
735
            client(m_callbacks,
35✔
736
                   m_session_mgr,
7✔
737
                   m_creds,
7✔
738
                   m_policy,
7✔
739
                   m_rng,
7✔
740
                   Botan::TLS::Server_Information("server"),
14✔
741
                   Botan::TLS::Protocol_Version::TLS_V13) {}
31✔
742

743
      void send(const std::vector<uint8_t>& data) override { client.send(data.data(), data.size()); }
2✔
744

745
      Botan::TLS::Client client;  // NOLINT(*-non-private-member-variable*)
746
};
747

748
class Server_Context : public TLS_Context {
749
   public:
750
      Server_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng,
7✔
751
                     std::shared_ptr<const RFC8448_Text_Policy> policy,
752
                     uint64_t timestamp,
753
                     Modify_Exts_Fn modify_exts_cb,
754
                     std::vector<MockSignature> mock_signatures,
755
                     bool use_alternative_server_certificate = false,
756
                     std::optional<std::pair<Session, Session_Ticket>> session_and_ticket = std::nullopt,
757
                     std::optional<ExternalPSK> external_psk = std::nullopt) :
7✔
758
            TLS_Context(std::move(rng),
759
                        std::move(policy),
760
                        std::move(modify_exts_cb),
761
                        std::move(mock_signatures),
762
                        timestamp,
763
                        std::move(session_and_ticket),
764
                        std::move(external_psk),
765
                        use_alternative_server_certificate),
766
            server(m_callbacks, m_session_mgr, m_creds, m_policy, m_rng, false /* DTLS NYI */) {}
53✔
767

768
      void send(const std::vector<uint8_t>& data) override { server.send(data.data(), data.size()); }
2✔
769

770
      Botan::TLS::Server server;  // NOLINT(*-non-private-member-variable*)
771
};
772

773
void sort_extensions(Botan::TLS::Extensions& exts, const std::vector<Botan::TLS::Extension_Code>& expected_order) {
30✔
774
   for(const auto ext_type : expected_order) {
283✔
775
      auto ext = exts.take(ext_type);
253✔
776
      if(ext != nullptr) {
253✔
777
         exts.add(std::move(ext));
230✔
778
      }
779
   }
253✔
780
}
30✔
781

782
/**
783
 * Because of the nature of the RFC 8448 test data we need to produce bit-compatible
784
 * TLS messages. Hence we sort the generated TLS extensions exactly as expected.
785
 */
786
void sort_rfc8448_extensions(Botan::TLS::Extensions& exts,
23✔
787
                             Botan::TLS::Connection_Side side,
788
                             Botan::TLS::Handshake_Type /*type*/ = Botan::TLS::Handshake_Type::ClientHello) {
789
   if(side == Botan::TLS::Connection_Side::Client) {
23✔
790
      sort_extensions(exts,
12✔
791
                      {
792
                         Botan::TLS::Extension_Code::ServerNameIndication,
793
                         Botan::TLS::Extension_Code::SafeRenegotiation,
794
                         Botan::TLS::Extension_Code::SupportedGroups,
795
                         Botan::TLS::Extension_Code::SessionTicket,
796
                         Botan::TLS::Extension_Code::KeyShare,
797
                         Botan::TLS::Extension_Code::EarlyData,
798
                         Botan::TLS::Extension_Code::SupportedVersions,
799
                         Botan::TLS::Extension_Code::SignatureAlgorithms,
800
                         Botan::TLS::Extension_Code::Cookie,
801
                         Botan::TLS::Extension_Code::PskKeyExchangeModes,
802
                         Botan::TLS::Extension_Code::RecordSizeLimit,
803
                         Padding::static_type(),
804
                         Botan::TLS::Extension_Code::PresharedKey,
805
                      });
806
   } else {
807
      sort_extensions(exts,
34✔
808
                      {
809
                         Botan::TLS::Extension_Code::SupportedGroups,
810
                         Botan::TLS::Extension_Code::KeyShare,
811
                         Botan::TLS::Extension_Code::Cookie,
812
                         Botan::TLS::Extension_Code::SupportedVersions,
813
                         Botan::TLS::Extension_Code::SignatureAlgorithms,
814
                         Botan::TLS::Extension_Code::RecordSizeLimit,
815
                         Botan::TLS::Extension_Code::ServerNameIndication,
816
                         Botan::TLS::Extension_Code::EarlyData,
817
                      });
818
   }
819
}
23✔
820

821
void add_renegotiation_extension(Botan::TLS::Extensions& exts) {
5✔
822
   // Renegotiation is not possible in TLS 1.3. Nevertheless, RFC 8448 requires
823
   // to add this to the Client Hello for reasons.
824
   exts.add(new Renegotiation_Extension());  // NOLINT(*-owning-memory)
5✔
825
}
5✔
826

827
void add_early_data_indication(Botan::TLS::Extensions& exts) {
1✔
828
   exts.add(new Botan::TLS::EarlyDataIndication());  // NOLINT(*-owning-memory)
1✔
829
}
1✔
830

831
std::vector<uint8_t> strip_message_header(const std::vector<uint8_t>& msg) {
31✔
832
   BOTAN_ASSERT_NOMSG(msg.size() >= 4);
31✔
833
   return {msg.begin() + 4, msg.end()};
31✔
834
}
835

836
std::vector<MockSignature> make_mock_signatures(const VarMap& vars) {
9✔
837
   std::vector<MockSignature> result;
9✔
838

839
   auto mock = [&](const std::string& msg, const std::string& sig) {
27✔
840
      if(vars.has_key(msg) && vars.has_key(sig)) {
18✔
841
         result.push_back({vars.get_opt_bin(msg), vars.get_opt_bin(sig)});
11✔
842
      }
843
   };
29✔
844

845
   mock("Server_MessageToSign", "Server_MessageSignature");
18✔
846
   mock("Client_MessageToSign", "Client_MessageSignature");
18✔
847

848
   return result;
9✔
849
}
×
850

851
}  // namespace
852

853
/**
854
 * Traffic transcripts and supporting data for the TLS RFC 8448 and TLS policy
855
 * configuration is kept in data files (accessible via `Test:::data_file()`).
856
 *
857
 * tls_13_rfc8448/transcripts.vec
858
 *   The record transcripts and RNG outputs as defined/required in RFC 8448 in
859
 *   Botan's Text_Based_Test vector format. Data from each RFC 8448 section is
860
 *   placed in a sub-section of the *.vec file. Each of those sections needs a
861
 *   specific test case implementation that is dispatched in `run_one_test()`.
862
 *
863
 * tls_13_rfc8448/client_certificate.pem
864
 *   The client certificate provided in RFC 8448 used to perform client auth.
865
 *   Note that RFC 8448 _does not_ provide the associated private key but only
866
 *   the resulting signature in the client's CertificateVerify message.
867
 *
868
 * tls_13_rfc8448/server_certificate.pem
869
 * tls_13_rfc8448/server_key.pem
870
 *   The server certificate and its associated private key.
871
 *
872
 * tls_13_rfc8448/server_certificate_client_auth.pem
873
 *   The server certificate used in the Client Authentication test case.
874
 *
875
 * tls_13_rfc8448/client_raw_public_keypair.pem
876
 * tls_13_rfc8448/server_raw_public_keypair.pem
877
 *   The raw public key pairs for client and server authentication in the
878
 *   equally named test cases.
879
 *
880
 * tls-policy/rfc8448_*.txt
881
 *   Each RFC 8448 section test required a slightly adapted Botan TLS policy
882
 *   to enable/disable certain features under test.
883
 *
884
 * While the test cases are split into Client-side and Server-side tests, the
885
 * transcript data is reused. See the concrete implementations of the abstract
886
 * Test_TLS_RFC8448 test class.
887
 */
888
class Test_TLS_RFC8448 : public Text_Based_Test {
889
   protected:
890
      // Those tests are based on the test vectors in RFC8448.
891
      virtual std::vector<Test::Result> simple_1_rtt(const VarMap& vars) = 0;
892
      virtual std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) = 0;
893
      virtual std::vector<Test::Result> hello_retry_request(const VarMap& vars) = 0;
894
      virtual std::vector<Test::Result> client_authentication(const VarMap& vars) = 0;
895
      virtual std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) = 0;
896

897
      // Those tests provide the same information as RFC8448 test vectors but
898
      // were sourced otherwise. Typically by temporarily instrumenting our implementation.
899
      virtual std::vector<Test::Result> externally_provided_psk_with_ephemeral_key(const VarMap& vars) = 0;
900
      virtual std::vector<Test::Result> raw_public_key_with_client_authentication(const VarMap& vars) = 0;
901

902
      virtual std::string side() const = 0;
903

904
   public:
905
      Test_TLS_RFC8448() :
2✔
906
            Text_Based_Test("tls_13_rfc8448/transcripts.vec",
907
                            // mandatory data fields
908
                            "Client_RNG_Pool,"
909
                            "Server_RNG_Pool,"
910
                            "CurrentTimestamp,"
911
                            "Record_ClientHello_1,"
912
                            "Record_ServerHello,"
913
                            "Record_ServerHandshakeMessages,"
914
                            "Record_ClientFinished,"
915
                            "Record_Client_CloseNotify,"
916
                            "Record_Server_CloseNotify",
917
                            // optional data fields
918
                            "Message_ServerHello,"
919
                            "Message_EncryptedExtensions,"
920
                            "Message_CertificateRequest,"
921
                            "Message_Server_Certificate,"
922
                            "Message_Server_CertificateVerify,"
923
                            "Message_Server_Finished,"
924
                            "Record_HelloRetryRequest,"
925
                            "Record_ClientHello_2,"
926
                            "Record_NewSessionTicket,"
927
                            "Client_AppData,"
928
                            "Record_Client_AppData,"
929
                            "Server_AppData,"
930
                            "Record_Server_AppData,"
931
                            "Client_EarlyAppData,"
932
                            "Record_Client_EarlyAppData,"
933
                            "SessionTicket,"
934
                            "Client_SessionData,"
935
                            "Server_MessageToSign,"
936
                            "Server_MessageSignature,"
937
                            "Client_MessageToSign,"
938
                            "Client_MessageSignature,"
939
                            "HelloRetryRequest_Cookie,"
940
                            "PskIdentity,"
941
                            "PskPRF,"
942
                            "PskSecret") {}
4✔
943

944
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
14✔
945
         if(header == "Simple_1RTT_Handshake") {
14✔
946
            return Test::Result("Simple 1-RTT (" + side() + " side)", simple_1_rtt(vars));
8✔
947
         } else if(header == "Resumed_0RTT_Handshake") {
12✔
948
            return Test::Result("Resumption with 0-RTT data (" + side() + " side)", resumed_handshake_with_0_rtt(vars));
8✔
949
         } else if(header == "HelloRetryRequest_Handshake") {
10✔
950
            return Test::Result("Handshake involving Hello Retry Request (" + side() + " side)",
8✔
951
                                hello_retry_request(vars));
6✔
952
         } else if(header == "Client_Authentication_Handshake") {
8✔
953
            return Test::Result("Client Authentication (" + side() + " side)", client_authentication(vars));
8✔
954
         } else if(header == "Middlebox_Compatibility_Mode") {
6✔
955
            return Test::Result("Middlebox Compatibility Mode (" + side() + " side)", middlebox_compatibility(vars));
8✔
956
         } else if(header == "Externally_Provided_PSK_with_Ephemeral_Key") {
4✔
957
            return Test::Result("Externally Provided PSK with ephemeral key (" + side() + " side)",
8✔
958
                                externally_provided_psk_with_ephemeral_key(vars));
6✔
959
         } else if(header == "RawPublicKey_With_Client_Authentication") {
2✔
960
            return Test::Result("RawPublicKey with Client Authentication (" + side() + " side)",
8✔
961
                                raw_public_key_with_client_authentication(vars));
6✔
962
         } else {
963
            return Test::Result::Failure("test dispatcher", "unknown sub-test: " + header);
×
964
         }
965
      }
966
};
967

968
class Test_TLS_RFC8448_Client : public Test_TLS_RFC8448 {
1✔
969
   private:
970
      std::string side() const override { return "Client"; }
7✔
971

972
      std::vector<Test::Result> simple_1_rtt(const VarMap& vars) override {
1✔
973
         auto rng = std::make_shared<Fixed_Output_RNG>("");
1✔
974

975
         // 32 - for client hello random
976
         // 32 - for KeyShare (eph. x25519 key pair)
977
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
978

979
         auto add_extensions_and_sort = [](Botan::TLS::Extensions& exts,
2✔
980
                                           Botan::TLS::Connection_Side side,
981
                                           Botan::TLS::Handshake_Type which_message) {
982
            if(which_message == Handshake_Type::ClientHello) {
1✔
983
               // For some reason, presumably checking compatibility, the RFC 8448 Client
984
               // Hello includes a (TLS 1.2) Session_Ticket extension. We don't normally add
985
               // this obsoleted extension in a TLS 1.3 client.
986
               exts.add(new Botan::TLS::Session_Ticket_Extension());  // NOLINT(*-owning-memory)
1✔
987

988
               add_renegotiation_extension(exts);
1✔
989
               sort_rfc8448_extensions(exts, side);
1✔
990
            }
991
         };
1✔
992

993
         std::unique_ptr<Client_Context> ctx;
1✔
994

995
         return {
1✔
996
            CHECK("Client Hello",
997
                  [&](Test::Result& result) {
1✔
998
                     ctx = std::make_unique<Client_Context>(rng,
1✔
999
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1000
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1001
                                                            add_extensions_and_sort);
1✔
1002

1003
                     result.test_is_true("client not closed", !ctx->client.is_closed());
1✔
1004
                     ctx->check_callback_invocations(result,
2✔
1005
                                                     "client hello prepared",
1006
                                                     {
1007
                                                        "tls_emit_data",
1008
                                                        "tls_inspect_handshake_msg_client_hello",
1009
                                                        "tls_modify_extensions_client_hello",
1010
                                                        "tls_generate_ephemeral_key",
1011
                                                        "tls_current_timestamp",
1012
                                                     });
1013

1014
                     result.test_bin_eq(
1✔
1015
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1016
                  }),
1✔
1017

1018
            CHECK("Server Hello",
1019
                  [&](Test::Result& result) {
1✔
1020
                     result.require("ctx is available", ctx != nullptr);
1✔
1021
                     const auto server_hello = vars.get_req_bin("Record_ServerHello");
1✔
1022
                     // splitting the input data to test partial reads
1023
                     const std::vector<uint8_t> server_hello_a(server_hello.begin(), server_hello.begin() + 20);
1✔
1024
                     const std::vector<uint8_t> server_hello_b(server_hello.begin() + 20, server_hello.end());
1✔
1025

1026
                     ctx->client.received_data(server_hello_a);
1✔
1027
                     ctx->check_callback_invocations(result, "server hello partially received", {});
2✔
1028

1029
                     ctx->client.received_data(server_hello_b);
1✔
1030
                     ctx->check_callback_invocations(result,
2✔
1031
                                                     "server hello received",
1032
                                                     {"tls_inspect_handshake_msg_server_hello",
1033
                                                      "tls_examine_extensions_server_hello",
1034
                                                      "tls_ephemeral_key_agreement"});
1035

1036
                     result.test_is_true("client is not yet active", !ctx->client.is_active());
1✔
1037
                     result.test_is_true("handshake is not yet complete", !ctx->client.is_handshake_complete());
1✔
1038
                  }),
3✔
1039

1040
            CHECK("Server HS messages .. Client Finished",
1041
                  [&](Test::Result& result) {
1✔
1042
                     result.require("ctx is available", ctx != nullptr);
1✔
1043
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1044

1045
                     ctx->check_callback_invocations(result,
2✔
1046
                                                     "encrypted handshake messages received",
1047
                                                     {"tls_inspect_handshake_msg_encrypted_extensions",
1048
                                                      "tls_inspect_handshake_msg_certificate",
1049
                                                      "tls_inspect_handshake_msg_certificate_verify",
1050
                                                      "tls_inspect_handshake_msg_finished",
1051
                                                      "tls_examine_extensions_encrypted_extensions",
1052
                                                      "tls_examine_extensions_certificate",
1053
                                                      "tls_emit_data",
1054
                                                      "tls_current_timestamp",
1055
                                                      "tls_session_established",
1056
                                                      "tls_session_activated",
1057
                                                      "tls_verify_cert_chain",
1058
                                                      "tls_verify_message"});
1059
                     result.require("certificate exists", !ctx->certs_verified().empty());
1✔
1060
                     result.require("correct certificate", ctx->certs_verified().front() == server_certificate());
1✔
1061
                     result.require("client is active", ctx->client.is_active());
1✔
1062
                     result.test_is_true("handshake is complete", ctx->client.is_handshake_complete());
1✔
1063

1064
                     result.test_bin_eq("correct handshake finished",
1✔
1065
                                        ctx->pull_send_buffer(),
2✔
1066
                                        vars.get_req_bin("Record_ClientFinished"));
2✔
1067
                  }),
1✔
1068

1069
            CHECK("Post-Handshake: NewSessionTicket",
1070
                  [&](Test::Result& result) {
1✔
1071
                     result.require("ctx is available", ctx != nullptr);
1✔
1072
                     result.require("no sessions so far", ctx->stored_sessions().empty());
1✔
1073
                     ctx->client.received_data(vars.get_req_bin("Record_NewSessionTicket"));
2✔
1074

1075
                     ctx->check_callback_invocations(result,
2✔
1076
                                                     "new session ticket received",
1077
                                                     {"tls_examine_extensions_new_session_ticket",
1078
                                                      "tls_should_persist_resumption_information",
1079
                                                      "tls_current_timestamp"});
1080
                     if(result.test_sz_eq("session was stored", ctx->stored_sessions().size(), 1)) {
1✔
1081
                        const auto& [stored_session, stored_handle] = ctx->stored_sessions().front();
1✔
1082
                        result.require("session handle contains a ticket", stored_handle.ticket().has_value());
1✔
1083
                        result.test_bin_eq("session was serialized as expected",
1✔
1084
                                           stored_session.DER_encode(),
2✔
1085
                                           vars.get_req_bin("Client_SessionData"));
2✔
1086
                     }
1087
                  }),
1✔
1088

1089
            CHECK("Send Application Data",
1090
                  [&](Test::Result& result) {
1✔
1091
                     result.require("ctx is available", ctx != nullptr);
1✔
1092
                     ctx->send(vars.get_req_bin("Client_AppData"));
3✔
1093

1094
                     ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
2✔
1095

1096
                     result.test_bin_eq("correct client application data",
1✔
1097
                                        ctx->pull_send_buffer(),
2✔
1098
                                        vars.get_req_bin("Record_Client_AppData"));
2✔
1099
                  }),
1✔
1100

1101
            CHECK("Receive Application Data",
1102
                  [&](Test::Result& result) {
1✔
1103
                     result.require("ctx is available", ctx != nullptr);
1✔
1104
                     ctx->client.received_data(vars.get_req_bin("Record_Server_AppData"));
2✔
1105

1106
                     ctx->check_callback_invocations(result, "application data sent", {"tls_record_received"});
2✔
1107

1108
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1109
                     result.test_bin_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
2✔
1110
                     result.test_u64_eq("sequence number", ctx->last_received_seq_no(), uint64_t(1));
1✔
1111
                  }),
1✔
1112

1113
            CHECK("Close Connection",
1114
                  [&](Test::Result& result) {
1✔
1115
                     result.require("ctx is available", ctx != nullptr);
1✔
1116
                     ctx->client.close();
1✔
1117

1118
                     result.test_bin_eq(
1✔
1119
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1120
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1121

1122
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1123
                     ctx->check_callback_invocations(
2✔
1124
                        result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1125

1126
                     result.test_is_true("connection is closed", ctx->client.is_closed());
1✔
1127
                  }),
1✔
1128
         };
8✔
1129
      }
3✔
1130

1131
      std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) override {
1✔
1132
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1133

1134
         // 32 - for client hello random
1135
         // 32 - for KeyShare (eph. x25519 key pair)
1136
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1137

1138
         auto add_extensions_and_sort = [](Botan::TLS::Extensions& exts,
2✔
1139
                                           Botan::TLS::Connection_Side side,
1140
                                           Botan::TLS::Handshake_Type which_message) {
1141
            if(which_message == Handshake_Type::ClientHello) {
1✔
1142
               exts.add(new Padding(87));  // NOLINT(*-owning-memory)
1✔
1143

1144
               add_renegotiation_extension(exts);
1✔
1145

1146
               // TODO: Implement early data support and remove this 'hack'.
1147
               //
1148
               // Currently, the production implementation will never add this
1149
               // extension even if the resumed session would allow early data.
1150
               add_early_data_indication(exts);
1✔
1151
               sort_rfc8448_extensions(exts, side);
1✔
1152
            }
1153
         };
1✔
1154

1155
         std::unique_ptr<Client_Context> ctx;
1✔
1156

1157
         return {
1✔
1158
            CHECK("Client Hello",
1159
                  [&](Test::Result& result) {
1✔
1160
                     ctx = std::make_unique<Client_Context>(
1✔
1161
                        std::move(rng),
1162
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1163
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1164
                        add_extensions_and_sort,
1165
                        std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1166
                                  Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1167

1168
                     result.test_is_true("client not closed", !ctx->client.is_closed());
1✔
1169
                     ctx->check_callback_invocations(result,
2✔
1170
                                                     "client hello prepared",
1171
                                                     {
1172
                                                        "tls_emit_data",
1173
                                                        "tls_inspect_handshake_msg_client_hello",
1174
                                                        "tls_modify_extensions_client_hello",
1175
                                                        "tls_current_timestamp",
1176
                                                        "tls_generate_ephemeral_key",
1177
                                                     });
1178

1179
                     result.test_bin_eq(
1✔
1180
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1181
                  })
1✔
1182

1183
            // TODO: The rest of this test vector requires 0-RTT which is not
1184
            //       yet implemented. For now we can only test the client's
1185
            //       ability to offer a session resumption via PSK.
1186
         };
2✔
1187
      }
2✔
1188

1189
      std::vector<Test::Result> hello_retry_request(const VarMap& vars) override {
1✔
1190
         auto add_extensions_and_sort = [flights = 0](Botan::TLS::Extensions& exts,
3✔
1191
                                                      Botan::TLS::Connection_Side side,
1192
                                                      Botan::TLS::Handshake_Type which_message) mutable {
1193
            if(which_message == Handshake_Type::ClientHello) {
2✔
1194
               ++flights;
2✔
1195

1196
               if(flights == 1) {
2✔
1197
                  add_renegotiation_extension(exts);
1✔
1198
               }
1199

1200
               // For some reason RFC8448 decided to require this (fairly obscure) extension
1201
               // in the second flight of the Client_Hello.
1202
               if(flights == 2) {
2✔
1203
                  exts.add(new Padding(175));  // NOLINT(*-owning-memory)
1✔
1204
               }
1205

1206
               sort_rfc8448_extensions(exts, side);
2✔
1207
            }
1208
         };
2✔
1209

1210
         // Fallback RNG is required to for blinding in ECDH with P-256
1211
         auto& fallback_rng = this->rng();
1✔
1212
         auto rng = std::make_unique<Fixed_Output_RNG>(fallback_rng);
1✔
1213

1214
         // 32 - client hello random
1215
         // 32 - eph. x25519 key pair
1216
         // 32 - eph. P-256 key pair
1217
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1218

1219
         std::unique_ptr<Client_Context> ctx;
1✔
1220

1221
         return {
1✔
1222
            CHECK("Client Hello",
1223
                  [&](Test::Result& result) {
1✔
1224
                     ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1225
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_client"),
2✔
1226
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1227
                                                            add_extensions_and_sort);
1✔
1228
                     result.test_is_true("client not closed", !ctx->client.is_closed());
1✔
1229

1230
                     ctx->check_callback_invocations(result,
2✔
1231
                                                     "client hello prepared",
1232
                                                     {
1233
                                                        "tls_emit_data",
1234
                                                        "tls_inspect_handshake_msg_client_hello",
1235
                                                        "tls_modify_extensions_client_hello",
1236
                                                        "tls_generate_ephemeral_key",
1237
                                                        "tls_current_timestamp",
1238
                                                     });
1239

1240
                     result.test_bin_eq(
1✔
1241
                        "TLS client hello (1)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1242
                  }),
1✔
1243

1244
            CHECK("Hello Retry Request .. second Client Hello",
1245
                  [&](Test::Result& result) {
1✔
1246
                     result.require("ctx is available", ctx != nullptr);
1✔
1247
                     ctx->client.received_data(vars.get_req_bin("Record_HelloRetryRequest"));
2✔
1248

1249
                     ctx->check_callback_invocations(result,
2✔
1250
                                                     "hello retry request received",
1251
                                                     {
1252
                                                        "tls_emit_data",
1253
                                                        "tls_inspect_handshake_msg_hello_retry_request",
1254
                                                        "tls_examine_extensions_hello_retry_request",
1255
                                                        "tls_inspect_handshake_msg_client_hello",
1256
                                                        "tls_modify_extensions_client_hello",
1257
                                                        "tls_generate_ephemeral_key",
1258
                                                     });
1259

1260
                     result.test_bin_eq(
1✔
1261
                        "TLS client hello (2)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_2"));
3✔
1262
                  }),
1✔
1263

1264
            CHECK("Server Hello",
1265
                  [&](Test::Result& result) {
1✔
1266
                     result.require("ctx is available", ctx != nullptr);
1✔
1267
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1268

1269
                     ctx->check_callback_invocations(result,
2✔
1270
                                                     "server hello received",
1271
                                                     {
1272
                                                        "tls_inspect_handshake_msg_server_hello",
1273
                                                        "tls_examine_extensions_server_hello",
1274
                                                        "tls_ephemeral_key_agreement",
1275
                                                     });
1276
                  }),
1✔
1277

1278
            CHECK("Server HS Messages .. Client Finished",
1279
                  [&](Test::Result& result) {
1✔
1280
                     result.require("ctx is available", ctx != nullptr);
1✔
1281
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1282

1283
                     ctx->check_callback_invocations(result,
2✔
1284
                                                     "encrypted handshake messages received",
1285
                                                     {"tls_inspect_handshake_msg_encrypted_extensions",
1286
                                                      "tls_inspect_handshake_msg_certificate",
1287
                                                      "tls_inspect_handshake_msg_certificate_verify",
1288
                                                      "tls_inspect_handshake_msg_finished",
1289
                                                      "tls_examine_extensions_encrypted_extensions",
1290
                                                      "tls_examine_extensions_certificate",
1291
                                                      "tls_emit_data",
1292
                                                      "tls_current_timestamp",
1293
                                                      "tls_session_established",
1294
                                                      "tls_session_activated",
1295
                                                      "tls_verify_cert_chain",
1296
                                                      "tls_verify_message"});
1297

1298
                     result.test_bin_eq(
1✔
1299
                        "client finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1300
                  }),
1✔
1301

1302
            CHECK("Close Connection",
1303
                  [&](Test::Result& result) {
1✔
1304
                     result.require("ctx is available", ctx != nullptr);
1✔
1305
                     ctx->client.close();
1✔
1306
                     ctx->check_callback_invocations(
2✔
1307
                        result, "encrypted handshake messages received", {"tls_emit_data"});
1308
                     result.test_bin_eq(
1✔
1309
                        "client close notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1310

1311
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1312
                     ctx->check_callback_invocations(
2✔
1313
                        result, "encrypted handshake messages received", {"tls_alert", "tls_peer_closed_connection"});
1314

1315
                     result.test_is_true("connection is closed", ctx->client.is_closed());
1✔
1316
                  }),
1✔
1317
         };
6✔
1318
      }
2✔
1319

1320
      std::vector<Test::Result> client_authentication(const VarMap& vars) override {
1✔
1321
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1322

1323
         // 32 - for client hello random
1324
         // 32 - for eph. x25519 key pair
1325
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1326

1327
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
3✔
1328
                                            Botan::TLS::Connection_Side side,
1329
                                            Botan::TLS::Handshake_Type which_message) {
1330
            if(which_message == Handshake_Type::ClientHello) {
2✔
1331
               add_renegotiation_extension(exts);
1✔
1332
               sort_rfc8448_extensions(exts, side);
1✔
1333
            }
1334
         };
1335

1336
         std::unique_ptr<Client_Context> ctx;
1✔
1337

1338
         return {
1✔
1339
            CHECK("Client Hello",
1340
                  [&](Test::Result& result) {
1✔
1341
                     ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1342
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1343
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1344
                                                            add_extensions_and_sort,
1345
                                                            std::nullopt,
1346
                                                            std::nullopt,
1347
                                                            make_mock_signatures(vars));
3✔
1348

1349
                     ctx->check_callback_invocations(result,
2✔
1350
                                                     "initial callbacks",
1351
                                                     {
1352
                                                        "tls_emit_data",
1353
                                                        "tls_inspect_handshake_msg_client_hello",
1354
                                                        "tls_modify_extensions_client_hello",
1355
                                                        "tls_generate_ephemeral_key",
1356
                                                        "tls_current_timestamp",
1357
                                                     });
1358

1359
                     result.test_bin_eq(
1✔
1360
                        "Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1361
                  }),
1✔
1362

1363
            CHECK("Server Hello",
1364
                  [&](auto& result) {
1✔
1365
                     result.require("ctx is available", ctx != nullptr);
1✔
1366
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1367

1368
                     ctx->check_callback_invocations(result,
2✔
1369
                                                     "callbacks after server hello",
1370
                                                     {
1371
                                                        "tls_examine_extensions_server_hello",
1372
                                                        "tls_inspect_handshake_msg_server_hello",
1373
                                                        "tls_ephemeral_key_agreement",
1374
                                                     });
1375
                  }),
1✔
1376

1377
            CHECK("other handshake messages and client auth",
1378
                  [&](Test::Result& result) {
1✔
1379
                     result.require("ctx is available", ctx != nullptr);
1✔
1380
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1381

1382
                     ctx->check_callback_invocations(result,
2✔
1383
                                                     "signing callbacks invoked",
1384
                                                     {
1385
                                                        "tls_sign_message",
1386
                                                        "tls_emit_data",
1387
                                                        "tls_examine_extensions_encrypted_extensions",
1388
                                                        "tls_examine_extensions_certificate",
1389
                                                        "tls_examine_extensions_certificate_request",
1390
                                                        "tls_modify_extensions_certificate",
1391
                                                        "tls_inspect_handshake_msg_certificate",
1392
                                                        "tls_inspect_handshake_msg_certificate_request",
1393
                                                        "tls_inspect_handshake_msg_certificate_verify",
1394
                                                        "tls_inspect_handshake_msg_encrypted_extensions",
1395
                                                        "tls_inspect_handshake_msg_finished",
1396
                                                        "tls_current_timestamp",
1397
                                                        "tls_session_established",
1398
                                                        "tls_session_activated",
1399
                                                        "tls_verify_cert_chain",
1400
                                                        "tls_verify_message",
1401
                                                     });
1402

1403
                     // ClientFinished contains the entire coalesced client authentication flight
1404
                     // Messages: Certificate, CertificateVerify, Finished
1405
                     result.test_bin_eq(
1✔
1406
                        "Client Auth and Finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1407
                  }),
1✔
1408

1409
            CHECK("Close Connection",
1410
                  [&](Test::Result& result) {
1✔
1411
                     result.require("ctx is available", ctx != nullptr);
1✔
1412
                     ctx->client.close();
1✔
1413
                     result.test_bin_eq(
1✔
1414
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1415

1416
                     ctx->check_callback_invocations(result,
2✔
1417
                                                     "after sending close notify",
1418
                                                     {
1419
                                                        "tls_emit_data",
1420
                                                     });
1421

1422
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1423
                     result.test_is_true("connection closed", ctx->client.is_closed());
1✔
1424

1425
                     ctx->check_callback_invocations(
2✔
1426
                        result, "after receiving close notify", {"tls_alert", "tls_peer_closed_connection"});
1427
                  }),
1✔
1428
         };
5✔
1429
      }
2✔
1430

1431
      std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) override {
1✔
1432
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1433

1434
         // 32 - client hello random
1435
         // 32 - legacy session ID
1436
         // 32 - eph. x25519 key pair
1437
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1438

1439
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
2✔
1440
                                            Botan::TLS::Connection_Side side,
1441
                                            Botan::TLS::Handshake_Type which_message) {
1442
            if(which_message == Handshake_Type::ClientHello) {
1✔
1443
               add_renegotiation_extension(exts);
1✔
1444
               sort_rfc8448_extensions(exts, side);
1✔
1445
            }
1446
         };
1447

1448
         std::unique_ptr<Client_Context> ctx;
1✔
1449

1450
         return {
1✔
1451
            CHECK(
1452
               "Client Hello",
1453
               [&](Test::Result& result) {
1✔
1454
                  ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1455
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_client"),
2✔
1456
                                                         vars.get_req_u64("CurrentTimestamp"),
1✔
1457
                                                         add_extensions_and_sort);
1✔
1458

1459
                  result.test_bin_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1460

1461
                  ctx->check_callback_invocations(result,
2✔
1462
                                                  "client hello prepared",
1463
                                                  {
1464
                                                     "tls_emit_data",
1465
                                                     "tls_inspect_handshake_msg_client_hello",
1466
                                                     "tls_modify_extensions_client_hello",
1467
                                                     "tls_generate_ephemeral_key",
1468
                                                     "tls_current_timestamp",
1469
                                                  });
1470
               }),
1✔
1471

1472
            CHECK("Server Hello + other handshake messages",
1473
                  [&](Test::Result& result) {
1✔
1474
                     result.require("ctx is available", ctx != nullptr);
1✔
1475
                     ctx->client.received_data(
2✔
1476
                        Botan::concat(vars.get_req_bin("Record_ServerHello"),
4✔
1477
                                      // ServerHandshakeMessages contains the expected ChangeCipherSpec record
1478
                                      vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1479

1480
                     ctx->check_callback_invocations(result,
2✔
1481
                                                     "callbacks after server's first flight",
1482
                                                     {
1483
                                                        "tls_inspect_handshake_msg_server_hello",
1484
                                                        "tls_inspect_handshake_msg_encrypted_extensions",
1485
                                                        "tls_inspect_handshake_msg_certificate",
1486
                                                        "tls_inspect_handshake_msg_certificate_verify",
1487
                                                        "tls_inspect_handshake_msg_finished",
1488
                                                        "tls_examine_extensions_server_hello",
1489
                                                        "tls_examine_extensions_encrypted_extensions",
1490
                                                        "tls_examine_extensions_certificate",
1491
                                                        "tls_emit_data",
1492
                                                        "tls_current_timestamp",
1493
                                                        "tls_session_established",
1494
                                                        "tls_session_activated",
1495
                                                        "tls_verify_cert_chain",
1496
                                                        "tls_verify_message",
1497
                                                        "tls_ephemeral_key_agreement",
1498
                                                     });
1499

1500
                     result.test_bin_eq("CCS + Client Finished",
1✔
1501
                                        ctx->pull_send_buffer(),
2✔
1502
                                        // ClientFinished contains the expected ChangeCipherSpec record
1503
                                        vars.get_req_bin("Record_ClientFinished"));
2✔
1504

1505
                     result.test_is_true("client is ready to send application traffic", ctx->client.is_active());
1✔
1506
                     result.test_is_true("handshake is complete", ctx->client.is_handshake_complete());
1✔
1507
                  }),
1✔
1508

1509
            CHECK("Close connection",
1510
                  [&](Test::Result& result) {
1✔
1511
                     result.require("ctx is available", ctx != nullptr);
1✔
1512
                     ctx->client.close();
1✔
1513

1514
                     result.test_bin_eq(
1✔
1515
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1516

1517
                     result.require("client cannot send application traffic anymore", !ctx->client.is_active());
1✔
1518
                     result.require("client is not fully closed yet", !ctx->client.is_closed());
1✔
1519
                     result.test_is_true("handshake stays completed", ctx->client.is_handshake_complete());
1✔
1520

1521
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1522

1523
                     result.test_is_true("client connection was terminated", ctx->client.is_closed());
1✔
1524
                  }),
1✔
1525
         };
4✔
1526
      }
2✔
1527

1528
      std::vector<Test::Result> externally_provided_psk_with_ephemeral_key(const VarMap& vars) override {
1✔
1529
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1530

1531
         // 32 - for client hello random
1532
         // 32 - for KeyShare (eph. x25519 key pair)
1533
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1534

1535
         auto sort_our_extensions = [](Botan::TLS::Extensions& exts,
2✔
1536
                                       Botan::TLS::Connection_Side /* side */,
1537
                                       Botan::TLS::Handshake_Type /* which_message */) {
1538
            // This is the order of extensions when we first introduced the PSK
1539
            // implementation and generated the transcript. To stay compatible
1540
            // with the now hard-coded transcript, we pin the extension order.
1541
            sort_extensions(exts,
1✔
1542
                            {
1543
                               Botan::TLS::Extension_Code::ServerNameIndication,
1544
                               Botan::TLS::Extension_Code::SupportedGroups,
1545
                               Botan::TLS::Extension_Code::KeyShare,
1546
                               Botan::TLS::Extension_Code::SupportedVersions,
1547
                               Botan::TLS::Extension_Code::SignatureAlgorithms,
1548
                               Botan::TLS::Extension_Code::PskKeyExchangeModes,
1549
                               Botan::TLS::Extension_Code::RecordSizeLimit,
1550
                               Botan::TLS::Extension_Code::PresharedKey,
1551
                            });
1552
         };
1✔
1553

1554
         std::unique_ptr<Client_Context> ctx;
1✔
1555

1556
         return {
1✔
1557
            CHECK("Client Hello",
1558
                  [&](Test::Result& result) {
1✔
1559
                     ctx = std::make_unique<Client_Context>(
1✔
1560
                        std::move(rng),
1561
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_psk_dhe", false /* no rfc8448 */),
2✔
1562
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1563
                        sort_our_extensions,
1564
                        std::nullopt,
1565
                        ExternalPSK(vars.get_req_str("PskIdentity"),
2✔
1566
                                    vars.get_req_str("PskPRF"),
2✔
1567
                                    lock(vars.get_req_bin("PskSecret"))));
5✔
1568

1569
                     result.test_is_true("client not closed", !ctx->client.is_closed());
1✔
1570
                     ctx->check_callback_invocations(result,
2✔
1571
                                                     "client hello prepared",
1572
                                                     {
1573
                                                        "tls_emit_data",
1574
                                                        "tls_inspect_handshake_msg_client_hello",
1575
                                                        "tls_modify_extensions_client_hello",
1576
                                                        "tls_current_timestamp",
1577
                                                        "tls_generate_ephemeral_key",
1578
                                                     });
1579

1580
                     result.test_bin_eq(
1✔
1581
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1582
                  }),
1✔
1583

1584
            CHECK("Server Hello",
1585
                  [&](Test::Result& result) {
1✔
1586
                     result.require("ctx is available", ctx != nullptr);
1✔
1587
                     const auto server_hello = vars.get_req_bin("Record_ServerHello");
1✔
1588
                     ctx->client.received_data(server_hello);
1✔
1589
                     ctx->check_callback_invocations(result,
2✔
1590
                                                     "server hello received",
1591
                                                     {"tls_inspect_handshake_msg_server_hello",
1592
                                                      "tls_examine_extensions_server_hello",
1593
                                                      "tls_ephemeral_key_agreement"});
1594

1595
                     result.test_is_true("client is not yet active", !ctx->client.is_active());
1✔
1596
                     result.test_is_true("handshake is not yet complete", !ctx->client.is_handshake_complete());
1✔
1597
                  }),
1✔
1598

1599
            CHECK(
1600
               "Server HS messages .. Client Finished",
1601
               [&](Test::Result& result) {
1✔
1602
                  result.require("ctx is available", ctx != nullptr);
1✔
1603
                  ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1604

1605
                  ctx->check_callback_invocations(result,
2✔
1606
                                                  "encrypted handshake messages received",
1607
                                                  {"tls_inspect_handshake_msg_encrypted_extensions",
1608
                                                   "tls_inspect_handshake_msg_finished",
1609
                                                   "tls_examine_extensions_encrypted_extensions",
1610
                                                   "tls_emit_data",
1611
                                                   "tls_current_timestamp",
1612
                                                   "tls_session_established",
1613
                                                   "tls_session_activated"});
1614
                  result.require("PSK negotiated", ctx->psk_identity_negotiated() == vars.get_req_str("PskIdentity"));
1✔
1615
                  result.require("client is active", ctx->client.is_active());
1✔
1616
                  result.test_is_true("handshake is complete", ctx->client.is_handshake_complete());
1✔
1617

1618
                  result.test_bin_eq(
1✔
1619
                     "correct handshake finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1620
               }),
1✔
1621

1622
            CHECK("Send Application Data",
1623
                  [&](Test::Result& result) {
1✔
1624
                     result.require("ctx is available", ctx != nullptr);
1✔
1625
                     ctx->send(vars.get_req_bin("Client_AppData"));
3✔
1626

1627
                     ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
2✔
1628

1629
                     result.test_bin_eq("correct client application data",
1✔
1630
                                        ctx->pull_send_buffer(),
2✔
1631
                                        vars.get_req_bin("Record_Client_AppData"));
2✔
1632
                  }),
1✔
1633

1634
            CHECK("Receive Application Data",
1635
                  [&](Test::Result& result) {
1✔
1636
                     result.require("ctx is available", ctx != nullptr);
1✔
1637
                     ctx->client.received_data(vars.get_req_bin("Record_Server_AppData"));
2✔
1638

1639
                     ctx->check_callback_invocations(result, "application data sent", {"tls_record_received"});
2✔
1640

1641
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1642
                     result.test_bin_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
2✔
1643
                     result.test_u64_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
1✔
1644
                  }),
1✔
1645

1646
            CHECK("Close Connection",
1647
                  [&](Test::Result& result) {
1✔
1648
                     result.require("ctx is available", ctx != nullptr);
1✔
1649
                     ctx->client.close();
1✔
1650

1651
                     result.test_bin_eq(
1✔
1652
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1653
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1654

1655
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1656
                     ctx->check_callback_invocations(
2✔
1657
                        result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1658

1659
                     result.test_is_true("connection is closed", ctx->client.is_closed());
1✔
1660
                  }),
1✔
1661
         };
7✔
1662
      }
2✔
1663

1664
      std::vector<Test::Result> raw_public_key_with_client_authentication(const VarMap& vars) override {
1✔
1665
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1666

1667
         // 32 - for client hello random
1668
         // 32 - for KeyShare (eph. x25519 key pair)
1669
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1670

1671
         auto sort_our_extensions = [&](Botan::TLS::Extensions& exts,
3✔
1672
                                        Botan::TLS::Connection_Side /* side */,
1673
                                        Botan::TLS::Handshake_Type /* which_message */) {
1674
            // This is the order of extensions when we first introduced the raw
1675
            // public key authentication implementation and generated the transcript.
1676
            // To stay compatible with the now hard-coded transcript, we pin the
1677
            // extension order.
1678
            sort_extensions(exts,
2✔
1679
                            {
1680
                               Botan::TLS::Extension_Code::ServerNameIndication,
1681
                               Botan::TLS::Extension_Code::SupportedGroups,
1682
                               Botan::TLS::Extension_Code::KeyShare,
1683
                               Botan::TLS::Extension_Code::SupportedVersions,
1684
                               Botan::TLS::Extension_Code::SignatureAlgorithms,
1685
                               Botan::TLS::Extension_Code::PskKeyExchangeModes,
1686
                               Botan::TLS::Extension_Code::RecordSizeLimit,
1687
                               Botan::TLS::Extension_Code::ClientCertificateType,
1688
                               Botan::TLS::Extension_Code::ServerCertificateType,
1689
                            });
1690
         };
2✔
1691

1692
         std::unique_ptr<Client_Context> ctx;
1✔
1693

1694
         return {
1✔
1695
            CHECK("Client Hello",
1696
                  [&](Test::Result& result) {
1✔
1697
                     ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1698
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_rawpubkey"),
2✔
1699
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1700
                                                            sort_our_extensions,
1701
                                                            std::nullopt,
1702
                                                            std::nullopt,
1703
                                                            make_mock_signatures(vars));
3✔
1704

1705
                     ctx->check_callback_invocations(result,
2✔
1706
                                                     "initial callbacks",
1707
                                                     {
1708
                                                        "tls_emit_data",
1709
                                                        "tls_inspect_handshake_msg_client_hello",
1710
                                                        "tls_modify_extensions_client_hello",
1711
                                                        "tls_generate_ephemeral_key",
1712
                                                        "tls_current_timestamp",
1713
                                                     });
1714

1715
                     result.test_bin_eq(
1✔
1716
                        "Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1717
                  }),
1✔
1718

1719
            CHECK("Server Hello",
1720
                  [&](auto& result) {
1✔
1721
                     result.require("ctx is available", ctx != nullptr);
1✔
1722
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1723

1724
                     ctx->check_callback_invocations(result,
2✔
1725
                                                     "callbacks after server hello",
1726
                                                     {
1727
                                                        "tls_examine_extensions_server_hello",
1728
                                                        "tls_inspect_handshake_msg_server_hello",
1729
                                                        "tls_ephemeral_key_agreement",
1730
                                                     });
1731
                  }),
1✔
1732

1733
            CHECK("other handshake messages and client auth",
1734
                  [&](Test::Result& result) {
1✔
1735
                     result.require("ctx is available", ctx != nullptr);
1✔
1736
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1737

1738
                     ctx->check_callback_invocations(result,
2✔
1739
                                                     "signing callbacks invoked",
1740
                                                     {
1741
                                                        "tls_sign_message",
1742
                                                        "tls_emit_data",
1743
                                                        "tls_examine_extensions_encrypted_extensions",
1744
                                                        "tls_examine_extensions_certificate",
1745
                                                        "tls_examine_extensions_certificate_request",
1746
                                                        "tls_modify_extensions_certificate",
1747
                                                        "tls_inspect_handshake_msg_certificate",
1748
                                                        "tls_inspect_handshake_msg_certificate_request",
1749
                                                        "tls_inspect_handshake_msg_certificate_verify",
1750
                                                        "tls_inspect_handshake_msg_encrypted_extensions",
1751
                                                        "tls_inspect_handshake_msg_finished",
1752
                                                        "tls_current_timestamp",
1753
                                                        "tls_session_established",
1754
                                                        "tls_session_activated",
1755
                                                        "tls_verify_raw_public_key",
1756
                                                        "tls_verify_message",
1757
                                                     });
1758

1759
                     const auto raw_pk = ctx->client.peer_raw_public_key();
1✔
1760
                     result.test_is_true(
1✔
1761
                        "Received server's raw public key",
1762
                        raw_pk && raw_pk->fingerprint_public() == server_raw_public_key_pair()->fingerprint_public());
5✔
1763

1764
                     // ClientFinished contains the entire coalesced client authentication flight
1765
                     // Messages: Certificate, CertificateVerify, Finished
1766
                     result.test_bin_eq(
1✔
1767
                        "Client Auth and Finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1768
                  }),
1✔
1769

1770
            CHECK("Close Connection",
1771
                  [&](Test::Result& result) {
1✔
1772
                     result.require("ctx is available", ctx != nullptr);
1✔
1773
                     ctx->client.close();
1✔
1774
                     result.test_bin_eq(
1✔
1775
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1776

1777
                     ctx->check_callback_invocations(result,
2✔
1778
                                                     "after sending close notify",
1779
                                                     {
1780
                                                        "tls_emit_data",
1781
                                                     });
1782

1783
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1784
                     result.test_is_true("connection closed", ctx->client.is_closed());
1✔
1785

1786
                     ctx->check_callback_invocations(
2✔
1787
                        result, "after receiving close notify", {"tls_alert", "tls_peer_closed_connection"});
1788
                  }),
1✔
1789
         };
5✔
1790
      }
2✔
1791
};
1792

1793
class Test_TLS_RFC8448_Server : public Test_TLS_RFC8448 {
1✔
1794
   private:
1795
      std::string side() const override { return "Server"; }
7✔
1796

1797
      std::vector<Test::Result> simple_1_rtt(const VarMap& vars) override {
1✔
1798
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
1799

1800
         // 32 - for server hello random
1801
         // 32 - for KeyShare (eph. x25519 key pair)  --  I guess?
1802
         //  4 - for ticket_age_add (in New Session Ticket)
1803
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1804

1805
         std::unique_ptr<Server_Context> ctx;
1✔
1806

1807
         return {
1✔
1808
            CHECK("Send Client Hello",
1809
                  [&](Test::Result& result) {
1✔
1810
                     auto add_early_data_and_sort = [&](Botan::TLS::Extensions& exts,
5✔
1811
                                                        Botan::TLS::Connection_Side side,
1812
                                                        Botan::TLS::Handshake_Type type) {
1813
                        if(type == Handshake_Type::NewSessionTicket) {
4✔
1814
                           exts.add(new EarlyDataIndication(1024));  // NOLINT(*-owning-memory)
1✔
1815
                        }
1816
                        sort_rfc8448_extensions(exts, side, type);
4✔
1817
                     };
4✔
1818

1819
                     ctx = std::make_unique<Server_Context>(
1✔
1820
                        std::move(rng),
1821
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1822
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1823
                        add_early_data_and_sort,
1824
                        make_mock_signatures(vars),
1✔
1825
                        false,
2✔
1826
                        std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1827
                                  Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1828
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
1829

1830
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1831

1832
                     ctx->check_callback_invocations(result,
2✔
1833
                                                     "client hello received",
1834
                                                     {"tls_emit_data",
1835
                                                      "tls_examine_extensions_client_hello",
1836
                                                      "tls_modify_extensions_server_hello",
1837
                                                      "tls_modify_extensions_encrypted_extensions",
1838
                                                      "tls_modify_extensions_certificate",
1839
                                                      "tls_sign_message",
1840
                                                      "tls_generate_ephemeral_key",
1841
                                                      "tls_ephemeral_key_agreement",
1842
                                                      "tls_inspect_handshake_msg_client_hello",
1843
                                                      "tls_inspect_handshake_msg_server_hello",
1844
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
1845
                                                      "tls_inspect_handshake_msg_certificate",
1846
                                                      "tls_inspect_handshake_msg_certificate_verify",
1847
                                                      "tls_inspect_handshake_msg_finished"});
1848
                  }),
1✔
1849

1850
            CHECK("Verify generated messages in server's first flight",
1851
                  [&](Test::Result& result) {
1✔
1852
                     result.require("ctx is available", ctx != nullptr);
1✔
1853
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
1854

1855
                     result.test_bin_eq("Server Hello",
2✔
1856
                                        msgs.at("server_hello")[0],
2✔
1857
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
1858
                     result.test_bin_eq("Encrypted Extensions",
2✔
1859
                                        msgs.at("encrypted_extensions")[0],
2✔
1860
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1861
                     result.test_bin_eq("Certificate",
2✔
1862
                                        msgs.at("certificate")[0],
2✔
1863
                                        strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
1864
                     result.test_bin_eq("CertificateVerify",
2✔
1865
                                        msgs.at("certificate_verify")[0],
2✔
1866
                                        strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
1867

1868
                     result.test_bin_eq("Server's entire first flight",
1✔
1869
                                        ctx->pull_send_buffer(),
2✔
1870
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
1871
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1872

1873
                     // Note: is_active() defines that we can send application data.
1874
                     //       RFC 8446 Section 4.4.4 explicitly allows that for servers
1875
                     //       that did not receive the client's Finished message, yet.
1876
                     //       However, before receiving and validating this message,
1877
                     //       the handshake is not yet finished.
1878
                     result.test_is_true("Server can now send application data", ctx->server.is_active());
1✔
1879
                     result.test_is_true("handshake is not yet complete", !ctx->server.is_handshake_complete());
1✔
1880
                  }),
1✔
1881

1882
            CHECK("Send Client Finished",
1883
                  [&](Test::Result& result) {
1✔
1884
                     result.require("ctx is available", ctx != nullptr);
1✔
1885
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
1886

1887
                     ctx->check_callback_invocations(result,
2✔
1888
                                                     "client finished received",
1889
                                                     {"tls_inspect_handshake_msg_finished",
1890
                                                      "tls_current_timestamp",
1891
                                                      "tls_session_established",
1892
                                                      "tls_session_activated"});
1893
                  }),
1✔
1894

1895
            CHECK("Send Session Ticket",
1896
                  [&](Test::Result& result) {
1✔
1897
                     result.require("ctx is available", ctx != nullptr);
1✔
1898
                     const auto new_tickets = ctx->server.send_new_session_tickets(1);
1✔
1899

1900
                     result.test_sz_eq("session ticket was sent", new_tickets, 1);
1✔
1901

1902
                     ctx->check_callback_invocations(result,
2✔
1903
                                                     "issued new session ticket",
1904
                                                     {"tls_inspect_handshake_msg_new_session_ticket",
1905
                                                      "tls_current_timestamp",
1906
                                                      "tls_emit_data",
1907
                                                      "tls_modify_extensions_new_session_ticket",
1908
                                                      "tls_should_persist_resumption_information"});
1909
                  }),
1✔
1910

1911
            CHECK("Verify generated new session ticket message",
1912
                  [&](Test::Result& result) {
1✔
1913
                     result.require("ctx is available", ctx != nullptr);
1✔
1914
                     result.test_bin_eq(
1✔
1915
                        "New Session Ticket", ctx->pull_send_buffer(), vars.get_req_bin("Record_NewSessionTicket"));
3✔
1916
                  }),
1✔
1917

1918
            CHECK("Receive Application Data",
1919
                  [&](Test::Result& result) {
1✔
1920
                     result.require("ctx is available", ctx != nullptr);
1✔
1921
                     ctx->server.received_data(vars.get_req_bin("Record_Client_AppData"));
2✔
1922
                     ctx->check_callback_invocations(result, "application data received", {"tls_record_received"});
2✔
1923

1924
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1925
                     result.test_bin_eq("decrypted application traffic", rcvd, vars.get_req_bin("Client_AppData"));
2✔
1926
                     result.test_u64_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
1✔
1927
                  }),
1✔
1928

1929
            CHECK("Send Application Data",
1930
                  [&](Test::Result& result) {
1✔
1931
                     result.require("ctx is available", ctx != nullptr);
1✔
1932
                     ctx->send(vars.get_req_bin("Server_AppData"));
3✔
1933

1934
                     ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
2✔
1935

1936
                     result.test_bin_eq("correct server application data",
1✔
1937
                                        ctx->pull_send_buffer(),
2✔
1938
                                        vars.get_req_bin("Record_Server_AppData"));
2✔
1939
                  }),
1✔
1940

1941
            CHECK("Receive Client's close_notify",
1942
                  [&](Test::Result& result) {
1✔
1943
                     result.require("ctx is available", ctx != nullptr);
1✔
1944
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
1945

1946
                     ctx->check_callback_invocations(
2✔
1947
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1948

1949
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
1950
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
1951
                     result.test_is_true("handshake is still finished", ctx->server.is_handshake_complete());
1✔
1952
                  }),
1✔
1953

1954
            CHECK("Expect Server close_notify",
1955
                  [&](Test::Result& result) {
1✔
1956
                     result.require("ctx is available", ctx != nullptr);
1✔
1957
                     ctx->server.close();
1✔
1958

1959
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
1960
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
1961
                     result.test_is_true("handshake is still finished", ctx->server.is_handshake_complete());
1✔
1962
                     result.test_bin_eq("Server's close notify",
1✔
1963
                                        ctx->pull_send_buffer(),
2✔
1964
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1965
                  }),
1✔
1966
         };
10✔
1967
      }
2✔
1968

1969
      std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) override {
1✔
1970
         auto rng = std::make_unique<Fixed_Output_RNG>();
1✔
1971

1972
         // 32 - for server hello random
1973
         // 32 - for KeyShare (eph. x25519 key pair)
1974
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1975

1976
         std::unique_ptr<Server_Context> ctx;
1✔
1977

1978
         return {
1✔
1979
            CHECK("Receive Client Hello",
1980
                  [&](Test::Result& result) {
1✔
1981
                     auto add_cookie_and_sort = [&](Botan::TLS::Extensions& exts,
3✔
1982
                                                    Botan::TLS::Connection_Side side,
1983
                                                    Botan::TLS::Handshake_Type type) {
1984
                        if(type == Handshake_Type::EncryptedExtensions) {
2✔
1985
                           exts.add(new EarlyDataIndication());  // NOLINT(*-owning-memory)
1✔
1986
                        }
1987
                        sort_rfc8448_extensions(exts, side, type);
2✔
1988
                     };
2✔
1989

1990
                     ctx = std::make_unique<Server_Context>(
1✔
1991
                        std::move(rng),
1992
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1993
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1994
                        add_cookie_and_sort,
1995
                        make_mock_signatures(vars),
1✔
1996
                        false,
2✔
1997
                        std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1998
                                  Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1999
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2000

2001
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2002

2003
                     ctx->check_callback_invocations(result,
2✔
2004
                                                     "client hello received",
2005
                                                     {
2006
                                                        "tls_emit_data",
2007
                                                        "tls_current_timestamp",
2008
                                                        "tls_generate_ephemeral_key",
2009
                                                        "tls_ephemeral_key_agreement",
2010
                                                        "tls_examine_extensions_client_hello",
2011
                                                        "tls_modify_extensions_server_hello",
2012
                                                        "tls_modify_extensions_encrypted_extensions",
2013
                                                        "tls_inspect_handshake_msg_client_hello",
2014
                                                        "tls_inspect_handshake_msg_server_hello",
2015
                                                        "tls_inspect_handshake_msg_encrypted_extensions",
2016
                                                        "tls_inspect_handshake_msg_finished",
2017
                                                     });
2018
                  }),
1✔
2019

2020
            CHECK("Verify generated messages in server's first flight",
2021
                  [&](Test::Result& result) {
1✔
2022
                     result.require("ctx is available", ctx != nullptr);
1✔
2023
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2024

2025
                     result.test_bin_eq("Server Hello",
2✔
2026
                                        msgs.at("server_hello")[0],
2✔
2027
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2028
                     result.test_bin_eq("Encrypted Extensions",
2✔
2029
                                        msgs.at("encrypted_extensions")[0],
2✔
2030
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2031

2032
                     result.test_bin_eq("Server's entire first flight",
1✔
2033
                                        ctx->pull_send_buffer(),
2✔
2034
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2035
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2036

2037
                     // Note: is_active() defines that we can send application data.
2038
                     //       RFC 8446 Section 4.4.4 explicitly allows that for servers
2039
                     //       that did not receive the client's Finished message, yet.
2040
                     //       However, before receiving and validating this message,
2041
                     //       the handshake is not yet finished.
2042
                     result.test_is_true("Server can now send application data", ctx->server.is_active());
1✔
2043
                     result.test_is_true("handshake is not yet complete", !ctx->server.is_handshake_complete());
1✔
2044
                  }),
1✔
2045

2046
            // TODO: The rest of this test vector requires 0-RTT which is not
2047
            //       yet implemented. For now we can only test the server's
2048
            //       ability to acknowledge a session resumption via PSK.
2049
         };
3✔
2050
      }
2✔
2051

2052
      std::vector<Test::Result> hello_retry_request(const VarMap& vars) override {
1✔
2053
         // Fallback RNG is required to for blinding in ECDH with P-256
2054
         auto& fallback_rng = this->rng();
1✔
2055
         auto rng = std::make_unique<Fixed_Output_RNG>(fallback_rng);
1✔
2056

2057
         // 32 - for server hello random
2058
         // 32 - for KeyShare (eph. P-256 key pair)
2059
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2060

2061
         std::unique_ptr<Server_Context> ctx;
1✔
2062

2063
         return {
1✔
2064
            CHECK("Receive Client Hello",
2065
                  [&](Test::Result& result) {
1✔
2066
                     auto add_cookie_and_sort = [&](Botan::TLS::Extensions& exts,
5✔
2067
                                                    Botan::TLS::Connection_Side side,
2068
                                                    Botan::TLS::Handshake_Type type) {
2069
                        if(type == Handshake_Type::HelloRetryRequest) {
4✔
2070
                           // This cookie needs to be mocked into the HRR since RFC 8448 contains it.
2071
                           exts.add(
1✔
2072
                              new Cookie(vars.get_opt_bin("HelloRetryRequest_Cookie")));  // NOLINT(*-owning-memory)
2✔
2073
                        }
2074
                        sort_rfc8448_extensions(exts, side, type);
4✔
2075
                     };
4✔
2076

2077
                     ctx = std::make_unique<Server_Context>(std::move(rng),
1✔
2078
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_server"),
2✔
2079
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
2080
                                                            add_cookie_and_sort,
2081
                                                            make_mock_signatures(vars));
3✔
2082
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2083

2084
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2085

2086
                     ctx->check_callback_invocations(result,
2✔
2087
                                                     "client hello received",
2088
                                                     {"tls_emit_data",
2089
                                                      "tls_examine_extensions_client_hello",
2090
                                                      "tls_modify_extensions_hello_retry_request",
2091
                                                      "tls_inspect_handshake_msg_client_hello",
2092
                                                      "tls_inspect_handshake_msg_hello_retry_request"});
2093
                  }),
1✔
2094

2095
            CHECK("Verify generated Hello Retry Request message",
2096
                  [&](Test::Result& result) {
1✔
2097
                     result.require("ctx is available", ctx != nullptr);
1✔
2098
                     result.test_bin_eq("Server's Hello Retry Request record",
1✔
2099
                                        ctx->pull_send_buffer(),
2✔
2100
                                        vars.get_req_bin("Record_HelloRetryRequest"));
2✔
2101
                     result.test_is_true("TLS handshake not yet finished", !ctx->server.is_active());
1✔
2102
                     result.test_is_true("handshake is not yet complete", !ctx->server.is_handshake_complete());
1✔
2103
                  }),
1✔
2104

2105
            CHECK("Receive updated Client Hello message",
2106
                  [&](Test::Result& result) {
1✔
2107
                     result.require("ctx is available", ctx != nullptr);
1✔
2108
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_2"));
2✔
2109

2110
                     ctx->check_callback_invocations(result,
2✔
2111
                                                     "updated client hello received",
2112
                                                     {"tls_emit_data",
2113
                                                      "tls_examine_extensions_client_hello",
2114
                                                      "tls_modify_extensions_server_hello",
2115
                                                      "tls_modify_extensions_encrypted_extensions",
2116
                                                      "tls_modify_extensions_certificate",
2117
                                                      "tls_sign_message",
2118
                                                      "tls_generate_ephemeral_key",
2119
                                                      "tls_ephemeral_key_agreement",
2120
                                                      "tls_inspect_handshake_msg_client_hello",
2121
                                                      "tls_inspect_handshake_msg_server_hello",
2122
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2123
                                                      "tls_inspect_handshake_msg_certificate",
2124
                                                      "tls_inspect_handshake_msg_certificate_verify",
2125
                                                      "tls_inspect_handshake_msg_finished"});
2126
                  }),
1✔
2127

2128
            CHECK("Verify generated messages in server's second flight",
2129
                  [&](Test::Result& result) {
1✔
2130
                     result.require("ctx is available", ctx != nullptr);
1✔
2131
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2132

2133
                     result.test_bin_eq("Server Hello",
2✔
2134
                                        msgs.at("server_hello")[0],
2✔
2135
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2136
                     result.test_bin_eq("Encrypted Extensions",
2✔
2137
                                        msgs.at("encrypted_extensions")[0],
2✔
2138
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2139
                     result.test_bin_eq("Certificate",
2✔
2140
                                        msgs.at("certificate")[0],
2✔
2141
                                        strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2142
                     result.test_bin_eq("CertificateVerify",
2✔
2143
                                        msgs.at("certificate_verify")[0],
2✔
2144
                                        strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2145
                     result.test_bin_eq("Finished",
2✔
2146
                                        msgs.at("finished")[0],
2✔
2147
                                        strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2148

2149
                     result.test_bin_eq("Server's entire second flight",
1✔
2150
                                        ctx->pull_send_buffer(),
2✔
2151
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2152
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2153
                     result.test_is_true("Server could now send application data", ctx->server.is_active());
1✔
2154
                     result.test_is_true("handshake is not yet complete",
1✔
2155
                                         !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2156
                  }),
1✔
2157

2158
            CHECK("Receive Client Finished",
2159
                  [&](Test::Result& result) {
1✔
2160
                     result.require("ctx is available", ctx != nullptr);
1✔
2161
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2162

2163
                     ctx->check_callback_invocations(result,
2✔
2164
                                                     "client finished received",
2165
                                                     {"tls_inspect_handshake_msg_finished",
2166
                                                      "tls_current_timestamp",
2167
                                                      "tls_session_established",
2168
                                                      "tls_session_activated"});
2169

2170
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2171
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2172
                  }),
1✔
2173

2174
            CHECK("Receive Client close_notify",
2175
                  [&](Test::Result& result) {
1✔
2176
                     result.require("ctx is available", ctx != nullptr);
1✔
2177
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2178

2179
                     ctx->check_callback_invocations(
2✔
2180
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2181

2182
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
2183
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
2184
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2185
                  }),
1✔
2186

2187
            CHECK("Expect Server close_notify",
2188
                  [&](Test::Result& result) {
1✔
2189
                     result.require("ctx is available", ctx != nullptr);
1✔
2190
                     ctx->server.close();
1✔
2191

2192
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
2193
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
2194
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2195
                     result.test_bin_eq("Server's close notify",
1✔
2196
                                        ctx->pull_send_buffer(),
2✔
2197
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2198
                  }),
1✔
2199

2200
         };
8✔
2201
      }
2✔
2202

2203
      std::vector<Test::Result> client_authentication(const VarMap& vars) override {
1✔
2204
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
2205

2206
         // 32 - for server hello random
2207
         // 32 - for KeyShare (eph. x25519 pair)
2208
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2209

2210
         std::unique_ptr<Server_Context> ctx;
1✔
2211

2212
         return {
1✔
2213
            CHECK("Receive Client Hello",
2214
                  [&](Test::Result& result) {
1✔
2215
                     ctx = std::make_unique<Server_Context>(
1✔
2216
                        std::move(rng),
2217
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_client_auth_server"),
2✔
2218
                        vars.get_req_u64("CurrentTimestamp"),
1✔
2219
                        sort_rfc8448_extensions,
2220
                        make_mock_signatures(vars),
1✔
2221
                        true /* use alternative certificate */);
2✔
2222
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2223

2224
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2225

2226
                     ctx->check_callback_invocations(result,
2✔
2227
                                                     "client hello received",
2228
                                                     {"tls_emit_data",
2229
                                                      "tls_examine_extensions_client_hello",
2230
                                                      "tls_modify_extensions_server_hello",
2231
                                                      "tls_modify_extensions_encrypted_extensions",
2232
                                                      "tls_modify_extensions_certificate_request",
2233
                                                      "tls_modify_extensions_certificate",
2234
                                                      "tls_sign_message",
2235
                                                      "tls_generate_ephemeral_key",
2236
                                                      "tls_ephemeral_key_agreement",
2237
                                                      "tls_inspect_handshake_msg_client_hello",
2238
                                                      "tls_inspect_handshake_msg_server_hello",
2239
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2240
                                                      "tls_inspect_handshake_msg_certificate_request",
2241
                                                      "tls_inspect_handshake_msg_certificate",
2242
                                                      "tls_inspect_handshake_msg_certificate_verify",
2243
                                                      "tls_inspect_handshake_msg_finished"});
2244
                  }),
1✔
2245

2246
            CHECK("Verify server's generated handshake messages",
2247
                  [&](Test::Result& result) {
1✔
2248
                     result.require("ctx is available", ctx != nullptr);
1✔
2249
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2250

2251
                     result.test_bin_eq("Server Hello",
2✔
2252
                                        msgs.at("server_hello")[0],
2✔
2253
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2254
                     result.test_bin_eq("Encrypted Extensions",
2✔
2255
                                        msgs.at("encrypted_extensions")[0],
2✔
2256
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2257
                     result.test_bin_eq("Certificate Request",
2✔
2258
                                        msgs.at("certificate_request")[0],
2✔
2259
                                        strip_message_header(vars.get_opt_bin("Message_CertificateRequest")));
3✔
2260
                     result.test_bin_eq("Certificate",
2✔
2261
                                        msgs.at("certificate")[0],
2✔
2262
                                        strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2263
                     result.test_bin_eq("CertificateVerify",
2✔
2264
                                        msgs.at("certificate_verify")[0],
2✔
2265
                                        strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2266
                     result.test_bin_eq("Finished",
2✔
2267
                                        msgs.at("finished")[0],
2✔
2268
                                        strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2269

2270
                     result.test_bin_eq("Server's entire first flight",
1✔
2271
                                        ctx->pull_send_buffer(),
2✔
2272
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2273
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2274

2275
                     result.test_is_true("Not yet aware of client's cert chain", ctx->server.peer_cert_chain().empty());
1✔
2276
                     result.test_is_true("Server could now send application data", ctx->server.is_active());
1✔
2277
                     result.test_is_true("handshake is not yet complete",
1✔
2278
                                         !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2279
                  }),
1✔
2280

2281
            CHECK("Receive Client's second flight",
2282
                  [&](Test::Result& result) {
1✔
2283
                     result.require("ctx is available", ctx != nullptr);
1✔
2284
                     // This encrypted message contains the following messages:
2285
                     // * client's Certificate message
2286
                     // * client's Certificate_Verify message
2287
                     // * client's Finished message
2288
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2289

2290
                     ctx->check_callback_invocations(result,
2✔
2291
                                                     "client finished received",
2292
                                                     {"tls_inspect_handshake_msg_certificate",
2293
                                                      "tls_inspect_handshake_msg_certificate_verify",
2294
                                                      "tls_inspect_handshake_msg_finished",
2295
                                                      "tls_examine_extensions_certificate",
2296
                                                      "tls_verify_cert_chain",
2297
                                                      "tls_verify_message",
2298
                                                      "tls_current_timestamp",
2299
                                                      "tls_session_established",
2300
                                                      "tls_session_activated"});
2301

2302
                     const auto cert_chain = ctx->server.peer_cert_chain();
1✔
2303
                     result.test_is_true("Received client's cert chain",
1✔
2304
                                         !cert_chain.empty() && cert_chain.front() == client_certificate());
2✔
2305

2306
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2307
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2308
                  }),
1✔
2309

2310
            CHECK("Receive Client close_notify",
2311
                  [&](Test::Result& result) {
1✔
2312
                     result.require("ctx is available", ctx != nullptr);
1✔
2313
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2314

2315
                     ctx->check_callback_invocations(
2✔
2316
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2317

2318
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
2319
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
2320
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2321
                  }),
1✔
2322

2323
            CHECK("Expect Server close_notify",
2324
                  [&](Test::Result& result) {
1✔
2325
                     result.require("ctx is available", ctx != nullptr);
1✔
2326
                     ctx->server.close();
1✔
2327

2328
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
2329
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
2330
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2331
                     result.test_bin_eq("Server's close notify",
1✔
2332
                                        ctx->pull_send_buffer(),
2✔
2333
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2334
                  }),
1✔
2335

2336
         };
6✔
2337
      }
2✔
2338

2339
      std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) override {
1✔
2340
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
2341

2342
         // 32 - for server hello random
2343
         // 32 - for KeyShare (eph. x25519 pair)
2344
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2345

2346
         std::unique_ptr<Server_Context> ctx;
1✔
2347

2348
         return {
1✔
2349
            CHECK("Receive Client Hello",
2350
                  [&](Test::Result& result) {
1✔
2351
                     ctx =
1✔
2352
                        std::make_unique<Server_Context>(std::move(rng),
1✔
2353
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_server"),
2✔
2354
                                                         vars.get_req_u64("CurrentTimestamp"),
1✔
2355
                                                         sort_rfc8448_extensions,
2356
                                                         make_mock_signatures(vars));
3✔
2357
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2358

2359
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2360

2361
                     ctx->check_callback_invocations(result,
2✔
2362
                                                     "client hello received",
2363
                                                     {"tls_emit_data",
2364
                                                      "tls_examine_extensions_client_hello",
2365
                                                      "tls_modify_extensions_server_hello",
2366
                                                      "tls_modify_extensions_encrypted_extensions",
2367
                                                      "tls_modify_extensions_certificate",
2368
                                                      "tls_sign_message",
2369
                                                      "tls_generate_ephemeral_key",
2370
                                                      "tls_ephemeral_key_agreement",
2371
                                                      "tls_inspect_handshake_msg_client_hello",
2372
                                                      "tls_inspect_handshake_msg_server_hello",
2373
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2374
                                                      "tls_inspect_handshake_msg_certificate",
2375
                                                      "tls_inspect_handshake_msg_certificate_verify",
2376
                                                      "tls_inspect_handshake_msg_finished"});
2377
                  }),
1✔
2378

2379
            CHECK("Verify server's generated handshake messages",
2380
                  [&](Test::Result& result) {
1✔
2381
                     result.require("ctx is available", ctx != nullptr);
1✔
2382
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2383

2384
                     result.test_bin_eq("Server Hello",
2✔
2385
                                        msgs.at("server_hello")[0],
2✔
2386
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2387
                     result.test_bin_eq("Encrypted Extensions",
2✔
2388
                                        msgs.at("encrypted_extensions")[0],
2✔
2389
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2390
                     result.test_bin_eq("Certificate",
2✔
2391
                                        msgs.at("certificate")[0],
2✔
2392
                                        strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2393
                     result.test_bin_eq("CertificateVerify",
2✔
2394
                                        msgs.at("certificate_verify")[0],
2✔
2395
                                        strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2396
                     result.test_bin_eq("Finished",
2✔
2397
                                        msgs.at("finished")[0],
2✔
2398
                                        strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2399

2400
                     // Those records contain the required Change Cipher Spec message the server must produce for compatibility mode compliance
2401
                     result.test_bin_eq("Server's entire first flight",
1✔
2402
                                        ctx->pull_send_buffer(),
2✔
2403
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2404
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2405

2406
                     result.test_is_true("Server could now send application data", ctx->server.is_active());
1✔
2407
                     result.test_is_true("handshake is not yet complete",
1✔
2408
                                         !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2409
                  }),
1✔
2410

2411
            CHECK("Receive Client Finished",
2412
                  [&](Test::Result& result) {
1✔
2413
                     result.require("ctx is available", ctx != nullptr);
1✔
2414
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2415

2416
                     ctx->check_callback_invocations(result,
2✔
2417
                                                     "client finished received",
2418
                                                     {"tls_inspect_handshake_msg_finished",
2419
                                                      "tls_current_timestamp",
2420
                                                      "tls_session_established",
2421
                                                      "tls_session_activated"});
2422

2423
                     result.test_is_true("TLS handshake fully finished", ctx->server.is_active());
1✔
2424
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2425
                  }),
1✔
2426

2427
            CHECK("Receive Client close_notify",
2428
                  [&](Test::Result& result) {
1✔
2429
                     result.require("ctx is available", ctx != nullptr);
1✔
2430
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2431

2432
                     ctx->check_callback_invocations(
2✔
2433
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2434

2435
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
2436
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
2437
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2438
                  }),
1✔
2439

2440
            CHECK("Expect Server close_notify",
2441
                  [&](Test::Result& result) {
1✔
2442
                     result.require("ctx is available", ctx != nullptr);
1✔
2443
                     ctx->server.close();
1✔
2444

2445
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
2446
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
2447
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2448
                     result.test_bin_eq("Server's close notify",
1✔
2449
                                        ctx->pull_send_buffer(),
2✔
2450
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2451
                  }),
1✔
2452

2453
         };
6✔
2454
      }
2✔
2455

2456
      std::vector<Test::Result> externally_provided_psk_with_ephemeral_key(const VarMap& vars) override {
1✔
2457
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
2458

2459
         // 32 - for server hello random
2460
         // 32 - for KeyShare (eph. x25519 key pair)
2461
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2462

2463
         std::unique_ptr<Server_Context> ctx;
1✔
2464

2465
         return {
1✔
2466
            CHECK("Send Client Hello",
2467
                  [&](Test::Result& result) {
1✔
2468
                     auto sort_our_extensions = [&](Botan::TLS::Extensions& exts,
3✔
2469
                                                    Botan::TLS::Connection_Side /* side */,
2470
                                                    Botan::TLS::Handshake_Type type) {
2471
                        // This is the order of extensions when we first introduced the PSK
2472
                        // implementation and generated the transcript. To stay compatible
2473
                        // with the now hard-coded transcript, we pin the extension order.
2474
                        if(type == Botan::TLS::Handshake_Type::EncryptedExtensions) {
2✔
2475
                           sort_extensions(exts,
2✔
2476
                                           {
2477
                                              Botan::TLS::Extension_Code::SupportedGroups,
2478
                                              Botan::TLS::Extension_Code::RecordSizeLimit,
2479
                                              Botan::TLS::Extension_Code::ServerNameIndication,
2480
                                           });
2481
                        } else if(type == Botan::TLS::Handshake_Type::ServerHello) {
1✔
2482
                           sort_extensions(exts,
2✔
2483
                                           {
2484
                                              Botan::TLS::Extension_Code::SupportedVersions,
2485
                                              Botan::TLS::Extension_Code::KeyShare,
2486
                                              Botan::TLS::Extension_Code::PresharedKey,
2487
                                           });
2488
                        }
2489
                     };
2✔
2490

2491
                     ctx = std::make_unique<Server_Context>(
1✔
2492
                        std::move(rng),
2493
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_psk_dhe", false /* no rfc8448 */),
2✔
2494
                        vars.get_req_u64("CurrentTimestamp"),
1✔
2495
                        sort_our_extensions,
2496
                        make_mock_signatures(vars),
1✔
2497
                        false,
1✔
2498
                        std::nullopt,
2499
                        ExternalPSK(vars.get_req_str("PskIdentity"),
2✔
2500
                                    vars.get_req_str("PskPRF"),
2✔
2501
                                    lock(vars.get_req_bin("PskSecret"))));
5✔
2502
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2503

2504
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2505

2506
                     ctx->check_callback_invocations(result,
2✔
2507
                                                     "client hello received",
2508
                                                     {"tls_emit_data",
2509
                                                      "tls_examine_extensions_client_hello",
2510
                                                      "tls_modify_extensions_server_hello",
2511
                                                      "tls_modify_extensions_encrypted_extensions",
2512
                                                      "tls_generate_ephemeral_key",
2513
                                                      "tls_ephemeral_key_agreement",
2514
                                                      "tls_inspect_handshake_msg_client_hello",
2515
                                                      "tls_inspect_handshake_msg_server_hello",
2516
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2517
                                                      "tls_inspect_handshake_msg_finished"});
2518
                  }),
1✔
2519

2520
            CHECK("Verify generated messages in server's first flight",
2521
                  [&](Test::Result& result) {
1✔
2522
                     result.require("ctx is available", ctx != nullptr);
1✔
2523
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2524

2525
                     result.test_bin_eq("Server Hello",
2✔
2526
                                        msgs.at("server_hello")[0],
2✔
2527
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2528
                     result.test_bin_eq("Encrypted Extensions",
2✔
2529
                                        msgs.at("encrypted_extensions")[0],
2✔
2530
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2531
                     result.test_bin_eq("Server Finished",
2✔
2532
                                        msgs.at("finished")[0],
2✔
2533
                                        strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2534

2535
                     result.test_bin_eq("Server's entire first flight",
1✔
2536
                                        ctx->pull_send_buffer(),
2✔
2537
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2538
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2539

2540
                     result.test_is_true("Server can now send application data", ctx->server.is_active());
1✔
2541
                     result.test_is_true("handshake is not yet complete",
1✔
2542
                                         !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2543
                  }),
1✔
2544

2545
            CHECK("Send Client Finished",
2546
                  [&](Test::Result& result) {
1✔
2547
                     result.require("ctx is available", ctx != nullptr);
1✔
2548
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2549
                     result.require("PSK negotiated",
1✔
2550
                                    ctx->psk_identity_negotiated() == vars.get_req_str("PskIdentity"));
2✔
2551

2552
                     ctx->check_callback_invocations(result,
2✔
2553
                                                     "client finished received",
2554
                                                     {"tls_inspect_handshake_msg_finished",
2555
                                                      "tls_current_timestamp",
2556
                                                      "tls_session_established",
2557
                                                      "tls_session_activated"});
2558
                  }),
1✔
2559

2560
            CHECK("Exchange Application Data",
2561
                  [&](Test::Result& result) {
1✔
2562
                     result.require("ctx is available", ctx != nullptr);
1✔
2563
                     ctx->server.received_data(vars.get_req_bin("Record_Client_AppData"));
2✔
2564
                     ctx->check_callback_invocations(result, "application data received", {"tls_record_received"});
2✔
2565

2566
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
2567
                     result.test_bin_eq("decrypted application traffic", rcvd, vars.get_req_bin("Client_AppData"));
2✔
2568
                     result.test_u64_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
1✔
2569

2570
                     ctx->send(vars.get_req_bin("Server_AppData"));
3✔
2571
                     ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
2✔
2572
                     result.test_bin_eq("correct server application data",
1✔
2573
                                        ctx->pull_send_buffer(),
2✔
2574
                                        vars.get_req_bin("Record_Server_AppData"));
2✔
2575
                  }),
1✔
2576

2577
            CHECK("Terminate Connection",
2578
                  [&](Test::Result& result) {
1✔
2579
                     result.require("ctx is available", ctx != nullptr);
1✔
2580
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2581

2582
                     ctx->check_callback_invocations(
2✔
2583
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2584

2585
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
2586
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
2587
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2588

2589
                     ctx->server.close();
1✔
2590

2591
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
2592
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
2593
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2594
                     result.test_bin_eq("Server's close notify",
1✔
2595
                                        ctx->pull_send_buffer(),
2✔
2596
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2597
                  }),
1✔
2598
         };
6✔
2599
      }
2✔
2600

2601
      std::vector<Test::Result> raw_public_key_with_client_authentication(const VarMap& vars) override {
1✔
2602
         auto rng = std::make_unique<Fixed_Output_RNG>("");
1✔
2603

2604
         // 32 - for server hello random
2605
         // 32 - for KeyShare (eph. x25519 key pair)
2606
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2607

2608
         auto sort_our_extensions =
1✔
2609
            [&](Botan::TLS::Extensions& exts, Botan::TLS::Connection_Side /* side */, Botan::TLS::Handshake_Type type) {
4✔
2610
               // This is the order of extensions when we first introduced the raw
2611
               // public key authentication implementation and generated the transcript.
2612
               // To stay compatible with the now hard-coded transcript, we pin the
2613
               // extension order.
2614
               if(type == Botan::TLS::Handshake_Type::EncryptedExtensions) {
4✔
2615
                  sort_extensions(exts,
2✔
2616
                                  {
2617
                                     Botan::TLS::Extension_Code::ClientCertificateType,
2618
                                     Botan::TLS::Extension_Code::ServerCertificateType,
2619
                                     Botan::TLS::Extension_Code::SupportedGroups,
2620
                                     Botan::TLS::Extension_Code::RecordSizeLimit,
2621
                                     Botan::TLS::Extension_Code::ServerNameIndication,
2622
                                  });
2623
               } else if(type == Botan::TLS::Handshake_Type::ServerHello) {
3✔
2624
                  sort_extensions(exts,
2✔
2625
                                  {
2626
                                     Botan::TLS::Extension_Code::KeyShare,
2627
                                     Botan::TLS::Extension_Code::SupportedVersions,
2628
                                  });
2629
               }
2630
            };
4✔
2631

2632
         std::unique_ptr<Server_Context> ctx;
1✔
2633

2634
         return {
1✔
2635
            CHECK("Receive Client Hello",
2636
                  [&](Test::Result& result) {
1✔
2637
                     ctx = std::make_unique<Server_Context>(std::move(rng),
1✔
2638
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_rawpubkey"),
2✔
2639
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
2640
                                                            sort_our_extensions,
2641
                                                            make_mock_signatures(vars));
3✔
2642
                     result.test_is_true("server not closed", !ctx->server.is_closed());
1✔
2643

2644
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2645

2646
                     ctx->check_callback_invocations(result,
2✔
2647
                                                     "client hello received",
2648
                                                     {"tls_emit_data",
2649
                                                      "tls_examine_extensions_client_hello",
2650
                                                      "tls_modify_extensions_server_hello",
2651
                                                      "tls_modify_extensions_encrypted_extensions",
2652
                                                      "tls_modify_extensions_certificate_request",
2653
                                                      "tls_modify_extensions_certificate",
2654
                                                      "tls_sign_message",
2655
                                                      "tls_generate_ephemeral_key",
2656
                                                      "tls_ephemeral_key_agreement",
2657
                                                      "tls_inspect_handshake_msg_client_hello",
2658
                                                      "tls_inspect_handshake_msg_server_hello",
2659
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2660
                                                      "tls_inspect_handshake_msg_certificate_request",
2661
                                                      "tls_inspect_handshake_msg_certificate",
2662
                                                      "tls_inspect_handshake_msg_certificate_verify",
2663
                                                      "tls_inspect_handshake_msg_finished"});
2664
                  }),
1✔
2665

2666
            CHECK("Verify server's generated handshake messages",
2667
                  [&](Test::Result& result) {
1✔
2668
                     result.require("ctx is available", ctx != nullptr);
1✔
2669
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2670

2671
                     result.test_bin_eq("Server Hello",
2✔
2672
                                        msgs.at("server_hello")[0],
2✔
2673
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2674
                     result.test_bin_eq("Encrypted Extensions",
2✔
2675
                                        msgs.at("encrypted_extensions")[0],
2✔
2676
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2677
                     result.test_bin_eq("Certificate Request",
2✔
2678
                                        msgs.at("certificate_request")[0],
2✔
2679
                                        strip_message_header(vars.get_opt_bin("Message_CertificateRequest")));
3✔
2680
                     result.test_bin_eq("Certificate",
2✔
2681
                                        msgs.at("certificate")[0],
2✔
2682
                                        strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2683
                     result.test_bin_eq("CertificateVerify",
2✔
2684
                                        msgs.at("certificate_verify")[0],
2✔
2685
                                        strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2686
                     result.test_bin_eq("Finished",
2✔
2687
                                        msgs.at("finished")[0],
2✔
2688
                                        strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2689

2690
                     result.test_bin_eq("Server's entire first flight",
1✔
2691
                                        ctx->pull_send_buffer(),
2✔
2692
                                        concat(vars.get_req_bin("Record_ServerHello"),
4✔
2693
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2694

2695
                     result.test_is_true("Not yet aware of client's cert chain", ctx->server.peer_cert_chain().empty());
1✔
2696
                     result.test_is_true("Server could now send application data", ctx->server.is_active());
1✔
2697
                     result.test_is_true("handshake is not yet complete",
1✔
2698
                                         !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2699
                  }),
1✔
2700

2701
            CHECK("Receive Client's second flight",
2702
                  [&](Test::Result& result) {
1✔
2703
                     result.require("ctx is available", ctx != nullptr);
1✔
2704
                     // This encrypted message contains the following messages:
2705
                     // * client's Certificate message
2706
                     // * client's Certificate_Verify message
2707
                     // * client's Finished message
2708
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2709

2710
                     ctx->check_callback_invocations(result,
2✔
2711
                                                     "client finished received",
2712
                                                     {"tls_inspect_handshake_msg_certificate",
2713
                                                      "tls_inspect_handshake_msg_certificate_verify",
2714
                                                      "tls_inspect_handshake_msg_finished",
2715
                                                      "tls_examine_extensions_certificate",
2716
                                                      "tls_verify_raw_public_key",
2717
                                                      "tls_verify_message",
2718
                                                      "tls_current_timestamp",
2719
                                                      "tls_session_established",
2720
                                                      "tls_session_activated"});
2721

2722
                     const auto raw_pk = ctx->server.peer_raw_public_key();
1✔
2723
                     result.test_is_true(
1✔
2724
                        "Received client's raw public key",
2725
                        raw_pk && raw_pk->fingerprint_public() == client_raw_public_key_pair()->fingerprint_public());
5✔
2726

2727
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2728
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2729
                  }),
1✔
2730

2731
            CHECK("Receive Client close_notify",
2732
                  [&](Test::Result& result) {
1✔
2733
                     result.require("ctx is available", ctx != nullptr);
1✔
2734
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2735

2736
                     ctx->check_callback_invocations(
2✔
2737
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2738

2739
                     result.test_is_true("connection is not yet closed", !ctx->server.is_closed());
1✔
2740
                     result.test_is_true("connection is still active", ctx->server.is_active());
1✔
2741
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2742
                  }),
1✔
2743

2744
            CHECK("Expect Server close_notify",
2745
                  [&](Test::Result& result) {
1✔
2746
                     result.require("ctx is available", ctx != nullptr);
1✔
2747
                     ctx->server.close();
1✔
2748

2749
                     result.test_is_true("connection is now inactive", !ctx->server.is_active());
1✔
2750
                     result.test_is_true("connection is now closed", ctx->server.is_closed());
1✔
2751
                     result.test_is_true("handshake is still complete", ctx->server.is_handshake_complete());
1✔
2752
                     result.test_bin_eq("Server's close notify",
1✔
2753
                                        ctx->pull_send_buffer(),
2✔
2754
                                        vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2755
                  }),
1✔
2756
         };
6✔
2757
      }
2✔
2758
};
2759

2760
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2761
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2762

2763
#endif
2764

2765
}  // namespace Botan_Tests
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