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 iOS (Swift).

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 Data. Write that data as it is: saving the document object again afterwards breaks the signature.

Sign with a PKCS#12 file

import NexPdf

let doc = try NexPdfDocument.open(url: contractURL)
let p12 = try Data(contentsOf: p12URL)
let options = """
{
  "reason": "Approved",
  "location": "Lisbon",
  "tsa_url": "http://timestamp.example.com",
  "ltv": true,
  "appearance": { "page": 0, "rect": [360, 60, 560, 120], "text": "Signed by Ana Silva" }
}
"""
let signed = try doc.signPkcs12(p12: p12, password: "p12-password", optionsJson: options)
try signed.write(to: signedURL)

Options: name, reason, location, contact, field (an existing signature field), tsa_url (PAdES B-T), ltv (PAdES B-LT), 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 NexPdfError with the message incorrect password. NexPdf.pkcs12InfoJson(data: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:

let cert = try Data(contentsOf: certURL)            // signer certificate (PEM), then its chain

let prepared = try doc.prepareExternalSignature(cert: cert, optionsJson: #"{"reason":"Approved"}"#)
let toSign = try NexPdf.externalSignatureData(prepared: prepared, cert: cert)

let signature = try myKey.signSHA256(toSign)         // your device or KMS

let signed = try NexPdf.completeExternalSignature(prepared: prepared, cert: cert,
                                                  signature: signature, algorithm: 0, optionsJson: nil)

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

Long-term validation

NexPdf.addLtv(data:) adds the OCSP responses and CRLs (a DSS) to a signed file so the signature can be validated after the certificates expire:

let ltv = try NexPdf.addLtv(data: try Data(contentsOf: signedURL))

Verify signatures

let data = try Data(contentsOf: signedURL)
let sigs = NexPdf.json(try NexPdf.verifySignaturesJson(data: data)) as? [[String: Any]] ?? []
for s in sigs {
    let ok = (s["digest_ok"] as? Bool ?? false) && (s["signature_ok"] as? Bool ?? false)
    print(s["signer"] ?? "", s["level"] ?? "", ok ? "valid" : "INVALID")
}

let roots = try String(contentsOf: rootsURL, encoding: .utf8)
let report = NexPdf.json(try NexPdf.verifySignaturesWithRootsJson(data: data, rootsPem: 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: addInkSignature(pageIndex:colorHex:strokeWidth:strokes:). See Annotations. The reader has a signature pad for finger and Apple Pencil.

On this page