ssi_jws/
error.rs

1use ssi_claims_core::SignatureError;
2
3use crate::InvalidHeader;
4
5#[derive(thiserror::Error, Debug)]
6pub enum Error {
7    /// Missing curve in JWK
8    #[error("Missing curve in JWK")]
9    MissingCurve,
10    /// Curve not implemented
11    #[error("Curve not implemented: '{0}'")]
12    CurveNotImplemented(String),
13    /// Errors from p256, k256 and ed25519-dalek
14    #[cfg(feature = "k256")]
15    #[error(transparent)]
16    CryptoErr(#[from] k256::ecdsa::Error),
17    #[cfg(all(feature = "p256", not(feature = "k256")))]
18    #[error(transparent)]
19    CryptoErr(#[from] p256::ecdsa::Error),
20    #[cfg(all(feature = "p384", not(any(feature = "k256", feature = "p256"))))]
21    #[error(transparent)]
22    CryptoErr(#[from] p384::ecdsa::Error),
23    #[error(transparent)]
24    Jwk(#[from] ssi_jwk::Error),
25    #[error(transparent)]
26    Json(#[from] serde_json::Error),
27    #[error(transparent)]
28    Base64(#[from] base64::DecodeError),
29    /// Invalid `crit` property in JWT header
30    #[error("Invalid crit property in JWT header")]
31    InvalidCriticalHeader,
32    /// Unknown `crit` header name in JWT header
33    #[error("Unknown critical header name in JWT header")]
34    UnknownCriticalHeader,
35    /// Algorithm in JWS header does not match JWK
36    #[error("Algorithm in JWS header does not match JWK")]
37    AlgorithmMismatch,
38    /// Invalid JWS
39    #[error("Invalid JWS")]
40    InvalidJws,
41    /// Unsupported algorithm
42    #[error("Unsupported algorithm `{0}`")]
43    UnsupportedAlgorithm(String),
44    /// Missing crate features
45    #[error("Missing features: {0}")]
46    MissingFeatures(&'static str),
47    #[error("Algorithm `{0}` not implemented")]
48    AlgorithmNotImplemented(String),
49    #[error("Expected signature length {0} but found {1}")]
50    UnexpectedSignatureLength(usize, usize),
51    #[error("Invalid signature")]
52    InvalidSignature,
53}
54
55impl From<InvalidHeader> for Error {
56    fn from(value: InvalidHeader) -> Self {
57        match value {
58            InvalidHeader::Base64(e) => Self::Base64(e),
59            InvalidHeader::Json(e) => Self::Json(e),
60        }
61    }
62}
63
64#[cfg(feature = "ring")]
65impl From<ring::error::Unspecified> for Error {
66    fn from(e: ring::error::Unspecified) -> Self {
67        ssi_jwk::Error::from(e).into()
68    }
69}
70
71impl From<Error> for SignatureError {
72    fn from(value: Error) -> Self {
73        match value {
74            Error::AlgorithmMismatch => Self::AlgorithmMismatch,
75            Error::AlgorithmNotImplemented(name) => Self::UnsupportedAlgorithm(name),
76            Error::UnsupportedAlgorithm(name) => Self::UnsupportedAlgorithm(name),
77            other => Self::other(other),
78        }
79    }
80}