kpdb/types/
error.rs

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