1//! An error type for the `tor-keymgr` crate.
23use tor_error::HasKind;
45use dyn_clone::DynClone;
6use tor_persist::slug::BadSlug;
78use std::error::Error as StdError;
9use std::fmt;
10use std::ops::Deref;
11use std::sync::Arc;
1213use crate::raw::RawKeystoreEntry;
14use crate::{KeyPathError, KeystoreId};
1516/// An Error type for this crate.
17#[derive(thiserror::Error, Debug, Clone)]
18#[non_exhaustive]
19pub enum Error {
20/// Detected keustore corruption.
21#[error("{0}")]
22Corruption(#[from] KeystoreCorruptionError),
2324/// An opaque error returned by a [`Keystore`](crate::Keystore).
25#[error("{0}")]
26Keystore(#[from] Arc<dyn KeystoreError>),
2728/// 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")]
37KeyAlreadyExists,
3839/// Error coming from the tor-key-forgecrate
40#[error("{0}")]
41KeyForge(#[from] tor_key_forge::Error),
4243/// An error caused by an invalid certificate.
44#[error("{0}")]
45InvalidCert(#[from] tor_key_forge::InvalidCertError),
4647/// 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")]
51KeystoreNotFound(KeystoreId),
5253/// An internal error.
54#[error("Internal error")]
55Bug(#[from] tor_error::Bug),
56}
5758/// An error returned by a [`Keystore`](crate::Keystore).
59pub trait KeystoreError:
60 HasKind + StdError + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
61{
62}
6364impl HasKind for Error {
65fn kind(&self) -> tor_error::ErrorKind {
66use tor_error::ErrorKind as EK;
67use Error as E;
6869match self {
70 E::Keystore(e) => e.kind(),
71 E::Corruption(_) => EK::KeystoreCorrupted,
72 E::KeyAlreadyExists => EK::BadApiUsage, // TODO: not strictly right
73E::KeystoreNotFound(_) => EK::BadApiUsage, // TODO: not strictly right
74E::KeyForge(_) => EK::BadApiUsage,
75 E::InvalidCert(_) => EK::BadApiUsage, // TODO: not strictly right
76E::Bug(e) => e.kind(),
77 }
78 }
79}
8081/// 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]
91pub enum ArtiPathSyntaxError {
92/// One of the path slugs was invalid.
93#[error("{0}")]
94Slug(#[from] BadSlug),
9596/// An internal error.
97#[error("Internal error")]
98Bug(#[from] tor_error::Bug),
99}
100101/// An error caused by keystore corruption.
102#[derive(thiserror::Error, Debug, Clone)]
103#[error("Keystore corruption")]
104#[non_exhaustive]
105pub enum KeystoreCorruptionError {
106/// A keystore contains a key that has an invalid [`KeyPath`](crate::KeyPath).
107#[error("{0}")]
108KeyPath(#[from] KeyPathError),
109110/// Missing certificate for key.
111#[error("Missing certificate for key")]
112MissingCertificate,
113114/// Missing the subject key of a certificate we own.
115#[error("Subject key of certificate not found")]
116MissingSubjectKey,
117118/// Missing signing key for certificate.
119#[error("Missing signing key for certificate")]
120MissingSigningKey,
121}
122123/// 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}")]
126pub struct UnknownKeyTypeError {
127/// The extension used for keys of this type in an Arti keystore.
128pub(crate) arti_extension: String,
129}
130131/// An unrecognized keystore entry.
132#[derive(Clone, Debug, amplify::Getters, thiserror::Error)]
133#[error("Unrecognized keystore entry")]
134pub struct UnrecognizedEntryError {
135/// An identifier of the entry that caused the error.
136entry: 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]
150error: Arc<dyn KeystoreError>,
151}
152153impl UnrecognizedEntryError {
154/// Create a new instance of `KeystoreListError` given an `UnrecognizedEntry`
155 /// and an `Arc<dyn KeystoreError>`.
156pub(crate) fn new(entry: UnrecognizedEntry, error: Arc<dyn KeystoreError>) -> Self {
157Self { entry, error }
158 }
159}
160161/// The opaque identifier of an unrecognized key inside a [`Keystore`](crate::Keystore).
162#[derive(Debug, Clone, PartialEq, derive_more::From, derive_more::Into)]
163pub struct UnrecognizedEntry(RawKeystoreEntry);
164165#[cfg(feature = "onion-service-cli-extra")]
166impl Deref for UnrecognizedEntry {
167type Target = RawKeystoreEntry;
168fn deref(&self) -> &Self::Target {
169&self.0
170}
171}
172173#[cfg(test)]
174mod 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 @@ -->
188use super::*;
189use tor_error::ErrorKind;
190191#[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)]
192 #[error("The source of a test error")]
193struct TestErrorSource;
194195#[derive(Debug, Clone, thiserror::Error)]
196 #[error("A test error")]
197struct TestError(#[from] TestErrorSource);
198199impl KeystoreError for TestError {}
200201impl HasKind for TestError {
202fn kind(&self) -> ErrorKind {
203 ErrorKind::Other
204 }
205 }
206207#[test]
208fn error_source() {
209let e: Error = (Arc::new(TestError(TestErrorSource)) as Arc<dyn KeystoreError>).into();
210211assert_eq!(
212 e.source().unwrap().to_string(),
213 TestError(TestErrorSource).to_string()
214 );
215 }
216}