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

randombit / botan / 19012754211

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

push

github

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

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

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

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

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

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

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

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

43
namespace Botan_Tests {
44

45
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
46

47
namespace {
48

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

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

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

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

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

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

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

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

102
      explicit Padding(const size_t padding_bytes) : m_padding_bytes(padding_bytes) {}
2✔
103

104
      std::vector<uint8_t> serialize(Botan::TLS::Connection_Side) const override {
5✔
105
         return std::vector<uint8_t>(m_padding_bytes, 0x00);
5✔
106
      }
107

108
      bool empty() const override { return m_padding_bytes == 0; }
5✔
109

110
   private:
111
      size_t m_padding_bytes;
112
};
113

114
using namespace Botan;
115
using namespace Botan::TLS;
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

290
         try {
102✔
291
            auto serialized_message = message.serialize();
102✔
292

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

301
         return Callbacks::tls_inspect_handshake_msg(message);
102✔
302
      }
303

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

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

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

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

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

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

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

338
      uint64_t last_received_seq_no() const { return received_seq_no; }
4✔
339

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

342
      void reset_callback_invocation_counters() { m_callback_invocations.clear(); }
60✔
343

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

350
         m_callback_invocations[callback_name]++;
339✔
351
      }
339✔
352

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

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

369
      mutable std::map<std::string, unsigned int> m_callback_invocations;
370
};
371

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

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

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

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

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

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

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

421
         if(m_alternative_server_certificate) {
4✔
422
            return m_bogus_alternative_server_private_key;
1✔
423
         }
424

425
         return m_server_private_key;
3✔
426
      }
427

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

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

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

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

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

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

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

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

488
         return Botan::TLS::Text_Policy(is);
14✔
489
      }
14✔
490

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

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

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

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

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

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

539
   private:
540
      bool m_rfc8448;
541
};
542

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

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

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

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

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

578
         if(m_sessions.size() != 1) {
1✔
579
            throw Test_Error("No mocked session handle available; Test bug?");
×
580
         }
581

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

587
         return handle;
1✔
588
      }
589

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

599
      std::vector<Session_with_Handle> find_some(const Server_Information& info, const size_t) override {
7✔
600
         std::vector<Session_with_Handle> found_sessions;
7✔
601
         for(const auto& [session, handle] : m_sessions) {
8✔
602
            if(session.server_info() == info) {
1✔
603
               found_sessions.emplace_back(Session_with_Handle{session, handle});
2✔
604
            }
605
         }
606

607
         return found_sessions;
7✔
608
      }
×
609

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

612
      size_t remove_all() override {
×
613
         const auto sessions = m_sessions.size();
×
614
         m_sessions.clear();
×
615
         return sessions;
×
616
      }
617

618
   private:
619
      std::vector<Session_with_Handle> m_sessions;
620
};
621

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

650
   public:
651
      virtual ~TLS_Context() = default;
70✔
652

653
      TLS_Context(TLS_Context&) = delete;
654
      TLS_Context& operator=(const TLS_Context&) = delete;
655

656
      TLS_Context(TLS_Context&&) = delete;
657
      TLS_Context& operator=(TLS_Context&&) = delete;
658

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

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

663
      uint64_t last_received_seq_no() const { return m_callbacks->last_received_seq_no(); }
4✔
664

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

679
         for(const auto& invoke : invokes) {
371✔
680
            if(invoke.second == 0) {
311✔
681
               continue;
×
682
            }
683
            result.confirm(
622✔
684
               invoke.first + " was expected (Context: " + context + ")",
1,244✔
685
               std::find(callback_names.cbegin(), callback_names.cend(), invoke.first) != callback_names.cend());
622✔
686
         }
687

688
         m_callbacks->reset_callback_invocation_counters();
60✔
689
      }
60✔
690

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

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

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

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

699
      /**
700
       * Send application data through the secure channel
701
       */
702
      virtual void send(const std::vector<uint8_t>& data) = 0;
703

704
   protected:
705
      std::shared_ptr<Test_TLS_13_Callbacks> m_callbacks;  // NOLINT(*-non-private-member-variable*)
706
      std::shared_ptr<Test_Credentials> m_creds;           // NOLINT(*-non-private-member-variable*)
707

708
      std::shared_ptr<Botan::RandomNumberGenerator> m_rng;     // NOLINT(*-non-private-member-variable*)
709
      std::shared_ptr<RFC8448_Session_Manager> m_session_mgr;  // NOLINT(*-non-private-member-variable*)
710
      std::shared_ptr<const RFC8448_Text_Policy> m_policy;     // NOLINT(*-non-private-member-variable*)
711
};
712

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

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

740
      Botan::TLS::Client client;  // NOLINT(*-non-private-member-variable*)
741
};
742

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

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

765
      Botan::TLS::Server server;  // NOLINT(*-non-private-member-variable*)
766
};
767

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

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

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

822
void add_early_data_indication(Botan::TLS::Extensions& exts) {
1✔
823
   exts.add(new Botan::TLS::EarlyDataIndication());  // NOLINT(*-owning-memory)
1✔
824
}
1✔
825

826
std::vector<uint8_t> strip_message_header(const std::vector<uint8_t>& msg) {
31✔
827
   BOTAN_ASSERT_NOMSG(msg.size() >= 4);
31✔
828
   return {msg.begin() + 4, msg.end()};
31✔
829
}
830

831
std::vector<MockSignature> make_mock_signatures(const VarMap& vars) {
9✔
832
   std::vector<MockSignature> result;
9✔
833

834
   auto mock = [&](const std::string& msg, const std::string& sig) {
27✔
835
      if(vars.has_key(msg) && vars.has_key(sig)) {
36✔
836
         result.push_back({vars.get_opt_bin(msg), vars.get_opt_bin(sig)});
11✔
837
      }
838
   };
29✔
839

840
   mock("Server_MessageToSign", "Server_MessageSignature");
18✔
841
   mock("Client_MessageToSign", "Client_MessageSignature");
18✔
842

843
   return result;
9✔
844
}
×
845

846
}  // namespace
847

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

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

897
      virtual std::string side() const = 0;
898

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

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

963
class Test_TLS_RFC8448_Client : public Test_TLS_RFC8448 {
1✔
964
   private:
965
      std::string side() const override { return "Client"; }
7✔
966

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

970
         // 32 - for client hello random
971
         // 32 - for KeyShare (eph. x25519 key pair)
972
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
973

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

983
               add_renegotiation_extension(exts);
1✔
984
               sort_rfc8448_extensions(exts, side);
1✔
985
            }
986
         };
1✔
987

988
         std::unique_ptr<Client_Context> ctx;
1✔
989

990
         return {
1✔
991
            CHECK("Client Hello",
992
                  [&](Test::Result& result) {
1✔
993
                     ctx = std::make_unique<Client_Context>(rng,
1✔
994
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
995
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
996
                                                            add_extensions_and_sort);
1✔
997

998
                     result.confirm("client not closed", !ctx->client.is_closed());
2✔
999
                     ctx->check_callback_invocations(result,
2✔
1000
                                                     "client hello prepared",
1001
                                                     {
1002
                                                        "tls_emit_data",
1003
                                                        "tls_inspect_handshake_msg_client_hello",
1004
                                                        "tls_modify_extensions_client_hello",
1005
                                                        "tls_generate_ephemeral_key",
1006
                                                        "tls_current_timestamp",
1007
                                                     });
1008

1009
                     result.test_eq(
2✔
1010
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1011
                  }),
1✔
1012

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

1021
                     ctx->client.received_data(server_hello_a);
1✔
1022
                     ctx->check_callback_invocations(result, "server hello partially received", {});
2✔
1023

1024
                     ctx->client.received_data(server_hello_b);
1✔
1025
                     ctx->check_callback_invocations(result,
2✔
1026
                                                     "server hello received",
1027
                                                     {"tls_inspect_handshake_msg_server_hello",
1028
                                                      "tls_examine_extensions_server_hello",
1029
                                                      "tls_ephemeral_key_agreement"});
1030

1031
                     result.confirm("client is not yet active", !ctx->client.is_active());
2✔
1032
                     result.confirm("handshake is not yet complete", !ctx->client.is_handshake_complete());
2✔
1033
                  }),
3✔
1034

1035
            CHECK("Server HS messages .. Client Finished",
1036
                  [&](Test::Result& result) {
1✔
1037
                     result.require("ctx is available", ctx != nullptr);
1✔
1038
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1039

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

1059
                     result.test_eq("correct handshake finished",
2✔
1060
                                    ctx->pull_send_buffer(),
2✔
1061
                                    vars.get_req_bin("Record_ClientFinished"));
2✔
1062
                  }),
1✔
1063

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

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

1084
            CHECK("Send Application Data",
1085
                  [&](Test::Result& result) {
1✔
1086
                     result.require("ctx is available", ctx != nullptr);
1✔
1087
                     ctx->send(vars.get_req_bin("Client_AppData"));
3✔
1088

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

1091
                     result.test_eq("correct client application data",
2✔
1092
                                    ctx->pull_send_buffer(),
2✔
1093
                                    vars.get_req_bin("Record_Client_AppData"));
2✔
1094
                  }),
1✔
1095

1096
            CHECK("Receive Application Data",
1097
                  [&](Test::Result& result) {
1✔
1098
                     result.require("ctx is available", ctx != nullptr);
1✔
1099
                     ctx->client.received_data(vars.get_req_bin("Record_Server_AppData"));
2✔
1100

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

1103
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1104
                     result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
3✔
1105
                     result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(1));
2✔
1106
                  }),
1✔
1107

1108
            CHECK("Close Connection",
1109
                  [&](Test::Result& result) {
1✔
1110
                     result.require("ctx is available", ctx != nullptr);
1✔
1111
                     ctx->client.close();
1✔
1112

1113
                     result.test_eq(
2✔
1114
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1115
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1116

1117
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1118
                     ctx->check_callback_invocations(
2✔
1119
                        result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1120

1121
                     result.confirm("connection is closed", ctx->client.is_closed());
2✔
1122
                  }),
1✔
1123
         };
8✔
1124
      }
3✔
1125

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

1129
         // 32 - for client hello random
1130
         // 32 - for KeyShare (eph. x25519 key pair)
1131
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1132

1133
         auto add_extensions_and_sort = [](Botan::TLS::Extensions& exts,
2✔
1134
                                           Botan::TLS::Connection_Side side,
1135
                                           Botan::TLS::Handshake_Type which_message) {
1136
            if(which_message == Handshake_Type::ClientHello) {
1✔
1137
               exts.add(new Padding(87));  // NOLINT(*-owning-memory)
1✔
1138

1139
               add_renegotiation_extension(exts);
1✔
1140

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

1150
         std::unique_ptr<Client_Context> ctx;
1✔
1151

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

1163
                     result.confirm("client not closed", !ctx->client.is_closed());
2✔
1164
                     ctx->check_callback_invocations(result,
2✔
1165
                                                     "client hello prepared",
1166
                                                     {
1167
                                                        "tls_emit_data",
1168
                                                        "tls_inspect_handshake_msg_client_hello",
1169
                                                        "tls_modify_extensions_client_hello",
1170
                                                        "tls_current_timestamp",
1171
                                                        "tls_generate_ephemeral_key",
1172
                                                     });
1173

1174
                     result.test_eq(
2✔
1175
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1176
                  })
1✔
1177

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

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

1191
               if(flights == 1) {
2✔
1192
                  add_renegotiation_extension(exts);
1✔
1193
               }
1194

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

1201
               sort_rfc8448_extensions(exts, side);
2✔
1202
            }
1203
         };
2✔
1204

1205
         // Fallback RNG is required to for blinding in ECDH with P-256
1206
         auto& fallback_rng = this->rng();
1✔
1207
         auto rng = std::make_unique<Fixed_Output_RNG>(fallback_rng);
1✔
1208

1209
         // 32 - client hello random
1210
         // 32 - eph. x25519 key pair
1211
         // 32 - eph. P-256 key pair
1212
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1213

1214
         std::unique_ptr<Client_Context> ctx;
1✔
1215

1216
         return {
1✔
1217
            CHECK("Client Hello",
1218
                  [&](Test::Result& result) {
1✔
1219
                     ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1220
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_client"),
2✔
1221
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1222
                                                            add_extensions_and_sort);
1✔
1223
                     result.confirm("client not closed", !ctx->client.is_closed());
2✔
1224

1225
                     ctx->check_callback_invocations(result,
2✔
1226
                                                     "client hello prepared",
1227
                                                     {
1228
                                                        "tls_emit_data",
1229
                                                        "tls_inspect_handshake_msg_client_hello",
1230
                                                        "tls_modify_extensions_client_hello",
1231
                                                        "tls_generate_ephemeral_key",
1232
                                                        "tls_current_timestamp",
1233
                                                     });
1234

1235
                     result.test_eq(
2✔
1236
                        "TLS client hello (1)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1237
                  }),
1✔
1238

1239
            CHECK("Hello Retry Request .. second Client Hello",
1240
                  [&](Test::Result& result) {
1✔
1241
                     result.require("ctx is available", ctx != nullptr);
1✔
1242
                     ctx->client.received_data(vars.get_req_bin("Record_HelloRetryRequest"));
2✔
1243

1244
                     ctx->check_callback_invocations(result,
2✔
1245
                                                     "hello retry request received",
1246
                                                     {
1247
                                                        "tls_emit_data",
1248
                                                        "tls_inspect_handshake_msg_hello_retry_request",
1249
                                                        "tls_examine_extensions_hello_retry_request",
1250
                                                        "tls_inspect_handshake_msg_client_hello",
1251
                                                        "tls_modify_extensions_client_hello",
1252
                                                        "tls_generate_ephemeral_key",
1253
                                                     });
1254

1255
                     result.test_eq(
2✔
1256
                        "TLS client hello (2)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_2"));
3✔
1257
                  }),
1✔
1258

1259
            CHECK("Server Hello",
1260
                  [&](Test::Result& result) {
1✔
1261
                     result.require("ctx is available", ctx != nullptr);
1✔
1262
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1263

1264
                     ctx->check_callback_invocations(result,
2✔
1265
                                                     "server hello received",
1266
                                                     {
1267
                                                        "tls_inspect_handshake_msg_server_hello",
1268
                                                        "tls_examine_extensions_server_hello",
1269
                                                        "tls_ephemeral_key_agreement",
1270
                                                     });
1271
                  }),
