From 936444f162b101bc9c3df98e48d6eaf45112d123 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Thu, 19 May 2022 22:38:57 -0400 Subject: [PATCH] Handle APDU responses with stripped status code better This is still pretty hacky. Ideally we should return structured data (at least separate the status code from the payload, or better, just do everything in binary) from ApduTransmitter / ApduChannel. --- .../lpa/impl/download/ApduTransmitter.kt | 8 ++++ .../download/InstallationPhaseWorker.java | 37 +++++-------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/ApduTransmitter.kt b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/ApduTransmitter.kt index 6608923..6bc3615 100644 --- a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/ApduTransmitter.kt +++ b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/ApduTransmitter.kt @@ -39,6 +39,10 @@ class ApduTransmitter(private val apduChannel: ApduChannel) { ) } + if (apduResponse.length < 4) { + throw RuntimeException("APDU response should at least contain a status code") + } + // Last 2 bytes are the status code (should be 0x9000) // TODO: Do this properly return apduResponse.substring(0, apduResponse.length - 4) @@ -57,6 +61,10 @@ class ApduTransmitter(private val apduChannel: ApduChannel) { ) } + if (apduResponse.length < 4) { + throw RuntimeException("APDU response should at least contain a status code") + } + // Last 2 bytes are the status code (should be 0x9000) // TODO: Do this properly return apduResponse.substring(0, apduResponse.length - 4) diff --git a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/InstallationPhaseWorker.java b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/InstallationPhaseWorker.java index eb4c57c..d18ccc2 100644 --- a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/InstallationPhaseWorker.java +++ b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/InstallationPhaseWorker.java @@ -98,10 +98,8 @@ public class InstallationPhaseWorker { String profileInstallationResult = apduTransmitter.transmitApdus(sbpp); - if (StringUtils.isNotBlank(profileInstallationResult) && profileInstallationResult.length() > 4) { + if (StringUtils.isNotBlank(profileInstallationResult)) { checkProfileInstallationResult(profileInstallationResult); - } else { - throw new RuntimeException("Unexpected response on loadBoundProfilePackage"); } } @@ -112,12 +110,8 @@ public class InstallationPhaseWorker { String profileInstallationResult = apduTransmitter.transmitApdus(sbpp); - if (profileInstallationResult.compareTo("9000") != 0) { - if (StringUtils.isNotBlank(profileInstallationResult) && profileInstallationResult.length() > 4) { - checkProfileInstallationResult(profileInstallationResult); - } else { - throw new RuntimeException("Unexpected response on loadStoreMetadata"); - } + if (StringUtils.isNotBlank(profileInstallationResult)) { + checkProfileInstallationResult(profileInstallationResult); } } @@ -128,12 +122,8 @@ public class InstallationPhaseWorker { String profileInstallationResult = apduTransmitter.transmitApdus(sbpp); - if (profileInstallationResult.compareTo("9000") != 0) { - if (StringUtils.isNotBlank(profileInstallationResult) && profileInstallationResult.length() > 4) { - checkProfileInstallationResult(profileInstallationResult); - } else { - throw new RuntimeException("Unexpected response on loadConfigureIsdpa"); - } + if (StringUtils.isNotBlank(profileInstallationResult)) { + checkProfileInstallationResult(profileInstallationResult); } } @@ -144,12 +134,8 @@ public class InstallationPhaseWorker { String profileInstallationResult = apduTransmitter.transmitApdus(sbpp); - if (profileInstallationResult.compareTo("9000") != 0) { - if (StringUtils.isNotBlank(profileInstallationResult) && profileInstallationResult.length() > 4) { - checkProfileInstallationResult(profileInstallationResult); - } else { - throw new RuntimeException("Unexpected response on loadInitialiseSecureChannel"); - } + if (StringUtils.isNotBlank(profileInstallationResult)) { + checkProfileInstallationResult(profileInstallationResult); } } @@ -237,13 +223,8 @@ public class InstallationPhaseWorker { "loadReplaceSessionsKeys..."); String profileInstallationResult = apduTransmitter.transmitApdus(sbpp); - - if (profileInstallationResult.compareTo("9000") != 0) { - if (StringUtils.isNotBlank(profileInstallationResult) && profileInstallationResult.length() > 4) { - checkProfileInstallationResult(profileInstallationResult); - } else { - throw new RuntimeException("Unexpected response on loadReplaceSessionsKeys"); - } + if (StringUtils.isNotBlank(profileInstallationResult)) { + checkProfileInstallationResult(profileInstallationResult); } } }