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

randombit / botan / 22093983912

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

push

github

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

Changes to reduce dependencies in TLS sources/headers

102345 of 113683 relevant lines covered (90.03%)

11397934.0 hits per line

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

87.21
/src/tests/test_tls.cpp
1
/*
2
* (C) 2014,2015,2017,2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8
#include <fstream>
9
#include <memory>
10

11
#if defined(BOTAN_HAS_TLS)
12
   #include "test_rng.h"
13

14
   #include <botan/mem_ops.h>
15
   #include <botan/tls_alert.h>
16
   #include <botan/tls_policy.h>
17
   #include <botan/tls_session.h>
18
   #include <botan/tls_signature_scheme.h>
19
   #include <botan/tls_version.h>
20
   #include <botan/x509cert.h>
21
   #include <botan/internal/fmt.h>
22
   #include <set>
23

24
   #if defined(BOTAN_HAS_TLS_CBC)
25
      #include <botan/block_cipher.h>
26
      #include <botan/mac.h>
27
      #include <botan/internal/tls_cbc.h>
28
   #endif
29

30
   #if defined(BOTAN_HAS_TLS_NULL)
31
      #include <botan/internal/tls_null.h>
32
   #endif
33

34
#endif
35

36
namespace Botan_Tests {
37

38
#if defined(BOTAN_HAS_TLS)
39

40
class TLS_Session_Tests final : public Test {
1✔
41
   public:
42
      std::vector<Test::Result> run() override {
1✔
43
         Test::Result result("TLS::Session");
1✔
44

45
         const Botan::TLS::Session session(Botan::secure_vector<uint8_t>{0xCC, 0xDD},
1✔
46
                                           Botan::TLS::Protocol_Version::TLS_V12,
47
                                           0xC02F,
48
                                           Botan::TLS::Connection_Side::Client,
49
                                           true,
50
                                           false,
51
                                           std::vector<Botan::X509_Certificate>(),
1✔
52
                                           Botan::TLS::Server_Information("server"),
1✔
53
                                           0x0000,
54
                                           std::chrono::system_clock::now());
2✔
55

56
         const std::string pem = session.PEM_encode();
1✔
57
         const Botan::TLS::Session session_from_pem(pem);
1✔
58
         result.test_bin_eq("Roundtrip from pem", session.DER_encode(), session_from_pem.DER_encode());
2✔
59

60
         const auto der = session.DER_encode();
1✔
61
         const Botan::TLS::Session session_from_der(der);
1✔
62
         result.test_bin_eq("Roundtrip from der", session.DER_encode(), session_from_der.DER_encode());
2✔
63

64
         const Botan::SymmetricKey key("ABCDEF");
1✔
65
         const std::vector<uint8_t> ctext1 = session.encrypt(key, this->rng());
1✔
66
         const std::vector<uint8_t> ctext2 = session.encrypt(key, this->rng());
1✔
67

68
         result.test_bin_ne("TLS session encryption is non-deterministic", ctext1, ctext2);
1✔
69

70
         result.test_bin_eq(
1✔
71
            "TLS session encryption same header", std::span{ctext1}.first(12), "068B5A9D396C0000F2322CAE");
72
         result.test_bin_eq(
1✔
73
            "TLS session encryption same header", std::span{ctext2}.first(12), "068B5A9D396C0000F2322CAE");
74

75
         const Botan::TLS::Session dsession = Botan::TLS::Session::decrypt(ctext1.data(), ctext1.size(), key);
1✔
76

77
         Fixed_Output_RNG frng1("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
78
         const std::vector<uint8_t> ctextf1 = session.encrypt(key, frng1);
1✔
79
         Fixed_Output_RNG frng2("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
80
         const std::vector<uint8_t> ctextf2 = session.encrypt(key, frng2);
1✔
81

82
         result.test_bin_eq("Only randomness comes from RNG", ctextf1, ctextf2);
1✔
83

84
         const Botan::TLS::Session session2(Botan::secure_vector<uint8_t>{0xCC, 0xEE},
1✔
85
                                            Botan::TLS::Protocol_Version::TLS_V12,
86
                                            0xBAAD,  // cipher suite does not exist
87
                                            Botan::TLS::Connection_Side::Client,
88
                                            true,
89
                                            false,
90
                                            std::vector<Botan::X509_Certificate>(),
1✔
91
                                            Botan::TLS::Server_Information("server"),
1✔
92
                                            0x0000,
93
                                            std::chrono::system_clock::now());
2✔
94
         const std::string pem_with_unknown_ciphersuite = session2.PEM_encode();
1✔
95

96
         result.test_throws("unknown ciphersuite during session parsing",
1✔
97
                            "Serialized TLS session contains unknown cipher suite (47789)",
98
                            [&] { Botan::TLS::Session{pem_with_unknown_ciphersuite}; });
2✔
99

100
         return {result};
3✔
101
      }
5✔
102
};
103

104
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
105

106
   #if defined(BOTAN_HAS_TLS_CBC)
107

108
class TLS_CBC_Padding_Tests final : public Text_Based_Test {
×
109
   public:
110
      TLS_CBC_Padding_Tests() : Text_Based_Test("tls_cbc_padding.vec", "Record,Output") {}
2✔
111

112
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
22✔
113
         const std::vector<uint8_t> record = vars.get_req_bin("Record");
22✔
114
         const size_t output = vars.get_req_sz("Output");
22✔
115

116
         const uint16_t res = Botan::TLS::check_tls_cbc_padding(record.data(), record.size());
22✔
117

118
         Test::Result result("TLS CBC padding check");
22✔
119
         result.test_sz_eq("Expected", res, output);
22✔
120
         return result;
22✔
121
      }
22✔
122
};
123

124
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
125

126
class TLS_CBC_Tests final : public Text_Based_Test {
×
127
   public:
128
      class ZeroMac : public Botan::MessageAuthenticationCode {
129
         public:
130
            explicit ZeroMac(size_t mac_len) : m_mac_len(mac_len) {}
10✔
131

132
            void clear() override {}
×
133

134
            std::string name() const override { return "ZeroMac"; }
16✔
135

136
            size_t output_length() const override { return m_mac_len; }
20✔
137

138
            void add_data(std::span<const uint8_t> /*input*/) override {}
