Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new dehydrated device format #199

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
50 changes: 49 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ pub enum PickleError {

/// Error type describing the various ways libolm pickles can fail to be
/// decoded.
#[cfg(feature = "libolm-compat")]
#[derive(Debug, thiserror::Error)]
pub enum LibolmPickleError {
/// The pickle is missing a valid version.
Expand Down Expand Up @@ -262,6 +261,55 @@ pub enum LibolmPickleError {
Encode(#[from] matrix_pickle::EncodeError),
}

/// Error type describing the various ways dehydrated devices can fail to be
/// decoded.
#[derive(Debug, thiserror::Error)]
pub enum DehydratedDeviceError {
/// The pickle is missing a valid version.
#[error("The pickle doesn't contain a version")]
MissingVersion,
/// The pickle has a unsupported version.
#[error("The pickle uses an unsupported version, expected {0}, got {1}")]
Version(u32, u32),
/// Invalid nonce.
#[error("The nonce was invalid")]
InvalidNonce,
/// The pickle wasn't valid base64.
#[error("The pickle wasn't valid base64: {0}")]
Base64(#[from] Base64DecodeError),
/// The pickle could not have been decrypted.
#[error("The pickle couldn't be decrypted: {0}")]
Decryption(chacha20poly1305::aead::Error),
/// The pickle contains an invalid public key.
#[error("The pickle contained an invalid ed25519 public key {0}")]
PublicKey(#[from] KeyError),
/// The payload of the pickle could not be decoded.
#[error(transparent)]
Decode(#[from] matrix_pickle::DecodeError),
/// The object could not be encoded as a pickle.
#[error(transparent)]
Encode(#[from] matrix_pickle::EncodeError),
}

impl From<LibolmPickleError> for DehydratedDeviceError {
fn from(err: LibolmPickleError) -> Self {
match err {
LibolmPickleError::PublicKey(e) => Self::PublicKey(e),
LibolmPickleError::Decode(e) => Self::Decode(e),
LibolmPickleError::Encode(e) => Self::Encode(e),
// Any other errors should not happen when we're dealing with
// dehydrated devices.
_ => panic!("Unexpected libolm pickle error {}", err),
}
}
}

impl From<chacha20poly1305::aead::Error> for DehydratedDeviceError {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using Decryption(#[from] chacha20poly1305::aead::Error), but it was complaining about some missing traits. So I had to do this instead. I'm not sure if there's a better way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to enable the std feature on the chacha20poly1305 crate for this to work. It requires the std::error::Error trait to be defined for aead::Error.

diff --git a/Cargo.toml b/Cargo.toml
index a44933a..c9c761d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -50,7 +50,7 @@ arrayvec = { version = "0.7.6", features = ["serde"] }
 base64 = "0.22.1"
 base64ct = { version = "1.6.0", features = ["std", "alloc"] }
 cbc = { version = "0.1.2", features = ["std"] }
-chacha20poly1305 = "0.10.1"
+chacha20poly1305 = { version = "0.10.1", features = ["std"] }
 curve25519-dalek = { version = "4.1.3", default-features = false, features = ["zeroize"] }
 ed25519-dalek = { version = "2.1.1", default-features = false, features = ["rand_core", "std", "serde", "hazmat", "zeroize"] }
 getrandom = "0.2.15"

fn from(error: chacha20poly1305::aead::Error) -> Self {
Self::Decryption(error)
}
}

/// Error type describing the different ways message decoding can fail.
#[derive(Debug, thiserror::Error)]
pub enum DecodeError {
Expand Down
Loading
Loading