feat(users): keystore v2 (8 slots, per-user salt+name tag, owner admin key opens sealed user dir, per-user duress); gatekeeper login by name+passcode, owner menu (add/remove/guests), passcode change, guest sessions
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//! randomness, and the owner (`Prompt`). No kernel, no hardware: tested on
|
||||
//! the host.
|
||||
|
||||
use keystore::{Block, Copies, KdfParams, Key, Outcome, RECORD_SIZE, Random, Record};
|
||||
use keystore::{Block, Copies, KdfParams, Key, Name, Outcome, RECORD_SIZE, Random, Record, Refused, Role, SLOTS, Session, User, UserId};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
/// The keystore device: the record's two copies.
|
||||
@@ -62,6 +62,8 @@ pub trait Prompt {
|
||||
/// Shows `question` and reads a passcode without showing it; false if
|
||||
/// nothing could be read.
|
||||
fn secret(&mut self, question: &str, out: &mut Secret) -> bool;
|
||||
/// Shows `question` and reads an ordinary line (a name, a choice).
|
||||
fn line(&mut self, question: &str, out: &mut Secret) -> bool;
|
||||
}
|
||||
|
||||
/// Why the gatekeeper could not hand over a key.
|
||||
@@ -75,11 +77,23 @@ pub enum Failure {
|
||||
Prompt,
|
||||
}
|
||||
|
||||
/// Wrong passcodes in a row that wipe the keys.
|
||||
/// Wrong passcodes in a row (for any name) that wipe every user.
|
||||
pub const THRESHOLD: u8 = 10;
|
||||
/// Shortest passcode accepted at setup.
|
||||
/// Shortest passcode accepted.
|
||||
pub const MIN_PASSCODE: usize = 4;
|
||||
|
||||
/// Someone is in.
|
||||
pub struct Unlocked {
|
||||
/// Who, or `None` for a guest (whose key is made up for this session).
|
||||
pub user: Option<User>,
|
||||
pub key: Key,
|
||||
/// The keystore is new (first boot, or just wiped): nothing sealed
|
||||
/// under an earlier key exists any more.
|
||||
pub fresh: bool,
|
||||
/// Users removed just now, whose files are to go.
|
||||
pub removed: [Option<UserId>; SLOTS],
|
||||
}
|
||||
|
||||
/// The keystore's newest record, and where the next write goes.
|
||||
fn load(ks: &mut impl Keystore) -> Result<(Option<Record>, usize), Failure> {
|
||||
let (mut a, mut b) = ([0u8; RECORD_SIZE], [0u8; RECORD_SIZE]);
|
||||
@@ -94,9 +108,15 @@ fn store(ks: &mut impl Keystore, copy: usize, r: &Record) -> Result<(), Failure>
|
||||
if ks.write(copy, &r.encode()) { Ok(()) } else { Err(Failure::Device) }
|
||||
}
|
||||
|
||||
/// Destroys the key slots: noise over both copies, twice, then blank (the
|
||||
/// device then looks never set up). Marked as wiping first, so a power cut
|
||||
/// part way is finished on the next boot.
|
||||
/// `r` written as the newest record.
|
||||
fn save(ks: &mut impl Keystore, r: &Record) -> Result<(), Failure> {
|
||||
let (_, next) = load(ks)?;
|
||||
store(ks, next, r)
|
||||
}
|
||||
|
||||
/// Destroys every slot and the directory: noise over both copies, twice,
|
||||
/// then blank (the device then looks never set up). Marked as wiping
|
||||
/// first, so a power cut part way is finished on the next boot.
|
||||
fn wipe(ks: &mut impl Keystore, record: &Record, next: usize, rng: &mut impl Random) -> Result<(), Failure> {
|
||||
let first = record.wiping(rng).ok_or(Failure::Random)?;
|
||||
let second = first.wiping(rng).ok_or(Failure::Random)?;
|
||||
@@ -127,50 +147,132 @@ fn choose(prompt: &mut impl Prompt, question: &str, optional: bool, out: &mut Se
|
||||
}
|
||||
}
|
||||
|
||||
/// A device never set up (or wiped): the owner chooses a passcode, and
|
||||
/// may choose a duress one. Returns the new master key.
|
||||
fn setup(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prompt, params: KdfParams, memory: &mut [Block]) -> Result<Key, Failure> {
|
||||
// After a wipe nothing earlier may show: it must look like a new device.
|
||||
prompt.clear();
|
||||
prompt.say("This device has no passcode yet. Choose one; it protects everything on it.\n");
|
||||
let mut real = Secret::new();
|
||||
choose(prompt, "New passcode: ", false, &mut real)?;
|
||||
prompt.say("A duress passcode wipes the device when it is entered instead. Leave it empty for none.\n");
|
||||
let mut duress = Secret::new();
|
||||
/// A name, asked until it is a valid one.
|
||||
fn ask_name(prompt: &mut impl Prompt, question: &str) -> Result<Name, Failure> {
|
||||
loop {
|
||||
choose(prompt, "Duress passcode: ", true, &mut duress)?;
|
||||
if duress.len == 0 || !duress.same(&real) {
|
||||
break;
|
||||
let mut typed = Secret::new();
|
||||
if !prompt.line(question, &mut typed) {
|
||||
return Err(Failure::Prompt);
|
||||
}
|
||||
match Name::new(typed.as_bytes()) {
|
||||
Some(n) => return Ok(n),
|
||||
None => prompt.say("A name is 1 to 16 letters, digits or signs, no spaces.\n"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A passcode and an optional duress passcode, different from it.
|
||||
fn ask_passcodes(prompt: &mut impl Prompt, real: &mut Secret, duress: &mut Secret) -> Result<(), Failure> {
|
||||
choose(prompt, "New passcode: ", false, real)?;
|
||||
prompt.say("A duress passcode wipes the device when it is entered instead. Leave it empty for none.\n");
|
||||
loop {
|
||||
choose(prompt, "Duress passcode: ", true, duress)?;
|
||||
if duress.len == 0 || !duress.same(real) {
|
||||
return Ok(());
|
||||
}
|
||||
prompt.say("It must differ from the passcode.\n");
|
||||
}
|
||||
}
|
||||
|
||||
/// A device never set up (or wiped): its first user, an owner.
|
||||
fn setup(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prompt, params: KdfParams, memory: &mut [Block]) -> Result<Unlocked, Failure> {
|
||||
// After a wipe nothing earlier may show: it must look like a new device.
|
||||
prompt.clear();
|
||||
prompt.say("This device has no users yet. Set up its owner; the passcode protects everything on it.\n");
|
||||
let name = ask_name(prompt, "Your name: ")?;
|
||||
let (mut real, mut duress) = (Secret::new(), Secret::new());
|
||||
ask_passcodes(prompt, &mut real, &mut duress)?;
|
||||
let duress = (duress.len > 0).then_some(duress.as_bytes());
|
||||
let (record, key) = Record::setup(real.as_bytes(), duress, THRESHOLD, params, memory, rng).ok_or(Failure::Random)?;
|
||||
let (record, session) = Record::setup(name, real.as_bytes(), duress, THRESHOLD, params, memory, rng).ok_or(Failure::Random)?;
|
||||
store(ks, 0, &record)?;
|
||||
store(ks, 1, &record)?;
|
||||
Ok(key)
|
||||
Ok(Unlocked { user: Some(session.user), key: session.key.clone(), fresh: true, removed: [None; SLOTS] })
|
||||
}
|
||||
|
||||
/// The owner is in: the master key, and whether it is new (a fresh
|
||||
/// device, or one just wiped), so nothing sealed under an old one exists.
|
||||
pub struct Unlocked {
|
||||
pub key: Key,
|
||||
pub fresh: bool,
|
||||
/// What the owner may change at the lock screen, after signing in.
|
||||
fn manage(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prompt, memory: &mut [Block], session: &Session, removed: &mut [Option<UserId>; SLOTS]) -> Result<(), Failure> {
|
||||
loop {
|
||||
let (Some(record), _) = load(ks)? else { return Err(Failure::Device) };
|
||||
let users = record.users(session).map_err(|_| Failure::Device)?;
|
||||
prompt.say("Users:");
|
||||
for u in users.iter().flatten() {
|
||||
prompt.say(" ");
|
||||
prompt.say(core::str::from_utf8(u.name.as_bytes()).unwrap_or("?"));
|
||||
if u.role == Role::Owner {
|
||||
prompt.say(" (owner)");
|
||||
}
|
||||
}
|
||||
prompt.say(if record.guests { ". Guests: allowed.\n" } else { ". Guests: not allowed.\n" });
|
||||
let mut choice = Secret::new();
|
||||
if !prompt.line("[a] add, [r] remove, [g] guests on/off, [Enter] done: ", &mut choice) {
|
||||
return Err(Failure::Prompt);
|
||||
}
|
||||
let changed = match choice.as_bytes() {
|
||||
b"" => return Ok(()),
|
||||
b"g" => Ok(record.next(|r| r.guests = !r.guests)),
|
||||
b"a" => {
|
||||
let name = ask_name(prompt, "Name: ")?;
|
||||
let mut role = Secret::new();
|
||||
if !prompt.line("Owner too? [y/N] ", &mut role) {
|
||||
return Err(Failure::Prompt);
|
||||
}
|
||||
let role = if role.as_bytes() == b"y" { Role::Owner } else { Role::Standard };
|
||||
let (mut real, mut duress) = (Secret::new(), Secret::new());
|
||||
ask_passcodes(prompt, &mut real, &mut duress)?;
|
||||
let duress = (duress.len > 0).then_some(duress.as_bytes());
|
||||
record.add_user(session, name, role, real.as_bytes(), duress, memory, rng).map(|(r, _)| r)
|
||||
}
|
||||
b"r" => {
|
||||
let name = ask_name(prompt, "Remove whom: ")?;
|
||||
record.remove_user(session, &name, rng).map(|(r, id)| {
|
||||
if let Some(slot) = removed.iter_mut().find(|s| s.is_none()) {
|
||||
*slot = Some(id);
|
||||
}
|
||||
r
|
||||
})
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
match changed {
|
||||
Ok(r) => save(ks, &r)?,
|
||||
Err(Refused::NameTaken) => prompt.say("That name is taken.\n"),
|
||||
Err(Refused::Full) => prompt.say("No room for another user.\n"),
|
||||
Err(Refused::NoSuchUser) => prompt.say("No such user.\n"),
|
||||
Err(Refused::LastOwner) => prompt.say("The device needs an owner.\n"),
|
||||
Err(_) => return Err(Failure::Random),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Authenticates the owner and returns the master key: setting one up on
|
||||
/// a fresh device, wiping on duress or too many failures (the device then
|
||||
/// looks fresh, and is set up anew).
|
||||
/// Authenticates someone and returns their key: setting up a fresh
|
||||
/// device's owner, wiping everyone on a duress passcode or too many
|
||||
/// failures (the device then looks fresh, and is set up anew).
|
||||
pub fn run(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prompt, params: KdfParams, memory: &mut [Block]) -> Result<Unlocked, Failure> {
|
||||
loop {
|
||||
let (record, next) = load(ks)?;
|
||||
let Some(record) = record else {
|
||||
return setup(ks, rng, prompt, params, memory).map(|key| Unlocked { key, fresh: true });
|
||||
return setup(ks, rng, prompt, params, memory);
|
||||
};
|
||||
if record.must_wipe() {
|
||||
wipe(ks, &record, next, rng)?;
|
||||
continue;
|
||||
}
|
||||
let mut name = Secret::new();
|
||||
if !prompt.line("Name: ", &mut name) {
|
||||
return Err(Failure::Prompt);
|
||||
}
|
||||
if name.len == 0 {
|
||||
if !record.guests {
|
||||
prompt.say("Guests are not allowed here.\n");
|
||||
continue;
|
||||
}
|
||||
// A key nobody keeps: what a guest leaves behind is noise.
|
||||
let mut key = Key([0; keystore::KEY_SIZE]);
|
||||
if !rng.fill(&mut key.0) {
|
||||
return Err(Failure::Random);
|
||||
}
|
||||
return Ok(Unlocked { user: None, key, fresh: false, removed: [None; SLOTS] });
|
||||
}
|
||||
let mut typed = Secret::new();
|
||||
if !prompt.secret("Passcode: ", &mut typed) {
|
||||
return Err(Failure::Prompt);
|
||||
@@ -178,10 +280,31 @@ pub fn run(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prom
|
||||
// Counted before it is checked: pulling the power cannot save a guess.
|
||||
let counted = record.next(|r| r.failures = r.failures.saturating_add(1));
|
||||
store(ks, next, &counted)?;
|
||||
match record.unlock(typed.as_bytes(), memory) {
|
||||
Outcome::Real(key) => {
|
||||
match record.unlock(name.as_bytes(), typed.as_bytes(), memory) {
|
||||
Outcome::User(session) => {
|
||||
store(ks, 1 - next, &counted.next(|r| r.failures = 0))?;
|
||||
return Ok(Unlocked { key, fresh: false });
|
||||
let mut removed = [None; SLOTS];
|
||||
loop {
|
||||
let mut choice = Secret::new();
|
||||
let menu = if session.is_owner() { "[Enter] continue, [p] passcode, [u] users: " } else { "[Enter] continue, [p] passcode: " };
|
||||
if !prompt.line(menu, &mut choice) {
|
||||
return Err(Failure::Prompt);
|
||||
}
|
||||
match choice.as_bytes() {
|
||||
b"" => break,
|
||||
b"p" => {
|
||||
let mut new = Secret::new();
|
||||
choose(prompt, "New passcode: ", false, &mut new)?;
|
||||
let (Some(now), _) = load(ks)? else { return Err(Failure::Device) };
|
||||
let changed = now.change_passcode(&session, new.as_bytes(), memory, rng).map_err(|_| Failure::Random)?;
|
||||
save(ks, &changed)?;
|
||||
prompt.say("Passcode changed.\n");
|
||||
}
|
||||
b"u" if session.is_owner() => manage(ks, rng, prompt, memory, &session, &mut removed)?,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
return Ok(Unlocked { user: Some(session.user), key: session.key.clone(), fresh: false, removed });
|
||||
}
|
||||
Outcome::Duress => {
|
||||
// Recorded as worn out first: a wipe cut short resumes.
|
||||
@@ -192,7 +315,7 @@ pub fn run(ks: &mut impl Keystore, rng: &mut impl Random, prompt: &mut impl Prom
|
||||
Outcome::Wrong => {
|
||||
let left = counted.threshold.saturating_sub(counted.failures);
|
||||
if left > 0 {
|
||||
prompt.say(if left == 1 { "Wrong passcode. One try left before the device is wiped.\n" } else { "Wrong passcode.\n" });
|
||||
prompt.say(if left == 1 { "Wrong name or passcode. One try left before the device is wiped.\n" } else { "Wrong name or passcode.\n" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user