Skip to content

Commit 2d70c01

Browse files
authored
fix: Use same default keystore values as Morphe Manager (#96)
1 parent 7ae4baa commit 2d70c01

2 files changed

Lines changed: 84 additions & 26 deletions

File tree

src/main/kotlin/app/morphe/cli/command/PatchCommand.kt

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@ import app.morphe.cli.command.model.mergeWith
1919
import app.morphe.cli.command.model.toPatchBundle
2020
import app.morphe.cli.command.model.toSerializablePatch
2121
import app.morphe.cli.command.model.withUpdatedBundle
22+
import app.morphe.engine.PatchEngine
23+
import app.morphe.engine.PatchEngine.Config.Companion.DEFAULT_KEYSTORE_ALIAS
24+
import app.morphe.engine.PatchEngine.Config.Companion.DEFAULT_KEYSTORE_PASSWORD
25+
import app.morphe.engine.PatchEngine.Config.Companion.LEGACY_KEYSTORE_ALIAS
26+
import app.morphe.engine.PatchEngine.Config.Companion.LEGACY_KEYSTORE_PASSWORD
2227
import app.morphe.engine.UpdateChecker
23-
import app.morphe.patcher.apk.ApkUtils
24-
import app.morphe.patcher.apk.ApkUtils.applyTo
25-
import app.morphe.library.installation.installer.*
28+
import app.morphe.library.installation.installer.AdbInstaller
29+
import app.morphe.library.installation.installer.AdbInstallerResult
30+
import app.morphe.library.installation.installer.AdbRootInstaller
31+
import app.morphe.library.installation.installer.DeviceNotFoundException
32+
import app.morphe.library.installation.installer.Installer
33+
import app.morphe.library.installation.installer.RootInstallerResult
2634
import app.morphe.patcher.Patcher
2735
import app.morphe.patcher.PatcherConfig
2836
import app.morphe.patcher.apk.ApkMerger
37+
import app.morphe.patcher.apk.ApkUtils
38+
import app.morphe.patcher.apk.ApkUtils.applyTo
2939
import app.morphe.patcher.logging.toMorpheLogger
3040
import app.morphe.patcher.patch.Patch
3141
import app.morphe.patcher.patch.loadPatchesFromJar
@@ -196,13 +206,13 @@ internal object PatchCommand : Callable<Int> {
196206
description = ["Alias of the private key and certificate pair keystore entry."],
197207
showDefaultValue = ALWAYS,
198208
)
199-
private var keyStoreEntryAlias = "Morphe Key"
209+
private var keyStoreEntryAlias = PatchEngine.Config.DEFAULT_KEYSTORE_ALIAS
200210

201211
@CommandLine.Option(
202212
names = ["--keystore-entry-password"],
203213
description = ["Password of the keystore entry."],
204214
)
205-
private var keyStoreEntryPassword = "" // Empty password by default
215+
private var keyStoreEntryPassword = PatchEngine.Config.DEFAULT_KEYSTORE_PASSWORD
206216

207217
@CommandLine.Option(
208218
names = ["--signer"],
@@ -666,17 +676,34 @@ internal object PatchCommand : Callable<Int> {
666676
patchingResult.addStepResult(
667677
PatchingStep.SIGNING,
668678
{
669-
ApkUtils.signApk(
670-
patchedApkFile,
671-
outputFilePath,
672-
signer,
673-
ApkUtils.KeyStoreDetails(
674-
keystoreFilePath,
675-
keyStorePassword,
676-
keyStoreEntryAlias,
677-
keyStoreEntryPassword,
678-
),
679-
)
679+
fun signApk(alias: String, password: String) {
680+
ApkUtils.signApk(
681+
patchedApkFile,
682+
outputFilePath,
683+
signer,
684+
ApkUtils.KeyStoreDetails(
685+
keystoreFilePath,
686+
keyStorePassword,
687+
alias,
688+
password,
689+
)
690+
)
691+
}
692+
try {
693+
signApk(keyStoreEntryAlias, keyStoreEntryPassword)
694+
} catch (e: Exception){
695+
// Retry with legacy keystore defaults.
696+
if (keyStoreEntryAlias == DEFAULT_KEYSTORE_ALIAS &&
697+
keyStoreEntryPassword == DEFAULT_KEYSTORE_PASSWORD &&
698+
keystoreFilePath.exists()
699+
) {
700+
logger.info("Using legacy keystore credentials")
701+
702+
signApk(LEGACY_KEYSTORE_ALIAS, LEGACY_KEYSTORE_PASSWORD)
703+
} else {
704+
throw e
705+
}
706+
}
680707
}
681708
)
682709
} else {

src/main/kotlin/app/morphe/engine/PatchEngine.kt

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import java.io.StringWriter
2727
import java.nio.file.Files
2828
import java.util.logging.Logger
2929

30-
/**
30+
/*
3131
* Single patching pipeline shared by CLI and GUI. (Eventually. Right now we are still having 2 pipelines)
3232
*/
33+
34+
3335
object PatchEngine {
3436

3537
enum class PatchStep {
@@ -54,7 +56,14 @@ object PatchEngine {
5456
val aaptBinaryPath: File? = null,
5557
val tempDir: File? = null,
5658
val failOnError: Boolean = true,
57-
)
59+
) {
60+
companion object {
61+
internal const val DEFAULT_KEYSTORE_ALIAS = "Morphe"
62+
internal const val DEFAULT_KEYSTORE_PASSWORD = "Morphe"
63+
internal const val LEGACY_KEYSTORE_ALIAS = "Morphe Key"
64+
internal const val LEGACY_KEYSTORE_PASSWORD = ""
65+
}
66+
}
5867

5968
data class Result(
6069
val success: Boolean,
@@ -215,18 +224,40 @@ object PatchEngine {
215224
if (!config.unsigned) {
216225
onProgress("Signing APK...")
217226
try {
227+
fun signApk(details: ApkUtils.KeyStoreDetails) {
228+
ApkUtils.signApk(
229+
rebuiltApk,
230+
tempOutput,
231+
config.signerName,
232+
details,
233+
)
234+
}
235+
218236
val keystoreDetails = config.keystoreDetails ?: ApkUtils.KeyStoreDetails(
219237
File(tempDir, "morphe.keystore"),
220238
null,
221-
"Morphe Key",
222-
"",
223-
)
224-
ApkUtils.signApk(
225-
rebuiltApk,
226-
tempOutput,
227-
config.signerName,
228-
keystoreDetails,
239+
Config.DEFAULT_KEYSTORE_ALIAS,
240+
Config.DEFAULT_KEYSTORE_PASSWORD,
229241
)
242+
243+
try {
244+
signApk(keystoreDetails)
245+
} catch (e: Exception){
246+
// Retry with legacy keystore defaults.
247+
if (config.keystoreDetails == null && keystoreDetails.keyStore.exists()) {
248+
logger.info("Using legacy keystore credentials")
249+
250+
val legacyKeystoreDetails = ApkUtils.KeyStoreDetails(
251+
keystoreDetails.keyStore,
252+
null,
253+
Config.LEGACY_KEYSTORE_ALIAS,
254+
Config.LEGACY_KEYSTORE_PASSWORD,
255+
)
256+
signApk(legacyKeystoreDetails)
257+
} else {
258+
throw e
259+
}
260+
}
230261
stepResults.add(StepResult(PatchStep.SIGNING, true))
231262
} catch (e: Exception) {
232263
stepResults.add(StepResult(PatchStep.SIGNING, false, e.toString()))

0 commit comments

Comments
 (0)