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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

96.63
/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_CURVE_25519) && defined(BOTAN_HAS_SHA2_32) &&                 \
18
   defined(BOTAN_HAS_SHA2_64) && defined(BOTAN_HAS_ECDSA)
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/credentials_manager.h>
26
   #include <botan/ecdsa.h>
27
   #include <botan/hash.h>
28
   #include <botan/rsa.h>
29
   #include <botan/tls_alert.h>
30
   #include <botan/tls_callbacks.h>
31
   #include <botan/tls_client.h>
32
   #include <botan/tls_messages.h>
33
   #include <botan/tls_policy.h>
34
   #include <botan/tls_server.h>
35
   #include <botan/tls_server_info.h>
36
   #include <botan/tls_session.h>
37
   #include <botan/tls_session_manager.h>
38
   #include <botan/tls_version.h>
39
   #include <botan/internal/tls_reader.h>
40

41
   #include <botan/assert.h>
42
   #include <botan/internal/stl_util.h>
43

44
   #include <botan/internal/loadstor.h>
45

46
   #include <botan/data_src.h>
47
   #include <botan/pk_algs.h>
48
   #include <botan/pkcs8.h>
49
#endif
50

51
namespace Botan_Tests {
52

53
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
54

55
namespace {
56

57
void add_entropy(Botan_Tests::Fixed_Output_RNG& rng, const std::vector<uint8_t>& bin) {
10✔
58
   rng.add_entropy(bin.data(), bin.size());
20✔
59
}
60

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

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

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

85
/**
86
* Simple version of the Padding extension (RFC 7685) to reproduce the
87
* 2nd Client_Hello in RFC8448 Section 5 (HelloRetryRequest)
88
*/
89
class Padding final : public Botan::TLS::Extension {
90
   public:
91
      static Botan::TLS::Extension_Code static_type() { return Botan::TLS::Extension_Code(21); }
92

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

95
      explicit Padding(const size_t padding_bytes) : m_padding_bytes(padding_bytes) {}
2✔
96

97
      std::vector<uint8_t> serialize(Botan::TLS::Connection_Side) const override {
5✔
98
         return std::vector<uint8_t>(m_padding_bytes, 0x00);
5✔
99
      }
100

101
      bool empty() const override { return m_padding_bytes == 0; }
5✔
102

103
   private:
104
      size_t m_padding_bytes;
105
};
106

107
using namespace Botan;
108
using namespace Botan::TLS;
109

110
std::chrono::system_clock::time_point from_milliseconds_since_epoch(uint64_t msecs) {
10✔
111
   const int64_t secs_since_epoch = msecs / 1000;
10✔
112
   const uint32_t additional_millis = msecs % 1000;
10✔
113

114
   BOTAN_ASSERT_NOMSG(secs_since_epoch <= std::numeric_limits<time_t>::max());
10✔
115
   return std::chrono::system_clock::from_time_t(static_cast<time_t>(secs_since_epoch)) +
10✔
116
          std::chrono::milliseconds(additional_millis);
10✔
117
}
118

119
using Modify_Exts_Fn =
120
   std::function<void(Botan::TLS::Extensions&, Botan::TLS::Connection_Side, Botan::TLS::Handshake_Type)>;
121

122
/**
123
 * We cannot actually reproduce the signatures stated in RFC 8448 as their
124
 * signature scheme is probabilistic and we're lacking the correct RNG
125
 * input. Hence, signatures are know beforehand and just reproduced by the
126
 * TLS callback when requested.
127
 */
128
struct MockSignature {
9✔
129
      std::vector<uint8_t> message_to_sign;
130
      std::vector<uint8_t> signature_to_produce;
131
};
132

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

149
      void tls_emit_data(std::span<const uint8_t> data) override {
33✔
150
         count_callback_invocation("tls_emit_data");
33✔
151
         send_buffer.insert(send_buffer.end(), data.begin(), data.end());
33✔
152
      }
33✔
153

154
      void tls_record_received(uint64_t seq_no, std::span<const uint8_t> data) override {
2✔
155
         count_callback_invocation("tls_record_received");
2✔
156
         received_seq_no = seq_no;
2✔
157
         receive_buffer.insert(receive_buffer.end(), data.begin(), data.end());
2✔
158
      }
2✔
159

160
      void tls_alert(Botan::TLS::Alert alert) override {
8✔
161
         count_callback_invocation("tls_alert");
8✔
162
         BOTAN_UNUSED(alert);
8✔
163
         // handle a tls alert received from the tls server
164
      }
8✔
165

166
      bool tls_peer_closed_connection() override {
8✔
167
         count_callback_invocation("tls_peer_closed_connection");
8✔
168
         // we want to handle the closure ourselves
169
         return false;
8✔
170
      }
171

172
      void tls_session_established(const Botan::TLS::Session_Summary&) override {
8✔
173
         count_callback_invocation("tls_session_established");
8✔
174
      }
8✔
175

176
      void tls_session_activated() override {
8✔
177
         count_callback_invocation("tls_session_activated");
8✔
178
         session_activated_called = true;
8✔
179
      }
8✔
180

181
      bool tls_should_persist_resumption_information(const Session&) override {
2✔
182
         count_callback_invocation("tls_should_persist_resumption_information");
2✔
183
         return true;  // should always store the session
2✔
184
      }
185

186
      void tls_verify_cert_chain(const std::vector<Botan::X509_Certificate>& cert_chain,
5✔
187
                                 const std::vector<std::optional<Botan::OCSP::Response>>&,
188
                                 const std::vector<Botan::Certificate_Store*>&,
189
                                 Botan::Usage_Type,
190
                                 std::string_view,
191
                                 const Botan::TLS::Policy&) override {
192
         count_callback_invocation("tls_verify_cert_chain");
5✔
193
         certificate_chain = cert_chain;
5✔
194
      }
5✔
195

196
      std::chrono::milliseconds tls_verify_cert_chain_ocsp_timeout() const override {
×
197
         count_callback_invocation("tls_verify_cert_chain");
×
198
         return std::chrono::milliseconds(0);
×
199
      }
200

201
      std::vector<uint8_t> tls_provide_cert_status(const std::vector<X509_Certificate>& chain,
×
202
                                                   const Certificate_Status_Request& csr) override {
203
         count_callback_invocation("tls_provide_cert_status");
×
204
         return Callbacks::tls_provide_cert_status(chain, csr);
×
205
      }
206

207
      std::vector<uint8_t> tls_sign_message(const Private_Key& key,
5✔
208
                                            RandomNumberGenerator& rng,
209
                                            std::string_view padding,
210
                                            Signature_Format format,
211
                                            const std::vector<uint8_t>& msg) override {
212
         BOTAN_UNUSED(key, rng);
5✔
213
         count_callback_invocation("tls_sign_message");
5✔
214

215
         if(key.algo_name() == "RSA") {
5✔
216
            if(format != Signature_Format::Standard) {
4✔
217
               throw Test_Error("TLS implementation selected unexpected signature format for RSA");
×
218
            }
219

220
            if(padding != "PSSR(SHA-256,MGF1,32)") {
8✔
221
               throw Test_Error("TLS implementation selected unexpected padding for RSA: " + std::string(padding));
×
222
            }
223
         } else if(key.algo_name() == "ECDSA") {
1✔
224
            if(format != Signature_Format::DerSequence) {
1✔
225
               throw Test_Error("TLS implementation selected unexpected signature format for ECDSA");
×
226
            }
227

228
            if(padding != "SHA-256") {
2✔
229
               throw Test_Error("TLS implementation selected unexpected padding for ECDSA: " + std::string(padding));
×
230
            }
231
         } else {
232
            throw Test_Error("TLS implementation trying to sign with unexpected algorithm (" + key.algo_name() + ")");
×
233
         }
234

235
         for(const auto& mock : m_mock_signatures) {
6✔
236
            if(mock.message_to_sign == msg) {
6✔
237
               return mock.signature_to_produce;
5✔
238
            }
239
         }
240

241
         throw Test_Error("TLS implementation produced an unexpected message to be signed: " + Botan::hex_encode(msg));
×
242
      }
243

244
      bool tls_verify_message(const Public_Key& key,
5✔
245
                              std::string_view padding,
246
                              Signature_Format format,
247
                              const std::vector<uint8_t>& msg,
248
                              const std::vector<uint8_t>& sig) override {
249
         count_callback_invocation("tls_verify_message");
5✔
250
         return Callbacks::tls_verify_message(key, padding, format, msg, sig);
5✔
251
      }
252

253
      std::unique_ptr<PK_Key_Agreement_Key> tls_generate_ephemeral_key(
11✔
254
         const std::variant<TLS::Group_Params, DL_Group>& group, RandomNumberGenerator& rng) override {
255
         count_callback_invocation("tls_generate_ephemeral_key");
11✔
256
         return Callbacks::tls_generate_ephemeral_key(group, rng);
11✔
257
      }
258

259
      secure_vector<uint8_t> tls_ephemeral_key_agreement(const std::variant<TLS::Group_Params, DL_Group>& group,
9✔
260
                                                         const PK_Key_Agreement_Key& private_key,
261
                                                         const std::vector<uint8_t>& public_value,
262
                                                         RandomNumberGenerator& rng,
263
                                                         const Policy& policy) override {
264
         count_callback_invocation("tls_ephemeral_key_agreement");
9✔
265
         return Callbacks::tls_ephemeral_key_agreement(group, private_key, public_value, rng, policy);
9✔
266
      }
267

268
      void tls_inspect_handshake_msg(const Handshake_Message& message) override {
72✔
269
         count_callback_invocation("tls_inspect_handshake_msg_" + message.type_string());
144✔
270

271
         try {
72✔
272
            auto serialized_message = message.serialize();
72✔
273

274
            serialized_messages.try_emplace(message.type_string())
146✔
275
               .first->second.emplace_back(std::move(serialized_message));
72✔
276
         } catch(const Not_Implemented&) {
72✔
277
            // TODO: Once the server implementation is finished, this crutch
278
            //       can likely be removed, as all message types will have a
279
            //       serialization method with actual business logic. :o)
280
         }
×
281

282
         return Callbacks::tls_inspect_handshake_msg(message);
72✔
283
      }
284

285
      std::string tls_server_choose_app_protocol(const std::vector<std::string>& client_protos) override {
×
286
         count_callback_invocation("tls_server_choose_app_protocol");
×
287
         return Callbacks::tls_server_choose_app_protocol(client_protos);
×
288
      }
289

290
      void tls_modify_extensions(Botan::TLS::Extensions& exts,
23✔
291
                                 Botan::TLS::Connection_Side side,
292
                                 Botan::TLS::Handshake_Type which_message) override {
293
         count_callback_invocation(std::string("tls_modify_extensions_") + handshake_type_to_string(which_message));
46✔
294
         m_modify_exts(exts, side, which_message);
23✔
295
         Callbacks::tls_modify_extensions(exts, side, which_message);
23✔
296
      }
23✔
297

298
      void tls_examine_extensions(const Botan::TLS::Extensions& extn,
22✔
299
                                  Connection_Side which_side,
300
                                  Botan::TLS::Handshake_Type which_message) override {
301
         count_callback_invocation(std::string("tls_examine_extensions_") + handshake_type_to_string(which_message));
44✔
302
         return Callbacks::tls_examine_extensions(extn, which_side, which_message);
22✔
303
      }
304

305
      std::string tls_peer_network_identity() override {
×
306
         count_callback_invocation("tls_peer_network_identity");
×
307
         return Callbacks::tls_peer_network_identity();
×
308
      }
309

310
      std::chrono::system_clock::time_point tls_current_timestamp() override {
17✔
311
         count_callback_invocation("tls_current_timestamp");
17✔
312
         return m_timestamp;
17✔
313
      }
314

315
      std::vector<uint8_t> pull_send_buffer() { return std::exchange(send_buffer, std::vector<uint8_t>()); }
27✔
316

317
      std::vector<uint8_t> pull_receive_buffer() { return std::exchange(receive_buffer, std::vector<uint8_t>()); }
2✔
318

319
      uint64_t last_received_seq_no() const { return received_seq_no; }
2✔
320

321
      const std::map<std::string, unsigned int>& callback_invocations() const { return m_callback_invocations; }
40✔
322

323
      void reset_callback_invocation_counters() { m_callback_invocations.clear(); }
40✔
324

325
   private:
326
      void count_callback_invocation(const std::string& callback_name) const {
238✔
327
         if(!m_callback_invocations.contains(callback_name)) {
238✔
328
            m_callback_invocations[callback_name] = 0;
225✔
329
         }
330

331
         m_callback_invocations[callback_name]++;
238✔
332
      }
238✔
333

334
   public:
335
      bool session_activated_called;
336

337
      std::vector<Botan::X509_Certificate> certificate_chain;
338
      std::map<std::string, std::vector<std::vector<uint8_t>>> serialized_messages;
339

340
   private:
341
      std::vector<uint8_t> send_buffer;
342
      std::vector<uint8_t> receive_buffer;
343
      uint64_t received_seq_no;
344
      Modify_Exts_Fn m_modify_exts;
345
      std::vector<MockSignature> m_mock_signatures;
346
      std::chrono::system_clock::time_point m_timestamp;
347

348
      mutable std::map<std::string, unsigned int> m_callback_invocations;
349
};
350

351
class Test_Credentials : public Botan::Credentials_Manager {
352
   public:
353
      explicit Test_Credentials(bool use_alternative_server_certificate) :
10✔
354
            m_alternative_server_certificate(use_alternative_server_certificate) {
10✔
355
         Botan::DataSource_Memory in(Test::read_data_file("tls_13_rfc8448/server_key.pem"));
20✔
356
         m_server_private_key.reset(Botan::PKCS8::load_key(in).release());
10✔
357

358
         // RFC 8448 does not actually provide these keys. Hence we generate one on the
359
         // fly as a stand-in. Instead of actually using it, the signatures generated
360
         // by this private key must be hard-coded in `Callbacks::sign_message()`; see
361
         // `MockSignature_Fn` for more details.
362
         m_bogus_alternative_server_private_key.reset(create_private_key("ECDSA", Test::rng(), "secp256r1").release());
10✔
363

364
         m_client_private_key.reset(create_private_key("RSA", Test::rng(), "1024").release());
20✔
365
      }
10✔
366

367
      std::vector<Botan::X509_Certificate> cert_chain(const std::vector<std::string>& cert_key_types,
5✔
368
                                                      const std::vector<AlgorithmIdentifier>& cert_signature_schemes,
369
                                                      const std::string& type,
370
                                                      const std::string& context) override {
371
         BOTAN_UNUSED(cert_key_types, cert_signature_schemes, context);
5✔
372
         return {(type == "tls-client")
5✔
373
                    ? client_certificate()
374
                    : ((m_alternative_server_certificate) ? alternative_server_certificate() : server_certificate())};
10✔
375
      }
376

377
      std::shared_ptr<Botan::Private_Key> private_key_for(const Botan::X509_Certificate& cert,
5✔
378
                                                          const std::string& type,
379
                                                          const std::string& context) override {
380
         BOTAN_UNUSED(cert, context);
5✔
381

382
         if(type == "tls-client")
5✔
383
            return m_client_private_key;
1✔
384

385
         if(m_alternative_server_certificate)
4✔
386
            return m_bogus_alternative_server_private_key;
1✔
387

388
         return m_server_private_key;
3✔
389
      }
390

391
   private:
392
      bool m_alternative_server_certificate;
393
      std::shared_ptr<Private_Key> m_client_private_key;
394
      std::shared_ptr<Private_Key> m_bogus_alternative_server_private_key;
395
      std::shared_ptr<Private_Key> m_server_private_key;
396
};
397

398
class RFC8448_Text_Policy : public Botan::TLS::Text_Policy {
10✔
399
   private:
400
      Botan::TLS::Text_Policy read_policy(const std::string& policy_file) {
10✔
401
         const std::string fspath = Test::data_file("tls-policy/" + policy_file + ".txt");
20✔
402

403
         std::ifstream is(fspath.c_str());
10✔
404
         if(!is.good()) {
10✔
405
            throw Test_Error("Missing policy file " + fspath);
×
406
         }
407

408
         return Botan::TLS::Text_Policy(is);
10✔
409
      }
20✔
410

411
   public:
412
      RFC8448_Text_Policy(const std::string& policy_file) : Botan::TLS::Text_Policy(read_policy(policy_file)) {}
10✔
413

414
      std::vector<Botan::TLS::Signature_Scheme> allowed_signature_schemes() const override {
11✔
415
         // We extend the allowed signature schemes with algorithms that we don't
416
         // actually support. The nature of the RFC 8448 test forces us to generate
417
         // bit-compatible TLS messages. Unfortunately, the test data offers all
418
         // those algorithms in its Client Hellos.
419
         return {
11✔
420
            Botan::TLS::Signature_Scheme::ECDSA_SHA256,
421
            Botan::TLS::Signature_Scheme::ECDSA_SHA384,
422
            Botan::TLS::Signature_Scheme::ECDSA_SHA512,
423
            Botan::TLS::Signature_Scheme::ECDSA_SHA1,  // not actually supported
424
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA256,
425
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA384,
426
            Botan::TLS::Signature_Scheme::RSA_PSS_SHA512,
427
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA256,
428
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA384,
429
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA512,
430
            Botan::TLS::Signature_Scheme::RSA_PKCS1_SHA1,  // not actually supported
431
            Botan::TLS::Signature_Scheme(0x0402),          // DSA_SHA256, not actually supported
432
            Botan::TLS::Signature_Scheme(0x0502),          // DSA_SHA384, not actually supported
433
            Botan::TLS::Signature_Scheme(0x0602),          // DSA_SHA512, not actually supported
434
            Botan::TLS::Signature_Scheme(0x0202),          // DSA_SHA1, not actually supported
435
         };
11✔
436
      }
437

438
      // Overriding the key exchange group selection to favour the server's key
439
      // exchange group preference. This is required to enforce a Hello Retry Request
440
      // when testing RFC 8448 5. from the server side.
441
      Named_Group choose_key_exchange_group(const std::vector<Group_Params>& supported_by_peer,
6✔
442
                                            const std::vector<Group_Params>& offered_by_peer) const override {
443
         BOTAN_UNUSED(offered_by_peer);
6✔
444

445
         const auto supported_by_us = key_exchange_groups();
6✔
446
         const auto selected_group =
6✔
447
            std::find_if(supported_by_us.begin(), supported_by_us.end(), [&](const auto group) {
6✔
448
               return value_exists(supported_by_peer, group);
12✔
449
            });
450

451
         return selected_group != supported_by_us.end() ? *selected_group : Named_Group::NONE;
6✔
452
      }
6✔
453
};
454

455
/**
456
 * In-Memory Session Manager that stores sessions verbatim, without encryption.
457
 * Therefor it is not dependent on a random number generator and can easily be
458
 * instrumented for test inspection.
459
 */
460
class RFC8448_Session_Manager : public Botan::TLS::Session_Manager {
461
   private:
462
      decltype(auto) find_by_handle(const Session_Handle& handle) {
2✔
463
         return [=](const Session_with_Handle& session) {
21✔
464
            if(session.handle.id().has_value() && handle.id().has_value() &&
4✔
465
               session.handle.id().value() == handle.id().value()) {
2✔
466
               return true;
467
            }
468
            if(session.handle.ticket().has_value() && handle.ticket().has_value() &&
8✔
469
               session.handle.ticket().value() == handle.ticket().value()) {
10✔
470
               return true;
2✔
471
            }
472
            return false;
473
         };
2✔
474
      }
475

476
   public:
477
      RFC8448_Session_Manager() : Session_Manager(Test::rng_as_shared()) {}
20✔
478

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

481
      void store(const Session& session, const Session_Handle& handle) override {
4✔
482
         m_sessions.push_back({session, handle});
4✔
483
      }
4✔
484

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

490
         if(m_sessions.size() != 1) {
1✔
491
            throw Botan_Tests::Test_Error("No mocked session handle available; Test bug?");
×
492
         }
493

494
         const auto& [mocked_session, handle] = m_sessions.front();
1✔
495
         if(mocked_session.master_secret() != session.master_secret()) {
1✔
496
            throw Botan_Tests::Test_Error("Generated session object does not match the expected mock");
×
497
         }
498

499
         return handle;
1✔
500
      }
501

502
      std::optional<Session> retrieve_one(const Session_Handle& handle) override {
1✔
503
         auto itr = std::find_if(m_sessions.begin(), m_sessions.end(), find_by_handle(handle));
1✔
504
         if(itr == m_sessions.end())
1✔
505
            return std::nullopt;
×
506
         else
507
            return itr->session;
2✔
508
      }
509

510
      std::vector<Session_with_Handle> find_some(const Server_Information& info, const size_t) override {
5✔
511
         std::vector<Session_with_Handle> found_sessions;
5✔
512
         for(const auto& [session, handle] : m_sessions) {
6✔
513
            if(session.server_info() == info) {
1✔
514
               found_sessions.emplace_back(Session_with_Handle{session, handle});
2✔
515
            }
516
         }
517

518
         return found_sessions;
5✔
519
      }
×
520

521
      size_t remove(const Session_Handle& handle) override {
1✔
522
         // TODO: C++20 allows to simply implement the entire method like:
523
         //
524
         //   return std::erase_if(m_sessions, find_by_handle(handle));
525
         //
526
         // Unfortunately, at the time of this writing Android NDK shipped with
527
         // a std::erase_if that returns void.
528
         auto rm_itr = std::remove_if(m_sessions.begin(), m_sessions.end(), find_by_handle(handle));
1✔
529

530
         const auto elements_being_removed = std::distance(rm_itr, m_sessions.end());
1✔
531
         m_sessions.erase(rm_itr);
1✔
532
         return elements_being_removed;
1✔
533
      }
534

535
      size_t remove_all() override {
×
536
         const auto sessions = m_sessions.size();
×
537
         m_sessions.clear();
×
538
         return sessions;
×
539
      }
540

541
   private:
542
      std::vector<Session_with_Handle> m_sessions;
543
};
544

