Skip to content

Commit 367e97c

Browse files
committed
Fix bug in generating EC curves
The EC curves use 'crv' to store the curve name, not curve. Support both and make sure params['crv'] is set once generation is done or the generated key will be non-functional as get_curve() will fail. Signed-off-by: Simo Sorce <[email protected]> Closes #12
1 parent ca203fc commit 367e97c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

jwcrypto/jwk.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,16 @@ def _generate_EC(self, params):
250250
if 'curve' in params:
251251
curve = params['curve']
252252
del params['curve']
253-
key = ec.generate_private_key(self._get_curve_by_name(curve),
254-
default_backend())
253+
# 'curve' is for backwards compat, if 'crv' is defined it takes
254+
# precedence
255+
if 'crv' in params:
256+
curve = params['crv']
257+
del params['crv']
258+
curve_name = self._get_curve_by_name(curve)
259+
key = ec.generate_private_key(curve_name, default_backend())
255260
pn = key.private_numbers()
256261
params['kty'] = 'EC'
262+
params['crv'] = curve
257263
params['x'] = self._encode_int(pn.public_numbers.x)
258264
params['y'] = self._encode_int(pn.public_numbers.y)
259265
params['d'] = self._encode_int(pn.private_value)

jwcrypto/tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ def test_generate_oct_key(self):
204204
E.deserialize(e, key)
205205
self.assertEqual(E.payload.decode('utf-8'), 'test')
206206

207+
def test_generate_EC_key(self):
208+
# Backwards compat curve
209+
key = jwk.JWK(generate='EC', curve='P-256')
210+
key.get_curve('P-256')
211+
# New param
212+
key = jwk.JWK(generate='EC', crv='P-521')
213+
key.get_curve('P-521')
214+
# New param prevails
215+
key = jwk.JWK(generate='EC', curve='P-256', crv='P-521')
216+
key.get_curve('P-521')
217+
218+
207219
# RFC 7515 - A.1
208220
A1_protected = \
209221
[123, 34, 116, 121, 112, 34, 58, 34, 74, 87, 84, 34, 44, 13, 10, 32,

0 commit comments

Comments
 (0)