From a4de07bbd41f07343cd2f10d752a21a12a399bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 3 Jan 2021 15:14:51 +0100 Subject: [PATCH] Do not call exit in library code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian Göttsche --- src/libsync/clientsideencryption.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/libsync/clientsideencryption.cpp b/src/libsync/clientsideencryption.cpp index 401f00f9f63d2..b49bcf402b967 100644 --- a/src/libsync/clientsideencryption.cpp +++ b/src/libsync/clientsideencryption.cpp @@ -781,33 +781,33 @@ 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; } @@ -815,7 +815,7 @@ QByteArray encryptStringAsymmetric(EVP_PKEY *publicKey, const QByteArray& data) QByteArray out(static_cast(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. @@ -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; @@ -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)); }