crypto: More Sas states and add an initial test.

master
Damir Jelić 2020-07-22 13:43:11 +02:00
parent a7bc1a95d3
commit 7a2d5c30db
1 changed files with 167 additions and 37 deletions

View File

@ -1,10 +1,17 @@
use crate::Device; use crate::Device;
use matrix_sdk_common::events::key::verification::{ use olm_rs::sas::OlmSas;
use matrix_sdk_common::events::{
key::verification::{
accept::AcceptEvent, accept::AcceptEvent,
start::{StartEvent, StartEventContent}, key::KeyEvent,
mac::MacEvent,
start::{MSasV1Content, MSasV1ContentOptions, StartEvent, StartEventContent},
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
VerificationMethod, VerificationMethod,
},
ToDeviceEvent,
}; };
use matrix_sdk_common::identifiers::{DeviceId, UserId}; use matrix_sdk_common::identifiers::{DeviceId, UserId};
use matrix_sdk_common::uuid::Uuid; use matrix_sdk_common::uuid::Uuid;
@ -15,13 +22,6 @@ struct SasIds {
other_device: Device, other_device: Device,
} }
struct ProtocolDefinitions {
key_agreement_protocols: Vec<KeyAgreementProtocol>,
hashes: Vec<HashAlgorithm>,
message_auth_codes: Vec<MessageAuthenticationCode>,
short_auth_string: Vec<ShortAuthenticationString>,
}
struct AcceptedProtocols { struct AcceptedProtocols {
method: VerificationMethod, method: VerificationMethod,
key_agreement_protocol: KeyAgreementProtocol, key_agreement_protocol: KeyAgreementProtocol,
@ -31,43 +31,61 @@ struct AcceptedProtocols {
} }
struct Sas<S> { struct Sas<S> {
inner: OlmSas,
ids: SasIds, ids: SasIds,
verification_flow_id: Uuid, verification_flow_id: Uuid,
protocol_definitions: ProtocolDefinitions,
state: S, state: S,
} }
impl<S> Sas<S> {
pub fn user_id(&self) -> &UserId {
&self.ids.own_user_id
}
}
impl Sas<Created> { impl Sas<Created> {
fn new(own_user_id: UserId, own_device_id: &DeviceId, other_device: Device) -> Sas<Created> { fn new(own_user_id: UserId, own_device_id: &DeviceId, other_device: Device) -> Sas<Created> {
let verification_flow_id = Uuid::new_v4();
Sas { Sas {
inner: OlmSas::new(),
ids: SasIds { ids: SasIds {
own_user_id, own_user_id,
own_device_id: own_device_id.into(), own_device_id: own_device_id.into(),
other_device, other_device,
}, },
verification_flow_id: Uuid::new_v4(), verification_flow_id,
protocol_definitions: ProtocolDefinitions { state: Created {
short_auth_string: vec![ protocol_definitions: MSasV1ContentOptions {
transaction_id: verification_flow_id.to_string(),
from_device: own_device_id.into(),
short_authentication_string: vec![
ShortAuthenticationString::Decimal, ShortAuthenticationString::Decimal,
ShortAuthenticationString::Emoji, ShortAuthenticationString::Emoji,
], ],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519HkdfSha256],
message_auth_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
hashes: vec![HashAlgorithm::Sha256], hashes: vec![HashAlgorithm::Sha256],
}, },
},
state: Created {},
} }
} }
fn get_start_event(&self) -> StartEventContent {
StartEventContent::MSasV1(
MSasV1Content::new(self.state.protocol_definitions.clone())
.expect("Invalid initial protocol definitions."),
)
}
fn into_accepted(self, event: &AcceptEvent) -> Sas<Accepted> { fn into_accepted(self, event: &AcceptEvent) -> Sas<Accepted> {
let content = &event.content; let content = &event.content;
Sas { Sas {
inner: self.inner,
ids: self.ids, ids: self.ids,
verification_flow_id: self.verification_flow_id, verification_flow_id: self.verification_flow_id,
protocol_definitions: self.protocol_definitions,
state: Accepted { state: Accepted {
commitment: content.commitment.clone(), commitment: content.commitment.clone(),
accepted_protocols: AcceptedProtocols { accepted_protocols: AcceptedProtocols {
@ -82,16 +100,20 @@ impl Sas<Created> {
} }
} }
struct Created {} struct Created {
protocol_definitions: MSasV1ContentOptions,
}
struct Started {} struct Started {
protocol_definitions: MSasV1Content,
}
impl Sas<Started> { impl Sas<Started> {
fn from_start_event( fn from_start_event(
own_user_id: UserId, own_user_id: &UserId,
own_device_id: &DeviceId, own_device_id: &DeviceId,
other_device: Device, other_device: Device,
event: &StartEvent, event: &ToDeviceEvent<StartEventContent>,
) -> Sas<Started> { ) -> Sas<Started> {
let content = if let StartEventContent::MSasV1(content) = &event.content { let content = if let StartEventContent::MSasV1(content) = &event.content {
content content
@ -100,26 +122,134 @@ impl Sas<Started> {
}; };
Sas { Sas {
inner: OlmSas::new(),
ids: SasIds { ids: SasIds {
own_user_id, own_user_id: own_user_id.clone(),
own_device_id: own_device_id.into(), own_device_id: own_device_id.into(),
other_device, other_device,
}, },
verification_flow_id: Uuid::new_v4(), verification_flow_id: Uuid::new_v4(),
protocol_definitions: ProtocolDefinitions { state: Started {
short_auth_string: content.short_authentication_string.clone(), protocol_definitions: content.clone(),
key_agreement_protocols: content.key_agreement_protocols.clone(),
message_auth_codes: content.message_authentication_codes.clone(),
hashes: content.hashes.clone(),
}, },
state: Started {},
} }
} }
fn into_key_received(self, event: &KeyEvent) -> Sas<KeyReceived> {
todo!()
}
} }
struct Accepted { struct Accepted {
accepted_protocols: AcceptedProtocols, accepted_protocols: AcceptedProtocols,
commitment: String, commitment: String,
} }
impl Sas<Accepted> {
fn into_key_received(self, event: &KeyEvent) -> Sas<KeyReceived> {
todo!()
}
}
struct KeyReceived {
accepted_protocols: AcceptedProtocols,
}
impl Sas<KeyReceived> {
fn into_mac_received(self, event: &MacEvent) -> Sas<MacReceived> {
todo!()
}
fn confirm(self) -> Sas<Confirmed> {
todo!()
}
}
struct Confirmed {
accepted_protocols: AcceptedProtocols,
}
impl Sas<Confirmed> {
fn confirm(self) -> Sas<Done> {
todo!()
}
}
struct MacReceived {
verified_devices: Vec<String>,
verified_master_keys: Vec<String>,
}
impl Sas<MacReceived> {
fn into_done(self, event: &MacEvent) -> Sas<Done> {
todo!()
}
}
struct Done {
verified_devices: Vec<String>,
verified_master_keys: Vec<String>,
}
#[cfg(test)]
mod test {
use std::convert::TryFrom;
use crate::{Account, Device};
use matrix_sdk_common::events::key::verification::{
accept::AcceptEvent,
key::KeyEvent,
mac::MacEvent,
start::{MSasV1Content, MSasV1ContentOptions, StartEvent, StartEventContent},
};
use matrix_sdk_common::events::{AnyToDeviceEvent, ToDeviceEvent};
use matrix_sdk_common::identifiers::{DeviceId, UserId};
use super::{Created, Sas, Started};
fn alice_id() -> UserId {
UserId::try_from("@alice:example.org").unwrap()
}
fn alice_device_id() -> Box<DeviceId> {
"JLAFKJWSCS".into()
}
fn bob_id() -> UserId {
UserId::try_from("@bob:example.org").unwrap()
}
fn bob_device_id() -> Box<DeviceId> {
"BOBDEVCIE".into()
}
fn wrap_start_event(
sender: &UserId,
content: StartEventContent,
) -> ToDeviceEvent<StartEventContent> {
ToDeviceEvent {
sender: sender.clone(),
content,
}
}
#[tokio::test]
async fn create_sas() {
let alice = Account::new(&alice_id(), &alice_device_id());
let alice_device = Device::from_account(&alice).await;
let bob = Account::new(&bob_id(), &bob_device_id());
let bob_device = Device::from_account(&bob).await;
let alice_sas = Sas::<Created>::new(alice_id(), &alice_device_id(), bob_device);
let start_content = alice_sas.get_start_event();
let event = wrap_start_event(alice_sas.user_id(), start_content);
let bob_sas =
Sas::<Started>::from_start_event(bob.user_id(), bob.device_id(), alice_device, &event);
}
}