From 230e61091b401b8b2bb9496d557a15660fb5072b Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Fri, 9 Mar 2018 10:26:55 +0100 Subject: Partially implement 'add' operation --- include/Database.hpp | 39 ++++++++++++++++++++++----------------- include/DatabaseStorage.hpp | 42 ++++++++++++++++++++++++++++++------------ include/Encryption.hpp | 7 +++++-- include/Hash.hpp | 11 ++++++++++- include/Permission.hpp | 14 -------------- include/Signature.hpp | 15 +++++++++++++++ include/StagedObject.hpp | 10 +++++----- 7 files changed, 87 insertions(+), 51 deletions(-) delete mode 100644 include/Permission.hpp (limited to 'include') diff --git a/include/Database.hpp b/include/Database.hpp index d160133..1024fe0 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -16,6 +16,7 @@ namespace odhtdb { + class User; class LocalUser; class Group; @@ -74,35 +75,39 @@ namespace odhtdb { DISABLE_COPY(DatabaseAddRequest) - u16 packetStructureVersion; u64 timestamp; // In microseconds - Signature::PublicKey creatorPublicKey; - DataView data; + const User *creatorUser; + Decryption decryptedData; - DatabaseAddRequest(u16 _packetStructureVersion, u64 _timestamp, Signature::PublicKey &&_creatorPublicKey, DataView &_data) : - packetStructureVersion(_packetStructureVersion), + DatabaseAddRequest(u64 _timestamp, const User *_creatorUser, Decryption &&_decryptedData) : timestamp(_timestamp), - creatorPublicKey(std::move(_creatorPublicKey)), - data(_data) + creatorUser(_creatorUser), + decryptedData(std::move(_decryptedData)) { } - ~DatabaseAddRequest() + DatabaseAddRequest(DatabaseAddRequest &&other) { - free(data.data); - data = DataView(); + timestamp = other.timestamp; + creatorUser = other.creatorUser; + decryptedData = std::move(other.decryptedData); + + other.timestamp = 0; + other.creatorUser = nullptr; } }; class DatabaseCreateResponse { public: - DatabaseCreateResponse(const std::shared_ptr &key, const std::shared_ptr &hash); + DatabaseCreateResponse(LocalUser *nodeAdminUser, const std::shared_ptr &key, const std::shared_ptr &hash); + const LocalUser* getNodeAdminUser() const; const std::shared_ptr getNodeEncryptionKey() const; const std::shared_ptr getRequestHash() const; private: + LocalUser *nodeAdminUser; std::shared_ptr key; std::shared_ptr hash; }; @@ -115,15 +120,15 @@ namespace odhtdb void seed(const std::shared_ptr hash, const std::shared_ptr encryptionKey); // Throws DatabaseCreateException on failure. - std::unique_ptr create(const LocalUser *owner, const std::string &name); + std::unique_ptr create(const std::string &ownerName, const std::string &nodeName); // Throws DatabaseAddException on failure - void add(const LocalUser *owner, const Key &key, DataView data); + void add(const std::unique_ptr &nodeInfo, DataView dataToAdd); void commit(); private: // Throws CommitCreateException on failure - void commitStagedCreateObject(const std::unique_ptr &stagedObject); + void commitStagedCreateObject(const std::unique_ptr &stagedObject); // Throws CommitAddException on failure - void commitStagedAddObject(const DataView &stagedObject); + void commitStagedAddObject(const std::unique_ptr &stagedObject); ntp::NtpTimestamp getSyncedTimestampUtc() const; DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); DatabaseAddRequest deserializeAddRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); @@ -131,8 +136,8 @@ namespace odhtdb bool listenAddData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); private: dht::DhtRunner node; - std::vector> stagedCreateObjects; - std::vector> stagedAddObjects; + std::vector> stagedCreateObjects; + std::vector> stagedAddObjects; DatabaseStorage databaseStorage; }; } diff --git a/include/DatabaseStorage.hpp b/include/DatabaseStorage.hpp index 6f251d1..fd29050 100644 --- a/include/DatabaseStorage.hpp +++ b/include/DatabaseStorage.hpp @@ -10,27 +10,33 @@ namespace odhtdb { class Group; + class User; struct DatabaseStorageObject { DataView data; - u64 timestamp; // In microseconds + u64 createdTimestamp; // In microseconds Signature::PublicKey creatorPublicKey; - DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : - data(_data), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey) - { - - } + DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey); }; struct DatabaseStorageObjectList { - u64 timestamp; // In microseconds + DataView data; + u64 createdTimestamp; // In microseconds std::vector groups; - std::vector objects; - u8 *createData; - usize createDataSize; + std::vector objects; + }; + + struct DatabaseStorageQuarantineObject + { + DataView data; + u64 createdTimestamp; // In microseconds + u64 storedTimestamp; // In microseconds + Signature::PublicKey creatorPublicKey; + + DatabaseStorageQuarantineObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey); }; class DatabaseStorageAlreadyExists : public std::runtime_error @@ -46,6 +52,7 @@ namespace odhtdb }; using DatabaseStorageMap = MapHashKey; + using DatabaseStorageQuarantineMap = Signature::MapPublicKey>; class DatabaseStorage { @@ -54,11 +61,22 @@ namespace odhtdb void createStorage(const Hash &hash, Group *creatorGroup, u64 timestamp, const u8 *data, usize dataSize); // Throws DatabaseStorageNotFound if data with hash does not exist - void appendStorage(const Hash &hash, DataView &data, u64 timestamp, const Signature::PublicKey &creatorPublicKey); + void appendStorage(const Hash &hash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize); + + void addToQuarantine(const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize); - // Returns nullptr if not storage with provided hash exists + // Returns nullptr if no storage with provided hash exists const DatabaseStorageObjectList* getStorage(const Hash &hash) const; + + // Returns nullptr if no node with the user exists + const Hash* getNodeByUserPublicKey(const Signature::PublicKey &userPublicKey) const; + + // Returns nullptr if no user with public key exists + const User* getUserByPublicKey(const Signature::PublicKey &userPublicKey) const; private: DatabaseStorageMap storageMap; + DatabaseStorageQuarantineMap quarantineStorageMap; + Signature::MapPublicKey userPublicKeyNodeMap; + Signature::MapPublicKey publicKeyUserMap; }; } diff --git a/include/Encryption.hpp b/include/Encryption.hpp index b2afe49..4697b35 100644 --- a/include/Encryption.hpp +++ b/include/Encryption.hpp @@ -31,8 +31,7 @@ namespace odhtdb DISABLE_COPY(Encryption) public: // Throws EncryptionException on failure (or std::bad_alloc on failed memory allocation) - Encryption(const DataView &data) : Encryption(data, DataView()) {} - Encryption(const DataView &data, const DataView &additionalData); + Encryption(const DataView &data, const DataView &additionalData = DataView(), const DataView &key = DataView()); ~Encryption(); DataView getKey() const; @@ -49,8 +48,12 @@ namespace odhtdb { DISABLE_COPY(Decryption) public: + Decryption() : decryptedText(nullptr), decryptedTextLength(0) {} + // Throws DecryptionException on failure Decryption(const DataView &data, const DataView &nonce, const DataView &key); + Decryption(Decryption &&other); + Decryption& operator=(Decryption &&other); ~Decryption(); DataView getDecryptedText() const; diff --git a/include/Hash.hpp b/include/Hash.hpp index d7c90b0..bd87b69 100644 --- a/include/Hash.hpp +++ b/include/Hash.hpp @@ -1,7 +1,6 @@ #pragma once #include "utils.hpp" -#include #include #include @@ -9,6 +8,15 @@ namespace odhtdb { const int HASH_BYTE_SIZE = 32; + // Source: https://stackoverflow.com/a/11414104 (public license) + static size_t fnvHash(const unsigned char *key, int len) + { + size_t h = 2166136261; + for (int i = 0; i < len; i++) + h = (h * 16777619) ^ key[i]; + return h; + } + class HashException : public std::runtime_error { public: @@ -21,6 +29,7 @@ namespace odhtdb Hash(); // Throws HashException on failure Hash(const void *input, const size_t inputSize); + Hash(const Hash &other); void* getData() const { return (void*)data; } size_t getSize() const { return HASH_BYTE_SIZE; } diff --git a/include/Permission.hpp b/include/Permission.hpp deleted file mode 100644 index a6a16c3..0000000 --- a/include/Permission.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "types.hpp" - -namespace odhtdb -{ - enum class Permission : u8 - { - NONE, - READ, - WRITE, - READ_WRITE - }; -} \ No newline at end of file diff --git a/include/Signature.hpp b/include/Signature.hpp index aace383..62c41c3 100644 --- a/include/Signature.hpp +++ b/include/Signature.hpp @@ -2,6 +2,7 @@ #include "DataView.hpp" #include +#include namespace odhtdb { @@ -67,6 +68,9 @@ namespace odhtdb // Both exceptions are derived from UnsignException std::string unsign(const DataView &signedMessage) const; + size_t operator()() const; + bool operator==(const PublicKey &other) const; + std::string toString() const; private: PublicKey(){} @@ -74,6 +78,17 @@ namespace odhtdb char data[PUBLIC_KEY_NUM_BYTES]; }; + struct PublicKeyHasher + { + size_t operator()(const PublicKey &publicKey) const + { + return publicKey(); + } + }; + + template + using MapPublicKey = std::unordered_map; + class PrivateKey { friend class KeyPair; diff --git a/include/StagedObject.hpp b/include/StagedObject.hpp index a75664e..0c9b534 100644 --- a/include/StagedObject.hpp +++ b/include/StagedObject.hpp @@ -6,14 +6,14 @@ namespace odhtdb { - struct StagedCreateObject + struct StagedObject { - DISABLE_COPY(StagedCreateObject) - DataView encryptedBody; + DISABLE_COPY(StagedObject) + DataView data; std::shared_ptr requestKey; - StagedCreateObject(DataView &_encryptedBody, const std::shared_ptr &_requestKey) : - encryptedBody(_encryptedBody), + StagedObject(DataView &_data, const std::shared_ptr &_requestKey) : + data(_data), requestKey(_requestKey) { -- cgit v1.2.3