fix: Migration of keystore, by fixing mislabeling of alias as cn (#2769)

This commit is contained in:
Ushie
2025-10-03 16:31:05 +03:00
committed by GitHub
parent 4016ae732c
commit aeab639b2b
5 changed files with 20 additions and 20 deletions

View File

@@ -27,25 +27,25 @@ class KeystoreManager(app: Application, private val prefs: PreferencesManager) {
private val keystorePath =
app.getDir("signing", Context.MODE_PRIVATE).resolve("manager.keystore")
private suspend fun updatePrefs(cn: String, pass: String) = prefs.edit {
prefs.keystoreCommonName.value = cn
private suspend fun updatePrefs(alias: String, pass: String) = prefs.edit {
prefs.keystoreAlias.value = alias
prefs.keystorePass.value = pass
}
private suspend fun signingDetails(path: File = keystorePath) = ApkUtils.KeyStoreDetails(
keyStore = path,
keyStorePassword = null,
alias = prefs.keystoreCommonName.get(),
alias = prefs.keystoreAlias.get(),
password = prefs.keystorePass.get()
)
suspend fun sign(input: File, output: File) = withContext(Dispatchers.Default) {
ApkUtils.signApk(input, output, prefs.keystoreCommonName.get(), signingDetails())
ApkUtils.signApk(input, output, prefs.keystoreAlias.get(), signingDetails())
}
suspend fun regenerate() = withContext(Dispatchers.Default) {
val keyCertPair = ApkSigner.newPrivateKeyCertificatePair(
prefs.keystoreCommonName.get(),
prefs.keystoreAlias.get(),
eightYearsFromNow
)
val ks = ApkSigner.newKeyStore(
@@ -64,13 +64,13 @@ class KeystoreManager(app: Application, private val prefs: PreferencesManager) {
updatePrefs(DEFAULT, DEFAULT)
}
suspend fun import(cn: String, pass: String, keystore: InputStream): Boolean {
suspend fun import(alias: String, pass: String, keystore: InputStream): Boolean {
val keystoreData = withContext(Dispatchers.IO) { keystore.readBytes() }
try {
val ks = ApkSigner.readKeyStore(ByteArrayInputStream(keystoreData), null)
ApkSigner.readPrivateKeyCertificatePair(ks, cn, pass)
ApkSigner.readPrivateKeyCertificatePair(ks, alias, pass)
} catch (_: UnrecoverableKeyException) {
return false
} catch (_: IllegalArgumentException) {
@@ -81,7 +81,7 @@ class KeystoreManager(app: Application, private val prefs: PreferencesManager) {
Files.write(keystorePath.toPath(), keystoreData)
}
updatePrefs(cn, pass)
updatePrefs(alias, pass)
return true
}

View File

@@ -16,7 +16,7 @@ class PreferencesManager(
val useProcessRuntime = booleanPreference("use_process_runtime", false)
val patcherProcessMemoryLimit = intPreference("process_runtime_memory_limit", 700)
val keystoreCommonName = stringPreference("keystore_cn", KeystoreManager.DEFAULT)
val keystoreAlias = stringPreference("keystore_alias", KeystoreManager.DEFAULT)
val keystorePass = stringPreference("keystore_pass", KeystoreManager.DEFAULT)
val firstLaunch = booleanPreference("first_launch", true)

View File

@@ -104,10 +104,10 @@ fun ImportExportSettingsScreen(
if (vm.showCredentialsDialog) {
KeystoreCredentialsDialog(
onDismissRequest = vm::cancelKeystoreImport,
onSubmit = { cn, pass ->
onSubmit = { alias, pass ->
vm.viewModelScope.launch {
uiSafe(context, R.string.failed_to_import_keystore, "Failed to import keystore") {
val result = vm.tryKeystoreImport(cn, pass)
val result = vm.tryKeystoreImport(alias, pass)
if (!result) context.toast(context.getString(R.string.import_keystore_wrong_credentials))
}
}
@@ -382,7 +382,7 @@ fun KeystoreCredentialsDialog(
onDismissRequest: () -> Unit,
onSubmit: (String, String) -> Unit
) {
var cn by rememberSaveable { mutableStateOf("") }
var alias by rememberSaveable { mutableStateOf("") }
var pass by rememberSaveable { mutableStateOf("") }
AlertDialog(
@@ -390,7 +390,7 @@ fun KeystoreCredentialsDialog(
confirmButton = {
TextButton(
onClick = {
onSubmit(cn, pass)
onSubmit(alias, pass)
}
) {
Text(stringResource(R.string.import_keystore_dialog_button))
@@ -422,8 +422,8 @@ fun KeystoreCredentialsDialog(
color = MaterialTheme.colorScheme.onSurfaceVariant
)
OutlinedTextField(
value = cn,
onValueChange = { cn = it },
value = alias,
onValueChange = { alias = it },
label = { Text(stringResource(R.string.import_keystore_dialog_alias_field)) }
)
PasswordField(

View File

@@ -154,12 +154,12 @@ class ImportExportViewModel(
keystoreImportPath = null
}
suspend fun tryKeystoreImport(cn: String, pass: String) =
tryKeystoreImport(cn, pass, keystoreImportPath!!)
suspend fun tryKeystoreImport(alias: String, pass: String) =
tryKeystoreImport(alias, pass, keystoreImportPath!!)
private suspend fun tryKeystoreImport(cn: String, pass: String, path: Path): Boolean {
private suspend fun tryKeystoreImport(alias: String, pass: String, path: Path): Boolean {
path.inputStream().use { stream ->
if (keystoreManager.import(cn, pass, stream)) {
if (keystoreManager.import(alias, pass, stream)) {
app.toast(app.getString(R.string.import_keystore_success))
cancelKeystoreImport()
return true

View File

@@ -143,7 +143,7 @@ class MainViewModel(
settings.keystore?.let { keystore ->
val keystoreBytes = Base64.decode(keystore, Base64.DEFAULT)
keystoreManager.import(
"ReVanced",
"alias",
settings.keystorePassword,
keystoreBytes.inputStream()
)