diff options
author | Jack Lloyd <[email protected]> | 2015-12-23 01:23:26 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-12-23 01:23:26 -0500 |
commit | 9c504c706eec7fd96f728d1da28f6661a3d640b8 (patch) | |
tree | 09ee6d8333cea4226834fc131d8a98594f1254eb /src/cli | |
parent | bafdddbbf4c89c0ac4c4d1a209fb00502c52a430 (diff) |
Add DSA keygen and --der-out flag to keygen and pkcs8 tools
If no files are given on the command line to `hash`, default to stdin
Diffstat (limited to 'src/cli')
-rw-r--r-- | src/cli/cli.h | 26 | ||||
-rw-r--r-- | src/cli/pubkey.cpp | 93 | ||||
-rw-r--r-- | src/cli/utils.cpp | 6 |
3 files changed, 77 insertions, 48 deletions
diff --git a/src/cli/cli.h b/src/cli/cli.h index fdc83e97b..017966eca 100644 --- a/src/cli/cli.h +++ b/src/cli/cli.h @@ -439,31 +439,9 @@ class Command } template<typename Alloc> - void write_output_file(const std::string& who, - const std::vector<uint8_t, Alloc>& vec) const + void write_output(const std::vector<uint8_t, Alloc>& vec) { - write_output_file(who, vec.begin(), vec.size()); - } - - void write_output_file(const std::string& output_file, - const uint8_t buf[], size_t buf_len) const - { - std::ofstream out(output_file, std::ios::binary); - if(!out.good()) - throw CLI_IO_Error("writing", output_file); - - out.write(reinterpret_cast<const char*>(buf), buf_len); - out.close(); - } - - void write_output_file(const std::string& output_file, const std::string& outstr) const - { - std::ofstream out(output_file); - if(!out.good()) - throw CLI_IO_Error("writing", output_file); - - out.write(outstr.data(), outstr.size()); - out.close(); + output().write(reinterpret_cast<const char*>(vec.data()), vec.size()); } private: diff --git a/src/cli/pubkey.cpp b/src/cli/pubkey.cpp index 35d50592f..2616f6065 100644 --- a/src/cli/pubkey.cpp +++ b/src/cli/pubkey.cpp @@ -24,6 +24,10 @@ #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 @@ -41,7 +45,7 @@ namespace Botan_CLI { class PK_Keygen : public Command { public: - PK_Keygen() : Command("keygen --algo=RSA --params= --passphrase= --pbe= --pbe-millis=300") {} + PK_Keygen() : Command("keygen --algo=RSA --params= --passphrase= --pbe= --pbe-millis=300 --der-out") {} std::unique_ptr<Botan::Private_Key> do_keygen(const std::string& algo, const std::string& params, @@ -59,6 +63,15 @@ class PK_Keygen : public Command }; #endif +#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, param)); + }; +#endif + #if defined(BOTAN_HAS_ECDSA) generators["ECDSA"] = [&rng](std::string param) { if(param.empty()) @@ -105,18 +118,32 @@ class PK_Keygen : public Command 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"); - if(pass.empty()) + const std::chrono::milliseconds pbe_millis(get_arg_sz("pbe-millis")); + const std::string pbe = get_arg("pbe"); + + if(der_out) { - output() << Botan::PKCS8::PEM_encode(*key); + if(pass.empty()) + { + write_output(Botan::PKCS8::BER_encode(*key)); + } + else + { + write_output(Botan::PKCS8::BER_encode(*key, rng, pass, pbe_millis, pbe)); + } } else { - output() << Botan::PKCS8::PEM_encode(*key, - rng, - pass, - std::chrono::milliseconds(get_arg_sz("pbe-millis")), - get_arg("pbe")); + if(pass.empty()) + { + output() << Botan::PKCS8::PEM_encode(*key); + } + else + { + output() << Botan::PKCS8::PEM_encode(*key, rng, pass, pbe_millis, pbe); + } } } }; @@ -235,39 +262,59 @@ BOTAN_REGISTER_COMMAND(Gen_DL_Group); class PKCS8_Tool : public Command { public: - PKCS8_Tool() : Command("pkcs8 --pass-in= --pub-out --pass-out= --pbe= --pbe-millis=300 key") {} + PKCS8_Tool() : Command("pkcs8 --pass-in= --pub-out --der-out --pass-out= --pbe= --pbe-millis=300 key") {} void go() override { Botan::AutoSeeded_RNG rng; - std::unique_ptr<Botan::Private_Key> key(Botan::PKCS8::load_key(get_arg("key"), - rng, - get_arg("pass-in"))); + std::unique_ptr<Botan::Private_Key> key( + Botan::PKCS8::load_key(get_arg("key"), + rng, + get_arg("pass-in"))); + + const std::chrono::milliseconds pbe_millis(get_arg_sz("pbe-millis")); + const std::string pbe = get_arg("pbe"); + const bool der_out = flag_set("der-out"); if(flag_set("pub-out")) { - output() << Botan::X509::PEM_encode(*key); + if(der_out) + { + write_output(Botan::X509::BER_encode(*key)); + } + else + { + output() << Botan::X509::PEM_encode(*key); + } } else { const std::string pass = get_arg("pass-out"); - if(pass.empty()) + if(der_out) { - output() << Botan::PKCS8::PEM_encode(*key); + if(pass.empty()) + { + write_output(Botan::PKCS8::BER_encode(*key)); + } + else + { + write_output(Botan::PKCS8::BER_encode(*key, rng, pass, pbe_millis, pbe)); + } } else { - output() << Botan::PKCS8::PEM_encode(*key, - rng, - pass, - std::chrono::milliseconds(get_arg_sz("pbe-millis")), - get_arg("pbe")); - } - + if(pass.empty()) + { + output() << Botan::PKCS8::PEM_encode(*key); + } + else + { + output() << Botan::PKCS8::PEM_encode(*key, rng, pass, pbe_millis, pbe); + } + } } - } }; diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index f3ce5f0f9..9302ec5d0 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -124,7 +124,11 @@ class Hash : public Command const size_t buf_size = get_arg_sz("buf-size"); - for(auto fsname : get_arg_list("files")) + auto files = get_arg_list("files"); + if(files.empty()) + files.push_back("-"); // read stdin if no arguments on command line + + for(auto fsname : files) { auto update_hash = [&](const uint8_t b[], size_t l) { hash_fn->update(b, l); }; read_file(fsname, update_hash, buf_size); |