From e52be3a6b82025b6795b73d448381953821d18bb Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 15 May 2018 18:24:50 +0200 Subject: Add methods to store/retrieve encrypted user (using argon2 for hash) --- include/odhtdb/Database.hpp | 9 +++++++++ include/odhtdb/DatabaseStorage.hpp | 27 +++++++++++++++------------ include/odhtdb/Signature.hpp | 3 +++ 3 files changed, 27 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 5ebe9c5..43c1ad9 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -175,6 +175,15 @@ namespace odhtdb void addUser(const DatabaseNode &nodeInfo, const Signature::KeyPair &userToPerformActionWith, const Signature::PublicKey &userToAddPublicKey, const DataView &groupToAddUserTo); ntp::NtpTimestamp getSyncedTimestampUtc() const; + + // Username has to be either unique or if it's the same as existing one, then password has to match. + // Node has to be unique for the user. + // Throws DatabaseStorageWrongPassword or SqlExecException on failure (if username is not unique in node). + void storeUserPasswordEncrypted(const Hash &nodeHash, const std::string &username, const std::string &password, const Signature::KeyPair &keyPair); + + // Returns nodes, public key and private key of encrypted user. + // Throws DatabaseStorageWrongPassword if password for the stored user is wrong. + std::vector getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); private: void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const std::shared_ptr encryptionKey); diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index 886412c..613f2e8 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -10,6 +10,8 @@ #include "OwnedMemory.hpp" #include "DatabaseOperation.hpp" #include "DatabaseOrder.hpp" +#include "sql/SqlQuery.hpp" +#include "sql/SqlExec.hpp" #include #include #include @@ -50,12 +52,6 @@ namespace odhtdb DatabaseStorageCorrupt(const std::string &errMsg) : DatabaseStorageException(errMsg) {} }; - class DatabaseStorageNoSuchLocalStorageUser : public DatabaseStorageException - { - public: - DatabaseStorageNoSuchLocalStorageUser(const std::string &errMsg) : DatabaseStorageException(errMsg) {} - }; - class DatabaseStorageWrongPassword : public DatabaseStorageException { public: @@ -70,6 +66,12 @@ namespace odhtdb using FetchNodeUserActionGapsCallbackFunc = std::function; using FetchNodeUserLatestActionCounterCallbackFunc = std::function; + struct NodeUserKeyPair + { + const Hash nodeHash; + const Signature::KeyPair keyPair; + }; + class DatabaseStorage { public: @@ -109,13 +111,14 @@ namespace odhtdb // Throws DatabaseStorageNotFound if user doesn't exist in node u64 getUserActionCounter(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; - // Username and key pair has to be unique, returns true on success - //bool storeLocalUser(const std::string &username, const Signature::KeyPair &keyPair, const std::string &password); + // Username has to be either unique or if it's the same as existing one, then password has to match. + // Node has to be unique for the user. + // Throws DatabaseStorageWrongPassword or SqlExecException on failure (if username is not unique in node). + void storeUserPasswordEncrypted(const Hash &nodeHash, const std::string &username, const std::string &password, const Signature::KeyPair &keyPair); - // Returns public key and private key of encrypted local user. - // Throws DatabaseStorageNoSuchLocalStorageUser if user does not exist in local storage. - // Throws DatabaseStorageWrongPassword if password for the stored local user is wrong. - //Signature::KeyPair decryptLocalEncryptedUser(const std::string &username, const std::string &password); + // Returns nodes, public key and private key of encrypted user. + // Throws DatabaseStorageWrongPassword if password for the stored user is wrong. + std::vector getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); // Returns true and node decryption key if node exists and we have the decryption key, // otherwise return false and OwnedMemory with data set to nullptr diff --git a/include/odhtdb/Signature.hpp b/include/odhtdb/Signature.hpp index 0fc9087..92042f4 100644 --- a/include/odhtdb/Signature.hpp +++ b/include/odhtdb/Signature.hpp @@ -106,6 +106,9 @@ namespace odhtdb const char* getData() const { return data; } size_t getSize() const { return PRIVATE_KEY_NUM_BYTES; } + bool operator==(const PrivateKey &other) const; + bool operator!=(const PrivateKey &other) const; + // Throws DataSignException if signing data failed for whatever reason. This wont happen unless there is an issue with the private key std::string sign(const DataView &dataToSign) const; std::string toString() const; -- cgit v1.2.3