diff --git a/.changeset/e2ee-send-local-state.md b/.changeset/e2ee-send-local-state.md new file mode 100644 index 0000000000..4da29e73ee --- /dev/null +++ b/.changeset/e2ee-send-local-state.md @@ -0,0 +1,5 @@ +--- +'livekit-client': patch +--- + +fix(e2ee): track the local encryption state separately from remote participants diff --git a/src/e2ee/E2eeManager.test.ts b/src/e2ee/E2eeManager.test.ts index 729e2825b0..3d0e7c3b05 100644 --- a/src/e2ee/E2eeManager.test.ts +++ b/src/e2ee/E2eeManager.test.ts @@ -4,8 +4,10 @@ import { Room } from '../room/Room'; import { ParticipantEvent } from '../room/events'; import { Track } from '../room/track/Track'; import type { VideoCodec } from '../room/track/options'; +import { subscribeToEvents } from '../utils/subscribeToEvents'; import { E2EEManager } from './E2eeManager'; import { BaseKeyProvider } from './KeyProvider'; +import { type E2EEManagerCallbacks, EncryptionEvent } from './events'; /** * Install just enough of the DOM to let isE2EESupported() return true so @@ -237,3 +239,70 @@ describe('E2EEManager sender codec seeding', () => { expect(encode.data.codec).toBeUndefined(); }); }); + +describe('E2EEManager local encryption state', () => { + it('posts the local state from setLocalParticipantCryptorEnabled', () => { + // Create a manager with a worker that records its messages + const { room, worker, manager } = makeManager(); + manager.setup(room); + + // Enable encryption for the local participant + manager.setLocalParticipantCryptorEnabled(true); + + // The worker got an enable message that is marked as local + expect(worker.postMessage).toHaveBeenCalledWith({ + kind: 'enable', + data: { enabled: true, participantIdentity: '', local: true }, + }); + + manager.dispose(); + }); + + it('posts the local state from the deprecated setParticipantCryptorEnabled', () => { + // Create a manager with a worker that records its messages + const { room, worker, manager } = makeManager(); + manager.setup(room); + + // Enable encryption with the deprecated method, which ignores the identity + manager.setParticipantCryptorEnabled(true, 'someone-else'); /* <-- DIFFERENCE */ + + // The worker got an enable message that is marked as local + expect(worker.postMessage).toHaveBeenCalledWith({ + kind: 'enable', + data: { enabled: true, participantIdentity: '', local: true }, + }); + + manager.dispose(); + }); + + it('emits the local status once when the worker confirms the same state twice', async () => { + // Create a manager with a worker that records its messages + const { room, worker, manager } = makeManager(); + manager.setup(room); + const managerEvents = subscribeToEvents(manager, [ + EncryptionEvent.ParticipantEncryptionStatusChanged, + ]); + const workerReplies = worker.onmessage as (ev: { data: unknown }) => void; + + // The worker confirms that local encryption is enabled + workerReplies({ + data: { kind: 'enable', data: { enabled: true, participantIdentity: '', local: true } }, + }); + + // The manager reports the new status + const enabled = await managerEvents.waitFor(EncryptionEvent.ParticipantEncryptionStatusChanged); + expect(enabled).toStrictEqual(true); + + // The worker confirms the same state again, as it does after the room connects + workerReplies({ + data: { kind: 'enable', data: { enabled: true, participantIdentity: 'me', local: true } }, + }); + + // The manager does not report the status a second time + expect( + managerEvents.areThereBufferedEvents(EncryptionEvent.ParticipantEncryptionStatusChanged), + ).toStrictEqual(false); + + manager.dispose(); + }); +}); diff --git a/src/e2ee/E2eeManager.ts b/src/e2ee/E2eeManager.ts index afe3231cac..72cc1b3a96 100644 --- a/src/e2ee/E2eeManager.ts +++ b/src/e2ee/E2eeManager.ts @@ -54,8 +54,17 @@ export interface BaseE2EEManager { setupEngine(engine: RTCEngine): void; isEnabled: boolean; isDataChannelEncryptionEnabled: boolean; - /** @internal */ + /** + * @internal + * @deprecated use `setLocalParticipantCryptorEnabled` + */ setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string): void; + /** + * Sets whether the local participant encrypts. Optional so that existing managers still + * satisfy the interface: without it, `setParticipantCryptorEnabled` is called. + * @internal + */ + setLocalParticipantCryptorEnabled?(enabled: boolean): void; /** @internal */ setSifTrailer(trailer: NonSharedUint8Array): void; encryptData(data: NonSharedUint8Array): Promise; @@ -232,12 +241,22 @@ export class E2EEManager this.removeAllListeners(); } + /** + * Sets whether the local participant encrypts. + * @internal + */ + setLocalParticipantCryptorEnabled(enabled: boolean) { + this.log.debug(`set e2ee to ${enabled} for the local participant`); + this.postEnable(enabled, this.room?.localParticipant.identity ?? '', true); + } + /** * @internal + * @deprecated use `setLocalParticipantCryptorEnabled`. The identity is ignored: this always + * sets the state of the local participant. */ - setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string) { - this.log.debug(`set e2ee to ${enabled} for participant ${participantIdentity}`); - this.postEnable(enabled, participantIdentity); + setParticipantCryptorEnabled(enabled: boolean, _participantIdentity: string) { + this.setLocalParticipantCryptorEnabled(enabled); } /** @@ -288,16 +307,15 @@ export class E2EEManager this.postKey(keyInfo, false); }); } - if ( - this.encryptionEnabled !== data.enabled && - data.participantIdentity === this.room?.localParticipant.identity - ) { - this.emit( - EncryptionEvent.ParticipantEncryptionStatusChanged, - data.enabled, - this.room!.localParticipant, - ); - this.encryptionEnabled = data.enabled; + if (data.local) { + if (this.encryptionEnabled !== data.enabled) { + this.emit( + EncryptionEvent.ParticipantEncryptionStatusChanged, + data.enabled, + this.room!.localParticipant, + ); + this.encryptionEnabled = data.enabled; + } } else if (data.participantIdentity) { const participant = this.room?.getParticipantByIdentity(data.participantIdentity); if (!participant) { @@ -413,10 +431,7 @@ export class E2EEManager keyProvider.getKeys().forEach((keyInfo) => { this.postKey(keyInfo, latestKeyIndex === (keyInfo.keyIndex ?? 0)); }); - this.setParticipantCryptorEnabled( - this.room.localParticipant.isE2EEEnabled, - this.room.localParticipant.identity, - ); + this.setLocalParticipantCryptorEnabled(this.room.localParticipant.isE2EEEnabled); }); room.localParticipant.on(ParticipantEvent.LocalSenderCreated, async (sender, track) => { @@ -435,6 +450,7 @@ export class E2EEManager codec: mimeTypeToVideoCodecString(publication.trackInfo!.codecs[0].mimeType), participantIdentity: this.room!.localParticipant.identity, hasPacketTrailer: false, + operation: 'encode', }, }; @@ -532,13 +548,14 @@ export class E2EEManager this.worker.postMessage(msg); } - private postEnable(enabled: boolean, participantIdentity: string) { + private postEnable(enabled: boolean, participantIdentity: string, local = false) { if (this.worker) { const enableMsg: EnableMessage = { kind: 'enable', data: { enabled, participantIdentity, + local, }, }; this.worker.postMessage(enableMsg); @@ -593,10 +610,7 @@ export class E2EEManager }); return; } - this.setParticipantCryptorEnabled( - pub.trackInfo.encryption !== Encryption_Type.NONE, - participantIdentity, - ); + this.postEnable(pub.trackInfo.encryption !== Encryption_Type.NONE, participantIdentity); } private setupE2EEReceiver(track: RemoteTrack, remoteId: string, trackInfo?: TrackInfo) { diff --git a/src/e2ee/extension.ts b/src/e2ee/extension.ts index 05140ce032..78802d2be8 100644 --- a/src/e2ee/extension.ts +++ b/src/e2ee/extension.ts @@ -93,7 +93,9 @@ export const e2ee = /* @__PURE__ */ defineExtension( if (room.isE2EEEnabled !== enabled) { await room.localParticipant.setE2EEEnabled(enabled); - if (room.localParticipant.identity !== '') { + if (manager.setLocalParticipantCryptorEnabled) { + manager.setLocalParticipantCryptorEnabled(enabled); + } else if (room.localParticipant.identity !== '') { manager.setParticipantCryptorEnabled(enabled, room.localParticipant.identity); } } diff --git a/src/e2ee/types.ts b/src/e2ee/types.ts index 756af8395f..307f9fb717 100644 --- a/src/e2ee/types.ts +++ b/src/e2ee/types.ts @@ -89,6 +89,8 @@ export interface UpdateCodecMessage extends BaseMessage { */ previousTrackId?: string; hasPacketTrailer: boolean; + /** which cryptor the update is for, a receiver's ('decode') when omitted */ + operation?: 'encode' | 'decode'; }; } @@ -123,6 +125,11 @@ export interface EnableMessage extends BaseMessage { data: { participantIdentity: string; enabled: boolean; + /** + * Set when the app enables or disables encryption for the local participant. Only such a + * message changes whether local media is encrypted. + */ + local?: boolean; }; } @@ -244,6 +251,7 @@ export type KeyInfo = { export type E2EEManagerOptions = { keyProvider: BaseKeyProvider; + /** The worker holds the encryption state of one room. Do not share it between rooms. */ worker: Worker; }; diff --git a/src/e2ee/worker/FrameCryptor.race.test.ts b/src/e2ee/worker/FrameCryptor.race.test.ts index a07424331a..ea1a872c65 100644 --- a/src/e2ee/worker/FrameCryptor.race.test.ts +++ b/src/e2ee/worker/FrameCryptor.race.test.ts @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest'; import { KEY_PROVIDER_DEFAULTS } from '../constants'; import type { KeyProviderOptions } from '../types'; import { createKeyMaterialFromString } from '../utils'; -import { FrameCryptor, encryptionEnabledMap } from './FrameCryptor'; +import { FrameCryptor, encryptionEnabledMap, setLocalEncryptionEnabled } from './FrameCryptor'; import { ParticipantKeyHandler } from './ParticipantKeyHandler'; function mockRTCEncodedVideoFrame(data: Uint8Array): RTCEncodedVideoFrame { @@ -47,6 +47,7 @@ function createCryptor( const keyProviderOptions = { ...KEY_PROVIDER_DEFAULTS, ...partialKeyProviderOptions }; const keys = new ParticipantKeyHandler(participantIdentity, keyProviderOptions); encryptionEnabledMap.set(participantIdentity, true); + setLocalEncryptionEnabled(true); const cryptor = new FrameCryptor({ participantIdentity, diff --git a/src/e2ee/worker/FrameCryptor.test.ts b/src/e2ee/worker/FrameCryptor.test.ts index f4e415c495..4b7e32bd66 100644 --- a/src/e2ee/worker/FrameCryptor.test.ts +++ b/src/e2ee/worker/FrameCryptor.test.ts @@ -4,11 +4,18 @@ import type { FrameMetadataPublishOptions } from '../../frameMetadata/types'; import { workerLogger } from '../../logger'; import type { VideoCodec } from '../../room/track/options'; import type { NonSharedUint8Array } from '../../type-polyfills/non-shared-typed-arrays'; +import { subscribeToEvents } from '../../utils/subscribeToEvents'; import { IV_LENGTH, KEY_PROVIDER_DEFAULTS } from '../constants'; -import { CryptorEvent } from '../events'; +import { CryptorErrorReason } from '../errors'; +import { type CryptorCallbacks, CryptorEvent } from '../events'; import type { KeyProviderOptions } from '../types'; import { createKeyMaterialFromString } from '../utils'; -import { FrameCryptor, encryptionEnabledMap, isFrameServerInjected } from './FrameCryptor'; +import { + FrameCryptor, + encryptionEnabledMap, + isFrameServerInjected, + setLocalEncryptionEnabled, +} from './FrameCryptor'; import { ParticipantKeyHandler } from './ParticipantKeyHandler'; import { parseH264IdrPpsId } from './naluUtils'; @@ -101,6 +108,7 @@ function prepareParticipantTest( const keys = new ParticipantKeyHandler(participantIdentity, keyProviderOptions); encryptionEnabledMap.set(participantIdentity, true); + setLocalEncryptionEnabled(true); const cryptor = new FrameCryptor({ participantIdentity, @@ -145,6 +153,45 @@ describe('FrameCryptor', () => { describe('encode', () => { afterEach(() => { encryptionEnabledMap.clear(); + setLocalEncryptionEnabled(undefined); + }); + + it('drops the frame and emits an error while the local state is unknown', async () => { + // Create an encoder for the local participant + const { cryptor, input, output } = prepareParticipantTestEncoder(participantIdentity, {}); + const cryptorEvents = subscribeToEvents(cryptor, [CryptorEvent.Error]); + + // The app did not say yet whether the local participant encrypts + setLocalEncryptionEnabled(undefined); + + // Send a frame + input.write(mockRTCEncodedVideoFrame(new Uint8Array(16))); + + // The cryptor reports an error + const error = await cryptorEvents.waitFor(CryptorEvent.Error); + expect(error.reason).toStrictEqual(CryptorErrorReason.InternalError); + + // The frame was not sent + expect(output.chunks).toStrictEqual([]); + }); + + it('encrypts after the participant was unset', async () => { + // Create an encoder for the local participant, with a key + const { cryptor, keys, input, output } = prepareParticipantTestEncoder( + participantIdentity, + {}, + ); + await keys.setKey(await createKeyMaterialFromString('key1'), 1); + + // Unset the participant of the cryptor + cryptor.unsetParticipant(); + + // Send a frame of 16 bytes + input.write(mockRTCEncodedVideoFrame(new Uint8Array(16))); + await vitest.waitFor(() => expect(output.chunks).toHaveLength(1)); + + // The sent frame is longer, because encryption appends the IV and the trailer + expect(output.chunks[0].data.byteLength).toBeGreaterThan(16); }); it('passthrough if participant encryption disabled', async () => { @@ -152,8 +199,8 @@ describe('FrameCryptor', () => { try { const { input, output } = prepareParticipantTestEncoder(participantIdentity, {}); - // disable encryption for participant - encryptionEnabledMap.set(participantIdentity, false); + // disable encryption for the local participant + setLocalEncryptionEnabled(false); const frame = mockRTCEncodedVideoFrame(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])); diff --git a/src/e2ee/worker/FrameCryptor.ts b/src/e2ee/worker/FrameCryptor.ts index c3ae04a7b8..89a0b4fde8 100644 --- a/src/e2ee/worker/FrameCryptor.ts +++ b/src/e2ee/worker/FrameCryptor.ts @@ -40,6 +40,13 @@ const MAX_PPS_ID_ENCRYPT_ATTEMPTS = 16; export const encryptionEnabledMap: Map = new Map(); +let localEncryptionEnabled: boolean | undefined; + +/** Sets whether local media is encrypted. */ +export function setLocalEncryptionEnabled(enabled: boolean | undefined) { + localEncryptionEnabled = enabled; +} + export interface FrameCryptorConstructor { new (opts?: unknown): BaseFrameCryptor; } @@ -472,7 +479,17 @@ export class FrameCryptor extends BaseFrameCryptor { return controller.enqueue(encodedFrame); } - if (!this.isEnabled()) { + if (localEncryptionEnabled === undefined) { + this.emitThrottledError( + new CryptorError( + `encryption state unknown for track ${this.trackId}, dropping frame`, + CryptorErrorReason.InternalError, + this.participantIdentity, + ), + ); + return; + } + if (!localEncryptionEnabled) { this.appendFrameMetadata(encodedFrame); return controller.enqueue(encodedFrame); } diff --git a/src/e2ee/worker/e2ee.worker.test.ts b/src/e2ee/worker/e2ee.worker.test.ts new file mode 100644 index 0000000000..f2ddbbb078 --- /dev/null +++ b/src/e2ee/worker/e2ee.worker.test.ts @@ -0,0 +1,91 @@ +import { afterEach, beforeAll, describe, expect, it } from 'vitest'; +import { KEY_PROVIDER_DEFAULTS } from '../constants'; +import type { E2EEWorkerMessage } from '../types'; +import { createKeyMaterialFromString } from '../utils'; +import { setLocalEncryptionEnabled } from './FrameCryptor'; + +describe('e2ee worker', () => { + // Sends a message to the worker, like `worker.postMessage` on the main thread + const post = (msg: E2EEWorkerMessage) => + (globalThis.onmessage as (ev: { data: E2EEWorkerMessage }) => void)({ data: msg }); + + beforeAll(async () => { + // The test runs outside of a worker scope: give the worker a place to post its replies + globalThis.postMessage = () => {}; + await import('./e2ee.worker'); + }); + + afterEach(() => { + setLocalEncryptionEnabled(undefined); + }); + + it('keeps sender and receiver cryptors with the same track id apart', async () => { + // Set up the worker with encryption enabled and a key for the local participant + post({ + kind: 'init', + data: { keyProviderOptions: { ...KEY_PROVIDER_DEFAULTS, sharedKey: false }, loglevel: 4 }, + }); + post({ kind: 'enable', data: { enabled: true, participantIdentity: 'me', local: true } }); + post({ + kind: 'setKey', + data: { + participantIdentity: 'me', + isPublisher: true, + key: await createKeyMaterialFromString('key'), + keyIndex: 0, + updateCurrentKeyIndex: true, + }, + }); + + // Set up a sender for the local track "mic" + let senderInput!: ReadableStreamDefaultController; + const sentFrames: RTCEncodedVideoFrame[] = []; + post({ + kind: 'encode', + data: { + participantIdentity: 'me', + trackId: 'mic', + hasPacketTrailer: false, + readableStream: new ReadableStream({ + start: (controller) => { + senderInput = controller; + }, + }), + writableStream: new WritableStream({ + write: (frame) => { + sentFrames.push(frame); + }, + }), + }, + }); + + // Set up a receiver for a remote track that is also called "mic", then remove it + post({ + kind: 'decode', + data: { + participantIdentity: 'remote', + trackId: 'mic', + hasPacketTrailer: false, + readableStream: new ReadableStream(), + writableStream: new WritableStream(), + }, + }); + post({ kind: 'removeTransform', data: { participantIdentity: 'remote', trackId: 'mic' } }); + + // The worker handles its messages one after the other: wait until it is through + await new Promise((resolve) => setTimeout(resolve, 20)); + + // Send a frame of 16 bytes on the local track + senderInput.enqueue({ + data: new Uint8Array(16).buffer, + timestamp: 1, + type: 'key', + getMetadata: () => ({}), + } as RTCEncodedVideoFrame); + await new Promise((resolve) => setTimeout(resolve, 20)); + + // The sent frame is longer, because encryption appends the IV and the trailer + expect(sentFrames).toHaveLength(1); + expect(sentFrames[0].data.byteLength).toBeGreaterThan(16); + }); +}); diff --git a/src/e2ee/worker/e2ee.worker.ts b/src/e2ee/worker/e2ee.worker.ts index a0ca1c5275..55f7c355af 100644 --- a/src/e2ee/worker/e2ee.worker.ts +++ b/src/e2ee/worker/e2ee.worker.ts @@ -19,10 +19,11 @@ import type { } from '../types'; import { DataCryptor } from './DataCryptor'; import { ErrorRateLimiter } from './ErrorRateLimiter'; -import { FrameCryptor, encryptionEnabledMap } from './FrameCryptor'; +import { FrameCryptor, encryptionEnabledMap, setLocalEncryptionEnabled } from './FrameCryptor'; import { ParticipantKeyHandler } from './ParticipantKeyHandler'; -const participantCryptors: FrameCryptor[] = []; +// senders and receivers have separate cryptors +const cryptorsByOperation: Record<'encode' | 'decode', FrameCryptor[]> = { encode: [], decode: [] }; const participantKeys: Map = new Map(); let sharedKeyHandler: ParticipantKeyHandler | undefined; let messageQueue = new AsyncQueue(); @@ -73,7 +74,11 @@ onmessage = (ev) => { workerLogger.setLevel(data.level); break; case 'enable': - setEncryptionEnabled(data.enabled, data.participantIdentity); + if (data.local) { + setLocalEncryptionEnabled(data.enabled); + } else { + setEncryptionEnabled(data.enabled, data.participantIdentity); + } workerLogger.info( `updated e2ee enabled status for ${data.participantIdentity} to ${data.enabled}`, ); @@ -81,7 +86,7 @@ onmessage = (ev) => { postMessage(ev.data); break; case 'decode': - let cryptor = getTrackCryptor(data.participantIdentity, data.trackId); + let cryptor = getTrackCryptor(kind, data.participantIdentity, data.trackId); cryptor.setHasFrameMetadata(data.hasPacketTrailer); cryptor.setupTransform( kind, @@ -92,7 +97,7 @@ onmessage = (ev) => { ); break; case 'encode': - let pubCryptor = getTrackCryptor(data.participantIdentity, data.trackId); + let pubCryptor = getTrackCryptor(kind, data.participantIdentity, data.trackId); pubCryptor.setHasFrameMetadata(data.hasPacketTrailer); pubCryptor.setupTransform( kind, @@ -185,6 +190,7 @@ onmessage = (ev) => { break; case 'updateCodec': const trackCryptor = getTrackCryptor( + data.operation ?? 'decode', data.participantIdentity, data.trackId, data.previousTrackId, @@ -214,10 +220,8 @@ onmessage = (ev) => { case 'setRTPMap': // this is only used for the local participant rtpMap = data.map; - participantCryptors.forEach((cr) => { - if (cr.getParticipantIdentity() === data.participantIdentity) { - cr.setRtpMap(data.map); - } + cryptorsByOperation.encode.forEach((cr) => { + cr.setRtpMap(data.map); }); break; case 'ratchetRequest': @@ -248,7 +252,13 @@ async function handleRatchetRequest(data: RatchetRequestMessage['data']) { } } -function getTrackCryptor(participantIdentity: string, trackId: string, previousTrackId?: string) { +function getTrackCryptor( + operation: 'encode' | 'decode', + participantIdentity: string, + trackId: string, + previousTrackId?: string, +) { + const participantCryptors = cryptorsByOperation[operation]; let cryptors = participantCryptors.filter((c) => c.getTrackId() === trackId); if (cryptors.length === 0 && previousTrackId !== undefined && previousTrackId !== trackId) { @@ -324,7 +334,7 @@ function getSharedKeyHandler() { } function unsetCryptorParticipant(trackId: string, participantIdentity: string) { - const cryptors = participantCryptors.filter( + const cryptors = cryptorsByOperation.decode.filter( (c) => c.getParticipantIdentity() === participantIdentity && c.getTrackId() === trackId, ); if (cryptors.length > 1) { @@ -384,7 +394,7 @@ function emitRatchetedKeys( function handleSifTrailer(trailer: NonSharedUint8Array) { sifTrailer = trailer; - participantCryptors.forEach((c) => { + cryptorsByOperation.decode.forEach((c) => { c.setSifTrailer(trailer); }); } @@ -399,7 +409,7 @@ if (self.RTCTransformEvent) { const options = transformer.options as ScriptTransformOptions; const { kind, participantIdentity, trackId, codec, hasPacketTrailer } = options; messageQueue.run(async () => { - const cryptor = getTrackCryptor(participantIdentity, trackId); + const cryptor = getTrackCryptor(kind, participantIdentity, trackId); cryptor.setHasFrameMetadata(hasPacketTrailer); workerLogger.debug('onrtctransform setup', { participantIdentity, trackId, codec }); cryptor.setupTransform(