diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/Database.hpp | 39 | ||||
-rw-r--r-- | include/DatabaseStorage.hpp | 42 | ||||
-rw-r--r-- | include/Encryption.hpp | 7 | ||||
-rw-r--r-- | include/Hash.hpp | 11 | ||||
-rw-r--r-- | include/Permission.hpp | 14 | ||||
-rw-r--r-- | include/Signature.hpp | 15 | ||||
-rw-r--r-- | include/StagedObject.hpp | 10 |
7 files changed, 87 insertions, 51 deletions
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<char*> &key, const std::shared_ptr<Hash> &hash); + DatabaseCreateResponse(LocalUser *nodeAdminUser, const std::shared_ptr<char*> &key, const std::shared_ptr<Hash> &hash); + const LocalUser* getNodeAdminUser() const; const std::shared_ptr<char*> getNodeEncryptionKey() const; const std::shared_ptr<Hash> getRequestHash() const; private: + LocalUser *nodeAdminUser; std::shared_ptr<char*> key; std::shared_ptr<Hash> hash; }; @@ -115,15 +120,15 @@ namespace odhtdb void seed(const std::shared_ptr<Hash> hash, const std::shared_ptr<char*> encryptionKey); // Throws DatabaseCreateException on failure. - std::unique_ptr<DatabaseCreateResponse> create(const LocalUser *owner, const std::string &name); + std::unique_ptr<DatabaseCreateResponse> 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<DatabaseCreateResponse> &nodeInfo, DataView dataToAdd); void commit(); private: // Throws CommitCreateException on failure - void commitStagedCreateObject(const std::unique_ptr<StagedCreateObject> &stagedObject); + void commitStagedCreateObject(const std::unique_ptr<StagedObject> &stagedObject); // Throws CommitAddException on failure - void commitStagedAddObject(const DataView &stagedObject); + void commitStagedAddObject(const std::unique_ptr<StagedObject> &stagedObject); ntp::NtpTimestamp getSyncedTimestampUtc() const; DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey); DatabaseAddRequest deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey); @@ -131,8 +136,8 @@ namespace odhtdb bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey); private: dht::DhtRunner node; - std::vector<std::unique_ptr<StagedCreateObject>> stagedCreateObjects; - std::vector<std::unique_ptr<DataView>> stagedAddObjects; + std::vector<std::unique_ptr<StagedObject>> stagedCreateObjects; + std::vector<std::unique_ptr<StagedObject>> 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<Group*> groups; - std::vector<DatabaseStorageObject> objects; - u8 *createData; - usize createDataSize; + std::vector<DatabaseStorageObject*> 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<DatabaseStorageObjectList*>; + using DatabaseStorageQuarantineMap = Signature::MapPublicKey<std::vector<DatabaseStorageQuarantineObject*>>; 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<Hash> userPublicKeyNodeMap; + Signature::MapPublicKey<const User*> 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 <sodium/crypto_generichash_blake2b.h> #include <stdexcept> #include <unordered_map> @@ -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 <stdexcept> +#include <unordered_map> 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 <typename ValueType> + using MapPublicKey = std::unordered_map<PublicKey, ValueType, PublicKeyHasher>; + 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<Hash> requestKey; - StagedCreateObject(DataView &_encryptedBody, const std::shared_ptr<Hash> &_requestKey) : - encryptedBody(_encryptedBody), + StagedObject(DataView &_data, const std::shared_ptr<Hash> &_requestKey) : + data(_data), requestKey(_requestKey) { |