Browser Integration code cleanup (#8489)

Co-authored-by: varjolintu <sami.vanttinen@protonmail.com>
This commit is contained in:
Sami Vänttinen 2023-02-18 22:52:31 +02:00 committed by GitHub
parent ba15981700
commit 4a30417f76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 321 additions and 336 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -66,17 +66,24 @@ QJsonObject BrowserMessageBuilder::buildMessage(const QString& nonce) const
}
QJsonObject BrowserMessageBuilder::buildResponse(const QString& action,
const QJsonObject& message,
const QString& nonce,
const Parameters& params,
const QString& publicKey,
const QString& secretKey)
{
QJsonObject response;
QString encryptedMessage = encryptMessage(message, nonce, publicKey, secretKey);
auto message = buildMessage(nonce);
Parameters::const_iterator i;
for (i = params.constBegin(); i != params.constEnd(); ++i) {
message[i.key()] = QJsonValue::fromVariant(i.value());
}
const auto encryptedMessage = encryptMessage(message, nonce, publicKey, secretKey);
if (encryptedMessage.isEmpty()) {
return getErrorReply(action, ERROR_KEEPASS_CANNOT_ENCRYPT_MESSAGE);
}
QJsonObject response;
response["action"] = action;
response["message"] = encryptedMessage;
response["nonce"] = nonce;
@ -127,7 +134,7 @@ QString BrowserMessageBuilder::encryptMessage(const QJsonObject& message,
const QString& secretKey)
{
if (message.isEmpty() || nonce.isEmpty()) {
return QString();
return {};
}
const QString reply(QJsonDocument(message).toJson());
@ -135,7 +142,7 @@ QString BrowserMessageBuilder::encryptMessage(const QJsonObject& message,
return encrypt(reply, nonce, publicKey, secretKey);
}
return QString();
return {};
}
QJsonObject BrowserMessageBuilder::decryptMessage(const QString& message,
@ -144,12 +151,12 @@ QJsonObject BrowserMessageBuilder::decryptMessage(const QString& message,
const QString& secretKey)
{
if (message.isEmpty() || nonce.isEmpty()) {
return QJsonObject();
return {};
}
QByteArray ba = decrypt(message, nonce, publicKey, secretKey);
if (ba.isEmpty()) {
return QJsonObject();
return {};
}
return getJsonObject(ba);
@ -174,7 +181,7 @@ QString BrowserMessageBuilder::encrypt(const QString& plaintext,
e.resize(BrowserShared::NATIVEMSG_MAX_LENGTH);
if (m.empty() || n.empty() || ck.empty() || sk.empty()) {
return QString();
return {};
}
if (crypto_box_easy(e.data(), m.data(), m.size(), n.data(), ck.data(), sk.data()) == 0) {
@ -182,7 +189,7 @@ QString BrowserMessageBuilder::encrypt(const QString& plaintext,
return res.toBase64();
}
return QString();
return {};
}
QByteArray BrowserMessageBuilder::decrypt(const QString& encrypted,
@ -204,14 +211,14 @@ QByteArray BrowserMessageBuilder::decrypt(const QString& encrypted,
d.resize(BrowserShared::NATIVEMSG_MAX_LENGTH);
if (m.empty() || n.empty() || ck.empty() || sk.empty()) {
return QByteArray();
return {};
}
if (crypto_box_open_easy(d.data(), m.data(), ma.length(), n.data(), ck.data(), sk.data()) == 0) {
return getQByteArray(d.data(), std::char_traits<char>::length(reinterpret_cast<const char*>(d.data())));
}
return QByteArray();
return {};
}
QString BrowserMessageBuilder::getBase64FromKey(const uchar* array, const uint len)