aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-04-03 11:11:03 -0400
committerJack Lloyd <[email protected]>2017-04-03 11:11:03 -0400
commitcc8d2eec88c8744152931b34d28619e7fc6e26db (patch)
tree35997d1089510cd1cf0eefa7ad91442dd9040728
parentd93a1ad12e4bd872a687ea31329efd2c9878c8d9 (diff)
Fix botan_privkey_create if the desired algorithm was not available in build
If DSA was disabled, caused memory corruption/crashes due to combination of uninitialized object and the tests not checking return values as carefully as they should.
-rw-r--r--src/lib/ffi/ffi.cpp35
-rw-r--r--src/tests/test_ffi.cpp19
-rw-r--r--src/tests/unit_tls.cpp2
3 files changed, 47 insertions, 9 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp
index f067db75b..390afae81 100644
--- a/src/lib/ffi/ffi.cpp
+++ b/src/lib/ffi/ffi.cpp
@@ -980,20 +980,31 @@ int botan_privkey_create(botan_privkey_t* key_obj,
{
try
{
- if(key_obj == nullptr || rng_obj == nullptr)
- return -1;
+ if(key_obj == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
+
+ *key_obj = nullptr;
+ if(rng_obj == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
+
if(algo_name == nullptr)
algo_name = "RSA";
if(algo_params == nullptr)
algo_params = "";
- *key_obj = nullptr;
-
Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
std::unique_ptr<Botan::Private_Key> key(
Botan::create_private_key(algo_name, rng, algo_params));
- *key_obj = new botan_privkey_struct(key.release());
- return 0;
+
+ if(key)
+ {
+ *key_obj = new botan_privkey_struct(key.release());
+ return 0;
+ }
+ else
+ {
+ return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
+ }
}
catch(std::exception& e)
{
@@ -1215,6 +1226,10 @@ int botan_privkey_load_dsa(botan_privkey_t* key,
}
return -1;
#else
+ BOTAN_UNUSED(p);
+ BOTAN_UNUSED(q);
+ BOTAN_UNUSED(g);
+ BOTAN_UNUSED(x);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}
@@ -1238,6 +1253,10 @@ int botan_pubkey_load_dsa(botan_pubkey_t* key,
return -1;
#else
+ BOTAN_UNUSED(p);
+ BOTAN_UNUSED(q);
+ BOTAN_UNUSED(g);
+ BOTAN_UNUSED(y);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}
@@ -1365,7 +1384,7 @@ int botan_pubkey_get_field(botan_mp_t output,
botan_pubkey_t key,
const char* field_name_cstr)
{
- if(field_name_cstr == NULL)
+ if(field_name_cstr == nullptr)
return BOTAN_FFI_ERROR_NULL_POINTER;
const std::string field_name(field_name_cstr);
@@ -1379,7 +1398,7 @@ int botan_privkey_get_field(botan_mp_t output,
botan_privkey_t key,
const char* field_name_cstr)
{
- if(field_name_cstr == NULL)
+ if(field_name_cstr == nullptr)
return BOTAN_FFI_ERROR_NULL_POINTER;
const std::string field_name(field_name_cstr);
diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp
index abc069c97..c196cf5c4 100644
--- a/src/tests/test_ffi.cpp
+++ b/src/tests/test_ffi.cpp
@@ -387,11 +387,26 @@ class FFI_Unit_Tests : public Test
std::vector<Test::Result> results;
results.push_back(ffi_test_mp(rng));
+
+#if defined(BOTAN_HAS_RSA)
results.push_back(ffi_test_rsa(rng));
+#endif
+
+#if defined(BOTAN_HAS_DSA)
results.push_back(ffi_test_dsa(rng));
+#endif
+
+#if defined(BOTAN_HAS_ECDSA)
results.push_back(ffi_test_ecdsa(rng));
+#endif
+
+#if defined(BOTAN_HAS_ECDH)
results.push_back(ffi_test_ecdh(rng));
+#endif
+
+#if defined(BOTAN_HAS_MCELIECE)
results.push_back(ffi_test_mceliece(rng));
+#endif
TEST_FFI_OK(botan_rng_destroy, (rng));
@@ -629,6 +644,7 @@ class FFI_Unit_Tests : public Test
Test::Result result("FFI RSA");
botan_privkey_t priv;
+
if(TEST_FFI_OK(botan_privkey_create_rsa, (&priv, rng, 1024)))
{
TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
@@ -754,6 +770,7 @@ class FFI_Unit_Tests : public Test
Test::Result result("FFI DSA");
botan_privkey_t priv;
+
if(TEST_FFI_OK(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng)))
{
TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
@@ -863,7 +880,7 @@ class FFI_Unit_Tests : public Test
if(TEST_FFI_OK(botan_privkey_create_ecdsa, (&priv, rng, "secp384r1")))
{
botan_pubkey_t pub;
- TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
+ REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
ffi_test_pubkey_export(result, pub, priv, rng);
diff --git a/src/tests/unit_tls.cpp b/src/tests/unit_tls.cpp
index 77aebce93..28152e624 100644
--- a/src/tests/unit_tls.cpp
+++ b/src/tests/unit_tls.cpp
@@ -132,6 +132,8 @@ class Credentials_Manager_Test : public Botan::Credentials_Manager
chain.push_back(*m_dsa_ca);
break;
}
+#else
+ BOTAN_UNUSED(context);
#endif
}
}