26✔
139

140
            void final_result(std::span<uint8_t> out) override {
10✔
141
               for(size_t i = 0; i != m_mac_len; ++i) {
206✔
142
                  out[i] = 0;
196✔
143
               }
144
            }
10✔
145

146
            bool has_keying_material() const override { return true; }
×
147

148
            Botan::Key_Length_Specification key_spec() const override {
20✔
149
               return Botan::Key_Length_Specification(0, 0, 1);
20✔
150
            }
151

152
            std::unique_ptr<MessageAuthenticationCode> new_object() const override {
×
153
               return std::make_unique<ZeroMac>(m_mac_len);
×
154
            }
155

156
         private:
157
            void key_schedule(std::span<const uint8_t> /* key */) override {}
10✔
158

159
            size_t m_mac_len;
160
      };
161

162
      class Noop_Block_Cipher : public Botan::BlockCipher {
163
         public:
164
            explicit Noop_Block_Cipher(size_t bs) : m_bs(bs) {}
10✔
165

166
            void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
×
167
               Botan::copy_mem(out, in, blocks * m_bs);
×
168
            }
×
169

170
            void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
10✔
171
               Botan::copy_mem(out, in, blocks * m_bs);
10✔
172
            }
10✔
173

174
            size_t block_size() const override { return m_bs; }
40✔
175

176
            void clear() override {}
×
177

178
            std::string name() const override { return "noop"; }
10✔
179

180
            bool has_keying_material() const override { return true; }
×
181

182
            Botan::Key_Length_Specification key_spec() const override {
30✔
183
               return Botan::Key_Length_Specification(0, 0, 1);
30✔
184
            }
185

186
            std::unique_ptr<BlockCipher> new_object() const override {
×
187
               return std::make_unique<Noop_Block_Cipher>(m_bs);
×
188
            }
189

190
         private:
191
            void key_schedule(std::span<const uint8_t> /*key*/) override {}
10✔
192

193
            size_t m_bs;
194
      };
195

196
      TLS_CBC_Tests() : Text_Based_Test("tls_cbc.vec", "Blocksize,MACsize,Record,Valid") {}
2✔
197

198
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
10✔
199
         Test::Result result("TLS CBC");
10✔
200

201
         const size_t block_size = vars.get_req_sz("Blocksize");
10✔
202
         const size_t mac_len = vars.get_req_sz("MACsize");
10✔
203
         const std::vector<uint8_t> record = vars.get_req_bin("Record");
10✔
204
         const bool is_valid = vars.get_req_sz("Valid") == 1;
10✔
205

206
         // todo test permutations
207
         const bool encrypt_then_mac = false;
10✔
208

209
         Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption tls_cbc(std::make_unique<Noop_Block_Cipher>(block_size),
30✔
210
                                                          std::make_unique<ZeroMac>(mac_len),
10✔
211
                                                          0,
212
                                                          0,
213
                                                          Botan::TLS::Protocol_Version::TLS_V12,
214
                                                          encrypt_then_mac);
20✔
215

216
         tls_cbc.set_key(std::vector<uint8_t>(0));
10✔
217
         std::vector<uint8_t> ad(13);
10✔
218
         tls_cbc.set_associated_data(ad.data(), ad.size());
10✔
219

220
         Botan::secure_vector<uint8_t> vec(record.begin(), record.end());
10✔
221

222
         try {
10✔
223
            tls_cbc.finish(vec, 0);
10✔
224
            if(is_valid) {
4✔
225
               result.test_success("Accepted valid TLS-CBC ciphertext");
4✔
226
            } else {
227
               result.test_failure("Accepted invalid TLS-CBC ciphertext");
×
228
            }
229
         } catch(std::exception&) {
6✔
230
            if(is_valid) {
6✔
231
               result.test_failure("Rejected valid TLS-CBC ciphertext");
×
232
            } else {
233
               result.test_success("Accepted invalid TLS-CBC ciphertext");
6✔
234
            }
235
         }
6✔
236

237
         return result;
20✔
238
      }
10✔
239
};
240

241
class TLS_CBC_KAT_Tests final : public Text_Based_Test {
×
242
   public:
243
      TLS_CBC_KAT_Tests() :
1✔
244
            Text_Based_Test(
245
               "tls_cbc_kat.vec",
246
               "BlockCipher,MAC,KeylenCipher,KeylenMAC,EncryptThenMAC,Protocol,Key,AssociatedData,Nonce,Plaintext,Ciphertext") {
2✔
247
      }
1✔
248

249
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
10✔
250
         Test::Result result("TLS CBC KAT");
10✔
251

252
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption>(result, vars);
10✔
253
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption>(result, vars);
10✔
254

255
         return result;
10✔
256
      }
×
257

258
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
10✔
259
         try {
10✔
260
            std::ignore = get_cipher_and_mac(vars);
10✔
261
            return false;
10✔
262
         } catch(const Botan::Lookup_Error&) {
×
263
            return true;
×
264
         }
×
265
      }
266

267
   private:
268
      [[nodiscard]] static std::pair<std::unique_ptr<Botan::BlockCipher>,
269
                                     std::unique_ptr<Botan::MessageAuthenticationCode>>
270
      get_cipher_and_mac(const VarMap& vars) {
30✔
271
         return {
30✔
272
            Botan::BlockCipher::create_or_throw(vars.get_req_str("BlockCipher")),
60✔
273
            Botan::MessageAuthenticationCode::create_or_throw(vars.get_req_str("MAC")),
30✔
274
         };
60✔
275
      }
276

277
      template <typename T>
278
         requires(std::same_as<T, Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption> ||
279
                  std::same_as<T, Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption>)
280
      static void run_kat(Test::Result& result, const VarMap& vars) {
20✔
281
         constexpr bool encrypt = std::same_as<T, Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption>;
20✔
282
         constexpr auto direction = [] {
20✔
283
            if constexpr(encrypt) {
284
               return "encryption";
285
            } else {
286
               return "decryption";
287
            }
288
         }();
289

290
         const auto keylen_cipher = vars.get_req_sz("KeylenCipher");
20✔
291
         const auto keylen_mac = vars.get_req_sz("KeylenMAC");
20✔
292
         const auto encrypt_then_mac = vars.get_req_bool("EncryptThenMAC");
20✔
293
         const auto protocol = [&] {
60✔
294
            const auto p = vars.get_req_str("Protocol");
20✔
295
            if(p == "TLS") {
20✔
296
               return Botan::TLS::Version_Code::TLS_V12;
297
            } else if(p == "DTLS") {
10✔
298
               return Botan::TLS::Version_Code::DTLS_V12;
299
            } else {
300
               throw Test_Error("unexpected protocol version");
×
301
            }
302
         }();
40✔
303

304
         const auto key = vars.get_req_bin("Key");
20✔
305
         const auto ad = vars.get_req_bin("AssociatedData");
20✔
306
         const auto nonce = vars.get_req_bin("Nonce");
20✔
307
         const auto pt = vars.get_req_bin("Plaintext");
20✔
308
         const auto ct = vars.get_req_bin("Ciphertext");
20✔
309

310
         auto [cipher, mac] = get_cipher_and_mac(vars);
20✔
311

312
         auto tls_cbc = T(std::move(cipher), std::move(mac), keylen_cipher, keylen_mac, protocol, encrypt_then_mac);
40✔
313

314
         tls_cbc.set_key(key);
20✔
315
         tls_cbc.set_associated_data(ad);
20✔
316

317
         std::vector<uint8_t> in(pt.begin(), pt.end());
20✔
318
         std::vector<uint8_t> out(ct.begin(), ct.end());
20✔
319

320
         if constexpr(!encrypt) {
321
            std::swap(in, out);
10✔
322
         }
323

324
         // Test 1: process the entire message at once
325
         std::vector<uint8_t> inout = in;
20✔
326
         tls_cbc.start(nonce);
20✔
327
         tls_cbc.finish(inout);  // in-place processing ('in' should now contain 'out')
20✔
328
         result.test_bin_eq(std::string("expected output of ") + direction, inout, out);
60✔
329

330
         // Test 2: process the message in chunks
331
         auto in_span = std::span{in};
20✔
332
         tls_cbc.start(nonce);
20✔
333
         constexpr size_t chunk_size = 7;
334
         while(in_span.size() >= chunk_size && in_span.size() > tls_cbc.minimum_final_size() + chunk_size) {
2,047✔
335
            tls_cbc.process(in_span.first(chunk_size));
2,027✔
336
            in_span = in_span.subspan(chunk_size);
2,027✔
337
         }
338

339
         std::vector<uint8_t> chunked_out(in_span.begin(), in_span.end());
20✔
340
         tls_cbc.finish(chunked_out);
20✔
341
         result.test_bin_eq(std::string("expected output with chunking of ") + direction, chunked_out, out);
60✔
342
      }
20✔
343
};
344

345
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
346
BOTAN_REGISTER_TEST("tls", "tls_cbc_kat", TLS_CBC_KAT_Tests);
347

348
   #endif
349

350
   #if defined(BOTAN_HAS_TLS_NULL)
351

352
class TLS_Null_Tests final : public Text_Based_Test {
×
353
   public:
354
      TLS_Null_Tests() : Text_Based_Test("tls_null.vec", "Hash,Key,AssociatedData,Message,Fragment") {}
2✔
355

356
      void encryption_test(Test::Result& result,
3✔
357
                           const std::string& hash,
358
                           const std::vector<uint8_t>& key,
359
                           const std::vector<uint8_t>& associated_data,
360
                           const std::vector<uint8_t>& message,
361
                           const std::vector<uint8_t>& expected_tls_fragment) {
362
         auto mac = Botan::MessageAuthenticationCode::create_or_throw(Botan::fmt("HMAC({})", hash));
3✔
363

364
         const auto mac_output_length = mac->output_length();
3✔
365
         Botan::TLS::TLS_NULL_HMAC_AEAD_Encryption tls_null_encrypt(std::move(mac), mac_output_length);
3✔
366

367
         tls_null_encrypt.set_key(key);
3✔
368
         tls_null_encrypt.set_associated_data(associated_data);
3✔
369

370
         Botan::secure_vector<uint8_t> buffer(message.begin(), message.end());
3✔
371
         tls_null_encrypt.finish(buffer);
3✔
372

373
         result.test_bin_eq("Encrypted TLS fragment matches expectation", buffer, expected_tls_fragment);
3✔
374
      }
3✔
375

376
      void decryption_test(Test::Result& result,
4✔
377
                           const std::string& hash,
378
                           const std::vector<uint8_t>& key,
379
                           const std::vector<uint8_t>& associated_data,
380
                           const std::vector<uint8_t>& expected_message,
381
                           const std::vector<uint8_t>& tls_fragment,
382
                           const std::string& header) {
383
         auto mac = Botan::MessageAuthenticationCode::create_or_throw(Botan::fmt("HMAC({})", hash));
4✔
384

385
         const auto mac_output_length = mac->output_length();
4✔
386
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
4✔
387

388
         tls_null_decrypt.set_key(key);
4✔
389
         tls_null_decrypt.set_associated_data(associated_data);
4✔
390

391
         Botan::secure_vector<uint8_t> buffer(tls_fragment.begin(), tls_fragment.end());
4✔
392

393
         if(header == "InvalidMAC") {
4✔
394
            result.test_throws("TLS_NULL_HMAC_AEAD_Decryption::finish()", "Message authentication failure", [&]() {
1✔
395
               tls_null_decrypt.finish(buffer, 0);
1✔
396
            });
397
         } else {
398
            tls_null_decrypt.finish(buffer, 0);
3✔
399
            result.test_bin_eq("Decrypted TLS fragment matches expectation", buffer, expected_message);
3✔
400
         }
401
      }
4✔
402

403
      void invalid_ad_length_test(Test::Result& result,
1✔
404
                                  const std::string& hash,
405
                                  const std::vector<uint8_t>& associated_data) {
406
         auto mac = Botan::MessageAuthenticationCode::create_or_throw(Botan::fmt("HMAC({})", hash));
1✔
407

408
         const auto mac_output_length = mac->output_length();
1✔
409
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
1✔
410

411
         result.test_throws<Botan::Invalid_Argument>("TLS_NULL_HMAC_AEAD_Decryption::set_associated_data()",
1✔
412
                                                     [&]() { tls_null_decrypt.set_associated_data(associated_data); });
2✔
413
         return;
1✔
414
      }
1✔
415

416
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
5✔
417
         Test::Result result("TLS Null Cipher");
5✔
418

419
         const std::string hash = vars.get_req_str("Hash");
5✔
420
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
5✔
421
         const std::vector<uint8_t> associated_data = vars.get_req_bin("AssociatedData");
5✔
422
         const std::vector<uint8_t> expected_message = vars.get_req_bin("Message");
5✔
423
         const std::vector<uint8_t> tls_fragment = vars.get_req_bin("Fragment");
5✔
424

425
         if(header.empty()) {
5✔
426
            encryption_test(result, hash, key, associated_data, expected_message, tls_fragment);
3✔
427
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
3✔
428
         }
429

430
         if(header == "InvalidMAC") {
5✔
431
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
1✔
432
         }
433

434
         if(header == "InvalidAssociatedDataLength") {
5✔
435
            invalid_ad_length_test(result, hash, associated_data);
1✔
436
         }
437

438
         return result;
10✔
439
      }
5✔
440
};
441

442
BOTAN_REGISTER_TEST("tls", "tls_null", TLS_Null_Tests);
443

