Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/e2ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ std::string bytesToString(const std::vector<std::uint8_t>& v) {

std::vector<std::uint8_t> stringToBytes(const std::string& s) { return std::vector<std::uint8_t>(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<std::string> participantTrackSids(std::uint64_t room_handle, const std::string& participant_identity) {
const auto resp = getFrameCryptors(room_handle);
std::vector<std::string> 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

// ============================================================================
Expand Down Expand Up @@ -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);
}
}

// ============================================================================
Expand All @@ -153,10 +179,7 @@ std::weak_ptr<E2EEManager::KeyProvider> E2EEManager::keyProvider() { return key_
std::weak_ptr<const E2EEManager::KeyProvider> E2EEManager::keyProvider() const { return key_provider_; }

std::vector<E2EEManager::FrameCryptor> 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<E2EEManager::FrameCryptor> out;
const auto& list = resp.e2ee().manager_get_frame_cryptors().frame_cryptors();
out.reserve(static_cast<std::size_t>(list.size()));
Expand Down
33 changes: 33 additions & 0 deletions src/tests/integration/test_e2ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <tuple>
#include <vector>

#include "../common/audio_utils.h"
Expand Down Expand Up @@ -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<std::uint8_t>{'k', 'e', 'y'};
ASSERT_TRUE(room.connect(config_.url, config_.token_a, room_options));
auto participant = lockLocalParticipant(room);
auto source = std::make_shared<AudioSource>(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<std::uint8_t>{'n', 'e', 'w'}, 1);

auto cryptors = manager->frameCryptors();
ASSERT_FALSE(cryptors.empty());
cryptors.front().setKeyIndex(1);
cryptors.front().setEnabled(false);

std::vector<std::tuple<std::string, int, bool>> states;
for (const auto& cryptor : manager->frameCryptors()) {
states.emplace_back(cryptor.participantIdentity(), cryptor.keyIndex(), cryptor.enabled());
}
EXPECT_EQ(states, (std::vector<std::tuple<std::string, int, bool>>{{participant->identity(), 1, false},
{participant->identity(), 1, false}}));
participant->unpublishTrack(microphone->publication()->sid());
participant->unpublishTrack(screen_share->publication()->sid());
}

} // namespace livekit::test
Loading