2020-02-25 16:36:11 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-08-14 12:25:51 +00:00
|
|
|
use matrix_sdk_common::identifiers::{DeviceId, Error as IdentifierError, UserId};
|
2020-03-25 10:32:40 +00:00
|
|
|
use olm_rs::errors::{OlmGroupSessionError, OlmSessionError};
|
2020-03-23 15:14:10 +00:00
|
|
|
use serde_json::Error as SerdeError;
|
2020-03-18 13:15:56 +00:00
|
|
|
use thiserror::Error;
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
use super::store::CryptoStoreError;
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
pub type OlmResult<T> = Result<T, OlmError>;
|
|
|
|
pub type MegolmResult<T> = Result<T, MegolmError>;
|
2020-03-18 14:50:32 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Error representing a failure during a device to device cryptographic
|
|
|
|
/// operation.
|
2020-03-18 14:50:32 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum OlmError {
|
2020-04-30 15:10:12 +00:00
|
|
|
/// The event that should have been decrypted is malformed.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
EventError(#[from] EventError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The received decrypted event couldn't be deserialized.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
JsonError(#[from] SerdeError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The underlying Olm session operation returned an error.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("can't finish Olm Session operation {0}")]
|
|
|
|
OlmSession(#[from] OlmSessionError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The underlying group session operation returned an error.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("can't finish Olm Session operation {0}")]
|
|
|
|
OlmGroupSession(#[from] OlmGroupSessionError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The storage layer returned an error.
|
2020-03-18 14:50:32 +00:00
|
|
|
#[error("failed to read or write to the crypto store {0}")]
|
|
|
|
Store(#[from] CryptoStoreError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The session with a device has become corrupted.
|
2020-12-01 13:50:04 +00:00
|
|
|
#[error(
|
|
|
|
"decryption failed likely because an Olm session from {0} with sender key {1} was wedged"
|
|
|
|
)]
|
2020-10-09 13:39:35 +00:00
|
|
|
SessionWedged(UserId, String),
|
2020-07-21 12:13:10 +00:00
|
|
|
|
2020-12-01 13:50:04 +00:00
|
|
|
/// An Olm message got replayed while the Olm ratchet has already moved
|
|
|
|
/// forward.
|
|
|
|
#[error("decryption failed because an Olm message from {0} with sender key {1} was replayed")]
|
|
|
|
ReplayedMessage(UserId, String),
|
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
/// Encryption failed because the device does not have a valid Olm session
|
|
|
|
/// with us.
|
|
|
|
#[error(
|
|
|
|
"encryption failed because the device does not \
|
|
|
|
have a valid Olm session with us"
|
|
|
|
)]
|
|
|
|
MissingSession,
|
2020-04-30 11:16:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Error representing a failure during a group encryption operation.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum MegolmError {
|
2020-04-30 15:10:12 +00:00
|
|
|
/// The event that should have been decrypted is malformed.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
EventError(#[from] EventError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The received decrypted event couldn't be deserialized.
|
|
|
|
#[error(transparent)]
|
|
|
|
JsonError(#[from] SerdeError),
|
|
|
|
|
|
|
|
/// Decryption failed because the session needed to decrypt the event is
|
|
|
|
/// missing.
|
|
|
|
#[error("decryption failed because the session to decrypt the message is missing")]
|
|
|
|
MissingSession,
|
|
|
|
|
|
|
|
/// The underlying group session operation returned an error.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("can't finish Olm group session operation {0}")]
|
|
|
|
OlmGroupSession(#[from] OlmGroupSessionError),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
|
|
|
/// The storage layer returned an error.
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
Store(#[from] CryptoStoreError),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum EventError {
|
2020-03-21 15:41:48 +00:00
|
|
|
#[error("the Olm message has a unsupported type")]
|
|
|
|
UnsupportedOlmType,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-03-23 15:14:10 +00:00
|
|
|
#[error("the Encrypted message has been encrypted with a unsupported algorithm.")]
|
|
|
|
UnsupportedAlgorithm,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("the provided JSON value isn't an object")]
|
|
|
|
NotAnObject,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-03-23 15:14:10 +00:00
|
|
|
#[error("the Encrypted message doesn't contain a ciphertext for our device")]
|
|
|
|
MissingCiphertext,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-03-27 16:01:21 +00:00
|
|
|
#[error("the Encrypted message is missing the signing key of the sender")]
|
|
|
|
MissingSigningKey,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
#[error("the Encrypted message is missing the sender key")]
|
|
|
|
MissingSenderKey,
|
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("the Encrypted message is missing the field {0}")]
|
|
|
|
MissingField(String),
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("the sender of the plaintext doesn't match the sender of the encrypted message.")]
|
|
|
|
MissmatchedSender,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
#[error("the keys of the message don't match the keys in our database.")]
|
|
|
|
MissmatchedKeys,
|
2020-03-18 14:50:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 10:11:06 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum SessionUnpicklingError {
|
|
|
|
/// The underlying Olm session operation returned an error.
|
|
|
|
#[error("can't finish Olm Session operation {0}")]
|
|
|
|
OlmSession(#[from] OlmSessionError),
|
|
|
|
/// The Session timestamp was invalid.
|
|
|
|
#[error("can't load session timestamps")]
|
|
|
|
SessionTimestampError,
|
|
|
|
}
|
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
#[derive(Error, Debug)]
|
2020-07-14 09:17:09 +00:00
|
|
|
pub enum SignatureError {
|
2020-08-14 12:08:53 +00:00
|
|
|
#[error("the signature used a unsupported algorithm")]
|
|
|
|
UnsupportedAlgorithm,
|
|
|
|
|
2020-08-14 12:25:51 +00:00
|
|
|
#[error("the key id of the signing key is invalid")]
|
|
|
|
InvalidKeyId(#[from] IdentifierError),
|
|
|
|
|
2020-07-14 09:17:09 +00:00
|
|
|
#[error("the signing key is missing from the object that signed the message")]
|
|
|
|
MissingSigningKey,
|
|
|
|
|
2020-08-20 13:14:58 +00:00
|
|
|
#[error("the user id of the signing differs from the subkey user id")]
|
|
|
|
UserIdMissmatch,
|
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
#[error("the provided JSON value isn't an object")]
|
2020-02-25 16:36:11 +00:00
|
|
|
NotAnObject,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
#[error("the provided JSON object doesn't contain a signatures field")]
|
2020-02-25 16:36:11 +00:00
|
|
|
NoSignatureFound,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
#[error("the signature didn't match the provided key")]
|
2020-02-25 16:36:11 +00:00
|
|
|
VerificationError,
|
2020-04-30 15:10:12 +00:00
|
|
|
|
2020-10-24 08:32:17 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
JsonError(#[from] SerdeError),
|
2020-02-25 16:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 13:39:56 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub(crate) enum SessionCreationError {
|
|
|
|
#[error(
|
|
|
|
"Failed to create a new Olm session for {0} {1}, the requested \
|
|
|
|
one-time key isn't a signed curve key"
|
|
|
|
)]
|
2020-07-18 12:51:19 +00:00
|
|
|
OneTimeKeyNotSigned(UserId, Box<DeviceId>),
|
2020-07-15 13:39:56 +00:00
|
|
|
#[error(
|
|
|
|
"Tried to create a new Olm session for {0} {1}, but the signed \
|
|
|
|
one-time key is missing"
|
|
|
|
)]
|
2020-07-18 12:51:19 +00:00
|
|
|
OneTimeKeyMissing(UserId, Box<DeviceId>),
|
2020-07-15 13:39:56 +00:00
|
|
|
#[error("Failed to verify the one-time key signatures for {0} {1}: {2:?}")]
|
2020-07-18 12:51:19 +00:00
|
|
|
InvalidSignature(UserId, Box<DeviceId>, SignatureError),
|
2020-07-15 13:39:56 +00:00
|
|
|
#[error(
|
|
|
|
"Tried to create an Olm session for {0} {1}, but the device is missing \
|
|
|
|
a curve25519 key"
|
|
|
|
)]
|
2020-07-18 12:51:19 +00:00
|
|
|
DeviceMissingCurveKey(UserId, Box<DeviceId>),
|
2020-07-15 13:39:56 +00:00
|
|
|
#[error("Error creating new Olm session for {0} {1}: {2:?}")]
|
2020-07-18 12:51:19 +00:00
|
|
|
OlmError(UserId, Box<DeviceId>, OlmSessionError),
|
2020-07-15 13:39:56 +00:00
|
|
|
}
|