From b8e93e8142d2f28711d43bdada5ef0800c35d52e Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 24 Jun 2020 10:34:00 +0200 Subject: [PATCH] change AuthenticatedEncryption to use auto ptr, if password will be calculated (changed password) and use to decrpyt it could be lead to an error --- src/cpp/Crypto/AuthenticatedEncryption.h | 9 ++++++--- src/cpp/Crypto/KeyPairEd25519.cpp | 8 ++++---- src/cpp/Crypto/KeyPairEd25519.h | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cpp/Crypto/AuthenticatedEncryption.h b/src/cpp/Crypto/AuthenticatedEncryption.h index 634c73a5d..154286883 100644 --- a/src/cpp/Crypto/AuthenticatedEncryption.h +++ b/src/cpp/Crypto/AuthenticatedEncryption.h @@ -3,9 +3,11 @@ #include "../SingletonManager/MemoryManager.h" +#include "../lib/AutoPtrContainer.h" #include #include + /*! * * \author: Dario Rekowski @@ -18,7 +20,7 @@ typedef Poco::UInt64 KeyHashed; -class AuthenticatedEncryption +class AuthenticatedEncryption : public AutoPtrContainer { public: @@ -42,9 +44,10 @@ public: inline KeyHashed getKeyHashed() const { std::shared_lock _lock(mWorkingMutex); return mEncryptionKeyHash; } - inline bool operator == (const AuthenticatedEncryption& b) const { + inline bool operator == (const Poco::AutoPtr& b) const { std::shared_lock _lock(mWorkingMutex); - return mEncryptionKeyHash == b.getKeyHashed(); + if (b.isNull()) return false; + return mEncryptionKeyHash == b->getKeyHashed(); } inline bool operator == (const KeyHashed& hash) const { return mEncryptionKeyHash == hash; diff --git a/src/cpp/Crypto/KeyPairEd25519.cpp b/src/cpp/Crypto/KeyPairEd25519.cpp index e06ff02d8..e2262b6a2 100644 --- a/src/cpp/Crypto/KeyPairEd25519.cpp +++ b/src/cpp/Crypto/KeyPairEd25519.cpp @@ -37,11 +37,11 @@ KeyPairEd25519::~KeyPairEd25519() } } -KeyPairEd25519* KeyPairEd25519::create(const Passphrase* passphrase) +KeyPairEd25519* KeyPairEd25519::create(const Poco::AutoPtr passphrase) { //auto er = ErrorManager::getInstance(); auto mm = MemoryManager::getInstance(); - assert(passphrase); + assert(!passphrase.isNull()); // libsodium doc: https://libsodium.gitbook.io/doc/advanced/hmac-sha2 // https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki @@ -152,9 +152,9 @@ MemoryBin* KeyPairEd25519::sign(const MemoryBin* message) const } -MemoryBin* KeyPairEd25519::getCryptedPrivKey(const AuthenticatedEncryption* password) const +MemoryBin* KeyPairEd25519::getCryptedPrivKey(const Poco::AutoPtr password) const { - if (!password) return nullptr; + if (password.isNull()) return nullptr; if (!mSodiumSecret) return nullptr; MemoryBin* encryptedKey = nullptr; diff --git a/src/cpp/Crypto/KeyPairEd25519.h b/src/cpp/Crypto/KeyPairEd25519.h index 15bd8a89d..0c0a1eb61 100644 --- a/src/cpp/Crypto/KeyPairEd25519.h +++ b/src/cpp/Crypto/KeyPairEd25519.h @@ -29,7 +29,7 @@ public: //! \param passphrase must contain word indices //! \return create KeyPairEd25519, caller muss call delete at return after finish - static KeyPairEd25519* create(const Passphrase* passphrase); + static KeyPairEd25519* create(const Poco::AutoPtr passphrase); //! \return caller take ownership of return value MemoryBin* sign(const MemoryBin* message) const; @@ -60,7 +60,7 @@ public: inline bool hasPrivateKey() const { return mSodiumSecret != nullptr; } //! \brief only way to get a private key.. encrypted - MemoryBin* getCryptedPrivKey(const AuthenticatedEncryption* password) const; + MemoryBin* getCryptedPrivKey(const Poco::AutoPtr password) const; protected: