2018-06-08 17:33:17 +03:00
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/engine.h>
|
2018-11-08 11:24:39 +03:00
|
|
|
#include <openssl/rand.h>
|
2018-06-08 17:33:17 +03:00
|
|
|
|
|
|
|
|
2017-09-11 16:00:01 +03:00
|
|
|
#include "clientsideencryption.h"
|
2017-09-11 17:52:57 +03:00
|
|
|
#include "account.h"
|
|
|
|
#include "capabilities.h"
|
2017-09-12 12:21:53 +03:00
|
|
|
#include "networkjobs.h"
|
2017-12-12 21:36:47 +03:00
|
|
|
#include "clientsideencryptionjobs.h"
|
2017-11-27 18:11:21 +03:00
|
|
|
#include "theme.h"
|
|
|
|
#include "creds/abstractcredentials.h"
|
2017-09-11 17:52:57 +03:00
|
|
|
|
2017-09-12 21:02:17 +03:00
|
|
|
#include <map>
|
2019-05-07 21:57:01 +03:00
|
|
|
#include <string>
|
2017-09-12 21:02:17 +03:00
|
|
|
|
2017-09-12 15:35:05 +03:00
|
|
|
#include <cstdio>
|
|
|
|
|
2017-09-11 17:52:57 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QLoggingCategory>
|
2017-09-12 12:21:53 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2017-09-14 16:57:41 +03:00
|
|
|
#include <QJsonObject>
|
2017-11-20 23:38:17 +03:00
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QXmlStreamNamespaceDeclaration>
|
|
|
|
#include <QStack>
|
2017-11-28 14:36:35 +03:00
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QLineEdit>
|
2017-12-14 18:54:56 +03:00
|
|
|
#include <QIODevice>
|
2018-03-30 20:28:00 +03:00
|
|
|
#include <QUuid>
|
2019-05-07 21:57:01 +03:00
|
|
|
#include <QScopeGuard>
|
2017-09-11 17:52:57 +03:00
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
#include <keychain.h>
|
2019-09-08 12:33:20 +03:00
|
|
|
#include "common/utility.h"
|
2017-11-27 18:11:21 +03:00
|
|
|
|
2017-09-14 19:39:18 +03:00
|
|
|
#include "wordlist.h"
|
|
|
|
|
2017-10-31 18:06:01 +03:00
|
|
|
QDebug operator<<(QDebug out, const std::string& str)
|
|
|
|
{
|
|
|
|
out << QString::fromStdString(str);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
using namespace QKeychain;
|
|
|
|
|
2017-09-11 17:52:57 +03:00
|
|
|
namespace OCC
|
|
|
|
{
|
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcCse, "nextcloud.sync.clientsideencryption", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcCseDecryption, "nextcloud.e2e", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcCseMetadata, "nextcloud.metadata", QtInfoMsg)
|
2017-09-11 17:52:57 +03:00
|
|
|
|
2017-10-18 21:21:35 +03:00
|
|
|
QString baseUrl(){
|
|
|
|
return QStringLiteral("ocs/v2.php/apps/end_to_end_encryption/api/v1/");
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:35:23 +03:00
|
|
|
namespace {
|
|
|
|
const char e2e_cert[] = "_e2e-certificate";
|
|
|
|
const char e2e_private[] = "_e2e-private";
|
|
|
|
const char e2e_mnemonic[] = "_e2e-mnemonic";
|
2020-08-10 13:45:32 +03:00
|
|
|
|
|
|
|
QList<QByteArray> oldCipherFormatSplit(const QByteArray &cipher)
|
|
|
|
{
|
|
|
|
const auto separator = QByteArrayLiteral("fA=="); // BASE64 encoded '|'
|
|
|
|
auto result = QList<QByteArray>();
|
|
|
|
|
|
|
|
auto data = cipher;
|
|
|
|
auto index = data.indexOf(separator);
|
|
|
|
while (index >=0) {
|
|
|
|
result.append(data.left(index));
|
|
|
|
data = data.mid(index + separator.size());
|
|
|
|
index = data.indexOf(separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append(data);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QByteArray> splitCipherParts(const QByteArray &data)
|
|
|
|
{
|
|
|
|
const auto isOldFormat = !data.contains('|');
|
|
|
|
const auto parts = isOldFormat ? oldCipherFormatSplit(data) : data.split('|');
|
|
|
|
qCInfo(lcCse()) << "found parts:" << parts << "old format?" << isOldFormat;
|
|
|
|
return parts;
|
|
|
|
}
|
2017-12-20 17:35:23 +03:00
|
|
|
} // ns
|
|
|
|
|
2017-09-14 23:45:12 +03:00
|
|
|
namespace {
|
2019-05-05 23:24:12 +03:00
|
|
|
unsigned char* unsignedData(QByteArray& array)
|
|
|
|
{
|
|
|
|
return (unsigned char*)array.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-05-07 21:57:01 +03:00
|
|
|
// Simple classes for safe (RAII) handling of OpenSSL
|
|
|
|
// data structures
|
2019-05-05 23:24:12 +03:00
|
|
|
//
|
2019-05-07 21:57:01 +03:00
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
class CipherCtx {
|
|
|
|
public:
|
|
|
|
CipherCtx()
|
2019-05-07 21:57:01 +03:00
|
|
|
: _ctx(EVP_CIPHER_CTX_new())
|
2019-05-05 23:24:12 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CipherCtx()
|
|
|
|
{
|
2019-05-07 21:57:01 +03:00
|
|
|
EVP_CIPHER_CTX_free(_ctx);
|
2019-05-05 23:24:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
operator EVP_CIPHER_CTX*()
|
|
|
|
{
|
2019-05-07 21:57:01 +03:00
|
|
|
return _ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(CipherCtx)
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX* _ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Bio {
|
|
|
|
public:
|
|
|
|
Bio()
|
|
|
|
: _bio(BIO_new(BIO_s_mem()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Bio()
|
|
|
|
{
|
|
|
|
BIO_free_all(_bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator BIO*()
|
|
|
|
{
|
|
|
|
return _bio;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(Bio)
|
|
|
|
|
|
|
|
BIO* _bio;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PKeyCtx {
|
|
|
|
public:
|
|
|
|
explicit PKeyCtx(int id, ENGINE *e = nullptr)
|
|
|
|
: _ctx(EVP_PKEY_CTX_new_id(id, e))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PKeyCtx()
|
|
|
|
{
|
|
|
|
EVP_PKEY_CTX_free(_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The move constructor is needed for pre-C++17 where
|
|
|
|
// return-value optimization (RVO) is not obligatory
|
|
|
|
// and we have a `forKey` static function that returns
|
|
|
|
// an instance of this class
|
|
|
|
PKeyCtx(PKeyCtx&& other)
|
|
|
|
{
|
|
|
|
std::swap(_ctx, other._ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
PKeyCtx& operator=(PKeyCtx&& other) = delete;
|
|
|
|
|
|
|
|
static PKeyCtx forKey(EVP_PKEY *pkey, ENGINE *e = nullptr)
|
|
|
|
{
|
|
|
|
PKeyCtx ctx;
|
|
|
|
ctx._ctx = EVP_PKEY_CTX_new(pkey, e);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator EVP_PKEY_CTX*()
|
|
|
|
{
|
|
|
|
return _ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(PKeyCtx)
|
|
|
|
|
2020-07-07 10:31:01 +03:00
|
|
|
PKeyCtx() = default;
|
2019-05-07 21:57:01 +03:00
|
|
|
|
2020-07-07 10:31:01 +03:00
|
|
|
EVP_PKEY_CTX* _ctx = nullptr;
|
2019-05-07 21:57:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class PKey {
|
|
|
|
public:
|
|
|
|
~PKey()
|
|
|
|
{
|
|
|
|
EVP_PKEY_free(_pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The move constructor is needed for pre-C++17 where
|
|
|
|
// return-value optimization (RVO) is not obligatory
|
|
|
|
// and we have a static functions that return
|
|
|
|
// an instance of this class
|
|
|
|
PKey(PKey&& other)
|
|
|
|
{
|
|
|
|
std::swap(_pkey, other._pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
PKey& operator=(PKey&& other) = delete;
|
|
|
|
|
|
|
|
static PKey readPublicKey(Bio &bio)
|
|
|
|
{
|
|
|
|
PKey result;
|
|
|
|
result._pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PKey readPrivateKey(Bio &bio)
|
|
|
|
{
|
|
|
|
PKey result;
|
|
|
|
result._pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PKey generate(PKeyCtx& ctx)
|
|
|
|
{
|
|
|
|
PKey result;
|
|
|
|
if (EVP_PKEY_keygen(ctx, &result._pkey) <= 0) {
|
|
|
|
result._pkey = nullptr;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator EVP_PKEY*()
|
|
|
|
{
|
|
|
|
return _pkey;
|
2019-05-05 23:24:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-07 21:57:01 +03:00
|
|
|
Q_DISABLE_COPY(PKey)
|
|
|
|
|
2020-07-07 10:31:01 +03:00
|
|
|
PKey() = default;
|
2019-05-05 23:24:12 +03:00
|
|
|
|
2020-07-07 10:31:01 +03:00
|
|
|
EVP_PKEY* _pkey = nullptr;
|
2019-05-05 23:24:12 +03:00
|
|
|
};
|
2019-05-07 21:57:01 +03:00
|
|
|
|
|
|
|
QByteArray BIO2ByteArray(Bio &b) {
|
|
|
|
int pending = BIO_ctrl_pending(b);
|
|
|
|
QByteArray res(pending, '\0');
|
|
|
|
BIO_read(b, unsignedData(res), pending);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-29 16:07:05 +03:00
|
|
|
QByteArray handleErrors()
|
2019-05-07 21:57:01 +03:00
|
|
|
{
|
|
|
|
Bio bioErrors;
|
|
|
|
ERR_print_errors(bioErrors); // This line is not printing anything.
|
|
|
|
return BIO2ByteArray(bioErrors);
|
|
|
|
}
|
2017-09-14 23:45:12 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
namespace EncryptionHelper {
|
2019-05-05 23:24:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray generateRandomFilename()
|
2017-12-21 01:09:28 +03:00
|
|
|
{
|
2018-03-30 20:28:00 +03:00
|
|
|
return QUuid::createUuid().toRfc4122().toHex();
|
2017-12-21 01:09:28 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray generateRandom(int size)
|
2017-12-21 01:09:28 +03:00
|
|
|
{
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray result(size, '\0');
|
2017-11-25 00:10:28 +03:00
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
int ret = RAND_bytes(unsignedData(result), size);
|
2017-11-25 00:10:28 +03:00
|
|
|
if (ret != 1) {
|
|
|
|
qCInfo(lcCse()) << "Random byte generation failed!";
|
|
|
|
// Error out?
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray generatePassword(const QString& wordlist, const QByteArray& salt) {
|
2017-11-24 22:58:20 +03:00
|
|
|
qCInfo(lcCse()) << "Start encryption key generation!";
|
|
|
|
|
|
|
|
const int iterationCount = 1024;
|
|
|
|
const int keyStrength = 256;
|
|
|
|
const int keyLength = keyStrength/8;
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
QByteArray secretKey(keyLength, '\0');
|
2017-11-24 22:58:20 +03:00
|
|
|
|
|
|
|
int ret = PKCS5_PBKDF2_HMAC_SHA1(
|
|
|
|
wordlist.toLocal8Bit().constData(), // const char *password,
|
|
|
|
wordlist.size(), // int password length,
|
2019-05-07 21:57:01 +03:00
|
|
|
(const unsigned char *)salt.constData(),// const unsigned char *salt,
|
2017-11-24 22:58:20 +03:00
|
|
|
salt.size(), // int saltlen,
|
|
|
|
iterationCount, // int iterations,
|
|
|
|
keyLength, // int keylen,
|
2019-05-07 21:57:01 +03:00
|
|
|
unsignedData(secretKey) // unsigned char *out
|
2017-11-24 22:58:20 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (ret != 1) {
|
|
|
|
qCInfo(lcCse()) << "Failed to generate encryption key";
|
|
|
|
// Error out?
|
|
|
|
}
|
|
|
|
|
|
|
|
qCInfo(lcCse()) << "Encryption key generated!";
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
return secretKey;
|
2017-11-25 00:10:28 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray encryptPrivateKey(
|
2017-11-27 17:21:29 +03:00
|
|
|
const QByteArray& key,
|
2017-12-20 18:07:33 +03:00
|
|
|
const QByteArray& privateKey,
|
|
|
|
const QByteArray& salt
|
2017-11-25 00:10:28 +03:00
|
|
|
) {
|
|
|
|
|
|
|
|
QByteArray iv = generateRandom(12);
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2020-05-29 16:07:05 +03:00
|
|
|
|
2017-11-25 00:10:28 +03:00
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error creating cipher";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr)) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error initializing context with aes_256";
|
|
|
|
handleErrors();
|
2017-11-24 22:58:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-25 00:10:28 +03:00
|
|
|
// No padding
|
|
|
|
EVP_CIPHER_CTX_set_padding(ctx, 0);
|
|
|
|
|
|
|
|
/* Set IV length. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error setting iv length";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, nullptr, nullptr, (unsigned char *)key.constData(), (unsigned char *)iv.constData())) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising key and iv";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We write the base64 encoded private key
|
|
|
|
QByteArray privateKeyB64 = privateKey.toBase64();
|
|
|
|
|
|
|
|
// Make sure we have enough room in the cipher text
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray ctext(privateKeyB64.size() + 32, '\0');
|
2017-11-25 00:10:28 +03:00
|
|
|
|
|
|
|
// Do the actual encryption
|
|
|
|
int len = 0;
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_EncryptUpdate(ctx, unsignedData(ctext), &len, (unsigned char *)privateKeyB64.constData(), privateKeyB64.size())) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error encrypting";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
int clen = len;
|
|
|
|
|
|
|
|
/* Finalise the encryption. Normally ciphertext bytes may be written at
|
|
|
|
* this stage, but this does not occur in GCM mode
|
|
|
|
*/
|
2019-05-05 23:24:12 +03:00
|
|
|
if(1 != EVP_EncryptFinal_ex(ctx, unsignedData(ctext) + len, &len)) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error finalizing encryption";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
clen += len;
|
|
|
|
|
|
|
|
/* Get the tag */
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray tag(16, '\0');
|
|
|
|
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, unsignedData(tag))) {
|
2017-11-25 00:10:28 +03:00
|
|
|
qCInfo(lcCse()) << "Error getting the tag";
|
|
|
|
handleErrors();
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray cipherTXT;
|
|
|
|
cipherTXT.reserve(clen + 16);
|
|
|
|
cipherTXT.append(ctext, clen);
|
|
|
|
cipherTXT.append(tag);
|
2017-11-25 00:10:28 +03:00
|
|
|
|
|
|
|
QByteArray result = cipherTXT.toBase64();
|
2020-07-09 10:54:39 +03:00
|
|
|
result += '|';
|
2017-11-25 00:10:28 +03:00
|
|
|
result += iv.toBase64();
|
2020-07-09 10:54:39 +03:00
|
|
|
result += '|';
|
2017-12-20 18:07:33 +03:00
|
|
|
result += salt.toBase64();
|
2017-11-25 00:10:28 +03:00
|
|
|
|
2017-11-24 22:58:20 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray decryptPrivateKey(const QByteArray& key, const QByteArray& data) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric key: " << key;
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric data: " << data;
|
|
|
|
|
2020-08-10 13:45:32 +03:00
|
|
|
const auto parts = splitCipherParts(data);
|
2020-08-10 12:19:20 +03:00
|
|
|
if (parts.size() < 2) {
|
|
|
|
qCInfo(lcCse()) << "Not enough parts found";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray cipherTXT64 = parts.at(0);
|
|
|
|
QByteArray ivB64 = parts.at(1);
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric cipherTXT: " << cipherTXT64;
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric IV: " << ivB64;
|
|
|
|
|
|
|
|
QByteArray cipherTXT = QByteArray::fromBase64(cipherTXT64);
|
|
|
|
QByteArray iv = QByteArray::fromBase64(ivB64);
|
|
|
|
|
|
|
|
QByteArray tag = cipherTXT.right(16);
|
|
|
|
cipherTXT.chop(16);
|
|
|
|
|
|
|
|
// Init
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error creating cipher";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr)) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising context with aes 256";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set IV length. Not necessary if this is 12 bytes (96 bits) */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error setting IV size";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, nullptr, nullptr, (unsigned char *)key.constData(), (unsigned char *)iv.constData())) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising key and iv";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray ptext(cipherTXT.size() + 16, '\0');
|
2020-05-29 16:07:05 +03:00
|
|
|
int plen = 0;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
/* Provide the message to be decrypted, and obtain the plaintext output.
|
|
|
|
* EVP_DecryptUpdate can be called multiple times if necessary
|
|
|
|
*/
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_DecryptUpdate(ctx, unsignedData(ptext), &plen, (unsigned char *)cipherTXT.constData(), cipherTXT.size())) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Could not decrypt";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set expected tag value. Works in OpenSSL 1.0.1d and later */
|
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), (unsigned char *)tag.constData())) {
|
|
|
|
qCInfo(lcCse()) << "Could not set tag";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finalise the decryption. A positive return value indicates success,
|
|
|
|
* anything else is a failure - the plaintext is not trustworthy.
|
|
|
|
*/
|
|
|
|
int len = plen;
|
2019-05-05 23:24:12 +03:00
|
|
|
if (EVP_DecryptFinal_ex(ctx, unsignedData(ptext) + plen, &len) == 0) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Tag did not match!";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray result(ptext, plen);
|
2017-11-28 14:36:35 +03:00
|
|
|
return QByteArray::fromBase64(result);
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:26:52 +03:00
|
|
|
QByteArray extractPrivateKeySalt(const QByteArray &data)
|
|
|
|
{
|
2020-08-10 13:45:32 +03:00
|
|
|
const auto parts = splitCipherParts(data);
|
2020-08-10 12:26:52 +03:00
|
|
|
if (parts.size() < 3) {
|
|
|
|
qCInfo(lcCse()) << "Not enough parts found";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QByteArray::fromBase64(parts.at(2));
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray decryptStringSymmetric(const QByteArray& key, const QByteArray& data) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric key: " << key;
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric data: " << data;
|
|
|
|
|
2020-08-10 13:45:32 +03:00
|
|
|
const auto parts = splitCipherParts(data);
|
|
|
|
if (parts.size() < 2) {
|
|
|
|
qCInfo(lcCse()) << "Not enough parts found";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
2017-11-28 14:36:35 +03:00
|
|
|
|
2020-08-10 13:45:32 +03:00
|
|
|
QByteArray cipherTXT64 = parts.at(0);
|
|
|
|
QByteArray ivB64 = parts.at(1);
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric cipherTXT: " << cipherTXT64;
|
|
|
|
qCInfo(lcCse()) << "decryptStringSymmetric IV: " << ivB64;
|
|
|
|
|
|
|
|
QByteArray cipherTXT = QByteArray::fromBase64(cipherTXT64);
|
|
|
|
QByteArray iv = QByteArray::fromBase64(ivB64);
|
|
|
|
|
|
|
|
QByteArray tag = cipherTXT.right(16);
|
|
|
|
cipherTXT.chop(16);
|
|
|
|
|
|
|
|
// Init
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error creating cipher";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
2017-11-25 23:43:15 +03:00
|
|
|
|
2017-11-28 14:36:35 +03:00
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising context with aes 128";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set IV length. Not necessary if this is 12 bytes (96 bits) */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error setting IV size";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, nullptr, nullptr, (unsigned char *)key.constData(), (unsigned char *)iv.constData())) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising key and iv";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray ptext(cipherTXT.size() + 16, '\0');
|
2020-05-29 16:07:05 +03:00
|
|
|
int plen = 0;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
/* Provide the message to be decrypted, and obtain the plaintext output.
|
|
|
|
* EVP_DecryptUpdate can be called multiple times if necessary
|
|
|
|
*/
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_DecryptUpdate(ctx, unsignedData(ptext), &plen, (unsigned char *)cipherTXT.constData(), cipherTXT.size())) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Could not decrypt";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set expected tag value. Works in OpenSSL 1.0.1d and later */
|
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), (unsigned char *)tag.constData())) {
|
|
|
|
qCInfo(lcCse()) << "Could not set tag";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finalise the decryption. A positive return value indicates success,
|
|
|
|
* anything else is a failure - the plaintext is not trustworthy.
|
|
|
|
*/
|
|
|
|
int len = plen;
|
2019-05-05 23:24:12 +03:00
|
|
|
if (EVP_DecryptFinal_ex(ctx, unsignedData(ptext) + plen, &len) == 0) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Tag did not match!";
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:08:15 +03:00
|
|
|
return QByteArray::fromBase64(QByteArray(ptext, plen));
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
2018-10-15 01:21:08 +03:00
|
|
|
QByteArray privateKeyToPem(const QByteArray key) {
|
2019-05-07 21:57:01 +03:00
|
|
|
Bio privateKeyBio;
|
2018-10-15 01:21:08 +03:00
|
|
|
BIO_write(privateKeyBio, key.constData(), key.size());
|
2019-05-07 21:57:01 +03:00
|
|
|
auto pkey = PKey::readPrivateKey(privateKeyBio);
|
2018-01-23 23:02:52 +03:00
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
Bio pemBio;
|
2018-11-12 20:46:39 +03:00
|
|
|
PEM_write_bio_PKCS8PrivateKey(pemBio, pkey, nullptr, nullptr, 0, nullptr, nullptr);
|
2018-01-23 23:02:52 +03:00
|
|
|
QByteArray pem = BIO2ByteArray(pemBio);
|
|
|
|
|
|
|
|
return pem;
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray encryptStringSymmetric(const QByteArray& key, const QByteArray& data) {
|
2017-11-25 23:43:15 +03:00
|
|
|
QByteArray iv = generateRandom(16);
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2020-05-29 16:07:05 +03:00
|
|
|
|
2017-11-25 23:43:15 +03:00
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error creating cipher";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Error initializing context with aes_128";
|
2017-11-25 23:43:15 +03:00
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// No padding
|
|
|
|
EVP_CIPHER_CTX_set_padding(ctx, 0);
|
|
|
|
|
|
|
|
/* Set IV length. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error setting iv length";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, nullptr, nullptr, (unsigned char *)key.constData(), (unsigned char *)iv.constData())) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error initialising key and iv";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We write the data base64 encoded
|
|
|
|
QByteArray dataB64 = data.toBase64();
|
|
|
|
|
|
|
|
// Make sure we have enough room in the cipher text
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray ctext(dataB64.size() + 16, '\0');
|
2017-11-25 23:43:15 +03:00
|
|
|
|
|
|
|
// Do the actual encryption
|
|
|
|
int len = 0;
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_EncryptUpdate(ctx, unsignedData(ctext), &len, (unsigned char *)dataB64.constData(), dataB64.size())) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error encrypting";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int clen = len;
|
|
|
|
|
|
|
|
/* Finalise the encryption. Normally ciphertext bytes may be written at
|
|
|
|
* this stage, but this does not occur in GCM mode
|
|
|
|
*/
|
2019-05-05 23:24:12 +03:00
|
|
|
if(1 != EVP_EncryptFinal_ex(ctx, unsignedData(ctext) + len, &len)) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error finalizing encryption";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
clen += len;
|
|
|
|
|
|
|
|
/* Get the tag */
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray tag(16, '\0');
|
|
|
|
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, unsignedData(tag))) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error getting the tag";
|
|
|
|
handleErrors();
|
2018-03-25 23:31:49 +03:00
|
|
|
return {};
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray cipherTXT;
|
|
|
|
cipherTXT.reserve(clen + 16);
|
|
|
|
cipherTXT.append(ctext, clen);
|
|
|
|
cipherTXT.append(tag);
|
2017-11-25 23:43:15 +03:00
|
|
|
|
|
|
|
QByteArray result = cipherTXT.toBase64();
|
2020-07-09 10:54:39 +03:00
|
|
|
result += '|';
|
2017-11-25 23:43:15 +03:00
|
|
|
result += iv.toBase64();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray decryptStringAsymmetric(EVP_PKEY *privateKey, const QByteArray& data) {
|
2017-12-12 23:23:02 +03:00
|
|
|
int err = -1;
|
|
|
|
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Start to work the decryption.";
|
2019-05-07 21:57:01 +03:00
|
|
|
auto ctx = PKeyCtx::forKey(privateKey, ENGINE_get_default_RSA());
|
2017-12-12 23:23:02 +03:00
|
|
|
if (!ctx) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Could not create the PKEY context.";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = EVP_PKEY_decrypt_init(ctx);
|
|
|
|
if (err <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Could not init the decryption of the metadata";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Error setting the encryption padding.";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256()) <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Error setting OAEP SHA 256";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Error setting MGF1 padding";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t outlen = 0;
|
2018-11-12 20:46:39 +03:00
|
|
|
err = EVP_PKEY_decrypt(ctx, nullptr, &outlen, (unsigned char *)data.constData(), data.size());
|
2017-12-12 23:23:02 +03:00
|
|
|
if (err <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Could not determine the buffer length";
|
|
|
|
handleErrors();
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:23:02 +03:00
|
|
|
} else {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Size of output is: " << outlen;
|
|
|
|
qCInfo(lcCseDecryption()) << "Size of data is: " << data.size();
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
QByteArray out(outlen, '\0');
|
2017-12-12 23:23:02 +03:00
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
if (EVP_PKEY_decrypt(ctx, unsignedData(out), &outlen, (unsigned char *)data.constData(), data.size()) <= 0) {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "Could not decrypt the data.";
|
|
|
|
ERR_print_errors_fp(stdout); // This line is not printing anything.
|
2018-03-26 23:02:46 +03:00
|
|
|
return {};
|
2017-12-12 23:29:06 +03:00
|
|
|
} else {
|
2017-12-12 23:50:30 +03:00
|
|
|
qCInfo(lcCseDecryption()) << "data decrypted successfully";
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
qCInfo(lcCse()) << out;
|
|
|
|
return out;
|
2017-12-12 23:23:02 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
QByteArray encryptStringAsymmetric(EVP_PKEY *publicKey, const QByteArray& data) {
|
2017-11-25 23:43:15 +03:00
|
|
|
int err = -1;
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
auto ctx = PKeyCtx::forKey(publicKey, ENGINE_get_default_RSA());
|
2017-11-25 23:43:15 +03:00
|
|
|
if (!ctx) {
|
|
|
|
qCInfo(lcCse()) << "Could not initialize the pkey context.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_encrypt_init(ctx) != 1) {
|
|
|
|
qCInfo(lcCse()) << "Error initilaizing the encryption.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) {
|
|
|
|
qCInfo(lcCse()) << "Error setting the encryption padding.";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256()) <= 0) {
|
|
|
|
qCInfo(lcCse()) << "Error setting OAEP SHA 256";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) <= 0) {
|
|
|
|
qCInfo(lcCse()) << "Error setting MGF1 padding";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t outLen = 0;
|
2018-11-12 20:46:39 +03:00
|
|
|
if (EVP_PKEY_encrypt(ctx, nullptr, &outLen, (unsigned char *)data.constData(), data.size()) != 1) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Error retrieving the size of the encrypted data";
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2020-05-03 23:54:50 +03:00
|
|
|
qCInfo(lcCse()) << "Encryption Length:" << outLen;
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
QByteArray out(outLen, '\0');
|
|
|
|
if (EVP_PKEY_encrypt(ctx, unsignedData(out), &outLen, (unsigned char *)data.constData(), data.size()) != 1) {
|
2017-11-25 23:43:15 +03:00
|
|
|
qCInfo(lcCse()) << "Could not encrypt key." << err;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the encrypted data into base64.
|
2019-05-07 21:57:01 +03:00
|
|
|
qCInfo(lcCse()) << out.toBase64();
|
|
|
|
return out.toBase64();
|
2017-11-25 23:43:15 +03:00
|
|
|
}
|
|
|
|
|
2018-02-18 03:23:59 +03:00
|
|
|
}
|
2020-05-25 22:33:24 +03:00
|
|
|
ClientSideEncryption::ClientSideEncryption() = default;
|
2017-09-11 17:52:57 +03:00
|
|
|
|
2017-09-12 12:21:53 +03:00
|
|
|
void ClientSideEncryption::setAccount(AccountPtr account)
|
2017-09-11 17:52:57 +03:00
|
|
|
{
|
2017-09-12 12:21:53 +03:00
|
|
|
_account = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::initialize()
|
|
|
|
{
|
|
|
|
qCInfo(lcCse()) << "Initializing";
|
2020-06-17 14:31:30 +03:00
|
|
|
if (!_account->capabilities().clientSideEncryptionAvailable()) {
|
2020-05-03 23:54:50 +03:00
|
|
|
qCInfo(lcCse()) << "No Client side encryption available on server.";
|
2017-09-11 17:52:57 +03:00
|
|
|
emit initializationFinished();
|
2018-03-26 21:51:14 +03:00
|
|
|
return;
|
2017-09-11 17:52:57 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
fetchFromKeyChain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::fetchFromKeyChain() {
|
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_cert,
|
2017-11-27 18:11:21 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new ReadPasswordJob(Theme::instance()->appName());
|
2017-11-27 18:11:21 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
|
|
|
connect(job, &ReadPasswordJob::finished, this, &ClientSideEncryption::publicKeyFetched);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::publicKeyFetched(Job *incoming) {
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *readJob = static_cast<ReadPasswordJob *>(incoming);
|
2017-11-27 18:11:21 +03:00
|
|
|
|
|
|
|
// Error or no valid public key error out
|
|
|
|
if (readJob->error() != NoError || readJob->binaryData().length() == 0) {
|
|
|
|
getPublicKeyFromServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_certificate = QSslCertificate(readJob->binaryData(), QSsl::Pem);
|
|
|
|
|
|
|
|
if (_certificate.isNull()) {
|
|
|
|
getPublicKeyFromServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-28 14:36:35 +03:00
|
|
|
_publicKey = _certificate.publicKey();
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
qCInfo(lcCse()) << "Public key fetched from keychain";
|
|
|
|
|
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_private,
|
2017-11-27 18:11:21 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new ReadPasswordJob(Theme::instance()->appName());
|
2017-11-27 18:11:21 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
|
|
|
connect(job, &ReadPasswordJob::finished, this, &ClientSideEncryption::privateKeyFetched);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2017-12-15 16:00:42 +03:00
|
|
|
void ClientSideEncryption::setFolderEncryptedStatus(const QString& folder, bool status)
|
|
|
|
{
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Setting folder" << folder << "as encrypted" << status;
|
2017-12-15 16:00:42 +03:00
|
|
|
_folder2encryptedStatus[folder] = status;
|
|
|
|
}
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
void ClientSideEncryption::privateKeyFetched(Job *incoming) {
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *readJob = static_cast<ReadPasswordJob *>(incoming);
|
2017-11-27 18:11:21 +03:00
|
|
|
|
|
|
|
// Error or no valid public key error out
|
|
|
|
if (readJob->error() != NoError || readJob->binaryData().length() == 0) {
|
|
|
|
_certificate = QSslCertificate();
|
2017-11-28 14:36:35 +03:00
|
|
|
_publicKey = QSslKey();
|
2017-11-27 18:11:21 +03:00
|
|
|
getPublicKeyFromServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-15 01:21:08 +03:00
|
|
|
//_privateKey = QSslKey(readJob->binaryData(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
|
|
|
_privateKey = readJob->binaryData();
|
2017-11-27 18:11:21 +03:00
|
|
|
|
|
|
|
if (_privateKey.isNull()) {
|
|
|
|
getPrivateKeyFromServer();
|
|
|
|
return;
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
qCInfo(lcCse()) << "Private key fetched from keychain";
|
|
|
|
|
2017-11-28 14:36:35 +03:00
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_mnemonic,
|
2017-11-28 14:36:35 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new ReadPasswordJob(Theme::instance()->appName());
|
2017-11-28 14:36:35 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
|
|
|
connect(job, &ReadPasswordJob::finished, this, &ClientSideEncryption::mnemonicKeyFetched);
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::mnemonicKeyFetched(QKeychain::Job *incoming) {
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *readJob = static_cast<ReadPasswordJob *>(incoming);
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
// Error or no valid public key error out
|
|
|
|
if (readJob->error() != NoError || readJob->textData().length() == 0) {
|
|
|
|
_certificate = QSslCertificate();
|
|
|
|
_publicKey = QSslKey();
|
2018-10-15 01:21:08 +03:00
|
|
|
_privateKey = QByteArray();
|
2017-11-28 14:36:35 +03:00
|
|
|
getPublicKeyFromServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mnemonic = readJob->textData();
|
|
|
|
|
2018-01-23 23:02:52 +03:00
|
|
|
qCInfo(lcCse()) << "Mnemonic key fetched from keychain: " << _mnemonic;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
emit initializationFinished();
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
2017-11-28 14:36:35 +03:00
|
|
|
void ClientSideEncryption::writePrivateKey() {
|
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_private,
|
2017-11-28 14:36:35 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new WritePasswordJob(Theme::instance()->appName());
|
2017-11-28 14:36:35 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
2018-10-15 01:21:08 +03:00
|
|
|
job->setBinaryData(_privateKey);
|
2017-11-28 14:36:35 +03:00
|
|
|
connect(job, &WritePasswordJob::finished, [this](Job *incoming) {
|
|
|
|
Q_UNUSED(incoming);
|
|
|
|
qCInfo(lcCse()) << "Private key stored in keychain";
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::writeCertificate() {
|
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_cert,
|
2017-11-28 14:36:35 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new WritePasswordJob(Theme::instance()->appName());
|
2017-11-28 14:36:35 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
|
|
|
job->setBinaryData(_certificate.toPem());
|
|
|
|
connect(job, &WritePasswordJob::finished, [this](Job *incoming) {
|
|
|
|
Q_UNUSED(incoming);
|
|
|
|
qCInfo(lcCse()) << "Certificate stored in keychain";
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::writeMnemonic() {
|
|
|
|
const QString kck = AbstractCredentials::keychainKey(
|
|
|
|
_account->url().toString(),
|
2017-12-20 17:35:23 +03:00
|
|
|
_account->credentials()->user() + e2e_mnemonic,
|
2017-11-28 14:36:35 +03:00
|
|
|
_account->id()
|
|
|
|
);
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new WritePasswordJob(Theme::instance()->appName());
|
2017-11-28 14:36:35 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(kck);
|
|
|
|
job->setTextData(_mnemonic);
|
|
|
|
connect(job, &WritePasswordJob::finished, [this](Job *incoming) {
|
|
|
|
Q_UNUSED(incoming);
|
|
|
|
qCInfo(lcCse()) << "Mnemonic stored in keychain";
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:35:23 +03:00
|
|
|
void ClientSideEncryption::forgetSensitiveData()
|
|
|
|
{
|
2018-10-15 01:21:08 +03:00
|
|
|
_privateKey = QByteArray();
|
2017-12-20 17:35:23 +03:00
|
|
|
_certificate = QSslCertificate();
|
|
|
|
_publicKey = QSslKey();
|
|
|
|
_mnemonic = QString();
|
|
|
|
|
|
|
|
auto startDeleteJob = [this](QString user) {
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new DeletePasswordJob(Theme::instance()->appName());
|
2017-12-20 17:35:23 +03:00
|
|
|
job->setInsecureFallback(false);
|
|
|
|
job->setKey(AbstractCredentials::keychainKey(_account->url().toString(), user, _account->id()));
|
|
|
|
job->start();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto user = _account->credentials()->user();
|
|
|
|
startDeleteJob(user + e2e_private);
|
|
|
|
startDeleteJob(user + e2e_cert);
|
|
|
|
startDeleteJob(user + e2e_mnemonic);
|
|
|
|
}
|
|
|
|
|
2018-06-21 18:20:09 +03:00
|
|
|
void ClientSideEncryption::slotRequestMnemonic() {
|
|
|
|
emit showMnemonic(_mnemonic);
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:21:53 +03:00
|
|
|
bool ClientSideEncryption::hasPrivateKey() const
|
|
|
|
{
|
2017-12-20 15:41:13 +03:00
|
|
|
return !_privateKey.isNull();
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientSideEncryption::hasPublicKey() const
|
|
|
|
{
|
2017-12-20 15:41:13 +03:00
|
|
|
return !_publicKey.isNull();
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::generateKeyPair()
|
|
|
|
{
|
2017-09-12 14:44:42 +03:00
|
|
|
// AES/GCM/NoPadding,
|
|
|
|
// metadataKeys with RSA/ECB/OAEPWithSHA-256AndMGF1Padding
|
|
|
|
qCInfo(lcCse()) << "No public key, generating a pair.";
|
|
|
|
const int rsaKeyLen = 2048;
|
|
|
|
|
|
|
|
|
|
|
|
// Init RSA
|
2019-05-07 21:57:01 +03:00
|
|
|
PKeyCtx ctx(EVP_PKEY_RSA);
|
2017-09-12 14:44:42 +03:00
|
|
|
|
|
|
|
if(EVP_PKEY_keygen_init(ctx) <= 0) {
|
|
|
|
qCInfo(lcCse()) << "Couldn't initialize the key generator";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, rsaKeyLen) <= 0) {
|
|
|
|
qCInfo(lcCse()) << "Couldn't initialize the key generator bits";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
auto localKeyPair = PKey::generate(ctx);
|
|
|
|
if(!localKeyPair) {
|
2017-09-12 14:44:42 +03:00
|
|
|
qCInfo(lcCse()) << "Could not generate the key";
|
|
|
|
return;
|
|
|
|
}
|
2019-05-07 21:57:01 +03:00
|
|
|
|
2017-09-12 14:44:42 +03:00
|
|
|
qCInfo(lcCse()) << "Key correctly generated";
|
2017-09-12 15:35:05 +03:00
|
|
|
qCInfo(lcCse()) << "Storing keys locally";
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
Bio privKey;
|
2018-11-12 20:46:39 +03:00
|
|
|
if (PEM_write_bio_PrivateKey(privKey, localKeyPair, nullptr, nullptr, 0, nullptr, nullptr) <= 0) {
|
2017-11-27 18:11:21 +03:00
|
|
|
qCInfo(lcCse()) << "Could not read private key from bio.";
|
2017-09-12 15:35:05 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-02-18 03:04:44 +03:00
|
|
|
QByteArray key = BIO2ByteArray(privKey);
|
2018-10-15 01:21:08 +03:00
|
|
|
//_privateKey = QSslKey(key, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
|
|
|
_privateKey = key;
|
2017-09-12 12:21:53 +03:00
|
|
|
|
2017-09-12 14:44:42 +03:00
|
|
|
qCInfo(lcCse()) << "Keys generated correctly, sending to server.";
|
2017-11-28 14:36:35 +03:00
|
|
|
generateCSR(localKeyPair);
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
2017-11-12 15:03:52 +03:00
|
|
|
void ClientSideEncryption::generateCSR(EVP_PKEY *keyPair)
|
2017-09-12 12:21:53 +03:00
|
|
|
{
|
2017-09-12 21:02:17 +03:00
|
|
|
// OpenSSL expects const char.
|
2017-11-06 22:57:50 +03:00
|
|
|
auto cnArray = _account->davUser().toLocal8Bit();
|
|
|
|
qCInfo(lcCse()) << "Getting the following array for the account Id" << cnArray;
|
|
|
|
|
2017-09-12 21:02:17 +03:00
|
|
|
auto certParams = std::map<const char *, const char*>{
|
|
|
|
{"C", "DE"},
|
|
|
|
{"ST", "Baden-Wuerttemberg"},
|
|
|
|
{"L", "Stuttgart"},
|
|
|
|
{"O","Nextcloud"},
|
2017-11-06 22:57:50 +03:00
|
|
|
{"CN", cnArray.constData()}
|
2017-09-12 21:02:17 +03:00
|
|
|
};
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
int ret = 0;
|
|
|
|
int nVersion = 1;
|
2017-09-12 21:02:17 +03:00
|
|
|
|
|
|
|
// 2. set version of x509 req
|
2019-05-07 21:57:01 +03:00
|
|
|
X509_REQ *x509_req = X509_REQ_new();
|
|
|
|
auto release_on_exit_x509_req = qScopeGuard([&] {
|
|
|
|
X509_REQ_free(x509_req);
|
|
|
|
});
|
|
|
|
|
2017-09-12 21:02:17 +03:00
|
|
|
ret = X509_REQ_set_version(x509_req, nVersion);
|
|
|
|
|
|
|
|
// 3. set subject of x509 req
|
|
|
|
auto x509_name = X509_REQ_get_subject_name(x509_req);
|
|
|
|
|
|
|
|
for(const auto& v : certParams) {
|
2019-05-07 21:57:01 +03:00
|
|
|
ret = X509_NAME_add_entry_by_txt(x509_name, v.first, MBSTRING_ASC, (const unsigned char*) v.second, -1, -1, 0);
|
2017-09-12 21:02:17 +03:00
|
|
|
if (ret != 1) {
|
|
|
|
qCInfo(lcCse()) << "Error Generating the Certificate while adding" << v.first << v.second;
|
2017-11-27 18:11:21 +03:00
|
|
|
return;
|
2017-09-12 21:02:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = X509_REQ_set_pubkey(x509_req, keyPair);
|
|
|
|
if (ret != 1){
|
|
|
|
qCInfo(lcCse()) << "Error setting the public key on the csr";
|
2017-11-27 18:11:21 +03:00
|
|
|
return;
|
2017-09-12 21:02:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = X509_REQ_sign(x509_req, keyPair, EVP_sha1()); // return x509_req->signature->length
|
|
|
|
if (ret <= 0){
|
|
|
|
qCInfo(lcCse()) << "Error setting the public key on the csr";
|
2017-11-27 18:11:21 +03:00
|
|
|
return;
|
2017-09-12 21:02:17 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
Bio out;
|
2017-09-12 21:02:17 +03:00
|
|
|
ret = PEM_write_bio_X509_REQ(out, x509_req);
|
2018-02-18 03:04:44 +03:00
|
|
|
QByteArray output = BIO2ByteArray(out);
|
2017-09-12 21:02:17 +03:00
|
|
|
|
2017-09-12 22:52:10 +03:00
|
|
|
qCInfo(lcCse()) << "Returning the certificate";
|
|
|
|
qCInfo(lcCse()) << output;
|
|
|
|
|
2019-05-07 21:57:01 +03:00
|
|
|
auto job = new SignPublicKeyApiJob(_account, baseUrl() + "public-key", this);
|
2017-09-13 20:57:39 +03:00
|
|
|
job->setCsr(output);
|
2017-09-14 19:39:18 +03:00
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
connect(job, &SignPublicKeyApiJob::jsonReceived, [this](const QJsonDocument& json, int retCode) {
|
2017-09-14 16:57:41 +03:00
|
|
|
if (retCode == 200) {
|
2017-11-27 18:11:21 +03:00
|
|
|
QString cert = json.object().value("ocs").toObject().value("data").toObject().value("public-key").toString();
|
|
|
|
_certificate = QSslCertificate(cert.toLocal8Bit(), QSsl::Pem);
|
2017-12-21 12:20:03 +03:00
|
|
|
_publicKey = _certificate.publicKey();
|
2017-11-27 18:11:21 +03:00
|
|
|
qCInfo(lcCse()) << "Certificate saved, Encrypting Private Key.";
|
|
|
|
encryptPrivateKey();
|
2017-09-14 16:57:41 +03:00
|
|
|
}
|
2017-09-13 20:57:39 +03:00
|
|
|
qCInfo(lcCse()) << retCode;
|
|
|
|
});
|
|
|
|
job->start();
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:04:02 +03:00
|
|
|
void ClientSideEncryption::setTokenForFolder(const QByteArray& folderId, const QByteArray& token)
|
|
|
|
{
|
2018-03-30 22:23:18 +03:00
|
|
|
_folder2token[folderId] = token;
|
2017-11-13 19:04:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray ClientSideEncryption::tokenForFolder(const QByteArray& folderId) const
|
|
|
|
{
|
2018-03-30 22:23:18 +03:00
|
|
|
Q_ASSERT(_folder2token.contains(folderId));
|
|
|
|
return _folder2token[folderId];
|
2017-11-13 19:04:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 18:11:21 +03:00
|
|
|
void ClientSideEncryption::encryptPrivateKey()
|
2017-09-14 19:39:18 +03:00
|
|
|
{
|
2017-11-27 18:11:21 +03:00
|
|
|
QStringList list = WordList::getRandomWords(12);
|
|
|
|
_mnemonic = list.join(' ');
|
2019-05-08 20:56:00 +03:00
|
|
|
_newMnemonicGenerated = true;
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "mnemonic Generated:" << _mnemonic;
|
2017-09-14 19:39:18 +03:00
|
|
|
|
2018-01-21 21:50:40 +03:00
|
|
|
emit mnemonicGenerated(_mnemonic);
|
2018-01-19 00:10:55 +03:00
|
|
|
|
2017-12-20 17:41:38 +03:00
|
|
|
QString passPhrase = list.join(QString()).toLower();
|
2017-11-24 22:58:20 +03:00
|
|
|
qCInfo(lcCse()) << "Passphrase Generated:" << passPhrase;
|
2017-09-14 19:39:18 +03:00
|
|
|
|
2017-12-20 18:07:33 +03:00
|
|
|
auto salt = EncryptionHelper::generateRandom(40);
|
|
|
|
auto secretKey = EncryptionHelper::generatePassword(passPhrase, salt);
|
2018-01-23 23:02:52 +03:00
|
|
|
auto cryptedText = EncryptionHelper::encryptPrivateKey(secretKey, EncryptionHelper::privateKeyToPem(_privateKey), salt);
|
2017-10-24 16:53:17 +03:00
|
|
|
|
2017-11-25 00:10:28 +03:00
|
|
|
// Send private key to the server
|
2018-03-30 22:23:18 +03:00
|
|
|
auto job = new StorePrivateKeyApiJob(_account, baseUrl() + "private-key", this);
|
2017-11-27 18:11:21 +03:00
|
|
|
job->setPrivateKey(cryptedText);
|
2018-03-30 22:23:18 +03:00
|
|
|
connect(job, &StorePrivateKeyApiJob::jsonReceived, [this](const QJsonDocument& doc, int retCode) {
|
|
|
|
Q_UNUSED(doc);
|
|
|
|
switch(retCode) {
|
|
|
|
case 200:
|
2017-11-27 18:11:21 +03:00
|
|
|
qCInfo(lcCse()) << "Private key stored encrypted on server.";
|
2017-11-28 14:36:35 +03:00
|
|
|
writePrivateKey();
|
|
|
|
writeCertificate();
|
|
|
|
writeMnemonic();
|
2018-03-30 22:23:18 +03:00
|
|
|
emit initializationFinished();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qCInfo(lcCse()) << "Store private key failed, return code:" << retCode;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
job->start();
|
2017-09-14 19:39:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 20:56:00 +03:00
|
|
|
bool ClientSideEncryption::newMnemonicGenerated() const
|
|
|
|
{
|
|
|
|
return _newMnemonicGenerated;
|
|
|
|
}
|
|
|
|
|
2017-11-28 14:36:35 +03:00
|
|
|
void ClientSideEncryption::decryptPrivateKey(const QByteArray &key) {
|
|
|
|
QString msg = tr("Please enter your end to end encryption passphrase:<br>"
|
|
|
|
"<br>"
|
|
|
|
"User: %2<br>"
|
|
|
|
"Account: %3<br>")
|
|
|
|
.arg(Utility::escape(_account->credentials()->user()),
|
|
|
|
Utility::escape(_account->displayName()));
|
|
|
|
|
|
|
|
QInputDialog dialog;
|
|
|
|
dialog.setWindowTitle(tr("Enter E2E passphrase"));
|
|
|
|
dialog.setLabelText(msg);
|
|
|
|
dialog.setTextEchoMode(QLineEdit::Normal);
|
|
|
|
|
|
|
|
QString prev;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
if (!prev.isEmpty()) {
|
|
|
|
dialog.setTextValue(prev);
|
|
|
|
}
|
|
|
|
bool ok = dialog.exec();
|
|
|
|
if (ok) {
|
|
|
|
qCInfo(lcCse()) << "Got mnemonic:" << dialog.textValue();
|
|
|
|
prev = dialog.textValue();
|
|
|
|
|
|
|
|
_mnemonic = prev;
|
2017-12-20 17:41:38 +03:00
|
|
|
QString mnemonic = prev.split(" ").join(QString()).toLower();
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "mnemonic:" << mnemonic;
|
|
|
|
|
2017-12-20 18:07:33 +03:00
|
|
|
// split off salt
|
2020-08-10 12:26:52 +03:00
|
|
|
const auto salt = EncryptionHelper::extractPrivateKeySalt(key);
|
2017-12-20 18:07:33 +03:00
|
|
|
|
|
|
|
auto pass = EncryptionHelper::generatePassword(mnemonic, salt);
|
|
|
|
qCInfo(lcCse()) << "Generated key:" << pass;
|
|
|
|
|
2020-08-10 12:19:20 +03:00
|
|
|
QByteArray privateKey = EncryptionHelper::decryptPrivateKey(pass, key);
|
2018-10-15 01:21:08 +03:00
|
|
|
//_privateKey = QSslKey(privateKey, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
|
|
|
_privateKey = privateKey;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
2018-10-15 01:21:08 +03:00
|
|
|
qCInfo(lcCse()) << "Private key: " << _privateKey;
|
2017-11-28 14:36:35 +03:00
|
|
|
|
|
|
|
if (!_privateKey.isNull()) {
|
|
|
|
writePrivateKey();
|
|
|
|
writeCertificate();
|
|
|
|
writeMnemonic();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_mnemonic = QString();
|
2018-10-15 01:21:08 +03:00
|
|
|
_privateKey = QByteArray();
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Cancelled";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit initializationFinished();
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:21:53 +03:00
|
|
|
void ClientSideEncryption::getPrivateKeyFromServer()
|
|
|
|
{
|
2017-11-28 14:36:35 +03:00
|
|
|
qCInfo(lcCse()) << "Retrieving private key from server";
|
|
|
|
auto job = new JsonApiJob(_account, baseUrl() + "private-key", this);
|
|
|
|
connect(job, &JsonApiJob::jsonReceived, [this](const QJsonDocument& doc, int retCode) {
|
|
|
|
if (retCode == 200) {
|
|
|
|
QString key = doc.object()["ocs"].toObject()["data"].toObject()["private-key"].toString();
|
|
|
|
qCInfo(lcCse()) << key;
|
|
|
|
qCInfo(lcCse()) << "Found private key, lets decrypt it!";
|
|
|
|
decryptPrivateKey(key.toLocal8Bit());
|
|
|
|
} else if (retCode == 404) {
|
|
|
|
qCInfo(lcCse()) << "No private key on the server: setup is incomplete.";
|
|
|
|
} else {
|
|
|
|
qCInfo(lcCse()) << "Error while requesting public key: " << retCode;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
job->start();
|
2017-09-12 12:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientSideEncryption::getPublicKeyFromServer()
|
|
|
|
{
|
|
|
|
qCInfo(lcCse()) << "Retrieving public key from server";
|
2017-10-18 21:21:35 +03:00
|
|
|
auto job = new JsonApiJob(_account, baseUrl() + "public-key", this);
|
2017-09-12 12:21:53 +03:00
|
|
|
connect(job, &JsonApiJob::jsonReceived, [this](const QJsonDocument& doc, int retCode) {
|
2017-11-28 14:36:35 +03:00
|
|
|
if (retCode == 200) {
|
|
|
|
QString publicKey = doc.object()["ocs"].toObject()["data"].toObject()["public-keys"].toObject()[_account->davUser()].toString();
|
|
|
|
_certificate = QSslCertificate(publicKey.toLocal8Bit(), QSsl::Pem);
|
|
|
|
_publicKey = _certificate.publicKey();
|
|
|
|
qCInfo(lcCse()) << publicKey;
|
|
|
|
qCInfo(lcCse()) << "Found Public key, requesting Private Key.";
|
|
|
|
getPrivateKeyFromServer();
|
|
|
|
} else if (retCode == 404) {
|
2017-09-12 14:44:42 +03:00
|
|
|
qCInfo(lcCse()) << "No public key on the server";
|
|
|
|
generateKeyPair();
|
2017-11-28 14:36:35 +03:00
|
|
|
} else {
|
|
|
|
qCInfo(lcCse()) << "Error while requesting public key: " << retCode;
|
|
|
|
}
|
2017-09-12 12:21:53 +03:00
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
2017-10-23 19:15:39 +03:00
|
|
|
|
2017-11-20 23:38:17 +03:00
|
|
|
void ClientSideEncryption::fetchFolderEncryptedStatus() {
|
2018-03-30 22:23:18 +03:00
|
|
|
_refreshingEncryptionStatus = true;
|
|
|
|
auto getEncryptedStatus = new GetFolderEncryptStatusJob(_account, QString());
|
|
|
|
connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusReceived,
|
|
|
|
this, &ClientSideEncryption::folderEncryptedStatusFetched);
|
|
|
|
connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusError,
|
|
|
|
this, &ClientSideEncryption::folderEncryptedStatusError);
|
|
|
|
getEncryptedStatus->start();
|
2017-11-20 23:38:17 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:15:25 +03:00
|
|
|
void ClientSideEncryption::folderEncryptedStatusFetched(const QHash<QString, bool>& result)
|
2017-11-20 23:38:17 +03:00
|
|
|
{
|
2018-03-30 22:23:18 +03:00
|
|
|
_refreshingEncryptionStatus = false;
|
|
|
|
_folder2encryptedStatus = result;
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Retrieved correctly the encrypted status of the folders." << result;
|
2020-07-02 17:37:01 +03:00
|
|
|
emit folderEncryptedStatusFetchDone(result);
|
2017-11-20 23:38:17 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 18:55:12 +03:00
|
|
|
void ClientSideEncryption::folderEncryptedStatusError(int error)
|
2017-11-20 23:38:17 +03:00
|
|
|
{
|
2018-03-30 22:23:18 +03:00
|
|
|
_refreshingEncryptionStatus = false;
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Failed to retrieve the status of the folders." << error;
|
2020-07-02 17:37:01 +03:00
|
|
|
emit folderEncryptedStatusFetchDone({});
|
2017-11-20 23:38:17 +03:00
|
|
|
}
|
2017-10-23 19:15:39 +03:00
|
|
|
|
2018-01-30 00:04:50 +03:00
|
|
|
FolderMetadata::FolderMetadata(AccountPtr account, const QByteArray& metadata, int statusCode) : _account(account)
|
2017-10-23 20:27:34 +03:00
|
|
|
{
|
2018-01-30 00:04:50 +03:00
|
|
|
if (metadata.isEmpty() || statusCode == 404) {
|
2017-12-20 00:47:05 +03:00
|
|
|
qCInfo(lcCseMetadata()) << "Setupping Empty Metadata";
|
2017-10-30 17:39:40 +03:00
|
|
|
setupEmptyMetadata();
|
2017-10-31 14:07:47 +03:00
|
|
|
} else {
|
2017-12-20 00:47:05 +03:00
|
|
|
qCInfo(lcCseMetadata()) << "Setting up existing metadata";
|
2017-12-21 00:04:26 +03:00
|
|
|
setupExistingMetadata(metadata);
|
2017-10-23 20:27:34 +03:00
|
|
|
}
|
2017-10-30 17:39:40 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 00:04:26 +03:00
|
|
|
void FolderMetadata::setupExistingMetadata(const QByteArray& metadata)
|
2017-12-12 17:35:53 +03:00
|
|
|
{
|
|
|
|
/* This is the json response from the server, it contains two extra objects that we are *not* interested.
|
|
|
|
* ocs and data.
|
|
|
|
*/
|
2017-12-21 00:04:26 +03:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(metadata);
|
2017-12-20 02:20:27 +03:00
|
|
|
qCInfo(lcCseMetadata()) << doc.toJson(QJsonDocument::Compact);
|
2017-12-12 17:35:53 +03:00
|
|
|
|
|
|
|
// The metadata is being retrieved as a string stored in a json.
|
2017-12-20 02:20:27 +03:00
|
|
|
// This *seems* to be broken but the RFC doesn't explicits how it wants.
|
2017-12-12 17:35:53 +03:00
|
|
|
// I'm currently unsure if this is error on my side or in the server implementation.
|
2017-12-12 18:09:31 +03:00
|
|
|
// And because inside of the meta-data there's an object called metadata, without '-'
|
|
|
|
// make it really different.
|
|
|
|
|
2017-12-20 02:20:27 +03:00
|
|
|
QString metaDataStr = doc.object()["ocs"]
|
|
|
|
.toObject()["data"]
|
|
|
|
.toObject()["meta-data"]
|
|
|
|
.toString();
|
|
|
|
|
|
|
|
QJsonDocument metaDataDoc = QJsonDocument::fromJson(metaDataStr.toLocal8Bit());
|
|
|
|
QJsonObject metadataObj = metaDataDoc.object()["metadata"].toObject();
|
|
|
|
QJsonObject metadataKeys = metadataObj["metadataKeys"].toObject();
|
|
|
|
QByteArray sharing = metadataObj["sharing"].toString().toLocal8Bit();
|
2017-12-20 22:49:16 +03:00
|
|
|
QJsonObject files = metaDataDoc.object()["files"].toObject();
|
2017-12-20 02:20:27 +03:00
|
|
|
|
|
|
|
QJsonDocument debugHelper;
|
|
|
|
debugHelper.setObject(metadataKeys);
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Keys: " << debugHelper.toJson(QJsonDocument::Compact);
|
2017-12-20 02:20:27 +03:00
|
|
|
|
|
|
|
// Iterate over the document to store the keys. I'm unsure that the keys are in order,
|
|
|
|
// perhaps it's better to store a map instead of a vector, perhaps this just doesn't matter.
|
|
|
|
for(auto it = metadataKeys.constBegin(), end = metadataKeys.constEnd(); it != end; it++) {
|
|
|
|
QByteArray currB64Pass = it.value().toString().toLocal8Bit();
|
2018-01-15 13:26:06 +03:00
|
|
|
/*
|
|
|
|
* We have to base64 decode the metadatakey here. This was a misunderstanding in the RFC
|
|
|
|
* Now we should be compatible with Android and IOS. Maybe we can fix it later.
|
|
|
|
*/
|
2018-03-26 23:02:46 +03:00
|
|
|
QByteArray b64DecryptedKey = decryptMetadataKey(currB64Pass);
|
|
|
|
if (b64DecryptedKey.isEmpty()) {
|
|
|
|
qCDebug(lcCse()) << "Could not decrypt metadata for key" << it.key();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray decryptedKey = QByteArray::fromBase64(b64DecryptedKey);
|
2017-12-20 23:25:27 +03:00
|
|
|
_metadataKeys.insert(it.key().toInt(), decryptedKey);
|
2017-12-20 02:20:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cool, We actually have the key, we can decrypt the rest of the metadata.
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Sharing: " << sharing;
|
2018-01-15 13:26:06 +03:00
|
|
|
if (sharing.size()) {
|
2020-08-10 12:08:15 +03:00
|
|
|
auto sharingDecrypted = decryptJsonObject(sharing, _metadataKeys.last());
|
2018-01-15 13:26:06 +03:00
|
|
|
qCDebug(lcCse) << "Sharing Decrypted" << sharingDecrypted;
|
|
|
|
|
|
|
|
//Sharing is also a JSON object, so extract it and populate.
|
|
|
|
auto sharingDoc = QJsonDocument::fromJson(sharingDecrypted);
|
|
|
|
auto sharingObj = sharingDoc.object();
|
|
|
|
for (auto it = sharingObj.constBegin(), end = sharingObj.constEnd(); it != end; it++) {
|
|
|
|
_sharing.push_back({it.key(), it.value().toString()});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qCDebug(lcCse) << "Skipping sharing section since it is empty";
|
2017-12-20 21:37:04 +03:00
|
|
|
}
|
|
|
|
|
2017-12-20 22:49:16 +03:00
|
|
|
for (auto it = files.constBegin(), end = files.constEnd(); it != end; it++) {
|
|
|
|
EncryptedFile file;
|
|
|
|
file.encryptedFilename = it.key();
|
|
|
|
|
|
|
|
auto fileObj = it.value().toObject();
|
|
|
|
file.metadataKey = fileObj["metadataKey"].toInt();
|
|
|
|
file.authenticationTag = QByteArray::fromBase64(fileObj["authenticationTag"].toString().toLocal8Bit());
|
|
|
|
file.initializationVector = QByteArray::fromBase64(fileObj["initializationVector"].toString().toLocal8Bit());
|
|
|
|
|
|
|
|
//Decrypt encrypted part
|
2017-12-20 23:25:27 +03:00
|
|
|
QByteArray key = _metadataKeys[file.metadataKey];
|
2017-12-20 22:49:16 +03:00
|
|
|
auto encryptedFile = fileObj["encrypted"].toString().toLocal8Bit();
|
2020-08-10 12:08:15 +03:00
|
|
|
auto decryptedFile = decryptJsonObject(encryptedFile, key);
|
2017-12-20 22:49:16 +03:00
|
|
|
auto decryptedFileDoc = QJsonDocument::fromJson(decryptedFile);
|
|
|
|
auto decryptedFileObj = decryptedFileDoc.object();
|
|
|
|
|
|
|
|
file.originalFilename = decryptedFileObj["filename"].toString();
|
|
|
|
file.encryptionKey = QByteArray::fromBase64(decryptedFileObj["key"].toString().toLocal8Bit());
|
|
|
|
file.mimetype = decryptedFileObj["mimetype"].toString().toLocal8Bit();
|
|
|
|
file.fileVersion = decryptedFileObj["version"].toInt();
|
|
|
|
|
|
|
|
_files.push_back(file);
|
|
|
|
}
|
2017-12-12 17:35:53 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:39:40 +03:00
|
|
|
// RSA/ECB/OAEPWithSHA-256AndMGF1Padding using private / public key.
|
2019-05-07 21:57:01 +03:00
|
|
|
QByteArray FolderMetadata::encryptMetadataKey(const QByteArray& data) const
|
|
|
|
{
|
|
|
|
Bio publicKeyBio;
|
2017-12-08 13:24:22 +03:00
|
|
|
QByteArray publicKeyPem = _account->e2e()->_publicKey.toPem();
|
|
|
|
BIO_write(publicKeyBio, publicKeyPem.constData(), publicKeyPem.size());
|
2019-05-07 21:57:01 +03:00
|
|
|
auto publicKey = PKey::readPublicKey(publicKeyBio);
|
2017-10-30 22:02:55 +03:00
|
|
|
|
2017-12-20 12:22:35 +03:00
|
|
|
// The metadata key is binary so base64 encode it first
|
2019-05-07 21:57:01 +03:00
|
|
|
return EncryptionHelper::encryptStringAsymmetric(publicKey, data.toBase64());
|
2017-10-30 17:39:40 +03:00
|
|
|
}
|
|
|
|
|
2017-12-20 00:47:05 +03:00
|
|
|
QByteArray FolderMetadata::decryptMetadataKey(const QByteArray& encryptedMetadata) const
|
2017-10-31 15:06:20 +03:00
|
|
|
{
|
2019-05-07 21:57:01 +03:00
|
|
|
Bio privateKeyBio;
|
2018-10-15 01:21:08 +03:00
|
|
|
QByteArray privateKeyPem = _account->e2e()->_privateKey;
|
2017-12-12 18:15:05 +03:00
|
|
|
BIO_write(privateKeyBio, privateKeyPem.constData(), privateKeyPem.size());
|
2019-05-07 21:57:01 +03:00
|
|
|
auto key = PKey::readPrivateKey(privateKeyBio);
|
2017-12-12 18:09:31 +03:00
|
|
|
|
2017-12-20 12:22:35 +03:00
|
|
|
// Also base64 decode the result
|
2018-03-26 23:02:46 +03:00
|
|
|
QByteArray decryptResult = EncryptionHelper::decryptStringAsymmetric(
|
|
|
|
key, QByteArray::fromBase64(encryptedMetadata));
|
|
|
|
|
|
|
|
if (decryptResult.isEmpty())
|
|
|
|
{
|
|
|
|
qCDebug(lcCse()) << "ERROR. Could not decrypt the metadata key";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return QByteArray::fromBase64(decryptResult);
|
2017-10-31 15:06:20 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:39:40 +03:00
|
|
|
// AES/GCM/NoPadding (128 bit key size)
|
2017-12-20 02:20:27 +03:00
|
|
|
QByteArray FolderMetadata::encryptJsonObject(const QByteArray& obj, const QByteArray pass) const
|
|
|
|
{
|
2017-12-20 00:47:05 +03:00
|
|
|
return EncryptionHelper::encryptStringSymmetric(pass, obj);
|
2017-11-01 16:42:39 +03:00
|
|
|
}
|
|
|
|
|
2017-12-20 00:47:05 +03:00
|
|
|
QByteArray FolderMetadata::decryptJsonObject(const QByteArray& encryptedMetadata, const QByteArray& pass) const
|
2017-11-01 16:42:39 +03:00
|
|
|
{
|
2017-12-20 02:20:27 +03:00
|
|
|
return EncryptionHelper::decryptStringSymmetric(pass, encryptedMetadata);
|
2017-10-30 17:39:40 +03:00
|
|
|
}
|
2017-10-23 20:27:34 +03:00
|
|
|
|
2017-10-30 17:39:40 +03:00
|
|
|
void FolderMetadata::setupEmptyMetadata() {
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Settint up empty metadata";
|
2017-11-25 23:43:15 +03:00
|
|
|
QByteArray newMetadataPass = EncryptionHelper::generateRandom(16);
|
2017-12-21 00:04:26 +03:00
|
|
|
_metadataKeys.insert(0, newMetadataPass);
|
2017-12-20 00:47:05 +03:00
|
|
|
|
|
|
|
QString publicKey = _account->e2e()->_publicKey.toPem().toBase64();
|
|
|
|
QString displayName = _account->displayName();
|
|
|
|
|
2017-12-21 00:04:26 +03:00
|
|
|
_sharing.append({displayName, publicKey});
|
|
|
|
}
|
2017-12-20 00:47:05 +03:00
|
|
|
|
2017-12-21 00:04:26 +03:00
|
|
|
QByteArray FolderMetadata::encryptedMetadata() {
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Generating metadata";
|
2017-12-20 00:47:05 +03:00
|
|
|
|
2017-12-21 00:04:26 +03:00
|
|
|
QJsonObject metadataKeys;
|
|
|
|
for (auto it = _metadataKeys.constBegin(), end = _metadataKeys.constEnd(); it != end; it++) {
|
2018-01-15 13:26:06 +03:00
|
|
|
/*
|
|
|
|
* We have to already base64 encode the metadatakey here. This was a misunderstanding in the RFC
|
|
|
|
* Now we should be compatible with Android and IOS. Maybe we can fix it later.
|
|
|
|
*/
|
|
|
|
const QByteArray encryptedKey = encryptMetadataKey(it.value().toBase64());
|
2017-12-21 00:04:26 +03:00
|
|
|
metadataKeys.insert(QString::number(it.key()), QString(encryptedKey));
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:02:52 +03:00
|
|
|
/* NO SHARING IN V1
|
2017-12-21 00:04:26 +03:00
|
|
|
QJsonObject recepients;
|
|
|
|
for (auto it = _sharing.constBegin(), end = _sharing.constEnd(); it != end; it++) {
|
|
|
|
recepients.insert(it->first, it->second);
|
|
|
|
}
|
|
|
|
QJsonDocument recepientDoc;
|
|
|
|
recepientDoc.setObject(recepients);
|
|
|
|
QString sharingEncrypted = encryptJsonObject(recepientDoc.toJson(QJsonDocument::Compact), _metadataKeys.last());
|
2018-01-23 23:02:52 +03:00
|
|
|
*/
|
2017-12-20 00:47:05 +03:00
|
|
|
|
|
|
|
QJsonObject metadata = {
|
|
|
|
{"metadataKeys", metadataKeys},
|
2018-01-23 23:02:52 +03:00
|
|
|
// {"sharing", sharingEncrypted},
|
2017-12-20 00:47:05 +03:00
|
|
|
{"version", 1}
|
|
|
|
};
|
|
|
|
|
2017-12-21 00:04:26 +03:00
|
|
|
QJsonObject files;
|
|
|
|
for (auto it = _files.constBegin(), end = _files.constEnd(); it != end; it++) {
|
|
|
|
QJsonObject encrypted;
|
|
|
|
encrypted.insert("key", QString(it->encryptionKey.toBase64()));
|
|
|
|
encrypted.insert("filename", it->originalFilename);
|
|
|
|
encrypted.insert("mimetype", QString(it->mimetype));
|
|
|
|
encrypted.insert("version", it->fileVersion);
|
|
|
|
QJsonDocument encryptedDoc;
|
|
|
|
encryptedDoc.setObject(encrypted);
|
|
|
|
|
|
|
|
QString encryptedEncrypted = encryptJsonObject(encryptedDoc.toJson(QJsonDocument::Compact), _metadataKeys.last());
|
2018-03-25 23:31:49 +03:00
|
|
|
if (encryptedEncrypted.isEmpty()) {
|
|
|
|
qCDebug(lcCse) << "Metadata generation failed!";
|
|
|
|
}
|
2017-12-21 00:04:26 +03:00
|
|
|
|
|
|
|
QJsonObject file;
|
|
|
|
file.insert("encrypted", encryptedEncrypted);
|
|
|
|
file.insert("initializationVector", QString(it->initializationVector.toBase64()));
|
|
|
|
file.insert("authenticationTag", QString(it->authenticationTag.toBase64()));
|
|
|
|
file.insert("metadataKey", _metadataKeys.lastKey());
|
|
|
|
|
|
|
|
files.insert(it->encryptedFilename, file);
|
|
|
|
}
|
2017-12-20 00:47:05 +03:00
|
|
|
|
|
|
|
QJsonObject metaObject = {
|
2017-12-20 01:09:39 +03:00
|
|
|
{"metadata", metadata},
|
2017-12-20 00:47:05 +03:00
|
|
|
{"files", files}
|
2017-10-30 17:39:40 +03:00
|
|
|
};
|
2017-10-30 21:05:55 +03:00
|
|
|
|
2017-12-20 00:47:05 +03:00
|
|
|
QJsonDocument internalMetadata;
|
|
|
|
internalMetadata.setObject(metaObject);
|
2017-12-21 00:04:26 +03:00
|
|
|
return internalMetadata.toJson();
|
2017-10-23 20:27:34 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 00:08:57 +03:00
|
|
|
void FolderMetadata::addEncryptedFile(const EncryptedFile &f) {
|
2018-01-23 13:58:05 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < _files.size(); i++) {
|
|
|
|
if (_files.at(i).originalFilename == f.originalFilename) {
|
|
|
|
_files.removeAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 00:08:57 +03:00
|
|
|
_files.append(f);
|
|
|
|
}
|
|
|
|
|
2018-04-04 01:41:11 +03:00
|
|
|
void FolderMetadata::removeEncryptedFile(const EncryptedFile &f)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _files.size(); i++) {
|
|
|
|
if (_files.at(i).originalFilename == f.originalFilename) {
|
|
|
|
_files.removeAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 00:08:57 +03:00
|
|
|
QVector<EncryptedFile> FolderMetadata::files() const {
|
|
|
|
return _files;
|
2017-11-13 18:46:30 +03:00
|
|
|
}
|
2017-11-01 19:36:54 +03:00
|
|
|
|
2017-12-29 18:22:23 +03:00
|
|
|
bool ClientSideEncryption::isFolderEncrypted(const QString& path) const {
|
|
|
|
auto it = _folder2encryptedStatus.constFind(path);
|
|
|
|
if (it == _folder2encryptedStatus.constEnd())
|
2017-11-27 23:19:54 +03:00
|
|
|
return false;
|
|
|
|
return (*it);
|
2017-11-27 23:06:38 +03:00
|
|
|
}
|
2017-11-27 23:19:54 +03:00
|
|
|
|
2020-05-28 21:23:55 +03:00
|
|
|
bool ClientSideEncryption::isAnyParentFolderEncrypted(const QString &path) const
|
|
|
|
{
|
|
|
|
int slashPosition = 0;
|
|
|
|
|
|
|
|
while ((slashPosition = path.indexOf("/", slashPosition + 1)) != -1) {
|
|
|
|
// Ignore the last slash
|
|
|
|
if (slashPosition == path.length() - 1) break;
|
|
|
|
|
|
|
|
if (isFolderEncrypted(path.left(slashPosition + 1))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-21 23:49:24 +03:00
|
|
|
bool EncryptionHelper::fileEncryption(const QByteArray &key, const QByteArray &iv, QFile *input, QFile *output, QByteArray& returnTag)
|
2017-12-14 18:54:56 +03:00
|
|
|
{
|
2017-12-21 02:00:27 +03:00
|
|
|
if (!input->open(QIODevice::ReadOnly)) {
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Could not open input file for reading" << input->errorString();
|
2017-12-21 02:00:27 +03:00
|
|
|
}
|
|
|
|
if (!output->open(QIODevice::WriteOnly)) {
|
2020-05-03 23:54:50 +03:00
|
|
|
qCDebug(lcCse) << "Could not oppen output file for writing" << output->errorString();
|
2017-12-21 02:00:27 +03:00
|
|
|
}
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
// Init
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not create context";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not init cipher";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_set_padding(ctx, 0);
|
|
|
|
|
|
|
|
/* Set IV length. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not set iv length";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_EncryptInit_ex(ctx, nullptr, nullptr, (const unsigned char *)key.constData(), (const unsigned char *)iv.constData())) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not set key and iv";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray out(1024 + 16 - 1, '\0');
|
2017-12-14 18:54:56 +03:00
|
|
|
int len = 0;
|
|
|
|
int total_len = 0;
|
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Starting to encrypt the file" << input->fileName() << input->atEnd();
|
2017-12-21 00:28:01 +03:00
|
|
|
while(!input->atEnd()) {
|
|
|
|
QByteArray data = input->read(1024);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
if (data.size() == 0) {
|
|
|
|
qCInfo(lcCse()) << "Could not read data from file";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "Encrypting " << data;
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_EncryptUpdate(ctx, unsignedData(out), &len, (unsigned char *)data.constData(), data.size())) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not encrypt";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
output->write(out, len);
|
2017-12-14 18:54:56 +03:00
|
|
|
total_len += len;
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
if(1 != EVP_EncryptFinal_ex(ctx, unsignedData(out), &len)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could finalize encryption";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
2019-05-05 23:24:12 +03:00
|
|
|
output->write(out, len);
|
2017-12-14 18:54:56 +03:00
|
|
|
total_len += len;
|
|
|
|
|
|
|
|
/* Get the tag */
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray tag(16, '\0');
|
|
|
|
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, unsignedData(tag))) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not get tag";
|
2018-01-21 23:40:53 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
returnTag = tag;
|
|
|
|
output->write(tag, 16);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
2017-12-21 00:28:01 +03:00
|
|
|
input->close();
|
|
|
|
output->close();
|
2017-12-28 22:33:10 +03:00
|
|
|
qCDebug(lcCse) << "File Encrypted Successfully";
|
2018-03-25 23:31:49 +03:00
|
|
|
return true;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 10:18:54 +03:00
|
|
|
bool EncryptionHelper::fileDecryption(const QByteArray &key, const QByteArray& iv,
|
2018-01-21 23:24:02 +03:00
|
|
|
QFile *input, QFile *output)
|
2017-12-14 18:54:56 +03:00
|
|
|
{
|
2018-01-21 23:24:02 +03:00
|
|
|
input->open(QIODevice::ReadOnly);
|
|
|
|
output->open(QIODevice::WriteOnly);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
// Init
|
2019-05-05 23:24:12 +03:00
|
|
|
CipherCtx ctx;
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
/* Create and initialise the context */
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!ctx) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not create context";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise the decryption operation. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not init cipher";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX_set_padding(ctx, 0);
|
|
|
|
|
|
|
|
/* Set IV length. */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not set iv length";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialise key and IV */
|
2018-11-12 20:46:39 +03:00
|
|
|
if(!EVP_DecryptInit_ex(ctx, nullptr, nullptr, (const unsigned char *) key.constData(), (const unsigned char *) iv.constData())) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not set key and iv";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
qint64 size = input->size() - 16;
|
2017-12-14 18:54:56 +03:00
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
QByteArray out(1024 + 16 - 1, '\0');
|
2017-12-14 18:54:56 +03:00
|
|
|
int len = 0;
|
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
while(input->pos() < size) {
|
2017-12-14 18:54:56 +03:00
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
int toRead = size - input->pos();
|
2017-12-14 18:54:56 +03:00
|
|
|
if (toRead > 1024) {
|
|
|
|
toRead = 1024;
|
|
|
|
}
|
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
QByteArray data = input->read(toRead);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
if (data.size() == 0) {
|
|
|
|
qCInfo(lcCse()) << "Could not read data from file";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
if(!EVP_DecryptUpdate(ctx, unsignedData(out), &len, (unsigned char *)data.constData(), data.size())) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could not decrypt";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
output->write(out, len);
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
QByteArray tag = input->read(16);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
|
|
|
/* Set expected tag value. Works in OpenSSL 1.0.1d and later */
|
|
|
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), (unsigned char *)tag.constData())) {
|
|
|
|
qCInfo(lcCse()) << "Could not set expected tag";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-05 23:24:12 +03:00
|
|
|
if(1 != EVP_DecryptFinal_ex(ctx, unsignedData(out), &len)) {
|
2017-12-14 18:54:56 +03:00
|
|
|
qCInfo(lcCse()) << "Could finalize decryption";
|
2018-03-27 10:18:54 +03:00
|
|
|
return false;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
2019-05-05 23:24:12 +03:00
|
|
|
output->write(out, len);
|
2017-12-14 18:54:56 +03:00
|
|
|
|
2018-01-21 23:24:02 +03:00
|
|
|
input->close();
|
|
|
|
output->close();
|
2018-03-27 10:18:54 +03:00
|
|
|
return true;
|
2017-12-14 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2017-09-11 16:00:01 +03:00
|
|
|
}
|