mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 18:50:00 +02:00
fix warnings, prepare a bit for hooking up the passphrase
This commit is contained in:
parent
57d973851e
commit
c77dafdf12
3 changed files with 40 additions and 11 deletions
|
@ -51,18 +51,26 @@ QString imageFilePath() {
|
||||||
return PathUtils::getAppDataFilePath(IMAGE_FILE);
|
return PathUtils::getAppDataFilePath(IMAGE_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for now the callback function just returns the same string. Later we can hook
|
// use the cached _passphrase if it exists, otherwise we need to prompt
|
||||||
// this to the gui (some thought required)
|
|
||||||
int passwordCallback(char* password, int maxPasswordSize, int rwFlag, void* u) {
|
int passwordCallback(char* password, int maxPasswordSize, int rwFlag, void* u) {
|
||||||
// just return a hardcoded pwd for now
|
// just return a hardcoded pwd for now
|
||||||
static const char* pwd = "pwd";
|
auto passphrase = DependencyManager::get<Wallet>()->getPassphrase();
|
||||||
strcpy(password, pwd);
|
if (passphrase) {
|
||||||
return static_cast<int>(strlen(pwd));
|
strcpy(password, passphrase->toLocal8Bit().constData());
|
||||||
|
return static_cast<int>(passphrase->size());
|
||||||
|
} else {
|
||||||
|
// ok gotta bring up modal dialog... But right now lets just
|
||||||
|
// just keep it empty
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BEGIN copied code - this will be removed/changed at some point soon
|
// BEGIN copied code - this will be removed/changed at some point soon
|
||||||
// copied (without emits for various signals) from libraries/networking/src/RSAKeypairGenerator.cpp.
|
// copied (without emits for various signals) from libraries/networking/src/RSAKeypairGenerator.cpp.
|
||||||
// We will have a different implementation in practice, but this gives us a start for now
|
// We will have a different implementation in practice, but this gives us a start for now
|
||||||
|
//
|
||||||
|
// NOTE: we don't really use the private keys returned - we can see how this evolves, but probably
|
||||||
|
// we should just return a list of public keys?
|
||||||
QPair<QByteArray*, QByteArray*> generateRSAKeypair() {
|
QPair<QByteArray*, QByteArray*> generateRSAKeypair() {
|
||||||
|
|
||||||
RSA* keyPair = RSA_new();
|
RSA* keyPair = RSA_new();
|
||||||
|
@ -138,7 +146,9 @@ QPair<QByteArray*, QByteArray*> generateRSAKeypair() {
|
||||||
|
|
||||||
RSA_free(keyPair);
|
RSA_free(keyPair);
|
||||||
|
|
||||||
// prepare the return values
|
// prepare the return values. TODO: Fix this - we probably don't really even want the
|
||||||
|
// private key at all (better to read it when we need it?). Or maybe we do, when we have
|
||||||
|
// multiple keys?
|
||||||
retval.first = new QByteArray(reinterpret_cast<char*>(publicKeyDER), publicKeyLength ),
|
retval.first = new QByteArray(reinterpret_cast<char*>(publicKeyDER), publicKeyLength ),
|
||||||
retval.second = new QByteArray(reinterpret_cast<char*>(privateKeyDER), privateKeyLength );
|
retval.second = new QByteArray(reinterpret_cast<char*>(privateKeyDER), privateKeyLength );
|
||||||
|
|
||||||
|
@ -214,6 +224,13 @@ void initializeAESKeys(unsigned char* ivec, unsigned char* ckey, const QByteArra
|
||||||
memcpy(ckey, hash.data(), 16);
|
memcpy(ckey, hash.data(), 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Wallet::setPassphrase(const QString& passphrase) {
|
||||||
|
if (_passphrase) {
|
||||||
|
delete _passphrase;
|
||||||
|
}
|
||||||
|
_passphrase = new QString(passphrase);
|
||||||
|
}
|
||||||
|
|
||||||
// encrypt some stuff
|
// encrypt some stuff
|
||||||
bool Wallet::encryptFile(const QString& inputFilePath, const QString& outputFilePath) {
|
bool Wallet::encryptFile(const QString& inputFilePath, const QString& outputFilePath) {
|
||||||
// aes requires a couple 128-bit keys (ckey and ivec). For now, I'll just
|
// aes requires a couple 128-bit keys (ckey and ivec). For now, I'll just
|
||||||
|
@ -243,17 +260,20 @@ bool Wallet::encryptFile(const QString& inputFilePath, const QString& outputFile
|
||||||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
||||||
|
|
||||||
// TODO: add error handling!!!
|
// TODO: add error handling!!!
|
||||||
if (!EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, ckey, ivec)) {
|
if (!EVP_EncryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, ckey, ivec)) {
|
||||||
qCDebug(commerce) << "encrypt init failure";
|
qCDebug(commerce) << "encrypt init failure";
|
||||||
|
delete outputFileBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!EVP_EncryptUpdate(ctx, outputFileBuffer, &tempSize, (unsigned char*)inputFileBuffer.data(), inputFileBuffer.size())) {
|
if (!EVP_EncryptUpdate(ctx, outputFileBuffer, &tempSize, (unsigned char*)inputFileBuffer.data(), inputFileBuffer.size())) {
|
||||||
qCDebug(commerce) << "encrypt update failure";
|
qCDebug(commerce) << "encrypt update failure";
|
||||||
|
delete outputFileBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
outSize = tempSize;
|
outSize = tempSize;
|
||||||
if (!EVP_EncryptFinal_ex(ctx, outputFileBuffer + outSize, &tempSize)) {
|
if (!EVP_EncryptFinal_ex(ctx, outputFileBuffer + outSize, &tempSize)) {
|
||||||
qCDebug(commerce) << "encrypt final failure";
|
qCDebug(commerce) << "encrypt final failure";
|
||||||
|
delete outputFileBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,6 +286,7 @@ bool Wallet::encryptFile(const QString& inputFilePath, const QString& outputFile
|
||||||
outputFile.write(output);
|
outputFile.write(output);
|
||||||
outputFile.close();
|
outputFile.close();
|
||||||
|
|
||||||
|
delete outputFileBuffer;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,28 +311,30 @@ bool Wallet::decryptFile(const QString& inputFilePath, unsigned char** outputBuf
|
||||||
|
|
||||||
// TODO: add error handling
|
// TODO: add error handling
|
||||||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
|
||||||
if (!EVP_DecryptInit_ex(ctx, EVP_idea_cbc(), NULL, ckey, ivec)) {
|
if (!EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, ckey, ivec)) {
|
||||||
qCDebug(commerce) << "decrypt init failure";
|
qCDebug(commerce) << "decrypt init failure";
|
||||||
|
delete outputBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!EVP_DecryptUpdate(ctx, outputBuffer, &tempSize, (unsigned char*)encryptedBuffer.data(), encryptedBuffer.size())) {
|
if (!EVP_DecryptUpdate(ctx, outputBuffer, &tempSize, (unsigned char*)encryptedBuffer.data(), encryptedBuffer.size())) {
|
||||||
qCDebug(commerce) << "decrypt update failure";
|
qCDebug(commerce) << "decrypt update failure";
|
||||||
|
delete outputBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*outputBufferSize = tempSize;
|
*outputBufferSize = tempSize;
|
||||||
if (!EVP_DecryptFinal_ex(ctx, outputBuffer + tempSize, &tempSize)) {
|
if (!EVP_DecryptFinal_ex(ctx, outputBuffer + tempSize, &tempSize)) {
|
||||||
qCDebug(commerce) << "decrypt final failure";
|
qCDebug(commerce) << "decrypt final failure";
|
||||||
|
delete outputBuffer;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
*outputBufferSize += tempSize;
|
*outputBufferSize += tempSize;
|
||||||
*outputBufferPtr = outputBuffer;
|
*outputBufferPtr = outputBuffer;
|
||||||
qCDebug(commerce) << "decrypted buffer size" << *outputBufferSize;
|
qCDebug(commerce) << "decrypted buffer size" << *outputBufferSize;
|
||||||
|
delete outputBuffer;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool Wallet::createIfNeeded() {
|
bool Wallet::createIfNeeded() {
|
||||||
if (_publicKeys.count() > 0) return false;
|
if (_publicKeys.count() > 0) return false;
|
||||||
|
|
||||||
|
@ -348,6 +371,7 @@ bool Wallet::generateKeyPair() {
|
||||||
auto ledger = DependencyManager::get<Ledger>();
|
auto ledger = DependencyManager::get<Ledger>();
|
||||||
return ledger->receiveAt(key, oldKey);
|
return ledger->receiveAt(key, oldKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList Wallet::listPublicKeys() {
|
QStringList Wallet::listPublicKeys() {
|
||||||
qCInfo(commerce) << "Enumerating public keys.";
|
qCInfo(commerce) << "Enumerating public keys.";
|
||||||
createIfNeeded();
|
createIfNeeded();
|
||||||
|
@ -418,6 +442,7 @@ void Wallet::chooseSecurityImage(const QString& filename) {
|
||||||
emit securityImageResult(false);
|
emit securityImageResult(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wallet::getSecurityImage() {
|
void Wallet::getSecurityImage() {
|
||||||
unsigned char* data;
|
unsigned char* data;
|
||||||
int dataLen;
|
int dataLen;
|
||||||
|
|
|
@ -35,6 +35,9 @@ public:
|
||||||
void setSalt(const QByteArray& salt) { _salt = salt; }
|
void setSalt(const QByteArray& salt) { _salt = salt; }
|
||||||
QByteArray getSalt() { return _salt; }
|
QByteArray getSalt() { return _salt; }
|
||||||
|
|
||||||
|
void setPassphrase(const QString& passphrase);
|
||||||
|
QString* getPassphrase() { return _passphrase; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void securityImageResult(bool exists) ;
|
void securityImageResult(bool exists) ;
|
||||||
void keyFilePathResult(const QString& path);
|
void keyFilePathResult(const QString& path);
|
||||||
|
@ -54,6 +57,7 @@ private:
|
||||||
QStringList _publicKeys{};
|
QStringList _publicKeys{};
|
||||||
QPixmap* _securityImage { nullptr };
|
QPixmap* _securityImage { nullptr };
|
||||||
QByteArray _salt {"iamsalt!"};
|
QByteArray _salt {"iamsalt!"};
|
||||||
|
QString* _passphrase { new QString("pwd") };
|
||||||
|
|
||||||
bool encryptFile(const QString& inputFilePath, const QString& outputFilePath);
|
bool encryptFile(const QString& inputFilePath, const QString& outputFilePath);
|
||||||
bool decryptFile(const QString& inputFilePath, unsigned char** outputBufferPtr, int* outputBufferLen);
|
bool decryptFile(const QString& inputFilePath, unsigned char** outputBufferPtr, int* outputBufferLen);
|
||||||
|
|
|
@ -20,7 +20,7 @@ public:
|
||||||
|
|
||||||
ImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {}
|
ImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {}
|
||||||
|
|
||||||
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize);
|
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
|
||||||
|
|
||||||
void setSecurityImage(QPixmap* pixmap) { _securityImage = pixmap; }
|
void setSecurityImage(QPixmap* pixmap) { _securityImage = pixmap; }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue