|
| 1 | +//! The "tag" recipient type, native to age. |
| 2 | +
|
| 3 | +use std::collections::HashSet; |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +use age_core::{ |
| 7 | + format::{FileKey, Stanza}, |
| 8 | + primitives::hpke_seal, |
| 9 | + secrecy::ExposeSecret, |
| 10 | +}; |
| 11 | +use base64::{prelude::BASE64_STANDARD_NO_PAD, Engine}; |
| 12 | +use bech32::{ToBase32, Variant}; |
| 13 | +use hpke::{Deserializable, Serializable}; |
| 14 | +use p256::{ |
| 15 | + elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint}, |
| 16 | + EncodedPoint, PublicKey, |
| 17 | +}; |
| 18 | +use rand::rngs::OsRng; |
| 19 | + |
| 20 | +use crate::{util::parse_bech32, EncryptError}; |
| 21 | + |
| 22 | +const RECIPIENT_PREFIX: &str = "age1tag"; |
| 23 | + |
| 24 | +const P256TAG_RECIPIENT_TAG: &str = "p256tag"; |
| 25 | +const P256TAG_SALT: &str = "age-encryption.org/p256tag"; |
| 26 | + |
| 27 | +type Kem = hpke::kem::DhP256HkdfSha256; |
| 28 | + |
| 29 | +/// The non-hybrid tagged age recipient type, designed for hardware keys where decryption |
| 30 | +/// potentially requires user presence. |
| 31 | +/// |
| 32 | +/// With knowledge of the recipient, it is possible to check if a stanza was addressed to |
| 33 | +/// a specific recipient before attempting decryption. This offers less privacy than the |
| 34 | +/// untagged recipient types. |
| 35 | +#[derive(Clone, PartialEq, Eq)] |
| 36 | +pub struct Recipient { |
| 37 | + /// Compressed encoding of the recipient public key. |
| 38 | + compressed: EncodedPoint, |
| 39 | + /// Cached uncompressed encoding, for computing the tag. |
| 40 | + uncompressed: EncodedPoint, |
| 41 | + /// Cached in-memory representation, for HPKE. |
| 42 | + pk_recip: <Kem as hpke::Kem>::PublicKey, |
| 43 | +} |
| 44 | + |
| 45 | +impl std::str::FromStr for Recipient { |
| 46 | + type Err = &'static str; |
| 47 | + |
| 48 | + /// Parses a recipient key from a string. |
| 49 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 50 | + let (hrp, bytes) = parse_bech32(s).ok_or("invalid Bech32 encoding")?; |
| 51 | + |
| 52 | + if hrp != RECIPIENT_PREFIX { |
| 53 | + return Err("incorrect HRP"); |
| 54 | + } |
| 55 | + |
| 56 | + let encoded = EncodedPoint::from_bytes(bytes).map_err(|_| "invalid SEC-1 encoding")?; |
| 57 | + if !encoded.is_compressed() { |
| 58 | + return Err("not a compressed SEC-1 encoding"); |
| 59 | + } |
| 60 | + |
| 61 | + let point = PublicKey::from_encoded_point(&encoded) |
| 62 | + .into_option() |
| 63 | + .ok_or("invalid P-256 point")?; |
| 64 | + |
| 65 | + let uncompressed = point.to_encoded_point(false); |
| 66 | + let pk_recip = |
| 67 | + <Kem as hpke::Kem>::PublicKey::from_bytes(uncompressed.as_bytes()).expect("valid"); |
| 68 | + |
| 69 | + Ok(Self { |
| 70 | + compressed: encoded, |
| 71 | + uncompressed, |
| 72 | + pk_recip, |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl fmt::Display for Recipient { |
| 78 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 79 | + write!( |
| 80 | + f, |
| 81 | + "{}", |
| 82 | + bech32::encode( |
| 83 | + RECIPIENT_PREFIX, |
| 84 | + self.compressed.as_bytes().to_base32(), |
| 85 | + Variant::Bech32 |
| 86 | + ) |
| 87 | + .expect("HRP is valid") |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl fmt::Debug for Recipient { |
| 93 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 94 | + write!(f, "{}", self) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl crate::Recipient for Recipient { |
| 99 | + fn wrap_file_key( |
| 100 | + &self, |
| 101 | + file_key: &FileKey, |
| 102 | + ) -> Result<(Vec<Stanza>, HashSet<String>), EncryptError> { |
| 103 | + let mut rng = OsRng; |
| 104 | + |
| 105 | + let (enc, ct) = hpke_seal::<Kem, _>( |
| 106 | + &self.pk_recip, |
| 107 | + P256TAG_SALT.as_bytes(), |
| 108 | + file_key.expose_secret(), |
| 109 | + &mut rng, |
| 110 | + ); |
| 111 | + |
| 112 | + let ikm = enc |
| 113 | + .to_bytes() |
| 114 | + .into_iter() |
| 115 | + .chain(self.uncompressed.as_bytes().iter().copied()) |
| 116 | + .collect::<Vec<u8>>(); |
| 117 | + let tag = super::tag(&ikm, P256TAG_SALT); |
| 118 | + |
| 119 | + let encoded_tag = BASE64_STANDARD_NO_PAD.encode(tag); |
| 120 | + let encoded_enc = BASE64_STANDARD_NO_PAD.encode(enc.to_bytes()); |
| 121 | + |
| 122 | + Ok(( |
| 123 | + vec![Stanza { |
| 124 | + tag: P256TAG_RECIPIENT_TAG.to_owned(), |
| 125 | + args: vec![encoded_tag, encoded_enc], |
| 126 | + body: ct, |
| 127 | + }], |
| 128 | + HashSet::new(), |
| 129 | + )) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +#[cfg(test)] |
| 134 | +pub(crate) mod tests { |
| 135 | + use super::Recipient; |
| 136 | + |
| 137 | + pub(crate) const TEST_RECIPIENT: &str = |
| 138 | + "age1tag1qt8lw0ual6avlwmwatk888yqnmdamm7xfd0wak53ut6elz5c4swx2yqdj4e"; |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn recipient_encoding() { |
| 142 | + let recipient: Recipient = TEST_RECIPIENT.parse().unwrap(); |
| 143 | + assert_eq!(recipient.to_string(), TEST_RECIPIENT); |
| 144 | + } |
| 145 | +} |
0 commit comments