use std::{fmt, io, string};
use crate::ssh::AllowedSignerParsingError;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Decode(base64::DecodeError),
Utf8Error(string::FromUtf8Error),
InvalidCertType(u32),
InvalidFormat,
UnexpectedEof,
NotCertificate,
KeyTypeMismatch,
InvalidSignature,
InvalidAllowedSigner(AllowedSignerParsingError),
InvalidAllowedSigners(AllowedSignerParsingError, usize),
SigningError,
EncryptedPrivateKey,
EncryptedPrivateKeyNotSupported,
UnknownKeyType(String),
UnknownCurve(String),
ParsingError,
#[cfg(feature = "yubikey-support")]
YubikeyPIVError(crate::yubikey::piv::Error),
#[cfg(any(feature = "fido-support", feature = "fido-lite"))]
FidoError(crate::fido::Error),
Unsupported,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(ref err) => err.fmt(f),
Error::Decode(ref err) => err.fmt(f),
Error::Utf8Error(ref err) => err.fmt(f),
Error::InvalidFormat => write!(f, "Invalid format"),
Error::InvalidCertType(v) => write!(f, "Invalid certificate type with value {}", v),
Error::UnexpectedEof => write!(f, "Unexpected EOF reached while reading data"),
Error::NotCertificate => write!(f, "Not a certificate"),
Error::KeyTypeMismatch => write!(f, "Key type mismatch"),
Error::InvalidSignature => write!(f, "Data is improperly signed"),
Error::InvalidAllowedSigner(ref v) => write!(f, "Invalid allowed signer format: {}", v),
Error::InvalidAllowedSigners(ref v, line) => write!(f, "Invalid allowed signer format on line {}: {}", line, v),
Error::SigningError => write!(f, "Could not sign data"),
Error::EncryptedPrivateKey => write!(f, "Encountered encrypted private key with no decryption key"),
Error::EncryptedPrivateKeyNotSupported => write!(f, "This method of private key encryption is not supported or sshcerts was not compiled with encrypted private key support"),
Error::UnknownKeyType(ref v) => write!(f, "Unknown key type {}", v),
Error::UnknownCurve(ref v) => write!(f, "Unknown curve {}", v),
Error::ParsingError => write!(f, "Could not parse the data provided"),
#[cfg(feature = "yubikey-support")]
Error::YubikeyPIVError(ref e) => write!(f, "{}", e),
#[cfg(any(feature = "fido-support", feature = "fido-lite"))]
Error::FidoError(ref e) => write!(f, "{}", e),
Error::Unsupported => write!(f, "Functionality either not implemented or cannot be technically supported"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(ref e) => e.source(),
Error::Decode(ref e) => e.source(),
Error::Utf8Error(ref e) => e.source(),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}
impl From<base64::DecodeError> for Error {
fn from(error: base64::DecodeError) -> Error {
Error::Decode(error)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(error: string::FromUtf8Error) -> Error {
Error::Utf8Error(error)
}
}
impl From<ring::error::Unspecified> for Error {
fn from(_: ring::error::Unspecified) -> Error {
Error::InvalidSignature
}
}
#[cfg(feature = "rsa-signing")]
impl From<simple_asn1::ASN1EncodeErr> for Error {
fn from(_e: simple_asn1::ASN1EncodeErr) -> Self {
Error::InvalidFormat
}
}