aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_pubkey.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-10-31 20:32:32 -0400
committerJack Lloyd <[email protected]>2016-10-31 20:32:32 -0400
commitcb6fa09b205c53d32e17a5785a4a1af5a9eaf7dd (patch)
treeac1b1dcc456ceb677f16f5f5a53475cc6def79e8 /src/tests/test_pubkey.cpp
parent73c2605f50e6192bf6cb560c51d32bc53b4c5597 (diff)
Test PK key creation via create_private_key API
Diffstat (limited to 'src/tests/test_pubkey.cpp')
-rw-r--r--src/tests/test_pubkey.cpp185
1 files changed, 95 insertions, 90 deletions
diff --git a/src/tests/test_pubkey.cpp b/src/tests/test_pubkey.cpp
index 8552e1e51..699a7f2df 100644
--- a/src/tests/test_pubkey.cpp
+++ b/src/tests/test_pubkey.cpp
@@ -11,6 +11,7 @@
#include "test_rng.h"
#include <botan/pubkey.h>
+#include <botan/pk_algs.h>
#include <botan/x509_key.h>
#include <botan/pkcs8.h>
#include <botan/oids.h>
@@ -368,116 +369,120 @@ std::vector<Test::Result> PK_Key_Generation_Test::run()
for(auto&& param : keygen_params())
{
- std::unique_ptr<Botan::Private_Key> key = make_key(Test::rng(), param);
+ const std::string report_name = algo_name() + (param.empty() ? param : " " + param);
- const std::string report_name = key->algo_name() + (param.empty() ? param : " " + param);
+ Test::Result result(report_name + " keygen");
- results.push_back(test_key(report_name, *key));
- }
- return results;
- }
+ result.start_timer();
+ std::unique_ptr<Botan::Private_Key> key_p =
+ Botan::create_private_key(algo_name(), Test::rng(), param);
-Test::Result
-PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_Key& key)
- {
- Test::Result result(algo + " keygen");
+ const Botan::Private_Key& key = *key_p;
- 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());
+ }
- 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());
+ }
- 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);
}
- return result;
+ return results;
}
}