Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 4 min read

Sign a PDF With a PFX File in Python: pyHanko and IronPDF

Released on 19 November 2026, pyHanko 0.38.2 fixed GHSA-639c-rx63-qgmx, a high-severity advisory in which whole-file signature coverage was derived from the decoded /Contents length without checking the position or encod

Released on 19 November 2026, pyHanko 0.38.2 fixed GHSA-639c-rx63-qgmx, a high-severity advisory in which whole-file signature coverage was derived from the decoded /Contents length without checking the position or encoding of the signature value. It was the third high-severity pyHanko advisory published that month, after two fixed in 0.38.1 on 3 November, and the project's security policy applies security fixes to one 0.x release, the one published last, and treats every other release as obsolete.

A service that signs offer letters with a PFX file through pyHanko therefore tracks every release, and its pinned version is the first item to check before any move. On 3 December 2026 we put one offer letter and one PFX file through both pyHanko 0.38.2 and IronPDF for Python 2026.12.0.1, and the password turned out to be the clearest difference.

Loading a PKCS#12 file in pyHanko

The SimpleSigner.load_pkcs12 method reads the PFX file and takes its passphrase as bytes. The signer then goes to sign_pdf with an incremental writer over the letter.

from pyhanko.sign import signers
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter

# Load the signer from the PFX file, with the passphrase as bytes
signer = signers.SimpleSigner.load_pkcs12("certificates/hr-signer.pfx", passphrase=b"offer-letter-pass")

with open("offer-letter.pdf", "rb") as letter, open("offer-letter-signed.pdf", "wb") as output:
    writer = IncrementalPdfFileWriter(letter)

    # Sign into a new field named HRSignature and write the signed copy
    signers.sign_pdf(writer, signers.PdfSignatureMetadata(field_name="HRSignature"), signer=signer, output=output)

The signed copy validated as intact with a SHA-256 digest, and because the letter had no field called HRSignature, pyHanko created an invisible one, which its documentation describes as the default.

When the passphrase arrives as a string

A password read from an environment variable or a configuration file is a Python str, and that is where the run changed. With passphrase="offer-letter-pass", load_pkcs12 logged Could not load key material from PKCS#12 file, with a TypeError underneath asking for a bytestring, and returned None instead of raising. A wrong password gave the same log line and the same None, which matches the method's docstring.

The None then reached sign_pdf, which stopped with AttributeError: 'NoneType' object has no attribute 'get_signature_mechanism_for_digest'. By then the output file had been opened for writing, so offer-letter-signed.pdf was left on disk at 0 bytes. A job that copies that file onward without checking its size sends an empty letter. A pre-move review of the existing job checks three points, whether the password is encoded in the encoding the PFX was made with, whether the signer is tested for None before signing and whether output goes to a temporary path that is renamed only after sign_pdf returns.

The same letter in IronPDF for Python

Here the PFX path and its password are both ordinary strings, and the letter is rendered and signed on one document object. The render step on its own appears in IronPDF's HTML string sample.

from ironpdf import *

# Render the offer letter from its HTML template
letter = ChromePdfRenderer().RenderHtmlAsPdf("<h1>Offer of Employment</h1><p>Backend Engineer, starting 4 January 2027</p>")

# Load the PFX file with its password as an ordinary string
signature = PdfSignature("certificates/hr-signer.pfx", "offer-letter-pass")
signature.SigningReason = "Offer of employment"

# Sign the letter and save the signed copy
letter.Sign(signature)
letter.SaveAs("offer-letter-signed-iron.pdf")

# Check the signature on the saved file
print(PdfDocument.FromFile("offer-letter-signed-iron.pdf").VerifyPdfSignatures(False))

The listing printed True. SHA-256 is the default digest for the signature and for any timestamp, and when the self-signed test certificate was supplied as a trust anchor, the pyHanko 0.38.2 validator accepted the IronPDF signature as intact, valid and covering the whole file. The password stayed a string from the configuration to the constructor, with no encoding step and no None to test for.

pyHanko's depth in signing and validation

The pyHanko project is a capable signing library with a wide feature set. Long-term signatures are a focus, with all four PAdES baseline levels up to B-LTA, timestamp tokens fetched under RFC 3161 and revocation data embedded in the Adobe style. Key material ranges from RSA and DSA to ECDSA, EdDSA and the post-quantum ML-DSA scheme, from a file or from a PKCS#11 token, and its incremental updates preserve each earlier revision of the letter.

On the receiving side, a returned letter can be checked for a trusted certificate path, for edits made after the HR signature, against AdES rules and against the EU trusted lists. The pyhanko-cli package signs from the shell with the pkcs12 subcommand, and each November advisory was published on the same day as the release that fixed it.

Moving a PFX signing job

  • The PFX files and their passwords carry over unchanged, and the password stays a string.
  • Jobs that render the letter from HTML and then sign it lead, since both steps fit in one listing.
  • Jobs that sign letters produced elsewhere are second, with PdfDocument.FromFile in place of the render call.
  • A signed sample from each job is opened in the viewer the recipients use before the old code is retired.

IronPDF for Python signed the offer letter from the PFX file with a string password, and in the 3 December 2026 run pyHanko's validator accepted that signature. Once a key is issued through the IronPDF for Python licensing page, the same listing signs the HR team's real letters.

pyHanko and Python are trademarks or works of their respective owners, we have no affiliation with any of them, and the facts above are based on the pyHanko documentation, source, security policy, changelog and GitHub security advisories, our own runs and IronPDF for Python documentation.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.