diff options
author | Jack Lloyd <[email protected]> | 2015-08-28 18:42:15 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-08-28 18:42:15 -0400 |
commit | d3360ca5fccb2f5cf941f1aa17498ae11bf4202a (patch) | |
tree | 30b67c5af517cbf0ab766782ed0a1613597c7a94 | |
parent | 58329e7602e07468396ba29d6404c01ce5edec42 (diff) | |
parent | 65bfc36c9fd414caf767465fbce09bc7e7e0ace9 (diff) |
Merge pull request #163 from cordney/add-pkcs8-cmd
Add PKCS#8 private/public key processing tool GH #163
-rw-r--r-- | src/cmd/pkcs8.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/cmd/pkcs8.cpp b/src/cmd/pkcs8.cpp new file mode 100644 index 000000000..19af70eb1 --- /dev/null +++ b/src/cmd/pkcs8.cpp @@ -0,0 +1,77 @@ +/* +* (C) 2015 René Korthaus +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "apps.h" +#include <iostream> +#include <fstream> +#include <string> +#include <memory> +#include <chrono> + +#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO) +#include <botan/pk_keys.h> +#include <botan/pkcs8.h> +#include <botan/x509_key.h> + +using namespace Botan; + +namespace { + +int pkcs8(int argc, char* argv[]) + { + BOTAN_UNUSED(argc); + OptionParser opts("in=|out=|passin=|passout=|pbe=|pubout"); + opts.parse(argv); + + const std::string passin = opts.value_or_else("passin", ""); + const std::string passout = opts.value_or_else("passout", ""); + const std::string pbe = opts.value_or_else("pbe", ""); + + if(argc < 3) + { + opts.help( std::cout, "pkcs8" ); + return 1; + } + + try + { + std::ofstream out_key(opts.value("out")); + + if (!out_key) + { + std::cout << "Couldn't write key" << std::endl; + return 1; + } + + AutoSeeded_RNG rng; + std::unique_ptr<Private_Key> key(PKCS8::load_key(opts.value("in"), rng, passin)); + + if(opts.is_set("pubout")) + { + out_key << X509::PEM_encode(*key); + } + else + { + if(passout.empty()) + out_key << PKCS8::PEM_encode(*key); + else + out_key << PKCS8::PEM_encode(*key, rng, passout, std::chrono::milliseconds(300), pbe); + } + } + catch(std::exception& e) + { + std::cout << "Exception caught: " << e.what() << std::endl; + return 2; + } + + return 0; + } + +REGISTER_APP(pkcs8); + +} + +#endif |