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

randombit / botan / 22032430615

15 Feb 2026 08:21AM UTC coverage: 89.997% (+0.002%) from 89.995%
22032430615

Pull #5303

github

web-flow
Merge 2cb874c36 into a985be798
Pull Request #5303: Refactor: Organize TLS Extensions into TLS 1.2 and 1.3 Modules

102265 of 113632 relevant lines covered (90.0%)

11431495.27 hits per line

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

97.35
/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_12) && defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) &&             \
17
   defined(BOTAN_HAS_AEAD_GCM) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_X25519) && defined(BOTAN_HAS_SHA2_32) && \
18
   defined(BOTAN_HAS_SHA2_64) && 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_12.h>
35
   #include <botan/tls_extensions_13.h>
36
   #include <botan/tls_messages.h>
37
   #include <botan/tls_policy.h>
38
   #include <botan/tls_server.h>
39
   #include <botan/tls_session_manager.h>
40
   #include <botan/x509_key.h>
41
   #include <botan/internal/concat_util.h>
42
   #include <botan/internal/fmt.h>
43
   #include <botan/internal/stl_util.h>
44
#endif
45

46
namespace Botan_Tests {
47

48
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
49

50
namespace {
51

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

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

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

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

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

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

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

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

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

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

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

113
   private:
114
      size_t m_padding_bytes;
115
};
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

428
         return m_server_private_key;
3✔
429
      }
430

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

542
   private:
543
      bool m_rfc8448;
544
};
545

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

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

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

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

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

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

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

592
         return handle;
1✔
593
      }
594

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

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

613
         return found_sessions;
7✔
614
      }
×
615

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

849
   return result;
9✔
850
}
×
851

852
}  // namespace
853

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1145
               add_renegotiation_extension(exts);
1✔
1146

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1360
                     result.test_eq("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_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_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("Client Hello",
1452
                  [&](Test::Result& result) {
1✔
1453
                     ctx =
1✔
1454
                        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_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_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_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_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_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_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_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
2✔
1643
                     result.test_is_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_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_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1716
                  }),
1✔
1717

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2199
         };
8✔
2200
      }
2✔
2201

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2335
         };
6✔
2336
      }
2✔
2337

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2452
         };
6✔
2453
      }
2✔
2454

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2762
#endif
2763

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