From d71fe129b83f34b46136070fcf9a64865203be14 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Wed, 25 Apr 2018 05:29:43 +0200 Subject: Store opendht identity in storage file (cached) --- include/odhtdb/Database.hpp | 3 --- include/odhtdb/DatabaseStorage.hpp | 4 ++++ include/odhtdb/env.hpp | 4 ---- src/Database.cpp | 4 +--- src/DatabaseStorage.cpp | 35 ++++++++++++++++++++++++++++++++++- tests/main.cpp | 22 ++++++++++++++++++++++ 6 files changed, 61 insertions(+), 11 deletions(-) diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index a8833fc..846ddaa 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -12,9 +12,6 @@ #include "DatabaseNode.hpp" #include "Encryption.hpp" #include "OwnedMemory.hpp" -#ifdef DEBUG -#undef DEBUG -#endif #include #include #include diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index a2789f7..a9e04ef 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace odhtdb { @@ -106,6 +107,8 @@ namespace odhtdb // Return users in node, or nullptr if no node with id @nodeHash exists const Signature::MapPublicKey* getUsersData(const Hash &nodeHash) const; + const dht::crypto::Identity& getIdentity() const; + // Update storage state (remove quarantine objects if they are too old, etc) void update(); private: @@ -126,5 +129,6 @@ namespace odhtdb boost::filesystem::path dataFilePath; boost::filesystem::path metadataFilePath; u8 passwordSalt[PASSWORD_SALT_LEN]; + std::pair, std::shared_ptr> identity; }; } diff --git a/include/odhtdb/env.hpp b/include/odhtdb/env.hpp index bafc750..abaedd8 100644 --- a/include/odhtdb/env.hpp +++ b/include/odhtdb/env.hpp @@ -57,7 +57,3 @@ #if !defined(OS_TYPE) #error "System not supported. Only Windows and linux systems supported right now" #endif - -#if !defined(DEBUG) && !defined(NDEBUG) -#define DEBUG -#endif diff --git a/src/Database.cpp b/src/Database.cpp index 226aa05..383cc18 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -89,8 +89,6 @@ namespace odhtdb onAddUserCallbackFunc(nullptr), databaseStorage(storageDir) { - // TODO: Cache this in storage. It takes pretty long time to generate new identity - auto identity = dht::crypto::generateIdentity(); node.run(port , { /*.dht_config = */{ /*.node_config = */{ @@ -99,7 +97,7 @@ namespace odhtdb /*.is_bootstrap = */false, /*.maintain_storage*/false }, - /*.id = */identity + /*.id = */databaseStorage.getIdentity() }, /*.threaded = */true, /*.proxy_server = */"", diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index bd98b8b..c507f02 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -24,6 +24,7 @@ namespace odhtdb }; const u64 QUARANTINE_STORAGE_TIME_MICROSECONDS = 60 * 1.0e6; + const u16 STORAGE_VERSION = 1; DatabaseStorageObject::DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : data(_data), createdTimestamp(_timestamp), creatorPublicKey(_creatorPublicKey) @@ -62,9 +63,21 @@ namespace odhtdb if(!metadataLoaded) { sibs::SafeSerializer metadataSerializer; - metadataSerializer.add((u16)0); // Storage version + metadataSerializer.add(STORAGE_VERSION); randombytes_buf(passwordSalt, PASSWORD_SALT_LEN); metadataSerializer.add(passwordSalt, PASSWORD_SALT_LEN); + + //string passwordSaltStr((const char*)passwordSalt, PASSWORD_SALT_LEN); + identity = dht::crypto::generateIdentity(); + dht::Blob privateKeyData = identity.first->serialize(); + metadataSerializer.add((u16)privateKeyData.size()); + metadataSerializer.add(privateKeyData.data(), privateKeyData.size()); + + dht::Blob certificateData; + identity.second->pack(certificateData); + metadataSerializer.add((u16)certificateData.size()); + metadataSerializer.add(certificateData.data(), certificateData.size()); + fileAppend(metadataFilePath, { metadataSerializer.getBuffer().data(), metadataSerializer.getBuffer().size() }); } } @@ -288,9 +301,24 @@ namespace odhtdb sibs::SafeDeserializer deserializer((u8*)metadataFileContent.data, metadataFileContent.size); u16 storageVersion = deserializer.extract(); + if(storageVersion != STORAGE_VERSION) + throw std::runtime_error("Wrong storage version!"); u8 passwordSalt[PASSWORD_SALT_LEN]; deserializer.extract(passwordSalt, PASSWORD_SALT_LEN); + //string passwordSaltStr((const char*)passwordSalt, PASSWORD_SALT_LEN); + + u16 privateKeySize = deserializer.extract(); + dht::Blob privateKeyRaw; + privateKeyRaw.resize(privateKeySize); + deserializer.extract(&privateKeyRaw[0], privateKeySize); + identity.first = make_shared(privateKeyRaw); + + u16 certificateSize = deserializer.extract(); + dht::Blob certificateRaw; + certificateRaw.resize(certificateSize); + deserializer.extract(&certificateRaw[0], certificateSize); + identity.second = make_shared(certificateRaw); assert(deserializer.empty()); } @@ -524,6 +552,11 @@ namespace odhtdb return nullptr; } + const dht::crypto::Identity& DatabaseStorage::getIdentity() const + { + return identity; + } + void DatabaseStorage::update() { // TODO: Modify this to iterate backwards. Because list is sorted in order of timestamp, we can remove data in range diff --git a/tests/main.cpp b/tests/main.cpp index d509972..3f23b32 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include using namespace std; using namespace chrono_literals; @@ -108,9 +109,30 @@ void testEncryption() assertEquals(0, strncmp(message, (const char*)decryption.getDecryptedText().data, messageLength)); } +void testCachedIdentity() +{ + pair, shared_ptr> identity = dht::crypto::generateIdentity(); + dht::Blob privateKeyData = identity.first->serialize(); + printf("Private key size: %d, serialized data: %s\n", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str()); + + dht::crypto::PrivateKey privateKeyDeserialized(privateKeyData); + privateKeyData = identity.first->serialize(); + printf("Private key size: %d, serialized data: %s\n", privateKeyData.size(), Hash(privateKeyData.data(), privateKeyData.size()).toString().c_str()); + + dht::Blob certificateData; + identity.second->pack(certificateData); + printf("Certificate data size: %d, serialized data: %s\n", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str()); + + dht::crypto::Certificate certificateDeserialized(certificateData); + certificateData.clear(); + identity.second->pack(certificateData); + printf("Certificate data size: %d, serialized data: %s\n", certificateData.size(), Hash(certificateData.data(), certificateData.size()).toString().c_str()); +} + int main() { Log::debug("Starting tests..."); + testCachedIdentity(); testBinHexConvert(); testHash(); testEncryption(); -- cgit v1.2.3