Skip to content

Commit

Permalink
Add serde to Public and Private
Browse files Browse the repository at this point in the history
Signed-off-by: William Brown <[email protected]>
  • Loading branch information
Firstyear committed Dec 14, 2023
1 parent 2dfc315 commit 12d0f4b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tss-esapi/src/structures/buffers/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@

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

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

impl_mu_standard!(Private, TPM2B_PRIVATE);

impl Serialize for Private {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.marshall()
.map_err(|err| {
serde::ser::Error::custom(err)
})?;
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for Private {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = <&[u8]>::deserialize(deserializer)?;
Self::unmarshall(bytes)
.map_err(|err| {
serde::de::Error::custom(err)
})
}
}

27 changes: 27 additions & 0 deletions tss-esapi/src/structures/tagged/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rsa::PublicRsaParameters;
use log::error;
use std::convert::{TryFrom, TryInto};
use tss_esapi_sys::{TPMU_PUBLIC_ID, TPMU_PUBLIC_PARMS};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// A builder for the [Public] type.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -493,6 +494,32 @@ impl TryFrom<TPMT_PUBLIC> for Public {

impl_mu_standard!(Public, TPMT_PUBLIC);

impl Serialize for Public {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.marshall()
.map_err(|err| {
serde::ser::Error::custom(err)
})?;
serializer.serialize_bytes(&bytes)
}
}

impl<'de> Deserialize<'de> for Public {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = <&[u8]>::deserialize(deserializer)?;
Self::unmarshall(bytes)
.map_err(|err| {
serde::de::Error::custom(err)
})
}
}

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

Expand Down

0 comments on commit 12d0f4b

Please sign in to comment.