diff --git a/Cargo.lock b/Cargo.lock index b088d0da44f..2645430ba2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2665,10 +2665,11 @@ dependencies = [ [[package]] name = "libp2p-identity" -version = "0.2.12" +version = "0.2.13" dependencies = [ "asn1_der", "bs58", + "cfg_aliases", "criterion", "ed25519-dalek", "hex-literal", 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/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. diff --git a/identity/Cargo.toml b/identity/Cargo.toml index b5120efe078..0c6201ec967 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`. @@ -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"] @@ -46,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..df9f4c5de5b --- /dev/null +++ b/identity/build.rs @@ -0,0 +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") } + } +} diff --git a/identity/src/error.rs b/identity/src/error.rs index 6e8c4d02caa..3bbd1956f89 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -76,7 +76,7 @@ impl DecodingError { } } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[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"), @@ -106,7 +106,7 @@ pub struct SigningError { /// An error during encoding of key material. impl SigningError { - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] pub(crate) fn new(msg: S) -> Self { Self { msg: msg.to_string(), @@ -114,7 +114,7 @@ impl SigningError { } } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[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 a1bbba00fa9..e07882d0916 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -50,7 +50,7 @@ use crate::error::OtherVariantError; feature = "rsa" ))] use crate::proto; -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all(feature = "rsa", rsa_supported))] use crate::rsa; #[cfg(feature = "secp256k1")] use crate::secp256k1; @@ -87,7 +87,7 @@ enum KeyPairInner { #[cfg(feature = "ed25519")] Ed25519(ed25519::Keypair), /// An RSA keypair. - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] Rsa(rsa::Keypair), /// A Secp256k1 keypair. #[cfg(feature = "secp256k1")] @@ -132,7 +132,7 @@ impl Keypair { self.try_into() } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -146,7 +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", not(target_arch = "wasm32")))] + #[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), @@ -180,7 +180,7 @@ 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", rsa_supported))] KeyPairInner::Rsa(ref pair) => pair.sign(msg), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref pair) => Ok(pair.secret().sign(msg)), @@ -196,7 +196,7 @@ impl Keypair { KeyPairInner::Ed25519(ref pair) => PublicKey { publickey: PublicKeyInner::Ed25519(pair.public()), }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(ref pair) => PublicKey { publickey: PublicKeyInner::Rsa(pair.public()), }, @@ -227,7 +227,7 @@ impl Keypair { Type: proto::KeyType::Ed25519, Data: data.to_bytes().to_vec(), }, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref data) => proto::PrivateKey { @@ -286,7 +286,7 @@ impl Keypair { Err(DecodingError::missing_feature("ed25519")) } proto::KeyType::RSA => { - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] return rsa::Keypair::try_decode_pkcs1(&mut private_key.Data).map(|sk| { Keypair { keypair: KeyPairInner::Rsa(sk), @@ -331,7 +331,7 @@ impl Keypair { match self.keypair { #[cfg(feature = "ed25519")] KeyPairInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] KeyPairInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => KeyType::Secp256k1, @@ -389,7 +389,7 @@ 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", rsa_supported))] KeyPairInner::Rsa(_) => None, #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(ref inner) => Some(inner.secret().to_bytes()), @@ -432,7 +432,7 @@ impl From for Keypair { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all(feature = "rsa", rsa_supported))] impl From for Keypair { fn from(kp: rsa::Keypair) -> Self { Keypair { @@ -448,7 +448,7 @@ 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", rsa_supported))] KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -467,7 +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", not(target_arch = "wasm32")))] + #[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)), @@ -484,7 +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", not(target_arch = "wasm32")))] + #[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)), @@ -492,7 +492,7 @@ impl TryInto for Keypair { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all(feature = "rsa", rsa_supported))] impl TryInto for Keypair { type Error = OtherVariantError; @@ -514,7 +514,7 @@ 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", rsa_supported))] /// A public RSA key. Rsa(rsa::PublicKey), #[cfg(feature = "secp256k1")] @@ -542,7 +542,7 @@ 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", rsa_supported))] PublicKeyInner::Rsa(ref pk) => pk.verify(msg, sig), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(ref pk) => pk.verify(msg, sig), @@ -561,7 +561,7 @@ impl PublicKey { self.try_into() } - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] pub fn try_into_rsa(self) -> Result { self.try_into() } @@ -642,7 +642,7 @@ impl PublicKey { match self.publickey { #[cfg(feature = "ed25519")] PublicKeyInner::Ed25519(_) => KeyType::Ed25519, - #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] PublicKeyInner::Rsa(_) => KeyType::RSA, #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => KeyType::Secp256k1, @@ -674,7 +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", not(target_arch = "wasm32")))] + #[cfg(all(feature = "rsa", rsa_supported))] proto::KeyType::RSA => { Ok( rsa::PublicKey::try_decode_x509(&pubkey.Data).map(|kp| PublicKey { @@ -682,7 +682,7 @@ impl TryFrom for PublicKey { })?, ) } - #[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))] + #[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")) @@ -719,7 +719,7 @@ 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", rsa_supported))] PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)), #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)), @@ -738,7 +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", not(target_arch = "wasm32")))] + #[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)), @@ -755,7 +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", not(target_arch = "wasm32")))] + #[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)), @@ -763,7 +763,7 @@ impl TryInto for PublicKey { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all(feature = "rsa", rsa_supported))] impl TryInto for PublicKey { type Error = OtherVariantError; @@ -807,7 +807,7 @@ impl From for PublicKey { } } -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[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 4f4313e8f17..f101f80a8bf 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -52,7 +52,7 @@ pub mod ecdsa; #[cfg(feature = "ed25519")] pub mod ed25519; -#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +#[cfg(all(feature = "rsa", rsa_supported))] pub mod rsa; #[cfg(feature = "secp256k1")] @@ -89,7 +89,7 @@ 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", rsa_supported))] keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { Type: proto::KeyType::RSA, Data: key.encode_x509(),