Skip to content

Commit

Permalink
update to prost 0.12
Browse files Browse the repository at this point in the history
derived Enumerations now provide their own conversion implementation
from i32, so map any DecodeErrors originating from there to
InvalidEncoding, and drop the manual impls that existed before.

Closes: parallaxsecond#153

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
  • Loading branch information
Fabian-Gruenbichler committed Jul 11, 2024
1 parent 9f50ebb commit efd5cdc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 106 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ edition = "2018"
rust-version = "1.66.0"

[build-dependencies]
prost-build = { version = "0.9.0", optional = true }
prost-build = { version = "0.12.0", optional = true }

[dependencies]
serde = { version = "1.0.115", features = ["derive"] }
bincode = "1.3.1"
num-traits = "0.2.12"
num-derive = "0.4.0"
num = "0.4.0"
prost = "0.9.0"
prost = "0.12.0"
arbitrary = { version = "0.4.6", features = ["derive"], optional = true }
uuid = "0.8.1"
log = "0.4.11"
Expand Down
4 changes: 3 additions & 1 deletion src/operations_protobuf/convert_can_do_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ impl TryFrom<CheckType> for CheckTypeProto {

// CheckType from protobuf to native
pub fn i32_to_check_type(check_type_val: i32) -> std::result::Result<CheckType, ResponseStatus> {
let check_type_proto: CheckTypeProto = check_type_val.try_into()?;
let check_type_proto: CheckTypeProto = check_type_val
.try_into()
.map_err(|_err: ::prost::DecodeError| ResponseStatus::InvalidEncoding)?;
check_type_proto.try_into()
}

Expand Down
121 changes: 18 additions & 103 deletions src/operations_protobuf/generated_ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,104 +36,15 @@ pub mod prepare_key_attestation;

use zeroize::Zeroize;

use crate::requests::{ResponseStatus, Result};
use log::error;
use psa_algorithm::algorithm::{aead::AeadWithDefaultLengthTag, key_agreement::Raw, Cipher, Hash};
use psa_key_attributes::key_type::{DhFamily, EccFamily};
use can_do_crypto::CheckType;
#[cfg(test)]
use crate::requests::ResponseStatus;
#[cfg(test)]
use psa_algorithm::algorithm::{Cipher, Hash};
#[cfg(test)]
use psa_key_attributes::key_type::EccFamily;
#[cfg(test)]
use std::convert::TryFrom;

impl TryFrom<i32> for Cipher {
type Error = ResponseStatus;
fn try_from(cipher_val: i32) -> Result<Self> {
Cipher::from_i32(cipher_val).ok_or_else(|| {
error!(
"Value {} not supported as a cipher algorithm encoding.",
cipher_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for Hash {
type Error = ResponseStatus;
fn try_from(hash_val: i32) -> Result<Self> {
Hash::from_i32(hash_val).ok_or_else(|| {
error!(
"Value {} not supported as a hash algorithm encoding.",
hash_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for AeadWithDefaultLengthTag {
type Error = ResponseStatus;
fn try_from(aead_val: i32) -> Result<Self> {
AeadWithDefaultLengthTag::from_i32(aead_val).ok_or_else(|| {
error!(
"Value {} not supported as an AEAD with default tag length algorithm encoding.",
aead_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for Raw {
type Error = ResponseStatus;
fn try_from(key_agreement_val: i32) -> Result<Self> {
Raw::from_i32(key_agreement_val).ok_or_else(|| {
error!(
"Value {} not supported as a raw key agreement algorithm encoding.",
key_agreement_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for EccFamily {
type Error = ResponseStatus;
fn try_from(ecc_family_val: i32) -> Result<Self> {
EccFamily::from_i32(ecc_family_val).ok_or_else(|| {
error!(
"Value {} not supported as an ECC family encoding.",
ecc_family_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for DhFamily {
type Error = ResponseStatus;
fn try_from(dh_family_val: i32) -> Result<Self> {
DhFamily::from_i32(dh_family_val).ok_or_else(|| {
error!(
"Value {} not supported as a DH family encoding.",
dh_family_val
);
ResponseStatus::InvalidEncoding
})
}
}

impl TryFrom<i32> for CheckType {
type Error = ResponseStatus;
fn try_from(check_type_val: i32) -> Result<Self> {
CheckType::from_i32(check_type_val).ok_or_else(|| {
error!(
"Value {} not supported as a check type.",
check_type_val
);
ResponseStatus::InvalidEncoding
})
}
}

pub(super) trait ClearProtoMessage {
fn clear_message(&mut self) {}
}
Expand Down Expand Up @@ -299,11 +210,15 @@ impl ClearProtoMessage for psa_cipher_decrypt::Operation {
}

impl ClearProtoMessage for psa_cipher_encrypt::Result {
fn clear_message(&mut self) { self.ciphertext.zeroize(); }
fn clear_message(&mut self) {
self.ciphertext.zeroize();
}
}

impl ClearProtoMessage for psa_cipher_decrypt::Result {
fn clear_message(&mut self) { self.plaintext.zeroize(); }
fn clear_message(&mut self) {
self.plaintext.zeroize();
}
}

impl ClearProtoMessage for psa_generate_random::Result {
Expand Down Expand Up @@ -402,23 +317,23 @@ impl ClearProtoMessage for prepare_key_attestation::Result {
#[test]
fn i32_conversions() {
assert_eq!(
Cipher::try_from(56).unwrap_err(),
<::prost::DecodeError as Into<ResponseStatus>>::into(Cipher::try_from(56).unwrap_err()),
ResponseStatus::InvalidEncoding
);
assert_eq!(
Cipher::try_from(-5).unwrap_err(),
<::prost::DecodeError as Into<ResponseStatus>>::into(Cipher::try_from(-5).unwrap_err()),
ResponseStatus::InvalidEncoding
);
assert_eq!(
Hash::try_from(89).unwrap_err(),
<::prost::DecodeError as Into<ResponseStatus>>::into(Hash::try_from(89).unwrap_err()),
ResponseStatus::InvalidEncoding
);
assert_eq!(
Hash::try_from(-4).unwrap_err(),
<::prost::DecodeError as Into<ResponseStatus>>::into(Hash::try_from(-4).unwrap_err()),
ResponseStatus::InvalidEncoding
);
assert_eq!(
EccFamily::try_from(78).unwrap_err(),
<::prost::DecodeError as Into<ResponseStatus>>::into(EccFamily::try_from(78).unwrap_err()),
ResponseStatus::InvalidEncoding
);
}
10 changes: 10 additions & 0 deletions src/requests/response_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ impl From<std::io::Error> for ResponseStatus {
}
}

impl From<::prost::DecodeError> for ResponseStatus {
fn from(err: ::prost::DecodeError) -> Self {
warn!(
"Conversion from {} to ResponseStatus::InvalidEncoding.",
err
);
ResponseStatus::InvalidEncoding
}
}

impl From<bincode::Error> for ResponseStatus {
fn from(err: bincode::Error) -> Self {
warn!(
Expand Down

0 comments on commit efd5cdc

Please sign in to comment.