Debian Codegraph @

src/cli/key/subkey/bind.rs

raw
1 use clap::Args;
2 use clap::ArgGroup;
3
4 use sequoia_openpgp as openpgp;
5 use openpgp::KeyHandle;
6
7 use crate::cli::examples;
8 use examples::Action;
9 use examples::Actions;
10
11 use crate::cli::types::CertDesignators;
12 use crate::cli::types::ClapData;
13 use crate::cli::types::EncryptPurpose;
14 use crate::cli::types::ExpirationArg;
15 use crate::cli::types::FileOrStdout;
16 use crate::cli::types::Time;
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 the specified subkeys to the key read from PATH"
26 .into(),
27 _ => {
28 debug_assert!(help.starts_with("Use certificates"));
29 help.replace("Use certificates",
30 "Add the specified subkeys on the key")
31 .into()
32 },
33 }
34 }
35 }
36
37 #[derive(Debug, Args)]
38 #[clap(
39 name = "bind",
40 about = "Bind keys from one certificate to another",
41 long_about = "\
42 Bind keys from one certificate to another
43
44 This command allows the user to attach a primary key or a subkey \
45 attached to one certificate to another certificate. Say you want to \
46 transition to a new certificate, but have an authentication subkey on \
47 your current certificate that you want to keep because it allows access \
48 a server and updating its configuration is not feasible. This command \
49 makes it easy to attach the subkey to the new certificate.
50
51 After the operation, the key is bound both to the old certificate and to \
52 the new one. To remove secret key material from the old certificate, use \
53 `sq key subkey delete` or `sq key delete`, as appropriate. To revoke the \
54 old subkey or key, use `sq key subkey revoke` or `sq key revoke`, \
55 respectively.
56 ",
57 after_help = EXAMPLES,
58 )]
59 #[clap(group(ArgGroup::new("cap-sign").args(&["can_sign", "cannot_sign"])))]
60 #[clap(group(ArgGroup::new("cap-authenticate").args(&["can_authenticate", "cannot_authenticate"])))]
61 #[clap(group(ArgGroup::new("cap-encrypt").args(&["can_encrypt", "cannot_encrypt"])))]
62 pub struct Command {
63 #[command(flatten)]
64 pub cert: CertDesignators<
65 cert_designator::CertUserIDEmailFileArgs,
66 cert_designator::CertPrefix,
67 cert_designator::OneValueAndFileRequiresOutput,
68 AdditionalDocs>,
69
70 #[clap(
71 long,
72 value_name = "KEY",
73 required(true),
74 help = "Add the key or subkey KEY to the certificate",
75 )]
76 pub key: Vec<KeyHandle>,
77 #[clap(
78 long,
79 allow_hyphen_values = true,
80 value_name = "CREATION_TIME",
81 help = "Make bound subkeys have the specified creation time",
82 long_help = "\
83 Make bound subkeys have the specified creation time
84
85 Normally, the key's creation time is preserved. The exception is if \
86 the key's creation time is the Unix epoch. In that case, the current \
87 time is used.
88
89 This option allows setting the key's creation time to a specified value. \
90 Note: changing a key's creation time also changes its fingerprint. \
91 Changing the fingerprint will make it impossible to look up the key for \
92 the purpose of signature verification, for example.",
93 )]
94 pub creation_time: Option<Time>,
95 #[clap(flatten)]
96 pub expiration: ExpirationArg,
97 #[clap(
98 long,
99 help = "Allow adopting keys from certificates \
100 using broken cryptography",
101 )]
102 pub allow_broken_crypto: bool,
103
104 #[clap(
105 long = "can-sign",
106 help ="Set the signing-capable flag",
107 )]
108 pub can_sign: bool,
109 #[clap(
110 long = "cannot-sign",
111 help = "Don't set the signing-capable flag",
112 )]
113 pub cannot_sign: bool,
114 #[clap(
115 long = "can-authenticate",
116 help = "Set the authentication-capable flag",
117 )]
118 pub can_authenticate: bool,
119 #[clap(
120 long = "cannot-authenticate",
121 help = "Don't set the authentication-capable flag",
122 )]
123 pub cannot_authenticate: bool,
124 #[clap(
125 long = "can-encrypt",
126 value_name = "PURPOSE",
127 help = "Set the encryption-capable flag",
128 long_help = "\
129 Set the encryption-capable flag
130
131 Encryption-capable subkeys can be marked as suitable for transport \
132 encryption, storage encryption, or both, i.e., universal. [default: \
133 universal]",
134 value_enum,
135 )]
136 pub can_encrypt: Option<EncryptPurpose>,
137 #[clap(
138 long = "cannot-encrypt",
139 help = "Don't set the encryption-capable flag",
140 )]
141 pub cannot_encrypt: bool,
142
143 #[clap(
144 long,
145 value_name = FileOrStdout::VALUE_NAME,
146 help = "Write to the specified FILE",
147 long_help = "\
148 Write to the specified FILE
149
150 If not specified, and the certificate was read from the certificate \
151 store, imports the modified certificate into the cert store. If not \
152 specified, and the certificate was read from a file, writes the \
153 modified certificate to stdout.",
154 )]
155 pub output: Option<FileOrStdout>,
156 }
157
158 const EXAMPLES: Actions = Actions {
159 actions: &[
160
161 Action::setup().command(&[
162 "sq", "key", "import",
163 "alice-secret.pgp", "alice-new-secret.pgp",
164 ]).build(),
165
166 Action::example().comment("\
167 Bind Alice's old authentication subkey to Alice's new certificate."
168 ).command(&[
169 "sq", "key", "subkey", "bind",
170 "--cert=C5999E8191BF7B503653BE958B1F7910D01F86E5",
171 "--key=0D45C6A756A038670FDFD85CB1C82E8D27DB23A1",
172 ]).build(),
173
174 Action::example().comment("\
175 Bind a bare key to Alice's certificate. A bare key is a public key \
176 without any components or signatures. This simplifies working with raw \
177 keys, e.g., keys generated on an OpenPGP card, a TPM device, etc."
178 ).command(&[
179 "sq", "key", "subkey", "bind",
180 "--keyring=bare.pgp",
181 "--cert=C5999E8191BF7B503653BE958B1F7910D01F86E5",
182 "--key=B321BA8F650CB16443E06826DBFA98A78CF6562F",
183 "--can-encrypt=universal",
184 ]).build(),
185 ]
186 };
187 test_examples!(sq_key_bind, EXAMPLES);