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

randombit / botan / 20160266155

12 Dec 2025 08:01AM UTC coverage: 90.357% (-0.001%) from 90.358%
20160266155

push

github

web-flow
Merge pull request #5172 from randombit/jack/fix-clang-tidy-misc-const-correctness

Fix and enable clang-tidy warning misc-const-correctness

100951 of 111725 relevant lines covered (90.36%)

12817908.54 hits per line

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

93.91
/src/tests/test_x509_path.cpp
1
/*
2
* (C) 2006,2011,2012,2014,2015 Jack Lloyd
3
* (C) 2022 René Meusel, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_X509_CERTIFICATES)
11
   #include <botan/data_src.h>
12
   #include <botan/exceptn.h>
13
   #include <botan/pk_keys.h>
14
   #include <botan/pkcs10.h>
15
   #include <botan/x509_crl.h>
16
   #include <botan/x509path.h>
17
   #include <botan/internal/calendar.h>
18
   #include <botan/internal/filesystem.h>
19
   #include <botan/internal/fmt.h>
20
   #include <botan/internal/parsing.h>
21

22
   #if defined(BOTAN_HAS_ECDSA)
23
      #include <botan/ec_group.h>
24
   #endif
25

26
   #include <algorithm>
27
   #include <fstream>
28
   #include <limits>
29
   #include <map>
30
   #include <string>
31
   #include <vector>
32
#endif
33

34
namespace Botan_Tests {
35

36
namespace {
37

38
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
39

40
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
41

42
std::map<std::string, std::string> read_results(const std::string& results_file, const char delim = ':') {
9✔
43
   std::ifstream in(results_file);
9✔
44
   if(!in.good()) {
9✔
45
      throw Test_Error("Failed reading " + results_file);
×
46
   }
47

48
   std::map<std::string, std::string> m;
9✔
49
   std::string line;
9✔
50
   while(in.good()) {
609✔
51
      std::getline(in, line);
600✔
52
      if(line.empty()) {
600✔
53
         continue;
27✔
54
      }
55
      if(line[0] == '#') {
583✔
56
         continue;
10✔
57
      }
58

59
      std::vector<std::string> parts = Botan::split_on(line, delim);
573✔
60

61
      if(parts.size() != 2) {
573✔
62
         throw Test_Error("Invalid line " + line);
×
63
      }
64

65
      m[parts[0]] = parts[1];
573✔
66
   }
573✔
67

68
   return m;
18✔
69
}
9✔
70

71
std::vector<Botan::X509_Certificate> load_cert_file(const std::string& filename) {
80✔
72
   Botan::DataSource_Stream in(filename);
80✔
73

74
   std::vector<Botan::X509_Certificate> certs;
80✔
75
   while(!in.end_of_data()) {
508✔
76
      try {
348✔
77
         certs.emplace_back(in);
348✔
78
      } catch(Botan::Decoding_Error&) {}
80✔
79
   }
80

81
   return certs;
80✔
82
}
80✔
83

84
std::set<Botan::Certificate_Status_Code> flatten(const Botan::CertificatePathStatusCodes& codes) {
4✔
85
   std::set<Botan::Certificate_Status_Code> result;
4✔
86

87
   for(const auto& statuses : codes) {
16✔
88
      result.insert(statuses.begin(), statuses.end());
12✔
89
   }
90

91
   return result;
4✔
92
}
×
93

94
Botan::Path_Validation_Restrictions get_default_restrictions(bool req_revocation_info, bool ocsp_all_intermediates) {
5✔
95
   return Botan::Path_Validation_Restrictions(req_revocation_info, 80 /*some tests use SHA-1*/, ocsp_all_intermediates);
10✔
96
}
97

98
Botan::Path_Validation_Restrictions get_allow_non_self_signed_anchors_restrictions(bool req_revocation_info,
6✔
99
                                                                                   bool ocsp_all_intermediates) {
100
   return Botan::Path_Validation_Restrictions(req_revocation_info,
6✔
101
                                              80, /*some tests use SHA-1*/
102
                                              ocsp_all_intermediates,
103
                                              std::chrono::seconds::zero(),
104
                                              std::make_unique<Botan::Certificate_Store_In_Memory>(),
6✔
105
                                              false,
106
                                              false /* require_self_signed_trust_anchors */);
12✔
107
}
108

109
std::vector<Botan::Path_Validation_Restrictions> restrictions_to_test(bool req_revocation_info,
3✔
110
                                                                      bool ocsp_all_intermediates) {
111
   std::vector<Botan::Path_Validation_Restrictions> restrictions;
3✔
112
   restrictions.emplace_back(get_default_restrictions(req_revocation_info, ocsp_all_intermediates));
3✔
113
   restrictions.emplace_back(
3✔
114
      get_allow_non_self_signed_anchors_restrictions(req_revocation_info, ocsp_all_intermediates));
6✔
115
   return restrictions;
3✔
116
}
×
117

118
class X509test_Path_Validation_Tests final : public Test {
×
119
   public:
120
      std::vector<Test::Result> run() override {
1✔
121
         std::vector<Test::Result> results;
1✔
122
         for(const auto& restrictions : restrictions_to_test(false, false)) {
3✔
123
            auto partial_res = run_with_restrictions(restrictions);
2✔
124
            results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
125
         }
3✔
126
         return results;
1✔
127
      }
×
128

129
   private:
130
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions) {
2✔
131
         std::vector<Test::Result> results;
2✔
132

133
         // Test certs generated by https://github.com/yymax/x509test
134
         const Botan::X509_Certificate root(Test::data_file("x509/x509test/root.pem"));
4✔
135
         Botan::Certificate_Store_In_Memory trusted;
2✔
136
         trusted.add_certificate(root);
2✔
137

138
         auto validation_time = Botan::calendar_point(2016, 10, 21, 4, 20, 0).to_std_timepoint();
2✔
139

140
         for(const auto& [filename, expected_result] : read_results(Test::data_file("x509/x509test/expected.txt"))) {
76✔
141
            Test::Result result("X509test path validation");
74✔
142
            result.start_timer();
74✔
143

144
            const std::vector<Botan::X509_Certificate> certs =
74✔
145
               load_cert_file(Test::data_file("x509/x509test/" + filename));
148✔
146

147
            if(certs.empty()) {
74✔
148
               throw Test_Error("Failed to read certs from " + filename);
×
149
            }
150

151
            Botan::Path_Validation_Result path_result = Botan::x509_path_validate(
74✔
152
               certs, restrictions, trusted, "www.tls.test", Botan::Usage_Type::TLS_SERVER_AUTH, validation_time);
74✔
153

154
            if(path_result.successful_validation() && path_result.trust_root() != root) {
74✔
155
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
156
            }
157

158
            result.test_eq("test " + filename, path_result.result_string(), expected_result);
148✔
159
            result.test_eq("test no warnings string", path_result.warnings_string(), "");
148✔
160
            result.confirm("test no warnings", path_result.no_warnings());
148✔
161
            result.end_timer();
74✔
162
            results.push_back(result);
74✔
163
         }
74✔
164

165
         // test softfail
166
         {
2✔
167
            Test::Result result("X509test path validation softfail");
2✔
168
            result.start_timer();
2✔
169

170
            // this certificate must not have a OCSP URL
171
            const std::string filename = "ValidAltName.pem";
2✔
172
            const std::vector<Botan::X509_Certificate> certs =
2✔
173
               load_cert_file(Test::data_file("x509/x509test/" + filename));
4✔
174
            if(certs.empty()) {
2✔
175
               throw Test_Error("Failed to read certs from " + filename);
×
176
            }
177

178
            Botan::Path_Validation_Result path_result =
2✔
179
               Botan::x509_path_validate(certs,
2✔
180
                                         restrictions,
181
                                         trusted,
182
                                         "www.tls.test",
183
                                         Botan::Usage_Type::TLS_SERVER_AUTH,
184
                                         validation_time,
185
                                         /* activate check_ocsp_online */ std::chrono::milliseconds(1000),
2✔
186
                                         {});
2✔
187

188
            if(path_result.successful_validation() && path_result.trust_root() != root) {
2✔
189
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
190
            }
191

192
            // certificate verification succeed even if no OCSP URL (softfail)
193
            result.confirm("test success", path_result.successful_validation());
4✔
194
            result.test_eq("test " + filename, path_result.result_string(), "Verified");
4✔
195
      #if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
196
            // if softfail, there is warnings
197
            result.confirm("test warnings", !path_result.no_warnings());
4✔
198
            result.test_eq("test warnings string", path_result.warnings_string(), "[0] OCSP URL not available");
4✔
199
      #endif
200
            result.end_timer();
2✔
201
            results.push_back(result);
2✔
202
         }
2✔
203

204
         return results;
2✔
205
      }
2✔
206
};
207

208
BOTAN_REGISTER_TEST("x509", "x509_path_x509test", X509test_Path_Validation_Tests);
209

210
class NIST_Path_Validation_Tests final : public Test {
×
211
   public:
212
      std::vector<Test::Result> run() override;
213

214
   private:
215
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
216
};
217

218
std::vector<Test::Result> NIST_Path_Validation_Tests::run() {
1✔
219
   std::vector<Test::Result> results;
1✔
220
   for(const auto& restrictions : restrictions_to_test(true, true)) {
3✔
221
      auto partial_res = run_with_restrictions(restrictions);
2✔
222
      results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
223
   }
3✔
224
   return results;
1✔
225
}
×
226

227
std::vector<Test::Result> NIST_Path_Validation_Tests::run_with_restrictions(
2✔
228
   const Botan::Path_Validation_Restrictions& restrictions) {
229
   if(Botan::has_filesystem_impl() == false) {
2✔
230
      return {Test::Result::Note("NIST path validation", "Skipping due to missing filesystem access")};
×
231
   }
232

233
   std::vector<Test::Result> results;
2✔
234

235
   /**
236
   * Code to run the X.509v3 processing tests described in "Conformance
237
   *  Testing of Relying Party Client Certificate Path Processing Logic",
238
   *  which is available on NIST's web site.
239
   *
240
   * https://csrc.nist.gov/projects/pki-testing/x-509-path-validation-test-suite
241
   *
242
   * Known Failures/Problems:
243
   *  - Policy extensions are not implemented, so we skip tests #34-#53.
244
   *  - Tests #75 and #76 are skipped as they make use of relatively
245
   *    obscure CRL extensions which are not supported.
246
   */
247
   const std::map<std::string, std::string> expected = read_results(Test::data_file("x509/nist/expected.txt"));
4✔
248

249
   const Botan::X509_Certificate root_cert(Test::data_file("x509/nist/root.crt"));
4✔
250
   const Botan::X509_CRL root_crl(Test::data_file("x509/nist/root.crl"));
4✔
251

252
   const auto validation_time = Botan::calendar_point(2018, 4, 1, 9, 30, 33).to_std_timepoint();
2✔
253

254
   for(const auto& [test_name, expected_result] : expected) {
154✔
255
      Test::Result result("NIST path validation");
152✔
256
      result.start_timer();
152✔
257

258
      try {
152✔
259
         const auto all_files = Test::files_in_data_dir("x509/nist/" + test_name);
152✔
260

261
         Botan::Certificate_Store_In_Memory store;
152✔
262
         store.add_certificate(root_cert);
152✔
263
         store.add_crl(root_crl);
152✔
264

265
         std::vector<Botan::X509_Certificate> end_certs = {
152✔
266
            Botan::X509_Certificate(Test::data_file("x509/nist/" + test_name + "/end.crt"))};
760✔
267

268
         for(const auto& file : all_files) {
798✔
269
            if(file.ends_with(".crt") && file != "end.crt") {
646✔
270
               end_certs.emplace_back(file);
400✔
271
            } else if(file.ends_with(".crl")) {
246✔
272
               Botan::DataSource_Stream in(file, true);
246✔
273
               const Botan::X509_CRL crl(in);
246✔
274
               store.add_crl(crl);
246✔
275
            }
246✔
276
         }
277

278
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
152✔
279
            end_certs, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
152✔
280

281
         result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
304✔
282
      } catch(std::exception& e) {
152✔
283
         result.test_failure(test_name, e.what());
×
284
      }
×
285

286
      result.end_timer();
152✔
287
      results.push_back(result);
152✔
288
   }
152✔
289

290
   return results;
2✔
291
}
154✔
292

293
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
294

295
class Extended_Path_Validation_Tests final : public Test {
×
296
   public:
297
      std::vector<Test::Result> run() override;
298
};
299

300
std::vector<Test::Result> Extended_Path_Validation_Tests::run() {
1✔
301
   if(Botan::has_filesystem_impl() == false) {
1✔
302
      return {Test::Result::Note("Extended x509 path validation", "Skipping due to missing filesystem access")};
×
303
   }
304

305
   std::vector<Test::Result> results;
1✔
306

307
   auto validation_time = Botan::calendar_point(2017, 9, 1, 9, 30, 33).to_std_timepoint();
1✔
308

309
   for(const auto& [test_name, expected_result] : read_results(Test::data_file("x509/extended/expected.txt"))) {
4✔
310
      Test::Result result("Extended X509 path validation");
3✔
311
      result.start_timer();
3✔
312

313
      const auto all_files = Test::files_in_data_dir("x509/extended/" + test_name);
3✔
314

315
      Botan::Certificate_Store_In_Memory store;
3✔
316

317
      for(const auto& file : all_files) {
13✔
318
         if(file.ends_with(".crt") && file != "end.crt") {
10✔
319
            store.add_certificate(Botan::X509_Certificate(file));
10✔
320
         }
321
      }
322

323
      const Botan::X509_Certificate end_user(Test::data_file("x509/extended/" + test_name + "/end.crt"));
9✔
324

325
      const Botan::Path_Validation_Restrictions restrictions;
6✔
326
      const Botan::Path_Validation_Result validation_result =
3✔
327
         Botan::x509_path_validate(end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
3✔
328

329
      result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
6✔
330

331
      result.end_timer();
3✔
332
      results.push_back(result);
3✔
333
   }
3✔
334

335
   return results;
1✔
336
}
1✔
337

338
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
339

340
      #if defined(BOTAN_HAS_PSS)
341

342
class PSS_Path_Validation_Tests : public Test {
×
343
   public:
344
      std::vector<Test::Result> run() override;
345
};
346

347
std::vector<Test::Result> PSS_Path_Validation_Tests::run() {
1✔
348
   if(Botan::has_filesystem_impl() == false) {
1✔
349
      return {Test::Result::Note("RSA-PSS X509 signature validation", "Skipping due to missing filesystem access")};
×
350
   }
351

352
   std::vector<Test::Result> results;
1✔
353

354
   const auto validation_times = read_results(Test::data_file("x509/pss_certs/validation_times.txt"));
2✔
355

356
   auto validation_times_iter = validation_times.begin();
1✔
357

358
   for(const auto& [test_name, expected_result] : read_results(Test::data_file("x509/pss_certs/expected.txt"))) {
119✔
359
      Test::Result result("RSA-PSS X509 signature validation");
118✔
360
      result.start_timer();
118✔
361

362
      const auto all_files = Test::files_in_data_dir("x509/pss_certs/" + test_name);
118✔
363

364
      std::optional<Botan::X509_CRL> crl;
118✔
365
      std::optional<Botan::X509_Certificate> end;
118✔
366
      std::optional<Botan::X509_Certificate> root;
118✔
367
      Botan::Certificate_Store_In_Memory store;
118✔
368
      std::optional<Botan::PKCS10_Request> csr;
118✔
369

370
      const auto validation_year = Botan::to_u32bit((validation_times_iter++)->second);
118✔
371

372
      const auto validation_time = Botan::calendar_point(validation_year, 0, 0, 0, 0, 0).to_std_timepoint();
118✔
373

374
      for(const auto& file : all_files) {
345✔
375
         if(file.find("end.crt") != std::string::npos) {
227✔
376
            end = Botan::X509_Certificate(file);
113✔
377
         } else if(file.find("root.crt") != std::string::npos) {
114✔
378
            root = Botan::X509_Certificate(file);
97✔
379
            store.add_certificate(*root);
97✔
380
         } else if(file.ends_with(".crl")) {
17✔
381
            crl = Botan::X509_CRL(file);
6✔
382
         } else if(file.ends_with(".csr")) {
11✔
383
            csr = Botan::PKCS10_Request(file);
5✔
384
         }
385
      }
386

387
      if(end && crl && root) {
118✔
388
         // CRL test
389
         const std::vector<Botan::X509_Certificate> cert_path = {*end, *root};
18✔
390
         const std::vector<std::optional<Botan::X509_CRL>> crls = {crl};
12✔
391
         auto crl_status = Botan::PKIX::check_crl(
6✔
392
            cert_path,
393
            crls,
394
            validation_time);  // alternatively we could just call crl.check_signature( root_pubkey )
6✔
395

396
         result.test_eq(test_name + " check_crl result",
12✔
397
                        Botan::Path_Validation_Result::status_string(Botan::PKIX::overall_status(crl_status)),
398
                        expected_result);
399
      } else if(end && root) {
118✔
400
         // CRT chain test
401

402
         const Botan::Path_Validation_Restrictions restrictions(false, 80);  // SHA-1 is used
182✔
403

404
         const Botan::Path_Validation_Result validation_result =
91✔
405
            Botan::x509_path_validate(*end, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
91✔
406

407
         result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
182✔
408
      } else if(end && !root) {
112✔
409
         // CRT self signed test
410
         auto pubkey = end->subject_public_key();
16✔
411
         const bool accept = expected_result == "Verified";
16✔
412
         result.test_eq(test_name + " verify signature", end->check_signature(*pubkey), accept);
32✔
413
      } else if(csr) {
21✔
414
         // PKCS#10 Request test
415
         auto pubkey = csr->subject_public_key();
5✔
416
         const bool accept = expected_result == "Verified";
5✔
417
         result.test_eq(test_name + " verify signature", csr->check_signature(*pubkey), accept);
10✔
418
      }
5✔
419

420
      result.end_timer();
118✔
421
      results.push_back(result);
118✔
422
   }
334✔
423

424
   return results;
1✔
425
}
19✔
426

427
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
428

429
      #endif
430

431
class Validate_V1Cert_Test final : public Test {
×
432
   public:
433
      std::vector<Test::Result> run() override;
434
};
435

436
std::vector<Test::Result> Validate_V1Cert_Test::run() {
1✔
437
   if(Botan::has_filesystem_impl() == false) {
1✔
438
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
439
   }
440

441
   const std::vector<Test::Result> results;
1✔
442

443
   const std::string root_crt = Test::data_file("x509/misc/v1ca/root.pem");
1✔
444
   const std::string int_crt = Test::data_file("x509/misc/v1ca/int.pem");
1✔
445
   const std::string ee_crt = Test::data_file("x509/misc/v1ca/ee.pem");
1✔
446

447
   auto validation_time = Botan::calendar_point(2019, 4, 19, 23, 0, 0).to_std_timepoint();
1✔
448

449
   const Botan::X509_Certificate root(root_crt);
1✔
450
   const Botan::X509_Certificate intermediate(int_crt);
1✔
451
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
452

453
   Botan::Certificate_Store_In_Memory trusted;
1✔
454
   trusted.add_certificate(root);
1✔
455

456
   const std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
457

458
   const Botan::Path_Validation_Restrictions restrictions;
2✔
459
   const Botan::Path_Validation_Result validation_result =
1✔
460
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
461

462
   Test::Result result("Verifying using v1 certificate");
1✔
463
   result.test_eq("Path validation result", validation_result.result_string(), "Verified");
2✔
464

465
   const Botan::Certificate_Store_In_Memory empty;
1✔
466

467
   const std::vector<Botan::X509_Certificate> new_chain = {ee_cert, intermediate, root};
4✔
468

469
   const Botan::Path_Validation_Result validation_result2 =
1✔
470
      Botan::x509_path_validate(new_chain, restrictions, empty, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
471

472
   result.test_eq("Path validation result", validation_result2.result_string(), "Cannot establish trust");
2✔
473

474
   return {result};
2✔
475
}
4✔
476

477
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
478

479
class Validate_V2Uid_in_V1_Test final : public Test {
×
480
   public:
481
      std::vector<Test::Result> run() override;
482
};
483

484
std::vector<Test::Result> Validate_V2Uid_in_V1_Test::run() {
1✔
485
   if(Botan::has_filesystem_impl() == false) {
1✔
486
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
487
   }
488

489
   const std::vector<Test::Result> results;
1✔
490

491
   const std::string root_crt = Test::data_file("x509/v2-in-v1/root.pem");
1✔
492
   const std::string int_crt = Test::data_file("x509/v2-in-v1/int.pem");
1✔
493
   const std::string ee_crt = Test::data_file("x509/v2-in-v1/leaf.pem");
1✔
494

495
   auto validation_time = Botan::calendar_point(2020, 1, 1, 1, 0, 0).to_std_timepoint();
1✔
496

497
   const Botan::X509_Certificate root(root_crt);
1✔
498
   const Botan::X509_Certificate intermediate(int_crt);
1✔
499
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
500

501
   Botan::Certificate_Store_In_Memory trusted;
1✔
502
   trusted.add_certificate(root);
1✔
503

504
   const std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
505

506
   const Botan::Path_Validation_Restrictions restrictions;
2✔
507
   const Botan::Path_Validation_Result validation_result =
1✔
508
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
509

510
   Test::Result result("Verifying v1 certificate using v2 uid fields");
1✔
511
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
512
   result.test_eq(
3✔
513
      "Path validation result", validation_result.result_string(), "Encountered v2 identifiers in v1 certificate");
2✔
514

515
   return {result};
2✔
516
}
3✔
517

518
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
519

520
class Validate_Name_Constraint_SAN_Test final : public Test {
×
521
   public:
522
      std::vector<Test::Result> run() override;
523
};
524

525
std::vector<Test::Result> Validate_Name_Constraint_SAN_Test::run() {
1✔
526
   if(Botan::has_filesystem_impl() == false) {
1✔
527
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
528
   }
529

530
   const std::vector<Test::Result> results;
1✔
531

532
   const std::string root_crt = Test::data_file("x509/name_constraint_san/root.pem");
1✔
533
   const std::string int_crt = Test::data_file("x509/name_constraint_san/int.pem");
1✔
534
   const std::string ee_crt = Test::data_file("x509/name_constraint_san/leaf.pem");
1✔
535

536
   auto validation_time = Botan::calendar_point(2020, 1, 1, 1, 0, 0).to_std_timepoint();
1✔
537

538
   const Botan::X509_Certificate root(root_crt);
1✔
539
   const Botan::X509_Certificate intermediate(int_crt);
1✔
540
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
541

542
   Botan::Certificate_Store_In_Memory trusted;
1✔
543
   trusted.add_certificate(root);
1✔
544

545
   const std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
546

547
   const Botan::Path_Validation_Restrictions restrictions;
2✔
548
   const Botan::Path_Validation_Result validation_result =
1✔
549
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
550

551
   Test::Result result("Verifying certificate with alternative SAN violating name constraint");
1✔
552
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
553
   result.test_eq(
3✔
554
      "Path validation result", validation_result.result_string(), "Certificate does not pass name constraint");
2✔
555

556
   return {result};
2✔
557
}
3✔
558

559
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
560

561
class Validate_Name_Constraint_CaseInsensitive final : public Test {
×
562
   public:
563
      std::vector<Test::Result> run() override;
564
};
565

566
std::vector<Test::Result> Validate_Name_Constraint_CaseInsensitive::run() {
1✔
567
   if(Botan::has_filesystem_impl() == false) {
1✔
568
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
569
   }
570

571
   const std::vector<Test::Result> results;
1✔
572

573
   const std::string root_crt = Test::data_file("x509/misc/name_constraint_ci/root.pem");
1✔
574
   const std::string int_crt = Test::data_file("x509/misc/name_constraint_ci/int.pem");
1✔
575
   const std::string ee_crt = Test::data_file("x509/misc/name_constraint_ci/leaf.pem");
1✔
576

577
   auto validation_time = Botan::calendar_point(2021, 5, 8, 1, 0, 0).to_std_timepoint();
1✔
578

579
   const Botan::X509_Certificate root(root_crt);
1✔
580
   const Botan::X509_Certificate intermediate(int_crt);
1✔
581
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
582

583
   Botan::Certificate_Store_In_Memory trusted;
1✔
584
   trusted.add_certificate(root);
1✔
585

586
   const std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
587

588
   const Botan::Path_Validation_Restrictions restrictions;
2✔
589
   const Botan::Path_Validation_Result validation_result =
1✔
590
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
591

592
   Test::Result result("DNS name constraints are case insensitive");
1✔
593
   result.test_eq("Path validation succeeded", validation_result.successful_validation(), true);
1✔
594

595
   return {result};
2✔
596
}
3✔
597

598
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
599

600
class Validate_Name_Constraint_NoCheckSelf final : public Test {
×
601
   public:
602
      std::vector<Test::Result> run() override;
603
};
604

605
std::vector<Test::Result> Validate_Name_Constraint_NoCheckSelf::run() {
1✔
606
   if(Botan::has_filesystem_impl() == false) {
1✔
607
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
608
   }
609

610
   const std::vector<Test::Result> results;
1✔
611

612
   const std::string root_crt = Test::data_file("x509/misc/nc_skip_self/root.pem");
1✔
613
   const std::string int_crt = Test::data_file("x509/misc/nc_skip_self/int.pem");
1✔
614
   const std::string ee_crt = Test::data_file("x509/misc/nc_skip_self/leaf.pem");
1✔
615

616
   auto validation_time = Botan::calendar_point(2021, 5, 8, 1, 0, 0).to_std_timepoint();
1✔
617

618
   const Botan::X509_Certificate root(root_crt);
1✔
619
   const Botan::X509_Certificate intermediate(int_crt);
1✔
620
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
621

622
   Botan::Certificate_Store_In_Memory trusted;
1✔
623
   trusted.add_certificate(root);
1✔
624

625
   const std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
626

627
   const Botan::Path_Validation_Restrictions restrictions;
2✔
628
   const Botan::Path_Validation_Result validation_result =
1✔
629
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
630

631
   Test::Result result("Name constraints do not apply to the certificate which includes them");
1✔
632
   result.test_eq("Path validation succeeded", validation_result.successful_validation(), true);
1✔
633

634
   return {result};
2✔
635
}
3✔
636

637
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
638

639
class Root_Cert_Time_Check_Test final : public Test {
×
640
   public:
641
      std::vector<Test::Result> run() override {
1✔
642
         if(Botan::has_filesystem_impl() == false) {
1✔
643
            return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
644
         }
645

646
         const std::string trusted_root_crt = Test::data_file("x509/misc/root_cert_time_check/root.crt");
1✔
647
         const std::string leaf_crt = Test::data_file("x509/misc/root_cert_time_check/leaf.crt");
1✔
648

649
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
650
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
651

652
         Botan::Certificate_Store_In_Memory trusted;
1✔
653
         trusted.add_certificate(trusted_root_cert);
1✔
654

655
         const std::vector<Botan::X509_Certificate> chain = {leaf_cert};
2✔
656

657
         Test::Result result("Root cert time check");
1✔
658

659
         auto assert_path_validation_result = [&](std::string_view descr,
11✔
660
                                                  bool ignore_trusted_root_time_range,
661
                                                  uint32_t year,
662
                                                  Botan::Certificate_Status_Code exp_status,
663
                                                  std::optional<Botan::Certificate_Status_Code> exp_warning =
664
                                                     std::nullopt) {
665
            const Botan::Path_Validation_Restrictions restrictions(
10✔
666
               false,
667
               110,
668
               false,
669
               std::chrono::seconds::zero(),
670
               std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
671
               ignore_trusted_root_time_range);
20✔
672

673
            const Botan::Path_Validation_Result validation_result =
10✔
674
               Botan::x509_path_validate(chain,
10✔
675
                                         restrictions,
676
                                         trusted,
10✔
677
                                         "",
678
                                         Botan::Usage_Type::UNSPECIFIED,
679
                                         Botan::calendar_point(year, 1, 1, 1, 0, 0).to_std_timepoint());
10✔
680
            const std::string descr_str = Botan::fmt(
10✔
681
               "Root cert validity range {}: {}", ignore_trusted_root_time_range ? "ignored" : "checked", descr);
15✔
682

683
            result.test_is_eq(descr_str, validation_result.result(), exp_status);
10✔
684
            const auto warnings = validation_result.warnings();
10✔
685
            BOTAN_ASSERT_NOMSG(warnings.size() == 2);
10✔
686
            result.confirm("No warning for leaf cert", warnings.at(0).empty());
20✔
687
            if(exp_warning) {
10✔
688
               result.confirm("Warning for root cert",
12✔
689
                              warnings.at(1).size() == 1 && warnings.at(1).contains(*exp_warning));
8✔
690
            } else {
691
               result.confirm("No warning for root cert", warnings.at(1).empty());
12✔
692
            }
693
         };
10✔
694
         // (Trusted) root cert validity range: 2022-2028
695
         // Leaf cert validity range: 2020-2030
696

697
         // Trusted root time range is checked
698
         assert_path_validation_result(
1✔
699
            "Root and leaf certs in validity range", false, 2025, Botan::Certificate_Status_Code::OK);
700
         assert_path_validation_result(
1✔
701
            "Root and leaf certs are expired", false, 2031, Botan::Certificate_Status_Code::CERT_HAS_EXPIRED);
702
         assert_path_validation_result(
1✔
703
            "Root and leaf certs are not yet valid", false, 2019, Botan::Certificate_Status_Code::CERT_NOT_YET_VALID);
704
         assert_path_validation_result(
1✔
705
            "Root cert is expired, leaf cert not", false, 2029, Botan::Certificate_Status_Code::CERT_HAS_EXPIRED);
706
         assert_path_validation_result("Root cert is not yet valid, leaf cert is",
1✔
707
                                       false,
708
                                       2021,
709
                                       Botan::Certificate_Status_Code::CERT_NOT_YET_VALID);
710

711
         // Trusted root time range is ignored
712
         assert_path_validation_result(
1✔
713
            "Root and leaf certs in validity range", true, 2025, Botan::Certificate_Status_Code::OK);
714
         assert_path_validation_result("Root and leaf certs are expired",
2✔
715
                                       true,
716
                                       2031,
717
                                       Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
718
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_HAS_EXPIRED);
1✔
719
         assert_path_validation_result("Root and leaf certs are not yet valid",
2✔
720
                                       true,
721
                                       2019,
722
                                       Botan::Certificate_Status_Code::CERT_NOT_YET_VALID,
723
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_NOT_YET_VALID);
1✔
724
         assert_path_validation_result("Root cert is expired, leaf cert not",
2✔
725
                                       true,
726
                                       2029,
727
                                       Botan::Certificate_Status_Code::OK,
728
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_HAS_EXPIRED);
1✔
729
         assert_path_validation_result("Root cert is not yet valid, leaf cert is",
2✔
730
                                       true,
731
                                       2021,
732
                                       Botan::Certificate_Status_Code::OK,
733
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_NOT_YET_VALID);
1✔
734

735
         return {result};
2✔
736
      }
3✔
737
};
738

739
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
740

741
class Non_Self_Signed_Trust_Anchors_Test final : public Test {
×
742
   private:
743
      using Cert_Path = std::vector<Botan::X509_Certificate>;
744

745
      std::vector<Botan::X509_Certificate> get_valid_cert_chain() {
4✔
746
         // Test certs generated by https://github.com/yymax/x509test
747
         const std::string valid_chain_pem = "x509/x509test/ValidChained.pem";
4✔
748
         auto certs = load_cert_file(Test::data_file(valid_chain_pem));
4✔
749
         if(certs.size() != 5) {
4✔
750
            throw Test_Error(Botan::fmt("Failed to read all certs from {}", valid_chain_pem));
×
751
         }
752
         return certs;
4✔
753
      }
4✔
754

755
      /// Time point fits for all certificates used in this class
756
      std::chrono::system_clock::time_point get_validation_time() {
8✔
757
         return Botan::calendar_point(2016, 10, 21, 4, 20, 0).to_std_timepoint();
16✔
758
      }
759

760
      Botan::X509_Certificate get_self_signed_ee_cert() {
1✔
761
         const std::string valid_chain_pem = "x509/misc/self-signed-end-entity.pem";
1✔
762
         return Botan::X509_Certificate(Test::data_file(valid_chain_pem));
2✔
763
      }
1✔
764

765
      std::vector<Test::Result> path_validate_test() {
1✔
766
         std::vector<Test::Result> results;
1✔
767

768
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
769
         const auto certs = get_valid_cert_chain();
1✔
770
         const auto validation_time = get_validation_time();
1✔
771

772
         const std::vector<std::tuple<std::string_view, Cert_Path, Botan::X509_Certificate>>
1✔
773
            info_w_end_certs_w_trust_anchor{
774
               {"length 1", Cert_Path{certs.at(0)}, certs.at(0)},
4✔
775
               {"length 2", Cert_Path{certs.at(0), certs.at(1)}, certs.at(1)},
5✔
776
               {"length 3 (store not in chain)", Cert_Path{certs.at(0), certs.at(1)}, certs.at(2)},
5✔
777
               {"length 4 (shuffled)", Cert_Path{certs.at(0), certs.at(3), certs.at(2), certs.at(1)}, certs.at(3)},
7✔
778
               {"full", Cert_Path{certs.at(0), certs.at(1), certs.at(2), certs.at(3), certs.at(4)}, certs.at(4)},
8✔
779
            };
6✔
780

781
         for(const auto& [info, end_certs, trust_anchor] : info_w_end_certs_w_trust_anchor) {
6✔
782
            Test::Result result(
5✔
783
               Botan::fmt("Non-self-signed trust anchor without require_self_signed_trust_anchors ({})", info));
5✔
784

785
            const Botan::Certificate_Store_In_Memory trusted(trust_anchor);
5✔
786

787
            auto path_result = Botan::x509_path_validate(
5✔
788
               end_certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
5✔
789

790
            if(path_result.successful_validation() && path_result.trust_root() != trust_anchor) {
5✔
791
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
792
            }
793

794
            result.test_eq(
15✔
795
               "path validation failed", path_result.result_string(), to_string(Botan::Certificate_Status_Code::OK));
10✔
796

797
            results.push_back(result);
5✔
798
         }
5✔
799
         return results;
1✔
800
      }
7✔
801

802
      std::vector<Test::Result> build_path_test() {
1✔
803
         std::vector<Test::Result> results;
1✔
804

805
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
806
         const auto certs = get_valid_cert_chain();
1✔
807

808
         // Helper to create a cert path {certs[0],...,certs[last_cert]}
809
         const auto path_to = [&](size_t last_cert) -> Cert_Path {
20✔
810
            BOTAN_ASSERT_NOMSG(last_cert <= certs.size());
19✔
811
            return {certs.begin(), certs.begin() + last_cert + 1};
19✔
812
         };
1✔
813

814
         // Helper to create a cert store of all certificates in certs given by their indices
815
         const auto store_of = [&](auto... cert_indices) -> Botan::Certificate_Store_In_Memory {
6✔
816
            Botan::Certificate_Store_In_Memory cert_store;
5✔
817
            (cert_store.add_certificate(certs.at(cert_indices)), ...);
5✔
818
            return cert_store;
5✔
819
         };
1✔
820

821
         const std::vector<std::tuple<std::string, Botan::Certificate_Store_In_Memory, std::vector<Cert_Path>>>
1✔
822
            test_names_with_stores_with_expected_paths{
823
               {"root in store", store_of(4), std::vector<Cert_Path>{path_to(4)}},
4✔
824
               {"intermediate in store", store_of(2), std::vector<Cert_Path>{path_to(2)}},
3✔
825
               {"leaf in store", store_of(0), std::vector<Cert_Path>{path_to(0)}},
3✔
826
               {"leaf, intermediate, and root in store",
827
                store_of(0, 1, 4),
1✔
828
                std::vector<Cert_Path>{path_to(0), path_to(1), path_to(4)}},
5✔
829
               {"all in store",
830
                store_of(0, 1, 2, 3, 4),
1✔
831
                std::vector<Cert_Path>{path_to(0), path_to(1), path_to(2), path_to(3), path_to(4)}},
7✔
832
            };
6✔
833

834
         for(auto [test_name, cert_store, expected_paths] : test_names_with_stores_with_expected_paths) {
6✔
835
            Test::Result result(Botan::fmt("Build certificate paths ({})", test_name));
5✔
836

837
            std::vector<Cert_Path> cert_paths;
5✔
838
            const auto build_all_res =
5✔
839
               Botan::PKIX::build_all_certificate_paths(cert_paths, {&cert_store}, certs.at(0), certs);
5✔
840
            result.test_is_eq("build_all_certificate_paths result",
5✔
841
                              to_string(build_all_res),
5✔
842
                              to_string(Botan::Certificate_Status_Code::OK));
5✔
843
            result.test_is_eq("build_all_certificate_paths paths", cert_paths, expected_paths);
5✔
844

845
            Cert_Path cert_path;
5✔
846
            const auto build_path_res =
5✔
847
               Botan::PKIX::build_certificate_path(cert_path, {&cert_store}, certs.at(0), certs);
5✔
848
            result.test_is_eq("build_certificate_path result",
5✔
849
                              to_string(build_path_res),
5✔
850
                              to_string(Botan::Certificate_Status_Code::OK));
5✔
851

852
            if(std::ranges::find(cert_paths, path_to(4)) != cert_paths.end()) {
10✔
853
               result.test_is_eq("build_certificate_path (with self-signed anchor)", cert_path, path_to(4));
6✔
854
            } else {
855
               result.test_is_eq(
4✔
856
                  "build_certificate_path (without self-signed anchor)", cert_path, expected_paths.at(0));
2✔
857
            }
858
            results.push_back(result);
5✔
859
         }
5✔
860

861
         return results;
1✔
862
      }
7✔
863

864
      std::vector<Test::Result> forbidden_self_signed_trust_anchors_test() {
1✔
865
         const auto restrictions = get_default_restrictions(false, false);  // non-self-signed anchors are forbidden
1✔
866
         const auto certs = get_valid_cert_chain();
1✔
867
         const auto validation_time = get_validation_time();
1✔
868

869
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
870

871
         Test::Result result("Build certificate paths with only non-self-signed trust anchors (forbidden)");
1✔
872

873
         const auto path_result = Botan::x509_path_validate(
1✔
874
            certs, restrictions, {&cert_store}, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
2✔
875

876
         result.test_eq("unexpected path validation result",
3✔
877
                        path_result.result_string(),
2✔
878
                        to_string(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST));
879

880
         const auto check_chain_result = Botan::PKIX::check_chain(
5✔
881
            {certs.at(0), certs.at(1), certs.at(2)}, validation_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions);
4✔
882

883
         result.test_ne("unexpected check_chain result",
3✔
884
                        Botan::Path_Validation_Result(check_chain_result, {}).result_string(),
2✔
885
                        to_string(Botan::Certificate_Status_Code::OK));
886

887
         return {result};
3✔
888
      }
3✔
889

890
      Test::Result stand_alone_root_test(std::string test_name,
6✔
891
                                         const Botan::Path_Validation_Restrictions& restrictions,
892
                                         const Botan::X509_Certificate& standalone_cert,
893
                                         Botan::Certificate_Status_Code expected_result) {
894
         Test::Result result(std::move(test_name));
6✔
895

896
         const auto validation_time = get_validation_time();
6✔
897
         Botan::Certificate_Store_In_Memory cert_store(standalone_cert);
6✔
898

899
         const auto path_result = Botan::x509_path_validate(
6✔
900
            standalone_cert, restrictions, {&cert_store}, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
12✔
901

902
         result.test_eq(
18✔
903
            "unexpected x509_path_validate result", path_result.result_string(), to_string(expected_result));
12✔
904

905
         return result;
6✔
906
      }
6✔
907

908
      std::vector<Test::Result> stand_alone_root_tests() {
1✔
909
         const auto self_signed_trust_anchor_forbidden = get_default_restrictions(false, false);
1✔
910
         const auto self_signed_trust_anchor_allowed = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
911

912
         const auto cert_chain = get_valid_cert_chain();
1✔
913
         const auto& self_signed_root = cert_chain.at(4);
1✔
914
         const auto& ica = cert_chain.at(3);
1✔
915
         const auto& self_signed_ee = get_self_signed_ee_cert();
1✔
916

917
         return {
1✔
918
            stand_alone_root_test("Standalone self-signed root (non-self-signed trust anchors forbidden)",
1✔
919
                                  self_signed_trust_anchor_forbidden,
920
                                  self_signed_root,
921
                                  Botan::Certificate_Status_Code::OK),
922
            stand_alone_root_test("Standalone self-signed root (non-self-signed trust anchors allowed)",
1✔
923
                                  self_signed_trust_anchor_allowed,
924
                                  self_signed_root,
925
                                  Botan::Certificate_Status_Code::OK),
926

927
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors forbidden)",
1✔
928
                                  self_signed_trust_anchor_forbidden,
929
                                  self_signed_ee,
930
                                  Botan::Certificate_Status_Code::OK),
931
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors allowed)",
1✔
932
                                  self_signed_trust_anchor_allowed,
933
                                  self_signed_ee,
934
                                  Botan::Certificate_Status_Code::OK),
935

936
            stand_alone_root_test("Standalone intermediate certificate (non-self-signed trust anchors forbidden)",
1✔
937
                                  self_signed_trust_anchor_forbidden,
938
                                  ica,
939
                                  Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST),
940
            stand_alone_root_test("Standalone intermediate certificate (non-self-signed trust anchors allowed)",
1✔
941
                                  self_signed_trust_anchor_allowed,
942
                                  ica,
943
                                  Botan::Certificate_Status_Code::OK),
944
         };
8✔
945
      }
7✔
946

947
   public:
948
      std::vector<Test::Result> run() override {
1✔
949
         std::vector<Test::Result> results;
1✔
950

951
         auto res = path_validate_test();
1✔
952
         results.insert(results.end(), res.begin(), res.end());
1✔
953

954
         res = build_path_test();
1✔
955
         results.insert(results.end(), res.begin(), res.end());
1✔
956

957
         res = forbidden_self_signed_trust_anchors_test();
1✔
958
         results.insert(results.end(), res.begin(), res.end());
1✔
959

960
         res = stand_alone_root_tests();
1✔
961
         results.insert(results.end(), res.begin(), res.end());
1✔
962

963
         return results;
1✔
964
      }
1✔
965
};
966

967
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
968

969
class BSI_Path_Validation_Tests final : public Test
×
970

971
{
972
   public:
973
      std::vector<Test::Result> run() override;
974

975
   private:
976
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
977
};
978

979
std::vector<Test::Result> BSI_Path_Validation_Tests::run() {
1✔
980
   std::vector<Test::Result> results;
1✔
981
   for(const auto& restrictions : restrictions_to_test(false, false)) {
3✔
982
      auto partial_res = run_with_restrictions(restrictions);
2✔
983
      results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
984
   }
3✔
985
   return results;
1✔
986
}
×
987

988
std::vector<Test::Result> BSI_Path_Validation_Tests::run_with_restrictions(
2✔
989
   const Botan::Path_Validation_Restrictions& restriction_template) {
990
   if(Botan::has_filesystem_impl() == false) {
2✔
991
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
992
   }
993

994
   std::vector<Test::Result> results;
2✔
995

996
   for(const auto& [test_name, expected_result] : read_results(Test::data_file("x509/bsi/expected.txt"), '$')) {
110✔
997
      Test::Result result("BSI path validation");
108✔
998
      result.start_timer();
108✔
999

1000
      const auto all_files = Test::files_in_data_dir("x509/bsi/" + test_name);
108✔
1001

1002
      Botan::Certificate_Store_In_Memory trusted;
108✔
1003
      std::vector<Botan::X509_Certificate> certs;
108✔
1004

1005
      #if defined(BOTAN_HAS_MD5)
1006
      const bool has_md5 = true;
108✔
1007
      #else
1008
      const bool has_md5 = false;
1009
      #endif
1010

1011
      auto validation_time = Botan::calendar_point(2017, 8, 19, 12, 0, 0).to_std_timepoint();
108✔
1012

1013
      // By convention: if CRL is a substring if the test name,
1014
      // we need to check the CRLs
1015
      bool use_crl = false;
108✔
1016
      if(test_name.find("CRL") != std::string::npos) {
108✔
1017
         use_crl = true;
32✔
1018
      }
1019

1020
      try {
108✔
1021
         for(const auto& file : all_files) {
890✔
1022
            // found a trust anchor
1023
            if(file.find("TA") != std::string::npos) {
790✔
1024
               trusted.add_certificate(Botan::X509_Certificate(file));
100✔
1025
            }
1026
            // found the target certificate. It needs to be at the front of certs
1027
            else if(file.find("TC") != std::string::npos) {
690✔
1028
               certs.insert(certs.begin(), Botan::X509_Certificate(file));
108✔
1029
            }
1030
            // found a certificate that might be part of a valid certificate chain to the trust anchor
1031
            else if(file.find(".crt") != std::string::npos) {
582✔
1032
               certs.push_back(Botan::X509_Certificate(file));
224✔
1033
            } else if(file.find(".crl") != std::string::npos) {
470✔
1034
               trusted.add_crl(Botan::X509_CRL(file));
56✔
1035
            }
1036
         }
1037

1038
         const Botan::Path_Validation_Restrictions restrictions(
100✔
1039
            use_crl,
1040
            restriction_template.minimum_key_strength(),
1041
            use_crl,
1042
            restriction_template.max_ocsp_age(),
1043
            std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
1044
            restriction_template.ignore_trusted_root_time_range(),
100✔
1045
            restriction_template.require_self_signed_trust_anchors());
200✔
1046

1047
         /*
1048
          * Following the test document, the test are executed 16 times with
1049
          * randomly chosen order of the available certificates. However, the target
1050
          * certificate needs to stay in front.
1051
          * For certain test, the order in which the certificates are given to
1052
          * the validation function may be relevant, i.e. if issuer DNs are
1053
          * ambiguous.
1054
          */
1055
         class random_bit_generator {
100✔
1056
            public:
1057
               using result_type = size_t;
1058

1059
               static constexpr result_type min() { return 0; }
1060

1061
               static constexpr result_type max() { return std::numeric_limits<size_t>::max(); }
1062

1063
               result_type operator()() {
160✔
1064
                  size_t s = 0;
160✔
1065
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
160✔
1066
                  return s;
160✔
1067
               }
1068

1069
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
1070

1071
            private:
1072
               Botan::RandomNumberGenerator& m_rng;
1073
         } rbg(this->rng());
100✔
1074

1075
         for(size_t r = 0; r < 16; r++) {
1,700✔
1076
            std::shuffle(++(certs.begin()), certs.end(), rbg);
1,600✔
1077

1078
            const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,600✔
1079
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,600✔
1080

1081
            // We expect to be warned
1082
            if(expected_result.starts_with("Warning: ")) {
1,600✔
1083
               const std::string stripped = expected_result.substr(std::string("Warning: ").size());
64✔
1084
               bool found_warning = false;
64✔
1085
               for(const auto& warning_set : validation_result.warnings()) {
256✔
1086
                  for(const auto& warning : warning_set) {
256✔
1087
                     const std::string warning_str(Botan::to_string(warning));
64✔
1088
                     if(stripped == warning_str) {
64✔
1089
                        result.test_eq(test_name + " path validation result", warning_str, stripped);
64✔
1090
                        found_warning = true;
64✔
1091
                     }
1092
                  }
64✔
1093
               }
64✔
1094
               if(!found_warning) {
64✔
1095
                  result.test_failure(test_name, "Did not receive the expected warning: " + stripped);
×
1096
               }
1097
            } else {
64✔
1098
               if(expected_result == "Hash function used is considered too weak for security" && has_md5 == false) {
1,536✔
1099
                  result.test_eq(test_name + " path validation result",
1100
                                 validation_result.result_string(),
1101
                                 "Certificate signed with unknown/unavailable algorithm");
1102
               } else {
1103
                  result.test_eq(
1,536✔
1104
                     test_name + " path validation result", validation_result.result_string(), expected_result);
3,072✔
1105
               }
1106
            }
1107
         }
1,600✔
1108
      }
100✔
1109

1110
      /* Some certificates are rejected when executing the X509_Certificate constructor
1111
       * by throwing a Decoding_Error exception.
1112
       */
1113
      catch(const Botan::Exception& e) {
8✔
1114
         if(e.error_type() == Botan::ErrorType::DecodingFailure) {
8✔
1115
            result.test_eq(test_name + " path validation result", e.what(), expected_result);
16✔
1116
         } else {
1117
            result.test_failure(test_name, e.what());
×
1118
         }
1119
      }
8✔
1120

1121
      result.end_timer();
108✔
1122
      results.push_back(result);
108✔
1123
   }
108✔
1124

1125
   return results;
2✔
1126
}
2✔
1127

1128
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1129

1130
class Path_Validation_With_OCSP_Tests final : public Test {
×
1131
   public:
1132
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
1133
         return Botan::X509_Certificate(Test::data_file(path));
48✔
1134
      }
1135

1136
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
1137
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
1138
      }
1139

1140
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
1141
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
1142
         Botan::Certificate_Store_In_Memory trusted;
1✔
1143

1144
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1145

1146
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1147
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1148
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1149
         trusted.add_certificate(trust_root);
1✔
1150

1151
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1152

1153
         std::optional<const Botan::OCSP::Response> ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
3✔
1154

1155
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1156
                               const Botan::Certificate_Status_Code expected) {
1157
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
1158
                                                               restrictions,
1159
                                                               trusted,
4✔
1160
                                                               "",
1161
                                                               Botan::Usage_Type::UNSPECIFIED,
1162
                                                               valid_time,
1163
                                                               std::chrono::milliseconds(0),
4✔
1164
                                                               {ocsp});
