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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

93.52
/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/parsing.h>
20

21
   #include <algorithm>
22
   #include <fstream>
23
   #include <limits>
24
   #include <map>
25
   #include <string>
26
   #include <vector>
27
#endif
28

29
namespace Botan_Tests {
30

31
namespace {
32

33
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
34

35
   #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EMSA_PKCS1)
36

37
std::map<std::string, std::string> read_results(const std::string& results_file, const char delim = ':') {
6✔
38
   std::ifstream in(results_file);
6✔
39
   if(!in.good()) {
6✔
40
      throw Test_Error("Failed reading " + results_file);
×
41
   }
42

43
   std::map<std::string, std::string> m;
6✔
44
   std::string line;
6✔
45
   while(in.good()) {
420✔
46
      std::getline(in, line);
414✔
47
      if(line.empty()) {
414✔
48
         continue;
14✔
49
      }
50
      if(line[0] == '#') {
409✔
51
         continue;
9✔
52
      }
53

54
      std::vector<std::string> parts = Botan::split_on(line, delim);
400✔
55

56
      if(parts.size() != 2) {
400✔
57
         throw Test_Error("Invalid line " + line);
×
58
      }
59

60
      m[parts[0]] = parts[1];
400✔
61
   }
400✔
62

63
   return m;
12✔
64
}
6✔
65

66
std::set<Botan::Certificate_Status_Code> flatten(const Botan::CertificatePathStatusCodes& codes) {
4✔
67
   std::set<Botan::Certificate_Status_Code> result;
4✔
68

69
   for(const auto& statuses : codes) {
16✔
70
      result.insert(statuses.begin(), statuses.end());
12✔
71
   }
72

73
   return result;
4✔
74
}
×
75

76
class X509test_Path_Validation_Tests final : public Test {
×
77
   public:
78
      std::vector<Test::Result> run() override {
1✔
79
         std::vector<Test::Result> results;
1✔
80

81
         // Test certs generated by https://github.com/yymax/x509test
82

83
         std::map<std::string, std::string> expected = read_results(Test::data_file("x509/x509test/expected.txt"));
2✔
84

85
         // Current tests use SHA-1
86
         const Botan::Path_Validation_Restrictions restrictions(false, 80);
2✔
87

88
         Botan::X509_Certificate root(Test::data_file("x509/x509test/root.pem"));
2✔
89
         Botan::Certificate_Store_In_Memory trusted;
1✔
90
         trusted.add_certificate(root);
1✔
91

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

94
         for(auto i = expected.begin(); i != expected.end(); ++i) {
38✔
95
            Test::Result result("X509test path validation");
37✔
96
            result.start_timer();
37✔
97
            const std::string filename = i->first;
37✔
98
            const std::string expected_result = i->second;
37✔
99

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

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

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

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

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

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

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

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

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

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

158
         return results;
2✔
159
      }
1✔
160

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

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

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

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

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

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

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

190
   /**
191
   * Code to run the X.509v3 processing tests described in "Conformance
192
   *  Testing of Relying Party Client Certificate Path Proccessing Logic",
193
   *  which is available on NIST's web site.
194
   *
195
   * Known Failures/Problems:
196
   *  - Policy extensions are not implemented, so we skip tests #34-#53.
197
   *  - Tests #75 and #76 are skipped as they make use of relatively
198
   *    obscure CRL extensions which are not supported.
199
   */
200
   const std::string nist_test_dir = Test::data_dir() + "/x509/nist";
1✔
201

202
   std::map<std::string, std::string> expected = read_results(Test::data_file("x509/nist/expected.txt"));
2✔
203

204
   const Botan::X509_Certificate root_cert(nist_test_dir + "/root.crt");
1✔
205
   const Botan::X509_CRL root_crl(nist_test_dir + "/root.crl");
1✔
206

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

209
   for(auto i = expected.begin(); i != expected.end(); ++i) {
72✔
210
      Test::Result result("NIST path validation");
71✔
211
      result.start_timer();
71✔
212

213
      const std::string test_name = i->first;
71✔
214

215
      try {
71✔
216
         const std::string expected_result = i->second;
71✔
217

218
         const std::string test_dir = nist_test_dir + "/" + test_name;
71✔
219

220
         const std::vector<std::string> all_files = Botan::get_files_recursive(test_dir);
71✔
221

222
         if(all_files.empty()) {
71✔
223
            result.test_failure("No test files found in " + test_dir);
×
224
            results.push_back(result);
×
225
            continue;
×
226
         }
227

228
         Botan::Certificate_Store_In_Memory store;
71✔
229

230
         store.add_certificate(root_cert);
71✔
231
         store.add_crl(root_crl);
71✔
232

233
         for(const auto& file : all_files) {
361✔
234
            if(file.find(".crt") != std::string::npos && file != "end.crt") {
290✔
235
               store.add_certificate(Botan::X509_Certificate(file));
181✔
236
            } else if(file.find(".crl") != std::string::npos) {
109✔
237
               Botan::DataSource_Stream in(file, true);
109✔
238
               Botan::X509_CRL crl(in);
109✔
239
               store.add_crl(crl);
109✔
240
            }
109✔
241
         }
242

243
         Botan::X509_Certificate end_user(test_dir + "/end.crt");
71✔
244

245
         // 1024 bit root cert
246
         Botan::Path_Validation_Restrictions restrictions(true, 80);
142✔
247

248
         Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
71✔
249
            end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
71✔
250

251
         result.test_eq(test_name + " path validation result", validation_result.result_string(), expected_result);
142✔
252
      } catch(std::exception& e) { result.test_failure(test_name, e.what()); }
173✔
253

254
      result.end_timer();
71✔
255
      results.push_back(result);
71✔
256
   }
71✔
257

258
   return results;
1✔
259
}
2✔
260

261
BOTAN_REGISTER_TEST("x509", "x509_path_nist", NIST_Path_Validation_Tests);
262

263
class Extended_Path_Validation_Tests final : public Test {
×
264
   public:
265
      std::vector<Test::Result> run() override;
266
};
267

268
std::vector<Test::Result> Extended_Path_Validation_Tests::run() {
1✔
269
   if(Botan::has_filesystem_impl() == false) {
1✔
270
      return {Test::Result::Note("Extended x509 path validation", "Skipping due to missing filesystem access")};
×
271
   }
272

273
   std::vector<Test::Result> results;
1✔
274

275
   const std::string extended_x509_test_dir = Test::data_dir() + "/x509/extended";
1✔
276

277
   std::map<std::string, std::string> expected = read_results(Test::data_file("x509/extended/expected.txt"));
2✔
278

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

281
   for(auto i = expected.begin(); i != expected.end(); ++i) {
4✔
282
      const std::string test_name = i->first;
3✔
283
      const std::string expected_result = i->second;
3✔
284

285
      const std::string test_dir = extended_x509_test_dir + "/" + test_name;
3✔
286

287
      Test::Result result("Extended X509 path validation");
3✔
288
      result.start_timer();
3✔
289

290
      const std::vector<std::string> all_files = Botan::get_files_recursive(test_dir);
3✔
291

292
      if(all_files.empty()) {
3✔
293
         result.test_failure("No test files found in " + test_dir);
×
294
         results.push_back(result);
×
295
         continue;
×
296
      }
297

298
      Botan::Certificate_Store_In_Memory store;
3✔
299

300
      for(const auto& file : all_files) {
13✔
301
         if(file.find(".crt") != std::string::npos && file != "end.crt") {
10✔
302
            store.add_certificate(Botan::X509_Certificate(file));
10✔
303
         }
304
      }
305

306
      Botan::X509_Certificate end_user(test_dir + "/end.crt");
3✔
307

308
      Botan::Path_Validation_Restrictions restrictions;
6✔
309
      Botan::Path_Validation_Result validation_result =
3✔
310
         Botan::x509_path_validate(end_user, restrictions, store, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
3✔
311

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

314
      result.end_timer();
3✔
315
      results.push_back(result);
3✔
316
   }
6✔
317

318
   return results;
1✔
319
}
2✔
320

321
BOTAN_REGISTER_TEST("x509", "x509_path_extended", Extended_Path_Validation_Tests);
322

323
class PSS_Path_Validation_Tests : public Test {
×
324
   public:
325
      std::vector<Test::Result> run() override;
326
};
327

328
std::vector<Test::Result> PSS_Path_Validation_Tests::run() {
1✔
329
   if(Botan::has_filesystem_impl() == false) {
1✔
330
      return {Test::Result::Note("RSA-PSS X509 signature validation", "Skipping due to missing filesystem access")};
×
331
   }
332

333
   std::vector<Test::Result> results;
1✔
334

335
   const std::string pss_x509_test_dir = Test::data_dir() + "/x509/pss_certs";
1✔
336

337
   std::map<std::string, std::string> expected = read_results(Test::data_file("x509/pss_certs/expected.txt"));
2✔
338

339
   std::map<std::string, std::string> validation_times =
1✔
340
      read_results(Test::data_file("x509/pss_certs/validation_times.txt"));
2✔
341

342
   auto validation_times_iter = validation_times.begin();
1✔
343
   for(auto i = expected.begin(); i != expected.end(); ++i) {
119✔
344
      const std::string test_name = i->first;
118✔
345
      const std::string expected_result = i->second;
118✔
346

347
      const std::string test_dir = pss_x509_test_dir + "/" + test_name;
118✔
348

349
      Test::Result result("RSA-PSS X509 signature validation");
118✔
350
      result.start_timer();
118✔
351

352
      const std::vector<std::string> all_files = Botan::get_files_recursive(test_dir);
118✔
353

354
      if(all_files.empty()) {
118✔
355
         result.test_failure("No test files found in " + test_dir);
×
356
         results.push_back(result);
×
357
         continue;
×
358
      }
359

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

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

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

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

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

392
         result.test_eq(test_name + " check_crl result",
12✔
393
                        Botan::Path_Validation_Result::status_string(Botan::PKIX::overall_status(crl_status)),
394
                        expected_result);
395
      } else if(end && root)  // CRT chain tests
118✔
396
      {
397
         // sha-1 is used
398
         Botan::Path_Validation_Restrictions restrictions(false, 80);
182✔
399

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

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

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

418
   return results;
1✔
419
}
2✔
420

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

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

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

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

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

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

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

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

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

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

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

457
   Botan::Certificate_Store_In_Memory empty;
1✔
458

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

631
class BSI_Path_Validation_Tests final : public Test
×
632

633
{
634
   public:
635
      std::vector<Test::Result> run() override;
636
};
637

638
std::vector<Test::Result> BSI_Path_Validation_Tests::run() {
1✔
639
   if(Botan::has_filesystem_impl() == false) {
1✔
640
      return {Test::Result::Note("BSI path validation", "Skipping due to missing filesystem access")};
×
641
   }
642

643
   std::vector<Test::Result> results;
1✔
644

645
   const std::string bsi_test_dir = Test::data_dir() + "/x509/bsi";
1✔
646

647
   const std::map<std::string, std::string> expected = read_results(Test::data_file("/x509/bsi/expected.txt"), '$');
2✔
648

649
   for(const auto& i : expected) {
54✔
650
      const std::string test_name = i.first;
53✔
651
      std::string expected_result = i.second;
53✔
652

653
      #if !defined(BOTAN_HAS_MD5)
654
      if(expected_result == "Hash function used is considered too weak for security")
655
         expected_result = "Certificate signed with unknown/unavailable algorithm";
656
      #endif
657

658
      const std::string test_dir = bsi_test_dir + "/" + test_name;
53✔
659

660
      Test::Result result("BSI path validation");
53✔
661
      result.start_timer();
53✔
662

663
      const std::vector<std::string> all_files = Botan::get_files_recursive(test_dir);
53✔
664

665
      if(all_files.empty()) {
53✔
666
         result.test_failure("No test files found in " + test_dir);
×
667
         results.push_back(result);
×
668
         continue;
×
669
      }
670

671
      Botan::Certificate_Store_In_Memory trusted;
53✔
672
      std::vector<Botan::X509_Certificate> certs;
53✔
673

674
      auto validation_time = Botan::calendar_point(2017, 8, 19, 12, 0, 0).to_std_timepoint();
53✔
675

676
      // By convention: if CRL is a substring if the directory name,
677
      // we need to check the CRLs
678
      bool use_crl = false;
53✔
679
      if(test_dir.find("CRL") != std::string::npos) {
53✔
680
         use_crl = true;
16✔
681
      }
682

683
      try {
53✔
684
         for(const auto& file : all_files) {
437✔
685
            // found a trust anchor
686
            if(file.find("TA") != std::string::npos) {
388✔
687
               trusted.add_certificate(Botan::X509_Certificate(file));
49✔
688
            }
689
            // found the target certificate. It needs to be at the front of certs
690
            else if(file.find("TC") != std::string::npos) {
339✔
691
               certs.insert(certs.begin(), Botan::X509_Certificate(file));
53✔
692
            }
693
            // found a certificate that might be part of a valid certificate chain to the trust anchor
694
            else if(file.find(".crt") != std::string::npos) {
286✔
695
               certs.push_back(Botan::X509_Certificate(file));
110✔
696
            } else if(file.find(".crl") != std::string::npos) {
231✔
697
               trusted.add_crl(Botan::X509_CRL(file));
28✔
698
            }
699
         }
700

701
         Botan::Path_Validation_Restrictions restrictions(use_crl, 79, use_crl);
98✔
702

703
         /*
704
          * Following the test document, the test are executed 16 times with
705
          * randomly chosen order of the available certificates. However, the target
706
          * certificate needs to stay in front.
707
          * For certain test, the order in which the certificates are given to
708
          * the validation function may be relevant, i.e. if issuer DNs are
709
          * ambiguous.
710
          */
711
         struct random_bit_generator {
49✔
712
               using result_type = size_t;
713

714
               static constexpr result_type min() { return 0; }
715

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

718
               result_type operator()() {
80✔
719
                  size_t s;
80✔
720
                  Test::rng().randomize(reinterpret_cast<uint8_t*>(&s), sizeof(s));
80✔
721
                  return s;
80✔
722
               }
723
         } rbg;
724

725
         for(size_t r = 0; r < 16; r++) {
833✔
726
            std::shuffle(++(certs.begin()), certs.end(), rbg);
784✔
727

728
            Botan::Path_Validation_Result validation_result = Botan::x509_path_validate(
784✔
729
               certs, restrictions, trusted, "", Botan::Usage_Type::UNSPECIFIED, validation_time);
784✔
730

731
            // We expect to be warned
732
            if(expected_result.find("Warning: ") == 0) {
784✔
733
               std::string stripped = expected_result.substr(std::string("Warning: ").size());
32✔
734
               bool found_warning = false;
32✔
735
               for(const auto& warning_set : validation_result.warnings()) {
128✔
736
                  for(const auto& warning : warning_set) {
128✔
737
                     std::string warning_str(Botan::to_string(warning));
32✔
738
                     if(stripped == warning_str) {
32✔
739
                        result.test_eq(test_name + " path validation result", warning_str, stripped);
32✔
740
                        found_warning = true;
32✔
741
                     }
742
                  }
32✔
743
               }
32✔
744
               if(!found_warning) {
32✔
745
                  result.test_failure(test_name, "Did not receive the expected warning: " + stripped);
×
746
               }
747
            } else {
32✔
748
               result.test_eq(
752✔
749
                  test_name + " path validation result", validation_result.result_string(), expected_result);
2,096✔
750
            }
751
         }
784✔
752
      }
49✔
753

754
      /* Some certificates are rejected when executing the X509_Certificate constructor
755
       * by throwing a Decoding_Error exception.
756
       */
757
      catch(const Botan::Exception& e) {
4✔
758
         if(e.error_type() == Botan::ErrorType::DecodingFailure) {
4✔
759
            result.test_eq(test_name + " path validation result", e.what(), expected_result);
12✔
760
         } else {
761
            result.test_failure(test_name, e.what());
×
762
         }
763
      }
4✔
764

765
      result.end_timer();
53✔
766
      results.push_back(result);
53✔
767
   }
152✔
768

769
   return results;
1✔
770
}
2✔
771

772
BOTAN_REGISTER_TEST("x509", "x509_path_bsi", BSI_Path_Validation_Tests);
773

774
class Path_Validation_With_OCSP_Tests final : public Test {
×
775
   public:
776
      static Botan::X509_Certificate load_test_X509_cert(const std::string& path) {
24✔
777
         return Botan::X509_Certificate(Test::data_file(path));
48✔
778
      }
779

780
      static std::optional<Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) {
12✔
781
         return Botan::OCSP::Response(Test::read_binary_data_file(path));
24✔
782
      }
783

784
      static Test::Result validate_with_ocsp_with_next_update_without_max_age() {
1✔
785
         Test::Result result("path check with ocsp with next_update w/o max_age");
1✔
786
         Botan::Certificate_Store_In_Memory trusted;
1✔
787

788
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
789

790
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
791
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
792
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
793
         trusted.add_certificate(trust_root);
1✔
794

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

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

799
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
800
                               const Botan::Certificate_Status_Code expected) {
801
            const auto path_result = Botan::x509_path_validate(cert_path,
8✔
802
                                                               restrictions,
4✔
803
                                                               trusted,
4✔
804
                                                               "",
805
                                                               Botan::Usage_Type::UNSPECIFIED,
806
                                                               valid_time,
807
                                                               std::chrono::milliseconds(0),
4✔
808
                                                               {ocsp});
4✔
809

810
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
12✔
811
                                     Botan::to_string(path_result.result()) + "'",
8✔
812
                                  path_result.result() == expected);
8✔
813
         };
4✔
814

815
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
816
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
817
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
818
                    Botan::Certificate_Status_Code::OK);
819
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
820
                    Botan::Certificate_Status_Code::OK);
821
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
822
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
823

824
         return result;
2✔
825
      }
1✔
826

827
      static Test::Result validate_with_ocsp_with_next_update_with_max_age() {
1✔
828
         Test::Result result("path check with ocsp with next_update with max_age");
1✔
829
         Botan::Certificate_Store_In_Memory trusted;
1✔
830

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

833
         auto ee = load_test_X509_cert("x509/ocsp/randombit.pem");
1✔
834
         auto ca = load_test_X509_cert("x509/ocsp/letsencrypt.pem");
1✔
835
         auto trust_root = load_test_X509_cert("x509/ocsp/identrust.pem");
1✔
836
         trusted.add_certificate(trust_root);
1✔
837

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

840
         auto ocsp = load_test_OCSP_resp("x509/ocsp/randombit_ocsp.der");
1✔
841

842
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
5✔
843
                               const Botan::Certificate_Status_Code expected) {
844
            const auto path_result = Botan::x509_path_validate(cert_path,
8✔
845
                                                               restrictions,
4✔
846
                                                               trusted,
4✔
847
                                                               "",
848
                                                               Botan::Usage_Type::UNSPECIFIED,
849
                                                               valid_time,
850
                                                               std::chrono::milliseconds(0),
4✔
851
                                                               {ocsp});
8✔
852

853
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
12✔
854
                                     Botan::to_string(path_result.result()) + "'",
8✔
855
                                  path_result.result() == expected);
8✔
856
         };
