diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-05-15 18:24:50 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-05-15 18:24:53 +0200 |
commit | d8e5c76c364450179f12fa985d50b7e4bfb5aa78 (patch) | |
tree | 567259ef8e3e662d6b742b9d33ad67f5c9f7545e /include | |
parent | 66ec8dd9b9d3f1f71ce2158c2603586814cc7b8d (diff) |
Add methods to store/retrieve encrypted user (using argon2 for hash)
Diffstat (limited to 'include')
-rw-r--r-- | include/odhtdb/Database.hpp | 9 | ||||
-rw-r--r-- | include/odhtdb/DatabaseStorage.hpp | 27 | ||||
-rw-r--r-- | include/odhtdb/Signature.hpp | 3 |
3 files changed, 27 insertions, 12 deletions
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<NodeUserKeyPair> getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); private: void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey); void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedMemory> 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 <vector> #include <stdexcept> #include <boost/filesystem/path.hpp> @@ -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<void(const DataView userPublicKey, u64 start, u64 range)>; using FetchNodeUserLatestActionCounterCallbackFunc = std::function<void(const DataView userPublicKey, u64 latestActionCounter)>; + 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<NodeUserKeyPair> 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; |