Skip to content

Commit

Permalink
fixup! Add marshalling to Private (TPM2B_PRIVATE)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandsimon committed Oct 17, 2023
1 parent d9ba0d9 commit 46a3d9d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
35 changes: 17 additions & 18 deletions tss-esapi/src/structures/buffers/private.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use crate::{
traits::{Marshall, UnMarshall},
ReturnCode,
Expand All @@ -11,45 +14,42 @@ impl Marshall for Private {
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2B_PRIVATE>();

/// Produce a marshalled [`TPM2B_PRIVATE`]
fn marshall(&self) -> Result<Vec<u8>> {
let mut buffer = vec![0; Self::BUFFER_SIZE];
let mut offset = 0;

fn marshall_offset(
&self,
marshalled_data: &mut [u8],
offset: &mut std::os::raw::c_ulong,
) -> Result<()> {
ReturnCode::ensure_success(
unsafe {
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Marshal(
&self.clone().try_into().map_err(|e| {
error!("Failed to convert Private to TPM2B_PRIVATE: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
buffer.as_mut_ptr(),
Self::BUFFER_SIZE.try_into().map_err(|e| {
marshalled_data.as_mut_ptr(),
marshalled_data.len().try_into().map_err(|e| {
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
&mut offset,
offset,
)
},
|ret| {
error!("Failed to marshal Private: {}", ret);
},
)?;

let checked_offset = usize::try_from(offset).map_err(|e| {
error!("Failed to parse offset as usize: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?;
buffer.truncate(checked_offset);
Ok(buffer)
Ok(())
}
}

impl UnMarshall for Private {
/// Unmarshall the structure from [`TPM2B_PRIVATE`]
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
) -> Result<Self> {
let mut dest = TPM2B_PRIVATE::default();
let mut offset = 0;

ReturnCode::ensure_success(
unsafe {
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Unmarshal(
Expand All @@ -58,13 +58,12 @@ impl UnMarshall for Private {
error!("Failed to convert length of marshalled data: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
&mut offset,
offset,
&mut dest,
)
},
|ret| error!("Failed to unmarshal Private: {}", ret),
)?;

Private::try_from(dest)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Contributors to the Parsec project.
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use std::convert::TryFrom;
Expand All @@ -7,6 +7,8 @@ use tss_esapi::structures::Private;
#[test]
fn marshall_unmarshall() {
crate::common::check_marshall_unmarshall(&Private::default());
crate::common::check_marshall_unmarshall_offset(&Private::default());
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
crate::common::check_marshall_unmarshall(&private);
crate::common::check_marshall_unmarshall_offset(&private);
}

0 comments on commit 46a3d9d

Please sign in to comment.