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

randombit / botan / 13702836595

06 Mar 2025 12:34PM UTC coverage: 91.672% (-0.01%) from 91.686%
13702836595

push

github

web-flow
Merge pull request #4753 from randombit/jack/gate-tls-text-policy

Only run tls_policy_text test in a known configuration

95867 of 104576 relevant lines covered (91.67%)

11320993.96 hits per line

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

83.83
/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
#endif
25

26
namespace Botan_Tests {
27

28
#if defined(BOTAN_HAS_TLS)
29

30
class TLS_Session_Tests final : public Test {
×
31
   public:
32
      std::vector<Test::Result> run() override {
1✔
33
         Test::Result result("TLS::Session");
1✔
34

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

46
         const std::string pem = session.PEM_encode();
1✔
47
         Botan::TLS::Session session_from_pem(pem);
1✔
48
         result.test_eq("Roundtrip from pem", session.DER_encode(), session_from_pem.DER_encode());
3✔
49

50
         const auto der = session.DER_encode();
1✔
51
         Botan::TLS::Session session_from_der(der);
1✔
52
         result.test_eq("Roundtrip from der", session.DER_encode(), session_from_der.DER_encode());
3✔
53

54
         const Botan::SymmetricKey key("ABCDEF");
1✔
55
         const std::vector<uint8_t> ctext1 = session.encrypt(key, this->rng());
1✔
56
         const std::vector<uint8_t> ctext2 = session.encrypt(key, this->rng());
1✔
57

58
         result.test_ne(
1✔
59
            "TLS session encryption is non-determinsitic", ctext1.data(), ctext1.size(), ctext2.data(), ctext2.size());
60

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

63
         result.test_eq("tls", "TLS session encryption same header", ctext1.data(), 12, expected_hdr.data(), 12);
1✔
64
         result.test_eq("tls", "TLS session encryption same header", ctext2.data(), 12, expected_hdr.data(), 12);
1✔
65

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

68
         Fixed_Output_RNG frng1("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
69
         const std::vector<uint8_t> ctextf1 = session.encrypt(key, frng1);
1✔
70
         Fixed_Output_RNG frng2("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
71
         const std::vector<uint8_t> ctextf2 = session.encrypt(key, frng2);
1✔
72

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

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

87
         result.test_throws("unknown ciphersuite during session parsing",
2✔
88
                            "Serialized TLS session contains unknown cipher suite (47789)",
89
                            [&] { Botan::TLS::Session{pem_with_unknown_ciphersuite}; });
2✔
90

91
         return {result};
3✔
92
      }
9✔
93
};
94

95
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
96

97
   #if defined(BOTAN_HAS_TLS_CBC)
98

99
class TLS_CBC_Padding_Tests final : public Text_Based_Test {
×
100
   public:
101
      TLS_CBC_Padding_Tests() : Text_Based_Test("tls_cbc_padding.vec", "Record,Output") {}
2✔
102

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

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

109
         Test::Result result("TLS CBC padding check");
22✔
110
         result.test_eq("Expected", res, output);
22✔
111
         return result;
22✔
112
      }
22✔
113
};
114

115
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
116

117
class TLS_CBC_Tests final : public Text_Based_Test {
×
118
   public:
119
      class ZeroMac : public Botan::MessageAuthenticationCode {
120
         public:
121
            explicit ZeroMac(size_t mac_len) : m_mac_len(mac_len) {}
10✔
122

123
            void clear() override {}
×
124

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

127
            size_t output_length() const override { return m_mac_len; }
20✔
128

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

131
            void final_result(std::span<uint8_t> out) override {
10✔
132
               for(size_t i = 0; i != m_mac_len; ++i) {
206✔
133
                  out[i] = 0;
196✔
134
               }
135
            }
10✔
136

137
            bool has_keying_material() const override { return true; }
×
138

139
            Botan::Key_Length_Specification key_spec() const override {
10✔
140
               return Botan::Key_Length_Specification(0, 0, 1);
10✔
141
            }
142

143
            std::unique_ptr<MessageAuthenticationCode> new_object() const override {
×
144
               return std::make_unique<ZeroMac>(m_mac_len);
×
145
            }
146

147
         private:
148
            void key_schedule(std::span<const uint8_t> /* key */) override {}
10✔
149

150
            size_t m_mac_len;
151
      };
152

153
      class Noop_Block_Cipher : public Botan::BlockCipher {
154
         public:
155
            explicit Noop_Block_Cipher(size_t bs) : m_bs(bs) {}
10✔
156

157
            void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
×
158
               Botan::copy_mem(out, in, blocks * m_bs);
×
159
            }
×
160

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

165
            size_t block_size() const override { return m_bs; }
40✔
166

167
            void clear() override {}
×
168

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

171
            bool has_keying_material() const override { return true; }
×
172

173
            Botan::Key_Length_Specification key_spec() const override {
20✔
174
               return Botan::Key_Length_Specification(0, 0, 1);
20✔
175
            }
176

177
            std::unique_ptr<BlockCipher> new_object() const override {
×
178
               return std::make_unique<Noop_Block_Cipher>(m_bs);
×
179
            }
180

181
         private:
182
            void key_schedule(std::span<const uint8_t> /*key*/) override {}
10✔
183

184
            size_t m_bs;
185
      };
186

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

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

192
         const size_t block_size = vars.get_req_sz("Blocksize");
10✔
193
         const size_t mac_len = vars.get_req_sz("MACsize");
10✔
194
         const std::vector<uint8_t> record = vars.get_req_bin("Record");
10✔
195
         const bool is_valid = vars.get_req_sz("Valid") == 1;
10✔
196

197
         // todo test permutations
198
         bool encrypt_then_mac = false;
10✔
199

200
         Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption tls_cbc(std::make_unique<Noop_Block_Cipher>(block_size),
20✔
201
                                                          std::make_unique<ZeroMac>(mac_len),
10✔
202
                                                          0,
203
                                                          0,
204
                                                          Botan::TLS::Protocol_Version::TLS_V12,
205
                                                          encrypt_then_mac);
20✔
206

207
         tls_cbc.set_key(std::vector<uint8_t>(0));
10✔
208
         std::vector<uint8_t> ad(13);
10✔
209
         tls_cbc.set_associated_data(ad.data(), ad.size());
10✔
210

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

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

228
         return result;
10✔
229
      }
30✔
230
};
231

