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

randombit / botan / 22093983912

17 Feb 2026 10:00AM UTC coverage: 90.027% (-0.001%) from 90.028%
22093983912

push

github

web-flow
Merge pull request #5347 from randombit/jack/tls-header-patrol-2

Changes to reduce dependencies in TLS sources/headers

102345 of 113683 relevant lines covered (90.03%)

11397934.0 hits per line

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

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

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

14
// Since RFC 8448 uses a specific set of cipher suites we can only run this
15
// test if all of them are enabled.
16
#if defined(BOTAN_HAS_TLS_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/hex.h>
31
   #include <botan/pk_algs.h>
32
   #include <botan/pkcs8.h>
33
   #include <botan/tls_callbacks.h>
34
   #include <botan/tls_client.h>
35
   #include <botan/tls_extensions_12.h>
36
   #include <botan/tls_extensions_13.h>
37
   #include <botan/tls_messages.h>
38
   #include <botan/tls_policy.h>
39
   #include <botan/tls_server.h>
40
   #include <botan/tls_session_manager.h>
41
   #include <botan/x509_key.h>
42
   #include <botan/x509cert.h>
43
   #include <botan/internal/concat_util.h>
44
   #include <botan/internal/fmt.h>
45
   #include <botan/internal/stl_util.h>
46
#endif
47

48
namespace Botan_Tests {
49

50
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
51

52
namespace {
53

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

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

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

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

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

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

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

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

107
      explicit Padding(const size_t padding_bytes) : m_padding_bytes(padding_bytes) {}
2✔
108

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

113
      bool empty() const override { return m_padding_bytes == 0; }
5✔
114

115
   private:
116
      size_t m_padding_bytes;
117
};
118

119
using namespace Botan;
120
using namespace Botan::TLS;
121

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

295
         try {
102✔
296
            auto serialized_message = message.serialize();
102✔
297

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

306
         return Callbacks::tls_inspect_handshake_msg(message);
102✔
307
      }
308

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

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

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

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

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

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

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

343
      uint64_t last_received_seq_no() const { return received_seq_no; }
4✔
344

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

347
      void reset_callback_invocation_counters() { m_callback_invocations.clear(); }
60✔
348

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

355
         m_callback_invocations[callback_name]++;
339✔
356
      }
339✔
357

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

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

374
      mutable std::map<std::string, unsigned int> m_callback_invocations;
375
};
376

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

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

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

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

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

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

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

426
         if(m_alternative_server_certificate) {
4✔
427
            return m_bogus_alternative_server_private_key;
1✔
428
         }
429

430
         return m_server_private_key;
3✔
431
      }
432

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

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

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

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

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

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

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

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

493
         return Botan::TLS::Text_Policy(is);
14✔
494
      }
14✔
495

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

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

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

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

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

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

544
   private:
545
      bool m_rfc8448;
546
};
547

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

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

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

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

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

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

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

594
         return handle;
1✔
595
      }
596

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

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

615
         return found_sessions;
7✔
616
      }
×
617

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

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

626
   private:
627
      std::vector<Session_with_Handle> m_sessions;
628
};
629

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

658
   public:
659
      virtual ~TLS_Context() = default;
70✔
660

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

664
      TLS_Context(TLS_Context&&) = delete;
665
      TLS_Context& operator=(TLS_Context&&) = delete;
666

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

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

671
      uint64_t last_received_seq_no() const { return m_callbacks->last_received_seq_no(); }
4✔
672

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

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

696
         m_callbacks->reset_callback_invocation_counters();
60✔
697
      }
60✔
698

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

848
   mock("Server_MessageToSign", "Server_MessageSignature");
18✔
849
   mock("Client_MessageToSign", "Client_MessageSignature");
18✔
850

851
   return result;
9✔
852
}
×
853

854
}  // namespace
855

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

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

905
      virtual std::string side() const = 0;
906

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

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

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

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

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

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

991
               add_renegotiation_extension(exts);
1✔
992
               sort_rfc8448_extensions(exts, side);
1✔
993
            }
994
         };
1✔
995

996
         std::unique_ptr<Client_Context> ctx;
1✔
997

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

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

1017
                     result.test_bin_eq(
1✔
1018
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1019
                  }),
