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

randombit / botan / 16780600750

06 Aug 2025 02:57PM UTC coverage: 90.704% (+0.03%) from 90.677%
16780600750

Pull #5047

github

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

100145 of 110409 relevant lines covered (90.7%)

12253687.17 hits per line

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

93.63
/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) {
79✔
72
   Botan::DataSource_Stream in(filename);
79✔
73

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

81
   return certs;
79✔
82
}
79✔
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) {
4✔
95
   return Botan::Path_Validation_Restrictions(req_revocation_info, 80 /*some tests use SHA-1*/, ocsp_all_intermediates);
8✔
96
}
97

98
Botan::Path_Validation_Restrictions get_allow_non_self_signed_anchors_restrictions(bool req_revocation_info,
5✔
99
                                                                                   bool ocsp_all_intermediates) {
100
   return Botan::Path_Validation_Restrictions(req_revocation_info,
5✔
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>(),
5✔
105
                                              false,
106
                                              true);
10✔
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 {
1✔
211
         std::vector<Test::Result> results;
1✔
212
         for(const auto& restrictions : restrictions_to_test(true, true)) {
3✔
213
            auto partial_res = run_with_restrictions(restrictions);
2✔
214
            results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
215
         }
3✔
216
         return results;
1✔
217
      }
×
218

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

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

229
   std::vector<Test::Result> results;
2✔
230

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

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

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

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

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

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

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

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

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

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

282
      result.end_timer();
152✔
283
      results.push_back(result);
152✔
284
   }
152✔
285

286
   return results;
2✔
287
}
154✔
288

289
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
290

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

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

301
   std::vector<Test::Result> results;
1✔
302

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

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

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

311
      Botan::Certificate_Store_In_Memory store;
3✔
312

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

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

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

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

327
      result.end_timer();
3✔
328
      results.push_back(result);
3✔
329
   }
3✔
330

331
   return results;
1✔
332
}
1✔
333

334
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
335

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

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

346
   std::vector<Test::Result> results;
1✔
347

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

350
   auto validation_times_iter = validation_times.begin();
1✔
351

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

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

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

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

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

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

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

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

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

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

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

414
      result.end_timer();
118✔
415
      results.push_back(result);
118✔
416
   }
334✔
417

418
   return results;
1✔
419
}
19✔
420

421
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
422

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

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

433
   std::vector<Test::Result> results;
1✔
434

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

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

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

445
   Botan::Certificate_Store_In_Memory trusted;
1✔
446
   trusted.add_certificate(root);
1✔
447

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

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

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

457
   Botan::Certificate_Store_In_Memory empty;
1✔
458

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

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

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

466
   return {result};
2✔
467
}
4✔
468

469
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
470

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

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

481
   std::vector<Test::Result> results;
1✔
482

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

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

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

493
   Botan::Certificate_Store_In_Memory trusted;
1✔
494
   trusted.add_certificate(root);
1✔
495

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

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

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

507
   return {result};
2✔
508
}
3✔
509

510
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
511

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

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

522
   std::vector<Test::Result> results;
1✔
523

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

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

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

534
   Botan::Certificate_Store_In_Memory trusted;
1✔
535
   trusted.add_certificate(root);
1✔
536

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

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

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

548
   return {result};
2✔
549
}
3✔
550

551
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
552

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

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

563
   std::vector<Test::Result> results;
1✔
564

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

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

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

575
   Botan::Certificate_Store_In_Memory trusted;
1✔
576
   trusted.add_certificate(root);
1✔
577

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

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

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

587
   return {result};
2✔
588
}
3✔
589

590
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
591

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

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

602
   std::vector<Test::Result> results;
1✔
603

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

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

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

614
   Botan::Certificate_Store_In_Memory trusted;
1✔
615
   trusted.add_certificate(root);
1✔
616

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

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

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

626
   return {result};
2✔
627
}
3✔
628

629
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
630

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

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

641
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
642
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
643

644
         Botan::Certificate_Store_In_Memory trusted;
1✔
645
         trusted.add_certificate(trusted_root_cert);
1✔
646

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

649
         Test::Result result("Root cert time check");
1✔
650

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

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

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

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

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

727
         return {result};
2✔
728
      }
3✔
729
};
730

731
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
732

