tor_keymgr/keystore/ctor/
err.rs1use crate::KeystoreError;
4use crate::keystore::fs_utils;
5use tor_error::{ErrorKind, HasKind};
6use tor_hscrypto::pk::HsIdParseError;
7use tor_key_forge::KeystoreItemType;
8
9use std::path::PathBuf;
10use std::sync::Arc;
11
12#[derive(thiserror::Error, Debug, Clone)]
15pub(crate) enum CTorKeystoreError {
16 #[error("{0}")]
18 Filesystem(#[from] fs_utils::FilesystemError),
19
20 #[error("Key {path} is malformed")]
22 MalformedKey {
23 path: PathBuf,
25 #[source]
27 err: MalformedKeyError,
28 },
29
30 #[error("Operation not supported: {action}")]
32 NotSupported {
33 action: &'static str,
35 },
36
37 #[error("Invalid item type {item_type:?} for {item}")]
39 InvalidKeystoreItemType {
40 item_type: KeystoreItemType,
42 item: String,
44 },
45
46 #[error("Internal error")]
48 Bug(#[from] tor_error::Bug),
49}
50
51#[derive(thiserror::Error, Debug, Clone)]
53pub(crate) enum MalformedKeyError {
54 #[error("{0}")]
56 Service(#[from] MalformedServiceKeyError),
57
58 #[error("{0}")]
60 Client(#[from] MalformedClientKeyError),
61}
62
63#[derive(thiserror::Error, Debug, Clone)]
65pub(crate) enum MalformedServiceKeyError {
66 #[error("invalid key length: {len} (expected {expected_len})")]
68 InvalidKeyLen {
69 len: usize,
71 expected_len: usize,
73 },
74
75 #[error("invalid tag: {tag:?} (expected {expected_tag:?})")]
77 InvalidTag {
78 tag: Vec<u8>,
80 expected_tag: Vec<u8>,
82 },
83
84 #[error("unrecognized key")]
88 NotAKey,
89
90 #[error("invalid ed25519 public key")]
92 Ed25519Public(#[from] Arc<signature::Error>),
93
94 #[error("invalid ed25519 keypair")]
101 Ed25519Keypair,
102
103 #[error("Internal error")]
105 Bug(#[from] tor_error::Bug),
106}
107
108#[derive(thiserror::Error, Debug, Clone)]
110pub(crate) enum MalformedClientKeyError {
111 #[error("Invalid auth type {0}")]
113 InvalidAuthType(String),
114
115 #[error("Invalid key type {0}")]
117 InvalidKeyType(String),
118
119 #[error("Invalid key format")]
121 InvalidFormat,
122
123 #[error("Invalid key material")]
125 InvalidKeyMaterial,
126
127 #[error("Invalid base32 in client key")]
129 InvalidBase32(#[from] data_encoding::DecodeError),
130
131 #[error("Invalid HsId client key")]
133 InvalidHsId(#[from] HsIdParseError),
134}
135
136impl KeystoreError for CTorKeystoreError {}
137
138impl HasKind for CTorKeystoreError {
139 fn kind(&self) -> ErrorKind {
140 use CTorKeystoreError as KE;
141
142 match self {
143 KE::Filesystem(e) => e.kind(),
144 KE::MalformedKey { .. } => ErrorKind::KeystoreCorrupted,
145 KE::NotSupported { .. } => ErrorKind::BadApiUsage,
146 KE::InvalidKeystoreItemType { .. } => ErrorKind::BadApiUsage,
147 KE::Bug(e) => e.kind(),
148 }
149 }
150}
151
152impl From<CTorKeystoreError> for crate::Error {
153 fn from(e: CTorKeystoreError) -> Self {
154 crate::Error::Keystore(Arc::new(e))
155 }
156}