Skip to Content
NexPDF

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 Rust.

English

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 Vec<u8>. Write those bytes as they are: saving the document object again afterwards breaks the signature. Signing takes &self; the document itself is not changed.

Sign with a PKCS#12 file

use nexpdf_sdk::Document;
use serde_json::json;

fn main() -> nexpdf_sdk::Result<()> {
    let doc = Document::open("contract.pdf")?;
    let options = json!({
        "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"}
    });
    let signed = doc.sign_pkcs12(&std::fs::read("signer.p12")?, "p12-password", &options.to_string())?;
    std::fs::write("contract-signed.pdf", &signed)?;
    Ok(())
}

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 is an Error::Engine with the message incorrect password. nexpdf_sdk::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:

let cert = std::fs::read("signer-cert.pem")?;          // signer certificate, then its chain

let prepared = doc.prepare_external_signature(&cert, r#"{"reason":"Approved"}"#)?;
let to_sign = nexpdf_sdk::external_signature_data(&prepared, &cert)?;

let signature: Vec<u8> = my_hsm.sign_sha256_rsa(&to_sign);   // your device or KMS

let signed = nexpdf_sdk::complete_external_signature(&prepared, &cert, &signature, 0, None)?;

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 last argument takes {"tsa_url": "...", "ltv": true} and also "level", so an external signature can be B-LTA too.

Long-term validation

nexpdf_sdk::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:

let ltv = nexpdf_sdk::add_ltv(&std::fs::read("contract-signed.pdf")?)?;

Verify signatures

let data = std::fs::read("contract-signed.pdf")?;
for s in nexpdf_sdk::verify_signatures_json_value(&data)?.as_array().into_iter().flatten() {
    let ok = s["digest_ok"].as_bool() == Some(true) && s["signature_ok"].as_bool() == Some(true);
    println!("{} {} {}", s["signer"], s["level"], if ok { "valid" } else { "INVALID" });
}

let roots = std::fs::read_to_string("company-roots.pem")?;
let report = nexpdf_sdk::verify_signatures_with_roots_json_value(&data, &roots)?;

The *_json_value functions need the serde feature; verify_signatures_json and verify_signatures_with_roots_json return the same JSON as a String. 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.

On this page