733
class Non_Self_Signed_Trust_Anchors_Test final : public Test {
×
734
   private:
735
      std::pair<std::vector<Botan::X509_Certificate>, std::chrono::system_clock::time_point>
736
      test_cert_chain_with_validation_time() {
3✔
737
         // Test certs generated by https://github.com/yymax/x509test
738
         const std::string valid_chain_pem = "x509/x509test/ValidChained.pem";
3✔
739
         auto certs = load_cert_file(Test::data_file(valid_chain_pem));
3✔
740
         if(certs.size() != 5) {
3✔
741
            throw Test_Error(Botan::fmt("Failed to read all certs from {}", valid_chain_pem));
×
742
         }
743

744
         auto validation_time = Botan::calendar_point(2016, 10, 21, 4, 20, 0).to_std_timepoint();
3✔
745
         return {std::move(certs), validation_time};
3✔
746
      }
3✔
747

748
   public:
749
      std::vector<Test::Result> path_validate_test() {
1✔
750
         std::vector<Test::Result> results;
1✔
751

752
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
753
         const auto [certs, validation_time] = test_cert_chain_with_validation_time();
1✔
754

755
         for(size_t trusted_cert_idx = 0; trusted_cert_idx < certs.size() - 1; ++trusted_cert_idx) {
5✔
756
            Test::Result result(
4✔
757
               Botan::fmt("Non-self-signed trust anchor with allow_non_self_signed_trust_anchors (chain length: {})",
4✔
758
                          trusted_cert_idx));
4✔
759

760
            const Botan::Certificate_Store_In_Memory trusted(certs[trusted_cert_idx]);
4✔
761

762
            std::vector<Botan::X509_Certificate> cert_chain(certs.begin(), certs.begin() + trusted_cert_idx);
4✔
763
            // Always add at least the leaf
764
            if(cert_chain.empty()) {
4✔
765
               cert_chain.push_back(certs.at(0));
1✔
766
            }
767

768
            Botan::Path_Validation_Result path_result = Botan::x509_path_validate(
4✔
769
               cert_chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
4✔
770

771
            if(path_result.successful_validation() && path_result.trust_root() != certs[trusted_cert_idx]) {
4✔
772
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
773
            }
774

775
            result.test_eq(
12✔
776
               "path validation failed", path_result.result_string(), to_string(Botan::Certificate_Status_Code::OK));
8✔
777

778
            results.push_back(result);
4✔
779
         }
4✔
780
         return results;
2✔
781
      }
1✔
782

783
      std::vector<Test::Result> build_path_test() {
1✔
784
         std::vector<Test::Result> results;
1✔
785

786
         const auto restrictions = get_allow_non_self_signed_anchors_restrictions(false, false);
1✔
787
         const auto certs = test_cert_chain_with_validation_time().first;
1✔
788

789
         using Cert_Path = std::vector<Botan::X509_Certificate>;
1✔
790

791
         // Helper to create a cert path {certs[0],...,certs[last_cert]}
792
         const auto path_to = [&](size_t last_cert) -> Cert_Path {
20✔
793
            return {certs.begin(), certs.begin() + last_cert + 1};
5✔
794
         };
1✔
795

796
         // Helper to create a cert store of all certificates in certs given by their indices
797
         const auto store_of = [&](auto... cert_indices) -> Botan::Certificate_Store_In_Memory {
6✔
798
            Botan::Certificate_Store_In_Memory cert_store;
5✔
799
            (cert_store.add_certificate(certs.at(cert_indices)), ...);
5✔
800
            return cert_store;
5✔
801
         };
1✔
802

803
         const std::vector<std::tuple<std::string, Botan::Certificate_Store_In_Memory, std::vector<Cert_Path>>>
1✔
804
            test_names_with_stores_with_expected_paths{
805
               {"root in store", store_of(4), std::vector<Cert_Path>{path_to(4)}},
3✔
806
               {"intermediate in store", store_of(2), std::vector<Cert_Path>{path_to(2)}},
2✔
807
               {"leaf in store", store_of(0), std::vector<Cert_Path>{path_to(0)}},
2✔
808
               {"leaf, intermediate, and root in store",
809
                store_of(0, 1, 4),
1✔
810
                std::vector<Cert_Path>{path_to(0), path_to(1), path_to(4)}},
4✔
811
               {"all in store",
812
                store_of(0, 1, 2, 3, 4),
1✔
813
                std::vector<Cert_Path>{path_to(0), path_to(1), path_to(2), path_to(3), path_to(4)}},
6✔
814
            };
6✔
815

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

819
            std::vector<Cert_Path> cert_paths;
5✔
820
            const auto build_all_res =
5✔
821
               Botan::PKIX::build_all_certificate_paths(cert_paths, {&cert_store}, certs.at(0), certs);
5✔
822
            result.test_is_eq("build_all_certificate_paths result",
5✔
823
                              to_string(build_all_res),
5✔
824
                              to_string(Botan::Certificate_Status_Code::OK));
5✔
825
            result.test_is_eq("build_all_certificate_paths paths", cert_paths, expected_paths);
5✔
826

827
            Cert_Path cert_path;
5✔
828
            const auto build_path_res =
5✔
829
               Botan::PKIX::build_certificate_path(cert_path, {&cert_store}, certs.at(0), certs);
5✔
830
            result.test_is_eq("build_all_certificate_paths result",
5✔
831
                              to_string(build_path_res),
5✔
832
                              to_string(Botan::Certificate_Status_Code::OK));
5✔
833

834
            if(std::ranges::find(cert_paths, path_to(4)) != cert_paths.end()) {
10✔
835
               result.test_is_eq("build_certificate_path (with self-signed anchor)", cert_path, path_to(4));
6✔
836
            } else {
837
               result.test_is_eq(
4✔
838
                  "build_certificate_path (without self-signed anchor)", cert_path, expected_paths.at(0));
2✔
839
            }
840
            results.push_back(result);
5✔
841
         }
5✔
842

843
         return results;
1✔
844
      }
7✔
845

846
      std::vector<Test::Result> forbidden_self_signed_trust_anchors_test() {
1✔
847
         auto restrictions = get_default_restrictions(false, false);  // non-self-signed anchors are forbidden
1✔
848
         auto [certs, validation_time] = test_cert_chain_with_validation_time();
1✔
849

850
         Botan::Certificate_Store_In_Memory cert_store(certs.at(3));
1✔
851

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

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

857
         result.test_eq("unexpected path validation result",
3✔
858
                        path_result.result_string(),
2✔
859
                        to_string(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST));
860

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

864
         result.test_ne("unexpected check_chain result",
3✔
865
                        Botan::Path_Validation_Result(check_chain_result, {}).result_string(),
2✔
866
                        to_string(Botan::Certificate_Status_Code::OK));
867

868
         return {result};
3✔
869
      }
3✔
870

871
      std::vector<Test::Result> run() override {
1✔
872
         std::vector<Test::Result> results;
1✔
873

874
         auto res = path_validate_test();
1✔
875
         results.insert(results.end(), res.begin(), res.end());
1✔
876

877
         res = build_path_test();
1✔
878
         results.insert(results.end(), res.begin(), res.end());
1✔
879

880
         res = forbidden_self_signed_trust_anchors_test();
1✔
881
         results.insert(results.end(), res.begin(), res.end());
1✔
882

883
         return results;
1✔
884
      }
1✔
885
};
886

887
BOTAN_REGISTER_TEST("x509", "x509_non_self_signed_trust_anchors", Non_Self_Signed_Trust_Anchors_Test);
888

889
class BSI_Path_Validation_Tests final : public Test
×
890

891
{
892
   public:
893
      std::vector<Test::Result> run() override {
1✔
894
         std::vector<Test::Result> results;
1✔
895
         for(const auto& restrictions : restrictions_to_test(false, false)) {
3✔
896
            auto partial_res = run_with_restrictions(restrictions);
2✔
897
            results.insert(results.end(), partial_res.begin(), partial_res.end());
2✔
898
         }
3✔
899
         return results;
1✔
900
      }
×
901

902
   private:
903
      std::vector<Test::Result> run_with_restrictions(const Botan::Path_Validation_Restrictions& restrictions);
904
};
905

906
std::vector<Test::Result> BSI_Path_Validation_Tests::run_with_restrictions(
2✔
907
   const Botan::Path_Validation_Restrictions& restriction_template) {
908
   if(Botan::has_filesystem_impl() == false) {
2✔
909
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
910
   }
911

912
   std::vector<Test::Result> results;
2✔
913

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

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

920
      Botan::Certificate_Store_In_Memory trusted;
108✔
921
      std::vector<Botan::X509_Certificate> certs;
108✔
922

923
      #if defined(BOTAN_HAS_MD5)
924
      const bool has_md5 = true;
108✔
925
      #else
926
      const bool has_md5 = false;
927
      #endif
928

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

931
      // By convention: if CRL is a substring if the test name,
932
      // we need to check the CRLs
933
      bool use_crl = false;
108✔
934
      if(test_name.find("CRL") != std::string::npos) {
108✔
935
         use_crl = true;
32✔
936
      }
937

938
      try {
108✔
939
         for(const auto& file : all_files) {
890✔
940
            // found a trust anchor
941
            if(file.find("TA") != std::string::npos) {
790✔
942
               trusted.add_certificate(Botan::X509_Certificate(file));
100✔
943
            }
944
            // found the target certificate. It needs to be at the front of certs
945
            else if(file.find("TC") != std::string::npos) {
690✔
946
               certs.insert(certs.begin(), Botan::X509_Certificate(file));
108✔
947
            }
948
            // found a certificate that might be part of a valid certificate chain to the trust anchor
949
            else if(file.find(".crt") != std::string::npos) {
582✔
950
               certs.push_back(Botan::X509_Certificate(file));
224✔
951
            } else if(file.find(".crl") != std::string::npos) {
470✔
952
               trusted.add_crl(Botan::X509_CRL(file));
56✔
953
            }
954
         }
955

956
         Botan::Path_Validation_Restrictions restrictions(use_crl,
100✔
957
                                                          restriction_template.minimum_key_strength(),
958
                                                          use_crl,
959
                                                          restriction_template.max_ocsp_age(),
960
                                                          std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
961
                                                          restriction_template.ignore_trusted_root_time_range(),
100✔
962
                                                          restriction_template.allow_non_self_signed_trust_anchors());
200✔
963

964
         /*
965
          * Following the test document, the test are executed 16 times with
966
          * randomly chosen order of the available certificates. However, the target
967
          * certificate needs to stay in front.
968
          * For certain test, the order in which the certificates are given to
969
          * the validation function may be relevant, i.e. if issuer DNs are
970
          * ambiguous.
971
          */
972
         class random_bit_generator {
100✔
973
            public:
974
               using result_type = size_t;
975

976
               static constexpr result_type min() { return 0; }
977

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

980
               result_type operator()() {
160✔
981
                  size_t s = 0;
160✔
982
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
160✔
983
                  return s;
160✔
984
               }
985

986
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
100✔
987

988
            private:
989
               Botan::RandomNumberGenerator& m_rng;
990
         } rbg(this->rng());
100✔
991

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

995
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
1,600✔
996
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1,600✔
997

998
            // We expect to be warned
999
            if(expected_result.starts_with("Warning: ")) {
1,600✔
1000
               std::string stripped = expected_result.substr(std::string("Warning: ").size());
64✔
1001
               bool found_warning = false;
64✔
1002
               for(const auto& warning_set : validation_result.warnings()) {
256✔
1003
                  for(const auto& warning : warning_set) {
256✔
1004
                     std::string warning_str(Botan::to_string(warning));
64✔
1005
                     if(stripped == warning_str) {
64✔
1006
                        result.test_eq(test_name + " path validation result", warning_str, stripped);
64✔
1007
                        found_warning = true;
64✔
1008
                     }
1009
                  }
64✔
1010
               }
64✔
1011
               if(!found_warning) {
64✔
1012
                  result.test_failure(test_name, "Did not receive the expected warning: " + stripped);
×
1013
               }
1014
            } else {
64✔
1015
               if(expected_result == "Hash function used is considered too weak for security" && has_md5 == false) {
1,536✔
1016
                  result.test_eq(test_name + " path validation result",
1017
                                 validation_result.result_string(),
1018
                                 "Certificate signed with unknown/unavailable algorithm");
1019
               } else {
1020
                  result.test_eq(
1,536✔
1021
                     test_name + " path validation result", validation_result.result_string(), expected_result);
3,072✔
1022
               }
1023
            }
1024
         }
1,600✔
1025
      }
100✔
1026

1027
      /* Some certificates are rejected when executing the X509_Certificate constructor
1028
       * by throwing a Decoding_Error exception.
1029
       */
1030
      catch(const Botan::Exception& e) {
8✔
1031
         if(e.error_type() == Botan::ErrorType::DecodingFailure) {
8✔
1032
            result.test_eq(test_name + " path validation result", e.what(), expected_result);
16✔
1033
         } else {
1034
            result.test_failure(test_name, e.what());
×
1035
         }
1036
      }
8✔
1037

1038
      result.end_timer();
108✔
1039
      results.push_back(result);
108✔
1040
   }
108✔
1041

1042
   return results;
2✔
1043
}
2✔
1044

1045
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
1046

1047
class Path_Validation_With_OCSP_Tests final : public Test {
×
1048
   public:
1049
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
1050
         return Botan::X509_Certificate(Test::data_file(path));
48✔
1051
      }
1052

1053
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
1054
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
1055
      }
1056

1057
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
1058
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
1059
         Botan::Certificate_Store_In_Memory trusted;
1✔
1060

1061
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
1062

1063
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1064
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1065
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1066
         trusted.add_certificate(trust_root);
1✔
1067

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

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

1072
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1073
                               const Botan::Certificate_Status_Code expected) {
1074
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
1075
                                                               restrictions,
1076
                                                               trusted,
4✔
1077
                                                               "",
1078
                                                               Botan::Usage_Type::UNSPECIFIED,
1079
                                                               valid_time,
1080
                                                               std::chrono::milliseconds(0),
4✔
1081
                                                               {ocsp});
8✔
1082

1083
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1084
                                     Botan::to_string(path_result.result()) + "'",
8✔
1085
                                  path_result.result() == expected);
8✔
1086
         };
8✔
1087

1088
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1089
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1090
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1091
                    Botan::Certificate_Status_Code::OK);
1092
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1093
                    Botan::Certificate_Status_Code::OK);
1094
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1095
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1096

1097
         return result;
2✔
1098
      }
2✔
1099

1100
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
1101
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
1102
         Botan::Certificate_Store_In_Memory trusted;
1✔
1103

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

1106
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1107
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1108
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1109
         trusted.add_certificate(trust_root);
1✔
1110

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

1113
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
1114

1115
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
1116
                               const Botan::Certificate_Status_Code expected) {
1117
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
1118
                                                               restrictions,
1119
                                                               trusted,
4✔
1120
                                                               "",
1121
                                                               Botan::Usage_Type::UNSPECIFIED,
1122
                                                               valid_time,
1123
                                                               std::chrono::milliseconds(0),
4✔
1124
                                                               {ocsp});
8✔
1125

1126
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
1127
                                     Botan::to_string(path_result.result()) + "'",
8✔
1128
                                  path_result.result() == expected);
8✔
1129
         };
12✔
1130

1131
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
1132
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1133
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
1134
                    Botan::Certificate_Status_Code::OK);
1135
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
1136
                    Botan::Certificate_Status_Code::OK);
1137
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
1138
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1139

1140
         return result;
2✔
1141
      }
2✔
1142

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

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

1149
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1150
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1151
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1152

1153
         trusted.add_certificate(trust_root);
1✔
1154

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

1157
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
1158

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

1170
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1171
                                     Botan::to_string(path_result.result()) + "'",
6✔
1172
                                  path_result.result() == expected);
6✔
1173
         };
9✔
1174

1175
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1176
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1177
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1178
                    Botan::Certificate_Status_Code::OK);
1179
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
1180

1181
         return result;
2✔
1182
      }
2✔
1183

1184
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
1185
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
1186
         Botan::Certificate_Store_In_Memory trusted;
1✔
1187

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

1190
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
1191
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
1192
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
1193

1194
         trusted.add_certificate(trust_root);
1✔
1195

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

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

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

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

1216
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1217
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1218
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1219
                    Botan::Certificate_Status_Code::OK);
1220
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1221
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1222

1223
         return result;
2✔
1224
      }
2✔
1225

1226
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1227
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1228
         Botan::Certificate_Store_In_Memory trusted;
1✔
1229

1230
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1231
                                                                 110,    // minimum key strength
1232
                                                                 true);  // OCSP for all intermediates
2✔
1233

1234
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1235
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1236
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1237

1238
         // These OCSP responses are signed by an authorized OCSP responder
1239
         // certificate issued by `ca` and `trust_root` respectively. Note that
1240
         // the responder certificates contain the "OCSP No Check" extension,
1241
         // meaning that they themselves do not need a revocation check via OCSP.
1242
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/bdr-ocsp-resp.der");
1✔
1243
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/bdr-int-ocsp-resp.der");
1✔
1244

1245
         trusted.add_certificate(trust_root);
1✔
1246
         const std::vector<Botan::X509_Certificate> cert_path = {ee, ca, trust_root};
4✔
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,
15✔
1251
                                                               restrictions,
1252
                                                               trusted,
3✔
1253
                                                               "",
1254
                                                               Botan::Usage_Type::UNSPECIFIED,
1255
                                                               valid_time,
1256
                                                               std::chrono::milliseconds(0),
