Skip to content

Commit 6f20503

Browse files
committed
Relocate algorithms to own module
1 parent 3d050da commit 6f20503

File tree

3 files changed

+8
-111
lines changed

3 files changed

+8
-111
lines changed

signxml/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
from OpenSSL.crypto import dump_certificate, load_certificate
1515
from OpenSSL.crypto import verify as openssl_verify
1616

17+
from .algorithms import XMLSecurityDigestAlgorithm as digest_algorithms
18+
from .algorithms import XMLSecuritySignatureMethod as signature_methods
19+
from .algorithms import XMLSignatureMethods as methods
20+
from .algorithms import digest_algorithm_implementations
1721
from .exceptions import InvalidCertificate, InvalidDigest, InvalidInput, InvalidSignature # noqa
18-
from .util import SigningSettings, XMLProcessor
19-
from .util import XMLSecurityDigestAlgorithm as digest_algorithms
20-
from .util import XMLSecuritySignatureMethod as signature_methods
21-
from .util import XMLSignatureMethods as methods
2222
from .util import (
23+
SigningSettings,
24+
XMLProcessor,
2325
_remove_sig,
2426
add_pem_header,
2527
bits_to_bytes_unit,
2628
bytes_to_long,
27-
digest_algorithm_implementations,
2829
ds_tag,
2930
dsig11_tag,
3031
ec_tag,

signxml/util/__init__.py

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import textwrap
1212
from base64 import b64decode, b64encode
1313
from dataclasses import dataclass
14-
from enum import Enum, auto
1514
from typing import Any, List, Optional
1615
from xml.etree import ElementTree as stdlibElementTree
1716

@@ -24,108 +23,6 @@
2423
PEM_FOOTER = "-----END CERTIFICATE-----"
2524

2625

27-
class XMLSignatureMethods(Enum):
28-
enveloped = auto()
29-
enveloping = auto()
30-
detached = auto()
31-
32-
33-
class FragmentLookupMixin:
34-
@classmethod
35-
def from_fragment(cls, fragment):
36-
for i in cls: # type: ignore
37-
if i.value.endswith("#" + fragment):
38-
return i
39-
else:
40-
raise InvalidInput(f"Unrecognized {cls.__name__} identifier fragment: {fragment}")
41-
42-
43-
class InvalidInputErrorMixin:
44-
@classmethod
45-
def _missing_(cls, value):
46-
raise InvalidInput(f"Unrecognized {cls.__name__}: {value}")
47-
48-
49-
class XMLSecurityDigestAlgorithm(FragmentLookupMixin, InvalidInputErrorMixin, Enum):
50-
SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1"
51-
SHA224 = "http://www.w3.org/2001/04/xmldsig-more#sha224"
52-
SHA384 = "http://www.w3.org/2001/04/xmldsig-more#sha384"
53-
SHA256 = "http://www.w3.org/2001/04/xmlenc#sha256"
54-
SHA512 = "http://www.w3.org/2001/04/xmlenc#sha512"
55-
SHA3_224 = "http://www.w3.org/2007/05/xmldsig-more#sha3-224"
56-
SHA3_256 = "http://www.w3.org/2007/05/xmldsig-more#sha3-256"
57-
SHA3_384 = "http://www.w3.org/2007/05/xmldsig-more#sha3-384"
58-
SHA3_512 = "http://www.w3.org/2007/05/xmldsig-more#sha3-512"
59-
60-
@property
61-
def implementation(self):
62-
return digest_algorithm_implementations[self]
63-
64-
65-
# TODO: check if padding errors are fixed by using padding=MGF1
66-
class XMLSecuritySignatureMethod(FragmentLookupMixin, InvalidInputErrorMixin, Enum):
67-
DSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
68-
HMAC_SHA1 = "http://www.w3.org/2000/09/xmldsig#hmac-sha1"
69-
RSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
70-
ECDSA_SHA1 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1"
71-
ECDSA_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha224"
72-
ECDSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"
73-
ECDSA_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384"
74-
ECDSA_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512"
75-
HMAC_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha224"
76-
HMAC_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"
77-
HMAC_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384"
78-
HMAC_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512"
79-
RSA_SHA224 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha224"
80-
RSA_SHA256 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
81-
RSA_SHA384 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384"
82-
RSA_SHA512 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"
83-
RSA_PSS = "http://www.w3.org/2007/05/xmldsig-more#rsa-pss"
84-
DSA_SHA256 = "http://www.w3.org/2009/xmldsig11#dsa-sha256"
85-
ECDSA_SHA3_224 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-224"
86-
ECDSA_SHA3_256 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-256"
87-
ECDSA_SHA3_384 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-384"
88-
ECDSA_SHA3_512 = "http://www.w3.org/2021/04/xmldsig-more#ecdsa-sha3-512"
89-
EDDSA_ED25519 = "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed25519"
90-
EDDSA_ED448 = "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed448"
91-
92-
93-
digest_algorithm_implementations = {
94-
XMLSecurityDigestAlgorithm.SHA1: hashes.SHA1,
95-
XMLSecurityDigestAlgorithm.SHA224: hashes.SHA224,
96-
XMLSecurityDigestAlgorithm.SHA384: hashes.SHA384,
97-
XMLSecurityDigestAlgorithm.SHA256: hashes.SHA256,
98-
XMLSecurityDigestAlgorithm.SHA512: hashes.SHA512,
99-
XMLSecurityDigestAlgorithm.SHA3_224: hashes.SHA3_224,
100-
XMLSecurityDigestAlgorithm.SHA3_256: hashes.SHA3_256,
101-
XMLSecurityDigestAlgorithm.SHA3_384: hashes.SHA3_384,
102-
XMLSecurityDigestAlgorithm.SHA3_512: hashes.SHA3_512,
103-
XMLSecuritySignatureMethod.DSA_SHA1: hashes.SHA1,
104-
XMLSecuritySignatureMethod.HMAC_SHA1: hashes.SHA1,
105-
XMLSecuritySignatureMethod.RSA_SHA1: hashes.SHA1,
106-
XMLSecuritySignatureMethod.ECDSA_SHA1: hashes.SHA1,
107-
XMLSecuritySignatureMethod.ECDSA_SHA224: hashes.SHA224,
108-
XMLSecuritySignatureMethod.ECDSA_SHA256: hashes.SHA256,
109-
XMLSecuritySignatureMethod.ECDSA_SHA384: hashes.SHA384,
110-
XMLSecuritySignatureMethod.ECDSA_SHA512: hashes.SHA512,
111-
XMLSecuritySignatureMethod.HMAC_SHA224: hashes.SHA224,
112-
XMLSecuritySignatureMethod.HMAC_SHA256: hashes.SHA256,
113-
XMLSecuritySignatureMethod.HMAC_SHA384: hashes.SHA384,
114-
XMLSecuritySignatureMethod.HMAC_SHA512: hashes.SHA512,
115-
XMLSecuritySignatureMethod.RSA_SHA224: hashes.SHA224,
116-
XMLSecuritySignatureMethod.RSA_SHA256: hashes.SHA256,
117-
XMLSecuritySignatureMethod.RSA_SHA384: hashes.SHA384,
118-
XMLSecuritySignatureMethod.RSA_SHA512: hashes.SHA512,
119-
XMLSecuritySignatureMethod.DSA_SHA256: hashes.SHA256,
120-
XMLSecuritySignatureMethod.ECDSA_SHA3_224: hashes.SHA1,
121-
XMLSecuritySignatureMethod.ECDSA_SHA3_256: hashes.SHA1,
122-
XMLSecuritySignatureMethod.ECDSA_SHA3_384: hashes.SHA1,
123-
XMLSecuritySignatureMethod.ECDSA_SHA3_512: hashes.SHA1,
124-
XMLSecuritySignatureMethod.EDDSA_ED25519: hashes.SHA512,
125-
XMLSecuritySignatureMethod.EDDSA_ED448: hashes.SHAKE256,
126-
}
127-
128-
12926
class Namespace(dict):
13027
def __getattr__(self, a):
13128
return dict.__getitem__(self, a)

signxml/xades/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@
5050
from OpenSSL.crypto import FILETYPE_ASN1, FILETYPE_PEM, X509, dump_certificate, load_certificate
5151

5252
from .. import VerifyResult, XMLSignatureProcessor, XMLSigner, XMLVerifier
53+
from ..algorithms import XMLSecurityDigestAlgorithm as digest_algorithms
5354
from ..exceptions import InvalidDigest, InvalidInput
54-
from ..util import SigningSettings
55-
from ..util import XMLSecurityDigestAlgorithm as digest_algorithms
56-
from ..util import add_pem_header, ds_tag, namespaces, xades_tag
55+
from ..util import SigningSettings, add_pem_header, ds_tag, namespaces, xades_tag
5756

5857
# TODO: make this a dataclass
5958
default_data_object_format = {"Description": "Default XAdES payload description", "MimeType": "text/xml"}

0 commit comments

Comments
 (0)