kpdb/types/
error.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
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 crate::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) => match err {
87                SymmetricCipherError::InvalidLength => {
88                    write!(f, "Crypto error: invalid length.")
89                }
90
91                SymmetricCipherError::InvalidPadding => {
92                    write!(f, "Crypto error: invalid padding.")
93                }
94            },
95
96            Error::InvalidBlockHash => write!(f, "Invalid block hash"),
97            Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
98            Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
99            Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
100            Error::InvalidHeaderSize {
101                id,
102                expected,
103                actual,
104            } => {
105                write!(
106                    f,
107                    "Invalid header size: id: {}, expected: {}, actual: {}",
108                    id, expected, actual
109                )
110            }
111            Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
112            Error::InvalidKey => write!(f, "Invalid key"),
113            Error::InvalidKeyFile => write!(f, "Invalid key file"),
114            Error::Io(ref err) => write!(f, "IO error: {}", err),
115            Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
116            Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
117            Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
118            Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
119            Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
120            Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
121            Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
122            Error::XmlError(ref val) => write!(f, "XML error: {}", val),
123        }
124    }
125}
126
127impl error::Error for Error {
128    fn cause(&self) -> Option<&dyn error::Error> {
129        match *self {
130            Error::Io(ref err) => Some(err),
131            _ => None,
132        }
133    }
134}
135
136impl From<io::Error> for Error {
137    fn from(err: io::Error) -> Error {
138        Error::Io(err)
139    }
140}
141
142impl From<xmlreader::Error> for Error {
143    fn from(err: xmlreader::Error) -> Error {
144        Error::XmlError(format!("{}", err))
145    }
146}
147
148impl From<xmlwriter::Error> for Error {
149    fn from(err: xmlwriter::Error) -> Error {
150        Error::XmlError(format!("{}", err))
151    }
152}
153
154impl From<SymmetricCipherError> for Error {
155    fn from(err: SymmetricCipherError) -> Error {
156        Error::CryptoError(err)
157    }
158}