aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/odhtdb/Database.hpp20
-rw-r--r--include/odhtdb/DatabaseNode.hpp6
-rw-r--r--include/odhtdb/DatabaseStorage.hpp16
-rw-r--r--include/odhtdb/FileUtils.hpp2
-rw-r--r--include/odhtdb/OwnedMemory.hpp28
-rw-r--r--include/odhtdb/PasswordHash.hpp2
-rw-r--r--src/Database.cpp50
-rw-r--r--src/DatabaseStorage.cpp36
-rw-r--r--src/FileUtils.cpp4
-rw-r--r--src/PasswordHash.cpp4
10 files changed, 99 insertions, 69 deletions
diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp
index 04c3b62..a2914ac 100644
--- a/include/odhtdb/Database.hpp
+++ b/include/odhtdb/Database.hpp
@@ -113,16 +113,16 @@ namespace odhtdb
class DatabaseCreateResponse
{
public:
- DatabaseCreateResponse(std::shared_ptr<Signature::KeyPair> nodeAdminKeyPair, std::shared_ptr<OwnedMemory> nodeAdminGroupId, std::shared_ptr<OwnedMemory> key, std::shared_ptr<Hash> hash);
+ DatabaseCreateResponse(std::shared_ptr<Signature::KeyPair> nodeAdminKeyPair, std::shared_ptr<OwnedByteArray> nodeAdminGroupId, std::shared_ptr<OwnedByteArray> key, std::shared_ptr<Hash> hash);
const std::shared_ptr<Signature::KeyPair> getNodeAdminKeyPair() const;
- const std::shared_ptr<OwnedMemory> getNodeAdminGroupId() const;
- const std::shared_ptr<OwnedMemory> getNodeEncryptionKey() const;
+ const std::shared_ptr<OwnedByteArray> getNodeAdminGroupId() const;
+ const std::shared_ptr<OwnedByteArray> getNodeEncryptionKey() const;
const std::shared_ptr<Hash> getRequestHash() const;
private:
std::shared_ptr<Signature::KeyPair> nodeAdminKeyPair;
- std::shared_ptr<OwnedMemory> nodeAdminGroupId;
- std::shared_ptr<OwnedMemory> key;
+ std::shared_ptr<OwnedByteArray> nodeAdminGroupId;
+ std::shared_ptr<OwnedByteArray> key;
std::shared_ptr<Hash> hash;
};
@@ -194,7 +194,7 @@ namespace odhtdb
// Throws DatabaseStorageWrongPassword if password for the stored user is wrong.
MapHash<StoredNodeInfo> getStoredNodeUserInfoDecrypted(const std::string &username, const std::string &password) const;
- std::vector<OwnedMemory> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
+ std::vector<OwnedByteArray> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
void receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc);
@@ -203,10 +203,10 @@ namespace odhtdb
static dht::InfoHash getInfoHash(const void *data, usize size);
private:
- void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey);
- void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey);
- bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey);
- bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey);
+ void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedByteArray> encryptionKey);
+ void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedByteArray> encryptionKey);
+ bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<OwnedByteArray> encryptionKey);
+ bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const std::shared_ptr<OwnedByteArray> encryptionKey);
private:
dht::DhtRunner node;
DatabaseStorage databaseStorage;
diff --git a/include/odhtdb/DatabaseNode.hpp b/include/odhtdb/DatabaseNode.hpp
index 620cd40..c707af9 100644
--- a/include/odhtdb/DatabaseNode.hpp
+++ b/include/odhtdb/DatabaseNode.hpp
@@ -11,14 +11,14 @@ namespace odhtdb
public:
DatabaseNode() {}
- DatabaseNode(const std::shared_ptr<OwnedMemory> &_encryptionKey, const std::shared_ptr<Hash> &_nodeHash) :
+ DatabaseNode(const std::shared_ptr<OwnedByteArray> &_encryptionKey, const std::shared_ptr<Hash> &_nodeHash) :
encryptionKey(_encryptionKey),
nodeHash(_nodeHash)
{
}
- const std::shared_ptr<OwnedMemory> getNodeEncryptionKey() const
+ const std::shared_ptr<OwnedByteArray> getNodeEncryptionKey() const
{
return encryptionKey;
}
@@ -28,7 +28,7 @@ namespace odhtdb
return nodeHash;
}
private:
- std::shared_ptr<OwnedMemory> encryptionKey;
+ std::shared_ptr<OwnedByteArray> encryptionKey;
std::shared_ptr<Hash> nodeHash;
};
}
diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp
index a618420..9f93994 100644
--- a/include/odhtdb/DatabaseStorage.hpp
+++ b/include/odhtdb/DatabaseStorage.hpp
@@ -75,7 +75,7 @@ namespace odhtdb
struct StoredNodeInfo
{
- std::shared_ptr<OwnedMemory> nodeEncryptionKey;
+ std::shared_ptr<OwnedByteArray> nodeEncryptionKey;
std::shared_ptr<Signature::KeyPair> userKeyPair;
};
@@ -135,14 +135,14 @@ namespace odhtdb
MapHash<StoredNodeInfo> getStoredNodeUserInfoDecrypted(const std::string &username, const std::string &password) const;
// Returns true and node decryption key if node exists and we have the decryption key,
- // otherwise return false and OwnedMemory with data set to nullptr
- std::pair<bool, std::shared_ptr<OwnedMemory>> getNodeDecryptionKey(const Hash &nodeHash);
+ // otherwise return false and OwnedByteArray with data set to nullptr
+ std::pair<bool, std::shared_ptr<OwnedByteArray>> getNodeDecryptionKey(const Hash &nodeHash);
void setNodeDecryptionKey(const Hash &nodeHash, const DataView &decryptionKey);
const std::vector<dht::NodeExport>& getRemoteNodes() const;
void setRemoteNodes(const std::vector<dht::NodeExport> &remoteNodes);
- std::vector<OwnedMemory> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
+ std::vector<OwnedByteArray> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
const dht::crypto::Identity& getIdentity() const;
@@ -155,10 +155,10 @@ namespace odhtdb
void bindCheckError(int sqliteBindResult);
void loadMetadataFromFile();
void loadRemoteNodesFromFile();
- bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr<OwnedMemory> decryptionKey);
- bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr<OwnedMemory> decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp);
- bool decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const std::shared_ptr<OwnedMemory> decryptionKey);
- bool decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const std::shared_ptr<OwnedMemory> decryptionKey);
+ bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr<OwnedByteArray> decryptionKey);
+ bool decryptNodeData(const Hash &nodeHash, const std::shared_ptr<OwnedByteArray> decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp);
+ bool decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const std::shared_ptr<OwnedByteArray> decryptionKey);
+ bool decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const std::shared_ptr<OwnedByteArray> decryptionKey);
i64 getNodeRowId(const Hash &nodeHash);
i64 getNodeAddDataRowId(const Hash &requestHash);
diff --git a/include/odhtdb/FileUtils.hpp b/include/odhtdb/FileUtils.hpp
index 88721ed..155f53a 100644
--- a/include/odhtdb/FileUtils.hpp
+++ b/include/odhtdb/FileUtils.hpp
@@ -14,7 +14,7 @@ namespace odhtdb
};
// Throws FileException on error
- OwnedMemory fileGetContent(const boost::filesystem::path &filepath);
+ OwnedByteArray fileGetContent(const boost::filesystem::path &filepath);
// Creates file if it doesn't exist.
// Throws FileException on error
diff --git a/include/odhtdb/OwnedMemory.hpp b/include/odhtdb/OwnedMemory.hpp
index 67afe06..4c6df1c 100644
--- a/include/odhtdb/OwnedMemory.hpp
+++ b/include/odhtdb/OwnedMemory.hpp
@@ -22,4 +22,32 @@ namespace odhtdb
void *data;
usize size;
};
+
+ class OwnedByteArray
+ {
+ public:
+ OwnedByteArray() : data(nullptr), size(0) {}
+ OwnedByteArray(u8 *_data, usize _size) : data(_data), size(_size) {}
+ OwnedByteArray(OwnedByteArray &&other)
+ {
+ data = other.data;
+ size = other.size;
+
+ other.data = nullptr;
+ other.size = 0;
+ }
+ ~OwnedByteArray()
+ {
+ delete[] data;
+ }
+
+ // Do not allow copy of this struct, forcing move when returning a OwnedByteArray in a function
+ OwnedByteArray(OwnedByteArray&) = delete;
+ OwnedByteArray& operator = (OwnedByteArray&) = delete;
+
+ const DataView getView() const { return DataView(data, size); }
+
+ u8 *data;
+ usize size;
+ };
}
diff --git a/include/odhtdb/PasswordHash.hpp b/include/odhtdb/PasswordHash.hpp
index bc02c53..6d23358 100644
--- a/include/odhtdb/PasswordHash.hpp
+++ b/include/odhtdb/PasswordHash.hpp
@@ -7,5 +7,5 @@ namespace odhtdb
{
const int HASH_PASSWORD_LENGTH = 32;
- OwnedMemory hashPassword(const DataView &plainPassword, const DataView &salt);
+ OwnedByteArray hashPassword(const DataView &plainPassword, const DataView &salt);
}
diff --git a/src/Database.cpp b/src/Database.cpp
index df11b3c..b4d0e12 100644
--- a/src/Database.cpp
+++ b/src/Database.cpp
@@ -41,26 +41,26 @@ namespace odhtdb
RequestQuarantineException() : runtime_error("Request quarantine, will be processed later (can be real of fake request)") {}
};
- OwnedMemory combine(sibs::SafeSerializer &headerSerializer, const Encryption &encryptedData)
+ OwnedByteArray combine(sibs::SafeSerializer &headerSerializer, const Encryption &encryptedData)
{
usize allocationSize = headerSerializer.getBuffer().size() + encryptedData.getNonce().size + encryptedData.getCipherText().size;
- char *result = new char[allocationSize];
+ u8 *result = new u8[allocationSize];
memcpy(result, headerSerializer.getBuffer().data(), headerSerializer.getBuffer().size());
memcpy(result + headerSerializer.getBuffer().size(), encryptedData.getNonce().data, encryptedData.getNonce().size);
memcpy(result + headerSerializer.getBuffer().size() + encryptedData.getNonce().size, encryptedData.getCipherText().data, encryptedData.getCipherText().size);
- return OwnedMemory(result, allocationSize);
+ return OwnedByteArray(result, allocationSize);
}
- OwnedMemory combine(const Signature::PublicKey &publicKey, const string &signedEncryptedData)
+ OwnedByteArray combine(const Signature::PublicKey &publicKey, const string &signedEncryptedData)
{
usize allocationSize = publicKey.getSize() + signedEncryptedData.size();
- char *result = new char[allocationSize];
+ u8 *result = new u8[allocationSize];
memcpy(result, publicKey.getData(), publicKey.getSize());
memcpy(result + publicKey.getSize(), signedEncryptedData.data(), signedEncryptedData.size());
- return OwnedMemory(result, allocationSize);
+ return OwnedByteArray(result, allocationSize);
}
- DatabaseCreateResponse::DatabaseCreateResponse(std::shared_ptr<Signature::KeyPair> _nodeAdminKeyPair, std::shared_ptr<OwnedMemory> _nodeAdminGroupId, shared_ptr<OwnedMemory> _key, shared_ptr<Hash> _hash) :
+ DatabaseCreateResponse::DatabaseCreateResponse(std::shared_ptr<Signature::KeyPair> _nodeAdminKeyPair, std::shared_ptr<OwnedByteArray> _nodeAdminGroupId, shared_ptr<OwnedByteArray> _key, shared_ptr<Hash> _hash) :
nodeAdminKeyPair(_nodeAdminKeyPair),
nodeAdminGroupId(_nodeAdminGroupId),
key(_key),
@@ -74,12 +74,12 @@ namespace odhtdb
return nodeAdminKeyPair;
}
- const shared_ptr<OwnedMemory> DatabaseCreateResponse::getNodeAdminGroupId() const
+ const shared_ptr<OwnedByteArray> DatabaseCreateResponse::getNodeAdminGroupId() const
{
return nodeAdminGroupId;
}
- const shared_ptr<OwnedMemory> DatabaseCreateResponse::getNodeEncryptionKey() const
+ const shared_ptr<OwnedByteArray> DatabaseCreateResponse::getNodeEncryptionKey() const
{
return key;
}
@@ -336,25 +336,27 @@ namespace odhtdb
bool iHaveCreateNode = databaseStorage.doesNodeExist(*nodeToSeed.getRequestHash());
serializer.add(iHaveCreateNode ? (u8)0 : (u8)1);
serializer.add(fetchOrder);
- DataViewMap<u64> userLatestActionCounter;
+ Signature::MapPublicKey<u64> userLatestActionCounter;
- databaseStorage.fetchNodeUserActionGaps(*nodeToSeed.getRequestHash(), [&serializer, &userLatestActionCounter](const DataView userPublicKey, u64 actionGapStart, u64 actionGapRange)
+ databaseStorage.fetchNodeUserActionGaps(*nodeToSeed.getRequestHash(), [&serializer, &userLatestActionCounter](const DataView userPublicKeyRaw, u64 actionGapStart, u64 actionGapRange)
{
- serializer.add((const u8*)userPublicKey.data, PUBLIC_KEY_NUM_BYTES);
+ serializer.add((const u8*)userPublicKeyRaw.data, PUBLIC_KEY_NUM_BYTES);
serializer.add(actionGapStart);
serializer.add(actionGapRange);
+ Signature::PublicKey userPublicKey((const char*)userPublicKeyRaw.data, userPublicKeyRaw.size);
userLatestActionCounter[userPublicKey] = std::max(userLatestActionCounter[userPublicKey], actionGapStart + actionGapRange);
});
- databaseStorage.fetchNodeUserLatestActionCounter(*nodeToSeed.getRequestHash(), [&userLatestActionCounter](const DataView userPublicKey, u64 latestActionCounter)
+ databaseStorage.fetchNodeUserLatestActionCounter(*nodeToSeed.getRequestHash(), [&userLatestActionCounter](const DataView userPublicKeyRaw, u64 latestActionCounter)
{
+ Signature::PublicKey userPublicKey((const char*)userPublicKeyRaw.data, userPublicKeyRaw.size);
userLatestActionCounter[userPublicKey] = std::max(userLatestActionCounter[userPublicKey], latestActionCounter);
});
for(auto userLatestActionCounterData : userLatestActionCounter)
{
// Public key
- serializer.add((const u8*)userLatestActionCounterData.first.data, PUBLIC_KEY_NUM_BYTES);
+ serializer.add((const u8*)userLatestActionCounterData.first.getData(), PUBLIC_KEY_NUM_BYTES);
// Latest action counter start
serializer.add(userLatestActionCounterData.second);
// Latest action counter range (infinite range, meaning we want all packets older than start (latest known packet by user))
@@ -407,7 +409,7 @@ namespace odhtdb
{
unsigned char *encryptionKeyRaw = new unsigned char[ENCRYPTION_KEY_BYTE_SIZE];
Encryption::generateKey(encryptionKeyRaw);
- shared_ptr<OwnedMemory> encryptionKey = make_shared<OwnedMemory>(encryptionKeyRaw, ENCRYPTION_KEY_BYTE_SIZE);
+ shared_ptr<OwnedByteArray> encryptionKey = make_shared<OwnedByteArray>(encryptionKeyRaw, ENCRYPTION_KEY_BYTE_SIZE);
shared_ptr<Hash> hashRequestKey = make_shared<Hash>(serializer.getBuffer().data(), serializer.getBuffer().size());
databaseStorage.setNodeDecryptionKey(*hashRequestKey, DataView(encryptionKey->data, encryptionKey->size));
@@ -422,7 +424,7 @@ namespace odhtdb
Log::warn("Failed to put: %s, what to do?", "Database::create");
});
- shared_ptr<OwnedMemory> adminGroupIdResponse = make_shared<OwnedMemory>(new u8[GROUP_ID_LENGTH], GROUP_ID_LENGTH);
+ shared_ptr<OwnedByteArray> adminGroupIdResponse = make_shared<OwnedByteArray>(new u8[GROUP_ID_LENGTH], GROUP_ID_LENGTH);
memcpy(adminGroupIdResponse->data, adminGroupId.data, GROUP_ID_LENGTH);
return make_unique<DatabaseCreateResponse>(creatorKeyPair, adminGroupIdResponse, encryptionKey, hashRequestKey);
}
@@ -444,9 +446,9 @@ namespace odhtdb
DataView encryptionKey(nodeInfo.getNodeEncryptionKey()->data, ENCRYPTION_KEY_BYTE_SIZE);
Encryption encryptedBody(dataToAdd, encryptionKey);
- OwnedMemory requestData = combine(serializer, encryptedBody);
+ OwnedByteArray requestData = combine(serializer, encryptedBody);
string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData.getView());
- OwnedMemory stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
+ OwnedByteArray stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
Hash requestDataHash(stagedAddObject.data, stagedAddObject.size);
DataView encryptedDataView((char*)requestData.data + serializer.getBuffer().size(), requestData.size - serializer.getBuffer().size());
databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, newActionCounter, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView);
@@ -480,7 +482,7 @@ namespace odhtdb
DataView requestData { serializer.getBuffer().data(), serializer.getBuffer().size() };
string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData);
- OwnedMemory stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
+ OwnedByteArray stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
Hash requestDataHash(stagedAddObject.data, stagedAddObject.size);
DataView additionalDataView((void*)(static_cast<const char*>(requestData.data) + additionalDataOffset), requestData.size - additionalDataOffset);
databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, newActionCounter, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, additionalDataView);
@@ -511,7 +513,7 @@ namespace odhtdb
return timestamp;
}
- void Database::deserializeCreateRequest(const shared_ptr<dht::Value> &value, const Hash &hash, const shared_ptr<OwnedMemory> encryptionKey)
+ void Database::deserializeCreateRequest(const shared_ptr<dht::Value> &value, const Hash &hash, const shared_ptr<OwnedByteArray> encryptionKey)
{
sibs::SafeDeserializer deserializer(value->data.data(), value->data.size());
u16 packetStructureVersion = deserializer.extract<u16>();
@@ -551,7 +553,7 @@ namespace odhtdb
databaseStorage.createStorage(hash, userPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH), creationDate, value->data.data(), value->data.size());
}
- void Database::deserializeAddRequest(const shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const shared_ptr<OwnedMemory> encryptionKey)
+ void Database::deserializeAddRequest(const shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const shared_ptr<OwnedByteArray> encryptionKey)
{
sibs::SafeDeserializer deserializer(value->data.data(), value->data.size());
char creatorPublicKeyRaw[PUBLIC_KEY_NUM_BYTES];
@@ -593,7 +595,7 @@ namespace odhtdb
databaseStorage.appendStorage(*nodeHash, requestDataHash, operation, newActionCounter, creatorPublicKey, creationDate, value->data.data(), value->data.size(), additionalDataView);
}
- bool Database::listenCreateData(shared_ptr<dht::Value> value, const Hash &hash, const shared_ptr<OwnedMemory> encryptionKey)
+ bool Database::listenCreateData(shared_ptr<dht::Value> value, const Hash &hash, const shared_ptr<OwnedByteArray> encryptionKey)
{
Log::debug("Got create data");
try
@@ -611,7 +613,7 @@ namespace odhtdb
return true;
}
- bool Database::listenAddData(shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const shared_ptr<OwnedMemory> encryptionKey)
+ 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");
try
@@ -653,7 +655,7 @@ namespace odhtdb
return databaseStorage.getStoredNodeUserInfoDecrypted(username, password);
}
- vector<OwnedMemory> Database::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
+ vector<OwnedByteArray> Database::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
{
return databaseStorage.getUserGroups(nodeHash, userPublicKey);
}
diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp
index 016c498..ccf5d26 100644
--- a/src/DatabaseStorage.cpp
+++ b/src/DatabaseStorage.cpp
@@ -285,7 +285,7 @@ namespace odhtdb
// TODO: There is no need to allocate/deallocate several times, this can be moved outside the while loop
const void *decryptedDataRaw = sqlite3_column_blob(selectNodeAddDataAdditionalStmt, 0);
int decryptedDataSize = sqlite3_column_bytes(selectNodeAddDataAdditionalStmt, 0);
- OwnedMemory decryptedData(new u8[decryptedDataSize], decryptedDataSize);
+ OwnedByteArray decryptedData(new u8[decryptedDataSize], decryptedDataSize);
memcpy(decryptedData.data, decryptedDataRaw, decryptedDataSize);
const DatabaseAddNodeRequest addNodeRequest(&nodeHash, &requestHash, timestamp, &creatorPublicKey, DataView(decryptedData.data, decryptedData.size));
@@ -324,7 +324,7 @@ namespace odhtdb
void DatabaseStorage::loadMetadataFromFile()
{
- OwnedMemory metadataFileContent = fileGetContent(metadataFilePath);
+ OwnedByteArray metadataFileContent = fileGetContent(metadataFilePath);
sibs::SafeDeserializer deserializer((u8*)metadataFileContent.data, metadataFileContent.size);
u16 storageVersion = deserializer.extract<u16>();
@@ -350,7 +350,7 @@ namespace odhtdb
void DatabaseStorage::loadRemoteNodesFromFile()
{
- OwnedMemory remoteNodesFileContent = fileGetContent(remoteNodesFilePath);
+ OwnedByteArray remoteNodesFileContent = fileGetContent(remoteNodesFilePath);
msgpack::unpacker pac;
pac.reserve_buffer(remoteNodesFileContent.size);
memcpy(pac.buffer(), remoteNodesFileContent.data, remoteNodesFileContent.size);
@@ -846,7 +846,7 @@ namespace odhtdb
void DatabaseStorage::storeUserWithoutNodes(const string &username, const string &password)
{
- OwnedMemory hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
+ OwnedByteArray hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
DataView hashedPasswordView(hashedPassword.data, hashedPassword.size);
DataView usernameView((void*)username.data(), username.size());
@@ -884,7 +884,7 @@ namespace odhtdb
void DatabaseStorage::storeNodeInfoForUserEncrypted(const DatabaseNode &nodeInfo, const string &username, const string &password, const Signature::KeyPair &keyPair)
{
- OwnedMemory hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
+ OwnedByteArray hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
DataView hashedPasswordView(hashedPassword.data, hashedPassword.size);
DataView privateKeyView((void*)keyPair.getPrivateKey().getData(), PRIVATE_KEY_NUM_BYTES);
@@ -929,7 +929,7 @@ namespace odhtdb
MapHash<StoredNodeInfo> DatabaseStorage::getStoredNodeUserInfoDecrypted(const string &username, const string &password) const
{
- OwnedMemory hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
+ OwnedByteArray hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN));
DataView hashedPasswordView(hashedPassword.data, hashedPassword.size);
i64 encryptedUserRowId = getStoredUserId(username, hashedPasswordView);
@@ -953,7 +953,7 @@ namespace odhtdb
throw DatabaseStorageException("Encrypted data size is of unexpected size");
Signature::PrivateKey userPrivateKey((const char*)decryptedStoredNodeUserPrivateKey.getDecryptedText().data, PRIVATE_KEY_NUM_BYTES);
shared_ptr<Signature::KeyPair> keyPair = make_shared<Signature::KeyPair>(userPublicKey, userPrivateKey);
- shared_ptr<OwnedMemory> nodeEncryptionKey = make_shared<OwnedMemory>(new u8[ENCRYPTION_KEY_BYTE_SIZE], ENCRYPTION_KEY_BYTE_SIZE);
+ shared_ptr<OwnedByteArray> nodeEncryptionKey = make_shared<OwnedByteArray>(new u8[ENCRYPTION_KEY_BYTE_SIZE], ENCRYPTION_KEY_BYTE_SIZE);
memcpy(nodeEncryptionKey->data, (char*)decryptedStoredNodeUserPrivateKey.getDecryptedText().data + PRIVATE_KEY_NUM_BYTES, ENCRYPTION_KEY_BYTE_SIZE);
result[nodeHash] = { nodeEncryptionKey, keyPair };
}
@@ -965,7 +965,7 @@ namespace odhtdb
return result;
}
- pair<bool, shared_ptr<OwnedMemory>> DatabaseStorage::getNodeDecryptionKey(const Hash &nodeHash)
+ pair<bool, shared_ptr<OwnedByteArray>> DatabaseStorage::getNodeDecryptionKey(const Hash &nodeHash)
{
sqlite3_reset(getNodeDecryptionKeyStmt);
sqlite3_clear_bindings(getNodeDecryptionKeyStmt);
@@ -976,12 +976,12 @@ namespace odhtdb
rc = sqlite3_step(getNodeDecryptionKeyStmt);
if(rc != SQLITE_ROW)
- return make_pair(false, make_shared<OwnedMemory>());
+ return make_pair(false, make_shared<OwnedByteArray>());
const void *decryptionKeyRaw = sqlite3_column_blob(getNodeDecryptionKeyStmt, 0);
u8 *decryptionKeyRawCopy = new u8[ENCRYPTION_KEY_BYTE_SIZE];
memcpy(decryptionKeyRawCopy, decryptionKeyRaw, ENCRYPTION_KEY_BYTE_SIZE);
- shared_ptr<OwnedMemory> decryptionKey = make_shared<OwnedMemory>(decryptionKeyRawCopy, ENCRYPTION_KEY_BYTE_SIZE);
+ shared_ptr<OwnedByteArray> decryptionKey = make_shared<OwnedByteArray>(decryptionKeyRawCopy, ENCRYPTION_KEY_BYTE_SIZE);
return make_pair(true, decryptionKey);
}
@@ -1033,21 +1033,21 @@ namespace odhtdb
fileOverwrite(remoteNodesFilePath, DataView(remoteNodePacker.serializer.getBuffer().data(), remoteNodePacker.serializer.getBuffer().size()));
}
- vector<OwnedMemory> DatabaseStorage::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
+ vector<OwnedByteArray> DatabaseStorage::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
{
- vector<OwnedMemory> result;
+ vector<OwnedByteArray> result;
SqlQuery query(sqliteDb, "SELECT groupId FROM NodeUserGroupAssoc WHERE node = ? AND userPublicKey = ?", { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()) });
while(query.next())
{
const DataView groupIdRaw = query.getBlob(0);
- OwnedMemory groupId(new u8[groupIdRaw.size], groupIdRaw.size);
+ OwnedByteArray groupId(new u8[groupIdRaw.size], groupIdRaw.size);
memcpy(groupId.data, groupIdRaw.data, groupIdRaw.size);
result.emplace_back(move(groupId));
}
return result;
}
- bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedMemory> decryptionKey)
+ bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedByteArray> decryptionKey)
{
sqlite3_reset(selectNodeStmt);
sqlite3_clear_bindings(selectNodeStmt);
@@ -1076,7 +1076,7 @@ namespace odhtdb
return decryptNodeData(nodeHash, decryptionKey, &creatorPublicKey, DataView(adminGroup, GROUP_ID_LENGTH), timestamp);
}
- bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedMemory> decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp)
+ bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedByteArray> decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp)
{
const DatabaseCreateNodeRequest createNodeRequest(&nodeHash, timestamp, creatorPublicKey, adminGroupId);
if(database->onCreateNodeCallbackFunc)
@@ -1129,7 +1129,7 @@ namespace odhtdb
// TODO: There is no need to allocate/deallocate several times, this can be moved outside the while loop
const void *encryptedDataRaw = sqlite3_column_blob(selectNodeAddDataAdditionalStmt, 0);
int encryptedDataSize = sqlite3_column_bytes(selectNodeAddDataAdditionalStmt, 0);
- OwnedMemory encryptedData(new u8[encryptedDataSize], encryptedDataSize);
+ OwnedByteArray encryptedData(new u8[encryptedDataSize], encryptedDataSize);
memcpy(encryptedData.data, encryptedDataRaw, encryptedDataSize);
bool appendObjectResult = decryptNodeAddData(rowId, nodeHash, requestHash, timestamp, &creatorPublicKey, DataView(encryptedData.data, encryptedData.size), decryptionKey);
@@ -1212,7 +1212,7 @@ namespace odhtdb
sqlite_step_throw_on_failure(sqliteDb, setNodeAddDataAdditionalDataStmt, "set NodeAddData decrypted");
}
- bool DatabaseStorage::decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const shared_ptr<OwnedMemory> decryptionKey)
+ bool DatabaseStorage::decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const shared_ptr<OwnedByteArray> decryptionKey)
{
if(!isUserAllowedToAddDataInNode(nodeHash, *creatorPublicKey))
{
@@ -1240,7 +1240,7 @@ namespace odhtdb
return true;
}
- bool DatabaseStorage::decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const shared_ptr<OwnedMemory> decryptionKey)
+ bool DatabaseStorage::decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const shared_ptr<OwnedByteArray> decryptionKey)
{
if(!isUserAllowedToAddUserToGroupInNode(nodeHash, *creatorPublicKey, groupToAddUserTo))
{
diff --git a/src/FileUtils.cpp b/src/FileUtils.cpp
index c4fb318..28bce29 100644
--- a/src/FileUtils.cpp
+++ b/src/FileUtils.cpp
@@ -5,7 +5,7 @@ using namespace std;
namespace odhtdb
{
- OwnedMemory fileGetContent(const boost::filesystem::path &filepath)
+ OwnedByteArray fileGetContent(const boost::filesystem::path &filepath)
{
#if OS_FAMILY == OS_FAMILY_POSIX
FILE *file = fopen(filepath.string().c_str(), "rb");
@@ -27,7 +27,7 @@ namespace odhtdb
size_t fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
- char *result = new char[fileSize];
+ u8 *result = new u8[fileSize];
fread(result, 1, fileSize, file);
fclose(file);
return { result, fileSize };
diff --git a/src/PasswordHash.cpp b/src/PasswordHash.cpp
index 329733b..f877d20 100644
--- a/src/PasswordHash.cpp
+++ b/src/PasswordHash.cpp
@@ -3,9 +3,9 @@
namespace odhtdb
{
- OwnedMemory hashPassword(const DataView &plainPassword, const DataView &salt)
+ OwnedByteArray hashPassword(const DataView &plainPassword, const DataView &salt)
{
- OwnedMemory result;
+ OwnedByteArray result;
const uint32_t tCost = 2;
const uint32_t mCost = 1 << 16;