2017-10-30 10:22:38 +01:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
//! The `secio` protocol is a middleware that will encrypt and decrypt communications going
|
|
|
|
//! through a socket (or anything that implements `AsyncRead + AsyncWrite`).
|
|
|
|
//!
|
2017-12-07 15:34:22 +01:00
|
|
|
//! # Connection upgrade
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-07 15:34:22 +01:00
|
|
|
//! The `SecioConfig` struct implements the `ConnectionUpgrade` trait. You can apply it over a
|
|
|
|
//! `Transport` by using the `with_upgrade` method. The returned object will also implement
|
|
|
|
//! `Transport` and will automatically apply the secio protocol over any connection that is opened
|
|
|
|
//! through it.
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-07 15:34:22 +01:00
|
|
|
//! ```no_run
|
|
|
|
//! # fn main() {
|
|
|
|
//! use futures::Future;
|
2019-03-11 13:42:53 +01:00
|
|
|
//! use libp2p_secio::{SecioConfig, SecioOutput};
|
|
|
|
//! use libp2p_core::{Multiaddr, identity, upgrade::apply_inbound};
|
2018-11-15 17:41:11 +01:00
|
|
|
//! use libp2p_core::transport::Transport;
|
2018-12-10 13:39:11 +01:00
|
|
|
//! use libp2p_tcp::TcpConfig;
|
2017-12-07 15:34:22 +01:00
|
|
|
//! use tokio_io::io::write_all;
|
2018-10-25 05:26:37 -04:00
|
|
|
//! use tokio::runtime::current_thread::Runtime;
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2018-11-15 17:41:11 +01:00
|
|
|
//! let dialer = TcpConfig::new()
|
2018-03-07 16:20:55 +01:00
|
|
|
//! .with_upgrade({
|
2019-03-11 13:42:53 +01:00
|
|
|
//! # let private_key = &mut [];
|
|
|
|
//! // See the documentation of `identity::Keypair`.
|
|
|
|
//! let keypair = identity::Keypair::rsa_from_pkcs8(private_key).unwrap();
|
2018-11-15 17:41:11 +01:00
|
|
|
//! SecioConfig::new(keypair)
|
|
|
|
//! })
|
|
|
|
//! .map(|out: SecioOutput<_>, _| out.stream);
|
2018-05-14 15:55:16 +02:00
|
|
|
//!
|
2018-11-15 17:41:11 +01:00
|
|
|
//! let future = dialer.dial("/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap())
|
2019-01-10 11:27:06 +01:00
|
|
|
//! .unwrap()
|
|
|
|
//! .map_err(|e| panic!("error: {:?}", e))
|
2018-10-17 10:17:40 +01:00
|
|
|
//! .and_then(|connection| {
|
2018-03-07 16:20:55 +01:00
|
|
|
//! // Sends "hello world" on the connection, will be encrypted.
|
|
|
|
//! write_all(connection, "hello world")
|
2018-11-15 17:41:11 +01:00
|
|
|
//! })
|
|
|
|
//! .map_err(|e| panic!("error: {:?}", e));
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2018-10-25 05:26:37 -04:00
|
|
|
//! let mut rt = Runtime::new().unwrap();
|
|
|
|
//! let _ = rt.block_on(future).unwrap();
|
2017-12-07 15:34:22 +01:00
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! # Manual usage
|
|
|
|
//!
|
|
|
|
//! > **Note**: You are encouraged to use `SecioConfig` as described above.
|
|
|
|
//!
|
2017-10-30 10:22:38 +01:00
|
|
|
//! You can add the `secio` layer over a socket by calling `SecioMiddleware::handshake()`. This
|
|
|
|
//! method will perform a handshake with the host, and return a future that corresponds to the
|
|
|
|
//! moment when the handshake succeeds or errored. On success, the future produces a
|
|
|
|
//! `SecioMiddleware` that implements `Sink` and `Stream` and can be used to send packets of data.
|
2017-11-02 11:58:02 +01:00
|
|
|
//!
|
2017-10-30 10:22:38 +01:00
|
|
|
|
|
|
|
pub use self::error::SecioError;
|
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
use bytes::BytesMut;
|
2017-11-02 11:58:02 +01:00
|
|
|
use futures::stream::MapErr as StreamMapErr;
|
2018-05-14 15:55:16 +02:00
|
|
|
use futures::{Future, Poll, Sink, StartSend, Stream};
|
2019-03-19 17:27:30 +01:00
|
|
|
use libp2p_core::{PublicKey, identity, upgrade::{UpgradeInfo, InboundUpgrade, OutboundUpgrade, Negotiated}};
|
2018-12-13 18:54:28 +01:00
|
|
|
use log::debug;
|
2017-11-02 11:58:02 +01:00
|
|
|
use rw_stream_sink::RwStreamSink;
|
|
|
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
|
|
|
use std::iter;
|
2017-10-30 10:22:38 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
|
|
mod algo_support;
|
|
|
|
mod codec;
|
|
|
|
mod error;
|
2018-10-01 15:42:40 +02:00
|
|
|
mod exchange;
|
2017-10-30 10:22:38 +01:00
|
|
|
mod handshake;
|
2018-09-07 14:05:42 +02:00
|
|
|
mod structs_proto;
|
2018-09-17 10:05:37 +02:00
|
|
|
mod stream_cipher;
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-12-13 18:54:28 +01:00
|
|
|
pub use crate::algo_support::Digest;
|
|
|
|
pub use crate::exchange::KeyAgreement;
|
|
|
|
pub use crate::stream_cipher::Cipher;
|
2018-09-12 09:10:05 +02:00
|
|
|
|
2018-05-16 12:59:36 +02:00
|
|
|
/// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies
|
2017-11-02 11:58:02 +01:00
|
|
|
/// secio on any connection.
|
|
|
|
#[derive(Clone)]
|
2017-12-04 15:50:14 +01:00
|
|
|
pub struct SecioConfig {
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Private and public keys of the local node.
|
2019-03-11 13:42:53 +01:00
|
|
|
pub(crate) key: identity::Keypair,
|
2018-09-12 09:10:05 +02:00
|
|
|
pub(crate) agreements_prop: Option<String>,
|
|
|
|
pub(crate) ciphers_prop: Option<String>,
|
|
|
|
pub(crate) digests_prop: Option<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SecioConfig {
|
|
|
|
/// Create a new `SecioConfig` with the given keypair.
|
2019-03-11 13:42:53 +01:00
|
|
|
pub fn new(kp: identity::Keypair) -> Self {
|
2018-09-12 09:10:05 +02:00
|
|
|
SecioConfig {
|
|
|
|
key: kp,
|
|
|
|
agreements_prop: None,
|
|
|
|
ciphers_prop: None,
|
|
|
|
digests_prop: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the default set of supported key agreement algorithms.
|
|
|
|
pub fn key_agreements<'a, I>(mut self, xs: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item=&'a KeyAgreement>
|
|
|
|
{
|
|
|
|
self.agreements_prop = Some(algo_support::key_agreements_proposition(xs));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the default set of supported ciphers.
|
|
|
|
pub fn ciphers<'a, I>(mut self, xs: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item=&'a Cipher>
|
|
|
|
{
|
|
|
|
self.ciphers_prop = Some(algo_support::ciphers_proposition(xs));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the default set of supported digest algorithms.
|
|
|
|
pub fn digests<'a, I>(mut self, xs: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item=&'a Digest>
|
|
|
|
{
|
|
|
|
self.digests_prop = Some(algo_support::digests_proposition(xs));
|
|
|
|
self
|
|
|
|
}
|
2018-11-15 17:41:11 +01:00
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
fn handshake<T>(self, socket: T) -> impl Future<Item=SecioOutput<T>, Error=SecioError>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Send + 'static
|
|
|
|
{
|
|
|
|
debug!("Starting secio upgrade");
|
|
|
|
SecioMiddleware::handshake(socket, self)
|
|
|
|
.map(|(stream_sink, pubkey, ephemeral)| {
|
|
|
|
let mapped = stream_sink.map_err(map_err as fn(_) -> _);
|
|
|
|
SecioOutput {
|
|
|
|
stream: RwStreamSink::new(mapped),
|
|
|
|
remote_key: pubkey,
|
|
|
|
ephemeral_public_key: ephemeral
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-11-02 11:58:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:07:38 +02:00
|
|
|
/// Output of the secio protocol.
|
|
|
|
pub struct SecioOutput<S>
|
2018-07-11 11:14:40 +02:00
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
2018-07-04 17:07:38 +02:00
|
|
|
{
|
|
|
|
/// The encrypted stream.
|
|
|
|
pub stream: RwStreamSink<StreamMapErr<SecioMiddleware<S>, fn(SecioError) -> IoError>>,
|
|
|
|
/// The public key of the remote.
|
|
|
|
pub remote_key: PublicKey,
|
|
|
|
/// Ephemeral public key used during the negotiation.
|
|
|
|
pub ephemeral_public_key: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
impl UpgradeInfo for SecioConfig {
|
2018-12-11 15:13:10 +01:00
|
|
|
type Info = &'static [u8];
|
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
2017-11-02 11:58:02 +01:00
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
|
|
iter::once(b"/secio/1.0.0")
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
2017-11-02 11:58:02 +01:00
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
impl<T> InboundUpgrade<T> for SecioConfig
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Send + 'static
|
|
|
|
{
|
2019-03-19 17:27:30 +01:00
|
|
|
type Output = SecioOutput<Negotiated<T>>;
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error = SecioError;
|
|
|
|
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
|
2018-02-14 13:02:56 +01:00
|
|
|
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_inbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
|
2018-12-11 15:13:10 +01:00
|
|
|
Box::new(self.handshake(socket))
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OutboundUpgrade<T> for SecioConfig
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Send + 'static
|
|
|
|
{
|
2019-03-19 17:27:30 +01:00
|
|
|
type Output = SecioOutput<Negotiated<T>>;
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error = SecioError;
|
|
|
|
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
|
|
|
|
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_outbound(self, socket: Negotiated<T>, _: Self::Info) -> Self::Future {
|
2018-12-11 15:13:10 +01:00
|
|
|
Box::new(self.handshake(socket))
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
2017-11-02 11:58:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn map_err(err: SecioError) -> IoError {
|
2018-05-17 15:14:13 +02:00
|
|
|
debug!("error during secio handshake {:?}", err);
|
2018-03-07 16:20:55 +01:00
|
|
|
IoError::new(IoErrorKind::InvalidData, err)
|
2017-11-02 11:58:02 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 10:22:38 +01:00
|
|
|
/// Wraps around an object that implements `AsyncRead` and `AsyncWrite`.
|
|
|
|
///
|
|
|
|
/// Implements `Sink` and `Stream` whose items are frames of data. Each frame is encoded
|
|
|
|
/// individually, so you are encouraged to group data in few frames if possible.
|
|
|
|
pub struct SecioMiddleware<S> {
|
2018-03-07 16:20:55 +01:00
|
|
|
inner: codec::FullCodec<S>,
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> SecioMiddleware<S>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
2018-09-06 09:54:35 +02:00
|
|
|
S: AsyncRead + AsyncWrite + Send,
|
2017-10-30 10:22:38 +01:00
|
|
|
{
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Attempts to perform a handshake on the given socket.
|
|
|
|
///
|
|
|
|
/// On success, produces a `SecioMiddleware` that can then be used to encode/decode
|
2018-07-04 17:07:38 +02:00
|
|
|
/// communications, plus the public key of the remote, plus the ephemeral public key.
|
2018-11-21 10:39:48 +01:00
|
|
|
pub fn handshake(socket: S, config: SecioConfig)
|
|
|
|
-> impl Future<Item = (SecioMiddleware<S>, PublicKey, Vec<u8>), Error = SecioError>
|
2018-03-07 16:20:55 +01:00
|
|
|
{
|
2018-11-21 10:39:48 +01:00
|
|
|
handshake::handshake(socket, config).map(|(inner, pubkey, ephemeral)| {
|
2018-05-14 15:55:16 +02:00
|
|
|
let inner = SecioMiddleware { inner };
|
2018-07-04 17:07:38 +02:00
|
|
|
(inner, pubkey, ephemeral)
|
2018-11-21 10:39:48 +01:00
|
|
|
})
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Sink for SecioMiddleware<S>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
2017-10-30 10:22:38 +01:00
|
|
|
{
|
2018-03-07 16:20:55 +01:00
|
|
|
type SinkItem = BytesMut;
|
|
|
|
type SinkError = IoError;
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
|
|
|
self.inner.start_send(item)
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.inner.poll_complete()
|
|
|
|
}
|
2018-09-17 15:01:37 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.inner.close()
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Stream for SecioMiddleware<S>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
S: AsyncRead + AsyncWrite,
|
2017-10-30 10:22:38 +01:00
|
|
|
{
|
2018-03-07 16:20:55 +01:00
|
|
|
type Item = Vec<u8>;
|
|
|
|
type Error = SecioError;
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
self.inner.poll()
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|