Repository navigation
Follow key provider key index on media frame cryptors - #1042
tangowhisky-dev wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 57a1cad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@davidliu can you please look into this. Merging it would remove issues associated with E2EE key rotations. |
|
@changt , could you please help review this PR ? |
|
Thanks, good catch. The initial stamp in
I added three tests to Could a maintainer also approve the CI workflows for this fork PR? No checks have run on it yet. |
|
@changt can you please relook into proposed changes. This one is becoming a blocker for my app as it adversly affects the UX. |
There was a problem hiding this comment.
This is a pretty big breaking change that will fail silently for upgraders. At the very minimum, it needs a minor version changeset, ideally with an example code upgrade path.
However, I think the fix for the actual bug, already-published senders never getting re-stamped, is a lot simpler than this, and doesn't need the index model to change.
Today latestSetIndex is already keyed by participant and E2EEManager only ever reads the local identity's entry, so setKey(key, localIdentity, n) moves the local sender and a remote participant's install can't. Swift had a single currentKeyIndex that any setKey could redirect, and carrying that behavior over here doesn't make sense here. What's missing is only the push: when the local index is written, tell the manager so it can update live senders. Something like:
- Keep
latestSetIndexas is. - Have
setKeynotify observers with(participantId, index)after it writes the map entry. - Have
setSharedKeynotify with(null, index)after a successful install. - In
E2EEManager, re-stamp the local sender cryptors only when the notified id isnullor matchesroom.localParticipant.identity.
This gives the live re-stamp in both modes with no behavior change for existing non-shared apps, and should cut down on the extra code needed. The changeset gets to stay a plain patch. The synchronization and remove-before-dispose fixes in E2EEManager and most of the new tests can carry over as-is.
0fdcffa to
17b95e1
Compare
|
Thanks, agreed. I've reworked the PR along those lines and force-pushed it as a single commit on top of current main (including #1061's keySize). What changed latestSetIndex stays per-participant, so setKey(key, localIdentity, n) moves the local sender and a remote participant's install never does. Existing non-shared apps see no behavior change for valid indexes. The single currentKeyIndex and setCurrentKeyIndex are gone. Two additions beyond the suggestion, both needed for the shared-key case: Senders created after a rotation. setSharedKey never wrote latestSetIndex, so in shared mode getLatestKeyIndex() always returned 0. Re-stamping fixes tracks that are already published, but a track published after setSharedKey(key, 1) (and every data packet) would still have used slot 0. BaseKeyProvider now records the installed shared slot, and getLatestKeyIndex returns it when enableSharedKey is on. Index handling, matched to libwebrtc. Once indexes are pushed to live senders, the recorded index has to be exactly what native writes: A non-negative index is recorded as index % keyRingSize, with the ring size clamped like native (≤ 0 → default, > 256 → 256). Native wraps on install, but GetKeySet does no bounds check, so stamping the raw index would read past the key ring. Tests: FrameCryptorKeyIndexTest has 23 cases. They cover: shared and per-participant rotation spotlessCheck, livekit-android-sdk:detektRelease and FrameCryptorKeyIndexTest pass locally. |
Media frame cryptors were stamped with a key index once, at sender creation, so after a key rotation already-published tracks kept encrypting with the old slot and receivers hit MISSING_KEY. In shared-key mode, tracks published and data packets sent after a rotation also used slot 0, because getLatestKeyIndex() only knew per-participant (setKey) indexes. KeyProvider - New opt-in KeyIndexObservable: observers are told (participantId, keyIndex) when a key index is written; null means the shared key. - BaseKeyProvider keeps the per-participant latestSetIndex model (now a ConcurrentHashMap), so setKey(key, localIdentity, n) moves the local sender and a remote participant's install never does. - setSharedKey records the installed shared slot; getLatestKeyIndex returns it in shared-key mode. - Recorded indexes are the slot libwebrtc actually writes: a non-negative index wraps by the effective key ring size (clamped like native: <= 0 -> default, > 256 -> 256), and a negative index records nothing, so senders are never stamped with a slot that doesn't exist. - Native fans shared installs and ratchets out to every participant handler with the given index, and each resolves -1 to its own current slot, so shared operations resolve a null or negative index to the recorded shared slot first. Per-participant operations reach one handler and pass -1 through for implicit ratchets / negative installs. E2EEManager - Re-stamps the local participant's sender cryptors when the notified id is null or the local identity, reading the index on the RTC thread. - ratchetKey() ratchets the slot senders encrypt with, not slot 0. - Sender/receiver setup inserts and stamps under the frameCryptors monitor; removePublishedTrack/removeSubscribedTrack remove the map entry before disposing. Tests: FrameCryptorKeyIndexTest (23 cases). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UsvujjBPaU4EHCGdR6rXK4
8a29111 to
57a1cad
Compare
Supersedes #1039 — ports the approach merged in client-sdk-swift#1157 to Android.
Problem
addRtpSenderstamps a sender'sFrameCryptor.keyIndexonce at creation and never updates it. Apps that rotate keys into different key-ring slots (e.g. per-epoch MLS key rings in E2EE deployments) can update live data packets viasetSharedKey(key, index), but media senders keep stamping the old index — receivers hitMISSING_KEYand drop frames. There is no public API to re-point live senders.Approach
Mirrors the Swift design rather than adding a
setKeyIndex(participant)API (whose identity parameter let a remote identity redirect local encryption — the review concern on the earlier Swift PR #1156).KeyIndexObservable— new opt-in contract forKeyProviders that track the local encryption index:BaseKeyProviderimplements it —currentKeyIndexis the single source of truth:setSharedKey(key, index)moves it (parity with JS SDKsetSharedKeybehavior merged in client-sdk-js#1830/#1831)setCurrentKeyIndex(index)exposes it directly (ring-size modulo)getLatestKeyIndex()returns it — senders stamp at creation, data packets pick it up per-sendE2EEManagersubscribes internally and re-stamps the frame cryptors of already-published local tracks on the RTC thread (viaexecuteOnRTCThread, honoring the SDK's threading rule). Receivers are untouched — they read the index from each frame's trailer.Behavior change
In non-shared key mode,
setKey()no longer moves the sender index — the key may belong to a remote participant, so it must not redirect local encryption. Apps switching the local index should callsetCurrentKeyIndex(). This matches the Swift change.Custom
KeyProviderimplementations (e.g. ones that bypassBaseKeyProviderto hand raw key bytes to the RTC provider) opt into live re-pointing by implementingKeyIndexObservable.Test plan
FrameCryptorKeyIndexTest(new, 7 tests): shared-key install moves index,setCurrentKeyIndexmodulo wrap, non-sharedsetKeydoes not move index, observer add/remove, new senders stamp current index, existing senders follow changes, receivers not re-stampede2eetests pass (DataPacketCryptorMockE2ETest,DataTrackCryptorTest):livekit-android-sdk:detektRelease,spotlessCheckclean