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

randombit / botan / 18904991682

29 Oct 2025 10:36AM UTC coverage: 90.665% (-0.01%) from 90.675%
18904991682

push

github

web-flow
Merge pull request #5122 from reneme/fix/test_without_emsa_pssr

Skip certain tests if RSA signature paddings are disabled

100405 of 110743 relevant lines covered (90.66%)

12104416.78 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 Processing 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
      #if defined(BOTAN_HAS_PSS)
300

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

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

311
   std::vector<Test::Result> results;
1✔
312

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

315
   auto validation_times_iter = validation_times.begin();
1✔
316

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

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

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

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

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

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

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

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

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

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

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

379
      result.end_timer();
118✔
380
      results.push_back(result);
118✔
381
   }
334✔
382

383
   return results;
1✔
384
}
19✔
385

386
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
387

388
      #endif
389

390
class Validate_V1Cert_Test final : public Test {
×
391
   public:
392
      std::vector<Test::Result> run() override;
393
};
394

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

400
   std::vector<Test::Result> results;
1✔
401

402
   const std::string root_crt = Test::data_file("x509/misc/v1ca/root.pem");
1✔
403
   const std::string int_crt = Test::data_file("x509/misc/v1ca/int.pem");
1✔
404
   const std::string ee_crt = Test::data_file("x509/misc/v1ca/ee.pem");
1✔
405

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

408
   Botan::X509_Certificate root(root_crt);
1✔
409
   Botan::X509_Certificate intermediate(int_crt);
1✔
410
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
411

412
   Botan::Certificate_Store_In_Memory trusted;
1✔
413
   trusted.add_certificate(root);
1✔
414

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

417
   Botan::Path_Validation_Restrictions restrictions;
2✔
418
   Botan::Path_Validation_Result validation_result =
1✔
419
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
420

421
   Test::Result result("Verifying using v1 certificate");
1✔
422
   result.test_eq("Path validation result", validation_result.result_string(), "Verified");
2✔
423

424
   Botan::Certificate_Store_In_Memory empty;
1✔
425

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

428
   Botan::Path_Validation_Result validation_result2 =
1✔
429
      Botan::x509_path_validate(new_chain, restrictions, empty, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
430

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

433
   return {result};
2✔
434
}
4✔
435

436
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
437

438
class Validate_V2Uid_in_V1_Test final : public Test {
×
439
   public:
440
      std::vector<Test::Result> run() override;
441
};
442

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

448
   std::vector<Test::Result> results;
1✔
449

450
   const std::string root_crt = Test::data_file("x509/v2-in-v1/root.pem");
1✔
451
   const std::string int_crt = Test::data_file("x509/v2-in-v1/int.pem");
1✔
452
   const std::string ee_crt = Test::data_file("x509/v2-in-v1/leaf.pem");
1✔
453

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

456
   Botan::X509_Certificate root(root_crt);
1✔
457
   Botan::X509_Certificate intermediate(int_crt);
1✔
458
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
459

460
   Botan::Certificate_Store_In_Memory trusted;
1✔
461
   trusted.add_certificate(root);
1✔
462

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

465
   Botan::Path_Validation_Restrictions restrictions;
2✔
466
   Botan::Path_Validation_Result validation_result =
1✔
467
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
468

469
   Test::Result result("Verifying v1 certificate using v2 uid fields");
1✔
470
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
471
   result.test_eq(
3✔
472
      "Path validation result", validation_result.result_string(), "Encountered v2 identifiers in v1 certificate");
2✔
473

474
   return {result};
2✔
475
}
3✔
476

477
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
478

479
class Validate_Name_Constraint_SAN_Test final : public Test {
×
480
   public:
481
      std::vector<Test::Result> run() override;
482
};
483

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

489
   std::vector<Test::Result> results;
1✔
490

491
   const std::string root_crt = Test::data_file("x509/name_constraint_san/root.pem");
1✔
492
   const std::string int_crt = Test::data_file("x509/name_constraint_san/int.pem");
1✔
493
   const std::string ee_crt = Test::data_file("x509/name_constraint_san/leaf.pem");
1✔
494

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

497
   Botan::X509_Certificate root(root_crt);
1✔
498
   Botan::X509_Certificate intermediate(int_crt);
1✔
499
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
500

501
   Botan::Certificate_Store_In_Memory trusted;
1✔
502
   trusted.add_certificate(root);
1✔
503

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

506
   Botan::Path_Validation_Restrictions restrictions;
2✔
507
   Botan::Path_Validation_Result validation_result =
1✔
508
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
509

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

515
   return {result};
2✔
516
}
3✔
517

518
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
519

520
class Validate_Name_Constraint_CaseInsensitive final : public Test {
×
521
   public:
522
      std::vector<Test::Result> run() override;
523
};
524

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

530
   std::vector<Test::Result> results;
1✔
531

532
   const std::string root_crt = Test::data_file("x509/misc/name_constraint_ci/root.pem");
1✔
533
   const std::string int_crt = Test::data_file("x509/misc/name_constraint_ci/int.pem");
1✔
534
   const std::string ee_crt = Test::data_file("x509/misc/name_constraint_ci/leaf.pem");
1✔
535

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

538
   Botan::X509_Certificate root(root_crt);
1✔
539
   Botan::X509_Certificate intermediate(int_crt);
1✔
540
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
541

542
   Botan::Certificate_Store_In_Memory trusted;
1✔
543
   trusted.add_certificate(root);
1✔
544

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

547
   Botan::Path_Validation_Restrictions restrictions;
2✔
548
   Botan::Path_Validation_Result validation_result =
1✔
549
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
550

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

554
   return {result};
2✔
555
}
3✔
556

557
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
558

559
class Validate_Name_Constraint_NoCheckSelf final : public Test {
×
560
   public:
561
      std::vector<Test::Result> run() override;
562
};
563

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

569
   std::vector<Test::Result> results;
1✔
570

571
   const std::string root_crt = Test::data_file("x509/misc/nc_skip_self/root.pem");
1✔
572
   const std::string int_crt = Test::data_file("x509/misc/nc_skip_self/int.pem");
1✔
573
   const std::string ee_crt = Test::data_file("x509/misc/nc_skip_self/leaf.pem");
1✔
574

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

577
   Botan::X509_Certificate root(root_crt);
1✔
578
   Botan::X509_Certificate intermediate(int_crt);
1✔
579
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
580

581
   Botan::Certificate_Store_In_Memory trusted;
1✔
582
   trusted.add_certificate(root);
1✔
583

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

586
   Botan::Path_Validation_Restrictions restrictions;
2✔
587
   Botan::Path_Validation_Result validation_result =
1✔
588
      Botan::x509_path_validate(chain, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
1✔
589

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

593
   return {result};
2✔
594
}
3✔
595

596
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
597

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

605
         const std::string trusted_root_crt = Test::data_file("x509/misc/root_cert_time_check/root.crt");
1✔
606
         const std::string leaf_crt = Test::data_file("x509/misc/root_cert_time_check/leaf.crt");
1✔
607

608
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
609
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
610

611
         Botan::Certificate_Store_In_Memory trusted;
1✔
612
         trusted.add_certificate(trusted_root_cert);
1✔
613

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

616
         Test::Result result("Root cert time check");
1✔
617

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

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

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

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

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

694
         return {result};
2✔
695
      }
3✔
696
};
697

698
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
699

700
class BSI_Path_Validation_Tests final : public Test
×
701

702
{
703
   public:
704
      std::vector<Test::Result> run() override;
705
};
706

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

712
   std::vector<Test::Result> results;
1✔
713

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

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

720
      Botan::Certificate_Store_In_Memory trusted;
54✔
721
      std::vector<Botan::X509_Certificate> certs;
54✔
722

723
      #if defined(BOTAN_HAS_MD5)
724
      const bool has_md5 = true;
54✔
725
      #else
726
      const bool has_md5 = false;
727
      #endif
728

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

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

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

756
         Botan::Path_Validation_Restrictions restrictions(use_crl, 79, use_crl);
100✔
757

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

770
               static constexpr result_type min() { return 0; }
