summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-js-sdk/lib/rust-crypto/rust-crypto.js
blob: 4faf66b8e8d03fc04ea815bd1b17893a611a1cd2 (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
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RustCrypto = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var RustSdkCryptoJs = _interopRequireWildcard(require("@matrix-org/matrix-sdk-crypto-js"));
var _logger = require("../logger");
var _CrossSigning = require("../crypto/CrossSigning");
var _RoomEncryptor = require("./RoomEncryptor");
var _OutgoingRequestProcessor = require("./OutgoingRequestProcessor");
var _KeyClaimManager = require("./KeyClaimManager");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
Copyright 2022 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.
*/

/**
 * An implementation of {@link CryptoBackend} using the Rust matrix-sdk-crypto.
 */
class RustCrypto {
  /** whether {@link stop} has been called */

  /** whether {@link outgoingRequestLoop} is currently running */

  /** mapping of roomId → encryptor class */

  constructor(olmMachine, http, _userId, _deviceId) {
    this.olmMachine = olmMachine;
    (0, _defineProperty2.default)(this, "globalErrorOnUnknownDevices", false);
    (0, _defineProperty2.default)(this, "stopped", false);
    (0, _defineProperty2.default)(this, "outgoingRequestLoopRunning", false);
    (0, _defineProperty2.default)(this, "roomEncryptors", {});
    (0, _defineProperty2.default)(this, "keyClaimManager", void 0);
    (0, _defineProperty2.default)(this, "outgoingRequestProcessor", void 0);
    (0, _defineProperty2.default)(this, "globalBlacklistUnverifiedDevices", false);
    this.outgoingRequestProcessor = new _OutgoingRequestProcessor.OutgoingRequestProcessor(olmMachine, http);
    this.keyClaimManager = new _KeyClaimManager.KeyClaimManager(olmMachine, this.outgoingRequestProcessor);
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // CryptoBackend implementation
  //
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  stop() {
    // stop() may be called multiple times, but attempting to close() the OlmMachine twice
    // will cause an error.
    if (this.stopped) {
      return;
    }
    this.stopped = true;
    this.keyClaimManager.stop();

    // make sure we close() the OlmMachine; doing so means that all the Rust objects will be
    // cleaned up; in particular, the indexeddb connections will be closed, which means they
    // can then be deleted.
    this.olmMachine.close();
  }
  async encryptEvent(event, _room) {
    const roomId = event.getRoomId();
    const encryptor = this.roomEncryptors[roomId];
    if (!encryptor) {
      throw new Error(`Cannot encrypt event in unconfigured room ${roomId}`);
    }
    await encryptor.encryptEvent(event);
  }
  async decryptEvent(event) {
    const roomId = event.getRoomId();
    if (!roomId) {
      // presumably, a to-device message. These are normally decrypted in preprocessToDeviceMessages
      // so the fact it has come back here suggests that decryption failed.
      //
      // once we drop support for the libolm crypto implementation, we can stop passing to-device messages
      // through decryptEvent and hence get rid of this case.
      throw new Error("to-device event was not decrypted in preprocessToDeviceMessages");
    }
    const res = await this.olmMachine.decryptRoomEvent(JSON.stringify({
      event_id: event.getId(),
      type: event.getWireType(),
      sender: event.getSender(),
      state_key: event.getStateKey(),
      content: event.getWireContent(),
      origin_server_ts: event.getTs()
    }), new RustSdkCryptoJs.RoomId(event.getRoomId()));
    return {
      clearEvent: JSON.parse(res.event),
      claimedEd25519Key: res.senderClaimedEd25519Key,
      senderCurve25519Key: res.senderCurve25519Key,
      forwardingCurve25519KeyChain: res.forwardingCurve25519KeyChain
    };
  }
  getEventEncryptionInfo(event) {
    var _event$getSenderKey;
    // TODO: make this work properly. Or better, replace it.

    const ret = {};
    ret.senderKey = (_event$getSenderKey = event.getSenderKey()) !== null && _event$getSenderKey !== void 0 ? _event$getSenderKey : undefined;
    ret.algorithm = event.getWireContent().algorithm;
    if (!ret.senderKey || !ret.algorithm) {
      ret.encrypted = false;
      return ret;
    }
    ret.encrypted = true;
    ret.authenticated = true;
    ret.mismatchedSender = true;
    return ret;
  }
  checkUserTrust(userId) {
    // TODO
    return new _CrossSigning.UserTrustLevel(false, false, false);
  }
  checkDeviceTrust(userId, deviceId) {
    // TODO
    return new _CrossSigning.DeviceTrustLevel(false, false, false, false);
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // CryptoApi implementation
  //
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  async userHasCrossSigningKeys() {
    // TODO
    return false;
  }
  prepareToEncrypt(room) {
    const encryptor = this.roomEncryptors[room.roomId];
    if (encryptor) {
      encryptor.ensureEncryptionSession();
    }
  }
  forceDiscardSession(roomId) {
    var _this$roomEncryptors$;
    return (_this$roomEncryptors$ = this.roomEncryptors[roomId]) === null || _this$roomEncryptors$ === void 0 ? void 0 : _this$roomEncryptors$.forceDiscardSession();
  }
  async exportRoomKeys() {
    // TODO
    return [];
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // SyncCryptoCallbacks implementation
  //
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Apply sync changes to the olm machine
   * @param events - the received to-device messages
   * @param oneTimeKeysCounts - the received one time key counts
   * @param unusedFallbackKeys - the received unused fallback keys
   * @returns A list of preprocessed to-device messages.
   */
  async receiveSyncChanges({
    events,
    oneTimeKeysCounts = new Map(),
    unusedFallbackKeys = new Set()
  }) {
    const result = await this.olmMachine.receiveSyncChanges(events ? JSON.stringify(events) : "[]", new RustSdkCryptoJs.DeviceLists(), oneTimeKeysCounts, unusedFallbackKeys);

    // receiveSyncChanges returns a JSON-encoded list of decrypted to-device messages.
    return JSON.parse(result);
  }

  /** called by the sync loop to preprocess incoming to-device messages
   *
   * @param events - the received to-device messages
   * @returns A list of preprocessed to-device messages.
   */
  preprocessToDeviceMessages(events) {
    // send the received to-device messages into receiveSyncChanges. We have no info on device-list changes,
    // one-time-keys, or fallback keys, so just pass empty data.
    return this.receiveSyncChanges({
      events
    });
  }

  /** called by the sync loop to preprocess one time key counts
   *
   * @param oneTimeKeysCounts - the received one time key counts
   * @returns A list of preprocessed to-device messages.
   */
  async preprocessOneTimeKeyCounts(oneTimeKeysCounts) {
    await this.receiveSyncChanges({
      oneTimeKeysCounts
    });
  }

  /** called by the sync loop to preprocess unused fallback keys
   *
   * @param unusedFallbackKeys - the received unused fallback keys
   * @returns A list of preprocessed to-device messages.
   */
  async preprocessUnusedFallbackKeys(unusedFallbackKeys) {
    await this.receiveSyncChanges({
      unusedFallbackKeys
    });
  }

  /** called by the sync loop on m.room.encrypted events
   *
   * @param room - in which the event was received
   * @param event - encryption event to be processed
   */
  async onCryptoEvent(room, event) {
    const config = event.getContent();
    const existingEncryptor = this.roomEncryptors[room.roomId];
    if (existingEncryptor) {
      existingEncryptor.onCryptoEvent(config);
    } else {
      this.roomEncryptors[room.roomId] = new _RoomEncryptor.RoomEncryptor(this.olmMachine, this.keyClaimManager, this.outgoingRequestProcessor, room, config);
    }

    // start tracking devices for any users already known to be in this room.
    const members = await room.getEncryptionTargetMembers();
    _logger.logger.debug(`[${room.roomId} encryption] starting to track devices for: `, members.map(u => `${u.userId} (${u.membership})`));
    await this.olmMachine.updateTrackedUsers(members.map(u => new RustSdkCryptoJs.UserId(u.userId)));
  }

  /** called by the sync loop after processing each sync.
   *
   * TODO: figure out something equivalent for sliding sync.
   *
   * @param syncState - information on the completed sync.
   */
  onSyncCompleted(syncState) {
    // Processing the /sync may have produced new outgoing requests which need sending, so kick off the outgoing
    // request loop, if it's not already running.
    this.outgoingRequestLoop();
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Other public functions
  //
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  /** called by the MatrixClient on a room membership event
   *
   * @param event - The matrix event which caused this event to fire.
   * @param member - The member whose RoomMember.membership changed.
   * @param oldMembership - The previous membership state. Null if it's a new member.
   */
  onRoomMembership(event, member, oldMembership) {
    const enc = this.roomEncryptors[event.getRoomId()];
    if (!enc) {
      // not encrypting in this room
      return;
    }
    enc.onRoomMembership(member);
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Outgoing requests
  //
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  async outgoingRequestLoop() {
    if (this.outgoingRequestLoopRunning) {
      return;
    }
    this.outgoingRequestLoopRunning = true;
    try {
      while (!this.stopped) {
        const outgoingRequests = await this.olmMachine.outgoingRequests();
        if (outgoingRequests.length == 0 || this.stopped) {
          // no more messages to send (or we have been told to stop): exit the loop
          return;
        }
        for (const msg of outgoingRequests) {
          await this.outgoingRequestProcessor.makeOutgoingRequest(msg);
        }
      }
    } catch (e) {
      _logger.logger.error("Error processing outgoing-message requests from rust crypto-sdk", e);
    } finally {
      this.outgoingRequestLoopRunning = false;
    }
  }
}
exports.RustCrypto = RustCrypto;
//# sourceMappingURL=rust-crypto.js.map