Skip to content

Commit

Permalink
WIP: Make Name wrap the raw type directly
Browse files Browse the repository at this point in the history
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
  • Loading branch information
wiktor-k committed Oct 18, 2021
1 parent c45d4d4 commit 98081bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
)
};
Expand Down
2 changes: 1 addition & 1 deletion tss-esapi/src/context/tpm_commands/object_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions tss-esapi/src/structures/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl TryFrom<CreationData> 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(),
})
}
Expand Down
43 changes: 22 additions & 21 deletions tss-esapi/src/structures/names/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ 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<u8>,
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<Vec<u8>> for Name {
type Error = Error;
fn try_from(bytes: Vec<u8>) -> Result<Self> {
if bytes.len() > Name::MAX_SIZE {
error!("Error: Invalid Vec<u8> 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 },
})
}
}

Expand All @@ -36,25 +50,12 @@ impl TryFrom<TPM2B_NAME> 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<Name> for TPM2B_NAME {
type Error = Error;
fn try_from(name: Name) -> Result<TPM2B_NAME> {
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<TPM2B_NAME> for Name {
fn as_ref(&self) -> &TPM2B_NAME {
&self.value
}
}

0 comments on commit 98081bf

Please sign in to comment.