Skip to content

Commit

Permalink
Add serde to Public and Private
Browse files Browse the repository at this point in the history
This allows serialisation and deserialisation of the Public and Private
structures, which is required for storing these in many database or
serialised forms.

Signed-off-by: William Brown <[email protected]>
  • Loading branch information
Firstyear committed Jan 20, 2024
1 parent 63298c2 commit 2f3cd59
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[codespell]
skip = .git,target,Cargo.lock
ignore-words-list = acsend,crate,keypair,daa
ignore-words-list = acsend,crate,keypair,daa,de,ser
1 change: 1 addition & 0 deletions tss-esapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ paste = "1.0.14"
[dev-dependencies]
env_logger = "0.9.0"
sha2 = "0.10.1"
serde_json = "^1.0.108"

[build-dependencies]
semver = "1.0.7"
Expand Down
26 changes: 26 additions & 0 deletions tss-esapi/src/structures/buffers/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,34 @@
// SPDX-License-Identifier: Apache-2.0

use crate::traits::impl_mu_standard;
use crate::traits::{Marshall, UnMarshall};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tss_esapi_sys::_PRIVATE;

buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);

impl_mu_standard!(Private, TPM2B_PRIVATE);

impl Serialize for Private {
/// Serialise the [Private] data into it's bytes representation of the TCG
/// TPM2B_PRIVATE structure.
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for Private {
/// Deserialise the [Private] data from it's bytes representation of the TCG
/// TPM2B_PRIVATE structure.
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
}
}
27 changes: 26 additions & 1 deletion tss-esapi/src/structures/tagged/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
attributes::ObjectAttributes,
interface_types::algorithm::{HashingAlgorithm, PublicAlgorithm},
structures::{Digest, EccPoint, PublicKeyRsa, SymmetricCipherParameters},
traits::{impl_mu_standard, Marshall},
traits::{impl_mu_standard, Marshall, UnMarshall},
tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC},
Error, Result, ReturnCode, WrapperErrorKind,
};
Expand All @@ -18,6 +18,7 @@ use keyed_hash::PublicKeyedHashParameters;
use rsa::PublicRsaParameters;

use log::error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::convert::{TryFrom, TryInto};
use tss_esapi_sys::{TPMU_PUBLIC_ID, TPMU_PUBLIC_PARMS};

Expand Down Expand Up @@ -493,6 +494,30 @@ impl TryFrom<TPMT_PUBLIC> for Public {

impl_mu_standard!(Public, TPMT_PUBLIC);

impl Serialize for Public {
/// Serialise the [Public] data into it's bytes representation of the TCG
/// TPMT_PUBLIC structure.
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for Public {
/// Deserialise the [Public] data from it's bytes representation of the TCG
/// TPMT_PUBLIC structure.
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
}
}

impl TryFrom<TPM2B_PUBLIC> for Public {
type Error = Error;

Expand Down
2 changes: 2 additions & 0 deletions tss-esapi/tests/integration_tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ use tss_esapi::{
};

mod marshall;
mod serde;
mod tpm2b_types_equality_checks;
mod tpma_types_equality_checks;
mod tpml_types_equality_checks;
mod tpms_types_equality_checks;
mod tpmt_types_equality_checks;
pub use self::serde::*;
pub use marshall::*;
pub use tpm2b_types_equality_checks::*;
pub use tpma_types_equality_checks::*;
Expand Down
16 changes: 16 additions & 0 deletions tss-esapi/tests/integration_tests/common/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use serde::{de::DeserializeOwned, Serialize};
use tss_esapi::traits::{Marshall, UnMarshall};

pub fn check_serialise_deserialise<
T: Serialize + DeserializeOwned + Marshall + UnMarshall + Eq + std::fmt::Debug,
>(
val: &T,
) {
let json = serde_json::to_vec(val).expect("Failed to serialise value");

let unmarshalled: T = serde_json::from_slice(&json).expect("Failed to deserialise");

assert_eq!(val, &unmarshalled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ fn marshall_unmarshall() {
crate::common::check_marshall_unmarshall(&private);
}

#[test]
fn serialise_deserialise() {
crate::common::check_serialise_deserialise(&Private::default());
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
crate::common::check_serialise_deserialise(&private);
}

#[test]
fn marshall_unmarshall_offset() {
crate::common::check_marshall_unmarshall_offset(&Private::default());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ fn marshall_unmarshall() {
.for_each(crate::common::check_marshall_unmarshall);
}

#[test]
fn serialise_deserialise() {
crate::common::publics()
.iter()
.for_each(crate::common::check_serialise_deserialise);
}

#[test]
fn tpm2b_conversion() {
crate::common::publics().iter().for_each(|public| {
Expand Down

0 comments on commit 2f3cd59

Please sign in to comment.