From 34d8f1e30e3d9e87bda14ff4da5aebab8dd56439 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sat, 3 Apr 2021 10:52:40 -0400 Subject: Remove most uses of explicit new operator in cli --- src/cli/cli.cpp | 7 +++---- src/cli/cli.h | 4 ++-- src/cli/cli_rng.cpp | 15 +++++++-------- src/cli/roughtime.cpp | 5 +++-- src/cli/speed.cpp | 16 ++++++++-------- src/cli/timing_tests.cpp | 18 +++++++++--------- src/cli/tls_helpers.h | 32 +++++++++++++------------------- src/cli/tls_http_server.cpp | 4 ++-- src/cli/tls_proxy.cpp | 17 ++++++++--------- 9 files changed, 55 insertions(+), 63 deletions(-) (limited to 'src/cli') diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 35e6464cf..cdfe6226c 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -339,16 +339,15 @@ std::vector Command::registered_cmds() //static std::unique_ptr Command::get_cmd(const std::string& name) { - const std::map& reg = Command::global_registry(); + const auto& reg = Command::global_registry(); - std::unique_ptr r; auto i = reg.find(name); if(i != reg.end()) { - r.reset(i->second()); + return i->second(); } - return r; + return nullptr; } } diff --git a/src/cli/cli.h b/src/cli/cli.h index bf17e5d5b..c8aac22e3 100644 --- a/src/cli/cli.h +++ b/src/cli/cli.h @@ -184,7 +184,7 @@ class Command Botan::RandomNumberGenerator& rng(); private: - typedef std::function cmd_maker_fn; + typedef std::function ()> cmd_maker_fn; static std::map& global_registry(); void parse_spec(); @@ -213,7 +213,7 @@ class Command #define BOTAN_REGISTER_COMMAND(name, CLI_Class) \ Botan_CLI::Command::Registration reg_cmd_ ## CLI_Class(name, \ - []() -> Botan_CLI::Command* { return new CLI_Class; }) + []() -> std::unique_ptr { return std::make_unique(); }) } diff --git a/src/cli/cli_rng.cpp b/src/cli/cli_rng.cpp index 426bcff8b..064286d55 100644 --- a/src/cli/cli_rng.cpp +++ b/src/cli/cli_rng.cpp @@ -34,7 +34,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed) #if defined(BOTAN_HAS_SYSTEM_RNG) if(rng_type == "system" || rng_type.empty()) { - return std::unique_ptr(new Botan::System_RNG); + return std::make_unique(); } #endif @@ -46,9 +46,9 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed) std::unique_ptr rng; if(rng_type == "entropy") - rng.reset(new Botan::AutoSeeded_RNG(Botan::Entropy_Sources::global_sources())); + rng = std::make_unique(Botan::Entropy_Sources::global_sources()); else - rng.reset(new Botan::AutoSeeded_RNG); + rng = std::make_unique(); if(drbg_seed.size() > 0) rng->add_entropy(drbg_seed.data(), drbg_seed.size()); @@ -59,9 +59,8 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed) #if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32) if(rng_type == "drbg" || (rng_type.empty() && drbg_seed.empty() == false)) { - std::unique_ptr mac = - Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)"); - std::unique_ptr rng(new Botan::HMAC_DRBG(std::move(mac))); + auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)"); + auto rng = std::make_unique(std::move(mac)); rng->add_entropy(drbg_seed.data(), drbg_seed.size()); if(rng->is_seeded() == false) @@ -69,7 +68,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed) std::to_string(rng->security_level()/8) + " bytes must be provided"); - return std::unique_ptr(rng.release()); + return rng; } #endif @@ -77,7 +76,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed) if(rng_type == "rdrand" || rng_type == "cpu" || rng_type.empty()) { if(Botan::Processor_RNG::available()) - return std::unique_ptr(new Botan::Processor_RNG); + return std::make_unique(); else if(rng_type.empty() == false) throw CLI_Error("RNG instruction not supported on this processor"); } diff --git a/src/cli/roughtime.cpp b/src/cli/roughtime.cpp index 11ac6990f..739b4a31b 100644 --- a/src/cli/roughtime.cpp +++ b/src/cli/roughtime.cpp @@ -156,11 +156,12 @@ class Roughtime final : public Command { try { - chain.reset(new Botan::Roughtime::Chain(slurp_file_as_str(chain_file))); + chain = std::make_unique(slurp_file_as_str(chain_file)); } catch(const CLI_IO_Error&) { - chain.reset(new Botan::Roughtime::Chain()); //file is to still be created + // file is to still be created + chain = std::make_unique(); } } diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp index 4a22fb57f..6dbe82aba 100644 --- a/src/cli/speed.cpp +++ b/src/cli/speed.cpp @@ -450,9 +450,9 @@ class Speed final : public Command } if(format == "table") - m_summary.reset(new Summary); + m_summary = std::make_unique(); else if(format == "json") - m_json.reset(new JSON_Output); + m_json = std::make_unique(); else if(format != "default") throw CLI_Usage_Error("Unknown --format type '" + format + "'"); @@ -876,9 +876,9 @@ class Speed final : public Command const std::string& provider = "", size_t buf_size = 0) { - return std::unique_ptr( - new Timer(name, provider, what, event_mult, buf_size, - m_clock_cycle_ratio, m_clock_speed)); + return std::make_unique( + name, provider, what, event_mult, buf_size, + m_clock_cycle_ratio, m_clock_speed); } std::unique_ptr make_timer(const std::string& algo, @@ -2103,10 +2103,10 @@ class Speed final : public Command std::unique_ptr keygen_timer = make_timer(nm, provider, "keygen"); - std::unique_ptr key(keygen_timer->run([&] + std::unique_ptr key = keygen_timer->run([&] { - return new Botan::McEliece_PrivateKey(rng(), n, t); - })); + return std::make_unique(rng(), n, t); + });; record_result(keygen_timer); bench_pk_kem(*key, nm, provider, "KDF2(SHA-256)", msec); diff --git a/src/cli/timing_tests.cpp b/src/cli/timing_tests.cpp index 0e3cac4f5..e3c28a633 100644 --- a/src/cli/timing_tests.cpp +++ b/src/cli/timing_tests.cpp @@ -554,57 +554,57 @@ std::unique_ptr Timing_Test_Command::lookup_timing_test(const std:: #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_PKCS1) && defined(BOTAN_HAS_EME_RAW) if(test_type == "bleichenbacher") { - return std::unique_ptr(new Bleichenbacker_Timing_Test(2048)); + return std::make_unique(2048); } #endif #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_OAEP) && defined(BOTAN_HAS_EME_RAW) if(test_type == "manger") { - return std::unique_ptr(new Manger_Timing_Test(2048)); + return std::make_unique(2048); } #endif #if defined(BOTAN_HAS_ECDSA) if(test_type == "ecdsa") { - return std::unique_ptr(new ECDSA_Timing_Test("secp384r1")); + return std::make_unique("secp384r1"); } #endif #if defined(BOTAN_HAS_ECC_GROUP) if(test_type == "ecc_mul") { - return std::unique_ptr(new ECC_Mul_Timing_Test("brainpool512r1")); + return std::make_unique("brainpool512r1"); } #endif #if defined(BOTAN_HAS_NUMBERTHEORY) if(test_type == "inverse_mod") { - return std::unique_ptr(new Invmod_Timing_Test(512)); + return std::make_unique(512); } #endif #if defined(BOTAN_HAS_DL_GROUP) if(test_type == "pow_mod") { - return std::unique_ptr(new Powmod_Timing_Test("modp/ietf/1024")); + return std::make_unique("modp/ietf/1024"); } #endif #if defined(BOTAN_HAS_TLS_CBC) if(test_type == "lucky13sec3" || test_type == "lucky13sec4sha1") { - return std::unique_ptr(new Lucky13_Timing_Test("SHA-1", 20)); + return std::make_unique("SHA-1", 20); } if(test_type == "lucky13sec4sha256") { - return std::unique_ptr(new Lucky13_Timing_Test("SHA-256", 32)); + return std::make_unique("SHA-256", 32); } if(test_type == "lucky13sec4sha384") { - return std::unique_ptr(new Lucky13_Timing_Test("SHA-384", 48)); + return std::make_unique("SHA-384", 48); } #endif diff --git a/src/cli/tls_helpers.h b/src/cli/tls_helpers.h index 48a856c1a..f93c14719 100644 --- a/src/cli/tls_helpers.h +++ b/src/cli/tls_helpers.h @@ -195,48 +195,42 @@ class TLS_All_Policy final : public Botan::TLS::Policy inline std::unique_ptr load_tls_policy(const std::string policy_type) { - std::unique_ptr policy; - if(policy_type == "default" || policy_type == "") { - policy.reset(new Botan::TLS::Policy); + return std::make_unique(); } else if(policy_type == "suiteb_128") { - policy.reset(new Botan::TLS::NSA_Suite_B_128); + return std::make_unique(); } else if(policy_type == "suiteb_192" || policy_type == "suiteb") { - policy.reset(new Botan::TLS::NSA_Suite_B_192); + return std::make_unique(); } else if(policy_type == "strict") { - policy.reset(new Botan::TLS::Strict_Policy); + return std::make_unique(); } else if(policy_type == "bsi") { - policy.reset(new Botan::TLS::BSI_TR_02102_2); + return std::make_unique(); } else if(policy_type == "datagram") { - policy.reset(new Botan::TLS::Strict_Policy); + return std::make_unique(); } else if(policy_type == "all" || policy_type == "everything") { - policy.reset(new TLS_All_Policy); + return std::make_unique(); } - else + + // if something we don't recognize, assume it's a file + std::ifstream policy_stream(policy_type); + if(!policy_stream.good()) { - // assume it's a file - std::ifstream policy_stream(policy_type); - if(!policy_stream.good()) - { - throw Botan_CLI::CLI_Usage_Error("Unknown TLS policy: not a file or known short name"); - } - policy.reset(new Botan::TLS::Text_Policy(policy_stream)); + throw Botan_CLI::CLI_Usage_Error("Unknown TLS policy: not a file or known short name"); } - - return policy; + return std::make_unique(policy_stream); } #endif diff --git a/src/cli/tls_http_server.cpp b/src/cli/tls_http_server.cpp index 6ce30312f..2d5bfa75e 100644 --- a/src/cli/tls_http_server.cpp +++ b/src/cli/tls_http_server.cpp @@ -181,7 +181,7 @@ class TLS_Asio_HTTP_Session final : public std::enable_shared_from_this(io, session_manager, credentials, policy); } tcp::socket& client_socket() @@ -200,7 +200,6 @@ class TLS_Asio_HTTP_Session final : public std::enable_shared_from_this( + io, + session_manager, + credentials, + policy, + endpoints + ); } tcp::socket& client_socket() @@ -139,7 +138,6 @@ class tls_proxy_session final : public std::enable_shared_from_this