forked from MarathonLabs/marathon
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use apkSigner instead of zip file contents to extract digest of apk (…
…to support all signing schemes)
- Loading branch information
Ivan Dyatlov
committed
Jun 24, 2024
1 parent
c77f13f
commit 714bc6a
Showing
3 changed files
with
16 additions
and
25 deletions.
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
38 changes: 13 additions & 25 deletions
38
...dor-android/base/src/main/kotlin/com/malinskiy/marathon/android/executor/ApkFileHasher.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 |
---|---|---|
@@ -1,40 +1,28 @@ | ||
package com.malinskiy.marathon.android | ||
|
||
import com.android.apksig.ApkVerifier | ||
import com.malinskiy.marathon.io.FileHasher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import java.nio.file.FileSystems | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.security.MessageDigest | ||
import java.util.HexFormat | ||
|
||
/** | ||
* Extracts digest of APK contents from signature file | ||
*/ | ||
class ApkFileHasher : FileHasher { | ||
|
||
override suspend fun getHash(file: File): String = withContext(Dispatchers.IO) { | ||
val zipFile = Paths.get(file.absolutePath) | ||
|
||
FileSystems.newFileSystem(zipFile, null as ClassLoader?) | ||
.use { fileSystem -> | ||
val certFile = fileSystem.getPath(SIGNATURE_FILE_PATH) | ||
Files.newInputStream(certFile) | ||
.use { | ||
it | ||
.bufferedReader() | ||
.lineSequence() | ||
.firstOrNull { line -> line.contains(DIGEST_MANIFEST_PROPERTY) } | ||
?.substringAfter(PROPERTY_DELIMITER) | ||
?.trim() | ||
?: throw IllegalArgumentException("Manifest digest not found") | ||
} | ||
} | ||
} | ||
val sha256digest = MessageDigest.getInstance("SHA-256") | ||
|
||
private companion object { | ||
private const val SIGNATURE_FILE_PATH = "META-INF/CERT.SF" | ||
private const val DIGEST_MANIFEST_PROPERTY = "-Digest-Manifest: " | ||
private const val PROPERTY_DELIMITER = ": " | ||
override suspend fun getHash(file: File): String = withContext(Dispatchers.IO) { | ||
val result = ApkVerifier.Builder(file).build().verify() | ||
if (result.signerCertificates.isNotEmpty()) { | ||
val certificate = result.signerCertificates.first() | ||
// https://cs.android.com/android/platform/superproject/main/+/main:tools/apksig/src/apksigner/java/com/android/apksigner/ApkSignerTool.java;l=1151;drc=d5137445c0d4067406cb3e38aade5507ff2fcd16 | ||
HexFormat.of().formatHex(sha256digest.digest(certificate.encoded)) | ||
} else { | ||
throw IllegalArgumentException("Certificate not found") | ||
} | ||
} | ||
} |