-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embed device keys in Olm-encrypted messages #3517
Changes from 1 commit
00584ff
c3f4a73
304bcf3
2e0d21b
4d0b45a
dce9baf
657204e
552e0bc
7fcdd6f
7b2a05f
392eacc
0b74654
91c2d9a
50b9ae7
6dfe864
447b212
2d66041
bab5d4f
0b1d221
b42acd0
304955d
fc1dabf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ use vodozemac::{ | |
#[cfg(feature = "experimental-algorithms")] | ||
use crate::types::events::room::encrypted::OlmV2Curve25519AesSha2Content; | ||
use crate::{ | ||
error::{EventError, OlmResult}, | ||
error::{EventError, OlmResult, SessionPickleError}, | ||
types::{ | ||
events::room::encrypted::{OlmV1Curve25519AesSha2Content, ToDeviceEncryptedEventContent}, | ||
DeviceKeys, EventEncryptionAlgorithm, | ||
|
@@ -45,8 +45,8 @@ pub struct Session { | |
pub session_id: Arc<str>, | ||
/// The Key of the sender | ||
pub sender_key: Curve25519PublicKey, | ||
/// The signed device keys | ||
pub device_keys: DeviceKeys, | ||
/// Our own signed device keys | ||
pub our_device_keys: DeviceKeys, | ||
/// Has this been created using the fallback key | ||
pub created_using_fallback_key: bool, | ||
/// When the session was created | ||
|
@@ -151,12 +151,12 @@ impl Session { | |
recipient_device.ed25519_key().ok_or(EventError::MissingSigningKey)?; | ||
|
||
let payload = json!({ | ||
"sender": &self.device_keys.user_id, | ||
"sender_device": &self.device_keys.device_id, | ||
"sender": &self.our_device_keys.user_id, | ||
"sender_device": &self.our_device_keys.device_id, | ||
"keys": { | ||
"ed25519": self.device_keys.ed25519_key().unwrap().to_base64(), | ||
"ed25519": self.our_device_keys.ed25519_key().expect("Device doesn't have ed25519 key").to_base64(), | ||
}, | ||
"device_keys": self.device_keys, | ||
"device_keys": self.our_device_keys, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this property need a different name:
We can do this in a follow up, so we don't risk more merge conflicts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"recipient": recipient_device.user_id(), | ||
"recipient_keys": { | ||
"ed25519": recipient_signing_key.to_base64(), | ||
|
@@ -174,14 +174,20 @@ impl Session { | |
EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => OlmV1Curve25519AesSha2Content { | ||
ciphertext, | ||
recipient_key: self.sender_key, | ||
sender_key: self.device_keys.curve25519_key().unwrap(), | ||
sender_key: self | ||
.our_device_keys | ||
.curve25519_key() | ||
.expect("Device doesn't have curve25519 key"), | ||
message_id, | ||
} | ||
.into(), | ||
#[cfg(feature = "experimental-algorithms")] | ||
EventEncryptionAlgorithm::OlmV2Curve25519AesSha2 => OlmV2Curve25519AesSha2Content { | ||
ciphertext, | ||
sender_key: self.device_keys.curve25519_key().unwrap(), | ||
sender_key: self | ||
.device_keys | ||
.curve25519_key() | ||
.expect("Device doesn't have curve25519 key"), | ||
message_id, | ||
} | ||
.into(), | ||
|
@@ -223,30 +229,32 @@ impl Session { | |
/// | ||
/// # Arguments | ||
/// | ||
/// * `user_id` - Our own user id that the session belongs to. | ||
/// | ||
/// * `device_id` - Our own device ID that the session belongs to. | ||
/// | ||
/// * `our_identity_keys` - An clone of the Arc to our own identity keys. | ||
/// * `our_device_keys` - Our own signed device keys. | ||
/// | ||
/// * `pickle` - The pickled version of the `Session`. | ||
/// | ||
/// * `pickle_mode` - The mode that was used to pickle the session, either | ||
/// an unencrypted mode or an encrypted using passphrase. | ||
pub fn from_pickle(device_keys: DeviceKeys, pickle: PickledSession) -> Self { | ||
// FIXME: assert that device_keys has curve25519 and ed25519 keys | ||
pub fn from_pickle( | ||
our_device_keys: DeviceKeys, | ||
pickle: PickledSession, | ||
) -> Result<Self, SessionPickleError> { | ||
if our_device_keys.curve25519_key().is_none() { | ||
return Err(SessionPickleError::MissingIdentityKey); | ||
} | ||
if our_device_keys.ed25519_key().is_none() { | ||
return Err(SessionPickleError::MissingSigningKey); | ||
} | ||
|
||
let session: vodozemac::olm::Session = pickle.pickle.into(); | ||
let session_id = session.session_id(); | ||
|
||
Session { | ||
Ok(Session { | ||
inner: Arc::new(Mutex::new(session)), | ||
session_id: session_id.into(), | ||
created_using_fallback_key: pickle.created_using_fallback_key, | ||
sender_key: pickle.sender_key, | ||
device_keys, | ||
our_device_keys, | ||
creation_time: pickle.creation_time, | ||
last_use_time: pickle.last_use_time, | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -278,6 +286,7 @@ pub struct PickledSession { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use assert_matches2::assert_let; | ||
use matrix_sdk_test::async_test; | ||
use ruma::{device_id, user_id}; | ||
use serde_json::{self, Value}; | ||
|
@@ -320,15 +329,9 @@ mod tests { | |
.unwrap(); | ||
|
||
#[cfg(feature = "experimental-algorithms")] | ||
let ToDeviceEncryptedEventContent::OlmV2Curve25519AesSha2(content) = message | ||
else { | ||
panic!("Invalid encrypted event algorithm {}", message.algorithm()); | ||
}; | ||
assert_let!(ToDeviceEncryptedEventContent::OlmV2Curve25519AesSha2(content) = message); | ||
#[cfg(not(feature = "experimental-algorithms"))] | ||
let ToDeviceEncryptedEventContent::OlmV1Curve25519AesSha2(content) = message | ||
else { | ||
panic!("Invalid encrypted event algorithm {}", message.algorithm()); | ||
}; | ||
assert_let!(ToDeviceEncryptedEventContent::OlmV1Curve25519AesSha2(content) = message); | ||
|
||
let prekey = if let OlmMessage::PreKey(m) = content.ciphertext { | ||
m | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
SessionUnpickleError
might be marginally better, feel free to disagree.