Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Name wrap the raw type directly #280

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
)
};
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
13 changes: 6 additions & 7 deletions tss-esapi/src/structures/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ impl TryFrom<TPM2B_CREATION_DATA> for CreationData {
}
}

impl TryFrom<CreationData> for TPMS_CREATION_DATA {
type Error = Error;
fn try_from(creation_data: CreationData) -> Result<Self> {
Ok(TPMS_CREATION_DATA {
impl From<CreationData> 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,
parentNameAlg: match creation_data.parent_name_alg {
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.into(),
parentQualifiedName: creation_data.parent_qualified_name.into(),
outsideInfo: creation_data.outside_info.into(),
})
}
}
}
49 changes: 28 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,18 @@ 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 From<Name> for TPM2B_NAME {
fn from(name: Name) -> Self {
name.value
}
}

impl AsRef<TPM2B_NAME> for Name {
fn as_ref(&self) -> &TPM2B_NAME {
&self.value
}
}