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

randombit / botan / 16675351906

01 Aug 2025 12:42PM UTC coverage: 90.677% (-0.005%) from 90.682%
16675351906

Pull #5032

github

web-flow
Merge a58f968aa into a361efb2e
Pull Request #5032: Fix some readability-implict-bool-conversion warnings from clang-tidy

99982 of 110262 relevant lines covered (90.68%)

12222015.95 hits per line

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

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

8
#include "tests.h"
9

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

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

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

34
namespace Botan_Tests {
35

36
namespace {
37

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

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

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

48
   std::map<std::string, std::string> m;
6✔
49
   std::string line;
6✔
50
   while(in.good()) {
426✔
51
      std::getline(in, line);
420✔
52
      if(line.empty()) {
420✔
53
         continue;
14✔
54
      }
55
      if(line[0] == '#') {
411✔
56
         continue;
5✔
57
      }
58

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

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

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

68
   return m;
12✔
69
}
6✔
70

71
std::set<Botan::Certificate_Status_Code> flatten(const Botan::CertificatePathStatusCodes& codes) {
4✔
72
   std::set<Botan::Certificate_Status_Code> result;
4✔
73

74
   for(const auto& statuses : codes) {
16✔
75
      result.insert(statuses.begin(), statuses.end());
12✔
76
   }
77

78
   return result;
4✔
79
}
×
80

81
class X509test_Path_Validation_Tests final : public Test {
×
82
   public:
83
      std::vector<Test::Result> run() override {
1✔
84
         std::vector<Test::Result> results;
1✔
85

86
         // Test certs generated by https://github.com/yymax/x509test
87

88
         // Current tests use SHA-1
89
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
90

91
         Botan::X509_Certificate root(Test::data_file("x509/x509test/root.pem"));
2✔
92
         Botan::Certificate_Store_In_Memory trusted;
1✔
93
         trusted.add_certificate(root);
1✔
94

95
         auto validation_time = Botan::calendar_point(2016, 10, 21, 4, 20, 0).to_std_timepoint();
1✔
96

97
         for(const auto& [filename, expected_result] : read_results(Test::data_file("x509/x509test/expected.txt"))) {
38✔
98
            Test::Result result("X509test path validation");
37✔
99
            result.start_timer();
37✔
100

101
            std::vector<Botan::X509_Certificate> certs = load_cert_file(Test::data_file("x509/x509test/" + filename));
74✔
102

103
            if(certs.empty()) {
37✔
104
               throw Test_Error("Failed to read certs from " + filename);
×
105
            }
106

107
            Botan::Path_Validation_Result path_result = Botan::x509_path_validate(
37✔
108
               certs, restrictions, trusted, "www.tls.test", Botan::Usage_Type::TLS_SERVER_AUTH, validation_time);
37✔
109

110
            if(path_result.successful_validation() && path_result.trust_root() != root) {
37✔
111
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
112
            }
113

114
            result.test_eq("test " + filename, path_result.result_string(), expected_result);
74✔
115
            result.test_eq("test no warnings string", path_result.warnings_string(), "");
74✔
116
            result.confirm("test no warnings", path_result.no_warnings());
74✔
117
            result.end_timer();
37✔
118
            results.push_back(result);
37✔
119
         }
37✔
120

121
         // test softfail
122
         {
1✔
123
            Test::Result result("X509test path validation softfail");
1✔
124
            result.start_timer();
1✔
125

126
            // this certificate must not have a OCSP URL
127
            const std::string filename = "ValidAltName.pem";
1✔
128
            std::vector<Botan::X509_Certificate> certs = load_cert_file(Test::data_file("x509/x509test/" + filename));
2✔
129
            if(certs.empty()) {
1✔
130
               throw Test_Error("Failed to read certs from " + filename);
×
131
            }
132

133
            Botan::Path_Validation_Result path_result =
1✔
134
               Botan::x509_path_validate(certs,
1✔
135
                                         restrictions,
136
                                         trusted,
137
                                         "www.tls.test",
138
                                         Botan::Usage_Type::TLS_SERVER_AUTH,
139
                                         validation_time,
140
                                         /* activate check_ocsp_online */ std::chrono::milliseconds(1000),
1✔
141
                                         {});
1✔
142

143
            if(path_result.successful_validation() && path_result.trust_root() != root) {
1✔
144
               path_result = Botan::Path_Validation_Result(Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
×
145
            }
146

147
            // certificate verification succeed even if no OCSP URL (softfail)
148
            result.confirm("test success", path_result.successful_validation());
2✔
149
            result.test_eq("test " + filename, path_result.result_string(), "Verified");
2✔
150
      #if defined(BOTAN_TARGET_OS_HAS_THREADS) && defined(BOTAN_HAS_HTTP_UTIL)
151
            // if softfail, there is warnings
152
            result.confirm("test warnings", !path_result.no_warnings());
2✔
153
            result.test_eq("test warnings string", path_result.warnings_string(), "[0] OCSP URL not available");
2✔
154
      #endif
155
            result.end_timer();
1✔
156
            results.push_back(result);
1✔
157
         }
1✔
158

159
         return results;
1✔
160
      }
1✔
161

162
   private:
163
      static std::vector<Botan::X509_Certificate> load_cert_file(const std::string& filename) {
38✔
164
         Botan::DataSource_Stream in(filename);
38✔
165

166
         std::vector<Botan::X509_Certificate> certs;
38✔
167
         while(!in.end_of_data()) {
238✔
168
            try {
162✔
169
               certs.emplace_back(in);
162✔
170
            } catch(Botan::Decoding_Error&) {}
38✔
171
         }
172

173
         return certs;
38✔
174
      }
38✔
175
};
176

177
BOTAN_REGISTER_TEST("x509", "x509_path_x509test", X509test_Path_Validation_Tests);
178

179
class NIST_Path_Validation_Tests final : public Test {
×
180
   public:
181
      std::vector<Test::Result> run() override;
182
};
183

184
std::vector<Test::Result> NIST_Path_Validation_Tests::run() {
1✔
185
   if(Botan::has_filesystem_impl() == false) {
1✔
186
      return {Test::Result::Note("NIST path validation", "Skipping due to missing filesystem access")};
×
187
   }
188

189
   std::vector<Test::Result> results;
1✔
190

191
   /**
192
   * Code to run the X.509v3 processing tests described in "Conformance
193
   *  Testing of Relying Party Client Certificate Path Proccessing Logic",
194
   *  which is available on NIST's web site.
195
   *
196
   * https://csrc.nist.gov/projects/pki-testing/x-509-path-validation-test-suite
197
   *
198
   * Known Failures/Problems:
199
   *  - Policy extensions are not implemented, so we skip tests #34-#53.
200
   *  - Tests #75 and #76 are skipped as they make use of relatively
201
   *    obscure CRL extensions which are not supported.
202
   */
203
   std::map<std::string, std::string> expected = read_results(Test::data_file("x509/nist/expected.txt"));
2✔
204

205
   const Botan::X509_Certificate root_cert(Test::data_file("x509/nist/root.crt"));
2✔
206
   const Botan::X509_CRL root_crl(Test::data_file("x509/nist/root.crl"));
2✔
207

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

210
   for(const auto& [test_name, expected_result] : expected) {
77✔
211
      Test::Result result("NIST path validation");
76✔
212
      result.start_timer();
76✔
213

214
      try {
76✔
215
         const auto all_files = Test::files_in_data_dir("x509/nist/" + test_name);
76✔
216

217
         Botan::Certificate_Store_In_Memory store;
76✔
218

219
         store.add_certificate(root_cert);
76✔
220
         store.add_crl(root_crl);
76✔
221

222
         for(const auto& file : all_files) {
399✔
223
            if(file.ends_with(".crt") && file != "end.crt") {
323✔
224
               store.add_certificate(Botan::X509_Certificate(file));
200✔
225
            } else if(file.ends_with(".crl")) {
123✔
226
               Botan::DataSource_Stream in(file, true);
123✔
227
               Botan::X509_CRL crl(in);
123✔
228
               store.add_crl(crl);
123✔
229
            }
123✔
230
         }
231

232
         Botan::X509_Certificate end_user(Test::data_file("x509/nist/" + test_name + "/end.crt"));
228✔
233

234
         // 1024 bit root cert
235
         Botan::Path_Validation_Restrictions restrictions(true, 80);
152✔
236

237
         Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
76✔
238
            end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
76✔
239

240
         result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
152✔
241
      } catch(std::exception& e) {
76✔
242
         result.test_failure(test_name, e.what());
×
243
      }
×
244

245
      result.end_timer();
76✔
246
      results.push_back(result);
76✔
247
   }
76✔
248

249
   return results;
1✔
250
}
1✔
251

252
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
253

254
class Extended_Path_Validation_Tests final : public Test {
×
255
   public:
256
      std::vector<Test::Result> run() override;
257
};
258

259
std::vector<Test::Result> Extended_Path_Validation_Tests::run() {
1✔
260
   if(Botan::has_filesystem_impl() == false) {
1✔
261
      return {Test::Result::Note("Extended x509 path validation", "Skipping due to missing filesystem access")};
×
262
   }
263

264
   std::vector<Test::Result> results;
1✔
265

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

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

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

274
      Botan::Certificate_Store_In_Memory store;
3✔
275

276
      for(const auto& file : all_files) {
13✔
277
         if(file.ends_with(".crt") && file != "end.crt") {
10✔
278
            store.add_certificate(Botan::X509_Certificate(file));
10✔
279
         }
280
      }
281

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

284
      Botan::Path_Validation_Restrictions restrictions;
6✔
285
      Botan::Path_Validation_Result validation_result =
3✔
286
         Botan::x509_path_validate(end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
3✔
287

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

290
      result.end_timer();
3✔
291
      results.push_back(result);
3✔
292
   }
3✔
293

294
   return results;
1✔
295
}
1✔
296

297
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
298

299
class PSS_Path_Validation_Tests : public Test {
×
300
   public:
301
      std::vector<Test::Result> run() override;
302
};
303

304
std::vector<Test::Result> PSS_Path_Validation_Tests::run() {
1✔
305
   if(Botan::has_filesystem_impl() == false) {
1✔
306
      return {Test::Result::Note("RSA-PSS X509 signature validation", "Skipping due to missing filesystem access")};
×
307
   }
308

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

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

313
   auto validation_times_iter = validation_times.begin();
1✔
314

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

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

321
      std::optional<Botan::X509_CRL> crl;
118✔
322
      std::optional<Botan::X509_Certificate> end;
118✔
323
      std::optional<Botan::X509_Certificate> root;
118✔
324
      Botan::Certificate_Store_In_Memory store;
118✔
325
      std::optional<Botan::PKCS10_Request> csr;
118✔
326

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

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

331
      for(const auto& file : all_files) {
345✔
332
         if(file.find("end.crt") != std::string::npos) {
227✔
333
            end = Botan::X509_Certificate(file);
113✔
334
         } else if(file.find("root.crt") != std::string::npos) {
114✔
335
            root = Botan::X509_Certificate(file);
97✔
336
            store.add_certificate(*root);
97✔
337
         } else if(file.ends_with(".crl")) {
17✔
338
            crl = Botan::X509_CRL(file);
6✔
339
         } else if(file.ends_with(".csr")) {
11✔
340
            csr = Botan::PKCS10_Request(file);
5✔
341
         }
342
      }
343

344
      if(end && crl && root) {
118✔
345
         // CRL test
346
         const std::vector<Botan::X509_Certificate> cert_path = {*end, *root};
18✔
347
         const std::vector<std::optional<Botan::X509_CRL>> crls = {crl};
12✔
348
         auto crl_status = Botan::PKIX::check_crl(
6✔
349
            cert_path,
350
            crls,
351
            validation_time);  // alternatively we could just call crl.check_signature( root_pubkey )
6✔
352

353
         result.test_eq(test_name + " check_crl result",
12✔
354
                        Botan::Path_Validation_Result::status_string(Botan::PKIX::overall_status(crl_status)),
355
                        expected_result);
356
      } else if(end && root) {
118✔
357
         // CRT chain test
358

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

361
         Botan::Path_Validation_Result validation_result =
91✔
362
            Botan::x509_path_validate(*end, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
91✔
363

364
         result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
182✔
365
      } else if(end && !root) {
112✔
366
         // CRT self signed test
367
         auto pubkey = end->subject_public_key();
16✔
368
         const bool accept = expected_result == "Verified";
16✔
369
         result.test_eq(test_name + " verify signature", end->check_signature(*pubkey), accept);
32✔
370
      } else if(csr) {
21✔
371
         // PKCS#10 Request test
372
         auto pubkey = csr->subject_public_key();
5✔
373
         const bool accept = expected_result == "Verified";
5✔
374
         result.test_eq(test_name + " verify signature", csr->check_signature(*pubkey), accept);
10✔
375
      }
5✔
376

377
      result.end_timer();
118✔
378
      results.push_back(result);
118✔
379
   }
334✔
380

381
   return results;
1✔
382
}
19✔
383

384
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
385

386
class Validate_V1Cert_Test final : public Test {
×
387
   public:
388
      std::vector<Test::Result> run() override;
389
};
390

391
std::vector<Test::Result> Validate_V1Cert_Test::run() {
1✔
392
   if(Botan::has_filesystem_impl() == false) {
1✔
393
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
394
   }
395

396
   std::vector<Test::Result> results;
1✔
397

398
   const std::string root_crt = Test::data_file("x509/misc/v1ca/root.pem");
1✔
399
   const std::string int_crt = Test::data_file("x509/misc/v1ca/int.pem");
1✔
400
   const std::string ee_crt = Test::data_file("x509/misc/v1ca/ee.pem");
1✔
401

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

404
   Botan::X509_Certificate root(root_crt);
1✔
405
   Botan::X509_Certificate intermediate(int_crt);
1✔
406
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
407

408
   Botan::Certificate_Store_In_Memory trusted;
1✔
409
   trusted.add_certificate(root);
1✔
410

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

413
   Botan::Path_Validation_Restrictions restrictions;
2✔
414
   Botan::Path_Validation_Result validation_result =
1✔
415
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
416

417
   Test::Result result("Verifying using v1 certificate");
1✔
418
   result.test_eq("Path validation result", validation_result.result_string(), "Verified");
2✔
419

420
   Botan::Certificate_Store_In_Memory empty;
1✔
421

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

424
   Botan::Path_Validation_Result validation_result2 =
1✔
425
      Botan::x509_path_validate(new_chain, restrictions, empty, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
426

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

429
   return {result};
2✔
430
}
4✔
431

432
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
433

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

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

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

446
   const std::string root_crt = Test::data_file("x509/v2-in-v1/root.pem");
1✔
447
   const std::string int_crt = Test::data_file("x509/v2-in-v1/int.pem");
1✔
448
   const std::string ee_crt = Test::data_file("x509/v2-in-v1/leaf.pem");
1✔
449

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

452
   Botan::X509_Certificate root(root_crt);
1✔
453
   Botan::X509_Certificate intermediate(int_crt);
1✔
454
   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
   std::vector<Botan::X509_Certificate> chain = {ee_cert, intermediate};
3✔
460

461
   Botan::Path_Validation_Restrictions restrictions;
2✔
462
   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 v1 certificate using v2 uid fields");
1✔
466
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
467
   result.test_eq(
3✔
468
      "Path validation result", validation_result.result_string(), "Encountered v2 identifiers in v1 certificate");
2✔
469

470
   return {result};
2✔
471
}
3✔
472

473
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
474

475
class Validate_Name_Constraint_SAN_Test final : public Test {
×
476
   public:
477
      std::vector<Test::Result> run() override;
478
};
479

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

485
   std::vector<Test::Result> results;
1✔
486

487
   const std::string root_crt = Test::data_file("x509/name_constraint_san/root.pem");
1✔
488
   const std::string int_crt = Test::data_file("x509/name_constraint_san/int.pem");
1✔
489
   const std::string ee_crt = Test::data_file("x509/name_constraint_san/leaf.pem");
1✔
490

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

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

497
   Botan::Certificate_Store_In_Memory trusted;
1✔
498
   trusted.add_certificate(root);
1✔
499

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

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

506
   Test::Result result("Verifying certificate with alternative SAN violating name constraint");
1✔
507
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
508
   result.test_eq(
3✔
509
      "Path validation result", validation_result.result_string(), "Certificate does not pass name constraint");
2✔
510

511
   return {result};
2✔
512
}
3✔
513

514
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
515

516
class Validate_Name_Constraint_CaseInsensitive final : public Test {
×
517
   public:
518
      std::vector<Test::Result> run() override;
519
};
520

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

526
   std::vector<Test::Result> results;
1✔
527

528
   const std::string root_crt = Test::data_file("x509/misc/name_constraint_ci/root.pem");
1✔
529
   const std::string int_crt = Test::data_file("x509/misc/name_constraint_ci/int.pem");
1✔
530
   const std::string ee_crt = Test::data_file("x509/misc/name_constraint_ci/leaf.pem");
1✔
531

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

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

538
   Botan::Certificate_Store_In_Memory trusted;
1✔
539
   trusted.add_certificate(root);
1✔
540

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

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

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

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

553
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
554

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

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

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

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

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

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

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

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

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

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

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

592
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
593

594
class Root_Cert_Time_Check_Test final : public Test {
×
595
   public:
596
      std::vector<Test::Result> run() override {
1✔
597
         if(Botan::has_filesystem_impl() == false) {
1✔
598
            return {Test::Result::Note("Path validation", "Skipping due to missing filesystem access")};
×
599
         }
600

601
         const std::string trusted_root_crt = Test::data_file("x509/misc/root_cert_time_check/root.crt");
1✔
602
         const std::string leaf_crt = Test::data_file("x509/misc/root_cert_time_check/leaf.crt");
1✔
603

604
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
605
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
606

607
         Botan::Certificate_Store_In_Memory trusted;
1✔
608
         trusted.add_certificate(trusted_root_cert);
1✔
609

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

612
         Test::Result result("Root cert time check");
1✔
613

614
         auto assert_path_validation_result = [&](std::string_view descr,
11✔
615
                                                  bool ignore_trusted_root_time_range,
616
                                                  uint32_t year,
617
                                                  Botan::Certificate_Status_Code exp_status,
618
                                                  std::optional<Botan::Certificate_Status_Code> exp_warning =
619
                                                     std::nullopt) {
620
            const Botan::Path_Validation_Restrictions restrictions(
10✔
621
               false,
622
               110,
623
               false,
624
               std::chrono::seconds::zero(),
625
               std::make_unique<Botan::Certificate_Store_In_Memory>(),
×
626
               ignore_trusted_root_time_range);
20✔
627

628
            const Botan::Path_Validation_Result validation_result =
10✔
629
               Botan::x509_path_validate(chain,
10✔
630
                                         restrictions,
631
                                         trusted,
10✔
632
                                         "",
633
                                         Botan::Usage_Type::UNSPECIFIED,
634
                                         Botan::calendar_point(year, 1, 1, 1, 0, 0).to_std_timepoint());
10✔
635
            const std::string descr_str = Botan::fmt(
10✔
636
               "Root cert validity range {}: {}", ignore_trusted_root_time_range ? "ignored" : "checked", descr);
15✔
637

638
            result.test_is_eq(descr_str, validation_result.result(), exp_status);
10✔
639
            const auto warnings = validation_result.warnings();
10✔
640
            BOTAN_ASSERT_NOMSG(warnings.size() == 2);
10✔
641
            result.confirm("No warning for leaf cert", warnings.at(0).empty());
20✔
642
            if(exp_warning) {
10✔
643
               result.confirm("Warning for root cert",
12✔
644
                              warnings.at(1).size() == 1 && warnings.at(1).contains(*exp_warning));
8✔
645
            } else {
646
               result.confirm("No warning for root cert", warnings.at(1).empty());
12✔
647
            }
648
         };
10✔
649
         // (Trusted) root cert validity range: 2022-2028
650
         // Leaf cert validity range: 2020-2030
651

652
         // Trusted root time range is checked
653
         assert_path_validation_result(
1✔
654
            "Root and leaf certs in validity range", false, 2025, Botan::Certificate_Status_Code::OK);
655
         assert_path_validation_result(
1✔
656
            "Root and leaf certs are expired", false, 2031, Botan::Certificate_Status_Code::CERT_HAS_EXPIRED);
657
         assert_path_validation_result(
1✔
658
            "Root and leaf certs are not yet valid", false, 2019, Botan::Certificate_Status_Code::CERT_NOT_YET_VALID);
659
         assert_path_validation_result(
1✔
660
            "Root cert is expired, leaf cert not", false, 2029, Botan::Certificate_Status_Code::CERT_HAS_EXPIRED);
661
         assert_path_validation_result("Root cert is not yet valid, leaf cert is",
1✔
662
                                       false,
663
                                       2021,
664
                                       Botan::Certificate_Status_Code::CERT_NOT_YET_VALID);
665

666
         // Trusted root time range is ignored
667
         assert_path_validation_result(
1✔
668
            "Root and leaf certs in validity range", true, 2025, Botan::Certificate_Status_Code::OK);
669
         assert_path_validation_result("Root and leaf certs are expired",
2✔
670
                                       true,
671
                                       2031,
672
                                       Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
673
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_HAS_EXPIRED);
1✔
674
         assert_path_validation_result("Root and leaf certs are not yet valid",
2✔
675
                                       true,
676
                                       2019,
677
                                       Botan::Certificate_Status_Code::CERT_NOT_YET_VALID,
678
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_NOT_YET_VALID);
1✔
679
         assert_path_validation_result("Root cert is expired, leaf cert not",
2✔
680
                                       true,
681
                                       2029,
682
                                       Botan::Certificate_Status_Code::OK,
683
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_HAS_EXPIRED);
1✔
684
         assert_path_validation_result("Root cert is not yet valid, leaf cert is",
2✔
685
                                       true,
686
                                       2021,
687
                                       Botan::Certificate_Status_Code::OK,
688
                                       Botan::Certificate_Status_Code::TRUSTED_CERT_NOT_YET_VALID);
1✔
689

690
         return {result};
2✔
691
      }
3✔
692
};
693

694
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
695

696
class BSI_Path_Validation_Tests final : public Test
×
697

698
{
699
   public:
700
      std::vector<Test::Result> run() override;
701
};
702

703
std::vector<Test::Result> BSI_Path_Validation_Tests::run() {
1✔
704
   if(Botan::has_filesystem_impl() == false) {
1✔
705
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
706
   }
707

708
   std::vector<Test::Result> results;
1✔
709

710
   for(const auto& [test_name, expected_result] : read_results(Test::data_file("x509/bsi/expected.txt"), '$')) {
55✔
711
      Test::Result result("BSI path validation");
54✔
712
      result.start_timer();
54✔
713

714
      const auto all_files = Test::files_in_data_dir("x509/bsi/" + test_name);
54✔
715

716
      Botan::Certificate_Store_In_Memory trusted;
54✔
717
      std::vector<Botan::X509_Certificate> certs;
54✔
718

719
      #if defined(BOTAN_HAS_MD5)
720
      const bool has_md5 = true;
54✔
721
      #else
722
      const bool has_md5 = false;
723
      #endif
724

725
      auto validation_time = Botan::calendar_point(2017, 8, 19, 12, 0, 0).to_std_timepoint();
54✔
726

727
      // By convention: if CRL is a substring if the test name,
728
      // we need to check the CRLs
729
      bool use_crl = false;
54✔
730
      if(test_name.find("CRL") != std::string::npos) {
54✔
731
         use_crl = true;
16✔
732
      }
733

734
      try {
54✔
735
         for(const auto& file : all_files) {
445✔
736
            // found a trust anchor
737
            if(file.find("TA") != std::string::npos) {
395✔
738
               trusted.add_certificate(Botan::X509_Certificate(file));
50✔
739
            }
740
            // found the target certificate. It needs to be at the front of certs
741
            else if(file.find("TC") != std::string::npos) {
345✔
742
               certs.insert(certs.begin(), Botan::X509_Certificate(file));
54✔
743
            }
744
            // found a certificate that might be part of a valid certificate chain to the trust anchor
745
            else if(file.find(".crt") != std::string::npos) {
291✔
746
               certs.push_back(Botan::X509_Certificate(file));
112✔
747
            } else if(file.find(".crl") != std::string::npos) {
235✔
748
               trusted.add_crl(Botan::X509_CRL(file));
28✔
749
            }
750
         }
751

752
         Botan::Path_Validation_Restrictions restrictions(use_crl, 79, use_crl);
100✔
753

754
         /*
755
          * Following the test document, the test are executed 16 times with
756
          * randomly chosen order of the available certificates. However, the target
757
          * certificate needs to stay in front.
758
          * For certain test, the order in which the certificates are given to
759
          * the validation function may be relevant, i.e. if issuer DNs are
760
          * ambiguous.
761
          */
762
         class random_bit_generator {
50✔
763
            public:
764
               using result_type = size_t;
765

766
               static constexpr result_type min() { return 0; }
767

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

770
               result_type operator()() {
80✔
771
                  size_t s = 0;
80✔
772
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
80✔
773
                  return s;
80✔
774
               }
775

776
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
50✔
777

778
            private:
779
               Botan::RandomNumberGenerator& m_rng;
780
         } rbg(this->rng());
50✔
781

782
         for(size_t r = 0; r < 16; r++) {
850✔
783
            std::shuffle(++(certs.begin()), certs.end(), rbg);
800✔
784

785
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
800✔
786
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
800✔
787

788
            // We expect to be warned
789
            if(expected_result.starts_with("Warning: ")) {
800✔
790
               std::string stripped = expected_result.substr(std::string("Warning: ").size());
32✔
791
               bool found_warning = false;
32✔
792
               for(const auto& warning_set : validation_result.warnings()) {
128✔
793
                  for(const auto& warning : warning_set) {
128✔
794
                     std::string warning_str(Botan::to_string(warning));
32✔
795
                     if(stripped == warning_str) {
32✔
796
                        result.test_eq(test_name + " path validation result", warning_str, stripped);
32✔
797
                        found_warning = true;
32✔
798
                     }
799
                  }
32✔
800
               }
32✔
801
               if(!found_warning) {
32✔
802
                  result.test_failure(test_name, "Did not receive the expected warning: " + stripped);
×
803
               }
804
            } else {
32✔
805
               if(expected_result == "Hash function used is considered too weak for security" && has_md5 == false) {
768✔
806
                  result.test_eq(test_name + " path validation result",
807
                                 validation_result.result_string(),
808
                                 "Certificate signed with unknown/unavailable algorithm");
809
               } else {
810
                  result.test_eq(
768✔
811
                     test_name + " path validation result", validation_result.result_string(), expected_result);
1,536✔
812
               }
813
            }
814
         }
800✔
815
      }
50✔
816

817
      /* Some certificates are rejected when executing the X509_Certificate constructor
818
       * by throwing a Decoding_Error exception.
819
       */
820
      catch(const Botan::Exception& e) {
4✔
821
         if(e.error_type() == Botan::ErrorType::DecodingFailure) {
4✔
822
            result.test_eq(test_name + " path validation result", e.what(), expected_result);
8✔
823
         } else {
824
            result.test_failure(test_name, e.what());
×
825
         }
826
      }
4✔
827

828
      result.end_timer();
54✔
829
      results.push_back(result);
54✔
830
   }
54✔
831

832
   return results;
1✔
833
}
1✔
834

835
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
836

837
class Path_Validation_With_OCSP_Tests final : public Test {
×
838
   public:
839
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
840
         return Botan::X509_Certificate(Test::data_file(path));
48✔
841
      }
842

843
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
844
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
845
      }
846

847
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
848
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
849
         Botan::Certificate_Store_In_Memory trusted;
1✔
850

851
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
852

853
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
854
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
855
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
856
         trusted.add_certificate(trust_root);
1✔
857

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

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

862
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
863
                               const Botan::Certificate_Status_Code expected) {
864
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
865
                                                               restrictions,
866
                                                               trusted,
4✔
867
                                                               "",
868
                                                               Botan::Usage_Type::UNSPECIFIED,
869
                                                               valid_time,
870
                                                               std::chrono::milliseconds(0),
4✔
871
                                                               {ocsp});
8✔
872

873
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
874
                                     Botan::to_string(path_result.result()) + "'",
8✔
875
                                  path_result.result() == expected);
8✔
876
         };
8✔
877

878
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
879
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
880
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
881
                    Botan::Certificate_Status_Code::OK);
882
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
883
                    Botan::Certificate_Status_Code::OK);
884
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
885
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
886

887
         return result;
2✔
888
      }
2✔
889

890
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
891
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
892
         Botan::Certificate_Store_In_Memory trusted;
1✔
893

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

896
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
897
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
898
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
899
         trusted.add_certificate(trust_root);
1✔
900

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

903
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
904

905
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
906
                               const Botan::Certificate_Status_Code expected) {
907
            const auto path_result = Botan::x509_path_validate(cert_path,
12✔
908
                                                               restrictions,
909
                                                               trusted,
4✔
910
                                                               "",
911
                                                               Botan::Usage_Type::UNSPECIFIED,
912
                                                               valid_time,
913
                                                               std::chrono::milliseconds(0),
4✔
914
                                                               {ocsp});
8✔
915

916
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
917
                                     Botan::to_string(path_result.result()) + "'",
8✔
918
                                  path_result.result() == expected);
8✔
919
         };
12✔
920

921
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
922
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
923
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
924
                    Botan::Certificate_Status_Code::OK);
925
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
926
                    Botan::Certificate_Status_Code::OK);
927
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
928
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
929

930
         return result;
2✔
931
      }
2✔
932

933
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
934
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
935
         Botan::Certificate_Store_In_Memory trusted;
1✔
936

937
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
938

939
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
940
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
941
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
942

943
         trusted.add_certificate(trust_root);
1✔
944

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

947
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
948

949
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
950
                               const Botan::Certificate_Status_Code expected) {
951
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
952
                                                               restrictions,
953
                                                               trusted,
3✔
954
                                                               "",
955
                                                               Botan::Usage_Type::UNSPECIFIED,
956
                                                               valid_time,
957
                                                               std::chrono::milliseconds(0),
3✔
958
                                                               {ocsp});
6✔
959

960
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
961
                                     Botan::to_string(path_result.result()) + "'",
6✔
962
                                  path_result.result() == expected);
6✔
963
         };
9✔
964

965
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
966
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
967
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
968
                    Botan::Certificate_Status_Code::OK);
969
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
970

971
         return result;
2✔
972
      }
2✔
973

974
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
975
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
976
         Botan::Certificate_Store_In_Memory trusted;
1✔
977

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

980
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
981
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
982
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
983

984
         trusted.add_certificate(trust_root);
1✔
985

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

988
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
989

990
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
991
                               const Botan::Certificate_Status_Code expected) {
992
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
993
                                                               restrictions,
994
                                                               trusted,
3✔
995
                                                               "",
996
                                                               Botan::Usage_Type::UNSPECIFIED,
997
                                                               valid_time,
998
                                                               std::chrono::milliseconds(0),
3✔
999
                                                               {ocsp});
6✔
1000

1001
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1002
                                     Botan::to_string(path_result.result()) + "'",
6✔
1003
                                  path_result.result() == expected);
6✔
1004
         };
9✔
1005

1006
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1007
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1008
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1009
                    Botan::Certificate_Status_Code::OK);
1010
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1011
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1012

1013
         return result;
2✔
1014
      }
2✔
1015

1016
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1017
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1018
         Botan::Certificate_Store_In_Memory trusted;
1✔
1019

1020
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1021
                                                                 110,    // minimum key strength
1022
                                                                 true);  // OCSP for all intermediates
2✔
1023

1024
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1025
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1026
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1027

1028
         // These OCSP responses are signed by an authorized OCSP responder
1029
         // certificate issued by `ca` and `trust_root` respectively. Note that
1030
         // the responder certificates contain the "OCSP No Check" extension,
1031
         // meaning that they themselves do not need a revocation check via OCSP.
1032
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/bdr-ocsp-resp.der");
1✔
1033
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/bdr-int-ocsp-resp.der");
1✔
1034

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

1038
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1039
                               const Botan::Certificate_Status_Code expected) {
1040
            const auto path_result = Botan::x509_path_validate(cert_path,
15✔
1041
                                                               restrictions,
1042
                                                               trusted,
3✔
1043
                                                               "",
1044
                                                               Botan::Usage_Type::UNSPECIFIED,
1045
                                                               valid_time,
1046
                                                               std::chrono::milliseconds(0),
3✔
1047
                                                               {ocsp_ee, ocsp_ca});
9✔
1048

1049
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1050
                                     Botan::to_string(path_result.result()) + "'",
6✔
1051
                                  path_result.result() == expected);
6✔
1052
         };
12✔
1053

1054
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1055
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1056
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1057
                    Botan::Certificate_Status_Code::OK);
1058
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1059
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1060

1061
         return result;
1✔
1062
      }
4✔
1063

1064
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1065
         Test::Result result(
1✔
1066
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1067
         Botan::Certificate_Store_In_Memory trusted;
1✔
1068

1069
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1070
                                                                 110,     // minimum key strength
1071
                                                                 false);  // OCSP for all intermediates
2✔
1072

1073
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1074
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1075
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1076
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1077

