1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/*
* (C) 1999-2019 Jack Lloyd
* (C) 2019 René Meusel
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_CERTSTOR_FLATFILE)
#include "test_certstor_utils.h"
#include <botan/certstor_flatfile.h>
#include <botan/ber_dec.h>
#include <botan/der_enc.h>
#include <botan/hex.h>
namespace Botan_Tests {
namespace {
std::string get_valid_ca_bundle_path()
{
return Test::data_file("x509/misc/certstor/valid_ca_bundle.pem");
}
std::string get_ca_bundle_containing_user_cert()
{
return Test::data_file("x509/misc/certstor/ca_bundle_containing_non_ca.pem");
}
Test::Result open_certificate_store()
{
Test::Result result("Flatfile Certificate Store - Open Store");
try
{
result.start_timer();
Botan::Flatfile_Certificate_Store unused(get_valid_ca_bundle_path());
result.end_timer();
result.test_gt("found some certificates", unused.all_subjects().size(), 0);
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
result.test_success();
return result;
}
Test::Result find_certificate_by_pubkey_sha1()
{
Test::Result result("Flatfile Certificate Store - Find Certificate by SHA1(pubkey)");
try
{
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto cert = certstore.find_cert_by_pubkey_sha1(get_key_id());
result.end_timer();
if(result.test_not_null("found certificate", cert.get()))
{
auto cns = cert->subject_dn().get_attribute("CN");
result.test_int_eq("exactly one CN", cns.size(), 1);
result.test_eq("CN", cns.front(), "DST Root CA X3");
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
result.test_throws("on invalid SHA1 hash data", [&]
{
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
certstore.find_cert_by_pubkey_sha1({});
});
return result;
}
Test::Result find_cert_by_subject_dn()
{
Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN");
try
{
auto dn = get_dn();
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
result.end_timer();
if(result.test_not_null("found certificate", cert.get()))
{
auto cns = cert->subject_dn().get_attribute("CN");
result.test_int_eq("exactly one CN", cns.size(), 1);
result.test_eq("CN", cns.front(), "DST Root CA X3");
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result find_cert_by_utf8_subject_dn()
{
Test::Result result("Flatfile Certificate Store - Find Certificate by UTF8 subject DN");
try
{
auto dn = get_utf8_dn();
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto cert = certstore.find_cert(dn, std::vector<uint8_t>());
result.end_timer();
if(result.test_not_null("found certificate", cert.get()))
{
auto cns = cert->subject_dn().get_attribute("CN");
result.test_is_eq("exactly one CN", cns.size(), size_t(1));
result.test_eq("CN", cns.front(), "D-TRUST Root Class 3 CA 2 EV 2009");
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result find_cert_by_subject_dn_and_key_id()
{
Test::Result result("Flatfile Certificate Store - Find Certificate by subject DN and key ID");
try
{
auto dn = get_dn();
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto cert = certstore.find_cert(dn, get_key_id());
result.end_timer();
if(result.test_not_null("found certificate", cert.get()))
{
auto cns = cert->subject_dn().get_attribute("CN");
result.test_int_eq("exactly one CN", cns.size(), 1);
result.test_eq("CN", cns.front(), "DST Root CA X3");
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result find_certs_by_subject_dn_and_key_id()
{
Test::Result result("Flatfile Certificate Store - Find Certificates by subject DN and key ID");
try
{
auto dn = get_dn();
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto certs = certstore.find_all_certs(dn, get_key_id());
result.end_timer();
if(result.confirm("result not empty", !certs.empty()) &&
result.test_eq("exactly one certificate", certs.size(), 1))
{
auto cns = certs.front()->subject_dn().get_attribute("CN");
result.test_int_eq("exactly one CN", cns.size(), 1);
result.test_eq("CN", cns.front(), "DST Root CA X3");
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result find_all_subjects()
{
Test::Result result("Flatfile Certificate Store - Find all Certificate Subjects");
try
{
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto subjects = certstore.all_subjects();
result.end_timer();
if(result.confirm("result not empty", !subjects.empty()))
{
auto dn = get_dn();
auto needle = std::find_if(subjects.cbegin(),
subjects.cend(),
[=](const Botan::X509_DN &subject)
{
return subject == dn;
});
if(result.confirm("found expected certificate", needle != subjects.end()))
{
result.confirm("expected certificate", *needle == dn);
}
}
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result no_certificate_matches()
{
Test::Result result("Flatfile Certificate Store - can deal with no matches (regression test)");
try
{
auto dn = get_unknown_dn();
auto kid = get_unknown_key_id();
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_valid_ca_bundle_path());
auto certs = certstore.find_all_certs(dn, kid);
auto cert = certstore.find_cert(dn, kid);
auto pubk_cert = certstore.find_cert_by_pubkey_sha1(kid);
result.end_timer();
result.confirm("find_all_certs did not find the dummy", certs.empty());
result.confirm("find_cert did not find the dummy", !cert);
result.confirm("find_cert_by_pubkey_sha1 did not find the dummy", !pubk_cert);
}
catch(std::exception& e)
{
result.test_failure(e.what());
}
return result;
}
Test::Result certstore_contains_user_certificate()
{
Test::Result result("Flatfile Certificate Store - rejects bundles with non-CA certs");
try
{
result.start_timer();
Botan::Flatfile_Certificate_Store certstore(get_ca_bundle_containing_user_cert());
result.test_failure("CA bundle with non-CA certs should be rejected");
}
catch(Botan::Invalid_Argument&)
{
result.test_success();
}
return result;
}
class Certstor_Flatfile_Tests final : public Test
{
public:
std::vector<Test::Result> run() override
{
std::vector<Test::Result> results;
results.push_back(open_certificate_store());
results.push_back(find_certificate_by_pubkey_sha1());
results.push_back(find_cert_by_subject_dn());
results.push_back(find_cert_by_utf8_subject_dn());
results.push_back(find_cert_by_subject_dn_and_key_id());
results.push_back(find_certs_by_subject_dn_and_key_id());
results.push_back(find_all_subjects());
results.push_back(no_certificate_matches());
results.push_back(certstore_contains_user_certificate());
return results;
}
};
BOTAN_REGISTER_TEST("certstor_flatfile", Certstor_Flatfile_Tests);
}
}
#endif
|