Debian Codegraph @

src/cli/key/rotate.rs

raw
1 use std::path::PathBuf;
2
3 use clap::{ArgGroup, Args};
4
5 use crate::cli::KEY_ROTATE_RETIRE_IN_DURATION;
6 use crate::cli::KEY_ROTATE_RETIRE_IN_IN_DAYS;
7 use crate::cli::KEY_VALIDITY_DURATION;
8 use crate::cli::KEY_VALIDITY_IN_YEARS;
9 use crate::cli::config;
10 use crate::cli::types::ClapData;
11 use crate::cli::types::EncryptPurpose;
12 use crate::cli::types::Expiration;
13 use crate::cli::types::ExpirationArg;
14 use crate::cli::types::FileOrStdout;
15 use crate::cli::types::Profile;
16 use crate::cli::types::expiration::RetireInKind;
17 use crate::cli::types::CertDesignators;
18 use crate::cli::types::cert_designator;
19
20 use crate::cli::examples::*;
21 use crate::cli::key::CipherSuite;
22
23 #[derive(Debug, Args)]
24 #[clap(
25 about = "Rotate a certificate",
26 long_about = format!(
27 "Rotate a certificate
28
29 Generates a new certificate to replace an existing one.
30
31 The new certificate will have the same capabilities as the old \
32 certificate. This can be overridden using the `--can-sign`, \
33 `--cannot-sign`, etc., arguments. Note: the new certificate may have \
34 a different shape from the old certificate. For instance, if the old \
35 certificate's primary key is marked as both certification and signing \
36 capable, the new certificate's primary key will be certification \
37 capable, and it will have a signing subkey.
38
39 By default the certificate expires after {} years. This can be changed \
40 using the `--expiration` argument.
41
42 The new certificate will have the same self-signed user IDs as the old \
43 certificate. Revoked user IDs are ignored.
44
45 The new certificate and the old certificate will cross certify each \
46 other as unconstrained trusted introducers.
47
48 The new certificate will be linked in the same way as the old \
49 certificate. This can be overridden using the `--own-key`, or \
50 the `--shared-key` argument.
51
52 The new certificate will certify the same certificates as the old \
53 certificate. That is, the old certificate's certifications will be \
54 replayed. See `sq pki vouch replay` for more information.
55
56 A revocation certificate indicating that the old certificate is \
57 retired, and that the new certificate should be instead used will be \
58 issued. By default, it will go into effect in {} days. This can be \
59 changed or suppressed using the `--retire-in` argument.
60
61 When using `--output`, the new certificate as well as all of the \
62 other updated certificates are written to the specified file.
63
64 Stable since 1.2.0.
65 ",
66 KEY_VALIDITY_IN_YEARS,
67 KEY_ROTATE_RETIRE_IN_IN_DAYS,
68 ),
69 after_help = ROTATE_EXAMPLES,
70 )]
71 #[clap(group(ArgGroup::new("cap-sign").args(&["can_sign", "cannot_sign"])))]
72 #[clap(group(ArgGroup::new("cap-authenticate").args(&["can_authenticate", "cannot_authenticate"])))]
73 #[clap(group(ArgGroup::new("cap-encrypt").args(&["can_encrypt", "cannot_encrypt"])))]
74 #[clap(mut_arg("expiration", |arg| {
75 arg.default_value(Expiration::from_duration(KEY_VALIDITY_DURATION))
76 }))]
77 #[clap(mut_arg("retire-in", |arg| {
78 arg.default_value(Expiration::from_duration(KEY_ROTATE_RETIRE_IN_DURATION))
79 }))]
80 pub struct Command {
81 #[command(flatten)]
82 pub cert: CertDesignators<cert_designator::CertUserIDEmailFileArgs,
83 cert_designator::CertPrefix,
84 cert_designator::OneValueAndFileRequiresOutput>,
85
86 #[clap(
87 long = "own-key",
88 help = "Mark the key as one's own key",
89 long_help = "Mark the key as one's own key
90
91 The newly generated key with all of its user IDs will be marked as \
92 authenticated and as a fully trusted introducer.",
93 )]
94 pub own_key: bool,
95
96 #[clap(
97 long = "shared-key",
98 help = "Mark the key as a shared key",
99 long_help = "Mark the key as a shared key
100
101 The newly generated key with all of its user IDs will be marked as \
102 authenticated, but not as a trusted introducer. Further, the key \
103 metadata will indicate that this is a shared key.
104
105 Use this option if you plan to share this key with other people. \
106 Normally, you shouldn't share keys material. An example of where you \
107 might want to do this is a shared mailbox."
108 )]
109 pub shared_key: bool,
110
111 #[clap(
112 long = "cipher-suite",
113 value_name = "CIPHER-SUITE",
114 default_value_t = Default::default(),
115 help = "Select the cryptographic algorithms for the key",
116 long_help = config::augment_help(
117 "key.generate.cipher-suite",
118 "Select the cryptographic algorithms for the key"),
119 value_enum,
120 )]
121 pub cipher_suite: CipherSuite,
122
123 /// Workaround for https://github.com/clap-rs/clap/issues/3846
124 #[clap(skip)]
125 pub cipher_suite_source: Option<clap::parser::ValueSource>,
126
127 #[clap(
128 long = "profile",
129 value_name = "PROFILE",
130 default_value_t = Default::default(),
131 help = "Select the OpenPGP standard for the key",
132 long_help = config::augment_help(
133 "key.generate.profile",
134 "Select the OpenPGP standard for the key
135
136 As OpenPGP evolves, new versions will become available. This option \
137 selects the version of OpenPGP to use for the newly generated key.
138
139 Currently, sq supports only one version: RFC4880. Consequently, this \
140 is the default. However, there is already a newer version of the \
141 standard: RFC9580. And, the default will change in a future version of \
142 sq."),
143 value_enum,
144 )]
145 pub profile: Profile,
146
147 /// Workaround for https://github.com/clap-rs/clap/issues/3846
148 #[clap(skip)]
149 pub profile_source: Option<clap::parser::ValueSource>,
150
151 #[clap(
152 long = "new-password-file",
153 value_name = "PASSWORD_FILE",
154 help = "\
155 File containing password to encrypt the secret key material",
156 long_help = "\
157 File containing password to encrypt the secret key material
158
159 Note that the entire key file will be used as the password including \
160 any surrounding whitespace like a trailing newline.",
161 conflicts_with = "without_password",
162 )]
163 pub new_password_file: Option<PathBuf>,
164
165 #[clap(
166 long = "without-password",
167 help = "Don't protect the secret key material with a password",
168 )]
169 pub without_password: bool,
170
171 #[command(flatten)]
172 pub expiration: ExpirationArg,
173
174 #[command(flatten)]
175 pub retire_in: ExpirationArg<RetireInKind>,
176
177 #[clap(
178 long = "can-sign",
179 help ="Add a signing-capable subkey",
180 )]
181 pub can_sign: bool,
182
183 #[clap(
184 long = "cannot-sign",
185 help = "Don't add a signing-capable subkey",
186 )]
187 pub cannot_sign: bool,
188
189 #[clap(
190 long = "can-authenticate",
191 help = "Add an authentication-capable subkey",
192 )]
193 pub can_authenticate: bool,
194
195 #[clap(
196 long = "cannot-authenticate",
197 help = "Don't add an authentication-capable subkey",
198 )]
199 pub cannot_authenticate: bool,
200
201 #[clap(
202 long = "can-encrypt",
203 value_name = "PURPOSE",
204 help = "Add an encryption-capable subkey",
205 long_help = "\
206 Add an encryption-capable subkey
207
208 Encryption-capable subkeys can be marked as suitable for transport \
209 encryption, storage encryption, or both, i.e., universal.",
210 value_enum,
211 )]
212 pub can_encrypt: Option<EncryptPurpose>,
213
214 #[clap(
215 long = "cannot-encrypt",
216 help = "Don't add an encryption-capable subkey",
217 )]
218 pub cannot_encrypt: bool,
219
220 #[clap(
221 long,
222 value_name = FileOrStdout::VALUE_NAME,
223 help = "Write the key to the specified file",
224 long_help = "\
225 Write the key to the specified file
226
227 When not specified, the key is saved on the key store.",
228 requires = "rev_cert",
229 )]
230 pub output: Option<FileOrStdout>,
231
232 #[clap(
233 long = "rev-cert",
234 value_name = "FILE",
235 help = "Write the emergency revocation certificate to FILE",
236 long_help = format!("\
237 Write the emergency revocation certificate to FILE
238
239 When the key is stored on the key store, the revocation certificate is \
240 stored in {} by default.
241
242 When `--output` is specified, the revocation certificate is written to \
243 the file specified by `--rev-cert`.
244
245 If `--output` is `-`, then this option must not also be `-`.",
246 sequoia_directories::Home::default()
247 .map(|home| {
248 let p = home.data_dir(sequoia_directories::Component::Other(
249 "revocation-certificates".into()));
250 let p = p.display().to_string();
251 if let Some(home) = dirs::home_dir() {
252 let home = home.display().to_string();
253 if let Some(rest) = p.strip_prefix(&home) {
254 return format!("$HOME{}", rest);
255 }
256 }
257 p
258 })
259 .unwrap_or("<unknown>".to_string()))
260 )]
261 pub rev_cert: Option<FileOrStdout>
262 }
263
264 const ROTATE_EXAMPLES: Actions = Actions {
265 actions: &[
266 Action::setup().command(&[
267 "sq", "key", "import", "alice-secret.pgp",
268 ]).build(),
269 Action::example().comment("\
270 Rotates Alice's certificate."
271 ).command(&[
272 "sq", "key", "rotate",
273 "--without-password",
274 "--cert", "EB28F26E2739A4870ECC47726F0073F60FD0CBF0",
275 ])
276 .hide(&["--without-password"]).build(),
277 ],
278 };
279 test_examples!(sq_key_rotate, ROTATE_EXAMPLES);