diff --git a/lib/src/event.dart b/lib/src/event.dart index 8558781..9963417 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -248,6 +248,8 @@ class Event { return MessageTypes.Sticker; case "m.location": return MessageTypes.Location; + case "m.bad.encrypted": + return MessageTypes.BadEncrypted; default: if (type == EventTypes.Message) { return MessageTypes.Text; @@ -378,7 +380,8 @@ class Event { /// Searches for the reply event in the given timeline. Future getReplyEvent(Timeline timeline) async { if (!isReply) return null; - final String replyEventId = content['m.relates_to']['m.in_reply_to']['event_id']; + final String replyEventId = + content['m.relates_to']['m.in_reply_to']['event_id']; return await timeline.getEventById(replyEventId); } } @@ -394,6 +397,7 @@ enum MessageTypes { Location, Reply, Sticker, + BadEncrypted, None, } diff --git a/lib/src/room.dart b/lib/src/room.dart index debadb3..d53f716 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1487,30 +1487,40 @@ class Room { /// Decrypts the given [event] with one of the available ingoingGroupSessions. Event decryptGroupMessage(Event event) { - if (!client.encryptionEnabled) throw ("Encryption is not enabled"); - if (event.content["algorithm"] != "m.megolm.v1.aes-sha2") { - throw ("Unknown encryption algorithm"); - } - final String sessionId = event.content["session_id"]; - if (!sessionKeys.containsKey(sessionId)) { - throw ("Unknown session id"); - } - final olm.DecryptResult decryptResult = sessionKeys[sessionId] - .inboundGroupSession - .decrypt(event.content["ciphertext"]); - final String messageIndexKey = - event.eventId + event.time.millisecondsSinceEpoch.toString(); - if (sessionKeys[sessionId].indexes.containsKey(messageIndexKey) && - sessionKeys[sessionId].indexes[messageIndexKey] != - decryptResult.message_index) { - throw ("Invalid message index"); - } - sessionKeys[sessionId].indexes[messageIndexKey] = - decryptResult.message_index; - // 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. + Map decryptedPayload; + try { + if (!client.encryptionEnabled) throw ("Encryption is not enabled"); + if (event.content["algorithm"] != "m.megolm.v1.aes-sha2") { + throw ("Unknown encryption algorithm"); + } + final String sessionId = event.content["session_id"]; + if (!sessionKeys.containsKey(sessionId)) { + throw ("Unknown session id"); + } + final olm.DecryptResult decryptResult = sessionKeys[sessionId] + .inboundGroupSession + .decrypt(event.content["ciphertext"]); + final String messageIndexKey = + event.eventId + event.time.millisecondsSinceEpoch.toString(); + if (sessionKeys[sessionId].indexes.containsKey(messageIndexKey) && + sessionKeys[sessionId].indexes[messageIndexKey] != + decryptResult.message_index) { + throw ("Invalid message index"); + } + sessionKeys[sessionId].indexes[messageIndexKey] = + decryptResult.message_index; + // 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. - final Map decryptedPayload = - json.decode(decryptResult.plaintext); + decryptedPayload = json.decode(decryptResult.plaintext); + } catch (exception) { + decryptedPayload = { + "content": { + "msgtype": "m.bad.encrypted", + "body": exception.toString(), + }, + "type": "m.room.message", + }; + } return Event( content: decryptedPayload["content"], typeKey: decryptedPayload["type"],