232
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
233

234
   #endif
235

236
class Test_TLS_Alert_Strings : public Test {
1✔
237
   public:
238
      std::vector<Test::Result> run() override {
1✔
239
         Test::Result result("TLS::Alert::type_string");
1✔
240

241
         const std::vector<Botan::TLS::Alert::Type> alert_types = {
1✔
242
            Botan::TLS::Alert::CloseNotify,
243
            Botan::TLS::Alert::UnexpectedMessage,
244
            Botan::TLS::Alert::BadRecordMac,
245
            Botan::TLS::Alert::DecryptionFailed,
246
            Botan::TLS::Alert::RecordOverflow,
247
            Botan::TLS::Alert::DecompressionFailure,
248
            Botan::TLS::Alert::HandshakeFailure,
249
            Botan::TLS::Alert::NoCertificate,
250
            Botan::TLS::Alert::BadCertificate,
251
            Botan::TLS::Alert::UnsupportedCertificate,
252
            Botan::TLS::Alert::CertificateRevoked,
253
            Botan::TLS::Alert::CertificateExpired,
254
            Botan::TLS::Alert::CertificateUnknown,
255
            Botan::TLS::Alert::IllegalParameter,
256
            Botan::TLS::Alert::UnknownCA,
257
            Botan::TLS::Alert::AccessDenied,
258
            Botan::TLS::Alert::DecodeError,
259
            Botan::TLS::Alert::DecryptError,
260
            Botan::TLS::Alert::ExportRestriction,
261
            Botan::TLS::Alert::ProtocolVersion,
262
            Botan::TLS::Alert::InsufficientSecurity,
263
            Botan::TLS::Alert::InternalError,
264
            Botan::TLS::Alert::InappropriateFallback,
265
            Botan::TLS::Alert::UserCanceled,
266
            Botan::TLS::Alert::NoRenegotiation,
267
            Botan::TLS::Alert::MissingExtension,
268
            Botan::TLS::Alert::UnsupportedExtension,
269
            Botan::TLS::Alert::CertificateUnobtainable,
270
            Botan::TLS::Alert::UnrecognizedName,
271
            Botan::TLS::Alert::BadCertificateStatusResponse,
272
            Botan::TLS::Alert::BadCertificateHashValue,
273
            Botan::TLS::Alert::UnknownPSKIdentity,
274
            Botan::TLS::Alert::NoApplicationProtocol,
275
         };
1✔
276

277
         std::set<std::string> seen;
1✔
278

279
         for(auto alert : alert_types) {
34✔
280
            const std::string str = Botan::TLS::Alert(alert).type_string();
33✔
281
            result.test_eq("No duplicate strings", seen.count(str), 0);
33✔
282
            seen.insert(str);
33✔
283
         }
33✔
284

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

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

289
         return {result};
3✔
290
      }
2✔
291
};
292

