diff options
author | Jack Lloyd <[email protected]> | 2016-09-28 03:23:12 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-20 22:45:47 -0400 |
commit | 6ceeab949aae9d53914838c542e7b156c80b4b57 (patch) | |
tree | 01d56f8570686eff1064b14c8e44c6654e626b11 /src/cli/pubkey.cpp | |
parent | 36e5b56eb4298e81e8413ac1ef0eada096df8abc (diff) |
Add create_private_key, expose key loading functions in pk_algs.h
Diffstat (limited to 'src/cli/pubkey.cpp')
-rw-r--r-- | src/cli/pubkey.cpp | 89 |
1 files changed, 7 insertions, 82 deletions
diff --git a/src/cli/pubkey.cpp b/src/cli/pubkey.cpp index 1c3f948d1..cb23ddb66 100644 --- a/src/cli/pubkey.cpp +++ b/src/cli/pubkey.cpp @@ -12,6 +12,7 @@ #include <botan/base64.h> #include <botan/pk_keys.h> +#include <botan/pk_algs.h> #include <botan/pkcs8.h> #include <botan/pubkey.h> @@ -19,26 +20,6 @@ #include <botan/dl_group.h> #endif -#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 - -#if defined(BOTAN_HAS_MCELIECE) - #include <botan/mceliece.h> -#endif - namespace Botan_CLI { class PK_Keygen final : public Command @@ -46,75 +27,19 @@ class PK_Keygen final : public Command public: PK_Keygen() : Command("keygen --algo=RSA --params= --passphrase= --pbe= --pbe-millis=300 --der-out") {} - static std::unique_ptr<Botan::Private_Key> do_keygen(const std::string& algo, - const std::string& params, - Botan::RandomNumberGenerator& rng) + void go() override { - typedef std::function<std::unique_ptr<Botan::Private_Key> (std::string)> gen_fn; - std::map<std::string, gen_fn> generators; - -#if defined(BOTAN_HAS_RSA) - generators["RSA"] = [&rng](std::string param) -> std::unique_ptr<Botan::Private_Key> { - if(param.empty()) - param = "2048"; - return std::unique_ptr<Botan::Private_Key>( - new Botan::RSA_PrivateKey(rng, Botan::to_u32bit(param))); - }; -#endif + const std::string algo = get_arg("algo"); + const std::string params = get_arg("params"); -#if defined(BOTAN_HAS_DSA) - generators["DSA"] = [&rng](std::string param) -> std::unique_ptr<Botan::Private_Key> { - if(param.empty()) - param = "dsa/botan/2048"; - return std::unique_ptr<Botan::Private_Key>( - new Botan::DSA_PrivateKey(rng, Botan::DL_Group(param))); - }; -#endif + std::unique_ptr<Botan::Private_Key> + key(Botan::create_private_key(algo, rng(), params)); -#if defined(BOTAN_HAS_ECDSA) - generators["ECDSA"] = [&rng](std::string param) { - if(param.empty()) - param = "secp256r1"; - Botan::EC_Group grp(param); - return std::unique_ptr<Botan::Private_Key>( - new Botan::ECDSA_PrivateKey(rng, grp)); - }; -#endif - -#if defined(BOTAN_HAS_CURVE_25519) - generators["Curve25519"] = [&rng](std::string /*ignored*/) { - return std::unique_ptr<Botan::Private_Key>( - new Botan::Curve25519_PrivateKey(rng)); - }; -#endif - -#if defined(BOTAN_HAS_MCELIECE) - generators["McEliece"] = [&rng](std::string param) { - if(param.empty()) - param = "2280,45"; - std::vector<std::string> param_parts = Botan::split_on(param, ','); - if(param_parts.size() != 2) - throw CLI_Usage_Error("Bad McEliece parameters " + param); - return std::unique_ptr<Botan::Private_Key>( - new Botan::McEliece_PrivateKey(rng, - Botan::to_u32bit(param_parts[0]), - Botan::to_u32bit(param_parts[1]))); - }; -#endif - - auto gen = generators.find(algo); - if(gen == generators.end()) + if(!key) { throw CLI_Error_Unsupported("keygen", algo); } - return gen->second(params); - } - - void go() override - { - std::unique_ptr<Botan::Private_Key> key(do_keygen(get_arg("algo"), get_arg("params"), rng())); - const std::string pass = get_arg("passphrase"); const bool der_out = flag_set("der-out"); |