[network/ruqola] src/core/encryption: Fix potential crash

Laurent Montel <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 4cdcd96d8d3b1172432f45082907496895c8f0fb by Laurent Montel.
Committed on 03/08/2026 at 05:40.
Pushed by mlaurent into branch 'master'.

Fix potential crash

M  +25   -0    src/core/encryption/encryptionutils.cpp

https://invent.kde.org/network/ruqola/-/commit/4cdcd96d8d3b1172432f45082907496895c8f0fb

diff --git a/src/core/encryption/encryptionutils.cpp b/src/core/encryption/encryptionutils.cpp
index ee3442b399..8161b3621e 100644
--- a/src/core/encryption/encryptionutils.cpp
+++ b/src/core/encryption/encryptionutils.cpp
@@ -55,6 +55,11 @@ using namespace Qt::Literals::StringLiterals;
  */
 QByteArray EncryptionUtils::exportJWKPublicKey(RSA *rsaKey)
 {
+    if (!rsaKey) {
+        qCWarning(RUQOLA_ENCRYPTION_LOG) << "RSA key is null";
+        return {};
+    }
+
     const BIGNUM *n;
     const BIGNUM *e;
     const BIGNUM *d;
@@ -305,6 +310,11 @@ QByteArray EncryptionUtils::generateSessionKey()
  */
 RSA *EncryptionUtils::publicKeyFromPEM(const QByteArray &pem)
 {
+    if (pem.isEmpty()) {
+        qCWarning(RUQOLA_ENCRYPTION_LOG) << "publicKeyFromPEM: pem is empty";
+        return nullptr;
+    }
+
     BIO *bio = BIO_new_mem_buf(pem.constData(), pem.size());
     if (!bio) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "BIO_new_mem_buf failed!";
@@ -329,6 +339,11 @@ RSA *EncryptionUtils::publicKeyFromPEM(const QByteArray &pem)
  */
 RSA *EncryptionUtils::privateKeyFromPEM(const QByteArray &pem)
 {
+    if (pem.isEmpty()) {
+        qCWarning(RUQOLA_ENCRYPTION_LOG) << "privateKeyFromPEM: pem is empty";
+        return nullptr;
+    }
+
     BIO *bio = BIO_new_mem_buf(pem.constData(), pem.size());
     if (!bio) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "BIO_new_mem_buf failed!";
@@ -348,6 +363,11 @@ RSA *EncryptionUtils::privateKeyFromPEM(const QByteArray &pem)
 
 QByteArray EncryptionUtils::encryptSessionKey(const QByteArray &sessionKey, RSA *publicKey)
 {
+    if (sessionKey.isEmpty() || !publicKey) {
+        qCWarning(RUQOLA_ENCRYPTION_LOG) << "Session key encryption failed: invalid input";
+        return {};
+    }
+
     QByteArray encryptedSessionKey(RSA_size(publicKey), 0);
     const int bytes = RSA_public_encrypt(sessionKey.size(),
                                          reinterpret_cast<const unsigned char *>(sessionKey.constData()),
@@ -364,6 +384,11 @@ QByteArray EncryptionUtils::encryptSessionKey(const QByteArray &sessionKey, RSA
 
 QByteArray EncryptionUtils::decryptSessionKey(const QByteArray &encryptedSessionKey, RSA *privateKey)
 {
+    if (encryptedSessionKey.isEmpty() || !privateKey) {
+        qCWarning(RUQOLA_ENCRYPTION_LOG) << "Session key decryption failed: invalid input";
+        return {};
+    }
+
     QByteArray decryptedSessionKey(RSA_size(privateKey), 0);
     const int bytes = RSA_private_decrypt(encryptedSessionKey.size(),
                                           reinterpret_cast<const unsigned char *>(encryptedSessionKey.constData()),
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.