| 1 |
use clap::{Args, Parser, Subcommand}; |
| 2 |
|
| 3 |
use crate::cli::config; |
| 4 |
use crate::cli::examples::*; |
| 5 |
use crate::cli::types::ClapData; |
| 6 |
use crate::cli::types::FileOrCertStore; |
| 7 |
use crate::cli::types::FileOrStdout; |
| 8 |
use crate::cli::types::cert_designator::*; |
| 9 |
|
| 10 |
/// The default key servers to query. |
| 11 |
pub const DEFAULT_KEYSERVERS: &[&'static str] = &[ |
| 12 |
"hkps://keys.openpgp.org", |
| 13 |
"hkps://mail-api.proton.me", |
| 14 |
"hkps://keys.mailvelope.com", |
| 15 |
"hkps://keyserver.ubuntu.com", |
| 16 |
"hkps://sks.pod01.fleetstreetops.com", |
| 17 |
]; |
| 18 |
|
| 19 |
#[derive(Parser, Debug)] |
| 20 |
#[clap( |
| 21 |
name = "keyserver", |
| 22 |
about = "Retrieve and publishes certificates via key servers", |
| 23 |
long_about = |
| 24 |
"Retrieve and publishes certificates via key servers |
| 25 |
|
| 26 |
The OpenPGP HTTP Keyserver Protocol (HKP) is a method for publishing \ |
| 27 |
and retrieving certificates from key servers. |
| 28 |
", |
| 29 |
subcommand_required = true, |
| 30 |
arg_required_else_help = true, |
| 31 |
disable_help_subcommand = true, |
| 32 |
)] |
| 33 |
pub struct Command { |
| 34 |
#[clap( |
| 35 |
long = "server", |
| 36 |
default_values_t = DEFAULT_KEYSERVERS.iter().map(ToString::to_string), |
| 37 |
global = true, |
| 38 |
// All global options should have a high display_order, so |
| 39 |
// that they are sorted to the bottom. |
| 40 |
display_order = 800, |
| 41 |
value_name = "URI", |
| 42 |
help = "Set a key server to use (can be given multiple times)", |
| 43 |
long_help = config::augment_help( |
| 44 |
"network.keyserver.servers", |
| 45 |
"Set a key server to use (can be given multiple times)"), |
| 46 |
)] |
| 47 |
pub servers: Vec<String>, |
| 48 |
|
| 49 |
#[clap(subcommand)] |
| 50 |
pub subcommand: Subcommands, |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Subcommand)] |
| 54 |
pub enum Subcommands { |
| 55 |
Search(SearchCommand), |
| 56 |
Publish(PublishCommand), |
| 57 |
} |
| 58 |
|
| 59 |
#[derive(Debug, Args)] |
| 60 |
#[clap( |
| 61 |
about = "Retrieve certificates from key servers", |
| 62 |
long_about = |
| 63 |
"Retrieve certificates from key servers |
| 64 |
|
| 65 |
By default, any returned certificates are stored in the local \ |
| 66 |
certificate store. This can be overridden by using `--output` \ |
| 67 |
option. |
| 68 |
|
| 69 |
When a certificate is retrieved from a verifying key server (currently, \ |
| 70 |
this is limited to a list of known servers: `hkps://keys.openpgp.org`, \ |
| 71 |
`hkps://keys.mailvelope.com`, and `hkps://mail-api.proton.me`), and \ |
| 72 |
imported into the local certificate store, the User IDs are also \ |
| 73 |
certificated with a local server-specific key. That proxy certificate \ |
| 74 |
is in turn certified as a minimally trusted CA (trust amount: 1 of \ |
| 75 |
120) by the local trust root. How much a proxy key server CA is \ |
| 76 |
trusted can be tuned using `sq pki link add` or `sq pki link retract` in \ |
| 77 |
the usual way. |
| 78 |
", |
| 79 |
after_help = SEARCH_EXAMPLES, |
| 80 |
)] |
| 81 |
pub struct SearchCommand { |
| 82 |
#[clap( |
| 83 |
help = FileOrCertStore::HELP_OPTIONAL, |
| 84 |
long, |
| 85 |
value_name = FileOrCertStore::VALUE_NAME, |
| 86 |
)] |
| 87 |
pub output: Option<FileOrStdout>, |
| 88 |
|
| 89 |
#[clap( |
| 90 |
long, |
| 91 |
conflicts_with = "query", |
| 92 |
help = "Fetch updates for all known certificates", |
| 93 |
)] |
| 94 |
pub all: bool, |
| 95 |
|
| 96 |
#[clap( |
| 97 |
value_name = "QUERY", |
| 98 |
required = true, |
| 99 |
help = "Retrieve certificate(s) using QUERY", |
| 100 |
long_help = "Retrieve certificate(s) using QUERY |
| 101 |
|
| 102 |
This may be a fingerprint, a KeyID, \ |
| 103 |
or an email address.", |
| 104 |
)] |
| 105 |
pub query: Vec<String>, |
| 106 |
} |
| 107 |
|
| 108 |
const SEARCH_EXAMPLES: Actions = Actions { |
| 109 |
actions: &[ |
| 110 |
Action::example().comment( |
| 111 |
"Retrieve Alice's certificate from the default keyservers." |
| 112 |
).command(&[ |
| 113 |
"sq", "network", "keyserver", "search", "[email protected]", |
| 114 |
]).syntax_check(), |
| 115 |
|
| 116 |
Action::example().comment( |
| 117 |
"Retrieve Alice's certificate addressed by fingerprint \ |
| 118 |
from the default keyservers." |
| 119 |
).command(&[ |
| 120 |
"sq", "network", "keyserver", "search", |
| 121 |
"EB28F26E2739A4870ECC47726F0073F60FD0CBF0", |
| 122 |
]).syntax_check(), |
| 123 |
|
| 124 |
Action::example().comment( |
| 125 |
"Retrieve Alice's certificate from a non-default keyserver." |
| 126 |
).command(&[ |
| 127 |
"sq", "network", "keyserver", "search", |
| 128 |
"--server=hkps://keys.example.org", |
| 129 |
"[email protected]", |
| 130 |
]).syntax_check(), |
| 131 |
|
| 132 |
Action::example().comment( |
| 133 |
"Retrieve updates for all known certificates from the default \ |
| 134 |
keyservers." |
| 135 |
).command(&[ |
| 136 |
"sq", "network", "keyserver", "search", "--all", |
| 137 |
]).syntax_check(), |
| 138 |
], |
| 139 |
}; |
| 140 |
test_examples!(sq_network_keyserver_search, SEARCH_EXAMPLES); |
| 141 |
|
| 142 |
#[derive(Debug, Args)] |
| 143 |
#[clap( |
| 144 |
about = "Publish certificates on key servers", |
| 145 |
long_about = format!( |
| 146 |
"Publish certificates on key servers |
| 147 |
|
| 148 |
Sends certificates to the configured key servers for publication. By \ |
| 149 |
default, the certificates are sent to {}. This can be tweaked using \ |
| 150 |
`--servers`. |
| 151 |
", |
| 152 |
join(DEFAULT_KEYSERVERS.iter().cloned())), |
| 153 |
after_help = PUBLISH_EXAMPLES, |
| 154 |
)] |
| 155 |
pub struct PublishCommand { |
| 156 |
#[command(flatten)] |
| 157 |
pub certs: CertDesignators<FileCertUserIDEmailDomainArgs, |
| 158 |
CertPrefix>, |
| 159 |
} |
| 160 |
|
| 161 |
const PUBLISH_EXAMPLES: Actions = Actions { |
| 162 |
actions: &[ |
| 163 |
Action::example().comment( |
| 164 |
"Publish Alice's certificate on the default keyservers." |
| 165 |
).command(&[ |
| 166 |
"sq", "network", "keyserver", "publish", |
| 167 |
"[email protected]", |
| 168 |
]).syntax_check(), |
| 169 |
], |
| 170 |
}; |
| 171 |
test_examples!(sq_network_keyserver_publish, PUBLISH_EXAMPLES); |
| 172 |
|
| 173 |
/// Joins the given key server URLs into a list. |
| 174 |
fn join<'a>(i: impl Iterator<Item = &'a str>) -> String { |
| 175 |
let mut i = i.map(|u| format!("`{}`", u)).collect::<Vec<_>>(); |
| 176 |
match i.len() { |
| 177 |
0 => panic!("empty list"), |
| 178 |
1 => i.pop().unwrap(), |
| 179 |
2 => format!("{} and {}", i[0], i[1]), |
| 180 |
n => format!("{}, and {}", i[..n-1].join(", "), i[n-1]), |
| 181 |
} |
| 182 |
} |