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

randombit / botan / 25476046290

07 May 2026 01:38AM UTC coverage: 89.331%. Remained the same
25476046290

push

github

randombit
Update for 3.12.0 release

107574 of 120422 relevant lines covered (89.33%)

11308499.77 hits per line

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

96.02
/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 "test_arb_eq.h"
12
   #include <botan/assert.h>
13
   #include <botan/data_src.h>
14
   #include <botan/exceptn.h>
15
   #include <botan/pk_keys.h>
16
   #include <botan/pkcs10.h>
17
   #include <botan/rng.h>
18
   #include <botan/x509_crl.h>
19
   #include <botan/x509_ext.h>
20
   #include <botan/x509path.h>
21
   #include <botan/internal/calendar.h>
22
   #include <botan/internal/filesystem.h>
23
   #include <botan/internal/fmt.h>
24
   #include <botan/internal/parsing.h>
25

26
   #if defined(BOTAN_HAS_ECDSA)
27
      #include <botan/ec_group.h>
28
   #endif
29

30
   #include <algorithm>
31
   #include <fstream>
32
   #include <limits>
33
   #include <map>
34
   #include <string>
35
   #include <vector>
36
#endif
37

38
namespace Botan_Tests {
39

40
namespace {
41

42
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
43

44
[[maybe_unused]] std::map<std::string, std::string> read_results(const std::string& results_file,
10✔
45
                                                                 const char delim = ':') {
46
   std::ifstream in(results_file);
10✔
47
   if(!in.good()) {
10✔
48
      throw Test_Error("Failed reading " + results_file);
×
49
   }
50

51
   std::map<std::string, std::string> m;
10✔
52
   std::string line;
10✔
53
   while(in.good()) {
622✔
54
      std::getline(in, line);
612✔
55
      if(line.empty()) {
612✔
56
         continue;
32✔
57
      }
58
      if(line[0] == '#') {
594✔
59
         continue;
14✔
60
      }
61

62
      std::vector<std::string> parts = Botan::split_on(line, delim);
580✔
63

64
      if(parts.size() != 2) {
580✔
65
         throw Test_Error("Invalid line " + line);
×
66
      }
67

68
      m[parts[0]] = parts[1];
580✔
69
   }
580✔
70

71
   return m;
20✔
72
}
10✔
73

74
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
75

76
std::vector<Botan::X509_Certificate> load_cert_file(const std::string& filename) {
80✔
77
   Botan::DataSource_Stream in(filename);
80✔
78

79
   std::vector<Botan::X509_Certificate> certs;
80✔
80
   while(!in.end_of_data()) {
508✔
81
      try {
348✔
82
         certs.emplace_back(in);
348✔
83
      } catch(Botan::Decoding_Error&) {}
80✔
84
   }
85

86
   return certs;
80✔
87
}
80✔
88

89
std::set<Botan::Certificate_Status_Code> flatten(const Botan::CertificatePathStatusCodes& codes) {
4✔
90
   std::set<Botan::Certificate_Status_Code> result;
4✔
91

92
   for(const auto& statuses : codes) {
16✔
93
      result.insert(statuses.begin(), statuses.end());
12✔
94
   }
95

96
   return result;
4✔
97
}
×
98

99
Botan::Path_Validation_Restrictions get_default_restrictions(bool req_revocation_info, bool ocsp_all_intermediates) {
5✔
100
   return Botan::Path_Validation_Restrictions(req_revocation_info, 80 /*some tests use SHA-1*/, ocsp_all_intermediates);
10✔
101
}
102

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

114
std::vector<Botan::Path_Validation_Restrictions> restrictions_to_test(bool req_revocation_info,
3✔
115
                                                                      bool ocsp_all_intermediates) {
116
   std::vector<Botan::Path_Validation_Restrictions> restrictions;
3✔
117
   restrictions.emplace_back(get_default_restrictions(req_revocation_info, ocsp_all_intermediates));
3✔
118
   restrictions.emplace_back(
3✔
119
      get_allow_non_self_signed_anchors_restrictions(req_revocation_info, ocsp_all_intermediates));
6✔
120
   return restrictions;
3✔
121
}
×
122

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

134
   private:
135
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions) {
2✔
136
         std::vector<Test::Result> results;
2✔
137

138
         // Test certs generated by https://github.com/yymax/x509test
139
         const Botan::X509_Certificate root(Test::data_file("x509/x509test/root.pem"));
4✔
140
         Botan::Certificate_Store_In_Memory trusted;
2✔
141
         trusted.add_certificate(root);
2✔
142

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

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

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

152
            if(certs.empty()) {
74✔
153
               throw Test_Error("Failed to read certs from " + filename);
×
154
            }
155

156
            Botan::Path_Validation_Result path_result = Botan::x509_path_validate(
74✔
157
               certs, restrictions, trusted, "www.tls.test", Botan::Usage_Type::TLS_SERVER_AUTH, validation_time);
74✔
158

159
            if(path_result.successful_validation() && path_result.trust_root() != root) {
74✔
160
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
161
            }
162

163
            result.test_str_eq("test " + filename, path_result.result_string(), expected_result);
148✔
164
            result.test_str_eq("test no warnings string", path_result.warnings_string(), "");
74✔
165
            result.test_is_true("test no warnings", path_result.no_warnings());
74✔
166
            result.end_timer();
74✔
167
            results.push_back(result);
74✔
168
         }
74✔
169

170
         // test softfail
171
         {
2✔
172
            Test::Result result("X509test path validation softfail");
2✔
173
            result.start_timer();
2✔
174

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

183
            Botan::Path_Validation_Result path_result =
2✔
184
               Botan::x509_path_validate(certs,
2✔
185
                                         restrictions,
186
                                         trusted,
187
                                         "www.tls.test",
188
                                         Botan::Usage_Type::TLS_SERVER_AUTH,
189
                                         validation_time,
190
                                         /* activate check_ocsp_online */ std::chrono::milliseconds(1000),
2✔
191
                                         {});
2✔
192

193
            if(path_result.successful_validation() && path_result.trust_root() != root) {
2✔
194
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
195
            }
196

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

209
         return results;
2✔
210
      }
2✔
211
};
212

213
BOTAN_REGISTER_TEST("x509", "x509_path_x509test", X509test_Path_Validation_Tests);
214

215
class NIST_Path_Validation_Tests final : public Test {
1✔
216
   public:
217
      std::vector<Test::Result> run() override;
218

219
   private:
220
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
221
};
222

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

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

238
   std::vector<Test::Result> results;
2✔
239

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

254
   const Botan::X509_Certificate root_cert(Test::data_file("x509/nist/root.crt"));
4✔
255
   const Botan::X509_CRL root_crl(Test::data_file("x509/nist/root.crl"));
4✔
256

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

259
   for(const auto& [test_name, expected_result] : expected) {
154✔
260
      Test::Result result("NIST path validation");
152✔
261
      result.start_timer();
152✔
262

263
      try {
152✔
264
         const auto all_files = Test::files_in_data_dir("x509/nist/" + test_name);
152✔
265

266
         Botan::Certificate_Store_In_Memory store;
152✔
267
         store.add_certificate(root_cert);
152✔
268
         store.add_crl(root_crl);
152✔
269

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

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

283
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
152✔
284
            end_certs, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
152✔
285

286
         result.test_str_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
304✔
287
      } catch(std::exception& e) {
152✔
288
         result.test_failure(test_name, e.what());
×
289
      }
×
290

291
      result.end_timer();
152✔
292
      results.push_back(result);
152✔
293
   }
152✔
294

295
   return results;
2✔
296
}
154✔
297

298
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
299

300
class Extended_Path_Validation_Tests final : public Test {
1✔
301
   public:
302
      std::vector<Test::Result> run() override;
303
};
304

305
std::vector<Test::Result> Extended_Path_Validation_Tests::run() {
1✔
306
   if(Botan::has_filesystem_impl() == false) {
1✔
307
      return {Test::Result::Note("Extended x509 path validation", "Skipping due to missing filesystem access")};
×
308
   }
309

310
   std::vector<Test::Result> results;
1✔
311

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

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

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

320
      Botan::Certificate_Store_In_Memory store;
3✔
321

322
      for(const auto& file : all_files) {
13✔
323
         if(file.ends_with(".crt") && file != "end.crt") {
10✔
324
            store.add_certificate(Botan::X509_Certificate(file));
10✔
325
         }
326
      }
327

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

330
      const Botan::Path_Validation_Restrictions restrictions;
6✔
331
      const Botan::Path_Validation_Result validation_result =
3✔
332
         Botan::x509_path_validate(end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
3✔
333

334
      result.test_str_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
6✔
335

336
      result.end_timer();
3✔
337
      results.push_back(result);
3✔
338
   }
3✔
339

340
   return results;
1✔
341
}
1✔
342

343
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
344

345
      #if defined(BOTAN_HAS_PSS)
346

347
class PSS_Path_Validation_Tests : public Test {
1✔
348
   public:
349
      std::vector<Test::Result> run() override;
350
};
351

352
std::vector<Test::Result> PSS_Path_Validation_Tests::run() {
1✔
353
   if(Botan::has_filesystem_impl() == false) {
1✔
354
      return {Test::Result::Note("RSA-PSS X509 signature validation", "Skipping due to missing filesystem access")};
×
355
   }
356

357
   std::vector<Test::Result> results;
1✔
358

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

361
   auto validation_times_iter = validation_times.begin();
1✔
362

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

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

369
      std::optional<Botan::X509_CRL> crl;
118✔
370
      std::optional<Botan::X509_Certificate> end;
118✔
371
      std::optional<Botan::X509_Certificate> root;
118✔
372
      Botan::Certificate_Store_In_Memory store;
118✔
373
      std::optional<Botan::PKCS10_Request> csr;
118✔
374

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

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

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

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

401
         result.test_str_eq(test_name + " check_crl result",
6✔
402
                            Botan::Path_Validation_Result::status_string(Botan::PKIX::overall_status(crl_status)),
403
                            expected_result);
404
      } else if(end && root) {
118✔
405
         // CRT chain test
406

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

409
         const Botan::Path_Validation_Result validation_result =
91✔
410
            Botan::x509_path_validate(*end, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
91✔
411

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

425
      result.end_timer();
118✔
426
      results.push_back(result);
118✔
427
   }
334✔
428

429
   return results;
1✔
430
}
13✔
431

432
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
433

434
      #endif
435

436
class Validate_V1Cert_Test final : public Test {
1✔
437
   public:
438
      std::vector<Test::Result> run() override;
439
};
440

441
std::vector<Test::Result> Validate_V1Cert_Test::run() {
1✔
442
   if(Botan::has_filesystem_impl() == false) {
1✔
443
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
444
   }
445

446
   const std::vector<Test::Result> results;
1✔
447

448
   const std::string root_crt = Test::data_file("x509/misc/v1ca/root.pem");
1✔
449
   const std::string int_crt = Test::data_file("x509/misc/v1ca/int.pem");
1✔
450
   const std::string ee_crt = Test::data_file("x509/misc/v1ca/ee.pem");
1✔
451

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

454
   const Botan::X509_Certificate root(root_crt);
1✔
455
   const Botan::X509_Certificate intermediate(int_crt);
1✔
456
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
457

458
   Botan::Certificate_Store_In_Memory trusted;
1✔
459
   trusted.add_certificate(root);
1✔
460

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

463
   const Botan::Path_Validation_Restrictions restrictions;
2✔
464
   const Botan::Path_Validation_Result validation_result =
1✔
465
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
466

467
   Test::Result result("Verifying using v1 certificate");
1✔
468
   result.test_str_eq("Path validation result", validation_result.result_string(), "Verified");
1✔
469

470
   const Botan::Certificate_Store_In_Memory empty;
1✔
471

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

474
   const Botan::Path_Validation_Result validation_result2 =
1✔
475
      Botan::x509_path_validate(new_chain, restrictions, empty, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
476

477
   result.test_str_eq("Path validation result", validation_result2.result_string(), "Cannot establish trust");
1✔
478

479
   return {result};
2✔
480
}
4✔
481

482
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
483

484
class Validate_V2Uid_in_V1_Test final : public Test {
1✔
485
   public:
486
      std::vector<Test::Result> run() override;
487
};
488

489
std::vector<Test::Result> Validate_V2Uid_in_V1_Test::run() {
1✔
490
   if(Botan::has_filesystem_impl() == false) {
1✔
491
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
492
   }
493

494
   const std::vector<Test::Result> results;
1✔
495

496
   const std::string root_crt = Test::data_file("x509/v2-in-v1/root.pem");
1✔
497
   const std::string int_crt = Test::data_file("x509/v2-in-v1/int.pem");
1✔
498
   const std::string ee_crt = Test::data_file("x509/v2-in-v1/leaf.pem");
1✔
499

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

502
   const Botan::X509_Certificate root(root_crt);
1✔
503
   const Botan::X509_Certificate intermediate(int_crt);
1✔
504
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
505

506
   Botan::Certificate_Store_In_Memory trusted;
1✔
507
   trusted.add_certificate(root);
1✔
508

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

511
   const Botan::Path_Validation_Restrictions restrictions;
2✔
512
   const Botan::Path_Validation_Result validation_result =
1✔
513
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
514

515
   Test::Result result("Verifying v1 certificate using v2 uid fields");
1✔
516
   result.test_is_false("Path validation failed", validation_result.successful_validation());
1✔
517
   result.test_str_eq(
1✔
518
      "Path validation result", validation_result.result_string(), "Encountered v2 identifiers in v1 certificate");
1✔
519

520
   return {result};
2✔
521
}
3✔
522

523
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
524

525
class Validate_Name_Constraint_SAN_Test final : public Test {
1✔
526
   public:
527
      std::vector<Test::Result> run() override;
528
};
529

530
std::vector<Test::Result> Validate_Name_Constraint_SAN_Test::run() {
1✔
531
   if(Botan::has_filesystem_impl() == false) {
1✔
532
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
533
   }
534

535
   const std::vector<Test::Result> results;
1✔
536

537
   const std::string root_crt = Test::data_file("x509/name_constraint_san/root.pem");
1✔
538
   const std::string int_crt = Test::data_file("x509/name_constraint_san/int.pem");
1✔
539
   const std::string ee_crt = Test::data_file("x509/name_constraint_san/leaf.pem");
1✔
540

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

543
   const Botan::X509_Certificate root(root_crt);
1✔
544
   const Botan::X509_Certificate intermediate(int_crt);
1✔
545
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
546

547
   Botan::Certificate_Store_In_Memory trusted;
1✔
548
   trusted.add_certificate(root);
1✔
549

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

552
   const Botan::Path_Validation_Restrictions restrictions;
2✔
553
   const Botan::Path_Validation_Result validation_result =
1✔
554
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
555

556
   Test::Result result("Verifying certificate with alternative SAN violating name constraint");
1✔
557
   result.test_is_false("Path validation failed", validation_result.successful_validation());
1✔
558
   result.test_str_eq(
1✔
559
      "Path validation result", validation_result.result_string(), "Certificate does not pass name constraint");
1✔
560

561
   return {result};
2✔
562
}
3✔
563

564
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
565

566
class Validate_Name_Constraint_CaseInsensitive final : public Test {
1✔
567
   public:
568
      std::vector<Test::Result> run() override;
569
};
570

571
std::vector<Test::Result> Validate_Name_Constraint_CaseInsensitive::run() {
1✔
572
   if(Botan::has_filesystem_impl() == false) {
1✔
573
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
574
   }
575

576
   const std::vector<Test::Result> results;
1✔
577

578
   const std::string root_crt = Test::data_file("x509/misc/name_constraint_ci/root.pem");
1✔
579
   const std::string int_crt = Test::data_file("x509/misc/name_constraint_ci/int.pem");
1✔
580
   const std::string ee_crt = Test::data_file("x509/misc/name_constraint_ci/leaf.pem");
1✔
581

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

584
   const Botan::X509_Certificate root(root_crt);
1✔
585
   const Botan::X509_Certificate intermediate(int_crt);
1✔
586
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
587

588
   Botan::Certificate_Store_In_Memory trusted;
1✔
589
   trusted.add_certificate(root);
1✔
590

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

593
   const Botan::Path_Validation_Restrictions restrictions;
2✔
594
   const Botan::Path_Validation_Result validation_result =
1✔
595
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
596

597
   Test::Result result("DNS name constraints are case insensitive");
1✔
598
   result.test_is_true("Path validation succeeded", validation_result.successful_validation());
1✔
599

600
   return {result};
2✔
601
}
3✔
602

603
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
604

605
class Validate_Name_Constraint_NoCheckSelf final : public Test {
1✔
606
   public:
607
      std::vector<Test::Result> run() override;
608
};
609

610
std::vector<Test::Result> Validate_Name_Constraint_NoCheckSelf::run() {
1✔
611
   if(Botan::has_filesystem_impl() == false) {
1✔
612
      return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
613
   }
614

615
   const std::vector<Test::Result> results;
1✔
616

617
   const std::string root_crt = Test::data_file("x509/misc/nc_skip_self/root.pem");
1✔
618
   const std::string int_crt = Test::data_file("x509/misc/nc_skip_self/int.pem");
1✔
619
   const std::string ee_crt = Test::data_file("x509/misc/nc_skip_self/leaf.pem");
1✔
620

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

623
   const Botan::X509_Certificate root(root_crt);
1✔
624
   const Botan::X509_Certificate intermediate(int_crt);
1✔
625
   const Botan::X509_Certificate ee_cert(ee_crt);
1✔
626

627
   Botan::Certificate_Store_In_Memory trusted;
1✔
628
   trusted.add_certificate(root);
1✔
629

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

632
   const Botan::Path_Validation_Restrictions restrictions;
2✔
633
   const Botan::Path_Validation_Result validation_result =
1✔
634
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
635

636
   Test::Result result("Name constraints do not apply to the certificate which includes them");
1✔
637
   result.test_is_true("Path validation succeeded", validation_result.successful_validation());
1✔
638

639
   return {result};
2✔
640
}
3✔
641

642
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
643

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

651
         const std::string trusted_root_crt = Test::data_file("x509/misc/root_cert_time_check/root.crt");
1✔
652
         const std::string leaf_crt = Test::data_file("x509/misc/root_cert_time_check/leaf.crt");
1✔
653

654
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
655
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
656

657
         Botan::Certificate_Store_In_Memory trusted;
1✔
658
         trusted.add_certificate(trusted_root_cert);
1✔
659

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

662
         Test::Result result("Root cert time check");
1✔
663

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

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

688
            result.test_enum_eq(descr_str, validation_result.result(), exp_status);
10✔
689
            const auto warnings = validation_result.warnings();
10✔
690
            BOTAN_ASSERT_NOMSG(warnings.size() == 2);
10✔
691
            result.test_is_true("No warning for leaf cert", warnings.at(0).empty());
10✔
692
            if(exp_warning) {
10✔
693
               result.test_is_true("Warning for root cert",
8✔
694
                                   warnings.at(1).size() == 1 && warnings.at(1).contains(*exp_warning));
8✔
695
            } else {
696
               result.test_is_true("No warning for root cert", warnings.at(1).empty());
6✔
697
            }
698
         };