1✔
1020

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1121
                     result.test_bin_eq(
1✔
1122
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
1✔
1123
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1124

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

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

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

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

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

1147
               add_renegotiation_extension(exts);
1✔
1148

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

1158
         std::unique_ptr<Client_Context> ctx;
1✔
1159

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

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

1182
                     result.test_bin_eq(
1✔
1183
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1184
                  })
1✔
1185

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

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

1199
               if(flights == 1) {
2✔
1200
                  add_renegotiation_extension(exts);
1✔
1201
               }
1202

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

1209
               sort_rfc8448_extensions(exts, side);
2✔
1210
            }
1211
         };
2✔
1212

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

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

1222
         std::unique_ptr<Client_Context> ctx;
1✔
1223

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

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

1243
                     result.test_bin_eq(
1✔
1244
                        "TLS client hello (1)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1245
                  }),
1✔
1246

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

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

1263
                     result.test_bin_eq(
1✔
1264
                        "TLS client hello (2)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_2"));
1✔
1265
                  }),
1✔
1266

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

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

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

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

1301
                     result.test_bin_eq(
1✔
1302
                        "client finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
1✔
1303
                  }),
1✔
1304

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

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

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

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

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

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

1339
         std::unique_ptr<Client_Context> ctx;
1✔
1340

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

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

1362
                     result.test_bin_eq(
1✔
1363
                        "Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1364
                  }),
1✔
1365

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

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

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

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

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

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

1419
                     ctx->check_callback_invocations(result,
2✔
1420
                                                     "after sending close notify",
1421
                                                     {
1422
                                                        "tls_emit_data",
1423
                                                     });
1424

1425
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
1✔
1426
                     result.test_is_true("connection closed", ctx->client.is_closed());
1✔
1427

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

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

1437
         // 32 - client hello random
1438
         // 32 - legacy session ID
1439
         // 32 - eph. x25519 key pair
1440
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
1✔
1441

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

1451
         std::unique_ptr<Client_Context> ctx;
1✔
1452

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

1462
                  result.test_bin_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1463

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

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

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

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

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

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

1517
                     result.test_bin_eq(
1✔
1518
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
1✔
1519

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

1524
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
1✔
1525

1526
                     result.test_is_true("client connection was terminated", ctx->client.is_closed());
1✔
1527
                  }),
1✔
1528
         };
4✔
1529
      }
2✔
1530

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

1534
         // 32 - for client hello random
1535
         // 32 - for KeyShare (eph. x25519 key pair)
1536
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
1✔
1537

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

1557
         std::unique_ptr<Client_Context> ctx;
1✔
1558

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

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

1583
                     result.test_bin_eq(
1✔
1584
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1585
                  }),
1✔
1586

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

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

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

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

1621
                  result.test_bin_eq(
1✔
1622
                     "correct handshake finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
1✔
1623
               }),
1✔
1624

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

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

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

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

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

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

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

1654
                     result.test_bin_eq(
1✔
1655
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
1✔
1656
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1657

1658
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
1✔
1659
                     ctx->check_callback_invocations(
2✔
1660
                        result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1661

1662
                     result.test_is_true("connection is closed", ctx->client.is_closed());
1✔
1663
                  }),
1✔
1664
         };
7✔
1665
      }
2✔
1666

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

1670
         // 32 - for client hello random
1671
         // 32 - for KeyShare (eph. x25519 key pair)
1672
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
1✔
1673

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

1695
         std::unique_ptr<Client_Context> ctx;
1✔
1696

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

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

1718
                     result.test_bin_eq(
1✔
1719
                        "Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
1✔
1720
                  }),
1✔
1721

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

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

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

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

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

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

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

1780
                     ctx->check_callback_invocations(result,
2✔
1781
                                                     "after sending close notify",
1782
                                                     {
1783
                                                        "tls_emit_data",
1784
                                                     });
1785

1786
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
1✔
1787
                     result.test_is_true("connection closed", ctx->client.is_closed());
1✔
1788

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

