|
11 | 11 | import textwrap |
12 | 12 | from base64 import b64decode, b64encode |
13 | 13 | from dataclasses import dataclass |
14 | | -from enum import Enum, auto |
15 | 14 | from typing import Any, List, Optional |
16 | 15 | from xml.etree import ElementTree as stdlibElementTree |
17 | 16 |
|
|
24 | 23 | PEM_FOOTER = "-----END CERTIFICATE-----" |
25 | 24 |
|
26 | 25 |
|
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 | | - |
129 | 26 | class Namespace(dict): |
130 | 27 | def __getattr__(self, a): |
131 | 28 | return dict.__getitem__(self, a) |
|
0 commit comments