livekit-client 2.11.4 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/livekit-client.e2ee.worker.js +1 -1
  2. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  3. package/dist/livekit-client.e2ee.worker.mjs +25 -14
  4. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  5. package/dist/livekit-client.esm.mjs +155 -22
  6. package/dist/livekit-client.esm.mjs.map +1 -1
  7. package/dist/livekit-client.umd.js +1 -1
  8. package/dist/livekit-client.umd.js.map +1 -1
  9. package/dist/src/api/SignalClient.d.ts +2 -1
  10. package/dist/src/api/SignalClient.d.ts.map +1 -1
  11. package/dist/src/e2ee/E2eeManager.d.ts.map +1 -1
  12. package/dist/src/e2ee/KeyProvider.d.ts +8 -5
  13. package/dist/src/e2ee/KeyProvider.d.ts.map +1 -1
  14. package/dist/src/e2ee/events.d.ts +8 -3
  15. package/dist/src/e2ee/events.d.ts.map +1 -1
  16. package/dist/src/e2ee/types.d.ts +5 -1
  17. package/dist/src/e2ee/types.d.ts.map +1 -1
  18. package/dist/src/e2ee/worker/FrameCryptor.d.ts.map +1 -1
  19. package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts +4 -4
  20. package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts.map +1 -1
  21. package/dist/src/room/RTCEngine.d.ts +2 -1
  22. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  23. package/dist/src/room/Room.d.ts +2 -0
  24. package/dist/src/room/Room.d.ts.map +1 -1
  25. package/dist/src/room/events.d.ts +22 -2
  26. package/dist/src/room/events.d.ts.map +1 -1
  27. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  28. package/dist/src/room/participant/Participant.d.ts +13 -0
  29. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  30. package/dist/src/room/track/RemoteAudioTrack.d.ts.map +1 -1
  31. package/dist/src/room/track/create.d.ts.map +1 -1
  32. package/dist/src/version.d.ts +1 -1
  33. package/dist/ts4.2/src/api/SignalClient.d.ts +2 -1
  34. package/dist/ts4.2/src/e2ee/KeyProvider.d.ts +8 -5
  35. package/dist/ts4.2/src/e2ee/events.d.ts +8 -3
  36. package/dist/ts4.2/src/e2ee/types.d.ts +5 -1
  37. package/dist/ts4.2/src/e2ee/worker/ParticipantKeyHandler.d.ts +4 -4
  38. package/dist/ts4.2/src/room/RTCEngine.d.ts +2 -1
  39. package/dist/ts4.2/src/room/Room.d.ts +2 -0
  40. package/dist/ts4.2/src/room/events.d.ts +22 -2
  41. package/dist/ts4.2/src/room/participant/Participant.d.ts +13 -0
  42. package/dist/ts4.2/src/version.d.ts +1 -1
  43. package/package.json +2 -2
  44. package/src/api/SignalClient.ts +10 -0
  45. package/src/e2ee/E2eeManager.ts +6 -1
  46. package/src/e2ee/KeyProvider.ts +13 -6
  47. package/src/e2ee/events.ts +12 -3
  48. package/src/e2ee/types.ts +8 -1
  49. package/src/e2ee/worker/FrameCryptor.ts +8 -4
  50. package/src/e2ee/worker/ParticipantKeyHandler.test.ts +104 -4
  51. package/src/e2ee/worker/ParticipantKeyHandler.ts +22 -23
  52. package/src/e2ee/worker/e2ee.worker.ts +7 -2
  53. package/src/room/RTCEngine.ts +8 -2
  54. package/src/room/Room.ts +25 -0
  55. package/src/room/events.ts +23 -0
  56. package/src/room/participant/LocalParticipant.ts +1 -5
  57. package/src/room/participant/Participant.ts +47 -2
  58. package/src/room/track/RemoteAudioTrack.ts +3 -2
  59. package/src/room/track/create.ts +3 -5
  60. package/src/version.ts +1 -1
@@ -1,21 +1,26 @@
1
1
  import type Participant from '../room/participant/Participant';
2
2
  import type { CryptorError } from './errors';
3
- import type { KeyInfo } from './types';
3
+ import type { KeyInfo, RatchetResult } from './types';
4
4
  export declare enum KeyProviderEvent {
5
5
  SetKey = "setKey",
6
+ /** Event for requesting to ratchet the key used to encrypt the stream */
6
7
  RatchetRequest = "ratchetRequest",
8
+ /** Emitted when a key is ratcheted. Could be after auto-ratcheting on decryption failure or
9
+ * following a `RatchetRequest`, will contain the ratcheted key material */
7
10
  KeyRatcheted = "keyRatcheted"
8
11
  }
