| 1 |
// Copyright (c) 2020 Nikos Filippakis |
| 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 |
"os" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"maunium.net/go/mautrix" |
| 14 |
"maunium.net/go/mautrix/event" |
| 15 |
"maunium.net/go/mautrix/id" |
| 16 |
) |
| 17 |
|
| 18 |
type emptyLogger struct{} |
| 19 |
|
| 20 |
func (emptyLogger) Error(message string, args ...interface{}) {} |
| 21 |
func (emptyLogger) Warn(message string, args ...interface{}) {} |
| 22 |
func (emptyLogger) Debug(message string, args ...interface{}) {} |
| 23 |
func (emptyLogger) Trace(message string, args ...interface{}) {} |
| 24 |
|
| 25 |
type mockStateStore struct{} |
| 26 |
|
| 27 |
func (mockStateStore) IsEncrypted(id.RoomID) bool { |
| 28 |
return true |
| 29 |
} |
| 30 |
|
| 31 |
func (mockStateStore) GetEncryptionEvent(id.RoomID) *event.EncryptionEventContent { |
| 32 |
return &event.EncryptionEventContent{ |
| 33 |
RotationPeriodMessages: 3, |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func (mockStateStore) FindSharedRooms(id.UserID) []id.RoomID { |
| 38 |
return []id.RoomID{"room1"} |
| 39 |
} |
| 40 |
|
| 41 |
func newMachine(t *testing.T, userID id.UserID) (*OlmMachine, string) { |
| 42 |
client, err := mautrix.NewClient("http://localhost", userID, "token") |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("Error creating client: %v", err) |
| 45 |
} |
| 46 |
client.DeviceID = "device1" |
| 47 |
|
| 48 |
storeFileName := "gob_store_test_" + userID.String() + ".gob" |
| 49 |
gobStore, err := NewGobStore(storeFileName) |
| 50 |
if err != nil { |
| 51 |
os.Remove(storeFileName) |
| 52 |
t.Fatalf("Error creating Gob store: %v", err) |
| 53 |
} |
| 54 |
|
| 55 |
machine := NewOlmMachine(client, emptyLogger{}, gobStore, mockStateStore{}) |
| 56 |
if err := machine.Load(); err != nil { |
| 57 |
os.Remove(storeFileName) |
| 58 |
t.Fatalf("Error creating account: %v", err) |
| 59 |
} |
| 60 |
|
| 61 |
return machine, storeFileName |
| 62 |
} |
| 63 |
|
| 64 |
func TestOlmMachineOlmMegolmSessions(t *testing.T) { |
| 65 |
machineOut, storeFileNameOut := newMachine(t, "user1") |
| 66 |
defer os.Remove(storeFileNameOut) |
| 67 |
machineIn, storeFileNameIn := newMachine(t, "user2") |
| 68 |
defer os.Remove(storeFileNameIn) |
| 69 |
|
| 70 |
// generate OTKs for receiving machine |
| 71 |
otks := machineIn.account.getOneTimeKeys("user2", "device2", 0) |
| 72 |
var otk mautrix.OneTimeKey |
| 73 |
for _, otkTmp := range otks { |
| 74 |
// take first OTK |
| 75 |
otk = otkTmp |
| 76 |
break |
| 77 |
} |
| 78 |
|
| 79 |
// create outbound olm session for sending machine using OTK |
| 80 |
olmSession, err := machineOut.account.Internal.NewOutboundSession(machineIn.account.IdentityKey(), otk.Key) |
| 81 |
if err != nil { |
| 82 |
t.Errorf("Failed to create outbound olm session: %v", err) |
| 83 |
} |
| 84 |
|
| 85 |
// store sender device identity in receiving machine store |
| 86 |
machineIn.CryptoStore.PutDevices("user1", map[id.DeviceID]*DeviceIdentity{ |
| 87 |
"device1": { |
| 88 |
UserID: "user1", |
| 89 |
DeviceID: "device1", |
| 90 |
IdentityKey: machineOut.account.IdentityKey(), |
| 91 |
SigningKey: machineOut.account.SigningKey(), |
| 92 |
}, |
| 93 |
}) |
| 94 |
|
| 95 |
// create & store outbound megolm session for sending the event later |
| 96 |
megolmOutSession := machineOut.newOutboundGroupSession("room1") |
| 97 |
megolmOutSession.Shared = true |
| 98 |
machineOut.CryptoStore.AddOutboundGroupSession(megolmOutSession) |
| 99 |
|
| 100 |
// encrypt m.room_key event with olm session |
| 101 |
deviceIdentity := &DeviceIdentity{ |
| 102 |
UserID: "user2", |
| 103 |
DeviceID: "device2", |
| 104 |
IdentityKey: machineIn.account.IdentityKey(), |
| 105 |
SigningKey: machineIn.account.SigningKey(), |
| 106 |
} |
| 107 |
wrapped := wrapSession(olmSession) |
| 108 |
content := machineOut.encryptOlmEvent(wrapped, deviceIdentity, event.ToDeviceRoomKey, megolmOutSession.ShareContent()) |
| 109 |
|
| 110 |
senderKey := machineOut.account.IdentityKey() |
| 111 |
signingKey := machineOut.account.SigningKey() |
| 112 |
|
| 113 |
for _, content := range content.OlmCiphertext { |
| 114 |
// decrypt olm ciphertext |
| 115 |
decrypted, err := machineIn.decryptAndParseOlmCiphertext("user1", senderKey, content.Type, content.Body, "test") |
| 116 |
if err != nil { |
| 117 |
t.Errorf("Error decrypting olm content: %v", err) |
| 118 |
} |
| 119 |
// store room key in new inbound group session |
| 120 |
decrypted.Content.ParseRaw(event.ToDeviceRoomKey) |
| 121 |
roomKeyEvt := decrypted.Content.AsRoomKey() |
| 122 |
igs, err := NewInboundGroupSession(senderKey, signingKey, "room1", roomKeyEvt.SessionKey) |
| 123 |
if err != nil { |
| 124 |
t.Errorf("Error creating inbound megolm session: %v", err) |
| 125 |
} |
| 126 |
if err = machineIn.CryptoStore.PutGroupSession("room1", senderKey, igs.ID(), igs); err != nil { |
| 127 |
t.Errorf("Error storing inbound megolm session: %v", err) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// encrypt event with megolm session in sending machine |
| 132 |
eventContent := map[string]string{"hello": "world"} |
| 133 |
encryptedEvtContent, err := machineOut.EncryptMegolmEvent("room1", event.EventMessage, eventContent) |
| 134 |
if err != nil { |
| 135 |
t.Errorf("Error encrypting megolm event: %v", err) |
| 136 |
} |
| 137 |
if megolmOutSession.MessageCount != 1 { |
| 138 |
t.Errorf("Megolm outbound session message count is not 1 but %d", megolmOutSession.MessageCount) |
| 139 |
} |
| 140 |
|
| 141 |
encryptedEvt := &event.Event{ |
| 142 |
Content: event.Content{Parsed: encryptedEvtContent}, |
| 143 |
Type: event.EventEncrypted, |
| 144 |
ID: "event1", |
| 145 |
RoomID: "room1", |
| 146 |
Sender: "user1", |
| 147 |
} |
| 148 |
|
| 149 |
// decrypt event on receiving machine and confirm |
| 150 |
decryptedEvt, err := machineIn.DecryptMegolmEvent(encryptedEvt) |
| 151 |
if err != nil { |
| 152 |
t.Errorf("Error decrypting megolm event: %v", err) |
| 153 |
} |
| 154 |
if decryptedEvt.Type != event.EventMessage { |
| 155 |
t.Errorf("Expected event type %v, got %v", event.EventMessage, decryptedEvt.Type) |
| 156 |
} |
| 157 |
if decryptedEvt.Content.Raw["hello"] != "world" { |
| 158 |
t.Errorf("Expected event content %v, got %v", eventContent, decryptedEvt.Content.Raw) |
| 159 |
} |
| 160 |
|
| 161 |
machineOut.EncryptMegolmEvent("room1", event.EventMessage, eventContent) |
| 162 |
if megolmOutSession.Expired() { |
| 163 |
t.Error("Megolm outbound session expired before 3rd message") |
| 164 |
} |
| 165 |
machineOut.EncryptMegolmEvent("room1", event.EventMessage, eventContent) |
| 166 |
if !megolmOutSession.Expired() { |
| 167 |
t.Error("Megolm outbound session not expired after 3rd message") |
| 168 |
} |
| 169 |
} |