Commit 2327658e8c

2327658e8cc580ff20a14f16b79a1830e8dcd38c

parent: b4e9c213e6

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-03-29 03:58 UTC
committer: <noreply@github.com>

Add commands for importing and exporting room keys (#233)

Layout: unified · split

docs/iamb.1 +12 −2
@@ -61,12 +61,22 @@ Log out of
61View a list of joined rooms. 61View a list of joined rooms.
62.It Sy ":spaces" 62.It Sy ":spaces"
63View a list of joined spaces. 63View a list of joined spaces.
64.It Sy ":verify"
65View a list of ongoing E2EE verifications.
66.It Sy ":welcome" 64.It Sy ":welcome"
67View the startup Welcome window. 65View the startup Welcome window.
68.El 66.El
69 67
68.Sh "E2EE COMMANDS"
69.Bl -tag -width Ds
70.It Sy ":keys export [path] [passphrase]"
71Export and encrypt keys to
72.Pa path .
73.It Sy ":keys import [path] [passphrase]"
74Import and decrypt keys from
75.Pa path .
76.It Sy ":verify"
77View a list of ongoing E2EE verifications.
78.El
79
70.Sh "MESSAGE COMMANDS" 80.Sh "MESSAGE COMMANDS"
71.Bl -tag -width Ds 81.Bl -tag -width Ds
72.It Sy ":download" 82.It Sy ":download"
src/base.rs +20 −1
@@ -420,6 +420,15 @@ pub enum HomeserverAction {
420 Logout(String, bool), 420 Logout(String, bool),
421} 421}
422 422
423/// An action performed against the user's room keys.
424#[derive(Clone, Debug, Eq, PartialEq)]
425pub enum KeysAction {
426 /// Export room keys to a file, encrypted with a passphrase.
427 Export(String, String),
428 /// Import room keys from a file, encrypted with a passphrase.
429 Import(String, String),
430}
431
423/// An action that the main program loop should. 432/// An action that the main program loop should.
424/// 433///
425/// See [the commands module][super::commands] for where these are usually created. 434/// See [the commands module][super::commands] for where these are usually created.
@@ -428,6 +437,9 @@ pub enum IambAction {
428 /// Perform an action against the homeserver. 437 /// Perform an action against the homeserver.
429 Homeserver(HomeserverAction), 438 Homeserver(HomeserverAction),
430 439
440 /// Perform an action over room keys.
441 Keys(KeysAction),
442
431 /// Perform an action on the currently selected message. 443 /// Perform an action on the currently selected message.
432 Message(MessageAction), 444 Message(MessageAction),
433 445
@@ -485,6 +497,7 @@ impl ApplicationAction for IambAction {
485 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus { 497 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
486 match self { 498 match self {
487 IambAction::Homeserver(..) => SequenceStatus::Break, 499 IambAction::Homeserver(..) => SequenceStatus::Break,
500 IambAction::Keys(..) => SequenceStatus::Break,
488 IambAction::Message(..) => SequenceStatus::Break, 501 IambAction::Message(..) => SequenceStatus::Break,
489 IambAction::Room(..) => SequenceStatus::Break, 502 IambAction::Room(..) => SequenceStatus::Break,
490 IambAction::OpenLink(..) => SequenceStatus::Break, 503 IambAction::OpenLink(..) => SequenceStatus::Break,
@@ -498,6 +511,7 @@ impl ApplicationAction for IambAction {
498 fn is_last_action(&self, _: &EditContext) -> SequenceStatus { 511 fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
499 match self { 512 match self {
500 IambAction::Homeserver(..) => SequenceStatus::Atom, 513 IambAction::Homeserver(..) => SequenceStatus::Atom,
514 IambAction::Keys(..) => SequenceStatus::Atom,
501 IambAction::Message(..) => SequenceStatus::Atom, 515 IambAction::Message(..) => SequenceStatus::Atom,
502 IambAction::OpenLink(..) => SequenceStatus::Atom, 516 IambAction::OpenLink(..) => SequenceStatus::Atom,
503 IambAction::Room(..) => SequenceStatus::Atom, 517 IambAction::Room(..) => SequenceStatus::Atom,
@@ -511,6 +525,7 @@ impl ApplicationAction for IambAction {
511 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus { 525 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
512 match self { 526 match self {
513 IambAction::Homeserver(..) => SequenceStatus::Ignore, 527 IambAction::Homeserver(..) => SequenceStatus::Ignore,
528 IambAction::Keys(..) => SequenceStatus::Ignore,
514 IambAction::Message(..) => SequenceStatus::Ignore, 529 IambAction::Message(..) => SequenceStatus::Ignore,
515 IambAction::Room(..) => SequenceStatus::Ignore, 530 IambAction::Room(..) => SequenceStatus::Ignore,
516 IambAction::OpenLink(..) => SequenceStatus::Ignore, 531 IambAction::OpenLink(..) => SequenceStatus::Ignore,
@@ -526,6 +541,7 @@ impl ApplicationAction for IambAction {
526 IambAction::Homeserver(..) => false, 541 IambAction::Homeserver(..) => false,
527 IambAction::Message(..) => false, 542 IambAction::Message(..) => false,
528 IambAction::Room(..) => false, 543 IambAction::Room(..) => false,
544 IambAction::Keys(..) => false,
529 IambAction::Send(..) => false, 545 IambAction::Send(..) => false,
530 IambAction::OpenLink(..) => false, 546 IambAction::OpenLink(..) => false,
531 IambAction::ToggleScrollbackFocus => false, 547 IambAction::ToggleScrollbackFocus => false,
@@ -585,6 +601,9 @@ pub enum IambError {
585 #[error("Cryptographic storage error: {0}")] 601 #[error("Cryptographic storage error: {0}")]
586 CryptoStore(#[from] matrix_sdk::encryption::CryptoStoreError), 602 CryptoStore(#[from] matrix_sdk::encryption::CryptoStoreError),
587 603
604 #[error("Failed to import room keys: {0}")]
605 FailedKeyImport(#[from] matrix_sdk::encryption::RoomKeyImportError),
606
588 /// A failure related to the cryptographic store. 607 /// A failure related to the cryptographic store.
589 #[error("Cannot export keys from sled: {0}")] 608 #[error("Cannot export keys from sled: {0}")]
590 UpgradeSled(#[from] crate::sled_export::SledMigrationError), 609 UpgradeSled(#[from] crate::sled_export::SledMigrationError),
@@ -1767,7 +1786,7 @@ fn complete_cmdarg(
1767 match cmd.name.as_str() { 1786 match cmd.name.as_str() {
1768 "cancel" | "dms" | "edit" | "redact" | "reply" => vec![], 1787 "cancel" | "dms" | "edit" | "redact" | "reply" => vec![],
1769 "members" | "rooms" | "spaces" | "welcome" => vec![], 1788 "members" | "rooms" | "spaces" | "welcome" => vec![],
1770 "download" | "open" | "upload" => complete_path(text, cursor), 1789 "download" | "keys" | "open" | "upload" => complete_path(text, cursor),
1771 "react" | "unreact" => complete_emoji(text, cursor, store), 1790 "react" | "unreact" => complete_emoji(text, cursor, store),
1772 1791
1773 "invite" => complete_users(text, cursor, store), 1792 "invite" => complete_users(text, cursor, store),
src/commands.rs +52
@@ -19,6 +19,7 @@ use crate::base::{
19 HomeserverAction, 19 HomeserverAction,
20 IambAction, 20 IambAction,
21 IambId, 21 IambId,
22 KeysAction,
22 MessageAction, 23 MessageAction,
23 ProgramCommand, 24 ProgramCommand,
24 ProgramCommands, 25 ProgramCommands,
@@ -102,6 +103,29 @@ fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
102 return Ok(step); 103 return Ok(step);
103} 104}
104 105
106fn iamb_keys(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
107 let mut args = desc.arg.strings()?;
108
109 if args.len() != 3 {
110 return Err(CommandError::InvalidArgument);
111 }
112
113 let act = args.remove(0);
114 let path = args.remove(0);
115 let passphrase = args.remove(0);
116
117 let act = match act.as_str() {
118 "export" => KeysAction::Export(path, passphrase),
119 "import" => KeysAction::Import(path, passphrase),
120 _ => return Err(CommandError::InvalidArgument),
121 };
122
123 let vact = IambAction::Keys(act);
124 let step = CommandStep::Continue(vact.into(), ctx.context.clone());
125
126 return Ok(step);
127}
128
105fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 129fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
106 let mut args = desc.arg.strings()?; 130 let mut args = desc.arg.strings()?;
107 131
@@ -523,6 +547,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
523 f: iamb_invite, 547 f: iamb_invite,
524 }); 548 });
525 cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join }); 549 cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join });
550 cmds.add_command(ProgramCommand { name: "keys".into(), aliases: vec![], f: iamb_keys });
526 cmds.add_command(ProgramCommand { 551 cmds.add_command(ProgramCommand {
527 name: "leave".into(), 552 name: "leave".into(),
528 aliases: vec![], 553 aliases: vec![],
@@ -959,4 +984,31 @@ mod tests {
959 let res = cmds.input_cmd("redact Removed Removed", ctx.clone()); 984 let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
960 assert_eq!(res, Err(CommandError::InvalidArgument)); 985 assert_eq!(res, Err(CommandError::InvalidArgument));
961 } 986 }
987
988 #[test]
989 fn test_cmd_keys() {
990 let mut cmds = setup_commands();
991 let ctx = EditContext::default();
992
993 let res = cmds.input_cmd("keys import /a/b/c pword", ctx.clone()).unwrap();
994 let act = IambAction::Keys(KeysAction::Import("/a/b/c".into(), "pword".into()));
995 assert_eq!(res, vec![(act.into(), ctx.clone())]);
996
997 let res = cmds.input_cmd("keys export /a/b/c pword", ctx.clone()).unwrap();
998 let act = IambAction::Keys(KeysAction::Export("/a/b/c".into(), "pword".into()));
999 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1000
1001 // Invalid invocations.
1002 let res = cmds.input_cmd("keys", ctx.clone());
1003 assert_eq!(res, Err(CommandError::InvalidArgument));
1004
1005 let res = cmds.input_cmd("keys import", ctx.clone());
1006 assert_eq!(res, Err(CommandError::InvalidArgument));
1007
1008 let res = cmds.input_cmd("keys import foo", ctx.clone());
1009 assert_eq!(res, Err(CommandError::InvalidArgument));
1010
1011 let res = cmds.input_cmd("keys import foo bar baz", ctx.clone());
1012 assert_eq!(res, Err(CommandError::InvalidArgument));
1013 }
962} 1014}
src/main.rs +32
@@ -87,6 +87,7 @@ use crate::{
87 IambId, 87 IambId,
88 IambInfo, 88 IambInfo,
89 IambResult, 89 IambResult,
90 KeysAction,
90 ProgramAction, 91 ProgramAction,
91 ProgramContext, 92 ProgramContext,
92 ProgramStore, 93 ProgramStore,
@@ -529,6 +530,7 @@ impl Application {
529 530
530 None 531 None
531 }, 532 },
533 IambAction::Keys(act) => self.keys_command(act, ctx, store).await?,
532 IambAction::Message(act) => { 534 IambAction::Message(act) => {
533 self.screen.current_window_mut()?.message_command(act, ctx, store).await? 535 self.screen.current_window_mut()?.message_command(act, ctx, store).await?
534 }, 536 },
@@ -603,6 +605,36 @@ impl Application {
603 } 605 }
604 } 606 }
605 607
608 async fn keys_command(
609 &mut self,
610 action: KeysAction,
611 _: ProgramContext,
612 store: &mut ProgramStore,
613 ) -> IambResult<EditInfo> {
614 let encryption = store.application.worker.client.encryption();
615
616 match action {
617 KeysAction::Export(path, passphrase) => {
618 encryption
619 .export_room_keys(path.into(), &passphrase, |_| true)
620 .await
621 .map_err(IambError::from)?;
622
623 Ok(Some("Successfully exported room keys".into()))
624 },
625 KeysAction::Import(path, passphrase) => {
626 let res = encryption
627 .import_room_keys(path.into(), &passphrase)
628 .await
629 .map_err(IambError::from)?;
630
631 let msg = format!("Imported {} of {} keys", res.imported_count, res.total_count);
632
633 Ok(Some(msg.into()))
634 },
635 }
636 }
637
606 fn handle_info(&mut self, info: InfoMessage) { 638 fn handle_info(&mut self, info: InfoMessage) {
607 match info { 639 match info {
608 InfoMessage::Message(info) => { 640 InfoMessage::Message(info) => {