[seal] Recipient: a kept key (from_secret refuses a known key, secret_bytes for DPAPI storage), Debug redacted; docs/SEAL.md
This commit is contained in:
@@ -52,7 +52,9 @@ fn random<const N: usize>() -> Zeroizing<[u8; N]> {
|
||||
|
||||
fn derive(ikm: &[u8], salt: &[u8], info: &[&[u8]]) -> Zeroizing<[u8; 32]> {
|
||||
let mut k = Zeroizing::new([0u8; 32]);
|
||||
hkdf::Hkdf::<Sha256>::new(Some(salt), ikm).expand(&info.concat(), k.as_mut()).expect("32 bytes is a valid HKDF length");
|
||||
hkdf::Hkdf::<Sha256>::new(Some(salt), ikm)
|
||||
.expand(&info.concat(), k.as_mut())
|
||||
.expect("32 bytes is a valid HKDF length");
|
||||
k
|
||||
}
|
||||
|
||||
@@ -64,8 +66,8 @@ fn xnonce(n: &[u8]) -> XNonce {
|
||||
XNonce::try_from(n).expect("24-byte nonce")
|
||||
}
|
||||
|
||||
/// The one-time key pair of a device waiting for a secret. Its secret half never leaves memory and is
|
||||
/// wiped on drop.
|
||||
/// The one-time key pair of a device waiting for a secret. Its secret half is wiped on drop and leaves
|
||||
/// memory only through `secret_bytes`, for a caller that keeps a long-lived key (`from_secret`).
|
||||
pub struct Recipient {
|
||||
secret: StaticSecret,
|
||||
public: [u8; 32],
|
||||
@@ -78,6 +80,30 @@ impl Recipient {
|
||||
Recipient { secret, public }
|
||||
}
|
||||
|
||||
/// A long-lived key the caller kept (docs/SEAL.md, "A kept key"): the PC's Provisio sealing key, stored
|
||||
/// DPAPI-sealed and restored at start. Refuses a secret everyone knows: one that clamps to zero (a
|
||||
/// wiped or never-filled buffer), and, as `seal` does for a public key, one whose shared secrets would
|
||||
/// not be contributory.
|
||||
pub fn from_secret(secret: Zeroizing<[u8; 32]>) -> Result<Recipient, SealError> {
|
||||
let secret = StaticSecret::from(*secret);
|
||||
let public = PublicKey::from(&secret).to_bytes();
|
||||
let zero = PublicKey::from(&StaticSecret::from([0u8; 32])).to_bytes();
|
||||
if public == zero {
|
||||
return Err(SealError::BadKey);
|
||||
}
|
||||
let probe = StaticSecret::from(*random::<32>());
|
||||
if !probe.diffie_hellman(&PublicKey::from(public)).was_contributory() {
|
||||
return Err(SealError::BadKey);
|
||||
}
|
||||
Ok(Recipient { secret, public })
|
||||
}
|
||||
|
||||
/// The secret half, for a caller that keeps this key (sealed with DPAPI or the like) instead of
|
||||
/// dropping it after one box. One-time keys never call this.
|
||||
pub fn secret_bytes(&self) -> Zeroizing<[u8; 32]> {
|
||||
Zeroizing::new(self.secret.to_bytes())
|
||||
}
|
||||
|
||||
/// What the device sends: 32 bytes.
|
||||
pub fn public(&self) -> [u8; 32] {
|
||||
self.public
|
||||
@@ -109,7 +135,7 @@ impl Recipient {
|
||||
|
||||
impl std::fmt::Debug for Recipient {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Recipient {{ match_code: {} }}", self.match_code())
|
||||
write!(f, "Recipient {{ match_code: {}, secret: [redacted] }}", self.match_code())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,3 +72,47 @@ fn at_rest_seals_open_with_the_same_key_and_context_only() {
|
||||
assert_eq!(open_at_rest(&key, b"totp:f2", &b), Err(SealError::Rejected));
|
||||
assert_eq!(open_at_rest(&key, b"totp:f1", b"CSR1"), Err(SealError::Malformed));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_kept_recipient_comes_back_from_its_secret() {
|
||||
// The PC keeps its Provisio sealing key (DPAPI-sealed) and restores it at the next start.
|
||||
let r = Recipient::generate();
|
||||
let kept = r.secret_bytes();
|
||||
let back = Recipient::from_secret(kept).unwrap();
|
||||
assert_eq!(back.public(), r.public());
|
||||
assert_eq!(back.match_code(), r.match_code());
|
||||
assert_eq!(back.secret_bytes().as_slice(), r.secret_bytes().as_slice());
|
||||
// A box sealed to the original opens with the restored key, for its purpose only.
|
||||
let ctx = b"provision-offer:vault:fabrica/gitea-read-token";
|
||||
let b = seal(&r.public(), ctx, b"gitea token").unwrap();
|
||||
assert_eq!(back.open(&b, ctx).unwrap().as_slice(), b"gitea token");
|
||||
assert_eq!(back.open(&b, b"provision-offer:vault:fabrica/other"), Err(SealError::Rejected));
|
||||
// Another kept key does not open it.
|
||||
let other = Recipient::from_secret(Recipient::generate().secret_bytes()).unwrap();
|
||||
assert_eq!(other.open(&b, ctx), Err(SealError::Rejected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_known_secret_is_refused() {
|
||||
// All zeros (a wiped or never-filled buffer) is a key everyone knows, and so is anything that clamps to it.
|
||||
assert_eq!(Recipient::from_secret(zeroize::Zeroizing::new([0u8; 32])).unwrap_err(), SealError::BadKey);
|
||||
let mut clamps_to_zero = [0u8; 32];
|
||||
clamps_to_zero[0] = 0b0000_0111;
|
||||
clamps_to_zero[31] = 0b1000_0000;
|
||||
assert_eq!(Recipient::from_secret(zeroize::Zeroizing::new(clamps_to_zero)).unwrap_err(), SealError::BadKey);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_kept_recipient_prints_redacted() {
|
||||
let r = Recipient::generate();
|
||||
let secret = r.secret_bytes();
|
||||
let back = Recipient::from_secret(secret.clone()).unwrap();
|
||||
for shown in [format!("{r:?}"), format!("{back:?}"), format!("{back:#?}")] {
|
||||
assert!(shown.contains("[redacted]"), "{shown}");
|
||||
assert!(!shown.contains(&format!("{:?}", *secret)));
|
||||
assert!(!shown.contains(&format!("{:?}", &secret[..8])));
|
||||
let hex: String = secret.iter().map(|b| format!("{b:02x}")).collect();
|
||||
assert!(!shown.contains(&hex[..16]));
|
||||
assert!(!shown.contains(&format!("{:?}", back.public())));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user