THotPDF.SignPDFWithPFX

THotPDF

 

Предишна  Методи  AddPubKeyRecipient

Signs an existing PDF placeholder using a PFX / PKCS#12 file, building a CMS SignedData container and writing the signed PDF in one call.

 

Delphi syntax (file overload):

class function SignPDFWithPFX(

  const InputPDFPath: string;

  const OutputPDFPath: string;

  const PFXFilePath: string;

  const Password: AnsiString): boolean; overload; static;

 

Delphi syntax (stream overload):

class function SignPDFWithPFX(

  InputStream: TStream;

  OutputStream: TStream;

  const PFXFilePath: string;

  const Password: AnsiString): boolean; overload; static;

 

Описание

SignPDFWithPFX е end-to-end PFX signing entry point, добавен във v2.119.27. Input PDF трябва вече да съдържа signature placeholder, издаден от THPDFPage.AddSignedSignatureField (или неговия PAdES wrapper) с subFilter adbe.pkcs7.detached. Методът:

 

1. Зарежда input PDF, намира /ByteRange + /Contents sentinel placeholder-ите и patch-ва /ByteRange с реалните byte offsets

2. Зарежда PFX файла и го дешифрира с подадената password. Поддържа се PBES2 с PBKDF2-HMAC-SHA-256 + AES-256-CBC (това е default за PFX файлове експортирани от OpenSSL 3.0+, Windows 11+ certutil и macOS Keychain Access). Legacy PBE-SHA1-3DES файловете повдигат диагностично предупреждение; прегенерирайте с openssl pkcs12 -export ... -keypbe AES-256-CBC -certpbe AES-256-CBC

3. Изчислява SHA-256 върху /ByteRange-covered document bytes и изгражда CMS SignedData (RFC 5652) DER blob, съдържащ X.509 certificate-а, signed attributes (contentType + messageDigest + signingTime) и RSA + SHA-256 signature върху SET-tagged signed attributes

4. Hex-encode-ва CMS DER, проверява че се побира в /Contents budget-а, резервиран от AddSignedSignatureField (default 8 KB покрива 1024 / 2048-bit RSA), и го инжектира в placeholder-а

5. Записва patch-натите bytes към output path или stream

 

Returns True on success. Raises EHPDFPFXError on a bad password, unsupported encryption profile, or malformed PFX; EHPDFCMSError when the input PDF lacks the expected placeholder or the CMS DER overflows the reserved /Contents budget; EHPDFRSAError on RSA key mismatch.

 

Typical workflow

 

Doc := THotPDF.Create(nil);

Doc.FileName := 'unsigned.pdf';

Doc.BeginDoc;

Doc.CurrentPage.AddSignedSignatureField(

  'Sig1', Rect(60, 60, 260, 90), 8192,

  'adbe.pkcs7.detached', 'Approved', 'Brussels', '', []);

Doc.EndDoc;

Doc.Free;

THotPDF.SignPDFWithPFX('unsigned.pdf', 'signed.pdf', 'mykey.pfx', 'mypassword');

 

Notes

Алгоритъмът на подписа е RSA + SHA-256 (1.2.840.113549.1.1.1 + 2.16.840.1.101.3.4.2.1). Идентификаторът на подписващия е IssuerAndSerialNumber, извлечен от X.509 сертификата. encapContentInfo е detached (eContent е пропуснат). Подписаните атрибути се сортират по възходящ DER byte string според RFC 5652 §5.4 преди да бъдат хеширани

 

За PAdES B-T / B-LT / B-LTA workflow-и, които се нуждаят от RFC 3161 timestamps, DSS dictionaries или document timestamp подписи, producer-side помощниците (AddPAdESSignatureField, AddPAdESDSSCertificate, AddDocumentTimestampSignature) продължават да важат; самият SignPDFWithPFX излъчва основен CMS-only подпис (еквивалент на PAdES-B-B)

 

See also: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField