Skip to content

Commit 4c9b7a7

Browse files
authored
Merge pull request #410 from ionut-arm/unmarshall-plus
Expanding UnMarshall + a few additions
2 parents 750f1d9 + 0e27599 commit 4c9b7a7

File tree

8 files changed

+427
-7
lines changed

8 files changed

+427
-7
lines changed

tss-esapi/src/constants/command_code.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
// SPDX-License-Identifier: Apache-2.0
33
mod structure;
44

5-
use crate::{tss2_esys::TPM2_CC, Error, Result, WrapperErrorKind};
5+
use crate::{
6+
traits::{Marshall, UnMarshall},
7+
tss2_esys::TPM2_CC,
8+
Error, Result, ReturnCode, WrapperErrorKind,
9+
};
610
use log::error;
711
use num_derive::{FromPrimitive, ToPrimitive};
812
use num_traits::{FromPrimitive, ToPrimitive};
9-
use std::convert::TryFrom;
13+
use std::convert::{TryFrom, TryInto};
1014
use structure::CommandCodeStructure;
1115

1216
/// Enum representing the command code constants.
@@ -150,3 +154,57 @@ impl From<CommandCode> for TPM2_CC {
150154
command_code.to_u32().unwrap()
151155
}
152156
}
157+
158+
impl Marshall for CommandCode {
159+
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2_CC>();
160+
161+
fn marshall_offset(
162+
&self,
163+
marshalled_data: &mut [u8],
164+
offset: &mut std::os::raw::c_ulong,
165+
) -> Result<()> {
166+
ReturnCode::ensure_success(
167+
unsafe {
168+
crate::tss2_esys::Tss2_MU_TPM2_CC_Marshal(
169+
(*self).into(),
170+
marshalled_data.as_mut_ptr(),
171+
marshalled_data.len().try_into().map_err(|e| {
172+
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
173+
Error::local_error(WrapperErrorKind::InvalidParam)
174+
})?,
175+
offset,
176+
)
177+
},
178+
|ret| {
179+
error!("Failed to marshal CommandCode: {}", ret);
180+
},
181+
)?;
182+
Ok(())
183+
}
184+
}
185+
186+
impl UnMarshall for CommandCode {
187+
fn unmarshall_offset(
188+
marshalled_data: &[u8],
189+
offset: &mut std::os::raw::c_ulong,
190+
) -> Result<Self> {
191+
let mut dest = TPM2_CC::default();
192+
193+
ReturnCode::ensure_success(
194+
unsafe {
195+
crate::tss2_esys::Tss2_MU_TPM2_CC_Unmarshal(
196+
marshalled_data.as_ptr(),
197+
marshalled_data.len().try_into().map_err(|e| {
198+
error!("Failed to convert length of marshalled data: {}", e);
199+
Error::local_error(WrapperErrorKind::InvalidParam)
200+
})?,
201+
offset,
202+
&mut dest,
203+
)
204+
},
205+
|ret| error!("Failed to unmarshal SensitiveCreate: {}", ret),
206+
)?;
207+
208+
CommandCode::try_from(dest)
209+
}
210+
}

tss-esapi/src/interface_types/structure_tags.rs

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// Copyright 2021 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::{constants::StructureTag, tss2_esys::TPMI_ST_ATTEST, Error, Result, WrapperErrorKind};
5-
use std::convert::TryFrom;
4+
use log::error;
5+
use tss_esapi_sys::TPMI_ST_COMMAND_TAG;
6+
7+
use crate::{
8+
constants::StructureTag,
9+
traits::{Marshall, UnMarshall},
10+
tss2_esys::TPMI_ST_ATTEST,
11+
Error, Result, ReturnCode, WrapperErrorKind,
12+
};
13+
use std::convert::{TryFrom, TryInto};
614

715
/// Type of attestation.
816
///
@@ -66,3 +74,158 @@ impl TryFrom<TPMI_ST_ATTEST> for AttestationType {
6674
AttestationType::try_from(StructureTag::try_from(tpmi_st_attest)?)
6775
}
6876
}
77+
78+
impl Marshall for AttestationType {
79+
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_ATTEST>();
80+
81+
fn marshall_offset(
82+
&self,
83+
marshalled_data: &mut [u8],
84+
offset: &mut std::os::raw::c_ulong,
85+
) -> Result<()> {
86+
ReturnCode::ensure_success(
87+
unsafe {
88+
crate::tss2_esys::Tss2_MU_TPM2_ST_Marshal(
89+
(*self).into(),
90+
marshalled_data.as_mut_ptr(),
91+
marshalled_data.len().try_into().map_err(|e| {
92+
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
93+
Error::local_error(WrapperErrorKind::InvalidParam)
94+
})?,
95+
offset,
96+
)
97+
},
98+
|ret| {
99+
error!("Failed to marshal AttestationType: {}", ret);
100+
},
101+
)?;
102+
103+
Ok(())
104+
}
105+
}
106+
107+
impl UnMarshall for AttestationType {
108+
fn unmarshall_offset(
109+
marshalled_data: &[u8],
110+
offset: &mut std::os::raw::c_ulong,
111+
) -> Result<Self> {
112+
let mut dest = TPMI_ST_ATTEST::default();
113+
114+
ReturnCode::ensure_success(
115+
unsafe {
116+
crate::tss2_esys::Tss2_MU_TPM2_ST_Unmarshal(
117+
marshalled_data.as_ptr(),
118+
marshalled_data.len().try_into().map_err(|e| {
119+
error!("Failed to convert length of marshalled data: {}", e);
120+
Error::local_error(WrapperErrorKind::InvalidParam)
121+
})?,
122+
offset,
123+
&mut dest,
124+
)
125+
},
126+
|ret| error!("Failed to unmarshal AttestationType: {}", ret),
127+
)?;
128+
129+
AttestationType::try_from(dest)
130+
}
131+
}
132+
133+
/// Type of command tag.
134+
///
135+
/// # Details
136+
/// Corresponds to `TPMI_ST_COMMAND_TAG`.
137+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138+
pub enum CommandTag {
139+
Sessions,
140+
NoSessions,
141+
}
142+
143+
impl From<CommandTag> for StructureTag {
144+
fn from(value: CommandTag) -> Self {
145+
match value {
146+
CommandTag::Sessions => StructureTag::Sessions,
147+
CommandTag::NoSessions => StructureTag::NoSessions,
148+
}
149+
}
150+
}
151+
152+
impl TryFrom<StructureTag> for CommandTag {
153+
type Error = Error;
154+
155+
fn try_from(value: StructureTag) -> std::result::Result<Self, Self::Error> {
156+
match value {
157+
StructureTag::Sessions => Ok(CommandTag::Sessions),
158+
StructureTag::NoSessions => Ok(CommandTag::NoSessions),
159+
_ => Err(Error::local_error(WrapperErrorKind::InvalidParam)),
160+
}
161+
}
162+
}
163+
164+
impl From<CommandTag> for TPMI_ST_COMMAND_TAG {
165+
fn from(command_tag: CommandTag) -> Self {
166+
StructureTag::from(command_tag).into()
167+
}
168+
}
169+
170+
impl TryFrom<TPMI_ST_COMMAND_TAG> for CommandTag {
171+
type Error = Error;
172+
173+
fn try_from(tpmi_st_command_tag: TPMI_ST_COMMAND_TAG) -> Result<Self> {
174+
CommandTag::try_from(StructureTag::try_from(tpmi_st_command_tag)?)
175+
}
176+
}
177+
178+
impl Marshall for CommandTag {
179+
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_COMMAND_TAG>();
180+
181+
fn marshall_offset(
182+
&self,
183+
marshalled_data: &mut [u8],
184+
offset: &mut std::os::raw::c_ulong,
185+
) -> Result<()> {
186+
ReturnCode::ensure_success(
187+
unsafe {
188+
crate::tss2_esys::Tss2_MU_TPM2_ST_Marshal(
189+
(*self).into(),
190+
marshalled_data.as_mut_ptr(),
191+
marshalled_data.len().try_into().map_err(|e| {
192+
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
193+
Error::local_error(WrapperErrorKind::InvalidParam)
194+
})?,
195+
offset,
196+
)
197+
},
198+
|ret| {
199+
error!("Failed to marshal CommandTag: {}", ret);
200+
},
201+
)?;
202+
203+
Ok(())
204+
}
205+
}
206+
207+
impl UnMarshall for CommandTag {
208+
fn unmarshall_offset(
209+
marshalled_data: &[u8],
210+
offset: &mut std::os::raw::c_ulong,
211+
) -> Result<Self> {
212+
let mut dest = TPMI_ST_COMMAND_TAG::default();
213+
214+
ReturnCode::ensure_success(
215+
unsafe {
216+
crate::tss2_esys::Tss2_MU_TPM2_ST_Unmarshal(
217+
marshalled_data.as_ptr(),
218+
marshalled_data.len().try_into().map_err(|e| {
219+
error!("Failed to convert length of marshalled data: {}", e);
220+
Error::local_error(WrapperErrorKind::InvalidParam)
221+
})?,
222+
offset,
223+
&mut dest,
224+
)
225+
},
226+
|ret| error!("Failed to unmarshal CommandTag: {}", ret),
227+
)?;
228+
229+
CommandTag::try_from(dest)
230+
}
231+
}

