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

randombit / botan / 21240376859

22 Jan 2026 07:50AM UTC coverage: 90.071% (-0.004%) from 90.075%
21240376859

push

github

web-flow
Merge pull request #5254 from Rohde-Schwarz/chore/tls_does_not_depend_on_secp

Avoid hard dependency on `pcurves_secp*` in the TLS module

102101 of 113356 relevant lines covered (90.07%)

11524547.86 hits per line

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

95.78
/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
         if(path_result.result() == Botan::Certificate_Status_Code::CERT_PUBKEY_INVALID) {
6✔
905
            result.test_note("CERT_PUBKEY_INVALID encountered - was that key type disabled at build time?");
×
906
         } else {
907
            result.test_eq(
24✔
908
               "unexpected x509_path_validate result", path_result.result_string(), to_string(expected_result));
12✔
909
         }
910

911
         return result;
6✔
912
      }
6✔
913

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

918
         const auto cert_chain = get_valid_cert_chain();
1✔
919
         const auto& self_signed_root = cert_chain.at(4);
1✔
920
         const auto& ica = cert_chain.at(3);
1✔
921
         const auto& self_signed_ee = get_self_signed_ee_cert();
1✔
922

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

933
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors forbidden)",
1✔
934
                                  self_signed_trust_anchor_forbidden,
935
                                  self_signed_ee,
936
                                  Botan::Certificate_Status_Code::OK),
937
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors allowed)",
1✔
938
                                  self_signed_trust_anchor_allowed,
939
                                  self_signed_ee,
940
                                  Botan::Certificate_Status_Code::OK),
941

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

953
   public:
954
      std::vector<Test::Result> run() override {
1✔
955
         std::vector<Test::Result> results;
1✔
956

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

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

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

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

969
         return results;
1✔
970
      }
1✔
971
};
972

973
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
974

975
class BSI_Path_Validation_Tests final : public Test
1✔
976

977
{
978
   public:
979
      std::vector<Test::Result> run() override;
980

981
   private:
982
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
983
};
984

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

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

1000
   std::vector<Test::Result> results;
2✔
1001

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

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

1008
      Botan::Certificate_Store_In_Memory trusted;
108✔
1009
      std::vector<Botan::X509_Certificate> certs;
108✔
1010

1011
      #if defined(BOTAN_HAS_MD5)
1012
      const bool has_md5 = true;
108✔
1013
      #else
1014
      const bool has_md5 = false;
1015
      #endif
1016

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

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

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

1044
         const Botan::Path_Validation_Restrictions restrictions(
100✔
1045
            use_crl,
1046
            restriction_template.minimum_key_strength(),
1047
            use_crl,
1048
            restriction_template.max_ocsp_age(),
1049
            std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
1050
            restriction_template.ignore_trusted_root_time_range(),
100✔
1051
            restriction_template.require_self_signed_trust_anchors());
200✔
1052

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

1065
               static constexpr result_type min() { return 0; }
1066

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

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

1075
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
1076

1077
            private:
1078
               Botan::RandomNumberGenerator& m_rng;
1079
         } rbg(this->rng());
100✔
1080

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

1084
            const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,600✔
1085
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,600✔
1086

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

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

1127
      result.end_timer();
108✔
1128
      results.push_back(result);
108✔
1129
   }
108✔
1130

1131
   return results;
2✔
1132
}
2✔
1133

1134
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1135

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

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

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

1150
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1151

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

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

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

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

1172
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1173
                                     Botan::to_string(path_result.result()) + "'",
8✔
1174
                                  path_result.result() == expected);
8✔
1175
         };
8✔
1176

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

1186
         return result;
2✔
1187
      }
2✔
1188

1189
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1190
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1191
         Botan::Certificate_Store_In_Memory trusted;
1✔
1192

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

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

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

1202
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1203

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

1215
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1216
                                     Botan::to_string(path_result.result()) + "'",
8✔
1217
                                  path_result.result() == expected);
8✔
1218
         };
12✔
1219

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

1229
         return result;
2✔
1230
      }
2✔
1231

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

1236
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1237

1238
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1239
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1240
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1241

1242
         trusted.add_certificate(trust_root);
1✔
1243

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

1246
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1247

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

1259
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1260
                                     Botan::to_string(path_result.result()) + "'",
6✔
1261
                                  path_result.result() == expected);
6✔
1262
         };
9✔
1263

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

1270
         return result;
2✔
1271
      }
2✔
1272

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

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

1279
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1280
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1281
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1282

1283
         trusted.add_certificate(trust_root);
1✔
1284

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

1287
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1288

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

1300
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1301
                                     Botan::to_string(path_result.result()) + "'",
6✔
1302
                                  path_result.result() == expected);
6✔
1303
         };
