1use std::path::PathBuf;
5
6#[derive(Debug)]
8pub enum SignerError {
9 UnsupportedAlgorithm(String),
10 UnknownKeyFormat,
11 InvalidKey(String),
12 InvalidSignature(String),
13 InvalidPublicKey(String),
14 Io(std::io::Error),
15 Pem(String),
16 Ed25519(String),
17 P256(String),
18 Pkcs8(String),
19 KeyNotFound(PathBuf),
20 InsecureKeyPermissions { path: PathBuf, mode: u32 },
21 VerificationFailed,
22}
23
24impl std::fmt::Display for SignerError {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 match self {
27 SignerError::UnsupportedAlgorithm(algo) => {
28 write!(f, "unsupported signature algorithm: {}", algo)
29 }
30 SignerError::UnknownKeyFormat => write!(f, "unknown or unsupported key format"),
31 SignerError::InvalidKey(msg) => write!(f, "invalid key: {}", msg),
32 SignerError::InvalidSignature(msg) => write!(f, "invalid signature: {}", msg),
33 SignerError::InvalidPublicKey(msg) => write!(f, "invalid public key: {}", msg),
34 SignerError::Io(e) => write!(f, "I/O error: {}", e),
35 SignerError::Pem(msg) => write!(f, "PEM error: {}", msg),
36 SignerError::Ed25519(msg) => write!(f, "Ed25519 error: {}", msg),
37 SignerError::P256(msg) => write!(f, "P256 error: {}", msg),
38 SignerError::Pkcs8(msg) => write!(f, "PKCS8 error: {}", msg),
39 SignerError::KeyNotFound(path) => write!(f, "key file not found: {}", path.display()),
40 SignerError::InsecureKeyPermissions { path, mode } => write!(
41 f,
42 "private key file {} is group/world-accessible (mode {mode:04o}, required 0600 \
43 or stricter); fix with: chmod 600 {}",
44 path.display(),
45 path.display()
46 ),
47 SignerError::VerificationFailed => write!(f, "signature verification failed"),
48 }
49 }
50}
51
52impl std::error::Error for SignerError {
53 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54 match self {
55 SignerError::Io(e) => Some(e),
56 _ => None,
57 }
58 }
59}
60
61impl From<std::io::Error> for SignerError {
62 fn from(e: std::io::Error) -> Self {
63 SignerError::Io(e)
64 }
65}
66
67impl From<ed25519_dalek::SignatureError> for SignerError {
68 fn from(e: ed25519_dalek::SignatureError) -> Self {
69 SignerError::Ed25519(e.to_string())
70 }
71}
72
73impl From<pkcs8::Error> for SignerError {
74 fn from(e: pkcs8::Error) -> Self {
75 SignerError::Pkcs8(e.to_string())
76 }
77}
78
79impl From<pkcs8::spki::Error> for SignerError {
80 fn from(e: pkcs8::spki::Error) -> Self {
81 SignerError::Pkcs8(e.to_string())
82 }
83}
84
85impl From<sec1::Error> for SignerError {
86 fn from(e: sec1::Error) -> Self {
87 SignerError::Pem(e.to_string())
88 }
89}