1use tor_error::HasKind;
4
5use dyn_clone::DynClone;
6use tor_persist::slug::BadSlug;
7
8use std::error::Error as StdError;
9use std::fmt;
10use std::ops::Deref;
11use std::sync::Arc;
12
13use crate::raw::RawKeystoreEntry;
14use crate::{KeyPath, KeyPathError, KeystoreId};
15
16#[derive(thiserror::Error, Debug, Clone)]
18#[non_exhaustive]
19pub enum Error {
20 #[error("{0}")]
22 Corruption(#[from] KeystoreCorruptionError),
23
24 #[error("{0}")]
26 Keystore(#[from] Arc<dyn KeystoreError>),
27
28 #[error("Key already exists")]
37 KeyAlreadyExists,
38
39 #[error("{0}")]
41 KeyForge(#[from] tor_key_forge::Error),
42
43 #[error("{0}")]
45 InvalidCert(#[from] tor_key_forge::InvalidCertError),
46
47 #[error("Keystore {0} not found")]
51 KeystoreNotFound(KeystoreId),
52
53 #[error("Internal error")]
55 Bug(#[from] tor_error::Bug),
56}
57
58pub trait KeystoreError:
60 HasKind + StdError + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
61{
62}
63
64impl HasKind for Error {
65 fn kind(&self) -> tor_error::ErrorKind {
66 use Error as E;
67 use tor_error::ErrorKind as EK;
68
69 match self {
70 E::Keystore(e) => e.kind(),
71 E::Corruption(_) => EK::KeystoreCorrupted,
72 E::KeyAlreadyExists => EK::BadApiUsage, E::KeystoreNotFound(_) => EK::BadApiUsage, E::KeyForge(_) => EK::BadApiUsage,
75 E::InvalidCert(_) => EK::BadApiUsage, E::Bug(e) => e.kind(),
77 }
78 }
79}
80
81#[derive(thiserror::Error, Debug, Clone)]
89#[error("Invalid ArtiPath")]
90#[non_exhaustive]
91pub enum ArtiPathSyntaxError {
92 #[error("{0}")]
94 Slug(#[from] BadSlug),
95
96 #[error("Internal error")]
98 Bug(#[from] tor_error::Bug),
99}
100
101#[derive(thiserror::Error, Debug, Clone)]
103#[error("Keystore corruption")]
104#[non_exhaustive]
105pub enum KeystoreCorruptionError {
106 #[error("{0}")]
108 KeyPath(#[from] KeyPathError),
109
110 #[error("Unrecognized key path {0}")]
112 Unrecognized(KeyPath),
113
114 #[error("Missing certificate for key")]
116 MissingCertificate,
117
118 #[error("Subject key of certificate not found")]
120 MissingSubjectKey,
121
122 #[error("Missing signing key for certificate")]
124 MissingSigningKey,
125}
126
127#[derive(thiserror::Error, PartialEq, Eq, Debug, Clone)]
129#[error("unknown key type: arti_extension={arti_extension}")]
130pub struct UnknownKeyTypeError {
131 pub(crate) arti_extension: String,
133}
134
135#[derive(Clone, Debug, amplify::Getters, thiserror::Error)]
137#[error("Unrecognized keystore entry")]
138pub struct UnrecognizedEntryError {
139 entry: UnrecognizedEntry,
141 #[source]
154 error: Arc<dyn KeystoreError>,
155}
156
157impl UnrecognizedEntryError {
158 pub(crate) fn new(entry: UnrecognizedEntry, error: Arc<dyn KeystoreError>) -> Self {
161 Self { entry, error }
162 }
163}
164
165#[derive(Debug, Clone, PartialEq, derive_more::From, derive_more::Into)]
167pub struct UnrecognizedEntry(RawKeystoreEntry);
168
169#[cfg(feature = "onion-service-cli-extra")]
170impl Deref for UnrecognizedEntry {
171 type Target = RawKeystoreEntry;
172 fn deref(&self) -> &Self::Target {
173 &self.0
174 }
175}
176
177#[cfg(test)]
178mod tests {
179 #![allow(clippy::bool_assert_comparison)]
181 #![allow(clippy::clone_on_copy)]
182 #![allow(clippy::dbg_macro)]
183 #![allow(clippy::mixed_attributes_style)]
184 #![allow(clippy::print_stderr)]
185 #![allow(clippy::print_stdout)]
186 #![allow(clippy::single_char_pattern)]
187 #![allow(clippy::unwrap_used)]
188 #![allow(clippy::unchecked_time_subtraction)]
189 #![allow(clippy::useless_vec)]
190 #![allow(clippy::needless_pass_by_value)]
191 use super::*;
193 use tor_error::ErrorKind;
194
195 #[derive(Debug, Copy, Clone, PartialEq, thiserror::Error)]
196 #[error("The source of a test error")]
197 struct TestErrorSource;
198
199 #[derive(Debug, Clone, thiserror::Error)]
200 #[error("A test error")]
201 struct TestError(#[from] TestErrorSource);
202
203 impl KeystoreError for TestError {}
204
205 impl HasKind for TestError {
206 fn kind(&self) -> ErrorKind {
207 ErrorKind::Other
208 }
209 }
210
211 #[test]
212 fn error_source() {
213 let e: Error = (Arc::new(TestError(TestErrorSource)) as Arc<dyn KeystoreError>).into();
214
215 assert_eq!(
216 e.source().unwrap().to_string(),
217 TestError(TestErrorSource).to_string()
218 );
219 }
220}