Skip to content

Commit c39d806

Browse files
Remove sharedHistory from OlmInboundGroupSessionWrapper2 while there are migration issues, and use only the equivalent DB entity value
1 parent b192da3 commit c39d806

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,13 +1387,14 @@ internal class DefaultCryptoService @Inject constructor(
13871387
val deviceId = it.key
13881388
sessionInfoSet?.mapNotNull { sessionInfoPair ->
13891389
// Get inbound session from sessionId and sessionKey
1390-
cryptoStore.getInboundGroupSession(sessionInfoPair.first, sessionInfoPair.second)
1391-
}?.filter { inboundGroupSession ->
1392-
// Filter only sessions with sharedHistory enabled
1393-
inboundGroupSession.sharedHistory
1390+
cryptoStore.getInboundGroupSession(
1391+
sessionId = sessionInfoPair.first,
1392+
senderKey = sessionInfoPair.second,
1393+
sharedHistory = true
1394+
)
13941395
}?.forEach { inboundGroupSession ->
1395-
// Share the session to userId with deviceId
1396-
val exportedKeys = inboundGroupSession.exportKeys()
1396+
// Share the sharable session to userId with deviceId
1397+
val exportedKeys = inboundGroupSession.exportKeys(sharedHistory = true)
13971398
val algorithm = exportedKeys?.algorithm
13981399
val decryptor = roomDecryptorProvider.getRoomDecryptor(roomId, algorithm)
13991400
decryptor?.shareKeysWithDevice(exportedKeys, deviceId, userId)
@@ -1404,26 +1405,6 @@ internal class DefaultCryptoService @Inject constructor(
14041405
}
14051406
}
14061407

1407-
override fun sendSharedHistoryKeys(roomId: String, userId: String) {
1408-
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
1409-
val userDevices = cryptoStore.getUserDevices(userId)
1410-
userDevices?.forEach {
1411-
// Lets share our existing inbound sessions for every user device
1412-
val deviceId = it.key
1413-
val inboundSessions = cryptoStore.getInboundGroupSessions(roomId)
1414-
inboundSessions.filter { inboundGroupSession ->
1415-
inboundGroupSession.sharedHistory
1416-
}.forEach { inboundGroupSession ->
1417-
// Share the session to userId with deviceId
1418-
val exportedKeys = inboundGroupSession.exportKeys()
1419-
val algorithm = exportedKeys?.algorithm
1420-
val decryptor = roomDecryptorProvider.getRoomDecryptor(roomId, algorithm)
1421-
decryptor?.shareKeysWithDevice(exportedKeys, deviceId, userId)
1422-
Timber.i("## CRYPTO | Sharing inbound session")
1423-
}
1424-
}
1425-
}
1426-
}
14271408
/* ==========================================================================================
14281409
* For test only
14291410
* ========================================================================================== */

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,13 @@ internal data class MegolmSessionData(
6969
* Devices which forwarded this session to us (normally empty).
7070
*/
7171
@Json(name = "forwarding_curve25519_key_chain")
72-
val forwardingCurve25519KeyChain: List<String>? = null
72+
val forwardingCurve25519KeyChain: List<String>? = null,
73+
74+
/**
75+
* Flag that indicates whether or not the current inboundSession will be shared to
76+
* invited users to decrypt past messages
77+
*/
78+
// When this feature lands in spec name = shared_history should be used
79+
@Json(name = "org.matrix.msc3061.shared_history")
80+
val sharedHistory: Boolean = false,
7381
)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ internal class OlmInboundGroupSessionWrapper2 : Serializable {
4343
// Devices which forwarded this session to us (normally empty).
4444
var forwardingCurve25519KeyChain: List<String>? = ArrayList()
4545

46-
// Flag that indicates whether or not the current inboundSession will be shared to
47-
// invited users to decrypt past messages
48-
var sharedHistory: Boolean = false
49-
5046
/**
5147
* @return the first known message index
5248
*/
@@ -110,10 +106,10 @@ internal class OlmInboundGroupSessionWrapper2 : Serializable {
110106
/**
111107
* Export the inbound group session keys
112108
* @param index the index to export. If null, the first known index will be used
113-
*
109+
* @param sharedHistory the flag that indicates whether or not the session can be shared
114110
* @return the inbound group session as MegolmSessionData if the operation succeeds
115111
*/
116-
fun exportKeys(index: Long? = null): MegolmSessionData? {
112+
fun exportKeys(sharedHistory: Boolean = false, index: Long? = null): MegolmSessionData? {
117113
return try {
118114
if (null == forwardingCurve25519KeyChain) {
119115
forwardingCurve25519KeyChain = ArrayList()
@@ -135,7 +131,8 @@ internal class OlmInboundGroupSessionWrapper2 : Serializable {
135131
roomId = roomId,
136132
sessionId = safeOlmInboundGroupSession.sessionIdentifier(),
137133
sessionKey = safeOlmInboundGroupSession.export(wantedIndex),
138-
algorithm = MXCRYPTO_ALGORITHM_MEGOLM
134+
algorithm = MXCRYPTO_ALGORITHM_MEGOLM,
135+
sharedHistory = sharedHistory
139136
)
140137
} catch (e: Exception) {
141138
Timber.e(e, "## export() : senderKey $senderKey failed")

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ internal interface IMXCryptoStore {
324324
*/
325325
fun getInboundGroupSession(sessionId: String, senderKey: String): OlmInboundGroupSessionWrapper2?
326326

327+
/**
328+
* Retrieve an inbound group session, filtering shared history.
329+
*
330+
* @param sessionId the session identifier.
331+
* @param senderKey the base64-encoded curve25519 key of the sender.
332+
* @param sharedHistory filter inbound session with respect to shared history field
333+
* @return an inbound group session.
334+
*/
335+
fun getInboundGroupSession(sessionId: String, senderKey: String, sharedHistory: Boolean): OlmInboundGroupSessionWrapper2?
336+
327337
/**
328338
* Get the current outbound group session for this encrypted room
329339
*/

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ internal class RealmCryptoStore @Inject constructor(
754754
val shouldShareHistory = session.roomId?.let { roomId ->
755755
CryptoRoomEntity.getById(realm, roomId)?.shouldShareHistory
756756
} ?: false
757-
session.sharedHistory = shouldShareHistory
758757
val key = OlmInboundGroupSessionEntity.createPrimaryKey(sessionIdentifier, session.senderKey)
759758

760759
val realmOlmInboundGroupSession = OlmInboundGroupSessionEntity().apply {
@@ -783,6 +782,17 @@ internal class RealmCryptoStore @Inject constructor(
783782
}
784783
}
785784

785+
override fun getInboundGroupSession(sessionId: String, senderKey: String, sharedHistory: Boolean): OlmInboundGroupSessionWrapper2? {
786+
val key = OlmInboundGroupSessionEntity.createPrimaryKey(sessionId, senderKey)
787+
return doWithRealm(realmConfiguration) {
788+
it.where<OlmInboundGroupSessionEntity>()
789+
.equalTo(OlmInboundGroupSessionEntityFields.SHARED_HISTORY, sharedHistory)
790+
.equalTo(OlmInboundGroupSessionEntityFields.PRIMARY_KEY, key)
791+
.findFirst()
792+
?.getInboundGroupSession()
793+
}
794+
}
795+
786796
override fun getCurrentOutboundGroupSessionForRoom(roomId: String): OutboundGroupSessionWrapper? {
787797
return doWithRealm(realmConfiguration) { realm ->
788798
realm.where<CryptoRoomEntity>()

0 commit comments

Comments
 (0)