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

randombit / botan / 22038388571

15 Feb 2026 03:39PM UTC coverage: 90.056% (-0.003%) from 90.059%
22038388571

Pull #5341

github

web-flow
Merge e5a537aa4 into 76dfe61e9
Pull Request #5341: Cleanup test predicates on binary strings

102343 of 113644 relevant lines covered (90.06%)

11670901.95 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/internal/fmt.h>
21
   #include <set>
22

23
   #if defined(BOTAN_HAS_TLS_CBC)
24
      #include <botan/internal/tls_cbc.h>
25
   #endif
26

27
   #if defined(BOTAN_HAS_TLS_NULL)
28
      #include <botan/internal/tls_null.h>
29
   #endif
30

31
#endif
32

33
namespace Botan_Tests {
34

35
#if defined(BOTAN_HAS_TLS)
36

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

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

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

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

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

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

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

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

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

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

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

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

97
         return {result};
3✔
98
      }
9✔
99
};
100

101
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
102

103
   #if defined(BOTAN_HAS_TLS_CBC)
104

105
class TLS_CBC_Padding_Tests final : public Text_Based_Test {
×
106
   public:
107
      TLS_CBC_Padding_Tests() : Text_Based_Test("tls_cbc_padding.vec", "Record,Output") {}
2✔
108

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

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

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

121
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
122

123
class TLS_CBC_Tests final : public Text_Based_Test {
×
124
   public:
125
      class ZeroMac : public Botan::MessageAuthenticationCode {
126
         public:
127
            explicit ZeroMac(size_t mac_len) : m_mac_len(mac_len) {}
10✔
128

129
            void clear() override {}
×
130

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

133
            size_t output_length() const override { return m_mac_len; }
20✔
134

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

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

143
            bool has_keying_material() const override { return true; }
×
144

145
            Botan::Key_Length_Specification key_spec() const override {
20✔
146
               return Botan::Key_Length_Specification(0, 0, 1);
20✔
147
            }
148

149
            std::unique_ptr<MessageAuthenticationCode> new_object() const override {
×
150
               return std::make_unique<ZeroMac>(m_mac_len);
×
151
            }
152

153
         private:
154
            void key_schedule(std::span<const uint8_t> /* key */) override {}
10✔
155

156
            size_t m_mac_len;
157
      };
158

159
      class Noop_Block_Cipher : public Botan::BlockCipher {
160
         public:
161
            explicit Noop_Block_Cipher(size_t bs) : m_bs(bs) {}
10✔
162

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

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

171
            size_t block_size() const override { return m_bs; }
40✔
172

173
            void clear() override {}
×
174

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

177
            bool has_keying_material() const override { return true; }
×
178

179
            Botan::Key_Length_Specification key_spec() const override {
30✔
180
               return Botan::Key_Length_Specification(0, 0, 1);
30✔
181
            }
182

183
            std::unique_ptr<BlockCipher> new_object() const override {
×
184
               return std::make_unique<Noop_Block_Cipher>(m_bs);
×
185
            }
186

187
         private:
188
            void key_schedule(std::span<const uint8_t> /*key*/) override {}
10✔
189

190
            size_t m_bs;
191
      };
192

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

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

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

203
         // todo test permutations
204
         const bool encrypt_then_mac = false;
10✔
205

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

213
         tls_cbc.set_key(std::vector<uint8_t>(0));
10✔
214
         std::vector<uint8_t> ad(13);
10✔
215
         tls_cbc.set_associated_data(ad.data(), ad.size());
10✔
216

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

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

234
         return result;
10✔
235
      }
30✔
236
};
237

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

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

249
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption>(result, vars);
10✔
250
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption>(result, vars);
10✔
251

252
         return result;
10✔
253
      }
×
254

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

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

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

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

301
         const auto key = vars.get_req_bin("Key");
40✔
302
         const auto ad = vars.get_req_bin("AssociatedData");
40✔
303
         const auto nonce = vars.get_req_bin("Nonce");
40✔
304
         const auto pt = vars.get_req_bin("Plaintext");
40✔
305
         const auto ct = vars.get_req_bin("Ciphertext");
20✔
306

307
         auto [cipher, mac] = get_cipher_and_mac(vars);
20✔
308

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

311
         tls_cbc.set_key(key);
20✔
312
         tls_cbc.set_associated_data(ad);
20✔
313

314
         std::vector<uint8_t> in(pt.begin(), pt.end());
20✔
315
         std::vector<uint8_t> out(ct.begin(), ct.end());
20✔
316

317
         if constexpr(!encrypt) {
318
            std::swap(in, out);
10✔
319
         }
320

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

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

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

342
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
343
BOTAN_REGISTER_TEST("tls", "tls_cbc_kat", TLS_CBC_KAT_Tests);
344

345
   #endif
346

347
   #if defined(BOTAN_HAS_TLS_NULL)
348

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

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

361
         const auto mac_output_length = mac->output_length();
3✔
362
         Botan::TLS::TLS_NULL_HMAC_AEAD_Encryption tls_null_encrypt(std::move(mac), mac_output_length);
3✔
363

364
         tls_null_encrypt.set_key(key);
3✔
365
         tls_null_encrypt.set_associated_data(associated_data);
3✔
366

367
         Botan::secure_vector<uint8_t> buffer(message.begin(), message.end());
3✔
368
         tls_null_encrypt.finish(buffer);
3✔
369

370
         result.test_bin_eq("Encrypted TLS fragment matches expectation", buffer, expected_tls_fragment);
3✔
371
      }
3✔
372

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

382
         const auto mac_output_length = mac->output_length();
4✔
383
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
4✔
384

385
         tls_null_decrypt.set_key(key);
4✔
386
         tls_null_decrypt.set_associated_data(associated_data);
4✔
387

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

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

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

405
         const auto mac_output_length = mac->output_length();
1✔
406
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
1✔
407

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

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

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

422
         if(header.empty()) {
5✔
423
            encryption_test(result, hash, key, associated_data, expected_message, tls_fragment);
3✔
424
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
3✔
425
         }
426

427
         if(header == "InvalidMAC") {
5✔
428
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
1✔
429
         }
430

431
         if(header == "InvalidAssociatedDataLength") {
5✔
432
            invalid_ad_length_test(result, hash, associated_data);
1✔
433
         }
434

435
         return result;
5✔
436
      }
20✔
437
};
438

439
BOTAN_REGISTER_TEST("tls", "tls_null", TLS_Null_Tests);
440

441
   #endif
442

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

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

484
         std::set<std::string> seen;
1✔
485

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

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

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

496
         return {result};
3✔
497
      }
2✔
498
};
499

500
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
501

502
   #if defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && defined(BOTAN_HAS_X25519) && \
503
      defined(BOTAN_HAS_X448)
504

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

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

512
         for(const std::string& policy : policies) {
7✔
513
            const std::string from_policy_obj = tls_policy_string(policy);
6✔
514

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

517
            const std::string from_file = read_tls_policy(policy_file);
6✔
518

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

527
         return {result};
3✔
528
      }
8✔
529

530
   private:
531
      static std::string diff(const std::string& a_str, const std::string& b_str) {
×
532
         std::istringstream a_ss(a_str);
×
533
         std::istringstream b_ss(b_str);
×
534

535
         std::ostringstream diff;
×
536

537
         for(;;) {
×
538
            if(!a_ss && !b_ss) {
×
539
               break;  // done
540
            }
541

542
            std::string a_line;
×
543
            std::getline(a_ss, a_line, '\n');
×
544

545
            std::string b_line;
×
546
            std::getline(b_ss, b_line, '\n');
×
547

548
            if(a_line != b_line) {
×
549
               diff << "- " << a_line << "\n"
×
550
                    << "+ " << b_line << "\n";
×
551
            }
552
         }
×
553

554
         return diff.str();
×
555
      }
×
556

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

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

565
         const Botan::TLS::Text_Policy policy(is);
6✔
566
         return policy.to_string();
6✔
567
      }
6✔
568

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

587
         return policy->to_string();
6✔
588
      }
6✔
589
};
590

591
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
592
   #endif
593

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

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

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

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

616
               if(ciphersuite->null_ciphersuite()) {
102✔
617
                  result.test_str_eq("Expected NULL ciphersuite", ciphersuite->cipher_algo(), "NULL");
8✔
618
               };
619

620
               // TODO more tests here
621
            }
622
         }
623

624
         return {result};
3✔
625
      }
2✔
626
};
627

628
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
629

630
class Test_TLS_Algo_Strings : public Test {
1✔
631
   public:
632
      std::vector<Test::Result> run() override {
1✔
633
         std::vector<Test::Result> results;
1✔
634

635
         results.push_back(test_auth_method_strings());
2✔
636
         results.push_back(test_kex_algo_strings());
2✔
637
         results.push_back(test_tls_sig_method_strings());
2✔
638

639
         return results;
1✔
640
      }
×
641

642
   private:
643
      static Test::Result test_tls_sig_method_strings() {
1✔
644
         Test::Result result("TLS::Signature_Scheme");
1✔
645

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

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

652
            scheme_strs.insert(scheme_str);
9✔
653
         }
9✔
654

655
         return result;
1✔
656
      }
1✔
657

658
      static Test::Result test_auth_method_strings() {
1✔
659
         Test::Result result("TLS::Auth_Method");
1✔
660

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

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

674
         return result;
1✔
675
      }
1✔
676

677
      static Test::Result test_kex_algo_strings() {
1✔
678
         Test::Result result("TLS::Kex_Algo");
1✔
679

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

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

693
         return result;
1✔
694
      }
1✔
695
};
696

697
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
698

699
#endif
700

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