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

randombit / botan / 17298625897

28 Aug 2025 02:17PM UTC coverage: 90.7% (+0.04%) from 90.665%
17298625897

Pull #5047

github

web-flow
Merge 1f5935899 into 1b82e522d
Pull Request #5047: X.509 Path: Option for Non-Self-Signed Trust Anchors

100409 of 110704 relevant lines covered (90.7%)

12162848.02 hits per line

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

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

8
#include "tests.h"
9

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

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

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

34
namespace Botan_Tests {
35

36
namespace {
37

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

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

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

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

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

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

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

68
   return m;
18✔
69
}
9✔
70

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

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

81
   return certs;
80✔
82
}
80✔
83

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

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

91
   return result;
4✔
92
}
×
93

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

288
   return results;
2✔
289
}
154✔
290

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

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

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

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

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

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

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

313
      Botan::Certificate_Store_In_Memory store;
3✔
314

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

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

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

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

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

333
   return results;
1✔
334
}
1✔
335

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

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

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

348
   std::vector<Test::Result> results;
1✔
349

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

352
   auto validation_times_iter = validation_times.begin();
1✔
353

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

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

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

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

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

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

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

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

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

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

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

416
      result.end_timer();
118✔
417
      results.push_back(result);
118✔
418
   }
334✔
419

420
   return results;
1✔
421
}
19✔
422

423
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
424

425
class Validate_V1Cert_Test final : public Test {
×
426
   public:
427
      std::vector<Test::Result> run() override;
428
};
429

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

435
   std::vector<Test::Result> results;
1✔
436

437
   const std::string root_crt = Test::data_file("x509/misc/v1ca/root.pem");
1✔
438
   const std::string int_crt = Test::data_file("x509/misc/v1ca/int.pem");
1✔
439
   const std::string ee_crt = Test::data_file("x509/misc/v1ca/ee.pem");
1✔
440

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

443
   Botan::X509_Certificate root(root_crt);
1✔
444
   Botan::X509_Certificate intermediate(int_crt);
1✔
445
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
446

447
   Botan::Certificate_Store_In_Memory trusted;
1✔
448
   trusted.add_certificate(root);
1✔
449

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

452
   Botan::Path_Validation_Restrictions restrictions;
2✔
453
   Botan::Path_Validation_Result validation_result =
1✔
454
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
455

456
   Test::Result result("Verifying using v1 certificate");
1✔
457
   result.test_eq("Path validation result", validation_result.result_string(), "Verified");
2✔
458

459
   Botan::Certificate_Store_In_Memory empty;
1✔
460

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

463
   Botan::Path_Validation_Result validation_result2 =
1✔
464
      Botan::x509_path_validate(new_chain, restrictions, empty, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
465

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

468
   return {result};
2✔
469
}
4✔
470

471
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
472

473
class Validate_V2Uid_in_V1_Test final : public Test {
×
474
   public:
475
      std::vector<Test::Result> run() override;
476
};
477

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

483
   std::vector<Test::Result> results;
1✔
484

485
   const std::string root_crt = Test::data_file("x509/v2-in-v1/root.pem");
1✔
486
   const std::string int_crt = Test::data_file("x509/v2-in-v1/int.pem");
1✔
487
   const std::string ee_crt = Test::data_file("x509/v2-in-v1/leaf.pem");
1✔
488

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

491
   Botan::X509_Certificate root(root_crt);
1✔
492
   Botan::X509_Certificate intermediate(int_crt);
1✔
493
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
494

495
   Botan::Certificate_Store_In_Memory trusted;
1✔
496
   trusted.add_certificate(root);
1✔
497

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

500
   Botan::Path_Validation_Restrictions restrictions;
2✔
501
   Botan::Path_Validation_Result validation_result =
1✔
502
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
503

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

509
   return {result};
2✔
510
}
3✔
511

512
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
513

514
class Validate_Name_Constraint_SAN_Test final : public Test {
×
515
   public:
516
      std::vector<Test::Result> run() override;
517
};
518

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

524
   std::vector<Test::Result> results;
1✔
525

526
   const std::string root_crt = Test::data_file("x509/name_constraint_san/root.pem");
1✔
527
   const std::string int_crt = Test::data_file("x509/name_constraint_san/int.pem");
1✔
528
   const std::string ee_crt = Test::data_file("x509/name_constraint_san/leaf.pem");
1✔
529

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

532
   Botan::X509_Certificate root(root_crt);
1✔
533
   Botan::X509_Certificate intermediate(int_crt);
1✔
534
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
535

536
   Botan::Certificate_Store_In_Memory trusted;
1✔
537
   trusted.add_certificate(root);
1✔
538

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

541
   Botan::Path_Validation_Restrictions restrictions;
2✔
542
   Botan::Path_Validation_Result validation_result =
1✔
543
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
544

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

550
   return {result};
2✔
551
}
3✔
552

553
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
554

555
class Validate_Name_Constraint_CaseInsensitive final : public Test {
×
556
   public:
557
      std::vector<Test::Result> run() override;
558
};
559

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

565
   std::vector<Test::Result> results;
1✔
566

567
   const std::string root_crt = Test::data_file("x509/misc/name_constraint_ci/root.pem");
1✔
568
   const std::string int_crt = Test::data_file("x509/misc/name_constraint_ci/int.pem");
1✔
569
   const std::string ee_crt = Test::data_file("x509/misc/name_constraint_ci/leaf.pem");
1✔
570

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

573
   Botan::X509_Certificate root(root_crt);
1✔
574
   Botan::X509_Certificate intermediate(int_crt);
1✔
575
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
576

577
   Botan::Certificate_Store_In_Memory trusted;
1✔
578
   trusted.add_certificate(root);
1✔
579

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

582
   Botan::Path_Validation_Restrictions restrictions;
2✔
583
   Botan::Path_Validation_Result validation_result =
1✔
584
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
585

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

589
   return {result};
2✔
590
}
3✔
591

592
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
593

594
class Validate_Name_Constraint_NoCheckSelf final : public Test {
×
595
   public:
596
      std::vector<Test::Result> run() override;
597
};
598

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

604
   std::vector<Test::Result> results;
1✔
605

606
   const std::string root_crt = Test::data_file("x509/misc/nc_skip_self/root.pem");
1✔
607
   const std::string int_crt = Test::data_file("x509/misc/nc_skip_self/int.pem");
1✔
608
   const std::string ee_crt = Test::data_file("x509/misc/nc_skip_self/leaf.pem");
1✔
609

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

612
   Botan::X509_Certificate root(root_crt);
1✔
613
   Botan::X509_Certificate intermediate(int_crt);
1✔
614
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
615

616
   Botan::Certificate_Store_In_Memory trusted;
1✔
617
   trusted.add_certificate(root);
1✔
618

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

621
   Botan::Path_Validation_Restrictions restrictions;
2✔
622
   Botan::Path_Validation_Result validation_result =
1✔
623
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
624

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

628
   return {result};
2✔
629
}
3✔
630

631
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
632

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

640
         const std::string trusted_root_crt = Test::data_file("x509/misc/root_cert_time_check/root.crt");
1✔
641
         const std::string leaf_crt = Test::data_file("x509/misc/root_cert_time_check/leaf.crt");
1✔
642

643
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
644
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
645

646
         Botan::Certificate_Store_In_Memory trusted;
1✔
647
         trusted.add_certificate(trusted_root_cert);
1✔
648

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

651
         Test::Result result("Root cert time check");
1✔
652

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

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

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

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

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

729
         return {result};
2✔
730
      }
3✔
731
};
732

733
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
734

735
class Non_Self_Signed_Trust_Anchors_Test final : public Test {
×
736
   private:
737
      using Cert_Path = std::vector<Botan::X509_Certificate>;
738

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

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

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

759
      std::vector<Test::Result> path_validate_test() {
1✔
760
         std::vector<Test::Result> results;
1✔
761

762
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
763
         const auto certs = get_valid_cert_chain();
1✔
764
         const auto validation_time = get_validation_time();
1✔
765

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

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

779
            const Botan::Certificate_Store_In_Memory trusted(trust_anchor);
5✔
780

781
            auto path_result = Botan::x509_path_validate(
5✔
782
               end_certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
5✔
783

784
            if(path_result.successful_validation() && path_result.trust_root() != trust_anchor) {
5✔
785
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
786
            }
787

788
            result.test_eq(
15✔
789
               "path validation failed", path_result.result_string(), to_string(Botan::Certificate_Status_Code::OK));
10✔
790

791
            results.push_back(result);
5✔
792
         }
5✔
793
         return results;
1✔
794
      }
7✔
795

796
      std::vector<Test::Result> build_path_test() {
1✔
797
         std::vector<Test::Result> results;
1✔
798

799
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
800
         const auto certs = get_valid_cert_chain();
1✔
801

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

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

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

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

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

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

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

855
         return results;
1✔
856
      }
7✔
857

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

863
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
864

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

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

870
         result.test_eq("unexpected path validation result",
3✔
871
                        path_result.result_string(),
2✔
872
                        to_string(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST));
873

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

877
         result.test_ne("unexpected check_chain result",
3✔
878
                        Botan::Path_Validation_Result(check_chain_result, {}).result_string(),
2✔
879
                        to_string(Botan::Certificate_Status_Code::OK));
880

881
         return {result};
3✔
882
      }
3✔
883

884
      Test::Result stand_alone_root_test(std::string test_name,
6✔
885
                                         const Botan::Path_Validation_Restrictions& restrictions,
886
                                         const Botan::X509_Certificate& standalone_cert,
887
                                         Botan::Certificate_Status_Code expected_result) {
888
         Test::Result result(std::move(test_name));
6✔
889

890
         const auto validation_time = get_validation_time();
6✔
891
         Botan::Certificate_Store_In_Memory cert_store(standalone_cert);
6✔
892

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

896
         result.test_eq(
18✔
897
            "unexpected x509_path_validate result", path_result.result_string(), to_string(expected_result));
12✔
898

899
         return result;
6✔
900
      }
6✔
901

902
      std::vector<Test::Result> stand_alone_root_tests() {
1✔
903
         const auto self_signed_trust_anchor_forbidden = get_default_restrictions(false, false);
1✔
904
         const auto self_signed_trust_anchor_allowed = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
905

906
         const auto cert_chain = get_valid_cert_chain();
1✔
907
         const auto& self_signed_root = cert_chain.at(4);
1✔
908
         const auto& ica = cert_chain.at(3);
1✔
909
         const auto& self_signed_ee = get_self_signed_ee_cert();
1✔
910

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

921
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors forbidden)",
1✔
922
                                  self_signed_trust_anchor_forbidden,
923
                                  self_signed_ee,
924
                                  Botan::Certificate_Status_Code::OK),
925
            stand_alone_root_test("Standalone self-signed end entity (non-self-signed trust anchors allowed)",
1✔
926
                                  self_signed_trust_anchor_allowed,
927
                                  self_signed_ee,
928
                                  Botan::Certificate_Status_Code::OK),
929

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

941
   public:
942
      std::vector<Test::Result> run() override {
1✔
943
         std::vector<Test::Result> results;
1✔
944

945
         auto res = path_validate_test();
1✔
946
         results.insert(results.end(), res.begin(), res.end());
1✔
947

948
         res = build_path_test();
1✔
949
         results.insert(results.end(), res.begin(), res.end());
1✔
950

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

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

957
         return results;
1✔
958
      }
1✔
959
};
960

961
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
962

963
class BSI_Path_Validation_Tests final : public Test
×
964

965
{
966
   public:
967
      std::vector<Test::Result> run() override;
968

969
   private:
970
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
971
};
972

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

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

988
   std::vector<Test::Result> results;
2✔
989

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

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

996
      Botan::Certificate_Store_In_Memory trusted;
108✔
997
      std::vector<Botan::X509_Certificate> certs;
108✔
998

999
      #if defined(BOTAN_HAS_MD5)
1000
      const bool has_md5 = true;