1078
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1079
         auto ocsp_ee_delegate_malformed =
1✔
1080
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1081

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

1085
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1086
                               const Botan::OCSP::Response& ocsp_ee,
1087
                               const Botan::Certificate_Status_Code expected,
1088
                               const std::optional<Botan::Certificate_Status_Code> also_expected = std::nullopt) {
1089
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1090
                                                               restrictions,
1091
                                                               trusted,
3✔
1092
                                                               "",
1093
                                                               Botan::Usage_Type::UNSPECIFIED,
1094
                                                               valid_time,
1095
                                                               std::chrono::milliseconds(0),
3✔
1096
                                                               {ocsp_ee});
6✔
1097

1098
            result.test_is_eq("should result in expected validation status code",
3✔
1099
                              static_cast<uint32_t>(path_result.result()),
3✔
1100
                              static_cast<uint32_t>(expected));
3✔
1101
            if(also_expected) {
3✔
1102
               result.confirm("Secondary error is also present",
4✔
1103
                              flatten(path_result.all_statuses()).contains(also_expected.value()));
6✔
1104
            }
1105
         };
6✔
1106

1107
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1108
                    ocsp_ee_delegate,
1109
                    Botan::Certificate_Status_Code::VERIFIED);
1110
         check_path(Botan::calendar_point(2022, 10, 8, 23, 30, 0).to_std_timepoint(),
1✔
1111
                    ocsp_ee_delegate,
1112
                    Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
1113
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1114
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1115
                    ocsp_ee_delegate_malformed,
1116
                    Botan::Certificate_Status_Code::OCSP_RESPONSE_MISSING_KEYUSAGE,
1117
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1118

1119
         return result;
1✔
1120
      }