293
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
294

295
   #if defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && defined(BOTAN_HAS_X25519) && \
296
      defined(BOTAN_HAS_X448)
297

298
class Test_TLS_Policy_Text : public Test {
1✔
299
   public:
300
      std::vector<Test::Result> run() override {
1✔
301
         Test::Result result("TLS Policy");
1✔
302

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

305
         for(const std::string& policy : policies) {
7✔
306
            const std::string from_policy_obj = tls_policy_string(policy);
6✔
307

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

310
            const std::string from_file = read_tls_policy(policy_file);
6✔
311

312
            if(from_policy_obj != from_file) {
6✔
313
               std::string d = diff(from_policy_obj, from_file);
×
314
               result.test_failure(Botan::fmt("Values for TLS policy from {} don't match (diff {})", policy_file, d));
×
315
            } else {
×
316
               result.test_success("Values from TLS policy from " + policy_file + " match");
18✔
317
            }
318
         }
6✔
319

320
         return {result};
3✔
321
      }
2✔
322

323
   private:
324
      static std::string diff(const std::string& a_str, const std::string& b_str) {
×
325
         std::istringstream a_ss(a_str);
×
326
         std::istringstream b_ss(b_str);
×
327

328
         std::ostringstream diff;
×
329

330
         for(;;) {
×
331
            if(!a_ss && !b_ss) {
×
332
               break;  // done
333
            }
334

335
            std::string a_line;
×
336
            std::getline(a_ss, a_line, '\n');
×
337

338
            std::string b_line;
×
339
            std::getline(b_ss, b_line, '\n');
×
340

341
            if(a_line != b_line) {
×
342
               diff << "- " << a_line << "\n"
×
343
                    << "+ " << b_line << "\n";
×
344
            }
345
         }
×
346

347
         return diff.str();
×
348
      }
×
349

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

353
         std::ifstream is(fspath.c_str());
6✔
354
         if(!is.good()) {
6✔
355
            throw Test_Error("Missing policy file " + fspath);
×
356
         }
357

358
         Botan::TLS::Text_Policy policy(is);
6✔
359
         return policy.to_string();
6✔
360
      }
6✔
361

362
      static std::string tls_policy_string(const std::string& policy_str) {
6✔
363
         std::unique_ptr<Botan::TLS::Policy> policy;
6✔
364
         if(policy_str == "default") {
6✔
365
            policy = std::make_unique<Botan::TLS::Policy>();
1✔
366
         } else if(policy_str == "suiteb_128") {
5✔
367
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_128>();
1✔
368
         } else if(policy_str == "suiteb_192") {
4✔
369
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_192>();
1✔
370
         } else if(policy_str == "bsi") {
3✔
371
            policy = std::make_unique<Botan::TLS::BSI_TR_02102_2>();
1✔
372
         } else if(policy_str == "strict") {
2✔
373
            policy = std::make_unique<Botan::TLS::Strict_Policy>();
1✔
374
         } else if(policy_str == "datagram") {
1✔
375
            policy = std::make_unique<Botan::TLS::Datagram_Policy>();
1✔
376
         } else {
377
            throw Test_Error("Unknown TLS policy type '" + policy_str + "'");
×
378
         }
379

380
         return policy->to_string();
6✔
381
      }
6✔
382
};
383

384
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
385
   #endif
386

