Android 12.0.0 release 16
Add initial support for appending signatures to an APK

The V1 and V2 signature schemes support multiple signers, but
currently apksig does not support appending a V1 or V2 signature
to an already signed APK. This commit adds the initial support to
append a V1 / V2 signature to an already signed APK through one of
the following options:
- The --append-signature option when signing via apksigner
- ApkSigner.Builder#setOtherSignersSignaturePreserved(true) when
  using the apksig Java API
These options can also be used when an APK has additional key / value
attribute blocks within the APK signing block; using the same signing
config will overwrite the existing signatures while preserving these
additional blocks.

Note, when an APK has multiple V1 and V2 signatures one signer must
sign the APK multiple times since another signer appending their
V1 signature will invalidate an existing V2 signature. The following
process will allow two signers, A and B, to sign an APK with V1 and
V2 signatures:
1. A signs the APK with a V1 and V2 signature; this will add the
   stripping protection attribute to A's V1 signature.
2. B uses one of the append options listed above to sign the APK
   with a V1 and V2 signature. This will invalidate A's V2
   signature since V2 is a whole APK signature scheme and B's V1
   signature will modify the APK, but A's V1 signature will still
   be valid.
3. A uses one of the append options listed above to sign the APK
   with a V2 signature. This will overwrite A's previous V2
   signature using the updated contents of the APK and append
   this signature to B's existing signature in the V2 signature
   block.

Bug: 186395570
Test: gradlew build
Change-Id: I8ad2f21e111dcdc5e2c41695439794afba9cc1de
9 files changed
tree: 9377b651b9a765ffbc05ec4a394972c54446e1fa
  1. etc/
  2. src/
  3. Android.bp
  4. android_plugin_for_gradle.gradle
  5. build.gradle
  6. LICENSE
  7. OWNERS
  8. README.md
README.md

apksig

apksig is a project which aims to simplify APK signing and checking whether APK signatures are expected to verify on Android. apksig supports JAR signing (used by Android since day one) and APK Signature Scheme v2 (supported since Android Nougat, API Level 24). apksig is meant to be used outside of Android devices.

The key feature of apksig is that it knows about differences in APK signature verification logic between different versions of the Android platform. apksig thus thoroughly checks whether an APK's signature is expected to verify on all Android platform versions supported by the APK. When signing an APK, apksig chooses the most appropriate cryptographic algorithms based on the Android platform versions supported by the APK being signed.

The project consists of two subprojects:

  • apksig -- a pure Java library, and
  • apksigner -- a pure Java command-line tool based on the apksig library.

apksig library

apksig library offers three primitives:

  • ApkSigner which signs the provided APK so that it verifies on all Android platform versions supported by the APK. The range of platform versions can be customized.
  • ApkVerifier which checks whether the provided APK is expected to verify on all Android platform versions supported by the APK. The range of platform versions can be customized.
  • (Default)ApkSignerEngine which abstracts away signing APKs from parsing and building APKs. This is useful in optimized APK building pipelines, such as in Android Plugin for Gradle, which need to perform signing while building an APK, instead of after. For simpler use cases where the APK to be signed is available upfront, the ApkSigner above is easier to use.

NOTE: Some public classes of the library are in packages having the word “internal” in their name. These are not public API of the library. Do not use *.internal.* classes directly because these classes may change any time without regard to existing clients outside of apksig and apksigner.

apksigner command-line tool

apksigner command-line tool offers two operations:

  • sign the provided APK so that it verifies on all Android platforms supported by the APK. Run apksigner sign for usage information.
  • check whether the provided APK's signatures are expected to verify on all Android platforms supported by the APK. Run apksigner verify for usage information.

The tool determines the range of Android platform versions (API Levels) supported by the APK by inspecting the APK's AndroidManifest.xml. This behavior can be overridden by specifying the range of platform versions on the command-line.