diff --git a/src/e2ee.cpp b/src/e2ee.cpp index 403582df..440f56bf 100644 --- a/src/e2ee.cpp +++ b/src/e2ee.cpp @@ -34,6 +34,26 @@ std::string bytesToString(const std::vector& v) { std::vector stringToBytes(const std::string& s) { return std::vector(s.begin(), s.end()); } +proto::FfiResponse getFrameCryptors(std::uint64_t room_handle) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle); + req.mutable_e2ee()->mutable_manager_get_frame_cryptors(); + return FfiClient::instance().sendRequest(req); +} + +// The FFI addresses a cryptor by participant and track, but a FrameCryptor only knows its participant (adding the +// track would change its layout), so its setters apply to each of the participant's tracks. +std::vector participantTrackSids(std::uint64_t room_handle, const std::string& participant_identity) { + const auto resp = getFrameCryptors(room_handle); + std::vector track_sids; + for (const auto& fc : resp.e2ee().manager_get_frame_cryptors().frame_cryptors()) { + if (fc.participant_identity() == participant_identity) { + track_sids.push_back(fc.track_sid()); + } + } + return track_sids; +} + } // namespace // ============================================================================ @@ -114,19 +134,25 @@ int E2EEManager::FrameCryptor::keyIndex() const { return key_index_; } bool E2EEManager::FrameCryptor::enabled() const { return enabled_; } void E2EEManager::FrameCryptor::setEnabled(bool enabled) { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_participant_identity(participant_identity_); - req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_enabled(enabled); - FfiClient::instance().sendRequest(req); + for (const auto& track_sid : participantTrackSids(room_handle_, participant_identity_)) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle_); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_track_sid(track_sid); + req.mutable_e2ee()->mutable_cryptor_set_enabled()->set_enabled(enabled); + FfiClient::instance().sendRequest(req); + } } void E2EEManager::FrameCryptor::setKeyIndex(int key_index) { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_participant_identity(participant_identity_); - req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_key_index(key_index); - FfiClient::instance().sendRequest(req); + for (const auto& track_sid : participantTrackSids(room_handle_, participant_identity_)) { + proto::FfiRequest req; + req.mutable_e2ee()->set_room_handle(room_handle_); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_participant_identity(participant_identity_); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_track_sid(track_sid); + req.mutable_e2ee()->mutable_cryptor_set_key_index()->set_key_index(key_index); + FfiClient::instance().sendRequest(req); + } } // ============================================================================ @@ -153,10 +179,7 @@ std::weak_ptr E2EEManager::keyProvider() { return key_ std::weak_ptr E2EEManager::keyProvider() const { return key_provider_; } std::vector E2EEManager::frameCryptors() const { - proto::FfiRequest req; - req.mutable_e2ee()->set_room_handle(room_handle_); - req.mutable_e2ee()->mutable_manager_get_frame_cryptors(); - auto resp = FfiClient::instance().sendRequest(req); + auto resp = getFrameCryptors(room_handle_); std::vector out; const auto& list = resp.e2ee().manager_get_frame_cryptors().frame_cryptors(); out.reserve(static_cast(list.size())); diff --git a/src/tests/integration/test_e2ee.cpp b/src/tests/integration/test_e2ee.cpp index 68880c59..47df0f43 100644 --- a/src/tests/integration/test_e2ee.cpp +++ b/src/tests/integration/test_e2ee.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "../common/audio_utils.h" @@ -53,4 +54,36 @@ TEST_F(E2EEIntegrationTest, FrameCryptorsIncludePublishedTrack) { participant->unpublishTrack(track->publication()->sid()); } +TEST_F(E2EEIntegrationTest, FrameCryptorSettersApplyToEachOfTheParticipantsTracks) { + failIfNotConfigured(); + + Room room; + RoomOptions room_options; + room_options.encryption = E2EEOptions{}; + room_options.encryption->key_provider_options.shared_key = std::vector{'k', 'e', 'y'}; + ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options)); + auto participant = lockLocalParticipant(room); + auto source = std::make_shared(kDefaultAudioSampleRate, kDefaultAudioChannels, 0); + auto microphone = participant->publishAudioTrack("e2ee-microphone", source, TrackSource::SOURCE_MICROPHONE); + auto screen_share = + participant->publishAudioTrack("e2ee-screen-share", source, TrackSource::SOURCE_SCREENSHARE_AUDIO); + auto manager = room.e2eeManager().lock(); + ASSERT_NE(manager, nullptr); + manager->keyProvider().lock()->setSharedKey(std::vector{'n', 'e', 'w'}, 1); + + auto cryptors = manager->frameCryptors(); + ASSERT_FALSE(cryptors.empty()); + cryptors.front().setKeyIndex(1); + cryptors.front().setEnabled(false); + + std::vector> states; + for (const auto& cryptor : manager->frameCryptors()) { + states.emplace_back(cryptor.participantIdentity(), cryptor.keyIndex(), cryptor.enabled()); + } + EXPECT_EQ(states, (std::vector>{{participant->identity(), 1, false}, + {participant->identity(), 1, false}})); + participant->unpublishTrack(microphone->publication()->sid()); + participant->unpublishTrack(screen_share->publication()->sid()); +} + } // namespace livekit::test