identity_jose/jws/custom_verification/error.rs
1// Copyright 2020-2023 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::fmt::Display;
5
6/// Error type for a failed jws signature verification. See [`JwsVerifier`](super::JwsVerifier).
7pub type SignatureVerificationError = identity_core::common::SingleStructError<SignatureVerificationErrorKind>;
8
9/// The cause of a failed jws signature verification.
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum SignatureVerificationErrorKind {
13 /// Indicates that the [`JwsVerifier`](super::JwsVerifier) implementation is not compatible with
14 /// the `alg` extracted from the JOSE header.
15 UnsupportedAlg,
16 /// Indicates that the [`JwsVerifier`](super::JwsVerifier) implementation does not support the
17 /// `kty` of the provided [`Jwk`](crate::jwk::Jwk).
18 UnsupportedKeyType,
19 /// Indicates that the [`JwsVerifier`](super::JwsVerifier) implementation does not support the
20 /// public key parameters extracted from the provided [`Jwk`](crate::jwk::Jwk).
21 UnsupportedKeyParams,
22 /// Indicates that the [`JwsVerifier`](super::JwsVerifier) implementation failed to decode the
23 /// public key extracted from the provided [`Jwk`](crate::jwk::Jwk).
24 KeyDecodingFailure,
25 /// Indicates that the [`JwsVerifier`](super::JwsVerifier) implementation considers the signature
26 /// to be invalid.
27 InvalidSignature,
28 /// Indicates that something went wrong when calling
29 /// [`JwsVerifier::verify`](super::JwsVerifier::verify), but it is unclear whether the reason
30 /// matches any of the other variants.
31 Unspecified,
32}
33
34impl SignatureVerificationErrorKind {
35 const fn as_str(&self) -> &str {
36 match self {
37 Self::UnsupportedAlg => "unsupported alg",
38 Self::UnsupportedKeyType => "unsupported key type",
39 Self::UnsupportedKeyParams => "unsupported key parameters",
40 Self::KeyDecodingFailure => "key decoding failure",
41 Self::InvalidSignature => "invalid signature",
42 Self::Unspecified => "unspecified failure",
43 }
44 }
45}
46
47impl Display for SignatureVerificationErrorKind {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 write!(f, "{}", self.as_str())
50 }
51}