mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-04 19:13:30 +01:00
add capability to construct EVPPKey from ECKey and RSAKey; RSA EVPPKey tests, RSA KeyPairImpl type bugfix
This commit is contained in:
parent
b90ee449a2
commit
dbd82953cb
@ -31,7 +31,7 @@ namespace Crypto {
|
|||||||
POCO_DECLARE_EXCEPTION(Crypto_API, CryptoException, Poco::Exception)
|
POCO_DECLARE_EXCEPTION(Crypto_API, CryptoException, Poco::Exception)
|
||||||
|
|
||||||
|
|
||||||
class OpenSSLException : public CryptoException
|
class Crypto_API OpenSSLException : public CryptoException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OpenSSLException(int code = 0);
|
OpenSSLException(int code = 0);
|
||||||
|
@ -30,6 +30,10 @@ namespace Poco {
|
|||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
|
||||||
|
|
||||||
|
class ECKey;
|
||||||
|
class RSAKey;
|
||||||
|
|
||||||
|
|
||||||
class Crypto_API EVPPKey
|
class Crypto_API EVPPKey
|
||||||
/// Utility class for conversion of native keys to EVP.
|
/// Utility class for conversion of native keys to EVP.
|
||||||
/// Currently, only RSA and EC keys are supported.
|
/// Currently, only RSA and EC keys are supported.
|
||||||
@ -79,11 +83,14 @@ public:
|
|||||||
int type() const;
|
int type() const;
|
||||||
/// Retuns the EVPPKey type NID.
|
/// Retuns the EVPPKey type NID.
|
||||||
|
|
||||||
|
bool isSupported(int type) const;
|
||||||
|
/// Returns true if OpenSSL type is supported
|
||||||
|
|
||||||
operator const EVP_PKEY*() const;
|
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*();
|
operator EVP_PKEY*();
|
||||||
/// Returns pointer to the EVP_PKEY structure.
|
/// Returns pointer to the OpenSSL EVP_PKEY structure.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EVPPKey();
|
EVPPKey();
|
||||||
@ -91,6 +98,9 @@ private:
|
|||||||
void newECKey(const char* group);
|
void newECKey(const char* group);
|
||||||
|
|
||||||
void duplicate(EVP_PKEY* pEVPPKey);
|
void duplicate(EVP_PKEY* pEVPPKey);
|
||||||
|
|
||||||
|
void setKey(ECKey* pKey);
|
||||||
|
void setKey(RSAKey* pKey);
|
||||||
void setKey(EC_KEY* pKey);
|
void setKey(EC_KEY* pKey);
|
||||||
void setKey(RSA* 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
|
inline EVPPKey::operator const EVP_PKEY*() const
|
||||||
/// Returns const pointer to the EVP_PKEY structure.
|
|
||||||
{
|
{
|
||||||
return _pEVPPKey;
|
return _pEVPPKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline EVPPKey::operator EVP_PKEY*()
|
inline EVPPKey::operator EVP_PKEY*()
|
||||||
/// Returns pointer to the EVP_PKEY structure.
|
|
||||||
{
|
{
|
||||||
return _pEVPPKey;
|
return _pEVPPKey;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ public:
|
|||||||
/// Returns key pair type
|
/// Returns key pair type
|
||||||
|
|
||||||
private:
|
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
|
} } // namespace Poco::Crypto
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,8 +50,6 @@ public:
|
|||||||
typedef Poco::AutoPtr<RSAKeyImpl> Ptr;
|
typedef Poco::AutoPtr<RSAKeyImpl> Ptr;
|
||||||
typedef std::vector<unsigned char> ByteVec;
|
typedef std::vector<unsigned char> ByteVec;
|
||||||
|
|
||||||
RSAKeyImpl() = delete;
|
|
||||||
|
|
||||||
RSAKeyImpl(const EVPPKey& key);
|
RSAKeyImpl(const EVPPKey& key);
|
||||||
/// Constructs ECKeyImpl by extracting the EC key.
|
/// Constructs ECKeyImpl by extracting the EC key.
|
||||||
|
|
||||||
@ -109,6 +107,8 @@ public:
|
|||||||
/// key is not exported.
|
/// key is not exported.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
RSAKeyImpl();
|
||||||
|
|
||||||
void freeRSA();
|
void freeRSA();
|
||||||
static ByteVec convertToByteVec(const BIGNUM* bn);
|
static ByteVec convertToByteVec(const BIGNUM* bn);
|
||||||
|
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Crypto/EVPPKey.h"
|
#include "Poco/Crypto/EVPPKey.h"
|
||||||
|
#include "Poco/Crypto/ECKey.h"
|
||||||
|
#include "Poco/Crypto/RSAKey.h"
|
||||||
#include "Poco/NumberFormatter.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
|
} } // namespace Poco::Crypto
|
||||||
|
@ -31,7 +31,7 @@ namespace Crypto {
|
|||||||
|
|
||||||
|
|
||||||
RSAKeyImpl::RSAKeyImpl(const EVPPKey& key):
|
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)))
|
_pRSA(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>((const EVP_PKEY*)key)))
|
||||||
{
|
{
|
||||||
if (!_pRSA) throw OpenSSLException();
|
if (!_pRSA) throw OpenSSLException();
|
||||||
|
@ -40,6 +40,9 @@ void ECTest::testEVPPKey()
|
|||||||
{
|
{
|
||||||
EVPPKey* pKey = new EVPPKey("secp521r1");
|
EVPPKey* pKey = new EVPPKey("secp521r1");
|
||||||
assert (pKey != 0);
|
assert (pKey != 0);
|
||||||
|
assert (!pKey->isSupported(0));
|
||||||
|
assert (!pKey->isSupported(-1));
|
||||||
|
assert (pKey->isSupported(pKey->type()));
|
||||||
assert (pKey->type() == EVP_PKEY_EC);
|
assert (pKey->type() == EVP_PKEY_EC);
|
||||||
|
|
||||||
BIO* bioPriv1 = BIO_new(BIO_s_mem());
|
BIO* bioPriv1 = BIO_new(BIO_s_mem());
|
||||||
|
@ -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()
|
void RSATest::testNewKeys()
|
||||||
{
|
{
|
||||||
RSAKey key(RSAKey::KL_1024, RSAKey::EXP_SMALL);
|
RSAKey key(RSAKey::KL_1024, RSAKey::EXP_SMALL);
|
||||||
@ -264,6 +370,7 @@ CppUnit::Test* RSATest::suite()
|
|||||||
{
|
{
|
||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RSATest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RSATest");
|
||||||
|
|
||||||
|
CppUnit_addTest(pSuite, RSATest, testEVPPKey);
|
||||||
CppUnit_addTest(pSuite, RSATest, testNewKeys);
|
CppUnit_addTest(pSuite, RSATest, testNewKeys);
|
||||||
CppUnit_addTest(pSuite, RSATest, testNewKeysNoPassphrase);
|
CppUnit_addTest(pSuite, RSATest, testNewKeysNoPassphrase);
|
||||||
CppUnit_addTest(pSuite, RSATest, testSign);
|
CppUnit_addTest(pSuite, RSATest, testSign);
|
||||||
|
@ -24,6 +24,7 @@ public:
|
|||||||
RSATest(const std::string& name);
|
RSATest(const std::string& name);
|
||||||
~RSATest();
|
~RSATest();
|
||||||
|
|
||||||
|
void testEVPPKey();
|
||||||
void testNewKeys();
|
void testNewKeys();
|
||||||
void testNewKeysNoPassphrase();
|
void testNewKeysNoPassphrase();
|
||||||
void testSign();
|
void testSign();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user