From 46a3d9dd1163f41b9390806409d016f9c0a01d26 Mon Sep 17 00:00:00 2001 From: Simon Brand Date: Tue, 17 Oct 2023 23:02:11 +0000 Subject: [PATCH] fixup! Add marshalling to Private (TPM2B_PRIVATE) --- tss-esapi/src/structures/buffers/private.rs | 35 +++++++++---------- .../structures_tests/buffers_tests/private.rs | 4 ++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/tss-esapi/src/structures/buffers/private.rs b/tss-esapi/src/structures/buffers/private.rs index 69d810e7..62ee5054 100644 --- a/tss-esapi/src/structures/buffers/private.rs +++ b/tss-esapi/src/structures/buffers/private.rs @@ -1,3 +1,6 @@ +// Copyright 2023 Contributors to the Parsec project. +// SPDX-License-Identifier: Apache-2.0 + use crate::{ traits::{Marshall, UnMarshall}, ReturnCode, @@ -11,10 +14,11 @@ impl Marshall for Private { const BUFFER_SIZE: usize = std::mem::size_of::(); /// Produce a marshalled [`TPM2B_PRIVATE`] - fn marshall(&self) -> Result> { - 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( @@ -22,12 +26,12 @@ impl Marshall for Private { 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| { @@ -35,21 +39,17 @@ impl Marshall for Private { }, )?; - 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 { + fn unmarshall_offset( + marshalled_data: &[u8], + offset: &mut std::os::raw::c_ulong, + ) -> Result { let mut dest = TPM2B_PRIVATE::default(); - let mut offset = 0; - ReturnCode::ensure_success( unsafe { crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Unmarshal( @@ -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) } } diff --git a/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs index 4293dcea..5f88f27d 100644 --- a/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs +++ b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs @@ -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; @@ -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); }