Fix: Move keystore back to app storage

This commit is contained in:
wingio 2023-03-30 13:30:52 -04:00
parent f6855e6f5b
commit 014bbfcc10
2 changed files with 33 additions and 27 deletions

View file

@ -14,8 +14,8 @@ android {
applicationId = "dev.beefers.vendetta.manager" applicationId = "dev.beefers.vendetta.manager"
minSdk = 24 minSdk = 24
targetSdk = 33 targetSdk = 33
versionCode = 1060 versionCode = 1061
versionName = "1.0.6" versionName = "1.0.61"
buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"") buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"")
buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"") buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"")

View file

@ -12,6 +12,7 @@ import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
import java.io.File import java.io.File
import java.math.BigInteger import java.math.BigInteger
import java.nio.file.Path
import java.security.KeyPairGenerator import java.security.KeyPairGenerator
import java.security.KeyStore import java.security.KeyStore
import java.security.PrivateKey import java.security.PrivateKey
@ -20,39 +21,32 @@ import java.security.cert.Certificate
import java.security.cert.X509Certificate import java.security.cert.X509Certificate
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import kotlin.io.path.Path
import kotlin.io.path.moveTo
object Signer : KoinComponent { object Signer : KoinComponent {
private val password = "password".toCharArray() private val password = "password".toCharArray()
private val cacheDir = inject<Context>().value.cacheDir private val context by inject<Context>()
private val cacheDir = context.cacheDir
private val filesDir = context.filesDir
val keyStore: File val keyStore: File by lazy {
get() { val ks = filesDir.resolve("ks.keystore")
lateinit var ks: File migrate(cacheDir, filesDir)
cacheDir.resolve("ks.keystore").also { migrate(Constants.VENDETTA_DIR, filesDir)
if (it.exists()) { ks.also {
it.copyTo(Constants.VENDETTA_DIR.resolve("ks.keystore"), true)
it.delete()
}
}
Constants.VENDETTA_DIR.resolve("ks.keystore").also {
if (!it.exists()) { if (!it.exists()) {
Constants.VENDETTA_DIR.mkdir() it.createNewFile()
newKeystore(it) newKeystore(it)
} }
ks = it
} }
return ks ks
} }
private val signerConfig: ApkSigner.SignerConfig by lazy { private val signerConfig: ApkSigner.SignerConfig by lazy {
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType()) val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
cacheDir.resolve("ks.keystore").also { this.keyStore.inputStream().use { stream ->
if (!it.exists()) {
cacheDir.mkdir()
newKeystore(it)
}
}.inputStream().use { stream ->
keyStore.load(stream, null) keyStore.load(stream, null)
} }
@ -66,13 +60,13 @@ object Signer : KoinComponent {
).build() ).build()
} }
private fun newKeystore(out: File?) { private fun newKeystore(out: File) {
val key = createKey() val key = createKey()
with(KeyStore.getInstance(KeyStore.getDefaultType())) { with(KeyStore.getInstance(KeyStore.getDefaultType())) {
load(null, password) load(null, password)
setKeyEntry("alias", key.privateKey, password, arrayOf<Certificate>(key.publicKey)) setKeyEntry("alias", key.privateKey, password, arrayOf<Certificate>(key.publicKey))
store(out?.outputStream(), password) store(out.outputStream(), password)
} }
} }
@ -125,5 +119,17 @@ object Signer : KoinComponent {
outputApk.renameTo(apkFile) outputApk.renameTo(apkFile)
} }
private fun migrate(oldDir: File, newDir: File) {
oldDir.resolve("ks.keystore").also {
if (it.exists()) {
Path(it.absolutePath)
.moveTo(
Path(newDir.resolve("ks.keystore").absolutePath),
overwrite = true
)
}
}
}
private class KeySet(val publicKey: X509Certificate, val privateKey: PrivateKey) private class KeySet(val publicKey: X509Certificate, val privateKey: PrivateKey)
} }