summaryrefslogtreecommitdiffstats
path: root/third_party/rust/authenticator/src/transport/errors.rs
blob: 451c27d6e8d436c172e34fd99257d56d2f14dc6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::consts::{SW_CONDITIONS_NOT_SATISFIED, SW_NO_ERROR, SW_WRONG_DATA, SW_WRONG_LENGTH};
use crate::ctap2::commands::CommandError;
use std::fmt;
use std::io;
use std::path;

#[allow(unused)]
#[derive(Debug, PartialEq, Eq)]
pub enum ApduErrorStatus {
    ConditionsNotSatisfied,
    WrongData,
    WrongLength,
    Unknown([u8; 2]),
}

impl ApduErrorStatus {
    pub fn from(status: [u8; 2]) -> Result<(), ApduErrorStatus> {
        match status {
            s if s == SW_NO_ERROR => Ok(()),
            s if s == SW_CONDITIONS_NOT_SATISFIED => Err(ApduErrorStatus::ConditionsNotSatisfied),
            s if s == SW_WRONG_DATA => Err(ApduErrorStatus::WrongData),
            s if s == SW_WRONG_LENGTH => Err(ApduErrorStatus::WrongLength),
            other => Err(ApduErrorStatus::Unknown(other)),
        }
    }
}

impl fmt::Display for ApduErrorStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ApduErrorStatus::ConditionsNotSatisfied => write!(f, "Apdu: condition not satisfied"),
            ApduErrorStatus::WrongData => write!(f, "Apdu: wrong data"),
            ApduErrorStatus::WrongLength => write!(f, "Apdu: wrong length"),
            ApduErrorStatus::Unknown(ref u) => write!(f, "Apdu: unknown error: {u:?}"),
        }
    }
}

#[allow(unused)]
#[derive(Debug)]
pub enum HIDError {
    /// Transport replied with a status not expected
    DeviceError,
    UnexpectedInitReplyLen,
    NonceMismatch,
    DeviceNotInitialized,
    DeviceNotSupported,
    UnsupportedCommand,
    UnexpectedVersion,
    IO(Option<path::PathBuf>, io::Error),
    UnexpectedCmd(u8),
    Command(CommandError),
    ApduStatus(ApduErrorStatus),
}

impl From<io::Error> for HIDError {
    fn from(e: io::Error) -> HIDError {
        HIDError::IO(None, e)
    }
}

impl From<CommandError> for HIDError {
    fn from(e: CommandError) -> HIDError {
        HIDError::Command(e)
    }
}

impl From<ApduErrorStatus> for HIDError {
    fn from(e: ApduErrorStatus) -> HIDError {
        HIDError::ApduStatus(e)
    }
}

impl fmt::Display for HIDError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            HIDError::UnexpectedInitReplyLen => {
                write!(f, "Error: Unexpected reply len when initilizaling")
            }
            HIDError::NonceMismatch => write!(f, "Error: Nonce mismatch"),
            HIDError::DeviceError => write!(f, "Error: device returned error"),
            HIDError::DeviceNotInitialized => write!(f, "Error: using not initiliazed device"),
            HIDError::DeviceNotSupported => {
                write!(f, "Error: requested operation is not available on device")
            }
            HIDError::UnexpectedVersion => write!(f, "Error: Unexpected protocol version"),
            HIDError::UnsupportedCommand => {
                write!(f, "Error: command is not supported on this device")
            }
            HIDError::IO(ref p, ref e) => write!(f, "Error: Ioerror({p:?}): {e}"),
            HIDError::Command(ref e) => write!(f, "Error: Error issuing command: {e}"),
            HIDError::UnexpectedCmd(s) => write!(f, "Error: Unexpected status: {s}"),
            HIDError::ApduStatus(ref status) => {
                write!(f, "Error: Unexpected apdu status: {status:?}")
            }
        }
    }
}