Skip to content

Commit

Permalink
Add test to check MSK signature on backup
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed May 5, 2022
1 parent c66697c commit e988804
Showing 1 changed file with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupLastVersio
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupStateListener
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrust
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrustSignature
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersion
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersionResult
import org.matrix.android.sdk.api.session.crypto.keysbackup.MegolmBackupCreationInfo
import org.matrix.android.sdk.api.session.crypto.keysbackup.toKeysVersionResult
import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
Expand Down Expand Up @@ -133,6 +135,7 @@ class KeysBackupTest : InstrumentedTest {
@Test
fun createKeysBackupVersionTest() {
val bobSession = testHelper.createAccount(TestConstants.USER_BOB, KeysBackupTestConstants.defaultSessionParams)
cryptoTestHelper.initializeCrossSigning(bobSession)

val keysBackup = bobSession.cryptoService().keysBackupService()

Expand All @@ -147,13 +150,46 @@ class KeysBackupTest : InstrumentedTest {
assertFalse(keysBackup.isEnabled)

// Create the version
testHelper.doSync<KeysVersion> {
val version = testHelper.doSync<KeysVersion> {
keysBackup.createKeysBackupVersion(megolmBackupCreationInfo, it)
}

// Backup must be enable now
assertTrue(keysBackup.isEnabled)

// Check that it's signed with MSK
val versionResult = testHelper.doSync<KeysVersionResult?> {
keysBackup.getVersion(version.version, it)
}
val trust = testHelper.doSync<KeysBackupVersionTrust> {
keysBackup.getKeysBackupTrust(versionResult!!, it)
}

assertEquals("Should have 2 signatures", 2, trust.signatures.size)

trust.signatures
.firstOrNull { it is KeysBackupVersionTrustSignature.DeviceSignature }
.let {
assertNotNull("Should be signed by a device", it)
it as KeysBackupVersionTrustSignature.DeviceSignature
}.let {
assertEquals("Should be signed by current device", bobSession.sessionParams.deviceId, it.deviceId)
assertTrue("Signature should be valid", it.valid)
}

trust.signatures
.firstOrNull { it is KeysBackupVersionTrustSignature.UserSignature }
.let {
assertNotNull("Should be signed by a user", it)
it as KeysBackupVersionTrustSignature.UserSignature
}.let {
val msk = bobSession.cryptoService().crossSigningService()
.getMyCrossSigningKeys()?.masterKey()?.unpaddedBase64PublicKey
assertEquals("Should be signed by my msk 1", msk, it.keyId)
assertEquals("Should be signed by my msk 2", msk, it.cryptoCrossSigningKey?.unpaddedBase64PublicKey)
assertTrue("Signature should be valid", it.valid)
}

stateObserver.stopAndCheckStates(null)
testHelper.signOutAndClose(bobSession)
}
Expand Down Expand Up @@ -272,10 +308,12 @@ class KeysBackupTest : InstrumentedTest {
assertNotNull(decryption)
// - Check decryptKeyBackupData() returns stg
val sessionData = keysBackup
.decryptKeyBackupData(keyBackupData,
.decryptKeyBackupData(
keyBackupData,
session.olmInboundGroupSession!!.sessionIdentifier(),
cryptoTestData.roomId,
decryption!!)
decryption!!
)
assertNotNull(sessionData)
// - Compare the decrypted megolm key with the original one
keysBackupTestHelper.assertKeysEquals(session.exportKeys(), sessionData)
Expand All @@ -297,7 +335,8 @@ class KeysBackupTest : InstrumentedTest {

// - Restore the e2e backup from the homeserver
val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
null,
null,
Expand Down Expand Up @@ -680,7 +719,8 @@ class KeysBackupTest : InstrumentedTest {
val steps = ArrayList<StepProgressListener.Step>()

val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
testData.aliceSession2.cryptoService().keysBackupService().restoreKeyBackupWithPassword(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
testData.aliceSession2.cryptoService().keysBackupService().restoreKeyBackupWithPassword(
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
password,
null,
null,
Expand Down Expand Up @@ -771,7 +811,8 @@ class KeysBackupTest : InstrumentedTest {

// - Restore the e2e backup with the recovery key.
val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
null,
null,
Expand Down Expand Up @@ -850,7 +891,7 @@ class KeysBackupTest : InstrumentedTest {
assertTrue(keysBackupVersionTrust.usable)
assertEquals(1, keysBackupVersionTrust.signatures.size)

val signature = keysBackupVersionTrust.signatures[0]
val signature = keysBackupVersionTrust.signatures[0] as KeysBackupVersionTrustSignature.DeviceSignature
assertTrue(signature.valid)
assertNotNull(signature.device)
assertEquals(cryptoTestData.firstSession.cryptoService().getMyDevice().deviceId, signature.deviceId)
Expand Down Expand Up @@ -1055,7 +1096,11 @@ class KeysBackupTest : InstrumentedTest {
assertFalse(keysBackup2.isEnabled)

// - Validate the old device from the new one
aliceSession2.cryptoService().setDeviceVerification(DeviceTrustLevel(crossSigningVerified = false, locallyVerified = true), aliceSession2.myUserId, oldDeviceId)
aliceSession2.cryptoService().setDeviceVerification(
DeviceTrustLevel(crossSigningVerified = false, locallyVerified = true),
aliceSession2.myUserId,
oldDeviceId
)

// -> Backup should automatically enable on the new device
val latch4 = CountDownLatch(1)
Expand Down

0 comments on commit e988804

Please sign in to comment.