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

randombit / botan / 24115861258

08 Apr 2026 03:20AM UTC coverage: 89.45% (-2.4%) from 91.821%
24115861258

Pull #5523

github

web-flow
Merge 8045a4e76 into 570b415e2
Pull Request #5523: Update BoGo shim, various TLS updates

106161 of 118682 relevant lines covered (89.45%)

11530654.21 hits per line

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

87.53
/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_external_psk.h>
17
   #include <botan/tls_policy.h>
18
   #include <botan/tls_session.h>
19
   #include <botan/tls_signature_scheme.h>
20
   #include <botan/tls_version.h>
21
   #include <botan/x509cert.h>
22
   #include <botan/internal/fmt.h>
23
   #include <set>
24

25
   #if defined(BOTAN_HAS_TLS_CBC)
26
      #include <botan/block_cipher.h>
27
      #include <botan/mac.h>
28
      #include <botan/internal/tls_cbc.h>
29
   #endif
30

31
   #if defined(BOTAN_HAS_TLS_NULL)
32
      #include <botan/internal/tls_null.h>
33
   #endif
34

35
#endif
36

37
namespace Botan_Tests {
38

39
namespace {
40

41
#if defined(BOTAN_HAS_TLS)
42

43
class TLS_Session_Tests final : public Test {
1✔
44
   public:
45
      std::vector<Test::Result> run() override {
1✔
46
         Test::Result result("TLS::Session");
1✔
47

48
         const Botan::TLS::Session session(Botan::secure_vector<uint8_t>{0xCC, 0xDD},
1✔
49
                                           Botan::TLS::Protocol_Version::TLS_V12,
50
                                           0xC02F,
51
                                           Botan::TLS::Connection_Side::Client,
52
                                           true,
53
                                           false,
54
                                           std::vector<Botan::X509_Certificate>(),
1✔
55
                                           Botan::TLS::Server_Information("server"),
1✔
56
                                           0x0000,
57
                                           std::chrono::system_clock::now());
2✔
58

59
         const std::string pem = session.PEM_encode();
1✔
60
         const Botan::TLS::Session session_from_pem(pem);
1✔
61
         result.test_bin_eq("Roundtrip from pem", session.DER_encode(), session_from_pem.DER_encode());
2✔
62

63
         const auto der = session.DER_encode();
1✔
64
         const Botan::TLS::Session session_from_der(der);
1✔
65
         result.test_bin_eq("Roundtrip from der", session.DER_encode(), session_from_der.DER_encode());
2✔
66

67
         const Botan::SymmetricKey key("ABCDEF");
1✔
68
         const std::vector<uint8_t> ctext1 = session.encrypt(key, this->rng());
1✔
69
         const std::vector<uint8_t> ctext2 = session.encrypt(key, this->rng());
1✔
70

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

73
         result.test_bin_eq(
1✔
74
            "TLS session encryption same header", std::span{ctext1}.first(12), "068B5A9D396C0000F2322CAE");
75
         result.test_bin_eq(
1✔
76
            "TLS session encryption same header", std::span{ctext2}.first(12), "068B5A9D396C0000F2322CAE");
77

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

80
         Fixed_Output_RNG frng1("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
81
         const std::vector<uint8_t> ctextf1 = session.encrypt(key, frng1);
1✔
82
         Fixed_Output_RNG frng2("00112233445566778899AABBCCDDEEFF802802802802802802802802");
1✔
83
         const std::vector<uint8_t> ctextf2 = session.encrypt(key, frng2);
1✔
84

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

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

99
         result.test_throws("unknown ciphersuite during session parsing",
1✔
100
                            "Serialized TLS session contains unknown cipher suite (47789)",
101
                            [&] { Botan::TLS::Session{pem_with_unknown_ciphersuite}; });
2✔
102

103
         return {result};
3✔
104
      }
5✔
105
};
106

107
BOTAN_REGISTER_TEST("tls", "tls_session", TLS_Session_Tests);
108

109
   #if defined(BOTAN_HAS_TLS_CBC)
110

111
class TLS_CBC_Padding_Tests final : public Text_Based_Test {
×
112
   public:
113
      TLS_CBC_Padding_Tests() : Text_Based_Test("tls_cbc_padding.vec", "Record,Output") {}
2✔
114

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

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

121
         Test::Result result("TLS CBC padding check");
22✔
122
         result.test_sz_eq("Expected", res, output);
22✔
123
         return result;
22✔
124
      }
22✔
125
};
126

127
BOTAN_REGISTER_TEST("tls", "tls_cbc_padding", TLS_CBC_Padding_Tests);
128

129
class TLS_CBC_Tests final : public Text_Based_Test {
×
130
   public:
131
      class ZeroMac : public Botan::MessageAuthenticationCode {
132
         public:
133
            explicit ZeroMac(size_t mac_len) : m_mac_len(mac_len) {}
10✔
134

135
            void clear() override {}
×
136

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

139
            size_t output_length() const override { return m_mac_len; }
20✔
140

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

143
            void final_result(std::span<uint8_t> out) override {
10✔
144
               for(size_t i = 0; i != m_mac_len; ++i) {
206✔
145
                  out[i] = 0;
196✔
146
               }
147
            }
10✔
148

149
            bool has_keying_material() const override { return true; }
×
150

151
            Botan::Key_Length_Specification key_spec() const override {
20✔
152
               return Botan::Key_Length_Specification(0, 0, 1);
20✔
153
            }
154

155
            std::unique_ptr<MessageAuthenticationCode> new_object() const override {
×
156
               return std::make_unique<ZeroMac>(m_mac_len);
×
157
            }
158

159
         private:
160
            void key_schedule(std::span<const uint8_t> /* key */) override {}
10✔
161

162
            size_t m_mac_len;
163
      };
164

165
      class Noop_Block_Cipher : public Botan::BlockCipher {
166
         public:
167
            explicit Noop_Block_Cipher(size_t bs) : m_bs(bs) {}
10✔
168

169
            void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
×
170
               Botan::copy_mem(out, in, blocks * m_bs);
×
171
            }
×
172

173
            void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
10✔
174
               Botan::copy_mem(out, in, blocks * m_bs);
10✔
175
            }
10✔
176

177
            size_t block_size() const override { return m_bs; }
40✔
178

179
            void clear() override {}
×
180

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

183
            bool has_keying_material() const override { return true; }
×
184

185
            Botan::Key_Length_Specification key_spec() const override {
30✔
186
               return Botan::Key_Length_Specification(0, 0, 1);
30✔
187
            }
188

189
            std::unique_ptr<BlockCipher> new_object() const override {
×
190
               return std::make_unique<Noop_Block_Cipher>(m_bs);
×
191
            }
192

193
         private:
194
            void key_schedule(std::span<const uint8_t> /*key*/) override {}
10✔
195

196
            size_t m_bs;
197
      };
198

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

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

204
         const size_t block_size = vars.get_req_sz("Blocksize");
10✔
205
         const size_t mac_len = vars.get_req_sz("MACsize");
10✔
206
         const std::vector<uint8_t> record = vars.get_req_bin("Record");
10✔
207
         const bool is_valid = vars.get_req_sz("Valid") == 1;
10✔
208

209
         // todo test permutations
210
         const bool encrypt_then_mac = false;
10✔
211

212
         Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption tls_cbc(std::make_unique<Noop_Block_Cipher>(block_size),
30✔
213
                                                          std::make_unique<ZeroMac>(mac_len),
10✔
214
                                                          0,
215
                                                          0,
216
                                                          Botan::TLS::Protocol_Version::TLS_V12,
217
                                                          encrypt_then_mac);
20✔
218

219
         tls_cbc.set_key(std::vector<uint8_t>(0));
10✔
220
         std::vector<uint8_t> ad(13);
10✔
221
         tls_cbc.set_associated_data(ad.data(), ad.size());
10✔
222

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

225
         try {
10✔
226
            tls_cbc.finish(vec, 0);
10✔
227
            if(is_valid) {
4✔
228
               result.test_success("Accepted valid TLS-CBC ciphertext");
4✔
229
            } else {
230
               result.test_failure("Accepted invalid TLS-CBC ciphertext");
×
231
            }
232
         } catch(std::exception&) {
6✔
233
            if(is_valid) {
6✔
234
               result.test_failure("Rejected valid TLS-CBC ciphertext");
×
235
            } else {
236
               result.test_success("Accepted invalid TLS-CBC ciphertext");
6✔
237
            }
238
         }
6✔
239

240
         return result;
20✔
241
      }
10✔
242
};
243

244
class TLS_CBC_KAT_Tests final : public Text_Based_Test {
×
245
   public:
246
      TLS_CBC_KAT_Tests() :
1✔
247
            Text_Based_Test(
248
               "tls_cbc_kat.vec",
249
               "BlockCipher,MAC,KeylenCipher,KeylenMAC,EncryptThenMAC,Protocol,Key,AssociatedData,Nonce,Plaintext,Ciphertext") {
2✔
250
      }
1✔
251

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

255
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption>(result, vars);
10✔
256
         run_kat<Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption>(result, vars);
10✔
257

258
         return result;
10✔
259
      }
×
260

261
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
10✔
262
         try {
10✔
263
            std::ignore = get_cipher_and_mac(vars);
10✔
264
            return false;
10✔
265
         } catch(const Botan::Lookup_Error&) {
×
266
            return true;
×
267
         }
×
268
      }
269

270
   private:
271
      [[nodiscard]] static std::pair<std::unique_ptr<Botan::BlockCipher>,
272
                                     std::unique_ptr<Botan::MessageAuthenticationCode>>
273
      get_cipher_and_mac(const VarMap& vars) {
30✔
274
         return {
30✔
275
            Botan::BlockCipher::create_or_throw(vars.get_req_str("BlockCipher")),
60✔
276
            Botan::MessageAuthenticationCode::create_or_throw(vars.get_req_str("MAC")),
30✔
277
         };
60✔
278
      }
