feat(crypto): Throw an error if our user/device pair isn't what we have in the store

This commit is contained in:
Damir Jelić
2023-02-06 11:40:55 +01:00
parent fc8cd2e7e5
commit 945c16a7fb
2 changed files with 27 additions and 6 deletions

View File

@@ -78,7 +78,8 @@ use crate::{
Signatures,
},
verification::{Verification, VerificationMachine, VerificationRequest},
CrossSigningKeyExport, ReadOnlyDevice, RoomKeyImportResult, SignatureError, ToDeviceRequest,
CrossSigningKeyExport, CryptoStoreError, ReadOnlyDevice, RoomKeyImportResult, SignatureError,
ToDeviceRequest,
};
/// State machine implementation of the Olm/Megolm encryption protocol used for
@@ -233,11 +234,18 @@ impl OlmMachine {
) -> StoreResult<Self> {
let account = match store.load_account().await? {
Some(a) => {
debug!(
ed25519_key = a.identity_keys().ed25519.to_base64().as_str(),
"Restored an Olm account"
);
a
if user_id != a.user_id() || device_id != a.device_id() {
return Err(CryptoStoreError::MismatchedAccount {
expected: (a.user_id().to_owned(), a.device_id().to_owned()),
got: (user_id.to_owned(), device_id.to_owned()),
});
} else {
debug!(
ed25519_key = a.identity_keys().ed25519.to_base64().as_str(),
"Restored an Olm account"
);
a
}
}
None => {
let account = ReadOnlyAccount::new(user_id, device_id);

View File

@@ -725,6 +725,19 @@ pub enum CryptoStoreError {
#[error("can't save/load sessions or group sessions in the store before an account is stored")]
AccountUnset,
/// The store doesn't support multiple accounts and data from another device
/// was discovered.
#[error(
"the account in the store doesn't match the account in the constructor: \
expected {}:{}, got {}:{}", .expected.0, .expected.1, .got.0, .got.1
)]
MismatchedAccount {
/// The expected user/device id pair.
expected: (OwnedUserId, OwnedDeviceId),
/// The user/device id pair that was loaded from the store.
got: (OwnedUserId, OwnedDeviceId),
},
/// An IO error occurred.
#[error(transparent)]
Io(#[from] IoError),