• 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

76.21
/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/hex.h>
15
   #include <botan/rng.h>
16

17
   #include <botan/data_src.h>
18
   #include <botan/hash.h>
19
   #include <botan/pk_algs.h>
20
   #include <botan/pk_keys.h>
21
   #include <botan/pkcs8.h>
22
   #include <botan/pubkey.h>
23
   #include <botan/x509_key.h>
24
   #include <botan/internal/workfactor.h>
25

26
   #include <fstream>
27
   #include <sstream>
28

29
   #if defined(BOTAN_HAS_DL_GROUP)
30
      #include <botan/dl_group.h>
31
   #endif
32

33
   #if defined(BOTAN_HAS_ECC_GROUP)
34
      #include <botan/ec_group.h>
35
   #endif
36

37
namespace Botan_CLI {
38

39
class PK_Keygen final : public Command {
40
   public:
41
      PK_Keygen() :
20✔
42
            Command(
43
               "keygen --algo=RSA --params= --passphrase= --cipher= --pbkdf= --pbkdf-ms=300 --pbkdf-iter= --provider= --der-out") {
20✔
44
      }
20✔
45

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

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

50
      void go() override {
19✔
51
         const std::string algo = get_arg("algo");
19✔
52
         const std::string params = get_arg("params");
19✔
53
         const std::string provider = get_arg("provider");
19✔
54

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

57
         if(!key) {
19✔
58
            throw CLI_Error_Unsupported("keygen", algo);
×
59
         }
60

61
         const std::string pass = get_passphrase_arg("Key passphrase", "passphrase");
38✔
62
         const bool der_out = flag_set("der-out");
19✔
63

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

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

94
BOTAN_REGISTER_COMMAND("keygen", PK_Keygen);
20✔
95

96
   #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
97

98
namespace {
99

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

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

121
}
122

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

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

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

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

135
         for(const std::string& key_file : get_arg_list("keys")) {
15✔
136
            std::unique_ptr<Botan::Public_Key> key(key_file == "-" ? Botan::X509::load_key(this->slurp_file("-", 4096))
17✔
137
                                                                   : Botan::X509::load_key(key_file));
9✔
138

139
            const std::string fprint = key->fingerprint_public(hash_algo);
8✔
140

141
            if(no_fsname || key_file == "-") {
8✔
142
               output() << fprint << "\n";
7✔
143
            } else {
144
               output() << key_file << ": " << fprint << "\n";
1✔
145
            }
146
         }
23✔
147
      }
7✔
148
};
149

150
BOTAN_REGISTER_COMMAND("fingerprint", PK_Fingerprint);
8✔
151

152
namespace {
153

154
std::unique_ptr<Botan::Private_Key> load_private_key(const std::string& key_filename, const std::string& passphrase) {
3✔
155
   std::string err_string;
3✔
156

157
   try {
3✔
158
      Botan::DataSource_Stream input(key_filename);
3✔
159
      return Botan::PKCS8::load_key(input, passphrase);
3✔
160
   } catch(Botan::Exception& e) { err_string = e.what(); }
3✔
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) { err_string = e.what(); }
×
167
   }
168

169
   throw CLI_Error("Loading private key failed (" + err_string + ")");
×
170
}
3✔
171

172
}
173

174
class PK_Sign final : public Command {
175
   public:
176
      PK_Sign() : Command("sign --der-format --passphrase= --hash=SHA-256 --padding= --provider= key file") {}
8✔
177

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

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

182
      void go() override {
3✔
183
         const std::string key_file = get_arg("key");
3✔
184
         const std::string passphrase = get_passphrase_arg("Passphrase for " + key_file, "passphrase");
6✔
185

186
         auto key = load_private_key(key_file, passphrase);
3✔
187

188
         const std::string hash_fn = get_arg("hash");
3✔
189

190
         if(!hash_fn.empty() && !Botan::HashFunction::create(hash_fn))
6✔
191
            throw CLI_Error_Unsupported("hashing", hash_fn);
×
192

193
         const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
6✔
194

195
         auto format = Botan::Signature_Format::Standard;
3✔
196

197
         if(flag_set("der-format")) {
3✔
198
            if(key->message_parts() == 1)
×
199
               throw CLI_Usage_Error("Key type " + key->algo_name() +
×
200
                                     " does not support DER formatting for signatures");
×
201
            format = Botan::Signature_Format::DerSequence;
202
         }
203

204
         const std::string provider = get_arg("provider");
3✔
205

206
         Botan::PK_Signer signer(*key, rng(), sig_padding, format, provider);
3✔
207

208
         auto onData = [&signer](const uint8_t b[], size_t l) { signer.update(b, l); };
3✔
209
         Command::read_file(get_arg("file"), onData);
6✔
210

211
         std::vector<uint8_t> sig{signer.signature(rng())};
3✔
212

213
         if(key->stateful_operation()) {
3✔
214
            std::ofstream updated_key(key_file);
2✔
215
            if(passphrase.empty()) {
2✔
216
               updated_key << Botan::PKCS8::PEM_encode(*key);
6✔
217
            } else {
218
               updated_key << Botan::PKCS8::PEM_encode(*key, rng(), passphrase);
×
219
            }
220
         }
2✔
221

222
         output() << Botan::base64_encode(sig) << "\n";
6✔
223
      }
9✔
224
};
225

226
BOTAN_REGISTER_COMMAND("sign", PK_Sign);
4✔
227

228
class PK_Verify final : public Command {
229
   public:
230
      PK_Verify() : Command("verify --der-format --hash=SHA-256 --padding= pubkey file signature") {}
10✔
231

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

234
      std::string description() const override {
1✔
235
         return "Verify the authenticity of the given file with the provided signature";
1✔
236
      }
237

238
      void go() override {
4✔
239
         auto key = Botan::X509::load_key(get_arg("pubkey"));
8✔
240
         if(!key) {
4✔
241
            throw CLI_Error("Unable to load public key");
×
242
         }
243

244
         const std::string hash_fn = get_arg("hash");
4✔
245

246
         if(!hash_fn.empty() && !Botan::HashFunction::create(hash_fn))
8✔
247
            throw CLI_Error_Unsupported("hashing", hash_fn);
×
248

249
         const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
8✔
250

251
         auto format = Botan::Signature_Format::Standard;
4✔
252
         if(flag_set("der-format")) {
4✔
253
            if(key->message_parts() == 1)
×
254
               throw CLI_Usage_Error("Key type " + key->algo_name() +
×
255
                                     " does not support DER formatting for signatures");
×
256
            format = Botan::Signature_Format::DerSequence;
257
         }
258

259
         Botan::PK_Verifier verifier(*key, sig_padding, format);
4✔
260
         auto onData = [&verifier](const uint8_t b[], size_t l) { verifier.update(b, l); };
4✔
261
         Command::read_file(get_arg("file"), onData);
8✔
262

263
         const Botan::secure_vector<uint8_t> signature =
4✔
264
            Botan::base64_decode(this->slurp_file_as_str(get_arg("signature")));
10✔
265

266
         const bool valid = verifier.check_signature(signature);
4✔
267

268
         output() << "Signature is " << (valid ? "valid" : "invalid") << "\n";
5✔
269
      }
8✔
270
};
271

272
BOTAN_REGISTER_COMMAND("verify", PK_Verify);
5✔
273

