algae_cli/
keys.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::path::PathBuf;

use age::{x25519, Identity, IdentityFile, Recipient};
use clap::Parser;
use miette::{bail, miette, Context as _, IntoDiagnostic as _, Result};
use tokio::fs::read_to_string;

/// [Clap][clap] arguments for keys (public, secret, and identity).
///
/// ```no_run
/// use clap::Parser;
/// use miette::Result;
/// use algae_cli::keys::KeyArgs;
///
/// /// Your CLI tool
/// #[derive(Parser)]
/// struct Args {
///     #[command(flatten)]
///     key: KeyArgs,
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let args = Args::parse();
///     let key = args.key.require_secret_key().await?;
///     // use key somehow...
/// # let _key = key;
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, Parser)]
pub struct KeyArgs {
	/// Path to the key or identity file to use for encrypting/decrypting.
	///
	/// The file can either be:
	/// - an identity file, which contains both a public and secret key, in age format;
	/// - a secret key in Bech32 encoding (usually uppercase);
	/// - when encrypting, a public key in Bech32 encoding (usually lowercase).
	///
	/// When encrypting and provided with a secret key, the corresponding public key will be derived
	/// first; there is no way to encrypt with a secret key such that a file is decodable with the
	/// public key.
	///
	/// There is no support (yet) for password-protected secret key or identity files.
	///
	/// ## Examples
	///
	/// An identity file:
	///
	/// ```identity.txt
	/// # created: 2024-12-20T05:36:10.267871872+00:00
	/// # public key: age1c3jdepjm05aey2dq9dgkfn4utj9a776zwqzqcar3879smuh04ysqttvmyd
	/// AGE-SECRET-KEY-1N84CR29PJTUQA22ALHP4YDL5ZFMXPW5GVETVY3UK58ZD6NPNPDLS4MCZFS
	/// ```
	///
	/// A public key file:
	///
	/// ```key.pub
	/// age1c3jdepjm05aey2dq9dgkfn4utj9a776zwqzqcar3879smuh04ysqttvmyd
	/// ```
	///
	/// A secret key file:
	///
	/// ```key.sec
	/// AGE-SECRET-KEY-1N84CR29PJTUQA22ALHP4YDL5ZFMXPW5GVETVY3UK58ZD6NPNPDLS4MCZFS
	/// ```
	#[cfg_attr(docsrs, doc("\n\n**Flag**: `-k, --key-path PATH`"))]
	#[arg(short, long, verbatim_doc_comment)]
	pub key_path: Option<PathBuf>,

	/// The key to use for encrypting/decrypting as a string.
	///
	/// This supports either public key or secret keys depending on the operation being done.
	/// It does not support the age identity format (with both public and secret keys).
	///
	/// When encrypting and provided with a secret key, the corresponding public key will be derived
	/// first; there is no way to encrypt with a secret key such that a file is decodable with the
	/// public key.
	///
	/// There is no support for password-protected secret keys.
	///
	/// ## Examples
	///
	/// With a public key:
	///
	/// ```console
	/// --key age1c3jdepjm05aey2dq9dgkfn4utj9a776zwqzqcar3879smuh04ysqttvmyd
	/// ```
	///
	/// With a secret key:
	///
	/// ```console
	/// --key AGE-SECRET-KEY-1N84CR29PJTUQA22ALHP4YDL5ZFMXPW5GVETVY3UK58ZD6NPNPDLS4MCZFS
	/// ```
	#[cfg_attr(docsrs, doc("\n\n**Flag**: `-K, --key STRING`"))]
	#[arg(short = 'K', verbatim_doc_comment, conflicts_with = "key_path")]
	pub key: Option<String>,

	#[command(flatten)]
	#[allow(missing_docs, reason = "don't interfere with clap")]
	pub pass: crate::passphrases::PassphraseArgs,
}

impl KeyArgs {
	/// Retrieve the secret key from the arguments, if one was provided.
	///
	/// Returns `None` if neither of `--key-path` or `--key` was given.
	///
	/// Use [`Self::require_secret_key`] instead of dealing with the `None` yourself if you need to
	/// have a mandatory interface.
	pub async fn get_secret_key(&self) -> Result<Option<Box<dyn Identity>>> {
		self.secret_key(false).await
	}

	/// Retrieve the secret key from the arguments, and error if none is available.
	///
	/// Use [`Self::get_secret_key`] instead of parsing the error if you need to have optional keys.
	pub async fn require_secret_key(&self) -> Result<Box<dyn Identity>> {
		self.secret_key(true)
			.await
			.transpose()
			.expect("BUG: when required:true, Some must not be produced")
	}

	/// Retrieve the public key from the arguments, if one was provided.
	///
	/// Returns `None` if neither of `--key-path` or `--key` was given.
	///
	/// Use [`Self::require_public_key`] instead of dealing with the `None` yourself if you need to
	/// have a mandatory interface.
	pub async fn get_public_key(&self) -> Result<Option<Box<dyn Recipient + Send>>> {
		self.public_key(false).await
	}

	/// Retrieve the public key from the arguments, and error if none is available.
	///
	/// Use [`Self::get_public_key`] instead of parsing the error if you need to have optional keys.
	pub async fn require_public_key(&self) -> Result<Box<dyn Recipient + Send>> {
		self.public_key(true)
			.await
			.transpose()
			.expect("BUG: when required:true, Some must not be produced")
	}

	async fn secret_key(&self, required: bool) -> Result<Option<Box<dyn Identity>>> {
		match self {
			Self {
				key_path: None,
				key: None,
				..
			} => {
				if required {
					bail!("one of `--key-path` or `--key` must be provided");
				} else {
					Ok(None)
				}
			}
			Self {
				key_path: Some(_),
				key: Some(_),
				..
			} => {
				bail!("one of `--key-path` or `--key` must be provided, not both");
			}
			Self { key: Some(key), .. } => key
				.parse::<x25519::Identity>()
				.map(|sec| Some(Box::new(sec) as _))
				.map_err(|err| miette!("{err}").wrap_err("parsing secret key")),
			Self {
				key_path: Some(path),
				pass,
				..
			} if path.extension().unwrap_or_default() == "age" => {
				let key = tokio::fs::read(path).await.into_diagnostic()?;
				let pass = pass.require().await?;
				let id = age::decrypt(&pass, &key)
					.into_diagnostic()
					.wrap_err("revealing identity file")?;

				parse_id_as_identity(
					&String::from_utf8(id)
						.into_diagnostic()
						.wrap_err("parsing identity file as UTF-8")?,
				)
				.map(Some)
			}
			Self {
				key_path: Some(path),
				..
			} => {
				let key = read_to_string(&path)
					.await
					.into_diagnostic()
					.wrap_err("reading identity file")?;
				parse_id_as_identity(&key).map(Some)
			}
		}
	}

	async fn public_key(&self, required: bool) -> Result<Option<Box<dyn Recipient + Send>>> {
		match self {
			Self {
				key_path: None,
				key: None,
				..
			} => {
				if required {
					bail!("one of `--key-path` or `--key` must be provided");
				} else {
					Ok(None)
				}
			}
			Self {
				key_path: Some(_),
				key: Some(_),
				..
			} => {
				bail!("one of `--key-path` or `--key` must be provided, not both");
			}
			Self { key: Some(key), .. } if key.starts_with("age") => key
				.parse::<x25519::Recipient>()
				.map(|key| Some(Box::new(key) as _))
				.map_err(|err| miette!("{err}").wrap_err("parsing public key")),
			Self { key: Some(key), .. } if key.starts_with("AGE-SECRET-KEY") => key
				.parse::<x25519::Identity>()
				.map(|sec| Some(Box::new(sec.to_public()) as _))
				.map_err(|err| miette!("{err}").wrap_err("parsing key")),
			Self { key: Some(_), .. } => {
				bail!("value passed to `--key` is not a public or secret age key");
			}
			Self {
				key_path: Some(path),
				pass,
				..
			} if path.extension().unwrap_or_default() == "age" => {
				let key = tokio::fs::read(path).await.into_diagnostic()?;
				let pass = pass.require().await?;
				let id = age::decrypt(&pass, &key)
					.into_diagnostic()
					.wrap_err("revealing identity file")?;

				parse_id_as_recipient(
					&String::from_utf8(id)
						.into_diagnostic()
						.wrap_err("parsing identity file as UTF-8")?,
				)
				.map(Some)
			}
			Self {
				key_path: Some(path),
				..
			} => {
				let key = read_to_string(path)
					.await
					.into_diagnostic()
					.wrap_err("reading identity file")?;
				parse_id_as_recipient(&key).map(Some)
			}
		}
	}
}

fn parse_id_as_identity(id: &str) -> Result<Box<dyn Identity>> {
	if id.starts_with("AGE-SECRET-KEY") {
		id.parse::<x25519::Identity>()
			.map(|sec| Box::new(sec) as _)
			.map_err(|err| miette!("{err}").wrap_err("parsing secret key"))
	} else {
		IdentityFile::from_buffer(id.as_bytes())
			.into_diagnostic()
			.wrap_err("parsing identity")?
			.into_identities()
			.into_diagnostic()
			.wrap_err("parsing keys from identity")?
			.pop()
			.ok_or_else(|| miette!("no identity available"))
	}
}

fn parse_id_as_recipient(id: &str) -> Result<Box<dyn Recipient + Send>> {
	if id.starts_with("age") {
		id.parse::<x25519::Recipient>()
			.map(|key| Box::new(key) as _)
			.map_err(|err| miette!("{err}").wrap_err("parsing public key"))
	} else if id.starts_with("AGE-SECRET-KEY") {
		id.parse::<x25519::Identity>()
			.map(|sec| Box::new(sec.to_public()) as _)
			.map_err(|err| miette!("{err}").wrap_err("parsing secret key"))
	} else {
		IdentityFile::from_buffer(id.as_bytes())
			.into_diagnostic()
			.wrap_err("parsing identity")?
			.to_recipients()
			.into_diagnostic()
			.wrap_err("parsing recipients from identity")?
			.pop()
			.ok_or_else(|| miette!("no recipient available in identity"))
	}
}