2✔
1121

1122
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1123
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1124
         Botan::Certificate_Store_In_Memory trusted;
1✔
1125

1126
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1127
                                                                 110,     // minimum key strength
1128
                                                                 false);  // OCSP for all intermediates
2✔
1129

1130
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1131
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1132
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1133
         trusted.add_certificate(trust_root);
1✔
1134

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

1137
         auto check_path = [&](const std::string& forged_ocsp,
3✔
1138
                               const Botan::Certificate_Status_Code expected,
1139
                               const Botan::Certificate_Status_Code also_expected) {
1140
            auto ocsp = load_test_OCSP_resp(forged_ocsp);
2✔
1141
            const auto path_result =
2✔
1142
               Botan::x509_path_validate(cert_path,
6✔
1143
                                         restrictions,
1144
                                         trusted,
2✔
1145
                                         "",
1146
                                         Botan::Usage_Type::UNSPECIFIED,
1147
                                         Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
4✔
1148
                                         std::chrono::milliseconds(0),
2✔
1149
                                         {ocsp});
4✔
1150

1151
            result.test_is_eq(
2✔
1152
               "Path validation with forged OCSP response should fail with", path_result.result(), expected);
2✔
1153
            result.confirm("Secondary error is also present",
4✔
1154
                           flatten(path_result.all_statuses()).contains(also_expected));
4✔
1155
            result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
6✔
1156
         };
8✔
1157

1158
         // In both cases the path validation should detect the forged OCSP
1159
         // response and generate an appropriate error. By no means it should
1160
         // follow the unauthentic OCSP response.
1161
         check_path("x509/ocsp/randombit_ocsp_forged_valid.der",
1✔
1162
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1163
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1164
         check_path("x509/ocsp/randombit_ocsp_forged_revoked.der",
1✔
1165
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1166
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1167

1168
         return result;
1✔
1169
      }
2✔
1170

1171
      static Test::Result validate_with_ocsp_self_signed_by_intermediate_cert() {
1✔
1172
         Test::Result result(
1✔
1173
            "path check with ocsp response for intermediate that is (maliciously) self-signed by the intermediate");
1✔
1174
         Botan::Certificate_Store_In_Memory trusted;
1✔
1175

1176
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1177
                                                                 110,    // minimum key strength
1178
                                                                 true);  // OCSP for all intermediates
2✔
1179

1180
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1181
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1182
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1183
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1184

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

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

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

1194
         const auto path_result =
1✔
1195
            Botan::x509_path_validate(cert_path,
5✔
1196
                                      restrictions,
1197
                                      trusted,
1198
                                      "",
1199
                                      Botan::Usage_Type::UNSPECIFIED,
1200
                                      Botan::calendar_point(2022, 9, 22, 22, 30, 0).to_std_timepoint(),
2✔
1201
                                      std::chrono::milliseconds(0),
1✔
1202
                                      {ocsp_ee, ocsp_ca});
3✔
1203
         result.confirm("should reject intermediate OCSP response",
2✔
1204
                        path_result.result() == Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_FOUND);
1✔
1205
         result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
3✔
1206

1207
         return result;
1✔
1208
      }
7✔
1209

1210
      std::vector<Test::Result> run() override {
1✔
1211
         return {validate_with_ocsp_with_next_update_without_max_age(),
1✔
1212
                 validate_with_ocsp_with_next_update_with_max_age(),
1213
                 validate_with_ocsp_without_next_update_without_max_age(),
1214
                 validate_with_ocsp_without_next_update_with_max_age(),
1215
                 validate_with_ocsp_with_authorized_responder(),
1216
                 validate_with_ocsp_with_authorized_responder_without_keyusage(),
1217
                 validate_with_forged_ocsp_using_self_signed_cert(),
1218
                 validate_with_ocsp_self_signed_by_intermediate_cert()};
9✔
1219
      }
1✔
1220
};
1221

1222
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1223

1224
   #endif
1225

1226
   #if defined(BOTAN_HAS_ECDSA)
1227

1228
class CVE_2020_0601_Tests final : public Test {
×
1229
   public:
1230
      std::vector<Test::Result> run() override {
1✔
1231
         Test::Result result("CVE-2020-0601");
1✔
1232

1233
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1234
            result.test_note("Skipping as application specific groups are not supported");
×
1235
            return {result};
×
1236
         }
1237

1238
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1239
            result.test_note("Skipping as secp384r1 is not supported");
×
1240
            return {result};
×
1241
         }
1242

1243
         const auto& secp384r1 = Botan::EC_Group::from_name("secp384r1");
1✔
1244
         Botan::OID curveball_oid("1.3.6.1.4.1.25258.4.2020.0601");
