| 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/json" |
| 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 |
func (mach *OlmMachine) encryptOlmEvent(session *OlmSession, recipient *DeviceIdentity, evtType event.Type, content event.Content) *event.EncryptedEventContent { |
| 20 |
evt := &DecryptedOlmEvent{ |
| 21 |
Sender: mach.Client.UserID, |
| 22 |
SenderDevice: mach.Client.DeviceID, |
| 23 |
Keys: OlmEventKeys{Ed25519: mach.account.SigningKey()}, |
| 24 |
Recipient: recipient.UserID, |
| 25 |
RecipientKeys: OlmEventKeys{Ed25519: recipient.SigningKey}, |
| 26 |
Type: evtType, |
| 27 |
Content: content, |
| 28 |
} |
| 29 |
plaintext, err := json.Marshal(evt) |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
mach.Log.Trace("Encrypting olm message for %s with session %s: %s", recipient.IdentityKey, session.ID(), session.Describe()) |
| 34 |
msgType, ciphertext := session.Encrypt(plaintext) |
| 35 |
err = mach.CryptoStore.UpdateSession(recipient.IdentityKey, session) |
| 36 |
if err != nil { |
| 37 |
mach.Log.Warn("Failed to update olm session in crypto store after encrypting: %v", err) |
| 38 |
} |
| 39 |
return &event.EncryptedEventContent{ |
| 40 |
Algorithm: id.AlgorithmOlmV1, |
| 41 |
SenderKey: mach.account.IdentityKey(), |
| 42 |
OlmCiphertext: event.OlmCiphertexts{ |
| 43 |
recipient.IdentityKey: { |
| 44 |
Type: msgType, |
| 45 |
Body: string(ciphertext), |
| 46 |
}, |
| 47 |
}, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func (mach *OlmMachine) shouldCreateNewSession(identityKey id.IdentityKey) bool { |
| 52 |
if !mach.CryptoStore.HasSession(identityKey) { |
| 53 |
return true |
| 54 |
} |
| 55 |
mach.devicesToUnwedgeLock.Lock() |
| 56 |
_, shouldUnwedge := mach.devicesToUnwedge[identityKey] |
| 57 |
if shouldUnwedge { |
| 58 |
delete(mach.devicesToUnwedge, identityKey) |
| 59 |
} |
| 60 |
mach.devicesToUnwedgeLock.Unlock() |
| 61 |
return shouldUnwedge |
| 62 |
} |
| 63 |
|
| 64 |
func (mach *OlmMachine) createOutboundSessions(input map[id.UserID]map[id.DeviceID]*DeviceIdentity) error { |
| 65 |
request := make(mautrix.OneTimeKeysRequest) |
| 66 |
for userID, devices := range input { |
| 67 |
request[userID] = make(map[id.DeviceID]id.KeyAlgorithm) |
| 68 |
for deviceID, identity := range devices { |
| 69 |
if mach.shouldCreateNewSession(identity.IdentityKey) { |
| 70 |
request[userID][deviceID] = id.KeyAlgorithmSignedCurve25519 |
| 71 |
} |
| 72 |
} |
| 73 |
if len(request[userID]) == 0 { |
| 74 |
delete(request, userID) |
| 75 |
} |
| 76 |
} |
| 77 |
if len(request) == 0 { |
| 78 |
return nil |
| 79 |
} |
| 80 |
resp, err := mach.Client.ClaimKeys(&mautrix.ReqClaimKeys{ |
| 81 |
OneTimeKeys: request, |
| 82 |
Timeout: 10 * 1000, |
| 83 |
}) |
| 84 |
if err != nil { |
| 85 |
return fmt.Errorf("failed to claim keys: %w", err) |
| 86 |
} |
| 87 |
for userID, user := range resp.OneTimeKeys { |
| 88 |
for deviceID, oneTimeKeys := range user { |
| 89 |
var oneTimeKey mautrix.OneTimeKey |
| 90 |
var keyID id.KeyID |
| 91 |
for keyID, oneTimeKey = range oneTimeKeys { |
| 92 |
break |
| 93 |
} |
| 94 |
keyAlg, keyIndex := keyID.Parse() |
| 95 |
if keyAlg != id.KeyAlgorithmSignedCurve25519 { |
| 96 |
mach.Log.Warn("Unexpected key ID algorithm in one-time key response for %s of %s: %s", deviceID, userID, keyID) |
| 97 |
continue |
| 98 |
} |
| 99 |
identity := input[userID][deviceID] |
| 100 |
if ok, err := olm.VerifySignatureJSON(oneTimeKey, userID, deviceID.String(), identity.SigningKey); err != nil { |
| 101 |
mach.Log.Error("Failed to verify signature for %s of %s: %v", deviceID, userID, err) |
| 102 |
} else if !ok { |
| 103 |
mach.Log.Warn("Invalid signature for %s of %s", deviceID, userID) |
| 104 |
} else if sess, err := mach.account.Internal.NewOutboundSession(identity.IdentityKey, oneTimeKey.Key); err != nil { |
| 105 |
mach.Log.Error("Failed to create outbound session for %s of %s: %v", deviceID, userID, err) |
| 106 |
} else { |
| 107 |
wrapped := wrapSession(sess) |
| 108 |
err = mach.CryptoStore.AddSession(identity.IdentityKey, wrapped) |
| 109 |
if err != nil { |
| 110 |
mach.Log.Error("Failed to store created session for %s of %s: %v", deviceID, userID, err) |
| 111 |
} else { |
| 112 |
mach.Log.Debug("Created new Olm session with %s/%s (OTK ID: %d)", userID, deviceID, keyIndex) |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
return nil |
| 118 |
} |