Test on wasm32-unknown-unknown (#806)

* Test on wasm32-unknown-unknown

* Fix compilation
This commit is contained in:
Pierre Krieger
2019-01-02 15:50:08 +01:00
committed by GitHub
parent ad7a3b94ba
commit 9ca215b1a3
12 changed files with 56 additions and 80 deletions

View File

@ -24,7 +24,7 @@
//! helps you with.
use crate::error::SecioError;
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
use ring::digest;
use std::cmp::Ordering;
use crate::stream_cipher::Cipher;
@ -204,7 +204,7 @@ pub fn select_digest(r: Ordering, ours: &str, theirs: &str) -> Result<Digest, Se
Err(SecioError::NoSupportIntersection)
}
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
impl Into<&'static digest::Algorithm> for Digest {
#[inline]
fn into(self) -> &'static digest::Algorithm {

View File

@ -24,10 +24,10 @@ use futures::prelude::*;
use crate::SecioError;
#[path = "impl_ring.rs"]
#[cfg(not(target_os = "emscripten"))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
mod platform;
#[path = "impl_webcrypto.rs"]
#[cfg(target_os = "emscripten")]
#[cfg(any(target_os = "emscripten", target_os = "unknown"))]
mod platform;
/// Possible key agreement algorithms.

View File

