[Event] Enhance decryption
This commit is contained in:
parent
5a4c484db6
commit
3926539661
|
@ -1192,10 +1192,10 @@ class Client {
|
||||||
type: type,
|
type: type,
|
||||||
content: event,
|
content: event,
|
||||||
);
|
);
|
||||||
this.store?.storeEventUpdate(update);
|
|
||||||
if (event["type"] == "m.room.encrypted") {
|
if (event["type"] == "m.room.encrypted") {
|
||||||
update = update.decrypt(this.getRoomById(update.roomID));
|
update = update.decrypt(this.getRoomById(update.roomID));
|
||||||
}
|
}
|
||||||
|
this.store?.storeEventUpdate(update);
|
||||||
_updateRoomsByEventUpdate(update);
|
_updateRoomsByEventUpdate(update);
|
||||||
onEvent.add(update);
|
onEvent.add(update);
|
||||||
|
|
||||||
|
|
|
@ -384,6 +384,10 @@ class Event {
|
||||||
content['m.relates_to']['m.in_reply_to']['event_id'];
|
content['m.relates_to']['m.in_reply_to']['event_id'];
|
||||||
return await timeline.getEventById(replyEventId);
|
return await timeline.getEventById(replyEventId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trys to decrypt this event. Returns a m.bad.encrypted event
|
||||||
|
/// if it fails and does nothing if the event was not encrypted.
|
||||||
|
Event get decrypted => room.decryptGroupMessage(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MessageTypes {
|
enum MessageTypes {
|
||||||
|
|
|
@ -186,6 +186,14 @@ class Room {
|
||||||
/// Adds the [state] to this room and overwrites a state with the same
|
/// Adds the [state] to this room and overwrites a state with the same
|
||||||
/// typeKey/stateKey key pair if there is one.
|
/// typeKey/stateKey key pair if there is one.
|
||||||
void setState(Event state) {
|
void setState(Event state) {
|
||||||
|
// Decrypt if necessary
|
||||||
|
if (state.type == EventTypes.Encrypted) {
|
||||||
|
try {
|
||||||
|
state = decryptGroupMessage(state);
|
||||||
|
} catch (e) {
|
||||||
|
print("[LibOlm] Could not decrypt room state: " + e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
// Check if this is a member change and we need to clear the outboundGroupSession.
|
// Check if this is a member change and we need to clear the outboundGroupSession.
|
||||||
if (encrypted &&
|
if (encrypted &&
|
||||||
outboundGroupSession != null &&
|
outboundGroupSession != null &&
|
||||||
|
@ -708,8 +716,8 @@ class Room {
|
||||||
roomID: id,
|
roomID: id,
|
||||||
eventType: resp["state"][i]["type"],
|
eventType: resp["state"][i]["type"],
|
||||||
content: resp["state"][i],
|
content: resp["state"][i],
|
||||||
);
|
).decrypt(this);
|
||||||
client.onEvent.add(eventUpdate.decrypt(this));
|
client.onEvent.add(eventUpdate);
|
||||||
client.store.storeEventUpdate(eventUpdate);
|
client.store.storeEventUpdate(eventUpdate);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -721,8 +729,8 @@ class Room {
|
||||||
roomID: id,
|
roomID: id,
|
||||||
eventType: resp["state"][i]["type"],
|
eventType: resp["state"][i]["type"],
|
||||||
content: resp["state"][i],
|
content: resp["state"][i],
|
||||||
);
|
).decrypt(this);
|
||||||
client.onEvent.add(eventUpdate.decrypt(this));
|
client.onEvent.add(eventUpdate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -735,8 +743,8 @@ class Room {
|
||||||
roomID: id,
|
roomID: id,
|
||||||
eventType: history[i]["type"],
|
eventType: history[i]["type"],
|
||||||
content: history[i],
|
content: history[i],
|
||||||
);
|
).decrypt(this);
|
||||||
client.onEvent.add(eventUpdate.decrypt(this));
|
client.onEvent.add(eventUpdate);
|
||||||
client.store.storeEventUpdate(eventUpdate);
|
client.store.storeEventUpdate(eventUpdate);
|
||||||
client.store.setRoomPrevBatch(id, resp["end"]);
|
client.store.setRoomPrevBatch(id, resp["end"]);
|
||||||
}
|
}
|
||||||
|
@ -749,8 +757,8 @@ class Room {
|
||||||
roomID: id,
|
roomID: id,
|
||||||
eventType: history[i]["type"],
|
eventType: history[i]["type"],
|
||||||
content: history[i],
|
content: history[i],
|
||||||
);
|
).decrypt(this);
|
||||||
client.onEvent.add(eventUpdate.decrypt(this));
|
client.onEvent.add(eventUpdate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client.onRoomUpdate.add(
|
client.onRoomUpdate.add(
|
||||||
|
@ -908,15 +916,28 @@ class Room {
|
||||||
onTimelineInsertCallback onInsert}) async {
|
onTimelineInsertCallback onInsert}) async {
|
||||||
List<Event> events =
|
List<Event> events =
|
||||||
client.store != null ? await client.store.getEventList(this) : [];
|
client.store != null ? await client.store.getEventList(this) : [];
|
||||||
if (this.encrypted) {
|
|
||||||
for (int i = 0; i < events.length; i++) {
|
// Try again to decrypt encrypted events and update the database.
|
||||||
try {
|
if (this.encrypted && client.store != null) {
|
||||||
events[i] = decryptGroupMessage(events[i]);
|
await client.store.transaction(() {
|
||||||
} catch (e) {
|
for (int i = 0; i < events.length; i++) {
|
||||||
print("[LibOlm] Could not decrypt group message: " + e.toString());
|
if (events[i].type == EventTypes.Encrypted) {
|
||||||
|
events[i] = events[i].decrypted;
|
||||||
|
if (events[i].type == EventTypes.Encrypted) {
|
||||||
|
client.store.storeEventUpdate(
|
||||||
|
EventUpdate(
|
||||||
|
eventType: events[i].typeKey,
|
||||||
|
content: events[i].toJson(),
|
||||||
|
roomID: events[i].roomId,
|
||||||
|
type: "timeline",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Timeline timeline = Timeline(
|
Timeline timeline = Timeline(
|
||||||
room: this,
|
room: this,
|
||||||
events: events,
|
events: events,
|
||||||
|
@ -1503,6 +1524,8 @@ class Room {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decrypts the given [event] with one of the available ingoingGroupSessions.
|
/// Decrypts the given [event] with one of the available ingoingGroupSessions.
|
||||||
|
/// Returns a m.bad.encrypted event if it fails and does nothing if the event
|
||||||
|
/// was not encrypted.
|
||||||
Event decryptGroupMessage(Event event) {
|
Event decryptGroupMessage(Event event) {
|
||||||
if (event.type != EventTypes.Encrypted) return event;
|
if (event.type != EventTypes.Encrypted) return event;
|
||||||
Map<String, dynamic> decryptedPayload;
|
Map<String, dynamic> decryptedPayload;
|
||||||
|
@ -1537,13 +1560,23 @@ class Room {
|
||||||
|
|
||||||
decryptedPayload = json.decode(decryptResult.plaintext);
|
decryptedPayload = json.decode(decryptResult.plaintext);
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
decryptedPayload = {
|
if (exception.toString() ==
|
||||||
"content": {
|
"The sender has not sent us the session key.") {
|
||||||
"msgtype": "m.bad.encrypted",
|
decryptedPayload = {
|
||||||
"body": exception.toString(),
|
"content": event.content,
|
||||||
},
|
"type": "m.room.encrypted",
|
||||||
"type": "m.room.message",
|
};
|
||||||
};
|
decryptedPayload["content"]["body"] = exception.toString();
|
||||||
|
decryptedPayload["content"]["msgtype"] = "m.bad.encrypted";
|
||||||
|
} else {
|
||||||
|
decryptedPayload = {
|
||||||
|
"content": {
|
||||||
|
"msgtype": "m.bad.encrypted",
|
||||||
|
"body": exception.toString(),
|
||||||
|
},
|
||||||
|
"type": "m.room.encrypted",
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Event(
|
return Event(
|
||||||
content: decryptedPayload["content"],
|
content: decryptedPayload["content"],
|
||||||
|
|
Loading…
Reference in a new issue