Merge branch 'room-fix-message-index-error' into 'master'

[Room] Fix message index error

See merge request famedly/famedlysdk!198
This commit is contained in:
Christian Pauly 2020-02-18 07:42:52 +00:00
commit bb1731a4c1

View file

@ -101,9 +101,7 @@ class Room {
if (_outboundGroupSession == null) return; if (_outboundGroupSession == null) return;
await client.storeAPI?.setItem( await _storeOutboundGroupSession();
"/clients/${client.deviceID}/rooms/${this.id}/outbound_group_session",
_outboundGroupSession.pickle(client.userID));
// Add as an inboundSession to the [sessionKeys]. // Add as an inboundSession to the [sessionKeys].
Map<String, dynamic> rawSession = { Map<String, dynamic> rawSession = {
"algorithm": "m.megolm.v1.aes-sha2", "algorithm": "m.megolm.v1.aes-sha2",
@ -124,6 +122,13 @@ class Room {
return; return;
} }
Future<void> _storeOutboundGroupSession() async {
await client.storeAPI?.setItem(
"/clients/${client.deviceID}/rooms/${this.id}/outbound_group_session",
_outboundGroupSession.pickle(client.userID));
return;
}
/// Clears the existing outboundGroupSession. /// Clears the existing outboundGroupSession.
Future<void> clearOutboundGroupSession() async { Future<void> clearOutboundGroupSession() async {
await client.storeAPI?.setItem( await client.storeAPI?.setItem(
@ -1482,6 +1487,7 @@ class Room {
"sender_key": client.identityKey, "sender_key": client.identityKey,
"session_id": _outboundGroupSession.session_id(), "session_id": _outboundGroupSession.session_id(),
}; };
await _storeOutboundGroupSession();
return encryptedPayload; return encryptedPayload;
} }
@ -1489,13 +1495,15 @@ class Room {
Event decryptGroupMessage(Event event) { Event decryptGroupMessage(Event event) {
Map<String, dynamic> decryptedPayload; Map<String, dynamic> decryptedPayload;
try { try {
if (!client.encryptionEnabled) throw ("Encryption is not enabled"); if (!client.encryptionEnabled) {
throw ("Encryption is not enabled in your client.");
}
if (event.content["algorithm"] != "m.megolm.v1.aes-sha2") { if (event.content["algorithm"] != "m.megolm.v1.aes-sha2") {
throw ("Unknown encryption algorithm"); throw ("Unknown encryption algorithm.");
} }
final String sessionId = event.content["session_id"]; final String sessionId = event.content["session_id"];
if (!sessionKeys.containsKey(sessionId)) { if (!sessionKeys.containsKey(sessionId)) {
throw ("Unknown session id"); throw ("The sender has not sent us the session key.");
} }
final olm.DecryptResult decryptResult = sessionKeys[sessionId] final olm.DecryptResult decryptResult = sessionKeys[sessionId]
.inboundGroupSession .inboundGroupSession
@ -1505,10 +1513,14 @@ class Room {
if (sessionKeys[sessionId].indexes.containsKey(messageIndexKey) && if (sessionKeys[sessionId].indexes.containsKey(messageIndexKey) &&
sessionKeys[sessionId].indexes[messageIndexKey] != sessionKeys[sessionId].indexes[messageIndexKey] !=
decryptResult.message_index) { decryptResult.message_index) {
throw ("Invalid message index"); if ((_outboundGroupSession?.session_id() ?? "") == sessionId) {
clearOutboundGroupSession();
}
throw ("The secure channel with the sender was corrupted.");
} }
sessionKeys[sessionId].indexes[messageIndexKey] = sessionKeys[sessionId].indexes[messageIndexKey] =
decryptResult.message_index; decryptResult.message_index;
_storeOutboundGroupSession();
// TODO: The client should check that the sender's fingerprint key matches the keys.ed25519 property of the event which established the Megolm session when marking the event as verified. // TODO: The client should check that the sender's fingerprint key matches the keys.ed25519 property of the event which established the Megolm session when marking the event as verified.
decryptedPayload = json.decode(decryptResult.plaintext); decryptedPayload = json.decode(decryptResult.plaintext);