Skip to content

Commit 02e7157

Browse files
committed
Introduce CryptoCrossSigningKeys container
1 parent 4c4ef0d commit 02e7157

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2023 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.api.session.crypto.crosssigning
18+
19+
/**
20+
* Container for the three cross signing keys: master, self signing and user signing.
21+
*/
22+
data class CryptoCrossSigningKeys(
23+
val masterKey: CryptoCrossSigningKey?,
24+
val selfSigningKey: CryptoCrossSigningKey?,
25+
val userSigningKey: CryptoCrossSigningKey?,
26+
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DeviceListManager.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.matrix.android.sdk.api.MatrixPatterns
2424
import org.matrix.android.sdk.api.auth.data.Credentials
2525
import org.matrix.android.sdk.api.extensions.measureMetric
2626
import org.matrix.android.sdk.api.metrics.DownloadDeviceKeysMetricsPlugin
27+
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKeys
2728
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
2829
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
2930
import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap
@@ -419,7 +420,11 @@ internal class DeviceListManager @Inject constructor(
419420
val userSigningKey = response.userSigningKeys?.get(userId)?.toCryptoModel()?.also {
420421
Timber.v("## CRYPTO | CrossSigning : Got keys for $userId : USK ${it.unpaddedBase64PublicKey}")
421422
}
422-
userDataToStore.userCrossSigningKeys[userId] = Triple(masterKey, selfSigningKey, userSigningKey)
423+
userDataToStore.userCrossSigningKeys[userId] = CryptoCrossSigningKeys(
424+
masterKey = masterKey,
425+
selfSigningKey = selfSigningKey,
426+
userSigningKey = userSigningKey
427+
)
423428
}
424429

425430
cryptoStore.storeUserDataToStore(userDataToStore)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.matrix.android.sdk.api.session.crypto.NewSessionListener
2323
import org.matrix.android.sdk.api.session.crypto.OutgoingKeyRequest
2424
import org.matrix.android.sdk.api.session.crypto.OutgoingRoomKeyRequestState
2525
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKey
26+
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKeys
2627
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo
2728
import org.matrix.android.sdk.api.session.crypto.crosssigning.PrivateKeysInfo
2829
import org.matrix.android.sdk.api.session.crypto.keysbackup.SavedKeyBackupKeyInfo
@@ -235,9 +236,7 @@ internal interface IMXCryptoStore {
235236

236237
fun storeUserCrossSigningKeys(
237238
userId: String,
238-
masterKey: CryptoCrossSigningKey?,
239-
selfSigningKey: CryptoCrossSigningKey?,
240-
userSigningKey: CryptoCrossSigningKey?
239+
cryptoCrossSigningKeys: CryptoCrossSigningKeys
241240
)
242241

243242
/**

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/UserDataToStore.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package org.matrix.android.sdk.internal.crypto.store
1818

19-
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKey
19+
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKeys
2020
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
2121

2222
internal data class UserDataToStore(
2323
val userDevices: MutableMap<String, Map<String, CryptoDeviceInfo>> = mutableMapOf(),
24-
val userCrossSigningKeys: MutableMap<String, Triple<CryptoCrossSigningKey?, CryptoCrossSigningKey?, CryptoCrossSigningKey?>> = mutableMapOf(),
24+
val userCrossSigningKeys: MutableMap<String, CryptoCrossSigningKeys> = mutableMapOf(),
2525
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import org.matrix.android.sdk.api.session.crypto.GlobalCryptoConfig
3333
import org.matrix.android.sdk.api.session.crypto.NewSessionListener
3434
import org.matrix.android.sdk.api.session.crypto.OutgoingKeyRequest
3535
import org.matrix.android.sdk.api.session.crypto.OutgoingRoomKeyRequestState
36-
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKey
36+
import org.matrix.android.sdk.api.session.crypto.crosssigning.CryptoCrossSigningKeys
3737
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo
3838
import org.matrix.android.sdk.api.session.crypto.crosssigning.PrivateKeysInfo
3939
import org.matrix.android.sdk.api.session.crypto.keysbackup.SavedKeyBackupKeyInfo
@@ -332,25 +332,21 @@ internal class RealmCryptoStore @Inject constructor(
332332

333333
override fun storeUserCrossSigningKeys(
334334
userId: String,
335-
masterKey: CryptoCrossSigningKey?,
336-
selfSigningKey: CryptoCrossSigningKey?,
337-
userSigningKey: CryptoCrossSigningKey?
335+
cryptoCrossSigningKeys: CryptoCrossSigningKeys,
338336
) {
339337
doRealmTransaction("storeUserCrossSigningKeys", realmConfiguration) { realm ->
340-
storeUserCrossSigningKeys(realm, userId, masterKey, selfSigningKey, userSigningKey)
338+
storeUserCrossSigningKeys(realm, userId, cryptoCrossSigningKeys)
341339
}
342340
}
343341

344342
private fun storeUserCrossSigningKeys(
345343
realm: Realm,
346344
userId: String,
347-
masterKey: CryptoCrossSigningKey?,
348-
selfSigningKey: CryptoCrossSigningKey?,
349-
userSigningKey: CryptoCrossSigningKey?
345+
keys: CryptoCrossSigningKeys,
350346
) {
351347
UserEntity.getOrCreate(realm, userId)
352348
.let { userEntity ->
353-
if (masterKey == null || selfSigningKey == null) {
349+
if (keys.masterKey == null || keys.selfSigningKey == null) {
354350
// The user has disabled cross signing?
355351
userEntity.crossSigningInfoEntity?.deleteOnCascade()
356352
userEntity.crossSigningInfoEntity = null
@@ -359,11 +355,11 @@ internal class RealmCryptoStore @Inject constructor(
359355
CrossSigningInfoEntity.getOrCreate(realm, userId).let { signingInfo ->
360356
// What should we do if we detect a change of the keys?
361357
val existingMaster = signingInfo.getMasterKey()
362-
if (existingMaster != null && existingMaster.publicKeyBase64 == masterKey.unpaddedBase64PublicKey) {
363-
crossSigningKeysMapper.update(existingMaster, masterKey)
358+
if (existingMaster != null && existingMaster.publicKeyBase64 == keys.masterKey.unpaddedBase64PublicKey) {
359+
crossSigningKeysMapper.update(existingMaster, keys.masterKey)
364360
} else {
365361
Timber.d("## CrossSigning MSK change for $userId")
366-
val keyEntity = crossSigningKeysMapper.map(masterKey)
362+
val keyEntity = crossSigningKeysMapper.map(keys.masterKey)
367363
signingInfo.setMasterKey(keyEntity)
368364
if (userId == this.userId) {
369365
shouldResetMyDevicesLocalTrust = true
@@ -378,11 +374,11 @@ internal class RealmCryptoStore @Inject constructor(
378374
}
379375

380376
val existingSelfSigned = signingInfo.getSelfSignedKey()
381-
if (existingSelfSigned != null && existingSelfSigned.publicKeyBase64 == selfSigningKey.unpaddedBase64PublicKey) {
382-
crossSigningKeysMapper.update(existingSelfSigned, selfSigningKey)
377+
if (existingSelfSigned != null && existingSelfSigned.publicKeyBase64 == keys.selfSigningKey.unpaddedBase64PublicKey) {
378+
crossSigningKeysMapper.update(existingSelfSigned, keys.selfSigningKey)
383379
} else {
384380
Timber.d("## CrossSigning SSK change for $userId")
385-
val keyEntity = crossSigningKeysMapper.map(selfSigningKey)
381+
val keyEntity = crossSigningKeysMapper.map(keys.selfSigningKey)
386382
signingInfo.setSelfSignedKey(keyEntity)
387383
if (userId == this.userId) {
388384
shouldResetMyDevicesLocalTrust = true
@@ -394,13 +390,13 @@ internal class RealmCryptoStore @Inject constructor(
394390
}
395391

396392
// Only for me
397-
if (userSigningKey != null) {
393+
if (keys.userSigningKey != null) {
398394
val existingUSK = signingInfo.getUserSigningKey()
399-
if (existingUSK != null && existingUSK.publicKeyBase64 == userSigningKey.unpaddedBase64PublicKey) {
400-
crossSigningKeysMapper.update(existingUSK, userSigningKey)
395+
if (existingUSK != null && existingUSK.publicKeyBase64 == keys.userSigningKey.unpaddedBase64PublicKey) {
396+
crossSigningKeysMapper.update(existingUSK, keys.userSigningKey)
401397
} else {
402398
Timber.d("## CrossSigning USK change for $userId")
403-
val keyEntity = crossSigningKeysMapper.map(userSigningKey)
399+
val keyEntity = crossSigningKeysMapper.map(keys.userSigningKey)
404400
signingInfo.setUserSignedKey(keyEntity)
405401
if (userId == this.userId) {
406402
shouldResetMyDevicesLocalTrust = true
@@ -1862,7 +1858,7 @@ internal class RealmCryptoStore @Inject constructor(
18621858
storeUserDevices(realm, it.key, it.value)
18631859
}
18641860
userDataToStore.userCrossSigningKeys.forEach {
1865-
storeUserCrossSigningKeys(realm, it.key, it.value.first, it.value.second, it.value.third)
1861+
storeUserCrossSigningKeys(realm, it.key, it.value)
18661862
}
18671863
}
18681864
}

0 commit comments

Comments
 (0)