| 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 | "errors" |
| 11 | "fmt" |
| 12 | |
| 13 | "maunium.net/go/mautrix" |
| 14 | "maunium.net/go/mautrix/crypto/olm" |
| 15 | "maunium.net/go/mautrix/event" |
| 16 | "maunium.net/go/mautrix/id" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | ErrCrossSigningKeysNotCached = errors.New("cross-signing private keys not in cache") |
| 21 | ErrUserSigningKeyNotCached = errors.New("user-signing private key not in cache") |
| 22 | ErrSelfSigningKeyNotCached = errors.New("self-signing private key not in cache") |
| 23 | ErrSignatureUploadFail = errors.New("server-side failure uploading signatures") |
| 24 | ErrCantSignOwnMasterKey = errors.New("signing your own master key is not allowed") |
| 25 | ErrCantSignOtherDevice = errors.New("signing other users' devices is not allowed") |
| 26 | ErrUserNotInQueryResponse = errors.New("could not find user in query keys response") |
| 27 | ErrDeviceNotInQueryResponse = errors.New("could not find device in query keys response") |
| 28 | ErrOlmAccountNotLoaded = errors.New("olm account has not been loaded") |
| 29 | |
| 30 | ErrCrossSigningMasterKeyNotFound = errors.New("cross-signing master key not found") |
| 31 | ErrMasterKeyMACNotFound = errors.New("found cross-signing master key, but didn't find corresponding MAC in verification request") |
| 32 | ErrMismatchingMasterKeyMAC = errors.New("mismatching cross-signing master key MAC") |
| 33 | ) |
| 34 | |
| 35 | func (mach *OlmMachine) fetchMasterKey(device *DeviceIdentity, content *event.VerificationMacEventContent, verState *verificationState, transactionID string) (id.Ed25519, error) { |
| 36 | crossSignKeys, err := mach.CryptoStore.GetCrossSigningKeys(device.UserID) |
| 37 | if err != nil { |
| 38 | return "", fmt.Errorf("failed to fetch cross-signing keys: %w", err) |
| 39 | } |
| 40 | masterKey, ok := crossSignKeys[id.XSUsageMaster] |
| 41 | if !ok { |
| 42 | return "", ErrCrossSigningMasterKeyNotFound |
| 43 | } |
| 44 | masterKeyID := id.NewKeyID(id.KeyAlgorithmEd25519, masterKey.String()) |
| 45 | masterKeyMAC, ok := content.Mac[masterKeyID] |
| 46 | if !ok { |
| 47 | return masterKey, ErrMasterKeyMACNotFound |
| 48 | } |
| 49 | expectedMasterKeyMAC, _, err := mach.getPKAndKeysMAC(verState.sas, device.UserID, device.DeviceID, |
| 50 | mach.Client.UserID, mach.Client.DeviceID, transactionID, masterKey, masterKeyID, content.Mac) |
| 51 | if err != nil { |
| 52 | return masterKey, fmt.Errorf("failed to calculate expected MAC for master key: %w", err) |
| 53 | } |
| 54 | if masterKeyMAC != expectedMasterKeyMAC { |
| 55 | err = fmt.Errorf("%w: expected %s, got %s", ErrMismatchingMasterKeyMAC, expectedMasterKeyMAC, masterKeyMAC) |
| 56 | } |
| 57 | return masterKey, err |
| 58 | } |
| 59 | |
| 60 | // SignUser creates a cross-signing signature for a user, stores it and uploads it to the server. |
| 61 | func (mach *OlmMachine) SignUser(userID id.UserID, masterKey id.Ed25519) error { |
| 62 | if userID == mach.Client.UserID { |
| 63 | return ErrCantSignOwnMasterKey |
| 64 | } else if mach.CrossSigningKeys == nil || mach.CrossSigningKeys.UserSigningKey == nil { |
| 65 | return ErrUserSigningKeyNotCached |
| 66 | } |
| 67 | |
| 68 | masterKeyObj := mautrix.ReqKeysSignatures{ |
| 69 | UserID: userID, |
| 70 | Usage: []id.CrossSigningUsage{id.XSUsageMaster}, |
| 71 | Keys: map[id.KeyID]string{ |
| 72 | id.NewKeyID(id.KeyAlgorithmEd25519, masterKey.String()): masterKey.String(), |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | signature, err := mach.signAndUpload(masterKeyObj, userID, masterKey.String(), mach.CrossSigningKeys.UserSigningKey) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | mach.Log.Trace("Signed master key of %s with user-signing key: `%v`", userID, signature) |
| 82 | |
| 83 | if err := mach.CryptoStore.PutSignature(userID, masterKey, mach.Client.UserID, mach.CrossSigningKeys.UserSigningKey.PublicKey, signature); err != nil { |
| 84 | return fmt.Errorf("error storing signature in crypto store: %w", err) |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // SignOwnMasterKey uses the current account for signing the current user's master key and uploads the signature. |
| 91 | func (mach *OlmMachine) SignOwnMasterKey() error { |
| 92 | if mach.CrossSigningKeys == nil { |
| 93 | return ErrCrossSigningKeysNotCached |
| 94 | } else if mach.account == nil { |
| 95 | return ErrOlmAccountNotLoaded |
| 96 | } |
| 97 | |
| 98 | userID := mach.Client.UserID |
| 99 | deviceID := mach.Client.DeviceID |
| 100 | masterKey := mach.CrossSigningKeys.MasterKey.PublicKey |
| 101 | |
| 102 | masterKeyObj := mautrix.ReqKeysSignatures{ |
| 103 | UserID: userID, |
| 104 | Usage: []id.CrossSigningUsage{id.XSUsageMaster}, |
| 105 | Keys: map[id.KeyID]string{ |
| 106 | id.NewKeyID(id.KeyAlgorithmEd25519, masterKey.String()): masterKey.String(), |
| 107 | }, |
| 108 | } |
| 109 | signature, err := mach.account.Internal.SignJSON(masterKeyObj) |
| 110 | if err != nil { |
| 111 | return fmt.Errorf("failed to sign JSON: %w", err) |
| 112 | } |
| 113 | masterKeyObj.Signatures = mautrix.Signatures{ |
| 114 | userID: map[id.KeyID]string{ |
| 115 | id.NewKeyID(id.KeyAlgorithmEd25519, deviceID.String()): signature, |
| 116 | }, |
| 117 | } |
| 118 | mach.Log.Trace("Signed own master key with device %v: `%v`", deviceID, signature) |
| 119 | |
| 120 | resp, err := mach.Client.UploadSignatures(&mautrix.ReqUploadSignatures{ |
| 121 | userID: map[string]mautrix.ReqKeysSignatures{ |
| 122 | masterKey.String(): masterKeyObj, |
| 123 | }, |
| 124 | }) |
| 125 | |
| 126 | if err != nil { |
| 127 | return fmt.Errorf("error while uploading signatures: %w", err) |
| 128 | } else if len(resp.Failures) > 0 { |
| 129 | return fmt.Errorf("%w: %+v", ErrSignatureUploadFail, resp.Failures) |
| 130 | } |
| 131 | |
| 132 | if err := mach.CryptoStore.PutSignature(userID, masterKey, userID, mach.account.SigningKey(), signature); err != nil { |
| 133 | return fmt.Errorf("error storing signature in crypto store: %w", err) |
| 134 | } |
| 135 | |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | // SignOwnDevice creates a cross-signing signature for a device belonging to the current user and uploads it to the server. |
| 140 | func (mach *OlmMachine) SignOwnDevice(device *DeviceIdentity) error { |
| 141 | if device.UserID != mach.Client.UserID { |
| 142 | return ErrCantSignOtherDevice |
| 143 | } else if mach.CrossSigningKeys == nil || mach.CrossSigningKeys.SelfSigningKey == nil { |
| 144 | return ErrSelfSigningKeyNotCached |
| 145 | } |
| 146 | |
| 147 | deviceKeys, err := mach.getFullDeviceKeys(device) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | deviceKeyObj := mautrix.ReqKeysSignatures{ |
| 153 | UserID: device.UserID, |
| 154 | DeviceID: device.DeviceID, |
| 155 | Algorithms: deviceKeys.Algorithms, |
| 156 | Keys: make(map[id.KeyID]string), |
| 157 | } |
| 158 | for keyID, key := range deviceKeys.Keys { |
| 159 | deviceKeyObj.Keys[id.KeyID(keyID)] = key |
| 160 | } |
| 161 | |
| 162 | signature, err := mach.signAndUpload(deviceKeyObj, device.UserID, device.DeviceID.String(), mach.CrossSigningKeys.SelfSigningKey) |
| 163 | if err != nil { |
| 164 | return err |
| 165 | } |
| 166 | |
| 167 | mach.Log.Trace("Signed own device %s with self-signing key: `%v`", device.UserID, device.DeviceID, signature) |
| 168 | |
| 169 | if err := mach.CryptoStore.PutSignature(device.UserID, device.SigningKey, mach.Client.UserID, mach.CrossSigningKeys.SelfSigningKey.PublicKey, signature); err != nil { |
| 170 | return fmt.Errorf("error storing signature in crypto store: %w", err) |
| 171 | } |
| 172 | |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | // getFullDeviceKeys gets the full device keys object for the given device. |
| 177 | // This is used because we don't cache some of the details like list of algorithms and unsupported key types. |
| 178 | func (mach *OlmMachine) getFullDeviceKeys(device *DeviceIdentity) (*mautrix.DeviceKeys, error) { |
| 179 | devicesKeys, err := mach.Client.QueryKeys(&mautrix.ReqQueryKeys{ |
| 180 | DeviceKeys: mautrix.DeviceKeysRequest{ |
| 181 | device.UserID: mautrix.DeviceIDList{device.DeviceID}, |
| 182 | }, |
| 183 | }) |
| 184 | if err != nil { |
| 185 | return nil, fmt.Errorf("error querying device keys for %s: %w", device.DeviceID, err) |
| 186 | } |
| 187 | userKeys, ok := devicesKeys.DeviceKeys[device.UserID] |
| 188 | if !ok { |
| 189 | return nil, ErrUserNotInQueryResponse |
| 190 | } |
| 191 | deviceKeys, ok := userKeys[device.DeviceID] |
| 192 | if !ok { |
| 193 | return nil, ErrDeviceNotInQueryResponse |
| 194 | } |
| 195 | _, err = mach.validateDevice(device.UserID, device.DeviceID, deviceKeys, device) |
| 196 | return &deviceKeys, err |
| 197 | } |
| 198 | |
| 199 | // signAndUpload signs the given key signatures object and uploads it to the server. |
| 200 | func (mach *OlmMachine) signAndUpload(req mautrix.ReqKeysSignatures, userID id.UserID, signedThing string, key *olm.PkSigning) (string, error) { |
| 201 | signature, err := key.SignJSON(req) |
| 202 | if err != nil { |
| 203 | return "", fmt.Errorf("failed to sign JSON: %w", err) |
| 204 | } |
| 205 | req.Signatures = mautrix.Signatures{ |
| 206 | mach.Client.UserID: map[id.KeyID]string{ |
| 207 | id.NewKeyID(id.KeyAlgorithmEd25519, key.PublicKey.String()): signature, |
| 208 | }, |
| 209 | } |
| 210 | |
| 211 | resp, err := mach.Client.UploadSignatures(&mautrix.ReqUploadSignatures{ |
| 212 | userID: map[string]mautrix.ReqKeysSignatures{ |
| 213 | signedThing: req, |
| 214 | }, |
| 215 | }) |
| 216 | if err != nil { |
| 217 | return "", fmt.Errorf("error while uploading signatures: %w", err) |
| 218 | } else if len(resp.Failures) > 0 { |
| 219 | return "", fmt.Errorf("%w: %+v", ErrSignatureUploadFail, resp.Failures) |
| 220 | } |
| 221 | return signature, nil |
| 222 | } |