feat: end to end encryption

This commit is contained in:
timokoesters 2020-05-17 19:56:40 +02:00
parent 4fb79ebb4c
commit 7fc71b3968
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
7 changed files with 461 additions and 65 deletions

View file

@ -1,7 +1,7 @@
use crate::Result;
use ruma_events::{collections::only::Event as EduEvent, EventJson};
use crate::{utils, Error, Result};
use ruma_events::{collections::only::Event as EduEvent, EventJson, EventType};
use ruma_identifiers::{RoomId, UserId};
use std::collections::HashMap;
use std::{collections::HashMap, convert::TryFrom};
pub struct AccountData {
pub(super) roomuserdataid_accountdata: sled::Tree, // RoomUserDataId = Room + User + Count + Type
@ -13,7 +13,8 @@ impl AccountData {
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
event: EduEvent,
kind: &EventType,
data: serde_json::Value,
globals: &super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id
@ -48,11 +49,10 @@ impl AccountData {
let mut key = prefix;
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
key.push(0xff);
let json = serde_json::to_value(&event)?;
key.extend_from_slice(json["type"].as_str().unwrap().as_bytes());
key.extend_from_slice(kind.to_string().as_bytes());
self.roomuserdataid_accountdata
.insert(key, &*json.to_string())
.insert(key, &*data.to_string())
.unwrap();
Ok(())
@ -64,7 +64,7 @@ impl AccountData {
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
kind: &str,
kind: &EventType,
) -> Result<Option<EventJson<EduEvent>>> {
Ok(self.all(room_id, user_id)?.remove(kind))
}
@ -75,7 +75,7 @@ impl AccountData {
room_id: Option<&RoomId>,
user_id: &UserId,
since: u64,
) -> Result<HashMap<String, EventJson<EduEvent>>> {
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
let mut userdata = HashMap::new();
let mut prefix = room_id
@ -91,17 +91,30 @@ impl AccountData {
let mut first_possible = prefix.clone();
first_possible.extend_from_slice(&(since + 1).to_be_bytes());
for json in self
for r in self
.roomuserdataid_accountdata
.range(&*first_possible..)
.filter_map(|r| r.ok())
.take_while(move |(k, _)| k.starts_with(&prefix))
.map(|(_, v)| serde_json::from_slice::<serde_json::Value>(&v).unwrap())
.map(|(k, v)| {
Ok::<_, Error>((
EventType::try_from(utils::string_from_bytes(
k.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("roomuserdataid is invalid"))?,
)?)
.map_err(|_| Error::BadDatabase("roomuserdataid is invalid"))?,
serde_json::from_slice::<serde_json::Value>(&v).unwrap(),
))
})
{
let (kind, content) = r.unwrap();
let mut json = serde_json::Map::new();
json.insert("content".to_owned(), content);
json.insert("type".to_owned(), kind.to_string().into());
userdata.insert(
json["type"].as_str().unwrap().to_owned(),
serde_json::from_value::<EventJson<EduEvent>>(json)
.expect("userdata in db is valid"),
kind,
serde_json::from_value::<EventJson<EduEvent>>(json.into())?,
);
}
@ -113,7 +126,7 @@ impl AccountData {
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
) -> Result<HashMap<String, EventJson<EduEvent>>> {
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
self.changes_since(room_id, user_id, 0)
}
}

View file

@ -3,8 +3,8 @@ use ruma_events::{collections::only::Event as EduEvent, EventJson};
use ruma_identifiers::UserId;
pub struct GlobalEdus {
//pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Count
pub(super) globallatestid_globallatest: sled::Tree, // Presence, GlobalLatestId = Count + UserId
//pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Count
}
impl GlobalEdus {

View file

@ -1,6 +1,9 @@
use crate::{utils, Error, Result};
use ruma_identifiers::UserId;
use std::convert::TryFrom;
use js_int::UInt;
use ruma_client_api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey};
use ruma_events::{to_device::AnyToDeviceEvent, EventJson, EventType};
use ruma_identifiers::{DeviceId, UserId};
use std::{collections::BTreeMap, convert::TryFrom};
pub struct Users {
pub(super) userid_password: sled::Tree,
@ -9,6 +12,11 @@ pub struct Users {
pub(super) userdeviceids: sled::Tree,
pub(super) userdeviceid_token: sled::Tree,
pub(super) token_userdeviceid: sled::Tree,
pub(super) onetimekeyid_onetimekeys: sled::Tree, // OneTimeKeyId = UserId + AlgorithmAndDeviceId
pub(super) userdeviceid_devicekeys: sled::Tree,
pub(super) todeviceid_events: sled::Tree, // ToDeviceId = UserId + DeviceId + Count
}
impl Users {
@ -96,7 +104,7 @@ impl Users {
}
/// Adds a new device to a user.
pub fn create_device(&self, user_id: &UserId, device_id: &str, token: &str) -> Result<()> {
pub fn create_device(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
if !self.exists(user_id)? {
return Err(Error::BadRequest(
"tried to create device for nonexistent user",
@ -114,8 +122,22 @@ impl Users {
Ok(())
}
/// Returns an iterator over all device ids of this user.
pub fn all_device_ids(&self, user_id: &UserId) -> impl Iterator<Item = Result<DeviceId>> {
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
self.userdeviceids.scan_prefix(prefix).keys().map(|bytes| {
Ok(utils::string_from_bytes(
&*bytes?
.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("userdeviceid is invalid"))?,
)?)
})
}
/// Replaces the access token of one device.
pub fn set_token(&self, user_id: &UserId, device_id: &str, token: &str) -> Result<()> {
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
@ -138,4 +160,196 @@ impl Users {
Ok(())
}
pub fn add_one_time_key(
&self,
user_id: &UserId,
device_id: &DeviceId,
one_time_key_key: &AlgorithmAndDeviceId,
one_time_key_value: &OneTimeKey,
) -> Result<()> {
let mut key = user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.as_bytes());
if self.userdeviceids.get(&key)?.is_none() {
return Err(Error::BadRequest(
"Tried to set token for nonexistent device",
));
}
key.push(0xff);
// TODO: Use AlgorithmAndDeviceId::to_string when it's available (and update everything,
// because there are no wrapping quotation marks anymore)
key.extend_from_slice(&serde_json::to_string(one_time_key_key)?.as_bytes());
self.onetimekeyid_onetimekeys
.insert(&key, &*serde_json::to_string(&one_time_key_value)?)?;
Ok(())
}
pub fn take_one_time_key(
&self,
user_id: &UserId,
device_id: &DeviceId,
key_algorithm: &KeyAlgorithm,
) -> Result<Option<(AlgorithmAndDeviceId, OneTimeKey)>> {
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(device_id.as_bytes());
prefix.push(0xff);
prefix.push(b'"'); // Annoying quotation mark
prefix.extend_from_slice(key_algorithm.to_string().as_bytes());
prefix.push(b':');
self.onetimekeyid_onetimekeys
.scan_prefix(&prefix)
.next()
.map(|r| {
let (key, value) = r?;
Ok((
serde_json::from_slice(
&*key
.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("onetimekeyid is invalid"))?,
)?,
serde_json::from_slice(&*value)?,
))
})
.transpose()
}
pub fn count_one_time_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
) -> Result<BTreeMap<KeyAlgorithm, UInt>> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
let mut counts = BTreeMap::new();
for algorithm in self
.onetimekeyid_onetimekeys
.scan_prefix(&userdeviceid)
.keys()
.map(|bytes| {
Ok::<_, Error>(
serde_json::from_slice::<AlgorithmAndDeviceId>(
&*bytes?
.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("onetimekeyid is invalid"))?,
)?
.0,
)
})
{
*counts.entry(algorithm?).or_default() += UInt::from(1_u32);
}
Ok(counts)
}
pub fn add_device_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_keys: &DeviceKeys,
) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
self.userdeviceid_devicekeys
.insert(&userdeviceid, &*serde_json::to_string(&device_keys)?)?;
Ok(())
}
pub fn get_device_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
) -> impl Iterator<Item = Result<DeviceKeys>> {
let mut key = user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.as_bytes());
self.userdeviceid_devicekeys
.scan_prefix(key)
.values()
.map(|bytes| Ok(serde_json::from_slice(&bytes?)?))
}
pub fn all_device_keys(
&self,
user_id: &UserId,
) -> impl Iterator<Item = Result<(String, DeviceKeys)>> {
let mut key = user_id.to_string().as_bytes().to_vec();
key.push(0xff);
self.userdeviceid_devicekeys.scan_prefix(key).map(|r| {
let (key, value) = r?;
Ok((
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("userdeviceid is invalid"))?,
)?,
serde_json::from_slice(&*value)?,
))
})
}
pub fn add_to_device_event(
&self,
sender: &UserId,
target_user_id: &UserId,
target_device_id: &DeviceId,
event_type: &EventType,
content: serde_json::Value,
globals: &super::globals::Globals,
) -> Result<()> {
let mut key = target_user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(target_device_id.as_bytes());
key.push(0xff);
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
let mut json = serde_json::Map::new();
json.insert("type".to_owned(), event_type.to_string().into());
json.insert("sender".to_owned(), sender.to_string().into());
json.insert("content".to_owned(), content);
self.todeviceid_events
.insert(&key, &*serde_json::to_string(&json)?)?;
Ok(())
}
pub fn take_to_device_events(
&self,
user_id: &UserId,
device_id: &DeviceId,
max: usize,
) -> Result<Vec<EventJson<AnyToDeviceEvent>>> {
let mut events = Vec::new();
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(device_id.as_bytes());
prefix.push(0xff);
for result in self.todeviceid_events.scan_prefix(&prefix).take(max) {
let (key, value) = result?;
events.push(serde_json::from_slice(&*value)?);
self.todeviceid_events.remove(key)?;
}
Ok(events)
}
}