| 1 |
use clap::{ValueEnum, ArgGroup, Args, Subcommand}; |
| 2 |
|
| 3 |
use sequoia_openpgp as openpgp; |
| 4 |
use openpgp::packet::UserID; |
| 5 |
use openpgp::types::ReasonForRevocation; |
| 6 |
|
| 7 |
use crate::cli::types::ClapData; |
| 8 |
use crate::cli::types::FileOrCertStore; |
| 9 |
use crate::cli::types::FileOrStdout; |
| 10 |
use crate::cli::types::UserIDDesignators; |
| 11 |
use crate::cli::types::cert_designator::*; |
| 12 |
use crate::cli::types::userid_designator; |
| 13 |
|
| 14 |
use crate::cli::examples; |
| 15 |
use examples::Action; |
| 16 |
use examples::Actions; |
| 17 |
|
| 18 |
#[derive(Debug, Subcommand)] |
| 19 |
#[clap( |
| 20 |
name = "userid", |
| 21 |
about = "Manage User IDs", |
| 22 |
long_about = |
| 23 |
"Manage User IDs |
| 24 |
|
| 25 |
Add User IDs to a key, or revoke them. |
| 26 |
", |
| 27 |
subcommand_required = true, |
| 28 |
arg_required_else_help = true, |
| 29 |
disable_help_subcommand = true, |
| 30 |
)] |
| 31 |
pub enum Command { |
| 32 |
Add(AddCommand), |
| 33 |
Revoke(RevokeCommand), |
| 34 |
} |
| 35 |
|
| 36 |
const USERID_ADD_EXAMPLES: Actions = Actions { |
| 37 |
actions: &[ |
| 38 |
Action::setup().command(&[ |
| 39 |
"sq", "key", "import", "alice-secret.pgp" |
| 40 |
]).build(), |
| 41 |
|
| 42 |
Action::example().comment(" |
| 43 |
Add a new user ID to Alice's key." |
| 44 |
).command(&[ |
| 45 |
"sq", "key", "userid", "add", |
| 46 |
"--cert", "EB28F26E2739A4870ECC47726F0073F60FD0CBF0", |
| 47 |
"--name", "Alice", |
| 48 |
"--email", "[email protected]", |
| 49 |
]).build(), |
| 50 |
] |
| 51 |
}; |
| 52 |
test_examples!(sq_key_userid_add, USERID_ADD_EXAMPLES); |
| 53 |
|
| 54 |
#[derive(Debug, Args)] |
| 55 |
#[clap( |
| 56 |
about = "Add a user ID", |
| 57 |
long_about = |
| 58 |
"Add a user ID |
| 59 |
|
| 60 |
A user ID can contain a name, like `Juliet`, or an email address, like \ |
| 61 |
`<[email protected]>`. Historically, a name and an email address were \ |
| 62 |
usually combined as a single user ID, like `Juliet <[email protected]>`. \ |
| 63 |
However, user IDs that include different information such as name and \ |
| 64 |
email address are more difficult to reason about, so using distinct \ |
| 65 |
user IDs for name and email address is preferred nowadays. |
| 66 |
|
| 67 |
`sq key userid add` respects the reference time set by the top-level \ |
| 68 |
`--time` argument. It sets the creation time of the user ID's \ |
| 69 |
binding signature to the specified time. |
| 70 |
", |
| 71 |
after_help = USERID_ADD_EXAMPLES, |
| 72 |
)] |
| 73 |
#[clap(group(ArgGroup::new("new-userid").args(&["names", "emails", "userid"]).required(true).multiple(true)))] |
| 74 |
pub struct AddCommand { |
| 75 |
#[command(flatten)] |
| 76 |
pub cert: CertDesignators<CertUserIDEmailFileArgs, |
| 77 |
CertPrefix, |
| 78 |
OneValueAndFileRequiresOutput, |
| 79 |
UserIDAddDoc>, |
| 80 |
|
| 81 |
#[clap( |
| 82 |
long = "name", |
| 83 |
value_name = "NAME", |
| 84 |
help = "Add a name as user ID to the key" |
| 85 |
)] |
| 86 |
pub names: Vec<String>, |
| 87 |
|
| 88 |
#[clap( |
| 89 |
long = "email", |
| 90 |
value_name = "ADDRESS", |
| 91 |
help = "Add an email address as user ID to the key" |
| 92 |
)] |
| 93 |
pub emails: Vec<String>, |
| 94 |
|
| 95 |
#[clap( |
| 96 |
long, |
| 97 |
value_name = "USERID", |
| 98 |
help = "Add a user ID to the key", |
| 99 |
long_help = "\ |
| 100 |
Add a user ID to the key |
| 101 |
|
| 102 |
This user ID can combine name and email address, can optionally |
| 103 |
contain a comment, or even be free-form if |
| 104 |
`--allow-non-canonical-userids` is given. However, user IDs that |
| 105 |
include different information such as name and email address are more |
| 106 |
difficult to reason about, so using distinct user IDs for name and |
| 107 |
email address is preferred nowadays. |
| 108 |
|
| 109 |
In doubt, prefer `--name` and `--email`. |
| 110 |
", |
| 111 |
)] |
| 112 |
pub userid: Vec<UserID>, |
| 113 |
|
| 114 |
#[clap( |
| 115 |
long, |
| 116 |
help = "Don't reject user IDs that are not in canonical form", |
| 117 |
long_help = "\ |
| 118 |
Don't reject user IDs that are not in canonical form |
| 119 |
|
| 120 |
Canonical user IDs are of the form `Name (Comment) \ |
| 121 |
<[email protected]>`.", |
| 122 |
)] |
| 123 |
pub allow_non_canonical_userids: bool, |
| 124 |
#[clap( |
| 125 |
long, |
| 126 |
value_name = FileOrCertStore::VALUE_NAME, |
| 127 |
help = "Write to the specified FILE", |
| 128 |
long_help = "\ |
| 129 |
Write to the specified FILE |
| 130 |
|
| 131 |
If not specified, and the certificate was read from the certificate \ |
| 132 |
store, imports the modified certificate into the cert store. If not \ |
| 133 |
specified, and the certificate was read from a file, writes the \ |
| 134 |
modified certificate to stdout.", |
| 135 |
)] |
| 136 |
pub output: Option<FileOrStdout>, |
| 137 |
} |
| 138 |
|
| 139 |
/// Documentation for the cert designators for the key userid add. |
| 140 |
pub struct UserIDAddDoc {} |
| 141 |
|
| 142 |
impl AdditionalDocs for UserIDAddDoc { |
| 143 |
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 144 |
match arg { |
| 145 |
"file" => |
| 146 |
"Add the user ID to the key \ |
| 147 |
read from PATH" |
| 148 |
.into(), |
| 149 |
_ => { |
| 150 |
debug_assert!(help.starts_with("Use certificates")); |
| 151 |
help.replace("Use certificates", |
| 152 |
"Add the user ID to the key") |
| 153 |
.into() |
| 154 |
}, |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
const USERID_REVOKE_EXAMPLES: Actions = Actions { |
| 160 |
actions: &[ |
| 161 |
Action::setup().command(&[ |
| 162 |
"sq", "key", "import", "alice-secret.pgp" |
| 163 |
]).build(), |
| 164 |
|
| 165 |
Action::example().comment("\ |
| 166 |
Retire a user ID on Alice's key." |
| 167 |
).command(&[ |
| 168 |
"sq", "key", "userid", "revoke", |
| 169 |
"--cert", "EB28F26E2739A4870ECC47726F0073F60FD0CBF0", |
| 170 |
"--userid", "Alice <[email protected]>", |
| 171 |
"--reason", "retired", |
| 172 |
"--message", "No longer at example.org.", |
| 173 |
]).build(), |
| 174 |
] |
| 175 |
}; |
| 176 |
test_examples!(sq_key_userid_revoke, USERID_REVOKE_EXAMPLES); |
| 177 |
|
| 178 |
#[derive(Debug, Args)] |
| 179 |
#[clap( |
| 180 |
about = "Revoke a user ID", |
| 181 |
long_about = "\ |
| 182 |
Revoke a user ID |
| 183 |
|
| 184 |
Creates a revocation certificate for a user ID. |
| 185 |
|
| 186 |
If `--revoker` or `--revoker-file` is provided, then that key is used \ |
| 187 |
to create the revocation certificate. If that key is different from \ |
| 188 |
the certificate that is being revoked, this results in a third-party \ |
| 189 |
revocation. This is normally only useful if the owner of the \ |
| 190 |
certificate designated the key to be a designated revoker. |
| 191 |
|
| 192 |
To revoke a user ID, the certificate must be valid under the current \ |
| 193 |
policy. If the certificate is not valid under the current policy, \ |
| 194 |
consider revoking the whole certificate, or fixing it using `sq cert \ |
| 195 |
lint` after verifying the certificate's integrity. If the certificate \ |
| 196 |
is valid under the current policy, but the user ID you want to revoke \ |
| 197 |
isn't, you can still revoke the user ID using `--add-userid`. |
| 198 |
|
| 199 |
`sq key userid revoke` respects the reference time set by the top-level \ |
| 200 |
`--time` argument. When set, it uses the specified time instead of \ |
| 201 |
the current time when determining what keys are valid, and it sets \ |
| 202 |
the revocation certificate's creation time to the reference time \ |
| 203 |
instead of the current time. |
| 204 |
", |
| 205 |
after_help = USERID_REVOKE_EXAMPLES, |
| 206 |
)] |
| 207 |
pub struct RevokeCommand { |
| 208 |
#[command(flatten)] |
| 209 |
pub cert: CertDesignators<CertUserIDEmailFileArgs, |
| 210 |
CertPrefix, |
| 211 |
OneValueAndFileRequiresOutput, |
| 212 |
UserIDRevokeCertDoc>, |
| 213 |
|
| 214 |
#[command(flatten)] |
| 215 |
pub revoker: CertDesignators<CertUserIDEmailFileArgs, |
| 216 |
RevokerPrefix, |
| 217 |
OneOptionalValue, |
| 218 |
UserIDRevokeRevokerDoc>, |
| 219 |
|
| 220 |
#[command(flatten)] |
| 221 |
pub userids: UserIDDesignators< |
| 222 |
userid_designator::ExactByAndAddArgs, |
| 223 |
userid_designator::OneValue>, |
| 224 |
|
| 225 |
#[clap( |
| 226 |
long, |
| 227 |
value_enum, |
| 228 |
value_name = "REASON", |
| 229 |
required = true, |
| 230 |
help = "The reason for the revocation", |
| 231 |
long_help = "\ |
| 232 |
The reason for the revocation |
| 233 |
|
| 234 |
If the reason happened in the past, you should specify that using the \ |
| 235 |
`--time` argument. This allows OpenPGP implementations to more \ |
| 236 |
accurately reason about artifacts whose validity depends on the validity \ |
| 237 |
of the user ID." |
| 238 |
)] |
| 239 |
pub reason: UserIDReasonForRevocation, |
| 240 |
|
| 241 |
#[clap( |
| 242 |
long, |
| 243 |
value_name = "MESSAGE", |
| 244 |
required = true, |
| 245 |
help = "A short, explanatory text", |
| 246 |
long_help = "\ |
| 247 |
A short, explanatory text |
| 248 |
|
| 249 |
The text is shown to a viewer of the revocation certificate, and \ |
| 250 |
explains why the certificate has been revoked. For instance, if Alice \ |
| 251 |
has left the organization, it might say who to contact instead.", |
| 252 |
)] |
| 253 |
pub message: String, |
| 254 |
|
| 255 |
#[command(flatten)] |
| 256 |
pub signature_notations: crate::cli::types::SignatureNotationsArg, |
| 257 |
|
| 258 |
#[clap( |
| 259 |
long, |
| 260 |
value_name = FileOrCertStore::VALUE_NAME, |
| 261 |
help = "Write to the specified FILE", |
| 262 |
long_help = "\ |
| 263 |
Write to the specified FILE |
| 264 |
|
| 265 |
If not specified, and the certificate was read from the certificate \ |
| 266 |
store, imports the modified certificate into the cert store. If not \ |
| 267 |
specified, and the certificate was read from a file, writes the \ |
| 268 |
modified certificate to stdout.", |
| 269 |
)] |
| 270 |
pub output: Option<FileOrStdout>, |
| 271 |
} |
| 272 |
|
| 273 |
/// Documentation for the cert designators for the key userid revoke. |
| 274 |
pub struct UserIDRevokeCertDoc {} |
| 275 |
|
| 276 |
impl AdditionalDocs for UserIDRevokeCertDoc { |
| 277 |
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 278 |
match arg { |
| 279 |
"file" => |
| 280 |
"Revoke the user ID from the key \ |
| 281 |
read from PATH" |
| 282 |
.into(), |
| 283 |
_ => { |
| 284 |
debug_assert!(help.starts_with("Use certificates")); |
| 285 |
help.replace("Use certificates", |
| 286 |
"Revoke the user ID from the key") |
| 287 |
.into() |
| 288 |
}, |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/// Documentation for the revoker designators for the key userid revoke revoker. |
| 294 |
pub struct UserIDRevokeRevokerDoc {} |
| 295 |
|
| 296 |
impl AdditionalDocs for UserIDRevokeRevokerDoc { |
| 297 |
fn help(_: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 298 |
format!("{} to create the revocation certificate", |
| 299 |
help.replace("certificates", "key")).into() |
| 300 |
} |
| 301 |
|
| 302 |
fn long_help(_: &'static str, help: &'static str) -> Option<clap::builder::StyledStr> { |
| 303 |
Some(format!("{} to create the revocation certificate |
| 304 |
|
| 305 |
Sign the revocation certificate using the specified key. By default, \ |
| 306 |
the certificate being revoked is used. Using this option, it is \ |
| 307 |
possible to create a third-party revocation.", |
| 308 |
help.replace("certificates", "key")).into()) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/// The revocation reason for a user ID |
| 313 |
#[derive(ValueEnum, Clone, Debug)] |
| 314 |
pub enum UserIDReasonForRevocation { |
| 315 |
/// The user ID is no longer valid. This is appropriate when |
| 316 |
/// someone leaves an organisation, and the organisation does not |
| 317 |
/// have their secret key material. For instance, if someone was |
| 318 |
/// part of Debian and retires, they would use this to indicate |
| 319 |
/// that a Debian-specific User ID is no longer valid. |
| 320 |
Retired, |
| 321 |
|
| 322 |
/// None of the other reasons apply. OpenPGP implementations |
| 323 |
/// conservatively treat this type of revocation similar to a |
| 324 |
/// compromised key. |
| 325 |
Unspecified |
| 326 |
} |
| 327 |
|
| 328 |
impl From<UserIDReasonForRevocation> for ReasonForRevocation { |
| 329 |
fn from(rr: UserIDReasonForRevocation) -> Self { |
| 330 |
match rr { |
| 331 |
UserIDReasonForRevocation::Retired => ReasonForRevocation::UIDRetired, |
| 332 |
UserIDReasonForRevocation::Unspecified => ReasonForRevocation::Unspecified, |
| 333 |
} |
| 334 |
} |
| 335 |
} |