Skip to content

Commit 1411c46

Browse files
authored
Merge pull request #526 from Superhepper/optional-serde
Makes the serde dependency optional.
2 parents 5c3a003 + 058cf33 commit 1411c46

File tree

12 files changed

+110
-83
lines changed

12 files changed

+110
-83
lines changed

tss-esapi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ name = "hmac"
1717

1818
[dependencies]
1919
bitfield = "0.14"
20-
serde = { version = "1.0.115", features = ["derive"] }
20+
serde = { version = "1.0.115", features = ["derive"], optional = true, default-features = false }
2121
malloced = "1.3.1"
2222
log = "0.4.11"
2323
enumflags2 = "0.7.7"

tss-esapi/src/abstraction/public.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ use picky_asn1::wrapper::{IntegerAsn1, OctetStringAsn1};
1212
use picky_asn1_x509::{
1313
AlgorithmIdentifier, EcParameters, EcPoint, PublicKey, RsaPublicKey, SubjectPublicKeyInfo,
1414
};
15-
use serde::{Deserialize, Serialize};
1615

1716
/// Can be converted from [`crate::structures::Public`] when not a fully constructed
1817
/// [`picky_asn1_x509::SubjectPublicKeyInfo`] is required.
1918
///
2019
/// # Details
21-
///
2220
/// Holds either [`picky_asn1_x509::RsaPublicKey`] for [`crate::structures::Public::Rsa`] or
2321
/// [`picky_asn1_x509::EcPoint`] for [`crate::structures::Public::Ecc`].
24-
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
22+
///
23+
/// This object can be serialized and deserialized
24+
/// using serde if the `serde` feature is enabled.
25+
#[derive(Debug, PartialEq, Eq, Clone)]
26+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
2527
pub enum DecodedKey {
2628
RsaPublicKey(RsaPublicKey),
2729
EcPoint(EcPoint),

tss-esapi/src/abstraction/transient/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::{
2929
};
3030

3131
use log::error;
32-
use serde::{Deserialize, Serialize};
3332
use std::collections::HashMap;
3433
use std::convert::{AsMut, AsRef, TryFrom, TryInto};
3534
use zeroize::Zeroize;
@@ -65,17 +64,22 @@ pub enum KeyParams {
6564

6665
/// Structure representing a key created or stored in the TPM
6766
///
67+
/// # Details
6868
/// The `public` field represents the public part of the key in plain text,
6969
/// while `private` is the encrypted version of the private key.
7070
///
7171
/// For information on public key formats, see the documentation of [`PublicKey`].
7272
/// The private part of the key should be treated as an opaque binary blob.
7373
///
74+
/// This object can be serialized and deserialized
75+
/// using serde if the `serde` feature is enabled.
76+
///
7477
/// # Warning
7578
///
7679
/// If the Owner hierarchy is cleared, any key material generated
7780
/// prior to that event will become unusable.
78-
#[derive(Debug, Serialize, Deserialize, Clone, Zeroize)]
81+
#[derive(Debug, Clone, Zeroize)]
82+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
7983
pub struct KeyMaterial {
8084
public: PublicKey,
8185
private: Vec<u8>,

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
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};
75
use tss_esapi_sys::_PRIVATE;
86

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

119
impl_mu_standard!(Private, TPM2B_PRIVATE);
1210

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-
}
11+
cfg_if::cfg_if! {
12+
if #[cfg(feature = "serde")] {
13+
use crate::traits::{Marshall, UnMarshall};
14+
impl serde::Serialize for Private {
15+
/// Serialize the [Private] data into it's bytes representation of the TCG
16+
/// TPM2B_PRIVATE structure.
17+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
18+
where
19+
S: serde::Serializer,
20+
{
21+
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
22+
serializer.serialize_bytes(&bytes)
23+
}
24+
}
2425

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)
26+
impl<'de> serde::Deserialize<'de> for Private {
27+
/// Deserialize the [Private] data from it's bytes representation of the TCG
28+
/// TPM2B_PRIVATE structure.
29+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
30+
where
31+
D: serde::Deserializer<'de>,
32+
{
33+
let bytes = <Vec<u8>>::deserialize(deserializer)?;
34+
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
35+
}
36+
}
3437
}
3538
}

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

Lines changed: 30 additions & 23 deletions
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, UnMarshall},
11+
traits::{impl_mu_standard, Marshall},
1212
tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC},
1313
Error, Result, ReturnCode, WrapperErrorKind,
1414
};
@@ -18,7 +18,6 @@ use keyed_hash::PublicKeyedHashParameters;
1818
use rsa::PublicRsaParameters;
1919

2020
use log::error;
21-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2221
use std::convert::{TryFrom, TryInto};
2322
use tss_esapi_sys::{TPMU_PUBLIC_ID, TPMU_PUBLIC_PARMS};
2423

@@ -299,6 +298,9 @@ impl Default for PublicBuilder {
299298
///
300299
/// # Details
301300
/// This corresponds to TPMT_PUBLIC
301+
///
302+
/// This object can be serialized and deserialized
303+
/// using serde if the `serde` feature is enabled.
302304
#[derive(Debug, Clone, Eq, PartialEq)]
303305
pub enum Public {
304306
Rsa {
@@ -500,30 +502,35 @@ impl TryFrom<TPMT_PUBLIC> for Public {
500502

501503
impl_mu_standard!(Public, TPMT_PUBLIC);
502504

503-
impl Serialize for Public {
504-
/// Serialize the [Public] data into it's bytes representation of the TCG
505-
/// TPMT_PUBLIC structure.
506-
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
507-
where
508-
S: Serializer,
509-
{
510-
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
511-
serializer.serialize_bytes(&bytes)
512-
}
513-
}
505+
cfg_if::cfg_if! {
506+
if #[cfg(feature = "serde")] {
507+
use crate::traits::UnMarshall;
508+
509+
impl serde::Serialize for Public {
510+
/// Serialize the [Public] data into it's bytes representation of the TCG
511+
/// TPMT_PUBLIC structure.
512+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
513+
where
514+
S: serde::Serializer,
515+
{
516+
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
517+
serializer.serialize_bytes(&bytes)
518+
}
519+
}
514520

515-
impl<'de> Deserialize<'de> for Public {
516-
/// Deserialise the [Public] data from it's bytes representation of the TCG
517-
/// TPMT_PUBLIC structure.
518-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
519-
where
520-
D: Deserializer<'de>,
521-
{
522-
let bytes = <Vec<u8>>::deserialize(deserializer)?;
523-
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
521+
impl<'de> serde::Deserialize<'de> for Public {
522+
/// Deserialise the [Public] data from it's bytes representation of the TCG
523+
/// TPMT_PUBLIC structure.
524+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
525+
where
526+
D: serde::Deserializer<'de>,
527+
{
528+
let bytes = <Vec<u8>>::deserialize(deserializer)?;
529+
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
530+
}
531+
}
524532
}
525533
}
526-
527534
impl TryFrom<TPM2B_PUBLIC> for Public {
528535
type Error = Error;
529536

tss-esapi/src/structures/tpm_context.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use crate::{
55
interface_types::{data_handles::Saved, reserved_handles::Hierarchy},
66
structures::TpmContextData,
77
traits::impl_mu_standard,
8-
traits::{Marshall, UnMarshall},
98
tss2_esys::TPMS_CONTEXT,
109
Error, Result,
1110
};
12-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1311
use std::convert::TryFrom;
1412

1513
/// Structure holding the content of a TPM context.
14+
///
15+
/// # Details
16+
/// This object can be serialized and deserialized
17+
/// using serde if the `serde` feature is enabled.
1618
#[derive(Debug, Clone)]
1719
pub struct SavedTpmContext {
1820
sequence: u64,
@@ -79,26 +81,31 @@ impl From<SavedTpmContext> for TPMS_CONTEXT {
7981

8082
impl_mu_standard!(SavedTpmContext, TPMS_CONTEXT);
8183

82-
impl Serialize for SavedTpmContext {
83-
/// Serialize the [SavedTpmContext] data into it's bytes representation of the TCG
84-
/// TPMS_CONTEXT structure.
85-
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
86-
where
87-
S: Serializer,
88-
{
89-
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
90-
serializer.serialize_bytes(&bytes)
91-
}
92-
}
84+
cfg_if::cfg_if! {
85+
if #[cfg(feature = "serde")] {
86+
use crate::traits::{Marshall, UnMarshall};
87+
impl serde::Serialize for SavedTpmContext {
88+
/// Serialize the [SavedTpmContext] data into it's bytes representation of the TCG
89+
/// TPMS_CONTEXT structure.
90+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
91+
where
92+
S: serde::Serializer,
93+
{
94+
let bytes = self.marshall().map_err(serde::ser::Error::custom)?;
95+
serializer.serialize_bytes(&bytes)
96+
}
97+
}
9398

94-
impl<'de> Deserialize<'de> for SavedTpmContext {
95-
/// Deserialize the [SavedTpmContext] data from it's bytes representation of the TCG
96-
/// TPMS_CONTEXT structure.
97-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
98-
where
99-
D: Deserializer<'de>,
100-
{
101-
let bytes = <Vec<u8>>::deserialize(deserializer)?;
102-
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
99+
impl<'de> serde::Deserialize<'de> for SavedTpmContext {
100+
/// Deserialize the [SavedTpmContext] data from it's bytes representation of the TCG
101+
/// TPMS_CONTEXT structure.
102+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
103+
where
104+
D: serde::Deserializer<'de>,
105+
{
106+
let bytes = <Vec<u8>>::deserialize(deserializer)?;
107+
Self::unmarshall(&bytes).map_err(serde::de::Error::custom)
108+
}
109+
}
103110
}
104111
}

tss-esapi/src/utils/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::structures::{
2020
PublicRsaParametersBuilder, RsaExponent, RsaScheme, SymmetricDefinitionObject,
2121
};
2222
use crate::{Context, Error, Result, WrapperErrorKind};
23-
use serde::{Deserialize, Serialize};
2423
use std::convert::TryFrom;
2524
use zeroize::Zeroize;
2625

@@ -201,7 +200,12 @@ pub fn create_unrestricted_signing_ecc_public(
201200
}
202201

203202
/// Container for public key values
204-
#[derive(Debug, Clone, Serialize, Deserialize, Zeroize, PartialEq, Eq)]
203+
///
204+
/// # Details
205+
/// This object can be serialized and deserialized
206+
/// using serde if the `serde` feature is enabled.
207+
#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
208+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
205209
pub enum PublicKey {
206210
/// RSA public modulus (see 27.5.3.4 in the Architecture spec)
207211
///

tss-esapi/tests/all-fedora.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ tpm2-abrmd \
4343
###################
4444
# Build the crate #
4545
###################
46-
RUST_BACKTRACE=1 cargo build --features "generate-bindings integration-tests"
46+
RUST_BACKTRACE=1 cargo build --features "generate-bindings integration-tests serde"
4747

4848
#################
4949
# Run the tests #
5050
#################
51-
TEST_TCTI=tabrmd:bus_type=session RUST_BACKTRACE=1 RUST_LOG=info cargo test --features "generate-bindings integration-tests" -- --test-threads=1 --nocapture
51+
TEST_TCTI=tabrmd:bus_type=session RUST_BACKTRACE=1 RUST_LOG=info cargo test --features "generate-bindings integration-tests serde" -- --test-threads=1 --nocapture

tss-esapi/tests/all-opensuse.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ swtpm socket --tpm2 \
3030
###################
3131
# Build the crate #
3232
###################
33-
RUST_BACKTRACE=1 cargo build --features "generate-bindings integration-tests"
33+
RUST_BACKTRACE=1 cargo build --features "generate-bindings integration-tests serde"
3434

3535
#################
3636
# Run the tests #
3737
#################
38-
TEST_TCTI="swtpm:host=localhost,port=2321" RUST_BACKTRACE=1 RUST_LOG=info cargo test --features "generate-bindings integration-tests" -- --test-threads=1 --nocapture
38+
TEST_TCTI="swtpm:host=localhost,port=2321" RUST_BACKTRACE=1 RUST_LOG=info cargo test --features "generate-bindings integration-tests serde" -- --test-threads=1 --nocapture
3939

tss-esapi/tests/all-ubuntu.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ fi
2424
# Generate bindings for non-"standard" versions #
2525
#################################################
2626
if [[ "${TPM2_TSS_VERSION}" != "${TPM2_TSS_BINDINGS_VERSION}" ]]; then
27-
FEATURES="generate-bindings integration-tests"
27+
FEATURES="generate-bindings integration-tests serde"
2828
else
29-
FEATURES="integration-tests"
29+
FEATURES="integration-tests serde"
3030
fi
3131

3232
if [[ ! -z ${TPM2_TSS_PATH:+x} ]]; then

0 commit comments

Comments
 (0)