Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 5 additions & 0 deletions identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 7 additions & 1 deletion identity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down Expand Up @@ -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"]
Expand All @@ -46,6 +49,9 @@ rmp-serde = "1.3"
criterion = "0.5"
hex-literal = "0.4.1"

[build-dependencies]
cfg_aliases = "0.2.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure if we should have another dependency in our tree (even though it's only for the building process). Thoughts @elenaf9 ?


[[bench]]
name = "peer_id"
harness = false
Expand Down
6 changes: 6 additions & 0 deletions identity/build.rs
Original file line number Diff line number Diff line change
@@ -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") }
}
}
6 changes: 3 additions & 3 deletions identity/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -106,15 +106,15 @@ 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<S: ToString>(msg: S) -> Self {
Self {
msg: msg.to_string(),
source: None,
}
}

#[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)),
Expand Down
52 changes: 26 additions & 26 deletions identity/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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<rsa::Keypair, OtherVariantError> {
self.try_into()
}
Expand All @@ -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<Keypair, DecodingError> {
rsa::Keypair::try_decode_pkcs8(pkcs8_der).map(|kp| Keypair {
keypair: KeyPairInner::Rsa(kp),
Expand Down Expand Up @@ -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)),
Expand All @@ -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()),
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -432,7 +432,7 @@ impl From<secp256k1::Keypair> for Keypair {
}
}

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg(all(feature = "rsa", rsa_supported))]
impl From<rsa::Keypair> for Keypair {
fn from(kp: rsa::Keypair) -> Self {
Keypair {
Expand All @@ -448,7 +448,7 @@ impl TryInto<ed25519::Keypair> for Keypair {
fn try_into(self) -> Result<ed25519::Keypair, Self::Error> {
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)),
Expand All @@ -467,7 +467,7 @@ impl TryInto<ecdsa::Keypair> 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)),
Expand All @@ -484,15 +484,15 @@ impl TryInto<secp256k1::Keypair> 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)),
}
}
}

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg(all(feature = "rsa", rsa_supported))]
impl TryInto<rsa::Keypair> for Keypair {
type Error = OtherVariantError;

Expand All @@ -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")]
Expand Down Expand Up @@ -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),
Expand All @@ -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<rsa::PublicKey, OtherVariantError> {
self.try_into()
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -674,15 +674,15 @@ impl TryFrom<proto::PublicKey> 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 {
publickey: PublicKeyInner::Rsa(kp),
})?,
)
}
#[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"))
Expand Down Expand Up @@ -719,7 +719,7 @@ impl TryInto<ed25519::PublicKey> for PublicKey {
fn try_into(self) -> Result<ed25519::PublicKey, Self::Error> {
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)),
Expand All @@ -738,7 +738,7 @@ impl TryInto<ecdsa::PublicKey> 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)),
Expand All @@ -755,15 +755,15 @@ impl TryInto<secp256k1::PublicKey> 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)),
}
}
}

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg(all(feature = "rsa", rsa_supported))]
impl TryInto<rsa::PublicKey> for PublicKey {
type Error = OtherVariantError;

Expand Down Expand Up @@ -807,7 +807,7 @@ impl From<ecdsa::PublicKey> for PublicKey {
}
}

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg(all(feature = "rsa", rsa_supported))]
impl From<rsa::PublicKey> for PublicKey {
fn from(key: rsa::PublicKey) -> Self {
PublicKey {
Expand Down
4 changes: 2 additions & 2 deletions identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(),
Expand Down
Loading