kpdb/types/
error.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <[email protected]>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use rust_crypto::symmetriccipher::SymmetricCipherError;
10use std::error;
11use std::fmt;
12use std::io;
13use xml::reader as xmlreader;
14use xml::writer as xmlwriter;
15
16/// Error type for database errors.
17#[derive(Debug)]
18pub enum Error {
19    /// Error during the encryption or decryption of the database.
20    CryptoError(SymmetricCipherError),
21
22    /// The hash of a data block is invalid.
23    InvalidBlockHash,
24
25    /// The data block has an invalid identifier.
26    InvalidBlockId(u32),
27
28    /// The database signature is invalid.
29    InvalidDbSignature([u8; 4]),
30
31    /// The hash of the final data block is invalid.
32    InvalidFinalBlockHash([u8; 32]),
33
34    /// The header hash is invalid (doesn't match expected hash).
35    InvalidHeaderHash,
36
37    /// The size of a header is invalid
38    InvalidHeaderSize {
39        /// Header identifier.
40        id: u8,
41
42        /// Expected size.
43        expected: u16,
44
45        /// Actual size.
46        actual: u16,
47    },
48
49    /// The key (user's password and key file) is invalid.
50    InvalidKey,
51
52    /// The key file is invalid.
53    InvalidKeyFile,
54
55    /// An I/O error has occurred.
56    Io(io::Error),
57
58    /// The supplied header is missing.
59    MissingHeader(u8),
60
61    /// The compression algorithm specified in the headers is not supported.
62    UnhandledCompression(u32),
63
64    /// The database type specified in the headers is not supported.
65    UnhandledDbType([u8; 4]),
66
67    /// The header type used in the headers is not supported.
68    UnhandledHeader(u8),
69
70    /// The master encryption algorithm is not supported.
71    UnhandledMasterCipher([u8; 16]),
72
73    /// The stream encryption algorithm is not supported.
74    UnhandledStreamCipher(u32),
75
76    /// The specified functionality is not yet supported.
77    Unimplemented(String),
78
79    /// The XML contains the specified error.
80    XmlError(String),
81}
82
83impl fmt::Display for Error {
84    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85        match *self {
86            Error::CryptoError(err) => {
87                match err {
88                    SymmetricCipherError::InvalidLength => {
89                        write!(f, "Crypto error: invalid length.")
90                    }
91
92                    SymmetricCipherError::InvalidPadding => {
93                        write!(f, "Crypto error: invalid padding.")
94                    }
95                }
96            }
97
98            Error::InvalidBlockHash => write!(f, "Invalid block hash"),
99            Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
100            Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
101            Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
102            Error::InvalidHeaderSize {
103                id,
104                expected,
105                actual,
106            } => {
107                write!(
108                    f,
109                    "Invalid header size: id: {}, expected: {}, actual: {}",
110                    id,
111                    expected,
112                    actual
113                )
114            }
115            Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
116            Error::InvalidKey => write!(f, "Invalid key"),
117            Error::InvalidKeyFile => write!(f, "Invalid key file"),
118            Error::Io(ref err) => write!(f, "IO error: {}", err),
119            Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
120            Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
121            Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
122            Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
123            Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
124            Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
125            Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
126            Error::XmlError(ref val) => write!(f, "XML error: {}", val),
127        }
128    }
129}
130
131impl error::Error for Error {
132    fn description(&self) -> &str {
133        match *self {
134            Error::CryptoError(_) => "crypto error",
135            Error::InvalidBlockHash => "invalid block hash",
136            Error::InvalidBlockId(_) => "invalid block id",
137            Error::InvalidDbSignature(_) => "invalid database signature",
138            Error::InvalidFinalBlockHash(_) => "invalid final block hash",
139            Error::InvalidHeaderSize { .. } => "invalid header size",
140            Error::InvalidHeaderHash => "invalid header hash",
141            Error::InvalidKey => "invalid key",
142            Error::InvalidKeyFile => "invalid key file",
143            Error::Io(ref err) => err.description(),
144            Error::MissingHeader(_) => "missing header",
145            Error::UnhandledCompression(_) => "unhandled compression",
146            Error::UnhandledDbType(_) => "unhandled database type",
147            Error::UnhandledHeader(_) => "unhandled header",
148            Error::UnhandledMasterCipher(_) => "unhandled master cipher",
149            Error::UnhandledStreamCipher(_) => "unhandled stream cipher",
150            Error::Unimplemented(_) => "unimplemented",
151            Error::XmlError(_) => "xml error",
152        }
153    }
154
155    fn cause(&self) -> Option<&error::Error> {
156        match *self {
157            Error::Io(ref err) => Some(err),
158            _ => None,
159        }
160    }
161}
162
163impl From<io::Error> for Error {
164    fn from(err: io::Error) -> Error {
165        Error::Io(err)
166    }
167}
168
169impl From<xmlreader::Error> for Error {
170    fn from(err: xmlreader::Error) -> Error {
171        Error::XmlError(format!("{}", err))
172    }
173}
174
175impl From<xmlwriter::Error> for Error {
176    fn from(err: xmlwriter::Error) -> Error {
177        Error::XmlError(format!("{}", err))
178    }
179}
180
181impl From<SymmetricCipherError> for Error {
182    fn from(err: SymmetricCipherError) -> Error {
183        Error::CryptoError(err)
184    }
185}