add controller classes for new db tables crypto_keys and hedera_ids, update model tables for them

This commit is contained in:
Dario 2020-08-28 13:14:33 +02:00 committed by Ulf Gebhardt
parent 71837dbe61
commit d8766f8d2d
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
11 changed files with 822 additions and 629 deletions

View File

@ -146,3 +146,26 @@ bool KeyPairHedera::verify(const unsigned char* message, size_t messageSize, Mem
}
return true;
}
MemoryBin* KeyPairHedera::getCryptedPrivKey(const Poco::AutoPtr<SecretKeyCryptography> 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;
}

View File

@ -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<SecretKeyCryptography> password) const;
protected:

View File

@ -44,7 +44,7 @@ bool SessionManager::init()
switch (i) {
//case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("/^[a-zA-Z_ -]{3,}$/"); break;
case VALIDATE_NAME: mValidations[i] = new Poco::RegularExpression("^[^<>&;]{3,}$"); break;
case VALIDATE_EMAIL: mValidations[i] = new Poco::RegularExpression("^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"); break;
case VALIDATE_EMAIL: mValidations[i] = new Poco::RegularExpression("^[a-zA-Z0-9.!#$%&<EFBFBD>*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"); break;
case VALIDATE_PASSWORD: mValidations[i] = new Poco::RegularExpression("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&+-_])[A-Za-z0-9@$!%*?&+-_]{8,}$"); break;
case VALIDATE_PASSPHRASE: mValidations[i] = new Poco::RegularExpression("^(?:[a-z]* ){23}[a-z]*\s*$"); break;
case VALIDATE_HAS_NUMBER: mValidations[i] = new Poco::RegularExpression(".*[0-9].*"); break;
@ -354,6 +354,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;
int iResult = result->isActive();
if (iResult == -1) {
@ -489,9 +490,9 @@ Session* SessionManager::findByEmail(const std::string& email)
mDeadLockedSessionCount++;
}
auto user = it->second->getNewUser();
if (email == user->getModel()->getEmail()) {
return it->second;
}
if (email == user->getModel()->getEmail()) {
return it->second;
}
}
mWorkingMutex.unlock();
return nullptr;
@ -499,12 +500,11 @@ if (email == user->getModel()->getEmail()) {
void SessionManager::checkTimeoutSession()
{
try {
//Poco::Mutex::ScopedLock _lock(mWorkingMutex, 500);
mWorkingMutex.tryLock(500);
}
catch (Poco::TimeoutException& ex) {
catch (Poco::TimeoutException &ex) {
printf("[SessionManager::checkTimeoutSession] exception timeout mutex: %s\n", ex.displayText().data());
return;
}
@ -515,7 +515,6 @@ void SessionManager::checkTimeoutSession()
//auto timeout = Poco::Timespan(1, 0);
std::stack<int> toRemove;
for (auto it = mRequestSessionMap.begin(); it != mRequestSessionMap.end(); it++) {
if (it->second->tryLock()) {
// skip already disabled sessions
if (!it->second->isActive()) {

View File

@ -0,0 +1,44 @@
#include "CryptoKey.h"
namespace controller {
CryptoKey::CryptoKey(model::table::CryptoKey* dbModel)
{
mDBModel = dbModel;
}
CryptoKey::~CryptoKey()
{
}
Poco::AutoPtr<CryptoKey> CryptoKey::create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr<controller::User> 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>(cryptoKey);
}
Poco::AutoPtr<CryptoKey> 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>(cryptoKey);
}
return nullptr;
}
}

View File

@ -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<CryptoKey> create(const KeyPairHedera* hederaKeyPair, Poco::AutoPtr<controller::User> user);
//! if returned ptr is NULL, dataset not found
static Poco::AutoPtr<CryptoKey> load(int id);
inline bool deleteFromDB() { return mDBModel->deleteFromDB(); }
inline Poco::AutoPtr<model::table::CryptoKey> getModel() { return _getModel<model::table::CryptoKey>(); }
protected:
CryptoKey(model::table::CryptoKey* dbModel);
};
}
#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_CRYPTO_KEY_INCLUDE

View File

@ -0,0 +1,32 @@
#include "HederaId.h"
namespace controller {
HederaId::HederaId(model::table::HederaId* dbModel)
{
mDBModel = dbModel;
}
HederaId::~HederaId()
{
}
Poco::AutoPtr<HederaId> 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<HederaId>(hedera_id);
}
Poco::AutoPtr<HederaId> HederaId::load(int id)
{
auto db = new model::table::HederaId();
if (1 == db->loadFromDB("id", id)) {
auto cryptoKey = new HederaId(db);
return Poco::AutoPtr<HederaId>(cryptoKey);
}
return nullptr;
}
}

View File

@ -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<HederaId> create(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num);
static Poco::AutoPtr<HederaId> load(int id);
inline bool deleteFromDB() { return mDBModel->deleteFromDB(); }
inline Poco::AutoPtr<model::table::HederaId> getModel() { return _getModel<model::table::HederaId>(); }
protected:
HederaId(model::table::HederaId* dbModel);
};
}
#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_HEDERA_ID_INCLUDE

View File

@ -9,6 +9,22 @@ namespace model {
}
CryptoKey::CryptoKey(MemoryBin* privateKey, MemoryBin* publicKey, KeyType keyType)
: mKeyType(keyType)
{
if (!privateKey) {
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>();
} else {
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>(Poco::Data::BLOB(*privateKey, privateKey->size()));
}
if (!publicKey) {
mPublicKey = Poco::Nullable<Poco::Data::BLOB>();
} else {
mPublicKey = Poco::Nullable<Poco::Data::BLOB>(Poco::Data::BLOB(*publicKey, publicKey->size()));
}
}
CryptoKey::~CryptoKey()
{

View File

@ -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

View File

@ -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)
{
}

View File

@ -11,6 +11,7 @@ namespace model {
{
public:
HederaId();
HederaId(Poco::UInt64 shardNum, Poco::UInt64 realmNum, Poco::UInt64 num);
~HederaId();
// generic db operations