108✔
1001
      #else
1002
      const bool has_md5 = false;
1003
      #endif
1004

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

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

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

1032
         Botan::Path_Validation_Restrictions restrictions(use_crl,
100✔
1033
                                                          restriction_template.minimum_key_strength(),
1034
                                                          use_crl,
1035
                                                          restriction_template.max_ocsp_age(),
1036
                                                          std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
1037
                                                          restriction_template.ignore_trusted_root_time_range(),
100✔
1038
                                                          restriction_template.require_self_signed_trust_anchors());
200✔
1039

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

1052
               static constexpr result_type min() { return 0; }
1053

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

1056
               result_type operator()() {
160✔
1057
                  size_t s = 0;
160✔
1058
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
160✔
1059
                  return s;
160✔
1060
               }
1061

1062
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
1063

1064
            private:
1065
               Botan::RandomNumberGenerator& m_rng;
1066
         } rbg(this->rng());
100✔
1067

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

1071
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,600✔
1072
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,600✔
1073

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

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

1114
      result.end_timer();
108✔
1115
      results.push_back(result);
108✔
1116
   }
108✔
1117

1118
   return results;
2✔
1119
}
2✔
1120

1121
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1122

1123
class Path_Validation_With_OCSP_Tests final : public Test {
×
1124
   public:
1125
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
1126
         return Botan::X509_Certificate(Test::data_file(path));
48✔
1127
      }
1128

1129
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
1130
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
1131
      }
1132

1133
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
1134
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
1135
         Botan::Certificate_Store_In_Memory trusted;
1✔
1136

1137
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1138

1139
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1140
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1141
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1142
         trusted.add_certificate(trust_root);
1✔
1143

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

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

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

1159
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1160
                                     Botan::to_string(path_result.result()) + "'",
8✔
1161
                                  path_result.result() == expected);
8✔
1162
         };
8✔
1163

1164
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1165
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1166
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1167
                    Botan::Certificate_Status_Code::OK);
1168
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1169
                    Botan::Certificate_Status_Code::OK);
1170
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1171
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1172

1173
         return result;
2✔
1174
      }
2✔
1175

1176
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1177
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1178
         Botan::Certificate_Store_In_Memory trusted;
1✔
1179

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

1182
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1183
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1184
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1185
         trusted.add_certificate(trust_root);
1✔
1186

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

1189
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1190

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

1202
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1203
                                     Botan::to_string(path_result.result()) + "'",
8✔
1204
                                  path_result.result() == expected);
8✔
1205
         };
12✔
1206

1207
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1208
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1209
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1210
                    Botan::Certificate_Status_Code::OK);
1211
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1212
                    Botan::Certificate_Status_Code::OK);
1213
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1214
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1215

1216
         return result;
2✔
1217
      }
2✔
1218

1219
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
1220
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
1221
         Botan::Certificate_Store_In_Memory trusted;
1✔
1222

1223
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1224

1225
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1226
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1227
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1228

1229
         trusted.add_certificate(trust_root);
1✔
1230

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

1233
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1234

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

1246
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1247
                                     Botan::to_string(path_result.result()) + "'",
6✔
1248
                                  path_result.result() == expected);
6✔
1249
         };
9✔
1250

1251
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1252
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1253
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1254
                    Botan::Certificate_Status_Code::OK);
1255
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
1256

1257
         return result;
2✔
1258
      }
2✔
1259

1260
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
1261
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
1262
         Botan::Certificate_Store_In_Memory trusted;
1✔
1263

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

1266
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1267
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1268
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1269

1270
         trusted.add_certificate(trust_root);
1✔
1271

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

1274
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1275

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

1287
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1288
                                     Botan::to_string(path_result.result()) + "'",
6✔
1289
                                  path_result.result() == expected);
6✔
1290
         };
9✔
1291

1292
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1293
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1294
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1295
                    Botan::Certificate_Status_Code::OK);
1296
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1297
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1298

1299
         return result;
2✔
1300
      }
2✔
1301

1302
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1303
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1304
         Botan::Certificate_Store_In_Memory trusted;