545
/**
546
 * This steers the TLS client handle and is the central entry point for the
547
 * test cases to interact with the TLS 1.3 implementation.
548
 *
549
 * Note: This class is abstract to be subclassed for both client and server tests.
550
 */
551
class TLS_Context {
552
   protected:
553
      TLS_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng_in,
10✔
554
                  std::shared_ptr<const RFC8448_Text_Policy> policy,
555
                  Modify_Exts_Fn modify_exts_cb,
556
                  std::vector<MockSignature> mock_signatures,
557
                  uint64_t timestamp,
558
                  std::optional<std::pair<Session, Session_Ticket>> session_and_ticket,
559
                  bool use_alternative_server_certificate) :
10✔
560
            m_callbacks(std::make_shared<Test_TLS_13_Callbacks>(
10✔
561
               std::move(modify_exts_cb), std::move(mock_signatures), timestamp)),
10✔
562
            m_creds(std::make_shared<Test_Credentials>(use_alternative_server_certificate)),
10✔
563
            m_rng(std::move(rng_in)),
10✔
564
            m_session_mgr(std::make_shared<RFC8448_Session_Manager>()),
565
            m_policy(std::move(policy)) {
20✔
566
         if(session_and_ticket.has_value()) {
10✔
567
            m_session_mgr->store(std::get<Session>(session_and_ticket.value()),
12✔
568
                                 std::get<Session_Ticket>(session_and_ticket.value()));
3✔
569
         }
570
      }
10✔
571

572
   public:
573
      virtual ~TLS_Context() = default;
50✔
574

575
      TLS_Context(TLS_Context&) = delete;
576
      TLS_Context& operator=(const TLS_Context&) = delete;
577

578
      TLS_Context(TLS_Context&&) = delete;
579
      TLS_Context& operator=(TLS_Context&&) = delete;
