add capability to construct EVPPKey from ECKey and RSAKey; RSA EVPPKey tests, RSA KeyPairImpl type bugfix

This commit is contained in:
Alex Fabijanic 2017-09-18 14:49:54 -05:00
parent b90ee449a2
commit dbd82953cb
9 changed files with 157 additions and 9 deletions

View File

@ -31,7 +31,7 @@ namespace Crypto {
POCO_DECLARE_EXCEPTION(Crypto_API, CryptoException, Poco::Exception)
class OpenSSLException : public CryptoException
class Crypto_API OpenSSLException : public CryptoException
{
public:
OpenSSLException(int code = 0);

View File

@ -30,6 +30,10 @@ namespace Poco {
namespace Crypto {
class ECKey;
class RSAKey;
class Crypto_API EVPPKey
/// Utility class for conversion of native keys to EVP.
/// Currently, only RSA and EC keys are supported.
@ -79,11 +83,14 @@ public:
int type() const;
/// Retuns the EVPPKey type NID.
bool isSupported(int type) const;
/// Returns true if OpenSSL type is supported
operator const EVP_PKEY*() const;
/// Returns const pointer to the EVP_PKEY structure.
/// Returns const pointer to the OpenSSL EVP_PKEY structure.
operator EVP_PKEY*();
/// Returns pointer to the EVP_PKEY structure.
/// Returns pointer to the OpenSSL EVP_PKEY structure.
private:
EVPPKey();
@ -91,6 +98,9 @@ private:
void newECKey(const char* group);
void duplicate(EVP_PKEY* pEVPPKey);
void setKey(ECKey* pKey);
void setKey(RSAKey* pKey);
void setKey(EC_KEY* pKey);
void setKey(RSA* pKey);
@ -109,15 +119,19 @@ inline int EVPPKey::type() const
}
inline bool EVPPKey::isSupported(int type) const
{
return type == EVP_PKEY_EC || type == EVP_PKEY_RSA;
}
inline EVPPKey::operator const EVP_PKEY*() const
/// Returns const pointer to the EVP_PKEY structure.
{
return _pEVPPKey;
}
inline EVPPKey::operator EVP_PKEY*()
/// Returns pointer to the EVP_PKEY structure.
{
return _pEVPPKey;
}

View File

@ -76,7 +76,7 @@ public:
/// Returns key pair type
private:
KeyPairImpl::Ptr _pImpl;
KeyPairImpl::Ptr _pImpl;
};
@ -113,6 +113,12 @@ inline KeyPairImpl::Ptr KeyPair::impl() const
}
inline KeyPair::Type KeyPair::type() const
{
return (KeyPair::Type)impl()->type();
}
} } // namespace Poco::Crypto

View File

@ -50,8 +50,6 @@ public:
typedef Poco::AutoPtr<RSAKeyImpl> Ptr;
typedef std::vector<unsigned char> ByteVec;
RSAKeyImpl() = delete;
RSAKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
@ -109,6 +107,8 @@ public:
/// key is not exported.
private:
RSAKeyImpl();
void freeRSA();
static ByteVec convertToByteVec(const BIGNUM* bn);

View File

@ -14,6 +14,8 @@
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Crypto/ECKey.h"
#include "Poco/Crypto/RSAKey.h"
#include "Poco/NumberFormatter.h"
@ -131,4 +133,19 @@ err:
}
void EVPPKey::setKey(ECKey* pKey)
{
poco_check_ptr(pKey);
poco_check_ptr(pKey->impl());
setKey(pKey->impl()->getECKey());
}
void EVPPKey::setKey(RSAKey* pKey)
{
poco_check_ptr(pKey);
poco_check_ptr(pKey->impl());
setKey(pKey->impl()->getRSA());
}
} } // namespace Poco::Crypto

View File

