security: deduplicate getting the key type from an EVP_PKEY structure
Change-Id: Idba7ddcdd10672a0305fe16b1e278f397ff094fe
diff --git a/src/security/detail/openssl-helper.cpp b/src/security/detail/openssl-helper.cpp
index 12d1106..27177e2 100644
--- a/src/security/detail/openssl-helper.cpp
+++ b/src/security/detail/openssl-helper.cpp
@@ -36,6 +36,17 @@
}
}
+int
+getEvpPkeyType(EVP_PKEY* key)
+{
+ return
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
+ EVP_PKEY_type(key->type);
+#else
+ EVP_PKEY_base_id(key);
+#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
+}
+
EvpPkeyCtx::EvpPkeyCtx(EVP_PKEY* key)
: m_ctx(EVP_PKEY_CTX_new(key, nullptr))
{
diff --git a/src/security/detail/openssl-helper.hpp b/src/security/detail/openssl-helper.hpp
index ddd1ea6..dfc630d 100644
--- a/src/security/detail/openssl-helper.hpp
+++ b/src/security/detail/openssl-helper.hpp
@@ -32,6 +32,9 @@
const EVP_MD*
toDigestEvpMd(DigestAlgorithm algo);
+int
+getEvpPkeyType(EVP_PKEY* key);
+
class EvpPkeyCtx : noncopyable
{
public:
diff --git a/src/security/transform/private-key.cpp b/src/security/transform/private-key.cpp
index b7820c3..3ec0ff6 100644
--- a/src/security/transform/private-key.cpp
+++ b/src/security/transform/private-key.cpp
@@ -265,13 +265,7 @@
{
ENSURE_PRIVATE_KEY_LOADED(m_impl->key);
- int keyType =
-#if OPENSSL_VERSION_NUMBER < 0x1010000fL
- EVP_PKEY_type(m_impl->key->type);
-#else
- EVP_PKEY_base_id(m_impl->key);
-#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
-
+ int keyType = detail::getEvpPkeyType(m_impl->key);
switch (keyType) {
case EVP_PKEY_NONE:
BOOST_THROW_EXCEPTION(Error("Failed to determine key type"));
diff --git a/src/security/transform/public-key.cpp b/src/security/transform/public-key.cpp
index 3240cee..13b8f00 100644
--- a/src/security/transform/public-key.cpp
+++ b/src/security/transform/public-key.cpp
@@ -73,20 +73,13 @@
{
ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
- int keyType =
-#if OPENSSL_VERSION_NUMBER < 0x1010000fL
- EVP_PKEY_type(m_impl->key->type);
-#else
- EVP_PKEY_base_id(m_impl->key);
-#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
-
- switch (keyType) {
+ switch (detail::getEvpPkeyType(m_impl->key)) {
case EVP_PKEY_RSA:
return KeyType::RSA;
case EVP_PKEY_EC:
return KeyType::EC;
default:
- BOOST_THROW_EXCEPTION(Error("Unrecognized public key type"));
+ return KeyType::NONE;
}
}
@@ -140,13 +133,7 @@
{
ENSURE_PUBLIC_KEY_LOADED(m_impl->key);
- int keyType =
-#if OPENSSL_VERSION_NUMBER < 0x1010000fL
- EVP_PKEY_type(m_impl->key->type);
-#else
- EVP_PKEY_base_id(m_impl->key);
-#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
-
+ int keyType = detail::getEvpPkeyType(m_impl->key);
switch (keyType) {
case EVP_PKEY_NONE:
BOOST_THROW_EXCEPTION(Error("Failed to determine key type"));