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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Errors raised by this library

use crate::messages::*;
use ascii::FromAsciiError;
use failure::*;
use failure_derive::Fail;
use red_asn1;
use std::fmt;
use std::result;
use std::string::FromUtf8Error;
use nom::Err as NomError;
use kerberos_crypto;

/// Result to wrap kerbeiros error.
pub type Result<T> = result::Result<T, Error>;

/// Error returned by functions of the kerbeiros library.
#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>,
}

/// Type of error in kerbeiros library.
#[derive(Clone, PartialEq, Debug, Fail)]
pub enum ErrorKind {
    /// Error handlening asn1 entities.
    #[fail(display = "Asn1 error: {}", _0)]
    Asn1Error(red_asn1::ErrorKind),

    /// Error produced in the application of cryptographic algorithms.
    #[fail(display = "Cryptography error: {}", _0)]
    CryptographyError(kerberos_crypto::Error),

    /// Invalid ascii string.
    #[fail(display = "Invalid ascii string")]
    InvalidAscii,

    /// Invalid utf8 string.
    #[fail(display = "Invalid utf-8 string")]
    InvalidUtf8,

    /// Invalid microseconds value. Minimum = 0, Maximum = 999999.
    #[fail(display = "Invalid microseconds value {}. Max is 999999", _0)]
    InvalidMicroseconds(u32),

    /// Error in i/o operation.
    #[fail(display = "Error in i/o operation")]
    IOError,

    /// Invalid key
    #[fail(
        display = "Invalid key: Only hexadecimal characters are allowed [1234567890abcdefABCDEF]"
    )]
    InvalidKeyCharset,

    /// Invalid key
    #[fail(display = "Invalid key: Length should be {}", _0)]
    InvalidKeyLength(usize),

    /// Received KRB-ERROR response.
    #[fail(display = "Received {}", _0)]
    KrbErrorResponse(KrbError),

    /// Error resolving name.
    #[fail(display = "Error resolving name: {}", _0)]
    NameResolutionError(String),

    /// Error sending/receiving data over the network.
    #[fail(display = "Network error")]
    NetworkError,

    /// No key was provided in order to decrypt the KDC response.
    #[fail(display = "No key was provided")]
    NoKeyProvided,

    /// None cipher algorithm supported was specified.
    #[fail(display = "None cipher algorithm supported was specified")]
    NoProvidedSupportedCipherAlgorithm,

    /// Some necessary data was not available in order to build the required message.
    #[fail(display = "Not available data {}", _0)]
    NotAvailableData(String),

    /// Error parsing KDC-REP message.
    #[fail(display = "Error parsing KdcRep: {}", _1)]
    ParseKdcRepError(KdcRep, Box<ErrorKind>),

    /// The type of the principal name was not specified.
    #[fail(display = "Undefined type of principal name: {}", _0)]
    PrincipalNameTypeUndefined(String),

    /// No principal name
    #[fail(display = "No principal name found")]
    NoPrincipalName,

    /// No address found
    #[fail(display = "No address found")]
    NoAddress,

    /// Error parsing binary data
    #[fail(display = "Error parsing binary data")]
    BinaryParseError,
}

impl Error {
    pub fn kind(&self) -> &ErrorKind {
        return self.inner.get_context();
    }
}

impl Fail for Error {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Error {
        return Error {
            inner: Context::new(kind),
        };
    }
}

impl From<Context<ErrorKind>> for Error {
    fn from(inner: Context<ErrorKind>) -> Error {
        return Error { inner };
    }
}

impl From<kerberos_crypto::Error> for Error {
    fn from(kind: kerberos_crypto::Error) -> Error {
        return Error {
            inner: Context::new(ErrorKind::CryptographyError(kind)),
        };
    }
}

impl From<FromAsciiError<&str>> for Error {
    fn from(_error: FromAsciiError<&str>) -> Self {
        return Error {
            inner: Context::new(ErrorKind::InvalidAscii),
        };
    }
}

impl From<FromAsciiError<Vec<u8>>> for Error {
    fn from(_error: FromAsciiError<Vec<u8>>) -> Self {
        return Error {
            inner: Context::new(ErrorKind::InvalidAscii),
        };
    }
}

impl From<FromUtf8Error> for Error {
    fn from(_error: FromUtf8Error) -> Self {
        return Error {
            inner: Context::new(ErrorKind::InvalidUtf8),
        };
    }
}

impl From<red_asn1::Error> for Error {
    fn from(error: red_asn1::Error) -> Self {
        return Error {
            inner: Context::new(ErrorKind::Asn1Error(error.kind().clone())),
        };
    }
}

impl<E> From<NomError<E>> for Error {
    fn from(_error: NomError<E>) -> Self {
        return Error {
            inner: Context::new(ErrorKind::BinaryParseError),
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_kerberos_error() {
        match produce_invalid_network_error() {
            Err(kerberos_error) => match kerberos_error.kind() {
                ErrorKind::NetworkError => {}
                _ => {
                    unreachable!();
                }
            },
            _ => {
                unreachable!();
            }
        }
    }

    fn produce_invalid_network_error() -> Result<()> {
        Err(ErrorKind::NetworkError)?;
        unreachable!();
    }
}