Skip to content

Commit e598597

Browse files
author
Roland Hedberg
committed
Merge branch 'master' of github.com:rohe/pyjwkest
2 parents 87c451b + 7f94c0c commit e598597

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

src/jwkest/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class Invalid(JWKESTException):
3535
"""The JWT is invalid."""
3636

3737

38+
class WrongNumberOfParts(Invalid):
39+
pass
40+
41+
3842
class BadSyntax(Invalid):
3943
"""The JWT could not be parsed because the syntax is invalid."""
4044

src/jwkest/jwe.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from Crypto.Cipher import PKCS1_v1_5
1919
from Crypto.Cipher import PKCS1_OAEP
2020

21-
from jwkest import b64d, as_bytes
21+
from jwkest import b64d, as_bytes, WrongNumberOfParts
2222
from jwkest import b64e
2323
from jwkest import JWKESTException
2424
from jwkest import MissingKey
@@ -307,6 +307,9 @@ def is_jwe(self):
307307
return False
308308
return True
309309

310+
def __len__(self):
311+
return len(self.part)
312+
310313

311314
class JWe(JWx):
312315
@staticmethod
@@ -430,6 +433,9 @@ def decrypt(self, token, key=None, cek=None):
430433

431434
jwe = JWEnc().unpack(token)
432435

436+
if len(jwe) != 5:
437+
raise WrongNumberOfParts(len(jwe))
438+
433439
if not cek:
434440
jek = jwe.encrypted_key()
435441
# The iv for this function must be 64 bit

src/jwkest/jws.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from Crypto.Util.number import bytes_to_long
2424
import sys
2525

26-
from jwkest import b64d, as_unicode
26+
from jwkest import b64d, as_unicode, Invalid, WrongNumberOfParts
2727
from jwkest import b64e
2828
from jwkest import constant_time_compare
2929
from jwkest import safe_str_cmp
@@ -219,6 +219,15 @@ def sign_input(self):
219219
def signature(self):
220220
return self.part[2]
221221

222+
def __len__(self):
223+
return len(self.part)
224+
225+
def valid(self):
226+
if len(self) != 3:
227+
return False
228+
229+
return True
230+
222231

223232
class JWx(object):
224233
args = ["alg", "jku", "jwk", "x5u", "x5t", "x5c", "kid", "typ", "cty",
@@ -483,6 +492,9 @@ def verify_compact(self, jws, keys=None, allow_none=False, sigalg=None):
483492
:return:
484493
"""
485494
jwt = JWSig().unpack(jws)
495+
if len(jwt) != 3:
496+
raise WrongNumberOfParts(len(jwt))
497+
486498
self.jwt = jwt
487499

488500
try:
@@ -528,7 +540,7 @@ def verify_compact(self, jws, keys=None, allow_none=False, sigalg=None):
528540
try:
529541
res = verifier.verify(jwt.sign_input(), jwt.signature(),
530542
key.get_key(alg=_alg, private=False))
531-
except BadSignature:
543+
except (BadSignature, IndexError):
532544
pass
533545
else:
534546
if res is True:

tests/test_3_jws.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,5 +487,24 @@ def test_dj_usage():
487487
_jwt = factory(sjwt)
488488
assert _jwt.jwt.headers['alg'] == 'RS256'
489489

490+
def test_rs256_rm_signature():
491+
payload = "Please take a moment to register today"
492+
keys = [RSAKey(key=import_rsa_key_from_file(KEY))]
493+
# keys[0]._keytype = "private"
494+
_jws = JWS(payload, alg="RS256")
495+
_jwt = _jws.sign_compact(keys)
496+
497+
p = _jwt.split('.')
498+
_jwt = '.'.join(p[:-1])
499+
500+
_rj = JWS()
501+
try:
502+
_ = _rj.verify_compact(_jwt, keys)
503+
except jwkest.WrongNumberOfParts:
504+
pass
505+
else:
506+
assert False
507+
508+
490509
if __name__ == "__main__":
491-
test_dj_usage()
510+
test_rs256_rm_signature()

0 commit comments

Comments
 (0)