#include "CryptoKey.h" #include "../SingletonManager/ErrorManager.h" #include "../lib/DataTypeConverter.h" namespace controller { CryptoKey::CryptoKey(model::table::CryptoKey* dbModel) { mDBModel = dbModel; } CryptoKey::~CryptoKey() { } Poco::AutoPtr CryptoKey::create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr user) { auto mm = MemoryManager::getInstance(); auto encrypted_priv_key = hederaKeyPair->getCryptedPrivKey(user->getPassword()); auto public_key = hederaKeyPair->getPublicKeyCopy(); auto db = new model::table::CryptoKey(encrypted_priv_key, public_key, model::table::KEY_TYPE_ED25519_HEDERA); mm->releaseMemory(encrypted_priv_key); mm->releaseMemory(public_key); auto cryptoKey = new CryptoKey(db); return Poco::AutoPtr(cryptoKey); } Poco::AutoPtr CryptoKey::load(int id) { auto db = new model::table::CryptoKey(); if (1 == db->loadFromDB("id", id)) { auto cryptoKey = new CryptoKey(db); return Poco::AutoPtr(cryptoKey); } return nullptr; } Poco::AutoPtr CryptoKey::load(MemoryBin* publicKey) { return load(*publicKey, publicKey->size()); } Poco::AutoPtr CryptoKey::load(const unsigned char* publicKey, size_t size) { assert(publicKey); assert(size); Poco::Data::BLOB public_key_blob(publicKey, size); auto db = new model::table::CryptoKey(); auto count = db->loadFromDB("public_key", public_key_blob); if (!count) return nullptr; if (1 == count) return new CryptoKey(db); auto em = ErrorManager::getInstance(); em->addError(new Error("CryptoKey::load", "found more than one crypto key with same public key")); em->sendErrorsAsEmail(); return nullptr; } std::unique_ptr CryptoKey::getKeyPair(Poco::AutoPtr user) { auto model = getModel(); auto password = user->getPassword(); auto mm = MemoryManager::getInstance(); if (!password || !model->hasPrivateKeyEncrypted()) { printf("[CryptoKey::getKeyPair] return null, password empty or no private key\n"); return nullptr; } MemoryBin* clearPassword = nullptr; auto encrypted_private_key = model->getPrivateKeyEncrypted(); auto encrypted_private_key_hex_string = DataTypeConverter::binToHex(encrypted_private_key); printf("[CryptoKey::getKeyPair] encrypted private key hex: %s\n", encrypted_private_key_hex_string.data()); if (password->decrypt(model->getPrivateKeyEncrypted(), &clearPassword) != SecretKeyCryptography::AUTH_DECRYPT_OK) { printf("[CryptoKey::getKeyPair] return null, error decrypting\n"); return nullptr; } auto key_pair = std::make_unique(clearPassword, model->getPublicKey(), model->getPublicKeySize()); mm->releaseMemory(clearPassword); return key_pair; } }