1
//! An error type for the `tor-keymgr` crate.
2

            
3
use tor_error::HasKind;
4

            
5
use dyn_clone::DynClone;
6
use tor_persist::slug::BadSlug;
7

            
8
use std::error::Error as StdError;
9
use std::fmt;
10
use std::ops::Deref;
11
use std::sync::Arc;
12

            
13
use crate::raw::RawKeystoreEntry;
14
use crate::{KeyPathError, KeystoreId};
15

            
16
/// An Error type for this crate.
17
#[derive(thiserror::Error, Debug, Clone)]
18
#[non_exhaustive]
19
pub enum Error {
20
    /// Detected keustore corruption.
21
    #[error("{0}")]
22
    Corruption(#[from] KeystoreCorruptionError),
23

            
24
    /// An opaque error returned by a [`Keystore`](crate::Keystore).
25
    #[error("{0}")]
26
    Keystore(#[from] Arc<dyn KeystoreError>),
27

            
28
    /// An error returned when the [`KeyMgr`](crate::KeyMgr) is asked to generate a key that already exists.
29
    ///
30
    /// Note that because there is no locking of the keystore,
31
    /// this situation is not reliably detected
32
    /// in the presence of concurrent tasks trying to generate the same key.
33
    ///
34
    /// So this error is provided to help the human user,
35
    /// but mustn't be relied on for correctness.
36
    #[error("Key already exists")]
37
    KeyAlreadyExists,
38

            
39
    /// Error coming from the tor-key-forgecrate
40
    #[error("{0}")]
41
    KeyForge(#[from] tor_key_forge::Error),
42

            
43
    /// An error caused by an invalid certificate.
44
    #[error("{0}")]
45
    InvalidCert(#[from] tor_key_forge::InvalidCertError),
46

            
47
    /// An error returned when the [`KeyMgr`](crate::KeyMgr) is unable to
48
    /// find a [`Keystore`](crate::Keystore) matching a given [`KeystoreId`]
49
    /// in either its `primary_store` field or the `secondary_stores` collection.
50
    #[error("Keystore {0} not found")]
51
    KeystoreNotFound(KeystoreId),
52

            
53
    /// An internal error.
54
    #[error("Internal error")]
55
    Bug(#[from] tor_error::Bug),
56
}
57

            
58
/// An error returned by a [`Keystore`](crate::Keystore).
59
pub trait KeystoreError:
60
    HasKind + StdError + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
61
{
62
}
63

            
64
impl HasKind for Error {
65
4
    fn kind(&self) -> tor_error::ErrorKind {
66
        use Error as E;
67
        use tor_error::ErrorKind as EK;
68

            
69
4
        match self {
70
            E::Keystore(e) => e.kind(),
71
            E::Corruption(_) => EK::KeystoreCorrupted,
72
            E::KeyAlreadyExists => EK::BadApiUsage, // TODO: not strictly right
73
            E::KeystoreNotFound(_) => EK::BadApiUsage, // TODO: not strictly right
74
            E::KeyForge(_) => EK::BadApiUsage,
75
            E::InvalidCert(_) => EK::BadApiUsage, // TODO: not strictly right
76
4
            E::Bug(e) => e.kind(),
77
        }
78
4
    }
79
}
80

            
81
/// An error caused by a syntactically invalid [`ArtiPath`](crate::ArtiPath).
82
///
83
/// The `ArtiPath` is not in the legal syntax: it contains bad characters,
84
/// or a syntactically invalid components.
85
///
86
/// (Does not include any errors arising from paths which are invalid
87
/// *for the particular key*.)
88
#[derive(thiserror::Error, Debug, Clone)]
89
#[error("Invalid ArtiPath")]
90
#[non_exhaustive]
91
pub enum ArtiPathSyntaxError {
92
    /// One of the path slugs was invalid.
93
    #[error("{0}")]
94
    Slug(#[from] BadSlug),
95

            
96
    /// An internal error.
97
    #[error("Internal error")]
98
    Bug(#[from] tor_error::Bug),
99
}
100

            
101
/// An error caused by keystore corruption.
102
#[derive(thiserror::Error, Debug, Clone)]
103
#[error("Keystore corruption")]
104
#[non_exhaustive]
105
pub enum KeystoreCorruptionError {
106
    /// A keystore contains a key that has an invalid [`KeyPath`](crate::KeyPath).
107
    #[error("{0}")]
108
    KeyPath(#[from] KeyPathError),
109

            
110
    /// Missing certificate for key.
111
    #[error("Missing certificate for key")]
112
    MissingCertificate,
113

            
114
    /// Missing the subject key of a certificate we own.
115
    #[error("Subject key of certificate not found")]
116
    MissingSubjectKey,
117

            
118
    /// Missing signing key for certificate.
119
    #[error("Missing signing key for certificate")]
120
    MissingSigningKey,
121
}
122

            
123
/// An error that happens when we encounter an unknown key type.
124
#[derive(thiserror::Error, PartialEq, Eq, Debug, Clone)]
125
#[error("unknown key type: arti_extension={arti_extension}")]
126
pub struct UnknownKeyTypeError {
127
    /// The extension used for keys of this type in an Arti keystore.
128
    pub(crate) arti_extension: String,
129
}
130

            
131
/// An unrecognized keystore entry.
132
#[derive(Clone, Debug, amplify::Getters, thiserror::Error)]
133
#[error("Unrecognized keystore entry")]
134
pub struct UnrecognizedEntryError {
135
    /// An identifier of the entry that caused the error.
136
    entry: UnrecognizedEntry,
137
    /// The underlying error that occurred.
138
    // TODO: This should be an `Error` specific for the situation.
139
    //
140
    // [`KeystoreError`] is a provvisory solution that presents
141
    // some issues, for example:
142
    //
143
    // * not all variants of `KeystoreError` are relevant
144
    // * redundancy with some other Error types like
145
    // [`MalformedServiceKeyError::NotAKey`](crate::keystore::ctor::err::MalformedServiceKeyError)
146
    // * [`Keystore::list`](crate::Keystore) returns
147
    // `StdResult<Vec<StdResult<(KeyPath, KeystoreItemType), UnrecognizedEntryError>>, KeystoreError>`,
148
    // `KeystoreError` presents itself twice at 2 different levels, there is ambiguity
149
    #[source]
150
    error: Arc<dyn KeystoreError>,
151
}
152

            
153
impl UnrecognizedEntryError {
154
    /// Create a new instance of `KeystoreListError` given an `UnrecognizedEntry`
155
    /// and an `Arc<dyn KeystoreError>`.
156
924
    pub(crate) fn new(entry: UnrecognizedEntry, error: Arc<dyn KeystoreError>) -> Self {
157
924
        Self { entry, error }
158
924
    }
159
}
160

            
161
/// The opaque identifier of an unrecognized key inside a [`Keystore`](crate::Keystore).
162
#[derive(Debug, Clone, PartialEq, derive_more::From, derive_more::Into)]
163
pub struct UnrecognizedEntry(RawKeystoreEntry);
164

            
165
#[cfg(feature = "onion-service-cli-extra")]
166
impl Deref for UnrecognizedEntry {
167
    type Target = RawKeystoreEntry;
168
730
    fn deref(&self) -> &Self::Target {
169
730
        &self.0
170
730
    }
171
}
172

            
173
#[cfg(test)]
174
mod tests {
175
    // @@ begin test lint list maintained by maint/add_warning @@
176
    #![allow(clippy::bool_assert_comparison)]
177
    #![allow(clippy::clone_on_copy)]
178
    #![allow(clippy::dbg_macro)]
179
    #![allow(clippy::mixed_attributes_style)]
180
    #![allow(clippy::print_stderr)]
181
    #![allow(clippy::print_stdout)]
182
    #![allow(clippy::single_char_pattern)]
183
    #![allow(clippy::unwrap_used)]
184
    #![allow(clippy::unchecked_duration_subtraction)]
185
    #![allow(clippy::useless_vec)]
186
    #![allow(clippy::needless_pass_by_value)]
187
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
188
    use super::*;
189
    use tor_error::ErrorKind;
190

            
191
    #[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)]
192
    #[error("The source of a test error")]
193
    struct TestErrorSource;
194

            
195
    #[derive(Debug, Clone, thiserror::Error)]
196
    #[error("A test error")]
197
    struct TestError(#[from] TestErrorSource);
198

            
199
    impl KeystoreError for TestError {}
200

            
201
    impl HasKind for TestError {
202
        fn kind(&self) -> ErrorKind {
203
            ErrorKind::Other
204
        }
205
    }
206

            
207
    #[test]
208
    fn error_source() {
209
        let e: Error = (Arc::new(TestError(TestErrorSource)) as Arc<dyn KeystoreError>).into();
210

            
211
        assert_eq!(
212
            e.source().unwrap().to_string(),
213
            TestError(TestErrorSource).to_string()
214
        );
215
    }
216
}