fix(apk): read constructed BER signer signatures

Share the bounded BER tree reader between PE certificates and APK v1 CMS, then accept constructed OCTET STRING signature values while retaining the declared signer and certificate facts. Embedded X.509 certificate decoding keeps its DER requirement.

Fixes #6603
This commit is contained in:
morluto committed 2026-10-23 19:07:45 +08:00
1 parent d0a8e3c60d
commit 55185abec8
6 files changed
+328 -193

No files matched your search

@@ -13,12 +13,16 @@ import { createServer } from "../../src/server/createServer.js";
import {
APK_SIGNATURE_V2_ID,
APK_SIGNATURE_V3_ID,
indefinite,
LEAF_CERTIFICATE,
LEAF_SHA256,
LEAF_SUBJECT,
OPENSSL_BLOCK_ISSUER_SERIAL,
OPENSSL_BLOCK_KEY_ID,
ROOT_CERTIFICATE,
signedDataBlock,
signerInfo,
tlv,
v2SchemeValue,
v3SchemeValue,
withSigningBlock,
@@ -175,6 +179,58 @@ cliTest(
},
);
cliTest(
"retains v1 signer identity when the CMS signature OCTET STRING is constructed BER",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-apk-v1-ber-signature-");
const encodings = [
[
"definite",
tlv(0x24, tlv(0x04, Uint8Array.of(1)), tlv(0x04, Uint8Array.of(2, 3))),
],
[
"indefinite",
indefinite(
0x24,
tlv(0x04, Uint8Array.of(1)),
tlv(0x04, Uint8Array.of(2, 3)),
),
],
] as const;
for (const [form, signature] of encodings) {
const path = join(root, `${form}.apk`);
await writeFile(
path,
await zipWith([
{ name: "META-INF/MANIFEST.MF", data: "Manifest-Version: 1.0" },
{ name: "META-INF/CERT.SF", data: "Signature-Version: 1.0" },
{
name: "META-INF/CERT.RSA",
data: signedDataBlock({
certificates: [LEAF_CERTIFICATE],
signerInfos: [signerInfo({ names: LEAF_CERTIFICATE, signature })],
}),
},
]),
);
const result = await cli.run({
arguments: ["inspect-apk-signature", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = completeEvidence(result.json);
expect(evidence.normalized_result.schemes.v1.signers).toMatchObject([
{
certificate: {
sha256_fingerprint: LEAF_SHA256,
subject: LEAF_SUBJECT,
},
decode_error: null,
},
]);
}
},
);
cliTest(
"keeps a subjectKeyIdentifier v1 signer out of the posture match, as apksig cannot resolve it",
async ({ cli }) => {