authenticator_ctap2_2021/transport/
errors.rs

1use crate::consts::{SW_CONDITIONS_NOT_SATISFIED, SW_NO_ERROR, SW_WRONG_DATA, SW_WRONG_LENGTH};
2use crate::ctap2::commands::CommandError;
3use std::fmt;
4use std::io;
5use std::path;
6
7#[allow(unused)]
8#[derive(Debug, PartialEq, Eq)]
9pub enum ApduErrorStatus {
10    ConditionsNotSatisfied,
11    WrongData,
12    WrongLength,
13    Unknown([u8; 2]),
14}
15
16impl ApduErrorStatus {
17    pub fn from(status: [u8; 2]) -> Result<(), ApduErrorStatus> {
18        match status {
19            s if s == SW_NO_ERROR => Ok(()),
20            s if s == SW_CONDITIONS_NOT_SATISFIED => Err(ApduErrorStatus::ConditionsNotSatisfied),
21            s if s == SW_WRONG_DATA => Err(ApduErrorStatus::WrongData),
22            s if s == SW_WRONG_LENGTH => Err(ApduErrorStatus::WrongLength),
23            other => Err(ApduErrorStatus::Unknown(other)),
24        }
25    }
26}
27
28impl fmt::Display for ApduErrorStatus {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match *self {
31            ApduErrorStatus::ConditionsNotSatisfied => write!(f, "Apdu: condition not satisfied"),
32            ApduErrorStatus::WrongData => write!(f, "Apdu: wrong data"),
33            ApduErrorStatus::WrongLength => write!(f, "Apdu: wrong length"),
34            ApduErrorStatus::Unknown(ref u) => write!(f, "Apdu: unknown error: {:?}", u),
35        }
36    }
37}
38
39#[allow(unused)]
40#[derive(Debug)]
41pub enum HIDError {
42    /// Transport replied with a status not expected
43    DeviceError,
44    UnexpectedInitReplyLen,
45    NonceMismatch,
46    DeviceNotInitialized,
47    DeviceNotSupported,
48    UnsupportedCommand,
49    UnexpectedVersion,
50    IO(Option<path::PathBuf>, io::Error),
51    UnexpectedCmd(u8),
52    Command(CommandError),
53    ApduStatus(ApduErrorStatus),
54}
55
56impl From<io::Error> for HIDError {
57    fn from(e: io::Error) -> HIDError {
58        HIDError::IO(None, e)
59    }
60}
61
62impl From<CommandError> for HIDError {
63    fn from(e: CommandError) -> HIDError {
64        HIDError::Command(e)
65    }
66}
67
68impl From<ApduErrorStatus> for HIDError {
69    fn from(e: ApduErrorStatus) -> HIDError {
70        HIDError::ApduStatus(e)
71    }
72}
73
74impl fmt::Display for HIDError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match *self {
77            HIDError::UnexpectedInitReplyLen => {
78                write!(f, "Error: Unexpected reply len when initilizaling")
79            }
80            HIDError::NonceMismatch => write!(f, "Error: Nonce mismatch"),
81            HIDError::DeviceError => write!(f, "Error: device returned error"),
82            HIDError::DeviceNotInitialized => write!(f, "Error: using not initiliazed device"),
83            HIDError::DeviceNotSupported => {
84                write!(f, "Error: requested operation is not available on device")
85            }
86            HIDError::UnexpectedVersion => write!(f, "Error: Unexpected protocol version"),
87            HIDError::UnsupportedCommand => {
88                write!(f, "Error: command is not supported on this device")
89            }
90            HIDError::IO(ref p, ref e) => write!(f, "Error: Ioerror({:?}): {}", p, e),
91            HIDError::Command(ref e) => write!(f, "Error: Error issuing command: {}", e),
92            HIDError::UnexpectedCmd(s) => write!(f, "Error: Unexpected status: {}", s),
93            HIDError::ApduStatus(ref status) => {
94                write!(f, "Error: Unexpected apdu status: {:?}", status)
95            }
96        }
97    }
98}