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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 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
namespace {
39

40
#if defined(BOTAN_HAS_TLS)
41

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

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

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

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

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

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

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

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

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

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

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

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

102
         return {result};
3✔
103
      }
5✔
104
};
105

106
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
107

108
   #if defined(BOTAN_HAS_TLS_CBC)
109

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

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

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

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

126
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
127

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

134
            void clear() override {}
×
135

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

138
            size_t output_length() const override { return m_mac_len; }
20✔
139

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

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

148
            bool has_keying_material() const override { return true; }
×
149

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

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

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

161
            size_t m_mac_len;
162
      };
163

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

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

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

176
            size_t block_size() const override { return m_bs; }
40✔
177

178
            void clear() override {}
×
179

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

182
            bool has_keying_material() const override { return true; }
×
183

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

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

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

195
            size_t m_bs;
196
      };
197

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

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

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

208
         // todo test permutations
209
         const bool encrypt_then_mac = false;
10✔
210

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

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

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

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

239
         return result;
20✔
240
      }
10✔
241
};
242

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

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

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

257
         return result;
10✔
258
      }
×
259

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

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

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

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

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

312
         auto [cipher, mac] = get_cipher_and_mac(vars);
20✔
313

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

316
         tls_cbc.set_key(key);
20✔
317
         tls_cbc.set_associated_data(ad);
20✔
318

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

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

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

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

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

347
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
348
BOTAN_REGISTER_TEST("tls", "tls_cbc_kat", TLS_CBC_KAT_Tests);
349

350
   #endif
351

352
   #if defined(BOTAN_HAS_TLS_NULL)
353

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

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

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

369
         tls_null_encrypt.set_key(key);
3✔
370
         tls_null_encrypt.set_associated_data(associated_data);
3✔
371

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

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

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

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

390
         tls_null_decrypt.set_key(key);
4✔
391
         tls_null_decrypt.set_associated_data(associated_data);
4✔
392

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

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

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

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

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

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

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

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

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

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

440
         return result;
10✔
441
      }
5✔
442
};
443

444
BOTAN_REGISTER_TEST("tls", "tls_null", TLS_Null_Tests);
445

446
   #endif
447

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

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

489
         std::set<std::string> seen;
1✔
490

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

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

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

501
         return {result};
3✔
502
      }
2✔
503
};
504

505
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
506

507
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && \
508
      defined(BOTAN_HAS_X25519) && defined(BOTAN_HAS_X448)
509

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

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

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

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

522
            const std::string from_file = read_tls_policy(policy_file);
6✔
523

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

532
         return {result};
3✔
533
      }
8✔
534

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

540
         std::ostringstream diff;
×
541

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

547
            std::string a_line;
×
548
            std::getline(a_ss, a_line, '\n');
×
549

550
            std::string b_line;
×
551
            std::getline(b_ss, b_line, '\n');
×
552

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

559
         return diff.str();
×
560
      }
×
561

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

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

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

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

592
         return policy->to_string();
6✔
593
      }
6✔
594
};
595

596
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
597
   #endif
598

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

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

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

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

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

625
               // TODO more tests here
626
            }
627
         }
628

629
         return {result};
3✔
630
      }
2✔
631
};
632

633
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
634

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

640
         results.push_back(test_auth_method_strings());
2✔
641
         results.push_back(test_kex_algo_strings());
2✔
642
         results.push_back(test_tls_sig_method_strings());
2✔
643

644
         return results;
1✔
645
      }
×
646

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

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

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

657
            scheme_strs.insert(scheme_str);
9✔
658
         }
9✔
659

660
         return result;
1✔
661
      }
1✔
662

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

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

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

679
         return result;
1✔
680
      }
1✔
681

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

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

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

698
         return result;
1✔
699
      }
1✔
700
};
701

702
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
703

704
#endif
705

706
}  // namespace
707

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