Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions jwcrypto/jwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ def sign(self, key, payload):

def verify(self, key, payload, signature):
pkey = key.get_op_key('verify', self._curve)
r = signature[:len(signature) // 2]
s = signature[len(signature) // 2:]
byte_length = (pkey.key_size + 7) // 8
if len(signature) != byte_length * 2:
raise InvalidSignature('Invalid signature length')
r = signature[:byte_length]
s = signature[byte_length:]
enc_signature = ec_utils.encode_dss_signature(
int(hexlify(r), 16), int(hexlify(s), 16))
pkey.verify(enc_signature, payload, ec.ECDSA(self.hashfn))
Expand Down
27 changes: 27 additions & 0 deletions jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,33 @@ def test_wycheproof_tcId_374(self):
with self.assertRaises(jws.InvalidJWSObject):
obj.deserialize(compact, key)

def test_ecdsa_signature_length_validation(self):
for crv, alg, coord_len in [
('P-256', 'ES256', 32),
('P-384', 'ES384', 48),
('P-521', 'ES512', 66),
]:
key = jwk.JWK.generate(kty='EC', crv=crv)
s = jws.JWS(b'test-payload')
s.add_signature(key, protected={'alg': alg})
hdr_b64, pay_b64, sig_b64 = s.serialize(compact=True).split('.')
raw_sig = base64url_decode(sig_b64)
r = raw_sig[:coord_len]
s_part = raw_sig[coord_len:]

for bad_raw_sig in [
r + b'\x00' + s_part,
b'\x00' + r + b'\x00' + s_part,
raw_sig[:-1],
b'',
]:
bad_tok = '.'.join([
hdr_b64, pay_b64, base64url_encode(bad_raw_sig)
])
v = jws.JWS()
with self.assertRaises(jws.InvalidJWSSignature):
v.deserialize(bad_tok, key.public())


E_A1_plaintext = \
[84, 104, 101, 32, 116, 114, 117, 101, 32, 115, 105, 103, 110, 32,
Expand Down