Skip to main content

ruma_signatures/
error.rs

1use ruma_common::{
2    IdParseError, canonical_json::CanonicalJsonFieldError, serde::Base64DecodeError,
3};
4use thiserror::Error;
5
6use crate::Ed25519VerificationError;
7
8/// All errors related to JSON validation/parsing.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum JsonError {
12    /// The PDU is too large.
13    #[error("PDU is larger than maximum of 65535 bytes")]
14    PduTooLarge,
15
16    /// A field is missing or invalid.
17    #[error(transparent)]
18    Field(#[from] CanonicalJsonFieldError),
19
20    /// A more generic JSON error from [`serde_json`].
21    #[error(transparent)]
22    Serde(#[from] serde_json::Error),
23}
24
25/// Errors relating to verification of signatures.
26#[derive(Debug, Error)]
27#[non_exhaustive]
28pub enum VerificationError {
29    /// The JSON to check is invalid.
30    #[error("Invalid JSON: {0}")]
31    Json(JsonError),
32
33    /// Parsing a base64-encoded signature failed.
34    #[error("Could not parse base64-encoded signature at `path`: {source}")]
35    InvalidBase64Signature {
36        /// The full path to the signature.
37        path: String,
38
39        /// The originating error.
40        #[source]
41        source: Base64DecodeError,
42    },
43
44    /// Parsing a Matrix identifier failed.
45    #[error("Could not parse {identifier_type}: {source}")]
46    ParseIdentifier {
47        /// The type of identifier that was parsed.
48        identifier_type: &'static str,
49
50        /// The error when parsing the identifier.
51        #[source]
52        source: IdParseError,
53    },
54
55    /// The signature uses an unsupported algorithm.
56    #[error("signature uses an unsupported algorithm")]
57    UnsupportedAlgorithm,
58
59    /// The signatures for an entity cannot be found in the signatures map.
60    #[error("Could not find signatures for entity {0:?}")]
61    NoSignaturesForEntity(String),
62
63    /// The public keys for an entity cannot be found in the public keys map.
64    #[error("Could not find public keys for entity {0:?}")]
65    NoPublicKeysForEntity(String),
66
67    /// No signature with a supported algorithm was found for the given entity.
68    #[error("Could not find supported signature for entity {0:?}")]
69    NoSupportedSignatureForEntity(String),
70
71    /// Error verifying an ed25519 signature.
72    #[error(transparent)]
73    Ed25519(#[from] Ed25519VerificationError),
74}
75
76impl<T> From<T> for VerificationError
77where
78    T: Into<JsonError>,
79{
80    fn from(value: T) -> Self {
81        Self::Json(value.into())
82    }
83}