PDFium Delphi Component Docs

FPdfCms unit

Unit: FPdfCms
Builds RFC 5652 CMS / CAdES SignedData structures, validates signature policy documents, and embeds optional RFC 3161 timestamps

Syntax

TCmsDigestAlgorithm = (cdaSha256, cdaSha3_256, cdaSha3_384, cdaSha3_512);

TCmsSignerIdKind = (sidIssuerAndSerial, sidSubjectKeyIdentifier);

TCmsSignerId = record
  Kind: TCmsSignerIdKind;
  IssuerDer: TBytes;
  SerialDer: TBytes;
  SubjectKeyId: TBytes;
end;

ICmsSigner = interface
  function GetDigestAlgorithmOid: AnsiString;
  function GetSignatureAlgorithmOid: AnsiString;
  function GetSignatureAlgorithmParams: TBytes;
  function GetSignerId: TCmsSignerId;
  function SignSignedAttrsDigest(const Digest: TSHA256Digest): TBytes;
end;

ICmsSignerEx = interface
  function SignDigest(const Digest: TBytes): TBytes;
end;

TCmsBuildOptions = record
  SigningTimeUtc: TDateTime;
  CommitmentTypeOid: AnsiString;
  SignaturePolicyOid: AnsiString;
  SignaturePolicyHash: TBytes;
  SignaturePolicyUri: AnsiString;
  SignaturePolicyDocument: TBytes;
  class function Default: TCmsBuildOptions; static;
end;
function CertKeyAlgorithmOid(const CertDer: TBytes): AnsiString;
function ExtractIssuerAndSerial(const CertDer: TBytes;
  out IssuerTlv, SerialTlv: TBytes): Boolean;
function EcDsaP1363ToDer(const Raw: TBytes): TBytes;

function VerifyCmsSignaturePolicyDocument(const Options: TCmsBuildOptions;
  const PolicyDocument: TBytes; out ErrorText: string): Boolean;
function CmsDigestOid(Algorithm: TCmsDigestAlgorithm): AnsiString;
function CmsDigestByteLength(Algorithm: TCmsDigestAlgorithm): Integer;
function CmsDigestAlgorithmOfOid(const Oid: AnsiString;
  out Algorithm: TCmsDigestAlgorithm): Boolean;
function CmsDigestOf(const Data: TBytes;
  Algorithm: TCmsDigestAlgorithm): TBytes;
function CmsDigestOfRanges(const Data: TBytes; Offset1, Length1, Offset2,
  Length2: Int64; Algorithm: TCmsDigestAlgorithm): TBytes;
function BuildSignedData(const DetachedDigest: TSHA256Digest;
  const Signer: ICmsSigner; const SignerCertDer: TBytes;
  const ChainCertDer: array of TBytes;
  const Options: TCmsBuildOptions): TBytes;
function BuildSignedDataEx(const DetachedDigest: TBytes;
  const Signer: ICmsSigner; const SignerCertDer: TBytes;
  const ChainCertDer: array of TBytes;
  const Options: TCmsBuildOptions): TBytes;
function ExtractSignatureValue(const CmsDer: TBytes;
  out SignatureValueOctets: TBytes): Boolean;
procedure AddSignatureTimestampToCms(var CmsDer: TBytes;
  const TimestampTokenDer: TBytes);

Description

FPdfCms is the low-level CMS builder consumed internally by the PAdES signature path. It lets a power user assemble a CMS SignedData value from a SHA-256 message digest of the signed attributes, a signer abstraction, the signer certificate and an optional certificate chain

The ICmsSigner interface is the crypto-backend contract: the caller supplies the digest and signature algorithm OIDs, the signer identifier and performs the actual private-key operation over the signed-attributes digest. This decouples CMS encoding from any specific crypto provider

TCmsDigestAlgorithm names the digest family a signature may declare: SHA-256, plus the SHA-3 members ISO/TS 32001 adds to PDF 2.0. BuildSignedDataEx builds a SignedData for any of them and keeps the declared digestAlgorithm, the messageDigest attribute length, the signer's signatureAlgorithm and the ESSCertIDv2 certHash consistent, whilst BuildSignedData remains the SHA-256 entry point. ICmsSignerEx is the digest-agnostic companion of ICmsSigner: it is a separate interface so a backend written before SHA-3 existed still satisfies the base contract, and only a backend able to produce such a signature has to answer SignDigest

TCmsBuildOptions can add signing time, commitment type and an explicit signature policy identifier with SHA-256 policy hash, URI and optional in-memory policy document

