77from cryptography .hazmat .primitives .hmac import HMAC
88from cryptography .hazmat .primitives .serialization import load_pem_private_key
99from lxml .etree import Element , SubElement , _Element
10- from OpenSSL .crypto import FILETYPE_PEM , dump_certificate
10+ from OpenSSL .crypto import FILETYPE_PEM , X509 , dump_certificate
1111
1212from .algorithms import (
1313 CanonicalizationMethod ,
@@ -62,13 +62,14 @@ class XMLSigner(XMLSignatureProcessor):
6262 ``signxml.methods.enveloped``, ``signxml.methods.enveloping``, or ``signxml.methods.detached``. See
6363 :class:`SignatureConstructionMethod` for details.
6464 :param signature_algorithm:
65- Algorithm that will be used to generate the signature, composed of the signature algorithm and the digest
66- algorithm, separated by a hyphen. All algorithm IDs listed under the `Algorithm Identifiers and
67- Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature
68- 1.1 standard are supported.
69- :param digest_algorithm: Algorithm that will be used to hash the data during signature generation. All algorithm IDs
70- listed under the `Algorithm Identifiers and Implementation Requirements
71- <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature 1.1 standard are supported.
65+ Algorithm that will be used to generate the signature. See :class:`SignatureMethod` for the list of algorithm
66+ IDs supported.
67+ :param digest_algorithm:
68+ Algorithm that will be used to hash the data during signature generation. See :class:`DigestAlgorithm` for the
69+ list of algorithm IDs supported.
70+ :param c14n_algorithm:
71+ Algorithm that will be used to canonicalize (serialize in a reproducible way) the XML that is signed. See
72+ :class:`CanonicalizationMethod` for the list of algorithm IDs supported.
7273 """
7374
7475 signature_annotators : List
@@ -92,7 +93,7 @@ def __init__(
9293 method : SignatureConstructionMethod = SignatureConstructionMethod .enveloped ,
9394 signature_algorithm : Union [SignatureMethod , str ] = SignatureMethod .RSA_SHA256 ,
9495 digest_algorithm : Union [DigestAlgorithm , str ] = DigestAlgorithm .SHA256 ,
95- c14n_algorithm = CanonicalizationMethod .CANONICAL_XML_1_1 ,
96+ c14n_algorithm : Union [ CanonicalizationMethod , str ] = CanonicalizationMethod .CANONICAL_XML_1_1 ,
9697 ):
9798 if method is None or method not in SignatureConstructionMethod :
9899 raise InvalidInput (f"Unknown signature construction method { method } " )
@@ -115,14 +116,14 @@ def sign(
115116 data ,
116117 key = None ,
117118 passphrase : Optional [bytes ] = None ,
118- cert = None ,
119+ cert : Optional [ Union [ str , List [ str ], List [ X509 ]]] = None ,
119120 reference_uri : Optional [Union [str , List [str ], List [XMLSignatureReference ]]] = None ,
120121 key_name : Optional [str ] = None ,
121122 key_info : Optional [_Element ] = None ,
122123 id_attribute : Optional [str ] = None ,
123124 always_add_key_value : bool = False ,
124125 inclusive_ns_prefixes : Optional [List [str ]] = None ,
125- signature_properties = None ,
126+ signature_properties : Optional [ Union [ _Element , List [ _Element ]]] = None ,
126127 ) -> _Element :
127128 """
128129 Sign the data and return the root element of the resulting XML tree.
@@ -131,20 +132,19 @@ def sign(
131132 :type data: String, file-like object, or XML ElementTree Element API compatible object
132133 :param key:
133134 Key to be used for signing. When signing with a certificate or RSA/DSA/ECDSA key, this can be a string/bytes
134- containing a PEM-formatted key, or a :py: class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`,
135- :py: class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or
136- :py: class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object. When signing with a
135+ containing a PEM-formatted key, or a :class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`,
136+ :class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or
137+ :class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object. When signing with a
137138 HMAC, this should be a string containing the shared secret.
138139 :type key:
139- string, bytes, :py: class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`,
140- :py: class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or
141- :py: class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object
140+ string, bytes, :class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`,
141+ :class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or
142+ :class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object
142143 :param passphrase: Passphrase to use to decrypt the key, if any.
143144 :param cert:
144145 X.509 certificate to use for signing. This should be a string containing a PEM-formatted certificate, or an
145- array of strings or OpenSSL.crypto.X509 objects containing the certificate and a chain of intermediate
146- certificates.
147- :type cert: string, array of strings, or array of OpenSSL.crypto.X509 objects
146+ array of strings or :class:`OpenSSL.crypto.X509` objects containing the certificate and a chain of
147+ intermediate certificates.
148148 :param reference_uri:
149149 Custom reference URI or list of reference URIs to incorporate into the signature. When ``method`` is set to
150150 ``detached`` or ``enveloped``, reference URIs are set to this value and only the referenced elements are
@@ -175,10 +175,9 @@ def sign(
175175 :param signature_properties:
176176 One or more Elements that are to be included in the SignatureProperies section when using the detached
177177 method.
178- :type signature_properties: :py:class:`lxml.etree.Element` or list of :py:class:`lxml.etree.Element` s
179178
180179 :returns:
181- A :py: class:`lxml.etree.Element ` object representing the root of the XML tree containing the signature and
180+ A :class:`lxml.etree._Element ` object representing the root of the XML tree containing the signature and
182181 the payload data.
183182
184183 To specify the location of an enveloped signature within **data**, insert a
@@ -192,7 +191,7 @@ def sign(
192191 if isinstance (cert , (str , bytes )):
193192 cert_chain = list (iterate_pem (cert ))
194193 else :
195- cert_chain = cert
194+ cert_chain = cert # type: ignore
196195
197196 input_references = self ._preprocess_reference_uri (reference_uri )
198197
0 commit comments