@ -31,7 +31,7 @@ namespace Crypto {
RSAKeyImpl::RSAKeyImpl(const EVPPKey& key):
KeyPairImpl("rsa", KT_EC_IMPL),
KeyPairImpl("rsa", KT_RSA_IMPL),
_pRSA(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>((const EVP_PKEY*)key)))
{
if (!_pRSA) throw OpenSSLException();

View File

@ -40,6 +40,9 @@ void ECTest::testEVPPKey()
{
EVPPKey* pKey = new EVPPKey("secp521r1");
assert (pKey != 0);
assert (!pKey->isSupported(0));
assert (!pKey->isSupported(-1));
assert (pKey->isSupported(pKey->type()));
assert (pKey->type() == EVP_PKEY_EC);
BIO* bioPriv1 = BIO_new(BIO_s_mem());

View File

@ -83,6 +83,112 @@ RSATest::~RSATest()
}
void RSATest::testEVPPKey()
{
try
{
RSAKey* key = new RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL);
assert(key->type() == Poco::Crypto::KeyPair::KT_RSA);
// construct EVPPKey from RSAKey*
EVPPKey* pKey = new EVPPKey(key);
// EVPPKey increments reference count, so freeing the original must be ok
delete key;
assert (!pKey->isSupported(0));
assert (!pKey->isSupported(-1));
assert (pKey->isSupported(pKey->type()));
assert (pKey->type() == EVP_PKEY_RSA);
// construct RSAKey from const EVPPKey&
key = new RSAKey(*pKey);
delete pKey;
assert(key->type() == Poco::Crypto::KeyPair::KT_RSA);
// construct EVPPKey from RSAKey*
pKey = new EVPPKey(key);
assert (pKey->type() == EVP_PKEY_RSA);
BIO* bioPriv1 = BIO_new(BIO_s_mem());
BIO* bioPub1 = BIO_new(BIO_s_mem());
assert (0 != PEM_write_bio_PrivateKey(bioPriv1, *pKey, NULL, NULL, 0, 0, NULL));
assert (0 != PEM_write_bio_PUBKEY(bioPub1, *pKey));
char* pPrivData1;
long sizePriv1 = BIO_get_mem_data(bioPriv1, &pPrivData1);
char* pPubData1;
long sizePub1 = BIO_get_mem_data(bioPub1, &pPubData1);
// construct EVPPKey from EVP_PKEY*
EVPPKey evpPKey(pKey->operator EVP_PKEY*());
// EVPPKey makes duplicate, so freeing the original must be ok
delete pKey;
assert (evpPKey.type() == EVP_PKEY_RSA);
BIO* bioPriv2 = BIO_new(BIO_s_mem());
BIO* bioPub2 = BIO_new(BIO_s_mem());
assert (0 != PEM_write_bio_PrivateKey(bioPriv2, evpPKey, NULL, NULL, 0, 0, NULL));
assert (0 != PEM_write_bio_PUBKEY(bioPub2, evpPKey));
char* pPrivData2;
long sizePriv2 = BIO_get_mem_data(bioPriv2, &pPrivData2);
char* pPubData2;
long sizePub2 = BIO_get_mem_data(bioPub2, &pPubData2);
assert (sizePriv1 && (sizePriv1 == sizePriv2));
assert (0 == memcmp(pPrivData1, pPrivData2, sizePriv1));
assert (sizePub1 && (sizePub1 == sizePub2));
assert (0 == memcmp(pPubData1, pPubData2, sizePub1));
BIO_free(bioPub2);
BIO_free(bioPriv2);
// copy
EVPPKey evpPKey2(evpPKey);
assert (evpPKey2.type() == EVP_PKEY_RSA);
bioPriv2 = BIO_new(BIO_s_mem());
bioPub2 = BIO_new(BIO_s_mem());
assert (0 != PEM_write_bio_PrivateKey(bioPriv2, evpPKey2, NULL, NULL, 0, 0, NULL));
assert (0 != PEM_write_bio_PUBKEY(bioPub2, evpPKey2));
sizePriv2 = BIO_get_mem_data(bioPriv2, &pPrivData2);
sizePub2 = BIO_get_mem_data(bioPub2, &pPubData2);
assert (sizePriv1 && (sizePriv1 == sizePriv2));
assert (0 == memcmp(pPrivData1, pPrivData2, sizePriv1));
assert (sizePub1 && (sizePub1 == sizePub2));
assert (0 == memcmp(pPubData1, pPubData2, sizePub1));
#ifdef POCO_ENABLE_CPP11
BIO_free(bioPub2);
BIO_free(bioPriv2);
// move
EVPPKey evpPKey3(std::move(evpPKey2));
assert (evpPKey3.type() == EVP_PKEY_RSA);
bioPriv2 = BIO_new(BIO_s_mem());
bioPub2 = BIO_new(BIO_s_mem());
assert (0 != PEM_write_bio_PrivateKey(bioPriv2, evpPKey3, NULL, NULL, 0, 0, NULL));
assert (0 != PEM_write_bio_PUBKEY(bioPub2, evpPKey3));
sizePriv2 = BIO_get_mem_data(bioPriv2, &pPrivData2);
sizePub2 = BIO_get_mem_data(bioPub2, &pPubData2);
assert (sizePriv1 && (sizePriv1 == sizePriv2));
assert (0 == memcmp(pPrivData1, pPrivData2, sizePriv1));
assert (sizePub1 && (sizePub1 == sizePub2));
assert (0 == memcmp(pPubData1, pPubData2, sizePub1));
#endif POCO_ENABLE_CPP11
BIO_free(bioPub2);
BIO_free(bioPriv2);
BIO_free(bioPub1);
BIO_free(bioPriv1);
}
catch (Poco::Exception& ex)
{
std::cerr << ex.displayText() << std::endl;
throw;
}
}
void RSATest::testNewKeys()
{
RSAKey key(RSAKey::KL_1024, RSAKey::EXP_SMALL);
@ -264,6 +370,7 @@ CppUnit::Test* RSATest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RSATest");
CppUnit_addTest(pSuite, RSATest, testEVPPKey);
CppUnit_addTest(pSuite, RSATest, testNewKeys);
CppUnit_addTest(pSuite, RSATest, testNewKeysNoPassphrase);
CppUnit_addTest(pSuite, RSATest, testSign);

View File

@ -24,6 +24,7 @@ public:
RSATest(const std::string& name);
~RSATest();
void testEVPPKey();
void testNewKeys();
void testNewKeysNoPassphrase();
void testSign();