| 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 ssss |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"reflect" |
| 13 |
|
| 14 |
"maunium.net/go/mautrix/event" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
ErrNoDefaultKeyID = errors.New("could not find default key ID") |
| 19 |
ErrNoDefaultKeyAccountDataEvent = fmt.Errorf("%w: no %s event in account data", ErrNoDefaultKeyID, event.AccountDataSecretStorageDefaultKey.Type) |
| 20 |
ErrNoKeyFieldInAccountDataEvent = fmt.Errorf("%w: missing key field in account data event", ErrNoDefaultKeyID) |
| 21 |
ErrNoKeyGiven = errors.New("must provide at least one key to encrypt for") |
| 22 |
|
| 23 |
ErrNotEncryptedForKey = errors.New("data is not encrypted for given key ID") |
| 24 |
ErrKeyDataMACMismatch = errors.New("key data MAC mismatch") |
| 25 |
ErrNoPassphrase = errors.New("no passphrase data has been set for the default key") |
| 26 |
ErrUnsupportedPassphraseAlgorithm = errors.New("unsupported passphrase KDF algorithm") |
| 27 |
ErrIncorrectSSSSKey = errors.New("incorrect SSSS key") |
| 28 |
ErrInvalidRecoveryKey = errors.New("invalid recovery key") |
| 29 |
) |
| 30 |
|
| 31 |
// Algorithm is the identifier for an SSSS encryption algorithm. |
| 32 |
type Algorithm string |
| 33 |
|
| 34 |
const ( |
| 35 |
// AlgorithmAESHMACSHA2 is the current main algorithm. |
| 36 |
AlgorithmAESHMACSHA2 Algorithm = "m.secret_storage.v1.aes-hmac-sha2" |
| 37 |
// AlgorithmCurve25519AESSHA2 is the old algorithm |
| 38 |
AlgorithmCurve25519AESSHA2 Algorithm = "m.secret_storage.v1.curve25519-aes-sha2" |
| 39 |
) |
| 40 |
|
| 41 |
// PassphraseAlgorithm is the identifier for an algorithm used to derive a key from a passphrase for SSSS. |
| 42 |
type PassphraseAlgorithm string |
| 43 |
|
| 44 |
const ( |
| 45 |
// PassphraseAlgorithmPBKDF2 is the current main algorithm |
| 46 |
PassphraseAlgorithmPBKDF2 PassphraseAlgorithm = "m.pbkdf2" |
| 47 |
) |
| 48 |
|
| 49 |
type EncryptedKeyData struct { |
| 50 |
Ciphertext string `json:"ciphertext"` |
| 51 |
IV string `json:"iv"` |
| 52 |
MAC string `json:"mac"` |
| 53 |
} |
| 54 |
|
| 55 |
type EncryptedAccountDataEventContent struct { |
| 56 |
Encrypted map[string]EncryptedKeyData `json:"encrypted"` |
| 57 |
} |
| 58 |
|
| 59 |
func (ed *EncryptedAccountDataEventContent) Decrypt(eventType string, key *Key) ([]byte, error) { |
| 60 |
keyEncData, ok := ed.Encrypted[key.ID] |
| 61 |
if !ok { |
| 62 |
return nil, ErrNotEncryptedForKey |
| 63 |
} |
| 64 |
|
| 65 |
return key.Decrypt(eventType, keyEncData) |
| 66 |
} |
| 67 |
|
| 68 |
func init() { |
| 69 |
encryptedContent := reflect.TypeOf(&EncryptedAccountDataEventContent{}) |
| 70 |
event.TypeMap[event.AccountDataCrossSigningMaster] = encryptedContent |
| 71 |
event.TypeMap[event.AccountDataCrossSigningSelf] = encryptedContent |
| 72 |
event.TypeMap[event.AccountDataCrossSigningUser] = encryptedContent |
| 73 |
event.TypeMap[event.AccountDataSecretStorageDefaultKey] = reflect.TypeOf(&DefaultSecretStorageKeyContent{}) |
| 74 |
event.TypeMap[event.AccountDataSecretStorageKey] = reflect.TypeOf(&KeyMetadata{}) |
| 75 |
} |