1//! An error type for [`ArtiNativeKeystore`](crate::ArtiNativeKeystore).
23use crate::keystore::fs_utils::FilesystemError;
4use crate::raw::RawEntryId;
5use crate::{ArtiPathSyntaxError, KeystoreError, UnknownKeyTypeError};
6use tor_error::{ErrorKind, HasKind};
7use tor_key_forge::{CertType, KeyType, SshKeyAlgorithm};
89use std::path::PathBuf;
10use std::sync::Arc;
1112/// An error returned by [`ArtiNativeKeystore`](crate::ArtiNativeKeystore)'s
13/// [`Keystore`](crate::Keystore) implementation.
14#[derive(thiserror::Error, Debug, Clone)]
15pub(crate) enum ArtiNativeKeystoreError {
16/// An error that occurred while accessing the filesystem.
17#[error("{0}")]
18Filesystem(#[from] FilesystemError),
1920/// Found a key with an invalid path.
21#[error("Key has invalid path: {path}")]
22MalformedPath {
23/// The path of the key.
24path: PathBuf,
25/// The underlying error.
26#[source]
27err: MalformedPathError,
28 },
2930/// An error due to encountering an unsupported [`KeyType`].
31#[error("{0}")]
32UnknownKeyType(#[from] UnknownKeyTypeError),
3334/// Failed to parse an OpenSSH key.
35#[error("Failed to parse OpenSSH with type {key_type:?}")]
36SshKeyParse {
37/// The path of the malformed key.
38path: PathBuf,
39/// The type of key we were trying to fetch.
40key_type: KeyType,
41/// The underlying error.
42#[source]
43err: Arc<ssh_key::Error>,
44 },
4546/// The OpenSSH key we retrieved is of the wrong type.
47#[error("Unexpected OpenSSH key type: wanted {wanted_key_algo}, found {found_key_algo}")]
48UnexpectedSshKeyType {
49/// The path of the malformed key.
50path: PathBuf,
51/// The algorithm we expected the key to use.
52wanted_key_algo: SshKeyAlgorithm,
53/// The algorithm of the key we got.
54found_key_algo: SshKeyAlgorithm,
55 },
5657/// Failed to parse an OpenSSH key.
58#[error("Failed to parse cert with type {cert_type:?}")]
59CertParse {
60/// The path of the malformed key.
61path: PathBuf,
62/// The type of cert we were trying to fetch.
63cert_type: CertType,
64/// The underlying error.
65#[source]
66err: tor_bytes::Error,
67 },
6869/// Encountered a non-path `RawEntryId` variant.
70#[error("Raw entry {:?} not supported in an Arti keystore", _0)]
71UnsupportedRawEntry(RawEntryId),
7273/// An internal error.
74#[error("Internal error")]
75Bug(#[from] tor_error::Bug),
76}
7778/// The keystore contained a file whose name syntactically improper
79///
80/// Keys are supposed to have pathnames consisting of an `ArtiPath`
81/// followed by a file extension.
82///
83/// See also [`KeyPathError`](crate::KeyPathError), which occurs at a higher level.
84#[derive(thiserror::Error, Debug, Clone)]
85pub(crate) enum MalformedPathError {
86/// Found a key with a non-UTF-8 path.
87#[error("the path is not valid UTF-8")]
88Utf8,
8990/// Found a key with no extension.
91#[error("no extension")]
92NoExtension,
9394/// The file path is not a valid [`ArtiPath`](crate::ArtiPath).
95#[error("not a valid ArtiPath")]
96InvalidArtiPath(ArtiPathSyntaxError),
97}
9899impl KeystoreError for ArtiNativeKeystoreError {}
100101impl HasKind for ArtiNativeKeystoreError {
102fn kind(&self) -> ErrorKind {
103use ArtiNativeKeystoreError as KE;
104105match self {
106 KE::Filesystem(e) => e.kind(),
107 KE::MalformedPath { .. } => ErrorKind::KeystoreAccessFailed,
108 KE::UnknownKeyType(_) => ErrorKind::KeystoreAccessFailed,
109 KE::SshKeyParse { .. } | KE::UnexpectedSshKeyType { .. } | KE::CertParse { .. } => {
110 ErrorKind::KeystoreCorrupted
111 }
112 KE::UnsupportedRawEntry { .. } => ErrorKind::BadApiUsage,
113 KE::Bug(e) => e.kind(),
114 }
115 }
116}
117118impl From<ArtiNativeKeystoreError> for crate::Error {
119fn from(e: ArtiNativeKeystoreError) -> Self {
120crate::Error::Keystore(Arc::new(e))
121 }
122}