Skip to main content

crypto/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Error types for cryptographic signing.
3
4use std::path::PathBuf;
5
6/// Error type for signer operations.
7#[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    Rsa(String),
18    P256(String),
19    Pkcs8(String),
20    KeyNotFound(PathBuf),
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::Rsa(msg) => write!(f, "RSA error: {}", msg),
38            SignerError::P256(msg) => write!(f, "P256 error: {}", msg),
39            SignerError::Pkcs8(msg) => write!(f, "PKCS8 error: {}", msg),
40            SignerError::KeyNotFound(path) => write!(f, "key file not found: {}", path.display()),
41            SignerError::VerificationFailed => write!(f, "signature verification failed"),
42        }
43    }
44}
45
46impl std::error::Error for SignerError {
47    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match self {
49            SignerError::Io(e) => Some(e),
50            _ => None,
51        }
52    }
53}
54
55impl From<std::io::Error> for SignerError {
56    fn from(e: std::io::Error) -> Self {
57        SignerError::Io(e)
58    }
59}
60
61impl From<ed25519_dalek::SignatureError> for SignerError {
62    fn from(e: ed25519_dalek::SignatureError) -> Self {
63        SignerError::Ed25519(e.to_string())
64    }
65}
66
67impl From<rsa::Error> for SignerError {
68    fn from(e: rsa::Error) -> Self {
69        SignerError::Rsa(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}