|
既存の PDF placeholder に PFX / PKCS#12 file を使って署名し、CMS SignedData container を構築して signed PDF を 1 回の呼び出しで書き込みます
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 で追加された end-to-end PFX signing entry point です。input PDF には、THPDFPage.AddSignedSignatureField (またはその PAdES wrapper) が subFilter adbe.pkcs7.detached で出力した signature placeholder がすでに含まれている必要があります。この method は次の処理を行います:
1. input PDF を読み込み、/ByteRange + /Contents sentinel placeholders を探し、実際の byte offsets で /ByteRange を patch します
2. PFX file を読み込み、指定された password で decrypt します。PBES2 with PBKDF2-HMAC-SHA-256 + AES-256-CBC を support します (これは OpenSSL 3.0+、Windows 11+ certutil、macOS Keychain Access が export する PFX files の default です)。legacy PBE-SHA1-3DES files は diagnostic を発生させます。openssl pkcs12 -export ... -keypbe AES-256-CBC -certpbe AES-256-CBC で再 export してください
3. /ByteRange が cover する document bytes に対して SHA-256 を計算し、X.509 certificate、signed attributes (contentType + messageDigest + signingTime)、SET-tagged signed attributes に対する RSA + SHA-256 signature を含む CMS SignedData (RFC 5652) DER blob を構築します
4. CMS DER を hex-encode し、AddSignedSignatureField が予約した /Contents budget に収まることを確認します (default 8 KB は 1024 / 2048-bit RSA を cover します)。その後 placeholder に注入します
5. patch 済み bytes を output path または stream に書き込みます
成功時は True を返します。bad password、unsupported encryption profile、malformed PFX では EHPDFPFXError、input PDF に expected placeholder がない場合または CMS DER が reserved /Contents budget を超える場合は EHPDFCMSError、RSA key mismatch では EHPDFRSAError を発生させます
典型的な 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 は X.509 certificate から抽出した IssuerAndSerialNumber です。encapContentInfo は detached (eContent omitted) です。signed attributes は hash 前に RFC 5652 sec 5.4 に従い ascending DER byte string で sort されます
RFC 3161 timestamps、DSS dictionaries、document timestamp signatures が必要な PAdES B-T / B-LT / B-LTA workflows では、producer-side helpers (AddPAdESSignatureField、AddPAdESDSSCertificate、AddDocumentTimestampSignature) を引き続き使用します。SignPDFWithPFX 自体は basic CMS-only signature (PAdES-B-B equivalent) を出力します
関連項目: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField
|