Signing with DroidDeck's own key: hand-over from the testkey, test builds and a four-package release

This commit is contained in:
The412Banner committed 2026-09-24 12:45:13 -04:00
1 parent 498a37db32
commit cd28b189a2
8 files changed
+453

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# axml_rename.py in.apk out.apk old=new ... : rewrite whole strings in AndroidManifest.xml's string
# pool (binary XML), leaving every other entry of the apk byte-identical. Unsigned output.
import struct, sys, zipfile
def dec_len8(b, o):
n = b[o]; o += 1
if n & 0x80: n = ((n & 0x7f) << 8) | b[o]; o += 1
return n, o
def dec_len16(b, o):
n = struct.unpack_from('<H', b, o)[0]; o += 2
if n & 0x8000: n = ((n & 0x7fff) << 16) | struct.unpack_from('<H', b, o)[0]; o += 2
return n, o
def enc_len8(n):
return bytes([n]) if n < 0x80 else bytes([0x80 | (n >> 8), n & 0xff])
def enc_len16(n):
return struct.pack('<H', n) if n < 0x8000 else struct.pack('<HH', 0x8000 | (n >> 16), n & 0xffff)
def rewrite(axml, repl):
ftype, fhs, fsize = struct.unpack_from('<HHI', axml, 0)
assert ftype == 0x0003, 'not binary XML'
sp = fhs
ctype, chs, csize, count, scount, flags, sstart, stystart = struct.unpack_from('<HHIIIIII', axml, sp)
assert ctype == 0x0001
utf8 = bool(flags & 0x100)
offs = struct.unpack_from('<%dI' % count, axml, sp + chs)
sty_offs = axml[sp + chs + 4 * count: sp + chs + 4 * count + 4 * scount]
base = sp + sstart
strings = []
for off in offs:
o = base + off
if utf8:
_, o = dec_len8(axml, o); n, o = dec_len8(axml, o)
strings.append(axml[o:o + n].decode('utf-8'))
else:
n, o = dec_len16(axml, o)
strings.append(axml[o:o + 2 * n].decode('utf-16-le'))
hits = 0
for i, s in enumerate(strings):
if s in repl: strings[i] = repl[s]; hits += 1
data = bytearray(); new_offs = []
for s in strings:
new_offs.append(len(data))
if utf8:
e = s.encode('utf-8'); data += enc_len8(len(s.encode('utf-16-le')) // 2) + enc_len8(len(e)) + e + b'\0'
else:
e = s.encode('utf-16-le'); data += enc_len16(len(e) // 2) + e + b'\0\0'
while len(data) % 4: data += b'\0'
styles = axml[sp + stystart: sp + csize] if scount else b''
new_sstart = chs + 4 * count + 4 * scount
new_stystart = new_sstart + len(data) if scount else 0
body = struct.pack('<%dI' % count, *new_offs) + sty_offs + bytes(data) + styles
new_csize = chs + len(body)
header = struct.pack('<HHIIIIII', ctype, chs, new_csize, count, scount, flags, new_sstart, new_stystart)
header += axml[sp + 28: sp + chs]
rest = axml[sp + csize:]
out = axml[:sp] + header + body + rest
out = out[:4] + struct.pack('<I', len(out)) + out[8:]
return out, hits
src, dst = sys.argv[1], sys.argv[2]
repl = dict(a.split('=', 1) for a in sys.argv[3:])
with zipfile.ZipFile(src) as zi, zipfile.ZipFile(dst, 'w') as zo:
for info in zi.infolist():
if info.filename.startswith('META-INF/') and info.filename.split('/')[-1].split('.')[-1] in ('SF', 'RSA', 'DSA', 'EC', 'MF'):
continue
b = zi.read(info)
if info.filename == 'AndroidManifest.xml':
b, hits = rewrite(b, repl)
print(f'manifest: {hits} strings replaced')
zo.writestr(info, b, compress_type=info.compress_type)
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# make-release-key.sh - run ONCE, by the owner. Makes DroidDeck's signing key and gives it to GitHub.
#
# - The key is made on this device, in ~/droiddeck-release-key (outside the repo), with a long
# random password written beside it. Nothing secret is printed.
# - The key and password go to GitHub as secrets of the three signing environments
# (signing-main, signing-branch, release). GitHub never shows a secret again.
# - The key's certificate digest - public, not a secret - goes to keystore/release-signer.sha256,
# which CI checks every signed apk against. That file is the only thing to commit.
#
# BACK UP ~/droiddeck-release-key (both files) somewhere safe - a password manager, a USB stick.
# Without it no update can ever be signed for the people who installed a signed build.
set -euo pipefail
REPO=The412Banner/DroidDeck
ENVIRONMENTS=(signing-main signing-branch release)
ALIAS=droiddeck
OUT=${DROIDDECK_KEY_DIR:-$HOME/droiddeck-release-key}
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
die() { echo "error: $*" >&2; exit 1; }
command -v keytool >/dev/null || die "keytool not found (Termux: pkg install openjdk-17)"
command -v gh >/dev/null || die "gh not found"
gh auth status >/dev/null 2>&1 || die "gh is not logged in (gh auth login)"
[ -e "$OUT/droiddeck-release.p12" ] && die "a key already exists in $OUT - there must only ever be one"
case "$OUT" in "$repo"|"$repo"/*) die "the key must not be made inside the repo" ;; esac
for env in "${ENVIRONMENTS[@]}"; do
gh api "repos/$REPO/environments/$env" >/dev/null 2>&1 || die "GitHub environment '$env' does not exist yet"
done
umask 077
mkdir -p "$OUT"
KS="$OUT/droiddeck-release.p12"
# 48 random characters; PKCS12 uses one password for the store and the key.
KS_PW=$(head -c 60 /dev/urandom | base64 -w0 | tr -d '/+=')
KS_PW=${KS_PW:0:48}
[ ${#KS_PW} -ge 40 ] || die "could not make a password"
export KS_PW
printf '%s\n' "$KS_PW" > "$OUT/password.txt"
keytool -genkeypair -keystore "$KS" -storetype PKCS12 -alias "$ALIAS" \
-keyalg RSA -keysize 4096 -validity 10950 -dname "CN=DroidDeck, O=The412Banner" \
-storepass:env KS_PW -keypass:env KS_PW >/dev/null 2>&1 || die "keytool could not make the key"
listing=$(keytool -list -v -keystore "$KS" -storepass:env KS_PW -alias "$ALIAS")
SHA=$(awk '/SHA256:/ { print $2 }' <<<"$listing" | head -n1 | tr -d ':' | tr 'A-F' 'a-f')
[[ $SHA =~ ^[0-9a-f]{64}$ ]] || die "could not read the key's certificate digest"
for env in "${ENVIRONMENTS[@]}"; do
base64 -w0 < "$KS" | gh secret set RELEASE_KEYSTORE_B64 --env "$env" -R "$REPO" >/dev/null
printf '%s' "$KS_PW" | gh secret set RELEASE_STORE_PASSWORD --env "$env" -R "$REPO" >/dev/null
printf '%s' "$ALIAS" | gh secret set RELEASE_KEY_ALIAS --env "$env" -R "$REPO" >/dev/null
echo "secrets set for environment: $env"
done
unset KS_PW
printf '%s\n' "$SHA" > "$repo/keystore/release-signer.sha256"
cat <<EOF
DroidDeck's signing key is made and in GitHub.
Key: $KS
Password: $OUT/password.txt
Digest: $SHA (public - written to keystore/release-signer.sha256)
Now:
1. Back up the folder $OUT (both files) to at least two safe places.
2. Commit keystore/release-signer.sha256 (only that file).
EOF
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env bash
# sign-apk.sh <in.apk> <variant> <out.apk>
#
# Turns a CI build (signed with the public AOSP testkey) into one package of tools/release/variants.txt
# signed with DroidDeck's own key, carrying the hand-over from the testkey:
# - v1 + v2 are signed by the testkey, v3 by our key, with a signed lineage testkey -> our key;
# - an install from before (testkey) updates to it and keeps its data;
# - the testkey loses its rollback right, so an apk signed with the testkey alone - which anyone
# can make - is refused as an update to it (device-proven on the Pocket FIT, 2026-09-24);
# - --rotation-min-sdk-version 28: the lineage applies from Android 9, not only from 13.
#
# The key comes from the environment and is never written anywhere but the path given:
# RELEASE_KEYSTORE path to the PKCS12 keystore
# RELEASE_STORE_PASSWORD its password (the key has the same one: PKCS12)
# RELEASE_KEY_ALIAS the key's alias
# BUILD_TOOLS Android build-tools dir (zipalign, apksigner, aapt)
# RELEASE_SIGNER_SHA256 optional: the expected certificate digest, else keystore/release-signer.sha256
set -euo pipefail
die() { echo "::error::$*" >&2; exit 1; }
[ $# -eq 3 ] || die "usage: sign-apk.sh <in.apk> <variant> <out.apk>"
in=$1 variant=$2 out=$3
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
pkg=$(awk -v v="$variant" '!/^#/ && $1 == v { print $2 }' "$here/variants.txt")
[ -n "$pkg" ] || die "no variant '$variant' in tools/release/variants.txt"
expected=${RELEASE_SIGNER_SHA256:-$(tr -d ' \n' < "$repo/keystore/release-signer.sha256" 2>/dev/null || true)}
[[ $expected =~ ^[0-9a-f]{64}$ ]] \
|| die "keystore/release-signer.sha256 is missing: make the key first (tools/release/make-release-key.sh)"
: "${RELEASE_KEYSTORE:?}" "${RELEASE_STORE_PASSWORD:?}" "${RELEASE_KEY_ALIAS:?}" "${BUILD_TOOLS:?}"
[ -s "$RELEASE_KEYSTORE" ] || die "the release keystore is empty - is the secret set for this environment?"
TESTKEY="$repo/keystore/testkey.p12"
TESTKEY_SHA256=a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
export TESTKEY_PASSWORD=android
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
# The package name and everything the manifest derives from it; component class names stay.
P=com.droiddeck.launcher
if [ "$pkg" = "$P" ]; then
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk"
else
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk" \
"$P=$pkg" "$P.androidx-startup=$pkg.androidx-startup" "$P.logs=$pkg.logs" \
"$P.documents=$pkg.documents" "$P.home=$pkg.home" \
"$P.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION=$pkg.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
fi
got=$("$BUILD_TOOLS/aapt" dump badging "$work/renamed.apk" | sed -n "s/^package: name='\([^']*\)'.*/\1/p")
[ "$got" = "$pkg" ] || die "$variant: the apk says package '$got', not '$pkg'"
if [ "$pkg" != "$P" ]; then
# A provider left on the old authority would clash with the standard app installed beside it.
tree=$("$BUILD_TOOLS/aapt" dump xmltree "$work/renamed.apk" AndroidManifest.xml)
grep -E 'android:(authorities|taskAffinity)' <<<"$tree" | grep -F "\"$P." \
&& die "$variant: the manifest still names $P in an authority or task"
fi
"$BUILD_TOOLS/zipalign" -p -f 4 "$work/renamed.apk" "$work/aligned.apk"
# The lineage: the testkey vouches for our key. Made fresh each time (it is two certificates and
# a signature, nothing secret beyond the key that signs it).
"$BUILD_TOOLS/apksigner" rotate --out "$work/lineage" \
--old-signer --ks "$TESTKEY" --ks-type PKCS12 --ks-pass env:TESTKEY_PASSWORD \
--ks-key-alias testkey --key-pass env:TESTKEY_PASSWORD --set-rollback false \
--new-signer --ks "$RELEASE_KEYSTORE" --ks-type PKCS12 --ks-pass env:RELEASE_STORE_PASSWORD \
--ks-key-alias "$RELEASE_KEY_ALIAS" --key-pass env:RELEASE_STORE_PASSWORD
"$BUILD_TOOLS/apksigner" sign \
--ks "$TESTKEY" --ks-type PKCS12 --ks-pass env:TESTKEY_PASSWORD \
--ks-key-alias testkey --key-pass env:TESTKEY_PASSWORD \
--next-signer --ks "$RELEASE_KEYSTORE" --ks-type PKCS12 --ks-pass env:RELEASE_STORE_PASSWORD \
--ks-key-alias "$RELEASE_KEY_ALIAS" --key-pass env:RELEASE_STORE_PASSWORD \
--lineage "$work/lineage" --rotation-min-sdk-version 28 \
--v1-signing-enabled true --v2-signing-enabled true --v3-signing-enabled true --v4-signing-enabled false \
--out "$out" "$work/aligned.apk"
rm -f "$out.idsig"
# What the apk must carry, checked rather than assumed.
"$BUILD_TOOLS/apksigner" verify --min-sdk-version 26 "$out" >/dev/null || die "$variant: does not verify"
# (Listed first, then searched: grep -q stops reading early, which fails a pipe under pipefail.)
listing=$(unzip -l "$out")
grep -qE 'META-INF/.*\.(SF|RSA)$' <<<"$listing" || die "$variant: no JAR signature (old installers)"
certs=$("$BUILD_TOOLS/apksigner" verify --min-sdk-version 28 --print-certs "$out")
v3=$(sed -n 's/^Signer #1 certificate SHA-256 digest: //p' <<<"$certs")
[ "$v3" = "$expected" ] || die "$variant: Android 9+ sees signer $v3, not DroidDeck's key $expected"
lineage=$("$BUILD_TOOLS/apksigner" lineage --in "$out" --print-certs)
grep -q "Signer #1 in lineage certificate SHA-256 digest: $TESTKEY_SHA256" <<<"$lineage" \
|| die "$variant: the lineage does not start at the testkey - old installs could not update"
grep -q "Signer #2 in lineage certificate SHA-256 digest: $expected" <<<"$lineage" \
|| die "$variant: the lineage does not hand over to DroidDeck's key"
rollback=$(awk '/Signer #1 in lineage/{s=1} /Signer #2 in lineage/{s=0} s && /rollback capability/' <<<"$lineage")
grep -q false <<<"$rollback" \
|| die "$variant: the testkey kept its rollback right - a testkey-signed apk could replace this one"
echo "$variant ($pkg): signed, hand-over from the testkey, v3 signer $expected"
+9
View File
@@ -0,0 +1,9 @@
# The packages a signed build ships as. The same apk, with only the manifest's package name (and
# the names derived from it) changed: some phones give their performance modes only to apps with
# certain names. Each is its own app to Android - installs beside the others, with its own data.
#
# name package file suffix
standard com.droiddeck.launcher -
pubg com.tencent.ig -pubg
antutu com.antutu.benchmark.full -antutu
ludashi com.ludashi.benchmark -ludashi