-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: android 5 support #74
Draft
rushiiMachine
wants to merge
1
commit into
main
Choose a base branch
from
feat/android-5-new
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/main/kotlin/com/aliucord/manager/patcher/signing/ApkSigSigner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.aliucord.manager.patcher.signing | ||
|
||
import com.aliucord.manager.manager.PathManager | ||
import com.android.apksig.ApkSigner | ||
import com.android.apksig.KeyConfig | ||
import org.bouncycastle.asn1.x500.X500Name | ||
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo | ||
import org.bouncycastle.cert.X509v3CertificateBuilder | ||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter | ||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.get | ||
import java.io.File | ||
import java.math.BigInteger | ||
import java.security.KeyPairGenerator | ||
import java.security.KeyStore | ||
import java.security.PrivateKey | ||
import java.security.SecureRandom | ||
import java.security.cert.Certificate | ||
import java.security.cert.X509Certificate | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
object ApkSigSigner : KoinComponent { | ||
private val paths = get<PathManager>() | ||
private val password = "password".toCharArray() | ||
|
||
private val signerConfig: ApkSigner.SignerConfig by lazy { | ||
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType()) | ||
|
||
// Create new keystore if it doesn't exist | ||
if (!paths.keystoreFile.exists()) { | ||
paths.aliucordDir.mkdirs() | ||
newKeystore(paths.keystoreFile) | ||
} | ||
|
||
paths.keystoreFile.inputStream() | ||
.use { keyStore.load(it, /* password = */ null) } | ||
|
||
val alias = keyStore.aliases().nextElement() | ||
val certificate = keyStore.getCertificate(alias) as X509Certificate | ||
|
||
ApkSigner.SignerConfig.Builder( | ||
"Aliucord Manager signer", | ||
KeyConfig.Jca(keyStore.getKey(alias, password) as PrivateKey), | ||
listOf(certificate) | ||
).build() | ||
} | ||
|
||
private fun newKeystore(out: File) { | ||
val key = createKey() | ||
|
||
with(KeyStore.getInstance(KeyStore.getDefaultType())) { | ||
load(null, password) | ||
setKeyEntry("alias", key.privateKey, password, arrayOf<Certificate>(key.publicKey)) | ||
store(out.outputStream(), password) | ||
} | ||
} | ||
|
||
private fun createKey(): KeySet { | ||
var serialNumber: BigInteger | ||
|
||
do serialNumber = SecureRandom().nextInt().toBigInteger() | ||
while (serialNumber < BigInteger.ZERO) | ||
|
||
val x500Name = X500Name("CN=Aliucord Manager") | ||
val pair = KeyPairGenerator.getInstance("RSA").run { | ||
initialize(2048) | ||
generateKeyPair() | ||
} | ||
val builder = X509v3CertificateBuilder( | ||
/* issuer = */ x500Name, | ||
/* serial = */ serialNumber, | ||
/* notBefore = */ Date(0), | ||
/* notAfter = */ Date(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * 366L * 30L), // 30 years | ||
/* dateLocale = */ Locale.ENGLISH, | ||
/* subject = */ x500Name, | ||
/* publicKeyInfo = */ SubjectPublicKeyInfo.getInstance(pair.public.encoded) | ||
) | ||
val signer = JcaContentSignerBuilder("SHA1withRSA").build(pair.private) | ||
|
||
return KeySet(JcaX509CertificateConverter().getCertificate(builder.build(signer)), pair.private) | ||
} | ||
|
||
fun signApk(apkFile: File) { | ||
val tmpApk = apkFile.resolveSibling(apkFile.name + ".tmp") | ||
|
||
ApkSigner.Builder(listOf(signerConfig)) | ||
.setV1SigningEnabled(false) // TODO: enable so api <24 devices can work, however zip-alignment breaks | ||
.setV2SigningEnabled(true) | ||
.setV3SigningEnabled(true) | ||
.setInputApk(apkFile) | ||
.setOutputApk(tmpApk) | ||
.build() | ||
.sign() | ||
|
||
tmpApk.renameTo(apkFile) | ||
} | ||
|
||
private class KeySet(val publicKey: X509Certificate, val privateKey: PrivateKey) | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/kotlin/com/aliucord/manager/patcher/signing/KeySet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.aliucord.manager.patcher.signing | ||
|
||
import java.security.PrivateKey | ||
import java.security.cert.X509Certificate | ||
|
||
data class KeySet( | ||
val publicKey: X509Certificate, | ||
val privateKey: PrivateKey, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to downgrade Java version
Even
VERSION_17
can be used with minSdk 21kotlinOptions.jvmTarget
is too