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 היא נקודת הכניסה מקצה לקצה לחתימת PFX שנוספה ב-v2.119.27. קובץ PDF הקלט חייב כבר להכיל signature placeholder שנפלט על ידי THPDFPage.AddSignedSignatureField, או עטיפת PAdES שלו, עם subFilter adbe.pkcs7.detached. המתודה:

 

1. טוענת את PDF הקלט, מאתרת placeholders sentinel של /ByteRange + /Contents, ומתקנת את /ByteRange עם offsets ה-bytes האמיתיים

2. טוענת את קובץ PFX ומפענחת אותו באמצעות הסיסמה שסופקה. נתמך PBES2 עם PBKDF2-HMAC-SHA-256 + AES-256-CBC, שהוא ברירת המחדל לקובצי PFX שמיוצאים על ידי OpenSSL 3.0+, ‏Windows 11+ certutil ו-macOS Keychain Access. קובצי PBE-SHA1-3DES legacy מעלים diagnostic; יצא מחדש עם openssl pkcs12 -export ... -keypbe AES-256-CBC -certpbe AES-256-CBC

3. מחשבת SHA-256 על bytes המסמך המכוסים על ידי /ByteRange ובונה blob ‏DER של CMS SignedData ‏(RFC 5652) המכיל את אישור X.509, את signed attributes ‏(contentType + messageDigest + signingTime), וחתימת RSA + SHA-256 על signed attributes המסומנים ב-SET

4. מקודדת את CMS DER ל-hex, מוודאת שהוא נכנס ל-budget של /Contents ששמור על ידי AddSignedSignatureField, כאשר ברירת מחדל של 8 KB מכסה RSA של 1024 / 2048-bit, ומזריקה אותו ל-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

אלגוריתם החתימה הוא 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 מושמט. signed attributes ממוינים לפי DER byte string עולה לפי RFC 5652 §5.4 לפני hashing

 

עבור תהליכי PAdES B-T / B-LT / B-LTA שצריכים RFC 3161 timestamps, מילוני DSS או חתימות document timestamp, עזרי producer-side כגון AddPAdESSignatureField, ‏AddPAdESDSSCertificate ו-AddDocumentTimestampSignature עדיין תקפים; SignPDFWithPFX עצמו פולט חתימה בסיסית של CMS בלבד, שקולה ל-PAdES-B-B

 

See also: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField