Debian Codegraph @

src/cli/encrypt.rs

raw
1 //! Command-line parser for `sq encrypt`.
2
3 use clap::{ValueEnum, Parser};
4
5 use super::types::ClapData;
6 use super::types::EncryptPurpose;
7 use super::types::FileOrStdin;
8 use super::types::FileOrStdout;
9
10 use crate::cli::config;
11 use crate::cli::types::CertDesignators;
12 use crate::cli::types::cert_designator::*;
13 use crate::cli::types::Profile;
14
15 use crate::cli::examples;
16 use examples::*;
17
18 /// Key for the help augmentation.
19 pub const ENCRYPT_FOR_SELF: &str = "encrypt.for-self";
20
21 const ENCRYPT_EXAMPLES: Actions = Actions {
22 actions: &[
23 Action::setup().command(&[
24 "sq", "pki", "link", "add",
25 "--cert", "EB28F26E2739A4870ECC47726F0073F60FD0CBF0",
26 "--email", "[email protected]",
27 ]).build(),
28
29 Action::setup().command(&[
30 "sq", "key", "import", "juliet-secret.pgp",
31 ]).build(),
32
33 Action::setup().command(&[
34 "sq", "pki", "link", "add",
35 "--cert=7A58B15E3B9459483D9FFA8D40E299AC5F2B0872",
36 "[email protected]",
37 ]).build(),
38
39 Action::example().comment(
40 "Encrypt a file for a recipient given by fingerprint.",
41 ).command(&[
42 "sq", "encrypt",
43 "--for=EB28F26E2739A4870ECC47726F0073F60FD0CBF0",
44 "[email protected]",
45 "document.txt",
46 ]).build(),
47
48 Action::example().comment(
49 "Encrypt a file for a recipient given by email.",
50 ).command(&[
51 "sq", "encrypt",
52 "[email protected]",
53 "[email protected]",
54 "document.txt",
55 ]).build(),
56 ]
57 };
58
59 test_examples!(sq_encrypt, ENCRYPT_EXAMPLES);
60
61 #[derive(Parser, Debug)]
62 #[clap(
63 name = "encrypt",
64 about = "Encrypt a message",
65 long_about =
66 "Encrypt a message
67
68 Encrypt a message for any number of recipients and with any number of \
69 passwords, optionally signing the message in the process.
70
71 The converse operation is `sq decrypt`.
72
73 `sq encrypt` respects the reference time set by the top-level \
74 `--time` argument. It uses the reference time when selecting \
75 encryption keys, and it sets the signature's creation time to the \
76 reference time.
77 ",
78 after_help = ENCRYPT_EXAMPLES,
79 )]
80 pub struct Command {
81 #[clap(
82 default_value_t = FileOrStdin::default(),
83 help = FileOrStdin::HELP_OPTIONAL,
84 value_name = FileOrStdin::VALUE_NAME,
85 )]
86 pub input: FileOrStdin,
87 #[clap(
88 default_value_t = FileOrStdout::default(),
89 help = FileOrStdout::HELP_OPTIONAL,
90 long,
91 value_name = FileOrStdout::VALUE_NAME,
92 )]
93 pub output: FileOrStdout,
94 #[clap(
95 long,
96 help = "Emit binary data",
97 )]
98 pub binary: bool,
99
100 #[command(flatten)]
101 pub recipients: CertDesignators<CertUserIDEmailFileSelfWithPasswordArgs,
102 RecipientPrefix>,
103
104 #[clap(
105 help = "Set the filename of the encrypted file as metadata",
106 long = "set-metadata-filename",
107 long_help =
108 "Set the filename of the encrypted file as metadata
109
110 Do note, that this metadata is not signed and as such relying on \
111 it - on sender or receiver side - is generally considered \
112 dangerous.",
113 )]
114 pub set_metadata_filename: Option<String>,
115
116 #[command(flatten)]
117 pub signers: CertDesignators<CertUserIDEmailFileSelfArgs,
118 SignerPrefix,
119 SignerOrWithoutSignature,
120 SignerDoc>,
121
122
123 #[command(flatten)]
124 pub signature_notations: crate::cli::types::SignatureNotationsArg,
125
126 #[clap(
127 long = "encrypt-for",
128 value_name = "PURPOSE",
129 default_value_t = EncryptPurpose::Universal,
130 help = "Select what kind of keys are considered for encryption",
131 value_enum,
132 )]
133 pub mode: EncryptPurpose,
134
135 #[clap(
136 long = "compression",
137 value_name = "KIND",
138 default_value_t = CompressionMode::Pad,
139 help = "Select compression scheme to use",
140 value_enum,
141 )]
142 pub compression: CompressionMode,
143 #[clap(
144 long = "use-expired-subkey",
145 help = "Fall back to expired encryption subkeys",
146 long_help = "Fall back to expired encryption subkeys
147
148 If a certificate has only expired \
149 encryption-capable subkeys, fall back \
150 to using the one that expired last",
151 )]
152 pub use_expired_subkey: bool,
153
154 #[clap(
155 long = "profile",
156 value_name = "PROFILE",
157 default_value_t = Default::default(),
158 help = "Select the default OpenPGP standard for the encryption container",
159 long_help = config::augment_help(
160 "key.generate.profile",
161 "Select the default OpenPGP standard for the encryption container
162
163 When encrypting for certificates, the encryption container is selected \
164 based on the stated preferences of the recipients. However, if there \
165 is no guidance, for example because the message is encrypted only with \
166 passwords, sq falls back to this profile.
167
168 As OpenPGP evolves, new versions will become available. This option \
169 selects the version of OpenPGP to use for encrypting messages if the \
170 version can not be inferred otherwise.
171
172 Currently, sq supports two profiles: RFC9580 and RFC4880. Currently, \
173 the default is RFC4880. However, once support for RFC9580 is rolled \
174 out further, the default will change in a future version of sq."),
175 value_enum,
176 )]
177 pub profile: Profile,
178
179 /// Workaround for https://github.com/clap-rs/clap/issues/3846
180 #[clap(skip)]
181 pub profile_source: Option<clap::parser::ValueSource>,
182 }
183
184 /// Documentation for signer arguments.
185 pub struct SignerDoc {}
186 impl AdditionalDocs for SignerDoc {
187 fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr {
188 match arg {
189 "file" =>
190 "Sign the message using the key read from PATH"
191 .into(),
192 _ => {
193 debug_assert!(help.starts_with("Use certificates"));
194 help.replace("Use certificates",
195 "Sign the message using the key")
196 .into()
197 },
198 }
199 }
200 }
201
202 #[derive(ValueEnum, Debug, Clone)]
203 pub enum CompressionMode {
204 None,
205 Pad,
206 Zip,
207 Zlib,
208 Bzip2
209 }