16
16
17
17
from cryptography import x509
18
18
from cryptography .hazmat .primitives import serialization
19
- from cryptography .hazmat .primitives .asymmetric import rsa
19
+ from cryptography .hazmat .primitives .asymmetric import ec , ed25519 , ed448 , rsa
20
20
21
21
import flaky
22
22
@@ -782,6 +782,21 @@ def normalize_privatekey_pem(pem):
782
782
-----END RSA PRIVATE KEY-----
783
783
"""
784
784
785
+ ed25519_private_key_pem = b"""-----BEGIN PRIVATE KEY-----
786
+ MC4CAQAwBQYDK2VwBCIEIKlxBbhVsSURoLTmsu9uTqYH6oF7zpxmp1ZQCAPhDmI2
787
+ -----END PRIVATE KEY-----
788
+ """
789
+
790
+ ed448_private_key_pem = b"""-----BEGIN PRIVATE KEY-----
791
+ MEcCAQAwBQYDK2VxBDsEOcqZ7a3k6JwrJbYO8CNTPT/d7dlWCo5vCf0EYDj79ZvA\n hD8u9EPHlYJw5Y8ZQdH4WmVEfpKA23xkdQ==
792
+ -----END PRIVATE KEY-----
793
+ """
794
+
795
+ x25519_private_key_pem = b"""-----BEGIN PRIVATE KEY-----
796
+ MC4CAQAwBQYDK2VuBCIEIPAjVfPNTm25VxtBRg+JjjFx9tA3M8aaBdVhjb92iBts
797
+ -----END PRIVATE KEY-----
798
+ """
799
+
785
800
786
801
@pytest .fixture
787
802
def x509_data ():
@@ -1012,15 +1027,35 @@ class TestPKey:
1012
1027
Tests for `OpenSSL.crypto.PKey`.
1013
1028
"""
1014
1029
1015
- def test_convert_from_cryptography_private_key (self ):
1030
+ @pytest .mark .parametrize (
1031
+ ("key_string" , "key_type" ),
1032
+ [
1033
+ (intermediate_key_pem , rsa .RSAPrivateKey ),
1034
+ (ec_private_key_pem , ec .EllipticCurvePrivateKey ),
1035
+ (ed25519_private_key_pem , ed25519 .Ed25519PrivateKey ),
1036
+ (ed448_private_key_pem , ed448 .Ed448PrivateKey ),
1037
+ ],
1038
+ )
1039
+ def test_convert_roundtrip_cryptography_private_key (
1040
+ self , key_string , key_type
1041
+ ):
1016
1042
"""
1017
1043
PKey.from_cryptography_key creates a proper private PKey.
1044
+ PKey.to_cryptography_key creates a proper cryptography private key.
1018
1045
"""
1019
- key = serialization .load_pem_private_key (intermediate_key_pem , None )
1046
+ key = serialization .load_pem_private_key (key_string , None )
1020
1047
pkey = PKey .from_cryptography_key (key )
1021
1048
1022
1049
assert isinstance (pkey , PKey )
1023
- assert pkey .bits () == key .key_size
1050
+ parsed_key = pkey .to_cryptography_key ()
1051
+ assert isinstance (parsed_key , key_type )
1052
+ assert parsed_key .public_key ().public_bytes (
1053
+ serialization .Encoding .PEM ,
1054
+ serialization .PublicFormat .SubjectPublicKeyInfo ,
1055
+ ) == key .public_key ().public_bytes (
1056
+ serialization .Encoding .PEM ,
1057
+ serialization .PublicFormat .SubjectPublicKeyInfo ,
1058
+ )
1024
1059
assert pkey ._only_public is False
1025
1060
assert pkey ._initialized is True
1026
1061
@@ -1040,7 +1075,7 @@ def test_convert_from_cryptography_unsupported_type(self):
1040
1075
"""
1041
1076
PKey.from_cryptography_key raises TypeError with an unsupported type.
1042
1077
"""
1043
- key = serialization .load_pem_private_key (ec_private_key_pem , None )
1078
+ key = serialization .load_pem_private_key (x25519_private_key_pem , None )
1044
1079
with pytest .raises (TypeError ):
1045
1080
PKey .from_cryptography_key (key )
1046
1081
@@ -1054,16 +1089,6 @@ def test_convert_public_pkey_to_cryptography_key(self):
1054
1089
assert isinstance (key , rsa .RSAPublicKey )
1055
1090
assert pkey .bits () == key .key_size
1056
1091
1057
- def test_convert_private_pkey_to_cryptography_key (self ):
1058
- """
1059
- PKey.to_cryptography_key creates a proper cryptography private key.
1060
- """
1061
- pkey = load_privatekey (FILETYPE_PEM , root_key_pem )
1062
- key = pkey .to_cryptography_key ()
1063
-
1064
- assert isinstance (key , rsa .RSAPrivateKey )
1065
- assert pkey .bits () == key .key_size
1066
-
1067
1092
def test_type (self ):
1068
1093
"""
1069
1094
`PKey` can be used to create instances of that type.
0 commit comments