Python XML Signature and XAdES library
Actively maintained. Last release 8 days ago, commits in 7 of the last 26 weeks. A safe choice.
$
pip install signxmlSignXML: XML Signature and XAdES in Python
==========================================
*SignXML* is an implementation of the W3C `XML Signature <http://en.wikipedia.org/wiki/XML_Signature>`_ standard in
Python. This standard (also known as "XMLDSig") is used to provide payload security in `SAML 2.0
<http://en.wikipedia.org/wiki/SAML_2.0>`_, `XAdES <https://en.wikipedia.org/wiki/XAdES>`_, `EBICS
<https://en.wikipedia.org/wiki/Electronic_Banking_Internet_Communication_Standard>`_, and `WS-Security
<https://en.wikipedia.org/wiki/WS-Security>`_, among other uses. The standard is defined in the `W3C Recommendation
<https://www.w3.org/standards/types#REC>`_ `XML Signature Syntax and Processing Version 1.1
<http://www.w3.org/TR/xmldsig-core1/>`_. *SignXML* implements all of the required components of the Version 1.1
standard, and most recommended ones. Its features are:
* Use of a libxml2-based XML parser configured to defend against
`common XML attacks <https://docs.python.org/3/library/xml.html#xml-vulnerabilities>`_ when verifying signatures
* Extensions to allow signing with and verifying X.509 certificate chains, including hostname/CN validation
* Extensions to sign and verify `XAdES <https://en.wikipedia.org/wiki/XAdES>`_ signatures
* Support for exclusive XML canonicalization with inclusive prefixes (`InclusiveNamespaces PrefixList
<http://www.w3.org/TR/xml-exc-c14n/#def-InclusiveNamespaces-PrefixList>`_, required to verify signatures generated by
some SAML implementations)
* Modern Python compatibility (3.10-3.15+ and PyPy)
* Well-supported, portable, reliable dependencies: `lxml <https://github.com/lxml/lxml>`_ and
`cryptography <https://github.com/pyca/cryptography>`_
* Comprehensive testing (including the XMLDSig interoperability suite) and `continuous integration
<https://github.com/XML-Security/signxml/actions>`_
* Simple interface with useful, ergonomic, and secure defaults (no network calls, XSLT or XPath transforms)
* Compactness, readability, and extensibility
Installation
------------
::
pip install signxml
Synopsis
--------
SignXML uses the `lxml ElementTree API <https://lxml.de/tutorial.html>`_ to work with XML data.
.. code-block:: python
from lxml import etree
from signxml import XMLSigner, XMLVerifier
data_to_sign = "<Test/>"
cert = open("cert.pem").read()
key = open("privkey.pem").read()
root = etree.fromstring(data_to_sign)
signed_root = XMLSigner().sign(root, key=key, cert=cert)
verified_data = XMLVerifier().verify(signed_root).signed_xml
To make this example self-sufficient for test purposes:
- Generate a test certificate and key using
``openssl req -x509 -nodes -subj "/CN=test" -days 1 -newkey rsa -keyout privkey.pem -out cert.pem -addext "keyUsage = digitalSignature"``
(run ``apt-get install openssl``, ``yum install openssl``, or ``brew install openssl`` if the ``openssl`` executable
is not found).
- Pass the ``x509_cert=cert`` keyword argument to ``XMLVerifier.verify()``. (In production, ensure this is replaced with
the correct configuration for the trusted CA or certificate - this determines which signatures your application
trusts.)
.. _verifying-saml-assertions:
Verifying SAML assertions
~~~~~~~~~~~~~~~~~~~~~~~~~
Assuming ``metadata.xml`` contains SAML metadata for the assertion source:
.. code-block:: python
from lxml import etree
from base64 import b64decode
from signxml import XMLVerifier
with open("metadata.xml", "rb") as fh:
cert = etree.parse(fh).find("//ds:X509Certificate").text
assertion_data = XMLVerifier().verify(b64decode(assertion_body), x509_cert=cert).signed_xml
.. admonition:: Signing SAML assertions
The SAML assertion schema specifies a location for the enveloped XML signature (between ``<Issuer>`` and
``<Subject>``). To sign a SAML assertion in a schema-compliant way, insert a signature placeholder tag at that location
before calling XMLSigner: ``<ds:Signature Id="placeholder"></ds:Signature>``.
.. admonition:: See what is signed
It is important to understand and follow the best practice rule of "See what is signed" when verifying XML
signatures. The gist of this rule is: if your application neglects to verify that the information it trusts is
what was actually signed, the attacker can supply a valid signature but point you to malicious data that wasn't signed
by that signature. Failure to follow this rule can lead to vulnerabilities against attacks like
`SAML signature wrapping <https://www.usenix.org/system/files/conference/usenixsecurity12/sec12-final91.pdf>`_ and
`XML comment canonicalization induced text element truncation <https://duo.com/blog/duo-finds-saml-vulnerabilities-affecting-multiple-implementations>`_.
In SignXML, you can ensure that the information signed is what you expect to be signed by only trusting the
data returned by ``XMLVerifier.verify()``. The ``signed_xml`` attribute of the return value is the XML node or string
that was signed. We also recommend that you assert the expected location for the signature within the document:
.. code-block:: python
from signxml import XMLVerifier, SignatureConfiguration
config = SignatureConfiguration(location="./")
XMLVerifier(...).verify(..., expect_config=config)
The ``location`` value describes the expected parent path for the ``ds:Signature`` element. SignXML
appends ``ds:Signature`` to this path internally. For example, if a SAML ``Response`` root contains a
signed ``Assertion`` child, configure verification like this:
.. code-block:: python
config = SignatureConfiguration(
location="./{urn:oasis:names:tc:SAML:2.0:assertion}Assertion/"
)
XMLVerifier().verify(response_xml, x509_cert=cert, expect_config=config)
**Recommended reading:** `W3C XML Signature Best Practices for Applications
<http://www.w3.org/TR/xmldsig-bestpractices/#practices-applications>`_, `On Breaking SAML: Be Whoever You Want to Be
<https://www.usenix.org/system/files/conference/usenixsecurity12/sec12-final91.pdf>`_, `Duo Finds SAML Vulnerabilities
Affecting Multiple Implementations <https://duo.com/blog/duo-finds-saml-vulnerabilities-affecting-multiple-implementations>`_,
`Sign in as anyone: Bypassing SAML SSO authentication with parser differentials
<https://github.blog/security/sign-in-as-anyone-bypassing-saml-sso-authentication-with-parser-differentials/>`_
.. admonition:: Establish trust
If you do not supply any keyword arguments to ``verify()``, the default behavior is to trust **any** valid XML
signature generated using a valid X.509 certificate trusted by your system's CA store. This means anyone can
get an SSL certificate and generate a signature that you will trust. To establish trust in the signer, use the
``x509_cert`` argument to specify a certificate that was pre-shared out-of-band (e.g. via SAML metadata, as
shown in *Verifying SAML assertions*), or ``cert_subject_name`` to specify a
subject name that must be in the signing X.509 certificate given by the signature (verified as if it were a
domain name), or ``ca_pem_file`` to give a custom CA.
To verify a signature at a specific point in time, such as an archived signature whose certificate has since
expired, configure ``verification_time``. The configured time is used for X.509 certificate validity checks,
including certificate chain validation and certificates supplied with ``x509_cert``:
.. code-block:: python
from datetime import datetime, timezone
from signxml import SignatureConfiguration, XMLVerifier
config = SignatureConfiguration(
verification_time=datetime(2018, 5, 28, 17, 0, tzinfo=timezone.utc)
)
verified_data = XMLVerifier().verify(data, expect_config=config).signed_xml
Signing with external private keys
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Use ``ExternalSigningKey`` to sign with a key held in a cloud service, HSM, hardware token, or OS keystore.
Supply its public key and a synchronous callback; pass the resulting object through the usual ``key=`` argument.
The callback receives an immutable ``SigningRequest`` with:
* ``data``: canonicalized SignedInfo, after reference digests and annotations have been added.
* ``signature_algorithm``: the requested ``SignatureMethod``, including hash and padding requirements.
* ``hash_algorithm``: the corresponding cryptography hash algorithm.
* ``digest``: the hash of ``data`` using the signature method's hash, independent of the document reference digest algorithm.
Use ``data`` for message-mode providers or ``digest`` for digest-mode providers. Do not hash ``digest`` again.
Return raw RSA signature bytes or ASN.1 DER signatures for ECDSA/DSA, following cryptography's conventions.
SignXML handles XMLDSig encoding and base64. If a provider returns ECDSA/DSA in another format, its adapter must
convert that format to DER before returning it. The callback must honor the requested algorithm or raise an error.
For example, an application with an authenticated AWS KMS client and an RSA signing key can use the
`KMS Sign API <https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html>`_ in digest mode:
.. code-block:: python
from cryptography.hazmat.primitives.serialization import load_der_public_key
from signxml import ExternalSigningKey, SigningRequest, SignatureMethod, XMLSigner
# kms_client and key_id are supplied by the application.
public_key = load_der_public_key(kms_client.get_public_key(KeyId=key_id)["PublicKey"])
def sign_with_kms(request: SigningRequest) -> bytes:
if request.signature_algorithm != SignatureMethod.RSA_SHA256:
raise ValueError("This adapter supports RSA-SHA256 only")
response = kms_client.sign(
KeyId=key_id,
Message=request.digest,
MessageType="DIGEST",
SigningAlgorithm="RSASSA_PKCS1_V1_5_SHA_256",
)
return response["Signature"]
key = ExternalSigningKey(public_key=public_key, signer=sign_with_kms)
signed_root = XMLSigner().sign(root, key=key)
Pass ``cert=`` as usual when embedding an X.509 certificate or chain. Its first certificate must match the
external public key. Without ``cert=`` or custom ``key_info=``, SignXML includes the public key in KeyValue.
``XAdESSigner`` accepts the same key object. External signing supports RSA PKCS#1 v1.5, RSA-PSS, ECDSA, and DSA;
it does not support HMAC or use ``passphrase``. RSA-PSS requires MGF1 with the signature hash and a salt whose
length equals that hash's digest size.
Relaxing digital signature key usage extension validation for certificates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When verifying X.509 certificate chains, SignXML's default end-entity certificate policy requires the
``KeyUsage`` extension to be present and to allow ``digitalSignature``. To accept certificates without that
key usage, pass a custom end-entity certificate policy that does not require it:
.. code-block:: python
ee_policy = x509.verification.ExtensionPolicy.permit_all()
XMLVerifier(...).verify(..., ee_policy=ee_policy)
XML signature construction methods: enveloped, detached, enveloping
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The XML Signature specification defines three ways to compose a signature with the data being signed: enveloped,
detached, and enveloping signature. Enveloped is the default method. To specify the type of signature that you want to
generate, pass the ``method`` argument to ``sign()``:
.. code-block:: python
signed_root = XMLSigner(method=signxml.methods.detached).sign(root, key=key, cert=cert)
verified_data = XMLVerifier().verify(signed_root).signed_xml
For detached signatures, the code above will use the ``Id`` or ``ID`` attribute of ``root`` to generate a relative URI
(``<Reference URI="#value"``). You can also override the value of ``URI`` by passing a ``reference_uri`` argument to
``sign()``. To verify a detached signature that refers to an external entity, pass a callable resolver in
``XMLVerifier().verify(data, uri_resolver=...)``.
See the `API documentation <https://xml-security.github.io/signxml/#id5>`_ for more details.
XML representation details: Configuring namespace prefixes and whitespace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some applications require a particular namespace prefix configuration - for example, a number of applications assume
that the ``http://www.w3.org/2000/09/xmldsig#`` namespace is set as the default, unprefixed namespace instead of using
the customary ``ds:`` prefix. While in normal use namespace prefix naming is an insignificant representation detail,
it can be significant in some XML canonicalization and signature configurations. To configure the namespace prefix map
when generating a signature, set the ``XMLSigner.namespaces`` attribute:
.. code-block:: python
signer = signxml.XMLSigner(...)
signer.namespaces = {None: signxml.namespaces.ds}
signed_root = signer.sign(...)
Similarly, whitespace in the signed document is significant for XML canonicalization and signature purposes. Do not
pretty-print the XML after generating the signature, since this can unfortunately render the signature invalid.
XML parsing security and compatibility with ``xml.etree.ElementTree``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SignXML uses the `lxml <https://github.com/lxml/lxml>`_ ElementTree library, not the
`ElementTree from Python's standard library <https://docs.python.org/3.8/library/xml.etree.elementtree.html>`_,
to work with XML. lxml is used due to its superior resistance to XML attacks, as well as XML canonicalization and
namespace organization features. It is recommended that you pass XML string input directly to signxml before further
parsing, and use lxml to work with untrusted XML input in general. If you do pass ``xml.etree.ElementTree`` objects to
SignXML, you should be aware of differences in XML namespace handling between the two libraries. See the following
references for more information:
* `How do I use lxml safely as a web-service endpoint?
<https://lxml.de/FAQ.html#how-do-i-use-lxml-safely-as-a-web-service-endpoint>`_
* `ElementTree compatibility of lxml.etree <https://lxml.de/compatibility.html>`_
* `XML Signatures with Python ElementTree <https://technotes.shemyak.com/posts/xml-signatures-with-python-elementtree>`_
XAdES signatures
~~~~~~~~~~~~~~~~
`XAdES ("XML Advanced Electronic Signatures") <https://en.wikipedia.org/wiki/XAdES>`_ is a standard for attaching
metadata to XML Signature objects. This standard is endorsed by the European Union as the implementation for its
`eSignature <https://ec.europa.eu/digital-building-blocks/wikis/display/DIGITAL/eSignature+Overview>`_ regulations.
SignXML supports signing and verifying documents using `XAdES <https://en.wikipedia.org/wiki/XAdES>`_ signatures:
.. code-block:: python
from signxml import DigestAlgorithm
from signxml.xades import (XAdESSigner, XAdESVerifier, XAdESVerifyResult,
XAdESSignaturePolicy, XAdESDataObjectFormat)
signature_policy = XAdESSignaturePolicy(
Identifier="MyPolicyIdentifier",
Description="Hello XAdES",
DigestMethod=DigestAlgorithm.SHA256,
DigestValue="Ohixl6upD6av8N7pEvDABhEL6hM=",
)
data_object_format = XAdESDataObjectFormat(
Description="My XAdES signature",
MimeType="text/xml",
)
signer = XAdESSigner(
signature_policy=signature_policy,
claimed_roles=["signer"],
data_object_format=data_object_format,
c14n_algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
)
signed_doc = signer.sign(doc, key=private_key, cert=certificate)
.. code-block:: python
verifier = XAdESVerifier()
verify_results = verifier.verify(
signed_doc, x509_cert=certificate, expect_references=3, expect_signature_policy=signature_policy
)
for verify_result in verify_results:
if isinstance(verify_result, XAdESVerifyResult):
verify_result.signed_properties # use this to access parsed XAdES properties
Authors
-------
* `Andrey Kislyuk <https://github.com/kislyuk>`_ and SignXML contributors.
Links
-----
* `Project home page (GitHub) <https://github.com/XML-Security/signxml>`_
* `Documentation <https://xml-security.github.io/signxml/>`_
* `Package distribution (PyPI) <https://pypi.python.org/pypi/signxml>`_
* `Change log <https://github.com/XML-Security/signxml/blob/master/Changes.rst>`_
* `List of W3C XML Signature standards and drafts <https://www.w3.org/TR/?title=xml%20signature>`_
* `W3C Recommendation: XML Signature Syntax and Processing Version 1.1 <http://www.w3.org/TR/xmldsig-core1>`_
* `W3C Working Group Note: XML Signature Best Practices <http://www.w3.org/TR/xmldsig-bestpractices/>`_
* `XML-Signature Interoperability <http://www.w3.org/Signature/2001/04/05-xmldsig-interop.html>`_
* `W3C Working Group Note: Test Cases for C14N 1.1 and XMLDSig Interoperability <http://www.w3.org/TR/xmldsig2ed-tests/>`_
* `W3C Working Group Note: XML Signature Syntax and Processing Version 2.0 <http://www.w3.org/TR/xmldsig-core2>`_
(This draft standard proposal was never finalized and is not in general use.)
* `Intelligence Community Technical Specification: Web Service Security Guidance for Use of XML Signature and XML
Encryption <https://github.com/XML-Security/signxml/blob/develop/docs/dni-guidance.pdf>`_
* `XMLSec: Related links <https://www.aleksey.com/xmlsec/related.html>`_
* `OWASP SAML Security Cheat Sheet <https://www.owasp.org/index.php/SAML_Security_Cheat_Sheet>`_
* `Okta Developer Docs: SAML <https://developer.okta.com/standards/SAML/>`_
Bugs
~~~~
Please report bugs, issues, feature requests, etc. on `GitHub <https://github.com/XML-Security/signxml/issues>`_.
Versioning
~~~~~~~~~~
This package follows the `Semantic Versioning 2.0.0 <http://semver.org/>`_ standard. To control changes, it is
recommended that application developers pin the package version and manage it using `uv
<https://github.com/astral-sh/uv>`_ or similar. For library developers, pinning the major version is
recommended.
License
-------
Copyright 2014-2026, Andrey Kislyuk and SignXML contributors. Licensed under the terms of the
`Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_. Distribution of the LICENSE and NOTICE
files with source copies of this package and derivative works is **REQUIRED** as specified by the Apache License.