274
class PKCS8_Tool final : public Command {
275
   public:
276
      PKCS8_Tool() :
10✔
277
            Command(
278
               "pkcs8 --pass-in= --pub-out --der-out --pass-out= --cipher= --pbkdf= --pbkdf-ms=300 --pbkdf-iter= key") {
10✔
279
      }
10✔
280

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

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

285
      void go() override {
9✔
286
         const std::string key_file = get_arg("key");
9✔
287
         const std::string pass_in = get_passphrase_arg("Password for " + key_file, "pass-in");
18✔
288

289
         Botan::DataSource_Memory key_src(slurp_file(key_file));
18✔
290
         std::unique_ptr<Botan::Private_Key> key;
9✔
291

292
         if(pass_in.empty()) {
9✔
293
            key = Botan::PKCS8::load_key(key_src);
7✔
294
         } else {
295
            key = Botan::PKCS8::load_key(key_src, pass_in);
2✔
296
         }
297

298
         const std::chrono::milliseconds pbkdf_ms(get_arg_sz("pbkdf-ms"));
9✔
299
         const bool der_out = flag_set("der-out");
9✔
300

301
         if(flag_set("pub-out")) {
9✔
302
            if(der_out) {
5✔
303
               write_output(Botan::X509::BER_encode(*key));
2✔
304
            } else {
305
               output() << Botan::X509::PEM_encode(*key);
12✔
306
            }
307
         } else {
308
            const std::string pass_out = get_passphrase_arg("Passphrase to encrypt key", "pass-out");
8✔
309

310
            if(der_out) {
4✔
311
               if(pass_out.empty()) {
1✔
312
                  write_output(Botan::PKCS8::BER_encode(*key));
×
313
               } else {
314
                  if(get_arg("pbkdf-iter").empty()) {
1✔
315
                     write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(
2✔
316
                        *key, rng(), pass_out, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf")));
3✔
317
                  } else {
318
                     write_output(Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(
×
319
                        *key, rng(), pass_out, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf")));
×
320
                  }
321
               }
322
            } else {
323
               if(pass_out.empty()) {
3✔
324
                  output() << Botan::PKCS8::PEM_encode(*key);
6✔
325
               } else {
326
                  if(get_arg("pbkdf-iter").empty()) {
1✔
327
                     output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(
3✔
328
                        *key, rng(), pass_out, pbkdf_ms, nullptr, get_arg("cipher"), get_arg("pbkdf"));
4✔
329
                  } else {
330
                     output() << Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(
×
331
                        *key, rng(), pass_out, get_arg_sz("pbkdf-iter"), get_arg("cipher"), get_arg("pbkdf"));
×
332
                  }
333
               }
334
            }
335
         }
4✔
336
      }
27✔
337
};
338

339
BOTAN_REGISTER_COMMAND("pkcs8", PKCS8_Tool);
10✔
340

341
   #endif
342

343
   #if defined(BOTAN_HAS_ECC_GROUP)
344

345
class EC_Group_Info final : public Command {
346
   public:
347
      EC_Group_Info() : Command("ec_group_info --pem name") {}
6✔
348

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

351
      std::string description() const override {
1✔
352
         return "Print raw elliptic curve domain parameters of the standardized curve name";
1✔
353
      }
354

355
      void go() override {
2✔
356
         Botan::EC_Group ec_group(get_arg("name"));
4✔
357

358
         if(flag_set("pem")) {
2✔
359
            output() << ec_group.PEM_encode();
3✔
360
         } else {
361
            output() << "P = " << std::hex << ec_group.get_p() << "\n"
1✔
362
                     << "A = " << std::hex << ec_group.get_a() << "\n"
1✔
363
                     << "B = " << std::hex << ec_group.get_b() << "\n"
1✔
364
                     << "N = " << std::hex << ec_group.get_order() << "\n"
1✔
365
                     << "G = " << ec_group.get_g_x() << "," << ec_group.get_g_y() << "\n";
1✔
366
         }
367
      }
2✔
368
};
369

370
BOTAN_REGISTER_COMMAND("ec_group_info", EC_Group_Info);
3✔
371

372
   #endif
373

374
   #if defined(BOTAN_HAS_DL_GROUP)
375

376
class DL_Group_Info final : public Command {
377
   public:
378
      DL_Group_Info() : Command("dl_group_info --pem name") {}
16✔
379

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

382
      std::string description() const override {
1✔
383
         return "Print raw Diffie-Hellman parameters (p,g) of the standardized DH group name";
1✔
384
      }
385

386
      void go() override {
7✔
387
         Botan::DL_Group dl_group(get_arg("name"));
14✔
388

389
         if(flag_set("pem")) {
7✔
390
            output() << dl_group.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42_DH_PARAMETERS);
×
391
         } else {
392
            output() << "P = " << std::hex << dl_group.get_p() << "\n"
7✔
393
                     << "G = " << dl_group.get_g() << "\n";
7✔
394
         }
395
      }
7✔
396
};
397

398
BOTAN_REGISTER_COMMAND("dl_group_info", DL_Group_Info);
8✔
399

400
class PK_Workfactor final : public Command {
401
   public:
402
      PK_Workfactor() : Command("pk_workfactor --type=rsa bits") {}
12✔
403

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

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

408
      void go() override {
5✔
409
         const size_t bits = get_arg_sz("bits");
5✔
410
         const std::string type = get_arg("type");
5✔
411

412
         if(type == "rsa") {
5✔
413
            output() << Botan::if_work_factor(bits) << "\n";
3✔
414
         } else if(type == "dl") {
2✔
415
            output() << Botan::dl_work_factor(bits) << "\n";
1✔
416
         } else if(type == "dl_exp") {
1✔
417
            output() << Botan::dl_exponent_size(bits) << "\n";
1✔
418
         } else {
419
            throw CLI_Usage_Error("Unknown type for pk_workfactor (rsa, dl, dl_exp)");
×
420
         }
421
      }
5✔
422
};
423

424
BOTAN_REGISTER_COMMAND("pk_workfactor", PK_Workfactor);
6✔
425

426
class Gen_DL_Group final : public Command {
427
   public:
428
      Gen_DL_Group() : Command("gen_dl_group --pbits=2048 --qbits=0 --seed= --type=subgroup") {}
6✔
429

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

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

434
      void go() override {
2✔
435
         const size_t pbits = get_arg_sz("pbits");
2✔
436
         const size_t qbits = get_arg_sz("qbits");
2✔
437

438
         const std::string type = get_arg("type");
2✔
439
         const std::string seed_str = get_arg("seed");
2✔
440

441
         if(type == "strong") {
2✔
442
            if(!seed_str.empty()) {
×
443
               throw CLI_Usage_Error("Seed only supported for DSA param gen");
×
444
            }
445
            Botan::DL_Group grp(rng(), Botan::DL_Group::Strong, pbits);
×
446
            output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
×
447
         } else if(type == "subgroup") {
2✔
448
            if(!seed_str.empty()) {
1✔
449
               throw CLI_Usage_Error("Seed only supported for DSA param gen");
×
450
            }
451
            Botan::DL_Group grp(rng(), Botan::DL_Group::Prime_Subgroup, pbits, qbits);
1✔
452
            output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
3✔
453
         } else if(type == "dsa") {
2✔
454
            size_t dsa_qbits = qbits;
1✔
455
            if(dsa_qbits == 0) {
1✔
456
               if(pbits == 1024) {
1✔
457
                  dsa_qbits = 160;
458
               } else if(pbits == 2048 || pbits == 3072) {
×
459
                  dsa_qbits = 256;
460
               } else {
461
                  throw CLI_Usage_Error("Invalid DSA p/q sizes");
×
462
               }
463
            }
464

465
            if(seed_str.empty()) {
1✔
466
               Botan::DL_Group grp(rng(), Botan::DL_Group::DSA_Kosherizer, pbits, dsa_qbits);
1✔
467
               output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
3✔
468
            } else {
1✔
469
               const std::vector<uint8_t> seed = Botan::hex_decode(seed_str);
×
470
               Botan::DL_Group grp(rng(), seed, pbits, dsa_qbits);
×
471
               output() << grp.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
×
472
            }
×
473

474
         } else {
475
            throw CLI_Usage_Error("Invalid DL type '" + type + "'");
×
476
         }
477
      }
2✔
478
};
479

480
BOTAN_REGISTER_COMMAND("gen_dl_group", Gen_DL_Group);
3✔
481

482
   #endif
483

484
}
485

486
#endif
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