Skip to main content

auths_core/crypto/ssh/
error.rs

1//! Domain errors for SSH cryptographic operations.
2
3/// Errors from SSH key construction, signing, and encoding operations.
4///
5/// Usage:
6/// ```ignore
7/// match result {
8///     Err(CryptoError::SshKeyConstruction(msg)) => { /* key creation failed */ }
9///     Err(CryptoError::InvalidSeedLength(n)) => { /* wrong seed size */ }
10///     Ok(pem) => { /* success */ }
11/// }
12/// ```
13#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum CryptoError {
16    /// SSH key construction failed.
17    #[error("SSH key construction failed: {0}")]
18    SshKeyConstruction(String),
19
20    /// Signing operation failed.
21    #[error("signing failed: {0}")]
22    SigningFailed(String),
23
24    /// PEM encoding failed.
25    #[error("PEM encoding failed: {0}")]
26    PemEncoding(String),
27
28    /// The seed has an unexpected length.
29    #[error("invalid seed length: expected 32, got {0}")]
30    InvalidSeedLength(usize),
31
32    /// The key format is invalid.
33    #[error("invalid key format: {0}")]
34    InvalidKeyFormat(String),
35}
36
37impl auths_crypto::AuthsErrorInfo for CryptoError {
38    fn error_code(&self) -> &'static str {
39        match self {
40            Self::SshKeyConstruction(_) => "AUTHS-E3301",
41            Self::SigningFailed(_) => "AUTHS-E3302",
42            Self::PemEncoding(_) => "AUTHS-E3303",
43            Self::InvalidSeedLength(_) => "AUTHS-E3304",
44            Self::InvalidKeyFormat(_) => "AUTHS-E3305",
45        }
46    }
47
48    fn suggestion(&self) -> Option<&'static str> {
49        match self {
50            Self::InvalidSeedLength(_) => Some("Ensure the seed is exactly 32 bytes"),
51            Self::InvalidKeyFormat(_) => Some("Check that the key file is a valid Ed25519 key"),
52            _ => None,
53        }
54    }
55}
56
57impl From<auths_crypto::CryptoError> for CryptoError {
58    fn from(e: auths_crypto::CryptoError) -> Self {
59        CryptoError::InvalidKeyFormat(e.to_string())
60    }
61}