From 977f135c500d3efb2d1e1c42206f74426494fa98 Mon Sep 17 00:00:00 2001 From: darken Date: Mon, 30 Jun 2025 13:52:15 +0200 Subject: [PATCH] Allow empty keys to clear IRK/EncKey This commit modifies the handling of Identity Resolving Key (IRK) and Encryption Key (EncKey) to allow users to clear these values by providing an empty input. Specifically: - In `GeneralSettingsFragment.kt`, the `onKey` callbacks for both IRK and EncKey dialogs now use `takeIf { it.isNotEmpty() }` after converting the input hex string to a byte array. This ensures that an empty input results in `null` being set for the respective key. - In `PodMonitor.kt`, when determining the main device, `mainDeviceIdentityKey.value` is now checked with `takeIf { it.isNotEmpty() }` to ensure an empty IRK is treated as no IRK being configured. - In `AppleFactory.kt`, when attempting to decrypt the private payload, `mainDeviceEncryptionKey.value` is now checked with `takeIf { it.isNotEmpty() }` to ensure an empty EncKey prevents decryption attempts. --- .../main/java/eu/darken/capod/monitor/core/PodMonitor.kt | 2 +- .../java/eu/darken/capod/pods/core/apple/AppleFactory.kt | 2 +- .../main/ui/settings/general/GeneralSettingsFragment.kt | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt b/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt index 1ddf0d4f..dc92fa5e 100644 --- a/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt +++ b/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt @@ -198,7 +198,7 @@ class PodMonitor @Inject constructor( } private fun determineMainDevice(pods: List): PodDevice? { - val identityKey = generalSettings.mainDeviceIdentityKey.value + val identityKey = generalSettings.mainDeviceIdentityKey.value?.takeIf { it.isNotEmpty() } if (identityKey != null) { val irkHit = pods.filterIsInstance().firstOrNull { it.flags.isIRKMatch } log(TAG) { "IRK is configured, main device irkHit=$irkHit" } diff --git a/app-common/src/main/java/eu/darken/capod/pods/core/apple/AppleFactory.kt b/app-common/src/main/java/eu/darken/capod/pods/core/apple/AppleFactory.kt index ef7546fe..6692c8a8 100644 --- a/app-common/src/main/java/eu/darken/capod/pods/core/apple/AppleFactory.kt +++ b/app-common/src/main/java/eu/darken/capod/pods/core/apple/AppleFactory.kt @@ -71,7 +71,7 @@ class AppleFactory @Inject constructor( if (!isIrkMatch) return@run null if (proximityMessage.data.size != ProximityPairing.PAIRING_MESSAGE_LENGTH) return@run null - val encKey = generalSettings.mainDeviceEncryptionKey.value + val encKey = generalSettings.mainDeviceEncryptionKey.value?.takeIf { it.isNotEmpty() } if (encKey == null) return@run null val encrypted = proximityMessage.data.takeLast(16).toUByteArray().toByteArray() diff --git a/app/src/main/java/eu/darken/capod/main/ui/settings/general/GeneralSettingsFragment.kt b/app/src/main/java/eu/darken/capod/main/ui/settings/general/GeneralSettingsFragment.kt index 4b628ca4..40c9b383 100644 --- a/app/src/main/java/eu/darken/capod/main/ui/settings/general/GeneralSettingsFragment.kt +++ b/app/src/main/java/eu/darken/capod/main/ui/settings/general/GeneralSettingsFragment.kt @@ -55,7 +55,9 @@ class GeneralSettingsFragment : PreferenceFragment3() { AirPodKeyInputDialog(requireContext()).create( mode = AirPodKeyInputDialog.Mode.IRK, current = generalSettings.mainDeviceIdentityKey.value?.toHex() ?: "", - onKey = { generalSettings.mainDeviceIdentityKey.value = it.fromHex() }, + onKey = { raw -> + generalSettings.mainDeviceIdentityKey.value = raw.fromHex().takeIf { it.isNotEmpty() } + }, onGuide = { webpageTool.open("https://github.com/d4rken-org/capod/wiki/airpod-Keys") } ).show() true @@ -64,7 +66,9 @@ class GeneralSettingsFragment : PreferenceFragment3() { AirPodKeyInputDialog(requireContext()).create( mode = AirPodKeyInputDialog.Mode.ENC, current = generalSettings.mainDeviceEncryptionKey.value?.toHex() ?: "", - onKey = { generalSettings.mainDeviceEncryptionKey.value = it.fromHex() }, + onKey = { raw -> + generalSettings.mainDeviceEncryptionKey.value = raw.fromHex().takeIf { it.isNotEmpty() } + }, onGuide = { webpageTool.open("https://github.com/d4rken-org/capod/wiki/airpod-Keys") } ).show() true