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

randombit / botan / 20853210396

09 Jan 2026 01:19PM UTC coverage: 90.427% (+0.004%) from 90.423%
20853210396

push

github

web-flow
Merge pull request #5223 from randombit/jack/more-chacha20poly1305-tests

Add the Poly1305 edge case tests from Wycheproof

101694 of 112460 relevant lines covered (90.43%)

12870862.34 hits per line

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

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

8
#include "tests.h"
9

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

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

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

36
namespace Botan_Tests {
37

38
namespace {
39

40
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
41

42
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
43

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

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

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

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

67
      m[parts[0]] = parts[1];
573✔
68
   }
573✔
69

70
   return m;
18✔
71
}
9✔
72

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

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

83
   return certs;
80✔
84
}
80✔
85

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

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

93
   return result;
4✔
94
}
×
95

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

206
         return results;
2✔
207
      }
2✔
208
};
209

210
BOTAN_REGISTER_TEST("x509", "x509_path_x509test", X509test_Path_Validation_Tests);
211

212
class NIST_Path_Validation_Tests final : public Test {
1✔
213
   public:
214
      std::vector<Test::Result> run() override;
215

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

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

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

235
   std::vector<Test::Result> results;
2✔
236

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

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

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

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

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

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

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

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

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

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

288
      result.end_timer();
152✔
289
      results.push_back(result);
152✔
290
   }
152✔
291

292
   return results;
2✔
293
}
154✔
294

295
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
296

297
class Extended_Path_Validation_Tests final : public Test {
1✔
298
   public:
299
      std::vector<Test::Result> run() override;
300
};
301

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

307
   std::vector<Test::Result> results;
1✔
308

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

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

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

317
      Botan::Certificate_Store_In_Memory store;
3✔
318

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

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

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

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

333
      result.end_timer();
3✔
334
      results.push_back(result);
3✔
335
   }
3✔
336

337
   return results;
1✔
338
}
1✔
339

340
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
341

342
      #if defined(BOTAN_HAS_PSS)
343

344
class PSS_Path_Validation_Tests : public Test {
1✔
345
   public:
346
      std::vector<Test::Result> run() override;
347
};
348

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

354
   std::vector<Test::Result> results;
1✔
355

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

358
   auto validation_times_iter = validation_times.begin();
1✔
359

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

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

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

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

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

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

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

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

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

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

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

422
      result.end_timer();
118✔
423
      results.push_back(result);
118✔
424
   }
334✔
425

426
   return results;
1✔
427
}
19✔
428

429
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
430

431
      #endif
432

433
class Validate_V1Cert_Test final : public Test {
1✔
434
   public:
435
      std::vector<Test::Result> run() override;
436
};
437

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

443
   const std::vector<Test::Result> results;
1✔
444

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

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

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

455
   Botan::Certificate_Store_In_Memory trusted;
1✔
456
   trusted.add_certificate(root);
1✔
457

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

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

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

467
   const Botan::Certificate_Store_In_Memory empty;
1✔
468

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

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

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

476
   return {result};
2✔
477
}
4✔
478

479
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
480

481
class Validate_V2Uid_in_V1_Test final : public Test {
1✔
482
   public:
483
      std::vector<Test::Result> run() override;
484
};
485

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

491
   const std::vector<Test::Result> results;
1✔
492

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

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

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

503
   Botan::Certificate_Store_In_Memory trusted;
1✔
504
   trusted.add_certificate(root);
1✔
505

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

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

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

517
   return {result};
2✔
518
}
3✔
519

520
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
521

522
class Validate_Name_Constraint_SAN_Test final : public Test {
1✔
523
   public:
524
      std::vector<Test::Result> run() override;
525
};
526

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

532
   const std::vector<Test::Result> results;
1✔
533

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

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

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

544
   Botan::Certificate_Store_In_Memory trusted;
1✔
545
   trusted.add_certificate(root);
1✔
546

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

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

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

558
   return {result};
2✔
559
}
3✔
560

561
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
562

563
class Validate_Name_Constraint_CaseInsensitive final : public Test {
1✔
564
   public:
565
      std::vector<Test::Result> run() override;
566
};
567

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

573
   const std::vector<Test::Result> results;
1✔
574

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

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

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

585
   Botan::Certificate_Store_In_Memory trusted;
