fix(pe): admit Authenticode nesting depth of signed Windows binaries (#12082)

* fix(pe): admit Authenticode nesting depth of signed Windows binaries

inspect_pe_certificates refused genuine Authenticode signatures with "ASN.1 nesting exceeds 32 levels". Windows dual signing embeds whole SignedData values in unsigned attributes: a nested signature (1.3.6.1.4.1.311.2.4.1) holding an RFC 3161 timestamp (1.3.6.1.4.1.311.3.3.1). Each embedding adds eight levels, so signed system binaries pass the shared reader's default of 32.

The PE certificate reader now gives the shared ASN.1 reader its own depth allowance of 64. The node allowance is unchanged, and a payload past the depth allowance is still refused as an exhausted fixed allowance.

Ported onto current main from morluto/rea#7160. The fixture gains signed attributes through its fields argument, and the provider's allowance test moves to the new limit.

* test(pe): pin the Authenticode depth allowance at its boundary

The dual-signed fixture has to be deeper than the shared reader's default, or it would pass without the PE depth allowance. An independent DER walk now asserts its depth, and a boundary case admits nesting at the allowance and refuses one level more as an exhausted allowance.

The certificate guide states the depth allowance next to the value allowance.

---------

Co-authored-by: AmmarJamil23 <ammarjamil7000@gmail.com>
This commit is contained in:
David Muñoz ReyandAmmarJamil23 authored and GitHub committed 2026-11-18 13:29:11 -05:00
1 parent 4992ca5b2f
commit 0b95fa7d69
5 files changed
+139 -9

No files matched your search

+3 -1
View File
@@ -87,7 +87,9 @@ and exactly two fields (type and value) in each attribute, as defined in
Malformed name structures fail before their text is reported. Decoded
certificate name text, serials, validity spellings, and subject key identifiers
share a 64 KiB budget per PKCS#7 payload, and each payload decodes at most
16,384 ASN.1 values. Exhausting either fixed allowance returns a nonretryable
16,384 ASN.1 values nested at most 64 levels deep. Dual-signed Windows binaries
embed a nested signature and its RFC 3161 timestamp in unsigned attributes, and
each embedding adds eight levels. Exhausting a fixed allowance returns a nonretryable
`resource_constraint` error naming it, not an invalid-input error; this release
has no runtime override, and `max_file_bytes` and `max_entries` do not raise
it. Unsupported certificate name
+8 -1
View File
@@ -57,10 +57,17 @@ const fail = (message: string): never => {
// few thousand nodes; the budget bounds sibling fan-out, which depth does not.
const ASN1_NODE_BUDGET = 16_384;
// Windows dual signing embeds whole SignedData values in unsigned attributes:
// a nested signature (1.3.6.1.4.1.311.2.4.1) holding an RFC 3161 timestamp
// (1.3.6.1.4.1.311.3.3.1). Each embedding adds eight levels, and signed system
// binaries put values 33 levels below the outermost one, past the shared
// reader's default of 32. The cap still bounds recursion for hostile input.
const ASN1_DEPTH_BUDGET = 64;
/** Decode one bounded ASN.1 TLV, including indefinite constructed BER values. */
export const readAsn1 = (bytes: Buffer): Asn1BerNode => {
try {
return new Asn1BerReader(bytes, ASN1_NODE_BUDGET).read();
return new Asn1BerReader(bytes, ASN1_NODE_BUDGET, ASN1_DEPTH_BUDGET).read();
} catch (cause: unknown) {
if (cause instanceof Asn1BerFailure)
throw new PeCertificateAsn1Failure(
+78
View File
@@ -8,6 +8,7 @@ import { ArtifactReaderFailure } from "../../resources/ArtifactReader.js";
import {
peCertificateFixture,
peWithoutCertificateTableFixture,
pkcs7DualSignedTimestampedFixture,
pkcs7SignedDataFixture,
pkcs7SignedDataIndefiniteEnvelopeFixture,
} from "./PeCertificates.fixture.js";
@@ -21,6 +22,7 @@ import { parsePeCertificates } from "./PeCertificateParser.js";
// Independent boundary examples: changing an allowance requires reviewing
// both the admitted and rejected certificate shapes.
const ASN1_NODE_BUDGET = 16_384;
const ASN1_DEPTH_BUDGET = 64;
const MAX_OID_BYTES = 256;
const MAX_OID_ARC_BYTES = 32;
const MAX_DECODED_NAME_BYTES = 65_536;
@@ -737,6 +739,82 @@ it("stops decoding a certificate payload at the ASN.1 node budget", () => {
);
});
it("reads a dual-signed, timestamped signature as deep as signed Windows binaries", () => {
const report = parse(
peCertificateFixture(true, pkcs7DualSignedTimestampedFixture()),
);
expect(report.entries[0]?.pkcs7?.signers).toEqual([
expect.objectContaining({
serial_hex: "11223344",
unsigned_attribute_oids: ["1.3.6.1.4.1.311.2.4.1"],
}),
]);
// The nested signature and its timestamp are not inventoried yet.
expect(report.coverage).toMatchObject({
status: "partial",
certificates: 1,
});
expect(report.limitations).toContain(
"Signer unsigned attribute contents are not decoded; nested signatures and timestamp token certificates and times may be omitted.",
);
});
it("stops decoding a certificate payload at the ASN.1 depth budget", () => {
let nested: Buffer = Buffer.from([0x05, 0x00]);
for (let level = 0; level <= ASN1_DEPTH_BUDGET; level += 1)
nested = der(0x30, nested);
expect(() => readAsn1(nested)).toThrow(
`ASN.1 nesting exceeds ${String(ASN1_DEPTH_BUDGET)} levels.`,
);
});
// Walks definite-length DER on its own, so the fixture's depth does not depend
// on the reader under test. The outermost value is level 0, as in the reader.
const derDepth = (bytes: Buffer, level = 0): number => {
let deepest = level;
let at = 0;
while (at < bytes.length) {
const constructed = ((bytes[at] ?? 0) & 0x20) !== 0;
const first = bytes[at + 1] ?? 0;
const lengthBytes = first < 0x80 ? 0 : first & 0x7f;
const length =
lengthBytes === 0 ? first : bytes.readUIntBE(at + 2, lengthBytes);
const content = bytes.subarray(
at + 2 + lengthBytes,
at + 2 + lengthBytes + length,
);
if (constructed) deepest = Math.max(deepest, derDepth(content, level + 1));
at += 2 + lengthBytes + length;
}
return deepest;
};
it("builds the dual-signed fixture one level past the shared reader's default", () => {
// The shared reader admits level 32 by default. A shallower fixture would
// pass without the PE depth budget and prove nothing.
expect(derDepth(pkcs7DualSignedTimestampedFixture())).toBe(33);
});
it("admits ASN.1 nesting at the depth budget and refuses one level more", () => {
const nestedTo = (depth: number): Buffer => {
let node: Buffer = Buffer.from([0x05, 0x00]);
for (let level = 0; level < depth; level += 1) node = der(0x30, node);
return node;
};
expect(derDepth(nestedTo(ASN1_DEPTH_BUDGET))).toBe(ASN1_DEPTH_BUDGET);
expect(() => readAsn1(nestedTo(ASN1_DEPTH_BUDGET))).not.toThrow();
let refusal: unknown;
try {
readAsn1(nestedTo(ASN1_DEPTH_BUDGET + 1));
} catch (cause: unknown) {
refusal = cause;
}
expect(refusal).toBeInstanceOf(PeCertificateAsn1Failure);
expect(refusal).toMatchObject({
exhausted: { allowance: "asn1-depth", limit: ASN1_DEPTH_BUDGET },
});
});
it("rejects headers whose section table does not fit SizeOfHeaders", () => {
const bytes = peWithoutCertificateTableFixture();
bytes.writeUInt16LE(0xffff, 0x86);
+47 -4
View File
@@ -67,9 +67,13 @@ export const pkcs7SignedDataFixture = (
commonName = "REA Fixture",
organization = "REA Tests",
signature = octet(Buffer.from("sig")),
unsignedAttributes?: readonly (readonly number[])[],
// An OID gets a placeholder value; a Buffer is a complete encoded attribute.
unsignedAttributes?: readonly (readonly number[] | Buffer)[],
fields: Partial<
Record<"issuer" | "subject" | "signerIssuer" | "extensions", Buffer>
Record<
"issuer" | "subject" | "signerIssuer" | "extensions" | "signedAttributes",
Buffer
>
> = {},
): Buffer => {
const serial = Buffer.from([0x11, 0x22, 0x33, 0x44]);
@@ -97,6 +101,9 @@ export const pkcs7SignedDataFixture = (
integer(1),
seq(fields.signerIssuer ?? subject, integer(serial)),
algorithm(SHA256),
...(fields.signedAttributes === undefined
? []
: [context(0, fields.signedAttributes)]),
algorithm(SHA256_RSA),
signature,
...(unsignedAttributes === undefined
@@ -105,8 +112,10 @@ export const pkcs7SignedDataFixture = (
context(
1,
concat(
...unsignedAttributes.map((type) =>
seq(oid(type), set(octet(Buffer.from([1])))),
...unsignedAttributes.map((entry) =>
Buffer.isBuffer(entry)
? entry
: seq(oid(entry), set(octet(Buffer.from([1])))),
),
),
),
@@ -122,6 +131,40 @@ export const pkcs7SignedDataFixture = (
return seq(SIGNED_DATA, context(0, signedData));
};
const NESTED_SIGNATURE = oid([1, 3, 6, 1, 4, 1, 311, 2, 4, 1]);
const RFC3161_TIMESTAMP = oid([1, 3, 6, 1, 4, 1, 311, 3, 3, 1]);
const SIGNING_CERTIFICATE_V2 = oid([1, 2, 840, 113549, 1, 9, 16, 2, 47]);
/**
* Windows dual-signing shape, 34 levels deep like signed system binaries: the
* signature's unsigned attributes embed a nested signature, whose own unsigned
* attributes embed an RFC 3161 timestamp. The timestamp's signed ESS
* signingCertificateV2 issuer name is the deepest branch.
*/
export const pkcs7DualSignedTimestampedFixture = (): Buffer => {
const issuerSerial = seq(
seq(context(4, name("REA Timestamp CA", "REA Tests"))),
integer(Buffer.from([0x11, 0x22, 0x33, 0x44])),
);
const signingCertificate = attribute(
SIGNING_CERTIFICATE_V2,
set(seq(seq(seq(octet(Buffer.alloc(32)), issuerSerial)))),
);
const timestamp = pkcs7SignedDataFixture(
"REA Timestamp",
"REA Tests",
undefined,
undefined,
{ signedAttributes: signingCertificate },
);
const nested = pkcs7SignedDataFixture("REA Nested", "REA Tests", undefined, [
attribute(RFC3161_TIMESTAMP, set(timestamp)),
]);
return pkcs7SignedDataFixture(undefined, undefined, undefined, [
attribute(NESTED_SIGNATURE, set(nested)),
]);
};
/** Same CMS fields with a BER indefinite-length outer ContentInfo SEQUENCE. */
export const pkcs7SignedDataIndefiniteEnvelopeFixture = (): Buffer => {
const definite = pkcs7SignedDataFixture();
+3 -3
View File
@@ -50,9 +50,9 @@ const inspect = async (payload: Buffer) => {
it.each([
{
name: "depth",
payload: () => encapsulated(40),
limits: { max_asn1_depth: 32 },
ceiling: { limit: 32, unit: "nesting levels per PKCS#7 payload" },
payload: () => encapsulated(72),
limits: { max_asn1_depth: 64 },
ceiling: { limit: 64, unit: "nesting levels per PKCS#7 payload" },
},
{
name: "nodes",