4✔
857

858
         check_path(Botan::calendar_point(2016, 11, 11, 12, 30, 0).to_std_timepoint(),
1✔
859
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
860
         check_path(Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
1✔
861
                    Botan::Certificate_Status_Code::OK);
862
         check_path(Botan::calendar_point(2016, 11, 20, 8, 30, 0).to_std_timepoint(),
1✔
863
                    Botan::Certificate_Status_Code::OK);
864
         check_path(Botan::calendar_point(2016, 11, 28, 8, 30, 0).to_std_timepoint(),
1✔
865
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
866

867
         return result;
2✔
868
      }
1✔
869

870
      static Test::Result validate_with_ocsp_without_next_update_without_max_age() {
1✔
871
         Test::Result result("path check with ocsp w/o next_update w/o max_age");
1✔
872
         Botan::Certificate_Store_In_Memory trusted;
1✔
873

874
         auto restrictions = Botan::Path_Validation_Restrictions(false, 110, false);
2✔
875

876
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
877
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
878
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
879

880
         trusted.add_certificate(trust_root);
1✔
881

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

884
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
885

886
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
887
                               const Botan::Certificate_Status_Code expected) {
888
            const auto path_result = Botan::x509_path_validate(cert_path,
6✔
889
                                                               restrictions,
3✔
890
                                                               trusted,
3✔
891
                                                               "",
892
                                                               Botan::Usage_Type::UNSPECIFIED,
893
                                                               valid_time,
894
                                                               std::chrono::milliseconds(0),
3✔
895
                                                               {ocsp});
6✔
896

897
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
9✔
898
                                     Botan::to_string(path_result.result()) + "'",
6✔
899
                                  path_result.result() == expected);
6✔
900
         };
3✔
901

902
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
903
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
904
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
905
                    Botan::Certificate_Status_Code::OK);
906
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(), Botan::Certificate_Status_Code::OK);
1✔
907

908
         return result;
2✔
909
      }
1✔
910

911
      static Test::Result validate_with_ocsp_without_next_update_with_max_age() {
1✔
912
         Test::Result result("path check with ocsp w/o next_update with max_age");
1✔
913
         Botan::Certificate_Store_In_Memory trusted;
1✔
914

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

917
         auto ee = load_test_X509_cert("x509/ocsp/patrickschmidt.pem");
1✔
918
         auto ca = load_test_X509_cert("x509/ocsp/bdrive_encryption.pem");
1✔
919
         auto trust_root = load_test_X509_cert("x509/ocsp/bdrive_root.pem");
1✔
920

921
         trusted.add_certificate(trust_root);
1✔
922

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

925
         auto ocsp = load_test_OCSP_resp("x509/ocsp/patrickschmidt_ocsp.der");
1✔
926

927
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
928
                               const Botan::Certificate_Status_Code expected) {
929
            const auto path_result = Botan::x509_path_validate(cert_path,
6✔
930
                                                               restrictions,
3✔
931
                                                               trusted,
3✔
932
                                                               "",
933
                                                               Botan::Usage_Type::UNSPECIFIED,
934
                                                               valid_time,
935
                                                               std::chrono::milliseconds(0),
3✔
936
                                                               {ocsp});
6✔
937

938
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
9✔
939
                                     Botan::to_string(path_result.result()) + "'",
6✔
940
                                  path_result.result() == expected);
6✔
941
         };
3✔
942

943
         check_path(Botan::calendar_point(2019, 5, 28, 7, 0, 0).to_std_timepoint(),
1✔
944
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
945
         check_path(Botan::calendar_point(2019, 5, 28, 7, 30, 0).to_std_timepoint(),
1✔
946
                    Botan::Certificate_Status_Code::OK);
947
         check_path(Botan::calendar_point(2019, 5, 28, 8, 0, 0).to_std_timepoint(),
1✔
948
                    Botan::Certificate_Status_Code::OCSP_IS_TOO_OLD);
949

950
         return result;
2✔
951
      }
1✔
952

953
      static Test::Result validate_with_ocsp_with_authorized_responder() {
1✔
954
         Test::Result result("path check with ocsp response from authorized responder certificate");
1✔
955
         Botan::Certificate_Store_In_Memory trusted;
1✔
956

957
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
958
                                                                 110,    // minimum key strength
959
                                                                 true);  // OCSP for all intermediates
2✔
960

961
         auto ee = load_test_X509_cert("x509/ocsp/bdr.pem");
1✔
962
         auto ca = load_test_X509_cert("x509/ocsp/bdr-int.pem");
1✔
963
         auto trust_root = load_test_X509_cert("x509/ocsp/bdr-root.pem");
1✔
964

965
         // These OCSP responses are signed by an authorized OCSP responder
966
         // certificate issued by `ca` and `trust_root` respectively. Note that
967
         // the responder certificates contain the "OCSP No Check" extension,
968
         // meaning that they themselves do not need a revocation check via OCSP.
969
         auto ocsp_ee = load_test_OCSP_resp("x509/ocsp/bdr-ocsp-resp.der");
1✔
970
         auto ocsp_ca = load_test_OCSP_resp("x509/ocsp/bdr-int-ocsp-resp.der");
1✔
971

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

975
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
976
                               const Botan::Certificate_Status_Code expected) {
977
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
978
                                                               restrictions,
3✔
979
                                                               trusted,
3✔
980
                                                               "",
981
                                                               Botan::Usage_Type::UNSPECIFIED,
982
                                                               valid_time,
983
                                                               std::chrono::milliseconds(0),
3✔
984
                                                               {ocsp_ee, ocsp_ca});
9✔
985

986
            return result.confirm(std::string("Status: '") + Botan::to_string(expected) + "' should match '" +
9✔
987
                                     Botan::to_string(path_result.result()) + "'",
6✔
988
                                  path_result.result() == expected);
6✔
989
         };
3✔
990

991
         check_path(Botan::calendar_point(2022, 9, 18, 16, 30, 0).to_std_timepoint(),
1✔
992
                    Botan::Certificate_Status_Code::OCSP_NOT_YET_VALID);
993
         check_path(Botan::calendar_point(2022, 9, 19, 16, 30, 0).to_std_timepoint(),
1✔
994
                    Botan::Certificate_Status_Code::OK);
995
         check_path(Botan::calendar_point(2022, 9, 20, 16, 30, 0).to_std_timepoint(),
1✔
996
                    Botan::Certificate_Status_Code::OCSP_HAS_EXPIRED);
997

998
         return result;
1✔
999
      }
3✔
1000

1001
      static Test::Result validate_with_ocsp_with_authorized_responder_without_keyusage() {
1✔
1002
         Test::Result result(
1✔
1003
            "path check with ocsp response from authorized responder certificate (without sufficient key usage)");
1✔
1004
         Botan::Certificate_Store_In_Memory trusted;
1✔
1005

1006
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1007
                                                                 110,     // minimum key strength
1008
                                                                 false);  // OCSP for all intermediates
2✔
1009

1010
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1011
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1012
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1013
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1014

1015
         auto ocsp_ee_delegate = load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed.der").value();
2✔
1016
         auto ocsp_ee_delegate_malformed =
1✔
1017
            load_test_OCSP_resp("x509/ocsp/mychain_ocsp_for_ee_delegate_signed_malformed.der").value();
2✔
1018

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

1022
         auto check_path = [&](const std::chrono::system_clock::time_point valid_time,
4✔
1023
                               const Botan::OCSP::Response& ocsp_ee,
1024
                               const Botan::Certificate_Status_Code expected,
1025
                               const std::optional<Botan::Certificate_Status_Code> also_expected = std::nullopt) {
1026
            const auto path_result = Botan::x509_path_validate(cert_path,
9✔
1027
                                                               restrictions,
3✔
1028
                                                               trusted,
3✔
1029
                                                               "",
1030
                                                               Botan::Usage_Type::UNSPECIFIED,
1031
                                                               valid_time,
1032
                                                               std::chrono::milliseconds(0),
3✔
1033
                                                               {ocsp_ee});
3✔
1034

1035
            result.test_is_eq("should result in expected validation status code", path_result.result(), expected);
3✔
1036
            if(also_expected) {
3✔
1037
               result.confirm("Secondary error is also present",
6✔
1038
                              flatten(path_result.all_statuses()).contains(also_expected.value()));
6✔
1039
            }
1040
         };
3✔
1041

1042
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1043
                    ocsp_ee_delegate,
1044
                    Botan::Certificate_Status_Code::VERIFIED);
1045
         check_path(Botan::calendar_point(2022, 10, 8, 23, 30, 0).to_std_timepoint(),
1✔
1046
                    ocsp_ee_delegate,
1047
                    Botan::Certificate_Status_Code::CERT_HAS_EXPIRED,
1048
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1049
         check_path(Botan::calendar_point(2022, 9, 22, 23, 30, 0).to_std_timepoint(),
1✔
1050
                    ocsp_ee_delegate_malformed,
1051
                    Botan::Certificate_Status_Code::OCSP_RESPONSE_MISSING_KEYUSAGE,
1052
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1✔
1053

1054
         return result;
1✔
1055
      }
1✔
1056

1057
      static Test::Result validate_with_forged_ocsp_using_self_signed_cert() {
1✔
1058
         Test::Result result("path check with forged ocsp using self-signed certificate");
1✔
1059
         Botan::Certificate_Store_In_Memory trusted;
1✔
1060

1061
         auto restrictions = Botan::Path_Validation_Restrictions(true,    // require revocation info
1✔
1062
                                                                 110,     // minimum key strength
1063
                                                                 false);  // OCSP for all intermediates
2✔
1064

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

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

1072
         auto check_path = [&](const std::string& forged_ocsp,
3✔
1073
                               const Botan::Certificate_Status_Code expected,
1074
                               const Botan::Certificate_Status_Code also_expected) {
1075
            auto ocsp = load_test_OCSP_resp(forged_ocsp);
2✔
1076
            const auto path_result =
2✔
1077
               Botan::x509_path_validate(cert_path,
4✔
1078
                                         restrictions,
2✔
1079
                                         trusted,
2✔
1080
                                         "",
1081
                                         Botan::Usage_Type::UNSPECIFIED,
1082
                                         Botan::calendar_point(2016, 11, 18, 12, 30, 0).to_std_timepoint(),
2✔
1083
                                         std::chrono::milliseconds(0),
2✔
1084
                                         {ocsp});
2✔
1085

1086
            result.test_is_eq(
2✔
1087
               "Path validation with forged OCSP response should fail with", path_result.result(), expected);
2✔
1088
            result.confirm("Secondary error is also present",
6✔
1089
                           flatten(path_result.all_statuses()).contains(also_expected));
4✔
1090
            result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
4✔
1091
         };
4✔
1092

1093
         // In both cases the path validation should detect the forged OCSP
1094
         // response and generate an appropriate error. By no means it should
1095
         // follow the unauthentic OCSP response.
1096
         check_path("x509/ocsp/randombit_ocsp_forged_valid.der",
1✔
1097
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1098
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1099
         check_path("x509/ocsp/randombit_ocsp_forged_revoked.der",
1✔
1100
                    Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND,
1101
                    Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_TRUSTED);
1102

1103
         return result;
1✔
1104
      }
1✔
1105

1106
      static Test::Result validate_with_ocsp_self_signed_by_intermediate_cert() {
1✔
1107
         Test::Result result(
1✔
1108
            "path check with ocsp response for intermediate that is (maliciously) self-signed by the intermediate");
1✔
1109
         Botan::Certificate_Store_In_Memory trusted;
1✔
1110

1111
         auto restrictions = Botan::Path_Validation_Restrictions(true,   // require revocation info
1✔
1112
                                                                 110,    // minimum key strength
1113
                                                                 true);  // OCSP for all intermediates
2✔
1114

1115
         // See `src/scripts/mychain_creater.sh` if you need to recreate those
1116
         auto ee = load_test_X509_cert("x509/ocsp/mychain_ee.pem");
1✔
1117
         auto ca = load_test_X509_cert("x509/ocsp/mychain_int.pem");
1✔
1118
         auto trust_root = load_test_X509_cert("x509/ocsp/mychain_root.pem");
1✔
1119

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

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

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

1129
         const auto path_result =
1✔
1130
            Botan::x509_path_validate(cert_path,
3✔
1131
                                      restrictions,
1132
                                      trusted,
1133
                                      "",
1134
                                      Botan::Usage_Type::UNSPECIFIED,
1135
                                      Botan::calendar_point(2022, 9, 22, 22, 30, 0).to_std_timepoint(),
1✔
1136
                                      std::chrono::milliseconds(0),
1✔
1137
                                      {ocsp_ee, ocsp_ca});
2✔
1138
         result.confirm("should reject intermediate OCSP response",
2✔
1139
                        path_result.result() == Botan::Certificate_Status_Code::OCSP_ISSUER_NOT_FOUND);
1✔
1140
         result.test_note(std::string("Failed with: ") + Botan::to_string(path_result.result()));
2✔
1141

1142
         return result;
1✔
1143
      }
3✔
1144

1145
      std::vector<Test::Result> run() override {
1✔
1146
         return {validate_with_ocsp_with_next_update_without_max_age(),
1✔
1147
                 validate_with_ocsp_with_next_update_with_max_age(),
1148
                 validate_with_ocsp_without_next_update_without_max_age(),
1149
                 validate_with_ocsp_without_next_update_with_max_age(),
1150
                 validate_with_ocsp_with_authorized_responder(),
1151
                 validate_with_ocsp_with_authorized_responder_without_keyusage(),
1152
                 validate_with_forged_ocsp_using_self_signed_cert(),
1153
                 validate_with_ocsp_self_signed_by_intermediate_cert()};
9✔
1154
      }
1155
};
1156

1157
BOTAN_REGISTER_TEST("x509", "x509_path_with_ocsp", Path_Validation_With_OCSP_Tests);
1158

1159
   #endif
1160

1161
   #if defined(BOTAN_HAS_ECDSA)
1162

1163
class CVE_2020_0601_Tests final : public Test {
×
1164
   public:
1165
      std::vector<Test::Result> run() override {
1✔
1166
         Test::Result result("CVE-2020-0601");
1✔
1167
         auto ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ca.pem"));
2✔
1168
         auto fake_ca_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/fake_ca.pem"));
2✔
1169
         auto ee_crt = Botan::X509_Certificate(Test::data_file("x509/cve-2020-0601/ee.pem"));
2✔
1170

1171
         Botan::Certificate_Store_In_Memory trusted;
1✔
1172
         trusted.add_certificate(ca_crt);
1✔
1173

1174
         const auto restrictions = Botan::Path_Validation_Restrictions(false, 80, false);
2✔
1175

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

1178
         const auto path_result1 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt, fake_ca_crt},
4✔
1179
                                                             restrictions,
1180
                                                             trusted,
1181
                                                             "",
1182
                                                             Botan::Usage_Type::UNSPECIFIED,
1183
                                                             valid_time,
1184
                                                             std::chrono::milliseconds(0),
1✔
1185
                                                             {});
3✔
1186

1187
         result.confirm("Validation failed", !path_result1.successful_validation());
2✔
1188

1189
         result.confirm("Expected status",
2✔
1190
                        path_result1.result() == Botan::Certificate_Status_Code::CANNOT_ESTABLISH_TRUST);
1✔
1191

1192
         const auto path_result2 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1193
                                                             restrictions,
1194
                                                             trusted,
1195
                                                             "",
1196
                                                             Botan::Usage_Type::UNSPECIFIED,
1197
                                                             valid_time,
1198
                                                             std::chrono::milliseconds(0),
1✔
1199
                                                             {});
3✔
1200

1201
         result.confirm("Validation failed", !path_result2.successful_validation());
2✔
1202

1203
         result.confirm("Expected status",
2✔
1204
                        path_result2.result() == Botan::Certificate_Status_Code::CERT_ISSUER_NOT_FOUND);
1✔
1205

1206
         // Verify the signature from the bad CA is actually correct
1207
         Botan::Certificate_Store_In_Memory frusted;
1✔
1208
         frusted.add_certificate(fake_ca_crt);
1✔
1209

1210
         const auto path_result3 = Botan::x509_path_validate(std::vector<Botan::X509_Certificate>{ee_crt},
3✔
1211
                                                             restrictions,
1212
                                                             frusted,
1213
                                                             "",
1214
                                                             Botan::Usage_Type::UNSPECIFIED,
1215
                                                             valid_time,
1216
                                                             std::chrono::milliseconds(0),
1✔
1217
                                                             {});
3✔
1218

1219
         result.confirm("Validation succeeded", path_result3.successful_validation());
2✔
1220

1221
         return {result};
3✔
1222
      }
1✔
1223
};
1224

1225
BOTAN_REGISTER_TEST("x509", "x509_cve_2020_0601", CVE_2020_0601_Tests);
1226

1227
   #endif
1228

1229
   #if defined(BOTAN_HAS_XMSS_RFC8391)
1230

1231
class XMSS_Path_Validation_Tests final : public Test {
×
1232
   public:
1233
      static Test::Result validate_self_signed(const std::string& name, const std::string& file) {
2✔
1234
         Test::Result result(name);
2✔
1235

1236
         Botan::Path_Validation_Restrictions restrictions;
4✔
1237
         auto self_signed = Botan::X509_Certificate(Test::data_dir() + "/x509/xmss/" + file);
4✔
1238

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

1242
         auto status = Botan::PKIX::overall_status(
2✔
1243
            Botan::PKIX::check_chain(cert_path, valid_time, "", Botan::Usage_Type::UNSPECIFIED, restrictions));
4✔
1244
         result.test_eq("Cert validation status", Botan::to_string(status), "Verified");
2✔
1245
         return result;
2✔
1246
      }
2✔
1247

1248
      std::vector<Test::Result> run() override {
1✔
1249
         if(Botan::has_filesystem_impl() == false) {
1✔
1250
            return {Test::Result::Note("XMSS path validation", "Skipping due to missing filesystem access")};
×
1251
         }
1252

1253
         return {
1✔
1254
            validate_self_signed("XMSS path validation with certificate created by ISARA corp", "xmss_isara_root.pem"),
1255
            validate_self_signed("XMSS path validation with certificate created by BouncyCastle",
1256
                                 "xmss_bouncycastle_sha256_10_root.pem")};
7✔
1257
      }
1258
};
1259

1260
BOTAN_REGISTER_TEST("x509", "x509_path_xmss", XMSS_Path_Validation_Tests);
1261

1262
   #endif
1263

1264
#endif
1265

1266
}
1267

1268
}
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

© 2025 Coveralls, Inc