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 C++.
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 std::vector<uint8_t>. Write those bytes
as they are: saving the document object again afterwards breaks the signature.
Sign with a PKCS#12 file
#include <nexpdf.hpp>
#include <nlohmann/json.hpp> // an example; use your JSON library
namespace nexpdf = nexdevit::nexpdf;
auto doc = nexpdf::Document::open("contract.pdf");
nlohmann::json options = {
{"reason", "Approved"},
{"location", "Lisbon"},
{"tsa_url", "http://timestamp.example.com"}, // optional: PAdES B-T
{"ltv", true}, // optional: PAdES B-LT
{"appearance", {{"page", 0}, {"rect", {360, 60, 560, 120}}, {"text", "Signed by Ana Silva"}}},
};
auto signed_pdf = doc.sign_pkcs12(read_file("signer.p12"), "p12-password", options.dump());
write_file("contract-signed.pdf", signed_pdf);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
nexdevit::nexpdf::Error with the message incorrect password.
nexpdf::pkcs12_info_json(p12, password) shows the certificate.
For tests, doc.sign_pades(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:
auto cert = read_file("signer-cert.pem"); // signer certificate, then its chain
auto prepared = doc.prepare_external_signature(cert, R"({"reason":"Approved"})");
auto to_sign = nexpdf::external_signature_data(prepared, cert);
std::vector<uint8_t> signature = my_hsm.sign_sha256_rsa(to_sign); // your device or KMS
auto signed_pdf = nexpdf::complete_external_signature(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 to_sign. The optional last argument takes
{"tsa_url": "...", "ltv": true} and also "level", so an external signature can be B-LTA too.
Long-term validation
nexpdf::add_ltv(signed_bytes) adds the OCSP responses and CRLs (a DSS) to a signed file so the
signature can be validated after the certificates expire:
auto ltv = nexpdf::add_ltv(read_file("contract-signed.pdf"));Verify signatures
auto data = read_file("contract-signed.pdf");
for (const auto& s : nlohmann::json::parse(nexpdf::verify_signatures_json(data))) {
bool ok = s["digest_ok"].get<bool>() && s["signature_ok"].get<bool>();
std::cout << s["signer"] << " " << s["level"] << " " << (ok ? "valid" : "INVALID") << "\n";
}
auto report = nlohmann::json::parse(
nexpdf::verify_signatures_with_roots_json(data, read_text("company-roots.pem")));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.add_ink_signature(page, color, width, strokes). See Annotations.