Debian Codegraph @

src/cli/key/subkey/add.rs

raw
1 use std::path::PathBuf;
2
3 use clap::Args;
4 use clap::ArgGroup;
5
6 use crate::cli::examples;
7 use examples::Action;
8 use examples::Actions;
9
10 use crate::cli::config;
11 use crate::cli::key::CipherSuite;
12 use crate::cli::types::CertDesignators;
13 use crate::cli::types::ClapData;
14 use crate::cli::types::EncryptPurpose;
15 use crate::cli::types::ExpirationArg;
16 use crate::cli::types::FileOrStdout;
17 use crate::cli::types::cert_designator;
18
19 pub struct AdditionalDocs {}
20
21 impl cert_designator::AdditionalDocs for AdditionalDocs {
22 fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr {
23 match arg {
24 "file" =>
25 "Add a subkey to the key read from PATH"
26 .into(),
27 _ => {
28 debug_assert!(help.starts_with("Use certificates"));
29 help.replace("Use certificates",
30 "Add a subkey to the key")
31 .into()
32 },
33 }
34 }
35 }
36
37 #[derive(Debug, Args)]
38 #[clap(
39 about = "Add a new subkey to a certificate",
40 long_about = "\
41 Add a new subkey to a certificate
42
43 A subkey has one or more capabilities.
44
45 `--can-sign` sets the signing capability, and means that the key may \
46 be used for signing. `--can-authenticate` sets the authentication \
47 capability, and means that the key may be used for authentication \
48 (e.g., as an SSH key). `--can-certify` sets the certificate \
49 capability, and means that the key may be used to make third-party \
50 certifications. These capabilities may be combined.
51
52 `--can-encrypt=storage` sets the storage encryption capability, and \
53 means that the key may be used for storage \
54 encryption. `--can-encrypt=transport` sets the transport encryption \
55 capability, and means that the key may be used for transport \
56 encryption. `--can-encrypt=universal` sets both the storage and the \
57 transport encryption capability, and means that the key may be used \
58 for both storage and transport encryption. The encryption \
59 capabilities must not be combined with the signing or authentication \
60 capability.
61
62 Normally, `sq` prompts the user for a password to use to encrypt the \
63 secret key material. The password for the new subkey may be different \
64 from the other keys. When using `--without-password`, `sq` doesn't \
65 prompt for a password, and doesn't password-protect the subkey.
66
67 By default a new subkey doesn't expire on its own. However, its \
68 validity period is limited by that of the certificate. Using the \
69 `--expiration` argument allows setting a different expiration time.
70
71 `sq key subkey add` respects the reference time set by the top-level \
72 `--time` argument. It sets the creation time of the subkey to the specified \
73 time.
74 ",
75 after_help = EXAMPLES,
76 )]
77 #[clap(group(ArgGroup::new("authentication-group").args(&["can_authenticate", "can_encrypt"])))]
78 #[clap(group(ArgGroup::new("sign-group").args(&["can_sign", "can_encrypt"])))]
79 #[clap(group(ArgGroup::new("required-group").args(&["can_authenticate", "can_sign", "can_encrypt"]).required(true)))]
80 pub struct Command {
81 #[command(flatten)]
82 pub cert: CertDesignators<
83 cert_designator::CertUserIDEmailFileArgs,
84 cert_designator::CertPrefix,
85 cert_designator::OneValueAndFileRequiresOutput,
86 AdditionalDocs>,
87
88 #[clap(
89 long,
90 value_name = "CIPHER-SUITE",
91 default_value_t = CipherSuite::Cv25519,
92 help = "Select the cryptographic algorithms for the subkey",
93 long_help = config::augment_help(
94 "key.generate.cipher-suite",
95 "Select the cryptographic algorithms for the subkey"),
96 value_enum,
97 )]
98 pub cipher_suite: CipherSuite,
99
100 /// Workaround for https://github.com/clap-rs/clap/issues/3846
101 #[clap(skip)]
102 pub cipher_suite_source: Option<clap::parser::ValueSource>,
103
104 #[command(flatten)]
105 pub expiration: ExpirationArg,
106
107 #[clap(
108 long,
109 help = "Add a signing-capable subkey",
110 )]
111 pub can_sign: bool,
112 #[clap(
113 long,
114 help = "Add an authentication-capable subkey",
115 )]
116 pub can_authenticate: bool,
117 #[clap(
118 long,
119 value_name = "PURPOSE",
120 help = "Add an encryption-capable subkey [default: universal]",
121 long_help = "\
122 Add an encryption-capable subkey [default: universal]
123
124 Encryption-capable subkeys can be marked as suitable for transport \
125 encryption, storage encryption, or both, i.e., universal.",
126 value_enum,
127 )]
128 pub can_encrypt: Option<EncryptPurpose>,
129
130 #[clap(
131 long = "new-password-file",
132 value_name = "PASSWORD_FILE",
133 help = "\
134 File containing password to encrypt the secret key material",
135 long_help = "\
136 File containing password to encrypt the secret key material
137
138 Note that the entire key file will be used as the password including \
139 any surrounding whitespace like a trailing newline.",
140 conflicts_with = "without_password",
141 )]
142 pub new_password_file: Option<PathBuf>,
143
144 #[clap(
145 long,
146 help = "Don't protect the subkey's secret key material with a password",
147 )]
148 pub without_password: bool,
149
150 #[clap(
151 long,
152 value_name = FileOrStdout::VALUE_NAME,
153 help = "Write to the specified FILE",
154 long_help = "\
155 Write to the specified FILE
156
157 If not specified, and the certificate was read from the certificate \
158 store, imports the modified certificate into the key store. If not \
159 specified, and the certificate was read from a file, writes the \
160 modified certificate to stdout.",
161 )]
162 pub output: Option<FileOrStdout>,
163 }
164
165 const EXAMPLES: Actions = Actions {
166 actions: &[
167 Action::setup().command(&[
168 "sq", "key", "import", "alice-secret.pgp",
169 ]).build(),
170
171 Action::example().comment("\
172 Add a new signing-capable subkey to Alice's key."
173 ).command(&[
174 "sq", "key", "subkey", "add",
175 "--without-password",
176 "--can-sign",
177 "--cert=EB28F26E2739A4870ECC47726F0073F60FD0CBF0",
178 ])
179 .hide(&["--without-password"]).build(),
180 ]
181 };
182 test_examples!(sq_key_subkey_add, EXAMPLES);