try to recover priv key from user backup in SigningTransaction if private key is missing

This commit is contained in:
Dario 2020-07-15 14:26:29 +02:00
parent 15d7cf5e61
commit 1b3cd9701c
3 changed files with 19 additions and 5 deletions

View File

@ -295,7 +295,7 @@ namespace controller {
//! \return -1 no matching entry found //! \return -1 no matching entry found
//! \return -2 if user id is not set or invalid //! \return -2 if user id is not set or invalid
//! \return 0 matching entry found //! \return 0 matching entry found
int User::tryLoadPassphraseUserBackup() int User::tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr)
{ {
auto user_model = getModel(); auto user_model = getModel();
if (user_model->getID() <= 0) return -2; if (user_model->getID() <= 0) return -2;
@ -308,7 +308,11 @@ namespace controller {
continue; continue;
} }
auto key_pair = std::unique_ptr<KeyPairEd25519>(user_backup->createGradidoKeyPair()); auto key_pair = std::unique_ptr<KeyPairEd25519>(user_backup->createGradidoKeyPair());
if (key_pair->isTheSame(user_model->getPublicKey())) { if (key_pair->isTheSame(user_model->getPublicKey())) {
if (createdKeyPair) {
*createdKeyPair = key_pair.get();
}
return 0; return 0;
} }
} }

View File

@ -46,7 +46,7 @@ namespace controller {
//! \return -1 no matching entry found //! \return -1 no matching entry found
//! \return -2 user id invalid or net set //! \return -2 user id invalid or net set
//! \return 0 matching entry found //! \return 0 matching entry found
int tryLoadPassphraseUserBackup(); int tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr);
inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); } inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); }
//! \brief try to load user from db via user_id //! \brief try to load user from db via user_id

View File

@ -71,10 +71,20 @@ int SigningTransaction::run() {
//auto privKey = mUser->getPrivKey(); //auto privKey = mUser->getPrivKey();
//if (!mUser->hasPrivKey()) { //if (!mUser->hasPrivKey()) {
auto gradido_key_pair = mNewUser->getGradidoKeyPair(); auto gradido_key_pair = mNewUser->getGradidoKeyPair();
if(!gradido_key_pair || !gradido_key_pair->hasPrivateKey()) { if(!gradido_key_pair || !gradido_key_pair->hasPrivateKey()) {
addError(new Error("SigningTransaction", "user cannot decrypt private key")); KeyPairEd25519** key_pair_ptr = nullptr;
sendErrorsAsEmail(); if (!mNewUser->tryLoadPassphraseUserBackup(key_pair_ptr)) {
return -2; if(mNewUser->setGradidoKeyPair(*key_pair_ptr))
{
mNewUser->getModel()->updatePrivkey();
}
}
else {
addError(new Error("SigningTransaction", "user cannot decrypt private key"));
sendErrorsAsEmail();
return -2;
}
} }
// get body bytes // get body bytes
model::messages::gradido::Transaction transaction; model::messages::gradido::Transaction transaction;