more cleanup, plus now keyfile associated with user directly

This commit is contained in:
David Kelly 2017-09-12 10:55:27 -07:00
parent 61e8458d13
commit 187ed71a8a
3 changed files with 26 additions and 11 deletions

View file

@ -18,6 +18,7 @@
#include <PathUtils.h>
#include <OffscreenUi.h>
#include <AccountManager.h>
#include <QFile>
#include <QCryptographicHash>
@ -54,7 +55,8 @@ void initialize() {
}
QString keyFilePath() {
return PathUtils::getAppDataFilePath(KEY_FILE);
auto accountManager = DependencyManager::get<AccountManager>();
return PathUtils::getAppDataFilePath(QString("%1.%2").arg(accountManager->getAccountInfo().getUsername(), KEY_FILE));
}
// use the cached _passphrase if it exists, otherwise we need to prompt
@ -262,6 +264,15 @@ RSA* readPrivateKey(const char* filename) {
return key;
}
// QT's QByteArray will convert to Base64 without any embedded newlines. This just
// writes it with embedded newlines, which is more readable.
void outputBase64WithNewlines(QFile& file, const QByteArray& b64Array) {
for (int i = 0; i < b64Array.size(); i += 64) {
file.write(b64Array.mid(i, 64));
file.write("\n");
}
}
void initializeAESKeys(unsigned char* ivec, unsigned char* ckey, const QByteArray& salt) {
// use the ones in the wallet
auto wallet = DependencyManager::get<Wallet>();
@ -331,11 +342,11 @@ bool Wallet::writeSecurityImage(const QPixmap* pixmap, const QString& outputFile
QByteArray output((const char*)outputFileBuffer, outSize);
// now APPEND to the file,
QByteArray b64output = output.toBase64();
QFile outputFile(outputFilePath);
outputFile.open(QIODevice::Append);
outputFile.write(IMAGE_HEADER);
outputFile.write(output.toBase64());
outputFile.write("\n");
outputBase64WithNewlines(outputFile, b64output);
outputFile.write(IMAGE_FOOTER);
outputFile.close();
@ -551,9 +562,11 @@ void Wallet::getSecurityImage() {
return;
}
// decrypt and return
bool success = false;
// decrypt and return. Don't bother if we have no file to decrypt, or
// no salt set yet.
QFileInfo fileInfo(keyFilePath());
if (fileInfo.exists() && readSecurityImage(keyFilePath(), &data, &dataLen)) {
if (fileInfo.exists() && _salt.size() > 0 && readSecurityImage(keyFilePath(), &data, &dataLen)) {
// create the pixmap
_securityImage = new QPixmap();
_securityImage->loadFromData(data, dataLen, "jpg");
@ -562,11 +575,9 @@ void Wallet::getSecurityImage() {
updateImageProvider();
delete[] data;
emit securityImageResult(true);
} else {
qCDebug(commerce) << "failed to decrypt security image (maybe none saved yet?)";
emit securityImageResult(false);
success = true;
}
emit securityImageResult(success);
}
void Wallet::sendKeyFilePathIfExists() {
QString filePath(keyFilePath());

View file

@ -55,7 +55,7 @@ signals:
private:
QStringList _publicKeys{};
QPixmap* _securityImage { nullptr };
QByteArray _salt {"iamsalt!"};
QByteArray _salt;
QByteArray _iv;
QByteArray _ckey;
QString* _passphrase { new QString("") };

View file

@ -51,5 +51,9 @@ QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize
return _securityImage->copy();
}
}
return QPixmap();
// otherwise just return a grey pixmap. This avoids annoying error messages in qml we would get
// when sending a 'null' pixmap (QPixmap())
QPixmap greyPixmap(200, 200);
greyPixmap.fill(QColor("darkGrey"));
return greyPixmap;
}