[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())));
|
||||
}
|
||||
}
|
||||
|
||||
+23
-2
@@ -20,8 +20,29 @@ aad "CSL1" ‖ E ‖ context
|
||||
A box opens only with the context it was sealed for, so a box for one request does not work as another.
|
||||
- **A low-order public key is refused** on both sides: the shared secret must be contributory.
|
||||
- **Plaintext limit:** 32 KiB.
|
||||
- **The recipient's secret half** lives only in memory, as `x25519_dalek::StaticSecret` with `zeroize`,
|
||||
and is wiped on drop. Nothing writes it to disk.
|
||||
- **The recipient's secret half** lives in memory as `x25519_dalek::StaticSecret` with `zeroize`, and is
|
||||
wiped on drop. One-time keys (`Recipient::generate`) are the default and nothing writes them to disk.
|
||||
|
||||
### A kept key (core 0.11.0)
|
||||
|
||||
The recipient's secret may now be kept by the caller, for one purpose: the PC's Provisio sealing key
|
||||
(docs/10_provisio.md in the app), the key the server seals **offers** to. Custos makes it once with
|
||||
`Recipient::generate`, keeps `Recipient::secret_bytes()` DPAPI-sealed in `<data>actors\provision.key`, and
|
||||
restores it with `Recipient::from_secret` at every start. Everything else (approvals, pairing, a Provisio *ask*)
|
||||
still uses a fresh one-time key per request.
|
||||
|
||||
```
|
||||
Recipient::from_secret(Zeroizing<[u8; 32]>) -> Result<Recipient, SealError> // BadKey for a known key
|
||||
Recipient::secret_bytes(&self) -> Zeroizing<[u8; 32]> // only for a key that is kept
|
||||
```
|
||||
|
||||
- `from_secret` refuses (`BadKey`) a secret that X25519 clamps to zero (all zeros: a wiped or never-filled
|
||||
buffer) and, as for public keys, one whose shared secrets would not be contributory.
|
||||
- A kept key opens every box ever sealed to it, so the **context** carries the binding: an offer is sealed for
|
||||
`provision-offer:<store>:<item>`, and the PC checks the plaintext's item, store, kind and code key against the
|
||||
offer before it shows a card. The mailbox refuses an offer whose ephemeral key it has seen (replay).
|
||||
- `secret_bytes` hands out the secret in a `Zeroizing` buffer. It goes to the caller's sealed storage and
|
||||
nowhere else: never a log, a message or a file in the clear. `Debug` of a `Recipient` prints `[redacted]`.
|
||||
|
||||
## The match code
|
||||
|
||||
|
||||
Reference in New Issue
Block a user