2020-07-23 07:21:11 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
use std::{
|
|
|
|
mem,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
|
|
|
|
|
|
|
use olm_rs::{sas::OlmSas, utility::OlmUtility};
|
|
|
|
|
|
|
|
use matrix_sdk_common::{
|
|
|
|
events::{
|
|
|
|
key::verification::{
|
|
|
|
accept::AcceptEventContent,
|
|
|
|
cancel::{CancelCode, CancelEventContent},
|
|
|
|
key::KeyEventContent,
|
|
|
|
mac::MacEventContent,
|
|
|
|
start::{MSasV1Content, MSasV1ContentOptions, StartEventContent},
|
|
|
|
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode,
|
|
|
|
ShortAuthenticationString, VerificationMethod,
|
|
|
|
},
|
|
|
|
AnyToDeviceEvent, AnyToDeviceEventContent, ToDeviceEvent,
|
2020-07-22 11:43:11 +00:00
|
|
|
},
|
2020-07-27 11:16:56 +00:00
|
|
|
identifiers::{DeviceId, UserId},
|
|
|
|
uuid::Uuid,
|
2020-07-14 15:04:08 +00:00
|
|
|
};
|
|
|
|
|
2020-07-23 12:26:50 +00:00
|
|
|
use super::{get_decimal, get_emoji, get_mac_content, receive_mac_event, SasIds};
|
2020-07-23 12:02:07 +00:00
|
|
|
use crate::{Account, Device};
|
2020-07-14 15:04:08 +00:00
|
|
|
|
2020-07-24 13:49:00 +00:00
|
|
|
#[derive(Clone)]
|
2020-07-24 15:51:20 +00:00
|
|
|
/// Short authentication string object.
|
2020-07-24 13:49:00 +00:00
|
|
|
struct Sas {
|
|
|
|
inner: Arc<Mutex<InnerSas>>,
|
|
|
|
account: Account,
|
|
|
|
other_device: Device,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sas {
|
2020-07-27 11:16:56 +00:00
|
|
|
const KEY_AGREEMENT_PROTOCOLS: &'static [KeyAgreementProtocol] =
|
|
|
|
&[KeyAgreementProtocol::Curve25519HkdfSha256];
|
|
|
|
const HASHES: &'static [HashAlgorithm] = &[HashAlgorithm::Sha256];
|
|
|
|
const MACS: &'static [MessageAuthenticationCode] = &[MessageAuthenticationCode::HkdfHmacSha256];
|
|
|
|
const STRINGS: &'static [ShortAuthenticationString] = &[
|
|
|
|
ShortAuthenticationString::Decimal,
|
|
|
|
ShortAuthenticationString::Emoji,
|
|
|
|
];
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
/// Get our own user id.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn user_id(&self) -> &UserId {
|
|
|
|
self.account.user_id()
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
/// Get our own device id.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn device_id(&self) -> &DeviceId {
|
|
|
|
self.account.device_id()
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
/// Start a new SAS auth flow with the given device.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `account` - Our own account.
|
|
|
|
///
|
|
|
|
/// * `other_device` - The other device which we are going to verify.
|
|
|
|
///
|
|
|
|
/// Returns the new `Sas` object and a `StartEventContent` that needs to be
|
|
|
|
/// sent out through the server to the other device.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn start(account: Account, other_device: Device) -> (Sas, StartEventContent) {
|
|
|
|
let (inner, content) = InnerSas::start(account.clone(), other_device.clone());
|
|
|
|
|
|
|
|
let sas = Sas {
|
|
|
|
inner: Arc::new(Mutex::new(inner)),
|
|
|
|
account,
|
|
|
|
other_device,
|
|
|
|
};
|
|
|
|
|
|
|
|
(sas, content)
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
/// Create a new Sas object from a m.key.verification.start request.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `account` - Our own account.
|
|
|
|
///
|
|
|
|
/// * `other_device` - The other device which we are going to verify.
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.start event that was sent to us by
|
|
|
|
/// the other side.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn from_start_event(
|
|
|
|
account: Account,
|
|
|
|
other_device: Device,
|
|
|
|
event: &ToDeviceEvent<StartEventContent>,
|
2020-07-27 13:56:28 +00:00
|
|
|
) -> Result<Sas, AnyToDeviceEventContent> {
|
2020-07-27 11:16:56 +00:00
|
|
|
let inner = InnerSas::from_start_event(account.clone(), other_device.clone(), event)?;
|
|
|
|
Ok(Sas {
|
2020-07-24 13:49:00 +00:00
|
|
|
inner: Arc::new(Mutex::new(inner)),
|
|
|
|
account,
|
|
|
|
other_device,
|
2020-07-27 11:16:56 +00:00
|
|
|
})
|
2020-07-24 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn accept(&self) -> Option<AcceptEventContent> {
|
|
|
|
self.inner.lock().unwrap().accept()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm(&self) -> Option<MacEventContent> {
|
|
|
|
let mut guard = self.inner.lock().unwrap();
|
|
|
|
let sas: InnerSas = (*guard).clone();
|
|
|
|
let (sas, content) = sas.confirm();
|
|
|
|
*guard = sas;
|
|
|
|
content
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_be_presented(&self) -> bool {
|
|
|
|
self.inner.lock().unwrap().can_be_presented()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_done(&self) -> bool {
|
|
|
|
self.inner.lock().unwrap().is_done()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emoji(&self) -> Option<Vec<(&'static str, &'static str)>> {
|
|
|
|
self.inner.lock().unwrap().emoji()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decimals(&self) -> Option<(u32, u32, u32)> {
|
|
|
|
self.inner.lock().unwrap().decimals()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn receive_event(&self, event: &mut AnyToDeviceEvent) -> Option<AnyToDeviceEventContent> {
|
|
|
|
let mut guard = self.inner.lock().unwrap();
|
|
|
|
let sas: InnerSas = (*guard).clone();
|
|
|
|
let (sas, content) = sas.receive_event(event);
|
|
|
|
*guard = sas;
|
|
|
|
|
|
|
|
content
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verified_devices(&self) -> Option<Arc<Vec<Box<DeviceId>>>> {
|
|
|
|
self.inner.lock().unwrap().verified_devices()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum InnerSas {
|
|
|
|
Created(SasState<Created>),
|
|
|
|
Started(SasState<Started>),
|
|
|
|
Accepted(SasState<Accepted>),
|
|
|
|
KeyRecieved(SasState<KeyReceived>),
|
|
|
|
Confirmed(SasState<Confirmed>),
|
|
|
|
MacReceived(SasState<MacReceived>),
|
|
|
|
Done(SasState<Done>),
|
2020-07-24 15:51:20 +00:00
|
|
|
Canceled(SasState<Canceled>),
|
2020-07-24 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerSas {
|
|
|
|
fn start(account: Account, other_device: Device) -> (InnerSas, StartEventContent) {
|
|
|
|
let sas = SasState::<Created>::new(account, other_device);
|
2020-07-27 11:16:56 +00:00
|
|
|
let content = sas.as_content();
|
2020-07-24 13:49:00 +00:00
|
|
|
(InnerSas::Created(sas), content)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_start_event(
|
|
|
|
account: Account,
|
|
|
|
other_device: Device,
|
|
|
|
event: &ToDeviceEvent<StartEventContent>,
|
2020-07-27 13:56:28 +00:00
|
|
|
) -> Result<InnerSas, AnyToDeviceEventContent> {
|
2020-07-24 15:51:20 +00:00
|
|
|
match SasState::<Started>::from_start_event(account, other_device, event) {
|
2020-07-27 11:16:56 +00:00
|
|
|
Ok(s) => Ok(InnerSas::Started(s)),
|
|
|
|
Err(s) => Err(s.as_content()),
|
2020-07-24 15:51:20 +00:00
|
|
|
}
|
2020-07-24 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn accept(&self) -> Option<AcceptEventContent> {
|
|
|
|
if let InnerSas::Started(s) = self {
|
2020-07-27 11:16:56 +00:00
|
|
|
Some(s.as_content())
|
2020-07-24 13:49:00 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm(self) -> (InnerSas, Option<MacEventContent>) {
|
|
|
|
match self {
|
|
|
|
InnerSas::KeyRecieved(s) => {
|
|
|
|
let sas = s.confirm();
|
2020-07-27 11:18:00 +00:00
|
|
|
let content = sas.as_content();
|
2020-07-24 13:49:00 +00:00
|
|
|
(InnerSas::Confirmed(sas), Some(content))
|
|
|
|
}
|
|
|
|
InnerSas::MacReceived(s) => {
|
|
|
|
let sas = s.confirm();
|
2020-07-27 11:18:00 +00:00
|
|
|
let content = sas.as_content();
|
2020-07-24 13:49:00 +00:00
|
|
|
(InnerSas::Done(sas), Some(content))
|
|
|
|
}
|
|
|
|
_ => (self, None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn receive_event(
|
|
|
|
self,
|
|
|
|
event: &mut AnyToDeviceEvent,
|
|
|
|
) -> (InnerSas, Option<AnyToDeviceEventContent>) {
|
|
|
|
match event {
|
|
|
|
AnyToDeviceEvent::KeyVerificationAccept(e) => {
|
|
|
|
if let InnerSas::Created(s) = self {
|
2020-07-27 11:18:00 +00:00
|
|
|
match s.into_accepted(e) {
|
|
|
|
Ok(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(
|
|
|
|
InnerSas::Accepted(s),
|
|
|
|
Some(AnyToDeviceEventContent::KeyVerificationKey(content)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Err(s) => {
|
2020-07-27 13:56:28 +00:00
|
|
|
let content = s.as_content();
|
2020-07-27 11:18:00 +00:00
|
|
|
(InnerSas::Canceled(s), Some(content))
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 13:49:00 +00:00
|
|
|
} else {
|
|
|
|
(self, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnyToDeviceEvent::KeyVerificationKey(e) => match self {
|
2020-07-27 11:18:00 +00:00
|
|
|
InnerSas::Accepted(s) => match s.into_key_received(e) {
|
|
|
|
Ok(s) => (InnerSas::KeyRecieved(s), None),
|
2020-07-27 13:56:28 +00:00
|
|
|
Err(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(InnerSas::Canceled(s), Some(content))
|
|
|
|
}
|
2020-07-27 11:18:00 +00:00
|
|
|
},
|
2020-07-27 13:28:14 +00:00
|
|
|
InnerSas::Started(s) => match s.into_key_received(e) {
|
|
|
|
Ok(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(
|
|
|
|
InnerSas::KeyRecieved(s),
|
|
|
|
Some(AnyToDeviceEventContent::KeyVerificationKey(content)),
|
|
|
|
)
|
|
|
|
}
|
2020-07-27 13:56:28 +00:00
|
|
|
Err(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(InnerSas::Canceled(s), Some(content))
|
|
|
|
}
|
2020-07-27 13:28:14 +00:00
|
|
|
},
|
2020-07-24 13:49:00 +00:00
|
|
|
_ => (self, None),
|
|
|
|
},
|
|
|
|
AnyToDeviceEvent::KeyVerificationMac(e) => match self {
|
2020-07-27 13:34:18 +00:00
|
|
|
InnerSas::KeyRecieved(s) => match s.into_mac_received(e) {
|
|
|
|
Ok(s) => (InnerSas::MacReceived(s), None),
|
2020-07-27 13:56:28 +00:00
|
|
|
Err(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(InnerSas::Canceled(s), Some(content))
|
|
|
|
}
|
2020-07-27 13:34:18 +00:00
|
|
|
},
|
|
|
|
InnerSas::Confirmed(s) => match s.into_done(e) {
|
|
|
|
Ok(s) => (InnerSas::Done(s), None),
|
2020-07-27 13:56:28 +00:00
|
|
|
Err(s) => {
|
|
|
|
let content = s.as_content();
|
|
|
|
(InnerSas::Canceled(s), Some(content))
|
|
|
|
}
|
2020-07-27 13:34:18 +00:00
|
|
|
},
|
2020-07-24 13:49:00 +00:00
|
|
|
_ => (self, None),
|
|
|
|
},
|
|
|
|
_ => (self, None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_be_presented(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
InnerSas::KeyRecieved(_) => true,
|
|
|
|
InnerSas::MacReceived(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_done(&self) -> bool {
|
|
|
|
if let InnerSas::Done(_) = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emoji(&self) -> Option<Vec<(&'static str, &'static str)>> {
|
|
|
|
match self {
|
|
|
|
InnerSas::KeyRecieved(s) => Some(s.get_emoji()),
|
|
|
|
InnerSas::MacReceived(s) => Some(s.get_emoji()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decimals(&self) -> Option<(u32, u32, u32)> {
|
|
|
|
match self {
|
|
|
|
InnerSas::KeyRecieved(s) => Some(s.get_decimal()),
|
|
|
|
InnerSas::MacReceived(s) => Some(s.get_decimal()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verified_devices(&self) -> Option<Arc<Vec<Box<DeviceId>>>> {
|
|
|
|
if let InnerSas::Done(s) = self {
|
|
|
|
Some(s.verified_devices())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verified_master_keys(&self) -> Option<Arc<Vec<String>>> {
|
|
|
|
if let InnerSas::Done(s) = self {
|
|
|
|
Some(s.verified_master_keys())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Struct containing the protocols that were agreed to be used for the SAS
|
|
|
|
/// flow.
|
2020-07-24 09:26:45 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-14 15:04:08 +00:00
|
|
|
struct AcceptedProtocols {
|
|
|
|
method: VerificationMethod,
|
|
|
|
key_agreement_protocol: KeyAgreementProtocol,
|
|
|
|
hash: HashAlgorithm,
|
|
|
|
message_auth_code: MessageAuthenticationCode,
|
2020-07-22 09:18:26 +00:00
|
|
|
short_auth_string: Vec<ShortAuthenticationString>,
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:11:34 +00:00
|
|
|
impl From<AcceptEventContent> for AcceptedProtocols {
|
|
|
|
fn from(content: AcceptEventContent) -> Self {
|
|
|
|
Self {
|
|
|
|
method: content.method,
|
|
|
|
hash: content.hash,
|
|
|
|
key_agreement_protocol: content.key_agreement_protocol,
|
|
|
|
message_auth_code: content.message_authentication_code,
|
2020-07-23 12:47:47 +00:00
|
|
|
short_auth_string: content.short_authentication_string,
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// A type level state machine modeling the Sas flow.
|
|
|
|
///
|
|
|
|
/// This is the generic struc holding common data between the different states
|
|
|
|
/// and the specific state.
|
2020-07-24 13:49:00 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct SasState<S: Clone> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// The Olm SAS struct.
|
2020-07-24 09:26:45 +00:00
|
|
|
inner: Arc<Mutex<OlmSas>>,
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Struct holding the identities that are doing the SAS dance.
|
2020-07-14 15:04:08 +00:00
|
|
|
ids: SasIds,
|
2020-07-23 15:25:57 +00:00
|
|
|
/// The unique identifier of this SAS flow.
|
|
|
|
///
|
|
|
|
/// This will be the transaction id for to-device events and the relates_to
|
|
|
|
/// field for in-room events.
|
2020-07-24 09:26:45 +00:00
|
|
|
verification_flow_id: Arc<String>,
|
2020-07-23 15:25:57 +00:00
|
|
|
/// The SAS state we're in.
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc<S>,
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
impl<S: Clone + std::fmt::Debug> std::fmt::Debug for SasState<S> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("SasState")
|
|
|
|
.field("ids", &self.ids)
|
|
|
|
.field("flow_id", &self.verification_flow_id)
|
|
|
|
.field("state", &self.state)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// The initial SAS state.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct Created {
|
|
|
|
protocol_definitions: MSasV1ContentOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The initial SAS state if the other side started the SAS verification.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct Started {
|
2020-07-27 11:16:56 +00:00
|
|
|
commitment: String,
|
2020-07-23 15:25:57 +00:00
|
|
|
protocol_definitions: MSasV1Content,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The SAS state we're going to be in after the other side accepted our
|
|
|
|
/// verification start event.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct Accepted {
|
2020-07-24 09:26:45 +00:00
|
|
|
accepted_protocols: Arc<AcceptedProtocols>,
|
2020-07-27 11:18:00 +00:00
|
|
|
json_start_content: String,
|
2020-07-23 15:25:57 +00:00
|
|
|
commitment: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The SAS state we're going to be in after we received the public key of the
|
|
|
|
/// other participant.
|
|
|
|
///
|
|
|
|
/// From now on we can show the short auth string to the user.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct KeyReceived {
|
|
|
|
we_started: bool,
|
2020-07-24 09:26:45 +00:00
|
|
|
accepted_protocols: Arc<AcceptedProtocols>,
|
2020-07-23 15:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The SAS state we're going to be in after the user has confirmed that the
|
|
|
|
/// short auth string matches. We still need to receive a MAC event from the
|
|
|
|
/// other side.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct Confirmed {
|
2020-07-24 09:26:45 +00:00
|
|
|
accepted_protocols: Arc<AcceptedProtocols>,
|
2020-07-23 15:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The SAS state we're going to be in after we receive a MAC event from the
|
|
|
|
/// other side. Our own user still needs to confirm that the short auth string
|
|
|
|
/// matches.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct MacReceived {
|
|
|
|
we_started: bool,
|
2020-07-24 09:26:45 +00:00
|
|
|
verified_devices: Arc<Vec<Box<DeviceId>>>,
|
|
|
|
verified_master_keys: Arc<Vec<String>>,
|
2020-07-23 15:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The SAS state indicating that the verification finished successfully.
|
|
|
|
///
|
|
|
|
/// We can now mark the device in our verified devices lits as verified and sign
|
|
|
|
/// the master keys in the verified devices list.
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-07-23 15:25:57 +00:00
|
|
|
struct Done {
|
2020-07-24 09:26:45 +00:00
|
|
|
verified_devices: Arc<Vec<Box<DeviceId>>>,
|
|
|
|
verified_master_keys: Arc<Vec<String>>,
|
2020-07-23 15:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct Canceled {
|
|
|
|
cancel_code: CancelCode,
|
|
|
|
reason: &'static str,
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:49:00 +00:00
|
|
|
impl<S: Clone> SasState<S> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get our own user id.
|
2020-07-27 13:28:14 +00:00
|
|
|
fn user_id(&self) -> &UserId {
|
2020-07-23 09:19:19 +00:00
|
|
|
&self.ids.account.user_id()
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get our own device id.
|
2020-07-27 13:28:14 +00:00
|
|
|
fn device_id(&self) -> &DeviceId {
|
2020-07-23 11:41:57 +00:00
|
|
|
&self.ids.account.device_id()
|
|
|
|
}
|
2020-07-25 08:24:44 +00:00
|
|
|
|
2020-07-27 13:28:14 +00:00
|
|
|
fn cancel(self, cancel_code: CancelCode) -> SasState<Canceled> {
|
2020-07-25 08:24:44 +00:00
|
|
|
SasState {
|
|
|
|
inner: self.inner,
|
|
|
|
ids: self.ids,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
state: Arc::new(Canceled::new(cancel_code)),
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 13:28:14 +00:00
|
|
|
|
|
|
|
fn check_sender_and_txid(&self, sender: &UserId, flow_id: &str) -> Result<(), CancelCode> {
|
|
|
|
if flow_id != &*self.verification_flow_id {
|
|
|
|
Err(CancelCode::UnknownTransaction)
|
|
|
|
} else if sender != self.ids.other_device.user_id() {
|
|
|
|
Err(CancelCode::UserMismatch)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<Created> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Create a new SAS verification flow.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `account` - Our own account.
|
|
|
|
///
|
|
|
|
/// * `other_device` - The other device which we are going to verify.
|
2020-07-24 09:32:38 +00:00
|
|
|
fn new(account: Account, other_device: Device) -> SasState<Created> {
|
2020-07-22 13:11:34 +00:00
|
|
|
let verification_flow_id = Uuid::new_v4().to_string();
|
2020-07-23 09:19:19 +00:00
|
|
|
let from_device: Box<DeviceId> = account.device_id().into();
|
2020-07-22 11:43:11 +00:00
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
SasState {
|
2020-07-24 09:26:45 +00:00
|
|
|
inner: Arc::new(Mutex::new(OlmSas::new())),
|
2020-07-14 15:04:08 +00:00
|
|
|
ids: SasIds {
|
2020-07-23 09:19:19 +00:00
|
|
|
account,
|
2020-07-14 15:04:08 +00:00
|
|
|
other_device,
|
|
|
|
},
|
2020-07-24 09:26:45 +00:00
|
|
|
verification_flow_id: Arc::new(verification_flow_id.clone()),
|
2020-07-22 11:43:11 +00:00
|
|
|
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(Created {
|
2020-07-22 11:43:11 +00:00
|
|
|
protocol_definitions: MSasV1ContentOptions {
|
2020-07-22 13:11:34 +00:00
|
|
|
transaction_id: verification_flow_id,
|
2020-07-23 09:19:19 +00:00
|
|
|
from_device,
|
2020-07-22 11:43:11 +00:00
|
|
|
short_authentication_string: vec![
|
|
|
|
ShortAuthenticationString::Decimal,
|
|
|
|
ShortAuthenticationString::Emoji,
|
|
|
|
],
|
|
|
|
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519HkdfSha256],
|
|
|
|
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
|
|
|
|
hashes: vec![HashAlgorithm::Sha256],
|
|
|
|
},
|
2020-07-24 09:26:45 +00:00
|
|
|
}),
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the start event.
|
|
|
|
///
|
|
|
|
/// The content needs to be sent to the other device.
|
2020-07-27 11:16:56 +00:00
|
|
|
fn as_content(&self) -> StartEventContent {
|
2020-07-22 11:43:11 +00:00
|
|
|
StartEventContent::MSasV1(
|
|
|
|
MSasV1Content::new(self.state.protocol_definitions.clone())
|
|
|
|
.expect("Invalid initial protocol definitions."),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Receive a m.key.verification.accept event, changing the state into
|
|
|
|
/// an Accepted one.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.accept event that was sent to us by
|
|
|
|
/// the other side.
|
2020-07-27 11:18:00 +00:00
|
|
|
fn into_accepted(
|
|
|
|
self,
|
|
|
|
event: &ToDeviceEvent<AcceptEventContent>,
|
|
|
|
) -> Result<SasState<Accepted>, SasState<Canceled>> {
|
2020-07-27 13:28:14 +00:00
|
|
|
self.check_sender_and_txid(&event.sender, &event.content.transaction_id)
|
|
|
|
.map_err(|c| self.clone().cancel(c))?;
|
2020-07-14 15:04:08 +00:00
|
|
|
|
2020-07-27 13:28:14 +00:00
|
|
|
let content = &event.content;
|
2020-07-27 11:18:00 +00:00
|
|
|
if !Sas::KEY_AGREEMENT_PROTOCOLS.contains(&event.content.key_agreement_protocol)
|
|
|
|
|| !Sas::HASHES.contains(&event.content.hash)
|
|
|
|
|| !Sas::MACS.contains(&event.content.message_authentication_code)
|
|
|
|
|| (!event
|
|
|
|
.content
|
|
|
|
.short_authentication_string
|
|
|
|
.contains(&ShortAuthenticationString::Emoji)
|
|
|
|
&& !event
|
|
|
|
.content
|
|
|
|
.short_authentication_string
|
|
|
|
.contains(&ShortAuthenticationString::Decimal))
|
|
|
|
{
|
|
|
|
Err(self.cancel(CancelCode::UnknownMethod))
|
|
|
|
} else {
|
|
|
|
let json_start_content = cjson::to_string(&self.as_content())
|
|
|
|
.expect("Can't deserialize start event content");
|
|
|
|
|
|
|
|
Ok(SasState {
|
|
|
|
inner: self.inner,
|
|
|
|
ids: self.ids,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
state: Arc::new(Accepted {
|
|
|
|
json_start_content,
|
|
|
|
commitment: content.commitment.clone(),
|
|
|
|
accepted_protocols: Arc::new(content.clone().into()),
|
|
|
|
}),
|
|
|
|
})
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<Started> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Create a new SAS verification flow from a m.key.verification.start
|
|
|
|
/// event.
|
|
|
|
///
|
|
|
|
/// This will put us in the `started` state.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `account` - Our own account.
|
|
|
|
///
|
|
|
|
/// * `other_device` - The other device which we are going to verify.
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.start event that was sent to us by
|
|
|
|
/// the other side.
|
2020-07-14 15:04:08 +00:00
|
|
|
fn from_start_event(
|
2020-07-23 09:19:19 +00:00
|
|
|
account: Account,
|
2020-07-14 15:04:08 +00:00
|
|
|
other_device: Device,
|
2020-07-22 11:43:11 +00:00
|
|
|
event: &ToDeviceEvent<StartEventContent>,
|
2020-07-24 15:51:20 +00:00
|
|
|
) -> Result<SasState<Started>, SasState<Canceled>> {
|
|
|
|
if let StartEventContent::MSasV1(content) = &event.content {
|
2020-07-27 11:16:56 +00:00
|
|
|
let sas = OlmSas::new();
|
|
|
|
let utility = OlmUtility::new();
|
|
|
|
|
|
|
|
let json_content = cjson::to_string(&event.content).expect("Can't serialize content");
|
|
|
|
let pubkey = sas.public_key();
|
|
|
|
let commitment = utility.sha256_utf8_msg(&format!("{}{}", pubkey, json_content));
|
|
|
|
|
2020-07-25 08:24:44 +00:00
|
|
|
let sas = SasState {
|
2020-07-27 11:16:56 +00:00
|
|
|
inner: Arc::new(Mutex::new(sas)),
|
2020-07-25 08:24:44 +00:00
|
|
|
|
|
|
|
ids: SasIds {
|
|
|
|
account,
|
|
|
|
other_device,
|
|
|
|
},
|
|
|
|
|
|
|
|
verification_flow_id: Arc::new(content.transaction_id.clone()),
|
|
|
|
|
|
|
|
state: Arc::new(Started {
|
|
|
|
protocol_definitions: content.clone(),
|
2020-07-27 11:16:56 +00:00
|
|
|
commitment,
|
2020-07-25 08:24:44 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
if !content
|
|
|
|
.key_agreement_protocols
|
|
|
|
.contains(&KeyAgreementProtocol::Curve25519HkdfSha256)
|
|
|
|
|| !content
|
|
|
|
.message_authentication_codes
|
|
|
|
.contains(&MessageAuthenticationCode::HkdfHmacSha256)
|
|
|
|
|| !content.hashes.contains(&HashAlgorithm::Sha256)
|
|
|
|
|| (!content
|
|
|
|
.short_authentication_string
|
|
|
|
.contains(&ShortAuthenticationString::Decimal)
|
|
|
|
&& !content
|
|
|
|
.short_authentication_string
|
|
|
|
.contains(&ShortAuthenticationString::Emoji))
|
|
|
|
{
|
2020-07-25 08:24:44 +00:00
|
|
|
Err(sas.cancel(CancelCode::UnknownMethod))
|
2020-07-24 15:51:20 +00:00
|
|
|
} else {
|
2020-07-25 08:24:44 +00:00
|
|
|
Ok(sas)
|
2020-07-24 15:51:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(SasState {
|
|
|
|
inner: Arc::new(Mutex::new(OlmSas::new())),
|
2020-07-14 15:04:08 +00:00
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
ids: SasIds {
|
|
|
|
account,
|
|
|
|
other_device,
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO we can't get to the transaction id currently since it's
|
2020-07-25 08:59:20 +00:00
|
|
|
// behind the content specific enum.
|
2020-07-24 15:51:20 +00:00
|
|
|
verification_flow_id: Arc::new("".to_owned()),
|
|
|
|
|
|
|
|
state: Arc::new(Canceled::new(CancelCode::UnknownMethod)),
|
|
|
|
})
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the accept event.
|
|
|
|
///
|
|
|
|
/// The content needs to be sent to the other device.
|
|
|
|
///
|
|
|
|
/// This should be sent out automatically if the SAS verification flow has
|
|
|
|
/// been started because of a
|
|
|
|
/// m.key.verification.request -> m.key.verification.ready flow.
|
2020-07-27 11:16:56 +00:00
|
|
|
fn as_content(&self) -> AcceptEventContent {
|
2020-07-22 13:11:34 +00:00
|
|
|
AcceptEventContent {
|
|
|
|
method: VerificationMethod::MSasV1,
|
|
|
|
transaction_id: self.verification_flow_id.to_string(),
|
2020-07-27 11:16:56 +00:00
|
|
|
commitment: self.state.commitment.clone(),
|
2020-07-22 13:11:34 +00:00
|
|
|
hash: HashAlgorithm::Sha256,
|
|
|
|
key_agreement_protocol: KeyAgreementProtocol::Curve25519HkdfSha256,
|
|
|
|
message_authentication_code: MessageAuthenticationCode::HkdfHmacSha256,
|
|
|
|
short_authentication_string: self
|
|
|
|
.state
|
|
|
|
.protocol_definitions
|
|
|
|
.short_authentication_string
|
|
|
|
.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Receive a m.key.verification.key event, changing the state into
|
|
|
|
/// a `KeyReceived` one
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.key event that was sent to us by
|
|
|
|
/// the other side. The event will be modified so it doesn't contain any key
|
|
|
|
/// anymore.
|
2020-07-24 09:32:38 +00:00
|
|
|
fn into_key_received(
|
|
|
|
self,
|
|
|
|
event: &mut ToDeviceEvent<KeyEventContent>,
|
2020-07-27 13:28:14 +00:00
|
|
|
) -> Result<SasState<KeyReceived>, SasState<Canceled>> {
|
|
|
|
self.check_sender_and_txid(&event.sender, &event.content.transaction_id)
|
|
|
|
.map_err(|c| self.clone().cancel(c))?;
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let accepted_protocols: AcceptedProtocols = self.as_content().into();
|
2020-07-22 13:11:34 +00:00
|
|
|
self.inner
|
2020-07-24 09:26:45 +00:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2020-07-22 13:11:34 +00:00
|
|
|
.set_their_public_key(&mem::take(&mut event.content.key))
|
|
|
|
.expect("Can't set public key");
|
|
|
|
|
2020-07-27 13:28:14 +00:00
|
|
|
Ok(SasState {
|
2020-07-22 13:11:34 +00:00
|
|
|
inner: self.inner,
|
|
|
|
ids: self.ids,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(KeyReceived {
|
2020-07-22 13:11:34 +00:00
|
|
|
we_started: false,
|
2020-07-24 09:26:45 +00:00
|
|
|
accepted_protocols: Arc::new(accepted_protocols),
|
|
|
|
}),
|
2020-07-27 13:28:14 +00:00
|
|
|
})
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
2020-07-14 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<Accepted> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Receive a m.key.verification.key event, changing the state into
|
|
|
|
/// a `KeyReceived` one
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.key event that was sent to us by
|
|
|
|
/// the other side. The event will be modified so it doesn't contain any key
|
|
|
|
/// anymore.
|
2020-07-24 09:32:38 +00:00
|
|
|
fn into_key_received(
|
|
|
|
self,
|
|
|
|
event: &mut ToDeviceEvent<KeyEventContent>,
|
2020-07-27 11:18:00 +00:00
|
|
|
) -> Result<SasState<KeyReceived>, SasState<Canceled>> {
|
2020-07-27 13:56:28 +00:00
|
|
|
self.check_sender_and_txid(&event.sender, &event.content.transaction_id)
|
|
|
|
.map_err(|c| self.clone().cancel(c))?;
|
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let utility = OlmUtility::new();
|
|
|
|
let commitment = utility.sha256_utf8_msg(&format!(
|
|
|
|
"{}{}",
|
|
|
|
event.content.key, self.state.json_start_content
|
|
|
|
));
|
|
|
|
|
|
|
|
if self.state.commitment != commitment {
|
|
|
|
Err(self.cancel(CancelCode::InvalidMessage))
|
|
|
|
} else {
|
|
|
|
self.inner
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.set_their_public_key(&mem::take(&mut event.content.key))
|
|
|
|
.expect("Can't set public key");
|
|
|
|
|
|
|
|
Ok(SasState {
|
|
|
|
inner: self.inner,
|
|
|
|
ids: self.ids,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
state: Arc::new(KeyReceived {
|
|
|
|
we_started: true,
|
|
|
|
accepted_protocols: self.state.accepted_protocols.clone(),
|
|
|
|
}),
|
|
|
|
})
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the key event.
|
|
|
|
///
|
|
|
|
/// The content needs to be automatically sent to the other side.
|
2020-07-27 11:18:00 +00:00
|
|
|
fn as_content(&self) -> KeyEventContent {
|
2020-07-22 13:11:34 +00:00
|
|
|
KeyEventContent {
|
|
|
|
transaction_id: self.verification_flow_id.to_string(),
|
2020-07-24 09:26:45 +00:00
|
|
|
key: self.inner.lock().unwrap().public_key(),
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<KeyReceived> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the key event.
|
|
|
|
///
|
|
|
|
/// The content needs to be automatically sent to the other side if and only
|
|
|
|
/// if we_started is false.
|
2020-07-27 11:18:00 +00:00
|
|
|
fn as_content(&self) -> KeyEventContent {
|
2020-07-22 13:11:34 +00:00
|
|
|
KeyEventContent {
|
|
|
|
transaction_id: self.verification_flow_id.to_string(),
|
2020-07-24 09:26:45 +00:00
|
|
|
key: self.inner.lock().unwrap().public_key(),
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the emoji version of the short authentication string.
|
|
|
|
///
|
|
|
|
/// Returns a vector of tuples where the first element is the emoji and the
|
|
|
|
/// second element the English description of the emoji.
|
2020-07-22 14:41:16 +00:00
|
|
|
fn get_emoji(&self) -> Vec<(&'static str, &'static str)> {
|
2020-07-23 12:26:50 +00:00
|
|
|
get_emoji(
|
2020-07-24 09:26:45 +00:00
|
|
|
&self.inner.lock().unwrap(),
|
2020-07-23 12:26:50 +00:00
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
self.state.we_started,
|
|
|
|
)
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the decimal version of the short authentication string.
|
|
|
|
///
|
|
|
|
/// Returns a tuple containing three 4 digit integer numbers that represent
|
|
|
|
/// the short auth string.
|
2020-07-22 14:41:16 +00:00
|
|
|
fn get_decimal(&self) -> (u32, u32, u32) {
|
2020-07-23 12:26:50 +00:00
|
|
|
get_decimal(
|
2020-07-24 09:26:45 +00:00
|
|
|
&self.inner.lock().unwrap(),
|
2020-07-23 12:26:50 +00:00
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
self.state.we_started,
|
|
|
|
)
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Receive a m.key.verification.mac event, changing the state into
|
|
|
|
/// a `MacReceived` one
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.mac event that was sent to us by
|
|
|
|
/// the other side.
|
2020-07-27 13:34:18 +00:00
|
|
|
fn into_mac_received(
|
|
|
|
self,
|
|
|
|
event: &ToDeviceEvent<MacEventContent>,
|
|
|
|
) -> Result<SasState<MacReceived>, SasState<Canceled>> {
|
|
|
|
self.check_sender_and_txid(&event.sender, &event.content.transaction_id)
|
|
|
|
.map_err(|c| self.clone().cancel(c))?;
|
|
|
|
|
2020-07-24 09:26:45 +00:00
|
|
|
let (devices, master_keys) = receive_mac_event(
|
|
|
|
&self.inner.lock().unwrap(),
|
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
event,
|
|
|
|
);
|
2020-07-27 13:34:18 +00:00
|
|
|
|
|
|
|
Ok(SasState {
|
2020-07-23 11:41:57 +00:00
|
|
|
inner: self.inner,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
ids: self.ids,
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(MacReceived {
|
2020-07-23 12:35:29 +00:00
|
|
|
we_started: self.state.we_started,
|
2020-07-24 09:26:45 +00:00
|
|
|
verified_devices: Arc::new(devices),
|
|
|
|
verified_master_keys: Arc::new(master_keys),
|
|
|
|
}),
|
2020-07-27 13:34:18 +00:00
|
|
|
})
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Confirm that the short auth string matches.
|
|
|
|
///
|
|
|
|
/// This needs to be done by the user, this will put us in the `Confirmed`
|
|
|
|
/// state.
|
2020-07-24 09:32:38 +00:00
|
|
|
fn confirm(self) -> SasState<Confirmed> {
|
|
|
|
SasState {
|
2020-07-23 11:41:57 +00:00
|
|
|
inner: self.inner,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
ids: self.ids,
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(Confirmed {
|
|
|
|
accepted_protocols: self.state.accepted_protocols.clone(),
|
|
|
|
}),
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<Confirmed> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Receive a m.key.verification.mac event, changing the state into
|
|
|
|
/// a `Done` one
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The m.key.verification.mac event that was sent to us by
|
|
|
|
/// the other side.
|
2020-07-27 13:34:18 +00:00
|
|
|
fn into_done(
|
|
|
|
self,
|
|
|
|
event: &ToDeviceEvent<MacEventContent>,
|
|
|
|
) -> Result<SasState<Done>, SasState<Canceled>> {
|
|
|
|
self.check_sender_and_txid(&event.sender, &event.content.transaction_id)
|
|
|
|
.map_err(|c| self.clone().cancel(c))?;
|
2020-07-24 09:26:45 +00:00
|
|
|
let (devices, master_keys) = receive_mac_event(
|
|
|
|
&self.inner.lock().unwrap(),
|
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
event,
|
|
|
|
);
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 13:34:18 +00:00
|
|
|
Ok(SasState {
|
2020-07-23 11:41:57 +00:00
|
|
|
inner: self.inner,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
ids: self.ids,
|
|
|
|
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(Done {
|
|
|
|
verified_devices: Arc::new(devices),
|
|
|
|
verified_master_keys: Arc::new(master_keys),
|
|
|
|
}),
|
2020-07-27 13:34:18 +00:00
|
|
|
})
|
2020-07-23 09:08:09 +00:00
|
|
|
}
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the mac event.
|
|
|
|
///
|
|
|
|
/// The content needs to be automatically sent to the other side.
|
2020-07-27 11:18:00 +00:00
|
|
|
fn as_content(&self) -> MacEventContent {
|
2020-07-24 09:26:45 +00:00
|
|
|
get_mac_content(
|
|
|
|
&self.inner.lock().unwrap(),
|
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
)
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<MacReceived> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Confirm that the short auth string matches.
|
|
|
|
///
|
|
|
|
/// This needs to be done by the user, this will put us in the `Done`
|
|
|
|
/// state since the other side already confirmed and sent us a MAC event.
|
2020-07-24 09:32:38 +00:00
|
|
|
fn confirm(self) -> SasState<Done> {
|
|
|
|
SasState {
|
2020-07-23 11:41:57 +00:00
|
|
|
inner: self.inner,
|
|
|
|
verification_flow_id: self.verification_flow_id,
|
|
|
|
ids: self.ids,
|
2020-07-24 09:26:45 +00:00
|
|
|
state: Arc::new(Done {
|
|
|
|
verified_devices: self.state.verified_devices.clone(),
|
|
|
|
verified_master_keys: self.state.verified_master_keys.clone(),
|
|
|
|
}),
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
2020-07-23 12:35:29 +00:00
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the emoji version of the short authentication string.
|
|
|
|
///
|
|
|
|
/// Returns a vector of tuples where the first element is the emoji and the
|
|
|
|
/// second element the English description of the emoji.
|
2020-07-23 12:35:29 +00:00
|
|
|
fn get_emoji(&self) -> Vec<(&'static str, &'static str)> {
|
|
|
|
get_emoji(
|
2020-07-24 09:26:45 +00:00
|
|
|
&self.inner.lock().unwrap(),
|
2020-07-23 12:35:29 +00:00
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
self.state.we_started,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the decimal version of the short authentication string.
|
|
|
|
///
|
|
|
|
/// Returns a tuple containing three 4 digit integer numbers that represent
|
|
|
|
/// the short auth string.
|
2020-07-23 12:35:29 +00:00
|
|
|
fn get_decimal(&self) -> (u32, u32, u32) {
|
|
|
|
get_decimal(
|
2020-07-24 09:26:45 +00:00
|
|
|
&self.inner.lock().unwrap(),
|
2020-07-23 12:35:29 +00:00
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
self.state.we_started,
|
|
|
|
)
|
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
impl SasState<Done> {
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the content for the mac event.
|
|
|
|
///
|
|
|
|
/// The content needs to be automatically sent to the other side if it
|
|
|
|
/// wasn't already sent.
|
2020-07-27 11:18:00 +00:00
|
|
|
fn as_content(&self) -> MacEventContent {
|
2020-07-24 09:26:45 +00:00
|
|
|
get_mac_content(
|
|
|
|
&self.inner.lock().unwrap(),
|
|
|
|
&self.ids,
|
|
|
|
&self.verification_flow_id,
|
|
|
|
)
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the list of verified devices.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn verified_devices(&self) -> Arc<Vec<Box<DeviceId>>> {
|
|
|
|
self.state.verified_devices.clone()
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:25:57 +00:00
|
|
|
/// Get the list of verified master keys.
|
2020-07-24 13:49:00 +00:00
|
|
|
fn verified_master_keys(&self) -> Arc<Vec<String>> {
|
|
|
|
self.state.verified_master_keys.clone()
|
2020-07-23 11:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
impl Canceled {
|
|
|
|
fn new(code: CancelCode) -> Canceled {
|
|
|
|
let reason = match code {
|
|
|
|
CancelCode::Accepted => {
|
|
|
|
"A m.key.verification.request was accepted by a different device."
|
|
|
|
}
|
|
|
|
CancelCode::InvalidMessage => "The received message was invalid.",
|
|
|
|
CancelCode::KeyMismatch => "The expected key did not match the verified one",
|
|
|
|
CancelCode::Timeout => "The verification process timed out.",
|
|
|
|
CancelCode::UnexpectedMessage => "The device received an unexpected message.",
|
|
|
|
CancelCode::UnknownMethod => {
|
|
|
|
"The device does not know how to handle the requested method."
|
|
|
|
}
|
|
|
|
CancelCode::UnknownTransaction => {
|
|
|
|
"The device does not know about the given transaction ID."
|
|
|
|
}
|
|
|
|
CancelCode::User => "The user cancelled the verification.",
|
|
|
|
CancelCode::UserMismatch => "The expected user did not match the verified user",
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Canceled {
|
|
|
|
cancel_code: code,
|
|
|
|
reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
impl SasState<Canceled> {
|
2020-07-27 13:56:28 +00:00
|
|
|
fn as_content(&self) -> AnyToDeviceEventContent {
|
|
|
|
AnyToDeviceEventContent::KeyVerificationCancel(CancelEventContent {
|
2020-07-27 11:16:56 +00:00
|
|
|
transaction_id: self.verification_flow_id.to_string(),
|
|
|
|
reason: self.state.reason.to_string(),
|
|
|
|
code: self.state.cancel_code.clone(),
|
2020-07-27 13:56:28 +00:00
|
|
|
})
|
2020-07-27 11:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 11:43:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use crate::{Account, Device};
|
2020-07-24 13:49:00 +00:00
|
|
|
use matrix_sdk_common::events::{
|
|
|
|
AnyToDeviceEvent, AnyToDeviceEventContent, EventContent, ToDeviceEvent,
|
|
|
|
};
|
2020-07-22 11:43:11 +00:00
|
|
|
use matrix_sdk_common::identifiers::{DeviceId, UserId};
|
|
|
|
|
2020-07-24 13:49:00 +00:00
|
|
|
use super::{Accepted, Created, Sas, SasState, Started};
|
2020-07-22 11:43:11 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:11:34 +00:00
|
|
|
fn wrap_to_device_event<C: EventContent>(sender: &UserId, content: C) -> ToDeviceEvent<C> {
|
2020-07-22 11:43:11 +00:00
|
|
|
ToDeviceEvent {
|
|
|
|
sender: sender.clone(),
|
|
|
|
content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:49:00 +00:00
|
|
|
fn wrap_any_to_device_content(
|
|
|
|
sender: &UserId,
|
|
|
|
content: AnyToDeviceEventContent,
|
|
|
|
) -> AnyToDeviceEvent {
|
|
|
|
match content {
|
|
|
|
AnyToDeviceEventContent::KeyVerificationKey(c) => {
|
|
|
|
AnyToDeviceEvent::KeyVerificationKey(ToDeviceEvent {
|
|
|
|
sender: sender.clone(),
|
|
|
|
content: c,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
async fn get_sas_pair() -> (SasState<Created>, SasState<Started>) {
|
2020-07-22 11:43:11 +00:00
|
|
|
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;
|
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
let alice_sas = SasState::<Created>::new(alice.clone(), bob_device);
|
2020-07-22 11:43:11 +00:00
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let start_content = alice_sas.as_content();
|
2020-07-22 13:11:34 +00:00
|
|
|
let event = wrap_to_device_event(alice_sas.user_id(), start_content);
|
2020-07-22 11:43:11 +00:00
|
|
|
|
2020-07-24 09:32:38 +00:00
|
|
|
let bob_sas = SasState::<Started>::from_start_event(bob.clone(), alice_device, &event);
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-24 15:51:20 +00:00
|
|
|
(alice_sas, bob_sas.unwrap())
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn create_sas() {
|
|
|
|
let (_, _) = get_sas_pair().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn sas_accept() {
|
|
|
|
let (alice, bob) = get_sas_pair().await;
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
alice.into_accepted(&event).unwrap();
|
2020-07-22 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn sas_key_share() {
|
|
|
|
let (alice, bob) = get_sas_pair().await;
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let alice: SasState<Accepted> = alice.into_accepted(&event).unwrap();
|
|
|
|
let mut event = wrap_to_device_event(alice.user_id(), alice.as_content());
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-27 13:28:14 +00:00
|
|
|
let bob = bob.into_key_received(&mut event).unwrap();
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let mut event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-22 13:11:34 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let alice = alice.into_key_received(&mut event).unwrap();
|
2020-07-22 13:11:34 +00:00
|
|
|
|
|
|
|
assert_eq!(alice.get_decimal(), bob.get_decimal());
|
2020-07-22 14:41:16 +00:00
|
|
|
assert_eq!(alice.get_emoji(), bob.get_emoji());
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|
2020-07-23 11:41:57 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2020-07-23 15:25:57 +00:00
|
|
|
async fn sas_full() {
|
2020-07-23 11:41:57 +00:00
|
|
|
let (alice, bob) = get_sas_pair().await;
|
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let alice: SasState<Accepted> = alice.into_accepted(&event).unwrap();
|
|
|
|
let mut event = wrap_to_device_event(alice.user_id(), alice.as_content());
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 13:28:14 +00:00
|
|
|
let bob = bob.into_key_received(&mut event).unwrap();
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let mut event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let alice = alice.into_key_received(&mut event).unwrap();
|
2020-07-23 11:41:57 +00:00
|
|
|
|
|
|
|
assert_eq!(alice.get_decimal(), bob.get_decimal());
|
|
|
|
assert_eq!(alice.get_emoji(), bob.get_emoji());
|
|
|
|
|
|
|
|
let bob = bob.confirm();
|
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.as_content());
|
2020-07-23 11:41:57 +00:00
|
|
|
|
2020-07-27 13:34:18 +00:00
|
|
|
let alice = alice.into_mac_received(&event).unwrap();
|
2020-07-23 12:35:29 +00:00
|
|
|
assert!(!alice.get_emoji().is_empty());
|
2020-07-23 11:41:57 +00:00
|
|
|
let alice = alice.confirm();
|
|
|
|
|
2020-07-27 11:18:00 +00:00
|
|
|
let event = wrap_to_device_event(alice.user_id(), alice.as_content());
|
2020-07-27 13:34:18 +00:00
|
|
|
let bob = bob.into_done(&event).unwrap();
|
2020-07-23 11:41:57 +00:00
|
|
|
|
|
|
|
assert!(bob.verified_devices().contains(&alice.device_id().into()));
|
|
|
|
assert!(alice.verified_devices().contains(&bob.device_id().into()));
|
|
|
|
}
|
2020-07-24 13:49:00 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn sas_wrapper_full() {
|
|
|
|
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, content) = Sas::start(alice, bob_device);
|
2020-07-24 14:04:47 +00:00
|
|
|
let event = wrap_to_device_event(alice.user_id(), content);
|
2020-07-24 13:49:00 +00:00
|
|
|
|
2020-07-27 11:16:56 +00:00
|
|
|
let bob = Sas::from_start_event(bob, alice_device, &event).unwrap();
|
2020-07-24 13:49:00 +00:00
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.accept().unwrap());
|
|
|
|
|
|
|
|
let content = alice.receive_event(&mut AnyToDeviceEvent::KeyVerificationAccept(event));
|
|
|
|
|
|
|
|
assert!(!alice.can_be_presented());
|
|
|
|
assert!(!bob.can_be_presented());
|
|
|
|
|
|
|
|
let mut event = wrap_any_to_device_content(alice.user_id(), content.unwrap());
|
|
|
|
let mut event =
|
|
|
|
wrap_any_to_device_content(bob.user_id(), bob.receive_event(&mut event).unwrap());
|
|
|
|
|
|
|
|
assert!(bob.can_be_presented());
|
|
|
|
|
|
|
|
alice.receive_event(&mut event);
|
|
|
|
assert!(alice.can_be_presented());
|
|
|
|
|
|
|
|
assert_eq!(alice.emoji().unwrap(), bob.emoji().unwrap());
|
|
|
|
assert_eq!(alice.decimals().unwrap(), bob.decimals().unwrap());
|
|
|
|
|
|
|
|
let event = wrap_to_device_event(alice.user_id(), alice.confirm().unwrap());
|
|
|
|
bob.receive_event(&mut AnyToDeviceEvent::KeyVerificationMac(event));
|
|
|
|
|
|
|
|
let event = wrap_to_device_event(bob.user_id(), bob.confirm().unwrap());
|
|
|
|
alice.receive_event(&mut AnyToDeviceEvent::KeyVerificationMac(event));
|
|
|
|
|
|
|
|
assert!(alice
|
|
|
|
.verified_devices()
|
|
|
|
.unwrap()
|
|
|
|
.contains(&bob.device_id().into()));
|
|
|
|
assert!(bob
|
|
|
|
.verified_devices()
|
|
|
|
.unwrap()
|
|
|
|
.contains(&alice.device_id().into()));
|
|
|
|
}
|
2020-07-22 11:43:11 +00:00
|
|
|
}
|