2020-04-02 09:14:23 +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-04-23 08:52:47 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-04-27 14:31:28 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-04-17 12:14:31 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2020-04-02 12:52:15 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use atomic::Atomic;
|
2020-07-14 09:17:09 +00:00
|
|
|
use serde_json::{json, Value};
|
2020-04-02 09:14:23 +00:00
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
use super::OlmMachine;
|
2020-07-14 09:27:50 +00:00
|
|
|
use matrix_sdk_common::api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm};
|
2020-05-07 06:51:59 +00:00
|
|
|
use matrix_sdk_common::events::Algorithm;
|
|
|
|
use matrix_sdk_common::identifiers::{DeviceId, UserId};
|
2020-04-02 09:14:23 +00:00
|
|
|
|
2020-07-14 09:17:09 +00:00
|
|
|
use crate::error::SignatureError;
|
2020-07-14 10:23:42 +00:00
|
|
|
use crate::verify_json;
|
2020-07-14 09:17:09 +00:00
|
|
|
|
2020-04-21 09:03:28 +00:00
|
|
|
/// A device represents a E2EE capable client of an user.
|
2020-04-02 12:52:15 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-02 09:14:23 +00:00
|
|
|
pub struct Device {
|
2020-04-09 14:22:25 +00:00
|
|
|
user_id: Arc<UserId>,
|
|
|
|
device_id: Arc<DeviceId>,
|
2020-04-02 12:52:15 +00:00
|
|
|
algorithms: Arc<Vec<Algorithm>>,
|
2020-07-14 10:00:29 +00:00
|
|
|
keys: Arc<BTreeMap<AlgorithmAndDeviceId, String>>,
|
2020-04-02 12:52:15 +00:00
|
|
|
display_name: Arc<Option<String>>,
|
|
|
|
deleted: Arc<AtomicBool>,
|
|
|
|
trust_state: Arc<Atomic<TrustState>>,
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 12:14:31 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
/// The trust state of a device.
|
2020-04-02 09:14:23 +00:00
|
|
|
pub enum TrustState {
|
2020-04-17 12:14:31 +00:00
|
|
|
/// The device has been verified and is trusted.
|
|
|
|
Verified = 0,
|
|
|
|
/// The device been blacklisted from communicating.
|
|
|
|
BlackListed = 1,
|
|
|
|
/// The trust state of the device is being ignored.
|
|
|
|
Ignored = 2,
|
|
|
|
/// The trust state is unset.
|
|
|
|
Unset = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i64> for TrustState {
|
|
|
|
fn from(state: i64) -> Self {
|
|
|
|
match state {
|
|
|
|
0 => TrustState::Verified,
|
|
|
|
1 => TrustState::BlackListed,
|
|
|
|
2 => TrustState::Ignored,
|
|
|
|
3 => TrustState::Unset,
|
|
|
|
_ => TrustState::Unset,
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Device {
|
2020-04-17 12:14:31 +00:00
|
|
|
/// Create a new Device.
|
|
|
|
pub fn new(
|
|
|
|
user_id: UserId,
|
|
|
|
device_id: DeviceId,
|
|
|
|
display_name: Option<String>,
|
|
|
|
trust_state: TrustState,
|
|
|
|
algorithms: Vec<Algorithm>,
|
2020-07-14 10:00:29 +00:00
|
|
|
keys: BTreeMap<AlgorithmAndDeviceId, String>,
|
2020-04-17 12:14:31 +00:00
|
|
|
) -> Self {
|
|
|
|
Device {
|
|
|
|
user_id: Arc::new(user_id),
|
|
|
|
device_id: Arc::new(device_id),
|
|
|
|
display_name: Arc::new(display_name),
|
|
|
|
trust_state: Arc::new(Atomic::new(trust_state)),
|
|
|
|
algorithms: Arc::new(algorithms),
|
|
|
|
keys: Arc::new(keys),
|
|
|
|
deleted: Arc::new(AtomicBool::new(false)),
|
|
|
|
}
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 12:14:31 +00:00
|
|
|
/// The user id of the device owner.
|
2020-04-09 14:22:25 +00:00
|
|
|
pub fn user_id(&self) -> &UserId {
|
2020-04-02 09:14:23 +00:00
|
|
|
&self.user_id
|
|
|
|
}
|
2020-04-15 16:55:15 +00:00
|
|
|
|
2020-04-17 12:14:31 +00:00
|
|
|
/// The unique ID of the device.
|
|
|
|
pub fn device_id(&self) -> &DeviceId {
|
|
|
|
&self.device_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the human readable name of the device.
|
|
|
|
pub fn display_name(&self) -> &Option<String> {
|
|
|
|
&self.display_name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the key of the given key algorithm belonging to this device.
|
2020-04-30 11:16:10 +00:00
|
|
|
pub fn get_key(&self, algorithm: KeyAlgorithm) -> Option<&String> {
|
2020-07-14 10:00:29 +00:00
|
|
|
self.keys
|
|
|
|
.get(&AlgorithmAndDeviceId(algorithm, self.device_id.to_string()))
|
2020-04-15 16:55:15 +00:00
|
|
|
}
|
2020-04-17 12:14:31 +00:00
|
|
|
|
|
|
|
/// Get a map containing all the device keys.
|
2020-07-14 10:00:29 +00:00
|
|
|
pub fn keys(&self) -> &BTreeMap<AlgorithmAndDeviceId, String> {
|
2020-04-17 12:14:31 +00:00
|
|
|
&self.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the trust state of the device.
|
|
|
|
pub fn trust_state(&self) -> TrustState {
|
|
|
|
self.trust_state.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of algorithms this device supports.
|
|
|
|
pub fn algorithms(&self) -> &[Algorithm] {
|
|
|
|
&self.algorithms
|
|
|
|
}
|
2020-04-21 07:31:33 +00:00
|
|
|
|
2020-04-21 07:40:50 +00:00
|
|
|
/// Is the device deleted.
|
|
|
|
pub fn deleted(&self) -> bool {
|
|
|
|
self.deleted.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:31:33 +00:00
|
|
|
/// Update a device with a new device keys struct.
|
2020-07-14 09:17:09 +00:00
|
|
|
pub(crate) fn update_device(&mut self, device_keys: &DeviceKeys) -> Result<(), SignatureError> {
|
|
|
|
self.verify_device_keys(device_keys)?;
|
|
|
|
|
2020-04-21 07:31:33 +00:00
|
|
|
let display_name = Arc::new(
|
|
|
|
device_keys
|
|
|
|
.unsigned
|
|
|
|
.as_ref()
|
2020-05-05 13:29:25 +00:00
|
|
|
.map(|d| d.device_display_name.clone())
|
|
|
|
.flatten(),
|
2020-04-21 07:31:33 +00:00
|
|
|
);
|
|
|
|
|
2020-07-14 10:03:27 +00:00
|
|
|
self.algorithms = Arc::new(device_keys.algorithms.clone());
|
|
|
|
self.keys = Arc::new(device_keys.keys.clone());
|
|
|
|
self.display_name = display_name;
|
2020-07-14 09:17:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_signed_by_device(&self, json: &mut Value) -> Result<(), SignatureError> {
|
2020-07-14 09:27:50 +00:00
|
|
|
let signing_key = self
|
2020-07-14 10:00:29 +00:00
|
|
|
.get_key(KeyAlgorithm::Ed25519)
|
2020-07-14 09:27:50 +00:00
|
|
|
.ok_or(SignatureError::MissingSigningKey)?;
|
2020-07-14 09:17:09 +00:00
|
|
|
|
2020-07-14 10:23:42 +00:00
|
|
|
verify_json(&self.user_id, &self.device_id, signing_key, json)
|
2020-07-14 09:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn verify_device_keys(
|
|
|
|
&self,
|
|
|
|
device_keys: &DeviceKeys,
|
|
|
|
) -> Result<(), SignatureError> {
|
|
|
|
self.is_signed_by_device(&mut json!(&device_keys))
|
2020-04-21 07:31:33 +00:00
|
|
|
}
|
2020-04-21 07:40:50 +00:00
|
|
|
|
|
|
|
/// Mark the device as deleted.
|
|
|
|
pub(crate) fn mark_as_deleted(&self) {
|
|
|
|
self.deleted.store(true, Ordering::Relaxed);
|
|
|
|
}
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
impl From<&OlmMachine> for Device {
|
|
|
|
fn from(machine: &OlmMachine) -> Self {
|
|
|
|
Device {
|
2020-04-28 13:05:20 +00:00
|
|
|
user_id: Arc::new(machine.user_id().clone()),
|
|
|
|
device_id: Arc::new(machine.device_id().clone()),
|
2020-04-27 14:31:28 +00:00
|
|
|
algorithms: Arc::new(vec![
|
|
|
|
Algorithm::MegolmV1AesSha2,
|
|
|
|
Algorithm::OlmV1Curve25519AesSha2,
|
|
|
|
]),
|
|
|
|
keys: Arc::new(
|
|
|
|
machine
|
|
|
|
.identity_keys()
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
(
|
2020-07-14 10:00:29 +00:00
|
|
|
AlgorithmAndDeviceId(
|
|
|
|
KeyAlgorithm::try_from(key.as_ref()).unwrap(),
|
|
|
|
machine.device_id().clone(),
|
|
|
|
),
|
2020-04-27 14:31:28 +00:00
|
|
|
value.to_owned(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
display_name: Arc::new(None),
|
|
|
|
deleted: Arc::new(AtomicBool::new(false)),
|
|
|
|
trust_state: Arc::new(Atomic::new(TrustState::Unset)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 09:17:09 +00:00
|
|
|
impl TryFrom<&DeviceKeys> for Device {
|
|
|
|
type Error = SignatureError;
|
|
|
|
|
|
|
|
fn try_from(device_keys: &DeviceKeys) -> Result<Self, Self::Error> {
|
|
|
|
let device = Device {
|
2020-04-09 14:22:25 +00:00
|
|
|
user_id: Arc::new(device_keys.user_id.clone()),
|
2020-04-02 12:52:15 +00:00
|
|
|
device_id: Arc::new(device_keys.device_id.clone()),
|
|
|
|
algorithms: Arc::new(device_keys.algorithms.clone()),
|
2020-07-14 10:00:29 +00:00
|
|
|
keys: Arc::new(device_keys.keys.clone()),
|
2020-04-02 12:52:15 +00:00
|
|
|
display_name: Arc::new(
|
|
|
|
device_keys
|
|
|
|
.unsigned
|
|
|
|
.as_ref()
|
2020-05-05 13:29:25 +00:00
|
|
|
.map(|d| d.device_display_name.clone())
|
|
|
|
.flatten(),
|
2020-04-02 12:52:15 +00:00
|
|
|
),
|
|
|
|
deleted: Arc::new(AtomicBool::new(false)),
|
|
|
|
trust_state: Arc::new(Atomic::new(TrustState::Unset)),
|
2020-07-14 09:17:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
device.verify_device_keys(device_keys)?;
|
|
|
|
Ok(device)
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 15:51:51 +00:00
|
|
|
|
|
|
|
impl PartialEq for Device {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.user_id() == other.user_id() && self.device_id() == other.device_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod test {
|
|
|
|
use serde_json::json;
|
2020-07-14 09:17:09 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-04-15 15:51:51 +00:00
|
|
|
|
2020-04-29 07:48:00 +00:00
|
|
|
use crate::device::{Device, TrustState};
|
2020-05-07 06:51:59 +00:00
|
|
|
use matrix_sdk_common::api::r0::keys::{DeviceKeys, KeyAlgorithm};
|
|
|
|
use matrix_sdk_common::identifiers::UserId;
|
2020-04-15 15:51:51 +00:00
|
|
|
|
2020-04-21 07:31:33 +00:00
|
|
|
fn device_keys() -> DeviceKeys {
|
2020-04-15 15:51:51 +00:00
|
|
|
let device_keys = json!({
|
|
|
|
"algorithms": vec![
|
|
|
|
"m.olm.v1.curve25519-aes-sha2",
|
|
|
|
"m.megolm.v1.aes-sha2"
|
|
|
|
],
|
2020-07-14 09:17:09 +00:00
|
|
|
"device_id": "BNYQQWUMXO",
|
|
|
|
"user_id": "@example:localhost",
|
2020-04-15 15:51:51 +00:00
|
|
|
"keys": {
|
2020-07-14 09:17:09 +00:00
|
|
|
"curve25519:BNYQQWUMXO": "xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc",
|
|
|
|
"ed25519:BNYQQWUMXO": "2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4"
|
2020-04-15 15:51:51 +00:00
|
|
|
},
|
|
|
|
"signatures": {
|
2020-07-14 09:17:09 +00:00
|
|
|
"@example:localhost": {
|
|
|
|
"ed25519:BNYQQWUMXO": "kTwMrbsLJJM/uFGOj/oqlCaRuw7i9p/6eGrTlXjo8UJMCFAetoyWzoMcF35vSe4S6FTx8RJmqX6rM7ep53MHDQ"
|
2020-04-15 15:51:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"unsigned": {
|
|
|
|
"device_display_name": "Alice's mobile phone"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-21 07:31:33 +00:00
|
|
|
serde_json::from_value(device_keys).unwrap()
|
|
|
|
}
|
2020-04-15 15:51:51 +00:00
|
|
|
|
2020-04-21 07:31:33 +00:00
|
|
|
pub(crate) fn get_device() -> Device {
|
|
|
|
let device_keys = device_keys();
|
2020-07-14 09:17:09 +00:00
|
|
|
Device::try_from(&device_keys).unwrap()
|
2020-04-15 15:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_a_device() {
|
2020-07-14 09:17:09 +00:00
|
|
|
let user_id = UserId::try_from("@example:localhost").unwrap();
|
|
|
|
let device_id = "BNYQQWUMXO";
|
2020-04-15 15:51:51 +00:00
|
|
|
|
|
|
|
let device = get_device();
|
|
|
|
|
|
|
|
assert_eq!(&user_id, device.user_id());
|
|
|
|
assert_eq!(device_id, device.device_id());
|
|
|
|
assert_eq!(device.algorithms.len(), 2);
|
2020-04-17 12:14:31 +00:00
|
|
|
assert_eq!(TrustState::Unset, device.trust_state());
|
|
|
|
assert_eq!(
|
|
|
|
"Alice's mobile phone",
|
|
|
|
device.display_name().as_ref().unwrap()
|
|
|
|
);
|
2020-04-15 16:55:15 +00:00
|
|
|
assert_eq!(
|
2020-04-30 11:16:10 +00:00
|
|
|
device.get_key(KeyAlgorithm::Curve25519).unwrap(),
|
2020-07-14 09:17:09 +00:00
|
|
|
"xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc"
|
2020-04-15 16:55:15 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-04-30 11:16:10 +00:00
|
|
|
device.get_key(KeyAlgorithm::Ed25519).unwrap(),
|
2020-07-14 09:17:09 +00:00
|
|
|
"2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4"
|
2020-04-15 16:55:15 +00:00
|
|
|
);
|
2020-04-15 15:51:51 +00:00
|
|
|
}
|
2020-04-21 07:31:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_a_device() {
|
|
|
|
let mut device = get_device();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
"Alice's mobile phone",
|
|
|
|
device.display_name().as_ref().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut device_keys = device_keys();
|
|
|
|
device_keys.unsigned.as_mut().unwrap().device_display_name =
|
2020-05-05 13:29:25 +00:00
|
|
|
Some("Alice's work computer".to_owned());
|
2020-07-14 09:17:09 +00:00
|
|
|
device.update_device(&device_keys).unwrap();
|
2020-04-21 07:31:33 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
"Alice's work computer",
|
|
|
|
device.display_name().as_ref().unwrap()
|
|
|
|
);
|
|
|
|
}
|
2020-04-21 07:40:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_a_device() {
|
|
|
|
let device = get_device();
|
|
|
|
assert!(!device.deleted());
|
|
|
|
|
|
|
|
let device_clone = device.clone();
|
|
|
|
|
|
|
|
device.mark_as_deleted();
|
|
|
|
assert!(device.deleted());
|
|
|
|
assert!(device_clone.deleted());
|
|
|
|
}
|
2020-04-15 15:51:51 +00:00
|
|
|
}
|