From fff485e70dffc4bcf2546c95a91c3556b80c9793 Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Mon, 18 Oct 2021 09:38:33 +0200 Subject: [PATCH 1/2] Make Name wrap the raw type directly Signed-off-by: Wiktor Kwapisiewicz --- .../enhanced_authorization_ea_commands.rs | 3 +- .../context/tpm_commands/object_commands.rs | 2 +- tss-esapi/src/structures/creation.rs | 4 +- tss-esapi/src/structures/names/name.rs | 43 ++++++++++--------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs b/tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs index eea2936a..91c71fde 100644 --- a/tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs +++ b/tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs @@ -352,7 +352,6 @@ impl Context { key_sign: &Name, check_ticket: VerifiedTicket, ) -> Result<()> { - let tss_key_sign = TPM2B_NAME::try_from(key_sign.clone())?; let check_ticket = TPMT_TK_VERIFIED::try_from(check_ticket)?; let ret = unsafe { Esys_PolicyAuthorize( @@ -363,7 +362,7 @@ impl Context { self.optional_session_3(), &approved_policy.clone().into(), &policy_ref.clone().into(), - &tss_key_sign, + key_sign.as_ref(), &check_ticket, ) }; diff --git a/tss-esapi/src/context/tpm_commands/object_commands.rs b/tss-esapi/src/context/tpm_commands/object_commands.rs index 85940474..ca83990f 100644 --- a/tss-esapi/src/context/tpm_commands/object_commands.rs +++ b/tss-esapi/src/context/tpm_commands/object_commands.rs @@ -298,7 +298,7 @@ impl Context { self.optional_session_2(), self.optional_session_3(), &credential.into(), - &object_name.try_into()?, + object_name.as_ref(), &mut out_credential_blob, &mut out_secret, ) diff --git a/tss-esapi/src/structures/creation.rs b/tss-esapi/src/structures/creation.rs index 63773aa2..981cf1f2 100644 --- a/tss-esapi/src/structures/creation.rs +++ b/tss-esapi/src/structures/creation.rs @@ -57,8 +57,8 @@ impl TryFrom for TPMS_CREATION_DATA { None => AlgorithmIdentifier::Null.into(), Some(alg) => alg.into(), }, - parentName: creation_data.parent_name.try_into()?, - parentQualifiedName: creation_data.parent_qualified_name.try_into()?, + parentName: *creation_data.parent_name.as_ref(), + parentQualifiedName: *creation_data.parent_qualified_name.as_ref(), outsideInfo: creation_data.outside_info.into(), }) } diff --git a/tss-esapi/src/structures/names/name.rs b/tss-esapi/src/structures/names/name.rs index f7c57e91..5fcc18bf 100644 --- a/tss-esapi/src/structures/names/name.rs +++ b/tss-esapi/src/structures/names/name.rs @@ -5,18 +5,27 @@ use crate::{Error, Result, WrapperErrorKind}; use log::error; use std::convert::TryFrom; /// Structure holding the data representing names -#[derive(Debug, Clone, PartialEq, Eq)] +#[allow(missing_copy_implementations)] +#[derive(Debug, Clone)] pub struct Name { - value: Vec, + value: TPM2B_NAME, } impl Name { const MAX_SIZE: usize = 68; pub fn value(&self) -> &[u8] { - &self.value + &self.value.name[..self.value.size as usize] + } +} + +impl PartialEq for Name { + fn eq(&self, other: &Self) -> bool { + self.value() == other.value() } } +impl Eq for Name {} + impl TryFrom> for Name { type Error = Error; fn try_from(bytes: Vec) -> Result { @@ -24,7 +33,12 @@ impl TryFrom> for Name { error!("Error: Invalid Vec size(> {})", Name::MAX_SIZE); return Err(Error::local_error(WrapperErrorKind::WrongParamSize)); } - Ok(Name { value: bytes }) + let size = bytes.len() as u16; + let mut name = [0; Name::MAX_SIZE]; + name.copy_from_slice(&bytes); + Ok(Name { + value: TPM2B_NAME { size, name }, + }) } } @@ -36,25 +50,12 @@ impl TryFrom for Name { error!("Error: Invalid TPM2B_NAME size(> {})", Name::MAX_SIZE); return Err(Error::local_error(WrapperErrorKind::InvalidParam)); } - Ok(Name { - value: tss_name.name[..size].to_vec(), - }) + Ok(Name { value: tss_name }) } } -impl TryFrom for TPM2B_NAME { - type Error = Error; - fn try_from(name: Name) -> Result { - let size = name.value.len(); - if size > Name::MAX_SIZE { - error!("Error: Invalid TPM2B_NAME size(> {})", Name::MAX_SIZE); - return Err(Error::local_error(WrapperErrorKind::WrongParamSize)); - } - let mut tss_name = TPM2B_NAME { - size: size as u16, - ..Default::default() - }; - tss_name.name[..size].copy_from_slice(name.value()); - Ok(tss_name) +impl AsRef for Name { + fn as_ref(&self) -> &TPM2B_NAME { + &self.value } } From 9d2678a0cca7c9d7347a0ae6efdca25942be61ca Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Wed, 20 Oct 2021 11:00:17 +0200 Subject: [PATCH 2/2] Add infallible conversion for Name and CreationData Signed-off-by: Wiktor Kwapisiewicz --- tss-esapi/src/structures/creation.rs | 13 ++++++------- tss-esapi/src/structures/names/name.rs | 6 ++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tss-esapi/src/structures/creation.rs b/tss-esapi/src/structures/creation.rs index 981cf1f2..8aa1f637 100644 --- a/tss-esapi/src/structures/creation.rs +++ b/tss-esapi/src/structures/creation.rs @@ -46,10 +46,9 @@ impl TryFrom for CreationData { } } -impl TryFrom for TPMS_CREATION_DATA { - type Error = Error; - fn try_from(creation_data: CreationData) -> Result { - Ok(TPMS_CREATION_DATA { +impl From for TPMS_CREATION_DATA { + fn from(creation_data: CreationData) -> Self { + TPMS_CREATION_DATA { pcrSelect: creation_data.pcr_select.into(), pcrDigest: creation_data.pcr_digest.into(), locality: creation_data.locality, @@ -57,9 +56,9 @@ impl TryFrom for TPMS_CREATION_DATA { None => AlgorithmIdentifier::Null.into(), Some(alg) => alg.into(), }, - parentName: *creation_data.parent_name.as_ref(), - parentQualifiedName: *creation_data.parent_qualified_name.as_ref(), + parentName: creation_data.parent_name.into(), + parentQualifiedName: creation_data.parent_qualified_name.into(), outsideInfo: creation_data.outside_info.into(), - }) + } } } diff --git a/tss-esapi/src/structures/names/name.rs b/tss-esapi/src/structures/names/name.rs index 5fcc18bf..54d05276 100644 --- a/tss-esapi/src/structures/names/name.rs +++ b/tss-esapi/src/structures/names/name.rs @@ -54,6 +54,12 @@ impl TryFrom for Name { } } +impl From for TPM2B_NAME { + fn from(name: Name) -> Self { + name.value + } +} + impl AsRef for Name { fn as_ref(&self) -> &TPM2B_NAME { &self.value