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

randombit / botan / 16247597625

13 Jul 2025 09:19AM UTC coverage: 90.574% (-0.001%) from 90.575%
16247597625

push

github

web-flow
Merge pull request #4974 from randombit/jack/fix-clang-tidy-modernize-loop-convert

Enable and fix clang-tidy warning modernize-loop-convert

99094 of 109407 relevant lines covered (90.57%)

12407263.12 hits per line

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

93.49
/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)  // CRL tests
118✔
345
      {
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)  // CRT chain tests
118✔
357
      {
358
         // sha-1 is used
359
         Botan::Path_Validation_Restrictions restrictions(false, 80);
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)  // CRT self signed tests
112✔
366
      {
367
         auto pubkey = end->subject_public_key();
16✔
368
         result.test_eq(test_name + " verify signature", end->check_signature(*pubkey), !!(std::stoi(expected_result)));
32✔
369
      } else if(csr)  // PKCS#10 Request
21✔
370
      {
371
         auto pubkey = csr->subject_public_key();
5✔
372
         result.test_eq(test_name + " verify signature", csr->check_signature(*pubkey), !!(std::stoi(expected_result)));
10✔
373
      }
5✔
374

375
      result.end_timer();
118✔
376
      results.push_back(result);
118✔
377
   }
334✔
378

379
   return results;
1✔
380
}
19✔
381

382
BOTAN_REGISTER_TEST("x509", "x509_path_rsa_pss", PSS_Path_Validation_Tests);
383

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

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

394
   std::vector<Test::Result> results;
1✔
395

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

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

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

406
   Botan::Certificate_Store_In_Memory trusted;
1✔
407
   trusted.add_certificate(root);
1✔
408

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

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

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

418
   Botan::Certificate_Store_In_Memory empty;
1✔
419

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

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

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

427
   return {result};
2✔
428
}
4✔
429

430
BOTAN_REGISTER_TEST("x509", "x509_v1_ca", Validate_V1Cert_Test);
431

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

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

442
   std::vector<Test::Result> results;
1✔
443

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

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

450
   Botan::X509_Certificate root(root_crt);
1✔
451
   Botan::X509_Certificate intermediate(int_crt);
1✔
452
   Botan::X509_Certificate ee_cert(ee_crt);
1✔
453

454
   Botan::Certificate_Store_In_Memory trusted;
1✔
455
   trusted.add_certificate(root);
1✔
456

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

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

463
   Test::Result result("Verifying v1 certificate using v2 uid fields");
1✔
464
   result.test_eq("Path validation failed", validation_result.successful_validation(), false);
1✔
465
   result.test_eq(
3✔
466
      "Path validation result", validation_result.result_string(), "Encountered v2 identifiers in v1 certificate");
2✔
467

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

471
BOTAN_REGISTER_TEST("x509", "x509_v2uid_in_v1", Validate_V2Uid_in_V1_Test);
472

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

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

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

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

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

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

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

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

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

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

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

512
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_san", Validate_Name_Constraint_SAN_Test);
513

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

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

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

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

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

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

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

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

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

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

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

551
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_ci", Validate_Name_Constraint_CaseInsensitive);
552

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

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

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

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

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

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

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

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

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

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

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

590
BOTAN_REGISTER_TEST("x509", "x509_name_constraint_no_check_self", Validate_Name_Constraint_NoCheckSelf);
591

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

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

602
         const Botan::X509_Certificate trusted_root_cert(trusted_root_crt);
1✔
603
         const Botan::X509_Certificate leaf_cert(leaf_crt);
1✔
604

605
         Botan::Certificate_Store_In_Memory trusted;
1✔
606
         trusted.add_certificate(trusted_root_cert);
1✔
607

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

610
         Test::Result result("Root cert time check");
1✔
611

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

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

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

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

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

688
         return {result};
2✔
689
      }
3✔
690
};
691

692
BOTAN_REGISTER_TEST("x509", "x509_root_cert_time_check", Root_Cert_Time_Check_Test);
693

694
class BSI_Path_Validation_Tests final : public Test
×
695

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

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

706
   std::vector<Test::Result> results;
1✔
707

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

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

714
      Botan::Certificate_Store_In_Memory trusted;
54✔
715
      std::vector<Botan::X509_Certificate> certs;
54✔
716

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

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

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

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

750
         Botan::Path_Validation_Restrictions restrictions(use_crl, 79, use_crl);
100✔
751

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

764
               static constexpr result_type min() { return 0; }
765

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

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

774
               explicit random_bit_generator(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
50✔
775

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

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

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

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

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

826
      result.end_timer();
54✔
827
      results.push_back(result);
54✔
828
   }
54✔
829

830
   return results;
1✔
831
}
1✔
832

833
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
834

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

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

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

849
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
850

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

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

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

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

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

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

885
         return result;
2✔
886
      }
2✔
887

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

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

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

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

901
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
902

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

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

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

928
         return result;
2✔
929
      }
2✔
930

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

935
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
936

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

941
         trusted.add_certificate(trust_root);
1✔
942

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

945
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
946

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

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

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

969
         return result;
2✔
970
      }
2✔
971

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

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

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

982
         trusted.add_certificate(trust_root);
1✔
983

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

986
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
987

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

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

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

1011
         return result;
2✔
1012
      }
2✔
1013

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

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

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

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

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

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

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

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

1059
         return result;
1✔
1060
      }
4✔
1061

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

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

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

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

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

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

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

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

1117
         return result;
1✔
1118
      }
2✔
1119

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

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

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

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

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

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

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

1166
         return result;
1✔
1167
      }
2✔
1168

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

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

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

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

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

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

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

1205
         return result;
1✔
1206
      }
7✔
1207

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

1220
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1221

1222
   #endif
1223

1224
   #if defined(BOTAN_HAS_ECDSA)
1225

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

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

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

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

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

1258
         Botan::Certificate_Store_In_Memory trusted;
1✔
1259
         trusted.add_certificate(ca_crt);
1✔
1260

1261
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1262

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

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

1274
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1275

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

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

1288
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1289

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

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

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

1306
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1307

1308
         return {result};
2✔
1309
      }
5✔
1310
};
1311

1312
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1313

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

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

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

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

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

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

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

1351
         Botan::Path_Validation_Restrictions restrictions(true /* require revocation info */);
2✔
1352

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

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

1368
         return {result};
2✔
1369
      }
2✔
1370
};
1371

1372
BOTAN_REGISTER_TEST("x509", "x509_path_immortal_crl", Path_Validation_With_Immortal_CRL);
1373

1374
   #endif
1375

1376
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1377

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

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

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

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

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

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

1407
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1408

1409
   #endif
1410

1411
#endif
1412

1413
}  // namespace
1414

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