use std::{error, fmt, io};
#[derive(Debug)]
pub enum SecioError {
IoError(io::Error),
#[cfg(unix)]
Openssl(openssl::error::ErrorStack),
CryptoError,
NotSupportKeyProvider,
EphemeralKeyGenerationFailed,
SecretGenerationFailed,
NoSupportIntersection,
NonceVerificationFailed,
FrameTooShort,
ConnectSelf,
HandshakeParsingFailure,
SignatureVerificationFailed,
InvalidMessage,
InvalidProposition(&'static str),
}
impl PartialEq for SecioError {
fn eq(&self, other: &SecioError) -> bool {
use self::SecioError::*;
match (self, other) {
(InvalidProposition(i), InvalidProposition(j)) => i == j,
(EphemeralKeyGenerationFailed, EphemeralKeyGenerationFailed)
| (SecretGenerationFailed, SecretGenerationFailed)
| (NoSupportIntersection, NoSupportIntersection)
| (NonceVerificationFailed, NonceVerificationFailed)
| (FrameTooShort, FrameTooShort)
| (ConnectSelf, ConnectSelf)
| (HandshakeParsingFailure, HandshakeParsingFailure)
| (SignatureVerificationFailed, SignatureVerificationFailed)
| (InvalidMessage, InvalidMessage)
| (NotSupportKeyProvider, NotSupportKeyProvider) => true,
_ => false,
}
}
}
impl From<io::Error> for SecioError {
#[inline]
fn from(err: io::Error) -> SecioError {
SecioError::IoError(err)
}
}
impl From<SecioError> for io::Error {
#[inline]
fn from(err: SecioError) -> io::Error {
match err {
SecioError::IoError(e) => e,
e => io::Error::new(io::ErrorKind::BrokenPipe, e.to_string()),
}
}
}
#[cfg(unix)]
impl From<openssl::error::ErrorStack> for SecioError {
fn from(err: openssl::error::ErrorStack) -> SecioError {
SecioError::Openssl(err)
}
}
#[cfg(not(target_family = "wasm"))]
impl From<ring::error::Unspecified> for SecioError {
fn from(_err: ring::error::Unspecified) -> SecioError {
SecioError::CryptoError
}
}
impl error::Error for SecioError {}
impl fmt::Display for SecioError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SecioError::IoError(e) => fmt::Display::fmt(&e, f),
#[cfg(unix)]
SecioError::Openssl(e) => fmt::Display::fmt(&e, f),
SecioError::CryptoError => write!(f, "Crypto Error"),
SecioError::EphemeralKeyGenerationFailed => write!(f, "EphemeralKey Generation Failed"),
SecioError::SecretGenerationFailed => write!(f, "Secret Generation Failed"),
SecioError::NoSupportIntersection => write!(f, "No Support Intersection"),
SecioError::NonceVerificationFailed => write!(f, "Nonce Verification Failed"),
SecioError::FrameTooShort => write!(f, "Frame Too Short"),
SecioError::ConnectSelf => write!(f, "Connect Self"),
SecioError::HandshakeParsingFailure => write!(f, "Handshake Parsing Failure"),
SecioError::InvalidMessage => write!(f, "Invalid Message"),
SecioError::SignatureVerificationFailed => write!(f, "Signature Verification Failed"),
SecioError::InvalidProposition(e) => write!(f, "Invalid Proposition: {}", e),
SecioError::NotSupportKeyProvider => write!(f, "Sign operation not supported"),
}
}
}