Skip to content

Commit b20a3d1

Browse files
committed
pkcs1v15: implement SignatureAlgorithmIdentifier for PKCS1v15 structs
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
1 parent 0c70c46 commit b20a3d1

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ subtle = { version = "2.1.1", default-features = false }
2424
digest = { version = "0.10.5", default-features = false, features = ["alloc", "oid"] }
2525
pkcs1 = { version = "0.7.1", default-features = false, features = ["alloc", "pkcs8"] }
2626
pkcs8 = { version = "0.10", default-features = false, features = ["alloc"] }
27-
serde = { version = "1.0.103", optional = true, default-features = false, features = ["derive"] }
28-
sha2 = { version = "0.10.6", optional = true, default-features = false, features = ["oid"] }
2927
signature = { version = "2", default-features = false , features = ["digest", "rand_core"] }
3028
zeroize = { version = "1", features = ["alloc"] }
3129

30+
# optional dependencies
31+
serde = { version = "1.0.103", optional = true, default-features = false, features = ["derive"] }
32+
sha1 = { version = "0.10.5", optional = true, default-features = false, features = ["oid"] }
33+
sha2 = { version = "0.10.6", optional = true, default-features = false, features = ["oid"] }
34+
3235
[dev-dependencies]
3336
base64ct = { version = "1", features = ["alloc"] }
3437
hex-literal = "0.3.3"

src/pkcs1v15.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
1212
use core::marker::PhantomData;
1313
use digest::Digest;
1414
use pkcs8::{
15-
spki::{der::AnyRef, AlgorithmIdentifierRef, AssociatedAlgorithmIdentifier},
15+
spki::{
16+
der::AnyRef, AlgorithmIdentifierRef, AssociatedAlgorithmIdentifier,
17+
SignatureAlgorithmIdentifier,
18+
},
1619
AssociatedOid, Document, EncodePrivateKey, EncodePublicKey, SecretDocument,
1720
};
1821
use rand_core::CryptoRngCore;
@@ -448,6 +451,19 @@ where
448451
const ALGORITHM_IDENTIFIER: AlgorithmIdentifierRef<'static> = pkcs1::ALGORITHM_ID;
449452
}
450453

454+
impl<D> SignatureAlgorithmIdentifier for SigningKey<D>
455+
where
456+
D: Digest + oid::RsaSignatureAssociatedOid,
457+
{
458+
type Params = AnyRef<'static>;
459+
460+
const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifierRef<'static> =
461+
AlgorithmIdentifierRef {
462+
oid: D::OID,
463+
parameters: Some(AnyRef::NULL),
464+
};
465+
}
466+
451467
impl<D> From<RsaPrivateKey> for SigningKey<D>
452468
where
453469
D: Digest,
@@ -627,6 +643,19 @@ where
627643
const ALGORITHM_IDENTIFIER: AlgorithmIdentifierRef<'static> = pkcs1::ALGORITHM_ID;
628644
}
629645

646+
impl<D> SignatureAlgorithmIdentifier for VerifyingKey<D>
647+
where
648+
D: Digest + oid::RsaSignatureAssociatedOid,
649+
{
650+
type Params = AnyRef<'static>;
651+
652+
const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifierRef<'static> =
653+
AlgorithmIdentifierRef {
654+
oid: D::OID,
655+
parameters: Some(AnyRef::NULL),
656+
};
657+
}
658+
630659
impl<D> From<RsaPublicKey> for VerifyingKey<D>
631660
where
632661
D: Digest,
@@ -795,6 +824,46 @@ impl EncryptingKeypair for DecryptingKey {
795824
}
796825
}
797826

827+
mod oid {
828+
use const_oid::ObjectIdentifier;
829+
830+
/// A trait which associates an RSA-specific OID with a type.
831+
pub(crate) trait RsaSignatureAssociatedOid {
832+
/// The OID associated with this type.
833+
const OID: ObjectIdentifier;
834+
}
835+
836+
#[cfg(feature = "sha1")]
837+
impl RsaSignatureAssociatedOid for sha1::Sha1 {
838+
const OID: ObjectIdentifier =
839+
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.5");
840+
}
841+
842+
#[cfg(feature = "sha2")]
843+
impl RsaSignatureAssociatedOid for sha2::Sha224 {
844+
const OID: ObjectIdentifier =
845+
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.14");
846+
}
847+
848+
#[cfg(feature = "sha2")]
849+
impl RsaSignatureAssociatedOid for sha2::Sha256 {
850+
const OID: ObjectIdentifier =
851+
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.11");
852+
}
853+
854+
#[cfg(feature = "sha2")]
855+
impl RsaSignatureAssociatedOid for sha2::Sha384 {
856+
const OID: ObjectIdentifier =
857+
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.12");
858+
}
859+
860+
#[cfg(feature = "sha2")]
861+
impl RsaSignatureAssociatedOid for sha2::Sha512 {
862+
const OID: ObjectIdentifier =
863+
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.13");
864+
}
865+
}
866+
798867
#[cfg(test)]
799868
mod tests {
800869
use super::*;

0 commit comments

Comments
 (0)