| 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/gob" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"os" |
| 14 |
"sort" |
| 15 |
"sync" |
| 16 |
|
| 17 |
"maunium.net/go/mautrix/event" |
| 18 |
"maunium.net/go/mautrix/id" |
| 19 |
) |
| 20 |
|
| 21 |
// TrustState determines how trusted a device is. |
| 22 |
type TrustState int |
| 23 |
|
| 24 |
const ( |
| 25 |
TrustStateUnset TrustState = iota |
| 26 |
TrustStateVerified |
| 27 |
TrustStateBlacklisted |
| 28 |
TrustStateIgnored |
| 29 |
) |
| 30 |
|
| 31 |
func (ts TrustState) String() string { |
| 32 |
switch ts { |
| 33 |
case TrustStateUnset: |
| 34 |
return "unverified" |
| 35 |
case TrustStateVerified: |
| 36 |
return "verified" |
| 37 |
case TrustStateBlacklisted: |
| 38 |
return "blacklisted" |
| 39 |
case TrustStateIgnored: |
| 40 |
return "ignored" |
| 41 |
default: |
| 42 |
return "" |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// DeviceIdentity contains the identity details of a device and some additional info. |
| 47 |
type DeviceIdentity struct { |
| 48 |
UserID id.UserID |
| 49 |
DeviceID id.DeviceID |
| 50 |
IdentityKey id.Curve25519 |
| 51 |
SigningKey id.Ed25519 |
| 52 |
|
| 53 |
Trust TrustState |
| 54 |
Deleted bool |
| 55 |
Name string |
| 56 |
} |
| 57 |
|
| 58 |
func (device *DeviceIdentity) Fingerprint() string { |
| 59 |
return Fingerprint(device.SigningKey) |
| 60 |
} |
| 61 |
|
| 62 |
var ErrGroupSessionWithheld = errors.New("group session has been withheld") |
| 63 |
|
| 64 |
// Store is used by OlmMachine to store Olm and Megolm sessions, user device lists and message indices. |
| 65 |
// |
| 66 |
// General implementation details: |
| 67 |
// * Get methods should not return errors if the requested data does not exist in the store, they should simply return nil. |
| 68 |
// * Update methods may assume that the pointer is the same as what has earlier been added to or fetched from the store. |
| 69 |
type Store interface { |
| 70 |
// Flush ensures that everything in the store is persisted to disk. |
| 71 |
// This doesn't have to do anything, e.g. for database-backed implementations that persist everything immediately. |
| 72 |
Flush() error |
| 73 |
|
| 74 |
// PutAccount updates the OlmAccount in the store. |
| 75 |
PutAccount(*OlmAccount) error |
| 76 |
// GetAccount returns the OlmAccount in the store that was previously inserted with PutAccount. |
| 77 |
GetAccount() (*OlmAccount, error) |
| 78 |
|
| 79 |
// AddSession inserts an Olm session into the store. |
| 80 |
AddSession(id.SenderKey, *OlmSession) error |
| 81 |
// HasSession returns whether or not the store has an Olm session with the given sender key. |
| 82 |
HasSession(id.SenderKey) bool |
| 83 |
// GetSessions returns all Olm sessions in the store with the given sender key. |
| 84 |
GetSessions(id.SenderKey) (OlmSessionList, error) |
| 85 |
// GetLatestSession returns the session with the highest session ID (lexiographically sorting). |
| 86 |
// It's usually safe to return the most recently added session if sorting by session ID is too difficult. |
| 87 |
GetLatestSession(id.SenderKey) (*OlmSession, error) |
| 88 |
// UpdateSession updates a session that has previously been inserted with AddSession. |
| 89 |
UpdateSession(id.SenderKey, *OlmSession) error |
| 90 |
|
| 91 |
// PutGroupSession inserts an inbound Megolm session into the store. If an earlier withhold event has been inserted |
| 92 |
// with PutWithheldGroupSession, this call should replace that. However, PutWithheldGroupSession must not replace |
| 93 |
// sessions inserted with this call. |
| 94 |
PutGroupSession(id.RoomID, id.SenderKey, id.SessionID, *InboundGroupSession) error |
| 95 |
// GetGroupSession gets an inbound Megolm session from the store. If the group session has been withheld |
| 96 |
// (i.e. a room key withheld event has been saved with PutWithheldGroupSession), this should return the |
| 97 |
// ErrGroupSessionWithheld error. The caller may use GetWithheldGroupSession to find more details. |
| 98 |
GetGroupSession(id.RoomID, id.SenderKey, id.SessionID) (*InboundGroupSession, error) |
| 99 |
// PutWithheldGroupSession tells the store that a specific Megolm session was withheld. |
| 100 |
PutWithheldGroupSession(event.RoomKeyWithheldEventContent) error |
| 101 |
// GetWithheldGroupSession gets the event content that was previously inserted with PutWithheldGroupSession. |
| 102 |
GetWithheldGroupSession(id.RoomID, id.SenderKey, id.SessionID) (*event.RoomKeyWithheldEventContent, error) |
| 103 |
|
| 104 |
// GetGroupSessionsForRoom gets all the inbound Megolm sessions for a specific room. This is used for creating key |
| 105 |
// export files. Unlike GetGroupSession, this should not return any errors about withheld keys. |
| 106 |
GetGroupSessionsForRoom(id.RoomID) ([]*InboundGroupSession, error) |
| 107 |
// GetGroupSessionsForRoom gets all the inbound Megolm sessions in the store. This is used for creating key export |
| 108 |
// files. Unlike GetGroupSession, this should not return any errors about withheld keys. |
| 109 |
GetAllGroupSessions() ([]*InboundGroupSession, error) |
| 110 |
|
| 111 |
// AddOutboundGroupSession inserts the given outbound Megolm session into the store. |
| 112 |
// |
| 113 |
// The store should index inserted sessions by the RoomID field to support getting and removing sessions. |
| 114 |
// There will only be one outbound session per room ID at a time. |
| 115 |
AddOutboundGroupSession(*OutboundGroupSession) error |
| 116 |
// UpdateOutboundGroupSession updates the given outbound Megolm session in the store. |
| 117 |
UpdateOutboundGroupSession(*OutboundGroupSession) error |
| 118 |
// GetOutboundGroupSession gets the stored outbound Megolm session for the given room ID from the store. |
| 119 |
GetOutboundGroupSession(id.RoomID) (*OutboundGroupSession, error) |
| 120 |
// RemoveOutboundGroupSession removes the stored outbound Megolm session for the given room ID. |
| 121 |
RemoveOutboundGroupSession(id.RoomID) error |
| 122 |
|
| 123 |
// ValidateMessageIndex validates that the given message details aren't from a replay attack. |
| 124 |
// |
| 125 |
// Implementations should store a map from (senderKey, sessionID, index) to (eventID, timestamp), then use that map |
| 126 |
// to check whether or not the message index is valid: |
| 127 |
// |
| 128 |
// * If the map key doesn't exist, the given values should be stored and this should return true. |
| 129 |
// * If the map key exists and the stored values match the given values, this should return true. |
| 130 |
// * If the map key exists, but the stored values do not match the given values, this should return false. |
| 131 |
ValidateMessageIndex(senderKey id.SenderKey, sessionID id.SessionID, eventID id.EventID, index uint, timestamp int64) bool |
| 132 |
|
| 133 |
// GetDevices returns a map from device ID to DeviceIdentity containing all devices of a given user. |
| 134 |
GetDevices(id.UserID) (map[id.DeviceID]*DeviceIdentity, error) |
| 135 |
// GetDevice returns a specific device of a given user. |
| 136 |
GetDevice(id.UserID, id.DeviceID) (*DeviceIdentity, error) |
| 137 |
// PutDevice stores a single device for a user, replacing it if it exists already. |
| 138 |
PutDevice(id.UserID, *DeviceIdentity) error |
| 139 |
// PutDevices overrides the stored device list for the given user with the given list. |
| 140 |
PutDevices(id.UserID, map[id.DeviceID]*DeviceIdentity) error |
| 141 |
// FindDeviceByKey finds a specific device by its identity key. |
| 142 |
FindDeviceByKey(id.UserID, id.IdentityKey) (*DeviceIdentity, error) |
| 143 |
// FilterTrackedUsers returns a filtered version of the given list that only includes user IDs whose device lists |
| 144 |
// have been stored with PutDevices. A user is considered tracked even if the PutDevices list was empty. |
| 145 |
FilterTrackedUsers([]id.UserID) []id.UserID |
| 146 |
|
| 147 |
// PutCrossSigningKey stores a cross-signing key of some user along with its usage. |
| 148 |
PutCrossSigningKey(id.UserID, id.CrossSigningUsage, id.Ed25519) error |
| 149 |
// GetCrossSigningKeys retrieves a user's stored cross-signing keys. |
| 150 |
GetCrossSigningKeys(id.UserID) (map[id.CrossSigningUsage]id.Ed25519, error) |
| 151 |
// PutSignature stores a signature of a cross-signing or device key along with the signer's user ID and key. |
| 152 |
PutSignature(id.UserID, id.Ed25519, id.UserID, id.Ed25519, string) error |
| 153 |
// GetSignaturesForKeyBy returns the signatures for a cross-signing or device key by the given signer. |
| 154 |
GetSignaturesForKeyBy(id.UserID, id.Ed25519, id.UserID) (map[id.Ed25519]string, error) |
| 155 |
// IsKeySignedBy returns whether a cross-signing or device key is signed by the given signer. |
| 156 |
IsKeySignedBy(id.UserID, id.Ed25519, id.UserID, id.Ed25519) (bool, error) |
| 157 |
// DropSignaturesByKey deletes the signatures made by the given user and key from the store. It returns the number of signatures deleted. |
| 158 |
DropSignaturesByKey(id.UserID, id.Ed25519) (int64, error) |
| 159 |
} |
| 160 |
|
| 161 |
type messageIndexKey struct { |
| 162 |
SenderKey id.SenderKey |
| 163 |
SessionID id.SessionID |
| 164 |
Index uint |
| 165 |
} |
| 166 |
|
| 167 |
type messageIndexValue struct { |
| 168 |
EventID id.EventID |
| 169 |
Timestamp int64 |
| 170 |
} |
| 171 |
|
| 172 |
// GobStore is a simple Store implementation that dumps everything into a .gob file. |
| 173 |
// |
| 174 |
// Deprecated: this is not atomic and can lose data. Using SQLCryptoStore or a custom implementation is recommended. |
| 175 |
type GobStore struct { |
| 176 |
lock sync.RWMutex |
| 177 |
path string |
| 178 |
|
| 179 |
Account *OlmAccount |
| 180 |
Sessions map[id.SenderKey]OlmSessionList |
| 181 |
GroupSessions map[id.RoomID]map[id.SenderKey]map[id.SessionID]*InboundGroupSession |
| 182 |
WithheldGroupSessions map[id.RoomID]map[id.SenderKey]map[id.SessionID]*event.RoomKeyWithheldEventContent |
| 183 |
OutGroupSessions map[id.RoomID]*OutboundGroupSession |
| 184 |
MessageIndices map[messageIndexKey]messageIndexValue |
| 185 |
Devices map[id.UserID]map[id.DeviceID]*DeviceIdentity |
| 186 |
CrossSigningKeys map[id.UserID]map[id.CrossSigningUsage]id.Ed25519 |
| 187 |
KeySignatures map[id.UserID]map[id.Ed25519]map[id.UserID]map[id.Ed25519]string |
| 188 |
} |
| 189 |
|
| 190 |
var _ Store = (*GobStore)(nil) |
| 191 |
|
| 192 |
// NewGobStore creates a new GobStore that saves everything to the given file. |
| 193 |
// |
| 194 |
// Deprecated: this is not atomic and can lose data. Using SQLCryptoStore or a custom implementation is recommended. |
| 195 |
func NewGobStore(path string) (*GobStore, error) { |
| 196 |
gs := &GobStore{ |
| 197 |
path: path, |
| 198 |
Sessions: make(map[id.SenderKey]OlmSessionList), |
| 199 |
GroupSessions: make(map[id.RoomID]map[id.SenderKey]map[id.SessionID]*InboundGroupSession), |
| 200 |
WithheldGroupSessions: make(map[id.RoomID]map[id.SenderKey]map[id.SessionID]*event.RoomKeyWithheldEventContent), |
| 201 |
OutGroupSessions: make(map[id.RoomID]*OutboundGroupSession), |
| 202 |
MessageIndices: make(map[messageIndexKey]messageIndexValue), |
| 203 |
Devices: make(map[id.UserID]map[id.DeviceID]*DeviceIdentity), |
| 204 |
CrossSigningKeys: make(map[id.UserID]map[id.CrossSigningUsage]id.Ed25519), |
| 205 |
KeySignatures: make(map[id.UserID]map[id.Ed25519]map[id.UserID]map[id.Ed25519]string), |
| 206 |
} |
| 207 |
return gs, gs.load() |
| 208 |
} |
| 209 |
|
| 210 |
func (gs *GobStore) save() error { |
| 211 |
file, err := os.OpenFile(gs.path, os.O_CREATE|os.O_WRONLY, 0600) |
| 212 |
if err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
err = gob.NewEncoder(file).Encode(gs) |
| 216 |
_ = file.Close() |
| 217 |
return err |
| 218 |
} |
| 219 |
|
| 220 |
func (gs *GobStore) load() error { |
| 221 |
file, err := os.OpenFile(gs.path, os.O_RDONLY, 0600) |
| 222 |
if err != nil { |
| 223 |
if os.IsNotExist(err) { |
| 224 |
return nil |
| 225 |
} |
| 226 |
return err |
| 227 |
} |
| 228 |
err = gob.NewDecoder(file).Decode(gs) |
| 229 |
_ = file.Close() |
| 230 |
return err |
| 231 |
} |
| 232 |
|
| 233 |
func (gs *GobStore) Flush() error { |
| 234 |
gs.lock.Lock() |
| 235 |
err := gs.save() |
| 236 |
gs.lock.Unlock() |
| 237 |
return err |
| 238 |
} |
| 239 |
|
| 240 |
func (gs *GobStore) GetAccount() (*OlmAccount, error) { |
| 241 |
return gs.Account, nil |
| 242 |
} |
| 243 |
|
| 244 |
func (gs *GobStore) PutAccount(account *OlmAccount) error { |
| 245 |
gs.lock.Lock() |
| 246 |
gs.Account = account |
| 247 |
err := gs.save() |
| 248 |
gs.lock.Unlock() |
| 249 |
return err |
| 250 |
} |
| 251 |
|
| 252 |
func (gs *GobStore) GetSessions(senderKey id.SenderKey) (OlmSessionList, error) { |
| 253 |
gs.lock.Lock() |
| 254 |
sessions, ok := gs.Sessions[senderKey] |
| 255 |
if !ok { |
| 256 |
sessions = []*OlmSession{} |
| 257 |
gs.Sessions[senderKey] = sessions |
| 258 |
} |
| 259 |
gs.lock.Unlock() |
| 260 |
return sessions, nil |
| 261 |
} |
| 262 |
|
| 263 |
func (gs *GobStore) AddSession(senderKey id.SenderKey, session *OlmSession) error { |
| 264 |
gs.lock.Lock() |
| 265 |
sessions, _ := gs.Sessions[senderKey] |
| 266 |
gs.Sessions[senderKey] = append(sessions, session) |
| 267 |
sort.Sort(gs.Sessions[senderKey]) |
| 268 |
err := gs.save() |
| 269 |
gs.lock.Unlock() |
| 270 |
return err |
| 271 |
} |
| 272 |
|
| 273 |
func (gs *GobStore) UpdateSession(_ id.SenderKey, _ *OlmSession) error { |
| 274 |
// we don't need to do anything here because the session is a pointer and already stored in our map |
| 275 |
return gs.save() |
| 276 |
} |
| 277 |
|
| 278 |
func (gs *GobStore) HasSession(senderKey id.SenderKey) bool { |
| 279 |
gs.lock.RLock() |
| 280 |
sessions, ok := gs.Sessions[senderKey] |
| 281 |
gs.lock.RUnlock() |
| 282 |
return ok && len(sessions) > 0 && !sessions[0].Expired() |
| 283 |
} |
| 284 |
|
| 285 |
func (gs *GobStore) GetLatestSession(senderKey id.SenderKey) (*OlmSession, error) { |
| 286 |
gs.lock.RLock() |
| 287 |
sessions, ok := gs.Sessions[senderKey] |
| 288 |
gs.lock.RUnlock() |
| 289 |
if !ok || len(sessions) == 0 { |
| 290 |
return nil, nil |
| 291 |
} |
| 292 |
return sessions[0], nil |
| 293 |
} |
| 294 |
|
| 295 |
func (gs *GobStore) getGroupSessions(roomID id.RoomID, senderKey id.SenderKey) map[id.SessionID]*InboundGroupSession { |
| 296 |
room, ok := gs.GroupSessions[roomID] |
| 297 |
if !ok { |
| 298 |
room = make(map[id.SenderKey]map[id.SessionID]*InboundGroupSession) |
| 299 |
gs.GroupSessions[roomID] = room |
| 300 |
} |
| 301 |
sender, ok := room[senderKey] |
| 302 |
if !ok { |
| 303 |
sender = make(map[id.SessionID]*InboundGroupSession) |
| 304 |
room[senderKey] = sender |
| 305 |
} |
| 306 |
return sender |
| 307 |
} |
| 308 |
|
| 309 |
func (gs *GobStore) PutGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID, igs *InboundGroupSession) error { |
| 310 |
gs.lock.Lock() |
| 311 |
gs.getGroupSessions(roomID, senderKey)[sessionID] = igs |
| 312 |
err := gs.save() |
| 313 |
gs.lock.Unlock() |
| 314 |
return err |
| 315 |
} |
| 316 |
|
| 317 |
func (gs *GobStore) GetGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID) (*InboundGroupSession, error) { |
| 318 |
gs.lock.Lock() |
| 319 |
session, ok := gs.getGroupSessions(roomID, senderKey)[sessionID] |
| 320 |
if !ok { |
| 321 |
withheld, ok := gs.getWithheldGroupSessions(roomID, senderKey)[sessionID] |
| 322 |
gs.lock.Unlock() |
| 323 |
if ok { |
| 324 |
return nil, fmt.Errorf("%w (%s)", ErrGroupSessionWithheld, withheld.Code) |
| 325 |
} |
| 326 |
return nil, nil |
| 327 |
} |
| 328 |
gs.lock.Unlock() |
| 329 |
return session, nil |
| 330 |
} |
| 331 |
|
| 332 |
func (gs *GobStore) getWithheldGroupSessions(roomID id.RoomID, senderKey id.SenderKey) map[id.SessionID]*event.RoomKeyWithheldEventContent { |
| 333 |
room, ok := gs.WithheldGroupSessions[roomID] |
| 334 |
if !ok { |
| 335 |
room = make(map[id.SenderKey]map[id.SessionID]*event.RoomKeyWithheldEventContent) |
| 336 |
gs.WithheldGroupSessions[roomID] = room |
| 337 |
} |
| 338 |
sender, ok := room[senderKey] |
| 339 |
if !ok { |
| 340 |
sender = make(map[id.SessionID]*event.RoomKeyWithheldEventContent) |
| 341 |
room[senderKey] = sender |
| 342 |
} |
| 343 |
return sender |
| 344 |
} |
| 345 |
|
| 346 |
func (gs *GobStore) PutWithheldGroupSession(content event.RoomKeyWithheldEventContent) error { |
| 347 |
gs.lock.Lock() |
| 348 |
gs.getWithheldGroupSessions(content.RoomID, content.SenderKey)[content.SessionID] = &content |
| 349 |
err := gs.save() |
| 350 |
gs.lock.Unlock() |
| 351 |
return err |
| 352 |
} |
| 353 |
|
| 354 |
func (gs *GobStore) GetWithheldGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID) (*event.RoomKeyWithheldEventContent, error) { |
| 355 |
gs.lock.Lock() |
| 356 |
session, ok := gs.getWithheldGroupSessions(roomID, senderKey)[sessionID] |
| 357 |
gs.lock.Unlock() |
| 358 |
if !ok { |
| 359 |
return nil, nil |
| 360 |
} |
| 361 |
return session, nil |
| 362 |
} |
| 363 |
|
| 364 |
func (gs *GobStore) GetGroupSessionsForRoom(roomID id.RoomID) ([]*InboundGroupSession, error) { |
| 365 |
gs.lock.Lock() |
| 366 |
defer gs.lock.Unlock() |
| 367 |
room, ok := gs.GroupSessions[roomID] |
| 368 |
if !ok { |
| 369 |
return []*InboundGroupSession{}, nil |
| 370 |
} |
| 371 |
var result []*InboundGroupSession |
| 372 |
for _, sessions := range room { |
| 373 |
for _, session := range sessions { |
| 374 |
result = append(result, session) |
| 375 |
} |
| 376 |
} |
| 377 |
return result, nil |
| 378 |
} |
| 379 |
|
| 380 |
func (gs *GobStore) GetAllGroupSessions() ([]*InboundGroupSession, error) { |
| 381 |
gs.lock.Lock() |
| 382 |
var result []*InboundGroupSession |
| 383 |
for _, room := range gs.GroupSessions { |
| 384 |
for _, sessions := range room { |
| 385 |
for _, session := range sessions { |
| 386 |
result = append(result, session) |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
gs.lock.Unlock() |
| 391 |
return result, nil |
| 392 |
} |
| 393 |
|
| 394 |
func (gs *GobStore) AddOutboundGroupSession(session *OutboundGroupSession) error { |
| 395 |
gs.lock.Lock() |
| 396 |
gs.OutGroupSessions[session.RoomID] = session |
| 397 |
err := gs.save() |
| 398 |
gs.lock.Unlock() |
| 399 |
return err |
| 400 |
} |
| 401 |
|
| 402 |
func (gs *GobStore) UpdateOutboundGroupSession(_ *OutboundGroupSession) error { |
| 403 |
// we don't need to do anything here because the session is a pointer and already stored in our map |
| 404 |
return gs.save() |
| 405 |
} |
| 406 |
|
| 407 |
func (gs *GobStore) GetOutboundGroupSession(roomID id.RoomID) (*OutboundGroupSession, error) { |
| 408 |
gs.lock.RLock() |
| 409 |
session, ok := gs.OutGroupSessions[roomID] |
| 410 |
gs.lock.RUnlock() |
| 411 |
if !ok { |
| 412 |
return nil, nil |
| 413 |
} |
| 414 |
return session, nil |
| 415 |
} |
| 416 |
|
| 417 |
func (gs *GobStore) RemoveOutboundGroupSession(roomID id.RoomID) error { |
| 418 |
gs.lock.Lock() |
| 419 |
session, ok := gs.OutGroupSessions[roomID] |
| 420 |
if !ok || session == nil { |
| 421 |
gs.lock.Unlock() |
| 422 |
return nil |
| 423 |
} |
| 424 |
delete(gs.OutGroupSessions, roomID) |
| 425 |
gs.lock.Unlock() |
| 426 |
return nil |
| 427 |
} |
| 428 |
|
| 429 |
func (gs *GobStore) ValidateMessageIndex(senderKey id.SenderKey, sessionID id.SessionID, eventID id.EventID, index uint, timestamp int64) bool { |
| 430 |
gs.lock.Lock() |
| 431 |
defer gs.lock.Unlock() |
| 432 |
key := messageIndexKey{ |
| 433 |
SenderKey: senderKey, |
| 434 |
SessionID: sessionID, |
| 435 |
Index: index, |
| 436 |
} |
| 437 |
val, ok := gs.MessageIndices[key] |
| 438 |
if !ok { |
| 439 |
gs.MessageIndices[key] = messageIndexValue{ |
| 440 |
EventID: eventID, |
| 441 |
Timestamp: timestamp, |
| 442 |
} |
| 443 |
_ = gs.save() |
| 444 |
return true |
| 445 |
} |
| 446 |
if val.EventID != eventID || val.Timestamp != timestamp { |
| 447 |
return false |
| 448 |
} |
| 449 |
return true |
| 450 |
} |
| 451 |
|
| 452 |
func (gs *GobStore) GetDevices(userID id.UserID) (map[id.DeviceID]*DeviceIdentity, error) { |
| 453 |
gs.lock.RLock() |
| 454 |
devices, ok := gs.Devices[userID] |
| 455 |
if !ok { |
| 456 |
devices = nil |
| 457 |
} |
| 458 |
gs.lock.RUnlock() |
| 459 |
return devices, nil |
| 460 |
} |
| 461 |
|
| 462 |
func (gs *GobStore) GetDevice(userID id.UserID, deviceID id.DeviceID) (*DeviceIdentity, error) { |
| 463 |
gs.lock.RLock() |
| 464 |
defer gs.lock.RUnlock() |
| 465 |
devices, ok := gs.Devices[userID] |
| 466 |
if !ok { |
| 467 |
return nil, nil |
| 468 |
} |
| 469 |
device, ok := devices[deviceID] |
| 470 |
if !ok { |
| 471 |
return nil, nil |
| 472 |
} |
| 473 |
return device, nil |
| 474 |
} |
| 475 |
|
| 476 |
func (gs *GobStore) FindDeviceByKey(userID id.UserID, identityKey id.IdentityKey) (*DeviceIdentity, error) { |
| 477 |
gs.lock.RLock() |
| 478 |
defer gs.lock.RUnlock() |
| 479 |
devices, ok := gs.Devices[userID] |
| 480 |
if !ok { |
| 481 |
return nil, nil |
| 482 |
} |
| 483 |
for _, device := range devices { |
| 484 |
if device.IdentityKey == identityKey { |
| 485 |
return device, nil |
| 486 |
} |
| 487 |
} |
| 488 |
return nil, nil |
| 489 |
} |
| 490 |
|
| 491 |
func (gs *GobStore) PutDevice(userID id.UserID, device *DeviceIdentity) error { |
| 492 |
gs.lock.Lock() |
| 493 |
devices, ok := gs.Devices[userID] |
| 494 |
if !ok { |
| 495 |
devices = make(map[id.DeviceID]*DeviceIdentity) |
| 496 |
gs.Devices[userID] = devices |
| 497 |
} |
| 498 |
devices[device.DeviceID] = device |
| 499 |
err := gs.save() |
| 500 |
gs.lock.Unlock() |
| 501 |
return err |
| 502 |
} |
| 503 |
|
| 504 |
func (gs *GobStore) PutDevices(userID id.UserID, devices map[id.DeviceID]*DeviceIdentity) error { |
| 505 |
gs.lock.Lock() |
| 506 |
gs.Devices[userID] = devices |
| 507 |
err := gs.save() |
| 508 |
gs.lock.Unlock() |
| 509 |
return err |
| 510 |
} |
| 511 |
|
| 512 |
func (gs *GobStore) FilterTrackedUsers(users []id.UserID) []id.UserID { |
| 513 |
gs.lock.RLock() |
| 514 |
var ptr int |
| 515 |
for _, userID := range users { |
| 516 |
_, ok := gs.Devices[userID] |
| 517 |
if ok { |
| 518 |
users[ptr] = userID |
| 519 |
ptr++ |
| 520 |
} |
| 521 |
} |
| 522 |
gs.lock.RUnlock() |
| 523 |
return users[:ptr] |
| 524 |
} |
| 525 |
|
| 526 |
func (gs *GobStore) PutCrossSigningKey(userID id.UserID, usage id.CrossSigningUsage, key id.Ed25519) error { |
| 527 |
gs.lock.RLock() |
| 528 |
userKeys, ok := gs.CrossSigningKeys[userID] |
| 529 |
if !ok { |
| 530 |
userKeys = make(map[id.CrossSigningUsage]id.Ed25519) |
| 531 |
gs.CrossSigningKeys[userID] = userKeys |
| 532 |
} |
| 533 |
userKeys[usage] = key |
| 534 |
err := gs.save() |
| 535 |
gs.lock.RUnlock() |
| 536 |
return err |
| 537 |
} |
| 538 |
|
| 539 |
func (gs *GobStore) GetCrossSigningKeys(userID id.UserID) (map[id.CrossSigningUsage]id.Ed25519, error) { |
| 540 |
gs.lock.RLock() |
| 541 |
defer gs.lock.RUnlock() |
| 542 |
keys, ok := gs.CrossSigningKeys[userID] |
| 543 |
if !ok { |
| 544 |
return map[id.CrossSigningUsage]id.Ed25519{}, nil |
| 545 |
} |
| 546 |
return keys, nil |
| 547 |
} |
| 548 |
|
| 549 |
func (gs *GobStore) PutSignature(signedUserID id.UserID, signedKey id.Ed25519, signerUserID id.UserID, signerKey id.Ed25519, signature string) error { |
| 550 |
gs.lock.RLock() |
| 551 |
signedUserSigs, ok := gs.KeySignatures[signedUserID] |
| 552 |
if !ok { |
| 553 |
signedUserSigs = make(map[id.Ed25519]map[id.UserID]map[id.Ed25519]string) |
| 554 |
gs.KeySignatures[signedUserID] = signedUserSigs |
| 555 |
} |
| 556 |
signaturesForKey, ok := signedUserSigs[signedKey] |
| 557 |
if !ok { |
| 558 |
signaturesForKey = make(map[id.UserID]map[id.Ed25519]string) |
| 559 |
signedUserSigs[signedKey] = signaturesForKey |
| 560 |
} |
| 561 |
signedByUser, ok := signaturesForKey[signerUserID] |
| 562 |
if !ok { |
| 563 |
signedByUser = make(map[id.Ed25519]string) |
| 564 |
signaturesForKey[signerUserID] = signedByUser |
| 565 |
} |
| 566 |
signedByUser[signerKey] = signature |
| 567 |
err := gs.save() |
| 568 |
gs.lock.RUnlock() |
| 569 |
return err |
| 570 |
} |
| 571 |
|
| 572 |
func (gs *GobStore) GetSignaturesForKeyBy(userID id.UserID, key id.Ed25519, signerID id.UserID) (map[id.Ed25519]string, error) { |
| 573 |
gs.lock.RLock() |
| 574 |
defer gs.lock.RUnlock() |
| 575 |
userKeys, ok := gs.KeySignatures[userID] |
| 576 |
if !ok { |
| 577 |
return map[id.Ed25519]string{}, nil |
| 578 |
} |
| 579 |
sigsForKey, ok := userKeys[key] |
| 580 |
if !ok { |
| 581 |
return map[id.Ed25519]string{}, nil |
| 582 |
} |
| 583 |
sigsBySigner, ok := sigsForKey[signerID] |
| 584 |
if !ok { |
| 585 |
return map[id.Ed25519]string{}, nil |
| 586 |
} |
| 587 |
return sigsBySigner, nil |
| 588 |
} |
| 589 |
|
| 590 |
func (gs *GobStore) IsKeySignedBy(userID id.UserID, key id.Ed25519, signerID id.UserID, signerKey id.Ed25519) (bool, error) { |
| 591 |
sigs, err := gs.GetSignaturesForKeyBy(userID, key, signerID) |
| 592 |
if err != nil { |
| 593 |
return false, err |
| 594 |
} |
| 595 |
_, ok := sigs[signerKey] |
| 596 |
return ok, nil |
| 597 |
} |
| 598 |
|
| 599 |
func (gs *GobStore) DropSignaturesByKey(userID id.UserID, key id.Ed25519) (int64, error) { |
| 600 |
var count int64 |
| 601 |
gs.lock.RLock() |
| 602 |
for _, userSigs := range gs.KeySignatures { |
| 603 |
for _, keySigs := range userSigs { |
| 604 |
if signedBySigner, ok := keySigs[userID]; ok { |
| 605 |
if _, ok := signedBySigner[key]; ok { |
| 606 |
count++ |
| 607 |
delete(signedBySigner, key) |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
gs.lock.RUnlock() |
| 613 |
return count, nil |
| 614 |
} |