From 7d9df7c90d6c1e5d77a997a45af52d9b0c161ad3 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Wed, 18 May 2022 22:11:05 -0400 Subject: [PATCH] Convert DownloadProfileWorker to Kotlin --- .../lpa/impl/DownloadProfileWorker.java | 78 ------------- .../lpa/impl/DownloadProfileWorker.kt | 105 ++++++++++++++++++ 2 files changed, 105 insertions(+), 78 deletions(-) delete mode 100644 libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.java create mode 100644 libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.kt diff --git a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.java b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.java deleted file mode 100644 index 2c1d559..0000000 --- a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.truphone.lpa.impl; - -import com.truphone.es9plus.Es9PlusImpl; -import com.truphone.lpa.ApduChannel; -import com.truphone.lpa.impl.download.*; -import com.truphone.lpa.progress.DownloadProgress; -import com.truphone.util.LogStub; - -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -class DownloadProfileWorker { - private static final Logger LOG = Logger.getLogger(DownloadProfileWorker.class.getName()); - - private final DownloadProgress progress; - private final Es9PlusImpl es9Module; - private String matchingId; - private final String imei; - private ApduTransmitter apduTransmitter; - - DownloadProfileWorker(String matchingId, String imei, DownloadProgress progress, ApduChannel apduChannel, Es9PlusImpl es9Module) { - - this.matchingId = matchingId; - this.imei = imei; - this.progress = progress; - this.es9Module = es9Module; - apduTransmitter = new ApduTransmitter(apduChannel); - } - - void run() throws Exception { - AuthenticatingPhaseWorker authenticatingPhaseWorker = new AuthenticatingPhaseWorker(progress, apduTransmitter, es9Module); - DownloadPhaseWorker downloadPhaseWorker = new DownloadPhaseWorker(progress, apduTransmitter, es9Module); - - LOG.info(LogStub.getInstance().getTag() + " - Downloading profile with matching Id: " + matchingId); - - - //AP Added this to support Activation Codes - //If matchingId is an Activation Code, parses AC to retrieve DP Address and Matching ID. - //Otherwise LPA shall use the default SMDP configured on the cards - - String serverAddress; - if(matchingId.contains("$")){ - //Its activation code - String[] acParts = matchingId.split("\\$", -1); - if(acParts.length<3 ) - throw new RuntimeException("Invalid ActivationCode format"); - - serverAddress = acParts[1]; - matchingId = acParts[2]; - }else - { - serverAddress = new ConnectingPhaseWorker(progress, apduTransmitter).getEuiccConfiguredAddress(matchingId); - - } - - - InitialAuthenticationKeys initialAuthenticationKeys = new InitialAuthenticationKeys(matchingId, - serverAddress, - authenticatingPhaseWorker.getEuiccInfo(), - authenticatingPhaseWorker.getEuiccChallenge(matchingId)); - - authenticatingPhaseWorker.initiateAuthentication(initialAuthenticationKeys, matchingId, imei); - downloadAndInstallProfilePackage(initialAuthenticationKeys, - downloadPhaseWorker.prepareDownload(authenticatingPhaseWorker.authenticateClient(initialAuthenticationKeys, - authenticatingPhaseWorker.authenticateWithEuicc(initialAuthenticationKeys))), downloadPhaseWorker); - } - - - private void downloadAndInstallProfilePackage(InitialAuthenticationKeys initialAuthenticationKeys, - String encodedPrepareDownloadResponse, DownloadPhaseWorker downloadPhaseWorker) throws IOException { - String bpp = downloadPhaseWorker.getBoundProfilePackage(initialAuthenticationKeys, encodedPrepareDownloadResponse); - Map> sbpp = new GeneratePhaseWorker(progress).generateSbpp(bpp); - - new InstallationPhaseWorker(progress, apduTransmitter).loadingSbppApdu(sbpp); - } -} diff --git a/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.kt b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.kt new file mode 100644 index 0000000..20f303f --- /dev/null +++ b/libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DownloadProfileWorker.kt @@ -0,0 +1,105 @@ +/* + * Copyright 2022 Peter Cai & Pierre-Hugues Husson + * + * This program is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +package com.truphone.lpa.impl + +import com.truphone.lpa.progress.DownloadProgress +import com.truphone.lpa.ApduChannel +import com.truphone.es9plus.Es9PlusImpl +import com.truphone.lpa.impl.download.ApduTransmitter +import com.truphone.lpa.impl.download.AuthenticatingPhaseWorker +import com.truphone.lpa.impl.download.DownloadPhaseWorker +import com.truphone.util.LogStub +import com.truphone.lpa.impl.download.ConnectingPhaseWorker +import com.truphone.lpa.impl.download.GeneratePhaseWorker +import com.truphone.lpa.impl.download.InstallationPhaseWorker +import java.lang.RuntimeException +import java.util.logging.Logger + +internal class DownloadProfileWorker( + private var matchingId: String, + private val imei: String, + private val progress: DownloadProgress, + apduChannel: ApduChannel?, + private val es9Module: Es9PlusImpl +) { + private val apduTransmitter = ApduTransmitter(apduChannel) + + companion object { + private val LOG = Logger.getLogger( + DownloadProfileWorker::class.java.name + ) + } + + fun run() { + val authenticatingPhaseWorker = AuthenticatingPhaseWorker( + progress, apduTransmitter, es9Module + ) + val downloadPhaseWorker = DownloadPhaseWorker(progress, apduTransmitter, es9Module) + LOG.info(LogStub.getInstance().tag + " - Downloading profile with matching Id: " + matchingId) + + + // AP Added this to support Activation Codes + // If matchingId is an Activation Code, parses AC to retrieve DP Address and Matching ID. + // Otherwise LPA shall use the default SMDP configured on the cards + val serverAddress: String + if (matchingId.contains("$")) { + // Its activation code + val acParts = matchingId.split("$").toTypedArray() + if (acParts.size < 3) throw RuntimeException("Invalid ActivationCode format") + serverAddress = acParts[1] + matchingId = acParts[2] + } else { + serverAddress = + ConnectingPhaseWorker(progress, apduTransmitter).getEuiccConfiguredAddress( + matchingId + ) + } + val initialAuthenticationKeys = InitialAuthenticationKeys( + matchingId, + serverAddress, + authenticatingPhaseWorker.euiccInfo, + authenticatingPhaseWorker.getEuiccChallenge(matchingId) + ) + authenticatingPhaseWorker.initiateAuthentication( + initialAuthenticationKeys, + matchingId, + imei + ) + downloadAndInstallProfilePackage( + initialAuthenticationKeys, + downloadPhaseWorker.prepareDownload( + authenticatingPhaseWorker.authenticateClient( + initialAuthenticationKeys, + authenticatingPhaseWorker.authenticateWithEuicc(initialAuthenticationKeys) + ) + ), downloadPhaseWorker + ) + } + + private fun downloadAndInstallProfilePackage( + initialAuthenticationKeys: InitialAuthenticationKeys, + encodedPrepareDownloadResponse: String, + downloadPhaseWorker: DownloadPhaseWorker + ) { + val bpp = downloadPhaseWorker.getBoundProfilePackage( + initialAuthenticationKeys, + encodedPrepareDownloadResponse + ) + val sbpp = GeneratePhaseWorker( + progress + ).generateSbpp(bpp) + InstallationPhaseWorker(progress, apduTransmitter).loadingSbppApdu(sbpp) + } +} \ No newline at end of file