aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-12-30 23:35:16 -0500
committerJack Lloyd <[email protected]>2015-12-30 23:35:16 -0500
commitd4915aa4cd9c11879780267413f2980d0014240e (patch)
tree8663a7cde0ad9f7fc632c6c2f5ce33a733f8ed26 /src
parentd27416e791c9aed684efe0d6071fe66a43cf7af0 (diff)
Avoid having Command* objects be created until requested.
Avoids various static init and destruction hassles.
Diffstat (limited to 'src')
-rw-r--r--src/cli/asn1.cpp2
-rw-r--r--src/cli/bench.cpp2
-rw-r--r--src/cli/cc_enc.cpp4
-rw-r--r--src/cli/cli.h56
-rw-r--r--src/cli/compress.cpp4
-rw-r--r--src/cli/main.cpp4
-rw-r--r--src/cli/math.cpp6
-rw-r--r--src/cli/pubkey.cpp22
-rw-r--r--src/cli/tls_client.cpp2
-rw-r--r--src/cli/tls_proxy.cpp2
-rw-r--r--src/cli/tls_server.cpp2
-rw-r--r--src/cli/utils.cpp33
-rw-r--r--src/cli/x509.cpp12
-rw-r--r--src/tests/main.cpp4
14 files changed, 94 insertions, 61 deletions
diff --git a/src/cli/asn1.cpp b/src/cli/asn1.cpp
index 957fa1f24..3af2be97d 100644
--- a/src/cli/asn1.cpp
+++ b/src/cli/asn1.cpp
@@ -379,7 +379,7 @@ class ASN1_Printer : public Command
}
};
-BOTAN_REGISTER_COMMAND(ASN1_Printer);
+BOTAN_REGISTER_COMMAND("asn1print", ASN1_Printer);
}
diff --git a/src/cli/bench.cpp b/src/cli/bench.cpp
index 0d77ac2ec..82d04b139 100644
--- a/src/cli/bench.cpp
+++ b/src/cli/bench.cpp
@@ -747,6 +747,6 @@ class Benchmark : public Command
};
-BOTAN_REGISTER_COMMAND(Benchmark);
+BOTAN_REGISTER_COMMAND("bench", Benchmark);
}
diff --git a/src/cli/cc_enc.cpp b/src/cli/cc_enc.cpp
index 35932b487..2e2f9c12a 100644
--- a/src/cli/cc_enc.cpp
+++ b/src/cli/cc_enc.cpp
@@ -128,7 +128,7 @@ class CC_Encrypt : public Command
}
};
-BOTAN_REGISTER_COMMAND(CC_Encrypt);
+BOTAN_REGISTER_COMMAND("cc_encrypt", CC_Encrypt);
class CC_Decrypt : public Command
{
@@ -154,7 +154,7 @@ class CC_Decrypt : public Command
}
};
-BOTAN_REGISTER_COMMAND(CC_Decrypt);
+BOTAN_REGISTER_COMMAND("cc_decrypt", CC_Decrypt);
}
diff --git a/src/cli/cli.h b/src/cli/cli.h
index f55316b9d..49167b2ad 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -201,7 +201,7 @@ class Command
if(flag_set("help"))
{
output() << help_text() << "\n";
- return 1;
+ return 2;
}
if(m_user_args.count("output"))
@@ -314,6 +314,7 @@ class Command
m_spec_flags.insert("help");
m_spec_opts.insert(std::make_pair("output", ""));
m_spec_opts.insert(std::make_pair("error-output", ""));
+ m_spec_opts.insert(std::make_pair("rng-type", "auto"));
}
/*
@@ -426,6 +427,8 @@ class Command
else
{
std::ifstream in(input_file, std::ios::binary);
+ if(!in)
+ throw CLI_IO_Error("reading file", input_file);
do_read_file(in, consumer_fn, buf_size);
}
}
@@ -454,10 +457,30 @@ class Command
{
if(m_rng == nullptr)
{
- m_rng.reset(new Botan::AutoSeeded_RNG);
+ const std::string rng_type = get_arg("rng-type");
+
+ if(rng_type == "system")
+ {
+#if defined(BOTAN_HAS_SYSTEM_RNG)
+ m_rng.reset(new Botan::System_RNG);
+#endif
+ }
+
+ // TODO --rng-type=drbg
+ // TODO --drbg-seed=hexstr
+
+ if(rng_type == "auto")
+ {
+ m_rng.reset(new Botan::AutoSeeded_RNG);
+ }
+
+ if(!m_rng)
+ {
+ throw CLI_Error_Unsupported("rng", rng_type);
+ }
}
- return *m_rng;
+ return *m_rng.get();
}
private:
@@ -483,30 +506,33 @@ class Command
public:
// the registry interface:
- static std::map<std::string, std::unique_ptr<Command>>& global_registry()
+ typedef std::function<Command* ()> cmd_maker_fn;
+
+ static std::map<std::string, cmd_maker_fn>& global_registry()
{
- static std::map<std::string, std::unique_ptr<Command>> g_cmds;
+ static std::map<std::string, cmd_maker_fn> g_cmds;
return g_cmds;
}
- static Command* get_cmd(const std::string& name)
+ static std::unique_ptr<Command> get_cmd(const std::string& name)
{
auto& reg = Command::global_registry();
+
+ std::unique_ptr<Command> r;
auto i = reg.find(name);
- if(i == reg.end())
+ if(i != reg.end())
{
- return nullptr;
+ r.reset(i->second());
}
- return i->second.get();
+ return r;
}
class Registration
{
public:
- Registration(Command* cmd)
+ Registration(const std::string& name, cmd_maker_fn maker_fn)
{
- const std::string name = cmd->cmd_name();
auto& reg = Command::global_registry();
if(reg.count(name) > 0)
@@ -514,14 +540,14 @@ class Command
throw CLI_Error("Duplicated registration of command " + name);
}
- Command::global_registry().insert(
- std::make_pair(name, std::unique_ptr<Command>(cmd)));
+ Command::global_registry().insert(std::make_pair(name, maker_fn));
}
};
};
-#define BOTAN_REGISTER_COMMAND(CLI_Class) \
- namespace { Botan_CLI::Command::Registration reg_cmd_ ## CLI_Class(new CLI_Class); }
+#define BOTAN_REGISTER_COMMAND(name, CLI_Class) \
+ namespace { Botan_CLI::Command::Registration \
+ reg_cmd_ ## CLI_Class(name, []() -> Botan_CLI::Command* { return new CLI_Class; }); }
}
diff --git a/src/cli/compress.cpp b/src/cli/compress.cpp
index 5d32267ce..1b7d67e57 100644
--- a/src/cli/compress.cpp
+++ b/src/cli/compress.cpp
@@ -101,7 +101,7 @@ class Compress : public Command
}
};
-BOTAN_REGISTER_COMMAND(Compress);
+BOTAN_REGISTER_COMMAND("compress", Compress);
class Decompress : public Command
{
@@ -149,6 +149,6 @@ class Decompress : public Command
}
};
-BOTAN_REGISTER_COMMAND(Decompress);
+BOTAN_REGISTER_COMMAND("decompress", Decompress);
}
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index f054e6005..f6bbcc30e 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -40,13 +40,13 @@ int main(int argc, char* argv[])
const std::string cmd_name = (argc <= 1) ? "help" : argv[1];
- if(cmd_name == "help" || cmd_name == "--help")
+ if(cmd_name == "help" || cmd_name == "--help" || cmd_name == "-h")
{
std::cout << main_help();
return 1;
}
- Botan_CLI::Command* cmd = Botan_CLI::Command::get_cmd(cmd_name);
+ std::unique_ptr<Botan_CLI::Command> cmd(Botan_CLI::Command::get_cmd(cmd_name));
if(!cmd)
{
diff --git a/src/cli/math.cpp b/src/cli/math.cpp
index 07b809d19..27f3660d1 100644
--- a/src/cli/math.cpp
+++ b/src/cli/math.cpp
@@ -32,7 +32,7 @@ class Gen_Prime : public Command
}
};
-BOTAN_REGISTER_COMMAND(Gen_Prime);
+BOTAN_REGISTER_COMMAND("gen_prime", Gen_Prime);
class Is_Prime : public Command
{
@@ -49,7 +49,7 @@ class Is_Prime : public Command
}
};
-BOTAN_REGISTER_COMMAND(Is_Prime);
+BOTAN_REGISTER_COMMAND("is_prime", Is_Prime);
/*
* Factor integers using a combination of trial division by small
@@ -175,7 +175,7 @@ class Factor : public Command
}
};
-BOTAN_REGISTER_COMMAND(Factor);
+BOTAN_REGISTER_COMMAND("factor", Factor);
}
diff --git a/src/cli/pubkey.cpp b/src/cli/pubkey.cpp
index fcda6d21e..93a36a9cc 100644
--- a/src/cli/pubkey.cpp
+++ b/src/cli/pubkey.cpp
@@ -46,9 +46,9 @@ class PK_Keygen : public Command
public:
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,
- Botan::RandomNumberGenerator& rng)
+ static std::unique_ptr<Botan::Private_Key> do_keygen(const std::string& algo,
+ const std::string& params,
+ Botan::RandomNumberGenerator& rng)
{
typedef std::function<std::unique_ptr<Botan::Private_Key> (std::string)> gen_fn;
std::map<std::string, gen_fn> generators;
@@ -129,7 +129,7 @@ class PK_Keygen : public Command
}
else
{
- write_output(Botan::PKCS8::BER_encode(*key, rng, pass, pbe_millis, pbe));
+ write_output(Botan::PKCS8::BER_encode(*key, rng(), pass, pbe_millis, pbe));
}
}
else
@@ -140,13 +140,13 @@ class PK_Keygen : public Command
}
else
{
- output() << Botan::PKCS8::PEM_encode(*key, rng, pass, pbe_millis, pbe);
+ output() << Botan::PKCS8::PEM_encode(*key, rng(), pass, pbe_millis, pbe);
}
}
}
};
-BOTAN_REGISTER_COMMAND(PK_Keygen);
+BOTAN_REGISTER_COMMAND("keygen", PK_Keygen);
namespace {
@@ -186,11 +186,11 @@ class PK_Sign : public Command
this->read_file(get_arg("file"),
[&signer](const uint8_t b[], size_t l) { signer.update(b, l); });
- output() << Botan::base64_encode(signer.signature(rng)) << "\n";
+ output() << Botan::base64_encode(signer.signature(rng())) << "\n";
}
};
-BOTAN_REGISTER_COMMAND(PK_Sign);
+BOTAN_REGISTER_COMMAND("sign", PK_Sign);
class PK_Verify : public Command
{
@@ -219,7 +219,7 @@ class PK_Verify : public Command
}
};
-BOTAN_REGISTER_COMMAND(PK_Verify);
+BOTAN_REGISTER_COMMAND("verify", PK_Verify);
#if defined(BOTAN_HAS_DL_GROUP)
@@ -249,7 +249,7 @@ class Gen_DL_Group : public Command
}
};
-BOTAN_REGISTER_COMMAND(Gen_DL_Group);
+BOTAN_REGISTER_COMMAND("gen_dl_group", Gen_DL_Group);
#endif
@@ -310,7 +310,7 @@ class PKCS8_Tool : public Command
}
};
-BOTAN_REGISTER_COMMAND(PKCS8_Tool);
+BOTAN_REGISTER_COMMAND("pkcs8", PKCS8_Tool);
}
diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp
index 0d584dee0..bf1c3a57c 100644
--- a/src/cli/tls_client.cpp
+++ b/src/cli/tls_client.cpp
@@ -283,7 +283,7 @@ class TLS_Client : public Command
}
};
-BOTAN_REGISTER_COMMAND(TLS_Client);
+BOTAN_REGISTER_COMMAND("tls_client", TLS_Client);
}
diff --git a/src/cli/tls_proxy.cpp b/src/cli/tls_proxy.cpp
index 1457700cf..83332bb49 100644
--- a/src/cli/tls_proxy.cpp
+++ b/src/cli/tls_proxy.cpp
@@ -441,7 +441,7 @@ class TLS_Proxy : public Command
}
};
-BOTAN_REGISTER_COMMAND(TLS_Proxy);
+BOTAN_REGISTER_COMMAND("tls_proxy", TLS_Proxy);
}
diff --git a/src/cli/tls_server.cpp b/src/cli/tls_server.cpp
index f83f008f7..ccf8f40ac 100644
--- a/src/cli/tls_server.cpp
+++ b/src/cli/tls_server.cpp
@@ -236,7 +236,7 @@ class TLS_Server : public Command
};
-BOTAN_REGISTER_COMMAND(TLS_Server);
+BOTAN_REGISTER_COMMAND("tls_server", TLS_Server);
}
diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp
index ac2d62396..526b59aba 100644
--- a/src/cli/utils.cpp
+++ b/src/cli/utils.cpp
@@ -72,7 +72,7 @@ class Config_Info : public Command
}
};
-BOTAN_REGISTER_COMMAND(Config_Info);
+BOTAN_REGISTER_COMMAND("config", Config_Info);
class Version_Info : public Command
{
@@ -94,7 +94,7 @@ class Version_Info : public Command
}
};
-BOTAN_REGISTER_COMMAND(Version_Info);
+BOTAN_REGISTER_COMMAND("version", Version_Info);
class Print_Cpuid : public Command
{
@@ -107,7 +107,7 @@ class Print_Cpuid : public Command
}
};
-BOTAN_REGISTER_COMMAND(Print_Cpuid);
+BOTAN_REGISTER_COMMAND("cpuid", Print_Cpuid);
class Hash : public Command
{
@@ -130,14 +130,21 @@ class Hash : public Command
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);
- output() << Botan::hex_encode(hash_fn->final()) << " " << fsname << "\n";
+ try
+ {
+ auto update_hash = [&](const uint8_t b[], size_t l) { hash_fn->update(b, l); };
+ read_file(fsname, update_hash, buf_size);
+ output() << Botan::hex_encode(hash_fn->final()) << " " << fsname << "\n";
+ }
+ catch(CLI_IO_Error& e)
+ {
+ error_output() << e.what() << "\n";
+ }
}
}
};
-BOTAN_REGISTER_COMMAND(Hash);
+BOTAN_REGISTER_COMMAND("hash", Hash);
class RNG : public Command
{
@@ -164,7 +171,7 @@ class RNG : public Command
}
};
-BOTAN_REGISTER_COMMAND(RNG);
+BOTAN_REGISTER_COMMAND("rng", RNG);
#if defined(BOTAN_HAS_HTTP_UTIL)
@@ -179,7 +186,7 @@ class HTTP_Get : public Command
}
};
-BOTAN_REGISTER_COMMAND(HTTP_Get);
+BOTAN_REGISTER_COMMAND("http_get", HTTP_Get);
#endif // http_util
@@ -198,7 +205,7 @@ class Base64_Encode : public Command
}
};
-BOTAN_REGISTER_COMMAND(Base64_Encode);
+BOTAN_REGISTER_COMMAND("base64_enc", Base64_Encode);
class Base64_Decode : public Command
{
@@ -219,7 +226,7 @@ class Base64_Decode : public Command
}
};
-BOTAN_REGISTER_COMMAND(Base64_Decode);
+BOTAN_REGISTER_COMMAND("base64_dec", Base64_Decode);
#endif // base64
@@ -239,7 +246,7 @@ class Generate_Bcrypt : public Command
}
};
-BOTAN_REGISTER_COMMAND(Generate_Bcrypt);
+BOTAN_REGISTER_COMMAND("gen_bcrypt", Generate_Bcrypt);
class Check_Bcrypt : public Command
{
@@ -262,7 +269,7 @@ class Check_Bcrypt : public Command
}
};
-BOTAN_REGISTER_COMMAND(Check_Bcrypt);
+BOTAN_REGISTER_COMMAND("check_bcrypt", Check_Bcrypt);
#endif // bcrypt
diff --git a/src/cli/x509.cpp b/src/cli/x509.cpp
index 7cac858a7..886b57259 100644
--- a/src/cli/x509.cpp
+++ b/src/cli/x509.cpp
@@ -58,7 +58,7 @@ class Sign_Cert : public Command
}
};
-BOTAN_REGISTER_COMMAND(Sign_Cert);
+BOTAN_REGISTER_COMMAND("sign_cert", Sign_Cert);
class Cert_Info : public Command
{
@@ -72,7 +72,7 @@ class Cert_Info : public Command
}
};
-BOTAN_REGISTER_COMMAND(Cert_Info);
+BOTAN_REGISTER_COMMAND("cert_info", Cert_Info);
#if defined(BOTAN_HAS_OCSP)
class OCSP_Check : public Command
@@ -103,7 +103,7 @@ class OCSP_Check : public Command
}
};
-BOTAN_REGISTER_COMMAND(OCSP_Check);
+BOTAN_REGISTER_COMMAND("ocsp_check", OCSP_Check);
#endif // OCSP
@@ -140,7 +140,7 @@ class Cert_Verify : public Command
}
};
-BOTAN_REGISTER_COMMAND(Cert_Verify);
+BOTAN_REGISTER_COMMAND("cert_verify", Cert_Verify);
class Gen_Self_Signed : public Command
{
@@ -176,7 +176,7 @@ class Gen_Self_Signed : public Command
}
};
-BOTAN_REGISTER_COMMAND(Gen_Self_Signed);
+BOTAN_REGISTER_COMMAND("gen_self_signed", Gen_Self_Signed);
class Generate_PKCS10 : public Command
{
@@ -210,7 +210,7 @@ class Generate_PKCS10 : public Command
}
};
-BOTAN_REGISTER_COMMAND(Generate_PKCS10);
+BOTAN_REGISTER_COMMAND("gen_pkcs10", Generate_PKCS10);
}
diff --git a/src/tests/main.cpp b/src/tests/main.cpp
index 57a525dbd..c15fab438 100644
--- a/src/tests/main.cpp
+++ b/src/tests/main.cpp
@@ -249,7 +249,7 @@ class Test_Runner : public Botan_CLI::Command
};
-BOTAN_REGISTER_COMMAND(Test_Runner);
+BOTAN_REGISTER_COMMAND("test", Test_Runner);
}
@@ -259,7 +259,7 @@ int main(int argc, char* argv[])
BOTAN_VERSION_MINOR,
BOTAN_VERSION_PATCH);
- Botan_CLI::Command* cmd = Botan_CLI::Command::get_cmd("test");
+ std::unique_ptr<Botan_CLI::Command> cmd(Botan_CLI::Command::get_cmd("test"));
if(!cmd)
{