279

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

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

307
         const auto key = vars.get_req_bin("Key");
20✔
308
         const auto ad = vars.get_req_bin("AssociatedData");
20✔
309
         const auto nonce = vars.get_req_bin("Nonce");
20✔
310
         const auto pt = vars.get_req_bin("Plaintext");
20✔
311
         const auto ct = vars.get_req_bin("Ciphertext");
20✔
312

313
         auto [cipher, mac] = get_cipher_and_mac(vars);
20✔
314

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

317
         tls_cbc.set_key(key);
20✔
318
         tls_cbc.set_associated_data(ad);
20✔
319

320
         std::vector<uint8_t> in(pt.begin(), pt.end());
20✔
321
         std::vector<uint8_t> out(ct.begin(), ct.end());
20✔
322

323
         if constexpr(!encrypt) {
324
            std::swap(in, out);
10✔
325
         }
326

327
         // Test 1: process the entire message at once
328
         std::vector<uint8_t> inout = in;
20✔
329
         tls_cbc.start(nonce);
20✔
330
         tls_cbc.finish(inout);  // in-place processing ('in' should now contain 'out')
20✔
331
         result.test_bin_eq(std::string("expected output of ") + direction, inout, out);
60✔
332

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

342
         std::vector<uint8_t> chunked_out(in_span.begin(), in_span.end());
20✔
343
         tls_cbc.finish(chunked_out);
20✔
344
         result.test_bin_eq(std::string("expected output with chunking of ") + direction, chunked_out, out);
60✔
345
      }
20✔
346
};
347

348
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
349
BOTAN_REGISTER_TEST("tls", "tls_cbc_kat", TLS_CBC_KAT_Tests);
350

351
   #endif
352

353
   #if defined(BOTAN_HAS_TLS_NULL)
354

355
class TLS_Null_Tests final : public Text_Based_Test {
×
356
   public:
357
      TLS_Null_Tests() : Text_Based_Test("tls_null.vec", "Hash,Key,AssociatedData,Message,Fragment") {}
2✔
358

359
      void encryption_test(Test::Result& result,
3✔
360
                           const std::string& hash,
361
                           const std::vector<uint8_t>& key,
362
                           const std::vector<uint8_t>& associated_data,
363
                           const std::vector<uint8_t>& message,
364
                           const std::vector<uint8_t>& expected_tls_fragment) {
365
         auto mac = Botan::MessageAuthenticationCode::create_or_throw(Botan::fmt("HMAC({})", hash));
3✔
366

367
         const auto mac_output_length = mac->output_length();
3✔
368
         Botan::TLS::TLS_NULL_HMAC_AEAD_Encryption tls_null_encrypt(std::move(mac), mac_output_length);
3✔
369

370
         tls_null_encrypt.set_key(key);
3✔
371
         tls_null_encrypt.set_associated_data(associated_data);
3✔
372

373
         Botan::secure_vector<uint8_t> buffer(message.begin(), message.end());
3✔
374
         tls_null_encrypt.finish(buffer);
3✔
375

376
         result.test_bin_eq("Encrypted TLS fragment matches expectation", buffer, expected_tls_fragment);
3✔
377
      }
3✔
378

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

388
         const auto mac_output_length = mac->output_length();
4✔
389
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
4✔
390

391
         tls_null_decrypt.set_key(key);
4✔
392
         tls_null_decrypt.set_associated_data(associated_data);
4✔
393

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

396
         if(header == "InvalidMAC") {
4✔
397
            result.test_throws("TLS_NULL_HMAC_AEAD_Decryption::finish()", "Message authentication failure", [&]() {
1✔
398
               tls_null_decrypt.finish(buffer, 0);
1✔
399
            });
400
         } else {
401
            tls_null_decrypt.finish(buffer, 0);
3✔
402
            result.test_bin_eq("Decrypted TLS fragment matches expectation", buffer, expected_message);
3✔
403
         }
404
      }
4✔
405

406
      void invalid_ad_length_test(Test::Result& result,
1✔
407
                                  const std::string& hash,
408
                                  const std::vector<uint8_t>& associated_data) {
409
         auto mac = Botan::MessageAuthenticationCode::create_or_throw(Botan::fmt("HMAC({})", hash));
1✔
410

411
         const auto mac_output_length = mac->output_length();
1✔
412
         Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption tls_null_decrypt(std::move(mac), mac_output_length);
1✔
413

414
         result.test_throws<Botan::Invalid_Argument>("TLS_NULL_HMAC_AEAD_Decryption::set_associated_data()",
1✔
415
                                                     [&]() { tls_null_decrypt.set_associated_data(associated_data); });
2✔
416
         return;
1✔
417
      }
1✔
418

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

422
         const std::string hash = vars.get_req_str("Hash");
5✔
423
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
5✔
424
         const std::vector<uint8_t> associated_data = vars.get_req_bin("AssociatedData");
5✔
425
         const std::vector<uint8_t> expected_message = vars.get_req_bin("Message");
5✔
426
         const std::vector<uint8_t> tls_fragment = vars.get_req_bin("Fragment");
5✔
427

428
         if(header.empty()) {
5✔
429
            encryption_test(result, hash, key, associated_data, expected_message, tls_fragment);
3✔
430
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
3✔
431
         }
432

433
         if(header == "InvalidMAC") {
5✔
434
            decryption_test(result, hash, key, associated_data, expected_message, tls_fragment, header);
1✔
435
         }
436

437
         if(header == "InvalidAssociatedDataLength") {
5✔
438
            invalid_ad_length_test(result, hash, associated_data);
1✔
439
         }
440

441
         return result;
10✔
442
      }
5✔
443
};
444

445
BOTAN_REGISTER_TEST("tls", "tls_null", TLS_Null_Tests);
446

447
   #endif
448

449
class Test_TLS_Alert_Strings : public Test {
1✔
450
   public:
451
      std::vector<Test::Result> run() override {
1✔
452
         Test::Result result("TLS::Alert::type_string");
1✔
453

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

490
         std::set<std::string> seen;
1✔
491

492
         for(auto alert : alert_types) {
34✔
493
            const std::string str = Botan::TLS::Alert(alert).type_string();
33✔
494
            result.test_sz_eq("No duplicate strings", seen.count(str), 0);
33✔
495
            seen.insert(str);
33✔
496
         }
33✔
497

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

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

502
         return {result};
3✔
503
      }
2✔
504
};
505

