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

randombit / botan / 16293079084

15 Jul 2025 12:20PM UTC coverage: 90.627% (+0.003%) from 90.624%
16293079084

push

github

web-flow
Merge pull request #4990 from randombit/jack/string-and-span

Improve string<->span conversions

99640 of 109945 relevant lines covered (90.63%)

12253617.72 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/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_version.h>
19
   #include <botan/internal/fmt.h>
20

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

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

29
#endif
30

31
namespace Botan_Tests {
32

33
#if defined(BOTAN_HAS_TLS)
34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

102
   #if defined(BOTAN_HAS_TLS_CBC)
103

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

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

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

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

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

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

128
            void clear() override {}
×
129

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

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

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

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

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

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

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

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

155
            size_t m_mac_len;
156
      };
157

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

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

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

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

172
            void clear() override {}
×
173

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

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

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

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

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

189
            size_t m_bs;
190
      };
191

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

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

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

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

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

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

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

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

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

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

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

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

251
         return result;
10✔
252
      }
×
253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

344
   #endif
345

346
   #if defined(BOTAN_HAS_TLS_NULL)
347

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

440
   #endif
441

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

534
         std::ostringstream diff;
×
535

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

638
         return results;
1✔
639
      }
×
640

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

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

647
         std::set<std::string> scheme_strs;
1✔
648
         for(auto scheme : schemes) {
10✔
649
            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;
2✔
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(Botan::TLS::Auth_Method meth : auth_methods) {
4✔
669
            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
            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(Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
688
            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
            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