|
11 | 11 | from cryptography.hazmat.primitives.asymmetric import ec
|
12 | 12 | from cryptography.hazmat.primitives.asymmetric import padding
|
13 | 13 | from cryptography.hazmat.primitives.asymmetric import utils
|
| 14 | +from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature, encode_dss_signature |
| 15 | +from cryptography.utils import int_from_bytes, int_to_bytes |
14 | 16 |
|
15 | 17 | try:
|
16 | 18 | from builtins import str
|
@@ -174,16 +176,27 @@ def __init__(self, algorithm='SHA256'):
|
174 | 176 | raise UnSupported('algorithm: {}'.format(algorithm))
|
175 | 177 |
|
176 | 178 | def sign(self, msg, key):
|
177 |
| - return key.sign(msg, ec.ECDSA(self.hash_algorithm())) |
| 179 | + asn1sig = key.sign(msg, ec.ECDSA(self.hash_algorithm())) |
| 180 | + (r,s) = decode_dss_signature(asn1sig) |
| 181 | + return int_to_bytes(r) + int_to_bytes(s) |
178 | 182 |
|
179 | 183 | def verify(self, msg, sig, key):
|
180 | 184 | try:
|
181 |
| - key.verify(sig, msg, ec.ECDSA(self.hash_algorithm())) |
| 185 | + (r,s) = self._split_raw(sig) |
| 186 | + asn1sig = encode_dss_signature(r, s) |
| 187 | + key.verify(asn1sig, msg, ec.ECDSA(self.hash_algorithm())) |
182 | 188 | except InvalidSignature as err:
|
183 | 189 | raise BadSignature(err)
|
184 | 190 | else:
|
185 | 191 | return True
|
186 | 192 |
|
| 193 | + @staticmethod |
| 194 | + def _split_raw(sig): |
| 195 | + c_length = len(sig) // 2 |
| 196 | + r = int_from_bytes(sig[:c_length], byteorder='big') |
| 197 | + s = int_from_bytes(sig[c_length:], byteorder='big') |
| 198 | + return (r,s) |
| 199 | + |
187 | 200 |
|
188 | 201 | class PSSSigner(Signer):
|
189 | 202 | def __init__(self, algorithm='SHA256'):
|
|
0 commit comments