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 เป็น entry point สำหรับ PFX signing แบบ end-to-end ที่เพิ่มใน v2.119.27 input PDF ต้องมี signature placeholder ที่ THPDFPage.AddSignedSignatureField หรือ PAdES wrapper ของมันสร้างไว้แล้วโดยใช้ subFilter adbe.pkcs7.detached method นี้ทำงานดังนี้:

 

1. โหลด input PDF หา sentinel placeholders /ByteRange + /Contents และ patch /ByteRange ด้วย byte offsets จริง

2. โหลด PFX file และ decrypt ด้วย password ที่ให้มา รองรับ PBES2 with PBKDF2-HMAC-SHA-256 + AES-256-CBC ซึ่งเป็น default สำหรับ PFX files ที่ export โดย OpenSSL 3.0+, Windows 11+ certutil, และ macOS Keychain Access ส่วน legacy PBE-SHA1-3DES files จะให้ diagnostic ให้ re-export ด้วย openssl pkcs12 -export ... -keypbe AES-256-CBC -certpbe AES-256-CBC

3. คำนวณ SHA-256 เหนือ document bytes ที่ /ByteRange cover แล้วสร้าง 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 reserve ไว้ได้ ค่า default 8 KB ครอบคลุม 1024 / 2048-bit RSA แล้ว inject เข้า placeholder

5. Writes the patched bytes to the output path or 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

signature algorithm คือ RSA + SHA-256 (1.2.840.113549.1.1.1 + 2.16.840.1.101.3.4.2.1) signer identifier คือ IssuerAndSerialNumber ที่ extract จาก X.509 certificate encapContentInfo เป็น detached โดย omit eContent signed attributes ถูก sort ตาม ascending DER byte string ตาม RFC 5652 sec 5.4 ก่อน hash

 

สำหรับ PAdES B-T / B-LT / B-LTA workflows ที่ต้องใช้ RFC 3161 timestamps, DSS dictionaries, หรือ document timestamp signatures ให้ใช้ producer-side helpers (AddPAdESSignatureField, AddPAdESDSSCertificate, AddDocumentTimestampSignature) ต่อไป SignPDFWithPFX เองจะสร้าง basic CMS-only signature เท่านั้น ซึ่งเทียบเท่า PAdES-B-B

 

See also: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField