diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Database.cpp | 11 | ||||
-rw-r--r-- | src/DatabaseStorage.cpp | 30 |
2 files changed, 24 insertions, 17 deletions
diff --git a/src/Database.cpp b/src/Database.cpp index 0fdc438..065f441 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -101,7 +101,7 @@ namespace odhtdb /*.node_config = */{ /*.node_id = */{}, /*.network = */0, - /*.is_bootstrap = */true, + /*.is_bootstrap = */false, /*.maintain_storage*/false }, /*.id = */databaseStorage.getIdentity() @@ -554,10 +554,7 @@ namespace odhtdb uint8_t adminGroupId[GROUP_ID_LENGTH]; deserializer.extract(adminGroupId, GROUP_ID_LENGTH); - - if(deserializer.getSize() < ENCRYPTION_NONCE_BYTE_SIZE) - throw sibs::DeserializeException("Unsigned encrypted body is too small (unable to extract nonce)"); - + databaseStorage.createStorage(hash, userPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH), creationDate, value->data.data(), value->data.size()); } @@ -605,7 +602,7 @@ namespace odhtdb bool Database::listenCreateData(shared_ptr<dht::Value> value, const Hash &hash, const shared_ptr<OwnedByteArray> encryptionKey) { - Log::debug("Got create data"); + Log::debug("Got create data in node %s", hash.toString().c_str()); try { // This check is here to reduce processing, it doesn't matter much if the packet bypasses this, @@ -623,7 +620,7 @@ namespace odhtdb bool Database::listenAddData(shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const shared_ptr<OwnedByteArray> encryptionKey) { - Log::debug("Got add data"); + Log::debug("Got add data in node %s", nodeHash->toString().c_str()); try { // This check is here to reduce processing, it doesn't matter much if the packet bypasses this, diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index ccf5d26..b4c9a9e 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -23,7 +23,7 @@ namespace odhtdb }; const u64 QUARANTINE_STORAGE_TIME_MICROSECONDS = 60 * 1.0e6; - const u16 STORAGE_VERSION = 3; + const u16 STORAGE_VERSION = 4; static void sqlite_exec_checked(sqlite3 *db, const char *sql) { @@ -111,7 +111,7 @@ namespace odhtdb "CREATE TABLE IF NOT EXISTS NodeDecryptionKey(node BLOB UNIQUE NOT NULL, decryptionKey BLOB NOT NULL);" "CREATE TABLE IF NOT EXISTS NodeUserGroupAssoc(node BLOB NOT NULL, userPublicKey BLOB NOT NULL, groupId BLOB NOT NULL, FOREIGN KEY(node, userPublicKey) REFERENCES NodeUser(node, publicKey), FOREIGN KEY(groupId) REFERENCES NodeGroup(groupId));" - "CREATE TABLE IF NOT EXISTS NodeRaw(node INTEGER NOT NULL, data BLOB NOT NULL, FOREIGN KEY(node) REFERENCES Node(id));" + "CREATE TABLE IF NOT EXISTS NodeRaw(node BLOB NOT NULL, data BLOB NOT NULL, FOREIGN KEY(node) REFERENCES Node(nodeHash));" "CREATE TABLE IF NOT EXISTS NodeAddDataRaw(nodeId INTEGER NOT NULL, nodeAddDataId INTEGER NOT NULL, data BLOB NOT NULL, FOREIGN KEY(nodeId) REFERENCES Node(id), FOREIGN KEY(nodeAddDataId) REFERENCES NodeAddData(id));" "CREATE TABLE IF NOT EXISTS NodeUserActionGap(id INTEGER PRIMARY KEY, nodeUserId INTEGER NOT NULL, start INTEGER NOT NULL, range INTEGER NOT NULL, FOREIGN KEY(nodeUserId) REFERENCES NodeUser(id));" @@ -471,7 +471,7 @@ namespace odhtdb sqlite3_clear_bindings(insertNodeRawStmt); int rc; - rc = sqlite3_bind_int64(insertNodeRawStmt, 1, getNodeRowId(hash)); + rc = sqlite3_bind_blob(insertNodeRawStmt, 1, hash.getData(), hash.getSize(), SQLITE_STATIC); bindCheckError(rc); rc = sqlite3_bind_blob(insertNodeRawStmt, 2, data, size, SQLITE_STATIC); @@ -1230,13 +1230,23 @@ namespace odhtdb u8 nonce[ENCRYPTION_NONCE_BYTE_SIZE]; deserializer.extract(nonce, ENCRYPTION_NONCE_BYTE_SIZE); DataView dataToDecrypt((void*)deserializer.getBuffer(), deserializer.getSize()); - Decryption decryptedBody(dataToDecrypt, DataView(nonce, ENCRYPTION_NONCE_BYTE_SIZE), DataView(decryptionKey->data, ENCRYPTION_KEY_BYTE_SIZE)); - setNodeAddDataDecryptedData(rowId, decryptedBody.getDecryptedText()); - - Log::debug("Got add object, timestamp: %zu, data: %.*s", timestamp, decryptedBody.getDecryptedText().size, decryptedBody.getDecryptedText().data); - const DatabaseAddNodeRequest addNodeRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, decryptedBody.getDecryptedText()); - if(database->onAddNodeCallbackFunc) - database->onAddNodeCallbackFunc(addNodeRequest); + try + { + Decryption decryptedBody(dataToDecrypt, DataView(nonce, ENCRYPTION_NONCE_BYTE_SIZE), DataView(decryptionKey->data, ENCRYPTION_KEY_BYTE_SIZE)); + setNodeAddDataDecryptedData(rowId, decryptedBody.getDecryptedText()); + + Log::debug("Got add object, timestamp: %zu, data: %.*s", timestamp, decryptedBody.getDecryptedText().size, decryptedBody.getDecryptedText().data); + const DatabaseAddNodeRequest addNodeRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, decryptedBody.getDecryptedText()); + if(database->onAddNodeCallbackFunc) + database->onAddNodeCallbackFunc(addNodeRequest); + } + catch(DecryptionException &e) + { + Log::error("Failed to decrypt data. Nonce: (data: %s, size: %u), dataToDecrypt: (data: %s, size: %u)", + bin2hex((const char*)nonce, ENCRYPTION_NONCE_BYTE_SIZE).c_str(), ENCRYPTION_NONCE_BYTE_SIZE, + bin2hex((const char*)dataToDecrypt.data, dataToDecrypt.size).c_str(), dataToDecrypt.size); + throw e; + } return true; } |