use crate::KeystoreError;
use crate::keystore::fs_utils;
use tor_error::{ErrorKind, HasKind};
use tor_hscrypto::pk::HsIdParseError;
use tor_key_forge::KeystoreItemType;
use std::path::PathBuf;
use std::sync::Arc;
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum CTorKeystoreError {
#[error("{0}")]
Filesystem(#[from] fs_utils::FilesystemError),
#[error("Key {path} is malformed")]
MalformedKey {
path: PathBuf,
#[source]
err: MalformedKeyError,
},
#[error("Operation not supported: {action}")]
NotSupported {
action: &'static str,
},
#[error("Invalid item type {item_type:?} for {item}")]
InvalidKeystoreItemType {
item_type: KeystoreItemType,
item: String,
},
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum MalformedKeyError {
#[error("{0}")]
Service(#[from] MalformedServiceKeyError),
#[error("{0}")]
Client(#[from] MalformedClientKeyError),
}
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum MalformedServiceKeyError {
#[error("invalid key length: {len} (expected {expected_len})")]
InvalidKeyLen {
len: usize,
expected_len: usize,
},
#[error("invalid tag: {tag:?} (expected {expected_tag:?})")]
InvalidTag {
tag: Vec<u8>,
expected_tag: Vec<u8>,
},
#[error("unrecognized key")]
NotAKey,
#[error("invalid ed25519 public key")]
Ed25519Public(#[from] Arc<signature::Error>),
#[error("invalid ed25519 keypair")]
Ed25519Keypair,
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum MalformedClientKeyError {
#[error("Invalid auth type {0}")]
InvalidAuthType(String),
#[error("Invalid key type {0}")]
InvalidKeyType(String),
#[error("Invalid key format")]
InvalidFormat,
#[error("Invalid key material")]
InvalidKeyMaterial,
#[error("Invalid base32 in client key")]
InvalidBase32(#[from] data_encoding::DecodeError),
#[error("Invalid HsId client key")]
InvalidHsId(#[from] HsIdParseError),
}
impl KeystoreError for CTorKeystoreError {}
impl HasKind for CTorKeystoreError {
fn kind(&self) -> ErrorKind {
use CTorKeystoreError as KE;
match self {
KE::Filesystem(e) => e.kind(),
KE::MalformedKey { .. } => ErrorKind::KeystoreCorrupted,
KE::NotSupported { .. } => ErrorKind::BadApiUsage,
KE::InvalidKeystoreItemType { .. } => ErrorKind::BadApiUsage,
KE::Bug(e) => e.kind(),
}
}
}
impl From<CTorKeystoreError> for crate::Error {
fn from(e: CTorKeystoreError) -> Self {
crate::Error::Keystore(Arc::new(e))
}
}