8✔
1165

1166
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1167
                                     Botan::to_string(path_result.result()) + "'",
8✔
1168
                                  path_result.result() == expected);
8✔
1169
         };
8✔
1170

1171
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1172
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1173
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1174
                    Botan::Certificate_Status_Code::OK);
1175
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1176
                    Botan::Certificate_Status_Code::OK);
1177
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1178
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1179

1180
         return result;
2✔
1181
      }
2✔
1182

1183
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1184
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1185
         Botan::Certificate_Store_In_Memory trusted;
1✔
1186

1187
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false, std::chrono::minutes(59));
1✔
1188

1189
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1190
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1191
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1192
         trusted.add_certificate(trust_root);
1✔
1193

1194
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1195

1196
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1197

1198
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1199
                               const Botan::Certificate_Status_Code expected) {
1200
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
1201
                                                               restrictions,
1202
                                                               trusted,
4✔
1203
                                                               "",
1204
                                                               Botan::Usage_Type::UNSPECIFIED,
1205
                                                               valid_time,
1206
                                                               std::chrono::milliseconds(0),
4✔
1207
                                                               {ocsp});
8✔
1208

1209
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1210
                                     Botan::to_string(path_result.result()) + "'",
8✔
1211
                                  path_result.result() == expected);
8✔
1212
         };