10✔
699
         // (Trusted) root cert validity range: 2022-2028
700
         // Leaf cert validity range: 2020-2030
701

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

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

740
         return {result};
2✔
741
      }
3✔
742
};
743

744
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
745

746
class Non_Self_Signed_Trust_Anchors_Test final : public Test {
1✔
747
   private:
748
      using Cert_Path = std::vector<Botan::X509_Certificate>;
749

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

760
      /// Time point fits for all certificates used in this class
761
      std::chrono::system_clock::time_point get_validation_time() {
8✔
762
         return Botan::calendar_point(2016, 10, 21, 4, 20, 0).to_std_timepoint();
16✔
763
      }
764

765
      Botan::X509_Certificate get_self_signed_ee_cert() {
1✔
766
         const std::string valid_chain_pem = "x509/misc/self-signed-end-entity.pem";
1✔
767
         return Botan::X509_Certificate(Test::data_file(valid_chain_pem));
2✔
768
      }
1✔
769

770
      std::vector<Test::Result> path_validate_test() {
1✔
771
         std::vector<Test::Result> results;
1✔
772

773
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
774
         const auto certs = get_valid_cert_chain();
1✔
775
         const auto validation_time = get_validation_time();
1✔
776

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

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

790
            const Botan::Certificate_Store_In_Memory trusted(trust_anchor);
5✔
791

792
            auto path_result = Botan::x509_path_validate(
5✔
793
               end_certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
5✔
794

795
            if(path_result.successful_validation() && path_result.trust_root() != trust_anchor) {
5✔
796
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
797
            }
798

799
            result.test_str_eq(
5✔
800
               "path validation failed", path_result.result_string(), to_string(Botan::Certificate_Status_Code::OK));
5✔
801

802
            results.push_back(result);
5✔
803
         }
5✔
804
         return results;
1✔
805
      }
7✔
806

807
      std::vector<Test::Result> build_path_test() {
1✔
808
         std::vector<Test::Result> results;
1✔
809

810
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
811
         const auto certs = get_valid_cert_chain();
1✔
812

813
         // Helper to create a cert path {certs[0],...,certs[last_cert]}
814
         const auto path_to = [&](size_t last_cert) -> Cert_Path {
20✔
815
            BOTAN_ASSERT_NOMSG(last_cert <= certs.size());
19✔
816
            return {certs.begin(), certs.begin() + last_cert + 1};
19✔
817
         };
1✔
818

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

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

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

842
            std::vector<Cert_Path> cert_paths;
5✔
843
            const auto build_all_res =
5✔
844
               Botan::PKIX::build_all_certificate_paths(cert_paths, {&cert_store}, certs.at(0), certs);
5✔
845
            result.test_str_eq("build_all_certificate_paths result",
5✔
846
                               to_string(build_all_res),
847
                               to_string(Botan::Certificate_Status_Code::OK));
848
            test_arb_eq(result, "build_all_certificate_paths paths", cert_paths, expected_paths);
5✔
849

850
            Cert_Path cert_path;
5✔
851
            const auto build_path_res =
5✔
852
               Botan::PKIX::build_certificate_path(cert_path, {&cert_store}, certs.at(0), certs);
5✔
853
            result.test_str_eq("build_certificate_path result",
5✔
854
                               to_string(build_path_res),
855
                               to_string(Botan::Certificate_Status_Code::OK));
856

857
            if(std::ranges::find(cert_paths, path_to(4)) != cert_paths.end()) {
10✔
858
               test_arb_eq(result, "build_certificate_path (with self-signed anchor)", cert_path, path_to(4));
3✔
859
            } else {
860
               test_arb_eq(
2✔
861
                  result, "build_certificate_path (without self-signed anchor)", cert_path, expected_paths.at(0));
2✔
862
            }
863
            results.push_back(result);
5✔
864
         }
5✔
865

866
         return results;
1✔
867
      }
