| 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 |
"maunium.net/go/mautrix" |
| 11 |
"maunium.net/go/mautrix/crypto/olm" |
| 12 |
"maunium.net/go/mautrix/id" |
| 13 |
) |
| 14 |
|
| 15 |
type OlmAccount struct { |
| 16 |
Internal olm.Account |
| 17 |
signingKey id.SigningKey |
| 18 |
identityKey id.IdentityKey |
| 19 |
Shared bool |
| 20 |
} |
| 21 |
|
| 22 |
func NewOlmAccount() *OlmAccount { |
| 23 |
return &OlmAccount{ |
| 24 |
Internal: *olm.NewAccount(), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func (account *OlmAccount) Keys() (id.SigningKey, id.IdentityKey) { |
| 29 |
if len(account.signingKey) == 0 || len(account.identityKey) == 0 { |
| 30 |
account.signingKey, account.identityKey = account.Internal.IdentityKeys() |
| 31 |
} |
| 32 |
return account.signingKey, account.identityKey |
| 33 |
} |
| 34 |
|
| 35 |
func (account *OlmAccount) SigningKey() id.SigningKey { |
| 36 |
if len(account.signingKey) == 0 { |
| 37 |
account.signingKey, account.identityKey = account.Internal.IdentityKeys() |
| 38 |
} |
| 39 |
return account.signingKey |
| 40 |
} |
| 41 |
|
| 42 |
func (account *OlmAccount) IdentityKey() id.IdentityKey { |
| 43 |
if len(account.identityKey) == 0 { |
| 44 |
account.signingKey, account.identityKey = account.Internal.IdentityKeys() |
| 45 |
} |
| 46 |
return account.identityKey |
| 47 |
} |
| 48 |
|
| 49 |
func (account *OlmAccount) getInitialKeys(userID id.UserID, deviceID id.DeviceID) *mautrix.DeviceKeys { |
| 50 |
deviceKeys := &mautrix.DeviceKeys{ |
| 51 |
UserID: userID, |
| 52 |
DeviceID: deviceID, |
| 53 |
Algorithms: []id.Algorithm{id.AlgorithmMegolmV1, id.AlgorithmOlmV1}, |
| 54 |
Keys: map[id.DeviceKeyID]string{ |
| 55 |
id.NewDeviceKeyID(id.KeyAlgorithmCurve25519, deviceID): string(account.IdentityKey()), |
| 56 |
id.NewDeviceKeyID(id.KeyAlgorithmEd25519, deviceID): string(account.SigningKey()), |
| 57 |
}, |
| 58 |
} |
| 59 |
|
| 60 |
signature, err := account.Internal.SignJSON(deviceKeys) |
| 61 |
if err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
|
| 65 |
deviceKeys.Signatures = mautrix.Signatures{ |
| 66 |
userID: { |
| 67 |
id.NewKeyID(id.KeyAlgorithmEd25519, deviceID.String()): signature, |
| 68 |
}, |
| 69 |
} |
| 70 |
return deviceKeys |
| 71 |
} |
| 72 |
|
| 73 |
func (account *OlmAccount) getOneTimeKeys(userID id.UserID, deviceID id.DeviceID, currentOTKCount int) map[id.KeyID]mautrix.OneTimeKey { |
| 74 |
newCount := int(account.Internal.MaxNumberOfOneTimeKeys()/2) - currentOTKCount |
| 75 |
if newCount > 0 { |
| 76 |
account.Internal.GenOneTimeKeys(uint(newCount)) |
| 77 |
} |
| 78 |
oneTimeKeys := make(map[id.KeyID]mautrix.OneTimeKey) |
| 79 |
// TODO do we need unsigned curve25519 one-time keys at all? |
| 80 |
// this just signs all of them |
| 81 |
for keyID, key := range account.Internal.OneTimeKeys() { |
| 82 |
key := mautrix.OneTimeKey{Key: key} |
| 83 |
signature, _ := account.Internal.SignJSON(key) |
| 84 |
key.Signatures = mautrix.Signatures{ |
| 85 |
userID: { |
| 86 |
id.NewKeyID(id.KeyAlgorithmEd25519, deviceID.String()): signature, |
| 87 |
}, |
| 88 |
} |
| 89 |
key.IsSigned = true |
| 90 |
oneTimeKeys[id.NewKeyID(id.KeyAlgorithmSignedCurve25519, keyID)] = key |
| 91 |
} |
| 92 |
account.Internal.MarkKeysAsPublished() |
| 93 |
return oneTimeKeys |
| 94 |
} |