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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
* (C) 2010,2014,2015,2018 Jack Lloyd
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "cli.h"
#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
#include <botan/certstor.h>
#include <botan/pk_keys.h>
#include <botan/pkcs8.h>
#include <botan/x509_ca.h>
#include <botan/x509cert.h>
#include <botan/x509path.h>
#include <botan/x509self.h>
#include <botan/data_src.h>
#include <botan/parsing.h>
#if defined(BOTAN_HAS_OCSP)
#include <botan/ocsp.h>
#endif
namespace Botan_CLI {
class Sign_Cert final : public Command
{
public:
Sign_Cert()
: Command("sign_cert --ca-key-pass= --hash=SHA-256 "
"--duration=365 --emsa= ca_cert ca_key pkcs10_req") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Create a CA-signed X.509 certificate from a PKCS #10 CSR";
}
void go() override
{
Botan::X509_Certificate ca_cert(get_arg("ca_cert"));
std::unique_ptr<Botan::Private_Key> key;
const std::string pass = get_arg("ca-key-pass");
const std::string emsa = get_arg("emsa");
const std::string hash = get_arg("hash");
if(!pass.empty())
{
key.reset(Botan::PKCS8::load_key(get_arg("ca_key"), rng(), pass));
}
else
{
key.reset(Botan::PKCS8::load_key(get_arg("ca_key"), rng()));
}
if(!key)
{
throw CLI_Error("Failed to load key from " + get_arg("ca_key"));
}
std::map<std::string, std::string> options;
if(emsa.empty() == false)
options["padding"] = emsa;
Botan::X509_CA ca(ca_cert, *key, options, hash, rng());
Botan::PKCS10_Request req(get_arg("pkcs10_req"));
auto now = std::chrono::system_clock::now();
Botan::X509_Time start_time(now);
typedef std::chrono::duration<int, std::ratio<86400>> days;
Botan::X509_Time end_time(now + days(get_arg_sz("duration")));
Botan::X509_Certificate new_cert = ca.sign_request(req, rng(), start_time, end_time);
output() << new_cert.PEM_encode();
}
};
BOTAN_REGISTER_COMMAND("sign_cert", Sign_Cert);
class Cert_Info final : public Command
{
public:
Cert_Info() : Command("cert_info --fingerprint file") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Parse X.509 certificate and display data fields";
}
void go() override
{
const std::string arg_file = get_arg("file");
std::vector<uint8_t> data = slurp_file(get_arg("file"));
Botan::DataSource_Memory in(data);
while(!in.end_of_data())
{
try
{
Botan::X509_Certificate cert(in);
try
{
output() << cert.to_string() << std::endl;
}
catch(Botan::Exception& e)
{
// to_string failed - report the exception and continue
output() << "X509_Certificate::to_string failed: " << e.what() << "\n";
}
if(flag_set("fingerprint"))
output() << "Fingerprint: " << cert.fingerprint("SHA-256") << std::endl;
}
catch(Botan::Exception& e)
{
if(!in.end_of_data())
{
output() << "X509_Certificate parsing failed " << e.what() << "\n";
}
}
}
}
};
BOTAN_REGISTER_COMMAND("cert_info", Cert_Info);
#if defined(BOTAN_HAS_OCSP) && defined(BOTAN_HAS_HTTP_UTIL)
class OCSP_Check final : public Command
{
public:
OCSP_Check() : Command("ocsp_check --timeout=3000 subject issuer") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Verify an X.509 certificate against the issuers OCSP responder";
}
void go() override
{
Botan::X509_Certificate subject(get_arg("subject"));
Botan::X509_Certificate issuer(get_arg("issuer"));
std::chrono::milliseconds timeout(get_arg_sz("timeout"));
Botan::Certificate_Store_In_Memory cas;
cas.add_certificate(issuer);
Botan::OCSP::Response resp = Botan::OCSP::online_check(issuer, subject, &cas, timeout);
auto status = resp.status_for(issuer, subject, std::chrono::system_clock::now());
if(status == Botan::Certificate_Status_Code::OCSP_RESPONSE_GOOD)
{
output() << "OCSP check OK\n";
}
else
{
output() << "OCSP check failed " << Botan::Path_Validation_Result::status_string(status) << "\n";
}
}
};
BOTAN_REGISTER_COMMAND("ocsp_check", OCSP_Check);
#endif // OCSP && HTTP
class Cert_Verify final : public Command
{
public:
Cert_Verify() : Command("cert_verify subject *ca_certs") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Verify if the passed X.509 certificate passes path validation";
}
void go() override
{
Botan::X509_Certificate subject_cert(get_arg("subject"));
Botan::Certificate_Store_In_Memory trusted;
for(auto const& certfile : get_arg_list("ca_certs"))
{
trusted.add_certificate(Botan::X509_Certificate(certfile));
}
Botan::Path_Validation_Restrictions restrictions;
Botan::Path_Validation_Result result =
Botan::x509_path_validate(subject_cert,
restrictions,
trusted);
if(result.successful_validation())
{
output() << "Certificate passes validation checks\n";
}
else
{
output() << "Certificate did not validate - " << result.result_string() << "\n";
}
}
};
BOTAN_REGISTER_COMMAND("cert_verify", Cert_Verify);
class Gen_Self_Signed final : public Command
{
public:
Gen_Self_Signed()
: Command("gen_self_signed key CN --country= --dns= "
"--organization= --email= --days=365 --key-pass= --ca --hash=SHA-256 --emsa= --der") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Generate a self signed X.509 certificate";
}
void go() override
{
std::unique_ptr<Botan::Private_Key> key(Botan::PKCS8::load_key(get_arg("key"), rng(), get_arg("key-pass")));
if(!key)
{
throw CLI_Error("Failed to load key from " + get_arg("key"));
}
const size_t days = get_arg_sz("days");
Botan::X509_Cert_Options opts("", days * 24 * 60 * 60);
opts.common_name = get_arg("CN");
opts.country = get_arg("country");
opts.organization = get_arg("organization");
opts.email = get_arg("email");
opts.more_dns = Botan::split_on(get_arg("dns"), ',');
const bool der_format = flag_set("der");
std::string emsa = get_arg("emsa");
if(emsa.empty() == false)
opts.set_padding_scheme(emsa);
if(flag_set("ca"))
{
opts.CA_key();
}
Botan::X509_Certificate cert = Botan::X509::create_self_signed_cert(opts, *key, get_arg("hash"), rng());
if(der_format)
{
auto der = cert.BER_encode();
output().write(reinterpret_cast<const char*>(der.data()), der.size());
}
else
output() << cert.PEM_encode();
}
};
BOTAN_REGISTER_COMMAND("gen_self_signed", Gen_Self_Signed);
class Generate_PKCS10 final : public Command
{
public:
Generate_PKCS10()
: Command("gen_pkcs10 key CN --country= --organization= "
"--email= --dns= --ext-ku= --key-pass= --hash=SHA-256 --emsa=") {}
std::string group() const override
{
return "x509";
}
std::string description() const override
{
return "Generate a PKCS #10 certificate signing request (CSR)";
}
void go() override
{
std::unique_ptr<Botan::Private_Key> key(Botan::PKCS8::load_key(get_arg("key"), rng(), get_arg("key-pass")));
if(!key)
{
throw CLI_Error("Failed to load key from " + get_arg("key"));
}
Botan::X509_Cert_Options opts;
opts.common_name = get_arg("CN");
opts.country = get_arg("country");
opts.organization = get_arg("organization");
opts.email = get_arg("email");
opts.more_dns = Botan::split_on(get_arg("dns"), ',');
for(std::string ext_ku : Botan::split_on(get_arg("ext-ku"), ','))
{
opts.add_ex_constraint(ext_ku);
}
std::string emsa = get_arg("emsa");
if(emsa.empty() == false)
opts.set_padding_scheme(emsa);
Botan::PKCS10_Request req = Botan::X509::create_cert_req(opts, *key, get_arg("hash"), rng());
output() << req.PEM_encode();
}
};
BOTAN_REGISTER_COMMAND("gen_pkcs10", Generate_PKCS10);
}
#endif
|