crypto: Reorder the errors so unpickling now returns the timestamp error.

master
Damir Jelić 2020-09-02 12:11:06 +02:00
parent c652762255
commit 8c4acf54e0
3 changed files with 24 additions and 10 deletions

View File

@ -115,6 +115,16 @@ pub enum EventError {
MissmatchedKeys, 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)] #[derive(Error, Debug)]
pub enum SignatureError { pub enum SignatureError {
#[error("the signature used a unsupported algorithm")] #[error("the signature used a unsupported algorithm")]

View File

@ -29,7 +29,7 @@ use serde_json::{json, Value};
use super::IdentityKeys; use super::IdentityKeys;
use crate::{ use crate::{
error::{EventError, OlmResult}, error::{EventError, OlmResult, SessionUnpicklingError},
ReadOnlyDevice, ReadOnlyDevice,
}; };
@ -192,8 +192,8 @@ impl Session {
/// Restore a Session from a previously pickled string. /// Restore a Session from a previously pickled string.
/// ///
/// Returns the restored Olm Session or a `OlmSessionError` if there was an /// Returns the restored Olm Session or a `SessionUnpicklingError` if there
/// error. /// was an error.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -213,17 +213,19 @@ impl Session {
our_identity_keys: Arc<IdentityKeys>, our_identity_keys: Arc<IdentityKeys>,
pickle: PickledSession, pickle: PickledSession,
pickle_mode: PicklingMode, pickle_mode: PicklingMode,
) -> Result<Self, OlmSessionError> { ) -> Result<Self, SessionUnpicklingError> {
let session = OlmSession::unpickle(pickle.pickle.0, pickle_mode)?; let session = OlmSession::unpickle(pickle.pickle.0, pickle_mode)?;
let session_id = session.session_id(); let session_id = session.session_id();
// FIXME this should use the UNIX epoch. // FIXME this should use the UNIX epoch.
let now = Instant::now(); let now = Instant::now();
let creation_time = now.checked_sub(pickle.creation_time).unwrap(); let creation_time = now
// .ok_or(CryptoStoreError::SessionTimestampError)?; .checked_sub(pickle.creation_time)
let last_use_time = now.checked_sub(pickle.last_use_time).unwrap(); .ok_or(SessionUnpicklingError::SessionTimestampError)?;
// .ok_or(CryptoStoreError::SessionTimestampError)?; let last_use_time = now
.checked_sub(pickle.last_use_time)
.ok_or(SessionUnpicklingError::SessionTimestampError)?;
Ok(Session { Ok(Session {
user_id, user_id,

View File

@ -34,6 +34,8 @@ use super::{
user_identity::UserIdentities, user_identity::UserIdentities,
}; };
use crate::error::SessionUnpicklingError;
pub mod memorystore; pub mod memorystore;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -76,8 +78,8 @@ pub enum CryptoStoreError {
OlmGroupSession(#[from] OlmGroupSessionError), OlmGroupSession(#[from] OlmGroupSessionError),
/// A session time-stamp couldn't be loaded. /// A session time-stamp couldn't be loaded.
#[error("can't load session timestamps")] #[error(transparent)]
SessionTimestampError, SessionUnpickling(#[from] SessionUnpicklingError),
/// The store failed to (de)serialize a data type. /// The store failed to (de)serialize a data type.
#[error(transparent)] #[error(transparent)]