Move the EventEncryptionAlgorithm into a more logical place

This commit is contained in:
Damir Jelić
2022-08-15 12:06:53 +02:00
parent 0673ad315f
commit 8cdc609876
18 changed files with 80 additions and 81 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ pub use error::{
use js_int::UInt;
pub use logger::{set_logger, Logger};
pub use machine::{KeyRequestPair, OlmMachine};
use matrix_sdk_crypto::types::{events::EventEncryptionAlgorithm, SigningKey};
use matrix_sdk_crypto::types::{EventEncryptionAlgorithm, SigningKey};
pub use responses::{
BootstrapCrossSigningResult, DeviceLists, KeysImportResult, OutgoingVerificationRequest,
Request, RequestType, SignatureUploadRequest, UploadSigningKeysRequest,
@@ -64,10 +64,8 @@ impl From<&EncryptionSettings> for matrix_sdk_crypto::olm::EncryptionSettings {
let algorithm: ruma::EventEncryptionAlgorithm = value.algorithm.clone().into();
Self {
algorithm: matrix_sdk_crypto::types::events::EventEncryptionAlgorithm::from(
algorithm.as_str(),
)
.into(),
algorithm: matrix_sdk_crypto::types::EventEncryptionAlgorithm::from(algorithm.as_str())
.into(),
rotation_period: Duration::from_micros(value.rotation_period),
rotation_period_msgs: value.rotation_period_messages,
history_visibility: value.history_visibility.clone().into(),
@@ -19,7 +19,7 @@ pub enum EncryptionAlgorithm {
MegolmV2AesSha2,
}
impl From<EncryptionAlgorithm> for matrix_sdk_crypto::types::events::EventEncryptionAlgorithm {
impl From<EncryptionAlgorithm> for matrix_sdk_crypto::types::EventEncryptionAlgorithm {
fn from(value: EncryptionAlgorithm) -> Self {
use EncryptionAlgorithm::*;
@@ -31,9 +31,9 @@ impl From<EncryptionAlgorithm> for matrix_sdk_crypto::types::events::EventEncryp
}
}
impl From<matrix_sdk_crypto::types::events::EventEncryptionAlgorithm> for EncryptionAlgorithm {
fn from(value: matrix_sdk_crypto::types::events::EventEncryptionAlgorithm) -> Self {
use matrix_sdk_crypto::types::events::EventEncryptionAlgorithm::*;
impl From<matrix_sdk_crypto::types::EventEncryptionAlgorithm> for EncryptionAlgorithm {
fn from(value: matrix_sdk_crypto::types::EventEncryptionAlgorithm) -> Self {
use matrix_sdk_crypto::types::EventEncryptionAlgorithm::*;
match value {
OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
@@ -44,11 +44,14 @@ use crate::{
requests::{OutgoingRequest, ToDeviceRequest},
session_manager::GroupSessionCache,
store::{Changes, CryptoStoreError, SecretImportError, Store},
types::events::{
forwarded_room_key::{ForwardedMegolmV1AesSha2Content, ForwardedRoomKeyContent},
olm_v1::{DecryptedForwardedRoomKeyEvent, DecryptedSecretSendEvent},
secret_send::SecretSendContent,
EventEncryptionAlgorithm, EventType,
types::{
events::{
forwarded_room_key::{ForwardedMegolmV1AesSha2Content, ForwardedRoomKeyContent},
olm_v1::{DecryptedForwardedRoomKeyEvent, DecryptedSecretSendEvent},
secret_send::SecretSendContent,
EventType,
},
EventEncryptionAlgorithm,
},
Device,
};
@@ -45,9 +45,9 @@ use crate::{
types::{
events::{
forwarded_room_key::ForwardedRoomKeyContent,
room::encrypted::ToDeviceEncryptedEventContent, EventEncryptionAlgorithm, EventType,
room::encrypted::ToDeviceEncryptedEventContent, EventType,
},
DeviceKey, DeviceKeys, Signatures, SignedKey, SigningKey,
DeviceKey, DeviceKeys, EventEncryptionAlgorithm, Signatures, SignedKey, SigningKey,
},
verification::VerificationMachine,
OutgoingVerificationRequest, ReadOnlyAccount, Sas, ToDeviceRequest, VerificationRequest,
+1 -2
View File
@@ -61,9 +61,8 @@ use crate::{
EncryptedToDeviceEvent, OlmV1Curve25519AesSha2Content,
OlmV2Curve25519AesSha2Content, ToDeviceEncryptedEventContent,
},
EventEncryptionAlgorithm,
},
CrossSigningKey, DeviceKeys, OneTimeKey, SignedKey,
CrossSigningKey, DeviceKeys, EventEncryptionAlgorithm, OneTimeKey, SignedKey,
},
utilities::encode,
CryptoStoreError, OlmError, SignatureError,
@@ -46,9 +46,8 @@ use crate::{
events::{
forwarded_room_key::ForwardedMegolmV1AesSha2Content,
room::encrypted::{EncryptedEvent, RoomEventEncryptionScheme},
EventEncryptionAlgorithm,
},
serialize_curve_key, SigningKeys,
serialize_curve_key, EventEncryptionAlgorithm, SigningKeys,
},
};
@@ -29,11 +29,8 @@ use vodozemac::{megolm::SessionKeyDecodeError, Curve25519PublicKey};
use crate::types::{
deserialize_curve_key,
events::{
forwarded_room_key::{ForwardedMegolmV1AesSha2Content, ForwardedRoomKeyContent},
EventEncryptionAlgorithm,
},
serialize_curve_key, SigningKey, SigningKeys,
events::forwarded_room_key::{ForwardedMegolmV1AesSha2Content, ForwardedRoomKeyContent},
serialize_curve_key, EventEncryptionAlgorithm, SigningKey, SigningKeys,
};
/// An error type for the creation of group sessions.
@@ -43,12 +43,14 @@ pub use vodozemac::{
use super::SessionCreationError;
use crate::{
types::events::{
room::encrypted::{
MegolmV1AesSha2Content, MegolmV2AesSha2Content, RoomEncryptedEventContent,
RoomEventEncryptionScheme,
types::{
events::{
room::encrypted::{
MegolmV1AesSha2Content, MegolmV2AesSha2Content, RoomEncryptedEventContent,
RoomEventEncryptionScheme,
},
room_key::{MegolmV1AesSha2Content as MegolmV1AesSha2RoomKeyContent, RoomKeyContent},
},
room_key::{MegolmV1AesSha2Content as MegolmV1AesSha2RoomKeyContent, RoomKeyContent},
EventEncryptionAlgorithm,
},
Device, ToDeviceRequest,
+2 -2
View File
@@ -26,8 +26,8 @@ use vodozemac::{
use super::IdentityKeys;
use crate::{
error::{EventError, OlmResult},
types::events::{
room::encrypted::{
types::{
events::room::encrypted::{
OlmV1Curve25519AesSha2Content, OlmV2Curve25519AesSha2Content,
ToDeviceEncryptedEventContent,
},
@@ -637,9 +637,7 @@ mod tests {
};
use serde_json::{json, Value};
use crate::{
types::events::EventEncryptionAlgorithm, EncryptionSettings, LocalTrust, OlmMachine,
};
use crate::{types::EventEncryptionAlgorithm, EncryptionSettings, LocalTrust, OlmMachine};
fn alice_id() -> &'static UserId {
user_id!("@alice:example.org")
@@ -38,7 +38,7 @@ use crate::{
olm::Account,
requests::{OutgoingRequest, ToDeviceRequest},
store::{Changes, Result as StoreResult, Store},
types::events::{EventEncryptionAlgorithm, EventType},
types::{events::EventType, EventEncryptionAlgorithm},
ReadOnlyDevice,
};
@@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
use serde_json::{value::to_raw_value, Value};
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
use super::{events::EventEncryptionAlgorithm, Signatures};
use super::{EventEncryptionAlgorithm, Signatures};
/// Identity keys for a device.
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -21,10 +21,10 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use vodozemac::{megolm::ExportedSessionKey, Curve25519PublicKey, Ed25519PublicKey};
use super::{EventEncryptionAlgorithm, EventType, ToDeviceEvent};
use super::{EventType, ToDeviceEvent};
use crate::types::{
deserialize_curve_key, deserialize_curve_key_vec, deserialize_ed25519_key, serialize_curve_key,
serialize_curve_key_vec, serialize_ed25519_key,
serialize_curve_key_vec, serialize_ed25519_key, EventEncryptionAlgorithm,
};
/// The `m.forwarded_room_key` to-device event.
@@ -25,7 +25,7 @@ pub mod room_key;
pub mod secret_send;
mod to_device;
use ruma::serde::{Raw, StringEnum};
use ruma::serde::Raw;
pub use to_device::{ToDeviceCustomEvent, ToDeviceEvent, ToDeviceEvents};
/// A trait for event contents to define their event type.
@@ -53,40 +53,3 @@ where
{
serde_json::from_str(string).map_err(serde::de::Error::custom)
}
// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of
// this crate. Used for string enums because their `_Custom` variant can't be
// truly private (only `#[doc(hidden)]`).
#[doc(hidden)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrivOwnedStr(Box<str>);
impl std::fmt::Debug for PrivOwnedStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
/// An encryption algorithm to be used to encrypt messages sent to a room.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
#[non_exhaustive]
pub enum EventEncryptionAlgorithm {
/// Olm version 1 using Curve25519, AES-256, and SHA-256.
#[ruma_enum(rename = "m.olm.v1.curve25519-aes-sha2")]
OlmV1Curve25519AesSha2,
/// Olm version 2 using Curve25519, AES-256, and SHA-256.
#[ruma_enum(rename = "m.olm.v2.curve25519-aes-sha2")]
OlmV2Curve25519AesSha2,
/// Megolm version 1 using AES-256 and SHA-256.
#[ruma_enum(rename = "m.megolm.v1.aes-sha2")]
MegolmV1AesSha2,
/// Megolm version 2 using AES-256 and SHA-256.
#[ruma_enum(rename = "m.megolm.v2.aes-sha2")]
MegolmV2AesSha2,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
@@ -24,8 +24,8 @@ use vodozemac::{megolm::MegolmMessage, olm::OlmMessage, Curve25519PublicKey};
use super::Event;
use crate::types::{
deserialize_curve_key,
events::{EventEncryptionAlgorithm, EventType, ToDeviceEvent},
serialize_curve_key,
events::{EventType, ToDeviceEvent},
serialize_curve_key, EventEncryptionAlgorithm,
};
/// An m.room.encrypted room event.
@@ -21,7 +21,8 @@ use serde::{Deserialize, Serialize};
use serde_json::{value::to_raw_value, Value};
use vodozemac::megolm::SessionKey;
use super::{EventEncryptionAlgorithm, EventType, ToDeviceEvent};
use super::{EventType, ToDeviceEvent};
use crate::types::EventEncryptionAlgorithm;
/// The `m.room_key` to-device event.
pub type RoomKeyEvent = ToDeviceEvent<RoomKeyContent>;
+40 -1
View File
@@ -41,7 +41,9 @@ pub use backup::*;
pub use cross_signing_key::*;
pub use device_keys::*;
pub use one_time_keys::*;
use ruma::{DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceKeyId, OwnedUserId, UserId};
use ruma::{
serde::StringEnum, DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceKeyId, OwnedUserId, UserId,
};
use serde::{Deserialize, Serialize, Serializer};
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey, Ed25519Signature, KeyError};
@@ -319,6 +321,43 @@ impl Algorithm for DeviceKeyAlgorithm {
}
}
// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of
// this crate. Used for string enums because their `_Custom` variant can't be
// truly private (only `#[doc(hidden)]`).
#[doc(hidden)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrivOwnedStr(Box<str>);
impl std::fmt::Debug for PrivOwnedStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
/// An encryption algorithm to be used to encrypt messages sent to a room.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
#[non_exhaustive]
pub enum EventEncryptionAlgorithm {
/// Olm version 1 using Curve25519, AES-256, and SHA-256.
#[ruma_enum(rename = "m.olm.v1.curve25519-aes-sha2")]
OlmV1Curve25519AesSha2,
/// Olm version 2 using Curve25519, AES-256, and SHA-256.
#[ruma_enum(rename = "m.olm.v2.curve25519-aes-sha2")]
OlmV2Curve25519AesSha2,
/// Megolm version 1 using AES-256 and SHA-256.
#[ruma_enum(rename = "m.megolm.v1.aes-sha2")]
MegolmV1AesSha2,
/// Megolm version 2 using AES-256 and SHA-256.
#[ruma_enum(rename = "m.megolm.v2.aes-sha2")]
MegolmV2AesSha2,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
impl<T: Ord + Serialize> Serialize for SigningKeys<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where