Digital signatures
Sign PDFs with PAdES using a PKCS#12 file or an external device, add long-term validation, and verify signatures with NexPDF for .NET.
License module
Signing, LTV and verification (with or without your own roots) need the signatures module
(Business tier and above).
Every signing call returns the complete signed file as byte[]. Write those bytes as they are:
saving the document object again afterwards breaks the signature.
Sign with a PKCS#12 file
using System.Text.Json;
using Nexdevit.NexPdf;
var options = JsonSerializer.Serialize(new
{
reason = "Approved",
location = "Lisbon",
tsa_url = "http://timestamp.example.com", // optional: PAdES B-T
ltv = true, // optional: PAdES B-LT
appearance = new { page = 0, rect = new[] { 360, 60, 560, 120 }, text = "Signed by Ana Silva" },
});
using var doc = NexPdfDocument.Open("contract.pdf");
var signed = doc.SignPkcs12(File.ReadAllBytes("signer.p12"), "p12-password", options);
File.WriteAllBytes("contract-signed.pdf", signed);Options: name, reason, location, contact, field (an existing signature field), tsa_url,
ltv, level (B-B, B-T, B-LT or B-LTA, see
Long-term signatures), timestamp (the B-LTA document
timestamp's hash, field and reserved) and appearance (page, rect, ink strokes,
text). A wrong password throws
NexPdfException with the message "incorrect password". NexPdfSdk.Pkcs12InfoJson(p12, password)
shows the certificate.
For tests, doc.SignPades(signer, reason, location) signs with a new self-signed certificate.
Sign with an HSM, smart card or cloud key
External signing works in three calls with no state kept between them:
var cert = File.ReadAllBytes("signer-cert.pem"); // signer certificate, then its chain
var prepared = doc.PrepareExternalSignature(cert, """{"reason":"Approved"}""");
var toSign = NexPdfSdk.ExternalSignatureData(prepared, cert);
var signature = myHsm.SignSha256Rsa(toSign); // your device or KMS
var signed = NexPdfSdk.CompleteExternalSignature(prepared, cert, signature, 0);algorithm is 0 for RSA PKCS#1 v1.5 with SHA-256 or 1 for ECDSA with SHA-256 (DER or raw
r||s). Devices that take a digest sign the SHA-256 of toSign. The optional last argument takes
{"tsa_url": "...", "ltv": true} and also "level", so an external signature can be B-LTA too.
With an X509Certificate2 that has an RSA private key (for example from the Windows certificate
store), cert.GetRSAPrivateKey()!.SignData(toSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)
produces the signature for algorithm 0.
Long-term validation
NexPdfSdk.AddLtv(signedBytes) adds the OCSP responses and CRLs (a DSS) to a signed file so the
signature can be validated after the certificates expire:
var ltv = NexPdfSdk.AddLtv(File.ReadAllBytes("contract-signed.pdf"));Verify signatures
using System.Text.Json.Nodes;
var data = File.ReadAllBytes("contract-signed.pdf");
foreach (var s in JsonNode.Parse(NexPdfSdk.VerifySignaturesJson(data))!.AsArray())
{
var ok = s!["digest_ok"]!.GetValue<bool>() && s["signature_ok"]!.GetValue<bool>();
Console.WriteLine($"{s["signer"]} {s["level"]} {(ok ? "valid" : "INVALID")}");
}
var roots = File.ReadAllText("company-roots.pem");
var report = JsonNode.Parse(NexPdfSdk.VerifySignaturesWithRootsJson(data, roots));Each entry includes field, signer, subject, digest_ok, signature_ok,
covers_whole_document, reason, signing_time, timestamp_ok, chain_ok, trusted,
revocation, ltv, level, pades_level (B-B, B-T, B-LT or B-LTA) and
document_timestamp (true for a document timestamp, whose signer is the timestamp authority).
Long-term archival
For PAdES B-LTA, document timestamps and archive timestamp renewal, see Long-term signatures.
Handwritten signatures
A drawn signature without cryptography is an ink annotation:
doc.AddInkSignature(page, color, width, strokes). See Annotations.