zaun_community_protocol/signed/error.rs
1//! Errors of signed objects and their validity rules.
2
3use crate::crypto::{SignError, VerifyError};
4use crate::encoding::{DecodeError, EncodeError};
5use crate::keys::KeyEncodingError;
6
7/// Signing, verifying, encoding or decoding one signed object failed.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
9pub enum SignedError {
10 /// The object breaks a field rule or limit and cannot be encoded.
11 #[error(transparent)]
12 Encode(#[from] EncodeError),
13 /// The verified bytes are not a canonical encoding of the expected type.
14 #[error(transparent)]
15 Decode(#[from] DecodeError),
16 /// The signature does not verify (checked before decoding).
17 #[error(transparent)]
18 Signature(#[from] VerifyError),
19 /// The signature bytes are not a valid `Signature` encoding.
20 #[error(transparent)]
21 SignatureEncoding(#[from] KeyEncodingError),
22 /// Signing failed.
23 #[error(transparent)]
24 Sign(#[from] SignError),
25}
26
27/// The rotation chain does not verify (specification §4.6). `index` is 1-based.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
29pub enum RotationError {
30 /// The genesis key does not derive the community id.
31 #[error("the genesis key does not derive the community id")]
32 Genesis,
33 /// A rotation's encoding or one of its signatures is invalid.
34 #[error("rotation {index}: {error}")]
35 Signed {
36 /// Position in the chain.
37 index: u64,
38 /// The failure.
39 error: SignedError,
40 },
41 /// A rotation names another community.
42 #[error("rotation {0} names another community")]
43 Community(u64),
44 /// `rotation_seq` is not the rotation's position.
45 #[error("rotation {0} has the wrong rotation_seq")]
46 Sequence(u64),
47 /// `old_root` is not the previous key of the chain.
48 #[error("rotation {0} does not continue the chain")]
49 OldRoot(u64),
50 /// `new_suite` is below the previous suite.
51 #[error("rotation {0} downgrades the suite")]
52 SuiteDowngrade(u64),
53 /// More rotations than `rotation_seq` can number.
54 #[error("rotation chain too long")]
55 TooLong,
56}
57
58/// A (descriptor, delegation) pair is not valid (specification §4.3).
59#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
60pub enum ValidityError {
61 /// Rule 1: the rotation chain does not verify.
62 #[error(transparent)]
63 Chain(#[from] RotationError),
64 /// Rule 1: the descriptor does not verify or decode.
65 #[error("descriptor: {0}")]
66 Descriptor(SignedError),
67 /// Rule 1: `community_id` is not the expected community.
68 #[error("descriptor names another community")]
69 Community,
70 /// Rule 1: `root_public_key` is not the chain's last key.
71 #[error("descriptor root key is not the chain's last key")]
72 RootKey,
73 /// Rule 1: the descriptor's latest rotation is not the chain's last rotation.
74 #[error("descriptor rotation does not match the chain")]
75 EmbeddedRotation,
76 /// §3.3: the root key's suite is below the descriptor's `min_suite`.
77 #[error("root key suite below min_suite")]
78 SuiteBelowMinimum,
79 /// Rule 2: the descriptor authorises no host (suspended).
80 #[error("no host is delegated")]
81 NoDelegation,
82 /// Rule 2: `SHA3-256(delegation.bytes)` differs from `delegation_hash`.
83 #[error("delegation hash mismatch")]
84 DelegationHash,
85 /// Rule 3: the delegation does not verify or decode.
86 #[error("delegation: {0}")]
87 Delegation(SignedError),
88 /// Rule 2: the delegation epoch differs from the reference.
89 #[error("delegation epoch mismatch")]
90 DelegationEpoch,
91 /// Rule 3: the delegation names another community.
92 #[error("delegation names another community")]
93 DelegationCommunity,
94 /// Rule 4: `now < not_before − 5 min`.
95 #[error("delegation not yet valid")]
96 NotYetValid,
97 /// Rule 4: `now > expires_at + 5 min`.
98 #[error("delegation expired")]
99 Expired,
100 /// Rule 5: a lower descriptor revision than accepted.
101 #[error("descriptor revision rollback")]
102 RevisionRollback,
103 /// Rule 5: an equal revision with different bytes.
104 #[error("descriptor revision conflict")]
105 RevisionConflict,
106 /// Rule 5: a lower delegation epoch than accepted.
107 #[error("delegation epoch rollback")]
108 EpochRollback,
109 /// Rule 5: an equal epoch with another `delegation_hash`.
110 #[error("delegation epoch conflict")]
111 EpochConflict,
112 /// Rule 5: a shorter rotation chain than accepted.
113 #[error("rotation chain rollback")]
114 ChainRollback,
115 /// Rule 5 (extended): the chain does not pass through the accepted root key.
116 #[error("rotation chain forks from the accepted root key")]
117 RootKeyFork,
118 /// Rule 5: a lower `min_suite` than accepted.
119 #[error("min_suite rollback")]
120 MinSuiteRollback,
121}
122
123/// A `MemberKeyRegistration` is not acceptable to the host (specification §4.4).
124#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
125pub enum RegistrationError {
126 /// The registration does not verify under the command's member key, or does not decode.
127 #[error(transparent)]
128 Signed(#[from] SignedError),
129 /// `member_public_key` differs from the command's key.
130 #[error("member key mismatch")]
131 MemberKey,
132 /// The purpose differs from the command's.
133 #[error("purpose mismatch")]
134 Purpose,
135 /// `community_id` differs from the command's.
136 #[error("community mismatch")]
137 Community,
138 /// `host_id` is not this host.
139 #[error("host mismatch")]
140 Host,
141 /// `host_challenge` is not the connection's current challenge.
142 #[error("challenge mismatch")]
143 Challenge,
144}
145
146/// A `HostSessionProof` is not acceptable to the client (specification §4.5).
147#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
148pub enum SessionProofError {
149 /// The proof does not verify under the host key, or does not decode.
150 #[error(transparent)]
151 Signed(#[from] SignedError),
152 /// `host_id` does not derive from the host key.
153 #[error("host id mismatch")]
154 HostId,
155 /// `client_nonce` is not the client's fresh nonce.
156 #[error("nonce mismatch")]
157 Nonce,
158 /// `community_id` and `delegation_epoch` are not both some or both none.
159 #[error("community and epoch presence differ")]
160 EpochPresence,
161 /// A community is named but no valid pair was presented.
162 #[error("no valid descriptor and delegation")]
163 MissingPair,
164 /// The valid pair is for another community, host or epoch.
165 #[error("descriptor and delegation do not match the proof")]
166 PairMismatch,
167}