Skip to content

Commit 7c4e16d

Browse files
authored
Merge pull request #4 from jschlyter/fix_dsasigner
Fix DSASigner
2 parents 8c6084d + 8723871 commit 7c4e16d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/cryptojwt/jws.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from cryptography.hazmat.primitives.asymmetric import ec
1212
from cryptography.hazmat.primitives.asymmetric import padding
1313
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
1416

1517
try:
1618
from builtins import str
@@ -174,16 +176,30 @@ def __init__(self, algorithm='SHA256'):
174176
raise UnSupported('algorithm: {}'.format(algorithm))
175177

176178
def sign(self, msg, key):
177-
return key.sign(msg, ec.ECDSA(self.hash_algorithm()))
179+
# cryptography returns ASN.1-encoded signature data; decode as JWS uses raw signatures (r||s)
180+
asn1sig = key.sign(msg, ec.ECDSA(self.hash_algorithm()))
181+
(r, s) = decode_dss_signature(asn1sig)
182+
return int_to_bytes(r) + int_to_bytes(s)
178183

179184
def verify(self, msg, sig, key):
180185
try:
181-
key.verify(sig, msg, ec.ECDSA(self.hash_algorithm()))
186+
# cryptography uses ASN.1-encoded signature data; split JWS signature (r||s) and encode before verification
187+
(r, s) = self._split_raw_signature(sig)
188+
asn1sig = encode_dss_signature(r, s)
189+
key.verify(asn1sig, msg, ec.ECDSA(self.hash_algorithm()))
182190
except InvalidSignature as err:
183191
raise BadSignature(err)
184192
else:
185193
return True
186194

195+
@staticmethod
196+
def _split_raw_signature(sig):
197+
"""Split raw signature into components"""
198+
c_length = len(sig) // 2
199+
r = int_from_bytes(sig[:c_length], byteorder='big')
200+
s = int_from_bytes(sig[c_length:], byteorder='big')
201+
return (r, s)
202+
187203

188204
class PSSSigner(Signer):
189205
def __init__(self, algorithm='SHA256'):

0 commit comments

Comments
 (0)