desktop -> app encryption

This commit is contained in:
Aditya Kulkarni
2019-01-25 11:04:03 -08:00
parent 050e686197
commit 82343f7209
3 changed files with 99 additions and 49 deletions

View File

@@ -562,6 +562,7 @@ void MainWindow::connectApp() {
QObject::connect(con.btnDisconnect, &QPushButton::clicked, [=]() { QObject::connect(con.btnDisconnect, &QPushButton::clicked, [=]() {
AppDataServer::saveNonceHex(NonceType::REMOTE, QString("00").repeated(24)); AppDataServer::saveNonceHex(NonceType::REMOTE, QString("00").repeated(24));
AppDataServer::saveNonceHex(NonceType::LOCAL, QString("00").repeated(24));
}); });
d.exec(); d.exec();

View File

@@ -45,7 +45,7 @@ void WSServer::processTextMessage(QString message)
if (pClient) { if (pClient) {
auto json = AppDataServer::processMessage(message, m_mainWindow); auto json = AppDataServer::processMessage(message, m_mainWindow);
if (!json.isEmpty()) if (!json.isEmpty())
pClient->sendTextMessage(json.toJson()); pClient->sendTextMessage(AppDataServer::encryptOutgoing(json.toJson()));
} }
} }
@@ -100,12 +100,48 @@ void AppDataServer::saveNonceHex(NonceType nt, QString noncehex) {
} }
} }
QJsonDocument AppDataServer::processMessage(QString message, MainWindow* mainWindow) { QString AppDataServer::encryptOutgoing(QString msg) {
// First, extract the command from the message QString localNonceHex = getNonceHex(NonceType::LOCAL);
auto msg = QJsonDocument::fromJson(message.toUtf8());
// First check if the message is encrpted unsigned char* noncebin = new unsigned char[crypto_secretbox_NONCEBYTES];
if (msg.object().contains("nonce")) { sodium_hex2bin(noncebin, crypto_secretbox_NONCEBYTES, localNonceHex.toStdString().c_str(), localNonceHex.length(),
NULL, NULL, NULL);
// Increment the nonce +2 and save
sodium_increment(noncebin, crypto_secretbox_NONCEBYTES);
sodium_increment(noncebin, crypto_secretbox_NONCEBYTES);
char* newLocalNonce = new char[crypto_secretbox_NONCEBYTES*2 + 1];
sodium_memzero(newLocalNonce, crypto_secretbox_NONCEBYTES*2 + 1);
sodium_bin2hex(newLocalNonce, crypto_secretbox_NONCEBYTES*2+1, noncebin, crypto_box_NONCEBYTES);
qDebug() << "Local nonce was " << localNonceHex << " now is " << QString(newLocalNonce);
saveNonceHex(NonceType::LOCAL, QString(newLocalNonce));
unsigned char* secret = new unsigned char[crypto_secretbox_KEYBYTES];
crypto_hash_sha256(secret, (const unsigned char*)"secret", QString("secret").length());
int msgSize = strlen(msg.toStdString().c_str());
unsigned char* encrpyted = new unsigned char[ msgSize + crypto_secretbox_MACBYTES];
crypto_secretbox_easy(encrpyted, (const unsigned char *)msg.toStdString().c_str(), msgSize, noncebin, secret);
int encryptedHexSize = (msgSize + crypto_secretbox_MACBYTES) * 2 + 1;
char * encryptedHex = new char[encryptedHexSize];
sodium_memzero(encryptedHex, encryptedHexSize);
sodium_bin2hex(encryptedHex, encryptedHexSize, encrpyted, msgSize + crypto_secretbox_MACBYTES);
qDebug() << "Encrypted to " << QString(encryptedHex);
auto json = QJsonDocument(QJsonObject{
{"nonce", QString(newLocalNonce)},
{"payload", QString(encryptedHex)}
});
return json.toJson();
}
QString AppDataServer::decryptMessage(QJsonDocument msg) {
// Decrypt and then process // Decrypt and then process
QString noncehex = msg.object().value("nonce").toString(); QString noncehex = msg.object().value("nonce").toString();
QString encryptedhex = msg.object().value("payload").toString(); QString encryptedhex = msg.object().value("payload").toString();
@@ -152,7 +188,16 @@ QJsonDocument AppDataServer::processMessage(QString message, MainWindow* mainWin
delete[] decrypted; delete[] decrypted;
delete[] decryptedStr; delete[] decryptedStr;
return processMessage(payload, mainWindow); return payload;
}
QJsonDocument AppDataServer::processMessage(QString message, MainWindow* mainWindow) {
// First, extract the command from the message
auto msg = QJsonDocument::fromJson(message.toUtf8());
// First check if the message is encrpted
if (msg.object().contains("nonce")) {
return processMessage(decryptMessage(msg), mainWindow);
} }
if (!msg.object().contains("command")) { if (!msg.object().contains("command")) {

View File

@@ -42,6 +42,10 @@ public:
static QJsonDocument processMessage(QString message, MainWindow* mainWindow); static QJsonDocument processMessage(QString message, MainWindow* mainWindow);
static QJsonDocument processGetInfo(MainWindow* mainWindow); static QJsonDocument processGetInfo(MainWindow* mainWindow);
static QJsonDocument processGetTransactions(MainWindow* mainWindow); static QJsonDocument processGetTransactions(MainWindow* mainWindow);
static QString decryptMessage(QJsonDocument msg);
static QString encryptOutgoing(QString msg);
static QString getSecretHex(); static QString getSecretHex();
static QString getNonceHex(NonceType nt); static QString getNonceHex(NonceType nt);
static void saveNonceHex(NonceType nt, QString noncehex); static void saveNonceHex(NonceType nt, QString noncehex);