1✔
586
   trusted.add_certificate(root);
1✔
587

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

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

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

597
   return {result};
2✔
598
}
3✔
599

600
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
601

602
class Validate_Name_Constraint_NoCheckSelf final : public Test {
1✔
603
   public:
604
      std::vector<Test::Result> run() override;
605
};
606

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

612
   const std::vector<Test::Result> results;
1✔
613

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

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

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

624
   Botan::Certificate_Store_In_Memory trusted;
1✔
625
   trusted.add_certificate(root);
1✔
626

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

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

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

636
   return {result};
2✔
637
}
3✔
638

639
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
640

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

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

651
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
652
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
653

654
         Botan::Certificate_Store_In_Memory trusted;
1✔
655
         trusted.add_certificate(trusted_root_cert);
1✔
656

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

659
         Test::Result result("Root cert time check");
1✔
660

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

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

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

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

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

737
         return {result};
2✔
738
      }
3✔
739
};
740

741
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
742

743
class Non_Self_Signed_Trust_Anchors_Test final : public Test {
1✔
744
   private:
745
      using Cert_Path = std::vector<Botan::X509_Certificate>;
746

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

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

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

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

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

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

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

787
            const Botan::Certificate_Store_In_Memory trusted(trust_anchor);
5✔
788

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

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

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

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

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

807
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
808
         const auto certs = get_valid_cert_chain();
1✔
809

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

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

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

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

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

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

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

863
         return results;
1✔
864
      }
7✔
865

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

871
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
872

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

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

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

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

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

889
         return {result};
3✔
890
      }
3✔
891

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

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

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

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

907
         return result;
6✔
908
      }
6✔
909

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

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

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

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

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

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

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

956
         res = build_path_test();
1✔
957
         results.insert(results.end(), res.begin(), res.end());
1✔
958

959
         res = forbidden_self_signed_trust_anchors_test();
1✔
960
         results.insert(results.end(), res.begin(), res.end());
1✔
961

962
         res = stand_alone_root_tests();
1✔
963
         results.insert(results.end(), res.begin(), res.end());
1✔
964

965
         return results;
1✔
966
      }
1✔
967
};
968

969
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
970

971
class BSI_Path_Validation_Tests final : public Test
1✔
972

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

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

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

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

996
   std::vector<Test::Result> results;
2✔
997

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

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

1004
      Botan::Certificate_Store_In_Memory trusted;
108✔
1005
      std::vector<Botan::X509_Certificate> certs;
108✔
1006

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

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

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

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

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

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

1061
               static constexpr result_type min() { return 0; }
1062

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

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

1071
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
1072

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

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

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

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

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

1123
      result.end_timer();
108✔
1124
      results.push_back(result);
108✔
1125
   }
108✔
1126

1127
   return results;
2✔
1128
}
2✔
1129

1130
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1131

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

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

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

1146
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1147

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

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

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

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

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

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

1182
         return result;
2✔
1183
      }
2✔
1184

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

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

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

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

1198
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1199

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

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

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

1225
         return result;
2✔
1226
      }
2✔
1227

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

1232
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1233

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

1238
         trusted.add_certificate(trust_root);
1✔
1239

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

1242
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1243

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

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

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

1266
         return result;
2✔
1267
      }
2✔
1268

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

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

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

1279
         trusted.add_certificate(trust_root);
1✔
1280

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

1283
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1284

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

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

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

1308
         return result;
2✔
1309
      }
2✔
1310

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

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

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

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

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

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

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

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

1356
         return result;
1✔
1357
      }
4✔
1358

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

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

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

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

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

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

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

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

1414
         return result;
1✔
1415
      }
2✔
1416

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

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

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

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

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

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

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

1463
         return result;
1✔
1464
      }
2✔
1465

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

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

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

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

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

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

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

1502
         return result;
1✔
1503
      }
7✔
1504

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

1517
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1518

1519
   #endif
1520

1521
   #if defined(BOTAN_HAS_ECDSA)
1522

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

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

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

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

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

1555
         Botan::Certificate_Store_In_Memory trusted;
1✔
1556
         trusted.add_certificate(ca_crt);
1✔
1557

1558
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1559

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

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

1571
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1572

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

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

1585
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1586

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

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

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

1603
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1604