1796
class Test_TLS_RFC8448_Server : public Test_TLS_RFC8448 {
1✔
1797
   private:
1798
      std::string side() const override { return "Server"; }
7✔
1799

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

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

1808
         std::unique_ptr<Server_Context> ctx;
1✔
1809

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

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

1833
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
1834

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

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

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

1871
                     result.test_bin_eq("Server's entire first flight",
1✔
1872
                                        ctx->pull_send_buffer(),
1✔
1873
                                        concat(vars.get_req_bin("Record_ServerHello"),
3✔
1874
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1875

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

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

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

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

1903
                     result.test_sz_eq("session ticket was sent", new_tickets, 1);
1✔
1904

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

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

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

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

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

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

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

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

1949
                     ctx->check_callback_invocations(
2✔
1950
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1951

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

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

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

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

1975
         // 32 - for server hello random
1976
         // 32 - for KeyShare (eph. x25519 key pair)
1977
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
1978

1979
         std::unique_ptr<Server_Context> ctx;
1✔
1980

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

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

2004
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2005

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

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

2028
                     result.test_bin_eq("Server Hello",
2✔
2029
                                        msgs.at("server_hello")[0],
1✔
2030
                                        strip_message_header(vars.get_opt_bin("Message_ServerHello")));
2✔
2031
                     result.test_bin_eq("Encrypted Extensions",
2✔
2032
                                        msgs.at("encrypted_extensions")[0],
1✔
2033
                                        strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
2✔
2034

2035
                     result.test_bin_eq("Server's entire first flight",
1✔
2036
                                        ctx->pull_send_buffer(),
1✔
2037
                                        concat(vars.get_req_bin("Record_ServerHello"),
3✔
2038
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2039

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

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

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

2060
         // 32 - for server hello random
2061
         // 32 - for KeyShare (eph. P-256 key pair)
2062
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
2063

2064
         std::unique_ptr<Server_Context> ctx;
1✔
2065

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

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

2087
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2088

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

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

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

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

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

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

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

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

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

2173
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2174
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2175
                  }),
1✔
2176

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

2182
                     ctx->check_callback_invocations(
2✔
2183
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2184

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

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

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

2203
         };
8✔
2204
      }
2✔
2205

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

2209
         // 32 - for server hello random
2210
         // 32 - for KeyShare (eph. x25519 pair)
2211
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
2212

2213
         std::unique_ptr<Server_Context> ctx;
1✔
2214

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

2227
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2228

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

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

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

2273
                     result.test_bin_eq("Server's entire first flight",
1✔
2274
                                        ctx->pull_send_buffer(),
1✔
2275
                                        concat(vars.get_req_bin("Record_ServerHello"),
3✔
2276
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2277

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

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

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

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

2309
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2310
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2311
                  }),
1✔
2312

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

2318
                     ctx->check_callback_invocations(
2✔
2319
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2320

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

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

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

2339
         };
6✔
2340
      }
2✔
2341

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

2345
         // 32 - for server hello random
2346
         // 32 - for KeyShare (eph. x25519 pair)
2347
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
2348

2349
         std::unique_ptr<Server_Context> ctx;
1✔
2350

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

2362
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2363

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

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

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

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

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

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

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

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

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

2435
                     ctx->check_callback_invocations(
2✔
2436
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2437

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

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

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

2456
         };
6✔
2457
      }
2✔
2458

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

2462
         // 32 - for server hello random
2463
         // 32 - for KeyShare (eph. x25519 key pair)
2464
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
2465

2466
         std::unique_ptr<Server_Context> ctx;
1✔
2467

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

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

2507
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2508

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

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

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

2538
                     result.test_bin_eq("Server's entire first flight",
1✔
2539
                                        ctx->pull_send_buffer(),
1✔
2540
                                        concat(vars.get_req_bin("Record_ServerHello"),
3✔
2541
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2542

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

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

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

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

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

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

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

2585
                     ctx->check_callback_invocations(
2✔
2586
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2587

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

2592
                     ctx->server.close();
1✔
2593

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

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

2607
         // 32 - for server hello random
2608
         // 32 - for KeyShare (eph. x25519 key pair)
2609
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
1✔
2610

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

2635
         std::unique_ptr<Server_Context> ctx;
1✔
2636

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

2647
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2648

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

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

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

2693
                     result.test_bin_eq("Server's entire first flight",
1✔
2694
                                        ctx->pull_send_buffer(),
1✔
2695
                                        concat(vars.get_req_bin("Record_ServerHello"),
3✔
2696
                                               vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2697

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

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

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

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

2730
                     result.test_is_true("TLS handshake finished", ctx->server.is_active());
1✔
2731
                     result.test_is_true("handshake is complete", ctx->server.is_handshake_complete());
1✔
2732
                  }),
1✔
2733

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

2739
                     ctx->check_callback_invocations(
2✔
2740
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2741

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

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

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

2763
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2764
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2765

2766
#endif
2767

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