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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2020 Johannes Hayeß
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A collection of all errors that can be returned by `libolm`.
//!
//! All error enums additionally contain an error named `Unknown`,
//! for returning an error, in case an error is encountered by `libolm`,
//! but no error code is provided.

use std::error::Error;
use std::fmt;
use std::fmt::Debug;

/// Since libolm does not do heap allocation and instead relies on the user to
/// provide already allocated buffers, a lot of potential errors regarding
/// buffer size can be encountered.
/// In most places in this library we create such buffers exactly the way
/// libolm would want, and as such a lot of potential errors would be eliminated.
/// If such an error is still encountered, it would indicate that something else
/// is seriously wrong with the execution environment, so we panic unrecoverably.
pub(crate) fn handle_fatal_error<E>(error: E)
where
    E: Debug,
{
    unreachable!("Encountered fatal error: {:?}", error);
}

pub(crate) fn olm_error() -> usize {
    unsafe { olm_sys::olm_error() }
}

static BAD_ACCOUNT_KEY: &str = "The supplied account key is invalid";
static INVALID_BASE64: &str = "The input base64 was invalid";
static BAD_MSG_KEY_ID: &str = "The message references an unknown key id";
static BAD_MSG_FMT: &str = "The message couldn't be decoded";
static BAD_MSG_MAC: &str = "The message couldn't be decrypted";
static BAD_MSG_VERSION: &str = "The message version is unsupported";
static BAD_SESSION_KEY: &str = "Can't initialise the inbound group session, invalid session key";
static BAD_MSG_INDEX: &str =
    "Can't decode the message, message index is earlier than our earliest known session key";
static NOT_ENOUGH_RAND: &str = "Not enough entropy was supplied";
static BUFFER_SMALL: &str = "Supplied output buffer is too small";
static INPUT_BUFFER_SMALL: &str = "Supplied input buffer is too small";
static UNKNOWN: &str = "An unknown error occured.";

/// All errors that could be caused by an operation regarding an `OlmAccount`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmAccountError {
    BadAccountKey,
    BadMessageKeyId,
    InvalidBase64,
    NotEnoughRandom,
    OutputBufferTooSmall,
    Unknown,
}

impl fmt::Display for OlmAccountError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmAccountError::BadAccountKey => BAD_ACCOUNT_KEY,
            OlmAccountError::BadMessageKeyId => BAD_MSG_KEY_ID,
            OlmAccountError::InvalidBase64 => INVALID_BASE64,
            OlmAccountError::NotEnoughRandom => NOT_ENOUGH_RAND,
            OlmAccountError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmAccountError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

impl Error for OlmAccountError {}
impl Error for OlmSessionError {}
impl Error for OlmGroupSessionError {}

/// All errors that could be caused by an operation regarding `OlmUitlity`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmUtilityError {
    InvalidBase64,
    OutputBufferTooSmall,
    BadMessageMac,
    Unknown,
}

/// All errors that could be caused by an operation regarding an `OlmSession`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmSessionError {
    BadAccountKey,
    BadMessageFormat,
    BadMessageKeyId,
    BadMessageMac,
    BadMessageVersion,
    InvalidBase64,
    NotEnoughRandom,
    OutputBufferTooSmall,
    Unknown,
}

impl fmt::Display for OlmSessionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmSessionError::BadAccountKey => BAD_ACCOUNT_KEY,
            OlmSessionError::BadMessageKeyId => BAD_MSG_KEY_ID,
            OlmSessionError::BadMessageFormat => BAD_MSG_FMT,
            OlmSessionError::BadMessageMac => BAD_MSG_MAC,
            OlmSessionError::BadMessageVersion => BAD_MSG_VERSION,
            OlmSessionError::InvalidBase64 => INVALID_BASE64,
            OlmSessionError::NotEnoughRandom => NOT_ENOUGH_RAND,
            OlmSessionError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmSessionError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

/// All errors that could be caused by an operation regarding
/// `OlmOutboundGroupSession` and `OlmInboundGroupSession`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmGroupSessionError {
    BadAccountKey,
    BadMessageFormat,
    BadMessageMac,
    BadMessageVersion,
    BadSessionKey,
    InvalidBase64,
    NotEnoughRandom,
    OutputBufferTooSmall,
    UnknownMessageIndex,
    Unknown,
}

impl fmt::Display for OlmGroupSessionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmGroupSessionError::BadAccountKey => BAD_ACCOUNT_KEY,
            OlmGroupSessionError::BadSessionKey => BAD_SESSION_KEY,
            OlmGroupSessionError::UnknownMessageIndex => BAD_MSG_INDEX,
            OlmGroupSessionError::BadMessageFormat => BAD_MSG_FMT,
            OlmGroupSessionError::BadMessageMac => BAD_MSG_MAC,
            OlmGroupSessionError::BadMessageVersion => BAD_MSG_VERSION,
            OlmGroupSessionError::InvalidBase64 => INVALID_BASE64,
            OlmGroupSessionError::NotEnoughRandom => NOT_ENOUGH_RAND,
            OlmGroupSessionError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmGroupSessionError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

/// All errors that could be caused by an operation regarding
/// `OlmSas`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmSasError {
    NotEnoughRandom,
    OutputBufferTooSmall,
    InputBufferTooSmall,
    OtherPublicKeyUnset,
    InvalidLength,
    Unknown,
}

impl fmt::Display for OlmSasError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmSasError::NotEnoughRandom => NOT_ENOUGH_RAND,
            OlmSasError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmSasError::InputBufferTooSmall => INPUT_BUFFER_SMALL,
            OlmSasError::OtherPublicKeyUnset => "The other public key isn't set",
            OlmSasError::InvalidLength => "The length can't be zero",
            OlmSasError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

/// All errors that could be caused by an operation regarding
/// `OlmPkSigning`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmPkSigningError {
    InvalidSeed,
    OutputBufferTooSmall,
    InputBufferTooSmall,
    Unknown,
}

impl fmt::Display for OlmPkSigningError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmPkSigningError::InvalidSeed => "The given seed is too short",
            OlmPkSigningError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmPkSigningError::InputBufferTooSmall => INPUT_BUFFER_SMALL,
            OlmPkSigningError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

impl From<&str> for OlmPkSigningError {
    fn from(value: &str) -> Self {
        match value {
            "OUTPUT_BUFFER_TOO_SMALL" => OlmPkSigningError::OutputBufferTooSmall,
            "INPUT_BUFFER_TOO_SMALL" => OlmPkSigningError::OutputBufferTooSmall,
            _ => OlmPkSigningError::Unknown,
        }
    }
}

/// All errors that could be caused by an operation regarding
/// `OlmPkEncryption`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmPkEncryptionError {
    OutputBufferTooSmall,
    InputBufferTooSmall,
    Unknown,
}

impl fmt::Display for OlmPkEncryptionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmPkEncryptionError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmPkEncryptionError::InputBufferTooSmall => INPUT_BUFFER_SMALL,
            OlmPkEncryptionError::Unknown => UNKNOWN,
        };
        write!(f, "{}", message)
    }
}

impl From<&str> for OlmPkEncryptionError {
    fn from(value: &str) -> Self {
        match value {
            "OUTPUT_BUFFER_TOO_SMALL" => OlmPkEncryptionError::OutputBufferTooSmall,
            "INPUT_BUFFER_TOO_SMALL" => OlmPkEncryptionError::OutputBufferTooSmall,
            _ => OlmPkEncryptionError::Unknown,
        }
    }
}

/// All errors that could be caused by an operation regarding
/// `OlmPkDecryption`.
/// Errors are named exactly like the ones in libolm.
#[derive(Debug, PartialEq)]
pub enum OlmPkDecryptionError {
    BadAccountKey,
    BadMessageMac,
    InvalidBase64,
    OutputBufferTooSmall,
    InputBufferTooSmall,
    Unknown(String),
}

impl fmt::Display for OlmPkDecryptionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            OlmPkDecryptionError::InvalidBase64 => INVALID_BASE64,
            OlmPkDecryptionError::BadAccountKey => BAD_ACCOUNT_KEY,
            OlmPkDecryptionError::BadMessageMac => BAD_MSG_MAC,
            OlmPkDecryptionError::OutputBufferTooSmall => BUFFER_SMALL,
            OlmPkDecryptionError::InputBufferTooSmall => INPUT_BUFFER_SMALL,
            OlmPkDecryptionError::Unknown(e) => e,
        };
        write!(f, "{}", message)
    }
}

impl From<&str> for OlmPkDecryptionError {
    fn from(value: &str) -> Self {
        match value {
            "INVALID_BASE64" => OlmPkDecryptionError::InvalidBase64,
            "BAD_MESSAGE_MAC" => OlmPkDecryptionError::BadMessageMac,
            "BAD_ACCOUNT_KEY" => OlmPkDecryptionError::BadAccountKey,
            "OUTPUT_BUFFER_TOO_SMALL" => OlmPkDecryptionError::OutputBufferTooSmall,
            "INPUT_BUFFER_TOO_SMALL" => OlmPkDecryptionError::OutputBufferTooSmall,
            m => OlmPkDecryptionError::Unknown(m.to_owned()),
        }
    }
}