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

randombit / botan / 19669262805

25 Nov 2025 12:18PM UTC coverage: 90.664% (+0.04%) from 90.627%
19669262805

push

github

web-flow
Merge pull request #5047 from Rohde-Schwarz/intermediate-trust-anchors

X.509 Path: Option for Non-Self-Signed Trust Anchors

100862 of 111248 relevant lines covered (90.66%)

12768805.85 hits per line

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

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

8
#include "tests.h"
9

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

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

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

34
namespace Botan_Tests {
35

36
namespace {
37

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

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

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

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

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

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

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

68
   return m;
18✔
69
}
9✔
70

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

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

81
   return certs;
80✔
82
}
80✔
83

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

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

91
   return result;
4✔
92
}
×
93

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

202
         return results;
2✔
203
      }
2✔
204
};
205

206
BOTAN_REGISTER_TEST("x509", "x509_path_x509test", X509test_Path_Validation_Tests);
207

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

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

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

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

231
   std::vector<Test::Result> results;
2✔
232

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

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

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

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

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

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

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

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

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

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

284
      result.end_timer();
152✔
285
      results.push_back(result);
152✔
286
   }
152✔
287

288
   return results;
2✔
289
}
154✔
290

291
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
292

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

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

303
   std::vector<Test::Result> results;
1✔
304

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

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

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

313
      Botan::Certificate_Store_In_Memory store;
3✔
314

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

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

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

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

329
      result.end_timer();
3✔
330
      results.push_back(result);
3✔
331
   }
3✔
332

333
   return results;
1✔
334
}
1✔
335

336
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
337

338
      #if defined(BOTAN_HAS_PSS)
339

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

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

350
   std::vector<Test::Result> results;
1✔
351

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

354
   auto validation_times_iter = validation_times.begin();
1✔
355

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

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

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

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

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

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

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

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

400
         Botan::Path_Validation_Restrictions restrictions(false, 80);  // SHA-1 is used
182✔
401

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

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

418
      result.end_timer();
118✔
419
      results.push_back(result);
118✔
420
   }
334✔
421

422
   return results;
1✔
423
}
19✔
424

425
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
426

427
      #endif
428

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

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

439
   std::vector<Test::Result> results;
1✔
440

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

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

447
   Botan::X509_Certificate root(root_crt);
1✔
448
   Botan::X509_Certificate intermediate(int_crt);
1✔
449
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
450

451
   Botan::Certificate_Store_In_Memory trusted;
1✔
452
   trusted.add_certificate(root);
1✔
453

454
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
455

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

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

463
   Botan::Certificate_Store_In_Memory empty;
1✔
464

465
   std::vector<Botan::X509_Certificate> new_chain = {ee_cert, intermediate, root};
4✔
466

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

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

472
   return {result};
2✔
473
}
4✔
474

475
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
476

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

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

487
   std::vector<Test::Result> results;
1✔
488

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

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

495
   Botan::X509_Certificate root(root_crt);
1✔
496
   Botan::X509_Certificate intermediate(int_crt);
1✔
497
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
498

499
   Botan::Certificate_Store_In_Memory trusted;
1✔
500
   trusted.add_certificate(root);
1✔
501

502
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
503

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

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

513
   return {result};
2✔
514
}
3✔
515

516
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
517

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

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

528
   std::vector<Test::Result> results;
1✔
529

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

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

536
   Botan::X509_Certificate root(root_crt);
1✔
537
   Botan::X509_Certificate intermediate(int_crt);
1✔
538
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
539

540
   Botan::Certificate_Store_In_Memory trusted;
1✔
541
   trusted.add_certificate(root);
1✔
542

543
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
544

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

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

554
   return {result};
2✔
555
}
3✔
556

557
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
558

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

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

569
   std::vector<Test::Result> results;
1✔
570

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

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

577
   Botan::X509_Certificate root(root_crt);
1✔
578
   Botan::X509_Certificate intermediate(int_crt);
1✔
579
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
580

581
   Botan::Certificate_Store_In_Memory trusted;
1✔
582
   trusted.add_certificate(root);
1✔
583

584
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
585

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

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

593
   return {result};
2✔
594
}
3✔
595

596
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
597

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

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

608
   std::vector<Test::Result> results;
1✔
609

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

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

616
   Botan::X509_Certificate root(root_crt);
1✔
617
   Botan::X509_Certificate intermediate(int_crt);
1✔
618
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
619

620
   Botan::Certificate_Store_In_Memory trusted;
1✔
621
   trusted.add_certificate(root);
1✔
622

623
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
624

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

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

632
   return {result};
2✔
633
}
3✔
634

635
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
636

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

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

647
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
648
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
649

650
         Botan::Certificate_Store_In_Memory trusted;
1✔
651
         trusted.add_certificate(trusted_root_cert);
1✔
652

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

655
         Test::Result result("Root cert time check");
1✔
656

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

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

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

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

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

733
         return {result};
2✔
734
      }
3✔
735
};
736

737
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
738

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

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

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

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

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

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

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

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

783
            const Botan::Certificate_Store_In_Memory trusted(trust_anchor);
5✔
784

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

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

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

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

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

803
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
804
         const auto certs = get_valid_cert_chain();
1✔
805

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

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

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

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

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

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

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

859
         return results;
1✔
860
      }
7✔
861

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

867
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
868

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

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

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

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

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

885
         return {result};
3✔
886
      }
3✔
887

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

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

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

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

903
         return result;
6✔
904
      }
6✔
905

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

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

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

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

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

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

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

952
         res = build_path_test();
1✔
953
         results.insert(results.end(), res.begin(), res.end());
1✔
954

955
         res = forbidden_self_signed_trust_anchors_test();
1✔
956
         results.insert(results.end(), res.begin(), res.end());
1✔
957

958
         res = stand_alone_root_tests();
1✔
959
         results.insert(results.end(), res.begin(), res.end());
1✔
960

961
         return results;
1✔
962
      }
1✔
963
};
964

965
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
966

967
class BSI_Path_Validation_Tests final : public Test
×
968

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

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

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

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

992
   std::vector<Test::Result> results;
2✔
993

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

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

1000
      Botan::Certificate_Store_In_Memory trusted;
108✔
1001
      std::vector<Botan::X509_Certificate> certs;
108✔
1002

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

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

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

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

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

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

1056
               static constexpr result_type min() { return 0; }
1057

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

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

1066
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
1067

1068
            private:
1069
               Botan::RandomNumberGenerator& m_rng;
1070
         } rbg(this->rng());
100✔
1071

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

1075
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,600✔
1076
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,600✔
1077

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

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

1118
      result.end_timer();
108✔
1119
      results.push_back(result);
108✔
1120
   }
108✔
1121

1122
   return results;
2✔
1123
}
2✔
1124

1125
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1126

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

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

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

1141
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1142

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

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

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

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

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

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

1177
         return result;
2✔
1178
      }
2✔
1179

1180
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1181
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1182
         Botan::Certificate_Store_In_Memory trusted;
1✔
1183

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

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

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

1193
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1194

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

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

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

1220
         return result;
2✔
1221
      }
2✔
1222

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

1227
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1228

1229
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1230
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1231
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1232

1233
         trusted.add_certificate(trust_root);
1✔
1234

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

1237
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1238

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

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

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

1261
         return result;
2✔
1262
      }
2✔
1263

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

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

1270
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1271
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1272
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1273

1274
         trusted.add_certificate(trust_root);
1✔
1275

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

1278
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1279

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

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

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

1303
         return result;
2✔
1304
      }
2✔
1305

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

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

1314
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1315
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1316
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1317

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

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

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

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

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

1351
         return result;
1✔
1352
      }
4✔
1353

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

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

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

1368
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1369
         auto ocsp_ee_delegate_malformed =
1✔
1370
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1371

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

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

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

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

1409
         return result;
1✔
1410
      }
2✔
1411

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

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

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

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

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

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

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

1458
         return result;
1✔
1459
      }
2✔
1460

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

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

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

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

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

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

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

1497
         return result;
1✔
1498
      }
7✔
1499

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

1512
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1513

1514
   #endif
1515

1516
   #if defined(BOTAN_HAS_ECDSA)
1517

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

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

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

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

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

1550
         Botan::Certificate_Store_In_Memory trusted;
1✔
1551
         trusted.add_certificate(ca_crt);
1✔
1552

1553
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1554

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

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

1566
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1567

1568
         result.confirm("Expected status",
2✔
1569
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1570

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

1580
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1581

1582
         result.confirm("Expected status",
2✔
1583
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1584

1585
         // Verify the signature from the bad CA is actually correct
1586
         Botan::Certificate_Store_In_Memory frusted;
1✔
1587
         frusted.add_certificate(fake_ca_crt);
1✔
1588

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

1598
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1599

1600
         return {result};
2✔
1601
      }
5✔
1602
};
1603

1604
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1605

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

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

1619
         Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1620
         Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1621
         Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1622

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

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

1635
         Botan::Certificate_Store_In_Memory trusted;
1✔
1636
         trusted.add_certificate(root);
1✔
1637
         trusted.add_crl(crl);
1✔
1638

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

1643
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1644

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

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

1660
         return {result};
2✔
1661
      }
2✔
1662
};
1663

1664
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1665

1666
   #endif
1667

1668
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1669

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

1675
         Botan::Path_Validation_Restrictions restrictions;
4✔
1676
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1677

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

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

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

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

1699
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1700

1701
   #endif
1702

1703
#endif
1704

1705
}  // namespace
1706

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