Browse Source

proper encrypted history loading

pull/985/head
apprb 11 years ago committed by Dubslow
parent
commit
a9fb75b5ed
  1. 34
      src/core.cpp
  2. 2
      src/core.h
  3. 8
      src/historykeeper.cpp
  4. 1
      src/historykeeper.h
  5. 2
      src/misc/db/encrypteddb.cpp
  6. 73
      src/widget/form/settings/privacyform.cpp
  7. 6
      src/widget/form/settings/privacysettings.ui
  8. 1
      src/widget/widget.h

34
src/core.cpp

@ -1235,13 +1235,9 @@ bool Core::loadConfiguration(QString path) @@ -1235,13 +1235,9 @@ bool Core::loadConfiguration(QString path)
bool error = true;
// get salt
QFile file(HistoryKeeper::getHistoryPath());
file.open(QIODevice::ReadOnly);
QByteArray data = file.read(tox_pass_encryption_extra_length());
file.close();
uint8_t salt[tox_pass_salt_length()];
int err = tox_get_salt(reinterpret_cast<uint8_t *>(data.data()), salt);
if (err)
QByteArray salt = getSaltFromFile(HistoryKeeper::getHistoryPath());
if (salt.size() == 0)
{ // maybe we should handle this better
qWarning() << "Core: history db isn't encrypted, but encryption is set!! No history loaded...";
}
@ -1251,7 +1247,8 @@ bool Core::loadConfiguration(QString path) @@ -1251,7 +1247,8 @@ bool Core::loadConfiguration(QString path)
{
while (!pwsaltedkeys[ptHistory])
{
emit blockingGetPassword(tr("History Log decryption password"), Core::ptHistory, salt);
emit blockingGetPassword(tr("History Log decryption password"), Core::ptHistory,
reinterpret_cast<uint8_t*>(salt.data()));
if (!pwsaltedkeys[ptHistory])
Widget::getInstance()->showWarningMsgBox(tr("Password error"), tr("Failed to setup password.\nEmpty password."));
}
@ -1891,3 +1888,24 @@ void Core::resetCallSources() @@ -1891,3 +1888,24 @@ void Core::resetCallSources()
}
}
}
QByteArray Core::getSaltFromFile(QString filename)
{
qDebug() << filename;
QFile file(filename);
file.open(QIODevice::ReadOnly);
QByteArray data = file.read(tox_pass_encryption_extra_length());
file.close();
qDebug() << "data size" << data.size();
uint8_t *salt = new uint8_t[tox_pass_salt_length()];
int err = tox_get_salt(reinterpret_cast<uint8_t *>(data.data()), salt);
if (err)
{
qWarning() << "Core: can't get salt from" << filename << "header";
return QByteArray();
}
return QByteArray::fromRawData(reinterpret_cast<const char*>(salt), tox_pass_salt_length());
}

2
src/core.h

@ -49,6 +49,8 @@ public: @@ -49,6 +49,8 @@ public:
static QString sanitize(QString name);
static QList<CString> splitMessage(const QString &message);
static QByteArray getSaltFromFile(QString filename);
QString getPeerName(const ToxID& id) const;
int getGroupNumberPeers(int groupId) const; ///< Return the number of peers in the group chat on success, or -1 on failure

8
src/historykeeper.cpp

@ -342,3 +342,11 @@ void HistoryKeeper::setSyncType(Db::syncType sType) @@ -342,3 +342,11 @@ void HistoryKeeper::setSyncType(Db::syncType sType)
db->exec(QString("PRAGMA synchronous=%1;").arg(syncCmd));
}
bool HistoryKeeper::isFileExist()
{
QString path = getHistoryPath();
QFile file(path);
return file.exists();
}

1
src/historykeeper.h

@ -45,6 +45,7 @@ public: @@ -45,6 +45,7 @@ public:
static QString getHistoryPath(QString currentProfile = QString(), int encrypted = -1); // -1 defaults to checking settings, 0 or 1 to specify
static bool checkPassword();
static bool isFileExist();
static void renameHistory(QString from, QString to);
int addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent);

2
src/misc/db/encrypteddb.cpp

@ -65,12 +65,14 @@ QSqlQuery EncryptedDb::exec(const QString &query) @@ -65,12 +65,14 @@ QSqlQuery EncryptedDb::exec(const QString &query)
bool EncryptedDb::pullFileContent()
{
qDebug() << "EncryptedDb::pullFileContent()";
encrFile.open(QIODevice::ReadOnly);
QByteArray fileContent;
while (!encrFile.atEnd())
{
QByteArray encrChunk = encrFile.read(encryptedChunkSize);
qDebug() << "got chunk:" << encrChunk.size();
buffer = Core::getInstance()->decryptData(encrChunk, Core::ptHistory);
if (buffer.size() > 0)
{

73
src/widget/form/settings/privacyform.cpp

@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
#include "src/widget/widget.h"
#include "src/widget/form/setpassworddialog.h"
#include <QMessageBox>
#include <QFile>
PrivacyForm::PrivacyForm() :
GenericForm(tr("Privacy"), QPixmap(":/img/settings/privacy.png"))
@ -60,43 +61,75 @@ void PrivacyForm::onTypingNotificationEnabledUpdated() @@ -60,43 +61,75 @@ void PrivacyForm::onTypingNotificationEnabledUpdated()
void PrivacyForm::onEncryptLogsUpdated()
{
bool encrytionState = bodyUI->cbEncryptHistory->isChecked();
bool keepOldFile = false;
if (encrytionState)
{
if (!Core::getInstance()->isPasswordSet(Core::ptHistory))
Settings::getInstance().setEncryptLogs(true);
if (HistoryKeeper::isFileExist())
{
SetPasswordDialog dialog;
if (dialog.exec())
QByteArray salt = Core::getSaltFromFile(HistoryKeeper::getHistoryPath());
if (salt.size() != 0)
{
QString pswd = dialog.getPassword();
if (pswd.size() == 0)
encrytionState = false;
Core::getInstance()->setPassword(pswd, Core::ptHistory);
} else {
encrytionState = false;
Core::getInstance()->clearPassword(Core::ptHistory);
if (QMessageBox::Ok == QMessageBox::warning(nullptr, tr("Encrypted log"),
tr("You already have history file.\nDo you want to try open it?"),
QMessageBox::Ok | QMessageBox::Cancel))
{
keepOldFile = true;
bool exit = false;
do
{
Widget::getInstance()->getPassword(tr("Encrypted log"), Core::ptHistory, reinterpret_cast<uint8_t*>(salt.data()));
exit = HistoryKeeper::checkPassword();
if (!exit)
{
if (QMessageBox::warning(nullptr, tr("Encrypted log"), tr("Wrong password!\nTry again?"),
QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok)
{
keepOldFile = false;
encrytionState = false;
exit = true;
QMessageBox::warning(nullptr, tr("Encrypetd log"), tr("Encrypted log will be disabled!"));
}
}
} while (!exit);
} else {
if (QMessageBox::warning(nullptr, tr("Encrypted log"), tr("Do you want to delete encrypted history file?"),
QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok)
{
keepOldFile = true;
encrytionState = false;
}
}
}
}
}
Settings::getInstance().setEncryptLogs(encrytionState);
if (encrytionState && !HistoryKeeper::checkPassword())
if (encrytionState && !keepOldFile)
{
if (QMessageBox::Ok != QMessageBox::warning(nullptr, tr("Encrypted log"),
tr("You already have history log file encrypted with different password\nDo you want to delete old history file?"),
QMessageBox::Ok | QMessageBox::Cancel))
Core::getInstance()->clearPassword(Core::ptHistory);
SetPasswordDialog dialog;
if (dialog.exec())
{
// TODO: ask user about reencryption with new password
QString pswd = dialog.getPassword();
if (pswd.size() == 0)
encrytionState = false;
Core::getInstance()->setPassword(pswd, Core::ptHistory);
} else {
encrytionState = false;
}
}
Settings::getInstance().setEncryptLogs(encrytionState);
bodyUI->cbEncryptHistory->setChecked(encrytionState);
if (encrytionState)
HistoryKeeper::resetInstance();
HistoryKeeper::resetInstance();
Settings::getInstance().setEncryptLogs(encrytionState);
bodyUI->cbEncryptHistory->setChecked(encrytionState);
if (!Settings::getInstance().getEncryptLogs())
Core::getInstance()->clearPassword(Core::ptHistory);

6
src/widget/form/settings/privacysettings.ui

@ -73,7 +73,7 @@ @@ -73,7 +73,7 @@
<item>
<widget class="QCheckBox" name="cbEncryptTox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="text">
<string>Encrypt Tox datafile</string>
@ -83,13 +83,13 @@ @@ -83,13 +83,13 @@
<item>
<widget class="QCheckBox" name="cbEncryptHistory">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="text">
<string>Encrypt History</string>
</property>
<property name="checkable">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>

1
src/widget/widget.h

@ -131,7 +131,6 @@ private slots: @@ -131,7 +131,6 @@ private slots:
void playRingtone();
void onIconClick(QSystemTrayIcon::ActivationReason);
void onUserAwayCheck();
void getPassword(QString info, int passtype, uint8_t* salt);
void onSetShowSystemTray(bool newValue);
void onSplitterMoved(int pos, int index);

Loading…
Cancel
Save