@@ -37,7 +37,9 @@ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupLastVersio
37
37
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
38
38
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupStateListener
39
39
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrust
40
+ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupVersionTrustSignature
40
41
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersion
42
+ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysVersionResult
41
43
import org.matrix.android.sdk.api.session.crypto.keysbackup.MegolmBackupCreationInfo
42
44
import org.matrix.android.sdk.api.session.crypto.keysbackup.toKeysVersionResult
43
45
import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
@@ -133,6 +135,7 @@ class KeysBackupTest : InstrumentedTest {
133
135
@Test
134
136
fun createKeysBackupVersionTest () {
135
137
val bobSession = testHelper.createAccount(TestConstants .USER_BOB , KeysBackupTestConstants .defaultSessionParams)
138
+ cryptoTestHelper.initializeCrossSigning(bobSession)
136
139
137
140
val keysBackup = bobSession.cryptoService().keysBackupService()
138
141
@@ -147,13 +150,46 @@ class KeysBackupTest : InstrumentedTest {
147
150
assertFalse(keysBackup.isEnabled)
148
151
149
152
// Create the version
150
- testHelper.doSync<KeysVersion > {
153
+ val version = testHelper.doSync<KeysVersion > {
151
154
keysBackup.createKeysBackupVersion(megolmBackupCreationInfo, it)
152
155
}
153
156
154
157
// Backup must be enable now
155
158
assertTrue(keysBackup.isEnabled)
156
159
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
+
157
193
stateObserver.stopAndCheckStates(null )
158
194
testHelper.signOutAndClose(bobSession)
159
195
}
@@ -272,10 +308,12 @@ class KeysBackupTest : InstrumentedTest {
272
308
assertNotNull(decryption)
273
309
// - Check decryptKeyBackupData() returns stg
274
310
val sessionData = keysBackup
275
- .decryptKeyBackupData(keyBackupData,
311
+ .decryptKeyBackupData(
312
+ keyBackupData,
276
313
session.olmInboundGroupSession!! .sessionIdentifier(),
277
314
cryptoTestData.roomId,
278
- decryption!! )
315
+ decryption!!
316
+ )
279
317
assertNotNull(sessionData)
280
318
// - Compare the decrypted megolm key with the original one
281
319
keysBackupTestHelper.assertKeysEquals(session.exportKeys(), sessionData)
@@ -297,7 +335,8 @@ class KeysBackupTest : InstrumentedTest {
297
335
298
336
// - Restore the e2e backup from the homeserver
299
337
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!! ,
301
340
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
302
341
null ,
303
342
null ,
@@ -680,7 +719,8 @@ class KeysBackupTest : InstrumentedTest {
680
719
val steps = ArrayList <StepProgressListener .Step >()
681
720
682
721
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!! ,
684
724
password,
685
725
null ,
686
726
null ,
@@ -771,7 +811,8 @@ class KeysBackupTest : InstrumentedTest {
771
811
772
812
// - Restore the e2e backup with the recovery key.
773
813
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!! ,
775
816
testData.prepareKeysBackupDataResult.megolmBackupCreationInfo.recoveryKey,
776
817
null ,
777
818
null ,
@@ -850,7 +891,7 @@ class KeysBackupTest : InstrumentedTest {
850
891
assertTrue(keysBackupVersionTrust.usable)
851
892
assertEquals(1 , keysBackupVersionTrust.signatures.size)
852
893
853
- val signature = keysBackupVersionTrust.signatures[0 ]
894
+ val signature = keysBackupVersionTrust.signatures[0 ] as KeysBackupVersionTrustSignature . DeviceSignature
854
895
assertTrue(signature.valid)
855
896
assertNotNull(signature.device)
856
897
assertEquals(cryptoTestData.firstSession.cryptoService().getMyDevice().deviceId, signature.deviceId)
@@ -1055,7 +1096,11 @@ class KeysBackupTest : InstrumentedTest {
1055
1096
assertFalse(keysBackup2.isEnabled)
1056
1097
1057
1098
// - 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
+ )
1059
1104
1060
1105
// -> Backup should automatically enable on the new device
1061
1106
val latch4 = CountDownLatch (1 )
0 commit comments