Debian Codegraph @

src/cli/sign.rs

raw
1 //! Command-line parser for `sq sign`.
2
3 use std::path::PathBuf;
4
5 use clap::{ArgGroup, Parser, ValueEnum};
6
7 use sequoia_openpgp::{
8 types::SignatureType,
9 };
10
11 use super::types::ClapData;
12 use super::types::FileOrStdin;
13 use super::types::FileOrStdout;
14
15 use crate::cli::examples;
16 use examples::*;
17 use crate::cli::types::cert_designator::*;
18
19 /// Key for the help augmentation.
20 pub const SIGNER_SELF: &str = "sign.signer-self";
21
22 const SIGN_EXAMPLES: Actions = Actions {
23 actions: &[
24 Action::example().comment(
25 "Create a signed message.",
26 ).command(&[
27 "sq", "sign", "--signer-file", "juliet-secret.pgp",
28 "--message",
29 "document.txt",
30 ]).build(),
31
32 Action::example().comment(
33 "Create a detached signature.",
34 ).command(&[
35 "sq", "sign", "--signer-file", "juliet-secret.pgp",
36 "--signature-file=document.txt.sig", "document.txt",
37 ]).build(),
38 ],
39 };
40 test_examples!(sq_sign, SIGN_EXAMPLES);
41
42 #[derive(Parser, Debug)]
43 #[clap(
44 name = "sign",
45 about = "Sign messages or data files",
46 long_about =
47 "Sign messages or data files
48
49 Creates signed messages or detached signatures. Detached signatures \
50 are often used to sign software packages.
51
52 The converse operation is `sq verify`.
53
54 `sq sign` respects the reference time set by the top-level \
55 `--time` argument. When set, it uses the specified time instead of \
56 the current time, when determining what keys are valid, and it sets \
57 the signature's creation time to the reference time instead of the \
58 current time.
59 ",
60 after_help = SIGN_EXAMPLES,
61 )]
62 #[clap(group(ArgGroup::new("kind")
63 .args(&["detached", "message", "cleartext"]).required(true)))]
64 pub struct Command {
65 #[clap(
66 default_value_t = FileOrStdin::default(),
67 help = FileOrStdin::HELP_OPTIONAL,
68 value_name = FileOrStdin::VALUE_NAME,
69 )]
70 pub input: FileOrStdin,
71 #[clap(
72 default_value_t = FileOrStdout::default(),
73 help = FileOrStdout::HELP_OPTIONAL,
74 long,
75 value_name = FileOrStdout::VALUE_NAME,
76 )]
77 pub output: FileOrStdout,
78 #[clap(
79 long,
80 help = "Emit binary data",
81 )]
82 pub binary: bool,
83
84 #[clap(
85 long = "signature-file",
86 value_name = "SIG",
87 help = "Create a detached signature file",
88 conflicts_with = "output",
89 )]
90 pub detached: Option<FileOrStdout>,
91
92 #[clap(
93 long = "message",
94 help = "Create an inline-signed message",
95 )]
96 pub message: bool,
97
98 #[clap(
99 long = "cleartext",
100 help = "Create a cleartext-signed message",
101 conflicts_with_all = &[
102 "append",
103 "notarize",
104 "binary",
105 ],
106 )]
107 pub cleartext: bool,
108
109 #[clap(
110 long = "mode",
111 default_value = "binary",
112 conflicts_with = "cleartext",
113 help = "Select the signature mode",
114 long_help = "Select the signature mode
115
116 Signatures can be made in binary mode or in text mode. \
117 Text mode normalizes line endings, which makes signatures \
118 more robust when a text is transported over a channel which \
119 may change line endings. In doubt, create binary signatures.",
120 )]
121 pub mode: Mode,
122
123 #[clap(
124 long,
125 conflicts_with = "notarize",
126 help = "Append a signature to existing signature",
127 )]
128 pub append: bool,
129 #[clap(
130 hide = true,
131 long,
132 conflicts_with = "append",
133 help = "Sign a message and all existing signatures",
134 )]
135 pub notarize: bool,
136 #[clap(
137 long,
138 value_name = "SIGNED-MESSAGE",
139 conflicts_with_all = &[
140 "append",
141 "detached",
142 "cleartext",
143 "notarize",
144 "signer",
145 "signer-file",
146 "signer-email",
147 "signer-userid",
148 ],
149 help = "Merge signatures from the input and SIGNED-MESSAGE",
150 )]
151 pub merge: Option<PathBuf>,
152
153 #[command(flatten)]
154 pub signers: CertDesignators<CertUserIDEmailFileSelfArgs,
155 SignerPrefix,
156 // XXX: should be NoOptions, but we
157 // cannot express that one cert
158 // designator must be given unless
159 // merge is given.
160 OptionalValue,
161 SignerDoc>,
162
163 #[command(flatten)]
164 pub signature_notations: crate::cli::types::SignatureNotationsArg,
165 }
166
167 /// Documentation for signer arguments.
168 pub struct SignerDoc {}
169 impl AdditionalDocs for SignerDoc {
170 fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr {
171 match arg {
172 "file" =>
173 "Create the signature using the key read from PATH"
174 .into(),
175 _ => {
176 debug_assert!(help.starts_with("Use certificates"));
177 help.replace("Use certificates",
178 "Create the signature using the key")
179 .into()
180 },
181 }
182 }
183 }
184
185 /// Signature mode, either binary or text.
186 #[derive(ValueEnum, Clone, Copy, Debug, Default)]
187 pub enum Mode {
188 /// Create binary signatures.
189 #[default]
190 Binary,
191
192 /// Create text signatures.
193 Text,
194 }
195
196 impl From<Mode> for SignatureType {
197 fn from(m: Mode) -> Self {
198 match m {
199 Mode::Binary => SignatureType::Binary,
200 Mode::Text => SignatureType::Text,
201 }
202 }
203 }