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.
|
|
|
|
|
|
|
|
//! Individual messages decoding.
|
|
|
|
|
|
|
|
use bytes::BytesMut;
|
2018-09-07 14:05:42 +02:00
|
|
|
use codec::StreamCipher;
|
2017-10-30 10:22:38 +01:00
|
|
|
use error::SecioError;
|
2018-07-11 11:14:40 +02:00
|
|
|
use futures::sink::Sink;
|
|
|
|
use futures::stream::Stream;
|
2017-10-30 10:22:38 +01:00
|
|
|
use futures::Async;
|
|
|
|
use futures::Poll;
|
|
|
|
use futures::StartSend;
|
|
|
|
use ring::hmac;
|
|
|
|
|
|
|
|
/// Wraps around a `Stream<Item = BytesMut>`. The buffers produced by the underlying stream
|
|
|
|
/// are decoded using the cipher and hmac.
|
|
|
|
///
|
|
|
|
/// This struct implements `Stream`, whose stream item are frames of data without the length
|
|
|
|
/// prefix. The mechanism for removing the length prefix and splitting the incoming data into
|
|
|
|
/// frames isn't handled by this module.
|
|
|
|
///
|
|
|
|
/// Also implements `Sink` for convenience.
|
|
|
|
pub struct DecoderMiddleware<S> {
|
2018-08-15 17:00:57 +02:00
|
|
|
cipher_state: StreamCipher,
|
2018-03-07 16:20:55 +01:00
|
|
|
hmac_key: hmac::VerificationKey,
|
2018-05-31 14:24:49 +02:00
|
|
|
// TODO: when a new version of ring is released, we can use `hmac_key.digest_algorithm().output_len` instead
|
|
|
|
hmac_num_bytes: usize,
|
2018-03-07 16:20:55 +01:00
|
|
|
raw_stream: S,
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> DecoderMiddleware<S> {
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn new(
|
|
|
|
raw_stream: S,
|
2018-08-15 17:00:57 +02:00
|
|
|
cipher: StreamCipher,
|
2018-03-07 16:20:55 +01:00
|
|
|
hmac_key: hmac::VerificationKey,
|
2018-07-11 11:14:40 +02:00
|
|
|
hmac_num_bytes: usize, // TODO: remove this parameter
|
2018-03-07 16:20:55 +01:00
|
|
|
) -> DecoderMiddleware<S> {
|
|
|
|
DecoderMiddleware {
|
|
|
|
cipher_state: cipher,
|
2018-05-31 14:24:49 +02:00
|
|
|
hmac_key,
|
|
|
|
raw_stream,
|
|
|
|
hmac_num_bytes,
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Stream for DecoderMiddleware<S>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
S: Stream<Item = BytesMut>,
|
|
|
|
S::Error: Into<SecioError>,
|
2017-10-30 10:22:38 +01:00
|
|
|
{
|
2018-03-07 16:20:55 +01:00
|
|
|
type Item = Vec<u8>;
|
|
|
|
type Error = SecioError;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
let frame = match self.raw_stream.poll() {
|
|
|
|
Ok(Async::Ready(Some(t))) => t,
|
|
|
|
Ok(Async::Ready(None)) => return Ok(Async::Ready(None)),
|
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
};
|
|
|
|
|
2018-05-31 14:24:49 +02:00
|
|
|
// TODO: when a new version of ring is released, we can use `hmac_key.digest_algorithm().output_len` instead
|
|
|
|
let hmac_num_bytes = self.hmac_num_bytes;
|
2018-03-07 16:20:55 +01:00
|
|
|
|
|
|
|
if frame.len() < hmac_num_bytes {
|
2018-05-17 15:14:13 +02:00
|
|
|
debug!("frame too short when decoding secio frame");
|
2018-03-07 16:20:55 +01:00
|
|
|
return Err(SecioError::FrameTooShort);
|
|
|
|
}
|
2018-08-10 18:27:20 +02:00
|
|
|
|
2018-09-07 14:05:42 +02:00
|
|
|
let (crypted_data, expected_hash) = frame.split_at(frame.len() - hmac_num_bytes);
|
|
|
|
debug_assert_eq!(expected_hash.len(), hmac_num_bytes);
|
|
|
|
|
|
|
|
if hmac::verify(&self.hmac_key, crypted_data, expected_hash).is_err() {
|
|
|
|
debug!("hmac mismatch when decoding secio frame");
|
|
|
|
return Err(SecioError::HmacNotMatching);
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:05:42 +02:00
|
|
|
// Note that there is no way to decipher in place with rust-crypto right now.
|
|
|
|
let mut decrypted_data = crypted_data.to_vec();
|
2018-03-07 16:20:55 +01:00
|
|
|
self.cipher_state
|
2018-09-07 14:05:42 +02:00
|
|
|
.process(&crypted_data, &mut decrypted_data);
|
2018-03-07 16:20:55 +01:00
|
|
|
|
2018-09-07 14:05:42 +02:00
|
|
|
Ok(Async::Ready(Some(decrypted_data)))
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Sink for DecoderMiddleware<S>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
S: Sink,
|
2017-10-30 10:22:38 +01:00
|
|
|
{
|
2018-03-07 16:20:55 +01:00
|
|
|
type SinkItem = S::SinkItem;
|
|
|
|
type SinkError = S::SinkError;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
|
|
|
self.raw_stream.start_send(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.raw_stream.poll_complete()
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|