@ -34,9 +34,9 @@ use log::{debug, trace};
use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
use protobuf::Message as ProtobufMessage;
use rand::{self, RngCore};
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
use ring::signature::{RSASigningState, RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_SHA256, verify as ring_verify};
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
use ring::rand::SystemRandom;
#[cfg(feature = "secp256k1")]
use secp256k1;
@ -46,7 +46,7 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use crate::structs_proto::{Exchange, Propose};
use tokio_io::codec::length_delimited;
use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
use untrusted::Input as UntrustedInput;
use crate::{KeyAgreement, SecioConfig, SecioKeyPairInner};
@ -368,7 +368,7 @@ where
exchange.set_epubkey(tmp_pub_key);
exchange.set_signature({
match context.config.key.inner {
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
SecioKeyPairInner::Rsa { ref private, .. } => {
let mut state = match RSASigningState::new(private.clone()) {
Ok(s) => s,
@ -401,7 +401,7 @@ where
let secp256k1 = secp256k1::Secp256k1::signing_only();
secp256k1
.sign(&message, private)
.serialize_der(&secp256k1)
.serialize_der()
},
}
});
@ -452,7 +452,7 @@ where
data_to_verify.extend_from_slice(remote_exch.get_epubkey());
match context.state.remote.public_key {
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
PublicKey::Rsa(ref remote_public_key) => {
// TODO: The ring library doesn't like some stuff in our DER public key,
// therefore we scrap the first 24 bytes of the key. A proper fix would
@ -492,8 +492,8 @@ where
let message = secp256k1::Message::from_slice(data_to_verify.as_ref())
.expect("digest output length doesn't match secp256k1 input length");
let secp256k1 = secp256k1::Secp256k1::verification_only();
let signature = secp256k1::Signature::from_der(&secp256k1, remote_exch.get_signature());
let remote_public_key = secp256k1::key::PublicKey::from_slice(&secp256k1, remote_public_key);
let signature = secp256k1::Signature::from_der(remote_exch.get_signature());
let remote_public_key = secp256k1::key::PublicKey::from_slice(remote_public_key);
if let (Ok(signature), Ok(remote_public_key)) = (signature, remote_public_key) {
match secp256k1.verify(&message, &signature, &remote_public_key) {
Ok(()) => (),
@ -507,7 +507,7 @@ where
return Err(SecioError::SignatureVerificationFailed)
}
},
#[cfg(not(all(feature = "ring", not(target_os = "emscripten"))))]
#[cfg(not(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown")))))]
PublicKey::Rsa(_) => {
debug!("support for RSA was disabled at compile-time");
return Err(SecioError::SignatureVerificationFailed);
@ -640,7 +640,7 @@ mod tests {
use crate::{SecioConfig, SecioKeyPair};
#[test]
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
fn handshake_with_self_succeeds_rsa() {
let key1 = {
let private = include_bytes!("../tests/test-rsa-private-key.pk8");

View File

@ -77,7 +77,7 @@
// TODO: unfortunately the `js!` macro of stdweb depends on tons of "private" macros, which we
// don't want to import manually
#[cfg(target_os = "emscripten")]
#[cfg(any(target_os = "emscripten", target_os = "unknown"))]
#[macro_use]
extern crate stdweb;
@ -91,7 +91,7 @@ use futures::stream::MapErr as StreamMapErr;
use futures::{Future, Poll, Sink, StartSend, Stream};
use libp2p_core::{PeerId, PublicKey, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade}};
use log::debug;
#[cfg(all(feature = "rsa", not(target_os = "emscripten")))]
#[cfg(all(feature = "rsa", not(any(target_os = "emscripten", target_os = "unknown"))))]
use ring::signature::RSAKeyPair;
use rw_stream_sink::RwStreamSink;
use std::error::Error;
@ -99,7 +99,7 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::iter;
use std::sync::Arc;
use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(all(feature = "rsa", not(target_os = "emscripten")))]
#[cfg(all(feature = "rsa", not(any(target_os = "emscripten", target_os = "unknown"))))]
use untrusted::Input;
mod algo_support;
@ -209,7 +209,7 @@ pub struct SecioKeyPair {
impl SecioKeyPair {
/// Builds a `SecioKeyPair` from a PKCS8 private key and public key.
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
pub fn rsa_from_pkcs8<P>(
private: &[u8],
public: P,
@ -229,7 +229,7 @@ impl SecioKeyPair {
/// Generates a new Ed25519 key pair and uses it.
pub fn ed25519_generated() -> Result<SecioKeyPair, Box<Error + Send + Sync>> {
let mut csprng = rand::rngs::OsRng::new()?;
let mut csprng = rand::thread_rng();
let keypair: Ed25519KeyPair = Ed25519KeyPair::generate::<sha2::Sha512, _>(&mut csprng);
Ok(SecioKeyPair {
inner: SecioKeyPairInner::Ed25519 {
@ -241,13 +241,7 @@ impl SecioKeyPair {
/// Generates a new random sec256k1 key pair.
#[cfg(feature = "secp256k1")]
pub fn secp256k1_generated() -> Result<SecioKeyPair, Box<Error + Send + Sync>> {
let secp = secp256k1::Secp256k1::new();
// TODO: This will work once 0.11.5 is released. See https://github.com/rust-bitcoin/rust-secp256k1/pull/80#pullrequestreview-172681778
// let private = secp256k1::key::SecretKey::new(&secp, &mut secp256k1::rand::thread_rng());
use rand::Rng;
let mut random_slice= [0u8; secp256k1::constants::SECRET_KEY_SIZE];
rand::thread_rng().fill(&mut random_slice[..]);
let private = secp256k1::key::SecretKey::from_slice(&secp, &random_slice).expect("slice has the right size");
let private = secp256k1::key::SecretKey::new(&mut secp256k1::rand::thread_rng());
Ok(SecioKeyPair {
inner: SecioKeyPairInner::Secp256k1 { private },
})
@ -259,8 +253,7 @@ impl SecioKeyPair {
where
K: AsRef<[u8]>,
{
let secp = secp256k1::Secp256k1::without_caps();
let private = secp256k1::key::SecretKey::from_slice(&secp, key.as_ref())?;
let private = secp256k1::key::SecretKey::from_slice(key.as_ref())?;
Ok(SecioKeyPair {
inner: SecioKeyPairInner::Secp256k1 { private },
@ -287,7 +280,7 @@ impl SecioKeyPair {
/// Returns the public key corresponding to this key pair.
pub fn to_public_key(&self) -> PublicKey {
match self.inner {
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
SecioKeyPairInner::Rsa { ref public, .. } => PublicKey::Rsa(public.clone()),
SecioKeyPairInner::Ed25519 { ref key_pair } => {
PublicKey::Ed25519(key_pair.public.as_bytes().to_vec())
@ -313,7 +306,7 @@ impl SecioKeyPair {
// Inner content of `SecioKeyPair`.
#[derive(Clone)]
enum SecioKeyPairInner {
#[cfg(all(feature = "ring", not(target_os = "emscripten")))]
#[cfg(all(feature = "ring", not(any(target_os = "emscripten", target_os = "unknown"))))]
Rsa {
public: Vec<u8>,
// We use an `Arc` so that we can clone the enum.