1use thiserror::Error;
4
5pub use auths_crypto::AuthsErrorInfo;
6
7#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum AgentError {
11 #[error("Key not found")]
13 KeyNotFound,
14
15 #[error("Incorrect passphrase")]
17 IncorrectPassphrase,
18
19 #[error("Missing Passphrase")]
21 MissingPassphrase,
22
23 #[error("Security error: {0}")]
25 SecurityError(String),
26
27 #[error("Crypto error: {0}")]
29 CryptoError(String),
30
31 #[error("Key deserialization error: {0}")]
33 KeyDeserializationError(String),
34
35 #[error("Signing failed: {0}")]
37 SigningFailed(String),
38
39 #[error("Protocol error: {0}")]
41 Proto(String),
42
43 #[error("IO error: {0}")]
45 IO(#[from] std::io::Error),
46
47 #[error("git error: {0}")]
49 GitError(String),
50
51 #[error("Invalid input: {0}")]
53 InvalidInput(String),
54
55 #[error("Mutex lock poisoned: {0}")]
57 MutexError(String),
58
59 #[error("Storage error: {0}")]
61 StorageError(String),
62
63 #[error("User input cancelled")]
65 UserInputCancelled,
66
67 #[error("Keychain backend unavailable: {backend} - {reason}")]
70 BackendUnavailable {
71 backend: &'static str,
73 reason: String,
75 },
76
77 #[error("Storage is locked, authentication required")]
79 StorageLocked,
80
81 #[error("Failed to initialize keychain backend: {backend} - {error}")]
83 BackendInitFailed {
84 backend: &'static str,
86 error: String,
88 },
89
90 #[error("Credential too large for backend (max {max_bytes} bytes, got {actual_bytes})")]
92 CredentialTooLarge {
93 max_bytes: usize,
95 actual_bytes: usize,
97 },
98
99 #[error("Agent is locked. Unlock with 'auths agent unlock' or restart the agent.")]
101 AgentLocked,
102
103 #[error("Passphrase too weak: {0}")]
105 WeakPassphrase(String),
106
107 #[error("HSM PIN is locked — reset required")]
110 HsmPinLocked,
111
112 #[error("HSM device removed")]
114 HsmDeviceRemoved,
115
116 #[error("HSM session expired")]
118 HsmSessionExpired,
119
120 #[error("HSM does not support mechanism: {0}")]
122 HsmUnsupportedMechanism(String),
123}
124
125impl AuthsErrorInfo for AgentError {
126 fn error_code(&self) -> &'static str {
127 match self {
128 Self::KeyNotFound => "AUTHS-E3001",
129 Self::IncorrectPassphrase => "AUTHS-E3002",
130 Self::MissingPassphrase => "AUTHS-E3003",
131 Self::SecurityError(_) => "AUTHS-E3004",
132 Self::CryptoError(_) => "AUTHS-E3005",
133 Self::KeyDeserializationError(_) => "AUTHS-E3006",
134 Self::SigningFailed(_) => "AUTHS-E3007",
135 Self::Proto(_) => "AUTHS-E3008",
136 Self::IO(_) => "AUTHS-E3009",
137 Self::GitError(_) => "AUTHS-E3010",
138 Self::InvalidInput(_) => "AUTHS-E3011",
139 Self::MutexError(_) => "AUTHS-E3012",
140 Self::StorageError(_) => "AUTHS-E3013",
141 Self::UserInputCancelled => "AUTHS-E3014",
142 Self::BackendUnavailable { .. } => "AUTHS-E3015",
143 Self::StorageLocked => "AUTHS-E3016",
144 Self::BackendInitFailed { .. } => "AUTHS-E3017",
145 Self::CredentialTooLarge { .. } => "AUTHS-E3018",
146 Self::AgentLocked => "AUTHS-E3019",
147 Self::WeakPassphrase(_) => "AUTHS-E3020",
148 Self::HsmPinLocked => "AUTHS-E3021",
149 Self::HsmDeviceRemoved => "AUTHS-E3022",
150 Self::HsmSessionExpired => "AUTHS-E3023",
151 Self::HsmUnsupportedMechanism(_) => "AUTHS-E3024",
152 }
153 }
154
155 fn suggestion(&self) -> Option<&'static str> {
156 match self {
157 Self::KeyNotFound => Some("Run `auths key list` to see available keys"),
158 Self::IncorrectPassphrase => Some("Check your passphrase and try again"),
159 Self::MissingPassphrase => {
160 Some("Provide a passphrase with --passphrase or set AUTHS_PASSPHRASE")
161 }
162 Self::BackendUnavailable { .. } => {
163 Some("Run `auths doctor` to diagnose keychain issues")
164 }
165 Self::StorageLocked => Some("Authenticate with your platform keychain"),
166 Self::BackendInitFailed { .. } => {
167 Some("Run `auths doctor` to diagnose keychain issues")
168 }
169 Self::GitError(_) => Some("Ensure you're in a Git repository"),
170 Self::AgentLocked => {
171 Some("Run `auths agent unlock` or restart with `auths agent start`")
172 }
173 Self::UserInputCancelled => {
174 Some("Run the command again and provide the required input")
175 }
176 Self::StorageError(_) => Some("Check file permissions and disk space"),
177 Self::SecurityError(_) => Some(
178 "Run `auths doctor` to check system keychain access and security configuration",
179 ),
180 Self::CryptoError(_) => {
181 Some("A cryptographic operation failed; check key material with `auths key list`")
182 }
183 Self::KeyDeserializationError(_) => {
184 Some("The stored key is corrupted; re-import with `auths key import`")
185 }
186 Self::SigningFailed(_) => Some(
187 "The signing operation failed; verify your key is accessible with `auths key list`",
188 ),
189 Self::Proto(_) => Some(
190 "A protocol error occurred; check that both sides are running compatible versions",
191 ),
192 Self::IO(_) => Some("Check file permissions and that the filesystem is not read-only"),
193 Self::InvalidInput(_) => Some("Check the command arguments and try again"),
194 Self::MutexError(_) => Some("A concurrency error occurred; restart the operation"),
195 Self::CredentialTooLarge { .. } => Some(
196 "Reduce the credential size or use file-based storage with AUTHS_KEYCHAIN_BACKEND=file",
197 ),
198 Self::WeakPassphrase(_) => {
199 Some("Use at least 12 characters with uppercase, lowercase, and a digit or symbol")
200 }
201 Self::HsmPinLocked => Some("Reset the HSM PIN using your HSM vendor's admin tools"),
202 Self::HsmDeviceRemoved => Some("Reconnect the HSM device and try again"),
203 Self::HsmSessionExpired => Some("Retry the operation — a new session will be opened"),
204 Self::HsmUnsupportedMechanism(_) => {
205 Some("Check that your HSM supports Ed25519 (CKM_EDDSA)")
206 }
207 }
208 }
209}
210
211#[derive(Debug, Error)]
213#[non_exhaustive]
214pub enum TrustError {
215 #[error("I/O error: {0}")]
217 Io(#[from] std::io::Error),
218 #[error("{0}")]
220 InvalidData(String),
221 #[error("not found: {0}")]
223 NotFound(String),
224 #[error("serialization error: {0}")]
226 Serialization(#[from] serde_json::Error),
227 #[error("already exists: {0}")]
229 AlreadyExists(String),
230 #[error("lock acquisition failed: {0}")]
232 Lock(String),
233 #[error("policy rejected: {0}")]
235 PolicyRejected(String),
236}
237
238impl AuthsErrorInfo for TrustError {
239 fn error_code(&self) -> &'static str {
240 match self {
241 Self::Io(_) => "AUTHS-E3101",
242 Self::InvalidData(_) => "AUTHS-E3102",
243 Self::NotFound(_) => "AUTHS-E3103",
244 Self::Serialization(_) => "AUTHS-E3104",
245 Self::AlreadyExists(_) => "AUTHS-E3105",
246 Self::Lock(_) => "AUTHS-E3106",
247 Self::PolicyRejected(_) => "AUTHS-E3107",
248 }
249 }
250
251 fn suggestion(&self) -> Option<&'static str> {
252 match self {
253 Self::NotFound(_) => Some("Run `auths trust list` to see pinned identities"),
254 Self::PolicyRejected(_) => Some("Run `auths trust add` to pin this identity"),
255 Self::Lock(_) => Some("Check file permissions and try again"),
256 Self::Io(_) => Some("Check disk space and file permissions"),
257 Self::AlreadyExists(_) => Some("Run `auths trust list` to see existing entries"),
258 Self::InvalidData(_) => {
259 Some("The trust store may be corrupted; delete and re-pin with `auths trust add`")
260 }
261 Self::Serialization(_) => {
262 Some("The trust store data is corrupted; delete and re-pin with `auths trust add`")
263 }
264 }
265 }
266}
267
268impl From<AgentError> for ssh_agent_lib::error::AgentError {
269 fn from(err: AgentError) -> Self {
270 match err {
271 AgentError::KeyNotFound => Self::Failure,
272 AgentError::IncorrectPassphrase => Self::Failure,
273 _ => Self::Failure,
274 }
275 }
276}