fix restore bugs

- read order of keys was wrong from staged data
 - make restore a blocking op so the Ux doesn't try to trigger
 other things at boot
 - fix pubkey check to use staged key for backup flow
 - fix some wording and skip redundant messages
This commit is contained in:
bunnie 2022-07-17 03:10:26 +08:00
parent ef164871a1
commit 667470ba05
4 changed files with 63 additions and 21 deletions

View file

@ -48,10 +48,10 @@
"en-tts": "Restore record is corrupted. Aborting restore!"
},
"rootkeys.restore_already_init": {
"en": "Device has already been initialized. A restore will overwrite all data, and potentially fail if your device is locked to a different backup key.\n\nProceed?",
"en": "Device has already been initialized. A restore will overwrite root keys, and potentially fail if your device is locked to a different backup key.\n\nProceed?",
"ja": "デバイスはすでに初期化されています。 復元するとすべてのデータが上書きされ、デバイスが別のバックアップキーにロックされていると失敗する可能性があります。\n \n続行しますか?",
"zh": "设备已经初始化。 还原将覆盖所有数据,如果您的设备被锁定到不同的备份密钥,则可能会失败。\n\n是否继续?",
"en-tts": "Device has already been initialized. A restore will overwrite all data, and potentially fail if your device is locked to a different backup key.\n\nProceed?"
"en-tts": "Device has already been initialized. A restore will overwrite root keys, and potentially fail if your device is locked to a different backup key.\n\nProceed?"
},
"rootkeys.backup_key_enter": {
"en": "Please enter your backup key now:",

View file

@ -740,6 +740,17 @@ impl<'a> RootKeys {
}
key
}
/// Reads a 256-bit key at a given index offset
fn read_staged_key_256(&mut self, index: u8) -> [u8; 32] {
let mut key: [u8; 32] = [0; 32];
for (addr, word) in key.chunks_mut(4).into_iter().enumerate() {
let keyword = self.sensitive_data.borrow().as_slice::<u32>()[index as usize + addr];
for (&byte, dst) in keyword.to_be_bytes().iter().zip(word.iter_mut()) {
*dst = byte;
}
}
key
}
/// Returns the `salt` needed for the `bcrypt` routine.
/// This routine handles the special-case of being unitialized: in that case, we need to get
@ -1200,12 +1211,7 @@ impl<'a> RootKeys {
return Err(RootkeyResult::KeyError);
}
let mut enc_signing_key = [0u8; 32];
enc_signing_key.copy_from_slice(
&self.sensitive_data.borrow().as_slice::<u8>()
[KeyRomLocs::SELFSIGN_PRIVKEY as usize * size_of::<u32>()..
KeyRomLocs::SELFSIGN_PRIVKEY as usize * size_of::<u32>() + 32]
);
let enc_signing_key = self.read_staged_key_256(KeyRomLocs::SELFSIGN_PRIVKEY);
for (key, (&enc_key, &pw)) in
keypair_bytes[..ed25519_dalek::SECRET_KEY_LENGTH].iter_mut()
.zip(enc_signing_key.iter().zip(pcache.hashed_update_pw.iter())) {
@ -1215,11 +1221,11 @@ impl<'a> RootKeys {
#[cfg(feature = "hazardous-debug")]
log::debug!("keypair privkey (after anti-rollback): {:x?}", &keypair_bytes[..ed25519_dalek::SECRET_KEY_LENGTH]);
// read in the public key from the staged data
keypair_bytes[ed25519_dalek::SECRET_KEY_LENGTH..].copy_from_slice(
&self.sensitive_data.borrow().as_slice::<u8>()
[KeyRomLocs::SELFSIGN_PUBKEY as usize * size_of::<u32>()..
KeyRomLocs::SELFSIGN_PUBKEY as usize * size_of::<u32>() + 32]
);
for (key, &src) in keypair_bytes[ed25519_dalek::SECRET_KEY_LENGTH..].iter_mut()
.zip(self.read_staged_key_256(KeyRomLocs::SELFSIGN_PUBKEY).iter()) {
*key = src;
}
log::info!("keypair_bytes {:x?}", keypair_bytes);
// Keypair zeroizes the secret key on drop.
let keypair = Keypair::from_bytes(&keypair_bytes).map_err(|_| RootkeyResult::KeyError)?;
#[cfg(feature = "hazardous-debug")]
@ -1472,7 +1478,13 @@ impl<'a> RootKeys {
// check signatures
if keypair.is_some() {
pb.set_percentage(96);
if !self.verify_gateware_self_signature() {
let pubkey = match update_type {
UpdateType::Restore => {
PublicKey::from_bytes(&self.read_staged_key_256(KeyRomLocs::SELFSIGN_PUBKEY)).expect("public key was not valid")
}
_ => PublicKey::from_bytes(&self.read_key_256(KeyRomLocs::SELFSIGN_PUBKEY)).expect("public key was not valid")
};
if !self.verify_gateware_self_signature(&pubkey) {
return Err(RootkeyResult::IntegrityError);
}
// as a sanity check, check the kernel self signature
@ -2259,10 +2271,9 @@ impl<'a> RootKeys {
}
/// This is a fast check on the gateware meant to be called on boot just to confirm that we're using a self-signed gateware
pub fn verify_gateware_self_signature(&mut self) -> bool {
pub fn verify_gateware_self_signature(&mut self, pubkey: &PublicKey) -> bool {
log::info!("verifying gateware self signature");
// read the public key directly out of the keyrom
let pubkey = PublicKey::from_bytes(&self.read_key_256(KeyRomLocs::SELFSIGN_PUBKEY)).expect("public key was not valid");
let gateware_region = self.gateware();
let mut sig_region: [u8; core::mem::size_of::<SignatureInFlash>()] = [0; core::mem::size_of::<SignatureInFlash>()];

View file

@ -143,13 +143,13 @@ impl RootKeys {
}
pub fn do_restore_backup_ux_flow(&self) {
send_message(self.conn,
Message::new_scalar(Opcode::DoRestore.to_usize().unwrap(),
Message::new_blocking_scalar(Opcode::DoRestore.to_usize().unwrap(),
0, 0, 0, 0)
).expect("couldn't send message to root keys");
}
pub fn do_erase_backup(&self) {
send_message(self.conn,
Message::new_scalar(Opcode::EraseBackupBlock.to_usize().unwrap(),
Message::new_blocking_scalar(Opcode::EraseBackupBlock.to_usize().unwrap(),
0, 0, 0, 0)
).expect("couldn't send message to root keys");
}

View file

@ -371,7 +371,7 @@ fn main() -> ! {
use crate::implementation::RootKeys;
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Debug);
log::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
@ -1404,6 +1404,7 @@ fn main() -> ! {
}
Some(Opcode::DoRestore) => {
// check to see if the device was already initialized. If so, warn the user.
deferred_response = Some(msg.sender);
if keys.is_initialized() {
modals.add_list_item(t!("rootkeys.confirm.yes", xous::LANG)).expect("modals error");
modals.add_list_item(t!("rootkeys.confirm.no", xous::LANG)).expect("modals error");
@ -1412,6 +1413,9 @@ fn main() -> ! {
Ok(response) => {
if response == t!("rootkeys.confirm.no", xous::LANG) {
modals.show_notification(t!("rootkeys.restore_abort", xous::LANG), None).ok();
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
continue;
}
}
@ -1419,10 +1423,14 @@ fn main() -> ! {
}
}
// try the '0' key and skip key entry (developers will not have burned an AES key)
let mut showed_zero_key_notice = false;
if let Ok((mut header, ct)) = keys.read_backup() {
if header.op != BackupOp::Restore {
log::warn!("Header op was not Restore. Aborting!");
modals.show_notification(t!("rootkeys.restore_corrupt", xous::LANG), None).ok();
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
continue;
}
let mut restore_key = backups::BackupKey::default(); // default is the 0 key
@ -1441,14 +1449,18 @@ fn main() -> ! {
} else {
// notify of zero key
modals.show_notification(t!("rootkeys.restore_zero_key", xous::LANG), None).ok();
showed_zero_key_notice = true;
}
// actual restore
if let Some(pt) = backups::restore_backup(&restore_key, &ct) {
// everything should match except for the op.
header.op = pt.header.op;
// now check that the two records are identical
if pt.deref() != header.deref() {
if pt.header.deref() != header.deref() {
modals.show_notification(t!("rootkeys.restore_corrupt", xous::LANG), None).ok();
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
continue;
}
let mut restore_rom = backups::KeyRomExport::default();
@ -1477,7 +1489,9 @@ fn main() -> ! {
restore_rom.0[0..8].copy_from_slice(&[0u32; 8]);
}
if restore_key.0 == [0u8; 32] {
modals.show_notification(t!("rootkeys.restore_needs_keyburn", xous::LANG), None).ok();
if !showed_zero_key_notice {
modals.show_notification(t!("rootkeys.restore_needs_keyburn", xous::LANG), None).ok();
}
}
// this will setup the pepper so we can request a password
keys.setup_restore_init(restore_key, restore_rom);
@ -1539,26 +1553,43 @@ fn main() -> ! {
send_message(main_cid,
xous::Message::new_scalar(Opcode::UxTryReboot.to_usize().unwrap(), 0, 0, 0, 0)
).expect("couldn't initiate dialog box");
// don't unblock the caller if we go to the reboot arc
}
Err(RootkeyResult::AlignmentError) => {
modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), None).expect("modals error");
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
}
Err(RootkeyResult::KeyError) => {
modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG), None).expect("modals error");
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
}
Err(RootkeyResult::IntegrityError) => {
modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG), None).expect("modals error");
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
}
Err(RootkeyResult::FlashError) => {
modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG), None).expect("modals error");
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
}
Err(RootkeyResult::StateError) => {
modals.show_notification(t!("rootkeys.wrong_state", xous::LANG), None).expect("modals error");
if let Some(sender) = deferred_response {
xous::return_scalar(sender, 1).ok();
}
}
}
}
Some(Opcode::EraseBackupBlock) => {
keys.erase_backup();
xous::return_scalar(msg.sender, 1).ok();
}
Some(Opcode::IsZeroKey) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
let query = keys.is_zero_key();