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
|
|
|
|
2021-07-29 20:07:10 +00:00
|
|
|
#[cfg(target_os = "none")]
|
|
|
|
|
mod bcrypt;
|
|
|
|
|
|
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;
|
2021-07-29 20:07:10 +00:00
|
|
|
use crate::PasswordRetentionPolicy;
|
2021-07-30 11:04:02 +00:00
|
|
|
use crate::PasswordType;
|
2021-07-25 16:51:22 +00:00
|
|
|
|
2021-07-21 11:47:09 +00:00
|
|
|
pub struct RootKeys {
|
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-19 17:59:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn suspend(&self) {
|
|
|
|
|
}
|
|
|
|
|
pub fn resume(&self) {
|
|
|
|
|
}
|
2021-07-25 16:51:22 +00:00
|
|
|
|
2021-07-29 20:07:10 +00:00
|
|
|
pub fn update_policy(&mut self, policy: Option<PasswordRetentionPolicy>) {
|
|
|
|
|
log::info!("policy updated: {:?}", policy);
|
|
|
|
|
}
|
2021-07-30 11:04:02 +00:00
|
|
|
pub fn hash_and_save_password(&mut self, pw: &str) {
|
2021-07-30 19:27:16 +00:00
|
|
|
log::info!("got password plaintext: {}", pw);
|
2021-07-29 20:07:10 +00:00
|
|
|
}
|
2021-07-30 11:04:02 +00:00
|
|
|
pub fn try_init_keys(&mut self) {
|
2021-07-25 16:51:22 +00:00
|
|
|
}
|
2021-07-30 11:04:02 +00:00
|
|
|
pub fn set_ux_password_type(&mut self, _cur_type: PasswordType) {
|
|
|
|
|
}
|
|
|
|
|
pub fn is_initialized(&self) -> bool {false}
|
2021-07-30 19:27:16 +00:00
|
|
|
pub fn setup_key_init(&mut self) {}
|
2021-07-19 17:59:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 20:07:10 +00:00
|
|
|
// enumerate the possible password types dealt with by this manager
|
2021-07-30 11:04:02 +00:00
|
|
|
// the discriminant is used to every-so-slightly change the salt going into bcrypt
|
|
|
|
|
// I don't think it hurts; but it also prevents an off-the-shelf "hashcat" run from
|
|
|
|
|
// being used to brute force all the passwords in a single go.
|
2021-07-29 20:07:10 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2021-07-30 11:04:02 +00:00
|
|
|
pub enum PasswordType {
|
|
|
|
|
Boot = 1,
|
|
|
|
|
Update = 2,
|
2021-07-29 20:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
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-29 20:07:10 +00:00
|
|
|
3. Key purge timer
|
2021-07-30 11:04:02 +00:00
|
|
|
4. Main menu -> trigger initialization
|
|
|
|
|
5. (future) PDDB
|
2021-07-19 17:59:52 +00:00
|
|
|
*/
|
2021-07-30 11:04:02 +00:00
|
|
|
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(4)).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,
|
2021-07-30 19:27:16 +00:00
|
|
|
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
|
2021-07-28 19:30:03 +00:00
|
|
|
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,
|
2021-07-30 19:27:16 +00:00
|
|
|
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
|
2021-07-28 19:30:03 +00:00
|
|
|
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,
|
2021-07-30 19:27:16 +00:00
|
|
|
action_opcode: Opcode::UxPolicyReturn.to_u32().unwrap(),
|
2021-07-28 19:30:03 +00:00
|
|
|
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());
|
|
|
|
|
|
2021-07-30 19:27:16 +00:00
|
|
|
let password_action = TextEntry {
|
2021-07-28 19:30:03 +00:00
|
|
|
is_password: true,
|
2021-07-30 19:27:16 +00:00
|
|
|
visibility: TextEntryVisibility::LastChars,
|
2021-07-28 19:30:03 +00:00
|
|
|
action_conn: main_cid,
|
2021-07-30 19:27:16 +00:00
|
|
|
action_opcode: Opcode::UxPasswordReturn.to_u32().unwrap(),
|
2021-07-28 19:30:03 +00:00
|
|
|
action_payload: TextEntryPayload::new(),
|
|
|
|
|
validator: None,
|
|
|
|
|
};
|
2021-07-30 19:27:16 +00:00
|
|
|
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(
|
2021-07-28 19:30:03 +00:00
|
|
|
crate::api::ROOTKEY_MODAL_NAME,
|
2021-07-30 19:27:16 +00:00
|
|
|
ActionType::TextEntry(password_action),
|
2021-07-28 19:30:03 +00:00
|
|
|
Some(t!("rootpass.top", xous::LANG)),
|
|
|
|
|
None,
|
|
|
|
|
GlyphStyle::Small,
|
|
|
|
|
4
|
|
|
|
|
);
|
2021-07-30 19:27:16 +00:00
|
|
|
rootkeys_modal.spawn_helper(keys_sid, rootkeys_modal.sid,
|
2021-07-28 19:30:03 +00:00
|
|
|
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();
|
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-30 11:04:02 +00:00
|
|
|
Some(Opcode::KeysInitialized) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
if keys.is_initialized() {
|
|
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
|
|
|
|
}
|
2021-07-21 11:47:09 +00:00
|
|
|
}),
|
2021-07-30 19:27:16 +00:00
|
|
|
|
|
|
|
|
// 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();
|
2021-07-25 16:51:22 +00:00
|
|
|
}),
|
2021-07-30 19:27:16 +00:00
|
|
|
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) => {
|
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-29 20:07:10 +00:00
|
|
|
|
2021-07-30 11:04:02 +00:00
|
|
|
keys.hash_and_save_password(plaintext_pw.as_str());
|
|
|
|
|
plaintext_pw.volatile_clear(); // ensure the data is destroyed after sending to the keys enclave
|
|
|
|
|
buf.volatile_clear();
|
2021-07-29 20:07:10 +00:00
|
|
|
|
2021-07-30 11:04:02 +00:00
|
|
|
// always explicitly request a policy decision for rootkeys
|
2021-07-28 19:30:03 +00:00
|
|
|
policy_menu.activate();
|
2021-07-30 19:27:16 +00:00
|
|
|
},
|
|
|
|
|
Some(Opcode::UxPolicyReturn) => msg_scalar_unpack!(msg, policy_code, _, _, _, {
|
2021-07-30 11:04:02 +00:00
|
|
|
keys.update_policy(FromPrimitive::from_usize(policy_code));
|
2021-07-28 19:30:03 +00:00
|
|
|
}),
|
2021-07-30 19:27:16 +00:00
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2021-07-29 20:07:10 +00:00
|
|
|
|
|
|
|
|
// boilerplate Ux handlers
|
2021-07-28 19:30:03 +00:00
|
|
|
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) => {
|
2021-07-30 19:27:16 +00:00
|
|
|
rootkeys_modal.redraw();
|
2021-07-28 19:30:03 +00:00
|
|
|
},
|
|
|
|
|
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}'},
|
|
|
|
|
];
|
2021-07-30 19:27:16 +00:00
|
|
|
rootkeys_modal.key_event(keys);
|
2021-07-28 19:30:03 +00:00
|
|
|
}),
|
|
|
|
|
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)
|
|
|
|
|
}
|