aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli/keygen.cpp
blob: 168b27a4a15d7ca5d11d3d1f82439990eb196b83 (plain)
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
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "apps.h"

#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO) && defined(BOTAN_HAS_X509_CERTIFICATES)

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <memory>
#include <botan/pk_keys.h>
#include <botan/pkcs8.h>

#if defined(BOTAN_HAS_RSA)
#include <botan/rsa.h>
#endif

#if defined(BOTAN_HAS_DSA)
#include <botan/dsa.h>
#endif

#if defined(BOTAN_HAS_ECDSA)
#include <botan/ecdsa.h>
#endif

#if defined(BOTAN_HAS_CURVE_25519)
#include <botan/curve25519.h>
#endif

using namespace Botan;

namespace {

std::string dsa_group_for(size_t bits)
   {
   if(bits == 1024)
      return "dsa/jce/1024";
   if(bits == 2048)
      return "dsa/botan/2048";
   if(bits == 3072)
      return "dsa/botan/3072";
   throw std::runtime_error("No registered DSA group for " + std::to_string(bits) + " bits");
   }

Private_Key* gen_key(RandomNumberGenerator& rng, const std::string& algo, size_t bits)
   {
#if defined(BOTAN_HAS_RSA)
   if(algo == "rsa")
      return new RSA_PrivateKey(rng, bits);
#endif

#if defined(BOTAN_HAS_DSA)
   if(algo == "dsa")
      {
      DL_Group grp(dsa_group_for(bits));
      return new DSA_PrivateKey(rng, grp);
      }
#endif

#if defined(BOTAN_HAS_ECDSA)
   if(algo == "ecdsa")
      {
      EC_Group grp("secp" + std::to_string(bits) + "r1");
      return new ECDSA_PrivateKey(rng, grp);
      }
#endif

#if defined(BOTAN_HAS_CURVE_25519)
   if(algo == "curve25519")
      return new Curve25519_PrivateKey(rng);
#endif

   throw std::runtime_error("Unknown algorithm " + algo);
   }


int keygen(const std::vector<std::string> &args)
   {
   OptionParser opts("algo=|bits=|passphrase=|pbe=");
   opts.parse(args);

   const std::string algo = opts.value_or_else("algo", "rsa");
   const size_t bits = opts.int_value_or_else("bits", 2048);
   const std::string pass = opts.value_or_else("passphrase", "");
   const std::string pbe = opts.value_or_else("pbe", "");

   try
      {
      std::ofstream pub("public.pem");
      std::ofstream priv("private.pem");

      if(!priv || !pub)
         {
         std::cout << "Couldn't write output files" << std::endl;
         return 1;
         }

      AutoSeeded_RNG rng;

      std::unique_ptr<Private_Key> key(gen_key(rng, algo, bits));

      pub << X509::PEM_encode(*key);

      if(pass == "")
         priv << PKCS8::PEM_encode(*key);
      else
         priv << PKCS8::PEM_encode(*key, rng, pass, std::chrono::milliseconds(300), pbe);

      std::cout << "Wrote " << bits << " bit " << algo << " key to public.pem / private.pem" << std::endl;
      }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      }

   return 0;
   }

REGISTER_APP(keygen);

}

#endif // BOTAN_HAS_PUBLIC_KEY_CRYPTO && BOTAN_HAS_X509_CERTIFICATES