2323from ..util import to_bytes
2424
2525
26- def load_pem_key (raw : bytes , password : bytes | None = None ) -> Any :
26+ def import_from_ssh_key (raw : bytes ) -> Any :
27+ return load_ssh_public_key (raw , backend = default_backend ())
28+
29+
30+ def import_from_pem_key (raw : bytes , password : bytes | None = None ) -> Any :
2731 key : Any
2832
2933 if b"OPENSSH PRIVATE" in raw :
@@ -91,15 +95,15 @@ def dump_pem_key(
9195class CryptographyBinding (NativeKeyBinding , metaclass = ABCMeta ):
9296 key_type : str
9397 ssh_type : bytes
94- cryptography_native_keys : Any
98+ _cryptography_key_types : Any
9599
96100 @classmethod
97101 def check_ssh_type (cls , value : bytes ) -> bool :
98102 return value .startswith (cls .ssh_type )
99103
100104 @classmethod
101- def check_cryptography_native_key (cls , native_key : Any ) -> bool :
102- return isinstance (native_key , cls .cryptography_native_keys )
105+ def check_cryptography_key (cls , native_key : Any ) -> bool :
106+ return isinstance (native_key , cls ._cryptography_key_types )
103107
104108 @classmethod
105109 def convert_raw_key_to_dict (cls , raw_key : Any , private : bool ) -> DictKey :
@@ -118,13 +122,13 @@ def import_from_dict(cls, value: DictKey) -> Any:
118122 @classmethod
119123 def import_from_bytes (cls , value : bytes , password : Any | None = None ) -> Any :
120124 if cls .check_ssh_type (value ):
121- return load_ssh_public_key (value , backend = default_backend () )
125+ return import_from_ssh_key (value )
122126
123127 if password is not None :
124128 password = to_bytes (password )
125129
126- key = load_pem_key (value , password )
127- if not cls .check_cryptography_native_key (key ):
130+ key = import_from_pem_key (value , password )
131+ if not cls .check_cryptography_key (key ):
128132 raise InvalidKeyTypeError (f"Not a key of: '{ cls .key_type } '" )
129133 return key
130134
@@ -135,11 +139,13 @@ def as_bytes(
135139 private : bool | None = False ,
136140 password : Any | None = None ,
137141 ) -> bytes :
142+ if private is None :
143+ private = key .is_private
144+
138145 if private :
139146 return dump_pem_key (key .private_key , encoding , private , password )
140- elif private is False :
147+ else :
141148 return dump_pem_key (key .public_key , encoding , private , password )
142- return dump_pem_key (key .raw_value , encoding , key .is_private , password )
143149
144150 @staticmethod
145151 @abstractmethod
0 commit comments