| 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 | "fmt" |
| 11 | |
| 12 | "maunium.net/go/mautrix" |
| 13 | "maunium.net/go/mautrix/crypto/ssss" |
| 14 | "maunium.net/go/mautrix/crypto/utils" |
| 15 | "maunium.net/go/mautrix/event" |
| 16 | ) |
| 17 | |
| 18 | // FetchCrossSigningKeysFromSSSS fetches all the cross-signing keys from SSSS, decrypts them using the given key and stores them in the olm machine. |
| 19 | func (mach *OlmMachine) FetchCrossSigningKeysFromSSSS(key *ssss.Key) error { |
| 20 | masterKey, err := mach.retrieveDecryptXSigningKey(event.AccountDataCrossSigningMaster, key) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | selfSignKey, err := mach.retrieveDecryptXSigningKey(event.AccountDataCrossSigningSelf, key) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | userSignKey, err := mach.retrieveDecryptXSigningKey(event.AccountDataCrossSigningUser, key) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | |
| 33 | return mach.ImportCrossSigningKeys(CrossSigningSeeds{ |
| 34 | MasterKey: masterKey[:], |
| 35 | SelfSigningKey: selfSignKey[:], |
| 36 | UserSigningKey: userSignKey[:], |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | // retrieveDecryptXSigningKey retrieves the requested cross-signing key from SSSS and decrypts it using the given SSSS key. |
| 41 | func (mach *OlmMachine) retrieveDecryptXSigningKey(keyName event.Type, key *ssss.Key) ([utils.AESCTRKeyLength]byte, error) { |
| 42 | var decryptedKey [utils.AESCTRKeyLength]byte |
| 43 | var encData ssss.EncryptedAccountDataEventContent |
| 44 | |
| 45 | // retrieve and parse the account data for this key type from SSSS |
| 46 | err := mach.Client.GetAccountData(keyName.Type, &encData) |
| 47 | if err != nil { |
| 48 | return decryptedKey, err |
| 49 | } |
| 50 | |
| 51 | decrypted, err := encData.Decrypt(keyName.Type, key) |
| 52 | if err != nil { |
| 53 | return decryptedKey, err |
| 54 | } |
| 55 | copy(decryptedKey[:], decrypted) |
| 56 | return decryptedKey, nil |
| 57 | } |
| 58 | |
| 59 | // GenerateAndUploadCrossSigningKeys generates a new key with all corresponding cross-signing keys. |
| 60 | // |
| 61 | // A passphrase can be provided to generate the SSSS key. If the passphrase is empty, a random key |
| 62 | // is used. The base58-formatted recovery key is the first return parameter. |
| 63 | // |
| 64 | // The account password of the user is required for uploading keys to the server. |
| 65 | func (mach *OlmMachine) GenerateAndUploadCrossSigningKeys(userPassword, passphrase string) (string, error) { |
| 66 | key, err := mach.SSSS.GenerateAndUploadKey(passphrase) |
| 67 | if err != nil { |
| 68 | return "", fmt.Errorf("failed to generate and upload SSSS key: %w", err) |
| 69 | } |
| 70 | |
| 71 | // generate the three cross-signing keys |
| 72 | keysCache, err := mach.GenerateCrossSigningKeys() |
| 73 | if err != nil { |
| 74 | return "", err |
| 75 | } |
| 76 | |
| 77 | recoveryKey := key.RecoveryKey() |
| 78 | |
| 79 | // Store the private keys in SSSS |
| 80 | if err := mach.UploadCrossSigningKeysToSSSS(key, keysCache); err != nil { |
| 81 | return recoveryKey, fmt.Errorf("failed to upload cross-signing keys to SSSS: %w", err) |
| 82 | } |
| 83 | |
| 84 | // Publish cross-signing keys |
| 85 | err = mach.PublishCrossSigningKeys(keysCache, func(uiResp *mautrix.RespUserInteractive) interface{} { |
| 86 | return &mautrix.ReqUIAuthLogin{ |
| 87 | BaseAuthData: mautrix.BaseAuthData{ |
| 88 | Type: mautrix.AuthTypePassword, |
| 89 | Session: uiResp.Session, |
| 90 | }, |
| 91 | User: mach.Client.UserID.String(), |
| 92 | Password: userPassword, |
| 93 | } |
| 94 | }) |
| 95 | if err != nil { |
| 96 | return recoveryKey, fmt.Errorf("failed to publish cross-signing keys: %w", err) |
| 97 | } |
| 98 | |
| 99 | err = mach.SSSS.SetDefaultKeyID(key.ID) |
| 100 | if err != nil { |
| 101 | return recoveryKey, fmt.Errorf("failed to mark %s as the default key: %w", key.ID, err) |
| 102 | } |
| 103 | |
| 104 | return recoveryKey, nil |
| 105 | } |
| 106 | |
| 107 | // UploadCrossSigningKeysToSSSS stores the given cross-signing keys on the server encrypted with the given key. |
| 108 | func (mach *OlmMachine) UploadCrossSigningKeysToSSSS(key *ssss.Key, keys *CrossSigningKeysCache) error { |
| 109 | if err := mach.SSSS.SetEncryptedAccountData(event.AccountDataCrossSigningMaster, keys.MasterKey.Seed, key); err != nil { |
| 110 | return err |
| 111 | } |
| 112 | if err := mach.SSSS.SetEncryptedAccountData(event.AccountDataCrossSigningSelf, keys.SelfSigningKey.Seed, key); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | if err := mach.SSSS.SetEncryptedAccountData(event.AccountDataCrossSigningUser, keys.UserSigningKey.Seed, key); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | return nil |
| 119 | } |