euicc: a confirmation code can no longer leak into the next profile's download
- Commit
afa1c34c5aff983d404b35b407532716901c374b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 09:34 UTC (5 days ago)
- Parents
a69a3d9
beginDownload keyed only the download state to its activation code (the landed cross-code fix); the submitted confirmation code had no such binding and was never reset. Leaving a completed/failed download via system Back bypassed the Done/Cancel paths that clear it, so the stale code survived; confirming profile B then passed A's code explicitly into nativeFinishDownload, where resolve_confirmation_code prefers it over the code B's session was authenticated with — B's PrepareDownload carried hashCc of A's code, a wrong-code refusal for a profile whose own code was correct. beginDownload now clears pendingTransaction, pendingConfirmationCode and pendingCarrier when a fresh authentication starts. Same-code resume from live states and re-authentication after a code refusal are untouched. New end-to-end test over the fake SM-DP+/eUICC asserts B's PrepareDownload APDUs carry hashCc(B) and not hashCc(A) (failed pre-fix). Verified: :euicc:testDevUnitTest 113 tests pass; lint errors are pre-existing on untouched files.
Diff
euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt b/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.ktindex 6b58074d29..a083c8c984 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt@@ -166,6 +166,15 @@ class EuiccViewModel(app: Application) : AndroidViewModel(app) { /** Authenticates [activationCode] and lands on its preview/code state. */ private fun beginDownload(activationCode: String, imei: String?, confirmationCode: String?) { activeActivationCode = activationCode.trim()+ // A fresh authenticate starts a new server session: inputs submitted+ // to a previous session — above all its confirmation code — must not+ // carry over. Terminal states retain them (leaving Complete/Failed by+ // Back, rather than Done/Cancel, never calls cancel/clear), and+ // confirmDownload would otherwise pass the stale code as an explicit+ // code, ahead of the one this session was authenticated with.+ pendingTransaction = null+ pendingConfirmationCode = null+ pendingCarrier = null download = DownloadState.Preparing viewModelScope.launch { val session = withContext(Dispatchers.IO) {
euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt
diff --git a/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt b/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.ktindex 9febc5dfa2..af1ae44801 100644--- a/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt+++ b/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt@@ -273,6 +273,64 @@ class FakeSmdpIntegrationTest { assertTrue(smdp.count("initiateAuthentication") >= 2, "counts=${smdp.counts}") } + @Test fun `viewmodel new download does not reuse previous confirmation code`() {+ // Regression: a confirmation code submitted for one profile stayed+ // in the ViewModel after that download completed, and the next+ // profile's confirm passed it to nativeFinishDownload as an explicit+ // code — where it wins over the code that profile's own session was+ // authenticated with. Leaving a terminal screen by Back (rather than+ // its Done/Cancel actions) never clears the stale code.+ smdp.ccRequired = true+ val vm = vm()+ awaitVm(vm)+ vm.startDownload(smdp.activationCode())+ awaitDownload(vm, { it is DownloadState.AwaitingConfirmationCode || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.AwaitingConfirmationCode, "download=${vm.download}")+ vm.submitConfirmationCode("1111")+ awaitDownload(vm, { it is DownloadState.Complete || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Complete, "download=${vm.download}")+ // Open a second profile, authenticated with its own code "2222".+ val second = "LPA:1\$http://127.0.0.1:${smdp.port}\$OTHER-2"+ vm.enterDownload(second, confirmationCode = "2222")+ awaitDownload(vm, { it is DownloadState.Confirm || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ val callsBefore = FakeEuiccTelephonyShadow.transmitCalls.size+ vm.confirmDownload()+ awaitDownload(vm, { it is DownloadState.Complete || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Complete, "download=${vm.download}")+ // Reassemble the STORE DATA commands sent while confirming, and read+ // the PrepareDownload (BF21) one: its hashCc must be the second+ // profile's "2222", never the first profile's submitted "1111".+ val commands = mutableListOf<String>()+ val current = StringBuilder()+ for (call in FakeEuiccTelephonyShadow.transmitCalls.drop(callsBefore)) {+ if (call.cla != 0x80 || call.instruction != 0xE2) continue+ current.append(call.data)+ if (call.p1 == 0x91) {+ commands += current.toString()+ current.clear()+ }+ }+ val prepare = commands.firstOrNull { it.startsWith("BF21") }+ assertNotNull(prepare, "commands=$commands")+ assertTrue(+ prepare.contains(hashCcHex("2222")),+ "second profile was not prepared with its own code: $prepare",+ )+ assertFalse(+ prepare.contains(hashCcHex("1111")),+ "second profile was prepared with the first profile's code: $prepare",+ )+ }++ /** SGP.22 `hashCc` for [code] under the fake SM-DP+'s transaction id. */+ private fun hashCcHex(code: String): String {+ val digest = java.security.MessageDigest.getInstance("SHA-256")+ val inner = digest.digest(code.toByteArray())+ digest.update(inner)+ return digest.digest("txbin123".toByteArray()).joinToString("") { "%02X".format(it.toInt() and 0xFF) }+ }+ @Test fun `manager service maps profiles and runs operations`() { val controller = Robolectric.buildService(EuiccManagerService::class.java) val service = controller.create().get()