Skip to content

Commit

Permalink
use built-in serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
LWEdslev committed Mar 26, 2024
1 parent d3ca866 commit 28ed656
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 53 deletions.
12 changes: 3 additions & 9 deletions src/pkcs1v15/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::algorithms::pad::uint_to_be_pad;
use ::signature::SignatureEncoding;
use alloc::{boxed::Box, string::ToString};
#[cfg(feature = "serde")]
use serdect::serde::{Deserialize, Serialize};
use serdect::serde::{de, Deserialize, Serialize};
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use num_bigint::BigUint;
use spki::{
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Serialize for Signature {
where
S: serdect::serde::Serializer,
{
serdect::slice::serialize_hex_lower_or_bin(&self.inner.to_bytes_be(), serializer)
serdect::slice::serialize_hex_lower_or_bin(&self.to_bytes(), serializer)
}
}

Expand All @@ -98,13 +98,7 @@ impl<'de> Deserialize<'de> for Signature {
where
D: serdect::serde::Deserializer<'de>,
{
let bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let inner = BigUint::from_bytes_be(&bytes);

Ok(Self {
inner,
len: bytes.len(),
})
serdect::slice::deserialize_hex_or_bin_vec(deserializer)?.as_slice().try_into().map_err(de::Error::custom)
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/pkcs1v15/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use pkcs8::{
spki::{
der::AnyRef, AlgorithmIdentifierRef, AssociatedAlgorithmIdentifier,
SignatureAlgorithmIdentifier,
},
AssociatedOid, EncodePrivateKey, SecretDocument,
}, AssociatedOid, EncodePrivateKey, SecretDocument
};
use rand_core::CryptoRngCore;
#[cfg(feature = "serde")]
use {
pkcs8::PrivateKeyInfo,
pkcs8::DecodePrivateKey,
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
};

use signature::{
Expand Down Expand Up @@ -276,7 +274,7 @@ where
where
S: serdect::serde::Serializer,
{
let der = self.inner.to_pkcs8_der().map_err(ser::Error::custom)?;
let der = self.to_pkcs8_der().map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_lower_or_bin(&der.as_bytes(), serializer)
}
}
Expand All @@ -291,8 +289,7 @@ where
De: serdect::serde::Deserializer<'de>,
{
let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let pki = PrivateKeyInfo::from_der(&der_bytes).map_err(de::Error::custom)?;
Self::try_from(pki).map_err(de::Error::custom)
Self::from_pkcs8_der(&der_bytes).map_err(de::Error::custom)
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/pkcs1v15/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use pkcs8::{

#[cfg(feature = "serde")]
use {
pkcs8::SubjectPublicKeyInfo,
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
spki::DecodePublicKey,
};

use signature::{hazmat::PrehashVerifier, DigestVerifier, Verifier};
Expand Down Expand Up @@ -229,7 +228,7 @@ where
where
S: serde::Serializer,
{
let der = self.inner.to_public_key_der().map_err(ser::Error::custom)?;
let der = self.to_public_key_der().map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_lower_or_bin(&der, serializer)
}
}
Expand All @@ -244,8 +243,7 @@ where
De: serde::Deserializer<'de>,
{
let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let spki = SubjectPublicKeyInfo::from_der(&der_bytes).map_err(de::Error::custom)?;
Self::try_from(spki).map_err(de::Error::custom)
Self::from_public_key_der(&der_bytes).map_err(de::Error::custom)
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/pss/blinded_signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use signature::{
use zeroize::ZeroizeOnDrop;
#[cfg(feature = "serde")]
use {
pkcs8::PrivateKeyInfo,
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
pkcs8::DecodePrivateKey,
};
/// Signing key for producing "blinded" RSASSA-PSS signatures as described in
/// [draft-irtf-cfrg-rsa-blind-signatures](https://datatracker.ietf.org/doc/draft-irtf-cfrg-rsa-blind-signatures/).
Expand Down Expand Up @@ -202,6 +201,17 @@ where
}
}

impl<D> TryFrom<pkcs8::PrivateKeyInfo<'_>> for BlindedSigningKey<D>
where
D: Digest + AssociatedOid,
{
type Error = pkcs8::Error;

fn try_from(private_key_info: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
RsaPrivateKey::try_from(private_key_info).map(Self::new)
}
}

impl<D> ZeroizeOnDrop for BlindedSigningKey<D> where D: Digest {}

impl<D> PartialEq for BlindedSigningKey<D>
Expand All @@ -222,7 +232,7 @@ where
where
S: serde::Serializer,
{
let der = self.inner.to_pkcs8_der().map_err(ser::Error::custom)?;
let der = self.to_pkcs8_der().map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_lower_or_bin(&der.as_bytes(), serializer)
}
}
Expand All @@ -237,10 +247,7 @@ where
De: serde::Deserializer<'de>,
{
let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let pki = PrivateKeyInfo::from_der(&der_bytes).map_err(de::Error::custom)?;
RsaPrivateKey::try_from(pki)
.map_err(de::Error::custom)
.map(Self::new)
Self::from_pkcs8_der(&der_bytes).map_err(de::Error::custom)
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/pss/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use crate::algorithms::pad::uint_to_be_pad;
use ::signature::SignatureEncoding;
use alloc::{boxed::Box, string::ToString};
#[cfg(feature = "serde")]
use serdect::serde::{Deserialize, Serialize};
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use num_bigint::BigUint;
#[cfg(feature = "serde")]
use serdect::serde::{de, Deserialize, Serialize};
use spki::{
der::{asn1::BitString, Result as DerResult},
SignatureBitStringEncoding,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Serialize for Signature {
where
S: serdect::serde::Serializer,
{
serdect::slice::serialize_hex_lower_or_bin(&self.inner.to_bytes_be(), serializer)
serdect::slice::serialize_hex_lower_or_bin(&self.to_bytes(), serializer)
}
}

Expand All @@ -92,13 +92,10 @@ impl<'de> Deserialize<'de> for Signature {
where
D: serdect::serde::Deserializer<'de>,
{
let bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let inner = BigUint::from_bytes_be(&bytes);

Ok(Self {
inner,
len: bytes.len(),
})
serdect::slice::deserialize_hex_or_bin_vec(deserializer)?
.as_slice()
.try_into()
.map_err(de::Error::custom)
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/pss/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use signature::{
use zeroize::ZeroizeOnDrop;
#[cfg(feature = "serde")]
use {
pkcs8::PrivateKeyInfo,
pkcs8::DecodePrivateKey,
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
};

#[cfg(feature = "getrandom")]
Expand Down Expand Up @@ -225,6 +224,17 @@ where
}
}

impl<D> TryFrom<pkcs8::PrivateKeyInfo<'_>> for SigningKey<D>
where
D: Digest + AssociatedOid,
{
type Error = pkcs8::Error;

fn try_from(private_key_info: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
RsaPrivateKey::try_from(private_key_info).map(Self::new)
}
}

impl<D> ZeroizeOnDrop for SigningKey<D> where D: Digest {}

impl<D> PartialEq for SigningKey<D>
Expand All @@ -245,7 +255,7 @@ where
where
S: serdect::serde::Serializer,
{
let der = self.inner.to_pkcs8_der().map_err(ser::Error::custom)?;
let der = self.to_pkcs8_der().map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_lower_or_bin(&der.as_bytes(), serializer)
}
}
Expand All @@ -260,10 +270,7 @@ where
De: serdect::serde::Deserializer<'de>,
{
let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let pki = PrivateKeyInfo::from_der(&der_bytes).map_err(de::Error::custom)?;
RsaPrivateKey::try_from(pki)
.map_err(de::Error::custom)
.map(Self::new)
Self::from_pkcs8_der(&der_bytes).map_err(de::Error::custom)
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/pss/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use core::marker::PhantomData;
use digest::{Digest, FixedOutputReset};
use pkcs8::{
spki::{der::AnyRef, AlgorithmIdentifierRef, AssociatedAlgorithmIdentifier},
Document, EncodePublicKey,
Document, EncodePublicKey, AssociatedOid,
};
use signature::{hazmat::PrehashVerifier, DigestVerifier, Verifier};
#[cfg(feature = "serde")]
use {
pkcs8::{AssociatedOid, SubjectPublicKeyInfo},
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
spki::DecodePublicKey,
};

/// Verifying key for checking the validity of RSASSA-PSS signatures as
Expand Down Expand Up @@ -163,6 +162,17 @@ where
}
}

impl<D> TryFrom<pkcs8::SubjectPublicKeyInfoRef<'_>> for VerifyingKey<D>
where
D: Digest + AssociatedOid,
{
type Error = pkcs8::spki::Error;

fn try_from(spki: pkcs8::SubjectPublicKeyInfoRef<'_>) -> pkcs8::spki::Result<Self> {
RsaPublicKey::try_from(spki).map(Self::new)
}
}

impl<D> PartialEq for VerifyingKey<D>
where
D: Digest,
Expand All @@ -181,7 +191,7 @@ where
where
S: serde::Serializer,
{
let der = self.inner.to_public_key_der().map_err(ser::Error::custom)?;
let der = self.to_public_key_der().map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_lower_or_bin(&der, serializer)
}
}
Expand All @@ -196,10 +206,7 @@ where
De: serde::Deserializer<'de>,
{
let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
let spki = SubjectPublicKeyInfo::from_der(&der_bytes).map_err(de::Error::custom)?;
RsaPublicKey::try_from(spki)
.map_err(de::Error::custom)
.map(Self::new)
Self::from_public_key_der(&der_bytes).map_err(de::Error::custom)
}
}

Expand Down

0 comments on commit 28ed656

Please sign in to comment.