Convert more utils.rs APIs to new pyo3 APIs (#10708)

This commit is contained in:
Facundo Tuesca 2024-04-04 19:39:02 +02:00 committed by GitHub
parent 52bed48a92
commit c0b80d6d0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 45 additions and 45 deletions

View file

@ -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(

View file

@ -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,
@ -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()?;
@ -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()?;
@ -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)
}

View file

@ -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;
@ -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()?;
@ -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)?;
@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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(|_| {
@ -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()?;
@ -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)
}

View file

@ -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<'_>,
@ -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",
@ -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.
@ -375,7 +376,7 @@ pub(crate) fn calculate_digest_and_algorithm<'p>(
));
}
Ok((data, algorithm))
Ok((data, algorithm_result))
}
pub(crate) enum PasswordCallbackStatus {

View file

@ -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)
}

View file

@ -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)
}