element-hq / element-ios

A glossy Matrix collaboration client for iOS
https://element.io
Apache License 2.0
1.72k stars 480 forks source link

Newly created Secure Backup Key refused by Element iOS #7737

Open NicolasBuquet opened 5 months ago

NicolasBuquet commented 5 months ago

Steps to reproduce

  1. Element iOS launched on device, Element web launched in browser on same account as Element iOS
  2. Regenerate a Secure Backup key on Element web
  3. Eventually disconnect Element iOS from internet during 1 minute during step 2
  4. on Element iOS, try to recover message from secure backup using the newly generated Secure Backup key from web session

Outcome

What did you expect?

I expected the Element iOS app to accept the new Secure backup key and restore the messages.

What happened instead?

The Secure Backup key is refused by Element iOS although it is the active Secure Backup code on the account.

Video showing the bug

https://github.com/element-hq/element-ios/assets/18608158/dc7cbf12-e35d-4896-ac1b-cf4eeccf5da5

Your phone model

iPhone 15 Pro simulator

Operating system version

iOS 17.0

Application version

Element iOS 1.11.5

Homeserver

matrix.org

Will you send logs?

No

NicolasBuquet commented 5 months ago

The Secure backup key verification fails here in the matrix-ios-sdk :

func validPrivateKey(forRecoveryKey recoveryKey: String, for keyBackupVersion: MXKeyBackupVersion) throws -> Data {
        let key = try BackupRecoveryKey.fromBase58(key: recoveryKey)
        guard key.megolmV1PublicKey().publicKey == publicKey(for: keyBackupVersion) else {
            throw Error.invalidPrivateKey
        }

        let privateKey = try MXRecoveryKey.decode(recoveryKey)
        log.debug("Created valid private key from recovery key")
        return privateKey
    }

Account data sample :

Printing description of JSONDictionary:
{
    algorithm = "m.megolm_backup.v1.curve25519-aes-sha2";
    "auth_data" =     {
        "public_key" = qUqwkb21yIJbeconn6wPivnpydHMBhnbHNV8QWyxrwY;
        signatures =         {
            "@xxxxx.yyyyyyy-beta.gouv.fr:agent.dinum.tchap.gouv.fr" =             {
                "ed25519:YPJAZRHKVW" = "xexoRSskmVDb4HT0did...G1wXsOzas6R7LfNzLA/eU44UdxgNQZHah9w2sborqf/+GBw";
                "ed25519:r5lpLNTV+66IGMBbJFKk2uJGmmmkHP7Zo1oedzyBzfM" = "Mx6SdshgOm+zlYn0RITvXjR1ku...T0r4B0VoSG33Ut5sF7lDFRKaATTS1XNBmWPUNiErlICg";
            };
        };
    };
    count = 8934;
    etag = 45;
    version = 30;
}

Result of publicKey evaluation inside Xcode session :

recoveryKey.megolmV1PublicKey().publicKey
    [MXCryptoSDK] DEBUG matrix_sdk_crypto_ffi::backup_recovery_key: megolm_v1_public_key
    "yE6IkfNFJT8wltlLtn/w7zXPSLSGLVFl/wFEgHi2KXQ"

key.megolmV1PublicKey().publicKey
    [MXCryptoSDK] DEBUG matrix_sdk_crypto_ffi::backup_recovery_key: megolm_v1_public_key
    "5IvnKhpDt4e/diKoGSU7Vc2rAiNmZHtFXJvX0wh1iFU"

@pixlwave can you evaluate the severity of this issue?