Skip to content

Commit 13a40f0

Browse files
committed
Fixing signature R and S value encoding, add tests for it
1 parent bca54cd commit 13a40f0

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

jwt/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ def prepare_RS_key(key):
107107
from Crypto.Hash import SHA512
108108

109109
signing_methods.update({
110-
'ES256': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha256),
111-
'ES384': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha384),
112-
'ES512': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha512),
110+
'ES256': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der),
111+
'ES384': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha384, sigencode=ecdsa.util.sigencode_der),
112+
'ES512': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha512, sigencode=ecdsa.util.sigencode_der),
113113
})
114114

115115
verify_methods.update({
116-
'ES256': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha256),
117-
'ES384': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha384),
118-
'ES512': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha512),
116+
'ES256': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha256, sigdecode=ecdsa.util.sigdecode_der),
117+
'ES384': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha384, sigdecode=ecdsa.util.sigdecode_der),
118+
'ES512': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha512, sigdecode=ecdsa.util.sigdecode_der),
119119
})
120120

121121
def prepare_ES_key(key):

tests/test_jwt.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def utc_timestamp():
1818
class TestJWT(unittest.TestCase):
1919

2020
def setUp(self):
21-
self.payload = {"iss": "jeff", "exp": utc_timestamp() + 10,
21+
self.payload = {"iss": "jeff", "exp": utc_timestamp() + 15,
2222
"claim": "insanity"}
2323

2424
def test_encode_decode(self):
@@ -76,6 +76,46 @@ def test_decodes_valid_jwt(self):
7676

7777
self.assertEqual(decoded_payload, example_payload)
7878

79+
# 'Control' Elliptic Curve JWT created by another library.
80+
# Used to test for regressions that could affect both
81+
# encoding / decoding operations equally (causing tests
82+
# to still pass).
83+
def test_decodes_valid_es384_jwt(self):
84+
example_payload = {"hello": "world"}
85+
example_pubkey = open('tests/testkey_ec.pub', 'r').read()
86+
example_jwt = (
87+
b"eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9"
88+
b".eyJoZWxsbyI6IndvcmxkIn0"
89+
b".MIGHAkEdh2kR7IRu5w0tGuY6Xz3Vqa7PHHY2DgXWeee"
90+
b"LXotEqpn9udp2NfVL-XFG0TDoCakzXbIGAWg42S69GFl"
91+
b"KZzxhXAJCAPLPuJoKyAixFnXPBkvkti-UzSIj4s6DePe"
92+
b"uTu7102G_QIXiijY5bx6mdmZa3xUuKeu-zobOIOqR8Zw"
93+
b"FqGjBLZum")
94+
decoded_payload = jwt.decode(example_jwt, example_pubkey)
95+
self.assertEqual(decoded_payload, example_payload)
96+
97+
# 'Control' RSA JWT created by another library.
98+
# Used to test for regressions that could affect both
99+
# encoding / decoding operations equally (causing tests
100+
# to still pass).
101+
def test_decodes_valid_rs384_jwt(self):
102+
example_payload = {"hello": "world"}
103+
example_pubkey = open('tests/testkey_rsa.pub', 'r').read()
104+
example_jwt = (
105+
b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9"
106+
b".eyJoZWxsbyI6IndvcmxkIn0"
107+
b".yNQ3nI9vEDs7lEh-Cp81McPuiQ4ZRv6FL4evTYYAh1X"
108+
b"lRTTR3Cz8pPA9Stgso8Ra9xGB4X3rlra1c8Jz10nTUju"
109+
b"O06OMm7oXdrnxp1KIiAJDerWHkQ7l3dlizIk1bmMA457"
110+
b"W2fNzNfHViuED5ISM081dgf_a71qBwJ_yShMMrSOfxDx"
111+
b"mX9c4DjRogRJG8SM5PvpLqI_Cm9iQPGMvmYK7gzcq2cJ"
112+
b"urHRJDJHTqIdpLWXkY7zVikeen6FhuGyn060Dz9gYq9t"
113+
b"uwmrtSWCBUjiN8sqJ00CDgycxKqHfUndZbEAOjcCAhBr"
114+
b"qWW3mSVivUfubsYbwUdUG3fSRPjaUPcpe8A")
115+
decoded_payload = jwt.decode(example_jwt, example_pubkey)
116+
117+
self.assertEqual(decoded_payload, example_payload)
118+
79119
def test_load_verify_valid_jwt(self):
80120
example_payload = {"hello": "world"}
81121
example_secret = "secret"

0 commit comments

Comments
 (0)