Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for noise IX, XX, and IK handshakes. #888

Merged
merged 20 commits into from
Jan 30, 2019
Merged
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tokio-io = "0.1"
[target.'cfg(not(any(target_os = "emscripten", target_os = "unknown")))'.dependencies]
libp2p-dns = { version = "0.2.0", path = "./transports/dns" }
libp2p-mdns = { version = "0.2.0", path = "./misc/mdns" }
libp2p-noise = { version = "0.1.0", path = "./protocols/noise" }
libp2p-tcp = { version = "0.2.0", path = "./transports/tcp" }

[target.'cfg(any(target_os = "emscripten", target_os = "unknown"))'.dependencies]
Expand Down Expand Up @@ -65,6 +66,7 @@ members = [
"protocols/floodsub",
"protocols/identify",
"protocols/kad",
"protocols/noise",
"protocols/observed",
"protocols/ping",
"protocols/plaintext",
Expand Down
24 changes: 24 additions & 0 deletions protocols/noise/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "libp2p-noise"
description = "Cryptographic handshake protocol using the noise framework."
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
edition = "2018"

[dependencies]
curve25519-dalek = "1"
futures = "0.1"
lazy_static = "1.2"
libp2p-core = { version = "0.2.0", path = "../../core" }
log = "0.4"
rand = "0.6"
snow = { version = "0.5.0-alpha1", default-features = false, features = ["ring-resolver"] }
tokio-io = "0.1"

[dev-dependencies]
env_logger = "0.6"
libp2p-tcp = { version = "0.2.0", path = "../../transports/tcp" }
quickcheck = "0.8"
tokio = "0.1"
69 changes: 69 additions & 0 deletions protocols/noise/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use snow::SnowError;
use std::{error::Error, fmt, io};

/// libp2p_noise error type.
#[derive(Debug)]
pub enum NoiseError {
/// An I/O error has been encountered.
Io(io::Error),
/// An noise framework error has been encountered.
Noise(SnowError),
/// A public key is invalid.
InvalidKey,
#[doc(hidden)]
__Nonexhaustive
}

impl fmt::Display for NoiseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
NoiseError::Io(e) => write!(f, "{}", e),
NoiseError::Noise(e) => write!(f, "{}", e),
NoiseError::InvalidKey => f.write_str("invalid public key"),
NoiseError::__Nonexhaustive => f.write_str("__Nonexhaustive")
}
}
}

impl Error for NoiseError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
NoiseError::Io(e) => Some(e),
NoiseError::Noise(_) => None, // TODO: `SnowError` should implement `Error`.
NoiseError::InvalidKey => None,
NoiseError::__Nonexhaustive => None
}
}
}

impl From<io::Error> for NoiseError {
fn from(e: io::Error) -> Self {
NoiseError::Io(e)
}
}

impl From<SnowError> for NoiseError {
fn from(e: SnowError) -> Self {
NoiseError::Noise(e)
}
}
Loading