From 98081bfb2de41c018ac973fe3bcf7df22da25e5c Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Mon, 18 Oct 2021 09:38:33 +0200 Subject: [PATCH] WIP: Make Name wrap the raw type directly Signed-off-by: Wiktor Kwapisiewicz --- .../enhanced_authorization_ea_commands.rs | 4 +- .../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, 27 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 eea2936a8..f231bcef0 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,7 @@ impl Context { key_sign: &Name, check_ticket: VerifiedTicket, ) -> Result<()> { - let tss_key_sign = TPM2B_NAME::try_from(key_sign.clone())?; + //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 +363,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 85940474b..ca83990fc 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 63773aa2e..981cf1f22 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 f7c57e91b..5fcc18bfc 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 } }