refactor: Add json parsing for encryption and encrypted content

This commit is contained in:
Christian Pauly 2021-01-08 09:01:28 +01:00
parent 734431d553
commit 8b13df8c9d
5 changed files with 24 additions and 27 deletions

View File

@ -149,16 +149,19 @@ class Encryption {
} }
Event decryptRoomEventSync(String roomId, Event event) { Event decryptRoomEventSync(String roomId, Event event) {
final content = event.parsedRoomEncryptedContent;
if (event.type != EventTypes.Encrypted || if (event.type != EventTypes.Encrypted ||
event.content['ciphertext'] == null) return event; content.ciphertextMegolm == null) {
return event;
}
Map<String, dynamic> decryptedPayload; Map<String, dynamic> decryptedPayload;
var canRequestSession = false; var canRequestSession = false;
try { try {
if (event.content['algorithm'] != AlgorithmTypes.megolmV1AesSha2) { if (content.algorithm != AlgorithmTypes.megolmV1AesSha2) {
throw DecryptException(DecryptException.unknownAlgorithm); throw DecryptException(DecryptException.unknownAlgorithm);
} }
final String sessionId = event.content['session_id']; final sessionId = content.sessionId;
final String senderKey = event.content['sender_key']; final senderKey = content.senderKey;
final inboundGroupSession = final inboundGroupSession =
keyManager.getInboundGroupSession(roomId, sessionId, senderKey); keyManager.getInboundGroupSession(roomId, sessionId, senderKey);
if (inboundGroupSession == null) { if (inboundGroupSession == null) {
@ -169,7 +172,7 @@ class Encryption {
canRequestSession = true; canRequestSession = true;
final decryptResult = inboundGroupSession.inboundGroupSession final decryptResult = inboundGroupSession.inboundGroupSession
.decrypt(event.content['ciphertext']); .decrypt(content.ciphertextMegolm);
canRequestSession = false; canRequestSession = false;
// we can't have the key be an int, else json-serializing will fail, thus we need it to be a string // 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(); final messageIndexKey = 'key-' + decryptResult.message_index.toString();
@ -204,7 +207,7 @@ class Encryption {
?.outboundGroupSession ?.outboundGroupSession
?.session_id() ?? ?.session_id() ??
'') == '') ==
event.content['session_id']) { content.sessionId) {
runInRoot(() => runInRoot(() =>
keyManager.clearOrUseOutboundGroupSession(roomId, wipe: true)); keyManager.clearOrUseOutboundGroupSession(roomId, wipe: true));
} }

View File

@ -275,15 +275,11 @@ class KeyManager {
} }
if (!wipe) { if (!wipe) {
// first check if it needs to be rotated // first check if it needs to be rotated
final encryptionContent = room.getState(EventTypes.Encryption)?.content; final encryptionContent =
final maxMessages = encryptionContent != null && room.getState(EventTypes.Encryption)?.parsedRoomEncryptionContent;
encryptionContent['rotation_period_msgs'] is int final maxMessages = encryptionContent?.rotationPeriodMsgs ?? 100;
? encryptionContent['rotation_period_msgs'] final maxAge = encryptionContent?.rotationPeriodMs ??
: 100; 604800000; // default of one week
final maxAge = encryptionContent != null &&
encryptionContent['rotation_period_ms'] is int
? encryptionContent['rotation_period_ms']
: 604800000; // default of one week
if (sess.sentMessages >= maxMessages || if (sess.sentMessages >= maxMessages ||
sess.creationTime sess.creationTime
.add(Duration(milliseconds: maxAge)) .add(Duration(milliseconds: maxAge))

View File

@ -247,16 +247,17 @@ class OlmManager {
if (event.type != EventTypes.Encrypted) { if (event.type != EventTypes.Encrypted) {
return event; return event;
} }
if (event.content['algorithm'] != AlgorithmTypes.olmV1Curve25519AesSha2) { final content = event.parsedRoomEncryptedContent;
if (content.algorithm != AlgorithmTypes.olmV1Curve25519AesSha2) {
throw DecryptException(DecryptException.unknownAlgorithm); throw DecryptException(DecryptException.unknownAlgorithm);
} }
if (!event.content['ciphertext'].containsKey(identityKey)) { if (!content.ciphertextOlm.containsKey(identityKey)) {
throw DecryptException(DecryptException.isntSentForThisDevice); throw DecryptException(DecryptException.isntSentForThisDevice);
} }
String plaintext; String plaintext;
final String senderKey = event.content['sender_key']; final senderKey = content.senderKey;
final String body = event.content['ciphertext'][identityKey]['body']; final body = content.ciphertextOlm[identityKey].body;
final int type = event.content['ciphertext'][identityKey]['type']; final type = content.ciphertextOlm[identityKey].type;
if (type != 0 && type != 1) { if (type != 0 && type != 1) {
throw DecryptException(DecryptException.unknownMessageType); throw DecryptException(DecryptException.unknownMessageType);
} }
@ -429,7 +430,7 @@ class OlmManager {
if (event.type != EventTypes.Encrypted) { if (event.type != EventTypes.Encrypted) {
return event; return event;
} }
final senderKey = event.content['sender_key']; final senderKey = event.parsedRoomEncryptedContent.senderKey;
final loadFromDb = () async { final loadFromDb = () async {
final sessions = await getOlmSessions(senderKey); final sessions = await getOlmSessions(senderKey);
return sessions.isNotEmpty; return sessions.isNotEmpty;

View File

@ -1670,9 +1670,8 @@ class Room {
/// Returns the encryption algorithm. Currently only `m.megolm.v1.aes-sha2` is supported. /// Returns the encryption algorithm. Currently only `m.megolm.v1.aes-sha2` is supported.
/// Returns null if there is no encryption algorithm. /// Returns null if there is no encryption algorithm.
String get encryptionAlgorithm => getState(EventTypes.Encryption) != null String get encryptionAlgorithm =>
? getState(EventTypes.Encryption).content['algorithm'].toString() getState(EventTypes.Encryption)?.parsedRoomEncryptionContent?.algorithm;
: null;
/// Checks if this room is encrypted. /// Checks if this room is encrypted.
bool get encrypted => encryptionAlgorithm != null; bool get encrypted => encryptionAlgorithm != null;

View File

@ -23,9 +23,7 @@ dependencies:
matrix_file_e2ee: ^1.0.5 matrix_file_e2ee: ^1.0.5
isolate: ^2.0.3 isolate: ^2.0.3
logger: ^0.9.4 logger: ^0.9.4
matrix_api_lite: matrix_api_lite: ^0.1.4
git:
url: https://gitlab.com/famedly/libraries/matrix_api_lite.git
dev_dependencies: dev_dependencies:
test: ^1.15.7 test: ^1.15.7