1✔
1272

1273
            CHECK("Server HS Messages .. Client Finished",
1274
                  [&](Test::Result& result) {
1✔
1275
                     result.require("ctx is available", ctx != nullptr);
1✔
1276
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1277

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

1293
                     result.test_eq(
2✔
1294
                        "client finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1295
                  }),
1✔
1296

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

1306
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1307
                     ctx->check_callback_invocations(
2✔
1308
                        result, "encrypted handshake messages received", {"tls_alert", "tls_peer_closed_connection"});
1309

1310
                     result.confirm("connection is closed", ctx->client.is_closed());
2✔
1311
                  }),
1✔
1312
         };
6✔
1313
      }
2✔
1314

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

1318
         // 32 - for client hello random
1319
         // 32 - for eph. x25519 key pair
1320
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1321

1322
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
3✔
1323
                                            Botan::TLS::Connection_Side side,
1324
                                            Botan::TLS::Handshake_Type which_message) {
1325
            if(which_message == Handshake_Type::ClientHello) {
2✔
1326
               add_renegotiation_extension(exts);
1✔
1327
               sort_rfc8448_extensions(exts, side);
1✔
1328
            }
1329
         };
1330

1331
         std::unique_ptr<Client_Context> ctx;
1✔
1332

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

1344
                     ctx->check_callback_invocations(result,
2✔
1345
                                                     "initial callbacks",
1346
                                                     {
1347
                                                        "tls_emit_data",
1348
                                                        "tls_inspect_handshake_msg_client_hello",
1349
                                                        "tls_modify_extensions_client_hello",
1350
                                                        "tls_generate_ephemeral_key",
1351
                                                        "tls_current_timestamp",
1352
                                                     });
1353

1354
                     result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1355
                  }),
1✔
1356

1357
            CHECK("Server Hello",
1358
                  [&](auto& result) {
1✔
1359
                     result.require("ctx is available", ctx != nullptr);
1✔
1360
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1361

1362
                     ctx->check_callback_invocations(result,
2✔
1363
                                                     "callbacks after server hello",
1364
                                                     {
1365
                                                        "tls_examine_extensions_server_hello",
1366
                                                        "tls_inspect_handshake_msg_server_hello",
1367
                                                        "tls_ephemeral_key_agreement",
1368
                                                     });
1369
                  }),
1✔
1370

1371
            CHECK("other handshake messages and client auth",
1372
                  [&](Test::Result& result) {
1✔
1373
                     result.require("ctx is available", ctx != nullptr);
1✔
1374
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1375

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

1397
                     // ClientFinished contains the entire coalesced client authentication flight
1398
                     // Messages: Certificate, CertificateVerify, Finished
1399
                     result.test_eq(
2✔
1400
                        "Client Auth and Finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1401
                  }),
1✔
1402

1403
            CHECK("Close Connection",
1404
                  [&](Test::Result& result) {
1✔
1405
                     result.require("ctx is available", ctx != nullptr);
1✔
1406
                     ctx->client.close();
1✔
1407
                     result.test_eq(
2✔
1408
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1409

1410
                     ctx->check_callback_invocations(result,
2✔
1411
                                                     "after sending close notify",
1412
                                                     {
1413
                                                        "tls_emit_data",
1414
                                                     });
1415

1416
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1417
                     result.confirm("connection closed", ctx->client.is_closed());
2✔
1418

1419
                     ctx->check_callback_invocations(
2✔
1420
                        result, "after receiving close notify", {"tls_alert", "tls_peer_closed_connection"});
1421
                  }),
1✔
1422
         };
5✔
1423
      }
2✔
1424

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

1428
         // 32 - client hello random
1429
         // 32 - legacy session ID
1430
         // 32 - eph. x25519 key pair
1431
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1432

1433
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
2✔
1434
                                            Botan::TLS::Connection_Side side,
1435
                                            Botan::TLS::Handshake_Type which_message) {
1436
            if(which_message == Handshake_Type::ClientHello) {
1✔
1437
               add_renegotiation_extension(exts);
1✔
1438
               sort_rfc8448_extensions(exts, side);
1✔
1439
            }
1440
         };
1441

1442
         std::unique_ptr<Client_Context> ctx;
1✔
1443

1444
         return {
1✔
1445
            CHECK("Client Hello",
1446
                  [&](Test::Result& result) {
1✔
1447
                     ctx =
1✔
1448
                        std::make_unique<Client_Context>(std::move(rng),
1✔
1449
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_client"),
2✔
1450
                                                         vars.get_req_u64("CurrentTimestamp"),
1✔
1451
                                                         add_extensions_and_sort);
1✔
1452

1453
                     result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1454

1455
                     ctx->check_callback_invocations(result,
2✔
1456
                                                     "client hello prepared",
1457
                                                     {
1458
                                                        "tls_emit_data",
1459
                                                        "tls_inspect_handshake_msg_client_hello",
1460
                                                        "tls_modify_extensions_client_hello",
1461
                                                        "tls_generate_ephemeral_key",
1462
                                                        "tls_current_timestamp",
1463
                                                     });
1464
                  }),
1✔
1465

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

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

1494
                     result.test_eq("CCS + Client Finished",
2✔
1495
                                    ctx->pull_send_buffer(),
2✔
1496
                                    // ClientFinished contains the expected ChangeCipherSpec record
1497
                                    vars.get_req_bin("Record_ClientFinished"));
2✔
1498

1499
                     result.confirm("client is ready to send application traffic", ctx->client.is_active());
2✔
1500
                     result.confirm("handshake is complete", ctx->client.is_handshake_complete());
2✔
1501
                  }),
1✔
1502

1503
            CHECK("Close connection",
1504
                  [&](Test::Result& result) {
1✔
1505
                     result.require("ctx is available", ctx != nullptr);
1✔
1506
                     ctx->client.close();
1✔
1507

1508
                     result.test_eq(
2✔
1509
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1510

1511
                     result.require("client cannot send application traffic anymore", !ctx->client.is_active());
1✔
1512
                     result.require("client is not fully closed yet", !ctx->client.is_closed());
1✔
1513
                     result.confirm("handshake stays completed", ctx->client.is_handshake_complete());
2✔
1514

1515
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1516

1517
                     result.confirm("client connection was terminated", ctx->client.is_closed());
2✔
1518
                  }),
1✔
1519
         };
4✔
1520
      }
2✔
1521

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

1525
         // 32 - for client hello random
1526
         // 32 - for KeyShare (eph. x25519 key pair)
1527
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1528

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

1548
         std::unique_ptr<Client_Context> ctx;
1✔
1549

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

1563
                     result.confirm("client not closed", !ctx->client.is_closed());
2✔
1564
                     ctx->check_callback_invocations(result,
2✔
1565
                                                     "client hello prepared",
1566
                                                     {
1567
                                                        "tls_emit_data",
1568
                                                        "tls_inspect_handshake_msg_client_hello",
1569
                                                        "tls_modify_extensions_client_hello",
1570
                                                        "tls_current_timestamp",
1571
                                                        "tls_generate_ephemeral_key",
1572
                                                     });
1573

1574
                     result.test_eq(
2✔
1575
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1576
                  }),
1✔
1577

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

1589
                     result.confirm("client is not yet active", !ctx->client.is_active());
2✔
1590
                     result.confirm("handshake is not yet complete", !ctx->client.is_handshake_complete());
2✔
1591
                  }),
1✔
1592

1593
            CHECK(
1594
               "Server HS messages .. Client Finished",
1595
               [&](Test::Result& result) {
1✔
1596
                  result.require("ctx is available", ctx != nullptr);
1✔
1597
                  ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1598

1599
                  ctx->check_callback_invocations(result,
2✔
1600
                                                  "encrypted handshake messages received",
1601
                                                  {"tls_inspect_handshake_msg_encrypted_extensions",
1602
                                                   "tls_inspect_handshake_msg_finished",
1603
                                                   "tls_examine_extensions_encrypted_extensions",
1604
                                                   "tls_emit_data",
1605
                                                   "tls_current_timestamp",
1606
                                                   "tls_session_established",
1607
                                                   "tls_session_activated"});
1608
                  result.require("PSK negotiated", ctx->psk_identity_negotiated() == vars.get_req_str("PskIdentity"));
2✔
1609
                  result.require("client is active", ctx->client.is_active());
1✔
1610
                  result.confirm("handshake is complete", ctx->client.is_handshake_complete());
2✔
1611

1612
                  result.test_eq(
2✔
1613
                     "correct handshake finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1614
               }),
1✔
1615

1616
            CHECK("Send Application Data",
1617
                  [&](Test::Result& result) {
1✔
1618
                     result.require("ctx is available", ctx != nullptr);
1✔
1619
                     ctx->send(vars.get_req_bin("Client_AppData"));
3✔
1620

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

1623
                     result.test_eq("correct client application data",
2✔
1624
                                    ctx->pull_send_buffer(),
2✔
1625
                                    vars.get_req_bin("Record_Client_AppData"));
2✔
1626
                  }),
1✔
1627

1628
            CHECK("Receive Application Data",
1629
                  [&](Test::Result& result) {
1✔
1630
                     result.require("ctx is available", ctx != nullptr);
1✔
1631
                     ctx->client.received_data(vars.get_req_bin("Record_Server_AppData"));
2✔
1632

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

1635
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1636
                     result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
3✔
1637
                     result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
2✔
1638
                  }),
1✔
1639

1640
            CHECK("Close Connection",
1641
                  [&](Test::Result& result) {
1✔
1642
                     result.require("ctx is available", ctx != nullptr);
1✔
1643
                     ctx->client.close();
1✔
1644

1645
                     result.test_eq(
2✔
1646
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1647
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1648

1649
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1650
                     ctx->check_callback_invocations(
2✔
1651
                        result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1652

1653
                     result.confirm("connection is closed", ctx->client.is_closed());
2✔
1654
                  }),
1✔
1655
         };
7✔
1656
      }
2✔
1657

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

1661
         // 32 - for client hello random
1662
         // 32 - for KeyShare (eph. x25519 key pair)
1663
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1664

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

1686
         std::unique_ptr<Client_Context> ctx;
1✔
1687

1688
         return {
1✔
1689
            CHECK("Client Hello",
1690
                  [&](Test::Result& result) {
1✔
1691
                     ctx = std::make_unique<Client_Context>(std::move(rng),
1✔
1692
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_rawpubkey"),
2✔
1693
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
1694
                                                            sort_our_extensions,
1695
                                                            std::nullopt,
1696
                                                            std::nullopt,
1697
                                                            make_mock_signatures(vars));
3✔
1698

1699
                     ctx->check_callback_invocations(result,
2✔
1700
                                                     "initial callbacks",
1701
                                                     {
1702
                                                        "tls_emit_data",
1703
                                                        "tls_inspect_handshake_msg_client_hello",
1704
                                                        "tls_modify_extensions_client_hello",
1705
                                                        "tls_generate_ephemeral_key",
1706
                                                        "tls_current_timestamp",
1707
                                                     });
1708

1709
                     result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1710
                  }),
1✔
1711

1712
            CHECK("Server Hello",
1713
                  [&](auto& result) {
1✔
1714
                     result.require("ctx is available", ctx != nullptr);
1✔
1715
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1716

1717
                     ctx->check_callback_invocations(result,
2✔
1718
                                                     "callbacks after server hello",
1719
                                                     {
1720
                                                        "tls_examine_extensions_server_hello",
1721
                                                        "tls_inspect_handshake_msg_server_hello",
1722
                                                        "tls_ephemeral_key_agreement",
1723
                                                     });
1724
                  }),
1✔
1725

1726
            CHECK("other handshake messages and client auth",
1727
                  [&](Test::Result& result) {
1✔
1728
                     result.require("ctx is available", ctx != nullptr);
1✔
1729
                     ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1730

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

1752
                     const auto raw_pk = ctx->client.peer_raw_public_key();
1✔
1753
                     result.confirm(
2✔
1754
                        "Received server's raw public key",
1755
                        raw_pk && raw_pk->fingerprint_public() == server_raw_public_key_pair()->fingerprint_public());
5✔
1756

1757
                     // ClientFinished contains the entire coalesced client authentication flight
1758
                     // Messages: Certificate, CertificateVerify, Finished
1759
                     result.test_eq(
2✔
1760
                        "Client Auth and Finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1761
                  }),
1✔
1762

1763
            CHECK("Close Connection",
1764
                  [&](Test::Result& result) {
1✔
1765
                     result.require("ctx is available", ctx != nullptr);
1✔
1766
                     ctx->client.close();
1✔
1767
                     result.test_eq(
2✔
1768
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1769

1770
                     ctx->check_callback_invocations(result,
2✔
1771
                                                     "after sending close notify",
1772
                                                     {
1773
                                                        "tls_emit_data",
1774
                                                     });
1775

1776
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1777
                     result.confirm("connection closed", ctx->client.is_closed());
2✔
1778

1779
                     ctx->check_callback_invocations(
2✔
1780
                        result, "after receiving close notify", {"tls_alert", "tls_peer_closed_connection"});
1781
                  }),
1✔
1782
         };
5✔
1783
      }
2✔
1784
};
1785

1786
class Test_TLS_RFC8448_Server : public Test_TLS_RFC8448 {
1✔
1787
   private:
1788
      std::string side() const override { return "Server"; }
7✔
1789

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

1793
         // 32 - for server hello random
1794
         // 32 - for KeyShare (eph. x25519 key pair)  --  I guess?
1795
         //  4 - for ticket_age_add (in New Session Ticket)
1796
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1797

1798
         std::unique_ptr<Server_Context> ctx;
1✔
1799

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

1812
                     ctx = std::make_unique<Server_Context>(
1✔
1813
                        std::move(rng),
1814
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1815
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1816
                        add_early_data_and_sort,
1817
                        make_mock_signatures(vars),
1✔
1818
                        false,
2✔
1819
                        std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1820
                                  Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1821
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
1822

1823
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1824

1825
                     ctx->check_callback_invocations(result,
2✔
1826
                                                     "client hello received",
1827
                                                     {"tls_emit_data",
1828
                                                      "tls_examine_extensions_client_hello",
1829
                                                      "tls_modify_extensions_server_hello",
1830
                                                      "tls_modify_extensions_encrypted_extensions",
1831
                                                      "tls_modify_extensions_certificate",
1832
                                                      "tls_sign_message",
1833
                                                      "tls_generate_ephemeral_key",
1834
                                                      "tls_ephemeral_key_agreement",
1835
                                                      "tls_inspect_handshake_msg_client_hello",
1836
                                                      "tls_inspect_handshake_msg_server_hello",
1837
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
1838
                                                      "tls_inspect_handshake_msg_certificate",
1839
                                                      "tls_inspect_handshake_msg_certificate_verify",
1840
                                                      "tls_inspect_handshake_msg_finished"});
1841
                  }),
1✔
1842

1843
            CHECK("Verify generated messages in server's first flight",
1844
                  [&](Test::Result& result) {
1✔
1845
                     result.require("ctx is available", ctx != nullptr);
1✔
1846
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
1847

1848
                     result.test_eq("Server Hello",
3✔
1849
                                    msgs.at("server_hello")[0],
2✔
1850
                                    strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
1851
                     result.test_eq("Encrypted Extensions",
3✔
1852
                                    msgs.at("encrypted_extensions")[0],
2✔
1853
                                    strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1854
                     result.test_eq("Certificate",
3✔
1855
                                    msgs.at("certificate")[0],
2✔
1856
                                    strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
1857
                     result.test_eq("CertificateVerify",
3✔
1858
                                    msgs.at("certificate_verify")[0],
2✔
1859
                                    strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
1860

1861
                     result.test_eq("Server's entire first flight",
2✔
1862
                                    ctx->pull_send_buffer(),
2✔
1863
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
1864
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1865

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

1875
            CHECK("Send Client Finished",
1876
                  [&](Test::Result& result) {
1✔
1877
                     result.require("ctx is available", ctx != nullptr);
1✔
1878
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
1879

1880
                     ctx->check_callback_invocations(result,
2✔
1881
                                                     "client finished received",
1882
                                                     {"tls_inspect_handshake_msg_finished",
1883
                                                      "tls_current_timestamp",
1884
                                                      "tls_session_established",
1885
                                                      "tls_session_activated"});
1886
                  }),
1✔
1887

1888
            CHECK("Send Session Ticket",
1889
                  [&](Test::Result& result) {
1✔
1890
                     result.require("ctx is available", ctx != nullptr);
1✔
1891
                     const auto new_tickets = ctx->server.send_new_session_tickets(1);
1✔
1892

1893
                     result.test_eq("session ticket was sent", new_tickets, 1);
1✔
1894

1895
                     ctx->check_callback_invocations(result,
2✔
1896
                                                     "issued new session ticket",
1897
                                                     {"tls_inspect_handshake_msg_new_session_ticket",
1898
                                                      "tls_current_timestamp",
1899
                                                      "tls_emit_data",
1900
                                                      "tls_modify_extensions_new_session_ticket",
1901
                                                      "tls_should_persist_resumption_information"});
1902
                  }),
1✔
1903

1904
            CHECK("Verify generated new session ticket message",
1905
                  [&](Test::Result& result) {
1✔
1906
                     result.require("ctx is available", ctx != nullptr);
1✔
1907
                     result.test_eq(
2✔
1908
                        "New Session Ticket", ctx->pull_send_buffer(), vars.get_req_bin("Record_NewSessionTicket"));
3✔
1909
                  }),
1✔
1910

1911
            CHECK("Receive Application Data",
1912
                  [&](Test::Result& result) {
1✔
1913
                     result.require("ctx is available", ctx != nullptr);
1✔
1914
                     ctx->server.received_data(vars.get_req_bin("Record_Client_AppData"));
2✔
1915
                     ctx->check_callback_invocations(result, "application data received", {"tls_record_received"});
2✔
1916

1917
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
1918
                     result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Client_AppData"));
3✔
1919
                     result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
2✔
1920
                  }),
1✔
1921

1922
            CHECK("Send Application Data",
1923
                  [&](Test::Result& result) {
1✔
1924
                     result.require("ctx is available", ctx != nullptr);
1✔
1925
                     ctx->send(vars.get_req_bin("Server_AppData"));
3✔
1926

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

1929
                     result.test_eq("correct server application data",
2✔
1930
                                    ctx->pull_send_buffer(),
2✔
1931
                                    vars.get_req_bin("Record_Server_AppData"));
2✔
1932
                  }),
1✔
1933

1934
            CHECK("Receive Client's close_notify",
1935
                  [&](Test::Result& result) {
1✔
1936
                     result.require("ctx is available", ctx != nullptr);
1✔
1937
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
1938

1939
                     ctx->check_callback_invocations(
2✔
1940
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1941

1942
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
1943
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
1944
                     result.confirm("handshake is still finished", ctx->server.is_handshake_complete());
2✔
1945
                  }),
1✔
1946

1947
            CHECK("Expect Server close_notify",
1948
                  [&](Test::Result& result) {
1✔
1949
                     result.require("ctx is available", ctx != nullptr);
1✔
1950
                     ctx->server.close();
1✔
1951

1952
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
1953
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
1954
                     result.confirm("handshake is still finished", ctx->server.is_handshake_complete());
2✔
1955
                     result.test_eq("Server's close notify",
2✔
1956
                                    ctx->pull_send_buffer(),
2✔
1957
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1958
                  }),
1✔
1959
         };
10✔
1960
      }
2✔
1961

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

1965
         // 32 - for server hello random
1966
         // 32 - for KeyShare (eph. x25519 key pair)
1967
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1968

1969
         std::unique_ptr<Server_Context> ctx;
1✔
1970

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

1983
                     ctx = std::make_unique<Server_Context>(
1✔
1984
                        std::move(rng),
1985
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
2✔
1986
                        vars.get_req_u64("CurrentTimestamp"),
1✔
1987
                        add_cookie_and_sort,
1988
                        make_mock_signatures(vars),
1✔
1989
                        false,
2✔
1990
                        std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1991
                                  Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1992
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
1993

1994
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1995

1996
                     ctx->check_callback_invocations(result,
2✔
1997
                                                     "client hello received",
1998
                                                     {
1999
                                                        "tls_emit_data",
2000
                                                        "tls_current_timestamp",
2001
                                                        "tls_generate_ephemeral_key",
2002
                                                        "tls_ephemeral_key_agreement",
2003
                                                        "tls_examine_extensions_client_hello",
2004
                                                        "tls_modify_extensions_server_hello",
2005
                                                        "tls_modify_extensions_encrypted_extensions",
2006
                                                        "tls_inspect_handshake_msg_client_hello",
2007
                                                        "tls_inspect_handshake_msg_server_hello",
2008
                                                        "tls_inspect_handshake_msg_encrypted_extensions",
2009
                                                        "tls_inspect_handshake_msg_finished",
2010
                                                     });
2011
                  }),
1✔
2012

2013
            CHECK("Verify generated messages in server's first flight",
2014
                  [&](Test::Result& result) {
1✔
2015
                     result.require("ctx is available", ctx != nullptr);
1✔
2016
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2017

2018
                     result.test_eq("Server Hello",
3✔
2019
                                    msgs.at("server_hello")[0],
2✔
2020
                                    strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2021
                     result.test_eq("Encrypted Extensions",
3✔
2022
                                    msgs.at("encrypted_extensions")[0],
2✔
2023
                                    strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2024

2025
                     result.test_eq("Server's entire first flight",
2✔
2026
                                    ctx->pull_send_buffer(),
2✔
2027
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2028
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2029

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

2039
            // TODO: The rest of this test vector requires 0-RTT which is not
2040
            //       yet implemented. For now we can only test the server's
2041
            //       ability to acknowledge a session resumption via PSK.
2042
         };
3✔
2043
      }
2✔
2044

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

2050
         // 32 - for server hello random
2051
         // 32 - for KeyShare (eph. P-256 key pair)
2052
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2053

2054
         std::unique_ptr<Server_Context> ctx;
1✔
2055

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

2070
                     ctx = std::make_unique<Server_Context>(std::move(rng),
1✔
2071
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_server"),
2✔
2072
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
2073
                                                            add_cookie_and_sort,
2074
                                                            make_mock_signatures(vars));
3✔
2075
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
2076

2077
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2078

2079
                     ctx->check_callback_invocations(result,
2✔
2080
                                                     "client hello received",
2081
                                                     {"tls_emit_data",
2082
                                                      "tls_examine_extensions_client_hello",
2083
                                                      "tls_modify_extensions_hello_retry_request",
2084
                                                      "tls_inspect_handshake_msg_client_hello",
2085
                                                      "tls_inspect_handshake_msg_hello_retry_request"});
2086
                  }),
1✔
2087

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

2098
            CHECK("Receive updated Client Hello message",
2099
                  [&](Test::Result& result) {
1✔
2100
                     result.require("ctx is available", ctx != nullptr);
1✔
2101
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_2"));
2✔
2102

2103
                     ctx->check_callback_invocations(result,
2✔
2104
                                                     "updated client hello received",
2105
                                                     {"tls_emit_data",
2106
                                                      "tls_examine_extensions_client_hello",
2107
                                                      "tls_modify_extensions_server_hello",
2108
                                                      "tls_modify_extensions_encrypted_extensions",
2109
                                                      "tls_modify_extensions_certificate",
2110
                                                      "tls_sign_message",
2111
                                                      "tls_generate_ephemeral_key",
2112
                                                      "tls_ephemeral_key_agreement",
2113
                                                      "tls_inspect_handshake_msg_client_hello",
2114
                                                      "tls_inspect_handshake_msg_server_hello",
2115
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2116
                                                      "tls_inspect_handshake_msg_certificate",
2117
                                                      "tls_inspect_handshake_msg_certificate_verify",
2118
                                                      "tls_inspect_handshake_msg_finished"});
2119
                  }),
1✔
2120

2121
            CHECK("Verify generated messages in server's second flight",
2122
                  [&](Test::Result& result) {
1✔
2123
                     result.require("ctx is available", ctx != nullptr);
1✔
2124
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2125

2126
                     result.test_eq("Server Hello",
3✔
2127
                                    msgs.at("server_hello")[0],
2✔
2128
                                    strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2129
                     result.test_eq("Encrypted Extensions",
3✔
2130
                                    msgs.at("encrypted_extensions")[0],
2✔
2131
                                    strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2132
                     result.test_eq("Certificate",
3✔
2133
                                    msgs.at("certificate")[0],
2✔
2134
                                    strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2135
                     result.test_eq("CertificateVerify",
3✔
2136
                                    msgs.at("certificate_verify")[0],
2✔
2137
                                    strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2138
                     result.test_eq("Finished",
3✔
2139
                                    msgs.at("finished")[0],
2✔
2140
                                    strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2141

2142
                     result.test_eq("Server's entire second flight",
2✔
2143
                                    ctx->pull_send_buffer(),
2✔
2144
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2145
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2146
                     result.confirm("Server could now send application data", ctx->server.is_active());
2✔
2147
                     result.confirm("handshake is not yet complete",
1✔
2148
                                    !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2149
                  }),
1✔
2150

2151
            CHECK("Receive Client Finished",
2152
                  [&](Test::Result& result) {
1✔
2153
                     result.require("ctx is available", ctx != nullptr);
1✔
2154
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2155

2156
                     ctx->check_callback_invocations(result,
2✔
2157
                                                     "client finished received",
2158
                                                     {"tls_inspect_handshake_msg_finished",
2159
                                                      "tls_current_timestamp",
2160
                                                      "tls_session_established",
2161
                                                      "tls_session_activated"});
2162

2163
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2164
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2165
                  }),
1✔
2166

2167
            CHECK("Receive Client close_notify",
2168
                  [&](Test::Result& result) {
1✔
2169
                     result.require("ctx is available", ctx != nullptr);
1✔
2170
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2171

2172
                     ctx->check_callback_invocations(
2✔
2173
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2174

2175
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2176
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2177
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2178
                  }),
1✔
2179

2180
            CHECK("Expect Server close_notify",
2181
                  [&](Test::Result& result) {
1✔
2182
                     result.require("ctx is available", ctx != nullptr);
1✔
2183
                     ctx->server.close();
1✔
2184

2185
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2186
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2187
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2188
                     result.test_eq("Server's close notify",
2✔
2189
                                    ctx->pull_send_buffer(),
2✔
2190
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2191
                  }),
1✔
2192

2193
         };
8✔
2194
      }
2✔
2195

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

2199
         // 32 - for server hello random
2200
         // 32 - for KeyShare (eph. x25519 pair)
2201
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2202

2203
         std::unique_ptr<Server_Context> ctx;
1✔
2204

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

2217
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2218

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

2239
            CHECK("Verify server's generated handshake messages",
2240
                  [&](Test::Result& result) {
1✔
2241
                     result.require("ctx is available", ctx != nullptr);
1✔
2242
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2243

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

2263
                     result.test_eq("Server's entire first flight",
2✔
2264
                                    ctx->pull_send_buffer(),
2✔
2265
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2266
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2267

2268
                     result.confirm("Not yet aware of client's cert chain", ctx->server.peer_cert_chain().empty());
2✔
2269
                     result.confirm("Server could now send application data", ctx->server.is_active());
2✔
2270
                     result.confirm("handshake is not yet complete",
1✔
2271
                                    !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2272
                  }),
1✔
2273

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

2283
                     ctx->check_callback_invocations(result,
2✔
2284
                                                     "client finished received",
2285
                                                     {"tls_inspect_handshake_msg_certificate",
2286
                                                      "tls_inspect_handshake_msg_certificate_verify",
2287
                                                      "tls_inspect_handshake_msg_finished",
2288
                                                      "tls_examine_extensions_certificate",
2289
                                                      "tls_verify_cert_chain",
2290
                                                      "tls_verify_message",
2291
                                                      "tls_current_timestamp",
2292
                                                      "tls_session_established",
2293
                                                      "tls_session_activated"});
2294

2295
                     const auto cert_chain = ctx->server.peer_cert_chain();
1✔
2296
                     result.confirm("Received client's cert chain",
2✔
2297
                                    !cert_chain.empty() && cert_chain.front() == client_certificate());
2✔
2298

2299
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2300
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2301
                  }),
1✔
2302

2303
            CHECK("Receive Client close_notify",
2304
                  [&](Test::Result& result) {
1✔
2305
                     result.require("ctx is available", ctx != nullptr);
1✔
2306
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2307

2308
                     ctx->check_callback_invocations(
2✔
2309
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2310

2311
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2312
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2313
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2314
                  }),
1✔
2315

2316
            CHECK("Expect Server close_notify",
2317
                  [&](Test::Result& result) {
1✔
2318
                     result.require("ctx is available", ctx != nullptr);
1✔
2319
                     ctx->server.close();
1✔
2320

2321
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2322
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2323
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2324
                     result.test_eq("Server's close notify",
2✔
2325
                                    ctx->pull_send_buffer(),
2✔
2326
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2327
                  }),
1✔
2328

2329
         };
6✔
2330
      }
2✔
2331

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

2335
         // 32 - for server hello random
2336
         // 32 - for KeyShare (eph. x25519 pair)
2337
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2338

2339
         std::unique_ptr<Server_Context> ctx;
1✔
2340

2341
         return {
1✔
2342
            CHECK("Receive Client Hello",
2343
                  [&](Test::Result& result) {
1✔
2344
                     ctx =
1✔
2345
                        std::make_unique<Server_Context>(std::move(rng),
1✔
2346
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_server"),
2✔
2347
                                                         vars.get_req_u64("CurrentTimestamp"),
1✔
2348
                                                         sort_rfc8448_extensions,
2349
                                                         make_mock_signatures(vars));
3✔
2350
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
2351

2352
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2353

2354
                     ctx->check_callback_invocations(result,
2✔
2355
                                                     "client hello received",
2356
                                                     {"tls_emit_data",
2357
                                                      "tls_examine_extensions_client_hello",
2358
                                                      "tls_modify_extensions_server_hello",
2359
                                                      "tls_modify_extensions_encrypted_extensions",
2360
                                                      "tls_modify_extensions_certificate",
2361
                                                      "tls_sign_message",
2362
                                                      "tls_generate_ephemeral_key",
2363
                                                      "tls_ephemeral_key_agreement",
2364
                                                      "tls_inspect_handshake_msg_client_hello",
2365
                                                      "tls_inspect_handshake_msg_server_hello",
2366
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2367
                                                      "tls_inspect_handshake_msg_certificate",
2368
                                                      "tls_inspect_handshake_msg_certificate_verify",
2369
                                                      "tls_inspect_handshake_msg_finished"});
2370
                  }),
1✔
2371

2372
            CHECK("Verify server's generated handshake messages",
2373
                  [&](Test::Result& result) {
1✔
2374
                     result.require("ctx is available", ctx != nullptr);
1✔
2375
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2376

2377
                     result.test_eq("Server Hello",
3✔
2378
                                    msgs.at("server_hello")[0],
2✔
2379
                                    strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2380
                     result.test_eq("Encrypted Extensions",
3✔
2381
                                    msgs.at("encrypted_extensions")[0],
2✔
2382
                                    strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2383
                     result.test_eq("Certificate",
3✔
2384
                                    msgs.at("certificate")[0],
2✔
2385
                                    strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2386
                     result.test_eq("CertificateVerify",
3✔
2387
                                    msgs.at("certificate_verify")[0],
2✔
2388
                                    strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2389
                     result.test_eq("Finished",
3✔
2390
                                    msgs.at("finished")[0],
2✔
2391
                                    strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2392

2393
                     // Those records contain the required Change Cipher Spec message the server must produce for compatibility mode compliance
2394
                     result.test_eq("Server's entire first flight",
2✔
2395
                                    ctx->pull_send_buffer(),
2✔
2396
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2397
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2398

2399
                     result.confirm("Server could now send application data", ctx->server.is_active());
2✔
2400
                     result.confirm("handshake is not yet complete",
1✔
2401
                                    !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2402
                  }),
1✔
2403

2404
            CHECK("Receive Client Finished",
2405
                  [&](Test::Result& result) {
1✔
2406
                     result.require("ctx is available", ctx != nullptr);
1✔
2407
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2408

2409
                     ctx->check_callback_invocations(result,
2✔
2410
                                                     "client finished received",
2411
                                                     {"tls_inspect_handshake_msg_finished",
2412
                                                      "tls_current_timestamp",
2413
                                                      "tls_session_established",
2414
                                                      "tls_session_activated"});
2415

2416
                     result.confirm("TLS handshake fully finished", ctx->server.is_active());
2✔
2417
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2418
                  }),
1✔
2419

2420
            CHECK("Receive Client close_notify",
2421
                  [&](Test::Result& result) {
1✔
2422
                     result.require("ctx is available", ctx != nullptr);
1✔
2423
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2424

2425
                     ctx->check_callback_invocations(
2✔
2426
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2427

2428
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2429
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2430
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2431
                  }),
1✔
2432

2433
            CHECK("Expect Server close_notify",
2434
                  [&](Test::Result& result) {
1✔
2435
                     result.require("ctx is available", ctx != nullptr);
1✔
2436
                     ctx->server.close();
1✔
2437

2438
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2439
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2440
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2441
                     result.test_eq("Server's close notify",
2✔
2442
                                    ctx->pull_send_buffer(),
2✔
2443
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2444
                  }),
1✔
2445

2446
         };
6✔
2447
      }
2✔
2448

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

2452
         // 32 - for server hello random
2453
         // 32 - for KeyShare (eph. x25519 key pair)
2454
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2455

2456
         std::unique_ptr<Server_Context> ctx;
1✔
2457

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

2484
                     ctx = std::make_unique<Server_Context>(
1✔
2485
                        std::move(rng),
2486
                        std::make_shared<RFC8448_Text_Policy>("rfc8448_psk_dhe", false /* no rfc8448 */),
2✔
2487
                        vars.get_req_u64("CurrentTimestamp"),
1✔
2488
                        sort_our_extensions,
2489
                        make_mock_signatures(vars),
1✔
2490
                        false,
1✔
2491
                        std::nullopt,
2492
                        ExternalPSK(vars.get_req_str("PskIdentity"),
2✔
2493
                                    vars.get_req_str("PskPRF"),
2✔
2494
                                    lock(vars.get_req_bin("PskSecret"))));
5✔
2495
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
2496

2497
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2498

2499
                     ctx->check_callback_invocations(result,
2✔
2500
                                                     "client hello received",
2501
                                                     {"tls_emit_data",
2502
                                                      "tls_examine_extensions_client_hello",
2503
                                                      "tls_modify_extensions_server_hello",
2504
                                                      "tls_modify_extensions_encrypted_extensions",
2505
                                                      "tls_generate_ephemeral_key",
2506
                                                      "tls_ephemeral_key_agreement",
2507
                                                      "tls_inspect_handshake_msg_client_hello",
2508
                                                      "tls_inspect_handshake_msg_server_hello",
2509
                                                      "tls_inspect_handshake_msg_encrypted_extensions",
2510
                                                      "tls_inspect_handshake_msg_finished"});
2511
                  }),
1✔
2512

2513
            CHECK("Verify generated messages in server's first flight",
2514
                  [&](Test::Result& result) {
1✔
2515
                     result.require("ctx is available", ctx != nullptr);
1✔
2516
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2517

2518
                     result.test_eq("Server Hello",
3✔
2519
                                    msgs.at("server_hello")[0],
2✔
2520
                                    strip_message_header(vars.get_opt_bin("Message_ServerHello")));
3✔
2521
                     result.test_eq("Encrypted Extensions",
3✔
2522
                                    msgs.at("encrypted_extensions")[0],
2✔
2523
                                    strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2524
                     result.test_eq("Server Finished",
3✔
2525
                                    msgs.at("finished")[0],
2✔
2526
                                    strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2527

2528
                     result.test_eq("Server's entire first flight",
2✔
2529
                                    ctx->pull_send_buffer(),
2✔
2530
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2531
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2532

2533
                     result.confirm("Server can now send application data", ctx->server.is_active());
2✔
2534
                     result.confirm("handshake is not yet complete",
1✔
2535
                                    !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2536
                  }),
1✔
2537

2538
            CHECK("Send Client Finished",
2539
                  [&](Test::Result& result) {
1✔
2540
                     result.require("ctx is available", ctx != nullptr);
1✔
2541
                     ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2542
                     result.require("PSK negotiated",
2✔
2543
                                    ctx->psk_identity_negotiated() == vars.get_req_str("PskIdentity"));
2✔
2544

2545
                     ctx->check_callback_invocations(result,
2✔
2546
                                                     "client finished received",
2547
                                                     {"tls_inspect_handshake_msg_finished",
2548
                                                      "tls_current_timestamp",
2549
                                                      "tls_session_established",
2550
                                                      "tls_session_activated"});
2551
                  }),
1✔
2552

2553
            CHECK("Exchange Application Data",
2554
                  [&](Test::Result& result) {
1✔
2555
                     result.require("ctx is available", ctx != nullptr);
1✔
2556
                     ctx->server.received_data(vars.get_req_bin("Record_Client_AppData"));
2✔
2557
                     ctx->check_callback_invocations(result, "application data received", {"tls_record_received"});
2✔
2558

2559
                     const auto rcvd = ctx->pull_receive_buffer();
1✔
2560
                     result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Client_AppData"));
3✔
2561
                     result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
1✔
2562

2563
                     ctx->send(vars.get_req_bin("Server_AppData"));
3✔
2564
                     ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
2✔
2565
                     result.test_eq("correct server application data",
2✔
2566
                                    ctx->pull_send_buffer(),
2✔
2567
                                    vars.get_req_bin("Record_Server_AppData"));
2✔
2568
                  }),
1✔
2569

2570
            CHECK("Terminate Connection",
2571
                  [&](Test::Result& result) {
1✔
2572
                     result.require("ctx is available", ctx != nullptr);
1✔
2573
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2574

2575
                     ctx->check_callback_invocations(
2✔
2576
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2577

2578
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2579
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2580
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2581

2582
                     ctx->server.close();
1✔
2583

2584
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2585
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2586
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2587
                     result.test_eq("Server's close notify",
2✔
2588
                                    ctx->pull_send_buffer(),
2✔
2589
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2590
                  }),
1✔
2591
         };
6✔
2592
      }
2✔
2593

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

2597
         // 32 - for server hello random
2598
         // 32 - for KeyShare (eph. x25519 key pair)
2599
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
2600

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

2625
         std::unique_ptr<Server_Context> ctx;
1✔
2626

2627
         return {
1✔
2628
            CHECK("Receive Client Hello",
2629
                  [&](Test::Result& result) {
1✔
2630
                     ctx = std::make_unique<Server_Context>(std::move(rng),
1✔
2631
                                                            std::make_shared<RFC8448_Text_Policy>("rfc8448_rawpubkey"),
2✔
2632
                                                            vars.get_req_u64("CurrentTimestamp"),
1✔
2633
                                                            sort_our_extensions,
2634
                                                            make_mock_signatures(vars));
3✔
2635
                     result.confirm("server not closed", !ctx->server.is_closed());
2✔
2636

2637
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2638

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

2659
            CHECK("Verify server's generated handshake messages",
2660
                  [&](Test::Result& result) {
1✔
2661
                     result.require("ctx is available", ctx != nullptr);
1✔
2662
                     const auto& msgs = ctx->observed_handshake_messages();
1✔
2663

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

2683
                     result.test_eq("Server's entire first flight",
2✔
2684
                                    ctx->pull_send_buffer(),
2✔
2685
                                    concat(vars.get_req_bin("Record_ServerHello"),
4✔
2686
                                           vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2687

2688
                     result.confirm("Not yet aware of client's cert chain", ctx->server.peer_cert_chain().empty());
2✔
2689
                     result.confirm("Server could now send application data", ctx->server.is_active());
2✔
2690
                     result.confirm("handshake is not yet complete",
1✔
2691
                                    !ctx->server.is_handshake_complete());  // See RFC 8446 4.4.4
1✔
2692
                  }),
1✔
2693

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

2703
                     ctx->check_callback_invocations(result,
2✔
2704
                                                     "client finished received",
2705
                                                     {"tls_inspect_handshake_msg_certificate",
2706
                                                      "tls_inspect_handshake_msg_certificate_verify",
2707
                                                      "tls_inspect_handshake_msg_finished",
2708
                                                      "tls_examine_extensions_certificate",
2709
                                                      "tls_verify_raw_public_key",
2710
                                                      "tls_verify_message",
2711
                                                      "tls_current_timestamp",
2712
                                                      "tls_session_established",
2713
                                                      "tls_session_activated"});
2714

2715
                     const auto raw_pk = ctx->server.peer_raw_public_key();
1✔
2716
                     result.confirm(
2✔
2717
                        "Received client's raw public key",
2718
                        raw_pk && raw_pk->fingerprint_public() == client_raw_public_key_pair()->fingerprint_public());
5✔
2719

2720
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2721
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2722
                  }),
1✔
2723

2724
            CHECK("Receive Client close_notify",
2725
                  [&](Test::Result& result) {
1✔
2726
                     result.require("ctx is available", ctx != nullptr);
1✔
2727
                     ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2728

2729
                     ctx->check_callback_invocations(
2✔
2730
                        result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2731

2732
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2733
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2734
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2735
                  }),
1✔
2736

2737
            CHECK("Expect Server close_notify",
2738
                  [&](Test::Result& result) {
1✔
2739
                     result.require("ctx is available", ctx != nullptr);
1✔
2740
                     ctx->server.close();
1✔
2741

2742
                     result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2743
                     result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2744
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2745
                     result.test_eq("Server's close notify",
2✔
2746
                                    ctx->pull_send_buffer(),
2✔
2747
                                    vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2748
                  }),
1✔
2749
         };
6✔
2750
      }
2✔
2751
};
2752

2753
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2754
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2755

2756
#endif
2757

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