580

581
      std::vector<uint8_t> pull_send_buffer() { return m_callbacks->pull_send_buffer(); }
27✔
582

583
      std::vector<uint8_t> pull_receive_buffer() { return m_callbacks->pull_receive_buffer(); }
2✔
584

585
      uint64_t last_received_seq_no() const { return m_callbacks->last_received_seq_no(); }
2✔
586

587
      /**
588
       * Checks that all of the listed callbacks were called at least once, no other
589
       * callbacks were called in addition to the expected ones. After the checks are
590
       * done, the callback invocation counters are reset.
591
       */
592
      void check_callback_invocations(Test::Result& result,
40✔
593
                                      const std::string& context,
594
                                      const std::vector<std::string>& callback_names) {
595
         const auto& invokes = m_callbacks->callback_invocations();
40✔
596
         for(const auto& cbn : callback_names) {
258✔
597
            result.confirm(cbn + " was invoked (Context: " + context + ")",
872✔
598
                           invokes.contains(cbn) && invokes.at(cbn) > 0);
218✔
599
         }
600

601
         for(const auto& invoke : invokes) {
258✔
602
            if(invoke.second == 0) {
218✔
603
               continue;
×
604
            }
605
            result.confirm(
436✔
606
               invoke.first + " was expected (Context: " + context + ")",
436✔
607
               std::find(callback_names.cbegin(), callback_names.cend(), invoke.first) != callback_names.cend());
436✔
608
         }
609

610
         m_callbacks->reset_callback_invocation_counters();
40✔
611
      }
40✔
612

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

615
      const std::vector<Botan::X509_Certificate>& certs_verified() const { return m_callbacks->certificate_chain; }
1✔
616

617
      decltype(auto) observed_handshake_messages() const { return m_callbacks->serialized_messages; }
5✔
618

619
      /**
620
       * Send application data through the secure channel
621
       */
622
      virtual void send(const std::vector<uint8_t>& data) = 0;
623

624
   protected:
625
      std::shared_ptr<Test_TLS_13_Callbacks> m_callbacks;
626
      std::shared_ptr<Test_Credentials> m_creds;
627

628
      std::shared_ptr<Botan::RandomNumberGenerator> m_rng;
629
      std::shared_ptr<RFC8448_Session_Manager> m_session_mgr;
630
      std::shared_ptr<const RFC8448_Text_Policy> m_policy;
631
};
632

633
class Client_Context : public TLS_Context {
634
   public:
635
      Client_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng_in,
5✔
636
                     std::shared_ptr<const RFC8448_Text_Policy> policy,
637
                     uint64_t timestamp,
638
                     Modify_Exts_Fn modify_exts_cb,
639
                     std::optional<std::pair<Session, Session_Ticket>> session_and_ticket = std::nullopt,
640
                     std::vector<MockSignature> mock_signatures = {}) :
5✔
641
            TLS_Context(std::move(rng_in),
5✔
642
                        std::move(policy),
5✔
643
                        std::move(modify_exts_cb),
5✔
644
                        std::move(mock_signatures),
5✔
645
                        timestamp,
646
                        std::move(session_and_ticket),
5✔
647
                        false),
648
            client(m_callbacks,
20✔
649
                   m_session_mgr,
5✔
650
                   m_creds,
5✔
651
                   m_policy,
5✔
652
                   m_rng,
5✔
653
                   Botan::TLS::Server_Information("server"),
10✔
654
                   Botan::TLS::Protocol_Version::TLS_V13) {}
21✔
655

656
      void send(const std::vector<uint8_t>& data) override { client.send(data.data(), data.size()); }
1✔
657

658
      Botan::TLS::Client client;
659
};
660

661
class Server_Context : public TLS_Context {
662
   public:
663
      Server_Context(std::shared_ptr<Botan::RandomNumberGenerator> rng,
5✔
664
                     std::shared_ptr<const RFC8448_Text_Policy> policy,
665
                     uint64_t timestamp,
666
                     Modify_Exts_Fn modify_exts_cb,
667
                     std::vector<MockSignature> mock_signatures,
668
                     bool use_alternative_server_certificate = false,
669
                     std::optional<std::pair<Session, Session_Ticket>> session_and_ticket = std::nullopt) :
5✔
670
            TLS_Context(std::move(rng),
5✔
671
                        std::move(policy),
5✔
672
                        std::move(modify_exts_cb),
5✔
673
                        std::move(mock_signatures),
5✔
674
                        timestamp,
675
                        std::move(session_and_ticket),
5✔
676
                        use_alternative_server_certificate),
677
            server(m_callbacks, m_session_mgr, m_creds, m_policy, m_rng, false /* DTLS NYI */) {}
37✔
678

679
      void send(const std::vector<uint8_t>& data) override { server.send(data.data(), data.size()); }
1✔
680

681
      Botan::TLS::Server server;
682
};
683

684
/**
685
 * Because of the nature of the RFC 8448 test data we need to produce bit-compatible
686
 * TLS messages. Hence we sort the generated TLS extensions exactly as expected.
687
 */
688
void sort_client_extensions(Botan::TLS::Extensions& exts, Botan::TLS::Connection_Side side) {
6✔
689
   if(side == Botan::TLS::Connection_Side::Client) {
6✔
690
      const std::vector<Botan::TLS::Extension_Code> expected_order = {
6✔
691
         Botan::TLS::Extension_Code::ServerNameIndication,
692
         Botan::TLS::Extension_Code::SafeRenegotiation,
693
         Botan::TLS::Extension_Code::SupportedGroups,
694
         Botan::TLS::Extension_Code::SessionTicket,
695
         Botan::TLS::Extension_Code::KeyShare,
696
         Botan::TLS::Extension_Code::EarlyData,
697
         Botan::TLS::Extension_Code::SupportedVersions,
698
         Botan::TLS::Extension_Code::SignatureAlgorithms,
699
         Botan::TLS::Extension_Code::Cookie,
700
         Botan::TLS::Extension_Code::PskKeyExchangeModes,
701
         Botan::TLS::Extension_Code::RecordSizeLimit,
702
         Padding::static_type(),
703
         Botan::TLS::Extension_Code::PresharedKey,
704
      };
6✔
705

706
      for(const auto ext_type : expected_order) {
84✔
707
         auto ext = exts.take(ext_type);
78✔
708
         if(ext != nullptr) {
78✔
709
            exts.add(std::move(ext));
108✔
710
         }
711
      }
78✔
712
   }
6✔
713
}
6✔
714

715
/**
716
 * Because of the nature of the RFC 8448 test data we need to produce bit-compatible
717
 * TLS messages. Hence we sort the generated TLS extensions exactly as expected.
718
 */
719
void sort_server_extensions(Botan::TLS::Extensions& exts,
16✔
720
                            Botan::TLS::Connection_Side side,
721
                            Botan::TLS::Handshake_Type /*type*/) {
722
   if(side == Botan::TLS::Connection_Side::Server) {
16✔
723
      const std::vector<Botan::TLS::Extension_Code> expected_order = {
16✔
724
         Botan::TLS::Extension_Code::SupportedGroups,
725
         Botan::TLS::Extension_Code::KeyShare,
726
         Botan::TLS::Extension_Code::Cookie,
727
         Botan::TLS::Extension_Code::SupportedVersions,
728
         Botan::TLS::Extension_Code::SignatureAlgorithms,
729
         Botan::TLS::Extension_Code::RecordSizeLimit,
730
         Botan::TLS::Extension_Code::ServerNameIndication,
731
         Botan::TLS::Extension_Code::EarlyData,
732
      };
16✔
733

734
      for(const auto ext_type : expected_order) {
144✔
735
         auto ext = exts.take(ext_type);
128✔
736
         if(ext != nullptr) {
128✔
737
            exts.add(std::move(ext));
60✔
738
         }
739
      }
128✔
740
   }
16✔
741
}
16✔
742

743
void add_renegotiation_extension(Botan::TLS::Extensions& exts) {
5✔
744
   // Renegotiation is not possible in TLS 1.3. Nevertheless, RFC 8448 requires
745
   // to add this to the Client Hello for reasons.
746
   exts.add(new Renegotiation_Extension());
5✔
747
}
5✔
748

749
void add_early_data_indication(Botan::TLS::Extensions& exts) { exts.add(new Botan::TLS::EarlyDataIndication()); }
1✔
750

751
std::vector<uint8_t> strip_message_header(const std::vector<uint8_t>& msg) {
22✔
752
   BOTAN_ASSERT_NOMSG(msg.size() >= 4);
22✔
753
   return {msg.begin() + 4, msg.end()};
22✔
754
}
755

756
std::vector<MockSignature> make_mock_signatures(const VarMap& vars) {
6✔
757
   std::vector<MockSignature> result;
6✔
758

759
   auto mock = [&](const std::string& msg, const std::string& sig) {
18✔
760
      if(vars.has_key(msg) && vars.has_key(sig))
31✔
761
         result.push_back({vars.get_opt_bin(msg), vars.get_opt_bin(sig)});
7✔
762
   };
12✔
763

764
   mock("Server_MessageToSign", "Server_MessageSignature");
12✔
765
   mock("Client_MessageToSign", "Client_MessageSignature");
12✔
766

767
   return result;
6✔
768
}
×
769

770
}  // namespace
771

772
/**
773
 * Traffic transcripts and supporting data for the TLS RFC 8448 and TLS policy
774
 * configuration is kept in data files (accessible via `Test:::data_file()`).
775
 *
776
 * tls_13_rfc8448/transcripts.vec
777
 *   The record transcripts and RNG outputs as defined/required in RFC 8448 in
778
 *   Botan's Text_Based_Test vector format. Data from each RFC 8448 section is
779
 *   placed in a sub-section of the *.vec file. Each of those sections needs a
780
 *   specific test case implementation that is dispatched in `run_one_test()`.
781
 *
782
 * tls_13_rfc8448/client_certificate.pem
783
 *   The client certificate provided in RFC 8448 used to perform client auth.
784
 *   Note that RFC 8448 _does not_ provide the associated private key but only
785
 *   the resulting signature in the client's CertificateVerify message.
786
 *
787
 * tls_13_rfc8448/server_certificate.pem
788
 * tls_13_rfc8448/server_key.pem
789
 *   The server certificate and its associated private key.
790
 *
791
 * tls_13_rfc8448/server_certificate_client_auth.pem
792
 *   The server certificate used in the Client Authentication test case.
793
 *
794
 * tls-policy/rfc8448_*.txt
795
 *   Each RFC 8448 section test required a slightly adapted Botan TLS policy
796
 *   to enable/disable certain features under test.
797
 *
798
 * While the test cases are split into Client-side and Server-side tests, the
799
 * transcript data is reused. See the concrete implementations of the abstract
800
 * Test_TLS_RFC8448 test class.
801
 */
802
class Test_TLS_RFC8448 : public Text_Based_Test {
803
   protected:
804
      virtual std::vector<Test::Result> simple_1_rtt(const VarMap& vars) = 0;
805
      virtual std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) = 0;
806
      virtual std::vector<Test::Result> hello_retry_request(const VarMap& vars) = 0;
807
      virtual std::vector<Test::Result> client_authentication(const VarMap& vars) = 0;
808
      virtual std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) = 0;
809

810
      virtual std::string side() const = 0;
811

812
   public:
813
      Test_TLS_RFC8448() :
