use super::*;
use std::{
fs,
os::unix::fs::{MetadataExt, PermissionsExt, symlink},
path::Path,
};
fn keygen(identity: &Path) -> Result<String> {
let result = execute(Command::Keygen {
identity: identity.into(),
})?;
let recipient = result["recipient"].as_str().unwrap();
assert_eq!(
result,
json!({"recipient":recipient,"url":format!("https://gent.link/assets/secret-handoff.html#{recipient}")})
);
assert_eq!(fs::metadata(identity)?.mode() & 0o777, 0o600);
Ok(recipient.to_owned())
}
fn seal(recipient: &str, input: &Path, out: &Path) -> Result<Value> {
execute(Command::Seal {
recipient: recipient.into(),
input: input.into(),
out: out.into(),
})
}
fn decrypt(identity: &Path, input: &Path, out: &Path) -> Result<Value> {
execute(Command::Open {
identity: identity.into(),
input: input.into(),
out: out.into(),
})
}
#[test]
fn maximum_binary_secret_round_trips_in_one_gent_message() -> Result<()> {
let temp = tempfile::tempdir()?;
let dir = temp.path().canonicalize()?;
let (identity, input, armor, out) = (
dir.join("key"),
dir.join("input"),
dir.join("armor"),
dir.join("out"),
);
let recipient = keygen(&identity)?;
let bytes = (0..MAX_SECRET).map(|i| (i % 256) as u8).collect::<Vec<_>>();
fs::write(&input, &bytes)?;
assert_eq!(seal(&recipient, &input, &armor)?, json!({"state":"sealed"}));
let encoded = fs::read_to_string(&armor)?;
assert!(encoded.starts_with("-----BEGIN AGE ENCRYPTED FILE-----\n"));
assert!(encoded.ends_with("-----END AGE ENCRYPTED FILE-----\n"));
assert!(encoded.len() <= gent_protocol::MAX_TEXT_BYTES);
assert_eq!(fs::metadata(&armor)?.mode() & 0o777, 0o600);
assert_eq!(decrypt(&identity, &armor, &out)?, json!({"state":"opened"}));
assert!(fs::read(&out)? == bytes);
assert_eq!(fs::metadata(&out)?.mode() & 0o777, 0o600);
Ok(())
}
#[test]
fn wrong_key_and_malformed_or_unauthenticated_envelopes_never_publish() -> Result<()> {
let temp = tempfile::tempdir()?;
let dir = temp.path().canonicalize()?;
let (key, other, input, armor, out) = (
dir.join("key"),
dir.join("other"),
dir.join("input"),
dir.join("armor"),
dir.join("out"),
);
let recipient = keygen(&key)?;
assert_ne!(keygen(&other)?, recipient);
fs::write(&input, b"fixture\0\xff\r\n")?;
seal(&recipient, &input, &armor)?;
assert!(decrypt(&other, &armor, &out).is_err());
assert!(!out.exists());
let text = fs::read(&armor)?;
let mut binary = Vec::new();
ArmoredReader::new(text.as_slice()).read_to_end(&mut binary)?;
let mut damaged = binary.clone();
*damaged.last_mut().unwrap() ^= 1;
let mut writer = ArmoredWriter::wrap_output(Vec::new(), Format::AsciiArmor)?;
writer.write_all(&damaged)?;
for malformed in [
binary,
writer.finish()?,
text[..text.len() / 2].to_vec(),
[text.as_slice(), b"private-marker"].concat(),
] {
fs::write(&armor, malformed)?;
let error = decrypt(&key, &armor, &out).unwrap_err();
assert!(!format!("{error:#}").contains("private-marker"));
assert!(!out.exists());
}
Ok(())
}
#[test]
fn limits_apply_on_both_sides_and_private_key_errors_are_redacted() -> Result<()> {
let temp = tempfile::tempdir()?;
let dir = temp.path().canonicalize()?;
let (key, input, armor, out) = (
dir.join("key"),
dir.join("input"),
dir.join("armor"),
dir.join("out"),
);
let recipient = keygen(&key)?;
for size in [0, MAX_SECRET + 1] {
let data = vec![0x41; size];
fs::write(&input, &data)?;
assert!(seal(&recipient, &input, &out).is_err());
let r: x25519::Recipient = recipient.parse().unwrap();
let enc = age::Encryptor::with_recipients(std::iter::once(&r as &dyn age::Recipient))?;
let mut writer =
enc.wrap_output(ArmoredWriter::wrap_output(Vec::new(), Format::AsciiArmor)?)?;
writer.write_all(&data)?;
fs::write(&armor, writer.finish()?.finish()?)?;
assert!(decrypt(&key, &armor, &out).is_err());
assert!(!out.exists());
}
fs::write(&key, b"private-marker")?;
let err = decrypt(&key, &armor, &out).unwrap_err();
assert!(!format!("{err:#}").contains("private-marker"));
let err = seal("private-marker", &input, &out).unwrap_err();
assert!(!format!("{err:#}").contains("private-marker"));
Ok(())
}
#[test]
fn rejects_noncontributory_recipients_before_opening_input() -> Result<()> {
use bech32::{ToBase32, Variant};
let temp = tempfile::tempdir()?;
let missing = temp.path().join("no-input");
let output = temp.path().join("no-output");
for first_byte in [0, 1] {
let mut point = [0; 32];
point[0] = first_byte;
let encoded = bech32::encode("age", point.to_base32(), Variant::Bech32)?;
assert!(encoded.parse::<x25519::Recipient>().is_ok());
let error = seal(&encoded, &missing, &output).unwrap_err();
assert_eq!(format!("{error:#}"), "invalid public age X25519 recipient");
assert!(!output.exists());
}
Ok(())
}
#[test]
fn refuses_links_shared_identities_and_overwrites() -> Result<()> {
let temp = tempfile::tempdir()?;
let dir = temp.path().canonicalize()?;
let (key, input, armor, out) = (
dir.join("key"),
dir.join("input"),
dir.join("armor"),
dir.join("out"),
);
let recipient = keygen(&key)?;
assert!(keygen(&key).is_err());
fs::write(&input, b"original")?;
seal(&recipient, &input, &armor)?;
assert!(seal(&recipient, &input, &armor).is_err());
assert!(decrypt(&key, &armor, &input).is_err());
assert_eq!(fs::read(&input)?, b"original");
let link = dir.join("link");
symlink(&out, &link)?;
assert!(decrypt(&key, &armor, &link).is_err());
assert!(!out.exists());
fs::remove_file(&link)?;
symlink(&input, &link)?;
assert!(seal(&recipient, &link, &out).is_err());
fs::remove_file(&link)?;
fs::hard_link(&input, &link)?;
assert!(seal(&recipient, &input, &out).is_err());
fs::remove_file(&link)?;
symlink(&dir, &link)?;
let alias_key = link.join("alias-key");
let alias_recipient = keygen(&alias_key)?;
seal(
&alias_recipient,
&link.join("input"),
&link.join("alias-armor"),
)?;
decrypt(
&alias_key,
&link.join("alias-armor"),
&link.join("alias-out"),
)?;
assert_eq!(fs::read(dir.join("alias-out"))?, b"original");
fs::set_permissions(&key, fs::Permissions::from_mode(0o644))?;
assert!(decrypt(&key, &armor, &out).is_err());
assert!(!out.exists());
assert!(fs::read_dir(&dir)?.all(|p| {
!p.unwrap()
.file_name()
.to_string_lossy()
.starts_with(".gent-secret-")
}));
Ok(())
}