Skip to content

Commit 0ce5e71

Browse files
committed
To fix the length of the octet string describing elliptic curve coordinates.
1 parent 1f76fbd commit 0ce5e71

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/cryptojwt/jwk/ec.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ def ec_construct_public(num):
3535
public key instance.
3636
3737
:param num: A dictionary with public attributes and their values
38-
:return: A
39-
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
38+
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
4039
instance.
4140
"""
4241
ecpn = ec.EllipticCurvePublicNumbers(num['x'], num['y'],
@@ -50,8 +49,7 @@ def ec_construct_private(num):
5049
curve private key instance.
5150
5251
:param num: A dictionary with public and private attributes and their values
53-
:return: A
54-
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
52+
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
5553
instance.
5654
"""
5755
pub_ecpn = ec.EllipticCurvePublicNumbers(num['x'], num['y'],
@@ -66,8 +64,7 @@ def import_private_key_from_file(filename, passphrase=None):
6664
6765
:param filename: The name of the file
6866
:param passphrase: A pass phrase to use to unpack the PEM file.
69-
:return: A
70-
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
67+
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
7168
instance
7269
"""
7370
with open(filename, "rb") as key_file:
@@ -84,9 +81,7 @@ def import_public_key_from_file(filename):
8481
Read a public Elliptic Curve key from a PEM file.
8582
8683
:param filename: The name of the file
87-
:param passphrase: A pass phrase to use to unpack the PEM file.
88-
:return: A
89-
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
84+
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
9085
instance
9186
"""
9287
with open(filename, "rb") as key_file:
@@ -188,17 +183,18 @@ def deserialize(self):
188183
{'x': _x, 'y': _y, 'crv': self.crv})
189184

190185
def _serialize(self, key):
186+
mlen = int(key.key_size/8)
191187
if isinstance(key, ec.EllipticCurvePublicKey):
192188
pn = key.public_numbers()
193-
self.x = long_to_base64(pn.x)
194-
self.y = long_to_base64(pn.y)
189+
self.x = long_to_base64(pn.x, mlen)
190+
self.y = long_to_base64(pn.y, mlen)
195191
self.crv = SEC2NIST[pn.curve.name]
196192
elif isinstance(key, ec.EllipticCurvePrivateKey):
197193
pn = key.private_numbers()
198-
self.x = long_to_base64(pn.public_numbers.x)
199-
self.y = long_to_base64(pn.public_numbers.y)
194+
self.x = long_to_base64(pn.public_numbers.x, mlen)
195+
self.y = long_to_base64(pn.public_numbers.y, mlen)
200196
self.crv = SEC2NIST[pn.public_numbers.curve.name]
201-
self.d = long_to_base64(pn.private_value)
197+
self.d = long_to_base64(pn.private_value, mlen)
202198

203199
def serialize(self, private=False):
204200
"""

src/cryptojwt/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ def long2intarr(long_int):
3030
return _bytes
3131

3232

33-
def long_to_base64(n):
33+
def long_to_base64(n, mlen=0):
3434
bys = long2intarr(n)
35+
_len = mlen - len(bys)
36+
if _len:
37+
bys = [0] * _len + bys
3538
data = struct.pack('%sB' % len(bys), *bys)
3639
if not len(data):
3740
data = '\x00'

0 commit comments

Comments
 (0)