endesive is a Python library for creating and verifying digital signatures in:
- PDF documents (CMS/CAdES detached signatures, Adobe-compatible)
- S/MIME messages (sign, verify, encrypt, decrypt)
- Plain data streams (CMS detached signatures)
- XML documents (XAdES BES/T, enveloped and enveloping)
The project also provides PKCS#11/HSM integration and a large set of runnable examples.
- PDF signing and verification with optional TSA timestamping and OCSP data
- S/MIME signing/verification and envelope encryption/decryption
- CMS detached signatures for arbitrary binary/text payloads
- XAdES BES/T generation for XML workflows
- Hardware-backed signing through PKCS#11 (PyKCS11) and SSH agent support (paramiko)
- Verification result model exposing signature, hash, certificate, OCSP and TSP statuses
Python requirement: 3.11+
pip install endesiveFrom source:
pip install -e .Development extras:
pip install -e ".[dev,docs]"endesive.pdfendesive.pdf.cms.sign(...)endesive.pdf.verify(...)endesive.pdf.PDFVerifier
endesive.emailendesive.email.sign(...)endesive.email.verify(...)endesive.email.encrypt(...)endesive.email.decrypt(...)
endesive.plainendesive.plain.sign(...)endesive.plain.verify(...)
endesive.xadesendesive.xades.BES
endesive.hsmendesive.hsm.HSM
import datetime
from cryptography.hazmat import backends
from cryptography.hazmat.primitives.serialization import pkcs12
from endesive.pdf import cms
with open("cert.p12", "rb") as fp:
key, cert, cert_chain = pkcs12.load_key_and_certificates(
fp.read(), b"password", backends.default_backend()
)
with open("input.pdf", "rb") as fp:
pdf_data = fp.read()
signing_date = datetime.datetime.now(datetime.UTC).strftime("D:%Y%m%d%H%M%S+00'00'")
pdf_signature_meta = {
"sigpage": 0,
"reason": "Approval",
"location": "HQ",
"contact": "security@example.com",
"signingdate": signing_date,
}
signature_block = cms.sign(
pdf_data,
pdf_signature_meta,
key,
cert,
cert_chain or [],
"sha256",
)
with open("output-signed.pdf", "wb") as fp:
fp.write(pdf_data)
fp.write(signature_block)from endesive import pdf
trusted_certs = [open("ca-root.pem", "rb").read()]
signed_pdf = open("output-signed.pdf", "rb").read()
result, unsigned_tail = pdf.verify(signed_pdf, trusted_certs)
print("signature ok:", result.signatureok)
print("hash ok:", result.hashok)
print("certificate ok:", result.certok)
print("ocsp ok:", result.ocspok)
print("tsp ok:", result.tspok)
print("remaining unsigned data:", unsigned_tail is not None)from endesive import plain
# detached_sig = plain.sign(data_bytes, private_key, certificate, cert_chain)
# verify_result = plain.verify(detached_sig, data_bytes, trusted_certs)from endesive import email
# signed_message = email.sign(data_bytes, private_key, certificate, cert_chain)
# verify_result = email.verify(signed_message_str, trusted_certs)
# encrypted_message = email.encrypt(data_bytes, recipient_certs, "aes256_cbc")
# decrypted_bytes = email.decrypt(encrypted_message_str, private_key)from endesive.xades import BES
# bes = BES()
# xml_tree = bes.enveloped(data, cert, cert_bytes, signproc, tsp_url, tsp_credentials)
# xml_tree = bes.enveloping(...)For complete XML flows, use the executable examples listed below.
The examples directory contains end-to-end scripts for certificate creation, data generation, signing, verification, HSM usage, timestamps, and advanced PDF signature appearance customization.
Useful starting points:
- Setup and test fixtures:
examples/make-cert-ca.pyexamples/make-cert-hsm.pyexamples/make-pdf.pyexamples/make-plain.pyexamples/make-smime.pyexamples/make-xml.py
- PDF:
examples/pdf-sign-cms.pyexamples/pdf-verify.pyexamples/pdf-timestamp-cms.py
- Plain/CMS:
examples/plain-sign-attr.pyexamples/plain-sign-noattr.pyexamples/plain-verify.py
- S/MIME:
examples/smime-sign-attr.pyexamples/smime-encrypt.pyexamples/smime-decrypt.pyexamples/smime-verify.py
- XAdES:
examples/xml-hsm-certum-enveloped.pyexamples/xml-hsm-certum-enveloping.py
Recommended execution order:
- Generate certificates.
- Generate input documents/messages.
- Run signing scripts.
- Run corresponding verification scripts.
PDF validators:
Signature validation services:
- https://ec.europa.eu/cefdigital/DSS/webapp-demo/validation
- https://pkitools.net/pages/validator/pdf.html
Run tests:
make testRun type checks:
make mypyBuild docs stubs:
make docsRuntime dependencies (from pyproject.toml):
cryptographyasn1cryptocertvalidatorlxmlpykcs11Pillowrequestsparamikopypdfattrs
MIT, with additional license files for bundled components:
LICENSELICENSE.pdf-annotateLICENSE.pyfpdf