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 Android.
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 a ByteArray. Write those bytes as they
are: saving the document object again afterwards breaks the signature. An empty array means the
call failed; NexPdfJni.lastError() says why.
Sign with a PKCS#12 file
import com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
val 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" }
}
"""
val signed = NexPdfDocument.openBytes(contract).use { doc ->
NexPdfJni.signPkcs12(doc.handle(), p12Bytes, "p12-password", options)
}
check(signed.isNotEmpty()) { NexPdfJni.lastError() }
File(filesDir, "contract-signed.pdf").writeBytes(signed)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 fails with incorrect password. NexPdfJni.pkcs12InfoJson(p12, password) shows the
certificate.
For tests, NexPdfJni.signPades(handle, 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:
val cert = certPem // signer certificate (PEM or DER), then its chain
val prepared = NexPdfDocument.openBytes(contract).use { doc ->
doc.prepareExternalSignature(cert, """{"reason":"Approved"}""")
}
val toSign = NexPdfDocument.externalSignatureData(prepared, cert)
val signature = myHsm.signSha256Rsa(toSign) // your device or KMS
val signed = NexPdfDocument.completeExternalSignature(prepared, cert, signature, 0, null)
check(signed.isNotEmpty()) { NexPdfJni.lastError() }The same three calls are NexPdfJni.prepareExternalSignature(handle, cert, optionsJson),
NexPdfJni.externalSignatureData and NexPdfJni.completeExternalSignature. The 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 last argument takes
{"tsa_url": "...", "ltv": true} and also "level", so an external signature can be B-LTA too.
Long-term validation
NexPdfJni.addLtv(signedBytes) adds the OCSP responses and CRLs (a DSS) to a signed file so the
signature can be validated after the certificates expire:
val ltv = NexPdfJni.addLtv(signed)Verify signatures
val sigs = JSONArray(NexPdfJni.verifySignaturesJson(signed))
for (i in 0 until sigs.length()) {
val s = sigs.getJSONObject(i)
val ok = s.optBoolean("digest_ok") && s.optBoolean("signature_ok")
Log.i("NexPDF", "${s.optString("signer")} ${s.optString("level")} ${if (ok) "valid" else "INVALID"}")
}
val report = NexPdfJni.verifySignaturesWithRootsJson(signed, rootsPem) // JSON textEach 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:
NexPdfJni.addInkSignature(handle, page, color, width, strokes). See
Annotations. The reader has a signature
pad.