From 2ecdfb3b47882411659a0efe451b0910c85a32f5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 27 Apr 2018 09:21:27 +0200 Subject: Change local storage to fit dchat better --- src/DatabaseStorage.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) (limited to 'src/DatabaseStorage.cpp') 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; -- cgit v1.2.3