|
1 | 1 | from cryptography.hazmat.backends import default_backend |
2 | | -from cryptography.hazmat.primitives import serialization |
3 | 2 | from cryptography.hazmat.primitives.asymmetric import ec |
4 | 3 |
|
5 | 4 | from ..exception import DeSerializationNotPossible |
|
9 | 8 | from ..utils import deser |
10 | 9 | from ..utils import long_to_base64 |
11 | 10 | from .asym import AsymmetricKey |
| 11 | +from .x509 import import_private_key_from_pem_file |
12 | 12 | from .x509 import import_public_key_from_pem_data |
13 | 13 | from .x509 import import_public_key_from_pem_file |
14 | 14 |
|
@@ -66,38 +66,6 @@ def ec_construct_private(num): |
66 | 66 | return priv_ecpn.private_key(default_backend()) |
67 | 67 |
|
68 | 68 |
|
69 | | -def import_private_key_from_file(filename, passphrase=None): |
70 | | - """ |
71 | | - Read a private Elliptic Curve key from a PEM file. |
72 | | -
|
73 | | - :param filename: The name of the file |
74 | | - :param passphrase: A pass phrase to use to unpack the PEM file. |
75 | | - :return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey |
76 | | - instance |
77 | | - """ |
78 | | - with open(filename, "rb") as key_file: |
79 | | - private_key = serialization.load_pem_private_key( |
80 | | - key_file.read(), password=passphrase, backend=default_backend() |
81 | | - ) |
82 | | - |
83 | | - return private_key |
84 | | - |
85 | | - |
86 | | -def import_public_key_from_file(filename): |
87 | | - """ |
88 | | - Read a public Elliptic Curve key from a PEM file. |
89 | | -
|
90 | | - :param filename: The name of the file |
91 | | - :return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey |
92 | | - instance |
93 | | - """ |
94 | | - with open(filename, "rb") as key_file: |
95 | | - public_key = serialization.load_pem_public_key( |
96 | | - key_file.read(), backend=default_backend() |
97 | | - ) |
98 | | - return public_key |
99 | | - |
100 | | - |
101 | 69 | class ECKey(AsymmetricKey): |
102 | 70 | """ |
103 | 71 | JSON Web key representation of a Elliptic curve key. |
@@ -250,7 +218,7 @@ def load(self, filename): |
250 | 218 |
|
251 | 219 | :param filename: File name |
252 | 220 | """ |
253 | | - return self.load_key(import_private_key_from_file(filename)) |
| 221 | + return self.load_key(import_private_ec_key_from_file(filename)) |
254 | 222 |
|
255 | 223 | def decryption_key(self): |
256 | 224 | """ |
@@ -334,6 +302,22 @@ def import_public_ec_key_from_file(filename): |
334 | 302 | return ValueError("Not a Elliptic Curve key") |
335 | 303 |
|
336 | 304 |
|
| 305 | +def import_private_ec_key_from_file(filename, passphrase=None): |
| 306 | + """ |
| 307 | + Read a private Elliptic Curve key from a PEM file. |
| 308 | +
|
| 309 | + :param filename: The name of the file |
| 310 | + :param passphrase: A pass phrase to use to unpack the PEM file. |
| 311 | + :return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey |
| 312 | + instance |
| 313 | + """ |
| 314 | + private_key = import_private_key_from_pem_file(filename, passphrase) |
| 315 | + if isinstance(private_key, ec.EllipticCurvePrivateKey): |
| 316 | + return private_key |
| 317 | + else: |
| 318 | + return ValueError("Not a private Elliptic Curve key") |
| 319 | + |
| 320 | + |
337 | 321 | def import_ec_key(pem_data): |
338 | 322 | """ |
339 | 323 | Extract an Elliptic Curve key from a PEM-encoded X.509 certificate |
|
0 commit comments