summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-js-sdk/src/crypto/dehydration.ts
blob: 373b236b256991caa4a52b741406777fd3d305c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright 2020-2021 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import anotherjson from "another-json";

import type { IDeviceKeys, IOneTimeKey } from "../@types/crypto";
import { decodeBase64, encodeBase64 } from "./olmlib";
import { IndexedDBCryptoStore } from "../crypto/store/indexeddb-crypto-store";
import { decryptAES, encryptAES } from "./aes";
import { logger } from "../logger";
import { Crypto } from "./index";
import { Method } from "../http-api";
import { SecretStorageKeyDescription } from "../secret-storage";

export interface IDehydratedDevice {
    device_id: string; // eslint-disable-line camelcase
    device_data: SecretStorageKeyDescription & {
        // eslint-disable-line camelcase
        algorithm: string;
        account: string; // pickle
    };
}

export interface IDehydratedDeviceKeyInfo {
    passphrase?: string;
}

export const DEHYDRATION_ALGORITHM = "org.matrix.msc2697.v1.olm.libolm_pickle";

const oneweek = 7 * 24 * 60 * 60 * 1000;

export class DehydrationManager {
    private inProgress = false;
    private timeoutId: any;
    private key?: Uint8Array;
    private keyInfo?: { [props: string]: any };
    private deviceDisplayName?: string;

    public constructor(private readonly crypto: Crypto) {
        this.getDehydrationKeyFromCache();
    }