7✔
868

869
      std::vector<Test::Result> forbidden_self_signed_trust_anchors_test() {
1✔
870
         const auto restrictions = get_default_restrictions(false, false);  // non-self-signed anchors are forbidden
1✔
871
         const auto certs = get_valid_cert_chain();
1✔
872
         const auto validation_time = get_validation_time();
1✔
873

874
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
875

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

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

881
         result.test_str_eq("unexpected path validation result",
1✔
882
                            path_result.result_string(),
1✔
883
                            to_string(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST));
884

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

888
         result.test_str_ne("unexpected check_chain result",
2✔
889
                            Botan::Path_Validation_Result(check_chain_result, {}).result_string(),
2✔
890
                            to_string(Botan::Certificate_Status_Code::OK));
891

892
         return {result};
3✔
893
      }
3✔
894

895
      Test::Result stand_alone_root_test(std::string_view test_name,
6✔
896
                                         const Botan::Path_Validation_Restrictions& restrictions,
897
                                         const Botan::X509_Certificate& standalone_cert,
898
                                         Botan::Certificate_Status_Code expected_result) {
899
         Test::Result result(test_name);
6✔
900

901
         const auto validation_time = get_validation_time();
6✔
902
         Botan::Certificate_Store_In_Memory cert_store(standalone_cert);
6✔
903

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

907
         if(path_result.result() == Botan::Certificate_Status_Code::CERT_PUBKEY_INVALID) {
6✔
908
            result.test_note("CERT_PUBKEY_INVALID encountered - was that key type disabled at build time?");
×
909
         } else {
910
            result.test_str_eq(
6✔
911
               "unexpected x509_path_validate result", path_result.result_string(), to_string(expected_result));
12✔
912
         }
913

914
         return result;
6✔
915
      }
6✔
916

917
      std::vector<Test::Result> stand_alone_root_tests() {
1✔
918
         const auto self_signed_trust_anchor_forbidden = get_default_restrictions(false, false);
1✔
919
         const auto self_signed_trust_anchor_allowed = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
920

921
         const auto cert_chain = get_valid_cert_chain();
1✔
922
         const auto& self_signed_root = cert_chain.at(4);
1✔
923
         const auto& ica = cert_chain.at(3);
1✔
924
         const auto& self_signed_ee = get_self_signed_ee_cert();
1✔
925

926
         return {
1✔
927
            stand_alone_root_test("Standalone self-signed root (non-self-signed trust anchors forbidden)",
928
                                  self_signed_trust_anchor_forbidden,
929
                                  self_signed_root,
930
                                  Botan::Certificate_Status_Code::OK),
931
            stand_alone_root_test("Standalone self-signed root (non-self-signed trust anchors allowed)",
932
                                  self_signed_trust_anchor_allowed,
933
                                  self_signed_root,
934
                                  Botan::Certificate_Status_Code::OK),
935

936
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors forbidden)",
937
                                  self_signed_trust_anchor_forbidden,
938
                                  self_signed_ee,
939
                                  Botan::Certificate_Status_Code::OK),
940
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors allowed)",
941
                                  self_signed_trust_anchor_allowed,
942
                                  self_signed_ee,
943
                                  Botan::Certificate_Status_Code::OK),
944

945
            stand_alone_root_test("Standalone intermediate certificate (non-self-signed trust anchors forbidden)",
946
                                  self_signed_trust_anchor_forbidden,
947
                                  ica,
948
                                  Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST),
949
            stand_alone_root_test("Standalone intermediate certificate (non-self-signed trust anchors allowed)",
950
                                  self_signed_trust_anchor_allowed,
951
                                  ica,
952
                                  Botan::Certificate_Status_Code::OK),
953
         };
8✔
954
      }
2✔
955

956
   public:
957
      std::vector<Test::Result> run() override {
1✔
958
         std::vector<Test::Result> results;
1✔
959

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

963
         res = build_path_test();
1✔
964
         results.insert(results.end(), res.begin(), res.end());
1✔
965

966
         res = forbidden_self_signed_trust_anchors_test();
1✔
967
         results.insert(results.end(), res.begin(), res.end());
1✔
968

969
         res = stand_alone_root_tests();
1✔
970
         results.insert(results.end(), res.begin(), res.end());
1✔
971

972
         return results;
1✔
973
      }
1✔
974
};
975

976
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
977

978
class BSI_Path_Validation_Tests final : public Test
1✔
979

980
{
981
   public:
982
      std::vector<Test::Result> run() override;
983

984
   private:
985
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
986
};
987

988
std::vector<Test::Result> BSI_Path_Validation_Tests::run() {
1✔
989
   std::vector<Test::Result> results;
1✔
990
   for(const auto& restrictions : restrictions_to_test(false, false)) {
3✔
991
      auto partial_res = run_with_restrictions(restrictions);
2✔
992
      results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
993
   }
3✔
994
   return results;
1✔
995
}
×
996

997
std::vector<Test::Result> BSI_Path_Validation_Tests::run_with_restrictions(
2✔
998
   const Botan::Path_Validation_Restrictions& restriction_template) {
999
   if(Botan::has_filesystem_impl() == false) {
2✔
1000
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
1001
   }
1002

1003
   std::vector<Test::Result> results;
2✔
1004

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

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

1011
      Botan::Certificate_Store_In_Memory trusted;
108✔
1012
      std::vector<Botan::X509_Certificate> certs;
108✔
1013

1014
      #if defined(BOTAN_HAS_MD5)
1015
      const bool has_md5 = true;
108✔
1016
      #else
1017
      const bool has_md5 = false;
1018
      #endif
1019

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

1022
      // By convention: if CRL is a substring if the test name,
1023
      // we need to check the CRLs
1024
      bool use_crl = false;
108✔
1025
      if(test_name.find("CRL") != std::string::npos) {
108✔
1026
         use_crl = true;
32✔
1027
      }
1028

1029
      try {
108✔
1030
         for(const auto& file : all_files) {
878✔
1031
            // found a trust anchor
1032
            if(file.find("TA") != std::string::npos) {
780✔
1033
               trusted.add_certificate(Botan::X509_Certificate(file));
98✔
1034
            }
1035
            // found the target certificate. It needs to be at the front of certs
1036
            else if(file.find("TC") != std::string::npos) {
682✔
1037
               certs.insert(certs.begin(), Botan::X509_Certificate(file));
108✔
1038
            }
1039
            // found a certificate that might be part of a valid certificate chain to the trust anchor
1040
            else if(file.find(".crt") != std::string::npos) {
574✔
1041
               certs.push_back(Botan::X509_Certificate(file));
220✔
1042
            } else if(file.find(".crl") != std::string::npos) {
464✔
1043
               trusted.add_crl(Botan::X509_CRL(file));
64✔
1044
            }
1045
         }
1046

1047
         const Botan::Path_Validation_Restrictions restrictions(
98✔
1048
            use_crl,
1049
            restriction_template.minimum_key_strength(),
1050
            use_crl,
1051
            restriction_template.max_ocsp_age(),
1052
            std::make_unique<Botan::Certificate_Store_In_Memory>(),
206✔
1053
            restriction_template.ignore_trusted_root_time_range(),
98✔
1054
            restriction_template.require_self_signed_trust_anchors());
196✔
1055

1056
         /*
1057
          * Following the test document, the test are executed 16 times with
1058
          * randomly chosen order of the available certificates. However, the target
1059
          * certificate needs to stay in front.
1060
          * For certain test, the order in which the certificates are given to
1061
          * the validation function may be relevant, i.e. if issuer DNs are
1062
          * ambiguous.
1063
          */
1064
         class random_bit_generator {
98✔
1065
            public:
1066
               using result_type = size_t;
1067

1068
               static constexpr result_type min() { return 0; }
1069

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

1072
               result_type operator()() {
160✔
1073
                  size_t s = 0;
160✔
1074
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
160✔
1075
                  return s;
160✔
1076
               }
1077

1078
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
98✔
1079

1080
            private:
1081
               Botan::RandomNumberGenerator& m_rng;
1082
         } rbg(this->rng());
98✔
1083

1084
         for(size_t r = 0; r < 16; r++) {
1,666✔
1085
            std::shuffle(++(certs.begin()), certs.end(), rbg);
1,568✔
1086

1087
            const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,568✔
1088
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,568✔
1089

1090
            // We expect to be warned
1091
            if(expected_result.starts_with("Warning: ")) {
1,568✔
1092
               const std::string stripped = expected_result.substr(std::string("Warning: ").size());
64✔
1093
               bool found_warning = false;
64✔
1094
               for(const auto& warning_set : validation_result.warnings()) {
256✔
1095
                  for(const auto& warning : warning_set) {
256✔
1096
                     const std::string warning_str(Botan::to_string(warning));
64✔
1097
                     if(stripped == warning_str) {
64✔
1098
                        result.test_str_eq(test_name + " path validation result", warning_str, stripped);
64✔
1099
                        found_warning = true;
64✔
1100
                     }
1101
                  }
64✔
1102
               }
64✔
1103
               if(!found_warning) {
64✔
1104
                  result.test_failure(test_name, "Did not receive the expected warning: " + stripped);
×
1105
               }
1106
            } else {
64✔
1107
               if(expected_result == "Hash function used is considered too weak for security" && has_md5 == false) {
1,504✔
1108
                  result.test_str_eq(test_name + " path validation result",
1109
                                     validation_result.result_string(),
1110
                                     "Certificate signed with unknown/unavailable algorithm");
1111
               } else {
1112
                  result.test_str_eq(
3,008✔
1113
                     test_name + " path validation result", validation_result.result_string(), expected_result);
3,008✔
1114
               }
1115
            }
1116
         }
1,568✔
1117
      }
98✔
1118

1119
      /* Some certificates are rejected when executing the X509_Certificate constructor
1120
       * by throwing a Decoding_Error exception.
1121
       */
1122
      catch(const Botan::Exception& e) {
10✔
1123
         if(e.error_type() == Botan::ErrorType::DecodingFailure) {
10✔
1124
            result.test_str_eq(test_name + " path validation result", e.what(), expected_result);
20✔
1125
         } else {
1126
            result.test_failure(test_name, e.what());
×
1127
         }
1128
      }
10✔
1129

1130
      result.end_timer();
108✔
1131
      results.push_back(result);
108✔
1132
   }
108✔
1133

1134
   return results;
2✔
1135
}
2✔
1136

1137
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1138

1139
class Path_Validation_With_OCSP_Tests final : public Test {
1✔
1140
   public:
1141
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
1142
         return Botan::X509_Certificate(Test::data_file(path));
48✔
1143
      }
1144

1145
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
1146
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
1147
      }
1148

1149
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
1150
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
1151
         Botan::Certificate_Store_In_Memory trusted;
1✔
1152

1153
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1154

1155
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1156
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1157
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1158
         trusted.add_certificate(trust_root);
1✔
1159

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

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

1164
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1165
                               const Botan::Certificate_Status_Code expected) {
1166
            const auto path_result = Botan::x509_path_validate(cert_path,
8✔
1167
                                                               restrictions,
1168
                                                               trusted,
4✔
1169
                                                               "",
1170
                                                               Botan::Usage_Type::UNSPECIFIED,
1171
                                                               valid_time,
1172
                                                               std::chrono::milliseconds(0),
4✔
1173
                                                               {ocsp});
8✔
1174

1175
            return result.test_is_true(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1176
                                          Botan::to_string(path_result.result()) + "'",
8✔
1177
                                       path_result.result() == expected);
8✔
1178
         };
8✔
1179

1180
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1181
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1182
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1183
                    Botan::Certificate_Status_Code::OK);
1184
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1185
                    Botan::Certificate_Status_Code::OK);
1186
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1187
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1188

1189
         return result;
2✔
1190
      }
2✔
1191

1192
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1193
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1194
         Botan::Certificate_Store_In_Memory trusted;
1✔
1195

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

1198
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1199
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1200
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1201
         trusted.add_certificate(trust_root);
1✔
1202

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

1205
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1206

1207
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1208
                               const Botan::Certificate_Status_Code expected) {
1209
            const auto path_result = Botan::x509_path_validate(cert_path,
8✔
1210
                                                               restrictions,
1211
                                                               trusted,
4✔
1212
                                                               "",
1213
                                                               Botan::Usage_Type::UNSPECIFIED,
1214
                                                               valid_time,
1215
                                                               std::chrono::milliseconds(0),
4✔
1216
                                                               {ocsp});
8✔
1217

1218
            return result.test_is_true(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1219
                                          Botan::to_string(path_result.result()) + "'",
8✔
1220
                                       path_result.result() == expected);
8✔
1221
         };
12✔
1222

1223
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1224
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1225
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1226
                    Botan::Certificate_Status_Code::OK);
1227
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1228
                    Botan::Certificate_Status_Code::OK);
1229
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1230
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1231

1232
         return result;
2✔
1233
      }
2✔
1234

1235
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
1236
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
1237
         Botan::Certificate_Store_In_Memory trusted;
1✔
1238

1239
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1240

1241
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1242
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1243
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1244

1245
         trusted.add_certificate(trust_root);
1✔
1246

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

1249
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1250

1251
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1252
                               const Botan::Certificate_Status_Code expected) {
1253
            const auto path_result = Botan::x509_path_validate(cert_path,
6✔
1254
                                                               restrictions,
1255
                                                               trusted,
3✔
1256
                                                               "",
1257
                                                               Botan::Usage_Type::UNSPECIFIED,
1258
                                                               valid_time,
1259
                                                               std::chrono::milliseconds(0),
3✔
1260
                                                               {ocsp});
6✔
1261

1262
            return result.test_is_true(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1263
                                          Botan::to_string(path_result.result()) + "'",
6✔
1264
                                       path_result.result() == expected);
6✔
1265
         };
9✔
1266

1267
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1268
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1269
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1270
                    Botan::Certificate_Status_Code::OK);
1271
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
1272

1273
         return result;
2✔
1274
      }
2✔
1275

1276
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
1277
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
1278
         Botan::Certificate_Store_In_Memory trusted;
1✔
1279

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

1282
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1283
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1284
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1285

1286
         trusted.add_certificate(trust_root);
1✔
1287

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

1290
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1291

1292
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1293
                               const Botan::Certificate_Status_Code expected) {
1294
            const auto path_result = Botan::x509_path_validate(cert_path,
6✔
1295
                                                               restrictions,
1296
                                                               trusted,
3✔
1297
                                                               "",
1298
                                                               Botan::Usage_Type::UNSPECIFIED,
1299
                                                               valid_time,
1300
                                                               std::chrono::milliseconds(0),
3✔
1301
                                                               {ocsp});
6✔
1302

1303
            return result.test_is_true(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1304
                                          Botan::to_string(path_result.result()) + "'",
6✔
1305
                                       path_result.result() == expected);
6✔
1306
         };
9✔
1307

1308
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1309
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1310
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1311
                    Botan::Certificate_Status_Code::OK);
1312
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1313
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1314

1315
         return result;
2✔
1316
      }
2✔
1317

1318
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1319
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1320
         Botan::Certificate_Store_In_Memory trusted;
1✔
1321

1322
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1323
                                                                 110,    // minimum key strength
1324
                                                                 true);  // OCSP for all intermediates
2✔
1325

1326
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1327
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1328
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1329

1330
         // These OCSP responses are signed by an authorized OCSP responder
1331
         // certificate issued by `ca` and `trust_root` respectively. Note that
1332
         // the responder certificates contain the "OCSP No Check" extension,
1333
         // meaning that they themselves do not need a revocation check via OCSP.
1334
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/bdr-ocsp-resp.der");
1✔
1335
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/bdr-int-ocsp-resp.der");
1✔
1336

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

1340
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1341
                               const Botan::Certificate_Status_Code expected) {
1342
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
1343
                                                               restrictions,
1344
                                                               trusted,
3✔
1345
                                                               "",
1346
                                                               Botan::Usage_Type::UNSPECIFIED,
1347
                                                               valid_time,
1348
                                                               std::chrono::milliseconds(0),
3✔
1349
                                                               {ocsp_ee, ocsp_ca});
9✔
1350

1351
            return result.test_is_true(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1352
                                          Botan::to_string(path_result.result()) + "'",
6✔
1353
                                       path_result.result() == expected);
6✔
1354
         };
12✔
1355

1356
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1357
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1358
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1359
                    Botan::Certificate_Status_Code::OK);
1360
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1361
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1362

1363
         return result;
1✔
1364
      }
4✔
1365

1366
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1367
         Test::Result result(
1✔
1368
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1369
         Botan::Certificate_Store_In_Memory trusted;
1✔
1370

1371
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1372
                                                                 110,     // minimum key strength
1373
                                                                 false);  // OCSP for all intermediates
2✔
1374

1375
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1376
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1377
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1378
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1379

1380
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1381
         auto ocsp_ee_delegate_malformed =
1✔
1382
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1383

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

1387
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1388
                               const Botan::OCSP::Response& ocsp_ee,
1389
                               const Botan::Certificate_Status_Code expected,
1390
                               const std::optional<Botan::Certificate_Status_Code> also_expected = std::nullopt) {
1391
            const auto path_result = Botan::x509_path_validate(cert_path,
6✔
1392
                                                               restrictions,
1393
                                                               trusted,
3✔
1394
                                                               "",
1395
                                                               Botan::Usage_Type::UNSPECIFIED,
1396
                                                               valid_time,
1397
                                                               std::chrono::milliseconds(0),
3✔
1398
                                                               {ocsp_ee});
6✔
1399

1400
            result.test_u32_eq("should result in expected validation status code",
3✔
1401
                               static_cast<uint32_t>(path_result.result()),
3✔
1402
                               static_cast<uint32_t>(expected));
1403
            if(also_expected) {
3✔
1404
               result.test_is_true("Secondary error is also present",
4✔
1405
                                   flatten(path_result.all_statuses()).contains(also_expected.value()));
6✔
1406
            }
1407
         };
6✔
1408

1409
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1410
                    ocsp_ee_delegate,
1411
                    Botan::Certificate_Status_Code::VERIFIED);
1412
         check_path(Botan::calendar_point(2022, 10, 8, 23, 30, 0).to_std_timepoint(),
1✔
1413
                    ocsp_ee_delegate,
1414
                    Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
1415
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1416
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1417
                    ocsp_ee_delegate_malformed,
1418
                    Botan::Certificate_Status_Code::OCSP_RESPONSE_MISSING_KEYUSAGE,
1419
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1420

1421
         return result;
1✔
1422
      }
2✔
1423

1424
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1425
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1426
         Botan::Certificate_Store_In_Memory trusted;
1✔
1427

1428
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1429
                                                                 110,     // minimum key strength
1430
                                                                 false);  // OCSP for all intermediates
2✔
1431

1432
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1433
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1434
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1435
         trusted.add_certificate(trust_root);
1✔
1436

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

1439
         auto check_path = [&](const std::string& forged_ocsp,
3✔
1440
                               const Botan::Certificate_Status_Code expected,
1441
                               const Botan::Certificate_Status_Code also_expected) {
1442
            auto ocsp = load_test_OCSP_resp(forged_ocsp);
2✔
1443
            const auto path_result =
2✔
1444
               Botan::x509_path_validate(cert_path,
4✔
1445
                                         restrictions,
1446
                                         trusted,
2✔
1447
                                         "",
1448
                                         Botan::Usage_Type::UNSPECIFIED,
1449
                                         Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
2✔
1450
                                         std::chrono::milliseconds(0),
2✔
1451
                                         {ocsp});
4✔
1452

1453
            result.test_enum_eq(
2✔
1454
               "Path validation with forged OCSP response should fail with", path_result.result(), expected);
2✔
1455
            result.test_is_true("Secondary error is also present",
4✔
1456
                                flatten(path_result.all_statuses()).contains(also_expected));
4✔
1457
            result.test_note("Validation result", Botan::to_string(path_result.result()));
2✔
1458
         };
8✔
1459

1460
         // In both cases the path validation should detect the forged OCSP
1461
         // response and generate an appropriate error. By no means it should
1462
         // follow the unauthentic OCSP response.
1463
         check_path("x509/ocsp/randombit_ocsp_forged_valid.der",
1✔
1464
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED,
1465
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1466
         check_path("x509/ocsp/randombit_ocsp_forged_revoked.der",
1✔
1467
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED,
1468
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1469

1470
         return result;
1✔
1471
      }
2✔
1472

1473
      static Test::Result validate_with_ocsp_self_signed_by_intermediate_cert() {
1✔
1474
         Test::Result result(
1✔
1475
            "path check with ocsp response for intermediate that is (maliciously) self-signed by the intermediate");
1✔
1476
         Botan::Certificate_Store_In_Memory trusted;
1✔
1477

1478
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1479
                                                                 110,    // minimum key strength
1480
                                                                 true);  // OCSP for all intermediates
2✔
1481

1482
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1483
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1484
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1485
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1486

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

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

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

1496
         const auto path_result =
1✔
1497
            Botan::x509_path_validate(cert_path,
4✔
1498
                                      restrictions,
1499
                                      trusted,
1500
                                      "",
1501
                                      Botan::Usage_Type::UNSPECIFIED,
1502
                                      Botan::calendar_point(2022, 9, 22, 22, 30, 0).to_std_timepoint(),
1✔
1503
                                      std::chrono::milliseconds(0),
1✔
1504
                                      {ocsp_ee, ocsp_ca});
3✔
1505
         result.test_is_true("should reject intermediate OCSP response",
1✔
1506
                             path_result.result() == Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_FOUND);
1✔
1507
         result.test_note("Validation result", Botan::to_string(path_result.result()));
1✔
1508

1509
         return result;
1✔
1510
      }
7✔
1511

1512
      std::vector<Test::Result> run() override {
1✔
1513
         return {validate_with_ocsp_with_next_update_without_max_age(),
1✔
1514
                 validate_with_ocsp_with_next_update_with_max_age(),
1515
                 validate_with_ocsp_without_next_update_without_max_age(),
1516
                 validate_with_ocsp_without_next_update_with_max_age(),
1517
                 validate_with_ocsp_with_authorized_responder(),
1518
                 validate_with_ocsp_with_authorized_responder_without_keyusage(),
1519
                 validate_with_forged_ocsp_using_self_signed_cert(),
1520
                 validate_with_ocsp_self_signed_by_intermediate_cert()};
9✔
1521
      }
1✔
1522
};
1523

1524
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1525

1526
   #endif
1527

1528
   #if defined(BOTAN_HAS_ECDSA)
1529

