From 1691a261635aeff797a8415478d0c6a21e922b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 14 Jul 2020 17:04:08 +0200 Subject: [PATCH] crypto: Add initial Sas scaffolding. --- matrix_sdk_common/Cargo.toml | 2 +- matrix_sdk_crypto/src/lib.rs | 1 + matrix_sdk_crypto/src/verification/mod.rs | 2 + matrix_sdk_crypto/src/verification/sas.rs | 125 ++++++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 matrix_sdk_crypto/src/verification/mod.rs create mode 100644 matrix_sdk_crypto/src/verification/sas.rs diff --git a/matrix_sdk_common/Cargo.toml b/matrix_sdk_common/Cargo.toml index 2bdc5f65..2ab514b0 100644 --- a/matrix_sdk_common/Cargo.toml +++ b/matrix_sdk_common/Cargo.toml @@ -15,7 +15,7 @@ instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] } js_int = "0.1.8" [dependencies.ruma] -git = "https://github.com/ruma/ruma" +path = "/home/poljar/werk/priv/ruma/ruma" features = ["client-api"] rev = "c19bcaab" diff --git a/matrix_sdk_crypto/src/lib.rs b/matrix_sdk_crypto/src/lib.rs index cd5556bc..e7c26a06 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -32,6 +32,7 @@ mod machine; mod memory_stores; mod olm; mod store; +mod verification; pub use device::{Device, TrustState}; pub use error::{MegolmError, OlmError}; diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs new file mode 100644 index 00000000..c994a095 --- /dev/null +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -0,0 +1,2 @@ +#[allow(dead_code)] +mod sas; diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs new file mode 100644 index 00000000..01d07380 --- /dev/null +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -0,0 +1,125 @@ +use crate::Device; + +use matrix_sdk_common::events::key::verification::{ + start::{StartEvent, StartEventContent}, + accept::AcceptEvent, + HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, + VerificationMethod, +}; +use matrix_sdk_common::identifiers::{DeviceId, UserId}; +use matrix_sdk_common::uuid::Uuid; + +struct SasIds { + own_user_id: UserId, + own_device_id: DeviceId, + other_device: Device, +} + +struct ProtocolDefinitions { + key_agreement_protocols: Vec, + hashes: Vec, + message_auth_codes: Vec, + short_auth_string: Vec, +} + +struct AcceptedProtocols { + method: VerificationMethod, + key_agreement_protocol: KeyAgreementProtocol, + hash: HashAlgorithm, + message_auth_code: MessageAuthenticationCode, + short_auth_string: Vec +} + +struct Sas { + ids: SasIds, + verification_flow_id: Uuid, + protocol_definitions: ProtocolDefinitions, + state: S, +} + +impl Sas { + fn new(own_user_id: UserId, own_device_id: DeviceId, other_device: Device) -> Sas { + Sas { + ids: SasIds { + own_user_id, + own_device_id, + other_device, + }, + verification_flow_id: Uuid::new_v4(), + + protocol_definitions: ProtocolDefinitions { + short_auth_string: vec![ + ShortAuthenticationString::Decimal, + ShortAuthenticationString::Emoji, + ], + key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], + message_auth_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], + hashes: vec![HashAlgorithm::Sha256], + }, + + state: Created {}, + } + } + + fn into_accepted(self, event: &AcceptEvent) -> Sas { + let content = &event.content; + + Sas { + ids: self.ids, + verification_flow_id: self.verification_flow_id, + protocol_definitions: self.protocol_definitions, + state: Accepted { + commitment: content.commitment.clone(), + accepted_protocols: AcceptedProtocols { + method: content.method, + hash: content.hash, + key_agreement_protocol: content.key_agreement_protocol, + message_auth_code: content.message_authentication_code, + short_auth_string: content.short_authentication_string.clone(), + } + }, + } + } +} + +struct Created {} + +struct Started {} + +impl Sas { + fn from_start_event( + own_user_id: UserId, + own_device_id: DeviceId, + other_device: Device, + event: &StartEvent, + ) -> Sas { + let content = if let StartEventContent::MSasV1(content) = &event.content { + content + } else { + panic!("Invalid sas version") + }; + + Sas { + ids: SasIds { + own_user_id, + own_device_id, + other_device, + }, + verification_flow_id: Uuid::new_v4(), + + protocol_definitions: ProtocolDefinitions { + short_auth_string: content.short_authentication_string.clone(), + key_agreement_protocols: content.key_agreement_protocols.clone(), + message_auth_codes: content.message_authentication_codes.clone(), + hashes: content.hashes.clone(), + }, + + state: Started {}, + } + } +} + +struct Accepted { + accepted_protocols: AcceptedProtocols, + commitment: String, +}