3✔
1257
                                                               {ocsp_ee, ocsp_ca});
9✔
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
         };
12✔
1263

1264
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1265
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1266
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1267
                    Botan::Certificate_Status_Code::OK);
1268
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1269
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1270

1271
         return result;
1✔
1272
      }
4✔
1273

1274
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1275
         Test::Result result(
1✔
1276
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1277
         Botan::Certificate_Store_In_Memory trusted;
1✔
1278

1279
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1280
                                                                 110,     // minimum key strength
1281
                                                                 false);  // OCSP for all intermediates
2✔
1282

1283
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1284
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1285
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1286
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1287

1288
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1289
         auto ocsp_ee_delegate_malformed =
1✔
1290
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1291

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

1295
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1296
                               const Botan::OCSP::Response& ocsp_ee,
1297
                               const Botan::Certificate_Status_Code expected,
1298
                               const std::optional<Botan::Certificate_Status_Code> also_expected = std::nullopt) {
1299
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1300
                                                               restrictions,
1301
                                                               trusted,
3✔
1302
                                                               "",
1303
                                                               Botan::Usage_Type::UNSPECIFIED,
1304
                                                               valid_time,
1305
                                                               std::chrono::milliseconds(0),
3✔
1306
                                                               {ocsp_ee});
6✔
1307

1308
            result.test_is_eq("should result in expected validation status code",
3✔
1309
                              static_cast<uint32_t>(path_result.result()),
3✔
1310
                              static_cast<uint32_t>(expected));
3✔
1311
            if(also_expected) {
3✔
1312
               result.confirm("Secondary error is also present",
4✔
1313
                              flatten(path_result.all_statuses()).contains(also_expected.value()));
6✔
1314
            }
1315
         };
6✔
1316

1317
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1318
                    ocsp_ee_delegate,
1319
                    Botan::Certificate_Status_Code::VERIFIED);
1320
         check_path(Botan::calendar_point(2022, 10, 8, 23, 30, 0).to_std_timepoint(),
1✔
1321
                    ocsp_ee_delegate,
1322
                    Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
1323
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1324
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1325
                    ocsp_ee_delegate_malformed,
1326
                    Botan::Certificate_Status_Code::OCSP_RESPONSE_MISSING_KEYUSAGE,
1327
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1328

1329
         return result;
1✔
1330
      }
2✔
1331

1332
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1333
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1334
         Botan::Certificate_Store_In_Memory trusted;
1✔
1335

1336
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1337
                                                                 110,     // minimum key strength
1338
                                                                 false);  // OCSP for all intermediates
2✔
1339

1340
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1341
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1342
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1343
         trusted.add_certificate(trust_root);
1✔
1344

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

1347
         auto check_path = [&](const std::string& forged_ocsp,
3✔
1348
                               const Botan::Certificate_Status_Code expected,
1349
                               const Botan::Certificate_Status_Code also_expected) {
1350
            auto ocsp = load_test_OCSP_resp(forged_ocsp);
2✔
1351
            const auto path_result =
2✔
1352
               Botan::x509_path_validate(cert_path,
6✔
1353
                                         restrictions,
1354
                                         trusted,
2✔
1355
                                         "",
1356
                                         Botan::Usage_Type::UNSPECIFIED,
1357
                                         Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
4✔
1358
                                         std::chrono::milliseconds(0),
2✔
1359
                                         {ocsp});
4✔
1360

1361
            result.test_is_eq(
2✔
1362
               "Path validation with forged OCSP response should fail with", path_result.result(), expected);
2✔
1363
            result.confirm("Secondary error is also present",
4✔
1364
                           flatten(path_result.all_statuses()).contains(also_expected));
4✔
1365
            result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
6✔
1366
         };
8✔
1367

1368
         // In both cases the path validation should detect the forged OCSP
1369
         // response and generate an appropriate error. By no means it should
1370
         // follow the unauthentic OCSP response.
