2021-07-19 17:59:52 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
2021-07-21 11:47:09 +00:00
|
|
|
use api::*;
|
2021-07-25 16:51:22 +00:00
|
|
|
use xous::{CID, msg_scalar_unpack, send_message, Message, msg_blocking_scalar_unpack};
|
|
|
|
|
use xous_ipc::{String, Buffer};
|
2021-07-19 17:59:52 +00:00
|
|
|
|
2021-07-21 11:47:09 +00:00
|
|
|
use num_traits::*;
|
2021-07-19 17:59:52 +00:00
|
|
|
|
2021-07-25 16:51:22 +00:00
|
|
|
use gam::modal::*;
|
2021-07-28 19:30:03 +00:00
|
|
|
use gam::menu::*;
|
2021-07-25 16:51:22 +00:00
|
|
|
|
2021-07-26 20:40:12 +00:00
|
|
|
use locales::t;
|
|
|
|
|
|
2021-07-28 19:30:03 +00:00
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
2021-07-19 17:59:52 +00:00
|
|
|
#[cfg(target_os = "none")]
|
2021-07-21 11:47:09 +00:00
|
|
|
mod implementation;
|
|
|
|
|
#[cfg(target_os = "none")]
|
|
|
|
|
use implementation::*;
|
2021-07-19 17:59:52 +00:00
|
|
|
|
|
|
|
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
|
|
|
|
#[cfg(not(target_os = "none"))]
|
|
|
|
|
mod implementation {
|
2021-07-25 16:51:22 +00:00
|
|
|
use crate::ROOTKEY_MODAL_NAME;
|
|
|
|
|
use gam::modal::*;
|
|
|
|
|
use xous_ipc::String;
|
|
|
|
|
|
2021-07-21 11:47:09 +00:00
|
|
|
pub struct RootKeys {
|
2021-07-25 16:51:22 +00:00
|
|
|
gam: gam::Gam,
|
|
|
|
|
ticktimer: ticktimer_server::Ticktimer,
|
2021-07-19 17:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 11:47:09 +00:00
|
|
|
impl RootKeys {
|
2021-07-25 16:51:22 +00:00
|
|
|
pub fn new(xns: &xous_names::XousNames) -> RootKeys {
|
2021-07-21 11:47:09 +00:00
|
|
|
RootKeys {
|
2021-07-25 16:51:22 +00:00
|
|
|
gam: gam::Gam::new(&xns).expect("couldn't allocate GAM for testing"),
|
|
|
|
|
ticktimer: ticktimer_server::Ticktimer::new().expect("couldn't get ticktimer"),
|
2021-07-19 17:59:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn suspend(&self) {
|
|
|
|
|
}
|
|
|
|
|
pub fn resume(&self) {
|
|
|
|
|
}
|
2021-07-25 16:51:22 +00:00
|
|
|
|
|
|
|
|
pub fn try_init_keys(&mut self, _maybe_progress: Option<xous::SID>) {
|
|
|
|
|
}
|
2021-07-19 17:59:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
2021-07-21 11:47:09 +00:00
|
|
|
use crate::implementation::RootKeys;
|
2021-07-19 17:59:52 +00:00
|
|
|
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-07-28 19:30:03 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Debug);
|
2021-07-21 11:47:09 +00:00
|
|
|
log::info!("my PID is {}", xous::process::id());
|
2021-07-19 17:59:52 +00:00
|
|
|
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
/*
|
|
|
|
|
Connections allowed to the keys server:
|
|
|
|
|
1. Shellchat (to originate update test requests)
|
2021-07-27 09:30:37 +00:00
|
|
|
2. Password entry UX thread
|
2021-07-19 17:59:52 +00:00
|
|
|
2. (future) PDDB
|
|
|
|
|
*/
|
2021-07-27 09:30:37 +00:00
|
|
|
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(2)).expect("can't register server");
|
2021-07-19 17:59:52 +00:00
|
|
|
log::trace!("registered with NS -- {:?}", keys_sid);
|
|
|
|
|
|
2021-07-21 11:47:09 +00:00
|
|
|
let mut keys = RootKeys::new(&xns);
|
2021-07-19 17:59:52 +00:00
|
|
|
|
|
|
|
|
log::trace!("ready to accept requests");
|
|
|
|
|
|
|
|
|
|
// register a suspend/resume listener
|
2021-07-28 19:30:03 +00:00
|
|
|
let main_cid = xous::connect(keys_sid).expect("couldn't create suspend callback connection");
|
|
|
|
|
let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, main_cid).expect("couldn't create suspend/resume object");
|
|
|
|
|
|
|
|
|
|
// create a policy menu object
|
|
|
|
|
let mut policy_menu = gam::menu::Menu::new(crate::ROOTKEY_MENU_NAME);
|
|
|
|
|
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_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_idle", xous::LANG)),
|
|
|
|
|
action_conn: main_cid,
|
|
|
|
|
action_opcode: Opcode::PasswordPolicy.to_u32().unwrap(),
|
|
|
|
|
action_payload: MenuPayload::Scalar([PasswordRetentionPolicy::EraseOnIdle.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_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_payload: MenuPayload::Scalar([PasswordRetentionPolicy::AlwaysPurge.to_u32().unwrap(), 0, 0, 0,]),
|
|
|
|
|
close_on_select: true,
|
|
|
|
|
});
|
|
|
|
|
policy_menu.spawn_helper(keys_sid, policy_menu.sid,
|
|
|
|
|
Opcode::MenuRedraw.to_u32().unwrap(),
|
|
|
|
|
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 {
|
|
|
|
|
is_password: true,
|
|
|
|
|
visibility: gam::modal::TextEntryVisibility::LastChars,
|
|
|
|
|
action_conn: main_cid,
|
|
|
|
|
action_opcode: Opcode::PasswordModalEntry.to_u32().unwrap(),
|
|
|
|
|
action_payload: TextEntryPayload::new(),
|
|
|
|
|
validator: None,
|
|
|
|
|
};
|
|
|
|
|
let mut password_modal = gam::Modal::new(
|
|
|
|
|
crate::api::ROOTKEY_MODAL_NAME,
|
|
|
|
|
gam::ActionType::TextEntry(password_action),
|
|
|
|
|
Some(t!("rootpass.top", xous::LANG)),
|
|
|
|
|
None,
|
|
|
|
|
GlyphStyle::Small,
|
|
|
|
|
4
|
|
|
|
|
);
|
|
|
|
|
password_modal.spawn_helper(keys_sid, password_modal.sid,
|
|
|
|
|
Opcode::ModalRedraw.to_u32().unwrap(),
|
|
|
|
|
Opcode::ModalKeys.to_u32().unwrap(),
|
|
|
|
|
Opcode::ModalDrop.to_u32().unwrap(),
|
|
|
|
|
);
|
2021-07-25 16:51:22 +00:00
|
|
|
|
2021-07-28 19:30:03 +00:00
|
|
|
let mut cur_plaintext = String::<256>::new();
|
|
|
|
|
let mut cur_policy: Option<PasswordRetentionPolicy> = None;
|
2021-07-19 17:59:52 +00:00
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(keys_sid).unwrap();
|
2021-07-25 16:51:22 +00:00
|
|
|
log::trace!("message: {:?}", msg);
|
2021-07-19 17:59:52 +00:00
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
2021-07-21 11:47:09 +00:00
|
|
|
Some(Opcode::SuspendResume) => msg_scalar_unpack!(msg, token, _, _, _, {
|
2021-07-19 17:59:52 +00:00
|
|
|
keys.suspend();
|
|
|
|
|
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
|
|
|
|
|
keys.resume();
|
|
|
|
|
}),
|
2021-07-21 11:47:09 +00:00
|
|
|
Some(Opcode::TryInitKeys) => msg_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
keys.try_init_keys(None);
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::TryInitKeysWithProgress) => msg_scalar_unpack!(msg, s0, s1, s2, s3, {
|
|
|
|
|
let sid = xous::SID::from_u32(s0 as u32, s1 as u32, s2 as u32, s3 as u32);
|
|
|
|
|
keys.try_init_keys(Some(sid));
|
|
|
|
|
}),
|
2021-07-28 19:30:03 +00:00
|
|
|
Some(Opcode::TestUx) => msg_scalar_unpack!(msg, arg, _, _, _, {
|
|
|
|
|
log::debug!("activating password modal");
|
|
|
|
|
password_modal.activate();
|
|
|
|
|
//policy_menu.activate();
|
2021-07-25 16:51:22 +00:00
|
|
|
}),
|
2021-07-27 09:30:37 +00:00
|
|
|
Some(Opcode::PasswordModalEntry) => {
|
2021-07-28 15:00:25 +00:00
|
|
|
let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
2021-07-27 09:30:37 +00:00
|
|
|
let mut plaintext_pw = buf.to_original::<gam::modal::TextEntryPayload, _>().unwrap();
|
2021-07-28 19:30:03 +00:00
|
|
|
write!(cur_plaintext, "{}", plaintext_pw.as_str()).expect("couldn't copy plaintext password");
|
2021-07-27 09:30:37 +00:00
|
|
|
plaintext_pw.volatile_clear(); // ensure the data is destroyed after printing
|
2021-07-28 15:00:25 +00:00
|
|
|
buf.volatile_clear();
|
2021-07-28 19:30:03 +00:00
|
|
|
log::info!("cur_plaintext: {}", cur_plaintext);
|
|
|
|
|
// now explicitly request a policy decision
|
|
|
|
|
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) => {
|
|
|
|
|
xous::yield_slice(); // let any pending redraws happen before raising this menu
|
|
|
|
|
log::debug!("raising policy menu");
|
|
|
|
|
policy_menu.activate();
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::PasswordPolicy) => msg_scalar_unpack!(msg, policy_code, _, _, _, {
|
|
|
|
|
cur_policy = FromPrimitive::from_usize(policy_code);
|
|
|
|
|
log::info!("cur_policy: {:?}", cur_policy);
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::MenuRedraw) => {
|
|
|
|
|
policy_menu.redraw();
|
|
|
|
|
},
|
|
|
|
|
Some(Opcode::MenuKeys) => msg_scalar_unpack!(msg, k1, k2, k3, k4, {
|
|
|
|
|
let keys = [
|
|
|
|
|
if let Some(a) = core::char::from_u32(k1 as u32) {a} else {'\u{0000}'},
|
|
|
|
|
if let Some(a) = core::char::from_u32(k2 as u32) {a} else {'\u{0000}'},
|
|
|
|
|
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}'},
|
|
|
|
|
];
|
|
|
|
|
policy_menu.key_event(keys);
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::MenuDrop) => {
|
|
|
|
|
panic!("Menu handler for rootkeys quit unexpectedly");
|
|
|
|
|
},
|
|
|
|
|
Some(Opcode::ModalRedraw) => {
|
|
|
|
|
password_modal.redraw();
|
|
|
|
|
},
|
|
|
|
|
Some(Opcode::ModalKeys) => msg_scalar_unpack!(msg, k1, k2, k3, k4, {
|
|
|
|
|
let keys = [
|
|
|
|
|
if let Some(a) = core::char::from_u32(k1 as u32) {a} else {'\u{0000}'},
|
|
|
|
|
if let Some(a) = core::char::from_u32(k2 as u32) {a} else {'\u{0000}'},
|
|
|
|
|
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);
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::ModalDrop) => {
|
|
|
|
|
panic!("Password modal for rootkeys quit unexpectedly")
|
2021-07-27 09:30:37 +00:00
|
|
|
}
|
2021-07-25 16:51:22 +00:00
|
|
|
Some(Opcode::Quit) => {
|
|
|
|
|
log::warn!("password thread received quit, exiting.");
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-07-19 17:59:52 +00:00
|
|
|
None => {
|
|
|
|
|
log::error!("couldn't convert opcode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
xns.unregister_server(keys_sid).unwrap();
|
|
|
|
|
xous::destroy_server(keys_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
|
|
|
|
xous::terminate_process(0)
|
|
|
|
|
}
|