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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

91.16
/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

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

23
#endif
24

25
namespace Botan_Tests {
26

27
#if defined(BOTAN_HAS_TLS)
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

96
   #if defined(BOTAN_HAS_TLS_CBC)
97

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

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

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

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

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

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

122
            void clear() override {}
×
123

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

126
            size_t output_length() const override { return m_mac_len; }
10✔
127

128
            void add_data(const uint8_t /*input*/[], size_t /*length*/) override {}
26✔
129

130
            void final_result(uint8_t out[]) override {
10✔
131
               for(size_t i = 0; i != m_mac_len; ++i)
206✔
132
                  out[i] = 0;
196✔
133
            }
10✔
134

135
            bool has_keying_material() const override { return true; }
×
136

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

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

145
         private:
146
            void key_schedule(const uint8_t /*key*/[], size_t /*length*/) override {}
10✔
147

148
            size_t m_mac_len;
149
      };
150

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

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

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

163
            size_t block_size() const override { return m_bs; }
40✔
164

165
            void clear() override {}
×
166

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

169
            bool has_keying_material() const override { return true; }
×
170

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

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

179
         private:
180
            void key_schedule(const uint8_t /*key*/[], size_t /*length*/) override {}
10✔
181

182
            size_t m_bs;
183
      };
184

185
      TLS_CBC_Tests() : Text_Based_Test("tls_cbc.vec", "Blocksize,MACsize,Record,Valid") {}
3✔
186

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

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

195
         // todo test permutations
196
         bool encrypt_then_mac = false;
10✔
197

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

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

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

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

224
         return result;
10✔
225
      }
30✔
226
};
227

228
BOTAN_REGISTER_TEST("tls", "tls_cbc", TLS_CBC_Tests);
229

230
   #endif
231

232
class Test_TLS_Alert_Strings : public Test {
1✔
233
   public:
234
      std::vector<Test::Result> run() override {
1✔
235
         Test::Result result("TLS::Alert::type_string");
1✔
236

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

273
         std::set<std::string> seen;
1✔
274

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

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

283
         result.test_eq("Unknown alert str", unknown_alert.type_string(), "unrecognized_alert_66");
3✔
284

285
         return {result};
3✔
286
      }
2✔
287
};
288

289
BOTAN_REGISTER_TEST("tls", "tls_alert_strings", Test_TLS_Alert_Strings);
290

291
class Test_TLS_Policy_Text : public Test {
1✔
292
   public:
293
      std::vector<Test::Result> run() override {
1✔
294
         Test::Result result("TLS Policy");
1✔
295

296
         const std::vector<std::string> policies = {"default", "suiteb_128", "suiteb_192", "strict", "datagram", "bsi"};
7✔
297

298
         for(const std::string& policy : policies) {
7✔
299
            const std::string from_policy_obj = tls_policy_string(policy);
6✔
300
            std::string from_file =
6✔
301
   #if defined(BOTAN_HAS_TLS_13)
302
               read_tls_policy(policy + (policy == "default" || policy == "strict" ? "_tls13" : ""));
7✔
303
   #else
304
               read_tls_policy(policy);
305
   #endif
306

307
            result.test_eq("Values for TLS " + policy + " policy", from_file, from_policy_obj);
12✔
308
         }
12✔
309

310
         return {result};
3✔
311
      }
1✔
312

313
   private:
314
      static std::string read_tls_policy(const std::string& policy_str) {
6✔
315
         const std::string fspath = Test::data_file("tls-policy/" + policy_str + ".txt");
12✔
316

317
         std::ifstream is(fspath.c_str());
6✔
318
         if(!is.good()) {
6✔
319
            throw Test_Error("Missing policy file " + fspath);
×
320
         }
321

322
         Botan::TLS::Text_Policy policy(is);
6✔
323
         return policy.to_string();
6✔
324
      }
12✔
325

326
      static std::string tls_policy_string(const std::string& policy_str) {
6✔
327
         std::unique_ptr<Botan::TLS::Policy> policy;
6✔
328
         if(policy_str == "default") {
6✔
329
            policy = std::make_unique<Botan::TLS::Policy>();
1✔
330
         } else if(policy_str == "suiteb_128") {
5✔
331
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_128>();
1✔
332
         } else if(policy_str == "suiteb_192") {
4✔
333
            policy = std::make_unique<Botan::TLS::NSA_Suite_B_192>();
1✔
334
         } else if(policy_str == "bsi") {
3✔
335
            policy = std::make_unique<Botan::TLS::BSI_TR_02102_2>();
1✔
336
         } else if(policy_str == "strict") {
2✔
337
            policy = std::make_unique<Botan::TLS::Strict_Policy>();
1✔
338
         } else if(policy_str == "datagram") {
1✔
339
            policy = std::make_unique<Botan::TLS::Datagram_Policy>();
1✔
340
         } else {
341
            throw Test_Error("Unknown TLS policy type '" + policy_str + "'");
×
342
         }
343

344
         return policy->to_string();
6✔
345
      }
6✔
346
};
347

348
BOTAN_REGISTER_TEST("tls", "tls_policy_text", Test_TLS_Policy_Text);
349

350
class Test_TLS_Ciphersuites : public Test {
1✔
351
   public:
352
      std::vector<Test::Result> run() override {
1✔
353
         Test::Result result("TLS::Ciphersuite");
1✔
354

355
         for(size_t csuite_id = 0; csuite_id <= 0xFFFF; ++csuite_id) {
65,537✔
356
            const uint16_t csuite_id16 = static_cast<uint16_t>(csuite_id);
65,536✔
357
            auto ciphersuite = Botan::TLS::Ciphersuite::by_id(csuite_id16);
65,536✔
358

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

362
               if(ciphersuite->cbc_ciphersuite() == false) {
94✔
363
                  result.test_eq("Expected AEAD ciphersuite", ciphersuite->aead_ciphersuite(), true);
64✔
364
                  result.test_eq("Expected MAC name for AEAD ciphersuites", ciphersuite->mac_algo(), "AEAD");
128✔
365
               } else {
366
                  result.test_eq("Did not expect AEAD ciphersuite", ciphersuite->aead_ciphersuite(), false);
30✔
367
                  result.test_eq(
90✔
368
                     "MAC algo and PRF algo same for CBC suites", ciphersuite->prf_algo(), ciphersuite->mac_algo());
60✔
369
               }
370

371
               // TODO more tests here
372
            }
373
         }
374

375
         return {result};
3✔
376
      }
1✔
377
};
378

379
BOTAN_REGISTER_TEST("tls", "tls_ciphersuites", Test_TLS_Ciphersuites);
380

381
class Test_TLS_Algo_Strings : public Test {
1✔
382
   public:
383
      std::vector<Test::Result> run() override {
1✔
384
         std::vector<Test::Result> results;
1✔
385

386
         results.push_back(test_auth_method_strings());
2✔
387
         results.push_back(test_kex_algo_strings());
2✔
388
         results.push_back(test_tls_sig_method_strings());
2✔
389

390
         return results;
1✔
391
      }
×
392

393
   private:
394
      static Test::Result test_tls_sig_method_strings() {
1✔
395
         Test::Result result("TLS::Signature_Scheme");
1✔
396

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

399
         std::set<std::string> scheme_strs;
1✔
400
         for(auto scheme : schemes) {
10✔
401
            std::string scheme_str = scheme.to_string();
9✔
402

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

405
            scheme_strs.insert(scheme_str);
9✔
406
         }
9✔
407

408
         return result;
1✔
409
      }
2✔
410

411
      static Test::Result test_auth_method_strings() {
1✔
412
         Test::Result result("TLS::Auth_Method");
1✔
413

414
         const std::vector<Botan::TLS::Auth_Method> auth_methods({
1✔
415
            Botan::TLS::Auth_Method::RSA,
416
            Botan::TLS::Auth_Method::ECDSA,
417
            Botan::TLS::Auth_Method::IMPLICIT,
418
         });
1✔
419

420
         for(Botan::TLS::Auth_Method meth : auth_methods) {
4✔
421
            std::string meth_str = Botan::TLS::auth_method_to_string(meth);
3✔
422
            result.test_ne("Method string is not empty", meth_str, "");
6✔
423
            Botan::TLS::Auth_Method meth2 = Botan::TLS::auth_method_from_string(meth_str);
3✔
424
            result.confirm("Decoded method matches", meth == meth2);
9✔
425
         }
3✔
426

427
         return result;
1✔
428
      }
1✔
429

430
      static Test::Result test_kex_algo_strings() {
1✔
431
         Test::Result result("TLS::Kex_Algo");
1✔
432

433
         const std::vector<Botan::TLS::Kex_Algo> kex_algos({Botan::TLS::Kex_Algo::STATIC_RSA,
1✔
434
                                                            Botan::TLS::Kex_Algo::DH,
435
                                                            Botan::TLS::Kex_Algo::ECDH,
436
                                                            Botan::TLS::Kex_Algo::PSK,
437
                                                            Botan::TLS::Kex_Algo::ECDHE_PSK});
1✔
438

439
         for(Botan::TLS::Kex_Algo meth : kex_algos) {
6✔
440
            std::string meth_str = Botan::TLS::kex_method_to_string(meth);
5✔
441
            result.test_ne("Method string is not empty", meth_str, "");
10✔
442
            Botan::TLS::Kex_Algo meth2 = Botan::TLS::kex_method_from_string(meth_str);
5✔
443
            result.confirm("Decoded method matches", meth == meth2);
15✔
444
         }
5✔
445

446
         return result;
1✔
447
      }
1✔
448
};
449

450
BOTAN_REGISTER_TEST("tls", "tls_algo_strings", Test_TLS_Algo_Strings);
451

452
#endif
453

454
}
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

© 2025 Coveralls, Inc