add function to crypto key controller to get directly a KeyPairHedera

This commit is contained in:
Dario 2020-08-31 15:05:54 +02:00 committed by Ulf Gebhardt
parent 3d4f0d7b64
commit a06cfebd60
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
5 changed files with 38 additions and 9 deletions

View File

@ -10,8 +10,7 @@ KeyPairHedera::KeyPairHedera()
}
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publicKey /* = nullptr*/)
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const unsigned char* publicKey/* = nullptr*/, size_t publicKeySize/* = 0*/)
: mPrivateKey(nullptr)
{
auto derPrefixPriv = DataTypeConverter::hexToBin("302e020100300506032b657004220420");
@ -50,15 +49,15 @@ KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publi
// check public
}
if (publicKey) {
switch (publicKey->size())
switch (publicKeySize)
{
case 32: { // raw public key
memcpy(mPublicKey, *publicKey, publicKey->size());
memcpy(mPublicKey, publicKey, publicKeySize);
break;
}
case 44: // DER encoded public key
if (0 == sodium_memcmp(*publicKey, *derPrefixPub, derPrefixPub->size())) {
memcpy(mPublicKey, publicKey->data(derPrefixPub->size()), ed25519_pubkey_SIZE);
if (0 == sodium_memcmp(publicKey, *derPrefixPub, derPrefixPub->size())) {
memcpy(mPublicKey, &publicKey[derPrefixPub->size()], ed25519_pubkey_SIZE);
}
break;
default:
@ -75,6 +74,11 @@ KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publi
mm->releaseMemory(derPrefixPriv);
mm->releaseMemory(derPrefixPub);
}
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publicKey /* = nullptr*/)
: KeyPairHedera(privateKey, publicKey->data(), publicKey->size())
{
}
KeyPairHedera::~KeyPairHedera()
{

View File

@ -22,6 +22,7 @@ public:
//! \param privateKey: copy
//! \param publicKey: copy
//!
KeyPairHedera(const MemoryBin* privateKey, const unsigned char* publicKey = nullptr, size_t publicKeySize = 0);
KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publicKey = nullptr);
~KeyPairHedera();

View File

@ -62,6 +62,23 @@ namespace controller {
return nullptr;
}
KeyPairHedera* CryptoKey::getKeyPair(Poco::AutoPtr<controller::User> user)
{
auto model = getModel();
auto password = user->getPassword();
auto mm = MemoryManager::getInstance();
if (!password || !model->hasPrivateKeyEncrypted()) {
return nullptr;
}
MemoryBin* clearPassword = nullptr;
if (password->decrypt(model->getPrivateKeyEncrypted(), &clearPassword) != SecretKeyCryptography::AUTH_DECRYPT_OK) {
return nullptr;
}
KeyPairHedera* key_pair = new KeyPairHedera(clearPassword, model->getPublicKey(), model->getPublicKeySize());
mm->releaseMemory(clearPassword);
return key_pair;
}
}

View File

@ -27,6 +27,8 @@ namespace controller {
inline Poco::AutoPtr<model::table::CryptoKey> getModel() { return _getModel<model::table::CryptoKey>(); }
KeyPairHedera* getKeyPair(Poco::AutoPtr<controller::User> user);
protected:
CryptoKey(model::table::CryptoKey* dbModel);

View File

@ -24,6 +24,11 @@ namespace model {
const char* getTableName() const { return "crypto_keys"; }
std::string toString();
inline const unsigned char* getPublicKey() const { if (mPublicKey.isNull()) return nullptr; return mPublicKey.value().content().data(); }
size_t getPublicKeySize() const { if (mPublicKey.isNull()) return 0; return mPublicKey.value().content().size(); }
inline bool hasPrivateKeyEncrypted() const { return !mPrivateKey.isNull(); }
inline const std::vector<unsigned char>& getPrivateKeyEncrypted() const { return mPrivateKey.value().content(); }
static const char* typeToString(KeyType type);
protected: