From cd4ff393e72331195687c1223aaaa432be3e5d0e Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Fri, 27 Apr 2018 04:15:33 +0200 Subject: Add local user storage function (locally stored encrypted user private key) --- include/odhtdb/Database.hpp | 2 +- include/odhtdb/DatabaseStorage.hpp | 23 ++++++++++------------- include/odhtdb/Encryption.hpp | 1 + include/odhtdb/LocalUser.hpp | 12 +++--------- include/odhtdb/LocalUserEncrypted.hpp | 24 +++++++++++++++--------- include/odhtdb/PasswordHash.hpp | 2 ++ include/odhtdb/User.hpp | 7 +++---- 7 files changed, 35 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 846ddaa..3e4a393 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -139,7 +139,7 @@ namespace odhtdb void seed(const DatabaseNode &nodeToSeed); // Throws DatabaseCreateException on failure. - std::unique_ptr create(const std::string &ownerName, const std::string &ownerPlainPassword, const std::string &nodeName); + std::unique_ptr create(const std::string &ownerName, const std::string &nodeName); // Throws DatabaseAddException on failure void addData(const DatabaseNode &nodeInfo, LocalUser *userToPerformActionWith, DataView dataToAdd); // Throws PermissionDeniedException if user @userToPerformActionWith is not allowed to add user @userToAdd to group @groupToAddUserTo diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index a9e04ef..f3c3087 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -6,6 +6,8 @@ #include "Signature.hpp" #include "Encryption.hpp" #include "Group.hpp" +#include "LocalUser.hpp" +#include "LocalUserEncrypted.hpp" #include #include #include @@ -14,8 +16,6 @@ namespace odhtdb { - class User; - struct DatabaseStorageObject { DataView data; @@ -70,12 +70,6 @@ namespace odhtdb class DatabaseStorage { public: - struct UserData - { - User *user; - u8 hashedPassword[HASHED_PASSWORD_LEN]; // All bytes are zero if user is not local - }; - // Throws DatabaseStorageCorrupt if storage is corrupted DatabaseStorage(const boost::filesystem::path &storagePath); @@ -99,13 +93,13 @@ namespace odhtdb const DatabaseStorageObjectList* getStorage(const Hash &hash) const; // Returns nullptr if a group with id @groupId doesn't exist in node @nodeHash or if no node with id @nodeHash exists - Group* getGroupById(const Hash &nodeHash, uint8_t groupId[GROUP_ID_LENGTH]); + Group* getGroupById(const Hash &nodeHash, uint8_t groupId[GROUP_ID_LENGTH]) const; // Returns nullptr if a user with public key @publicKey doesn't exist in node @nodeHash or if no node with id @nodeHash exists - User* getUserByPublicKey(const Hash &nodeHash, Signature::PublicKey &userPublicKey); + User* getUserByPublicKey(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; - // Return users in node, or nullptr if no node with id @nodeHash exists - const Signature::MapPublicKey* getUsersData(const Hash &nodeHash) const; + // Username, public key and private key has to be unique + bool storeLocalUser(const std::string &username, const Signature::PublicKey &publicKey, const Signature::PrivateKey &privateKey, const std::string &password); const dht::crypto::Identity& getIdentity() const; @@ -115,6 +109,7 @@ namespace odhtdb void loadGroupsFromFile(); void loadUsersFromFile(); void loadDataFromFile(); + void loadLocalUsersFromFile(); void loadMetadataFromFile(); void loadStorageCreate(sibs::SafeDeserializer &deserializer); void loadStorageAppend(sibs::SafeDeserializer &deserializer); @@ -122,12 +117,14 @@ namespace odhtdb DatabaseStorageMap storageMap; DatabaseStorageQuarantineMap quarantineStorageMap; SetHash storedDataHash; // Prevent duplicate data from being added - MapHash*> nodePublicKeyUserDataMap; + MapHash*> nodePublicKeyUserDataMap; MapHash*> nodeGroupByIdMap; + std::unordered_map nameLocalUsersMap; boost::filesystem::path groupsFilePath; boost::filesystem::path usersFilePath; boost::filesystem::path dataFilePath; boost::filesystem::path metadataFilePath; + boost::filesystem::path localUsersFilePath; u8 passwordSalt[PASSWORD_SALT_LEN]; std::pair, std::shared_ptr> identity; }; diff --git a/include/odhtdb/Encryption.hpp b/include/odhtdb/Encryption.hpp index 5271cbd..e710760 100644 --- a/include/odhtdb/Encryption.hpp +++ b/include/odhtdb/Encryption.hpp @@ -11,6 +11,7 @@ namespace odhtdb { + const int ENCRYPTION_CHECKSUM_BYTE_SIZE = 16; const int ENCRYPTION_NONCE_BYTE_SIZE = 24; const int ENCRYPTION_KEY_BYTE_SIZE = 32; diff --git a/include/odhtdb/LocalUser.hpp b/include/odhtdb/LocalUser.hpp index 0312a38..b9bdde6 100644 --- a/include/odhtdb/LocalUser.hpp +++ b/include/odhtdb/LocalUser.hpp @@ -8,9 +8,9 @@ namespace odhtdb class LocalUser : public User { public: - static LocalUser* create(const Signature::KeyPair &keyPair, const std::string &name, Group *group, const std::string &plainPassword) + static LocalUser* create(const Signature::KeyPair &keyPair, const std::string &name, Group *group) { - return new LocalUser(keyPair, name, group, plainPassword); + return new LocalUser(keyPair, name, group); } const Signature::PublicKey& getPublicKey() const override @@ -22,15 +22,9 @@ namespace odhtdb { return keyPair.getPrivateKey(); } - - const std::string& getPlainPassword() const - { - return plainPassword; - } private: - LocalUser(const Signature::KeyPair &_keyPair, const std::string &name, Group *group, const std::string &plainPassword); + LocalUser(const Signature::KeyPair &_keyPair, const std::string &name, Group *group); private: Signature::KeyPair keyPair; - std::string plainPassword; }; } diff --git a/include/odhtdb/LocalUserEncrypted.hpp b/include/odhtdb/LocalUserEncrypted.hpp index c250d13..952892f 100644 --- a/include/odhtdb/LocalUserEncrypted.hpp +++ b/include/odhtdb/LocalUserEncrypted.hpp @@ -1,15 +1,15 @@ #pragma once -#include "User.hpp" #include "types.hpp" #include "Encryption.hpp" +#include "Signature.hpp" namespace odhtdb { struct EncryptedPrivateKey { u8 nonce[ENCRYPTION_NONCE_BYTE_SIZE]; - u8 encryptedPrivateKey[16 + PRIVATE_KEY_NUM_BYTES]; + u8 encryptedPrivateKey[ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES]; EncryptedPrivateKey(); EncryptedPrivateKey(const EncryptedPrivateKey &other); @@ -19,15 +19,15 @@ namespace odhtdb }; // Local user with encrypted private key - class LocalUserEncrypted : public User + class LocalUserEncrypted { public: - static LocalUserEncrypted* create(const Signature::PublicKey &publicKey, const EncryptedPrivateKey &encryptedPrivateKey, const std::string &name, Group *group) + static LocalUserEncrypted* create(const Signature::PublicKey &publicKey, const EncryptedPrivateKey &encryptedPrivateKey, const std::string &name) { - return new LocalUserEncrypted(publicKey, encryptedPrivateKey, name, group); + return new LocalUserEncrypted(publicKey, encryptedPrivateKey, name); } - const Signature::PublicKey& getPublicKey() const override + const Signature::PublicKey& getPublicKey() const { return publicKey; } @@ -36,16 +36,22 @@ namespace odhtdb { return encryptedPrivateKey; } + + const std::string& getName() const + { + return name; + } private: - LocalUserEncrypted(const Signature::PublicKey &_publicKey, const EncryptedPrivateKey &_encryptedPrivateKey, const std::string &name, Group *group) : - User(User::Type::LOCAL_ENCRYPTED, name, group), + LocalUserEncrypted(const Signature::PublicKey &_publicKey, const EncryptedPrivateKey &_encryptedPrivateKey, const std::string &_name) : publicKey(_publicKey), - encryptedPrivateKey(_encryptedPrivateKey) + encryptedPrivateKey(_encryptedPrivateKey), + name(_name) { } private: Signature::PublicKey publicKey; EncryptedPrivateKey encryptedPrivateKey; + std::string name; }; } diff --git a/include/odhtdb/PasswordHash.hpp b/include/odhtdb/PasswordHash.hpp index 08b1857..bc02c53 100644 --- a/include/odhtdb/PasswordHash.hpp +++ b/include/odhtdb/PasswordHash.hpp @@ -5,5 +5,7 @@ namespace odhtdb { + const int HASH_PASSWORD_LENGTH = 32; + OwnedMemory hashPassword(const DataView &plainPassword, const DataView &salt); } diff --git a/include/odhtdb/User.hpp b/include/odhtdb/User.hpp index d6e551a..3236d4c 100644 --- a/include/odhtdb/User.hpp +++ b/include/odhtdb/User.hpp @@ -26,21 +26,20 @@ namespace odhtdb enum class Type : u8 { LOCAL, - LOCAL_ENCRYPTED, REMOTE }; virtual ~User(){} - void addToGroup(Group *group); + virtual void addToGroup(Group *group); Type getType() const { return type; } const std::string& getName() const { return name; } - const std::vector& getGroups() const { return groups; } + virtual const std::vector& getGroups() const { return groups; } virtual const Signature::PublicKey& getPublicKey() const = 0; protected: User(Type type, const std::string &name, Group *group); - private: + protected: Type type; std::string name; std::vector groups; -- cgit v1.2.3