Skip to content

Fix attach-signature -nest accepting a signature of another file - #541

Open
francoisgirinon-netizen wants to merge 1 commit into
mtrojnar:masterfrom
francoisgirinon-netizen:fix/attach-nest-digest-mismatch
Open

francoisgirinon-netizen wants to merge 1 commit into
mtrojnar:masterfrom
francoisgirinon-netizen:fix/attach-nest-digest-mismatch

Conversation

@francoisgirinon-netizen

Copy link
Copy Markdown
Contributor

Pull Request Type

  • Bug fix
  • New feature
  • Code style / formatting / renaming
  • Refactoring (no functional or API changes)
  • Build / CI related changes
  • Documentation
  • Other (please describe):

Related Issue

Issue number: N/A

Current Behavior

attach-signature -nest accepts a signature computed for another file, and
writes a file whose nested signature fails verification:

$ osslsigncode attach-signature -nest -sigin ex_.der -in signed.exe -out out.exe
Signature successfully attached
Succeeded
$ osslsigncode verify -in out.exe ...
Calculated message digest : ... MISMATCH!!!
Signature verification: failed

After attaching, check_attached_data() calls verify_signed_file() without
signature verification, which succeeds as soon as the digest of one signature
matches. With -nest, the current signature always matches.

Reproduced on master for PE, CAB, PowerShell (.ps1, .psc1), MOF and
JavaScript files. APPX does not support nesting, so it is not affected.

New Behavior

attach-signature fails with Signature mismatch when the attached signature
does not match the file, and no output file is left.

Scope of Changes

  • osslsigncode.c: in verify_signed_file(), without signature verification,
    the digest of every signature must match, not just one. This path is only used
    by check_attached_data().
  • cmake/CMakeTest.cmake: new tests attached_mismatch_<ext> attach a nested
    signature of another file and expect Signature mismatch, for exe, ex_,
    ps1, psc1, mof and js.

Testing

  • Existing tests

  • New tests added

  • Manual testing

  • ctest: 430/430 passed on Windows x86-64 (MSVC 2019, OpenSSL 3.5.9), without
    LARGE_FILE_TESTS.

  • The six new tests fail on master (the attach succeeds), so they catch the
    bug.

  • Still accepted, as on master: a matching nested signature (the existing
    attached_pem_* and attached_der_* tests, with -h sha512), attaching
    without -nest to replace a signature, and attaching a signature that already
    contains a nested one.

Additional Notes

  • -nest onto a file whose existing signature no longer matches its content
    (the file was modified after signing) is now refused too: master accepted
    it and wrote a file whose primary signature fails verification. Attaching
    without -nest to such a file still works.
  • With -add-msi-dse, nesting a signature with a different digest algorithm is
    now refused: master accepted it, but rewriting the MsiDigitalSignatureEx
    stream broke the primary signature. sign -nest already refuses this case.
  • MSI and APPX are not covered by the new tests: a signature of another MSI
    file is rejected earlier when the MsiDigitalSignatureEx digests differ, and
    APPX does not support nesting.

License Declaration

  • I hereby agree to license my contribution under the project's license.

After attaching a signature, check_attached_data() calls
verify_signed_file() without signature verification, which succeeded as
soon as the digest of one signature matched the file. With -nest, the
current signature always matches, so attach-signature -nest accepted a
signature computed for another file, and wrote a file whose nested
signature fails verification.

Without signature verification, require the digest of every signature to
match. Test the attach of a nested signature of another file for each
format but MSI, CAT and APPX.
@mtrojnar
mtrojnar requested a review from olszomal November 23, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant