88from lxml .etree import Element , SubElement
99from OpenSSL .crypto import FILETYPE_PEM , dump_certificate
1010
11- from .algorithms import XMLSecurityDigestAlgorithm as digest_algorithms
12- from .algorithms import XMLSecuritySignatureMethod as signature_methods
13- from .algorithms import XMLSignatureMethods as methods
14- from .algorithms import digest_algorithm_implementations
11+ from .algorithms import DigestAlgorithm , SignatureMethod , SignatureType , digest_algorithm_implementations
1512from .exceptions import InvalidInput
1613from .processor import XMLSignatureProcessor
1714from .util import (
@@ -38,37 +35,34 @@ class XMLSigner(XMLSignatureProcessor):
3835 ``signxml.methods.enveloped``, ``signxml.methods.enveloping``, or ``signxml.methods.detached``. See the list
3936 of signature types under `XML Signature Syntax and Processing Version 2.0, Definitions
4037 <http://www.w3.org/TR/xmldsig-core2/#sec-Definitions>`_.
41- :type method: :py:class:`methods`
4238 :param signature_algorithm:
4339 Algorithm that will be used to generate the signature, composed of the signature algorithm and the digest
4440 algorithm, separated by a hyphen. All algorithm IDs listed under the `Algorithm Identifiers and
4541 Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature
4642 1.1 standard are supported.
47- :type signature_algorithm: string
4843 :param digest_algorithm: Algorithm that will be used to hash the data during signature generation. All algorithm IDs
4944 listed under the `Algorithm Identifiers and Implementation Requirements
5045 <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature 1.1 standard are supported.
51- :type digest_algorithm: string
5246 """
5347
5448 def __init__ (
5549 self ,
56- method : methods = methods .enveloped ,
57- signature_algorithm : Union [signature_methods , str ] = signature_methods .RSA_SHA256 ,
58- digest_algorithm : Union [digest_algorithms , str ] = digest_algorithms .SHA256 ,
50+ method : SignatureType = SignatureType .enveloped ,
51+ signature_algorithm : Union [SignatureMethod , str ] = SignatureMethod .RSA_SHA256 ,
52+ digest_algorithm : Union [DigestAlgorithm , str ] = DigestAlgorithm .SHA256 ,
5953 c14n_algorithm = XMLSignatureProcessor .default_c14n_algorithm ,
6054 ):
61- if method is None or method not in methods :
55+ if method is None or method not in SignatureType :
6256 raise InvalidInput ("Unknown signature method {}" .format (method ))
63- self .method = method
57+ self .signature_type = method
6458 if isinstance (signature_algorithm , str ) and "#" not in signature_algorithm :
65- self .sign_alg = signature_methods .from_fragment (signature_algorithm )
59+ self .sign_alg = SignatureMethod .from_fragment (signature_algorithm )
6660 else :
67- self .sign_alg = signature_methods (signature_algorithm )
61+ self .sign_alg = SignatureMethod (signature_algorithm )
6862 if isinstance (digest_algorithm , str ) and "#" not in digest_algorithm :
69- self .digest_alg = digest_algorithms .from_fragment (digest_algorithm )
63+ self .digest_alg = DigestAlgorithm .from_fragment (digest_algorithm )
7064 else :
71- self .digest_alg = digest_algorithms (digest_algorithm )
65+ self .digest_alg = DigestAlgorithm (digest_algorithm )
7266 assert c14n_algorithm in self .known_c14n_algorithms
7367 self .c14n_alg = c14n_algorithm
7468 self .namespaces = dict (ds = namespaces .ds )
@@ -189,7 +183,7 @@ def sign(
189183
190184 sig_root , doc_root , c14n_inputs , reference_uris = self ._unpack (data , reference_uris )
191185
192- if self .method == methods .detached and signature_properties is not None :
186+ if self .signature_type == SignatureType .detached and signature_properties is not None :
193187 reference_uris .append ("#prop" )
194188 if signature_properties is not None and not isinstance (signature_properties , list ):
195189 signature_properties = [signature_properties ]
@@ -237,14 +231,14 @@ def sign(
237231 else :
238232 raise NotImplementedError ()
239233
240- if self .method == methods .enveloping :
234+ if self .signature_type == SignatureType .enveloping :
241235 for c14n_input in c14n_inputs :
242236 doc_root .append (c14n_input )
243237
244- if self .method == methods .detached and signature_properties is not None :
238+ if self .signature_type == SignatureType .detached and signature_properties is not None :
245239 sig_root .append (signature_properties_el )
246240
247- return doc_root if self .method == methods .enveloped else sig_root
241+ return doc_root if self .signature_type == SignatureType .enveloped else sig_root
248242
249243 def _add_key_info (self , sig_root , signing_settings : SigningSettings ):
250244 if self .sign_alg .name .startswith ("HMAC_" ):
@@ -280,7 +274,7 @@ def _get_c14n_inputs_from_reference_uris(self, doc_root, reference_uris):
280274
281275 def _unpack (self , data , reference_uris ):
282276 sig_root = Element (ds_tag ("Signature" ), nsmap = self .namespaces )
283- if self .method == methods .enveloped :
277+ if self .signature_type == SignatureType .enveloped :
284278 if isinstance (data , (str , bytes )):
285279 raise InvalidInput ("When using enveloped signature, **data** must be an XML element" )
286280 doc_root = self .get_root (data )
@@ -309,7 +303,7 @@ def _unpack(self, data, reference_uris):
309303 for c14n_input in c14n_inputs :
310304 payload_id = c14n_input .get ("Id" , c14n_input .get ("ID" ))
311305 reference_uris .append ("#{}" .format (payload_id ) if payload_id is not None else "" )
312- elif self .method == methods .detached :
306+ elif self .signature_type == SignatureType .detached :
313307 doc_root = self .get_root (data )
314308 if reference_uris is None :
315309 reference_uris = ["#{}" .format (data .get ("Id" , data .get ("ID" , "object" )))]
@@ -318,7 +312,7 @@ def _unpack(self, data, reference_uris):
318312 c14n_inputs , reference_uris = self ._get_c14n_inputs_from_reference_uris (doc_root , reference_uris )
319313 except InvalidInput : # Dummy reference URI
320314 c14n_inputs = [self .get_root (data )]
321- elif self .method == methods .enveloping :
315+ elif self .signature_type == SignatureType .enveloping :
322316 doc_root = sig_root
323317 c14n_inputs = [Element (ds_tag ("Object" ), nsmap = self .namespaces , Id = "object" )]
324318 if isinstance (data , (str , bytes )):
@@ -338,7 +332,7 @@ def _build_sig(self, sig_root, reference_uris, c14n_inputs, sig_insp, payload_in
338332 for i , reference_uri in enumerate (reference_uris ):
339333 reference = SubElement (signed_info , ds_tag ("Reference" ), URI = reference_uri )
340334 transforms = SubElement (reference , ds_tag ("Transforms" ))
341- if self .method == methods .enveloped :
335+ if self .signature_type == SignatureType .enveloped :
342336 SubElement (transforms , ds_tag ("Transform" ), Algorithm = namespaces .ds + "enveloped-signature" )
343337 SubElement (transforms , ds_tag ("Transform" ), Algorithm = self .c14n_alg )
344338 else :
0 commit comments