From 8c4acf54e0e46f3218c8fda0bc59e66026332840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 2 Sep 2020 12:11:06 +0200 Subject: [PATCH] crypto: Reorder the errors so unpickling now returns the timestamp error. --- matrix_sdk_crypto/src/error.rs | 10 ++++++++++ matrix_sdk_crypto/src/olm/session.rs | 18 ++++++++++-------- matrix_sdk_crypto/src/store/mod.rs | 6 ++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/matrix_sdk_crypto/src/error.rs b/matrix_sdk_crypto/src/error.rs index cc679c0c..ee4e5f1f 100644 --- a/matrix_sdk_crypto/src/error.rs +++ b/matrix_sdk_crypto/src/error.rs @@ -115,6 +115,16 @@ pub enum EventError { MissmatchedKeys, } +#[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, +} + #[derive(Error, Debug)] pub enum SignatureError { #[error("the signature used a unsupported algorithm")] diff --git a/matrix_sdk_crypto/src/olm/session.rs b/matrix_sdk_crypto/src/olm/session.rs index f8545d58..554e75cb 100644 --- a/matrix_sdk_crypto/src/olm/session.rs +++ b/matrix_sdk_crypto/src/olm/session.rs @@ -29,7 +29,7 @@ use serde_json::{json, Value}; use super::IdentityKeys; use crate::{ - error::{EventError, OlmResult}, + error::{EventError, OlmResult, SessionUnpicklingError}, ReadOnlyDevice, }; @@ -192,8 +192,8 @@ impl Session { /// Restore a Session from a previously pickled string. /// - /// Returns the restored Olm Session or a `OlmSessionError` if there was an - /// error. + /// Returns the restored Olm Session or a `SessionUnpicklingError` if there + /// was an error. /// /// # Arguments /// @@ -213,17 +213,19 @@ impl Session { our_identity_keys: Arc, pickle: PickledSession, pickle_mode: PicklingMode, - ) -> Result { + ) -> Result { let session = OlmSession::unpickle(pickle.pickle.0, pickle_mode)?; let session_id = session.session_id(); // FIXME this should use the UNIX epoch. let now = Instant::now(); - let creation_time = now.checked_sub(pickle.creation_time).unwrap(); - // .ok_or(CryptoStoreError::SessionTimestampError)?; - let last_use_time = now.checked_sub(pickle.last_use_time).unwrap(); - // .ok_or(CryptoStoreError::SessionTimestampError)?; + let creation_time = now + .checked_sub(pickle.creation_time) + .ok_or(SessionUnpicklingError::SessionTimestampError)?; + let last_use_time = now + .checked_sub(pickle.last_use_time) + .ok_or(SessionUnpicklingError::SessionTimestampError)?; Ok(Session { user_id, diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 4a9a4044..3e66661c 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -34,6 +34,8 @@ use super::{ user_identity::UserIdentities, }; +use crate::error::SessionUnpicklingError; + pub mod memorystore; #[cfg(not(target_arch = "wasm32"))] @@ -76,8 +78,8 @@ pub enum CryptoStoreError { OlmGroupSession(#[from] OlmGroupSessionError), /// A session time-stamp couldn't be loaded. - #[error("can't load session timestamps")] - SessionTimestampError, + #[error(transparent)] + SessionUnpickling(#[from] SessionUnpicklingError), /// The store failed to (de)serialize a data type. #[error(transparent)]