Skip to main content

ruma_signatures/
error.rs

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