| 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 | "maunium.net/go/mautrix" |
| 11 | "maunium.net/go/mautrix/crypto/olm" |
| 12 | "maunium.net/go/mautrix/id" |
| 13 | ) |
| 14 | |
| 15 | func (mach *OlmMachine) storeCrossSigningKeys(crossSigningKeys map[id.UserID]mautrix.CrossSigningKeys, deviceKeys map[id.UserID]map[id.DeviceID]mautrix.DeviceKeys) { |
| 16 | for userID, userKeys := range crossSigningKeys { |
| 17 | currentKeys, err := mach.CryptoStore.GetCrossSigningKeys(userID) |
| 18 | if err != nil { |
| 19 | mach.Log.Error("Error fetching current cross-signing keys of user %v: %v", userID, err) |
| 20 | } |
| 21 | if currentKeys != nil { |
| 22 | for curKeyUsage, curKey := range currentKeys { |
| 23 | // got a new key with the same usage as an existing key |
| 24 | for _, newKeyUsage := range userKeys.Usage { |
| 25 | if newKeyUsage == curKeyUsage { |
| 26 | if _, ok := userKeys.Keys[id.NewKeyID(id.KeyAlgorithmEd25519, curKey.String())]; !ok { |
| 27 | // old key is not in the new key map so we drop signatures made by it |
| 28 | if count, err := mach.CryptoStore.DropSignaturesByKey(userID, curKey); err != nil { |
| 29 | mach.Log.Error("Error deleting old signatures: %v", err) |
| 30 | } else { |
| 31 | mach.Log.Debug("Dropped %v signatures made by key `%v` (%v) as it has been replaced", count, curKey, curKeyUsage) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | for _, key := range userKeys.Keys { |
| 40 | for _, usage := range userKeys.Usage { |
| 41 | mach.Log.Debug("Storing cross-signing key for %v: %v (type %v)", userID, key, usage) |
| 42 | if err := mach.CryptoStore.PutCrossSigningKey(userID, usage, key); err != nil { |
| 43 | mach.Log.Error("Error storing cross-signing key: %v", err) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | for signUserID, keySigs := range userKeys.Signatures { |
| 48 | for signKeyID, signature := range keySigs { |
| 49 | _, signKeyName := signKeyID.Parse() |
| 50 | signingKey := id.Ed25519(signKeyName) |
| 51 | // if the signer is one of this user's own devices, find the key from the key ID |
| 52 | if signUserID == userID { |
| 53 | ownDeviceID := id.DeviceID(signKeyName) |
| 54 | if ownDeviceKeys, ok := deviceKeys[userID][ownDeviceID]; ok { |
| 55 | signingKey = ownDeviceKeys.Keys.GetEd25519(ownDeviceID) |
| 56 | mach.Log.Debug("Treating %v as the device name", signKeyName) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | mach.Log.Debug("Verifying with key %v of user %v", signingKey, signUserID) |
| 61 | if verified, err := olm.VerifySignatureJSON(userKeys, signUserID, signKeyName, signingKey); err != nil { |
| 62 | mach.Log.Error("Error while verifying cross-signing keys: %v", err) |
| 63 | } else { |
| 64 | if verified { |
| 65 | mach.Log.Debug("Cross-signing keys verified") |
| 66 | mach.CryptoStore.PutSignature(userID, key, signUserID, signingKey, signature) |
| 67 | } else { |
| 68 | mach.Log.Error("Cross-signing keys verification unsuccessful", err) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |