| 1 |
use std::path::PathBuf; |
| 2 |
|
| 3 |
use clap::{ArgGroup, Args}; |
| 4 |
|
| 5 |
use sequoia_openpgp as openpgp; |
| 6 |
use openpgp::packet::UserID; |
| 7 |
|
| 8 |
use crate::cli::KEY_VALIDITY_DURATION; |
| 9 |
use crate::cli::KEY_VALIDITY_IN_YEARS; |
| 10 |
use crate::cli::config; |
| 11 |
use crate::cli::types::ClapData; |
| 12 |
use crate::cli::types::EncryptPurpose; |
| 13 |
use crate::cli::types::Expiration; |
| 14 |
use crate::cli::types::ExpirationArg; |
| 15 |
use crate::cli::types::FileOrStdout; |
| 16 |
use crate::cli::types::Profile; |
| 17 |
|
| 18 |
use crate::cli::examples::*; |
| 19 |
use crate::cli::key::CipherSuite; |
| 20 |
|
| 21 |
#[derive(Debug, Args)] |
| 22 |
#[clap( |
| 23 |
about = "Generate a new key", |
| 24 |
long_about = format!( |
| 25 |
"Generate a new key |
| 26 |
|
| 27 |
Generating a key is the prerequisite to receiving encrypted messages \ |
| 28 |
and creating signatures. There are a few parameters to this process, \ |
| 29 |
but we provide reasonable defaults for most users. |
| 30 |
|
| 31 |
When generating a key, we also generate an emergency revocation \ |
| 32 |
certificate. This can be used in case the key is lost or compromised. \ |
| 33 |
It is saved alongside the key. This can be changed using the \ |
| 34 |
`--rev-cert` argument. |
| 35 |
|
| 36 |
By default a key expires after {} years. This can be changed using \ |
| 37 |
the `--expiration` argument. |
| 38 |
|
| 39 |
`sq key generate` respects the reference time set by the top-level \ |
| 40 |
`--time` argument. It sets the creation time of the primary key, any \ |
| 41 |
subkeys, and the binding signatures to the reference time. |
| 42 |
", |
| 43 |
KEY_VALIDITY_IN_YEARS, |
| 44 |
), |
| 45 |
after_help = GENERATE_EXAMPLES, |
| 46 |
)] |
| 47 |
#[clap(group(ArgGroup::new("cap-sign").args(&["can_sign", "cannot_sign"])))] |
| 48 |
#[clap(group(ArgGroup::new("cap-authenticate").args(&["can_authenticate", "cannot_authenticate"])))] |
| 49 |
#[clap(group(ArgGroup::new("cap-encrypt").args(&["can_encrypt", "cannot_encrypt"])))] |
| 50 |
#[clap(group(ArgGroup::new("cert-userid").args(&["names", "emails", "userid", "no_userids"]).required(true).multiple(true)))] |
| 51 |
#[clap(group(ArgGroup::new("key-owner") |
| 52 |
.args(&["own_key", "shared_key"]) |
| 53 |
.required(true)))] |
| 54 |
#[clap(mut_arg("expiration", |arg| { |
| 55 |
arg.default_value(Expiration::from_duration(KEY_VALIDITY_DURATION)) |
| 56 |
}))] |
| 57 |
pub struct Command { |
| 58 |
#[clap( |
| 59 |
long = "own-key", |
| 60 |
help = "Mark the key as one's own key", |
| 61 |
long_help = "Mark the key as one's own key |
| 62 |
|
| 63 |
The newly generated key with all of its user IDs will be marked as \ |
| 64 |
authenticated and as a fully trusted introducer.", |
| 65 |
)] |
| 66 |
pub own_key: bool, |
| 67 |
|
| 68 |
#[clap( |
| 69 |
long = "shared-key", |
| 70 |
help = "Mark the key as a shared key", |
| 71 |
long_help = "Mark the key as a shared key |
| 72 |
|
| 73 |
The newly generated key with all of its user IDs will be marked as \ |
| 74 |
authenticated, but not as a trusted introducer. Further, the key \ |
| 75 |
metadata will indicate that this is a shared key. |
| 76 |
|
| 77 |
Use this option if you plan to share this key with other people. \ |
| 78 |
Normally, you shouldn't share keys material. An example of where you \ |
| 79 |
might want to do this is a shared mailbox." |
| 80 |
)] |
| 81 |
pub shared_key: bool, |
| 82 |
|
| 83 |
#[clap( |
| 84 |
long = "name", |
| 85 |
value_name = "NAME", |
| 86 |
help = "Add a name as user ID to the key" |
| 87 |
)] |
| 88 |
pub names: Vec<String>, |
| 89 |
|
| 90 |
#[clap( |
| 91 |
long = "email", |
| 92 |
value_name = "ADDRESS", |
| 93 |
help = "Add an email address as user ID to the key" |
| 94 |
)] |
| 95 |
pub emails: Vec<String>, |
| 96 |
|
| 97 |
#[clap( |
| 98 |
long = "userid", |
| 99 |
value_name = "USERID", |
| 100 |
help = "Add a user ID to the key", |
| 101 |
long_help = "\ |
| 102 |
Add a user ID to the key |
| 103 |
|
| 104 |
This user ID can combine name and email address, can optionally \ |
| 105 |
contain a comment, or even be free-form if \ |
| 106 |
`--allow-non-canonical-userids` is given. However, user IDs that \ |
| 107 |
include different information such as name and email address are more \ |
| 108 |
difficult to reason about, so using distinct user IDs for name and \ |
| 109 |
email address is preferred nowadays. |
| 110 |
|
| 111 |
In doubt, prefer `--name` and `--email`. |
| 112 |
", |
| 113 |
)] |
| 114 |
pub userid: Vec<UserID>, |
| 115 |
|
| 116 |
#[clap( |
| 117 |
long = "allow-non-canonical-userids", |
| 118 |
help = "Don't reject user IDs that are not in canonical form", |
| 119 |
long_help = "\ |
| 120 |
Don't reject user IDs that are not in canonical form |
| 121 |
|
| 122 |
Canonical user IDs are of the form `Name (Comment) \ |
| 123 |
<[email protected]>`.", |
| 124 |
|
| 125 |
)] |
| 126 |
pub allow_non_canonical_userids: bool, |
| 127 |
|
| 128 |
#[clap( |
| 129 |
long = "no-userids", |
| 130 |
help = "Create a key without any user IDs", |
| 131 |
conflicts_with_all = ["names", "emails", "userid"], |
| 132 |
)] |
| 133 |
pub no_userids: bool, |
| 134 |
|
| 135 |
#[clap( |
| 136 |
long = "cipher-suite", |
| 137 |
value_name = "CIPHER-SUITE", |
| 138 |
default_value_t = Default::default(), |
| 139 |
help = "Select the cryptographic algorithms for the key", |
| 140 |
long_help = config::augment_help( |
| 141 |
"key.generate.cipher-suite", |
| 142 |
"Select the cryptographic algorithms for the key"), |
| 143 |
value_enum, |
| 144 |
)] |
| 145 |
pub cipher_suite: CipherSuite, |
| 146 |
|
| 147 |
/// Workaround for https://github.com/clap-rs/clap/issues/3846 |
| 148 |
#[clap(skip)] |
| 149 |
pub cipher_suite_source: Option<clap::parser::ValueSource>, |
| 150 |
|
| 151 |
#[clap( |
| 152 |
long = "profile", |
| 153 |
value_name = "PROFILE", |
| 154 |
default_value_t = Default::default(), |
| 155 |
help = "Select the OpenPGP standard for the key", |
| 156 |
long_help = config::augment_help( |
| 157 |
"key.generate.profile", |
| 158 |
"Select the OpenPGP standard for the key |
| 159 |
|
| 160 |
As OpenPGP evolves, new versions will become available. This option \ |
| 161 |
selects the version of OpenPGP to use for the newly generated key. |
| 162 |
|
| 163 |
Currently, sq supports two profiles: RFC9580 and RFC4880. Currently, \ |
| 164 |
the default is RFC4880. However, once support for RFC9580 is rolled \ |
| 165 |
out further, the default will change in a future version of sq."), |
| 166 |
value_enum, |
| 167 |
)] |
| 168 |
pub profile: Profile, |
| 169 |
|
| 170 |
/// Workaround for https://github.com/clap-rs/clap/issues/3846 |
| 171 |
#[clap(skip)] |
| 172 |
pub profile_source: Option<clap::parser::ValueSource>, |
| 173 |
|
| 174 |
#[clap( |
| 175 |
long = "new-password-file", |
| 176 |
value_name = "PASSWORD_FILE", |
| 177 |
help = "\ |
| 178 |
File containing password to encrypt the secret key material", |
| 179 |
long_help = "\ |
| 180 |
File containing password to encrypt the secret key material |
| 181 |
|
| 182 |
Note that the entire key file will be used as the password including \ |
| 183 |
any surrounding whitespace like a trailing newline.", |
| 184 |
conflicts_with = "without_password", |
| 185 |
)] |
| 186 |
pub new_password_file: Option<PathBuf>, |
| 187 |
|
| 188 |
#[clap( |
| 189 |
long = "without-password", |
| 190 |
help = "Don't protect the secret key material with a password", |
| 191 |
)] |
| 192 |
pub without_password: bool, |
| 193 |
|
| 194 |
#[command(flatten)] |
| 195 |
pub expiration: ExpirationArg, |
| 196 |
|
| 197 |
#[clap( |
| 198 |
long = "can-sign", |
| 199 |
help ="Add a signing-capable subkey (default)", |
| 200 |
)] |
| 201 |
pub can_sign: bool, |
| 202 |
|
| 203 |
#[clap( |
| 204 |
long = "cannot-sign", |
| 205 |
help = "Don't add a signing-capable subkey", |
| 206 |
)] |
| 207 |
pub cannot_sign: bool, |
| 208 |
|
| 209 |
#[clap( |
| 210 |
long = "can-authenticate", |
| 211 |
help = "Add an authentication-capable subkey (default)", |
| 212 |
)] |
| 213 |
pub can_authenticate: bool, |
| 214 |
|
| 215 |
#[clap( |
| 216 |
long = "cannot-authenticate", |
| 217 |
help = "Don't add an authentication-capable subkey", |
| 218 |
)] |
| 219 |
pub cannot_authenticate: bool, |
| 220 |
|
| 221 |
#[clap( |
| 222 |
long = "can-encrypt", |
| 223 |
value_name = "PURPOSE", |
| 224 |
help = "Add an encryption-capable subkey [default: universal]", |
| 225 |
long_help = "\ |
| 226 |
Add an encryption-capable subkey [default: universal] |
| 227 |
|
| 228 |
Encryption-capable subkeys can be marked as suitable for transport \ |
| 229 |
encryption, storage encryption, or both, i.e., universal.", |
| 230 |
value_enum, |
| 231 |
)] |
| 232 |
pub can_encrypt: Option<EncryptPurpose>, |
| 233 |
|
| 234 |
#[clap( |
| 235 |
long = "cannot-encrypt", |
| 236 |
help = "Don't add an encryption-capable subkey", |
| 237 |
)] |
| 238 |
pub cannot_encrypt: bool, |
| 239 |
|
| 240 |
#[clap( |
| 241 |
long, |
| 242 |
value_name = FileOrStdout::VALUE_NAME, |
| 243 |
help = "Write the key to the specified file", |
| 244 |
long_help = "\ |
| 245 |
Write the key to the specified file |
| 246 |
|
| 247 |
When not specified, the key is saved on the key store.", |
| 248 |
requires = "rev_cert", |
| 249 |
)] |
| 250 |
pub output: Option<FileOrStdout>, |
| 251 |
|
| 252 |
#[clap( |
| 253 |
long = "rev-cert", |
| 254 |
value_name = "FILE", |
| 255 |
help = "Write the emergency revocation certificate to FILE", |
| 256 |
long_help = format!("\ |
| 257 |
Write the emergency revocation certificate to FILE |
| 258 |
|
| 259 |
When the key is stored on the key store, the revocation certificate is \ |
| 260 |
stored in {} by default. |
| 261 |
|
| 262 |
When `--output` is specified, the revocation certificate is written to \ |
| 263 |
the file specified by `--rev-cert`. |
| 264 |
|
| 265 |
If `--output` is `-`, then this option must not also be `-`.", |
| 266 |
sequoia_directories::Home::default() |
| 267 |
.map(|home| { |
| 268 |
let p = home.data_dir(sequoia_directories::Component::Other( |
| 269 |
"revocation-certificates".into())); |
| 270 |
let p = p.display().to_string(); |
| 271 |
if let Some(home) = dirs::home_dir() { |
| 272 |
let home = home.display().to_string(); |
| 273 |
if let Some(rest) = p.strip_prefix(&home) { |
| 274 |
return format!("$HOME{}", rest); |
| 275 |
} |
| 276 |
} |
| 277 |
p |
| 278 |
}) |
| 279 |
.unwrap_or("<unknown>".to_string())) |
| 280 |
)] |
| 281 |
pub rev_cert: Option<FileOrStdout> |
| 282 |
} |
| 283 |
|
| 284 |
const GENERATE_EXAMPLES: Actions = Actions { |
| 285 |
actions: &[ |
| 286 |
Action::example().comment("\ |
| 287 |
Generate a key, and save it on the key store." |
| 288 |
).command(&[ |
| 289 |
"sq", "key", "generate", "--own-key", |
| 290 |
"--without-password", |
| 291 |
"--name", "Alice", |
| 292 |
"--email", "[email protected]", |
| 293 |
]) |
| 294 |
.hide(&["--without-password"]).build(), |
| 295 |
|
| 296 |
Action::example().comment("\ |
| 297 |
Generate a key, and save it in a file instead of in the key store." |
| 298 |
).command(&[ |
| 299 |
"sq", "key", "generate", "--own-key", |
| 300 |
"--without-password", |
| 301 |
"--name", "Alice", |
| 302 |
"--email", "[email protected]", |
| 303 |
"--output", "alice-priv.pgp", |
| 304 |
"--rev-cert", "alice-priv.rev", |
| 305 |
]) |
| 306 |
.hide(&["--without-password"]).build(), |
| 307 |
] |
| 308 |
}; |
| 309 |
test_examples!(sq_key_generate, GENERATE_EXAMPLES); |