Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

Pin the release signing certificate

chethan6168995f32d1415
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 66903e4..1cabd17 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -126,6 +126,8 @@ jobs:
- name: APK
run: |
apksigner="$(ls -d "$ANDROID_HOME"/build-tools/*/ | sort -V | tail -n 1)apksigner"
+ # the certificate every earlier release was signed with: an update from any other key can't install
+ expected="$(sed -n 's/^artefact.releaseCertSha256=//p' android/gradle.properties | tr -d ':[:space:]' | tr 'A-F' 'a-f')"
echo >>"$GITHUB_STEP_SUMMARY"
for apk in dist/*.apk; do
certs="$("$apksigner" verify --print-certs "$apk")"
@@ -133,6 +135,11 @@ jobs:
echo "::error::$apk is signed with the debug key"
exit 1
fi
+ actual="$(sed -n 's/.*certificate SHA-256 digest: //p' <<<"$certs" | head -n 1)"
+ if [ "$GITHUB_REF_TYPE" = tag ] && [ -n "$expected" ] && [ "$actual" != "$expected" ]; then
+ echo "::error::$apk is signed with $actual, not the release key $expected: it could not update earlier installs"
+ exit 1
+ fi
{
echo " $(basename "$apk")"
grep "certificate SHA-256" <<<"$certs" | sed 's/^/ /'
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index da273c3..d170aac 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -1,3 +1,5 @@
+import java.security.KeyStore
+import java.security.MessageDigest
import java.util.Properties
import javax.inject.Inject
import org.gradle.process.ExecOperations
@@ -61,6 +63,30 @@ val releaseKeystore: File? = signingValue("storeFile", "CLEARPDF_UPLOAD_STORE_FI
?.let { File(it).takeIf(File::isAbsolute) ?: File(repoDir, it) }
val unsignedRelease: Boolean = providers.gradleProperty("artefact.unsigned").orNull == "true"
+/**
+ * The release key's certificate against the one every published release carries
+ * (gradle.properties' artefact.releaseCertSha256): another key could never update the installed app.
+ * A tag build (the release workflow) fails on any mismatch, as does -Partefact.requireReleaseKey=true;
+ * a local release build only warns, loudly, since its APK can't replace a published install.
+ */
+fun checkReleaseKey(logger: org.gradle.api.logging.Logger) {
+ val expected = providers.gradleProperty("artefact.releaseCertSha256").orNull?.lowercase()?.replace(":", "") ?: return
+ val strict = providers.environmentVariable("GITHUB_REF_TYPE").orNull == "tag" ||
+ providers.gradleProperty("artefact.requireReleaseKey").orNull == "true"
+ fun fail(why: String) {
+ if (strict) throw GradleException("Release signing: $why")
+ logger.warn("WARNING: release signing: $why. This APK cannot update a published install (uninstall first).")
+ }
+ val store = releaseKeystore ?: return fail("no release key (signed with the debug key)")
+ val actual = runCatching {
+ val ks = KeyStore.getInstance(if (store.name.endsWith(".jks")) "JKS" else KeyStore.getDefaultType())
+ store.inputStream().use { ks.load(it, signingValue("storePassword", "CLEARPDF_UPLOAD_STORE_PASSWORD")?.toCharArray()) }
+ val cert = ks.getCertificate(signingValue("keyAlias", "CLEARPDF_UPLOAD_KEY_ALIAS")) ?: error("no such key alias")
+ MessageDigest.getInstance("SHA-256").digest(cert.encoded).joinToString("") { "%02x".format(it) }
+ }.getOrElse { return fail("could not read the release key (${it.message})") }
+ if (actual != expected) fail("the key's certificate is $actual, but every published release is signed with $expected")
+}
+
/** rustup's `cargo` (Android Studio may not see rustup's PATH), else `cargo` from PATH; `CARGO` overrides. */
val cargoExecutable: String = run {
val exe = if (System.getProperty("os.name").lowercase().startsWith("windows")) "cargo.exe" else "cargo"
@@ -308,6 +334,14 @@ android {
}
}
+// before every signed release build (not F-Droid's unsigned one, not the debug or fast builds)
+val releaseKeyCheck = tasks.register("checkReleaseKey") {
+ group = "verification"
+ description = "Checks the release key is the one every published release was signed with"
+ doLast { checkReleaseKey(logger) }
+}
+tasks.matching { it.name == "preReleaseBuild" }.configureEach { if (!unsignedRelease) dependsOn(releaseKeyCheck) }
+
androidComponents {
onVariants { variant ->
val name = variant.name.replaceFirstChar { it.uppercase() }
diff --git a/android/gradle.properties b/android/gradle.properties
index 969a1ef..e4f16d3 100644
--- a/android/gradle.properties
+++ b/android/gradle.properties
@@ -14,3 +14,10 @@ artefact.abis=arm64-v8a,x86_64
# tag that differs, and F-Droid's update check reads them from here.
artefact.version=3.0.0-beta.11
artefact.versionCode=3000011
+
+# The certificate every published release has been signed with (CN=ClearPDF Upload; SHA-256 of
+# the certificate, as `apksigner verify --print-certs` prints it). Android installs an update only
+# from the same key, so a release signed with any other can never update the app people have: a
+# tag build fails on a mismatch (app/build.gradle.kts, and the release workflow checks each APK),
+# a local release build warns. Change it only if the app's key is ever rotated.
+artefact.releaseCertSha256=92fa506003c3e83b933cea5dec33cdae9d4f4beb27bcadf48a15fd0e487401e9