Skip to content

Align PE certificate tables before portable signing - #36

Open
nurfed1 wants to merge 2 commits into
Devolutions:masterfrom
nurfed1:pr/pe-certificate-table-alignment
Open

Align PE certificate tables before portable signing#36
nurfed1 wants to merge 2 commits into
Devolutions:masterfrom
nurfed1:pr/pe-certificate-table-alignment

Conversation

@nurfed1

@nurfed1 nurfed1 commented Sep 11, 2026

Copy link
Copy Markdown

Summary

  • pad unsigned PE input to an 8-byte boundary before computing its Authenticode digest
  • require existing PE certificate-table offsets and sizes to be 8-byte aligned
  • apply the preparation step across the portable CLI, portable core, local signing, and remote signing paths
  • add Rust coverage for every possible EOF alignment residue and a Windows test verified by native WinTrust

Problem

The PE attribute certificate table must start on an 8-byte boundary. The portable signing paths currently compute the Authenticode digest over the original PE and then append the WIN_CERTIFICATE record directly at EOF.

When an unsigned PE ends at an unaligned offset, the resulting security-directory file offset is also unaligned. psign can parse and verify its own output, but Windows rejects that signature even though the embedded digest is otherwise internally consistent.

This is observable with valid PE inputs carrying an overlay or other trailing data whose total file length is not divisible by eight.

Microsoft documents the attribute certificate table layout and alignment requirements in the PE format specification:
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only

Change

For a PE without an existing certificate table, the signer now appends zero padding to the next 8-byte boundary before hashing. Because that padding is added before digest computation, it remains part of the Authenticode-hashed region and the subsequently embedded PKCS#7 digest matches the final file layout.

The low-level append helper rejects an unaligned unsigned image rather than silently adding unhashed bytes. Existing certificate tables are accepted only when both their offset and size are aligned; misaligned tables are rejected instead of being relocated and invalidating an existing signature.

The same preparation is used by all PE signing entry points. Verification also rejects misaligned certificate-table metadata explicitly.

Tests

Rust tests cover:

  • all eight possible unsigned-PE EOF residues
  • zero-padding placement and preservation of the original bytes
  • rejection when the append helper receives an unprepared image
  • rejection of a misaligned existing certificate table
  • digest consistency after signing every EOF residue

The Windows parity test builds the repro input at runtime from the existing CI PE fixture, signs it through portable sign-pe, and requires native signtool verify /pa to succeed. No signed product binary is committed as a fixture.

The fork branch passed formatting, clippy, the portable test matrix, packaging builds, and the Windows native verification workflow:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant