Skip to content
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

Don't use the Curve25519 sender key to store room keys #1091

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions benchmarks/benches/crypto_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ pub fn keys_query(c: &mut Criterion) {
});

let dir = tempfile::tempdir().unwrap();
let store = Arc::new(SledCryptoStore::open_with_passphrase(dir.path(), None).unwrap());
let store = Arc::new(
runtime.block_on(SledCryptoStore::open_with_passphrase(dir.path(), None)).unwrap(),
);
let machine =
runtime.block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store)).unwrap();

Expand Down Expand Up @@ -118,8 +120,11 @@ pub fn keys_claiming(c: &mut Criterion) {
b.iter_batched(
|| {
let dir = tempfile::tempdir().unwrap();
let store =
Arc::new(SledCryptoStore::open_with_passphrase(dir.path(), None).unwrap());
let store = Arc::new(
runtime
.block_on(SledCryptoStore::open_with_passphrase(dir.path(), None))
.unwrap(),
);

let machine = runtime
.block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store))
Expand Down Expand Up @@ -181,7 +186,9 @@ pub fn room_key_sharing(c: &mut Criterion) {
})
});
let dir = tempfile::tempdir().unwrap();
let store = Arc::new(SledCryptoStore::open_with_passphrase(dir.path(), None).unwrap());
let store = Arc::new(
runtime.block_on(SledCryptoStore::open_with_passphrase(dir.path(), None)).unwrap(),
);

let machine =
runtime.block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store)).unwrap();
Expand Down Expand Up @@ -236,7 +243,9 @@ pub fn devices_missing_sessions_collecting(c: &mut Criterion) {
});

let dir = tempfile::tempdir().unwrap();
let store = Arc::new(SledCryptoStore::open_with_passphrase(dir.path(), None).unwrap());
let store = Arc::new(
runtime.block_on(SledCryptoStore::open_with_passphrase(dir.path(), None)).unwrap(),
);

let machine =
runtime.block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store)).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion bindings/matrix-sdk-crypto-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ pub fn migrate(
progress_listener.on_progress(progress as i32, total as i32)
};

let store = SledCryptoStore::open_with_passphrase(path, passphrase.as_deref())?;
let runtime = Runtime::new()?;
let store =
runtime.block_on(SledCryptoStore::open_with_passphrase(path, passphrase.as_deref()))?;

