Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert private_bytes methods to new pyo3 APIs #10707

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rust/src/backend/dh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
if !format.is(types::PRIVATE_FORMAT_PKCS8.get(py)?) {
return Err(CryptographyError::from(
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/ed448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
10 changes: 5 additions & 5 deletions src/rust/src/backend/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ fn setup_encryption_ctx(

if let Some(label) = padding
.getattr(pyo3::intern!(py, "_label"))?
.extract::<Option<&[u8]>>()?
.extract::<Option<pyo3::pybacked::PyBackedBytes>>()?
{
if !label.is_empty() {
ctx.set_rsa_oaep_label(label)?;
ctx.set_rsa_oaep_label(&label)?;
}
}
}
Expand Down Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
28 changes: 14 additions & 14 deletions src/rust/src/backend/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<openssl::pkey::Private>,
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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
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",
Expand All @@ -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()"
Expand All @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down
6 changes: 3 additions & 3 deletions src/rust/src/backend/x448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_private_bytes(
py,
Expand Down