1605
         return {result};
2✔
1606
      }
5✔
1607
};
1608

1609
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1610

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

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

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

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

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

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

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

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

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

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

1665
         return {result};
2✔
1666
      }
2✔
1667
};
1668

1669
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1670

1671
   #endif
1672

1673
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1674

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

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

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

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

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

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

1704
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1705

1706
   #endif
1707

1708
class Name_Constraint_DN_Prefix_Test final : public Test {
1✔
1709
   public:
1710
      static Test::Result validate_name_constraint_dn_prefix_accepted() {
1✔
1711
         // Regression test case taken from strongSwan that originally failed with Botan.
1712
         // OpenSSL also accepts this case.
1713
         // Excluded constraint: C=CH, O=another
1714
         // Subject DN: C=CH, CN=tester, O=another
1715
         // The validation should accept the chain since CN at position 1 breaks prefix match.
1716
         Test::Result result("strongSwan name constraint DN prefix matching accepted");
1✔
1717

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

1720
         // These files were created with strongSwan using the OpenSSL plugin based on the
1721
         // strongSwan `test_excluded_dn` test case of the `certnames` test suite.
1722
         const Botan::X509_Certificate ca_accepted(
1✔
1723
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_ca_accepted.pem"));
2✔
1724
         const Botan::X509_Certificate intermediate_accepted(
1✔
1725
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_im_accepted.pem"));
2✔
1726
         const Botan::X509_Certificate subject_cert_accepted(
1✔
1727
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_subject_accepted.pem"));
2✔
1728

1729
         result.test_eq("CA DN", ca_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
2✔
1730
         result.test_eq("IM DN", intermediate_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
2✔
1731
         result.test_eq(
3✔
1732
            "Subject DN", subject_cert_accepted.subject_dn().to_string(), R"(C="CH",CN="tester",O="another")");
2✔
1733

1734
         Botan::Certificate_Store_In_Memory trusted;
1✔
1735
         trusted.add_certificate(ca_accepted);
1✔
1736
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_accepted, intermediate_accepted};
3✔
1737

1738
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1739

1740
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1741
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1742

1743
         result.confirm("Verification passes since Name Constraints are not a prefix",
2✔
1744
                        validation_result.successful_validation());
1✔
1745
         return result;
1✔
1746
      }
2✔
1747

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

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

1758
         // These files were created with strongSwan using the OpenSSL plugin based on the
1759
         // strongSwan `test_excluded_dn` test case of the `certnames` test suite.
1760
         const Botan::X509_Certificate ca_not_accepted(
1✔
1761
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_ca_not_accepted.pem"));
2✔
1762
         const Botan::X509_Certificate intermediate_not_accepted(
1✔
1763
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_im_not_accepted.pem"));
2✔
1764
         const Botan::X509_Certificate subject_cert_not_accepted(
1✔
1765
            Test::data_file("x509/name_constraint_prefix/nc_prefix_strongswan_subject_not_accepted.pem"));
2✔
1766

1767
         result.test_eq("CA DN", ca_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
2✔
1768
         result.test_eq(
3✔
1769
            "IM DN", intermediate_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
2✔
1770
         result.test_eq(
3✔
1771
            "Subject DN", subject_cert_not_accepted.subject_dn().to_string(), R"(C="CH",O="another",CN="tester")");
2✔
1772

1773
         Botan::Certificate_Store_In_Memory trusted;
1✔
1774
         trusted.add_certificate(ca_not_accepted);
1✔
1775
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_not_accepted, intermediate_not_accepted};
3✔
1776

1777
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1778

1779
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1780
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1781

1782
         result.test_eq("Verification does not pass since Name Constraints is a prefix",
1✔
1783
                        validation_result.successful_validation(),
1✔
1784
                        false);
1785
         return result;
1✔
1786
      }
2✔
1787

1788
      std::vector<Test::Result> run() override {
1✔
1789
         return {validate_name_constraint_dn_prefix_accepted(), validate_name_constraint_dn_prefix_not_accepted()};
3✔
1790
      }
1✔
1791
};
1792

1793
BOTAN_REGISTER_TEST("x509", "x509_path_nc_prefix_dn", Name_Constraint_DN_Prefix_Test);
1794

1795
#endif
1796

1797
}  // namespace
1798

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