| 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 |
"database/sql" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"maunium.net/go/mautrix" |
| 14 |
"maunium.net/go/mautrix/crypto/olm" |
| 15 |
sqlUpgrade "maunium.net/go/mautrix/crypto/sql_store_upgrade" |
| 16 |
"maunium.net/go/mautrix/id" |
| 17 |
) |
| 18 |
|
| 19 |
func getOlmMachine(t *testing.T) *OlmMachine { |
| 20 |
db, err := sql.Open("sqlite3", ":memory:?_busy_timeout=5000") |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("Error opening db: %v", err) |
| 23 |
} |
| 24 |
sqlUpgrade.Upgrade(db, "sqlite3") |
| 25 |
sqlStore := NewSQLCryptoStore(db, "sqlite3", "accid", id.DeviceID("dev"), []byte("test"), emptyLogger{}) |
| 26 |
|
| 27 |
userID := id.UserID("@mautrix") |
| 28 |
mk, _ := olm.NewPkSigning() |
| 29 |
ssk, _ := olm.NewPkSigning() |
| 30 |
usk, _ := olm.NewPkSigning() |
| 31 |
|
| 32 |
sqlStore.PutCrossSigningKey(userID, id.XSUsageMaster, mk.PublicKey) |
| 33 |
sqlStore.PutCrossSigningKey(userID, id.XSUsageSelfSigning, ssk.PublicKey) |
| 34 |
sqlStore.PutCrossSigningKey(userID, id.XSUsageUserSigning, usk.PublicKey) |
| 35 |
|
| 36 |
return &OlmMachine{ |
| 37 |
CryptoStore: sqlStore, |
| 38 |
CrossSigningKeys: &CrossSigningKeysCache{ |
| 39 |
MasterKey: mk, |
| 40 |
SelfSigningKey: ssk, |
| 41 |
UserSigningKey: usk, |
| 42 |
}, |
| 43 |
Client: &mautrix.Client{ |
| 44 |
UserID: userID, |
| 45 |
}, |
| 46 |
Log: emptyLogger{}, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestTrustOwnDevice(t *testing.T) { |
| 51 |
m := getOlmMachine(t) |
| 52 |
ownDevice := &DeviceIdentity{ |
| 53 |
UserID: m.Client.UserID, |
| 54 |
DeviceID: "device", |
| 55 |
SigningKey: id.Ed25519("deviceKey"), |
| 56 |
} |
| 57 |
if m.IsDeviceTrusted(ownDevice) { |
| 58 |
t.Error("Own device trusted while it shouldn't be") |
| 59 |
} |
| 60 |
|
| 61 |
m.CryptoStore.PutSignature(ownDevice.UserID, m.CrossSigningKeys.SelfSigningKey.PublicKey, |
| 62 |
ownDevice.UserID, m.CrossSigningKeys.MasterKey.PublicKey, "sig1") |
| 63 |
m.CryptoStore.PutSignature(ownDevice.UserID, ownDevice.SigningKey, |
| 64 |
ownDevice.UserID, m.CrossSigningKeys.SelfSigningKey.PublicKey, "sig2") |
| 65 |
|
| 66 |
if !m.IsUserTrusted(ownDevice.UserID) { |
| 67 |
t.Error("Own user not trusted while they should be") |
| 68 |
} |
| 69 |
if !m.IsDeviceTrusted(ownDevice) { |
| 70 |
t.Error("Own device not trusted while it should be") |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestTrustOtherUser(t *testing.T) { |
| 75 |
m := getOlmMachine(t) |
| 76 |
otherUser := id.UserID("@user") |
| 77 |
if m.IsUserTrusted(otherUser) { |
| 78 |
t.Error("Other user trusted while they shouldn't be") |
| 79 |
} |
| 80 |
|
| 81 |
theirMasterKey, _ := olm.NewPkSigning() |
| 82 |
m.CryptoStore.PutCrossSigningKey(otherUser, id.XSUsageMaster, theirMasterKey.PublicKey) |
| 83 |
|
| 84 |
m.CryptoStore.PutSignature(m.Client.UserID, m.CrossSigningKeys.UserSigningKey.PublicKey, |
| 85 |
m.Client.UserID, m.CrossSigningKeys.MasterKey.PublicKey, "sig1") |
| 86 |
|
| 87 |
// sign them with self-signing instead of user-signing key |
| 88 |
m.CryptoStore.PutSignature(otherUser, theirMasterKey.PublicKey, |
| 89 |
m.Client.UserID, m.CrossSigningKeys.SelfSigningKey.PublicKey, "invalid_sig") |
| 90 |
|
| 91 |
if m.IsUserTrusted(otherUser) { |
| 92 |
t.Error("Other user trusted before their master key has been signed with our user-signing key") |
| 93 |
} |
| 94 |
|
| 95 |
m.CryptoStore.PutSignature(otherUser, theirMasterKey.PublicKey, |
| 96 |
m.Client.UserID, m.CrossSigningKeys.UserSigningKey.PublicKey, "sig2") |
| 97 |
|
| 98 |
if !m.IsUserTrusted(otherUser) { |
| 99 |
t.Error("Other user not trusted while they should be") |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestTrustOtherDevice(t *testing.T) { |
| 104 |
m := getOlmMachine(t) |
| 105 |
otherUser := id.UserID("@user") |
| 106 |
theirDevice := &DeviceIdentity{ |
| 107 |
UserID: otherUser, |
| 108 |
DeviceID: "theirDevice", |
| 109 |
SigningKey: id.Ed25519("theirDeviceKey"), |
| 110 |
} |
| 111 |
if m.IsUserTrusted(otherUser) { |
| 112 |
t.Error("Other user trusted while they shouldn't be") |
| 113 |
} |
| 114 |
if m.IsDeviceTrusted(theirDevice) { |
| 115 |
t.Error("Other device trusted while it shouldn't be") |
| 116 |
} |
| 117 |
|
| 118 |
theirMasterKey, _ := olm.NewPkSigning() |
| 119 |
m.CryptoStore.PutCrossSigningKey(otherUser, id.XSUsageMaster, theirMasterKey.PublicKey) |
| 120 |
theirSSK, _ := olm.NewPkSigning() |
| 121 |
m.CryptoStore.PutCrossSigningKey(otherUser, id.XSUsageSelfSigning, theirSSK.PublicKey) |
| 122 |
|
| 123 |
m.CryptoStore.PutSignature(m.Client.UserID, m.CrossSigningKeys.UserSigningKey.PublicKey, |
| 124 |
m.Client.UserID, m.CrossSigningKeys.MasterKey.PublicKey, "sig1") |
| 125 |
m.CryptoStore.PutSignature(otherUser, theirMasterKey.PublicKey, |
| 126 |
m.Client.UserID, m.CrossSigningKeys.UserSigningKey.PublicKey, "sig2") |
| 127 |
|
| 128 |
if !m.IsUserTrusted(otherUser) { |
| 129 |
t.Error("Other user not trusted while they should be") |
| 130 |
} |
| 131 |
|
| 132 |
m.CryptoStore.PutSignature(otherUser, theirSSK.PublicKey, |
| 133 |
otherUser, theirMasterKey.PublicKey, "sig3") |
| 134 |
|
| 135 |
if m.IsDeviceTrusted(theirDevice) { |
| 136 |
t.Error("Other device trusted before it has been signed with user's SSK") |
| 137 |
} |
| 138 |
|
| 139 |
m.CryptoStore.PutSignature(otherUser, theirDevice.SigningKey, |
| 140 |
otherUser, theirSSK.PublicKey, "sig4") |
| 141 |
|
| 142 |
if !m.IsDeviceTrusted(theirDevice) { |
| 143 |
t.Error("Other device not trusted while it should be") |
| 144 |
} |
| 145 |
} |