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

randombit / botan / 22061970273

16 Feb 2026 12:03PM UTC coverage: 90.044% (+0.007%) from 90.037%
22061970273

push

github

web-flow
Merge pull request #5345 from randombit/jack/min-test-rng-h

Minimize includes into test_rng.h

102336 of 113651 relevant lines covered (90.04%)

11395171.59 hits per line

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

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

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

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

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

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

46
namespace Botan_Tests {
47

48
#if defined(BOTAN_CAN_RUN_TEST_TLS_RFC8448)
49

50
namespace {
51

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

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

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

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

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

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

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

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

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

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

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

113
   private:
114
      size_t m_padding_bytes;
115
};
116

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

428
         return m_server_private_key;
3✔
429
      }
430

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

542
   private:
543
      bool m_rfc8448;
544
};
545

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

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

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

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

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

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

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

592
         return handle;
1✔
593
      }
594

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

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

613
         return found_sessions;
7✔
614
      }
×
615

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

849
   return result;
9✔
850
}
×
851

852
}  // namespace
853

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1145
               add_renegotiation_extension(exts);
1✔
1146

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1449
         std::unique_ptr<Client_Context> ctx;
1✔
1450

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

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

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

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

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

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

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

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

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

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

1522
                     ctx->client.received_data(vars.get_req_bin("Record_Server_CloseNotify"));
1✔
1523

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

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

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

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

1555
         std::unique_ptr<Client_Context> ctx;
1✔
1556

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1693
         std::unique_ptr<Client_Context> ctx;
1✔
1694

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1806
         std::unique_ptr<Server_Context> ctx;
1✔
1807

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

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

1831
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
1832

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

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

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

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

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

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

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

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

1901
                     result.test_sz_eq("session ticket was sent", new_tickets, 1);
1✔
1902

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1977
         std::unique_ptr<Server_Context> ctx;
1✔
1978

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

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

2002
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2003

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

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

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

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

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

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

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

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

2062
         std::unique_ptr<Server_Context> ctx;
1✔
2063

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

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

2085
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2086

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2201
         };
8✔
2202
      }
2✔
2203

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

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

2211
         std::unique_ptr<Server_Context> ctx;
1✔
2212

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

2225
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2226

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2337
         };
6✔
2338
      }
2✔
2339

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

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

2347
         std::unique_ptr<Server_Context> ctx;
1✔
2348

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

2360
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2361

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

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

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

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

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

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

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

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

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

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

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

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

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

2454
         };
6✔
2455
      }
2✔
2456

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

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

2464
         std::unique_ptr<Server_Context> ctx;
1✔
2465

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

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

2505
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2506

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

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

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

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

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

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

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

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

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

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

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

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

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

2590
                     ctx->server.close();
1✔
2591

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

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

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

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

2633
         std::unique_ptr<Server_Context> ctx;
1✔
2634

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

2645
                     ctx->server.received_data(vars.get_req_bin("Record_ClientHello_1"));
1✔
2646

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2761
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_client", Test_TLS_RFC8448_Client);
2762
BOTAN_REGISTER_TEST("tls", "tls_rfc8448_server", Test_TLS_RFC8448_Server);
2763

2764
#endif
2765

2766
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc