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 more utils.rs APIs to new pyo3 APIs #10708

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
4 changes: 2 additions & 2 deletions src/rust/src/backend/dh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ impl DHPublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
if !format.is(types::PUBLIC_FORMAT_SUBJECT_PUBLIC_KEY_INFO.get(py)?) {
return Err(CryptographyError::from(
Expand Down
11 changes: 7 additions & 4 deletions src/rust/src/backend/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::buf::CffiBuf;
use crate::error::{CryptographyError, CryptographyResult};
use crate::exceptions;
use pyo3::prelude::PyAnyMethods;
use pyo3::PyNativeType;

#[pyo3::prelude::pyclass(
frozen,
Expand Down Expand Up @@ -71,7 +72,8 @@ impl DsaPrivateKey {
data: CffiBuf<'_>,
algorithm: &pyo3::PyAny,
) -> CryptographyResult<&'p pyo3::types::PyBytes> {
let (data, _) = utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
let (data, _) =
utils::calculate_digest_and_algorithm(py, data.as_bytes(), &algorithm.as_borrowed())?;

let mut signer = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
signer.sign_init()?;
Expand Down Expand Up @@ -157,7 +159,8 @@ impl DsaPublicKey {
data: CffiBuf<'_>,
algorithm: &pyo3::PyAny,
) -> CryptographyResult<()> {
let (data, _) = utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;
let (data, _) =
utils::calculate_digest_and_algorithm(py, data.as_bytes(), &algorithm.as_borrowed())?;

let mut verifier = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
verifier.verify_init()?;
Expand Down Expand Up @@ -204,8 +207,8 @@ impl DsaPublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}
Expand Down
20 changes: 11 additions & 9 deletions src/rust/src/backend/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use pyo3::prelude::PyAnyMethods;
use pyo3::ToPyObject;
use pyo3::{PyNativeType, ToPyObject};

use crate::backend::utils;
use crate::buf::CffiBuf;
Expand Down Expand Up @@ -274,11 +274,11 @@ impl ECPrivateKey {
)),
));
}
let (data, algo) = utils::calculate_digest_and_algorithm(
py,
data.as_bytes(),
signature_algorithm.getattr(pyo3::intern!(py, "algorithm"))?,
)?;
let bound_algorithm = signature_algorithm
.getattr(pyo3::intern!(py, "algorithm"))?
.as_borrowed();
let (data, algo) =
utils::calculate_digest_and_algorithm(py, data.as_bytes(), &bound_algorithm)?;

let mut signer = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
signer.sign_init()?;
Expand Down Expand Up @@ -398,7 +398,9 @@ impl ECPublicKey {
let (data, _) = utils::calculate_digest_and_algorithm(
py,
data.as_bytes(),
signature_algorithm.getattr(pyo3::intern!(py, "algorithm"))?,
&signature_algorithm
.as_borrowed()
.getattr(pyo3::intern!(py, "algorithm"))?,
)?;

let mut verifier = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
Expand Down Expand Up @@ -437,8 +439,8 @@ impl ECPublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/backend/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ impl Ed25519PublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, true)
}
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/backend/ed448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl Ed448PublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, true)
}
Expand Down
18 changes: 6 additions & 12 deletions src/rust/src/backend/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,8 @@ impl RsaPrivateKey {
padding: &pyo3::Bound<'p, pyo3::PyAny>,
algorithm: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyAny>> {
let (data, algorithm) = utils::calculate_digest_and_algorithm(
py,
data.as_bytes(),
algorithm.clone().into_gil_ref(),
)?;
let (data, algorithm) =
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;

let mut ctx = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
ctx.sign_init().map_err(|_| {
Expand Down Expand Up @@ -438,11 +435,8 @@ impl RsaPublicKey {
padding: &pyo3::Bound<'_, pyo3::PyAny>,
algorithm: &pyo3::Bound<'_, pyo3::PyAny>,
) -> CryptographyResult<()> {
let (data, algorithm) = utils::calculate_digest_and_algorithm(
py,
data.as_bytes(),
algorithm.clone().into_gil_ref(),
)?;
let (data, algorithm) =
utils::calculate_digest_and_algorithm(py, data.as_bytes(), algorithm)?;

let mut ctx = openssl::pkey_ctx::PkeyCtx::new(&self.pkey)?;
ctx.verify_init()?;
Expand Down Expand Up @@ -534,8 +528,8 @@ impl RsaPublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, true, false)
}
Expand Down
21 changes: 11 additions & 10 deletions src/rust/src/backend/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::backend::hashes::Hash;
use crate::error::{CryptographyError, CryptographyResult};
use crate::{error, types};
use pyo3::prelude::PyAnyMethods;
use pyo3::{PyNativeType, ToPyObject};
use pyo3::ToPyObject;

pub(crate) fn py_int_to_bn(
py: pyo3::Python<'_>,
Expand Down Expand Up @@ -240,19 +240,19 @@ pub(crate) fn pkey_public_bytes<'p>(
py: pyo3::Python<'p>,
key_obj: &pyo3::Bound<'p, pyo3::PyAny>,
pkey: &openssl::pkey::PKey<openssl::pkey::Public>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &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::PUBLIC_FORMAT.get(py)?)? {
if !format.is_instance(&types::PUBLIC_FORMAT.get_bound(py)?)? {
return Err(CryptographyError::from(
pyo3::exceptions::PyTypeError::new_err(
"format must be an item from the PublicFormat enum",
Expand Down Expand Up @@ -355,10 +355,11 @@ pub(crate) fn pkey_public_bytes<'p>(
pub(crate) fn calculate_digest_and_algorithm<'p>(
py: pyo3::Python<'p>,
mut data: &'p [u8],
mut algorithm: &'p pyo3::PyAny,
) -> CryptographyResult<(&'p [u8], &'p pyo3::PyAny)> {
if algorithm.is_instance(types::PREHASHED.get(py)?)? {
algorithm = algorithm.getattr("_algorithm")?;
algorithm: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<(&'p [u8], pyo3::Bound<'p, pyo3::PyAny>)> {
let mut algorithm_result = algorithm.clone();
if algorithm.is_instance(&types::PREHASHED.get_bound(py)?)? {
algorithm_result = algorithm.getattr("_algorithm")?;
} else {
// Potential optimization: rather than allocate a PyBytes in
// `h.finalize()`, have a way to get the `DigestBytes` directly.
Expand All @@ -375,7 +376,7 @@ pub(crate) fn calculate_digest_and_algorithm<'p>(
));
}

Ok((data, algorithm))
Ok((data, algorithm_result))
}

pub(crate) enum PasswordCallbackStatus {
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/backend/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl X25519PublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, false, true)
}
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/backend/x448.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl X448PublicKey {
fn public_bytes<'p>(
slf: &pyo3::Bound<'p, Self>,
py: pyo3::Python<'p>,
encoding: &pyo3::PyAny,
format: &pyo3::PyAny,
encoding: &pyo3::Bound<'p, pyo3::PyAny>,
format: &pyo3::Bound<'p, pyo3::PyAny>,
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
utils::pkey_public_bytes(py, slf, &slf.borrow().pkey, encoding, format, false, true)
}
Expand Down