@@ -6,6 +6,7 @@ use pyo3::types::{PyAnyMethods, PyBytesMethods};
66
77use crate :: backend:: hashes:: Hash ;
88use crate :: error:: { CryptographyError , CryptographyResult } ;
9+ use crate :: serialization:: Encoding ;
910use crate :: types;
1011
1112pub ( crate ) fn py_int_to_bn (
@@ -42,19 +43,12 @@ pub(crate) fn pkey_private_bytes<'p>(
4243 py : pyo3:: Python < ' p > ,
4344 key_obj : & pyo3:: Bound < ' p , pyo3:: PyAny > ,
4445 pkey : & openssl:: pkey:: PKey < openssl:: pkey:: Private > ,
45- encoding : & pyo3 :: Bound < ' p , pyo3 :: PyAny > ,
46+ encoding : Encoding ,
4647 format : & pyo3:: Bound < ' p , pyo3:: PyAny > ,
4748 encryption_algorithm : & pyo3:: Bound < ' p , pyo3:: PyAny > ,
4849 openssh_allowed : bool ,
4950 raw_allowed : bool ,
5051) -> CryptographyResult < pyo3:: Bound < ' p , pyo3:: types:: PyBytes > > {
51- if !encoding. is_instance ( & types:: ENCODING . get ( py) ?) ? {
52- return Err ( CryptographyError :: from (
53- pyo3:: exceptions:: PyTypeError :: new_err (
54- "encoding must be an item from the Encoding enum" ,
55- ) ,
56- ) ) ;
57- }
5852 if !format. is_instance ( & types:: PRIVATE_FORMAT . get ( py) ?) ? {
5953 return Err ( CryptographyError :: from (
6054 pyo3:: exceptions:: PyTypeError :: new_err (
@@ -70,11 +64,9 @@ pub(crate) fn pkey_private_bytes<'p>(
7064 ) ) ;
7165 }
7266
73- if raw_allowed
74- && ( encoding. is ( & types:: ENCODING_RAW . get ( py) ?)
75- || format. is ( & types:: PRIVATE_FORMAT_RAW . get ( py) ?) )
67+ if raw_allowed && ( encoding == Encoding :: Raw || format. is ( & types:: PRIVATE_FORMAT_RAW . get ( py) ?) )
7668 {
77- if ! encoding. is ( & types :: ENCODING_RAW . get ( py ) ? )
69+ if encoding != Encoding :: Raw
7870 || !format. is ( & types:: PRIVATE_FORMAT_RAW . get ( py) ?)
7971 || !encryption_algorithm. is_instance ( & types:: NO_ENCRYPTION . get ( py) ?) ?
8072 {
@@ -139,14 +131,14 @@ pub(crate) fn pkey_private_bytes<'p>(
139131 }
140132 if let Ok ( rsa) = pkey. rsa ( ) {
141133 let der_bytes = cryptography_key_parsing:: rsa:: serialize_pkcs1_private_key ( & rsa) ?;
142- if encoding. is ( & types :: ENCODING_PEM . get ( py ) ? ) {
134+ if encoding == Encoding :: PEM {
143135 let pem_bytes = cryptography_key_parsing:: pem:: encrypt_pem (
144136 "RSA PRIVATE KEY" ,
145137 & der_bytes,
146138 password,
147139 ) ?;
148140 return Ok ( pyo3:: types:: PyBytes :: new ( py, & pem_bytes) ) ;
149- } else if encoding. is ( & types :: ENCODING_DER . get ( py ) ? ) {
141+ } else if encoding == Encoding :: DER {
150142 if !password. is_empty ( ) {
151143 return Err ( CryptographyError :: from (
152144 pyo3:: exceptions:: PyValueError :: new_err (
@@ -159,14 +151,14 @@ pub(crate) fn pkey_private_bytes<'p>(
159151 }
160152 } else if let Ok ( dsa) = pkey. dsa ( ) {
161153 let der_bytes = cryptography_key_parsing:: dsa:: serialize_pkcs1_private_key ( & dsa) ?;
162- if encoding. is ( & types :: ENCODING_PEM . get ( py ) ? ) {
154+ if encoding == Encoding :: PEM {
163155 let pem_bytes = cryptography_key_parsing:: pem:: encrypt_pem (
164156 "DSA PRIVATE KEY" ,
165157 & der_bytes,
166158 password,
167159 ) ?;
168160 return Ok ( pyo3:: types:: PyBytes :: new ( py, & pem_bytes) ) ;
169- } else if encoding. is ( & types :: ENCODING_DER . get ( py ) ? ) {
161+ } else if encoding == Encoding :: DER {
170162 if !password. is_empty ( ) {
171163 return Err ( CryptographyError :: from (
172164 pyo3:: exceptions:: PyValueError :: new_err (
@@ -179,14 +171,14 @@ pub(crate) fn pkey_private_bytes<'p>(
179171 }
180172 } else if let Ok ( ec) = pkey. ec_key ( ) {
181173 let der_bytes = cryptography_key_parsing:: ec:: serialize_pkcs1_private_key ( & ec, true ) ?;
182- if encoding. is ( & types :: ENCODING_PEM . get ( py ) ? ) {
174+ if encoding == Encoding :: PEM {
183175 let pem_bytes = cryptography_key_parsing:: pem:: encrypt_pem (
184176 "EC PRIVATE KEY" ,
185177 & der_bytes,
186178 password,
187179 ) ?;
188180 return Ok ( pyo3:: types:: PyBytes :: new ( py, & pem_bytes) ) ;
189- } else if encoding. is ( & types :: ENCODING_DER . get ( py ) ? ) {
181+ } else if encoding == Encoding :: DER {
190182 if !password. is_empty ( ) {
191183 return Err ( CryptographyError :: from (
192184 pyo3:: exceptions:: PyValueError :: new_err (
@@ -202,7 +194,7 @@ pub(crate) fn pkey_private_bytes<'p>(
202194
203195 // OpenSSH + PEM
204196 if openssh_allowed && format. is ( & types:: PRIVATE_FORMAT_OPENSSH . get ( py) ?) {
205- if encoding. is ( & types :: ENCODING_PEM . get ( py ) ? ) {
197+ if encoding == Encoding :: PEM {
206198 return Ok ( types:: SERIALIZE_SSH_PRIVATE_KEY
207199 . get ( py) ?
208200 . call1 ( ( key_obj, password, encryption_algorithm) ) ?
@@ -225,18 +217,11 @@ pub(crate) fn pkey_public_bytes<'p>(
225217 py : pyo3:: Python < ' p > ,
226218 key_obj : & pyo3:: Bound < ' p , pyo3:: PyAny > ,
227219 pkey : & openssl:: pkey:: PKey < openssl:: pkey:: Public > ,
228- encoding : & pyo3 :: Bound < ' p , pyo3 :: PyAny > ,
220+ encoding : Encoding ,
229221 format : & pyo3:: Bound < ' p , pyo3:: PyAny > ,
230222 openssh_allowed : bool ,
231223 raw_allowed : bool ,
232224) -> CryptographyResult < pyo3:: Bound < ' p , pyo3:: types:: PyBytes > > {
233- if !encoding. is_instance ( & types:: ENCODING . get ( py) ?) ? {
234- return Err ( CryptographyError :: from (
235- pyo3:: exceptions:: PyTypeError :: new_err (
236- "encoding must be an item from the Encoding enum" ,
237- ) ,
238- ) ) ;
239- }
240225 if !format. is_instance ( & types:: PUBLIC_FORMAT . get ( py) ?) ? {
241226 return Err ( CryptographyError :: from (
242227 pyo3:: exceptions:: PyTypeError :: new_err (
@@ -245,13 +230,8 @@ pub(crate) fn pkey_public_bytes<'p>(
245230 ) ) ;
246231 }
247232
248- if raw_allowed
249- && ( encoding. is ( & types:: ENCODING_RAW . get ( py) ?)
250- || format. is ( & types:: PUBLIC_FORMAT_RAW . get ( py) ?) )
251- {
252- if !encoding. is ( & types:: ENCODING_RAW . get ( py) ?)
253- || !format. is ( & types:: PUBLIC_FORMAT_RAW . get ( py) ?)
254- {
233+ if raw_allowed && ( encoding == Encoding :: Raw || format. is ( & types:: PUBLIC_FORMAT_RAW . get ( py) ?) ) {
234+ if encoding != Encoding :: Raw || !format. is ( & types:: PUBLIC_FORMAT_RAW . get ( py) ?) {
255235 return Err ( CryptographyError :: from (
256236 pyo3:: exceptions:: PyValueError :: new_err (
257237 "When using Raw both encoding and format must be Raw" ,
@@ -270,7 +250,7 @@ pub(crate) fn pkey_public_bytes<'p>(
270250 }
271251
272252 if let Ok ( ec) = pkey. ec_key ( ) {
273- if encoding. is ( & types :: ENCODING_X962 . get ( py ) ? ) {
253+ if encoding == Encoding :: X962 {
274254 let point_form = if format. is ( & types:: PUBLIC_FORMAT_UNCOMPRESSED_POINT . get ( py) ?) {
275255 openssl:: ec:: PointConversionForm :: UNCOMPRESSED
276256 } else if format. is ( & types:: PUBLIC_FORMAT_COMPRESSED_POINT . get ( py) ?) {
@@ -305,7 +285,7 @@ pub(crate) fn pkey_public_bytes<'p>(
305285
306286 // OpenSSH + OpenSSH
307287 if openssh_allowed && format. is ( & types:: PUBLIC_FORMAT_OPENSSH . get ( py) ?) {
308- if encoding. is ( & types :: ENCODING_OPENSSH . get ( py ) ? ) {
288+ if encoding == Encoding :: OpenSSH {
309289 return Ok ( types:: SERIALIZE_SSH_PUBLIC_KEY
310290 . get ( py) ?
311291 . call1 ( ( key_obj, ) ) ?
0 commit comments