From e525203d8ba04fded00a1fecf4fbce617f46e263 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 15:49:14 +0200 Subject: [PATCH 01/31] work in progress Keypair --- identity/src/keypair.rs | 195 ++++++++++++++++-------- identity/src/lib.rs | 3 + transports/noise/src/protocol/x25519.rs | 6 +- 3 files changed, 139 insertions(+), 65 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 6b76f5638de..a1dbd00cfd7 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -36,6 +36,56 @@ use crate::secp256k1; #[cfg(feature = "ecdsa")] use crate::ecdsa; +// /// Identity keypair of a node. +// /// +// /// # Example: Generating RSA keys with OpenSSL +// /// +// /// ```text +// /// openssl genrsa -out private.pem 2048 +// /// openssl pkcs8 -in private.pem -inform PEM -topk8 -out private.pk8 -outform DER -nocrypt +// /// rm private.pem # optional +// /// ``` +// /// +// /// Loading the keys: +// /// +// /// ```text +// /// let mut bytes = std::fs::read("private.pk8").unwrap(); +// /// let keypair = Keypair::rsa_from_pkcs8(&mut bytes); +// /// ``` +// /// +// #[derive(Debug, Clone)] +// #[allow(clippy::large_enum_variant)] +// pub enum Keypair { +// /// An Ed25519 keypair. +// #[cfg(feature = "ed25519")] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `Keypair::try_into_ed25519` instead." +// )] +// Ed25519(ed25519::Keypair), +// /// An RSA keypair. +// #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `Keypair::try_into_rsa` instead." +// )] +// Rsa(rsa::Keypair), +// /// A Secp256k1 keypair. +// #[cfg(feature = "secp256k1")] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `Keypair::try_into_secp256k1` instead." +// )] +// Secp256k1(secp256k1::Keypair), +// /// An ECDSA keypair. +// #[cfg(feature = "ecdsa")] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `Keypair::try_into_ecdsa` instead." +// )] +// Ecdsa(ecdsa::Keypair), +// } + /// Identity keypair of a node. /// /// # Example: Generating RSA keys with OpenSSL @@ -55,57 +105,51 @@ use crate::ecdsa; /// #[derive(Debug, Clone)] #[allow(clippy::large_enum_variant)] -pub enum Keypair { +enum KeypairType { /// An Ed25519 keypair. #[cfg(feature = "ed25519")] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::try_into_ed25519` instead." - )] Ed25519(ed25519::Keypair), /// An RSA keypair. #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::try_into_rsa` instead." - )] Rsa(rsa::Keypair), /// A Secp256k1 keypair. #[cfg(feature = "secp256k1")] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::try_into_secp256k1` instead." - )] Secp256k1(secp256k1::Keypair), /// An ECDSA keypair. #[cfg(feature = "ecdsa")] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::try_into_ecdsa` instead." - )] Ecdsa(ecdsa::Keypair), } +#[derive(Debug, Clone)] +pub struct Keypair { + keypair: KeypairType, +} + impl Keypair { /// Generate a new Ed25519 keypair. #[cfg(feature = "ed25519")] pub fn generate_ed25519() -> Keypair { + // #[allow(deprecated)] + // KeypairType::Ed25519(ed25519::Keypair::generate()) + #[allow(deprecated)] - Keypair::Ed25519(ed25519::Keypair::generate()) + Keypair { + keypair: KeypairType::Ed25519(ed25519::Keypair::generate()), + } } /// Generate a new Secp256k1 keypair. #[cfg(feature = "secp256k1")] - pub fn generate_secp256k1() -> Keypair { + pub fn generate_secp256k1() -> KeypairType { #[allow(deprecated)] - Keypair::Secp256k1(secp256k1::Keypair::generate()) + KeypairType::Secp256k1(secp256k1::Keypair::generate()) } /// Generate a new ECDSA keypair. #[cfg(feature = "ecdsa")] - pub fn generate_ecdsa() -> Keypair { + pub fn generate_ecdsa() -> KeypairType { #[allow(deprecated)] - Keypair::Ecdsa(ecdsa::Keypair::generate()) + KeypairType::Ecdsa(ecdsa::Keypair::generate()) } #[cfg(feature = "ed25519")] @@ -166,8 +210,13 @@ impl Keypair { /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { + // #[allow(deprecated)] + // rsa::Keypair::from_pkcs8(pkcs8_der).map(KeypairType::Rsa) + #[allow(deprecated)] - rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa) + Ok(rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { + keypair: KeypairType::Rsa(kp), + })?) } /// Decode a keypair from a DER-encoded Secp256k1 secret key in an ECPrivateKey @@ -175,26 +224,28 @@ impl Keypair { /// /// [RFC5915]: https://tools.ietf.org/html/rfc5915 #[cfg(feature = "secp256k1")] - pub fn secp256k1_from_der(der: &mut [u8]) -> Result { + pub fn secp256k1_from_der(der: &mut [u8]) -> Result { #[allow(deprecated)] secp256k1::SecretKey::from_der(der) - .map(|sk| Keypair::Secp256k1(secp256k1::Keypair::from(sk))) + .map(|sk| KeypairType::Secp256k1(secp256k1::Keypair::from(sk))) } #[cfg(feature = "ed25519")] pub fn ed25519_from_bytes(bytes: impl AsMut<[u8]>) -> Result { #[allow(deprecated)] - Ok(Keypair::Ed25519(ed25519::Keypair::from( - ed25519::SecretKey::from_bytes(bytes)?, - ))) + Ok(Keypair { + keypair: KeypairType::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( + bytes, + )?)), + }) } /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { - use Keypair::*; + use KeypairType::*; #[allow(deprecated)] - match self { + match self.keypair { #[cfg(feature = "ed25519")] Ed25519(ref pair) => Ok(pair.sign(msg)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] @@ -208,9 +259,9 @@ impl Keypair { /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { - use Keypair::*; + use KeypairType::*; #[allow(deprecated)] - match self { + match &self.keypair { #[cfg(feature = "ed25519")] Ed25519(pair) => PublicKey::Ed25519(pair.public()), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] @@ -234,18 +285,20 @@ impl Keypair { return Err(DecodingError::missing_feature("ed25519")); #[allow(deprecated)] - let pk: proto::PrivateKey = match self { + let pk: proto::PrivateKey = match &self.keypair { #[cfg(feature = "ed25519")] - Self::Ed25519(data) => proto::PrivateKey { + KeypairType::Ed25519(data) => proto::PrivateKey { Type: proto::KeyType::Ed25519, Data: data.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Self::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), + KeypairType::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] - Self::Secp256k1(_) => return Err(DecodingError::encoding_unsupported("secp256k1")), + KeypairType::Secp256k1(_) => { + return Err(DecodingError::encoding_unsupported("secp256k1")) + } #[cfg(feature = "ecdsa")] - Self::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), + KeypairType::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), }; let mut buf = Vec::with_capacity(pk.get_size()); @@ -267,10 +320,22 @@ impl Keypair { match private_key.Type { #[cfg(feature = "ed25519")] - proto::KeyType::Ed25519 => - { + proto::KeyType::Ed25519 => { #[allow(deprecated)] - ed25519::Keypair::decode(&mut private_key.Data).map(Keypair::Ed25519) + // ed25519::Keypair::decode(&mut private_key.Data).map(Keypair { + // keypair: KeypairType::Ed25519, + // }) + // #[allow(deprecated)] + // ed25519::Keypair::decode(&mut private_key.Data).map(KeypairType::Ed25519) + // let keypair = + // ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { + // keypair: KeypairType::Ed25519(kp), + // })?; + Ok( + ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { + keypair: KeypairType::Ed25519(kp), + })?, + ) } #[cfg(not(feature = "ed25519"))] proto::KeyType::Ed25519 => Err(DecodingError::missing_feature("ed25519")), @@ -285,7 +350,7 @@ impl Keypair { impl From for Keypair { fn from(kp: ecdsa::Keypair) -> Self { #[allow(deprecated)] - Keypair::Ecdsa(kp) + KeypairType::Ecdsa(kp) } } @@ -293,7 +358,10 @@ impl From for Keypair { impl From for Keypair { fn from(kp: ed25519::Keypair) -> Self { #[allow(deprecated)] - Keypair::Ed25519(kp) + // KeypairType::Ed25519(kp) + Keypair { + keypair: KeypairType::Ed25519(kp), + } } } @@ -301,7 +369,7 @@ impl From for Keypair { impl From for Keypair { fn from(kp: secp256k1::Keypair) -> Self { #[allow(deprecated)] - Keypair::Secp256k1(kp) + KeypairType::Secp256k1(kp) } } @@ -309,7 +377,10 @@ impl From for Keypair { impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { #[allow(deprecated)] - Keypair::Rsa(kp) + // KeypairType::Rsa(kp) + Keypair { + keypair: KeypairType::Rsa(kp), + } } } @@ -319,14 +390,14 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] - match self { - Keypair::Ed25519(inner) => Ok(inner), + match self.keypair { + KeypairType::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -338,13 +409,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self { - Keypair::Ecdsa(inner) => Ok(inner), + KeypairType::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), } } } @@ -356,13 +427,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self { - Keypair::Secp256k1(inner) => Ok(inner), + KeypairType::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "ecdsa")] - Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -373,14 +444,14 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] - match self { - Keypair::Rsa(inner) => Ok(inner), + match self.keypair { + KeypairType::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } diff --git a/identity/src/lib.rs b/identity/src/lib.rs index ea9ced51622..9f938552ac7 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -126,6 +126,9 @@ impl From<&PublicKey> for proto::PublicKey { pub use error::{DecodingError, OtherVariantError, SigningError}; pub use keypair::{Keypair, PublicKey}; +// use keypair::Keypair; +// pub use keypair::PublicKey; + #[cfg(feature = "peerid")] pub use peer_id::{ParseError, PeerId}; diff --git a/transports/noise/src/protocol/x25519.rs b/transports/noise/src/protocol/x25519.rs index cc1586feb0d..ce681d838b3 100644 --- a/transports/noise/src/protocol/x25519.rs +++ b/transports/noise/src/protocol/x25519.rs @@ -161,9 +161,9 @@ impl Keypair { /// > * [Noise: Static Key Reuse](http://www.noiseprotocol.org/noise.html#security-considerations) #[allow(unreachable_patterns)] pub fn from_identity(id_keys: &identity::Keypair) -> Option> { - match id_keys { - identity::Keypair::Ed25519(p) => { - let kp = Keypair::from(SecretKey::from_ed25519(&p.secret())); + match id_keys.clone().try_into_ed25519().ok() { + Some(ed25519_keypair) => { + let kp = Keypair::from(SecretKey::from_ed25519(&ed25519_keypair.secret())); let id = KeypairIdentity { public: id_keys.public(), signature: None, From 65d6546eb8e7a8cfb9e0e32059a34146ccf5c3b8 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 15:55:57 +0200 Subject: [PATCH 02/31] clean up --- identity/src/keypair.rs | 70 ++--------------------------------------- 1 file changed, 2 insertions(+), 68 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index a1dbd00cfd7..25f4d53eab8 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -36,56 +36,6 @@ use crate::secp256k1; #[cfg(feature = "ecdsa")] use crate::ecdsa; -// /// Identity keypair of a node. -// /// -// /// # Example: Generating RSA keys with OpenSSL -// /// -// /// ```text -// /// openssl genrsa -out private.pem 2048 -// /// openssl pkcs8 -in private.pem -inform PEM -topk8 -out private.pk8 -outform DER -nocrypt -// /// rm private.pem # optional -// /// ``` -// /// -// /// Loading the keys: -// /// -// /// ```text -// /// let mut bytes = std::fs::read("private.pk8").unwrap(); -// /// let keypair = Keypair::rsa_from_pkcs8(&mut bytes); -// /// ``` -// /// -// #[derive(Debug, Clone)] -// #[allow(clippy::large_enum_variant)] -// pub enum Keypair { -// /// An Ed25519 keypair. -// #[cfg(feature = "ed25519")] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `Keypair::try_into_ed25519` instead." -// )] -// Ed25519(ed25519::Keypair), -// /// An RSA keypair. -// #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `Keypair::try_into_rsa` instead." -// )] -// Rsa(rsa::Keypair), -// /// A Secp256k1 keypair. -// #[cfg(feature = "secp256k1")] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `Keypair::try_into_secp256k1` instead." -// )] -// Secp256k1(secp256k1::Keypair), -// /// An ECDSA keypair. -// #[cfg(feature = "ecdsa")] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `Keypair::try_into_ecdsa` instead." -// )] -// Ecdsa(ecdsa::Keypair), -// } - /// Identity keypair of a node. /// /// # Example: Generating RSA keys with OpenSSL @@ -129,9 +79,6 @@ impl Keypair { /// Generate a new Ed25519 keypair. #[cfg(feature = "ed25519")] pub fn generate_ed25519() -> Keypair { - // #[allow(deprecated)] - // KeypairType::Ed25519(ed25519::Keypair::generate()) - #[allow(deprecated)] Keypair { keypair: KeypairType::Ed25519(ed25519::Keypair::generate()), @@ -210,9 +157,6 @@ impl Keypair { /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { - // #[allow(deprecated)] - // rsa::Keypair::from_pkcs8(pkcs8_der).map(KeypairType::Rsa) - #[allow(deprecated)] Ok(rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { keypair: KeypairType::Rsa(kp), @@ -320,17 +264,9 @@ impl Keypair { match private_key.Type { #[cfg(feature = "ed25519")] - proto::KeyType::Ed25519 => { + proto::KeyType::Ed25519 => + { #[allow(deprecated)] - // ed25519::Keypair::decode(&mut private_key.Data).map(Keypair { - // keypair: KeypairType::Ed25519, - // }) - // #[allow(deprecated)] - // ed25519::Keypair::decode(&mut private_key.Data).map(KeypairType::Ed25519) - // let keypair = - // ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { - // keypair: KeypairType::Ed25519(kp), - // })?; Ok( ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { keypair: KeypairType::Ed25519(kp), @@ -358,7 +294,6 @@ impl From for Keypair { impl From for Keypair { fn from(kp: ed25519::Keypair) -> Self { #[allow(deprecated)] - // KeypairType::Ed25519(kp) Keypair { keypair: KeypairType::Ed25519(kp), } @@ -377,7 +312,6 @@ impl From for Keypair { impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { #[allow(deprecated)] - // KeypairType::Rsa(kp) Keypair { keypair: KeypairType::Rsa(kp), } From 7940834afb3b8fe761e70940b41c35e1f71da3bc Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 13:55:05 +0200 Subject: [PATCH 03/31] remove KeypairType return and replace by Keypair --- identity/src/keypair.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 25f4d53eab8..3e9a7d92171 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -87,16 +87,20 @@ impl Keypair { /// Generate a new Secp256k1 keypair. #[cfg(feature = "secp256k1")] - pub fn generate_secp256k1() -> KeypairType { + pub fn generate_secp256k1() -> Keypair { #[allow(deprecated)] - KeypairType::Secp256k1(secp256k1::Keypair::generate()) + Keypair { + keypair: KeypairType::Secp256k1(secp256k1::Keypair::generate()), + } } /// Generate a new ECDSA keypair. #[cfg(feature = "ecdsa")] - pub fn generate_ecdsa() -> KeypairType { + pub fn generate_ecdsa() -> Keypair { #[allow(deprecated)] - KeypairType::Ecdsa(ecdsa::Keypair::generate()) + Keypair { + keypair: KeypairType::Ecdsa(ecdsa::Keypair::generate()), + } } #[cfg(feature = "ed25519")] From 8670e748f3b31f4068129190ff8aa30fd42b3051 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 13:58:36 +0200 Subject: [PATCH 04/31] move pub Keypair to the top of file and replace KeypairType by Inner --- identity/src/keypair.rs | 83 ++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 3e9a7d92171..075eee0926e 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -36,6 +36,11 @@ use crate::secp256k1; #[cfg(feature = "ecdsa")] use crate::ecdsa; +#[derive(Debug, Clone)] +pub struct Keypair { + keypair: Inner, +} + /// Identity keypair of a node. /// /// # Example: Generating RSA keys with OpenSSL @@ -55,7 +60,7 @@ use crate::ecdsa; /// #[derive(Debug, Clone)] #[allow(clippy::large_enum_variant)] -enum KeypairType { +enum Inner { /// An Ed25519 keypair. #[cfg(feature = "ed25519")] Ed25519(ed25519::Keypair), @@ -70,18 +75,13 @@ enum KeypairType { Ecdsa(ecdsa::Keypair), } -#[derive(Debug, Clone)] -pub struct Keypair { - keypair: KeypairType, -} - impl Keypair { /// Generate a new Ed25519 keypair. #[cfg(feature = "ed25519")] pub fn generate_ed25519() -> Keypair { #[allow(deprecated)] Keypair { - keypair: KeypairType::Ed25519(ed25519::Keypair::generate()), + keypair: Inner::Ed25519(ed25519::Keypair::generate()), } } @@ -90,7 +90,7 @@ impl Keypair { pub fn generate_secp256k1() -> Keypair { #[allow(deprecated)] Keypair { - keypair: KeypairType::Secp256k1(secp256k1::Keypair::generate()), + keypair: Inner::Secp256k1(secp256k1::Keypair::generate()), } } @@ -99,7 +99,7 @@ impl Keypair { pub fn generate_ecdsa() -> Keypair { #[allow(deprecated)] Keypair { - keypair: KeypairType::Ecdsa(ecdsa::Keypair::generate()), + keypair: Inner::Ecdsa(ecdsa::Keypair::generate()), } } @@ -163,7 +163,7 @@ impl Keypair { pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { #[allow(deprecated)] Ok(rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { - keypair: KeypairType::Rsa(kp), + keypair: Inner::Rsa(kp), })?) } @@ -172,17 +172,16 @@ impl Keypair { /// /// [RFC5915]: https://tools.ietf.org/html/rfc5915 #[cfg(feature = "secp256k1")] - pub fn secp256k1_from_der(der: &mut [u8]) -> Result { + pub fn secp256k1_from_der(der: &mut [u8]) -> Result { #[allow(deprecated)] - secp256k1::SecretKey::from_der(der) - .map(|sk| KeypairType::Secp256k1(secp256k1::Keypair::from(sk))) + secp256k1::SecretKey::from_der(der).map(|sk| Inner::Secp256k1(secp256k1::Keypair::from(sk))) } #[cfg(feature = "ed25519")] pub fn ed25519_from_bytes(bytes: impl AsMut<[u8]>) -> Result { #[allow(deprecated)] Ok(Keypair { - keypair: KeypairType::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( + keypair: Inner::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( bytes, )?)), }) @@ -191,7 +190,7 @@ impl Keypair { /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { - use KeypairType::*; + use Inner::*; #[allow(deprecated)] match self.keypair { #[cfg(feature = "ed25519")] @@ -207,7 +206,7 @@ impl Keypair { /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { - use KeypairType::*; + use Inner::*; #[allow(deprecated)] match &self.keypair { #[cfg(feature = "ed25519")] @@ -235,18 +234,16 @@ impl Keypair { #[allow(deprecated)] let pk: proto::PrivateKey = match &self.keypair { #[cfg(feature = "ed25519")] - KeypairType::Ed25519(data) => proto::PrivateKey { + Inner::Ed25519(data) => proto::PrivateKey { Type: proto::KeyType::Ed25519, Data: data.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeypairType::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), + Inner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] - KeypairType::Secp256k1(_) => { - return Err(DecodingError::encoding_unsupported("secp256k1")) - } + Inner::Secp256k1(_) => return Err(DecodingError::encoding_unsupported("secp256k1")), #[cfg(feature = "ecdsa")] - KeypairType::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), + Inner::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), }; let mut buf = Vec::with_capacity(pk.get_size()); @@ -273,7 +270,7 @@ impl Keypair { #[allow(deprecated)] Ok( ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { - keypair: KeypairType::Ed25519(kp), + keypair: Inner::Ed25519(kp), })?, ) } @@ -290,7 +287,7 @@ impl Keypair { impl From for Keypair { fn from(kp: ecdsa::Keypair) -> Self { #[allow(deprecated)] - KeypairType::Ecdsa(kp) + Inner::Ecdsa(kp) } } @@ -299,7 +296,7 @@ impl From for Keypair { fn from(kp: ed25519::Keypair) -> Self { #[allow(deprecated)] Keypair { - keypair: KeypairType::Ed25519(kp), + keypair: Inner::Ed25519(kp), } } } @@ -308,7 +305,7 @@ impl From for Keypair { impl From for Keypair { fn from(kp: secp256k1::Keypair) -> Self { #[allow(deprecated)] - KeypairType::Secp256k1(kp) + Inner::Secp256k1(kp) } } @@ -317,7 +314,7 @@ impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { #[allow(deprecated)] Keypair { - keypair: KeypairType::Rsa(kp), + keypair: Inner::Rsa(kp), } } } @@ -329,13 +326,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self.keypair { - KeypairType::Ed25519(inner) => Ok(inner), + Inner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -347,13 +344,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self { - KeypairType::Ecdsa(inner) => Ok(inner), + Inner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), } } } @@ -365,13 +362,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self { - KeypairType::Secp256k1(inner) => Ok(inner), + Inner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeypairType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "ecdsa")] - KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -383,13 +380,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { #[allow(deprecated)] match self.keypair { - KeypairType::Rsa(inner) => Ok(inner), + Inner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeypairType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - KeypairType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - KeypairType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } From 1be78fafeba6e632fb0d0508fbb2e3a045593a4c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 14:03:56 +0200 Subject: [PATCH 05/31] add ? to let ed25519_keypair --- transports/noise/src/protocol/x25519.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/transports/noise/src/protocol/x25519.rs b/transports/noise/src/protocol/x25519.rs index ce681d838b3..c88fd5b5a53 100644 --- a/transports/noise/src/protocol/x25519.rs +++ b/transports/noise/src/protocol/x25519.rs @@ -161,20 +161,16 @@ impl Keypair { /// > * [Noise: Static Key Reuse](http://www.noiseprotocol.org/noise.html#security-considerations) #[allow(unreachable_patterns)] pub fn from_identity(id_keys: &identity::Keypair) -> Option> { - match id_keys.clone().try_into_ed25519().ok() { - Some(ed25519_keypair) => { - let kp = Keypair::from(SecretKey::from_ed25519(&ed25519_keypair.secret())); - let id = KeypairIdentity { - public: id_keys.public(), - signature: None, - }; - Some(AuthenticKeypair { - keypair: kp, - identity: id, - }) - } - _ => None, - } + let ed25519_keypair = id_keys.clone().try_into_ed25519().ok()?; + let kp = Keypair::from(SecretKey::from_ed25519(&ed25519_keypair.secret())); + let id = KeypairIdentity { + public: id_keys.public(), + signature: None, + }; + Some(AuthenticKeypair { + keypair: kp, + identity: id, + }) } } From 2ae737c734afc8c5105081383bdfc7da18a54902 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 14:09:18 +0200 Subject: [PATCH 06/31] clean up and remove some deprecated warnings --- identity/src/keypair.rs | 8 -------- identity/src/lib.rs | 2 -- 2 files changed, 10 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 075eee0926e..00df3653b63 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -286,7 +286,6 @@ impl Keypair { #[cfg(feature = "ecdsa")] impl From for Keypair { fn from(kp: ecdsa::Keypair) -> Self { - #[allow(deprecated)] Inner::Ecdsa(kp) } } @@ -294,7 +293,6 @@ impl From for Keypair { #[cfg(feature = "ed25519")] impl From for Keypair { fn from(kp: ed25519::Keypair) -> Self { - #[allow(deprecated)] Keypair { keypair: Inner::Ed25519(kp), } @@ -304,7 +302,6 @@ impl From for Keypair { #[cfg(feature = "secp256k1")] impl From for Keypair { fn from(kp: secp256k1::Keypair) -> Self { - #[allow(deprecated)] Inner::Secp256k1(kp) } } @@ -312,7 +309,6 @@ impl From for Keypair { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { - #[allow(deprecated)] Keypair { keypair: Inner::Rsa(kp), } @@ -324,7 +320,6 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] match self.keypair { Inner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] @@ -342,7 +337,6 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] match self { Inner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] @@ -360,7 +354,6 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] match self { Inner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] @@ -378,7 +371,6 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] match self.keypair { Inner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 9f938552ac7..617c8531680 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -126,8 +126,6 @@ impl From<&PublicKey> for proto::PublicKey { pub use error::{DecodingError, OtherVariantError, SigningError}; pub use keypair::{Keypair, PublicKey}; -// use keypair::Keypair; -// pub use keypair::PublicKey; #[cfg(feature = "peerid")] pub use peer_id::{ParseError, PeerId}; From bf065eb23eb299f246c58e726fa407d7ceaf2b5e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 14:12:46 +0200 Subject: [PATCH 07/31] remove other deprecated warnings --- identity/src/keypair.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 00df3653b63..c6b7ead2b37 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -79,7 +79,6 @@ impl Keypair { /// Generate a new Ed25519 keypair. #[cfg(feature = "ed25519")] pub fn generate_ed25519() -> Keypair { - #[allow(deprecated)] Keypair { keypair: Inner::Ed25519(ed25519::Keypair::generate()), } @@ -88,7 +87,6 @@ impl Keypair { /// Generate a new Secp256k1 keypair. #[cfg(feature = "secp256k1")] pub fn generate_secp256k1() -> Keypair { - #[allow(deprecated)] Keypair { keypair: Inner::Secp256k1(secp256k1::Keypair::generate()), } @@ -97,7 +95,6 @@ impl Keypair { /// Generate a new ECDSA keypair. #[cfg(feature = "ecdsa")] pub fn generate_ecdsa() -> Keypair { - #[allow(deprecated)] Keypair { keypair: Inner::Ecdsa(ecdsa::Keypair::generate()), } From fdd2b51ebba01042d616bebc8761faede8483058 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 16:39:22 +0200 Subject: [PATCH 08/31] encapsulate PublicKeyType --- identity/src/keypair.rs | 180 +++++++++++++++--------- identity/src/lib.rs | 46 +++++- transports/noise/src/protocol/x25519.rs | 4 +- 3 files changed, 157 insertions(+), 73 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index c6b7ead2b37..b0f42dc86f9 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -204,16 +204,23 @@ impl Keypair { /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { use Inner::*; - #[allow(deprecated)] match &self.keypair { #[cfg(feature = "ed25519")] - Ed25519(pair) => PublicKey::Ed25519(pair.public()), + Ed25519(pair) => PublicKey { + publickey: PublicKeyType::Ed25519(pair.public()), + }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Rsa(pair) => PublicKey::Rsa(pair.public()), + Rsa(pair) => PublicKey { + publickey: PublicKeyType::Rsa(pair.public()), + }, #[cfg(feature = "secp256k1")] - Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()), + Secp256k1(pair) => PublicKey { + publickey: PublicKeyType::Secp256k1(pair.public().clone()), + }, #[cfg(feature = "ecdsa")] - Ecdsa(pair) => PublicKey::Ecdsa(pair.public().clone()), + Ecdsa(pair) => PublicKey { + publickey: PublicKeyType::Ecdsa(pair.public().clone()), + }, } } @@ -299,7 +306,9 @@ impl From for Keypair { #[cfg(feature = "secp256k1")] impl From for Keypair { fn from(kp: secp256k1::Keypair) -> Self { - Inner::Secp256k1(kp) + Keypair { + keypair: Inner::Secp256k1(kp), + } } } @@ -351,7 +360,7 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - match self { + match self.keypair { Inner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), @@ -380,40 +389,62 @@ impl TryInto for Keypair { } } -/// The public key of a node's identity keypair. +// /// The public key of a node's identity keypair. +// #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +// pub enum PublicKey { +// /// A public Ed25519 key. +// #[cfg(feature = "ed25519")] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ed25519` instead." +// )] +// Ed25519(ed25519::PublicKey), +// #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +// /// A public RSA key. + +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_rsa` instead." +// )] +// Rsa(rsa::PublicKey), +// #[cfg(feature = "secp256k1")] +// /// A public Secp256k1 key. +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_secp256k1` instead." +// )] +// Secp256k1(secp256k1::PublicKey), +// /// A public ECDSA key. +// #[cfg(feature = "ecdsa")] +// #[deprecated( +// since = "0.1.0", +// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ecdsa` instead." +// )] +// Ecdsa(ecdsa::PublicKey), +// } + #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum PublicKey { +/// The public key of a node's identity keypair. +enum PublicKeyType { /// A public Ed25519 key. #[cfg(feature = "ed25519")] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ed25519` instead." - )] Ed25519(ed25519::PublicKey), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] /// A public RSA key. - - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_rsa` instead." - )] Rsa(rsa::PublicKey), #[cfg(feature = "secp256k1")] /// A public Secp256k1 key. - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_secp256k1` instead." - )] Secp256k1(secp256k1::PublicKey), /// A public ECDSA key. #[cfg(feature = "ecdsa")] - #[deprecated( - since = "0.1.0", - note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ecdsa` instead." - )] Ecdsa(ecdsa::PublicKey), } +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct PublicKey { + publickey: PublicKeyType, +} + impl PublicKey { /// Verify a signature for a message using this public key, i.e. check /// that the signature has been produced by the corresponding @@ -421,9 +452,9 @@ impl PublicKey { /// tampered with (integrity). #[must_use] pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { - use PublicKey::*; + use PublicKeyType::*; #[allow(deprecated)] - match self { + match &self.publickey { #[cfg(feature = "ed25519")] Ed25519(pk) => pk.verify(msg, sig), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] @@ -516,7 +547,7 @@ impl PublicKey { note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead." )] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - Self::try_decode_protobuf(bytes) + Ok(Self::try_decode_protobuf(bytes)?) } /// Decode a public key from a protobuf structure, e.g. read from storage @@ -547,7 +578,11 @@ impl TryFrom for PublicKey { match pubkey.Type { #[cfg(feature = "ed25519")] proto::KeyType::Ed25519 => { - ed25519::PublicKey::decode(&pubkey.Data).map(PublicKey::Ed25519) + Ok( + ed25519::PublicKey::decode(&pubkey.Data).map(|kp| PublicKey { + publickey: PublicKeyType::Ed25519(kp), + })?, + ) } #[cfg(not(feature = "ed25519"))] proto::KeyType::Ed25519 => { @@ -555,16 +590,24 @@ impl TryFrom for PublicKey { Err(DecodingError::missing_feature("ed25519")) } #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - proto::KeyType::RSA => rsa::PublicKey::decode_x509(&pubkey.Data).map(PublicKey::Rsa), + proto::KeyType::RSA => { + Ok( + rsa::PublicKey::decode_x509(&pubkey.Data).map(|kp| PublicKey { + publickey: PublicKeyType::Rsa(kp), + })?, + ) + } #[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))] proto::KeyType::RSA => { log::debug!("support for RSA was disabled at compile-time"); Err(DecodingError::missing_feature("rsa")) } #[cfg(feature = "secp256k1")] - proto::KeyType::Secp256k1 => { - secp256k1::PublicKey::decode(&pubkey.Data).map(PublicKey::Secp256k1) - } + proto::KeyType::Secp256k1 => Ok(secp256k1::PublicKey::decode(&pubkey.Data).map( + |kp| PublicKey { + publickey: PublicKeyType::Secp256k1(kp), + }, + )?), #[cfg(not(feature = "secp256k1"))] proto::KeyType::Secp256k1 => { log::debug!("support for secp256k1 was disabled at compile-time"); @@ -572,7 +615,11 @@ impl TryFrom for PublicKey { } #[cfg(feature = "ecdsa")] proto::KeyType::ECDSA => { - ecdsa::PublicKey::decode_der(&pubkey.Data).map(PublicKey::Ecdsa) + Ok( + ecdsa::PublicKey::decode_der(&pubkey.Data).map(|kp| PublicKey { + publickey: PublicKeyType::Ecdsa(kp), + })?, + ) } #[cfg(not(feature = "ecdsa"))] proto::KeyType::ECDSA => { @@ -589,14 +636,14 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { #[allow(deprecated)] - match self { - PublicKey::Ed25519(inner) => Ok(inner), + match self.publickey { + PublicKeyType::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -606,15 +653,14 @@ impl TryInto for PublicKey { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] - match self { - PublicKey::Ecdsa(inner) => Ok(inner), + match self.publickey { + PublicKeyType::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), } } } @@ -624,15 +670,14 @@ impl TryInto for PublicKey { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] - match self { - PublicKey::Secp256k1(inner) => Ok(inner), + match self.publickey { + PublicKeyType::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "ecdsa")] - PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -642,15 +687,14 @@ impl TryInto for PublicKey { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] - match self { - PublicKey::Rsa(inner) => Ok(inner), + match self.publickey { + PublicKeyType::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -658,32 +702,36 @@ impl TryInto for PublicKey { #[cfg(feature = "ed25519")] impl From for PublicKey { fn from(key: ed25519::PublicKey) -> Self { - #[allow(deprecated)] // TODO: Remove when PublicKey::Ed25519 is made opaque - PublicKey::Ed25519(key) + PublicKey { + publickey: PublicKeyType::Ed25519(key), + } } } #[cfg(feature = "secp256k1")] impl From for PublicKey { fn from(key: secp256k1::PublicKey) -> Self { - #[allow(deprecated)] // TODO: Remove when PublicKey::Secp256k1 is made opaque - PublicKey::Secp256k1(key) + PublicKey { + publickey: PublicKeyType::Secp256k1(key), + } } } #[cfg(feature = "ecdsa")] impl From for PublicKey { fn from(key: ecdsa::PublicKey) -> Self { - #[allow(deprecated)] // TODO: Remove when PublicKey::Ecdsa is made opaque - PublicKey::Ecdsa(key) + PublicKey { + publickey: PublicKeyType::Ecdsa(key), + } } } #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] impl From for PublicKey { fn from(key: rsa::PublicKey) -> Self { - #[allow(deprecated)] // TODO: Remove when PublicKey::Rsa is made opaque - PublicKey::Rsa(key) + PublicKey { + publickey: PublicKeyType::Rsa(key), + } } } diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 617c8531680..c64ad4af392 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -98,28 +98,64 @@ impl zeroize::Zeroize for proto::PrivateKey { ))] impl From<&PublicKey> for proto::PublicKey { fn from(key: &PublicKey) -> Self { + // #[allow(deprecated)] + // match key { + // #[cfg(feature = "ed25519")] + // PublicKey::Ed25519(key) => proto::PublicKey { + // Type: proto::KeyType::Ed25519, + // Data: key.encode().to_vec(), + // }, + // #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + // PublicKey::Rsa(key) => proto::PublicKey { + // Type: proto::KeyType::RSA, + // Data: key.encode_x509(), + // }, + // #[cfg(feature = "secp256k1")] + // PublicKey::Secp256k1(key) => proto::PublicKey { + // Type: proto::KeyType::Secp256k1, + // Data: key.encode().to_vec(), + // }, + // #[cfg(feature = "ecdsa")] + // PublicKey::Ecdsa(key) => proto::PublicKey { + // Type: proto::KeyType::ECDSA, + // Data: key.encode_der(), + // }, + // } + #[allow(deprecated)] - match key { + #[cfg(any( + feature = "ed25519", + all(feature = "rsa", not(target_arch = "wasm32")), + feature = "secp256k1", + feature = "ecdsa" + ))] + match ( + key.clone().try_into_ed25519(), + key.clone().try_into_rsa(), + key.clone().try_into_secp256k1(), + key.clone().try_into_ecdsa(), + ) { #[cfg(feature = "ed25519")] - PublicKey::Ed25519(key) => proto::PublicKey { + (Ok(key), _, _, _) => proto::PublicKey { Type: proto::KeyType::Ed25519, Data: key.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKey::Rsa(key) => proto::PublicKey { + (_, Ok(key), _, _) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(), }, #[cfg(feature = "secp256k1")] - PublicKey::Secp256k1(key) => proto::PublicKey { + (_, _, Ok(key), _) => proto::PublicKey { Type: proto::KeyType::Secp256k1, Data: key.encode().to_vec(), }, #[cfg(feature = "ecdsa")] - PublicKey::Ecdsa(key) => proto::PublicKey { + (_, _, _, Ok(key)) => proto::PublicKey { Type: proto::KeyType::ECDSA, Data: key.encode_der(), }, + (Err(_), Err(_), Err(_), Err(_)) => todo!(), } } } diff --git a/transports/noise/src/protocol/x25519.rs b/transports/noise/src/protocol/x25519.rs index c88fd5b5a53..8915f56159a 100644 --- a/transports/noise/src/protocol/x25519.rs +++ b/transports/noise/src/protocol/x25519.rs @@ -127,8 +127,8 @@ impl Protocol for X25519 { #[allow(irrefutable_let_patterns)] fn linked(id_pk: &identity::PublicKey, dh_pk: &PublicKey) -> bool { - if let identity::PublicKey::Ed25519(ref p) = id_pk { - PublicKey::from_ed25519(p).as_ref() == dh_pk.as_ref() + if let Ok(p) = identity::PublicKey::try_into_ed25519(id_pk.clone()) { + PublicKey::from_ed25519(&p).as_ref() == dh_pk.as_ref() } else { false } From c649da8048f0441296484532fbffb8c97a22ff18 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 16:40:47 +0200 Subject: [PATCH 09/31] clean up --- identity/src/keypair.rs | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index b0f42dc86f9..375fad92625 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -389,40 +389,6 @@ impl TryInto for Keypair { } } -// /// The public key of a node's identity keypair. -// #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -// pub enum PublicKey { -// /// A public Ed25519 key. -// #[cfg(feature = "ed25519")] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ed25519` instead." -// )] -// Ed25519(ed25519::PublicKey), -// #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] -// /// A public RSA key. - -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_rsa` instead." -// )] -// Rsa(rsa::PublicKey), -// #[cfg(feature = "secp256k1")] -// /// A public Secp256k1 key. -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_secp256k1` instead." -// )] -// Secp256k1(secp256k1::PublicKey), -// /// A public ECDSA key. -// #[cfg(feature = "ecdsa")] -// #[deprecated( -// since = "0.1.0", -// note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ecdsa` instead." -// )] -// Ecdsa(ecdsa::PublicKey), -// } - #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] /// The public key of a node's identity keypair. enum PublicKeyType { @@ -453,7 +419,6 @@ impl PublicKey { #[must_use] pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { use PublicKeyType::*; - #[allow(deprecated)] match &self.publickey { #[cfg(feature = "ed25519")] Ed25519(pk) => pk.verify(msg, sig), @@ -635,7 +600,6 @@ impl TryInto for PublicKey { type Error = OtherVariantError; fn try_into(self) -> Result { - #[allow(deprecated)] match self.publickey { PublicKeyType::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] From c5c51256fbf2d91eb80cfea3ecaa27668be92894 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 16:46:18 +0200 Subject: [PATCH 10/31] clean up --- identity/src/keypair.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 375fad92625..58b178de2fd 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -169,9 +169,10 @@ impl Keypair { /// /// [RFC5915]: https://tools.ietf.org/html/rfc5915 #[cfg(feature = "secp256k1")] - pub fn secp256k1_from_der(der: &mut [u8]) -> Result { - #[allow(deprecated)] - secp256k1::SecretKey::from_der(der).map(|sk| Inner::Secp256k1(secp256k1::Keypair::from(sk))) + pub fn secp256k1_from_der(der: &mut [u8]) -> Result { + Ok(secp256k1::SecretKey::from_der(der).map(|sk| Keypair { + keypair: Inner::Secp256k1(secp256k1::Keypair::from(sk)), + })?) } #[cfg(feature = "ed25519")] @@ -188,7 +189,6 @@ impl Keypair { /// a signature that can be verified using the corresponding public key. pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { use Inner::*; - #[allow(deprecated)] match self.keypair { #[cfg(feature = "ed25519")] Ed25519(ref pair) => Ok(pair.sign(msg)), From f4b172fde3e79d9ae4618df6817a62b3f8bb243e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 16:49:42 +0200 Subject: [PATCH 11/31] move doc from private to public struct --- identity/src/keypair.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 58b178de2fd..3258274d10a 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -36,11 +36,6 @@ use crate::secp256k1; #[cfg(feature = "ecdsa")] use crate::ecdsa; -#[derive(Debug, Clone)] -pub struct Keypair { - keypair: Inner, -} - /// Identity keypair of a node. /// /// # Example: Generating RSA keys with OpenSSL @@ -58,6 +53,11 @@ pub struct Keypair { /// let keypair = Keypair::rsa_from_pkcs8(&mut bytes); /// ``` /// +#[derive(Debug, Clone)] +pub struct Keypair { + keypair: Inner, +} + #[derive(Debug, Clone)] #[allow(clippy::large_enum_variant)] enum Inner { @@ -390,7 +390,6 @@ impl TryInto for Keypair { } #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -/// The public key of a node's identity keypair. enum PublicKeyType { /// A public Ed25519 key. #[cfg(feature = "ed25519")] @@ -406,6 +405,7 @@ enum PublicKeyType { Ecdsa(ecdsa::PublicKey), } +/// The public key of a node's identity keypair. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct PublicKey { publickey: PublicKeyType, From 5c7d3cb66d53f85af432e24e0bec7a561585703e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 4 May 2023 16:52:17 +0200 Subject: [PATCH 12/31] remove keypair_dummy.rs --- identity/src/keypair_dummy.rs | 69 ----------------------------------- identity/src/lib.rs | 1 - 2 files changed, 70 deletions(-) delete mode 100644 identity/src/keypair_dummy.rs diff --git a/identity/src/keypair_dummy.rs b/identity/src/keypair_dummy.rs deleted file mode 100644 index 4e1bb61f15c..00000000000 --- a/identity/src/keypair_dummy.rs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2023 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -use crate::error::{DecodingError, SigningError}; - -#[derive(Debug, Clone)] -pub enum Keypair {} - -impl Keypair { - pub fn sign(&self, _: &[u8]) -> Result, SigningError> { - unreachable!("Can never construct empty enum") - } - - pub fn public(&self) -> PublicKey { - unreachable!("Can never construct empty enum") - } - - pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { - unreachable!("Can never construct empty enum") - } - - pub fn from_protobuf_encoding(_: &[u8]) -> Result { - Err(DecodingError::missing_feature( - "ecdsa|rsa|ed25519|secp256k1", - )) - } -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum PublicKey {} - -impl PublicKey { - #[must_use] - pub fn verify(&self, _: &[u8], _: &[u8]) -> bool { - unreachable!("Can never construct empty enum") - } - - pub fn to_protobuf_encoding(&self) -> Vec { - unreachable!("Can never construct empty enum") - } - - pub fn from_protobuf_encoding(_: &[u8]) -> Result { - Err(DecodingError::missing_feature( - "ecdsa|rsa|ed25519|secp256k1", - )) - } - - #[cfg(feature = "peerid")] - pub fn to_peer_id(&self) -> crate::PeerId { - unreachable!("Can never construct empty enum") - } -} diff --git a/identity/src/lib.rs b/identity/src/lib.rs index c64ad4af392..aef70da9230 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -73,7 +73,6 @@ mod keypair; not(feature = "ed25519"), not(feature = "rsa") ))] -#[path = "./keypair_dummy.rs"] mod keypair; #[cfg(feature = "peerid")] mod peer_id; From 894d22bc8fba2b7ffddd926b2cb409e61fbaee54 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 00:50:10 +0200 Subject: [PATCH 13/31] rename Inner to KeyPairInner and PublicKeyType to PublicKeyInner --- identity/src/keypair.rs | 134 ++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 66 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index c385724581e..f88c04e510f 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -55,12 +55,12 @@ use crate::ecdsa; /// #[derive(Debug, Clone)] pub struct Keypair { - keypair: Inner, + keypair: KeyPairInner, } #[derive(Debug, Clone)] #[allow(clippy::large_enum_variant)] -enum Inner { +enum KeyPairInner { /// An Ed25519 keypair. #[cfg(feature = "ed25519")] Ed25519(ed25519::Keypair), @@ -80,7 +80,7 @@ impl Keypair { #[cfg(feature = "ed25519")] pub fn generate_ed25519() -> Keypair { Keypair { - keypair: Inner::Ed25519(ed25519::Keypair::generate()), + keypair: KeyPairInner::Ed25519(ed25519::Keypair::generate()), } } @@ -88,7 +88,7 @@ impl Keypair { #[cfg(feature = "secp256k1")] pub fn generate_secp256k1() -> Keypair { Keypair { - keypair: Inner::Secp256k1(secp256k1::Keypair::generate()), + keypair: KeyPairInner::Secp256k1(secp256k1::Keypair::generate()), } } @@ -96,7 +96,7 @@ impl Keypair { #[cfg(feature = "ecdsa")] pub fn generate_ecdsa() -> Keypair { Keypair { - keypair: Inner::Ecdsa(ecdsa::Keypair::generate()), + keypair: KeyPairInner::Ecdsa(ecdsa::Keypair::generate()), } } @@ -160,7 +160,7 @@ impl Keypair { pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { #[allow(deprecated)] Ok(rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { - keypair: Inner::Rsa(kp), + keypair: KeyPairInner::Rsa(kp), })?) } @@ -171,7 +171,7 @@ impl Keypair { #[cfg(feature = "secp256k1")] pub fn secp256k1_from_der(der: &mut [u8]) -> Result { Ok(secp256k1::SecretKey::from_der(der).map(|sk| Keypair { - keypair: Inner::Secp256k1(secp256k1::Keypair::from(sk)), + keypair: KeyPairInner::Secp256k1(secp256k1::Keypair::from(sk)), })?) } @@ -179,7 +179,7 @@ impl Keypair { pub fn ed25519_from_bytes(bytes: impl AsMut<[u8]>) -> Result { #[allow(deprecated)] Ok(Keypair { - keypair: Inner::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( + keypair: KeyPairInner::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( bytes, )?)), }) @@ -188,7 +188,7 @@ impl Keypair { /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { - use Inner::*; + use KeyPairInner::*; match self.keypair { #[cfg(feature = "ed25519")] Ed25519(ref pair) => Ok(pair.sign(msg)), @@ -203,23 +203,23 @@ impl Keypair { /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { - use Inner::*; + use KeyPairInner::*; match &self.keypair { #[cfg(feature = "ed25519")] Ed25519(pair) => PublicKey { - publickey: PublicKeyType::Ed25519(pair.public()), + publickey: PublicKeyInner::Ed25519(pair.public()), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] Rsa(pair) => PublicKey { - publickey: PublicKeyType::Rsa(pair.public()), + publickey: PublicKeyInner::Rsa(pair.public()), }, #[cfg(feature = "secp256k1")] Secp256k1(pair) => PublicKey { - publickey: PublicKeyType::Secp256k1(pair.public().clone()), + publickey: PublicKeyInner::Secp256k1(pair.public().clone()), }, #[cfg(feature = "ecdsa")] Ecdsa(pair) => PublicKey { - publickey: PublicKeyType::Ecdsa(pair.public().clone()), + publickey: PublicKeyInner::Ecdsa(pair.public().clone()), }, } } @@ -238,16 +238,18 @@ impl Keypair { #[allow(deprecated)] let pk: proto::PrivateKey = match &self.keypair { #[cfg(feature = "ed25519")] - Inner::Ed25519(data) => proto::PrivateKey { + KeyPairInner::Ed25519(data) => proto::PrivateKey { Type: proto::KeyType::Ed25519, Data: data.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Inner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), + KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] - Inner::Secp256k1(_) => return Err(DecodingError::encoding_unsupported("secp256k1")), + KeyPairInner::Secp256k1(_) => { + return Err(DecodingError::encoding_unsupported("secp256k1")) + } #[cfg(feature = "ecdsa")] - Inner::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), + KeyPairInner::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")), }; let mut buf = Vec::with_capacity(pk.get_size()); @@ -274,7 +276,7 @@ impl Keypair { #[allow(deprecated)] Ok( ed25519::Keypair::decode(&mut private_key.Data).map(|kp| Keypair { - keypair: Inner::Ed25519(kp), + keypair: KeyPairInner::Ed25519(kp), })?, ) } @@ -290,7 +292,7 @@ impl Keypair { #[cfg(feature = "ecdsa")] impl From for Keypair { fn from(kp: ecdsa::Keypair) -> Self { - Inner::Ecdsa(kp) + KeyPairInner::Ecdsa(kp) } } @@ -298,7 +300,7 @@ impl From for Keypair { impl From for Keypair { fn from(kp: ed25519::Keypair) -> Self { Keypair { - keypair: Inner::Ed25519(kp), + keypair: KeyPairInner::Ed25519(kp), } } } @@ -307,7 +309,7 @@ impl From for Keypair { impl From for Keypair { fn from(kp: secp256k1::Keypair) -> Self { Keypair { - keypair: Inner::Secp256k1(kp), + keypair: KeyPairInner::Secp256k1(kp), } } } @@ -316,7 +318,7 @@ impl From for Keypair { impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { Keypair { - keypair: Inner::Rsa(kp), + keypair: KeyPairInner::Rsa(kp), } } } @@ -327,13 +329,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { - Inner::Ed25519(inner) => Ok(inner), + KeyPairInner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -344,13 +346,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self { - Inner::Ecdsa(inner) => Ok(inner), + KeyPairInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), } } } @@ -361,13 +363,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { - Inner::Secp256k1(inner) => Ok(inner), + KeyPairInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Inner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "ecdsa")] - Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -378,19 +380,19 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { - Inner::Rsa(inner) => Ok(inner), + KeyPairInner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - Inner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - Inner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - Inner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -enum PublicKeyType { +enum PublicKeyInner { /// A public Ed25519 key. #[cfg(feature = "ed25519")] Ed25519(ed25519::PublicKey), @@ -408,7 +410,7 @@ enum PublicKeyType { /// The public key of a node's identity keypair. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct PublicKey { - publickey: PublicKeyType, + publickey: PublicKeyInner, } impl PublicKey { @@ -418,7 +420,7 @@ impl PublicKey { /// tampered with (integrity). #[must_use] pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { - use PublicKeyType::*; + use PublicKeyInner::*; match &self.publickey { #[cfg(feature = "ed25519")] Ed25519(pk) => pk.verify(msg, sig), @@ -545,7 +547,7 @@ impl TryFrom for PublicKey { proto::KeyType::Ed25519 => { Ok( ed25519::PublicKey::decode(&pubkey.Data).map(|kp| PublicKey { - publickey: PublicKeyType::Ed25519(kp), + publickey: PublicKeyInner::Ed25519(kp), })?, ) } @@ -558,7 +560,7 @@ impl TryFrom for PublicKey { proto::KeyType::RSA => { Ok( rsa::PublicKey::decode_x509(&pubkey.Data).map(|kp| PublicKey { - publickey: PublicKeyType::Rsa(kp), + publickey: PublicKeyInner::Rsa(kp), })?, ) } @@ -570,7 +572,7 @@ impl TryFrom for PublicKey { #[cfg(feature = "secp256k1")] proto::KeyType::Secp256k1 => Ok(secp256k1::PublicKey::decode(&pubkey.Data).map( |kp| PublicKey { - publickey: PublicKeyType::Secp256k1(kp), + publickey: PublicKeyInner::Secp256k1(kp), }, )?), #[cfg(not(feature = "secp256k1"))] @@ -582,7 +584,7 @@ impl TryFrom for PublicKey { proto::KeyType::ECDSA => { Ok( ecdsa::PublicKey::decode_der(&pubkey.Data).map(|kp| PublicKey { - publickey: PublicKeyType::Ecdsa(kp), + publickey: PublicKeyInner::Ecdsa(kp), })?, ) } @@ -601,13 +603,13 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { - PublicKeyType::Ed25519(inner) => Ok(inner), + PublicKeyInner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -618,13 +620,13 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { - PublicKeyType::Ecdsa(inner) => Ok(inner), + PublicKeyInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), } } } @@ -635,13 +637,13 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { - PublicKeyType::Secp256k1(inner) => Ok(inner), + PublicKeyInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyType::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), #[cfg(feature = "ecdsa")] - PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -652,13 +654,13 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { - PublicKeyType::Rsa(inner) => Ok(inner), + PublicKeyInner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyType::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - PublicKeyType::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKeyType::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), } } } @@ -667,7 +669,7 @@ impl TryInto for PublicKey { impl From for PublicKey { fn from(key: ed25519::PublicKey) -> Self { PublicKey { - publickey: PublicKeyType::Ed25519(key), + publickey: PublicKeyInner::Ed25519(key), } } } @@ -676,7 +678,7 @@ impl From for PublicKey { impl From for PublicKey { fn from(key: secp256k1::PublicKey) -> Self { PublicKey { - publickey: PublicKeyType::Secp256k1(key), + publickey: PublicKeyInner::Secp256k1(key), } } } @@ -685,7 +687,7 @@ impl From for PublicKey { impl From for PublicKey { fn from(key: ecdsa::PublicKey) -> Self { PublicKey { - publickey: PublicKeyType::Ecdsa(key), + publickey: PublicKeyInner::Ecdsa(key), } } } @@ -694,7 +696,7 @@ impl From for PublicKey { impl From for PublicKey { fn from(key: rsa::PublicKey) -> Self { PublicKey { - publickey: PublicKeyType::Rsa(key), + publickey: PublicKeyInner::Rsa(key), } } } From d96e3f5626f34f2ea597639b09ae56a205456115 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 01:08:08 +0200 Subject: [PATCH 14/31] pub(crate) publickey --- identity/src/keypair.rs | 46 ++++++++++++++++++++-------------------- identity/src/lib.rs | 47 ++++++----------------------------------- 2 files changed, 29 insertions(+), 64 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index f88c04e510f..8b579aa8247 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -331,11 +331,11 @@ impl TryInto for Keypair { match self.keypair { KeyPairInner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } @@ -348,11 +348,11 @@ impl TryInto for Keypair { match self { KeyPairInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), } } } @@ -365,11 +365,11 @@ impl TryInto for Keypair { match self.keypair { KeyPairInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeyPairInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } @@ -382,17 +382,17 @@ impl TryInto for Keypair { match self.keypair { KeyPairInner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -enum PublicKeyInner { +pub enum PublicKeyInner { /// A public Ed25519 key. #[cfg(feature = "ed25519")] Ed25519(ed25519::PublicKey), @@ -410,7 +410,7 @@ enum PublicKeyInner { /// The public key of a node's identity keypair. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct PublicKey { - publickey: PublicKeyInner, + pub(crate) publickey: PublicKeyInner, } impl PublicKey { @@ -605,11 +605,11 @@ impl TryInto for PublicKey { match self.publickey { PublicKeyInner::Ed25519(inner) => Ok(inner), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } @@ -639,11 +639,11 @@ impl TryInto for PublicKey { match self.publickey { PublicKeyInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } @@ -656,11 +656,11 @@ impl TryInto for PublicKey { match self.publickey { PublicKeyInner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(feature = "secp256k1")] - PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), } } } diff --git a/identity/src/lib.rs b/identity/src/lib.rs index aef70da9230..51997b96be9 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -97,69 +97,34 @@ impl zeroize::Zeroize for proto::PrivateKey { ))] impl From<&PublicKey> for proto::PublicKey { fn from(key: &PublicKey) -> Self { - // #[allow(deprecated)] - // match key { - // #[cfg(feature = "ed25519")] - // PublicKey::Ed25519(key) => proto::PublicKey { - // Type: proto::KeyType::Ed25519, - // Data: key.encode().to_vec(), - // }, - // #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - // PublicKey::Rsa(key) => proto::PublicKey { - // Type: proto::KeyType::RSA, - // Data: key.encode_x509(), - // }, - // #[cfg(feature = "secp256k1")] - // PublicKey::Secp256k1(key) => proto::PublicKey { - // Type: proto::KeyType::Secp256k1, - // Data: key.encode().to_vec(), - // }, - // #[cfg(feature = "ecdsa")] - // PublicKey::Ecdsa(key) => proto::PublicKey { - // Type: proto::KeyType::ECDSA, - // Data: key.encode_der(), - // }, - // } - #[allow(deprecated)] - #[cfg(any( - feature = "ed25519", - all(feature = "rsa", not(target_arch = "wasm32")), - feature = "secp256k1", - feature = "ecdsa" - ))] - match ( - key.clone().try_into_ed25519(), - key.clone().try_into_rsa(), - key.clone().try_into_secp256k1(), - key.clone().try_into_ecdsa(), - ) { + match &key.publickey { #[cfg(feature = "ed25519")] - (Ok(key), _, _, _) => proto::PublicKey { + PublicKeyInner::Ed25519(key) => proto::PublicKey { Type: proto::KeyType::Ed25519, Data: key.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - (_, Ok(key), _, _) => proto::PublicKey { + PublicKeyInner::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(), }, #[cfg(feature = "secp256k1")] - (_, _, Ok(key), _) => proto::PublicKey { + PublicKeyInner::Secp256k1(key) => proto::PublicKey { Type: proto::KeyType::Secp256k1, Data: key.encode().to_vec(), }, #[cfg(feature = "ecdsa")] - (_, _, _, Ok(key)) => proto::PublicKey { + PublicKeyInner::Ecdsa(key) => proto::PublicKey { Type: proto::KeyType::ECDSA, Data: key.encode_der(), }, - (Err(_), Err(_), Err(_), Err(_)) => todo!(), } } } pub use error::{DecodingError, OtherVariantError, SigningError}; +use keypair::PublicKeyInner; pub use keypair::{Keypair, PublicKey}; #[cfg(feature = "peerid")] From 9d4fe3dc6edc1ac5725375898a005ea2cd298cf6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 01:11:34 +0200 Subject: [PATCH 15/31] keypair module def unconditional --- identity/src/lib.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 51997b96be9..bd3e90a786f 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -60,19 +60,6 @@ pub mod rsa; pub mod secp256k1; mod error; -#[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" -))] -mod keypair; -#[cfg(all( - not(feature = "ecdsa"), - not(feature = "secp256k1"), - not(feature = "ed25519"), - not(feature = "rsa") -))] mod keypair; #[cfg(feature = "peerid")] mod peer_id; From 1eb59bb9d30c9f9d9331e58b42c74b5fc8645454 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 01:17:08 +0200 Subject: [PATCH 16/31] pub(crate) enum PublicKeyInner --- identity/src/keypair.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 8b579aa8247..4ccc8a6b481 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -392,7 +392,7 @@ impl TryInto for Keypair { } #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum PublicKeyInner { +pub(crate) enum PublicKeyInner { /// A public Ed25519 key. #[cfg(feature = "ed25519")] Ed25519(ed25519::PublicKey), From ab11cd15a0ce803a3669bf5afae5f073ea2e8bb0 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 01:34:02 +0200 Subject: [PATCH 17/31] clean up after merge --- identity/src/keypair.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index baf7cbb9aa9..07f8b025998 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -268,8 +268,9 @@ impl Keypair { match private_key.Type { proto::KeyType::Ed25519 => { #[cfg(feature = "ed25519")] - return ed25519::Keypair::try_from_bytes(&mut private_key.Data) - .map(Keypair::Ed25519); + return ed25519::Keypair::try_from_bytes(&mut private_key.Data).map(|sk| Keypair { + keypair: KeyPairInner::Ed25519(sk), + }); Err(DecodingError::missing_feature("ed25519")) } proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), From e1dcbdb87b125aab5e87c6675ddb914b28b504d4 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 08:38:28 +0200 Subject: [PATCH 18/31] clean up --- identity/src/keypair.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 07f8b025998..6ed2f034dae 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -277,8 +277,12 @@ impl Keypair { proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), proto::KeyType::ECDSA => { #[cfg(feature = "ecdsa")] - return ecdsa::SecretKey::try_decode_der(&mut private_key.Data) - .map(|key| Keypair::Ecdsa(key.into())); + return ecdsa::SecretKey::try_decode_der(&mut private_key.Data).map(|key| { + Keypair { + keypair: KeyPairInner::Ecdsa(key.into()), + } + }); + Err(DecodingError::missing_feature("ecdsa")) } } @@ -288,7 +292,9 @@ impl Keypair { #[cfg(feature = "ecdsa")] impl From for Keypair { fn from(kp: ecdsa::Keypair) -> Self { - KeyPairInner::Ecdsa(kp) + Keypair { + keypair: KeyPairInner::Ecdsa(kp), + } } } @@ -341,7 +347,7 @@ impl TryInto for Keypair { type Error = OtherVariantError; fn try_into(self) -> Result { - match self { + match self.keypair { KeyPairInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), @@ -618,11 +624,11 @@ impl TryInto for PublicKey { match self.publickey { PublicKeyInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] - PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] - PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), } } } From 2e6baef5e939bef8e63635804cad7a9bceb49f99 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 08:50:55 +0200 Subject: [PATCH 19/31] clean up useless ok()? --- identity/src/keypair.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 6ed2f034dae..65d6a766397 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -159,9 +159,9 @@ impl Keypair { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { #[allow(deprecated)] - Ok(rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { + rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { keypair: KeyPairInner::Rsa(kp), - })?) + }) } /// Decode a keypair from a DER-encoded Secp256k1 secret key in an ECPrivateKey @@ -170,9 +170,9 @@ impl Keypair { /// [RFC5915]: https://tools.ietf.org/html/rfc5915 #[cfg(feature = "secp256k1")] pub fn secp256k1_from_der(der: &mut [u8]) -> Result { - Ok(secp256k1::SecretKey::from_der(der).map(|sk| Keypair { + secp256k1::SecretKey::from_der(der).map(|sk| Keypair { keypair: KeyPairInner::Secp256k1(secp256k1::Keypair::from(sk)), - })?) + }) } #[cfg(feature = "ed25519")] @@ -516,7 +516,7 @@ impl PublicKey { note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead." )] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - Ok(Self::try_decode_protobuf(bytes)?) + Self::try_decode_protobuf(bytes) } /// Decode a public key from a protobuf structure, e.g. read from storage From e852a0c3c241c0d348c1486df651bc72c70e3581 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 09:27:29 +0200 Subject: [PATCH 20/31] clean up cfg --- identity/src/keypair.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 65d6a766397..37a6c77cb5e 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -20,6 +20,12 @@ use crate::error::OtherVariantError; use crate::error::{DecodingError, SigningError}; +#[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" +))] use crate::proto; use quick_protobuf::{BytesReader, Writer}; use std::convert::TryFrom; @@ -225,6 +231,12 @@ impl Keypair { } /// Encode a private key as protobuf structure. + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { use quick_protobuf::MessageWrite; @@ -256,6 +268,12 @@ impl Keypair { } /// Decode a private key from a protobuf structure and parse it as a [`Keypair`]. + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { use quick_protobuf::MessageRead; @@ -490,12 +508,24 @@ impl PublicKey { /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. #[deprecated(note = "Renamed to `PublicKey::encode_protobuf`.")] + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn to_protobuf_encoding(&self) -> Vec { Self::encode_protobuf(self) } /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn encode_protobuf(&self) -> Vec { use quick_protobuf::MessageWrite; @@ -515,12 +545,24 @@ impl PublicKey { #[deprecated( note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead." )] + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { Self::try_decode_protobuf(bytes) } /// Decode a public key from a protobuf structure, e.g. read from storage /// or received from another node. + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] pub fn try_decode_protobuf(bytes: &[u8]) -> Result { use quick_protobuf::MessageRead; @@ -539,6 +581,12 @@ impl PublicKey { } } +#[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" +))] impl TryFrom for PublicKey { type Error = DecodingError; From d9969f945db13625a83b1bb1c2af2a2c05117050 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 11:45:39 +0200 Subject: [PATCH 21/31] remove all allow(deprecated) in keypair.rs --- identity/src/keypair.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 37a6c77cb5e..506db330ed2 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -164,8 +164,7 @@ impl Keypair { /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { - #[allow(deprecated)] - rsa::Keypair::from_pkcs8(pkcs8_der).map(|kp| Keypair { + rsa::Keypair::try_decode_pkcs8(pkcs8_der).map(|kp| Keypair { keypair: KeyPairInner::Rsa(kp), }) } @@ -183,11 +182,10 @@ impl Keypair { #[cfg(feature = "ed25519")] pub fn ed25519_from_bytes(bytes: impl AsMut<[u8]>) -> Result { - #[allow(deprecated)] Ok(Keypair { - keypair: KeyPairInner::Ed25519(ed25519::Keypair::from(ed25519::SecretKey::from_bytes( - bytes, - )?)), + keypair: KeyPairInner::Ed25519(ed25519::Keypair::from( + ed25519::SecretKey::try_from_bytes(bytes)?, + )), }) } @@ -240,7 +238,6 @@ impl Keypair { pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { use quick_protobuf::MessageWrite; - #[allow(deprecated)] let pk: proto::PrivateKey = match &self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(data) => proto::PrivateKey { @@ -591,16 +588,13 @@ impl TryFrom for PublicKey { type Error = DecodingError; fn try_from(pubkey: proto::PublicKey) -> Result { - #[allow(deprecated)] match pubkey.Type { #[cfg(feature = "ed25519")] - proto::KeyType::Ed25519 => { - Ok( - ed25519::PublicKey::decode(&pubkey.Data).map(|kp| PublicKey { - publickey: PublicKeyInner::Ed25519(kp), - })?, - ) - } + proto::KeyType::Ed25519 => Ok(ed25519::PublicKey::try_from_bytes(&pubkey.Data).map( + |kp| PublicKey { + publickey: PublicKeyInner::Ed25519(kp), + }, + )?), #[cfg(not(feature = "ed25519"))] proto::KeyType::Ed25519 => { log::debug!("support for ed25519 was disabled at compile-time"); @@ -609,7 +603,7 @@ impl TryFrom for PublicKey { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] proto::KeyType::RSA => { Ok( - rsa::PublicKey::decode_x509(&pubkey.Data).map(|kp| PublicKey { + rsa::PublicKey::try_decode_x509(&pubkey.Data).map(|kp| PublicKey { publickey: PublicKeyInner::Rsa(kp), })?, ) From a0d9267c7de87b82f878172045c711e2c43a30de Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 11:48:02 +0200 Subject: [PATCH 22/31] add changelog --- identity/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 9aacfac822d..45a1abd6ea8 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -5,8 +5,12 @@ - Add support for exporting and importing ECDSA keys via the libp2p [protobuf format]. See [PR 3863]. +- Make `Keypair` and `Publickey` opaque. + See [PR 3866]. + [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 [PR 3863]: https://github.com/libp2p/rust-libp2p/pull/3863 +[PR 3866]: https://github.com/libp2p/rust-libp2p/pull/3866 [protobuf format]: https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#keys ## 0.1.2 From c3bd310d56d6f449ff166d7a5360cc45be556eac Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 14:27:41 +0200 Subject: [PATCH 23/31] clean up and fix quick_protobuf dependencies --- identity/Cargo.toml | 10 +- identity/src/keypair.rs | 224 +++++++++++++++++++++++----------------- identity/src/lib.rs | 9 +- 3 files changed, 136 insertions(+), 107 deletions(-) diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 4a17e806554..d8a453cd5c4 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -20,7 +20,7 @@ log = "0.4" multiaddr = { version = "0.17.1", optional = true } multihash = { version = "0.17.0", default-features = false, features = ["std"], optional = true } p256 = { version = "0.13", default-features = false, features = ["ecdsa", "std", "pem"], optional = true } -quick-protobuf = { version = "0.8.1", optional = true } +quick-protobuf = "0.8.1" rand = { version = "0.8", optional = true } sec1 = { version = "0.7", default-features = false, optional = true } serde = { version = "1", optional = true, features = ["derive"] } @@ -33,10 +33,10 @@ zeroize = { version = "1.6", optional = true } ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false, optional = true} [features] -secp256k1 = [ "dep:libsecp256k1", "dep:asn1_der", "dep:rand", "dep:sha2", "dep:zeroize", "dep:quick-protobuf" ] -ecdsa = [ "dep:p256", "dep:rand", "dep:void", "dep:zeroize", "dep:sec1", "dep:quick-protobuf" ] -rsa = [ "dep:ring", "dep:asn1_der", "dep:rand", "dep:zeroize", "dep:quick-protobuf" ] -ed25519 = [ "dep:ed25519-dalek", "dep:rand", "dep:zeroize", "dep:quick-protobuf" ] +secp256k1 = [ "dep:libsecp256k1", "dep:asn1_der", "dep:rand", "dep:sha2", "dep:zeroize" ] +ecdsa = [ "dep:p256", "dep:rand", "dep:void", "dep:zeroize", "dep:sec1" ] +rsa = [ "dep:ring", "dep:asn1_der", "dep:rand", "dep:zeroize" ] +ed25519 = [ "dep:ed25519-dalek", "dep:rand", "dep:zeroize" ] peerid = [ "dep:multihash", "dep:multiaddr", "dep:bs58", "dep:rand", "dep:thiserror", "dep:sha2" ] [dev-dependencies] diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 506db330ed2..9a3cdc57f4c 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -208,7 +208,7 @@ impl Keypair { /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { use KeyPairInner::*; - match &self.keypair { + match self.keypair { #[cfg(feature = "ed25519")] Ed25519(pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), @@ -229,78 +229,100 @@ impl Keypair { } /// Encode a private key as protobuf structure. - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { use quick_protobuf::MessageWrite; - let pk: proto::PrivateKey = match &self.keypair { - #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(data) => proto::PrivateKey { - Type: proto::KeyType::Ed25519, - Data: data.to_bytes().to_vec(), - }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), - #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(_) => { - return Err(DecodingError::encoding_unsupported("secp256k1")) - } - #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(data) => proto::PrivateKey { - Type: proto::KeyType::ECDSA, - Data: data.secret().encode_der(), - }, - }; + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] + { + let pk: proto::PrivateKey = match &self.keypair { + #[cfg(feature = "ed25519")] + KeyPairInner::Ed25519(data) => proto::PrivateKey { + Type: proto::KeyType::Ed25519, + Data: data.to_bytes().to_vec(), + }, + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), + #[cfg(feature = "secp256k1")] + KeyPairInner::Secp256k1(_) => { + return Err(DecodingError::encoding_unsupported("secp256k1")) + } + #[cfg(feature = "ecdsa")] + KeyPairInner::Ecdsa(data) => proto::PrivateKey { + Type: proto::KeyType::ECDSA, + Data: data.secret().encode_der(), + }, + }; - let mut buf = Vec::with_capacity(pk.get_size()); - let mut writer = Writer::new(&mut buf); - pk.write_message(&mut writer).expect("Encoding to succeed"); + let mut buf = Vec::with_capacity(pk.get_size()); + let mut writer = Writer::new(&mut buf); + pk.write_message(&mut writer).expect("Encoding to succeed"); - Ok(buf) + Ok(buf) + } + + #[cfg(not(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + )))] + unreachable!() } /// Decode a private key from a protobuf structure and parse it as a [`Keypair`]. - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { use quick_protobuf::MessageRead; - let mut reader = BytesReader::from_bytes(bytes); - let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) - .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) - .map(zeroize::Zeroizing::new)?; - - #[allow(deprecated, unreachable_code)] - match private_key.Type { - proto::KeyType::Ed25519 => { - #[cfg(feature = "ed25519")] - return ed25519::Keypair::try_from_bytes(&mut private_key.Data).map(|sk| Keypair { - keypair: KeyPairInner::Ed25519(sk), - }); - Err(DecodingError::missing_feature("ed25519")) - } - proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), - proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), - proto::KeyType::ECDSA => { - #[cfg(feature = "ecdsa")] - return ecdsa::SecretKey::try_decode_der(&mut private_key.Data).map(|key| { - Keypair { - keypair: KeyPairInner::Ecdsa(key.into()), - } - }); - - Err(DecodingError::missing_feature("ecdsa")) + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] + { + let mut reader = BytesReader::from_bytes(bytes); + let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) + .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) + .map(zeroize::Zeroizing::new)?; + + #[allow(deprecated, unreachable_code)] + match private_key.Type { + proto::KeyType::Ed25519 => { + #[cfg(feature = "ed25519")] + return ed25519::Keypair::try_from_bytes(&mut private_key.Data).map(|sk| { + Keypair { + keypair: KeyPairInner::Ed25519(sk), + } + }); + Err(DecodingError::missing_feature("ed25519")) + } + proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), + proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), + proto::KeyType::ECDSA => { + #[cfg(feature = "ecdsa")] + return ecdsa::SecretKey::try_decode_der(&mut private_key.Data).map(|key| { + Keypair { + keypair: KeyPairInner::Ecdsa(key.into()), + } + }); + + Err(DecodingError::missing_feature("ecdsa")) + } } } + + #[cfg(not(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + )))] + unreachable!() } } @@ -438,7 +460,7 @@ impl PublicKey { #[must_use] pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { use PublicKeyInner::*; - match &self.publickey { + match self.publickey { #[cfg(feature = "ed25519")] Ed25519(pk) => pk.verify(msg, sig), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] @@ -505,36 +527,40 @@ impl PublicKey { /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. #[deprecated(note = "Renamed to `PublicKey::encode_protobuf`.")] - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn to_protobuf_encoding(&self) -> Vec { Self::encode_protobuf(self) } /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn encode_protobuf(&self) -> Vec { use quick_protobuf::MessageWrite; - let public_key = proto::PublicKey::from(self); - - let mut buf = Vec::with_capacity(public_key.get_size()); - let mut writer = Writer::new(&mut buf); - public_key - .write_message(&mut writer) - .expect("Encoding to succeed"); + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] + { + let public_key = proto::PublicKey::from(self); + + let mut buf = Vec::with_capacity(public_key.get_size()); + let mut writer = Writer::new(&mut buf); + public_key + .write_message(&mut writer) + .expect("Encoding to succeed"); + + buf + } - buf + #[cfg(not(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + )))] + unreachable!() } /// Decode a public key from a protobuf structure, e.g. read from storage @@ -542,33 +568,37 @@ impl PublicKey { #[deprecated( note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead." )] - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { Self::try_decode_protobuf(bytes) } /// Decode a public key from a protobuf structure, e.g. read from storage /// or received from another node. - #[cfg(any( - feature = "ecdsa", - feature = "secp256k1", - feature = "ed25519", - feature = "rsa" - ))] pub fn try_decode_protobuf(bytes: &[u8]) -> Result { use quick_protobuf::MessageRead; - let mut reader = BytesReader::from_bytes(bytes); + #[cfg(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + ))] + { + let mut reader = BytesReader::from_bytes(bytes); + + let pubkey = proto::PublicKey::from_reader(&mut reader, bytes) + .map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?; - let pubkey = proto::PublicKey::from_reader(&mut reader, bytes) - .map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?; + pubkey.try_into() + } - pubkey.try_into() + #[cfg(not(any( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa" + )))] + unreachable!() } /// Convert the `PublicKey` into the corresponding `PeerId`. diff --git a/identity/src/lib.rs b/identity/src/lib.rs index bd3e90a786f..5d562576dab 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -87,22 +87,22 @@ impl From<&PublicKey> for proto::PublicKey { #[allow(deprecated)] match &key.publickey { #[cfg(feature = "ed25519")] - PublicKeyInner::Ed25519(key) => proto::PublicKey { + keypair::PublicKeyInner::Ed25519(key) => proto::PublicKey { Type: proto::KeyType::Ed25519, Data: key.encode().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyInner::Rsa(key) => proto::PublicKey { + keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(), }, #[cfg(feature = "secp256k1")] - PublicKeyInner::Secp256k1(key) => proto::PublicKey { + keypair::PublicKeyInner::Secp256k1(key) => proto::PublicKey { Type: proto::KeyType::Secp256k1, Data: key.encode().to_vec(), }, #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(key) => proto::PublicKey { + keypair::PublicKeyInner::Ecdsa(key) => proto::PublicKey { Type: proto::KeyType::ECDSA, Data: key.encode_der(), }, @@ -111,7 +111,6 @@ impl From<&PublicKey> for proto::PublicKey { } pub use error::{DecodingError, OtherVariantError, SigningError}; -use keypair::PublicKeyInner; pub use keypair::{Keypair, PublicKey}; #[cfg(feature = "peerid")] From a6e45ec5b0233d223e4395bd4908f475f0325d8e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 15:04:13 +0200 Subject: [PATCH 24/31] fix self.keypair --- identity/src/keypair.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 9a3cdc57f4c..6032df06e3c 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -210,19 +210,19 @@ impl Keypair { use KeyPairInner::*; match self.keypair { #[cfg(feature = "ed25519")] - Ed25519(pair) => PublicKey { + Ed25519(ref pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Rsa(pair) => PublicKey { + Rsa(ref pair) => PublicKey { publickey: PublicKeyInner::Rsa(pair.public()), }, #[cfg(feature = "secp256k1")] - Secp256k1(pair) => PublicKey { + Secp256k1(ref pair) => PublicKey { publickey: PublicKeyInner::Secp256k1(pair.public().clone()), }, #[cfg(feature = "ecdsa")] - Ecdsa(pair) => PublicKey { + Ecdsa(ref pair) => PublicKey { publickey: PublicKeyInner::Ecdsa(pair.public().clone()), }, } @@ -239,9 +239,9 @@ impl Keypair { feature = "rsa" ))] { - let pk: proto::PrivateKey = match &self.keypair { + let pk: proto::PrivateKey = match self.keypair { #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(data) => proto::PrivateKey { + KeyPairInner::Ed25519(ref data) => proto::PrivateKey { Type: proto::KeyType::Ed25519, Data: data.to_bytes().to_vec(), }, @@ -252,7 +252,7 @@ impl Keypair { return Err(DecodingError::encoding_unsupported("secp256k1")) } #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(data) => proto::PrivateKey { + KeyPairInner::Ecdsa(ref data) => proto::PrivateKey { Type: proto::KeyType::ECDSA, Data: data.secret().encode_der(), }, @@ -419,7 +419,7 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { - KeyPairInner::Rsa(inner) => Ok(inner), + KeyPairInner::Rsa(inner) => Ok(inner.clone()), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(feature = "secp256k1")] @@ -460,7 +460,7 @@ impl PublicKey { #[must_use] pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { use PublicKeyInner::*; - match self.publickey { + match &self.publickey { #[cfg(feature = "ed25519")] Ed25519(pk) => pk.verify(msg, sig), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] From 18e77d1e64e1096dafd0acebc3e11f627d802a0c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 15:07:39 +0200 Subject: [PATCH 25/31] fix clippy --- identity/src/keypair.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 6032df06e3c..7192fa4448e 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -419,7 +419,7 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { - KeyPairInner::Rsa(inner) => Ok(inner.clone()), + KeyPairInner::Rsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), #[cfg(feature = "secp256k1")] @@ -644,24 +644,21 @@ impl TryFrom for PublicKey { Err(DecodingError::missing_feature("rsa")) } #[cfg(feature = "secp256k1")] - proto::KeyType::Secp256k1 => Ok(secp256k1::PublicKey::decode(&pubkey.Data).map( - |kp| PublicKey { + proto::KeyType::Secp256k1 => Ok(secp256k1::PublicKey::try_from_bytes(&pubkey.Data) + .map(|kp| PublicKey { publickey: PublicKeyInner::Secp256k1(kp), - }, - )?), + })?), #[cfg(not(feature = "secp256k1"))] proto::KeyType::Secp256k1 => { log::debug!("support for secp256k1 was disabled at compile-time"); Err(DecodingError::missing_feature("secp256k1")) } #[cfg(feature = "ecdsa")] - proto::KeyType::ECDSA => { - Ok( - ecdsa::PublicKey::decode_der(&pubkey.Data).map(|kp| PublicKey { - publickey: PublicKeyInner::Ecdsa(kp), - })?, - ) - } + proto::KeyType::ECDSA => Ok(ecdsa::PublicKey::try_decode_der(&pubkey.Data).map( + |kp| PublicKey { + publickey: PublicKeyInner::Ecdsa(kp), + }, + )?), #[cfg(not(feature = "ecdsa"))] proto::KeyType::ECDSA => { log::debug!("support for ECDSA was disabled at compile-time"); From 60808fa97ae4402943b1eedf41f8fb72250016ba Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 15:24:39 +0200 Subject: [PATCH 26/31] fix clippy --- identity/src/error.rs | 1 + identity/src/keypair.rs | 60 +++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/identity/src/error.rs b/identity/src/error.rs index 0c86b346032..cb39e6ef312 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -33,6 +33,7 @@ pub struct DecodingError { } impl DecodingError { + #[allow(dead_code)] pub(crate) fn missing_feature(feature_name: &'static str) -> Self { Self { msg: format!("cargo feature `{feature_name}` is not enabled"), diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 7192fa4448e..2a0887314b0 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -191,38 +191,36 @@ impl Keypair { /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. - pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { - use KeyPairInner::*; + pub fn sign(&self, _msg: &[u8]) -> Result, SigningError> { match self.keypair { #[cfg(feature = "ed25519")] - Ed25519(ref pair) => Ok(pair.sign(msg)), + KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(_msg)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Rsa(ref pair) => pair.sign(msg), + KeyPairInner::Rsa(ref pair) => pair.sign(_msg), #[cfg(feature = "secp256k1")] - Secp256k1(ref pair) => pair.secret().sign(msg), + KeyPairInner::Secp256k1(ref pair) => pair.secret().sign(_msg), #[cfg(feature = "ecdsa")] - Ecdsa(ref pair) => Ok(pair.secret().sign(msg)), + KeyPairInner::Ecdsa(ref pair) => Ok(pair.secret().sign(_msg)), } } /// Get the public key of this keypair. pub fn public(&self) -> PublicKey { - use KeyPairInner::*; match self.keypair { #[cfg(feature = "ed25519")] - Ed25519(ref pair) => PublicKey { + KeyPairInner::Ed25519(ref pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Rsa(ref pair) => PublicKey { + KeyPairInner::Rsa(ref pair) => PublicKey { publickey: PublicKeyInner::Rsa(pair.public()), }, #[cfg(feature = "secp256k1")] - Secp256k1(ref pair) => PublicKey { + KeyPairInner::Secp256k1(ref pair) => PublicKey { publickey: PublicKeyInner::Secp256k1(pair.public().clone()), }, #[cfg(feature = "ecdsa")] - Ecdsa(ref pair) => PublicKey { + KeyPairInner::Ecdsa(ref pair) => PublicKey { publickey: PublicKeyInner::Ecdsa(pair.public().clone()), }, } @@ -230,8 +228,6 @@ impl Keypair { /// Encode a private key as protobuf structure. pub fn to_protobuf_encoding(&self) -> Result, DecodingError> { - use quick_protobuf::MessageWrite; - #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -239,6 +235,7 @@ impl Keypair { feature = "rsa" ))] { + use quick_protobuf::MessageWrite; let pk: proto::PrivateKey = match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(ref data) => proto::PrivateKey { @@ -275,9 +272,7 @@ impl Keypair { } /// Decode a private key from a protobuf structure and parse it as a [`Keypair`]. - pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { - use quick_protobuf::MessageRead; - + pub fn from_protobuf_encoding(_bytes: &[u8]) -> Result { #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -285,8 +280,9 @@ impl Keypair { feature = "rsa" ))] { - let mut reader = BytesReader::from_bytes(bytes); - let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) + use quick_protobuf::MessageRead; + let mut reader = BytesReader::from_bytes(_bytes); + let mut private_key = proto::PrivateKey::from_reader(&mut reader, _bytes) .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) .map(zeroize::Zeroizing::new)?; @@ -458,17 +454,16 @@ impl PublicKey { /// private key (authenticity), and that the message has not been /// tampered with (integrity). #[must_use] - pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { - use PublicKeyInner::*; - match &self.publickey { + pub fn verify(&self, _msg: &[u8], _sig: &[u8]) -> bool { + match self.publickey { #[cfg(feature = "ed25519")] - Ed25519(pk) => pk.verify(msg, sig), + PublicKeyInner::Ed25519(ref pk) => pk.verify(_msg, _sig), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - Rsa(pk) => pk.verify(msg, sig), + PublicKeyInner::Rsa(ref pk) => pk.verify(_msg, _sig), #[cfg(feature = "secp256k1")] - Secp256k1(pk) => pk.verify(msg, sig), + PublicKeyInner::Secp256k1(ref pk) => pk.verify(_msg, _sig), #[cfg(feature = "ecdsa")] - Ecdsa(pk) => pk.verify(msg, sig), + PublicKeyInner::Ecdsa(ref pk) => pk.verify(_msg, _sig), } } @@ -534,8 +529,6 @@ impl PublicKey { /// Encode the public key into a protobuf structure for storage or /// exchange with other nodes. pub fn encode_protobuf(&self) -> Vec { - use quick_protobuf::MessageWrite; - #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -543,6 +536,7 @@ impl PublicKey { feature = "rsa" ))] { + use quick_protobuf::MessageWrite; let public_key = proto::PublicKey::from(self); let mut buf = Vec::with_capacity(public_key.get_size()); @@ -574,9 +568,7 @@ impl PublicKey { /// Decode a public key from a protobuf structure, e.g. read from storage /// or received from another node. - pub fn try_decode_protobuf(bytes: &[u8]) -> Result { - use quick_protobuf::MessageRead; - + pub fn try_decode_protobuf(_bytes: &[u8]) -> Result { #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -584,9 +576,10 @@ impl PublicKey { feature = "rsa" ))] { - let mut reader = BytesReader::from_bytes(bytes); + use quick_protobuf::MessageRead; + let mut reader = BytesReader::from_bytes(_bytes); - let pubkey = proto::PublicKey::from_reader(&mut reader, bytes) + let pubkey = proto::PublicKey::from_reader(&mut reader, _bytes) .map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?; pubkey.try_into() @@ -774,11 +767,8 @@ impl From for PublicKey { #[cfg(test)] mod tests { - use super::*; #[cfg(feature = "peerid")] use crate::PeerId; - use base64::prelude::*; - use std::str::FromStr; #[test] #[cfg(feature = "ed25519")] From 9cb06fa9d163cf91050b39988857079b108a44be Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 15:39:24 +0200 Subject: [PATCH 27/31] fix clippy --- identity/src/keypair.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 8839394c7f2..ca197e7a4f4 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -191,16 +191,17 @@ impl Keypair { /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. - pub fn sign(&self, _msg: &[u8]) -> Result, SigningError> { + #[allow(unused_variables)] + pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { match self.keypair { #[cfg(feature = "ed25519")] - KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(_msg)), + KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(msg)), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - KeyPairInner::Rsa(ref pair) => pair.sign(_msg), + KeyPairInner::Rsa(ref pair) => pair.sign(msg), #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(ref pair) => pair.secret().sign(_msg), + KeyPairInner::Secp256k1(ref pair) => pair.secret().sign(msg), #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(ref pair) => Ok(pair.secret().sign(_msg)), + KeyPairInner::Ecdsa(ref pair) => Ok(pair.secret().sign(msg)), } } @@ -272,7 +273,8 @@ impl Keypair { } /// Decode a private key from a protobuf structure and parse it as a [`Keypair`]. - pub fn from_protobuf_encoding(_bytes: &[u8]) -> Result { + #[allow(unused_variables)] + pub fn from_protobuf_encoding(bytes: &[u8]) -> Result { #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -281,8 +283,8 @@ impl Keypair { ))] { use quick_protobuf::MessageRead; - let mut reader = BytesReader::from_bytes(_bytes); - let mut private_key = proto::PrivateKey::from_reader(&mut reader, _bytes) + let mut reader = BytesReader::from_bytes(bytes); + let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) .map(zeroize::Zeroizing::new)?; @@ -454,16 +456,17 @@ impl PublicKey { /// private key (authenticity), and that the message has not been /// tampered with (integrity). #[must_use] - pub fn verify(&self, _msg: &[u8], _sig: &[u8]) -> bool { + #[allow(unused_variables)] + pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { match self.publickey { #[cfg(feature = "ed25519")] - PublicKeyInner::Ed25519(ref pk) => pk.verify(_msg, _sig), + PublicKeyInner::Ed25519(ref pk) => pk.verify(msg, sig), #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] - PublicKeyInner::Rsa(ref pk) => pk.verify(_msg, _sig), + PublicKeyInner::Rsa(ref pk) => pk.verify(msg, sig), #[cfg(feature = "secp256k1")] - PublicKeyInner::Secp256k1(ref pk) => pk.verify(_msg, _sig), + PublicKeyInner::Secp256k1(ref pk) => pk.verify(msg, sig), #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(ref pk) => pk.verify(_msg, _sig), + PublicKeyInner::Ecdsa(ref pk) => pk.verify(msg, sig), } } @@ -568,7 +571,8 @@ impl PublicKey { /// Decode a public key from a protobuf structure, e.g. read from storage /// or received from another node. - pub fn try_decode_protobuf(_bytes: &[u8]) -> Result { + #[allow(unused_variables)] + pub fn try_decode_protobuf(bytes: &[u8]) -> Result { #[cfg(any( feature = "ecdsa", feature = "secp256k1", @@ -577,9 +581,9 @@ impl PublicKey { ))] { use quick_protobuf::MessageRead; - let mut reader = BytesReader::from_bytes(_bytes); + let mut reader = BytesReader::from_bytes(bytes); - let pubkey = proto::PublicKey::from_reader(&mut reader, _bytes) + let pubkey = proto::PublicKey::from_reader(&mut reader, bytes) .map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?; pubkey.try_into() From c98dfd12f03c1aa0782eda8435e5958516700a5d Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 15:43:30 +0200 Subject: [PATCH 28/31] fix clippy --- identity/src/keypair.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index ca197e7a4f4..f2e34908878 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -771,8 +771,11 @@ impl From for PublicKey { #[cfg(test)] mod tests { + use super::*; #[cfg(feature = "peerid")] use crate::PeerId; + use base64::prelude::*; + use std::str::FromStr; #[test] #[cfg(feature = "ed25519")] From b255326931e7f3e75d67a33e3ca1ec0d369eba4c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 16:15:05 +0200 Subject: [PATCH 29/31] fix due to keypair_protobuf_roundtrip_secp256k1 update --- identity/src/keypair.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index f2e34908878..aca021687fa 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -246,9 +246,10 @@ impl Keypair { #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] - KeyPairInner::Secp256k1(_) => { - return Err(DecodingError::encoding_unsupported("secp256k1")) - } + KeyPairInner::Secp256k1(ref data) => proto::PrivateKey { + Type: proto::KeyType::Secp256k1, + Data: data.secret().to_bytes().to_vec(), + }, #[cfg(feature = "ecdsa")] KeyPairInner::Ecdsa(ref data) => proto::PrivateKey { Type: proto::KeyType::ECDSA, @@ -283,6 +284,7 @@ impl Keypair { ))] { use quick_protobuf::MessageRead; + let mut reader = BytesReader::from_bytes(bytes); let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes) .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) @@ -300,7 +302,16 @@ impl Keypair { Err(DecodingError::missing_feature("ed25519")) } proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")), - proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")), + proto::KeyType::Secp256k1 => { + #[cfg(feature = "secp256k1")] + return secp256k1::SecretKey::try_from_bytes(&mut private_key.Data).map( + |key| Keypair { + keypair: KeyPairInner::Secp256k1(key.into()), + }, + ); + + Err(DecodingError::missing_feature("secp256k1")) + } proto::KeyType::ECDSA => { #[cfg(feature = "ecdsa")] return ecdsa::SecretKey::try_decode_der(&mut private_key.Data).map(|key| { From e65f045c5df63afde66256b3e214647560e49051 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 5 May 2023 16:34:23 +0200 Subject: [PATCH 30/31] add features to keypair_from_protobuf_encoding test --- identity/src/keypair.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index aca021687fa..398dfb10e19 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -854,7 +854,13 @@ mod tests { } #[test] - #[cfg(feature = "peerid")] + #[cfg(all( + feature = "ecdsa", + feature = "secp256k1", + feature = "ed25519", + feature = "rsa", + feature = "peerid" + ))] fn keypair_from_protobuf_encoding() { // E.g. retrieved from an IPFS config file. let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA="; From b9983726f3d69afbbcbb21a7d44a279a300a0363 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 5 May 2023 21:59:30 +0200 Subject: [PATCH 31/31] Minor tidy-up --- identity/CHANGELOG.md | 2 +- identity/src/keypair.rs | 3 +-- identity/src/lib.rs | 6 ++---- identity/src/peer_id.rs | 2 -- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 45a1abd6ea8..14d6405743b 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -5,7 +5,7 @@ - Add support for exporting and importing ECDSA keys via the libp2p [protobuf format]. See [PR 3863]. -- Make `Keypair` and `Publickey` opaque. +- Make `Keypair` and `PublicKey` opaque. See [PR 3866]. [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 398dfb10e19..a3128e56fbc 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -191,7 +191,6 @@ impl Keypair { /// Sign a message using the private key of this keypair, producing /// a signature that can be verified using the corresponding public key. - #[allow(unused_variables)] pub fn sign(&self, msg: &[u8]) -> Result, SigningError> { match self.keypair { #[cfg(feature = "ed25519")] @@ -290,7 +289,7 @@ impl Keypair { .map_err(|e| DecodingError::bad_protobuf("private key bytes", e)) .map(zeroize::Zeroizing::new)?; - #[allow(deprecated, unreachable_code)] + #[allow(unreachable_code)] match private_key.Type { proto::KeyType::Ed25519 => { #[cfg(feature = "ed25519")] diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 5d562576dab..9a6c42374f6 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -84,12 +84,11 @@ impl zeroize::Zeroize for proto::PrivateKey { ))] impl From<&PublicKey> for proto::PublicKey { fn from(key: &PublicKey) -> Self { - #[allow(deprecated)] match &key.publickey { #[cfg(feature = "ed25519")] keypair::PublicKeyInner::Ed25519(key) => proto::PublicKey { Type: proto::KeyType::Ed25519, - Data: key.encode().to_vec(), + Data: key.to_bytes().to_vec(), }, #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { @@ -99,7 +98,7 @@ impl From<&PublicKey> for proto::PublicKey { #[cfg(feature = "secp256k1")] keypair::PublicKeyInner::Secp256k1(key) => proto::PublicKey { Type: proto::KeyType::Secp256k1, - Data: key.encode().to_vec(), + Data: key.to_bytes().to_vec(), }, #[cfg(feature = "ecdsa")] keypair::PublicKeyInner::Ecdsa(key) => proto::PublicKey { @@ -112,7 +111,6 @@ impl From<&PublicKey> for proto::PublicKey { pub use error::{DecodingError, OtherVariantError, SigningError}; pub use keypair::{Keypair, PublicKey}; - #[cfg(feature = "peerid")] pub use peer_id::{ParseError, PeerId}; diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 51d9e4f1f6d..184b75aa785 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -307,7 +307,6 @@ mod tests { .parse() .unwrap(); - #[allow(deprecated)] let peer_id = PeerId::try_from_multiaddr(&address).unwrap(); assert_eq!( @@ -322,7 +321,6 @@ mod tests { fn no_panic_on_extract_peer_id_from_multi_address_if_not_present() { let address = "/memory/1234".to_string().parse().unwrap(); - #[allow(deprecated)] let maybe_empty = PeerId::try_from_multiaddr(&address); assert!(maybe_empty.is_none());