1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use ruma::{signatures::CanonicalJsonError, IdParseError, OwnedDeviceId, OwnedRoomId, OwnedUserId};
use serde_json::Error as SerdeError;
use thiserror::Error;
use super::store::CryptoStoreError;
pub type OlmResult<T> = Result<T, OlmError>;
pub type MegolmResult<T> = Result<T, MegolmError>;
#[derive(Error, Debug)]
pub enum OlmError {
#[error(transparent)]
EventError(#[from] EventError),
#[error(transparent)]
JsonError(#[from] SerdeError),
#[error(transparent)]
Decryption(#[from] vodozemac::olm::DecryptionError),
#[error(transparent)]
SessionCreation(#[from] vodozemac::megolm::SessionKeyDecodeError),
#[error("failed to read or write to the crypto store {0}")]
Store(#[from] CryptoStoreError),
#[error(
"decryption failed likely because an Olm session from {0} with sender key {1} was wedged"
)]
SessionWedged(OwnedUserId, String),
#[error("decryption failed because an Olm message from {0} with sender key {1} was replayed")]
ReplayedMessage(OwnedUserId, String),
#[error(
"encryption failed because the device does not \
have a valid Olm session with us"
)]
MissingSession,
}
#[derive(Error, Debug)]
pub enum MegolmError {
#[error(transparent)]
EventError(#[from] EventError),
#[error(transparent)]
JsonError(#[from] SerdeError),
#[error("decryption failed because the room key is missing")]
MissingRoomKey,
#[error(transparent)]
Decode(#[from] vodozemac::DecodeError),
#[error("The room where a group session should be shared is not encrypted")]
EncryptionNotEnabled,
#[error(transparent)]
Decryption(#[from] vodozemac::megolm::DecryptionError),
#[error(transparent)]
Store(#[from] CryptoStoreError),
}
#[derive(Error, Debug)]
pub enum EventError {
#[error("the Olm message has a unsupported type, got {0}, expected 0 or 1")]
UnsupportedOlmType(u64),
#[error("the Encrypted message has been encrypted with a unsupported algorithm.")]
UnsupportedAlgorithm,
#[error("the provided JSON value isn't an object")]
NotAnObject,
#[error("the Encrypted message doesn't contain a ciphertext for our device")]
MissingCiphertext,
#[error("the Encrypted message is missing the signing key of the sender")]
MissingSigningKey,
#[error("the Encrypted message is missing the sender key")]
MissingSenderKey,
#[error("the Encrypted message is missing the field {0}")]
MissingField(String),
#[error(
"the sender of the plaintext doesn't match the sender of the encrypted \
message, got {0}, expected {0}"
)]
MismatchedSender(OwnedUserId, OwnedUserId),
#[error(
"the public that was part of the message doesn't match to the key we \
have stored, expected {0}, got {0}"
)]
MismatchedKeys(String, String),
#[error(
"the room id of the room key doesn't match the room id of the \
decrypted event: expected {0}, got {:1}"
)]
MismatchedRoom(OwnedRoomId, Option<OwnedRoomId>),
}
#[derive(Error, Debug)]
pub enum SignatureError {
#[error("the signature used an unsupported algorithm")]
UnsupportedAlgorithm,
#[error("the ID of the signing key is invalid")]
InvalidKeyId(#[from] IdParseError),
#[error("the signing key is missing from the object that signed the message")]
MissingSigningKey,
#[error("the user id of the signing key differs user id that provided the signature")]
UserIdMismatch,
#[error("the provided JSON value isn't an object")]
NotAnObject,
#[error("the provided JSON object doesn't contain a signatures field")]
NoSignatureFound,
#[error(transparent)]
VerificationError(#[from] vodozemac::SignatureError),
#[error(transparent)]
InvalidKey(#[from] vodozemac::KeyError),
#[error("the given signature is not valid and can't be decoded")]
InvalidSignature,
#[error(transparent)]
JsonError(#[from] CanonicalJsonError),
}
impl From<SerdeError> for SignatureError {
fn from(e: SerdeError) -> Self {
CanonicalJsonError::SerDe(e).into()
}
}
#[derive(Error, Debug)]
pub enum SessionCreationError {
#[error(
"Failed to create a new Olm session for {0} {1}, the requested \
one-time key isn't a signed curve key"
)]
OneTimeKeyNotSigned(OwnedUserId, OwnedDeviceId),
#[error(
"Tried to create a new Olm session for {0} {1}, but the signed \
one-time key is missing"
)]
OneTimeKeyMissing(OwnedUserId, OwnedDeviceId),
#[error(
"Tried to create a new Olm session for {0} {1}, but the one-time \
key algorithm is unsupported"
)]
OneTimeKeyUnknown(OwnedUserId, OwnedDeviceId),
#[error("Failed to verify the one-time key signatures for {0} {1}: {2:?}")]
InvalidSignature(OwnedUserId, OwnedDeviceId, SignatureError),
#[error(
"Tried to create an Olm session for {0} {1}, but the device is missing \
a curve25519 key"
)]
DeviceMissingCurveKey(OwnedUserId, OwnedDeviceId),
#[error("Error deserializing the one-time key: {0}")]
InvalidJson(#[from] serde_json::Error),
#[error("The given curve25519 key is not a valid key")]
InvalidCurveKey(#[from] vodozemac::KeyError),
#[error(transparent)]
InboundCreation(#[from] vodozemac::olm::SessionCreationError),
}