    public getDehydrationKeyFromCache(): Promise<void> {
        return this.crypto.cryptoStore.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
            this.crypto.cryptoStore.getSecretStorePrivateKey(
                txn,
                async (result) => {
                    if (result) {
                        const { key, keyInfo, deviceDisplayName, time } = result;
                        const pickleKey = Buffer.from(this.crypto.olmDevice.pickleKey);
                        const decrypted = await decryptAES(key, pickleKey, DEHYDRATION_ALGORITHM);
                        this.key = decodeBase64(decrypted);
                        this.keyInfo = keyInfo;
                        this.deviceDisplayName = deviceDisplayName;
                        const now = Date.now();
                        const delay = Math.max(1, time + oneweek - now);
                        this.timeoutId = global.setTimeout(this.dehydrateDevice.bind(this), delay);
                    }
                },
                "dehydration",
            );
        });
    }

    /** set the key, and queue periodic dehydration to the server in the background */
    public async setKeyAndQueueDehydration(
        key: Uint8Array,
        keyInfo: { [props: string]: any } = {},
        deviceDisplayName?: string,
    ): Promise<void> {
        const matches = await this.setKey(key, keyInfo, deviceDisplayName);
        if (!matches) {
            // start dehydration in the background
            this.dehydrateDevice();
        }
    }

    public async setKey(
        key: Uint8Array,
        keyInfo: { [props: string]: any } = {},
        deviceDisplayName?: string,
    ): Promise<boolean | undefined> {
        if (!key) {
            // unsetting the key -- cancel any pending dehydration task
            if (this.timeoutId) {
                global.clearTimeout(this.timeoutId);
                this.timeoutId = undefined;
            }
            // clear storage
            await this.crypto.cryptoStore.doTxn("readwrite", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
                this.crypto.cryptoStore.storeSecretStorePrivateKey(txn, "dehydration", null);
            });
            this.key = undefined;
            this.keyInfo = undefined;
            return;
        }

        // Check to see if it's the same key as before.  If it's different,
        // dehydrate a new device.  If it's the same, we can keep the same
        // device.  (Assume that keyInfo and deviceDisplayName will be the
        // same if the key is the same.)
        let matches: boolean = !!this.key && key.length == this.key.length;
        for (let i = 0; matches && i < key.length; i++) {
            if (key[i] != this.key![i]) {
                matches = false;
            }
        }
        if (!matches) {
            this.key = key;
            this.keyInfo = keyInfo;
            this.deviceDisplayName = deviceDisplayName;
        }
        return matches;
    }

    /** returns the device id of the newly created dehydrated device */
    public async dehydrateDevice(): Promise<string | undefined> {
        if (this.inProgress) {
            logger.log("Dehydration already in progress -- not starting new dehydration");
            return;
        }
        this.inProgress = true;
        if (this.timeoutId) {
            global.clearTimeout(this.timeoutId);
            this.timeoutId = undefined;
        }
        try {
            const pickleKey = Buffer.from(this.crypto.olmDevice.pickleKey);

            // update the crypto store with the timestamp
            const key = await encryptAES(encodeBase64(this.key!), pickleKey, DEHYDRATION_ALGORITHM);
            await this.crypto.cryptoStore.doTxn("readwrite", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
                this.crypto.cryptoStore.storeSecretStorePrivateKey(txn, "dehydration", {
                    keyInfo: this.keyInfo,
                    key,
                    deviceDisplayName: this.deviceDisplayName!,
                    time: Date.now(),
                });
            });
            logger.log("Attempting to dehydrate device");

            logger.log("Creating account");
            // create the account and all the necessary keys
            const account = new global.Olm.Account();
            account.create();
            const e2eKeys = JSON.parse(account.identity_keys());

            const maxKeys = account.max_number_of_one_time_keys();
            // FIXME: generate in small batches?
            account.generate_one_time_keys(maxKeys / 2);
            account.generate_fallback_key();
            const otks: Record<string, string> = JSON.parse(account.one_time_keys());
            const fallbacks: Record<string, string> = JSON.parse(account.fallback_key());
            account.mark_keys_as_published();

            // dehydrate the account and store it on the server
            const pickledAccount = account.pickle(new Uint8Array(this.key!));

            const deviceData: { [props: string]: any } = {
                algorithm: DEHYDRATION_ALGORITHM,
                account: pickledAccount,
            };
            if (this.keyInfo!.passphrase) {
                deviceData.passphrase = this.keyInfo!.passphrase;
            }

            logger.log("Uploading account to server");
            // eslint-disable-next-line camelcase
            const dehydrateResult = await this.crypto.baseApis.http.authedRequest<{ device_id: string }>(
                Method.Put,
                "/dehydrated_device",
                undefined,
                {
                    device_data: deviceData,
                    initial_device_display_name: this.deviceDisplayName,
                },
                {
                    prefix: "/_matrix/client/unstable/org.matrix.msc2697.v2",
                },
            );

            // send the keys to the server
            const deviceId = dehydrateResult.device_id;
            logger.log("Preparing device keys", deviceId);
            const deviceKeys: IDeviceKeys = {
                algorithms: this.crypto.supportedAlgorithms,
                device_id: deviceId,
                user_id: this.crypto.userId,
                keys: {
                    [`ed25519:${deviceId}`]: e2eKeys.ed25519,
                    [`curve25519:${deviceId}`]: e2eKeys.curve25519,
                },
            };
            const deviceSignature = account.sign(anotherjson.stringify(deviceKeys));
            deviceKeys.signatures = {
                [this.crypto.userId]: {
                    [`ed25519:${deviceId}`]: deviceSignature,
                },
            };
            if (this.crypto.crossSigningInfo.getId("self_signing")) {
                await this.crypto.crossSigningInfo.signObject(deviceKeys, "self_signing");
            }

            logger.log("Preparing one-time keys");
            const oneTimeKeys: Record<string, IOneTimeKey> = {};
            for (const [keyId, key] of Object.entries(otks.curve25519)) {
                const k: IOneTimeKey = { key };
                const signature = account.sign(anotherjson.stringify(k));
                k.signatures = {
                    [this.crypto.userId]: {
                        [`ed25519:${deviceId}`]: signature,
                    },
                };
                oneTimeKeys[`signed_curve25519:${keyId}`] = k;
            }

            logger.log("Preparing fallback keys");
            const fallbackKeys: Record<string, IOneTimeKey> = {};
            for (const [keyId, key] of Object.entries(fallbacks.curve25519)) {
                const k: IOneTimeKey = { key, fallback: true };
                const signature = account.sign(anotherjson.stringify(k));
                k.signatures = {
                    [this.crypto.userId]: {
                        [`ed25519:${deviceId}`]: signature,
                    },
                };
                fallbackKeys[`signed_curve25519:${keyId}`] = k;
            }

            logger.log("Uploading keys to server");
            await this.crypto.baseApis.http.authedRequest(
                Method.Post,
                "/keys/upload/" + encodeURI(deviceId),
                undefined,
                {
                    "device_keys": deviceKeys,
                    "one_time_keys": oneTimeKeys,
                    "org.matrix.msc2732.fallback_keys": fallbackKeys,
                },
            );
            logger.log("Done dehydrating");

            // dehydrate again in a week
            this.timeoutId = global.setTimeout(this.dehydrateDevice.bind(this), oneweek);

            return deviceId;
        } finally {
            this.inProgress = false;
        }
    }

    public stop(): void {
        if (this.timeoutId) {
            global.clearTimeout(this.timeoutId);
            this.timeoutId = undefined;
        }
    }
}