Debian Codegraph @

signxml/algorithms.py

raw
1 from enum import Enum
2 from typing import Callable, Dict, Type, Union
3
4 from cryptography.hazmat.primitives import hashes
5
6 from .exceptions import InvalidInput
7
8
9 class SignatureConstructionMethod(Enum):
10 """
11 An enumeration of signature construction methods supported by SignXML, used to specify the method when signing.
12 See the list of signature types under `XML Signature Syntax and Processing Version 2.0, Definitions
13 <http://www.w3.org/TR/xmldsig-core2/#sec-Definitions>`_.
14 """
15
16 enveloped = "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
17 """
18 The signature is over the XML content that contains the signature as an element. The content provides the root
19 XML document element. This is the most common XML signature type in modern applications.
20 """
21
22 enveloping = "enveloping-signature"
23 """
24 The signature is over content found within an Object element of the signature itself. The Object (or its
25 content) is identified via a Reference (via a URI fragment identifier or transform).
26 """
27
28 detached = "detached-signature"
29 """
30 The signature is over content external to the Signature element, and can be identified via a URI or
31 transform. Consequently, the signature is "detached" from the content it signs. This definition typically applies to
32 separate data objects, but it also includes the instance where the Signature and data object reside within the same
33 XML document but are sibling elements.
34 """
35
36
37 class FragmentLookupMixin:
38 @classmethod
39 def from_fragment(cls, fragment):
40 for i in cls: # type: ignore[attr-defined]
41 if i.value.endswith("#" + fragment):
42 return i
43 else:
44 raise InvalidInput(f"Unrecognized {cls.__name__} identifier fragment: {fragment}")
45
46
47 class InvalidInputErrorMixin:
48 @classmethod
49 def _missing_(cls, value):
50 raise InvalidInput(f"Unrecognized {cls.__name__}: {value}")
51
52 def __repr__(self):
53 return f"{self.__class__.__name__}.{self.name}" # type: ignore[attr-defined]
54
55
56 class DigestAlgorithm(FragmentLookupMixin, InvalidInputErrorMixin, Enum):
57 """
58 An enumeration of digest algorithms supported by SignXML. See the
59 `Algorithm Identifiers and Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of
60 the XML Signature 1.1 standard for details.
61 """
62
63 SHA224 = "http://www.w3.org/2001/04/xmldsig-more#sha224"
64 SHA384 = "http://www.w3.org/2001/04/xmldsig-more#sha384"
65 SHA256 = "http://www.w3.org/2001/04/xmlenc#sha256"
66 SHA512 = "http://www.w3.org/2001/04/xmlenc#sha512"
67 SHA3_224 = "http://www.w3.org/2007/05/xmldsig-more#sha3-224"
68 SHA3_256 = "http://www.w3.org/2007/05/xmldsig-more#sha3-256"
69 SHA3_384 = "http://www.w3.org/2007/05/xmldsig-more#sha3-384"
70 SHA3_512 = "http://www.w3.org/2007/05/xmldsig-more#sha3-512"
71
72 SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1"
73 "See `SHA1 deprecation`_."
74
75 @property
76 def implementation(self) -> Callable:
77 """
78 The cryptography callable that implements the specified algorithm.
79 """
80 return digest_algorithm_implementations[self]
81
82
83 # TODO: check if padding errors are fixed by using padding=MGF1
84 class SignatureMethod(FragmentLookupMixin, InvalidInputErrorMixin, Enum):
85 """
86 An enumeration of signature methods (also referred to as signature algorithms) supported by SignXML. See the
87 `Algorithm Identifiers and Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of
88 the XML Signature 1.1 standard for details.
89 """
90
91 RSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
92 """
93 The RSASSA-PKCS1-v1_5 algorithm described in RFC 3447. This is the default, most widely supported signature method.
94 """
95
96 RSA_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha224"
97 RSA_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384"
98 RSA_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"
99 ECDSA_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha224"
100 ECDSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"
101 ECDSA_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384"
102 ECDSA_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512"
103 ECDSA_SHA3_224 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-224"
104 ECDSA_SHA3_256 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-256"
105 ECDSA_SHA3_384 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-384"
106 ECDSA_SHA3_512 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-512"
107 DSA_SHA256 = "http://www.w3.org/2009/xmldsig11#dsa-sha256"
108 HMAC_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha224"
109 HMAC_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"
110 HMAC_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384"
111 HMAC_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512"
112 SHA3_224_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha3-224-rsa-MGF1"
113 SHA3_256_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha3-256-rsa-MGF1"
114 SHA3_384_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha3-384-rsa-MGF1"
115 SHA3_512_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha3-512-rsa-MGF1"
116 SHA224_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha224-rsa-MGF1"
117 SHA256_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1"
118 SHA384_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha384-rsa-MGF1"
119 SHA512_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha512-rsa-MGF1"
120
121 DSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
122 """
123 _`SHA1 deprecation`: SHA1 based algorithms are not secure for use in digital signatures. They are included for
124 legacy compatibility only and disabled by default. To verify SHA1 based signatures, use::
125
126 XMLVerifier().verify(
127 expect_config=SignatureConfiguration(
128 signature_methods=...,
129 digest_algorithms=...
130 )
131 )
132 """
133 HMAC_SHA1 = "http://www.w3.org/2000/09/xmldsig#hmac-sha1"
134 "See `SHA1 deprecation`_."
135 RSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
136 "See `SHA1 deprecation`_."
137 ECDSA_SHA1 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1"
138 "See `SHA1 deprecation`_."
139 SHA1_RSA_MGF1 = "http://www.w3.org/2007/05/xmldsig-more#sha1-rsa-MGF1"
140 "See `SHA1 deprecation`_."
141
142
143 class CanonicalizationMethod(InvalidInputErrorMixin, Enum):
144 """
145 An enumeration of XML canonicalization methods (also referred to as canonicalization algorithms) supported by
146 SignXML. See the `Algorithm Identifiers and Implementation Requirements
147 <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature 1.1 standard for details.
148 """
149
150 CANONICAL_XML_1_0 = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
151 CANONICAL_XML_1_0_WITH_COMMENTS = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
152 CANONICAL_XML_1_1 = "http://www.w3.org/2006/12/xml-c14n11"
153 CANONICAL_XML_1_1_WITH_COMMENTS = "http://www.w3.org/2006/12/xml-c14n11#WithComments"
154 EXCLUSIVE_XML_CANONICALIZATION_1_0 = "http://www.w3.org/2001/10/xml-exc-c14n#"
155 EXCLUSIVE_XML_CANONICALIZATION_1_0_WITH_COMMENTS = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
156
157 # The identifier for Canonical XML 2.0 is "http://www.w3.org/2010/xml-c14n2", but it is not a W3C standard.
158 # While it is supported by lxml, it's not in general use and not supported by SignXML
159
160
161 digest_algorithm_implementations: Dict[Union[DigestAlgorithm, SignatureMethod], Type[hashes.HashAlgorithm]] = {
162 DigestAlgorithm.SHA1: hashes.SHA1,
163 DigestAlgorithm.SHA224: hashes.SHA224,
164 DigestAlgorithm.SHA384: hashes.SHA384,
165 DigestAlgorithm.SHA256: hashes.SHA256,
166 DigestAlgorithm.SHA512: hashes.SHA512,
167 DigestAlgorithm.SHA3_224: hashes.SHA3_224,
168 DigestAlgorithm.SHA3_256: hashes.SHA3_256,
169 DigestAlgorithm.SHA3_384: hashes.SHA3_384,
170 DigestAlgorithm.SHA3_512: hashes.SHA3_512,
171 SignatureMethod.DSA_SHA1: hashes.SHA1,
172 SignatureMethod.HMAC_SHA1: hashes.SHA1,
173 SignatureMethod.RSA_SHA1: hashes.SHA1,
174 SignatureMethod.ECDSA_SHA1: hashes.SHA1,
175 SignatureMethod.ECDSA_SHA224: hashes.SHA224,
176 SignatureMethod.ECDSA_SHA256: hashes.SHA256,
177 SignatureMethod.ECDSA_SHA384: hashes.SHA384,
178 SignatureMethod.ECDSA_SHA512: hashes.SHA512,
179 SignatureMethod.HMAC_SHA224: hashes.SHA224,
180 SignatureMethod.HMAC_SHA256: hashes.SHA256,
181 SignatureMethod.HMAC_SHA384: hashes.SHA384,
182 SignatureMethod.HMAC_SHA512: hashes.SHA512,
183 SignatureMethod.RSA_SHA224: hashes.SHA224,
184 SignatureMethod.RSA_SHA256: hashes.SHA256,
185 SignatureMethod.RSA_SHA384: hashes.SHA384,
186 SignatureMethod.RSA_SHA512: hashes.SHA512,
187 SignatureMethod.DSA_SHA256: hashes.SHA256,
188 SignatureMethod.ECDSA_SHA3_224: hashes.SHA3_224,
189 SignatureMethod.ECDSA_SHA3_256: hashes.SHA3_256,
190 SignatureMethod.ECDSA_SHA3_384: hashes.SHA3_384,
191 SignatureMethod.ECDSA_SHA3_512: hashes.SHA3_512,
192 SignatureMethod.SHA3_224_RSA_MGF1: hashes.SHA3_224,
193 SignatureMethod.SHA3_256_RSA_MGF1: hashes.SHA3_256,
194 SignatureMethod.SHA3_384_RSA_MGF1: hashes.SHA3_384,
195 SignatureMethod.SHA3_512_RSA_MGF1: hashes.SHA3_512,
196 SignatureMethod.SHA224_RSA_MGF1: hashes.SHA224,
197 SignatureMethod.SHA256_RSA_MGF1: hashes.SHA256,
198 SignatureMethod.SHA384_RSA_MGF1: hashes.SHA384,
199 SignatureMethod.SHA512_RSA_MGF1: hashes.SHA512,
200 SignatureMethod.SHA1_RSA_MGF1: hashes.SHA1,
201 }