From 33daf87786d036ca7e3c16bd7e4e9bb1160b256a Mon Sep 17 00:00:00 2001 From: "Surkov, Kirill" Date: Wed, 13 Aug 2025 06:07:35 +0300 Subject: [PATCH 1/5] Enable rsa for wasm32-unknown-unknown targets --- Cargo.lock | 2 +- identity/Cargo.toml | 3 + identity/src/error.rs | 24 ++++- identity/src/keypair.rs | 208 +++++++++++++++++++++++++++++++++++----- identity/src/lib.rs | 16 +++- 5 files changed, 221 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b088d0da44f..c7cd1790549 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2665,7 +2665,7 @@ dependencies = [ [[package]] name = "libp2p-identity" -version = "0.2.12" +version = "0.2.13" dependencies = [ "asn1_der", "bs58", diff --git a/identity/Cargo.toml b/identity/Cargo.toml index b5120efe078..54c241c2eba 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -31,6 +31,9 @@ zeroize = { version = "1.8", optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] ring = { workspace = true, features = ["alloc", "std"], optional = true } +[target.wasm32-unknown-unknown.dependencies] +ring = { workspace = true, features = ["alloc", "std", "wasm32_unknown_unknown_js"], optional = true } + [features] secp256k1 = ["dep:k256", "dep:asn1_der", "dep:sha2", "dep:hkdf", "dep:zeroize"] ecdsa = ["dep:p256", "dep:zeroize", "dep:sec1", "dep:sha2", "dep:hkdf"] diff --git a/identity/src/error.rs b/identity/src/error.rs index 6e8c4d02caa..3f49bdd1d3b 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -76,7 +76,13 @@ impl DecodingError { } } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub(crate) fn encoding_unsupported(key_type: &'static str) -> Self { Self { msg: format!("encoding {key_type} key to Protobuf is unsupported"), @@ -106,7 +112,13 @@ pub struct SigningError { /// An error during encoding of key material. impl SigningError { - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub(crate) fn new(msg: S) -> Self { Self { msg: msg.to_string(), @@ -114,7 +126,13 @@ impl SigningError { } } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self { Self { source: Some(Box::new(source)), diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index a1bbba00fa9..a60681e7933 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -50,7 +50,13 @@ use crate::error::OtherVariantError; feature = "rsa" ))] use crate::proto; -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] use crate::rsa; #[cfg(feature = "secp256k1")] use crate::secp256k1; @@ -87,7 +93,13 @@ enum KeyPairInner { #[cfg(feature = "ed25519")] Ed25519(ed25519::Keypair), /// An RSA keypair. - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] Rsa(rsa::Keypair), /// A Secp256k1 keypair. #[cfg(feature = "secp256k1")] @@ -132,7 +144,13 @@ impl Keypair { self.try_into() } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -146,7 +164,13 @@ impl Keypair { /// format (i.e. unencrypted) as defined in [RFC5208]. /// /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { rsa::Keypair::try_decode_pkcs8(pkcs8_der).map(|kp| Keypair { keypair: KeyPairInner::Rsa(kp), @@ -180,7 +204,13 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(msg)), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(ref pair) => pair.sign(msg), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref pair) => Ok(pair.secret().sign(msg)), @@ -196,7 +226,13 @@ impl Keypair { KeyPairInner::Ed25519(ref pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(ref pair) => PublicKey { publickey: PublicKeyInner::Rsa(pair.public()), }, @@ -227,7 +263,13 @@ impl Keypair { Type: proto::KeyType::Ed25519, Data: data.to_bytes().to_vec(), }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref data) => proto::PrivateKey { @@ -286,7 +328,13 @@ impl Keypair { Err(DecodingError::missing_feature("ed25519")) } proto::KeyType::RSA => { - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] return rsa::Keypair::try_decode_pkcs1(&mut private_key.Data).map(|sk| { Keypair { keypair: KeyPairInner::Rsa(sk), @@ -331,7 +379,13 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => KeyType::Secp256k1, @@ -389,7 +443,13 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(ref inner) => Some(inner.secret().to_bytes()), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => None, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref inner) => Some(inner.secret().to_bytes()), @@ -432,7 +492,13 @@ impl From for Keypair { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { Keypair { @@ -448,7 +514,13 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { KeyPairInner::Ed25519(inner) => Ok(inner), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -467,7 +539,13 @@ impl TryInto for Keypair { KeyPairInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -484,7 +562,13 @@ impl TryInto for Keypair { KeyPairInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), @@ -492,7 +576,13 @@ impl TryInto for Keypair { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] impl TryInto for Keypair { type Error = OtherVariantError; @@ -514,7 +604,13 @@ pub(crate) enum PublicKeyInner { /// A public Ed25519 key. #[cfg(feature = "ed25519")] Ed25519(ed25519::PublicKey), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] /// A public RSA key. Rsa(rsa::PublicKey), #[cfg(feature = "secp256k1")] @@ -542,7 +638,13 @@ impl PublicKey { match self.publickey { #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(ref pk) => pk.verify(msg, sig), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] PublicKeyInner::Rsa(ref pk) => pk.verify(msg, sig), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(ref pk) => pk.verify(msg, sig), @@ -561,7 +663,13 @@ impl PublicKey { self.try_into() } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -642,7 +750,13 @@ impl PublicKey { match self.publickey { #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] PublicKeyInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => KeyType::Secp256k1, @@ -674,7 +788,13 @@ impl TryFrom for PublicKey { tracing::debug!("support for ed25519 was disabled at compile-time"); Err(DecodingError::missing_feature("ed25519")) } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] proto::KeyType::RSA => { Ok( rsa::PublicKey::try_decode_x509(&pubkey.Data).map(|kp| PublicKey { @@ -682,7 +802,13 @@ impl TryFrom for PublicKey { })?, ) } - #[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))] + #[cfg(not(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + )))] proto::KeyType::RSA => { tracing::debug!("support for RSA was disabled at compile-time"); Err(DecodingError::missing_feature("rsa")) @@ -719,7 +845,13 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { PublicKeyInner::Ed25519(inner) => Ok(inner), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -738,7 +870,13 @@ impl TryInto for PublicKey { PublicKeyInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -755,7 +893,13 @@ impl TryInto for PublicKey { PublicKeyInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), @@ -763,7 +907,13 @@ impl TryInto for PublicKey { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] impl TryInto for PublicKey { type Error = OtherVariantError; @@ -807,7 +957,13 @@ impl From for PublicKey { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] impl From for PublicKey { fn from(key: rsa::PublicKey) -> Self { PublicKey { diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 4f4313e8f17..f8709632f3a 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -52,7 +52,13 @@ pub mod ecdsa; #[cfg(feature = "ed25519")] pub mod ed25519; -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) +))] pub mod rsa; #[cfg(feature = "secp256k1")] @@ -89,7 +95,13 @@ impl From<&PublicKey> for proto::PublicKey { Type: proto::KeyType::Ed25519, Data: key.to_bytes().to_vec(), }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all( + feature = "rsa", + any( + not(target_arch = "wasm32"), + all(target_arch = "wasm32", target_os = "unknown") + ) + ))] keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(), From 6cecc68dd3214238f58bd7bd1885c8d71ee717a8 Mon Sep 17 00:00:00 2001 From: "Surkov, Kirill" Date: Wed, 13 Aug 2025 06:41:22 +0300 Subject: [PATCH 2/5] Bump libp2p-identity version --- Cargo.toml | 2 +- identity/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 527d20c27e4..e71684132b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ libp2p-dns = { version = "0.44.0", path = "transports/dns" } libp2p-floodsub = { version = "0.47.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.50.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.47.0", path = "protocols/identify" } -libp2p-identity = { version = "0.2.12" } +libp2p-identity = { version = "0.2.13" } libp2p-kad = { version = "0.49.0", path = "protocols/kad" } libp2p-mdns = { version = "0.48.0", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.5.0", path = "misc/memory-connection-limits" } diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 54c241c2eba..f544675b9c5 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-identity" -version = "0.2.12" +version = "0.2.13" edition = "2021" # MUST NOT inherit from workspace because we don't want to publish breaking changes to `libp2p-identity`. description = "Data structures and algorithms for identifying peers in libp2p." rust-version = "1.73.0" # MUST NOT inherit from workspace because we don't want to publish breaking changes to `libp2p-identity`. From 9517e701bcacd6d403bd48144ce1bcd9be2f9eff Mon Sep 17 00:00:00 2001 From: "Surkov, Kirill" Date: Wed, 13 Aug 2025 06:43:47 +0300 Subject: [PATCH 3/5] Update libp2p-identity changelog --- identity/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 6c58a32af3f..9a3ee69ac17 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.13 + +- Enable rsa for wasm32-unknown-unknown targets. + See [PR 6137](https://github.com/libp2p/rust-libp2p/pull/6137) + ## 0.2.12 - Avoid depending on the `rand_core` feature in `ed25519-dalek` crate. From 689e2df8289c62fca1ca2a0f130563c7d9e0400d Mon Sep 17 00:00:00 2001 From: "Surkov, Kirill" Date: Wed, 3 Sep 2025 17:39:49 +0300 Subject: [PATCH 4/5] Use cfg_aliases to simplify cfg! expressions --- Cargo.lock | 1 + identity/Cargo.toml | 3 + identity/build.rs | 5 + identity/src/error.rs | 24 +---- identity/src/keypair.rs | 208 +++++----------------------------------- identity/src/lib.rs | 16 +--- 6 files changed, 40 insertions(+), 217 deletions(-) create mode 100644 identity/build.rs diff --git a/Cargo.lock b/Cargo.lock index c7cd1790549..2645430ba2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2669,6 +2669,7 @@ version = "0.2.13" dependencies = [ "asn1_der", "bs58", + "cfg_aliases", "criterion", "ed25519-dalek", "hex-literal", diff --git a/identity/Cargo.toml b/identity/Cargo.toml index f544675b9c5..0c6201ec967 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -49,6 +49,9 @@ rmp-serde = "1.3" criterion = "0.5" hex-literal = "0.4.1" +[build-dependencies] +cfg_aliases = "0.2.1" + [[bench]] name = "peer_id" harness = false diff --git a/identity/build.rs b/identity/build.rs new file mode 100644 index 00000000000..2078da22113 --- /dev/null +++ b/identity/build.rs @@ -0,0 +1,5 @@ +fn main() { + cfg_aliases::cfg_aliases! { + rsa_supported: { any(not(target_arch = "wasm32"), target_os = "unknown") } + } +} diff --git a/identity/src/error.rs b/identity/src/error.rs index 3f49bdd1d3b..3bbd1956f89 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -76,13 +76,7 @@ impl DecodingError { } } - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub(crate) fn encoding_unsupported(key_type: &'static str) -> Self { Self { msg: format!("encoding {key_type} key to Protobuf is unsupported"), @@ -112,13 +106,7 @@ pub struct SigningError { /// An error during encoding of key material. impl SigningError { - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub(crate) fn new(msg: S) -> Self { Self { msg: msg.to_string(), @@ -126,13 +114,7 @@ impl SigningError { } } - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self { Self { source: Some(Box::new(source)), diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index a60681e7933..e07882d0916 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -50,13 +50,7 @@ use crate::error::OtherVariantError; feature = "rsa" ))] use crate::proto; -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] use crate::rsa; #[cfg(feature = "secp256k1")] use crate::secp256k1; @@ -93,13 +87,7 @@ enum KeyPairInner { #[cfg(feature = "ed25519")] Ed25519(ed25519::Keypair), /// An RSA keypair. - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] Rsa(rsa::Keypair), /// A Secp256k1 keypair. #[cfg(feature = "secp256k1")] @@ -144,13 +132,7 @@ impl Keypair { self.try_into() } - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -164,13 +146,7 @@ impl Keypair { /// format (i.e. unencrypted) as defined in [RFC5208]. /// /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result { rsa::Keypair::try_decode_pkcs8(pkcs8_der).map(|kp| Keypair { keypair: KeyPairInner::Rsa(kp), @@ -204,13 +180,7 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(msg)), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(ref pair) => pair.sign(msg), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref pair) => Ok(pair.secret().sign(msg)), @@ -226,13 +196,7 @@ impl Keypair { KeyPairInner::Ed25519(ref pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), }, - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(ref pair) => PublicKey { publickey: PublicKeyInner::Rsa(pair.public()), }, @@ -263,13 +227,7 @@ impl Keypair { Type: proto::KeyType::Ed25519, Data: data.to_bytes().to_vec(), }, - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref data) => proto::PrivateKey { @@ -328,13 +286,7 @@ impl Keypair { Err(DecodingError::missing_feature("ed25519")) } proto::KeyType::RSA => { - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] return rsa::Keypair::try_decode_pkcs1(&mut private_key.Data).map(|sk| { Keypair { keypair: KeyPairInner::Rsa(sk), @@ -379,13 +331,7 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => KeyType::Secp256k1, @@ -443,13 +389,7 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(ref inner) => Some(inner.secret().to_bytes()), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => None, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref inner) => Some(inner.secret().to_bytes()), @@ -492,13 +432,7 @@ impl From for Keypair { } } -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { Keypair { @@ -514,13 +448,7 @@ impl TryInto for Keypair { fn try_into(self) -> Result { match self.keypair { KeyPairInner::Ed25519(inner) => Ok(inner), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -539,13 +467,7 @@ impl TryInto for Keypair { KeyPairInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -562,13 +484,7 @@ impl TryInto for Keypair { KeyPairInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), @@ -576,13 +492,7 @@ impl TryInto for Keypair { } } -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] impl TryInto for Keypair { type Error = OtherVariantError; @@ -604,13 +514,7 @@ pub(crate) enum PublicKeyInner { /// A public Ed25519 key. #[cfg(feature = "ed25519")] Ed25519(ed25519::PublicKey), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] /// A public RSA key. Rsa(rsa::PublicKey), #[cfg(feature = "secp256k1")] @@ -638,13 +542,7 @@ impl PublicKey { match self.publickey { #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(ref pk) => pk.verify(msg, sig), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(ref pk) => pk.verify(msg, sig), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(ref pk) => pk.verify(msg, sig), @@ -663,13 +561,7 @@ impl PublicKey { self.try_into() } - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -750,13 +642,7 @@ impl PublicKey { match self.publickey { #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => KeyType::Secp256k1, @@ -788,13 +674,7 @@ impl TryFrom for PublicKey { tracing::debug!("support for ed25519 was disabled at compile-time"); Err(DecodingError::missing_feature("ed25519")) } - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] proto::KeyType::RSA => { Ok( rsa::PublicKey::try_decode_x509(&pubkey.Data).map(|kp| PublicKey { @@ -802,13 +682,7 @@ impl TryFrom for PublicKey { })?, ) } - #[cfg(not(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - )))] + #[cfg(not(all(feature = "rsa", rsa_supported)))] proto::KeyType::RSA => { tracing::debug!("support for RSA was disabled at compile-time"); Err(DecodingError::missing_feature("rsa")) @@ -845,13 +719,7 @@ impl TryInto for PublicKey { fn try_into(self) -> Result { match self.publickey { PublicKeyInner::Ed25519(inner) => Ok(inner), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -870,13 +738,7 @@ impl TryInto for PublicKey { PublicKeyInner::Ecdsa(inner) => Ok(inner), #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -893,13 +755,7 @@ impl TryInto for PublicKey { PublicKeyInner::Secp256k1(inner) => Ok(inner), #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)), - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "ecdsa")] PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)), @@ -907,13 +763,7 @@ impl TryInto for PublicKey { } } -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] impl TryInto for PublicKey { type Error = OtherVariantError; @@ -957,13 +807,7 @@ impl From for PublicKey { } } -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] impl From for PublicKey { fn from(key: rsa::PublicKey) -> Self { PublicKey { diff --git a/identity/src/lib.rs b/identity/src/lib.rs index f8709632f3a..f101f80a8bf 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -52,13 +52,7 @@ pub mod ecdsa; #[cfg(feature = "ed25519")] pub mod ed25519; -#[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) -))] +#[cfg(all(feature = "rsa", rsa_supported))] pub mod rsa; #[cfg(feature = "secp256k1")] @@ -95,13 +89,7 @@ impl From<&PublicKey> for proto::PublicKey { Type: proto::KeyType::Ed25519, Data: key.to_bytes().to_vec(), }, - #[cfg(all( - feature = "rsa", - any( - not(target_arch = "wasm32"), - all(target_arch = "wasm32", target_os = "unknown") - ) - ))] + #[cfg(all(feature = "rsa", rsa_supported))] keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(), From 8ef97dc6f03ceb7792144c9430877c642a95754d Mon Sep 17 00:00:00 2001 From: "Surkov, Kirill" Date: Wed, 3 Sep 2025 18:04:51 +0300 Subject: [PATCH 5/5] Add a comment --- identity/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/identity/build.rs b/identity/build.rs index 2078da22113..df9f4c5de5b 100644 --- a/identity/build.rs +++ b/identity/build.rs @@ -1,5 +1,6 @@ fn main() { cfg_aliases::cfg_aliases! { + // Only native targets and `wasm32-unknown-unknown` are supported by `ring` crate. rsa_supported: { any(not(target_arch = "wasm32"), target_os = "unknown") } } }