387
class Test_TLS_Ciphersuites : public Test {
1✔
388
   public:
389
      std::vector<Test::Result> run() override {
1✔
390
         Test::Result result("TLS::Ciphersuite");
1✔
391

392
         for(size_t csuite_id = 0; csuite_id <= 0xFFFF; ++csuite_id) {
65,537✔
393
            const uint16_t csuite_id16 = static_cast<uint16_t>(csuite_id);
65,536✔
394
            auto ciphersuite = Botan::TLS::Ciphersuite::by_id(csuite_id16);
65,536✔
395

396
            if(ciphersuite && ciphersuite->valid()) {
65,536✔
397
               result.test_eq("Valid Ciphersuite is not SCSV", Botan::TLS::Ciphersuite::is_scsv(csuite_id16), false);
94✔
398

399
               if(ciphersuite->cbc_ciphersuite() == false) {
94✔
400
                  result.test_eq("Expected AEAD ciphersuite", ciphersuite->aead_ciphersuite(), true);
64✔
401
                  result.test_eq("Expected MAC name for AEAD ciphersuites", ciphersuite->mac_algo(), "AEAD");
128✔
402
               } else {
403
                  result.test_eq("Did not expect AEAD ciphersuite", ciphersuite->aead_ciphersuite(), false);
30✔
404
                  result.test_eq(
90✔
405
                     "MAC algo and PRF algo same for CBC suites", ciphersuite->prf_algo(), ciphersuite->mac_algo());
60✔
406
               }
407

408
               // TODO more tests here
409
            }
410
         }
411

412
         return {result};
3✔
413
      }
2✔
414
};
415

416
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
417

418
class Test_TLS_Algo_Strings : public Test {
1✔
419
   public:
420
      std::vector<Test::Result> run() override {
1✔
421
         std::vector<Test::Result> results;
1✔
422

423
         results.push_back(test_auth_method_strings());
2✔
424
         results.push_back(test_kex_algo_strings());
2✔
425
         results.push_back(test_tls_sig_method_strings());
2✔
426

427
         return results;
1✔
428
      }
×
429

430
   private:
431
      static Test::Result test_tls_sig_method_strings() {
1✔
432
         Test::Result result("TLS::Signature_Scheme");
1✔
433

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

436
         std::set<std::string> scheme_strs;
1✔
437
         for(auto scheme : schemes) {
10✔
438
            std::string scheme_str = scheme.to_string();
9✔
439

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

442
            scheme_strs.insert(scheme_str);
9✔
443
         }
9✔
444

445
         return result;
2✔
446
      }
1✔
447

448
      static Test::Result test_auth_method_strings() {
1✔
449
         Test::Result result("TLS::Auth_Method");
1✔
450

451
         const std::vector<Botan::TLS::Auth_Method> auth_methods({
1✔
452
            Botan::TLS::Auth_Method::RSA,
453
            Botan::TLS::Auth_Method::ECDSA,
454
            Botan::TLS::Auth_Method::IMPLICIT,
455
         });
1✔
456

457
         for(Botan::TLS::Auth_Method meth : auth_methods) {
4✔
458
            std::string meth_str = Botan::TLS::auth_method_to_string(meth);
3✔
459
            result.test_ne("Method string is not empty", meth_str, "");
6✔
460
            Botan::TLS::Auth_Method meth2 = Botan::TLS::auth_method_from_string(meth_str);
3✔
461
            result.confirm("Decoded method matches", meth == meth2);
6✔
462
         }
3✔
463

464
         return result;
1✔
465
      }
1✔
466

467
      static Test::Result test_kex_algo_strings() {
1✔
468
         Test::Result result("TLS::Kex_Algo");
1✔
469

470
         const std::vector<Botan::TLS::Kex_Algo> kex_algos({Botan::TLS::Kex_Algo::STATIC_RSA,
1✔
471
                                                            Botan::TLS::Kex_Algo::DH,
472
                                                            Botan::TLS::Kex_Algo::ECDH,
473
                                                            Botan::TLS::Kex_Algo::PSK,
474
                                                            Botan::TLS::Kex_Algo::ECDHE_PSK});
1✔
475

476
         for(Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
477
            std::string meth_str = Botan::TLS::kex_method_to_string(meth);
5✔
478
            result.test_ne("Method string is not empty", meth_str, "");
10✔
479
            Botan::TLS::Kex_Algo meth2 = Botan::TLS::kex_method_from_string(meth_str);
5✔
480
            result.confirm("Decoded method matches", meth == meth2);
10✔
481
         }
5✔
482

483
         return result;
1✔
484
      }
1✔
485
};
486

487
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
488

489
#endif
490

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