core/: Introduce rsa feature flag to avoid ring dependency (#2860)

- Introduce `rsa` feature flag to `libp2p-core`.
- Expose `rsa` feature in `libp2p`.
- Add `rsa` feature to `libp2p` `default`.
This commit is contained in:
Alexander Shishenko
2022-09-07 09:16:22 +03:00
committed by GitHub
parent a40180c3d8
commit 8644c65a22
54 changed files with 145 additions and 58 deletions

View File

@@ -35,7 +35,7 @@
#[cfg(feature = "ecdsa")]
pub mod ecdsa;
pub mod ed25519;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub mod rsa;
#[cfg(feature = "secp256k1")]
pub mod secp256k1;
@@ -68,8 +68,8 @@ use std::convert::{TryFrom, TryInto};
pub enum Keypair {
/// An Ed25519 keypair.
Ed25519(ed25519::Keypair),
#[cfg(not(target_arch = "wasm32"))]
/// An RSA keypair.
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(rsa::Keypair),
/// A Secp256k1 keypair.
#[cfg(feature = "secp256k1")]
@@ -101,7 +101,7 @@ impl Keypair {
/// format (i.e. unencrypted) as defined in [RFC5208].
///
/// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
}
@@ -122,7 +122,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(ref pair) => Ok(pair.sign(msg)),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(ref pair) => pair.sign(msg),
#[cfg(feature = "secp256k1")]
Secp256k1(ref pair) => pair.secret().sign(msg),
@@ -136,7 +136,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(pair) => PublicKey::Ed25519(pair.public()),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pair) => PublicKey::Rsa(pair.public()),
#[cfg(feature = "secp256k1")]
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
@@ -154,7 +154,7 @@ impl Keypair {
r#type: keys_proto::KeyType::Ed25519.into(),
data: data.encode().into(),
},
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Self::Rsa(_) => {
return Err(DecodingError::new(
"Encoding RSA key into Protobuf is unsupported",
@@ -218,7 +218,7 @@ impl zeroize::Zeroize for keys_proto::PrivateKey {
pub enum PublicKey {
/// A public Ed25519 key.
Ed25519(ed25519::PublicKey),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
/// A public RSA key.
Rsa(rsa::PublicKey),
#[cfg(feature = "secp256k1")]
@@ -239,7 +239,7 @@ impl PublicKey {
use PublicKey::*;
match self {
Ed25519(pk) => pk.verify(msg, sig),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pk) => pk.verify(msg, sig),
#[cfg(feature = "secp256k1")]
Secp256k1(pk) => pk.verify(msg, sig),
@@ -286,7 +286,7 @@ impl From<&PublicKey> for keys_proto::PublicKey {
r#type: keys_proto::KeyType::Ed25519 as i32,
data: key.encode().to_vec(),
},
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKey::Rsa(key) => keys_proto::PublicKey {
r#type: keys_proto::KeyType::Rsa as i32,
data: key.encode_x509(),
@@ -316,11 +316,11 @@ impl TryFrom<keys_proto::PublicKey> for PublicKey {
keys_proto::KeyType::Ed25519 => {
ed25519::PublicKey::decode(&pubkey.data).map(PublicKey::Ed25519)
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
keys_proto::KeyType::Rsa => {
rsa::PublicKey::decode_x509(&pubkey.data).map(PublicKey::Rsa)
}
#[cfg(target_arch = "wasm32")]
#[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))]
keys_proto::KeyType::Rsa => {
log::debug!("support for RSA was disabled at compile-time");
Err(DecodingError::new("Unsupported"))

View File

@@ -67,6 +67,7 @@ pub struct SigningError {
/// An error during encoding of key material.
impl SigningError {
#[cfg(any(feature = "secp256k1", feature = "rsa"))]
pub(crate) fn new<S: ToString>(msg: S) -> Self {
Self {
msg: msg.to_string(),
@@ -74,6 +75,7 @@ impl SigningError {
}
}
#[cfg(feature = "rsa")]
pub(crate) fn source(self, source: impl Error + Send + Sync + 'static) -> Self {
Self {
source: Some(Box::new(source)),