Skip to content

Commit b682845

Browse files
fix e2ee proto gen (#126)
1 parent 1ee53a6 commit b682845

6 files changed

Lines changed: 259 additions & 125 deletions

File tree

include/livekit/e2ee.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ enum class EncryptionType {
3434
CUSTOM = 2,
3535
};
3636

37-
/* Defaults (match other SDKs / Python defaults). */
37+
/* Key derivation algorithm used by the key provider. */
38+
enum class KeyDerivationFunction {
39+
PBKDF2 = 0,
40+
HKDF = 1,
41+
};
42+
43+
/* Defaults (match Rust KeyProviderOptions::default()). */
3844
inline constexpr const char* kDefaultRatchetSalt = "LKFrameEncryptionKey";
3945
inline constexpr int kDefaultRatchetWindowSize = 16;
4046
inline constexpr int kDefaultFailureTolerance = -1;
47+
inline constexpr int kDefaultKeyRingSize = 16;
48+
inline constexpr KeyDerivationFunction kDefaultKeyDerivationFunction = KeyDerivationFunction::PBKDF2;
4149

4250
/**
4351
* Options for configuring the key provider used by E2EE.
@@ -46,8 +54,7 @@ inline constexpr int kDefaultFailureTolerance = -1;
4654
* - `shared_key` is optional. If omitted, the application may set keys later
4755
* (e.g. via KeyProvider::setSharedKey / per-participant keys).
4856
* - `ratchet_salt` may be empty to indicate "use implementation default".
49-
* - `ratchet_window_size` and `failure_tolerance` use SDK defaults unless
50-
* overridden.
57+
* - Other key provider fields use SDK defaults unless overridden.
5158
*/
5259
struct KeyProviderOptions {
5360
/// Shared static key for "shared-key E2EE" (optional).
@@ -70,6 +77,12 @@ struct KeyProviderOptions {
7077

7178
/// Number of tolerated ratchet failures before reporting encryption errors.
7279
int failure_tolerance = kDefaultFailureTolerance;
80+
81+
/// Number of key slots retained by the key provider.
82+
int key_ring_size = kDefaultKeyRingSize;
83+
84+
/// Algorithm used when deriving ratcheted keys.
85+
KeyDerivationFunction key_derivation_function = kDefaultKeyDerivationFunction;
7386
};
7487

7588
/**

src/ffi_client.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "livekit/ffi_handle.h"
2828
#include "livekit/room.h"
2929
#include "livekit/rpc_error.h"
30-
#include "livekit/track.h"
3130
#include "livekit_ffi.h"
3231
#include "lk_log.h"
3332
#include "room.pb.h"
@@ -37,10 +36,6 @@ namespace livekit {
3736

3837
namespace {
3938

40-
std::string bytesToString(const std::vector<std::uint8_t>& b) {
41-
return std::string(reinterpret_cast<const char*>(b.data()), b.size());
42-
}
43-
4439
inline void logAndThrow(const std::string& error_msg) {
4540
LK_LOG_ERROR("LiveKit SDK Error: {}", error_msg);
4641
throw std::runtime_error(error_msg);
@@ -333,35 +328,7 @@ std::future<proto::ConnectCallback> FfiClient::connectAsync(const std::string& u
333328

334329
auto* enc = opts->mutable_encryption();
335330
enc->set_encryption_type(static_cast<proto::EncryptionType>(e2ee.encryption_type));
336-
auto* kp = enc->mutable_key_provider_options();
337-
// shared_key is optional. If not set, leave the field unset/cleared.
338-
if (kpo.shared_key && !kpo.shared_key->empty()) {
339-
kp->set_shared_key(bytesToString(*kpo.shared_key));
340-
} else {
341-
kp->clear_shared_key();
342-
}
343-
// Only set ratchet_salt if caller overrides. Otherwise clear so Rust side
344-
// uses default.
345-
if (!kpo.ratchet_salt.empty() &&
346-
kpo.ratchet_salt !=
347-
std::vector<std::uint8_t>(kDefaultRatchetSalt,
348-
kDefaultRatchetSalt + std::char_traits<char>::length(kDefaultRatchetSalt))) {
349-
kp->set_ratchet_salt(bytesToString(kpo.ratchet_salt));
350-
} else {
351-
kp->clear_ratchet_salt();
352-
}
353-
// Same idea for window size / tolerance: set only on override; otherwise
354-
// clear.
355-
if (kpo.ratchet_window_size != kDefaultRatchetWindowSize) {
356-
kp->set_ratchet_window_size(kpo.ratchet_window_size);
357-
} else {
358-
kp->clear_ratchet_window_size();
359-
}
360-
if (kpo.failure_tolerance != kDefaultFailureTolerance) {
361-
kp->set_failure_tolerance(kpo.failure_tolerance);
362-
} else {
363-
kp->clear_failure_tolerance();
364-
}
331+
enc->mutable_key_provider_options()->CopyFrom(toProto(kpo));
365332
}
366333

367334
// --- RTC configuration (optional) ---

src/room_proto_converter.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
#include "room_proto_converter.h"
1818

1919
#include "livekit/data_stream.h"
20-
#include "livekit/local_participant.h"
2120
#include "room.pb.h"
2221

2322
namespace livekit {
2423

2524
namespace {
2625

26+
std::string bytesToString(const std::vector<std::uint8_t>& bytes) {
27+
return std::string(reinterpret_cast<const char*>(bytes.data()), bytes.size());
28+
}
29+
2730
std::vector<proto::PacketTrailerFeature> toProto(const PacketTrailerFeatures& features) {
2831
std::vector<proto::PacketTrailerFeature> out;
2932
out.reserve(2);
@@ -309,6 +312,25 @@ RoomMovedEvent roomMovedFromProto(const proto::RoomInfo& in) {
309312

310313
// ---------------- Room Options ----------------
311314

315+
proto::KeyProviderOptions toProto(const KeyProviderOptions& in) {
316+
proto::KeyProviderOptions out;
317+
if (in.shared_key && !in.shared_key->empty()) {
318+
out.set_shared_key(bytesToString(*in.shared_key));
319+
} else {
320+
out.clear_shared_key();
321+
}
322+
if (!in.ratchet_salt.empty()) {
323+
out.set_ratchet_salt(bytesToString(in.ratchet_salt));
324+
} else {
325+
out.set_ratchet_salt(kDefaultRatchetSalt);
326+
}
327+
out.set_ratchet_window_size(in.ratchet_window_size);
328+
out.set_failure_tolerance(in.failure_tolerance);
329+
out.set_key_ring_size(in.key_ring_size);
330+
out.set_key_derivation_function(static_cast<proto::KeyDerivationFunction>(in.key_derivation_function));
331+
return out;
332+
}
333+
312334
proto::AudioEncoding toProto(const AudioEncodingOptions& in) {
313335
proto::AudioEncoding msg;
314336
msg.set_max_bitrate(in.max_bitrate);

src/room_proto_converter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <string>
2020

21+
#include "e2ee.pb.h"
22+
#include "livekit/e2ee.h"
2123
#include "livekit/room_event_types.h"
2224
#include "livekit/visibility.h"
2325
#include "room.pb.h"
@@ -69,6 +71,8 @@ LIVEKIT_INTERNAL_API RoomMovedEvent roomMovedFromProto(const proto::RoomInfo& in
6971

7072
// --------- room options conversions ---------
7173

74+
LIVEKIT_INTERNAL_API proto::KeyProviderOptions toProto(const KeyProviderOptions& in);
75+
7276
LIVEKIT_INTERNAL_API proto::AudioEncoding toProto(const AudioEncodingOptions& in);
7377
LIVEKIT_INTERNAL_API AudioEncodingOptions fromProto(const proto::AudioEncoding& in);
7478

0 commit comments

Comments
 (0)