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
6161View a list of joined rooms.
6262.It Sy ":spaces"
6363View a list of joined spaces.
64.It Sy ":verify"
65View a list of ongoing E2EE verifications.
6664.It Sy ":welcome"
6765View the startup Welcome window.
6866.El
6967
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
7080.Sh "MESSAGE COMMANDS"
7181.Bl -tag -width Ds
7282.It Sy ":download"
src/base.rs +20 −1
@@ -420,6 +420,15 @@ pub enum HomeserverAction {
420420 Logout(String, bool),
421421}
422422
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
423432/// An action that the main program loop should.
424433///
425434/// See [the commands module][super::commands] for where these are usually created.
@@ -428,6 +437,9 @@ pub enum IambAction {
428437 /// Perform an action against the homeserver.
429438 Homeserver(HomeserverAction),
430439
440 /// Perform an action over room keys.
441 Keys(KeysAction),
442
431443 /// Perform an action on the currently selected message.
432444 Message(MessageAction),
433445
@@ -485,6 +497,7 @@ impl ApplicationAction for IambAction {
485497 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
486498 match self {
487499 IambAction::Homeserver(..) => SequenceStatus::Break,
500 IambAction::Keys(..) => SequenceStatus::Break,
488501 IambAction::Message(..) => SequenceStatus::Break,
489502 IambAction::Room(..) => SequenceStatus::Break,
490503 IambAction::OpenLink(..) => SequenceStatus::Break,
@@ -498,6 +511,7 @@ impl ApplicationAction for IambAction {
498511 fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
499512 match self {
500513 IambAction::Homeserver(..) => SequenceStatus::Atom,
514 IambAction::Keys(..) => SequenceStatus::Atom,
501515 IambAction::Message(..) => SequenceStatus::Atom,
502516 IambAction::OpenLink(..) => SequenceStatus::Atom,
503517 IambAction::Room(..) => SequenceStatus::Atom,
@@ -511,6 +525,7 @@ impl ApplicationAction for IambAction {
511525 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
512526 match self {
513527 IambAction::Homeserver(..) => SequenceStatus::Ignore,
528 IambAction::Keys(..) => SequenceStatus::Ignore,
514529 IambAction::Message(..) => SequenceStatus::Ignore,
515530 IambAction::Room(..) => SequenceStatus::Ignore,
516531 IambAction::OpenLink(..) => SequenceStatus::Ignore,
@@ -526,6 +541,7 @@ impl ApplicationAction for IambAction {
526541 IambAction::Homeserver(..) => false,
527542 IambAction::Message(..) => false,
528543 IambAction::Room(..) => false,
544 IambAction::Keys(..) => false,
529545 IambAction::Send(..) => false,
530546 IambAction::OpenLink(..) => false,
531547 IambAction::ToggleScrollbackFocus => false,
@@ -585,6 +601,9 @@ pub enum IambError {
585601 #[error("Cryptographic storage error: {0}")]
586602 CryptoStore(#[from] matrix_sdk::encryption::CryptoStoreError),
587603
604 #[error("Failed to import room keys: {0}")]
605 FailedKeyImport(#[from] matrix_sdk::encryption::RoomKeyImportError),
606
588607 /// A failure related to the cryptographic store.
589608 #[error("Cannot export keys from sled: {0}")]
590609 UpgradeSled(#[from] crate::sled_export::SledMigrationError),
@@ -1767,7 +1786,7 @@ fn complete_cmdarg(
17671786 match cmd.name.as_str() {
17681787 "cancel" | "dms" | "edit" | "redact" | "reply" => vec![],
17691788 "members" | "rooms" | "spaces" | "welcome" => vec![],
1770 "download" | "open" | "upload" => complete_path(text, cursor),
1789 "download" | "keys" | "open" | "upload" => complete_path(text, cursor),
17711790 "react" | "unreact" => complete_emoji(text, cursor, store),
17721791
17731792 "invite" => complete_users(text, cursor, store),
src/commands.rs +52
@@ -19,6 +19,7 @@ use crate::base::{
1919 HomeserverAction,
2020 IambAction,
2121 IambId,
22 KeysAction,
2223 MessageAction,
2324 ProgramCommand,
2425 ProgramCommands,
@@ -102,6 +103,29 @@ fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
102103 return Ok(step);
103104}
104105
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
105129fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
106130 let mut args = desc.arg.strings()?;
107131
@@ -523,6 +547,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
523547 f: iamb_invite,
524548 });
525549 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 });
526551 cmds.add_command(ProgramCommand {
527552 name: "leave".into(),
528553 aliases: vec![],
@@ -959,4 +984,31 @@ mod tests {
959984 let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
960985 assert_eq!(res, Err(CommandError::InvalidArgument));
961986 }
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 }
9621014}
src/main.rs +32
@@ -87,6 +87,7 @@ use crate::{
8787 IambId,
8888 IambInfo,
8989 IambResult,
90 KeysAction,
9091 ProgramAction,
9192 ProgramContext,
9293 ProgramStore,
@@ -529,6 +530,7 @@ impl Application {
529530
530531 None
531532 },
533 IambAction::Keys(act) => self.keys_command(act, ctx, store).await?,
532534 IambAction::Message(act) => {
533535 self.screen.current_window_mut()?.message_command(act, ctx, store).await?
534536 },
@@ -603,6 +605,36 @@ impl Application {
603605 }
604606 }
605607
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
606638 fn handle_info(&mut self, info: InfoMessage) {
607639 match info {
608640 InfoMessage::Message(info) => {