506
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
507

508
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13) && defined(BOTAN_HAS_TLS_13_PQC) && \
509
      defined(BOTAN_HAS_X25519) && defined(BOTAN_HAS_X448)
510

511
class Test_TLS_Policy_Text : public Test {
1✔
512
   public:
513
      std::vector<Test::Result> run() override {
1✔
514
         Test::Result result("TLS Policy");
1✔
515

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

518
         for(const std::string& policy : policies) {
7✔
519
            const std::string from_policy_obj = tls_policy_string(policy);
6✔
520

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

523
            const std::string from_file = read_tls_policy(policy_file);
6✔
524

525
            if(from_policy_obj != from_file) {
6✔
526
               const std::string d = diff(from_policy_obj, from_file);
×
527
               result.test_failure(Botan::fmt("Values for TLS policy from {} don't match (diff {})", policy_file, d));
×
528
            } else {
×
529
               result.test_success("Values from TLS policy from " + policy_file + " match");
12✔
530
            }
531
         }
6✔
532

533
         return {result};
3✔
534
      }
8✔
535

536
   private:
537
      static std::string diff(const std::string& a_str, const std::string& b_str) {
×
538
         std::istringstream a_ss(a_str);
×
539
         std::istringstream b_ss(b_str);
×
540

541
         std::ostringstream diff;
×
542

543
         for(;;) {
×
544
            if(!a_ss && !b_ss) {
×
545
               break;  // done
546
            }
547

548
            std::string a_line;
×
549
            std::getline(a_ss, a_line, '\n');
×
550

551
            std::string b_line;
×
552
            std::getline(b_ss, b_line, '\n');
×
553

554
            if(a_line != b_line) {
×
555
               diff << "- " << a_line << "\n"
×
556
                    << "+ " << b_line << "\n";
×
557
            }
558
         }
×
559

560
         return diff.str();
×
561
      }
×
562

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

566
         std::ifstream is(fspath.c_str());
6✔
567
         if(!is.good()) {
6✔
568
            throw Test_Error("Missing policy file " + fspath);
×
569
         }
570

571
         const Botan::TLS::Text_Policy policy(is);
6✔
572
         return policy.to_string();
6✔
573
      }
6✔
574

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

593
         return policy->to_string();
6✔
594
      }
6✔
595
};
596

597
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
598
   #endif
599

600
class Test_TLS_Ciphersuites : public Test {
1✔
601
   public:
602
      std::vector<Test::Result> run() override {
1✔
603
         Test::Result result("TLS::Ciphersuite");
1✔
604

605
         for(size_t csuite_id = 0; csuite_id <= 0xFFFF; ++csuite_id) {
65,537✔
606
            const uint16_t csuite_id16 = static_cast<uint16_t>(csuite_id);
65,536✔
607
            auto ciphersuite = Botan::TLS::Ciphersuite::by_id(csuite_id16);
65,536✔
608

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

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

622
               if(ciphersuite->null_ciphersuite()) {
102✔
623
                  result.test_str_eq("Expected NULL ciphersuite", ciphersuite->cipher_algo(), "NULL");
8✔
624
               };
625

626
               // TODO more tests here
627
            }
628
         }
629

630
         return {result};
3✔
631
      }
2✔
632
};
633

634
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
635

636
class Test_TLS_Algo_Strings : public Test {
1✔
637
   public:
638
      std::vector<Test::Result> run() override {
1✔
639
         std::vector<Test::Result> results;
1✔
640

641
         results.push_back(test_auth_method_strings());
2✔
642
         results.push_back(test_kex_algo_strings());
2✔
643
         results.push_back(test_tls_sig_method_strings());
2✔
644

645
         return results;
1✔
646
      }
×
647

648
   private:
649
      static Test::Result test_tls_sig_method_strings() {
1✔
650
         Test::Result result("TLS::Signature_Scheme");
1✔
651

652
         std::set<std::string> scheme_strs;
1✔
653
         for(auto scheme : Botan::TLS::Signature_Scheme::all_available_schemes()) {
10✔
654
            const std::string scheme_str = scheme.to_string();
9✔
655

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

658
            scheme_strs.insert(scheme_str);
9✔
659
         }
9✔
660

661
         return result;
1✔
662
      }
1✔
663

664
      static Test::Result test_auth_method_strings() {
1✔
665
         Test::Result result("TLS::Auth_Method");
1✔
666

667
         const std::vector<Botan::TLS::Auth_Method> auth_methods({
1✔
668
            Botan::TLS::Auth_Method::RSA,
669
            Botan::TLS::Auth_Method::ECDSA,
670
            Botan::TLS::Auth_Method::IMPLICIT,
671
         });
1✔
672

673
         for(const Botan::TLS::Auth_Method meth : auth_methods) {
4✔
674
            const std::string meth_str = Botan::TLS::auth_method_to_string(meth);
3✔
675
            result.test_str_not_empty("Method string is not empty", meth_str);
3✔
676
            const Botan::TLS::Auth_Method meth2 = Botan::TLS::auth_method_from_string(meth_str);
3✔
677
            result.test_is_true("Decoded method matches", meth == meth2);
3✔
678
         }
3✔
679

680
         return result;
1✔
681
      }
1✔
682

683
      static Test::Result test_kex_algo_strings() {
1✔
684
         Test::Result result("TLS::Kex_Algo");
1✔
685

686
         const std::vector<Botan::TLS::Kex_Algo> kex_algos({Botan::TLS::Kex_Algo::STATIC_RSA,
1✔
687
                                                            Botan::TLS::Kex_Algo::DH,
688
                                                            Botan::TLS::Kex_Algo::ECDH,
689
                                                            Botan::TLS::Kex_Algo::PSK,
690
                                                            Botan::TLS::Kex_Algo::ECDHE_PSK});
1✔
691

692
         for(const Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
693
            const std::string meth_str = Botan::TLS::kex_method_to_string(meth);
5✔
694
            result.test_str_not_empty("Method string is not empty", meth_str);
5✔
695
            const Botan::TLS::Kex_Algo meth2 = Botan::TLS::kex_method_from_string(meth_str);
5✔
696
            result.test_is_true("Decoded method matches", meth == meth2);
5✔
697
         }
5✔
698

699
         return result;
1✔
700
      }
1✔
701
};
702

703
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
704

705
class TLS13_PSK_Import_Tests final : public Text_Based_Test {
×
706
   public:
707
      TLS13_PSK_Import_Tests() :
1✔
708
            Text_Based_Test("tls_13_psk_import.vec", "Key,Identity,TargetHash,Output", "Context") {}
2✔
709

710
      Test::Result run_one_test(const std::string& hash_name, const VarMap& vars) override {
6✔
711
         Test::Result result("PSK Import " + hash_name);
6✔
712

713
         const auto key = vars.get_req_bin("Key");
6✔
714
         const auto identity = vars.get_req_bin("Identity");
6✔
715
         const auto context = vars.get_opt_bin("Context");
6✔
716
         const auto target_hash = vars.get_req_str("TargetHash");
6✔
717
         const auto expected = vars.get_req_bin("Output");
6✔
718

719
         const Botan::TLS::PSKImporter importer(key, identity, context, hash_name);
6✔
720
         auto psk = importer.derive_imported_psk(Botan::TLS::Protocol_Version::TLS_V13, target_hash);
6✔
721

722
         result.test_is_true("PSK is marked as imported", psk.is_imported());
6✔
723
         result.test_str_eq("PRF algo matches target hash", psk.prf_algo(), target_hash);
6✔
724
         result.test_bin_eq("Derived PSK", psk.extract_master_secret(), expected);
6✔
725

726
         return result;
6✔
727
      }
6✔
728
};
729

730
BOTAN_REGISTER_TEST("tls", "tls13_psk_import", TLS13_PSK_Import_Tests);
731

732
#endif
733

734
}  // namespace
735

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