771

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

774
               result_type operator()() {
80✔
775
                  size_t s = 0;
80✔
776
                  m_rng.randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
80✔
777
                  return s;
80✔
778
               }
779

780
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
50✔
781

782
            private:
783
               Botan::RandomNumberGenerator& m_rng;
784
         } rbg(this->rng());
50✔
785

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

789
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
800✔
790
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
800✔
791

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

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

832
      result.end_timer();
54✔
833
      results.push_back(result);
54✔
834
   }
54✔
835

836
   return results;
1✔
837
}
1✔
838

839
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
840

841
class Path_Validation_With_OCSP_Tests final : public Test {
×
842
   public:
843
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
844
         return Botan::X509_Certificate(Test::data_file(path));
48✔
845
      }
846

847
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
848
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
36✔
849
      }
850

851
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
852
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
853
         Botan::Certificate_Store_In_Memory trusted;
1✔
854

855
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
856

857
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
858
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
859
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
860
         trusted.add_certificate(trust_root);
1✔
861

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

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

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

877
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
878
                                     Botan::to_string(path_result.result()) + "'",
8✔
879
                                  path_result.result() == expected);
8✔
880
         };
8✔
881

882
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
883
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
884
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
885
                    Botan::Certificate_Status_Code::OK);
886
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
887
                    Botan::Certificate_Status_Code::OK);
888
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
889
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
890

891
         return result;
2✔
892
      }
2✔
893

894
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
895
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
896
         Botan::Certificate_Store_In_Memory trusted;
1✔
897

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

900
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
901
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
902
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
903
         trusted.add_certificate(trust_root);
1✔
904

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

907
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
908

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

920
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
24✔
921
                                     Botan::to_string(path_result.result()) + "'",
8✔
922
                                  path_result.result() == expected);
8✔
923
         };
12✔
924

925
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
926
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
927
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
928
                    Botan::Certificate_Status_Code::OK);
929
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
930
                    Botan::Certificate_Status_Code::OK);
931
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
932
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
933

934
         return result;
2✔
935
      }
2✔
936

937
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
938
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
939
         Botan::Certificate_Store_In_Memory trusted;
1✔
940

941
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
942

943
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
944
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
945
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
946

947
         trusted.add_certificate(trust_root);
1✔
948

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

951
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
952

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

964
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
965
                                     Botan::to_string(path_result.result()) + "'",
6✔
966
                                  path_result.result() == expected);
6✔
967
         };
9✔
968

969
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
970
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
971
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
972
                    Botan::Certificate_Status_Code::OK);
973
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
974

975
         return result;
2✔
976
      }
2✔
977

978
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
979
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
980
         Botan::Certificate_Store_In_Memory trusted;
1✔
981

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

984
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
985
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
986
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
987

988
         trusted.add_certificate(trust_root);
1✔
989

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

992
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
993

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

1005
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1006
                                     Botan::to_string(path_result.result()) + "'",
6✔
1007
                                  path_result.result() == expected);
6✔
1008
         };
9✔
1009

1010
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
1011
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1012
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
1013
                    Botan::Certificate_Status_Code::OK);
1014
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
1015
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
1016

1017
         return result;
2✔
1018
      }
2✔
1019

1020
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
1021
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
1022
         Botan::Certificate_Store_In_Memory trusted;
1✔
1023

1024
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1025
                                                                 110,    // minimum key strength
1026
                                                                 true);  // OCSP for all intermediates
2✔
1027

1028
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
1029
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
1030
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
1031

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

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

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

1053
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
18✔
1054
                                     Botan::to_string(path_result.result()) + "'",
6✔
1055
                                  path_result.result() == expected);
6✔
1056
         };
12✔
1057

1058
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
1059
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
1060
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
1061
                    Botan::Certificate_Status_Code::OK);
1062
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
1063
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
1064

1065
         return result;
1✔
1066
      }
4✔
1067

1068
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1069
         Test::Result result(
1✔
1070
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1071
         Botan::Certificate_Store_In_Memory trusted;
1✔
1072

1073
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1074
                                                                 110,     // minimum key strength
1075
                                                                 false);  // OCSP for all intermediates
2✔
1076

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

1082
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1083
         auto ocsp_ee_delegate_malformed =
1✔
1084
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1085

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

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

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

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

1123
         return result;
1✔
1124
      }
2✔
1125

1126
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1127
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1128
         Botan::Certificate_Store_In_Memory trusted;
1✔
1129

1130
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1131
                                                                 110,     // minimum key strength
1132
                                                                 false);  // OCSP for all intermediates
2✔
1133

1134
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
1135
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
1136
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
1137
         trusted.add_certificate(trust_root);
1✔
1138

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

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

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

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

1172
         return result;
1✔
1173
      }
2✔
1174

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

1180
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1181
                                                                 110,    // minimum key strength
1182
                                                                 true);  // OCSP for all intermediates
2✔
1183

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

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

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

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

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

1211
         return result;
1✔
1212
      }
7✔
1213

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

1226
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1227

1228
   #endif
1229

1230
   #if defined(BOTAN_HAS_ECDSA)
1231

1232
class CVE_2020_0601_Tests final : public Test {
×
1233
   public:
1234
      std::vector<Test::Result> run() override {
1✔
1235
         Test::Result result("CVE-2020-0601");
1✔
1236

1237
         if(!Botan::EC_Group::supports_application_specific_group()) {
1✔
1238
            result.test_note("Skipping as application specific groups are not supported");
×
1239
            return {result};
×
1240
         }
1241

1242
         if(!Botan::EC_Group::supports_named_group("secp384r1")) {
1✔
1243
            result.test_note("Skipping as secp384r1 is not supported");
×
1244
            return {result};
×
1245
         }
1246

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

1260
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1261
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1262
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1263

1264
         Botan::Certificate_Store_In_Memory trusted;
1✔
1265
         trusted.add_certificate(ca_crt);
1✔
1266

1267
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1268

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

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

1280
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1281

1282
         result.confirm("Expected status",
2✔
1283
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1284

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

1294
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1295

1296
         result.confirm("Expected status",
2✔
1297
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1298

1299
         // Verify the signature from the bad CA is actually correct
1300
         Botan::Certificate_Store_In_Memory frusted;
1✔
1301
         frusted.add_certificate(fake_ca_crt);
1✔
1302

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

1312
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1313

1314
         return {result};
2✔
1315
      }
5✔
1316
};
1317

1318
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1319

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

1328
         if(Botan::has_filesystem_impl() == false) {
1✔
1329
            result.test_note("Skipping due to missing filesystem access");
×
1330
            return {result};
×
1331
         }
1332

1333
         Botan::X509_Certificate root(Test::data_file("x509/misc/crl_without_nextupdate/ca.pem"));
2✔
1334
         Botan::X509_Certificate revoked_subject(Test::data_file("x509/misc/crl_without_nextupdate/01.pem"));
2✔
1335
         Botan::X509_Certificate valid_subject(Test::data_file("x509/misc/crl_without_nextupdate/42.pem"));
2✔
1336

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

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

1349
         Botan::Certificate_Store_In_Memory trusted;
1✔
1350
         trusted.add_certificate(root);
1✔
1351
         trusted.add_crl(crl);
1✔
1352

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

1357
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1358

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

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

1374
         return {result};
2✔
1375
      }
2✔
1376
};
1377

1378
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1379

1380
   #endif
1381

1382
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1383

1384
class XMSS_Path_Validation_Tests final : public Test {
×
1385
   public:
1386
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1387
         Test::Result result(name);
2✔
1388

1389
         Botan::Path_Validation_Restrictions restrictions;
4✔
1390
         auto self_signed = Botan::X509_Certificate(Test::data_file("x509/xmss/" + file));
4✔
1391

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

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

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

1406
         return {
1✔
1407
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1✔
1408
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1✔
1409
                                 "xmss_bouncycastle_sha256_10_root.pem")};
3✔
1410
      }
4✔
1411
};
1412

1413
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1414

1415
   #endif
1416

1417
#endif
1418

1419
}  // namespace
1420

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