From 8b13df8c9d242484aaba98e47bb6250bb5167942 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 8 Jan 2021 09:01:28 +0100 Subject: [PATCH] refactor: Add json parsing for encryption and encrypted content --- lib/encryption/encryption.dart | 15 +++++++++------ lib/encryption/key_manager.dart | 14 +++++--------- lib/encryption/olm_manager.dart | 13 +++++++------ lib/src/room.dart | 5 ++--- pubspec.yaml | 4 +--- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index 7bf18b13..12239ddf 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -149,16 +149,19 @@ class Encryption { } Event decryptRoomEventSync(String roomId, Event event) { + final content = event.parsedRoomEncryptedContent; if (event.type != EventTypes.Encrypted || - event.content['ciphertext'] == null) return event; + content.ciphertextMegolm == null) { + return event; + } Map decryptedPayload; var canRequestSession = false; try { - if (event.content['algorithm'] != AlgorithmTypes.megolmV1AesSha2) { + if (content.algorithm != AlgorithmTypes.megolmV1AesSha2) { throw DecryptException(DecryptException.unknownAlgorithm); } - final String sessionId = event.content['session_id']; - final String senderKey = event.content['sender_key']; + final sessionId = content.sessionId; + final senderKey = content.senderKey; final inboundGroupSession = keyManager.getInboundGroupSession(roomId, sessionId, senderKey); if (inboundGroupSession == null) { @@ -169,7 +172,7 @@ class Encryption { canRequestSession = true; final decryptResult = inboundGroupSession.inboundGroupSession - .decrypt(event.content['ciphertext']); + .decrypt(content.ciphertextMegolm); canRequestSession = false; // we can't have the key be an int, else json-serializing will fail, thus we need it to be a string final messageIndexKey = 'key-' + decryptResult.message_index.toString(); @@ -204,7 +207,7 @@ class Encryption { ?.outboundGroupSession ?.session_id() ?? '') == - event.content['session_id']) { + content.sessionId) { runInRoot(() => keyManager.clearOrUseOutboundGroupSession(roomId, wipe: true)); } diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 00f954c4..d15d9004 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -275,15 +275,11 @@ class KeyManager { } if (!wipe) { // first check if it needs to be rotated - final encryptionContent = room.getState(EventTypes.Encryption)?.content; - final maxMessages = encryptionContent != null && - encryptionContent['rotation_period_msgs'] is int - ? encryptionContent['rotation_period_msgs'] - : 100; - final maxAge = encryptionContent != null && - encryptionContent['rotation_period_ms'] is int - ? encryptionContent['rotation_period_ms'] - : 604800000; // default of one week + final encryptionContent = + room.getState(EventTypes.Encryption)?.parsedRoomEncryptionContent; + final maxMessages = encryptionContent?.rotationPeriodMsgs ?? 100; + final maxAge = encryptionContent?.rotationPeriodMs ?? + 604800000; // default of one week if (sess.sentMessages >= maxMessages || sess.creationTime .add(Duration(milliseconds: maxAge)) diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index 1c961ca9..7ccab5d7 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -247,16 +247,17 @@ class OlmManager { if (event.type != EventTypes.Encrypted) { return event; } - if (event.content['algorithm'] != AlgorithmTypes.olmV1Curve25519AesSha2) { + final content = event.parsedRoomEncryptedContent; + if (content.algorithm != AlgorithmTypes.olmV1Curve25519AesSha2) { throw DecryptException(DecryptException.unknownAlgorithm); } - if (!event.content['ciphertext'].containsKey(identityKey)) { + if (!content.ciphertextOlm.containsKey(identityKey)) { throw DecryptException(DecryptException.isntSentForThisDevice); } String plaintext; - final String senderKey = event.content['sender_key']; - final String body = event.content['ciphertext'][identityKey]['body']; - final int type = event.content['ciphertext'][identityKey]['type']; + final senderKey = content.senderKey; + final body = content.ciphertextOlm[identityKey].body; + final type = content.ciphertextOlm[identityKey].type; if (type != 0 && type != 1) { throw DecryptException(DecryptException.unknownMessageType); } @@ -429,7 +430,7 @@ class OlmManager { if (event.type != EventTypes.Encrypted) { return event; } - final senderKey = event.content['sender_key']; + final senderKey = event.parsedRoomEncryptedContent.senderKey; final loadFromDb = () async { final sessions = await getOlmSessions(senderKey); return sessions.isNotEmpty; diff --git a/lib/src/room.dart b/lib/src/room.dart index 63b97322..3add8c85 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1670,9 +1670,8 @@ class Room { /// Returns the encryption algorithm. Currently only `m.megolm.v1.aes-sha2` is supported. /// Returns null if there is no encryption algorithm. - String get encryptionAlgorithm => getState(EventTypes.Encryption) != null - ? getState(EventTypes.Encryption).content['algorithm'].toString() - : null; + String get encryptionAlgorithm => + getState(EventTypes.Encryption)?.parsedRoomEncryptionContent?.algorithm; /// Checks if this room is encrypted. bool get encrypted => encryptionAlgorithm != null; diff --git a/pubspec.yaml b/pubspec.yaml index 57dda557..23e06626 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,9 +23,7 @@ dependencies: matrix_file_e2ee: ^1.0.5 isolate: ^2.0.3 logger: ^0.9.4 - matrix_api_lite: - git: - url: https://gitlab.com/famedly/libraries/matrix_api_lite.git + matrix_api_lite: ^0.1.4 dev_dependencies: test: ^1.15.7