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

randombit / botan / 22018375255

14 Feb 2026 01:37PM UTC coverage: 90.069% (+0.007%) from 90.062%
22018375255

Pull #5330

github

web-flow
Merge d3b2de7c6 into fa5638d32
Pull Request #5330: Avoid including assert.h into tests.h

102246 of 113520 relevant lines covered (90.07%)

11438982.24 hits per line

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

95.76
/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/assert.h>
12
   #include <botan/data_src.h>
13
   #include <botan/exceptn.h>
14
   #include <botan/pk_keys.h>
15
   #include <botan/pkcs10.h>
16
   #include <botan/rng.h>
17
   #include <botan/x509_crl.h>
18
   #include <botan/x509_ext.h>
19
   #include <botan/x509path.h>
20
   #include <botan/internal/calendar.h>
21
   #include <botan/internal/filesystem.h>
22
   #include <botan/internal/fmt.h>
23
   #include <botan/internal/parsing.h>
24

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

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

37
namespace Botan_Tests {
38

39
namespace {
40

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

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

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

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

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

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

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

71
   return m;
18✔
72
}
9✔
73

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

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

84
   return certs;
80✔
85
}
80✔
86

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

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

94
   return result;
4✔
95
}
×
96

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

293
   return results;
2✔
294
}
154✔
295

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

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

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

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

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

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

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

318
      Botan::Certificate_Store_In_Memory store;
3✔
319

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

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

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

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

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

338
   return results;
1✔
339
}
1✔
340

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

343
      #if defined(BOTAN_HAS_PSS)
344

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

427
   return results;
1✔
428
}
19✔
429

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

432
      #endif
433

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

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

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

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

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

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

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

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

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

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

468
   const Botan::Certificate_Store_In_Memory empty;
1✔
469

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

864
         return results;
1✔
865
      }
7✔
866

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

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

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

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

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

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

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

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

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

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

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

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

912
         return result;
6✔
913
      }
6✔
914

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

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

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

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

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

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

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

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

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

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

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

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

976
class BSI_Path_Validation_Tests final : public Test
1✔
977

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1132
   return results;
2✔
1133
}
2✔
1134

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

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

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

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

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

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

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

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

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

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

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

1187
         return result;
2✔
1188
      }
2✔
1189

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

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

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

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

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

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

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

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

1230
         return result;
2✔
1231
      }
2✔
1232

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

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

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

1243
         trusted.add_certificate(trust_root);
1✔
1244

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

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

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

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

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

1271
         return result;
2✔
1272
      }
2✔
1273

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

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

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

1284
         trusted.add_certificate(trust_root);
1✔
1285

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

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

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

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

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

1313
         return result;
2✔
1314
      }
2✔
1315

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

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

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

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

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

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

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

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

1361
         return result;
1✔
1362
      }
4✔
1363

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

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

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

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

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

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

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

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

1419
         return result;
1✔
1420
      }
2✔
1421

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

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

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

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

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

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

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

1468
         return result;
1✔
1469
      }
2✔
1470

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

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

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

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

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

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

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

1507
         return result;
1✔
1508
      }
7✔
1509

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

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

1524
   #endif
1525

1526
   #if defined(BOTAN_HAS_ECDSA)
1527

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

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

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

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

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

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

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

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

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

1576
         result.confirm("Validation failed", !path_result1.successful_validation());
1✔
1577

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

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

1590
         result.confirm("Validation failed", !path_result2.successful_validation());
1✔
1591

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

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

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

1608
         result.confirm("Validation succeeded", path_result3.successful_validation());
1✔
1609

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1676
   #endif
1677

1678
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1679

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

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

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

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

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

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

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

1711
   #endif
1712

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1802
   #endif
1803

1804
#endif
1805

1806
}  // namespace
1807

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