diff options
author | lloyd <[email protected]> | 2014-11-12 01:23:55 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-11-12 01:23:55 +0000 |
commit | 8b0cbccc7b11e545ed27bc6d7bda04b5cf632e60 (patch) | |
tree | 7ea9368d6ccaa85337a63b55e8bd15efa46fd357 /src/cmd/keygen.cpp | |
parent | 67161b91163afad417f9483cb557b26c5f5f4bc0 (diff) |
Command line prog cleanup
Diffstat (limited to 'src/cmd/keygen.cpp')
-rw-r--r-- | src/cmd/keygen.cpp | 84 |
1 files changed, 68 insertions, 16 deletions
diff --git a/src/cmd/keygen.cpp b/src/cmd/keygen.cpp index 40055f6cf..6aa74a08f 100644 --- a/src/cmd/keygen.cpp +++ b/src/cmd/keygen.cpp @@ -5,29 +5,74 @@ #include <cstdlib> #include <memory> +#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 + using namespace Botan; -int keygen_main(int argc, char* argv[]) +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(argc != 2 && argc != 3) +#if defined(BOTAN_HAS_RSA) + if(algo == "rsa") + return new RSA_PrivateKey(rng, bits); +#endif + +#if defined(BOTAN_HAS_DSA) + if(algo == "dsa") { - std::cout << "Usage: " << argv[0] << " bitsize [passphrase]" - << std::endl; - return 1; + DL_Group grp(dsa_group_for(bits)); + return new DSA_PrivateKey(rng, grp); } +#endif - const size_t bits = std::atoi(argv[1]); - if(bits < 1024 || bits > 16384) +#if defined(BOTAN_HAS_ECDSA) + if(algo == "ecdsa") { - std::cout << "Invalid argument for bitsize" << std::endl; - return 1; + EC_Group grp("secp" + std::to_string(bits) + "r1"); + return new ECDSA_PrivateKey(rng, grp); } +#endif + + throw std::runtime_error("Unknown algorithm " + algo); + } + + +int keygen(int argc, char* argv[]) + { + OptionParser opts("algo=|bits=|passphrase="); + opts.parse(argv); + + const std::string algo = opts.value_or_else("algo", "rsa"); + const size_t bits = opts.int_value_or_else("bits", 1024); + const std::string pass = opts.value_or_else("passphrase", ""); try { - std::ofstream pub("rsapub.pem"); - std::ofstream priv("rsapriv.pem"); + std::ofstream pub("public.pem"); + std::ofstream priv("private.pem"); + if(!priv || !pub) { std::cout << "Couldn't write output files" << std::endl; @@ -36,13 +81,16 @@ int keygen_main(int argc, char* argv[]) AutoSeeded_RNG rng; - RSA_PrivateKey key(rng, bits); - pub << X509::PEM_encode(key); + std::auto_ptr<Private_Key> key(gen_key(rng, algo, bits)); - if(argc == 2) - priv << PKCS8::PEM_encode(key); + pub << X509::PEM_encode(*key); + + if(pass == "") + priv << PKCS8::PEM_encode(*key); else - priv << PKCS8::PEM_encode(key, rng, argv[2]); + priv << PKCS8::PEM_encode(*key, rng, pass); + + std::cout << "Wrote " << bits << " bit " << algo << " key to public.pem / private.pem\n"; } catch(std::exception& e) { @@ -51,3 +99,7 @@ int keygen_main(int argc, char* argv[]) return 0; } + +REGISTER_APP(keygen); + +} |