2014-04-29 11:13:08 -04:00
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
// CryptoKey.cpp
|
2014-04-29 11:13:08 -04:00
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
// Implements the cCryptoKey class representing a RSA public key in PolarSSL
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
2014-05-01 09:21:41 -04:00
|
|
|
#include "CryptoKey.h"
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey::cCryptoKey(void)
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
|
|
|
pk_init(&m_Pk);
|
2014-04-30 18:28:27 -04:00
|
|
|
m_CtrDrbg.Initialize("rsa_pubkey", 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey::cCryptoKey(const AString & a_PublicKeyData)
|
2014-04-30 18:28:27 -04:00
|
|
|
{
|
|
|
|
pk_init(&m_Pk);
|
|
|
|
m_CtrDrbg.Initialize("rsa_pubkey", 10);
|
|
|
|
int res = ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size());
|
|
|
|
if (res != 0)
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
2014-04-30 18:28:27 -04:00
|
|
|
LOGWARNING("Failed to parse public key: -0x%x", res);
|
|
|
|
ASSERT(!"Cannot parse PubKey");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey::cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password)
|
2014-04-30 18:28:27 -04:00
|
|
|
{
|
|
|
|
pk_init(&m_Pk);
|
|
|
|
m_CtrDrbg.Initialize("rsa_privkey", 11);
|
|
|
|
int res = ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
LOGWARNING("Failed to parse private key: -0x%x", res);
|
2014-04-29 11:13:08 -04:00
|
|
|
ASSERT(!"Cannot parse PubKey");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey::~cCryptoKey()
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
|
|
|
pk_free(&m_Pk);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
int cCryptoKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength)
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
2014-04-30 18:28:27 -04:00
|
|
|
ASSERT(IsValid());
|
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
size_t DecryptedLen = a_DecryptedMaxLength;
|
|
|
|
int res = pk_decrypt(&m_Pk,
|
|
|
|
a_EncryptedData, a_EncryptedLength,
|
|
|
|
a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength,
|
|
|
|
ctr_drbg_random, m_CtrDrbg.GetInternal()
|
|
|
|
);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return (int)DecryptedLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
int cCryptoKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength)
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
2014-04-30 18:28:27 -04:00
|
|
|
ASSERT(IsValid());
|
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
size_t EncryptedLength = a_EncryptedMaxLength;
|
|
|
|
int res = pk_encrypt(&m_Pk,
|
|
|
|
a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength,
|
|
|
|
ctr_drbg_random, m_CtrDrbg.GetInternal()
|
|
|
|
);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return (int)EncryptedLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-30 18:28:27 -04:00
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
int cCryptoKey::ParsePublic(const void * a_Data, size_t a_NumBytes)
|
2014-04-30 18:28:27 -04:00
|
|
|
{
|
|
|
|
ASSERT(!IsValid()); // Cannot parse a second key
|
|
|
|
|
|
|
|
return pk_parse_public_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
int cCryptoKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password)
|
2014-04-30 18:28:27 -04:00
|
|
|
{
|
|
|
|
ASSERT(!IsValid()); // Cannot parse a second key
|
|
|
|
|
|
|
|
if (a_Password.empty())
|
|
|
|
{
|
|
|
|
return pk_parse_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes, NULL, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return pk_parse_key(
|
|
|
|
&m_Pk,
|
|
|
|
(const unsigned char *)a_Data, a_NumBytes,
|
|
|
|
(const unsigned char *)a_Password.c_str(), a_Password.size()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
bool cCryptoKey::IsValid(void) const
|
2014-04-30 18:28:27 -04:00
|
|
|
{
|
|
|
|
return (pk_get_type(&m_Pk) != POLARSSL_PK_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|