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

randombit / botan / 21848380424

10 Feb 2026 01:47AM UTC coverage: 91.634% (+1.6%) from 90.069%
21848380424

push

github

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

Various changes to reduce header dependencies in TLS

104002 of 113497 relevant lines covered (91.63%)

11230277.53 hits per line

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

87.25
/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_eq("Roundtrip from pem", session.DER_encode(), session_from_pem.DER_encode());
3✔
56

57
         const auto der = session.DER_encode();
1✔
58
         const Botan::TLS::Session session_from_der(der);
1✔
59
         result.test_eq("Roundtrip from der", session.DER_encode(), session_from_der.DER_encode());
3✔
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_ne(
1✔
66
            "TLS session encryption is non-deterministic", ctext1.data(), ctext1.size(), ctext2.data(), ctext2.size());
67

68
         const std::vector<uint8_t> expected_hdr = Botan::hex_decode("068B5A9D396C0000F2322CAE");
1✔
69

70
         result.test_eq("tls", "TLS session encryption same header", ctext1.data(), 12, expected_hdr.data(), 12);
1✔
71
         result.test_eq("tls", "TLS session encryption same header", ctext2.data(), 12, expected_hdr.data(), 12);
1✔
72

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

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

80
         result.test_eq("Only randomness comes from RNG", ctextf1, ctextf2);
2✔
81

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

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

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

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

104
   #if defined(BOTAN_HAS_TLS_CBC)
105

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

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

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

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

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

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

130
            void clear() override {}
×
131

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

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

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

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

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

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

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

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

157
            size_t m_mac_len;
158
      };
159

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

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

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

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

174
            void clear() override {}
×
175

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

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

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

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

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

191
            size_t m_bs;
192
      };
193

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

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

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

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

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

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

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

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

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

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

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

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

253
         return result;
10✔
254
      }
×
255

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

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

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

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

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

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

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

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

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

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

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

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

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

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

346
   #endif
347

348
   #if defined(BOTAN_HAS_TLS_NULL)
349

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

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

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

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

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

371
         result.test_eq("Encrypted TLS fragment matches expectation", Botan::unlock(buffer), expected_tls_fragment);
9✔
372
      }
3✔
373

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

442
   #endif
443

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

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

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

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

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

495
         result.test_eq("Unknown alert str", unknown_alert.type_string(), "unrecognized_alert_66");
2✔
496

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

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

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

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

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

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

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

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

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

528
         return {result};
3✔
529
      }
2✔
530

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

536
         std::ostringstream diff;
×
537

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

617
               if(ciphersuite->null_ciphersuite()) {
102✔
618
                  result.test_eq("Expected NULL ciphersuite", ciphersuite->cipher_algo(), "NULL");
16✔
619
               };
620

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

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

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

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

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

640
         return results;
1✔
641
      }
×
642

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

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

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

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

656
         return result;
1✔
657
      }
1✔
658

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

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

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

675
         return result;
1✔
676
      }
1✔
677

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

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

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

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

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

700
#endif
701

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