Skip to content

Commit 1d711a8

Browse files
authored
feat(e2ee): add BaseKeyProvider.setKey(keyData:) for raw binary keys (#1070)
Accept arbitrary key bytes instead of a UTF-8 String so binary key material from external E2EE layers can be installed without string conversion. The String variant now delegates to it.
1 parent 1f5bbb7 commit 1d711a8

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

.changes/e2ee-raw-key-data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="added" "Add `BaseKeyProvider.setKey(keyData:participantId:index:)` for installing raw binary E2EE key material without String conversion"

Sources/LiveKit/E2EE/KeyProvider.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,26 @@ public final class BaseKeyProvider: NSObject, Loggable, Sendable {
150150
// MARK: - Key management
151151

152152
public func setKey(key: String, participantId: String? = nil, index: Int32? = nil) {
153+
setKey(keyData: Data(key.utf8), participantId: participantId, index: index)
154+
}
155+
156+
/// Sets a raw binary key without any string conversion.
157+
///
158+
/// - Parameters:
159+
/// - keyData: The key bytes, stored as provided.
160+
/// - participantId: Required in non-shared key mode; the call is a no-op when `nil`.
161+
/// - index: The key index to set. Defaults to the current key index.
162+
public func setKey(keyData: Data, participantId: String? = nil, index: Int32? = nil) {
153163
let targetIndex = index ?? getCurrentKeyIndex()
154164

155165
if options.sharedKey {
156-
let keyData = key.data(using: .utf8)!
157166
rtcKeyProvider.setSharedKey(keyData, with: targetIndex)
158167
} else {
159168
if participantId == nil {
160169
log("setKey: Please provide valid participantId for non-SharedKey mode.")
161170
return
162171
}
163172

164-
let keyData = key.data(using: .utf8)!
165173
rtcKeyProvider.setKey(keyData, with: targetIndex, forParticipant: participantId!)
166174
}
167175

Tests/LiveKitCoreTests/E2EE/KeyProvider.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,22 @@ struct BaseKeyProviderTests {
8585
let exported = try #require(provider.exportKey(index: -1))
8686
#expect(exported == Data("k".utf8))
8787
}
88+
89+
static let nonUtf8Key = Data([0xFF, 0xFE, 0x80, 0x00, 0xDE, 0xAD])
90+
91+
@Test
92+
func setKeyDataSharedKeyPreservesNonUtf8Bytes() throws {
93+
let provider = BaseKeyProvider(options: KeyProviderOptions(sharedKey: true, keyRingSize: 16))
94+
provider.setKey(keyData: Self.nonUtf8Key, index: 2)
95+
let exported = try #require(provider.exportKey(index: 2))
96+
#expect(exported == Self.nonUtf8Key)
97+
}
98+
99+
@Test
100+
func setKeyDataPerParticipantPreservesNonUtf8Bytes() throws {
101+
let provider = BaseKeyProvider(options: KeyProviderOptions(sharedKey: false, keyRingSize: 16))
102+
provider.setKey(keyData: Self.nonUtf8Key, participantId: "alice", index: 1)
103+
let exported = try #require(provider.exportKey(participantId: "alice", index: 1))
104+
#expect(exported == Self.nonUtf8Key)
105+
}
88106
}

0 commit comments

Comments
 (0)