summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/lib/matrix-sdk/secret-storage.js
blob: badd37914860c0a70b938f256ede692de4a7f2cf (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ServerSideSecretStorageImpl = exports.SECRET_STORAGE_ALGORITHM_V1_AES = void 0;
exports.trimTrailingEquals = trimTrailingEquals;
var _client = require("./client");
var _aes = require("./crypto/aes");
var _randomstring = require("./randomstring");
var _logger = require("./logger");
/*
Copyright 2021-2023 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.
*/

/**
 * Implementation of server-side secret storage
 *
 * @see https://spec.matrix.org/v1.6/client-server-api/#storage
 */

const SECRET_STORAGE_ALGORITHM_V1_AES = "m.secret_storage.v1.aes-hmac-sha2";

/**
 * Common base interface for Secret Storage Keys.
 *
 * The common properties for all encryption keys used in server-side secret storage.
 *
 * @see https://spec.matrix.org/v1.6/client-server-api/#key-storage
 */

/**
 * Properties for a SSSS key using the `m.secret_storage.v1.aes-hmac-sha2` algorithm.
 *
 * Corresponds to `AesHmacSha2KeyDescription` in the specification.
 *
 * @see https://spec.matrix.org/v1.6/client-server-api/#msecret_storagev1aes-hmac-sha2
 */

/**
 * Union type for secret storage keys.
 *
 * For now, this is only {@link SecretStorageKeyDescriptionAesV1}, but other interfaces may be added in future.
 */

/**
 * Information on how to generate the key from a passphrase.
 *
 * @see https://spec.matrix.org/v1.6/client-server-api/#deriving-keys-from-passphrases
 */

/**
 * Options for {@link ServerSideSecretStorageImpl#addKey}.
 */

/**
 * Return type for {@link ServerSideSecretStorageImpl#getKey}.
 */

/**
 * Return type for {@link ServerSideSecretStorageImpl#addKey}.
 */

/** Interface for managing account data on the server.
 *
 * A subset of {@link MatrixClient}.
 */

/**
 *  Application callbacks for use with {@link SecretStorage.ServerSideSecretStorageImpl}
 */

/**
 * Interface provided by SecretStorage implementations
 *
 * Normally this will just be an {@link ServerSideSecretStorageImpl}, but for backwards
 * compatibility some methods allow other implementations.
 */
exports.SECRET_STORAGE_ALGORITHM_V1_AES = SECRET_STORAGE_ALGORITHM_V1_AES;
/**
 * Implementation of Server-side secret storage.
 *
 * Secret *sharing* is *not* implemented here: this class is strictly about the storage component of
 * SSSS.
 *
 * @see https://spec.matrix.org/v1.6/client-server-api/#storage
 */
class ServerSideSecretStorageImpl {
  /**
   * Construct a new `SecretStorage`.
   *
   * Normally, it is unnecessary to call this directly, since MatrixClient automatically constructs one.
   * However, it may be useful to construct a new `SecretStorage`, if custom `callbacks` are required, for example.
   *
   * @param accountDataAdapter - interface for fetching and setting account data on the server. Normally an instance
   *   of {@link MatrixClient}.
   * @param callbacks - application level callbacks for retrieving secret keys
   */
  constructor(accountDataAdapter, callbacks) {
    this.accountDataAdapter = accountDataAdapter;
    this.callbacks = callbacks;
  }

  /**
   * Get the current default key ID for encrypting secrets.
   *
   * @returns The default key ID or null if no default key ID is set
   */
  async getDefaultKeyId() {
    const defaultKey = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.default_key");
    if (!defaultKey) return null;
    return defaultKey.key;
  }

  /**
   * Set the default key ID for encrypting secrets.
   *
   * @param keyId - The new default key ID
   */
  setDefaultKeyId(keyId) {
    return new Promise((resolve, reject) => {
      const listener = ev => {
        if (ev.getType() === "m.secret_storage.default_key" && ev.getContent().key === keyId) {
          this.accountDataAdapter.removeListener(_client.ClientEvent.AccountData, listener);
          resolve();
        }
      };
      this.accountDataAdapter.on(_client.ClientEvent.AccountData, listener);
      this.accountDataAdapter.setAccountData("m.secret_storage.default_key", {
        key: keyId
      }).catch(e => {
        this.accountDataAdapter.removeListener(_client.ClientEvent.AccountData, listener);
        reject(e);
      });
    });
  }

  /**
   * Add a key for encrypting secrets.
   *
   * @param algorithm - the algorithm used by the key.
   * @param opts - the options for the algorithm.  The properties used
   *     depend on the algorithm given.
   * @param keyId - the ID of the key.  If not given, a random
   *     ID will be generated.
   *
   * @returns An object with:
   *     keyId: the ID of the key
   *     keyInfo: details about the key (iv, mac, passphrase)
   */
  async addKey(algorithm, opts = {}, keyId) {
    if (algorithm !== SECRET_STORAGE_ALGORITHM_V1_AES) {
      throw new Error(`Unknown key algorithm ${algorithm}`);
    }
    const keyInfo = {
      algorithm
    };
    if (opts.name) {
      keyInfo.name = opts.name;
    }
    if (opts.passphrase) {
      keyInfo.passphrase = opts.passphrase;
    }
    if (opts.key) {
      const {
        iv,
        mac
      } = await (0, _aes.calculateKeyCheck)(opts.key);
      keyInfo.iv = iv;
      keyInfo.mac = mac;
    }

    // Create a unique key id. XXX: this is racey.
    if (!keyId) {
      do {
        keyId = (0, _randomstring.randomString)(32);
      } while (await this.accountDataAdapter.getAccountDataFromServer(`m.secret_storage.key.${keyId}`));
    }
    await this.accountDataAdapter.setAccountData(`m.secret_storage.key.${keyId}`, keyInfo);
    return {
      keyId,
      keyInfo
    };
  }

  /**
   * Get the key information for a given ID.
   *
   * @param keyId - The ID of the key to check
   *     for. Defaults to the default key ID if not provided.
   * @returns If the key was found, the return value is an array of
   *     the form [keyId, keyInfo].  Otherwise, null is returned.
   *     XXX: why is this an array when addKey returns an object?
   */
  async getKey(keyId) {
    if (!keyId) {
      keyId = await this.getDefaultKeyId();
    }
    if (!keyId) {
      return null;
    }
    const keyInfo = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.key." + keyId);
    return keyInfo ? [keyId, keyInfo] : null;
  }

  /**
   * Check whether we have a key with a given ID.
   *
   * @param keyId - The ID of the key to check
   *     for. Defaults to the default key ID if not provided.
   * @returns Whether we have the key.
   */
  async hasKey(keyId) {
    const key = await this.getKey(keyId);
    return Boolean(key);
  }

  /**
   * Check whether a key matches what we expect based on the key info
   *
   * @param key - the key to check
   * @param info - the key info
   *
   * @returns whether or not the key matches
   */
  async checkKey(key, info) {
    if (info.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
      if (info.mac) {
        const {
          mac
        } = await (0, _aes.calculateKeyCheck)(key, info.iv);
        return trimTrailingEquals(info.mac) === trimTrailingEquals(mac);
      } else {
        // if we have no information, we have to assume the key is right
        return true;
      }
    } else {
      throw new Error("Unknown algorithm");
    }
  }

  /**
   * Store an encrypted secret on the server.
   *
   * Details of the encryption keys to be used must previously have been stored in account data
   * (for example, via {@link ServerSideSecretStorageImpl#addKey}. {@link SecretStorageCallbacks#getSecretStorageKey} will be called to obtain a secret storage
   * key to decrypt the secret.
   *
   * @param name - The name of the secret - i.e., the "event type" to be stored in the account data
   * @param secret - The secret contents.
   * @param keys - The IDs of the keys to use to encrypt the secret, or null/undefined to use the default key.
   */
  async store(name, secret, keys) {
    const encrypted = {};
    if (!keys) {
      const defaultKeyId = await this.getDefaultKeyId();
      if (!defaultKeyId) {
        throw new Error("No keys specified and no default key present");
      }
      keys = [defaultKeyId];
    }
    if (keys.length === 0) {
      throw new Error("Zero keys given to encrypt with!");
    }
    for (const keyId of keys) {
      // get key information from key storage
      const keyInfo = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.key." + keyId);
      if (!keyInfo) {
        throw new Error("Unknown key: " + keyId);
      }

      // encrypt secret, based on the algorithm
      if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
        const keys = {
          [keyId]: keyInfo
        };
        const [, encryption] = await this.getSecretStorageKey(keys, name);
        encrypted[keyId] = await encryption.encrypt(secret);
      } else {
        _logger.logger.warn("unknown algorithm for secret storage key " + keyId + ": " + keyInfo.algorithm);
        // do nothing if we don't understand the encryption algorithm
      }
    }

    // save encrypted secret
    await this.accountDataAdapter.setAccountData(name, {
      encrypted
    });
  }

  /**
   * Get a secret from storage, and decrypt it.
   *
   * {@link SecretStorageCallbacks#getSecretStorageKey} will be called to obtain a secret storage
   * key to decrypt the secret.
   *
   * @param name - the name of the secret - i.e., the "event type" stored in the account data
   *
   * @returns the decrypted contents of the secret, or "undefined" if `name` is not found in
   *    the user's account data.
   */
  async get(name) {
    const secretInfo = await this.accountDataAdapter.getAccountDataFromServer(name);
    if (!secretInfo) {
      return;
    }
    if (!secretInfo.encrypted) {
      throw new Error("Content is not encrypted!");
    }

    // get possible keys to decrypt
    const keys = {};
    for (const keyId of Object.keys(secretInfo.encrypted)) {
      // get key information from key storage
      const keyInfo = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.key." + keyId);
      const encInfo = secretInfo.encrypted[keyId];
      // only use keys we understand the encryption algorithm of
      if (keyInfo?.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
        if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
          keys[keyId] = keyInfo;
        }
      }
    }
    if (Object.keys(keys).length === 0) {
      throw new Error(`Could not decrypt ${name} because none of ` + `the keys it is encrypted with are for a supported algorithm`);
    }

    // fetch private key from app
    const [keyId, decryption] = await this.getSecretStorageKey(keys, name);
    const encInfo = secretInfo.encrypted[keyId];
    return decryption.decrypt(encInfo);
  }

  /**
   * Check if a secret is stored on the server.
   *
   * @param name - the name of the secret
   *
   * @returns map of key name to key info the secret is encrypted
   *     with, or null if it is not present or not encrypted with a trusted
   *     key
   */
  async isStored(name) {
    // check if secret exists
    const secretInfo = await this.accountDataAdapter.getAccountDataFromServer(name);
    if (!secretInfo?.encrypted) return null;
    const ret = {};

    // filter secret encryption keys with supported algorithm
    for (const keyId of Object.keys(secretInfo.encrypted)) {
      // get key information from key storage
      const keyInfo = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.key." + keyId);
      if (!keyInfo) continue;
      const encInfo = secretInfo.encrypted[keyId];

      // only use keys we understand the encryption algorithm of
      if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
        if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
          ret[keyId] = keyInfo;
        }
      }
    }
    return Object.keys(ret).length ? ret : null;
  }
  async getSecretStorageKey(keys, name) {
    if (!this.callbacks.getSecretStorageKey) {
      throw new Error("No getSecretStorageKey callback supplied");
    }
    const returned = await this.callbacks.getSecretStorageKey({
      keys
    }, name);
    if (!returned) {
      throw new Error("getSecretStorageKey callback returned falsey");
    }
    if (returned.length < 2) {
      throw new Error("getSecretStorageKey callback returned invalid data");
    }
    const [keyId, privateKey] = returned;
    if (!keys[keyId]) {
      throw new Error("App returned unknown key from getSecretStorageKey!");
    }
    if (keys[keyId].algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
      const decryption = {
        encrypt: function (secret) {
          return (0, _aes.encryptAES)(secret, privateKey, name);
        },
        decrypt: function (encInfo) {
          return (0, _aes.decryptAES)(encInfo, privateKey, name);
        }
      };
      return [keyId, decryption];
    } else {
      throw new Error("Unknown key type: " + keys[keyId].algorithm);
    }
  }
}

/** trim trailing instances of '=' from a string
 *
 * @internal
 *
 * @param input - input string
 */
exports.ServerSideSecretStorageImpl = ServerSideSecretStorageImpl;
function trimTrailingEquals(input) {
  // according to Sonar and CodeQL, a regex such as /=+$/ is superlinear.
  // Not sure I believe it, but it's easy enough to work around.

  // find the number of characters before the trailing =
  let i = input.length;
  while (i >= 1 && input.charCodeAt(i - 1) == 0x3d) i--;

  // trim to the calculated length
  if (i < input.length) {
    return input.substring(0, i);
  } else {
    return input;
  }
}