| 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 |
"errors" |
| 11 |
"fmt" |
| 12 |
|
| 13 |
"maunium.net/go/mautrix" |
| 14 |
"maunium.net/go/mautrix/crypto/olm" |
| 15 |
"maunium.net/go/mautrix/id" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
MismatchingDeviceID = errors.New("mismatching device ID in parameter and keys object") |
| 20 |
MismatchingUserID = errors.New("mismatching user ID in parameter and keys object") |
| 21 |
MismatchingSigningKey = errors.New("received update for device with different signing key") |
| 22 |
NoSigningKeyFound = errors.New("didn't find ed25519 signing key") |
| 23 |
NoIdentityKeyFound = errors.New("didn't find curve25519 identity key") |
| 24 |
InvalidKeySignature = errors.New("invalid signature on device keys") |
| 25 |
) |
| 26 |
|
| 27 |
func (mach *OlmMachine) LoadDevices(user id.UserID) map[id.DeviceID]*DeviceIdentity { |
| 28 |
return mach.fetchKeys([]id.UserID{user}, "", true)[user] |
| 29 |
} |
| 30 |
|
| 31 |
func (mach *OlmMachine) fetchKeys(users []id.UserID, sinceToken string, includeUntracked bool) (data map[id.UserID]map[id.DeviceID]*DeviceIdentity) { |
| 32 |
req := &mautrix.ReqQueryKeys{ |
| 33 |
DeviceKeys: mautrix.DeviceKeysRequest{}, |
| 34 |
Timeout: 10 * 1000, |
| 35 |
Token: sinceToken, |
| 36 |
} |
| 37 |
if !includeUntracked { |
| 38 |
users = mach.CryptoStore.FilterTrackedUsers(users) |
| 39 |
} |
| 40 |
if len(users) == 0 { |
| 41 |
return |
| 42 |
} |
| 43 |
for _, userID := range users { |
| 44 |
req.DeviceKeys[userID] = mautrix.DeviceIDList{} |
| 45 |
} |
| 46 |
mach.Log.Trace("Querying keys for %v", users) |
| 47 |
resp, err := mach.Client.QueryKeys(req) |
| 48 |
if err != nil { |
| 49 |
mach.Log.Warn("Failed to query keys: %v", err) |
| 50 |
return |
| 51 |
} |
| 52 |
for server, err := range resp.Failures { |
| 53 |
mach.Log.Warn("Query keys failure for %s: %v", server, err) |
| 54 |
} |
| 55 |
mach.Log.Trace("Query key result received with %d users", len(resp.DeviceKeys)) |
| 56 |
data = make(map[id.UserID]map[id.DeviceID]*DeviceIdentity) |
| 57 |
for userID, devices := range resp.DeviceKeys { |
| 58 |
delete(req.DeviceKeys, userID) |
| 59 |
|
| 60 |
newDevices := make(map[id.DeviceID]*DeviceIdentity) |
| 61 |
existingDevices, err := mach.CryptoStore.GetDevices(userID) |
| 62 |
if err != nil { |
| 63 |
mach.Log.Warn("Failed to get existing devices for %s: %v", userID, err) |
| 64 |
existingDevices = make(map[id.DeviceID]*DeviceIdentity) |
| 65 |
} |
| 66 |
mach.Log.Trace("Updating devices for %s, got %d devices, have %d in store", userID, len(devices), len(existingDevices)) |
| 67 |
changed := false |
| 68 |
for deviceID, deviceKeys := range devices { |
| 69 |
existing, ok := existingDevices[deviceID] |
| 70 |
if !ok { |
| 71 |
// New device |
| 72 |
changed = true |
| 73 |
} |
| 74 |
mach.Log.Trace("Validating device %s of %s", deviceID, userID) |
| 75 |
newDevice, err := mach.validateDevice(userID, deviceID, deviceKeys, existing) |
| 76 |
if err != nil { |
| 77 |
mach.Log.Error("Failed to validate device %s of %s: %v", deviceID, userID, err) |
| 78 |
} else if newDevice != nil { |
| 79 |
newDevices[deviceID] = newDevice |
| 80 |
for signerUserID, signerKeys := range deviceKeys.Signatures { |
| 81 |
for signerKey, signature := range signerKeys { |
| 82 |
// verify and save self-signing key signature for each device |
| 83 |
if selfSignKeys, ok := resp.SelfSigningKeys[signerUserID]; ok { |
| 84 |
for _, pubKey := range selfSignKeys.Keys { |
| 85 |
if selfSigs, ok := deviceKeys.Signatures[signerUserID]; !ok { |
| 86 |
continue |
| 87 |
} else if _, ok := selfSigs[id.NewKeyID(id.KeyAlgorithmEd25519, pubKey.String())]; !ok { |
| 88 |
continue |
| 89 |
} |
| 90 |
if verified, err := olm.VerifySignatureJSON(deviceKeys, signerUserID, pubKey.String(), pubKey); verified { |
| 91 |
if signKey, ok := deviceKeys.Keys[id.DeviceKeyID(signerKey)]; ok { |
| 92 |
signature := deviceKeys.Signatures[signerUserID][id.NewKeyID(id.KeyAlgorithmEd25519, pubKey.String())] |
| 93 |
mach.Log.Trace("Verified self-signing signature for device %v: `%v`", deviceID, signature) |
| 94 |
mach.CryptoStore.PutSignature(userID, id.Ed25519(signKey), signerUserID, pubKey, signature) |
| 95 |
} |
| 96 |
} else { |
| 97 |
mach.Log.Warn("Could not verify device self-signing signatures: %v", err) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
// save signature of device made by its own device signing key |
| 102 |
if signKey, ok := deviceKeys.Keys[id.DeviceKeyID(signerKey)]; ok { |
| 103 |
mach.CryptoStore.PutSignature(userID, id.Ed25519(signKey), signerUserID, id.Ed25519(signKey), signature) |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
mach.Log.Trace("Storing new device list for %s containing %d devices", userID, len(newDevices)) |
| 110 |
err = mach.CryptoStore.PutDevices(userID, newDevices) |
| 111 |
if err != nil { |
| 112 |
mach.Log.Warn("Failed to update device list for %s: %v", userID, err) |
| 113 |
} |
| 114 |
data[userID] = newDevices |
| 115 |
|
| 116 |
changed = changed || len(newDevices) != len(existingDevices) |
| 117 |
if changed { |
| 118 |
mach.OnDevicesChanged(userID) |
| 119 |
} |
| 120 |
} |
| 121 |
for userID := range req.DeviceKeys { |
| 122 |
mach.Log.Warn("Didn't get any keys for user %s", userID) |
| 123 |
} |
| 124 |
|
| 125 |
mach.storeCrossSigningKeys(resp.MasterKeys, resp.DeviceKeys) |
| 126 |
mach.storeCrossSigningKeys(resp.SelfSigningKeys, resp.DeviceKeys) |
| 127 |
mach.storeCrossSigningKeys(resp.UserSigningKeys, resp.DeviceKeys) |
| 128 |
|
| 129 |
return data |
| 130 |
} |
| 131 |
|
| 132 |
// OnDevicesChanged finds all shared rooms with the given user and invalidates outbound sessions in those rooms. |
| 133 |
// |
| 134 |
// This is called automatically whenever a device list change is noticed in ProcessSyncResponse and usually does |
| 135 |
// not need to be called manually. |
| 136 |
func (mach *OlmMachine) OnDevicesChanged(userID id.UserID) { |
| 137 |
for _, roomID := range mach.StateStore.FindSharedRooms(userID) { |
| 138 |
mach.Log.Debug("Devices of %s changed, invalidating group session for %s", userID, roomID) |
| 139 |
err := mach.CryptoStore.RemoveOutboundGroupSession(roomID) |
| 140 |
if err != nil { |
| 141 |
mach.Log.Warn("Failed to invalidate outbound group session of %s on device change for %s: %v", roomID, userID, err) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func (mach *OlmMachine) validateDevice(userID id.UserID, deviceID id.DeviceID, deviceKeys mautrix.DeviceKeys, existing *DeviceIdentity) (*DeviceIdentity, error) { |
| 147 |
if deviceID != deviceKeys.DeviceID { |
| 148 |
return nil, MismatchingDeviceID |
| 149 |
} else if userID != deviceKeys.UserID { |
| 150 |
return nil, MismatchingUserID |
| 151 |
} |
| 152 |
|
| 153 |
signingKey := deviceKeys.Keys.GetEd25519(deviceID) |
| 154 |
identityKey := deviceKeys.Keys.GetCurve25519(deviceID) |
| 155 |
if signingKey == "" { |
| 156 |
return nil, NoSigningKeyFound |
| 157 |
} else if identityKey == "" { |
| 158 |
return nil, NoIdentityKeyFound |
| 159 |
} |
| 160 |
|
| 161 |
if existing != nil && existing.SigningKey != signingKey { |
| 162 |
return existing, MismatchingSigningKey |
| 163 |
} |
| 164 |
|
| 165 |
ok, err := olm.VerifySignatureJSON(deviceKeys, userID, deviceID.String(), signingKey) |
| 166 |
if err != nil { |
| 167 |
return existing, fmt.Errorf("failed to verify signature: %w", err) |
| 168 |
} else if !ok { |
| 169 |
return existing, InvalidKeySignature |
| 170 |
} |
| 171 |
|
| 172 |
name, ok := deviceKeys.Unsigned["device_display_name"].(string) |
| 173 |
if !ok { |
| 174 |
name = string(deviceID) |
| 175 |
} |
| 176 |
|
| 177 |
return &DeviceIdentity{ |
| 178 |
UserID: userID, |
| 179 |
DeviceID: deviceID, |
| 180 |
IdentityKey: identityKey, |
| 181 |
SigningKey: signingKey, |
| 182 |
Trust: TrustStateUnset, |
| 183 |
Name: name, |
| 184 |
Deleted: false, |
| 185 |
}, nil |
| 186 |
} |