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.
|
|
|
|
|
|
|
|
//! Defines the `SecioError` enum that groups all possible errors in SECIO.
|
|
|
|
|
2018-08-10 18:27:20 +02:00
|
|
|
use aes_ctr::stream_cipher::LoopError;
|
2017-10-30 10:22:38 +01:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io::Error as IoError;
|
|
|
|
|
|
|
|
/// Error at the SECIO layer communication.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
/// I/O error.
|
|
|
|
IoError(IoError),
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Failed to parse one of the handshake protobuf messages.
|
|
|
|
HandshakeParsingFailure,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// There is no protocol supported by both the local and remote hosts.
|
|
|
|
NoSupportIntersection(&'static str, String),
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Failed to generate nonce.
|
|
|
|
NonceGenerationFailed,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Failed to generate ephemeral key.
|
|
|
|
EphemeralKeyGenerationFailed,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Failed to sign a message with our local private key.
|
|
|
|
SigningFailure,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// The signature of the exchange packet doesn't verify the remote public key.
|
|
|
|
SignatureVerificationFailed,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// Failed to generate the secret shared key from the ephemeral key.
|
|
|
|
SecretGenerationFailed,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// The final check of the handshake failed.
|
|
|
|
NonceVerificationFailed,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-08-10 18:27:20 +02:00
|
|
|
/// Error with block cipher.
|
|
|
|
CipherError(LoopError),
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// The received frame was of invalid length.
|
|
|
|
FrameTooShort,
|
2017-10-30 10:22:38 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
/// The hashes of the message didn't match.
|
|
|
|
HmacNotMatching,
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
SecioError::IoError(_) => "I/O error",
|
|
|
|
SecioError::HandshakeParsingFailure => {
|
|
|
|
"Failed to parse one of the handshake protobuf messages"
|
|
|
|
}
|
|
|
|
SecioError::NoSupportIntersection(_, _) => {
|
|
|
|
"There is no protocol supported by both the local and remote hosts"
|
|
|
|
}
|
|
|
|
SecioError::NonceGenerationFailed => "Failed to generate nonce",
|
|
|
|
SecioError::EphemeralKeyGenerationFailed => "Failed to generate ephemeral key",
|
|
|
|
SecioError::SigningFailure => "Failed to sign a message with our local private key",
|
|
|
|
SecioError::SignatureVerificationFailed => {
|
|
|
|
"The signature of the exchange packet doesn't verify the remote public key"
|
|
|
|
}
|
|
|
|
SecioError::SecretGenerationFailed => {
|
|
|
|
"Failed to generate the secret shared key from the ephemeral key"
|
|
|
|
}
|
|
|
|
SecioError::NonceVerificationFailed => "The final check of the handshake failed",
|
|
|
|
SecioError::CipherError(_) => "Error while decoding/encoding data",
|
|
|
|
SecioError::FrameTooShort => "The received frame was of invalid length",
|
|
|
|
SecioError::HmacNotMatching => "The hashes of the message didn't match",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
match *self {
|
|
|
|
SecioError::IoError(ref err) => Some(err),
|
|
|
|
// TODO: The type doesn't implement `Error`
|
|
|
|
/*SecioError::CipherError(ref err) => {
|
|
|
|
Some(err)
|
|
|
|
},*/
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(fmt, "{}", error::Error::description(self))
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:27:20 +02:00
|
|
|
impl From<LoopError> for SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
2018-08-10 18:27:20 +02:00
|
|
|
fn from(err: LoopError) -> SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
SecioError::CipherError(err)
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<IoError> for SecioError {
|
2018-03-07 16:20:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn from(err: IoError) -> SecioError {
|
|
|
|
SecioError::IoError(err)
|
|
|
|
}
|
2017-10-30 10:22:38 +01:00
|
|
|
}
|