12✔
1213

1214
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1215
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1216
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1217
                    Botan::Certificate_Status_Code::OK);
1218
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1219
                    Botan::Certificate_Status_Code::OK);
1220
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1221
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1222

1223
         return result;
2✔
1224
      }
2✔
1225

1226
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
1227
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
1228
         Botan::Certificate_Store_In_Memory trusted;
1✔
1229

1230
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1231

1232
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1233
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1234
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1235

1236
         trusted.add_certificate(trust_root);
1✔
1237

1238
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1239

1240
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1241

1242
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1243
                               const Botan::Certificate_Status_Code expected) {
1244
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1245
                                                               restrictions,
1246
                                                               trusted,
3✔
1247
                                                               "",
1248
                                                               Botan::Usage_Type::UNSPECIFIED,
1249
                                                               valid_time,
1250
                                                               std::chrono::milliseconds(0),
3✔
1251
                                                               {ocsp});
6✔
1252

1253
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1254
                                     Botan::to_string(path_result.result()) + "'",
6✔
1255
                                  path_result.result() == expected);
6✔
1256
         };
9✔
1257

1258
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1259
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1260
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1261
                    Botan::Certificate_Status_Code::OK);
1262
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
1263

1264
         return result;
2✔
1265
      }
2✔
1266

1267
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
1268
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
1269
         Botan::Certificate_Store_In_Memory trusted;
1✔
1270

1271
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false, std::chrono::minutes(59));
1✔
1272

1273
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1274
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1275
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1276

1277
         trusted.add_certificate(trust_root);
1✔
1278

1279
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1280

1281
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1282

1283
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1284
                               const Botan::Certificate_Status_Code expected) {
1285
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1286
                                                               restrictions,
1287
                                                               trusted,
3✔
1288
                                                               "",
1289
                                                               Botan::Usage_Type::UNSPECIFIED,
1290
                                                               valid_time,
1291
                                                               std::chrono::milliseconds(0),
3✔
1292
                                                               {ocsp});
6✔
1293

1294
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1295
                                     Botan::to_string(path_result.result()) + "'",
6✔
1296
                                  path_result.result() == expected);
6✔
1297
         };
9✔
1298

1299
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1300
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1301
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1302
                    Botan::Certificate_Status_Code::OK);
1303
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1304
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1305

1306
         return result;
2✔
1307
      }
2✔
1308

1309
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1310
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1311
         Botan::Certificate_Store_In_Memory trusted;
1✔
1312

1313
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1314
                                                                 110,    // minimum key strength
1315
                                                                 true);  // OCSP for all intermediates
2✔
1316

1317
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1318
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1319
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1320

1321
         // These OCSP responses are signed by an authorized OCSP responder