9
12
  export type KeyProviderCallbacks = {
10
13
  [KeyProviderEvent.SetKey]: (keyInfo: KeyInfo) => void;
11
14
  [KeyProviderEvent.RatchetRequest]: (participantIdentity?: string, keyIndex?: number) => void;
12
- [KeyProviderEvent.KeyRatcheted]: (material: CryptoKey, keyIndex?: number) => void;
15
+ [KeyProviderEvent.KeyRatcheted]: (ratchetedResult: RatchetResult, participantIdentity?: string, keyIndex?: number) => void;
13
16
  };
14
17
  export declare enum KeyHandlerEvent {
18
+ /** Emitted when a key has been ratcheted. Is emitted when any key has been ratcheted
19
+ * i.e. when the FrameCryptor tried to ratchet when decryption is failing */
15
20
  KeyRatcheted = "keyRatcheted"
16
21
  }
17
22
  export type ParticipantKeyHandlerCallbacks = {
18
- [KeyHandlerEvent.KeyRatcheted]: (material: CryptoKey, participantIdentity: string, keyIndex?: number) => void;
23
+ [KeyHandlerEvent.KeyRatcheted]: (ratchetResult: RatchetResult, participantIdentity: string, keyIndex?: number) => void;
19
24
  };
20
25
  export declare enum EncryptionEvent {
21
26
  ParticipantEncryptionStatusChanged = "participantEncryptionStatusChanged",
@@ -72,7 +72,7 @@ export interface RatchetMessage extends BaseMessage {
72
72
  data: {
73
73
  participantIdentity: string;
74
74
  keyIndex?: number;
75
- material: CryptoKey;
75
+ ratchetResult: RatchetResult;
76
76
  };
77
77
  }
78
78
  export interface ErrorMessage extends BaseMessage {
@@ -99,6 +99,10 @@ export type KeySet = {
99
99
  material: CryptoKey;
100
100
  encryptionKey: CryptoKey;
101
101
  };
102
+ export type RatchetResult = {
103
+ chainKey: ArrayBuffer;
104
+ cryptoKey: CryptoKey;
105
+ };
102
106
  export type KeyProviderOptions = {
103
107
  sharedKey: boolean;
104
108
  ratchetSalt: string;
@@ -1,6 +1,6 @@
1
1
  import type TypedEventEmitter from 'typed-emitter';
2
2
  import type { ParticipantKeyHandlerCallbacks } from '../events';
3
- import type { KeyProviderOptions, KeySet } from '../types';
3
+ import type { KeyProviderOptions, KeySet, RatchetResult } from '../types';
4
4
  declare const ParticipantKeyHandler_base: new () => TypedEventEmitter<ParticipantKeyHandlerCallbacks>;
5
5
  /**
6
6
  * ParticipantKeyHandler is responsible for providing a cryptor instance with the
@@ -54,7 +54,7 @@ export declare class ParticipantKeyHandler extends ParticipantKeyHandler_base {
54
54
  * @param keyIndex
55
55
  * @param setKey
56
56
  */
57
- ratchetKey(keyIndex?: number, setKey?: boolean): Promise<CryptoKey>;
57
+ ratchetKey(keyIndex?: number, setKey?: boolean): Promise<RatchetResult>;
58
58
  /**
59
59
  * takes in a key material with `deriveBits` and `deriveKey` set as key usages
60
60
  * and derives encryption keys from the material and sets it on the key ring buffer
@@ -68,8 +68,8 @@ export declare class ParticipantKeyHandler extends ParticipantKeyHandler_base {
68
68
  * together with the material
69
69
  * also updates the currentKeyIndex
70
70
  */
71
- setKeyFromMaterial(material: CryptoKey, keyIndex: number, emitRatchetEvent?: boolean): Promise<void>;
72
- setKeySet(keySet: KeySet, keyIndex: number, emitRatchetEvent?: boolean): void;
71
+ setKeyFromMaterial(material: CryptoKey, keyIndex: number, ratchetedResult?: RatchetResult | null): Promise<void>;
72
+ setKeySet(keySet: KeySet, keyIndex: number, ratchetedResult?: RatchetResult | null): void;
73
73
  setCurrentKeyIndex(index: number): Promise<void>;
74
74
  getCurrentKeyIndex(): number;
75
75
  /**
@@ -1,5 +1,5 @@
1
1
  import type { AddTrackRequest, ConnectionQualityUpdate, JoinResponse, StreamStateUpdate, SubscriptionPermissionUpdate, SubscriptionResponse } from '@livekit/protocol';
2
- import { DataPacket, DataPacket_Kind, DisconnectReason, ParticipantInfo, RequestResponse, Room as RoomModel, SpeakerInfo, SubscribedQualityUpdate, TrackInfo, TrackUnpublishedResponse, Transcription } from '@livekit/protocol';
2
+ import { DataPacket, DataPacket_Kind, DisconnectReason, ParticipantInfo, RequestResponse, Room as RoomModel, RoomMovedResponse, SpeakerInfo, SubscribedQualityUpdate, TrackInfo, TrackUnpublishedResponse, Transcription } from '@livekit/protocol';
3
3
  import type TypedEventEmitter from 'typed-emitter';
4
4
  import type { SignalOptions } from '../api/SignalClient';
5
5
  import { SignalClient } from '../api/SignalClient';
@@ -153,6 +153,7 @@ export type EngineEventCallbacks = {
153
153
  dcBufferStatusChanged: (isLow: boolean, kind: DataPacket_Kind) => void;
154
154
  participantUpdate: (infos: ParticipantInfo[]) => void;
155
155
  roomUpdate: (room: RoomModel) => void;
156
+ roomMoved: (room: RoomMovedResponse) => void;
156
157
  connectionQualityUpdate: (update: ConnectionQualityUpdate) => void;
157
158
  speakersChanged: (speakerUpdates: SpeakerInfo[]) => void;
158
159
  streamStateChanged: (update: StreamStateUpdate) => void;
@@ -286,6 +286,7 @@ export type RoomEventCallbacks = {
286
286
  reconnected: () => void;
287
287
  disconnected: (reason?: DisconnectReason) => void;
288
288
  connectionStateChanged: (state: ConnectionState) => void;
289
+ moved: (name: string, token: string) => void;
289
290
  mediaDevicesChanged: () => void;
290
291
  participantConnected: (participant: RemoteParticipant) => void;
291
292
  participantDisconnected: (participant: RemoteParticipant) => void;
@@ -324,5 +325,6 @@ export type RoomEventCallbacks = {
324
325
  chatMessage: (message: ChatMessage, participant?: RemoteParticipant | LocalParticipant) => void;
325
326
  localTrackSubscribed: (publication: LocalTrackPublication, participant: LocalParticipant) => void;
326
327
  metricsReceived: (metrics: MetricsBatch, participant?: Participant) => void;
328
+ participantActive: (participant: Participant) => void;
327
329
  };
328
330
  //# sourceMappingURL=Room.d.ts.map
@@ -45,6 +45,15 @@ export declare enum RoomEvent {
45
45
  * args: ([[ConnectionState]])
46
46
  */
47
47
  ConnectionStateChanged = "connectionStateChanged",
48
+ /**
49
+ * When participant has been moved to a different room by the service request.
50
+ * The behavior looks like the participant has been disconnected and reconnected to a different room
51
+ * seamlessly without connection state transition.
52
+ * A new token will be provided for reconnecting to the new room if needed.
53
+ *
54
+ * args: ([[room: string, token: string]])
55
+ */
56
+ Moved = "moved",
48
57
  /**
49
58
  * When input or output devices on the machine have changed.
50
59
  */
@@ -169,6 +178,12 @@ export declare enum RoomEvent {
169
178
  * args: (changedAttributes: [[Record<string, string]], participant: [[Participant]])
170
179
  */
171
180
  ParticipantAttributesChanged = "participantAttributesChanged",
181
+ /**
182
+ * Emitted when the participant's state changes to ACTIVE and is ready to send/receive data messages
183
+ *
184
+ * args: (participant: [[Participant]])
185
+ */
186
+ ParticipantActive = "participantActive",
172
187
  /**
173
188
  * Room metadata is a simple way for app-specific state to be pushed to
174
189
  * all users.
@@ -459,7 +474,11 @@ export declare enum ParticipantEvent {
459
474
  */
460
475
  LocalTrackSubscribed = "localTrackSubscribed",
461
476
  /** only emitted on local participant */
462
- ChatMessage = "chatMessage"
477
+ ChatMessage = "chatMessage",
478
+ /**
479
+ * Emitted when the participant's state changes to ACTIVE and is ready to send/receive data messages
480
+ */
481
+ Active = "active"
463
482
  }
464
483
  /** @internal */
465
484
  export declare enum EngineEvent {
@@ -491,7 +510,8 @@ export declare enum EngineEvent {
491
510
  LocalTrackSubscribed = "localTrackSubscribed",
492
511
  Offline = "offline",
493
512
  SignalRequestResponse = "signalRequestResponse",
494
- SignalConnected = "signalConnected"
513
+ SignalConnected = "signalConnected",
514
+ RoomMoved = "roomMoved"
495
515
  }
496
516
  export declare enum TrackEvent {
497
517
  Message = "message",
@@ -8,6 +8,7 @@ import type RemoteTrackPublication from '../track/RemoteTrackPublication';
8
8
  import { Track } from '../track/Track';
9
9
  import type { TrackPublication } from '../track/TrackPublication';
10
10
  import type { ChatMessage, LoggerOptions, TranscriptionSegment } from '../types';
11
+ import { Future } from '../utils';
11
12
  export declare enum ConnectionQuality {
12
13
  Excellent = "excellent",
13
14
  Good = "good",
@@ -47,11 +48,13 @@ export default class Participant extends Participant_base {
47
48
  protected audioContext?: AudioContext;
48
49
  protected log: StructuredLogger;
49
50
  protected loggerOptions?: LoggerOptions;
51
+ protected activeFuture?: Future<void>;
50
52
  protected get logContext(): {
51
53
  [x: string]: unknown;
52
54
  };
53
55
  get isEncrypted(): boolean;
54
56
  get isAgent(): boolean;
57
+ get isActive(): boolean;
55
58
  get kind(): ParticipantKind;
56
59
  /** participant attributes, similar to metadata, but as a key/value map */
57
60
  get attributes(): Readonly<Record<string, string>>;
@@ -67,6 +70,11 @@ export default class Participant extends Participant_base {
67
70
  * Finds the first track that matches the track's name.
68
71
  */
69
72
  getTrackPublicationByName(name: string): TrackPublication | undefined;
73
+ /**
74
+ * Waits until the participant is active and ready to receive data messages
75
+ * @returns a promise that resolves when the participant is active
76
+ */
77
+ waitUntilActive(): Promise<void>;
70
78
  get connectionQuality(): ConnectionQuality;
71
79
  get isCameraEnabled(): boolean;
72
80
  get isMicrophoneEnabled(): boolean;
@@ -91,6 +99,10 @@ export default class Participant extends Participant_base {
91
99
  setIsSpeaking(speaking: boolean): void;
92
100
  /** @internal */
93
101
  setConnectionQuality(q: ProtoQuality): void;
102
+ /**
103
+ * @internal
104
+ */
105
+ setDisconnected(): void;
94
106
  /**
95
107
  * @internal
96
108
  */
@@ -123,5 +135,6 @@ export type ParticipantEventCallbacks = {
123
135
  attributesChanged: (changedAttributes: Record<string, string>) => void;
124
136
  localTrackSubscribed: (trackPublication: LocalTrackPublication) => void;
125
137
  chatMessage: (msg: ChatMessage) => void;
138
+ active: () => void;
126
139
  };
127
140
  //# sourceMappingURL=Participant.d.ts.map
@@ -1,3 +1,3 @@
1
1
  export declare const version: string;
2
- export declare const protocolVersion = 15;
2
+ export declare const protocolVersion = 16;
3
3
  //# sourceMappingURL=version.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "livekit-client",
3
- "version": "2.11.4",
3
+ "version": "2.13.0",
4
4
  "description": "JavaScript/TypeScript client SDK for LiveKit",
5
5
  "main": "./dist/livekit-client.umd.js",
6
6
  "unpkg": "./dist/livekit-client.umd.js",
@@ -37,7 +37,7 @@
37
37
  "license": "Apache-2.0",
38
38
  "dependencies": {
39
39
  "@livekit/mutex": "1.1.1",
40
- "@livekit/protocol": "1.36.1",
40
+ "@livekit/protocol": "1.38.0",
41
41
  "events": "^3.3.0",
42
42
  "loglevel": "^1.9.2",
43
43
  "sdp-transform": "^2.15.0",
@@ -15,6 +15,7 @@ import {
15
15
  ReconnectResponse,
16
16
  RequestResponse,
17
17
  Room,
18
+ RoomMovedResponse,
18
19
  SessionDescription,
19
20
  SignalRequest,
20
21
  SignalResponse,
@@ -148,6 +149,8 @@ export class SignalClient {
148
149
 
149
150
  onLocalTrackSubscribed?: (trackSid: string) => void;
150
151
 
152
+ onRoomMoved?: (res: RoomMovedResponse) => void;
153
+
151
154
  connectOptions?: ConnectOpts;
152
155
 
153
156
  ws?: WebSocket;
@@ -774,6 +777,13 @@ export class SignalClient {
774
777
  if (this.onLocalTrackSubscribed) {
775
778
  this.onLocalTrackSubscribed(msg.value.trackSid);
776
779
  }
780
+ } else if (msg.case === 'roomMoved') {
781
+ if (this.onTokenRefresh) {
782
+ this.onTokenRefresh(msg.value.token);
783
+ }
784
+ if (this.onRoomMoved) {
785
+ this.onRoomMoved(msg.value);
786
+ }
777
787
  } else {
778
788
  this.log.debug('unsupported message', { ...this.logContext, msgCase: msg.case });
779
789
  }
@@ -152,7 +152,12 @@ export class E2EEManager
152
152
  }
153
153
  break;
154
154
  case 'ratchetKey':
155
- this.keyProvider.emit(KeyProviderEvent.KeyRatcheted, data.material, data.keyIndex);
155
+ this.keyProvider.emit(
156
+ KeyProviderEvent.KeyRatcheted,
157
+ data.ratchetResult,
158
+ data.participantIdentity,
159
+ data.keyIndex,
160
+ );
156
161
  break;
157
162
  default:
158
163
  break;
@@ -3,7 +3,7 @@ import type TypedEventEmitter from 'typed-emitter';
3
3
  import log from '../logger';
4
4
  import { KEY_PROVIDER_DEFAULTS } from './constants';
5
5
  import { type KeyProviderCallbacks, KeyProviderEvent } from './events';
6
- import type { KeyInfo, KeyProviderOptions } from './types';
6
+ import type { KeyInfo, KeyProviderOptions, RatchetResult } from './types';
7
7
  import { createKeyMaterialFromBuffer, createKeyMaterialFromString } from './utils';
8
8
 
9
9
  /**
@@ -39,13 +39,20 @@ export class BaseKeyProvider extends (EventEmitter as new () => TypedEventEmitte
39
39
  }
40
40
 
41
41
  /**
42
- * callback being invoked after a ratchet request has been performed on a participant
43
- * that surfaces the new key material.
44
- * @param material
42
+ * Callback being invoked after a key has been ratcheted.
43
+ * Can happen when:
44
+ * - A decryption failure occurs and the key is auto-ratcheted
45
+ * - A ratchet request is sent (see {@link ratchetKey()})
46
+ * @param ratchetResult Contains the ratcheted chain key (exportable to other participants) and the derived new key material.
47
+ * @param participantId
45
48
  * @param keyIndex
46
49
  */
47
- protected onKeyRatcheted = (material: CryptoKey, keyIndex?: number) => {
48
- log.debug('key ratcheted event received', { material, keyIndex });
50
+ protected onKeyRatcheted = (
51
+ ratchetResult: RatchetResult,
52
+ participantId?: string,
53
+ keyIndex?: number,
54
+ ) => {
55
+ log.debug('key ratcheted event received', { ratchetResult, participantId, keyIndex });
49
56
  };
50
57
 
51
58
  getKeys() {
@@ -1,26 +1,35 @@
1
1
  import type Participant from '../room/participant/Participant';
2
2
  import type { CryptorError } from './errors';
3
- import type { KeyInfo } from './types';
3
+ import type { KeyInfo, RatchetResult } from './types';
4
4
 
5
5
  export enum KeyProviderEvent {
6
6
  SetKey = 'setKey',
7
+ /** Event for requesting to ratchet the key used to encrypt the stream */
7
8
  RatchetRequest = 'ratchetRequest',
9
+ /** Emitted when a key is ratcheted. Could be after auto-ratcheting on decryption failure or
10
+ * following a `RatchetRequest`, will contain the ratcheted key material */
8
11
  KeyRatcheted = 'keyRatcheted',
9
12
  }
10
13
 
11
14
  export type KeyProviderCallbacks = {
12
15
  [KeyProviderEvent.SetKey]: (keyInfo: KeyInfo) => void;
13
16
  [KeyProviderEvent.RatchetRequest]: (participantIdentity?: string, keyIndex?: number) => void;
14
- [KeyProviderEvent.KeyRatcheted]: (material: CryptoKey, keyIndex?: number) => void;
17
+ [KeyProviderEvent.KeyRatcheted]: (
18
+ ratchetedResult: RatchetResult,
19
+ participantIdentity?: string,
20
+ keyIndex?: number,
21
+ ) => void;
15
22
  };
16
23
 
17
24
  export enum KeyHandlerEvent {
25
+ /** Emitted when a key has been ratcheted. Is emitted when any key has been ratcheted
26
+ * i.e. when the FrameCryptor tried to ratchet when decryption is failing */
18
27
  KeyRatcheted = 'keyRatcheted',
19
28
  }
20
29
 
21
30
  export type ParticipantKeyHandlerCallbacks = {
22
31
  [KeyHandlerEvent.KeyRatcheted]: (
23
- material: CryptoKey,
32
+ ratchetResult: RatchetResult,
24
33
  participantIdentity: string,
25
34
  keyIndex?: number,
26
35
  ) => void;
package/src/e2ee/types.ts CHANGED
@@ -82,7 +82,7 @@ export interface RatchetMessage extends BaseMessage {
82
82
  data: {
83
83
  participantIdentity: string;
84
84
  keyIndex?: number;
85
- material: CryptoKey;
85
+ ratchetResult: RatchetResult;
86
86
  };
87
87
  }
88
88
 
@@ -124,6 +124,13 @@ export type E2EEWorkerMessage =
124
124
 
125
125
  export type KeySet = { material: CryptoKey; encryptionKey: CryptoKey };
126
126
 
127
+ export type RatchetResult = {
128
+ // The ratchet chain key, which is used to derive the next key.
129
+ // Can be shared/exported to other participants.
130
+ chainKey: ArrayBuffer;
131
+ cryptoKey: CryptoKey;
132
+ };
133
+
127
134
  export type KeyProviderOptions = {
128
135
  sharedKey: boolean;
129
136
  ratchetSalt: string;
@@ -7,7 +7,7 @@ import type { VideoCodec } from '../../room/track/options';
7
7
  import { ENCRYPTION_ALGORITHM, IV_LENGTH, UNENCRYPTED_BYTES } from '../constants';
8
8
  import { CryptorError, CryptorErrorReason } from '../errors';
9
9
  import { type CryptorCallbacks, CryptorEvent } from '../events';
10
- import type { DecodeRatchetOptions, KeyProviderOptions, KeySet } from '../types';
10
+ import type { DecodeRatchetOptions, KeyProviderOptions, KeySet, RatchetResult } from '../types';
11
11
  import { deriveKeys, isVideoFrame, needsRbspUnescaping, parseRbsp, writeRbsp } from '../utils';
12
12
  import type { ParticipantKeyHandler } from './ParticipantKeyHandler';
13
13
  import { SifGuard } from './SifGuard';
@@ -477,12 +477,16 @@ export class FrameCryptor extends BaseFrameCryptor {
477
477
  );
478
478
 
479
479
  let ratchetedKeySet: KeySet | undefined;
480
+ let ratchetResult: RatchetResult | undefined;
480
481
  if ((initialMaterial ?? keySet) === this.keys.getKeySet(keyIndex)) {
481
482
  // only ratchet if the currently set key is still the same as the one used to decrypt this frame
482
483
  // if not, it might be that a different frame has already ratcheted and we try with that one first
483
- const newMaterial = await this.keys.ratchetKey(keyIndex, false);
484
+ ratchetResult = await this.keys.ratchetKey(keyIndex, false);
484
485
 
485
- ratchetedKeySet = await deriveKeys(newMaterial, this.keyProviderOptions.ratchetSalt);
486
+ ratchetedKeySet = await deriveKeys(
487
+ ratchetResult.cryptoKey,
488
+ this.keyProviderOptions.ratchetSalt,
489
+ );
486
490
  }
487
491
 
488
492
  const frame = await this.decryptFrame(encodedFrame, keyIndex, initialMaterial || keySet, {
@@ -493,7 +497,7 @@ export class FrameCryptor extends BaseFrameCryptor {
493
497
  // before updating the keys, make sure that the keySet used for this frame is still the same as the currently set key
494
498
  // if it's not, a new key might have been set already, which we don't want to override
495
499
  if ((initialMaterial ?? keySet) === this.keys.getKeySet(keyIndex)) {
496
- this.keys.setKeySet(ratchetedKeySet, keyIndex, true);
500
+ this.keys.setKeySet(ratchetedKeySet, keyIndex, ratchetResult);
497
501
  // decryption was successful, set the new key index to reflect the ratcheted key set
498
502
  this.keys.setCurrentKeyIndex(keyIndex);
499
503
  }
@@ -1,7 +1,7 @@
1
- import { describe, expect, it, vitest } from 'vitest';
1
+ import { describe, expect, it, test, vitest } from 'vitest';
2
2
  import { ENCRYPTION_ALGORITHM, KEY_PROVIDER_DEFAULTS } from '../constants';
3
3
  import { KeyHandlerEvent } from '../events';
4
- import { createKeyMaterialFromString } from '../utils';
4
+ import { createKeyMaterialFromString, importKey } from '../utils';
5
5
  import { ParticipantKeyHandler } from './ParticipantKeyHandler';
6
6
 
7
7
  describe('ParticipantKeyHandler', () => {
@@ -239,11 +239,18 @@ describe('ParticipantKeyHandler', () => {
239
239
 
240
240
  await keyHandler.setKey(material);
241
241
 
242
- await keyHandler.ratchetKey();
242
+ const ratchetResult = await keyHandler.ratchetKey();
243
243
 
244
244
  const newMaterial = keyHandler.getKeySet()?.material;
245
245
 
246
- expect(keyRatched).toHaveBeenCalledWith(newMaterial, participantIdentity, 0);
246
+ expect(keyRatched).toHaveBeenCalledWith(
247
+ {
248
+ chainKey: ratchetResult.chainKey,
249
+ cryptoKey: newMaterial,
250
+ },
251
+ participantIdentity,
252
+ 0,
253
+ );
247
254
  });
248
255
 
249
256
  it('ratchets keys predictably', async () => {
@@ -283,4 +290,97 @@ describe('ParticipantKeyHandler', () => {
283
290
  expect(ciphertexts).matchSnapshot('ciphertexts');
284
291
  });
285
292
  });
293
+
294
+ describe(`E2EE Ratcheting`, () => {
295
+ test('Should be possible to share ratcheted material to remote participant', async () => {
296
+ const senderKeyHandler = new ParticipantKeyHandler('test-sender', KEY_PROVIDER_DEFAULTS);
297
+ // Initial key
298
+ const initialMaterial = new Uint8Array(32);
299
+ crypto.getRandomValues(initialMaterial);
300
+ const rootMaterial = await importKey(initialMaterial, 'HKDF', 'derive');
301
+ await senderKeyHandler.setKeyFromMaterial(rootMaterial, 0);
302
+
303
+ const iv = new Uint8Array(12);
304
+ crypto.getRandomValues(iv);
305
+
306
+ const firstMessagePreRatchet = new TextEncoder().encode(
307
+ 'Hello world, this is the first message',
308
+ );
309
+ const firstCipherText = await encrypt(senderKeyHandler, 0, iv, firstMessagePreRatchet);
310
+
311
+ let ratchetBufferResolve: (key: ArrayBuffer) => void;
312
+ const expectEmitted = new Promise<ArrayBuffer>(async (resolve) => {
313
+ ratchetBufferResolve = resolve;
314
+ });
315
+
316
+ senderKeyHandler.on(KeyHandlerEvent.KeyRatcheted, (material, identity, keyIndex) => {
317
+ expect(identity).toEqual('test-sender');
318
+ expect(keyIndex).toEqual(0);
319
+ ratchetBufferResolve(material.chainKey);
320
+ });
321
+
322
+ const currentKeyIndex = senderKeyHandler.getCurrentKeyIndex();
323
+ const ratchetResult = await senderKeyHandler.ratchetKey(currentKeyIndex, true);
324
+
325
+ // Notice that ratchetedKeySet is not exportable, so we cannot share it out-of-band.
326
+ // This is a limitation of webcrypto for KDFs keys, they cannot be exported.
327
+ expect(ratchetResult.cryptoKey.extractable).toBe(false);
328
+
329
+ const ratchetedMaterial = await expectEmitted;
330
+
331
+ // The ratcheted material can be sent out-of-band to new participants. And they
332
+ // should be able to generate the same keyMaterial
333
+
334
+ const generatedMaterial = await importKey(ratchetedMaterial, 'HKDF', 'derive');
335
+ const receiverKeyHandler = new ParticipantKeyHandler('test-receiver', KEY_PROVIDER_DEFAULTS);
336
+ await receiverKeyHandler.setKeyFromMaterial(generatedMaterial, 0);
337
+
338
+ // Now sender should be able to encrypt to recipient
339
+
340
+ const plainText = new TextEncoder().encode('Hello world, this is a test message');
341
+
342
+ const cipherText = await encrypt(senderKeyHandler, 0, iv, plainText);
343
+
344
+ const clearTextBuffer = await decrypt(receiverKeyHandler, 0, iv, cipherText);
345
+
346
+ const clearText = new Uint8Array(clearTextBuffer);
347
+ expect(clearText).toEqual(plainText);
348
+
349
+ // The receiver should not be able to decrypt the first message
350
+ const decryptPromise = decrypt(receiverKeyHandler, 0, iv, firstCipherText);
351
+ await expect(decryptPromise).rejects.toThrowError();
352
+ });
353
+
354
+ async function encrypt(
355
+ participantKeyHandler: ParticipantKeyHandler,
356
+ keyIndex: number,
357
+ iv: Uint8Array,
358
+ data: Uint8Array,
359
+ ): Promise<ArrayBuffer> {
360
+ return crypto.subtle.encrypt(
361
+ {
362
+ name: ENCRYPTION_ALGORITHM,
363
+ iv,
364
+ },
365
+ participantKeyHandler.getKeySet(keyIndex)!.encryptionKey,
366
+ data,
367
+ );
368
+ }
369
+
370
+ async function decrypt(
371
+ participantKeyHandler: ParticipantKeyHandler,
372
+ keyIndex: number,
373
+ iv: Uint8Array,
374
+ cipherText: ArrayBuffer,
375
+ ): Promise<ArrayBuffer> {
376
+ return crypto.subtle.decrypt(
377
+ {
378
+ name: ENCRYPTION_ALGORITHM,
379
+ iv,
380
+ },
381
+ participantKeyHandler.getKeySet(keyIndex)!.encryptionKey,
382
+ cipherText,
383
+ );
384
+ }
385
+ });
286
386
  });
@@ -2,7 +2,7 @@ import { EventEmitter } from 'events';
2
2
  import type TypedEventEmitter from 'typed-emitter';
3
3
  import { workerLogger } from '../../logger';
4
4
  import { KeyHandlerEvent, type ParticipantKeyHandlerCallbacks } from '../events';
5
- import type { KeyProviderOptions, KeySet } from '../types';
5
+ import type { KeyProviderOptions, KeySet, RatchetResult } from '../types';
6
6
  import { deriveKeys, importKey, ratchet } from '../utils';
7
7
 
8
8
  // TODO ParticipantKeyHandlers currently don't get destroyed on participant disconnect
@@ -25,7 +25,7 @@ export class ParticipantKeyHandler extends (EventEmitter as new () => TypedEvent
25
25
 
26
26
  private keyProviderOptions: KeyProviderOptions;
27
27
 
28
- private ratchetPromiseMap: Map<number, Promise<CryptoKey>>;
28
+ private ratchetPromiseMap: Map<number, Promise<RatchetResult>>;
29
29
 
30
30
  private participantIdentity: string;
31
31
 
@@ -110,14 +110,14 @@ export class ParticipantKeyHandler extends (EventEmitter as new () => TypedEvent
110
110
  * @param keyIndex
111
111
  * @param setKey
112
112
  */
113
- ratchetKey(keyIndex?: number, setKey = true): Promise<CryptoKey> {
113
+ ratchetKey(keyIndex?: number, setKey = true): Promise<RatchetResult> {
114
114
  const currentKeyIndex = keyIndex ?? this.getCurrentKeyIndex();
115
115
 
116
116
  const existingPromise = this.ratchetPromiseMap.get(currentKeyIndex);
117
117
  if (typeof existingPromise !== 'undefined') {
118
118
  return existingPromise;
119
119
  }
120
- const ratchetPromise = new Promise<CryptoKey>(async (resolve, reject) => {
120
+ const ratchetPromise = new Promise<RatchetResult>(async (resolve, reject) => {
121
121
  try {
122
122
  const keySet = this.getKeySet(currentKeyIndex);
123
123
  if (!keySet) {
@@ -126,22 +126,17 @@ export class ParticipantKeyHandler extends (EventEmitter as new () => TypedEvent
126
126
  );
127
127
  }
128
128
  const currentMaterial = keySet.material;
129
- const newMaterial = await importKey(
130
- await ratchet(currentMaterial, this.keyProviderOptions.ratchetSalt),
131
- currentMaterial.algorithm.name,
132
- 'derive',
133
- );
134
-
129
+ const chainKey = await ratchet(currentMaterial, this.keyProviderOptions.ratchetSalt);
130
+ const newMaterial = await importKey(chainKey, currentMaterial.algorithm.name, 'derive');
131
+ const ratchetResult: RatchetResult = {
132
+ chainKey,
133
+ cryptoKey: newMaterial,
134
+ };
135
135
  if (setKey) {
136
- await this.setKeyFromMaterial(newMaterial, currentKeyIndex, true);
137
- this.emit(
138
- KeyHandlerEvent.KeyRatcheted,
139
- newMaterial,
140
- this.participantIdentity,
141
- currentKeyIndex,
142
- );
136
+ // Set the new key and emit a ratchet event with the ratcheted chain key
137
+ await this.setKeyFromMaterial(newMaterial, currentKeyIndex, ratchetResult);
143
138
  }
144
- resolve(newMaterial);
139
+ resolve(ratchetResult);
145
140
  } catch (e) {
146
141
  reject(e);
147
142
  } finally {
@@ -169,7 +164,11 @@ export class ParticipantKeyHandler extends (EventEmitter as new () => TypedEvent
169
164
  * together with the material
170
165
  * also updates the currentKeyIndex
171
166
  */
172
- async setKeyFromMaterial(material: CryptoKey, keyIndex: number, emitRatchetEvent = false) {
167
+ async setKeyFromMaterial(
168
+ material: CryptoKey,
169
+ keyIndex: number,
170
+ ratchetedResult: RatchetResult | null = null,
171
+ ) {
173
172
  const keySet = await deriveKeys(material, this.keyProviderOptions.ratchetSalt);
174
173
  const newIndex = keyIndex >= 0 ? keyIndex % this.cryptoKeyRing.length : this.currentKeyIndex;
175
174
  workerLogger.debug(`setting new key with index ${keyIndex}`, {
@@ -177,15 +176,15 @@ export class ParticipantKeyHandler extends (EventEmitter as new () => TypedEvent
177
176
  algorithm: material.algorithm,
178
177
  ratchetSalt: this.keyProviderOptions.ratchetSalt,
179
178
  });
180
- this.setKeySet(keySet, newIndex, emitRatchetEvent);
179
+ this.setKeySet(keySet, newIndex, ratchetedResult);
181
180
  if (newIndex >= 0) this.currentKeyIndex = newIndex;
182
181
  }
183
182
 
184
- setKeySet(keySet: KeySet, keyIndex: number, emitRatchetEvent = false) {
183
+ setKeySet(keySet: KeySet, keyIndex: number, ratchetedResult: RatchetResult | null = null) {
185
184
  this.cryptoKeyRing[keyIndex % this.cryptoKeyRing.length] = keySet;
186
185
 
187
- if (emitRatchetEvent) {
188
- this.emit(KeyHandlerEvent.KeyRatcheted, keySet.material, this.participantIdentity, keyIndex);
186
+ if (ratchetedResult) {
187
+ this.emit(KeyHandlerEvent.KeyRatcheted, ratchetedResult, this.participantIdentity, keyIndex);
189
188
  }
190
189
  }
191
190