444
   #endif
445

446
class Test_TLS_Alert_Strings : public Test {
1✔
447
   public:
448
      std::vector<Test::Result> run() override {
1✔
449
         Test::Result result("TLS::Alert::type_string");
1✔
450

451
         const std::vector<Botan::TLS::Alert::Type> alert_types = {
1✔
452
            Botan::TLS::Alert::CloseNotify,
453
            Botan::TLS::Alert::UnexpectedMessage,
454
            Botan::TLS::Alert::BadRecordMac,
455
            Botan::TLS::Alert::DecryptionFailed,
456
            Botan::TLS::Alert::RecordOverflow,
457
            Botan::TLS::Alert::DecompressionFailure,
458
            Botan::TLS::Alert::HandshakeFailure,
459
            Botan::TLS::Alert::NoCertificate,
460
            Botan::TLS::Alert::BadCertificate,
461
            Botan::TLS::Alert::UnsupportedCertificate,
462
            Botan::TLS::Alert::CertificateRevoked,
463
            Botan::TLS::Alert::CertificateExpired,
464
            Botan::TLS::Alert::CertificateUnknown,
465
            Botan::TLS::Alert::IllegalParameter,
466
            Botan::TLS::Alert::UnknownCA,
467
            Botan::TLS::Alert::AccessDenied,
468
            Botan::TLS::Alert::DecodeError,
469
            Botan::TLS::Alert::DecryptError,
470
            Botan::TLS::Alert::ExportRestriction,
471
            Botan::TLS::Alert::ProtocolVersion,
472
            Botan::TLS::Alert::InsufficientSecurity,
473
            Botan::TLS::Alert::InternalError,
474
            Botan::TLS::Alert::InappropriateFallback,
475
            Botan::TLS::Alert::UserCanceled,
476
            Botan::TLS::Alert::NoRenegotiation,
477
            Botan::TLS::Alert::MissingExtension,
478
            Botan::TLS::Alert::UnsupportedExtension,
479
            Botan::TLS::Alert::CertificateUnobtainable,
480
            Botan::TLS::Alert::UnrecognizedName,
481
            Botan::TLS::Alert::BadCertificateStatusResponse,
482
            Botan::TLS::Alert::BadCertificateHashValue,
483
            Botan::TLS::Alert::UnknownPSKIdentity,
484
            Botan::TLS::Alert::NoApplicationProtocol,
485
         };
1✔
486

487
         std::set<std::string> seen;
1✔
488

489
         for(auto alert : alert_types) {
34✔
490
            const std::string str = Botan::TLS::Alert(alert).type_string();
33✔
491
            result.test_sz_eq("No duplicate strings", seen.count(str), 0);
33✔
492
            seen.insert(str);
33✔
493
         }
33✔
494

495
         const Botan::TLS::Alert unknown_alert = Botan::TLS::Alert({01, 66});
1✔
496

497
         result.test_str_eq("Unknown alert str", unknown_alert.type_string(), "unrecognized_alert_66");
1✔
498

499
         return {result};
3✔
500
      }
2✔
501
};
502

503
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
504

505
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && \
506
      defined(BOTAN_HAS_X25519) && defined(BOTAN_HAS_X448)
507

508
class Test_TLS_Policy_Text : public Test {
1✔
509
   public:
510
      std::vector<Test::Result> run() override {
1✔
511
         Test::Result result("TLS Policy");
1✔
512

513
         const std::vector<std::string> policies = {"default", "suiteb_128", "suiteb_192", "strict", "datagram", "bsi"};
1✔
514

515
         for(const std::string& policy : policies) {
7✔
516
            const std::string from_policy_obj = tls_policy_string(policy);
6✔
517

518
            const std::string policy_file = policy + (policy == "default" || policy == "strict" ? "_tls13" : "");
8✔
519

520
            const std::string from_file = read_tls_policy(policy_file);
6✔
521

522
            if(from_policy_obj != from_file) {
6✔
523
               const std::string d = diff(from_policy_obj, from_file);
×
524
               result.test_failure(Botan::fmt("Values for TLS policy from {} don't match (diff {})", policy_file, d));
×
525
            } else {
×
526
               result.test_success("Values from TLS policy from " + policy_file + " match");
12✔
527
            }
528
         }
6✔
529

530
         return {result};
3✔
531
      }
8✔
532

533
   private:
534
      static std::string diff(const std::string& a_str, const std::string& b_str) {
×
535
         std::istringstream a_ss(a_str);
×
536
         std::istringstream b_ss(b_str);
×
537

538
         std::ostringstream diff;
×
539

540
         for(;;) {
×
541
            if(!a_ss && !b_ss) {
×
542
               break;  // done
543
            }
544

545
            std::string a_line;
×
546
            std::getline(a_ss, a_line, '\n');
×
547

548
            std::string b_line;
×
549
            std::getline(b_ss, b_line, '\n');
×
550

551
            if(a_line != b_line) {
×
552
               diff << "- " << a_line << "\n"
×
553
                    << "+ " << b_line << "\n";
×
554
            }
555
         }
×
556

557
         return diff.str();
×
558
      }
×
559

560
      static std::string read_tls_policy(const std::string& policy_str) {
6✔
561
         const std::string fspath = Test::data_file("tls-policy/" + policy_str + ".txt");
18✔
562

563
         std::ifstream is(fspath.c_str());
6✔
564
         if(!is.good()) {
6✔
565
            throw Test_Error("Missing policy file " + fspath);
×
566
         }
567

568
         const Botan::TLS::Text_Policy policy(is);
6✔
569
         return policy.to_string();
6✔
570
      }
6✔
571

572
      static std::string tls_policy_string(const std::string& policy_str) {
6✔
573
         std::unique_ptr<Botan::TLS::Policy> policy;
6✔
574
         if(policy_str == "default") {
6✔
575
            policy = std::make_unique<Botan::TLS::Policy>();
1✔
576
         } else if(policy_str == "suiteb_128") {
5✔
577
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_128>();
1✔
578
         } else if(policy_str == "suiteb_192") {
4✔
579
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_192>();
1✔
580
         } else if(policy_str == "bsi") {
3✔
581
            policy = std::make_unique<Botan::TLS::BSI_TR_02102_2>();
1✔
582
         } else if(policy_str == "strict") {
2✔
583
            policy = std::make_unique<Botan::TLS::Strict_Policy>();
1✔
584
         } else if(policy_str == "datagram") {
1✔
585
            policy = std::make_unique<Botan::TLS::Datagram_Policy>();
1✔
586
         } else {
587
            throw Test_Error("Unknown TLS policy type '" + policy_str + "'");
×
588
         }
589

590
         return policy->to_string();
6✔
591
      }
6✔
592
};
593

594
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
595
   #endif
596

597
class Test_TLS_Ciphersuites : public Test {
1✔
598
   public:
599
      std::vector<Test::Result> run() override {
1✔
600
         Test::Result result("TLS::Ciphersuite");
1✔
601

602
         for(size_t csuite_id = 0; csuite_id <= 0xFFFF; ++csuite_id) {
65,537✔
603
            const uint16_t csuite_id16 = static_cast<uint16_t>(csuite_id);
65,536✔
604
            auto ciphersuite = Botan::TLS::Ciphersuite::by_id(csuite_id16);
65,536✔
605

606
            if(ciphersuite && ciphersuite->valid()) {
65,536✔
607
               result.test_is_false("Valid Ciphersuite is not SCSV", Botan::TLS::Ciphersuite::is_scsv(csuite_id16));
102✔
608

609
               if(ciphersuite->cbc_ciphersuite() == false && ciphersuite->null_ciphersuite() == false) {
102✔
610
                  result.test_is_true("Expected AEAD ciphersuite", ciphersuite->aead_ciphersuite());
64✔
611
                  result.test_str_eq("Expected MAC name for AEAD ciphersuites", ciphersuite->mac_algo(), "AEAD");
64✔
612
               } else {
613
                  result.test_is_false("Did not expect AEAD ciphersuite", ciphersuite->aead_ciphersuite());
38✔
614
                  result.test_str_eq("MAC algo and PRF algo same for CBC and NULL suites",
38✔
615
                                     ciphersuite->prf_algo(),
76✔
616
                                     ciphersuite->mac_algo());
76✔
617
               }
618

619
               if(ciphersuite->null_ciphersuite()) {
102✔
620
                  result.test_str_eq("Expected NULL ciphersuite", ciphersuite->cipher_algo(), "NULL");
8✔
621
               };
622

623
               // TODO more tests here
624
            }
625
         }
626

627
         return {result};
3✔
628
      }
2✔
629
};
630

631
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
632

633
class Test_TLS_Algo_Strings : public Test {
1✔
634
   public:
635
      std::vector<Test::Result> run() override {
1✔
636
         std::vector<Test::Result> results;
1✔
637

638
         results.push_back(test_auth_method_strings());
2✔
639
         results.push_back(test_kex_algo_strings());
2✔
640
         results.push_back(test_tls_sig_method_strings());
2✔
641

642
         return results;
1✔
643
      }
×
644

645
   private:
646
      static Test::Result test_tls_sig_method_strings() {
1✔
647
         Test::Result result("TLS::Signature_Scheme");
1✔
648

649
         std::set<std::string> scheme_strs;
1✔
650
         for(auto scheme : Botan::TLS::Signature_Scheme::all_available_schemes()) {
10✔
651
            const std::string scheme_str = scheme.to_string();
9✔
652

653
            result.test_sz_eq("Scheme strings unique", scheme_strs.count(scheme_str), 0);
9✔
654

655
            scheme_strs.insert(scheme_str);
9✔
656
         }
9✔
657

658
         return result;
1✔
659
      }
1✔
660

661
      static Test::Result test_auth_method_strings() {
1✔
662
         Test::Result result("TLS::Auth_Method");
1✔
663

664
         const std::vector<Botan::TLS::Auth_Method> auth_methods({
1✔
665
            Botan::TLS::Auth_Method::RSA,
666
            Botan::TLS::Auth_Method::ECDSA,
667
            Botan::TLS::Auth_Method::IMPLICIT,
668
         });
1✔
669

670
         for(const Botan::TLS::Auth_Method meth : auth_methods) {
4✔
671
            const std::string meth_str = Botan::TLS::auth_method_to_string(meth);
3✔
672
            result.test_str_not_empty("Method string is not empty", meth_str);
3✔
673
            const Botan::TLS::Auth_Method meth2 = Botan::TLS::auth_method_from_string(meth_str);
3✔
674
            result.test_is_true("Decoded method matches", meth == meth2);
3✔
675
         }
3✔
676

677
         return result;
1✔
678
      }
1✔
679

680
      static Test::Result test_kex_algo_strings() {
1✔
681
         Test::Result result("TLS::Kex_Algo");
1✔
682

683
         const std::vector<Botan::TLS::Kex_Algo> kex_algos({Botan::TLS::Kex_Algo::STATIC_RSA,
1✔
684
                                                            Botan::TLS::Kex_Algo::DH,
685
                                                            Botan::TLS::Kex_Algo::ECDH,
686
                                                            Botan::TLS::Kex_Algo::PSK,
687
                                                            Botan::TLS::Kex_Algo::ECDHE_PSK});
1✔
688

689
         for(const Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
690
            const std::string meth_str = Botan::TLS::kex_method_to_string(meth);
5✔
691
            result.test_str_not_empty("Method string is not empty", meth_str);
5✔
692
            const Botan::TLS::Kex_Algo meth2 = Botan::TLS::kex_method_from_string(meth_str);
5✔
693
            result.test_is_true("Decoded method matches", meth == meth2);
5✔
694
         }
5✔
695

696
         return result;
1✔
697
      }
1✔
698
};
699

700
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
701

702
#endif
703

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