| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package crypto |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
|
| 14 |
"maunium.net/go/mautrix/event" |
| 15 |
"maunium.net/go/mautrix/id" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
IncorrectEncryptedContentType = errors.New("event content is not instance of *event.EncryptedEventContent") |
| 20 |
NoSessionFound = errors.New("failed to decrypt megolm event: no session with given ID found") |
| 21 |
DuplicateMessageIndex = errors.New("duplicate megolm message index") |
| 22 |
WrongRoom = errors.New("encrypted megolm event is not intended for this room") |
| 23 |
DeviceKeyMismatch = errors.New("device keys in event and verified device info do not match") |
| 24 |
) |
| 25 |
|
| 26 |
type megolmEvent struct { |
| 27 |
RoomID id.RoomID `json:"room_id"` |
| 28 |
Type event.Type `json:"type"` |
| 29 |
Content event.Content `json:"content"` |
| 30 |
} |
| 31 |
|
| 32 |
// DecryptMegolmEvent decrypts an m.room.encrypted event where the algorithm is m.megolm.v1.aes-sha2 |
| 33 |
func (mach *OlmMachine) DecryptMegolmEvent(evt *event.Event) (*event.Event, error) { |
| 34 |
content, ok := evt.Content.Parsed.(*event.EncryptedEventContent) |
| 35 |
if !ok { |
| 36 |
return nil, IncorrectEncryptedContentType |
| 37 |
} else if content.Algorithm != id.AlgorithmMegolmV1 { |
| 38 |
return nil, UnsupportedAlgorithm |
| 39 |
} |
| 40 |
sess, err := mach.CryptoStore.GetGroupSession(evt.RoomID, content.SenderKey, content.SessionID) |
| 41 |
if err != nil { |
| 42 |
return nil, fmt.Errorf("failed to get group session: %w", err) |
| 43 |
} else if sess == nil { |
| 44 |
return nil, fmt.Errorf("%w (ID %s)", NoSessionFound, content.SessionID) |
| 45 |
} |
| 46 |
plaintext, messageIndex, err := sess.Internal.Decrypt(content.MegolmCiphertext) |
| 47 |
if err != nil { |
| 48 |
return nil, fmt.Errorf("failed to decrypt megolm event: %w", err) |
| 49 |
} else if !mach.CryptoStore.ValidateMessageIndex(content.SenderKey, content.SessionID, evt.ID, messageIndex, evt.Timestamp) { |
| 50 |
return nil, DuplicateMessageIndex |
| 51 |
} |
| 52 |
|
| 53 |
var verified bool |
| 54 |
ownSigningKey, ownIdentityKey := mach.account.Keys() |
| 55 |
if content.DeviceID == mach.Client.DeviceID && sess.SigningKey == ownSigningKey && content.SenderKey == ownIdentityKey { |
| 56 |
verified = true |
| 57 |
} else { |
| 58 |
device, err := mach.GetOrFetchDevice(evt.Sender, content.DeviceID) |
| 59 |
if err != nil { |
| 60 |
// We don't want to throw these errors as the message can still be decrypted. |
| 61 |
mach.Log.Debug("Failed to get device %s/%s to verify session %s: %v", evt.Sender, content.DeviceID, sess.ID(), err) |
| 62 |
// TODO maybe store the info that the device is deleted? |
| 63 |
} else if mach.IsDeviceTrusted(device) && len(sess.ForwardingChains) == 0 { // For some reason, matrix-nio had a comment saying not to events decrypted using a forwarded key as verified. |
| 64 |
if device.SigningKey != sess.SigningKey || device.IdentityKey != content.SenderKey { |
| 65 |
return nil, DeviceKeyMismatch |
| 66 |
} |
| 67 |
verified = true |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
megolmEvt := &megolmEvent{} |
| 72 |
err = json.Unmarshal(plaintext, &megolmEvt) |
| 73 |
if err != nil { |
| 74 |
return nil, fmt.Errorf("failed to parse megolm payload: %w", err) |
| 75 |
} else if megolmEvt.RoomID != evt.RoomID { |
| 76 |
return nil, WrongRoom |
| 77 |
} |
| 78 |
megolmEvt.Type.Class = evt.Type.Class |
| 79 |
err = megolmEvt.Content.ParseRaw(megolmEvt.Type) |
| 80 |
if err != nil { |
| 81 |
if event.IsUnsupportedContentType(err) { |
| 82 |
mach.Log.Warn("Unsupported event type %s in encrypted event %s", megolmEvt.Type.Repr(), evt.ID) |
| 83 |
} else { |
| 84 |
return nil, fmt.Errorf("failed to parse content of megolm payload event: %w", err) |
| 85 |
} |
| 86 |
} |
| 87 |
if content.RelatesTo != nil { |
| 88 |
relatable, ok := megolmEvt.Content.Parsed.(event.Relatable) |
| 89 |
if ok { |
| 90 |
if relatable.OptionalGetRelatesTo() == nil { |
| 91 |
relatable.SetRelatesTo(content.RelatesTo) |
| 92 |
} else { |
| 93 |
mach.Log.Trace("Not overriding relation data in %s, as encrypted payload already has it", evt.ID) |
| 94 |
} |
| 95 |
} else { |
| 96 |
mach.Log.Warn("Encrypted event %s has relation data, but content type %T (%s) doesn't support it", evt.ID, megolmEvt.Content.Parsed, megolmEvt.Type.String()) |
| 97 |
} |
| 98 |
} |
| 99 |
megolmEvt.Type.Class = evt.Type.Class |
| 100 |
return &event.Event{ |
| 101 |
Sender: evt.Sender, |
| 102 |
Type: megolmEvt.Type, |
| 103 |
Timestamp: evt.Timestamp, |
| 104 |
ID: evt.ID, |
| 105 |
RoomID: evt.RoomID, |
| 106 |
Content: megolmEvt.Content, |
| 107 |
Unsigned: evt.Unsigned, |
| 108 |
Mautrix: event.MautrixInfo{ |
| 109 |
Verified: verified, |
| 110 |
}, |
| 111 |
}, nil |
| 112 |
} |