use crate::keystore::fs_utils::FilesystemError;
use crate::raw::RawEntryId;
use crate::{ArtiPathSyntaxError, KeystoreError, UnknownKeyTypeError};
use tor_error::{ErrorKind, HasKind};
use tor_key_forge::{CertType, KeyType, SshKeyAlgorithm};
use std::path::PathBuf;
use std::sync::Arc;
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum ArtiNativeKeystoreError {
#[error("{0}")]
Filesystem(#[from] FilesystemError),
#[error("Key has invalid path: {path}")]
MalformedPath {
path: PathBuf,
#[source]
err: MalformedPathError,
},
#[error("{0}")]
UnknownKeyType(#[from] UnknownKeyTypeError),
#[error("Failed to parse OpenSSH with type {key_type:?}")]
SshKeyParse {
path: PathBuf,
key_type: KeyType,
#[source]
err: Arc<ssh_key::Error>,
},
#[error("Unexpected OpenSSH key type: wanted {wanted_key_algo}, found {found_key_algo}")]
UnexpectedSshKeyType {
path: PathBuf,
wanted_key_algo: SshKeyAlgorithm,
found_key_algo: SshKeyAlgorithm,
},
#[error("Failed to parse cert with type {cert_type:?}")]
CertParse {
path: PathBuf,
cert_type: CertType,
#[source]
err: tor_bytes::Error,
},
#[error("Raw entry {:?} not supported in an Arti keystore", _0)]
UnsupportedRawEntry(RawEntryId),
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum MalformedPathError {
#[error("the path is not valid UTF-8")]
Utf8,
#[error("no extension")]
NoExtension,
#[error("not a valid ArtiPath")]
InvalidArtiPath(ArtiPathSyntaxError),
}
impl KeystoreError for ArtiNativeKeystoreError {}
impl HasKind for ArtiNativeKeystoreError {
fn kind(&self) -> ErrorKind {
use ArtiNativeKeystoreError as KE;
match self {
KE::Filesystem(e) => e.kind(),
KE::MalformedPath { .. } => ErrorKind::KeystoreAccessFailed,
KE::UnknownKeyType(_) => ErrorKind::KeystoreAccessFailed,
KE::SshKeyParse { .. } | KE::UnexpectedSshKeyType { .. } | KE::CertParse { .. } => {
ErrorKind::KeystoreCorrupted
}
KE::UnsupportedRawEntry { .. } => ErrorKind::BadApiUsage,
KE::Bug(e) => e.kind(),
}
}
}
impl From<ArtiNativeKeystoreError> for crate::Error {
fn from(e: ArtiNativeKeystoreError) -> Self {
crate::Error::Keystore(Arc::new(e))
}
}