Skip to content

Commit

Permalink
Do not call exit in library code
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
  • Loading branch information
cgzones committed Dec 8, 2022
1 parent 3b7ad15 commit a4de07b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/libsync/clientsideencryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,41 +781,41 @@ QByteArray encryptStringAsymmetric(EVP_PKEY *publicKey, const QByteArray& data)
auto ctx = PKeyCtx::forKey(publicKey, ENGINE_get_default_RSA());
if (!ctx) {
qCInfo(lcCse()) << "Could not initialize the pkey context.";
exit(1);
return {};
}

if (EVP_PKEY_encrypt_init(ctx) != 1) {
qCInfo(lcCse()) << "Error initilaizing the encryption.";
exit(1);
return {};
}

if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) {
qCInfo(lcCse()) << "Error setting the encryption padding.";
exit(1);
return {};
}

if (EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256()) <= 0) {
qCInfo(lcCse()) << "Error setting OAEP SHA 256";
exit(1);
return {};
}

if (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) <= 0) {
qCInfo(lcCse()) << "Error setting MGF1 padding";
exit(1);
return {};
}

size_t outLen = 0;
if (EVP_PKEY_encrypt(ctx, nullptr, &outLen, (unsigned char *)data.constData(), data.size()) != 1) {
qCInfo(lcCse()) << "Error retrieving the size of the encrypted data";
exit(1);
return {};
} else {
qCInfo(lcCse()) << "Encryption Length:" << outLen;
}

QByteArray out(static_cast<int>(outLen), '\0');
if (EVP_PKEY_encrypt(ctx, unsignedData(out), &outLen, (unsigned char *)data.constData(), data.size()) != 1) {
qCInfo(lcCse()) << "Could not encrypt key." << err;
exit(1);
return {};
}

// Transform the encrypted data into base64.
Expand Down Expand Up @@ -868,6 +868,10 @@ bool ClientSideEncryption::checkPublicKeyValidity(const AccountPtr &account) con
auto publicKey = PKey::readPublicKey(publicKeyBio);

auto encryptedData = EncryptionHelper::encryptStringAsymmetric(publicKey, data.toBase64());
if (encryptedData.isEmpty()) {
qCInfo(lcCse()) << "encryption failed";
return false;
}

Bio privateKeyBio;
QByteArray privateKeyPem = account->e2e()->_privateKey;
Expand Down Expand Up @@ -1547,6 +1551,8 @@ QByteArray FolderMetadata::encryptedMetadata() {
* Now we should be compatible with Android and IOS. Maybe we can fix it later.
*/
const QByteArray encryptedKey = encryptMetadataKey(it.value().toBase64());
if (encryptedKey.isEmpty())
qCDebug(lcCse) << "Key encryption failed!";
metadataKeys.insert(QString::number(it.key()), QString(encryptedKey));
}

Expand Down

0 comments on commit a4de07b

Please sign in to comment.