Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(pe): reconcile counted certificate padding with main
  • Loading branch information
morluto committed Oct 23, 2026
commit 49c75e05af4b93b91a81dcbb82eaac94c5e2b09d
5 changes: 3 additions & 2 deletions docs/pe-certificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ revision, type, and the payload SHA-256.

Type-2 entries may count their zero alignment padding in `dwLength`. The PE
entry owner accepts only the exact gap to the next eight-byte boundary after
the parsed ContentInfo; extra data or nonzero counted padding fails. Entry and
payload ranges and hashes retain all declared bytes, including counted padding.
the parsed ContentInfo; extra data or nonzero counted padding fails. Entry
ranges retain all declared bytes, including counted padding. CMS payload
ranges and hashes exclude counted padding.

For `WIN_CERTIFICATE` type 2 (PKCS#7 signedData) `pkcs7` contains digest
algorithm OIDs, embedded X.509 serial/issuer/subject/validity fields, and
Expand Down
6 changes: 1 addition & 5 deletions src/native/pe/PeCertificateAsn1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,7 @@ const parseSignedData = (
};

/** Decode ContentInfo wrapping SignedData; other content types remain unsupported. */
export const parsePkcs7SignedData = (
bytes: Buffer,
root: Asn1Node = readAsn1(bytes),
): ParsedSignedData => {
if (root.end !== bytes.length) return fail("ContentInfo has trailing data.");
export const parsePkcs7SignedData = (root: Asn1Node): ParsedSignedData => {
const children = sequence(root, "ContentInfo");
const type = children[0];
const content = children[1];
Expand Down
36 changes: 36 additions & 0 deletions src/native/pe/PeCertificateParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ it("accepts only zero eight-byte alignment padding included in dwLength", () =>
}
});

it.each([false, true])(
"reads all counted alignment gaps and consecutive entries (PE32+ = %s)",
(plus) => {
const gaps = new Set<number>();
for (let size = 1; size <= 32; size += 1) {
const payload = pkcs7SignedDataFixture("x".repeat(size));
const gap = (8 - ((8 + payload.length) % 8)) % 8;
if (gaps.has(gap)) continue;
gaps.add(gap);
const single = peCertificateFixture(plus, payload, 2, true);
const table = single.subarray(0x200);
const bytes = Buffer.concat([single, table]);
const directoryAt = 0x98 + (plus ? 112 : 96) + 4 * 8;
bytes.writeUInt32LE(table.length * 2, directoryAt + 4);
const report = peCertificatesSchema.parse(parse(bytes));
expect(report.coverage).toEqual({
status: "complete",
examined_entries: 2,
certificates: 2,
});
for (const [index, entry] of report.entries.entries()) {
expect(entry.location).toEqual({
offset: 0x200 + index * table.length,
bytes: table.length,
});
expect(entry.payload.bytes).toBe(payload.length);
expect(entry.payload.sha256).toBe(
createHash("sha256").update(payload).digest("hex"),
);
expect(entry.pkcs7?.signers).toHaveLength(1);
}
}
expect([...gaps].sort()).toEqual([0, 1, 2, 3, 4, 5, 6, 7]);
},
);

it("rejects nonzero or excessive bytes after the encoded PKCS#7 value", () => {
const payload = pkcs7SignedDataFixture();
const nonzero = peCertificateFixture(false, payload, 2, true);
Expand Down
3 changes: 1 addition & 2 deletions src/native/pe/PeCertificateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const parsePeCertificates = (
)
fail("WIN_CERTIFICATE has invalid PKCS#7 alignment padding.");
payload = entryPayload.subarray(0, root.end);
const parsed = parsePkcs7SignedData(payload, root);
const parsed = parsePkcs7SignedData(root);
teletexStringCompatibilityUsed ||=
parsed.teletexStringCompatibilityUsed;
pkcs7 = {
Expand Down Expand Up @@ -180,7 +180,6 @@ export const parsePeCertificates = (
})),
};
} catch (cause) {
if (cause instanceof ArtifactReaderFailure) throw cause;
if (cause instanceof PeCertificateAsn1Failure)
fail(`PKCS#7 signedData is malformed: ${cause.message}`);
throw cause;
Expand Down
27 changes: 12 additions & 15 deletions tests/acceptance/peCertificatesCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ cliTest("reports a truncated PE as invalid input", async ({ cli }) => {
});

cliTest(
"rejects TeletexString names without returning corrupted identity text",
"reports TeletexString compatibility text with its decoding limitation",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pe-cert-cli-teletex-");
const path = join(root, "teletex.exe");
Expand All @@ -104,19 +104,16 @@ cliTest(
const result = await cli.run({
arguments: ["inspect-pe-certificates", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "invalid_request",
details: {
operation: "inspect_pe_certificates",
issues: [
{
message: expect.stringContaining(
"Unsupported certificate name encoding (DER tag 0x14)",
),
},
],
},
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_pe_certificates").outputSchema.parse(
result.json,
);
expect(
evidence.normalized_result.entries[0]?.pkcs7?.certificates[0]?.issuer
.text,
).toBe("commonName=Âe, organizationName=REA Tests");
expect(evidence.normalized_result.limitations).toContain(
"TeletexString values use OpenSSL's ISO-8859-1 compatibility mapping; this is not a full T.61 decoder.",
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ it.skipIf(process.platform === "win32")(
path: "container",
kind: "directory",
tree_state: "unreadable",
limitations: [expect.stringContaining(readFailure.message)],
limitations: [
expect.stringContaining("This directory could not be read"),
],
local_diagnostics: [expect.stringContaining(readFailure.message)],
}),
expect.objectContaining({
path: "container/hidden",
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.