mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
add function to crypto key controller to get directly a KeyPairHedera
This commit is contained in:
parent
3d4f0d7b64
commit
a06cfebd60
@ -10,12 +10,11 @@ KeyPairHedera::KeyPairHedera()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const unsigned char* publicKey/* = nullptr*/, size_t publicKeySize/* = 0*/)
|
||||||
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publicKey /* = nullptr*/)
|
|
||||||
: mPrivateKey(nullptr)
|
: mPrivateKey(nullptr)
|
||||||
{
|
{
|
||||||
auto derPrefixPriv = DataTypeConverter::hexToBin("302e020100300506032b657004220420");
|
auto derPrefixPriv = DataTypeConverter::hexToBin("302e020100300506032b657004220420");
|
||||||
auto derPrefixPub = DataTypeConverter::hexToBin("302a300506032b6570032100");
|
auto derPrefixPub = DataTypeConverter::hexToBin("302a300506032b6570032100");
|
||||||
|
|
||||||
auto mm = MemoryManager::getInstance();
|
auto mm = MemoryManager::getInstance();
|
||||||
|
|
||||||
@ -50,15 +49,15 @@ KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publi
|
|||||||
// check public
|
// check public
|
||||||
}
|
}
|
||||||
if (publicKey) {
|
if (publicKey) {
|
||||||
switch (publicKey->size())
|
switch (publicKeySize)
|
||||||
{
|
{
|
||||||
case 32: { // raw public key
|
case 32: { // raw public key
|
||||||
memcpy(mPublicKey, *publicKey, publicKey->size());
|
memcpy(mPublicKey, publicKey, publicKeySize);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 44: // DER encoded public key
|
case 44: // DER encoded public key
|
||||||
if (0 == sodium_memcmp(*publicKey, *derPrefixPub, derPrefixPub->size())) {
|
if (0 == sodium_memcmp(publicKey, *derPrefixPub, derPrefixPub->size())) {
|
||||||
memcpy(mPublicKey, publicKey->data(derPrefixPub->size()), ed25519_pubkey_SIZE);
|
memcpy(mPublicKey, &publicKey[derPrefixPub->size()], ed25519_pubkey_SIZE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -75,6 +74,11 @@ KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publi
|
|||||||
mm->releaseMemory(derPrefixPriv);
|
mm->releaseMemory(derPrefixPriv);
|
||||||
mm->releaseMemory(derPrefixPub);
|
mm->releaseMemory(derPrefixPub);
|
||||||
}
|
}
|
||||||
|
KeyPairHedera::KeyPairHedera(const MemoryBin* privateKey, const MemoryBin* publicKey /* = nullptr*/)
|
||||||
|
: KeyPairHedera(privateKey, publicKey->data(), publicKey->size())
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
KeyPairHedera::~KeyPairHedera()
|
KeyPairHedera::~KeyPairHedera()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -22,6 +22,7 @@ public:
|
|||||||
//! \param privateKey: copy
|
//! \param privateKey: copy
|
||||||
//! \param publicKey: 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(const MemoryBin* privateKey, const MemoryBin* publicKey = nullptr);
|
||||||
|
|
||||||
~KeyPairHedera();
|
~KeyPairHedera();
|
||||||
|
|||||||
@ -62,6 +62,23 @@ namespace controller {
|
|||||||
return nullptr;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,8 @@ namespace controller {
|
|||||||
|
|
||||||
inline Poco::AutoPtr<model::table::CryptoKey> getModel() { return _getModel<model::table::CryptoKey>(); }
|
inline Poco::AutoPtr<model::table::CryptoKey> getModel() { return _getModel<model::table::CryptoKey>(); }
|
||||||
|
|
||||||
|
KeyPairHedera* getKeyPair(Poco::AutoPtr<controller::User> user);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
CryptoKey(model::table::CryptoKey* dbModel);
|
CryptoKey(model::table::CryptoKey* dbModel);
|
||||||
|
|||||||
@ -24,6 +24,11 @@ namespace model {
|
|||||||
const char* getTableName() const { return "crypto_keys"; }
|
const char* getTableName() const { return "crypto_keys"; }
|
||||||
std::string toString();
|
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);
|
static const char* typeToString(KeyType type);
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user