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

randombit / botan / 5356326050

23 Jun 2023 01:05PM UTC coverage: 91.728% (-0.008%) from 91.736%
5356326050

Pull #3595

github

web-flow
Merge a5b917599 into 92171c524
Pull Request #3595: Improve clang-tidy coverage

78163 of 85212 relevant lines covered (91.73%)

12690161.35 hits per line

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

79.48
/src/tests/test_certstor.cpp
1
/*
2
* (C) 2016 Kai Michaelis, Rohde & Schwarz Cybersecurity
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_X509_CERTIFICATES)
10
   #include <botan/certstor.h>
11
   #include <botan/pk_keys.h>
12
   #include <botan/pkcs8.h>
13
   #include <botan/pkix_types.h>
14
   #include <botan/x509cert.h>
15
   #include <botan/internal/filesystem.h>
16

17
   #if defined(BOTAN_HAS_CERTSTOR_SQLITE3)
18
      #include <botan/certstor_sqlite.h>
19
      #include <botan/sqlite3.h>
20
   #endif
21
#endif
22

23
namespace Botan_Tests {
24

25
namespace {
26

27
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_HAS_RSA) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
28

29
class CertificateAndKey {
13✔
30
   public:
31
      CertificateAndKey(const Botan::X509_Certificate& cert, std::shared_ptr<Botan::Private_Key> key) :
6✔
32
            m_certificate(cert), m_private_key(std::move(key)) {}
6✔
33

34
      bool operator!=(const CertificateAndKey& rhs) const {
9✔
35
         if(m_certificate != rhs.m_certificate) {
9✔
36
            return false;
37
         }
38
         // XXX: this is comparing the pointers, is that really correct?
39
         if(m_private_key != rhs.m_private_key) {
2✔
40
            return false;
41
         }
42
         return true;
43
      }
44

45
      const Botan::X509_DN& subject_dn() const { return certificate().subject_dn(); }
32✔
46

47
      const Botan::X509_Certificate& certificate() const { return m_certificate; }
13✔
48

49
      const Botan::Private_Key& private_key() const { return *m_private_key; }
12✔
50

51
   private:
52
      const Botan::X509_Certificate m_certificate;
53
      const std::shared_ptr<Botan::Private_Key> m_private_key;
54
};
55

56
   #if defined(BOTAN_HAS_CERTSTOR_SQLITE3)
57
Test::Result test_certstor_sqlite3_insert_find_remove_test(const std::vector<CertificateAndKey>& certsandkeys) {
1✔
58
   Test::Result result("Certificate Store SQLITE3 - Insert, Find, Remove");
1✔
59

60
   try {
1✔
61
      auto& rng = Test::rng();
1✔
62
      const std::string passwd(reinterpret_cast<const char*>(rng.random_vec(8).data()), 8);
3✔
63
      // Just create a database in memory for testing (https://sqlite.org/inmemorydb.html)
64
      Botan::Certificate_Store_In_SQLite store(":memory:", passwd, rng);
1✔
65

66
      for(const auto& a : certsandkeys) {
7✔
67
         store.insert_key(a.certificate(), a.private_key());
6✔
68
      }
69

70
      for(const auto& certandkey : certsandkeys) {
7✔
71
         const auto& cert = certandkey.certificate();
6✔
72
         const auto& key = certandkey.private_key();
6✔
73
         const auto wo_keyid = store.find_cert(cert.subject_dn(), {});
6✔
74
         const auto w_keyid = store.find_cert(cert.subject_dn(), cert.subject_key_id());
6✔
75

76
         if(!wo_keyid || !w_keyid) {
6✔
77
            result.test_failure("Can't retrieve certificate");
×
78
            return result;
×
79
         }
80

81
         const auto priv = store.find_key(cert);
6✔
82
         if(!priv && (certsandkeys[1] != certandkey && certsandkeys[0] != certandkey)) {
7✔
83
            result.test_failure("Can't retrieve private key for " + cert.fingerprint("SHA-1"));
×
84
            return result;
×
85
         }
86

87
         result.test_eq("Got wrong certificate", cert.fingerprint(), w_keyid->fingerprint());
18✔
88

89
         if(priv) {
6✔
90
            result.test_eq("Got wrong private key", key.private_key_bits(), priv->private_key_bits());
15✔
91

92
            const auto rev_certs = store.find_certs_for_key(*priv);
5✔
93

94
            if(rev_certs.empty()) {
5✔
95
               result.test_failure("No certificate");
×
96
            } else {
97
               const bool found =
5✔
98
                  std::any_of(rev_certs.begin(), rev_certs.end(), [&](const Botan::X509_Certificate& c) {
5✔
99
                     return c.fingerprint() == cert.fingerprint();
15✔
100
                  });
101

102
               result.test_eq("Got wrong/no certificate", found, true);
10✔
103
            }
104
         }
5✔
105

106
         if(certsandkeys[4] != certandkey && certsandkeys[5] != certandkey) {
7✔
107
            result.test_eq("Got wrong certificate", cert.fingerprint(), wo_keyid->fingerprint());
×
108
         }
109

110
         result.test_eq("Can't remove certificate", store.remove_cert(cert), true);
6✔
111
         result.test_eq("Can't remove certificate", !store.find_cert(cert.subject_dn(), cert.subject_key_id()), true);
12✔
112

113
         if(priv) {
6✔
114
            store.remove_key(key);
5✔
115
         }
116

117
         result.test_eq("Can't remove key", !store.find_key(cert), true);
12✔
118
      }
18✔
119

120
      return result;
121
   } catch(std::exception& e) {
1✔
122
      result.test_failure(e.what());
×
123
      return result;
×
124
   }
×
125
}
×
126

127
Test::Result test_certstor_sqlite3_crl_test(const std::vector<CertificateAndKey>& certsandkeys) {
1✔
128
   Test::Result result("Certificate Store SQLITE3 - CRL");
1✔
129
   try {
1✔
130
      auto& rng = Test::rng();
1✔
131
      const std::string passwd(reinterpret_cast<const char*>(rng.random_vec(8).data()), 8);
3✔
132
      // Just create a database in memory for testing (https://sqlite.org/inmemorydb.html)
133
      Botan::Certificate_Store_In_SQLite store(":memory:", passwd, rng);
1✔
134

135
      for(const auto& a : certsandkeys) {
7✔
136
         store.insert_cert(a.certificate());
6✔
137
      }
138

139
      store.revoke_cert(certsandkeys[0].certificate(), Botan::CRL_Code::CaCompromise);
1✔
140
      store.revoke_cert(certsandkeys[3].certificate(), Botan::CRL_Code::CaCompromise);
1✔
141
      store.revoke_cert(certsandkeys[3].certificate(), Botan::CRL_Code::CaCompromise);
1✔
142

143
      {
1✔
144
         const auto crls = store.generate_crls();
1✔
145

146
         result.test_eq("Can't revoke certificate", crls.size(), 2);
1✔
147
         result.test_eq(
2✔
148
            "Can't revoke certificate",
149
            crls[0].is_revoked(certsandkeys[0].certificate()) ^ crls[1].is_revoked(certsandkeys[0].certificate()),
1✔
150
            true);
151
         result.test_eq(
2✔
152
            "Can't revoke certificate",
153
            crls[0].is_revoked(certsandkeys[3].certificate()) ^ crls[1].is_revoked(certsandkeys[3].certificate()),
1✔
154
            true);
155
      }
1✔
156

157
      store.affirm_cert(certsandkeys[3].certificate());
1✔
158

159
      {
1✔
160
         const auto crls = store.generate_crls();
1✔
161

162
         result.test_eq("Can't revoke certificate, wrong crl size", crls.size(), 1);
1✔
163
         result.test_eq(
1✔
164
            "Can't revoke certificate, cert 0 not revoked", crls[0].is_revoked(certsandkeys[0].certificate()), true);
1✔
165
      }
1✔
166

167
      const auto cert0_crl = store.find_crl_for(certsandkeys[0].certificate());
1✔
168

169
      result.test_eq("Can't revoke certificate, crl for cert 0", !cert0_crl, false);
1✔
170
      result.test_eq("Can't revoke certificate, crl for cert 0 size check", cert0_crl->get_revoked().size(), 1);
1✔
171
      result.test_eq(
1✔
172
         "Can't revoke certificate, no crl for cert 0", cert0_crl->is_revoked(certsandkeys[0].certificate()), true);
1✔
173

174
      const auto cert3_crl = store.find_crl_for(certsandkeys[3].certificate());
1✔
175

176
      result.test_eq("Can't revoke certificate, crl for cert 3", !cert3_crl, true);
1✔
177

178
      return result;
1✔
179
   } catch(std::exception& e) {
2✔
180
      result.test_failure(e.what());
×
181
      return result;
×
182
   }
×
183
}
×
184

185
Test::Result test_certstor_sqlite3_all_subjects_test(const std::vector<CertificateAndKey>& certsandkeys) {
1✔
186
   Test::Result result("Certificate Store SQLITE3 - All subjects");
1✔
187
   try {
1✔
188
      auto& rng = Test::rng();
1✔
189
      const std::string passwd(reinterpret_cast<const char*>(rng.random_vec(8).data()), 8);
3✔
190
      // Just create a database in memory for testing (https://sqlite.org/inmemorydb.html)
191
      Botan::Certificate_Store_In_SQLite store(":memory:", passwd, rng);
1✔
192

193
      for(const auto& a : certsandkeys) {
7✔
194
         store.insert_cert(a.certificate());
6✔
195
      }
196

197
      const auto subjects = store.all_subjects();
1✔
198

199
      result.test_eq("Check subject list length", subjects.size(), 6);
1✔
200

201
      for(const auto& sub : subjects) {
7✔
202
         const std::string ss = sub.to_string();
6✔
203

204
         result.test_eq("Check subject " + ss,
18✔
205
                        certsandkeys[0].subject_dn() == sub || certsandkeys[1].subject_dn() == sub ||
15✔
206
                           certsandkeys[2].subject_dn() == sub || certsandkeys[3].subject_dn() == sub ||
9✔
207
                           certsandkeys[4].subject_dn() == sub || certsandkeys[5].subject_dn() == sub,
8✔
208
                        true);
209
      }
6✔
210
      return result;
1✔
211
   } catch(std::exception& e) {
1✔
212
      result.test_failure(e.what());
×
213
      return result;
×
214
   }
×
215
}
×
216

217
Test::Result test_certstor_sqlite3_find_all_certs_test(const std::vector<CertificateAndKey>& certsandkeys) {
1✔
218
   Test::Result result("Certificate Store SQLITE3 - Find all certs");
1✔
219
   try {
1✔
220
      auto& rng = Test::rng();
1✔
221
      const std::string passwd(reinterpret_cast<const char*>(rng.random_vec(8).data()), 8);
3✔
222
      // Just create a database in memory for testing (https://sqlite.org/inmemorydb.html)
223
      Botan::Certificate_Store_In_SQLite store(":memory:", passwd, rng);
1✔
224

225
      for(const auto& a : certsandkeys) {
7✔
226
         store.insert_cert(a.certificate());
6✔
227
      }
228

229
      for(const auto& a : certsandkeys) {
7✔
230
         auto res_vec = store.find_all_certs(a.subject_dn(), a.certificate().subject_key_id());
6✔
231
         if(res_vec.size() != 1) {
6✔
232
            result.test_failure("SQLITE all lookup error");
×
233
            return result;
×
234
         } else {
235
            const std::string a_str = a.subject_dn().to_string();
6✔
236
            const std::string res_str = res_vec.at(0).subject_dn().to_string();
6✔
237
            result.test_eq("Check subject " + a_str, a_str, res_str);
12✔
238
         }
6✔
239
      }
6✔
240

241
      Botan::X509_Certificate same_dn_1 =
1✔
242
         Botan::X509_Certificate(Test::data_file("x509/bsi/common_14/common_14_sub_ca.ca.pem.crt"));
2✔
243
      Botan::X509_Certificate same_dn_2 =
1✔
244
         Botan::X509_Certificate(Test::data_file("x509/bsi/common_14/common_14_wrong_sub_ca.ca.pem.crt"));
2✔
245

246
      store.insert_cert(same_dn_1);
1✔
247
      store.insert_cert(same_dn_2);
1✔
248
      auto res_vec = store.find_all_certs(same_dn_1.subject_dn(), {});
1✔
249

250
      if(res_vec.size() != 2) {
1✔
251
         result.test_failure("SQLITE all lookup error (duplicate) " + std::to_string(res_vec.size()));
×
252
         return result;
×
253
      } else {
254
         const std::string cert_dn = same_dn_1.subject_dn().to_string();
1✔
255
         const std::string res0_dn = res_vec.at(0).subject_dn().to_string();
1✔
256

257
         result.test_eq("Check subject " + cert_dn, cert_dn, res0_dn);
1✔
258

259
         const std::string res1_dn = res_vec.at(1).subject_dn().to_string();
1✔
260
         result.test_eq("Check subject " + cert_dn, cert_dn, res1_dn);
2✔
261
      }
2✔
262
   } catch(const std::exception& e) {
1✔
263
      result.test_failure(e.what());
×
264
      return result;
×
265
   }
×
266
   return result;
267
}
×
268

269
   #endif
270

271
Test::Result test_certstor_find_hash_subject(const std::vector<CertificateAndKey>& certsandkeys) {
1✔
272
   Test::Result result("Certificate Store - Find by subject hash");
1✔
273

274
   try {
1✔
275
      Botan::Certificate_Store_In_Memory store;
1✔
276

277
      for(const auto& a : certsandkeys) {
7✔
278
         store.add_certificate(a.certificate());
6✔
279
      }
280

281
      for(const auto& certandkey : certsandkeys) {
7✔
282
         const auto& cert = certandkey.certificate();
6✔
283
         const auto hash = cert.raw_subject_dn_sha256();
6✔
284

285
         const auto found = store.find_cert_by_raw_subject_dn_sha256(hash);
6✔
286
         if(!found) {
6✔
287
            result.test_failure("Can't retrieve certificate " + cert.fingerprint("SHA-1"));
×
288
            return result;
×
289
         }
290

291
         result.test_eq("Got wrong certificate", hash, found->raw_subject_dn_sha256());
18✔
292
      }
12✔
293

294
      const auto found = store.find_cert_by_raw_subject_dn_sha256(std::vector<uint8_t>(32, 0));
1✔
295
      if(found) {
1✔
296
         result.test_failure("Certificate found for dummy hash");
×
297
         return result;
×
298
      }
299

300
      return result;
301
   } catch(std::exception& e) {
1✔
302
      result.test_failure(e.what());
×
303
      return result;
×
304
   }
×
305
}
×
306

307
Test::Result test_certstor_load_allcert() {
1✔
308
   Test::Result result("Certificate Store - Load every cert of every files");
1✔
309
   // test_dir_bundled dir should contain only one file with 2 certificates
310
   // concatenated (ValidCert and root)
311
   const std::string test_dir_bundled = Test::data_dir() + "/x509/misc/bundledcertdir";
1✔
312

313
   try {
1✔
314
      result.test_note("load certs from dir: " + test_dir_bundled);
1✔
315
      // Certificate_Store_In_Memory constructor loads every cert of every files of the dir.
316
      Botan::Certificate_Store_In_Memory store(test_dir_bundled);
1✔
317

318
      // X509_Certificate constructor loads only the first certificate found in the file.
319
      Botan::X509_Certificate root_cert(Test::data_dir() + "/x509/x509test/root.pem");
1✔
320
      Botan::X509_Certificate valid_cert(Test::data_dir() + "/x509/x509test/ValidCert.pem");
1✔
321
      std::vector<uint8_t> key_id;
1✔
322
      result.confirm("Root cert found", store.find_cert(root_cert.subject_dn(), key_id) != std::nullopt);
2✔
323
      result.confirm("ValidCert found", store.find_cert(valid_cert.subject_dn(), key_id) != std::nullopt);
2✔
324
      return result;
1✔
325
   } catch(std::exception& e) {
1✔
326
      result.test_failure(e.what());
×
327
      return result;
×
328
   }
×
329
}
1✔
330

331
class Certstor_Tests final : public Test {
×
332
   public:
333
      std::vector<Test::Result> run() override {
1✔
334
         if(Botan::has_filesystem_impl() == false) {
1✔
335
            return {Test::Result::Note("Certificate Store", "Skipping due to missing filesystem access")};
×
336
         }
337

338
         const std::string test_dir = Test::data_dir() + "/x509/certstor";
1✔
339

340
         struct CertificateAndKeyFilenames {
1✔
341
               const std::string certificate;
342
               const std::string private_key;
343
         } const certsandkeys_filenames[]{
344
            {"cert1.crt", "key01.pem"},
345
            {"cert2.crt", "key01.pem"},
346
            {"cert3.crt", "key03.pem"},
347
            {"cert4.crt", "key04.pem"},
348
            {"cert5a.crt", "key05.pem"},
349
            {"cert5b.crt", "key06.pem"},
350
         };
7✔
351

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

354
         if(all_files.empty()) {
1✔
355
            Test::Result result("Certificate Store");
×
356
            result.test_failure("No test files found in " + test_dir);
×
357
            return {result};
×
358
         }
×
359

360
         std::vector<CertificateAndKey> certsandkeys;
1✔
361

362
         for(const auto& certandkey_filenames : certsandkeys_filenames) {
7✔
363
            const Botan::X509_Certificate certificate(test_dir + "/" + certandkey_filenames.certificate);
12✔
364

365
            Botan::DataSource_Stream key_stream(test_dir + "/" + certandkey_filenames.private_key);
12✔
366
            std::shared_ptr<Botan::Private_Key> private_key = Botan::PKCS8::load_key(key_stream);
12✔
367

368
            if(!private_key) {
6✔
369
               Test::Result result("Certificate Store");
×
370
               result.test_failure("Failed to load key from disk at path: " + test_dir + "/" +
×
371
                                   certandkey_filenames.private_key);
372
               return {result};
×
373
            }
×
374

375
            certsandkeys.push_back(CertificateAndKey(certificate, private_key));
6✔
376
         }
6✔
377

378
         std::vector<Test::Result> results;
1✔
379

380
         results.push_back(test_certstor_find_hash_subject(certsandkeys));
2✔
381
         results.push_back(test_certstor_load_allcert());
2✔
382
   #if defined(BOTAN_HAS_CERTSTOR_SQLITE3)
383
         results.push_back(test_certstor_sqlite3_insert_find_remove_test(certsandkeys));
2✔
384
         results.push_back(test_certstor_sqlite3_crl_test(certsandkeys));
2✔
385
         results.push_back(test_certstor_sqlite3_all_subjects_test(certsandkeys));
2✔
386
         results.push_back(test_certstor_sqlite3_find_all_certs_test(certsandkeys));
2✔
387
   #endif
388
         return results;
1✔
389
      }
8✔
390
};
391

392
BOTAN_REGISTER_TEST("x509", "certstor", Certstor_Tests);
393
#endif
394
}  // namespace
395
}  // 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

© 2025 Coveralls, Inc