2✔
814
            Text_Based_Test("tls_13_rfc8448/transcripts.vec",
815
                            // mandatory data fields
816
                            "Client_RNG_Pool,"
817
                            "Server_RNG_Pool,"
818
                            "CurrentTimestamp,"
819
                            "Record_ClientHello_1,"
820
                            "Record_ServerHello,"
821
                            "Record_ServerHandshakeMessages,"
822
                            "Record_ClientFinished,"
823
                            "Record_Client_CloseNotify,"
824
                            "Record_Server_CloseNotify",
825
                            // optional data fields
826
                            "Message_ServerHello,"
827
                            "Message_EncryptedExtensions,"
828
                            "Message_CertificateRequest,"
829
                            "Message_Server_Certificate,"
830
                            "Message_Server_CertificateVerify,"
831
                            "Message_Server_Finished,"
832
                            "Record_HelloRetryRequest,"
833
                            "Record_ClientHello_2,"
834
                            "Record_NewSessionTicket,"
835
                            "Client_AppData,"
836
                            "Record_Client_AppData,"
837
                            "Server_AppData,"
838
                            "Record_Server_AppData,"
839
                            "Client_EarlyAppData,"
840
                            "Record_Client_EarlyAppData,"
841
                            "SessionTicket,"
842
                            "Client_SessionData,"
843
                            "Server_MessageToSign,"
844
                            "Server_MessageSignature,"
845
                            "Client_MessageToSign,"
846
                            "Client_MessageSignature,"
847
                            "HelloRetryRequest_Cookie") {}
8✔
848

849
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
10✔
850
         if(header == "Simple_1RTT_Handshake")
10✔
851
            return Test::Result("Simple 1-RTT (" + side() + " side)", simple_1_rtt(vars));
4✔
852
         else if(header == "Resumed_0RTT_Handshake")
8✔
853
            return Test::Result("Resumption with 0-RTT data (" + side() + " side)", resumed_handshake_with_0_rtt(vars));
4✔
854
         else if(header == "HelloRetryRequest_Handshake")
6✔
855
            return Test::Result("Handshake involving Hello Retry Request (" + side() + " side)",
4✔
856
                                hello_retry_request(vars));
6✔
857
         else if(header == "Client_Authentication_Handshake")
4✔
858
            return Test::Result("Client Authentication (" + side() + " side)", client_authentication(vars));
4✔
859
         else if(header == "Middlebox_Compatibility_Mode")
2✔
860
            return Test::Result("Middlebox Compatibility Mode (" + side() + " side)", middlebox_compatibility(vars));
4✔
861
         else
862
            return Test::Result::Failure("test dispatcher", "unknown sub-test: " + header);
×
863
      }
864
};
865

