u2f/
u2ferror.rs

1use std::error;
2use std::fmt;
3
4#[derive(Debug)]
5pub enum U2fError {
6    Asm1DecoderError,
7    BadSignature,
8    RandomSecureBytesError,
9    InvalidReservedByte,
10    ChallengeExpired,
11    WrongKeyHandler,
12    InvalidClientData,
13    InvalidSignatureData,
14    InvalidUserPresenceByte,
15    BadCertificate,
16    NotTrustedAnchor,
17    CounterTooLow,
18    OpenSSLNoCurveName,
19    InvalidPublicKey,
20    OpenSSLError(openssl::error::ErrorStack),
21}
22
23impl fmt::Display for U2fError {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        match &self {
26            U2fError::Asm1DecoderError => write!(f, "ASM1 Decoder error"),
27            U2fError::BadSignature => write!(f, "Not able to verify signature"),
28            U2fError::RandomSecureBytesError => write!(f, "Not able to generate random bytes"),
29            U2fError::InvalidReservedByte => write!(f, "Invalid Reserved Byte"),
30            U2fError::ChallengeExpired => write!(f, "Challenge Expired"),
31            U2fError::WrongKeyHandler => write!(f, "Wrong Key Handler"),
32            U2fError::InvalidClientData => write!(f, "Invalid Client Data"),
33            U2fError::InvalidSignatureData => write!(f, "Invalid Signature Data"),
34            U2fError::InvalidUserPresenceByte => write!(f, "Invalid User Presence Byte"),
35            U2fError::BadCertificate => write!(f, "Failed to parse certificate"),
36            U2fError::NotTrustedAnchor => write!(f, "Not Trusted Anchor"),
37            U2fError::CounterTooLow => write!(f, "Counter too low"),
38            U2fError::InvalidPublicKey => write!(f, "Invalid public key"),
39            U2fError::OpenSSLNoCurveName => write!(f, "OpenSSL no curve name"),
40            U2fError::OpenSSLError(e) => e.fmt(f),
41        }
42    }
43}
44
45impl error::Error for U2fError {
46    fn description(&self) -> &str {
47        match &self {
48            U2fError::Asm1DecoderError => "Error attempting to decode Asm1 message",
49            U2fError::BadSignature => "Error attempting to verify provided signature",
50            U2fError::RandomSecureBytesError => "Error attempting to generate random bytes",
51            U2fError::InvalidReservedByte => "Error attempting to parse Reserved Byte",
52            U2fError::ChallengeExpired => "Challenge has expired",
53            U2fError::WrongKeyHandler => "Wrong Key Handler",
54            U2fError::InvalidClientData => "Invalid Client Data",
55            U2fError::InvalidSignatureData => "Invalid Signature Data",
56            U2fError::InvalidUserPresenceByte => "Invalid User Presence Byte",
57            U2fError::BadCertificate => "Failed to parse certificate",
58            U2fError::NotTrustedAnchor => "Not Trusted Anchor",
59            U2fError::CounterTooLow => "Counter too low",
60            U2fError::InvalidPublicKey => "Invalid public key",
61            U2fError::OpenSSLNoCurveName => "OpenSSL no curve name",
62            U2fError::OpenSSLError(e) => e.description(),
63        }
64    }
65
66    fn cause(&self) -> Option<&dyn error::Error> {
67        match *self {
68            U2fError::Asm1DecoderError => None,
69            U2fError::BadSignature => None,
70            U2fError::RandomSecureBytesError => None,
71            U2fError::InvalidReservedByte => None,
72            U2fError::ChallengeExpired => None,
73            U2fError::WrongKeyHandler => None,
74            U2fError::InvalidClientData => None,
75            U2fError::InvalidSignatureData => None,
76            U2fError::InvalidUserPresenceByte => None,  
77            U2fError::BadCertificate => None,
78            U2fError::NotTrustedAnchor => None,
79            U2fError::CounterTooLow => None,
80            U2fError::InvalidPublicKey => None,
81            U2fError::OpenSSLNoCurveName => None,
82            U2fError::OpenSSLError(_) => None,
83        }
84    }
85}