2
2
from dataclasses import dataclass
3
3
from typing import List , Optional , Union
4
4
5
- from cryptography .hazmat .primitives .asymmetric import ec , utils
5
+ from cryptography .hazmat .primitives .asymmetric import dsa , ec , rsa , utils
6
6
from cryptography .hazmat .primitives .asymmetric .padding import PKCS1v15
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 } " )
@@ -113,16 +114,16 @@ def __init__(
113
114
def sign (
114
115
self ,
115
116
data ,
116
- key = None ,
117
+ key : Optional [ Union [ str , bytes , rsa . RSAPrivateKey , dsa . DSAPrivateKey , ec . EllipticCurvePrivateKey ]] = 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,15 @@ 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.asymmetric.rsa .RSAPrivateKey`,
136
+ :class:`cryptography.hazmat.primitives.asymmetric.dsa .DSAPrivateKey`, or
137
+ :class:`cryptography.hazmat.primitives.asymmetric.ec .EllipticCurvePrivateKey` object. When signing with a
137
138
HMAC, this should be a string containing the shared secret.
138
- :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
142
139
:param passphrase: Passphrase to use to decrypt the key, if any.
143
140
:param cert:
144
141
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
142
+ array of strings or :class:`OpenSSL.crypto.X509` objects containing the certificate and a chain of
143
+ intermediate certificates.
148
144
:param reference_uri:
149
145
Custom reference URI or list of reference URIs to incorporate into the signature. When ``method`` is set to
150
146
``detached`` or ``enveloped``, reference URIs are set to this value and only the referenced elements are
@@ -175,10 +171,9 @@ def sign(
175
171
:param signature_properties:
176
172
One or more Elements that are to be included in the SignatureProperies section when using the detached
177
173
method.
178
- :type signature_properties: :py:class:`lxml.etree.Element` or list of :py:class:`lxml.etree.Element` s
179
174
180
175
:returns:
181
- A :py: class:`lxml.etree.Element ` object representing the root of the XML tree containing the signature and
176
+ A :class:`lxml.etree._Element ` object representing the root of the XML tree containing the signature and
182
177
the payload data.
183
178
184
179
To specify the location of an enveloped signature within **data**, insert a
@@ -192,7 +187,7 @@ def sign(
192
187
if isinstance (cert , (str , bytes )):
193
188
cert_chain = list (iterate_pem (cert ))
194
189
else :
195
- cert_chain = cert
190
+ cert_chain = cert # type: ignore
196
191
197
192
input_references = self ._preprocess_reference_uri (reference_uri )
198
193
@@ -235,7 +230,7 @@ def sign(
235
230
signed_info_node , algorithm = self .c14n_alg , inclusive_ns_prefixes = inclusive_ns_prefixes
236
231
)
237
232
if self .sign_alg .name .startswith ("HMAC_" ):
238
- signer = HMAC (key = key , algorithm = digest_algorithm_implementations [self .sign_alg ]())
233
+ signer = HMAC (key = key , algorithm = digest_algorithm_implementations [self .sign_alg ]()) # type: ignore
239
234
signer .update (signed_info_c14n )
240
235
signature_value_node .text = b64encode (signer .finalize ()).decode ()
241
236
sig_root .append (signature_value_node )
@@ -378,7 +373,7 @@ def _build_sig(self, sig_root, references, c14n_inputs, inclusive_ns_prefixes):
378
373
reference_node = SubElement (signed_info , ds_tag ("Reference" ), URI = reference .URI )
379
374
transforms = SubElement (reference_node , ds_tag ("Transforms" ))
380
375
if self .construction_method == SignatureConstructionMethod .enveloped :
381
- SubElement (transforms , ds_tag ("Transform" ), Algorithm = namespaces . ds + " enveloped-signature" )
376
+ SubElement (transforms , ds_tag ("Transform" ), Algorithm = SignatureConstructionMethod . enveloped . value )
382
377
SubElement (transforms , ds_tag ("Transform" ), Algorithm = reference .c14n_method .value )
383
378
else :
384
379
c14n_xform = SubElement (transforms , ds_tag ("Transform" ), Algorithm = reference .c14n_method .value )
0 commit comments