From 60e819baeed7ad2ef7a3097169d2daeefa6f2e60 Mon Sep 17 00:00:00 2001 From: Dubslow Date: Tue, 27 Jan 2015 14:16:28 -0600 Subject: [PATCH] Hacky fix to cancelling a switch to an encrypted profile closing It's either this or some fairly major refactoring, which is preferable, but I've got another even bigger refactoring in mind, and fixing this will be rolled into that. Closes dubslow/qTox#4 --- src/core.cpp | 29 ++++++++++++++++++++--------- src/core.h | 18 ++++++++++++++---- src/coreencryption.cpp | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/core.cpp b/src/core.cpp index e4fdd7529..df3f9f562 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -178,7 +178,7 @@ void Core::make_tox() { qCritical() << "Core: bad proxy! no toxcore!"; emit badProxy(); - } + } else { qCritical() << "Tox core failed to start"; @@ -223,9 +223,19 @@ void Core::start() { if (loadPath.isEmpty()) { - qCritical() << "Core: loadConfiguration failed, exiting now"; - emit failedToStart(); - return; + QString profile; + if ((profile = loadOldInformation()).isEmpty()) + { + qCritical() << "Core: loadConfiguration failed, exiting now"; + emit failedToStart(); + return; + } + else + { + loadPath = QDir(Settings::getSettingsDirPath()).filePath(profile + TOX_EXT); + Settings::getInstance().switchProfile(profile); + HistoryKeeper::resetInstance(); // I'm not actually sure if this is necessary + } } } // loadPath is meaningless after this @@ -284,7 +294,7 @@ void Core::start() } else qDebug() << "Core: Error loading self avatar"; - + ready = true; process(); // starts its own timer @@ -1201,7 +1211,7 @@ bool Core::loadConfiguration(QString path) QString name = getUsername(); if (!name.isEmpty()) emit usernameSet(name); - + QString msg = getStatusMessage(); if (!msg.isEmpty()) emit statusMessageSet(msg); @@ -1229,7 +1239,7 @@ void Core::saveConfiguration() qCritical() << "Error while creating directory " << dir; return; } - + QString profile = Settings::getInstance().getCurrentProfile(); if (profile == "") @@ -1241,9 +1251,9 @@ void Core::saveConfiguration() Settings::getInstance().switchProfile(profile); } - + QString path = directory.filePath(profile + TOX_EXT); - + saveConfiguration(path); } @@ -1255,6 +1265,7 @@ void Core::switchConfiguration(const QString& profile) qDebug() << "Core: switching from" << Settings::getInstance().getCurrentProfile() << "to" << profile; saveConfiguration(); + saveCurrentInformation(); // part of a hack, see core.h ready = false; Widget::getInstance()->setEnabledThreadsafe(false); diff --git a/src/core.h b/src/core.h index e4166c572..a3389e023 100644 --- a/src/core.h +++ b/src/core.h @@ -46,7 +46,7 @@ public: explicit Core(Camera* cam, QThread* coreThread, QString initialLoadPath); static Core* getInstance(); ///< Returns the global widget's Core instance ~Core(); - + static const QString TOX_EXT; static const QString CONFIG_FILE_NAME; static QString sanitize(QString name); @@ -69,9 +69,9 @@ public: void saveConfiguration(); void saveConfiguration(const QString& path); - + QString getIDString() const; ///< Get the 12 first characters of our Tox ID - + QString getUsername() const; ///< Returns our username, or an empty string on failure QString getStatusMessage() const; ///< Returns our status message, or an empty string on failure ToxID getSelfId() const; ///< Returns our Tox ID @@ -299,7 +299,17 @@ private: QMutex fileSendMutex, messageSendMutex; bool ready; - uint8_t* pwsaltedkeys[PasswordType::ptCounter]; // use the pw's hash as the "pw" + uint8_t* pwsaltedkeys[PasswordType::ptCounter] = {nullptr}; // use the pw's hash as the "pw" + + // Hack for reloading current profile if switching to an encrypted one fails. + // Testing the passwords before killing the current profile is perfectly doable, + // however it would require major refactoring; + // the Core class as a whole also requires major refactoring (especially to support multiple IDs at once), + // so I'm punting on this until then, when it would get fixed anyways + uint8_t* backupkeys[PasswordType::ptCounter] = {nullptr}; + QString* backupProfile = nullptr; + void saveCurrentInformation(); + QString loadOldInformation(); static const int videobufsize; static uint8_t* videobuf; diff --git a/src/coreencryption.cpp b/src/coreencryption.cpp index 42ee93c90..f47ada5f8 100644 --- a/src/coreencryption.cpp +++ b/src/coreencryption.cpp @@ -65,6 +65,43 @@ void Core::clearPassword(PasswordType passtype) pwsaltedkeys[passtype] = nullptr; } +// part of a hack, see core.h +void Core::saveCurrentInformation() +{ + if (pwsaltedkeys[ptMain]) + { + backupkeys[ptMain] = new uint8_t[tox_pass_key_length()]; + std::copy(pwsaltedkeys[ptMain], pwsaltedkeys[ptMain]+tox_pass_key_length(), backupkeys[ptMain]); + } + if (pwsaltedkeys[ptHistory]) + { + backupkeys[ptHistory] = new uint8_t[tox_pass_key_length()]; + std::copy(pwsaltedkeys[ptHistory], pwsaltedkeys[ptHistory]+tox_pass_key_length(), backupkeys[ptHistory]); + } + backupProfile = new QString(Settings::getInstance().getCurrentProfile()); +} + +QString Core::loadOldInformation() +{ + QString out; + if (backupProfile) + { + out = *backupProfile; + delete backupProfile; + backupProfile = nullptr; + } + backupProfile = nullptr; + clearPassword(ptMain); + clearPassword(ptHistory); + // we can just copy the pointer, as long as we null out backupkeys + // (if backupkeys was null anyways, then this is a null-op) + pwsaltedkeys[ptMain] = backupkeys[ptMain]; + pwsaltedkeys[ptHistory] = backupkeys[ptHistory]; + backupkeys[ptMain] = nullptr; + backupkeys[ptHistory] = nullptr; + return out; +} + QByteArray Core::encryptData(const QByteArray& data, PasswordType passtype) { if (!pwsaltedkeys[passtype])