aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorAlexander Bluhm <alexander.bluhm@gmx.net>2017-04-28 14:43:08 +0200
committerAlexander Bluhm <alexander.bluhm@gmx.net>2017-04-30 23:01:04 +0200
commit17afb2681aa704d8241f4dcaeb949d806ba8df09 (patch)
treecc121843296d867ce9fe2d90c93649e608cb31e2 /src/tests
parent29cc6bebe132a34f882d450b35a69bf71bb3e27b (diff)
Generate private RSA key with OpenSSL.
Implement RSA private key generation with RSA_generate_key_ex(). Make PK_Key_Generation_Test iterate over all providers.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_pubkey.cpp196
-rw-r--r--src/tests/test_pubkey.h2
-rw-r--r--src/tests/tests.cpp10
-rw-r--r--src/tests/tests.h2
4 files changed, 115 insertions, 95 deletions
diff --git a/src/tests/test_pubkey.cpp b/src/tests/test_pubkey.cpp
index d392437c9..9d73f78db 100644
--- a/src/tests/test_pubkey.cpp
+++ b/src/tests/test_pubkey.cpp
@@ -402,6 +402,14 @@ Test::Result PK_Key_Agreement_Test::run_one_test(const std::string& header, cons
return result;
}
+std::vector<std::string> PK_Key_Generation_Test::possible_providers(
+ const std::string& algo)
+ {
+ std::vector<std::string> pk_provider =
+ Botan::probe_provider_private_key(algo, { "base", "openssl", "tpm" });
+ return Test::provider_filter(pk_provider);
+ }
+
std::vector<Test::Result> PK_Key_Generation_Test::run()
{
std::vector<Test::Result> results;
@@ -412,115 +420,125 @@ std::vector<Test::Result> PK_Key_Generation_Test::run()
Test::Result result(report_name + " keygen");
+ const std::vector<std::string> providers = possible_providers(algo_name());
+
+ if(providers.empty())
+ {
+ result.note_missing("provider key generation " + algo_name());
+ }
+
result.start_timer();
- std::unique_ptr<Botan::Private_Key> key_p =
- Botan::create_private_key(algo_name(), Test::rng(), param);
+ for(auto&& prov : providers)
+ {
+ std::unique_ptr<Botan::Private_Key> key_p =
+ Botan::create_private_key(algo_name(), Test::rng(), param, prov);
- const Botan::Private_Key& key = *key_p;
+ const Botan::Private_Key& key = *key_p;
- result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
+ result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
- result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
- result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
+ result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
+ result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
- // Test PEM public key round trips OK
- try
- {
- Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
- std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src));
+ // Test PEM public key round trips OK
+ try
+ {
+ Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
+ std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src));
- result.confirm("recovered public key from private", loaded.get() != nullptr);
- result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip PEM public key", e.what());
- }
+ result.confirm("recovered public key from private", loaded.get() != nullptr);
+ result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip PEM public key", e.what());
+ }
- // Test DER public key round trips OK
- try
- {
- Botan::DataSource_Memory data_src(Botan::X509::BER_encode(key));
- std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src));
+ // Test DER public key round trips OK
+ try
+ {
+ Botan::DataSource_Memory data_src(Botan::X509::BER_encode(key));
+ std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src));
- result.confirm("recovered public key from private", loaded.get() != nullptr);
- result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip BER public key", e.what());
- }
+ result.confirm("recovered public key from private", loaded.get() != nullptr);
+ result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip BER public key", e.what());
+ }
- // Test PEM private key round trips OK
- try
- {
- Botan::DataSource_Memory data_src(Botan::PKCS8::PEM_encode(key));
- std::unique_ptr<Botan::Private_Key> loaded(
- Botan::PKCS8::load_key(data_src, Test::rng()));
+ // Test PEM private key round trips OK
+ try
+ {
+ Botan::DataSource_Memory data_src(Botan::PKCS8::PEM_encode(key));
+ std::unique_ptr<Botan::Private_Key> loaded(
+ Botan::PKCS8::load_key(data_src, Test::rng()));
- result.confirm("recovered private key from PEM blob", loaded.get() != nullptr);
- result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip PEM private key", e.what());
- }
+ result.confirm("recovered private key from PEM blob", loaded.get() != nullptr);
+ result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip PEM private key", e.what());
+ }
- try
- {
- Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
- std::unique_ptr<Botan::Public_Key> loaded(Botan::PKCS8::load_key(data_src, Test::rng()));
+ try
+ {
+ Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
+ std::unique_ptr<Botan::Public_Key> loaded(Botan::PKCS8::load_key(data_src, Test::rng()));
- result.confirm("recovered public key from private", loaded.get() != nullptr);
- result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip BER private key", e.what());
- }
+ result.confirm("recovered public key from private", loaded.get() != nullptr);
+ result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip BER private key", e.what());
+ }
- const std::string passphrase = Test::random_password();
+ const std::string passphrase = Test::random_password();
- try
- {
- Botan::DataSource_Memory data_src(
- Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase,
- std::chrono::milliseconds(10)));
+ try
+ {
+ Botan::DataSource_Memory data_src(
+ Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase,
+ std::chrono::milliseconds(10)));
- std::unique_ptr<Botan::Private_Key> loaded(
- Botan::PKCS8::load_key(data_src, Test::rng(), passphrase));
+ std::unique_ptr<Botan::Private_Key> loaded(
+ Botan::PKCS8::load_key(data_src, Test::rng(), passphrase));
- result.confirm("recovered private key from encrypted blob", loaded.get() != nullptr);
- result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip encrypted PEM private key", e.what());
- }
+ result.confirm("recovered private key from encrypted blob", loaded.get() != nullptr);
+ result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip encrypted PEM private key", e.what());
+ }
- try
- {
- Botan::DataSource_Memory data_src(
- Botan::PKCS8::BER_encode(key, Test::rng(), passphrase,
- std::chrono::milliseconds(10)));
+ try
+ {
+ Botan::DataSource_Memory data_src(
+ Botan::PKCS8::BER_encode(key, Test::rng(), passphrase,
+ std::chrono::milliseconds(10)));
- std::unique_ptr<Botan::Private_Key> loaded(
- Botan::PKCS8::load_key(data_src, Test::rng(), passphrase));
+ std::unique_ptr<Botan::Private_Key> loaded(
+ Botan::PKCS8::load_key(data_src, Test::rng(), passphrase));
- result.confirm("recovered private key from BER blob", loaded.get() != nullptr);
- result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
- result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
- }
- catch(std::exception& e)
- {
- result.test_failure("roundtrip encrypted BER private key", e.what());
- }
+ result.confirm("recovered private key from BER blob", loaded.get() != nullptr);
+ result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
+ result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("roundtrip encrypted BER private key", e.what());
+ }
+ }
result.end_timer();
results.push_back(result);
diff --git a/src/tests/test_pubkey.h b/src/tests/test_pubkey.h
index 7709f51c7..32a5e1c24 100644
--- a/src/tests/test_pubkey.h
+++ b/src/tests/test_pubkey.h
@@ -161,6 +161,8 @@ class PK_Key_Generation_Test : public Test
virtual std::vector<std::string> keygen_params() const = 0;
virtual std::string algo_name() const = 0;
+
+ std::vector<std::string> possible_providers(const std::string&) override;
};
void check_invalid_signatures(Test::Result& result,
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index a678c64ae..a647600b5 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -349,6 +349,11 @@ bool Test::Result::test_rc(const std::string& func, int expected, int rc)
return test_success();
}
+std::vector<std::string> Test::possible_providers(const std::string&)
+ {
+ return Test::provider_filter({ "base" });
+ }
+
//static
std::string Test::format_time(uint64_t ns)
{
@@ -849,11 +854,6 @@ parse_cpuid_bits(const std::vector<std::string>& tok)
}
-std::vector<std::string> Text_Based_Test::possible_providers(const std::string&)
- {
- return Test::provider_filter({ "base" });
- }
-
bool Text_Based_Test::skip_this_test(const std::string& /*header*/,
const VarMap& /*vars*/)
{
diff --git a/src/tests/tests.h b/src/tests/tests.h
index 406cff57a..0d6dd2533 100644
--- a/src/tests/tests.h
+++ b/src/tests/tests.h
@@ -334,6 +334,7 @@ class Test
virtual std::vector<Test::Result> run() = 0;
virtual ~Test() = default;
+ virtual std::vector<std::string> possible_providers(const std::string&);
static std::vector<Test::Result> run_test(const std::string& what, bool fail_if_missing);
@@ -442,7 +443,6 @@ class Text_Based_Test : public Test
virtual Test::Result run_one_test(const std::string& header,
const VarMap& vars) = 0;
// Called before run_one_test
- virtual std::vector<std::string> possible_providers(const std::string&);
virtual bool skip_this_test(const std::string& header,
const VarMap& vars);