1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/core/coreencryption.cpp

175 lines
5.5 KiB
C++
Raw Normal View History

/*
Copyright © 2014-2015 by The qTox Project
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
/* This file is part of the Core class, but was separated for my sanity */
/* The load function delegates to loadEncrypted here, and the save function */
/* was permanently moved here to handle encryption */
#include "core.h"
#include "src/widget/gui.h"
2015-06-06 07:44:47 +08:00
#include "src/persistence/settings.h"
#include "src/core/cstring.h"
#include "src/persistence/historykeeper.h"
2015-06-04 20:19:18 +08:00
#include "src/nexus.h"
2015-06-06 07:44:47 +08:00
#include "src/persistence/profile.h"
#include <tox/tox.h>
#include <tox/toxencryptsave.h>
#include <QApplication>
#include <QDebug>
#include <QSaveFile>
#include <QFile>
#include <QThread>
#include <algorithm>
#include <cassert>
void Core::setPassword(const QString& password, uint8_t* salt)
{
clearPassword();
if (password.isEmpty())
return;
2014-12-09 06:40:20 +08:00
encryptionKey = new TOX_PASS_KEY;
CString str(password);
if (salt)
tox_derive_key_with_salt(str.data(), str.size(), salt, encryptionKey, nullptr);
else
tox_derive_key_from_pass(str.data(), str.size(), encryptionKey, nullptr);
}
void Core::clearPassword()
{
delete encryptionKey;
encryptionKey = nullptr;
}
QByteArray Core::encryptData(const QByteArray& data)
{
if (!encryptionKey)
{
qWarning() << "No encryption key set";
return QByteArray();
}
2015-04-20 15:58:06 +08:00
uint8_t encrypted[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
if (!tox_pass_key_encrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
encryptionKey, encrypted, nullptr))
{
qWarning() << "Encryption failed";
return QByteArray();
}
return QByteArray(reinterpret_cast<char*>(encrypted), data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
}
QByteArray Core::decryptData(const QByteArray& data)
{
if (!encryptionKey)
{
qWarning() << "No encryption key set";
return QByteArray();
}
2015-04-20 15:58:06 +08:00
int sz = data.size() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t decrypted[sz];
if (!tox_pass_key_decrypt(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
encryptionKey, decrypted, nullptr))
{
qWarning() << "Decryption failed";
return QByteArray();
}
return QByteArray(reinterpret_cast<char*>(decrypted), sz);
}
bool Core::isPasswordSet()
{
return static_cast<bool>(encryptionKey);
}
QByteArray Core::getSaltFromFile(QString filename)
{
QFile file(filename);
2014-12-10 08:08:38 +08:00
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "file" << filename << "doesn't exist";
2014-12-10 08:08:38 +08:00
return QByteArray();
}
QByteArray data = file.read(TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
file.close();
uint8_t *salt = new uint8_t[TOX_PASS_SALT_LENGTH];
if (!tox_get_salt(reinterpret_cast<uint8_t *>(data.data()), salt))
{
qWarning() << "can't get salt from" << filename << "header";
return QByteArray();
}
QByteArray res(reinterpret_cast<const char*>(salt), TOX_PASS_SALT_LENGTH);
2015-03-22 04:58:26 +08:00
delete[] salt;
return res;
}
void Core::checkEncryptedHistory()
{
QString path = HistoryKeeper::getHistoryPath();
bool exists = QFile::exists(path) && QFile(path).size()>0;
QByteArray salt = getSaltFromFile(path);
if (exists && salt.size() == 0)
{ // maybe we should handle this better
GUI::showWarning(tr("Encrypted chat history"), tr("No encrypted chat history file found, or it was corrupted.\nHistory will be disabled!"));
HistoryKeeper::resetInstance();
return;
}
setPassword(Nexus::getProfile()->getPassword(), reinterpret_cast<uint8_t*>(salt.data()));
2015-06-04 20:19:18 +08:00
QString a(tr("Please enter the password for the chat history for the profile \"%1\".", "used in load() when no hist pw set").arg(Nexus::getProfile()->getName()));
QString b(tr("The previous password is incorrect; please try again:", "used on retries in load()"));
QString c(tr("\nDisabling chat history now will leave the encrypted history intact (but not usable); if you later remember the password, you may re-enable encryption from the Privacy tab with the correct password to use the history.", "part of history password dialog"));
QString dialogtxt;
2015-04-20 15:58:06 +08:00
if (!exists || HistoryKeeper::checkPassword())
return;
2015-04-20 15:58:06 +08:00
dialogtxt = tr("The chat history password failed. Please try another?", "used only when pw set before load() doesn't work");
2015-02-04 08:22:05 +08:00
dialogtxt += "\n" + c;
bool error = true;
do
{
QString pw = GUI::passwordDialog(tr("Disable chat history"), dialogtxt);
if (pw.isEmpty())
{
clearPassword();
Settings::getInstance().setEnableLogging(false);
HistoryKeeper::resetInstance();
return;
}
else
{
setPassword(pw, reinterpret_cast<uint8_t*>(salt.data()));
}
error = exists && !HistoryKeeper::checkPassword();
2015-02-04 08:22:05 +08:00
dialogtxt = a + "\n" + c + "\n" + b;
} while (error);
}