7
7
from cryptography .hazmat .primitives .hmac import HMAC
8
8
from cryptography .hazmat .primitives .serialization import load_pem_private_key
9
9
from 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
11
11
12
12
from .algorithms import (
13
13
CanonicalizationMethod ,
@@ -62,13 +62,14 @@ class XMLSigner(XMLSignatureProcessor):
62
62
``signxml.methods.enveloped``, ``signxml.methods.enveloping``, or ``signxml.methods.detached``. See
63
63
:class:`SignatureConstructionMethod` for details.
64
64
: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.
72
73
"""
73
74
74
75
signature_annotators : List
@@ -92,7 +93,7 @@ def __init__(
92
93
method : SignatureConstructionMethod = SignatureConstructionMethod .enveloped ,
93
94
signature_algorithm : Union [SignatureMethod , str ] = SignatureMethod .RSA_SHA256 ,
94
95
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 ,
96
97
):
97
98
if method is None or method not in SignatureConstructionMethod :
98
99
raise InvalidInput (f"Unknown signature construction method { method } " )
@@ -115,14 +116,14 @@ def sign(
115
116
data ,
116
117
key = None ,
117
118
passphrase : Optional [bytes ] = None ,
118
- cert = None ,
119
+ cert : Optional [ Union [ str , List [ str ], List [ X509 ]]] = None ,
119
120
reference_uri : Optional [Union [str , List [str ], List [XMLSignatureReference ]]] = None ,
120
121
key_name : Optional [str ] = None ,
121
122
key_info : Optional [_Element ] = None ,
122
123
id_attribute : Optional [str ] = None ,
123
124
always_add_key_value : bool = False ,
124
125
inclusive_ns_prefixes : Optional [List [str ]] = None ,
125
- signature_properties = None ,
126
+ signature_properties : Optional [ Union [ _Element , List [ _Element ]]] = None ,
126
127
) -> _Element :
127
128
"""
128
129
Sign the data and return the root element of the resulting XML tree.
@@ -131,20 +132,19 @@ def sign(
131
132
:type data: String, file-like object, or XML ElementTree Element API compatible object
132
133
:param key:
133
134
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
137
138
HMAC, this should be a string containing the shared secret.
138
139
: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
142
143
:param passphrase: Passphrase to use to decrypt the key, if any.
143
144
:param cert:
144
145
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.
148
148
:param reference_uri:
149
149
Custom reference URI or list of reference URIs to incorporate into the signature. When ``method`` is set to
150
150
``detached`` or ``enveloped``, reference URIs are set to this value and only the referenced elements are
@@ -175,10 +175,9 @@ def sign(
175
175
:param signature_properties:
176
176
One or more Elements that are to be included in the SignatureProperies section when using the detached
177
177
method.
178
- :type signature_properties: :py:class:`lxml.etree.Element` or list of :py:class:`lxml.etree.Element` s
179
178
180
179
: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
182
181
the payload data.
183
182
184
183
To specify the location of an enveloped signature within **data**, insert a
@@ -192,7 +191,7 @@ def sign(
192
191
if isinstance (cert , (str , bytes )):
193
192
cert_chain = list (iterate_pem (cert ))
194
193
else :
195
- cert_chain = cert
194
+ cert_chain = cert # type: ignore
196
195
197
196
input_references = self ._preprocess_reference_uri (reference_uri )
198
197
0 commit comments