diff --git a/src/cpp/Crypto/KeyPairHedera.cpp b/src/cpp/Crypto/KeyPairHedera.cpp index 80c4649b1..a0e0c5d43 100644 --- a/src/cpp/Crypto/KeyPairHedera.cpp +++ b/src/cpp/Crypto/KeyPairHedera.cpp @@ -145,4 +145,27 @@ bool KeyPairHedera::verify(const unsigned char* message, size_t messageSize, Mem return false; } return true; +} + +MemoryBin* KeyPairHedera::getCryptedPrivKey(const Poco::AutoPtr password) const +{ + if (password.isNull()) return nullptr; + if (!mPrivateKey) return nullptr; + + MemoryBin* encryptedKey = nullptr; + if (SecretKeyCryptography::AUTH_ENCRYPT_OK == password->encrypt(mPrivateKey, &encryptedKey)) { + return encryptedKey; + } + else { + return nullptr; + } + +} + +MemoryBin* KeyPairHedera::getPublicKeyCopy() const +{ + auto mm = MemoryManager::getInstance(); + auto public_key = mm->getFreeMemory(ed25519_pubkey_SIZE); + memcpy(*public_key, mPublicKey, ed25519_pubkey_SIZE); + return public_key; } \ No newline at end of file diff --git a/src/cpp/Crypto/KeyPairHedera.h b/src/cpp/Crypto/KeyPairHedera.h index 358a3b680..871453afc 100644 --- a/src/cpp/Crypto/KeyPairHedera.h +++ b/src/cpp/Crypto/KeyPairHedera.h @@ -13,6 +13,7 @@ #include "sodium.h" +#include "SecretKeyCryptography.h" #include "iroha-ed25519/include/ed25519/ed25519.h" class KeyPairHedera : public IKeyPair @@ -34,6 +35,7 @@ public: bool verify(const unsigned char* message, size_t messageSize, MemoryBin* signature) const; inline const unsigned char* getPublicKey() const { return mPublicKey; } + MemoryBin* getPublicKeyCopy() const; inline bool isTheSame(const KeyPairHedera& b) const { return 0 == sodium_memcmp(mPublicKey, b.mPublicKey, ed25519_pubkey_SIZE); @@ -60,7 +62,8 @@ public: inline bool hasPrivateKey() const { return mPrivateKey != nullptr; } - + //! \brief only way to get a private key.. encrypted + MemoryBin* getCryptedPrivKey(const Poco::AutoPtr password) const; protected: diff --git a/src/cpp/SingletonManager/SessionManager.cpp b/src/cpp/SingletonManager/SessionManager.cpp index 3c2144b1a..8de83d6c5 100644 --- a/src/cpp/SingletonManager/SessionManager.cpp +++ b/src/cpp/SingletonManager/SessionManager.cpp @@ -329,6 +329,7 @@ Session* SessionManager::getSession(int handle) //mWorkingMutex.lock(); auto it = mRequestSessionMap.find(handle); if (it != mRequestSessionMap.end()) { + printf("[SessionManager::getSession] found existing session, try if active...\n"); result = it->second; if (!result->isActive()) { //printf("[SessionManager::getSession] session isn't active\n"); diff --git a/src/cpp/controller/CryptoKey.cpp b/src/cpp/controller/CryptoKey.cpp new file mode 100644 index 000000000..809673bc5 --- /dev/null +++ b/src/cpp/controller/CryptoKey.cpp @@ -0,0 +1,44 @@ + +#include "CryptoKey.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; + } + + +} + diff --git a/src/cpp/controller/CryptoKey.h b/src/cpp/controller/CryptoKey.h new file mode 100644 index 000000000..9e76123ef --- /dev/null +++ b/src/cpp/controller/CryptoKey.h @@ -0,0 +1,35 @@ +#ifndef GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE +#define GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE + +#include "../model/table/CryptoKey.h" +#include "../Crypto/KeyPairHedera.h" + +#include "Poco/SharedPtr.h" + +#include "TableControllerBase.h" +#include "User.h" + +namespace controller { + class CryptoKey : public TableControllerBase + { + public: + + ~CryptoKey(); + + static Poco::AutoPtr create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr user); + + //! if returned ptr is NULL, dataset not found + static Poco::AutoPtr load(int id); + + inline bool deleteFromDB() { return mDBModel->deleteFromDB(); } + + inline Poco::AutoPtr getModel() { return _getModel(); } + + + protected: + CryptoKey(model::table::CryptoKey* dbModel); + + }; +} + +#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE \ No newline at end of file diff --git a/src/cpp/controller/HederaId.cpp b/src/cpp/controller/HederaId.cpp new file mode 100644 index 000000000..e6d281907 --- /dev/null +++ b/src/cpp/controller/HederaId.cpp @@ -0,0 +1,32 @@ +#include "HederaId.h" + +namespace controller { + + HederaId::HederaId(model::table::HederaId* dbModel) + { + mDBModel = dbModel; + } + + HederaId::~HederaId() + { + + } + + Poco::AutoPtr HederaId::create(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num) + { + auto db = new model::table::HederaId(shardNum, realmNum, num); + + auto hedera_id = new HederaId(db); + return Poco::AutoPtr(hedera_id); + } + + Poco::AutoPtr HederaId::load(int id) + { + auto db = new model::table::HederaId(); + if (1 == db->loadFromDB("id", id)) { + auto cryptoKey = new HederaId(db); + return Poco::AutoPtr(cryptoKey); + } + return nullptr; + } +} \ No newline at end of file diff --git a/src/cpp/controller/HederaId.h b/src/cpp/controller/HederaId.h new file mode 100644 index 000000000..f02e69373 --- /dev/null +++ b/src/cpp/controller/HederaId.h @@ -0,0 +1,32 @@ +#ifndef GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE +#define GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE + +#include "../model/table/HederaId.h" + +#include "Poco/SharedPtr.h" + +#include "TableControllerBase.h" + +namespace controller { + class HederaId : public TableControllerBase + { + public: + + ~HederaId(); + + static Poco::AutoPtr create(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num); + + static Poco::AutoPtr load(int id); + + inline bool deleteFromDB() { return mDBModel->deleteFromDB(); } + + inline Poco::AutoPtr getModel() { return _getModel(); } + + + protected: + HederaId(model::table::HederaId* dbModel); + + }; +} + +#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE \ No newline at end of file diff --git a/src/cpp/model/table/CryptoKey.cpp b/src/cpp/model/table/CryptoKey.cpp index 201047a5b..d36f44de6 100644 --- a/src/cpp/model/table/CryptoKey.cpp +++ b/src/cpp/model/table/CryptoKey.cpp @@ -9,6 +9,22 @@ namespace model { } + CryptoKey::CryptoKey(MemoryBin* privateKey, MemoryBin* publicKey, KeyType keyType) + : mKeyType(keyType) + { + if (!privateKey) { + mPrivateKey = Poco::Nullable(); + } else { + mPrivateKey = Poco::Nullable(Poco::Data::BLOB(*privateKey, privateKey->size())); + } + + if (!publicKey) { + mPublicKey = Poco::Nullable(); + } else { + mPublicKey = Poco::Nullable(Poco::Data::BLOB(*publicKey, publicKey->size())); + } + } + CryptoKey::~CryptoKey() { diff --git a/src/cpp/model/table/CryptoKey.h b/src/cpp/model/table/CryptoKey.h index f4fd8bebf..58d6df5dc 100644 --- a/src/cpp/model/table/CryptoKey.h +++ b/src/cpp/model/table/CryptoKey.h @@ -8,8 +8,8 @@ namespace model { namespace table { enum KeyType { - KEY_TYPE_SODIUM_ED25519 = 0, - KEY_TYPE_ED25519_REF10 = 1, + KEY_TYPE_ED25519_SODIUM = 0, + KEY_TYPE_ED25519_HEDERA = 1, KEY_TYPE_COUNT }; @@ -17,6 +17,7 @@ namespace model { { public: CryptoKey(); + CryptoKey(MemoryBin* privateKey, MemoryBin* publicKey, KeyType keyType); ~CryptoKey(); // generic db operations diff --git a/src/cpp/model/table/HederaId.cpp b/src/cpp/model/table/HederaId.cpp index b048a0d71..a4f25cc78 100644 --- a/src/cpp/model/table/HederaId.cpp +++ b/src/cpp/model/table/HederaId.cpp @@ -5,6 +5,13 @@ using namespace Poco::Data::Keywords; namespace model { namespace table { HederaId::HederaId() + : mShardNum(0), mRealmNum(0), mNum(0) + { + + } + + HederaId::HederaId(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num) + : mShardNum(shardNum), mRealmNum(realmNum), mNum(num) { } diff --git a/src/cpp/model/table/HederaId.h b/src/cpp/model/table/HederaId.h index ef35b2e70..c36d1f656 100644 --- a/src/cpp/model/table/HederaId.h +++ b/src/cpp/model/table/HederaId.h @@ -11,6 +11,7 @@ namespace model { { public: HederaId(); + HederaId(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num); ~HederaId(); // generic db operations