9✔
1304

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

1312
         return result;
2✔
1313
      }
2✔
1314

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

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

1323
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1324
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1325
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1326

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

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

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

1348
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1349
                                     Botan::to_string(path_result.result()) + "'",
6✔
1350
                                  path_result.result() == expected);
6✔
1351
         };
12✔
1352

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

1360
         return result;
1✔
1361
      }
4✔
1362

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

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

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

1377
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1378
         auto ocsp_ee_delegate_malformed =
1✔
1379
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1380

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

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

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

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

1418
         return result;
1✔
1419
      }
2✔
1420

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

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

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

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

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

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

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

1467
         return result;
1✔
1468
      }
2✔
1469

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

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

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

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

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

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

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

1506
         return result;
1✔
1507
      }
7✔
1508

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

1521
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1522

1523
   #endif
1524

1525
   #if defined(BOTAN_HAS_ECDSA)
1526

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

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

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

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

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

1559
         Botan::Certificate_Store_In_Memory trusted;
1✔
1560
         trusted.add_certificate(ca_crt);
1✔
1561

1562
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1563

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

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

1575
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1576

1577
         result.confirm("Expected status",
2✔
1578
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1579

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

1589
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1590

1591
         result.confirm("Expected status",
2✔
1592
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1593

1594
         // Verify the signature from the bad CA is actually correct
1595
         Botan::Certificate_Store_In_Memory frusted;
1✔
1596
         frusted.add_certificate(fake_ca_crt);
1✔
1597

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

1607
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1608

1609
         return {result};
2✔
1610
      }
5✔
1611
};
1612

1613
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1614

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

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

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

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

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

1644
         Botan::Certificate_Store_In_Memory trusted;
1✔
1645
         trusted.add_certificate(root);
1✔
1646
         trusted.add_crl(crl);
1✔
1647

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

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

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

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

1669
         return {result};
2✔
1670
      }
2✔
1671
};
1672

1673
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1674

1675
   #endif
1676

1677
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1678

1679
class XMSS_Path_Validation_Tests final : public Test {
1✔
1680
   public:
1681
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1682
         Test::Result result(name);
2✔
1683

1684
         const Botan::Path_Validation_Restrictions restrictions;
4✔
1685
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1686

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

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

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

1701
         return {
1✔
1702
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1703
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1704
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1705
      }
4✔
1706
};
1707

1708
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1709

1710
   #endif
1711

1712
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
1713

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

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

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

1735
         result.test_eq("CA DN", ca_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
2✔
1736
         result.test_eq("IM DN", intermediate_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
2✔
1737
         result.test_eq(
3✔
1738
            "Subject DN", subject_cert_accepted.subject_dn().to_string(), R"(C="CH",CN="tester",O="another")");
2✔
1739

1740
         Botan::Certificate_Store_In_Memory trusted;
1✔
1741
         trusted.add_certificate(ca_accepted);
1✔
1742
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_accepted, intermediate_accepted};
3✔
1743

1744
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1745

1746
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1747
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1748

1749
         result.confirm("Verification passes since Name Constraints are not a prefix",
2✔
1750
                        validation_result.successful_validation());
1✔
1751
         return result;
1✔
1752
      }
2✔
1753

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

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

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

1773
         result.test_eq("CA DN", ca_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="CA")");
2✔
1774
         result.test_eq(
3✔
1775
            "IM DN", intermediate_not_accepted.subject_dn().to_string(), R"(C="CH",O="strongSwan",CN="IM")");
2✔
1776
         result.test_eq(
3✔
1777
            "Subject DN", subject_cert_not_accepted.subject_dn().to_string(), R"(C="CH",O="another",CN="tester")");
2✔
1778

1779
         Botan::Certificate_Store_In_Memory trusted;
1✔
1780
         trusted.add_certificate(ca_not_accepted);
1✔
1781
         const std::vector<Botan::X509_Certificate> chain = {subject_cert_not_accepted, intermediate_not_accepted};
3✔
1782

1783
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
1784

1785
         const Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1✔
1786
            chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
1787

1788
         result.test_eq("Verification does not pass since Name Constraints is a prefix",
1✔
1789
                        validation_result.successful_validation(),
1✔
1790
                        false);
1791
         return result;
1✔
1792
      }
2✔
1793

1794
      std::vector<Test::Result> run() override {
1✔
1795
         return {validate_name_constraint_dn_prefix_accepted(), validate_name_constraint_dn_prefix_not_accepted()};
3✔
1796
      }
1✔
1797
};
1798

1799
BOTAN_REGISTER_TEST("x509", "x509_path_nc_prefix_dn", Name_Constraint_DN_Prefix_Test);
1800

1801
   #endif
1802

1803
#endif
1804

1805
}  // namespace
1806

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