bh_jws_utils/
error.rs

1// Copyright (C) 2020-2025  The Blockhouse Technology Limited (TBTL).
2//
3// This program is free software: you can redistribute it and/or modify it
4// under the terms of the GNU Affero General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or (at your
6// option) any later version.
7//
8// This program is distributed in the hope that it will be useful, but
9// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
11// License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16/// Error in JWK format
17#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
18pub enum FormatError {
19    /// Error that occurs when JWK parsing failed
20    #[strum(to_string = "JWK parsing failed: {0}")]
21    JwkParsingFailed(String),
22}
23
24impl bherror::BhError for FormatError {}
25
26/// Error in JWS signature
27#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
28pub enum SignatureError {
29    /// Error that occurs when the signing algorithm is invalid
30    #[strum(to_string = "Invalid signing algorithm {0}")]
31    InvalidSigningAlgorithm(String),
32}
33
34impl bherror::BhError for SignatureError {}
35
36/// Cryptographic error
37#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
38pub enum CryptoError {
39    /// Error that occurs when key generation failed
40    #[strum(to_string = "Key generation failed")]
41    KeyGenerationFailed,
42    /// Error that occurs when the cryptographic backend
43    /// unexpectedly failed
44    #[strum(to_string = "Crypto backend failed")]
45    CryptoBackend,
46    /// Error that occurs when the x5chain is invalid
47    #[strum(to_string = "Invalid x5chain")]
48    InvalidX5Chain,
49    /// Error that occurs when the signing algorithm is unsupported
50    #[strum(to_string = "Unsupported: {0}")]
51    Unsupported(String),
52    /// Error that occurs when a public key is incorrectly formatted or
53    /// otherwise not valid.
54    #[strum(to_string = "Invalid public key")]
55    InvalidPublicKey,
56    /// Error that occurs when public keys which are supposed to be match (e.g.
57    /// between a [`Signer`](crate::Signer) and [`X5Chain`](bhx5chain::X5Chain))
58    /// do not match.
59    #[strum(to_string = "Public key mismatch")]
60    PublicKeyMismatch,
61}
62
63impl bherror::BhError for CryptoError {}