refactor ux flow, now on a good footing

This commit is contained in:
bunnie 2021-07-31 03:27:16 +08:00
parent 41843db988
commit d7d052b6ff
9 changed files with 224 additions and 104 deletions

View file

@ -3,5 +3,10 @@
"en": "[ Okay ]",
"zh": "[ 确定 ]",
"en-audio": "Accept and close radio box"
},
"notification.dismiss": {
"en": "[ Press any key ]",
"zh": "[ 按任意键 ]",
"en-audio": "Press any key"
}
}

View file

@ -47,6 +47,8 @@ General form for modals:
- slider - left/right moves the slider, enter/select closes the modal
*/
use enum_dispatch::enum_dispatch;
use xous::MessageEnvelope;
use xous::send_message;
use crate::api::*;
use crate::Gam;
@ -163,6 +165,7 @@ pub struct TextEntry {
pub validator: Option<fn(TextEntryPayload, u32) -> Option<xous_ipc::String::<512>> >,
}
impl ActionApi for TextEntry {
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
fn is_password(&self) -> bool {
self.is_password
}
@ -417,6 +420,7 @@ impl RadioButtons {
}
}
impl ActionApi for RadioButtons {
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
fn height(&self, glyph_height: i16, margin: i16) -> i16 {
let mut total_items = 0;
// total items, then +1 for the "Okay" message
@ -589,6 +593,7 @@ impl CheckBoxes {
}
}
impl ActionApi for CheckBoxes {
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
fn height(&self, glyph_height: i16, margin: i16) -> i16 {
let mut total_items = 0;
// total items, then +1 for the "Okay" message
@ -731,6 +736,96 @@ impl ActionApi for CheckBoxes {
(None, false)
}
}
#[derive(Debug, Copy, Clone)]
pub struct Notification {
pub action_conn: xous::CID,
pub action_opcode: u32,
pub is_password: bool,
}
impl Notification {
pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self {
Notification {
action_conn,
action_opcode,
is_password: false,
}
}
pub fn set_is_password(&mut self, setting: bool) {
// this will cause text to be inverted. Untrusted entities can try to set this,
// but the GAM should defeat this for dialog boxes outside of the trusted boot
// set because they can't achieve a high enough trust level.
self.is_password = true;
}
}
impl ActionApi for Notification {
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
fn height(&self, glyph_height: i16, margin: i16) -> i16 {
glyph_height + margin * 2 + 5
}
fn redraw(&self, at_height: i16, modal: &Modal) {
// prime a textview with the correct general style parameters
let mut tv = TextView::new(
modal.canvas,
TextBounds::BoundingBox(Rectangle::new_coords(0, 0, 1, 1))
);
tv.ellipsis = true;
tv.style = modal.style;
tv.invert = self.is_password;
tv.draw_border= false;
tv.margin = Point::new(0, 0,);
tv.insertion = None;
tv.bounds_computed = None;
tv.bounds_hint = TextBounds::GrowableFromTl(
Point::new(modal.margin, at_height + modal.margin * 2),
(modal.canvas_width - modal.margin * 2) as u16
);
write!(tv, "{}", t!("notification.dismiss", xous::LANG)).unwrap();
modal.gam.bounds_compute_textview(&mut tv).expect("couldn't simulate text size");
let textwidth = if let Some(bounds) = tv.bounds_computed {
bounds.br.x - bounds.tl.x
} else {
modal.canvas_width - modal.margin * 2
};
log::info!("tw: {}", textwidth);
let offset = (modal.canvas_width - textwidth) / 2;
log::info!("offset2: {}", offset);
tv.bounds_computed = None;
tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new(
Point::new(offset, at_height + modal.margin * 2),
Point::new(modal.canvas_width - modal.margin, at_height + modal.line_height + modal.margin * 2)
));
modal.gam.post_textview(&mut tv).expect("couldn't post tv");
// divider lines
let color = if self.is_password {
PixelColor::Light
} else {
PixelColor::Dark
};
modal.gam.draw_line(modal.canvas, Line::new_with_style(
Point::new(modal.margin, at_height + modal.margin),
Point::new(modal.canvas_width - modal.margin, at_height + modal.margin),
DrawStyle::new(color, color, 1))
).expect("couldn't draw entry line");
}
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) {
log::trace!("key_action: {}", k);
match k {
'\u{0}' => {
// ignore null messages
}
_ => {
send_message(self.action_conn, xous::Message::new_scalar(self.action_opcode as usize, 0, 0, 0, 0)).expect("couldn't pass on dismissal");
return(None, true)
}
}
(None, false)
}
}
#[derive(Debug, Copy, Clone)]
pub struct Slider {
pub min: u32,
@ -749,6 +844,7 @@ impl ActionApi for Slider {
*/
glyph_height * 3 + margin * 2
}
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
}
@ -756,13 +852,14 @@ impl ActionApi for Slider {
#[enum_dispatch]
trait ActionApi {
pub trait ActionApi {
fn height(&self, glyph_height: i16, margin: i16) -> i16 {glyph_height + margin * 2}
fn redraw(&self, _at_height: i16, _modal: &Modal) { unimplemented!() }
fn close(&mut self) {}
fn is_password(&self) -> bool { false }
/// navigation is one of '∴' | '←' | '→' | '↑' | '↓'
fn key_action(&mut self, _key: char) -> (Option<xous_ipc::String::<512>>, bool) {(None, true)}
fn set_action_opcode(&mut self, _op: u32) {}
}
#[enum_dispatch(ActionApi)]
@ -771,7 +868,8 @@ pub enum ActionType {
TextEntry,
RadioButtons,
CheckBoxes,
Slider
Slider,
Notification,
}
//#[derive(Debug)]

View file

@ -9,6 +9,16 @@
"zh": "输入密码\n软件更新\n安全含义:至关重要 ",
"en-audio": "Enter update password. This password is critically important."
},
"rootkeys.already_init": {
"en": "Root keys already initialized.",
"zh": "根密码已经设置",
"en-audio": "Root keys already initialized."
},
"rootkeys.setup": {
"en": "This will init the root keys.\nI'm going to need two\npasswords:\n 1. Device unlock\n 2. Software update",
"zh": "这将初始化根密钥\n需要两个密码\n1.解锁\n2.软件更新",
"en-audio": "This will initialize the root keys. We're going to need two passwords, one for device unlock, and one for software updates."
},
"rootkeys.policy_clear": {
"en": "Clear after use",
"zh": "使用后删除",

View file

@ -6,17 +6,16 @@ pub(crate) const ROOTKEY_MENU_NAME: &'static str = "rootkeys menu";
#[allow(dead_code)]
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Opcode {
/// attempt to initialize keys on a brand new system. Does nothing if the keys are already provisioned.
TryInitKeys,
/// use to check if we've been initialized
KeysInitialized,
TestUx,
/// attempt to initialize keys on a brand new system. Does nothing if the keys are already provisioned.
UxTryInitKeys,
UxRequestBootPassword,
UxPasswordReturn,
UxPolicyReturn,
UxGutter, // NOP for UX calls that require a destination
/// UX opcodes
PasswordModalEntry,
PasswordPolicy,
RaisePolicyMenu,
MenuRedraw,
MenuKeys,
MenuDrop,

View file

@ -326,62 +326,29 @@ impl RootKeys {
}
}
pub fn try_init_keys(&mut self) {
let mut step = 0; // current step
let total = 10; // total number of steps
{ // this block checks if we're already initialized, and aborts if we are
let progress_cid = if let Some(progress_sid) = maybe_progress {
Some(xous::connect(progress_sid).expect("couldn't connect to originator for progress updates"))
} else {
None
};
// first progress update: we could make a progress update
step += 1;
self.update_progress(progress_cid, step, total, false);
self.keyrom.wfo(utra::keyrom::ADDRESS_ADDRESS, KeyRomLocations::CONFIG as u32);
if self.is_initialized() {
self.update_progress(progress_cid, step, total, true);
// don't re-initialize an initialized KEYROM
return;
}
}
pub fn setup_key_init(&mut self) {
// block suspend/resume ops during security-sensitive operations
self.susres.set_suspendable(false).expect("couldn't block suspend/resume");
{ // in this block, keyrom data is copied into RAM.
// make a copy of the KEYROM to hold the new mods, in the sensitive data area
let sensitive_slice = self.sensitive_data.as_slice_mut::<u32>();
for addr in 0..256 {
self.keyrom.wfo(utra::keyrom::ADDRESS_ADDRESS, addr);
sensitive_slice[addr as usize] = self.keyrom.rf(utra::keyrom::DATA_DATA);
}
// the following keys should be provisioned:
// - self-signing private key
// - self-signing public key
// - user root key
// - pepper
// provision the pepper
for keyword in sensitive_slice[KeyRomLocations::PEPPER as usize..(KeyRomLocations::PEPPER + 4) as usize].iter_mut() {
*keyword = self.trng.get_u32().expect("couldn't get random number");
}
// prompt the user for a password
self.cur_password_type = Some(PasswordType::Boot);
// zeroize the RAM-backed data
for data in sensitive_slice.iter_mut() {
*data = 0;
}
// in this block, keyrom data is copied into RAM.
// make a copy of the KEYROM to hold the new mods, in the sensitive data area
let sensitive_slice = self.sensitive_data.as_slice_mut::<u32>();
for addr in 0..256 {
self.keyrom.wfo(utra::keyrom::ADDRESS_ADDRESS, addr);
sensitive_slice[addr as usize] = self.keyrom.rf(utra::keyrom::DATA_DATA);
}
// provision the pepper
for keyword in sensitive_slice[KeyRomLocations::PEPPER as usize..(KeyRomLocations::PEPPER + 4) as usize].iter_mut() {
*keyword = self.trng.get_u32().expect("couldn't get random number");
}
}
pub fn finish_key_init(&mut self) {
let sensitive_slice = self.sensitive_data.as_slice_mut::<u32>();
// zeroize the RAM-backed data
for data in sensitive_slice.iter_mut() {
*data = 0;
}
// re-allow suspend/resume ops
self.susres.set_suspendable(true).expect("couldn't re-allow suspend/resume");
}

View file

@ -44,7 +44,7 @@ impl RootKeys {
}
pub fn conn(&self) -> CID {self.conn}
pub fn get_try_init_keys_op(&self) -> u32 {
Opcode::TryInitKeys.to_u32().unwrap()
Opcode::UxTryInitKeys.to_u32().unwrap()
}
fn ensure_progress_server(&mut self) -> xous::SID {
@ -93,7 +93,7 @@ impl RootKeys {
/// if the KEYROM entries are not 0, it will abort without any user prompt, but with an error code.
pub fn try_init_keys(&mut self) -> Result<(), xous::Error> {
send_message(self.conn,
Message::new_scalar(Opcode::TryInitKeys.to_usize().unwrap(),
Message::new_scalar(Opcode::UxTryInitKeys.to_usize().unwrap(),
0, 0, 0, 0)
).map(|_| ())
}
@ -119,13 +119,13 @@ impl RootKeys {
pub fn self_sign(&mut self, which: ImageType) -> Result<(), xous::Error> {
unimplemented!();
}
/*
pub fn test_ux(&mut self, arg: usize) {
send_message(self.conn,
Message::new_scalar(Opcode::TestUx.to_usize().unwrap(),
arg, 0, 0, 0)
).expect("couldn't send test message");
}
}*/
}
use core::sync::atomic::{AtomicU32, Ordering};

View file

@ -47,13 +47,14 @@ mod implementation {
log::info!("policy updated: {:?}", policy);
}
pub fn hash_and_save_password(&mut self, pw: &str) {
log::info!("got password plaintext: {}, type: {:?}", pw, pw_type);
log::info!("got password plaintext: {}", pw);
}
pub fn try_init_keys(&mut self) {
}
pub fn set_ux_password_type(&mut self, _cur_type: PasswordType) {
}
pub fn is_initialized(&self) -> bool {false}
pub fn setup_key_init(&mut self) {}
}
}
@ -100,21 +101,21 @@ fn xmain() -> ! {
policy_menu.add_item(MenuItem {
name: String::<64>::from_str(t!("rootkeys.policy_keep", xous::LANG)),
action_conn: main_cid,
action_opcode: Opcode::PasswordPolicy.to_u32().unwrap(),
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
action_payload: MenuPayload::Scalar([PasswordRetentionPolicy::AlwaysKeep.to_u32().unwrap(), 0, 0, 0,]),
close_on_select: true,
});
policy_menu.add_item(MenuItem {
name: String::<64>::from_str(t!("rootkeys.policy_suspend", xous::LANG)),
action_conn: main_cid,
action_opcode: Opcode::PasswordPolicy.to_u32().unwrap(),
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
action_payload: MenuPayload::Scalar([PasswordRetentionPolicy::EraseOnSuspend.to_u32().unwrap(), 0, 0, 0,]),
close_on_select: true,
});
policy_menu.add_item(MenuItem {
name: String::<64>::from_str(t!("rootkeys.policy_clear", xous::LANG)),
action_conn: main_cid,
action_opcode: Opcode::PasswordPolicy.to_u32().unwrap(),
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
action_payload: MenuPayload::Scalar([PasswordRetentionPolicy::AlwaysPurge.to_u32().unwrap(), 0, 0, 0,]),
close_on_select: true,
});
@ -123,24 +124,26 @@ fn xmain() -> ! {
Opcode::MenuKeys.to_u32().unwrap(),
Opcode::MenuDrop.to_u32().unwrap());
//xous::create_thread_0(rootkeys_ux_thread).expect("couldn't start rootkeys UX thread");
let password_action = gam::modal::TextEntry {
let password_action = TextEntry {
is_password: true,
visibility: gam::modal::TextEntryVisibility::LastChars,
visibility: TextEntryVisibility::LastChars,
action_conn: main_cid,
action_opcode: Opcode::PasswordModalEntry.to_u32().unwrap(),
action_opcode: Opcode::UxPasswordReturn.to_u32().unwrap(),
action_payload: TextEntryPayload::new(),
validator: None,
};
let mut password_modal = gam::Modal::new(
let mut dismiss_modal_action = Notification::new(main_cid, Opcode::UxGutter.to_u32().unwrap());
dismiss_modal_action.set_is_password(true);
let mut rootkeys_modal = Modal::new(
crate::api::ROOTKEY_MODAL_NAME,
gam::ActionType::TextEntry(password_action),
ActionType::TextEntry(password_action),
Some(t!("rootpass.top", xous::LANG)),
None,
GlyphStyle::Small,
4
);
password_modal.spawn_helper(keys_sid, password_modal.sid,
rootkeys_modal.spawn_helper(keys_sid, rootkeys_modal.sid,
Opcode::ModalRedraw.to_u32().unwrap(),
Opcode::ModalKeys.to_u32().unwrap(),
Opcode::ModalDrop.to_u32().unwrap(),
@ -156,9 +159,6 @@ fn xmain() -> ! {
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
keys.resume();
}),
Some(Opcode::TryInitKeys) => msg_scalar_unpack!(msg, _, _, _, _, {
keys.try_init_keys();
}),
Some(Opcode::KeysInitialized) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
if keys.is_initialized() {
xous::return_scalar(msg.sender, 1).unwrap();
@ -166,13 +166,48 @@ fn xmain() -> ! {
xous::return_scalar(msg.sender, 0).unwrap();
}
}),
Some(Opcode::TestUx) => msg_scalar_unpack!(msg, arg, _, _, _, {
log::debug!("activating password modal");
keys.set_ux_password_type(PasswordType::Boot);
password_modal.activate();
//policy_menu.activate();
// UX flow opcodes
Some(Opcode::UxTryInitKeys) => msg_scalar_unpack!(msg, _, _, _, _, {
// overall flow:
// - setup the init
// - prompt for root password
// - prompt for boot password
// - create the keys
// - write the keys
// - clear the passwords
// - reboot
// the following keys should be provisioned:
// - self-signing private key
// - self-signing public key
// - user root key
// - pepper
if keys.is_initialized() {
dismiss_modal_action.set_action_opcode(Opcode::UxGutter.to_u32().unwrap());
rootkeys_modal.modify(
Some(ActionType::Notification(dismiss_modal_action)),
Some(t!("rootkeys.already_init", xous::LANG)), false,
None, true, None);
} else {
dismiss_modal_action.set_action_opcode(Opcode::UxRequestBootPassword.to_u32().unwrap());
rootkeys_modal.modify(
Some(ActionType::Notification(dismiss_modal_action)),
Some(t!("rootkeys.setup", xous::LANG)), false,
None, true, None);
}
rootkeys_modal.activate();
}),
Some(Opcode::PasswordModalEntry) => {
Some(Opcode::UxRequestBootPassword) => {
keys.setup_key_init();
keys.set_ux_password_type(PasswordType::Boot);
rootkeys_modal.modify(
Some(ActionType::TextEntry(password_action)),
Some(t!("rootpass.top", xous::LANG)), false,
None, true, None
);
rootkeys_modal.activate();
}
Some(Opcode::UxPasswordReturn) => {
let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let mut plaintext_pw = buf.to_original::<gam::modal::TextEntryPayload, _>().unwrap();
@ -181,17 +216,23 @@ fn xmain() -> ! {
buf.volatile_clear();
// always explicitly request a policy decision for rootkeys
xous::send_message(main_cid,
Message::new_scalar(Opcode::RaisePolicyMenu.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't raise policy menu follow-up dialogue");
},
Some(Opcode::RaisePolicyMenu) => {
log::debug!("raising policy menu");
policy_menu.activate();
}
Some(Opcode::PasswordPolicy) => msg_scalar_unpack!(msg, policy_code, _, _, _, {
},
Some(Opcode::UxPolicyReturn) => msg_scalar_unpack!(msg, policy_code, _, _, _, {
keys.update_policy(FromPrimitive::from_usize(policy_code));
}),
/*
Some(Opcode::TestUx) => msg_scalar_unpack!(msg, arg, _, _, _, {
log::debug!("activating password modal");
keys.set_ux_password_type(PasswordType::Boot);
rootkeys_modal.activate();
//policy_menu.activate();
}),*/
Some(Opcode::UxGutter) => {
// an intentional NOP for UX actions that require a destintation but need no action
},
// boilerplate Ux handlers
Some(Opcode::MenuRedraw) => {
@ -210,7 +251,7 @@ fn xmain() -> ! {
panic!("Menu handler for rootkeys quit unexpectedly");
},
Some(Opcode::ModalRedraw) => {
password_modal.redraw();
rootkeys_modal.redraw();
},
Some(Opcode::ModalKeys) => msg_scalar_unpack!(msg, k1, k2, k3, k4, {
let keys = [
@ -219,7 +260,7 @@ fn xmain() -> ! {
if let Some(a) = core::char::from_u32(k3 as u32) {a} else {'\u{0000}'},
if let Some(a) = core::char::from_u32(k4 as u32) {a} else {'\u{0000}'},
];
password_modal.key_event(keys);
rootkeys_modal.key_event(keys);
}),
Some(Opcode::ModalDrop) => {
panic!("Password modal for rootkeys quit unexpectedly")

View file

@ -750,7 +750,7 @@ fn xmain() -> ! {
Some(Opcode::UxSetTime) => msg_scalar_unpack!(msg, _, _, _, _, {
rtc_textentry.action_opcode = Opcode::UxMonth.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)), Some(t!("rtc.month", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}),
Some(Opcode::UxMonth) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -762,7 +762,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxDay.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)),
Some(t!("rtc.day", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxDay) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -774,7 +774,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxYear.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)),
Some(t!("rtc.year", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxYear) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -785,7 +785,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxDayOfWeek.to_u32().unwrap();
modal.modify(Some(RadioButtons(rtc_radiobox)),
Some(t!("rtc.day_of_week", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxDayOfWeek) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -810,7 +810,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxHour.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)),
Some(t!("rtc.hour", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxHour) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -821,7 +821,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxMinute.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)),
Some(t!("rtc.minute", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxMinute) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
@ -832,7 +832,7 @@ fn xmain() -> ! {
rtc_textentry.action_opcode = Opcode::UxSeconds.to_u32().unwrap();
modal.modify(Some(TextEntry(rtc_textentry)),
Some(t!("rtc.seconds", xous::LANG)), false, None, true, None);
gam.raise_modal(crate::RTC_MODAL_NAME).expect("couldn't raise RTC modal");
modal.activate();
}
Some(Opcode::UxSeconds) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };

View file

@ -50,11 +50,11 @@ impl<'a> ShellCmdApi<'a> for Keys {
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"ux" => {
/*"ux" => {
self.rootkeys.test_ux(0);
//debug_here::debug_here!();
write!(ret, "show UX").unwrap();
}
}*/
"usblock" => {
env.llio.debug_usb(Some(true)).unwrap();
write!(ret, "USB debug port locked out; one word at 0x80000000 is disclosable via USB.").unwrap();