Skip to content

Commit 09e46a2

Browse files
committed
Got it working :-)
1 parent 174b19e commit 09e46a2

3 files changed

Lines changed: 53 additions & 44 deletions

File tree

src/cryptojwt/jwk/ec.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# This is used to translate between the curve representation in
1313
# Cryptography and the one used by NIST (and in RFC 7518)
1414
NIST2SEC = {
15+
'B-571': ec.SECT571R1,
1516
'K-571': ec.SECT571K1,
1617
'K-409': ec.SECT409K1,
1718
'K-283': ec.SECT283K1,
@@ -59,40 +60,40 @@ def ec_construct_private(num):
5960
return priv_ecpn.private_key(default_backend())
6061

6162

62-
# def import_private_key_from_file(filename, passphrase=None):
63-
# """
64-
# Read a private Elliptic Curve key from a PEM file.
65-
#
66-
# :param filename: The name of the file
67-
# :param passphrase: A pass phrase to use to unpack the PEM file.
68-
# :return: A
69-
# cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
70-
# instance
71-
# """
72-
# with open(filename, "rb") as key_file:
73-
# private_key = serialization.load_pem_private_key(
74-
# key_file.read(),
75-
# password=passphrase,
76-
# backend=default_backend())
77-
#
78-
# return private_key
79-
#
80-
#
81-
# def import_public_key_from_file(filename):
82-
# """
83-
# Read a public Elliptic Curve key from a PEM file.
84-
#
85-
# :param filename: The name of the file
86-
# :param passphrase: A pass phrase to use to unpack the PEM file.
87-
# :return: A
88-
# cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
89-
# instance
90-
# """
91-
# with open(filename, "rb") as key_file:
92-
# public_key = serialization.load_pem_public_key(
93-
# key_file.read(),
94-
# backend=default_backend())
95-
# return public_key
63+
def import_private_key_from_file(filename, passphrase=None):
64+
"""
65+
Read a private Elliptic Curve key from a PEM file.
66+
67+
:param filename: The name of the file
68+
:param passphrase: A pass phrase to use to unpack the PEM file.
69+
:return: A
70+
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
71+
instance
72+
"""
73+
with open(filename, "rb") as key_file:
74+
private_key = serialization.load_pem_private_key(
75+
key_file.read(),
76+
password=passphrase,
77+
backend=default_backend())
78+
79+
return private_key
80+
81+
82+
def import_public_key_from_file(filename):
83+
"""
84+
Read a public Elliptic Curve key from a PEM file.
85+
86+
: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
90+
instance
91+
"""
92+
with open(filename, "rb") as key_file:
93+
public_key = serialization.load_pem_public_key(
94+
key_file.read(),
95+
backend=default_backend())
96+
return public_key
9697

9798

9899
class ECKey(AsymmetricKey):
@@ -242,13 +243,13 @@ def load_key(self, key):
242243

243244
return self
244245

245-
# def load(self, filename):
246-
# """
247-
# Load an Elliptic curve key from a file.
248-
#
249-
# :param filename: File name
250-
# """
251-
# return self.load_key(import_private_key_from_file(filename))
246+
def load(self, filename):
247+
"""
248+
Load an Elliptic curve key from a file.
249+
250+
:param filename: File name
251+
"""
252+
return self.load_key(import_private_key_from_file(filename))
252253

253254
def decryption_key(self):
254255
"""

tests/570-ec-sect571r1-keypair.pem

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MIHuAgEBBEgBH3PwaVkfMDZwbwQB0FlKTYFTObjkNxRo++SRg8XOIpNCJk118Z26
3+
pjVLS9UadaisLopUMsZ+f/pQITUhbPl//0rRBfM2KtigBwYFK4EEACehgZUDgZIA
4+
BAIZ0Rc0Y3jsqPqqptRz3tiSAuvTHA9vUigM2gUjM6YkTKofP7RRls4dqt6aM7/1
5+
eLbFg4Jdh9DXS4zU1EFeiZQZ+drSQYAmAgAtTzpmtmUoy+miwtiSBomu3CSUe6Yr
6+
VvWb+Oirmvw2x3BCTJW2Xjhy5y6tDPVRRyhg0nh5wm/UxZv4jo7AZuJV8ztZKwCE
7+
AA==
8+
-----END EC PRIVATE KEY-----

tests/test_02_jwk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def test_load_pem_file_rsa():
570570
assert key.has_private_key()
571571

572572

573-
# def test_load_pem_file_ec():
574-
# key = ECKey().load(full_path('ec.pem'))
575-
# assert key.has_private_key()
573+
def test_load_pem_file_ec():
574+
key = ECKey().load(full_path('570-ec-sect571r1-keypair.pem'))
575+
assert key.has_private_key()
576576

0 commit comments

Comments
 (0)