diff --git a/.changeset/e2ee-key-size.md b/.changeset/e2ee-key-size.md new file mode 100644 index 000000000..1fe755f43 --- /dev/null +++ b/.changeset/e2ee-key-size.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": minor +--- + +Add a `keySize` option to `BaseKeyProvider` (`KeyProviderKeySize.BITS_128` by default, or `BITS_256`) to derive AES-256-GCM frame keys for E2EE. All participants in a room must use the same size. Matches `KeyProviderOptions.keySize` in the JS and Swift SDKs. diff --git a/livekit-android-sdk/detekt-baseline-release.xml b/livekit-android-sdk/detekt-baseline-release.xml index 026aca043..6f7928891 100644 --- a/livekit-android-sdk/detekt-baseline-release.xml +++ b/livekit-android-sdk/detekt-baseline-release.xml @@ -40,7 +40,7 @@ LongMethod:Room.kt$Room$@Throws(Exception::class) suspend fun connect(url: String, token: String, options: ConnectOptions = ConnectOptions()) LongMethod:SignalClient.kt$SignalClient$private fun handleSignalResponseImpl(ws: WebSocket, response: LivekitRtc.SignalResponse, encoded: ByteArray) LongParameterList:AudioBufferCallbackDispatcher.kt$AudioBufferCallback$(buffer: ByteBuffer, audioFormat: Int, channelCount: Int, sampleRate: Int, bytesRead: Int, captureTimeNs: Long) - LongParameterList:KeyProvider.kt$BaseKeyProvider$( ratchetSalt: String = defaultRatchetSalt, uncryptedMagicBytes: String = defaultMagicBytes, ratchetWindowSize: Int = defaultRatchetWindowSize, override var enableSharedKey: Boolean = true, failureTolerance: Int = defaultFailureTolerance, keyRingSize: Int = defaultKeyRingSize, discardFrameWhenCryptorNotReady: Boolean = defaultDiscardFrameWhenCryptorNotReady, keyDerivationAlgorithm: FrameCryptorKeyDerivationAlgorithm = defaultKeyDerivationAlgorithm, ) + LongParameterList:KeyProvider.kt$BaseKeyProvider$( ratchetSalt: String = defaultRatchetSalt, uncryptedMagicBytes: String = defaultMagicBytes, ratchetWindowSize: Int = defaultRatchetWindowSize, override var enableSharedKey: Boolean = true, failureTolerance: Int = defaultFailureTolerance, keyRingSize: Int = defaultKeyRingSize, discardFrameWhenCryptorNotReady: Boolean = defaultDiscardFrameWhenCryptorNotReady, keyDerivationAlgorithm: FrameCryptorKeyDerivationAlgorithm = defaultKeyDerivationAlgorithm, keySize: KeyProviderKeySize = defaultKeySize, ) LongParameterList:LiveKitOverrides.kt$AudioOptions$( /** * Override the default output [AudioType]. * * This affects the audio routing and how the audio is handled. Default is [AudioType.CallAudioType]. * * Note: if [audioHandler] is also passed, the values from [audioOutputType] will not be reflected in it, * and must be set yourself. */ val audioOutputType: AudioType? = null, /** * Override the default [AudioHandler]. * * Default is [AudioSwitchHandler]. * * Use [NoAudioHandler] to turn off automatic audio handling or * [AudioFocusHandler] to get simple audio focus handling. */ val audioHandler: AudioHandler? = null, /** * Override the default [AudioDeviceModule]. * * If a non-null value is passed, the library does not * take ownership of the object and will not release it upon [Room.release]. * It is the responsibility of the owner to call [AudioDeviceModule.release] when finished * with it to prevent memory leaks. */ val audioDeviceModule: AudioDeviceModule? = null, /** * Called after default setup to allow for customizations on the [JavaAudioDeviceModule]. * * Not used if [audioDeviceModule] is provided. * * Note: We require setting the [JavaAudioDeviceModule.Builder.setSamplesReadyCallback] to provide * support for [LocalAudioTrack.addSink]. If you wish to grab the audio samples * from the local microphone track, use [LocalAudioTrack.addSink] instead of setting your own * callback. */ val javaAudioDeviceModuleCustomizer: ((builder: JavaAudioDeviceModule.Builder) -> Unit)? = null, /** * On Android 11+, the audio mode will reset itself from [AudioManager.MODE_IN_COMMUNICATION] if * there is no audio playback or capture for 6 seconds (for example when joining a room with * no speakers and the local mic is muted.) This mode reset will cause unexpected * behavior when trying to change the volume, causing it to not properly change the volume. * * We use a workaround by playing a silent audio track to keep the communication mode from * resetting. * * Setting this flag to true will disable the workaround. * * This flag is a no-op when the audio mode is set to anything other than * [AudioManager.MODE_IN_COMMUNICATION]. */ val disableCommunicationModeWorkaround: Boolean = false, /** * Options for processing the mic and incoming audio. */ val audioProcessorOptions: AudioProcessorOptions? = null, /** * Devices may take some time initializing the audio stack for recording. * Prewarming allows starting up the underlying audio recording prior to publish, letting * the audio device be ready immediately when the track is fully published. * * If set to true, disables audio recording prewarming (and the related * [LocalAudioTrack.prewarm] function), and audio resources are only used while the * track is connected and published. Defaults to false. */ val disableAudioPrewarming: Boolean = false, ) LongParameterList:LocalAudioTrack.kt$LocalAudioTrack$( @Assisted name: String, @Assisted mediaTrack: livekit.org.webrtc.AudioTrack, @Assisted options: LocalAudioTrackOptions, private val audioProcessingController: AudioProcessingController, @Named(InjectionNames.DISPATCHER_DEFAULT) private val dispatcher: CoroutineDispatcher, @Named(InjectionNames.LOCAL_AUDIO_RECORD_SAMPLES_DISPATCHER) private val audioRecordSamplesDispatcher: AudioRecordSamplesDispatcher, @Named(InjectionNames.LOCAL_AUDIO_BUFFER_CALLBACK_DISPATCHER) private val audioBufferCallbackDispatcher: AudioBufferCallbackDispatcher, private val audioRecordPrewarmer: AudioRecordPrewarmer, rtcThreadToken: RTCThreadToken, ) LongParameterList:LocalParticipant.kt$LocalParticipant$( @Assisted internal var dynacast: Boolean, internal val engine: RTCEngine, private val peerConnectionFactory: PeerConnectionFactory, private val context: Context, private val eglBase: EglBase, private val screencastVideoTrackFactory: LocalScreencastVideoTrack.Factory, private val videoTrackFactory: LocalVideoTrack.Factory, private val audioTrackFactory: LocalAudioTrack.Factory, private val defaultsManager: DefaultsManager, @Named(InjectionNames.DISPATCHER_DEFAULT) coroutineDispatcher: CoroutineDispatcher, @Named(InjectionNames.SENDER) private val capabilitiesGetter: CapabilitiesGetter, private val outgoingDataStreamManager: OutgoingDataStreamManager, private val outgoingDataTrackManager: OutgoingDataTrackManager, private val rpcClientManager: RpcClientManager, private val rpcServerManager: RpcServerManager, ) @@ -66,7 +66,6 @@ NestedBlockDepth:LocalParticipant.kt$LocalParticipant$@Throws(TrackException.PublishException::class) private suspend fun publishTrackImpl( track: Track, options: TrackPublishOptions, requestConfig: AddTrackRequest.Builder.() -> Unit, encodings: List<RtpParameters.Encoding> = emptyList(), publishListener: PublishListener? = null, ): LocalTrackPublication? NestedBlockDepth:LocalParticipant.kt$LocalParticipant$fun cleanup() NestedBlockDepth:LocalVideoTrack.kt$LocalVideoTrack$internal fun setPublishingCodecs(codecs: List<SubscribedCodec>): List<VideoCodec> - NestedBlockDepth:LocalVideoTrack.kt$LocalVideoTrack$private fun setPublishingLayersForSender( sender: RtpSender, qualities: List<LivekitRtc.SubscribedQuality>, ) NestedBlockDepth:RTCEngine.kt$RTCEngine$@CheckResult internal suspend fun sendData(dataPacket: LivekitModels.DataPacket): Result<Unit> NestedBlockDepth:Room.kt$Room$override suspend fun onPostReconnect(isFullReconnect: Boolean) NestedBlockDepth:SignalClient.kt$SignalClient$override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/E2EEOptions.kt b/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/E2EEOptions.kt index a09d05779..641f18a4a 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/E2EEOptions.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/E2EEOptions.kt @@ -26,6 +26,7 @@ internal const val defaultFailureTolerance = -1 internal const val defaultKeyRingSize = 16 internal const val defaultDiscardFrameWhenCryptorNotReady = false internal val defaultKeyDerivationAlgorithm = FrameCryptorKeyDerivationAlgorithm.PBKDF2 +internal val defaultKeySize = KeyProviderKeySize.BITS_128 data class E2EEOptions( var keyProvider: KeyProvider = BaseKeyProvider( @@ -37,6 +38,7 @@ data class E2EEOptions( defaultKeyRingSize, defaultDiscardFrameWhenCryptorNotReady, defaultKeyDerivationAlgorithm, + defaultKeySize, ), var encryptionType: Encryption.Type = Encryption.Type.GCM, ) { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/KeyProvider.kt b/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/KeyProvider.kt index 55dc57abd..e76b23f2f 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/KeyProvider.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/e2ee/KeyProvider.kt @@ -27,6 +27,18 @@ internal class KeyInfo(var participantId: String, var keyIndex: Int, var key: St } } +/** + * The length of the AES-GCM key derived from the shared or per-participant key material. + * + * Every participant in a room must use the same size: a 256-bit client and a 128-bit client + * cannot decrypt each other's media or data. The default is [BITS_128], which matches the + * historical behavior of all SDKs. + */ +enum class KeyProviderKeySize(val bits: Int) { + BITS_128(128), + BITS_256(256), +} + interface KeyProvider { fun setSharedKey(key: String, keyIndex: Int? = 0): Boolean fun ratchetSharedKey(keyIndex: Int? = 0): ByteArray @@ -51,6 +63,7 @@ class BaseKeyProvider( keyRingSize: Int = defaultKeyRingSize, discardFrameWhenCryptorNotReady: Boolean = defaultDiscardFrameWhenCryptorNotReady, keyDerivationAlgorithm: FrameCryptorKeyDerivationAlgorithm = defaultKeyDerivationAlgorithm, + keySize: KeyProviderKeySize = defaultKeySize, ) : KeyProvider { private val latestSetIndex = mutableMapOf() @@ -64,6 +77,7 @@ class BaseKeyProvider( keyRingSize, discardFrameWhenCryptorNotReady, keyDerivationAlgorithm, + keySize.bits, ) override fun setSharedKey(key: String, keyIndex: Int?): Boolean { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt index ebb9de898..753bea9d3 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt @@ -332,6 +332,14 @@ constructor( * Options for end-to-end encryption. Must be setup prior to [connect]. * * If null, e2ee will be disabled. + * + * To use AES-256-GCM frame keys instead of the default AES-128-GCM, pass a key size to the + * key provider. All participants in the room must use the same key size. + * ``` + * val keyProvider = BaseKeyProvider(keySize = KeyProviderKeySize.BITS_256) + * keyProvider.setSharedKey("my-shared-key") + * room.e2eeOptions = E2EEOptions(keyProvider = keyProvider) + * ``` */ var e2eeOptions: E2EEOptions? = null