1371
         check_path("x509/ocsp/randombit_ocsp_forged_valid.der",
1✔
1372
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1373
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1374
         check_path("x509/ocsp/randombit_ocsp_forged_revoked.der",
1✔
1375
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1376
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1377

1378
         return result;
1✔
1379
      }
2✔
1380

1381
      static Test::Result validate_with_ocsp_self_signed_by_intermediate_cert() {
1✔
1382
         Test::Result result(
1✔
1383
            "path check with ocsp response for intermediate that is (maliciously) self-signed by the intermediate");
1✔
1384
         Botan::Certificate_Store_In_Memory trusted;
1✔
1385

1386
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1387
                                                                 110,    // minimum key strength
1388
                                                                 true);  // OCSP for all intermediates
2✔
1389

1390
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1391
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1392
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1393
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1394

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

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

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

1404
         const auto path_result =
1✔
1405
            Botan::x509_path_validate(cert_path,
5✔
1406
                                      restrictions,
1407
                                      trusted,
1408
                                      "",
1409
                                      Botan::Usage_Type::UNSPECIFIED,
1410
                                      Botan::calendar_point(2022, 9, 22, 22, 30, 0).to_std_timepoint(),
2✔
1411
                                      std::chrono::milliseconds(0),
1✔
1412
                                      {ocsp_ee, ocsp_ca});
3✔
1413
         result.confirm("should reject intermediate OCSP response",
2✔
1414
                        path_result.result() == Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_FOUND);
1✔
1415
         result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
3✔
1416

1417
         return result;
1✔
1418
      }
7✔
1419

1420
      std::vector<Test::Result> run() override {
1✔
1421
         return {validate_with_ocsp_with_next_update_without_max_age(),
1✔
1422
                 validate_with_ocsp_with_next_update_with_max_age(),
1423
                 validate_with_ocsp_without_next_update_without_max_age(),
1424
                 validate_with_ocsp_without_next_update_with_max_age(),
1425
                 validate_with_ocsp_with_authorized_responder(),
1426
                 validate_with_ocsp_with_authorized_responder_without_keyusage(),
1427
                 validate_with_forged_ocsp_using_self_signed_cert(),
1428
                 validate_with_ocsp_self_signed_by_intermediate_cert()};
9✔
1429
      }
1✔
1430
};
1431

1432
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1433

1434
   #endif
1435

1436
   #if defined(BOTAN_HAS_ECDSA)
1437

1438
class CVE_2020_0601_Tests final : public Test {
×
1439
   public:
1440
      std::vector<Test::Result> run() override {
1✔
1441
         Test::Result result("CVE-2020-0601");
1✔
1442

1443
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1444
            result.test_note("Skipping as application specific groups are not supported");
×
1445
            return {result};
×
1446
         }
1447

1448
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1449
            result.test_note("Skipping as secp384r1 is not supported");
×
1450
            return {result};
×
1451
         }
1452

1453
         const auto& secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
1454
         Botan::OID curveball_oid("1.3.6.1.4.1.25258.4.2020.0601");
1✔
1455
         Botan::EC_Group curveball(
1✔
1456
            curveball_oid,
1457
            secp384r1.get_p(),
1458
            secp384r1.get_a(),
1459
            secp384r1.get_b(),
1460
            BigInt(
2✔
1461
               "0xC711162A761D568EBEB96265D4C3CEB4F0C330EC8F6DD76E39BCC849ABABB8E34378D581065DEFC77D9FCED6B39075DE"),
1462
            BigInt(
2✔
1463
               "0x0CB090DE23BAC8D13E67E019A91B86311E5F342DEE17FD15FB7E278A32A1EAC98FC97E18CB2F3B2C487A7DA6F40107AC"),
1464
            secp384r1.get_order());
2✔
1465

1466
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1467
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1468
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1469

1470
         Botan::Certificate_Store_In_Memory trusted;
1✔
1471
         trusted.add_certificate(ca_crt);
1✔
1472

1473
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1474

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

1477
         const auto path_result1 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt, fake_ca_crt},
5✔
1478
                                                             restrictions,
1479
                                                             trusted,
1480
                                                             "",
1481
                                                             Botan::Usage_Type::UNSPECIFIED,
1482
                                                             valid_time,
1483
                                                             std::chrono::milliseconds(0),
1✔
1484
                                                             {});
2✔
1485

1486
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1487

1488
         result.confirm("Expected status",
2✔
1489
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1490

1491
         const auto path_result2 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1492
                                                             restrictions,
1493
                                                             trusted,
1494
                                                             "",
1495
                                                             Botan::Usage_Type::UNSPECIFIED,
1496
                                                             valid_time,
1497
                                                             std::chrono::milliseconds(0),
1✔
1498
                                                             {});
2✔
1499

1500
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1501

