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

randombit / botan / 21227550842

21 Jan 2026 10:11PM UTC coverage: 90.091% (-0.3%) from 90.4%
21227550842

push

github

web-flow
Merge pull request #5240 from randombit/jack/coverage-build-speedups

Some changes to reduce time taken by the coverage build

102081 of 113309 relevant lines covered (90.09%)

11632809.03 hits per line

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

73.54
/src/cli/pubkey.cpp
1
/*
2
* (C) 2010,2014,2015,2019 Jack Lloyd
3
* (C) 2019 Matthias Gierlings
4
* (C) 2015 René Korthaus
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "cli.h"
10

11
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
12

13
   #include <botan/base64.h>
14
   #include <botan/data_src.h>
15
   #include <botan/hash.h>
16
   #include <botan/hex.h>
17
   #include <botan/pk_algs.h>
18
   #include <botan/pk_keys.h>
19
   #include <botan/pkcs8.h>
20
   #include <botan/pubkey.h>
21
   #include <botan/x509_key.h>
22
   #include <botan/internal/workfactor.h>
23
   #include <fstream>
24
   #include <sstream>
25

26
   #if defined(BOTAN_HAS_DL_GROUP)
27
      #include <botan/dl_group.h>
28
   #endif
29

30
   #if defined(BOTAN_HAS_ECC_GROUP)
31
      #include <botan/ec_group.h>
32
   #endif
33

34
namespace Botan_CLI {
35

36
class PK_Keygen final : public Command {
37
   public:
38
      PK_Keygen() :
20 ✔
39
            Command(
40
               "keygen --algo=RSA --params= --passphrase= --cipher= --pbkdf= --pbkdf-ms=300 --pbkdf-iter= --provider= --rng-type= --drbg-seed= --der-out") {
20 ✔
41
      }
20 ✔
42

43
      std::string group() const override { return "pubkey"; }
1 ✔
44

45
      std::string description() const override { return "Generate a PKCS #8 private key"; }
1 ✔
46

47
      void go() override {
19 ✔
48
         const std::string algo = get_arg("algo");
19 ✔
49
         const std::string params = get_arg("params");
19 ✔
50
         const std::string provider = get_arg("provider");
19 ✔
51

52
         const std::unique_ptr<Botan::Private_Key> key = Botan::create_private_key(algo, rng(), params, provider);
19 ✔
53

54
         if(!key) {
19 ✔
55
            throw CLI_Error_Unsupported("keygen", algo);
×
56
         }
57

58
         const std::string pass = get_passphrase_arg("Key passphrase", "passphrase");
38 ✔
59
         const bool der_out = flag_set("der-out");
19 ✔
60

61
         const std::chrono::milliseconds pbkdf_ms(get_arg_sz("pbkdf-ms"));
19 ✔
62

63
         if(der_out) {
19 ✔
64
            if(pass.empty()) {
×
65
               write_output(Botan::PKCS8::BER_encode(*key));
×
66
            } else {
67
               if(get_arg("pbkdf-iter").empty()) {
×
68
                  write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(
×
69
                     *key, rng(), pass, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf")));
×
70
               } else {
71
                  write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(
×
72
                     *key, rng(), pass, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf")));
×
73
               }
74
            }
75
         } else {
76
            if(pass.empty()) {
19 ✔
77
               output() << Botan::PKCS8::PEM_encode(*key);
38 ✔
78
            } else {
79
               if(get_arg("pbkdf-iter").empty()) {
×
80
                  output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(
×
81
                     *key, rng(), pass, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf"));
×
82
               } else {
83
                  output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(
×
84
                     *key, rng(), pass, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf"));
×
85
               }
86
            }
87
         }
88
      }
38 ✔
89
};
90

91
BOTAN_REGISTER_COMMAND("keygen", PK_Keygen);
20 ✔
92

93
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
94

95
namespace {
96

97
std::string choose_sig_padding(const std::string& key, const std::string& padding, const std::string& hash) {
3 ✔
98
   if(key == "RSA") {
3 ✔
99
      std::ostringstream oss;
×
100
      if(padding.empty()) {
×
101
         oss << "PSS";
×
102
      } else {
103
         oss << padding;
×
104
      }
105

106
      oss << "(" << hash << ")";
×
107
      return oss.str();
×
108
   } else if(padding.empty()) {
3 ✔
109
      return hash;
3 ✔
110
   } else if(hash.empty()) {
×
111
      return padding;
×
112
   } else {
113
      std::ostringstream oss;
×
114
      oss << padding << "(" << hash << ")";
×
115
      return oss.str();
×
116
   }
×
117
}
118

119
}  // namespace
120

121
class PK_Fingerprint final : public Command {
122
   public:
123
      PK_Fingerprint() : Command("fingerprint --no-fsname --algo=SHA-256 *keys") {}
12 ✔
124

125
      std::string group() const override { return "pubkey"; }
1 ✔
126

127
      std::string description() const override { return "Calculate a public key fingerprint"; }
1 ✔
128

129
      void go() override {
5 ✔
130
         const std::string hash_algo = get_arg("algo");
5 ✔
131
         const bool no_fsname = flag_set("no-fsname");
5 ✔
132

133
         for(const std::string& key_file : get_arg_list("keys")) {
11 ✔
134
            std::unique_ptr<Botan::Public_Key> key(key_file == "-" ? Botan::X509::load_key(this->slurp_file("-", 4096))
14 ✔
135
                                                                   : Botan::X509::load_key(key_file));
6 ✔
136

137
            const std::string fprint = key->fingerprint_public(hash_algo);
6 ✔
138

139
            if(no_fsname || key_file == "-") {
6 ✔
140
               output() << fprint << "\n";
5 ✔
141
            } else {
142
               output() << key_file << ": " << fprint << "\n";
1 ✔
143
            }
144
         }
17 ✔
145
      }
5 ✔
146
};
147

148
BOTAN_REGISTER_COMMAND("fingerprint", PK_Fingerprint);
6 ✔
149

150
namespace {
151

152
std::unique_ptr<Botan::Private_Key> load_private_key(const std::string& key_filename, const std::string& passphrase) {
1 ✔
153
   std::string err_string;
1 ✔
154

155
   try {
1 ✔
156
      Botan::DataSource_Stream input(key_filename);
1 ✔
157
      return Botan::PKCS8::load_key(input, passphrase);
1 ✔
158
   } catch(Botan::Exception& e) {
1 ✔
159
      err_string = e.what();
×
160
   }
×
161

162
   if(passphrase.empty()) {
×
163
      try {
×
164
         Botan::DataSource_Stream input(key_filename);
×
165
         return Botan::PKCS8::load_key(input);
×
166
      } catch(Botan::Exception& e) {
×
167
         err_string = e.what();
×
168
      }
×
169
   }
170

171
   throw CLI_Error("Loading private key failed (" + err_string + ")");
×
172
}
1 ✔
173

174
}  // namespace
175

176
class PK_Sign final : public Command {
177
   public:
178
      PK_Sign() :
2 ✔
179
            Command(
180
               "sign --der-format --passphrase= --hash=SHA-256 --padding= --provider= --rng-type= --drbg-seed= key file") {
2 ✔
181
      }
2 ✔
182

183
      std::string group() const override { return "pubkey"; }
1 ✔
184

185
      std::string description() const override { return "Sign arbitrary data"; }
1 ✔
186

187
      void go() override {
1 ✔
188
         const std::string key_file = get_arg("key");
1 ✔
189
         const std::string passphrase = get_passphrase_arg("Passphrase for " + key_file, "passphrase");
2 ✔
190

191
         auto key = load_private_key(key_file, passphrase);
1 ✔
192

193
         const std::string hash_fn = get_arg("hash");
1 ✔
194

195
         if(!hash_fn.empty() && !Botan::HashFunction::create(hash_fn)) {
2 ✔
196
            throw CLI_Error_Unsupported("hashing", hash_fn);
×
197
         }
198

199
         const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
2 ✔
200

201
         auto format = Botan::Signature_Format::Standard;
1 ✔
202

203
         if(flag_set("der-format")) {
1 ✔
204
            if(!key->_signature_element_size_for_DER_encoding()) {
×
205
               throw CLI_Usage_Error("Key type " + key->algo_name() +
×
206
                                     " does not support DER formatting for signatures");
×
207
            }
208
            format = Botan::Signature_Format::DerSequence;
209
         }
210

211
         const std::string provider = get_arg("provider");
1 ✔
212

213
         Botan::PK_Signer signer(*key, rng(), sig_padding, format, provider);
1 ✔
214

215
         auto onData = [&signer](const uint8_t b[], size_t l) { signer.update(b, l); };
1 ✔
216
         Command::read_file(get_arg("file"), onData);
2 ✔
217

218
         std::vector<uint8_t> sig{signer.signature(rng())};
1 ✔
219

220
         if(key->stateful_operation()) {
1 ✔
221
            std::ofstream updated_key(key_file);
×
222
            if(passphrase.empty()) {
×
223
               updated_key << Botan::PKCS8::PEM_encode(*key);
×
224
            } else {
225
               updated_key << Botan::PKCS8::PEM_encode(*key, rng(), passphrase);
×
226
            }
227
         }
×
228

229
         output() << Botan::base64_encode(sig) << "\n";
2 ✔
230
      }
2 ✔
231
};
232

233
BOTAN_REGISTER_COMMAND("sign", PK_Sign);
2 ✔
234

235
class PK_Verify final : public Command {
236
   public:
237
      PK_Verify() : Command("verify --der-format --hash=SHA-256 --padding= pubkey file signature") {}
6 ✔
238

239
      std::string group() const override { return "pubkey"; }
1 ✔
240

241
      std::string description() const override {
1 ✔
242
         return "Verify the authenticity of the given file with the provided signature";
1 ✔
243
      }
244

245
      void go() override {
2 ✔
246
         auto key = Botan::X509::load_key(get_arg("pubkey"));
4 ✔
247
         if(!key) {
2 ✔
248
            throw CLI_Error("Unable to load public key");
×
249
         }
250

251
         const std::string hash_fn = get_arg("hash");
2 ✔
252

253
         if(!hash_fn.empty() && !Botan::HashFunction::create(hash_fn)) {
4 ✔
254
            throw CLI_Error_Unsupported("hashing", hash_fn);
×
255
         }
256

257
         const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
4 ✔
258

259
         auto format = Botan::Signature_Format::Standard;
2 ✔
260
         if(flag_set("der-format")) {
2 ✔
261
            if(key->message_parts() == 1) {
×
262
               throw CLI_Usage_Error("Key type " + key->algo_name() +
×
263
                                     " does not support DER formatting for signatures");
×
264
            }
265
            format = Botan::Signature_Format::DerSequence;
266
         }
267

268
         Botan::PK_Verifier verifier(*key, sig_padding, format);
2 ✔
269
         auto onData = [&verifier](const uint8_t b[], size_t l) { verifier.update(b, l); };
2 ✔
270
         Command::read_file(get_arg("file"), onData);
4 ✔
271

272
         const Botan::secure_vector<uint8_t> signature =
2 ✔
273
            Botan::base64_decode(this->slurp_file_as_str(get_arg("signature")));
4 ✔
274

275
         const bool valid = verifier.check_signature(signature);
2 ✔
276

277
         output() << "Signature is " << (valid ? "valid" : "invalid") << "\n";
3 ✔
278
      }
4 ✔
279
};
280

281
BOTAN_REGISTER_COMMAND("verify", PK_Verify);
3 ✔
282

283
class PKCS8_Tool final : public Command {
284
   public:
285
      PKCS8_Tool() :
8 ✔
286
            Command(
287
               "pkcs8 --pass-in= --pub-out --der-out --pass-out= --cipher= --pbkdf= --pbkdf-ms=300 --pbkdf-iter= --rng-type= --drbg-seed= key") {
8 ✔
288
      }
8 ✔
289

290
      std::string group() const override { return "pubkey"; }
1 ✔
291

292
      std::string description() const override { return "Open a PKCS #8 formatted key"; }
1 ✔
293

294
      void go() override {
7 ✔
295
         const std::string key_file = get_arg("key");
7 ✔
296
         const std::string pass_in = get_passphrase_arg("Password for " + key_file, "pass-in");
14 ✔
297

298
         Botan::DataSource_Memory key_src(slurp_file(key_file));
14 ✔
299
         std::unique_ptr<Botan::Private_Key> key;
7 ✔
300

301
         if(pass_in.empty()) {
7 ✔
302
            key = Botan::PKCS8::load_key(key_src);
5 ✔
303
         } else {
304
            key = Botan::PKCS8::load_key(key_src, pass_in);
2 ✔
305
         }
306

307
         const std::chrono::milliseconds pbkdf_ms(get_arg_sz("pbkdf-ms"));
7 ✔
308
         const bool der_out = flag_set("der-out");
7 ✔
309

310
         if(flag_set("pub-out")) {
7 ✔
311
            auto pk = key->public_key();
3 ✔
312
            if(der_out) {
3 ✔
313
               write_output(Botan::X509::BER_encode(*pk));
2 ✔
314
            } else {
315
               output() << Botan::X509::PEM_encode(*pk);
4 ✔
316
            }
317
         } else {
3 ✔
318
            const std::string pass_out = get_passphrase_arg("Passphrase to encrypt key", "pass-out");
8 ✔
319

320
            if(der_out) {
4 ✔
321
               if(pass_out.empty()) {
1 ✔
322
                  write_output(Botan::PKCS8::BER_encode(*key));
×
323
               } else {
324
                  if(get_arg("pbkdf-iter").empty()) {
1 ✔
325
                     write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(
2 ✔
326
                        *key, rng(), pass_out, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf")));
3 ✔
327
                  } else {
328
                     write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(
×
329
                        *key, rng(), pass_out, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf")));
×
330
                  }
331
               }
332
            } else {
333
               if(pass_out.empty()) {
3 ✔
334
                  output() << Botan::PKCS8::PEM_encode(*key);
4 ✔
335
               } else {
336
                  if(get_arg("pbkdf-iter").empty()) {
1 ✔
337
                     output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(
3 ✔
338
                        *key, rng(), pass_out, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf"));
4 ✔
339
                  } else {
340
                     output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(
×
341
                        *key, rng(), pass_out, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf"));
×
342
                  }
343
               }
344
            }
345
         }
4 ✔
346
      }
14 ✔
347
};
348

349
BOTAN_REGISTER_COMMAND("pkcs8", PKCS8_Tool);
8 ✔
350

351
   #endif
352

353
   #if defined(BOTAN_HAS_ECC_GROUP)
354

355
class EC_Group_Info final : public Command {
356
   public:
357
      EC_Group_Info() : Command("ec_group_info --pem name") {}
6 ✔
358

359
      std::string group() const override { return "pubkey"; }
1 ✔
360

361
      std::string description() const override {
1 ✔
362
         return "Print raw elliptic curve domain parameters of the standardized curve name";
1 ✔
363
      }
364

365
      void go() override {
2 ✔
366
         const auto ec_group = Botan::EC_Group::from_name(get_arg("name"));
4 ✔
367

368
         if(flag_set("pem")) {
2 ✔
369
            output() << ec_group.PEM_encode(Botan::EC_Group_Encoding::NamedCurve);
2 ✔
370
         } else {
371
            output() << "P = " << std::hex << ec_group.get_p() << "\n"
1 ✔
372
                     << "A = " << std::hex << ec_group.get_a() << "\n"
1 ✔
373
                     << "B = " << std::hex << ec_group.get_b() << "\n"
1 ✔
374
                     << "N = " << std::hex << ec_group.get_order() << "\n"
1 ✔
375
                     << "G = " << ec_group.get_g_x() << "," << ec_group.get_g_y() << "\n";
1 ✔
376
         }
377
      }
2 ✔
378
};
379

380
BOTAN_REGISTER_COMMAND("ec_group_info", EC_Group_Info);
3 ✔
381

382
   #endif
383

384
   #if defined(BOTAN_HAS_DL_GROUP)
385

386
class DL_Group_Info final : public Command {
387
   public:
388
      DL_Group_Info() : Command("dl_group_info --pem name") {}
16 ✔
389

390
      std::string group() const override { return "pubkey"; }
1 ✔
391

392
      std::string description() const override {
1 ✔
393
         return "Print raw Diffie-Hellman parameters (p,g) of the standardized DH group name";
1 ✔
394
      }
395

396
      void go() override {
7 ✔
397
         auto dl_group = Botan::DL_Group::from_name(get_arg("name"));
14 ✔
398

399
         if(flag_set("pem")) {
7 ✔
400
            output() << dl_group.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42_DH_PARAMETERS);
×
401
         } else {
402
            output() << "P = " << std::hex << dl_group.get_p() << "\n"
7 ✔
403
                     << "G = " << dl_group.get_g() << "\n";
7 ✔
404
         }
405
      }
7 ✔
406
};
407

408
BOTAN_REGISTER_COMMAND("dl_group_info", DL_Group_Info);
8 ✔
409

410
class PK_Workfactor final : public Command {
411
   public:
412
      PK_Workfactor() : Command("pk_workfactor --type=rsa bits") {}
12 ✔
413

414
      std::string group() const override { return "pubkey"; }
1 ✔
415

416
      std::string description() const override { return "Provide estimate of strength of public key based on size"; }
1 ✔
417

418
      void go() override {
5 ✔
419
         const size_t bits = get_arg_sz("bits");
5 ✔
420
         const std::string type = get_arg("type");
5 ✔
421

422
         if(type == "rsa") {
5 ✔
423
            output() << Botan::if_work_factor(bits) << "\n";
3 ✔
424
         } else if(type == "dl") {
2 ✔
425
            output() << Botan::dl_work_factor(bits) << "\n";
1 ✔
426
         } else if(type == "dl_exp") {
1 ✔
427
            output() << Botan::dl_exponent_size(bits) << "\n";
1 ✔
428
         } else {
429
            throw CLI_Usage_Error("Unknown type for pk_workfactor (rsa, dl, dl_exp)");
×
430
         }
431
      }
5 ✔
432
};
433

434
BOTAN_REGISTER_COMMAND("pk_workfactor", PK_Workfactor);
6 ✔
435

436
class Gen_DL_Group final : public Command {
437
   public:
438
      Gen_DL_Group() :
3 ✔
439
            Command("gen_dl_group --pbits=2048 --qbits=0 --seed= --type=subgroup --rng-type= --drbg-seed=") {}
6 ✔
440

441
      std::string group() const override { return "pubkey"; }
1 ✔
442

443
      std::string description() const override { return "Generate ANSI X9.42 encoded Diffie-Hellman group parameters"; }
1 ✔
444

445
      void go() override {
2 ✔
446
         const size_t pbits = get_arg_sz("pbits");
2 ✔
447
         const size_t qbits = get_arg_sz("qbits");
2 ✔
448

449
         const std::string type = get_arg("type");
2 ✔
450
         const std::string seed_str = get_arg("seed");
2 ✔
451

452
         if(type == "strong") {
2 ✔
453
            if(!seed_str.empty()) {
×
454
               throw CLI_Usage_Error("Seed only supported for DSA param gen");
×
455
            }
456
            const Botan::DL_Group grp(rng(), Botan::DL_Group::Strong, pbits);
×
457
            output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
×
458
         } else if(type == "subgroup") {
2 ✔
459
            if(!seed_str.empty()) {
1 ✔
460
               throw CLI_Usage_Error("Seed only supported for DSA param gen");
×
461
            }
462
            const Botan::DL_Group grp(rng(), Botan::DL_Group::Prime_Subgroup, pbits, qbits);
1 ✔
463
            output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
2 ✔
464
         } else if(type == "dsa") {
2 ✔
465
            size_t dsa_qbits = qbits;
1 ✔
466
            if(dsa_qbits == 0) {
1 ✔
467
               if(pbits == 1024) {
1 ✔
468
                  dsa_qbits = 160;
469
               } else if(pbits == 2048 || pbits == 3072) {
×
470
                  dsa_qbits = 256;
471
               } else {
472
                  throw CLI_Usage_Error("Invalid DSA p/q sizes");
×
473
               }
474
            }
475

476
            if(seed_str.empty()) {
1 ✔
477
               const Botan::DL_Group grp(rng(), Botan::DL_Group::DSA_Kosherizer, pbits, dsa_qbits);
1 ✔
478
               output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
2 ✔
479
            } else {
1 ✔
480
               const std::vector<uint8_t> seed = Botan::hex_decode(seed_str);
×
481
               const Botan::DL_Group grp(rng(), seed, pbits, dsa_qbits);
×
482
               output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
×
483
            }
×
484

485
         } else {
486
            throw CLI_Usage_Error("Invalid DL type '" + type + "'");
×
487
         }
488
      }
2 ✔
489
};
490

491
BOTAN_REGISTER_COMMAND("gen_dl_group", Gen_DL_Group);
3 ✔
492

493
   #endif
494

495
}  // namespace Botan_CLI
496

497
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc