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 是 v2.119.27 新增的端對端 PFX 簽章入口點。輸入 PDF 必須已包含由 THPDFPage.AddSignedSignatureField(或其 PAdES wrapper)發出、subFilter 為 adbe.pkcs7.detached 的簽章 placeholder。此方法會:

 

1. 以有界的 256 KB 緩衝區複製輸入,或重用同一個可 seek 的可讀寫串流,找出 /ByteRange 與 /Contents placeholder,並以實際 byte offset 用 seek 修補固定寬度的 /ByteRange 欄位

2. 載入 PFX 檔案並以提供的 password 解密。PBES2 接受 AES-128-CBC、AES-192-CBC 與 AES-256-CBC,搭配預設的 HMAC-SHA-1,或明確指定的 HMAC-SHA-256、HMAC-SHA-384 與 HMAC-SHA-512 PBKDF2 PRF。選用的 PBKDF2 keyLength 必須與所選的 AES cipher 相符,舊版 PBE-SHA1-3DES 檔案仍支援較舊的憑證匯出。其他加密設定檔會產生帶出問題 OID 的診斷訊息

3. 依 public-key 材料與完全相符的 RFC 2985 localKeyId 值,把 RSA、named-curve ECDSA、Ed25519 或 Ed448 private key 與 certificate 配對,再依 issuer / subject 名稱及 Authority Key Identifier / Subject Key Identifier 擴充,保留可用的中繼與 root 鏈

4. 依配對到的金鑰選擇簽章方案、計算所需的文件摘要,並建立 CMS SignedData (RFC 5652) DER blob,內含 signer certificate、去除重複 DER 憑證後的對應鏈與 signed attributes。RSA 與 ECDSA 對 attribute digest 簽章;Ed25519 與 Ed448 以 PureEdDSA 模式對完整的 DER SET OF attributes 簽章。Ed25519 使用 SHA-512,Ed448 使用輸出 512-bit 的 SHAKE256 與 id-shake256-len

5. 把 CMS DER 轉成十六進位編碼,確認能放進 AddSignedSignatureField 預留的 /Contents 預算(預設 8 KB 足以涵蓋 1024 / 2048-bit RSA),再注入 placeholder

6. 把修補後的位元組寫到輸出路徑或串流

 

成功時傳回 True。password 錯誤、加密或 private-key 設定檔不支援、PFX 格式錯誤、身分不明確、issuer 不明確或憑證鏈成環時丟出 EHPDFPFXError;輸入 PDF 缺少預期 placeholder、選用的 EdDSA provider 不可用、簽章失敗,或 CMS DER 超出預留的 /Contents 預算時丟出 EHPDFCMSError;RSA 金鑰不相符時丟出 EHPDFRSAError

 

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');

 

Multi-identity PFX files

便利多載會刻意拒絕含有一個以上相符 certificate 與 private key 的 PFX。要檢視具決定性的身分清單及其 LocalKeyID 與 FriendlyName 欄位時,請呼叫 HPDFEnumeratePFXIdentities;只需要選定金鑰材料時,請呼叫 HPDFParsePFXByLocalKeyID

端對端簽章時,請以 HPDFCMSDefaultSignOptions 初始化 THPDFCMSSignOptions,把選定的精確 octets 複製到 Options.PFXLocalKeyID,再呼叫接受這些 options 的 HPDFCMSSignPDFStream 多載。選定的 leaf、可用的中繼憑證與可用的 root 會自動嵌入;呼叫端提供的 Options.AdditionalCertsDER 值會併入,並以完全相同的 DER 去重

 

Notes

RSA 與 ECDSA 的 PFX 簽章走 HotPDF 原生與 Windows 密碼學路徑。Ed25519 與 Ed448 的 PFX 簽章需要 OpenSSL 3 libcrypto;直接呼叫 HPDFCMSSignPDFStream 時可設定 THPDFCMSSignOptions.OpenSSLLibraryPath,便利多載則使用平台一般的程式庫搜尋路徑

簽章演算法為 RSA + SHA-256 (1.2.840.113549.1.1.1 + 2.16.840.1.101.3.4.2.1)。signer identifier 是從 X.509 certificate 擷取的 IssuerAndSerialNumber。encapContentInfo 為 detached(省略 eContent)。Signed attributes 會依 RFC 5652 §5.4,在雜湊前按 DER byte string 遞增排序

 

對於需要 RFC 3161 timestamps、DSS dictionaries 或 document timestamp signatures 的 PAdES B-T / B-LT / B-LTA 工作流程,producer-side helpers(AddPAdESSignatureField、AddPAdESDSSCertificate、AddDocumentTimestampSignature)仍然適用;SignPDFWithPFX 本身會輸出基礎的 CMS-only signature(等同 PAdES-B-B)

stream 多載從不會在記憶體中保留完整的第二份 PDF。它會以有界區段對修補後的輸出計算雜湊,並在不改變檔案長度的前提下以 seek 方式修補十六進位的 CMS 值。固定寬度的十進位位移可支援小於 10,000,000,000 bytes 的已備妥 PDF。

 

請參閱: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField