Skip to content

Commit 4652d21

Browse files
committed
WIP: Make Name wrap the raw type directly
1 parent c45d4d4 commit 4652d21

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

tss-esapi/src/context/tpm_commands/enhanced_authorization_ea_commands.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl Context {
352352
key_sign: &Name,
353353
check_ticket: VerifiedTicket,
354354
) -> Result<()> {
355-
let tss_key_sign = TPM2B_NAME::try_from(key_sign.clone())?;
355+
//let tss_key_sign = TPM2B_NAME::try_from(key_sign.clone())?;
356356
let check_ticket = TPMT_TK_VERIFIED::try_from(check_ticket)?;
357357
let ret = unsafe {
358358
Esys_PolicyAuthorize(
@@ -363,7 +363,7 @@ impl Context {
363363
self.optional_session_3(),
364364
&approved_policy.clone().into(),
365365
&policy_ref.clone().into(),
366-
&tss_key_sign,
366+
key_sign.as_ref(),
367367
&check_ticket,
368368
)
369369
};

tss-esapi/src/context/tpm_commands/object_commands.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl Context {
298298
self.optional_session_2(),
299299
self.optional_session_3(),
300300
&credential.into(),
301-
&object_name.try_into()?,
301+
object_name.as_ref(),
302302
&mut out_credential_blob,
303303
&mut out_secret,
304304
)

tss-esapi/src/structures/creation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl TryFrom<CreationData> for TPMS_CREATION_DATA {
5757
None => AlgorithmIdentifier::Null.into(),
5858
Some(alg) => alg.into(),
5959
},
60-
parentName: creation_data.parent_name.try_into()?,
61-
parentQualifiedName: creation_data.parent_qualified_name.try_into()?,
60+
parentName: *creation_data.parent_name.as_ref(),
61+
parentQualifiedName: *creation_data.parent_qualified_name.as_ref(),
6262
outsideInfo: creation_data.outside_info.into(),
6363
})
6464
}

tss-esapi/src/structures/names/name.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,40 @@ use crate::{Error, Result, WrapperErrorKind};
55
use log::error;
66
use std::convert::TryFrom;
77
/// Structure holding the data representing names
8-
#[derive(Debug, Clone, PartialEq, Eq)]
8+
#[allow(missing_copy_implementations)]
9+
#[derive(Debug, Clone)]
910
pub struct Name {
10-
value: Vec<u8>,
11+
value: TPM2B_NAME,
1112
}
1213

1314
impl Name {
1415
const MAX_SIZE: usize = 68;
1516
pub fn value(&self) -> &[u8] {
16-
&self.value
17+
&self.value.name[..self.value.size as usize]
18+
}
19+
}
20+
21+
impl PartialEq for Name {
22+
fn eq(&self, other: &Self) -> bool {
23+
self.value() == other.value()
1724
}
1825
}
1926

27+
impl Eq for Name {}
28+
2029
impl TryFrom<Vec<u8>> for Name {
2130
type Error = Error;
2231
fn try_from(bytes: Vec<u8>) -> Result<Self> {
2332
if bytes.len() > Name::MAX_SIZE {
2433
error!("Error: Invalid Vec<u8> size(> {})", Name::MAX_SIZE);
2534
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
2635
}
27-
Ok(Name { value: bytes })
36+
let size = bytes.len() as u16;
37+
let mut name = [0; Name::MAX_SIZE];
38+
name.copy_from_slice(&bytes);
39+
Ok(Name {
40+
value: TPM2B_NAME { size, name },
41+
})
2842
}
2943
}
3044

@@ -36,25 +50,12 @@ impl TryFrom<TPM2B_NAME> for Name {
3650
error!("Error: Invalid TPM2B_NAME size(> {})", Name::MAX_SIZE);
3751
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
3852
}
39-
Ok(Name {
40-
value: tss_name.name[..size].to_vec(),
41-
})
53+
Ok(Name { value: tss_name })
4254
}
4355
}
4456

45-
impl TryFrom<Name> for TPM2B_NAME {
46-
type Error = Error;
47-
fn try_from(name: Name) -> Result<TPM2B_NAME> {
48-
let size = name.value.len();
49-
if size > Name::MAX_SIZE {
50-
error!("Error: Invalid TPM2B_NAME size(> {})", Name::MAX_SIZE);
51-
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
52-
}
53-
let mut tss_name = TPM2B_NAME {
54-
size: size as u16,
55-
..Default::default()
56-
};
57-
tss_name.name[..size].copy_from_slice(name.value());
58-
Ok(tss_name)
57+
impl AsRef<TPM2B_NAME> for Name {
58+
fn as_ref(&self) -> &TPM2B_NAME {
59+
&self.value
5960
}
6061
}

0 commit comments

Comments
 (0)