diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-04-27 09:21:27 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:46 +0200 |
commit | 2ecdfb3b47882411659a0efe451b0910c85a32f5 (patch) | |
tree | 1c2c5c91c9e2264580b707d5616b786f85b5ce85 /src/DatabaseStorage.cpp | |
parent | 04cfe9c03baa5691ebfad6e039e4f0acd74fd8e1 (diff) |
Change local storage to fit dchat better
Diffstat (limited to 'src/DatabaseStorage.cpp')
-rw-r--r-- | src/DatabaseStorage.cpp | 68 |
1 files changed, 64 insertions, 4 deletions
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<NodeLocalUser> DatabaseStorage::getLocalNodeUsers(const Signature::KeyPair &keyPair) + { + vector<NodeLocalUser> 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<LocalUser*>(user))); + } + } + + return localUsers; + } + const dht::crypto::Identity& DatabaseStorage::getIdentity() const { return identity; |