• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

randombit / botan / 15428696210

03 Jun 2025 07:49PM UTC coverage: 90.964% (-0.004%) from 90.968%
15428696210

push

github

web-flow
Merge pull request #4776 from cariad-tech/add-null-ciphers

Add support for NULL cipher suites in TLS 1.2

98221 of 107978 relevant lines covered (90.96%)

12566270.48 hits per line

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

86.99
/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/tls_alert.h>
15
   #include <botan/tls_policy.h>
16
   #include <botan/tls_session.h>
17
   #include <botan/tls_version.h>
18
   #include <botan/internal/fmt.h>
19

20
   #if defined(BOTAN_HAS_TLS_CBC)
21
      #include <botan/internal/tls_cbc.h>
22
   #endif
23

24
   #if defined(BOTAN_HAS_TLS_NULL)
25
      #include <botan/internal/tls_null.h>
26
   #endif
27

28
#endif
29

30
namespace Botan_Tests {
31

32
#if defined(BOTAN_HAS_TLS)
33

34
class TLS_Session_Tests final : public Test {
×
35
   public:
36
      std::vector<Test::Result> run() override {
1✔
37
         Test::Result result("TLS::Session");
1✔
38

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

50
         const std::string pem = session.PEM_encode();
1✔
51
         Botan::TLS::Session session_from_pem(pem);
1✔
52
         result.test_eq("Roundtrip from pem", session.DER_encode(), session_from_pem.DER_encode());
3✔
53

54
         const auto der = session.DER_encode();
1✔
55
         Botan::TLS::Session session_from_der(der);
1✔
56
         result.test_eq("Roundtrip from der", session.DER_encode(), session_from_der.DER_encode());
3✔
57

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

62
         result.test_ne(
1✔
63
            "TLS session encryption is non-determinsitic", ctext1.data(), ctext1.size(), ctext2.data(), ctext2.size());
64

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

67
         result.test_eq("tls", "TLS session encryption same header", ctext1.data(), 12, expected_hdr.data(), 12);
1✔
68
         result.test_eq("tls", "TLS session encryption same header", ctext2.data(), 12, expected_hdr.data(), 12);
1✔
69

70
         Botan::TLS::Session dsession = Botan::TLS::Session::decrypt(ctext1.data(), ctext1.size(), key);
1✔
71

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

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

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

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

95
         return {result};
3✔
96
      }
9✔
97
};
98

99
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
100

101
   #if defined(BOTAN_HAS_TLS_CBC)
102

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

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

111
         uint16_t res = Botan::TLS::check_tls_cbc_padding(record.data(), record.size());
22✔
112

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

119
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
120

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

127
            void clear() override {}
×
128

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

131
            size_t output_length() const override { return m_mac_len; }
20✔
132

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

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

141
            bool has_keying_material() const override { return true; }
×
142

143
            Botan::Key_Length_Specification key_spec() const override {
10✔
144
               return Botan::Key_Length_Specification(0, 0, 1);
10✔
145
            }
146

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

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

154
            size_t m_mac_len;
155
      };
156

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

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

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

169
            size_t block_size() const override { return m_bs; }
40✔
170

171
            void clear() override {}
×
172

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

175
            bool has_keying_material() const override { return true; }
×
176

177
            Botan::Key_Length_Specification key_spec() const override {
20✔
178
               return Botan::Key_Length_Specification(0, 0, 1);
20✔
179
            }
180

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

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

188
            size_t m_bs;
189
      };
190

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

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

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

201
         // todo test permutations
202
         bool encrypt_then_mac = false;
10✔
203

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

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

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

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

232
         return result;
10✔
233
      }
30✔
234
};
235

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

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

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

250
         return result;
10✔
251
      }
×
252

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

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

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

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

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

305
         auto [cipher, mac] = get_cipher_and_mac(vars);
20✔
306

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

309
         tls_cbc.set_key(key);
20✔
310
         tls_cbc.set_associated_data(ad);
20✔
311

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

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

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

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

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

340
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
341
BOTAN_REGISTER_TEST("tls", "tls_cbc_kat", TLS_CBC_KAT_Tests);
342

343
   #endif
344

345
   #if defined(BOTAN_HAS_TLS_NULL)
346

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

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

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

362
         tls_null_encrypt.set_key(key);
3✔
363
         tls_null_encrypt.set_associated_data(associated_data);
3✔
364

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

368
         result.test_eq("Encrypted TLS fragment matches expectation", Botan::unlock(buffer), expected_tls_fragment);
9✔
369
      }
3✔
370

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

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

383
         tls_null_decrypt.set_key(key);
4✔
384
         tls_null_decrypt.set_associated_data(associated_data);
4✔
385

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

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

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

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

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

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

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

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

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

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

433
         return result;
5✔
434
      }
20✔
435
};
436

437
BOTAN_REGISTER_TEST("tls", "tls_null", TLS_Null_Tests);
438

439
   #endif
440

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

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

482
         std::set<std::string> seen;
1✔
483

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

490
         Botan::TLS::Alert unknown_alert = Botan::TLS::Alert({01, 66});
1✔
491

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

494
         return {result};
3✔
495
      }
2✔
496
};
497

498
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
499

500
   #if defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && defined(BOTAN_HAS_X25519) && \
501
      defined(BOTAN_HAS_X448)
502

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

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

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

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

515
            const std::string from_file = read_tls_policy(policy_file);
6✔
516

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

525
         return {result};
3✔
526
      }
2✔
527

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

533
         std::ostringstream diff;
×
534

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

540
            std::string a_line;
×
541
            std::getline(a_ss, a_line, '\n');
×
542

543
            std::string b_line;
×
544
            std::getline(b_ss, b_line, '\n');
×
545

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

552
         return diff.str();
×
553
      }
×
554

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

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

563
         Botan::TLS::Text_Policy policy(is);
6✔
564
         return policy.to_string();
6✔
565
      }
6✔
566

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

585
         return policy->to_string();
6✔
586
      }
6✔
587
};
588

589
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
590
   #endif
591

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

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

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

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

614
               if(ciphersuite->null_ciphersuite()) {
102✔
615
                  result.test_eq("Expected NULL ciphersuite", ciphersuite->cipher_algo(), "NULL");
16✔
616
               };
617

618
               // TODO more tests here
619
            }
620
         }
621

622
         return {result};
3✔
623
      }
2✔
624
};
625

626
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
627

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

633
         results.push_back(test_auth_method_strings());
2✔
634
         results.push_back(test_kex_algo_strings());
2✔
635
         results.push_back(test_tls_sig_method_strings());
2✔
636

637
         return results;
1✔
638
      }
×
639

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

644
         std::vector<Botan::TLS::Signature_Scheme> schemes = Botan::TLS::Signature_Scheme::all_available_schemes();
1✔
645

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

650
            result.test_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;
2✔
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(Botan::TLS::Auth_Method meth : auth_methods) {
4✔
668
            std::string meth_str = Botan::TLS::auth_method_to_string(meth);
3✔
669
            result.test_ne("Method string is not empty", meth_str, "");
6✔
670
            Botan::TLS::Auth_Method meth2 = Botan::TLS::auth_method_from_string(meth_str);
3✔
671
            result.confirm("Decoded method matches", meth == meth2);
6✔
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(Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
687
            std::string meth_str = Botan::TLS::kex_method_to_string(meth);
5✔
688
            result.test_ne("Method string is not empty", meth_str, "");
10✔
689
            Botan::TLS::Kex_Algo meth2 = Botan::TLS::kex_method_from_string(meth_str);
5✔
690
            result.confirm("Decoded method matches", meth == meth2);
10✔
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