[network/ruqola] src/core: Allow to decode keyid

Laurent Montel <[email protected]> Wed, 5 Aug 2026 12:01:26 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 4e3a06470be47ab7e7198c4877dc9418165d1801 by Laurent Montel.
Committed on 05/08/2026 at 11:54.
Pushed by mlaurent into branch 'master'.

Allow to decode keyid

M  +47   -0    src/core/roomencryptionkey.cpp
M  +4    -0    src/core/roomencryptionkey.h

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

diff --git a/src/core/roomencryptionkey.cpp b/src/core/roomencryptionkey.cpp
index 627924ab6b..3488e05122 100644
--- a/src/core/roomencryptionkey.cpp
+++ b/src/core/roomencryptionkey.cpp
@@ -25,6 +25,48 @@ QString RoomEncryptionKey::e2EKey() const
 void RoomEncryptionKey::setE2EKey(const QString &newE2EKey)
 {
     mE2EKey = newE2EKey;
+    parseSessionKey();
+}
+
+void RoomEncryptionKey::parseSessionKey()
+{
+    if (mE2EKey.isEmpty()) {
+        mSessionKey.clear();
+        mE2eKeyId.clear();
+        return;
+    }
+
+    // Format E2EKey: keyId(36) + encryptedKey(base64)
+    if (mE2EKey.size() < 36) {
+        qCWarning(RUQOLA_ROOM_MEMORY_LOG) << "E2EKey too short:" << mE2EKey.size();
+        mSessionKey.clear();
+        return;
+    }
+
+    mE2eKeyId = mE2EKey.left(36); // ← UUID
+
+    // Extraire encryptedKey
+    const QString encryptedKeyBase64 = mE2EKey.mid(36); // ← base64
+
+    // Décoder le base64
+    const QByteArray encryptedKey = QByteArray::fromBase64(encryptedKeyBase64.toLatin1());
+
+    if (encryptedKey.isEmpty()) {
+        qCWarning(RUQOLA_ROOM_MEMORY_LOG) << "Failed to decode E2EKey from base64";
+        mSessionKey.clear();
+        return;
+    }
+
+    if (encryptedKey.size() != 256) {
+        qCWarning(RUQOLA_ROOM_MEMORY_LOG) << "Invalid encryptedKey size:" << encryptedKey.size() << "(expected 256 for RSA-2048)";
+        mSessionKey.clear();
+        return;
+    }
+
+    qDebug() << "E2EKey parsed - keyId:" << mE2eKeyId << "encryptedKey size:" << encryptedKey.size();
+
+    // TODO: Decrypt with user's RSA private key
+    // mSessionKey = EncryptionUtils::decryptSessionKey(encryptedKey, privateKey);
 }
 
 QString RoomEncryptionKey::e2eKeyId() const
@@ -41,3 +83,8 @@ bool RoomEncryptionKey::operator==(const RoomEncryptionKey &other) const
 {
     return other.mE2EKey == mE2EKey && other.mE2eKeyId == mE2eKeyId;
 }
+
+QByteArray RoomEncryptionKey::sessionKey() const
+{
+    return mSessionKey;
+}
diff --git a/src/core/roomencryptionkey.h b/src/core/roomencryptionkey.h
index 2ee8145594..a6df609984 100644
--- a/src/core/roomencryptionkey.h
+++ b/src/core/roomencryptionkey.h
@@ -20,8 +20,12 @@ public:
 
     [[nodiscard]] bool operator==(const RoomEncryptionKey &other) const;
 
+    [[nodiscard]] QByteArray sessionKey() const;
+
 private:
+    LIBRUQOLACORE_NO_EXPORT void parseSessionKey();
     // Encryption Key
     QString mE2EKey;
     QString mE2eKeyId;
+    QByteArray mSessionKey;
 };