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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 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_policy.h>
37
   #include <botan/tls_server.h>
38
   #include <botan/tls_session_manager.h>
39
   #include <botan/x509_key.h>
40
   #include <botan/internal/fmt.h>
41
   #include <botan/internal/stl_util.h>
42
#endif
43

44
namespace Botan_Tests {
45

46
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
47

48
namespace {
49

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

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

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

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

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

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

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

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

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

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

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

111
   private:
112
      size_t m_padding_bytes;
113
};
114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

426
         return m_server_private_key;
3✔
427
      }
428

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

540
   private:
541
      bool m_rfc8448;
542
};
543

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

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

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

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

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

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

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

590
         return handle;
1✔
591
      }
592

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

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

611
         return found_sessions;
7✔
612
      }
×
613

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

847
   return result;
9✔
848
}
×
849

850
}  // namespace
851

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1143
               add_renegotiation_extension(exts);
1✔
1144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1358
                     result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1359
                  }),
1✔
1360

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

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

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

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

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

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

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

1420
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1421
                     result.confirm("connection closed", ctx->client.is_closed());
2✔
1422

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

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

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

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

1446
         std::unique_ptr<Client_Context> ctx;
1✔
1447

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

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

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

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

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

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

1503
                     result.confirm("client is ready to send application traffic", ctx->client.is_active());
2✔
1504
                     result.confirm("handshake is complete", ctx->client.is_handshake_complete());
2✔
1505
                  }),
1✔
1506

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

1512
                     result.test_eq(
2✔
1513
                        "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1514

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

1519
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1520

1521
                     result.confirm("client connection was terminated", ctx->client.is_closed());
2✔
1522
                  }),
1✔
1523
         };
4✔
1524
      }
2✔
1525

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

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

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

1552
         std::unique_ptr<Client_Context> ctx;
1✔
1553

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

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

1578
                     result.test_eq(
2✔
1579
                        "TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1580
                  }),
1✔
1581

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

1593
                     result.confirm("client is not yet active", !ctx->client.is_active());
2✔
1594
                     result.confirm("handshake is not yet complete", !ctx->client.is_handshake_complete());
2✔
1595
                  }),
1✔
1596

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

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

1616
                  result.test_eq(
2✔
1617
                     "correct handshake finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
1618
               }),
1✔
1619

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

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

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

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

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

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

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

1649
                     result.test_eq(
2✔
1650
                        "close payload", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1651
                     ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
2✔
1652

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

1657
                     result.confirm("connection is closed", ctx->client.is_closed());
2✔
1658
                  }),
1✔
1659
         };
7✔
1660
      }
2✔
1661

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

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

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

1690
         std::unique_ptr<Client_Context> ctx;
1✔
1691

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

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

1713
                     result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1714
                  }),
1✔
1715

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

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

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

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

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

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

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

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

1780
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1781
                     result.confirm("connection closed", ctx->client.is_closed());
2✔
1782

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

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

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

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

1802
         std::unique_ptr<Server_Context> ctx;
1✔
1803

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

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

1827
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1828

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

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

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

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

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

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

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

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

1897
                     result.test_eq("session ticket was sent", new_tickets, 1);
1✔
1898

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1973
         std::unique_ptr<Server_Context> ctx;
1✔
1974

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

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

1998
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1999

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

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

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

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

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

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

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

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

2058
         std::unique_ptr<Server_Context> ctx;
1✔
2059

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

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

2081
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2082

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

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

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

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

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

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

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

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

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

2167
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2168
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2169
                  }),
1✔
2170

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

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

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

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

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

2197
         };
8✔
2198
      }
2✔
2199

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

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

2207
         std::unique_ptr<Server_Context> ctx;
1✔
2208

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

2221
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2222

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

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

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

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

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

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

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

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

2303
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2304
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2305
                  }),
1✔
2306

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

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

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

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

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

2333
         };
6✔
2334
      }
2✔
2335

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

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

2343
         std::unique_ptr<Server_Context> ctx;
1✔
2344

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

2356
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2357

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

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

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

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

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

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

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

2420
                     result.confirm("TLS handshake fully finished", ctx->server.is_active());
2✔
2421
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2422
                  }),
1✔
2423

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

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

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

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

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

2450
         };
6✔
2451
      }
2✔
2452

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

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

2460
         std::unique_ptr<Server_Context> ctx;
1✔
2461

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

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

2501
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2502

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

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

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

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

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

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

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

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

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

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

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

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

2582
                     result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2583
                     result.confirm("connection is still active", ctx->server.is_active());
2✔
2584
                     result.confirm("handshake is still complete", ctx->server.is_handshake_complete());
2✔
2585

2586
                     ctx->server.close();
1✔
2587

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

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

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

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

2629
         std::unique_ptr<Server_Context> ctx;
1✔
2630

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

2641
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
2642

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

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

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

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

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

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

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

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

2724
                     result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2725
                     result.confirm("handshake is complete", ctx->server.is_handshake_complete());
2✔
2726
                  }),
1✔
2727

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

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

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

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

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

2757
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2758
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2759

2760
#endif
2761

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