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
//! This crate provides an API for building age plugins.
//!
//! # Introduction
//!
//! The [age file encryption format] follows the "one well-oiled joint" design philosophy.
//! The mechanism for extensibility (within a particular format version) is the recipient
//! stanzas within the age header: file keys can be wrapped in any number of ways, and age
//! clients are required to ignore stanzas that they do not understand.
//!
//! The core APIs that exercise this mechanism are:
//! - A recipient that wraps a file key and returns a stanza.
//! - An identity that unwraps a stanza and returns a file key.
//!
//! The age plugin system provides a mechanism for exposing these core APIs across process
//! boundaries. It has two main components:
//!
//! - A map from recipients and identities to plugin binaries.
//! - State machines for wrapping and unwrapping file keys.
//!
//! With this composable design, you can implement a recipient or identity that you might
//! use directly with the [`age`] library crate, and also deploy it as a plugin binary for
//! use with clients like [`rage`].
//!
//! [age file encryption format]: https://age-encryption.org/v1
//! [`age`]: https://crates.io/crates/age
//! [`rage`]: https://crates.io/crates/rage
//!
//! # Mapping recipients and identities to plugin binaries
//!
//! age plugins are identified by an arbitrary case-insensitive string `NAME`. This string
//! is used in three places:
//!
//! - Plugin-compatible recipients are encoded using Bech32 with the HRP `age1name`
//! (lowercase).
//! - Plugin-compatible identities are encoded using Bech32 with the HRP
//! `AGE-PLUGIN-NAME-` (uppercase).
//! - Plugin binaries (to be started by age clients) are named `age-plugin-name`.
//!
//! Users interact with age clients by providing either recipients for file encryption, or
//! identities for file decryption. When a plugin recipient or identity is provided, the
//! age client searches the `PATH` for a binary with the corresponding plugin name.
//!
//! Recipient stanza types are not required to be correlated to specific plugin names.
//! When decrypting, age clients will pass all recipient stanzas to every connected
//! plugin. Plugins MUST ignore stanzas that they do not know about.
//!
//! A plugin binary may handle multiple recipient or identity types by being present in
//! the `PATH` under multiple names. This can be implemented with symlinks or aliases to
//! the canonical binary.
//!
//! Multiple plugin binaries can support the same recipient and identity types; the first
//! binary found in the `PATH` will be used by age clients. Some Unix OSs support
//! "alternatives", which plugin binaries should leverage if they provide support for a
//! common recipient or identity type.
//!
//! Note that the identity specified by a user doesn't need to point to a specific
//! decryption key, or indeed contain any key material at all. It only needs to contain
//! sufficient information for the plugin to locate the necessary key material.
//!
//! ## Standard age keys
//!
//! A plugin MAY support decrypting files encrypted to native age recipients, by including
//! support for the `x25519` recipient stanza. Such plugins will pick their own name, and
//! users will use identity files containing identities that specify that plugin name.
//!
//! # Example plugin binary
//!
//! The following example uses `gumdrop` to parse CLI arguments, but any argument parsing
//! logic will work as long as it can detect the `--age-plugin=STATE_MACHINE` flag.
//!
//! ```
//! use age_core::format::{FileKey, Stanza};
//! use age_plugin::{
//! identity::{self, IdentityPluginV1},
//! print_new_identity,
//! recipient::{self, RecipientPluginV1},
//! Callbacks, run_state_machine,
//! };
//! use gumdrop::Options;
//! use std::collections::HashMap;
//! use std::io;
//!
//! struct RecipientPlugin;
//!
//! impl RecipientPluginV1 for RecipientPlugin {
//! fn add_recipient(
//! &mut self,
//! index: usize,
//! plugin_name: &str,
//! bytes: &[u8],
//! ) -> Result<(), recipient::Error> {
//! todo!()
//! }
//!
//! fn add_identity(
//! &mut self,
//! index: usize,
//! plugin_name: &str,
//! bytes: &[u8]
//! ) -> Result<(), recipient::Error> {
//! todo!()
//! }
//!
//! fn wrap_file_keys(
//! &mut self,
//! file_keys: Vec<FileKey>,
//! mut callbacks: impl Callbacks<recipient::Error>,
//! ) -> io::Result<Result<Vec<Vec<Stanza>>, Vec<recipient::Error>>> {
//! todo!()
//! }
//! }
//!
//! struct IdentityPlugin;
//!
//! impl IdentityPluginV1 for IdentityPlugin {
//! fn add_identity(
//! &mut self,
//! index: usize,
//! plugin_name: &str,
//! bytes: &[u8]
//! ) -> Result<(), identity::Error> {
//! todo!()
//! }
//!
//! fn unwrap_file_keys(
//! &mut self,
//! files: Vec<Vec<Stanza>>,
//! mut callbacks: impl Callbacks<identity::Error>,
//! ) -> io::Result<HashMap<usize, Result<FileKey, Vec<identity::Error>>>> {
//! todo!()
//! }
//! }
//!
//! #[derive(Debug, Options)]
//! struct PluginOptions {
//! #[options(help = "print help message")]
//! help: bool,
//!
//! #[options(help = "run the given age plugin state machine", no_short)]
//! age_plugin: Option<String>,
//! }
//!
//! fn main() -> io::Result<()> {
//! let opts = PluginOptions::parse_args_default_or_exit();
//!
//! if let Some(state_machine) = opts.age_plugin {
//! // The plugin was started by an age client; run the state machine.
//! run_state_machine(
//! &state_machine,
//! || RecipientPlugin,
//! || IdentityPlugin,
//! )?;
//! return Ok(());
//! }
//!
//! // Here you can assume the binary is being run directly by a user,
//! // and perform administrative tasks like generating keys.
//!
//! Ok(())
//! }
//! ```
#![forbid(unsafe_code)]
// Catch documentation errors caused by code changes.
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_docs)]
use age_core::secrecy::SecretString;
use bech32::Variant;
use std::io;
pub mod identity;
pub mod recipient;
// Plugin HRPs are age1[name] and AGE-PLUGIN-[NAME]-
const PLUGIN_RECIPIENT_PREFIX: &str = "age1";
const PLUGIN_IDENTITY_PREFIX: &str = "age-plugin-";
/// Prints the newly-created identity and corresponding recipient to standard out.
///
/// A "created" time is included in the output, set to the current local time.
pub fn print_new_identity(plugin_name: &str, identity: &[u8], recipient: &[u8]) {
use bech32::ToBase32;
println!(
"# created: {}",
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
);
println!(
"# recipient: {}",
bech32::encode(
&format!("{}{}", PLUGIN_RECIPIENT_PREFIX, plugin_name),
recipient.to_base32(),
Variant::Bech32
)
.expect("HRP is valid")
);
println!(
"{}",
bech32::encode(
&format!("{}{}-", PLUGIN_IDENTITY_PREFIX, plugin_name),
identity.to_base32(),
Variant::Bech32,
)
.expect("HRP is valid")
.to_uppercase()
);
}
/// Runs the plugin state machine defined by `state_machine`.
///
/// This should be triggered if the `--age-plugin=state_machine` flag is provided as an
/// argument when starting the plugin.
pub fn run_state_machine<R: recipient::RecipientPluginV1, I: identity::IdentityPluginV1>(
state_machine: &str,
recipient_v1: impl FnOnce() -> R,
identity_v1: impl FnOnce() -> I,
) -> io::Result<()> {
use age_core::plugin::{IDENTITY_V1, RECIPIENT_V1};
match state_machine {
RECIPIENT_V1 => recipient::run_v1(recipient_v1()),
IDENTITY_V1 => identity::run_v1(identity_v1()),
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"unknown plugin state machine",
)),
}
}
/// The interface that age plugins can use to interact with an age implementation.
pub trait Callbacks<E> {
/// Shows a message to the user.
///
/// This can be used to prompt the user to take some physical action, such as
/// inserting a hardware key.
fn message(&mut self, message: &str) -> age_core::plugin::Result<()>;
/// Requests that the user provides confirmation for some action.
///
/// This can be used to, for example, request that a hardware key the plugin wants to
/// try either be plugged in, or skipped.
///
/// - `message` is the request or call-to-action to be displayed to the user.
/// - `yes_string` and (optionally) `no_string` will be displayed on buttons or next
/// to selection options in the user's UI.
///
/// Returns:
/// - `Ok(true)` if the user selected the option marked with `yes_string`.
/// - `Ok(false)` if the user selected the option marked with `no_string` (or the
/// default negative confirmation label).
/// - `Err(Error::Fail)` if the confirmation request could not be given to the user
/// (for example, if there is no UI for displaying messages).
/// - `Err(Error::Unsupported)` if the user's client does not support this callback.
fn confirm(
&mut self,
message: &str,
yes_string: &str,
no_string: Option<&str>,
) -> age_core::plugin::Result<bool>;
/// Requests a non-secret value from the user.
///
/// `message` will be displayed to the user, providing context for the request.
///
/// To request secrets, use [`Callbacks::request_secret`].
fn request_public(&mut self, message: &str) -> age_core::plugin::Result<String>;
/// Requests a secret value from the user, such as a passphrase.
///
/// `message` will be displayed to the user, providing context for the request.
fn request_secret(&mut self, message: &str) -> age_core::plugin::Result<SecretString>;
/// Sends an error.
///
/// Note: This API may be removed in a subsequent API refactor, after we've figured
/// out how errors should be handled overall, and how to distinguish between hard and
/// soft errors.
fn error(&mut self, error: E) -> age_core::plugin::Result<()>;
}