1530
class CVE_2020_0601_Tests final : public Test {
1✔
1531
   public:
1532
      std::vector<Test::Result> run() override {
1✔
1533
         Test::Result result("CVE-2020-0601");
1✔
1534

1535
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1536
            result.test_note("Skipping as application specific groups are not supported");
×
1537
            return {result};
×
1538
         }
1539

1540
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1541
            result.test_note("Skipping as secp384r1 is not supported");
×
1542
            return {result};
×
1543
         }
1544

1545
         const auto& secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
1546
         const Botan::OID curveball_oid("1.3.6.1.4.1.25258.4.2020.0601");
1✔
1547
         const Botan::EC_Group curveball(
1✔
1548
            curveball_oid,
1549
            secp384r1.get_p(),
1550
            secp384r1.get_a(),
1551
            secp384r1.get_b(),
1552
            BigInt(
2✔
1553
               "0xC711162A761D568EBEB96265D4C3CEB4F0C330EC8F6DD76E39BCC849ABABB8E34378D581065DEFC77D9FCED6B39075DE"),
1554
            BigInt(
2✔
1555
               "0x0CB090DE23BAC8D13E67E019A91B86311E5F342DEE17FD15FB7E278A32A1EAC98FC97E18CB2F3B2C487A7DA6F40107AC"),
1556
            secp384r1.get_order());
2✔
1557

1558
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1559
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1560
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1561

1562
         Botan::Certificate_Store_In_Memory trusted;
1✔
1563
         trusted.add_certificate(ca_crt);
1✔
1564

1565
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1566

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

1569
         const auto path_result1 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt, fake_ca_crt},
5✔
1570
                                                             restrictions,
1571
                                                             trusted,
1572
                                                             "",
1573
                                                             Botan::Usage_Type::UNSPECIFIED,
1574
                                                             valid_time,
1575
                                                             std::chrono::milliseconds(0),
1✔
1576
                                                             {});
3✔
1577

1578
         result.test_is_true("Validation failed", !path_result1.successful_validation());
1✔
1579

1580
         result.test_is_true("Expected status",
1✔
1581
                             path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1582

1583
         const auto path_result2 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1584
                                                             restrictions,
1585
                                                             trusted,
1586
                                                             "",
1587
                                                             Botan::Usage_Type::UNSPECIFIED,
1588
                                                             valid_time,
1589
                                                             std::chrono::milliseconds(0),
1✔
1590
                                                             {});
3✔
1591

1592
         result.test_is_true("Validation failed", !path_result2.successful_validation());
1✔
1593

1594
         result.test_is_true("Expected status",
1✔
1595
                             path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1596

1597
         // Verify the signature from the bad CA is actually correct
1598
         Botan::Certificate_Store_In_Memory frusted;
1✔
1599
         frusted.add_certificate(fake_ca_crt);
1✔
1600

1601
         const auto path_result3 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1602
                                                             restrictions,
1603
                                                             frusted,
1604
                                                             "",
1605
                                                             Botan::Usage_Type::UNSPECIFIED,
1606
                                                             valid_time,
1607
                                                             std::chrono::milliseconds(0),
1✔
1608
                                                             {});
3✔
1609

1610
         result.test_is_true("Validation succeeded", path_result3.successful_validation());
1✔
1611

1612
         return {result};
2✔
1613
      }
5✔
1614
};
1615

1616
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1617