ExtractSignatureValue pulls the raw signature octets out of a finished CMS value so they can be sent to a timestamp authority; AddSignatureTimestampToCms then embeds the returned timestamp token as an unsigned attribute, producing the B-T/B-LTA compliant form

ICmsSigner methods

MethodDescription
GetDigestAlgorithmOidReturns the OID of the hash algorithm used over the signed attributes (for example 2.16.840.1.101.3.4.2.1 for SHA-256)
GetSignatureAlgorithmOidReturns the OID of the signature algorithm applied to the digest
GetSignatureAlgorithmParamsReturns algorithm parameters (often empty for ECDSA; carries the RSA-PSS parameters blob for PSS)
GetSignerIdReturns the TCmsSignerId identifying the signer by issuer/serial or by subject key identifier
SignSignedAttrsDigestPerforms the private-key signature over the supplied digest and returns the signature octets

ICmsSignerEx methods

MethodDescription
SignDigestPerforms the private-key signature over an already-computed digest of any supported length, which is what a SHA-3 SignerInfo needs; the digest length must equal CmsDigestByteLength of the OID the signer declares

Functions and procedures

RoutineDescription
CertKeyAlgorithmOidReads the SubjectPublicKeyInfo algorithm OID out of a certificate, which is what decides between an RSA and an ECDSA signature algorithm; every signing backend needs it, so it lives here rather than inside one of them
ExtractIssuerAndSerialExtracts the raw issuer and serialNumber TLVs that a SignerIdentifier carries, into IssuerTlv and SerialTlv
EcDsaP1363ToDerWraps a raw P1363 r||s ECDSA signature into the DER SEQUENCE of two INTEGERs that CMS expects; tokens and platform crypto APIs hand back the raw form
VerifyCmsSignaturePolicyDocumentRequires a non-empty SignaturePolicyOid and a 32-byte SignaturePolicyHash, then verifies the supplied policy document with SHA-256 and returns diagnostic text on failure
BuildSignedDataBuilds a complete detached RFC 5652 CMS ContentInfo with one CAdES SignerInfo, signer certificate, optional chain and configured signed attributes
CmsDigestOidReturns the object identifier a signature places in digestAlgorithm for the given family member: 2.16.840.1.101.3.4.2.1 for SHA-256 and the NIST-registered .8, .9 and .10 for SHA3-256, SHA3-384 and SHA3-512
CmsDigestByteLengthReturns the digest output length in octets (32, 32, 48 or 64), which is the length the messageDigest attribute must carry
CmsDigestAlgorithmOfOidReverse lookup used to identify the algorithm a signer declares; returns False for an OID outside the supported set, so a caller can reject it before encoding anything
CmsDigestOfOne-shot digest of a contiguous buffer with the requested algorithm
CmsDigestOfRangesDigests two discontiguous spans of the same buffer in order. A PDF signature covers everything except the /Contents hole, so this feeds both ByteRange spans through one context in bounded chunks instead of copying the whole document in order to hash it
BuildSignedDataExBuilds the same SignedData for any digest in TCmsDigestAlgorithm rather than SHA-256 only: the digest length must match the algorithm the signer declares, the signer must implement ICmsSignerEx unless that algorithm is SHA-256, and a mismatched declaration raises rather than writing a signature whose stated algorithms disagree
ExtractSignatureValueExtracts the raw SignerInfo signature octets used as the RFC 3161 timestamp message imprint input
AddSignatureTimestampToCmsAdds a full RFC 3161 TimeStampToken as the signature-time-stamp unsigned attribute and re-encodes enclosing lengths

Types

TypeDescription
TCmsSignerIdKindsidIssuerAndSerial selects issuer Name plus serial TLV and sidSubjectKeyIdentifier selects raw subject-key-identifier octets
TCmsDigestAlgorithmcdaSha256 is the PAdES baseline default; cdaSha3_256, cdaSha3_384 and cdaSha3_512 are the SHA-3 members ISO/TS 32001 adds to the algorithms a PDF 2.0 signature may declare
TCmsSignerIdKind, IssuerDer, SerialDer and SubjectKeyId carry the selected SignerInfo identifier form
TCmsBuildOptionsSigningTimeUtc, CommitmentTypeOid, SignaturePolicyOid, SignaturePolicyHash, SignaturePolicyUri and SignaturePolicyDocument configure signed attributes; Default clears every optional value

Remarks

See Also

FPdfTsa, FPdfCrypto, FPdfSha256, TPdf.SignPades