-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
api: add API to reset contact encryption #6170
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,43 @@ impl ContactId { | |
.await?; | ||
Ok(()) | ||
} | ||
|
||
/// Returns contact adress. | ||
pub async fn addr(&self, context: &Context) -> Result<String> { | ||
let addr = context | ||
.sql | ||
.query_row("SELECT addr FROM contacts WHERE id=?", (self,), |row| { | ||
let addr: String = row.get(0)?; | ||
Ok(addr) | ||
}) | ||
.await?; | ||
Ok(addr) | ||
} | ||
|
||
/// Resets encryption with the contact. | ||
/// | ||
/// Effect is similar to receiving a message without Autocrypt header | ||
/// from the contact, but this action is triggered manually by the user. | ||
/// | ||
/// For example, this will result in sending the next message | ||
/// to 1:1 chat unencrypted, but will not remove existing verified keys. | ||
pub async fn reset_encryption(self, context: &Context) -> Result<()> { | ||
let now = time(); | ||
|
||
let addr = self.addr(context).await?; | ||
if let Some(mut peerstate) = Peerstate::from_addr(context, &addr).await? { | ||
peerstate.degrade_encryption(now); | ||
peerstate.save_to_db(&context.sql).await?; | ||
} | ||
|
||
// Reset 1:1 chat protection. | ||
if let Some(chat_id) = ChatId::lookup_by_contact(context, self).await? { | ||
chat_id | ||
.set_protection(context, ProtectionStatus::Unprotected, now, Some(self)) | ||
.await?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for ContactId { | ||
|
@@ -3152,4 +3189,59 @@ Until the false-positive is fixed: | |
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
async fn test_reset_encryption() -> Result<()> { | ||
let mut tcm = TestContextManager::new(); | ||
let alice = &tcm.alice().await; | ||
let bob = &tcm.bob().await; | ||
|
||
let msg = tcm.send_recv_accept(alice, bob, "Hello!").await; | ||
assert_eq!(msg.get_showpadlock(), false); | ||
|
||
let msg = tcm.send_recv(bob, alice, "Hi!").await; | ||
assert_eq!(msg.get_showpadlock(), true); | ||
let alice_bob_contact_id = msg.from_id; | ||
|
||
alice_bob_contact_id.reset_encryption(alice).await?; | ||
|
||
let msg = tcm.send_recv(alice, bob, "Unencrypted").await; | ||
assert_eq!(msg.get_showpadlock(), false); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
async fn test_reset_verified_encryption() -> Result<()> { | ||
let mut tcm = TestContextManager::new(); | ||
let alice = &tcm.alice().await; | ||
let bob = &tcm.bob().await; | ||
|
||
tcm.execute_securejoin(bob, alice).await; | ||
|
||
let msg = tcm.send_recv(bob, alice, "Encrypted").await; | ||
assert_eq!(msg.get_showpadlock(), true); | ||
|
||
let alice_bob_chat_id = msg.chat_id; | ||
let alice_bob_contact_id = msg.from_id; | ||
alice_bob_contact_id.reset_encryption(alice).await?; | ||
|
||
// Check that the contact is still verified after resetting encryption. | ||
let alice_bob_contact = Contact::get_by_id(alice, alice_bob_contact_id).await?; | ||
assert_eq!(alice_bob_contact.is_verified(alice).await?, true); | ||
|
||
// 1:1 chat and profile is no longer verified. | ||
assert_eq!(alice_bob_contact.is_profile_verified(alice).await?, false); | ||
|
||
let info_msg = alice.get_last_msg_in(alice_bob_chat_id).await; | ||
assert_eq!( | ||
info_msg.text, | ||
"[email protected] sent a message from another device." | ||
); | ||
|
||
let msg = tcm.send_recv(alice, bob, "Unencrypted").await; | ||
assert_eq!(msg.get_showpadlock(), false); | ||
link2xt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The text is not really fitting. Not sure we need a new translatable string now for this. If it is hidden behind encryption info and developer options then can be considered more a debugging tool: deltachat/deltachat-desktop#4312