| 1 |
//! Command-line parser for `sq packet`. |
| 2 |
|
| 3 |
use std::{ |
| 4 |
ffi::OsString, |
| 5 |
path::PathBuf, |
| 6 |
}; |
| 7 |
|
| 8 |
use clap::{Args, Parser, Subcommand}; |
| 9 |
|
| 10 |
use crate::cli::examples::*; |
| 11 |
use crate::cli::types::ArmorKind; |
| 12 |
use crate::cli::types::ClapData; |
| 13 |
use crate::cli::types::FileOrStdin; |
| 14 |
use crate::cli::types::FileOrStdout; |
| 15 |
use crate::cli::types::SessionKey; |
| 16 |
use crate::cli::types::cert_designator::*; |
| 17 |
|
| 18 |
pub mod armor; |
| 19 |
pub mod dearmor; |
| 20 |
|
| 21 |
#[derive(Parser, Debug)] |
| 22 |
#[clap( |
| 23 |
name = "packet", |
| 24 |
about = "Low-level packet manipulation", |
| 25 |
long_about = |
| 26 |
"Low-level packet manipulation |
| 27 |
|
| 28 |
An OpenPGP data stream consists of packets. These tools allow working \ |
| 29 |
with packet streams. They are mostly of interest to developers, but \ |
| 30 |
`sq packet dump` may be helpful to a wider audience both to provide \ |
| 31 |
valuable information in bug reports to OpenPGP-related software, and \ |
| 32 |
as a learning tool. |
| 33 |
", |
| 34 |
subcommand_required = true, |
| 35 |
arg_required_else_help = true, |
| 36 |
disable_help_subcommand = true, |
| 37 |
)] |
| 38 |
pub struct Command { |
| 39 |
#[clap(subcommand)] |
| 40 |
pub subcommand: Subcommands, |
| 41 |
} |
| 42 |
|
| 43 |
#[derive(Debug, Subcommand)] |
| 44 |
pub enum Subcommands { |
| 45 |
Armor(armor::Command), |
| 46 |
Dearmor(dearmor::Command), |
| 47 |
Dump(DumpCommand), |
| 48 |
Decrypt(DecryptCommand), |
| 49 |
Split(SplitCommand), |
| 50 |
Join(JoinCommand), |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Args)] |
| 54 |
#[clap( |
| 55 |
about = "List packets", |
| 56 |
long_about = |
| 57 |
"List packets |
| 58 |
|
| 59 |
Creates a human-readable description of the packet sequence. \ |
| 60 |
Additionally, it can print cryptographic artifacts, and print the raw \ |
| 61 |
octet stream similar to hexdump(1), annotating specifically which \ |
| 62 |
bytes are parsed into OpenPGP values. |
| 63 |
|
| 64 |
If the packet stream includes an encryption container, `sq` will attempt \ |
| 65 |
to decrypt it. |
| 66 |
", |
| 67 |
after_help = DUMP_EXAMPLES, |
| 68 |
)] |
| 69 |
pub struct DumpCommand { |
| 70 |
#[command(flatten)] |
| 71 |
pub cert: CertDesignators<CertUserIDEmailFileArgs, |
| 72 |
CertPrefix, |
| 73 |
OneOptionalValue, |
| 74 |
PacketDumpDoc>, |
| 75 |
|
| 76 |
#[clap( |
| 77 |
default_value_t = FileOrStdin::default(), |
| 78 |
help = FileOrStdin::HELP_OPTIONAL, |
| 79 |
value_name = FileOrStdin::VALUE_NAME, |
| 80 |
conflicts_with_all = ["cert", "cert-userid", "cert-email", "cert-file"], |
| 81 |
)] |
| 82 |
pub input: FileOrStdin, |
| 83 |
|
| 84 |
#[clap( |
| 85 |
default_value_t = FileOrStdout::default(), |
| 86 |
help = FileOrStdout::HELP_OPTIONAL, |
| 87 |
long, |
| 88 |
value_name = FileOrStdout::VALUE_NAME, |
| 89 |
)] |
| 90 |
pub output: FileOrStdout, |
| 91 |
#[clap( |
| 92 |
long = "session-key", |
| 93 |
value_name = "SESSION-KEY", |
| 94 |
help = "Decrypt an encrypted message using SESSION-KEY", |
| 95 |
)] |
| 96 |
pub session_key: Vec<SessionKey>, |
| 97 |
|
| 98 |
#[clap( |
| 99 |
long = "recipient-file", |
| 100 |
value_name = "KEY_FILE", |
| 101 |
help = "Decrypt the message using the key in KEY_FILE", |
| 102 |
)] |
| 103 |
pub recipient_file: Vec<PathBuf>, |
| 104 |
|
| 105 |
#[clap( |
| 106 |
long = "mpis", |
| 107 |
help = "Print cryptographic artifacts", |
| 108 |
)] |
| 109 |
pub mpis: bool, |
| 110 |
#[clap( |
| 111 |
long = "hex", |
| 112 |
help = "Print a hexdump", |
| 113 |
)] |
| 114 |
pub hex: bool, |
| 115 |
} |
| 116 |
|
| 117 |
const DUMP_EXAMPLES: Actions = Actions { |
| 118 |
actions: &[ |
| 119 |
Action::example().comment( |
| 120 |
"Print the packets of a certificate.", |
| 121 |
).command(&[ |
| 122 |
"sq", "packet", "dump", |
| 123 |
"juliet.pgp", |
| 124 |
]).build(), |
| 125 |
|
| 126 |
Action::example().comment( |
| 127 |
"Print the packets including cryptographic artifacts of a certificate.", |
| 128 |
).command(&[ |
| 129 |
"sq", "packet", "dump", |
| 130 |
"--mpis", "juliet.pgp", |
| 131 |
]).build(), |
| 132 |
|
| 133 |
Action::example().comment( |
| 134 |
"Print the packets including a dump of every byte of a certificate.", |
| 135 |
).command(&[ |
| 136 |
"sq", "packet", "dump", |
| 137 |
"--hex", "juliet.pgp", |
| 138 |
]).build(), |
| 139 |
|
| 140 |
Action::example().comment( |
| 141 |
"Prints the packets of an encrypted message, decrypting it using a \ |
| 142 |
secret key file.", |
| 143 |
).command(&[ |
| 144 |
"sq", "packet", "dump", |
| 145 |
"--recipient-file", "bob-secret.pgp", |
| 146 |
"message.pgp", |
| 147 |
]).build(), |
| 148 |
], |
| 149 |
}; |
| 150 |
test_examples!(sq_packet_dump, DUMP_EXAMPLES); |
| 151 |
|
| 152 |
/// Documentation for the cert designators for the packet dump |
| 153 |
/// command. |
| 154 |
pub struct PacketDumpDoc {} |
| 155 |
|
| 156 |
impl AdditionalDocs for PacketDumpDoc { |
| 157 |
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 158 |
match arg { |
| 159 |
"file" => |
| 160 |
"Dump the packets of the cert read from PATH" |
| 161 |
.into(), |
| 162 |
_ => { |
| 163 |
debug_assert!(help.starts_with("Use certificates")); |
| 164 |
help.replace("Use certificates", |
| 165 |
"Dump the packets of the certificate") |
| 166 |
.into() |
| 167 |
}, |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
#[derive(Debug, Args)] |
| 173 |
#[clap( |
| 174 |
about = "Unwrap an encryption container", |
| 175 |
long_about = "Unwrap an encryption container |
| 176 |
|
| 177 |
Decrypts a message, dumping the content of the encryption container \ |
| 178 |
without further processing. The result is a valid OpenPGP message \ |
| 179 |
that can, among other things, be inspected using `sq packet dump`. |
| 180 |
", |
| 181 |
after_help = DECRYPT_EXAMPLES, |
| 182 |
)] |
| 183 |
pub struct DecryptCommand { |
| 184 |
#[clap( |
| 185 |
default_value_t = FileOrStdin::default(), |
| 186 |
help = FileOrStdin::HELP_OPTIONAL, |
| 187 |
value_name = FileOrStdin::VALUE_NAME, |
| 188 |
)] |
| 189 |
pub input: FileOrStdin, |
| 190 |
#[clap( |
| 191 |
default_value_t = FileOrStdout::default(), |
| 192 |
help = FileOrStdout::HELP_OPTIONAL, |
| 193 |
long, |
| 194 |
value_name = FileOrStdout::VALUE_NAME, |
| 195 |
)] |
| 196 |
pub output: FileOrStdout, |
| 197 |
#[clap( |
| 198 |
long, |
| 199 |
help = "Emit binary data", |
| 200 |
)] |
| 201 |
pub binary: bool, |
| 202 |
#[clap( |
| 203 |
long = "recipient-file", |
| 204 |
value_name = "KEY_FILE", |
| 205 |
help = "Decrypt the message using the key in KEY_FILE", |
| 206 |
)] |
| 207 |
pub secret_key_file: Vec<PathBuf>, |
| 208 |
#[clap( |
| 209 |
long = "session-key", |
| 210 |
value_name = "SESSION-KEY", |
| 211 |
help = "Decrypt an encrypted message using SESSION-KEY", |
| 212 |
)] |
| 213 |
pub session_key: Vec<SessionKey>, |
| 214 |
#[clap( |
| 215 |
long = "dump-session-key", |
| 216 |
help = "Print the session key to stderr", |
| 217 |
)] |
| 218 |
pub dump_session_key: bool, |
| 219 |
} |
| 220 |
|
| 221 |
const DECRYPT_EXAMPLES: Actions = Actions { |
| 222 |
actions: &[ |
| 223 |
Action::example().comment( |
| 224 |
"Unwrap the encryption revealing the signed message.", |
| 225 |
).command(&[ |
| 226 |
"sq", "packet", "decrypt", |
| 227 |
"--recipient-file", "bob-secret.pgp", |
| 228 |
"message.pgp", |
| 229 |
]).build(), |
| 230 |
], |
| 231 |
}; |
| 232 |
test_examples!(sq_packet_decrypt, DECRYPT_EXAMPLES); |
| 233 |
|
| 234 |
#[derive(Debug, Args)] |
| 235 |
#[clap( |
| 236 |
about = "Split a message into packets", |
| 237 |
long_about = "Split a message into packets |
| 238 |
|
| 239 |
Splitting a packet sequence into individual packets, then recombining \ |
| 240 |
them freely with `sq packet join` is a great way to experiment with \ |
| 241 |
OpenPGP data. |
| 242 |
|
| 243 |
By default, the packets are written to stdout as a sequence of ASCII \ |
| 244 |
armored blocks. It is possible to edit this file directly (e.g., \ |
| 245 |
moving, adding, or removing packets), and then use `sq packet \ |
| 246 |
join` to assemble the stream. |
| 247 |
|
| 248 |
Alternatively, if a `--output-prefix` is given, the packets are written \ |
| 249 |
into individual files starting with the prefix, and can be reassembled \ |
| 250 |
with `sq packet join`. |
| 251 |
|
| 252 |
The converse operation is `sq packet join`. |
| 253 |
", |
| 254 |
after_help = SPLIT_EXAMPLES, |
| 255 |
)] |
| 256 |
pub struct SplitCommand { |
| 257 |
#[clap( |
| 258 |
default_value_t = FileOrStdin::default(), |
| 259 |
help = FileOrStdin::HELP_OPTIONAL, |
| 260 |
value_name = FileOrStdin::VALUE_NAME, |
| 261 |
)] |
| 262 |
pub input: FileOrStdin, |
| 263 |
|
| 264 |
#[clap( |
| 265 |
help = FileOrStdout::HELP_OPTIONAL, |
| 266 |
long, |
| 267 |
value_name = FileOrStdout::VALUE_NAME, |
| 268 |
)] |
| 269 |
pub output: Option<FileOrStdout>, |
| 270 |
|
| 271 |
#[clap( |
| 272 |
long = "binary", |
| 273 |
requires = "prefix", |
| 274 |
help = "Emit binary data", |
| 275 |
)] |
| 276 |
pub binary: bool, |
| 277 |
|
| 278 |
#[clap( |
| 279 |
long = "output-prefix", |
| 280 |
value_name = "PREFIX", |
| 281 |
help = "Write packets to files starting with PREFIX", |
| 282 |
help = "\ |
| 283 |
Write packets to individual files starting with the given prefix. |
| 284 |
|
| 285 |
The file names are formed by joining the prefix, the path of the \ |
| 286 |
packet in the source object (recall: packets can be nested), and \ |
| 287 |
a human-readable packet type with dashes ('-'). |
| 288 |
", |
| 289 |
)] |
| 290 |
pub prefix: Option<OsString>, |
| 291 |
} |
| 292 |
|
| 293 |
const SPLIT_EXAMPLES: Actions = Actions { |
| 294 |
actions: &[ |
| 295 |
Action::example().comment( |
| 296 |
"Split a certificate into individual packets printed to stdout.", |
| 297 |
).command(&[ |
| 298 |
"sq", "packet", "split", |
| 299 |
"--output=-", |
| 300 |
"juliet.pgp", |
| 301 |
]).build(), |
| 302 |
|
| 303 |
Action::example().comment( |
| 304 |
"Split a inline-signed message into individual packets written to \ |
| 305 |
individual files with the prefix 'packet'.", |
| 306 |
).command(&[ |
| 307 |
"sq", "packet", "split", |
| 308 |
"--output-prefix", "packet", |
| 309 |
"document.pgp", |
| 310 |
]).build(), |
| 311 |
|
| 312 |
Action::example().comment( |
| 313 |
"Then reassemble the message, transforming it into an old-style \ |
| 314 |
signed message with a prefix signature.", |
| 315 |
).command(&[ |
| 316 |
"sq", "packet", "join", |
| 317 |
"--output", "prefix-signature.pgp", |
| 318 |
"--label", "message", |
| 319 |
"packet-2-Signature-Packet", |
| 320 |
"packet-1-Literal-Data-Packet", |
| 321 |
]).build(), |
| 322 |
], |
| 323 |
}; |
| 324 |
test_examples!(sq_packet_split, SPLIT_EXAMPLES); |
| 325 |
|
| 326 |
#[derive(Debug, Args)] |
| 327 |
#[clap( |
| 328 |
about = "Join packets split across files", |
| 329 |
long_about = "Join packets split across files |
| 330 |
|
| 331 |
Splitting a packet sequence into individual packets, then recombining \ |
| 332 |
them freely with `sq packet join` is a great way to experiment with \ |
| 333 |
OpenPGP data. |
| 334 |
|
| 335 |
The converse operation is `sq packet split`. |
| 336 |
", |
| 337 |
after_help = JOIN_EXAMPLES, |
| 338 |
)] |
| 339 |
pub struct JoinCommand { |
| 340 |
#[clap(value_name = "FILE", help = "Read from FILE or stdin if omitted")] |
| 341 |
pub input: Vec<PathBuf>, |
| 342 |
#[clap( |
| 343 |
default_value_t = FileOrStdout::default(), |
| 344 |
help = FileOrStdout::HELP_OPTIONAL, |
| 345 |
long, |
| 346 |
value_name = FileOrStdout::VALUE_NAME, |
| 347 |
)] |
| 348 |
pub output: FileOrStdout, |
| 349 |
#[clap( |
| 350 |
long = "label", |
| 351 |
value_name = "LABEL", |
| 352 |
default_value_t = ArmorKind::Auto, |
| 353 |
conflicts_with = "binary", |
| 354 |
help = "Select the kind of armor header", |
| 355 |
value_enum, |
| 356 |
)] |
| 357 |
pub kind: ArmorKind, |
| 358 |
#[clap( |
| 359 |
long, |
| 360 |
help = "Emit binary data", |
| 361 |
)] |
| 362 |
pub binary: bool, |
| 363 |
} |
| 364 |
|
| 365 |
const JOIN_EXAMPLES: Actions = Actions { |
| 366 |
actions: &[ |
| 367 |
Action::example().comment( |
| 368 |
"Split a inline-signed message into individual packets written to \ |
| 369 |
individual files with the prefix 'packet'.", |
| 370 |
).command(&[ |
| 371 |
"sq", "packet", "split", |
| 372 |
"--output-prefix", "packet", |
| 373 |
"document.pgp", |
| 374 |
]).build(), |
| 375 |
|
| 376 |
Action::example().comment( |
| 377 |
"Then reassemble the message, transforming it into an old-style \ |
| 378 |
signed message with a prefix signature.", |
| 379 |
).command(&[ |
| 380 |
"sq", "packet", "join", |
| 381 |
"--output", "prefix-signature.pgp", |
| 382 |
"--label", "message", |
| 383 |
"packet-2-Signature-Packet", |
| 384 |
"packet-1-Literal-Data-Packet", |
| 385 |
]).build(), |
| 386 |
], |
| 387 |
}; |
| 388 |
test_examples!(sq_packet_join, JOIN_EXAMPLES); |