crypto: Hold a copy of the account to get the ed25519 key when doing SAS.

master
Damir Jelić 2020-07-23 11:19:19 +02:00
parent 9214f01185
commit b1ae5534a1
1 changed files with 21 additions and 24 deletions

View File

@ -15,7 +15,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::mem; use std::mem;
use crate::Device; use crate::{Account, Device};
use olm_rs::sas::OlmSas; use olm_rs::sas::OlmSas;
@ -35,8 +35,7 @@ use matrix_sdk_common::identifiers::{DeviceId, UserId};
use matrix_sdk_common::uuid::Uuid; use matrix_sdk_common::uuid::Uuid;
struct SasIds { struct SasIds {
own_user_id: UserId, account: Account,
own_device_id: Box<DeviceId>,
other_device: Device, other_device: Device,
} }
@ -69,7 +68,7 @@ struct Sas<S> {
impl<S> Sas<S> { impl<S> Sas<S> {
pub fn user_id(&self) -> &UserId { pub fn user_id(&self) -> &UserId {
&self.ids.own_user_id &self.ids.account.user_id()
} }
} }
@ -144,14 +143,14 @@ fn get_emoji(index: u8) -> (&'static str, &'static str) {
} }
impl Sas<Created> { impl Sas<Created> {
fn new(own_user_id: UserId, own_device_id: &DeviceId, other_device: Device) -> Sas<Created> { fn new(account: Account, other_device: Device) -> Sas<Created> {
let verification_flow_id = Uuid::new_v4().to_string(); let verification_flow_id = Uuid::new_v4().to_string();
let from_device: Box<DeviceId> = account.device_id().into();
Sas { Sas {
inner: OlmSas::new(), inner: OlmSas::new(),
ids: SasIds { ids: SasIds {
own_user_id, account,
own_device_id: own_device_id.into(),
other_device, other_device,
}, },
verification_flow_id: verification_flow_id.clone(), verification_flow_id: verification_flow_id.clone(),
@ -159,7 +158,7 @@ impl Sas<Created> {
state: Created { state: Created {
protocol_definitions: MSasV1ContentOptions { protocol_definitions: MSasV1ContentOptions {
transaction_id: verification_flow_id, transaction_id: verification_flow_id,
from_device: own_device_id.into(), from_device,
short_authentication_string: vec![ short_authentication_string: vec![
ShortAuthenticationString::Decimal, ShortAuthenticationString::Decimal,
ShortAuthenticationString::Emoji, ShortAuthenticationString::Emoji,
@ -204,8 +203,7 @@ struct Started {
impl Sas<Started> { impl Sas<Started> {
fn from_start_event( fn from_start_event(
own_user_id: &UserId, account: Account,
own_device_id: &DeviceId,
other_device: Device, other_device: Device,
event: &ToDeviceEvent<StartEventContent>, event: &ToDeviceEvent<StartEventContent>,
) -> Sas<Started> { ) -> Sas<Started> {
@ -219,8 +217,7 @@ impl Sas<Started> {
inner: OlmSas::new(), inner: OlmSas::new(),
ids: SasIds { ids: SasIds {
own_user_id: own_user_id.clone(), account,
own_device_id: own_device_id.into(),
other_device, other_device,
}, },
@ -314,8 +311,8 @@ impl Sas<KeyReceived> {
format!( format!(
"MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\ "MATRIX_KEY_VERIFICATION_SAS{first_user}{first_device}\
{second_user}{second_device}{transaction_id}", {second_user}{second_device}{transaction_id}",
first_user = self.ids.own_user_id, first_user = self.ids.account.user_id(),
first_device = self.ids.own_device_id, first_device = self.ids.account.device_id(),
second_user = self.ids.other_device.user_id(), second_user = self.ids.other_device.user_id(),
second_device = self.ids.other_device.device_id(), second_device = self.ids.other_device.device_id(),
transaction_id = self.verification_flow_id, transaction_id = self.verification_flow_id,
@ -326,8 +323,8 @@ impl Sas<KeyReceived> {
{second_user}{second_device}{transaction_id}", {second_user}{second_device}{transaction_id}",
first_user = self.ids.other_device.user_id(), first_user = self.ids.other_device.user_id(),
first_device = self.ids.other_device.device_id(), first_device = self.ids.other_device.device_id(),
second_user = self.ids.own_user_id, second_user = self.ids.account.user_id(),
second_device = self.ids.own_device_id, second_device = self.ids.account.device_id(),
transaction_id = self.verification_flow_id, transaction_id = self.verification_flow_id,
) )
} }
@ -400,8 +397,8 @@ impl Sas<Confirmed> {
format!( format!(
"MATRIX_KEY_VERIFICATION_MAC{first_user}{first_device}\ "MATRIX_KEY_VERIFICATION_MAC{first_user}{first_device}\
{second_user}{second_device}{transaction_id}", {second_user}{second_device}{transaction_id}",
first_user = self.ids.own_user_id, first_user = self.ids.account.user_id(),
first_device = self.ids.own_device_id, first_device = self.ids.account.device_id(),
second_user = self.ids.other_device.user_id(), second_user = self.ids.other_device.user_id(),
second_device = self.ids.other_device.device_id(), second_device = self.ids.other_device.device_id(),
transaction_id = self.verification_flow_id, transaction_id = self.verification_flow_id,
@ -413,13 +410,14 @@ impl Sas<Confirmed> {
let info = self.get_mac_info(); let info = self.get_mac_info();
let key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.ids.own_device_id.clone()); let key_id =
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.ids.account.device_id().into());
let key = self.ids.account.identity_keys().ed25519();
// TODO the MAC needs to include our own ed25519 key.
mac.insert( mac.insert(
key_id.to_string(), key_id.to_string(),
self.inner self.inner
.calculate_mac("TODO", &format!("{}{}", info, key_id)) .calculate_mac(key, &format!("{}{}", info, key_id))
.expect("Can't calculate SAS MAC"), .expect("Can't calculate SAS MAC"),
); );
@ -496,13 +494,12 @@ mod test {
let bob = Account::new(&bob_id(), &bob_device_id()); let bob = Account::new(&bob_id(), &bob_device_id());
let bob_device = Device::from_account(&bob).await; let bob_device = Device::from_account(&bob).await;
let alice_sas = Sas::<Created>::new(alice_id(), &alice_device_id(), bob_device); let alice_sas = Sas::<Created>::new(alice.clone(), bob_device);
let start_content = alice_sas.get_start_event(); let start_content = alice_sas.get_start_event();
let event = wrap_to_device_event(alice_sas.user_id(), start_content); let event = wrap_to_device_event(alice_sas.user_id(), start_content);
let bob_sas = let bob_sas = Sas::<Started>::from_start_event(bob.clone(), alice_device, &event);
Sas::<Started>::from_start_event(bob.user_id(), bob.device_id(), alice_device, &event);
(alice_sas, bob_sas) (alice_sas, bob_sas)
} }