1322
         // certificate issued by `ca` and `trust_root` respectively. Note that
1323
         // the responder certificates contain the "OCSP No Check" extension,
1324
         // meaning that they themselves do not need a revocation check via OCSP.
1325
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/bdr-ocsp-resp.der");
1✔
1326
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/bdr-int-ocsp-resp.der");
1✔
1327

1328
         trusted.add_certificate(trust_root);
1✔
1329
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1330

1331
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1332
                               const Botan::Certificate_Status_Code expected) {
1333
            const auto path_result = Botan::x509_path_validate(cert_path,
15✔
1334
                                                               restrictions,
1335
                                                               trusted,
3✔
1336
                                                               "",
1337
                                                               Botan::Usage_Type::UNSPECIFIED,
1338
                                                               valid_time,
1339
                                                               std::chrono::milliseconds(0),
3✔
1340
                                                               {ocsp_ee, ocsp_ca});
9✔
1341

1342
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1343
                                     Botan::to_string(path_result.result()) + "'",
6✔
1344
                                  path_result.result() == expected);
6✔
1345
         };
12✔
1346

1347
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1348
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1349
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1350
                    Botan::Certificate_Status_Code::OK);
1351
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1352
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1353

1354
         return result;
1✔
1355
      }
4✔
1356

1357
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1358
         Test::Result result(
1✔
1359
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1360
         Botan::Certificate_Store_In_Memory trusted;
1✔
1361

1362
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1363
                                                                 110,     // minimum key strength
1364
                                                                 false);  // OCSP for all intermediates
2✔
1365

1366
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1367
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1368
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1369
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1370

1371
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1372
         auto ocsp_ee_delegate_malformed =
1✔
1373
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1374

1375
         trusted.add_certificate(trust_root);
1✔
1376
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1377

1378
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1379
                               const Botan::OCSP::Response& ocsp_ee,
1380
                               const Botan::Certificate_Status_Code expected,
1381
                               const std::optional<Botan::Certificate_Status_Code> also_expected = std::nullopt) {
1382
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1383
                                                               restrictions,
1384
                                                               trusted,
3✔
1385
                                                               "",
1386
                                                               Botan::Usage_Type::UNSPECIFIED,
1387
                                                               valid_time,
1388
                                                               std::chrono::milliseconds(0),
3✔
1389
                                                               {ocsp_ee});
6✔
1390

1391
            result.test_is_eq("should result in expected validation status code",
3✔
1392
                              static_cast<uint32_t>(path_result.result()),
3✔
1393
                              static_cast<uint32_t>(expected));
3✔
1394
            if(also_expected) {
3✔
1395
               result.confirm("Secondary error is also present",
4✔
1396
                              flatten(path_result.all_statuses()).contains(also_expected.value()));
6✔
1397
            }
1398
         };
6✔
1399

1400
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1401
                    ocsp_ee_delegate,
1402
                    Botan::Certificate_Status_Code::VERIFIED);
1403
         check_path(Botan::calendar_point(2022, 10, 8, 23, 30, 0).to_std_timepoint(),
1✔
1404
                    ocsp_ee_delegate,
1405
                    Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
1406
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1407
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1408
                    ocsp_ee_delegate_malformed,
1409
                    Botan::Certificate_Status_Code::OCSP_RESPONSE_MISSING_KEYUSAGE,
1410
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1411

1412
         return result;
1✔
1413
      }
2✔
1414

1415
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1416
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1417
         Botan::Certificate_Store_In_Memory trusted;
1✔
1418

1419
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1420
                                                                 110,     // minimum key strength
1421
                                                                 false);  // OCSP for all intermediates
2✔
1422

1423
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1424
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1425
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1426
         trusted.add_certificate(trust_root);
1✔
1427

1428
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1429

1430
         auto check_path = [&](const std::string& forged_ocsp,
3✔
1431
                               const Botan::Certificate_Status_Code expected,
1432
                               const Botan::Certificate_Status_Code also_expected) {
1433
            auto ocsp = load_test_OCSP_resp(forged_ocsp);
2✔
1434
            const auto path_result =
2✔
1435
               Botan::x509_path_validate(cert_path,
6✔
1436
                                         restrictions,
1437
                                         trusted,
2✔
1438
                                         "",
1439
                                         Botan::Usage_Type::UNSPECIFIED,
1440
                                         Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
4✔
1441
                                         std::chrono::milliseconds(0),
2✔
1442
                                         {ocsp});
4✔
1443

1444
            result.test_is_eq(
2✔
1445
               "Path validation with forged OCSP response should fail with", path_result.result(), expected);
2✔
1446
            result.confirm("Secondary error is also present",
4✔
1447
                           flatten(path_result.all_statuses()).contains(also_expected));
4✔
1448
            result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
6✔
1449
         };
8✔
1450

1451
         // In both cases the path validation should detect the forged OCSP
1452
         // response and generate an appropriate error. By no means it should
1453
         // follow the unauthentic OCSP response.
1454
         check_path("x509/ocsp/randombit_ocsp_forged_valid.der",
1✔
1455
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1456
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1457
         check_path("x509/ocsp/randombit_ocsp_forged_revoked.der",
1✔
1458
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1459
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1460

1461
         return result;
1✔
1462
      }
2✔
1463

1464
      static Test::Result validate_with_ocsp_self_signed_by_intermediate_cert() {
1✔
1465
         Test::Result result(
1✔
1466
            "path check with ocsp response for intermediate that is (maliciously) self-signed by the intermediate");
1✔
1467
         Botan::Certificate_Store_In_Memory trusted;
1✔
1468

1469
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1470
                                                                 110,    // minimum key strength
1471
                                                                 true);  // OCSP for all intermediates
2✔
1472

1473
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1474
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1475
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1476
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1477

1478
         // this OCSP response for EE is valid (signed by intermediate cert)
1479
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee.der");
1✔
1480

1481
         // this OCSP response for Intermediate is malicious (signed by intermediate itself)
1482
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_int_self_signed.der");
1✔
1483

1484
         trusted.add_certificate(trust_root);
1✔
1485
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
1486

1487
         const auto path_result =
1✔
1488
            Botan::x509_path_validate(cert_path,
5✔
1489
                                      restrictions,
1490
                                      trusted,
1491
                                      "",
1492
                                      Botan::Usage_Type::UNSPECIFIED,
1493
                                      Botan::calendar_point(2022, 9, 22, 22, 30, 0).to_std_timepoint(),
2✔
1494
                                      std::chrono::milliseconds(0),
1✔
1495
                                      {ocsp_ee, ocsp_ca});
3✔
1496
         result.confirm("should reject intermediate OCSP response",
2✔
1497
                        path_result.result() == Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_FOUND);
1✔
1498
         result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
3✔
1499

1500
         return result;
1✔
1501
      }
7✔
1502

1503
      std::vector<Test::Result> run() override {
1✔
1504
         return {validate_with_ocsp_with_next_update_without_max_age(),
1✔
1505
                 validate_with_ocsp_with_next_update_with_max_age(),
1506
                 validate_with_ocsp_without_next_update_without_max_age(),
1507
                 validate_with_ocsp_without_next_update_with_max_age(),
1508
                 validate_with_ocsp_with_authorized_responder(),
1509
                 validate_with_ocsp_with_authorized_responder_without_keyusage(),
1510
                 validate_with_forged_ocsp_using_self_signed_cert(),
1511
                 validate_with_ocsp_self_signed_by_intermediate_cert()};
9✔
1512
      }
1✔
1513
};
1514

1515
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1516

1517
   #endif
1518

1519
   #if defined(BOTAN_HAS_ECDSA)
1520

1521
class CVE_2020_0601_Tests final : public Test {
×
1522
   public:
1523
      std::vector<Test::Result> run() override {
1✔
1524
         Test::Result result("CVE-2020-0601");
1✔
1525

1526
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1527
            result.test_note("Skipping as application specific groups are not supported");
×
1528
            return {result};
×
1529
         }
1530

1531
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1532
            result.test_note("Skipping as secp384r1 is not supported");
×
1533
            return {result};
×
1534
         }
1535

1536
         const auto& secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
1537
         const Botan::OID curveball_oid("1.3.6.1.4.1.25258.4.2020.0601");
1✔
1538
         const Botan::EC_Group curveball(
1✔
1539
            curveball_oid,
1540
            secp384r1.get_p(),
1541
            secp384r1.get_a(),
1542
            secp384r1.get_b(),
1543
            BigInt(
2✔
1544
               "0xC711162A761D568EBEB96265D4C3CEB4F0C330EC8F6DD76E39BCC849ABABB8E34378D581065DEFC77D9FCED6B39075DE"),
1545
            BigInt(
2✔
1546
               "0x0CB090DE23BAC8D13E67E019A91B86311E5F342DEE17FD15FB7E278A32A1EAC98FC97E18CB2F3B2C487A7DA6F40107AC"),
1547
            secp384r1.get_order());
2✔
1548

1549
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1550
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1551
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1552

1553
         Botan::Certificate_Store_In_Memory trusted;
1✔
1554
         trusted.add_certificate(ca_crt);
1✔
1555

1556
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1557

1558
         const auto valid_time = Botan::calendar_point(2020, 1, 20, 0, 0, 0).to_std_timepoint();
1✔
1559

1560
         const auto path_result1 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt, fake_ca_crt},
5✔
1561
                                                             restrictions,
1562
                                                             trusted,
1563
                                                             "",
1564
                                                             Botan::Usage_Type::UNSPECIFIED,
1565
                                                             valid_time,
1566
                                                             std::chrono::milliseconds(0),
1✔
1567
                                                             {});
2✔
1568

1569
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1570

1571
         result.confirm("Expected status",
2✔
1572
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1573

1574
         const auto path_result2 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1575
                                                             restrictions,
1576
                                                             trusted,
1577
                                                             "",
1578
                                                             Botan::Usage_Type::UNSPECIFIED,
1579
                                                             valid_time,
1580
                                                             std::chrono::milliseconds(0),
1✔
1581
                                                             {});
2✔
1582

1583
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1584

1585
         result.confirm("Expected status",
2✔
1586
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1587

1588
         // Verify the signature from the bad CA is actually correct
1589
         Botan::Certificate_Store_In_Memory frusted;
1✔
1590
         frusted.add_certificate(fake_ca_crt);
1✔
1591

1592
         const auto path_result3 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1593
                                                             restrictions,
1594
                                                             frusted,
1595
                                                             "",
1596
                                                             Botan::Usage_Type::UNSPECIFIED,
1597
                                                             valid_time,
1598
                                                             std::chrono::milliseconds(0),
1✔
1599
                                                             {});
2✔
1600

1601
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1602

1603
         return {result};
2✔
1604
      }
5✔
1605
};
1606

1607
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1608

1609
class Path_Validation_With_Immortal_CRL final : public Test {
×
1610
   public:
1611
      std::vector<Test::Result> run() override {
1✔
1612
         // RFC 5280 defines the nextUpdate field as "optional" (in line with
1613
         // the original X.509 standard), but then requires all conforming CAs
1614
         // to always define it. For best compatibility we must deal with both.
1615
         Test::Result result("Using a CRL without a nextUpdate field");
1✔
1616

1617
         if(Botan::has_filesystem_impl() == false) {
1✔
1618
            result.test_note("Skipping due to missing filesystem access");
×
1619
            return {result};
×
1620
         }
1621

1622
         const Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1623
         const Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1624
         const Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1625

1626
         // Check that a CRL without nextUpdate is parsable
1627
         auto crl = Botan::X509_CRL(Test::data_file("x509/misc/crl_without_nextupdate/valid_forever.crl"));
2✔
1628
         result.confirm("this update is set", crl.this_update().time_is_set());
2✔
1629
         result.confirm("next update is not set", !crl.next_update().time_is_set());
2✔
1630
         result.confirm("CRL is not empty", !crl.get_revoked().empty());
2✔
1631

1632
         // Ensure that we support the used sig algo, otherwish stop here
1633
         if(!Botan::EC_Group::supports_named_group("brainpool512r1")) {
1✔
1634
            result.test_note("Cannot test path validation because signature algorithm is not support in this build");
×
1635
            return {result};
×
1636
         }
1637

1638
         Botan::Certificate_Store_In_Memory trusted;
1✔
1639
         trusted.add_certificate(root);
1✔
1640
         trusted.add_crl(crl);
1✔
1641

1642
         // Just before the CA and subject certificates expire
1643
         // (validity from 01 March 2025 to 24 February 2026)
1644
         auto valid_time = Botan::calendar_point(2026, 2, 23, 0, 0, 0).to_std_timepoint();
1✔
1645

1646
         const Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1647

1648
         // Validate a certificate that is not listed in the CRL
1649
         const auto valid = Botan::x509_path_validate(
1✔
1650
            valid_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1651
         if(!result.confirm("Valid certificate", valid.successful_validation())) {
2✔
1652
            result.test_note(valid.result_string());
×
1653
         }
1654

1655
         // Ensure that a certificate listed in the CRL is recognized as revoked
1656
         const auto revoked = Botan::x509_path_validate(
1✔
1657
            revoked_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1658
         if(!result.confirm("No valid certificate", !revoked.successful_validation())) {
2✔
1659
            result.test_note(revoked.result_string());
×
1660
         }
1661
         result.test_is_eq("Certificate is revoked", revoked.result(), Botan::Certificate_Status_Code::CERT_IS_REVOKED);
1✔
1662

1663
         return {result};
2✔
1664
      }
2✔
1665
};
1666

1667
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1668

1669
   #endif
1670

1671
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1672

1673
class XMSS_Path_Validation_Tests final : public Test {
×
1674
   public:
1675
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1676
         Test::Result result(name);
2✔
1677

1678
         const Botan::Path_Validation_Restrictions restrictions;
4✔
1679
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1680

1681
         auto cert_path = std::vector<Botan::X509_Certificate>{self_signed};
4✔
1682
         auto valid_time = Botan::calendar_point(2019, 10, 8, 4, 45, 0).to_std_timepoint();
2✔
1683

1684
         auto status = Botan::PKIX::overall_status(
2✔
1685
            Botan::PKIX::check_chain(cert_path, valid_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions));
4✔
1686
         result.test_eq("Cert validation status", Botan::to_string(status), "Verified");
2✔
1687
         return result;
2✔
1688
      }
4✔
1689

1690
      std::vector<Test::Result> run() override {
1✔
1691
         if(Botan::has_filesystem_impl() == false) {
1✔
1692
            return {Test::Result::Note("XMSS path validation", "Skipping due to missing filesystem access")};
×
1693
         }
1694

1695
         return {
1✔
1696
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1697
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1698
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1699
      }
4✔
1700
};
1701

1702
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1703

1704
   #endif
1705

1706
#endif
1707

1708
}  // namespace
1709

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