1502
         result.confirm("Expected status",
2✔
1503
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1504

1505
         // Verify the signature from the bad CA is actually correct
1506
         Botan::Certificate_Store_In_Memory frusted;
1✔
1507
         frusted.add_certificate(fake_ca_crt);
1✔
1508

1509
         const auto path_result3 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1510
                                                             restrictions,
1511
                                                             frusted,
1512
                                                             "",
1513
                                                             Botan::Usage_Type::UNSPECIFIED,
1514
                                                             valid_time,
1515
                                                             std::chrono::milliseconds(0),
1✔
1516
                                                             {});
2✔
1517

1518
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1519

1520
         return {result};
2✔
1521
      }
5✔
1522
};
1523

1524
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1525

1526
class Path_Validation_With_Immortal_CRL final : public Test {
×
1527
   public:
1528
      std::vector<Test::Result> run() override {
1✔
1529
         // RFC 5280 defines the nextUpdate field as "optional" (in line with
1530
         // the original X.509 standard), but then requires all conforming CAs
1531
         // to always define it. For best compatibility we must deal with both.
1532
         Test::Result result("Using a CRL without a nextUpdate field");
1✔
1533

1534
         if(Botan::has_filesystem_impl() == false) {
1✔
1535
            result.test_note("Skipping due to missing filesystem access");
×
1536
            return {result};
×
1537
         }
1538

1539
         Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1540
         Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1541
         Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1542

1543
         // Check that a CRL without nextUpdate is parsable
1544
         auto crl = Botan::X509_CRL(Test::data_file("x509/misc/crl_without_nextupdate/valid_forever.crl"));
2✔
1545
         result.confirm("this update is set", crl.this_update().time_is_set());
2✔
1546
         result.confirm("next update is not set", !crl.next_update().time_is_set());
2✔
1547
         result.confirm("CRL is not empty", !crl.get_revoked().empty());
2✔
1548

1549
         // Ensure that we support the used sig algo, otherwish stop here
1550
         if(!Botan::EC_Group::supports_named_group("brainpool512r1")) {
1✔
1551
            result.test_note("Cannot test path validation because signature algorithm is not support in this build");
×
1552
            return {result};
×
1553
         }
1554

1555
         Botan::Certificate_Store_In_Memory trusted;
1✔
1556
         trusted.add_certificate(root);
1✔
1557
         trusted.add_crl(crl);
1✔
1558

1559
         // Just before the CA and subject certificates expire
1560
         // (validity from 01 March 2025 to 24 Feburary 2026)
1561
         auto valid_time = Botan::calendar_point(2026, 2, 23, 0, 0, 0).to_std_timepoint();
1✔
1562

1563
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1564

1565
         // Validate a certificate that is not listed in the CRL
1566
         const auto valid = Botan::x509_path_validate(
1✔
1567
            valid_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1568
         if(!result.confirm("Valid certificate", valid.successful_validation())) {
2✔
1569
            result.test_note(valid.result_string());
×
1570
         }
1571

1572
         // Ensure that a certificate listed in the CRL is recognized as revoked
1573
         const auto revoked = Botan::x509_path_validate(
1✔
1574
            revoked_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1575
         if(!result.confirm("No valid certificate", !revoked.successful_validation())) {
2✔
1576
            result.test_note(revoked.result_string());
×
1577
         }
1578
         result.test_is_eq("Certificate is revoked", revoked.result(), Botan::Certificate_Status_Code::CERT_IS_REVOKED);
1✔
1579

1580
         return {result};
2✔
1581
      }
2✔
1582
};
1583

1584
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1585

1586
   #endif
1587

1588
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1589

1590
class XMSS_Path_Validation_Tests final : public Test {
×
1591
   public:
1592
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1593
         Test::Result result(name);
2✔
1594

1595
         Botan::Path_Validation_Restrictions restrictions;
4✔
1596
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1597

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

1601
         auto status = Botan::PKIX::overall_status(
2✔
1602
            Botan::PKIX::check_chain(cert_path, valid_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions));
4✔
1603
         result.test_eq("Cert validation status", Botan::to_string(status), "Verified");
2✔
1604
         return result;
2✔
1605
      }
4✔
1606

1607
      std::vector<Test::Result> run() override {
1✔
1608
         if(Botan::has_filesystem_impl() == false) {
1✔
1609
            return {Test::Result::Note("XMSS path validation", "Skipping due to missing filesystem access")};
×
1610
         }
1611

1612
         return {
1✔
1613
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1614
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1615
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1616
      }
4✔
1617
};
1618

1619
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1620

1621
   #endif
1622

1623
#endif
1624

1625
}  // namespace
1626

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