|
| 1 | +package com.aliucord.manager.patcher.steps.patch |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.os.Build |
| 5 | +import com.aliucord.manager.R |
| 6 | +import com.aliucord.manager.patcher.StepRunner |
| 7 | +import com.aliucord.manager.patcher.steps.StepGroup |
| 8 | +import com.aliucord.manager.patcher.steps.base.Step |
| 9 | +import com.aliucord.manager.patcher.steps.base.StepState |
| 10 | +import com.aliucord.manager.patcher.steps.download.CopyDependenciesStep |
| 11 | +import com.aliucord.manager.patcher.util.ArscUtil |
| 12 | +import com.aliucord.manager.patcher.util.ArscUtil.addResource |
| 13 | +import com.aliucord.manager.patcher.util.ArscUtil.getMainArscChunk |
| 14 | +import com.aliucord.manager.patcher.util.ArscUtil.getPackageChunk |
| 15 | +import com.aliucord.manager.patcher.util.ArscUtil.getResourceFileNames |
| 16 | +import com.aliucord.manager.patcher.util.AxmlUtil.getMainAxmlChunk |
| 17 | +import com.aliucord.manager.util.find |
| 18 | +import com.github.diamondminer88.zip.ZipReader |
| 19 | +import com.github.diamondminer88.zip.ZipWriter |
| 20 | +import com.google.devrel.gmscore.tools.apk.arsc.* |
| 21 | +import org.koin.core.component.KoinComponent |
| 22 | +import org.koin.core.component.inject |
| 23 | +import java.io.File |
| 24 | + |
| 25 | +/** |
| 26 | + * Adds a network security config that manually adds new CA root certificates. |
| 27 | + * This is useful for old Android devices that do not have updated root certs. |
| 28 | + */ |
| 29 | +class PatchCertsStep : Step(), KoinComponent { |
| 30 | + private val context: Application by inject() |
| 31 | + |
| 32 | + override val group = StepGroup.Patch |
| 33 | + override val localizedName = R.string.patch_step_patch_certs |
| 34 | + |
| 35 | + // Manager's (this application) network security config is used as a template |
| 36 | + // to inject into Aliucord, except with the resource ids pointing to certificate files changed |
| 37 | + // to new ones injected into the patched app's arsc |
| 38 | + override suspend fun execute(container: StepRunner) { |
| 39 | + val apk = container.getStep<CopyDependenciesStep>().patchedApk |
| 40 | + |
| 41 | + if (Build.VERSION.SDK_INT >= 26) { |
| 42 | + container.log("Modern device detected, skipping injecting root certs") |
| 43 | + state = StepState.Skipped |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + container.log("Parsing resources.arsc") |
| 48 | + val arsc = ArscUtil.readArsc(apk) |
| 49 | + val resourcesChunk = arsc.getMainArscChunk() |
| 50 | + val packageChunk = arsc.getPackageChunk() |
| 51 | + |
| 52 | + container.log("Creating new raw resources in arsc") |
| 53 | + val certificateIds = CERTIFICATES.keys.map { certificateName -> |
| 54 | + packageChunk.addResource( |
| 55 | + typeName = "raw", |
| 56 | + resourceName = certificateName, |
| 57 | + configurations = { it.isDefault }, |
| 58 | + valueType = BinaryResourceValue.Type.STRING, |
| 59 | + valueData = resourcesChunk.stringPool.addString("res/$certificateName.der"), |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + container.log("Generating new network security config AXML") |
| 64 | + val newNetworkSecurityConfigBytes = generateNetworkConfig(certificateIds) |
| 65 | + |
| 66 | + container.log("Parsing existing AndroidManifest.xml") |
| 67 | + val networkSecurityConfigId = getNetworkSecurityConfigResourceId(apk) |
| 68 | + val networkSecurityConfigPath = resourcesChunk.getResourceFileNames( |
| 69 | + resourceId = networkSecurityConfigId, |
| 70 | + configurations = { it.isDefault }, |
| 71 | + ).single() |
| 72 | + |
| 73 | + ZipWriter(apk, /* append = */ true).use { zip -> |
| 74 | + zip.deleteEntries(networkSecurityConfigPath, "resources.arsc") |
| 75 | + |
| 76 | + container.log("Writing new network security config AXML") |
| 77 | + zip.writeEntry(networkSecurityConfigPath, newNetworkSecurityConfigBytes) |
| 78 | + |
| 79 | + container.log("Writing new arsc") |
| 80 | + zip.writeEntry("resources.arsc", arsc.toByteArray()) |
| 81 | + |
| 82 | + for ((name, id) in CERTIFICATES) { |
| 83 | + container.log("Writing $name CA certificate to apk") |
| 84 | + |
| 85 | + val bytes = context.resources.openRawResource(id).use { it.readBytes() } |
| 86 | + zip.writeEntry("res/$name.der", bytes) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * From an APK, read the manifest's `android:networkSecurityConfig` references to a resource. |
| 93 | + * This is then used to get the filename of the resource from `resources.arsc`. |
| 94 | + */ |
| 95 | + fun getNetworkSecurityConfigResourceId(apk: File): BinaryResourceIdentifier { |
| 96 | + val manifestBytes = ZipReader(apk).use { |
| 97 | + it.openEntry("AndroidManifest.xml")?.read() |
| 98 | + } ?: error("APK missing manifest") |
| 99 | + val manifest = BinaryResourceFile(manifestBytes) |
| 100 | + val mainChunk = manifest.getMainAxmlChunk() |
| 101 | + |
| 102 | + // Prefetch string indexes to avoid parsing the entire string pool |
| 103 | + val networkSecurityConfigStringIdx = mainChunk.stringPool.indexOf("networkSecurityConfig") |
| 104 | + val applicationStringIdx = mainChunk.stringPool.indexOf("application") |
| 105 | + |
| 106 | + val applicationChunk = mainChunk.chunks |
| 107 | + .find { it is XmlStartElementChunk && it.nameIndex == applicationStringIdx } as? XmlStartElementChunk |
| 108 | + ?: error("Unable to find <application> in manifest") |
| 109 | + val networkSecurityConfig = applicationChunk.attributes |
| 110 | + .find { it.nameIndex() == networkSecurityConfigStringIdx } |
| 111 | + ?: error("Unable to find android:networkSecurityConfig in manifest") |
| 112 | + |
| 113 | + assert(networkSecurityConfig.typedValue().type() == BinaryResourceValue.Type.REFERENCE) |
| 114 | + |
| 115 | + return BinaryResourceIdentifier.create(networkSecurityConfig.typedValue().data()) |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * This generates a binary AXML representation a network security config similar to the one |
| 120 | + * manager uses [R.xml.network_security_config], except with resource IDs generated for the patched APK. |
| 121 | + */ |
| 122 | + private fun generateNetworkConfig(certificateIds: List<BinaryResourceIdentifier>): ByteArray { |
| 123 | + val axml = BinaryResourceFile(byteArrayOf()) |
| 124 | + val xmlChunk = XmlChunk(null) |
| 125 | + val strings = StringPoolChunk(xmlChunk) |
| 126 | + |
| 127 | + axml.appendChunk(xmlChunk) |
| 128 | + xmlChunk.appendChunk(strings) |
| 129 | + xmlChunk.appendChunk(XmlResourceMapChunk(intArrayOf(), xmlChunk)) |
| 130 | + |
| 131 | + // Nested chunks |
| 132 | + // @formatter:off |
| 133 | + val chunkNames = arrayOf("network-security-config", "base-config", "trust-anchors") |
| 134 | + for (chunkName in chunkNames) { |
| 135 | + xmlChunk.appendChunk(XmlStartElementChunk( |
| 136 | + /* namespaceIndex = */ -1, |
| 137 | + /* nameIndex = */ strings.addString(chunkName), |
| 138 | + /* idIndex = */ -1, |
| 139 | + /* classIndex = */ -1, |
| 140 | + /* styleIndex = */ -1, |
| 141 | + /* attributes = */ emptyList(), |
| 142 | + /* parent = */ xmlChunk, |
| 143 | + )) |
| 144 | + } |
| 145 | + |
| 146 | + // Allow "system" certificates |
| 147 | + run { |
| 148 | + val certificateChunk = XmlStartElementChunk( |
| 149 | + /* namespaceIndex = */ -1, |
| 150 | + /* nameIndex = */ strings.addString("certificates", /* deduplicate = */ true), |
| 151 | + /* idIndex = */ -1, |
| 152 | + /* classIndex = */ -1, |
| 153 | + /* styleIndex = */ -1, |
| 154 | + /* attributes = */ listOf(), |
| 155 | + /* parent = */ xmlChunk, |
| 156 | + ) |
| 157 | + certificateChunk.attributes += XmlAttribute( |
| 158 | + /* namespaceIndex = */ -1, |
| 159 | + /* nameIndex = */ xmlChunk.stringPool.addString("src", /* deduplicate = */ true), |
| 160 | + /* rawValueIndex = */ xmlChunk.stringPool.addString("system"), |
| 161 | + /* typedValue = */ BinaryResourceValue( |
| 162 | + /* type = */ BinaryResourceValue.Type.STRING, |
| 163 | + /* data = */ xmlChunk.stringPool.addString("system", /* deduplicate = */ true), |
| 164 | + ), |
| 165 | + /* parent = */ certificateChunk, |
| 166 | + ) |
| 167 | + |
| 168 | + xmlChunk.appendChunk(certificateChunk) |
| 169 | + xmlChunk.appendChunk(XmlEndElementChunk( |
| 170 | + /* namespaceIndex = */ -1, |
| 171 | + /* nameIndex = */ strings.addString("certificates", /* deduplicate = */ true), |
| 172 | + /* parent = */ xmlChunk, |
| 173 | + )) |
| 174 | + } |
| 175 | + |
| 176 | + // Add custom certificate references |
| 177 | + for (certificateId in certificateIds) { |
| 178 | + val certificateChunk = XmlStartElementChunk( |
| 179 | + /* namespaceIndex = */ -1, |
| 180 | + /* nameIndex = */ strings.addString("certificates", /* deduplicate = */ true), |
| 181 | + /* idIndex = */ -1, |
| 182 | + /* classIndex = */ -1, |
| 183 | + /* styleIndex = */ -1, |
| 184 | + /* attributes = */ listOf(), |
| 185 | + /* parent = */ xmlChunk, |
| 186 | + ) |
| 187 | + certificateChunk.attributes += XmlAttribute( |
| 188 | + /* namespaceIndex = */ -1, |
| 189 | + /* nameIndex = */ xmlChunk.stringPool.addString("src", /* deduplicate = */ true), |
| 190 | + /* rawValueIndex = */ -1, |
| 191 | + /* typedValue = */ BinaryResourceValue( |
| 192 | + /* type = */ BinaryResourceValue.Type.REFERENCE, |
| 193 | + /* data = */ certificateId.resourceId(), |
| 194 | + ), |
| 195 | + /* parent = */ certificateChunk, |
| 196 | + ) |
| 197 | + |
| 198 | + xmlChunk.appendChunk(certificateChunk) |
| 199 | + xmlChunk.appendChunk(XmlEndElementChunk( |
| 200 | + /* namespaceIndex = */ -1, |
| 201 | + /* nameIndex = */ strings.addString("certificates", /* deduplicate = */ true), |
| 202 | + /* parent = */ xmlChunk, |
| 203 | + )) |
| 204 | + } |
| 205 | + |
| 206 | + // Reverse nested chunks |
| 207 | + for (chunkName in chunkNames.reversed()) { |
| 208 | + xmlChunk.appendChunk(XmlEndElementChunk( |
| 209 | + /* namespaceIndex = */ -1, |
| 210 | + /* nameIndex = */ strings.addString(chunkName, /* deduplicate = */ true), |
| 211 | + /* parent = */ xmlChunk, |
| 212 | + )) |
| 213 | + } |
| 214 | + // @formatter:on |
| 215 | + |
| 216 | + return axml.toByteArray() |
| 217 | + } |
| 218 | + |
| 219 | + private companion object { |
| 220 | + val CERTIFICATES = mapOf( |
| 221 | + "globalsign_root_r4" to R.raw.globalsign_root_r4, |
| 222 | + "gts_root_r1" to R.raw.gts_root_r1, |
| 223 | + "gts_root_r2" to R.raw.gts_root_r2, |
| 224 | + "gts_root_r3" to R.raw.gts_root_r3, |
| 225 | + "gts_root_r4" to R.raw.gts_root_r4, |
| 226 | + "isrg_root_x1" to R.raw.isrg_root_x1, |
| 227 | + "isrg_root_x2" to R.raw.isrg_root_x2, |
| 228 | + ) |
| 229 | + } |
| 230 | +} |
0 commit comments