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) {
final content = event.parsedRoomEncryptedContent;
if (event.type != EventTypes.Encrypted ||
event.content['ciphertext'] == null) return event;
content.ciphertextMegolm == null) {
return event;
}
Map<String, dynamic> 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));
}

View File

@ -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))

View File

@ -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;

View File

@ -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;

View File

@ -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