Skip to content

Commit e988804

Browse files
committed
Add test to check MSK signature on backup
1 parent c66697c commit e988804

File tree

1 file changed

+53
-8
lines changed
  • matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/keysbackup

1 file changed

+53
-8
lines changed

matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/keysbackup/KeysBackupTest.kt

+53-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupLastVersio
3737
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
3838
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupStateListener
3939
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrust
40+
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrustSignature
4041
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersion
42+
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersionResult
4143
import org.matrix.android.sdk.api.session.crypto.keysbackup.MegolmBackupCreationInfo
4244
import org.matrix.android.sdk.api.session.crypto.keysbackup.toKeysVersionResult
4345
import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
@@ -133,6 +135,7 @@ class KeysBackupTest : InstrumentedTest {
133135
@Test
134136
fun createKeysBackupVersionTest() {
135137
val bobSession = testHelper.createAccount(TestConstants.USER_BOB, KeysBackupTestConstants.defaultSessionParams)
138+
cryptoTestHelper.initializeCrossSigning(bobSession)
136139

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

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

149152
// Create the version
150-
testHelper.doSync<KeysVersion> {
153+
val version = testHelper.doSync<KeysVersion> {
151154
keysBackup.createKeysBackupVersion(megolmBackupCreationInfo, it)
152155
}
153156

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

160+
// Check that it's signed with MSK
161+
val versionResult = testHelper.doSync<KeysVersionResult?> {
162+
keysBackup.getVersion(version.version, it)
163+
}
164+
val trust = testHelper.doSync<KeysBackupVersionTrust> {
165+
keysBackup.getKeysBackupTrust(versionResult!!, it)
166+
}
167+
168+
assertEquals("Should have 2 signatures", 2, trust.signatures.size)
169+
170+
trust.signatures
171+
.firstOrNull { it is KeysBackupVersionTrustSignature.DeviceSignature }
172+
.let {
173+
assertNotNull("Should be signed by a device", it)
174+
it as KeysBackupVersionTrustSignature.DeviceSignature
175+
}.let {
176+
assertEquals("Should be signed by current device", bobSession.sessionParams.deviceId, it.deviceId)
177+
assertTrue("Signature should be valid", it.valid)
178+
}
179+
180+
trust.signatures
181+
.firstOrNull { it is KeysBackupVersionTrustSignature.UserSignature }
182+
.let {
183+
assertNotNull("Should be signed by a user", it)
184+
it as KeysBackupVersionTrustSignature.UserSignature
185+
}.let {
186+
val msk = bobSession.cryptoService().crossSigningService()
187+
.getMyCrossSigningKeys()?.masterKey()?.unpaddedBase64PublicKey
188+
assertEquals("Should be signed by my msk 1", msk, it.keyId)
189+
assertEquals("Should be signed by my msk 2", msk, it.cryptoCrossSigningKey?.unpaddedBase64PublicKey)
190+
assertTrue("Signature should be valid", it.valid)
191+
}
192+
157193
stateObserver.stopAndCheckStates(null)
158194
testHelper.signOutAndClose(bobSession)
159195
}
@@ -272,10 +308,12 @@ class KeysBackupTest : InstrumentedTest {
272308
assertNotNull(decryption)
273309
// - Check decryptKeyBackupData() returns stg
274310
val sessionData = keysBackup
275-
.decryptKeyBackupData(keyBackupData,
311+
.decryptKeyBackupData(
312+
keyBackupData,
276313
session.olmInboundGroupSession!!.sessionIdentifier(),
277314
cryptoTestData.roomId,
278-
decryption!!)
315+
decryption!!
316+
)
279317
assertNotNull(sessionData)
280318
// - Compare the decrypted megolm key with the original one
281319
keysBackupTestHelper.assertKeysEquals(session.exportKeys(), sessionData)
@@ -297,7 +335,8 @@ class KeysBackupTest : InstrumentedTest {
297335

298336
// - Restore the e2e backup from the homeserver
299337
val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
300-
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
338+
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(
339+
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
301340
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
302341
null,
303342
null,
@@ -680,7 +719,8 @@ class KeysBackupTest : InstrumentedTest {
680719
val steps = ArrayList<StepProgressListener.Step>()
681720

682721
val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
683-
testData.aliceSession2.cryptoService().keysBackupService().restoreKeyBackupWithPassword(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
722+
testData.aliceSession2.cryptoService().keysBackupService().restoreKeyBackupWithPassword(
723+
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
684724
password,
685725
null,
686726
null,
@@ -771,7 +811,8 @@ class KeysBackupTest : InstrumentedTest {
771811

772812
// - Restore the e2e backup with the recovery key.
773813
val importRoomKeysResult = testHelper.doSync<ImportRoomKeysResult> {
774-
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
814+
testData.aliceSession2.cryptoService().keysBackupService().restoreKeysWithRecoveryKey(
815+
testData.aliceSession2.cryptoService().keysBackupService().keysBackupVersion!!,
775816
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
776817
null,
777818
null,
@@ -850,7 +891,7 @@ class KeysBackupTest : InstrumentedTest {
850891
assertTrue(keysBackupVersionTrust.usable)
851892
assertEquals(1, keysBackupVersionTrust.signatures.size)
852893

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

10571098
// - Validate the old device from the new one
1058-
aliceSession2.cryptoService().setDeviceVerification(DeviceTrustLevel(crossSigningVerified = false, locallyVerified = true), aliceSession2.myUserId, oldDeviceId)
1099+
aliceSession2.cryptoService().setDeviceVerification(
1100+
DeviceTrustLevel(crossSigningVerified = false, locallyVerified = true),
1101+
aliceSession2.myUserId,
1102+
oldDeviceId
1103+
)
10591104

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

0 commit comments

Comments
 (0)