THotPDF.SignPDFWithPFX

THotPDF

 

Anterior  Métodos  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;

 

Descripción

SignPDFWithPFX es el punto de entrada de firma PFX end-to-end agregado en v2.119.27. El PDF de entrada ya debe contener un placeholder de firma emitido por THPDFPage.AddSignedSignatureField (o su wrapper PAdES) con subFilter adbe.pkcs7.detached. El método:

 

1. Carga el PDF de entrada, localiza los placeholders centinela /ByteRange + /Contenido y parchea /ByteRange con los offsets de bytes reales

2. Carga el archivo PFX y lo descifra con la contraseña suministrada. Se admite PBES2 con PBKDF2-HMAC-SHA-256 + AES-256-CBC (es el valor predeterminado para archivos PFX exportados por OpenSSL 3.0+, certutil de Windows 11+ y macOS Keychain Access). Los archivos heredados PBE-SHA1-3DES generan un diagnóstico; vuelva a exportar con openssl pkcs12 -export ... -keypbe AES-256-CBC -certpbe AES-256-CBC

3. Calcula SHA-256 sobre los bytes del documento cubiertos por /ByteRange y construye un blob DER CMS SignedData (RFC 5652) que contiene el certificado X.509, los atributos firmados (contentType + messageDigest + signingTime) y una firma RSA + SHA-256 sobre los atributos firmados etiquetados como SET

4. Codifica en hexadecimal el DER CMS, verifica que quepa en el presupuesto /Contenido reservado por AddSignedSignatureField (8 KB predeterminados cubren RSA de 1024 / 2048 bits) y lo inyecta en el 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 /Contenido budget; EHPDFRSAError on RSA key mismatch.

 

Flujo de trabajo típico

 

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

El algoritmo de firma es RSA + SHA-256 (1.2.840.113549.1.1.1 + 2.16.840.1.101.3.4.2.1). El identificador del firmante es IssuerAndSerialNumber extraído del certificado X.509. encapContentInfo está separado (eContent omitido). Los atributos firmados se ordenan por cadena de bytes DER ascendente según RFC 5652 §5.4 antes de calcular el hash

 

Para flujos PAdES B-T / B-LT / B-LTA que necesitan timestamps RFC 3161, diccionarios DSS o firmas de timestamp de documento, siguen aplicando los helpers del lado productor (AddPAdESSignatureField, AddPAdESDSSCertificate, AddDocumentTimestampSignature); SignPDFWithPFX en sí emite una firma básica solo CMS (equivalente PAdES-B-B)

 

Véase también: AddSignedSignatureField, PreparePDFForSigning, InsertSignatureHex, AddPAdESSignatureField