|
| 1 | +# © 2017 Creu Blanca |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | + |
| 4 | +import struct |
| 5 | +from base64 import b64decode, b64encode |
| 6 | + |
| 7 | +from asn1crypto.algos import DSASignature |
| 8 | +from cryptography.hazmat.backends import default_backend |
| 9 | +from cryptography.hazmat.primitives.asymmetric import dsa |
| 10 | +from xmlsig.algorithms.base import Algorithm |
| 11 | +from xmlsig.ns import NS_MAP, DSigNs |
| 12 | +from xmlsig.utils import USING_PYTHON2, b64_print, create_node, long_to_bytes |
| 13 | + |
| 14 | + |
| 15 | +def i2osp(x, x_len): |
| 16 | + if x >= pow(256, x_len): |
| 17 | + raise ValueError("integer too large") |
| 18 | + digits = [] |
| 19 | + while x: |
| 20 | + digits.append(int(x % 256)) |
| 21 | + x //= 256 |
| 22 | + for _i in range(x_len - len(digits)): |
| 23 | + digits.append(0) |
| 24 | + return bytearray(digits[::-1]) |
| 25 | + |
| 26 | + |
| 27 | +def os2ip(arr): |
| 28 | + x_len = len(arr) |
| 29 | + x = 0 |
| 30 | + for i in range(x_len): |
| 31 | + if USING_PYTHON2: |
| 32 | + val = struct.unpack("B", arr[i])[0] |
| 33 | + else: |
| 34 | + val = arr[i] |
| 35 | + x = x + (val * pow(256, x_len - i - 1)) |
| 36 | + return x |
| 37 | + |
| 38 | + |
| 39 | +def to_int(element): |
| 40 | + if USING_PYTHON2: |
| 41 | + return struct.unpack("B", element)[0] |
| 42 | + return element |
| 43 | + |
| 44 | + |
| 45 | +class DSAAlgorithm(Algorithm): |
| 46 | + private_key_class = dsa.DSAPrivateKey |
| 47 | + public_key_class = dsa.DSAPublicKey |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def sign(data, private_key, digest): |
| 51 | + return DSASignature.load(private_key.sign(data, digest())).to_p1363() |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def verify(signature_value, data, public_key, digest): |
| 55 | + public_key.verify( |
| 56 | + DSASignature.from_p1363(b64decode(signature_value)).dump(), data, digest() |
| 57 | + ) |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def key_value(node, public_key): |
| 61 | + result = create_node("DSAKeyValue", node, DSigNs, "\n", "\n") |
| 62 | + public_numbers = public_key.public_numbers() |
| 63 | + if public_numbers.parameter_numbers.p is not None: |
| 64 | + create_node( |
| 65 | + "P", |
| 66 | + result, |
| 67 | + DSigNs, |
| 68 | + tail="\n", |
| 69 | + text="\n" |
| 70 | + + b64_print( |
| 71 | + b64encode(long_to_bytes(public_numbers.parameter_numbers.p)) |
| 72 | + ) |
| 73 | + + "\n", |
| 74 | + ) |
| 75 | + if public_numbers.parameter_numbers.q is not None: |
| 76 | + create_node( |
| 77 | + "Q", |
| 78 | + result, |
| 79 | + DSigNs, |
| 80 | + tail="\n", |
| 81 | + text="\n" |
| 82 | + + b64_print( |
| 83 | + b64encode(long_to_bytes(public_numbers.parameter_numbers.q)) |
| 84 | + ) |
| 85 | + + "\n", |
| 86 | + ) |
| 87 | + if public_numbers.parameter_numbers.g is not None: |
| 88 | + create_node( |
| 89 | + "G", |
| 90 | + result, |
| 91 | + DSigNs, |
| 92 | + tail="\n", |
| 93 | + text="\n" |
| 94 | + + b64_print( |
| 95 | + b64encode(long_to_bytes(public_numbers.parameter_numbers.g)) |
| 96 | + ) |
| 97 | + + "\n", |
| 98 | + ) |
| 99 | + if public_numbers.y is not None: |
| 100 | + create_node( |
| 101 | + "Y", |
| 102 | + result, |
| 103 | + DSigNs, |
| 104 | + tail="\n", |
| 105 | + text="\n" |
| 106 | + + b64_print(b64encode(long_to_bytes(public_numbers.y))) |
| 107 | + + "\n", |
| 108 | + ) |
| 109 | + return result |
| 110 | + |
| 111 | + @staticmethod |
| 112 | + def get_public_key(key_info, context): |
| 113 | + key = key_info.find("ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue", namespaces=NS_MAP) |
| 114 | + if key is not None: |
| 115 | + p = os2ip(b64decode(key.find("ds:P", namespaces=NS_MAP).text)) |
| 116 | + q = os2ip(b64decode(key.find("ds:Q", namespaces=NS_MAP).text)) |
| 117 | + g = os2ip(b64decode(key.find("ds:G", namespaces=NS_MAP).text)) |
| 118 | + y = os2ip(b64decode(key.find("ds:Y", namespaces=NS_MAP).text)) |
| 119 | + return dsa.DSAPublicNumbers(y, dsa.DSAParameterNumbers(p, q, g)).public_key( |
| 120 | + default_backend() |
| 121 | + ) |
| 122 | + return super(DSAAlgorithm, DSAAlgorithm).get_public_key(key_info, context) |
0 commit comments