Skip to content

Commit 2f3cd59

Browse files
committed
Add serde to Public and Private
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 <william.brown@suse.com>
1 parent 63298c2 commit 2f3cd59

File tree

8 files changed

+86
-2
lines changed

8 files changed

+86
-2
lines changed

.codespellrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[codespell]
22
skip = .git,target,Cargo.lock
3-
ignore-words-list = acsend,crate,keypair,daa
3+
ignore-words-list = acsend,crate,keypair,daa,de,ser

tss-esapi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ paste = "1.0.14"
3737
[dev-dependencies]
3838
env_logger = "0.9.0"
3939
sha2 = "0.10.1"
40+
serde_json = "^1.0.108"
4041

4142
[build-dependencies]
4243
semver = "1.0.7"

tss-esapi/src/structures/buffers/private.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::traits::impl_mu_standard;
5+
use crate::traits::{Marshall, UnMarshall};
6+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
57
use tss_esapi_sys::_PRIVATE;
68

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

911
impl_mu_standard!(Private, TPM2B_PRIVATE);
12+
13+
impl Serialize for Private {
14+
/// Serialise the [Private] data into it's bytes representation of the TCG
15+
/// TPM2B_PRIVATE structure.
16+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
17+
where
18+
S: Serializer,
19+
{
20+
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
21+
serializer.serialize_bytes(&bytes)
22+
}
23+
}
24+
25+
impl<'de> Deserialize<'de> for Private {
26+
/// Deserialise the [Private] data from it's bytes representation of the TCG
27+
/// TPM2B_PRIVATE structure.
28+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
29+
where
30+
D: Deserializer<'de>,
31+
{
32+
let bytes = <Vec<u8>>::deserialize(deserializer)?;
33+
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
34+
}
35+
}

tss-esapi/src/structures/tagged/public.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
attributes::ObjectAttributes,
99
interface_types::algorithm::{HashingAlgorithm, PublicAlgorithm},
1010
structures::{Digest, EccPoint, PublicKeyRsa, SymmetricCipherParameters},
11-
traits::{impl_mu_standard, Marshall},
11+
traits::{impl_mu_standard, Marshall, UnMarshall},
1212
tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC},
1313
Error, Result, ReturnCode, WrapperErrorKind,
1414
};
@@ -18,6 +18,7 @@ use keyed_hash::PublicKeyedHashParameters;
1818
use rsa::PublicRsaParameters;
1919

2020
use log::error;
21+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2122
use std::convert::{TryFrom, TryInto};
2223
use tss_esapi_sys::{TPMU_PUBLIC_ID, TPMU_PUBLIC_PARMS};
2324

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

494495
impl_mu_standard!(Public, TPMT_PUBLIC);
495496

497+
impl Serialize for Public {
498+
/// Serialise the [Public] data into it's bytes representation of the TCG
499+
/// TPMT_PUBLIC structure.
500+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
501+
where
502+
S: Serializer,
503+
{
504+
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
505+
serializer.serialize_bytes(&bytes)
506+
}
507+
}
508+
509+
impl<'de> Deserialize<'de> for Public {
510+
/// Deserialise the [Public] data from it's bytes representation of the TCG
511+
/// TPMT_PUBLIC structure.
512+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
513+
where
514+
D: Deserializer<'de>,
515+
{
516+
let bytes = <Vec<u8>>::deserialize(deserializer)?;
517+
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
518+
}
519+
}
520+
496521
impl TryFrom<TPM2B_PUBLIC> for Public {
497522
type Error = Error;
498523

tss-esapi/tests/integration_tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ use tss_esapi::{
3232
};
3333

3434
mod marshall;
35+
mod serde;
3536
mod tpm2b_types_equality_checks;
3637
mod tpma_types_equality_checks;
3738
mod tpml_types_equality_checks;
3839
mod tpms_types_equality_checks;
3940
mod tpmt_types_equality_checks;
41+
pub use self::serde::*;
4042
pub use marshall::*;
4143
pub use tpm2b_types_equality_checks::*;
4244
pub use tpma_types_equality_checks::*;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use serde::{de::DeserializeOwned, Serialize};
4+
use tss_esapi::traits::{Marshall, UnMarshall};
5+
6+
pub fn check_serialise_deserialise<
7+
T: Serialize + DeserializeOwned + Marshall + UnMarshall + Eq + std::fmt::Debug,
8+
>(
9+
val: &T,
10+
) {
11+
let json = serde_json::to_vec(val).expect("Failed to serialise value");
12+
13+
let unmarshalled: T = serde_json::from_slice(&json).expect("Failed to deserialise");
14+
15+
assert_eq!(val, &unmarshalled);
16+
}

tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ fn marshall_unmarshall() {
1111
crate::common::check_marshall_unmarshall(&private);
1212
}
1313

14+
#[test]
15+
fn serialise_deserialise() {
16+
crate::common::check_serialise_deserialise(&Private::default());
17+
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
18+
crate::common::check_serialise_deserialise(&private);
19+
}
20+
1421
#[test]
1522
fn marshall_unmarshall_offset() {
1623
crate::common::check_marshall_unmarshall_offset(&Private::default());

tss-esapi/tests/integration_tests/structures_tests/tagged_tests/public.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ fn marshall_unmarshall() {
1414
.for_each(crate::common::check_marshall_unmarshall);
1515
}
1616

17+
#[test]
18+
fn serialise_deserialise() {
19+
crate::common::publics()
20+
.iter()
21+
.for_each(crate::common::check_serialise_deserialise);
22+
}
23+
1724
#[test]
1825
fn tpm2b_conversion() {
1926
crate::common::publics().iter().for_each(|public| {

0 commit comments

Comments
 (0)