866
class Test_TLS_RFC8448_Client : public Test_TLS_RFC8448 {
1✔
867
   private:
868
      std::string side() const override { return "Client"; }
5✔
869

870
      std::vector<Test::Result> simple_1_rtt(const VarMap& vars) override {
1✔
871
         auto rng = std::make_shared<Botan_Tests::Fixed_Output_RNG>("");
1✔
872

873
         // 32 - for client hello random
874
         // 32 - for KeyShare (eph. x25519 key pair)
875
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
876

877
         auto add_extensions_and_sort = [](Botan::TLS::Extensions& exts,
2✔
878
                                           Botan::TLS::Connection_Side side,
879
                                           Botan::TLS::Handshake_Type which_message) {
880
            if(which_message == Handshake_Type::ClientHello) {
1✔
881
               // For some reason, presumably checking compatibility, the RFC 8448 Client
882
               // Hello includes a (TLS 1.2) Session_Ticket extension. We don't normally add
883
               // this obsoleted extension in a TLS 1.3 client.
884
               exts.add(new Botan::TLS::Session_Ticket_Extension());
1✔
885

886
               add_renegotiation_extension(exts);
1✔
887
               sort_client_extensions(exts, side);
1✔
888
            }
889
         };
1✔
890

891
         std::unique_ptr<Client_Context> ctx;
1✔
892

893
         return {
1✔
894
            Botan_Tests::CHECK(
895
               "Client Hello",
896
               [&](Test::Result& result) {
1✔
897
                  ctx = std::make_unique<Client_Context>(rng,
3✔
898
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
1✔
899
                                                         vars.get_req_u64("CurrentTimestamp"),
2✔
900
                                                         add_extensions_and_sort);
2✔
901

902
                  result.confirm("client not closed", !ctx->client.is_closed());
2✔
903
                  ctx->check_callback_invocations(result,
7✔
904
                                                  "client hello prepared",
905
                                                  {
906
                                                     "tls_emit_data",
907
                                                     "tls_inspect_handshake_msg_client_hello",
908
                                                     "tls_modify_extensions_client_hello",
909
                                                     "tls_generate_ephemeral_key",
910
                                                     "tls_current_timestamp",
911
                                                  });
912

913
                  result.test_eq("TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
914
               }),
1✔
915

916
            Botan_Tests::CHECK(
917
               "Server Hello",
918
               [&](Test::Result& result) {
1✔
919
                  result.require("ctx is available", ctx != nullptr);
1✔
920
                  const auto server_hello = vars.get_req_bin("Record_ServerHello");
1✔
921
                  // splitting the input data to test partial reads
922
                  const std::vector<uint8_t> server_hello_a(server_hello.begin(), server_hello.begin() + 20);
1✔
923
                  const std::vector<uint8_t> server_hello_b(server_hello.begin() + 20, server_hello.end());
1✔
924

925
                  ctx->client.received_data(server_hello_a);
1✔
926
                  ctx->check_callback_invocations(result, "server hello partially received", {});
2✔
927

928
                  ctx->client.received_data(server_hello_b);
1✔
929
                  ctx->check_callback_invocations(result,
5✔
930
                                                  "server hello received",
931
                                                  {"tls_inspect_handshake_msg_server_hello",
932
                                                   "tls_examine_extensions_server_hello",
933
                                                   "tls_ephemeral_key_agreement"});
934

935
                  result.confirm("client is not yet active", !ctx->client.is_active());
3✔
936
               }),
3✔
937

938
            Botan_Tests::CHECK(
939
               "Server HS messages .. Client Finished",
940
               [&](Test::Result& result) {
1✔
941
                  result.require("ctx is available", ctx != nullptr);
3✔
942
                  ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
943

944
                  ctx->check_callback_invocations(result,
14✔
945
                                                  "encrypted handshake messages received",
946
                                                  {"tls_inspect_handshake_msg_encrypted_extensions",
947
                                                   "tls_inspect_handshake_msg_certificate",
948
                                                   "tls_inspect_handshake_msg_certificate_verify",
949
                                                   "tls_inspect_handshake_msg_finished",
950
                                                   "tls_examine_extensions_encrypted_extensions",
951
                                                   "tls_examine_extensions_certificate",
952
                                                   "tls_emit_data",
953
                                                   "tls_current_timestamp",
954
                                                   "tls_session_established",
955
                                                   "tls_session_activated",
956
                                                   "tls_verify_cert_chain",
957
                                                   "tls_verify_message"});
958
                  result.require("certificate exists", !ctx->certs_verified().empty());
1✔
959
                  result.require("correct certificate", ctx->certs_verified().front() == server_certificate());
2✔
960
                  result.require("client is active", ctx->client.is_active());
1✔
961

962
                  result.test_eq(
3✔
963
                     "correct handshake finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
3✔
964
               }),
1✔
965

966
            Botan_Tests::CHECK("Post-Handshake: NewSessionTicket",
967
                               [&](Test::Result& result) {
1✔
968
                                  result.require("ctx is available", ctx != nullptr);
1✔
969
                                  result.require("no sessions so far", ctx->stored_sessions().empty());
1✔
970
                                  ctx->client.received_data(vars.get_req_bin("Record_NewSessionTicket"));
2✔
971

972
                                  ctx->check_callback_invocations(result,
5✔
973
                                                                  "new session ticket received",
974
                                                                  {"tls_examine_extensions_new_session_ticket",
975
                                                                   "tls_should_persist_resumption_information",
976
                                                                   "tls_current_timestamp"});
977
                                  if(result.test_eq("session was stored", ctx->stored_sessions().size(), 1)) {
2✔
978
                                     const auto& [stored_session, stored_handle] = ctx->stored_sessions().front();
1✔
979
                                     result.require("session handle contains a ticket",
2✔
980
                                                    stored_handle.ticket().has_value());
1✔
981
                                     result.test_is_eq("session was serialized as expected",
3✔
982
                                                       Botan::unlock(stored_session.DER_encode()),
4✔
983
                                                       vars.get_req_bin("Client_SessionData"));
3✔
984
                                  }
985
                               }),
1✔
986

987
            Botan_Tests::CHECK("Send Application Data",
988
                               [&](Test::Result& result) {
1✔
989
                                  result.require("ctx is available", ctx != nullptr);
2✔
990
                                  ctx->send(vars.get_req_bin("Client_AppData"));
3✔
991

992
                                  ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
3✔
993

994
                                  result.test_eq("correct client application data",
3✔
995
                                                 ctx->pull_send_buffer(),
2✔
996
                                                 vars.get_req_bin("Record_Client_AppData"));
2✔
997
                               }),
1✔
998

999
            Botan_Tests::CHECK(
1000
               "Receive Application Data",
1001
               [&](Test::Result& result) {
1✔
1002
                  result.require("ctx is available", ctx != nullptr);
1✔
1003
                  ctx->client.received_data(vars.get_req_bin("Record_Server_AppData"));
2✔
1004

1005
                  ctx->check_callback_invocations(result, "application data sent", {"tls_record_received"});
3✔
1006

1007
                  const auto rcvd = ctx->pull_receive_buffer();
1✔
1008
                  result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Server_AppData"));
3✔
1009
                  result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(1));
2✔
1010
               }),
1✔
1011

1012
            Botan_Tests::CHECK("Close Connection",
1013
                               [&](Test::Result& result) {
1✔
1014
                                  result.require("ctx is available", ctx != nullptr);
2✔
1015
                                  ctx->client.close();
1✔
1016

1017
                                  result.test_eq("close payload",
2✔
1018
                                                 ctx->pull_send_buffer(),
2✔
1019
                                                 vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1020
                                  ctx->check_callback_invocations(result, "CLOSE_NOTIFY sent", {"tls_emit_data"});
3✔
1021

1022
                                  ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1023
                                  ctx->check_callback_invocations(
4✔
1024
                                     result, "CLOSE_NOTIFY received", {"tls_alert", "tls_peer_closed_connection"});
1025

1026
                                  result.confirm("connection is closed", ctx->client.is_closed());
2✔
1027
                               }),
1✔
1028
         };
8✔
1029
      }
2✔
1030

1031
      std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) override {
1✔
1032
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>("");
1✔
1033

1034
         // 32 - for client hello random
1035
         // 32 - for KeyShare (eph. x25519 key pair)
1036
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1037

1038
         auto add_extensions_and_sort = [](Botan::TLS::Extensions& exts,
2✔
1039
                                           Botan::TLS::Connection_Side side,
1040
                                           Botan::TLS::Handshake_Type which_message) {
1041
            if(which_message == Handshake_Type::ClientHello) {
1✔
1042
               exts.add(new Padding(87));
1✔
1043

1044
               add_renegotiation_extension(exts);
1✔
1045

1046
               // TODO: Implement early data support and remove this 'hack'.
1047
               //
1048
               // Currently, the production implementation will never add this
1049
               // extension even if the resumed session would allow early data.
1050
               add_early_data_indication(exts);
1✔
1051
               sort_client_extensions(exts, side);
1✔
1052
            }
1053
         };
1✔
1054

1055
         std::unique_ptr<Client_Context> ctx;
1✔
1056

1057
         return {
1✔
1058
            Botan_Tests::CHECK(
1059
               "Client Hello",
1060
               [&](Test::Result& result) {
1✔
1061
                  ctx = std::make_unique<Client_Context>(
3✔
1062
                     std::move(rng),
1✔
1063
                     std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
1✔
1064
                     vars.get_req_u64("CurrentTimestamp"),
4✔
1065
                     add_extensions_and_sort,
1✔
1066
                     std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1067
                               Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1068

1069
                  result.confirm("client not closed", !ctx->client.is_closed());
2✔
1070
                  ctx->check_callback_invocations(result,
7✔
1071
                                                  "client hello prepared",
1072
                                                  {
1073
                                                     "tls_emit_data",
1074
                                                     "tls_inspect_handshake_msg_client_hello",
1075
                                                     "tls_modify_extensions_client_hello",
1076
                                                     "tls_current_timestamp",
1077
                                                     "tls_generate_ephemeral_key",
1078
                                                  });
1079

1080
                  result.test_eq("TLS client hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1081
               })
1✔
1082

1083
            // TODO: The rest of this test vector requires 0-RTT which is not
1084
            //       yet implemented. For now we can only test the client's
1085
            //       ability to offer a session resumption via PSK.
1086
         };
2✔
1087
      }
1✔
1088

1089
      std::vector<Test::Result> hello_retry_request(const VarMap& vars) override {
1✔
1090
         auto add_extensions_and_sort = [flights = 0](Botan::TLS::Extensions& exts,
3✔
1091
                                                      Botan::TLS::Connection_Side side,
1092
                                                      Botan::TLS::Handshake_Type which_message) mutable {
4✔
1093
            if(which_message == Handshake_Type::ClientHello) {
2✔
1094
               ++flights;
2✔
1095

1096
               if(flights == 1) {
2✔
1097
                  add_renegotiation_extension(exts);
1✔
1098
               }
1099

1100
               // For some reason RFC8448 decided to require this (fairly obscure) extension
1101
               // in the second flight of the Client_Hello.
1102
               if(flights == 2) {
2✔
1103
                  exts.add(new Padding(175));
1✔
1104
               }
1105

1106
               sort_client_extensions(exts, side);
2✔
1107
            }
1108
         };
2✔
1109

1110
         // Fallback RNG is required to for blinding in ECDH with P-256
1111
         auto& fallback_rng = Test::rng();
1✔
1112
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>(fallback_rng);
1✔
1113

1114
         // 32 - client hello random
1115
         // 32 - eph. x25519 key pair
1116
         // 32 - eph. P-256 key pair
1117
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1118

1119
         std::unique_ptr<Client_Context> ctx;
1✔
1120

1121
         return {
1✔
1122
            Botan_Tests::CHECK(
1123
               "Client Hello",
1124
               [&](Test::Result& result) {
1✔
1125
                  ctx = std::make_unique<Client_Context>(std::move(rng),
3✔
1126
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_client"),
1✔
1127
                                                         vars.get_req_u64("CurrentTimestamp"),
2✔
1128
                                                         add_extensions_and_sort);
2✔
1129
                  result.confirm("client not closed", !ctx->client.is_closed());
2✔
1130

1131
                  ctx->check_callback_invocations(result,
7✔
1132
                                                  "client hello prepared",
1133
                                                  {
1134
                                                     "tls_emit_data",
1135
                                                     "tls_inspect_handshake_msg_client_hello",
1136
                                                     "tls_modify_extensions_client_hello",
1137
                                                     "tls_generate_ephemeral_key",
1138
                                                     "tls_current_timestamp",
1139
                                                  });
1140

1141
                  result.test_eq(
3✔
1142
                     "TLS client hello (1)", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
3✔
1143
               }),
1✔
1144

1145
            Botan_Tests::CHECK("Hello Retry Request .. second Client Hello",
1146
                               [&](Test::Result& result) {
1✔
1147
                                  result.require("ctx is available", ctx != nullptr);
2✔
1148
                                  ctx->client.received_data(vars.get_req_bin("Record_HelloRetryRequest"));
2✔
1149

1150
                                  ctx->check_callback_invocations(result,
8✔
1151
                                                                  "hello retry request received",
1152
                                                                  {
1153
                                                                     "tls_emit_data",
1154
                                                                     "tls_inspect_handshake_msg_hello_retry_request",
1155
                                                                     "tls_examine_extensions_hello_retry_request",
1156
                                                                     "tls_inspect_handshake_msg_client_hello",
1157
                                                                     "tls_modify_extensions_client_hello",
1158
                                                                     "tls_generate_ephemeral_key",
1159
                                                                  });
1160

1161
                                  result.test_eq("TLS client hello (2)",
3✔
1162
                                                 ctx->pull_send_buffer(),
2✔
1163
                                                 vars.get_req_bin("Record_ClientHello_2"));
2✔
1164
                               }),
1✔
1165

1166
            Botan_Tests::CHECK("Server Hello",
1167
                               [&](Test::Result& result) {
1✔
1168
                                  result.require("ctx is available", ctx != nullptr);
1✔
1169
                                  ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
2✔
1170

1171
                                  ctx->check_callback_invocations(result,
5✔
1172
                                                                  "server hello received",
1173
                                                                  {
1174
                                                                     "tls_inspect_handshake_msg_server_hello",
1175
                                                                     "tls_examine_extensions_server_hello",
1176
                                                                     "tls_ephemeral_key_agreement",
1177
                                                                  });
1178
                               }),
1✔
1179

1180
            Botan_Tests::CHECK(
1181
               "Server HS Messages .. Client Finished",
1182
               [&](Test::Result& result) {
1✔
1183
                  result.require("ctx is available", ctx != nullptr);
2✔
1184
                  ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
2✔
1185

1186
                  ctx->check_callback_invocations(result,
14✔
1187
                                                  "encrypted handshake messages received",
1188
                                                  {"tls_inspect_handshake_msg_encrypted_extensions",
1189
                                                   "tls_inspect_handshake_msg_certificate",
1190
                                                   "tls_inspect_handshake_msg_certificate_verify",
1191
                                                   "tls_inspect_handshake_msg_finished",
1192
                                                   "tls_examine_extensions_encrypted_extensions",
1193
                                                   "tls_examine_extensions_certificate",
1194
                                                   "tls_emit_data",
1195
                                                   "tls_current_timestamp",
1196
                                                   "tls_session_established",
1197
                                                   "tls_session_activated",
1198
                                                   "tls_verify_cert_chain",
1199
                                                   "tls_verify_message"});
1200

1201
                  result.test_eq("client finished", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientFinished"));
4✔
1202
               }),
1✔
1203

1204
            Botan_Tests::CHECK(
1205
               "Close Connection",
1206
               [&](Test::Result& result) {
1✔
1207
                  result.require("ctx is available", ctx != nullptr);
2✔
1208
                  ctx->client.close();
1✔
1209
                  ctx->check_callback_invocations(result, "encrypted handshake messages received", {"tls_emit_data"});
3✔
1210
                  result.test_eq(
2✔
1211
                     "client close notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
3✔
1212

1213
                  ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1214
                  ctx->check_callback_invocations(
4✔
1215
                     result, "encrypted handshake messages received", {"tls_alert", "tls_peer_closed_connection"});
1216

1217
                  result.confirm("connection is closed", ctx->client.is_closed());
2✔
1218
               }),
1✔
1219
         };
6✔
1220
      }
1✔
1221

1222
      std::vector<Test::Result> client_authentication(const VarMap& vars) override {
1✔
1223
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>("");
1✔
1224

1225
         // 32 - for client hello random
1226
         // 32 - for eph. x25519 key pair
1227
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1228

1229
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
3✔
1230
                                            Botan::TLS::Connection_Side side,
1231
                                            Botan::TLS::Handshake_Type which_message) {
1232
            if(which_message == Handshake_Type::ClientHello) {
2✔
1233
               add_renegotiation_extension(exts);
1✔
1234
               sort_client_extensions(exts, side);
1✔
1235
            }
1236
         };
1237

1238
         std::unique_ptr<Client_Context> ctx;
1✔
1239

1240
         return {
1✔
1241
            Botan_Tests::CHECK(
1242
               "Client Hello",
1243
               [&](Test::Result& result) {
1✔
1244
                  ctx = std::make_unique<Client_Context>(std::move(rng),
3✔
1245
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
1✔
1246
                                                         vars.get_req_u64("CurrentTimestamp"),
2✔
1247
                                                         add_extensions_and_sort,
1✔
1248
                                                         std::nullopt,
1249
                                                         make_mock_signatures(vars));
3✔
1250

1251
                  ctx->check_callback_invocations(result,
7✔
1252
                                                  "initial callbacks",
1253
                                                  {
1254
                                                     "tls_emit_data",
1255
                                                     "tls_inspect_handshake_msg_client_hello",
1256
                                                     "tls_modify_extensions_client_hello",
1257
                                                     "tls_generate_ephemeral_key",
1258
                                                     "tls_current_timestamp",
1259
                                                  });
1260

1261
                  result.test_eq("Client Hello", ctx->pull_send_buffer(), vars.get_req_bin("Record_ClientHello_1"));
4✔
1262
               }),
1✔
1263

1264
            Botan_Tests::CHECK("Server Hello",
1265
                               [&](auto& result) {
1✔
1266
                                  ctx->client.received_data(vars.get_req_bin("Record_ServerHello"));
3✔
1267

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

1277
            Botan_Tests::CHECK("other handshake messages and client auth",
1278
                               [&](Test::Result& result) {
1✔
1279
                                  ctx->client.received_data(vars.get_req_bin("Record_ServerHandshakeMessages"));
3✔
1280

1281
                                  ctx->check_callback_invocations(result,
18✔
1282
                                                                  "signing callbacks invoked",
1283
                                                                  {
1284
                                                                     "tls_sign_message",
1285
                                                                     "tls_emit_data",
1286
                                                                     "tls_examine_extensions_encrypted_extensions",
1287
                                                                     "tls_examine_extensions_certificate",
1288
                                                                     "tls_examine_extensions_certificate_request",
1289
                                                                     "tls_modify_extensions_certificate",
1290
                                                                     "tls_inspect_handshake_msg_certificate",
1291
                                                                     "tls_inspect_handshake_msg_certificate_request",
1292
                                                                     "tls_inspect_handshake_msg_certificate_verify",
1293
                                                                     "tls_inspect_handshake_msg_encrypted_extensions",
1294
                                                                     "tls_inspect_handshake_msg_finished",
1295
                                                                     "tls_current_timestamp",
1296
                                                                     "tls_session_established",
1297
                                                                     "tls_session_activated",
1298
                                                                     "tls_verify_cert_chain",
1299
                                                                     "tls_verify_message",
1300
                                                                  });
1301

1302
                                  // ClientFinished contains the entire coalesced client authentication flight
1303
                                  // Messages: Certificate, CertificateVerify, Finished
1304
                                  result.test_eq("Client Auth and Finished",
3✔
1305
                                                 ctx->pull_send_buffer(),
2✔
1306
                                                 vars.get_req_bin("Record_ClientFinished"));
2✔
1307
                               }),
1✔
1308

1309
            Botan_Tests::CHECK(
1310
               "Close Connection",
1311
               [&](Test::Result& result) {
1✔
1312
                  ctx->client.close();
2✔
1313
                  result.test_eq(
2✔
1314
                     "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
4✔
1315

1316
                  ctx->check_callback_invocations(result,
3✔
1317
                                                  "after sending close notify",
1318
                                                  {
1319
                                                     "tls_emit_data",
1320
                                                  });
1321

1322
                  ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1323
                  result.confirm("connection closed", ctx->client.is_closed());
2✔
1324

1325
                  ctx->check_callback_invocations(
4✔
1326
                     result, "after receiving close notify", {"tls_alert", "tls_peer_closed_connection"});
1327
               }),
1✔
1328
         };
5✔
1329
      }
1✔
1330

1331
      std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) override {
1✔
1332
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>("");
1✔
1333

1334
         // 32 - client hello random
1335
         // 32 - legacy session ID
1336
         // 32 - eph. x25519 key pair
1337
         add_entropy(*rng, vars.get_req_bin("Client_RNG_Pool"));
2✔
1338

1339
         auto add_extensions_and_sort = [&](Botan::TLS::Extensions& exts,
2✔
1340
                                            Botan::TLS::Connection_Side side,
1341
                                            Botan::TLS::Handshake_Type which_message) {
1342
            if(which_message == Handshake_Type::ClientHello) {
1✔
1343
               add_renegotiation_extension(exts);
1✔
1344
               sort_client_extensions(exts, side);
1✔
1345
            }
1346
         };
1347

1348
         std::unique_ptr<Client_Context> ctx;
1✔
1349

1350
         return {
1✔
1351
            Botan_Tests::CHECK(
1352
               "Client Hello",
1353
               [&](Test::Result& result) {
1✔
1354
                  ctx = std::make_unique<Client_Context>(std::move(rng),
3✔
1355
                                                         std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_client"),
1✔
1356
                                                         vars.get_req_u64("CurrentTimestamp"),
2✔
1357
                                                         add_extensions_and_sort);
2✔
1358

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

1361
                  ctx->check_callback_invocations(result,
7✔
1362
                                                  "client hello prepared",
1363
                                                  {
1364
                                                     "tls_emit_data",
1365
                                                     "tls_inspect_handshake_msg_client_hello",
1366
                                                     "tls_modify_extensions_client_hello",
1367
                                                     "tls_generate_ephemeral_key",
1368
                                                     "tls_current_timestamp",
1369
                                                  });
1370
               }),
1✔
1371

1372
            Botan_Tests::CHECK("Server Hello + other handshake messages",
1373
                               [&](Test::Result& result) {
1✔
1374
                                  result.require("ctx is available", ctx != nullptr);
2✔
1375
                                  ctx->client.received_data(Botan::concat(
3✔
1376
                                     vars.get_req_bin("Record_ServerHello"),
4✔
1377
                                     // ServerHandshakeMessages contains the expected ChangeCipherSpec record
1378
                                     vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1379

1380
                                  ctx->check_callback_invocations(result,
17✔
1381
                                                                  "callbacks after server's first flight",
1382
                                                                  {
1383
                                                                     "tls_inspect_handshake_msg_server_hello",
1384
                                                                     "tls_inspect_handshake_msg_encrypted_extensions",
1385
                                                                     "tls_inspect_handshake_msg_certificate",
1386
                                                                     "tls_inspect_handshake_msg_certificate_verify",
1387
                                                                     "tls_inspect_handshake_msg_finished",
1388
                                                                     "tls_examine_extensions_server_hello",
1389
                                                                     "tls_examine_extensions_encrypted_extensions",
1390
                                                                     "tls_examine_extensions_certificate",
1391
                                                                     "tls_emit_data",
1392
                                                                     "tls_current_timestamp",
1393
                                                                     "tls_session_established",
1394
                                                                     "tls_session_activated",
1395
                                                                     "tls_verify_cert_chain",
1396
                                                                     "tls_verify_message",
1397
                                                                     "tls_ephemeral_key_agreement",
1398
                                                                  });
1399

1400
                                  result.test_eq("CCS + Client Finished",
3✔
1401
                                                 ctx->pull_send_buffer(),
2✔
1402
                                                 // ClientFinished contains the expected ChangeCipherSpec record
1403
                                                 vars.get_req_bin("Record_ClientFinished"));
2✔
1404

1405
                                  result.confirm("client is ready to send application traffic",
2✔
1406
                                                 ctx->client.is_active());
1✔
1407
                               }),
1✔
1408

1409
            Botan_Tests::CHECK(
1410
               "Close connection",
1411
               [&](Test::Result& result) {
1✔
1412
                  result.require("ctx is available", ctx != nullptr);
2✔
1413
                  ctx->client.close();
1✔
1414

1415
                  result.test_eq(
2✔
1416
                     "Client close_notify", ctx->pull_send_buffer(), vars.get_req_bin("Record_Client_CloseNotify"));
4✔
1417

1418
                  result.require("client cannot send application traffic anymore", !ctx->client.is_active());
1✔
1419
                  result.require("client is not fully closed yet", !ctx->client.is_closed());
1✔
1420

1421
                  ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1422

1423
                  result.confirm("client connection was terminated", ctx->client.is_closed());
2✔
1424
               }),
1✔
1425
         };
4✔
1426
      }
1✔
1427
};
1428

1429
class Test_TLS_RFC8448_Server : public Test_TLS_RFC8448 {
1✔
1430
   private:
1431
      std::string side() const override { return "Server"; }
5✔
1432

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

1436
         // 32 - for server hello random
1437
         // 32 - for KeyShare (eph. x25519 key pair)  --  I guess?
1438
         //  4 - for ticket_age_add (in New Session Ticket)
1439
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1440

1441
         std::unique_ptr<Server_Context> ctx;
1✔
1442

1443
         return {
1✔
1444
            Botan_Tests::CHECK("Send Client Hello",
1445
                               [&](Test::Result& result) {
1✔
1446
                                  auto add_early_data_and_sort = [&](Botan::TLS::Extensions& exts,
5✔
1447
                                                                     Botan::TLS::Connection_Side side,
1448
                                                                     Botan::TLS::Handshake_Type type) {
1449
                                     if(type == Handshake_Type::NewSessionTicket) {
4✔
1450
                                        exts.add(new EarlyDataIndication(1024));
1✔
1451
                                     }
1452
                                     sort_server_extensions(exts, side, type);
4✔
1453
                                  };
4✔
1454

1455
                                  ctx = std::make_unique<Server_Context>(
2✔
1456
                                     std::move(rng),
1✔
1457
                                     std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
1✔
1458
                                     vars.get_req_u64("CurrentTimestamp"),
5✔
1459
                                     add_early_data_and_sort,
1460
                                     make_mock_signatures(vars),
1✔
1461
                                     false,
2✔
1462
                                     std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1463
                                               Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1464
                                  result.confirm("server not closed", !ctx->server.is_closed());
2✔
1465

1466
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1467

1468
                                  ctx->check_callback_invocations(result,
16✔
1469
                                                                  "client hello received",
1470
                                                                  {"tls_emit_data",
1471
                                                                   "tls_examine_extensions_client_hello",
1472
                                                                   "tls_modify_extensions_server_hello",
1473
                                                                   "tls_modify_extensions_encrypted_extensions",
1474
                                                                   "tls_modify_extensions_certificate",
1475
                                                                   "tls_sign_message",
1476
                                                                   "tls_generate_ephemeral_key",
1477
                                                                   "tls_ephemeral_key_agreement",
1478
                                                                   "tls_inspect_handshake_msg_client_hello",
1479
                                                                   "tls_inspect_handshake_msg_server_hello",
1480
                                                                   "tls_inspect_handshake_msg_encrypted_extensions",
1481
                                                                   "tls_inspect_handshake_msg_certificate",
1482
                                                                   "tls_inspect_handshake_msg_certificate_verify",
1483
                                                                   "tls_inspect_handshake_msg_finished"});
1484
                               }),
1✔
1485

1486
            Botan_Tests::CHECK("Verify generated messages in server's first flight",
1487
                               [&](Test::Result& result) {
1✔
1488
                                  result.require("ctx is available", ctx != nullptr);
2✔
1489
                                  const auto& msgs = ctx->observed_handshake_messages();
1✔
1490

1491
                                  result.test_eq("Server Hello",
2✔
1492
                                                 msgs.at("server_hello")[0],
2✔
1493
                                                 strip_message_header(vars.get_opt_bin("Message_ServerHello")));
4✔
1494
                                  result.test_eq("Encrypted Extensions",
3✔
1495
                                                 msgs.at("encrypted_extensions")[0],
2✔
1496
                                                 strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1497
                                  result.test_eq("Certificate",
3✔
1498
                                                 msgs.at("certificate")[0],
2✔
1499
                                                 strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
1500
                                  result.test_eq(
3✔
1501
                                     "CertificateVerify",
1502
                                     msgs.at("certificate_verify")[0],
2✔
1503
                                     strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
1504

1505
                                  result.test_eq("Server's entire first flight",
3✔
1506
                                                 ctx->pull_send_buffer(),
2✔
1507
                                                 concat(vars.get_req_bin("Record_ServerHello"),
4✔
1508
                                                        vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1509

1510
                                  result.confirm("Server can now send application data", ctx->server.is_active());
3✔
1511
                               }),
1✔
1512

1513
            Botan_Tests::CHECK("Send Client Finished",
1514
                               [&](Test::Result& result) {
1✔
1515
                                  result.require("ctx is available", ctx != nullptr);
1✔
1516
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
1517

1518
                                  ctx->check_callback_invocations(result,
6✔
1519
                                                                  "client finished received",
1520
                                                                  {"tls_inspect_handshake_msg_finished",
1521
                                                                   "tls_current_timestamp",
1522
                                                                   "tls_session_established",
1523
                                                                   "tls_session_activated"});
1524
                               }),
1✔
1525

1526
            Botan_Tests::CHECK("Send Session Ticket",
1527
                               [&](Test::Result& result) {
1✔
1528
                                  result.require("ctx is available", ctx != nullptr);
1✔
1529
                                  const auto new_tickets = ctx->server.send_new_session_tickets(1);
1✔
1530

1531
                                  result.test_eq("session ticket was sent", new_tickets, 1);
1✔
1532

1533
                                  ctx->check_callback_invocations(result,
7✔
1534
                                                                  "issued new session ticket",
1535
                                                                  {"tls_inspect_handshake_msg_new_session_ticket",
1536
                                                                   "tls_current_timestamp",
1537
                                                                   "tls_emit_data",
1538
                                                                   "tls_modify_extensions_new_session_ticket",
1539
                                                                   "tls_should_persist_resumption_information"});
1540
                               }),
1✔
1541

1542
            Botan_Tests::CHECK("Verify generated new session ticket message",
1543
                               [&](Test::Result& result) {
1✔
1544
                                  result.require("ctx is available", ctx != nullptr);
2✔
1545
                                  result.test_eq("New Session Ticket",
2✔
1546
                                                 ctx->pull_send_buffer(),
2✔
1547
                                                 vars.get_req_bin("Record_NewSessionTicket"));
2✔
1548
                               }),
1✔
1549

1550
            Botan_Tests::CHECK(
1551
               "Receive Application Data",
1552
               [&](Test::Result& result) {
1✔
1553
                  result.require("ctx is available", ctx != nullptr);
1✔
1554
                  ctx->server.received_data(vars.get_req_bin("Record_Client_AppData"));
2✔
1555
                  ctx->check_callback_invocations(result, "application data received", {"tls_record_received"});
3✔
1556

1557
                  const auto rcvd = ctx->pull_receive_buffer();
1✔
1558
                  result.test_eq("decrypted application traffic", rcvd, vars.get_req_bin("Client_AppData"));
3✔
1559
                  result.test_is_eq("sequence number", ctx->last_received_seq_no(), uint64_t(0));
2✔
1560
               }),
1✔
1561

1562
            Botan_Tests::CHECK("Send Application Data",
1563
                               [&](Test::Result& result) {
1✔
1564
                                  result.require("ctx is available", ctx != nullptr);
2✔
1565
                                  ctx->send(vars.get_req_bin("Server_AppData"));
3✔
1566

1567
                                  ctx->check_callback_invocations(result, "application data sent", {"tls_emit_data"});
3✔
1568

1569
                                  result.test_eq("correct server application data",
3✔
1570
                                                 ctx->pull_send_buffer(),
2✔
1571
                                                 vars.get_req_bin("Record_Server_AppData"));
2✔
1572
                               }),
1✔
1573

1574
            Botan_Tests::CHECK("Receive Client's close_notify",
1575
                               [&](Test::Result& result) {
1✔
1576
                                  result.require("ctx is available", ctx != nullptr);
1✔
1577
                                  ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
1578

1579
                                  ctx->check_callback_invocations(
4✔
1580
                                     result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1581

1582
                                  result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
1583
                                  result.confirm("connection is still active", ctx->server.is_active());
2✔
1584
                               }),
1✔
1585

1586
            Botan_Tests::CHECK("Expect Server close_notify",
1587
                               [&](Test::Result& result) {
1✔
1588
                                  result.require("ctx is available", ctx != nullptr);
2✔
1589
                                  ctx->server.close();
1✔
1590

1591
                                  result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
1592
                                  result.confirm("connection is now closed", ctx->server.is_closed());
2✔
1593
                                  result.test_eq("Server's close notify",
2✔
1594
                                                 ctx->pull_send_buffer(),
2✔
1595
                                                 vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1596
                               }),
1✔
1597
         };
10✔
1598
      }
1✔
1599

1600
      std::vector<Test::Result> resumed_handshake_with_0_rtt(const VarMap& vars) override {
1✔
1601
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>();
1✔
1602

1603
         // 32 - for server hello random
1604
         // 32 - for KeyShare (eph. x25519 key pair)
1605
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1606

1607
         std::unique_ptr<Server_Context> ctx;
1✔
1608

1609
         return {
1✔
1610
            Botan_Tests::CHECK("Receive Client Hello",
1611
                               [&](Test::Result& result) {
1✔
1612
                                  auto add_cookie_and_sort = [&](Botan::TLS::Extensions& exts,
3✔
1613
                                                                 Botan::TLS::Connection_Side side,
1614
                                                                 Botan::TLS::Handshake_Type type) {
1615
                                     if(type == Handshake_Type::EncryptedExtensions) {
2✔
1616
                                        exts.add(new EarlyDataIndication());
1✔
1617
                                     }
1618
                                     sort_server_extensions(exts, side, type);
2✔
1619
                                  };
2✔
1620

1621
                                  ctx = std::make_unique<Server_Context>(
2✔
1622
                                     std::move(rng),
1✔
1623
                                     std::make_shared<RFC8448_Text_Policy>("rfc8448_1rtt"),
1✔
1624
                                     vars.get_req_u64("CurrentTimestamp"),
5✔
1625
                                     add_cookie_and_sort,
1626
                                     make_mock_signatures(vars),
1✔
1627
                                     false,
2✔
1628
                                     std::pair{Botan::TLS::Session(vars.get_req_bin("Client_SessionData")),
5✔
1629
                                               Botan::TLS::Session_Ticket(vars.get_req_bin("SessionTicket"))});
3✔
1630
                                  result.confirm("server not closed", !ctx->server.is_closed());
2✔
1631

1632
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1633

1634
                                  ctx->check_callback_invocations(result,
13✔
1635
                                                                  "client hello received",
1636
                                                                  {
1637
                                                                     "tls_emit_data",
1638
                                                                     "tls_current_timestamp",
1639
                                                                     "tls_generate_ephemeral_key",
1640
                                                                     "tls_ephemeral_key_agreement",
1641
                                                                     "tls_examine_extensions_client_hello",
1642
                                                                     "tls_modify_extensions_server_hello",
1643
                                                                     "tls_modify_extensions_encrypted_extensions",
1644
                                                                     "tls_inspect_handshake_msg_client_hello",
1645
                                                                     "tls_inspect_handshake_msg_server_hello",
1646
                                                                     "tls_inspect_handshake_msg_encrypted_extensions",
1647
                                                                     "tls_inspect_handshake_msg_finished",
1648
                                                                  });
1649
                               }),
1✔
1650

1651
            Botan_Tests::CHECK("Verify generated messages in server's first flight",
1652
                               [&](Test::Result& result) {
1✔
1653
                                  result.require("ctx is available", ctx != nullptr);
2✔
1654
                                  const auto& msgs = ctx->observed_handshake_messages();
1✔
1655

1656
                                  result.test_eq("Server Hello",
2✔
1657
                                                 msgs.at("server_hello")[0],
2✔
1658
                                                 strip_message_header(vars.get_opt_bin("Message_ServerHello")));
4✔
1659
                                  result.test_eq("Encrypted Extensions",
3✔
1660
                                                 msgs.at("encrypted_extensions")[0],
2✔
1661
                                                 strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1662

1663
                                  result.test_eq("Server's entire first flight",
3✔
1664
                                                 ctx->pull_send_buffer(),
2✔
1665
                                                 concat(vars.get_req_bin("Record_ServerHello"),
4✔
1666
                                                        vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1667

1668
                                  result.confirm("Server can now send application data", ctx->server.is_active());
3✔
1669
                               }),
1✔
1670

1671
            // TODO: The rest of this test vector requires 0-RTT which is not
1672
            //       yet implemented. For now we can only test the server's
1673
            //       ability to acknowledge a session resumption via PSK.
1674
         };
3✔
1675
      }
1✔
1676

1677
      std::vector<Test::Result> hello_retry_request(const VarMap& vars) override {
1✔
1678
         // Fallback RNG is required to for blinding in ECDH with P-256
1679
         auto& fallback_rng = Test::rng();
1✔
1680
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>(fallback_rng);
1✔
1681

1682
         // 32 - for server hello random
1683
         // 32 - for KeyShare (eph. P-256 key pair)
1684
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1685

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

1688
         return {
1✔
1689
            Botan_Tests::CHECK("Receive Client Hello",
1690
                               [&](Test::Result& result) {
1✔
1691
                                  auto add_cookie_and_sort = [&](Botan::TLS::Extensions& exts,
5✔
1692
                                                                 Botan::TLS::Connection_Side side,
1693
                                                                 Botan::TLS::Handshake_Type type) {
1694
                                     if(type == Handshake_Type::HelloRetryRequest) {
4✔
1695
                                        // This cookie needs to be mocked into the HRR since RFC 8448 contains it.
1696
                                        exts.add(new Cookie(vars.get_opt_bin("HelloRetryRequest_Cookie")));
5✔
1697
                                     }
1698
                                     sort_server_extensions(exts, side, type);
4✔
1699
                                  };
4✔
1700

1701
                                  ctx = std::make_unique<Server_Context>(
2✔
1702
                                     std::move(rng),
1✔
1703
                                     std::make_shared<RFC8448_Text_Policy>("rfc8448_hrr_server"),
1✔
1704
                                     vars.get_req_u64("CurrentTimestamp"),
2✔
1705
                                     add_cookie_and_sort,
1706
                                     make_mock_signatures(vars));
3✔
1707
                                  result.confirm("server not closed", !ctx->server.is_closed());
2✔
1708

1709
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1710

1711
                                  ctx->check_callback_invocations(result,
7✔
1712
                                                                  "client hello received",
1713
                                                                  {"tls_emit_data",
1714
                                                                   "tls_examine_extensions_client_hello",
1715
                                                                   "tls_modify_extensions_hello_retry_request",
1716
                                                                   "tls_inspect_handshake_msg_client_hello",
1717
                                                                   "tls_inspect_handshake_msg_hello_retry_request"});
1718
                               }),
1✔
1719

1720
            Botan_Tests::CHECK("Verify generated Hello Retry Request message",
1721
                               [&](Test::Result& result) {
1✔
1722
                                  result.require("ctx is available", ctx != nullptr);
2✔
1723
                                  result.test_eq("Server's Hello Retry Request record",
2✔
1724
                                                 ctx->pull_send_buffer(),
2✔
1725
                                                 vars.get_req_bin("Record_HelloRetryRequest"));
2✔
1726
                                  result.confirm("TLS handshake not yet finished", !ctx->server.is_active());
2✔
1727
                               }),
1✔
1728

1729
            Botan_Tests::CHECK("Receive updated Client Hello message",
1730
                               [&](Test::Result& result) {
1✔
1731
                                  result.require("ctx is available", ctx != nullptr);
1✔
1732
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_2"));
2✔
1733

1734
                                  ctx->check_callback_invocations(result,
16✔
1735
                                                                  "updated client hello received",
1736
                                                                  {"tls_emit_data",
1737
                                                                   "tls_examine_extensions_client_hello",
1738
                                                                   "tls_modify_extensions_server_hello",
1739
                                                                   "tls_modify_extensions_encrypted_extensions",
1740
                                                                   "tls_modify_extensions_certificate",
1741
                                                                   "tls_sign_message",
1742
                                                                   "tls_generate_ephemeral_key",
1743
                                                                   "tls_ephemeral_key_agreement",
1744
                                                                   "tls_inspect_handshake_msg_client_hello",
1745
                                                                   "tls_inspect_handshake_msg_server_hello",
1746
                                                                   "tls_inspect_handshake_msg_encrypted_extensions",
1747
                                                                   "tls_inspect_handshake_msg_certificate",
1748
                                                                   "tls_inspect_handshake_msg_certificate_verify",
1749
                                                                   "tls_inspect_handshake_msg_finished"});
1750
                               }),
1✔
1751

1752
            Botan_Tests::CHECK("Verify generated messages in server's second flight",
1753
                               [&](Test::Result& result) {
1✔
1754
                                  result.require("ctx is available", ctx != nullptr);
2✔
1755
                                  const auto& msgs = ctx->observed_handshake_messages();
1✔
1756

1757
                                  result.test_eq("Server Hello",
2✔
1758
                                                 msgs.at("server_hello")[0],
2✔
1759
                                                 strip_message_header(vars.get_opt_bin("Message_ServerHello")));
4✔
1760
                                  result.test_eq("Encrypted Extensions",
3✔
1761
                                                 msgs.at("encrypted_extensions")[0],
2✔
1762
                                                 strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1763
                                  result.test_eq("Certificate",
3✔
1764
                                                 msgs.at("certificate")[0],
2✔
1765
                                                 strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
1766
                                  result.test_eq(
3✔
1767
                                     "CertificateVerify",
1768
                                     msgs.at("certificate_verify")[0],
2✔
1769
                                     strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
1770
                                  result.test_eq("Finished",
3✔
1771
                                                 msgs.at("finished")[0],
2✔
1772
                                                 strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
1773

1774
                                  result.test_eq("Server's entire second flight",
3✔
1775
                                                 ctx->pull_send_buffer(),
2✔
1776
                                                 concat(vars.get_req_bin("Record_ServerHello"),
4✔
1777
                                                        vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1778
                                  result.confirm("Server could now send application data", ctx->server.is_active());
3✔
1779
                               }),
1✔
1780

1781
            Botan_Tests::CHECK("Receive Client Finished",
1782
                               [&](Test::Result& result) {
1✔
1783
                                  result.require("ctx is available", ctx != nullptr);
1✔
1784
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
1785

1786
                                  ctx->check_callback_invocations(result,
6✔
1787
                                                                  "client finished received",
1788
                                                                  {"tls_inspect_handshake_msg_finished",
1789
                                                                   "tls_current_timestamp",
1790
                                                                   "tls_session_established",
1791
                                                                   "tls_session_activated"});
1792

1793
                                  result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
1794
                               }),
1✔
1795

1796
            Botan_Tests::CHECK("Receive Client close_notify",
1797
                               [&](Test::Result& result) {
1✔
1798
                                  result.require("ctx is available", ctx != nullptr);
1✔
1799
                                  ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
1800

1801
                                  ctx->check_callback_invocations(
4✔
1802
                                     result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1803

1804
                                  result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
1805
                                  result.confirm("connection is still active", ctx->server.is_active());
2✔
1806
                               }),
1✔
1807

1808
            Botan_Tests::CHECK("Expect Server close_notify",
1809
                               [&](Test::Result& result) {
1✔
1810
                                  result.require("ctx is available", ctx != nullptr);
2✔
1811
                                  ctx->server.close();
1✔
1812

1813
                                  result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
1814
                                  result.confirm("connection is now closed", ctx->server.is_closed());
2✔
1815
                                  result.test_eq("Server's close notify",
2✔
1816
                                                 ctx->pull_send_buffer(),
2✔
1817
                                                 vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1818
                               }),
1✔
1819

1820
         };
8✔
1821
      }
1✔
1822

1823
      std::vector<Test::Result> client_authentication(const VarMap& vars) override {
1✔
1824
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>("");
1✔
1825

1826
         // 32 - for server hello random
1827
         // 32 - for KeyShare (eph. x25519 pair)
1828
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1829

1830
         std::unique_ptr<Server_Context> ctx;
1✔
1831

1832
         return {
1✔
1833
            Botan_Tests::CHECK("Receive Client Hello",
1834
                               [&](Test::Result& result) {
1✔
1835
                                  ctx = std::make_unique<Server_Context>(
2✔
1836
                                     std::move(rng),
1✔
1837
                                     std::make_shared<RFC8448_Text_Policy>("rfc8448_client_auth_server"),
1✔
1838
                                     vars.get_req_u64("CurrentTimestamp"),
3✔
1839
                                     sort_server_extensions,
1840
                                     make_mock_signatures(vars),
1✔
1841
                                     true /* use alternative certificate */);
2✔
1842
                                  result.confirm("server not closed", !ctx->server.is_closed());
2✔
1843

1844
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1845

1846
                                  ctx->check_callback_invocations(result,
17✔
1847
                                                                  "client hello received",
1848
                                                                  {"tls_emit_data",
1849
                                                                   "tls_examine_extensions_client_hello",
1850
                                                                   "tls_modify_extensions_server_hello",
1851
                                                                   "tls_modify_extensions_encrypted_extensions",
1852
                                                                   "tls_modify_extensions_certificate",
1853
                                                                   "tls_sign_message",
1854
                                                                   "tls_generate_ephemeral_key",
1855
                                                                   "tls_ephemeral_key_agreement",
1856
                                                                   "tls_inspect_handshake_msg_client_hello",
1857
                                                                   "tls_inspect_handshake_msg_server_hello",
1858
                                                                   "tls_inspect_handshake_msg_encrypted_extensions",
1859
                                                                   "tls_inspect_handshake_msg_certificate_request",
1860
                                                                   "tls_inspect_handshake_msg_certificate",
1861
                                                                   "tls_inspect_handshake_msg_certificate_verify",
1862
                                                                   "tls_inspect_handshake_msg_finished"});
1863
                               }),
1✔
1864

1865
            Botan_Tests::CHECK(
1866
               "Verify server's generated handshake messages",
1867
               [&](Test::Result& result) {
1✔
1868
                  result.require("ctx is available", ctx != nullptr);
2✔
1869
                  const auto& msgs = ctx->observed_handshake_messages();
1✔
1870

1871
                  result.test_eq("Server Hello",
2✔
1872
                                 msgs.at("server_hello")[0],
2✔
1873
                                 strip_message_header(vars.get_opt_bin("Message_ServerHello")));
4✔
1874
                  result.test_eq("Encrypted Extensions",
3✔
1875
                                 msgs.at("encrypted_extensions")[0],
2✔
1876
                                 strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
1877
                  result.test_eq("Certificate Request",
3✔
1878
                                 msgs.at("certificate_request")[0],
2✔
1879
                                 strip_message_header(vars.get_opt_bin("Message_CertificateRequest")));
3✔
1880
                  result.test_eq("Certificate",
3✔
1881
                                 msgs.at("certificate")[0],
2✔
1882
                                 strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
1883
                  result.test_eq("CertificateVerify",
3✔
1884
                                 msgs.at("certificate_verify")[0],
2✔
1885
                                 strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
1886
                  result.test_eq("Finished",
3✔
1887
                                 msgs.at("finished")[0],
2✔
1888
                                 strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
1889

1890
                  result.test_eq("Server's entire first flight",
3✔
1891
                                 ctx->pull_send_buffer(),
2✔
1892
                                 concat(vars.get_req_bin("Record_ServerHello"),
4✔
1893
                                        vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
1894

1895
                  result.confirm("Not yet aware of client's cert chain", ctx->server.peer_cert_chain().empty());
3✔
1896
                  result.confirm("Server could now send application data", ctx->server.is_active());
3✔
1897
               }),
1✔
1898

1899
            Botan_Tests::CHECK("Receive Client's second flight",
1900
                               [&](Test::Result& result) {
1✔
1901
                                  result.require("ctx is available", ctx != nullptr);
1✔
1902
                                  // This encrypted message contains the following messages:
1903
                                  // * client's Certificate message
1904
                                  // * client's Certificate_Verify message
1905
                                  // * client's Finished message
1906
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
1907

1908
                                  ctx->check_callback_invocations(result,
11✔
1909
                                                                  "client finished received",
1910
                                                                  {"tls_inspect_handshake_msg_certificate",
1911
                                                                   "tls_inspect_handshake_msg_certificate_verify",
1912
                                                                   "tls_inspect_handshake_msg_finished",
1913
                                                                   "tls_examine_extensions_certificate",
1914
                                                                   "tls_verify_cert_chain",
1915
                                                                   "tls_verify_message",
1916
                                                                   "tls_current_timestamp",
1917
                                                                   "tls_session_established",
1918
                                                                   "tls_session_activated"});
1919

1920
                                  const auto cert_chain = ctx->server.peer_cert_chain();
1✔
1921
                                  result.confirm("Received client's cert chain",
2✔
1922
                                                 !cert_chain.empty() && cert_chain.front() == client_certificate());
2✔
1923

1924
                                  result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
1925
                               }),
1✔
1926

1927
            Botan_Tests::CHECK("Receive Client close_notify",
1928
                               [&](Test::Result& result) {
1✔
1929
                                  result.require("ctx is available", ctx != nullptr);
1✔
1930
                                  ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
1931

1932
                                  ctx->check_callback_invocations(
4✔
1933
                                     result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
1934

1935
                                  result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
1936
                                  result.confirm("connection is still active", ctx->server.is_active());
2✔
1937
                               }),
1✔
1938

1939
            Botan_Tests::CHECK("Expect Server close_notify",
1940
                               [&](Test::Result& result) {
1✔
1941
                                  result.require("ctx is available", ctx != nullptr);
2✔
1942
                                  ctx->server.close();
1✔
1943

1944
                                  result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
1945
                                  result.confirm("connection is now closed", ctx->server.is_closed());
2✔
1946
                                  result.test_eq("Server's close notify",
2✔
1947
                                                 ctx->pull_send_buffer(),
2✔
1948
                                                 vars.get_req_bin("Record_Server_CloseNotify"));
2✔
1949
                               }),
1✔
1950

1951
         };
6✔
1952
      }
1✔
1953

1954
      std::vector<Test::Result> middlebox_compatibility(const VarMap& vars) override {
1✔
1955
         auto rng = std::make_unique<Botan_Tests::Fixed_Output_RNG>("");
1✔
1956

1957
         // 32 - for server hello random
1958
         // 32 - for KeyShare (eph. x25519 pair)
1959
         add_entropy(*rng, vars.get_req_bin("Server_RNG_Pool"));
2✔
1960

1961
         std::unique_ptr<Server_Context> ctx;
1✔
1962

1963
         return {
1✔
1964
            Botan_Tests::CHECK("Receive Client Hello",
1965
                               [&](Test::Result& result) {
1✔
1966
                                  ctx = std::make_unique<Server_Context>(
2✔
1967
                                     std::move(rng),
1✔
1968
                                     std::make_shared<RFC8448_Text_Policy>("rfc8448_compat_server"),
1✔
1969
                                     vars.get_req_u64("CurrentTimestamp"),
3✔
1970
                                     sort_server_extensions,
1971
                                     make_mock_signatures(vars));
3✔
1972
                                  result.confirm("server not closed", !ctx->server.is_closed());
2✔
1973

1974
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
2✔
1975

1976
                                  ctx->check_callback_invocations(result,
16✔
1977
                                                                  "client hello received",
1978
                                                                  {"tls_emit_data",
1979
                                                                   "tls_examine_extensions_client_hello",
1980
                                                                   "tls_modify_extensions_server_hello",
1981
                                                                   "tls_modify_extensions_encrypted_extensions",
1982
                                                                   "tls_modify_extensions_certificate",
1983
                                                                   "tls_sign_message",
1984
                                                                   "tls_generate_ephemeral_key",
1985
                                                                   "tls_ephemeral_key_agreement",
1986
                                                                   "tls_inspect_handshake_msg_client_hello",
1987
                                                                   "tls_inspect_handshake_msg_server_hello",
1988
                                                                   "tls_inspect_handshake_msg_encrypted_extensions",
1989
                                                                   "tls_inspect_handshake_msg_certificate",
1990
                                                                   "tls_inspect_handshake_msg_certificate_verify",
1991
                                                                   "tls_inspect_handshake_msg_finished"});
1992
                               }),
1✔
1993

1994
            Botan_Tests::CHECK("Verify server's generated handshake messages",
1995
                               [&](Test::Result& result) {
1✔
1996
                                  result.require("ctx is available", ctx != nullptr);
2✔
1997
                                  const auto& msgs = ctx->observed_handshake_messages();
1✔
1998

1999
                                  result.test_eq("Server Hello",
2✔
2000
                                                 msgs.at("server_hello")[0],
2✔
2001
                                                 strip_message_header(vars.get_opt_bin("Message_ServerHello")));
4✔
2002
                                  result.test_eq("Encrypted Extensions",
3✔
2003
                                                 msgs.at("encrypted_extensions")[0],
2✔
2004
                                                 strip_message_header(vars.get_opt_bin("Message_EncryptedExtensions")));
3✔
2005
                                  result.test_eq("Certificate",
3✔
2006
                                                 msgs.at("certificate")[0],
2✔
2007
                                                 strip_message_header(vars.get_opt_bin("Message_Server_Certificate")));
3✔
2008
                                  result.test_eq(
3✔
2009
                                     "CertificateVerify",
2010
                                     msgs.at("certificate_verify")[0],
2✔
2011
                                     strip_message_header(vars.get_opt_bin("Message_Server_CertificateVerify")));
3✔
2012
                                  result.test_eq("Finished",
3✔
2013
                                                 msgs.at("finished")[0],
2✔
2014
                                                 strip_message_header(vars.get_opt_bin("Message_Server_Finished")));
3✔
2015

2016
                                  // Those records contain the required Change Cipher Spec message the server must produce for compatibility mode compliance
2017
                                  result.test_eq("Server's entire first flight",
3✔
2018
                                                 ctx->pull_send_buffer(),
2✔
2019
                                                 concat(vars.get_req_bin("Record_ServerHello"),
4✔
2020
                                                        vars.get_req_bin("Record_ServerHandshakeMessages")));
2✔
2021

2022
                                  result.confirm("Server could now send application data", ctx->server.is_active());
3✔
2023
                               }),
1✔
2024

2025
            Botan_Tests::CHECK("Receive Client Finished",
2026
                               [&](Test::Result& result) {
1✔
2027
                                  result.require("ctx is available", ctx != nullptr);
1✔
2028
                                  ctx->server.received_data(vars.get_req_bin("Record_ClientFinished"));
2✔
2029

2030
                                  ctx->check_callback_invocations(result,
6✔
2031
                                                                  "client finished received",
2032
                                                                  {"tls_inspect_handshake_msg_finished",
2033
                                                                   "tls_current_timestamp",
2034
                                                                   "tls_session_established",
2035
                                                                   "tls_session_activated"});
2036

2037
                                  result.confirm("TLS handshake finished", ctx->server.is_active());
2✔
2038
                               }),
1✔
2039

2040
            Botan_Tests::CHECK("Receive Client close_notify",
2041
                               [&](Test::Result& result) {
1✔
2042
                                  result.require("ctx is available", ctx != nullptr);
1✔
2043
                                  ctx->server.received_data(vars.get_req_bin("Record_Client_CloseNotify"));
2✔
2044

2045
                                  ctx->check_callback_invocations(
4✔
2046
                                     result, "client finished received", {"tls_alert", "tls_peer_closed_connection"});
2047

2048
                                  result.confirm("connection is not yet closed", !ctx->server.is_closed());
2✔
2049
                                  result.confirm("connection is still active", ctx->server.is_active());
2✔
2050
                               }),
1✔
2051

2052
            Botan_Tests::CHECK("Expect Server close_notify",
2053
                               [&](Test::Result& result) {
1✔
2054
                                  result.require("ctx is available", ctx != nullptr);
2✔
2055
                                  ctx->server.close();
1✔
2056

2057
                                  result.confirm("connection is now inactive", !ctx->server.is_active());
2✔
2058
                                  result.confirm("connection is now closed", ctx->server.is_closed());
2✔
2059
                                  result.test_eq("Server's close notify",
2✔
2060
                                                 ctx->pull_send_buffer(),
2✔
2061
                                                 vars.get_req_bin("Record_Server_CloseNotify"));
2✔
2062
                               }),
1✔
2063

2064
         };
6✔
2065
      }
1✔
2066
};
2067

2068
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2069
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2070

2071
#endif
2072

2073
}
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