processed_steps += 1;
listener(processed_steps, total_steps);
Expand Down
6 changes: 5 additions & 1 deletion bindings/matrix-sdk-crypto-ffi/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ impl OlmMachine {
let runtime = Runtime::new().expect("Couldn't create a tokio runtime");

let store = Arc::new(
matrix_sdk_sled::SledCryptoStore::open_with_passphrase(path, passphrase.as_deref())
runtime
.block_on(matrix_sdk_sled::SledCryptoStore::open_with_passphrase(
path,
passphrase.as_deref(),
))
.map_err(|e| {
match e {
// This is a bit of an error in the sled store, the
Expand Down
13 changes: 8 additions & 5 deletions bindings/matrix-sdk-crypto-nodejs/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ impl OlmMachine {
let user_id = user_id.clone();
let device_id = device_id.clone();

let store = store_path
.map(|store_path| {
let store = if let Some(store_path) = store_path {
Some(
matrix_sdk_sled::SledCryptoStore::open_with_passphrase(
store_path,
store_passphrase.as_deref(),
)
.await
.map(Arc::new)
.map_err(into_err)
})
.transpose()?;
.map_err(into_err)?,
)
} else {
None
};

store_passphrase.zeroize();

Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ClientBuilder {
let data_path = PathBuf::from(base_path).join(sanitize(username));
fs::create_dir_all(&data_path)?;

inner_builder = inner_builder.sled_store(data_path, None)?;
inner_builder = RUNTIME.block_on(inner_builder.sled_store(data_path, None))?;
}

// Determine server either from URL, server name or user ID.
Expand Down
98 changes: 23 additions & 75 deletions crates/matrix-sdk-crypto/src/gossiping/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,8 @@ impl GossipMachine {
event: &RoomKeyRequestEvent,
key_info: &MegolmV1AesSha2Content,
) -> OlmResult<Option<Session>> {
let session = self
.store
.get_inbound_group_session(
&key_info.room_id,
&key_info.sender_key.to_base64(),
&key_info.session_id,
)
.await?;
let session =
self.store.get_inbound_group_session(&key_info.room_id, &key_info.session_id).await?;

let session = if let Some(s) = session {
s
Expand Down Expand Up @@ -881,55 +875,33 @@ impl GossipMachine {
) -> Result<Option<InboundGroupSession>, CryptoStoreError> {
match InboundGroupSession::from_forwarded_key(&algorithm, content) {
Ok(session) => {
let old_session = self
.store
.get_inbound_group_session(
session.room_id(),
&session.sender_key.to_base64(),
session.session_id(),
)
.await?;

let session_id = session.session_id().to_owned();

// If we have a previous session, check if we have a better version
// and store the new one if so.
let session = if let Some(old_session) = old_session {
if session.compare(&old_session).await == SessionOrdering::Better {
self.mark_as_done(info).await?;
Some(session)
} else {
None
}
// If we didn't have a previous session, store it.
} else {
if self.store.compare_group_session(&session).await? == SessionOrdering::Better {
self.mark_as_done(info).await?;
Some(session)
};

if let Some(s) = &session {
info!(
%sender,
sender_key = sender_key.to_base64(),
claimed_sender_key = content.claimed_sender_key.to_base64(),
room_id = s.room_id().as_str(),
session_id = session_id.as_str(),
%algorithm,
%sender_key,
claimed_sender_key = %session.sender_key(),
room_id = session.room_id().as_str(),
session_id = session.session_id(),
algorithm = %session.algorithm(),
"Received a forwarded room key",
);

Ok(Some(session))
} else {
info!(
%sender,
sender_key = sender_key.to_base64(),
claimed_sender_key = content.claimed_sender_key.to_base64(),
room_id = %content.room_id,
session_id = session_id.as_str(),
%algorithm,
%sender_key,
claimed_sender_key = %session.sender_key(),
room_id = %session.room_id,
session_id = session.session_id(),
algorithm = %session.algorithm(),
"Received a forwarded room key but we already have a better version of it",
);
}

Ok(session)
Ok(None)
}
}
Err(e) => {
warn!(
Expand Down Expand Up @@ -1351,11 +1323,7 @@ mod tests {

assert!(machine
.store
.get_inbound_group_session(
session.room_id(),
&session.sender_key.to_base64(),
session.session_id(),
)
.get_inbound_group_session(session.room_id(), session.session_id(),)
.await
.unwrap()
.is_none());
Expand Down Expand Up @@ -1566,11 +1534,7 @@ mod tests {
// Check that alice doesn't have the session.
assert!(alice_machine
.store
.get_inbound_group_session(
room_id(),
&bob_machine.store.account().identity_keys().curve25519.to_base64(),
group_session.session_id()
)
.get_inbound_group_session(room_id(), group_session.session_id())
.await
.unwrap()
.is_none());
Expand All @@ -1590,11 +1554,7 @@ mod tests {
// Check that alice now does have the session.
let session = alice_machine
.store
.get_inbound_group_session(
room_id(),
&decrypted.result.sender_key.to_base64(),
group_session.session_id(),
)
.get_inbound_group_session(room_id(), group_session.session_id())
.await
.unwrap()
.unwrap();
Expand Down Expand Up @@ -1633,11 +1593,7 @@ mod tests {
// Check that alice doesn't have the session.
assert!(alice_machine
.store
.get_inbound_group_session(
room_id(),
&bob_machine.store.account().identity_keys().curve25519.to_base64(),
group_session.session_id()
)
.get_inbound_group_session(room_id(), group_session.session_id())
.await
.unwrap()
.is_none());
Expand Down Expand Up @@ -1777,11 +1733,7 @@ mod tests {
// Check that alice doesn't have the session.
assert!(alice_machine
.store
.get_inbound_group_session(
room_id(),
&bob_machine.store.account().identity_keys().curve25519.to_base64(),
group_session.session_id()
)
.get_inbound_group_session(room_id(), group_session.session_id())
.await
.unwrap()
.is_none());
Expand All @@ -1801,11 +1753,7 @@ mod tests {
// Check that alice now does have the session.
let session = alice_machine
.store
.get_inbound_group_session(
room_id(),
&decrypted.result.sender_key.to_base64(),
group_session.session_id(),
)
.get_inbound_group_session(room_id(), group_session.session_id())
.await
.unwrap()
.unwrap();
Expand Down
Loading