[cse] Start the encryption algorithm for the Private Key

This commit is contained in:
Tomaz Canabrava 2017-09-14 18:39:18 +02:00 committed by Roeland Jago Douma
parent fd00e180f5
commit 5395fc56b1
No known key found for this signature in database
GPG key ID: F941078878347C0C
2 changed files with 43 additions and 4 deletions

View file

@ -18,6 +18,8 @@
#include <QDir>
#include <QJsonObject>
#include "wordlist.h"
namespace OCC
{
@ -198,18 +200,19 @@ QString ClientSideEncryption::generateCSR(EVP_PKEY *keyPair)
job = new SignPublicKeyApiJob(_account, baseUrl + "public-key", this);
job->setCsr(output);
// TODO: Extract this function, should not be a lambda.
connect(job, &SignPublicKeyApiJob::jsonReceived, [this](const QJsonDocument& json, int retCode) {
connect(job, &SignPublicKeyApiJob::jsonReceived, [this, keyPair](const QJsonDocument& json, int retCode) {
if (retCode == 200) {
auto caps = json.object().value("ocs").toObject().value("data").toObject().value("public-key").toString();
qCInfo(lcCse()) << "Public Key Returned" << caps;
QFile file(publicKeyPath() + ".sign"); // TODO: Verify if I need to keep the old file.
QFile file(publicKeyPath() + ".sign");
if (file.open(QIODevice::WriteOnly)) {
QTextStream s(&file);
s << caps;
}
file.close();
qCInfo(lcCse()) << "public key saved.";
qCInfo(lcCse()) << "public key saved, Encrypting Private Key.";
encryptPrivateKey(keyPair);
}
qCInfo(lcCse()) << retCode;
});
@ -221,6 +224,41 @@ free_all:
return "";
}
void ClientSideEncryption::encryptPrivateKey(EVP_PKEY *keyPair)
{
// Write the Private File to a BIO
// Retrieve the BIO contents, and encrypt it.
// Send the encrypted key to the server.
// I have no idea what I'm doing.
static const char* salt = "$4$YmBjm3hk$Qb74D5IUYwghUmzsMqeNFx5z0/8$";
static const int iterationCount = 1024;
static const int keyStrength = 256;
BIO* bio = BIO_new(BIO_s_mem());
QString passPhrase = WordList::getUnifiedString(WordList::getRandomWords(12));
const char* passPhrasePtr = qPrintable(passPhrase);
qCInfo(lcCse()) << "Passphrase Generated:";
qCInfo(lcCse()) << passPhrase;
// Extract the Private key from the key pair.
PEM_write_bio_PrivateKey(bio, keyPair, NULL, NULL, 0, 0, NULL);
char data[80];
QString output;
int ret = 0;
do {
ret = BIO_gets(bio, data, 80);
output += data;
if (output.endsWith("-----END PRIVATE KEY-----")) {
output = output.trimmed();
break;
}
} while (ret > 0 );
qCInfo(lcCse()) << "Private Key Extracted";
qCInfo(lcCse()) << output;
}
void ClientSideEncryption::getPrivateKeyFromServer()
{

View file

@ -26,6 +26,7 @@ public:
void getPrivateKeyFromServer();
void getPublicKeyFromServer();
void signPublicKey();
void encryptPrivateKey(EVP_PKEY *keyPair);
QString privateKeyPath() const;
QString publicKeyPath() const;