1✔
1305

1306
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1307
                                                                 110,    // minimum key strength
1308
                                                                 true);  // OCSP for all intermediates
2✔
1309

1310
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1311
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1312
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1313

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

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

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

1335
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1336
                                     Botan::to_string(path_result.result()) + "'",
6✔
1337
                                  path_result.result() == expected);
6✔
1338
         };
12✔
1339

1340
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1341
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1342
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1343
                    Botan::Certificate_Status_Code::OK);
1344
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1345
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1346

1347
         return result;
1✔
1348
      }
4✔
1349

1350
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1351
         Test::Result result(
1✔
1352
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1353
         Botan::Certificate_Store_In_Memory trusted;
1✔
1354

1355
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1356
                                                                 110,     // minimum key strength
1357
                                                                 false);  // OCSP for all intermediates
2✔
1358

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

1364
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1365
         auto ocsp_ee_delegate_malformed =
1✔
1366
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1367

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

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

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

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

1405
         return result;
1✔
1406
      }
2✔
1407

1408
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1409
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1410
         Botan::Certificate_Store_In_Memory trusted;
1✔
1411

1412
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1413
                                                                 110,     // minimum key strength
1414
                                                                 false);  // OCSP for all intermediates
2✔
1415

1416
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1417
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1418
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1419
         trusted.add_certificate(trust_root);
1✔
1420

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

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

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

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

1454
         return result;
1✔
1455
      }
2✔
1456

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

1462
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1463
                                                                 110,    // minimum key strength
1464
                                                                 true);  // OCSP for all intermediates
2✔
1465

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

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

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

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

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

1493
         return result;
1✔
1494
      }
7✔
1495

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

1508
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1509

1510
   #endif
1511

1512
   #if defined(BOTAN_HAS_ECDSA)
1513

1514
class CVE_2020_0601_Tests final : public Test {
×
1515
   public:
1516
      std::vector<Test::Result> run() override {
1✔
1517
         Test::Result result("CVE-2020-0601");
1✔
1518

1519
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1520
            result.test_note("Skipping as application specific groups are not supported");
×
1521
            return {result};
×
1522
         }
1523

1524
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1525
            result.test_note("Skipping as secp384r1 is not supported");
×
1526
            return {result};
×
1527
         }
1528

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

1542
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1543
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1544
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1545

1546
         Botan::Certificate_Store_In_Memory trusted;
1✔
1547
         trusted.add_certificate(ca_crt);
1✔
1548

1549
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1550

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

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

1562
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1563

1564
         result.confirm("Expected status",
2✔
1565
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1566

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

1576
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1577

1578
         result.confirm("Expected status",
2✔
1579
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1580

1581
         // Verify the signature from the bad CA is actually correct
1582
         Botan::Certificate_Store_In_Memory frusted;
1✔
1583
         frusted.add_certificate(fake_ca_crt);
1✔
1584

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

1594
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1595

1596
         return {result};
2✔
1597
      }
5✔
1598
};
1599

1600
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1601

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

1610
         if(Botan::has_filesystem_impl() == false) {
1✔
1611
            result.test_note("Skipping due to missing filesystem access");
×
1612
            return {result};
×
1613
         }
1614

1615
         Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1616
         Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1617
         Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1618

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

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

1631
         Botan::Certificate_Store_In_Memory trusted;
1✔
1632
         trusted.add_certificate(root);
1✔
1633
         trusted.add_crl(crl);
1✔
1634

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

1639
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1640

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

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

1656
         return {result};
2✔
1657
      }
2✔
1658
};
1659

1660
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1661

1662
   #endif
1663

1664
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1665

1666
class XMSS_Path_Validation_Tests final : public Test {
×
1667
   public:
1668
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1669
         Test::Result result(name);
2✔
1670

1671
         Botan::Path_Validation_Restrictions restrictions;
4✔
1672
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1673

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

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

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

1688
         return {
1✔
1689
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1690
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1691
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1692
      }
4✔
1693
};
1694

1695
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1696

1697
   #endif
1698

1699
#endif
1700

1701
}  // namespace
1702

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