tss-esapi/src/traits.rs

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,115 @@
1+
use std::convert::{TryFrom, TryInto};
2+
3+
use log::error;
4+
use tss_esapi_sys::UINT32;
5+
16
// Copyright 2021 Contributors to the Parsec project.
27
// SPDX-License-Identifier: Apache-2.0
3-
use crate::Result;
8+
use crate::{Error, Result, ReturnCode, WrapperErrorKind};
49

510
/// Trait for types that can be converted into
611
/// TPM marshalled data.
712
pub trait Marshall: Sized {
813
const BUFFER_SIZE: usize;
914
/// Returns the type in the form of marshalled data
10-
fn marshall(&self) -> Result<Vec<u8>>;
15+
fn marshall(&self) -> Result<Vec<u8>> {
16+
let mut buffer = vec![0; Self::BUFFER_SIZE];
17+
let mut offset = 0;
18+
19+
self.marshall_offset(&mut buffer, &mut offset)?;
20+
21+
let checked_offset = usize::try_from(offset).map_err(|e| {
22+
error!("Failed to parse offset as usize: {}", e);
23+
Error::local_error(WrapperErrorKind::InvalidParam)
24+
})?;
25+
26+
buffer.truncate(checked_offset);
27+
28+
Ok(buffer)
29+
}
30+
31+
/// Writes the type in the form of marshalled data to `marshalled_data`,
32+
/// and modifies the `offset` to point to the first byte in the buffer
33+
/// which was not written in the conversion.
34+
fn marshall_offset(
35+
&self,
36+
_marshalled_data: &mut [u8],
37+
_offset: &mut std::os::raw::c_ulong,
38+
) -> Result<()> {
39+
unimplemented!();
40+
}
1141
}
1242

1343
/// Trait for types that can be created from
1444
/// TPM marshalled data.
1545
pub trait UnMarshall: Sized {
1646
/// Creates the type from marshalled data.
17-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self>;
47+
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
48+
Self::unmarshall_offset(marshalled_data, &mut 0)
49+
}
50+
51+
/// Creates the type from the marshalled data, and modifies
52+
/// the `offset` to point to the first byte in the `marshalled_data`
53+
/// buffer which was not used in the conversion.
54+
fn unmarshall_offset(
55+
_marshalled_data: &[u8],
56+
_offset: &mut std::os::raw::c_ulong,
57+
) -> Result<Self> {
58+
unimplemented!();
59+
}
60+
}
61+
62+
impl Marshall for u32 {
63+
const BUFFER_SIZE: usize = std::mem::size_of::<UINT32>();
64+
65+
fn marshall_offset(
66+
&self,
67+
marshalled_data: &mut [u8],
68+
offset: &mut std::os::raw::c_ulong,
69+
) -> Result<()> {
70+
ReturnCode::ensure_success(
71+
unsafe {
72+
crate::tss2_esys::Tss2_MU_UINT32_Marshal(
73+
*self,
74+
marshalled_data.as_mut_ptr(),
75+
marshalled_data.len().try_into().map_err(|e| {
76+
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
77+
Error::local_error(WrapperErrorKind::InvalidParam)
78+
})?,
79+
offset,
80+
)
81+
},
82+
|ret| {
83+
error!("Failed to marshall u32: {}", ret);
84+
},
85+
)?;
86+
87+
Ok(())
88+
}
89+
}
90+
91+
impl UnMarshall for u32 {
92+
fn unmarshall_offset(
93+
marshalled_data: &[u8],
94+
offset: &mut std::os::raw::c_ulong,
95+
) -> Result<Self> {
96+
let mut dest = 0_u32;
97+
98+
ReturnCode::ensure_success(
99+
unsafe {
100+
crate::tss2_esys::Tss2_MU_UINT32_Unmarshal(
101+
marshalled_data.as_ptr(),
102+
marshalled_data.len().try_into().map_err(|e| {
103+
error!("Failed to convert length of marshalled data: {}", e);
104+
Error::local_error(WrapperErrorKind::InvalidParam)
105+
})?,
106+
offset,
107+
&mut dest,
108+
)
109+
},
110+
|ret| error!("Failed to unmarshal SensitiveCreate: {}", ret),
111+
)?;
112+
113+
Ok(dest)
114+
}
18115
}

0 commit comments

Comments
 (0)