Builder

Native builder for a detached CAdES-BES CMS SignedData, suitable for a PAdES B-B signature (/SubFilter /ETSI.CAdES.detached). It assembles the SignerInfo with the mandatory signed attributes (content-type, message-digest, signing-time, and the ESS signing-certificate-v2 that plain openssl_pkcs7_sign() cannot add), signs the DER SET OF signed attributes with openssl_sign(), and encodes the ContentInfo. RSA and ECDSA keys are supported with SHA-256/384/512.

Namespace: Com\Tecnick\Pdf\Sign\Cms

Final: Yes

File: Builder.php line 50

Native builder for a detached CAdES-BES CMS SignedData, suitable for a PAdES B-B signature (/SubFilter /ETSI.CAdES.detached). It assembles the SignerInfo with the mandatory signed attributes (content-type, message-digest, signing-time, and the ESS signing-certificate-v2 that plain openssl_pkcs7_sign() cannot add), signs the DER SET OF signed attributes with openssl_sign(), and encodes the ContentInfo. RSA and ECDSA keys are supported with SHA-256/384/512.

sign() covers the case where the private key is available in this process. When it is not, or when the content is too large to hold as a string, the two halves are available on their own: signaturePayload() returns the bytes a signer has to sign for a given SigningRequest, and buildFromSignature() turns those plus the signature into the CMS. sign() is implemented over both.

Tags

Methods

__construct()

File: Builder.php line 69

public __construct([Asn1|null $asn1 = null ][, Certificate|null $certificate = null ]) : mixed

Parameters

buildFromSignature()

File: Builder.php line 187

public buildFromSignature(SigningRequest $request, string $signature[, array<int, string> $chainCertsDer = [] ][, callable(string): string|null $signatureTimestamp = null ][, string|SignatureEncoding $signatureEncoding = SignatureEncoding::Der ]) : string

The second half of sign(), for a signature produced elsewhere. The signature AlgorithmIdentifier is read from the signing certificate rather than from a private key, since there may be none in this process.

The signature is verified against the certificate before anything is emitted, so a signature over the wrong bytes, from the wrong key, or in the wrong encoding fails at the call.

Parameters

  • $request : SigningRequest: The same request passed to signaturePayload().
  • $signature : string: Signature over $this->signaturePayload($request).
  • $chainCertsDer : array<int, string> = []: Additional certificates to embed, each as PEM or as DER.
  • $signatureTimestamp : callable(string): string|null = null: Optional provider that receives the raw SignerInfo signature bytes and returns a DER-encoded RFC 3161 timestamp token (ContentInfo). When supplied, the token is embedded as the id-aa-signatureTimeStampToken unsigned attribute (PAdES B-T).
  • $signatureEncoding : string|SignatureEncoding = SignatureEncoding::Der: Encoding of $signature. An ECDSA signature returned as the fixed-width r || s concatenation is converted to the DER form CMS requires when this is P1363.

Tags

  • throws: Exception If the digest or certificate key type is unsupported, if the signature is empty, malformed, or does not verify, or if encoding fails.

Return values

string —

DER-encoded CMS ContentInfo.

sign()

File: Builder.php line 101

public sign(string $data, string $signerCertDer, OpenSSLAsymmetricKey $privateKey, array<int, string> $chainCertsDer, string|DigestAlgorithm $digestAlgorithm, int $signingTime[, callable(string): string|null $signatureTimestamp = null ][, bool $includeSigningTime = true ][, array<string|int, string> $extraSignedAttributes = [] ]) : string

Parameters

  • $data : string: Detached content bytes (the signed data).
  • $signerCertDer : string: DER of the signing certificate.
  • $privateKey : OpenSSLAsymmetricKey: Signing private key (RSA or EC).
  • $chainCertsDer : array<int, string>: Additional certificates to embed, each as PEM or as DER. Every entry is parsed.
  • $digestAlgorithm : string|DigestAlgorithm: Digest algorithm name or enum case.
  • $signingTime : int: Unix timestamp for the signing-time attribute.
  • $signatureTimestamp : callable(string): string|null = null: Optional provider that receives the raw SignerInfo signature bytes and returns a DER-encoded RFC 3161 timestamp token (ContentInfo). When supplied, the token is embedded as the id-aa-signatureTimeStampToken unsigned attribute (PAdES B-T).
  • $includeSigningTime : bool = true: Whether to add the CMS signing-time signed attribute. The legacy (ISO 32000-1) profile includes it; PAdES-BASELINE forbids it (ETSI EN 319 142-1) and carries the time in the /M signature dictionary entry instead.
  • $extraSignedAttributes : array<string|int, string> = []: Additional signed attributes as OID => DER-encoded attribute value, for a profile that requires one such as the CAdES signature-policy-identifier.

Tags

  • throws: Exception If the digest or key is unsupported, or signing fails.

Return values

string —

DER-encoded CMS ContentInfo.

signaturePayload()

File: Builder.php line 155

public signaturePayload(SigningRequest $request) : string

The first half of sign() on its own, for a signer whose private key this process cannot reach: a hardware token, a smart card, or a remote signing service. It also serves a signer that holds the key but not the content, since the request carries the message digest rather than the content.

The result is the DER SET OF signed attributes defined by RFC 5652 section 5.4, which is what the signature covers. It is a pure function of the request, so buildFromSignature() derives the same bytes again rather than taking them from the caller.

Parameters

  • $request : SigningRequest: Validated inputs for the signed attributes.

Tags

  • throws: Exception If the digest is unsupported or encoding fails.

Return values

string —

DER SET OF signed attributes, ready to be signed.