From d6d484bdc06260f9bd2b138c498dc7ec814e74a0 Mon Sep 17 00:00:00 2001 From: Facundo Tuesca Date: Thu, 4 Apr 2024 15:40:14 +0200 Subject: [PATCH] Convert `private_bytes` methods to new pyo3 APIs --- src/rust/src/backend/dh.rs | 6 +++--- src/rust/src/backend/dsa.rs | 6 +++--- src/rust/src/backend/ec.rs | 6 +++--- src/rust/src/backend/ed25519.rs | 6 +++--- src/rust/src/backend/ed448.rs | 6 +++--- src/rust/src/backend/rsa.rs | 10 +++++----- src/rust/src/backend/utils.rs | 28 ++++++++++++++-------------- src/rust/src/backend/x25519.rs | 6 +++--- src/rust/src/backend/x448.rs | 6 +++--- 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/rust/src/backend/dh.rs b/src/rust/src/backend/dh.rs index 2eb9189bb1ce..5e84febbc1c1 100644 --- a/src/rust/src/backend/dh.rs +++ b/src/rust/src/backend/dh.rs @@ -224,9 +224,9 @@ impl DHPrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { if !format.is(types::PRIVATE_FORMAT_PKCS8.get(py)?) { return Err(CryptographyError::from( diff --git a/src/rust/src/backend/dsa.rs b/src/rust/src/backend/dsa.rs index a62de7c73239..0bcfd2bf7120 100644 --- a/src/rust/src/backend/dsa.rs +++ b/src/rust/src/backend/dsa.rs @@ -131,9 +131,9 @@ impl DsaPrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/ec.rs b/src/rust/src/backend/ec.rs index f63444ef0fab..500e0b6e7a22 100644 --- a/src/rust/src/backend/ec.rs +++ b/src/rust/src/backend/ec.rs @@ -355,9 +355,9 @@ impl ECPrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/ed25519.rs b/src/rust/src/backend/ed25519.rs index 4ddb8d14abe7..55db28c30c55 100644 --- a/src/rust/src/backend/ed25519.rs +++ b/src/rust/src/backend/ed25519.rs @@ -99,9 +99,9 @@ impl Ed25519PrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/ed448.rs b/src/rust/src/backend/ed448.rs index 0e6698af0f1e..a8678a6aa01e 100644 --- a/src/rust/src/backend/ed448.rs +++ b/src/rust/src/backend/ed448.rs @@ -97,9 +97,9 @@ impl Ed448PrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/rsa.rs b/src/rust/src/backend/rsa.rs index 45dd5c309c4f..f1d9217d9f62 100644 --- a/src/rust/src/backend/rsa.rs +++ b/src/rust/src/backend/rsa.rs @@ -147,10 +147,10 @@ fn setup_encryption_ctx( if let Some(label) = padding .getattr(pyo3::intern!(py, "_label"))? - .extract::>()? + .extract::>()? { if !label.is_empty() { - ctx.set_rsa_oaep_label(label)?; + ctx.set_rsa_oaep_label(&label)?; } } } @@ -411,9 +411,9 @@ impl RsaPrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/utils.rs b/src/rust/src/backend/utils.rs index 63ee13bca525..7c01e0be3772 100644 --- a/src/rust/src/backend/utils.rs +++ b/src/rust/src/backend/utils.rs @@ -46,27 +46,27 @@ pub(crate) fn pkey_private_bytes<'p>( py: pyo3::Python<'p>, key_obj: &pyo3::Bound<'p, pyo3::PyAny>, pkey: &openssl::pkey::PKey, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, openssh_allowed: bool, raw_allowed: bool, ) -> CryptographyResult> { - if !encoding.is_instance(types::ENCODING.get(py)?)? { + if !encoding.is_instance(&types::ENCODING.get_bound(py)?)? { return Err(CryptographyError::from( pyo3::exceptions::PyTypeError::new_err( "encoding must be an item from the Encoding enum", ), )); } - if !format.is_instance(types::PRIVATE_FORMAT.get(py)?)? { + if !format.is_instance(&types::PRIVATE_FORMAT.get_bound(py)?)? { return Err(CryptographyError::from( pyo3::exceptions::PyTypeError::new_err( "format must be an item from the PrivateFormat enum", ), )); } - if !encryption_algorithm.is_instance(types::KEY_SERIALIZATION_ENCRYPTION.get(py)?)? { + if !encryption_algorithm.is_instance(&types::KEY_SERIALIZATION_ENCRYPTION.get_bound(py)?)? { return Err(CryptographyError::from( pyo3::exceptions::PyTypeError::new_err( "Encryption algorithm must be a KeySerializationEncryption instance", @@ -80,7 +80,7 @@ pub(crate) fn pkey_private_bytes<'p>( { if !encoding.is(types::ENCODING_RAW.get(py)?) || !format.is(types::PRIVATE_FORMAT_RAW.get(py)?) - || !encryption_algorithm.is_instance(types::NO_ENCRYPTION.get(py)?)? + || !encryption_algorithm.is_instance(&types::NO_ENCRYPTION.get_bound(py)?)? { return Err(CryptographyError::from(pyo3::exceptions::PyValueError::new_err( "When using Raw both encoding and format must be Raw and encryption_algorithm must be NoEncryption()" @@ -90,10 +90,10 @@ pub(crate) fn pkey_private_bytes<'p>( return Ok(pyo3::types::PyBytes::new_bound(py, &raw_bytes)); } - let password = if encryption_algorithm.is_instance(types::NO_ENCRYPTION.get(py)?)? { + let password = if encryption_algorithm.is_instance(&types::NO_ENCRYPTION.get_bound(py)?)? { b"" - } else if encryption_algorithm.is_instance(types::BEST_AVAILABLE_ENCRYPTION.get(py)?)? - || (encryption_algorithm.is_instance(types::ENCRYPTION_BUILDER.get(py)?)? + } else if encryption_algorithm.is_instance(&types::BEST_AVAILABLE_ENCRYPTION.get_bound(py)?)? + || (encryption_algorithm.is_instance(&types::ENCRYPTION_BUILDER.get_bound(py)?)? && encryption_algorithm .getattr(pyo3::intern!(py, "_format"))? .is(format)) @@ -144,7 +144,7 @@ pub(crate) fn pkey_private_bytes<'p>( if format.is(types::PRIVATE_FORMAT_TRADITIONAL_OPENSSL.get(py)?) { if let Ok(rsa) = pkey.rsa() { - if encoding.is(types::ENCODING_PEM.get(py)?) { + if encoding.is(&types::ENCODING_PEM.get_bound(py)?) { let pem_bytes = if password.is_empty() { rsa.private_key_to_pem()? } else { @@ -154,7 +154,7 @@ pub(crate) fn pkey_private_bytes<'p>( )? }; return Ok(pyo3::types::PyBytes::new_bound(py, &pem_bytes)); - } else if encoding.is(types::ENCODING_DER.get(py)?) { + } else if encoding.is(&types::ENCODING_DER.get_bound(py)?) { if !password.is_empty() { return Err(CryptographyError::from( pyo3::exceptions::PyValueError::new_err( @@ -167,7 +167,7 @@ pub(crate) fn pkey_private_bytes<'p>( return Ok(pyo3::types::PyBytes::new_bound(py, &der_bytes)); } } else if let Ok(dsa) = pkey.dsa() { - if encoding.is(types::ENCODING_PEM.get(py)?) { + if encoding.is(&types::ENCODING_PEM.get_bound(py)?) { let pem_bytes = if password.is_empty() { dsa.private_key_to_pem()? } else { @@ -177,7 +177,7 @@ pub(crate) fn pkey_private_bytes<'p>( )? }; return Ok(pyo3::types::PyBytes::new_bound(py, &pem_bytes)); - } else if encoding.is(types::ENCODING_DER.get(py)?) { + } else if encoding.is(&types::ENCODING_DER.get_bound(py)?) { if !password.is_empty() { return Err(CryptographyError::from( pyo3::exceptions::PyValueError::new_err( diff --git a/src/rust/src/backend/x25519.rs b/src/rust/src/backend/x25519.rs index 89d8a53e500e..45d397e751f0 100644 --- a/src/rust/src/backend/x25519.rs +++ b/src/rust/src/backend/x25519.rs @@ -100,9 +100,9 @@ impl X25519PrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py, diff --git a/src/rust/src/backend/x448.rs b/src/rust/src/backend/x448.rs index 49dbfbd65e06..bd2833df48dc 100644 --- a/src/rust/src/backend/x448.rs +++ b/src/rust/src/backend/x448.rs @@ -99,9 +99,9 @@ impl X448PrivateKey { fn private_bytes<'p>( slf: &pyo3::Bound<'p, Self>, py: pyo3::Python<'p>, - encoding: &pyo3::PyAny, - format: &pyo3::PyAny, - encryption_algorithm: &pyo3::PyAny, + encoding: &pyo3::Bound<'p, pyo3::PyAny>, + format: &pyo3::Bound<'p, pyo3::PyAny>, + encryption_algorithm: &pyo3::Bound<'p, pyo3::PyAny>, ) -> CryptographyResult> { utils::pkey_private_bytes( py,