Debian Codegraph @

crypto/ssss/client.go

raw
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 "fmt"
11
12 "maunium.net/go/mautrix"
13 "maunium.net/go/mautrix/event"
14 )
15
16 // Machine contains utility methods for interacting with SSSS data on the server.
17 type Machine struct {
18 Client *mautrix.Client
19 }
20
21 func NewSSSSMachine(client *mautrix.Client) *Machine {
22 return &Machine{
23 Client: client,
24 }
25 }
26
27 type DefaultSecretStorageKeyContent struct {
28 KeyID string `json:"key"`
29 }
30
31 // GetDefaultKeyID retrieves the default key ID for this account from SSSS.
32 func (mach *Machine) GetDefaultKeyID() (string, error) {
33 var data DefaultSecretStorageKeyContent
34 err := mach.Client.GetAccountData(event.AccountDataSecretStorageDefaultKey.Type, &data)
35 if err != nil {
36 if httpErr, ok := err.(mautrix.HTTPError); ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_NOT_FOUND" {
37 return "", ErrNoDefaultKeyAccountDataEvent
38 }
39 return "", fmt.Errorf("failed to get default key account data from server: %w", err)
40 }
41 if len(data.KeyID) == 0 {
42 return "", ErrNoKeyFieldInAccountDataEvent
43 }
44 return data.KeyID, nil
45 }
46
47 // SetDefaultKeyID sets the default key ID for this account on the server.
48 func (mach *Machine) SetDefaultKeyID(keyID string) error {
49 return mach.Client.SetAccountData(event.AccountDataSecretStorageDefaultKey.Type, &DefaultSecretStorageKeyContent{keyID})
50 }
51
52 // GetKeyData gets the details about the given key ID.
53 func (mach *Machine) GetKeyData(keyID string) (keyData *KeyMetadata, err error) {
54 keyData = &KeyMetadata{id: keyID}
55 err = mach.Client.GetAccountData(fmt.Sprintf("%s.%s", event.AccountDataSecretStorageKey.Type, keyID), keyData)
56 return
57 }
58
59 // SetKeyData stores SSSS key metadata on the server.
60 func (mach *Machine) SetKeyData(keyID string, keyData *KeyMetadata) error {
61 return mach.Client.SetAccountData(fmt.Sprintf("%s.%s", event.AccountDataSecretStorageKey.Type, keyID), keyData)
62 }
63
64 // GetDefaultKeyData gets the details about the default key ID (see GetDefaultKeyID).
65 func (mach *Machine) GetDefaultKeyData() (keyID string, keyData *KeyMetadata, err error) {
66 keyID, err = mach.GetDefaultKeyID()
67 if err != nil {
68 return
69 }
70 keyData, err = mach.GetKeyData(keyID)
71 return
72 }
73
74 // GetDecryptedAccountData gets the account data event with the given event type and decrypts it using the given key.
75 func (mach *Machine) GetDecryptedAccountData(eventType event.Type, key *Key) ([]byte, error) {
76 var encData EncryptedAccountDataEventContent
77 err := mach.Client.GetAccountData(eventType.Type, &encData)
78 if err != nil {
79 return nil, err
80 }
81 return encData.Decrypt(eventType.Type, key)
82 }
83
84 // SetEncryptedAccountData encrypts the given data with the given keys and stores it on the server.
85 func (mach *Machine) SetEncryptedAccountData(eventType event.Type, data []byte, keys ...*Key) error {
86 if len(keys) == 0 {
87 return ErrNoKeyGiven
88 }
89 encrypted := make(map[string]EncryptedKeyData, len(keys))
90 for _, key := range keys {
91 encrypted[key.ID] = key.Encrypt(eventType.Type, data)
92 }
93 return mach.Client.SetAccountData(eventType.Type, &EncryptedAccountDataEventContent{Encrypted: encrypted})
94 }
95
96 // GenerateAndUploadKey generates a new SSSS key and stores the metadata on the server.
97 func (mach *Machine) GenerateAndUploadKey(passphrase string) (key *Key, err error) {
98 key, err = NewKey(passphrase)
99 if err != nil {
100 return nil, fmt.Errorf("failed to generate new key: %w", err)
101 }
102
103 err = mach.SetKeyData(key.ID, key.Metadata)
104 if err != nil {
105 err = fmt.Errorf("failed to upload key: %w", err)
106 }
107 return key, err
108 }