1618
class Path_Validation_With_Immortal_CRL final : public Test {
1✔
1619
   public:
1620
      std::vector<Test::Result> run() override {
1✔
1621
         // RFC 5280 defines the nextUpdate field as "optional" (in line with
1622
         // the original X.509 standard), but then requires all conforming CAs
1623
         // to always define it. For best compatibility we must deal with both.
1624
         Test::Result result("Using a CRL without a nextUpdate field");
1✔
1625

1626
         if(Botan::has_filesystem_impl() == false) {
1✔
1627
            result.test_note("Skipping due to missing filesystem access");
×
1628
            return {result};
×
1629
         }
1630

1631
         const Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1632
         const Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1633
         const Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1634

1635
         // Check that a CRL without nextUpdate is parsable
1636
         auto crl = Botan::X509_CRL(Test::data_file("x509/misc/crl_without_nextupdate/valid_forever.crl"));
2✔
1637
         result.test_is_true("this update is set", crl.this_update().time_is_set());
1✔
1638
         result.test_is_true("next update is not set", !crl.next_update().time_is_set());
1✔
1639
         result.test_is_true("CRL is not empty", !crl.get_revoked().empty());
1✔
1640

1641
         // Ensure that we support the used sig algo, otherwish stop here
1642
         if(!Botan::EC_Group::supports_named_group("brainpool512r1")) {
1✔
1643
            result.test_note("Cannot test path validation because signature algorithm is not support in this build");
×
1644
            return {result};
×
1645
         }
1646

1647
         Botan::Certificate_Store_In_Memory trusted;
1✔
1648
         trusted.add_certificate(root);
1✔
1649
         trusted.add_crl(crl);
1✔
1650

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

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

1657
         // Validate a certificate that is not listed in the CRL
1658
         const auto valid = Botan::x509_path_validate(
1✔
1659
            valid_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1660
         if(!result.test_is_true("Valid certificate", valid.successful_validation())) {
1✔
1661
            result.test_note(valid.result_string());
×
1662
         }
1663

1664
         // Ensure that a certificate listed in the CRL is recognized as revoked
1665
         const auto revoked = Botan::x509_path_validate(
1✔
1666
            revoked_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1667
         if(!result.test_is_true("No valid certificate", !revoked.successful_validation())) {
1✔
1668
            result.test_note(revoked.result_string());
×
1669
         }
1670
         result.test_enum_eq(
2✔
1671
            "Certificate is revoked", revoked.result(), Botan::Certificate_Status_Code::CERT_IS_REVOKED);
1✔
1672

1673
         return {result};
2✔
1674
      }
2✔
1675
};
1676

1677
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1678

1679
   #endif
1680

1681
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1682

1683
class XMSS_Path_Validation_Tests final : public Test {
1✔
1684
   public:
1685
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1686
         Test::Result result(name);
2✔
1687

1688
         const Botan::Path_Validation_Restrictions restrictions;
4✔
1689
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1690

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

1694
         auto status = Botan::PKIX::overall_status(
2✔
1695
            Botan::PKIX::check_chain(cert_path, valid_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions));
4✔
1696
         result.test_str_eq("Cert validation status", Botan::to_string(status), "Verified");
2✔
1697
         return result;
2✔
1698
      }
4✔
1699

1700
      std::vector<Test::Result> run() override {
1✔
1701
         if(Botan::has_filesystem_impl() == false) {
1✔
1702
            return {Test::Result::Note("XMSS path validation", "Skipping due to missing filesystem access")};
×
1703
         }
1704

1705
         return {
1✔
1706
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1707
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1708
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1709
      }
4✔
1710
};
1711

1712
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1713

1714
   #endif
1715

1716
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
1717

1718
class Name_Constraint_DN_Prefix_Test final : public Test {
1✔
1719
   public:
1720
      static Test::Result validate_name_constraint_dn_prefix_accepted() {
1✔
1721
         // Regression test case taken from strongSwan that originally failed with Botan.
1722
         // OpenSSL also accepts this case.
1723
         // Excluded constraint: C=CH, O=another
1724
         // Subject DN: C=CH, CN=tester, O=another
1725
         // The validation should accept the chain since CN at position 1 breaks prefix match.
1726
         Test::Result result("strongSwan name constraint DN prefix matching accepted");
1✔
1727

1728
         const auto validation_time = Botan::calendar_point(2025, 12, 29, 9, 50, 0).to_std_timepoint();
1✔
1729

1730
         // These files were created with strongSwan using the OpenSSL plugin based on the
1731
         // strongSwan `test_excluded_dn` test case of the `certnames` test suite.
1732
         const Botan::X509_Certificate ca_accepted(
1✔
1733
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_ca_accepted.pem"));
2✔
1734
         const Botan::X509_Certificate intermediate_accepted(
1✔
1735
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_im_accepted.pem"));
2✔
1736
         const Botan::X509_Certificate subject_cert_accepted(
1✔
1737
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_subject_accepted.pem"));
2✔
1738

1739
         result.test_str_eq("CA DN", ca_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
1✔
1740
         result.test_str_eq(
2✔
1741
            "IM DN", intermediate_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
1✔
1742
         result.test_str_eq(
2✔
1743
            "Subject DN", subject_cert_accepted.subject_dn().to_string(), R"(C="CH",CN="tester",O="another")");
1✔
1744

1745
         Botan::Certificate_Store_In_Memory trusted;
1✔
1746
         trusted.add_certificate(ca_accepted);
1✔
1747
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_accepted, intermediate_accepted};
3✔
1748

1749
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1750

1751
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1752
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1753

1754
         result.test_is_true("Verification passes since Name Constraints are not a prefix",
1✔
1755
                             validation_result.successful_validation());
1✔
1756
         return result;
1✔
1757
      }
2✔
1758

1759
      static Test::Result validate_name_constraint_dn_prefix_not_accepted() {
1✔
1760
         // Excluded constraint: C=CH, O=another
1761
         // Subject DN: C=CH, O=another, CN=tester
1762
         // The validation should *not* accept the chain since the constraint is a prefix.
1763
         // This test catches changes in order of the RDNs to be compared in name constraint checks,
1764
         // which was an issue due to constructors of Botan::X509_DN taking a multimap.
1765
         Test::Result result("strongSwan name constraint DN prefix matching not accepted");
1✔
1766

1767
         const auto validation_time = Botan::calendar_point(2025, 12, 29, 9, 50, 0).to_std_timepoint();
1✔
1768

1769
         // These files were created with strongSwan using the OpenSSL plugin based on the
1770
         // strongSwan `test_excluded_dn` test case of the `certnames` test suite.
1771
         const Botan::X509_Certificate ca_not_accepted(
1✔
1772
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_ca_not_accepted.pem"));
2✔
1773
         const Botan::X509_Certificate intermediate_not_accepted(
1✔
1774
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_im_not_accepted.pem"));
2✔
1775
         const Botan::X509_Certificate subject_cert_not_accepted(
1✔
1776
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_subject_not_accepted.pem"));
2✔
1777

1778
         result.test_str_eq("CA DN", ca_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
1✔
1779
         result.test_str_eq(
2✔
1780
            "IM DN", intermediate_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
1✔
1781
         result.test_str_eq(
2✔
1782
            "Subject DN", subject_cert_not_accepted.subject_dn().to_string(), R"(C="CH",O="another",CN="tester")");
1✔
1783

1784
         Botan::Certificate_Store_In_Memory trusted;
1✔
1785
         trusted.add_certificate(ca_not_accepted);
1✔
1786
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_not_accepted, intermediate_not_accepted};
3✔
1787

1788
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1789

1790
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1791
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1792

1793
         result.test_is_false("Verification does not pass since Name Constraints is a prefix",
1✔
1794
                              validation_result.successful_validation());
1✔
1795
         return result;
1✔
1796
      }
2✔
1797

1798
      std::vector<Test::Result> run() override {
1✔
1799
         return {validate_name_constraint_dn_prefix_accepted(), validate_name_constraint_dn_prefix_not_accepted()};
3✔
1800
      }
1✔
1801
};
1802

1803
BOTAN_REGISTER_TEST("x509", "x509_path_nc_prefix_dn", Name_Constraint_DN_Prefix_Test);
1804

1805
class CVE_2026_35580_Test final : public Test {
1✔
1806
   public:
1807
      std::vector<Test::Result> run() override {
1✔
1808
         Test::Result result("CVE-2026-35580");
1✔
1809

1810
         const Botan::Path_Validation_Restrictions restrictions;
2✔
1811

1812
         auto end_entity = Botan::X509_Certificate(Test::data_file("x509/cve_2026_35580/end_entity.pem"));
2✔
1813
         auto trusted_root = Botan::X509_Certificate(Test::data_file("x509/cve_2026_35580/root.pem"));
2✔
1814

1815
         auto validation_time = Botan::calendar_point(2026, 3, 29, 0, 0, 0).to_std_timepoint();
1✔
1816

1817
         Botan::Certificate_Store_In_Memory trusted;
1✔
1818
         trusted.add_certificate(trusted_root);
1✔
1819

1820
         const auto validation_result = Botan::x509_path_validate(
1✔
1821
            {end_entity}, restrictions, {&trusted}, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
2✔
1822

1823
         result.test_str_eq(
1✔
1824
            "Cert validation status", Botan::to_string(validation_result.result()), "Cannot establish trust");
1825

1826
         return {result};
3✔
1827
      }
2✔
1828
};
1829

1830
BOTAN_REGISTER_TEST("x509", "x509_cve_2026_35580", CVE_2026_35580_Test);
1831

1832
   #endif
1833

1834
   #if defined(BOTAN_HAS_ECDSA)
1835

1836
/**
1837
* Test that the certificate path building DFS correctly handles constructing
1838
* paths with many possible intermediates.
1839
*/
1840
class Path_Building_Tests final : public Test {
1✔
1841
   public:
1842
      std::vector<Test::Result> run() override {
1✔
1843
         if(Botan::has_filesystem_impl() == false) {
1✔
1844
            return {Test::Result::Note("X509 path building", "Skipping due to missing filesystem access")};
×
1845
         }
1846

1847
         const std::string base_dir = "x509/path_building";
1✔
1848
         auto validation_time = Botan::calendar_point(2026, 4, 1, 4, 20, 0).to_std_timepoint();
1✔
1849

1850
         // Load root into trust store
1851
         Botan::Certificate_Store_In_Memory trust_store;
1✔
1852
         trust_store.add_certificate(Botan::X509_Certificate(Test::data_file(base_dir + "/root.pem")));
1✔
1853

1854
         // Load the intermediates
1855
         std::vector<Botan::X509_Certificate> intermediates;
1✔
1856
         for(const auto& file : Test::files_in_data_dir(base_dir)) {
24✔
1857
            if(file.find("level1_") != std::string::npos || file.find("level2_") != std::string::npos) {
23✔
1858
               intermediates.emplace_back(file);
14✔
1859
            }
1860
         }
1✔
1861

1862
         Test::Result result("X509 path building DFS");
1✔
1863
         result.start_timer();
1✔
1864

1865
         const Botan::Path_Validation_Restrictions restrictions(false, 80, false);
2✔
1866

1867
         for(const auto& [test_name, expected_result] : read_results(Test::data_file(base_dir + "/expected.txt"))) {
8✔
1868
            const Botan::X509_Certificate end_entity(Test::data_file(Botan::fmt("{}/{}", base_dir, test_name)));
14✔
1869

1870
            std::vector<Botan::X509_Certificate> end_certs;
7✔
1871
            end_certs.push_back(end_entity);
7✔
1872
            end_certs.insert(end_certs.end(), intermediates.begin(), intermediates.end());
7✔
1873

1874
            const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
7✔
1875
               end_certs, restrictions, {&trust_store}, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
14✔
1876

1877
            result.test_str_eq(
14✔
1878
               test_name + " path validation result", validation_result.result_string(), expected_result);
14✔
1879
         }
7✔
1880

1881
         result.end_timer();
1✔
1882
         return {result};
2✔
1883
      }
2✔
1884
};
1885

1886
BOTAN_REGISTER_TEST("x509", "x509_path_building", Path_Building_Tests);
1887

1888
   #endif
1889

1890
#endif
1891

1892
}  // namespace
1893

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