1✔
1245
         Botan::EC_Group curveball(
1✔
1246
            curveball_oid,
1247
            secp384r1.get_p(),
1248
            secp384r1.get_a(),
1249
            secp384r1.get_b(),
1250
            BigInt(
2✔
1251
               "0xC711162A761D568EBEB96265D4C3CEB4F0C330EC8F6DD76E39BCC849ABABB8E34378D581065DEFC77D9FCED6B39075DE"),
1252
            BigInt(
2✔
1253
               "0x0CB090DE23BAC8D13E67E019A91B86311E5F342DEE17FD15FB7E278A32A1EAC98FC97E18CB2F3B2C487A7DA6F40107AC"),
1254
            secp384r1.get_order());
2✔
1255

1256
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1257
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1258
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1259

1260
         Botan::Certificate_Store_In_Memory trusted;
1✔
1261
         trusted.add_certificate(ca_crt);
1✔
1262

1263
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1264

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

1267
         const auto path_result1 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt, fake_ca_crt},
5✔
1268
                                                             restrictions,
1269
                                                             trusted,
1270
                                                             "",
1271
                                                             Botan::Usage_Type::UNSPECIFIED,
1272
                                                             valid_time,
1273
                                                             std::chrono::milliseconds(0),
1✔
1274
                                                             {});
2✔
1275

1276
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1277

1278
         result.confirm("Expected status",
2✔
1279
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1280

1281
         const auto path_result2 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1282
                                                             restrictions,
1283
                                                             trusted,
1284
                                                             "",
1285
                                                             Botan::Usage_Type::UNSPECIFIED,
1286
                                                             valid_time,
1287
                                                             std::chrono::milliseconds(0),
1✔
1288
                                                             {});
2✔
1289

1290
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1291

1292
         result.confirm("Expected status",
2✔
1293
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1294

1295
         // Verify the signature from the bad CA is actually correct
1296
         Botan::Certificate_Store_In_Memory frusted;
1✔
1297
         frusted.add_certificate(fake_ca_crt);
1✔
1298

1299
         const auto path_result3 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1300
                                                             restrictions,
1301
                                                             frusted,
1302
                                                             "",
1303
                                                             Botan::Usage_Type::UNSPECIFIED,
1304
                                                             valid_time,
1305
                                                             std::chrono::milliseconds(0),
1✔
1306
                                                             {});
2✔
1307

1308
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1309

1310
         return {result};
2✔
1311
      }
5✔
1312
};
1313

1314
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1315

1316
class Path_Validation_With_Immortal_CRL final : public Test {
×
1317
   public:
1318
      std::vector<Test::Result> run() override {
1✔
1319
         // RFC 5280 defines the nextUpdate field as "optional" (in line with
1320
         // the original X.509 standard), but then requires all conforming CAs
1321
         // to always define it. For best compatibility we must deal with both.
1322
         Test::Result result("Using a CRL without a nextUpdate field");
1✔
1323

1324
         if(Botan::has_filesystem_impl() == false) {
1✔
1325
            result.test_note("Skipping due to missing filesystem access");
×
1326
            return {result};
×
1327
         }
1328

1329
         Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1330
         Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1331
         Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1332

1333
         // Check that a CRL without nextUpdate is parsable
1334
         auto crl = Botan::X509_CRL(Test::data_file("x509/misc/crl_without_nextupdate/valid_forever.crl"));
2✔
1335
         result.confirm("this update is set", crl.this_update().time_is_set());
2✔
1336
         result.confirm("next update is not set", !crl.next_update().time_is_set());
2✔
1337
         result.confirm("CRL is not empty", !crl.get_revoked().empty());
2✔
1338

1339
         // Ensure that we support the used sig algo, otherwish stop here
1340
         if(!Botan::EC_Group::supports_named_group("brainpool512r1")) {
1✔
1341
            result.test_note("Cannot test path validation because signature algorithm is not support in this build");
×
1342
            return {result};
×
1343
         }
1344

1345
         Botan::Certificate_Store_In_Memory trusted;
1✔
1346
         trusted.add_certificate(root);
1✔
1347
         trusted.add_crl(crl);
1✔
1348

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

1353
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1354

1355
         // Validate a certificate that is not listed in the CRL
1356
         const auto valid = Botan::x509_path_validate(
1✔
1357
            valid_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1358
         if(!result.confirm("Valid certificate", valid.successful_validation())) {
2✔
1359
            result.test_note(valid.result_string());
×
1360
         }
1361

1362
         // Ensure that a certificate listed in the CRL is recognized as revoked
1363
         const auto revoked = Botan::x509_path_validate(
1✔
1364
            revoked_subject, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, valid_time);
1✔
1365
         if(!result.confirm("No valid certificate", !revoked.successful_validation())) {
2✔
1366
            result.test_note(revoked.result_string());
×
1367
         }
1368
         result.test_is_eq("Certificate is revoked", revoked.result(), Botan::Certificate_Status_Code::CERT_IS_REVOKED);
1✔
1369

1370
         return {result};
2✔
1371
      }
2✔
1372
};
1373

1374
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1375

1376
   #endif
1377

1378
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1379

1380
class XMSS_Path_Validation_Tests final : public Test {
×
1381
   public:
1382
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1383
         Test::Result result(name);
2✔
1384

1385
         Botan::Path_Validation_Restrictions restrictions;
4✔
1386
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1387

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

1391
         auto status = Botan::PKIX::overall_status(
2✔
1392
            Botan::PKIX::check_chain(cert_path, valid_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions));
4✔
1393
         result.test_eq("Cert validation status", Botan::to_string(status), "Verified");
2✔
1394
         return result;
2✔
1395
      }
4✔
1396

1397
      std::vector<Test::Result> run() override {
1✔
1398
         if(Botan::has_filesystem_impl() == false) {
1✔
1399
            return {Test::Result::Note("XMSS path validation", "Skipping due to missing filesystem access")};
×
1400
         }
1401

1402
         return {
1✔
1403
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1404
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1405
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1406
      }
4✔
1407
};
1408

1409
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1410

1411
   #endif
1412

1413
#endif
1414

1415
}  // namespace
1416

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