From 166e132873fccf62327d2190fe1d827d89a5db63 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Fri, 27 Apr 2018 09:21:27 +0200 Subject: Change local storage to fit dchat better --- src/Database.cpp | 7 ++++- src/DatabaseStorage.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- src/Group.cpp | 13 ++++++++++ src/User.cpp | 8 ++++++ 4 files changed, 91 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index 9b01f72..bdf9104 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -233,12 +233,17 @@ namespace odhtdb } unique_ptr Database::create(const string &ownerName, const string &nodeName) + { + return create(ownerName, Signature::KeyPair(), nodeName); + } + + unique_ptr Database::create(const string &ownerName, const Signature::KeyPair &keyPair, const string &nodeName) { // TODO: Should this be declared static? is there any difference in behavior/performance? boost::uuids::random_generator uuidGen; auto adminGroupId = uuidGen(); auto adminGroup = new Group("administrator", adminGroupId.data, ADMIN_PERMISSION); - LocalUser *nodeAdminUser = LocalUser::create(Signature::KeyPair(), ownerName, adminGroup); + LocalUser *nodeAdminUser = LocalUser::create(keyPair, ownerName, adminGroup); // Header sibs::SafeSerializer serializer; diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 7ce4919..34e6da4 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -533,14 +533,14 @@ namespace odhtdb return nullptr; } - bool DatabaseStorage::storeLocalUser(const string &username, const Signature::PublicKey &publicKey, const Signature::PrivateKey &privateKey, const string &password) + bool DatabaseStorage::storeLocalUser(const string &username, const Signature::KeyPair &keyPair, const string &password) { auto it = nameLocalUsersMap.find(username); if(it != nameLocalUsersMap.end()) return false; OwnedMemory hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN)); - DataView privateKeyView((void*)privateKey.getData(), PRIVATE_KEY_NUM_BYTES); + DataView privateKeyView((void*)keyPair.getPrivateKey().getData(), PRIVATE_KEY_NUM_BYTES); DataView hashedPasswordView(hashedPassword.data, hashedPassword.size); Encryption encryptedPrivateKey(privateKeyView, {}, hashedPasswordView); @@ -549,13 +549,13 @@ namespace odhtdb assert(sizeof(userEncryptedPrivateKey.encryptedPrivateKey) == encryptedPrivateKey.getCipherText().size); memcpy(userEncryptedPrivateKey.encryptedPrivateKey, encryptedPrivateKey.getCipherText().data, encryptedPrivateKey.getCipherText().size); - LocalUserEncrypted *localUserEncrypted = LocalUserEncrypted::create(publicKey, userEncryptedPrivateKey, username); + LocalUserEncrypted *localUserEncrypted = LocalUserEncrypted::create(keyPair.getPublicKey(), userEncryptedPrivateKey, username); nameLocalUsersMap[username] = localUserEncrypted; sibs::SafeSerializer serializer; serializer.add((u8)username.size()); serializer.add((const u8*)username.data(), username.size()); - serializer.add((const u8*)publicKey.getData(), PUBLIC_KEY_NUM_BYTES); + serializer.add((const u8*)keyPair.getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES); serializer.add((const u8*)encryptedPrivateKey.getNonce().data, ENCRYPTION_NONCE_BYTE_SIZE); serializer.add((const u8*)encryptedPrivateKey.getCipherText().data, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES); @@ -563,6 +563,66 @@ namespace odhtdb return true; } + Signature::KeyPair DatabaseStorage::decryptLocalEncryptedUser(const string &username, const string &password) + { + auto localUserIt = nameLocalUsersMap.find(username); + if(localUserIt == nameLocalUsersMap.end()) + { + string errMsg = "User "; + errMsg += username; + errMsg += " does not exist in local storage"; + throw DatabaseStorageNoSuchLocalStorageUser(errMsg); + } + + DataView passwordView((void*)password.data(), password.size()); + DataView saltView((void*)passwordSalt, PASSWORD_SALT_LEN); + try + { + auto privateKey = localUserIt->second->getPrivateKey().decrypt(passwordView, saltView); + Signature::KeyPair keyPair(localUserIt->second->getPublicKey(), privateKey); + return keyPair; + } + catch(DecryptionException &e) + { + string errMsg = "Wrong password provided for user "; + errMsg += username; + errMsg += " in local storage ("; + errMsg += e.what(); + errMsg += ")"; + throw DatabaseStorageWrongPassword(errMsg); + } + } + + vector DatabaseStorage::getLocalNodeUsers(const Signature::KeyPair &keyPair) + { + vector localUsers; + + for(auto nodeIt : nodePublicKeyUserDataMap) + { + auto userIt = nodeIt.second->find(keyPair.getPublicKey()); + if(userIt != nodeIt.second->end()) + { + User *user = userIt->second; + if(user->getType() != User::Type::LOCAL) + { + LocalUser *localUser = LocalUser::create(keyPair, user->getName(), nullptr); + for(Group *group : user->getGroups()) + { + localUser->addToGroup(group); + } + + (*nodeIt.second)[keyPair.getPublicKey()] = localUser; + localUsers.push_back(make_pair(nodeIt.first, localUser)); + delete user; + } + else + localUsers.push_back(make_pair(nodeIt.first, static_cast(user))); + } + } + + return localUsers; + } + const dht::crypto::Identity& DatabaseStorage::getIdentity() const { return identity; diff --git a/src/Group.cpp b/src/Group.cpp index a99fdf6..4210142 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -24,6 +24,19 @@ namespace odhtdb { users.push_back(user); } + + bool Group::removeUser(const User *user) + { + for(std::vector::iterator it = users.begin(); it != users.end(); ++it) + { + if(*it == user) + { + users.erase(it); + return true; + } + } + return false; + } const string& Group::getName() const { diff --git a/src/User.cpp b/src/User.cpp index 1fb4a11..d157c74 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -10,6 +10,14 @@ namespace odhtdb addToGroup(group); } + User::~User() + { + for(Group *group : groups) + { + group->removeUser(this); + } + } + void User::addToGroup(Group *group) { if(group) -- cgit v1.2.3