diff --git a/elliptic-curve/Cargo.lock b/elliptic-curve/Cargo.lock index 8cf4ebe3a..33bca6c28 100644 --- a/elliptic-curve/Cargo.lock +++ b/elliptic-curve/Cargo.lock @@ -14,6 +14,11 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "base64ct" +version = "1.6.0" +source = "git+https://github.com/RustCrypto/formats.git#fd069a6ff7a9f5abfd3d797b51f50ca0a5307e44" + [[package]] name = "bitvec" version = "1.0.1" @@ -80,12 +85,11 @@ dependencies = [ [[package]] name = "der" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc302fd9b18d66834a6f092d10ea85489c0ca8ad6b7304092135fab171d853cd" +version = "0.7.1" +source = "git+https://github.com/RustCrypto/formats.git#fd069a6ff7a9f5abfd3d797b51f50ca0a5307e44" dependencies = [ "const-oid", - "pem-rfc7468", + "pem-rfc7468 0.7.0 (git+https://github.com/RustCrypto/formats.git)", "zeroize", ] @@ -105,7 +109,7 @@ name = "elliptic-curve" version = "0.13.2" dependencies = [ "base16ct", - "base64ct", + "base64ct 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "crypto-bigint", "digest", "ff", @@ -113,7 +117,7 @@ dependencies = [ "group", "hex-literal", "hkdf", - "pem-rfc7468", + "pem-rfc7468 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "pkcs8", "rand_core", "sec1", @@ -226,7 +230,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" dependencies = [ - "base64ct", + "base64ct 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "git+https://github.com/RustCrypto/formats.git#fd069a6ff7a9f5abfd3d797b51f50ca0a5307e44" +dependencies = [ + "base64ct 1.6.0 (git+https://github.com/RustCrypto/formats.git)", ] [[package]] @@ -326,10 +338,9 @@ dependencies = [ [[package]] name = "spki" version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0445c905640145c7ea8c1993555957f65e7c46d0535b91ba501bc9bfc85522f" +source = "git+https://github.com/RustCrypto/formats.git#fd069a6ff7a9f5abfd3d797b51f50ca0a5307e44" dependencies = [ - "base64ct", + "base64ct 1.6.0 (git+https://github.com/RustCrypto/formats.git)", "der", ] @@ -374,6 +385,6 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.5.7" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" +checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" diff --git a/elliptic-curve/Cargo.toml b/elliptic-curve/Cargo.toml index 6a34fbe12..0a5442935 100644 --- a/elliptic-curve/Cargo.toml +++ b/elliptic-curve/Cargo.toml @@ -74,3 +74,8 @@ voprf = ["digest"] [package.metadata.docs.rs] features = ["bits", "ecdh", "hash2curve", "jwk", "pem", "std", "voprf"] rustdoc-args = ["--cfg", "docsrs"] + + +[patch.crates-io] +der = { git = "https://github.com/RustCrypto/formats.git" } +spki = { git = "https://github.com/RustCrypto/formats.git" } diff --git a/elliptic-curve/src/public_key.rs b/elliptic-curve/src/public_key.rs index 6af60d4dc..e2d71b3f0 100644 --- a/elliptic-curve/src/public_key.rs +++ b/elliptic-curve/src/public_key.rs @@ -12,6 +12,9 @@ use alloc::boxed::Box; #[cfg(feature = "jwk")] use crate::{JwkEcKey, JwkParameters}; +#[cfg(feature = "pkcs8")] +use pkcs8::spki::{AlgorithmIdentifier, AssociatedAlgorithmIdentifier, ObjectIdentifier}; + #[cfg(feature = "pem")] use core::str::FromStr; @@ -419,7 +422,20 @@ where } } -#[cfg(all(feature = "pkcs8", feature = "sec1"))] +#[cfg(feature = "pkcs8")] +impl AssociatedAlgorithmIdentifier for PublicKey +where + C: AssociatedOid + CurveArithmetic, +{ + type Params = ObjectIdentifier; + + const ALGORITHM_IDENTIFIER: AlgorithmIdentifier = AlgorithmIdentifier { + oid: ALGORITHM_OID, + parameters: Some(C::OID), + }; +} + +#[cfg(feature = "pkcs8")] impl TryFrom> for PublicKey where C: AssociatedOid + CurveArithmetic, @@ -433,7 +449,7 @@ where } } -#[cfg(all(feature = "pkcs8", feature = "sec1"))] +#[cfg(feature = "pkcs8")] impl TryFrom<&pkcs8::SubjectPublicKeyInfoRef<'_>> for PublicKey where C: AssociatedOid + CurveArithmetic, @@ -463,16 +479,11 @@ where FieldBytesSize: ModulusSize, { fn to_public_key_der(&self) -> pkcs8::spki::Result { - let algorithm = pkcs8::AlgorithmIdentifierRef { - oid: ALGORITHM_OID, - parameters: Some((&C::OID).into()), - }; - let public_key_bytes = self.to_encoded_point(false); let subject_public_key = der::asn1::BitStringRef::new(0, public_key_bytes.as_bytes())?; - pkcs8::SubjectPublicKeyInfoRef { - algorithm, + pkcs8::SubjectPublicKeyInfo { + algorithm: Self::ALGORITHM_IDENTIFIER, subject_public_key, } .try_into() diff --git a/elliptic-curve/src/secret_key/pkcs8.rs b/elliptic-curve/src/secret_key/pkcs8.rs index 79215b71f..92c81f18a 100644 --- a/elliptic-curve/src/secret_key/pkcs8.rs +++ b/elliptic-curve/src/secret_key/pkcs8.rs @@ -6,6 +6,7 @@ use crate::{ sec1::{ModulusSize, ValidatePublicKey}, Curve, FieldBytesSize, ALGORITHM_OID, }; +use pkcs8::spki::{AlgorithmIdentifier, AssociatedAlgorithmIdentifier, ObjectIdentifier}; use sec1::EcPrivateKey; // Imports for the `EncodePrivateKey` impl @@ -26,9 +27,21 @@ use { pkcs8::DecodePrivateKey, }; +impl AssociatedAlgorithmIdentifier for SecretKey +where + C: AssociatedOid + Curve, +{ + type Params = ObjectIdentifier; + + const ALGORITHM_IDENTIFIER: AlgorithmIdentifier = AlgorithmIdentifier { + oid: ALGORITHM_OID, + parameters: Some(C::OID), + }; +} + impl TryFrom> for SecretKey where - C: Curve + AssociatedOid + ValidatePublicKey, + C: AssociatedOid + Curve + ValidatePublicKey, FieldBytesSize: ModulusSize, { type Error = pkcs8::Error; @@ -51,6 +64,7 @@ where FieldBytesSize: ModulusSize, { fn to_pkcs8_der(&self) -> pkcs8::Result { + // TODO(tarcieri): make `PrivateKeyInfo` generic around `Params` let algorithm_identifier = pkcs8::AlgorithmIdentifierRef { oid: ALGORITHM_OID, parameters: Some((&C::OID).into()),