From e36a9287cee8fddbc8c7ce7032c7fbb3be9eddb7 Mon Sep 17 00:00:00 2001 From: Patryk Laurent Date: Sat, 14 Jan 2023 15:10:50 -0800 Subject: [PATCH 01/80] add pddb edit (types pddb write ) --- services/gam/src/lib.rs | 19 ++++++++++ services/shellchat/src/cmds/pddb_cmd.rs | 50 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 546584b1d..18265b0d4 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -278,6 +278,25 @@ impl Gam { } } + pub fn type_keys(&self, s: &str) -> Result { + let mut view = s.chars().peekable(); + + while view.peek().is_some() { + let chunk: std::string::String = view.by_ref().take(4).collect(); + let bytes = chunk.as_bytes(); + send_message(self.conn, + Message::new_scalar(Opcode::KeyboardEvent.to_usize().unwrap(), + bytes[0] as usize, + if bytes.len() > 1 {bytes[1]} else {0} as usize, + if bytes.len() > 2 {bytes[2]} else {0} as usize, + if bytes.len() > 3 {bytes[3]} else {0} as usize + ) + ).expect("Couldn't type keys"); + } + + Ok(true) + } + pub fn claim_token(&self, name: &str) -> Result, xous::Error> { let tokenclaim = TokenClaim { token: None, diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 058cdd6b8..0f64a45e5 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -157,6 +157,56 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { write!(ret, "Missing query of form 'dict:key'").unwrap(); } } + "edit" => { + ( || { + let Some(descriptor) = tokens.next() else { + write!(ret, "Missing query of form 'dict:key'").unwrap(); + return; + }; + let Some((dict, keyname)) = descriptor.split_once(':') else { + write!(ret, "Query is of form 'dict:key'").unwrap(); + return; + }; + match self.pddb.get(dict, keyname, None, + false, false, None, None::) { + Ok(mut key) => { + let mut readbuf = [0u8; 512]; // up to the first 512 chars of the key + match key.read(&mut readbuf) { + Ok(len) => { + match std::string::String::from_utf8(readbuf[..len].to_vec()) { + Ok(s) => { + let mut editcmd = "pddb write ".to_string(); + editcmd.push_str(&descriptor); + editcmd.push_str(" "); + editcmd.push_str(&s); + + match _env.gam.type_keys(&editcmd) { + Ok(_) => { + write!(ret, "Edit the value and press enter:").unwrap() + } + _ => { + write!(ret, "Couldn't type out write command.").unwrap() + } + } + + } + _ => { + for &b in readbuf[..len].iter() { + match write!(ret, "Not editing non-string: {:02x} ", b) { + Ok(_) => (), + Err(_) => break, // we can overflow our return buffer returning hex chars + } + } + } + } + } + _ => write!(ret, "Error encountered reading {}:{}", dict, keyname).unwrap() + } + } + _ => write!(ret, "{}:{} not found or other error", dict, keyname).unwrap() + } + })() + } "copy" => { (|| { let Some(srcdescriptor) = tokens.next() else { From 6045f8a05b3d212596d6110217c50c1ecde2d66d Mon Sep 17 00:00:00 2001 From: Patryk Laurent Date: Sun, 15 Jan 2023 08:10:55 -0800 Subject: [PATCH 02/80] type chars rather than bytes --- services/gam/src/lib.rs | 13 ++++++------- services/shellchat/src/cmds/pddb_cmd.rs | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 18265b0d4..cee1a732e 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -278,20 +278,19 @@ impl Gam { } } - pub fn type_keys(&self, s: &str) -> Result { + pub fn type_chars(&self, s: &str) -> Result { let mut view = s.chars().peekable(); while view.peek().is_some() { let chunk: std::string::String = view.by_ref().take(4).collect(); - let bytes = chunk.as_bytes(); send_message(self.conn, Message::new_scalar(Opcode::KeyboardEvent.to_usize().unwrap(), - bytes[0] as usize, - if bytes.len() > 1 {bytes[1]} else {0} as usize, - if bytes.len() > 2 {bytes[2]} else {0} as usize, - if bytes.len() > 3 {bytes[3]} else {0} as usize + chunk.chars().nth(0).unwrap() as usize, + if chunk.len() > 1 {chunk.chars().nth(1).unwrap()} else {'\u{0000}'} as usize, + if chunk.len() > 2 {chunk.chars().nth(2).unwrap()} else {'\u{0000}'} as usize, + if chunk.len() > 3 {chunk.chars().nth(3).unwrap()} else {'\u{0000}'} as usize ) - ).expect("Couldn't type keys"); + ).expect("Couldn't type chars"); } Ok(true) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 0f64a45e5..e85f569e8 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -180,7 +180,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { editcmd.push_str(" "); editcmd.push_str(&s); - match _env.gam.type_keys(&editcmd) { + match _env.gam.type_chars(&editcmd) { Ok(_) => { write!(ret, "Edit the value and press enter:").unwrap() } From a042deb4629700dcab9686ea9fe1f5ad848e1cfc Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Thu, 5 Jan 2023 08:34:05 -0600 Subject: [PATCH 03/80] WIP #287: Adds /heap command to mtxcli Signed-off-by: Tom Marble --- apps/mtxcli/locales/i18n.json | 7 ++++++ apps/mtxcli/src/cmds.rs | 46 +++++++++++++++++++++++------------ apps/mtxcli/src/cmds/heap.rs | 25 +++++++++++++++++++ apps/mtxcli/src/cmds/help.rs | 4 +++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 apps/mtxcli/src/cmds/heap.rs diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index 19509beb7..4c79128ee 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -20,6 +20,13 @@ "ja": "シンプルなMTXCLIデモへようこそ", "zh": "简单的 mtxcli 演示活动" }, + "mtxcli.heap.help": { + "en": "/heap", + "en-tts": "heap", + "fr": "/heap", + "ja": "/heap", + "zh": "/heap" + }, "mtxcli.help.help": { "en": "/help [cmd]", "en-tts": "help [cmd]", diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 2559f73ad..eee2c6f0b 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -26,6 +26,8 @@ const SERVER_KEY: &str = "server"; const TOKEN_KEY: &str = "_token"; const USER_KEY: &str = "user"; const USERNAME_KEY: &str = "username"; +const VERSION_KEY: &str = "_version"; +const CURRENT_VERSION_KEY: &str = "__version"; const HTTPS: &str = "https://"; const SERVER_MATRIX: &str = "https://matrix.org"; @@ -203,23 +205,33 @@ impl CommonEnv { } } + pub fn xous_version(&self) -> String { + // Currently breaks in hosted mode: + // self.ticktimer.get_version() + "v0.9.11".to_string() + } + pub fn get(&mut self, key: &str) -> Result, Error> { - let mut keypath = PathBuf::new(); - keypath.push(MTXCLI_DICT); - if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + if key.eq(CURRENT_VERSION_KEY) { + Ok(Some(self.xous_version())) } else { - log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; - } - keypath.push(key); - if let Ok(mut file)= File::open(keypath) { - let mut value = String::new(); - file.read_to_string(&mut value)?; - // log::info!("get '{}' = '{}'", key, value); - Ok(Some(value)) - } else { - Ok(None) + let mut keypath = PathBuf::new(); + keypath.push(MTXCLI_DICT); + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } + keypath.push(key); + if let Ok(mut file)= File::open(keypath) { + let mut value = String::new(); + file.read_to_string(&mut value)?; + // log::info!("get '{}' = '{}'", key, value); + Ok(Some(value)) + } else { + Ok(None) + } } } @@ -422,6 +434,7 @@ impl CommonEnv { ///// 1. add your module here, and pull its namespace into the local crate mod get; use get::*; +mod heap; use heap::*; mod help; use help::*; mod login; use login::*; mod logout; use logout::*; @@ -434,6 +447,7 @@ pub struct CmdEnv { lastverb: XousString::<256>, ///// 2. declare storage for your command here. get_cmd: Get, + heap_cmd: Heap, help_cmd: Help, login_cmd: Login, logout_cmd: Logout, @@ -471,6 +485,7 @@ impl CmdEnv { lastverb: XousString::<256>::new(), ///// 3. initialize your storage, by calling new() get_cmd: Get::new(&xns), + heap_cmd: Heap::new(&xns), help_cmd: Help::new(&xns), login_cmd: Login::new(&xns), logout_cmd: Logout::new(&xns), @@ -487,6 +502,7 @@ impl CmdEnv { let commands: &mut [& mut dyn ShellCmdApi] = &mut [ ///// 4. add your command to this array, so that it can be looked up and dispatched &mut self.get_cmd, + &mut self.heap_cmd, &mut self.help_cmd, &mut self.login_cmd, &mut self.logout_cmd, diff --git a/apps/mtxcli/src/cmds/heap.rs b/apps/mtxcli/src/cmds/heap.rs new file mode 100644 index 000000000..b8d71ce02 --- /dev/null +++ b/apps/mtxcli/src/cmds/heap.rs @@ -0,0 +1,25 @@ +use crate::{ShellCmdApi,CommonEnv,heap_usage}; +use xous_ipc::String as XousString; +use core::fmt::Write; + +#[derive(Debug)] +pub struct Heap { +} +impl Heap { + pub fn new(_xns: &xous_names::XousNames) -> Self { + Heap { + } + } +} + +impl<'a> ShellCmdApi<'a> for Heap { + cmd_api!(heap); + + fn process(&mut self, _args: XousString::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { + let mut ret = XousString::<1024>::new(); + let heap = heap_usage(); + write!(ret, "heap usage: {}", heap).unwrap(); + log::info!("heap usage: {}", heap); + Ok(Some(ret)) + } +} diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index aa7602646..69a386923 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -30,6 +30,9 @@ impl<'a> ShellCmdApi<'a> for Help { "get" => { write!(ret, "{}", t!("mtxcli.get.help", xous::LANG)).unwrap(); } + "heap" => { + write!(ret, "{}", t!("mtxcli.heap.help", xous::LANG)).unwrap(); + } "help" => { write!(ret, "{}", t!("mtxcli.help.help", xous::LANG)).unwrap(); } @@ -51,6 +54,7 @@ impl<'a> ShellCmdApi<'a> for Help { "" => { write!(ret, "{}\n", t!("mtxcli.help.overview", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.get.help", xous::LANG)).unwrap(); + write!(ret, "{}\n", t!("mtxcli.heap.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.help.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.login.help", xous::LANG)).unwrap(); write!(ret, "{}\n", t!("mtxcli.logout.help", xous::LANG)).unwrap(); From c213dfb74537c93475539c039cafcb84e2ebbd7a Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Fri, 13 Jan 2023 16:52:28 -0600 Subject: [PATCH 04/80] WIP: Fix mtxcli excessive memory usage #287 Adds migrations framework - Migration for v0.9.11-0120: update the filter Filter - narrow filter to only the fields and events for the current room Introduces "read only" variables (e.g. __version). Signed-off-by: Tom Marble --- apps/mtxcli/Cargo.toml | 2 +- apps/mtxcli/src/cmds.rs | 108 +++++++++++------- apps/mtxcli/src/cmds/migrations.rs | 69 +++++++++++ .../src/cmds/migrations/v0_9_11_0120.rs | 26 +++++ apps/mtxcli/src/cmds/web.rs | 79 +++++++------ 5 files changed, 205 insertions(+), 79 deletions(-) create mode 100644 apps/mtxcli/src/cmds/migrations.rs create mode 100644 apps/mtxcli/src/cmds/migrations/v0_9_11_0120.rs diff --git a/apps/mtxcli/Cargo.toml b/apps/mtxcli/Cargo.toml index 159c77fbc..4fad08b42 100644 --- a/apps/mtxcli/Cargo.toml +++ b/apps/mtxcli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mtxcli" -version = "0.1.0" +version = "0.6.0" authors = ["bunnie "] edition = "2018" description = "Matrix chat" diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index eee2c6f0b..53dd2c4a9 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -5,10 +5,11 @@ use locales::t; use std::collections::HashMap; use std::fs::File; -use std::io::{Read, Write as StdWrite, Error}; +use std::io::{Read, Write as StdWrite, Error, ErrorKind}; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; // to help gen_txn_id +mod migrations; use migrations::run_migrations; mod url; mod web; @@ -35,6 +36,24 @@ const SERVER_MATRIX: &str = "https://matrix.org"; const EMPTY: &str = ""; const MTX_TIMEOUT: i32 = 300; // ms +/// Returns a version string which is more likely to string compare +/// correctly vs. another version. FFI please see +/// https://git-scm.com/docs/git-describe#_examples +fn get_version(ticktimer: &ticktimer_server::Ticktimer) -> String { + let xous_version = ticktimer.get_version(); + let v: Vec<&str> = xous_version.split('-').collect(); + if v.len() > 2 { + let n = v[1].parse::().expect("could not parse version"); + let version = format!("{}-{:04}",v[0], n); + log::info!("version={}=", version); + version + } else { + log::error!("ERROR, couldn't find version from xous_version: {}", + xous_version); + xous_version + } +} + /////////////////////////// Common items to all commands pub trait ShellCmdApi<'a> { // user implemented: @@ -90,6 +109,7 @@ pub struct CommonEnv { filter: String, since: String, first_line: bool, + version: String, } impl CommonEnv { @@ -103,24 +123,29 @@ impl CommonEnv { } pub fn set(&mut self, key: &str, value: &str) -> Result<(), Error> { - log::info!("set '{}' = '{}'", key, value); - let mut keypath = PathBuf::new(); - keypath.push(MTXCLI_DICT); - if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + if key.starts_with("__") { + Err(Error::new(ErrorKind::PermissionDenied, + "may not set a variable beginning with __ ")) } else { - log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; + log::info!("set '{}' = '{}'", key, value); + let mut keypath = PathBuf::new(); + keypath.push(MTXCLI_DICT); + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } + keypath.push(key); + File::create(keypath)?.write_all(value.as_bytes())?; + match key { // special case side effects + USER_KEY => { self.set_user(value); } + PASSWORD_KEY => { self.set_password(); } + ROOM_KEY => { self.set_room(); } + _ => { } + } + Ok(()) } - keypath.push(key); - File::create(keypath)?.write_all(value.as_bytes())?; - match key { // special case side effects - USER_KEY => { self.set_user(value); } - PASSWORD_KEY => { self.set_password(); } - ROOM_KEY => { self.set_room(); } - _ => { } - } - Ok(()) } // will log on error (vs. panic) @@ -174,22 +199,27 @@ impl CommonEnv { } pub fn unset(&mut self, key: &str) -> Result<(), Error> { - // log::info!("unset '{}'", key); - let mut keypath = PathBuf::new(); - keypath.push(MTXCLI_DICT); - if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + if key.starts_with("__") { + Err(Error::new(ErrorKind::PermissionDenied, + "may not unset a variable beginning with __ ")) } else { - log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); - std::fs::create_dir_all(&keypath)?; - } - keypath.push(key); - if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); + log::info!("unset '{}'", key); + let mut keypath = PathBuf::new(); + keypath.push(MTXCLI_DICT); + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict '{}' exists", MTXCLI_DICT); + } else { + log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); + std::fs::create_dir_all(&keypath)?; + } + keypath.push(key); + if std::fs::metadata(&keypath).is_ok() { // keypath exists + log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); - std::fs::remove_file(keypath)?; + std::fs::remove_file(keypath)?; + } + Ok(()) } - Ok(()) } // will log on error (vs. panic) @@ -205,15 +235,9 @@ impl CommonEnv { } } - pub fn xous_version(&self) -> String { - // Currently breaks in hosted mode: - // self.ticktimer.get_version() - "v0.9.11".to_string() - } - pub fn get(&mut self, key: &str) -> Result, Error> { if key.eq(CURRENT_VERSION_KEY) { - Ok(Some(self.xous_version())) + Ok(Some(self.version.clone())) } else { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); @@ -383,7 +407,8 @@ impl CommonEnv { if self.filter.len() > 0 { true } else { - if let Some(new_filter) = web::get_filter(&self.user, &self.server, &self.token) { + if let Some(new_filter) = web::get_filter(&self.user, &self.server, + &self.room_id, &self.token) { self.set_debug(FILTER_KEY, &new_filter); self.filter = new_filter; true @@ -458,8 +483,9 @@ pub struct CmdEnv { impl CmdEnv { pub fn new(xns: &xous_names::XousNames) -> CmdEnv { let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); + let version = get_version(&ticktimer); log::info!("creating CommonEnv"); - let common = CommonEnv { + let mut common = CommonEnv { llio: llio::Llio::new(&xns), com: com::Com::new(&xns).expect("could't connect to COM"), codec: codec::Codec::new(&xns).expect("couldn't connect to CODEC"), @@ -477,9 +503,11 @@ impl CmdEnv { room_id: EMPTY.to_string(), filter: EMPTY.to_string(), since: EMPTY.to_string(), - first_line: true + first_line: true, + version, }; log::info!("done creating CommonEnv"); + run_migrations(&mut common); CmdEnv { common_env: common, lastverb: XousString::<256>::new(), diff --git a/apps/mtxcli/src/cmds/migrations.rs b/apps/mtxcli/src/cmds/migrations.rs new file mode 100644 index 000000000..6fa7cd953 --- /dev/null +++ b/apps/mtxcli/src/cmds/migrations.rs @@ -0,0 +1,69 @@ +//! Migrations +//! +//! Runs migrations up to the current version + +use std::io::Error; +use crate::cmds::{CommonEnv,VERSION_KEY}; + +mod v0_9_11_0120; use v0_9_11_0120::*; + +const DEFAULT_VERSION: &str = "0"; + +pub trait MigrationApi<'a> { + // created with migration_api! macro + // returns my version + fn version(&self) -> &'static str; + // checks if the migration should be applied + fn applies(&self, version: &str) -> bool; + // run the migration + fn process(&self, common: &mut CommonEnv) -> Result; +} + +// the argument to this macro is the command verb +#[macro_export] +macro_rules! migration_api { + ($version:expr) => { // NOTE the expr will have literal quotes + fn version(&self) -> &'static str { + $version + } + + fn applies(&self, version: &str) -> bool { + if version < self.version() { + true + } else { + false + } + } + }; +} + +/// Run migrations as needed +pub fn run_migrations(common: &mut CommonEnv) { + let version = common.get_default(VERSION_KEY, DEFAULT_VERSION); + if version.ne(&common.version) { + log::info!("Running migrations from version {} to {}", + version, common.version); + let mut migrations: Vec> = Vec::new(); + migrations.push(Box::new(V0_9_11_0120::new())); + for migration in migrations.iter() { + if migration.applies(&version) { + match migration.process(common) { + Ok(boolean) => { + log::info!("migration competed: {}: {}", + migration.version(), boolean); + if boolean { + common.set(VERSION_KEY, migration.version()) + .expect("cannot set _version"); + } + }, + Err(e) => { + log::error!("error running migration: {}: {:?}", + migration.version(), e); + } + } + } + } + common.set(VERSION_KEY, &common.version.clone()) + .expect("cannot set _version"); + } +} diff --git a/apps/mtxcli/src/cmds/migrations/v0_9_11_0120.rs b/apps/mtxcli/src/cmds/migrations/v0_9_11_0120.rs new file mode 100644 index 000000000..9a1151383 --- /dev/null +++ b/apps/mtxcli/src/cmds/migrations/v0_9_11_0120.rs @@ -0,0 +1,26 @@ +use std::io::Error; + +use crate::cmds::{CommonEnv,FILTER_KEY}; +use crate::cmds::migrations::MigrationApi; +use crate::migration_api; + +#[derive(Debug)] +pub struct V0_9_11_0120 { +} +impl V0_9_11_0120 { + pub fn new() -> Self { + V0_9_11_0120 { + } + } +} + +impl<'a> MigrationApi<'a> for V0_9_11_0120 { + migration_api!("v0.9.11-0120"); + + fn process(&self, common: &mut CommonEnv) -> Result { + log::info!("Running migration for: {}", self.version()); + // we need to provoke the creation of a new/updated filter + common.unset(FILTER_KEY)?; + Ok(true) + } +} diff --git a/apps/mtxcli/src/cmds/web.rs b/apps/mtxcli/src/cmds/web.rs index 6cd988972..258c09212 100644 --- a/apps/mtxcli/src/cmds/web.rs +++ b/apps/mtxcli/src/cmds/web.rs @@ -29,8 +29,8 @@ fn serialize(object: &T) -> Option { Ok(value) => { Some(value) }, - Err(_) => { - // println!("FAILED TO SERIALIZE"); + Err(e) => { + log::error!("ERROR in serialize: {:?}", e); None } } @@ -39,10 +39,6 @@ fn serialize(object: &T) -> Option { pub fn handle_response(maybe_response: Result) -> Option { match maybe_response { Ok(response) => { - // DEBUG - // if let Ok(json) = response.into_string() { - // println!("JSON = \n{}\n", json); - // None if let Ok(body) = response.into_json() { Some(body) } else { @@ -55,27 +51,22 @@ pub fn handle_response(maybe_response: Result) -> Op code (such as 400, 500 etc) */ let err_body = response.into_string().unwrap(); log::error!("ERROR code {} err_body = {}", code, err_body); - // let status_text = "the status text"; - // Err(ureq::Error::Status(code, ureq::Response::new(code, status_text, &body).unwrap())) None } - Err(_) => { - log::error!("Unknown Error"); - // Err(e) + Err(e) => { + log::error!("ERROR in handle_response: {:?}", e); None } } } pub fn get_json(url: &str) -> Result { - // println!("getting json from {}", &url); ureq::get(&url) .set(ACCEPT, ACCEPT_JSON) .call() } pub fn get_json_auth(url: &str, token:&str) -> Result { - // println!("getting json auth from {}", &url); let mut authorization = String::from(BEARER); authorization.push_str(token); ureq::get(&url) @@ -85,14 +76,12 @@ pub fn get_json_auth(url: &str, token:&str) -> Result Result { - // println!("post json to {}", &url); ureq::post(&url) .set(ACCEPT, ACCEPT_JSON) .send_string(request_body) } pub fn post_string_auth(url: &str, request_body: &str, token: &str) -> Result { - // println!("post json auth from {}", &url); let mut authorization = String::from(BEARER); authorization.push_str(token); ureq::post(&url) @@ -102,7 +91,6 @@ pub fn post_string_auth(url: &str, request_body: &str, token: &str) -> Result Result { - // println!("put json auth from {}", &url); let mut authorization = String::from(BEARER); authorization.push_str(token); ureq::put(&url) @@ -225,13 +213,17 @@ pub fn get_room_id(server: &str, room_server: &str, token: &str) -> Option, } impl EventFilter { pub fn new(limit: i32) -> Self { + let mut not_types: Vec = Vec::new(); + not_types.push("*".to_string()); EventFilter { - limit + limit, + not_types, } } } @@ -240,15 +232,19 @@ impl EventFilter { struct RoomEventFilter { limit: i32, types: Vec, + rooms: Vec, } impl RoomEventFilter { - pub fn new(limit: i32, type_0: &str) -> Self { + pub fn new(limit: i32, room_id: &str, type_0: &str) -> Self { let mut types = Vec::new(); types.push(type_0.to_string()); + let mut rooms: Vec = Vec::new(); + rooms.push(room_id.to_string()); RoomEventFilter { limit, types, + rooms, } } } @@ -256,21 +252,25 @@ impl RoomEventFilter { #[derive(Serialize, Deserialize)] struct RoomFilter { account_data: EventFilter, // Should be RoomEventFilter - state: EventFilter, // Should be StateFilter ephemeral: EventFilter, + rooms: Vec, + state: EventFilter, // Should be StateFilter timeline: RoomEventFilter, } impl RoomFilter { - pub fn new() -> Self { + pub fn new(room_id: &str) -> Self { let account_data = EventFilter::new(0); - let state = EventFilter::new(0); let ephemeral = EventFilter::new(0); - let timeline = RoomEventFilter::new(10, "m.room.message"); + let mut rooms: Vec = Vec::new(); + rooms.push(room_id.to_string()); + let state = EventFilter::new(0); + let timeline = RoomEventFilter::new(10, room_id, "m.room.message"); RoomFilter { account_data, - state, ephemeral, + rooms, + state, timeline, } } @@ -278,38 +278,44 @@ impl RoomFilter { #[derive(Serialize, Deserialize)] struct FilterRequest { - presence: EventFilter, account_data: EventFilter, + event_fields: Vec, + presence: EventFilter, room: RoomFilter, } impl FilterRequest { - pub fn new() -> Self { - let presence = EventFilter::new(0); + pub fn new(room_id: &str) -> Self { let account_data = EventFilter::new(0); - let room = RoomFilter::new(); + let mut event_fields: Vec = Vec::new(); + event_fields.push("type".to_string()); + event_fields.push("sender".to_string()); + event_fields.push("content.body".to_string()); + let presence = EventFilter::new(0); + let room = RoomFilter::new(room_id); FilterRequest { - presence, account_data, - room + event_fields, + presence, + room, } } } -pub fn get_filter(user: &str, server: &str, token: &str) -> Option { +pub fn get_filter(user: &str, server: &str, room_id: &str, token: &str) + -> Option { let user_encoded = url::encode(user); let mut url = String::from(server); - url.push_str("/_matrix/client/r0/user/"); + url.push_str("/_matrix/client/v3/user/"); url.push_str(&user_encoded); url.push_str("/filter"); log::debug!("get_filter = {}", url); - let filter_request = FilterRequest::new(); + let filter_request = FilterRequest::new(room_id); if let Some(request_body) = serialize(&filter_request) { - // println!("filter_request = {}", request_body); if let Some(value) = handle_response(post_string_auth(&url, &request_body, token)) { if let Value::Object(body) = value { if let Some(Value::String(filter_id)) = body.get("filter_id") { - // log::debug!("filter_id = {}", filter_id); + log::debug!("filter_id = {}", filter_id); Some(filter_id.to_string()) } else { log::error!("invalid response for get_filter"); @@ -384,7 +390,6 @@ pub fn client_sync(server: &str, filter: &str, since: &str, timeout: i32, } log::debug!("client_sync = {}", url); if let Some(value) = handle_response(get_json_auth(&url, token)) { - // println!("SYNC = {:?}", value); if let Value::Object(body) = value { if let Some(Value::String(next_batch)) = body.get("next_batch") { Some((next_batch.to_string(), get_messages(body, room_id))) @@ -429,10 +434,8 @@ pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token log::debug!("send_message = {}", url); let message_request = MessageRequest::new(text); if let Some(request_body) = serialize(&message_request) { - // println!("request_body = {}", request_body); if let Some(value) = handle_response(put_string_auth(&url, &request_body, token)) { if let Value::Object(_body) = value { - // println!("SENT = {:?}", body); true } else { log::error!("invalid response for send_message"); From e9f21396aa2432c56b1347c068de3a027d07b4e6 Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Tue, 17 Jan 2023 10:11:34 -0600 Subject: [PATCH 05/80] Completes fix for #287 Adds migrations framework Delays mtxcli initialization until first user input, because we know: - PDDB is mounted - Timer is stable - User actually using mtxcli Adds send_async_msg scalar_async_msg - basis for automatically updating chatroom Signed-off-by: Tom Marble --- Cargo.lock | 2 +- apps/mtxcli/README.md | 13 ++ apps/mtxcli/locales/i18n.json | 21 ++++ apps/mtxcli/src/cmds.rs | 192 ++++++++++++++++++++--------- apps/mtxcli/src/cmds/get.rs | 2 +- apps/mtxcli/src/cmds/heap.rs | 2 +- apps/mtxcli/src/cmds/help.rs | 35 +++++- apps/mtxcli/src/cmds/login.rs | 2 +- apps/mtxcli/src/cmds/logout.rs | 2 +- apps/mtxcli/src/cmds/migrations.rs | 14 ++- apps/mtxcli/src/cmds/set.rs | 2 +- apps/mtxcli/src/cmds/status.rs | 2 +- apps/mtxcli/src/cmds/unset.rs | 2 +- apps/mtxcli/src/mtxcli.rs | 11 +- 14 files changed, 225 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e74932c99..f206184e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2240,7 +2240,7 @@ dependencies = [ [[package]] name = "mtxcli" -version = "0.1.0" +version = "0.6.0" dependencies = [ "codec", "com", diff --git a/apps/mtxcli/README.md b/apps/mtxcli/README.md index a6378e9a1..9cefcfa89 100644 --- a/apps/mtxcli/README.md +++ b/apps/mtxcli/README.md @@ -62,6 +62,19 @@ Xous please see the [locales README](https://github.com/betrusted-io/xous-core/b ![screenshot](xous-french.png) +## Troubleshooting + +If you see the message `WARNING: clock not set` that is likely because +the Precursor real time clock needs to be set (e.g. if the battery +has been completely discharged). Please go to the menu +**Preferences | Set Timezone** to set the time zone +(and update the time via NTP). + +If you see the message `authentication failed` it might be because +the `user` and `password` variables are not set properly. +Alternatively it could be because TLS certificate validation +has failed because the clock has not been set (see above). + ## Acknowledgements This app is supported in part by a grant from the diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index 4c79128ee..a6dc0fdf4 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -1,4 +1,11 @@ { + "mtxcli.clock.warning": { + "en": "WARNING: clock not set", + "en-tts": "WARNING: clock not set", + "fr": "AVERTISSEMENT : horloge non réglée", + "ja": "WARNING: clock not set *EN*", + "zh": "WARNING: clock not set *EN*" + }, "mtxcli.filter.failed": { "en": "error: could not create filter", "en-tts": "error: could not create filter", @@ -83,6 +90,13 @@ "ja": "/logout *EN*", "zh": "/logout *EN*" }, + "mtxcli.migration.completed": { + "en": "migration completed", + "en-tts": "migration completed", + "fr": "migration terminée", + "ja": "migration completed *EN*", + "zh": "migration completed *EN*" + }, "mtxcli.not.connected": { "en": "not connected", "en-tts": "not conntected", @@ -125,6 +139,13 @@ "ja": "error: could not find room_id *EN*", "zh": "error: could not find room_id *EN*" }, + "mtxcli.running.migrations": { + "en": "Running migrations from version", + "en-tts": "Running migrations from version", + "fr": "Exécution des migrations à partir de la version", + "ja": "Running migrations from version *EN*", + "zh": "Running migrations from version *EN*" + }, "mtxcli.send.failed": { "en": "failed to send!", "en-tts": "failed to send!", diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 53dd2c4a9..caca54299 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -1,4 +1,4 @@ -use xous::{MessageEnvelope}; +use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; use locales::t; @@ -8,11 +8,13 @@ use std::fs::File; use std::io::{Read, Write as StdWrite, Error, ErrorKind}; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; // to help gen_txn_id +use core::str::FromStr; mod migrations; use migrations::run_migrations; mod url; mod web; +const DATE_2023: u64 = 1672538401; // for clock_not_set() const APP: &str = "mtxcli"; /// PDDB Dict for mtxcli keys @@ -36,6 +38,8 @@ const SERVER_MATRIX: &str = "https://matrix.org"; const EMPTY: &str = ""; const MTX_TIMEOUT: i32 = 300; // ms +pub const CLOCK_NOT_SET_ID: usize = 1; + /// Returns a version string which is more likely to string compare /// correctly vs. another version. FFI please see /// https://git-scm.com/docs/git-describe#_examples @@ -90,14 +94,11 @@ macro_rules! cmd_api { use trng::*; /////////////////////////// Command shell integration #[derive(Debug)] -#[allow(dead_code)] // there's more in the envornment right now than we need for the demo pub struct CommonEnv { - llio: llio::Llio, - com: com::Com, - codec: codec::Codec, ticktimer: ticktimer_server::Ticktimer, - gam: gam::Gam, cb_registrations: HashMap::>, + async_msg_callback_id: u32, + async_msg_conn: u32, trng: Trng, xns: xous_names::XousNames, user: String, @@ -110,8 +111,72 @@ pub struct CommonEnv { since: String, first_line: bool, version: String, + initialized: bool, } impl CommonEnv { + pub fn new() -> CommonEnv { + let xns = xous_names::XousNames::new().unwrap(); + let ticktimer = ticktimer_server::Ticktimer::new() + .expect("Couldn't connect to Ticktimer"); + let cb_registrations = HashMap::new(); + let async_msg_callback_id: u32 = 0; + let async_msg_conn: u32 = 0; + let common = CommonEnv { + ticktimer, + cb_registrations, + async_msg_callback_id, + async_msg_conn, + trng: Trng::new(&xns).unwrap(), + xns, + user: EMPTY.to_string(), + username: EMPTY.to_string(), + server: SERVER_MATRIX.to_string(), + token: EMPTY.to_string(), + logged_in: false, + room_id: EMPTY.to_string(), + filter: EMPTY.to_string(), + since: EMPTY.to_string(), + first_line: true, + version: EMPTY.to_string(), + initialized: false, + }; + common + } + + pub fn register_handler(&mut self, verb: XousString::<256>) -> u32 { + let mut key: u32; + loop { + key = self.trng.get_u32().unwrap(); + // reserve the bottom 1000 IDs for the main loop enums. + if !self.cb_registrations.contains_key(&key) && (key > 1000) { + break; + } + } + self.cb_registrations.insert(key, verb); + key + } + + // NOTE: Here the async callbacks will be managed by the command "help" + // as it is already an unsual command + pub fn register_async_msg(&mut self) { + self.async_msg_conn = self.xns.request_connection_blocking(crate::SERVER_NAME_MTXCLI).unwrap(); + self.async_msg_callback_id = self.register_handler(XousString::<256>::from_str("help")); + } + + pub fn scalar_async_msg(&self, async_msg_id: usize) { + let msg = Message::new_scalar(self.async_msg_callback_id as usize, + 0, 0, 0, async_msg_id); + xous::send_message(self.async_msg_conn, msg).unwrap(); + } + + pub fn send_async_msg(&self, async_msg: &str) { + let str_buf = StringBuffer::from_str(async_msg) + .expect("unable to create string message"); + str_buf.send(self.async_msg_conn, self.async_msg_callback_id) + .expect("unable to send string message"); + // let msg = str_buf.create_memory_message(self.async_msg_callback_id); + // xous::send_message(self.async_msg_conn, msg).unwrap(); + } pub fn gen_txn_id(&mut self) -> String { let mut txn_id = self.trng.get_u32() @@ -131,7 +196,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -139,9 +204,14 @@ impl CommonEnv { keypath.push(key); File::create(keypath)?.write_all(value.as_bytes())?; match key { // special case side effects - USER_KEY => { self.set_user(value); } + FILTER_KEY => { self.filter = value.to_string(); } PASSWORD_KEY => { self.set_password(); } + ROOM_ID_KEY => { self.room_id = value.to_string(); } ROOM_KEY => { self.set_room(); } + SERVER_KEY => { self.server = value.to_string(); } + SINCE_KEY => { self.since = value.to_string(); } + USERNAME_KEY => { self.username = value.to_string(); } + USER_KEY => { self.set_user(value); } _ => { } } Ok(()) @@ -194,8 +264,11 @@ impl CommonEnv { pub fn set_room(&mut self) { log::debug!("# ROOM_KEY set '{}' => clearing ROOM_ID_KEY, SINCE_KEY, FILTER_KEY", ROOM_KEY); self.unset_debug(ROOM_ID_KEY); + self.room_id = EMPTY.to_string(); self.unset_debug(SINCE_KEY); + self.since = EMPTY.to_string(); self.unset_debug(FILTER_KEY); + self.filter = EMPTY.to_string(); } pub fn unset(&mut self, key: &str) -> Result<(), Error> { @@ -207,7 +280,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -218,6 +291,15 @@ impl CommonEnv { std::fs::remove_file(keypath)?; } + match key { // special case side effects -- update cached values + FILTER_KEY => { self.filter = EMPTY.to_string(); } + ROOM_ID_KEY => { self.room_id = EMPTY.to_string(); } + SINCE_KEY => { self.since = EMPTY.to_string(); } + SERVER_KEY => { self.server = EMPTY.to_string(); } + USER_KEY => { self.user = EMPTY.to_string(); } + USERNAME_KEY => { self.username = EMPTY.to_string(); } + _ => { } + } Ok(()) } } @@ -242,7 +324,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -481,45 +563,21 @@ pub struct CmdEnv { unset_cmd: Unset, } impl CmdEnv { - pub fn new(xns: &xous_names::XousNames) -> CmdEnv { - let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); - let version = get_version(&ticktimer); - log::info!("creating CommonEnv"); - let mut common = CommonEnv { - llio: llio::Llio::new(&xns), - com: com::Com::new(&xns).expect("could't connect to COM"), - codec: codec::Codec::new(&xns).expect("couldn't connect to CODEC"), - ticktimer, - gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"), - cb_registrations: HashMap::new(), - trng: Trng::new(&xns).unwrap(), - xns: xous_names::XousNames::new().unwrap(), - user: EMPTY.to_string(), - username: EMPTY.to_string(), - - server: SERVER_MATRIX.to_string(), - token: EMPTY.to_string(), - logged_in: false, - room_id: EMPTY.to_string(), - filter: EMPTY.to_string(), - since: EMPTY.to_string(), - first_line: true, - version, - }; - log::info!("done creating CommonEnv"); - run_migrations(&mut common); + pub fn new() -> CmdEnv { + let mut common_env = CommonEnv::new(); + common_env.register_async_msg(); CmdEnv { - common_env: common, + common_env, lastverb: XousString::<256>::new(), ///// 3. initialize your storage, by calling new() - get_cmd: Get::new(&xns), - heap_cmd: Heap::new(&xns), - help_cmd: Help::new(&xns), - login_cmd: Login::new(&xns), - logout_cmd: Logout::new(&xns), - set_cmd: Set::new(&xns), - status_cmd: Status::new(&xns), - unset_cmd: Unset::new(&xns), + get_cmd: Get::new(), + heap_cmd: Heap::new(), + help_cmd: Help::new(), + login_cmd: Login::new(), + logout_cmd: Logout::new(), + set_cmd: Set::new(), + status_cmd: Status::new(), + unset_cmd: Unset::new(), } } @@ -539,16 +597,27 @@ impl CmdEnv { &mut self.unset_cmd, ]; - if self.common_env.username.len() == 0 { // first time initialization - self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); - self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); - self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); - self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); - self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); - self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); - } - if let Some(cmdline) = maybe_cmdline { + // Initialization (must wait until PDDB is mounted and llio can + // report accurate time). Here we wait to check for initialization + // once the user has typed something + if ! self.initialized { + log::info!("initializing"); + if clock_not_set() { + self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID); + } + self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); + self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); + self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); + self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); + self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); + self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); + self.common_env.version = get_version(&self.common_env.ticktimer); + + run_migrations(&mut self.common_env); + self.initialized = true; + } + let maybe_verb = tokenize(cmdline); let mut cmd_ret: Result>, xous::Error> = Ok(None); @@ -677,3 +746,16 @@ pub(crate) fn heap_usage() -> usize { }, } } + + +pub fn clock_not_set() -> bool { + let now = SystemTime::now(); + let since_the_epoch = now.duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + let seconds: u64 = since_the_epoch.as_secs(); + if seconds < DATE_2023 { + true + } else { + false + } +} diff --git a/apps/mtxcli/src/cmds/get.rs b/apps/mtxcli/src/cmds/get.rs index b2904f23e..037ee4ee9 100644 --- a/apps/mtxcli/src/cmds/get.rs +++ b/apps/mtxcli/src/cmds/get.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Get { } impl Get { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Get { } } diff --git a/apps/mtxcli/src/cmds/heap.rs b/apps/mtxcli/src/cmds/heap.rs index b8d71ce02..2a4f4c0f1 100644 --- a/apps/mtxcli/src/cmds/heap.rs +++ b/apps/mtxcli/src/cmds/heap.rs @@ -6,7 +6,7 @@ use core::fmt::Write; pub struct Heap { } impl Heap { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Heap { } } diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index 69a386923..3d8f4850f 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -1,4 +1,6 @@ -use crate::{ShellCmdApi,CommonEnv}; +use crate::{ShellCmdApi,CommonEnv, + cmds::CLOCK_NOT_SET_ID}; +use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; use locales::t; @@ -7,7 +9,7 @@ use locales::t; pub struct Help { } impl Help { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Help { } } @@ -68,7 +70,34 @@ impl<'a> ShellCmdApi<'a> for Help { } } } - Ok(Some(ret)) } + + // NOTE: the help callback is used to process async messages + fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { + match &msg.body { + Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _, + arg3: _, arg4: async_msg_id}) => { + if *async_msg_id == CLOCK_NOT_SET_ID { + let mut ret = XousString::<1024>::new(); + let warning = t!("mtxcli.clock.warning", xous::LANG); + write!(ret, "{}", warning).unwrap(); + log::warn!("{}", warning); + return Ok(Some(ret)); + } + }, + Message::Move(mm) => { + let str_buf = unsafe { StringBuffer::from_memory_message(mm) }; + let msg = str_buf.to_str(); + let mut ret = XousString::<1024>::new(); + write!(ret, "{}", msg).unwrap(); + // log::info!("async message \"{}\"", msg); + return Ok(Some(ret)); + }, + _ => { + log::error!("received unknown callback type: {:?}", msg) + } + } + Ok(None) + } } diff --git a/apps/mtxcli/src/cmds/login.rs b/apps/mtxcli/src/cmds/login.rs index c0f463cfd..4ae0f32e3 100644 --- a/apps/mtxcli/src/cmds/login.rs +++ b/apps/mtxcli/src/cmds/login.rs @@ -5,7 +5,7 @@ use xous_ipc::String as XousString; pub struct Login { } impl Login { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Login { } } diff --git a/apps/mtxcli/src/cmds/logout.rs b/apps/mtxcli/src/cmds/logout.rs index 5e490d8b4..af1fc7e5d 100644 --- a/apps/mtxcli/src/cmds/logout.rs +++ b/apps/mtxcli/src/cmds/logout.rs @@ -5,7 +5,7 @@ use xous_ipc::String as XousString; pub struct Logout { } impl Logout { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Logout { } } diff --git a/apps/mtxcli/src/cmds/migrations.rs b/apps/mtxcli/src/cmds/migrations.rs index 6fa7cd953..1b610a92e 100644 --- a/apps/mtxcli/src/cmds/migrations.rs +++ b/apps/mtxcli/src/cmds/migrations.rs @@ -4,6 +4,7 @@ use std::io::Error; use crate::cmds::{CommonEnv,VERSION_KEY}; +use locales::t; mod v0_9_11_0120; use v0_9_11_0120::*; @@ -41,16 +42,21 @@ macro_rules! migration_api { pub fn run_migrations(common: &mut CommonEnv) { let version = common.get_default(VERSION_KEY, DEFAULT_VERSION); if version.ne(&common.version) { - log::info!("Running migrations from version {} to {}", - version, common.version); + let running_migrations = t!("mtxcli.running.migrations", xous::LANG); + let msg = format!("{} {} .. {}", + running_migrations, version, common.version); + log::info!("{}", msg); + common.send_async_msg(&msg); let mut migrations: Vec> = Vec::new(); migrations.push(Box::new(V0_9_11_0120::new())); for migration in migrations.iter() { if migration.applies(&version) { match migration.process(common) { Ok(boolean) => { - log::info!("migration competed: {}: {}", - migration.version(), boolean); + let migration_completed = t!("mtxcli.migration.completed", xous::LANG); + let msg = format!("{}: {}: {}", + migration_completed, migration.version(), boolean); + common.send_async_msg(&msg); if boolean { common.set(VERSION_KEY, migration.version()) .expect("cannot set _version"); diff --git a/apps/mtxcli/src/cmds/set.rs b/apps/mtxcli/src/cmds/set.rs index b0c23dda2..1513ea380 100644 --- a/apps/mtxcli/src/cmds/set.rs +++ b/apps/mtxcli/src/cmds/set.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Set { } impl Set { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Set { } } diff --git a/apps/mtxcli/src/cmds/status.rs b/apps/mtxcli/src/cmds/status.rs index 3ef2f106d..05990973d 100644 --- a/apps/mtxcli/src/cmds/status.rs +++ b/apps/mtxcli/src/cmds/status.rs @@ -6,7 +6,7 @@ use locales::t; pub struct Status { } impl Status { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Status { } } diff --git a/apps/mtxcli/src/cmds/unset.rs b/apps/mtxcli/src/cmds/unset.rs index 60543e8ba..9b5fd4325 100644 --- a/apps/mtxcli/src/cmds/unset.rs +++ b/apps/mtxcli/src/cmds/unset.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Unset { } impl Unset { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Unset { } } diff --git a/apps/mtxcli/src/mtxcli.rs b/apps/mtxcli/src/mtxcli.rs index 1d9ba6134..7d6655ca2 100644 --- a/apps/mtxcli/src/mtxcli.rs +++ b/apps/mtxcli/src/mtxcli.rs @@ -14,7 +14,6 @@ struct History { pub is_input: bool, } -#[allow(dead_code)] pub(crate) struct Mtxcli { // optional structures that indicate new input to the Mtxcli loop per iteration // an input string @@ -38,9 +37,6 @@ pub(crate) struct Mtxcli { // command environment env: CmdEnv, - - // our security token for making changes to our record on the GAM - token: [u32; 4], } impl Mtxcli{ pub(crate) fn new(xns: &xous_names::XousNames, sid: xous::SID) -> Self { @@ -60,10 +56,12 @@ impl Mtxcli{ let content = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas"); let screensize = gam.get_canvas_bounds(content).expect("couldn't get dimensions of content canvas"); + let history: Vec:: = vec![History{text: String::from(t!("mtxcli.greeting", xous::LANG)), is_input: false}]; + let env = CmdEnv::new(); Mtxcli { input: None, msg: None, - history: vec![History{text: String::from(t!("mtxcli.greeting", xous::LANG)), is_input: false}], + history, history_len: 10, content, gam, @@ -73,8 +71,7 @@ impl Mtxcli{ bubble_margin: Point::new(4, 4), bubble_radius: 4, bubble_space: 4, - env: CmdEnv::new(xns), - token: token.unwrap(), + env, } } From 277d014f39f13a02e3a9dbf80344add58e89b336 Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Tue, 17 Jan 2023 12:19:33 -0600 Subject: [PATCH 06/80] Updates for #287 Allows a variable to be set to the EMPTY string - by not setting a value `/set myvar` During initialization now verifies that PDDB is mounted. Added warnings for PDDB not mounted and WiFi not connected. - the WiFi check is not yet implemented Signed-off-by: Tom Marble --- Cargo.lock | 1 + apps/mtxcli/Cargo.toml | 2 ++ apps/mtxcli/README.md | 4 +++ apps/mtxcli/locales/i18n.json | 14 +++++++++++ apps/mtxcli/src/cmds.rs | 47 +++++++++++++++++++++++------------ apps/mtxcli/src/cmds/help.rs | 27 ++++++++++++++------ apps/mtxcli/src/cmds/set.rs | 15 ++++++++--- 7 files changed, 83 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f206184e3..13dad3927 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2254,6 +2254,7 @@ dependencies = [ "log", "num-derive", "num-traits", + "pddb", "percent-encoding", "serde", "trng", diff --git a/apps/mtxcli/Cargo.toml b/apps/mtxcli/Cargo.toml index 4fad08b42..2e8342b1c 100644 --- a/apps/mtxcli/Cargo.toml +++ b/apps/mtxcli/Cargo.toml @@ -29,6 +29,8 @@ codec = {path = "../../services/codec"} percent-encoding = "2.2" serde = { version = "1.0", features = [ "derive" ] } ureq = { version = "2.5", features = ["json"] } +# Xous APIs +pddb = {path = "../../services/pddb" } [features] default = [] diff --git a/apps/mtxcli/README.md b/apps/mtxcli/README.md index 9cefcfa89..4b564ef8f 100644 --- a/apps/mtxcli/README.md +++ b/apps/mtxcli/README.md @@ -64,6 +64,10 @@ Xous please see the [locales README](https://github.com/betrusted-io/xous-core/b ## Troubleshooting +Prequisites: it is essential that the PDDB is mounted and WiFi is +connected before running mtxcli. You can verify these in Shellchat +with `net ping 1.1.1.1`. + If you see the message `WARNING: clock not set` that is likely because the Precursor real time clock needs to be set (e.g. if the battery has been completely discharged). Please go to the menu diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index a6dc0fdf4..5f9b384cc 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -104,6 +104,13 @@ "ja": "not connected *EN*", "zh": "not connected *EN*" }, + "mtxcli.pddb.warning": { + "en": "WARNING: pddb not mounted", + "en-tts": "WARNING: pddb not mounted", + "fr": "AVERTISSEMENT: pddb pas monté", + "ja": "WARNING: pddb not mounted *EN*", + "zh": "WARNING: pddb not mounted *EN*" + }, "mtxcli.please.set.password": { "en": "please /set password my-password", "en-tts": "please /set password my-password", @@ -180,5 +187,12 @@ "fr": "/unset clé", "ja": "/unset key *EN*", "zh": "/unset key *EN*" + }, + "mtxcli.wifi.warning": { + "en": "WARNING: WiFi not connected", + "en-tts": "WARNING: WiFi not connected", + "fr": "AVERTISSEMENT: WiFi pas connecté", + "ja": "WARNING: WiFi not connected *EN*", + "zh": "WARNING: WiFi not connected *EN*" } } diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index caca54299..f50f28ced 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -39,6 +39,8 @@ const EMPTY: &str = ""; const MTX_TIMEOUT: i32 = 300; // ms pub const CLOCK_NOT_SET_ID: usize = 1; +pub const PDDB_NOT_MOUNTED_ID: usize = 2; +pub const WIFI_NOT_CONNECTED_ID: usize = 3; /// Returns a version string which is more likely to string compare /// correctly vs. another version. FFI please see @@ -196,7 +198,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - // log::info!("dict '{}' exists", MTXCLI_DICT); + log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -280,15 +282,19 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - // log::info!("dict '{}' exists", MTXCLI_DICT); + log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; } keypath.push(key); + // NOTE: the std:fs::metadata call may return Ok() even when + // the file does not exist. + // As a workaround WE COULD set the variable to the empty string + // log::info!("dict:key = '{}:{}' set to empty string (not deleting it)", MTXCLI_DICT, key); + // File::create(keypath)?.write_all(EMPTY.as_bytes())?; if std::fs::metadata(&keypath).is_ok() { // keypath exists log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); - std::fs::remove_file(keypath)?; } match key { // special case side effects -- update cached values @@ -324,7 +330,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - // log::info!("dict '{}' exists", MTXCLI_DICT); + log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -333,7 +339,7 @@ impl CommonEnv { if let Ok(mut file)= File::open(keypath) { let mut value = String::new(); file.read_to_string(&mut value)?; - // log::info!("get '{}' = '{}'", key, value); + log::info!("get '{}' = '{}'", key, value); Ok(Some(value)) } else { Ok(None) @@ -601,21 +607,30 @@ impl CmdEnv { // Initialization (must wait until PDDB is mounted and llio can // report accurate time). Here we wait to check for initialization // once the user has typed something - if ! self.initialized { + if ! self.common_env.initialized { log::info!("initializing"); if clock_not_set() { self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID); - } - self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); - self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); - self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); - self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); - self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); - self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); - self.common_env.version = get_version(&self.common_env.ticktimer); + } else { + let pddb = pddb::PddbMountPoller::new(); + if ! pddb.is_mounted_nonblocking() { + self.common_env.scalar_async_msg(PDDB_NOT_MOUNTED_ID); + } else { + // FUTURE: Check if Wifi is connected + // if not then + // self.common_env.scalar_async_msg(WIFI_NOT_CONNECTED_ ID); + self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); + self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); + self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); + self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); + self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); + self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); + self.common_env.version = get_version(&self.common_env.ticktimer); - run_migrations(&mut self.common_env); - self.initialized = true; + run_migrations(&mut self.common_env); + self.common_env.initialized = true; + } + } } let maybe_verb = tokenize(cmdline); diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index 3d8f4850f..248ac5b29 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -1,5 +1,5 @@ use crate::{ShellCmdApi,CommonEnv, - cmds::CLOCK_NOT_SET_ID}; + cmds::{CLOCK_NOT_SET_ID,PDDB_NOT_MOUNTED_ID,WIFI_NOT_CONNECTED_ID}}; use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; @@ -78,13 +78,24 @@ impl<'a> ShellCmdApi<'a> for Help { match &msg.body { Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _, arg3: _, arg4: async_msg_id}) => { - if *async_msg_id == CLOCK_NOT_SET_ID { - let mut ret = XousString::<1024>::new(); - let warning = t!("mtxcli.clock.warning", xous::LANG); - write!(ret, "{}", warning).unwrap(); - log::warn!("{}", warning); - return Ok(Some(ret)); - } + let mut ret = XousString::<1024>::new(); + let warning = match *async_msg_id { + CLOCK_NOT_SET_ID => { + t!("mtxcli.clock.warning", xous::LANG) + }, + PDDB_NOT_MOUNTED_ID => { + t!("mtxcli.pddb.warning", xous::LANG) + }, + WIFI_NOT_CONNECTED_ID => { + t!("mtxcli.wifi.warning", xous::LANG) + }, + _ => { + "unknown async_msg_id" + }, + }; + write!(ret, "{}", warning).unwrap(); + log::warn!("{}", warning); + return Ok(Some(ret)); }, Message::Move(mm) => { let str_buf = unsafe { StringBuffer::from_memory_message(mm) }; diff --git a/apps/mtxcli/src/cmds/set.rs b/apps/mtxcli/src/cmds/set.rs index 1513ea380..5b9e67bde 100644 --- a/apps/mtxcli/src/cmds/set.rs +++ b/apps/mtxcli/src/cmds/set.rs @@ -30,21 +30,30 @@ impl<'a> ShellCmdApi<'a> for Set { match value { "" => { write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); - } + } _ => { match env.set(key, value) { Ok(()) => { write!(ret, "set {}", key).unwrap(); }, Err(e) => { - // write!(ret, "error setting key {}: {:?}", key, e).unwrap(); log::error!("error setting key {}: {:?}", key, e); } } } } } else { - write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); + // Instead of an error -- set to the empty string + // write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); + // write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); + match env.set(key, "") { + Ok(()) => { + write!(ret, "set {} EMPTY", key).unwrap(); + }, + Err(e) => { + log::error!("error setting key {}: {:?}", key, e); + } + } } } } From 8f85aefa273484977301ddca45af0b51c7bb4287 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 19 Jan 2023 00:30:49 +0800 Subject: [PATCH 07/80] chore: fix typo :P --- RELEASE-v0.9.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 276a3f9dc..07cb575f2 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -312,7 +312,7 @@ perform the Xous firmware upgrade. This requires running manual update commands, - More optimizations to `vault` passwords path. Records are re-used instead of re-allocated if they don't change. This should speedup switching to `vault` passwords by about 2x after the very first time the records are loaded (the first time will take longer because the records have to be built up). - Extend watchdog reset time to ~30s from 7s, to enable easier guru meditation reporting. - Add USB mass-storage drivers (thanks @gsora for all the help there!). Currently able to emulate a blank USB drive in RAM; more to come soon. -- Performance fixes to xous scalar and memory messanges. A subtle bug was uncovered and fixed in the way scalar messages were being returned, and memory messages now initialize memory using an unrolled loop that takes better advantage of the 32-bit architecture and cache line size. A corresponding `std` fix to an unnecessary `yield_slice()` inside `dl_malloc` improves `vault` PDDB large-key readout performance by 30%. +- Performance fixes to xous scalar and memory messages. A subtle bug was uncovered and fixed in the way scalar messages were being returned, and memory messages now initialize memory using an unrolled loop that takes better advantage of the 32-bit architecture and cache line size. A corresponding `std` fix to an unnecessary `yield_slice()` inside `dl_malloc` improves `vault` PDDB large-key readout performance by 30%. ## Roadmap - Lots of testing and bug fixes From 71294f7591808c277aa3ee1d2ab65519d461cf9a Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 19 Jan 2023 14:46:25 +0800 Subject: [PATCH 08/80] add a missing sync after key deletion --- services/pddb/src/backend/basis.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 33af420f2..82238de11 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -571,14 +571,15 @@ impl BasisCache { } else { // this implementation is still in progress dict_entry.key_erase(key); - - // encrypt and write the dict entry to disk - basis.dict_sync(hw, dict)?; - // sync the root basis structure as well, while we're at it... - basis.basis_sync(hw); - // finally, sync the page tables. - basis.pt_sync(hw); } + // sync the key pools to disk + dict_entry.sync_large_pool(); + // encrypt and write the dict entry to disk + basis.dict_sync(hw, dict)?; + // sync the root basis structure as well, while we're at it... + basis.basis_sync(hw); + // finally, sync the page tables. + basis.pt_sync(hw); return Ok(()) } else { return Err(Error::new(ErrorKind::NotFound, "key not found")); From 0d65d9a7cafc2fb9e892384eae11ee8d55ec6188 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 19 Jan 2023 15:04:00 +0800 Subject: [PATCH 09/80] chore: fix another typo --- services/pddb/src/backend/dictionary.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 28e45b81d..9fcbc094d 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -49,7 +49,7 @@ pub(crate) struct DictCacheEntry { /// read in, and is the "master" for tracking purposes. We always fill this from index 0 and go up; if a KeySmallPool /// goes completely empty, the entry should still exist but indicate that it's got space. Thus if a key was found allocated /// to the Nth index position, but the previous N-1 positions are empty, the only way we could have gotten there was if we - /// had allocated lots of small data, filled upo the pool to the Nth position, and then deleted all of that prior data. + /// had allocated lots of small data, filled up the pool to the Nth position, and then deleted all of that prior data. /// This situation could create pathologies in the memory usage overhead of the small_pool, which until we have a "defrag" /// operation for the small pool, we may just have to live with. pub(crate) small_pool: Vec, From 329569803d90781ddf3a2d7cfb763cefbfe026ab Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 00:15:03 +0800 Subject: [PATCH 10/80] note that lock device also has a reboot aspect to it in the menu --- services/status/locales/i18n.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/status/locales/i18n.json b/services/status/locales/i18n.json index 4d83b0d9a..5f1fc22e1 100644 --- a/services/status/locales/i18n.json +++ b/services/status/locales/i18n.json @@ -182,11 +182,11 @@ "zh": "键盘布局..." }, "mainmenu.lockdevice": { - "en": "Lock device", - "en-tts": "Lock device", - "fr": "Verrouiller l'appareil", - "ja": "ロック装置", - "zh": "锁定装置" + "en": "Lock device (reboot)", + "en-tts": "Lock device (reboot)", + "fr": "Verrouiller l'appareil (redémarrer)", + "ja": "ロック装置 (リブート)", + "zh": "锁定装置 (重启)" }, "mainmenu.pddb": { "en": "PDDB submenu...", From d811ef0d2e51a02845ad809a280389f027535649 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 02:00:35 +0800 Subject: [PATCH 11/80] fix deletion UX in vault --- apps/vault/src/actions.rs | 3 +++ apps/vault/src/ux/framework.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 9b614d3e4..69e479597 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -511,6 +511,9 @@ impl<'a> ActionManager<'a> { Ok(_) => self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok().unwrap(), Err(e) => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } + if entry.mode == VaultMode::Password { + self.item_lists.lock().unwrap().pw.remove(entry.key_name.as_str().unwrap_or("UTF-8 error")); + } } } } diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index f9e89686a..33bda05a5 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -36,7 +36,7 @@ impl ListItem { } } pub fn key(&self) -> String { - self.name.to_string() + &self.guid.to_string() + self.guid.to_string() } } impl Ord for ListItem { From 20270609b9f0a1d3a6c5439a68c6671c0771d92c Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 05:06:16 +0800 Subject: [PATCH 12/80] fix cached state to match disk state in UX for edit/delete in passwords --- apps/vault/src/actions.rs | 101 ++++++++++++++++++++++++++++++--- apps/vault/src/main.rs | 5 ++ apps/vault/src/ux/framework.rs | 7 ++- 3 files changed, 104 insertions(+), 9 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 69e479597..a87858d32 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -44,6 +44,7 @@ pub(crate) enum ActionOp { MenuManageBasis, /// Internal ops UpdateMode, + UpdateOneItem, Quit, #[cfg(feature="vault-testing")] /// Testing @@ -68,6 +69,8 @@ pub(crate) fn start_actions_thread( manager.activate(); manager.menu_addnew(); // this is necessary so the next redraw shows the newly added entry + // no cache clear is called for because new entries will always add to the list; + // there is no risk of "stale" entries persisting manager.retrieve_db(); manager.deactivate(); }, @@ -90,12 +93,14 @@ pub(crate) fn start_actions_thread( Some(ActionOp::MenuUnlockBasis) => { manager.activate(); manager.unlock_basis(); + manager.item_lists.lock().unwrap().pw.clear(); // clear the cached item list for passwords (totp/fido are not cached and don't need clearing) manager.retrieve_db(); manager.deactivate(); }, Some(ActionOp::MenuManageBasis) => { manager.activate(); manager.manage_basis(); + manager.item_lists.lock().unwrap().pw.clear(); // clear the cached item list for passwords manager.retrieve_db(); manager.deactivate(); } @@ -104,6 +109,13 @@ pub(crate) fn start_actions_thread( manager.activate(); manager.deactivate(); }, + Some(ActionOp::UpdateOneItem) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let entry = buffer.to_original::().unwrap(); + manager.activate(); + manager.update_db_entry(entry); + manager.deactivate(); + } Some(ActionOp::UpdateMode) => msg_blocking_scalar_unpack!(msg, _, _, _, _,{ manager.retrieve_db(); xous::return_scalar(msg.sender, 1).unwrap(); @@ -507,17 +519,80 @@ impl<'a> ActionManager<'a> { } } else { // we're deleting either a password, or a totp - match self.storage.borrow_mut().delete(choice.unwrap(), entry.key_name.as_str().unwrap_or("UTF-8 error")) { + let choice = choice.unwrap(); + let guid = entry.key_name.as_str().unwrap_or("UTF8-error"); + if entry.mode == VaultMode::Password { + // self.modals.show_notification(&format!("deleting key {}", guid), None).ok(); + // if it's a password, we have to pull the full record, and then reconstitute the item_lists index key so we can remove it from the UX cache + let storage = self.storage.borrow_mut(); + let pw: storage::PasswordRecord = match storage.get_record(&choice, guid) { + Ok(record) => record, + Err(error) => { + self.report_err(t!("vault.error.internal_error", xous::LANG), Some(error)); + return; + } + }; + let mut desc = String::with_capacity(256); + make_pw_name(&pw.description, &pw.username, &mut desc); + let key = ListItem::key_from_parts(&desc, guid); + self.item_lists.lock().unwrap().pw.remove(&key); + /* + if self.item_lists.lock().unwrap().pw.remove(&key).is_some() { + self.modals.show_notification(&format!("deleted UX {}", &key), None).ok(); + } else { + self.modals.show_notification(&format!("could not delete UX {}", &key), None).ok(); + }; */ + } + match self.storage.borrow_mut().delete(choice, guid) { Ok(_) => self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok().unwrap(), Err(e) => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } - if entry.mode == VaultMode::Password { - self.item_lists.lock().unwrap().pw.remove(entry.key_name.as_str().unwrap_or("UTF-8 error")); - } } + self.pddb.borrow().sync().ok(); } } + /// Update UX cached data for just one entry + pub(crate) fn update_db_entry(&mut self, entry: SelectedEntry) { + match entry.mode { + VaultMode::Password => { + let choice = storage::ContentKind::Password; + let guid = entry.key_name.as_str().unwrap_or("UTF8-error"); + let storage = self.storage.borrow_mut(); + let pw: storage::PasswordRecord = match storage.get_record(&choice, guid) { + Ok(record) => record, + Err(error) => { + self.report_err(t!("vault.error.internal_error", xous::LANG), Some(error)); + return; + } + }; + // create the list item from the updated entry + let mut desc = String::with_capacity(256); + make_pw_name(&pw.description, &pw.username, &mut desc); + let mut extra = String::with_capacity(256); + let human_time = crate::ux::atime_to_str(pw.atime); + extra.push_str(&human_time); + extra.push_str("; "); + extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG)); + extra.push_str(&pw.count.to_string()); + let li = ListItem { + name: desc.to_string(), // these allocs will be slow, but we do it only once on boot + extra: extra.to_string(), + dirty: true, + guid: guid.to_string(), + atime: pw.atime, + count: pw.count, + }; + log::debug!("updating {} to list item {}", li.extra, li.key()); + let il = &mut self.item_lists.lock().unwrap().pw; + assert!(il.insert(li.key(), li).is_some(), "Somehow, the autotyped record isn't in the UX list for updating!"); + }, + _ => { + // no cached data, no action + } + }; + } + pub(crate) fn menu_edit(&mut self, entry: SelectedEntry) { let choice = match entry.mode { VaultMode::Password => Some(storage::ContentKind::Password), @@ -654,7 +729,12 @@ impl<'a> ActionManager<'a> { return; } }; + // remove the entry from the old UX list + let mut desc = String::new(); + make_pw_name(&pw.description, &pw.username, &mut desc); + self.item_lists.lock().unwrap().pw.remove(&ListItem::key_from_parts(&desc, key_name)); + // display previous data for edit let edit_data = if pw.notes != t!("vault.notes", xous::LANG) { self.modals .alert_builder(t!("vault.edit_dialog", xous::LANG)) @@ -759,15 +839,12 @@ impl<'a> ActionManager<'a> { if pw_rec.from_vec(data).is_ok() { // reset the re-usable structures lookup_key.clear(); - desc.clear(); extra.clear(); #[cfg(feature="vaultperf")] self.perfentry(&self.pm, PERFMETA_NONE, 2, std::line!()); // build the description string - desc.push_str(&pw_rec.description); - desc.push_str("/"); - desc.push_str(&pw_rec.username); + make_pw_name(&pw_rec.description, &pw_rec.username, &mut desc); // build the storage key in the list array lookup_key.push_str(&desc); @@ -784,6 +861,7 @@ impl<'a> ActionManager<'a> { if prev_entry.atime != pw_rec.atime || prev_entry.count != pw_rec.count { // this is expensive, so don't run it unless we have to let human_time = crate::ux::atime_to_str(pw_rec.atime); + // note this code is duplicated in update_db_entry() extra.push_str(&human_time); extra.push_str("; "); extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG)); @@ -1454,4 +1532,11 @@ pub(crate) fn heap_usage() -> usize { 0 }, } +} + +fn make_pw_name(description: &str, username: &str, dest: &mut String) { + dest.clear(); + dest.push_str(description); + dest.push_str("/"); + dest.push_str(username); } \ No newline at end of file diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index e0ea3dbd0..e9237bdaf 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -523,6 +523,11 @@ fn main() -> ! { } } modals.dynamic_notification_close().ok(); + // force the one entry to update its UX cache so that the autotype time increments up + if let Some(entry) = vaultux.selected_entry() { + let buf = Buffer::into_buf(entry).expect("IPC error"); + buf.send(actions_conn, ActionOp::UpdateOneItem.to_u32().unwrap()).expect("messaging error"); + } } Some(VaultOp::MenuDeleteStage1) => { // stage 1 happens here because the filtered list and selection entry are in the responsive UX section. diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 33bda05a5..a1a40fff4 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -35,8 +35,13 @@ impl ListItem { count: self.count, } } + /// This is made available for edit/delete routines to generate the key without having to + /// make a whole ListItem record (which is somewhat expensive). + pub fn key_from_parts(name: &str, guid: &str) -> String { + name.to_lowercase() + &guid.to_string() + } pub fn key(&self) -> String { - self.guid.to_string() + Self::key_from_parts(&self.name, &self.guid) } } impl Ord for ListItem { From 168b1a7f5c97b215a64e73ace5160f259a721e6b Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Thu, 19 Jan 2023 21:59:50 -0600 Subject: [PATCH 13/80] mtxcli: chat room updates asynchronously Added more predefined scalar messages Checks WiFi status Spawns separate thread for client_sync (reading the network) Most all log messages are now INFO (to be shown by default) Closes #300 Signed-off-by: Tom Marble --- Cargo.lock | 5 +- apps/mtxcli/Cargo.toml | 32 ++-- apps/mtxcli/locales/i18n.json | 28 +++ apps/mtxcli/src/cmds.rs | 283 +++++++++++++++++------------ apps/mtxcli/src/cmds/help.rs | 76 +++++++- apps/mtxcli/src/cmds/login.rs | 5 +- apps/mtxcli/src/cmds/logout.rs | 5 +- apps/mtxcli/src/cmds/migrations.rs | 3 + apps/mtxcli/src/cmds/status.rs | 12 +- apps/mtxcli/src/cmds/web.rs | 52 +++--- 10 files changed, 326 insertions(+), 175 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13dad3927..33aa60674 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2240,10 +2240,11 @@ dependencies = [ [[package]] name = "mtxcli" -version = "0.6.0" +version = "0.7.0" dependencies = [ "codec", "com", + "com_rs-ref", "content-plugin-api", "gam", "graphics-server", @@ -2252,11 +2253,13 @@ dependencies = [ "llio", "locales", "log", + "net", "num-derive", "num-traits", "pddb", "percent-encoding", "serde", + "status", "trng", "ureq", "xous 0.9.31 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/apps/mtxcli/Cargo.toml b/apps/mtxcli/Cargo.toml index 2e8342b1c..cb39858a0 100644 --- a/apps/mtxcli/Cargo.toml +++ b/apps/mtxcli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mtxcli" -version = "0.6.0" +version = "0.7.0" authors = ["bunnie "] edition = "2018" description = "Matrix chat" @@ -8,29 +8,31 @@ description = "Matrix chat" # Dependency versions enforced by Cargo.lock. [dependencies] log = "0.4.14" -num-derive = {version = "0.3.3", default-features = false} -num-traits = {version = "0.2.14", default-features = false} +num-derive = { version = "0.3.3", default-features = false} +num-traits = { version = "0.2.14", default-features = false} xous = "0.9.31" xous-ipc = "0.9.31" log-server = { package = "xous-api-log", version = "0.1.26" } ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.26" } xous-names = { package = "xous-api-names", version = "0.9.28" } -gam = {path = "../../services/gam" } -graphics-server = {path = "../../services/graphics-server" } -trng = {path = "../../services/trng"} -locales = {path = "../../locales"} -ime-plugin-api = {path = "../../services/ime-plugin-api"} -ime-plugin-shell = {path = "../../services/ime-plugin-shell"} -content-plugin-api = {path = "../../services/content-plugin-api"} # all content canvas providers must provide this API -com = {path = "../../services/com"} -llio = {path = "../../services/llio"} -codec = {path = "../../services/codec"} +gam = { path = "../../services/gam" } +graphics-server = { path = "../../services/graphics-server" } +trng = { path = "../../services/trng"} +locales = { path = "../../locales"} +ime-plugin-api = { path = "../../services/ime-plugin-api"} +ime-plugin-shell = { path = "../../services/ime-plugin-shell"} +content-plugin-api = { path = "../../services/content-plugin-api"} # all content canvas providers must provide this API +codec = { path = "../../services/codec"} +com = { path = "../../services/com"} +com_rs-ref = { path = "../../imports/com_rs-ref"} +llio = { path = "../../services/llio"} +net = { path = "../../services/net" } +pddb = { path = "../../services/pddb" } +status = { path = "../../services/status" } # new dependencies for mtxcli percent-encoding = "2.2" serde = { version = "1.0", features = [ "derive" ] } ureq = { version = "2.5", features = ["json"] } -# Xous APIs -pddb = {path = "../../services/pddb" } [features] default = [] diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index 5f9b384cc..fb0eb3f57 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -48,6 +48,13 @@ "ja": "available mtxcli commands: *EN*", "zh": "available mtxcli commands: *EN*" }, + "mtxcli.initialized": { + "en": "MTXCLI ready", + "en-tts": "MTXCLI ready", + "fr": "MTXCLI prêt", + "ja": "MTXCLI ready *EN*", + "zh": "MTXCLI ready *EN*" + }, "mtxcli.logged.in": { "en": "logged in", "en-tts": "logged in", @@ -97,6 +104,13 @@ "ja": "migration completed *EN*", "zh": "migration completed *EN*" }, + "mtxcli.migrations.complete": { + "en": "migrations complete", + "en-tts": "migrations complete", + "fr": "tous les migrations terminées", + "ja": "migrations complete *EN*", + "zh": "migrations complete *EN*" + }, "mtxcli.not.connected": { "en": "not connected", "en-tts": "not conntected", @@ -111,6 +125,13 @@ "ja": "WARNING: pddb not mounted *EN*", "zh": "WARNING: pddb not mounted *EN*" }, + "mtxcli.please.login": { + "en": "please /login", + "en-tts": "please /login", + "fr": "Veuillez /login", + "ja": "please /login *EN*", + "zh": "please /login *EN*" + }, "mtxcli.please.set.password": { "en": "please /set password my-password", "en-tts": "please /set password my-password", @@ -188,6 +209,13 @@ "ja": "/unset key *EN*", "zh": "/unset key *EN*" }, + "mtxcli.wifi.connected": { + "en": "WiFi connected", + "en-tts": "WiFi connected", + "fr": "WiFi connecté", + "ja": "WiFi connected *EN*", + "zh": "WiFi connected *EN*" + }, "mtxcli.wifi.warning": { "en": "WARNING: WiFi not connected", "en-tts": "WARNING: WiFi not connected", diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index f50f28ced..fc2a91b06 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -1,7 +1,6 @@ use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; -use locales::t; use std::collections::HashMap; use std::fs::File; @@ -15,7 +14,6 @@ mod url; mod web; const DATE_2023: u64 = 1672538401; // for clock_not_set() -const APP: &str = "mtxcli"; /// PDDB Dict for mtxcli keys const MTXCLI_DICT: &str = "mtxcli"; @@ -36,11 +34,32 @@ const HTTPS: &str = "https://"; const SERVER_MATRIX: &str = "https://matrix.org"; const EMPTY: &str = ""; -const MTX_TIMEOUT: i32 = 300; // ms +const SENTINEL: char = '‡'; +const MTX_LONG_TIMEOUT: i32 = 60000; // ms pub const CLOCK_NOT_SET_ID: usize = 1; pub const PDDB_NOT_MOUNTED_ID: usize = 2; pub const WIFI_NOT_CONNECTED_ID: usize = 3; +pub const MTXCLI_INITIALIZED_ID: usize = 4; +pub const WIFI_CONNECTED_ID: usize = 5; +pub const SET_USER_ID: usize = 6; +pub const SET_PASSWORD_ID: usize = 7; +pub const LOGGED_IN_ID: usize = 8; +pub const LOGIN_FAILED_ID: usize = 9; +pub const SET_ROOM_ID: usize = 10; +pub const ROOMID_FAILED_ID: usize = 11; +pub const FILTER_FAILED_ID: usize = 12; +pub const SET_SERVER_ID: usize = 13; +pub const LOGGING_IN_ID: usize = 14; +pub const LOGGED_OUT_ID: usize = 15; +pub const NOT_CONNECTED_ID: usize = 16; +pub const FAILED_TO_SEND_ID: usize = 17; +pub const PLEASE_LOGIN_ID: usize = 18; + +#[cfg(not(target_os = "xous"))] +pub const HOSTED_MODE: bool = true; +#[cfg(target_os = "xous")] +pub const HOSTED_MODE: bool = false; /// Returns a version string which is more likely to string compare /// correctly vs. another version. FFI please see @@ -54,7 +73,7 @@ fn get_version(ticktimer: &ticktimer_server::Ticktimer) -> String { log::info!("version={}=", version); version } else { - log::error!("ERROR, couldn't find version from xous_version: {}", + log::info!("ERROR, couldn't find version from xous_version: {}", xous_version); xous_version } @@ -102,6 +121,7 @@ pub struct CommonEnv { async_msg_callback_id: u32, async_msg_conn: u32, trng: Trng, + netmgr: net::NetManager, xns: xous_names::XousNames, user: String, username: String, @@ -114,21 +134,26 @@ pub struct CommonEnv { first_line: bool, version: String, initialized: bool, + wifi_connected: bool, + listening: bool, } impl CommonEnv { pub fn new() -> CommonEnv { - let xns = xous_names::XousNames::new().unwrap(); let ticktimer = ticktimer_server::Ticktimer::new() .expect("Couldn't connect to Ticktimer"); let cb_registrations = HashMap::new(); let async_msg_callback_id: u32 = 0; let async_msg_conn: u32 = 0; + let netmgr = net::NetManager::new(); + let xns = xous_names::XousNames::new().unwrap(); + let trng = Trng::new(&xns).unwrap(); let common = CommonEnv { ticktimer, cb_registrations, async_msg_callback_id, async_msg_conn, - trng: Trng::new(&xns).unwrap(), + trng, + netmgr, xns, user: EMPTY.to_string(), username: EMPTY.to_string(), @@ -141,6 +166,8 @@ impl CommonEnv { first_line: true, version: EMPTY.to_string(), initialized: false, + wifi_connected: false, + listening: false, }; common } @@ -176,8 +203,6 @@ impl CommonEnv { .expect("unable to create string message"); str_buf.send(self.async_msg_conn, self.async_msg_callback_id) .expect("unable to send string message"); - // let msg = str_buf.create_memory_message(self.async_msg_callback_id); - // xous::send_message(self.async_msg_conn, msg).unwrap(); } pub fn gen_txn_id(&mut self) -> String { @@ -198,7 +223,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -227,14 +252,14 @@ impl CommonEnv { true }, Err(e) => { - log::error!("error setting key {}: {:?}", key, e); + log::info!("error setting key {}: {:?}", key, e); false } } } pub fn set_user(&mut self, value: &str) { - log::debug!("# USER_KEY set '{}' = '{}'", USER_KEY, value); + log::info!("# USER_KEY set '{}' = '{}'", USER_KEY, value); let i = match value.find('@') { Some(index) => { index + 1 }, None => { 0 }, @@ -251,26 +276,22 @@ impl CommonEnv { self.server = SERVER_MATRIX.to_string(); } self.user = value.to_string(); - log::debug!("# user = '{}' username = '{}' server = '{}'", self.user, self.username, self.server); + log::info!("# user = '{}' username = '{}' server = '{}'", self.user, self.username, self.server); self.set_debug(USERNAME_KEY, &self.username.clone()); self.set_debug(SERVER_KEY, &self.server.clone()); self.unset_debug(TOKEN_KEY); - self.token = EMPTY.to_string(); } pub fn set_password(&mut self) { - log::debug!("# PASSWORD_KEY set '{}' => clearing TOKEN_KEY", PASSWORD_KEY); + log::info!("# PASSWORD_KEY set '{}' => clearing TOKEN_KEY", PASSWORD_KEY); self.unset_debug(TOKEN_KEY); } pub fn set_room(&mut self) { - log::debug!("# ROOM_KEY set '{}' => clearing ROOM_ID_KEY, SINCE_KEY, FILTER_KEY", ROOM_KEY); + log::info!("# ROOM_KEY set '{}' => clearing ROOM_ID_KEY, SINCE_KEY, FILTER_KEY", ROOM_KEY); self.unset_debug(ROOM_ID_KEY); - self.room_id = EMPTY.to_string(); self.unset_debug(SINCE_KEY); - self.since = EMPTY.to_string(); self.unset_debug(FILTER_KEY); - self.filter = EMPTY.to_string(); } pub fn unset(&mut self, key: &str) -> Result<(), Error> { @@ -282,17 +303,12 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; } keypath.push(key); - // NOTE: the std:fs::metadata call may return Ok() even when - // the file does not exist. - // As a workaround WE COULD set the variable to the empty string - // log::info!("dict:key = '{}:{}' set to empty string (not deleting it)", MTXCLI_DICT, key); - // File::create(keypath)?.write_all(EMPTY.as_bytes())?; if std::fs::metadata(&keypath).is_ok() { // keypath exists log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); std::fs::remove_file(keypath)?; @@ -317,7 +333,7 @@ impl CommonEnv { true }, Err(e) => { - log::error!("error unsetting key {}: {:?}", key, e); + log::info!("error unsetting key {}: {:?}", key, e); false } } @@ -330,7 +346,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -356,55 +372,40 @@ impl CommonEnv { value.to_string() } Err(e) => { - log::error!("error getting key {}: {:?}", key, e); + log::info!("error getting key {}: {:?}", key, e); default.to_string() } } } - pub fn prompt(&mut self, ret: &mut XousString::<1024>, text: &str) { - if self.first_line { - write!(ret, "{}> {}", APP, text).unwrap(); - self.first_line = false; - } else { - write!(ret, "\n{}> {}", APP, text).unwrap(); - } - } - - pub fn user_says(&mut self, ret: &mut XousString::<1024>, text: &str) { + pub fn user_says(&mut self, text: &str) { if ! self.logged_in { - if ! self.login(ret) { + if ! self.login() { return; } } if self.room_id.len() == 0 { - if ! self.get_room_id(ret) { + if ! self.get_room_id() { return; } } if self.filter.len() == 0 { - if ! self.get_filter(ret) { + if ! self.get_filter() { return; } } - self.read_messages(ret); - // The following is not required, because we will get what - // the user said when we read_messages the second time - // write!(ret, "{}> {}", self.username, text).unwrap(); - let txn_id = self.gen_txn_id(); log::info!("txn_id = {}", txn_id); if web::send_message(&self.server, &self.room_id, &text, &txn_id, &self.token) { log::info!("SENT: {}", text); - self.read_messages(ret); // update since to include what user said } else { log::info!("FAILED TO SEND"); - write!(ret, "{}", t!("mtxcli.send.failed", xous::LANG)).unwrap(); + self.scalar_async_msg(FAILED_TO_SEND_ID); } } - pub fn login(&mut self, ret: &mut XousString::<1024>) -> bool { - self.prompt(ret, t!("mtxcli.logging.in", xous::LANG)); + pub fn login(&mut self) -> bool { + self.scalar_async_msg(LOGGING_IN_ID); self.token = self.get_default(TOKEN_KEY, EMPTY); self.logged_in = false; if self.token.len() > 0 { @@ -415,47 +416,48 @@ impl CommonEnv { if ! self.logged_in { if web::get_login_type(&self.server) { let user = self.get_default(USER_KEY, USER_KEY); + if user.len() == 0 { + self.scalar_async_msg(SET_USER_ID); + } let password = self.get_default(PASSWORD_KEY, EMPTY); if password.len() == 0 { - self.prompt(ret, t!("mtxcli.please.set.user", xous::LANG)); - self.prompt(ret, t!("mtxcli.please.set.password", xous::LANG)); + self.scalar_async_msg(SET_PASSWORD_ID); } else { if let Some(new_token) = web::authenticate_user(&self.server, &user, &password) { self.set_debug(TOKEN_KEY, &new_token); - self.token = new_token; self.logged_in = true; } else { - log::error!("Error: cannnot login with type: {}", web::MTX_LOGIN_PASSWORD); + log::info!("Error: cannnot login with type: {}", web::MTX_LOGIN_PASSWORD); } } } } if self.logged_in { - self.prompt(ret, t!("mtxcli.logged.in", xous::LANG)); + self.scalar_async_msg(LOGGED_IN_ID); } else { - self.prompt(ret, t!("mtxcli.login.failed", xous::LANG)); + self.scalar_async_msg(LOGIN_FAILED_ID); } self.logged_in } - pub fn logout(&mut self, ret: &mut XousString::<1024>) { + pub fn logout(&mut self) { self.unset_debug(TOKEN_KEY); - self.prompt(ret, t!("mtxcli.logged.out", xous::LANG)); + self.scalar_async_msg(LOGGED_OUT_ID); self.logged_in = false; } // assume logged in, token is valid - pub fn get_room_id(&mut self, ret: &mut XousString::<1024>) -> bool { + pub fn get_room_id(&mut self) -> bool { if self.room_id.len() > 0 { true } else { let room = self.get_default(ROOM_KEY, EMPTY); let server = self.get_default(SERVER_KEY, EMPTY); if room.len() == 0 { - self.prompt(ret, t!("mtxcli.please.set.room", xous::LANG)); + self.scalar_async_msg(SET_ROOM_ID); false } else if server.len() == 0 { - self.prompt(ret, t!("mtxcli.please.set.server", xous::LANG)); + self.scalar_async_msg(SET_SERVER_ID); false } else { let mut room_server = String::new(); @@ -473,16 +475,15 @@ impl CommonEnv { }, }; if i >= server.len() { - self.prompt(ret, t!("mtxcli.please.set.server", xous::LANG)); + self.scalar_async_msg(SET_SERVER_ID); false } else { room_server.push_str(&server[i..]); if let Some(new_room_id) = web::get_room_id(&self.server, &room_server, &self.token) { self.set_debug(ROOM_ID_KEY, &new_room_id); - self.room_id = new_room_id; true } else { - self.prompt(ret, t!("mtxcli.roomid.failed", xous::LANG)); + self.scalar_async_msg(ROOMID_FAILED_ID); false } } @@ -491,46 +492,79 @@ impl CommonEnv { } // assume logged in, token is valid, room_id is valid, user is valid - pub fn get_filter(&mut self, ret: &mut XousString::<1024>) -> bool { + pub fn get_filter(&mut self) -> bool { if self.filter.len() > 0 { true } else { if let Some(new_filter) = web::get_filter(&self.user, &self.server, &self.room_id, &self.token) { self.set_debug(FILTER_KEY, &new_filter); - self.filter = new_filter; true } else { - self.prompt(ret, t!("mtxcli.filter.failed", xous::LANG)); + self.scalar_async_msg(FILTER_FAILED_ID); false } } } - // assume logged in, token is valid, room_id is valid, user is valid, - // and filter is valid - pub fn read_messages(&mut self, ret: &mut XousString::<1024>) -> bool { - if let Some((since, messages)) = web::client_sync(&self.server, &self.filter, - &self.since, MTX_TIMEOUT, - &self.room_id, &self.token) { - self.set_debug(SINCE_KEY, &since); - self.since = since; - if messages.len() > 0 { - if self.first_line { - write!(ret, "{}", messages).unwrap(); - self.first_line = false; - } else { - write!(ret, "\n{}", messages).unwrap(); + pub fn listen(&mut self) { + if self.listening { + log::info!("Already listening"); + return; + } + if ! self.logged_in { + log::info!("Not logged in"); + return; + } + if self.room_id.len() == 0 { + if ! self.get_room_id() { + return; + } + } + if self.filter.len() == 0 { + if ! self.get_filter() { + return; + } + } + self.listening = true; + log::info!("Started listening"); + std::thread::spawn({ + let server = self.server.clone(); + let filter = self.filter.clone(); + let since = self.since.clone(); + let room_id = self.room_id.clone(); + let token = self.token.clone(); + let async_msg_conn = self.async_msg_conn.clone(); + let async_msg_callback_id = self.async_msg_callback_id.clone(); + move || { + // log::info!("client_sync for {} ms...", MTX_LONG_TIMEOUT); + let mut response = String::new(); + response.push(SENTINEL); + if let Some((since, messages)) = web::client_sync(&server, &filter, &since, MTX_LONG_TIMEOUT, &room_id, &token) { + response.push_str(&since); + response.push(SENTINEL); + response.push_str(&messages); + response.push(SENTINEL); } - true - } else { - false + let str_buf = StringBuffer::from_str(&response) + .expect("unable to create string message"); + str_buf.send(async_msg_conn, async_msg_callback_id) + .expect("unable to send string message"); } - } else { - false - } + }); } + pub fn listen_over(&mut self, since: &str) { + self.listening = false; + log::info!("Stopped listening"); + if since.len() > 0 { + self.set_debug(SINCE_KEY, since); + // don't re-start listening if there was an error + if self.logged_in && (HOSTED_MODE || self.wifi_connected) { + self.listen(); + } + } + } } /* @@ -603,36 +637,61 @@ impl CmdEnv { &mut self.unset_cmd, ]; + let prev_wifi_connected = self.common_env.wifi_connected; + self.common_env.wifi_connected = false; + if let Some(conf) = self.common_env.netmgr.get_ipv4_config() { + if conf.dhcp == com_rs_ref::DhcpState::Bound { + self.common_env.wifi_connected = true; + } + } + if self.common_env.wifi_connected != prev_wifi_connected { + if self.common_env.wifi_connected { + self.common_env.scalar_async_msg(WIFI_CONNECTED_ID); + } else { + self.common_env.scalar_async_msg(WIFI_NOT_CONNECTED_ID); + } + } + if let Some(cmdline) = maybe_cmdline { - // Initialization (must wait until PDDB is mounted and llio can - // report accurate time). Here we wait to check for initialization - // once the user has typed something if ! self.common_env.initialized { log::info!("initializing"); + log::info!("WiFi connected: {}", self.common_env.wifi_connected); if clock_not_set() { self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID); + } + if ! self.common_env.wifi_connected && ! prev_wifi_connected { + self.common_env.scalar_async_msg(WIFI_NOT_CONNECTED_ID); + } + let pddb = pddb::PddbMountPoller::new(); + if ! pddb.is_mounted_nonblocking() { + self.common_env.scalar_async_msg(PDDB_NOT_MOUNTED_ID); } else { - let pddb = pddb::PddbMountPoller::new(); - if ! pddb.is_mounted_nonblocking() { - self.common_env.scalar_async_msg(PDDB_NOT_MOUNTED_ID); - } else { - // FUTURE: Check if Wifi is connected - // if not then - // self.common_env.scalar_async_msg(WIFI_NOT_CONNECTED_ ID); - self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); - self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); - self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); - self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); - self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); - self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); - self.common_env.version = get_version(&self.common_env.ticktimer); - + log::info!("PDDB is mounted"); + self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); + self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); + self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); + self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); + self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); + self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); + self.common_env.version = get_version(&self.common_env.ticktimer); + if self.common_env.logged_in || self.common_env.login() { run_migrations(&mut self.common_env); self.common_env.initialized = true; + log::info!("initialized"); + self.common_env.scalar_async_msg(MTXCLI_INITIALIZED_ID); + } else { + self.common_env.scalar_async_msg(PLEASE_LOGIN_ID); } } } + if ! self.common_env.listening && + self.common_env.logged_in && + (HOSTED_MODE || self.common_env.wifi_connected) && + self.common_env.initialized { + self.common_env.listen(); + } + let maybe_verb = tokenize(cmdline); let mut cmd_ret: Result>, xous::Error> = Ok(None); @@ -674,16 +733,13 @@ impl CmdEnv { let mut text = String::from(verb); text.push_str(" "); text.push_str(cmdline.to_str()); - self.common_env.user_says(&mut ret, &text); - Ok(Some(ret)) + self.common_env.user_says(&text); + // only for sync case Ok(Some(ret)) + Ok(None) } } else { log::info!("NO INPUT"); - if self.common_env.read_messages(&mut ret) { - Ok(Some(ret)) - } else { - Ok(None) - } + Ok(None) } } else if let Some(callback) = maybe_callback { let mut cmd_ret: Result>, xous::Error> = Ok(None); @@ -716,6 +772,7 @@ impl CmdEnv { } } + /// extract the first token, as delimited by spaces /// modifies the incoming line by removing the token and returning the remainder /// returns the found token @@ -756,7 +813,7 @@ pub(crate) fn heap_usage() -> usize { usage } _ => { - log::error!("Couldn't measure heap usage"); + log::info!("Couldn't measure heap usage"); 0 }, } @@ -769,8 +826,10 @@ pub fn clock_not_set() -> bool { .expect("Time went backwards"); let seconds: u64 = since_the_epoch.as_secs(); if seconds < DATE_2023 { + log::info!("clock NOT set, seconds since epoch: {}", seconds); true } else { + log::info!("clock set, seconds since epoch: {}", seconds); false } } diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index 248ac5b29..1c8b364a6 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -1,6 +1,6 @@ -use crate::{ShellCmdApi,CommonEnv, - cmds::{CLOCK_NOT_SET_ID,PDDB_NOT_MOUNTED_ID,WIFI_NOT_CONNECTED_ID}}; -use xous::{MessageEnvelope, Message,StringBuffer}; +use crate::{ShellCmdApi, CommonEnv}; +use crate::cmds::*; +use xous::{MessageEnvelope, Message, StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; use locales::t; @@ -74,7 +74,7 @@ impl<'a> ShellCmdApi<'a> for Help { } // NOTE: the help callback is used to process async messages - fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { + fn callback(&mut self, msg: &MessageEnvelope, env: &mut CommonEnv) -> Result>, xous::Error> { match &msg.body { Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _, arg3: _, arg4: async_msg_id}) => { @@ -89,6 +89,51 @@ impl<'a> ShellCmdApi<'a> for Help { WIFI_NOT_CONNECTED_ID => { t!("mtxcli.wifi.warning", xous::LANG) }, + MTXCLI_INITIALIZED_ID => { + t!("mtxcli.initialized", xous::LANG) + }, + WIFI_CONNECTED_ID => { + t!("mtxcli.wifi.connected", xous::LANG) + }, + SET_USER_ID => { + t!("mtxcli.please.set.user", xous::LANG) + }, + SET_PASSWORD_ID => { + t!("mtxcli.please.set.password", xous::LANG) + }, + LOGGED_IN_ID => { + t!("mtxcli.logged.in", xous::LANG) + }, + LOGIN_FAILED_ID => { + t!("mtxcli.login.failed", xous::LANG) + }, + SET_ROOM_ID => { + t!("mtxcli.please.set.room", xous::LANG) + }, + ROOMID_FAILED_ID => { + t!("mtxcli.roomid.failed", xous::LANG) + }, + FILTER_FAILED_ID => { + t!("mtxcli.filter.failed", xous::LANG) + }, + SET_SERVER_ID => { + t!("mtxcli.please.set.server", xous::LANG) + }, + LOGGING_IN_ID => { + t!("mtxcli.logging.in", xous::LANG) + }, + LOGGED_OUT_ID => { + t!("mtxcli.logged.out", xous::LANG) + }, + NOT_CONNECTED_ID => { + t!("mtxcli.not.connected", xous::LANG) + }, + FAILED_TO_SEND_ID => { + t!("mtxcli.send.failed", xous::LANG) + }, + PLEASE_LOGIN_ID => { + t!("mtxcli.please.login", xous::LANG) + }, _ => { "unknown async_msg_id" }, @@ -100,10 +145,27 @@ impl<'a> ShellCmdApi<'a> for Help { Message::Move(mm) => { let str_buf = unsafe { StringBuffer::from_memory_message(mm) }; let msg = str_buf.to_str(); - let mut ret = XousString::<1024>::new(); - write!(ret, "{}", msg).unwrap(); // log::info!("async message \"{}\"", msg); - return Ok(Some(ret)); + let mut ret = XousString::<1024>::new(); + if msg.starts_with(SENTINEL) { + let msgv: Vec<&str> = msg.split(SENTINEL).collect(); + if msgv.len() == 4 { + env.listen_over(msgv[1]); + if msgv[2].len() > 0 { + write!(ret, "{}", msgv[2]).unwrap(); + return Ok(Some(ret)); + } else { + return Ok(None); + } + } else { + log::info!("client_sync had an error"); + env.listen_over(msgv[0]); + return Ok(None); + } + } else { + write!(ret, "{}", msg).unwrap(); + return Ok(Some(ret)); + } }, _ => { log::error!("received unknown callback type: {:?}", msg) diff --git a/apps/mtxcli/src/cmds/login.rs b/apps/mtxcli/src/cmds/login.rs index 4ae0f32e3..ac6685b13 100644 --- a/apps/mtxcli/src/cmds/login.rs +++ b/apps/mtxcli/src/cmds/login.rs @@ -15,8 +15,7 @@ impl<'a> ShellCmdApi<'a> for Login { cmd_api!(login); fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { - let mut ret = XousString::<1024>::new(); - env.login(&mut ret); - Ok(Some(ret)) + env.login(); + Ok(None) } } diff --git a/apps/mtxcli/src/cmds/logout.rs b/apps/mtxcli/src/cmds/logout.rs index af1fc7e5d..914d588da 100644 --- a/apps/mtxcli/src/cmds/logout.rs +++ b/apps/mtxcli/src/cmds/logout.rs @@ -15,8 +15,7 @@ impl<'a> ShellCmdApi<'a> for Logout { cmd_api!(logout); fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { - let mut ret = XousString::<1024>::new(); - env.logout(&mut ret); - Ok(Some(ret)) + env.logout(); + Ok(None) } } diff --git a/apps/mtxcli/src/cmds/migrations.rs b/apps/mtxcli/src/cmds/migrations.rs index 1b610a92e..b61f4180b 100644 --- a/apps/mtxcli/src/cmds/migrations.rs +++ b/apps/mtxcli/src/cmds/migrations.rs @@ -71,5 +71,8 @@ pub fn run_migrations(common: &mut CommonEnv) { } common.set(VERSION_KEY, &common.version.clone()) .expect("cannot set _version"); + let migrations_complete = t!("mtxcli.migrations.complete", xous::LANG); + log::info!("{}", migrations_complete); + common.send_async_msg(&migrations_complete); } } diff --git a/apps/mtxcli/src/cmds/status.rs b/apps/mtxcli/src/cmds/status.rs index 05990973d..93379fae2 100644 --- a/apps/mtxcli/src/cmds/status.rs +++ b/apps/mtxcli/src/cmds/status.rs @@ -1,6 +1,6 @@ use crate::{ShellCmdApi,CommonEnv}; +use crate::cmds::*; use xous_ipc::String as XousString; -use locales::t; #[derive(Debug)] pub struct Status { @@ -16,15 +16,11 @@ impl<'a> ShellCmdApi<'a> for Status { cmd_api!(status); fn process(&mut self, _args: XousString::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { - let mut ret = XousString::<1024>::new(); - let mut text = String::new(); - text.push_str("status: "); if env.logged_in { - text.push_str(t!("mtxcli.logged.in", xous::LANG)); + env.scalar_async_msg(LOGGED_IN_ID); } else { - text.push_str(t!("mtxcli.not.connected", xous::LANG)); + env.scalar_async_msg(NOT_CONNECTED_ID); } - env.prompt(&mut ret, &text); - Ok(Some(ret)) + Ok(None) } } diff --git a/apps/mtxcli/src/cmds/web.rs b/apps/mtxcli/src/cmds/web.rs index 258c09212..6cad891b8 100644 --- a/apps/mtxcli/src/cmds/web.rs +++ b/apps/mtxcli/src/cmds/web.rs @@ -30,7 +30,7 @@ fn serialize(object: &T) -> Option { Some(value) }, Err(e) => { - log::error!("ERROR in serialize: {:?}", e); + log::info!("ERROR in serialize: {:?}", e); None } } @@ -42,7 +42,7 @@ pub fn handle_response(maybe_response: Result) -> Op if let Ok(body) = response.into_json() { Some(body) } else { - log::error!("Error: could not convert response into JSON"); + log::info!("Error: could not convert response into JSON"); None } }, @@ -50,11 +50,11 @@ pub fn handle_response(maybe_response: Result) -> Op /* the server returned an unexpected status code (such as 400, 500 etc) */ let err_body = response.into_string().unwrap(); - log::error!("ERROR code {} err_body = {}", code, err_body); + log::info!("ERROR code {} err_body = {}", code, err_body); None } Err(e) => { - log::error!("ERROR in handle_response: {:?}", e); + log::info!("ERROR in handle_response: {:?}", e); None } } @@ -107,10 +107,10 @@ pub fn whoami(server: &str, token: &str) -> bool { if let Some(value) = handle_response(get_json_auth(&url, token)) { if let Value::Object(body) = value { if let Some(Value::String(device_id)) = body.get("device_id") { - log::debug!("device_id = {}", device_id); + log::info!("device_id = {}", device_id); } if let Some(Value::String(user_id)) = body.get("user_id") { - log::debug!("user_id = {}", user_id); + log::info!("user_id = {}", user_id); } } true @@ -192,21 +192,21 @@ pub fn get_room_id(server: &str, room_server: &str, token: &str) -> Option, room_id: &str) -> String { - log::debug!("heap usage: {}", crate::cmds::heap_usage()); + log::info!("heap usage: {}", crate::cmds::heap_usage()); let mut messages = String::new(); if let Some(Value::Object(rooms)) = body.get("rooms") { if let Some(Value::Object(join)) = rooms.get("join") { @@ -378,7 +378,7 @@ fn get_messages(body: Map, room_id: &str) -> String { pub fn client_sync(server: &str, filter: &str, since: &str, timeout: i32, room_id: &str, token: &str) -> Option<(String, String)> { - log::debug!("heap usage: {}", crate::cmds::heap_usage()); + log::info!("heap usage: {}", crate::cmds::heap_usage()); let mut url = String::from(server); url.push_str("/_matrix/client/r0/sync?filter="); url.push_str(filter); @@ -388,17 +388,17 @@ pub fn client_sync(server: &str, filter: &str, since: &str, timeout: i32, url.push_str("&since="); url.push_str(since); } - log::debug!("client_sync = {}", url); + log::info!("client_sync = {}", url); if let Some(value) = handle_response(get_json_auth(&url, token)) { if let Value::Object(body) = value { if let Some(Value::String(next_batch)) = body.get("next_batch") { Some((next_batch.to_string(), get_messages(body, room_id))) } else { - log::error!("invalid response for client_sync"); + log::info!("invalid response for client_sync"); None } } else { - log::error!("Error for client_sync: deserialization"); + log::info!("Error for client_sync: deserialization"); None } } else { @@ -424,29 +424,29 @@ impl MessageRequest { } pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token: &str) -> bool { - log::debug!("heap usage: {}", crate::cmds::heap_usage()); + log::info!("heap usage: {}", crate::cmds::heap_usage()); let room_id_encoded = url::encode(room_id); let mut url = String::from(server); url.push_str("/_matrix/client/r0/rooms/"); url.push_str(&room_id_encoded); url.push_str("/send/m.room.message/"); url.push_str(txn_id); - log::debug!("send_message = {}", url); + log::info!("send_message = {}", url); let message_request = MessageRequest::new(text); if let Some(request_body) = serialize(&message_request) { if let Some(value) = handle_response(put_string_auth(&url, &request_body, token)) { if let Value::Object(_body) = value { true } else { - log::error!("invalid response for send_message"); + log::info!("invalid response for send_message"); false } } else { - log::error!("Error for send_message"); + log::info!("Error for send_message"); false } } else { - log::error!("Error unable to serialize request for send_message"); + log::info!("Error unable to serialize request for send_message"); false } } From b7380db79dd44453aed3bf634ba640fdb0e4965c Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:05:36 +0800 Subject: [PATCH 14/80] add test case for pddb delete keys --- services/shellchat/src/cmds/pddb_cmd.rs | 90 +++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index e85f569e8..05ad1cd88 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -393,6 +393,96 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } } + #[cfg(feature="pddbtest")] + "deltest" => { + const JUNK_MIN: usize = 512; + const JUNK_VAR: usize = 1500; + const TOTAL_KEYS: usize = 40; + self.pddb.delete_dict("deltest", None).ok(); + for outer_iter in 0..512 { + log::info!("fill junk iter {}", outer_iter); + let mut junk_keys = Vec::::new(); + let mut junk_checksum = std::collections::HashMap::::new(); + for index in 0..TOTAL_KEYS { // this should allocate a few key small pools + let mut junk = Vec::::new(); + let total_junk = JUNK_MIN + _env.trng.get_u32().unwrap() as usize % JUNK_VAR; + let mut checksum = 0; + for i in 0..total_junk { + junk.push((i as usize + index) as u8); + checksum += ((i as usize + index) as u8) as usize; + } + let junkname = format!("junk{}", index); + match self.pddb.get( + "deltest", + &junkname, + None, true, true, + None, + None:: + ) { + Ok(mut junk_key) => { + match junk_key.write_all(&junk) { + Ok(_) => { + log::debug!("wrote {} of len {}", junkname, total_junk); + } + Err(e) => { + log::error!("couldn't write {}: {:?}", junkname, e); + } + } + } + Err(e) => { + log::error!("couldn't allocate junk key {}: {:?}", junkname, e); + } + } + log::debug!("name: {}, checksum: {}", junkname, checksum); + junk_checksum.insert(junkname.to_string(), checksum); + junk_keys.push(junkname); + } + while junk_keys.len() > 0 { + let to_remove = _env.trng.get_u32().unwrap() as usize % junk_keys.len(); + match self.pddb.delete_key("deltest", &junk_keys[to_remove], None) { + Ok(_) => log::debug!("remove {} OK", junk_keys[to_remove]), + Err(e) => log::error!("remove {} error: {:?}", junk_keys[to_remove], e), + } + self.pddb.sync().ok(); + let check = self.pddb.list_keys("deltest", None).unwrap(); + assert!(check.len() == (junk_keys.len() - 1)); + for (_key_index, key) in check.iter().enumerate() { + if key == &junk_keys[to_remove] { + panic!("Removed key was not removed"); + } + assert!(junk_keys.contains(key), "Unexpected key returned in key list"); + let mut junk = Vec::::new(); + match self.pddb.get( + "deltest", + &key, + None, false, false, None, None:: + ) { + Ok(mut junk_key) => { + let readback_len = junk_key.read_to_end(&mut junk).unwrap(); + let mut checksum = 0; + for &d in junk.iter() { + checksum += d as usize; + } + if checksum != *junk_checksum.get(key).unwrap() { + log::info!("readback {}", readback_len); + log::info!("array: {:?}", junk); + log::error!("Key data did not match name: {}, checksum: {}, expected: {}", + key, + checksum, + *junk_checksum.get(key).unwrap() + ); + panic!("data mismatch!"); + } + } + Err(e) => log::error!("error reading back key that should exist: {:?}", e), + } + } + junk_keys.remove(to_remove); + } + assert!(self.pddb.list_keys("deltest", None).unwrap().len() == 0, "Not all keys were deleted!"); + // self.pddb.sync().ok(); + } + } #[cfg(feature="test-rekey")] "rekey" => { let old_dna = if let Some(dna_str) = tokens.next() { From ea913a6fb4bb38599dab7516a5d523b937e80bfe Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:07:09 +0800 Subject: [PATCH 15/80] fix initialization order so that CI can run --- services/pddb/src/main.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 537190a60..97f7a320e 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -528,6 +528,15 @@ fn wrapped_main() -> ! { } }); + // our menu handler + let my_cid = xous::connect(pddb_sid).unwrap(); + let _ = thread::spawn({ + let my_cid = my_cid.clone(); + move || { + pddb_menu(my_cid); + } + }); + // run the CI tests if the option has been selected #[cfg(all( not(target_os = "xous"), @@ -539,14 +548,6 @@ fn wrapped_main() -> ! { hw_testcase(&mut pddb_os); } - // our menu handler - let my_cid = xous::connect(pddb_sid).unwrap(); - let _ = thread::spawn({ - let my_cid = my_cid.clone(); - move || { - pddb_menu(my_cid); - } - }); // a thread to trigger period scrubbing of the PDDB let scrub_run = Arc::new(AtomicBool::new(false)); let _ = thread::spawn({ From 71a1dddf15a3091f42c4994dee9cd0f685197b07 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:07:24 +0800 Subject: [PATCH 16/80] fix some more edge cases in PDDB key deletion --- services/pddb/src/backend/basis.rs | 11 +++++++++++ services/pddb/src/backend/dictionary.rs | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 82238de11..b538e281b 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -572,7 +572,11 @@ impl BasisCache { // this implementation is still in progress dict_entry.key_erase(key); } + // sync the key pools to disk + if !dict_entry.sync_small_pool(hw, &mut basis.v2p_map, &basis.cipher) { + return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of memory syncing small pool")); + } dict_entry.sync_large_pool(); // encrypt and write the dict entry to disk basis.dict_sync(hw, dict)?; @@ -1697,6 +1701,13 @@ impl BasisCacheEntry { } } for dict in dictnames { + if let Some(dict_entry) = self.dicts.get_mut(&dict) { + if !dict_entry.sync_small_pool(hw, &mut self.v2p_map, &self.cipher) { + return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of memory syncing small pool")); + } + dict_entry.sync_large_pool(); + } + match self.dict_sync(hw, &dict) { Ok(_) => {}, Err(e) => { diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 9fcbc094d..b2eae3e35 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -307,6 +307,10 @@ impl DictCacheEntry { fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, data_cache: &mut PlaintextCache, key_name: &str) { if let Some(kcache) = self.keys.get_mut(key_name) { + if !kcache.flags.valid() { + // nothing to fill, the key entry isn't valid + return; + } if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { // if the key is within the small pool space, create a bookkeeping record for it, and pre-cache its data. // generate the index within the small pool based on the address @@ -1014,7 +1018,8 @@ impl DictCacheEntry { log::error!("Double-free error in free_keys()"); log::info!("free_key_vec[i].0: {:?}", free_key_vec[i].0); log::info!("index: {}", index); - panic!("Double-free error in free_keys()"); + panic!("Double-free error in free_keys(). free_key_vec[i].0: {:?}, index: {}, free_key_vec.len(): {}", + free_key_vec[i].0, index, free_key_vec.len()); } FreeKeyCases::RightAdjacent => { // see if we should merge to the right From f542943df116b0357dd2ea10486d297fa86b645f Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:13:39 +0800 Subject: [PATCH 17/80] fix search criteria to match case-insensitive listing --- apps/vault/src/ux/framework.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index a1a40fff4..17a2d6d24 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -684,7 +684,7 @@ impl VaultUx { VaultMode::Password => &il.pw, }; for item in item_list.values() { - if item.name.starts_with(criteria) { + if item.name.to_lowercase().starts_with(criteria) { let mut staged_item = item.clone(); staged_item.dirty = true; self.filtered_list.push(staged_item); From 1786089187c78c896a005983ccd30a48802065d7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:24:26 +0800 Subject: [PATCH 18/80] bump release notes --- RELEASE-v0.9.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 07cb575f2..0c10581e6 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -313,6 +313,7 @@ perform the Xous firmware upgrade. This requires running manual update commands, - Extend watchdog reset time to ~30s from 7s, to enable easier guru meditation reporting. - Add USB mass-storage drivers (thanks @gsora for all the help there!). Currently able to emulate a blank USB drive in RAM; more to come soon. - Performance fixes to xous scalar and memory messages. A subtle bug was uncovered and fixed in the way scalar messages were being returned, and memory messages now initialize memory using an unrolled loop that takes better advantage of the 32-bit architecture and cache line size. A corresponding `std` fix to an unnecessary `yield_slice()` inside `dl_malloc` improves `vault` PDDB large-key readout performance by 30%. +- Find and fix some edge cases in key deletion. Keys that were supposed to be deleted were being re-fetched from RAM cache, which leads to them re-appearing in the UX and when one attempts to re-delete the key it triggers a double-free error. ## Roadmap - Lots of testing and bug fixes From 5b00daba0ded52d586074c03df6aad39ae6ba63f Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 20 Jan 2023 18:25:21 +0800 Subject: [PATCH 19/80] bump release notes --- RELEASE-v0.9.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 0c10581e6..234cb35d3 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -314,6 +314,7 @@ perform the Xous firmware upgrade. This requires running manual update commands, - Add USB mass-storage drivers (thanks @gsora for all the help there!). Currently able to emulate a blank USB drive in RAM; more to come soon. - Performance fixes to xous scalar and memory messages. A subtle bug was uncovered and fixed in the way scalar messages were being returned, and memory messages now initialize memory using an unrolled loop that takes better advantage of the 32-bit architecture and cache line size. A corresponding `std` fix to an unnecessary `yield_slice()` inside `dl_malloc` improves `vault` PDDB large-key readout performance by 30%. - Find and fix some edge cases in key deletion. Keys that were supposed to be deleted were being re-fetched from RAM cache, which leads to them re-appearing in the UX and when one attempts to re-delete the key it triggers a double-free error. +- `mtxcli` has a message filter and async message updates! (thanks @tmarble) ## Roadmap - Lots of testing and bug fixes From 2fb776b2a6949def4b52a516b5ade8b1d6f98747 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 24 Jan 2023 03:56:30 +0800 Subject: [PATCH 20/80] first cut at porting in FIDO2.1 features from OpenSK Code now builds, but, it doesn't fully work. Also, the vault backup vendor routines have yet to be wedged in. On the other hand, this edit is much more targeted than the first one, so hopefully future port-ins of OpenSK will be much smoother. In particular, OpenSK has a nice `env` abstraction that has enough power now for us to stick most of the Xous-specific stuff in there. There's still a few patches needed to the main code loop, mainly to accommodate our ability to display more information than flash an LED. The storage layer is also migrated to a totally new dictionary, because the latest version also completely redoes the storage with some sort of key/value system, which maps nicely into the PDDB. --- apps/vault/Cargo.toml | 16 +- apps/vault/libraries/backup/src/lib.rs | 68 +- apps/vault/libraries/cbor/src/lib.rs | 2 +- apps/vault/libraries/cbor/src/macros.rs | 370 +- apps/vault/libraries/cbor/src/reader.rs | 200 +- apps/vault/libraries/cbor/src/values.rs | 300 +- apps/vault/libraries/cbor/src/writer.rs | 233 +- apps/vault/libraries/crypto/src/aes256.rs | 309 ++ apps/vault/libraries/crypto/src/cbc.rs | 222 +- apps/vault/libraries/crypto/src/ecdh.rs | 7 + apps/vault/libraries/crypto/src/ecdsa.rs | 61 +- apps/vault/libraries/crypto/src/hkdf.rs | 80 + apps/vault/libraries/crypto/src/lib.rs | 2 + .../libraries/persistent_store/Cargo.toml | 5 + .../libraries/persistent_store/src/buffer.rs | 44 +- .../libraries/persistent_store/src/concat.rs | 242 + .../libraries/persistent_store/src/driver.rs | 32 +- .../libraries/persistent_store/src/file.rs | 193 + .../libraries/persistent_store/src/format.rs | 213 +- .../persistent_store/src/fragment.rs | 345 ++ .../libraries/persistent_store/src/lib.rs | 479 +- .../libraries/persistent_store/src/linear.rs | 373 ++ .../libraries/persistent_store/src/model.rs | 9 +- .../libraries/persistent_store/src/storage.rs | 22 +- .../libraries/persistent_store/src/store.rs | 1394 +----- .../libraries/persistent_store/src/test.rs | 89 + apps/vault/src/actions.rs | 78 +- apps/vault/src/api/attestation_store.rs | 100 + apps/vault/src/api/connection.rs | 33 + apps/vault/src/api/customization.rs | 449 ++ apps/vault/src/api/firmware_protection.rs | 20 + apps/vault/src/api/key_store.rs | 149 + apps/vault/src/api/mod.rs | 26 + apps/vault/src/api/upgrade_storage/helper.rs | 385 ++ apps/vault/src/api/upgrade_storage/mod.rs | 39 + apps/vault/src/api/user_presence.rs | 52 + apps/vault/src/ctap/apdu.rs | 37 +- apps/vault/src/ctap/client_pin.rs | 1711 +++++++ apps/vault/src/ctap/command.rs | 840 +++- apps/vault/src/ctap/config_command.rs | 457 ++ apps/vault/src/ctap/credential_id.rs | 495 ++ apps/vault/src/ctap/credential_management.rs | 913 ++++ apps/vault/src/ctap/crypto_wrapper.rs | 429 ++ apps/vault/src/ctap/ctap1.rs | 469 +- apps/vault/src/ctap/data_formats.rs | 1218 ++++- apps/vault/src/ctap/hid/mod.rs | 946 ++-- apps/vault/src/ctap/hid/receive.rs | 284 +- apps/vault/src/ctap/hid/send.rs | 80 +- apps/vault/src/ctap/key_material.rs | 2 +- apps/vault/src/ctap/large_blobs.rs | 431 ++ apps/vault/src/ctap/main_hid.rs | 235 + apps/vault/src/ctap/mod.rs | 4079 ++++++++++++----- apps/vault/src/ctap/pin_protocol.rs | 409 ++ apps/vault/src/ctap/pin_protocol_v1.rs | 1296 ------ apps/vault/src/ctap/response.rs | 503 +- apps/vault/src/ctap/status_code.rs | 59 +- apps/vault/src/ctap/storage.rs | 2201 ++++----- apps/vault/src/ctap/storage/key.rs | 137 +- apps/vault/src/ctap/timed_permission.rs | 187 +- apps/vault/src/ctap/token_state.rs | 277 ++ apps/vault/src/ctap/vendor_hid.rs | 174 + apps/vault/src/env/mod.rs | 75 + apps/vault/src/env/test/customization.rs | 188 + apps/vault/src/env/test/mod.rs | 219 + apps/vault/src/env/test/upgrade_storage.rs | 117 + apps/vault/src/env/tock/mod.rs | 328 ++ apps/vault/src/env/tock/storage.rs | 572 +++ apps/vault/src/env/xous/mod.rs | 492 ++ apps/vault/src/env/xous/storage.rs | 75 + apps/vault/src/lib.rs | 116 + apps/vault/src/main.rs | 288 +- apps/vault/src/prereqs.rs | 2 +- apps/vault/src/storage.rs | 17 +- apps/vault/src/submenu.rs | 2 +- apps/vault/src/totp.rs | 10 +- apps/vault/src/ux.rs | 60 +- apps/vault/src/ux/framework.rs | 91 +- apps/vault/src/vault_api.rs | 200 + apps/vault/src/vendor_commands.rs | 29 +- 79 files changed, 19518 insertions(+), 7873 deletions(-) create mode 100644 apps/vault/libraries/crypto/src/aes256.rs create mode 100644 apps/vault/libraries/crypto/src/hkdf.rs create mode 100644 apps/vault/libraries/persistent_store/src/concat.rs create mode 100644 apps/vault/libraries/persistent_store/src/file.rs create mode 100644 apps/vault/libraries/persistent_store/src/fragment.rs create mode 100644 apps/vault/libraries/persistent_store/src/linear.rs create mode 100644 apps/vault/libraries/persistent_store/src/test.rs create mode 100644 apps/vault/src/api/attestation_store.rs create mode 100644 apps/vault/src/api/connection.rs create mode 100644 apps/vault/src/api/customization.rs create mode 100644 apps/vault/src/api/firmware_protection.rs create mode 100644 apps/vault/src/api/key_store.rs create mode 100644 apps/vault/src/api/mod.rs create mode 100644 apps/vault/src/api/upgrade_storage/helper.rs create mode 100644 apps/vault/src/api/upgrade_storage/mod.rs create mode 100644 apps/vault/src/api/user_presence.rs create mode 100644 apps/vault/src/ctap/client_pin.rs create mode 100644 apps/vault/src/ctap/config_command.rs create mode 100644 apps/vault/src/ctap/credential_id.rs create mode 100644 apps/vault/src/ctap/credential_management.rs create mode 100644 apps/vault/src/ctap/crypto_wrapper.rs create mode 100644 apps/vault/src/ctap/large_blobs.rs create mode 100644 apps/vault/src/ctap/main_hid.rs create mode 100644 apps/vault/src/ctap/pin_protocol.rs delete mode 100644 apps/vault/src/ctap/pin_protocol_v1.rs create mode 100644 apps/vault/src/ctap/token_state.rs create mode 100644 apps/vault/src/ctap/vendor_hid.rs create mode 100644 apps/vault/src/env/mod.rs create mode 100644 apps/vault/src/env/test/customization.rs create mode 100644 apps/vault/src/env/test/mod.rs create mode 100644 apps/vault/src/env/test/upgrade_storage.rs create mode 100644 apps/vault/src/env/tock/mod.rs create mode 100644 apps/vault/src/env/tock/storage.rs create mode 100644 apps/vault/src/env/xous/mod.rs create mode 100644 apps/vault/src/env/xous/storage.rs create mode 100644 apps/vault/src/lib.rs create mode 100644 apps/vault/src/vault_api.rs diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index 174f44129..4681b48cc 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -24,8 +24,6 @@ trng = {path="../../services/trng"} susres = {package = "xous-api-susres", version = "0.9.26"} ime-plugin-api = {path = "../../services/ime-plugin-api"} content-plugin-api = {path = "../../services/content-plugin-api"} # all content canvas providers must provide this API -ctap-crypto = {path = "libraries/crypto"} -cbor = {path = "libraries/cbor"} backup = {path = "libraries/backup"} byteorder = { version = "1.4.3", default-features = false } arrayref = "0.3.6" @@ -33,6 +31,13 @@ subtle = { version = "2.2.3", default-features = false, features = ["nightly"] } rand_core = "0.6.3" ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.26"} +# opensk +ctap-crypto = {path = "libraries/crypto"} +cbor = {path = "libraries/cbor"} +persistent_store = { path = "libraries/persistent_store" } +ed25519-compact = { version = "1", default-features = false, optional = true } +rand = { version = "0.8.4", optional = true } + # ux formatting hex = { version = "0.4.3", default-features = false, features = ["alloc"] } chrono = {version = "0.4.19", default-features = false, features = ["std"]} @@ -66,12 +71,13 @@ hosted = [] renode = [] tts = [] with_ctap1 = ["ctap-crypto/with_ctap1"] -with_ctap2_1 = [] autotest = [] multireset = [] # allows credential reset more than once per boot vaultperf = ["perflib", "xous/v2p", "utralib", "utralib/precursor"] # vault is the performance logging manager. Do not use concurrently with "shellperf", as there can only be one manager. vault-testing = ["random-pick"] # introduce a slight delay after UX boxes swap, in case of race conditions. Shouldn't be necessary anymore, but kept around in case there's a new edge case we missed. ux-swap-delay = [] - -default = ["with_ctap1", "with_ctap2_1"] # testing removed for release tag \ No newline at end of file +osk-upgrade = [] # placeholder to select out OpenSK's "vendor upgrade" over HID, which we don't use because we have our own upgrade path. +xous = [] # marks xous-specific code segments deep inside the CTAP crate. Mostly to help evaluate if a patch is even feasible in the future. +ed25519 = ["ed25519-compact"] +default = ["with_ctap1", "ed25519", "xous"] # testing removed for release tag \ No newline at end of file diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs index 89eee282c..b08e82d1b 100644 --- a/apps/vault/libraries/backup/src/lib.rs +++ b/apps/vault/libraries/backup/src/lib.rs @@ -1,4 +1,4 @@ -use cbor::{self, cbor_array_vec, cbor_key_int, cbor_map, cbor_unsigned, destructure_cbor_map}; +use cbor::{self, cbor_array_vec, cbor_int, cbor_map, cbor_unsigned, destructure_cbor_map}; use serde::{Deserialize, Serialize}; use std::fmt::Display; @@ -63,14 +63,12 @@ impl From for cbor::Value { impl TryFrom for HashAlgorithms { fn try_from(value: cbor::Value) -> Result { - match value { - cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => match unsigned { - 1 => Ok(HashAlgorithms::SHA1), - 2 => Ok(HashAlgorithms::SHA256), - 3 => Ok(HashAlgorithms::SHA512), - other => Err(CborConversionError::UnknownAlgorithm(other)), - }, - _ => Err(CborConversionError::BadCbor), + let v = extract_unsigned(value)?; + match v { + 1 => Ok(HashAlgorithms::SHA1), + 2 => Ok(HashAlgorithms::SHA256), + 3 => Ok(HashAlgorithms::SHA512), + _ => Err(CborConversionError::UnknownAlgorithm(v)), } } @@ -104,11 +102,11 @@ pub struct TotpEntry { impl From for cbor::Value { fn from(te: TotpEntry) -> Self { cbor_map! { - cbor_key_int!(1) => te.step_seconds as i64, - cbor_key_int!(2) => te.shared_secret, - cbor_key_int!(3) => te.digit_count as i64, - cbor_key_int!(4) => te.algorithm, - cbor_key_int!(5) => te.name, + cbor_int!(1) => te.step_seconds as i64, + cbor_int!(2) => te.shared_secret, + cbor_int!(3) => te.digit_count as i64, + cbor_int!(4) => te.algorithm, + cbor_int!(5) => te.name, } } } @@ -179,7 +177,7 @@ impl From<&TotpEntries> for Vec { fn from(te: &TotpEntries) -> Self { let mut ret = vec![]; let te_cbor: cbor::Value = te.into(); - cbor::write(te_cbor, &mut ret); + cbor::write(te_cbor, &mut ret).ok(); ret } } @@ -195,10 +193,10 @@ pub struct PasswordEntry { impl From for cbor::Value { fn from(te: PasswordEntry) -> Self { cbor_map! { - cbor_key_int!(1) => te.description, - cbor_key_int!(2) => te.username, - cbor_key_int!(3) => te.password, - cbor_key_int!(4) => te.notes, + cbor_int!(1) => te.description, + cbor_int!(2) => te.username, + cbor_int!(3) => te.password, + cbor_int!(4) => te.notes, } } } @@ -248,7 +246,7 @@ impl From<&PasswordEntries> for Vec { fn from(te: &PasswordEntries) -> Self { let mut ret = vec![]; let te_cbor: cbor::Value = te.into(); - cbor::write(te_cbor, &mut ret); + cbor::write(te_cbor, &mut ret).ok(); ret } } @@ -287,7 +285,7 @@ impl From for Vec { fn from(dp: DataPacket) -> Self { let mut ret = vec![]; let te_cbor: cbor::Value = dp.into(); - cbor::write(te_cbor, &mut ret); + cbor::write(te_cbor, &mut ret).ok(); ret } } @@ -298,14 +296,14 @@ impl From for cbor::Value { match dp { DataPacket::Password(ref p) => { cbor_map! { - cbor_key_int!(1) => dpt, - cbor_key_int!(2) => p, + cbor_int!(1) => dpt, + cbor_int!(2) => p, } } DataPacket::TOTP(ref t) => { cbor_map! { - cbor_key_int!(1) => dpt, - cbor_key_int!(2) => t, + cbor_int!(1) => dpt, + cbor_int!(2) => t, } } } @@ -401,7 +399,7 @@ impl TryFrom for PayloadSize { impl From<&PayloadSize> for cbor::Value { fn from(te: &PayloadSize) -> Self { cbor_map! { - cbor_key_int!(0) => te.0, + cbor_int!(0) => te.0, } } } @@ -410,7 +408,7 @@ impl From<&PayloadSize> for Vec { fn from(te: &PayloadSize) -> Self { let mut ret = vec![]; let te_cbor: cbor::Value = te.into(); - cbor::write(te_cbor, &mut ret); + cbor::write(te_cbor, &mut ret).ok(); ret } } @@ -468,10 +466,10 @@ impl TryFrom> for DataPacket { impl From<&Wire> for cbor::Value { fn from(te: &Wire) -> Self { cbor_map! { - cbor_key_int!(1) => te.index, - cbor_key_int!(2) => te.size, - cbor_key_int!(3) => te.more_data, - cbor_key_int!(4) => te.data.clone(), + cbor_int!(1) => te.index, + cbor_int!(2) => te.size, + cbor_int!(3) => te.more_data, + cbor_int!(4) => te.data.clone(), } } } @@ -480,7 +478,7 @@ impl From<&Wire> for Vec { fn from(te: &Wire) -> Self { let mut ret = vec![]; let te_cbor: cbor::Value = te.into(); - cbor::write(te_cbor, &mut ret); + cbor::write(te_cbor, &mut ret).ok(); ret } } @@ -527,14 +525,14 @@ fn extract_bool(cbor_value: cbor::Value) -> Result { fn extract_unsigned(cbor_value: cbor::Value) -> Result { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned), + cbor::Value::Unsigned(unsigned) => Ok(unsigned), _ => Err(CborConversionError::BadCbor), } } fn extract_byte_string(cbor_value: cbor::Value) -> Result, CborConversionError> { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::ByteString(byte_string)) => Ok(byte_string), + cbor::Value::ByteString(byte_string) => Ok(byte_string), _ => Err(CborConversionError::BadCbor), } } @@ -548,7 +546,7 @@ fn extract_array(cbor_value: cbor::Value) -> Result, CborConver fn extract_string(cbor_value: cbor::Value) -> Result { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::TextString(string)) => Ok(string), + cbor::Value::TextString(text_string) => Ok(text_string), _ => Err(CborConversionError::BadCbor), } } diff --git a/apps/vault/libraries/cbor/src/lib.rs b/apps/vault/libraries/cbor/src/lib.rs index 462a6bea1..ca70f6e40 100644 --- a/apps/vault/libraries/cbor/src/lib.rs +++ b/apps/vault/libraries/cbor/src/lib.rs @@ -18,5 +18,5 @@ pub mod values; pub mod writer; pub use self::reader::read; -pub use self::values::{KeyType, SimpleValue, Value}; +pub use self::values::{SimpleValue, Value}; pub use self::writer::write; diff --git a/apps/vault/libraries/cbor/src/macros.rs b/apps/vault/libraries/cbor/src/macros.rs index 368c83076..7d5a661c9 100644 --- a/apps/vault/libraries/cbor/src/macros.rs +++ b/apps/vault/libraries/cbor/src/macros.rs @@ -12,15 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::values::{KeyType, Value}; -use std::collections::btree_map; +//! Convenience macros for working with CBOR values. + +use crate::values::Value; +use std::vec; use core::cmp::Ordering; use core::iter::Peekable; -/// This macro generates code to extract multiple values from a `BTreeMap` at once -/// in an optimized manner, consuming the input map. +/// This macro generates code to extract multiple values from a `Vec<(Value, Value)>` at once +/// in an optimized manner, consuming the input vector. /// -/// It takes as input a `BTreeMap` as well as a list of identifiers and keys, and generates code +/// It takes as input a `Vec` as well as a list of identifiers and keys, and generates code /// that assigns the corresponding values to new variables using the given identifiers. Each of /// these variables has type `Option`, to account for the case where keys aren't found. /// @@ -32,16 +34,14 @@ use core::iter::Peekable; /// the keys are indeed sorted. This macro is therefore **not suitable for dynamic keys** that can /// change at runtime. /// -/// Semantically, provided that the keys are sorted as specified above, the following two snippets -/// of code are equivalent, but the `destructure_cbor_map!` version is more optimized, as it doesn't -/// re-balance the `BTreeMap` for each key, contrary to the `BTreeMap::remove` operations. +/// Example usage: /// /// ```rust /// # extern crate alloc; /// # use cbor::destructure_cbor_map; /// # /// # fn main() { -/// # let map = std::collections::BTreeMap::new(); +/// # let map = std::vec::Vec::new(); /// destructure_cbor_map! { /// let { /// 1 => x, @@ -50,17 +50,6 @@ use core::iter::Peekable; /// } /// # } /// ``` -/// -/// ```rust -/// # extern crate alloc; -/// # -/// # fn main() { -/// # let mut map = std::collections::BTreeMap::::new(); -/// use cbor::values::IntoCborKey; -/// let x: Option = map.remove(&1.into_cbor_key()); -/// let y: Option = map.remove(&"key".into_cbor_key()); -/// # } -/// ``` #[macro_export] macro_rules! destructure_cbor_map { ( let { $( $key:expr => $variable:ident, )+ } = $map:expr; ) => { @@ -70,7 +59,7 @@ macro_rules! destructure_cbor_map { #[cfg(test)] $crate::assert_sorted_keys!($( $key, )+); - use $crate::values::{IntoCborKey, Value}; + use $crate::values::{IntoCborValue, Value}; use $crate::macros::destructure_cbor_map_peek_value; // This algorithm first converts the map into a peekable iterator - whose items are sorted @@ -83,7 +72,7 @@ macro_rules! destructure_cbor_map { // to come in the same order (i.e. sorted). let mut it = $map.into_iter().peekable(); $( - let $variable: Option = destructure_cbor_map_peek_value(&mut it, $key.into_cbor_key()); + let $variable: Option = destructure_cbor_map_peek_value(&mut it, $key.into_cbor_value()); )+ }; } @@ -100,14 +89,14 @@ macro_rules! destructure_cbor_map { /// would be inlined for every use case. As of June 2020, this saves ~40KB of binary size for the /// CTAP2 application of OpenSK. pub fn destructure_cbor_map_peek_value( - it: &mut Peekable>, - needle: KeyType, + it: &mut Peekable>, + needle: Value, ) -> Option { loop { match it.peek() { None => return None, Some(item) => { - let key: &KeyType = &item.0; + let key: &Value = &item.0; match key.cmp(&needle) { Ordering::Less => { it.next(); @@ -123,6 +112,7 @@ pub fn destructure_cbor_map_peek_value( } } +/// Assert that the keys in a vector of key-value pairs are in canonical order. #[macro_export] macro_rules! assert_sorted_keys { // Last key @@ -131,9 +121,9 @@ macro_rules! assert_sorted_keys { ( $key1:expr, $key2:expr, $( $keys:expr, )* ) => { { - use $crate::values::{IntoCborKey, KeyType}; - let k1: KeyType = $key1.into_cbor_key(); - let k2: KeyType = $key2.into_cbor_key(); + use $crate::values::{IntoCborValue, Value}; + let k1: Value = $key1.into_cbor_value(); + let k2: Value = $key2.into_cbor_value(); assert!( k1 < k2, "{:?} < {:?} failed. The destructure_cbor_map! macro requires keys in sorted order.", @@ -145,6 +135,23 @@ macro_rules! assert_sorted_keys { }; } +/// Creates a CBOR Value of type Map with the specified key-value pairs. +/// +/// Keys and values are expressions and converted into CBOR Keys and Values. +/// The syntax for these pairs is `key_expression => value_expression,`. +/// Duplicate keys will lead to invalid CBOR, i.e. writing these values fails. +/// Keys do not have to be sorted. +/// +/// Example usage: +/// +/// ```rust +/// # extern crate alloc; +/// # use cbor::cbor_map; +/// let map = cbor_map! { +/// 0x01 => false, +/// "02" => -3, +/// }; +/// ``` #[macro_export] macro_rules! cbor_map { // trailing comma case @@ -156,16 +163,36 @@ macro_rules! cbor_map { { // The import is unused if the list is empty. #[allow(unused_imports)] - use $crate::values::{IntoCborKey, IntoCborValue}; - let mut _map = ::std::collections::BTreeMap::new(); + use $crate::values::IntoCborValue; + let mut _map = ::std::vec::Vec::new(); $( - _map.insert($key.into_cbor_key(), $value.into_cbor_value()); + _map.push(($key.into_cbor_value(), $value.into_cbor_value())); )* $crate::values::Value::Map(_map) } }; } +/// Creates a CBOR Value of type Map with key-value pairs where values can be Options. +/// +/// Keys and values are expressions and converted into CBOR Keys and Value Options. +/// The map entry is included iff the Value is not an Option or Option is Some. +/// The syntax for these pairs is `key_expression => value_expression,`. +/// Duplicate keys will lead to invalid CBOR, i.e. writing these values fails. +/// Keys do not have to be sorted. +/// +/// Example usage: +/// +/// ```rust +/// # extern crate alloc; +/// # use cbor::cbor_map_options; +/// let missing_value: Option = None; +/// let map = cbor_map_options! { +/// 0x01 => Some(false), +/// "02" => -3, +/// "not in map" => missing_value, +/// }; +/// ``` #[macro_export] macro_rules! cbor_map_options { // trailing comma case @@ -177,13 +204,13 @@ macro_rules! cbor_map_options { { // The import is unused if the list is empty. #[allow(unused_imports)] - use $crate::values::{IntoCborKey, IntoCborValueOption}; - let mut _map = ::std::collections::BTreeMap::<_, $crate::values::Value>::new(); + use $crate::values::{IntoCborValue, IntoCborValueOption}; + let mut _map = ::std::vec::Vec::<(_, $crate::values::Value)>::new(); $( { let opt: Option<$crate::values::Value> = $value.into_cbor_value_option(); if let Some(val) = opt { - _map.insert($key.into_cbor_key(), val); + _map.push(($key.into_cbor_value(), val)); } } )* @@ -192,13 +219,25 @@ macro_rules! cbor_map_options { }; } +/// Creates a CBOR Value of type Map from a Vec<(Value, Value)>. #[macro_export] -macro_rules! cbor_map_btree { - ( $tree:expr ) => { - $crate::values::Value::Map($tree) - }; +macro_rules! cbor_map_collection { + ( $tree:expr ) => {{ + $crate::values::Value::from($tree) + }}; } +/// Creates a CBOR Value of type Array with the given elements. +/// +/// Elements are expressions and converted into CBOR Values. Elements are comma-separated. +/// +/// Example usage: +/// +/// ```rust +/// # extern crate alloc; +/// # use cbor::cbor_array; +/// let array = cbor_array![1, "2"]; +/// ``` #[macro_export] macro_rules! cbor_array { // trailing comma case @@ -216,6 +255,7 @@ macro_rules! cbor_array { }; } +/// Creates a CBOR Value of type Array from a Vec. #[macro_export] macro_rules! cbor_array_vec { ( $vec:expr ) => {{ @@ -224,6 +264,7 @@ macro_rules! cbor_array_vec { }}; } +/// Creates a CBOR Value of type Simple with value true. #[macro_export] macro_rules! cbor_true { ( ) => { @@ -231,6 +272,7 @@ macro_rules! cbor_true { }; } +/// Creates a CBOR Value of type Simple with value false. #[macro_export] macro_rules! cbor_false { ( ) => { @@ -238,6 +280,7 @@ macro_rules! cbor_false { }; } +/// Creates a CBOR Value of type Simple with value null. #[macro_export] macro_rules! cbor_null { ( ) => { @@ -245,6 +288,7 @@ macro_rules! cbor_null { }; } +/// Creates a CBOR Value of type Simple with the undefined value. #[macro_export] macro_rules! cbor_undefined { ( ) => { @@ -252,6 +296,7 @@ macro_rules! cbor_undefined { }; } +/// Creates a CBOR Value of type Simple with the given bool value. #[macro_export] macro_rules! cbor_bool { ( $x:expr ) => { @@ -259,37 +304,55 @@ macro_rules! cbor_bool { }; } -// For key types, we construct a KeyType and call .into(), which will automatically convert it to a -// KeyType or a Value depending on the context. +/// Creates a CBOR Value of type Unsigned with the given numeric value. #[macro_export] macro_rules! cbor_unsigned { ( $x:expr ) => { - $crate::cbor_key_unsigned!($x).into() + $crate::values::Value::Unsigned($x) }; } +/// Creates a CBOR Value of type Unsigned or Negative with the given numeric value. #[macro_export] macro_rules! cbor_int { ( $x:expr ) => { - $crate::cbor_key_int!($x).into() + $crate::values::Value::integer($x) }; } +/// Creates a CBOR Value of type Text String with the given string. #[macro_export] macro_rules! cbor_text { ( $x:expr ) => { - $crate::cbor_key_text!($x).into() + $crate::values::Value::TextString($x.into()) }; } +/// Creates a CBOR Value of type Byte String with the given slice or vector. #[macro_export] macro_rules! cbor_bytes { ( $x:expr ) => { - $crate::cbor_key_bytes!($x).into() + $crate::values::Value::ByteString($x) }; } -// Macro to use with a literal, e.g. cbor_bytes_lit!(b"foo") +/// Creates a CBOR Value of type Tag with the given tag and object. +#[macro_export] +macro_rules! cbor_tagged { + ( $t:expr, $x: expr ) => { + $crate::values::Value::Tag($t, ::std::boxed::Box::new($x)) + }; +} + +/// Creates a CBOR Value of type Byte String with the given byte string literal. +/// +/// Example usage: +/// +/// ```rust +/// # extern crate alloc; +/// # use cbor::cbor_bytes_lit; +/// let byte_array = cbor_bytes_lit!(b"foo"); +/// ``` #[macro_export] macro_rules! cbor_bytes_lit { ( $x:expr ) => { @@ -297,39 +360,12 @@ macro_rules! cbor_bytes_lit { }; } -// Some explicit macros are also available for contexts where the type is not explicit. -#[macro_export] -macro_rules! cbor_key_unsigned { - ( $x:expr ) => { - $crate::values::KeyType::Unsigned($x) - }; -} - -#[macro_export] -macro_rules! cbor_key_int { - ( $x:expr ) => { - $crate::values::KeyType::integer($x) - }; -} - -#[macro_export] -macro_rules! cbor_key_text { - ( $x:expr ) => { - $crate::values::KeyType::TextString($x.into()) - }; -} - -#[macro_export] -macro_rules! cbor_key_bytes { - ( $x:expr ) => { - $crate::values::KeyType::ByteString($x) - }; -} - #[cfg(test)] mod test { - use super::super::values::{KeyType, SimpleValue, Value}; - use std::collections::BTreeMap; + use super::super::values::{SimpleValue, Value}; + use std::string::String; + use std::vec; + use std::vec::Vec; #[test] fn test_cbor_simple_values() { @@ -347,50 +383,47 @@ mod test { #[test] fn test_cbor_int_unsigned() { - assert_eq!(cbor_key_int!(0), KeyType::Unsigned(0)); - assert_eq!(cbor_key_int!(1), KeyType::Unsigned(1)); - assert_eq!(cbor_key_int!(123456), KeyType::Unsigned(123456)); + assert_eq!(cbor_int!(0), Value::Unsigned(0)); + assert_eq!(cbor_int!(1), Value::Unsigned(1)); + assert_eq!(cbor_int!(123456), Value::Unsigned(123456)); assert_eq!( - cbor_key_int!(std::i64::MAX), - KeyType::Unsigned(std::i64::MAX as u64) + cbor_int!(core::i64::MAX), + Value::Unsigned(core::i64::MAX as u64) ); } #[test] fn test_cbor_int_negative() { - assert_eq!(cbor_key_int!(-1), KeyType::Negative(-1)); - assert_eq!(cbor_key_int!(-123456), KeyType::Negative(-123456)); - assert_eq!( - cbor_key_int!(std::i64::MIN), - KeyType::Negative(std::i64::MIN) - ); + assert_eq!(cbor_int!(-1), Value::Negative(-1)); + assert_eq!(cbor_int!(-123456), Value::Negative(-123456)); + assert_eq!(cbor_int!(core::i64::MIN), Value::Negative(core::i64::MIN)); } #[test] fn test_cbor_int_literals() { let a = cbor_array![ - std::i64::MIN, - std::i32::MIN, + core::i64::MIN, + core::i32::MIN, -123456, -1, 0, 1, 123456, - std::i32::MAX, - std::i64::MAX, - std::u64::MAX, + core::i32::MAX, + core::i64::MAX, + core::u64::MAX, ]; let b = Value::Array(vec![ - Value::KeyValue(KeyType::Negative(std::i64::MIN)), - Value::KeyValue(KeyType::Negative(std::i32::MIN as i64)), - Value::KeyValue(KeyType::Negative(-123456)), - Value::KeyValue(KeyType::Negative(-1)), - Value::KeyValue(KeyType::Unsigned(0)), - Value::KeyValue(KeyType::Unsigned(1)), - Value::KeyValue(KeyType::Unsigned(123456)), - Value::KeyValue(KeyType::Unsigned(std::i32::MAX as u64)), - Value::KeyValue(KeyType::Unsigned(std::i64::MAX as u64)), - Value::KeyValue(KeyType::Unsigned(std::u64::MAX)), + Value::Negative(core::i64::MIN), + Value::Negative(core::i32::MIN as i64), + Value::Negative(-123456), + Value::Negative(-1), + Value::Unsigned(0), + Value::Unsigned(1), + Value::Unsigned(123456), + Value::Unsigned(core::i32::MAX as u64), + Value::Unsigned(core::i64::MAX as u64), + Value::Unsigned(core::u64::MAX), ]); assert_eq!(a, b); } @@ -410,20 +443,17 @@ mod test { cbor_map! {2 => 3}, ]; let b = Value::Array(vec![ - Value::KeyValue(KeyType::Negative(-123)), - Value::KeyValue(KeyType::Unsigned(456)), + Value::Negative(-123), + Value::Unsigned(456), Value::Simple(SimpleValue::TrueValue), Value::Simple(SimpleValue::NullValue), - Value::KeyValue(KeyType::TextString(String::from("foo"))), - Value::KeyValue(KeyType::ByteString(b"bar".to_vec())), + Value::TextString(String::from("foo")), + Value::ByteString(b"bar".to_vec()), Value::Array(Vec::new()), - Value::Array(vec![ - Value::KeyValue(KeyType::Unsigned(0)), - Value::KeyValue(KeyType::Unsigned(1)), - ]), - Value::Map(BTreeMap::new()), + Value::Array(vec![Value::Unsigned(0), Value::Unsigned(1)]), + Value::Map(Vec::new()), Value::Map( - [(KeyType::Unsigned(2), Value::KeyValue(KeyType::Unsigned(3)))] + [(Value::Unsigned(2), Value::Unsigned(3))] .iter() .cloned() .collect(), @@ -443,10 +473,10 @@ mod test { fn test_cbor_array_vec_int() { let a = cbor_array_vec!(vec![1, 2, 3, 4]); let b = Value::Array(vec![ - Value::KeyValue(KeyType::Unsigned(1)), - Value::KeyValue(KeyType::Unsigned(2)), - Value::KeyValue(KeyType::Unsigned(3)), - Value::KeyValue(KeyType::Unsigned(4)), + Value::Unsigned(1), + Value::Unsigned(2), + Value::Unsigned(3), + Value::Unsigned(4), ]); assert_eq!(a, b); } @@ -455,9 +485,9 @@ mod test { fn test_cbor_array_vec_text() { let a = cbor_array_vec!(vec!["a", "b", "c"]); let b = Value::Array(vec![ - Value::KeyValue(KeyType::TextString(String::from("a"))), - Value::KeyValue(KeyType::TextString(String::from("b"))), - Value::KeyValue(KeyType::TextString(String::from("c"))), + Value::TextString(String::from("a")), + Value::TextString(String::from("b")), + Value::TextString(String::from("c")), ]); assert_eq!(a, b); } @@ -466,9 +496,9 @@ mod test { fn test_cbor_array_vec_bytes() { let a = cbor_array_vec!(vec![b"a", b"b", b"c"]); let b = Value::Array(vec![ - Value::KeyValue(KeyType::ByteString(b"a".to_vec())), - Value::KeyValue(KeyType::ByteString(b"b".to_vec())), - Value::KeyValue(KeyType::ByteString(b"c".to_vec())), + Value::ByteString(b"a".to_vec()), + Value::ByteString(b"b".to_vec()), + Value::ByteString(b"c".to_vec()), ]); assert_eq!(a, b); } @@ -489,40 +519,28 @@ mod test { }; let b = Value::Map( [ + (Value::Negative(-1), Value::Negative(-23)), + (Value::Unsigned(4), Value::Unsigned(56)), ( - KeyType::Negative(-1), - Value::KeyValue(KeyType::Negative(-23)), - ), - (KeyType::Unsigned(4), Value::KeyValue(KeyType::Unsigned(56))), - ( - KeyType::TextString(String::from("foo")), + Value::TextString(String::from("foo")), Value::Simple(SimpleValue::TrueValue), ), ( - KeyType::ByteString(b"bar".to_vec()), + Value::ByteString(b"bar".to_vec()), Value::Simple(SimpleValue::NullValue), ), + (Value::Unsigned(5), Value::TextString(String::from("foo"))), + (Value::Unsigned(6), Value::ByteString(b"bar".to_vec())), + (Value::Unsigned(7), Value::Array(Vec::new())), ( - KeyType::Unsigned(5), - Value::KeyValue(KeyType::TextString(String::from("foo"))), + Value::Unsigned(8), + Value::Array(vec![Value::Unsigned(0), Value::Unsigned(1)]), ), + (Value::Unsigned(9), Value::Map(Vec::new())), ( - KeyType::Unsigned(6), - Value::KeyValue(KeyType::ByteString(b"bar".to_vec())), - ), - (KeyType::Unsigned(7), Value::Array(Vec::new())), - ( - KeyType::Unsigned(8), - Value::Array(vec![ - Value::KeyValue(KeyType::Unsigned(0)), - Value::KeyValue(KeyType::Unsigned(1)), - ]), - ), - (KeyType::Unsigned(9), Value::Map(BTreeMap::new())), - ( - KeyType::Unsigned(10), + Value::Unsigned(10), Value::Map( - [(KeyType::Unsigned(2), Value::KeyValue(KeyType::Unsigned(3)))] + [(Value::Unsigned(2), Value::Unsigned(3))] .iter() .cloned() .collect(), @@ -560,40 +578,28 @@ mod test { }; let b = Value::Map( [ + (Value::Negative(-1), Value::Negative(-23)), + (Value::Unsigned(4), Value::Unsigned(56)), ( - KeyType::Negative(-1), - Value::KeyValue(KeyType::Negative(-23)), - ), - (KeyType::Unsigned(4), Value::KeyValue(KeyType::Unsigned(56))), - ( - KeyType::TextString(String::from("foo")), + Value::TextString(String::from("foo")), Value::Simple(SimpleValue::TrueValue), ), ( - KeyType::ByteString(b"bar".to_vec()), + Value::ByteString(b"bar".to_vec()), Value::Simple(SimpleValue::NullValue), ), + (Value::Unsigned(5), Value::TextString(String::from("foo"))), + (Value::Unsigned(6), Value::ByteString(b"bar".to_vec())), + (Value::Unsigned(7), Value::Array(Vec::new())), ( - KeyType::Unsigned(5), - Value::KeyValue(KeyType::TextString(String::from("foo"))), + Value::Unsigned(8), + Value::Array(vec![Value::Unsigned(0), Value::Unsigned(1)]), ), + (Value::Unsigned(9), Value::Map(Vec::new())), ( - KeyType::Unsigned(6), - Value::KeyValue(KeyType::ByteString(b"bar".to_vec())), - ), - (KeyType::Unsigned(7), Value::Array(Vec::new())), - ( - KeyType::Unsigned(8), - Value::Array(vec![ - Value::KeyValue(KeyType::Unsigned(0)), - Value::KeyValue(KeyType::Unsigned(1)), - ]), - ), - (KeyType::Unsigned(9), Value::Map(BTreeMap::new())), - ( - KeyType::Unsigned(10), + Value::Unsigned(10), Value::Map( - [(KeyType::Unsigned(2), Value::KeyValue(KeyType::Unsigned(3)))] + [(Value::Unsigned(2), Value::Unsigned(3))] .iter() .cloned() .collect(), @@ -608,30 +614,20 @@ mod test { } #[test] - fn test_cbor_map_btree_empty() { - let a = cbor_map_btree!(BTreeMap::new()); - let b = Value::Map(BTreeMap::new()); + fn test_cbor_map_collection_empty() { + let a = cbor_map_collection!(Vec::<(_, _)>::new()); + let b = Value::Map(Vec::new()); assert_eq!(a, b); } #[test] - fn test_cbor_map_btree_foo() { - let a = cbor_map_btree!( - [(KeyType::Unsigned(2), Value::KeyValue(KeyType::Unsigned(3)))] - .iter() - .cloned() - .collect() - ); - let b = Value::Map( - [(KeyType::Unsigned(2), Value::KeyValue(KeyType::Unsigned(3)))] - .iter() - .cloned() - .collect(), - ); + fn test_cbor_map_collection_foo() { + let a = cbor_map_collection!(vec![(Value::Unsigned(2), Value::Unsigned(3))]); + let b = Value::Map(vec![(Value::Unsigned(2), Value::Unsigned(3))]); assert_eq!(a, b); } - fn extract_map(cbor_value: Value) -> BTreeMap { + fn extract_map(cbor_value: Value) -> Vec<(Value, Value)> { match cbor_value { Value::Map(map) => map, _ => panic!("Expected CBOR map."), diff --git a/apps/vault/libraries/cbor/src/reader.rs b/apps/vault/libraries/cbor/src/reader.rs index a955a1df0..f19ef854b 100644 --- a/apps/vault/libraries/cbor/src/reader.rs +++ b/apps/vault/libraries/cbor/src/reader.rs @@ -12,21 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::values::{Constants, KeyType, SimpleValue, Value}; -use crate::{cbor_array_vec, cbor_bytes_lit, cbor_map_btree, cbor_text, cbor_unsigned}; -use std::collections::BTreeMap; +//! Functionality for deserializing CBOR data into values. + +use super::values::{Constants, SimpleValue, Value}; +use crate::{ + cbor_array_vec, cbor_bytes_lit, cbor_map_collection, cbor_tagged, cbor_text, cbor_unsigned, +}; use std::str; use std::vec::Vec; +/// Possible errors from a deserialization operation. #[derive(Debug, PartialEq)] pub enum DecoderError { UnsupportedMajorType, UnknownAdditionalInfo, IncompleteCborData, - IncorrectMapKeyType, TooMuchNesting, InvalidUtf8, - ExtranousData, + ExtraneousData, OutOfOrderKey, NonMinimalCborEncoding, UnsupportedSimpleValue, @@ -34,11 +37,21 @@ pub enum DecoderError { OutOfRangeIntegerValue, } +/// Deserialize CBOR binary data to produce a single [`Value`], expecting that there is no additional data. +/// Maximum level of nesting supported is 127; more deeply nested structures will fail with +/// [`DecoderError::TooMuchNesting`]. pub fn read(encoded_cbor: &[u8]) -> Result { + read_nested(encoded_cbor, Some(i8::MAX)) +} + +/// Deserialize CBOR binary data to produce a single [`Value`], expecting that there is no additional data. If +/// `max_nest` is `Some(max)`, then nested structures are only supported up to the given limit (returning +/// [`DecoderError::TooMuchNesting`] if the limit is hit). +pub fn read_nested(encoded_cbor: &[u8], max_nest: Option) -> Result { let mut reader = Reader::new(encoded_cbor); - let value = reader.decode_complete_data_item(Reader::MAX_NESTING_DEPTH)?; + let value = reader.decode_complete_data_item(max_nest)?; if !reader.remaining_cbor.is_empty() { - return Err(DecoderError::ExtranousData); + return Err(DecoderError::ExtraneousData); } Ok(value) } @@ -48,8 +61,6 @@ struct Reader<'a> { } impl<'a> Reader<'a> { - const MAX_NESTING_DEPTH: i8 = 4; - pub fn new(cbor: &'a [u8]) -> Reader<'a> { Reader { remaining_cbor: cbor, @@ -58,9 +69,9 @@ impl<'a> Reader<'a> { pub fn decode_complete_data_item( &mut self, - remaining_depth: i8, + remaining_depth: Option, ) -> Result { - if remaining_depth < 0 { + if remaining_depth.map_or(false, |d| d < 0) { return Err(DecoderError::TooMuchNesting); } @@ -69,19 +80,17 @@ impl<'a> Reader<'a> { // Unsigned byte means logical shift, so only zeros get shifted in. let major_type_value = first_byte >> Constants::MAJOR_TYPE_BIT_SHIFT; let additional_info = first_byte & Constants::ADDITIONAL_INFORMATION_MASK; - let size_result = self.read_variadic_length_integer(additional_info); - match size_result { - Ok(size_value) => match major_type_value { - 0 => self.decode_value_to_unsigned(size_value), - 1 => self.decode_value_to_negative(size_value), - 2 => self.read_byte_string_content(size_value), - 3 => self.read_text_string_content(size_value), - 4 => self.read_array_content(size_value, remaining_depth), - 5 => self.read_map_content(size_value, remaining_depth), - 7 => self.decode_to_simple_value(size_value, additional_info), - _ => Err(DecoderError::UnsupportedMajorType), - }, - Err(decode_error) => Err(decode_error), + let size_value = self.read_variadic_length_integer(additional_info)?; + match major_type_value { + 0 => self.decode_value_to_unsigned(size_value), + 1 => self.decode_value_to_negative(size_value), + 2 => self.read_byte_string_content(size_value), + 3 => self.read_text_string_content(size_value), + 4 => self.read_array_content(size_value, remaining_depth), + 5 => self.read_map_content(size_value, remaining_depth), + 6 => self.read_tagged_content(size_value, remaining_depth), + 7 => self.decode_to_simple_value(size_value, additional_info), + _ => Err(DecoderError::UnsupportedMajorType), } } _ => Err(DecoderError::IncompleteCborData), @@ -135,7 +144,7 @@ impl<'a> Reader<'a> { if signed_size < 0 { Err(DecoderError::OutOfRangeIntegerValue) } else { - Ok(Value::KeyValue(KeyType::Negative(-(size_value as i64) - 1))) + Ok(Value::Negative(-(size_value as i64) - 1)) } } @@ -159,12 +168,12 @@ impl<'a> Reader<'a> { fn read_array_content( &mut self, size_value: u64, - remaining_depth: i8, + remaining_depth: Option, ) -> Result { // Don't set the capacity already, it is an unsanitized input. let mut value_array = Vec::new(); for _ in 0..size_value { - value_array.push(self.decode_complete_data_item(remaining_depth - 1)?); + value_array.push(self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?); } Ok(cbor_array_vec!(value_array)) } @@ -172,25 +181,31 @@ impl<'a> Reader<'a> { fn read_map_content( &mut self, size_value: u64, - remaining_depth: i8, + remaining_depth: Option, ) -> Result { - let mut value_map = BTreeMap::new(); - let mut last_key_option = None; + let mut value_map = Vec::<(Value, Value)>::new(); for _ in 0..size_value { - let key_value = self.decode_complete_data_item(remaining_depth - 1)?; - if let Value::KeyValue(key) = key_value { - if let Some(last_key) = last_key_option { - if last_key >= key { - return Err(DecoderError::OutOfOrderKey); - } + let key = self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?; + if let Some(last_item) = value_map.last() { + if last_item.0 >= key { + return Err(DecoderError::OutOfOrderKey); } - last_key_option = Some(key.clone()); - value_map.insert(key, self.decode_complete_data_item(remaining_depth - 1)?); - } else { - return Err(DecoderError::IncorrectMapKeyType); } + value_map.push(( + key, + self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?, + )); } - Ok(cbor_map_btree!(value_map)) + Ok(cbor_map_collection!(value_map)) + } + + fn read_tagged_content( + &mut self, + tag_value: u64, + remaining_depth: Option, + ) -> Result { + let inner_value = self.decode_complete_data_item(remaining_depth.map(|d| d - 1))?; + Ok(cbor_tagged!(tag_value, inner_value)) } fn decode_to_simple_value( @@ -219,6 +234,7 @@ mod test { cbor_array, cbor_bytes, cbor_false, cbor_int, cbor_map, cbor_null, cbor_true, cbor_undefined, }; + use alloc::vec; #[test] fn test_read_unsigned() { @@ -238,14 +254,14 @@ mod test { vec![0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MAX, + core::i64::MAX, vec![0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; for (unsigned, mut cbor) in cases { assert_eq!(read(&cbor), Ok(cbor_int!(unsigned))); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -300,14 +316,14 @@ mod test { (-1000000, vec![0x3A, 0x00, 0x0F, 0x42, 0x3F]), (-4294967296, vec![0x3A, 0xFF, 0xFF, 0xFF, 0xFF]), ( - std::i64::MIN, + core::i64::MIN, vec![0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; for (negative, mut cbor) in cases { assert_eq!(read(&cbor), Ok(cbor_int!(negative))); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -323,7 +339,7 @@ mod test { for (byte_string, mut cbor) in cases { assert_eq!(read(&cbor), Ok(cbor_bytes!(byte_string))); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -337,7 +353,7 @@ mod test { ("\"\\", vec![0x62, 0x22, 0x5C]), ("ü", vec![0x62, 0xC3, 0xBC]), ( - std::str::from_utf8(&unicode_3byte).unwrap(), + core::str::from_utf8(&unicode_3byte).unwrap(), vec![0x63, 0xE6, 0xB0, 0xB4], ), ("𐅑", vec![0x64, 0xF0, 0x90, 0x85, 0x91]), @@ -345,7 +361,7 @@ mod test { for (text_string, mut cbor) in cases { assert_eq!(read(&cbor), Ok(cbor_text!(text_string))); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -383,7 +399,7 @@ mod test { for (text_string, mut cbor) in cases { assert_eq!(read(&cbor), Ok(cbor_text!(text_string))); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -405,7 +421,7 @@ mod test { ]; assert_eq!(read(&test_cbor.clone()), Ok(cbor_array_vec!(value_vec))); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -429,7 +445,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -453,7 +469,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -474,7 +490,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -492,7 +508,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -509,7 +525,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -526,7 +542,7 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); } #[test] @@ -548,7 +564,36 @@ mod test { ]; assert_eq!(read(&test_cbor), Ok(value_map)); test_cbor.push(0x01); - assert_eq!(read(&test_cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&test_cbor), Err(DecoderError::ExtraneousData)); + } + + #[test] + fn test_read_tagged() { + let cases = vec![ + (cbor_tagged!(6, cbor_int!(0x42)), vec![0xc6, 0x18, 0x42]), + (cbor_tagged!(1, cbor_true!()), vec![0xc1, 0xf5]), + ( + cbor_tagged!( + 1000, + cbor_map! { + "a" => 1, + "b" => cbor_array![2, 3], + } + ), + vec![ + 0xd9, 0x03, 0xe8, 0xa2, // map of 2 pairs + 0x61, 0x61, // "a" + 0x01, 0x61, 0x62, // "b" + 0x82, // array with 2 elements + 0x02, 0x03, + ], + ), + ]; + for (value, mut cbor) in cases { + assert_eq!(read(&cbor), Ok(value)); + cbor.push(0x01); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); + } } #[test] @@ -574,7 +619,7 @@ mod test { for (simple, mut cbor) in cases { assert_eq!(read(&cbor.clone()), Ok(simple)); cbor.push(0x01); - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -615,19 +660,6 @@ mod test { } } - #[test] - fn test_read_unsupported_map_key_format_error() { - // While CBOR can handle all types as map keys, we only support a subset. - let bad_map_cbor = vec![ - 0xa2, // map of 2 pairs - 0x82, 0x01, 0x02, // invalid key : [1, 2] - 0x02, // value : 2 - 0x61, 0x64, // key : "d" - 0x03, // value : 3 - ]; - assert_eq!(read(&bad_map_cbor), Err(DecoderError::IncorrectMapKeyType)); - } - #[test] fn test_read_unknown_additional_info_error() { let cases = vec![ @@ -656,7 +688,7 @@ mod test { ]; for cbor in cases { let mut reader = Reader::new(&cbor); - assert!(reader.decode_complete_data_item(0).is_ok()); + assert!(reader.decode_complete_data_item(Some(0)).is_ok()); } let map_cbor = vec![ 0xa2, // map of 2 pairs @@ -667,11 +699,11 @@ mod test { ]; let mut reader = Reader::new(&map_cbor); assert_eq!( - reader.decode_complete_data_item(1), + reader.decode_complete_data_item(Some(1)), Err(DecoderError::TooMuchNesting) ); reader = Reader::new(&map_cbor); - assert!(reader.decode_complete_data_item(2).is_ok()); + assert!(reader.decode_complete_data_item(Some(2)).is_ok()); } #[test] @@ -756,7 +788,7 @@ mod test { } #[test] - fn test_read_extranous_cbor_data_error() { + fn test_read_extraneous_cbor_data_error() { let cases = vec![ vec![0x19, 0x03, 0x05, 0x00], vec![0x44, 0x01, 0x02, 0x03, 0x04, 0x00], @@ -765,7 +797,7 @@ mod test { vec![0xa1, 0x61, 0x63, 0x02, 0x61, 0x64, 0x03], ]; for cbor in cases { - assert_eq!(read(&cbor), Err(DecoderError::ExtranousData)); + assert_eq!(read(&cbor), Err(DecoderError::ExtraneousData)); } } @@ -797,20 +829,4 @@ mod test { assert_eq!(read(&cbor), Err(DecoderError::IncompleteCborData)); } } - - #[test] - fn test_read_unsupported_major_type() { - let cases = vec![ - vec![0xC0], - vec![0xD8, 0xFF], - // multi-dimensional array example using tags - vec![ - 0x82, 0x82, 0x02, 0x03, 0xd8, 0x41, 0x4a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, - 0x03, 0x00, 0x04, 0x00, 0x05, - ], - ]; - for cbor in cases { - assert_eq!(read(&cbor), Err(DecoderError::UnsupportedMajorType)); - } - } } diff --git a/apps/vault/libraries/cbor/src/values.rs b/apps/vault/libraries/cbor/src/values.rs index 3c9a9f67a..6cc1dce37 100644 --- a/apps/vault/libraries/cbor/src/values.rs +++ b/apps/vault/libraries/cbor/src/values.rs @@ -12,32 +12,36 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::collections::BTreeMap; +//! Types for expressing CBOR values. + +use std::boxed::Box; use std::string::{String, ToString}; use std::vec::Vec; use core::cmp::Ordering; -#[derive(Clone, Debug, PartialEq)] +/// Possible CBOR values. +#[derive(Clone, Debug)] pub enum Value { - KeyValue(KeyType), + /// Unsigned integer value (uint). + Unsigned(u64), + /// Signed integer value (nint). Only 63 bits of information are used here. + Negative(i64), + /// Byte string (bstr). + ByteString(Vec), + /// Text string (tstr). + TextString(String), + /// Array/tuple of values. Array(Vec), - Map(BTreeMap), - // TAG is omitted + /// Map of key-value pairs. + Map(Vec<(Value, Value)>), + /// Tagged value. + Tag(u64, Box), + /// Simple value. Simple(SimpleValue), } -// The specification recommends to limit the available keys. -// Currently supported are both integer and string types. -#[derive(Clone, Debug, Eq, PartialEq)] -pub enum KeyType { - Unsigned(u64), - // We only use 63 bits of information here. - Negative(i64), - ByteString(Vec), - TextString(String), -} - -#[derive(Clone, Debug, PartialEq)] +/// Specific simple CBOR values. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum SimpleValue { FalseValue = 20, TrueValue = 21, @@ -45,19 +49,39 @@ pub enum SimpleValue { Undefined = 23, } +/// Constant values required for CBOR encoding. pub struct Constants {} impl Constants { + /// Number of bits used to shift left the major type of a CBOR type byte. pub const MAJOR_TYPE_BIT_SHIFT: u8 = 5; + /// Mask to retrieve the additional information held in a CBOR type bytes, + /// ignoring the major type. pub const ADDITIONAL_INFORMATION_MASK: u8 = 0x1F; + /// Additional information value that indicates the largest inline value. pub const ADDITIONAL_INFORMATION_MAX_INT: u8 = 23; + /// Additional information value indicating that a 1-byte length follows. pub const ADDITIONAL_INFORMATION_1_BYTE: u8 = 24; + /// Additional information value indicating that a 2-byte length follows. pub const ADDITIONAL_INFORMATION_2_BYTES: u8 = 25; + /// Additional information value indicating that a 4-byte length follows. pub const ADDITIONAL_INFORMATION_4_BYTES: u8 = 26; + /// Additional information value indicating that an 8-byte length follows. pub const ADDITIONAL_INFORMATION_8_BYTES: u8 = 27; } impl Value { + /// Create an appropriate CBOR integer value (uint/nint). + /// For simplicity, this only takes i64. Construct directly for the last bit. + pub fn integer(int: i64) -> Value { + if int >= 0 { + Value::Unsigned(int as u64) + } else { + Value::Negative(int) + } + } + + /// Create a CBOR boolean simple value. pub fn bool_value(b: bool) -> Value { if b { Value::Simple(SimpleValue::TrueValue) @@ -66,39 +90,28 @@ impl Value { } } + /// Return the major type for the [`Value`]. pub fn type_label(&self) -> u8 { + // TODO use enum discriminant instead when stable + // https://github.com/rust-lang/rust/issues/60553 match self { - Value::KeyValue(key) => key.type_label(), + Value::Unsigned(_) => 0, + Value::Negative(_) => 1, + Value::ByteString(_) => 2, + Value::TextString(_) => 3, Value::Array(_) => 4, Value::Map(_) => 5, + Value::Tag(_, _) => 6, Value::Simple(_) => 7, } } } -impl KeyType { - // For simplicity, this only takes i64. Construct directly for the last bit. - pub fn integer(int: i64) -> KeyType { - if int >= 0 { - KeyType::Unsigned(int as u64) - } else { - KeyType::Negative(int) - } - } - - pub fn type_label(&self) -> u8 { - match self { - KeyType::Unsigned(_) => 0, - KeyType::Negative(_) => 1, - KeyType::ByteString(_) => 2, - KeyType::TextString(_) => 3, - } - } -} - -impl Ord for KeyType { - fn cmp(&self, other: &KeyType) -> Ordering { - use super::values::KeyType::{ByteString, Negative, TextString, Unsigned}; +impl Ord for Value { + fn cmp(&self, other: &Value) -> Ordering { + use super::values::Value::{ + Array, ByteString, Map, Negative, Simple, Tag, TextString, Unsigned, + }; let self_type_value = self.type_label(); let other_type_value = other.type_label(); if self_type_value != other_type_value { @@ -109,18 +122,56 @@ impl Ord for KeyType { (Negative(n1), Negative(n2)) => n1.cmp(n2).reverse(), (ByteString(b1), ByteString(b2)) => b1.len().cmp(&b2.len()).then(b1.cmp(b2)), (TextString(t1), TextString(t2)) => t1.len().cmp(&t2.len()).then(t1.cmp(t2)), - _ => unreachable!(), + (Array(a1), Array(a2)) if a1.len() != a2.len() => a1.len().cmp(&a2.len()), + (Array(a1), Array(a2)) => { + // Arrays of same length. + let mut ordering = Ordering::Equal; + for (e1, e2) in a1.iter().zip(a2.iter()) { + ordering = e1.cmp(e2); + if !matches!(ordering, Ordering::Equal) { + break; + } + } + ordering + } + (Map(m1), Map(m2)) if m1.len() != m2.len() => m1.len().cmp(&m2.len()), + (Map(m1), Map(m2)) => { + // Maps of same length. + let mut ordering = Ordering::Equal; + for ((k1, v1), (k2, v2)) in m1.iter().zip(m2.iter()) { + ordering = k1.cmp(k2).then_with(|| v1.cmp(v2)); + if !matches!(ordering, Ordering::Equal) { + break; + } + } + ordering + } + (Tag(t1, v1), Tag(t2, v2)) => t1.cmp(t2).then(v1.cmp(v2)), + (Simple(s1), Simple(s2)) => s1.cmp(s2), + (_, _) => { + // The case of different major types is caught above. + unreachable!(); + } } } } -impl PartialOrd for KeyType { - fn partial_cmp(&self, other: &KeyType) -> Option { +impl PartialOrd for Value { + fn partial_cmp(&self, other: &Value) -> Option { Some(self.cmp(other)) } } +impl Eq for Value {} + +impl PartialEq for Value { + fn eq(&self, other: &Value) -> bool { + self.cmp(other) == Ordering::Equal + } +} + impl SimpleValue { + /// Create a simple value from its encoded value. pub fn from_integer(int: u64) -> Option { match int { 20 => Some(SimpleValue::FalseValue), @@ -132,54 +183,57 @@ impl SimpleValue { } } -impl From for KeyType { +impl From for Value { fn from(unsigned: u64) -> Self { - KeyType::Unsigned(unsigned) + Value::Unsigned(unsigned) } } -impl From for KeyType { +impl From for Value { fn from(i: i64) -> Self { - KeyType::integer(i) + Value::integer(i) } } -impl From for KeyType { +impl From for Value { fn from(i: i32) -> Self { - KeyType::integer(i as i64) + Value::integer(i as i64) } } -impl From> for KeyType { +impl From> for Value { fn from(bytes: Vec) -> Self { - KeyType::ByteString(bytes) + Value::ByteString(bytes) } } -impl From<&[u8]> for KeyType { +impl From<&[u8]> for Value { fn from(bytes: &[u8]) -> Self { - KeyType::ByteString(bytes.to_vec()) + Value::ByteString(bytes.to_vec()) } } -impl From for KeyType { +impl From for Value { fn from(text: String) -> Self { - KeyType::TextString(text) + Value::TextString(text) } } -impl From<&str> for KeyType { +impl From<&str> for Value { fn from(text: &str) -> Self { - KeyType::TextString(text.to_string()) + Value::TextString(text.to_string()) } } -impl From for Value -where - KeyType: From, -{ - fn from(t: T) -> Self { - Value::KeyValue(KeyType::from(t)) +impl From> for Value { + fn from(array: Vec) -> Self { + Value::Array(array) + } +} + +impl From> for Value { + fn from(map: Vec<(Value, Value)>) -> Self { + Value::Map(map) } } @@ -189,20 +243,9 @@ impl From for Value { } } -pub trait IntoCborKey { - fn into_cbor_key(self) -> KeyType; -} - -impl IntoCborKey for T -where - KeyType: From, -{ - fn into_cbor_key(self) -> KeyType { - KeyType::from(self) - } -} - +/// Trait that indicates that a type can be converted to a CBOR [`Value`]. pub trait IntoCborValue { + /// Convert `self` into a CBOR [`Value`], consuming it along the way. fn into_cbor_value(self) -> Value; } @@ -215,7 +258,9 @@ where } } +/// Trait that indicates that a type can be converted to a CBOR [`Option`]. pub trait IntoCborValueOption { + /// Convert `self` into a CBOR [`Option`], consuming it along the way. fn into_cbor_value_option(self) -> Option; } @@ -239,32 +284,83 @@ where #[cfg(test)] mod test { - use crate::{cbor_key_bytes, cbor_key_int, cbor_key_text}; + use super::*; + use crate::{cbor_array, cbor_bool, cbor_bytes, cbor_int, cbor_map, cbor_tagged, cbor_text}; + use std::vec; #[test] - fn test_key_type_ordering() { - assert!(cbor_key_int!(0) < cbor_key_int!(23)); - assert!(cbor_key_int!(23) < cbor_key_int!(24)); - assert!(cbor_key_int!(24) < cbor_key_int!(1000)); - assert!(cbor_key_int!(1000) < cbor_key_int!(1000000)); - assert!(cbor_key_int!(1000000) < cbor_key_int!(std::i64::MAX)); - assert!(cbor_key_int!(std::i64::MAX) < cbor_key_int!(-1)); - assert!(cbor_key_int!(-1) < cbor_key_int!(-23)); - assert!(cbor_key_int!(-23) < cbor_key_int!(-24)); - assert!(cbor_key_int!(-24) < cbor_key_int!(-1000)); - assert!(cbor_key_int!(-1000) < cbor_key_int!(-1000000)); - assert!(cbor_key_int!(-1000000) < cbor_key_int!(std::i64::MIN)); - assert!(cbor_key_int!(std::i64::MIN) < cbor_key_bytes!(vec![])); - assert!(cbor_key_bytes!(vec![]) < cbor_key_bytes!(vec![0x00])); - assert!(cbor_key_bytes!(vec![0x00]) < cbor_key_bytes!(vec![0x01])); - assert!(cbor_key_bytes!(vec![0x01]) < cbor_key_bytes!(vec![0xFF])); - assert!(cbor_key_bytes!(vec![0xFF]) < cbor_key_bytes!(vec![0x00, 0x00])); - assert!(cbor_key_bytes!(vec![0x00, 0x00]) < cbor_key_text!("")); - assert!(cbor_key_text!("") < cbor_key_text!("a")); - assert!(cbor_key_text!("a") < cbor_key_text!("b")); - assert!(cbor_key_text!("b") < cbor_key_text!("aa")); - assert!(cbor_key_int!(1) < cbor_key_bytes!(vec![0x00])); - assert!(cbor_key_int!(1) < cbor_key_text!("s")); - assert!(cbor_key_int!(-1) < cbor_key_text!("s")); + fn test_value_ordering() { + assert!(cbor_int!(0) < cbor_int!(23)); + assert!(cbor_int!(23) < cbor_int!(24)); + assert!(cbor_int!(24) < cbor_int!(1000)); + assert!(cbor_int!(1000) < cbor_int!(1000000)); + assert!(cbor_int!(1000000) < cbor_int!(core::i64::MAX)); + assert!(cbor_int!(core::i64::MAX) < cbor_int!(-1)); + assert!(cbor_int!(-1) < cbor_int!(-23)); + assert!(cbor_int!(-23) < cbor_int!(-24)); + assert!(cbor_int!(-24) < cbor_int!(-1000)); + assert!(cbor_int!(-1000) < cbor_int!(-1000000)); + assert!(cbor_int!(-1000000) < cbor_int!(core::i64::MIN)); + assert!(cbor_int!(core::i64::MIN) < cbor_bytes!(vec![])); + assert!(cbor_bytes!(vec![]) < cbor_bytes!(vec![0x00])); + assert!(cbor_bytes!(vec![0x00]) < cbor_bytes!(vec![0x01])); + assert!(cbor_bytes!(vec![0x01]) < cbor_bytes!(vec![0xFF])); + assert!(cbor_bytes!(vec![0xFF]) < cbor_bytes!(vec![0x00, 0x00])); + assert!(cbor_bytes!(vec![0x00, 0x00]) < cbor_text!("")); + assert!(cbor_text!("") < cbor_text!("a")); + assert!(cbor_text!("a") < cbor_text!("b")); + assert!(cbor_text!("b") < cbor_text!("aa")); + assert!(cbor_text!("aa") < cbor_array![]); + assert!(cbor_array![] < cbor_array![0]); + assert!(cbor_array![0] < cbor_array![-1]); + assert!(cbor_array![1] < cbor_array![b""]); + assert!(cbor_array![b""] < cbor_array![""]); + assert!(cbor_array![""] < cbor_array![cbor_array![]]); + assert!(cbor_array![cbor_array![]] < cbor_array![cbor_map! {}]); + assert!(cbor_array![cbor_map! {}] < cbor_array![false]); + assert!(cbor_array![false] < cbor_array![0, 0]); + assert!(cbor_array![0, 0] < cbor_map! {}); + assert!(cbor_map! {} < cbor_map! {0 => 0}); + assert!(cbor_map! {0 => 0} < cbor_map! {0 => 1}); + assert!(cbor_map! {0 => 1} < cbor_map! {1 => 0}); + assert!(cbor_map! {1 => 0} < cbor_map! {-1 => 0}); + assert!(cbor_map! {-1 => 0} < cbor_map! {b"" => 0}); + assert!(cbor_map! {b"" => 0} < cbor_map! {"" => 0}); + assert!(cbor_map! {"" => 0} < cbor_map! {cbor_array![] => 0}); + assert!(cbor_map! {cbor_array![] => 0} < cbor_map! {cbor_map!{} => 0}); + assert!(cbor_map! {cbor_map!{} => 0} < cbor_map! {false => 0}); + assert!(cbor_map! {false => 0} < cbor_map! {0 => 0, 0 => 0}); + assert!(cbor_map! {0 => 0} < cbor_tagged!(2, cbor_int!(0))); + assert!(cbor_map! {0 => 0, 0 => 0} < cbor_bool!(false)); + assert!(cbor_bool!(false) < cbor_bool!(true)); + assert!(cbor_bool!(true) < Value::Simple(SimpleValue::NullValue)); + assert!(Value::Simple(SimpleValue::NullValue) < Value::Simple(SimpleValue::Undefined)); + assert!(cbor_tagged!(1, cbor_text!("s")) < cbor_tagged!(2, cbor_int!(0))); + assert!(cbor_int!(1) < cbor_int!(-1)); + assert!(cbor_int!(1) < cbor_bytes!(vec![0x00])); + assert!(cbor_int!(1) < cbor_text!("s")); + assert!(cbor_int!(1) < cbor_array![]); + assert!(cbor_int!(1) < cbor_map! {}); + assert!(cbor_int!(1) < cbor_tagged!(1, cbor_text!("s"))); + assert!(cbor_int!(1) < cbor_bool!(false)); + assert!(cbor_int!(-1) < cbor_bytes!(vec![0x00])); + assert!(cbor_int!(-1) < cbor_text!("s")); + assert!(cbor_int!(-1) < cbor_array![]); + assert!(cbor_int!(-1) < cbor_map! {}); + assert!(cbor_int!(-1) < cbor_tagged!(1, cbor_text!("s"))); + assert!(cbor_int!(-1) < cbor_bool!(false)); + assert!(cbor_bytes!(vec![0x00]) < cbor_text!("s")); + assert!(cbor_bytes!(vec![0x00]) < cbor_array![]); + assert!(cbor_bytes!(vec![0x00]) < cbor_map! {}); + assert!(cbor_bytes!(vec![0x00]) < cbor_tagged!(1, cbor_text!("s"))); + assert!(cbor_bytes!(vec![0x00]) < cbor_bool!(false)); + assert!(cbor_text!("s") < cbor_array![]); + assert!(cbor_text!("s") < cbor_map! {}); + assert!(cbor_text!("s") < cbor_tagged!(1, cbor_text!("s"))); + assert!(cbor_text!("s") < cbor_bool!(false)); + assert!(cbor_array![] < cbor_map!(0 => 1)); + assert!(cbor_array![] < cbor_tagged!(2, cbor_int!(0))); + assert!(cbor_array![] < cbor_bool!(false)); + assert!(cbor_tagged!(1, cbor_text!("s")) < cbor_bool!(false)); } } diff --git a/apps/vault/libraries/cbor/src/writer.rs b/apps/vault/libraries/cbor/src/writer.rs index 3cde113d7..ba8bdd4c9 100644 --- a/apps/vault/libraries/cbor/src/writer.rs +++ b/apps/vault/libraries/cbor/src/writer.rs @@ -12,12 +12,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::values::{Constants, KeyType, Value}; +//! Functionality for serializing CBOR values into bytes. + +use super::values::{Constants, Value}; use std::vec::Vec; -pub fn write(value: Value, encoded_cbor: &mut Vec) -> bool { +/// Possible errors from a serialization operation. +#[derive(Debug, PartialEq)] +pub enum EncoderError { + TooMuchNesting, + DuplicateMapKey, +} + +/// Convert a [`Value`] to serialized CBOR data, consuming it along the way and appending to the provided vector. +/// Maximum level of nesting supported is 127; more deeply nested structures will fail with +/// [`EncoderError::TooMuchNesting`]. +pub fn write(value: Value, encoded_cbor: &mut Vec) -> Result<(), EncoderError> { + write_nested(value, encoded_cbor, Some(i8::MAX)) +} + +/// Convert a [`Value`] to serialized CBOR data, consuming it along the way and appending to the provided vector. If +/// `max_nest` is `Some(max)`, then nested structures are only supported up to the given limit (returning +/// [`DecoderError::TooMuchNesting`] if the limit is hit). +pub fn write_nested( + value: Value, + encoded_cbor: &mut Vec, + max_nest: Option, +) -> Result<(), EncoderError> { let mut writer = Writer::new(encoded_cbor); - writer.encode_cbor(value, Writer::MAX_NESTING_DEPTH) + writer.encode_cbor(value, max_nest) } struct Writer<'a> { @@ -25,51 +48,56 @@ struct Writer<'a> { } impl<'a> Writer<'a> { - const MAX_NESTING_DEPTH: i8 = 4; - pub fn new(encoded_cbor: &mut Vec) -> Writer { Writer { encoded_cbor } } - fn encode_cbor(&mut self, value: Value, remaining_depth: i8) -> bool { - if remaining_depth < 0 { - return false; + fn encode_cbor( + &mut self, + value: Value, + remaining_depth: Option, + ) -> Result<(), EncoderError> { + if remaining_depth.map_or(false, |d| d < 0) { + return Err(EncoderError::TooMuchNesting); } + let type_label = value.type_label(); match value { - Value::KeyValue(KeyType::Unsigned(unsigned)) => self.start_item(0, unsigned), - Value::KeyValue(KeyType::Negative(negative)) => { - self.start_item(1, -(negative + 1) as u64) - } - Value::KeyValue(KeyType::ByteString(byte_string)) => { - self.start_item(2, byte_string.len() as u64); + Value::Unsigned(unsigned) => self.start_item(type_label, unsigned), + Value::Negative(negative) => self.start_item(type_label, -(negative + 1) as u64), + Value::ByteString(byte_string) => { + self.start_item(type_label, byte_string.len() as u64); self.encoded_cbor.extend(byte_string); } - Value::KeyValue(KeyType::TextString(text_string)) => { - self.start_item(3, text_string.len() as u64); + Value::TextString(text_string) => { + self.start_item(type_label, text_string.len() as u64); self.encoded_cbor.extend(text_string.into_bytes()); } Value::Array(array) => { - self.start_item(4, array.len() as u64); + self.start_item(type_label, array.len() as u64); for el in array { - if !self.encode_cbor(el, remaining_depth - 1) { - return false; - } + self.encode_cbor(el, remaining_depth.map(|d| d - 1))?; } } - Value::Map(map) => { - self.start_item(5, map.len() as u64); + Value::Map(mut map) => { + map.sort_by(|a, b| a.0.cmp(&b.0)); + let map_len = map.len(); + map.dedup_by(|a, b| a.0.eq(&b.0)); + if map_len != map.len() { + return Err(EncoderError::DuplicateMapKey); + } + self.start_item(type_label, map_len as u64); for (k, v) in map { - if !self.encode_cbor(Value::KeyValue(k), remaining_depth - 1) { - return false; - } - if !self.encode_cbor(v, remaining_depth - 1) { - return false; - } + self.encode_cbor(k, remaining_depth.map(|d| d - 1))?; + self.encode_cbor(v, remaining_depth.map(|d| d - 1))?; } } - Value::Simple(simple_value) => self.start_item(7, simple_value as u64), + Value::Tag(tag, inner_value) => { + self.start_item(type_label, tag); + self.encode_cbor(*inner_value, remaining_depth.map(|d| d - 1))?; + } + Value::Simple(simple_value) => self.start_item(type_label, simple_value as u64), } - true + Ok(()) } fn start_item(&mut self, type_label: u8, size: u64) { @@ -94,12 +122,13 @@ mod test { use super::*; use crate::{ cbor_array, cbor_array_vec, cbor_bytes, cbor_false, cbor_int, cbor_map, cbor_null, - cbor_text, cbor_true, cbor_undefined, + cbor_tagged, cbor_text, cbor_true, cbor_undefined, }; + use std::vec; fn write_return(value: Value) -> Option> { let mut encoded_cbor = Vec::new(); - if write(value, &mut encoded_cbor) { + if write(value, &mut encoded_cbor).is_ok() { Some(encoded_cbor) } else { None @@ -124,7 +153,7 @@ mod test { vec![0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MAX, + core::i64::MAX, vec![0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -149,7 +178,7 @@ mod test { vec![0x3B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00], ), ( - std::i64::MIN, + core::i64::MIN, vec![0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], ), ]; @@ -182,7 +211,7 @@ mod test { ("\"\\", vec![0x62, 0x22, 0x5C]), ("ü", vec![0x62, 0xC3, 0xBC]), ( - std::str::from_utf8(&unicode_3byte).unwrap(), + core::str::from_utf8(&unicode_3byte).unwrap(), vec![0x63, 0xE6, 0xB0, 0xB4], ), ("𐅑", vec![0x64, 0xF0, 0x90, 0x85, 0x91]), @@ -209,9 +238,16 @@ mod test { #[test] fn test_write_map() { let value_map = cbor_map! { - "aa" => "AA", - "e" => "E", - "" => ".", + 0 => "a", + 23 => "b", + 24 => "c", + core::u8::MAX as i64 => "d", + 256 => "e", + core::u16::MAX as i64 => "f", + 65536 => "g", + core::u32::MAX as i64 => "h", + 4294967296_i64 => "i", + core::i64::MAX => "j", -1 => "k", -24 => "l", -25 => "m", @@ -220,20 +256,13 @@ mod test { -65537 => "p", -4294967296_i64 => "q", -4294967297_i64 => "r", - std::i64::MIN => "s", + core::i64::MIN => "s", b"a" => 2, b"bar" => 3, b"foo" => 4, - 0 => "a", - 23 => "b", - 24 => "c", - std::u8::MAX as i64 => "d", - 256 => "e", - std::u16::MAX as i64 => "f", - 65536 => "g", - std::u32::MAX as i64 => "h", - 4294967296_i64 => "i", - std::i64::MAX => "j", + "" => ".", + "e" => "E", + "aa" => "AA", }; let expected_cbor = vec![ 0xb8, 0x19, // map of 25 pairs: @@ -288,6 +317,67 @@ mod test { assert_eq!(write_return(value_map), Some(expected_cbor)); } + #[test] + fn test_write_map_sorted() { + let sorted_map = cbor_map! { + 0 => "a", + 1 => "b", + -1 => "c", + -2 => "d", + b"a" => "e", + b"b" => "f", + "" => "g", + "c" => "h", + }; + let unsorted_map = cbor_map! { + 1 => "b", + -2 => "d", + b"b" => "f", + "c" => "h", + "" => "g", + b"a" => "e", + -1 => "c", + 0 => "a", + }; + assert_eq!(write_return(sorted_map), write_return(unsorted_map)); + } + + #[test] + fn test_write_map_duplicates() { + let duplicate0 = cbor_map! { + 0 => "a", + -1 => "c", + b"a" => "e", + "c" => "g", + 0 => "b", + }; + assert_eq!(write_return(duplicate0), None); + let duplicate1 = cbor_map! { + 0 => "a", + -1 => "c", + b"a" => "e", + "c" => "g", + -1 => "d", + }; + assert_eq!(write_return(duplicate1), None); + let duplicate2 = cbor_map! { + 0 => "a", + -1 => "c", + b"a" => "e", + "c" => "g", + b"a" => "f", + }; + assert_eq!(write_return(duplicate2), None); + let duplicate3 = cbor_map! { + 0 => "a", + -1 => "c", + b"a" => "e", + "c" => "g", + "c" => "h", + }; + assert_eq!(write_return(duplicate3), None); + } + #[test] fn test_write_map_with_array() { let value_map = cbor_map! { @@ -325,6 +415,33 @@ mod test { assert_eq!(write_return(value_map), Some(expected_cbor)); } + #[test] + fn test_write_tagged() { + let cases = vec![ + (cbor_tagged!(6, cbor_int!(0x42)), vec![0xc6, 0x18, 0x42]), + (cbor_tagged!(1, cbor_true!()), vec![0xc1, 0xf5]), + ( + cbor_tagged!( + 1000, + cbor_map! { + "a" => 1, + "b" => cbor_array![2, 3], + } + ), + vec![ + 0xd9, 0x03, 0xe8, 0xa2, // map of 2 pairs + 0x61, 0x61, // "a" + 0x01, 0x61, 0x62, // "b" + 0x82, // array with 2 elements + 0x02, 0x03, + ], + ), + ]; + for (value, correct_cbor) in cases { + assert_eq!(write_return(value), Some(correct_cbor)); + } + } + #[test] fn test_write_simple() { let cases = vec![ @@ -355,12 +472,12 @@ mod test { for (value, level) in positive_cases { let mut buf = Vec::new(); let mut writer = Writer::new(&mut buf); - assert!(writer.encode_cbor(value, level)); + assert!(writer.encode_cbor(value, Some(level)).is_ok()); } for (value, level) in negative_cases { let mut buf = Vec::new(); let mut writer = Writer::new(&mut buf); - assert!(!writer.encode_cbor(value, level)); + assert!(!writer.encode_cbor(value, Some(level)).is_ok()); } } @@ -376,9 +493,10 @@ mod test { let mut buf = Vec::new(); let mut writer = Writer::new(&mut buf); - assert!(writer.encode_cbor(cbor_map.clone(), 2)); + assert!(writer.encode_cbor(cbor_map.clone(), Some(2)).is_ok()); + assert!(writer.encode_cbor(cbor_map.clone(), None).is_ok()); writer = Writer::new(&mut buf); - assert!(!writer.encode_cbor(cbor_map, 1)); + assert!(writer.encode_cbor(cbor_map, Some(1)).is_err()); } #[test] @@ -398,9 +516,9 @@ mod test { let mut buf = Vec::new(); let mut writer = Writer::new(&mut buf); - assert!(writer.encode_cbor(cbor_array.clone(), 3)); + assert!(writer.encode_cbor(cbor_array.clone(), Some(3)).is_ok()); writer = Writer::new(&mut buf); - assert!(!writer.encode_cbor(cbor_array, 2)); + assert!(writer.encode_cbor(cbor_array, Some(2)).is_err()); } #[test] @@ -426,8 +544,9 @@ mod test { let mut buf = Vec::new(); let mut writer = Writer::new(&mut buf); - assert!(writer.encode_cbor(cbor_map.clone(), 5)); + assert!(writer.encode_cbor(cbor_map.clone(), Some(5)).is_ok()); + assert!(writer.encode_cbor(cbor_map.clone(), None).is_ok()); writer = Writer::new(&mut buf); - assert!(!writer.encode_cbor(cbor_map, 4)); + assert!(writer.encode_cbor(cbor_map, Some(4)).is_err()); } } diff --git a/apps/vault/libraries/crypto/src/aes256.rs b/apps/vault/libraries/crypto/src/aes256.rs new file mode 100644 index 000000000..85e8a6dfb --- /dev/null +++ b/apps/vault/libraries/crypto/src/aes256.rs @@ -0,0 +1,309 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::util::Block16; +use aes::Aes256; +use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray}; + +/** This structure caches the round keys, to avoid re-computing the key schedule for each block. **/ +pub struct EncryptionKey { + enc_cipher: Aes256, + // have to keep a copy of this around because the API wants to create a DecryptionKey from this, + // but the Aes256 key doesn't support a clone option. + key: [u8; 32], +} + +pub struct DecryptionKey { + dec_cipher: Aes256, +} + +impl EncryptionKey { + // Computes the round keys. + pub fn new(key: &[u8; 32]) -> EncryptionKey { + let mut local_key = [0u8; 32]; + local_key.copy_from_slice(key); + let enc_cipher = Aes256::new(GenericArray::from_slice(key)); + EncryptionKey { + enc_cipher, + key: local_key, + } + } + + // Encrypt an AES block in place. + pub fn encrypt_block(&self, block: &mut Block16) { + self.enc_cipher.encrypt_block(GenericArray::from_mut_slice(block)); + } +} + +impl DecryptionKey { + // Computes the round keys. + pub fn new(key: &EncryptionKey) -> DecryptionKey { + let dec_cipher = Aes256::new(GenericArray::from_slice(&key.key)); + + DecryptionKey { dec_cipher } + } + + // Decrypt an AES block in place. + pub fn decrypt_block(&self, block: &mut Block16) { + self.dec_cipher.decrypt_block(GenericArray::from_mut_slice(block)); + } +} + +#[cfg(test)] +mod test { + use super::*; + + // Test vector from the NIST obtained at: + // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_ECB.pdf + #[test] + fn test_nist_aes256_ecb_encrypt() { + let src = b"\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\ + \xe9\x3d\x7e\x11\x73\x93\x17\x2a"; + let key = b"\x60\x3d\xeb\x10\x15\xca\x71\xbe\ + \x2b\x73\xae\xf0\x85\x7d\x77\x81\ + \x1f\x35\x2c\x07\x3b\x61\x08\xd7\ + \x2d\x98\x10\xa3\x09\x14\xdf\xf4"; + let expected = b"\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\ + \x06\x4b\x5a\x7e\x3d\xb1\x81\xf8"; + + let mut dst: Block16 = Default::default(); + dst.copy_from_slice(src); + EncryptionKey::new(key).encrypt_block(&mut dst); + assert_eq!(&dst, expected); + } + + #[test] + fn test_nist_aes256_ecb_decrypt() { + let src = b"\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\ + \x06\x4b\x5a\x7e\x3d\xb1\x81\xf8"; + let key = b"\x60\x3d\xeb\x10\x15\xca\x71\xbe\ + \x2b\x73\xae\xf0\x85\x7d\x77\x81\ + \x1f\x35\x2c\x07\x3b\x61\x08\xd7\ + \x2d\x98\x10\xa3\x09\x14\xdf\xf4"; + let expected = b"\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\ + \xe9\x3d\x7e\x11\x73\x93\x17\x2a"; + + let mut dst: Block16 = Default::default(); + dst.copy_from_slice(src); + DecryptionKey::new(&EncryptionKey::new(key)).decrypt_block(&mut dst); + assert_eq!(&dst, expected); + } + + #[test] + fn test_encrypt_decrypt() { + // Test that decrypt_block is the inverse of encrypt_block for a bunch of block values. + let key_bytes = b"\x60\x3d\xeb\x10\x15\xca\x71\xbe\ + \x2b\x73\xae\xf0\x85\x7d\x77\x81\ + \x1f\x35\x2c\x07\x3b\x61\x08\xd7\ + \x2d\x98\x10\xa3\x09\x14\xdf\xf4"; + let enc_key = EncryptionKey::new(key_bytes); + let dec_key = DecryptionKey::new(&enc_key); + let mut block: Block16 = [0; 16]; + for i in 0..=255 { + for j in 0..16 { + block[j] = (i + j) as u8; + } + let expected = block; + enc_key.encrypt_block(&mut block); + dec_key.decrypt_block(&mut block); + assert_eq!(block, expected); + } + } + + #[test] + fn test_sbox_is_permutation() { + let mut image = [false; 256]; + for &sboxed in SBOX.iter() { + assert_eq!(image[sboxed as usize], false); + image[sboxed as usize] = true; + } + } + + #[test] + fn test_sbox_inv_is_permutation() { + let mut image = [false; 256]; + for &sboxed in SBOX_INV.iter() { + assert_eq!(image[sboxed as usize], false); + image[sboxed as usize] = true; + } + } + + #[test] + fn test_sbox_inverse() { + for i in 0..=255 { + assert_eq!(SBOX_INV[SBOX[i as usize] as usize], i); + } + } + + #[test] + fn test_subbytes() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let expected = [ + 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, + ]; + sub_bytes(&mut block); + assert_eq!(block, expected); + } + + #[test] + fn test_subbytes_inv() { + // Test that inv_sub_bytes is the inverse of sub_bytes for a bunch of block values. + let mut block: Block16 = [0; 16]; + for i in 0..=255 { + for j in 0..16 { + block[j] = (i + j) as u8; + } + let expected = block; + sub_bytes(&mut block); + inv_sub_bytes(&mut block); + assert_eq!(block, expected); + } + } + + #[test] + fn test_shift_rows() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let expected = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]; + shift_rows(&mut block); + assert_eq!(block, expected); + } + + #[test] + fn test_shift_rows_inv() { + // Test that inv_shift_rows is the inverse of shift_rows for a bunch of block values. + let mut block: Block16 = [0; 16]; + for i in 0..=255 { + for j in 0..16 { + block[j] = (i + j) as u8; + } + let expected = block; + shift_rows(&mut block); + inv_shift_rows(&mut block); + assert_eq!(block, expected); + } + } + + #[test] + fn test_mix_columns_inv() { + // Test that inv_mix_columns is the inverse of mix_columns for a bunch of block values. + let mut block: Block16 = [0; 16]; + for i in 0..=255 { + for j in 0..16 { + block[j] = (i + j) as u8; + } + let expected = block; + mix_columns(&mut block); + inv_mix_columns(&mut block); + assert_eq!(block, expected); + } + } + + /** Comparison with AES-NI instructions for CPUs that support them **/ + #[cfg(all(target_arch = "x86_64", target_feature = "aes"))] + mod aesni { + use super::super::*; + + fn aes_enc_ni(block: &mut Block16, rkey: &Block16) { + use core::arch::x86_64::{__m128i, _mm_aesenc_si128}; + + unsafe { + let block_mm: __m128i = core::mem::transmute(*block); + let rkey_mm: __m128i = core::mem::transmute(*rkey); + let encrypted_mm: __m128i = _mm_aesenc_si128(block_mm, rkey_mm); + *block = core::mem::transmute(encrypted_mm) + } + } + + fn aes_enc_last_ni(block: &mut Block16, rkey: &Block16) { + use core::arch::x86_64::{__m128i, _mm_aesenclast_si128}; + + unsafe { + let block_mm: __m128i = core::mem::transmute(*block); + let rkey_mm: __m128i = core::mem::transmute(*rkey); + let encrypted_mm: __m128i = _mm_aesenclast_si128(block_mm, rkey_mm); + *block = core::mem::transmute(encrypted_mm) + } + } + + fn aes_dec_ni(block: &mut Block16, rkey: &Block16) { + use core::arch::x86_64::{__m128i, _mm_aesdec_si128}; + + unsafe { + let block_mm: __m128i = core::mem::transmute(*block); + let rkey_mm: __m128i = core::mem::transmute(*rkey); + let decrypted_mm: __m128i = _mm_aesdec_si128(block_mm, rkey_mm); + *block = core::mem::transmute(decrypted_mm) + } + } + + fn aes_dec_last_ni(block: &mut Block16, rkey: &Block16) { + use core::arch::x86_64::{__m128i, _mm_aesdeclast_si128}; + + unsafe { + let block_mm: __m128i = core::mem::transmute(*block); + let rkey_mm: __m128i = core::mem::transmute(*rkey); + let decrypted_mm: __m128i = _mm_aesdeclast_si128(block_mm, rkey_mm); + *block = core::mem::transmute(decrypted_mm) + } + } + + #[test] + fn test_aes_enc_ni() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let mut block_ni = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let rkey = [ + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ]; + aes_enc(&mut block, &rkey); + aes_enc_ni(&mut block_ni, &rkey); + assert_eq!(block, block_ni); + } + + #[test] + fn test_aes_enc_last_ni() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let mut block_ni = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let rkey = [ + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ]; + aes_enc_last(&mut block, &rkey); + aes_enc_last_ni(&mut block_ni, &rkey); + assert_eq!(block, block_ni); + } + + #[test] + fn test_aes_dec_ni() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let mut block_ni = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let rkey = [ + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ]; + aes_dec(&mut block, &rkey); + aes_dec_ni(&mut block_ni, &rkey); + assert_eq!(block, block_ni); + } + + #[test] + fn test_aes_dec_last_ni() { + let mut block = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let mut block_ni = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + let rkey = [ + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ]; + aes_dec_last(&mut block, &rkey); + aes_dec_last_ni(&mut block_ni, &rkey); + assert_eq!(block, block_ni); + } + } +} diff --git a/apps/vault/libraries/crypto/src/cbc.rs b/apps/vault/libraries/crypto/src/cbc.rs index ae30b123a..03ee39bf0 100644 --- a/apps/vault/libraries/crypto/src/cbc.rs +++ b/apps/vault/libraries/crypto/src/cbc.rs @@ -1,18 +1,3 @@ -// Xous maintainer's note: -// -// This library is vendored in from the Google OpenSK reference implementation. -// The OpenSK library contains its own implementations of crypto functions. -// The port to Xous attempts to undo that, but where possible leaves a thin -// adapter between the OpenSK custom APIs and the more "standard" Rustcrypto APIs. -// There is always a hazard in adapting crypto APIs and reviewers should take -// note of this. However, by calling out the API differences, it hopefully highlights -// any potential problems in the OpenSK library, rather than papering them over. -// -// Leaving the OpenSK APIs in place also makes it easier to apply upstream -// patches from OpenSK to fix bugs in the code base. - -// Original copyright notice preserved below: - // Copyright 2019 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,69 +12,58 @@ // See the License for the specific language governing permissions and // limitations under the License. -use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit, - generic_array::GenericArray, Key, Iv, consts::U16}; +use super::util::{xor_block_16, Block16}; +use crate::aes256::{DecryptionKey, EncryptionKey}; +use core::convert::TryInto; -type Aes256CbcEnc = cbc::Encryptor; -type Aes256CbcDec = cbc::Decryptor; - -use super::util::Block16; - -pub fn cbc_encrypt(key: &[u8; 32], iv: Block16, blocks: &mut [Block16]) -{ - // we get a mut slice of Block16 which is a [u8; 16], and we want a mut slice - // of GenericArray::. Unfortunately, I don't think there is any way - // to do this transformation except either something awful and unsafe, or, - // making a heap allocated copy into and out of the structures. Since the - // data handled by the authenticator is small, and we value correctness, - // we are going to go the inefficient-but-safe route. - let mut ga = vec![]; - for block in blocks.iter() { - ga.push(GenericArray::::clone_from_slice(block)); - } - Aes256CbcEnc::new(Key::::from_slice(key), Iv::::from_slice(&iv)) - .encrypt_blocks_mut(&mut ga); - for (src, dst) in ga.iter().zip(blocks.iter_mut()) { - dst.copy_from_slice(src.as_slice()); +/// Encrypts a byte slice. +/// +/// # Panics +/// +/// Panics if the byte slice is not a multiple of the block size (16 bytes). +pub fn cbc_encrypt(key: &EncryptionKey, mut iv: Block16, bytes: &mut [u8]) { + for block in bytes.chunks_mut(16) { + let block: &mut Block16 = block.try_into().unwrap(); + xor_block_16(block, &iv); + key.encrypt_block(block); + iv = *block; } } -pub fn cbc_decrypt(key: &[u8; 32], iv: Block16, blocks: &mut [Block16]) -{ - let mut ga = vec![]; - for block in blocks.iter() { - ga.push(GenericArray::::clone_from_slice(block)); - } - Aes256CbcDec::new(Key::::from_slice(key), Iv::::from_slice(&iv)) - .decrypt_blocks_mut(&mut ga); - for (src, dst) in ga.iter().zip(blocks.iter_mut()) { - dst.copy_from_slice(src.as_slice()); +/// Decrypts a byte slice. +/// +/// # Panics +/// +/// Panics if the byte slice is not a multiple of the block size (16 bytes). +pub fn cbc_decrypt(key: &DecryptionKey, mut iv: Block16, bytes: &mut [u8]) { + for block in bytes.chunks_mut(16) { + let block: &mut Block16 = block.try_into().unwrap(); + let tmp = *block; + key.decrypt_block(block); + xor_block_16(block, &iv); + iv = tmp; } } #[cfg(test)] mod test { + use super::super::aes256; use super::*; - use crate::util::xor_block_16; - use aes::Aes256Soft as Aes256; - use aes::cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray, BlockDecrypt}; #[test] fn test_cbc_encrypt_decrypt() { // Test that cbc_decrypt is the inverse of cbc_encrypt for a bunch of block values. - let enc_key = &[ + let enc_key = aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; - let dec_key = enc_key; + ]); + let dec_key = aes256::DecryptionKey::new(&enc_key); for len in 0..16 { - let mut blocks: Vec = vec![Default::default(); len]; - for i in 0..len { - for j in 0..16 { - blocks[i][j] = ((len + i) * 16 + j) as u8; - } + let mut blocks: Vec = vec![0; 16 * len]; + for (i, x) in blocks.iter_mut().enumerate() { + *x = (16 * len + i) as u8; } let iv = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, @@ -105,16 +79,16 @@ mod test { #[test] fn test_cbc_encrypt_1block_zero_iv() { - let key = &[ + let key = aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ]); - let mut blocks = [[ + let mut blocks = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - ]]; + ]; let iv = [0; 16]; cbc_encrypt(&key, iv, &mut blocks); @@ -122,25 +96,23 @@ mod test { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, ]; - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.encrypt_block(GenericArray::from_mut_slice(&mut expected)); - // key.encrypt_block(&mut expected); + key.encrypt_block(&mut expected); - assert_eq!(blocks, [expected]); + assert_eq!(blocks, expected); } #[test] fn test_cbc_decrypt_1block_zero_iv() { - let key = &[ + let key = aes256::DecryptionKey::new(&aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ])); - let mut blocks = [[ + let mut blocks = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - ]]; + ]; let iv = [0; 16]; cbc_decrypt(&key, iv, &mut blocks); @@ -148,25 +120,23 @@ mod test { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, ]; - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.decrypt_block(GenericArray::from_mut_slice(&mut expected)); - // key.decrypt_block(&mut expected); + key.decrypt_block(&mut expected); - assert_eq!(blocks, [expected]); + assert_eq!(blocks, expected); } #[test] fn test_cbc_encrypt_1block() { - let key = &[ + let key = aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ]); - let mut blocks = [[ + let mut blocks = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - ]]; + ]; let iv = [ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, @@ -178,25 +148,23 @@ mod test { 0x2e, 0x2f, ]; xor_block_16(&mut expected, &iv); - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.encrypt_block(GenericArray::from_mut_slice(&mut expected)); - // key.encrypt_block(&mut expected); + key.encrypt_block(&mut expected); - assert_eq!(blocks, [expected]); + assert_eq!(blocks, expected); } #[test] fn test_cbc_decrypt_1block() { - let key = &[ + let key = aes256::DecryptionKey::new(&aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ])); - let mut blocks = [[ + let mut blocks = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - ]]; + ]; let iv = [ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, @@ -207,31 +175,24 @@ mod test { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, ]; - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.decrypt_block(GenericArray::from_mut_slice(&mut expected)); - // key.decrypt_block(&mut expected); + key.decrypt_block(&mut expected); xor_block_16(&mut expected, &iv); - assert_eq!(blocks, [expected]); + assert_eq!(blocks, expected); } #[test] fn test_cbc_encrypt_2blocks() { - let key = &[ + let key = aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ]); let mut blocks = [ - [ - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, - 0x2e, 0x2f, - ], - [ - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, - 0x4e, 0x4f, - ], + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, + 0x2e, 0x2f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, + 0x4c, 0x4d, 0x4e, 0x4f, ]; let iv = [ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, @@ -239,43 +200,34 @@ mod test { ]; cbc_encrypt(&key, iv, &mut blocks); - let mut expected0 = [ + let mut expected = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, - 0x2e, 0x2f, - ]; - let mut expected1 = [ - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, - 0x4e, 0x4f, + 0x2e, 0x2f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, + 0x4c, 0x4d, 0x4e, 0x4f, ]; + let (expected0, expected1) = expected.split_at_mut(16); + let mut expected0: &mut Block16 = expected0.try_into().unwrap(); + let mut expected1: &mut Block16 = expected1.try_into().unwrap(); xor_block_16(&mut expected0, &iv); - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.encrypt_block(GenericArray::from_mut_slice(&mut expected0)); - // key.encrypt_block(&mut expected0); + key.encrypt_block(&mut expected0); xor_block_16(&mut expected1, &expected0); - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.encrypt_block(GenericArray::from_mut_slice(&mut expected1)); - // key.encrypt_block(&mut expected1); + key.encrypt_block(&mut expected1); - assert_eq!(blocks, [expected0, expected1]); + assert_eq!(blocks, expected); } #[test] fn test_cbc_decrypt_2blocks() { - let key = &[ + let key = aes256::DecryptionKey::new(&aes256::EncryptionKey::new(&[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - ]; + ])); let mut blocks = [ - [ - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, - 0x2e, 0x2f, - ], - [ - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, - 0x4e, 0x4f, - ], + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, + 0x2e, 0x2f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, + 0x4c, 0x4d, 0x4e, 0x4f, ]; let iv = [ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, @@ -283,23 +235,19 @@ mod test { ]; cbc_decrypt(&key, iv, &mut blocks); - let mut expected0 = [ + let mut expected = [ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, - 0x2e, 0x2f, + 0x2e, 0x2f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, + 0x4c, 0x4d, 0x4e, 0x4f, ]; - let mut expected1 = [ - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, - 0x4e, 0x4f, - ]; - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.decrypt_block(GenericArray::from_mut_slice(&mut expected1)); - // key.decrypt_block(&mut expected1); + let (expected0, expected1) = expected.split_at_mut(16); + let mut expected0: &mut Block16 = expected0.try_into().unwrap(); + let mut expected1: &mut Block16 = expected1.try_into().unwrap(); + key.decrypt_block(&mut expected1); xor_block_16(&mut expected1, &expected0); - let cipher = Aes256::new(GenericArray::from_slice(key)); - cipher.decrypt_block(GenericArray::from_mut_slice(&mut expected0)); - // key.decrypt_block(&mut expected0); + key.decrypt_block(&mut expected0); xor_block_16(&mut expected0, &iv); - assert_eq!(blocks, [expected0, expected1]); + assert_eq!(blocks, expected); } } diff --git a/apps/vault/libraries/crypto/src/ecdh.rs b/apps/vault/libraries/crypto/src/ecdh.rs index 261b84d94..49906a601 100644 --- a/apps/vault/libraries/crypto/src/ecdh.rs +++ b/apps/vault/libraries/crypto/src/ecdh.rs @@ -107,6 +107,13 @@ impl SecKey { hasher.update(shared.as_bytes().as_slice().try_into().unwrap()); hasher.finalize() } + + pub fn exchange_x(&self, other: &PubKey) -> [u8; 32] { + let shared = self.a.diffie_hellman(&other.p); + let mut ret = [0u8; 32]; + ret.copy_from_slice(shared.as_bytes().as_slice()); + ret + } } #[cfg(test)] diff --git a/apps/vault/libraries/crypto/src/ecdsa.rs b/apps/vault/libraries/crypto/src/ecdsa.rs index dd908819d..8ea8ab79d 100644 --- a/apps/vault/libraries/crypto/src/ecdsa.rs +++ b/apps/vault/libraries/crypto/src/ecdsa.rs @@ -18,12 +18,11 @@ use p256::{ use p256::ecdsa::VerifyingKey; use rand_core::OsRng; -use cbor::{cbor_bytes, cbor_map_options}; - use super::rng256::Rng256; use super::Hash256; const INT256_NBYTES: usize = 32; +pub const NBYTES: usize = INT256_NBYTES; #[derive(Clone, PartialEq)] #[cfg_attr(feature = "derive_debug", derive(Debug))] @@ -32,17 +31,6 @@ pub struct SecKey { // k: NonZeroExponentP256, } -pub struct Signature { - sig: P256Signature, - // r: NonZeroExponentP256, - // s: NonZeroExponentP256, -} - -pub struct PubKey { - p: VerifyingKey, - // p: PointP256, -} - impl SecKey { // we bypass the OpenSK "rng" specifier, because we have a standard RNG API in our system and do // not need to rely on its hack to pass an RNG around. @@ -114,6 +102,11 @@ impl SecKey { } } +pub struct Signature { + sig: P256Signature, + // r: NonZeroExponentP256, + // s: NonZeroExponentP256, +} impl Signature { pub fn to_asn1_der(&self) -> Vec { // let's hope this shakes out in testing. @@ -196,6 +189,10 @@ impl Signature { } } +pub struct PubKey { + p: VerifyingKey, + // p: PointP256, +} impl PubKey { pub const ES256_ALGORITHM: i64 = -7; #[cfg(feature = "with_ctap1")] @@ -208,6 +205,17 @@ impl PubKey { _ => None } } + + /// Creates a new PubKey from its coordinates on the elliptic curve. + pub fn from_coordinates(x: &[u8; INT256_NBYTES], y: &[u8; INT256_NBYTES]) -> Option { + let encoded_point: p256::EncodedPoint = + p256::EncodedPoint::from_affine_coordinates(x.into(), y.into(), false); + match VerifyingKey::from_encoded_point(&encoded_point) { + Ok(p) => Some(PubKey{p}), + _ => None + } + } + //#[cfg(test)] - commented out because for some reason we can't get the test configs to work right #[allow(dead_code)] fn to_bytes_uncompressed(&self, bytes: &mut [u8; 65]) { @@ -246,39 +254,20 @@ impl PubKey { representation } - // Encodes the key according to CBOR Object Signing and Encryption, defined in RFC 8152. - pub fn to_cose_key(&self) -> Option> { - const EC2_KEY_TYPE: i64 = 2; - const P_256_CURVE: i64 = 1; - let mut x_bytes = vec![0; INT256_NBYTES]; - x_bytes.copy_from_slice( + /// Writes the coordinates into the passed in arrays. + pub fn to_coordinates(&self, x: &mut [u8; NBYTES], y: &mut [u8; NBYTES]) { + x.copy_from_slice( self.p .to_encoded_point(false) .x().unwrap() .as_slice() ); - let x_byte_cbor: cbor::Value = cbor_bytes!(x_bytes); - let mut y_bytes = vec![0; INT256_NBYTES]; - y_bytes.copy_from_slice( + y.copy_from_slice( self.p .to_encoded_point(false) .y().unwrap() .as_slice() ); - let y_byte_cbor: cbor::Value = cbor_bytes!(y_bytes); - let cbor_value = cbor_map_options! { - 1 => EC2_KEY_TYPE, - 3 => PubKey::ES256_ALGORITHM, - -1 => P_256_CURVE, - -2 => x_byte_cbor, - -3 => y_byte_cbor, - }; - let mut encoded_key = Vec::new(); - if cbor::write(cbor_value, &mut encoded_key) { - Some(encoded_key) - } else { - None - } } // #[cfg(feature = "std")] diff --git a/apps/vault/libraries/crypto/src/hkdf.rs b/apps/vault/libraries/crypto/src/hkdf.rs new file mode 100644 index 000000000..baaa52396 --- /dev/null +++ b/apps/vault/libraries/crypto/src/hkdf.rs @@ -0,0 +1,80 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::hmac::hmac_256; +use super::Hash256; + +const HASH_SIZE: usize = 32; + +/// Computes the HKDF with empty salt and 256 bit (one block) output. +/// +/// # Arguments +/// +/// * `ikm` - Input keying material +/// * `info` - Optional context and application specific information +/// +/// This implementation is equivalent to the below hkdf, with `salt` set to the +/// default block of zeros and the output length l as 32. +pub fn hkdf_empty_salt_256(ikm: &[u8], info: &[u8]) -> [u8; HASH_SIZE] +where + H: Hash256, +{ + // Salt is a zero block here. + let prk = hmac_256::(&[0; HASH_SIZE], ikm); + // l is implicitly the block size, so we iterate exactly once. + let mut t = info.to_vec(); + t.push(1); + hmac_256::(&prk, t.as_slice()) +} + +#[cfg(test)] +mod test { + use super::super::sha256::Sha256; + use super::*; + use arrayref::array_ref; + + #[test] + fn test_hkdf_empty_salt_256_sha256_vectors() { + // Test vectors generated by pycryptodome using: + // HKDF(b'0', 32, b'', SHA256, context=b'\x00').hex() + let test_okms = [ + hex::decode("f9be72116cb97f41828210289caafeabde1f3dfb9723bf43538ab18f3666783a") + .unwrap(), + hex::decode("f50f964f5b94d62fd1da9356ab8662b0a0f5b8e36e277178b69b6ffecf50cf44") + .unwrap(), + hex::decode("fc8772ceb5592d67442dcb4353cdd28519e82d6e55b4cf664b5685252c2d2998") + .unwrap(), + hex::decode("62831b924839a180f53be5461eeea1b89dc21779f50142b5a54df0f0cc86d61a") + .unwrap(), + hex::decode("6991f00a12946a4e3b8315cdcf0132c2ca508fd17b769f08d1454d92d33733e0") + .unwrap(), + hex::decode("0f9bb7dddd1ec61f91d8c4f5369b5870f9d44c4ceabccca1b83f06fec115e4e3") + .unwrap(), + hex::decode("235367e2ab6cca2aba1a666825458dba6b272a215a2537c05feebe4b80dab709") + .unwrap(), + hex::decode("96e8edad661da48d1a133b38c255d33e05555bc9aa442579dea1cd8d8b8d2aef") + .unwrap(), + ]; + for (i, okm) in test_okms.iter().enumerate() { + // String of number i. + let ikm = i.to_string(); + // Byte i. + let info = [i as u8]; + assert_eq!( + &hkdf_empty_salt_256::(&ikm.as_bytes(), &info[..]), + array_ref!(okm, 0, 32) + ); + } + } +} diff --git a/apps/vault/libraries/crypto/src/lib.rs b/apps/vault/libraries/crypto/src/lib.rs index 27efd9509..6ace8bf0f 100644 --- a/apps/vault/libraries/crypto/src/lib.rs +++ b/apps/vault/libraries/crypto/src/lib.rs @@ -16,9 +16,11 @@ pub mod rng256; pub mod cbc; mod util; pub mod hmac; +pub mod hkdf; pub mod sha256; pub mod ecdh; pub mod ecdsa; +pub mod aes256; // Trait for hash functions that returns a 256-bit hash. // The type must be Sized (size known at compile time) so that we can instanciate one on the stack diff --git a/apps/vault/libraries/persistent_store/Cargo.toml b/apps/vault/libraries/persistent_store/Cargo.toml index 5fe7c9f0b..8c09b4338 100644 --- a/apps/vault/libraries/persistent_store/Cargo.toml +++ b/apps/vault/libraries/persistent_store/Cargo.toml @@ -6,6 +6,11 @@ license = "Apache-2.0" edition = "2018" [dependencies] +"pddb" = {path="../../../../services/pddb"} +log = "0.4.14" + +[dev-dependencies] +tempfile = "3" [features] std = [] diff --git a/apps/vault/libraries/persistent_store/src/buffer.rs b/apps/vault/libraries/persistent_store/src/buffer.rs index 0d0b33300..bc06e06f3 100644 --- a/apps/vault/libraries/persistent_store/src/buffer.rs +++ b/apps/vault/libraries/persistent_store/src/buffer.rs @@ -12,8 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Flash storage for testing. +//! +//! [`BufferStorage`] implements the flash [`Storage`] interface but doesn't interface with an +//! actual flash storage. Instead it uses a buffer in memory to represent the storage state. + use crate::{Storage, StorageError, StorageIndex, StorageResult}; -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::boxed::Box; use std::vec; @@ -63,8 +68,8 @@ pub struct BufferOptions { /// /// When set, the following conditions would panic: /// - A bit is written from 0 to 1. - /// - A word is written more than `max_word_writes`. - /// - A page is erased more than `max_page_erases`. + /// - A word is written more than [`Self::max_word_writes`]. + /// - A page is erased more than [`Self::max_page_erases`]. pub strict_mode: bool, } @@ -110,15 +115,13 @@ impl BufferStorage { /// /// Before each subsequent mutable operation (write or erase), the delay is decremented if /// positive. If the delay is elapsed, the operation is saved and an error is returned. - /// Subsequent operations will panic until the interrupted operation is [corrupted] or the - /// interruption is [reset]. + /// Subsequent operations will panic until either of: + /// - The interrupted operation is [corrupted](BufferStorage::corrupt_operation). + /// - The interruption is [reset](BufferStorage::reset_interruption). /// /// # Panics /// /// Panics if an interruption is already armed. - /// - /// [corrupted]: struct.BufferStorage.html#method.corrupt_operation - /// [reset]: struct.BufferStorage.html#method.reset_interruption pub fn arm_interruption(&mut self, delay: usize) { self.interruption.arm(delay); } @@ -130,10 +133,8 @@ impl BufferStorage { /// # Panics /// /// Panics if any of the following conditions hold: - /// - An interruption was not [armed]. + /// - An interruption was not [armed](BufferStorage::arm_interruption). /// - An interruption was armed and it has triggered. - /// - /// [armed]: struct.BufferStorage.html#method.arm_interruption pub fn disarm_interruption(&mut self) -> usize { self.interruption.get().err().unwrap() } @@ -142,16 +143,14 @@ impl BufferStorage { /// /// # Panics /// - /// Panics if an interruption was not [armed]. - /// - /// [armed]: struct.BufferStorage.html#method.arm_interruption + /// Panics if an interruption was not [armed](BufferStorage::arm_interruption). pub fn reset_interruption(&mut self) { let _ = self.interruption.get(); } /// Corrupts an interrupted operation. /// - /// Applies the [corruption function] to the storage. Counters are updated accordingly: + /// Applies the corruption function to the storage. Counters are updated accordingly: /// - If a word is fully written, its counter is incremented regardless of whether other words /// of the same operation have been fully written. /// - If a page is fully erased, its counter is incremented (and its word counters are reset). @@ -159,13 +158,10 @@ impl BufferStorage { /// # Panics /// /// Panics if any of the following conditions hold: - /// - An interruption was not [armed]. + /// - An interruption was not [armed](BufferStorage::arm_interruption). /// - An interruption was armed but did not trigger. /// - The corruption function corrupts more bits than allowed. /// - The interrupted operation itself would have panicked. - /// - /// [armed]: struct.BufferStorage.html#method.arm_interruption - /// [corruption function]: type.BufferCorruptFunction.html pub fn corrupt_operation(&mut self, corrupt: BufferCorruptFunction) { let operation = self.interruption.get().unwrap(); let range = self.operation_range(&operation).unwrap(); @@ -217,7 +213,8 @@ impl BufferStorage { /// /// # Panics /// - /// Panics if the maximum number of erase cycles per page is reached. + /// Panics if the [maximum number of erase cycles per page](BufferOptions::max_page_erases) is + /// reached. fn incr_page_erases(&mut self, page: usize) { // Check that pages are not erased too many times. if self.options.strict_mode { @@ -243,7 +240,8 @@ impl BufferStorage { /// /// # Panics /// - /// Panics if the maximum number of writes per word is reached. + /// Panics if the [maximum number of writes per word](BufferOptions::max_word_writes) is + /// reached. fn incr_word_writes(&mut self, index: usize, value: &[u8], complete: &[u8]) { let word_size = self.word_size(); for i in 0..value.len() / word_size { @@ -303,8 +301,8 @@ impl Storage for BufferStorage { self.options.max_page_erases } - fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]> { - Ok(&self.storage[index.range(length, self)?]) + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult> { + Ok(Cow::Borrowed(&self.storage[index.range(length, self)?])) } fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()> { diff --git a/apps/vault/libraries/persistent_store/src/concat.rs b/apps/vault/libraries/persistent_store/src/concat.rs new file mode 100644 index 000000000..ef476eb30 --- /dev/null +++ b/apps/vault/libraries/persistent_store/src/concat.rs @@ -0,0 +1,242 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Support for concatenated entries. +//! +//! This module permits to store multiple indexed values under the same key by concatenation. Such +//! values must be at most 255 bytes and there can't be more than 255 such values under the same +//! key (they are indexed with a `u8`). +//! +//! The rationale for using those particular constraints is that we want the number of bits to store +//! the index and the number of bits to store the length to fit in an integer number of bytes +//! (because the values are an integer number of bytes). Using only one byte is too restrictive +//! (e.g. 8 values of at most 31 bytes or 16 values of at most 15 bytes). Using 2 bytes is plenty of +//! space, so using one byte for each field makes parsing simpler and faster. +//! +//! The format is thus `(index:u8 length:u8 payload:[u8; length])*`. The concatenation is not +//! particularly sorted. + +use crate::{Storage, Store, StoreError, StoreResult}; +use std::vec::Vec; +use core::cmp::Ordering; +use core::ops::Range; + +/// Reads a value from a concatenated entry. +pub fn read(store: &Store, key: usize, index: u8) -> StoreResult>> { + let values = match store.find(key)? { + None => return Ok(None), + Some(x) => x, + }; + Ok(find(&values, index)?.map(|range| values[range].to_vec())) +} + +/// Writes a value to a concatenated entry. +pub fn write( + store: &mut Store, + key: usize, + index: u8, + value: &[u8], +) -> StoreResult<()> { + if value.len() > 255 { + return Err(StoreError::InvalidArgument); + } + let mut values = store.find(key)?.unwrap_or(vec![]); + match find(&values, index)? { + None => { + values.push(index); + values.push(value.len() as u8); + values.extend_from_slice(value); + } + Some(mut range) => { + values[range.start - 1] = value.len() as u8; + match range.len().cmp(&value.len()) { + Ordering::Less => { + let diff = value.len() - range.len(); + values.resize(values.len() + diff, 0); + values[range.end..].rotate_right(diff); + range.end += diff; + } + Ordering::Equal => (), + Ordering::Greater => { + let diff = range.len() - value.len(); + range.end -= diff; + values[range.end..].rotate_left(diff); + values.truncate(values.len() - diff); + } + } + values[range].copy_from_slice(value); + } + } + store.insert(key, &values) +} + +/// Deletes the value from a concatenated entry. +pub fn delete(store: &mut Store, key: usize, index: u8) -> StoreResult<()> { + let mut values = match store.find(key)? { + None => return Ok(()), + Some(x) => x, + }; + let mut range = match find(&values, index)? { + None => return Ok(()), + Some(x) => x, + }; + range.start -= 2; + values[range.start..].rotate_left(range.len()); + values.truncate(values.len() - range.len()); + store.insert(key, &values) +} + +fn find(values: &[u8], index: u8) -> StoreResult>> { + let mut pos = 0; + while pos < values.len() { + if pos == values.len() - 1 { + return Err(StoreError::InvalidStorage); + } + let len = values[pos + 1] as usize; + if len > values.len() - 2 || pos > values.len() - 2 - len { + return Err(StoreError::InvalidStorage); + } + if index == values[pos] { + return Ok(Some(pos + 2..pos + 2 + len)); + } + pos += 2 + len; + } + Ok(None) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test::MINIMAL; + + #[test] + fn read_empty_entry() { + let store = MINIMAL.new_store(); + assert_eq!(read(&store, 0, 0), Ok(None)); + assert_eq!(read(&store, 0, 1), Ok(None)); + } + + #[test] + fn read_missing_value() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(read(&store, 0, 1), Ok(None)); + } + + #[test] + fn read_existing_value() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(read(&store, 0, 0), Ok(Some(b"foo".to_vec()))); + assert_eq!(read(&store, 0, 2), Ok(Some(b"hello".to_vec()))); + } + + #[test] + fn read_invalid_entry_too_long() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x08hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(read(&store, 0, 1), Err(StoreError::InvalidStorage)); + } + + #[test] + fn read_invalid_entry_too_short() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(read(&store, 0, 1), Err(StoreError::InvalidStorage)); + } + + #[test] + fn write_empty_entry() { + let mut store = MINIMAL.new_store(); + assert_eq!(write(&mut store, 0, 0, b"foo"), Ok(())); + assert_eq!(store.find(0), Ok(Some(b"\x00\x03foo".to_vec()))); + } + + #[test] + fn write_missing_value() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(write(&mut store, 0, 1, b"bar"), Ok(())); + assert_eq!(store.find(0), Ok(Some(b"\x00\x03foo\x01\x03bar".to_vec()))); + } + + #[test] + fn write_existing_value_same_size() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(write(&mut store, 0, 0, b"bar"), Ok(())); + assert_eq!( + store.find(0), + Ok(Some(b"\x00\x03bar\x02\x05hello".to_vec())) + ); + } + + #[test] + fn write_existing_value_longer() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(write(&mut store, 0, 0, b"barrage"), Ok(())); + assert_eq!( + store.find(0), + Ok(Some(b"\x00\x07barrage\x02\x05hello".to_vec())) + ); + } + + #[test] + fn write_existing_value_shorter() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x08football\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(write(&mut store, 0, 0, b"bar"), Ok(())); + assert_eq!( + store.find(0), + Ok(Some(b"\x00\x03bar\x02\x05hello".to_vec())) + ); + } + + #[test] + fn delete_empty_entry() { + let mut store = MINIMAL.new_store(); + assert_eq!(delete(&mut store, 0, 0), Ok(())); + assert_eq!(delete(&mut store, 0, 1), Ok(())); + } + + #[test] + fn delete_missing_value() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(delete(&mut store, 0, 1), Ok(())); + assert_eq!( + store.find(0), + Ok(Some(b"\x00\x03foo\x02\x05hello".to_vec())) + ); + } + + #[test] + fn delete_existing_value() { + let mut store = MINIMAL.new_store(); + let value = b"\x00\x03foo\x02\x05hello".to_vec(); + store.insert(0, &value).unwrap(); + assert_eq!(delete(&mut store, 0, 0), Ok(())); + assert_eq!(store.find(0), Ok(Some(b"\x02\x05hello".to_vec()))); + } +} diff --git a/apps/vault/libraries/persistent_store/src/driver.rs b/apps/vault/libraries/persistent_store/src/driver.rs index f17f5762f..143f5f73f 100644 --- a/apps/vault/libraries/persistent_store/src/driver.rs +++ b/apps/vault/libraries/persistent_store/src/driver.rs @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Store wrapper for testing. +//! +//! [`StoreDriver`] wraps a [`Store`] and compares its behavior with its associated [`StoreModel`]. + use crate::format::{Format, Position}; -#[cfg(test)] +#[cfg(feature = "std")] use crate::StoreUpdate; use crate::{ BufferCorruptFunction, BufferOptions, BufferStorage, Nat, Store, StoreError, StoreHandle, @@ -196,6 +200,14 @@ impl StoreDriver { } } + /// Provides read-only access to the model. + pub fn model(&self) -> &StoreModel { + match self { + StoreDriver::On(x) => x.model(), + StoreDriver::Off(x) => x.model(), + } + } + /// Extracts the power-on version of the driver. pub fn on(self) -> Option { match self { @@ -427,7 +439,7 @@ impl StoreDriverOn { } /// Applies an insertion to the store and model without interruption. - #[cfg(test)] + #[cfg(feature = "std")] pub fn insert(&mut self, key: usize, value: &[u8]) -> Result<(), StoreInvariant> { let value = value.to_vec(); let updates = vec![StoreUpdate::Insert { key, value }]; @@ -435,12 +447,18 @@ impl StoreDriverOn { } /// Applies a deletion to the store and model without interruption. - #[cfg(test)] + #[cfg(feature = "std")] pub fn remove(&mut self, key: usize) -> Result<(), StoreInvariant> { let updates = vec![StoreUpdate::Remove { key }]; self.apply(StoreOperation::Transaction { updates }) } + /// Applies a clear operation to the store and model without interruption. + #[cfg(feature = "std")] + pub fn clear(&mut self, min_key: usize) -> Result<(), StoreInvariant> { + self.apply(StoreOperation::Clear { min_key }) + } + /// Checks that the store and model are in sync. pub fn check(&self) -> Result<(), StoreInvariant> { self.recover_check(&[]) @@ -598,4 +616,12 @@ impl<'a> StoreInterruption<'a> { corrupt: Box::new(|_, _| {}), } } + + /// Builds an interruption without corruption. + pub fn pure(delay: usize) -> StoreInterruption<'a> { + StoreInterruption { + delay, + corrupt: Box::new(|_, _| {}), + } + } } diff --git a/apps/vault/libraries/persistent_store/src/file.rs b/apps/vault/libraries/persistent_store/src/file.rs new file mode 100644 index 000000000..8401e1a2f --- /dev/null +++ b/apps/vault/libraries/persistent_store/src/file.rs @@ -0,0 +1,193 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! File-backed persistent flash storage for virtual authenticator. +//! +//! [`FileStorage`] implements the flash [`Storage`] interface but doesn't interface with an +//! actual flash storage. Instead it uses a host-based file to persist the storage state. + +use crate::{Storage, StorageIndex, StorageResult}; +use std::borrow::Cow; +use core::cell::RefCell; +use std::fs::{File, OpenOptions}; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::Path; + +/// Simulates a flash storage using a host-based file. +/// +/// This is usable for emulating authenticator hardware on VM hypervisor's host OS. +pub struct FileStorage { + // Options of the storage. + options: FileOptions, + + /// File for persisting contents of the storage. + /// + /// Reading data from File requires mutable reference, as seeking and reading data + /// changes file's current position. + /// + /// All operations on backing file internally always first seek to needed position, + /// so it's safe to borrow mutable reference to backing file for the time of operation. + file: RefCell, +} + +/// Options for file-backed storage. +pub struct FileOptions { + /// Size of a word in bytes. + pub word_size: usize, + + /// Size of a page in bytes. + pub page_size: usize, + + /// Number of pages in storage. + pub num_pages: usize, +} + +impl FileStorage { + pub fn new(path: &Path, options: FileOptions) -> StorageResult { + let mut file_ref = RefCell::new( + OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(path)?, + ); + let file = file_ref.get_mut(); + let file_len = file.metadata()?.len(); + let store_len: u64 = (options.page_size * options.num_pages) as u64; + + if file_len == 0 { + file.seek(SeekFrom::Start(0))?; + let buf = vec![0xff; options.page_size]; + for _ in 0..options.num_pages { + file.write(&buf)?; + } + } else if file_len != store_len { + // FileStorage buffer should be of fixed size, opening previously saved file + // from storage of different size is not supported + panic!("Invalid file size {}, should be {}", file_len, store_len); + } + Ok(FileStorage { + options, + file: file_ref, + }) + } +} + +impl Storage for FileStorage { + fn word_size(&self) -> usize { + self.options.word_size + } + + fn page_size(&self) -> usize { + self.options.page_size + } + + fn num_pages(&self) -> usize { + self.options.num_pages + } + + fn max_word_writes(&self) -> usize { + // We can write an unlimited amount of times in a file, but the store arithmetic + // uses `Nat` so the value should fit in a `Nat`. + u32::MAX as usize + } + + fn max_page_erases(&self) -> usize { + // We can "erase" an unlimited amount of times in a file, but the store format + // encodes the number of erase cycles on 16 bits. + u16::MAX as usize + } + + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult> { + let mut file = self.file.borrow_mut(); + file.seek(SeekFrom::Start(index.range(length, self)?.start as u64))?; + let mut buf = vec![0u8; length]; + file.read_exact(&mut buf)?; + Ok(Cow::Owned(buf)) + } + + fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()> { + let mut file = self.file.borrow_mut(); + file.seek(SeekFrom::Start( + index.range(value.len(), self)?.start as u64, + ))?; + file.write_all(value)?; + Ok(()) + } + + fn erase_page(&mut self, page: usize) -> StorageResult<()> { + let mut file = self.file.borrow_mut(); + let index = StorageIndex { page, byte: 0 }; + file.seek(SeekFrom::Start( + index.range(self.page_size(), self)?.start as u64, + ))?; + file.write_all(&vec![0xff; self.page_size()][..])?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + use tempfile::TempDir; + + const BLANK_WORD: &[u8] = &[0xff, 0xff, 0xff, 0xff]; + const DATA_WORD: &[u8] = &[0xee, 0xdd, 0xbb, 0x77]; + + const FILE_NAME: &str = "opensk_storage.bin"; + + const OPTIONS: FileOptions = FileOptions { + word_size: 4, + page_size: 0x1000, + num_pages: 20, + }; + + fn make_tmp_dir() -> PathBuf { + let tmp_dir = TempDir::new().unwrap(); + tmp_dir.into_path() + } + + fn remove_tmp_dir(tmp_dir: PathBuf) { + std::fs::remove_dir_all(tmp_dir).unwrap(); + } + + fn temp_storage(tmp_dir: &PathBuf) -> FileStorage { + let mut tmp_file = tmp_dir.clone(); + tmp_file.push(FILE_NAME); + FileStorage::new(&tmp_file, OPTIONS).unwrap() + } + + #[test] + fn read_write_persist_ok() { + let index = StorageIndex { page: 0, byte: 0 }; + let next_index = StorageIndex { page: 0, byte: 4 }; + + let tmp_dir = make_tmp_dir(); + { + let mut file_storage = temp_storage(&tmp_dir); + assert_eq!(file_storage.read_slice(index, 4).unwrap(), BLANK_WORD); + file_storage.write_slice(index, DATA_WORD).unwrap(); + assert_eq!(file_storage.read_slice(index, 4).unwrap(), DATA_WORD); + assert_eq!(file_storage.read_slice(next_index, 4).unwrap(), BLANK_WORD); + } + // Reload and check the data from previously persisted storage + { + let file_storage = temp_storage(&tmp_dir); + assert_eq!(file_storage.read_slice(index, 4).unwrap(), DATA_WORD); + assert_eq!(file_storage.read_slice(next_index, 4).unwrap(), BLANK_WORD); + } + remove_tmp_dir(tmp_dir); + } +} diff --git a/apps/vault/libraries/persistent_store/src/format.rs b/apps/vault/libraries/persistent_store/src/format.rs index 1d8912603..44e5091a4 100644 --- a/apps/vault/libraries/persistent_store/src/format.rs +++ b/apps/vault/libraries/persistent_store/src/format.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Storage representation of a store. + #[macro_use] mod bitfield; @@ -20,18 +22,20 @@ use self::bitfield::Length; use self::bitfield::{count_zeros, num_bits, Bit, Checksum, ConstField, Field}; use crate::{usize_to_nat, Nat, Storage, StorageIndex, StoreError, StoreResult, StoreUpdate}; use std::vec::Vec; +use core::borrow::Borrow; use core::cmp::min; use core::convert::TryFrom; /// Internal representation of a word in flash. /// -/// Currently, the store only supports storages where a word is 32 bits. +/// Currently, the store only supports storages where a word is 32 bits, i.e. the [word +/// size](Storage::word_size) is 4 bytes. type WORD = u32; /// Abstract representation of a word in flash. /// -/// This type is kept abstract to avoid possible confusion with `Nat` if they happen to have the -/// same representation. This is because they have different semantics, `Nat` represents natural +/// This type is kept abstract to avoid possible confusion with [`Nat`] if they happen to have the +/// same representation. This is because they have different semantics, [`Nat`] represents natural /// numbers while `Word` represents sequences of bits (and thus has no arithmetic). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Word(WORD); @@ -46,7 +50,7 @@ impl Word { /// /// # Panics /// - /// Panics if `slice.len() != WORD_SIZE`. + /// Panics if `slice.len()` is not [`WORD_SIZE`] bytes. pub fn from_slice(slice: &[u8]) -> Word { Word(WORD::from_le_bytes(::try_from(slice).unwrap())) } @@ -59,47 +63,49 @@ impl Word { /// Size of a word in bytes. /// -/// Currently, the store only supports storages where a word is 4 bytes. +/// Currently, the store only supports storages where the [word size](Storage::word_size) is 4 +/// bytes. const WORD_SIZE: Nat = core::mem::size_of::() as Nat; /// Minimum number of words per page. /// -/// Currently, the store only supports storages where pages have at least 8 words. -const MIN_NUM_WORDS_PER_PAGE: Nat = 8; +/// Currently, the store only supports storages where pages have at least 8 [words](WORD_SIZE), i.e. +/// the [page size](Storage::page_size) is at least 32 bytes. +const MIN_PAGE_SIZE: Nat = 8; /// Maximum size of a page in bytes. /// -/// Currently, the store only supports storages where pages are between 8 and 1024 [words]. -/// -/// [words]: constant.WORD_SIZE.html +/// Currently, the store only supports storages where pages have at most 1024 [words](WORD_SIZE), +/// i.e. the [page size](Storage::page_size) is at most 4096 bytes. const MAX_PAGE_SIZE: Nat = 4096; /// Maximum number of erase cycles. /// -/// Currently, the store only supports storages where the maximum number of erase cycles fits on 16 -/// bits. +/// Currently, the store only supports storages where the [maximum number of erase +/// cycles](Storage::max_page_erases) fits in 16 bits, i.e. it is at most 65535. const MAX_ERASE_CYCLE: Nat = 65535; /// Minimum number of pages. /// -/// Currently, the store only supports storages with at least 3 pages. +/// Currently, the store only supports storages where the [number of pages](Storage::num_pages) is +/// at least 3. const MIN_NUM_PAGES: Nat = 3; /// Maximum page index. /// -/// Thus the maximum number of pages is one more than this number. Currently, the store only -/// supports storages where the number of pages is between 3 and 64. +/// Currently, the store only supports storages where the [number of pages](Storage::num_pages) is +/// at most 64, i.e. the maximum page index is 63. const MAX_PAGE_INDEX: Nat = 63; /// Maximum key index. /// -/// Thus the number of keys is one more than this number. Currently, the store only supports 4096 -/// keys. +/// Currently, the store only supports 4096 keys, i.e. the maximum key index is 4095. const MAX_KEY_INDEX: Nat = 4095; /// Maximum length in bytes of a user payload. /// -/// Currently, the store only supports values smaller than 1024 bytes. +/// Currently, the store only supports values at most 1023 bytes long. This may be further reduced +/// depending on the [page size](Storage::page_size), see [`Format::max_value_len`]. const MAX_VALUE_LEN: Nat = 1023; /// Maximum number of updates per transaction. @@ -108,9 +114,15 @@ const MAX_VALUE_LEN: Nat = 1023; const MAX_UPDATES: Nat = 31; /// Maximum number of words per virtual page. -const MAX_VIRT_PAGE_SIZE: Nat = div_ceil(MAX_PAGE_SIZE, WORD_SIZE) - CONTENT_WORD; +/// +/// A virtual page has [`CONTENT_WORD`] less [words](WORD_SIZE) than the storage [page +/// size](Storage::page_size). Those words are used to store the page header. Since a page has at +/// least [8](MIN_PAGE_SIZE) words, a virtual page has at least 6 words. +const MAX_VIRT_PAGE_SIZE: Nat = MAX_PAGE_SIZE / WORD_SIZE - CONTENT_WORD; /// Word with all bits set to one. +/// +/// After a page is erased, all words are equal to this value. const ERASED_WORD: Word = Word(!(0 as WORD)); /// Helpers for a given storage configuration. @@ -120,33 +132,31 @@ pub struct Format { /// /// # Invariant /// - /// - Words divide a page evenly. - /// - There are at least 8 words in a page. - /// - There are at most `MAX_PAGE_SIZE` bytes in a page. + /// - [Words](WORD_SIZE) divide a page evenly. + /// - There are at least [`MIN_PAGE_SIZE`] words in a page. + /// - There are at most [`MAX_PAGE_SIZE`] bytes in a page. page_size: Nat, /// The number of pages in the storage. /// /// # Invariant /// - /// - There are at least 3 pages. - /// - There are at most `MAX_PAGE_INDEX + 1` pages. + /// - There are at least [`MIN_NUM_PAGES`] pages. + /// - There are at most [`MAX_PAGE_INDEX`] + 1 pages. num_pages: Nat, /// The maximum number of times a page can be erased. /// /// # Invariant /// - /// - A page can be erased at most `MAX_ERASE_CYCLE` times. + /// - A page can be erased at most [`MAX_ERASE_CYCLE`] times. max_page_erases: Nat, } impl Format { /// Extracts the format from a storage. /// - /// Returns `None` if the storage is not [supported]. - /// - /// [supported]: struct.Format.html#method.is_storage_supported + /// Returns `None` if the storage is not [supported](Format::is_storage_supported). pub fn new(storage: &S) -> Option { if Format::is_storage_supported(storage) { Some(Format { @@ -162,21 +172,12 @@ impl Format { /// Returns whether a storage is supported. /// /// A storage is supported if the following conditions hold: - /// - The size of a word is [`WORD_SIZE`] bytes. - /// - The size of a word evenly divides the size of a page. - /// - A page contains at least [`MIN_NUM_WORDS_PER_PAGE`] words. - /// - A page contains at most [`MAX_PAGE_SIZE`] bytes. - /// - There are at least [`MIN_NUM_PAGES`] pages. - /// - There are at most [`MAX_PAGE_INDEX`]` + 1` pages. - /// - A word can be written at least twice between erase cycles. - /// - The maximum number of erase cycles is at most [`MAX_ERASE_CYCLE`]. - /// - /// [`WORD_SIZE`]: constant.WORD_SIZE.html - /// [`MIN_NUM_WORDS_PER_PAGE`]: constant.MIN_NUM_WORDS_PER_PAGE.html - /// [`MAX_PAGE_SIZE`]: constant.MAX_PAGE_SIZE.html - /// [`MIN_NUM_PAGES`]: constant.MIN_NUM_PAGES.html - /// [`MAX_PAGE_INDEX`]: constant.MAX_PAGE_INDEX.html - /// [`MAX_ERASE_CYCLE`]: constant.MAX_ERASE_CYCLE.html + /// - The [`Storage::word_size`] is [`WORD_SIZE`] bytes. + /// - The [`Storage::word_size`] evenly divides the [`Storage::page_size`]. + /// - The [`Storage::page_size`] is between [`MIN_PAGE_SIZE`] words and [`MAX_PAGE_SIZE`] bytes. + /// - The [`Storage::num_pages`] is between [`MIN_NUM_PAGES`] and [`MAX_PAGE_INDEX`] + 1. + /// - The [`Storage::max_word_writes`] is at least 2. + /// - The [`Storage::max_page_erases`] is at most [`MAX_ERASE_CYCLE`]. fn is_storage_supported(storage: &S) -> bool { let word_size = usize_to_nat(storage.word_size()); let page_size = usize_to_nat(storage.page_size()); @@ -185,7 +186,7 @@ impl Format { let max_page_erases = usize_to_nat(storage.max_page_erases()); word_size == WORD_SIZE && page_size % word_size == 0 - && (MIN_NUM_WORDS_PER_PAGE * word_size <= page_size && page_size <= MAX_PAGE_SIZE) + && (MIN_PAGE_SIZE * word_size <= page_size && page_size <= MAX_PAGE_SIZE) && (MIN_NUM_PAGES <= num_pages && num_pages <= MAX_PAGE_INDEX + 1) && max_word_writes >= 2 && max_page_erases <= MAX_ERASE_CYCLE @@ -198,28 +199,28 @@ impl Format { /// The size of a page in bytes. /// - /// We have `MIN_NUM_WORDS_PER_PAGE * self.word_size() <= self.page_size() <= MAX_PAGE_SIZE`. + /// This is at least [`MIN_PAGE_SIZE`] [words](WORD_SIZE) and at most [`MAX_PAGE_SIZE`] bytes. pub fn page_size(&self) -> Nat { self.page_size } - /// The number of pages in the storage, denoted by `N`. + /// The number of pages in the storage, denoted by N. /// - /// We have `MIN_NUM_PAGES <= N <= MAX_PAGE_INDEX + 1`. + /// We have [`MIN_NUM_PAGES`] ≤ N ≤ [`MAX_PAGE_INDEX`] + 1. pub fn num_pages(&self) -> Nat { self.num_pages } /// The maximum page index. /// - /// We have `2 <= self.max_page() <= MAX_PAGE_INDEX`. + /// This is at least [`MIN_NUM_PAGES`] - 1 and at most [`MAX_PAGE_INDEX`]. pub fn max_page(&self) -> Nat { self.num_pages - 1 } - /// The maximum number of times a page can be erased, denoted by `E`. + /// The maximum number of times a page can be erased, denoted by E. /// - /// We have `E <= MAX_ERASE_CYCLE`. + /// We have E ≤ [`MAX_ERASE_CYCLE`]. pub fn max_page_erases(&self) -> Nat { self.max_page_erases } @@ -234,19 +235,18 @@ impl Format { MAX_UPDATES } - /// The size of a virtual page in words, denoted by `Q`. + /// The size of a virtual page in words, denoted by Q. /// /// A virtual page is stored in a physical page after the page header. /// - /// We have `MIN_NUM_WORDS_PER_PAGE - 2 <= Q <= MAX_VIRT_PAGE_SIZE`. + /// We have [`MIN_PAGE_SIZE`] - 2 ≤ Q ≤ [`MAX_VIRT_PAGE_SIZE`]. pub fn virt_page_size(&self) -> Nat { self.page_size() / self.word_size() - CONTENT_WORD } /// The maximum length in bytes of a user payload. /// - /// We have `(MIN_NUM_WORDS_PER_PAGE - 3) * self.word_size() <= self.max_value_len() <= - /// MAX_VALUE_LEN`. + /// This is at least [`MIN_PAGE_SIZE`] - 3 [words](WORD_SIZE) and at most [`MAX_VALUE_LEN`]. pub fn max_value_len(&self) -> Nat { min( (self.virt_page_size() - 1) * self.word_size(), @@ -254,57 +254,59 @@ impl Format { ) } - /// The maximum prefix length in words, denoted by `M`. + /// The maximum prefix length in words, denoted by M. /// /// A prefix is the first words of a virtual page that belong to the last entry of the previous /// virtual page. This happens because entries may overlap up to 2 virtual pages. /// - /// We have `MIN_NUM_WORDS_PER_PAGE - 3 <= M < Q`. + /// We have [`MIN_PAGE_SIZE`] - 3 ≤ M < Q. pub fn max_prefix_len(&self) -> Nat { self.bytes_to_words(self.max_value_len()) } - /// The total virtual capacity in words, denoted by `V`. + /// The virtual window size in words, denoted by W. /// - /// We have `V = (N - 1) * (Q - 1) - M`. + /// This is the span of virtual storage that is accessible. In particular, all store content + /// fits within this window. /// - /// We can show `V >= (N - 2) * (Q - 1)` with the following steps: - /// - `M <= Q - 1` from `M < Q` from [`M`] definition - /// - `-M >= -(Q - 1)` from above - /// - `V >= (N - 1) * (Q - 1) - (Q - 1)` from `V` definition + /// We have W = (N - 1) × Q - M. + pub fn window_size(&self) -> Nat { + (self.num_pages() - 1) * self.virt_page_size() - self.max_prefix_len() + } + + /// The total virtual capacity in words, denoted by V. /// - /// [`M`]: struct.Format.html#method.max_prefix_len + /// This is the span of virtual storage after which we trigger a compaction. This is smaller + /// than the virtual window because compaction may transiently overflow out of this virtual + /// capacity. + /// + /// We have V = W - (N - 1) = (N - 1) × (Q - 1) - M. pub fn virt_size(&self) -> Nat { (self.num_pages() - 1) * (self.virt_page_size() - 1) - self.max_prefix_len() } - /// The total user capacity in words, denoted by `C`. + /// The total user capacity in words, denoted by C. /// - /// We have `C = V - N = (N - 1) * (Q - 2) - M - 1`. + /// We have C = V - N = (N - 1) × (Q - 2) - M - 1. /// - /// We can show `C >= (N - 2) * (Q - 2) - 2` with the following steps: - /// - `V >= (N - 2) * (Q - 1)` from [`V`] definition - /// - `C >= (N - 2) * (Q - 1) - N` from `C` definition - /// - `(N - 2) * (Q - 1) - N = (N - 2) * (Q - 2) - 2` by calculus - /// - /// [`V`]: struct.Format.html#method.virt_size + /// We can show C ≥ (N - 2) × (Q - 2) - 2 with the following steps: + /// - M ≤ Q - 1 from M < Q from [M](Format::max_prefix_len)'s definition + /// - C ≥ (N - 1) × (Q - 2) - (Q - 1) - 1 from C's definition + /// - C ≥ (N - 2) × (Q - 2) - 2 by calculus pub fn total_capacity(&self) -> Nat { // From the virtual capacity, we reserve N - 1 words for `Erase` entries and 1 word for a // `Clear` entry. self.virt_size() - self.num_pages() } - /// The total virtual lifetime in words, denoted by `L`. + /// The total virtual lifetime in words, denoted by L. /// - /// We have `L = (E * N + N - 1) * Q`. + /// We have L = (E × N + N - 1) × Q. pub fn total_lifetime(&self) -> Position { Position::new(self, self.max_page_erases(), self.num_pages() - 1, 0) } /// Returns the word position of the first entry of a page. - /// - /// The init info of the page must be provided to know where the first entry of the page - /// starts. pub fn page_head(&self, init: InitInfo, page: Nat) -> Position { Position::new(self, init.cycle, page, init.prefix) } @@ -360,7 +362,7 @@ impl Format { WordState::Partial } else { let tail = COMPACT_TAIL.get(word); - if tail > self.virt_size() + self.max_prefix_len() { + if tail > self.window_size() { return Err(StoreError::InvalidStorage); } WordState::Valid(CompactInfo { tail }) @@ -492,13 +494,16 @@ impl Format { } /// Returns the capacity required by a transaction. - pub fn transaction_capacity(&self, updates: &[StoreUpdate]) -> Nat { + pub fn transaction_capacity>( + &self, + updates: &[StoreUpdate], + ) -> Nat { match updates.len() { // An empty transaction doesn't consume anything. 0 => 0, // Transactions with a single update are optimized by avoiding a marker entry. 1 => match &updates[0] { - StoreUpdate::Insert { value, .. } => self.entry_size(value), + StoreUpdate::Insert { value, .. } => self.entry_size(value.borrow()), // Transactions with a single update which is a removal don't consume anything. StoreUpdate::Remove { .. } => 0, }, @@ -508,9 +513,9 @@ impl Format { } /// Returns the capacity of an update. - fn update_capacity(&self, update: &StoreUpdate) -> Nat { + fn update_capacity>(&self, update: &StoreUpdate) -> Nat { match update { - StoreUpdate::Insert { value, .. } => self.entry_size(value), + StoreUpdate::Insert { value, .. } => self.entry_size(value.borrow()), StoreUpdate::Remove { .. } => 1, } } @@ -523,7 +528,10 @@ impl Format { /// Checks if a transaction is valid and returns its sorted keys. /// /// Returns `None` if the transaction is invalid. - pub fn transaction_valid(&self, updates: &[StoreUpdate]) -> Option> { + pub fn transaction_valid>( + &self, + updates: &[StoreUpdate], + ) -> Option> { if usize_to_nat(updates.len()) > self.max_updates() { return None; } @@ -550,7 +558,7 @@ impl Format { /// /// # Preconditions /// - /// - `bytes + self.word_size()` does not overflow. + /// - `bytes` + [`Self::word_size`] does not overflow. pub fn bytes_to_words(&self, bytes: Nat) -> Nat { div_ceil(bytes, self.word_size()) } @@ -564,7 +572,7 @@ const COMPACT_WORD: Nat = 1; /// The word index of the content of a page. /// -/// Since a page is at least 8 words, there is always at least 6 words of content. +/// This is also the length in words of the page header. const CONTENT_WORD: Nat = 2; /// The checksum for a single word. @@ -711,21 +719,21 @@ bitfield! { /// The position of a word in the virtual storage. /// -/// With the notations defined in `Format`, let: -/// - `w` a virtual word offset in a page which is between `0` and `Q - 1` -/// - `p` a page offset which is between `0` and `N - 1` -/// - `c` the number of erase cycles of a page which is between `0` and `E` +/// With the notations defined in [`Format`], let: +/// - w denote a word offset in a virtual page, thus between 0 and Q - 1 +/// - p denote a page offset, thus between 0 and N - 1 +/// - c denote the number of times a page was erased, thus between 0 and E /// -/// Then the position of a word is `(c*N + p)*Q + w`. This position monotonically increases and +/// The position of a word is (c × N + p) × Q + w. This position monotonically increases and /// represents the consumed lifetime of the storage. /// -/// This type is kept abstract to avoid possible confusion with `Nat` and `Word` if they happen to -/// have the same representation. Here is an overview of their semantics: +/// This type is kept abstract to avoid possible confusion with [`Nat`] and [`Word`] if they happen +/// to have the same representation. Here is an overview of their semantics: /// /// | Name | Semantics | Arithmetic operations | Bit-wise operations | /// | ---------- | --------------------------- | --------------------- | ------------------- | -/// | `Nat` | Natural numbers | Yes (no overflow) | No | -/// | `Word` | Word in flash | No | Yes | +/// | [`Nat`] | Natural numbers | Yes (no overflow) | No | +/// | [`Word`] | Word in flash | No | Yes | /// | `Position` | Position in virtual storage | Yes (no overflow) | No | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Position(Nat); @@ -756,9 +764,9 @@ impl Position { /// Create a word position given its coordinates. /// /// The coordinates of a word are: - /// - Its word index in its page. + /// - Its word index in its virtual page. /// - Its page index in the storage. - /// - The number of times that page was erased. + /// - The number of times its page was erased. pub fn new(format: &Format, cycle: Nat, page: Nat, word: Nat) -> Position { Position((cycle * format.num_pages() + page) * format.virt_page_size() + word) } @@ -921,11 +929,11 @@ pub fn is_erased(slice: &[u8]) -> bool { /// Divides then takes ceiling. /// -/// Returns `ceil(x / m)` in mathematical notations (not Rust code). +/// Returns ⌈x / m⌉, i.e. the lowest natural number r such that r ≥ x / m. /// /// # Preconditions /// -/// - `x + m` does not overflow. +/// - x + m does not overflow. const fn div_ceil(x: Nat, m: Nat) -> Nat { (x + m - 1) / m } @@ -1077,4 +1085,15 @@ mod tests { 0xff800000 ); } + + #[test] + fn position_offsets_fit_in_a_halfword() { + // The store stores in RAM the entry positions as their offset from the head. Those offsets + // are represented as u16. The bound below is a large over-approximation of the maximal + // offset. We first make sure it fits in a u16. + const MAX_POS: Nat = (MAX_PAGE_INDEX + 1) * MAX_VIRT_PAGE_SIZE; + assert!(MAX_POS <= u16::MAX as Nat); + // We also check the actual value for up-to-date documentation, since it's a constant. + assert_eq!(MAX_POS, 0xff80); + } } diff --git a/apps/vault/libraries/persistent_store/src/fragment.rs b/apps/vault/libraries/persistent_store/src/fragment.rs new file mode 100644 index 000000000..9d2e06793 --- /dev/null +++ b/apps/vault/libraries/persistent_store/src/fragment.rs @@ -0,0 +1,345 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Support for fragmented entries. +//! +//! This module permits to handle entries larger than the [maximum value +//! length](Store::max_value_length) by storing ordered consecutive fragments in a sequence of keys. +//! The first keys hold fragments of maximal length, followed by a possibly partial fragment. The +//! remaining keys are not used. + +use crate::{Storage, Store, StoreError, StoreHandle, StoreResult, StoreUpdate}; +use std::vec::Vec; +use core::ops::Range; + +/// Represents a sequence of keys. +#[allow(clippy::len_without_is_empty)] +pub trait Keys { + /// Returns the number of keys. + fn len(&self) -> usize; + + /// Returns the position of a key in the sequence. + fn pos(&self, key: usize) -> Option; + + /// Returns the key of a position in the sequence. + /// + /// # Preconditions + /// + /// The position must be within the length: `pos` < [`Self::len`]. + fn key(&self, pos: usize) -> usize; +} + +impl Keys for Range { + fn len(&self) -> usize { + self.end - self.start + } + + fn pos(&self, key: usize) -> Option { + if self.start <= key && key < self.end { + Some(key - self.start) + } else { + None + } + } + + fn key(&self, pos: usize) -> usize { + debug_assert!(pos < Keys::len(self)); + self.start + pos + } +} + +/// Reads the concatenated value of a sequence of keys. +pub fn read(store: &Store, keys: &impl Keys) -> StoreResult>> { + let handles = get_handles(store, keys)?; + if handles.is_empty() { + return Ok(None); + } + let mut result = Vec::with_capacity(handles.len() * store.max_value_length()); + for handle in handles { + result.extend(handle.get_value(store)?); + } + Ok(Some(result)) +} + +/// Reads a range from the concatenated value of a sequence of keys. +/// +/// This is equivalent to calling [`read`] then taking the range except that: +/// - Only the needed chunks are read. +/// - The range is truncated to fit in the value. +pub fn read_range( + store: &Store, + keys: &impl Keys, + range: Range, +) -> StoreResult>> { + let range_len = match range.end.checked_sub(range.start) { + None => return Err(StoreError::InvalidArgument), + Some(x) => x, + }; + let handles = get_handles(store, keys)?; + if handles.is_empty() { + return Ok(None); + } + let mut result = Vec::with_capacity(range_len); + let mut offset = 0; + for handle in handles { + let start = range.start.saturating_sub(offset); + let length = handle.get_length(store)?; + let end = core::cmp::min(range.end.saturating_sub(offset), length); + offset += length; + if start < end { + result.extend(&handle.get_value(store)?[start..end]); + } + } + Ok(Some(result)) +} + +/// Writes a value to a sequence of keys as chunks. +pub fn write(store: &mut Store, keys: &impl Keys, value: &[u8]) -> StoreResult<()> { + let handles = get_handles(store, keys)?; + let keys_len = keys.len(); + let mut updates = Vec::with_capacity(keys_len); + let mut chunks = value.chunks(store.max_value_length()); + for pos in 0..keys_len { + let key = keys.key(pos); + match (handles.get(pos), chunks.next()) { + // No existing handle and no new chunk: nothing to do. + (None, None) => (), + // Existing handle and no new chunk: remove old handle. + (Some(_), None) => updates.push(StoreUpdate::Remove { key }), + // Existing handle with same value as new chunk: nothing to do. + (Some(handle), Some(value)) if handle.get_value(store)? == value => (), + // New chunk: Write (or overwrite) the new value. + (_, Some(value)) => updates.push(StoreUpdate::Insert { key, value }), + } + } + if chunks.next().is_some() { + // The value is too long. + return Err(StoreError::InvalidArgument); + } + store.transaction(&updates) +} + +/// Deletes the value of a sequence of keys. +pub fn delete(store: &mut Store, keys: &impl Keys) -> StoreResult<()> { + let updates: Vec>> = get_handles(store, keys)? + .iter() + .map(|handle| StoreUpdate::Remove { + key: handle.get_key(), + }) + .collect(); + store.transaction(&updates) +} + +/// Returns the handles of a sequence of keys. +/// +/// The handles are truncated to the keys that are present. +fn get_handles(store: &Store, keys: &impl Keys) -> StoreResult> { + let keys_len = keys.len(); + let mut handles: Vec> = vec![None; keys_len as usize]; + for handle in store.iter()? { + let handle = handle?; + let pos = match keys.pos(handle.get_key()) { + Some(pos) => pos, + None => continue, + }; + if pos >= keys_len { + return Err(StoreError::InvalidArgument); + } + if let Some(old_handle) = &handles[pos] { + if old_handle.get_key() != handle.get_key() { + // The user provided a non-injective `pos` function. + return Err(StoreError::InvalidArgument); + } else { + return Err(StoreError::InvalidStorage); + } + } + handles[pos] = Some(handle); + } + let num_handles = handles.iter().filter(|x| x.is_some()).count(); + let mut result = Vec::with_capacity(num_handles); + for (i, handle) in handles.into_iter().enumerate() { + match (i < num_handles, handle) { + (true, Some(handle)) => result.push(handle), + (false, None) => (), + // We should have `num_handles` Somes followed by Nones. + _ => return Err(StoreError::InvalidStorage), + } + } + Ok(result) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test::MINIMAL; + + #[test] + fn read_empty_entry() { + let store = MINIMAL.new_store(); + assert_eq!(read(&store, &(0..4)), Ok(None)); + } + + #[test] + fn read_single_chunk() { + let mut store = MINIMAL.new_store(); + let value = b"hello".to_vec(); + assert_eq!(store.insert(0, &value), Ok(())); + assert_eq!(read(&store, &(0..4)), Ok(Some(value))); + } + + #[test] + fn read_multiple_chunks() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!(read(&store, &(0..4)), Ok(Some(value))); + } + + #[test] + fn read_range_first_chunk() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!( + read_range(&store, &(0..4), 0..10), + Ok(Some((0..10).collect())) + ); + assert_eq!( + read_range(&store, &(0..4), 10..20), + Ok(Some((10..20).collect())) + ); + assert_eq!( + read_range(&store, &(0..4), 40..52), + Ok(Some((40..52).collect())) + ); + } + + #[test] + fn read_range_second_chunk() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!(read_range(&store, &(0..4), 52..53), Ok(Some(vec![52]))); + assert_eq!(read_range(&store, &(0..4), 53..54), Ok(Some(vec![53]))); + assert_eq!(read_range(&store, &(0..4), 59..60), Ok(Some(vec![59]))); + } + + #[test] + fn read_range_both_chunks() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!( + read_range(&store, &(0..4), 40..60), + Ok(Some((40..60).collect())) + ); + assert_eq!( + read_range(&store, &(0..4), 0..60), + Ok(Some((0..60).collect())) + ); + } + + #[test] + fn read_range_outside() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!( + read_range(&store, &(0..4), 40..100), + Ok(Some((40..60).collect())) + ); + assert_eq!(read_range(&store, &(0..4), 60..100), Ok(Some(vec![]))); + } + + #[test] + fn write_single_chunk() { + let mut store = MINIMAL.new_store(); + let value = b"hello".to_vec(); + assert_eq!(write(&mut store, &(0..4), &value), Ok(())); + assert_eq!(store.find(0), Ok(Some(value))); + assert_eq!(store.find(1), Ok(None)); + assert_eq!(store.find(2), Ok(None)); + assert_eq!(store.find(3), Ok(None)); + } + + #[test] + fn write_multiple_chunks() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(write(&mut store, &(0..4), &value), Ok(())); + assert_eq!(store.find(0), Ok(Some((0..52).collect()))); + assert_eq!(store.find(1), Ok(Some((52..60).collect()))); + assert_eq!(store.find(2), Ok(None)); + assert_eq!(store.find(3), Ok(None)); + } + + #[test] + fn overwrite_less_chunks() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + let value: Vec<_> = (42..69).collect(); + assert_eq!(write(&mut store, &(0..4), &value), Ok(())); + assert_eq!(store.find(0), Ok(Some((42..69).collect()))); + assert_eq!(store.find(1), Ok(None)); + assert_eq!(store.find(2), Ok(None)); + assert_eq!(store.find(3), Ok(None)); + } + + #[test] + fn overwrite_needed_chunks() { + let mut store = MINIMAL.new_store(); + let mut value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + // Current lifetime is 2 words of overhead (2 insert) and 60 bytes of data. + let mut lifetime = 2 + 60 / 4; + assert_eq!(store.lifetime().unwrap().used(), lifetime); + // Update the value. + value.extend(60..80); + assert_eq!(write(&mut store, &(0..4), &value), Ok(())); + // Added lifetime is 1 word of overhead (1 insert) and (80 - 52) bytes of data. + lifetime += 1 + (80 - 52) / 4; + assert_eq!(store.lifetime().unwrap().used(), lifetime); + } + + #[test] + fn delete_empty() { + let mut store = MINIMAL.new_store(); + assert_eq!(delete(&mut store, &(0..4)), Ok(())); + assert_eq!(store.find(0), Ok(None)); + assert_eq!(store.find(1), Ok(None)); + assert_eq!(store.find(2), Ok(None)); + assert_eq!(store.find(3), Ok(None)); + } + + #[test] + fn delete_chunks() { + let mut store = MINIMAL.new_store(); + let value: Vec<_> = (0..60).collect(); + assert_eq!(store.insert(0, &value[..52]), Ok(())); + assert_eq!(store.insert(1, &value[52..]), Ok(())); + assert_eq!(delete(&mut store, &(0..4)), Ok(())); + assert_eq!(store.find(0), Ok(None)); + assert_eq!(store.find(1), Ok(None)); + assert_eq!(store.find(2), Ok(None)); + assert_eq!(store.find(3), Ok(None)); + } +} diff --git a/apps/vault/libraries/persistent_store/src/lib.rs b/apps/vault/libraries/persistent_store/src/lib.rs index 2be9d1009..61ab90acc 100644 --- a/apps/vault/libraries/persistent_store/src/lib.rs +++ b/apps/vault/libraries/persistent_store/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2019-2020 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,191 +12,193 @@ // See the License for the specific language governing permissions and // limitations under the License. -// TODO(ia0): Add links once the code is complete. +// The documentation is easier to read from a browser: +// - Run: cargo doc --document-private-items --features=std +// - Open: target/doc/persistent_store/index.html + //! Store abstraction for flash storage //! //! # Specification //! -//! The store provides a partial function from keys to values on top of a storage -//! interface. The store total capacity depends on the size of the storage. Store -//! updates may be bundled in transactions. Mutable operations are atomic, including -//! when interrupted. +//! The [store](Store) provides a partial function from keys to values on top of a +//! [storage](Storage) interface. The store total [capacity](Store::capacity) depends on the size of +//! the storage. Store [updates](StoreUpdate) may be bundled in [transactions](Store::transaction). +//! Mutable operations are atomic, including when interrupted. //! -//! The store is flash-efficient in the sense that it uses the storage lifetime -//! efficiently. For each page, all words are written at least once between erase -//! cycles and all erase cycles are used. However, not all written words are user -//! content: lifetime is also consumed with metadata and compaction. +//! The store is flash-efficient in the sense that it uses the storage [lifetime](Store::lifetime) +//! efficiently. For each page, all words are written at least once between erase cycles and all +//! erase cycles are used. However, not all written words are user content: Lifetime is also +//! consumed with metadata and compaction. //! -//! The store is extendable with other entries than key-values. It is essentially a -//! framework providing access to the storage lifetime. The partial function is -//! simply the most common usage and can be used to encode other usages. +//! The store is extendable with other entries than key-values. It is essentially a framework +//! providing access to the storage lifetime. The partial function is simply the most common usage +//! and can be used to encode other usages. //! //! ## Definitions //! -//! An _entry_ is a pair of a key and a value. A _key_ is a number between 0 -//! and 4095. A _value_ is a byte slice with a length between 0 and 1023 bytes (for -//! large enough pages). +//! An _entry_ is a pair of a key and a value. A _key_ is a number between 0 and +//! [4095](format::MAX_KEY_INDEX). A _value_ is a byte slice with a length between 0 and +//! [1023](format::Format::max_value_len) bytes (for large enough pages). //! //! The store provides the following _updates_: -//! - Given a key and a value, `Insert` updates the store such that the value is +//! - Given a key and a value, [`StoreUpdate::Insert`] updates the store such that the value is //! associated with the key. The values for other keys are left unchanged. -//! - Given a key, `Remove` updates the store such that no value is associated with -//! the key. The values for other keys are left unchanged. Additionally, if there -//! was a value associated with the key, the value is wiped from the storage -//! (all its bits are set to 0). +//! - Given a key, [`StoreUpdate::Remove`] updates the store such that no value is associated with +//! the key. The values for other keys are left unchanged. Additionally, if there was a value +//! associated with the key, the value is wiped from the storage (all its bits are set to 0). //! //! The store provides the following _read-only operations_: -//! - `Iter` iterates through the store returning all entries exactly once. The -//! iteration order is not specified but stable between mutable operations. -//! - `Capacity` returns how many words can be stored before the store is full. -//! - `Lifetime` returns how many words can be written before the storage lifetime -//! is consumed. +//! - [`Store::iter`] iterates through the store returning all entries exactly once. The iteration +//! order is not specified but stable between mutable operations. +//! - [`Store::capacity`] returns how many words can be stored before the store is full. +//! - [`Store::lifetime`] returns how many words can be written before the storage lifetime is +//! consumed. //! //! The store provides the following _mutable operations_: -//! - Given a set of independent updates, `Transaction` applies the sequence of -//! updates. -//! - Given a threshold, `Clear` removes all entries with a key greater or equal -//! to the threshold. -//! - Given a length in words, `Prepare` makes one step of compaction unless that -//! many words can be written without compaction. This operation has no effect -//! on the store but may still mutate its storage. In particular, the store has -//! the same capacity but a possibly reduced lifetime. +//! - Given a set of independent updates, [`Store::transaction`] applies the sequence of updates. +//! - Given a threshold, [`Store::clear`] removes all entries with a key greater or equal to the +//! threshold. +//! - Given a length in words, [`Store::prepare`] makes one step of compaction unless that many +//! words can be written without compaction. This operation has no effect on the store but may +//! still mutate its storage. In particular, the store has the same capacity but a possibly +//! reduced lifetime. //! -//! A mutable operation is _atomic_ if, when power is lost during the operation, the -//! store is either updated (as if the operation succeeded) or left unchanged (as if -//! the operation did not occur). If the store is left unchanged, lifetime may still -//! be consumed. +//! A mutable operation is _atomic_ if, when power is lost during the operation, the store is either +//! updated (as if the operation succeeded) or left unchanged (as if the operation did not occur). +//! If the store is left unchanged, lifetime may still be consumed. //! //! The store relies on the following _storage interface_: -//! - It is possible to read a byte slice. The slice won't span multiple pages. -//! - It is possible to write a word slice. The slice won't span multiple pages. -//! - It is possible to erase a page. -//! - The pages are sequentially indexed from 0. If the actual underlying storage -//! is segmented, then the storage layer should translate those indices to -//! actual page addresses. +//! - It is possible to [read](Storage::read_slice) a byte slice. The slice won't span multiple +//! pages. +//! - It is possible to [write](Storage::write_slice) a word slice. The slice won't span multiple +//! pages. +//! - It is possible to [erase](Storage::erase_page) a page. +//! - The pages are sequentially indexed from 0. If the actual underlying storage is segmented, +//! then the storage layer should translate those indices to actual page addresses. //! -//! The store has a _total capacity_ of `C = (N - 1) * (P - 4) - M - 1` words, where -//! `P` is the number of words per page, `N` is the number of pages, and `M` is the -//! maximum length in words of a value (256 for large enough pages). The capacity -//! used by each mutable operation is given below (a transient word only uses -//! capacity during the operation): -//! - `Insert` uses `1 + ceil(len / 4)` words where `len` is the length of the -//! value in bytes. If an entry was replaced, the words used by its insertion -//! are freed. -//! - `Remove` doesn't use capacity if alone in the transaction and 1 transient -//! word otherwise. If an entry was deleted, the words used by its insertion are -//! freed. -//! - `Transaction` uses 1 transient word. In addition, the updates of the -//! transaction use and free words as described above. -//! - `Clear` doesn't use capacity and frees the words used by the insertion of -//! the deleted entries. -//! - `Prepare` doesn't use capacity. +//! The store has a _total capacity_ of C = (N - 1) × (P - 4) - M - 1 words, where: +//! - P is the number of words per page +//! - [N](format::Format::num_pages) is the number of pages +//! - [M](format::Format::max_prefix_len) is the maximum length in words of a value (256 for large +//! enough pages) //! -//! The _total lifetime_ of the store is below `L = ((E + 1) * N - 1) * (P - 2)` and -//! above `L - M` words, where `E` is the maximum number of erase cycles. The -//! lifetime is used when capacity is used, including transiently, as well as when -//! compaction occurs. Compaction frequency and lifetime consumption are positively -//! correlated to the store load factor (the ratio of used capacity to total capacity). +//! The capacity used by each mutable operation is given below (a transient word only uses capacity +//! during the operation): //! -//! It is possible to approximate the cost of transient words in terms of capacity: -//! `L` transient words are equivalent to `C - x` words of capacity where `x` is the -//! average capacity (including transient) of operations. +//! | Operation/Update | Used capacity | Freed capacity | Transient capacity | +//! | ----------------------- | ---------------- | ----------------- | ------------------ | +//! | [`StoreUpdate::Insert`] | 1 + value length | overwritten entry | 0 | +//! | [`StoreUpdate::Remove`] | 0 | deleted entry | see below\* | +//! | [`Store::transaction`] | 0 + updates | 0 + updates | 1 | +//! | [`Store::clear`] | 0 | deleted entries | 0 | +//! | [`Store::prepare`] | 0 | 0 | 0 | +//! +//! \*0 if the update is alone in the transaction, otherwise 1. +//! +//! The _total lifetime_ of the store is below L = ((E + 1) × N - 1) × (P - 2) and above L - M +//! words, where E is the maximum number of erase cycles. The lifetime is used when capacity is +//! used, including transiently, as well as when compaction occurs. Compaction frequency and +//! lifetime consumption are positively correlated to the store load factor (the ratio of used +//! capacity to total capacity). +//! +//! It is possible to approximate the cost of transient words in terms of capacity: L transient +//! words are equivalent to C - x words of capacity where x is the average capacity (including +//! transient) of operations. //! //! ## Preconditions //! //! The following assumptions need to hold, or the store may behave in unexpected ways: -//! - A word can be written twice between erase cycles. -//! - A page can be erased `E` times after the first boot of the store. -//! - When power is lost while writing a slice or erasing a page, the next read -//! returns a slice where a subset (possibly none or all) of the bits that -//! should have been modified have been modified. -//! - Reading a slice is deterministic. When power is lost while writing a slice -//! or erasing a slice (erasing a page containing that slice), reading that -//! slice repeatedly returns the same result (until it is overwritten or its -//! page is erased). -//! - To decide whether a page has been erased, it is enough to test if all its -//! bits are equal to 1. -//! - When power is lost while writing a slice or erasing a page, that operation -//! does not count towards the limits. However, completing that write or erase -//! operation would count towards the limits, as if the number of writes per -//! word and number of erase cycles could be fractional. -//! - The storage is only modified by the store. Note that completely erasing the -//! storage is supported, essentially losing all content and lifetime tracking. -//! It is preferred to use `Clear` with a threshold of 0 to keep the lifetime -//! tracking. +//! - A word can be written [twice](Storage::max_word_writes) between erase cycles. +//! - A page can be erased [E](Storage::max_page_erases) times after the first boot of the store. +//! - When power is lost while writing a slice or erasing a page, the next read returns a slice +//! where a subset (possibly none or all) of the bits that should have been modified have been +//! modified. +//! - Reading a slice is deterministic. When power is lost while writing a slice or erasing a +//! slice (erasing a page containing that slice), reading that slice repeatedly returns the same +//! result (until it is overwritten or its page is erased). +//! - To decide whether a page has been erased, it is enough to test if all its bits are equal +//! to 1. +//! - When power is lost while writing a slice or erasing a page, that operation does not count +//! towards the limits. However, completing that write or erase operation would count towards +//! the limits, as if the number of writes per word and number of erase cycles could be +//! fractional. +//! - The storage is only modified by the store. Note that completely erasing the storage is +//! supported, essentially losing all content and lifetime tracking. It is preferred to use +//! [`Store::clear`] with a threshold of 0 to keep the lifetime tracking. //! -//! The store properties may still hold outside some of those assumptions, but with -//! an increasing chance of failure. +//! The store properties may still hold outside some of those assumptions, but with an increasing +//! chance of failure. //! //! # Implementation //! //! We define the following constants: -//! - `E < 65536` the number of times a page can be erased. -//! - `3 <= N < 64` the number of pages in the storage. -//! - `8 <= P <= 1024` the number of words in a page. -//! - `Q = P - 2` the number of words in a virtual page. -//! - `K = 4096` the maximum number of keys. -//! - `M = min(Q - 1, 256)` the maximum length in words of a value. -//! - `V = (N - 1) * (Q - 1) - M` the virtual capacity. -//! - `C = V - N` the user capacity. +//! - [E](format::Format::max_page_erases) ≤ [65535](format::MAX_ERASE_CYCLE) the number of times +//! a page can be erased. +//! - 3 ≤ [N](format::Format::num_pages) < 64 the number of pages in the storage. +//! - 8 ≤ P ≤ 1024 the number of words in a page. +//! - [Q](format::Format::virt_page_size) = P - 2 the number of words in a virtual page. +//! - [M](format::Format::max_prefix_len) = min(Q - 1, 256) the maximum length in words of a +//! value. +//! - [W](format::Format::window_size) = (N - 1) × Q - M the window size. +//! - [V](format::Format::virt_size) = (N - 1) × (Q - 1) - M the virtual capacity. +//! - [C](format::Format::total_capacity) = V - N the user capacity. //! -//! We build a virtual storage from the physical storage using the first 2 words of -//! each page: +//! We build a virtual storage from the physical storage using the first 2 words of each page: //! - The first word contains the number of times the page has been erased. -//! - The second word contains the starting word to which this page is being moved -//! during compaction. +//! - The second word contains the starting word to which this page is being moved during +//! compaction. //! -//! The virtual storage has a length of `(E + 1) * N * Q` words and represents the -//! lifetime of the store. (We reserve the last `Q + M` words to support adding -//! emergency lifetime.) This virtual storage has a linear address space. +//! The virtual storage has a length of (E + 1) × N × Q words and represents the lifetime of the +//! store. (We reserve the last Q + M words to support adding emergency lifetime.) This virtual +//! storage has a linear address space. //! -//! We define a set of overlapping windows of `N * Q` words at each `Q`-aligned -//! boundary. We call `i` the window spanning from `i * Q` to `(i + N) * Q`. Only -//! those windows actually exist in the underlying storage. We use compaction to -//! shift the current window from `i` to `i + 1`, preserving the content of the -//! store. +//! We define a set of overlapping windows of N × Q words at each Q-aligned boundary. We call i the +//! window spanning from i × Q to (i + N) × Q. Only those windows actually exist in the underlying +//! storage. We use compaction to shift the current window from i to i + 1, preserving the content +//! of the store. //! -//! For a given state of the virtual storage, we define `h_i` as the position of the -//! first entry of the window `i`. We call it the head of the window `i`. Because -//! entries are at most `M + 1` words, they can overlap on the next page only by `M` -//! words. So we have `i * Q <= h_i <= i * Q + M` . Since there are no entries -//! before the first page, we have `h_0 = 0`. +//! For a given state of the virtual storage, we define h\_i as the position of the first entry of +//! the window i. We call it the head of the window i. Because entries are at most M + 1 words, they +//! can overlap on the next page only by M words. So we have i × Q ≤ h_i ≤ i × Q + M . Since there +//! are no entries before the first page, we have h\_0 = 0. //! -//! We define `t_i` as one past the last entry of the window `i`. If there are no -//! entries in that window, we have `t_i = h_i`. We call `t_i` the tail of the -//! window `i`. We define the compaction invariant as `t_i - h_i <= V`. +//! We define t\_i as one past the last entry of the window i. If there are no entries in that +//! window, we have t\_i = h\_i. We call t\_i the tail of the window i. We define the compaction +//! invariant as t\_i - h\_i ≤ V and the window invariant as t\_i - h\_i ≤ W. The compaction +//! invariant may temporarily be broken during a sequence of (at most N - 1) compactions. //! -//! We define `|x|` as the capacity used before position `x`. We have `|x| <= x`. We -//! define the capacity invariant as `|t_i| - |h_i| <= C`. +//! We define |x| as the capacity used before position x. We have |x| ≤ x. We define the capacity +//! invariant as |t\_i| - |h\_i| ≤ C. //! -//! Using this virtual storage, entries are appended to the tail as long as there is -//! both virtual capacity to preserve the compaction invariant and capacity to -//! preserve the capacity invariant. When virtual capacity runs out, the first page -//! of the window is compacted and the window is shifted. +//! Using this virtual storage, entries are appended to the tail as long as there is both virtual +//! capacity to preserve the compaction invariant and capacity to preserve the capacity invariant. +//! When virtual capacity runs out, the first page of the window is compacted and the window is +//! shifted. //! -//! Entries are identified by a prefix of bits. The prefix has to contain at least -//! one bit set to zero to differentiate from the tail. Entries can be one of: -//! - Padding: A word whose first bit is set to zero. The rest is arbitrary. This -//! entry is used to mark words partially written after an interrupted operation -//! as padding such that they are ignored by future operations. -//! - Header: A word whose second bit is set to zero. It contains the following fields: -//! - A bit indicating whether the entry is deleted. -//! - A bit indicating whether the value is word-aligned and has all bits set -//! to 1 in its last word. The last word of an entry is used to detect that -//! an entry has been fully written. As such it must contain at least one -//! bit equal to zero. -//! - The key of the entry. -//! - The length in bytes of the value. The value follows the header. The -//! entry is word-aligned if the value is not. -//! - The checksum of the first and last word of the entry. -//! - Erase: A word used during compaction. It contains the page to be erased and -//! a checksum. -//! - Clear: A word used during the `Clear` operation. It contains the threshold -//! and a checksum. -//! - Marker: A word used during the `Transaction` operation. It contains the -//! number of updates following the marker and a checksum. -//! - Remove: A word used during the `Transaction` operation. It contains the key -//! of the entry to be removed and a checksum. +//! Entries are identified by a prefix of bits. The prefix has to contain at least one bit set to +//! zero to differentiate from the tail. Entries can be one of: +//! - [Padding](format::ID_PADDING): A word whose first bit is set to zero. The rest is arbitrary. +//! This entry is used to mark words partially written after an interrupted operation as padding +//! such that they are ignored by future operations. +//! - [Header](format::ID_HEADER): A word whose second bit is set to zero. It contains the +//! following fields: +//! - A [bit](format::HEADER_DELETED) indicating whether the entry is deleted. +//! - A [bit](format::HEADER_FLIPPED) indicating whether the value is word-aligned and has all +//! bits set to 1 in its last word. The last word of an entry is used to detect that an +//! entry has been fully written. As such it must contain at least one bit equal to zero. +//! - The [key](format::HEADER_KEY) of the entry. +//! - The [length](format::HEADER_LENGTH) in bytes of the value. The value follows the header. +//! The entry is word-aligned if the value is not. +//! - The [checksum](format::HEADER_CHECKSUM) of the first and last word of the entry. +//! - [Erase](format::ID_ERASE): A word used during compaction. It contains the +//! [page](format::ERASE_PAGE) to be erased and a [checksum](format::WORD_CHECKSUM). +//! - [Clear](format::ID_CLEAR): A word used during the clear operation. It contains the +//! [threshold](format::CLEAR_MIN_KEY) and a [checksum](format::WORD_CHECKSUM). +//! - [Marker](format::ID_MARKER): A word used during a transaction. It contains the [number of +//! updates](format::MARKER_COUNT) following the marker and a [checksum](format::WORD_CHECKSUM). +//! - [Remove](format::ID_REMOVE): A word used inside a transaction. It contains the +//! [key](format::REMOVE_KEY) of the entry to be removed and a +//! [checksum](format::WORD_CHECKSUM). //! //! Checksums are the number of bits equal to 0. //! @@ -204,107 +206,125 @@ //! //! ## Compaction //! -//! It should always be possible to fully compact the store, after what the -//! remaining capacity should be available in the current window (restoring the -//! compaction invariant). We consider all notations on the virtual storage after -//! the full compaction. We will use the `|x|` notation although we update the state -//! of the virtual storage. This is fine because compaction doesn't change the -//! status of an existing word. +//! Let I be a window at which all invariants hold. We will show that the next N - 1 compactions +//! will preserve the window invariant (the capacity invariant is trivially preserved) after each +//! compaction. We will also show that after N - 1 compactions, the compaction invariant is +//! restored. //! -//! We want to show that the next `N - 1` compactions won't move the tail past the -//! last page of their window, with `I` the initial window: +//! We consider all notations on the virtual storage after the full compaction. We will use the |x| +//! notation although we update the state of the virtual storage. This is fine because compaction +//! doesn't change the status of an existing word. +//! +//! We first show that after each compaction, the window invariant is preserved. //! //! ```text -//! forall 1 <= i <= N - 1, t_{I + i} <= (I + i + N - 1) * Q +//! ∀(1 ≤ i ≤ N - 1) t_{I + i} - h_{I + i} ≤ W //! ``` //! -//! We assume `i` between `1` and `N - 1`. +//! We assume i between 1 and N - 1. //! -//! One step of compaction advances the tail by how many words were used in the -//! first page of the window with the last entry possibly overlapping on the next -//! page. +//! One step of compaction advances the tail by how many words were used in the first page of the +//! window with the last entry possibly overlapping on the next page. //! //! ```text -//! forall j, t_{j + 1} = t_j + |h_{j + 1}| - |h_j| + 1 +//! ∀j t_{j + 1} = t_j + |h_{j + 1}| - |h_j| + 1 //! ``` //! //! By induction, we have: //! //! ```text -//! t_{I + i} <= t_I + |h_{I + i}| - |h_I| + i +//! t_{I + i} = t_I + |h_{I + i}| - |h_I| + i //! ``` //! //! We have the following properties: //! //! ```text -//! t_I <= h_I + V -//! |h_{I + i}| - |h_I| <= h_{I + i} - h_I -//! h_{I + i} <= (I + i) * Q + M +//! t_I ≤ h_I + V +//! |h_{I + i}| - |h_I| ≤ h_{I + i} - h_I //! ``` //! //! Replacing into our previous equality, we can conclude: //! //! ```text -//! t_{I + i} = t_I + |h_{I + i}| - |h_I| + i -//! <= h_I + V + (I + i) * Q + M - h_I + i -//! = (N - 1) * (Q - 1) - M + (I + i) * Q + M + i -//! = (N - 1) * (Q - 1) + (I + i) * Q + i -//! = (I + i + N - 1) * Q + i - (N - 1) -//! <= (I + i + N - 1) * Q +//! t_{I + i} = t_I + |h_{I + i}| - |h_I| + i +//! ≤ h_I + V + h_{I + 1} - h_I + i +//! iff +//! t_{I + i} - h_{I + 1} ≤ V + i +//! ≤ V + N - 1 +//! = W //! ``` //! -//! We also want to show that after `N - 1` compactions, the remaining capacity is -//! available without compaction. +//! An important corollary is that the tail stays within the window: //! //! ```text -//! V - (t_{I + N - 1} - h_{I + N - 1}) >= // The available words in the window. -//! C - (|t_{I + N - 1}| - |h_{I + N - 1}|) // The remaining capacity. -//! + 1 // Reserved for Clear. +//! t_{I + i} ≤ (I + i + N - 1) × Q //! ``` //! -//! We can replace the definition of `C` and simplify: +//! We have the following property: //! //! ```text -//! V - (t_{I + N - 1} - h_{I + N - 1}) >= V - N - (|t_{I + N - 1}| - |h_{I + N - 1}|) + 1 -//! iff t_{I + N - 1} - h_{I + N - 1} <= |t_{I + N - 1}| - |h_{I + N - 1}| + N - 1 +//! h_{I + i} ≤ (I + i) × Q + M +//! ``` +//! +//! From which we conclude with the definition of W: +//! +//! ```text +//! t_{I + i} ≤ h_{I + i} + W +//! ≤ (I + i) × Q + M + (N - 1) × Q - M +//! = (I + i + N - 1) × Q +//! ``` +//! +//! We finally show that after N - 1 compactions, the compaction invariant is restored. In +//! particular, the remaining capacity is available without compaction. +//! +//! ```text +//! V - (t_{I + N - 1} - h_{I + N - 1}) ≥ C - (|t_{I + N - 1}| - |h_{I + N - 1}|) + 1 +//! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ +//! immediate capacity remaining capacity | +//! reserved for clear +//! ``` +//! +//! We can replace the definition of C and simplify: +//! +//! ```text +//! V - (t_{I + N - 1} - h_{I + N - 1}) ≥ V - N - (|t_{I + N - 1}| - |h_{I + N - 1}|) + 1 +//! iff t_{I + N - 1} - h_{I + N - 1} ≤ |t_{I + N - 1}| - |h_{I + N - 1}| + N - 1 //! ``` //! //! We have the following properties: //! //! ```text -//! t_{I + N - 1} = t_I + |h_{I + N - 1}| - |h_I| + N - 1 -//! |t_{I + N - 1}| - |h_{I + N - 1}| = |t_I| - |h_I| // Compaction preserves capacity. -//! |h_{I + N - 1}| - |t_I| <= h_{I + N - 1} - t_I +//! t_{I + N - 1} = t_I + |h_{I + N - 1}| - |h_I| + N - 1 +//! |t_{I + N - 1}| - |h_{I + N - 1}| = |t_I| - |h_I| +//! |h_{I + N - 1}| - |t_I| ≤ h_{I + N - 1} - t_I //! ``` //! //! From which we conclude: //! //! ```text -//! t_{I + N - 1} - h_{I + N - 1} <= |t_{I + N - 1}| - |h_{I + N - 1}| + N - 1 -//! iff t_I + |h_{I + N - 1}| - |h_I| + N - 1 - h_{I + N - 1} <= |t_I| - |h_I| + N - 1 -//! iff t_I + |h_{I + N - 1}| - h_{I + N - 1} <= |t_I| -//! iff |h_{I + N - 1}| - |t_I| <= h_{I + N - 1} - t_I +//! t_{I + N - 1} - h_{I + N - 1} ≤ |t_{I + N - 1}| - |h_{I + N - 1}| + N - 1 +//! iff t_I + |h_{I + N - 1}| - |h_I| + N - 1 - h_{I + N - 1} ≤ |t_I| - |h_I| + N - 1 +//! iff t_I + |h_{I + N - 1}| - h_{I + N - 1} ≤ |t_I| +//! iff |h_{I + N - 1}| - |t_I| ≤ h_{I + N - 1} - t_I //! ``` //! -//! //! ## Checksum //! -//! The main property we want is that all partially written/erased words are either -//! the initial word, the final word, or invalid. +//! The main property we want is that all partially written/erased words are either the initial +//! word, the final word, or invalid. //! -//! We say that a bit sequence `TARGET` is reachable from a bit sequence `SOURCE` if -//! both have the same length and `SOURCE & TARGET == TARGET` where `&` is the -//! bitwise AND operation on bit sequences of that length. In other words, when -//! `SOURCE` has a bit equal to 0 then `TARGET` also has that bit equal to 0. +//! We say that a bit sequence `TARGET` is reachable from a bit sequence `SOURCE` if both have the +//! same length and `SOURCE & TARGET == TARGET` where `&` is the bitwise AND operation on bit +//! sequences of that length. In other words, when `SOURCE` has a bit equal to 0 then `TARGET` also +//! has that bit equal to 0. //! -//! The only written entries start with `101` or `110` and are written from an -//! erased word. Marking an entry as padding or deleted is a single bit operation, -//! so the property trivially holds. For those cases, the proof relies on the fact -//! that there is exactly one bit equal to 0 in the 3 first bits. Either the 3 first -//! bits are still `111` in which case we expect the remaining bits to be equal -//! to 1. Otherwise we can use the checksum of the given type of entry because those -//! 2 types of entries are not reachable from each other. Here is a visualization of -//! the partitioning based on the first 3 bits: +//! The only written entries start with `101` or `110` and are written from an erased word. Marking +//! an entry as padding or deleted is a single bit operation, so the property trivially holds. For +//! those cases, the proof relies on the fact that there is exactly one bit equal to 0 in the 3 +//! first bits. Either the 3 first bits are still `111` in which case we expect the remaining bits +//! to be equal to 1. Otherwise we can use the checksum of the given type of entry because those 2 +//! types of entries are not reachable from each other. Here is a visualization of the partitioning +//! based on the first 3 bits: //! //! | First 3 bits | Description | How to check | //! | ------------:| ------------------ | ---------------------------- | @@ -314,47 +334,49 @@ //! | `100` | Deleted user entry | No check, atomically written | //! | `0??` | Padding entry | No check, atomically written | //! -//! To show that valid entries of a given type are not reachable from each other, we -//! show 3 lemmas: +//! To show that valid entries of a given type are not reachable from each other, we show 3 lemmas: //! -//! 1. A bit sequence is not reachable from another if its number of bits equal to -//! 0 is smaller. +//! 1. A bit sequence is not reachable from another if its number of bits equal to 0 is smaller. +//! 2. A bit sequence is not reachable from another if they have the same number of bits equals to +//! 0 and are different. +//! 3. A bit sequence is not reachable from another if it is bigger when they are interpreted as +//! numbers in binary representation. //! -//! 2. A bit sequence is not reachable from another if they have the same number of -//! bits equals to 0 and are different. -//! -//! 3. A bit sequence is not reachable from another if it is bigger when they are -//! interpreted as numbers in binary representation. -//! -//! From those lemmas we consider the 2 cases. If both entries have the same number -//! of bits equal to 0, they are either equal or not reachable from each other -//! because of the second lemma. If they don't have the same number of bits equal to -//! 0, then the one with less bits equal to 0 is not reachable from the other -//! because of the first lemma and the one with more bits equal to 0 is not -//! reachable from the other because of the third lemma and the definition of the -//! checksum. +//! From those lemmas we consider the 2 cases. If both entries have the same number of bits equal to +//! 0, they are either equal or not reachable from each other because of the second lemma. If they +//! don't have the same number of bits equal to 0, then the one with less bits equal to 0 is not +//! reachable from the other because of the first lemma and the one with more bits equal to 0 is not +//! reachable from the other because of the third lemma and the definition of the checksum. //! //! # Fuzzing //! -//! For any sequence of operations and interruptions starting from an erased -//! storage, the store is checked against its model and some internal invariant at -//! each step. +//! For any sequence of operations and interruptions starting from an erased storage, the store is +//! checked against its model and some internal invariant at each step. //! -//! For any sequence of operations and interruptions starting from an arbitrary -//! storage, the store is checked not to crash. +//! For any sequence of operations and interruptions starting from an arbitrary storage, the store +//! is checked not to crash. -#![cfg_attr(not(feature = "std"), no_std)] +// #![cfg_attr(not(feature = "std"), no_std)] +#[macro_use] +extern crate alloc; #[cfg(feature = "std")] mod buffer; +pub mod concat; #[cfg(feature = "std")] mod driver; -mod format; +#[cfg(feature = "std")] +mod file; +// mod format; +pub mod fragment; +mod linear; #[cfg(feature = "std")] mod model; mod storage; -mod store; +pub mod store; +#[cfg(test)] +mod test; #[cfg(feature = "std")] pub use self::buffer::{BufferCorruptFunction, BufferOptions, BufferStorage}; @@ -363,6 +385,9 @@ pub use self::driver::{ StoreDriver, StoreDriverOff, StoreDriverOn, StoreInterruption, StoreInvariant, }; #[cfg(feature = "std")] +pub use self::file::{FileOptions, FileStorage}; +pub use self::linear::Linear; +#[cfg(feature = "std")] pub use self::model::{StoreModel, StoreOperation}; pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult}; pub use self::store::{ @@ -379,6 +404,10 @@ pub use self::store::{ /// with Rust, `usize` is used instead of `Nat` in code meant only for tests. /// /// Currently, the store only supports targets with `usize = u32`. +// Make sure production builds have `usize = 32`. +#[cfg(any(target_pointer_width = "32", feature = "std"))] +type Nat = u32; +#[cfg(any(target_pointer_width = "64", feature = "std"))] type Nat = u32; /// Returns the internal representation of a Rust natural number. diff --git a/apps/vault/libraries/persistent_store/src/linear.rs b/apps/vault/libraries/persistent_store/src/linear.rs new file mode 100644 index 000000000..5e432f6c2 --- /dev/null +++ b/apps/vault/libraries/persistent_store/src/linear.rs @@ -0,0 +1,373 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Provides a linear view into a storage. + +use core::ops::Range; + +use std::borrow::Cow; +use std::vec::Vec; + +use crate::{Storage, StorageError, StorageIndex, StorageResult}; + +/// Provides a linear view into a storage. +/// +/// The storage may be read and written as if it were a contiguous slice. This implementation is not +/// power-loss resistant. +pub struct Linear { + pub storage: S, +} + +impl Linear { + /// Returns the length of the storage. + /// + /// In particular, offsets should be less than this value (or equal for the one-past-the-end + /// offset). + pub fn length(&self) -> usize { + self.storage.num_pages() * self.storage.page_size() + } + + /// Reads a slice from the storage (ignoring page boundaries). + /// + /// This function may allocate if the slice spans multiple pages (because they may not be + /// contiguous). However, a slice is returned if the range fits within a page. + pub fn read(&self, range: Range) -> StorageResult> { + if range.is_empty() { + return Ok(Cow::Borrowed(&[])); + } + let Range { start, end } = range; + if end < start { + return Err(StorageError::OutOfBounds); + } + let page_size = self.storage.page_size(); + let mut index = StorageIndex { + page: start / page_size, + byte: start % page_size, + }; + let mut length = end - start; + if end <= (index.page + 1) * page_size { + // The range fits in a page. + return self.storage.read_slice(index, length); + } + // The range doesn't fit in a page. + let mut result = Vec::with_capacity(length); + while length > 0 { + let slice_length = core::cmp::min(length, page_size - index.byte); + result.extend_from_slice(&self.storage.read_slice(index, slice_length)?); + index.page += 1; + index.byte = 0; + length -= slice_length; + } + Ok(Cow::Owned(result)) + } + + /// Writes a slice to the storage (ignoring page boundaries and regardless of previous value). + /// + /// This function will erase pages as needed (and restore the non-overwritten parts). As such it + /// may allocate and is not power-loss resistant. + pub fn write(&mut self, offset: usize, mut value: &[u8]) -> StorageResult<()> { + if self.length() < value.len() || self.length() - value.len() < offset { + return Err(StorageError::OutOfBounds); + } + let page_size = self.storage.page_size(); + let mut index = StorageIndex { + page: offset / page_size, + byte: offset % page_size, + }; + while !value.is_empty() { + let length = core::cmp::min(value.len(), page_size - index.byte); + self.write_within(index, &value[..length])?; + index.page += 1; + index.byte = 0; + value = &value[length..]; + } + Ok(()) + } + + /// Erases a slice from the storage (ignoring page boundaries and preserving previous value). + /// + /// This is equivalent to writing a slice of `0xff` bytes but doesn't need the slice as + /// argument. But the function still allocates to preserve previous values if needed and is thus + /// still not power-loss resistant. + pub fn erase(&mut self, mut range: Range) -> StorageResult<()> { + if range.end < range.start || self.length() < range.end { + return Err(StorageError::OutOfBounds); + } + let page_size = self.storage.page_size(); + let mut index = StorageIndex { + page: range.start / page_size, + byte: range.start % page_size, + }; + while !range.is_empty() { + let length = core::cmp::min(range.len(), page_size - index.byte); + self.erase_within(index, length)?; + index.page += 1; + index.byte = 0; + range.start += length; + } + Ok(()) + } + + /// Writes a slice fitting a page (regardless of previous value). + /// + /// This function will erase the page if needed and restore the non-overwritten part. This is + /// not power-loss resistant. + fn write_within(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()> { + self.erase_within(index, value.len())?; + self.write_unaligned(index, value) + } + + /// Writes a slice fitting a page (assuming erased but not necessarily word-aligned). + fn write_unaligned(&mut self, mut index: StorageIndex, mut value: &[u8]) -> StorageResult<()> { + let word_size = self.storage.word_size(); + // Align the beginning if needed. + let unaligned = index.byte % word_size; + if unaligned != 0 { + let len = core::cmp::min(value.len(), word_size - unaligned); + index.byte -= unaligned; + let mut word = self.storage.read_slice(index, word_size)?.into_owned(); + word[unaligned..unaligned + len].copy_from_slice(&value[..len]); + self.storage.write_slice(index, &word)?; + value = &value[len..]; + index.byte += word_size; + } + // Write as long as aligned. + let len = value.len() - (value.len() % word_size); + self.storage.write_slice(index, &value[..len])?; + value = &value[len..]; + index.byte += len; + // Write the unaligned end if needed. + if value.len() > 0 { + let mut word = self.storage.read_slice(index, word_size)?.into_owned(); + word[..value.len()].copy_from_slice(value); + self.storage.write_slice(index, &word)?; + } + Ok(()) + } + + /// Erases a slice fitting a page (regardless of previous value). + /// + /// This function will erase the page if needed and restore the non-overwritten part. This is + /// not power-loss resistant. + fn erase_within(&mut self, index: StorageIndex, length: usize) -> StorageResult<()> { + let previous_value = self.storage.read_slice(index, length)?; + if previous_value.iter().all(|&x| x == 0xff) { + // The slice is already erased, so nothing to do. + return Ok(()); + } + // We must erase the page, so we save the rest. + let complement = self.save_complement(index, length)?; + self.storage.erase_page(index.page)?; + self.restore_complement(index.page, complement) + } + + /// Saves the complement of a slice fitting a page. + fn save_complement(&self, index: StorageIndex, length: usize) -> StorageResult { + let page_size = self.storage.page_size(); + let prefix = self + .storage + .read_slice( + StorageIndex { + page: index.page, + byte: 0, + }, + index.byte, + )? + .into_owned(); + let suffix = self + .storage + .read_slice( + StorageIndex { + page: index.page, + byte: index.byte + length, + }, + page_size - index.byte - length, + )? + .into_owned(); + Ok(Complement { prefix, suffix }) + } + + /// Restores the complement of a slice fitting a page. + fn restore_complement(&mut self, page: usize, complement: Complement) -> StorageResult<()> { + let page_size = self.storage.page_size(); + let Complement { prefix, suffix } = complement; + self.write_unaligned(StorageIndex { page, byte: 0 }, &prefix)?; + self.write_unaligned( + StorageIndex { + page, + byte: page_size - suffix.len(), + }, + &suffix, + )?; + Ok(()) + } +} + +/// Represents the complement of a slice within a page. +struct Complement { + prefix: Vec, + suffix: Vec, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test::MINIMAL; + use crate::{BufferOptions, BufferStorage}; + + /// Creates a linear view into a fresh minimal buffer storage. + fn new_linear() -> Linear { + let mut options = BufferOptions::from(&MINIMAL); + options.strict_mode = false; + let storage = vec![0xff; MINIMAL.num_pages * MINIMAL.page_size].into_boxed_slice(); + let storage = BufferStorage::new(storage, options); + Linear { storage } + } + + /// Returns some interesting test ranges. + fn ranges(linear: &Linear) -> Vec> { + let storage_length = linear.length(); + let page_size = linear.storage.page_size(); + let count = 2 * linear.storage.word_size(); + let mut ranges = Vec::new(); + let convert = |page: usize, byte: usize, rev| { + if rev { + (page + 1) * page_size - byte + } else { + page * page_size + byte + } + }; + for &start_rev in [false, true].iter() { + for &length_rev in [false, true].iter() { + for start_page in 0..=2 { + for length_page in 0..2 { + for start_byte in 0..count { + for length_byte in 0..count { + let start = convert(start_page, start_byte, start_rev); + let length = convert(length_page, length_byte, length_rev); + ranges.push(start..start + length); + let end = storage_length - start; + let start = end - length; + ranges.push(start..end); + } + } + } + } + } + } + ranges + } + + #[test] + fn simple_usage() { + let mut linear = new_linear(); + linear.write(3, b"hello").unwrap(); + assert_eq!( + linear.read(0..10).unwrap(), + &b"\xff\xff\xffhello\xff\xff"[..] + ); + linear.write(5, b"y you").unwrap(); + assert_eq!(linear.read(0..10).unwrap(), &b"\xff\xff\xffhey you"[..]); + linear.erase(6..10).unwrap(); + assert_eq!( + linear.read(0..10).unwrap(), + &b"\xff\xff\xffhey\xff\xff\xff\xff"[..] + ); + } + + #[test] + fn round_trip() { + let mut linear = new_linear(); + let pattern: Vec = (0..255).collect(); + for range in ranges(&linear) { + // Check that writing and reading back gives the same value. + let value = &pattern[..range.len()]; + assert_eq!(linear.write(range.start, value), Ok(()), "{:?}", range); + match linear.read(range.clone()) { + Ok(actual) => assert_eq!(actual, value, "{:?}", range), + Err(error) => panic!("{:?} {:?}", error, range), + } + } + } + + #[test] + fn out_of_bound() { + let mut linear = new_linear(); + let length = linear.length(); + assert!(linear.read(length..length + 1).is_err()); + assert!(linear.read(length + 1..length + 5).is_err()); + assert!(linear.read(0..length + 1).is_err()); + assert!(linear.read(length - 10..length + 1).is_err()); + assert!(linear.write(length, &[0]).is_err()); + assert!(linear.write(length - 10, &[0; 11]).is_err()); + } + + #[test] + fn erase_before() { + let mut linear = new_linear(); + let pattern: Vec = (0..255).collect(); + for mut range in ranges(&linear) { + if range.is_empty() { + continue; + } + let value = &pattern[..range.len()]; + // We write the pattern. + assert_eq!(linear.write(range.start, value), Ok(()), "{:?}", range); + // We erase the pattern except for the last byte. + range.end -= 1; + assert_eq!(linear.erase(range.clone()), Ok(()), "{:?}", range); + // We check that the pattern was erased except for the last byte. + match linear.read(range.clone()) { + Ok(actual) => assert_eq!(actual, vec![0xff; range.len()], "{:?}", range), + Err(error) => panic!("{:?} {:?}", error, range), + } + // We check that the last byte is still there. + range.start = range.end; + range.end += 1; + match linear.read(range.clone()) { + Ok(actual) => assert_eq!(actual, &value[value.len() - 1..], "{:?}", range), + Err(error) => panic!("{:?} {:?}", error, range), + } + } + } + + #[test] + fn erase_after() { + let mut linear = new_linear(); + let pattern: Vec = (0..255).collect(); + for mut range in ranges(&linear) { + if range.is_empty() { + continue; + } + let value = &pattern[..range.len()]; + // We write the pattern. + assert_eq!(linear.write(range.start, value), Ok(()), "{:?}", range); + // We erase the pattern except for the first byte. + range.start += 1; + assert_eq!(linear.erase(range.clone()), Ok(()), "{:?}", range); + // We check that the pattern was erased except for the first byte. + match linear.read(range.clone()) { + Ok(actual) => assert_eq!(actual, vec![0xff; range.len()], "{:?}", range), + Err(error) => panic!("{:?} {:?}", error, range), + } + // We check that the first byte is still there. + range.end = range.start; + range.start -= 1; + match linear.read(range.clone()) { + Ok(actual) => assert_eq!(actual, &value[..1], "{:?}", range), + Err(error) => panic!("{:?} {:?}", error, range), + } + } + } +} diff --git a/apps/vault/libraries/persistent_store/src/model.rs b/apps/vault/libraries/persistent_store/src/model.rs index c509b03cf..e0bf9e387 100644 --- a/apps/vault/libraries/persistent_store/src/model.rs +++ b/apps/vault/libraries/persistent_store/src/model.rs @@ -12,13 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Store specification. + use crate::format::Format; use crate::{usize_to_nat, StoreError, StoreRatio, StoreResult, StoreUpdate}; use std::collections::HashMap; /// Models the mutable operations of a store. /// -/// The model doesn't model the storage and read-only operations. This is done by the driver. +/// The model doesn't model the storage and read-only operations. This is done by the +/// [driver](crate::StoreDriver). #[derive(Clone, Debug)] pub struct StoreModel { /// Represents the content of the store. @@ -34,7 +37,7 @@ pub enum StoreOperation { /// Applies a transaction. Transaction { /// The list of updates to be applied. - updates: Vec, + updates: Vec>>, }, /// Deletes all keys above a threshold. @@ -89,7 +92,7 @@ impl StoreModel { } /// Applies a transaction. - fn transaction(&mut self, updates: Vec) -> StoreResult<()> { + fn transaction(&mut self, updates: Vec>>) -> StoreResult<()> { // Fail if the transaction is invalid. if self.format.transaction_valid(&updates).is_none() { return Err(StoreError::InvalidArgument); diff --git a/apps/vault/libraries/persistent_store/src/storage.rs b/apps/vault/libraries/persistent_store/src/storage.rs index becd9002b..e906013cc 100644 --- a/apps/vault/libraries/persistent_store/src/storage.rs +++ b/apps/vault/libraries/persistent_store/src/storage.rs @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Flash storage abstraction. + +use std::borrow::Cow; + /// Represents a byte position in a storage. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct StorageIndex { @@ -32,6 +36,13 @@ pub enum StorageError { CustomError, } +#[cfg(feature = "std")] +impl From for StorageError { + fn from(_: std::io::Error) -> Self { + Self::CustomError + } +} + pub type StorageResult = Result; /// Abstracts a flash storage. @@ -58,19 +69,24 @@ pub trait Storage { /// Reads a byte slice from the storage. /// /// The `index` must designate `length` bytes in the storage. - fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>; + /// + /// Note that we use `Cow` just because it derefs to `[u8]`. We don't really need the fact that + /// one can convert it to a `Vec`. In particular we don't do it in the store implementation. + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult>; /// Writes a word slice to the storage. /// /// The following pre-conditions must hold: /// - The `index` must designate `value.len()` bytes in the storage. /// - Both `index` and `value.len()` must be word-aligned. - /// - The written words should not have been written too many times since last page erasure. + /// - The written words should not have been written [too many](Self::max_word_writes) times + /// since the last page erasure. fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>; /// Erases a page of the storage. /// - /// The `page` must be in the storage. + /// The `page` must be in the storage, i.e. less than [`Storage::num_pages`]. And the page + /// should not have been erased [too many](Self::max_page_erases) times. fn erase_page(&mut self, page: usize) -> StorageResult<()>; } diff --git a/apps/vault/libraries/persistent_store/src/store.rs b/apps/vault/libraries/persistent_store/src/store.rs index 7b56aff65..25dbae758 100644 --- a/apps/vault/libraries/persistent_store/src/store.rs +++ b/apps/vault/libraries/persistent_store/src/store.rs @@ -12,21 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::format::{ - is_erased, CompactInfo, Format, Header, InitInfo, InternalEntry, Padding, ParsedWord, Position, - Word, WordState, -}; +//! Store implementation. +pub const OPENSK2_DICT: &'static str = "opensk"; + #[cfg(feature = "std")] pub use crate::model::{StoreModel, StoreOperation}; -use crate::{usize_to_nat, Nat, Storage, StorageError, StorageIndex}; +use crate::{usize_to_nat, Nat, Storage, StorageError}; #[cfg(feature = "std")] pub use crate::{ BufferStorage, StoreDriver, StoreDriverOff, StoreDriverOn, StoreInterruption, StoreInvariant, }; -use std::vec::Vec; -use core::cmp::{max, min, Ordering}; -#[cfg(feature = "std")] -use std::collections::HashSet; +use core::borrow::Borrow; +use std::io::{Write, Read}; /// Errors returned by store operations. #[derive(Debug, PartialEq, Eq)] @@ -51,17 +48,14 @@ pub enum StoreError { /// /// The consequences depend on the storage failure. In particular, the operation may or may not /// have succeeded, and the storage may have become invalid. Before doing any other operation, - /// the store should be [recovered]. The operation may then be retried if idempotent. - /// - /// [recovered]: struct.Store.html#method.recover + /// the store should be [recovered](Store::recover). The operation may then be retried if + /// idempotent. StorageError, /// Storage is invalid. /// - /// The storage should be erased and the store [recovered]. The store would be empty and have - /// lost track of lifetime. - /// - /// [recovered]: struct.Store.html#method.recover + /// The storage should be erased and the store [recovered](Store::recover). The store would be + /// empty and have lost track of lifetime. InvalidStorage, } @@ -78,17 +72,22 @@ impl From for StoreError { /// Result of store operations. pub type StoreResult = Result; +/// Converts an Option into a StoreResult. +/// +/// The None case is considered invalid and returns [`StoreError::InvalidStorage`]. +fn or_invalid(x: Option) -> StoreResult { + x.ok_or(StoreError::InvalidStorage) +} + /// Progression ratio for store metrics. /// -/// This is used for the [capacity] and [lifetime] metrics. Those metrics are measured in words. +/// This is used for the [`Store::capacity`] and [`Store::lifetime`] metrics. Those metrics are +/// measured in words. /// /// # Invariant /// -/// - The used value does not exceed the total: `used <= total`. -/// -/// [capacity]: struct.Store.html#method.capacity -/// [lifetime]: struct.Store.html#method.lifetime -#[derive(Copy, Clone, PartialEq, Eq)] +/// - The used value does not exceed the total: `used` ≤ `total`. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct StoreRatio { /// How much of the metric is used. pub(crate) used: Nat, @@ -121,26 +120,29 @@ impl StoreRatio { #[derive(Clone, Debug)] pub struct StoreHandle { /// The key of the entry. - key: Nat, - - /// The position of the entry. - pos: Position, - - /// The length in bytes of the value. - len: Nat, + key: usize, } impl StoreHandle { /// Returns the key of the entry. pub fn get_key(&self) -> usize { - self.key as usize + self.key + } + + /// Returns the value length of the entry. + /// + /// # Errors + /// + /// Returns [`StoreError::InvalidArgument`] if the entry has been deleted or compacted. + pub fn get_length(&self, store: &Store) -> StoreResult { + store.get_length(self) } /// Returns the value of the entry. /// /// # Errors /// - /// Returns `InvalidArgument` if the entry has been deleted or compacted. + /// Returns [`StoreError::InvalidArgument`] if the entry has been deleted or compacted. pub fn get_value(&self, store: &Store) -> StoreResult> { store.get_value(self) } @@ -148,15 +150,15 @@ impl StoreHandle { /// Represents an update to the store as part of a transaction. #[derive(Clone, Debug)] -pub enum StoreUpdate { +pub enum StoreUpdate> { /// Inserts or replaces an entry in the store. - Insert { key: usize, value: Vec }, + Insert { key: usize, value: ByteSlice }, /// Removes an entry from the store. Remove { key: usize }, } -impl StoreUpdate { +impl> StoreUpdate { /// Returns the key affected by the update. pub fn key(&self) -> usize { match *self { @@ -168,20 +170,28 @@ impl StoreUpdate { /// Returns the value written by the update. pub fn value(&self) -> Option<&[u8]> { match self { - StoreUpdate::Insert { value, .. } => Some(value), + StoreUpdate::Insert { value, .. } => Some(value.borrow()), StoreUpdate::Remove { .. } => None, } } } -/// Implements a store with a map interface over a storage. -#[derive(Clone)] -pub struct Store { - /// The underlying storage. - storage: S, +pub type StoreIter<'a> = Box> + 'a>; - /// The storage configuration. - format: Format, +/// Implements a store with a map interface over a storage. +pub struct Store { + storage: S, + pddb: pddb::Pddb, + entries: Option>, +} +impl Clone for Store { + fn clone(&self) -> Self { + Store { + storage: self.storage.clone(), + entries: self.pddb.list_keys(crate::store::OPENSK2_DICT, None).ok(), + pddb: pddb::Pddb::new(), + } + } } impl Store { @@ -193,146 +203,154 @@ impl Store { /// /// # Errors /// - /// Returns `InvalidArgument` if the storage is not supported. + /// Returns [`StoreError::InvalidArgument`] if the storage is not + /// [supported](Format::is_storage_supported). pub fn new(storage: S) -> Result, (StoreError, S)> { - let format = match Format::new(&storage) { - None => return Err((StoreError::InvalidArgument, storage)), - Some(x) => x, - }; - let mut store = Store { storage, format }; - if let Err(error) = store.recover() { - return Err((error, store.storage)); - } - Ok(store) + let pddb = pddb::Pddb::new(); + Ok(Store { + storage, + entries: pddb.list_keys(crate::store::OPENSK2_DICT, None).ok(), + pddb, + }) + } + + /// Extracts the storage. + pub fn extract_storage(self) -> S { + self.storage } /// Iterates over the entries. - pub fn iter<'a>(&'a self) -> StoreResult> { - StoreIter::new(self) + pub fn iter<'a>(&'a self) -> StoreResult> { + Ok(Box::new( + or_invalid(self.entries.as_ref())? + .iter() + .map( + move |key_name| { + Ok( + StoreHandle { + key: usize::from_str_radix(key_name, 10) + .map_err(|_| StoreError::InvalidStorage)? + } + ) + } + ) + ) + ) } - /// Returns the current capacity in words. + /// Returns the current and total capacity in words. /// /// The capacity represents the size of what is stored. pub fn capacity(&self) -> StoreResult { - let total = self.format.total_capacity(); - let mut used = 0; - let mut pos = self.head()?; - let end = pos + self.format.virt_size(); - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Tail => break, - ParsedEntry::Padding => (), - ParsedEntry::User(_) => used += pos - entry_pos, - _ => return Err(StoreError::InvalidStorage), - } - } - Ok(StoreRatio { used, total }) + // The PDDB deliberately does not know its free space capacity, + // so we return some bogus hard-coded numbers. + Ok(StoreRatio { used: 16384, total: 1024 * 1024 * 96 }) } - /// Returns the current lifetime in words. + /// Returns the current and total lifetime in words. /// /// The lifetime represents the age of the storage. The limit is an over-approximation by at /// most the maximum length of a value (the actual limit depends on the length of the prefix of /// the first physical page once all its erase cycles have been used). pub fn lifetime(&self) -> StoreResult { - let total = self.format.total_lifetime().get(); - let used = self.tail()?.get(); - Ok(StoreRatio { used, total }) + Ok(StoreRatio { used: 16384, total: 1024 * 1024 * 96 }) } /// Applies a sequence of updates as a single transaction. /// /// # Errors /// - /// Returns `InvalidArgument` in the following circumstances: - /// - There are too many updates. + /// Returns [`StoreError::InvalidArgument`] in the following circumstances: + /// - There are [too many](Format::max_updates) updates. /// - The updates overlap, i.e. their keys are not disjoint. - /// - The updates are invalid, e.g. key out of bound or value too long. - pub fn transaction(&mut self, updates: &[StoreUpdate]) -> StoreResult<()> { + /// - The updates are invalid, e.g. key [out of bound](Format::max_key) or value [too + /// long](Format::max_value_len). + pub fn transaction>( + &mut self, + updates: &[StoreUpdate], + ) -> StoreResult<()> { let count = usize_to_nat(updates.len()); if count == 0 { return Ok(()); } - if count == 1 { - match updates[0] { - StoreUpdate::Insert { key, ref value } => return self.insert(key, value), - StoreUpdate::Remove { key } => return self.remove(key), - } - } - // Get the sorted keys. Fail if the transaction is invalid. - let sorted_keys = match self.format.transaction_valid(updates) { - None => return Err(StoreError::InvalidArgument), - Some(x) => x, - }; - // Reserve the capacity. - self.reserve(self.format.transaction_capacity(updates))?; - // Write the marker entry. - let marker = self.tail()?; - let entry = self - .format - .build_internal(InternalEntry::Marker { count })?; - self.write_slice(marker, &entry)?; - self.init_page(marker, marker)?; - // Write the updates. - let mut tail = marker + 1; + for update in updates { - let length = match *update { + match *update { StoreUpdate::Insert { key, ref value } => { - let entry = self.format.build_user(usize_to_nat(key), value)?; - let word_size = self.format.word_size(); - let footer = usize_to_nat(entry.len()) / word_size - 1; - self.write_slice(tail, &entry[..(footer * word_size) as usize])?; - self.write_slice(tail + footer, &entry[(footer * word_size) as usize..])?; - footer + // if it exists, remove the key; if it doesn't exist, ignore the error + log::debug!("delete key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); + self.pddb.delete_key( + crate::store::OPENSK2_DICT, + &key.to_string(), + None + ).ok(); + log::debug!("access key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); + match self.pddb.get( + crate::store::OPENSK2_DICT, + &key.to_string(), + None, + true, true, None, // for some reason, we can't call .len() on ByteSlice?? + None:: + ) { + Ok(mut record) => { + match record.write_all(value.borrow().into()) { + Ok(_) => {} + Err(e) => { + log::error!("Error {:?} writing {}:{}", e, crate::store::OPENSK2_DICT, key.to_string()); + return Err(StoreError::StorageError); + } + } + } + Err(e) => { + log::error!("Storage error in transaction() processing key {}: {:?}", key, e); + return Err(StoreError::StorageError); + } + } } StoreUpdate::Remove { key } => { - let key = usize_to_nat(key); - let remove = self.format.build_internal(InternalEntry::Remove { key })?; - self.write_slice(tail, &remove)?; - 0 + self.pddb.delete_key( + crate::store::OPENSK2_DICT, + &key.to_string(), + None + ).map_err(|_|StoreError::StorageError)? } - }; - self.init_page(tail, tail + length)?; - tail += 1 + length; + } } - // Apply the transaction. - self.transaction_apply(&sorted_keys, marker) + self.entries = self.pddb.list_keys(crate::store::OPENSK2_DICT, None).ok(); + Ok(()) } /// Removes multiple entries as part of a single transaction. /// /// Entries with a key larger or equal to `min_key` are deleted. pub fn clear(&mut self, min_key: usize) -> StoreResult<()> { - let min_key = usize_to_nat(min_key); - if min_key > self.format.max_key() { - return Err(StoreError::InvalidArgument); + let keys = self.pddb.list_keys(crate::store::OPENSK2_DICT, None) + .map_err(|_| StoreError::StorageError)?; + for key in keys { + if let Ok(key_as_usize) = usize::from_str_radix(&key, 10) { + if key_as_usize >= min_key { + self.pddb.delete_key(crate::store::OPENSK2_DICT, + &key, + None + ).map_err(|_| StoreError::StorageError)?; + } + } else { + log::error!("Internal coding error: all keys in {} should be numeric. Got: {}", + crate::store::OPENSK2_DICT, + &key + ); + return Err(StoreError::InvalidArgument); + } } - let clear = self - .format - .build_internal(InternalEntry::Clear { min_key })?; - // We always have one word available. We can't use `reserve` because this is internal - // capacity, not user capacity. - while self.immediate_capacity()? < 1 { - self.compact()?; - } - let tail = self.tail()?; - self.write_slice(tail, &clear)?; - self.clear_delete(tail) + self.entries = self.pddb.list_keys(crate::store::OPENSK2_DICT, None).ok(); + Ok(()) } /// Compacts the store once if needed. /// /// If the immediate capacity is at least `length` words, then nothing is modified. Otherwise, /// one page is compacted. - pub fn prepare(&mut self, length: usize) -> Result<(), StoreError> { - if self.capacity()?.remaining() < length { - return Err(StoreError::NoCapacity); - } - if self.immediate_capacity()? < usize_to_nat(length) { - self.compact()?; - } + pub fn prepare(&mut self, _length: usize) -> Result<(), StoreError> { Ok(()) } @@ -340,703 +358,111 @@ impl Store { /// /// If the storage is completely erased, it is initialized. pub fn recover(&mut self) -> StoreResult<()> { - self.recover_initialize()?; - self.recover_erase()?; - self.recover_compaction()?; - self.recover_operation()?; Ok(()) } /// Returns the value of an entry given its key. pub fn find(&self, key: usize) -> StoreResult>> { - Ok(match self.find_handle(key)? { - None => None, - Some(handle) => Some(self.get_value(&handle)?), - }) + match self.pddb.get( + crate::store::OPENSK2_DICT, + &key.to_string(), + None, + false, false, None, None:: + ) { + Ok(mut record) => { + let mut data = Vec::::new(); + record.read_to_end(&mut data).map_err(|_| StoreError::StorageError)?; + Ok(Some(data)) + } + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => Ok(None), + _ => Err(StoreError::StorageError), + } + } } /// Returns a handle to an entry given its key. pub fn find_handle(&self, key: usize) -> StoreResult> { - let key = usize_to_nat(key); - for handle in self.iter()? { - let handle = handle?; - if handle.key == key { - return Ok(Some(handle)); + log::debug!("listing keys"); + let keys = match self.pddb.list_keys( + crate::store::OPENSK2_DICT, + None + ) { + Ok(k) => k, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => { + log::debug!("didn't find any keys, returning empty list"); + Vec::new() + }, + _ => return Err(StoreError::StorageError) } + }; + + if keys.contains(&key.to_string()) { + Ok(Some(StoreHandle {key})) + } else { + Ok(None) } - Ok(None) } /// Inserts an entry in the store. /// /// If an entry for the same key is already present, it is replaced. pub fn insert(&mut self, key: usize, value: &[u8]) -> StoreResult<()> { - // NOTE: This (and transaction) could take a position hint on the value to delete. - let key = usize_to_nat(key); - let value_len = usize_to_nat(value.len()); - if key > self.format.max_key() || value_len > self.format.max_value_len() { - return Err(StoreError::InvalidArgument); - } - let entry = self.format.build_user(key, value)?; - let entry_len = usize_to_nat(entry.len()); - self.reserve(entry_len / self.format.word_size())?; - let tail = self.tail()?; - let word_size = self.format.word_size(); - let footer = entry_len / word_size - 1; - self.write_slice(tail, &entry[..(footer * word_size) as usize])?; - self.write_slice(tail + footer, &entry[(footer * word_size) as usize..])?; - self.insert_init(tail, footer, key) + self.transaction(&[ + StoreUpdate::Insert { + key, + value + } + ]) } /// Removes an entry given its key. /// /// This is not an error if there is no entry for this key. pub fn remove(&mut self, key: usize) -> StoreResult<()> { - let key = usize_to_nat(key); - if key > self.format.max_key() { - return Err(StoreError::InvalidArgument); - } - self.delete_keys(&[key], self.tail()?) + self.transaction(&[ + StoreUpdate::<&[u8]>::Remove { key } + ]) } /// Removes an entry given a handle. pub fn remove_handle(&mut self, handle: &StoreHandle) -> StoreResult<()> { - self.check_handle(handle)?; - self.delete_pos(handle.pos, self.format.bytes_to_words(handle.len)) + self.transaction(&[ + StoreUpdate::<&[u8]>::Remove { + key: handle.key + } + ]) } /// Returns the maximum length in bytes of a value. pub fn max_value_length(&self) -> usize { - self.format.max_value_len() as usize + // an arbitrarily large limit; large keys can theoretically go to the entire size of + // the PDDB, but 10 MiB should be more than enough for FIDO2 + 10 * 1024 * 1024 + } + + fn get_length(&self, handle: &StoreHandle) -> StoreResult { + match self.pddb.get( + crate::store::OPENSK2_DICT, + &handle.key.to_string(), + None, + false, false, None, None:: + ) { + Ok(record) => { + let attr = record.attributes().map_err(|_| StoreError::StorageError)?; + Ok(attr.len) + } + _ => Err(StoreError::StorageError) + } } - /// Returns the value of an entry given its handle. fn get_value(&self, handle: &StoreHandle) -> StoreResult> { - self.check_handle(handle)?; - let mut pos = handle.pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::User(header) => { - let mut result = self.read_slice(handle.pos + 1, header.length); - if header.flipped { - let last_byte = result.len() - 1; - result[last_byte] = 0xff; - } - Ok(result) - } - ParsedEntry::Padding => Err(StoreError::InvalidArgument), - _ => Err(StoreError::InvalidStorage), - } + self.find( + handle.key + ).map(|val| val.ok_or(StoreError::StorageError))? } - /// Initializes the storage if completely erased or partially initialized. - fn recover_initialize(&mut self) -> StoreResult<()> { - let word_size = self.format.word_size(); - for page in 0..self.format.num_pages() { - let (init, rest) = self.read_page(page).split_at(word_size as usize); - if (page > 0 && !is_erased(init)) || !is_erased(rest) { - return Ok(()); - } - } - let index = self.format.index_init(0); - let init_info = self.format.build_init(InitInfo { - cycle: 0, - prefix: 0, - })?; - self.storage_write_slice(index, &init_info) - } - - /// Recovers a possible compaction interrupted while erasing the page. - fn recover_erase(&mut self) -> StoreResult<()> { - let mut pos = self.get_extremum_page_head(Ordering::Greater)?; - let end = pos.next_page(&self.format); - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Internal(InternalEntry::Erase { .. }) => { - return self.compact_erase(entry_pos) - } - ParsedEntry::Padding | ParsedEntry::User(_) => (), - _ => break, - } - } - Ok(()) - } - - /// Recovers a possible compaction interrupted while copying the entries. - fn recover_compaction(&mut self) -> StoreResult<()> { - let head_page = self.head()?.page(&self.format); - match self.parse_compact(head_page)? { - WordState::Erased => Ok(()), - WordState::Partial => self.compact(), - WordState::Valid(_) => self.compact_copy(), - } - } - - /// Recover a possible interrupted operation which is not a compaction. - fn recover_operation(&mut self) -> StoreResult<()> { - let mut pos = self.head()?; - let mut prev_pos = pos; - let end = pos + self.format.virt_size(); - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Tail => break, - ParsedEntry::User(_) => (), - ParsedEntry::Padding => { - self.wipe_span(entry_pos + 1, pos - entry_pos - 1)?; - } - ParsedEntry::Internal(InternalEntry::Erase { .. }) => { - return Err(StoreError::InvalidStorage); - } - ParsedEntry::Internal(InternalEntry::Clear { .. }) => { - return self.clear_delete(entry_pos); - } - ParsedEntry::Internal(InternalEntry::Marker { .. }) => { - return self.recover_transaction(entry_pos, end); - } - ParsedEntry::Internal(InternalEntry::Remove { .. }) => { - self.set_padding(entry_pos)?; - } - ParsedEntry::Partial => { - return self.recover_wipe_partial(entry_pos, pos - entry_pos - 1); - } - ParsedEntry::PartialUser => { - return self.recover_delete_user(entry_pos, pos - entry_pos - 1); - } - } - prev_pos = entry_pos; - } - pos = prev_pos; - if let ParsedEntry::User(header) = self.parse_entry(&mut pos)? { - self.insert_init(prev_pos, pos - prev_pos - 1, header.key)?; - } - Ok(()) - } - - /// Recovers a possible interrupted transaction. - fn recover_transaction(&mut self, marker: Position, end: Position) -> StoreResult<()> { - let mut pos = marker; - let count = match self.parse_entry(&mut pos)? { - ParsedEntry::Internal(InternalEntry::Marker { count }) => count, - _ => return Err(StoreError::InvalidStorage), - }; - let sorted_keys = self.recover_transaction_keys(count, pos, end)?; - match usize_to_nat(sorted_keys.len()).cmp(&count) { - Ordering::Less => (), - Ordering::Equal => return self.transaction_apply(&sorted_keys, marker), - Ordering::Greater => return Err(StoreError::InvalidStorage), - } - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Tail => break, - ParsedEntry::Padding => (), - ParsedEntry::User(_) => { - self.delete_pos(entry_pos, pos - entry_pos - 1)?; - } - ParsedEntry::Internal(InternalEntry::Remove { .. }) => { - self.set_padding(entry_pos)?; - } - ParsedEntry::Partial => { - self.recover_wipe_partial(entry_pos, pos - entry_pos - 1)?; - break; - } - ParsedEntry::PartialUser => { - self.recover_delete_user(entry_pos, pos - entry_pos - 1)?; - break; - } - ParsedEntry::Internal(InternalEntry::Erase { .. }) - | ParsedEntry::Internal(InternalEntry::Clear { .. }) - | ParsedEntry::Internal(InternalEntry::Marker { .. }) => { - return Err(StoreError::InvalidStorage); - } - } - } - self.init_page(marker, marker)?; - self.set_padding(marker)?; - Ok(()) - } - - /// Returns the domain of a possible interrupted transaction. - /// - /// The domain is returned as a sorted list of keys. - fn recover_transaction_keys( - &mut self, - count: Nat, - mut pos: Position, - end: Position, - ) -> StoreResult> { - let mut sorted_keys = Vec::with_capacity(count as usize); - let mut prev_pos = pos; - while pos < end { - let entry_pos = pos; - let key = match self.parse_entry(&mut pos)? { - ParsedEntry::Tail - | ParsedEntry::Padding - | ParsedEntry::Partial - | ParsedEntry::PartialUser => break, - ParsedEntry::User(header) => header.key, - ParsedEntry::Internal(InternalEntry::Remove { key }) => key, - ParsedEntry::Internal(_) => return Err(StoreError::InvalidStorage), - }; - match sorted_keys.binary_search(&key) { - Ok(_) => return Err(StoreError::InvalidStorage), - Err(pos) => sorted_keys.insert(pos, key), - } - prev_pos = entry_pos; - } - pos = prev_pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::User(_) | ParsedEntry::Internal(InternalEntry::Remove { .. }) => { - let length = pos - prev_pos - 1; - self.init_page(prev_pos, prev_pos + length)?; - } - _ => (), - } - Ok(sorted_keys) - } - - /// Completes a possible partial entry wipe. - fn recover_wipe_partial(&mut self, pos: Position, length: Nat) -> StoreResult<()> { - self.wipe_span(pos + 1, length)?; - self.init_page(pos, pos + length)?; - self.set_padding(pos)?; - Ok(()) - } - - /// Completes a possible partial entry deletion. - fn recover_delete_user(&mut self, pos: Position, length: Nat) -> StoreResult<()> { - self.init_page(pos, pos + length)?; - self.delete_pos(pos, length) - } - - /// Checks that a handle still points in the current window. - /// - /// In particular, the handle has not been compacted. - fn check_handle(&self, handle: &StoreHandle) -> StoreResult<()> { - if handle.pos < self.head()? { - Err(StoreError::InvalidArgument) - } else { - Ok(()) - } - } - - /// Compacts the store as needed. - /// - /// If there is at least `length` words of remaining capacity, pages are compacted until that - /// amount is immediately available. - fn reserve(&mut self, length: Nat) -> Result<(), StoreError> { - if self.capacity()?.remaining() < length as usize { - return Err(StoreError::NoCapacity); - } - while self.immediate_capacity()? < length { - self.compact()?; - } - Ok(()) - } - - /// Continues an entry insertion after it has been written. - fn insert_init(&mut self, pos: Position, length: Nat, key: Nat) -> StoreResult<()> { - self.init_page(pos, pos + length)?; - self.delete_keys(&[key], pos)?; - Ok(()) - } - - /// Compacts one page. - fn compact(&mut self) -> StoreResult<()> { - let head = self.head()?; - if head.cycle(&self.format) >= self.format.max_page_erases() { - return Err(StoreError::NoLifetime); - } - let tail = max(self.tail()?, head.next_page(&self.format)); - let index = self.format.index_compact(head.page(&self.format)); - let compact_info = self - .format - .build_compact(CompactInfo { tail: tail - head })?; - self.storage_write_slice(index, &compact_info)?; - self.compact_copy() - } - - /// Continues a compaction after its compact page info has been written. - fn compact_copy(&mut self) -> StoreResult<()> { - let mut head = self.head()?; - let page = head.page(&self.format); - let end = head.next_page(&self.format); - let mut tail = match self.parse_compact(page)? { - WordState::Valid(CompactInfo { tail }) => head + tail, - _ => return Err(StoreError::InvalidStorage), - }; - if tail < end { - return Err(StoreError::InvalidStorage); - } - while head < end { - let pos = head; - match self.parse_entry(&mut head)? { - ParsedEntry::Tail => break, - ParsedEntry::User(_) => (), - _ => continue, - }; - let length = head - pos; - // We have to copy the slice for 2 reasons: - // 1. We would need to work around the lifetime. This is possible using unsafe. - // 2. We can't pass a flash slice to the kernel. This should get fixed with - // https://github.com/tock/tock/issues/1274. - let entry = self.read_slice(pos, length * self.format.word_size()); - self.write_slice(tail, &entry)?; - self.init_page(tail, tail + (length - 1))?; - tail += length; - } - let erase = self.format.build_internal(InternalEntry::Erase { page })?; - self.write_slice(tail, &erase)?; - self.init_page(tail, tail)?; - self.compact_erase(tail) - } - - /// Continues a compaction after its erase entry has been written. - fn compact_erase(&mut self, erase: Position) -> StoreResult<()> { - let page = match self.parse_entry(&mut erase.clone())? { - ParsedEntry::Internal(InternalEntry::Erase { page }) => page, - _ => return Err(StoreError::InvalidStorage), - }; - self.storage_erase_page(page)?; - let head = self.head()?; - let pos = head.page_begin(&self.format); - self.wipe_span(pos, head - pos)?; - self.set_padding(erase)?; - Ok(()) - } - - /// Continues a transaction after it has been written. - fn transaction_apply(&mut self, sorted_keys: &[Nat], marker: Position) -> StoreResult<()> { - self.delete_keys(&sorted_keys, marker)?; - self.set_padding(marker)?; - let end = self.head()? + self.format.virt_size(); - let mut pos = marker + 1; - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Tail => break, - ParsedEntry::User(_) => (), - ParsedEntry::Internal(InternalEntry::Remove { .. }) => { - self.set_padding(entry_pos)? - } - _ => return Err(StoreError::InvalidStorage), - } - } - Ok(()) - } - - /// Continues a clear operation after its internal entry has been written. - fn clear_delete(&mut self, clear: Position) -> StoreResult<()> { - self.init_page(clear, clear)?; - let min_key = match self.parse_entry(&mut clear.clone())? { - ParsedEntry::Internal(InternalEntry::Clear { min_key }) => min_key, - _ => return Err(StoreError::InvalidStorage), - }; - let mut pos = self.head()?; - let end = pos + self.format.virt_size(); - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Internal(InternalEntry::Clear { .. }) if entry_pos == clear => break, - ParsedEntry::User(header) if header.key >= min_key => { - self.delete_pos(entry_pos, pos - entry_pos - 1)?; - } - ParsedEntry::Padding | ParsedEntry::User(_) => (), - _ => return Err(StoreError::InvalidStorage), - } - } - self.set_padding(clear)?; - Ok(()) - } - - /// Deletes a set of entries up to a certain position. - fn delete_keys(&mut self, sorted_keys: &[Nat], end: Position) -> StoreResult<()> { - let mut pos = self.head()?; - while pos < end { - let entry_pos = pos; - match self.parse_entry(&mut pos)? { - ParsedEntry::Tail => break, - ParsedEntry::User(header) if sorted_keys.binary_search(&header.key).is_ok() => { - self.delete_pos(entry_pos, pos - entry_pos - 1)?; - } - ParsedEntry::Padding | ParsedEntry::User(_) => (), - _ => return Err(StoreError::InvalidStorage), - } - } - Ok(()) - } - - /// Deletes the entry at a given position. - fn delete_pos(&mut self, pos: Position, length: Nat) -> StoreResult<()> { - self.set_deleted(pos)?; - self.wipe_span(pos + 1, length)?; - Ok(()) - } - - /// Writes the init info of a page between 2 positions if needed. - /// - /// The positions should designate the first and last word of an entry. The init info of the - /// highest page is written in any of the following conditions: - /// - The entry starts at the beginning of a virtual page. - /// - The entry spans 2 pages. - fn init_page(&mut self, first: Position, last: Position) -> StoreResult<()> { - debug_assert!(first <= last); - debug_assert!(last - first < self.format.virt_page_size()); - let new_first = if first.word(&self.format) == 0 { - first - } else if first.page(&self.format) == last.page(&self.format) { - return Ok(()); - } else { - last + 1 - }; - let page = new_first.page(&self.format); - if let WordState::Valid(_) = self.parse_init(page)? { - return Ok(()); - } - let index = self.format.index_init(page); - let init_info = self.format.build_init(InitInfo { - cycle: new_first.cycle(&self.format), - prefix: new_first.word(&self.format), - })?; - self.storage_write_slice(index, &init_info)?; - Ok(()) - } - - /// Sets the padding bit of a user header. - fn set_padding(&mut self, pos: Position) -> StoreResult<()> { - let mut word = Word::from_slice(self.read_word(pos)); - self.format.set_padding(&mut word)?; - self.write_slice(pos, &word.as_slice())?; - Ok(()) - } - - /// Sets the deleted bit of a user header. - fn set_deleted(&mut self, pos: Position) -> StoreResult<()> { - let mut word = Word::from_slice(self.read_word(pos)); - self.format.set_deleted(&mut word); - self.write_slice(pos, &word.as_slice())?; - Ok(()) - } - - /// Wipes a slice of words. - fn wipe_span(&mut self, pos: Position, length: Nat) -> StoreResult<()> { - let length = (length * self.format.word_size()) as usize; - self.write_slice(pos, &vec![0x00; length]) - } - - /// Returns an extremum page. - /// - /// With `Greater` returns the most recent page (or the tail). With `Less` returns the oldest - /// page (or the head). - fn get_extremum_page_head(&self, ordering: Ordering) -> StoreResult { - let mut best = None; - for page in 0..self.format.num_pages() { - let init = match self.parse_init(page)? { - WordState::Valid(x) => x, - _ => continue, - }; - let pos = self.format.page_head(init, page); - if best.map_or(true, |x| pos.cmp(&x) == ordering) { - best = Some(pos); - } - } - // There is always at least one initialized page. - best.ok_or(StoreError::InvalidStorage) - } - - /// Returns the number of words that can be written without compaction. - fn immediate_capacity(&self) -> StoreResult { - let tail = self.tail()?; - let end = self.head()? + self.format.virt_size(); - Ok(end.get().saturating_sub(tail.get())) - } - - /// Returns the position of the first word in the store. - pub(crate) fn head(&self) -> StoreResult { - self.get_extremum_page_head(Ordering::Less) - } - - /// Returns one past the position of the last word in the store. - pub(crate) fn tail(&self) -> StoreResult { - let mut pos = self.get_extremum_page_head(Ordering::Greater)?; - let end = pos.next_page(&self.format); - while pos < end { - if let ParsedEntry::Tail = self.parse_entry(&mut pos)? { - break; - } - } - Ok(pos) - } - - /// Parses the entry at a given position. - /// - /// The position is updated to point to the next entry. - fn parse_entry(&self, pos: &mut Position) -> StoreResult { - let valid = match self.parse_word(*pos)? { - WordState::Erased | WordState::Partial => return Ok(self.parse_partial(pos)), - WordState::Valid(x) => x, - }; - Ok(match valid { - ParsedWord::Padding(Padding { length }) => { - *pos += 1 + length; - ParsedEntry::Padding - } - ParsedWord::Header(header) if header.length > self.format.max_value_len() => { - self.parse_partial(pos) - } - ParsedWord::Header(header) => { - let length = self.format.bytes_to_words(header.length); - let footer = match length { - 0 => None, - _ => Some(self.read_word(*pos + length)), - }; - if header.check(footer) { - if header.key > self.format.max_key() { - return Err(StoreError::InvalidStorage); - } - *pos += 1 + length; - ParsedEntry::User(header) - } else if footer.map_or(true, |x| is_erased(x)) { - self.parse_partial(pos) - } else { - *pos += 1 + length; - ParsedEntry::PartialUser - } - } - ParsedWord::Internal(internal) => { - *pos += 1; - ParsedEntry::Internal(internal) - } - }) - } - - /// Parses a possible partial user entry. - /// - /// This does look ahead past the header and possible erased word in case words near the end of - /// the entry were written first. - fn parse_partial(&self, pos: &mut Position) -> ParsedEntry { - let mut length = None; - for i in 0..self.format.max_prefix_len() { - if !is_erased(self.read_word(*pos + i)) { - length = Some(i); - } - } - match length { - None => ParsedEntry::Tail, - Some(length) => { - *pos += 1 + length; - ParsedEntry::Partial - } - } - } - - /// Parses the init info of a page. - fn parse_init(&self, page: Nat) -> StoreResult> { - let index = self.format.index_init(page); - let word = self.storage_read_slice(index, self.format.word_size()); - self.format.parse_init(Word::from_slice(word)) - } - - /// Parses the compact info of a page. - fn parse_compact(&self, page: Nat) -> StoreResult> { - let index = self.format.index_compact(page); - let word = self.storage_read_slice(index, self.format.word_size()); - self.format.parse_compact(Word::from_slice(word)) - } - - /// Parses a word from the virtual storage. - fn parse_word(&self, pos: Position) -> StoreResult> { - self.format - .parse_word(Word::from_slice(self.read_word(pos))) - } - - /// Reads a slice from the virtual storage. - /// - /// The slice may span 2 pages. - fn read_slice(&self, pos: Position, length: Nat) -> Vec { - let mut result = Vec::with_capacity(length as usize); - let index = pos.index(&self.format); - let max_length = self.format.page_size() - usize_to_nat(index.byte); - result.extend_from_slice(self.storage_read_slice(index, min(length, max_length))); - if length > max_length { - // The slice spans the next page. - let index = pos.next_page(&self.format).index(&self.format); - result.extend_from_slice(self.storage_read_slice(index, length - max_length)); - } - result - } - - /// Reads a word from the virtual storage. - fn read_word(&self, pos: Position) -> &[u8] { - self.storage_read_slice(pos.index(&self.format), self.format.word_size()) - } - - /// Reads a physical page. - fn read_page(&self, page: Nat) -> &[u8] { - let index = StorageIndex { - page: page as usize, - byte: 0, - }; - self.storage_read_slice(index, self.format.page_size()) - } - - /// Reads a slice from the physical storage. - fn storage_read_slice(&self, index: StorageIndex, length: Nat) -> &[u8] { - // The only possible failures are if the slice spans multiple pages. - self.storage.read_slice(index, length as usize).unwrap() - } - - /// Writes a slice to the virtual storage. - /// - /// The slice may span 2 pages. - fn write_slice(&mut self, pos: Position, value: &[u8]) -> StoreResult<()> { - let index = pos.index(&self.format); - let max_length = (self.format.page_size() - usize_to_nat(index.byte)) as usize; - self.storage_write_slice(index, &value[..min(value.len(), max_length)])?; - if value.len() > max_length { - // The slice spans the next page. - let index = pos.next_page(&self.format).index(&self.format); - self.storage_write_slice(index, &value[max_length..])?; - } - Ok(()) - } - - /// Writes a slice to the physical storage. - /// - /// Only starts writing the slice from the first word that needs to be written (because it - /// differs from the current value). - fn storage_write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StoreResult<()> { - let word_size = self.format.word_size(); - debug_assert!(usize_to_nat(value.len()) % word_size == 0); - let slice = self.storage.read_slice(index, value.len())?; - // Skip as many words that don't need to be written as possible. - for start in (0..usize_to_nat(value.len())).step_by(word_size as usize) { - if is_write_needed( - &slice[start as usize..][..word_size as usize], - &value[start as usize..][..word_size as usize], - )? { - // We must write the remaining slice. - let index = StorageIndex { - page: index.page, - byte: (usize_to_nat(index.byte) + start) as usize, - }; - let value = &value[start as usize..]; - self.storage.write_slice(index, value)?; - break; - } - } - // There is nothing remaining to write. - Ok(()) - } - - /// Erases a page if not already erased. - fn storage_erase_page(&mut self, page: Nat) -> StoreResult<()> { - if !is_erased(self.read_page(page)) { - self.storage.erase_page(page as usize)?; - } - Ok(()) - } } // Those functions are not meant for production. @@ -1057,30 +483,12 @@ impl Store { &mut self.storage } - /// Extracts the storage. - pub fn extract_storage(self) -> BufferStorage { - self.storage - } - /// Returns the value of a possibly deleted entry. /// /// If the value has been partially compacted, only return the non-compacted part. Returns an /// empty value if it has been fully compacted. pub fn inspect_value(&self, handle: &StoreHandle) -> Vec { - let head = self.head().unwrap(); - let length = self.format.bytes_to_words(handle.len); - if head <= handle.pos { - // The value has not been compacted. - self.read_slice(handle.pos + 1, handle.len) - } else if (handle.pos + length).page(&self.format) == head.page(&self.format) { - // The value has been partially compacted. - let next_page = handle.pos.next_page(&self.format); - let erased_len = (next_page - (handle.pos + 1)) * self.format.word_size(); - self.read_slice(next_page, handle.len - erased_len) - } else { - // The value has been fully compacted. - Vec::new() - } + unimplemented!() } /// Applies an operation and returns the deleted entries. @@ -1089,361 +497,11 @@ impl Store { /// window. This is more expressive than returning the deleted entries after compaction since /// compaction can be controlled independently. pub fn apply(&mut self, operation: &StoreOperation) -> (Vec, StoreResult<()>) { - let deleted = |store: &Store, delete_key: &dyn Fn(usize) -> bool| { - store - .iter() - .unwrap() - .map(|x| x.unwrap()) - .filter(|x| delete_key(x.key as usize)) - .collect::>() - }; - match *operation { - StoreOperation::Transaction { ref updates } => { - let keys: HashSet = updates.iter().map(|x| x.key()).collect(); - let deleted = deleted(self, &|key| keys.contains(&key)); - (deleted, self.transaction(updates)) - } - StoreOperation::Clear { min_key } => { - let deleted = deleted(self, &|key| key >= min_key); - (deleted, self.clear(min_key)) - } - StoreOperation::Prepare { length } => (Vec::new(), self.prepare(length)), - } + unimplemented!() } /// Initializes an erased storage as if it has been erased `cycle` times. pub fn init_with_cycle(storage: &mut BufferStorage, cycle: usize) { - let format = Format::new(storage).unwrap(); - // Write the init info of the first page. - let mut index = format.index_init(0); - let init_info = format - .build_init(InitInfo { - cycle: usize_to_nat(cycle), - prefix: 0, - }) - .unwrap(); - storage.write_slice(index, &init_info).unwrap(); - // Pad the first word of the page. This makes the store looks used, otherwise we may confuse - // it with a partially initialized store. - index.byte += 2 * format.word_size() as usize; - storage - .write_slice(index, &vec![0; format.word_size() as usize]) - .unwrap(); - // Inform the storage that the pages have been used. - for page in 0..storage.num_pages() { - storage.set_page_erases(page, cycle); - } - } -} - -/// Represents an entry in the store. -#[derive(Debug)] -enum ParsedEntry { - /// Padding entry. - /// - /// This can be any of the following: - /// - A deleted user entry. - /// - A completed internal entry. - /// - A wiped partial entry. - Padding, - - /// Non-deleted user entry. - User(Header), - - /// Internal entry. - Internal(InternalEntry), - - /// Partial user entry with non-erased footer. - /// - /// The fact that the footer is not erased and does not checksum, means that the header is - /// valid. In particular, the length is valid. We cannot wipe the entry because wiping the - /// footer may validate the checksum. So we mark the entry as deleted, which also wipes it. - PartialUser, - - /// Partial entry. - /// - /// This can be any of the following: - /// - A partial user entry with erased footer. - /// - A partial user entry with invalid length. - Partial, - - /// End of entries. - /// - /// In particular this is where the next entry will be written. - Tail, -} - -/// Iterates over the entries of a store. -pub struct StoreIter<'a, S: Storage> { - /// The store being iterated. - store: &'a Store, - - /// The position of the next entry. - pos: Position, - - /// Iteration stops when reaching this position. - end: Position, -} - -impl<'a, S: Storage> StoreIter<'a, S> { - /// Creates an iterator over the entries of a store. - fn new(store: &'a Store) -> StoreResult> { - let pos = store.head()?; - let end = pos + store.format.virt_size(); - Ok(StoreIter { store, pos, end }) - } -} - -impl<'a, S: Storage> StoreIter<'a, S> { - /// Returns the next entry and advances the iterator. - fn transposed_next(&mut self) -> StoreResult> { - if self.pos >= self.end { - return Ok(None); - } - while self.pos < self.end { - let entry_pos = self.pos; - match self.store.parse_entry(&mut self.pos)? { - ParsedEntry::Tail => break, - ParsedEntry::Padding => (), - ParsedEntry::User(header) => { - return Ok(Some(StoreHandle { - key: header.key, - pos: entry_pos, - len: header.length, - })) - } - _ => return Err(StoreError::InvalidStorage), - } - } - self.pos = self.end; - Ok(None) - } -} - -impl<'a, S: Storage> Iterator for StoreIter<'a, S> { - type Item = StoreResult; - - fn next(&mut self) -> Option> { - self.transposed_next().transpose() - } -} - -/// Returns whether 2 slices are different. -/// -/// Returns an error if `target` has a bit set to one for which `source` is set to zero. -fn is_write_needed(source: &[u8], target: &[u8]) -> StoreResult { - debug_assert_eq!(source.len(), target.len()); - for (&source, &target) in source.iter().zip(target.iter()) { - if source & target != target { - return Err(StoreError::InvalidStorage); - } - if source != target { - return Ok(true); - } - } - Ok(false) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::BufferOptions; - - #[derive(Clone)] - struct Config { - word_size: usize, - page_size: usize, - num_pages: usize, - max_word_writes: usize, - max_page_erases: usize, - } - - impl Config { - fn new_driver(&self) -> StoreDriverOff { - let options = BufferOptions { - word_size: self.word_size, - page_size: self.page_size, - max_word_writes: self.max_word_writes, - max_page_erases: self.max_page_erases, - strict_mode: true, - }; - StoreDriverOff::new(options, self.num_pages) - } - } - - const MINIMAL: Config = Config { - word_size: 4, - page_size: 64, - num_pages: 5, - max_word_writes: 2, - max_page_erases: 9, - }; - - const NORDIC: Config = Config { - word_size: 4, - page_size: 0x1000, - num_pages: 20, - max_word_writes: 2, - max_page_erases: 10000, - }; - - const TITAN: Config = Config { - word_size: 4, - page_size: 0x800, - num_pages: 10, - max_word_writes: 2, - max_page_erases: 10000, - }; - - #[test] - fn nordic_capacity() { - let driver = NORDIC.new_driver().power_on().unwrap(); - assert_eq!(driver.model().capacity().total, 19123); - } - - #[test] - fn titan_capacity() { - let driver = TITAN.new_driver().power_on().unwrap(); - assert_eq!(driver.model().capacity().total, 4315); - } - - #[test] - fn minimal_virt_page_size() { - // Make sure a virtual page has 14 words. We use this property in the other tests below to - // know whether entries are spanning, starting, and ending pages. - assert_eq!(MINIMAL.new_driver().model().format().virt_page_size(), 14); - } - - #[test] - fn init_ok() { - assert!(MINIMAL.new_driver().power_on().is_ok()); - } - - #[test] - fn insert_ok() { - let mut driver = MINIMAL.new_driver().power_on().unwrap(); - // Empty entry. - driver.insert(0, &[]).unwrap(); - driver.insert(1, &[]).unwrap(); - driver.check().unwrap(); - // Last word is erased but last bit is not user data. - driver.insert(0, &[0xff]).unwrap(); - driver.insert(1, &[0xff]).unwrap(); - driver.check().unwrap(); - // Last word is erased and last bit is user data. - driver.insert(0, &[0xff, 0xff, 0xff, 0xff]).unwrap(); - driver.insert(1, &[0xff, 0xff, 0xff, 0xff]).unwrap(); - driver.insert(2, &[0x5c; 6]).unwrap(); - driver.check().unwrap(); - // Entry spans 2 pages. - assert_eq!(driver.store().tail().unwrap().get(), 13); - driver.insert(3, &[0x5c; 8]).unwrap(); - driver.check().unwrap(); - assert_eq!(driver.store().tail().unwrap().get(), 16); - // Entry ends a page. - driver.insert(2, &[0x93; (28 - 16 - 1) * 4]).unwrap(); - driver.check().unwrap(); - assert_eq!(driver.store().tail().unwrap().get(), 28); - // Entry starts a page. - driver.insert(3, &[0x81; 10]).unwrap(); - driver.check().unwrap(); - } - - #[test] - fn remove_ok() { - let mut driver = MINIMAL.new_driver().power_on().unwrap(); - // Remove absent entry. - driver.remove(0).unwrap(); - driver.remove(1).unwrap(); - driver.check().unwrap(); - // Remove last inserted entry. - driver.insert(0, &[0x5c; 6]).unwrap(); - driver.remove(0).unwrap(); - driver.check().unwrap(); - // Remove empty entries. - driver.insert(0, &[]).unwrap(); - driver.insert(1, &[]).unwrap(); - driver.remove(0).unwrap(); - driver.remove(1).unwrap(); - driver.check().unwrap(); - // Remove entry with flipped bit. - driver.insert(0, &[0xff]).unwrap(); - driver.insert(1, &[0xff; 4]).unwrap(); - driver.remove(0).unwrap(); - driver.remove(1).unwrap(); - driver.check().unwrap(); - // Write some entries with one spanning 2 pages. - driver.insert(2, &[0x93; 9]).unwrap(); - assert_eq!(driver.store().tail().unwrap().get(), 13); - driver.insert(3, &[0x81; 10]).unwrap(); - assert_eq!(driver.store().tail().unwrap().get(), 17); - driver.insert(4, &[0x76; 11]).unwrap(); - driver.check().unwrap(); - // Remove the entry spanning 2 pages. - driver.remove(3).unwrap(); - driver.check().unwrap(); - // Write some entries with one ending a page and one starting the next. - assert_eq!(driver.store().tail().unwrap().get(), 21); - driver.insert(2, &[0xd7; (28 - 21 - 1) * 4]).unwrap(); - assert_eq!(driver.store().tail().unwrap().get(), 28); - driver.insert(4, &[0xe2; 21]).unwrap(); - driver.check().unwrap(); - // Remove them. - driver.remove(2).unwrap(); - driver.remove(4).unwrap(); - driver.check().unwrap(); - } - - #[test] - fn prepare_ok() { - let mut driver = MINIMAL.new_driver().power_on().unwrap(); - - // Don't compact if enough immediate capacity. - assert_eq!(driver.store().immediate_capacity().unwrap(), 39); - assert_eq!(driver.store().capacity().unwrap().remaining(), 34); - assert_eq!(driver.store().head().unwrap().get(), 0); - driver.store_mut().prepare(34).unwrap(); - assert_eq!(driver.store().head().unwrap().get(), 0); - - // Fill the store. - for key in 0..4 { - driver.insert(key, &[0x38; 28]).unwrap(); - } - driver.check().unwrap(); - assert_eq!(driver.store().immediate_capacity().unwrap(), 7); - assert_eq!(driver.store().capacity().unwrap().remaining(), 2); - // Removing entries increases available capacity but not immediate capacity. - driver.remove(0).unwrap(); - driver.remove(2).unwrap(); - driver.check().unwrap(); - assert_eq!(driver.store().immediate_capacity().unwrap(), 7); - assert_eq!(driver.store().capacity().unwrap().remaining(), 18); - - // Prepare for next write (7 words data + 1 word overhead). - assert_eq!(driver.store().head().unwrap().get(), 0); - driver.store_mut().prepare(8).unwrap(); - driver.check().unwrap(); - assert_eq!(driver.store().head().unwrap().get(), 16); - // The available capacity did not change, but the immediate capacity is above 8. - assert_eq!(driver.store().immediate_capacity().unwrap(), 14); - assert_eq!(driver.store().capacity().unwrap().remaining(), 18); - } - - #[test] - fn reboot_ok() { - let mut driver = MINIMAL.new_driver().power_on().unwrap(); - - // Do some operations and reboot. - driver.insert(0, &[0x38; 24]).unwrap(); - driver.insert(1, &[0x5c; 13]).unwrap(); - driver = driver.power_off().power_on().unwrap(); - driver.check().unwrap(); - - // Do more operations and reboot. - driver.insert(2, &[0x93; 1]).unwrap(); - driver.remove(0).unwrap(); - driver.insert(3, &[0xde; 9]).unwrap(); - driver = driver.power_off().power_on().unwrap(); - driver.check().unwrap(); + unimplemented!() } } diff --git a/apps/vault/libraries/persistent_store/src/test.rs b/apps/vault/libraries/persistent_store/src/test.rs new file mode 100644 index 000000000..da834ee8b --- /dev/null +++ b/apps/vault/libraries/persistent_store/src/test.rs @@ -0,0 +1,89 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::{BufferOptions, BufferStorage, Store, StoreDriverOff}; + +#[derive(Clone)] +pub struct Config { + pub word_size: usize, + pub page_size: usize, + pub num_pages: usize, + pub max_word_writes: usize, + pub max_page_erases: usize, +} + +impl Config { + pub fn new_driver(&self) -> StoreDriverOff { + StoreDriverOff::new(self.into(), self.num_pages) + } + + pub fn new_store(&self) -> Store { + self.new_driver().power_on().unwrap().extract_store() + } +} + +impl<'a> From<&'a Config> for BufferOptions { + fn from(config: &'a Config) -> Self { + BufferOptions { + word_size: config.word_size, + page_size: config.page_size, + max_word_writes: config.max_word_writes, + max_page_erases: config.max_page_erases, + strict_mode: true, + } + } +} + +pub const MINIMAL: Config = Config { + word_size: 4, + page_size: 64, + num_pages: 5, + max_word_writes: 2, + max_page_erases: 9, +}; + +const NORDIC: Config = Config { + word_size: 4, + page_size: 0x1000, + num_pages: 20, + max_word_writes: 2, + max_page_erases: 10000, +}; + +const TITAN: Config = Config { + word_size: 4, + page_size: 0x800, + num_pages: 10, + max_word_writes: 2, + max_page_erases: 10000, +}; + +#[test] +fn nordic_capacity() { + let driver = NORDIC.new_driver().power_on().unwrap(); + assert_eq!(driver.model().capacity().total, 19123); +} + +#[test] +fn titan_capacity() { + let driver = TITAN.new_driver().power_on().unwrap(); + assert_eq!(driver.model().capacity().total, 4315); +} + +#[test] +fn minimal_virt_page_size() { + // Make sure a virtual page has 14 words. We use this property in the other tests below to + // know whether entries are spanning, starting, and ending pages. + assert_eq!(MINIMAL.new_driver().model().format().virt_page_size(), 14); +} diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index a87858d32..f8594c640 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -11,22 +11,24 @@ use std::io::{Write, Read}; use passwords::PasswordGenerator; use std::cell::RefCell; -use crate::{ux::{ListItem, deserialize_app_info}, ctap::FIDO_CRED_DICT, storage::{self, PasswordRecord, StorageContent}, ItemLists}; -use crate::{VaultMode, SelectedEntry}; -use crate::utc_now; - -use crate::fido::U2F_APP_DICT; +use vault::{ + deserialize_app_info, serialize_app_info, AppInfo, basis_change, + VAULT_PASSWORD_DICT, VAULT_TOTP_DICT, VAULT_ALLOC_HINT, utc_now, + atime_to_str +}; +use crate::ux::framework::ListItem; +use crate::{ItemLists, VaultMode, SelectedEntry}; +use crate::storage::{self, PasswordRecord, StorageContent}; use crate::totp::TotpAlgorithm; +use persistent_store::store::OPENSK2_DICT; + +use vault::env::xous::U2F_APP_DICT; #[cfg(feature="vaultperf")] use perflib::*; #[cfg(feature="vaultperf")] const FILE_ID_APPS_VAULT_SRC_ACTIONS: u32 = 1; -pub(crate) const VAULT_PASSWORD_DICT: &'static str = "vault.passwords"; -pub(crate) const VAULT_TOTP_DICT: &'static str = "vault.totp"; -/// bytes to reserve for a key entry. Making this slightly larger saves on some churn as stuff gets updated -pub(crate) const VAULT_ALLOC_HINT: usize = 256; const VAULT_PASSWORD_REC_VERSION: u32 = 1; const VAULT_TOTP_REC_VERSION: u32 = 1; /// time allowed between dialog box swaps for background operations to redraw @@ -34,7 +36,7 @@ const VAULT_TOTP_REC_VERSION: u32 = 1; const SWAP_DELAY_MS: usize = 300; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub(crate) enum ActionOp { +pub enum ActionOp { /// Menu items MenuAddnew, MenuEditStage2, @@ -570,7 +572,7 @@ impl<'a> ActionManager<'a> { let mut desc = String::with_capacity(256); make_pw_name(&pw.description, &pw.username, &mut desc); let mut extra = String::with_capacity(256); - let human_time = crate::ux::atime_to_str(pw.atime); + let human_time = atime_to_str(pw.atime); extra.push_str(&human_time); extra.push_str("; "); extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG)); @@ -606,7 +608,7 @@ impl<'a> ActionManager<'a> { // storage format that's a bit funkier to edit. let maybe_update = match self.pddb.borrow().get( dict, entry.key_name.as_str().unwrap(), None, - false, false, None, Some(crate::basis_change) + false, false, None, Some(basis_change) ) { Ok(mut record) => { // resolve the basis of the key, so that we are editing it "in place" @@ -614,7 +616,7 @@ impl<'a> ActionManager<'a> { let mut data = Vec::::new(); let maybe_update = match record.read_to_end(&mut data) { Ok(_len) => { - if let Some(mut ai) = crate::fido::deserialize_app_info(data) { + if let Some(mut ai) = deserialize_app_info(data) { let edit_data = if ai.notes != t!("vault.notes", xous::LANG) { self.modals .alert_builder(t!("vault.edit_dialog", xous::LANG)) @@ -654,10 +656,10 @@ impl<'a> ActionManager<'a> { match self.pddb.borrow().get( dict, entry.key_name.as_str().unwrap(), Some(&basis), false, true, Some(VAULT_ALLOC_HINT), - Some(crate::basis_change) + Some(basis_change) ) { Ok(mut record) => { - let ser = crate::fido::serialize_app_info(&update); + let ser = serialize_app_info(&update); record.write(&ser).unwrap_or_else(|e| { self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); 0}); } @@ -860,7 +862,7 @@ impl<'a> ActionManager<'a> { self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); if prev_entry.atime != pw_rec.atime || prev_entry.count != pw_rec.count { // this is expensive, so don't run it unless we have to - let human_time = crate::ux::atime_to_str(pw_rec.atime); + let human_time = atime_to_str(pw_rec.atime); // note this code is duplicated in update_db_entry() extra.push_str(&human_time); extra.push_str("; "); @@ -887,7 +889,7 @@ impl<'a> ActionManager<'a> { #[cfg(feature="vaultperf")] self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 4, std::line!()); - let human_time = crate::ux::atime_to_str(pw_rec.atime); + let human_time = atime_to_str(pw_rec.atime); extra.push_str(&human_time); extra.push_str("; "); @@ -951,7 +953,7 @@ impl<'a> ActionManager<'a> { if let Some(data) = key.data { if let Some(ai) = deserialize_app_info(data) { let extra = format!("{}; {}{}", - crate::ux::atime_to_str(ai.atime), + atime_to_str(ai.atime), t!("vault.u2f.appinfo.authcount", xous::LANG), ai.count, ); @@ -993,13 +995,13 @@ impl<'a> ActionManager<'a> { }, } - log::debug!("listing in {}", FIDO_CRED_DICT); - match self.pddb.borrow().read_dict(FIDO_CRED_DICT, None, Some(256 * 1024)) { + log::debug!("listing in {}", OPENSK2_DICT); + match self.pddb.borrow().read_dict(OPENSK2_DICT, None, Some(256 * 1024)) { Ok(keys) => { let mut oom_keys = 0; for key in keys { if let Some(data) = key.data { - match crate::ctap::storage::deserialize_credential(&data) { + match vault::ctap::storage::deserialize_credential(&data) { Some(result) => { let name = if let Some(display_name) = result.user_display_name { display_name @@ -1019,10 +1021,13 @@ impl<'a> ActionManager<'a> { il.insert(li.key(), li); } None => { + // this is "harmless" because we're now returning, like, everything + // print to info! for now, so we can be more selective down the road about + // deserializations and maybe save some time let err = format!("{}:{}:{}: ({}){:x?}...", - key.basis, FIDO_CRED_DICT, key.name, key.len, &data[..16] + key.basis, OPENSK2_DICT, key.name, key.len, &data[..16] ); - self.report_err("Couldn't deserialize FIDO2:", Some(err)) + log::info!("Couldn't deserialize FIDO2 key {}", err); }, } } else { @@ -1239,7 +1244,7 @@ impl<'a> ActionManager<'a> { #[cfg(feature="vault-testing")] pub(crate) fn populate_tests(&mut self) { - use crate::ux::serialize_app_info; + use serialize_app_info; self.modals.dynamic_notification(Some("Creating test entries..."), None).ok(); let words = [ @@ -1292,7 +1297,7 @@ impl<'a> ActionManager<'a> { } } // --- U2F + FIDO --- - let fido = self.pddb.borrow().list_keys(FIDO_CRED_DICT, None).unwrap_or(Vec::new()); + let fido = self.pddb.borrow().list_keys(OPENSK2_DICT, None).unwrap_or(Vec::new()); let u2f = self.pddb.borrow().list_keys(U2F_APP_DICT, None).unwrap_or(Vec::new()); let total = fido.len() + u2f.len(); if total < TARGET_ENTRIES { @@ -1304,7 +1309,7 @@ impl<'a> ActionManager<'a> { let notes = random_pick::pick_from_slice(&words, &weights).unwrap().to_string(); let mut id = [0u8; 32]; self.trng.borrow_mut().fill_bytes(&mut id); - let record = crate::AppInfo { + let record = AppInfo { name, id, notes, @@ -1318,7 +1323,7 @@ impl<'a> ActionManager<'a> { U2F_APP_DICT, &app_id_str, None, true, true, - Some(256), Some(crate::basis_change) + Some(256), Some(basis_change) ) { Ok(mut app_data) => { match app_data.write(&ser) { @@ -1335,16 +1340,17 @@ impl<'a> ActionManager<'a> { let xns = xous_names::XousNames::new().unwrap(); let mut rng = ctap_crypto::rng256::XousRng256::new(&xns); for index in 0..extra_fido { - use crate::ctap::data_formats::*; - let c_id = random_pick::pick_multiple_from_slice(&words, &weights, 2); - let cred_id = format!("{} {} {}", c_id[0], c_id[1], index); + use vault::ctap::data_formats::*; + use ctap_crypto::rng256::Rng256; + let _c_id = random_pick::pick_multiple_from_slice(&words, &weights, 2); + let cred_id = format!("{}", index + 1800); // 1800 is extracted from ctap/storage/keys.rs; 1700 is the start of the credential region, this sticks it...somewhere "above" that let r_id = random_pick::pick_multiple_from_slice(&words, &weights, 2); let rp_id = format!("{} {}", r_id[0], r_id[1]); let handle = random_pick::pick_from_slice(&words, &weights).unwrap().to_string(); let new_credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, - credential_id: cred_id.as_bytes().to_vec(), - private_key: ctap_crypto::ecdsa::SecKey::gensk(&mut rng), + credential_id: rng.gen_uniform_u8x32().to_vec(), + private_key: vault::ctap::crypto_wrapper::PrivateKey::Ecdsa(rng.gen_uniform_u8x32()), rp_id, user_handle: handle.as_bytes().to_vec(), user_display_name: None, @@ -1352,16 +1358,18 @@ impl<'a> ActionManager<'a> { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; let shortid = &cred_id; match self.pddb.borrow().get( - FIDO_CRED_DICT, + OPENSK2_DICT, shortid, None, true, true, - Some(crate::ctap::storage::CRED_INITAL_SIZE), Some(crate::basis_change) + Some(128), Some(basis_change) ) { Ok(mut cred) => { - let value = crate::ctap::storage::serialize_credential(new_credential).unwrap(); + let value = vault::ctap::storage::serialize_credential(new_credential).unwrap(); match cred.write(&value) { Ok(len) => { log::debug!("fido2 wrote {} bytes", len); diff --git a/apps/vault/src/api/attestation_store.rs b/apps/vault/src/api/attestation_store.rs new file mode 100644 index 000000000..6e6326fc1 --- /dev/null +++ b/apps/vault/src/api/attestation_store.rs @@ -0,0 +1,100 @@ +use std::vec::Vec; +use persistent_store::{StoreError, StoreUpdate}; + +use crate::env::Env; + +/// Identifies an attestation. +#[derive(Clone, PartialEq, Eq)] +pub enum Id { + Batch, + Enterprise, +} + +#[cfg_attr(feature = "std", derive(Debug, PartialEq, Eq))] +pub struct Attestation { + /// ECDSA private key (big-endian). + pub private_key: [u8; 32], + pub certificate: Vec, +} + +/// Stores enterprise or batch attestations. +pub trait AttestationStore { + /// Returns an attestation given its id, if it exists. + /// + /// This should always return the attestation. Checking whether it is ok to use the attestation + /// is done in the CTAP library. + fn get(&mut self, id: &Id) -> Result, Error>; + + /// Sets the attestation for a given id. + /// + /// This function may not be supported. + fn set(&mut self, id: &Id, attestation: Option<&Attestation>) -> Result<(), Error>; +} + +/// Attestation store errors. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Error { + Storage, + Internal, + NoSupport, +} + +/// Keys of the environment store reserved for the attestation store. +pub const STORAGE_KEYS: &[usize] = &[1, 2]; + +pub fn helper_get(env: &mut impl Env) -> Result, Error> { + let private_key = env.store().find(PRIVATE_KEY_STORAGE_KEY)?; + let certificate = env.store().find(CERTIFICATE_STORAGE_KEY)?; + let (private_key, certificate) = match (private_key, certificate) { + (Some(x), Some(y)) => (x, y), + (None, None) => return Ok(None), + _ => return Err(Error::Internal), + }; + if private_key.len() != 32 { + return Err(Error::Internal); + } + Ok(Some(Attestation { + private_key: *array_ref![private_key, 0, 32], + certificate, + })) +} + +pub fn helper_set(env: &mut impl Env, attestation: Option<&Attestation>) -> Result<(), Error> { + let updates = match attestation { + None => [ + StoreUpdate::Remove { + key: PRIVATE_KEY_STORAGE_KEY, + }, + StoreUpdate::Remove { + key: CERTIFICATE_STORAGE_KEY, + }, + ], + Some(attestation) => [ + StoreUpdate::Insert { + key: PRIVATE_KEY_STORAGE_KEY, + value: &attestation.private_key[..], + }, + StoreUpdate::Insert { + key: CERTIFICATE_STORAGE_KEY, + value: &attestation.certificate[..], + }, + ], + }; + Ok(env.store().transaction(&updates)?) +} + +const PRIVATE_KEY_STORAGE_KEY: usize = STORAGE_KEYS[0]; +const CERTIFICATE_STORAGE_KEY: usize = STORAGE_KEYS[1]; + +impl From for Error { + fn from(error: StoreError) -> Self { + match error { + StoreError::InvalidArgument + | StoreError::NoCapacity + | StoreError::NoLifetime + | StoreError::InvalidStorage => Error::Internal, + StoreError::StorageError => Error::Storage, + } + } +} diff --git a/apps/vault/src/api/connection.rs b/apps/vault/src/api/connection.rs new file mode 100644 index 000000000..0bf788331 --- /dev/null +++ b/apps/vault/src/api/connection.rs @@ -0,0 +1,33 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::time::Duration; + +pub enum SendOrRecvStatus { + Timeout, + Sent, + Received, +} + +pub struct SendOrRecvError; + +pub type SendOrRecvResult = Result; + +pub trait HidConnection { + fn send_and_maybe_recv( + &mut self, + buf: &mut [u8; 64], + timeout: Duration, + ) -> SendOrRecvResult; +} diff --git a/apps/vault/src/api/customization.rs b/apps/vault/src/api/customization.rs new file mode 100644 index 000000000..f4d456de7 --- /dev/null +++ b/apps/vault/src/api/customization.rs @@ -0,0 +1,449 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! This file contains all customizable constants. +//! +//! If you adapt them, make sure to run the tests before flashing the firmware. +//! Our deploy script enforces the invariants. + +use crate::ctap::data_formats::{CredentialProtectionPolicy, EnterpriseAttestationMode}; +use std::string::String; +use std::vec::Vec; + +pub trait Customization { + // ########################################################################### + // Constants for adjusting privacy and protection levels. + // ########################################################################### + + /// Removes support for PIN protocol v1. + /// + /// We support PIN protocol v2, "intended to aid FIPS certification". + /// To certify, you might want to remove support for v1 using this customization. + fn allows_pin_protocol_v1(&self) -> bool; + + /// Changes the default level for the credProtect extension. + /// + /// You can change this value to one of the following for more privacy: + /// - CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList + /// - CredentialProtectionPolicy::UserVerificationRequired + /// + /// UserVerificationOptionalWithCredentialIdList + /// Resident credentials are discoverable with + /// - an allowList, + /// - an excludeList, + /// - user verification. + /// + /// UserVerificationRequired + /// Resident credentials are discoverable with user verification only. + /// + /// This can improve privacy, but can make usage less comfortable. + fn default_cred_protect(&self) -> Option; + + /// Sets the initial minimum PIN length in code points. + /// + /// # Invariant + /// + /// - The minimum PIN length must be at least 4. + /// - The minimum PIN length must be at most 63. + /// - default_min_pin_length_rp_ids() must be non-empty if max_rp_ids_length() is 0. + /// + /// Requiring longer PINs can help establish trust between users and relying + /// parties. It makes user verification harder to break, but less convenient. + /// NIST recommends at least 6-digit PINs in section 5.1.9.1: + /// https://pages.nist.gov/800-63-3/sp800-63b.html + /// + /// Reset reverts the minimum PIN length to this DEFAULT_MIN_PIN_LENGTH. + fn default_min_pin_length(&self) -> u8; + + /// Lists relying parties that can read the minimum PIN length. + /// + /// # Invariant + /// + /// - default_min_pin_length_rp_ids() must be non-empty if max_rp_ids_length() is 0 + /// + /// Only the RP IDs listed in default_min_pin_length_rp_ids are allowed to read + /// the minimum PIN length with the minPinLength extension. + fn default_min_pin_length_rp_ids(&self) -> Vec; + + /// Enforces the alwaysUv option. + /// + /// When setting to true, commands require a PIN. + /// Also, alwaysUv can not be disabled by commands. + /// + /// A certification (additional to FIDO Alliance's) might require enforcing + /// alwaysUv. Otherwise, users should have the choice to configure alwaysUv. + /// Calling toggleAlwaysUv is preferred over enforcing alwaysUv here. + fn enforce_always_uv(&self) -> bool; + + /// Allows usage of enterprise attestation. + /// + /// # Invariant + /// + /// - Enterprise and batch attestation can not both be active. + /// - If the mode is VendorFacilitated, enterprise_attestation_mode() must be non-empty. + /// + /// For privacy reasons, it is disabled by default. You can choose between: + /// - EnterpriseAttestationMode::VendorFacilitated + /// - EnterpriseAttestationMode::PlatformManaged + /// + /// VendorFacilitated + /// Enterprise attestation is restricted to enterprise_attestation_mode(). Add your + /// enterprises domain, e.g. "example.com", to the list below. + /// + /// PlatformManaged + /// All relying parties can request an enterprise attestation. The authenticator + /// trusts the platform to filter requests. + /// + /// To enable the feature, send the subcommand enableEnterpriseAttestation in + /// AuthenticatorConfig. An enterprise might want to customize the type of + /// attestation that is used. OpenSK defaults to batch attestation. Configuring + /// individual certificates then makes authenticators identifiable. + /// + /// OpenSK prevents activating batch and enterprise attestation together. The + /// current implementation uses the same key material at the moment, and these + /// two modes have conflicting privacy guarantees. + /// If you implement your own enterprise attestation mechanism, and you want + /// batch attestation at the same time, proceed carefully and remove the + /// assertion. + fn enterprise_attestation_mode(&self) -> Option; + + /// Lists relying party IDs that can perform enterprise attestation. + /// + /// # Invariant + /// + /// - If the mode is VendorFacilitated, enterprise_attestation_mode() must be non-empty. + /// + /// This list is only considered if enterprise attestation is used. + #[cfg(feature = "std")] + fn enterprise_rp_id_list(&self) -> Vec; + + /// Returns whether the rp_id is contained in enterprise_rp_id_list(). + fn is_enterprise_rp_id(&self, rp_id: &str) -> bool; + + /// Maximum message size send for CTAP commands. + /// + /// The maximum value is 7609, as HID packets can not encode longer messages. + /// 1024 is the default mentioned in the authenticatorLargeBlobs commands. + /// Larger values are preferred, as that allows more parameters in commands. + /// If long commands are too unreliable on your hardware, consider decreasing + /// this value. + fn max_msg_size(&self) -> usize; + + /// Sets the number of consecutive failed PINs before blocking interaction. + /// + /// # Invariant + /// + /// - CTAP2.0: Maximum PIN retries must be 8. + /// - CTAP2.1: Maximum PIN retries must be 8 at most. + /// + /// The fail retry counter is reset after entering the correct PIN. + fn max_pin_retries(&self) -> u8; + + /// Enables or disables basic attestation for FIDO2. + /// + /// # Invariant + /// + /// - Enterprise and batch attestation can not both be active (see above). + /// + /// The basic attestation uses the signing key configured with a vendor command + /// as a batch key. If you turn batch attestation on, be aware that it is your + /// responsibility to safely generate and store the key material. Also, the + /// batches must have size of at least 100k authenticators before using new key + /// material. + /// U2F is unaffected by this setting. + /// + /// https://www.w3.org/TR/webauthn/#attestation + fn use_batch_attestation(&self) -> bool; + + /// Enables or disables signature counters. + /// + /// The signature counter is currently implemented as a global counter. + /// The specification strongly suggests to have per-credential counters. + /// Implementing those means you can't have an infinite amount of server-side + /// credentials anymore. Also, since counters need frequent writes on the + /// persistent storage, we might need a flash friendly implementation. This + /// solution is a compromise to be compatible with U2F and not wasting storage. + /// + /// https://www.w3.org/TR/webauthn/#signature-counter + fn use_signature_counter(&self) -> bool; + + // ########################################################################### + // Constants for performance optimization or adapting to different hardware. + // + // Those constants may be modified before compilation to tune the behavior of + // the key. + // ########################################################################### + + /// Sets the maximum blob size stored with the credBlob extension. + /// + /// # Invariant + /// + /// - The length must be at least 32. + /// - OpenSK puts a limit that the length must be at most 64, as it needs to + /// be persisted in the credential ID. + fn max_cred_blob_length(&self) -> usize; + + /// Limits the number of considered entries in credential lists. + /// + /// # Invariant + /// + /// - This value, if present, must be at least 1 (more is preferred). + /// + /// Depending on your memory, you can use Some(n) to limit request sizes in + /// MakeCredential and GetAssertion. This affects allowList and excludeList. + fn max_credential_count_in_list(&self) -> Option; + + /// Limits the size of largeBlobs the authenticator stores. + /// + /// # Invariant + /// + /// - The allowed size must be at least 1024. + /// - The array must fit into the shards reserved in storage/key.rs. + fn max_large_blob_array_size(&self) -> usize; + + /// Limits the number of RP IDs that can change the minimum PIN length. + /// + /// # Invariant + /// + /// - If this value is 0, default_min_pin_length_rp_ids() must be non-empty. + /// + /// You can use this constant to have an upper limit in storage requirements. + /// This might be useful if you want to more reliably predict the remaining + /// storage. Stored string can still be of arbitrary length though, until RP ID + /// truncation is implemented. + /// Outside of memory considerations, you can set this value to 0 if only RP IDs + /// in default_min_pin_length_rp_ids() should be allowed to change the minimum + /// PIN length. + fn max_rp_ids_length(&self) -> usize; + + /// Sets the number of resident keys you can store. + /// + /// # Invariant + /// + /// - The storage key CREDENTIALS must fit at least this number of credentials. + /// + /// Limiting the number of resident keys permits to ensure a minimum number of + /// counter increments. + /// Let: + /// - P the number of pages (NUM_PAGES in the board definition) + /// - K the maximum number of resident keys (max_supported_resident_keys()) + /// - S the maximum size of a resident key (about 500) + /// - C the number of erase cycles (10000) + /// - I the minimum number of counter increments + /// + /// We have: I = (P * 4084 - 5107 - K * S) / 8 * C + /// + /// With P=20 and K=150, we have I=2M which is enough for 500 increments per day + /// for 10 years. + fn max_supported_resident_keys(&self) -> usize; +} + +#[derive(Clone)] +pub struct CustomizationImpl { + pub allows_pin_protocol_v1: bool, + pub default_cred_protect: Option, + pub default_min_pin_length: u8, + pub default_min_pin_length_rp_ids: &'static [&'static str], + pub enforce_always_uv: bool, + pub enterprise_attestation_mode: Option, + pub enterprise_rp_id_list: &'static [&'static str], + pub max_msg_size: usize, + pub max_pin_retries: u8, + pub use_batch_attestation: bool, + pub use_signature_counter: bool, + pub max_cred_blob_length: usize, + pub max_credential_count_in_list: Option, + pub max_large_blob_array_size: usize, + pub max_rp_ids_length: usize, + pub max_supported_resident_keys: usize, +} + +pub const DEFAULT_CUSTOMIZATION: CustomizationImpl = CustomizationImpl { + allows_pin_protocol_v1: true, + default_cred_protect: None, + default_min_pin_length: 4, + default_min_pin_length_rp_ids: &[], + enforce_always_uv: false, + enterprise_attestation_mode: None, + enterprise_rp_id_list: &[], + max_msg_size: 7609, + max_pin_retries: 8, + use_batch_attestation: false, + use_signature_counter: true, + max_cred_blob_length: 32, + max_credential_count_in_list: None, + max_large_blob_array_size: 2048, + max_rp_ids_length: 8, + max_supported_resident_keys: 150, +}; + +impl Customization for CustomizationImpl { + fn allows_pin_protocol_v1(&self) -> bool { + self.allows_pin_protocol_v1 + } + + fn default_cred_protect(&self) -> Option { + self.default_cred_protect + } + + fn default_min_pin_length(&self) -> u8 { + self.default_min_pin_length + } + + fn default_min_pin_length_rp_ids(&self) -> Vec { + self.default_min_pin_length_rp_ids + .iter() + .map(|s| String::from(*s)) + .collect() + } + + fn enforce_always_uv(&self) -> bool { + self.enforce_always_uv + } + + fn enterprise_attestation_mode(&self) -> Option { + self.enterprise_attestation_mode + } + + #[cfg(feature = "std")] + fn enterprise_rp_id_list(&self) -> Vec { + self.enterprise_rp_id_list + .iter() + .map(|s| String::from(*s)) + .collect() + } + + fn is_enterprise_rp_id(&self, rp_id: &str) -> bool { + self.enterprise_rp_id_list.contains(&rp_id) + } + + fn max_msg_size(&self) -> usize { + self.max_msg_size + } + + fn max_pin_retries(&self) -> u8 { + self.max_pin_retries + } + + fn use_batch_attestation(&self) -> bool { + self.use_batch_attestation + } + + fn use_signature_counter(&self) -> bool { + self.use_signature_counter + } + + fn max_cred_blob_length(&self) -> usize { + self.max_cred_blob_length + } + + fn max_credential_count_in_list(&self) -> Option { + self.max_credential_count_in_list + } + + fn max_large_blob_array_size(&self) -> usize { + self.max_large_blob_array_size + } + + fn max_rp_ids_length(&self) -> usize { + self.max_rp_ids_length + } + + fn max_supported_resident_keys(&self) -> usize { + self.max_supported_resident_keys + } +} + +#[cfg(feature = "std")] +pub fn is_valid(customization: &impl Customization) -> bool { + // Two invariants are currently tested in different files: + // - storage.rs: if max_large_blob_array_size() fits the shards + // - storage/key.rs: if max_supported_resident_keys() fits CREDENTIALS + + // Max message size must be between 1024 and 7609. + if customization.max_msg_size() < 1024 || customization.max_msg_size() > 7609 { + return false; + } + + // Default min pin length must be between 4 and 63. + if customization.default_min_pin_length() < 4 || customization.default_min_pin_length() > 63 { + return false; + } + + // OpenSK prevents activating batch and enterprise attestation together. The + // current implementation uses the same key material at the moment, and these + // two modes have conflicting privacy guarantees. + if customization.use_batch_attestation() + && customization.enterprise_attestation_mode().is_some() + { + return false; + } + + // enterprise_rp_id_list() should be non-empty in vendor facilitated mode. + if matches!( + customization.enterprise_attestation_mode(), + Some(EnterpriseAttestationMode::VendorFacilitated) + ) && customization.enterprise_rp_id_list().is_empty() + { + return false; + } + + // enterprise_rp_id_list() should be empty without an enterprise attestation mode. + if customization.enterprise_attestation_mode().is_none() + && !customization.enterprise_rp_id_list().is_empty() + { + return false; + } + + // Max pin retries must be less or equal than 8. + if customization.max_pin_retries() > 8 { + return false; + } + + // Max cred blob length should be at least 32, and at most 64. + if customization.max_cred_blob_length() < 32 || customization.max_cred_blob_length() > 64 { + return false; + } + + // Max credential count in list should be positive if exists. + if let Some(count) = customization.max_credential_count_in_list() { + if count < 1 { + return false; + } + } + + // Max large blob array size should not be less than 1024. + if customization.max_large_blob_array_size() < 1024 { + return false; + } + + // Default min pin length rp ids must be non-empty if max rp ids length is 0. + if customization.max_rp_ids_length() == 0 + && customization.default_min_pin_length_rp_ids().is_empty() + { + return false; + } + + true +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_invariants() { + assert!(is_valid(&DEFAULT_CUSTOMIZATION)); + } +} diff --git a/apps/vault/src/api/firmware_protection.rs b/apps/vault/src/api/firmware_protection.rs new file mode 100644 index 000000000..924d314e8 --- /dev/null +++ b/apps/vault/src/api/firmware_protection.rs @@ -0,0 +1,20 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub trait FirmwareProtection { + /// Locks the firmware. + /// + /// Returns whether the operation was successful. + fn lock(&mut self) -> bool; +} diff --git a/apps/vault/src/api/key_store.rs b/apps/vault/src/api/key_store.rs new file mode 100644 index 000000000..87c91a832 --- /dev/null +++ b/apps/vault/src/api/key_store.rs @@ -0,0 +1,149 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::vec::Vec; +use ctap_crypto::ecdsa::SecKey; +use persistent_store::StoreError; +use ctap_crypto::rng256::Rng256; + +use crate::env::Env; + +/// Provides storage for secret keys. +/// +/// Implementations may use the environment store: [`STORAGE_KEY`] is reserved for this usage. +pub trait KeyStore { + /// Returns the AES key for key handles encryption. + fn key_handle_encryption(&mut self) -> Result<[u8; 32], Error>; + + /// Returns the key for key handles authentication. + fn key_handle_authentication(&mut self) -> Result<[u8; 32], Error>; + + /// Derives an ECDSA private key from a seed. + /// + /// The result is big-endian. + fn derive_ecdsa(&mut self, seed: &[u8; 32]) -> Result<[u8; 32], Error>; + + /// Generates a seed to derive an ECDSA private key. + fn generate_ecdsa_seed(&mut self) -> Result<[u8; 32], Error>; + + /// Resets the key store. + fn reset(&mut self) -> Result<(), Error>; +} + +/// Key store errors. +/// +/// They are deliberately indistinguishable to avoid leaking information. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct Error; + +/// Key of the environment store reserved for the key store. +pub const STORAGE_KEY: usize = 2046; + +/// Implements a default key store using the environment rng and store. +pub trait Helper: Env {} + +impl KeyStore for T { + fn key_handle_encryption(&mut self) -> Result<[u8; 32], Error> { + Ok(get_master_keys(self)?.encryption) + } + + fn key_handle_authentication(&mut self) -> Result<[u8; 32], Error> { + Ok(get_master_keys(self)?.authentication) + } + + fn derive_ecdsa(&mut self, seed: &[u8; 32]) -> Result<[u8; 32], Error> { + match SecKey::from_bytes(seed) { + None => Err(Error), + Some(_) => Ok(*seed), + } + } + + fn generate_ecdsa_seed(&mut self) -> Result<[u8; 32], Error> { + let mut seed = [0; 32]; + SecKey::gensk(self.rng()).to_bytes(&mut seed); + Ok(seed) + } + + fn reset(&mut self) -> Result<(), Error> { + Ok(self.store().remove(STORAGE_KEY)?) + } +} + +/// Wrapper for master keys. +struct MasterKeys { + /// Master encryption key. + encryption: [u8; 32], + + /// Master authentication key. + authentication: [u8; 32], +} + +fn get_master_keys(env: &mut impl Env) -> Result { + let master_keys = match env.store().find(STORAGE_KEY)? { + Some(x) => x, + None => { + let master_encryption_key = env.rng().gen_uniform_u8x32(); + let master_authentication_key = env.rng().gen_uniform_u8x32(); + let mut master_keys = Vec::with_capacity(64); + master_keys.extend_from_slice(&master_encryption_key); + master_keys.extend_from_slice(&master_authentication_key); + env.store().insert(STORAGE_KEY, &master_keys)?; + master_keys + } + }; + if master_keys.len() != 64 { + return Err(Error); + } + Ok(MasterKeys { + encryption: *array_ref![master_keys, 0, 32], + authentication: *array_ref![master_keys, 32, 32], + }) +} + +impl From for Error { + fn from(_: StoreError) -> Self { + Error + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_key_store() { + let mut env = crate::env::test::TestEnv::new(); + let key_store = env.key_store(); + + // Master keys are well-defined and stable. + let encryption_key = key_store.key_handle_encryption().unwrap(); + let authentication_key = key_store.key_handle_authentication().unwrap(); + assert_eq!(key_store.key_handle_encryption(), Ok(encryption_key)); + assert_eq!( + key_store.key_handle_authentication(), + Ok(authentication_key) + ); + + // ECDSA seeds are well-defined and stable. + let ecdsa_seed = key_store.generate_ecdsa_seed().unwrap(); + let ecdsa_key = key_store.derive_ecdsa(&ecdsa_seed).unwrap(); + assert_eq!(key_store.derive_ecdsa(&ecdsa_seed), Ok(ecdsa_key)); + + // Master keys change after reset. We don't require this for ECDSA seeds because it's not + // the case, but it might be better. + key_store.reset().unwrap(); + assert!(key_store.key_handle_encryption().unwrap() != encryption_key); + assert!(key_store.key_handle_authentication().unwrap() != authentication_key); + } +} diff --git a/apps/vault/src/api/mod.rs b/apps/vault/src/api/mod.rs new file mode 100644 index 000000000..1239c1123 --- /dev/null +++ b/apps/vault/src/api/mod.rs @@ -0,0 +1,26 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! APIs for the environment. +//! +//! The [environment](crate::env::Env) is split into components. Each component has an API described +//! by a trait. This module gathers the API of those components. + +pub mod attestation_store; +pub mod connection; +pub mod customization; +pub mod firmware_protection; +pub mod key_store; +pub mod upgrade_storage; +pub mod user_presence; diff --git a/apps/vault/src/api/upgrade_storage/helper.rs b/apps/vault/src/api/upgrade_storage/helper.rs new file mode 100644 index 000000000..c561fb0cf --- /dev/null +++ b/apps/vault/src/api/upgrade_storage/helper.rs @@ -0,0 +1,385 @@ +// Copyright 2019-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// For compiling with std outside of tests. +#![cfg_attr(feature = "std", allow(dead_code))] +#![allow(dead_code)] + +use std::vec::Vec; +use core::iter::Iterator; +use persistent_store::{StorageError, StorageResult}; + +/// Reads a slice from a list of slices. +/// +/// The returned slice contains the interval `[start, start+length)`. +/// +/// # Errors +/// +/// Returns [`StorageError::OutOfBounds`] if the range is not within exactly one slice. +pub fn find_slice<'a>( + slices: &'a [&'a [u8]], + mut start: usize, + length: usize, +) -> StorageResult<&'a [u8]> { + for slice in slices { + if start >= slice.len() { + start -= slice.len(); + continue; + } + if start + length > slice.len() { + break; + } + return Ok(&slice[start..][..length]); + } + Err(StorageError::OutOfBounds) +} + +/// Checks whether the address is aligned with the block size. +/// +/// Requires `block_size` to be a power of two. +pub fn is_aligned(block_size: usize, address: usize) -> bool { + debug_assert!(block_size.is_power_of_two()); + address & (block_size - 1) == 0 +} + +/// A range implementation using start and length. +/// +/// The range is treated as the interval `[start, start + length)`. +/// All objects with length of 0, regardless of the start value, are considered empty. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ModRange { + start: usize, + length: usize, +} + +impl ModRange { + /// Returns a new range of given start and length. + pub fn new(start: usize, length: usize) -> ModRange { + ModRange { start, length } + } + + /// Create a new empty range. + pub fn new_empty() -> ModRange { + ModRange::new(0, 0) + } + + /// Returns the start of the range. + pub fn start(&self) -> usize { + self.start + } + + /// Returns the length of the range. + pub fn length(&self) -> usize { + self.length + } + + /// Returns whether this range contains any addresses. + pub fn is_empty(&self) -> bool { + self.length == 0 + } + + /// Returns the disjoint union with the other range, if consecutive. + /// + /// Appending empty ranges is not possible. + /// Appending to the empty range returns the other range. + /// + /// Returns true if successful. + pub fn append(&mut self, other: &ModRange) -> bool { + if self.is_empty() { + self.start = other.start; + self.length = other.length; + return true; + } + if other.is_empty() { + return false; + } + if self.start >= other.start { + return false; + } + if self.length != other.start - self.start { + return false; + } + if let Some(new_length) = self.length.checked_add(other.length) { + self.length = new_length; + true + } else { + false + } + } + + /// Helper function to check whether a range starts within another. + fn starts_inside(&self, range: &ModRange) -> bool { + !range.is_empty() && self.start >= range.start && self.start - range.start < range.length + } + + /// Returns whether the given range has intersects. + /// + /// Mathematically, we calculate whether: `self ∩ range ≠ ∅`. + pub fn intersects_range(&self, range: &ModRange) -> bool { + self.starts_inside(range) || range.starts_inside(self) + } + + /// Returns whether the given range is fully contained. + /// + /// Mathematically, we calculate whether: `self ∩ range = range`. + pub fn contains_range(&self, range: &ModRange) -> bool { + range.is_empty() + || (self.start <= range.start + && range.length <= self.length + && range.start - self.start <= self.length - range.length) + } + + /// Returns an iterator for all contained numbers that are divisible by the modulus. + pub fn aligned_iter(&self, modulus: usize) -> impl Iterator { + (self.start..=usize::MAX) + .take(self.length) + // Skip the minimum number of elements to align. + .skip((modulus - self.start % modulus) % modulus) + // Only return aligned elements. + .step_by(modulus) + } +} + +pub struct Partition { + ranges: Vec, +} + +impl Partition { + pub fn new() -> Partition { + Partition { ranges: Vec::new() } + } + + /// Total length of all ranges. + pub fn length(&self) -> usize { + self.ranges.iter().map(|r| r.length()).sum() + } + + /// Appends the given range. + /// + /// Ranges should be appending with ascending start addresses. + pub fn append(&mut self, range: ModRange) -> bool { + if let Some(last_range) = self.ranges.last_mut() { + if range.start() <= last_range.start() + || range.start() - last_range.start() < last_range.length() + { + return false; + } + if !last_range.append(&range) { + self.ranges.push(range); + } + } else { + self.ranges.push(range); + } + true + } + + /// Returns the start address that corresponds to the given offset. + /// + /// If the offset bigger than the accumulated length or the requested slice doesn't fit a + /// connected component, return `None`. + pub fn find_address(&self, mut offset: usize, length: usize) -> Option { + for range in &self.ranges { + if offset < range.length() { + return if range.length() - offset >= length { + Some(range.start() + offset) + } else { + None + }; + } + offset -= range.length() + } + None + } + + pub fn ranges_from(&self, start_address: usize) -> Vec { + let mut result = Vec::new(); + for range in &self.ranges { + match start_address.checked_sub(range.start()) { + None | Some(0) => result.push(range.clone()), + Some(offset) => { + if range.length() > offset { + result.push(ModRange::new(start_address, range.length() - offset)); + } + } + } + } + result + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn find_slice_ok() { + assert_eq!( + find_slice(&[&[1, 2, 3, 4]], 0, 4).ok(), + Some(&[1u8, 2, 3, 4] as &[u8]) + ); + assert_eq!( + find_slice(&[&[1, 2, 3, 4], &[5, 6]], 1, 2).ok(), + Some(&[2u8, 3] as &[u8]) + ); + assert_eq!( + find_slice(&[&[1, 2, 3, 4], &[5, 6]], 4, 2).ok(), + Some(&[5u8, 6] as &[u8]) + ); + assert_eq!( + find_slice(&[&[1, 2, 3, 4], &[5, 6]], 4, 0).ok(), + Some(&[] as &[u8]) + ); + assert!(find_slice(&[], 0, 1).is_err()); + assert!(find_slice(&[&[1, 2, 3, 4], &[5, 6]], 6, 0).is_err()); + assert!(find_slice(&[&[1, 2, 3, 4], &[5, 6]], 3, 2).is_err()); + } + + #[test] + fn alignment() { + for exponent in 0..8 { + let block_size = 1 << exponent; + for i in 0..10 { + assert!(is_aligned(block_size, block_size * i)); + } + for i in 1..block_size { + assert!(!is_aligned(block_size, block_size + i)); + } + } + } + + #[test] + fn mod_range_parameters() { + let range = ModRange::new(200, 100); + assert_eq!(range.start(), 200); + assert_eq!(range.length(), 100); + assert_eq!(ModRange::new_empty().length(), 0); + } + + #[test] + fn mod_range_is_empty() { + assert!(!ModRange::new(200, 100).is_empty()); + assert!(ModRange::new(200, 0).is_empty()); + assert!(ModRange::new_empty().is_empty()); + assert!(!ModRange::new(usize::MAX, 2).is_empty()); + } + + #[test] + fn mod_range_append() { + let mut range = ModRange::new(200, 100); + assert!(range.append(&ModRange::new(300, 400))); + assert!(range.start() == 200); + assert!(range.length() == 500); + assert!(!range.append(&ModRange::new(499, 400))); + assert!(!range.append(&ModRange::new(501, 400))); + assert!(!range.append(&ModRange::new(300, 400))); + let mut range = ModRange::new_empty(); + assert!(range.append(&ModRange::new(200, 100))); + assert!(range.start() == 200); + assert!(range.length() == 100); + } + + #[test] + fn mod_range_contains_range() { + let range = ModRange::new(200, 100); + assert!(!range.contains_range(&ModRange::new(199, 100))); + assert!(!range.contains_range(&ModRange::new(201, 100))); + assert!(!range.contains_range(&ModRange::new(199, 99))); + assert!(!range.contains_range(&ModRange::new(202, 99))); + assert!(!range.contains_range(&ModRange::new(200, 101))); + assert!(range.contains_range(&ModRange::new(200, 100))); + assert!(range.contains_range(&ModRange::new(200, 99))); + assert!(range.contains_range(&ModRange::new(201, 99))); + assert!(ModRange::new_empty().contains_range(&ModRange::new_empty())); + assert!(ModRange::new(usize::MAX, 1).contains_range(&ModRange::new(usize::MAX, 1))); + assert!(ModRange::new(usize::MAX, 2).contains_range(&ModRange::new(usize::MAX, 2))); + } + + #[test] + fn mod_range_intersects_range() { + let range = ModRange::new(200, 100); + assert!(range.intersects_range(&ModRange::new(200, 1))); + assert!(range.intersects_range(&ModRange::new(299, 1))); + assert!(!range.intersects_range(&ModRange::new(199, 1))); + assert!(!range.intersects_range(&ModRange::new(300, 1))); + assert!(!ModRange::new_empty().intersects_range(&ModRange::new_empty())); + assert!(!ModRange::new_empty().intersects_range(&ModRange::new(200, 100))); + assert!(!ModRange::new(200, 100).intersects_range(&ModRange::new_empty())); + assert!(ModRange::new(usize::MAX, 1).intersects_range(&ModRange::new(usize::MAX, 1))); + assert!(ModRange::new(usize::MAX, 2).intersects_range(&ModRange::new(usize::MAX, 2))); + } + + #[test] + fn mod_range_aligned_iter() { + let mut iter = ModRange::new(200, 100).aligned_iter(100); + assert_eq!(iter.next(), Some(200)); + assert_eq!(iter.next(), None); + let mut iter = ModRange::new(200, 101).aligned_iter(100); + assert_eq!(iter.next(), Some(200)); + assert_eq!(iter.next(), Some(300)); + assert_eq!(iter.next(), None); + let mut iter = ModRange::new(199, 100).aligned_iter(100); + assert_eq!(iter.next(), Some(200)); + assert_eq!(iter.next(), None); + let mut iter = ModRange::new(201, 99).aligned_iter(100); + assert_eq!(iter.next(), None); + let mut iter = ModRange::new(usize::MAX - 16, 20).aligned_iter(16); + assert_eq!(iter.next(), Some(0xffff_ffff_ffff_fff0)); + assert_eq!(iter.next(), None); + } + + #[test] + fn partition_append() { + let mut partition = Partition::new(); + partition.append(ModRange::new(0x4000, 0x1000)); + partition.append(ModRange::new(0x20000, 0x20000)); + partition.append(ModRange::new(0x40000, 0x20000)); + assert_eq!(partition.find_address(0, 1), Some(0x4000)); + assert_eq!(partition.length(), 0x41000); + } + + #[test] + fn partition_find_address() { + let mut partition = Partition::new(); + partition.append(ModRange::new(0x4000, 0x1000)); + partition.append(ModRange::new(0x20000, 0x20000)); + partition.append(ModRange::new(0x40000, 0x20000)); + assert_eq!(partition.find_address(0, 0x1000), Some(0x4000)); + assert_eq!(partition.find_address(0x1000, 0x1000), Some(0x20000)); + assert_eq!(partition.find_address(0x20000, 0x1000), Some(0x3F000)); + assert_eq!(partition.find_address(0x21000, 0x1000), Some(0x40000)); + assert_eq!(partition.find_address(0x40000, 0x1000), Some(0x5F000)); + assert_eq!(partition.find_address(0x41000, 0x1000), None); + assert_eq!(partition.find_address(0x40000, 0x2000), None); + } + + #[test] + fn partition_ranges_from() { + let mut partition = Partition::new(); + partition.append(ModRange::new(0x4000, 0x1000)); + partition.append(ModRange::new(0x20000, 0x20000)); + partition.append(ModRange::new(0x40000, 0x20000)); + let all_ranges = partition.ranges_from(0); + let from_start_ranges = partition.ranges_from(0x4000); + assert_eq!(&all_ranges, &from_start_ranges); + assert_eq!(all_ranges.len(), 2); + assert_eq!(all_ranges[0], ModRange::new(0x4000, 0x1000)); + assert_eq!(all_ranges[1], ModRange::new(0x20000, 0x40000)); + let second_range = partition.ranges_from(0x20000); + let same_second_range = partition.ranges_from(0x1F000); + assert_eq!(&second_range, &same_second_range); + assert_eq!(&second_range, &all_ranges[1..]); + let partial_range = partition.ranges_from(0x30000); + assert_eq!(partial_range[0], ModRange::new(0x30000, 0x30000)); + } +} diff --git a/apps/vault/src/api/upgrade_storage/mod.rs b/apps/vault/src/api/upgrade_storage/mod.rs new file mode 100644 index 000000000..f2a1fdeda --- /dev/null +++ b/apps/vault/src/api/upgrade_storage/mod.rs @@ -0,0 +1,39 @@ +// Copyright 2021-2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::vec::Vec; +use persistent_store::StorageResult; + +pub(crate) mod helper; + +/// Accessors to storage locations used for upgrading from a CTAP command. +pub trait UpgradeStorage { + /// Processes the given data as part of an upgrade. + /// + /// The offset indicates the data location inside the bundle. + /// + /// # Errors + /// + /// - Returns [`StorageError::OutOfBounds`] if the data does not fit. + /// - Returns [`StorageError::CustomError`] if any Metadata or other check fails. + fn write_bundle(&mut self, offset: usize, data: Vec) -> StorageResult<()>; + + /// Returns an identifier for the requested bundle. + /// + /// Use this to determine whether you are writing to A or B. + fn bundle_identifier(&self) -> u32; + + /// Returns the currently running firmware version. + fn running_firmware_version(&self) -> u64; +} diff --git a/apps/vault/src/api/user_presence.rs b/apps/vault/src/api/user_presence.rs new file mode 100644 index 000000000..e8f46fa86 --- /dev/null +++ b/apps/vault/src/api/user_presence.rs @@ -0,0 +1,52 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::time::Duration; + +#[derive(Debug)] +pub enum UserPresenceError { + /// User explicitly declined user presence check. + Declined, + /// User presence check was canceled by User Agent. + Canceled, + /// User presence check timed out. + Timeout, +} + +pub type UserPresenceResult = Result<(), UserPresenceError>; + +pub trait UserPresence { + /// Initializes for a user presence check. + /// + /// Must eventually be followed by a call to [`Self::check_complete`]. + fn check_init(&mut self); + + /// Waits until user presence is confirmed, rejected, or the given timeout expires. + /// + /// Must be called between calls to [`Self::check_init`] and [`Self::check_complete`]. + #[cfg(feature="xous")] + fn wait_with_timeout(&mut self, timeout: Duration, reason: Option, cid: [u8; 4]) -> UserPresenceResult; + #[cfg(not(feature="xous"))] + fn wait_with_timeout(&mut self, timeout: Duration) -> UserPresenceResult; + + /// CTAP1 waits don't require keepalive packets. They just block. However, they also have + /// an `app_id` which we have to lookup to turn into a human-readable identifier. + #[cfg(feature="xous")] + fn wait_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool; + + /// Finalizes a user presence check. + /// + /// Must be called after [`Self::check_init`]. + fn check_complete(&mut self); +} diff --git a/apps/vault/src/ctap/apdu.rs b/apps/vault/src/ctap/apdu.rs index 31d8ff4c6..8fe733380 100644 --- a/apps/vault/src/ctap/apdu.rs +++ b/apps/vault/src/ctap/apdu.rs @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2020-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,13 +15,14 @@ use std::vec::Vec; use byteorder::{BigEndian, ByteOrder}; use core::convert::TryFrom; -use crate::ctap::array_ref; +use arrayref::array_ref; + +use crate::api::attestation_store; const APDU_HEADER_LEN: usize = 4; -#[cfg_attr(test, derive(Clone))] +#[derive(Clone, Debug, PartialEq, Eq)] #[allow(non_camel_case_types, dead_code)] -#[derive(PartialEq, Debug)] pub enum ApduStatusCode { SW_SUCCESS = 0x90_00, /// Command successfully executed; 'XX' bytes of data are @@ -31,6 +32,7 @@ pub enum ApduStatusCode { SW_WRONG_DATA = 0x6a_80, SW_WRONG_LENGTH = 0x67_00, SW_COND_USE_NOT_SATISFIED = 0x69_85, + SW_COMMAND_NOT_ALLOWED = 0x69_86, SW_FILE_NOT_FOUND = 0x6a_82, SW_INCORRECT_P1P2 = 0x6a_86, /// Instruction code not supported or invalid @@ -45,6 +47,17 @@ impl From for u16 { } } +impl From for ApduStatusCode { + fn from(error: attestation_store::Error) -> Self { + use attestation_store::Error; + match error { + Error::Storage => ApduStatusCode::SW_MEMERR, + Error::Internal => ApduStatusCode::SW_INTERNAL_EXCEPTION, + Error::NoSupport => ApduStatusCode::SW_INTERNAL_EXCEPTION, + } + } +} + #[allow(dead_code)] pub enum ApduInstructions { Select = 0xA4, @@ -52,9 +65,8 @@ pub enum ApduInstructions { GetResponse = 0xC0, } -#[cfg_attr(test, derive(Clone, Debug))] +#[derive(Clone, Debug, Default, PartialEq, Eq)] #[allow(dead_code)] -#[derive(Default, PartialEq)] pub struct ApduHeader { pub cla: u8, pub ins: u8, @@ -73,9 +85,8 @@ impl From<&[u8; APDU_HEADER_LEN]> for ApduHeader { } } -#[cfg_attr(test, derive(Clone, Debug))] -#[derive(PartialEq)] -/// The Apdu cases +#[derive(Clone, Debug, PartialEq, Eq)] +/// The APDU cases pub enum Case { Le1, Lc1Data, @@ -86,18 +97,16 @@ pub enum Case { Le3, } -#[cfg_attr(test, derive(Clone, Debug))] +#[derive(Clone, Debug, PartialEq, Eq)] #[allow(dead_code)] -#[derive(PartialEq)] pub enum ApduType { Instruction, Short(Case), Extended(Case), } -#[cfg_attr(test, derive(Clone, Debug))] +#[derive(Clone, Debug, PartialEq, Eq)] #[allow(dead_code)] -#[derive(PartialEq)] pub struct Apdu { pub header: ApduHeader, pub lc: u16, @@ -187,7 +196,7 @@ impl TryFrom<&[u8]> for Apdu { if byte_0 == 0 && extended_apdu_le_len <= 3 { // If first byte is zero AND the next two bytes can be parsed as a big-endian // length that covers the rest of the block (plus few additional bytes for Le), we - // have an extended-length Apdu + // have an extended-length APDU let last_byte: u32 = (*payload.last().unwrap()).into(); return Ok(Apdu { header: array_ref!(header, 0, APDU_HEADER_LEN).into(), diff --git a/apps/vault/src/ctap/client_pin.rs b/apps/vault/src/ctap/client_pin.rs new file mode 100644 index 000000000..abf626617 --- /dev/null +++ b/apps/vault/src/ctap/client_pin.rs @@ -0,0 +1,1711 @@ +// Copyright 2020-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::time::Instant; +use super::command::AuthenticatorClientPinParameters; +use super::data_formats::{ + ok_or_missing, ClientPinSubCommand, CoseKey, GetAssertionHmacSecretInput, PinUvAuthProtocol, +}; +use super::pin_protocol::{verify_pin_uv_auth_token, PinProtocol, SharedSecret}; +use super::response::{AuthenticatorClientPinResponse, ResponseData}; +use super::status_code::Ctap2StatusCode; +use super::token_state::PinUvAuthTokenState; +use crate::api::customization::Customization; +use crate::ctap::storage; +use crate::env::Env; +use std::boxed::Box; +use std::str; +use std::string::String; +use std::vec::Vec; +use ctap_crypto::hmac::hmac_256; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; +#[cfg(test)] +use enum_iterator::IntoEnumIterator; +use ctap_crypto::rng256::Rng256; +use subtle::ConstantTimeEq; + +/// The prefix length of the PIN hash that is stored and compared. +/// +/// The code assumes that this value is a multiple of the AES block length, fits +/// an u8 and is at most as long as a SHA256. The value is fixed for all PIN +/// protocols. +pub const PIN_AUTH_LENGTH: usize = 16; + +/// The length of the pinUvAuthToken used throughout PIN protocols. +/// +/// The code assumes that this value is a multiple of the AES block length. It +/// is fixed since CTAP2.1, and the specification suggests that it coincides +/// with the HMAC key length. Therefore a change would require a more general +/// HMAC implementation. +pub const PIN_TOKEN_LENGTH: usize = 32; + +/// The length of the encrypted PINs when received by SetPin or ChangePin. +/// +/// The code assumes that this value is a multiple of the AES block length. It +/// is fixed since CTAP2.1. +const PIN_PADDED_LENGTH: usize = 64; + +/// Decrypts the new_pin_enc and outputs the found PIN. +fn decrypt_pin( + shared_secret: &dyn SharedSecret, + new_pin_enc: Vec, +) -> Result, Ctap2StatusCode> { + let decrypted_pin = shared_secret.decrypt(&new_pin_enc)?; + if decrypted_pin.len() != PIN_PADDED_LENGTH { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + // In CTAP 2.1, the specification changed. The new wording might lead to + // different behavior when there are non-zero bytes after zero bytes. + // This implementation consistently ignores those degenerate cases. + Ok(decrypted_pin.into_iter().take_while(|&c| c != 0).collect()) +} + +/// Stores a hash prefix of the new PIN in the persistent storage, if correct. +/// +/// The new PIN is passed encrypted, so it is first decrypted and stripped from +/// padding. Next, it is checked against the PIN policy. Last, it is hashed and +/// truncated for persistent storage. +fn check_and_store_new_pin( + env: &mut impl Env, + shared_secret: &dyn SharedSecret, + new_pin_enc: Vec, +) -> Result<(), Ctap2StatusCode> { + let pin = decrypt_pin(shared_secret, new_pin_enc)?; + let min_pin_length = storage::min_pin_length(env)? as usize; + let pin_length = str::from_utf8(&pin).unwrap_or("").chars().count(); + if pin_length < min_pin_length || pin.len() == PIN_PADDED_LENGTH { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION); + } + let mut pin_hash = [0u8; PIN_AUTH_LENGTH]; + pin_hash.copy_from_slice(&Sha256::hash(&pin[..])[..PIN_AUTH_LENGTH]); + // The PIN length is always < PIN_PADDED_LENGTH < 256. + storage::set_pin(env, &pin_hash, pin_length as u8)?; + Ok(()) +} + +#[cfg_attr(test, derive(IntoEnumIterator))] +pub enum PinPermission { + // All variants should use integers with a single bit set. + MakeCredential = 0x01, + GetAssertion = 0x02, + CredentialManagement = 0x04, + _BioEnrollment = 0x08, + LargeBlobWrite = 0x10, + AuthenticatorConfiguration = 0x20, +} + +pub struct ClientPin { + pin_protocol_v1: PinProtocol, + pin_protocol_v2: PinProtocol, + consecutive_pin_mismatches: u8, + pin_uv_auth_token_state: PinUvAuthTokenState, +} + +impl ClientPin { + pub fn new(rng: &mut impl Rng256) -> ClientPin { + ClientPin { + pin_protocol_v1: PinProtocol::new(rng), + pin_protocol_v2: PinProtocol::new(rng), + consecutive_pin_mismatches: 0, + pin_uv_auth_token_state: PinUvAuthTokenState::new(), + } + } + + /// Gets a reference to the PIN protocol of the given version. + fn get_pin_protocol(&self, pin_uv_auth_protocol: PinUvAuthProtocol) -> &PinProtocol { + match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => &self.pin_protocol_v1, + PinUvAuthProtocol::V2 => &self.pin_protocol_v2, + } + } + + /// Gets a mutable reference to the PIN protocol of the given version. + fn get_mut_pin_protocol( + &mut self, + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> &mut PinProtocol { + match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => &mut self.pin_protocol_v1, + PinUvAuthProtocol::V2 => &mut self.pin_protocol_v2, + } + } + + /// Computes the shared secret for the given version. + fn get_shared_secret( + &self, + pin_uv_auth_protocol: PinUvAuthProtocol, + key_agreement: CoseKey, + ) -> Result, Ctap2StatusCode> { + self.get_pin_protocol(pin_uv_auth_protocol) + .decapsulate(key_agreement, pin_uv_auth_protocol) + } + + /// Checks the given encrypted PIN hash against the stored PIN hash. + /// + /// Decrypts the encrypted pin_hash and compares it to the stored pin_hash. + /// Resets or decreases the PIN retries, depending on success or failure. + /// Also, in case of failure, the key agreement key is randomly reset. + fn verify_pin_hash_enc( + &mut self, + env: &mut impl Env, + pin_uv_auth_protocol: PinUvAuthProtocol, + shared_secret: &dyn SharedSecret, + pin_hash_enc: Vec, + ) -> Result<(), Ctap2StatusCode> { + match storage::pin_hash(env)? { + Some(pin_hash) => { + if self.consecutive_pin_mismatches >= 3 { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED); + } + storage::decr_pin_retries(env)?; + let pin_hash_dec = shared_secret + .decrypt(&pin_hash_enc) + .map_err(|_| Ctap2StatusCode::CTAP2_ERR_PIN_INVALID)?; + + if !bool::from(pin_hash.ct_eq(&pin_hash_dec)) { + self.get_mut_pin_protocol(pin_uv_auth_protocol) + .regenerate(env.rng()); + if storage::pin_retries(env)? == 0 { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); + } + self.consecutive_pin_mismatches += 1; + if self.consecutive_pin_mismatches >= 3 { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED); + } + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID); + } + } + // This status code is not explicitly mentioned in the specification. + None => return Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED), + } + storage::reset_pin_retries(env)?; + self.consecutive_pin_mismatches = 0; + Ok(()) + } + + fn process_get_pin_retries( + &self, + env: &mut impl Env, + ) -> Result { + Ok(AuthenticatorClientPinResponse { + key_agreement: None, + pin_uv_auth_token: None, + retries: Some(storage::pin_retries(env)? as u64), + power_cycle_state: Some(self.consecutive_pin_mismatches >= 3), + }) + } + + fn process_get_key_agreement( + &self, + client_pin_params: AuthenticatorClientPinParameters, + ) -> Result { + let key_agreement = Some( + self.get_pin_protocol(client_pin_params.pin_uv_auth_protocol) + .get_public_key(), + ); + Ok(AuthenticatorClientPinResponse { + key_agreement, + pin_uv_auth_token: None, + retries: None, + power_cycle_state: None, + }) + } + + fn process_set_pin( + &mut self, + env: &mut impl Env, + client_pin_params: AuthenticatorClientPinParameters, + ) -> Result<(), Ctap2StatusCode> { + let AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + key_agreement, + pin_uv_auth_param, + new_pin_enc, + .. + } = client_pin_params; + let key_agreement = ok_or_missing(key_agreement)?; + let pin_uv_auth_param = ok_or_missing(pin_uv_auth_param)?; + let new_pin_enc = ok_or_missing(new_pin_enc)?; + + if storage::pin_hash(env)?.is_some() { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); + } + let shared_secret = self.get_shared_secret(pin_uv_auth_protocol, key_agreement)?; + shared_secret.verify(&new_pin_enc, &pin_uv_auth_param)?; + + check_and_store_new_pin(env, shared_secret.as_ref(), new_pin_enc)?; + storage::reset_pin_retries(env)?; + Ok(()) + } + + fn process_change_pin( + &mut self, + env: &mut impl Env, + client_pin_params: AuthenticatorClientPinParameters, + ) -> Result<(), Ctap2StatusCode> { + let AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + key_agreement, + pin_uv_auth_param, + new_pin_enc, + pin_hash_enc, + .. + } = client_pin_params; + let key_agreement = ok_or_missing(key_agreement)?; + let pin_uv_auth_param = ok_or_missing(pin_uv_auth_param)?; + let new_pin_enc = ok_or_missing(new_pin_enc)?; + let pin_hash_enc = ok_or_missing(pin_hash_enc)?; + + if storage::pin_retries(env)? == 0 { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); + } + let shared_secret = self.get_shared_secret(pin_uv_auth_protocol, key_agreement)?; + let mut auth_param_data = new_pin_enc.clone(); + auth_param_data.extend(&pin_hash_enc); + shared_secret.verify(&auth_param_data, &pin_uv_auth_param)?; + self.verify_pin_hash_enc( + env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc, + )?; + + check_and_store_new_pin(env, shared_secret.as_ref(), new_pin_enc)?; + self.pin_protocol_v1.reset_pin_uv_auth_token(env.rng()); + self.pin_protocol_v2.reset_pin_uv_auth_token(env.rng()); + Ok(()) + } + + fn process_get_pin_token( + &mut self, + env: &mut impl Env, + client_pin_params: AuthenticatorClientPinParameters, + now: Instant, + ) -> Result { + let AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + key_agreement, + pin_hash_enc, + permissions, + permissions_rp_id, + .. + } = client_pin_params; + let key_agreement = ok_or_missing(key_agreement)?; + let pin_hash_enc = ok_or_missing(pin_hash_enc)?; + if permissions.is_some() || permissions_rp_id.is_some() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + + if storage::pin_retries(env)? == 0 { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); + } + let shared_secret = self.get_shared_secret(pin_uv_auth_protocol, key_agreement)?; + self.verify_pin_hash_enc( + env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc, + )?; + if storage::has_force_pin_change(env)? { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID); + } + + self.pin_protocol_v1.reset_pin_uv_auth_token(env.rng()); + self.pin_protocol_v2.reset_pin_uv_auth_token(env.rng()); + self.pin_uv_auth_token_state + .begin_using_pin_uv_auth_token(now); + self.pin_uv_auth_token_state.set_default_permissions(); + let pin_uv_auth_token = shared_secret.encrypt( + env.rng(), + self.get_pin_protocol(pin_uv_auth_protocol) + .get_pin_uv_auth_token(), + )?; + + Ok(AuthenticatorClientPinResponse { + key_agreement: None, + pin_uv_auth_token: Some(pin_uv_auth_token), + retries: None, + power_cycle_state: None, + }) + } + + fn process_get_pin_uv_auth_token_using_uv_with_permissions( + &self, + // If you want to support local user verification, implement this function. + // Lacking a fingerprint reader, this subcommand is currently unsupported. + _client_pin_params: AuthenticatorClientPinParameters, + ) -> Result { + // User verification is only supported through PIN currently. + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND) + } + + fn process_get_uv_retries(&self) -> Result { + // User verification is only supported through PIN currently. + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND) + } + + fn process_get_pin_uv_auth_token_using_pin_with_permissions( + &mut self, + env: &mut impl Env, + mut client_pin_params: AuthenticatorClientPinParameters, + now: Instant, + ) -> Result { + // Mutating client_pin_params is just an optimization to move it into + // process_get_pin_token, without cloning permissions_rp_id here. + // getPinToken requires permissions* to be None. + let permissions = ok_or_missing(client_pin_params.permissions.take())?; + let permissions_rp_id = client_pin_params.permissions_rp_id.take(); + + if permissions == 0 { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + // This check is not mentioned protocol steps, but mentioned in a side note. + if permissions & 0x03 != 0 && permissions_rp_id.is_none() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + + let response = self.process_get_pin_token(env, client_pin_params, now)?; + self.pin_uv_auth_token_state.set_permissions(permissions); + self.pin_uv_auth_token_state + .set_permissions_rp_id(permissions_rp_id); + + Ok(response) + } + + /// Processes the authenticatorClientPin command. + pub fn process_command( + &mut self, + env: &mut impl Env, + client_pin_params: AuthenticatorClientPinParameters, + now: Instant, + ) -> Result { + if !env.customization().allows_pin_protocol_v1() + && client_pin_params.pin_uv_auth_protocol == PinUvAuthProtocol::V1 + { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + let response = match client_pin_params.sub_command { + ClientPinSubCommand::GetPinRetries => Some(self.process_get_pin_retries(env)?), + ClientPinSubCommand::GetKeyAgreement => { + Some(self.process_get_key_agreement(client_pin_params)?) + } + ClientPinSubCommand::SetPin => { + self.process_set_pin(env, client_pin_params)?; + None + } + ClientPinSubCommand::ChangePin => { + self.process_change_pin(env, client_pin_params)?; + None + } + ClientPinSubCommand::GetPinToken => { + Some(self.process_get_pin_token(env, client_pin_params, now)?) + } + ClientPinSubCommand::GetPinUvAuthTokenUsingUvWithPermissions => Some( + self.process_get_pin_uv_auth_token_using_uv_with_permissions(client_pin_params)?, + ), + ClientPinSubCommand::GetUvRetries => Some(self.process_get_uv_retries()?), + ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions => Some( + self.process_get_pin_uv_auth_token_using_pin_with_permissions( + env, + client_pin_params, + now, + )?, + ), + }; + Ok(ResponseData::AuthenticatorClientPin(response)) + } + + /// Verifies the HMAC for the pinUvAuthToken of the given version. + pub fn verify_pin_uv_auth_token( + &self, + hmac_contents: &[u8], + pin_uv_auth_param: &[u8], + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> Result<(), Ctap2StatusCode> { + if !self.pin_uv_auth_token_state.is_in_use() { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); + } + verify_pin_uv_auth_token( + self.get_pin_protocol(pin_uv_auth_protocol) + .get_pin_uv_auth_token(), + hmac_contents, + pin_uv_auth_param, + pin_uv_auth_protocol, + ) + } + + /// Resets all held state. + pub fn reset(&mut self, rng: &mut impl Rng256) { + self.pin_protocol_v1.regenerate(rng); + self.pin_protocol_v1.reset_pin_uv_auth_token(rng); + self.pin_protocol_v2.regenerate(rng); + self.pin_protocol_v2.reset_pin_uv_auth_token(rng); + self.consecutive_pin_mismatches = 0; + self.pin_uv_auth_token_state.stop_using_pin_uv_auth_token(); + } + + /// Verifies, computes and encrypts the HMAC-secret outputs. + /// + /// The salt_enc is + /// - verified with the shared secret and salt_auth, + /// - decrypted with the shared secret, + /// - HMAC'ed with cred_random. + /// The length of the output matches salt_enc and has to be 1 or 2 blocks of + /// 32 byte. + pub fn process_hmac_secret( + &self, + rng: &mut impl Rng256, + hmac_secret_input: GetAssertionHmacSecretInput, + cred_random: &[u8; 32], + ) -> Result, Ctap2StatusCode> { + let GetAssertionHmacSecretInput { + key_agreement, + salt_enc, + salt_auth, + pin_uv_auth_protocol, + } = hmac_secret_input; + let shared_secret = self + .get_pin_protocol(pin_uv_auth_protocol) + .decapsulate(key_agreement, pin_uv_auth_protocol)?; + shared_secret.verify(&salt_enc, &salt_auth)?; + + let decrypted_salts = shared_secret.decrypt(&salt_enc)?; + if decrypted_salts.len() != 32 && decrypted_salts.len() != 64 { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + let mut output = hmac_256::(cred_random, &decrypted_salts[..32]).to_vec(); + if decrypted_salts.len() == 64 { + let mut output2 = hmac_256::(cred_random, &decrypted_salts[32..]).to_vec(); + output.append(&mut output2); + } + shared_secret.encrypt(rng, &output) + } + + /// Consumes flags and permissions related to the pinUvAuthToken. + pub fn clear_token_flags(&mut self) { + self.pin_uv_auth_token_state.clear_user_verified_flag(); + self.pin_uv_auth_token_state + .clear_pin_uv_auth_token_permissions_except_lbw(); + } + + /// Updates the running timers, triggers timeout events. + pub fn update_timeouts(&mut self, now: Instant) { + self.pin_uv_auth_token_state + .pin_uv_auth_token_usage_timer_observer(now); + } + + /// Checks if user verification is cached for use of the pinUvAuthToken. + pub fn check_user_verified_flag(&mut self) -> Result<(), Ctap2StatusCode> { + if self.pin_uv_auth_token_state.get_user_verified_flag_value() { + Ok(()) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + } + } + + /// Check if the required command's token permission is granted. + pub fn has_permission(&self, permission: PinPermission) -> Result<(), Ctap2StatusCode> { + self.pin_uv_auth_token_state.has_permission(permission) + } + + /// Check if no RP ID is associated with the token permission. + pub fn has_no_rp_id_permission(&self) -> Result<(), Ctap2StatusCode> { + self.pin_uv_auth_token_state.has_no_permissions_rp_id() + } + + /// Check if no or the passed RP ID is associated with the token permission. + pub fn has_no_or_rp_id_permission(&mut self, rp_id: &str) -> Result<(), Ctap2StatusCode> { + self.pin_uv_auth_token_state + .has_no_permissions_rp_id() + .or_else(|_| self.pin_uv_auth_token_state.has_permissions_rp_id(rp_id)) + } + + /// Check if no RP ID is associated with the token permission, or it matches the hash. + pub fn has_no_or_rp_id_hash_permission( + &self, + rp_id_hash: &[u8], + ) -> Result<(), Ctap2StatusCode> { + self.pin_uv_auth_token_state + .has_no_permissions_rp_id() + .or_else(|_| { + self.pin_uv_auth_token_state + .has_permissions_rp_id_hash(rp_id_hash) + }) + } + + /// Check if the passed RP ID is associated with the token permission. + /// + /// If no RP ID is associated, associate the passed RP ID as a side effect. + pub fn ensure_rp_id_permission(&mut self, rp_id: &str) -> Result<(), Ctap2StatusCode> { + if self + .pin_uv_auth_token_state + .has_no_permissions_rp_id() + .is_ok() + { + self.pin_uv_auth_token_state + .set_permissions_rp_id(Some(String::from(rp_id))); + return Ok(()); + } + self.pin_uv_auth_token_state.has_permissions_rp_id(rp_id) + } + + #[cfg(test)] + pub fn new_test( + key_agreement_key: ctap_crypto::ecdh::SecKey, + pin_uv_auth_token: [u8; PIN_TOKEN_LENGTH], + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> ClientPin { + let mut env = crate::env::test::TestEnv::new(); + let (key_agreement_key_v1, key_agreement_key_v2) = match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => (key_agreement_key, ctap_crypto::ecdh::SecKey::gensk(env.rng())), + PinUvAuthProtocol::V2 => (crypto::ecdh::SecKey::gensk(env.rng()), key_agreement_key), + }; + let mut pin_uv_auth_token_state = PinUvAuthTokenState::new(); + pin_uv_auth_token_state.set_permissions(0xFF); + pin_uv_auth_token_state.begin_using_pin_uv_auth_token(Instant::new(0)); + ClientPin { + pin_protocol_v1: PinProtocol::new_test(key_agreement_key_v1, pin_uv_auth_token), + pin_protocol_v2: PinProtocol::new_test(key_agreement_key_v2, pin_uv_auth_token), + consecutive_pin_mismatches: 0, + pin_uv_auth_token_state, + } + } +} + +#[cfg(test)] +mod test { + use super::super::pin_protocol::authenticate_pin_uv_auth_token; + use super::*; + use crate::env::test::TestEnv; + use std::vec; + use std::time::Duration; + + /// Stores a PIN hash corresponding to the dummy PIN "1234". + fn set_standard_pin(env: &mut TestEnv) { + let mut pin = [0u8; 64]; + pin[..4].copy_from_slice(b"1234"); + let mut pin_hash = [0u8; 16]; + pin_hash.copy_from_slice(&Sha256::hash(&pin[..])[..16]); + storage::set_pin(env, &pin_hash, 4).unwrap(); + } + + /// Fails on PINs bigger than 64 bytes. + fn encrypt_pin(shared_secret: &dyn SharedSecret, pin: Vec) -> Vec { + assert!(pin.len() <= 64); + let mut env = TestEnv::new(); + let mut padded_pin = [0u8; 64]; + padded_pin[..pin.len()].copy_from_slice(&pin[..]); + shared_secret.encrypt(env.rng(), &padded_pin).unwrap() + } + + /// Generates a ClientPin instance and a shared secret for testing. + /// + /// The shared secret for the desired PIN protocol is generated in a + /// handshake with itself. The other protocol has a random private key, so + /// tests using the wrong combination of PIN protocol and shared secret + /// should fail. + fn create_client_pin_and_shared_secret( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> (ClientPin, Box) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pk = key_agreement_key.genpk(); + let key_agreement = CoseKey::from(pk); + let pin_uv_auth_token = [0x91; PIN_TOKEN_LENGTH]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + let shared_secret = client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .decapsulate(key_agreement, pin_uv_auth_protocol) + .unwrap(); + (client_pin, shared_secret) + } + + /// Generates standard input parameters to the ClientPin command. + /// + /// All fields are populated for simplicity, even though most are unused. + fn create_client_pin_and_parameters( + pin_uv_auth_protocol: PinUvAuthProtocol, + sub_command: ClientPinSubCommand, + ) -> (ClientPin, AuthenticatorClientPinParameters) { + let mut env = TestEnv::new(); + let (client_pin, shared_secret) = create_client_pin_and_shared_secret(pin_uv_auth_protocol); + + let pin = b"1234"; + let mut padded_pin = [0u8; 64]; + padded_pin[..pin.len()].copy_from_slice(&pin[..]); + let pin_hash = Sha256::hash(&padded_pin); + let new_pin_enc = shared_secret + .as_ref() + .encrypt(env.rng(), &padded_pin) + .unwrap(); + let pin_uv_auth_param = shared_secret.as_ref().authenticate(&new_pin_enc); + let pin_hash_enc = shared_secret + .as_ref() + .encrypt(env.rng(), &pin_hash[..16]) + .unwrap(); + let (permissions, permissions_rp_id) = match sub_command { + ClientPinSubCommand::GetPinUvAuthTokenUsingUvWithPermissions + | ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions => { + (Some(0x03), Some("example.com".to_string())) + } + _ => (None, None), + }; + let params = AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + sub_command, + key_agreement: Some( + client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .get_public_key(), + ), + pin_uv_auth_param: Some(pin_uv_auth_param), + new_pin_enc: Some(new_pin_enc), + pin_hash_enc: Some(pin_hash_enc), + permissions, + permissions_rp_id, + }; + (client_pin, params) + } + + #[test] + fn test_mix_pin_protocols() { + let mut env = TestEnv::new(); + let client_pin = ClientPin::new(env.rng()); + let pin_protocol_v1 = client_pin.get_pin_protocol(PinUvAuthProtocol::V1); + let pin_protocol_v2 = client_pin.get_pin_protocol(PinUvAuthProtocol::V2); + let message = vec![0xAA; 16]; + + let shared_secret_v1 = pin_protocol_v1 + .decapsulate(pin_protocol_v1.get_public_key(), PinUvAuthProtocol::V1) + .unwrap(); + let shared_secret_v2 = pin_protocol_v2 + .decapsulate(pin_protocol_v2.get_public_key(), PinUvAuthProtocol::V2) + .unwrap(); + let ciphertext = shared_secret_v1.encrypt(env.rng(), &message).unwrap(); + let plaintext = shared_secret_v2.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + let ciphertext = shared_secret_v2.encrypt(env.rng(), &message).unwrap(); + let plaintext = shared_secret_v1.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + + let fake_secret_v1 = pin_protocol_v1 + .decapsulate(pin_protocol_v2.get_public_key(), PinUvAuthProtocol::V1) + .unwrap(); + let ciphertext = shared_secret_v1.encrypt(env.rng(), &message).unwrap(); + let plaintext = fake_secret_v1.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + let ciphertext = fake_secret_v1.encrypt(env.rng(), &message).unwrap(); + let plaintext = shared_secret_v1.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + + let fake_secret_v2 = pin_protocol_v2 + .decapsulate(pin_protocol_v1.get_public_key(), PinUvAuthProtocol::V2) + .unwrap(); + let ciphertext = shared_secret_v2.encrypt(env.rng(), &message).unwrap(); + let plaintext = fake_secret_v2.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + let ciphertext = fake_secret_v2.encrypt(env.rng(), &message).unwrap(); + let plaintext = shared_secret_v2.decrypt(&ciphertext).unwrap(); + assert_ne!(&message, &plaintext); + } + + fn test_helper_verify_pin_hash_enc(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + let pin_protocol = client_pin.get_pin_protocol(pin_uv_auth_protocol); + let shared_secret = pin_protocol + .decapsulate(pin_protocol.get_public_key(), pin_uv_auth_protocol) + .unwrap(); + // The PIN is "1234". + let pin_hash = [ + 0x01, 0xD9, 0x88, 0x40, 0x50, 0xBB, 0xD0, 0x7A, 0x23, 0x1A, 0xEB, 0x69, 0xD8, 0x36, + 0xC4, 0x12, + ]; + storage::set_pin(&mut env, &pin_hash, 4).unwrap(); + + let pin_hash_enc = shared_secret + .as_ref() + .encrypt(env.rng(), &pin_hash) + .unwrap(); + assert_eq!( + client_pin.verify_pin_hash_enc( + &mut env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc + ), + Ok(()) + ); + + let pin_hash_enc = vec![0xEE; 16]; + assert_eq!( + client_pin.verify_pin_hash_enc( + &mut env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + + let pin_hash_enc = shared_secret + .as_ref() + .encrypt(env.rng(), &pin_hash) + .unwrap(); + client_pin.consecutive_pin_mismatches = 3; + assert_eq!( + client_pin.verify_pin_hash_enc( + &mut env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED) + ); + client_pin.consecutive_pin_mismatches = 0; + + let pin_hash_enc = vec![0x77; PIN_AUTH_LENGTH - 1]; + assert_eq!( + client_pin.verify_pin_hash_enc( + &mut env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + + let pin_hash_enc = vec![0x77; PIN_AUTH_LENGTH + 1]; + assert_eq!( + client_pin.verify_pin_hash_enc( + &mut env, + pin_uv_auth_protocol, + shared_secret.as_ref(), + pin_hash_enc + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + } + + #[test] + fn test_verify_pin_hash_enc_v1() { + test_helper_verify_pin_hash_enc(PinUvAuthProtocol::V1); + } + + #[test] + fn test_verify_pin_hash_enc_v2() { + test_helper_verify_pin_hash_enc(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_pin_retries(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetPinRetries, + ); + let mut env = TestEnv::new(); + let expected_response = Some(AuthenticatorClientPinResponse { + key_agreement: None, + pin_uv_auth_token: None, + retries: Some(storage::pin_retries(&mut env).unwrap() as u64), + power_cycle_state: Some(false), + }); + assert_eq!( + client_pin.process_command(&mut env, params.clone(), Instant::new(0)), + Ok(ResponseData::AuthenticatorClientPin(expected_response)) + ); + + client_pin.consecutive_pin_mismatches = 3; + let expected_response = Some(AuthenticatorClientPinResponse { + key_agreement: None, + pin_uv_auth_token: None, + retries: Some(storage::pin_retries(&mut env).unwrap() as u64), + power_cycle_state: Some(true), + }); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Ok(ResponseData::AuthenticatorClientPin(expected_response)) + ); + } + + #[test] + fn test_process_get_pin_retries_v1() { + test_helper_process_get_pin_retries(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_pin_retries_v2() { + test_helper_process_get_pin_retries(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_key_agreement(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetKeyAgreement, + ); + let mut env = TestEnv::new(); + let expected_response = Some(AuthenticatorClientPinResponse { + key_agreement: params.key_agreement.clone(), + pin_uv_auth_token: None, + retries: None, + power_cycle_state: None, + }); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Ok(ResponseData::AuthenticatorClientPin(expected_response)) + ); + } + + #[test] + fn test_process_get_key_agreement_v1() { + test_helper_process_get_key_agreement(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_key_agreement_v2() { + test_helper_process_get_key_agreement(PinUvAuthProtocol::V2); + } + + #[test] + fn test_process_get_key_agreement_v1_not_allowed() { + let (mut client_pin, params) = create_client_pin_and_parameters( + PinUvAuthProtocol::V1, + ClientPinSubCommand::GetKeyAgreement, + ); + let mut env = TestEnv::new(); + env.customization_mut().set_allows_pin_protocol_v1(false); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + + fn test_helper_process_set_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, params) = + create_client_pin_and_parameters(pin_uv_auth_protocol, ClientPinSubCommand::SetPin); + let mut env = TestEnv::new(); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Ok(ResponseData::AuthenticatorClientPin(None)) + ); + } + + #[test] + fn test_process_set_pin_v1() { + test_helper_process_set_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_set_pin_v2() { + test_helper_process_set_pin(PinUvAuthProtocol::V2); + } + + fn test_helper_process_change_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, mut params) = + create_client_pin_and_parameters(pin_uv_auth_protocol, ClientPinSubCommand::ChangePin); + let shared_secret = client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .decapsulate( + params.key_agreement.clone().unwrap(), + params.pin_uv_auth_protocol, + ) + .unwrap(); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + + let mut auth_param_data = params.new_pin_enc.clone().unwrap(); + auth_param_data.extend(params.pin_hash_enc.as_ref().unwrap()); + let pin_uv_auth_param = shared_secret.authenticate(&auth_param_data); + params.pin_uv_auth_param = Some(pin_uv_auth_param); + assert_eq!( + client_pin.process_command(&mut env, params.clone(), Instant::new(0)), + Ok(ResponseData::AuthenticatorClientPin(None)) + ); + + let mut bad_params = params.clone(); + bad_params.pin_hash_enc = Some(vec![0xEE; 16]); + assert_eq!( + client_pin.process_command(&mut env, bad_params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + + while storage::pin_retries(&mut env).unwrap() > 0 { + storage::decr_pin_retries(&mut env).unwrap(); + } + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED) + ); + } + + #[test] + fn test_process_change_pin_v1() { + test_helper_process_change_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_change_pin_v2() { + test_helper_process_change_pin(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_pin_token(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetPinToken, + ); + let shared_secret = client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .decapsulate( + params.key_agreement.clone().unwrap(), + params.pin_uv_auth_protocol, + ) + .unwrap(); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + + let response = client_pin + .process_command(&mut env, params.clone(), Instant::new(0)) + .unwrap(); + let encrypted_token = match response { + ResponseData::AuthenticatorClientPin(Some(response)) => { + response.pin_uv_auth_token.unwrap() + } + _ => panic!("Invalid response type"), + }; + assert_eq!( + &shared_secret.decrypt(&encrypted_token).unwrap(), + client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .get_pin_uv_auth_token() + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::MakeCredential), + Ok(()) + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::GetAssertion), + Ok(()) + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_no_permissions_rp_id(), + Ok(()) + ); + + let mut bad_params = params; + bad_params.pin_hash_enc = Some(vec![0xEE; 16]); + assert_eq!( + client_pin.process_command(&mut env, bad_params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + } + + #[test] + fn test_process_get_pin_token_v1() { + test_helper_process_get_pin_token(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_pin_token_v2() { + test_helper_process_get_pin_token(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_pin_token_force_pin_change(pin_uv_auth_protocol: PinUvAuthProtocol) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetPinToken, + ); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + + assert_eq!(storage::force_pin_change(&mut env), Ok(())); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID), + ); + } + + #[test] + fn test_process_get_pin_token_force_pin_change_v1() { + test_helper_process_get_pin_token_force_pin_change(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_pin_token_force_pin_change_v2() { + test_helper_process_get_pin_token_force_pin_change(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions, + ); + let shared_secret = client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .decapsulate( + params.key_agreement.clone().unwrap(), + params.pin_uv_auth_protocol, + ) + .unwrap(); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + + let response = client_pin + .process_command(&mut env, params.clone(), Instant::new(0)) + .unwrap(); + let encrypted_token = match response { + ResponseData::AuthenticatorClientPin(Some(response)) => { + response.pin_uv_auth_token.unwrap() + } + _ => panic!("Invalid response type"), + }; + assert_eq!( + &shared_secret.decrypt(&encrypted_token).unwrap(), + client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .get_pin_uv_auth_token() + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::MakeCredential), + Ok(()) + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::GetAssertion), + Ok(()) + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permissions_rp_id("example.com"), + Ok(()) + ); + + let mut bad_params = params.clone(); + bad_params.permissions = Some(0x00); + assert_eq!( + client_pin.process_command(&mut env, bad_params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + let mut bad_params = params.clone(); + bad_params.permissions_rp_id = None; + assert_eq!( + client_pin.process_command(&mut env, bad_params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + let mut bad_params = params; + bad_params.pin_hash_enc = Some(vec![0xEE; 16]); + assert_eq!( + client_pin.process_command(&mut env, bad_params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + } + + #[test] + fn test_process_get_pin_uv_auth_token_using_pin_with_permissions_v1() { + test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_pin_uv_auth_token_using_pin_with_permissions_v2() { + test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions(PinUvAuthProtocol::V2); + } + + fn test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions_force_pin_change( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) { + let (mut client_pin, params) = create_client_pin_and_parameters( + pin_uv_auth_protocol, + ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions, + ); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + + assert_eq!(storage::force_pin_change(&mut env), Ok(())); + assert_eq!( + client_pin.process_command(&mut env, params, Instant::new(0)), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) + ); + } + + #[test] + fn test_process_get_pin_uv_auth_token_using_pin_with_permissions_force_pin_change_v1() { + test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions_force_pin_change( + PinUvAuthProtocol::V1, + ); + } + + #[test] + fn test_process_get_pin_uv_auth_token_using_pin_with_permissions_force_pin_change_v2() { + test_helper_process_get_pin_uv_auth_token_using_pin_with_permissions_force_pin_change( + PinUvAuthProtocol::V2, + ); + } + + fn test_helper_decrypt_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let pin_protocol = PinProtocol::new(env.rng()); + let shared_secret = pin_protocol + .decapsulate(pin_protocol.get_public_key(), pin_uv_auth_protocol) + .unwrap(); + + let new_pin_enc = encrypt_pin(shared_secret.as_ref(), b"1234".to_vec()); + assert_eq!( + decrypt_pin(shared_secret.as_ref(), new_pin_enc), + Ok(b"1234".to_vec()), + ); + + let new_pin_enc = encrypt_pin(shared_secret.as_ref(), b"123".to_vec()); + assert_eq!( + decrypt_pin(shared_secret.as_ref(), new_pin_enc), + Ok(b"123".to_vec()), + ); + + // Encrypted PIN is too short. + let new_pin_enc = vec![0x44; 63]; + assert_eq!( + decrypt_pin(shared_secret.as_ref(), new_pin_enc), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // Encrypted PIN is too long. + let new_pin_enc = vec![0x44; 65]; + assert_eq!( + decrypt_pin(shared_secret.as_ref(), new_pin_enc), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + + #[test] + fn test_decrypt_pin_v1() { + test_helper_decrypt_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_decrypt_pin_v2() { + test_helper_decrypt_pin(PinUvAuthProtocol::V2); + } + + fn test_helper_check_and_store_new_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let pin_protocol = PinProtocol::new(env.rng()); + let shared_secret = pin_protocol + .decapsulate(pin_protocol.get_public_key(), pin_uv_auth_protocol) + .unwrap(); + + let test_cases = vec![ + // Accept PIN "1234". + (b"1234".to_vec(), Ok(())), + // Reject PIN "123" since it is too short. + ( + b"123".to_vec(), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), + ), + // Reject PIN "12'\0'4" (a zero byte at index 2). + ( + b"12\04".to_vec(), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), + ), + // PINs must be at most 63 bytes long, to allow for a trailing 0u8 padding. + ( + vec![0x30; 64], + Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), + ), + ]; + for (pin, result) in test_cases { + let old_pin_hash = storage::pin_hash(&mut env).unwrap(); + let new_pin_enc = encrypt_pin(shared_secret.as_ref(), pin); + + assert_eq!( + check_and_store_new_pin(&mut env, shared_secret.as_ref(), new_pin_enc), + result + ); + if result.is_ok() { + assert_ne!(old_pin_hash, storage::pin_hash(&mut env).unwrap()); + } else { + assert_eq!(old_pin_hash, storage::pin_hash(&mut env).unwrap()); + } + } + } + + #[test] + fn test_check_and_store_new_pin_v1() { + test_helper_check_and_store_new_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_check_and_store_new_pin_v2() { + test_helper_check_and_store_new_pin(PinUvAuthProtocol::V2); + } + + /// Generates valid inputs for process_hmac_secret and returns the output. + fn get_process_hmac_secret_decrypted_output( + pin_uv_auth_protocol: PinUvAuthProtocol, + cred_random: &[u8; 32], + salt: Vec, + ) -> Result, Ctap2StatusCode> { + let mut env = TestEnv::new(); + let (client_pin, shared_secret) = create_client_pin_and_shared_secret(pin_uv_auth_protocol); + + let salt_enc = shared_secret.as_ref().encrypt(env.rng(), &salt).unwrap(); + let salt_auth = shared_secret.authenticate(&salt_enc); + let hmac_secret_input = GetAssertionHmacSecretInput { + key_agreement: client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .get_public_key(), + salt_enc, + salt_auth, + pin_uv_auth_protocol, + }; + let output = client_pin.process_hmac_secret(env.rng(), hmac_secret_input, cred_random); + output.map(|v| shared_secret.as_ref().decrypt(&v).unwrap()) + } + + fn test_helper_process_hmac_secret_bad_salt_auth(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let (client_pin, shared_secret) = create_client_pin_and_shared_secret(pin_uv_auth_protocol); + let cred_random = [0xC9; 32]; + + let salt_enc = vec![0x01; 32]; + let mut salt_auth = shared_secret.authenticate(&salt_enc); + salt_auth[0] ^= 0x01; + let hmac_secret_input = GetAssertionHmacSecretInput { + key_agreement: client_pin + .get_pin_protocol(pin_uv_auth_protocol) + .get_public_key(), + salt_enc, + salt_auth, + pin_uv_auth_protocol, + }; + let output = client_pin.process_hmac_secret(env.rng(), hmac_secret_input, &cred_random); + assert_eq!(output, Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID)); + } + + #[test] + fn test_process_hmac_secret_bad_salt_auth_v1() { + test_helper_process_hmac_secret_bad_salt_auth(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_hmac_secret_bad_salt_auth_v2() { + test_helper_process_hmac_secret_bad_salt_auth(PinUvAuthProtocol::V2); + } + + fn test_helper_process_hmac_secret_one_salt(pin_uv_auth_protocol: PinUvAuthProtocol) { + let cred_random = [0xC9; 32]; + + let salt = vec![0x01; 32]; + let expected_output = hmac_256::(&cred_random, &salt); + + let output = + get_process_hmac_secret_decrypted_output(pin_uv_auth_protocol, &cred_random, salt) + .unwrap(); + assert_eq!(&output, &expected_output); + } + + #[test] + fn test_process_hmac_secret_one_salt_v1() { + test_helper_process_hmac_secret_one_salt(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_hmac_secret_one_salt_v2() { + test_helper_process_hmac_secret_one_salt(PinUvAuthProtocol::V2); + } + + fn test_helper_process_hmac_secret_two_salts(pin_uv_auth_protocol: PinUvAuthProtocol) { + let cred_random = [0xC9; 32]; + + let salt1 = [0x01; 32]; + let salt2 = [0x02; 32]; + let expected_output1 = hmac_256::(&cred_random, &salt1); + let expected_output2 = hmac_256::(&cred_random, &salt2); + + let mut salt12 = vec![0x00; 64]; + salt12[..32].copy_from_slice(&salt1); + salt12[32..].copy_from_slice(&salt2); + let output = + get_process_hmac_secret_decrypted_output(pin_uv_auth_protocol, &cred_random, salt12) + .unwrap(); + assert_eq!(&output[..32], &expected_output1); + assert_eq!(&output[32..], &expected_output2); + + let mut salt02 = vec![0x00; 64]; + salt02[32..].copy_from_slice(&salt2); + let output = + get_process_hmac_secret_decrypted_output(pin_uv_auth_protocol, &cred_random, salt02) + .unwrap(); + assert_eq!(&output[32..], &expected_output2); + + let mut salt10 = vec![0x00; 64]; + salt10[..32].copy_from_slice(&salt1); + let output = + get_process_hmac_secret_decrypted_output(pin_uv_auth_protocol, &cred_random, salt10) + .unwrap(); + assert_eq!(&output[..32], &expected_output1); + } + + #[test] + fn test_process_hmac_secret_two_salts_v1() { + test_helper_process_hmac_secret_two_salts(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_hmac_secret_two_salts_v2() { + test_helper_process_hmac_secret_two_salts(PinUvAuthProtocol::V2); + } + + fn test_helper_process_hmac_secret_wrong_length(pin_uv_auth_protocol: PinUvAuthProtocol) { + let cred_random = [0xC9; 32]; + + let output = get_process_hmac_secret_decrypted_output( + pin_uv_auth_protocol, + &cred_random, + vec![0x5E; 48], + ); + assert_eq!(output, Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)); + } + + #[test] + fn test_process_hmac_secret_wrong_length_v1() { + test_helper_process_hmac_secret_wrong_length(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_hmac_secret_wrong_length_v2() { + test_helper_process_hmac_secret_wrong_length(PinUvAuthProtocol::V2); + } + + #[test] + fn test_has_permission() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + client_pin.pin_uv_auth_token_state.set_permissions(0x7F); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(permission), + Ok(()) + ); + } + client_pin.pin_uv_auth_token_state.set_permissions(0x00); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(permission), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + } + + #[test] + fn test_has_no_rp_id_permission() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + assert_eq!(client_pin.has_no_rp_id_permission(), Ok(())); + client_pin + .pin_uv_auth_token_state + .set_permissions_rp_id(Some("example.com".to_string())); + assert_eq!( + client_pin.has_no_rp_id_permission(), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_has_no_or_rp_id_permission() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + assert_eq!(client_pin.has_no_or_rp_id_permission("example.com"), Ok(())); + client_pin + .pin_uv_auth_token_state + .set_permissions_rp_id(Some("example.com".to_string())); + assert_eq!(client_pin.has_no_or_rp_id_permission("example.com"), Ok(())); + assert_eq!( + client_pin.has_no_or_rp_id_permission("another.example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_has_no_or_rp_id_hash_permission() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + let rp_id_hash = Sha256::hash(b"example.com"); + assert_eq!( + client_pin.has_no_or_rp_id_hash_permission(&rp_id_hash), + Ok(()) + ); + client_pin + .pin_uv_auth_token_state + .set_permissions_rp_id(Some("example.com".to_string())); + assert_eq!( + client_pin.has_no_or_rp_id_hash_permission(&rp_id_hash), + Ok(()) + ); + assert_eq!( + client_pin.has_no_or_rp_id_hash_permission(&[0x4A; 32]), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_ensure_rp_id_permission() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + assert_eq!(client_pin.ensure_rp_id_permission("example.com"), Ok(())); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permissions_rp_id("example.com"), + Ok(()) + ); + assert_eq!(client_pin.ensure_rp_id_permission("example.com"), Ok(())); + assert_eq!( + client_pin.ensure_rp_id_permission("another.example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_verify_pin_uv_auth_token() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + let message = [0xAA]; + client_pin + .pin_uv_auth_token_state + .begin_using_pin_uv_auth_token(Instant::new(0)); + + let pin_uv_auth_token_v1 = client_pin + .get_pin_protocol(PinUvAuthProtocol::V1) + .get_pin_uv_auth_token(); + let pin_uv_auth_param_v1 = + authenticate_pin_uv_auth_token(pin_uv_auth_token_v1, &message, PinUvAuthProtocol::V1); + let pin_uv_auth_token_v2 = client_pin + .get_pin_protocol(PinUvAuthProtocol::V2) + .get_pin_uv_auth_token(); + let pin_uv_auth_param_v2 = + authenticate_pin_uv_auth_token(pin_uv_auth_token_v2, &message, PinUvAuthProtocol::V2); + let pin_uv_auth_param_v1_from_v2_token = + authenticate_pin_uv_auth_token(pin_uv_auth_token_v2, &message, PinUvAuthProtocol::V1); + let pin_uv_auth_param_v2_from_v1_token = + authenticate_pin_uv_auth_token(pin_uv_auth_token_v1, &message, PinUvAuthProtocol::V2); + + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v1, + PinUvAuthProtocol::V1 + ), + Ok(()) + ); + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v2, + PinUvAuthProtocol::V2 + ), + Ok(()) + ); + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v1, + PinUvAuthProtocol::V2 + ), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v2, + PinUvAuthProtocol::V1 + ), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v1_from_v2_token, + PinUvAuthProtocol::V1 + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v2_from_v1_token, + PinUvAuthProtocol::V2 + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_verify_pin_uv_auth_token_not_in_use() { + let mut env = TestEnv::new(); + let client_pin = ClientPin::new(env.rng()); + let message = [0xAA]; + + let pin_uv_auth_token_v1 = client_pin + .get_pin_protocol(PinUvAuthProtocol::V1) + .get_pin_uv_auth_token(); + let pin_uv_auth_param_v1 = + authenticate_pin_uv_auth_token(pin_uv_auth_token_v1, &message, PinUvAuthProtocol::V1); + + assert_eq!( + client_pin.verify_pin_uv_auth_token( + &message, + &pin_uv_auth_param_v1, + PinUvAuthProtocol::V1 + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_reset() { + let mut env = TestEnv::new(); + let mut client_pin = ClientPin::new(env.rng()); + let public_key_v1 = client_pin.pin_protocol_v1.get_public_key(); + let public_key_v2 = client_pin.pin_protocol_v2.get_public_key(); + let token_v1 = *client_pin.pin_protocol_v1.get_pin_uv_auth_token(); + let token_v2 = *client_pin.pin_protocol_v2.get_pin_uv_auth_token(); + client_pin.pin_uv_auth_token_state.set_permissions(0xFF); + client_pin + .pin_uv_auth_token_state + .set_permissions_rp_id(Some(String::from("example.com"))); + client_pin.reset(env.rng()); + assert_ne!(public_key_v1, client_pin.pin_protocol_v1.get_public_key()); + assert_ne!(public_key_v2, client_pin.pin_protocol_v2.get_public_key()); + assert_ne!( + &token_v1, + client_pin.pin_protocol_v1.get_pin_uv_auth_token() + ); + assert_ne!( + &token_v2, + client_pin.pin_protocol_v2.get_pin_uv_auth_token() + ); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin.has_permission(permission), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + assert_eq!(client_pin.has_no_rp_id_permission(), Ok(())); + } + + #[test] + fn test_update_timeouts() { + let (mut client_pin, mut params) = create_client_pin_and_parameters( + PinUvAuthProtocol::V2, + ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions, + ); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + params.permissions = Some(0xFF); + + assert!(client_pin + .process_command(&mut env, params, Instant::new(0)) + .is_ok()); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(permission), + Ok(()) + ); + } + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permissions_rp_id("example.com"), + Ok(()) + ); + + let timeout = Instant::new(0) + Duration::from_millis(30001_u32); + client_pin.update_timeouts(timeout); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(permission), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permissions_rp_id("example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_clear_token_flags() { + let (mut client_pin, mut params) = create_client_pin_and_parameters( + PinUvAuthProtocol::V2, + ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions, + ); + let mut env = TestEnv::new(); + set_standard_pin(&mut env); + params.permissions = Some(0xFF); + + assert!(client_pin + .process_command(&mut env, params, Instant::new(0)) + .is_ok()); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(permission), + Ok(()) + ); + } + assert_eq!(client_pin.check_user_verified_flag(), Ok(())); + + client_pin.clear_token_flags(); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::CredentialManagement), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + client_pin + .pin_uv_auth_token_state + .has_permission(PinPermission::LargeBlobWrite), + Ok(()) + ); + assert_eq!( + client_pin.check_user_verified_flag(), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } +} diff --git a/apps/vault/src/ctap/command.rs b/apps/vault/src/ctap/command.rs index 1a4ee6702..73d0fdb0f 100644 --- a/apps/vault/src/ctap/command.rs +++ b/apps/vault/src/ctap/command.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,27 +14,29 @@ use super::data_formats::{ extract_array, extract_bool, extract_byte_string, extract_map, extract_text_string, - extract_unsigned, ok_or_missing, ClientPinSubCommand, CoseKey, GetAssertionExtensions, - GetAssertionOptions, MakeCredentialExtensions, MakeCredentialOptions, - PublicKeyCredentialDescriptor, PublicKeyCredentialParameter, PublicKeyCredentialRpEntity, - PublicKeyCredentialUserEntity, + extract_unsigned, ok_or_missing, ClientPinSubCommand, ConfigSubCommand, ConfigSubCommandParams, + CoseKey, CredentialManagementSubCommand, CredentialManagementSubCommandParameters, + GetAssertionExtensions, GetAssertionOptions, MakeCredentialExtensions, MakeCredentialOptions, + PinUvAuthProtocol, PublicKeyCredentialDescriptor, PublicKeyCredentialParameter, + PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity, SetMinPinLengthParams, }; -use super::key_material; use super::status_code::Ctap2StatusCode; +use super::{cbor_read, key_material}; use std::string::String; use std::vec::Vec; +#[cfg(feature = "fuzz")] +use arbitrary::Arbitrary; use arrayref::array_ref; -use cbor::destructure_cbor_map; use core::convert::TryFrom; +use cbor as cbor; +use cbor::destructure_cbor_map; -// Depending on your memory, you can use Some(n) to limit request sizes in -// MakeCredential and GetAssertion. This affects allowList and excludeList. -// You might also want to set the max credential size in process_get_info then. -pub const MAX_CREDENTIAL_COUNT_IN_LIST: Option = None; +// This constant is a consequence of the structure of messages. +const MIN_LARGE_BLOB_LEN: usize = 17; // CTAP specification (version 20190130) section 6.1 -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] +#[allow(clippy::enum_variant_names)] pub enum Command { AuthenticatorMakeCredential(AuthenticatorMakeCredentialParameters), AuthenticatorGetAssertion(AuthenticatorGetAssertionParameters), @@ -42,21 +44,16 @@ pub enum Command { AuthenticatorClientPin(AuthenticatorClientPinParameters), AuthenticatorReset, AuthenticatorGetNextAssertion, - #[cfg(feature = "with_ctap2_1")] + AuthenticatorCredentialManagement(AuthenticatorCredentialManagementParameters), AuthenticatorSelection, - // TODO(kaczmarczyck) implement FIDO 2.1 commands (see below consts) + AuthenticatorLargeBlobs(AuthenticatorLargeBlobsParameters), + AuthenticatorConfig(AuthenticatorConfigParameters), // Vendor specific commands AuthenticatorVendorConfigure(AuthenticatorVendorConfigureParameters), + AuthenticatorVendorUpgrade(AuthenticatorVendorUpgradeParameters), + AuthenticatorVendorUpgradeInfo, } -impl From for Ctap2StatusCode { - fn from(_: cbor::reader::DecoderError) -> Self { - Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR - } -} - -// TODO: Remove this `allow(dead_code)` once the constants are used. -#[allow(dead_code)] impl Command { const AUTHENTICATOR_MAKE_CREDENTIAL: u8 = 0x01; const AUTHENTICATOR_GET_ASSERTION: u8 = 0x02; @@ -64,14 +61,20 @@ impl Command { const AUTHENTICATOR_CLIENT_PIN: u8 = 0x06; const AUTHENTICATOR_RESET: u8 = 0x07; const AUTHENTICATOR_GET_NEXT_ASSERTION: u8 = 0x08; - // TODO(kaczmarczyck) use or remove those constants - const AUTHENTICATOR_BIO_ENROLLMENT: u8 = 0x09; + // Implement Bio Enrollment when your hardware supports biometrics. + const _AUTHENTICATOR_BIO_ENROLLMENT: u8 = 0x09; const AUTHENTICATOR_CREDENTIAL_MANAGEMENT: u8 = 0x0A; const AUTHENTICATOR_SELECTION: u8 = 0x0B; const AUTHENTICATOR_LARGE_BLOBS: u8 = 0x0C; const AUTHENTICATOR_CONFIG: u8 = 0x0D; const _AUTHENTICATOR_VENDOR_FIRST: u8 = 0x40; const AUTHENTICATOR_VENDOR_CONFIGURE: u8 = 0x40; + // This commands is the same as AUTHENTICATOR_CREDENTIAL_MANAGEMENT but is duplicated as a + // vendor command for legacy and compatibility reasons. See + // https://github.com/Yubico/libfido2/issues/628 for more information. + const AUTHENTICATOR_VENDOR_CREDENTIAL_MANAGEMENT: u8 = 0x41; + const AUTHENTICATOR_VENDOR_UPGRADE: u8 = 0x42; + const AUTHENTICATOR_VENDOR_UPGRADE_INFO: u8 = 0x43; const _AUTHENTICATOR_VENDOR_LAST: u8 = 0xBF; pub fn deserialize(bytes: &[u8]) -> Result { @@ -83,13 +86,13 @@ impl Command { let command_value = bytes[0]; match command_value { Command::AUTHENTICATOR_MAKE_CREDENTIAL => { - let decoded_cbor = cbor::read(&bytes[1..])?; + let decoded_cbor = cbor_read(&bytes[1..])?; Ok(Command::AuthenticatorMakeCredential( AuthenticatorMakeCredentialParameters::try_from(decoded_cbor)?, )) } Command::AUTHENTICATOR_GET_ASSERTION => { - let decoded_cbor = cbor::read(&bytes[1..])?; + let decoded_cbor = cbor_read(&bytes[1..])?; Ok(Command::AuthenticatorGetAssertion( AuthenticatorGetAssertionParameters::try_from(decoded_cbor)?, )) @@ -99,7 +102,7 @@ impl Command { Ok(Command::AuthenticatorGetInfo) } Command::AUTHENTICATOR_CLIENT_PIN => { - let decoded_cbor = cbor::read(&bytes[1..])?; + let decoded_cbor = cbor_read(&bytes[1..])?; Ok(Command::AuthenticatorClientPin( AuthenticatorClientPinParameters::try_from(decoded_cbor)?, )) @@ -112,35 +115,65 @@ impl Command { // Parameters are ignored. Ok(Command::AuthenticatorGetNextAssertion) } - #[cfg(feature = "with_ctap2_1")] + Command::AUTHENTICATOR_CREDENTIAL_MANAGEMENT + | Command::AUTHENTICATOR_VENDOR_CREDENTIAL_MANAGEMENT => { + let decoded_cbor = cbor_read(&bytes[1..])?; + Ok(Command::AuthenticatorCredentialManagement( + AuthenticatorCredentialManagementParameters::try_from(decoded_cbor)?, + )) + } Command::AUTHENTICATOR_SELECTION => { // Parameters are ignored. Ok(Command::AuthenticatorSelection) } + Command::AUTHENTICATOR_LARGE_BLOBS => { + let decoded_cbor = cbor_read(&bytes[1..])?; + Ok(Command::AuthenticatorLargeBlobs( + AuthenticatorLargeBlobsParameters::try_from(decoded_cbor)?, + )) + } + Command::AUTHENTICATOR_CONFIG => { + let decoded_cbor = cbor_read(&bytes[1..])?; + Ok(Command::AuthenticatorConfig( + AuthenticatorConfigParameters::try_from(decoded_cbor)?, + )) + } Command::AUTHENTICATOR_VENDOR_CONFIGURE => { - let decoded_cbor = cbor::read(&bytes[1..])?; + let decoded_cbor = cbor_read(&bytes[1..])?; Ok(Command::AuthenticatorVendorConfigure( AuthenticatorVendorConfigureParameters::try_from(decoded_cbor)?, )) } + Command::AUTHENTICATOR_VENDOR_UPGRADE => { + let decoded_cbor = cbor_read(&bytes[1..])?; + Ok(Command::AuthenticatorVendorUpgrade( + AuthenticatorVendorUpgradeParameters::try_from(decoded_cbor)?, + )) + } + Command::AUTHENTICATOR_VENDOR_UPGRADE_INFO => { + // Parameters are ignored. + Ok(Command::AuthenticatorVendorUpgradeInfo) + } _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND), } } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct AuthenticatorMakeCredentialParameters { pub client_data_hash: Vec, pub rp: PublicKeyCredentialRpEntity, pub user: PublicKeyCredentialUserEntity, pub pub_key_cred_params: Vec, pub exclude_list: Option>, - pub extensions: Option, - // Even though options are optional, we can use the default if not present. + // Extensions are optional, but we can use defaults for all missing fields. + pub extensions: MakeCredentialExtensions, + // Same for options, use defaults when not present. pub options: MakeCredentialOptions, pub pin_uv_auth_param: Option>, - pub pin_uv_auth_protocol: Option, + pub pin_uv_auth_protocol: Option, + pub enterprise_attestation: Option, } impl TryFrom for AuthenticatorMakeCredentialParameters { @@ -149,15 +182,16 @@ impl TryFrom for AuthenticatorMakeCredentialParameters { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { - 1 => client_data_hash, - 2 => rp, - 3 => user, - 4 => cred_param_vec, - 5 => exclude_list, - 6 => extensions, - 7 => options, - 8 => pin_uv_auth_param, - 9 => pin_uv_auth_protocol, + 0x01 => client_data_hash, + 0x02 => rp, + 0x03 => user, + 0x04 => cred_param_vec, + 0x05 => exclude_list, + 0x06 => extensions, + 0x07 => options, + 0x08 => pin_uv_auth_param, + 0x09 => pin_uv_auth_protocol, + 0x0A => enterprise_attestation, } = extract_map(cbor_value)?; } @@ -174,10 +208,8 @@ impl TryFrom for AuthenticatorMakeCredentialParameters { let exclude_list = match exclude_list { Some(entry) => { let exclude_list_vec = extract_array(entry)?; - let list_len = MAX_CREDENTIAL_COUNT_IN_LIST.unwrap_or(exclude_list_vec.len()); let exclude_list = exclude_list_vec .into_iter() - .take(list_len) .map(PublicKeyCredentialDescriptor::try_from) .collect::, Ctap2StatusCode>>()?; Some(exclude_list) @@ -187,18 +219,20 @@ impl TryFrom for AuthenticatorMakeCredentialParameters { let extensions = extensions .map(MakeCredentialExtensions::try_from) - .transpose()?; + .transpose()? + .unwrap_or_default(); - let options = match options { - Some(entry) => MakeCredentialOptions::try_from(entry)?, - None => MakeCredentialOptions { - rk: false, - uv: false, - }, - }; + let options = options + .map(MakeCredentialOptions::try_from) + .transpose()? + .unwrap_or_default(); let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; - let pin_uv_auth_protocol = pin_uv_auth_protocol.map(extract_unsigned).transpose()?; + let pin_uv_auth_protocol = pin_uv_auth_protocol + .map(PinUvAuthProtocol::try_from) + .transpose()?; + // We don't convert into EnterpriseAttestationMode to maintain the correct order of errors. + let enterprise_attestation = enterprise_attestation.map(extract_unsigned).transpose()?; Ok(AuthenticatorMakeCredentialParameters { client_data_hash, @@ -210,21 +244,23 @@ impl TryFrom for AuthenticatorMakeCredentialParameters { options, pin_uv_auth_param, pin_uv_auth_protocol, + enterprise_attestation, }) } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct AuthenticatorGetAssertionParameters { pub rp_id: String, pub client_data_hash: Vec, pub allow_list: Option>, - pub extensions: Option, - // Even though options are optional, we can use the default if not present. + // Extensions are optional, but we can use defaults for all missing fields. + pub extensions: GetAssertionExtensions, + // Same for options, use defaults when not present. pub options: GetAssertionOptions, pub pin_uv_auth_param: Option>, - pub pin_uv_auth_protocol: Option, + pub pin_uv_auth_protocol: Option, } impl TryFrom for AuthenticatorGetAssertionParameters { @@ -233,13 +269,13 @@ impl TryFrom for AuthenticatorGetAssertionParameters { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { - 1 => rp_id, - 2 => client_data_hash, - 3 => allow_list, - 4 => extensions, - 5 => options, - 6 => pin_uv_auth_param, - 7 => pin_uv_auth_protocol, + 0x01 => rp_id, + 0x02 => client_data_hash, + 0x03 => allow_list, + 0x04 => extensions, + 0x05 => options, + 0x06 => pin_uv_auth_param, + 0x07 => pin_uv_auth_protocol, } = extract_map(cbor_value)?; } @@ -249,10 +285,8 @@ impl TryFrom for AuthenticatorGetAssertionParameters { let allow_list = match allow_list { Some(entry) => { let allow_list_vec = extract_array(entry)?; - let list_len = MAX_CREDENTIAL_COUNT_IN_LIST.unwrap_or(allow_list_vec.len()); let allow_list = allow_list_vec .into_iter() - .take(list_len) .map(PublicKeyCredentialDescriptor::try_from) .collect::, Ctap2StatusCode>>()?; Some(allow_list) @@ -262,18 +296,18 @@ impl TryFrom for AuthenticatorGetAssertionParameters { let extensions = extensions .map(GetAssertionExtensions::try_from) - .transpose()?; + .transpose()? + .unwrap_or_default(); - let options = match options { - Some(entry) => GetAssertionOptions::try_from(entry)?, - None => GetAssertionOptions { - up: true, - uv: false, - }, - }; + let options = options + .map(GetAssertionOptions::try_from) + .transpose()? + .unwrap_or_default(); let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; - let pin_uv_auth_protocol = pin_uv_auth_protocol.map(extract_unsigned).transpose()?; + let pin_uv_auth_protocol = pin_uv_auth_protocol + .map(PinUvAuthProtocol::try_from) + .transpose()?; Ok(AuthenticatorGetAssertionParameters { rp_id, @@ -287,22 +321,16 @@ impl TryFrom for AuthenticatorGetAssertionParameters { } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct AuthenticatorClientPinParameters { - pub pin_protocol: u64, + pub pin_uv_auth_protocol: PinUvAuthProtocol, pub sub_command: ClientPinSubCommand, pub key_agreement: Option, - pub pin_auth: Option>, + pub pin_uv_auth_param: Option>, pub new_pin_enc: Option>, pub pin_hash_enc: Option>, - #[cfg(feature = "with_ctap2_1")] - pub min_pin_length: Option, - #[cfg(feature = "with_ctap2_1")] - pub min_pin_length_rp_ids: Option>, - #[cfg(feature = "with_ctap2_1")] pub permissions: Option, - #[cfg(feature = "with_ctap2_1")] pub permissions_rp_id: Option, } @@ -310,87 +338,164 @@ impl TryFrom for AuthenticatorClientPinParameters { type Error = Ctap2StatusCode; fn try_from(cbor_value: cbor::Value) -> Result { - #[cfg(not(feature = "with_ctap2_1"))] destructure_cbor_map! { let { - 1 => pin_protocol, - 2 => sub_command, - 3 => key_agreement, - 4 => pin_auth, - 5 => new_pin_enc, - 6 => pin_hash_enc, - } = extract_map(cbor_value)?; - } - #[cfg(feature = "with_ctap2_1")] - destructure_cbor_map! { - let { - 1 => pin_protocol, - 2 => sub_command, - 3 => key_agreement, - 4 => pin_auth, - 5 => new_pin_enc, - 6 => pin_hash_enc, - 7 => min_pin_length, - 8 => min_pin_length_rp_ids, - 9 => permissions, - 10 => permissions_rp_id, + 0x01 => pin_uv_auth_protocol, + 0x02 => sub_command, + 0x03 => key_agreement, + 0x04 => pin_uv_auth_param, + 0x05 => new_pin_enc, + 0x06 => pin_hash_enc, + 0x09 => permissions, + 0x0A => permissions_rp_id, } = extract_map(cbor_value)?; } - let pin_protocol = extract_unsigned(ok_or_missing(pin_protocol)?)?; + let pin_uv_auth_protocol = + PinUvAuthProtocol::try_from(ok_or_missing(pin_uv_auth_protocol)?)?; let sub_command = ClientPinSubCommand::try_from(ok_or_missing(sub_command)?)?; - let key_agreement = key_agreement.map(extract_map).transpose()?.map(CoseKey); - let pin_auth = pin_auth.map(extract_byte_string).transpose()?; + let key_agreement = key_agreement.map(CoseKey::try_from).transpose()?; + let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; let new_pin_enc = new_pin_enc.map(extract_byte_string).transpose()?; let pin_hash_enc = pin_hash_enc.map(extract_byte_string).transpose()?; - #[cfg(feature = "with_ctap2_1")] - let min_pin_length = min_pin_length - .map(extract_unsigned) - .transpose()? - .map(u8::try_from) - .transpose() - .map_err(|_| Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION)?; - #[cfg(feature = "with_ctap2_1")] - let min_pin_length_rp_ids = match min_pin_length_rp_ids { - Some(entry) => Some( - extract_array(entry)? - .into_iter() - .map(extract_text_string) - .collect::, Ctap2StatusCode>>()?, - ), - None => None, - }; - #[cfg(feature = "with_ctap2_1")] // We expect a bit field of 8 bits, and drop everything else. // This means we ignore extensions in future versions. let permissions = permissions .map(extract_unsigned) .transpose()? .map(|p| p as u8); - #[cfg(feature = "with_ctap2_1")] let permissions_rp_id = permissions_rp_id.map(extract_text_string).transpose()?; Ok(AuthenticatorClientPinParameters { - pin_protocol, + pin_uv_auth_protocol, sub_command, key_agreement, - pin_auth, + pin_uv_auth_param, new_pin_enc, pin_hash_enc, - #[cfg(feature = "with_ctap2_1")] - min_pin_length, - #[cfg(feature = "with_ctap2_1")] - min_pin_length_rp_ids, - #[cfg(feature = "with_ctap2_1")] permissions, - #[cfg(feature = "with_ctap2_1")] permissions_rp_id, }) } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorLargeBlobsParameters { + pub get: Option, + pub set: Option>, + pub offset: usize, + pub length: Option, + pub pin_uv_auth_param: Option>, + pub pin_uv_auth_protocol: Option, +} + +impl TryFrom for AuthenticatorLargeBlobsParameters { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => get, + 0x02 => set, + 0x03 => offset, + 0x04 => length, + 0x05 => pin_uv_auth_param, + 0x06 => pin_uv_auth_protocol, + } = extract_map(cbor_value)?; + } + + // careful: some missing parameters here are CTAP1_ERR_INVALID_PARAMETER + let get = get.map(extract_unsigned).transpose()?.map(|u| u as usize); + let set = set.map(extract_byte_string).transpose()?; + let offset = + extract_unsigned(offset.ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)?)? as usize; + let length = length + .map(extract_unsigned) + .transpose()? + .map(|u| u as usize); + let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; + let pin_uv_auth_protocol = pin_uv_auth_protocol + .map(PinUvAuthProtocol::try_from) + .transpose()?; + + if get.is_none() && set.is_none() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if get.is_some() && set.is_some() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if get.is_some() + && (length.is_some() || pin_uv_auth_param.is_some() || pin_uv_auth_protocol.is_some()) + { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if set.is_some() && offset == 0 { + match length { + None => return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), + Some(len) if len < MIN_LARGE_BLOB_LEN => { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + } + Some(_) => (), + } + } + if set.is_some() && offset != 0 && length.is_some() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + + Ok(AuthenticatorLargeBlobsParameters { + get, + set, + offset, + length, + pin_uv_auth_param, + pin_uv_auth_protocol, + }) + } +} + +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorConfigParameters { + pub sub_command: ConfigSubCommand, + pub sub_command_params: Option, + pub pin_uv_auth_param: Option>, + pub pin_uv_auth_protocol: Option, +} + +impl TryFrom for AuthenticatorConfigParameters { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => sub_command, + 0x02 => sub_command_params, + 0x03 => pin_uv_auth_param, + 0x04 => pin_uv_auth_protocol, + } = extract_map(cbor_value)?; + } + + let sub_command = ConfigSubCommand::try_from(ok_or_missing(sub_command)?)?; + let sub_command_params = match sub_command { + ConfigSubCommand::SetMinPinLength => Some(ConfigSubCommandParams::SetMinPinLength( + SetMinPinLengthParams::try_from(ok_or_missing(sub_command_params)?)?, + )), + _ => None, + }; + let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; + let pin_uv_auth_protocol = pin_uv_auth_protocol + .map(PinUvAuthProtocol::try_from) + .transpose()?; + + Ok(AuthenticatorConfigParameters { + sub_command, + sub_command_params, + pin_uv_auth_param, + pin_uv_auth_protocol, + }) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] pub struct AuthenticatorAttestationMaterial { pub certificate: Vec, pub private_key: [u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH], @@ -402,8 +507,8 @@ impl TryFrom for AuthenticatorAttestationMaterial { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { - 1 => certificate, - 2 => private_key, + 0x01 => certificate, + 0x02 => private_key, } = extract_map(cbor_value)?; } let certificate = extract_byte_string(ok_or_missing(certificate)?)?; @@ -419,8 +524,46 @@ impl TryFrom for AuthenticatorAttestationMaterial { } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorCredentialManagementParameters { + pub sub_command: CredentialManagementSubCommand, + pub sub_command_params: Option, + pub pin_uv_auth_protocol: Option, + pub pin_uv_auth_param: Option>, +} + +impl TryFrom for AuthenticatorCredentialManagementParameters { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => sub_command, + 0x02 => sub_command_params, + 0x03 => pin_uv_auth_protocol, + 0x04 => pin_uv_auth_param, + } = extract_map(cbor_value)?; + } + + let sub_command = CredentialManagementSubCommand::try_from(ok_or_missing(sub_command)?)?; + let sub_command_params = sub_command_params + .map(CredentialManagementSubCommandParameters::try_from) + .transpose()?; + let pin_uv_auth_protocol = pin_uv_auth_protocol + .map(PinUvAuthProtocol::try_from) + .transpose()?; + let pin_uv_auth_param = pin_uv_auth_param.map(extract_byte_string).transpose()?; + + Ok(AuthenticatorCredentialManagementParameters { + sub_command, + sub_command_params, + pin_uv_auth_protocol, + pin_uv_auth_param, + }) + } +} + +#[derive(Debug, PartialEq, Eq)] pub struct AuthenticatorVendorConfigureParameters { pub lockdown: bool, pub attestation_material: Option, @@ -432,8 +575,8 @@ impl TryFrom for AuthenticatorVendorConfigureParameters { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { - 1 => lockdown, - 2 => attestation_material, + 0x01 => lockdown, + 0x02 => attestation_material, } = extract_map(cbor_value)?; } let lockdown = lockdown.map_or(Ok(false), extract_bool)?; @@ -447,6 +590,32 @@ impl TryFrom for AuthenticatorVendorConfigureParameters { } } +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorVendorUpgradeParameters { + pub offset: usize, + pub data: Vec, + pub hash: [u8; 32], +} + +impl TryFrom for AuthenticatorVendorUpgradeParameters { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => offset, + 0x02 => data, + 0x03 => hash, + } = extract_map(cbor_value)?; + } + let offset = extract_unsigned(ok_or_missing(offset)?)? as usize; + let data = extract_byte_string(ok_or_missing(data)?)?; + let hash = <[u8; 32]>::try_from(extract_byte_string(ok_or_missing(hash)?)?) + .map_err(|_| Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)?; + Ok(AuthenticatorVendorUpgradeParameters { offset, data, hash }) + } +} + #[cfg(test)] mod test { use super::super::data_formats::{ @@ -455,28 +624,29 @@ mod test { }; use super::super::ES256_CRED_PARAM; use super::*; - use std::collections::BTreeMap; + use crate::env::test::TestEnv; use cbor::{cbor_array, cbor_map}; #[test] fn test_from_cbor_make_credential_parameters() { let cbor_value = cbor_map! { - 1 => vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F], - 2 => cbor_map! { + 0x01 => vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F], + 0x02 => cbor_map! { "id" => "example.com", - "name" => "Example", "icon" => "example.com/icon.png", + "name" => "Example", }, - 3 => cbor_map! { + 0x03 => cbor_map! { "id" => vec![0x1D, 0x1D, 0x1D, 0x1D], + "icon" => "example.com/foo/icon.png", "name" => "foo", "displayName" => "bar", - "icon" => "example.com/foo/icon.png", }, - 4 => cbor_array![ES256_CRED_PARAM], - 5 => cbor_array![], - 8 => vec![0x12, 0x34], - 9 => 1, + 0x04 => cbor_array![ES256_CRED_PARAM], + 0x05 => cbor_array![], + 0x08 => vec![0x12, 0x34], + 0x09 => 1, + 0x0A => 2, }; let returned_make_credential_parameters = AuthenticatorMakeCredentialParameters::try_from(cbor_value).unwrap(); @@ -506,10 +676,11 @@ mod test { user, pub_key_cred_params: vec![ES256_CRED_PARAM], exclude_list: Some(vec![]), - extensions: None, + extensions: MakeCredentialExtensions::default(), options, pin_uv_auth_param: Some(vec![0x12, 0x34]), - pin_uv_auth_protocol: Some(1), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + enterprise_attestation: Some(2), }; assert_eq!( @@ -521,15 +692,15 @@ mod test { #[test] fn test_from_cbor_get_assertion_parameters() { let cbor_value = cbor_map! { - 1 => "example.com", - 2 => vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F], - 3 => cbor_array![ cbor_map! { - "type" => "public-key", + 0x01 => "example.com", + 0x02 => vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F], + 0x03 => cbor_array![ cbor_map! { "id" => vec![0x2D, 0x2D, 0x2D, 0x2D], + "type" => "public-key", "transports" => cbor_array!["usb"], } ], - 6 => vec![0x12, 0x34], - 7 => 1, + 0x06 => vec![0x12, 0x34], + 0x07 => 1, }; let returned_get_assertion_parameters = AuthenticatorGetAssertionParameters::try_from(cbor_value).unwrap(); @@ -552,10 +723,10 @@ mod test { rp_id, client_data_hash, allow_list: Some(vec![pub_key_cred_descriptor]), - extensions: None, + extensions: GetAssertionExtensions::default(), options, pin_uv_auth_param: Some(vec![0x12, 0x34]), - pin_uv_auth_protocol: Some(1), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), }; assert_eq!( @@ -566,53 +737,38 @@ mod test { #[test] fn test_from_cbor_client_pin_parameters() { - // TODO(kaczmarczyck) inline the #cfg when #128 is resolved: - // https://github.com/google/OpenSK/issues/128 - #[cfg(not(feature = "with_ctap2_1"))] + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pk = sk.genpk(); + let cose_key = CoseKey::from(pk); + let cbor_value = cbor_map! { - 1 => 1, - 2 => ClientPinSubCommand::GetPinRetries, - 3 => cbor_map!{}, - 4 => vec! [0xBB], - 5 => vec! [0xCC], - 6 => vec! [0xDD], + 0x01 => 1, + 0x02 => ClientPinSubCommand::GetPinRetries, + 0x03 => cbor::Value::from(cose_key.clone()), + 0x04 => vec! [0xBB], + 0x05 => vec! [0xCC], + 0x06 => vec! [0xDD], + 0x09 => 0x03, + 0x0A => "example.com", }; - #[cfg(feature = "with_ctap2_1")] - let cbor_value = cbor_map! { - 1 => 1, - 2 => ClientPinSubCommand::GetPinRetries, - 3 => cbor_map!{}, - 4 => vec! [0xBB], - 5 => vec! [0xCC], - 6 => vec! [0xDD], - 7 => 4, - 8 => cbor_array!["example.com"], - 9 => 0x03, - 10 => "example.com", - }; - let returned_pin_protocol_parameters = + let returned_client_pin_parameters = AuthenticatorClientPinParameters::try_from(cbor_value).unwrap(); - let expected_pin_protocol_parameters = AuthenticatorClientPinParameters { - pin_protocol: 1, + let expected_client_pin_parameters = AuthenticatorClientPinParameters { + pin_uv_auth_protocol: PinUvAuthProtocol::V1, sub_command: ClientPinSubCommand::GetPinRetries, - key_agreement: Some(CoseKey(BTreeMap::new())), - pin_auth: Some(vec![0xBB]), + key_agreement: Some(cose_key), + pin_uv_auth_param: Some(vec![0xBB]), new_pin_enc: Some(vec![0xCC]), pin_hash_enc: Some(vec![0xDD]), - #[cfg(feature = "with_ctap2_1")] - min_pin_length: Some(4), - #[cfg(feature = "with_ctap2_1")] - min_pin_length_rp_ids: Some(vec!["example.com".to_string()]), - #[cfg(feature = "with_ctap2_1")] permissions: Some(0x03), - #[cfg(feature = "with_ctap2_1")] permissions_rp_id: Some("example.com".to_string()), }; assert_eq!( - returned_pin_protocol_parameters, - expected_pin_protocol_parameters + returned_client_pin_parameters, + expected_client_pin_parameters ); } @@ -638,7 +794,37 @@ mod test { assert_eq!(command, Ok(Command::AuthenticatorGetNextAssertion)); } - #[cfg(feature = "with_ctap2_1")] + #[test] + fn test_from_cbor_cred_management_parameters() { + let cbor_value = cbor_map! { + 0x01 => CredentialManagementSubCommand::EnumerateCredentialsBegin as u64, + 0x02 => cbor_map!{ + 0x01 => vec![0x1D; 32], + }, + 0x03 => 1, + 0x04 => vec! [0x9A; 16], + }; + let returned_cred_management_parameters = + AuthenticatorCredentialManagementParameters::try_from(cbor_value).unwrap(); + + let params = CredentialManagementSubCommandParameters { + rp_id_hash: Some(vec![0x1D; 32]), + credential_id: None, + user: None, + }; + let expected_cred_management_parameters = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateCredentialsBegin, + sub_command_params: Some(params), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param: Some(vec![0x9A; 16]), + }; + + assert_eq!( + returned_cred_management_parameters, + expected_cred_management_parameters + ); + } + #[test] fn test_deserialize_selection() { let cbor_bytes = [Command::AUTHENTICATOR_SELECTION]; @@ -646,6 +832,149 @@ mod test { assert_eq!(command, Ok(Command::AuthenticatorSelection)); } + #[test] + fn test_from_cbor_large_blobs_parameters() { + // successful get + let cbor_value = cbor_map! { + 0x01 => 2, + 0x03 => 4, + }; + let returned_large_blobs_parameters = + AuthenticatorLargeBlobsParameters::try_from(cbor_value).unwrap(); + let expected_large_blobs_parameters = AuthenticatorLargeBlobsParameters { + get: Some(2), + set: None, + offset: 4, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + assert_eq!( + returned_large_blobs_parameters, + expected_large_blobs_parameters + ); + + // successful first set + let cbor_value = cbor_map! { + 0x02 => vec! [0x5E], + 0x03 => 0, + 0x04 => MIN_LARGE_BLOB_LEN as u64, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + let returned_large_blobs_parameters = + AuthenticatorLargeBlobsParameters::try_from(cbor_value).unwrap(); + let expected_large_blobs_parameters = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(vec![0x5E]), + offset: 0, + length: Some(MIN_LARGE_BLOB_LEN), + pin_uv_auth_param: Some(vec![0xA9]), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + }; + assert_eq!( + returned_large_blobs_parameters, + expected_large_blobs_parameters + ); + + // successful next set + let cbor_value = cbor_map! { + 0x02 => vec! [0x5E], + 0x03 => 1, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + let returned_large_blobs_parameters = + AuthenticatorLargeBlobsParameters::try_from(cbor_value).unwrap(); + let expected_large_blobs_parameters = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(vec![0x5E]), + offset: 1, + length: None, + pin_uv_auth_param: Some(vec![0xA9]), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + }; + assert_eq!( + returned_large_blobs_parameters, + expected_large_blobs_parameters + ); + + // failing with neither get nor set + let cbor_value = cbor_map! { + 0x03 => 4, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // failing with get and set + let cbor_value = cbor_map! { + 0x01 => 2, + 0x02 => vec! [0x5E], + 0x03 => 4, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // failing with get and length + let cbor_value = cbor_map! { + 0x01 => 2, + 0x03 => 4, + 0x04 => MIN_LARGE_BLOB_LEN as u64, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // failing with zero offset and no length present + let cbor_value = cbor_map! { + 0x02 => vec! [0x5E], + 0x03 => 0, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // failing with length smaller than minimum + let cbor_value = cbor_map! { + 0x02 => vec! [0x5E], + 0x03 => 0, + 0x04 => MIN_LARGE_BLOB_LEN as u64 - 1, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // failing with non-zero offset and length present + let cbor_value = cbor_map! { + 0x02 => vec! [0x5E], + 0x03 => 4, + 0x04 => MIN_LARGE_BLOB_LEN as u64, + 0x05 => vec! [0xA9], + 0x06 => 1, + }; + assert_eq!( + AuthenticatorLargeBlobsParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + #[test] fn test_vendor_configure() { // Incomplete command @@ -670,10 +999,10 @@ mod test { // Attestation key is too short. let cbor_value = cbor_map! { - 1 => false, - 2 => cbor_map! { - 1 => dummy_cert, - 2 => dummy_pkey[..key_material::ATTESTATION_PRIVATE_KEY_LENGTH - 1] + 0x01 => false, + 0x02 => cbor_map! { + 0x01 => dummy_cert, + 0x02 => dummy_pkey[..key_material::ATTESTATION_PRIVATE_KEY_LENGTH - 1] } }; assert_eq!( @@ -683,9 +1012,9 @@ mod test { // Missing private key let cbor_value = cbor_map! { - 1 => false, - 2 => cbor_map! { - 1 => dummy_cert + 0x01 => false, + 0x02 => cbor_map! { + 0x01 => dummy_cert } }; assert_eq!( @@ -695,9 +1024,9 @@ mod test { // Missing certificate let cbor_value = cbor_map! { - 1 => false, - 2 => cbor_map! { - 2 => dummy_pkey + 0x01 => false, + 0x02 => cbor_map! { + 0x02 => dummy_pkey } }; assert_eq!( @@ -707,11 +1036,11 @@ mod test { // Valid let cbor_value = cbor_map! { - 1 => false, - 2 => cbor_map! { - 1 => dummy_cert, - 2 => dummy_pkey - } + 0x01 => false, + 0x02 => cbor_map! { + 0x01 => dummy_cert, + 0x02 => dummy_pkey + }, }; assert_eq!( AuthenticatorVendorConfigureParameters::try_from(cbor_value), @@ -720,8 +1049,79 @@ mod test { attestation_material: Some(AuthenticatorAttestationMaterial { certificate: dummy_cert.to_vec(), private_key: dummy_pkey - }) + }), }) ); } + + #[test] + fn test_vendor_upgrade() { + // Incomplete command + let cbor_bytes = vec![Command::AUTHENTICATOR_VENDOR_UPGRADE]; + let command = Command::deserialize(&cbor_bytes); + assert_eq!(command, Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR)); + + // Missing offset + let cbor_value = cbor_map! { + 0x02 => [0xFF; 0x100], + 0x03 => [0x44; 32], + }; + assert_eq!( + AuthenticatorVendorUpgradeParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER) + ); + + // Missing data + let cbor_value = cbor_map! { + 0x01 => 0x1000, + 0x03 => [0x44; 32], + }; + assert_eq!( + AuthenticatorVendorUpgradeParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER) + ); + + // Invalid hash size + let cbor_value = cbor_map! { + 0x01 => 0x1000, + 0x02 => [0xFF; 0x100], + 0x03 => [0x44; 33], + }; + assert_eq!( + AuthenticatorVendorUpgradeParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + // Missing hash + let cbor_value = cbor_map! { + 0x01 => 0x1000, + 0x02 => [0xFF; 0x100], + }; + assert_eq!( + AuthenticatorVendorUpgradeParameters::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER) + ); + + // Valid + let cbor_value = cbor_map! { + 0x01 => 0x1000, + 0x02 => [0xFF; 0x100], + 0x03 => [0x44; 32], + }; + assert_eq!( + AuthenticatorVendorUpgradeParameters::try_from(cbor_value), + Ok(AuthenticatorVendorUpgradeParameters { + offset: 0x1000, + data: vec![0xFF; 0x100], + hash: [0x44; 32], + }) + ); + } + + #[test] + fn test_deserialize_vendor_upgrade_info() { + let cbor_bytes = [Command::AUTHENTICATOR_VENDOR_UPGRADE_INFO]; + let command = Command::deserialize(&cbor_bytes); + assert_eq!(command, Ok(Command::AuthenticatorVendorUpgradeInfo)); + } } diff --git a/apps/vault/src/ctap/config_command.rs b/apps/vault/src/ctap/config_command.rs new file mode 100644 index 000000000..52bfd4274 --- /dev/null +++ b/apps/vault/src/ctap/config_command.rs @@ -0,0 +1,457 @@ +// Copyright 2020-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::client_pin::{ClientPin, PinPermission}; +use super::command::AuthenticatorConfigParameters; +use super::data_formats::{ConfigSubCommand, ConfigSubCommandParams, SetMinPinLengthParams}; +use super::response::ResponseData; +use super::status_code::Ctap2StatusCode; +use crate::api::customization::Customization; +use crate::ctap::storage; +use crate::env::Env; +use std::vec; + +/// Processes the subcommand enableEnterpriseAttestation for AuthenticatorConfig. +fn process_enable_enterprise_attestation( + env: &mut impl Env, +) -> Result { + if env.customization().enterprise_attestation_mode().is_some() { + storage::enable_enterprise_attestation(env)?; + Ok(ResponseData::AuthenticatorConfig) + } else { + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + } +} + +/// Processes the subcommand toggleAlwaysUv for AuthenticatorConfig. +fn process_toggle_always_uv(env: &mut impl Env) -> Result { + storage::toggle_always_uv(env)?; + Ok(ResponseData::AuthenticatorConfig) +} + +/// Processes the subcommand setMinPINLength for AuthenticatorConfig. +fn process_set_min_pin_length( + env: &mut impl Env, + params: SetMinPinLengthParams, +) -> Result { + let SetMinPinLengthParams { + new_min_pin_length, + min_pin_length_rp_ids, + force_change_pin, + } = params; + let store_min_pin_length = storage::min_pin_length(env)?; + let new_min_pin_length = new_min_pin_length.unwrap_or(store_min_pin_length); + if new_min_pin_length < store_min_pin_length { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION); + } + let mut force_change_pin = force_change_pin.unwrap_or(false); + if force_change_pin && storage::pin_hash(env)?.is_none() { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_NOT_SET); + } + if let Some(old_length) = storage::pin_code_point_length(env)? { + force_change_pin |= new_min_pin_length > old_length; + } + if force_change_pin { + storage::force_pin_change(env)?; + } + storage::set_min_pin_length(env, new_min_pin_length)?; + if let Some(min_pin_length_rp_ids) = min_pin_length_rp_ids { + storage::set_min_pin_length_rp_ids(env, min_pin_length_rp_ids)?; + } + Ok(ResponseData::AuthenticatorConfig) +} + +/// Processes the AuthenticatorConfig command. +pub fn process_config( + env: &mut impl Env, + client_pin: &mut ClientPin, + params: AuthenticatorConfigParameters, +) -> Result { + let AuthenticatorConfigParameters { + sub_command, + sub_command_params, + pin_uv_auth_param, + pin_uv_auth_protocol, + } = params; + + let enforce_uv = + !matches!(sub_command, ConfigSubCommand::ToggleAlwaysUv) && storage::has_always_uv(env)?; + if storage::pin_hash(env)?.is_some() || enforce_uv { + let pin_uv_auth_param = + pin_uv_auth_param.ok_or(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED)?; + let pin_uv_auth_protocol = + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + // Constants are taken from the specification, section 6.11, step 4.2. + let mut config_data = vec![0xFF; 32]; + config_data.extend(&[0x0D, sub_command as u8]); + if let Some(sub_command_params) = sub_command_params.clone() { + super::cbor_write(sub_command_params.into(), &mut config_data)?; + } + client_pin.verify_pin_uv_auth_token( + &config_data, + &pin_uv_auth_param, + pin_uv_auth_protocol, + )?; + client_pin.has_permission(PinPermission::AuthenticatorConfiguration)?; + } + + match sub_command { + ConfigSubCommand::EnableEnterpriseAttestation => process_enable_enterprise_attestation(env), + ConfigSubCommand::ToggleAlwaysUv => process_toggle_always_uv(env), + ConfigSubCommand::SetMinPinLength => { + if let Some(ConfigSubCommandParams::SetMinPinLength(params)) = sub_command_params { + process_set_min_pin_length(env, params) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER) + } + } + _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::api::customization::Customization; + use crate::ctap::data_formats::PinUvAuthProtocol; + use crate::ctap::pin_protocol::authenticate_pin_uv_auth_token; + use crate::env::test::TestEnv; + + #[test] + fn test_process_enable_enterprise_attestation() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::EnableEnterpriseAttestation, + sub_command_params: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + + if env.customization().enterprise_attestation_mode().is_some() { + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::enterprise_attestation(&mut env), Ok(true)); + } else { + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + } + + #[test] + fn test_process_toggle_always_uv() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::ToggleAlwaysUv, + sub_command_params: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert!(storage::has_always_uv(&mut env).unwrap()); + + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::ToggleAlwaysUv, + sub_command_params: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + if env.customization().enforce_always_uv() { + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP2_ERR_OPERATION_DENIED) + ); + } else { + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert!(!storage::has_always_uv(&mut env).unwrap()); + } + } + + fn test_helper_process_toggle_always_uv_with_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + + let mut config_data = vec![0xFF; 32]; + config_data.extend(&[0x0D, ConfigSubCommand::ToggleAlwaysUv as u8]); + let pin_uv_auth_param = + authenticate_pin_uv_auth_token(&pin_uv_auth_token, &config_data, pin_uv_auth_protocol); + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::ToggleAlwaysUv, + sub_command_params: None, + pin_uv_auth_param: Some(pin_uv_auth_param.clone()), + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + if env.customization().enforce_always_uv() { + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP2_ERR_OPERATION_DENIED) + ); + return; + } + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert!(storage::has_always_uv(&mut env).unwrap()); + + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::ToggleAlwaysUv, + sub_command_params: None, + pin_uv_auth_param: Some(pin_uv_auth_param), + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert!(!storage::has_always_uv(&mut env).unwrap()); + } + + #[test] + fn test_process_toggle_always_uv_with_pin_v1() { + test_helper_process_toggle_always_uv_with_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_toggle_always_uv_with_pin_v2() { + test_helper_process_toggle_always_uv_with_pin(PinUvAuthProtocol::V2); + } + + fn create_min_pin_config_params( + min_pin_length: u8, + min_pin_length_rp_ids: Option>, + ) -> AuthenticatorConfigParameters { + let set_min_pin_length_params = SetMinPinLengthParams { + new_min_pin_length: Some(min_pin_length), + min_pin_length_rp_ids, + force_change_pin: None, + }; + AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::SetMinPinLength, + sub_command_params: Some(ConfigSubCommandParams::SetMinPinLength( + set_min_pin_length_params, + )), + pin_uv_auth_param: None, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + } + } + + #[test] + fn test_process_set_min_pin_length() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + // First, increase minimum PIN length from 4 to 6 without PIN auth. + let min_pin_length = 6; + let config_params = create_min_pin_config_params(min_pin_length, None); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + + // Second, increase minimum PIN length from 6 to 8 with PIN auth. + // The stored PIN or its length don't matter since we control the token. + storage::set_pin(&mut env, &[0x88; 16], 8).unwrap(); + let min_pin_length = 8; + let mut config_params = create_min_pin_config_params(min_pin_length, None); + let pin_uv_auth_param = vec![ + 0x5C, 0x69, 0x71, 0x29, 0xBD, 0xCC, 0x53, 0xE8, 0x3C, 0x97, 0x62, 0xDD, 0x90, 0x29, + 0xB2, 0xDE, + ]; + config_params.pin_uv_auth_param = Some(pin_uv_auth_param); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + + // Third, decreasing the minimum PIN length from 8 to 7 fails. + let mut config_params = create_min_pin_config_params(7, None); + let pin_uv_auth_param = vec![ + 0xC5, 0xEA, 0xC1, 0x5E, 0x7F, 0x80, 0x70, 0x1A, 0x4E, 0xC4, 0xAD, 0x85, 0x35, 0xD8, + 0xA7, 0x71, + ]; + config_params.pin_uv_auth_param = Some(pin_uv_auth_param); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION) + ); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + } + + #[test] + fn test_process_set_min_pin_length_rp_ids() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + // First, set RP IDs without PIN auth. + let min_pin_length = 6; + let min_pin_length_rp_ids = vec!["example.com".to_string()]; + let config_params = + create_min_pin_config_params(min_pin_length, Some(min_pin_length_rp_ids.clone())); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + assert_eq!( + storage::min_pin_length_rp_ids(&mut env), + Ok(min_pin_length_rp_ids) + ); + + // Second, change the RP IDs with PIN auth. + let min_pin_length = 8; + let min_pin_length_rp_ids = vec!["another.example.com".to_string()]; + // The stored PIN or its length don't matter since we control the token. + storage::set_pin(&mut env, &[0x88; 16], 8).unwrap(); + let mut config_params = + create_min_pin_config_params(min_pin_length, Some(min_pin_length_rp_ids.clone())); + let pin_uv_auth_param = vec![ + 0x40, 0x51, 0x2D, 0xAC, 0x2D, 0xE2, 0x15, 0x77, 0x5C, 0xF9, 0x5B, 0x62, 0x9A, 0x2D, + 0xD6, 0xDA, + ]; + config_params.pin_uv_auth_param = Some(pin_uv_auth_param.clone()); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + assert_eq!( + storage::min_pin_length_rp_ids(&mut env), + Ok(min_pin_length_rp_ids.clone()) + ); + + // Third, changing RP IDs with bad PIN auth fails. + // One PIN auth shouldn't work for different lengths. + let mut config_params = + create_min_pin_config_params(9, Some(min_pin_length_rp_ids.clone())); + config_params.pin_uv_auth_param = Some(pin_uv_auth_param.clone()); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + assert_eq!( + storage::min_pin_length_rp_ids(&mut env), + Ok(min_pin_length_rp_ids.clone()) + ); + + // Forth, changing RP IDs with bad PIN auth fails. + // One PIN auth shouldn't work for different RP IDs. + let mut config_params = create_min_pin_config_params( + min_pin_length, + Some(vec!["counter.example.com".to_string()]), + ); + config_params.pin_uv_auth_param = Some(pin_uv_auth_param); + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + assert_eq!( + storage::min_pin_length_rp_ids(&mut env), + Ok(min_pin_length_rp_ids) + ); + } + + #[test] + fn test_process_set_min_pin_length_force_pin_change_implicit() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + // Increase min PIN, force PIN change. + let min_pin_length = 6; + let mut config_params = create_min_pin_config_params(min_pin_length, None); + let pin_uv_auth_param = Some(vec![ + 0x81, 0x37, 0x37, 0xF3, 0xD8, 0x69, 0xBD, 0x74, 0xFE, 0x88, 0x30, 0x8C, 0xC4, 0x2E, + 0xA8, 0xC8, + ]); + config_params.pin_uv_auth_param = pin_uv_auth_param; + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::min_pin_length(&mut env), Ok(min_pin_length)); + assert_eq!(storage::has_force_pin_change(&mut env), Ok(true)); + } + + #[test] + fn test_process_set_min_pin_length_force_pin_change_explicit() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0xE3, 0x74, 0xF4, 0x27, 0xBE, 0x7D, 0x40, 0xB5, 0x71, 0xB6, 0xB4, 0x1A, 0xD2, 0xC1, + 0x53, 0xD7, + ]); + let set_min_pin_length_params = SetMinPinLengthParams { + new_min_pin_length: Some(storage::min_pin_length(&mut env).unwrap()), + min_pin_length_rp_ids: None, + force_change_pin: Some(true), + }; + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::SetMinPinLength, + sub_command_params: Some(ConfigSubCommandParams::SetMinPinLength( + set_min_pin_length_params, + )), + pin_uv_auth_param, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!(config_response, Ok(ResponseData::AuthenticatorConfig)); + assert_eq!(storage::has_force_pin_change(&mut env), Ok(true)); + } + + #[test] + fn test_process_config_vendor_prototype() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + let config_params = AuthenticatorConfigParameters { + sub_command: ConfigSubCommand::VendorPrototype, + sub_command_params: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let config_response = process_config(&mut env, &mut client_pin, config_params); + assert_eq!( + config_response, + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } +} diff --git a/apps/vault/src/ctap/credential_id.rs b/apps/vault/src/ctap/credential_id.rs new file mode 100644 index 000000000..5824abd06 --- /dev/null +++ b/apps/vault/src/ctap/credential_id.rs @@ -0,0 +1,495 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::crypto_wrapper::{aes256_cbc_decrypt, aes256_cbc_encrypt, PrivateKey}; +use super::data_formats::{ + CredentialProtectionPolicy, PublicKeyCredentialSource, PublicKeyCredentialType, +}; +use super::status_code::Ctap2StatusCode; +use super::{cbor_read, cbor_write}; +use crate::api::key_store::KeyStore; +use crate::ctap::data_formats::{extract_byte_string, extract_map}; +use crate::env::Env; +use std::string::String; +use std::vec::Vec; +use core::convert::{TryFrom, TryInto}; +use ctap_crypto::hmac::{hmac_256, verify_hmac_256}; +use ctap_crypto::sha256::Sha256; +use cbor::{cbor_map_options, destructure_cbor_map}; +use arrayref::array_ref; + +pub const LEGACY_CREDENTIAL_ID_SIZE: usize = 112; +// CBOR credential IDs consist of +// - 1 byte : version number +// - 16 bytes: initialization vector for AES-256, +// - 192 bytes: encrypted block of the key handle cbor, +// - 32 bytes: HMAC-SHA256 over everything else. +pub const CBOR_CREDENTIAL_ID_SIZE: usize = 241; +pub const MIN_CREDENTIAL_ID_SIZE: usize = LEGACY_CREDENTIAL_ID_SIZE; +pub const MAX_CREDENTIAL_ID_SIZE: usize = CBOR_CREDENTIAL_ID_SIZE; + +pub const CBOR_CREDENTIAL_ID_VERSION: u8 = 0x01; + +pub const MAX_PADDING_LENGTH: u8 = 0xBF; + +// Data fields that are contained in the credential ID of non-discoverable credentials. +struct CredentialSource { + private_key: PrivateKey, + rp_id_hash: [u8; 32], + cred_protect_policy: Option, + cred_blob: Option>, +} + +// The data fields contained in the credential ID are serialized using CBOR maps. +// Each field is associated with a unique tag, implemented with a CBOR unsigned key. +enum CredentialSourceField { + PrivateKey = 0, + RpIdHash = 1, + CredProtectPolicy = 2, + CredBlob = 3, +} + +impl From for cbor::Value { + fn from(field: CredentialSourceField) -> cbor::Value { + (field as u64).into() + } +} + +fn decrypt_legacy_credential_id( + env: &mut impl Env, + bytes: &[u8], +) -> Result, Ctap2StatusCode> { + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&env.key_store().key_handle_encryption()?); + let plaintext = aes256_cbc_decrypt(&aes_enc_key, bytes, true)?; + if plaintext.len() != 64 { + return Ok(None); + } + let private_key = if let Some(key) = PrivateKey::new_ecdsa_from_bytes(&plaintext[..32]) { + key + } else { + return Ok(None); + }; + Ok(Some(CredentialSource { + private_key, + rp_id_hash: plaintext[32..64].try_into().unwrap(), + cred_protect_policy: None, + cred_blob: None, + })) +} + +fn decrypt_cbor_credential_id( + env: &mut impl Env, + bytes: &[u8], +) -> Result, Ctap2StatusCode> { + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&env.key_store().key_handle_encryption()?); + let mut plaintext = aes256_cbc_decrypt(&aes_enc_key, bytes, true)?; + remove_padding(&mut plaintext)?; + + let cbor_credential_source = cbor_read(plaintext.as_slice())?; + destructure_cbor_map! { + let { + CredentialSourceField::PrivateKey => private_key, + CredentialSourceField::RpIdHash=> rp_id_hash, + CredentialSourceField::CredProtectPolicy => cred_protect_policy, + CredentialSourceField::CredBlob => cred_blob, + } = extract_map(cbor_credential_source)?; + } + Ok(match (private_key, rp_id_hash) { + (Some(private_key), Some(rp_id_hash)) => { + let private_key = PrivateKey::try_from(private_key)?; + let rp_id_hash = extract_byte_string(rp_id_hash)?; + if rp_id_hash.len() != 32 { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + let cred_protect_policy = cred_protect_policy + .map(CredentialProtectionPolicy::try_from) + .transpose()?; + let cred_blob = cred_blob.map(extract_byte_string).transpose()?; + Some(CredentialSource { + private_key, + rp_id_hash: rp_id_hash.try_into().unwrap(), + cred_protect_policy, + cred_blob, + }) + } + _ => None, + }) +} + +/// Pad data to MAX_PADDING_LENGTH+1 (192) bytes using PKCS padding scheme. +/// Let N = 192 - data.len(), the PKCS padding scheme would pad N bytes of N after the data. +fn add_padding(data: &mut Vec) -> Result<(), Ctap2StatusCode> { + // The data should be between 1 to MAX_PADDING_LENGTH bytes for the padding scheme to be valid. + if data.is_empty() || data.len() > MAX_PADDING_LENGTH as usize { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + let pad_length = MAX_PADDING_LENGTH - (data.len() as u8 - 1); + data.extend(core::iter::repeat(pad_length).take(pad_length as usize)); + Ok(()) +} + +fn remove_padding(data: &mut Vec) -> Result<(), Ctap2StatusCode> { + if data.len() != MAX_PADDING_LENGTH as usize + 1 { + // This is an internal error instead of corrupted credential ID which we should just ignore because + // we've already checked that the HMAC matched. + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + let pad_length = *data.last().unwrap(); + if pad_length == 0 || pad_length > MAX_PADDING_LENGTH { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + if !data + .drain((data.len() - pad_length as usize)..) + .all(|x| x == pad_length) + { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + Ok(()) +} + +/// Encrypts the given private key, relying party ID hash, and some other metadata into a credential ID. +/// +/// Other information, such as a user name, are not stored. Since encrypted credential IDs are +/// stored server-side, this information is already available (unencrypted). +pub fn encrypt_to_credential_id( + env: &mut impl Env, + private_key: &PrivateKey, + rp_id_hash: &[u8; 32], + cred_protect_policy: Option, + cred_blob: Option>, +) -> Result, Ctap2StatusCode> { + let mut payload = Vec::new(); + let cbor = cbor_map_options! { + CredentialSourceField::PrivateKey => private_key, + CredentialSourceField::RpIdHash=> rp_id_hash, + CredentialSourceField::CredProtectPolicy => cred_protect_policy, + CredentialSourceField::CredBlob => cred_blob, + }; + cbor_write(cbor, &mut payload)?; + add_padding(&mut payload)?; + + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&env.key_store().key_handle_encryption()?); + let encrypted_payload = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &payload, true)?; + let mut credential_id = encrypted_payload; + credential_id.insert(0, CBOR_CREDENTIAL_ID_VERSION); + + let id_hmac = hmac_256::( + &env.key_store().key_handle_authentication()?, + &credential_id[..], + ); + credential_id.extend(&id_hmac); + Ok(credential_id) +} + +/// Decrypts the given credential ID into a PublicKeyCredentialSource, populating only the recorded fields. +/// +/// Returns None if +/// - the format does not match any known versions, or +/// - the HMAC test fails. +/// +/// For v0 (legacy U2F) the credential ID consists of: +/// - 16 bytes: initialization vector for AES-256, +/// - 32 bytes: encrypted ECDSA private key for the credential, +/// - 32 bytes: encrypted relying party ID hashed with SHA256, +/// - 32 bytes: HMAC-SHA256 over everything else. +/// +/// For v1 (CBOR) the credential ID consists of: +/// - 1 byte : version number, +/// - 16 bytes: initialization vector for AES-256, +/// - 192 bytes: encrypted CBOR-encoded credential source fields, +/// - 32 bytes: HMAC-SHA256 over everything else. +pub fn decrypt_credential_id( + env: &mut impl Env, + credential_id: Vec, + rp_id_hash: &[u8], +) -> Result, Ctap2StatusCode> { + if credential_id.len() < MIN_CREDENTIAL_ID_SIZE { + return Ok(None); + } + let hmac_message_size = credential_id.len() - 32; + if !verify_hmac_256::( + &env.key_store().key_handle_authentication()?, + &credential_id[..hmac_message_size], + array_ref![credential_id, hmac_message_size, 32], + ) { + return Ok(None); + } + + let credential_source = if credential_id.len() == LEGACY_CREDENTIAL_ID_SIZE { + decrypt_legacy_credential_id(env, &credential_id[..hmac_message_size])? + } else { + match credential_id[0] { + CBOR_CREDENTIAL_ID_VERSION => { + if credential_id.len() != CBOR_CREDENTIAL_ID_SIZE { + return Ok(None); + } + decrypt_cbor_credential_id(env, &credential_id[1..hmac_message_size])? + } + _ => return Ok(None), + } + }; + + let credential_source = if let Some(credential_source) = credential_source { + credential_source + } else { + return Ok(None); + }; + + if rp_id_hash != credential_source.rp_id_hash { + return Ok(None); + } + + Ok(Some(PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id, + private_key: credential_source.private_key, + rp_id: String::new(), + user_handle: Vec::new(), + user_display_name: None, + cred_protect_policy: credential_source.cred_protect_policy, + creation_order: 0, + user_name: None, + user_icon: None, + cred_blob: credential_source.cred_blob, + large_blob_key: None, + })) +} + +#[cfg(test)] +mod test { + use super::*; + use crate::api::customization::Customization; + use crate::ctap::credential_id::CBOR_CREDENTIAL_ID_SIZE; + use crate::ctap::SignatureAlgorithm; + use crate::env::test::TestEnv; + use ctap_crypto::hmac::hmac_256; + + const UNSUPPORTED_CREDENTIAL_ID_VERSION: u8 = 0x80; + + fn test_encrypt_decrypt_credential(signature_algorithm: SignatureAlgorithm) { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, signature_algorithm); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, None).unwrap(); + let decrypted_source = decrypt_credential_id(&mut env, encrypted_id, &rp_id_hash) + .unwrap() + .unwrap(); + + assert_eq!(private_key, decrypted_source.private_key); + } + + #[test] + fn test_encrypt_decrypt_ecdsa_credential() { + test_encrypt_decrypt_credential(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_encrypt_decrypt_ed25519_credential() { + test_encrypt_decrypt_credential(SignatureAlgorithm::Eddsa); + } + + #[test] + fn test_encrypt_decrypt_bad_version() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + + let rp_id_hash = [0x55; 32]; + let mut encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, None).unwrap(); + encrypted_id[0] = UNSUPPORTED_CREDENTIAL_ID_VERSION; + // Override the HMAC to pass the check. + encrypted_id.truncate(&encrypted_id.len() - 32); + let hmac_key = env.key_store().key_handle_authentication().unwrap(); + let id_hmac = hmac_256::(&hmac_key, &encrypted_id[..]); + encrypted_id.extend(&id_hmac); + + assert_eq!( + decrypt_credential_id(&mut env, encrypted_id, &rp_id_hash), + Ok(None) + ); + } + + fn test_encrypt_decrypt_bad_hmac(signature_algorithm: SignatureAlgorithm) { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, signature_algorithm); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, None).unwrap(); + for i in 0..encrypted_id.len() { + let mut modified_id = encrypted_id.clone(); + modified_id[i] ^= 0x01; + assert_eq!( + decrypt_credential_id(&mut env, modified_id, &rp_id_hash), + Ok(None) + ); + } + } + + #[test] + fn test_ecdsa_encrypt_decrypt_bad_hmac() { + test_encrypt_decrypt_bad_hmac(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_ed25519_encrypt_decrypt_bad_hmac() { + test_encrypt_decrypt_bad_hmac(SignatureAlgorithm::Eddsa); + } + + fn test_decrypt_credential_missing_blocks(signature_algorithm: SignatureAlgorithm) { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, signature_algorithm); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, None).unwrap(); + + for length in (1..CBOR_CREDENTIAL_ID_SIZE).step_by(16) { + assert_eq!( + decrypt_credential_id(&mut env, encrypted_id[..length].to_vec(), &rp_id_hash), + Ok(None) + ); + } + } + + #[test] + fn test_ecdsa_decrypt_credential_missing_blocks() { + test_decrypt_credential_missing_blocks(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_ed25519_decrypt_credential_missing_blocks() { + test_decrypt_credential_missing_blocks(SignatureAlgorithm::Eddsa); + } + + /// This is a copy of the function that genereated deprecated key handles. + fn legacy_encrypt_to_credential_id( + env: &mut impl Env, + private_key: ctap_crypto::ecdsa::SecKey, + application: &[u8; 32], + ) -> Result, Ctap2StatusCode> { + let aes_enc_key = + ctap_crypto::aes256::EncryptionKey::new(&env.key_store().key_handle_encryption()?); + let mut plaintext = [0; 64]; + private_key.to_bytes(array_mut_ref!(plaintext, 0, 32)); + plaintext[32..64].copy_from_slice(application); + + let mut encrypted_id = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, true)?; + let id_hmac = hmac_256::( + &env.key_store().key_handle_authentication()?, + &encrypted_id[..], + ); + encrypted_id.extend(&id_hmac); + Ok(encrypted_id) + } + + #[test] + fn test_encrypt_decrypt_credential_legacy() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let ecdsa_key = private_key.ecdsa_key(&mut env).unwrap(); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = + legacy_encrypt_to_credential_id(&mut env, ecdsa_key, &rp_id_hash).unwrap(); + let decrypted_source = decrypt_credential_id(&mut env, encrypted_id, &rp_id_hash) + .unwrap() + .unwrap(); + + assert_eq!(private_key, decrypted_source.private_key); + // Legacy credentials didn't persist credProtectPolicy info, so it should be treated as None. + assert!(decrypted_source.cred_protect_policy.is_none()); + } + + #[test] + fn test_encrypt_credential_size() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, None).unwrap(); + assert_eq!(encrypted_id.len(), CBOR_CREDENTIAL_ID_SIZE); + } + + #[test] + fn test_encrypt_credential_max_cbor_size() { + // The cbor encoding length is variadic and depends on size of fields. Try to put maximum length + // for each encoded field and ensure that it doesn't go over the padding size. + let mut env = TestEnv::new(); + // Currently all private key types have same length when transformed to bytes. + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let rp_id_hash = [0x55; 32]; + let cred_protect_policy = Some(CredentialProtectionPolicy::UserVerificationOptional); + let cred_blob = Some(vec![0x55; env.customization().max_cred_blob_length()]); + + let encrypted_id = encrypt_to_credential_id( + &mut env, + &private_key, + &rp_id_hash, + cred_protect_policy, + cred_blob, + ); + + assert!(encrypted_id.is_ok()); + } + + #[test] + fn test_cred_protect_persisted() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + + let rp_id_hash = [0x55; 32]; + let encrypted_id = encrypt_to_credential_id( + &mut env, + &private_key, + &rp_id_hash, + Some(CredentialProtectionPolicy::UserVerificationRequired), + None, + ) + .unwrap(); + + let decrypted_source = decrypt_credential_id(&mut env, encrypted_id, &rp_id_hash) + .unwrap() + .unwrap(); + assert_eq!(decrypted_source.private_key, private_key); + assert_eq!( + decrypted_source.cred_protect_policy, + Some(CredentialProtectionPolicy::UserVerificationRequired) + ); + } + + #[test] + fn test_cred_blob_persisted() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + + let rp_id_hash = [0x55; 32]; + let cred_blob = Some(vec![0x55; env.customization().max_cred_blob_length()]); + let encrypted_id = + encrypt_to_credential_id(&mut env, &private_key, &rp_id_hash, None, cred_blob.clone()) + .unwrap(); + + let decrypted_source = decrypt_credential_id(&mut env, encrypted_id, &rp_id_hash) + .unwrap() + .unwrap(); + assert_eq!(decrypted_source.private_key, private_key); + assert_eq!(decrypted_source.cred_blob, cred_blob); + } +} diff --git a/apps/vault/src/ctap/credential_management.rs b/apps/vault/src/ctap/credential_management.rs new file mode 100644 index 000000000..3dd988a37 --- /dev/null +++ b/apps/vault/src/ctap/credential_management.rs @@ -0,0 +1,913 @@ +// Copyright 2020-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::time::Instant; +use super::client_pin::{ClientPin, PinPermission}; +use super::command::AuthenticatorCredentialManagementParameters; +use super::data_formats::{ + CredentialManagementSubCommand, CredentialManagementSubCommandParameters, + PublicKeyCredentialDescriptor, PublicKeyCredentialRpEntity, PublicKeyCredentialSource, + PublicKeyCredentialUserEntity, +}; +use super::response::{AuthenticatorCredentialManagementResponse, ResponseData}; +use super::status_code::Ctap2StatusCode; +use super::{Channel, StatefulCommand, StatefulPermission}; +use crate::ctap::storage; +use crate::env::Env; +use std::collections::BTreeSet; +use std::string::String; +use std::vec; +use std::vec::Vec; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; + +/// Generates a set with all existing RP IDs. +fn get_stored_rp_ids(env: &mut impl Env) -> Result, Ctap2StatusCode> { + let mut rp_set = BTreeSet::new(); + let mut iter_result = Ok(()); + for (_, credential) in storage::iter_credentials(env, &mut iter_result)? { + rp_set.insert(credential.rp_id); + } + iter_result?; + Ok(rp_set) +} + +/// Generates the response for subcommands enumerating RPs. +fn enumerate_rps_response( + rp_id: String, + total_rps: Option, +) -> Result { + let rp_id_hash = Some(Sha256::hash(rp_id.as_bytes()).to_vec()); + let rp = Some(PublicKeyCredentialRpEntity { + rp_id, + rp_name: None, + rp_icon: None, + }); + Ok(AuthenticatorCredentialManagementResponse { + rp, + rp_id_hash, + total_rps, + ..Default::default() + }) +} + +/// Generates the response for subcommands enumerating credentials. +fn enumerate_credentials_response( + env: &mut impl Env, + credential: PublicKeyCredentialSource, + total_credentials: Option, +) -> Result { + let PublicKeyCredentialSource { + key_type, + credential_id, + private_key, + rp_id: _, + user_handle, + user_display_name, + cred_protect_policy, + creation_order: _, + user_name, + user_icon, + cred_blob: _, + large_blob_key, + } = credential; + let user = PublicKeyCredentialUserEntity { + user_id: user_handle, + user_name, + user_display_name, + user_icon, + }; + let credential_id = PublicKeyCredentialDescriptor { + key_type, + key_id: credential_id, + transports: None, // You can set USB as a hint here. + }; + let public_key = private_key.get_pub_key(env)?; + Ok(AuthenticatorCredentialManagementResponse { + user: Some(user), + credential_id: Some(credential_id), + public_key: Some(public_key), + total_credentials, + cred_protect: cred_protect_policy, + large_blob_key, + ..Default::default() + }) +} + +/// Check if the token permissions have the correct associated RP ID. +/// +/// Either no RP ID is associated, or the RP ID matches the stored credential. +fn check_rp_id_permissions( + env: &mut impl Env, + client_pin: &mut ClientPin, + credential_id: &[u8], +) -> Result<(), Ctap2StatusCode> { + // Pre-check a sufficient condition before calling the store. + if client_pin.has_no_rp_id_permission().is_ok() { + return Ok(()); + } + let (_, credential) = storage::find_credential_item(env, credential_id)?; + client_pin.has_no_or_rp_id_permission(&credential.rp_id) +} + +/// Processes the subcommand getCredsMetadata for CredentialManagement. +fn process_get_creds_metadata( + env: &mut impl Env, +) -> Result { + Ok(AuthenticatorCredentialManagementResponse { + existing_resident_credentials_count: Some(storage::count_credentials(env)? as u64), + max_possible_remaining_resident_credentials_count: Some( + storage::remaining_credentials(env)? as u64, + ), + ..Default::default() + }) +} + +/// Processes the subcommand enumerateRPsBegin for CredentialManagement. +fn process_enumerate_rps_begin( + env: &mut impl Env, + stateful_command_permission: &mut StatefulPermission, + channel: Channel, + now: Instant, +) -> Result { + let rp_set = get_stored_rp_ids(env)?; + let total_rps = rp_set.len(); + + if total_rps > 1 { + stateful_command_permission.set_command(now, StatefulCommand::EnumerateRps(1), channel); + } + // TODO https://github.com/rust-lang/rust/issues/62924 replace with pop_first() + let rp_id = rp_set + .into_iter() + .next() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS)?; + enumerate_rps_response(rp_id, Some(total_rps as u64)) +} + +/// Processes the subcommand enumerateRPsGetNextRP for CredentialManagement. +fn process_enumerate_rps_get_next_rp( + env: &mut impl Env, + stateful_command_permission: &mut StatefulPermission, +) -> Result { + let rp_id_index = stateful_command_permission.next_enumerate_rp()?; + let rp_set = get_stored_rp_ids(env)?; + // A BTreeSet is already sorted. + let rp_id = rp_set + .into_iter() + .nth(rp_id_index) + .ok_or(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)?; + enumerate_rps_response(rp_id, None) +} + +/// Processes the subcommand enumerateCredentialsBegin for CredentialManagement. +fn process_enumerate_credentials_begin( + env: &mut impl Env, + stateful_command_permission: &mut StatefulPermission, + client_pin: &mut ClientPin, + sub_command_params: CredentialManagementSubCommandParameters, + channel: Channel, + now: Instant, +) -> Result { + let rp_id_hash = sub_command_params + .rp_id_hash + .ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + client_pin.has_no_or_rp_id_hash_permission(&rp_id_hash[..])?; + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(env, &mut iter_result)?; + let mut rp_credentials: Vec = iter + .filter_map(|(key, credential)| { + let cred_rp_id_hash = Sha256::hash(credential.rp_id.as_bytes()); + if cred_rp_id_hash == rp_id_hash.as_slice() { + Some(key) + } else { + None + } + }) + .collect(); + iter_result?; + let total_credentials = rp_credentials.len(); + let current_key = rp_credentials + .pop() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS)?; + let credential = storage::get_credential(env, current_key)?; + if total_credentials > 1 { + stateful_command_permission.set_command( + now, + StatefulCommand::EnumerateCredentials(rp_credentials), + channel, + ); + } + enumerate_credentials_response(env, credential, Some(total_credentials as u64)) +} + +/// Processes the subcommand enumerateCredentialsGetNextCredential for CredentialManagement. +fn process_enumerate_credentials_get_next_credential( + env: &mut impl Env, + stateful_command_permission: &mut StatefulPermission, +) -> Result { + let credential_key = stateful_command_permission.next_enumerate_credential()?; + let credential = storage::get_credential(env, credential_key)?; + enumerate_credentials_response(env, credential, None) +} + +/// Processes the subcommand deleteCredential for CredentialManagement. +fn process_delete_credential( + env: &mut impl Env, + client_pin: &mut ClientPin, + sub_command_params: CredentialManagementSubCommandParameters, +) -> Result<(), Ctap2StatusCode> { + let credential_id = sub_command_params + .credential_id + .ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)? + .key_id; + check_rp_id_permissions(env, client_pin, &credential_id)?; + storage::delete_credential(env, &credential_id) +} + +/// Processes the subcommand updateUserInformation for CredentialManagement. +fn process_update_user_information( + env: &mut impl Env, + client_pin: &mut ClientPin, + sub_command_params: CredentialManagementSubCommandParameters, +) -> Result<(), Ctap2StatusCode> { + let credential_id = sub_command_params + .credential_id + .ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)? + .key_id; + let user = sub_command_params + .user + .ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + check_rp_id_permissions(env, client_pin, &credential_id)?; + storage::update_credential(env, &credential_id, user) +} + +/// Processes the CredentialManagement command and all its subcommands. +pub fn process_credential_management( + env: &mut impl Env, + stateful_command_permission: &mut StatefulPermission, + client_pin: &mut ClientPin, + cred_management_params: AuthenticatorCredentialManagementParameters, + channel: Channel, + now: Instant, +) -> Result { + let AuthenticatorCredentialManagementParameters { + sub_command, + sub_command_params, + pin_uv_auth_protocol, + pin_uv_auth_param, + } = cred_management_params; + + match (sub_command, stateful_command_permission.get_command()) { + ( + CredentialManagementSubCommand::EnumerateRpsGetNextRp, + Ok(StatefulCommand::EnumerateRps(_)), + ) + | ( + CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential, + Ok(StatefulCommand::EnumerateCredentials(_)), + ) => (), + (_, _) => { + stateful_command_permission.clear(); + } + } + + match sub_command { + CredentialManagementSubCommand::GetCredsMetadata + | CredentialManagementSubCommand::EnumerateRpsBegin + | CredentialManagementSubCommand::EnumerateCredentialsBegin + | CredentialManagementSubCommand::DeleteCredential + | CredentialManagementSubCommand::UpdateUserInformation => { + let pin_uv_auth_param = + pin_uv_auth_param.ok_or(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED)?; + let pin_uv_auth_protocol = + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + let mut management_data = vec![sub_command as u8]; + if let Some(sub_command_params) = sub_command_params.clone() { + super::cbor_write(sub_command_params.into(), &mut management_data)?; + } + client_pin.verify_pin_uv_auth_token( + &management_data, + &pin_uv_auth_param, + pin_uv_auth_protocol, + )?; + // The RP ID permission is handled differently per subcommand below. + client_pin.has_permission(PinPermission::CredentialManagement)?; + } + CredentialManagementSubCommand::EnumerateRpsGetNextRp + | CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential => {} + } + + let response = match sub_command { + CredentialManagementSubCommand::GetCredsMetadata => { + client_pin.has_no_rp_id_permission()?; + Some(process_get_creds_metadata(env)?) + } + CredentialManagementSubCommand::EnumerateRpsBegin => { + client_pin.has_no_rp_id_permission()?; + Some(process_enumerate_rps_begin( + env, + stateful_command_permission, + channel, + now, + )?) + } + CredentialManagementSubCommand::EnumerateRpsGetNextRp => Some( + process_enumerate_rps_get_next_rp(env, stateful_command_permission)?, + ), + CredentialManagementSubCommand::EnumerateCredentialsBegin => { + Some(process_enumerate_credentials_begin( + env, + stateful_command_permission, + client_pin, + sub_command_params.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, + channel, + now, + )?) + } + CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential => Some( + process_enumerate_credentials_get_next_credential(env, stateful_command_permission)?, + ), + CredentialManagementSubCommand::DeleteCredential => { + process_delete_credential( + env, + client_pin, + sub_command_params.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, + )?; + None + } + CredentialManagementSubCommand::UpdateUserInformation => { + process_update_user_information( + env, + client_pin, + sub_command_params.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, + )?; + None + } + }; + Ok(ResponseData::AuthenticatorCredentialManagement(response)) +} + +#[cfg(test)] +mod test { + use super::super::crypto_wrapper::PrivateKey; + use super::super::data_formats::{PinUvAuthProtocol, PublicKeyCredentialType}; + use super::super::pin_protocol::authenticate_pin_uv_auth_token; + use super::super::CtapState; + use super::*; + use crate::env::test::TestEnv; + use ctap_crypto::rng256::Rng256; + + const DUMMY_CHANNEL: Channel = Channel::MainHid([0x12, 0x34, 0x56, 0x78]); + + fn create_credential_source(env: &mut TestEnv) -> PublicKeyCredentialSource { + let private_key = PrivateKey::new_ecdsa(env); + PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id: env.rng().gen_uniform_u8x32().to_vec(), + private_key, + rp_id: String::from("example.com"), + user_handle: vec![0x01], + user_display_name: Some("display_name".to_string()), + cred_protect_policy: None, + creation_order: 0, + user_name: Some("name".to_string()), + user_icon: Some("icon".to_string()), + cred_blob: None, + large_blob_key: None, + } + } + + fn test_helper_process_get_creds_metadata(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + let credential_source = create_credential_source(&mut env); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let management_data = vec![CredentialManagementSubCommand::GetCredsMetadata as u8]; + let pin_uv_auth_param = authenticate_pin_uv_auth_token( + &pin_uv_auth_token, + &management_data, + pin_uv_auth_protocol, + ); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::GetCredsMetadata, + sub_command_params: None, + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), + pin_uv_auth_param: Some(pin_uv_auth_param.clone()), + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let initial_capacity = match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert_eq!(response.existing_resident_credentials_count, Some(0)); + response + .max_possible_remaining_resident_credentials_count + .unwrap() + } + _ => panic!("Invalid response type"), + }; + + storage::store_credential(&mut env, credential_source).unwrap(); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::GetCredsMetadata, + sub_command_params: None, + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), + pin_uv_auth_param: Some(pin_uv_auth_param), + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert_eq!(response.existing_resident_credentials_count, Some(1)); + assert_eq!( + response.max_possible_remaining_resident_credentials_count, + Some(initial_capacity - 1) + ); + } + _ => panic!("Invalid response type"), + }; + } + + #[test] + fn test_process_get_creds_metadata_v1() { + test_helper_process_get_creds_metadata(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_creds_metadata_v2() { + test_helper_process_get_creds_metadata(PinUvAuthProtocol::V2); + } + + #[test] + fn test_process_enumerate_rps_with_uv() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let credential_source1 = create_credential_source(&mut env); + let mut credential_source2 = create_credential_source(&mut env); + credential_source2.rp_id = "another.example.com".to_string(); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + storage::store_credential(&mut env, credential_source1).unwrap(); + storage::store_credential(&mut env, credential_source2).unwrap(); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0x1A, 0xA4, 0x96, 0xDA, 0x62, 0x80, 0x28, 0x13, 0xEB, 0x32, 0xB9, 0xF1, 0xD2, 0xA9, + 0xD0, 0xD1, + ]); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsBegin, + sub_command_params: None, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let first_rp_id = match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert_eq!(response.total_rps, Some(2)); + let rp_id = response.rp.unwrap().rp_id; + let rp_id_hash = Sha256::hash(rp_id.as_bytes()); + assert_eq!(rp_id_hash, response.rp_id_hash.unwrap().as_slice()); + rp_id + } + _ => panic!("Invalid response type"), + }; + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsGetNextRp, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let second_rp_id = match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert_eq!(response.total_rps, None); + let rp_id = response.rp.unwrap().rp_id; + let rp_id_hash = Sha256::hash(rp_id.as_bytes()); + assert_eq!(rp_id_hash, response.rp_id_hash.unwrap().as_slice()); + rp_id + } + _ => panic!("Invalid response type"), + }; + + assert!(first_rp_id != second_rp_id); + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsGetNextRp, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + } + + #[test] + fn test_process_enumerate_rps_completeness() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let credential_source = create_credential_source(&mut env); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + const NUM_CREDENTIALS: usize = 20; + for i in 0..NUM_CREDENTIALS { + let mut credential = credential_source.clone(); + credential.rp_id = i.to_string(); + storage::store_credential(&mut env, credential).unwrap(); + } + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0x1A, 0xA4, 0x96, 0xDA, 0x62, 0x80, 0x28, 0x13, 0xEB, 0x32, 0xB9, 0xF1, 0xD2, 0xA9, + 0xD0, 0xD1, + ]); + + let mut rp_set = BTreeSet::new(); + // This mut is just to make the test code shorter. + // The command is different on the first loop iteration. + let mut cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsBegin, + sub_command_params: None, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + + for _ in 0..NUM_CREDENTIALS { + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + if rp_set.is_empty() { + assert_eq!(response.total_rps, Some(NUM_CREDENTIALS as u64)); + } else { + assert_eq!(response.total_rps, None); + } + let rp_id = response.rp.unwrap().rp_id; + let rp_id_hash = Sha256::hash(rp_id.as_bytes()); + assert_eq!(rp_id_hash, response.rp_id_hash.unwrap().as_slice()); + assert!(!rp_set.contains(&rp_id)); + rp_set.insert(rp_id); + } + _ => panic!("Invalid response type"), + }; + cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsGetNextRp, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + } + + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + } + + #[test] + fn test_process_enumerate_credentials_with_uv() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let credential_source1 = create_credential_source(&mut env); + let mut credential_source2 = create_credential_source(&mut env); + credential_source2.user_handle = vec![0x02]; + credential_source2.user_name = Some("user2".to_string()); + credential_source2.user_display_name = Some("User Two".to_string()); + credential_source2.user_icon = Some("icon2".to_string()); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + storage::store_credential(&mut env, credential_source1).unwrap(); + storage::store_credential(&mut env, credential_source2).unwrap(); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0xF8, 0xB0, 0x3C, 0xC1, 0xD5, 0x58, 0x9C, 0xB7, 0x4D, 0x42, 0xA1, 0x64, 0x14, 0x28, + 0x2B, 0x68, + ]); + + let sub_command_params = CredentialManagementSubCommandParameters { + rp_id_hash: Some(Sha256::hash(b"example.com").to_vec()), + credential_id: None, + user: None, + }; + // RP ID hash: + // A379A6F6EEAFB9A55E378C118034E2751E682FAB9F2D30AB13D2125586CE1947 + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateCredentialsBegin, + sub_command_params: Some(sub_command_params), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let first_credential_id = match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert!(response.user.is_some()); + assert!(response.public_key.is_some()); + assert_eq!(response.total_credentials, Some(2)); + response.credential_id.unwrap().key_id + } + _ => panic!("Invalid response type"), + }; + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let second_credential_id = match cred_management_response.unwrap() { + ResponseData::AuthenticatorCredentialManagement(Some(response)) => { + assert!(response.user.is_some()); + assert!(response.public_key.is_some()); + assert_eq!(response.total_credentials, None); + response.credential_id.unwrap().key_id + } + _ => panic!("Invalid response type"), + }; + + assert!(first_credential_id != second_credential_id); + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + } + + #[test] + fn test_process_delete_credential() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut credential_source = create_credential_source(&mut env); + credential_source.credential_id = vec![0x1D; 32]; + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + storage::store_credential(&mut env, credential_source).unwrap(); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0xBD, 0xE3, 0xEF, 0x8A, 0x77, 0x01, 0xB1, 0x69, 0x19, 0xE6, 0x62, 0xB9, 0x9B, 0x89, + 0x9C, 0x64, + ]); + + let credential_id = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: vec![0x1D; 32], + transports: None, // You can set USB as a hint here. + }; + let sub_command_params = CredentialManagementSubCommandParameters { + rp_id_hash: None, + credential_id: Some(credential_id), + user: None, + }; + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::DeleteCredential, + sub_command_params: Some(sub_command_params.clone()), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param: pin_uv_auth_param.clone(), + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Ok(ResponseData::AuthenticatorCredentialManagement(None)) + ); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::DeleteCredential, + sub_command_params: Some(sub_command_params), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Err(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS) + ); + } + + #[test] + fn test_process_update_user_information() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut credential_source = create_credential_source(&mut env); + credential_source.credential_id = vec![0x1D; 32]; + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + storage::store_credential(&mut env, credential_source).unwrap(); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0xA5, 0x55, 0x8F, 0x03, 0xC3, 0xD3, 0x73, 0x1C, 0x07, 0xDA, 0x1F, 0x8C, 0xC7, 0xBD, + 0x9D, 0xB7, + ]); + + let credential_id = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: vec![0x1D; 32], + transports: None, // You can set USB as a hint here. + }; + let new_user = PublicKeyCredentialUserEntity { + user_id: vec![0xFF], + user_name: Some("new_name".to_string()), + user_display_name: Some("new_display_name".to_string()), + user_icon: Some("new_icon".to_string()), + }; + let sub_command_params = CredentialManagementSubCommandParameters { + rp_id_hash: None, + credential_id: Some(credential_id), + user: Some(new_user), + }; + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::UpdateUserInformation, + sub_command_params: Some(sub_command_params), + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Ok(ResponseData::AuthenticatorCredentialManagement(None)) + ); + + let updated_credential = storage::find_credential(&mut env, "example.com", &[0x1D; 32]) + .unwrap() + .unwrap(); + assert_eq!(updated_credential.user_handle, vec![0x01]); + assert_eq!(&updated_credential.user_name.unwrap(), "new_name"); + assert_eq!( + &updated_credential.user_display_name.unwrap(), + "new_display_name" + ); + assert_eq!(&updated_credential.user_icon.unwrap(), "new_icon"); + } + + #[test] + fn test_process_credential_management_invalid_pin_uv_auth_param() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::GetCredsMetadata, + sub_command_params: None, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param: Some(vec![0u8; 16]), + }; + let cred_management_response = process_credential_management( + &mut env, + &mut ctap_state.stateful_command_permission, + &mut ctap_state.client_pin, + cred_management_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + cred_management_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } +} diff --git a/apps/vault/src/ctap/crypto_wrapper.rs b/apps/vault/src/ctap/crypto_wrapper.rs new file mode 100644 index 000000000..6db49d8f0 --- /dev/null +++ b/apps/vault/src/ctap/crypto_wrapper.rs @@ -0,0 +1,429 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::api::key_store::KeyStore; +use crate::ctap::data_formats::{extract_array, extract_byte_string, CoseKey, SignatureAlgorithm}; +use crate::ctap::status_code::Ctap2StatusCode; +use crate::env::Env; +use std::vec; +use std::vec::Vec; +use core::convert::TryFrom; +use ctap_crypto::cbc::{cbc_decrypt, cbc_encrypt}; +use ctap_crypto::ecdsa; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::rng256::Rng256; +use cbor as cbor; +use cbor::{cbor_array, cbor_bytes, cbor_int}; +use arrayref::array_ref; + +/// Wraps the AES256-CBC encryption to match what we need in CTAP. +pub fn aes256_cbc_encrypt( + rng: &mut dyn Rng256, + aes_enc_key: &ctap_crypto::aes256::EncryptionKey, + plaintext: &[u8], + embeds_iv: bool, +) -> Result, Ctap2StatusCode> { + if plaintext.len() % 16 != 0 { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + // The extra 1 capacity is because encrypt_key_handle adds a version number. + let mut ciphertext = Vec::with_capacity(plaintext.len() + 16 * embeds_iv as usize + 1); + let iv = if embeds_iv { + let random_bytes = rng.gen_uniform_u8x32(); + ciphertext.extend_from_slice(&random_bytes[..16]); + *array_ref!(ciphertext, 0, 16) + } else { + [0u8; 16] + }; + let start = ciphertext.len(); + ciphertext.extend_from_slice(plaintext); + cbc_encrypt(aes_enc_key, iv, &mut ciphertext[start..]); + Ok(ciphertext) +} + +/// Wraps the AES256-CBC decryption to match what we need in CTAP. +pub fn aes256_cbc_decrypt( + aes_enc_key: &ctap_crypto::aes256::EncryptionKey, + ciphertext: &[u8], + embeds_iv: bool, +) -> Result, Ctap2StatusCode> { + if ciphertext.len() % 16 != 0 || (embeds_iv && ciphertext.is_empty()) { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + let (iv, ciphertext) = if embeds_iv { + let (iv, ciphertext) = ciphertext.split_at(16); + (*array_ref!(iv, 0, 16), ciphertext) + } else { + ([0u8; 16], ciphertext) + }; + let mut plaintext = ciphertext.to_vec(); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(aes_enc_key); + cbc_decrypt(&aes_dec_key, iv, &mut plaintext); + Ok(plaintext) +} + +/// An asymmetric private key that can sign messages. +#[derive(Clone, Debug)] +// We shouldn't compare private keys in prod without constant-time operations. +#[cfg_attr(test, derive(PartialEq, Eq))] +pub enum PrivateKey { + // We store the seed instead of the key since we can't get the seed back from the key. We could + // store both if we believe deriving the key is done more than once and costly. + Ecdsa([u8; 32]), + #[cfg(feature = "ed25519")] + Ed25519(ed25519_compact::SecretKey), +} + +impl PrivateKey { + /// Creates a new private key for the given algorithm. + /// + /// # Panics + /// + /// Panics if the algorithm is [`SignatureAlgorithm::Unknown`]. + pub fn new(env: &mut impl Env, alg: SignatureAlgorithm) -> Self { + match alg { + SignatureAlgorithm::Es256 => { + PrivateKey::Ecdsa(env.key_store().generate_ecdsa_seed().unwrap()) + } + #[cfg(feature = "ed25519")] + SignatureAlgorithm::Eddsa => { + let bytes = env.rng().gen_uniform_u8x32(); + Self::new_ed25519_from_bytes(&bytes).unwrap() + } + SignatureAlgorithm::Unknown => unreachable!(), + } + } + + /// Creates a new ecdsa private key. + pub fn new_ecdsa(env: &mut impl Env) -> PrivateKey { + Self::new(env, SignatureAlgorithm::Es256) + } + + /// Helper function that creates a private key of type ECDSA. + /// + /// This function is public for legacy credential source parsing only. + pub fn new_ecdsa_from_bytes(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + Some(PrivateKey::Ecdsa(*array_ref!(bytes, 0, 32))) + } + + #[cfg(feature = "ed25519")] + pub fn new_ed25519_from_bytes(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + let seed = ed25519_compact::Seed::from_slice(bytes).unwrap(); + Some(Self::Ed25519(ed25519_compact::KeyPair::from_seed(seed).sk)) + } + + /// Returns the ECDSA private key. + pub fn ecdsa_key(&self, env: &mut impl Env) -> Result { + match self { + PrivateKey::Ecdsa(seed) => ecdsa_key_from_seed(env, seed), + #[allow(unreachable_patterns)] + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } + } + + /// Returns the corresponding public key. + pub fn get_pub_key(&self, env: &mut impl Env) -> Result { + Ok(match self { + PrivateKey::Ecdsa(ecdsa_seed) => { + CoseKey::from(ecdsa_key_from_seed(env, ecdsa_seed)?.genpk()) + } + #[cfg(feature = "ed25519")] + PrivateKey::Ed25519(ed25519_key) => CoseKey::from(ed25519_key.public_key()), + }) + } + + /// Returns the encoded signature for a given message. + pub fn sign_and_encode( + &self, + env: &mut impl Env, + message: &[u8], + ) -> Result, Ctap2StatusCode> { + Ok(match self { + PrivateKey::Ecdsa(ecdsa_seed) => ecdsa_key_from_seed(env, ecdsa_seed)? + .sign_rfc6979::(message) + .to_asn1_der(), + #[cfg(feature = "ed25519")] + PrivateKey::Ed25519(ed25519_key) => ed25519_key.sign(message, None).to_vec(), + }) + } + + /// The associated COSE signature algorithm identifier. + pub fn signature_algorithm(&self) -> SignatureAlgorithm { + match self { + PrivateKey::Ecdsa(_) => SignatureAlgorithm::Es256, + #[cfg(feature = "ed25519")] + PrivateKey::Ed25519(_) => SignatureAlgorithm::Eddsa, + } + } + + /// Writes the key bytes. + pub fn to_bytes(&self) -> Vec { + match self { + PrivateKey::Ecdsa(ecdsa_seed) => ecdsa_seed.to_vec(), + #[cfg(feature = "ed25519")] + PrivateKey::Ed25519(ed25519_key) => ed25519_key.seed().to_vec(), + } + } +} + +fn ecdsa_key_from_seed( + env: &mut impl Env, + seed: &[u8; 32], +) -> Result { + let ecdsa_bytes = env.key_store().derive_ecdsa(seed)?; + Ok(ecdsa::SecKey::from_bytes(&ecdsa_bytes).unwrap()) +} + +impl From<&PrivateKey> for cbor::Value { + fn from(private_key: &PrivateKey) -> Self { + cbor_array![ + cbor_int!(private_key.signature_algorithm() as i64), + cbor_bytes!(private_key.to_bytes()), + ] + } +} + +impl TryFrom for PrivateKey { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + let mut array = extract_array(cbor_value)?; + if array.len() != 2 { + return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR); + } + let key_bytes = extract_byte_string(array.pop().unwrap())?; + match SignatureAlgorithm::try_from(array.pop().unwrap())? { + SignatureAlgorithm::Es256 => PrivateKey::new_ecdsa_from_bytes(&key_bytes) + .ok_or(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR), + #[cfg(feature = "ed25519")] + SignatureAlgorithm::Eddsa => PrivateKey::new_ed25519_from_bytes(&key_bytes) + .ok_or(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR), + _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR), + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::env::test::TestEnv; + + #[test] + fn test_encrypt_decrypt_with_iv() { + let mut env = TestEnv::new(); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&[0xC2; 32]); + let plaintext = vec![0xAA; 64]; + let ciphertext = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, true).unwrap(); + let decrypted = aes256_cbc_decrypt(&aes_enc_key, &ciphertext, true).unwrap(); + assert_eq!(decrypted, plaintext); + } + + #[test] + fn test_encrypt_decrypt_without_iv() { + let mut env = TestEnv::new(); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&[0xC2; 32]); + let plaintext = vec![0xAA; 64]; + let ciphertext = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, false).unwrap(); + let decrypted = aes256_cbc_decrypt(&aes_enc_key, &ciphertext, false).unwrap(); + assert_eq!(decrypted, plaintext); + } + + #[test] + fn test_correct_iv_usage() { + let mut env = TestEnv::new(); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&[0xC2; 32]); + let plaintext = vec![0xAA; 64]; + let mut ciphertext_no_iv = + aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, false).unwrap(); + let mut ciphertext_with_iv = vec![0u8; 16]; + ciphertext_with_iv.append(&mut ciphertext_no_iv); + let decrypted = aes256_cbc_decrypt(&aes_enc_key, &ciphertext_with_iv, true).unwrap(); + assert_eq!(decrypted, plaintext); + } + + #[test] + fn test_iv_manipulation_property() { + let mut env = TestEnv::new(); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&[0xC2; 32]); + let plaintext = vec![0xAA; 64]; + let mut ciphertext = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, true).unwrap(); + let mut expected_plaintext = plaintext; + for i in 0..16 { + ciphertext[i] ^= 0xBB; + expected_plaintext[i] ^= 0xBB; + } + let decrypted = aes256_cbc_decrypt(&aes_enc_key, &ciphertext, true).unwrap(); + assert_eq!(decrypted, expected_plaintext); + } + + #[test] + fn test_chaining() { + let mut env = TestEnv::new(); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&[0xC2; 32]); + let plaintext = vec![0xAA; 64]; + let ciphertext1 = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, true).unwrap(); + let ciphertext2 = aes256_cbc_encrypt(env.rng(), &aes_enc_key, &plaintext, true).unwrap(); + assert_eq!(ciphertext1.len(), 80); + assert_eq!(ciphertext2.len(), 80); + // The ciphertext should mutate in all blocks with a different IV. + let block_iter1 = ciphertext1.chunks_exact(16); + let block_iter2 = ciphertext2.chunks_exact(16); + for (block1, block2) in block_iter1.zip(block_iter2) { + assert_ne!(block1, block2); + } + } + + #[test] + fn test_new_ecdsa_from_bytes() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let key_bytes = private_key.to_bytes(); + assert_eq!( + PrivateKey::new_ecdsa_from_bytes(&key_bytes), + Some(private_key) + ); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_new_ed25519_from_bytes() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, SignatureAlgorithm::Eddsa); + let key_bytes = private_key.to_bytes(); + assert_eq!( + PrivateKey::new_ed25519_from_bytes(&key_bytes), + Some(private_key) + ); + } + + #[test] + fn test_new_ecdsa_from_bytes_wrong_length() { + assert_eq!(PrivateKey::new_ecdsa_from_bytes(&[0x55; 16]), None); + assert_eq!(PrivateKey::new_ecdsa_from_bytes(&[0x55; 31]), None); + assert_eq!(PrivateKey::new_ecdsa_from_bytes(&[0x55; 33]), None); + assert_eq!(PrivateKey::new_ecdsa_from_bytes(&[0x55; 64]), None); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_new_ed25519_from_bytes_wrong_length() { + assert_eq!(PrivateKey::new_ed25519_from_bytes(&[0x55; 16]), None); + assert_eq!(PrivateKey::new_ed25519_from_bytes(&[0x55; 31]), None); + assert_eq!(PrivateKey::new_ed25519_from_bytes(&[0x55; 33]), None); + assert_eq!(PrivateKey::new_ed25519_from_bytes(&[0x55; 64]), None); + } + + #[test] + fn test_private_key_get_pub_key() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let ecdsa_key = private_key.ecdsa_key(&mut env).unwrap(); + let public_key = ecdsa_key.genpk(); + assert_eq!( + private_key.get_pub_key(&mut env), + Ok(CoseKey::from(public_key)) + ); + } + + #[test] + fn test_private_key_sign_and_encode() { + let mut env = TestEnv::new(); + let message = [0x5A; 32]; + let private_key = PrivateKey::new_ecdsa(&mut env); + let ecdsa_key = private_key.ecdsa_key(&mut env).unwrap(); + let signature = ecdsa_key.sign_rfc6979::(&message).to_asn1_der(); + assert_eq!( + private_key.sign_and_encode(&mut env, &message), + Ok(signature) + ); + } + + fn test_private_key_signature_algorithm(signature_algorithm: SignatureAlgorithm) { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, signature_algorithm); + assert_eq!(private_key.signature_algorithm(), signature_algorithm); + } + + #[test] + fn test_ecdsa_private_key_signature_algorithm() { + test_private_key_signature_algorithm(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_ed25519_private_key_signature_algorithm() { + test_private_key_signature_algorithm(SignatureAlgorithm::Eddsa); + } + + fn test_private_key_from_to_cbor(signature_algorithm: SignatureAlgorithm) { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new(&mut env, signature_algorithm); + let cbor = cbor::Value::from(&private_key); + assert_eq!(PrivateKey::try_from(cbor), Ok(private_key),); + } + + #[test] + fn test_ecdsa_private_key_from_to_cbor() { + test_private_key_from_to_cbor(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_ed25519_private_key_from_to_cbor() { + test_private_key_from_to_cbor(SignatureAlgorithm::Eddsa); + } + + fn test_private_key_from_bad_cbor(signature_algorithm: SignatureAlgorithm) { + let cbor = cbor_array![ + cbor_int!(signature_algorithm as i64), + cbor_bytes!(vec![0x88; 32]), + // The array is too long. + cbor_int!(0), + ]; + assert_eq!( + PrivateKey::try_from(cbor), + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR), + ); + } + + #[test] + fn test_ecdsa_private_key_from_bad_cbor() { + test_private_key_from_bad_cbor(SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_ed25519_private_key_from_bad_cbor() { + test_private_key_from_bad_cbor(SignatureAlgorithm::Eddsa); + } + + #[test] + fn test_private_key_from_bad_cbor_unsupported_algo() { + let cbor = cbor_array![ + // This algorithms doesn't exist. + cbor_int!(-1), + cbor_bytes!(vec![0x88; 32]), + ]; + assert_eq!( + PrivateKey::try_from(cbor), + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR), + ); + } +} diff --git a/apps/vault/src/ctap/ctap1.rs b/apps/vault/src/ctap/ctap1.rs index 40b5fafad..6d8365e01 100644 --- a/apps/vault/src/ctap/ctap1.rs +++ b/apps/vault/src/ctap/ctap1.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,25 +12,27 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::time::Instant; use super::apdu::{Apdu, ApduStatusCode}; -use super::hid::ChannelID; -use super::status_code::Ctap2StatusCode; +use super::credential_id::{decrypt_credential_id, encrypt_to_credential_id}; +use super::crypto_wrapper::PrivateKey; use super::CtapState; +use crate::api::attestation_store::{self, Attestation, AttestationStore}; +use crate::env::Env; use std::vec::Vec; use arrayref::array_ref; use core::convert::TryFrom; -use ctap_crypto::rng256::Rng256; -use crate::shims::ClockValue; -use locales::t; // For now, they're the same thing with apdu.rs containing the authoritative definition pub type Ctap1StatusCode = ApduStatusCode; +#[cfg(feature="xous")] +use locales::t; + // The specification referenced in this file is at: // https://fidoalliance.org/specs/fido-u2f-v1.2-ps-20170411/fido-u2f-raw-message-formats-v1.2-ps-20170411.pdf -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone))] -#[derive(PartialEq, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum Ctap1Flags { CheckOnly = 0x07, EnforceUpAndSign = 0x03, @@ -56,16 +58,12 @@ impl From for u8 { } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] -// TODO: remove #allow when https://github.com/rust-lang/rust/issues/64362 is fixed +#[derive(Debug, PartialEq, Eq)] enum U2fCommand { - #[allow(dead_code)] Register { challenge: [u8; 32], application: [u8; 32], }, - #[allow(dead_code)] Authenticate { challenge: [u8; 32], application: [u8; 32], @@ -73,7 +71,6 @@ enum U2fCommand { flags: Ctap1Flags, }, Version, - #[allow(dead_code)] VendorSpecific { payload: Vec, }, @@ -85,14 +82,12 @@ impl TryFrom<&[u8]> for U2fCommand { fn try_from(message: &[u8]) -> Result { let apdu: Apdu = match Apdu::try_from(message) { Ok(apdu) => apdu, - Err(apdu_status_code) => { - return Err(Ctap1StatusCode::try_from(apdu_status_code).unwrap()) - } + Err(apdu_status_code) => return Err(apdu_status_code), }; let lc = apdu.lc as usize; - // ISO7816 Apdu Header format. Each cell is 1 byte. Note that the CTAP flavor always + // ISO7816 APDU Header format. Each cell is 1 byte. Note that the CTAP flavor always // encodes the length on 3 bytes and doesn't use the field "Le" (Length Expected). // We keep the 2 byte of "Le" for the packet length in mind, but always ignore its value. // Lc is using big-endian encoding @@ -182,29 +177,35 @@ impl Ctap1Command { const VENDOR_SPECIFIC_FIRST: u8 = 0x40; const VENDOR_SPECIFIC_LAST: u8 = 0xBF; - pub fn process_command( + pub fn process_command( + env: &mut impl Env, message: &[u8], - ctap_state: &mut CtapState, - clock_value: ClockValue, - ) -> Result, Ctap1StatusCode> - where - R: Rng256, - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { + ctap_state: &mut CtapState, + _clock_value: Instant, + ) -> Result, Ctap1StatusCode> { + if !ctap_state + .allows_ctap1(env) + .map_err(|_| Ctap1StatusCode::SW_INTERNAL_EXCEPTION)? + { + return Err(Ctap1StatusCode::SW_COMMAND_NOT_ALLOWED); + } let command = U2fCommand::try_from(message)?; match command { U2fCommand::Register { challenge, application, } => { - log::debug!("REGISTER"); + #[cfg(not(feature="xous"))] + if !ctap_state.u2f_up_state.consume_up(clock_value) { + return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); + } + #[cfg(feature="xous")] let regstr = String::from(t!("vault.u2f.register", xous::LANG)); - if !ctap_state.u2f_up_state.consume_up(clock_value, regstr, application) { + if !ctap_state.u2f_up_state.consume_up(env, regstr, application) { log::debug!("still waiting..."); return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); } - log::debug!("GOT IT!..."); - Ctap1Command::process_register(challenge, application, ctap_state) + Ctap1Command::process_register(env, challenge, application) } U2fCommand::Authenticate { @@ -213,15 +214,23 @@ impl Ctap1Command { key_handle, flags, } => { - log::debug!("AUTHENTICATE"); + // The order is important due to side effects of checking user presence. + #[cfg(not(feature="xous"))] + if flags == Ctap1Flags::EnforceUpAndSign + && !ctap_state.u2f_up_state.consume_up(clock_value) + { + return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); + } + #[cfg(feature="xous")] let authstr = String::from(t!("vault.u2f.authenticate", xous::LANG)); // The order is important due to side effects of checking user presence. if flags == Ctap1Flags::EnforceUpAndSign - && !ctap_state.u2f_up_state.consume_up(clock_value, authstr, application) + && !ctap_state.u2f_up_state.consume_up(env, authstr, application) { return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); } Ctap1Command::process_authenticate( + env, challenge, application, key_handle, @@ -231,16 +240,10 @@ impl Ctap1Command { } // U2F raw message format specification (version 20170411) section 6.3 - U2fCommand::Version => { - log::debug!("VERSION"); - Ok(Vec::::from(super::U2F_VERSION_STRING)) - }, + U2fCommand::Version => Ok(Vec::::from(super::U2F_VERSION_STRING)), // TODO: should we return an error instead such as SW_INS_NOT_SUPPORTED? - U2fCommand::VendorSpecific { .. } => { - log::debug!("VENDOR"); - Err(Ctap1StatusCode::SW_SUCCESS) - }, + U2fCommand::VendorSpecific { .. } => Err(Ctap1StatusCode::SW_SUCCESS), } } @@ -255,35 +258,29 @@ impl Ctap1Command { // +------+-------------------+-----------------+------------+--------------------+ // + 0x00 | application (32B) | challenge (32B) | key handle | User pub key (65B) | // +------+-------------------+-----------------+------------+--------------------+ - fn process_register( + fn process_register( + env: &mut impl Env, challenge: [u8; 32], application: [u8; 32], - ctap_state: &mut CtapState, - ) -> Result, Ctap1StatusCode> - where - R: Rng256, - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { - let sk = ctap_crypto::ecdsa::SecKey::gensk(ctap_state.rng); + ) -> Result, Ctap1StatusCode> { + let private_key = PrivateKey::new_ecdsa(env); + let sk = private_key + .ecdsa_key(env) + .map_err(|_| Ctap1StatusCode::SW_INTERNAL_EXCEPTION)?; let pk = sk.genpk(); - let key_handle = ctap_state - .encrypt_key_handle(sk, &application) + let key_handle = encrypt_to_credential_id(env, &private_key, &application, None, None) .map_err(|_| Ctap1StatusCode::SW_INTERNAL_EXCEPTION)?; if key_handle.len() > 0xFF { - log::info!("key_handle.len() invalid"); // This is just being defensive with unreachable code. return Err(Ctap1StatusCode::SW_INTERNAL_EXCEPTION); } - let certificate = ctap_state - .persistent_store - .attestation_certificate() - .map_err(|_| Ctap1StatusCode::SW_MEMERR)? - .ok_or(Ctap1StatusCode::SW_INTERNAL_EXCEPTION)?; - let private_key = ctap_state - .persistent_store - .attestation_private_key() - .map_err(|_| Ctap1StatusCode::SW_INTERNAL_EXCEPTION)? + let Attestation { + private_key, + certificate, + } = env + .attestation_store() + .get(&attestation_store::Id::Batch)? .ok_or(Ctap1StatusCode::SW_INTERNAL_EXCEPTION)?; let mut response = Vec::with_capacity(105 + key_handle.len() + certificate.len()); @@ -306,7 +303,6 @@ impl Ctap1Command { let signature = attestation_key.sign_rfc6979::(&signature_data); response.extend(signature.to_asn1_der()); - log::trace!("process_register response: {:x?}", &response[..32]); Ok(response) } @@ -324,34 +320,36 @@ impl Ctap1Command { // +-------------------+---------+--------------+-----------------+ // + application (32B) | UP (1B) | Counter (4B) | challenge (32B) | // +-------------------+---------+--------------+-----------------+ - fn process_authenticate( + fn process_authenticate( + env: &mut impl Env, challenge: [u8; 32], application: [u8; 32], key_handle: Vec, flags: Ctap1Flags, - ctap_state: &mut CtapState, - ) -> Result, Ctap1StatusCode> - where - R: Rng256, - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { - let credential_source = ctap_state - .decrypt_credential_source(key_handle, &application) + ctap_state: &mut CtapState, + ) -> Result, Ctap1StatusCode> { + let credential_source = decrypt_credential_id(env, key_handle, &application) .map_err(|_| Ctap1StatusCode::SW_WRONG_DATA)?; if let Some(credential_source) = credential_source { + let ecdsa_key = credential_source + .private_key + .ecdsa_key(env) + .map_err(|_| Ctap1StatusCode::SW_WRONG_DATA)?; if flags == Ctap1Flags::CheckOnly { return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); } ctap_state - .increment_global_signature_counter() + .increment_global_signature_counter(env) .map_err(|_| Ctap1StatusCode::SW_WRONG_DATA)?; let mut signature_data = ctap_state - .generate_auth_data(&application, Ctap1Command::USER_PRESENCE_INDICATOR_BYTE) + .generate_auth_data( + env, + &application, + Ctap1Command::USER_PRESENCE_INDICATOR_BYTE, + ) .map_err(|_| Ctap1StatusCode::SW_WRONG_DATA)?; signature_data.extend(&challenge); - let signature = credential_source - .private_key - .sign_rfc6979::(&signature_data); + let signature = ecdsa_key.sign_rfc6979::(&signature_data); let mut response = signature_data[application.len()..application.len() + 5].to_vec(); response.extend(signature.to_asn1_der()); @@ -364,18 +362,15 @@ impl Ctap1Command { #[cfg(test)] mod test { - use super::super::{key_material, CREDENTIAL_ID_SIZE, USE_SIGNATURE_COUNTER}; + use super::super::credential_id::CBOR_CREDENTIAL_ID_SIZE; + use super::super::data_formats::SignatureAlgorithm; use super::*; - use ctap_crypto::rng256::ThreadRng256; + use crate::api::customization::Customization; + use crate::clock::TEST_CLOCK_FREQUENCY_HZ; + use crate::ctap::storage; + use crate::env::test::TestEnv; use ctap_crypto::Hash256; - const CLOCK_FREQUENCY_HZ: usize = 32768; - const START_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); - const TIMEOUT_CLOCK_VALUE: ClockValue = ClockValue::new( - (30001 * CLOCK_FREQUENCY_HZ as i64) / 1000, - CLOCK_FREQUENCY_HZ, - ); - fn create_register_message(application: &[u8; 32]) -> Vec { let mut message = vec![ Ctap1Command::CTAP1_CLA, @@ -395,7 +390,7 @@ mod test { fn create_authenticate_message( application: &[u8; 32], flags: Ctap1Flags, - key_handle: &Vec, + key_handle: &[u8], ) -> Vec { let mut message = vec![ Ctap1Command::CTAP1_CLA, @@ -403,140 +398,166 @@ mod test { flags.into(), 0x00, 0x00, - 0x00, - 65 + CREDENTIAL_ID_SIZE as u8, ]; + message.extend(&(65 + CBOR_CREDENTIAL_ID_SIZE as u16).to_be_bytes()); let challenge = [0x0C; 32]; message.extend(&challenge); message.extend(application); - message.push(CREDENTIAL_ID_SIZE as u8); + message.push(CBOR_CREDENTIAL_ID_SIZE as u8); message.extend(key_handle); message } #[test] - fn test_process_register() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + #[cfg(not(feature="xous"))] + fn test_process_allowed() { + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + storage::toggle_always_uv(&mut env).unwrap(); let application = [0x0A; 32]; let message = create_register_message(&application); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); + assert_eq!(response, Err(Ctap1StatusCode::SW_COMMAND_NOT_ALLOWED)); + } + + #[test] + #[cfg(not(feature="xous"))] + fn test_process_register() { + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let application = [0x0A; 32]; + let message = create_register_message(&application); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); // Certificate and private key are missing assert_eq!(response, Err(Ctap1StatusCode::SW_INTERNAL_EXCEPTION)); - let fake_key = [0x41u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH]; - assert!(ctap_state - .persistent_store - .set_attestation_private_key(&fake_key) - .is_ok()); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); - // Certificate is still missing - assert_eq!(response, Err(Ctap1StatusCode::SW_INTERNAL_EXCEPTION)); - - let fake_cert = [0x99u8; 100]; // Arbitrary length - assert!(ctap_state - .persistent_store - .set_attestation_certificate(&fake_cert[..]) - .is_ok()); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); + let attestation = Attestation { + private_key: [0x41; 32], + certificate: vec![0x99; 100], + }; + env.attestation_store() + .set(&attestation_store::Id::Batch, Some(&attestation)) + .unwrap(); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); let response = - Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE).unwrap(); + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)) + .unwrap(); assert_eq!(response[0], Ctap1Command::LEGACY_BYTE); - assert_eq!(response[66], CREDENTIAL_ID_SIZE as u8); - assert!(ctap_state - .decrypt_credential_source(response[67..67 + CREDENTIAL_ID_SIZE].to_vec(), &application) - .unwrap() - .is_some()); - const CERT_START: usize = 67 + CREDENTIAL_ID_SIZE; + assert_eq!(response[66], CBOR_CREDENTIAL_ID_SIZE as u8); + assert!(decrypt_credential_id( + &mut env, + response[67..67 + CBOR_CREDENTIAL_ID_SIZE].to_vec(), + &application, + ) + .unwrap() + .is_some()); + const CERT_START: usize = 67 + CBOR_CREDENTIAL_ID_SIZE; assert_eq!( - &response[CERT_START..CERT_START + fake_cert.len()], - &fake_cert[..] + &response[CERT_START..][..attestation.certificate.len()], + &attestation.certificate ); } #[test] fn test_process_register_bad_message() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let application = [0x0A; 32]; let message = create_register_message(&application); let response = Ctap1Command::process_command( + &mut env, &message[..message.len() - 1], &mut ctap_state, - START_CLOCK_VALUE, + Instant::new(0), ); assert_eq!(response, Err(Ctap1StatusCode::SW_WRONG_LENGTH)); } #[test] + #[cfg(not(feature="xous"))] fn test_process_register_without_up() { let application = [0x0A; 32]; let message = create_register_message(&application); - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); + let timeout_clock_value: Instant = + Instant::new((30001 * TEST_CLOCK_FREQUENCY_HZ as u64) / 1000); let response = - Ctap1Command::process_command(&message, &mut ctap_state, TIMEOUT_CLOCK_VALUE); + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, timeout_clock_value); assert_eq!(response, Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED)); } #[test] fn test_process_authenticate_check_only() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED)); } #[test] fn test_process_authenticate_check_only_wrong_rp() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let application = [0x55; 32]; let message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_WRONG_DATA)); } #[test] fn test_process_authenticate_check_only_wrong_length() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let mut message = create_authenticate_message( &application, Ctap1Flags::DontEnforceUpAndSign, @@ -544,78 +565,88 @@ mod test { ); message.push(0x00); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert!(response.is_ok()); message.push(0x00); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert!(response.is_ok()); message.push(0x00); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert!(response.is_ok()); message.push(0x00); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_WRONG_LENGTH)); } #[test] fn test_process_authenticate_check_only_wrong_cla() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); message[0] = 0xEE; - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_CLA_INVALID)); } #[test] fn test_process_authenticate_check_only_wrong_ins() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); message[1] = 0xEE; - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_INS_INVALID)); } #[test] fn test_process_authenticate_check_only_wrong_flags() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); message[2] = 0xEE; - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_WRONG_DATA)); } - fn check_signature_counter(response: &[u8; 4], signature_counter: u32) { - if USE_SIGNATURE_COUNTER { + fn check_signature_counter(env: &mut impl Env, response: &[u8; 4], signature_counter: u32) { + if env.customization().use_signature_counter() { assert_eq!(u32::from_be_bytes(*response), signature_counter); } else { assert_eq!(response, &[0x00, 0x00, 0x00, 0x00]); @@ -623,92 +654,108 @@ mod test { } #[test] + #[cfg(not(feature="xous"))] fn test_process_authenticate_enforce() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let message = create_authenticate_message(&application, Ctap1Flags::EnforceUpAndSign, &key_handle); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); let response = - Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE).unwrap(); + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)) + .unwrap(); assert_eq!(response[0], 0x01); + let global_signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_signature_counter( + &mut env, array_ref!(response, 1, 4), - ctap_state - .persistent_store - .global_signature_counter() - .unwrap(), + global_signature_counter, ); } #[test] fn test_process_authenticate_dont_enforce() { - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let sk = PrivateKey::new(&mut env, SignatureAlgorithm::Es256); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let rp_id = "example.com"; let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); - let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); + let key_handle = encrypt_to_credential_id(&mut env, &sk, &application, None, None).unwrap(); let message = create_authenticate_message( &application, Ctap1Flags::DontEnforceUpAndSign, &key_handle, ); - let response = - Ctap1Command::process_command(&message, &mut ctap_state, TIMEOUT_CLOCK_VALUE).unwrap(); + let response = Ctap1Command::process_command( + &mut env, + &message, + &mut ctap_state, + Instant::new((30001 * TEST_CLOCK_FREQUENCY_HZ as u64) / 1000), + ) + .unwrap(); assert_eq!(response[0], 0x01); + let global_signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_signature_counter( + &mut env, array_ref!(response, 1, 4), - ctap_state - .persistent_store - .global_signature_counter() - .unwrap(), + global_signature_counter, ); } #[test] + #[cfg(not(feature="xous"))] fn test_process_authenticate_bad_key_handle() { let application = [0x0A; 32]; - let key_handle = vec![0x00; CREDENTIAL_ID_SIZE]; + let key_handle = vec![0x00; CBOR_CREDENTIAL_ID_SIZE]; let message = create_authenticate_message(&application, Ctap1Flags::EnforceUpAndSign, &key_handle); - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); - let response = Ctap1Command::process_command(&message, &mut ctap_state, START_CLOCK_VALUE); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); + let response = + Ctap1Command::process_command(&mut env, &message, &mut ctap_state, Instant::new(0)); assert_eq!(response, Err(Ctap1StatusCode::SW_WRONG_DATA)); } #[test] + #[cfg(not(feature="xous"))] fn test_process_authenticate_without_up() { let application = [0x0A; 32]; - let key_handle = vec![0x00; CREDENTIAL_ID_SIZE]; + let key_handle = vec![0x00; CBOR_CREDENTIAL_ID_SIZE]; let message = create_authenticate_message(&application, Ctap1Flags::EnforceUpAndSign, &key_handle); - let mut rng = ThreadRng256 {}; - let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence() + .set(|| panic!("Unexpected user presence check in CTAP1")); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - //ctap_state.u2f_up_state.consume_up(START_CLOCK_VALUE); - //ctap_state.u2f_up_state.grant_up(START_CLOCK_VALUE); - let response = - Ctap1Command::process_command(&message, &mut ctap_state, TIMEOUT_CLOCK_VALUE); + ctap_state.u2f_up_state.consume_up(Instant::new(0)); + ctap_state.u2f_up_state.grant_up(Instant::new(0)); + let response = Ctap1Command::process_command( + &mut env, + &message, + &mut ctap_state, + Instant::new((30001 * TEST_CLOCK_FREQUENCY_HZ as u64) / 1000), + ); assert_eq!(response, Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED)); } } diff --git a/apps/vault/src/ctap/data_formats.rs b/apps/vault/src/ctap/data_formats.rs index eeea4da9b..345ded26c 100644 --- a/apps/vault/src/ctap/data_formats.rs +++ b/apps/vault/src/ctap/data_formats.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,20 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. +use super::crypto_wrapper::PrivateKey; use super::status_code::Ctap2StatusCode; -use std::collections::BTreeMap; use std::string::String; use std::vec::Vec; +#[cfg(feature = "fuzz")] +use arbitrary::Arbitrary; use arrayref::array_ref; -use cbor::{cbor_array_vec, cbor_bytes_lit, cbor_map_options, destructure_cbor_map}; use core::convert::TryFrom; use ctap_crypto::{ecdh, ecdsa}; #[cfg(test)] use enum_iterator::IntoEnumIterator; +use cbor as cbor; +use cbor::{cbor_array_vec, cbor_map, cbor_map_options, destructure_cbor_map}; + +// Used as the identifier for ECDSA in assertion signatures and COSE. +pub const ES256_ALGORITHM: i64 = -7; +#[cfg(feature = "ed25519")] +pub const EDDSA_ALGORITHM: i64 = -8; // https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialrpentity -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct PublicKeyCredentialRpEntity { pub rp_id: String, pub rp_name: Option, @@ -56,9 +64,19 @@ impl TryFrom for PublicKeyCredentialRpEntity { } } +impl From for cbor::Value { + fn from(entity: PublicKeyCredentialRpEntity) -> Self { + cbor_map_options! { + "id" => entity.rp_id, + "icon" => entity.rp_icon, + "name" => entity.rp_name, + } + } +} + // https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialuserentity -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct PublicKeyCredentialUserEntity { pub user_id: Vec, pub user_name: Option, @@ -97,17 +115,16 @@ impl From for cbor::Value { fn from(entity: PublicKeyCredentialUserEntity) -> Self { cbor_map_options! { "id" => entity.user_id, + "icon" => entity.user_icon, "name" => entity.user_name, "displayName" => entity.user_display_name, - "icon" => entity.user_icon, } } } // https://www.w3.org/TR/webauthn/#enumdef-publickeycredentialtype -#[derive(Clone, PartialEq)] -//#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub enum PublicKeyCredentialType { PublicKey, // This is the default for all strings not covered above. @@ -139,9 +156,8 @@ impl TryFrom for PublicKeyCredentialType { } // https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialparameters -#[derive(PartialEq)] -// #[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct PublicKeyCredentialParameter { pub cred_type: PublicKeyCredentialType, pub alg: SignatureAlgorithm, @@ -167,16 +183,16 @@ impl TryFrom for PublicKeyCredentialParameter { impl From for cbor::Value { fn from(cred_param: PublicKeyCredentialParameter) -> Self { cbor_map_options! { - "type" => cred_param.cred_type, "alg" => cred_param.alg, + "type" => cred_param.cred_type, } } } // https://www.w3.org/TR/webauthn/#enumdef-authenticatortransport -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] +#[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(test, derive(IntoEnumIterator))] -#[derive(Debug)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub enum AuthenticatorTransport { Usb, Nfc, @@ -212,8 +228,8 @@ impl TryFrom for AuthenticatorTransport { } // https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialdescriptor -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct PublicKeyCredentialDescriptor { pub key_type: PublicKeyCredentialType, pub key_id: Vec, @@ -257,18 +273,21 @@ impl TryFrom for PublicKeyCredentialDescriptor { impl From for cbor::Value { fn from(desc: PublicKeyCredentialDescriptor) -> Self { cbor_map_options! { - "type" => desc.key_type, "id" => desc.key_id, + "type" => desc.key_type, "transports" => desc.transports.map(|vec| cbor_array_vec!(vec)), } } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct MakeCredentialExtensions { pub hmac_secret: bool, pub cred_protect: Option, + pub min_pin_length: bool, + pub cred_blob: Option>, + pub large_blob_key: Option, } impl TryFrom for MakeCredentialExtensions { @@ -277,8 +296,11 @@ impl TryFrom for MakeCredentialExtensions { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { + "credBlob" => cred_blob, "credProtect" => cred_protect, "hmac-secret" => hmac_secret, + "largeBlobKey" => large_blob_key, + "minPinLength" => min_pin_length, } = extract_map(cbor_value)?; } @@ -286,17 +308,30 @@ impl TryFrom for MakeCredentialExtensions { let cred_protect = cred_protect .map(CredentialProtectionPolicy::try_from) .transpose()?; + let min_pin_length = min_pin_length.map_or(Ok(false), extract_bool)?; + let cred_blob = cred_blob.map(extract_byte_string).transpose()?; + let large_blob_key = large_blob_key.map(extract_bool).transpose()?; + if let Some(large_blob_key) = large_blob_key { + if !large_blob_key { + return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); + } + } Ok(Self { hmac_secret, cred_protect, + min_pin_length, + cred_blob, + large_blob_key, }) } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct GetAssertionExtensions { pub hmac_secret: Option, + pub cred_blob: bool, + pub large_blob_key: Option, } impl TryFrom for GetAssertionExtensions { @@ -305,24 +340,37 @@ impl TryFrom for GetAssertionExtensions { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { + "credBlob" => cred_blob, "hmac-secret" => hmac_secret, + "largeBlobKey" => large_blob_key, } = extract_map(cbor_value)?; } let hmac_secret = hmac_secret .map(GetAssertionHmacSecretInput::try_from) .transpose()?; - Ok(Self { hmac_secret }) + let cred_blob = cred_blob.map_or(Ok(false), extract_bool)?; + let large_blob_key = large_blob_key.map(extract_bool).transpose()?; + if let Some(large_blob_key) = large_blob_key { + if !large_blob_key { + return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); + } + } + Ok(Self { + hmac_secret, + cred_blob, + large_blob_key, + }) } } -#[derive(Clone)] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct GetAssertionHmacSecretInput { pub key_agreement: CoseKey, pub salt_enc: Vec, pub salt_auth: Vec, + pub pin_uv_auth_protocol: PinUvAuthProtocol, } impl TryFrom for GetAssertionHmacSecretInput { @@ -331,26 +379,30 @@ impl TryFrom for GetAssertionHmacSecretInput { fn try_from(cbor_value: cbor::Value) -> Result { destructure_cbor_map! { let { - 1 => cose_key, + 1 => key_agreement, 2 => salt_enc, 3 => salt_auth, + 4 => pin_uv_auth_protocol, } = extract_map(cbor_value)?; } - let cose_key = extract_map(ok_or_missing(cose_key)?)?; + let key_agreement = CoseKey::try_from(ok_or_missing(key_agreement)?)?; let salt_enc = extract_byte_string(ok_or_missing(salt_enc)?)?; let salt_auth = extract_byte_string(ok_or_missing(salt_auth)?)?; + let pin_uv_auth_protocol = + pin_uv_auth_protocol.map_or(Ok(PinUvAuthProtocol::V1), PinUvAuthProtocol::try_from)?; Ok(Self { - key_agreement: CoseKey(cose_key), + key_agreement, salt_enc, salt_auth, + pin_uv_auth_protocol, }) } } // Even though options are optional, we can use the default if not present. -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct MakeCredentialOptions { pub rk: bool, pub uv: bool, @@ -372,8 +424,11 @@ impl TryFrom for MakeCredentialOptions { Some(options_entry) => extract_bool(options_entry)?, None => false, }; - if up.is_some() { - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); + // In CTAP2.0, the up option is supposed to always fail when present. + if let Some(options_entry) = up { + if !extract_bool(options_entry)? { + return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); + } } let uv = match uv { Some(options_entry) => extract_bool(options_entry)?, @@ -383,13 +438,22 @@ impl TryFrom for MakeCredentialOptions { } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub struct GetAssertionOptions { pub up: bool, pub uv: bool, } +impl Default for GetAssertionOptions { + fn default() -> Self { + GetAssertionOptions { + up: true, + uv: false, + } + } +} + impl TryFrom for GetAssertionOptions { type Error = Ctap2StatusCode; @@ -420,8 +484,7 @@ impl TryFrom for GetAssertionOptions { } // https://www.w3.org/TR/webauthn/#packed-attestation -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] pub struct PackedAttestationStatement { pub alg: i64, pub sig: Vec, @@ -440,9 +503,13 @@ impl From for cbor::Value { } } -#[derive(PartialEq, Debug)] +/// Signature algorithm identifier, as specified for COSE. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub enum SignatureAlgorithm { - ES256 = ecdsa::PubKey::ES256_ALGORITHM as isize, + Es256 = ES256_ALGORITHM as isize, + #[cfg(feature = "ed25519")] + Eddsa = EDDSA_ALGORITHM as isize, // This is the default for all numbers not covered above. // Unknown types should be ignored, instead of returning errors. Unknown = 0, @@ -454,24 +521,39 @@ impl From for cbor::Value { } } -impl TryFrom for SignatureAlgorithm { - type Error = Ctap2StatusCode; - - fn try_from(cbor_value: cbor::Value) -> Result { - match extract_integer(cbor_value)? { - ecdsa::PubKey::ES256_ALGORITHM => Ok(SignatureAlgorithm::ES256), - _ => Ok(SignatureAlgorithm::Unknown), +impl From for SignatureAlgorithm { + fn from(int: i64) -> Self { + match int { + ES256_ALGORITHM => SignatureAlgorithm::Es256, + #[cfg(feature = "ed25519")] + EDDSA_ALGORITHM => SignatureAlgorithm::Eddsa, + _ => SignatureAlgorithm::Unknown, } } } -#[derive(Clone, Copy, PartialEq, PartialOrd)] -//#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] -#[derive(Debug)] +impl TryFrom for SignatureAlgorithm { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + extract_integer(cbor_value).map(SignatureAlgorithm::from) + } +} + +/// The credProtect extension's policies for resident credentials. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)] #[cfg_attr(test, derive(IntoEnumIterator))] +#[allow(clippy::enum_variant_names)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub enum CredentialProtectionPolicy { + /// The credential is always discoverable, as if it had no protection level. UserVerificationOptional = 0x01, + /// The credential is discoverable with + /// - an allowList, + /// - an excludeList, + /// - user verification. UserVerificationOptionalWithCredentialIdList = 0x02, + /// The credentials is discoverable with user verification only. UserVerificationRequired = 0x03, } @@ -498,14 +580,12 @@ impl TryFrom for CredentialProtectionPolicy { // // Note that we only use the WebAuthn definition as an example. This data-structure is not specified // by FIDO. In particular we may choose how we serialize and deserialize it. -#[derive(Clone)] -#[cfg_attr(test, derive(PartialEq))] -// #[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Clone, Debug)] +#[cfg_attr(test, derive(PartialEq, Eq))] pub struct PublicKeyCredentialSource { - // TODO function to convert to / from Vec pub key_type: PublicKeyCredentialType, pub credential_id: Vec, - pub private_key: ecdsa::SecKey, // TODO(kaczmarczyck) open for other algorithms + pub private_key: PrivateKey, pub rp_id: String, pub user_handle: Vec, // not optional, but nullable pub user_display_name: Option, @@ -513,30 +593,16 @@ pub struct PublicKeyCredentialSource { pub creation_order: u64, pub user_name: Option, pub user_icon: Option, -} - -impl std::fmt::Debug for PublicKeyCredentialSource { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("PublicKeyCredentialSource") - .field("key_type", &self.key_type) - .field("credential_id", &self.credential_id) - // private key field is missing because it's not allowed to be debugged - .field("rp_id", &self.rp_id) - .field("user_handle", &self.user_handle) - .field("user_display_name", &self.user_display_name) - .field("cred_protect_policy", &self.cred_protect_policy) - .field("creation_order", &self.creation_order) - .field("user_name", &self.user_name) - .field("user_icon", &self.user_icon) - .finish() - } + pub cred_blob: Option>, + pub large_blob_key: Option>, } // We serialize credentials for the persistent storage using CBOR maps. Each field of a credential // is associated with a unique tag, implemented with a CBOR unsigned key. enum PublicKeyCredentialSourceField { CredentialId = 0, - PrivateKey = 1, + // Deprecated, we still read this field for backwards compatibility. + EcdsaPrivateKey = 1, RpId = 2, UserHandle = 3, UserDisplayName = 4, @@ -544,25 +610,25 @@ enum PublicKeyCredentialSourceField { CreationOrder = 7, UserName = 8, UserIcon = 9, + CredBlob = 10, + LargeBlobKey = 11, + PrivateKey = 12, // When a field is removed, its tag should be reserved and not used for new fields. We document // those reserved tags below. // Reserved tags: // - CredRandom = 5, } -impl From for cbor::KeyType { - fn from(field: PublicKeyCredentialSourceField) -> cbor::KeyType { +impl From for cbor::Value { + fn from(field: PublicKeyCredentialSourceField) -> cbor::Value { (field as u64).into() } } impl From for cbor::Value { fn from(credential: PublicKeyCredentialSource) -> cbor::Value { - let mut private_key = [0u8; 32]; - credential.private_key.to_bytes(&mut private_key); cbor_map_options! { PublicKeyCredentialSourceField::CredentialId => Some(credential.credential_id), - PublicKeyCredentialSourceField::PrivateKey => Some(private_key.to_vec()), PublicKeyCredentialSourceField::RpId => Some(credential.rp_id), PublicKeyCredentialSourceField::UserHandle => Some(credential.user_handle), PublicKeyCredentialSourceField::UserDisplayName => credential.user_display_name, @@ -570,6 +636,9 @@ impl From for cbor::Value { PublicKeyCredentialSourceField::CreationOrder => credential.creation_order, PublicKeyCredentialSourceField::UserName => credential.user_name, PublicKeyCredentialSourceField::UserIcon => credential.user_icon, + PublicKeyCredentialSourceField::CredBlob => credential.cred_blob, + PublicKeyCredentialSourceField::LargeBlobKey => credential.large_blob_key, + PublicKeyCredentialSourceField::PrivateKey => credential.private_key, } } } @@ -581,7 +650,7 @@ impl TryFrom for PublicKeyCredentialSource { destructure_cbor_map! { let { PublicKeyCredentialSourceField::CredentialId => credential_id, - PublicKeyCredentialSourceField::PrivateKey => private_key, + PublicKeyCredentialSourceField::EcdsaPrivateKey => ecdsa_private_key, PublicKeyCredentialSourceField::RpId => rp_id, PublicKeyCredentialSourceField::UserHandle => user_handle, PublicKeyCredentialSourceField::UserDisplayName => user_display_name, @@ -589,25 +658,36 @@ impl TryFrom for PublicKeyCredentialSource { PublicKeyCredentialSourceField::CreationOrder => creation_order, PublicKeyCredentialSourceField::UserName => user_name, PublicKeyCredentialSourceField::UserIcon => user_icon, + PublicKeyCredentialSourceField::CredBlob => cred_blob, + PublicKeyCredentialSourceField::LargeBlobKey => large_blob_key, + PublicKeyCredentialSourceField::PrivateKey => private_key, } = extract_map(cbor_value)?; } let credential_id = extract_byte_string(ok_or_missing(credential_id)?)?; - let private_key = extract_byte_string(ok_or_missing(private_key)?)?; - if private_key.len() != 32 { - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR); - } - let private_key = ecdsa::SecKey::from_bytes(array_ref!(private_key, 0, 32)) - .ok_or(Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR)?; let rp_id = extract_text_string(ok_or_missing(rp_id)?)?; let user_handle = extract_byte_string(ok_or_missing(user_handle)?)?; let user_display_name = user_display_name.map(extract_text_string).transpose()?; let cred_protect_policy = cred_protect_policy .map(CredentialProtectionPolicy::try_from) .transpose()?; - let creation_order = creation_order.map(extract_unsigned).unwrap_or(Ok(0))?; + let creation_order = creation_order.map_or(Ok(0), extract_unsigned)?; let user_name = user_name.map(extract_text_string).transpose()?; let user_icon = user_icon.map(extract_text_string).transpose()?; + let cred_blob = cred_blob.map(extract_byte_string).transpose()?; + let large_blob_key = large_blob_key.map(extract_byte_string).transpose()?; + + // Parse the private key from the deprecated field if necessary. + let ecdsa_private_key = ecdsa_private_key.map(extract_byte_string).transpose()?; + let private_key = private_key.map(PrivateKey::try_from).transpose()?; + let private_key = match (ecdsa_private_key, private_key) { + (None, None) => return Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER), + (Some(_), Some(_)) => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + (Some(k), None) => PrivateKey::new_ecdsa_from_bytes(&k) + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?, + (None, Some(k)) => k, + }; + // We don't return whether there were unknown fields in the CBOR value. This means that // deserialization is not injective. In particular deserialization is only an inverse of // serialization at a given version of OpenSK. This is not a problem because: @@ -629,6 +709,8 @@ impl TryFrom for PublicKeyCredentialSource { creation_order, user_name, user_icon, + cred_blob, + large_blob_key, }) } } @@ -642,43 +724,140 @@ impl PublicKeyCredentialSource { } } -// TODO(kaczmarczyck) we could decide to split this data type up -// It depends on the algorithm though, I think. -// So before creating a mess, this is my workaround. -#[derive(Clone)] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(PartialEq))] -#[derive(Debug)] -pub struct CoseKey(pub BTreeMap); +// The COSE key is used for both ECDH and ECDSA public keys for transmission. +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] +pub struct CoseKey { + x_bytes: [u8; ecdh::NBYTES], + y_bytes: [u8; ecdh::NBYTES], + algorithm: i64, + key_type: i64, + curve: i64, +} -// This is the algorithm specifier that is supposed to be used in a COSE key -// map. The CTAP specification says -25 which represents ECDH-ES + HKDF-256 -// here: https://www.iana.org/assignments/cose/cose.xhtml#algorithms -// In fact, this is just used for compatibility with older specification versions. -const ECDH_ALGORITHM: i64 = -25; -// This is the identifier used by OpenSSH. To be compatible, we accept both. -const ES256_ALGORITHM: i64 = -7; -const EC2_KEY_TYPE: i64 = 2; -const P_256_CURVE: i64 = 1; +impl CoseKey { + // This is the algorithm specifier for ECDH. + // CTAP requests -25 which represents ECDH-ES + HKDF-256 here: + // https://www.iana.org/assignments/cose/cose.xhtml#algorithms + const ECDH_ALGORITHM: i64 = -25; + // The parameter behind map key 1. + const EC2_KEY_TYPE: i64 = 2; + #[cfg(feature = "ed25519")] + const OKP_KEY_TYPE: i64 = 1; + // The parameter behind map key -1. + const P_256_CURVE: i64 = 1; + #[cfg(feature = "ed25519")] + const ED25519_CURVE: i64 = 6; +} + +// This conversion accepts both ECDH and ECDSA. +impl TryFrom for CoseKey { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + // This is sorted correctly, negative encoding is bigger. + 1 => key_type, + 3 => algorithm, + -1 => curve, + -2 => x_bytes, + -3 => y_bytes, + } = extract_map(cbor_value)?; + } + + let algorithm = extract_integer(ok_or_missing(algorithm)?)?; + let nbytes = match algorithm { + CoseKey::ECDH_ALGORITHM => ecdh::NBYTES, + ES256_ALGORITHM => ecdsa::NBYTES, + _ => return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM), + }; + let x_bytes = extract_byte_string(ok_or_missing(x_bytes)?)?; + if x_bytes.len() != nbytes { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + let y_bytes = extract_byte_string(ok_or_missing(y_bytes)?)?; + if y_bytes.len() != nbytes { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + let curve = extract_integer(ok_or_missing(curve)?)?; + if curve != CoseKey::P_256_CURVE { + return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); + } + let key_type = extract_integer(ok_or_missing(key_type)?)?; + if key_type != CoseKey::EC2_KEY_TYPE { + return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); + } + + Ok(CoseKey { + x_bytes: *array_ref![x_bytes.as_slice(), 0, ecdh::NBYTES], + y_bytes: *array_ref![y_bytes.as_slice(), 0, ecdh::NBYTES], + algorithm, + key_type, + curve, + }) + } +} + +impl From for cbor::Value { + fn from(cose_key: CoseKey) -> Self { + let CoseKey { + x_bytes, + y_bytes, + algorithm, + key_type, + curve, + } = cose_key; + + cbor_map! { + 1 => key_type, + 3 => algorithm, + -1 => curve, + -2 => x_bytes, + -3 => y_bytes, + } + } +} impl From for CoseKey { fn from(pk: ecdh::PubKey) -> Self { let mut x_bytes = [0; ecdh::NBYTES]; let mut y_bytes = [0; ecdh::NBYTES]; pk.to_coordinates(&mut x_bytes, &mut y_bytes); - let x_byte_cbor: cbor::Value = cbor_bytes_lit!(&x_bytes); - let y_byte_cbor: cbor::Value = cbor_bytes_lit!(&y_bytes); - // TODO(kaczmarczyck) do not write optional parameters, spec is unclear - let cose_cbor_value = cbor_map_options! { - 1 => EC2_KEY_TYPE, - 3 => ECDH_ALGORITHM, - -1 => P_256_CURVE, - -2 => x_byte_cbor, - -3 => y_byte_cbor, - }; - if let cbor::Value::Map(cose_map) = cose_cbor_value { - CoseKey(cose_map) - } else { - unreachable!(); + CoseKey { + x_bytes, + y_bytes, + algorithm: CoseKey::ECDH_ALGORITHM, + key_type: CoseKey::EC2_KEY_TYPE, + curve: CoseKey::P_256_CURVE, + } + } +} + +impl From for CoseKey { + fn from(pk: ecdsa::PubKey) -> Self { + let mut x_bytes = [0; ecdsa::NBYTES]; + let mut y_bytes = [0; ecdsa::NBYTES]; + pk.to_coordinates(&mut x_bytes, &mut y_bytes); + CoseKey { + x_bytes, + y_bytes, + algorithm: ES256_ALGORITHM, + key_type: CoseKey::EC2_KEY_TYPE, + curve: CoseKey::P_256_CURVE, + } + } +} + +#[cfg(feature = "ed25519")] +impl From for CoseKey { + fn from(pk: ed25519_compact::PublicKey) -> Self { + CoseKey { + x_bytes: *pk, + y_bytes: [0u8; 32], + key_type: CoseKey::OKP_KEY_TYPE, + curve: CoseKey::ED25519_CURVE, + algorithm: EDDSA_ALGORITHM, } } } @@ -687,60 +866,82 @@ impl TryFrom for ecdh::PubKey { type Error = Ctap2StatusCode; fn try_from(cose_key: CoseKey) -> Result { - destructure_cbor_map! { - let { - 1 => key_type, - 3 => algorithm, - -1 => curve, - -2 => x_bytes, - -3 => y_bytes, - } = cose_key.0; - } + let CoseKey { + x_bytes, + y_bytes, + algorithm, + key_type, + curve, + } = cose_key; - let key_type = extract_integer(ok_or_missing(key_type)?)?; - if key_type != EC2_KEY_TYPE { + // Since algorithm can be used for different COSE key types, we check + // whether the current type is correct for ECDH. For an OpenSSH bugfix, + // the algorithm ES256_ALGORITHM is allowed here too. + // https://github.com/google/OpenSK/issues/90 + if algorithm != CoseKey::ECDH_ALGORITHM && algorithm != ES256_ALGORITHM { return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); } - let algorithm = extract_integer(ok_or_missing(algorithm)?)?; - if algorithm != ECDH_ALGORITHM && algorithm != ES256_ALGORITHM { + if key_type != CoseKey::EC2_KEY_TYPE || curve != CoseKey::P_256_CURVE { return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); } - let curve = extract_integer(ok_or_missing(curve)?)?; - if curve != P_256_CURVE { - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); - } - let x_bytes = extract_byte_string(ok_or_missing(x_bytes)?)?; - if x_bytes.len() != ecdh::NBYTES { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - let y_bytes = extract_byte_string(ok_or_missing(y_bytes)?)?; - if y_bytes.len() != ecdh::NBYTES { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - - let x_array_ref = array_ref![x_bytes.as_slice(), 0, ecdh::NBYTES]; - let y_array_ref = array_ref![y_bytes.as_slice(), 0, ecdh::NBYTES]; - ecdh::PubKey::from_coordinates(x_array_ref, y_array_ref) + ecdh::PubKey::from_coordinates(&x_bytes, &y_bytes) .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) } } -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Clone, PartialEq))] +impl TryFrom for ecdsa::PubKey { + type Error = Ctap2StatusCode; + + fn try_from(cose_key: CoseKey) -> Result { + let CoseKey { + x_bytes, + y_bytes, + algorithm, + key_type, + curve, + } = cose_key; + + if algorithm != ES256_ALGORITHM + || key_type != CoseKey::EC2_KEY_TYPE + || curve != CoseKey::P_256_CURVE + { + return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); + } + ecdsa::PubKey::from_coordinates(&x_bytes, &y_bytes) + .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] +pub enum PinUvAuthProtocol { + V1 = 1, + V2 = 2, +} + +impl TryFrom for PinUvAuthProtocol { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + match extract_unsigned(cbor_value)? { + 1 => Ok(PinUvAuthProtocol::V1), + 2 => Ok(PinUvAuthProtocol::V2), + _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(test, derive(IntoEnumIterator))] -#[derive(Debug)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] pub enum ClientPinSubCommand { GetPinRetries = 0x01, GetKeyAgreement = 0x02, SetPin = 0x03, ChangePin = 0x04, GetPinToken = 0x05, - #[cfg(feature = "with_ctap2_1")] GetPinUvAuthTokenUsingUvWithPermissions = 0x06, - #[cfg(feature = "with_ctap2_1")] GetUvRetries = 0x07, - #[cfg(feature = "with_ctap2_1")] - SetMinPinLength = 0x08, - #[cfg(feature = "with_ctap2_1")] GetPinUvAuthTokenUsingPinWithPermissions = 0x09, } @@ -761,53 +962,248 @@ impl TryFrom for ClientPinSubCommand { 0x03 => Ok(ClientPinSubCommand::SetPin), 0x04 => Ok(ClientPinSubCommand::ChangePin), 0x05 => Ok(ClientPinSubCommand::GetPinToken), - #[cfg(feature = "with_ctap2_1")] 0x06 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingUvWithPermissions), - #[cfg(feature = "with_ctap2_1")] 0x07 => Ok(ClientPinSubCommand::GetUvRetries), - #[cfg(feature = "with_ctap2_1")] - 0x08 => Ok(ClientPinSubCommand::SetMinPinLength), - #[cfg(feature = "with_ctap2_1")] 0x09 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions), - #[cfg(feature = "with_ctap2_1")] _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND), - #[cfg(not(feature = "with_ctap2_1"))] - _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(IntoEnumIterator))] +pub enum ConfigSubCommand { + EnableEnterpriseAttestation = 0x01, + ToggleAlwaysUv = 0x02, + SetMinPinLength = 0x03, + VendorPrototype = 0xFF, +} + +impl From for cbor::Value { + fn from(subcommand: ConfigSubCommand) -> Self { + (subcommand as u64).into() + } +} + +impl TryFrom for ConfigSubCommand { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + let subcommand_int = extract_unsigned(cbor_value)?; + match subcommand_int { + 0x01 => Ok(ConfigSubCommand::EnableEnterpriseAttestation), + 0x02 => Ok(ConfigSubCommand::ToggleAlwaysUv), + 0x03 => Ok(ConfigSubCommand::SetMinPinLength), + 0xFF => Ok(ConfigSubCommand::VendorPrototype), + _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ConfigSubCommandParams { + SetMinPinLength(SetMinPinLengthParams), +} + +impl From for cbor::Value { + fn from(params: ConfigSubCommandParams) -> Self { + match params { + ConfigSubCommandParams::SetMinPinLength(set_min_pin_length_params) => { + set_min_pin_length_params.into() + } + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SetMinPinLengthParams { + pub new_min_pin_length: Option, + pub min_pin_length_rp_ids: Option>, + pub force_change_pin: Option, +} + +impl TryFrom for SetMinPinLengthParams { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => new_min_pin_length, + 0x02 => min_pin_length_rp_ids, + 0x03 => force_change_pin, + } = extract_map(cbor_value)?; + } + + let new_min_pin_length = new_min_pin_length + .map(extract_unsigned) + .transpose()? + .map(u8::try_from) + .transpose() + .map_err(|_| Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION)?; + let min_pin_length_rp_ids = match min_pin_length_rp_ids { + Some(entry) => Some( + extract_array(entry)? + .into_iter() + .map(extract_text_string) + .collect::, Ctap2StatusCode>>()?, + ), + None => None, + }; + let force_change_pin = force_change_pin.map(extract_bool).transpose()?; + + Ok(Self { + new_min_pin_length, + min_pin_length_rp_ids, + force_change_pin, + }) + } +} + +impl From for cbor::Value { + fn from(params: SetMinPinLengthParams) -> Self { + cbor_map_options! { + 0x01 => params.new_min_pin_length.map(|u| u as u64), + 0x02 => params.min_pin_length_rp_ids.map(|vec| cbor_array_vec!(vec)), + 0x03 => params.force_change_pin, + } + } +} + +/// The level of enterprise attestation allowed in MakeCredential. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "fuzz", derive(Arbitrary))] +pub enum EnterpriseAttestationMode { + /// Enterprise attestation is restricted to a list of RP IDs. Add your + /// enterprises domain, e.g. "example.com", to the list below. + VendorFacilitated = 0x01, + /// All relying parties can request an enterprise attestation. The authenticator + /// trusts the platform to filter requests. + PlatformManaged = 0x02, +} + +impl TryFrom for EnterpriseAttestationMode { + type Error = Ctap2StatusCode; + + fn try_from(value: u64) -> Result { + match value { + 1 => Ok(EnterpriseAttestationMode::VendorFacilitated), + 2 => Ok(EnterpriseAttestationMode::PlatformManaged), + _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(IntoEnumIterator))] +pub enum CredentialManagementSubCommand { + GetCredsMetadata = 0x01, + EnumerateRpsBegin = 0x02, + EnumerateRpsGetNextRp = 0x03, + EnumerateCredentialsBegin = 0x04, + EnumerateCredentialsGetNextCredential = 0x05, + DeleteCredential = 0x06, + UpdateUserInformation = 0x07, +} + +impl From for cbor::Value { + fn from(subcommand: CredentialManagementSubCommand) -> Self { + (subcommand as u64).into() + } +} + +impl TryFrom for CredentialManagementSubCommand { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + let subcommand_int = extract_unsigned(cbor_value)?; + match subcommand_int { + 0x01 => Ok(CredentialManagementSubCommand::GetCredsMetadata), + 0x02 => Ok(CredentialManagementSubCommand::EnumerateRpsBegin), + 0x03 => Ok(CredentialManagementSubCommand::EnumerateRpsGetNextRp), + 0x04 => Ok(CredentialManagementSubCommand::EnumerateCredentialsBegin), + 0x05 => Ok(CredentialManagementSubCommand::EnumerateCredentialsGetNextCredential), + 0x06 => Ok(CredentialManagementSubCommand::DeleteCredential), + 0x07 => Ok(CredentialManagementSubCommand::UpdateUserInformation), + _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND), + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CredentialManagementSubCommandParameters { + pub rp_id_hash: Option>, + pub credential_id: Option, + pub user: Option, +} + +impl TryFrom for CredentialManagementSubCommandParameters { + type Error = Ctap2StatusCode; + + fn try_from(cbor_value: cbor::Value) -> Result { + destructure_cbor_map! { + let { + 0x01 => rp_id_hash, + 0x02 => credential_id, + 0x03 => user, + } = extract_map(cbor_value)?; + } + + let rp_id_hash = rp_id_hash.map(extract_byte_string).transpose()?; + let credential_id = credential_id + .map(PublicKeyCredentialDescriptor::try_from) + .transpose()?; + let user = user + .map(PublicKeyCredentialUserEntity::try_from) + .transpose()?; + Ok(Self { + rp_id_hash, + credential_id, + user, + }) + } +} + +impl From for cbor::Value { + fn from(sub_command_params: CredentialManagementSubCommandParameters) -> Self { + cbor_map_options! { + 0x01 => sub_command_params.rp_id_hash, + 0x02 => sub_command_params.credential_id, + 0x03 => sub_command_params.user, } } } pub(super) fn extract_unsigned(cbor_value: cbor::Value) -> Result { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned), + cbor::Value::Unsigned(unsigned) => Ok(unsigned), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } pub(super) fn extract_integer(cbor_value: cbor::Value) -> Result { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => { + cbor::Value::Unsigned(unsigned) => { if unsigned <= core::i64::MAX as u64 { Ok(unsigned as i64) } else { Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE) } } - cbor::Value::KeyValue(cbor::KeyType::Negative(signed)) => Ok(signed), + cbor::Value::Negative(signed) => Ok(signed), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } pub fn extract_byte_string(cbor_value: cbor::Value) -> Result, Ctap2StatusCode> { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::ByteString(byte_string)) => Ok(byte_string), + cbor::Value::ByteString(byte_string) => Ok(byte_string), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } pub(super) fn extract_text_string(cbor_value: cbor::Value) -> Result { match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::TextString(text_string)) => Ok(text_string), + cbor::Value::TextString(text_string) => Ok(text_string), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } @@ -821,7 +1217,7 @@ pub(super) fn extract_array(cbor_value: cbor::Value) -> Result, pub(super) fn extract_map( cbor_value: cbor::Value, -) -> Result, Ctap2StatusCode> { +) -> Result, Ctap2StatusCode> { match cbor_value { cbor::Value::Map(map) => Ok(map), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), @@ -844,12 +1240,12 @@ pub(super) fn ok_or_missing(value_option: Option) -> Result cbor_false!(), - "foo" => b"bar", b"bin" => -42, + "foo" => b"bar", }), - Ok([ + Ok(vec![ (cbor_unsigned!(1), cbor_false!()), - (cbor_text!("foo"), cbor_bytes_lit!(b"bar")), (cbor_bytes_lit!(b"bin"), cbor_int!(-42)), - ] - .iter() - .cloned() - .collect::>()) + (cbor_text!("foo"), cbor_bytes_lit!(b"bar")), + ]) ); } @@ -1121,8 +1514,8 @@ mod test { fn test_from_public_key_credential_rp_entity() { let cbor_rp_entity = cbor_map! { "id" => "example.com", - "name" => "Example", "icon" => "example.com/icon.png", + "name" => "Example", }; let rp_entity = PublicKeyCredentialRpEntity::try_from(cbor_rp_entity); let expected_rp_entity = PublicKeyCredentialRpEntity { @@ -1137,9 +1530,9 @@ mod test { fn test_from_into_public_key_credential_user_entity() { let cbor_user_entity = cbor_map! { "id" => vec![0x1D, 0x1D, 0x1D, 0x1D], + "icon" => "example.com/foo/icon.png", "name" => "foo", "displayName" => "bar", - "icon" => "example.com/foo/icon.png", }; let user_entity = PublicKeyCredentialUserEntity::try_from(cbor_user_entity.clone()); let expected_user_entity = PublicKeyCredentialUserEntity { @@ -1168,15 +1561,44 @@ mod test { assert_eq!(unknown_type, Ok(expected_unknown_type)); } + #[test] + fn test_from_into_signature_algorithm_int() { + let alg_int = SignatureAlgorithm::Es256 as i64; + let signature_algorithm = SignatureAlgorithm::from(alg_int); + assert_eq!(signature_algorithm, SignatureAlgorithm::Es256); + + #[cfg(feature = "ed25519")] + { + let alg_int = SignatureAlgorithm::Eddsa as i64; + let signature_algorithm = SignatureAlgorithm::from(alg_int); + assert_eq!(signature_algorithm, SignatureAlgorithm::Eddsa); + } + + let unknown_alg_int = -1; + let unknown_algorithm = SignatureAlgorithm::from(unknown_alg_int); + assert_eq!(unknown_algorithm, SignatureAlgorithm::Unknown); + } + #[test] fn test_from_into_signature_algorithm() { - let cbor_signature_algorithm: cbor::Value = cbor_int!(ecdsa::PubKey::ES256_ALGORITHM); + let cbor_signature_algorithm: cbor::Value = cbor_int!(ES256_ALGORITHM); let signature_algorithm = SignatureAlgorithm::try_from(cbor_signature_algorithm.clone()); - let expected_signature_algorithm = SignatureAlgorithm::ES256; + let expected_signature_algorithm = SignatureAlgorithm::Es256; assert_eq!(signature_algorithm, Ok(expected_signature_algorithm)); let created_cbor: cbor::Value = signature_algorithm.unwrap().into(); assert_eq!(created_cbor, cbor_signature_algorithm); + #[cfg(feature = "ed25519")] + { + let cbor_signature_algorithm: cbor::Value = cbor_int!(EDDSA_ALGORITHM); + let signature_algorithm = + SignatureAlgorithm::try_from(cbor_signature_algorithm.clone()); + let expected_signature_algorithm = SignatureAlgorithm::Eddsa; + assert_eq!(signature_algorithm, Ok(expected_signature_algorithm)); + let created_cbor: cbor::Value = signature_algorithm.unwrap().into(); + assert_eq!(created_cbor, cbor_signature_algorithm); + } + let cbor_unknown_algorithm: cbor::Value = cbor_int!(-1); let unknown_algorithm = SignatureAlgorithm::try_from(cbor_unknown_algorithm); let expected_unknown_algorithm = SignatureAlgorithm::Unknown; @@ -1240,28 +1662,41 @@ mod test { } } - #[test] - fn test_from_into_public_key_credential_parameter() { + fn test_from_into_public_key_credential_parameter( + alg_int: i64, + signature_algorithm: SignatureAlgorithm, + ) { let cbor_credential_parameter = cbor_map! { + "alg" => alg_int, "type" => "public-key", - "alg" => ecdsa::PubKey::ES256_ALGORITHM, }; let credential_parameter = PublicKeyCredentialParameter::try_from(cbor_credential_parameter.clone()); let expected_credential_parameter = PublicKeyCredentialParameter { cred_type: PublicKeyCredentialType::PublicKey, - alg: SignatureAlgorithm::ES256, + alg: signature_algorithm, }; assert_eq!(credential_parameter, Ok(expected_credential_parameter)); let created_cbor: cbor::Value = credential_parameter.unwrap().into(); assert_eq!(created_cbor, cbor_credential_parameter); } + #[test] + fn test_from_into_ecdsa_public_key_credential_parameter() { + test_from_into_public_key_credential_parameter(ES256_ALGORITHM, SignatureAlgorithm::Es256); + } + + #[test] + #[cfg(feature = "ed25519")] + fn test_from_into_ed25519_public_key_credential_parameter() { + test_from_into_public_key_credential_parameter(EDDSA_ALGORITHM, SignatureAlgorithm::Eddsa); + } + #[test] fn test_from_into_public_key_credential_descriptor() { let cbor_credential_descriptor = cbor_map! { - "type" => "public-key", "id" => vec![0x2D, 0x2D, 0x2D, 0x2D], + "type" => "public-key", "transports" => cbor_array!["usb"], }; let credential_descriptor = @@ -1279,42 +1714,85 @@ mod test { #[test] fn test_from_make_credential_extensions() { let cbor_extensions = cbor_map! { - "hmac-secret" => true, + "credBlob" => vec![0xCB], "credProtect" => CredentialProtectionPolicy::UserVerificationRequired, + "hmac-secret" => true, + "largeBlobKey" => true, + "minPinLength" => true, }; let extensions = MakeCredentialExtensions::try_from(cbor_extensions); let expected_extensions = MakeCredentialExtensions { hmac_secret: true, cred_protect: Some(CredentialProtectionPolicy::UserVerificationRequired), + min_pin_length: true, + cred_blob: Some(vec![0xCB]), + large_blob_key: Some(true), }; assert_eq!(extensions, Ok(expected_extensions)); } #[test] - fn test_from_get_assertion_extensions() { - let mut rng = ThreadRng256 {}; - let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); + fn test_from_get_assertion_extensions_default_protocol() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); let pk = sk.genpk(); let cose_key = CoseKey::from(pk); let cbor_extensions = cbor_map! { + "credBlob" => true, "hmac-secret" => cbor_map! { - 1 => cbor::Value::Map(cose_key.0.clone()), + 1 => cbor::Value::from(cose_key.clone()), 2 => vec![0x02; 32], 3 => vec![0x03; 16], }, + "largeBlobKey" => true, }; let extensions = GetAssertionExtensions::try_from(cbor_extensions); let expected_input = GetAssertionHmacSecretInput { key_agreement: cose_key, salt_enc: vec![0x02; 32], salt_auth: vec![0x03; 16], + pin_uv_auth_protocol: PinUvAuthProtocol::V1, }; let expected_extensions = GetAssertionExtensions { hmac_secret: Some(expected_input), + cred_blob: true, + large_blob_key: Some(true), }; assert_eq!(extensions, Ok(expected_extensions)); } + #[test] + fn test_from_get_assertion_extensions_with_protocol() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pk = sk.genpk(); + let cose_key = CoseKey::from(pk); + let cbor_extensions = cbor_map! { + "credBlob" => true, + "hmac-secret" => cbor_map! { + 1 => cbor::Value::from(cose_key.clone()), + 2 => vec![0x02; 32], + 3 => vec![0x03; 16], + 4 => 2, + }, + "largeBlobKey" => true, + }; + let extensions = GetAssertionExtensions::try_from(cbor_extensions); + let expected_input = GetAssertionHmacSecretInput { + key_agreement: cose_key, + salt_enc: vec![0x02; 32], + salt_auth: vec![0x03; 16], + pin_uv_auth_protocol: PinUvAuthProtocol::V2, + }; + let expected_extensions = GetAssertionExtensions { + hmac_secret: Some(expected_input), + cred_blob: true, + large_blob_key: Some(true), + }; + assert_eq!(extensions, Ok(expected_extensions)); + // TODO more tests, check default + } + #[test] fn test_from_make_credential_options() { let cbor_make_options = cbor_map! { @@ -1345,11 +1823,11 @@ mod test { #[test] fn test_into_packed_attestation_statement() { - let certificate: cbor::values::KeyType = cbor_bytes![vec![0x5C, 0x5C, 0x5C, 0x5C]]; + let certificate = cbor_bytes![vec![0x5C, 0x5C, 0x5C, 0x5C]]; let cbor_packed_attestation_statement = cbor_map! { "alg" => 1, "sig" => vec![0x55, 0x55, 0x55, 0x55], - "x5c" => cbor_array_vec![vec![certificate]], + "x5c" => cbor_array![certificate], "ecdaaKeyId" => vec![0xEC, 0xDA, 0x1D], }; let packed_attestation_statement = PackedAttestationStatement { @@ -1363,15 +1841,139 @@ mod test { } #[test] - fn test_from_into_cose_key() { - let mut rng = ThreadRng256 {}; - let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); + fn test_from_into_cose_key_cbor() { + for algorithm in &[CoseKey::ECDH_ALGORITHM, ES256_ALGORITHM] { + let cbor_value = cbor_map! { + 1 => CoseKey::EC2_KEY_TYPE, + 3 => algorithm, + -1 => CoseKey::P_256_CURVE, + -2 => [0u8; 32], + -3 => [0u8; 32], + }; + let cose_key = CoseKey::try_from(cbor_value.clone()).unwrap(); + let created_cbor_value = cbor::Value::from(cose_key); + assert_eq!(created_cbor_value, cbor_value); + } + } + + #[test] + fn test_cose_key_unknown_algorithm() { + let cbor_value = cbor_map! { + 1 => CoseKey::EC2_KEY_TYPE, + // unknown algorithm + 3 => 0, + -1 => CoseKey::P_256_CURVE, + -2 => [0u8; 32], + -3 => [0u8; 32], + }; + assert_eq!( + CoseKey::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM) + ); + } + + #[test] + fn test_cose_key_unknown_type() { + let cbor_value = cbor_map! { + // unknown type + 1 => 0, + 3 => CoseKey::ECDH_ALGORITHM, + -1 => CoseKey::P_256_CURVE, + -2 => [0u8; 32], + -3 => [0u8; 32], + }; + assert_eq!( + CoseKey::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM) + ); + } + + #[test] + fn test_cose_key_unknown_curve() { + let cbor_value = cbor_map! { + 1 => CoseKey::EC2_KEY_TYPE, + 3 => CoseKey::ECDH_ALGORITHM, + // unknown curve + -1 => 0, + -2 => [0u8; 32], + -3 => [0u8; 32], + }; + assert_eq!( + CoseKey::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM) + ); + } + + #[test] + fn test_cose_key_wrong_length_x() { + let cbor_value = cbor_map! { + 1 => CoseKey::EC2_KEY_TYPE, + 3 => CoseKey::ECDH_ALGORITHM, + -1 => CoseKey::P_256_CURVE, + // wrong length + -2 => [0u8; 31], + -3 => [0u8; 32], + }; + assert_eq!( + CoseKey::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + + #[test] + fn test_cose_key_wrong_length_y() { + let cbor_value = cbor_map! { + 1 => CoseKey::EC2_KEY_TYPE, + 3 => CoseKey::ECDH_ALGORITHM, + -1 => CoseKey::P_256_CURVE, + -2 => [0u8; 32], + // wrong length + -3 => [0u8; 33], + }; + assert_eq!( + CoseKey::try_from(cbor_value), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + + #[test] + fn test_from_into_cose_key_ecdh() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); let pk = sk.genpk(); let cose_key = CoseKey::from(pk.clone()); let created_pk = ecdh::PubKey::try_from(cose_key); assert_eq!(created_pk, Ok(pk)); } + #[test] + fn test_into_cose_key_ecdsa() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdsa::SecKey::gensk(env.rng()); + let pk = sk.genpk(); + let cose_key = CoseKey::from(pk); + assert_eq!(cose_key.algorithm, ES256_ALGORITHM); + } + + #[test] + fn test_from_pin_uv_auth_protocol() { + let cbor_protocol: cbor::Value = cbor_int!(0x01); + assert_eq!( + PinUvAuthProtocol::try_from(cbor_protocol), + Ok(PinUvAuthProtocol::V1) + ); + let cbor_protocol: cbor::Value = cbor_int!(0x02); + assert_eq!( + PinUvAuthProtocol::try_from(cbor_protocol), + Ok(PinUvAuthProtocol::V2) + ); + let cbor_protocol: cbor::Value = cbor_int!(0x03); + assert_eq!( + PinUvAuthProtocol::try_from(cbor_protocol), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + } + #[test] fn test_from_into_client_pin_sub_command() { let cbor_sub_command: cbor::Value = cbor_int!(0x01); @@ -1388,13 +1990,130 @@ mod test { } } + #[test] + fn test_from_into_config_sub_command() { + let cbor_sub_command: cbor::Value = cbor_int!(0x01); + let sub_command = ConfigSubCommand::try_from(cbor_sub_command.clone()); + let expected_sub_command = ConfigSubCommand::EnableEnterpriseAttestation; + assert_eq!(sub_command, Ok(expected_sub_command)); + let created_cbor: cbor::Value = sub_command.unwrap().into(); + assert_eq!(created_cbor, cbor_sub_command); + + for command in ConfigSubCommand::into_enum_iter() { + let created_cbor: cbor::Value = command.into(); + let reconstructed = ConfigSubCommand::try_from(created_cbor).unwrap(); + assert_eq!(command, reconstructed); + } + } + + #[test] + fn test_from_set_min_pin_length_params() { + let params = SetMinPinLengthParams { + new_min_pin_length: Some(6), + min_pin_length_rp_ids: Some(vec!["example.com".to_string()]), + force_change_pin: Some(true), + }; + let cbor_params = cbor_map! { + 0x01 => 6, + 0x02 => cbor_array!("example.com".to_string()), + 0x03 => true, + }; + assert_eq!(cbor::Value::from(params.clone()), cbor_params); + let reconstructed_params = SetMinPinLengthParams::try_from(cbor_params); + assert_eq!(reconstructed_params, Ok(params)); + } + + #[test] + fn test_from_config_sub_command_params() { + let set_min_pin_length_params = SetMinPinLengthParams { + new_min_pin_length: Some(6), + min_pin_length_rp_ids: Some(vec!["example.com".to_string()]), + force_change_pin: Some(true), + }; + let config_sub_command_params = + ConfigSubCommandParams::SetMinPinLength(set_min_pin_length_params); + let cbor_params = cbor_map! { + 0x01 => 6, + 0x02 => cbor_array!("example.com".to_string()), + 0x03 => true, + }; + assert_eq!(cbor::Value::from(config_sub_command_params), cbor_params); + } + + #[test] + fn test_from_enterprise_attestation_mode() { + assert_eq!( + EnterpriseAttestationMode::try_from(0), + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION), + ); + assert_eq!( + EnterpriseAttestationMode::try_from(1), + Ok(EnterpriseAttestationMode::VendorFacilitated), + ); + assert_eq!( + EnterpriseAttestationMode::try_from(2), + Ok(EnterpriseAttestationMode::PlatformManaged), + ); + assert_eq!( + EnterpriseAttestationMode::try_from(3), + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION), + ); + } + + #[test] + fn test_from_into_cred_management_sub_command() { + let cbor_sub_command: cbor::Value = cbor_int!(0x01); + let sub_command = CredentialManagementSubCommand::try_from(cbor_sub_command.clone()); + let expected_sub_command = CredentialManagementSubCommand::GetCredsMetadata; + assert_eq!(sub_command, Ok(expected_sub_command)); + let created_cbor: cbor::Value = sub_command.unwrap().into(); + assert_eq!(created_cbor, cbor_sub_command); + + for command in CredentialManagementSubCommand::into_enum_iter() { + let created_cbor: cbor::Value = command.into(); + let reconstructed = CredentialManagementSubCommand::try_from(created_cbor).unwrap(); + assert_eq!(command, reconstructed); + } + } + + #[test] + fn test_from_into_cred_management_sub_command_params() { + let credential_id = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: vec![0x2D, 0x2D, 0x2D, 0x2D], + transports: Some(vec![AuthenticatorTransport::Usb]), + }; + let user_entity = PublicKeyCredentialUserEntity { + user_id: vec![0x1D, 0x1D, 0x1D, 0x1D], + user_name: Some("foo".to_string()), + user_display_name: Some("bar".to_string()), + user_icon: Some("example.com/foo/icon.png".to_string()), + }; + let cbor_sub_command_params = cbor_map! { + 0x01 => vec![0x1D; 32], + 0x02 => credential_id.clone(), + 0x03 => user_entity.clone(), + }; + let sub_command_params = + CredentialManagementSubCommandParameters::try_from(cbor_sub_command_params.clone()); + let expected_sub_command_params = CredentialManagementSubCommandParameters { + rp_id_hash: Some(vec![0x1D; 32]), + credential_id: Some(credential_id), + user: Some(user_entity), + }; + assert_eq!(sub_command_params, Ok(expected_sub_command_params)); + let created_cbor: cbor::Value = sub_command_params.unwrap().into(); + assert_eq!(created_cbor, cbor_sub_command_params); + } + #[test] fn test_credential_source_cbor_round_trip() { - let mut rng = ThreadRng256 {}; + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, - credential_id: rng.gen_uniform_u8x32().to_vec(), - private_key: ctap_crypto::ecdsa::SecKey::gensk(&mut rng), + credential_id: env.rng().gen_uniform_u8x32().to_vec(), + private_key, rp_id: "example.com".to_string(), user_handle: b"foo".to_vec(), user_display_name: None, @@ -1402,6 +2121,8 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; assert_eq!( @@ -1444,12 +2165,107 @@ mod test { ..credential }; + assert_eq!( + PublicKeyCredentialSource::try_from(cbor::Value::from(credential.clone())), + Ok(credential.clone()) + ); + + let credential = PublicKeyCredentialSource { + cred_blob: Some(vec![0xCB]), + ..credential + }; + + assert_eq!( + PublicKeyCredentialSource::try_from(cbor::Value::from(credential.clone())), + Ok(credential.clone()) + ); + + let credential = PublicKeyCredentialSource { + large_blob_key: Some(vec![0x1B]), + ..credential + }; + assert_eq!( PublicKeyCredentialSource::try_from(cbor::Value::from(credential.clone())), Ok(credential) ); } + #[test] + fn test_credential_source_cbor_read_legacy() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let key_bytes = private_key.to_bytes(); + let credential = PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id: env.rng().gen_uniform_u8x32().to_vec(), + private_key, + rp_id: "example.com".to_string(), + user_handle: b"foo".to_vec(), + user_display_name: None, + cred_protect_policy: None, + creation_order: 0, + user_name: None, + user_icon: None, + cred_blob: None, + large_blob_key: None, + }; + + let source_cbor = cbor_map! { + PublicKeyCredentialSourceField::CredentialId => credential.credential_id.clone(), + PublicKeyCredentialSourceField::EcdsaPrivateKey => key_bytes, + PublicKeyCredentialSourceField::RpId => credential.rp_id.clone(), + PublicKeyCredentialSourceField::UserHandle => credential.user_handle.clone(), + }; + assert_eq!( + PublicKeyCredentialSource::try_from(source_cbor), + Ok(credential) + ); + } + + #[test] + fn test_credential_source_cbor_legacy_error() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let key_bytes = private_key.to_bytes(); + let credential = PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id: env.rng().gen_uniform_u8x32().to_vec(), + private_key: private_key.clone(), + rp_id: "example.com".to_string(), + user_handle: b"foo".to_vec(), + user_display_name: None, + cred_protect_policy: None, + creation_order: 0, + user_name: None, + user_icon: None, + cred_blob: None, + large_blob_key: None, + }; + + let source_cbor = cbor_map! { + PublicKeyCredentialSourceField::CredentialId => credential.credential_id.clone(), + PublicKeyCredentialSourceField::RpId => credential.rp_id.clone(), + PublicKeyCredentialSourceField::UserHandle => credential.user_handle.clone(), + }; + assert_eq!( + PublicKeyCredentialSource::try_from(source_cbor), + Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER) + ); + + let source_cbor = cbor_map! { + PublicKeyCredentialSourceField::CredentialId => credential.credential_id, + PublicKeyCredentialSourceField::EcdsaPrivateKey => key_bytes, + PublicKeyCredentialSourceField::RpId => credential.rp_id, + PublicKeyCredentialSourceField::UserHandle => credential.user_handle, + PublicKeyCredentialSourceField::PrivateKey => private_key, + }; + assert_eq!( + PublicKeyCredentialSource::try_from(source_cbor), + Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) + ); + } + #[test] fn test_credential_source_invalid_cbor() { assert!(PublicKeyCredentialSource::try_from(cbor_false!()).is_err()); diff --git a/apps/vault/src/ctap/hid/mod.rs b/apps/vault/src/ctap/hid/mod.rs index f825a10d8..4f969b21e 100644 --- a/apps/vault/src/ctap/hid/mod.rs +++ b/apps/vault/src/ctap/hid/mod.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,33 +12,99 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub mod receive; +mod receive; pub mod send; -use self::{receive::MessageAssembler, send::CTAPHIDResponse}; -use self::send::HidPacketIterator; -#[cfg(feature = "with_ctap1")] -use super::ctap1; +// Implementation details must be public for testing (in particular fuzzing). +#[cfg(feature = "std")] +pub use self::receive::MessageAssembler; +#[cfg(not(feature = "std"))] +use self::receive::MessageAssembler; +pub use self::send::HidPacketIterator; +use std::time::Instant; use super::status_code::Ctap2StatusCode; -use super::timed_permission::TimedPermission; -use super::CtapState; +use crate::env::Env; use std::vec; use std::vec::Vec; use arrayref::{array_ref, array_refs}; -#[cfg(feature = "debug_ctap")] -use core::fmt::Write; -use ctap_crypto::rng256::Rng256; -#[cfg(feature = "debug_ctap")] -use libtock_drivers::console::Console; -use crate::shims::{ClockValue, Duration, Timestamp}; - -// CTAP specification (version 20190130) section 8.1 -// TODO: Channel allocation, section 8.1.3? -// TODO: Transaction timeout, section 8.1.5.2 +use std::time::Duration; +#[cfg(test)] +use enum_iterator::IntoEnumIterator; pub type HidPacket = [u8; 64]; pub type ChannelID = [u8; 4]; +/// CTAPHID commands +/// +/// See section 11.2.9. of FIDO 2.1 (2021-06-15). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[cfg_attr(test, derive(IntoEnumIterator))] +pub enum CtapHidCommand { + Ping = 0x01, + Msg = 0x03, + // Lock is optional and may be used in the future. + Lock = 0x04, + Init = 0x06, + Wink = 0x08, + Cbor = 0x10, + Cancel = 0x11, + Keepalive = 0x3B, + Error = 0x3F, + // The vendor range starts here, going from 0x40 to 0x7F. +} + +impl From for CtapHidCommand { + fn from(cmd: u8) -> Self { + match cmd { + x if x == CtapHidCommand::Ping as u8 => CtapHidCommand::Ping, + x if x == CtapHidCommand::Msg as u8 => CtapHidCommand::Msg, + x if x == CtapHidCommand::Lock as u8 => CtapHidCommand::Lock, + x if x == CtapHidCommand::Init as u8 => CtapHidCommand::Init, + x if x == CtapHidCommand::Wink as u8 => CtapHidCommand::Wink, + x if x == CtapHidCommand::Cbor as u8 => CtapHidCommand::Cbor, + x if x == CtapHidCommand::Cancel as u8 => CtapHidCommand::Cancel, + x if x == CtapHidCommand::Keepalive as u8 => CtapHidCommand::Keepalive, + // This includes the actual error code 0x3F. Error is not used for incoming packets in + // the specification, so we can safely reuse it for unknown bytes. + _ => CtapHidCommand::Error, + } + } +} + +/// CTAPHID errors +/// +/// See section 11.2.9.1.6. of FIDO 2.1 (2021-06-15). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CtapHidError { + /// The command in the request is invalid. + InvalidCmd = 0x01, + /// A parameter in the request is invalid. + _InvalidPar = 0x02, + /// The length of a message is too big. + InvalidLen = 0x03, + /// Expected a continuation packet with a specific sequence number, got another sequence number. + /// + /// This error code is also used if we expect a continuation packet, and receive an init + /// packet. We interpreted it as invalid seq number 0. + InvalidSeq = 0x04, + /// This packet arrived after a timeout. + MsgTimeout = 0x05, + /// A packet arrived on one channel while another is busy. + ChannelBusy = 0x06, + /// Command requires channel lock. + _LockRequired = 0x0A, + /// The requested channel ID is invalid. + InvalidChannel = 0x0B, + /// Unspecified error. + _Other = 0x7F, + /// This error is silently ignored. + UnexpectedContinuation, +} + +/// Describes the structure of a parsed HID packet. +/// +/// A packet is either an Init or a Continuation packet. +#[derive(Clone, Debug, PartialEq, Eq)] pub enum ProcessedPacket<'a> { InitPacket { cmd: u8, @@ -51,316 +117,215 @@ pub enum ProcessedPacket<'a> { }, } -// An assembled CTAPHID command. +/// An assembled CTAPHID command. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Message { // Channel ID. pub cid: ChannelID, // Command. - pub cmd: u8, + pub cmd: CtapHidCommand, // Bytes of the message. pub payload: Vec, } +/// A keepalive packet reports the reason why a command does not finish. +#[allow(dead_code)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum KeepaliveStatus { + Processing = 0x01, + UpNeeded = 0x02, +} + +/// Holds all state for receiving and sending HID packets. +/// +/// This includes +/// - state from not fully processed messages, +/// - all allocated channels. +/// +/// To process a packet and receive the response, call `parse_packet`. If you didn't receive any +/// message or preprocessing discarded it, stop. Else process the message further, by handling the +/// commands: +/// +/// - PING (optional) +/// - MSG +/// - WINK +/// - CBOR +/// +/// To get packets to send from your processed message, call `split_message`. Summary: +/// +/// 1. `HidPacket` -> `Option` +/// 2. `Option` -> `Message` +/// 3. `Message` -> `Message` +/// 4. `Message` -> `HidPacketIterator` +/// +/// These steps correspond to: +/// +/// 1. `parse_packet` assembles the message and preprocesses all pure HID commands and errors. +/// 2. If you didn't receive any message or preprocessing discarded it, stop. +/// 3. Handles all CTAP protocol interactions. +/// 4. `split_message` creates packets out of the response message. pub struct CtapHid { assembler: MessageAssembler, - // The specification (version 20190130) only requires unique CIDs ; the allocation algorithm is - // vendor specific. + // The specification only requires unique CIDs, the allocation algorithm is vendor specific. // We allocate them incrementally, that is all `cid` such that 1 <= cid <= allocated_cids are // allocated. // In packets, the ID encoding is Big Endian to match what is used throughout CTAP (with the // u32::to/from_be_bytes methods). + // TODO(kaczmarczyck) We might want to limit or timeout open channels. allocated_cids: usize, - pub wink_permission: TimedPermission, + capabilities: u8, } -#[allow(dead_code)] -pub enum KeepaliveStatus { - Processing, - UpNeeded, -} - -#[allow(dead_code)] -// TODO(kaczmarczyck) disable the warning in the end impl CtapHid { - // CTAP specification (version 20190130) section 8.1.3 + // We implement CTAP 2.1 from 2021-06-15. Please see section + // 11.2. USB Human Interface Device (USB HID) const CHANNEL_RESERVED: ChannelID = [0, 0, 0, 0]; const CHANNEL_BROADCAST: ChannelID = [0xFF, 0xFF, 0xFF, 0xFF]; const TYPE_INIT_BIT: u8 = 0x80; const PACKET_TYPE_MASK: u8 = 0x80; - // CTAP specification (version 20190130) section 8.1.9 - const COMMAND_PING: u8 = 0x01; - const COMMAND_MSG: u8 = 0x03; - const COMMAND_INIT: u8 = 0x06; - const COMMAND_CBOR: u8 = 0x10; - pub const COMMAND_CANCEL: u8 = 0x11; - const COMMAND_KEEPALIVE: u8 = 0x3B; - const COMMAND_ERROR: u8 = 0x3F; - // TODO: optional lock command - const COMMAND_LOCK: u8 = 0x04; - const COMMAND_WINK: u8 = 0x08; - const COMMAND_VENDOR_FIRST: u8 = 0x40; - const COMMAND_VENDOR_LAST: u8 = 0x7F; - - // CTAP specification (version 20190130) section 8.1.9.1.6 - const ERR_INVALID_CMD: u8 = 0x01; - const ERR_INVALID_PAR: u8 = 0x02; - const ERR_INVALID_LEN: u8 = 0x03; - const ERR_INVALID_SEQ: u8 = 0x04; - const ERR_MSG_TIMEOUT: u8 = 0x05; - const ERR_CHANNEL_BUSY: u8 = 0x06; - const ERR_LOCK_REQUIRED: u8 = 0x0A; - const ERR_INVALID_CHANNEL: u8 = 0x0B; - const ERR_OTHER: u8 = 0x7F; - - // CTAP specification (version 20190130) section 8.1.9.1.3 + // See section 11.2.9.1.3. CTAPHID_INIT (0x06). const PROTOCOL_VERSION: u8 = 2; - // The device version number is vendor-defined. const DEVICE_VERSION_MAJOR: u8 = 1; const DEVICE_VERSION_MINOR: u8 = 0; const DEVICE_VERSION_BUILD: u8 = 0; - const CAPABILITY_WINK: u8 = 0x01; - const CAPABILITY_CBOR: u8 = 0x04; - const CAPABILITY_NMSG: u8 = 0x08; - // Capabilitites currently supported by this device. - #[cfg(feature = "with_ctap1")] - const CAPABILITIES: u8 = CtapHid::CAPABILITY_WINK | CtapHid::CAPABILITY_CBOR; - #[cfg(not(feature = "with_ctap1"))] - const CAPABILITIES: u8 = - CtapHid::CAPABILITY_WINK | CtapHid::CAPABILITY_CBOR | CtapHid::CAPABILITY_NMSG; + pub const CAPABILITY_WINK: u8 = 0x01; + pub const CAPABILITY_CBOR: u8 = 0x04; + #[cfg(any(not(feature = "with_ctap1"), feature = "vendor_hid"))] + pub const CAPABILITY_NMSG: u8 = 0x08; // TODO: Is this timeout duration specified? - const TIMEOUT_DURATION: Duration = Duration::from_ms(100); - const WINK_TIMEOUT_DURATION: Duration = Duration::from_ms(5000); + const TIMEOUT_DURATION: Duration = Duration::from_millis(100); - pub fn new() -> CtapHid { + /// Creates a new CTAP HID packet parser. + /// + /// The capabilities passed in are reported to the client in Init. + pub fn new(capabilities: u8) -> CtapHid { CtapHid { assembler: MessageAssembler::new(), allocated_cids: 0, - wink_permission: TimedPermission::waiting(), + capabilities, } } - // Process an incoming USB HID packet, and optionally returns a list of outgoing packets to - // send as a reply. - pub fn process_hid_packet( + /// Parses a packet, and preprocesses some messages and errors. + /// + /// The preprocessed commands are: + /// - INIT + /// - CANCEL + /// - ERROR + /// - Unknown and unexpected commands like KEEPALIVE + /// - LOCK is not implemented and currently treated like an unknown command + /// + /// Commands that may still be processed: + /// - PING + /// - MSG + /// - WINK + /// - CBOR + /// + /// You may ignore PING, it's behaving correctly by default (input == output). + /// Ignoring the others is incorrect behavior. You have to at least replace them with an error + /// message: + /// `CtapHid::error_message(message.cid, CtapHid::ERR_INVALID_CMD)` + pub fn parse_packet( &mut self, + env: &mut impl Env, packet: &HidPacket, - clock_value: ClockValue, - ctap_state: &mut CtapState, - ) -> CTAPHIDResponse - where - R: Rng256, - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { - // TODO: Send COMMAND_KEEPALIVE every 100ms? - log::trace!("now: {}", clock_value.ms()); - match self - .assembler - .parse_packet(packet, Timestamp::::from_clock_value(clock_value)) - { + clock_value: Instant, + ) -> Option { + match self.assembler.parse_packet(env, packet, clock_value) { Ok(Some(message)) => { - #[cfg(feature = "debug_ctap")] - writeln!(&mut Console::new(), "Received message: {:02x?}", message).unwrap(); - - let cid = message.cid; - if !self.has_valid_channel(&message) { - #[cfg(feature = "debug_ctap")] - writeln!(&mut Console::new(), "Invalid channel: {:02x?}", cid).unwrap(); - return CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_CHANNEL)); - } - // If another command arrives, stop winking to prevent accidential button touches. - self.wink_permission = TimedPermission::waiting(); - - match message.cmd { - // CTAP specification (version 20190130) section 8.1.9.1.1 - CtapHid::COMMAND_MSG => { - log::trace!("COMMAND_MSG"); - // If we don't have CTAP1 backward compatibilty, this command in invalid. - #[cfg(not(feature = "with_ctap1"))] - return CtapHid::error_message(cid, CtapHid::ERR_INVALID_CMD); - - #[cfg(feature = "with_ctap1")] - match ctap1::Ctap1Command::process_command( - &message.payload, - ctap_state, - clock_value, - ) { - Ok(payload) => { - log::trace!("command success: {:x?}", payload); - CTAPHIDResponse::StandardCommand(CtapHid::ctap1_success_message(cid, &payload)) - }, - Err(ctap1_status_code) => { - log::trace!("command failure: {:?}", ctap1_status_code); - CTAPHIDResponse::StandardCommand(CtapHid::ctap1_error_message(cid, ctap1_status_code)) - } - } - } - // CTAP specification (version 20190130) section 8.1.9.1.2 - CtapHid::COMMAND_CBOR => { - log::trace!("COMMAND_CBOR"); - // CTAP specification (version 20190130) section 8.1.5.1 - // Each transaction is atomic, so we process the command directly here and - // don't handle any other packet in the meantime. - // TODO: Send keep-alive packets in the meantime. - let response = - ctap_state.process_command(&message.payload, cid, clock_value); - if let Some(iterator) = CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_CBOR, - payload: response, - }) { - CTAPHIDResponse::StandardCommand(iterator) - } else { - // Handle the case of a payload > 7609 bytes. - // Although this shouldn't happen if the FIDO2 commands are implemented - // correctly, we reply with a vendor specific code instead of silently - // ignoring the error. - // - // The error payload that we send instead is 1 <= 7609 bytes, so it is - // safe to unwrap() the result. - CTAPHIDResponse::StandardCommand( - CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_CBOR, - payload: vec![ - Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_TOO_LONG as u8, - ], - }) - .unwrap() - ) - } - } - // CTAP specification (version 20190130) section 8.1.9.1.3 - CtapHid::COMMAND_INIT => { - log::debug!("COMMAND_INIT"); - if message.payload.len() != 8 { - return CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_LEN)); - } - - let new_cid = if cid == CtapHid::CHANNEL_BROADCAST { - // TODO: Prevent allocating 2^32 channels. - self.allocated_cids += 1; - (self.allocated_cids as u32).to_be_bytes() - } else { - // Sync the channel and discard the current transaction. - cid - }; - - let mut payload = vec![0; 17]; - payload[..8].copy_from_slice(&message.payload); - payload[8..12].copy_from_slice(&new_cid); - payload[12] = CtapHid::PROTOCOL_VERSION; - payload[13] = CtapHid::DEVICE_VERSION_MAJOR; - payload[14] = CtapHid::DEVICE_VERSION_MINOR; - payload[15] = CtapHid::DEVICE_VERSION_BUILD; - payload[16] = CtapHid::CAPABILITIES; - - // This unwrap is safe because the payload length is 17 <= 7609 bytes. - CTAPHIDResponse::StandardCommand( - CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_INIT, - payload, - }) - .unwrap() - ) - } - // CTAP specification (version 20190130) section 8.1.9.1.4 - CtapHid::COMMAND_PING => { - log::debug!("COMMAND_PING"); - // Pong the same message. - // This unwrap is safe because if we could parse the incoming message, it's - // payload length must be <= 7609 bytes. - CTAPHIDResponse::StandardCommand(CtapHid::split_message(message).unwrap()) - } - // CTAP specification (version 20190130) section 8.1.9.1.5 - CtapHid::COMMAND_CANCEL => { - log::debug!("COMMAND_CANCEL"); - // Authenticators MUST NOT reply to this message. - // CANCEL is handled during user presence checks in main. - CTAPHIDResponse::StandardCommand(HidPacketIterator::none()) - } - // Optional commands - // CTAP specification (version 20190130) section 8.1.9.2.1 - CtapHid::COMMAND_WINK => { - log::debug!("COMMAND_WINK"); - if !message.payload.is_empty() { - return CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_LEN)); - } - self.wink_permission = - TimedPermission::granted(clock_value, CtapHid::WINK_TIMEOUT_DURATION); - CTAPHIDResponse::StandardCommand( - CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_WINK, - payload: vec![], - }) - .unwrap() - ) - } - // If we encountered a vendor command, return VendorCommand and let upstream code - // deal with it. - CtapHid::COMMAND_VENDOR_FIRST..=CtapHid::COMMAND_VENDOR_LAST => { - CTAPHIDResponse::VendorCommand( - message.cmd, - message.cid, - message.payload, - ) - } - // TODO: implement LOCK - _ => { - log::debug!("ERR_INVALID_CMD"); - // Unknown or unsupported command. - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_CMD)) - } - } + log::debug!("Received message: {:02x?}", message); + self.preprocess_message(message) } Ok(None) => { // Waiting for more packets to assemble the message, nothing to send for now. - CTAPHIDResponse::StandardCommand(HidPacketIterator::none()) + None } Err((cid, error)) => { - if !self.is_allocated_channel(cid) - && error != receive::Error::UnexpectedContinuation - { - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_CHANNEL)) + if matches!(error, CtapHidError::UnexpectedContinuation) { + None + } else if !self.is_allocated_channel(cid) { + Some(CtapHid::error_message(cid, CtapHidError::InvalidChannel)) } else { - match error { - receive::Error::UnexpectedChannel => { - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_CHANNEL_BUSY)) - } - receive::Error::UnexpectedInit => { - // TODO: Should we send another error code in this case? - // Technically, we were expecting a sequence number and got another - // byte, although the command/seqnum bit has higher-level semantics - // than sequence numbers. - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_SEQ)) - } - receive::Error::UnexpectedContinuation => { - // CTAP specification (version 20190130) section 8.1.5.4 - // Spurious continuation packets will be ignored. - CTAPHIDResponse::StandardCommand(HidPacketIterator::none()) - } - receive::Error::UnexpectedSeq => { - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_INVALID_SEQ)) - } - receive::Error::Timeout => { - CTAPHIDResponse::StandardCommand(CtapHid::error_message(cid, CtapHid::ERR_MSG_TIMEOUT)) - } - } + Some(CtapHid::error_message(cid, error)) } } } } + /// Processes HID-only commands of a message and returns an outgoing message if necessary. + /// + /// The preprocessed commands are: + /// - INIT + /// - CANCEL + /// - ERROR + /// - Unknown and unexpected commands like KEEPALIVE + /// - LOCK is not implemented and currently treated like an unknown command + fn preprocess_message(&mut self, message: Message) -> Option { + let cid = message.cid; + if !self.has_valid_channel(&message) { + return Some(CtapHid::error_message(cid, CtapHidError::InvalidChannel)); + } + + match message.cmd { + CtapHidCommand::Msg => Some(message), + CtapHidCommand::Cbor => Some(message), + // CTAP 2.1 from 2021-06-15, section 11.2.9.1.3. + CtapHidCommand::Init => { + if message.payload.len() != 8 { + return Some(CtapHid::error_message(cid, CtapHidError::InvalidLen)); + } + + let new_cid = if cid == CtapHid::CHANNEL_BROADCAST { + // TODO: Prevent allocating 2^32 channels. + self.allocated_cids += 1; + (self.allocated_cids as u32).to_be_bytes() + } else { + // Sync the channel and discard the current transaction. + cid + }; + + let mut payload = vec![0; 17]; + payload[..8].copy_from_slice(&message.payload); + payload[8..12].copy_from_slice(&new_cid); + payload[12] = CtapHid::PROTOCOL_VERSION; + payload[13] = CtapHid::DEVICE_VERSION_MAJOR; + payload[14] = CtapHid::DEVICE_VERSION_MINOR; + payload[15] = CtapHid::DEVICE_VERSION_BUILD; + payload[16] = self.capabilities; + + Some(Message { + cid, + cmd: CtapHidCommand::Init, + payload, + }) + } + // CTAP 2.1 from 2021-06-15, section 11.2.9.1.4. + CtapHidCommand::Ping => { + // Pong the same message. + Some(message) + } + // CTAP 2.1 from 2021-06-15, section 11.2.9.1.5. + CtapHidCommand::Cancel => { + // Authenticators MUST NOT reply to this message. + // CANCEL is handled during user presence checks in main. + None + } + CtapHidCommand::Wink => Some(message), + _ => { + // Unknown or unsupported command. + Some(CtapHid::error_message(cid, CtapHidError::InvalidCmd)) + } + } + } + fn has_valid_channel(&self, message: &Message) -> bool { match message.cid { // Only INIT commands use the broadcast channel. - CtapHid::CHANNEL_BROADCAST => message.cmd == CtapHid::COMMAND_INIT, + CtapHid::CHANNEL_BROADCAST => message.cmd == CtapHidCommand::Init, // Check that the channel is allocated. _ => self.is_allocated_channel(message.cid), } @@ -370,18 +335,17 @@ impl CtapHid { cid != CtapHid::CHANNEL_RESERVED && u32::from_be_bytes(cid) as usize <= self.allocated_cids } - fn error_message(cid: ChannelID, error_code: u8) -> HidPacketIterator { - // This unwrap is safe because the payload length is 1 <= 7609 bytes. - CtapHid::split_message(Message { + pub fn error_message(cid: ChannelID, error_code: CtapHidError) -> Message { + Message { cid, - cmd: CtapHid::COMMAND_ERROR, - payload: vec![error_code], - }) - .unwrap() + cmd: CtapHidCommand::Error, + payload: vec![error_code as u8], + } } - pub fn process_single_packet(packet: &HidPacket) -> (&ChannelID, ProcessedPacket) { - let (cid, rest) = array_refs![packet, 4, 60]; + /// Helper function to parse a raw packet. + pub fn process_single_packet(packet: &HidPacket) -> (ChannelID, ProcessedPacket) { + let (&cid, rest) = array_refs![packet, 4, 60]; if rest[0] & CtapHid::PACKET_TYPE_MASK != 0 { let cmd = rest[0] & !CtapHid::PACKET_TYPE_MASK; let len = (rest[1] as usize) << 8 | (rest[2] as usize); @@ -404,150 +368,71 @@ impl CtapHid { } } - fn split_message(message: Message) -> Option { - #[cfg(feature = "debug_ctap")] - writeln!(&mut Console::new(), "Sending message: {:02x?}", message).unwrap(); - HidPacketIterator::new(message) + /// Splits the message and unwraps the result. + /// + /// Unwrapping handles the case of payload lengths > 7609 bytes. All responses are fixed + /// length, with the exception of: + /// - PING, but here output equals the (validated) input, + /// - CBOR, where long responses are conceivable. + /// + /// Long CBOR responses should not happen, but we might not catch all edge cases, like for + /// example long user names that are part of the output of an assertion. These cases should be + /// correctly handled by the CTAP implementation. It is therefore an internal error from the + /// HID perspective. + pub fn split_message(message: Message) -> HidPacketIterator { + let cid = message.cid; + HidPacketIterator::new(message).unwrap_or_else(|| { + // The error payload is 1 <= 7609 bytes, so unwrap() is safe. + HidPacketIterator::new(Message { + cid, + cmd: CtapHidCommand::Cbor, + payload: vec![Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR as u8], + }) + .unwrap() + }) } + /// Generates the HID response packets for a keepalive status. pub fn keepalive(cid: ChannelID, status: KeepaliveStatus) -> HidPacketIterator { - let status_code = match status { - KeepaliveStatus::Processing => 1, - KeepaliveStatus::UpNeeded => 2, - }; - // This unwrap is safe because the payload length is 1 <= 7609 bytes. CtapHid::split_message(Message { cid, - cmd: CtapHid::COMMAND_KEEPALIVE, - payload: vec![status_code], + cmd: CtapHidCommand::Keepalive, + payload: vec![status as u8], }) - .unwrap() } - #[cfg(feature = "with_ctap1")] - fn ctap1_error_message( - cid: ChannelID, - error_code: ctap1::Ctap1StatusCode, - ) -> HidPacketIterator { - // This unwrap is safe because the payload length is 2 <= 7609 bytes - let code: u16 = error_code.into(); - CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_MSG, - payload: code.to_be_bytes().to_vec(), - }) - .unwrap() - } - - #[cfg(feature = "with_ctap1")] - fn ctap1_success_message(cid: ChannelID, payload: &[u8]) -> HidPacketIterator { - let mut response = payload.to_vec(); - let code: u16 = ctap1::Ctap1StatusCode::SW_SUCCESS.into(); - response.extend_from_slice(&code.to_be_bytes()); - CtapHid::split_message(Message { - cid, - cmd: CtapHid::COMMAND_MSG, - payload: response, - }) - .unwrap() - } - - #[cfg(feature = "with_ctap1")] - fn ctap1_generic_success_message(cid: ChannelID, cmd: u8, payload: &[u8]) -> HidPacketIterator { - let mut response = payload.to_vec(); - let code: u16 = ctap1::Ctap1StatusCode::SW_SUCCESS.into(); - response.extend_from_slice(&code.to_be_bytes()); - CtapHid::split_message(Message { - cid, - cmd: cmd, - payload: response, - }) - .unwrap() + #[cfg(test)] + pub fn new_initialized() -> (CtapHid, ChannelID) { + ( + CtapHid { + assembler: MessageAssembler::new(), + allocated_cids: 1, + capabilities: 0x0D, + }, + [0x00, 0x00, 0x00, 0x01], + ) } } #[cfg(test)] mod test { use super::*; - use ctap_crypto::rng256::ThreadRng256; - - const CLOCK_FREQUENCY_HZ: usize = 32768; - // Except for tests for timeouts (done in ctap1.rs), transactions are time independant. - const DUMMY_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); - const DUMMY_TIMESTAMP: Timestamp = Timestamp::from_ms(0); - - fn process_messages( - ctap_hid: &mut CtapHid, - ctap_state: &mut CtapState, - request: Vec, - ) -> Option> - where - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { - let mut result = Vec::new(); - let mut assembler_reply = MessageAssembler::new(); - for msg_request in request { - for pkt_request in HidPacketIterator::new(msg_request).unwrap() { - for pkt_reply in - match ctap_hid.process_hid_packet(&pkt_request, DUMMY_CLOCK_VALUE, ctap_state) { - CTAPHIDResponse::StandardCommand(iter) => iter, - CTAPHIDResponse::VendorCommand(_, _, _) => panic!("expected StandardCommand, not VendorCommand") - } - { - match assembler_reply.parse_packet(&pkt_reply, DUMMY_TIMESTAMP) { - Ok(Some(message)) => result.push(message), - Ok(None) => (), - Err(_) => return None, - } - } - } - } - Some(result) - } - - fn cid_from_init( - ctap_hid: &mut CtapHid, - ctap_state: &mut CtapState, - ) -> ChannelID - where - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, - { - let nonce = vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]; - let reply = process_messages( - ctap_hid, - ctap_state, - vec![Message { - cid: CtapHid::CHANNEL_BROADCAST, - cmd: CtapHid::COMMAND_INIT, - payload: nonce.clone(), - }], - ); - - let mut cid_in_payload: ChannelID = Default::default(); - if let Some(messages) = reply { - assert_eq!(messages.len(), 1); - assert!(messages[0].payload.len() >= 12); - assert_eq!(nonce, &messages[0].payload[..8]); - cid_in_payload.copy_from_slice(&messages[0].payload[8..12]); - } else { - panic!("The init process was not successful to generate a valid channel ID.") - } - cid_in_payload - } + use crate::env::test::TestEnv; #[test] fn test_split_assemble() { + let mut env = TestEnv::new(); for payload_len in 0..7609 { let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x00, + cmd: CtapHidCommand::Cbor, payload: vec![0xFF; payload_len], }; let mut messages = Vec::new(); let mut assembler = MessageAssembler::new(); for packet in HidPacketIterator::new(message.clone()).unwrap() { - match assembler.parse_packet(&packet, DUMMY_TIMESTAMP) { + match assembler.parse_packet(&mut env, &packet, Instant::new(0)) { Ok(Some(msg)) => messages.push(msg), Ok(None) => (), Err(_) => panic!("Couldn't assemble packet: {:02x?}", &packet as &[u8]), @@ -560,82 +445,46 @@ mod test { #[test] fn test_spurious_continuation_packet() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - let mut ctap_hid = CtapHid::new(); - + let mut env = TestEnv::new(); + let mut ctap_hid = CtapHid::new(0x0D); let mut packet = [0x00; 64]; packet[0..7].copy_from_slice(&[0xC1, 0xC1, 0xC1, 0xC1, 0x00, 0x51, 0x51]); - let mut assembler_reply = MessageAssembler::new(); - let reply = match ctap_hid.process_hid_packet(&packet, DUMMY_CLOCK_VALUE, &mut ctap_state) { - CTAPHIDResponse::StandardCommand(iter) => iter, - CTAPHIDResponse::VendorCommand(_, _, _) => panic!("expected StandardCommand, not VendorCommand") - }; - - for pkt_reply in reply { - // Continuation packets are silently ignored. - assert_eq!( - assembler_reply - .parse_packet(&pkt_reply, DUMMY_TIMESTAMP) - .unwrap(), - None - ); - } + // Continuation packets are silently ignored. + assert_eq!( + ctap_hid.parse_packet(&mut env, &packet, Instant::new(0)), + None + ); } #[test] fn test_command_init() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - let mut ctap_hid = CtapHid::new(); - - let reply = process_messages( - &mut ctap_hid, - &mut ctap_state, - vec![Message { - cid: CtapHid::CHANNEL_BROADCAST, - cmd: CtapHid::COMMAND_INIT, - payload: vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], - }], - ); - + let mut ctap_hid = CtapHid::new(0x0D); + let init_message = Message { + cid: CtapHid::CHANNEL_BROADCAST, + cmd: CtapHidCommand::Init, + payload: vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], + }; + let reply = ctap_hid.preprocess_message(init_message); assert_eq!( reply, - Some(vec![Message { + Some(Message { cid: CtapHid::CHANNEL_BROADCAST, - cmd: CtapHid::COMMAND_INIT, + cmd: CtapHidCommand::Init, payload: vec![ - 0x12, // Nonce - 0x34, - 0x56, - 0x78, - 0x9A, - 0xBC, - 0xDE, - 0xF0, - 0x00, // Allocated CID - 0x00, - 0x00, - 0x01, + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // Nonce + 0x00, 0x00, 0x00, 0x01, // Allocated CID 0x02, // Protocol version - 0x01, // Device version - 0x00, - 0x00, - CtapHid::CAPABILITIES + 0x01, 0x00, 0x00, // Device version + 0x0D, // Capabilities ] - }]) + }) ); } #[test] fn test_command_init_for_sync() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - let mut ctap_hid = CtapHid::new(); - let cid = cid_from_init(&mut ctap_hid, &mut ctap_state); + let mut env = TestEnv::new(); + let (mut ctap_hid, cid) = CtapHid::new_initialized(); // Ping packet with a length longer than one packet. let mut packet1 = [0x51; 64]; @@ -647,76 +496,143 @@ mod test { packet2[4..15].copy_from_slice(&[ 0x86, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, ]); - let mut result = Vec::new(); - let mut assembler_reply = MessageAssembler::new(); - for pkt_request in &[packet1, packet2] { - for pkt_reply in - match ctap_hid.process_hid_packet(&pkt_request, DUMMY_CLOCK_VALUE, &mut ctap_state) { - CTAPHIDResponse::StandardCommand(iter) => iter, - CTAPHIDResponse::VendorCommand(_, _, _) => panic!("expected StandardCommand, not VendorCommand") - } - { - if let Some(message) = assembler_reply - .parse_packet(&pkt_reply, DUMMY_TIMESTAMP) - .unwrap() - { - result.push(message); - } - } - } assert_eq!( - result, - vec![Message { + ctap_hid.parse_packet(&mut env, &packet1, Instant::new(0)), + None + ); + assert_eq!( + ctap_hid.parse_packet(&mut env, &packet2, Instant::new(0)), + Some(Message { cid, - cmd: CtapHid::COMMAND_INIT, + cmd: CtapHidCommand::Init, payload: vec![ - 0x12, // Nonce - 0x34, - 0x56, - 0x78, - 0x9A, - 0xBC, - 0xDE, - 0xF0, - cid[0], // Allocated CID - cid[1], - cid[2], - cid[3], - 0x02, // Protocol version - 0x01, // Device version - 0x00, - 0x00, - CtapHid::CAPABILITIES + 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // Nonce + cid[0], cid[1], cid[2], cid[3], // Allocated CID + 0x02, // Protocol version + 0x01, 0x00, 0x00, // Device version + 0x0D, // Capabilities ] - }] + }) ); } #[test] fn test_command_ping() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - let mut ctap_hid = CtapHid::new(); - let cid = cid_from_init(&mut ctap_hid, &mut ctap_state); - - let reply = process_messages( - &mut ctap_hid, - &mut ctap_state, - vec![Message { - cid, - cmd: CtapHid::COMMAND_PING, - payload: vec![0x99, 0x99], - }], - ); + let mut env = TestEnv::new(); + let (mut ctap_hid, cid) = CtapHid::new_initialized(); + let mut ping_packet = [0x00; 64]; + ping_packet[..4].copy_from_slice(&cid); + ping_packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]); assert_eq!( - reply, - Some(vec![Message { + ctap_hid.parse_packet(&mut env, &ping_packet, Instant::new(0)), + Some(Message { cid, - cmd: CtapHid::COMMAND_PING, + cmd: CtapHidCommand::Ping, payload: vec![0x99, 0x99] - }]) + }) + ); + } + + #[test] + fn test_command_cancel() { + let mut env = TestEnv::new(); + let (mut ctap_hid, cid) = CtapHid::new_initialized(); + + let mut cancel_packet = [0x00; 64]; + cancel_packet[..4].copy_from_slice(&cid); + cancel_packet[4..7].copy_from_slice(&[0x91, 0x00, 0x00]); + + let response = ctap_hid.parse_packet(&mut env, &cancel_packet, Instant::new(0)); + assert_eq!(response, None); + } + + #[test] + fn test_split_message() { + let message = Message { + cid: [0x12, 0x34, 0x56, 0x78], + cmd: CtapHidCommand::Ping, + payload: vec![0x99, 0x99], + }; + let mut response = CtapHid::split_message(message); + let mut expected_packet = [0x00; 64]; + expected_packet[..9] + .copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x02, 0x99, 0x99]); + assert_eq!(response.next(), Some(expected_packet)); + assert_eq!(response.next(), None); + } + + #[test] + fn test_split_message_too_large() { + let payload = vec![0xFF; 7609 + 1]; + let message = Message { + cid: [0x12, 0x34, 0x56, 0x78], + cmd: CtapHidCommand::Cbor, + payload, + }; + let mut response = CtapHid::split_message(message); + let mut expected_packet = [0x00; 64]; + expected_packet[..8].copy_from_slice(&[0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x01, 0xF2]); + assert_eq!(response.next(), Some(expected_packet)); + assert_eq!(response.next(), None); + } + + #[test] + fn test_keepalive() { + for &status in [KeepaliveStatus::Processing, KeepaliveStatus::UpNeeded].iter() { + let cid = [0x12, 0x34, 0x56, 0x78]; + let mut response = CtapHid::keepalive(cid, status); + let mut expected_packet = [0x00; 64]; + expected_packet[..8].copy_from_slice(&[ + 0x12, + 0x34, + 0x56, + 0x78, + 0xBB, + 0x00, + 0x01, + status as u8, + ]); + assert_eq!(response.next(), Some(expected_packet)); + assert_eq!(response.next(), None); + } + } + + #[test] + fn test_process_single_packet() { + let cid = [0x12, 0x34, 0x56, 0x78]; + let mut packet = [0x00; 64]; + packet[..4].copy_from_slice(&cid); + packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]); + let (processed_cid, processed_packet) = CtapHid::process_single_packet(&packet); + assert_eq!(processed_cid, cid); + let expected_packet = ProcessedPacket::InitPacket { + cmd: CtapHidCommand::Ping as u8, + len: 2, + data: array_ref!(packet, 7, 57), + }; + assert_eq!(processed_packet, expected_packet); + } + + #[test] + fn test_from_ctap_hid_command() { + // 0x3E is unassigned. + assert_eq!(CtapHidCommand::from(0x3E), CtapHidCommand::Error); + for command in CtapHidCommand::into_enum_iter() { + assert_eq!(CtapHidCommand::from(command as u8), command); + } + } + + #[test] + fn test_error_message() { + let cid = [0x12, 0x34, 0x56, 0x78]; + assert_eq!( + CtapHid::error_message(cid, CtapHidError::InvalidCmd), + Message { + cid, + cmd: CtapHidCommand::Error, + payload: vec![0x01], + } ); } } diff --git a/apps/vault/src/ctap/hid/receive.rs b/apps/vault/src/ctap/hid/receive.rs index 8fea20ce0..429343f0e 100644 --- a/apps/vault/src/ctap/hid/receive.rs +++ b/apps/vault/src/ctap/hid/receive.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,19 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::{ChannelID, CtapHid, HidPacket, Message, ProcessedPacket}; +use super::{ + ChannelID, CtapHid, CtapHidCommand, CtapHidError, HidPacket, Message, ProcessedPacket, +}; +use crate::api::customization::Customization; +use std::time::Instant; +use crate::env::Env; use std::vec::Vec; use core::mem::swap; -use crate::shims::Timestamp; -// A structure to assemble CTAPHID commands from a series of incoming USB HID packets. +/// A structure to assemble CTAPHID commands from a series of incoming USB HID packets. pub struct MessageAssembler { // Whether this is waiting to receive an initialization packet. idle: bool, // Current channel ID. cid: ChannelID, // Timestamp of the last packet received on the current channel. - last_timestamp: Timestamp, + last_timestamp: Instant, // Current command. cmd: u8, // Sequence number expected for the next packet. @@ -35,26 +39,12 @@ pub struct MessageAssembler { payload: Vec, } -#[derive(PartialEq, Debug)] -pub enum Error { - // Expected a continuation packet on a specific channel, got a packet on another channel. - UnexpectedChannel, - // Expected a continuation packet, got an init packet. - UnexpectedInit, - // Expected an init packet, got a continuation packet. - UnexpectedContinuation, - // Expected a continuation packet with a specific sequence number, got another sequence number. - UnexpectedSeq, - // This packet arrived after a timeout. - Timeout, -} - impl MessageAssembler { pub fn new() -> MessageAssembler { MessageAssembler { idle: true, cid: [0, 0, 0, 0], - last_timestamp: Timestamp::from_ms(0), + last_timestamp: Instant::now(), cmd: 0, seq: 0, remaining_payload_len: 0, @@ -64,10 +54,10 @@ impl MessageAssembler { // Resets the message assembler to the idle state. // The caller can reset the assembler for example due to a timeout. - pub fn reset(&mut self) { + fn reset(&mut self) { self.idle = true; self.cid = [0, 0, 0, 0]; - self.last_timestamp = Timestamp::from_ms(0); + self.last_timestamp = Instant::now(); self.cmd = 0; self.seq = 0; self.remaining_payload_len = 0; @@ -83,16 +73,15 @@ impl MessageAssembler { // packet was received. pub fn parse_packet( &mut self, + env: &mut impl Env, packet: &HidPacket, - timestamp: Timestamp, - ) -> Result, (ChannelID, Error)> { + timestamp: Instant, + ) -> Result, (ChannelID, CtapHidError)> { // TODO: Support non-full-speed devices (i.e. packet len != 64)? This isn't recommended by // section 8.8.1 - let (cid, processed_packet) = CtapHid::process_single_packet(&packet); - log::trace!("parsed cid: {:x?}", cid); + let (cid, processed_packet) = CtapHid::process_single_packet(packet); - if !self.idle && timestamp - self.last_timestamp >= CtapHid::TIMEOUT_DURATION { - log::debug!("parse timeout"); + if !self.idle && timestamp >= self.last_timestamp + CtapHid::TIMEOUT_DURATION { // The current channel timed out. // Save the channel ID and reset the state. let current_cid = self.cid; @@ -100,8 +89,8 @@ impl MessageAssembler { // If the packet is from the timed-out channel, send back a timeout error. // Otherwise, proceed with processing the packet. - if *cid == current_cid { - return Err((*cid, Error::Timeout)); + if cid == current_cid { + return Err((cid, CtapHidError::MsgTimeout)); } } @@ -109,14 +98,12 @@ impl MessageAssembler { // Expecting an initialization packet. match processed_packet { ProcessedPacket::InitPacket { cmd, len, data } => { - log::trace!("init packet cmd: {:x}", cmd); - Ok(self.accept_init_packet(*cid, cmd, len, data, timestamp)) + self.parse_init_packet(env, cid, cmd, len, data, timestamp) } ProcessedPacket::ContinuationPacket { .. } => { - log::debug!("spurious continuation"); // CTAP specification (version 20190130) section 8.1.5.4 // Spurious continuation packets will be ignored. - Err((*cid, Error::UnexpectedContinuation)) + Err((cid, CtapHidError::UnexpectedContinuation)) } } } else { @@ -124,28 +111,25 @@ impl MessageAssembler { // CTAP specification (version 20190130) section 8.1.5.1 // Reject packets from other channels. - if *cid != self.cid { - log::debug!("unexpected channel"); - return Err((*cid, Error::UnexpectedChannel)); + if cid != self.cid { + return Err((cid, CtapHidError::ChannelBusy)); } match processed_packet { // Unexpected initialization packet. ProcessedPacket::InitPacket { cmd, len, data } => { - log::debug!("unexpected init"); self.reset(); - if cmd == CtapHid::COMMAND_INIT { - Ok(self.accept_init_packet(*cid, cmd, len, data, timestamp)) + if cmd == CtapHidCommand::Init as u8 { + self.parse_init_packet(env, cid, cmd, len, data, timestamp) } else { - Err((*cid, Error::UnexpectedInit)) + Err((cid, CtapHidError::InvalidSeq)) } } ProcessedPacket::ContinuationPacket { seq, data } => { - log::trace!("continuation"); if seq != self.seq { // Reject packets with the wrong sequence number. self.reset(); - Err((*cid, Error::UnexpectedSeq)) + Err((cid, CtapHidError::InvalidSeq)) } else { // Update the last timestamp. self.last_timestamp = timestamp; @@ -158,24 +142,26 @@ impl MessageAssembler { } } - fn accept_init_packet( + fn parse_init_packet( &mut self, + env: &mut impl Env, cid: ChannelID, cmd: u8, len: usize, data: &[u8], - timestamp: Timestamp, - ) -> Option { - // TODO: Should invalid commands/payload lengths be rejected early, i.e. as soon as the - // initialization packet is received, or should we build a message and then catch the - // error? - // The specification (version 20190130) isn't clear on this point. + timestamp: Instant, + ) -> Result, (ChannelID, CtapHidError)> { + // Reject invalid lengths early to reduce the risk of running out of memory. + // TODO: also reject invalid commands early? + if len > env.customization().max_msg_size() { + return Err((cid, CtapHidError::InvalidLen)); + } self.cid = cid; self.last_timestamp = timestamp; self.cmd = cmd; self.seq = 0; self.remaining_payload_len = len; - self.append_payload(data) + Ok(self.append_payload(data)) } fn append_payload(&mut self, data: &[u8]) -> Option { @@ -192,7 +178,7 @@ impl MessageAssembler { swap(&mut self.payload, &mut payload); Some(Message { cid: self.cid, - cmd: self.cmd, + cmd: CtapHidCommand::from(self.cmd), payload, }) } @@ -201,12 +187,11 @@ impl MessageAssembler { #[cfg(test)] mod test { - use super::*; - use crate::shims::Duration; + use crate::ctap::hid::CtapHid; + use crate::env::test::TestEnv; + use std::time::Duration; - // Except for tests that exercise timeouts, all packets are synchronized at the same dummy - // timestamp. - const DUMMY_TIMESTAMP: Timestamp = Timestamp::from_ms(0); + use super::*; fn byte_extend(bytes: &[u8], padding: u8) -> HidPacket { let len = bytes.len(); @@ -225,15 +210,19 @@ mod test { #[test] fn test_empty_payload() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); + // Except for tests that exercise timeouts, all packets are synchronized at the same dummy + // timestamp. assert_eq!( assembler.parse_packet( - &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x80]), - DUMMY_TIMESTAMP + &mut env, + &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x90]), + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x00, + cmd: CtapHidCommand::Cbor, payload: vec![] })) ); @@ -241,15 +230,17 @@ mod test { #[test] fn test_one_packet() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( - &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x80, 0x00, 0x10]), - DUMMY_TIMESTAMP + &mut env, + &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x10]), + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x00, + cmd: CtapHidCommand::Cbor, payload: vec![0x00; 0x10] })) ); @@ -257,18 +248,20 @@ mod test { #[test] fn test_nonzero_padding() { + let mut env = TestEnv::new(); // CTAP specification (version 20190130) section 8.1.4 // It is written that "Unused bytes SHOULD be set to zero", so we test that non-zero // padding is accepted as well. let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( - &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x80, 0x00, 0x10], 0xFF), - DUMMY_TIMESTAMP + &mut env, + &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x10], 0xFF), + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x00, + cmd: CtapHidCommand::Cbor, payload: vec![0xFF; 0x10] })) ); @@ -276,22 +269,25 @@ mod test { #[test] fn test_two_packets() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x40]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x00]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x01, + cmd: CtapHidCommand::Ping, payload: vec![0x00; 0x40] })) ); @@ -299,29 +295,33 @@ mod test { #[test] fn test_three_packets() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x80]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x00]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x01]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x01, + cmd: CtapHidCommand::Ping, payload: vec![0x00; 0x80] })) ); @@ -329,31 +329,35 @@ mod test { #[test] fn test_max_packets() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x1D, 0xB9]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); for seq in 0..0x7F { assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, seq]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); } assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x7F]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x01, + cmd: CtapHidCommand::Ping, payload: vec![0x00; 0x1DB9] })) ); @@ -361,31 +365,38 @@ mod test { #[test] fn test_multiple_messages() { + let mut env = TestEnv::new(); // Check that after yielding a message, the assembler is ready to process new messages. let mut assembler = MessageAssembler::new(); for i in 0..10 { // Introduce some variability in the messages. - let cmd = 2 * i; + let cmd = CtapHidCommand::from(i + 1); let byte = 3 * i; assert_eq!( assembler.parse_packet( - &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x80 | cmd, 0x00, 0x80], byte), - DUMMY_TIMESTAMP + &mut env, + &byte_extend( + &[0x12, 0x34, 0x56, 0x78, 0x80 | cmd as u8, 0x00, 0x80], + byte + ), + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x00], byte), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x01], byte), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], @@ -398,32 +409,36 @@ mod test { #[test] fn test_channel_switch() { + let mut env = TestEnv::new(); // Check that the assembler can process messages from multiple channels, sequentially. let mut assembler = MessageAssembler::new(); for i in 0..10 { // Introduce some variability in the messages. let cid = 0x78 + i; - let cmd = 2 * i; + let cmd = CtapHidCommand::from(i + 1); let byte = 3 * i; assert_eq!( assembler.parse_packet( - &byte_extend(&[0x12, 0x34, 0x56, cid, 0x80 | cmd, 0x00, 0x80], byte), - DUMMY_TIMESTAMP + &mut env, + &byte_extend(&[0x12, 0x34, 0x56, cid, 0x80 | cmd as u8, 0x00, 0x80], byte), + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &byte_extend(&[0x12, 0x34, 0x56, cid, 0x00], byte), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &byte_extend(&[0x12, 0x34, 0x56, cid, 0x01], byte), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, cid], @@ -436,36 +451,41 @@ mod test { #[test] fn test_unexpected_channel() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x40]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); // Check that many sorts of packets on another channel are ignored. - for cmd in 0..=0xFF { + for i in 0..=0xFF { + let cmd = CtapHidCommand::from(i); for byte in 0..=0xFF { assert_eq!( assembler.parse_packet( - &byte_extend(&[0x12, 0x34, 0x56, 0x9A, cmd, 0x00], byte), - DUMMY_TIMESTAMP + &mut env, + &byte_extend(&[0x12, 0x34, 0x56, 0x9A, cmd as u8, 0x00], byte), + Instant::new(0) ), - Err(([0x12, 0x34, 0x56, 0x9A], Error::UnexpectedChannel)) + Err(([0x12, 0x34, 0x56, 0x9A], CtapHidError::ChannelBusy)) ); } } assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x00]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x01, + cmd: CtapHidCommand::Ping, payload: vec![0x00; 0x40] })) ); @@ -473,6 +493,7 @@ mod test { #[test] fn test_spurious_continuation_packets() { + let mut env = TestEnv::new(); // CTAP specification (version 20190130) section 8.1.5.4 // Spurious continuation packets appearing without a prior initialization packet will be // ignored. @@ -482,12 +503,13 @@ mod test { let byte = 2 * i; assert_eq!( assembler.parse_packet( - &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x80, 0x00, 0x10], byte), - DUMMY_TIMESTAMP + &mut env, + &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x10], byte), + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x00, + cmd: CtapHidCommand::Ping, payload: vec![byte; 0x10] })) ); @@ -496,98 +518,121 @@ mod test { let seq = i; assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, seq]), - DUMMY_TIMESTAMP + Instant::new(0) ), - Err(([0x12, 0x34, 0x56, 0x78], Error::UnexpectedContinuation)) + Err(( + [0x12, 0x34, 0x56, 0x78], + CtapHidError::UnexpectedContinuation + )) ); } } #[test] fn test_unexpected_init() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x40]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x80]), - DUMMY_TIMESTAMP + Instant::new(0) ), - Err(([0x12, 0x34, 0x56, 0x78], Error::UnexpectedInit)) + Err(([0x12, 0x34, 0x56, 0x78], CtapHidError::InvalidSeq)) ); } #[test] fn test_unexpected_seq() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x40]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x01]), - DUMMY_TIMESTAMP + Instant::new(0) ), - Err(([0x12, 0x34, 0x56, 0x78], Error::UnexpectedSeq)) + Err(([0x12, 0x34, 0x56, 0x78], CtapHidError::InvalidSeq)) ); } #[test] fn test_timed_out_packet() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x00, 0x40]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x00]), - DUMMY_TIMESTAMP + CtapHid::TIMEOUT_DURATION + Instant::new(0) + CtapHid::TIMEOUT_DURATION ), - Err(([0x12, 0x34, 0x56, 0x78], Error::Timeout)) + Err(([0x12, 0x34, 0x56, 0x78], CtapHidError::MsgTimeout)) ); } #[test] fn test_just_in_time_packets() { - let mut timestamp = DUMMY_TIMESTAMP; + let mut env = TestEnv::new(); + let mut timestamp: Instant = Instant::new(0); // Delay between each packet is just below the threshold. - let delay = CtapHid::TIMEOUT_DURATION - Duration::from_ms(1); + let delay = CtapHid::TIMEOUT_DURATION - Duration::from_millis(1_u32); let mut assembler = MessageAssembler::new(); assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x1D, 0xB9]), timestamp ), Ok(None) ); for seq in 0..0x7F { - timestamp += delay; + timestamp = timestamp + delay; assert_eq!( - assembler.parse_packet(&zero_extend(&[0x12, 0x34, 0x56, 0x78, seq]), timestamp), + assembler.parse_packet( + &mut env, + &zero_extend(&[0x12, 0x34, 0x56, 0x78, seq]), + timestamp + ), Ok(None) ); } - timestamp += delay; + timestamp = timestamp + delay; assert_eq!( - assembler.parse_packet(&zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x7F]), timestamp), + assembler.parse_packet( + &mut env, + &zero_extend(&[0x12, 0x34, 0x56, 0x78, 0x7F]), + timestamp + ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x01, + cmd: CtapHidCommand::Ping, payload: vec![0x00; 0x1DB9] })) ); @@ -595,27 +640,30 @@ mod test { #[test] fn test_init_sync() { + let mut env = TestEnv::new(); let mut assembler = MessageAssembler::new(); // Ping packet with a length longer than one packet. assert_eq!( assembler.parse_packet( + &mut env, &byte_extend(&[0x12, 0x34, 0x56, 0x78, 0x81, 0x02, 0x00], 0x51), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(None) ); // Init packet on the same channel. assert_eq!( assembler.parse_packet( + &mut env, &zero_extend(&[ 0x12, 0x34, 0x56, 0x78, 0x86, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 ]), - DUMMY_TIMESTAMP + Instant::new(0) ), Ok(Some(Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x06, + cmd: CtapHidCommand::Init, payload: vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0] })) ); diff --git a/apps/vault/src/ctap/hid/send.rs b/apps/vault/src/ctap/hid/send.rs index 7348bc70f..afe49ecbd 100644 --- a/apps/vault/src/ctap/hid/send.rs +++ b/apps/vault/src/ctap/hid/send.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,16 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::{CtapHid, HidPacket, Message, ChannelID}; - -pub enum CTAPHIDResponse { - /// Standard-compliant command processing result. - StandardCommand(HidPacketIterator), - - /// Vendor-specific command processing result. - VendorCommand(u8, ChannelID, Vec), -} +use super::{CtapHid, HidPacket, Message}; +/// Iterator for HID packets. +/// +/// The `new` constructor splits the CTAP `Message` into `HidPacket`s for sending over USB. pub struct HidPacketIterator(Option); impl HidPacketIterator { @@ -37,6 +32,14 @@ impl HidPacketIterator { pub fn none() -> HidPacketIterator { HidPacketIterator(None) } + + pub fn has_data(&self) -> bool { + if let Some(ms) = &self.0 { + ms.finished() + } else { + false + } + } } impl Iterator for HidPacketIterator { @@ -50,7 +53,7 @@ impl Iterator for HidPacketIterator { } } -pub struct MessageSplitter { +struct MessageSplitter { message: Message, packet: HidPacket, seq: Option, @@ -58,8 +61,9 @@ pub struct MessageSplitter { } impl MessageSplitter { - // Try to split this message into an iterator of HID packets. This fails if the message is too - // long to fit into a sequence of HID packets (which is limited to 7609 bytes). + /// Try to split this message into an iterator of HID packets. + /// + /// This fails if the message is too long to fit into a sequence of HID packets. pub fn new(message: Message) -> Option { if message.payload.len() > 7609 { None @@ -77,9 +81,10 @@ impl MessageSplitter { } } - // Copy as many bytes as possible from data to dst, and return how many bytes are copied. - // Contrary to copy_from_slice, this doesn't require slices of the same length. - // All unused bytes in dst are set to zero, as if the data was padded with zeros to match. + /// Copy as many bytes as possible from data to dst, and return how many bytes are copied. + /// + /// Contrary to copy_from_slice, this doesn't require slices of the same length. + /// All unused bytes in dst are set to zero, as if the data was padded with zeros to match. fn consume_data(dst: &mut [u8], data: &[u8]) -> usize { let dst_len = dst.len(); let data_len = data.len(); @@ -97,6 +102,15 @@ impl MessageSplitter { dst_len } } + + // Is there more data to iterate over? + fn finished(&self) -> bool { + let payload_len = self.message.payload.len(); + match self.seq { + None => true, + Some(_) => self.i < payload_len, + } + } } impl Iterator for MessageSplitter { @@ -107,7 +121,7 @@ impl Iterator for MessageSplitter { match self.seq { None => { // First, send an initialization packet. - self.packet[4] = self.message.cmd | CtapHid::TYPE_INIT_BIT; + self.packet[4] = self.message.cmd as u8 | CtapHid::TYPE_INIT_BIT; self.packet[5] = (payload_len >> 8) as u8; self.packet[6] = payload_len as u8; @@ -136,6 +150,7 @@ impl Iterator for MessageSplitter { #[cfg(test)] mod test { + use super::super::CtapHidCommand; use super::*; fn assert_packet_output_equality(message: Message, expected_packets: Vec) { @@ -150,11 +165,11 @@ mod test { fn test_hid_packet_iterator_single_packet() { let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x4C, + cmd: CtapHidCommand::Cbor, payload: vec![0xAA, 0xBB], }; let expected_packets: Vec = vec![[ - 0x12, 0x34, 0x56, 0x78, 0xCC, 0x00, 0x02, 0xAA, 0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x02, 0xAA, 0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -167,11 +182,11 @@ mod test { fn test_hid_packet_iterator_big_single_packet() { let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x4C, + cmd: CtapHidCommand::Cbor, payload: vec![0xAA; 64 - 7], }; let expected_packets: Vec = vec![[ - 0x12, 0x34, 0x56, 0x78, 0xCC, 0x00, 0x39, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, + 0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x39, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, @@ -184,12 +199,12 @@ mod test { fn test_hid_packet_iterator_two_packets() { let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x4C, + cmd: CtapHidCommand::Cbor, payload: vec![0xAA; 64 - 7 + 1], }; let expected_packets: Vec = vec![ [ - 0x12, 0x34, 0x56, 0x78, 0xCC, 0x00, 0x3A, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, + 0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x3A, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, @@ -212,12 +227,12 @@ mod test { payload.extend(vec![0xBB; 64 - 5]); let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0x4C, + cmd: CtapHidCommand::Cbor, payload, }; let expected_packets: Vec = vec![ [ - 0x12, 0x34, 0x56, 0x78, 0xCC, 0x00, 0x74, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, + 0x12, 0x34, 0x56, 0x78, 0x90, 0x00, 0x74, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, @@ -235,7 +250,6 @@ mod test { } #[test] - #[allow(clippy::eq_op)] fn test_hid_packet_iterator_max_packets() { let mut payload = vec![0xFF; 64 - 7]; for i in 0..128 { @@ -243,18 +257,16 @@ mod test { } // Sanity check for the length of the payload. - assert_eq!((64 - 7) + 128 * (64 - 5), 0x1db9); - assert_eq!(7609, 0x1db9); assert_eq!(payload.len(), 0x1db9); let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0xAB, + cmd: CtapHidCommand::Msg, payload, }; - let mut expected_packets = vec![[ - 0x12, 0x34, 0x56, 0x78, 0xAB, 0x1D, 0xB9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + let mut expected_packets: Vec = vec![[ + 0x12, 0x34, 0x56, 0x78, 0x83, 0x1D, 0xB9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -282,7 +294,7 @@ mod test { assert_eq!(payload.len(), 0x1dba); let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0xAB, + cmd: CtapHidCommand::Msg, payload, }; assert!(HidPacketIterator::new(message).is_none()); @@ -294,11 +306,9 @@ mod test { let payload = vec![0xFF; 0x10000]; let message = Message { cid: [0x12, 0x34, 0x56, 0x78], - cmd: 0xAB, + cmd: CtapHidCommand::Msg, payload, }; assert!(HidPacketIterator::new(message).is_none()); } - - // TODO(kaczmarczyck) implement and test limits (maximum bytes and packets) } diff --git a/apps/vault/src/ctap/key_material.rs b/apps/vault/src/ctap/key_material.rs index 907d55620..90568a90d 100644 --- a/apps/vault/src/ctap/key_material.rs +++ b/apps/vault/src/ctap/key_material.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/apps/vault/src/ctap/large_blobs.rs b/apps/vault/src/ctap/large_blobs.rs new file mode 100644 index 000000000..898638a13 --- /dev/null +++ b/apps/vault/src/ctap/large_blobs.rs @@ -0,0 +1,431 @@ +// Copyright 2020-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::client_pin::{ClientPin, PinPermission}; +use super::command::AuthenticatorLargeBlobsParameters; +use super::response::{AuthenticatorLargeBlobsResponse, ResponseData}; +use super::status_code::Ctap2StatusCode; +use crate::api::customization::Customization; +use crate::ctap::storage; +use crate::env::Env; +use std::vec; +use std::vec::Vec; +use byteorder::{ByteOrder, LittleEndian}; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; + +/// The length of the truncated hash that as appended to the large blob data. +const TRUNCATED_HASH_LEN: usize = 16; + +pub struct LargeBlobs { + buffer: Vec, + expected_length: usize, + expected_next_offset: usize, +} + +/// Implements the logic for the AuthenticatorLargeBlobs command and keeps its state. +impl LargeBlobs { + pub fn new() -> LargeBlobs { + LargeBlobs { + buffer: Vec::new(), + expected_length: 0, + expected_next_offset: 0, + } + } + + /// Process the large blob command. + pub fn process_command( + &mut self, + env: &mut impl Env, + client_pin: &mut ClientPin, + large_blobs_params: AuthenticatorLargeBlobsParameters, + ) -> Result { + let AuthenticatorLargeBlobsParameters { + get, + set, + offset, + length, + pin_uv_auth_param, + pin_uv_auth_protocol, + } = large_blobs_params; + + let max_fragment_size = env.customization().max_msg_size() - 64; + + if let Some(get) = get { + if get > max_fragment_size || offset.checked_add(get).is_none() { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_LENGTH); + } + let config = storage::get_large_blob_array(env, offset, get)?; + return Ok(ResponseData::AuthenticatorLargeBlobs(Some( + AuthenticatorLargeBlobsResponse { config }, + ))); + } + + if let Some(mut set) = set { + if set.len() > max_fragment_size { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_LENGTH); + } + if offset == 0 { + self.expected_length = + length.ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)?; + if self.expected_length > env.customization().max_large_blob_array_size() { + return Err(Ctap2StatusCode::CTAP2_ERR_LARGE_BLOB_STORAGE_FULL); + } + self.expected_next_offset = 0; + } + if offset != self.expected_next_offset { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_SEQ); + } + if storage::pin_hash(env)?.is_some() || storage::has_always_uv(env)? { + let pin_uv_auth_param = + pin_uv_auth_param.ok_or(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED)?; + let pin_uv_auth_protocol = + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + let mut large_blob_data = vec![0xFF; 32]; + large_blob_data.extend(&[0x0C, 0x00]); + let mut offset_bytes = [0u8; 4]; + LittleEndian::write_u32(&mut offset_bytes, offset as u32); + large_blob_data.extend(&offset_bytes); + large_blob_data.extend(&Sha256::hash(set.as_slice())); + client_pin.verify_pin_uv_auth_token( + &large_blob_data, + &pin_uv_auth_param, + pin_uv_auth_protocol, + )?; + client_pin.has_permission(PinPermission::LargeBlobWrite)?; + } + if offset + set.len() > self.expected_length { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if offset == 0 { + self.buffer = Vec::with_capacity(self.expected_length); + } + self.buffer.append(&mut set); + self.expected_next_offset = self.buffer.len(); + if self.expected_next_offset == self.expected_length { + self.expected_length = 0; + self.expected_next_offset = 0; + // Must be a positive number. + let buffer_hash_index = self.buffer.len() - TRUNCATED_HASH_LEN; + if Sha256::hash(&self.buffer[..buffer_hash_index])[..TRUNCATED_HASH_LEN] + != self.buffer[buffer_hash_index..] + { + self.buffer = Vec::new(); + return Err(Ctap2StatusCode::CTAP2_ERR_INTEGRITY_FAILURE); + } + storage::commit_large_blob_array(env, &self.buffer)?; + self.buffer = Vec::new(); + } + return Ok(ResponseData::AuthenticatorLargeBlobs(None)); + } + + // This should be unreachable, since the command has either get or set. + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + } +} + +#[cfg(test)] +mod test { + use super::super::data_formats::PinUvAuthProtocol; + use super::super::pin_protocol::authenticate_pin_uv_auth_token; + use super::*; + use crate::env::test::TestEnv; + + #[test] + fn test_process_command_get_empty() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + let large_blob = vec![ + 0x80, 0x76, 0xBE, 0x8B, 0x52, 0x8D, 0x00, 0x75, 0xF7, 0xAA, 0xE9, 0x8D, 0x6F, 0xA5, + 0x7A, 0x6D, 0x3C, + ]; + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: Some(large_blob.len()), + set: None, + offset: 0, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + match large_blobs_response.unwrap() { + ResponseData::AuthenticatorLargeBlobs(Some(response)) => { + assert_eq!(response.config, large_blob); + } + _ => panic!("Invalid response type"), + }; + } + + #[test] + fn test_process_command_commit_and_get() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + const BLOB_LEN: usize = 200; + const DATA_LEN: usize = BLOB_LEN - TRUNCATED_HASH_LEN; + let mut large_blob = vec![0x1B; DATA_LEN]; + large_blob.extend_from_slice(&Sha256::hash(&large_blob[..])[..TRUNCATED_HASH_LEN]); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[..BLOB_LEN / 2].to_vec()), + offset: 0, + length: Some(BLOB_LEN), + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Ok(ResponseData::AuthenticatorLargeBlobs(None)) + ); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[BLOB_LEN / 2..].to_vec()), + offset: BLOB_LEN / 2, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Ok(ResponseData::AuthenticatorLargeBlobs(None)) + ); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: Some(BLOB_LEN), + set: None, + offset: 0, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + match large_blobs_response.unwrap() { + ResponseData::AuthenticatorLargeBlobs(Some(response)) => { + assert_eq!(response.config, large_blob); + } + _ => panic!("Invalid response type"), + }; + } + + #[test] + fn test_process_command_commit_unexpected_offset() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + const BLOB_LEN: usize = 200; + const DATA_LEN: usize = BLOB_LEN - TRUNCATED_HASH_LEN; + let mut large_blob = vec![0x1B; DATA_LEN]; + large_blob.extend_from_slice(&Sha256::hash(&large_blob[..])[..TRUNCATED_HASH_LEN]); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[..BLOB_LEN / 2].to_vec()), + offset: 0, + length: Some(BLOB_LEN), + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Ok(ResponseData::AuthenticatorLargeBlobs(None)) + ); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[BLOB_LEN / 2..].to_vec()), + // The offset is 1 too big. + offset: BLOB_LEN / 2 + 1, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_SEQ), + ); + } + + #[test] + fn test_process_command_commit_unexpected_length() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + const BLOB_LEN: usize = 200; + const DATA_LEN: usize = BLOB_LEN - TRUNCATED_HASH_LEN; + let mut large_blob = vec![0x1B; DATA_LEN]; + large_blob.extend_from_slice(&Sha256::hash(&large_blob[..])[..TRUNCATED_HASH_LEN]); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[..BLOB_LEN / 2].to_vec()), + offset: 0, + // The length is 1 too small. + length: Some(BLOB_LEN - 1), + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Ok(ResponseData::AuthenticatorLargeBlobs(None)) + ); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob[BLOB_LEN / 2..].to_vec()), + offset: BLOB_LEN / 2, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), + ); + } + + #[test] + fn test_process_command_commit_end_offset_overflow() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: Some(1), + set: None, + offset: usize::MAX, + length: None, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + assert_eq!( + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params), + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_LENGTH), + ); + } + + #[test] + fn test_process_command_commit_unexpected_hash() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + let mut large_blobs = LargeBlobs::new(); + + const BLOB_LEN: usize = 20; + // This blob does not have an appropriate hash. + let large_blob = vec![0x1B; BLOB_LEN]; + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob.to_vec()), + offset: 0, + length: Some(BLOB_LEN), + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Err(Ctap2StatusCode::CTAP2_ERR_INTEGRITY_FAILURE), + ); + } + + fn test_helper_process_command_commit_with_pin(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let mut client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + let mut large_blobs = LargeBlobs::new(); + + const BLOB_LEN: usize = 20; + const DATA_LEN: usize = BLOB_LEN - TRUNCATED_HASH_LEN; + let mut large_blob = vec![0x1B; DATA_LEN]; + large_blob.extend_from_slice(&Sha256::hash(&large_blob[..])[..TRUNCATED_HASH_LEN]); + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let mut large_blob_data = vec![0xFF; 32]; + // Command constant and offset bytes. + large_blob_data.extend(&[0x0C, 0x00, 0x00, 0x00, 0x00, 0x00]); + large_blob_data.extend(&Sha256::hash(&large_blob)); + let pin_uv_auth_param = authenticate_pin_uv_auth_token( + &pin_uv_auth_token, + &large_blob_data, + pin_uv_auth_protocol, + ); + + let large_blobs_params = AuthenticatorLargeBlobsParameters { + get: None, + set: Some(large_blob), + offset: 0, + length: Some(BLOB_LEN), + pin_uv_auth_param: Some(pin_uv_auth_param), + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), + }; + let large_blobs_response = + large_blobs.process_command(&mut env, &mut client_pin, large_blobs_params); + assert_eq!( + large_blobs_response, + Ok(ResponseData::AuthenticatorLargeBlobs(None)) + ); + } + + #[test] + fn test_process_command_commit_with_pin_v1() { + test_helper_process_command_commit_with_pin(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_command_commit_with_pin_v2() { + test_helper_process_command_commit_with_pin(PinUvAuthProtocol::V2); + } +} diff --git a/apps/vault/src/ctap/main_hid.rs b/apps/vault/src/ctap/main_hid.rs new file mode 100644 index 000000000..74244f9ed --- /dev/null +++ b/apps/vault/src/ctap/main_hid.rs @@ -0,0 +1,235 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::time::Instant; +#[cfg(feature = "with_ctap1")] +use crate::ctap::ctap1; +#[cfg(feature = "with_ctap1")] +use crate::ctap::hid::ChannelID; +use crate::ctap::hid::{ + CtapHid, CtapHidCommand, CtapHidError, HidPacket, HidPacketIterator, Message, +}; +use crate::ctap::{Channel, CtapState, TimedPermission}; +use crate::env::Env; +use std::time::Duration; + +/// Implements the standard CTAP command processing for HID. +pub struct MainHid { + hid: CtapHid, + wink_permission: TimedPermission, +} + +impl MainHid { + const WINK_TIMEOUT_DURATION: Duration = Duration::from_millis(5000); + + /// Instantiates a HID handler for CTAP1, CTAP2 and Wink. + pub fn new() -> Self { + #[cfg(feature = "with_ctap1")] + let capabilities = CtapHid::CAPABILITY_WINK | CtapHid::CAPABILITY_CBOR; + #[cfg(not(feature = "with_ctap1"))] + let capabilities = + CtapHid::CAPABILITY_WINK | CtapHid::CAPABILITY_CBOR | CtapHid::CAPABILITY_NMSG; + + let hid = CtapHid::new(capabilities); + let wink_permission = TimedPermission::waiting(); + MainHid { + hid, + wink_permission, + } + } + + /// Processes an incoming USB HID packet, and returns an iterator for all outgoing packets. + pub fn process_hid_packet( + &mut self, + env: &mut impl Env, + packet: &HidPacket, + now: Instant, + ctap_state: &mut CtapState, + ) -> HidPacketIterator { + if let Some(message) = self.hid.parse_packet(env, packet, now) { + let processed_message = self.process_message(env, message, now, ctap_state); + log::debug!("Sending message: {:02x?}", processed_message); + CtapHid::split_message(processed_message) + } else { + HidPacketIterator::none() + } + } + + /// Processes a message's commands that affect the protocol outside HID. + pub fn process_message( + &mut self, + env: &mut impl Env, + message: Message, + now: Instant, + ctap_state: &mut CtapState, + ) -> Message { + // If another command arrives, stop winking to prevent accidential button touches. + self.wink_permission = TimedPermission::waiting(); + + let cid = message.cid; + match message.cmd { + // CTAP 2.1 from 2021-06-15, section 11.2.9.1.1. + CtapHidCommand::Msg => { + // If we don't have CTAP1 backward compatibilty, this command is invalid. + #[cfg(not(feature = "with_ctap1"))] + return CtapHid::error_message(cid, CtapHidError::InvalidCmd); + + #[cfg(feature = "with_ctap1")] + match ctap1::Ctap1Command::process_command(env, &message.payload, ctap_state, now) { + Ok(payload) => MainHid::ctap1_success_message(cid, &payload), + Err(ctap1_status_code) => MainHid::ctap1_error_message(cid, ctap1_status_code), + } + } + // CTAP 2.1 from 2021-06-15, section 11.2.9.1.2. + CtapHidCommand::Cbor => { + // Each transaction is atomic, so we process the command directly here and + // don't handle any other packet in the meantime. + // TODO: Send "Processing" type keep-alive packets in the meantime. + let response = + ctap_state.process_command(env, &message.payload, Channel::MainHid(cid), now); + Message { + cid, + cmd: CtapHidCommand::Cbor, + payload: response, + } + } + // CTAP 2.1 from 2021-06-15, section 11.2.9.2.1. + CtapHidCommand::Wink => { + if message.payload.is_empty() { + self.wink_permission = + TimedPermission::granted(now, Self::WINK_TIMEOUT_DURATION); + // The response is empty like the request. + message + } else { + CtapHid::error_message(cid, CtapHidError::InvalidLen) + } + } + // All other commands have already been processed, keep them as is. + _ => message, + } + } + + /// Returns whether a wink permission is currently granted. + pub fn should_wink(&self, now: Instant) -> bool { + self.wink_permission.is_granted(now) + } + + /// Updates the timeout for the wink permission. + pub fn update_wink_timeout(&mut self, now: Instant) { + self.wink_permission = self.wink_permission.check_expiration(now); + } + + #[cfg(feature = "with_ctap1")] + fn ctap1_error_message(cid: ChannelID, error_code: ctap1::Ctap1StatusCode) -> Message { + let code: u16 = error_code.into(); + Message { + cid, + cmd: CtapHidCommand::Msg, + payload: code.to_be_bytes().to_vec(), + } + } + + #[cfg(feature = "with_ctap1")] + fn ctap1_success_message(cid: ChannelID, payload: &[u8]) -> Message { + let mut response = payload.to_vec(); + let code: u16 = ctap1::Ctap1StatusCode::SW_SUCCESS.into(); + response.extend_from_slice(&code.to_be_bytes()); + Message { + cid, + cmd: CtapHidCommand::Msg, + payload: response, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::ctap::hid::ChannelID; + use crate::env::test::TestEnv; + + fn new_initialized() -> (MainHid, ChannelID) { + let (hid, cid) = CtapHid::new_initialized(); + let wink_permission = TimedPermission::waiting(); + ( + MainHid { + hid, + wink_permission, + }, + cid, + ) + } + + #[test] + fn test_process_hid_packet() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut main_hid, cid) = new_initialized(); + + let mut ping_packet = [0x00; 64]; + ping_packet[..4].copy_from_slice(&cid); + ping_packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]); + + let mut response = main_hid.process_hid_packet( + &mut env, + &ping_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), Some(ping_packet)); + assert_eq!(response.next(), None); + } + + #[test] + fn test_process_hid_packet_empty() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut main_hid, cid) = new_initialized(); + + let mut cancel_packet = [0x00; 64]; + cancel_packet[..4].copy_from_slice(&cid); + cancel_packet[4..7].copy_from_slice(&[0x91, 0x00, 0x00]); + + let mut response = main_hid.process_hid_packet( + &mut env, + &cancel_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), None); + } + + #[test] + fn test_wink() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut main_hid, cid) = new_initialized(); + assert!(!main_hid.should_wink(Instant::new(0))); + + let mut wink_packet = [0x00; 64]; + wink_packet[..4].copy_from_slice(&cid); + wink_packet[4..7].copy_from_slice(&[0x88, 0x00, 0x00]); + + let mut response = main_hid.process_hid_packet( + &mut env, + &wink_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), Some(wink_packet)); + assert_eq!(response.next(), None); + assert!(main_hid.should_wink(Instant::new(0))); + assert!(!main_hid.should_wink(Instant::new(1) + MainHid::WINK_TIMEOUT_DURATION)); + } +} diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index 28e55e942..16a49c6e8 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -13,82 +13,90 @@ // limitations under the License. pub mod apdu; +mod client_pin; pub mod command; +mod config_command; +mod credential_id; +mod credential_management; +#[cfg(feature="vault-testing")] +pub mod crypto_wrapper; +#[cfg(not(feature="vault-testing"))] +mod crypto_wrapper; #[cfg(feature = "with_ctap1")] mod ctap1; pub mod data_formats; pub mod hid; -mod key_material; -mod pin_protocol_v1; +pub mod key_material; +mod large_blobs; +pub mod main_hid; +mod pin_protocol; pub mod response; pub mod status_code; pub mod storage; mod timed_permission; +mod token_state; +#[cfg(feature = "vendor_hid")] +pub mod vendor_hid; -#[cfg(feature = "with_ctap2_1")] -use self::command::MAX_CREDENTIAL_COUNT_IN_LIST; +use self::client_pin::{ClientPin, PinPermission}; use self::command::{ - AuthenticatorClientPinParameters, AuthenticatorGetAssertionParameters, - AuthenticatorMakeCredentialParameters, AuthenticatorVendorConfigureParameters, Command, + AuthenticatorGetAssertionParameters, AuthenticatorMakeCredentialParameters, + AuthenticatorVendorConfigureParameters, AuthenticatorVendorUpgradeParameters, Command, }; -#[cfg(feature = "with_ctap2_1")] -use self::data_formats::AuthenticatorTransport; -use self::data_formats::{ - CredentialProtectionPolicy, GetAssertionHmacSecretInput, PackedAttestationStatement, +use self::config_command::process_config; +use self::credential_id::{ + decrypt_credential_id, encrypt_to_credential_id, MAX_CREDENTIAL_ID_SIZE, +}; +use self::credential_management::process_credential_management; +use self::crypto_wrapper::PrivateKey; +use data_formats::{ + AuthenticatorTransport, CredentialProtectionPolicy, EnterpriseAttestationMode, + GetAssertionExtensions, PackedAttestationStatement, PinUvAuthProtocol, PublicKeyCredentialDescriptor, PublicKeyCredentialParameter, PublicKeyCredentialSource, PublicKeyCredentialType, PublicKeyCredentialUserEntity, SignatureAlgorithm, }; use self::hid::ChannelID; -#[cfg(feature = "with_ctap2_1")] -use self::pin_protocol_v1::PinPermission; -use self::pin_protocol_v1::PinProtocolV1; +use self::large_blobs::LargeBlobs; use self::response::{ AuthenticatorGetAssertionResponse, AuthenticatorGetInfoResponse, - AuthenticatorMakeCredentialResponse, AuthenticatorVendorResponse, ResponseData, + AuthenticatorMakeCredentialResponse, AuthenticatorVendorConfigureResponse, + AuthenticatorVendorUpgradeInfoResponse, ResponseData, }; use self::status_code::Ctap2StatusCode; -use self::storage::PersistentStore; use self::timed_permission::TimedPermission; #[cfg(feature = "with_ctap1")] use self::timed_permission::U2fUserPresenceState; -use std::collections::BTreeMap; +use crate::api::attestation_store::{self, Attestation, AttestationStore}; +use crate::api::customization::Customization; +use crate::api::firmware_protection::FirmwareProtection; +use crate::api::upgrade_storage::UpgradeStorage; +use crate::api::user_presence::UserPresence; +use std::time::Instant; +use crate::env::Env; +use std::boxed::Box; use std::string::{String, ToString}; use std::vec; use std::vec::Vec; -use arrayref::array_ref; use byteorder::{BigEndian, ByteOrder}; -use cbor::{cbor_map, cbor_map_options}; -#[cfg(feature = "debug_ctap")] -use core::fmt::Write; -use ctap_crypto::cbc::{cbc_decrypt, cbc_encrypt}; -use ctap_crypto::hmac::{hmac_256, verify_hmac_256}; -use ctap_crypto::rng256::Rng256; +use core::convert::TryFrom; +use ctap_crypto::hmac::hmac_256; use ctap_crypto::sha256::Sha256; -use ctap_crypto::Hash256; -#[cfg(feature = "debug_ctap")] -use libtock_drivers::console::Console; -use crate::shims::{ClockValue, Duration}; +use ctap_crypto::{ecdsa, Hash256}; +use std::time::Duration; +use ctap_crypto::rng256::Rng256; +use cbor as cbor; +use cbor::cbor_map_options; + use locales::t; -// This flag enables or disables basic attestation for FIDO2. U2F is unaffected by -// this setting. The basic attestation uses the signing key from key_material.rs -// as a batch key. Turn it on if you want attestation. In this case, be aware that -// it is your responsibility to generate your own key material and keep it secret. -const USE_BATCH_ATTESTATION: bool = false; -// The signature counter is currently implemented as a global counter, if you set -// this flag to true. The spec strongly suggests to have per-credential-counters, -// but it means you can't have an infinite amount of credentials anymore. Also, -// since this is the only piece of information that needs writing often, we might -// need a flash storage friendly way to implement this feature. The implemented -// solution is a compromise to be compatible with U2F and not wasting storage. -const USE_SIGNATURE_COUNTER: bool = true; +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum UsbEndpoint { + MainHid = 1, + #[cfg(feature = "vendor_hid")] + VendorHid = 2, +} + pub const INITIAL_SIGNATURE_COUNTER: u32 = 1; -// Our credential ID consists of -// - 16 byte initialization vector for AES-256, -// - 32 byte ECDSA private key for the credential, -// - 32 byte relying party ID hashed with SHA256, -// - 32 byte HMAC-SHA256 over everything else. -pub const CREDENTIAL_ID_SIZE: usize = 112; // Set this bit when checking user presence. const UP_FLAG: u8 = 0x01; // Set this bit when checking user verification. @@ -98,31 +106,103 @@ const AT_FLAG: u8 = 0x40; // Set this bit when an extension is used. const ED_FLAG: u8 = 0x80; -pub const TOUCH_TIMEOUT_MS: i64 = 30000; +// CTAP2 specification section 6 requires that the depth of nested CBOR structures be limited to at most four levels. +const MAX_CBOR_NESTING_DEPTH: i8 = 4; + +pub const TOUCH_TIMEOUT_MS: u64 = 30000; #[cfg(feature = "with_ctap1")] -const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_ms(10000); -const RESET_TIMEOUT_DURATION: Duration = Duration::from_ms(10000); -const STATEFUL_COMMAND_TIMEOUT_DURATION: Duration = Duration::from_ms(30000); +pub const TOUCH_TIMEOUT: Duration = Duration::from_millis(TOUCH_TIMEOUT_MS); +#[cfg(feature = "with_ctap1")] +const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_millis(10000); +// TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? +const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(10000); +const STATEFUL_COMMAND_TIMEOUT_DURATION: Duration = Duration::from_millis(30000); pub const FIDO2_VERSION_STRING: &str = "FIDO_2_0"; #[cfg(feature = "with_ctap1")] pub const U2F_VERSION_STRING: &str = "U2F_V2"; // TODO(#106) change to final string when ready -#[cfg(feature = "with_ctap2_1")] pub const FIDO2_1_VERSION_STRING: &str = "FIDO_2_1_PRE"; // We currently only support one algorithm for signatures: ES256. // This algorithm is requested in MakeCredential and advertized in GetInfo. pub const ES256_CRED_PARAM: PublicKeyCredentialParameter = PublicKeyCredentialParameter { cred_type: PublicKeyCredentialType::PublicKey, - alg: SignatureAlgorithm::ES256, + alg: SignatureAlgorithm::Es256, }; -// You can change this value to one of the following for more privacy. -// - Some(CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList) -// - Some(CredentialProtectionPolicy::UserVerificationRequired) -const DEFAULT_CRED_PROTECT: Option = Some(CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList); -pub(crate) const FIDO_CRED_DICT: &'static str = "fido.cred"; +#[cfg(feature = "ed25519")] +pub const EDDSA_CRED_PARAM: PublicKeyCredentialParameter = PublicKeyCredentialParameter { + cred_type: PublicKeyCredentialType::PublicKey, + alg: SignatureAlgorithm::Eddsa, +}; + +const SUPPORTED_CRED_PARAMS: &[PublicKeyCredentialParameter] = &[ + ES256_CRED_PARAM, + #[cfg(feature = "ed25519")] + EDDSA_CRED_PARAM, +]; + +fn get_preferred_cred_param( + params: &[PublicKeyCredentialParameter], +) -> Option<&PublicKeyCredentialParameter> { + params + .iter() + .find(|¶m| SUPPORTED_CRED_PARAMS.contains(param)) +} + +/// Transports supported by OpenSK. +/// +/// An OpenSK library user annotates incoming data with this data type. +/// +/// The difference between this data type and `AuthenticatorTransport` is that the latter +/// corresponds to the communication defined in the CTAP specification. This data type describes +/// the hardware path a packet took. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Transport { + /// Corresponds to CTAP's USB transport. + MainHid, + /// No equivalent in CTAP, used for communication outside the specification. + #[cfg(feature = "vendor_hid")] + VendorHid, +} + +impl Transport { + pub fn hid_connection(self, env: &mut E) -> &mut E::HidConnection { + match self { + Transport::MainHid => env.main_hid_connection(), + #[cfg(feature = "vendor_hid")] + Transport::VendorHid => env.vendor_hid_connection(), + } + } +} + +/// Communication channels between authenticator and client. +/// +/// From OpenSK's perspective, a channel represents a client. When we receive data from a new +/// channel, we have to assume it's a new client. +/// +/// For HID, communication channels coincide with the channel ID. NFC and HID transports are unique +/// channels themselves. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Channel { + /// Corresponds to CTAP's USB transport. + MainHid(ChannelID), + /// No equivalent in CTAP, used for communication outside the specification. + #[cfg(feature = "vendor_hid")] + VendorHid(ChannelID), +} + +// Helpers to perform CBOR read/write while respecting CTAP2 nesting limits. +pub fn cbor_read(encoded_cbor: &[u8]) -> Result { + cbor::reader::read_nested(encoded_cbor, Some(MAX_CBOR_NESTING_DEPTH)) + .map_err(|_e| Ctap2StatusCode::CTAP2_ERR_INVALID_CBOR) +} + +fn cbor_write(value: cbor::Value, encoded_cbor: &mut Vec) -> Result<(), Ctap2StatusCode> { + cbor::writer::write_nested(value, encoded_cbor, Some(MAX_CBOR_NESTING_DEPTH)) + .map_err(|_e| Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) +} // This function is adapted from https://doc.rust-lang.org/nightly/src/core/str/mod.rs.html#2110 // (as of 2020-01-20) and truncates to "max" bytes, not breaking the encoding. @@ -138,293 +218,531 @@ fn truncate_to_char_boundary(s: &str, mut max: usize) -> &str { } } +// Sends keepalive packet during user presence checking. If user agent replies with CANCEL response, +// returns Err(UserPresenceError::Canceled). +#[cfg(not(feature="xous"))] +fn send_keepalive_up_needed( + env: &mut impl Env, + channel: Channel, + timeout: Duration, +) -> Result<(), UserPresenceError> { + let (cid, transport) = match channel { + Channel::MainHid(cid) => (cid, Transport::MainHid), + #[cfg(feature = "vendor_hid")] + Channel::VendorHid(cid) => (cid, Transport::VendorHid), + }; + let keepalive_msg = CtapHid::keepalive(cid, KeepaliveStatus::UpNeeded); + for mut pkt in keepalive_msg { + let ctap_hid_connection = transport.hid_connection(env); + match ctap_hid_connection.send_and_maybe_recv(&mut pkt, timeout) { + Ok(SendOrRecvStatus::Timeout) => { + debug_ctap!(env, "Sending a KEEPALIVE packet timed out"); + // TODO: abort user presence test? + } + Err(_) => panic!("Error sending KEEPALIVE packet"), + Ok(SendOrRecvStatus::Sent) => { + debug_ctap!(env, "Sent KEEPALIVE packet"); + } + Ok(SendOrRecvStatus::Received(endpoint)) => { + let rx_transport = match endpoint { + UsbEndpoint::MainHid => Transport::MainHid, + #[cfg(feature = "vendor_hid")] + UsbEndpoint::VendorHid => Transport::VendorHid, + }; + if rx_transport != transport { + debug_ctap!( + env, + "Received a packet on transport {:?} while sending a KEEPALIVE packet on transport {:?}", + rx_transport, transport + ); + // Ignore this packet. + // TODO(liamjm): Support receiving packets on both interfaces. + continue; + } + + // We only parse one packet, because we only care about CANCEL. + let (received_cid, processed_packet) = CtapHid::process_single_packet(&pkt); + if received_cid != cid { + debug_ctap!( + env, + "Received a packet on channel ID {:?} while sending a KEEPALIVE packet", + received_cid, + ); + return Ok(()); + } + match processed_packet { + ProcessedPacket::InitPacket { cmd, .. } => { + if cmd == CtapHidCommand::Cancel as u8 { + // We ignore the payload, we can't answer with an error code anyway. + debug_ctap!(env, "User presence check cancelled"); + return Err(UserPresenceError::Canceled); + } else { + debug_ctap!( + env, + "Discarded packet with command {} received while sending a KEEPALIVE packet", + cmd, + ); + } + } + ProcessedPacket::ContinuationPacket { .. } => { + debug_ctap!( + env, + "Discarded continuation packet received while sending a KEEPALIVE packet", + ); + } + } + } + } + } + Ok(()) +} + +/// Blocks for user presence. +/// +/// Returns an error in case of timeout, user declining presence request, or keepalive error. +#[cfg(feature="xous")] +fn check_user_presence(env: &mut impl Env, channel: Channel, reason: Option) -> Result<(), Ctap2StatusCode> { + env.user_presence().check_init(); + + let cid = match channel { + Channel::MainHid(cid) => cid, + }; + let result = env.user_presence().wait_with_timeout( + TOUCH_TIMEOUT, + reason, + cid, + ); + + env.user_presence().check_complete(); + result.map_err(|e| e.into()) +} +#[cfg(not(feature="xous"))] +fn check_user_presence(env: &mut impl Env, channel: Channel) -> Result<(), Ctap2StatusCode> { + env.user_presence().check_init(); + + // The timeout is N times the keepalive delay. + const TIMEOUT_ITERATIONS: usize = TOUCH_TIMEOUT_MS as usize / KEEPALIVE_DELAY_MS as usize; + + // All fallible functions are called without '?' operator to always reach + // check_complete(...) cleanup function. + + let mut result = Err(UserPresenceError::Timeout); + for i in 0..=TIMEOUT_ITERATIONS { + // First presence check is made without timeout. That way Env implementation may return + // user presence check result immediately to client, without sending any keepalive packets. + result = env.user_presence().wait_with_timeout(if i == 0 { + Milliseconds(0) + } else { + KEEPALIVE_DELAY + }); + if !matches!(result, Err(UserPresenceError::Timeout)) { + break; + } + // TODO: this may take arbitrary time. Next wait's delay should be adjusted + // accordingly, so that all wait_with_timeout invocations are separated by + // equal time intervals. That way token indicators, such as LEDs, will blink + // with a consistent pattern. + let keepalive_result = send_keepalive_up_needed(env, channel, KEEPALIVE_DELAY); + if keepalive_result.is_err() { + debug_ctap!( + env, + "Sending keepalive failed with error {:?}", + keepalive_result.as_ref().unwrap_err() + ); + result = keepalive_result; + break; + } + } + + env.user_presence().check_complete(); + result.map_err(|e| e.into()) +} + +/// Holds data necessary to sign an assertion for a credential. #[derive(Clone)] -struct AssertionInput { +pub struct AssertionInput { client_data_hash: Vec, auth_data: Vec, - hmac_secret_input: Option, + extensions: GetAssertionExtensions, has_uv: bool, } -struct AssertionState { +/// Contains the state we need to store for GetNextAssertion. +pub struct AssertionState { assertion_input: AssertionInput, // Sorted by ascending order of creation, so the last element is the most recent one. - next_credentials: Vec, + next_credential_keys: Vec, } -enum StatefulCommand { +/// Stores which command currently holds state for subsequent calls. +pub enum StatefulCommand { Reset, - GetAssertion(AssertionState), + GetAssertion(Box), + EnumerateRps(usize), + EnumerateCredentials(Vec), } -// This struct currently holds all state, not only the persistent memory. The persistent members are -// in the persistent store field. -pub struct CtapState<'a, R: Rng256, CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>> -{ - rng: &'a mut R, - // A function to check user presence, ultimately returning true if user presence was detected, - // false otherwise. - _check_user_presence: CheckUserPresence, - persistent_store: PersistentStore, - pin_protocol_v1: PinProtocolV1, - #[cfg(feature = "with_ctap1")] - pub u2f_up_state: U2fUserPresenceState, - // The state initializes to Reset and its timeout, and never goes back to Reset. - stateful_command_permission: TimedPermission, - stateful_command_type: Option, +/// Stores the current CTAP command state and when it times out. +/// +/// Some commands are executed in a series of calls to the authenticator. +/// Interleaving calls to other commands interrupt the current command and +/// remove all state and permissions. Power cycling allows the Reset command, +/// and to prevent misuse or accidents, we disallow Reset after receiving +/// different commands. Therefore, Reset behaves just like all other stateful +/// commands and is included here. Please note that the allowed time for Reset +/// differs from all other stateful commands. +/// +/// Additionally, state that is held over multiple commands is assigned to a channel. We discard +/// all state when we receive data on a different channel. +pub struct StatefulPermission { + permission: TimedPermission, + command_type: Option, + channel: Option, } -impl<'a, R, CheckUserPresence> CtapState<'a, R, CheckUserPresence> -where - R: Rng256, - CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, -{ - pub const PIN_PROTOCOL_VERSION: u64 = 1; - - pub fn new( - rng: &'a mut R, - _check_user_presence: CheckUserPresence, - now: ClockValue, - ) -> CtapState<'a, R, CheckUserPresence> { - let persistent_store = PersistentStore::new(rng); - let pin_protocol_v1 = PinProtocolV1::new(rng); - CtapState { - rng, - _check_user_presence, - persistent_store, - pin_protocol_v1, - #[cfg(feature = "with_ctap1")] - u2f_up_state: U2fUserPresenceState::new( - U2F_UP_PROMPT_TIMEOUT, - Duration::from_ms(TOUCH_TIMEOUT_MS), - ), - stateful_command_permission: TimedPermission::granted(now, RESET_TIMEOUT_DURATION), - stateful_command_type: Some(StatefulCommand::Reset), +impl StatefulPermission { + /// Creates the command state at device startup. + /// + /// Resets are only possible after a power cycle. Therefore, initialization + /// means allowing Reset, and Reset cannot be granted later. + pub fn new_reset(now: Instant) -> StatefulPermission { + StatefulPermission { + permission: TimedPermission::granted(now, RESET_TIMEOUT_DURATION), + command_type: Some(StatefulCommand::Reset), + channel: None, } } - #[allow(dead_code)] // Tock Legacy - pub fn update_command_permission(&mut self, now: ClockValue) { - self.stateful_command_permission = self.stateful_command_permission.check_expiration(now); + + /// Clears all permissions and state. + pub fn clear(&mut self) { + self.permission = TimedPermission::waiting(); + self.command_type = None; + self.channel = None; } - #[allow(dead_code)] // Tock Legacy - fn check_command_permission(&mut self, now: ClockValue) -> Result<(), Ctap2StatusCode> { - self.update_command_permission(now); - if self.stateful_command_permission.is_granted(now) { - Ok(()) + + /// Clears all state if communication is coming from a different channel. + pub fn clear_old_channels(&mut self, channel: &Channel) { + // There are different possible choices for incoming traffic on a different channel: + // A) Always reset state (our choice). + // B) Only reset state if the new command is stateful. + // C) Keep state on all channels until timeout. + // + // If we wanted to switch to (B) or (C), we'd have to be very careful that the state does + // not go stale. For example, we keep credential keys that we expect to still exist. + // However, interleaving (stateless) commands could delete credentials or change the PIN, + // which chould make invalidate our access. Some read-only commands should be okay to run, + // but (A) is the safest and easiest solution. + if let Some(c) = &self.channel { + if c != channel { + self.clear(); + } + } + } + + /// Clears all state if the permission timed out. + pub fn clear_timer(&mut self, now: Instant) { + if !self.permission.is_granted(now) { + self.clear(); + } + } + + /// Gets a reference to the current command state, if any exists. + pub fn get_command(&self) -> Result<&StatefulCommand, Ctap2StatusCode> { + self.command_type + .as_ref() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + } + + /// Sets a new command state, and starts a new clock for timeouts. + pub fn set_command( + &mut self, + now: Instant, + new_command_type: StatefulCommand, + channel: Channel, + ) { + match &new_command_type { + // Reset is only allowed after a power cycle. + StatefulCommand::Reset => unreachable!(), + _ => { + self.permission = TimedPermission::granted(now, STATEFUL_COMMAND_TIMEOUT_DURATION); + self.command_type = Some(new_command_type); + self.channel = Some(channel); + } + } + } + + /// Returns the state for the next assertion and advances it. + /// + /// The state includes all information from GetAssertion and the storage key + /// to the next credential that needs to be processed. + pub fn next_assertion_credential( + &mut self, + ) -> Result<(AssertionInput, usize), Ctap2StatusCode> { + if let Some(StatefulCommand::GetAssertion(assertion_state)) = &mut self.command_type { + let credential_key = assertion_state + .next_credential_keys + .pop() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)?; + Ok((assertion_state.assertion_input.clone(), credential_key)) } else { Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) } } - pub fn increment_global_signature_counter(&mut self) -> Result<(), Ctap2StatusCode> { - if USE_SIGNATURE_COUNTER { - let increment = self.rng.gen_uniform_u32x8()[0] % 8 + 1; - self.persistent_store - .incr_global_signature_counter(increment)?; + /// Returns the index to the next RP ID for enumeration and advances it. + pub fn next_enumerate_rp(&mut self) -> Result { + if let Some(StatefulCommand::EnumerateRps(rp_id_index)) = &mut self.command_type { + let current_index = *rp_id_index; + *rp_id_index += 1; + Ok(current_index) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + } + } + + /// Returns the next storage credential key for enumeration and advances it. + pub fn next_enumerate_credential(&mut self) -> Result { + if let Some(StatefulCommand::EnumerateCredentials(rp_credentials)) = &mut self.command_type + { + rp_credentials + .pop() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + } + } +} + +// This struct currently holds all state, not only the persistent memory. The persistent members are +// in the persistent store field. +pub struct CtapState { + client_pin: ClientPin, + #[cfg(feature = "with_ctap1")] + pub(crate) u2f_up_state: U2fUserPresenceState, + // The state initializes to Reset and its timeout, and never goes back to Reset. + stateful_command_permission: StatefulPermission, + large_blobs: LargeBlobs, +} + +impl CtapState { + pub fn new(env: &mut impl Env, now: Instant) -> Self { + storage::init(env).ok().unwrap(); + let client_pin = ClientPin::new(env.rng()); + CtapState { + client_pin, + #[cfg(feature = "with_ctap1")] + u2f_up_state: U2fUserPresenceState::new(U2F_UP_PROMPT_TIMEOUT, TOUCH_TIMEOUT), + stateful_command_permission: StatefulPermission::new_reset(now), + large_blobs: LargeBlobs::new(), + } + } + + pub fn update_timeouts(&mut self, now: Instant) { + self.stateful_command_permission.clear_timer(now); + self.client_pin.update_timeouts(now); + } + + pub fn increment_global_signature_counter( + &mut self, + env: &mut impl Env, + ) -> Result<(), Ctap2StatusCode> { + if env.customization().use_signature_counter() { + let increment = env.rng().gen_uniform_u32x8()[0] % 8 + 1; + storage::incr_global_signature_counter(env, increment)?; } Ok(()) } - // Encrypts the private key and relying party ID hash into a credential ID. Other - // information, such as a user name, are not stored, because encrypted credential IDs - // are used for credentials stored server-side. Also, we want the key handle to be - // compatible with U2F. - pub fn encrypt_key_handle( - &mut self, - private_key: ctap_crypto::ecdsa::SecKey, - application: &[u8; 32], - ) -> Result, Ctap2StatusCode> { - let master_keys = self.persistent_store.master_keys()?; - let aes_enc_key = &master_keys.encryption; - let mut sk_bytes = [0; 32]; - private_key.to_bytes(&mut sk_bytes); - let mut iv = [0; 16]; - iv.copy_from_slice(&self.rng.gen_uniform_u8x32()[..16]); - - let mut blocks = [[0u8; 16]; 4]; - blocks[0].copy_from_slice(&sk_bytes[..16]); - blocks[1].copy_from_slice(&sk_bytes[16..]); - blocks[2].copy_from_slice(&application[..16]); - blocks[3].copy_from_slice(&application[16..]); - cbc_encrypt(&aes_enc_key, iv, &mut blocks); - - let mut encrypted_id = Vec::with_capacity(0x70); - encrypted_id.extend(&iv); - for b in &blocks { - encrypted_id.extend(b); - } - let id_hmac = hmac_256::(&master_keys.hmac, &encrypted_id[..]); - encrypted_id.extend(&id_hmac); - Ok(encrypted_id) - } - - // Decrypts a credential ID and writes the private key into a PublicKeyCredentialSource. - // None is returned if the HMAC test fails or the relying party does not match the - // decrypted relying party ID hash. - pub fn decrypt_credential_source( - &self, - credential_id: Vec, - rp_id_hash: &[u8], - ) -> Result, Ctap2StatusCode> { - if credential_id.len() != CREDENTIAL_ID_SIZE { - return Ok(None); - } - let master_keys = self.persistent_store.master_keys()?; - let payload_size = credential_id.len() - 32; - if !verify_hmac_256::( - &master_keys.hmac, - &credential_id[..payload_size], - array_ref![credential_id, payload_size, 32], - ) { - return Ok(None); - } - let aes_enc_key = &master_keys.encryption; - let aes_dec_key = aes_enc_key; - let mut iv = [0; 16]; - iv.copy_from_slice(&credential_id[..16]); - let mut blocks = [[0u8; 16]; 4]; - for i in 0..4 { - blocks[i].copy_from_slice(&credential_id[16 * (i + 1)..16 * (i + 2)]); - } - - cbc_decrypt(&aes_dec_key, iv, &mut blocks); - let mut decrypted_sk = [0; 32]; - let mut decrypted_rp_id_hash = [0; 32]; - decrypted_sk[..16].clone_from_slice(&blocks[0]); - decrypted_sk[16..].clone_from_slice(&blocks[1]); - decrypted_rp_id_hash[..16].clone_from_slice(&blocks[2]); - decrypted_rp_id_hash[16..].clone_from_slice(&blocks[3]); - if rp_id_hash != decrypted_rp_id_hash { - return Ok(None); - } - - let sk_option = ctap_crypto::ecdsa::SecKey::from_bytes(&decrypted_sk); - Ok(sk_option.map(|sk| PublicKeyCredentialSource { - key_type: PublicKeyCredentialType::PublicKey, - credential_id, - private_key: sk, - rp_id: String::from(""), - user_handle: vec![], - user_display_name: None, - cred_protect_policy: None, - creation_order: 0, - user_name: None, - user_icon: None, - })) + // Returns whether CTAP1 commands are currently supported. + // If alwaysUv is enabled and the authenticator does not support internal UV, + // CTAP1 needs to be disabled. + #[cfg(feature = "with_ctap1")] + pub fn allows_ctap1(&self, env: &mut impl Env) -> Result { + Ok(!storage::has_always_uv(env)?) } pub fn process_command( &mut self, + env: &mut impl Env, command_cbor: &[u8], - cid: ChannelID, - now: ClockValue, + channel: Channel, + now: Instant, ) -> Vec { let cmd = Command::deserialize(command_cbor); - #[cfg(feature = "debug_ctap")] - writeln!(&mut Console::new(), "Received command: {:#?}", cmd).unwrap(); - match cmd { - Ok(command) => { - log::debug!("{:?}", command); - // Correct behavior between CTAP1 and CTAP2 isn't defined yet. Just a guess. - #[cfg(feature = "with_ctap1")] - { - self.u2f_up_state = U2fUserPresenceState::new( - U2F_UP_PROMPT_TIMEOUT, - Duration::from_ms(TOUCH_TIMEOUT_MS), - ); - } - match (&command, &self.stateful_command_type) { - ( - Command::AuthenticatorGetNextAssertion, - Some(StatefulCommand::GetAssertion(_)), - ) => (), - (Command::AuthenticatorReset, Some(StatefulCommand::Reset)) => (), - // GetInfo does not reset stateful commands. - (Command::AuthenticatorGetInfo, _) => (), - // AuthenticatorSelection does not reset stateful commands. - #[cfg(feature = "with_ctap2_1")] - (Command::AuthenticatorSelection, _) => (), - (_, _) => { - self.stateful_command_type = None; + log::debug!("Received command: {:#?}", cmd); + let response = + cmd.and_then(|command| self.process_parsed_command(env, command, channel, now)); + log::debug!("Sending response: {:#?}", response); + match response { + Ok(response_data) => { + let mut response_vec = vec![Ctap2StatusCode::CTAP2_OK as u8]; + if let Some(value) = response_data.into() { + if cbor_write(value, &mut response_vec).is_err() { + response_vec = vec![Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR as u8]; } } - let response = match command { - Command::AuthenticatorMakeCredential(params) => { - self.process_make_credential(params, cid) - } - Command::AuthenticatorGetAssertion(params) => { - self.process_get_assertion(params, cid, now) - } - Command::AuthenticatorGetNextAssertion => self.process_get_next_assertion(now), - Command::AuthenticatorGetInfo => self.process_get_info(), - Command::AuthenticatorClientPin(params) => self.process_client_pin(params), - Command::AuthenticatorReset => self.process_reset(cid, now), - #[cfg(feature = "with_ctap2_1")] - Command::AuthenticatorSelection => self.process_selection(cid), - // TODO(kaczmarczyck) implement FIDO 2.1 commands - // Vendor specific commands - Command::AuthenticatorVendorConfigure(params) => { - self.process_vendor_configure(params, cid) - } - }; - #[cfg(feature = "debug_ctap")] - writeln!(&mut Console::new(), "Sending response: {:#?}", response).unwrap(); - match response { - Ok(response_data) => { - let mut response_vec = vec![0x00]; - if let Some(value) = response_data.into() { - if !cbor::write(value, &mut response_vec) { - response_vec = vec![ - Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR - as u8, - ]; - } - } - response_vec - } - Err(error_code) => vec![error_code as u8], - } + response_vec } Err(error_code) => vec![error_code as u8], } } + /// Processed a command after parsing from CBOR, returning its structured output. + /// + /// This function contains the logic of `parse_command`, minus all CBOR encoding and decoding. + /// It should make command parsing easier to test. + pub fn process_parsed_command( + &mut self, + env: &mut impl Env, + command: Command, + channel: Channel, + now: Instant, + ) -> Result { + // Correct behavior between CTAP1 and CTAP2 isn't defined yet. Just a guess. + #[cfg(feature = "with_ctap1")] + { + // We create a block statement to wrap this assignment expression, because attributes + // (like #[cfg]) are not supported on expressions. + self.u2f_up_state = U2fUserPresenceState::new(U2F_UP_PROMPT_TIMEOUT, TOUCH_TIMEOUT); + } + self.stateful_command_permission + .clear_old_channels(&channel); + self.stateful_command_permission.clear_timer(now); + match (&command, self.stateful_command_permission.get_command()) { + (Command::AuthenticatorGetNextAssertion, Ok(StatefulCommand::GetAssertion(_))) + | (Command::AuthenticatorReset, Ok(StatefulCommand::Reset)) + // AuthenticatorGetInfo still allows Reset. + | (Command::AuthenticatorGetInfo, Ok(StatefulCommand::Reset)) + // AuthenticatorSelection still allows Reset. + | (Command::AuthenticatorSelection, Ok(StatefulCommand::Reset)) + // AuthenticatorMakeCredential is used like AuthenticatorSelection in 2.0. + | (Command::AuthenticatorMakeCredential(_), Ok(StatefulCommand::Reset)) + // AuthenticatorCredentialManagement handles its subcommands later. + | ( + Command::AuthenticatorCredentialManagement(_), + Ok(StatefulCommand::EnumerateRps(_)), + ) + | ( + Command::AuthenticatorCredentialManagement(_), + Ok(StatefulCommand::EnumerateCredentials(_)), + ) => (), + (_, _) => self.stateful_command_permission.clear(), + } + match &channel { + Channel::MainHid(_) => self.process_fido_command(env, command, channel, now), + #[cfg(feature = "vendor_hid")] + Channel::VendorHid(_) => self.process_vendor_command(env, command, channel), + } + } + + fn process_fido_command( + &mut self, + env: &mut impl Env, + command: Command, + channel: Channel, + now: Instant, + ) -> Result { + match command { + Command::AuthenticatorMakeCredential(params) => { + self.process_make_credential(env, params, channel) + } + Command::AuthenticatorGetAssertion(params) => { + self.process_get_assertion(env, params, channel, now) + } + Command::AuthenticatorGetNextAssertion => self.process_get_next_assertion(env), + Command::AuthenticatorGetInfo => self.process_get_info(env), + Command::AuthenticatorClientPin(params) => { + self.client_pin.process_command(env, params, now) + } + Command::AuthenticatorReset => self.process_reset(env, channel), + Command::AuthenticatorCredentialManagement(params) => process_credential_management( + env, + &mut self.stateful_command_permission, + &mut self.client_pin, + params, + channel, + now, + ), + Command::AuthenticatorSelection => self.process_selection(env, channel), + Command::AuthenticatorLargeBlobs(params) => { + self.large_blobs + .process_command(env, &mut self.client_pin, params) + } + Command::AuthenticatorConfig(params) => { + process_config(env, &mut self.client_pin, params) + } + #[cfg(feature = "vendor_hid")] + _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND), + #[cfg(not(feature = "vendor_hid"))] + _ => self.process_vendor_command(env, command, channel), + } + } + + fn process_vendor_command( + &mut self, + env: &mut impl Env, + command: Command, + channel: Channel, + ) -> Result { + match command { + Command::AuthenticatorVendorConfigure(params) => { + self.process_vendor_configure(env, params, channel) + } + Command::AuthenticatorVendorUpgrade(params) => self.process_vendor_upgrade(env, params), + Command::AuthenticatorVendorUpgradeInfo => self.process_vendor_upgrade_info(env), + Command::AuthenticatorGetInfo => self.process_get_info(env), + _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND), + } + } + fn pin_uv_auth_precheck( &mut self, + env: &mut impl Env, pin_uv_auth_param: &Option>, - pin_uv_auth_protocol: Option, - cid: ChannelID, + pin_uv_auth_protocol: Option, + channel: Channel, ) -> Result<(), Ctap2StatusCode> { if let Some(auth_param) = &pin_uv_auth_param { // This case was added in FIDO 2.1. if auth_param.is_empty() { - //(self.check_user_presence)(cid)?; - if crate::fido::request_permission_blocking( - t!("vault.fido.pin_uv_auth", xous::LANG).to_string(), - cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) - } - if self.persistent_store.pin_hash()?.is_none() { + check_user_presence(env, channel, Some( + format!("{}\n{:x?}", + t!("vault.fido.pin_uv_auth", xous::LANG), + channel + ) + ))?; + if storage::pin_hash(env)?.is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_NOT_SET); } else { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID); } } + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?; + } + Ok(()) + } - match pin_uv_auth_protocol { - Some(CtapState::::PIN_PROTOCOL_VERSION) => Ok(()), - Some(_) => Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID), - None => Err(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER), - } + fn check_cred_protect_for_listed_credential( + &mut self, + credential: &Option, + has_uv: bool, + ) -> bool { + if let Some(credential) = credential { + has_uv + || !matches!( + credential.cred_protect_policy, + Some(CredentialProtectionPolicy::UserVerificationRequired), + ) } else { - Ok(()) + false } } fn process_make_credential( &mut self, + env: &mut impl Env, make_credential_params: AuthenticatorMakeCredentialParameters, - cid: ChannelID, + channel: Channel, ) -> Result { let AuthenticatorMakeCredentialParameters { client_data_hash, @@ -436,116 +754,166 @@ where options, pin_uv_auth_param, pin_uv_auth_protocol, + enterprise_attestation, } = make_credential_params; - self.pin_uv_auth_precheck(&pin_uv_auth_param, pin_uv_auth_protocol, cid)?; + self.pin_uv_auth_precheck(env, &pin_uv_auth_param, pin_uv_auth_protocol, channel)?; - if !pub_key_cred_params.contains(&ES256_CRED_PARAM) { - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM); - } - - let (use_hmac_extension, cred_protect_policy) = if let Some(extensions) = extensions { - let mut cred_protect = extensions.cred_protect; - if cred_protect.unwrap_or(CredentialProtectionPolicy::UserVerificationOptional) - < DEFAULT_CRED_PROTECT - .unwrap_or(CredentialProtectionPolicy::UserVerificationOptional) - { - cred_protect = DEFAULT_CRED_PROTECT; - } - (extensions.hmac_secret, cred_protect) - } else { - (false, DEFAULT_CRED_PROTECT) - }; - - let has_extension_output = use_hmac_extension || cred_protect_policy.is_some(); + // When more algorithms are supported, iterate and pick the first match. + let cred_param = get_preferred_cred_param(&pub_key_cred_params) + .ok_or(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_ALGORITHM)?; + let algorithm = cred_param.alg; let rp_id = rp.rp_id; + let ep_att = if let Some(enterprise_attestation) = enterprise_attestation { + let authenticator_mode = env + .customization() + .enterprise_attestation_mode() + .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)?; + if !storage::enterprise_attestation(env)? { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + match ( + EnterpriseAttestationMode::try_from(enterprise_attestation)?, + authenticator_mode, + ) { + ( + EnterpriseAttestationMode::PlatformManaged, + EnterpriseAttestationMode::PlatformManaged, + ) => true, + _ => env.customization().is_enterprise_rp_id(&rp_id), + } + } else { + false + }; + + // MakeCredential always requires user presence. + // User verification depends on the PIN auth inputs, which are checked here. + // The ED flag is added later, if applicable. + let has_uv = pin_uv_auth_param.is_some(); + let mut flags = match pin_uv_auth_param { + Some(pin_uv_auth_param) => { + // This case is not mentioned in CTAP2.1, so we keep 2.0 logic. + if storage::pin_hash(env)?.is_none() { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_NOT_SET); + } + self.client_pin.verify_pin_uv_auth_token( + &client_data_hash, + &pin_uv_auth_param, + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, + )?; + self.client_pin + .has_permission(PinPermission::MakeCredential)?; + self.client_pin.check_user_verified_flag()?; + // Checking for the correct permissions_rp_id is specified earlier. + // Error codes are identical though, so the implementation can be identical with + // GetAssertion. + self.client_pin.ensure_rp_id_permission(&rp_id)?; + UV_FLAG + } + None => { + if options.uv { + return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); + } + if storage::has_always_uv(env)? { + return Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED); + } + // Corresponds to makeCredUvNotRqd set to true. + if options.rk && storage::pin_hash(env)?.is_some() { + return Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED); + } + 0x00 + } + }; + flags |= UP_FLAG | AT_FLAG; + let rp_id_hash = Sha256::hash(rp_id.as_bytes()); if let Some(exclude_list) = exclude_list { for cred_desc in exclude_list { - if self - .persistent_store - .find_credential(&rp_id, &cred_desc.key_id, pin_uv_auth_param.is_none())? - .is_some() - || self - .decrypt_credential_source(cred_desc.key_id, &rp_id_hash)? - .is_some() - { + if self.check_cred_protect_for_listed_credential( + &storage::find_credential(env, &rp_id, &cred_desc.key_id)?, + has_uv, + ) || self.check_cred_protect_for_listed_credential( + &decrypt_credential_id(env, cred_desc.key_id, &rp_id_hash)?, + has_uv, + ) { // Perform this check, so bad actors can't brute force exclude_list // without user interaction. - // (self.check_user_presence)(cid)?; - let mut desc = String::from(t!("vault.fido.exclude_list", xous::LANG)); - desc.push_str( - &format!("\n\nRelying Party: {}\nUser name: {}", - if let Some(name) = rp.rp_name {name.to_string()} else {rp_id.to_string()}, - if let Some(name) = user.user_display_name {name} else {user.user_name.unwrap_or("*Unspecified*".to_string())}, - ) - ); - if crate::fido::request_permission_blocking(desc, cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + #[cfg(feature="xous")] + { + let mut desc = String::from(t!("vault.fido.exclude_list", xous::LANG)); + desc.push_str( + &format!("\n\nRelying Party: {}\nUser name: {}", + if let Some(name) = rp.rp_name {name.to_string()} else {rp_id.to_string()}, + if let Some(name) = user.user_display_name {name} else {user.user_name.unwrap_or("*Unspecified*".to_string())}, + ) + ); + let _ = check_user_presence(env, channel, Some(desc)); } + #[cfg(not(feature="xous"))] + let _ = check_user_presence(env, channel, Some(desc)); return Err(Ctap2StatusCode::CTAP2_ERR_CREDENTIAL_EXCLUDED); } } } - // MakeCredential always requires user presence. - // User verification depends on the PIN auth inputs, which are checked here. - let ed_flag = if has_extension_output { ED_FLAG } else { 0 }; - let flags = match pin_uv_auth_param { - Some(pin_auth) => { - if self.persistent_store.pin_hash()?.is_none() { - // Specification is unclear, could be CTAP2_ERR_INVALID_OPTION. - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_NOT_SET); - } - if !self - .pin_protocol_v1 - .verify_pin_auth_token(&client_data_hash, &pin_auth) - { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - #[cfg(feature = "with_ctap2_1")] - { - self.pin_protocol_v1 - .has_permission(PinPermission::MakeCredential)?; - self.pin_protocol_v1.has_permission_for_rp_id(&rp_id)?; - } - UP_FLAG | UV_FLAG | AT_FLAG | ed_flag - } - None => { - if self.persistent_store.pin_hash()?.is_some() { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_REQUIRED); - } - if options.uv { - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); - } - UP_FLAG | AT_FLAG | ed_flag - } + #[cfg(feature="xous")] + { + let mut make_cred_desc = String::from(t!("vault.fido.make_credential", xous::LANG)); + let alt_name = user.user_name.as_deref().unwrap_or("*Unspecified*"); + make_cred_desc.push_str( + &format!("\n\nRelying Party: {}\nUser name: {}", + // prefer the "friendly name" over the technical name + if let Some(name) = &rp.rp_name {name.to_string()} else {rp_id.to_string()}, + if let Some(name) = &user.user_display_name {name} else {alt_name} + ) + ); + check_user_presence(env, channel, Some(make_cred_desc))?; + } + #[cfg(not(feature="xous"))] + check_user_presence(env, channel)?; + self.client_pin.clear_token_flags(); + + let default_cred_protect = env.customization().default_cred_protect(); + let mut cred_protect_policy = extensions.cred_protect; + if cred_protect_policy.unwrap_or(CredentialProtectionPolicy::UserVerificationOptional) + < default_cred_protect.unwrap_or(CredentialProtectionPolicy::UserVerificationOptional) + { + cred_protect_policy = default_cred_protect; + } + let min_pin_length = + extensions.min_pin_length && storage::min_pin_length_rp_ids(env)?.contains(&rp_id); + // None for no input, false for invalid input, true for valid input. + let has_cred_blob_output = extensions.cred_blob.is_some(); + let cred_blob = extensions + .cred_blob + .filter(|c| c.len() <= env.customization().max_cred_blob_length()); + let cred_blob_output = if has_cred_blob_output { + Some(cred_blob.is_some()) + } else { + None + }; + let has_extension_output = extensions.hmac_secret + || extensions.cred_protect.is_some() + || min_pin_length + || has_cred_blob_output; + if has_extension_output { + flags |= ED_FLAG + }; + let large_blob_key = match (options.rk, extensions.large_blob_key) { + (true, Some(true)) => Some(env.rng().gen_uniform_u8x32().to_vec()), + _ => None, }; - //(self.check_user_presence)(cid)?; - let mut make_cred_desc = String::from(t!("vault.fido.make_credential", xous::LANG)); - let alt_name = user.user_name.as_deref().unwrap_or("*Unspecified*"); - make_cred_desc.push_str( - &format!("\n\nRelying Party: {}\nUser name: {}", - // prefer the "friendly name" over the technical name - if let Some(name) = &rp.rp_name {name.to_string()} else {rp_id.to_string()}, - if let Some(name) = &user.user_display_name {name} else {alt_name} - ) - ); - if crate::fido::request_permission_blocking(make_cred_desc, cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) - } - - let sk = ctap_crypto::ecdsa::SecKey::gensk(self.rng); - let pk = sk.genpk(); - + // We decide on the algorithm early, but delay key creation since it takes time. + // We rather do that later so all intermediate checks may return faster. + let private_key = PrivateKey::new(env, algorithm); let credential_id = if options.rk { - let random_id = self.rng.gen_uniform_u8x32().to_vec(); + let random_id = env.rng().gen_uniform_u8x32().to_vec(); let credential_source = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: random_id.clone(), - private_key: sk.clone(), + private_key: private_key.clone(), rp_id, user_handle: user.user_id, // This input is user provided, so we crop it to 64 byte for storage. @@ -554,79 +922,102 @@ where .user_display_name .map(|s| truncate_to_char_boundary(&s, 64).to_string()), cred_protect_policy, - creation_order: self.persistent_store.new_creation_order()?, + creation_order: storage::new_creation_order(env)?, user_name: user .user_name .map(|s| truncate_to_char_boundary(&s, 64).to_string()), user_icon: user .user_icon .map(|s| truncate_to_char_boundary(&s, 64).to_string()), + cred_blob, + large_blob_key: large_blob_key.clone(), }; - self.persistent_store.store_credential(credential_source)?; + storage::store_credential(env, credential_source)?; random_id } else { - self.encrypt_key_handle(sk.clone(), &rp_id_hash)? + encrypt_to_credential_id( + env, + &private_key, + &rp_id_hash, + cred_protect_policy, + cred_blob, + )? }; - let mut auth_data = self.generate_auth_data(&rp_id_hash, flags)?; - auth_data.extend(&self.persistent_store.aaguid()?); - // The length is fixed to 0x20 or 0x70 and fits one byte. + let mut auth_data = self.generate_auth_data(env, &rp_id_hash, flags)?; + auth_data.extend(&storage::aaguid(env)?); + // The length is fixed to 0x20 or 0x80 and fits one byte. if credential_id.len() > 0xFF { - return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_TOO_LONG); + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); } auth_data.extend(vec![0x00, credential_id.len() as u8]); auth_data.extend(&credential_id); - let cose_key = match pk.to_cose_key() { - Some(cose_key) => cose_key, - None => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR), - }; - auth_data.extend(cose_key); + let public_cose_key = private_key.get_pub_key(env)?; + cbor_write(cbor::Value::from(public_cose_key), &mut auth_data)?; if has_extension_output { - let hmac_secret_output = if use_hmac_extension { Some(true) } else { None }; - let extensions_output = cbor_map_options! { - "hmac-secret" => hmac_secret_output, - "credProtect" => cred_protect_policy, + let hmac_secret_output = if extensions.hmac_secret { + Some(true) + } else { + None }; - if !cbor::write(extensions_output, &mut auth_data) { - return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR); - } + let min_pin_length_output = if min_pin_length { + Some(storage::min_pin_length(env)? as u64) + } else { + None + }; + let cred_protect_output = extensions.cred_protect.and(cred_protect_policy); + let extensions_output = cbor_map_options! { + "credBlob" => cred_blob_output, + "credProtect" => cred_protect_output, + "hmac-secret" => hmac_secret_output, + "minPinLength" => min_pin_length_output, + }; + cbor_write(extensions_output, &mut auth_data)?; } let mut signature_data = auth_data.clone(); signature_data.extend(client_data_hash); - let (signature, x5c) = if USE_BATCH_ATTESTATION { - let attestation_private_key = self - .persistent_store - .attestation_private_key()? - .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; - let attestation_key = - ctap_crypto::ecdsa::SecKey::from_bytes(&attestation_private_key).unwrap(); - let attestation_certificate = self - .persistent_store - .attestation_certificate()? - .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; - ( - attestation_key.sign_rfc6979::(&signature_data), - Some(vec![attestation_certificate]), - ) + let attestation_id = if ep_att { + Some(attestation_store::Id::Enterprise) + } else if env.customization().use_batch_attestation() { + Some(attestation_store::Id::Batch) } else { - ( - sk.sign_rfc6979::(&signature_data), - None, - ) + None + }; + let (signature, x5c) = match attestation_id { + Some(id) => { + let Attestation { + private_key, + certificate, + } = env + .attestation_store() + .get(&id)? + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; + let attestation_key = ecdsa::SecKey::from_bytes(&private_key).unwrap(); + ( + attestation_key + .sign_rfc6979::(&signature_data) + .to_asn1_der(), + Some(vec![certificate]), + ) + } + None => (private_key.sign_and_encode(env, &signature_data)?, None), }; let attestation_statement = PackedAttestationStatement { - alg: SignatureAlgorithm::ES256 as i64, - sig: signature.to_asn1_der(), + alg: SignatureAlgorithm::Es256 as i64, + sig: signature, x5c, ecdaa_key_id: None, }; + let ep_att = if ep_att { Some(true) } else { None }; Ok(ResponseData::AuthenticatorMakeCredential( AuthenticatorMakeCredentialResponse { fmt: String::from("packed"), auth_data, att_stmt: attestation_statement, + ep_att, + large_blob_key, }, )) } @@ -635,55 +1026,79 @@ where // The computation is deterministic, and private_key expected to be unique. fn generate_cred_random( &mut self, - private_key: &ctap_crypto::ecdsa::SecKey, + env: &mut impl Env, + private_key: &PrivateKey, has_uv: bool, ) -> Result<[u8; 32], Ctap2StatusCode> { - let mut private_key_bytes = [0u8; 32]; - private_key.to_bytes(&mut private_key_bytes); - let key = self.persistent_store.cred_random_secret(has_uv)?; - Ok(hmac_256::(&key, &private_key_bytes)) + let entropy = private_key.to_bytes(); + let key = storage::cred_random_secret(env, has_uv)?; + Ok(hmac_256::(&key, &entropy)) } // Processes the input of a get_assertion operation for a given credential // and returns the correct Get(Next)Assertion response. fn assertion_response( &mut self, - credential: PublicKeyCredentialSource, + env: &mut impl Env, + mut credential: PublicKeyCredentialSource, assertion_input: AssertionInput, number_of_credentials: Option, + is_next: bool, ) -> Result { let AssertionInput { client_data_hash, mut auth_data, - hmac_secret_input, + extensions, has_uv, } = assertion_input; // Process extensions. - if let Some(hmac_secret_input) = hmac_secret_input { - let cred_random = self.generate_cred_random(&credential.private_key, has_uv)?; - let encrypted_output = self - .pin_protocol_v1 - .process_hmac_secret(hmac_secret_input, &cred_random)?; - let extensions_output = cbor_map! { + if extensions.hmac_secret.is_some() || extensions.cred_blob { + let encrypted_output = if let Some(hmac_secret_input) = extensions.hmac_secret { + let cred_random = + self.generate_cred_random(env, &credential.private_key, has_uv)?; + Some(self.client_pin.process_hmac_secret( + env.rng(), + hmac_secret_input, + &cred_random, + )?) + } else { + None + }; + // This could be written more nicely with `then_some` when stable. + let cred_blob = if extensions.cred_blob { + Some(credential.cred_blob.unwrap_or_default()) + } else { + None + }; + let extensions_output = cbor_map_options! { + "credBlob" => cred_blob, "hmac-secret" => encrypted_output, }; - if !cbor::write(extensions_output, &mut auth_data) { - return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR); - } + cbor_write(extensions_output, &mut auth_data)?; } + let large_blob_key = match extensions.large_blob_key { + Some(true) => credential.large_blob_key, + _ => None, + }; let mut signature_data = auth_data.clone(); signature_data.extend(client_data_hash); let signature = credential .private_key - .sign_rfc6979::(&signature_data); + .sign_and_encode(env, &signature_data)?; let cred_desc = PublicKeyCredentialDescriptor { key_type: PublicKeyCredentialType::PublicKey, key_id: credential.credential_id, transports: None, // You can set USB as a hint here. }; + // Remove user identifiable information without uv. + if !has_uv { + credential.user_name = None; + credential.user_display_name = None; + credential.user_icon = None; + } let user = if !credential.user_handle.is_empty() { Some(PublicKeyCredentialUserEntity { user_id: credential.user_handle, @@ -694,37 +1109,38 @@ where } else { None }; - Ok(ResponseData::AuthenticatorGetAssertion( - AuthenticatorGetAssertionResponse { - credential: Some(cred_desc), - auth_data, - signature: signature.to_asn1_der(), - user, - number_of_credentials: number_of_credentials.map(|n| n as u64), - }, - )) + let response_data = AuthenticatorGetAssertionResponse { + credential: Some(cred_desc), + auth_data, + signature, + user, + number_of_credentials: number_of_credentials.map(|n| n as u64), + large_blob_key, + }; + // Only returned for the first GetAssertion, not for Next calls. + if is_next { + Ok(ResponseData::AuthenticatorGetNextAssertion(response_data)) + } else { + Ok(ResponseData::AuthenticatorGetAssertion(response_data)) + } } // Returns the first applicable credential from the allow list. fn get_any_credential_from_allow_list( &mut self, + env: &mut impl Env, allow_list: Vec, rp_id: &str, rp_id_hash: &[u8], has_uv: bool, ) -> Result, Ctap2StatusCode> { for allowed_credential in allow_list { - let credential = self.persistent_store.find_credential( - rp_id, - &allowed_credential.key_id, - !has_uv, - )?; - if credential.is_some() { + let credential = storage::find_credential(env, rp_id, &allowed_credential.key_id)?; + if self.check_cred_protect_for_listed_credential(&credential, has_uv) { return Ok(credential); } - let credential = - self.decrypt_credential_source(allowed_credential.key_id, &rp_id_hash)?; - if credential.is_some() { + let credential = decrypt_credential_id(env, allowed_credential.key_id, rp_id_hash)?; + if self.check_cred_protect_for_listed_credential(&credential, has_uv) { return Ok(credential); } } @@ -733,9 +1149,10 @@ where fn process_get_assertion( &mut self, + env: &mut impl Env, get_assertion_params: AuthenticatorGetAssertionParameters, - cid: ChannelID, - now: ClockValue, + channel: Channel, + now: Instant, ) -> Result { let AuthenticatorGetAssertionParameters { rp_id, @@ -747,296 +1164,314 @@ where pin_uv_auth_protocol, } = get_assertion_params; - self.pin_uv_auth_precheck(&pin_uv_auth_param, pin_uv_auth_protocol, cid)?; + self.pin_uv_auth_precheck(env, &pin_uv_auth_param, pin_uv_auth_protocol, channel)?; - let hmac_secret_input = extensions.map(|e| e.hmac_secret).flatten(); - if hmac_secret_input.is_some() && !options.up { + if extensions.hmac_secret.is_some() && !options.up { // The extension is actually supported, but we need user presence. - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION); + return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_OPTION); } // The user verification bit depends on the existance of PIN auth, since we do // not support internal UV. User presence is requested as an option. let has_uv = pin_uv_auth_param.is_some(); let mut flags = match pin_uv_auth_param { - Some(pin_auth) => { - if self.persistent_store.pin_hash()?.is_none() { - // Specification is unclear, could be CTAP2_ERR_UNSUPPORTED_OPTION. + Some(pin_uv_auth_param) => { + // This case is not mentioned in CTAP2.1, so we keep 2.0 logic. + if storage::pin_hash(env)?.is_none() { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_NOT_SET); } - if !self - .pin_protocol_v1 - .verify_pin_auth_token(&client_data_hash, &pin_auth) - { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - #[cfg(feature = "with_ctap2_1")] - { - self.pin_protocol_v1 - .has_permission(PinPermission::GetAssertion)?; - self.pin_protocol_v1.has_permission_for_rp_id(&rp_id)?; - } + self.client_pin.verify_pin_uv_auth_token( + &client_data_hash, + &pin_uv_auth_param, + pin_uv_auth_protocol.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, + )?; + self.client_pin + .has_permission(PinPermission::GetAssertion)?; + // Checking for the UV flag is specified earlier for GetAssertion. + // Error codes are identical though, so the implementation can be identical with + // MakeCredential. + self.client_pin.check_user_verified_flag()?; + self.client_pin.ensure_rp_id_permission(&rp_id)?; UV_FLAG } None => { if options.uv { - // The specification (inconsistently) wants CTAP2_ERR_UNSUPPORTED_OPTION. return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION); } + if options.up && storage::has_always_uv(env)? { + return Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED); + } 0x00 } }; if options.up { flags |= UP_FLAG; } - if hmac_secret_input.is_some() { + if extensions.hmac_secret.is_some() || extensions.cred_blob { flags |= ED_FLAG; } let rp_id_hash = Sha256::hash(rp_id.as_bytes()); - let mut applicable_credentials = if let Some(allow_list) = allow_list { - if let Some(credential) = - self.get_any_credential_from_allow_list(allow_list, &rp_id, &rp_id_hash, has_uv)? - { - vec![credential] - } else { - vec![] - } + let (credential, next_credential_keys) = if let Some(allow_list) = allow_list { + ( + self.get_any_credential_from_allow_list( + env, + allow_list, + &rp_id, + &rp_id_hash, + has_uv, + )?, + vec![], + ) } else { - self.persistent_store.filter_credential(&rp_id, !has_uv)? + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(env, &mut iter_result)?; + let mut stored_credentials: Vec<(usize, u64)> = iter + .filter_map(|(key, credential)| { + if credential.rp_id == rp_id && (has_uv || credential.is_discoverable()) { + Some((key, credential.creation_order)) + } else { + None + } + }) + .collect(); + iter_result?; + stored_credentials.sort_unstable_by_key(|&(_key, order)| order); + let mut stored_credentials: Vec = stored_credentials + .into_iter() + .map(|(key, _order)| key) + .collect(); + let credential = stored_credentials + .pop() + .map(|key| storage::get_credential(env, key)) + .transpose()?; + (credential, stored_credentials) }; - // Remove user identifiable information without uv. - if !has_uv { - for credential in &mut applicable_credentials { - credential.user_name = None; - credential.user_display_name = None; - credential.user_icon = None; - } - } - applicable_credentials.sort_unstable_by_key(|c| c.creation_order); + + let credential = credential.ok_or(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS)?; // This check comes before CTAP2_ERR_NO_CREDENTIALS in CTAP 2.0. - // For CTAP 2.1, it was moved to a later protocol step. if options.up { - //(self.check_user_presence)(cid)?; - let mut desc = String::from(t!("vault.fido.get_assertion", xous::LANG)); - desc.push_str( - &format!("\n\nRelying Party: {}\nClient data hash: {}", - rp_id, - hex::encode(&client_data_hash), - ) - ); - if crate::fido::request_permission_blocking(desc, cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + #[cfg(feature="xous")] + { + let mut desc = String::from(t!("vault.fido.get_assertion", xous::LANG)); + desc.push_str( + &format!("\n\nRelying Party: {}\nClient data hash: {}", + rp_id, + hex::encode(&client_data_hash), + ) + ); + check_user_presence(env, channel, Some(desc))?; } + #[cfg(not(feature="xous"))] + check_user_presence(env, channel)?; + self.client_pin.clear_token_flags(); } - let credential = applicable_credentials - .pop() - .ok_or(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS)?; - - self.increment_global_signature_counter()?; + self.increment_global_signature_counter(env)?; let assertion_input = AssertionInput { client_data_hash, - auth_data: self.generate_auth_data(&rp_id_hash, flags)?, - hmac_secret_input, + auth_data: self.generate_auth_data(env, &rp_id_hash, flags)?, + extensions, has_uv, }; - let number_of_credentials = if applicable_credentials.is_empty() { + let number_of_credentials = if next_credential_keys.is_empty() { None } else { - let number_of_credentials = Some(applicable_credentials.len() + 1); - self.stateful_command_permission = - TimedPermission::granted(now, STATEFUL_COMMAND_TIMEOUT_DURATION); - self.stateful_command_type = Some(StatefulCommand::GetAssertion(AssertionState { + let number_of_credentials = Some(next_credential_keys.len() + 1); + let assertion_state = StatefulCommand::GetAssertion(Box::new(AssertionState { assertion_input: assertion_input.clone(), - next_credentials: applicable_credentials, + next_credential_keys, })); + self.stateful_command_permission + .set_command(now, assertion_state, channel); number_of_credentials }; - self.assertion_response(credential, assertion_input, number_of_credentials) + self.assertion_response( + env, + credential, + assertion_input, + number_of_credentials, + false, + ) } fn process_get_next_assertion( &mut self, - _now: ClockValue, + env: &mut impl Env, ) -> Result { - //self.check_command_permission(now)?; - if crate::fido::request_permission_blocking( - t!("vault.fido.next_assertion", xous::LANG).to_string(), - [0xff, 0xff, 0xff, 0xff] - ).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED); - } - let (assertion_input, credential) = - if let Some(StatefulCommand::GetAssertion(assertion_state)) = - &mut self.stateful_command_type - { - let credential = assertion_state - .next_credentials - .pop() - .ok_or(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)?; - (assertion_state.assertion_input.clone(), credential) - } else { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED); - }; - self.assertion_response(credential, assertion_input, None) + let (assertion_input, credential_key) = self + .stateful_command_permission + .next_assertion_credential()?; + let credential = storage::get_credential(env, credential_key)?; + self.assertion_response(env, credential, assertion_input, None, true) } - fn process_get_info(&self) -> Result { - let mut options_map = BTreeMap::new(); - // TODO(kaczmarczyck) add authenticatorConfig and credProtect options - options_map.insert(String::from("rk"), true); - options_map.insert(String::from("up"), true); - options_map.insert( - String::from("clientPin"), - self.persistent_store.pin_hash()?.is_some(), - ); + fn process_get_info(&self, env: &mut impl Env) -> Result { + let has_always_uv = storage::has_always_uv(env)?; + #[cfg_attr(not(feature = "with_ctap1"), allow(unused_mut))] + let mut versions = vec![ + String::from(FIDO2_VERSION_STRING), + String::from(FIDO2_1_VERSION_STRING), + ]; + #[cfg(feature = "with_ctap1")] + if !has_always_uv { + versions.insert(0, String::from(U2F_VERSION_STRING)) + } + let mut options = vec![]; + if env.customization().enterprise_attestation_mode().is_some() { + options.push((String::from("ep"), storage::enterprise_attestation(env)?)); + } + options.append(&mut vec![ + (String::from("rk"), true), + (String::from("up"), true), + (String::from("alwaysUv"), has_always_uv), + (String::from("credMgmt"), true), + (String::from("authnrCfg"), true), + (String::from("clientPin"), storage::pin_hash(env)?.is_some()), + (String::from("largeBlobs"), true), + (String::from("pinUvAuthToken"), true), + (String::from("setMinPINLength"), true), + (String::from("makeCredUvNotRqd"), !has_always_uv), + ]); + let mut pin_protocols = vec![PinUvAuthProtocol::V2 as u64]; + if env.customization().allows_pin_protocol_v1() { + pin_protocols.push(PinUvAuthProtocol::V1 as u64); + } + Ok(ResponseData::AuthenticatorGetInfo( AuthenticatorGetInfoResponse { - versions: vec![ - #[cfg(feature = "with_ctap1")] - String::from(U2F_VERSION_STRING), - String::from(FIDO2_VERSION_STRING), - #[cfg(feature = "with_ctap2_1")] - String::from(FIDO2_1_VERSION_STRING), - ], - extensions: Some(vec![String::from("hmac-secret")]), - aaguid: self.persistent_store.aaguid()?, - options: Some(options_map), - max_msg_size: Some(1024), - pin_protocols: Some(vec![ - CtapState::::PIN_PROTOCOL_VERSION, + versions, + extensions: Some(vec![ + String::from("hmac-secret"), + String::from("credProtect"), + String::from("minPinLength"), + String::from("credBlob"), + String::from("largeBlobKey"), ]), - #[cfg(feature = "with_ctap2_1")] - max_credential_count_in_list: MAX_CREDENTIAL_COUNT_IN_LIST.map(|c| c as u64), - // #TODO(106) update with version 2.1 of HMAC-secret - #[cfg(feature = "with_ctap2_1")] - max_credential_id_length: Some(CREDENTIAL_ID_SIZE as u64), - #[cfg(feature = "with_ctap2_1")] + aaguid: storage::aaguid(env)?, + options: Some(options), + max_msg_size: Some(env.customization().max_msg_size() as u64), + // The order implies preference. We favor the new V2. + pin_protocols: Some(pin_protocols), + max_credential_count_in_list: env + .customization() + .max_credential_count_in_list() + .map(|c| c as u64), + max_credential_id_length: Some(MAX_CREDENTIAL_ID_SIZE as u64), transports: Some(vec![AuthenticatorTransport::Usb]), - #[cfg(feature = "with_ctap2_1")] - algorithms: Some(vec![ES256_CRED_PARAM]), - default_cred_protect: DEFAULT_CRED_PROTECT.is_some(), - #[cfg(feature = "with_ctap2_1")] - min_pin_length: self.persistent_store.min_pin_length()?, - #[cfg(feature = "with_ctap2_1")] - firmware_version: None, + algorithms: Some(SUPPORTED_CRED_PARAMS.to_vec()), + max_serialized_large_blob_array: Some( + env.customization().max_large_blob_array_size() as u64, + ), + force_pin_change: Some(storage::has_force_pin_change(env)?), + min_pin_length: storage::min_pin_length(env)?, + firmware_version: env.upgrade_storage().map(|u| u.running_firmware_version()), + max_cred_blob_length: Some(env.customization().max_cred_blob_length() as u64), + max_rp_ids_for_set_min_pin_length: Some( + env.customization().max_rp_ids_length() as u64 + ), + certifications: None, + remaining_discoverable_credentials: Some( + storage::remaining_credentials(env)? as u64 + ), }, )) } - fn process_client_pin( - &mut self, - client_pin_params: AuthenticatorClientPinParameters, - ) -> Result { - self.pin_protocol_v1.process_subcommand( - self.rng, - &mut self.persistent_store, - client_pin_params, - ) - } - - pub fn unplug_reset(&mut self) { - self.pin_protocol_v1.reset(self.rng); - } fn process_reset( &mut self, - cid: ChannelID, - _now: ClockValue, + env: &mut impl Env, + channel: Channel, ) -> Result { - // Resets are only possible in the first 10 seconds after booting. - // TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? - // self.check_command_permission(now)?; - if let Some(c) = crate::fido::request_permission_blocking(t!("vault.u2f.factoryreset", xous::LANG).to_string(), cid) { - if c != 'y' { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) - } - } else { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) - } - #[cfg(all(not(feature="autotest"), not(feature="multireset")))] // skip this check on autotest - match &self.stateful_command_type { - Some(StatefulCommand::Reset) => (), + match self.stateful_command_permission.get_command()? { + StatefulCommand::Reset => (), _ => return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED), } - //(self.check_user_presence)(cid)?; - // we skip the double-confirmation during autotesting, but leave it here for production - #[cfg(not(feature="autotest"))] - if crate::fido::request_permission_blocking(t!("vault.u2f.reset_check", xous::LANG).to_string(), cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + #[cfg(feature="xous")] + { + check_user_presence(env, channel, Some( + format!("{}\n{:x?}", + t!("vault.u2f.factoryreset", xous::LANG), + channel + ) + ))?; } + #[cfg(not(feature="xous"))] + check_user_presence(env, channel)?; - self.persistent_store.reset(self.rng)?; - self.pin_protocol_v1.reset(self.rng); + storage::reset(env)?; + self.client_pin.reset(env.rng()); #[cfg(feature = "with_ctap1")] { - self.u2f_up_state = U2fUserPresenceState::new( - U2F_UP_PROMPT_TIMEOUT, - Duration::from_ms(TOUCH_TIMEOUT_MS), - ); + // We create a block statement to wrap this assignment expression, because attributes + // (like #[cfg]) are not supported on expressions. + self.u2f_up_state = U2fUserPresenceState::new(U2F_UP_PROMPT_TIMEOUT, TOUCH_TIMEOUT); } Ok(ResponseData::AuthenticatorReset) } - #[cfg(feature = "with_ctap2_1")] - fn process_selection(&self, cid: ChannelID) -> Result { - //(self.check_user_presence)(cid)?; - if crate::fido::request_permission_blocking(t!("vault.u2f.authenticator_selection", xous::LANG).to_string(), cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + fn process_selection( + &self, + env: &mut impl Env, + channel: Channel, + ) -> Result { + #[cfg(feature="xous")] + { + check_user_presence(env, channel, Some( + format!("{}\n{:x?}", + t!("vault.u2f.authenticator_selection", xous::LANG), + channel + ) + ))?; } + #[cfg(not(feature="xous"))] + check_user_presence(env, channel)?; Ok(ResponseData::AuthenticatorSelection) } fn process_vendor_configure( &mut self, + env: &mut impl Env, params: AuthenticatorVendorConfigureParameters, - cid: ChannelID, + channel: Channel, ) -> Result { - // (self.check_user_presence)(cid)?; - if crate::fido::request_permission_blocking(t!("vault.u2f.vendor_configure", xous::LANG).to_string(), cid).is_none() { - return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + if params.attestation_material.is_some() || params.lockdown { + #[cfg(feature="xous")] + { + check_user_presence(env, channel, Some( + format!("{}\n{:x?}", + t!("vault.u2f.vendor_configure", xous::LANG), + channel + ) + ))?; + } + #[cfg(not(feature="xous"))] + check_user_presence(env, channel)?; } + // This command is for U2F support and we use the batch attestation there. + let attestation_id = attestation_store::Id::Batch; // Sanity checks - let current_priv_key = self.persistent_store.attestation_private_key()?; - let current_cert = self.persistent_store.attestation_certificate()?; - + let current_attestation = env.attestation_store().get(&attestation_id)?; let response = match params.attestation_material { - // Only reading values. - None => AuthenticatorVendorResponse { - cert_programmed: current_cert.is_some(), - pkey_programmed: current_priv_key.is_some(), + None => AuthenticatorVendorConfigureResponse { + cert_programmed: current_attestation.is_some(), + pkey_programmed: current_attestation.is_some(), }, - // Device is already fully programmed. We don't leak information. - Some(_) if current_cert.is_some() && current_priv_key.is_some() => { - AuthenticatorVendorResponse { - cert_programmed: true, - pkey_programmed: true, - } - } - // Device is partially or not programmed. We complete the process. Some(data) => { - if let Some(current_cert) = ¤t_cert { - if current_cert != &data.certificate { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } + // We don't overwrite the attestation if it's already set. We don't return any error + // to not leak information. + if current_attestation.is_none() { + let attestation = Attestation { + private_key: data.private_key, + certificate: data.certificate, + }; + env.attestation_store() + .set(&attestation_id, Some(&attestation))?; } - if let Some(current_priv_key) = ¤t_priv_key { - if current_priv_key != &data.private_key { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - } - if current_cert.is_none() { - self.persistent_store - .set_attestation_certificate(&data.certificate)?; - } - if current_priv_key.is_none() { - self.persistent_store - .set_attestation_private_key(&data.private_key)?; - } - AuthenticatorVendorResponse { + AuthenticatorVendorConfigureResponse { cert_programmed: true, pkey_programmed: true, } @@ -1049,113 +1484,220 @@ where #[cfg(feature = "with_ctap1")] let need_certificate = true; #[cfg(not(feature = "with_ctap1"))] - let need_certificate = USE_BATCH_ATTESTATION; + let need_certificate = env.customization().use_batch_attestation(); - let pddb = pddb::Pddb::new(); - // Note that in the OpenSK implementation, this command both causes the - // token to enter a locked down state, and confirms it has entered that state. - // Here, we only query if the user has put the device into a locked down state. - // Perhaps in the future, this should pop up a modal dialog box requesting the - // user to approve a full device lockdown. if (need_certificate && !(response.pkey_programmed && response.cert_programmed)) - || pddb.is_efuse_secured() + || !env.firmware_protection().lock() { return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); } } - Ok(ResponseData::AuthenticatorVendor(response)) + Ok(ResponseData::AuthenticatorVendorConfigure(response)) + } + + fn process_vendor_upgrade( + &mut self, + env: &mut impl Env, + params: AuthenticatorVendorUpgradeParameters, + ) -> Result { + let AuthenticatorVendorUpgradeParameters { offset, data, hash } = params; + let calculated_hash = Sha256::hash(&data); + if hash != calculated_hash { + return Err(Ctap2StatusCode::CTAP2_ERR_INTEGRITY_FAILURE); + } + env.upgrade_storage() + .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND)? + .write_bundle(offset, data) + .map_err(|_| Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)?; + Ok(ResponseData::AuthenticatorVendorUpgrade) + } + + fn process_vendor_upgrade_info( + &self, + env: &mut impl Env, + ) -> Result { + let upgrade_locations = env + .upgrade_storage() + .ok_or(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND)?; + Ok(ResponseData::AuthenticatorVendorUpgradeInfo( + AuthenticatorVendorUpgradeInfoResponse { + info: upgrade_locations.bundle_identifier(), + }, + )) } pub fn generate_auth_data( &self, + env: &mut impl Env, rp_id_hash: &[u8], flag_byte: u8, ) -> Result, Ctap2StatusCode> { let mut auth_data = vec![]; auth_data.extend(rp_id_hash); auth_data.push(flag_byte); - // The global counter is only increased if USE_SIGNATURE_COUNTER is true. + // The global counter is only increased if use_signature_counter() is true. // It uses a big-endian representation. let mut signature_counter = [0u8; 4]; BigEndian::write_u32( &mut signature_counter, - self.persistent_store.global_signature_counter()?, + storage::global_signature_counter(env)?, ); auth_data.extend(&signature_counter); Ok(auth_data) } + + #[cfg(feature = "with_ctap1")] + pub fn u2f_grant_user_presence(&mut self, now: Instant) { + self.u2f_up_state.grant_up(now) + } + + #[cfg(feature = "with_ctap1")] + pub fn u2f_needs_user_presence(&mut self, now: Instant) -> bool { + self.u2f_up_state.is_up_needed(now) + } } #[cfg(test)] mod test { - use super::command::AuthenticatorAttestationMaterial; - use super::data_formats::{ - CoseKey, GetAssertionExtensions, GetAssertionOptions, MakeCredentialExtensions, - MakeCredentialOptions, PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity, + use super::client_pin::PIN_TOKEN_LENGTH; + use super::command::{ + AuthenticatorAttestationMaterial, AuthenticatorClientPinParameters, + AuthenticatorCredentialManagementParameters, }; + use super::credential_id::CBOR_CREDENTIAL_ID_SIZE; + use super::data_formats::{ + ClientPinSubCommand, CoseKey, CredentialManagementSubCommand, GetAssertionHmacSecretInput, + GetAssertionOptions, MakeCredentialExtensions, MakeCredentialOptions, PinUvAuthProtocol, + PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity, + }; + use super::pin_protocol::{authenticate_pin_uv_auth_token, PinProtocol}; use super::*; - use cbor::cbor_array; - use ctap_crypto::rng256::ThreadRng256; + use crate::api::customization; + use crate::api::user_presence::UserPresenceResult; + use crate::env::test::TestEnv; + use crate::test_helpers; + use cbor::{cbor_array, cbor_array_vec, cbor_map}; - const CLOCK_FREQUENCY_HZ: usize = 32768; - const DUMMY_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); // The keep-alive logic in the processing of some commands needs a channel ID to send // keep-alive packets to. // In tests where we define a dummy user-presence check that immediately returns, the channel // ID is irrelevant, so we pass this (dummy but valid) value. - const DUMMY_CHANNEL_ID: ChannelID = [0x12, 0x34, 0x56, 0x78]; + const DUMMY_CHANNEL: Channel = Channel::MainHid([0x12, 0x34, 0x56, 0x78]); + #[cfg(feature = "vendor_hid")] + const VENDOR_CHANNEL: Channel = Channel::VendorHid([0x12, 0x34, 0x56, 0x78]); + + fn check_make_response( + make_credential_response: &Result, + flags: u8, + expected_aaguid: &[u8], + expected_credential_id_size: u8, + expected_extension_cbor: &[u8], + ) { + match make_credential_response.as_ref().unwrap() { + ResponseData::AuthenticatorMakeCredential(make_credential_response) => { + let AuthenticatorMakeCredentialResponse { + fmt, + auth_data, + att_stmt, + ep_att, + large_blob_key, + } = make_credential_response; + // The expected response is split to only assert the non-random parts. + assert_eq!(fmt, "packed"); + let mut expected_auth_data = vec![ + 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, + 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, + 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, flags, 0x00, 0x00, 0x00, + ]; + expected_auth_data.push(INITIAL_SIGNATURE_COUNTER as u8); + expected_auth_data.extend(expected_aaguid); + expected_auth_data.extend(&[0x00, expected_credential_id_size]); + assert_eq!( + auth_data[0..expected_auth_data.len()], + expected_auth_data[..] + ); + assert_eq!( + &auth_data[auth_data.len() - expected_extension_cbor.len()..auth_data.len()], + expected_extension_cbor + ); + assert!(ep_att.is_none()); + assert_eq!(att_stmt.alg, SignatureAlgorithm::Es256 as i64); + assert_eq!(large_blob_key, &None); + } + _ => panic!("Invalid response type"), + } + } #[test] fn test_get_info() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - let info_reponse = ctap_state.process_command(&[0x04], DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let info_reponse = + ctap_state.process_command(&mut env, &[0x04], DUMMY_CHANNEL, Instant::new(0)); - #[cfg(feature = "with_ctap2_1")] - let mut expected_response = vec![0x00, 0xAA, 0x01]; - #[cfg(not(feature = "with_ctap2_1"))] - let mut expected_response = vec![0x00, 0xA6, 0x01]; - // The difference here is a longer array of supported versions. - let mut version_count = 0; - // CTAP 2 is always supported - version_count += 1; - #[cfg(feature = "with_ctap1")] - { - version_count += 1; - } - #[cfg(feature = "with_ctap2_1")] - { - version_count += 1; - } - expected_response.push(0x80 + version_count); - #[cfg(feature = "with_ctap1")] - expected_response.extend(&[0x66, 0x55, 0x32, 0x46, 0x5F, 0x56, 0x32]); - expected_response.extend(&[0x68, 0x46, 0x49, 0x44, 0x4F, 0x5F, 0x32, 0x5F, 0x30]); - #[cfg(feature = "with_ctap2_1")] - expected_response.extend(&[ - 0x6C, 0x46, 0x49, 0x44, 0x4F, 0x5F, 0x32, 0x5F, 0x31, 0x5F, 0x50, 0x52, 0x45, - ]); - expected_response.extend(&[ - 0x02, 0x81, 0x6B, 0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, - 0x03, 0x50, - ]); - expected_response.extend(&ctap_state.persistent_store.aaguid().unwrap()); - expected_response.extend(&[ - 0x04, 0xA3, 0x62, 0x72, 0x6B, 0xF5, 0x62, 0x75, 0x70, 0xF5, 0x69, 0x63, 0x6C, 0x69, - 0x65, 0x6E, 0x74, 0x50, 0x69, 0x6E, 0xF4, 0x05, 0x19, 0x04, 0x00, 0x06, 0x81, 0x01, - ]); - #[cfg(feature = "with_ctap2_1")] - expected_response.extend( - [ - 0x08, 0x18, 0x70, 0x09, 0x81, 0x63, 0x75, 0x73, 0x62, 0x0A, 0x81, 0xA2, 0x63, 0x61, - 0x6C, 0x67, 0x26, 0x64, 0x74, 0x79, 0x70, 0x65, 0x6A, 0x70, 0x75, 0x62, 0x6C, 0x69, - 0x63, 0x2D, 0x6B, 0x65, 0x79, 0x0D, 0x04, - ] - .iter(), - ); + let expected_cbor = cbor_map_options! { + 0x01 => cbor_array_vec![vec![ + #[cfg(feature = "with_ctap1")] + String::from(U2F_VERSION_STRING), + String::from(FIDO2_VERSION_STRING), + String::from(FIDO2_1_VERSION_STRING), + ]], + 0x02 => cbor_array![ + String::from("hmac-secret"), + String::from("credProtect"), + String::from("minPinLength"), + String::from("credBlob"), + String::from("largeBlobKey"), + ], + 0x03 => storage::aaguid(&mut env).unwrap(), + 0x04 => cbor_map_options! { + "ep" => env.customization().enterprise_attestation_mode().map(|_| false), + "rk" => true, + "up" => true, + "alwaysUv" => false, + "credMgmt" => true, + "authnrCfg" => true, + "clientPin" => false, + "largeBlobs" => true, + "pinUvAuthToken" => true, + "setMinPINLength" => true, + "makeCredUvNotRqd" => true, + }, + 0x05 => env.customization().max_msg_size() as u64, + 0x06 => cbor_array![2, 1], + 0x07 => env.customization().max_credential_count_in_list().map(|c| c as u64), + 0x08 => MAX_CREDENTIAL_ID_SIZE as u64, + 0x09 => cbor_array!["usb"], + 0x0A => cbor_array_vec!(SUPPORTED_CRED_PARAMS.to_vec()), + 0x0B => env.customization().max_large_blob_array_size() as u64, + 0x0C => false, + 0x0D => storage::min_pin_length(&mut env).unwrap() as u64, + 0x0E => 0, + 0x0F => env.customization().max_cred_blob_length() as u64, + 0x10 => env.customization().max_rp_ids_length() as u64, + 0x14 => storage::remaining_credentials(&mut env).unwrap() as u64, + }; - assert_eq!(info_reponse, expected_response); + let mut response_cbor = vec![0x00]; + assert!(cbor_write(expected_cbor, &mut response_cbor).is_ok()); + assert_eq!(info_reponse, response_cbor); + } + + #[test] + fn test_get_info_no_pin_protocol_v1() { + let mut env = TestEnv::new(); + env.customization_mut().set_allows_pin_protocol_v1(false); + let ctap_state = CtapState::new(&mut env, Instant::new(0)); + let info_response = ctap_state.process_get_info(&mut env).unwrap(); + match info_response { + ResponseData::AuthenticatorGetInfo(response) => { + assert_eq!( + response.pin_protocols, + Some(vec![PinUvAuthProtocol::V2 as u64]) + ); + } + _ => panic!("Invalid response type"), + } } fn create_minimal_make_credential_parameters() -> AuthenticatorMakeCredentialParameters { @@ -1182,10 +1724,11 @@ mod test { user, pub_key_cred_params, exclude_list: None, - extensions: None, + extensions: MakeCredentialExtensions::default(), options, pin_uv_auth_param: None, pin_uv_auth_protocol: None, + enterprise_attestation: None, } } @@ -1206,100 +1749,77 @@ mod test { fn create_make_credential_parameters_with_cred_protect_policy( policy: CredentialProtectionPolicy, ) -> AuthenticatorMakeCredentialParameters { - let extensions = Some(MakeCredentialExtensions { - hmac_secret: false, + let extensions = MakeCredentialExtensions { cred_protect: Some(policy), - }); + ..Default::default() + }; let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.extensions = extensions; make_credential_params } - #[test] - fn test_residential_process_make_credential() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - - let make_credential_params = create_minimal_make_credential_parameters(); - let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); - - match make_credential_response.unwrap() { + fn parse_credential_id_from_non_resident_make_credential_response( + env: &mut impl Env, + make_credential_response: ResponseData, + ) -> Vec { + match make_credential_response { ResponseData::AuthenticatorMakeCredential(make_credential_response) => { - let AuthenticatorMakeCredentialResponse { - fmt, - auth_data, - att_stmt, - } = make_credential_response; - // The expected response is split to only assert the non-random parts. - assert_eq!(fmt, "packed"); - let mut expected_auth_data = vec![ - 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, - 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, - 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, 0x41, 0x00, 0x00, 0x00, - ]; - expected_auth_data.push(INITIAL_SIGNATURE_COUNTER as u8); - expected_auth_data.extend(&ctap_state.persistent_store.aaguid().unwrap()); - expected_auth_data.extend(&[0x00, 0x20]); - assert_eq!( - auth_data[0..expected_auth_data.len()], - expected_auth_data[..] - ); - assert_eq!(att_stmt.alg, SignatureAlgorithm::ES256 as i64); + let auth_data = make_credential_response.auth_data; + let offset = 37 + storage::aaguid(env).unwrap().len(); + assert_eq!(auth_data[offset], 0x00); + assert_eq!(auth_data[offset + 1] as usize, CBOR_CREDENTIAL_ID_SIZE); + auth_data[offset + 2..offset + 2 + CBOR_CREDENTIAL_ID_SIZE].to_vec() } _ => panic!("Invalid response type"), } } #[test] - fn test_non_residential_process_make_credential() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + fn test_resident_process_make_credential() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let make_credential_params = create_minimal_make_credential_parameters(); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + + check_make_response( + &make_credential_response, + 0x41, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &[], + ); + } + + #[test] + fn test_non_resident_process_make_credential() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.options.rk = false; let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); - match make_credential_response.unwrap() { - ResponseData::AuthenticatorMakeCredential(make_credential_response) => { - let AuthenticatorMakeCredentialResponse { - fmt, - auth_data, - att_stmt, - } = make_credential_response; - // The expected response is split to only assert the non-random parts. - assert_eq!(fmt, "packed"); - let mut expected_auth_data = vec![ - 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, - 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, - 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, 0x41, 0x00, 0x00, 0x00, - ]; - expected_auth_data.push(INITIAL_SIGNATURE_COUNTER as u8); - expected_auth_data.extend(&ctap_state.persistent_store.aaguid().unwrap()); - expected_auth_data.extend(&[0x00, CREDENTIAL_ID_SIZE as u8]); - assert_eq!( - auth_data[0..expected_auth_data.len()], - expected_auth_data[..] - ); - assert_eq!(att_stmt.alg, SignatureAlgorithm::ES256 as i64); - } - _ => panic!("Invalid response type"), - } + check_make_response( + &make_credential_response, + 0x41, + &storage::aaguid(&mut env).unwrap(), + CBOR_CREDENTIAL_ID_SIZE as u8, + &[], + ); } #[test] fn test_process_make_credential_unsupported_algorithm() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.pub_key_cred_params = vec![]; let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert_eq!( make_credential_response, @@ -1309,10 +1829,9 @@ mod test { #[test] fn test_process_make_credential_credential_excluded() { - let mut rng = ThreadRng256 {}; - let excluded_private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let excluded_private_key = PrivateKey::new_ecdsa(&mut env); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let excluded_credential_id = vec![0x01, 0x23, 0x45, 0x67]; let make_credential_params = @@ -1328,46 +1847,43 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; - assert!(ctap_state - .persistent_store - .store_credential(excluded_credential_source) - .is_ok()); + assert!(storage::store_credential(&mut env, excluded_credential_source).is_ok()); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert_eq!( make_credential_response, Err(Ctap2StatusCode::CTAP2_ERR_CREDENTIAL_EXCLUDED) ); } - #[test] // this exhibits compilation errors in some configs + #[test] fn test_process_make_credential_credential_with_cred_protect() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let test_policy = CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList; let make_credential_params = create_make_credential_parameters_with_cred_protect_policy(test_policy); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert!(make_credential_response.is_ok()); - let stored_credential = ctap_state - .persistent_store - .filter_credential("example.com", false) - .unwrap() - .pop() - .unwrap(); + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(&mut env, &mut iter_result).unwrap(); + // There is only 1 credential, so last is good enough. + let (_, stored_credential) = iter.last().unwrap(); + iter_result.unwrap(); let credential_id = stored_credential.credential_id; assert_eq!(stored_credential.cred_protect_policy, Some(test_policy)); let make_credential_params = create_make_credential_parameters_with_exclude_list(&credential_id); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert_eq!( make_credential_response, Err(Ctap2StatusCode::CTAP2_ERR_CREDENTIAL_EXCLUDED) @@ -1377,136 +1893,508 @@ mod test { let make_credential_params = create_make_credential_parameters_with_cred_protect_policy(test_policy); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert!(make_credential_response.is_ok()); - let stored_credential = ctap_state - .persistent_store - .filter_credential("example.com", false) - .unwrap() - .pop() - .unwrap(); + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(&mut env, &mut iter_result).unwrap(); + // There is only 1 credential, so last is good enough. + let (_, stored_credential) = iter.last().unwrap(); + iter_result.unwrap(); let credential_id = stored_credential.credential_id; assert_eq!(stored_credential.cred_protect_policy, Some(test_policy)); let make_credential_params = create_make_credential_parameters_with_exclude_list(&credential_id); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert!(make_credential_response.is_ok()); + } + + #[test] + fn test_non_resident_process_make_credential_credential_with_cred_protect() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let test_policy = CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList; + let mut make_credential_params = + create_make_credential_parameters_with_cred_protect_policy(test_policy); + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert!(make_credential_response.is_ok()); + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); + let make_credential_params = + create_make_credential_parameters_with_exclude_list(&credential_id); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_CREDENTIAL_EXCLUDED) + ); + + let test_policy = CredentialProtectionPolicy::UserVerificationRequired; + let mut make_credential_params = + create_make_credential_parameters_with_cred_protect_policy(test_policy); + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert!(make_credential_response.is_ok()); + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); + let make_credential_params = + create_make_credential_parameters_with_exclude_list(&credential_id); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert!(make_credential_response.is_ok()); } #[test] fn test_process_make_credential_hmac_secret() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let extensions = Some(MakeCredentialExtensions { + let extensions = MakeCredentialExtensions { hmac_secret: true, - cred_protect: None, - }); + ..Default::default() + }; let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.options.rk = false; make_credential_params.extensions = extensions; let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); - match make_credential_response.unwrap() { - ResponseData::AuthenticatorMakeCredential(make_credential_response) => { - let AuthenticatorMakeCredentialResponse { - fmt, - auth_data, - att_stmt, - } = make_credential_response; - // The expected response is split to only assert the non-random parts. - assert_eq!(fmt, "packed"); - let mut expected_auth_data = vec![ - 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, - 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, - 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, 0xC1, 0x00, 0x00, 0x00, - ]; - expected_auth_data.push(INITIAL_SIGNATURE_COUNTER as u8); - expected_auth_data.extend(&ctap_state.persistent_store.aaguid().unwrap()); - expected_auth_data.extend(&[0x00, CREDENTIAL_ID_SIZE as u8]); - assert_eq!( - auth_data[0..expected_auth_data.len()], - expected_auth_data[..] - ); - let expected_extension_cbor = vec![ - 0xA1, 0x6B, 0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, - 0xF5, - ]; - assert_eq!( - auth_data[auth_data.len() - expected_extension_cbor.len()..auth_data.len()], - expected_extension_cbor[..] - ); - assert_eq!(att_stmt.alg, SignatureAlgorithm::ES256 as i64); - } - _ => panic!("Invalid response type"), - } + let expected_extension_cbor = [ + 0xA1, 0x6B, 0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xF5, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + CBOR_CREDENTIAL_ID_SIZE as u8, + &expected_extension_cbor, + ); } #[test] fn test_process_make_credential_hmac_secret_resident_key() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let extensions = Some(MakeCredentialExtensions { + let extensions = MakeCredentialExtensions { hmac_secret: true, - cred_protect: None, - }); + ..Default::default() + }; let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.extensions = extensions; let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let expected_extension_cbor = [ + 0xA1, 0x6B, 0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xF5, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &expected_extension_cbor, + ); + } + + #[test] + fn test_process_make_credential_min_pin_length() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + // First part: The extension is ignored, since the RP ID is not on the list. + let extensions = MakeCredentialExtensions { + min_pin_length: true, + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_make_response( + &make_credential_response, + 0x41, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &[], + ); + + // Second part: The extension is used. + assert_eq!( + storage::set_min_pin_length_rp_ids(&mut env, vec!["example.com".to_string()]), + Ok(()) + ); + + let extensions = MakeCredentialExtensions { + min_pin_length: true, + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let expected_extension_cbor = [ + 0xA1, 0x6C, 0x6D, 0x69, 0x6E, 0x50, 0x69, 0x6E, 0x4C, 0x65, 0x6E, 0x67, 0x74, 0x68, + 0x04, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &expected_extension_cbor, + ); + } + + #[test] + fn test_process_make_credential_cred_blob_ok() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let extensions = MakeCredentialExtensions { + cred_blob: Some(vec![0xCB]), + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let expected_extension_cbor = [ + 0xA1, 0x68, 0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, 0xF5, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &expected_extension_cbor, + ); + + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(&mut env, &mut iter_result).unwrap(); + // There is only 1 credential, so last is good enough. + let (_, stored_credential) = iter.last().unwrap(); + iter_result.unwrap(); + assert_eq!(stored_credential.cred_blob, Some(vec![0xCB])); + } + + #[test] + fn test_process_make_credential_cred_blob_too_big() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let extensions = MakeCredentialExtensions { + cred_blob: Some(vec![0xCB; env.customization().max_cred_blob_length() + 1]), + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let expected_extension_cbor = [ + 0xA1, 0x68, 0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, 0xF4, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &expected_extension_cbor, + ); + + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(&mut env, &mut iter_result).unwrap(); + // There is only 1 credential, so last is good enough. + let (_, stored_credential) = iter.last().unwrap(); + iter_result.unwrap(); + assert_eq!(stored_credential.cred_blob, None); + } + + #[test] + fn test_process_make_credential_large_blob_key() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let extensions = MakeCredentialExtensions { + large_blob_key: Some(true), + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let large_blob_key = match make_credential_response.unwrap() { + ResponseData::AuthenticatorMakeCredential(make_credential_response) => { + make_credential_response.large_blob_key.unwrap() + } + _ => panic!("Invalid response type"), + }; + assert_eq!(large_blob_key.len(), 32); + + let mut iter_result = Ok(()); + let iter = storage::iter_credentials(&mut env, &mut iter_result).unwrap(); + // There is only 1 credential, so last is good enough. + let (_, stored_credential) = iter.last().unwrap(); + iter_result.unwrap(); + assert_eq!(stored_credential.large_blob_key.unwrap(), large_blob_key); + } + + fn test_helper_process_make_credential_with_pin_and_uv( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x91; PIN_TOKEN_LENGTH]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + + let client_data_hash = [0xCD]; + let pin_uv_auth_param = authenticate_pin_uv_auth_token( + &pin_uv_auth_token, + &client_data_hash, + pin_uv_auth_protocol, + ); + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.options.uv = true; + make_credential_params.pin_uv_auth_param = Some(pin_uv_auth_param); + make_credential_params.pin_uv_auth_protocol = Some(pin_uv_auth_protocol); + let make_credential_response = ctap_state.process_make_credential( + &mut env, + make_credential_params.clone(), + DUMMY_CHANNEL, + ); + + check_make_response( + &make_credential_response, + 0x45, + &storage::aaguid(&mut env).unwrap(), + 0x20, + &[], + ); + + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ) + } + + #[test] + fn test_process_make_credential_with_pin_and_uv_v1() { + test_helper_process_make_credential_with_pin_and_uv(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_make_credential_with_pin_and_uv_v2() { + test_helper_process_make_credential_with_pin_and_uv(PinUvAuthProtocol::V2); + } + + #[test] + fn test_non_resident_process_make_credential_with_pin() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + + check_make_response( + &make_credential_response, + 0x41, + &storage::aaguid(&mut env).unwrap(), + CBOR_CREDENTIAL_ID_SIZE as u8, + &[], + ); + } + + #[test] + fn test_resident_process_make_credential_with_pin() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + + let make_credential_params = create_minimal_make_credential_parameters(); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED) + ); + } + + #[test] + fn test_process_make_credential_with_pin_always_uv() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + storage::toggle_always_uv(&mut env).unwrap(); + let make_credential_params = create_minimal_make_credential_parameters(); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_PUAT_REQUIRED) + ); + + storage::set_pin(&mut env, &[0x88; 16], 4).unwrap(); + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.pin_uv_auth_param = Some(vec![0xA4; 16]); + make_credential_params.pin_uv_auth_protocol = Some(PinUvAuthProtocol::V1); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + fn check_ep(make_credential_response: Result, has_ep: bool) { + let ep_att = if has_ep { Some(true) } else { None }; match make_credential_response.unwrap() { ResponseData::AuthenticatorMakeCredential(make_credential_response) => { - let AuthenticatorMakeCredentialResponse { - fmt, - auth_data, - att_stmt, - } = make_credential_response; - // The expected response is split to only assert the non-random parts. - assert_eq!(fmt, "packed"); - let mut expected_auth_data = vec![ - 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, - 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, - 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, 0xC1, 0x00, 0x00, 0x00, - ]; - expected_auth_data.push(INITIAL_SIGNATURE_COUNTER as u8); - expected_auth_data.extend(&ctap_state.persistent_store.aaguid().unwrap()); - expected_auth_data.extend(&[0x00, 0x20]); - assert_eq!( - auth_data[0..expected_auth_data.len()], - expected_auth_data[..] - ); - let expected_extension_cbor = vec![ - 0xA1, 0x6B, 0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, - 0xF5, - ]; - assert_eq!( - auth_data[auth_data.len() - expected_extension_cbor.len()..auth_data.len()], - expected_extension_cbor[..] - ); - assert_eq!(att_stmt.alg, SignatureAlgorithm::ES256 as i64); + assert_eq!(make_credential_response.ep_att, ep_att); } _ => panic!("Invalid response type"), } } + #[test] + fn test_process_make_credential_with_enterprise_attestation_vendor_facilitated() { + let mut env = TestEnv::new(); + env.customization_mut().setup_enterprise_attestation( + Some(EnterpriseAttestationMode::VendorFacilitated), + Some(vec!["example.com".to_string()]), + ); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + test_helpers::enable_enterprise_attestation(&mut ctap_state, &mut env).unwrap(); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(1); + make_credential_params.rp = PublicKeyCredentialRpEntity { + rp_id: "counter-example.com".to_string(), + rp_name: None, + rp_icon: None, + }; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, false); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(2); + make_credential_params.rp = PublicKeyCredentialRpEntity { + rp_id: "counter-example.com".to_string(), + rp_name: None, + rp_icon: None, + }; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, false); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(1); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, true); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(2); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, true); + } + + #[test] + fn test_process_make_credential_with_enterprise_attestation_platform_managed() { + let mut env = TestEnv::new(); + env.customization_mut().setup_enterprise_attestation( + Some(EnterpriseAttestationMode::PlatformManaged), + Some(vec!["example.com".to_string()]), + ); + assert!(customization::is_valid(env.customization())); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + test_helpers::enable_enterprise_attestation(&mut ctap_state, &mut env).unwrap(); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(1); + make_credential_params.rp = PublicKeyCredentialRpEntity { + rp_id: "counter-example.com".to_string(), + rp_name: None, + rp_icon: None, + }; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, false); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(1); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, true); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(2); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + check_ep(make_credential_response, true); + } + + #[test] + fn test_process_make_credential_with_enterprise_attestation_invalid() { + let mut env = TestEnv::new(); + env.customization_mut() + .setup_enterprise_attestation(Some(EnterpriseAttestationMode::PlatformManaged), None); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(2); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) + ); + + test_helpers::enable_enterprise_attestation(&mut ctap_state, &mut env).unwrap(); + + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.enterprise_attestation = Some(3); + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert_eq!( + make_credential_response, + Err(Ctap2StatusCode::CTAP2_ERR_INVALID_OPTION) + ); + } + #[test] fn test_process_make_credential_cancelled() { - let mut rng = ThreadRng256 {}; - let user_presence_always_cancel = |_| Err(Ctap2StatusCode::CTAP2_ERR_KEEPALIVE_CANCEL); - let mut ctap_state = - CtapState::new(&mut rng, user_presence_always_cancel, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence().set(|| Err(UserPresenceError::Canceled)); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let make_credential_params = create_minimal_make_credential_parameters(); let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert_eq!( make_credential_response, @@ -1516,35 +2404,60 @@ mod test { fn check_assertion_response_with_user( response: Result, - expected_user: PublicKeyCredentialUserEntity, + expected_user: Option, flags: u8, signature_counter: u32, expected_number_of_credentials: Option, + expected_extension_cbor: &[u8], ) { - match response.unwrap() { - ResponseData::AuthenticatorGetAssertion(get_assertion_response) => { - let AuthenticatorGetAssertionResponse { - auth_data, - user, - number_of_credentials, - .. - } = get_assertion_response; - let mut expected_auth_data = vec![ - 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, - 0x34, 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, - 0x12, 0x55, 0x86, 0xCE, 0x19, 0x47, flags, 0x00, 0x00, 0x00, 0x00, - ]; - let signature_counter_position = expected_auth_data.len() - 4; - BigEndian::write_u32( - &mut expected_auth_data[signature_counter_position..], - signature_counter, - ); - assert_eq!(auth_data, expected_auth_data); - assert_eq!(user, Some(expected_user)); - assert_eq!(number_of_credentials, expected_number_of_credentials); - } + let assertion_response = match response.unwrap() { + ResponseData::AuthenticatorGetAssertion(r) => r, + ResponseData::AuthenticatorGetNextAssertion(r) => r, _ => panic!("Invalid response type"), - } + }; + let AuthenticatorGetAssertionResponse { + auth_data, + user, + number_of_credentials, + .. + } = assertion_response; + let mut expected_auth_data = vec![ + 0xA3, 0x79, 0xA6, 0xF6, 0xEE, 0xAF, 0xB9, 0xA5, 0x5E, 0x37, 0x8C, 0x11, 0x80, 0x34, + 0xE2, 0x75, 0x1E, 0x68, 0x2F, 0xAB, 0x9F, 0x2D, 0x30, 0xAB, 0x13, 0xD2, 0x12, 0x55, + 0x86, 0xCE, 0x19, 0x47, flags, 0x00, 0x00, 0x00, 0x00, + ]; + let signature_counter_position = expected_auth_data.len() - 4; + BigEndian::write_u32( + &mut expected_auth_data[signature_counter_position..], + signature_counter, + ); + expected_auth_data.extend(expected_extension_cbor); + assert_eq!(auth_data, expected_auth_data); + assert_eq!(user, expected_user); + assert_eq!(number_of_credentials, expected_number_of_credentials); + } + + fn check_assertion_response_with_extension( + response: Result, + expected_user_id: Option>, + signature_counter: u32, + expected_number_of_credentials: Option, + expected_extension_cbor: &[u8], + ) { + let expected_user = expected_user_id.map(|user_id| PublicKeyCredentialUserEntity { + user_id, + user_name: None, + user_display_name: None, + user_icon: None, + }); + check_assertion_response_with_user( + response, + expected_user, + 0x80, + signature_counter, + expected_number_of_credentials, + expected_extension_cbor, + ); } fn check_assertion_response( @@ -1561,29 +2474,29 @@ mod test { }; check_assertion_response_with_user( response, - expected_user, + Some(expected_user), 0x00, signature_counter, expected_number_of_credentials, + &[], ); } #[test] - fn test_residential_process_get_assertion() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + fn test_resident_process_get_assertion() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let make_credential_params = create_minimal_make_credential_parameters(); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: None, - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -1592,147 +2505,194 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); - let signature_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_assertion_response(get_assertion_response, vec![0x1D], signature_counter, None); } - #[test] - fn test_process_get_assertion_hmac_secret() { - let mut rng = ThreadRng256 {}; - let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + fn get_assertion_hmac_secret_params( + key_agreement_key: ctap_crypto::ecdh::SecKey, + key_agreement_response: ResponseData, + credential_id: Option>, + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> AuthenticatorGetAssertionParameters { + let mut env = TestEnv::new(); + let platform_public_key = key_agreement_key.genpk(); + let public_key = match key_agreement_response { + ResponseData::AuthenticatorClientPin(Some(client_pin_response)) => { + client_pin_response.key_agreement.unwrap() + } + _ => panic!("Invalid response type"), + }; + let pin_protocol = PinProtocol::new_test(key_agreement_key, [0x91; 32]); + let shared_secret = pin_protocol + .decapsulate(public_key, pin_uv_auth_protocol) + .unwrap(); - let make_extensions = Some(MakeCredentialExtensions { - hmac_secret: true, - cred_protect: None, + let salt = vec![0x01; 32]; + let salt_enc = shared_secret.as_ref().encrypt(env.rng(), &salt).unwrap(); + let salt_auth = shared_secret.authenticate(&salt_enc); + let hmac_secret_input = GetAssertionHmacSecretInput { + key_agreement: CoseKey::from(platform_public_key), + salt_enc, + salt_auth, + pin_uv_auth_protocol, + }; + let get_extensions = GetAssertionExtensions { + hmac_secret: Some(hmac_secret_input), + ..Default::default() + }; + + let credential_descriptor = credential_id.map(|key_id| PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id, + transports: None, }); + let allow_list = credential_descriptor.map(|c| vec![c]); + AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list, + extensions: get_extensions, + options: GetAssertionOptions { + up: true, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + } + } + + fn test_helper_process_get_assertion_hmac_secret(pin_uv_auth_protocol: PinUvAuthProtocol) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let make_extensions = MakeCredentialExtensions { + hmac_secret: true, + ..Default::default() + }; let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.options.rk = false; make_credential_params.extensions = make_extensions; let make_credential_response = - ctap_state.process_make_credential(make_credential_params, DUMMY_CHANNEL_ID); + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); assert!(make_credential_response.is_ok()); - let credential_id = match make_credential_response.unwrap() { - ResponseData::AuthenticatorMakeCredential(make_credential_response) => { - let auth_data = make_credential_response.auth_data; - let offset = 37 + ctap_state.persistent_store.aaguid().unwrap().len(); - assert_eq!(auth_data[offset], 0x00); - assert_eq!(auth_data[offset + 1] as usize, CREDENTIAL_ID_SIZE); - auth_data[offset + 2..offset + 2 + CREDENTIAL_ID_SIZE].to_vec() - } - _ => panic!("Invalid response type"), - }; + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); - let pk = sk.genpk(); - let hmac_secret_input = GetAssertionHmacSecretInput { - key_agreement: CoseKey::from(pk), - salt_enc: vec![0x02; 32], - salt_auth: vec![0x03; 16], - }; - let get_extensions = Some(GetAssertionExtensions { - hmac_secret: Some(hmac_secret_input), - }); - - let cred_desc = PublicKeyCredentialDescriptor { - key_type: PublicKeyCredentialType::PublicKey, - key_id: credential_id, - transports: None, - }; - let get_assertion_params = AuthenticatorGetAssertionParameters { - rp_id: String::from("example.com"), - client_data_hash: vec![0xCD], - allow_list: Some(vec![cred_desc]), - extensions: get_extensions, - options: GetAssertionOptions { - up: false, - uv: false, - }, + let client_pin_params = AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + sub_command: ClientPinSubCommand::GetKeyAgreement, + key_agreement: None, pin_uv_auth_param: None, - pin_uv_auth_protocol: None, + new_pin_enc: None, + pin_hash_enc: None, + permissions: None, + permissions_rp_id: None, }; + let key_agreement_response = + ctap_state + .client_pin + .process_command(&mut env, client_pin_params, Instant::new(0)); + let get_assertion_params = get_assertion_hmac_secret_params( + key_agreement_key, + key_agreement_response.unwrap(), + Some(credential_id), + pin_uv_auth_protocol, + ); let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, - ); - - assert_eq!( - get_assertion_response, - Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION) + DUMMY_CHANNEL, + Instant::new(0), ); + assert!(get_assertion_response.is_ok()); } #[test] - fn test_residential_process_get_assertion_hmac_secret() { - let mut rng = ThreadRng256 {}; - let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + fn test_process_get_assertion_hmac_secret_v1() { + test_helper_process_get_assertion_hmac_secret(PinUvAuthProtocol::V1); + } - let make_extensions = Some(MakeCredentialExtensions { + #[test] + fn test_process_get_assertion_hmac_secret_v2() { + test_helper_process_get_assertion_hmac_secret(PinUvAuthProtocol::V2); + } + + fn test_helper_resident_process_get_assertion_hmac_secret( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let make_extensions = MakeCredentialExtensions { hmac_secret: true, - cred_protect: None, - }); + ..Default::default() + }; let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.extensions = make_extensions; assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); - let pk = sk.genpk(); - let hmac_secret_input = GetAssertionHmacSecretInput { - key_agreement: CoseKey::from(pk), - salt_enc: vec![0x02; 32], - salt_auth: vec![0x03; 16], - }; - let get_extensions = Some(GetAssertionExtensions { - hmac_secret: Some(hmac_secret_input), - }); - - let get_assertion_params = AuthenticatorGetAssertionParameters { - rp_id: String::from("example.com"), - client_data_hash: vec![0xCD], - allow_list: None, - extensions: get_extensions, - options: GetAssertionOptions { - up: false, - uv: false, - }, + let client_pin_params = AuthenticatorClientPinParameters { + pin_uv_auth_protocol, + sub_command: ClientPinSubCommand::GetKeyAgreement, + key_agreement: None, pin_uv_auth_param: None, - pin_uv_auth_protocol: None, + new_pin_enc: None, + pin_hash_enc: None, + permissions: None, + permissions_rp_id: None, }; + let key_agreement_response = + ctap_state + .client_pin + .process_command(&mut env, client_pin_params, Instant::new(0)); + let get_assertion_params = get_assertion_hmac_secret_params( + key_agreement_key, + key_agreement_response.unwrap(), + None, + pin_uv_auth_protocol, + ); let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, - ); - - assert_eq!( - get_assertion_response, - Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION) + DUMMY_CHANNEL, + Instant::new(0), ); + assert!(get_assertion_response.is_ok()); } #[test] - fn test_residential_process_get_assertion_with_cred_protect() { - let mut rng = ThreadRng256 {}; - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let credential_id = rng.gen_uniform_u8x32().to_vec(); - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + fn test_process_resident_get_assertion_hmac_secret_v1() { + test_helper_resident_process_get_assertion_hmac_secret(PinUvAuthProtocol::V1); + } + + #[test] + fn test_resident_process_get_assertion_hmac_secret_v2() { + test_helper_resident_process_get_assertion_hmac_secret(PinUvAuthProtocol::V2); + } + + #[test] + fn test_resident_process_get_assertion_with_cred_protect() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let credential_id = env.rng().gen_uniform_u8x32().to_vec(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let cred_desc = PublicKeyCredentialDescriptor { key_type: PublicKeyCredentialType::PublicKey, key_id: credential_id.clone(), - transports: None, // You can set USB as a hint here. + transports: None, }; let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, @@ -1747,17 +2707,16 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; - assert!(ctap_state - .persistent_store - .store_credential(credential) - .is_ok()); + assert!(storage::store_credential(&mut env, credential).is_ok()); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: None, - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -1766,9 +2725,10 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); assert_eq!( get_assertion_response, @@ -1779,7 +2739,7 @@ mod test { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: Some(vec![cred_desc.clone()]), - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -1788,14 +2748,12 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); - let signature_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_assertion_response(get_assertion_response, vec![0x1D], signature_counter, None); let credential = PublicKeyCredentialSource { @@ -1809,17 +2767,16 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; - assert!(ctap_state - .persistent_store - .store_credential(credential) - .is_ok()); + assert!(storage::store_credential(&mut env, credential).is_ok()); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: Some(vec![cred_desc]), - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -1828,9 +2785,10 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); assert_eq!( get_assertion_response, @@ -1839,15 +2797,277 @@ mod test { } #[test] - fn test_process_get_next_assertion_two_credentials_with_uv() { - let mut rng = ThreadRng256 {}; - let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(&mut rng); - let pin_uv_auth_token = [0x88; 32]; - let pin_protocol_v1 = PinProtocolV1::new_test(key_agreement_key, pin_uv_auth_token); + fn test_non_resident_process_get_assertion_with_cred_protect() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - ctap_state.pin_protocol_v1 = pin_protocol_v1; + let test_policy = CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList; + let mut make_credential_params = + create_make_credential_parameters_with_cred_protect_policy(test_policy); + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert!(make_credential_response.is_ok()); + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); + let cred_desc = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: credential_id, + transports: None, + }; + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: Some(vec![cred_desc]), + extensions: GetAssertionExtensions::default(), + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_get_assertion( + &mut env, + get_assertion_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(get_assertion_response.is_ok()); + + let test_policy = CredentialProtectionPolicy::UserVerificationRequired; + let mut make_credential_params = + create_make_credential_parameters_with_cred_protect_policy(test_policy); + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + assert!(make_credential_response.is_ok()); + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); + let cred_desc = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: credential_id, + transports: None, + }; + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: Some(vec![cred_desc]), + extensions: GetAssertionExtensions::default(), + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_get_assertion( + &mut env, + get_assertion_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + get_assertion_response, + Err(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS), + ); + } + + #[test] + fn test_process_get_assertion_with_cred_blob() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let credential_id = env.rng().gen_uniform_u8x32().to_vec(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let credential = PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id, + private_key, + rp_id: String::from("example.com"), + user_handle: vec![0x1D], + user_display_name: None, + cred_protect_policy: None, + creation_order: 0, + user_name: None, + user_icon: None, + cred_blob: Some(vec![0xCB]), + large_blob_key: None, + }; + assert!(storage::store_credential(&mut env, credential).is_ok()); + + let extensions = GetAssertionExtensions { + cred_blob: true, + ..Default::default() + }; + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: None, + extensions, + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_get_assertion( + &mut env, + get_assertion_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); + let expected_extension_cbor = [ + 0xA1, 0x68, 0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, 0x41, 0xCB, + ]; + check_assertion_response_with_extension( + get_assertion_response, + Some(vec![0x1D]), + signature_counter, + None, + &expected_extension_cbor, + ); + } + + #[test] + fn test_non_resident_process_get_assertion_with_cred_blob() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let extensions = MakeCredentialExtensions { + cred_blob: Some(vec![0xCB]), + ..Default::default() + }; + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.extensions = extensions; + make_credential_params.options.rk = false; + let make_credential_response = + ctap_state.process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL); + let expected_extension_cbor = [ + 0xA1, 0x68, 0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, 0xF5, + ]; + check_make_response( + &make_credential_response, + 0xC1, + &storage::aaguid(&mut env).unwrap(), + CBOR_CREDENTIAL_ID_SIZE as u8, + &expected_extension_cbor, + ); + + let credential_id = parse_credential_id_from_non_resident_make_credential_response( + &mut env, + make_credential_response.unwrap(), + ); + let cred_desc = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: credential_id, + transports: None, + }; + let extensions = GetAssertionExtensions { + cred_blob: true, + ..Default::default() + }; + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: Some(vec![cred_desc]), + extensions, + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_get_assertion( + &mut env, + get_assertion_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); + let expected_extension_cbor = [ + 0xA1, 0x68, 0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, 0x41, 0xCB, + ]; + check_assertion_response_with_extension( + get_assertion_response, + None, + signature_counter, + None, + &expected_extension_cbor, + ); + } + + #[test] + fn test_process_get_assertion_with_large_blob_key() { + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let credential_id = env.rng().gen_uniform_u8x32().to_vec(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let credential = PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id, + private_key, + rp_id: String::from("example.com"), + user_handle: vec![0x1D], + user_display_name: None, + cred_protect_policy: None, + creation_order: 0, + user_name: None, + user_icon: None, + cred_blob: None, + large_blob_key: Some(vec![0x1C; 32]), + }; + assert!(storage::store_credential(&mut env, credential).is_ok()); + + let extensions = GetAssertionExtensions { + large_blob_key: Some(true), + ..Default::default() + }; + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: None, + extensions, + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_get_assertion( + &mut env, + get_assertion_params, + DUMMY_CHANNEL, + Instant::new(0), + ); + let large_blob_key = match get_assertion_response.unwrap() { + ResponseData::AuthenticatorGetAssertion(get_assertion_response) => { + get_assertion_response.large_blob_key.unwrap() + } + _ => panic!("Invalid response type"), + }; + assert_eq!(large_blob_key, vec![0x1C; 32]); + } + + fn test_helper_process_get_next_assertion_two_credentials_with_uv( + pin_uv_auth_protocol: PinUvAuthProtocol, + ) { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x88; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, pin_uv_auth_protocol); + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let mut make_credential_params = create_minimal_make_credential_parameters(); let user1 = PublicKeyCredentialUserEntity { @@ -1858,7 +3078,7 @@ mod test { }; make_credential_params.user = user1.clone(); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let mut make_credential_params = create_minimal_make_credential_parameters(); let user2 = PublicKeyCredentialUserEntity { @@ -1869,68 +3089,78 @@ mod test { }; make_credential_params.user = user2.clone(); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); - ctap_state - .persistent_store - .set_pin_hash(&[0u8; 16]) - .unwrap(); - let pin_uv_auth_param = Some(vec![ - 0x6F, 0x52, 0x83, 0xBF, 0x1A, 0x91, 0xEE, 0x67, 0xE9, 0xD4, 0x4C, 0x80, 0x08, 0x79, - 0x90, 0x8D, - ]); + ctap_state.client_pin = client_pin; + // The PIN length is outside of the test scope and most likely incorrect. + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let client_data_hash = vec![0xCD]; + let pin_uv_auth_param = authenticate_pin_uv_auth_token( + &pin_uv_auth_token, + &client_data_hash, + pin_uv_auth_protocol, + ); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), - client_data_hash: vec![0xCD], + client_data_hash, allow_list: None, - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: true, }, - pin_uv_auth_param, - pin_uv_auth_protocol: Some(1), + pin_uv_auth_param: Some(pin_uv_auth_param), + pin_uv_auth_protocol: Some(pin_uv_auth_protocol), }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); - let signature_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_assertion_response_with_user( get_assertion_response, - user2, + Some(user2), 0x04, signature_counter, Some(2), + &[], ); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); check_assertion_response_with_user( get_assertion_response, - user1, + Some(user1), 0x04, signature_counter, None, + &[], ); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); assert_eq!( get_assertion_response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) ); } + #[test] + fn test_process_get_next_assertion_two_credentials_with_uv_v1() { + test_helper_process_get_next_assertion_two_credentials_with_uv(PinUvAuthProtocol::V1); + } + + #[test] + fn test_process_get_next_assertion_two_credentials_with_uv_v2() { + test_helper_process_get_next_assertion_two_credentials_with_uv(PinUvAuthProtocol::V2); + } + #[test] fn test_process_get_next_assertion_three_credentials_no_uv() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.user.user_id = vec![0x01]; @@ -1938,7 +3168,7 @@ mod test { make_credential_params.user.user_display_name = Some("removed".to_string()); make_credential_params.user.user_icon = Some("removed".to_string()); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.user.user_id = vec![0x02]; @@ -1946,7 +3176,7 @@ mod test { make_credential_params.user.user_display_name = Some("removed".to_string()); make_credential_params.user.user_icon = Some("removed".to_string()); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.user.user_id = vec![0x03]; @@ -1954,14 +3184,14 @@ mod test { make_credential_params.user.user_display_name = Some("removed".to_string()); make_credential_params.user.user_icon = Some("removed".to_string()); assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: None, - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -1970,14 +3200,12 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); - let signature_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + let signature_counter = storage::global_signature_counter(&mut env).unwrap(); check_assertion_response( get_assertion_response, vec![0x03], @@ -1985,26 +3213,25 @@ mod test { Some(3), ); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); check_assertion_response(get_assertion_response, vec![0x02], signature_counter, None); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); check_assertion_response(get_assertion_response, vec![0x01], signature_counter, None); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); assert_eq!( get_assertion_response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) ); } - #[test] // this exhibits compilation errors in some configs + #[test] fn test_process_get_next_assertion_not_allowed() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); assert_eq!( get_assertion_response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) @@ -2013,19 +3240,19 @@ mod test { let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.user.user_id = vec![0x01]; assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let mut make_credential_params = create_minimal_make_credential_parameters(); make_credential_params.user.user_id = vec![0x02]; assert!(ctap_state - .process_make_credential(make_credential_params, DUMMY_CHANNEL_ID) + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) .is_ok()); let get_assertion_params = AuthenticatorGetAssertionParameters { rp_id: String::from("example.com"), client_data_hash: vec![0xCD], allow_list: None, - extensions: None, + extensions: GetAssertionExtensions::default(), options: GetAssertionOptions { up: false, uv: false, @@ -2034,9 +3261,10 @@ mod test { pin_uv_auth_protocol: None, }; let get_assertion_response = ctap_state.process_get_assertion( + &mut env, get_assertion_params, - DUMMY_CHANNEL_ID, - DUMMY_CLOCK_VALUE, + DUMMY_CHANNEL, + Instant::new(0), ); assert!(get_assertion_response.is_ok()); @@ -2052,10 +3280,10 @@ mod test { }, 4 => cbor_array![ES256_CRED_PARAM], }; - assert!(cbor::write(cbor_value, &mut command_cbor)); - ctap_state.process_command(&command_cbor, DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + assert!(cbor_write(cbor_value, &mut command_cbor).is_ok()); + ctap_state.process_command(&mut env, &command_cbor, DUMMY_CHANNEL, Instant::new(0)); - let get_assertion_response = ctap_state.process_get_next_assertion(DUMMY_CLOCK_VALUE); + let get_assertion_response = ctap_state.process_get_next_assertion(&mut env); assert_eq!( get_assertion_response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) @@ -2064,10 +3292,9 @@ mod test { #[test] fn test_process_reset() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); let credential_id = vec![0x01, 0x23, 0x45, 0x67]; let credential_source = PublicKeyCredentialSource { @@ -2081,28 +3308,26 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; - assert!(ctap_state - .persistent_store - .store_credential(credential_source) - .is_ok()); - assert!(ctap_state.persistent_store.count_credentials().unwrap() > 0); + assert!(storage::store_credential(&mut env, credential_source).is_ok()); + assert!(storage::count_credentials(&mut env).unwrap() > 0); let reset_reponse = - ctap_state.process_command(&[0x07], DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + ctap_state.process_command(&mut env, &[0x07], DUMMY_CHANNEL, Instant::new(0)); let expected_response = vec![0x00]; assert_eq!(reset_reponse, expected_response); - assert!(ctap_state.persistent_store.count_credentials().unwrap() == 0); + assert!(storage::count_credentials(&mut env).unwrap() == 0); } #[test] fn test_process_reset_cancelled() { - let mut rng = ThreadRng256 {}; - let user_presence_always_cancel = |_| Err(Ctap2StatusCode::CTAP2_ERR_KEEPALIVE_CANCEL); - let mut ctap_state = - CtapState::new(&mut rng, user_presence_always_cancel, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + env.user_presence().set(|| Err(UserPresenceError::Canceled)); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let reset_reponse = ctap_state.process_reset(DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + let reset_reponse = ctap_state.process_reset(&mut env, DUMMY_CHANNEL); assert_eq!( reset_reponse, @@ -2112,91 +3337,56 @@ mod test { #[test] fn test_process_reset_not_first() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); // This is a GetNextAssertion command. - ctap_state.process_command(&[0x08], DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + ctap_state.process_command(&mut env, &[0x08], DUMMY_CHANNEL, Instant::new(0)); - let reset_reponse = ctap_state.process_reset(DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + let reset_reponse = ctap_state.process_reset(&mut env, DUMMY_CHANNEL); assert_eq!(reset_reponse, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)); } + #[test] + fn test_process_credential_management_unknown_subcommand() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + // The subcommand 0xEE does not exist. + let reponse = ctap_state.process_command( + &mut env, + &[0x0A, 0xA1, 0x01, 0x18, 0xEE], + DUMMY_CHANNEL, + Instant::new(0), + ); + let expected_response = vec![Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND as u8]; + assert_eq!(reponse, expected_response); + } + #[test] fn test_process_unknown_command() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); // This command does not exist. - let reset_reponse = - ctap_state.process_command(&[0xDF], DUMMY_CHANNEL_ID, DUMMY_CLOCK_VALUE); + let reponse = + ctap_state.process_command(&mut env, &[0xDF], DUMMY_CHANNEL, Instant::new(0)); let expected_response = vec![Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND as u8]; - assert_eq!(reset_reponse, expected_response); - } - - #[test] - fn test_encrypt_decrypt_credential() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - - // Usually, the relying party ID or its hash is provided by the client. - // We are not testing the correctness of our SHA256 here, only if it is checked. - let rp_id_hash = [0x55; 32]; - let encrypted_id = ctap_state - .encrypt_key_handle(private_key.clone(), &rp_id_hash) - .unwrap(); - let decrypted_source = ctap_state - .decrypt_credential_source(encrypted_id, &rp_id_hash) - .unwrap() - .unwrap(); - - println!("WARNING: missing private key test due to incompatible debug deriver on ecdh"); - // assert_eq!(private_key, decrypted_source.private_key); - } - - #[test] - fn test_encrypt_decrypt_bad_hmac() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); - - // Same as above. - let rp_id_hash = [0x55; 32]; - let encrypted_id = ctap_state - .encrypt_key_handle(private_key, &rp_id_hash) - .unwrap(); - for i in 0..encrypted_id.len() { - let mut modified_id = encrypted_id.clone(); - modified_id[i] ^= 0x01; - assert!(ctap_state - .decrypt_credential_source(modified_id, &rp_id_hash) - .unwrap() - .is_none()); - } + assert_eq!(reponse, expected_response); } #[test] fn test_signature_counter() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); - let mut last_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + let mut last_counter = storage::global_signature_counter(&mut env).unwrap(); assert!(last_counter > 0); for _ in 0..100 { - assert!(ctap_state.increment_global_signature_counter().is_ok()); - let next_counter = ctap_state - .persistent_store - .global_signature_counter() - .unwrap(); + assert!(ctap_state + .increment_global_signature_counter(&mut env) + .is_ok()); + let next_counter = storage::global_signature_counter(&mut env).unwrap(); assert!(next_counter > last_counter); last_counter = next_counter; } @@ -2204,22 +3394,22 @@ mod test { #[test] fn test_vendor_configure() { - let mut rng = ThreadRng256 {}; - let user_immediately_present = |_| Ok(()); - let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); // Nothing should be configured at the beginning let response = ctap_state.process_vendor_configure( + &mut env, AuthenticatorVendorConfigureParameters { lockdown: false, attestation_material: None, }, - DUMMY_CHANNEL_ID, + DUMMY_CHANNEL, ); assert_eq!( response, - Ok(ResponseData::AuthenticatorVendor( - AuthenticatorVendorResponse { + Ok(ResponseData::AuthenticatorVendorConfigure( + AuthenticatorVendorConfigureResponse { cert_programmed: false, pkey_programmed: false, } @@ -2230,6 +3420,7 @@ mod test { let dummy_key = [0x41u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH]; let dummy_cert = [0xddu8; 20]; let response = ctap_state.process_vendor_configure( + &mut env, AuthenticatorVendorConfigureParameters { lockdown: false, attestation_material: Some(AuthenticatorAttestationMaterial { @@ -2237,37 +3428,29 @@ mod test { private_key: dummy_key, }), }, - DUMMY_CHANNEL_ID, + DUMMY_CHANNEL, ); assert_eq!( response, - Ok(ResponseData::AuthenticatorVendor( - AuthenticatorVendorResponse { + Ok(ResponseData::AuthenticatorVendorConfigure( + AuthenticatorVendorConfigureResponse { cert_programmed: true, pkey_programmed: true, } )) ); assert_eq!( - ctap_state - .persistent_store - .attestation_certificate() - .unwrap() - .unwrap(), - dummy_cert - ); - assert_eq!( - ctap_state - .persistent_store - .attestation_private_key() - .unwrap() - .unwrap(), - dummy_key + env.attestation_store().get(&attestation_store::Id::Batch), + Ok(Some(Attestation { + private_key: dummy_key, + certificate: dummy_cert.to_vec(), + })) ); // Try to inject other dummy values and check that initial values are retained. let other_dummy_key = [0x44u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH]; let response = ctap_state.process_vendor_configure( + &mut env, AuthenticatorVendorConfigureParameters { lockdown: false, attestation_material: Some(AuthenticatorAttestationMaterial { @@ -2275,50 +3458,476 @@ mod test { private_key: other_dummy_key, }), }, - DUMMY_CHANNEL_ID, + DUMMY_CHANNEL, ); assert_eq!( response, - Ok(ResponseData::AuthenticatorVendor( - AuthenticatorVendorResponse { + Ok(ResponseData::AuthenticatorVendorConfigure( + AuthenticatorVendorConfigureResponse { cert_programmed: true, pkey_programmed: true, } )) ); assert_eq!( - ctap_state - .persistent_store - .attestation_certificate() - .unwrap() - .unwrap(), - dummy_cert - ); - assert_eq!( - ctap_state - .persistent_store - .attestation_private_key() - .unwrap() - .unwrap(), - dummy_key + env.attestation_store().get(&attestation_store::Id::Batch), + Ok(Some(Attestation { + private_key: dummy_key, + certificate: dummy_cert.to_vec(), + })) ); // Now try to lock the device let response = ctap_state.process_vendor_configure( + &mut env, AuthenticatorVendorConfigureParameters { lockdown: true, attestation_material: None, }, - DUMMY_CHANNEL_ID, + DUMMY_CHANNEL, ); assert_eq!( response, - Ok(ResponseData::AuthenticatorVendor( - AuthenticatorVendorResponse { + Ok(ResponseData::AuthenticatorVendorConfigure( + AuthenticatorVendorConfigureResponse { cert_programmed: true, pkey_programmed: true, } )) ); } + + #[test] + fn test_vendor_upgrade() { + // The test partition storage has size 0x40000. + // The test metadata storage has size 0x1000. + // The test identifier matches partition B. + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + const METADATA_LEN: usize = 0x1000; + let metadata = vec![0xFF; METADATA_LEN]; + let metadata_hash = Sha256::hash(&metadata); + let data = vec![0xFF; 0x1000]; + let hash = Sha256::hash(&data); + + // Write to partition. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0x20000, + data: data.clone(), + hash, + }, + ); + assert_eq!(response, Ok(ResponseData::AuthenticatorVendorUpgrade)); + + // TestEnv doesn't check the metadata, test its parser in your Env. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0, + data: metadata.clone(), + hash: metadata_hash, + }, + ); + assert_eq!(response, Ok(ResponseData::AuthenticatorVendorUpgrade)); + + // TestEnv doesn't check the metadata, test its parser in your Env. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: METADATA_LEN, + data: data.clone(), + hash, + }, + ); + assert_eq!(response, Ok(ResponseData::AuthenticatorVendorUpgrade)); + + // Write metadata of a wrong size. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0, + data: metadata[..METADATA_LEN - 1].to_vec(), + hash: metadata_hash, + }, + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP2_ERR_INTEGRITY_FAILURE)); + + // Write outside of the partition. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0x41000, + data: data.clone(), + hash, + }, + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER)); + + // Write a bad hash. + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0x20000, + data, + hash: [0xEE; 32], + }, + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP2_ERR_INTEGRITY_FAILURE)); + } + + #[test] + fn test_vendor_upgrade_no_second_partition() { + let mut env = TestEnv::new(); + env.disable_upgrade_storage(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let data = vec![0xFF; 0x1000]; + let hash = Sha256::hash(&data); + let response = ctap_state.process_vendor_upgrade( + &mut env, + AuthenticatorVendorUpgradeParameters { + offset: 0, + data, + hash, + }, + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND)); + } + + #[test] + fn test_vendor_upgrade_info() { + let mut env = TestEnv::new(); + let ctap_state = CtapState::new(&mut env, Instant::new(0)); + let bundle_identifier = env.upgrade_storage().unwrap().bundle_identifier(); + + let upgrade_info_reponse = ctap_state.process_vendor_upgrade_info(&mut env); + assert_eq!( + upgrade_info_reponse, + Ok(ResponseData::AuthenticatorVendorUpgradeInfo( + AuthenticatorVendorUpgradeInfoResponse { + info: bundle_identifier, + } + )) + ); + } + + #[test] + fn test_permission_timeout() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + // Write 2 credentials for later assertions. + for i in 0..3 { + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.user.user_id = vec![i as u8]; + assert!(ctap_state + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) + .is_ok()); + } + + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: None, + extensions: GetAssertionExtensions::default(), + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetAssertion(get_assertion_params), + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(get_assertion_response.is_ok()); + let get_next_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + DUMMY_CHANNEL, + Instant::new(0) + STATEFUL_COMMAND_TIMEOUT_DURATION - Duration::from_millis(1), + ); + assert!(get_next_assertion_response.is_ok()); + let get_next_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + DUMMY_CHANNEL, + Instant::new(0) + STATEFUL_COMMAND_TIMEOUT_DURATION + Duration::from_millis(1), + ); + assert_eq!( + get_next_assertion_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + } + + #[test] + fn test_reset_timeout() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorReset, + DUMMY_CHANNEL, + Instant::new(0) + RESET_TIMEOUT_DURATION + Duration::from_millis(1), + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)); + } + + #[test] + fn test_credential_management_timeout() { + let mut env = TestEnv::new(); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pin_uv_auth_token = [0x55; 32]; + let client_pin = + ClientPin::new_test(key_agreement_key, pin_uv_auth_token, PinUvAuthProtocol::V1); + + let private_key = PrivateKey::new_ecdsa(&mut env); + let credential_source = PublicKeyCredentialSource { + key_type: PublicKeyCredentialType::PublicKey, + credential_id: env.rng().gen_uniform_u8x32().to_vec(), + private_key, + rp_id: String::from("example.com"), + user_handle: vec![0x01], + user_display_name: Some("display_name".to_string()), + cred_protect_policy: None, + creation_order: 0, + user_name: Some("name".to_string()), + user_icon: Some("icon".to_string()), + cred_blob: None, + large_blob_key: None, + }; + + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + ctap_state.client_pin = client_pin; + + for i in 0..3 { + let mut credential = credential_source.clone(); + credential.rp_id = i.to_string(); + storage::store_credential(&mut env, credential).unwrap(); + } + + storage::set_pin(&mut env, &[0u8; 16], 4).unwrap(); + let pin_uv_auth_param = Some(vec![ + 0x1A, 0xA4, 0x96, 0xDA, 0x62, 0x80, 0x28, 0x13, 0xEB, 0x32, 0xB9, 0xF1, 0xD2, 0xA9, + 0xD0, 0xD1, + ]); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsBegin, + sub_command_params: None, + pin_uv_auth_protocol: Some(PinUvAuthProtocol::V1), + pin_uv_auth_param, + }; + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorCredentialManagement(cred_management_params), + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(matches!( + response, + Ok(ResponseData::AuthenticatorCredentialManagement(_)) + )); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsGetNextRp, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorCredentialManagement(cred_management_params), + DUMMY_CHANNEL, + Instant::new(0) + STATEFUL_COMMAND_TIMEOUT_DURATION - Duration::from_millis(1), + ); + assert!(matches!( + response, + Ok(ResponseData::AuthenticatorCredentialManagement(_)) + )); + + let cred_management_params = AuthenticatorCredentialManagementParameters { + sub_command: CredentialManagementSubCommand::EnumerateRpsGetNextRp, + sub_command_params: None, + pin_uv_auth_protocol: None, + pin_uv_auth_param: None, + }; + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorCredentialManagement(cred_management_params), + DUMMY_CHANNEL, + Instant::new(0) + STATEFUL_COMMAND_TIMEOUT_DURATION + Duration::from_millis(1), + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED)); + } + + #[test] + fn test_check_user_presence() { + // This TestEnv always returns successful user_presence checks. + let mut env = TestEnv::new(); + let response = check_user_presence(&mut env, DUMMY_CHANNEL, None); + assert!(matches!(response, Ok(_))); + } + + #[test] + fn test_check_user_presence_timeout() { + // This will always return timeout. + fn user_presence_timeout() -> UserPresenceResult { + Err(UserPresenceError::Timeout) + } + + let mut env = TestEnv::new(); + env.user_presence().set(user_presence_timeout); + let response = check_user_presence(&mut env, DUMMY_CHANNEL, None); + assert!(matches!( + response, + Err(Ctap2StatusCode::CTAP2_ERR_USER_ACTION_TIMEOUT) + )); + } + + #[test] + fn test_channel_interleaving() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + const NEW_CHANNEL: Channel = Channel::MainHid([0xAA, 0xAA, 0xAA, 0xAA]); + + // Write 3 credentials for later assertions. + for i in 0..3 { + let mut make_credential_params = create_minimal_make_credential_parameters(); + make_credential_params.user.user_id = vec![i as u8]; + assert!(ctap_state + .process_make_credential(&mut env, make_credential_params, DUMMY_CHANNEL) + .is_ok()); + } + + let get_assertion_params = AuthenticatorGetAssertionParameters { + rp_id: String::from("example.com"), + client_data_hash: vec![0xCD], + allow_list: None, + extensions: GetAssertionExtensions::default(), + options: GetAssertionOptions { + up: false, + uv: false, + }, + pin_uv_auth_param: None, + pin_uv_auth_protocol: None, + }; + let get_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetAssertion(get_assertion_params), + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(matches!( + get_assertion_response, + Ok(ResponseData::AuthenticatorGetAssertion(_)) + )); + let get_next_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(get_next_assertion_response.is_ok()); + assert!(matches!( + get_next_assertion_response, + Ok(ResponseData::AuthenticatorGetNextAssertion(_)) + )); + let get_next_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + NEW_CHANNEL, + Instant::new(0), + ); + assert_eq!( + get_next_assertion_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + let get_next_assertion_response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!( + get_next_assertion_response, + Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED) + ); + } + + #[test] + fn test_get_info_command() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetInfo, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert!(matches!( + response, + Ok(ResponseData::AuthenticatorGetInfo(_)) + )); + #[cfg(feature = "vendor_hid")] + { + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetInfo, + VENDOR_CHANNEL, + Instant::new(0), + ); + assert!(matches!( + response, + Ok(ResponseData::AuthenticatorGetInfo(_)) + )); + } + } + + #[test] + #[cfg(feature = "vendor_hid")] + fn test_vendor_hid_does_not_support_fido_command() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorGetNextAssertion, + VENDOR_CHANNEL, + Instant::new(0), + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND)); + } + + #[test] + #[cfg(feature = "vendor_hid")] + fn test_vendor_hid() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorVendorUpgradeInfo, + VENDOR_CHANNEL, + Instant::new(0), + ); + assert!(matches!( + response, + Ok(ResponseData::AuthenticatorVendorUpgradeInfo(_)) + )); + let response = ctap_state.process_parsed_command( + &mut env, + Command::AuthenticatorVendorUpgradeInfo, + DUMMY_CHANNEL, + Instant::new(0), + ); + assert_eq!(response, Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND)); + } } diff --git a/apps/vault/src/ctap/pin_protocol.rs b/apps/vault/src/ctap/pin_protocol.rs new file mode 100644 index 000000000..bb923f26f --- /dev/null +++ b/apps/vault/src/ctap/pin_protocol.rs @@ -0,0 +1,409 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::ctap::client_pin::PIN_TOKEN_LENGTH; +use crate::ctap::crypto_wrapper::{aes256_cbc_decrypt, aes256_cbc_encrypt}; +use crate::ctap::data_formats::{CoseKey, PinUvAuthProtocol}; +use crate::ctap::status_code::Ctap2StatusCode; +use std::boxed::Box; +use std::vec::Vec; +use core::convert::TryInto; +use ctap_crypto::hkdf::hkdf_empty_salt_256; +#[cfg(test)] +use ctap_crypto::hmac::hmac_256; +use ctap_crypto::hmac::{verify_hmac_256, verify_hmac_256_first_128bits}; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; +use ctap_crypto::rng256::Rng256; +use arrayref::array_ref; + +/// Implements common functions between existing PIN protocols for handshakes. +pub struct PinProtocol { + key_agreement_key: ctap_crypto::ecdh::SecKey, + pin_uv_auth_token: [u8; PIN_TOKEN_LENGTH], +} + +impl PinProtocol { + /// This process is run by the authenticator at power-on. + /// + /// This function implements "initialize" from the specification. + pub fn new(rng: &mut impl Rng256) -> PinProtocol { + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); + let pin_uv_auth_token = rng.gen_uniform_u8x32(); + PinProtocol { + key_agreement_key, + pin_uv_auth_token, + } + } + + /// Generates a fresh public key. + pub fn regenerate(&mut self, rng: &mut impl Rng256) { + self.key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); + } + + /// Generates a fresh pinUvAuthToken. + pub fn reset_pin_uv_auth_token(&mut self, rng: &mut impl Rng256) { + self.pin_uv_auth_token = rng.gen_uniform_u8x32(); + } + + /// Returns the authenticator’s public key as a CoseKey structure. + pub fn get_public_key(&self) -> CoseKey { + CoseKey::from(self.key_agreement_key.genpk()) + } + + /// Processes the peer's encapsulated CoseKey and returns the shared secret. + pub fn decapsulate( + &self, + peer_cose_key: CoseKey, + pin_uv_auth_protocol: PinUvAuthProtocol, + ) -> Result, Ctap2StatusCode> { + let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(peer_cose_key)?; + let handshake = self.key_agreement_key.exchange_x(&pk); + match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => Ok(Box::new(SharedSecretV1::new(handshake))), + PinUvAuthProtocol::V2 => Ok(Box::new(SharedSecretV2::new(handshake))), + } + } + + /// Getter for pinUvAuthToken. + pub fn get_pin_uv_auth_token(&self) -> &[u8; PIN_TOKEN_LENGTH] { + &self.pin_uv_auth_token + } + + /// This is used for debugging to inject key material. + #[cfg(test)] + pub fn new_test( + key_agreement_key: ctap_crypto::ecdh::SecKey, + pin_uv_auth_token: [u8; PIN_TOKEN_LENGTH], + ) -> PinProtocol { + PinProtocol { + key_agreement_key, + pin_uv_auth_token, + } + } +} + +/// Authenticates the pinUvAuthToken for the given PIN protocol. +#[cfg(test)] +pub fn authenticate_pin_uv_auth_token( + token: &[u8; PIN_TOKEN_LENGTH], + message: &[u8], + pin_uv_auth_protocol: PinUvAuthProtocol, +) -> Vec { + match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => hmac_256::(token, message)[..16].to_vec(), + PinUvAuthProtocol::V2 => hmac_256::(token, message).to_vec(), + } +} + +/// Verifies the pinUvAuthToken for the given PIN protocol. +pub fn verify_pin_uv_auth_token( + token: &[u8; PIN_TOKEN_LENGTH], + message: &[u8], + signature: &[u8], + pin_uv_auth_protocol: PinUvAuthProtocol, +) -> Result<(), Ctap2StatusCode> { + match pin_uv_auth_protocol { + PinUvAuthProtocol::V1 => verify_v1(token, message, signature), + PinUvAuthProtocol::V2 => verify_v2(token, message, signature), + } +} + +pub trait SharedSecret { + /// Returns the encrypted plaintext. + fn encrypt(&self, rng: &mut dyn Rng256, plaintext: &[u8]) -> Result, Ctap2StatusCode>; + + /// Returns the decrypted ciphertext. + fn decrypt(&self, ciphertext: &[u8]) -> Result, Ctap2StatusCode>; + + /// Verifies that the signature is a valid MAC for the given message. + fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), Ctap2StatusCode>; + + /// Creates a signature that matches verify. + #[cfg(test)] + fn authenticate(&self, message: &[u8]) -> Vec; +} + +fn verify_v1(key: &[u8; 32], message: &[u8], signature: &[u8]) -> Result<(), Ctap2StatusCode> { + if signature.len() != 16 { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if verify_hmac_256_first_128bits::(key, message, array_ref![signature, 0, 16]) { + Ok(()) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + } +} + +fn verify_v2(key: &[u8; 32], message: &[u8], signature: &[u8]) -> Result<(), Ctap2StatusCode> { + if signature.len() != 32 { + return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); + } + if verify_hmac_256::(key, message, array_ref![signature, 0, 32]) { + Ok(()) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + } +} + +pub struct SharedSecretV1 { + common_secret: [u8; 32], + aes_enc_key: ctap_crypto::aes256::EncryptionKey, +} + +impl SharedSecretV1 { + /// Creates a new shared secret from the handshake result. + fn new(handshake: [u8; 32]) -> SharedSecretV1 { + let common_secret = Sha256::hash(&handshake); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&common_secret); + SharedSecretV1 { + common_secret, + aes_enc_key, + } + } +} + +impl SharedSecret for SharedSecretV1 { + fn encrypt(&self, rng: &mut dyn Rng256, plaintext: &[u8]) -> Result, Ctap2StatusCode> { + aes256_cbc_encrypt(rng, &self.aes_enc_key, plaintext, false) + } + + fn decrypt(&self, ciphertext: &[u8]) -> Result, Ctap2StatusCode> { + aes256_cbc_decrypt(&self.aes_enc_key, ciphertext, false) + } + + fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), Ctap2StatusCode> { + verify_v1(&self.common_secret, message, signature) + } + + #[cfg(test)] + fn authenticate(&self, message: &[u8]) -> Vec { + hmac_256::(&self.common_secret, message)[..16].to_vec() + } +} + +pub struct SharedSecretV2 { + aes_enc_key: ctap_crypto::aes256::EncryptionKey, + hmac_key: [u8; 32], +} + +impl SharedSecretV2 { + /// Creates a new shared secret from the handshake result. + fn new(handshake: [u8; 32]) -> SharedSecretV2 { + let aes_key = hkdf_empty_salt_256::(&handshake, b"CTAP2 AES key"); + SharedSecretV2 { + aes_enc_key: ctap_crypto::aes256::EncryptionKey::new(&aes_key), + hmac_key: hkdf_empty_salt_256::(&handshake, b"CTAP2 HMAC key"), + } + } +} + +impl SharedSecret for SharedSecretV2 { + fn encrypt(&self, rng: &mut dyn Rng256, plaintext: &[u8]) -> Result, Ctap2StatusCode> { + aes256_cbc_encrypt(rng, &self.aes_enc_key, plaintext, true) + } + + fn decrypt(&self, ciphertext: &[u8]) -> Result, Ctap2StatusCode> { + aes256_cbc_decrypt(&self.aes_enc_key, ciphertext, true) + } + + fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), Ctap2StatusCode> { + verify_v2(&self.hmac_key, message, signature) + } + + #[cfg(test)] + fn authenticate(&self, message: &[u8]) -> Vec { + hmac_256::(&self.hmac_key, message).to_vec() + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::env::test::TestEnv; + + #[test] + fn test_pin_protocol_public_key() { + let mut env = TestEnv::new(); + let mut pin_protocol = PinProtocol::new(env.rng()); + let public_key = pin_protocol.get_public_key(); + pin_protocol.regenerate(env.rng()); + let new_public_key = pin_protocol.get_public_key(); + assert_ne!(public_key, new_public_key); + } + + #[test] + fn test_pin_protocol_pin_uv_auth_token() { + let mut env = TestEnv::new(); + let mut pin_protocol = PinProtocol::new(env.rng()); + let token = *pin_protocol.get_pin_uv_auth_token(); + pin_protocol.reset_pin_uv_auth_token(env.rng()); + let new_token = pin_protocol.get_pin_uv_auth_token(); + assert_ne!(&token, new_token); + } + + #[test] + fn test_shared_secret_v1_encrypt_decrypt() { + let mut env = TestEnv::new(); + let shared_secret = SharedSecretV1::new([0x55; 32]); + let plaintext = vec![0xAA; 64]; + let ciphertext = shared_secret.encrypt(env.rng(), &plaintext).unwrap(); + assert_eq!(shared_secret.decrypt(&ciphertext), Ok(plaintext)); + } + + #[test] + fn test_shared_secret_v1_authenticate_verify() { + let shared_secret = SharedSecretV1::new([0x55; 32]); + let message = [0xAA; 32]; + let signature = shared_secret.authenticate(&message); + assert_eq!(shared_secret.verify(&message, &signature), Ok(())); + } + + #[test] + fn test_shared_secret_v1_verify() { + let shared_secret = SharedSecretV1::new([0x55; 32]); + let message = [0xAA]; + let signature = [ + 0x8B, 0x60, 0x15, 0x7D, 0xF3, 0x44, 0x82, 0x2E, 0x54, 0x34, 0x7A, 0x01, 0xFB, 0x02, + 0x48, 0xA6, + ]; + assert_eq!(shared_secret.verify(&message, &signature), Ok(())); + assert_eq!( + shared_secret.verify(&[0xBB], &signature), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + shared_secret.verify(&message, &[0x12; 16]), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_shared_secret_v2_encrypt_decrypt() { + let mut env = TestEnv::new(); + let shared_secret = SharedSecretV2::new([0x55; 32]); + let plaintext = vec![0xAA; 64]; + let ciphertext = shared_secret.encrypt(env.rng(), &plaintext).unwrap(); + assert_eq!(shared_secret.decrypt(&ciphertext), Ok(plaintext)); + } + + #[test] + fn test_shared_secret_v2_authenticate_verify() { + let shared_secret = SharedSecretV2::new([0x55; 32]); + let message = [0xAA; 32]; + let signature = shared_secret.authenticate(&message); + assert_eq!(shared_secret.verify(&message, &signature), Ok(())); + } + + #[test] + fn test_shared_secret_v2_verify() { + let shared_secret = SharedSecretV2::new([0x55; 32]); + let message = [0xAA]; + let signature = [ + 0xC0, 0x3F, 0x2A, 0x22, 0x5C, 0xC3, 0x4E, 0x05, 0xC1, 0x0E, 0x72, 0x9C, 0x8D, 0xD5, + 0x7D, 0xE5, 0x98, 0x9C, 0x68, 0x15, 0xEC, 0xE2, 0x3A, 0x95, 0xD5, 0x90, 0xE1, 0xE9, + 0x3F, 0xF0, 0x1A, 0xAF, + ]; + assert_eq!(shared_secret.verify(&message, &signature), Ok(())); + assert_eq!( + shared_secret.verify(&[0xBB], &signature), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + shared_secret.verify(&message, &[0x12; 32]), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_decapsulate_symmetric() { + let mut env = TestEnv::new(); + let pin_protocol1 = PinProtocol::new(env.rng()); + let pin_protocol2 = PinProtocol::new(env.rng()); + for &protocol in &[PinUvAuthProtocol::V1, PinUvAuthProtocol::V2] { + let shared_secret1 = pin_protocol1 + .decapsulate(pin_protocol2.get_public_key(), protocol) + .unwrap(); + let shared_secret2 = pin_protocol2 + .decapsulate(pin_protocol1.get_public_key(), protocol) + .unwrap(); + let plaintext = vec![0xAA; 64]; + let ciphertext = shared_secret1.encrypt(env.rng(), &plaintext).unwrap(); + assert_eq!(plaintext, shared_secret2.decrypt(&ciphertext).unwrap()); + } + } + + #[test] + fn test_verify_pin_uv_auth_token_v1() { + let token = [0x91; PIN_TOKEN_LENGTH]; + let message = [0xAA]; + let signature = [ + 0x9C, 0x1C, 0xFE, 0x9D, 0xD7, 0x64, 0x6A, 0x06, 0xB9, 0xA8, 0x0F, 0x96, 0xAD, 0x50, + 0x49, 0x68, + ]; + assert_eq!( + verify_pin_uv_auth_token(&token, &message, &signature, PinUvAuthProtocol::V1), + Ok(()) + ); + assert_eq!( + verify_pin_uv_auth_token( + &[0x12; PIN_TOKEN_LENGTH], + &message, + &signature, + PinUvAuthProtocol::V1 + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + verify_pin_uv_auth_token(&token, &[0xBB], &signature, PinUvAuthProtocol::V1), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + verify_pin_uv_auth_token(&token, &message, &[0x12; 16], PinUvAuthProtocol::V1), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_verify_pin_uv_auth_token_v2() { + let token = [0x91; PIN_TOKEN_LENGTH]; + let message = [0xAA]; + let signature = [ + 0x9C, 0x1C, 0xFE, 0x9D, 0xD7, 0x64, 0x6A, 0x06, 0xB9, 0xA8, 0x0F, 0x96, 0xAD, 0x50, + 0x49, 0x68, 0x94, 0x90, 0x20, 0x53, 0x0F, 0xA3, 0xD2, 0x7A, 0x9F, 0xFD, 0xFA, 0x62, + 0x36, 0x93, 0xF7, 0x84, + ]; + assert_eq!( + verify_pin_uv_auth_token(&token, &message, &signature, PinUvAuthProtocol::V2), + Ok(()) + ); + assert_eq!( + verify_pin_uv_auth_token( + &[0x12; PIN_TOKEN_LENGTH], + &message, + &signature, + PinUvAuthProtocol::V2 + ), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + verify_pin_uv_auth_token(&token, &[0xBB], &signature, PinUvAuthProtocol::V2), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + verify_pin_uv_auth_token(&token, &message, &[0x12; 32], PinUvAuthProtocol::V2), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } +} diff --git a/apps/vault/src/ctap/pin_protocol_v1.rs b/apps/vault/src/ctap/pin_protocol_v1.rs deleted file mode 100644 index 1eaf7ba41..000000000 --- a/apps/vault/src/ctap/pin_protocol_v1.rs +++ /dev/null @@ -1,1296 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use super::command::AuthenticatorClientPinParameters; -use super::data_formats::{ClientPinSubCommand, CoseKey, GetAssertionHmacSecretInput}; -use super::response::{AuthenticatorClientPinResponse, ResponseData}; -use super::status_code::Ctap2StatusCode; -use super::storage::PersistentStore; -#[cfg(feature = "with_ctap2_1")] -use std::string::String; -use std::vec; -use std::vec::Vec; -use arrayref::array_ref; -use core::convert::TryInto; -use ctap_crypto::cbc::{cbc_decrypt, cbc_encrypt}; -use ctap_crypto::hmac::{hmac_256, verify_hmac_256_first_128bits}; -use ctap_crypto::rng256::Rng256; -use ctap_crypto::sha256::Sha256; -use ctap_crypto::Hash256; -#[cfg(all(test, feature = "with_ctap2_1"))] -use enum_iterator::IntoEnumIterator; -use subtle::ConstantTimeEq; - -// Those constants have to be multiples of 16, the AES block size. -pub const PIN_AUTH_LENGTH: usize = 16; -const PIN_PADDED_LENGTH: usize = 64; -const PIN_TOKEN_LENGTH: usize = 32; - -/// Checks the given pin_auth against the truncated output of HMAC-SHA256. -/// Returns LEFT(HMAC(hmac_key, hmac_contents), 16) == pin_auth). -fn verify_pin_auth(hmac_key: &[u8], hmac_contents: &[u8], pin_auth: &[u8]) -> bool { - if pin_auth.len() != PIN_AUTH_LENGTH { - log::info!("pin_auth length is wrong: {}", pin_auth.len()); - return false; - } - log::info!("key {:?}, contents: {:?}, pin: {:?}", hmac_key, hmac_contents, pin_auth); - verify_hmac_256_first_128bits::( - hmac_key, - hmac_contents, - array_ref![pin_auth, 0, PIN_AUTH_LENGTH], - ) -} - -/// Encrypts the HMAC-secret outputs. To compute them, we first have to -/// decrypt the HMAC secret salt(s) that were encrypted with the shared secret. -/// The credRandom is used as a secret to HMAC those salts. -fn encrypt_hmac_secret_output( - shared_secret: &[u8; 32], - salt_enc: &[u8], - cred_random: &[u8; 32], -) -> Result, Ctap2StatusCode> { - if salt_enc.len() != 32 && salt_enc.len() != 64 { - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION); - } - let aes_enc_key = shared_secret; - let aes_dec_key = aes_enc_key; - // The specification specifically asks for a zero IV. - let iv = [0u8; 16]; - - // With the if clause restriction above, block_len can only be 2 or 4. - let block_len = salt_enc.len() / 16; - let mut blocks = vec![[0u8; 16]; block_len]; - for i in 0..block_len { - blocks[i].copy_from_slice(&salt_enc[16 * i..16 * (i + 1)]); - } - cbc_decrypt(&aes_dec_key, iv, &mut blocks[..block_len]); - - let mut decrypted_salt1 = [0u8; 32]; - decrypted_salt1[..16].copy_from_slice(&blocks[0]); - decrypted_salt1[16..].copy_from_slice(&blocks[1]); - let output1 = hmac_256::(&cred_random[..], &decrypted_salt1[..]); - for i in 0..2 { - blocks[i].copy_from_slice(&output1[16 * i..16 * (i + 1)]); - } - - if block_len == 4 { - let mut decrypted_salt2 = [0u8; 32]; - decrypted_salt2[..16].copy_from_slice(&blocks[2]); - decrypted_salt2[16..].copy_from_slice(&blocks[3]); - let output2 = hmac_256::(&cred_random[..], &decrypted_salt2[..]); - for i in 0..2 { - blocks[i + 2].copy_from_slice(&output2[16 * i..16 * (i + 1)]); - } - } - - cbc_encrypt(&aes_enc_key, iv, &mut blocks[..block_len]); - let mut encrypted_output = Vec::with_capacity(salt_enc.len()); - for b in &blocks[..block_len] { - encrypted_output.extend(b); - } - Ok(encrypted_output) -} - -/// Decrypts the new_pin_enc and outputs the found PIN. -fn decrypt_pin( - aes_dec_key: &[u8; 32], - new_pin_enc: Vec, -) -> Option> { - if new_pin_enc.len() != PIN_PADDED_LENGTH { - return None; - } - let iv = [0u8; 16]; - // Assuming PIN_PADDED_LENGTH % block_size == 0 here. - const BLOCK_COUNT: usize = PIN_PADDED_LENGTH / 16; - let mut blocks = [[0u8; 16]; BLOCK_COUNT]; - for i in 0..BLOCK_COUNT { - blocks[i].copy_from_slice(&new_pin_enc[i * 16..(i + 1) * 16]); - } - cbc_decrypt(aes_dec_key, iv, &mut blocks); - // In CTAP 2.1, the specification changed. The new wording might lead to - // different behavior when there are non-zero bytes after zero bytes. - // This implementation consistently ignores those degenerate cases. - Some( - blocks - .iter() - .flatten() - .cloned() - .take_while(|&c| c != 0) - .collect::>(), - ) -} - -/// Stores the encrypted new PIN in the persistent storage, if it satisfies the -/// PIN policy. The PIN is decrypted and stripped from its padding. Next, the -/// length of the PIN is checked to fulfill policy requirements. Last, the PIN -/// is hashed, truncated to 16 bytes and persistently stored. -fn check_and_store_new_pin( - persistent_store: &mut PersistentStore, - aes_dec_key: &[u8; 32], - new_pin_enc: Vec, -) -> Result<(), Ctap2StatusCode> { - let pin = decrypt_pin(aes_dec_key, new_pin_enc) - .ok_or(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION)?; - - #[cfg(feature = "with_ctap2_1")] - let min_pin_length = persistent_store.min_pin_length()? as usize; - #[cfg(not(feature = "with_ctap2_1"))] - let min_pin_length = 4; - if pin.len() < min_pin_length || pin.len() == PIN_PADDED_LENGTH { - // TODO(kaczmarczyck) check 4 code point minimum instead - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION); - } - let mut pin_hash = [0u8; 16]; - pin_hash.copy_from_slice(&Sha256::hash(&pin[..])[..16]); - persistent_store.set_pin_hash(&pin_hash)?; - Ok(()) -} - -#[cfg(feature = "with_ctap2_1")] -#[cfg_attr(test, derive(IntoEnumIterator))] -// TODO remove when all variants are used -#[allow(dead_code)] -pub enum PinPermission { - // All variants should use integers with a single bit set. - MakeCredential = 0x01, - GetAssertion = 0x02, - CredentialManagement = 0x04, - BioEnrollment = 0x08, - PlatformConfiguration = 0x10, - AuthenticatorConfiguration = 0x20, -} - -pub struct PinProtocolV1 { - key_agreement_key: ctap_crypto::ecdh::SecKey, - pin_uv_auth_token: [u8; PIN_TOKEN_LENGTH], - consecutive_pin_mismatches: u8, - #[cfg(feature = "with_ctap2_1")] - permissions: u8, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id: Option, -} - -impl PinProtocolV1 { - pub fn new(rng: &mut impl Rng256) -> PinProtocolV1 { - let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); - let pin_uv_auth_token = rng.gen_uniform_u8x32(); - PinProtocolV1 { - key_agreement_key, - pin_uv_auth_token, - consecutive_pin_mismatches: 0, - #[cfg(feature = "with_ctap2_1")] - permissions: 0, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id: None, - } - } - - /// Decrypts the encrypted pin_hash and compares it to the stored pin_hash. - /// Resets or decreases the PIN retries, depending on success or failure. - /// Also, in case of failure, the key agreement key is randomly reset. - fn verify_pin_hash_enc( - &mut self, - rng: &mut impl Rng256, - persistent_store: &mut PersistentStore, - aes_dec_key: &[u8; 32], - pin_hash_enc: Vec, - ) -> Result<(), Ctap2StatusCode> { - match persistent_store.pin_hash()? { - Some(pin_hash) => { - if self.consecutive_pin_mismatches >= 3 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED); - } - persistent_store.decr_pin_retries()?; - if pin_hash_enc.len() != PIN_AUTH_LENGTH { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID); - } - - let iv = [0u8; 16]; - let mut blocks = [[0u8; 16]; 1]; - blocks[0].copy_from_slice(&pin_hash_enc); - cbc_decrypt(aes_dec_key, iv, &mut blocks); - - if !bool::from(pin_hash.ct_eq(&blocks[0])) { - self.key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); - if persistent_store.pin_retries()? == 0 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); - } - self.consecutive_pin_mismatches += 1; - if self.consecutive_pin_mismatches >= 3 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED); - } - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID); - } - } - // This status code is not explicitly mentioned in the specification. - None => return Err(Ctap2StatusCode::CTAP2_ERR_PIN_REQUIRED), - } - persistent_store.reset_pin_retries()?; - self.consecutive_pin_mismatches = 0; - Ok(()) - } - - /// Uses the self-owned and passed halves of the key agreement to generate the - /// shared secret for checking pin_auth and generating a decryption key. - fn exchange_decryption_key( - &self, - key_agreement: CoseKey, - pin_auth: &[u8], - authenticated_message: &[u8], - ) -> Result<[u8; 32], Ctap2StatusCode> { - let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; - log::trace!("HOST pk: {:?}", pk); - let mut hpk_x = [0u8; 32]; - let mut hpk_y = [0u8; 32]; - pk.to_coordinates(&mut hpk_x, &mut hpk_y); - log::trace!("HOST pk coords (host_pk): x: {:?} y: {:?}", hpk_x, hpk_y); - // log::info!("MY private key: {:?}", self.key_agreement_key.to_bytes()); // requires a "test" feature to access - let my_pk = self.key_agreement_key.genpk(); - log::trace!("MY pk: {:?}", my_pk); - let mut x = [0u8; 32]; - let mut y = [0u8; 32]; - my_pk.to_coordinates(&mut x, &mut y); - log::trace!("MY pk coords (device_pk): x: {:?} y: {:?}", x, y); - let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); - log::trace!("shared_secret (expected_ss): {:?}", shared_secret); - - if !verify_pin_auth(&shared_secret, authenticated_message, pin_auth) { - log::debug!("pin_auth invalid"); - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - - let aes_enc_key = &shared_secret; - let aes_dec_key = aes_enc_key; - Ok(*aes_dec_key) - } - - fn process_get_pin_retries( - &self, - persistent_store: &PersistentStore, - ) -> Result { - Ok(AuthenticatorClientPinResponse { - key_agreement: None, - pin_token: None, - retries: Some(persistent_store.pin_retries()? as u64), - }) - } - - fn process_get_key_agreement(&self) -> Result { - let pk = self.key_agreement_key.genpk(); - log::info!("GEN pk: {:?}", pk); - Ok(AuthenticatorClientPinResponse { - key_agreement: Some(CoseKey::from(pk)), - pin_token: None, - retries: None, - }) - } - - fn process_set_pin( - &mut self, - persistent_store: &mut PersistentStore, - key_agreement: CoseKey, - pin_auth: Vec, - new_pin_enc: Vec, - ) -> Result<(), Ctap2StatusCode> { - if persistent_store.pin_hash()?.is_some() { - log::debug!("pin_hash() is_some()"); - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - let pin_decryption_key = - self.exchange_decryption_key(key_agreement, &pin_auth, &new_pin_enc)?; - log::info!("exchanged decryption key"); - check_and_store_new_pin(persistent_store, &pin_decryption_key, new_pin_enc)?; - persistent_store.reset_pin_retries()?; - Ok(()) - } - - fn process_change_pin( - &mut self, - rng: &mut impl Rng256, - persistent_store: &mut PersistentStore, - key_agreement: CoseKey, - pin_auth: Vec, - new_pin_enc: Vec, - pin_hash_enc: Vec, - ) -> Result<(), Ctap2StatusCode> { - if persistent_store.pin_retries()? == 0 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); - } - let mut auth_param_data = new_pin_enc.clone(); - auth_param_data.extend(&pin_hash_enc); - let pin_decryption_key = - self.exchange_decryption_key(key_agreement, &pin_auth, &auth_param_data)?; - self.verify_pin_hash_enc(rng, persistent_store, &pin_decryption_key, pin_hash_enc)?; - - check_and_store_new_pin(persistent_store, &pin_decryption_key, new_pin_enc)?; - self.pin_uv_auth_token = rng.gen_uniform_u8x32(); - Ok(()) - } - - fn process_get_pin_token( - &mut self, - rng: &mut impl Rng256, - persistent_store: &mut PersistentStore, - key_agreement: CoseKey, - pin_hash_enc: Vec, - ) -> Result { - if persistent_store.pin_retries()? == 0 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); - } - let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; - let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); - - let token_encryption_key = &shared_secret; - let pin_decryption_key = token_encryption_key; - self.verify_pin_hash_enc(rng, persistent_store, &pin_decryption_key, pin_hash_enc)?; - - // Assuming PIN_TOKEN_LENGTH % block_size == 0 here. - let iv = [0u8; 16]; - let mut blocks = [[0u8; 16]; PIN_TOKEN_LENGTH / 16]; - for (i, item) in blocks.iter_mut().take(PIN_TOKEN_LENGTH / 16).enumerate() { - item.copy_from_slice(&self.pin_uv_auth_token[i * 16..(i + 1) * 16]); - } - cbc_encrypt(&token_encryption_key, iv, &mut blocks); - let pin_token: Vec = blocks.iter().flatten().cloned().collect(); - - #[cfg(feature = "with_ctap2_1")] - { - self.permissions = 0x03; - self.permissions_rp_id = None; - } - - Ok(AuthenticatorClientPinResponse { - key_agreement: None, - pin_token: Some(pin_token), - retries: None, - }) - } - - #[cfg(feature = "with_ctap2_1")] - fn process_get_pin_uv_auth_token_using_uv_with_permissions( - &self, - // If you want to support local user verification, implement this function. - // Lacking a fingerprint reader, this subcommand is currently unsupported. - _key_agreement: CoseKey, - _permissions: u8, - _permissions_rp_id: Option, - ) -> Result { - // User verifications is only supported through PIN currently. - #[cfg(not(feature = "with_ctap2_1"))] - { - Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND) - } - #[cfg(feature = "with_ctap2_1")] - { - Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND) - } - } - - #[cfg(feature = "with_ctap2_1")] - fn process_get_uv_retries(&self) -> Result { - // User verifications is only supported through PIN currently. - #[cfg(not(feature = "with_ctap2_1"))] - { - Err(Ctap2StatusCode::CTAP1_ERR_INVALID_COMMAND) - } - #[cfg(feature = "with_ctap2_1")] - { - Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND) - } - } - - #[cfg(feature = "with_ctap2_1")] - fn process_set_min_pin_length( - &mut self, - persistent_store: &mut PersistentStore, - min_pin_length: u8, - min_pin_length_rp_ids: Option>, - pin_auth: Option>, - ) -> Result<(), Ctap2StatusCode> { - if min_pin_length_rp_ids.is_some() { - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION); - } - if persistent_store.pin_hash()?.is_some() { - match pin_auth { - Some(pin_auth) => { - if self.consecutive_pin_mismatches >= 3 { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED); - } - // TODO(kaczmarczyck) Values are taken from the (not yet public) new revision - // of CTAP 2.1. The code should link the specification when published. - // From CTAP2.1: "If request contains pinUvAuthParam, the Authenticator calls - // verify(pinUvAuthToken, 32×0xff || 0x0608 || uint32LittleEndian(minPINLength) - // || minPinLengthRPIDs, pinUvAuthParam)" - let mut message = vec![0xFF; 32]; - message.extend(&[0x06, 0x08]); - message.extend(&[min_pin_length as u8, 0x00, 0x00, 0x00]); - // TODO(kaczmarczyck) commented code is useful for the extension - // https://github.com/google/OpenSK/issues/129 - // if !cbor::write(cbor_array_vec!(min_pin_length_rp_ids), &mut message) { - // return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR); - // } - if !verify_pin_auth(&self.pin_uv_auth_token, &message, &pin_auth) { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - } - None => return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID), - }; - } - if min_pin_length < persistent_store.min_pin_length()? { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION); - } - persistent_store.set_min_pin_length(min_pin_length)?; - // TODO(kaczmarczyck) commented code is useful for the extension - // https://github.com/google/OpenSK/issues/129 - // if let Some(min_pin_length_rp_ids) = min_pin_length_rp_ids { - // persistent_store.set_min_pin_length_rp_ids(min_pin_length_rp_ids)?; - // } - Ok(()) - } - - #[cfg(feature = "with_ctap2_1")] - fn process_get_pin_uv_auth_token_using_pin_with_permissions( - &mut self, - rng: &mut impl Rng256, - persistent_store: &mut PersistentStore, - key_agreement: CoseKey, - pin_hash_enc: Vec, - permissions: u8, - permissions_rp_id: Option, - ) -> Result { - if permissions == 0 { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - // This check is not mentioned protocol steps, but mentioned in a side note. - if permissions & 0x03 != 0 && permissions_rp_id.is_none() { - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - - let response = - self.process_get_pin_token(rng, persistent_store, key_agreement, pin_hash_enc)?; - - self.permissions = permissions; - self.permissions_rp_id = permissions_rp_id; - - Ok(response) - } - - pub fn process_subcommand( - &mut self, - rng: &mut impl Rng256, - persistent_store: &mut PersistentStore, - client_pin_params: AuthenticatorClientPinParameters, - ) -> Result { - let AuthenticatorClientPinParameters { - pin_protocol, - sub_command, - key_agreement, - pin_auth, - new_pin_enc, - pin_hash_enc, - #[cfg(feature = "with_ctap2_1")] - min_pin_length, - #[cfg(feature = "with_ctap2_1")] - min_pin_length_rp_ids, - #[cfg(feature = "with_ctap2_1")] - permissions, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id, - } = client_pin_params; - - if pin_protocol != 1 { - #[cfg(not(feature = "with_ctap2_1"))] - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - #[cfg(feature = "with_ctap2_1")] - return Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER); - } - log::info!("{:?}", sub_command); - let response = match sub_command { - ClientPinSubCommand::GetPinRetries => { - Some(self.process_get_pin_retries(persistent_store)?) - } - ClientPinSubCommand::GetKeyAgreement => Some(self.process_get_key_agreement()?), - ClientPinSubCommand::SetPin => { - self.process_set_pin( - persistent_store, - key_agreement.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - pin_auth.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - new_pin_enc.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - )?; - None - } - ClientPinSubCommand::ChangePin => { - self.process_change_pin( - rng, - persistent_store, - key_agreement.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - pin_auth.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - new_pin_enc.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - pin_hash_enc.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - )?; - None - } - ClientPinSubCommand::GetPinToken => Some(self.process_get_pin_token( - rng, - persistent_store, - key_agreement.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - pin_hash_enc.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - )?), - #[cfg(feature = "with_ctap2_1")] - ClientPinSubCommand::GetPinUvAuthTokenUsingUvWithPermissions => Some( - self.process_get_pin_uv_auth_token_using_uv_with_permissions( - key_agreement.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - permissions.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - permissions_rp_id, - )?, - ), - #[cfg(feature = "with_ctap2_1")] - ClientPinSubCommand::GetUvRetries => Some(self.process_get_uv_retries()?), - #[cfg(feature = "with_ctap2_1")] - ClientPinSubCommand::SetMinPinLength => { - self.process_set_min_pin_length( - persistent_store, - min_pin_length.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - min_pin_length_rp_ids, - pin_auth, - )?; - None - } - #[cfg(feature = "with_ctap2_1")] - ClientPinSubCommand::GetPinUvAuthTokenUsingPinWithPermissions => Some( - self.process_get_pin_uv_auth_token_using_pin_with_permissions( - rng, - persistent_store, - key_agreement.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - pin_hash_enc.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - permissions.ok_or(Ctap2StatusCode::CTAP2_ERR_MISSING_PARAMETER)?, - permissions_rp_id, - )?, - ), - }; - Ok(ResponseData::AuthenticatorClientPin(response)) - } - - pub fn verify_pin_auth_token(&self, hmac_contents: &[u8], pin_auth: &[u8]) -> bool { - verify_pin_auth(&self.pin_uv_auth_token, &hmac_contents, &pin_auth) - } - - pub fn reset(&mut self, rng: &mut impl Rng256) { - self.key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); - self.pin_uv_auth_token = rng.gen_uniform_u8x32(); - self.consecutive_pin_mismatches = 0; - #[cfg(feature = "with_ctap2_1")] - { - self.permissions = 0; - self.permissions_rp_id = None; - } - } - - pub fn process_hmac_secret( - &self, - hmac_secret_input: GetAssertionHmacSecretInput, - cred_random: &[u8; 32], - ) -> Result, Ctap2StatusCode> { - let GetAssertionHmacSecretInput { - key_agreement, - salt_enc, - salt_auth, - } = hmac_secret_input; - let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; - let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); - // HMAC-secret does the same 16 byte truncated check. - if !verify_pin_auth(&shared_secret, &salt_enc, &salt_auth) { - // Hard to tell what the correct error code here is. - return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION); - } - encrypt_hmac_secret_output(&shared_secret, &salt_enc[..], cred_random) - } - - #[cfg(feature = "with_ctap2_1")] - pub fn has_permission(&self, permission: PinPermission) -> Result<(), Ctap2StatusCode> { - // Relies on the fact that all permissions are represented by powers of two. - if permission as u8 & self.permissions != 0 { - Ok(()) - } else { - Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) - } - } - - #[cfg(feature = "with_ctap2_1")] - pub fn has_permission_for_rp_id(&mut self, rp_id: &str) -> Result<(), Ctap2StatusCode> { - if let Some(permissions_rp_id) = &self.permissions_rp_id { - if rp_id != permissions_rp_id { - return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); - } - } else { - self.permissions_rp_id = Some(String::from(rp_id)); - } - Ok(()) - } - - #[cfg(test)] - pub fn new_test( - key_agreement_key: ctap_crypto::ecdh::SecKey, - pin_uv_auth_token: [u8; 32], - ) -> PinProtocolV1 { - PinProtocolV1 { - key_agreement_key, - pin_uv_auth_token, - consecutive_pin_mismatches: 0, - #[cfg(feature = "with_ctap2_1")] - permissions: 0xFF, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id: None, - } - } -} - -#[cfg(test)] -mod test { - use super::*; - use ctap_crypto::rng256::ThreadRng256; - - // Stores a PIN hash corresponding to the dummy PIN "1234". - fn set_standard_pin(persistent_store: &mut PersistentStore) { - let mut pin = [0u8; 64]; - pin[..4].copy_from_slice(b"1234"); - let mut pin_hash = [0u8; 16]; - pin_hash.copy_from_slice(&Sha256::hash(&pin[..])[..16]); - persistent_store.set_pin_hash(&pin_hash).unwrap(); - } - - // Encrypts the message with a zero IV and key derived from shared_secret. - fn encrypt_message(shared_secret: &[u8; 32], message: &[u8]) -> Vec { - assert!(message.len() % 16 == 0); - let block_len = message.len() / 16; - let mut blocks = vec![[0u8; 16]; block_len]; - for i in 0..block_len { - blocks[i][..].copy_from_slice(&message[i * 16..(i + 1) * 16]); - } - let aes_enc_key = shared_secret; - let iv = [0u8; 16]; - cbc_encrypt(&aes_enc_key, iv, &mut blocks); - blocks.iter().flatten().cloned().collect::>() - } - - // Decrypts the message with a zero IV and key derived from shared_secret. - fn decrypt_message(shared_secret: &[u8; 32], message: &[u8]) -> Vec { - assert!(message.len() % 16 == 0); - let block_len = message.len() / 16; - let mut blocks = vec![[0u8; 16]; block_len]; - for i in 0..block_len { - blocks[i][..].copy_from_slice(&message[i * 16..(i + 1) * 16]); - } - let aes_enc_key = shared_secret; - let aes_dec_key = aes_enc_key; - let iv = [0u8; 16]; - cbc_decrypt(&aes_dec_key, iv, &mut blocks); - blocks.iter().flatten().cloned().collect::>() - } - - // Fails on PINs bigger than 64 bytes. - fn encrypt_pin(shared_secret: &[u8; 32], pin: Vec) -> Vec { - assert!(pin.len() <= 64); - let mut padded_pin = [0u8; 64]; - padded_pin[..pin.len()].copy_from_slice(&pin[..]); - encrypt_message(shared_secret, &padded_pin) - } - - // Encrypts the dummy PIN "1234". - fn encrypt_standard_pin(shared_secret: &[u8; 32]) -> Vec { - encrypt_pin(shared_secret, b"1234".to_vec()) - } - - // Encrypts the PIN hash corresponding to the dummy PIN "1234". - fn encrypt_standard_pin_hash(shared_secret: &[u8; 32]) -> Vec { - let mut pin = [0u8; 64]; - pin[..4].copy_from_slice(b"1234"); - let pin_hash = Sha256::hash(&pin); - encrypt_message(shared_secret, &pin_hash[..16]) - } - - #[test] - fn test_verify_pin_hash_enc() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - // The PIN is "1234". - let pin_hash = [ - 0x01, 0xD9, 0x88, 0x40, 0x50, 0xBB, 0xD0, 0x7A, 0x23, 0x1A, 0xEB, 0x69, 0xD8, 0x36, - 0xC4, 0x12, - ]; - persistent_store.set_pin_hash(&pin_hash).unwrap(); - let shared_secret = [0x88; 32]; - let aes_enc_key = &shared_secret; - let aes_dec_key = aes_enc_key; - - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pin_hash_enc = vec![ - 0x8D, 0x7A, 0xA3, 0x9F, 0x7F, 0xC6, 0x08, 0x13, 0x9A, 0xC8, 0x56, 0x97, 0x70, 0x74, - 0x99, 0x66, - ]; - assert_eq!( - pin_protocol_v1.verify_pin_hash_enc( - &mut rng, - &mut persistent_store, - &aes_dec_key, - pin_hash_enc - ), - Ok(()) - ); - - let pin_hash_enc = vec![0xEE; 16]; - assert_eq!( - pin_protocol_v1.verify_pin_hash_enc( - &mut rng, - &mut persistent_store, - &aes_dec_key, - pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) - ); - - let pin_hash_enc = vec![ - 0x8D, 0x7A, 0xA3, 0x9F, 0x7F, 0xC6, 0x08, 0x13, 0x9A, 0xC8, 0x56, 0x97, 0x70, 0x74, - 0x99, 0x66, - ]; - pin_protocol_v1.consecutive_pin_mismatches = 3; - assert_eq!( - pin_protocol_v1.verify_pin_hash_enc( - &mut rng, - &mut persistent_store, - &aes_dec_key, - pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_BLOCKED) - ); - pin_protocol_v1.consecutive_pin_mismatches = 0; - - let pin_hash_enc = vec![0x77; PIN_AUTH_LENGTH - 1]; - assert_eq!( - pin_protocol_v1.verify_pin_hash_enc( - &mut rng, - &mut persistent_store, - &aes_dec_key, - pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) - ); - - let pin_hash_enc = vec![0x77; PIN_AUTH_LENGTH + 1]; - assert_eq!( - pin_protocol_v1.verify_pin_hash_enc( - &mut rng, - &mut persistent_store, - &aes_dec_key, - pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) - ); - } - - #[test] - fn test_process_get_pin_retries() { - let mut rng = ThreadRng256 {}; - let persistent_store = PersistentStore::new(&mut rng); - let pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let expected_response = Ok(AuthenticatorClientPinResponse { - key_agreement: None, - pin_token: None, - retries: Some(persistent_store.pin_retries().unwrap() as u64), - }); - assert_eq!( - pin_protocol_v1.process_get_pin_retries(&persistent_store), - expected_response - ); - } - - #[test] - fn test_process_get_key_agreement() { - let mut rng = ThreadRng256 {}; - let pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pk = pin_protocol_v1.key_agreement_key.genpk(); - let expected_response = Ok(AuthenticatorClientPinResponse { - key_agreement: Some(CoseKey::from(pk)), - pin_token: None, - retries: None, - }); - assert_eq!( - pin_protocol_v1.process_get_key_agreement(), - expected_response - ); - } - - #[test] - fn test_process_set_pin() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pk = pin_protocol_v1.key_agreement_key.genpk(); - let shared_secret = pin_protocol_v1.key_agreement_key.exchange_x_sha256(&pk); - let key_agreement = CoseKey::from(pk); - let new_pin_enc = encrypt_standard_pin(&shared_secret); - let pin_auth = hmac_256::(&shared_secret, &new_pin_enc[..])[..16].to_vec(); - assert_eq!( - pin_protocol_v1.process_set_pin( - &mut persistent_store, - key_agreement, - pin_auth, - new_pin_enc - ), - Ok(()) - ); - } - - #[test] - fn test_process_change_pin() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - set_standard_pin(&mut persistent_store); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pk = pin_protocol_v1.key_agreement_key.genpk(); - let shared_secret = pin_protocol_v1.key_agreement_key.exchange_x_sha256(&pk); - let key_agreement = CoseKey::from(pk); - let new_pin_enc = encrypt_standard_pin(&shared_secret); - let pin_hash_enc = encrypt_standard_pin_hash(&shared_secret); - let mut auth_param_data = new_pin_enc.clone(); - auth_param_data.extend(&pin_hash_enc); - let pin_auth = hmac_256::(&shared_secret, &auth_param_data[..])[..16].to_vec(); - assert_eq!( - pin_protocol_v1.process_change_pin( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_auth.clone(), - new_pin_enc.clone(), - pin_hash_enc.clone() - ), - Ok(()) - ); - - let bad_pin_hash_enc = vec![0xEE; 16]; - assert_eq!( - pin_protocol_v1.process_change_pin( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_auth.clone(), - new_pin_enc.clone(), - bad_pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) - ); - - while persistent_store.pin_retries().unwrap() > 0 { - persistent_store.decr_pin_retries().unwrap(); - } - assert_eq!( - pin_protocol_v1.process_change_pin( - &mut rng, - &mut persistent_store, - key_agreement, - pin_auth, - new_pin_enc, - pin_hash_enc, - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED) - ); - } - - #[test] - fn test_process_get_pin_token() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - set_standard_pin(&mut persistent_store); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pk = pin_protocol_v1.key_agreement_key.genpk(); - let shared_secret = pin_protocol_v1.key_agreement_key.exchange_x_sha256(&pk); - let key_agreement = CoseKey::from(pk); - let pin_hash_enc = encrypt_standard_pin_hash(&shared_secret); - assert!(pin_protocol_v1 - .process_get_pin_token( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_hash_enc - ) - .is_ok()); - - let pin_hash_enc = vec![0xEE; 16]; - assert_eq!( - pin_protocol_v1.process_get_pin_token( - &mut rng, - &mut persistent_store, - key_agreement, - pin_hash_enc - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) - ); - } - - #[cfg(feature = "with_ctap2_1")] - #[test] - fn test_process_get_pin_uv_auth_token_using_pin_with_permissions() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - set_standard_pin(&mut persistent_store); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let pk = pin_protocol_v1.key_agreement_key.genpk(); - let shared_secret = pin_protocol_v1.key_agreement_key.exchange_x_sha256(&pk); - let key_agreement = CoseKey::from(pk); - let pin_hash_enc = encrypt_standard_pin_hash(&shared_secret); - assert!(pin_protocol_v1 - .process_get_pin_uv_auth_token_using_pin_with_permissions( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_hash_enc.clone(), - 0x03, - Some(String::from("example.com")), - ) - .is_ok()); - assert_eq!(pin_protocol_v1.permissions, 0x03); - assert_eq!( - pin_protocol_v1.permissions_rp_id, - Some(String::from("example.com")) - ); - - assert_eq!( - pin_protocol_v1.process_get_pin_uv_auth_token_using_pin_with_permissions( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_hash_enc.clone(), - 0x00, - Some(String::from("example.com")), - ), - Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) - ); - - assert_eq!( - pin_protocol_v1.process_get_pin_uv_auth_token_using_pin_with_permissions( - &mut rng, - &mut persistent_store, - key_agreement.clone(), - pin_hash_enc.clone(), - 0x03, - None, - ), - Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER) - ); - - let pin_hash_enc = vec![0xEE; 16]; - assert_eq!( - pin_protocol_v1.process_get_pin_uv_auth_token_using_pin_with_permissions( - &mut rng, - &mut persistent_store, - key_agreement, - pin_hash_enc, - 0x03, - Some(String::from("example.com")), - ), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_INVALID) - ); - } - - #[cfg(feature = "with_ctap2_1")] - #[test] - fn test_process_set_min_pin_length() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let min_pin_length = 8; - pin_protocol_v1.pin_uv_auth_token = [0x55; PIN_TOKEN_LENGTH]; - let pin_auth = vec![ - 0x94, 0x86, 0xEF, 0x4C, 0xB3, 0x84, 0x2C, 0x85, 0x72, 0x02, 0xBF, 0xE4, 0x36, 0x22, - 0xFE, 0xC9, - ]; - // TODO(kaczmarczyck) implement test for the min PIN length extension - // https://github.com/google/OpenSK/issues/129 - let response = pin_protocol_v1.process_set_min_pin_length( - &mut persistent_store, - min_pin_length, - None, - Some(pin_auth.clone()), - ); - assert_eq!(response, Ok(())); - assert_eq!(persistent_store.min_pin_length().unwrap(), min_pin_length); - let response = pin_protocol_v1.process_set_min_pin_length( - &mut persistent_store, - 7, - None, - Some(pin_auth), - ); - assert_eq!( - response, - Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION) - ); - assert_eq!(persistent_store.min_pin_length().unwrap(), min_pin_length); - } - - #[test] - fn test_process() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - let client_pin_params = AuthenticatorClientPinParameters { - pin_protocol: 1, - sub_command: ClientPinSubCommand::GetPinRetries, - key_agreement: None, - pin_auth: None, - new_pin_enc: None, - pin_hash_enc: None, - #[cfg(feature = "with_ctap2_1")] - min_pin_length: None, - #[cfg(feature = "with_ctap2_1")] - min_pin_length_rp_ids: None, - #[cfg(feature = "with_ctap2_1")] - permissions: None, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id: None, - }; - assert!(pin_protocol_v1 - .process_subcommand(&mut rng, &mut persistent_store, client_pin_params) - .is_ok()); - - let client_pin_params = AuthenticatorClientPinParameters { - pin_protocol: 2, - sub_command: ClientPinSubCommand::GetPinRetries, - key_agreement: None, - pin_auth: None, - new_pin_enc: None, - pin_hash_enc: None, - #[cfg(feature = "with_ctap2_1")] - min_pin_length: None, - #[cfg(feature = "with_ctap2_1")] - min_pin_length_rp_ids: None, - #[cfg(feature = "with_ctap2_1")] - permissions: None, - #[cfg(feature = "with_ctap2_1")] - permissions_rp_id: None, - }; - #[cfg(not(feature = "with_ctap2_1"))] - let error_code = Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID; - #[cfg(feature = "with_ctap2_1")] - let error_code = Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER; - assert_eq!( - pin_protocol_v1.process_subcommand(&mut rng, &mut persistent_store, client_pin_params), - Err(error_code) - ); - } - - #[test] - fn test_decrypt_pin() { - let shared_secret = [0x88; 32]; - let aes_enc_key = &shared_secret; - let aes_dec_key = aes_enc_key; - - // "1234" - let new_pin_enc = vec![ - 0xC0, 0xCF, 0xAE, 0x4C, 0x79, 0x56, 0x87, 0x99, 0xE5, 0x83, 0x4F, 0xE6, 0x4D, 0xFE, - 0x53, 0x32, 0x36, 0x0D, 0xF9, 0x1E, 0x47, 0x66, 0x10, 0x5C, 0x63, 0x30, 0x1D, 0xCC, - 0x00, 0x09, 0x91, 0xA4, 0x20, 0x6B, 0x78, 0x10, 0xFE, 0xC6, 0x2E, 0x7E, 0x75, 0x14, - 0xEE, 0x01, 0x99, 0x6C, 0xD7, 0xE5, 0x2B, 0xA5, 0x7A, 0x5A, 0xE1, 0xEC, 0x69, 0x31, - 0x18, 0x35, 0x06, 0x66, 0x97, 0x84, 0x68, 0xC2, - ]; - assert_eq!( - decrypt_pin(&aes_dec_key, new_pin_enc), - Some(b"1234".to_vec()), - ); - - // "123" - let new_pin_enc = vec![ - 0xF3, 0x54, 0x29, 0x17, 0xD4, 0xF8, 0xCD, 0x23, 0x1D, 0x59, 0xED, 0xE5, 0x33, 0x42, - 0x13, 0x39, 0x22, 0xBB, 0x91, 0x28, 0x87, 0x6A, 0xF9, 0xB1, 0x80, 0x9C, 0x9D, 0x76, - 0xFF, 0xDD, 0xB8, 0xD6, 0x8D, 0x66, 0x99, 0xA2, 0x42, 0x67, 0xB0, 0x5C, 0x82, 0x3F, - 0x08, 0x55, 0x8C, 0x04, 0xC5, 0x91, 0xF0, 0xF9, 0x58, 0x44, 0x00, 0x1B, 0x99, 0xA6, - 0x7C, 0xC7, 0x2D, 0x43, 0x74, 0x4C, 0x1D, 0x7E, - ]; - assert_eq!( - decrypt_pin(&aes_dec_key, new_pin_enc), - Some(b"123".to_vec()), - ); - - // Encrypted PIN is too short. - let new_pin_enc = vec![0x44; 63]; - assert_eq!(decrypt_pin(&aes_dec_key, new_pin_enc), None,); - - // Encrypted PIN is too long. - let new_pin_enc = vec![0x44; 65]; - assert_eq!(decrypt_pin(&aes_dec_key, new_pin_enc), None,); - } - - #[test] - fn test_check_and_store_new_pin() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - let shared_secret = [0x88; 32]; - let aes_enc_key = &shared_secret; - let aes_dec_key = aes_enc_key; - - let test_cases = vec![ - // Accept PIN "1234". - (b"1234".to_vec(), Ok(())), - // Reject PIN "123" since it is too short. - ( - b"123".to_vec(), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), - ), - // Reject PIN "12'\0'4" (a zero byte at index 2). - ( - b"12\04".to_vec(), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), - ), - // PINs must be at most 63 bytes long, to allow for a trailing 0u8 padding. - ( - vec![0x30; 64], - Err(Ctap2StatusCode::CTAP2_ERR_PIN_POLICY_VIOLATION), - ), - ]; - for (pin, result) in test_cases { - let old_pin_hash = persistent_store.pin_hash().unwrap(); - let new_pin_enc = encrypt_pin(&shared_secret, pin); - assert_eq!( - check_and_store_new_pin(&mut persistent_store, &aes_dec_key, new_pin_enc), - result - ); - if result.is_ok() { - assert_ne!(old_pin_hash, persistent_store.pin_hash().unwrap()); - } else { - assert_eq!(old_pin_hash, persistent_store.pin_hash().unwrap()); - } - } - } - - #[test] - fn test_verify_pin_auth() { - let hmac_key = [0x88; 16]; - let pin_auth = [ - 0x88, 0x09, 0x41, 0x13, 0xF7, 0x97, 0x32, 0x0B, 0x3E, 0xD9, 0xBC, 0x76, 0x4F, 0x18, - 0x56, 0x5D, - ]; - assert!(verify_pin_auth(&hmac_key, &[], &pin_auth)); - assert!(!verify_pin_auth(&hmac_key, &[0x00], &pin_auth)); - } - - #[test] - fn test_encrypt_hmac_secret_output() { - let shared_secret = [0x55; 32]; - let salt_enc = [0x5E; 32]; - let cred_random = [0xC9; 32]; - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random); - assert_eq!(output.unwrap().len(), 32); - - let salt_enc = [0x5E; 48]; - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random); - assert_eq!( - output, - Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION) - ); - - let salt_enc = [0x5E; 64]; - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random); - assert_eq!(output.unwrap().len(), 64); - - let mut salt_enc = [0x00; 32]; - let cred_random = [0xC9; 32]; - - // Test values to check for reproducibility. - let salt1 = [0x01; 32]; - let salt2 = [0x02; 32]; - let expected_output1 = hmac_256::(&cred_random, &salt1); - let expected_output2 = hmac_256::(&cred_random, &salt2); - - let salt_enc1 = encrypt_message(&shared_secret, &salt1); - salt_enc.copy_from_slice(salt_enc1.as_slice()); - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random).unwrap(); - let output_dec = decrypt_message(&shared_secret, &output); - assert_eq!(&output_dec, &expected_output1); - - let salt_enc2 = &encrypt_message(&shared_secret, &salt2); - salt_enc.copy_from_slice(salt_enc2.as_slice()); - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random).unwrap(); - let output_dec = decrypt_message(&shared_secret, &output); - assert_eq!(&output_dec, &expected_output2); - - let mut salt_enc = [0x00; 64]; - let mut salt12 = [0x00; 64]; - salt12[..32].copy_from_slice(&salt1); - salt12[32..].copy_from_slice(&salt2); - let salt_enc12 = encrypt_message(&shared_secret, &salt12); - salt_enc.copy_from_slice(salt_enc12.as_slice()); - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random).unwrap(); - let output_dec = decrypt_message(&shared_secret, &output); - assert_eq!(&output_dec[..32], &expected_output1); - assert_eq!(&output_dec[32..], &expected_output2); - - let mut salt_enc = [0x00; 64]; - let mut salt02 = [0x00; 64]; - salt02[32..].copy_from_slice(&salt2); - let salt_enc02 = encrypt_message(&shared_secret, &salt02); - salt_enc.copy_from_slice(salt_enc02.as_slice()); - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random).unwrap(); - let output_dec = decrypt_message(&shared_secret, &output); - assert_eq!(&output_dec[32..], &expected_output2); - - let mut salt_enc = [0x00; 64]; - let mut salt10 = [0x00; 64]; - salt10[..32].copy_from_slice(&salt1); - let salt_enc10 = encrypt_message(&shared_secret, &salt10); - salt_enc.copy_from_slice(salt_enc10.as_slice()); - let output = encrypt_hmac_secret_output(&shared_secret, &salt_enc, &cred_random).unwrap(); - let output_dec = decrypt_message(&shared_secret, &output); - assert_eq!(&output_dec[..32], &expected_output1); - } - - #[cfg(feature = "with_ctap2_1")] - #[test] - fn test_has_permission() { - let mut rng = ThreadRng256 {}; - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - pin_protocol_v1.permissions = 0x7F; - for permission in PinPermission::into_enum_iter() { - assert_eq!(pin_protocol_v1.has_permission(permission), Ok(())); - } - pin_protocol_v1.permissions = 0x00; - for permission in PinPermission::into_enum_iter() { - assert_eq!( - pin_protocol_v1.has_permission(permission), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) - ); - } - } - - #[cfg(feature = "with_ctap2_1")] - #[test] - fn test_has_permission_for_rp_id() { - let mut rng = ThreadRng256 {}; - let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); - assert_eq!( - pin_protocol_v1.has_permission_for_rp_id("example.com"), - Ok(()) - ); - assert_eq!( - pin_protocol_v1.permissions_rp_id, - Some(String::from("example.com")) - ); - assert_eq!( - pin_protocol_v1.has_permission_for_rp_id("example.com"), - Ok(()) - ); - assert_eq!( - pin_protocol_v1.has_permission_for_rp_id("counter-example.com"), - Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) - ); - } -} diff --git a/apps/vault/src/ctap/response.rs b/apps/vault/src/ctap/response.rs index bd5294a38..d140bed13 100644 --- a/apps/vault/src/ctap/response.rs +++ b/apps/vault/src/ctap/response.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,19 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[cfg(feature = "with_ctap2_1")] -use super::data_formats::{AuthenticatorTransport, PublicKeyCredentialParameter}; use super::data_formats::{ - CoseKey, PackedAttestationStatement, PublicKeyCredentialDescriptor, + AuthenticatorTransport, CoseKey, CredentialProtectionPolicy, PackedAttestationStatement, + PublicKeyCredentialDescriptor, PublicKeyCredentialParameter, PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity, }; -use std::collections::BTreeMap; use std::string::String; use std::vec::Vec; -use cbor::{cbor_array_vec, cbor_bool, cbor_map_btree, cbor_map_options, cbor_text}; +use cbor as cbor; +use cbor::{ + cbor_array_vec, cbor_bool, cbor_int, cbor_map_collection, cbor_map_options, cbor_text, +}; -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] +#[allow(clippy::enum_variant_names)] pub enum ResponseData { AuthenticatorMakeCredential(AuthenticatorMakeCredentialResponse), AuthenticatorGetAssertion(AuthenticatorGetAssertionResponse), @@ -32,9 +33,13 @@ pub enum ResponseData { AuthenticatorGetInfo(AuthenticatorGetInfoResponse), AuthenticatorClientPin(Option), AuthenticatorReset, - #[cfg(feature = "with_ctap2_1")] + AuthenticatorCredentialManagement(Option), AuthenticatorSelection, - AuthenticatorVendor(AuthenticatorVendorResponse), + AuthenticatorLargeBlobs(Option), + AuthenticatorConfig, + AuthenticatorVendorConfigure(AuthenticatorVendorConfigureResponse), + AuthenticatorVendorUpgrade, + AuthenticatorVendorUpgradeInfo(AuthenticatorVendorUpgradeInfoResponse), } impl From for Option { @@ -44,22 +49,26 @@ impl From for Option { ResponseData::AuthenticatorGetAssertion(data) => Some(data.into()), ResponseData::AuthenticatorGetNextAssertion(data) => Some(data.into()), ResponseData::AuthenticatorGetInfo(data) => Some(data.into()), - ResponseData::AuthenticatorClientPin(Some(data)) => Some(data.into()), - ResponseData::AuthenticatorClientPin(None) => None, + ResponseData::AuthenticatorClientPin(data) => data.map(|d| d.into()), ResponseData::AuthenticatorReset => None, - #[cfg(feature = "with_ctap2_1")] + ResponseData::AuthenticatorCredentialManagement(data) => data.map(|d| d.into()), ResponseData::AuthenticatorSelection => None, - ResponseData::AuthenticatorVendor(data) => Some(data.into()), + ResponseData::AuthenticatorLargeBlobs(data) => data.map(|d| d.into()), + ResponseData::AuthenticatorConfig => None, + ResponseData::AuthenticatorVendorConfigure(data) => Some(data.into()), + ResponseData::AuthenticatorVendorUpgrade => None, + ResponseData::AuthenticatorVendorUpgradeInfo(data) => Some(data.into()), } } } -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] pub struct AuthenticatorMakeCredentialResponse { pub fmt: String, pub auth_data: Vec, pub att_stmt: PackedAttestationStatement, + pub ep_att: Option, + pub large_blob_key: Option>, } impl From for cbor::Value { @@ -68,24 +77,29 @@ impl From for cbor::Value { fmt, auth_data, att_stmt, + ep_att, + large_blob_key, } = make_credential_response; cbor_map_options! { - 1 => fmt, - 2 => auth_data, - 3 => att_stmt, + 0x01 => fmt, + 0x02 => auth_data, + 0x03 => att_stmt, + 0x04 => ep_att, + 0x05 => large_blob_key, } } } -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] pub struct AuthenticatorGetAssertionResponse { pub credential: Option, pub auth_data: Vec, pub signature: Vec, pub user: Option, pub number_of_credentials: Option, + // 0x06: userSelected missing as we don't support displays. + pub large_blob_key: Option>, } impl From for cbor::Value { @@ -96,45 +110,49 @@ impl From for cbor::Value { signature, user, number_of_credentials, + large_blob_key, } = get_assertion_response; cbor_map_options! { - 1 => credential, - 2 => auth_data, - 3 => signature, - 4 => user, - 5 => number_of_credentials, + 0x01 => credential, + 0x02 => auth_data, + 0x03 => signature, + 0x04 => user, + 0x05 => number_of_credentials, + 0x07 => large_blob_key, } } } -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] pub struct AuthenticatorGetInfoResponse { - // TODO(kaczmarczyck) add maxAuthenticatorConfigLength and defaultCredProtect pub versions: Vec, pub extensions: Option>, pub aaguid: [u8; 16], - pub options: Option>, + pub options: Option>, pub max_msg_size: Option, pub pin_protocols: Option>, - #[cfg(feature = "with_ctap2_1")] pub max_credential_count_in_list: Option, - #[cfg(feature = "with_ctap2_1")] pub max_credential_id_length: Option, - #[cfg(feature = "with_ctap2_1")] pub transports: Option>, - #[cfg(feature = "with_ctap2_1")] pub algorithms: Option>, - pub default_cred_protect: bool, - #[cfg(feature = "with_ctap2_1")] + pub max_serialized_large_blob_array: Option, + pub force_pin_change: Option, pub min_pin_length: u8, - #[cfg(feature = "with_ctap2_1")] pub firmware_version: Option, + pub max_cred_blob_length: Option, + pub max_rp_ids_for_set_min_pin_length: Option, + // Missing response fields as they are only relevant for internal UV: + // - 0x11: preferredPlatformUvAttempts + // - 0x12: uvModality + // Add them when your hardware supports any kind of user verification within + // the boundary of the device, e.g. fingerprint or built-in keyboard. + pub certifications: Option>, + pub remaining_discoverable_credentials: Option, + // - 0x15: vendorPrototypeConfigCommands missing as we don't support it. } impl From for cbor::Value { - #[cfg(feature = "with_ctap2_1")] fn from(get_info_response: AuthenticatorGetInfoResponse) -> Self { let AuthenticatorGetInfoResponse { versions, @@ -147,17 +165,30 @@ impl From for cbor::Value { max_credential_id_length, transports, algorithms, - default_cred_protect, + max_serialized_large_blob_array, + force_pin_change, min_pin_length, firmware_version, + max_cred_blob_length, + max_rp_ids_for_set_min_pin_length, + certifications, + remaining_discoverable_credentials, } = get_info_response; let options_cbor: Option = options.map(|options| { - let option_map: BTreeMap<_, _> = options + let options_map: Vec<(_, _)> = options .into_iter() .map(|(key, value)| (cbor_text!(key), cbor_bool!(value))) .collect(); - cbor_map_btree!(option_map) + cbor_map_collection!(options_map) + }); + + let certifications_cbor: Option = certifications.map(|certifications| { + let certifications_map: Vec<(_, _)> = certifications + .into_iter() + .map(|(key, value)| (cbor_text!(key), cbor_int!(value))) + .collect(); + cbor_map_collection!(certifications_map) }); cbor_map_options! { @@ -171,100 +202,153 @@ impl From for cbor::Value { 0x08 => max_credential_id_length, 0x09 => transports.map(|vec| cbor_array_vec!(vec)), 0x0A => algorithms.map(|vec| cbor_array_vec!(vec)), - 0x0C => default_cred_protect, + 0x0B => max_serialized_large_blob_array, + 0x0C => force_pin_change, 0x0D => min_pin_length as u64, 0x0E => firmware_version, - } - } - - #[cfg(not(feature = "with_ctap2_1"))] - fn from(get_info_response: AuthenticatorGetInfoResponse) -> Self { - let AuthenticatorGetInfoResponse { - versions, - extensions, - aaguid, - options, - max_msg_size, - pin_protocols, - default_cred_protect, - } = get_info_response; - - let options_cbor: Option = options.map(|options| { - let option_map: BTreeMap<_, _> = options - .into_iter() - .map(|(key, value)| (cbor_text!(key), cbor_bool!(value))) - .collect(); - cbor_map_btree!(option_map) - }); - - cbor_map_options! { - 0x01 => cbor_array_vec!(versions), - 0x02 => extensions.map(|vec| cbor_array_vec!(vec)), - 0x03 => &aaguid, - 0x04 => options_cbor, - 0x05 => max_msg_size, - 0x06 => pin_protocols.map(|vec| cbor_array_vec!(vec)), - 0x0C => default_cred_protect.map(|p| p as u64), + 0x0F => max_cred_blob_length, + 0x10 => max_rp_ids_for_set_min_pin_length, + 0x13 => certifications_cbor, + 0x14 => remaining_discoverable_credentials, } } } -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] +#[derive(Debug, PartialEq, Eq)] pub struct AuthenticatorClientPinResponse { pub key_agreement: Option, - pub pin_token: Option>, + pub pin_uv_auth_token: Option>, pub retries: Option, + pub power_cycle_state: Option, + // - 0x05: uvRetries missing as we don't support internal UV. } impl From for cbor::Value { fn from(client_pin_response: AuthenticatorClientPinResponse) -> Self { let AuthenticatorClientPinResponse { key_agreement, - pin_token, + pin_uv_auth_token, retries, + power_cycle_state, } = client_pin_response; cbor_map_options! { - 1 => key_agreement.map(|cose_key| cbor_map_btree!(cose_key.0)), - 2 => pin_token, - 3 => retries, + 0x01 => key_agreement.map(cbor::Value::from), + 0x02 => pin_uv_auth_token, + 0x03 => retries, + 0x04 => power_cycle_state, } } } -#[cfg_attr(test, derive(PartialEq))] -#[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug))] -pub struct AuthenticatorVendorResponse { +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorLargeBlobsResponse { + pub config: Vec, +} + +impl From for cbor::Value { + fn from(platform_large_blobs_response: AuthenticatorLargeBlobsResponse) -> Self { + let AuthenticatorLargeBlobsResponse { config } = platform_large_blobs_response; + + cbor_map_options! { + 0x01 => config, + } + } +} + +#[derive(Debug, Default, PartialEq, Eq)] +pub struct AuthenticatorCredentialManagementResponse { + pub existing_resident_credentials_count: Option, + pub max_possible_remaining_resident_credentials_count: Option, + pub rp: Option, + pub rp_id_hash: Option>, + pub total_rps: Option, + pub user: Option, + pub credential_id: Option, + pub public_key: Option, + pub total_credentials: Option, + pub cred_protect: Option, + pub large_blob_key: Option>, +} + +impl From for cbor::Value { + fn from(cred_management_response: AuthenticatorCredentialManagementResponse) -> Self { + let AuthenticatorCredentialManagementResponse { + existing_resident_credentials_count, + max_possible_remaining_resident_credentials_count, + rp, + rp_id_hash, + total_rps, + user, + credential_id, + public_key, + total_credentials, + cred_protect, + large_blob_key, + } = cred_management_response; + + cbor_map_options! { + 0x01 => existing_resident_credentials_count, + 0x02 => max_possible_remaining_resident_credentials_count, + 0x03 => rp, + 0x04 => rp_id_hash, + 0x05 => total_rps, + 0x06 => user, + 0x07 => credential_id, + 0x08 => public_key.map(cbor::Value::from), + 0x09 => total_credentials, + 0x0A => cred_protect, + 0x0B => large_blob_key, + } + } +} + +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorVendorConfigureResponse { pub cert_programmed: bool, pub pkey_programmed: bool, } -impl From for cbor::Value { - fn from(vendor_response: AuthenticatorVendorResponse) -> Self { - let AuthenticatorVendorResponse { +impl From for cbor::Value { + fn from(vendor_response: AuthenticatorVendorConfigureResponse) -> Self { + let AuthenticatorVendorConfigureResponse { cert_programmed, pkey_programmed, } = vendor_response; cbor_map_options! { - 1 => cert_programmed, - 2 => pkey_programmed, + 0x01 => cert_programmed, + 0x02 => pkey_programmed, + } + } +} + +#[derive(Debug, PartialEq, Eq)] +pub struct AuthenticatorVendorUpgradeInfoResponse { + pub info: u32, +} + +impl From for cbor::Value { + fn from(vendor_upgrade_info_response: AuthenticatorVendorUpgradeInfoResponse) -> Self { + let AuthenticatorVendorUpgradeInfoResponse { info } = vendor_upgrade_info_response; + + cbor_map_options! { + 0x01 => info as u64, } } } #[cfg(test)] mod test { - use super::super::data_formats::PackedAttestationStatement; - #[cfg(feature = "with_ctap2_1")] + use super::super::data_formats::{PackedAttestationStatement, PublicKeyCredentialType}; use super::super::ES256_CRED_PARAM; use super::*; - use cbor::{cbor_bytes, cbor_map}; + use crate::env::test::TestEnv; + use cbor::{cbor_array, cbor_bytes, cbor_map}; #[test] fn test_make_credential_into_cbor() { - let certificate: cbor::values::KeyType = cbor_bytes![vec![0x5C, 0x5C, 0x5C, 0x5C]]; + let certificate = cbor_bytes![vec![0x5C, 0x5C, 0x5C, 0x5C]]; let att_stmt = PackedAttestationStatement { alg: 1, sig: vec![0x55, 0x55, 0x55, 0x55], @@ -274,7 +358,7 @@ mod test { let cbor_packed_attestation_statement = cbor_map! { "alg" => 1, "sig" => vec![0x55, 0x55, 0x55, 0x55], - "x5c" => cbor_array_vec![vec![certificate]], + "x5c" => cbor_array![certificate], "ecdaaKeyId" => vec![0xEC, 0xDA, 0x1D], }; @@ -282,31 +366,60 @@ mod test { fmt: "packed".to_string(), auth_data: vec![0xAD], att_stmt, + ep_att: Some(true), + large_blob_key: Some(vec![0x1B]), }; let response_cbor: Option = ResponseData::AuthenticatorMakeCredential(make_credential_response).into(); let expected_cbor = cbor_map_options! { - 1 => "packed", - 2 => vec![0xAD], - 3 => cbor_packed_attestation_statement, + 0x01 => "packed", + 0x02 => vec![0xAD], + 0x03 => cbor_packed_attestation_statement, + 0x04 => true, + 0x05 => vec![0x1B], }; assert_eq!(response_cbor, Some(expected_cbor)); } #[test] fn test_get_assertion_into_cbor() { + let pub_key_cred_descriptor = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: vec![0x2D, 0x2D, 0x2D, 0x2D], + transports: Some(vec![AuthenticatorTransport::Usb]), + }; + let user = PublicKeyCredentialUserEntity { + user_id: vec![0x1D, 0x1D, 0x1D, 0x1D], + user_name: Some("foo".to_string()), + user_display_name: Some("bar".to_string()), + user_icon: Some("example.com/foo/icon.png".to_string()), + }; let get_assertion_response = AuthenticatorGetAssertionResponse { - credential: None, + credential: Some(pub_key_cred_descriptor), auth_data: vec![0xAD], signature: vec![0x51], - user: None, - number_of_credentials: None, + user: Some(user), + number_of_credentials: Some(2), + large_blob_key: Some(vec![0x1B]), }; let response_cbor: Option = ResponseData::AuthenticatorGetAssertion(get_assertion_response).into(); let expected_cbor = cbor_map_options! { - 2 => vec![0xAD], - 3 => vec![0x51], + 0x01 => cbor_map! { + "id" => vec![0x2D, 0x2D, 0x2D, 0x2D], + "type" => "public-key", + "transports" => cbor_array!["usb"], + }, + 0x02 => vec![0xAD], + 0x03 => vec![0x51], + 0x04 => cbor_map! { + "id" => vec![0x1D, 0x1D, 0x1D, 0x1D], + "icon" => "example.com/foo/icon.png".to_string(), + "name" => "foo".to_string(), + "displayName" => "bar".to_string(), + }, + 0x05 => 2, + 0x07 => vec![0x1B], }; assert_eq!(response_cbor, Some(expected_cbor)); } @@ -321,28 +434,21 @@ mod test { options: None, max_msg_size: None, pin_protocols: None, - #[cfg(feature = "with_ctap2_1")] max_credential_count_in_list: None, - #[cfg(feature = "with_ctap2_1")] max_credential_id_length: None, - #[cfg(feature = "with_ctap2_1")] transports: None, - #[cfg(feature = "with_ctap2_1")] algorithms: None, - default_cred_protect: None, - #[cfg(feature = "with_ctap2_1")] + max_serialized_large_blob_array: None, + force_pin_change: None, min_pin_length: 4, - #[cfg(feature = "with_ctap2_1")] firmware_version: None, + max_cred_blob_length: None, + max_rp_ids_for_set_min_pin_length: None, + certifications: None, + remaining_discoverable_credentials: None, }; let response_cbor: Option = ResponseData::AuthenticatorGetInfo(get_info_response).into(); - #[cfg(not(feature = "with_ctap2_1"))] - let expected_cbor = cbor_map_options! { - 0x01 => cbor_array_vec![versions], - 0x03 => vec![0x00; 16], - }; - #[cfg(feature = "with_ctap2_1")] let expected_cbor = cbor_map_options! { 0x01 => cbor_array_vec![versions], 0x03 => vec![0x00; 16], @@ -352,56 +458,71 @@ mod test { } #[test] - #[cfg(feature = "with_ctap2_1")] fn test_get_info_optionals_into_cbor() { - let mut options_map = BTreeMap::new(); - options_map.insert(String::from("rk"), true); let get_info_response = AuthenticatorGetInfoResponse { versions: vec!["FIDO_2_0".to_string()], extensions: Some(vec!["extension".to_string()]), aaguid: [0x00; 16], - options: Some(options_map), + options: Some(vec![(String::from("rk"), true)]), max_msg_size: Some(1024), pin_protocols: Some(vec![1]), max_credential_count_in_list: Some(20), max_credential_id_length: Some(256), transports: Some(vec![AuthenticatorTransport::Usb]), algorithms: Some(vec![ES256_CRED_PARAM]), - default_cred_protect: Some(CredentialProtectionPolicy::UserVerificationRequired), + max_serialized_large_blob_array: Some(1024), + force_pin_change: Some(false), min_pin_length: 4, firmware_version: Some(0), + max_cred_blob_length: Some(1024), + max_rp_ids_for_set_min_pin_length: Some(8), + certifications: Some(vec![(String::from("example-cert"), 1)]), + remaining_discoverable_credentials: Some(150), }; let response_cbor: Option = ResponseData::AuthenticatorGetInfo(get_info_response).into(); let expected_cbor = cbor_map_options! { - 0x01 => cbor_array_vec![vec!["FIDO_2_0"]], - 0x02 => cbor_array_vec![vec!["extension"]], + 0x01 => cbor_array!["FIDO_2_0"], + 0x02 => cbor_array!["extension"], 0x03 => vec![0x00; 16], 0x04 => cbor_map! {"rk" => true}, 0x05 => 1024, - 0x06 => cbor_array_vec![vec![1]], + 0x06 => cbor_array![1], 0x07 => 20, 0x08 => 256, - 0x09 => cbor_array_vec![vec!["usb"]], - 0x0A => cbor_array_vec![vec![ES256_CRED_PARAM]], - 0x0C => CredentialProtectionPolicy::UserVerificationRequired as u64, + 0x09 => cbor_array!["usb"], + 0x0A => cbor_array![ES256_CRED_PARAM], + 0x0B => 1024, + 0x0C => false, 0x0D => 4, 0x0E => 0, + 0x0F => 1024, + 0x10 => 8, + 0x13 => cbor_map! {"example-cert" => 1}, + 0x14 => 150, }; assert_eq!(response_cbor, Some(expected_cbor)); } #[test] fn test_used_client_pin_into_cbor() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let pk = sk.genpk(); + let cose_key = CoseKey::from(pk); let client_pin_response = AuthenticatorClientPinResponse { - key_agreement: None, - pin_token: Some(vec![70]), - retries: None, + key_agreement: Some(cose_key.clone()), + pin_uv_auth_token: Some(vec![70]), + retries: Some(8), + power_cycle_state: Some(false), }; let response_cbor: Option = ResponseData::AuthenticatorClientPin(Some(client_pin_response)).into(); let expected_cbor = cbor_map_options! { - 2 => vec![70], + 0x01 => cbor::Value::from(cose_key), + 0x02 => vec![70], + 0x03 => 8, + 0x04 => false, }; assert_eq!(response_cbor, Some(expected_cbor)); } @@ -418,17 +539,109 @@ mod test { assert_eq!(response_cbor, None); } - #[cfg(feature = "with_ctap2_1")] + #[test] + fn test_used_credential_management_into_cbor() { + let cred_management_response = AuthenticatorCredentialManagementResponse::default(); + let response_cbor: Option = + ResponseData::AuthenticatorCredentialManagement(Some(cred_management_response)).into(); + let expected_cbor = cbor_map_options! {}; + assert_eq!(response_cbor, Some(expected_cbor)); + } + + #[test] + fn test_used_credential_management_optionals_into_cbor() { + let mut env = TestEnv::new(); + let sk = ctap_crypto::ecdh::SecKey::gensk(env.rng()); + let rp = PublicKeyCredentialRpEntity { + rp_id: String::from("example.com"), + rp_name: None, + rp_icon: None, + }; + let user = PublicKeyCredentialUserEntity { + user_id: vec![0xFA, 0xB1, 0xA2], + user_name: None, + user_display_name: None, + user_icon: None, + }; + let cred_descriptor = PublicKeyCredentialDescriptor { + key_type: PublicKeyCredentialType::PublicKey, + key_id: vec![0x1D; 32], + transports: None, + }; + let pk = sk.genpk(); + let cose_key = CoseKey::from(pk); + + let cred_management_response = AuthenticatorCredentialManagementResponse { + existing_resident_credentials_count: Some(100), + max_possible_remaining_resident_credentials_count: Some(96), + rp: Some(rp.clone()), + rp_id_hash: Some(vec![0x1D; 32]), + total_rps: Some(3), + user: Some(user.clone()), + credential_id: Some(cred_descriptor.clone()), + public_key: Some(cose_key.clone()), + total_credentials: Some(2), + cred_protect: Some(CredentialProtectionPolicy::UserVerificationOptional), + large_blob_key: Some(vec![0xBB; 64]), + }; + let response_cbor: Option = + ResponseData::AuthenticatorCredentialManagement(Some(cred_management_response)).into(); + let expected_cbor = cbor_map_options! { + 0x01 => 100, + 0x02 => 96, + 0x03 => rp, + 0x04 => vec![0x1D; 32], + 0x05 => 3, + 0x06 => user, + 0x07 => cred_descriptor, + 0x08 => cbor::Value::from(cose_key), + 0x09 => 2, + 0x0A => 0x01, + 0x0B => vec![0xBB; 64], + }; + assert_eq!(response_cbor, Some(expected_cbor)); + } + + #[test] + fn test_empty_credential_management_into_cbor() { + let response_cbor: Option = + ResponseData::AuthenticatorCredentialManagement(None).into(); + assert_eq!(response_cbor, None); + } + #[test] fn test_selection_into_cbor() { let response_cbor: Option = ResponseData::AuthenticatorSelection.into(); assert_eq!(response_cbor, None); } + #[test] + fn test_large_blobs_into_cbor() { + let large_blobs_response = AuthenticatorLargeBlobsResponse { config: vec![0xC0] }; + let response_cbor: Option = + ResponseData::AuthenticatorLargeBlobs(Some(large_blobs_response)).into(); + let expected_cbor = cbor_map_options! { + 0x01 => vec![0xC0], + }; + assert_eq!(response_cbor, Some(expected_cbor)); + } + + #[test] + fn test_empty_large_blobs_into_cbor() { + let response_cbor: Option = ResponseData::AuthenticatorLargeBlobs(None).into(); + assert_eq!(response_cbor, None); + } + + #[test] + fn test_config_into_cbor() { + let response_cbor: Option = ResponseData::AuthenticatorConfig.into(); + assert_eq!(response_cbor, None); + } + #[test] fn test_vendor_response_into_cbor() { let response_cbor: Option = - ResponseData::AuthenticatorVendor(AuthenticatorVendorResponse { + ResponseData::AuthenticatorVendorConfigure(AuthenticatorVendorConfigureResponse { cert_programmed: true, pkey_programmed: false, }) @@ -436,12 +649,12 @@ mod test { assert_eq!( response_cbor, Some(cbor_map_options! { - 1 => true, - 2 => false, + 0x01 => true, + 0x02 => false, }) ); let response_cbor: Option = - ResponseData::AuthenticatorVendor(AuthenticatorVendorResponse { + ResponseData::AuthenticatorVendorConfigure(AuthenticatorVendorConfigureResponse { cert_programmed: false, pkey_programmed: true, }) @@ -449,9 +662,27 @@ mod test { assert_eq!( response_cbor, Some(cbor_map_options! { - 1 => false, - 2 => true, + 0x01 => false, + 0x02 => true, }) ); } + + #[test] + fn test_vendor_upgrade_into_cbor() { + let response_cbor: Option = ResponseData::AuthenticatorVendorUpgrade.into(); + assert_eq!(response_cbor, None); + } + + #[test] + fn test_vendor_upgrade_info_into_cbor() { + let vendor_upgrade_info_response = + AuthenticatorVendorUpgradeInfoResponse { info: 0x00060000 }; + let response_cbor: Option = + ResponseData::AuthenticatorVendorUpgradeInfo(vendor_upgrade_info_response).into(); + let expected_cbor = cbor_map! { + 0x01 => 0x00060000, + }; + assert_eq!(response_cbor, Some(expected_cbor)); + } } diff --git a/apps/vault/src/ctap/status_code.rs b/apps/vault/src/ctap/status_code.rs index 097d7ecc0..58d02a487 100644 --- a/apps/vault/src/ctap/status_code.rs +++ b/apps/vault/src/ctap/status_code.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,11 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::api::user_presence::UserPresenceError; +use crate::api::{attestation_store, key_store}; + // CTAP specification (version 20190130) section 6.3 // For now, only the CTAP2 codes are here, the CTAP1 are not included. #[allow(non_camel_case_types)] #[allow(dead_code)] -#[derive(Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] pub enum Ctap2StatusCode { CTAP2_OK = 0x00, CTAP1_ERR_INVALID_COMMAND = 0x01, @@ -31,11 +34,8 @@ pub enum Ctap2StatusCode { CTAP2_ERR_INVALID_CBOR = 0x12, CTAP2_ERR_MISSING_PARAMETER = 0x14, CTAP2_ERR_LIMIT_EXCEEDED = 0x15, - CTAP2_ERR_UNSUPPORTED_EXTENSION = 0x16, - #[cfg(feature = "with_ctap2_1")] CTAP2_ERR_FP_DATABASE_FULL = 0x17, - #[cfg(feature = "with_ctap2_1")] - CTAP2_ERR_PC_STORAGE_FULL = 0x18, + CTAP2_ERR_LARGE_BLOB_STORAGE_FULL = 0x18, CTAP2_ERR_CREDENTIAL_EXCLUDED = 0x19, CTAP2_ERR_PROCESSING = 0x21, CTAP2_ERR_INVALID_CREDENTIAL = 0x22, @@ -57,25 +57,22 @@ pub enum Ctap2StatusCode { CTAP2_ERR_PIN_AUTH_INVALID = 0x33, CTAP2_ERR_PIN_AUTH_BLOCKED = 0x34, CTAP2_ERR_PIN_NOT_SET = 0x35, - CTAP2_ERR_PIN_REQUIRED = 0x36, + CTAP2_ERR_PUAT_REQUIRED = 0x36, CTAP2_ERR_PIN_POLICY_VIOLATION = 0x37, CTAP2_ERR_PIN_TOKEN_EXPIRED = 0x38, CTAP2_ERR_REQUEST_TOO_LARGE = 0x39, CTAP2_ERR_ACTION_TIMEOUT = 0x3A, CTAP2_ERR_UP_REQUIRED = 0x3B, CTAP2_ERR_UV_BLOCKED = 0x3C, - #[cfg(feature = "with_ctap2_1")] CTAP2_ERR_INTEGRITY_FAILURE = 0x3D, - #[cfg(feature = "with_ctap2_1")] CTAP2_ERR_INVALID_SUBCOMMAND = 0x3E, + CTAP2_ERR_UV_INVALID = 0x3F, + CTAP2_ERR_UNAUTHORIZED_PERMISSION = 0x40, CTAP1_ERR_OTHER = 0x7F, - CTAP2_ERR_SPEC_LAST = 0xDF, - CTAP2_ERR_EXTENSION_FIRST = 0xE0, - CTAP2_ERR_EXTENSION_LAST = 0xEF, - // CTAP2_ERR_VENDOR_FIRST = 0xF0, - CTAP2_ERR_VENDOR_RESPONSE_TOO_LONG = 0xF0, - CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR = 0xF1, - + _CTAP2_ERR_SPEC_LAST = 0xDF, + _CTAP2_ERR_EXTENSION_FIRST = 0xE0, + _CTAP2_ERR_EXTENSION_LAST = 0xEF, + _CTAP2_ERR_VENDOR_FIRST = 0xF0, /// An internal invariant is broken. /// /// This type of error is unexpected and the current state is undefined. @@ -85,6 +82,32 @@ pub enum Ctap2StatusCode { /// /// It may be possible that some of those errors are actually internal errors. CTAP2_ERR_VENDOR_HARDWARE_FAILURE = 0xF3, - - CTAP2_ERR_VENDOR_LAST = 0xFF, + _CTAP2_ERR_VENDOR_LAST = 0xFF, +} + +impl From for Ctap2StatusCode { + fn from(user_presence_error: UserPresenceError) -> Self { + match user_presence_error { + UserPresenceError::Timeout => Self::CTAP2_ERR_USER_ACTION_TIMEOUT, + UserPresenceError::Declined => Self::CTAP2_ERR_OPERATION_DENIED, + UserPresenceError::Canceled => Self::CTAP2_ERR_KEEPALIVE_CANCEL, + } + } +} + +impl From for Ctap2StatusCode { + fn from(_: key_store::Error) -> Self { + Self::CTAP2_ERR_VENDOR_INTERNAL_ERROR + } +} + +impl From for Ctap2StatusCode { + fn from(error: attestation_store::Error) -> Self { + use attestation_store::Error; + match error { + Error::Storage => Self::CTAP2_ERR_VENDOR_HARDWARE_FAILURE, + Error::Internal => Self::CTAP2_ERR_VENDOR_INTERNAL_ERROR, + Error::NoSupport => Self::CTAP2_ERR_VENDOR_INTERNAL_ERROR, + } + } } diff --git a/apps/vault/src/ctap/storage.rs b/apps/vault/src/ctap/storage.rs index 1cb1514fd..64e44bc14 100644 --- a/apps/vault/src/ctap/storage.rs +++ b/apps/vault/src/ctap/storage.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,1021 +14,619 @@ mod key; -#[cfg(feature = "with_ctap2_1")] -use crate::ctap::data_formats::{extract_array, extract_text_string}; -use crate::ctap::data_formats::{CredentialProtectionPolicy, PublicKeyCredentialSource}; -use crate::ctap::key_material; -use crate::ctap::pin_protocol_v1::PIN_AUTH_LENGTH; +use crate::api::attestation_store::{self, AttestationStore}; +use crate::api::customization::Customization; +use crate::api::key_store::KeyStore; +use crate::ctap::client_pin::PIN_AUTH_LENGTH; +use crate::ctap::data_formats::{ + extract_array, extract_text_string, PublicKeyCredentialSource, PublicKeyCredentialUserEntity, +}; use crate::ctap::status_code::Ctap2StatusCode; -use crate::ctap::INITIAL_SIGNATURE_COUNTER; -use std::io::{Write, Read, SeekFrom, Seek}; -#[cfg(feature = "with_ctap2_1")] +use crate::ctap::{key_material, INITIAL_SIGNATURE_COUNTER}; +use crate::env::Env; use std::string::String; use std::vec; use std::vec::Vec; use arrayref::array_ref; -#[cfg(feature = "with_ctap2_1")] -use cbor::cbor_array_vec; +use core::cmp; use core::convert::TryInto; +use persistent_store::{fragment, StoreUpdate}; use ctap_crypto::rng256::Rng256; -use pddb::Pddb; -use rand_core::{OsRng, RngCore}; -use std::cell::RefCell; +use cbor::cbor_array_vec; -use self::key::PIN_RETRIES; +/// Wrapper for PIN properties. +struct PinProperties { + /// 16 byte prefix of SHA256 of the currently set PIN. + hash: [u8; PIN_AUTH_LENGTH], -use super::CREDENTIAL_ID_SIZE; -/// Size hint for storing a credential record. It can grow larger than this -/// because things like icons and free-form strings are supported for storing -/// credentials, but what happens in this case is just a re-allocation in the PDDB. -/// Whereas if this number is "too big" you end up with wasted space. I think a -/// typical record will be around 300-400 bytes, so, this is a good compromise. -pub(crate) const CRED_INITAL_SIZE: usize = 512; - -// Those constants may be modified before compilation to tune the behavior of the key. -// -// The number of pages should be at least 3 and at most what the flash can hold. There should be no -// reason to put a small number here, except that the latency of flash operations is linear in the -// number of pages. This may improve in the future. Currently, using 20 pages gives between 20ms and -// 240ms per operation. The rule of thumb is between 1ms and 12ms per additional page. -// -// Limiting the number of residential keys permits to ensure a minimum number of counter increments. -// Let: -// - P the number of pages (NUM_PAGES) -// - K the maximum number of residential keys (MAX_SUPPORTED_RESIDENTIAL_KEYS) -// - S the maximum size of a residential key (about 500) -// - C the number of erase cycles (10000) -// - I the minimum number of counter increments -// -// We have: I = (P * 4084 - 5107 - K * S) / 8 * C -// -// With P=20 and K=150, we have I=2M which is enough for 500 increments per day for 10 years. -#[allow(dead_code)] // openSK legacy -const NUM_PAGES: usize = 20; - -/// The limit is based on the performance of the PDDB to get a query response within -/// the compliance time-out limit. Note that most keys support about 25 credentials. -const MAX_SUPPORTED_RESIDENTIAL_KEYS: usize = 150; - -const MAX_PIN_RETRIES: u8 = 8; -#[cfg(feature = "with_ctap2_1")] -const DEFAULT_MIN_PIN_LENGTH: u8 = 4; -// TODO(kaczmarczyck) use this for the minPinLength extension -// https://github.com/google/OpenSK/issues/129 -#[cfg(feature = "with_ctap2_1")] -const _DEFAULT_MIN_PIN_LENGTH_RP_IDS: Vec = Vec::new(); -// TODO(kaczmarczyck) Check whether this constant is necessary, or replace it accordingly. -#[cfg(feature = "with_ctap2_1")] -const _MAX_RP_IDS_LENGTH: usize = 8; - -const FIDO_DICT: &'static str = "fido.cfg"; -use crate::ctap::FIDO_CRED_DICT; -const FIDO_PERSISTENT_DICT: &'static str = "fido.persistent"; - -/// Wrapper for master keys. -pub struct MasterKeys { - /// Master encryption key. - pub encryption: [u8; 32], - - /// Master hmac key. - pub hmac: [u8; 32], + /// Length of the current PIN in code points. + code_point_length: u8, } -/// CTAP persistent storage. -pub struct PersistentStore { - pddb: RefCell::, +/// Initializes the store by creating missing objects. +pub fn init(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + // Generate and store the CredRandom secrets if they are missing. + if env.store().find_handle(key::CRED_RANDOM_SECRET)?.is_none() { + let cred_random_with_uv = env.rng().gen_uniform_u8x32(); + let cred_random_without_uv = env.rng().gen_uniform_u8x32(); + let mut cred_random = Vec::with_capacity(64); + cred_random.extend_from_slice(&cred_random_without_uv); + cred_random.extend_from_slice(&cred_random_with_uv); + env.store().insert(key::CRED_RANDOM_SECRET, &cred_random)?; + } + + if env.store().find_handle(key::AAGUID)?.is_none() { + set_aaguid(env, key_material::AAGUID)?; + } + Ok(()) } -use std::num::ParseIntError; - -pub fn decode_hex(s: &str) -> Result, ParseIntError> { - (0..s.len()) - .step_by(2) - .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) - .collect() +/// Returns the credential at the given key. +/// +/// # Errors +/// +/// Returns `CTAP2_ERR_VENDOR_INTERNAL_ERROR` if the key does not hold a valid credential. +pub fn get_credential( + env: &mut impl Env, + key: usize, +) -> Result { + let min_key = key::CREDENTIALS.start; + if key < min_key || key >= min_key + env.customization().max_supported_resident_keys() { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + let credential_entry = env + .store() + .find(key)? + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; + deserialize_credential(&credential_entry) + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) } -impl PersistentStore { - /// Gives access to the persistent store. +/// Finds the key and value for a given credential ID. +/// +/// # Errors +/// +/// Returns `CTAP2_ERR_NO_CREDENTIALS` if the credential is not found. +pub fn find_credential_item( + env: &mut impl Env, + credential_id: &[u8], +) -> Result<(usize, PublicKeyCredentialSource), Ctap2StatusCode> { + let mut iter_result = Ok(()); + let iter = iter_credentials(env, &mut iter_result)?; + let mut credentials: Vec<(usize, PublicKeyCredentialSource)> = iter + .filter(|(_, credential)| credential.credential_id == credential_id) + .collect(); + iter_result?; + if credentials.len() > 1 { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + credentials + .pop() + .ok_or(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS) +} + +/// Returns the first matching credential. +/// +/// Returns `None` if no credentials are matched or if `check_cred_protect` is set and the first +/// matched credential requires user verification. +pub fn find_credential( + env: &mut impl Env, + rp_id: &str, + credential_id: &[u8], +) -> Result, Ctap2StatusCode> { + let credential = match find_credential_item(env, credential_id) { + Err(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS) => return Ok(None), + Err(e) => return Err(e), + Ok((_key, credential)) => credential, + }; + if credential.rp_id != rp_id { + return Ok(None); + } + Ok(Some(credential)) +} + +/// Stores or updates a credential. +/// +/// If a credential with the same RP id and user handle already exists, it is replaced. +pub fn store_credential( + env: &mut impl Env, + new_credential: PublicKeyCredentialSource, +) -> Result<(), Ctap2StatusCode> { + let max_supported_resident_keys = env.customization().max_supported_resident_keys(); + // Holds the key of the existing credential if this is an update. + let mut old_key = None; + let min_key = key::CREDENTIALS.start; + // Holds whether a key is used (indices are shifted by min_key). + let mut keys = vec![false; max_supported_resident_keys]; + let mut iter_result = Ok(()); + let iter = iter_credentials(env, &mut iter_result)?; + for (key, credential) in iter { + if key < min_key || key - min_key >= max_supported_resident_keys || keys[key - min_key] { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + keys[key - min_key] = true; + if credential.rp_id == new_credential.rp_id + && credential.user_handle == new_credential.user_handle + { + if old_key.is_some() { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + old_key = Some(key); + } + } + iter_result?; + if old_key.is_none() && keys.iter().filter(|&&x| x).count() >= max_supported_resident_keys { + return Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL); + } + let key = match old_key { + // This is a new credential being added, we need to allocate a free key. We choose the + // first available key. + None => key::CREDENTIALS + .take(max_supported_resident_keys) + .find(|key| !keys[key - min_key]) + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?, + // This is an existing credential being updated, we reuse its key. + Some(x) => x, + }; + let value = serialize_credential(new_credential)?; + env.store().insert(key, &value)?; + Ok(()) +} + +/// Deletes a credential. +/// +/// # Errors +/// +/// Returns `CTAP2_ERR_NO_CREDENTIALS` if the credential is not found. +pub fn delete_credential(env: &mut impl Env, credential_id: &[u8]) -> Result<(), Ctap2StatusCode> { + let (key, _) = find_credential_item(env, credential_id)?; + Ok(env.store().remove(key)?) +} + +/// Updates a credential's user information. +/// +/// # Errors +/// +/// Returns `CTAP2_ERR_NO_CREDENTIALS` if the credential is not found. +pub fn update_credential( + env: &mut impl Env, + credential_id: &[u8], + user: PublicKeyCredentialUserEntity, +) -> Result<(), Ctap2StatusCode> { + let (key, mut credential) = find_credential_item(env, credential_id)?; + credential.user_name = user.user_name; + credential.user_display_name = user.user_display_name; + credential.user_icon = user.user_icon; + let value = serialize_credential(credential)?; + Ok(env.store().insert(key, &value)?) +} + +/// Returns the number of credentials. +pub fn count_credentials(env: &mut impl Env) -> Result { + let mut count = 0; + for handle in env.store().iter()? { + count += key::CREDENTIALS.contains(&handle?.get_key()) as usize; + } + Ok(count) +} + +/// Returns the estimated number of credentials that can still be stored. +pub fn remaining_credentials(env: &mut impl Env) -> Result { + env.customization() + .max_supported_resident_keys() + .checked_sub(count_credentials(env)?) + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) +} + +/// Iterates through the credentials. +/// +/// If an error is encountered during iteration, it is written to `result`. +pub fn iter_credentials<'a, E: Env>( + env: &'a mut E, + result: &'a mut Result<(), Ctap2StatusCode>, +) -> Result, Ctap2StatusCode> { + IterCredentials::new(env.store(), result) +} + +/// Returns the next creation order. +pub fn new_creation_order(env: &mut impl Env) -> Result { + let mut iter_result = Ok(()); + let iter = iter_credentials(env, &mut iter_result)?; + let max = iter.map(|(_, credential)| credential.creation_order).max(); + iter_result?; + Ok(max.unwrap_or(0).wrapping_add(1)) +} + +/// Returns the global signature counter. +pub fn global_signature_counter(env: &mut impl Env) -> Result { + match env.store().find(key::GLOBAL_SIGNATURE_COUNTER)? { + None => Ok(INITIAL_SIGNATURE_COUNTER), + Some(value) if value.len() == 4 => Ok(u32::from_ne_bytes(*array_ref!(&value, 0, 4))), + Some(_) => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Increments the global signature counter. +pub fn incr_global_signature_counter( + env: &mut impl Env, + increment: u32, +) -> Result<(), Ctap2StatusCode> { + let old_value = global_signature_counter(env)?; + // In hopes that servers handle the wrapping gracefully. + let new_value = old_value.wrapping_add(increment); + env.store() + .insert(key::GLOBAL_SIGNATURE_COUNTER, &new_value.to_ne_bytes())?; + Ok(()) +} + +/// Returns the CredRandom secret. +pub fn cred_random_secret(env: &mut impl Env, has_uv: bool) -> Result<[u8; 32], Ctap2StatusCode> { + let cred_random_secret = env + .store() + .find(key::CRED_RANDOM_SECRET)? + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; + if cred_random_secret.len() != 64 { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + let offset = if has_uv { 32 } else { 0 }; + Ok(*array_ref![cred_random_secret, offset, 32]) +} + +/// Reads the PIN properties and wraps them into PinProperties. +fn pin_properties(env: &mut impl Env) -> Result, Ctap2StatusCode> { + let pin_properties = match env.store().find(key::PIN_PROPERTIES)? { + None => return Ok(None), + Some(pin_properties) => pin_properties, + }; + const PROPERTIES_LENGTH: usize = PIN_AUTH_LENGTH + 1; + match pin_properties.len() { + PROPERTIES_LENGTH => Ok(Some(PinProperties { + hash: *array_ref![pin_properties, 1, PIN_AUTH_LENGTH], + code_point_length: pin_properties[0], + })), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Returns the PIN hash if defined. +pub fn pin_hash(env: &mut impl Env) -> Result, Ctap2StatusCode> { + Ok(pin_properties(env)?.map(|p| p.hash)) +} + +/// Returns the length of the currently set PIN if defined. +pub fn pin_code_point_length(env: &mut impl Env) -> Result, Ctap2StatusCode> { + Ok(pin_properties(env)?.map(|p| p.code_point_length)) +} + +/// Sets the PIN hash and length. +/// +/// If it was already defined, it is updated. +pub fn set_pin( + env: &mut impl Env, + pin_hash: &[u8; PIN_AUTH_LENGTH], + pin_code_point_length: u8, +) -> Result<(), Ctap2StatusCode> { + let mut pin_properties = [0; 1 + PIN_AUTH_LENGTH]; + pin_properties[0] = pin_code_point_length; + pin_properties[1..].clone_from_slice(pin_hash); + Ok(env.store().transaction(&[ + StoreUpdate::Insert { + key: key::PIN_PROPERTIES, + value: &pin_properties[..], + }, + StoreUpdate::Remove { + key: key::FORCE_PIN_CHANGE, + }, + ])?) +} + +/// Returns the number of remaining PIN retries. +pub fn pin_retries(env: &mut impl Env) -> Result { + match env.store().find(key::PIN_RETRIES)? { + None => Ok(env.customization().max_pin_retries()), + Some(value) if value.len() == 1 => Ok(value[0]), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Decrements the number of remaining PIN retries. +pub fn decr_pin_retries(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + let old_value = pin_retries(env)?; + let new_value = old_value.saturating_sub(1); + if new_value != old_value { + env.store().insert(key::PIN_RETRIES, &[new_value])?; + } + Ok(()) +} + +/// Resets the number of remaining PIN retries. +pub fn reset_pin_retries(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + Ok(env.store().remove(key::PIN_RETRIES)?) +} + +/// Returns the minimum PIN length. +pub fn min_pin_length(env: &mut impl Env) -> Result { + match env.store().find(key::MIN_PIN_LENGTH)? { + None => Ok(env.customization().default_min_pin_length()), + Some(value) if value.len() == 1 => Ok(value[0]), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Sets the minimum PIN length. +pub fn set_min_pin_length(env: &mut impl Env, min_pin_length: u8) -> Result<(), Ctap2StatusCode> { + Ok(env.store().insert(key::MIN_PIN_LENGTH, &[min_pin_length])?) +} + +/// Returns the list of RP IDs that are used to check if reading the minimum PIN length is +/// allowed. +pub fn min_pin_length_rp_ids(env: &mut impl Env) -> Result, Ctap2StatusCode> { + let rp_ids = env.store().find(key::MIN_PIN_LENGTH_RP_IDS)?.map_or_else( + || Some(env.customization().default_min_pin_length_rp_ids()), + |value| deserialize_min_pin_length_rp_ids(&value), + ); + debug_assert!(rp_ids.is_some()); + Ok(rp_ids.unwrap_or_default()) +} + +/// Sets the list of RP IDs that are used to check if reading the minimum PIN length is allowed. +pub fn set_min_pin_length_rp_ids( + env: &mut impl Env, + min_pin_length_rp_ids: Vec, +) -> Result<(), Ctap2StatusCode> { + let mut min_pin_length_rp_ids = min_pin_length_rp_ids; + for rp_id in env.customization().default_min_pin_length_rp_ids() { + if !min_pin_length_rp_ids.contains(&rp_id) { + min_pin_length_rp_ids.push(rp_id); + } + } + if min_pin_length_rp_ids.len() > env.customization().max_rp_ids_length() { + return Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL); + } + Ok(env.store().insert( + key::MIN_PIN_LENGTH_RP_IDS, + &serialize_min_pin_length_rp_ids(min_pin_length_rp_ids)?, + )?) +} + +/// Reads the byte vector stored as the serialized large blobs array. +/// +/// If too few bytes exist at that offset, return the maximum number +/// available. This includes cases of offset being beyond the stored array. +/// +/// If no large blob is committed to the store, get responds as if an empty +/// CBOR array (0x80) was written, together with the 16 byte prefix of its +/// SHA256, to a total length of 17 byte (which is the shortest legitimate +/// large blob entry possible). +pub fn get_large_blob_array( + env: &mut impl Env, + offset: usize, + byte_count: usize, +) -> Result, Ctap2StatusCode> { + let byte_range = offset..offset + byte_count; + let output = fragment::read_range(env.store(), &key::LARGE_BLOB_SHARDS, byte_range)?; + Ok(output.unwrap_or_else(|| { + const EMPTY_LARGE_BLOB: [u8; 17] = [ + 0x80, 0x76, 0xBE, 0x8B, 0x52, 0x8D, 0x00, 0x75, 0xF7, 0xAA, 0xE9, 0x8D, 0x6F, 0xA5, + 0x7A, 0x6D, 0x3C, + ]; + let last_index = cmp::min(EMPTY_LARGE_BLOB.len(), offset + byte_count); + EMPTY_LARGE_BLOB + .get(offset..last_index) + .unwrap_or_default() + .to_vec() + })) +} + +/// Sets a byte vector as the serialized large blobs array. +pub fn commit_large_blob_array( + env: &mut impl Env, + large_blob_array: &[u8], +) -> Result<(), Ctap2StatusCode> { + // This input should have been caught at caller level. + if large_blob_array.len() > env.customization().max_large_blob_array_size() { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + Ok(fragment::write( + env.store(), + &key::LARGE_BLOB_SHARDS, + large_blob_array, + )?) +} + +/// Returns the AAGUID. +pub fn aaguid(env: &mut impl Env) -> Result<[u8; key_material::AAGUID_LENGTH], Ctap2StatusCode> { + let aaguid = env + .store() + .find(key::AAGUID)? + .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; + if aaguid.len() != key_material::AAGUID_LENGTH { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + Ok(*array_ref![aaguid, 0, key_material::AAGUID_LENGTH]) +} + +/// Sets the AAGUID. +/// +/// If it is already defined, it is overwritten. +pub fn set_aaguid( + env: &mut impl Env, + aaguid: &[u8; key_material::AAGUID_LENGTH], +) -> Result<(), Ctap2StatusCode> { + Ok(env.store().insert(key::AAGUID, aaguid)?) +} + +/// Resets the store as for a CTAP reset. +/// +/// In particular persistent entries are not reset. +pub fn reset(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + env.store().clear(key::NUM_PERSISTENT_KEYS)?; + env.key_store().reset()?; + init(env)?; + Ok(()) +} + +/// Returns whether the PIN needs to be changed before its next usage. +pub fn has_force_pin_change(env: &mut impl Env) -> Result { + match env.store().find(key::FORCE_PIN_CHANGE)? { + None => Ok(false), + Some(value) if value.is_empty() => Ok(true), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Marks the PIN as outdated with respect to the new PIN policy. +pub fn force_pin_change(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + Ok(env.store().insert(key::FORCE_PIN_CHANGE, &[])?) +} + +/// Returns whether enterprise attestation is enabled. +pub fn enterprise_attestation(env: &mut impl Env) -> Result { + match env.store().find(key::ENTERPRISE_ATTESTATION)? { + None => Ok(false), + Some(value) if value.is_empty() => Ok(true), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Marks enterprise attestation as enabled. +pub fn enable_enterprise_attestation(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + if env + .attestation_store() + .get(&attestation_store::Id::Enterprise)? + .is_none() + { + return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + if !enterprise_attestation(env)? { + env.store().insert(key::ENTERPRISE_ATTESTATION, &[])?; + } + Ok(()) +} + +/// Returns whether alwaysUv is enabled. +pub fn has_always_uv(env: &mut impl Env) -> Result { + if env.customization().enforce_always_uv() { + return Ok(true); + } + match env.store().find(key::ALWAYS_UV)? { + None => Ok(false), + Some(value) if value.is_empty() => Ok(true), + _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + } +} + +/// Enables alwaysUv, when disabled, and vice versa. +pub fn toggle_always_uv(env: &mut impl Env) -> Result<(), Ctap2StatusCode> { + if env.customization().enforce_always_uv() { + return Err(Ctap2StatusCode::CTAP2_ERR_OPERATION_DENIED); + } + if has_always_uv(env)? { + Ok(env.store().remove(key::ALWAYS_UV)?) + } else { + Ok(env.store().insert(key::ALWAYS_UV, &[])?) + } +} + +impl From for Ctap2StatusCode { + fn from(error: persistent_store::StoreError) -> Ctap2StatusCode { + use persistent_store::StoreError; + match error { + // This error is expected. The store is full. + StoreError::NoCapacity => Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL, + // This error is expected. The flash is out of life. + StoreError::NoLifetime => Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL, + // This error is expected if we don't satisfy the store preconditions. For example we + // try to store a credential which is too long. + StoreError::InvalidArgument => Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR, + // This error is not expected. The storage has been tempered with. We could erase the + // storage. + StoreError::InvalidStorage => Ctap2StatusCode::CTAP2_ERR_VENDOR_HARDWARE_FAILURE, + // This error is not expected. The kernel is failing our syscalls. + StoreError::StorageError => Ctap2StatusCode::CTAP1_ERR_OTHER, + } + } +} + +/// Iterator for credentials. +pub struct IterCredentials<'a, E: Env> { + /// The store being iterated. + store: &'a persistent_store::Store, + + /// The store iterator. + iter: persistent_store::StoreIter<'a>, + + /// The iteration result. /// - /// # Safety + /// It starts as success and gets written at most once with an error if something fails. The + /// iteration stops as soon as an error is encountered. + result: &'a mut Result<(), Ctap2StatusCode>, +} + +impl<'a, E: Env> IterCredentials<'a, E> { + /// Creates a credential iterator. + fn new( + store: &'a persistent_store::Store, + result: &'a mut Result<(), Ctap2StatusCode>, + ) -> Result { + let iter = store.iter()?; + Ok(IterCredentials { + store, + iter, + result, + }) + } + + /// Marks the iteration as failed if the content is absent. /// - /// This should be at most one instance of persistent store per program lifetime. - pub fn new(_rng: &mut impl Rng256) -> PersistentStore { - let mut store = PersistentStore { - pddb: RefCell::new(Pddb::new()), - }; - // block until the PDDB is mounted - store.pddb.borrow().is_mounted_blocking(); - store.init().unwrap(); - store + /// For convenience, the function takes and returns ownership instead of taking a shared + /// reference and returning nothing. This permits to use it in both expressions and statements + /// instead of statements only. + fn unwrap(&mut self, x: Option) -> Option { + if x.is_none() { + *self.result = Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); + } + x } +} - /// Initializes the store by creating missing objects. - fn init(&mut self) -> Result<(), Ctap2StatusCode> { +impl<'a, E: Env> Iterator for IterCredentials<'a, E> { + type Item = (usize, PublicKeyCredentialSource); - // Generate and store the master keys if they are missing. - match self.pddb.borrow().get( - FIDO_DICT, - key::MASTER_KEYS, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(64), Some(crate::basis_change) - ) { - Ok(mut master_keys) => { - let mk_attr = master_keys.attributes().unwrap(); // attribute fetches should not fail, so we don't kick it up. We want to see the panic at this line if it does fail. - if mk_attr.len != 64 { - if mk_attr.len != 0 { - log::error!("Master key has an illegal length. Suspect PDDB corruption?"); - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CREDENTIAL); - } - let mut keys = [0u8; 64]; - OsRng.fill_bytes(&mut keys); - master_keys.write(&keys) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } - } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + fn next(&mut self) -> Option<(usize, PublicKeyCredentialSource)> { + if self.result.is_err() { + return None; } - - // Generate and store the CredRandom secrets if they are missing. - match self.pddb.borrow().get( - FIDO_DICT, - key::CRED_RANDOM_SECRET, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(64), Some(crate::basis_change) - ) { - Ok(mut cred_random) => { - let cred_attr = cred_random.attributes().unwrap(); - if cred_attr.len != 64 { - if cred_attr.len != 0 { - log::error!("Random credentials has an illegal length. Suspect PDDB corruption?"); - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CREDENTIAL); - } - let mut creds = [0u8; 64]; - OsRng.fill_bytes(&mut creds); - cred_random.write(&creds) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } + while let Some(next) = self.iter.next() { + let handle = self.unwrap(next.ok())?; + let key = handle.get_key(); + if !key::CREDENTIALS.contains(&key) { + continue; } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), + let value = self.unwrap(handle.get_value(self.store).ok())?; + let credential = self.unwrap(deserialize_credential(&value))?; + return Some((key, credential)); } - - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::AAGUID, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(16), Some(crate::basis_change) - ) { - Ok(mut aaguid) => { - let aaguid_attr = aaguid.attributes().unwrap(); - if aaguid_attr.len != 16 { - if aaguid_attr.len != 0 { - log::error!("AAGUID has an illegal length. Suspect PDDB corruption?"); - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CREDENTIAL); - } - aaguid.write(key_material::AAGUID) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } - } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - - /* - The point of the attestation key is to delegate the question of the authenticity of your U2F - key effectively to a transaction that happens between the factory and the server. If the - attestation private key is an impenetrable secret and you can trust the factory, then the - attestation key effectively proves the U2F token was made by the factory to its specifications. - - In the case of Precursor's threat model, you don't trust the factory, and you assume there is - basically no such thing as an impenetrable secret. So an attestation key is equivalent to any - private key created entirely within your own device; its use proves that you're you, or at - least, someone has possession of your device and your unlock password. - - Thus, for Precursor, there is no delegation of the question of authenticity -- you - choose to verify the device to the level you're satisfied with. If you're happy to - just take it out of the box and use it without checking anything, that's your choice, - but you can also check everything as much as you like. - - As a result, the "attestation key" serves no role for Precursor users, other than to - identify them as Precursor users. A unique per-device key would furthermore allow - a server to uniquely identify each Precursor user. Therefore, we commit the private key - into this public repository so any and all Precursor users may share it. The more - widely it is used, the harder it becomes to de-anonymize a Precursor users. - - This key was generated using the script in `tools/gen_key_materials.sh`, and then printed - out by calling - `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem` - */ - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_PRIVATE_KEY, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(32), Some(crate::basis_change) - ) { - Ok(mut aapriv) => { - let aapriv_attr = aapriv.attributes().unwrap(); - if aapriv_attr.len != 32 { - if aapriv_attr.len != 0 { - log::error!("AAPRIV has an illegal length. Suspect PDDB corruption?"); - return Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CREDENTIAL); - } - let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap(); - log::info!("writing PK of length {}", pk.len()); - log::debug!("pk: {:x?}", pk); - assert!(pk.len() == 32, "PK len is wrong"); - aapriv.write(&pk) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } - } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_CERTIFICATE, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(512), Some(crate::basis_change) - ) { - Ok(mut cert) => { - let cert_attr = cert.attributes().unwrap(); - if cert_attr.len == 0 { - // DER-encoded public key - let der = decode_hex( - "308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\ - 082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\ - 55324620444959204341301e170d3232303630353138313233395a170d333230\ - 3630343138313233395a30293127302506035504030c1e507265637572736f72\ - 2053656c662d5665726966696564204465766963653059301306072a8648ce3d\ - 020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\ - 1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\ - 55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\ - 45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\ - e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\ - 1f0bdf82d1f4").unwrap(); - log::info!("writing cert of length {}", der.len()); - log::debug!("der: {:x?}", der); - cert.write(&der) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } - } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - - Ok(()) - } - /* Notes in case we want to muck with the above certificates more later on: -private key: ------BEGIN EC PARAMETERS----- -BggqhkjOPQMBBw== ------END EC PARAMETERS----- ------BEGIN EC PRIVATE KEY----- -MHcCAQEEILjDq9Bcvhey+vh2Wcb3PoRngyESoOYJgHy2iZbJoMaooAoGCCqGSM49 -AwEHoUQDQgAELY+tdsJYUe5wrmUasjYb7htdk3aa/5nZWtEShxBg8vNC+3lWb+6e -G63iUKKc1lASVecxUxdVKEv8voWtofOfRA== ------END EC PRIVATE KEY----- - -certificate: ------BEGIN CERTIFICATE----- -MIIBQjCB6QIUSaPipOEHjq4vGhhWfwpzSwnbJHgwCgYIKoZIzj0EAwIwHzEdMBsG -A1UEAwwUUHJlY3Vyc29yIFUyRiBESVkgQ0EwHhcNMjIwNjA1MTgxMjM5WhcNMzIw -NjA0MTgxMjM5WjApMScwJQYDVQQDDB5QcmVjdXJzb3IgU2VsZi1WZXJpZmllZCBE -ZXZpY2UwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQtj612wlhR7nCuZRqyNhvu -G12Tdpr/mdla0RKHEGDy80L7eVZv7p4breJQopzWUBJV5zFTF1UoS/y+ha2h859E -MAoGCCqGSM49BAMCA0gAMEUCIQDSfjkYfaXvqjdiVDKUmbtwX3GI3Yx44HJcftKN -5dIY4QIgcIU+GkNwcpjgfr6bnrfNWDm3lNtMPSIglVQfC9+C0fQ= ------END CERTIFICATE----- - -analysis: -openssl x509 -in opensk_cert.pem -inform pem -noout -text -Certificate: - Data: - Version: 1 (0x0) - Serial Number: - 49:a3:e2:a4:e1:07:8e:ae:2f:1a:18:56:7f:0a:73:4b:09:db:24:78 - Signature Algorithm: ecdsa-with-SHA256 - Issuer: CN = Precursor U2F DIY CA - Validity - Not Before: Jun 5 18:12:39 2022 GMT - Not After : Jun 4 18:12:39 2032 GMT - Subject: CN = Precursor Self-Verified Device - Subject Public Key Info: - Public Key Algorithm: id-ecPublicKey - Public-Key: (256 bit) - pub: - 04:2d:8f:ad:76:c2:58:51:ee:70:ae:65:1a:b2:36: - 1b:ee:1b:5d:93:76:9a:ff:99:d9:5a:d1:12:87:10: - 60:f2:f3:42:fb:79:56:6f:ee:9e:1b:ad:e2:50:a2: - 9c:d6:50:12:55:e7:31:53:17:55:28:4b:fc:be:85: - ad:a1:f3:9f:44 - ASN1 OID: prime256v1 - NIST CURVE: P-256 - Signature Algorithm: ecdsa-with-SHA256 - 30:45:02:21:00:d2:7e:39:18:7d:a5:ef:aa:37:62:54:32:94: - 99:bb:70:5f:71:88:dd:8c:78:e0:72:5c:7e:d2:8d:e5:d2:18: - e1:02:20:70:85:3e:1a:43:70:72:98:e0:7e:be:9b:9e:b7:cd: - 58:39:b7:94:db:4c:3d:22:20:95:54:1f:0b:df:82:d1:f4 - -openssl asn1parse -in opensk_cert.pem -inform pem - 0:d=0 hl=4 l= 322 cons: SEQUENCE - 4:d=1 hl=3 l= 233 cons: SEQUENCE - 7:d=2 hl=2 l= 20 prim: INTEGER :49A3E2A4E1078EAE2F1A18567F0A734B09DB2478 - 29:d=2 hl=2 l= 10 cons: SEQUENCE - 31:d=3 hl=2 l= 8 prim: OBJECT :ecdsa-with-SHA256 - 41:d=2 hl=2 l= 31 cons: SEQUENCE - 43:d=3 hl=2 l= 29 cons: SET - 45:d=4 hl=2 l= 27 cons: SEQUENCE - 47:d=5 hl=2 l= 3 prim: OBJECT :commonName - 52:d=5 hl=2 l= 20 prim: UTF8STRING :Precursor U2F DIY CA - 74:d=2 hl=2 l= 30 cons: SEQUENCE - 76:d=3 hl=2 l= 13 prim: UTCTIME :220605181239Z - 91:d=3 hl=2 l= 13 prim: UTCTIME :320604181239Z - 106:d=2 hl=2 l= 41 cons: SEQUENCE - 108:d=3 hl=2 l= 39 cons: SET - 110:d=4 hl=2 l= 37 cons: SEQUENCE - 112:d=5 hl=2 l= 3 prim: OBJECT :commonName - 117:d=5 hl=2 l= 30 prim: UTF8STRING :Precursor Self-Verified Device - 149:d=2 hl=2 l= 89 cons: SEQUENCE - 151:d=3 hl=2 l= 19 cons: SEQUENCE - 153:d=4 hl=2 l= 7 prim: OBJECT :id-ecPublicKey - 162:d=4 hl=2 l= 8 prim: OBJECT :prime256v1 - 172:d=3 hl=2 l= 66 prim: BIT STRING - 240:d=1 hl=2 l= 10 cons: SEQUENCE - 242:d=2 hl=2 l= 8 prim: OBJECT :ecdsa-with-SHA256 - 252:d=1 hl=2 l= 72 prim: BIT STRING -*/ - - /// The credential ID, as stored in OpenSK, is a 112-entry Vec that starts with - /// a random 128-bit AES IV. This effectively takes the 128-bit AES IV and turns it into - /// a hex string that is suitable for indexing into the PDDB. Collisions - /// are very rare in a 128-bit space, but the "full" credential is still checked - /// after the lookup. - fn cid_to_str(&self, credential_id: &[u8]) -> String { - let mut hex = String::new(); - // yes, I do know the "hex" crate exists but have you looked at its dependency tree?? - for &b in credential_id[..16].iter() { - hex.push_str(&format!("{:x}", b)); - } - hex - } - - /// Returns the first matching credential. - /// - /// Returns `None` if no credentials are matched or if `check_cred_protect` is set and the first - /// matched credential requires user verification. - pub fn find_credential( - &self, - rp_id: &str, - credential_id: &[u8], - check_cred_protect: bool, - ) -> Result, Ctap2StatusCode> { - let shortid = self.cid_to_str(credential_id); - match self.pddb.borrow().get( - FIDO_CRED_DICT, - &shortid, - None, false, false, - Some(CREDENTIAL_ID_SIZE), Some(crate::basis_change) - ) { - Ok(mut cred) => { - log::trace!("find {}", shortid); - let mut data = Vec::::new(); - cred.read_to_end(&mut data).or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - match deserialize_credential(&data) { - Some(result) => { - if result.credential_id == credential_id && result.rp_id == rp_id { - let user_verification_required = - result.cred_protect_policy == Some(CredentialProtectionPolicy::UserVerificationRequired); - if check_cred_protect && user_verification_required { - Ok(None) - } else { - Ok(Some(result)) - } - } else { - Ok(None) - } - } - None => { - log::warn!("Credential entry {} did not deserialize", shortid); - Err(Ctap2StatusCode::CTAP2_ERR_INVALID_CREDENTIAL) - } - } - } - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(None), - _ => { - log::error!("PDDB internal error in find_credential: {:?}", e); - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - }, // PDDB internal error - } - } - } - - /// Stores or updates a credential. - /// - /// If a credential with the same RP id and user handle already exists, it is replaced. - pub fn store_credential( - &mut self, - new_credential: PublicKeyCredentialSource, - ) -> Result<(), Ctap2StatusCode> { - if self.count_credentials()? >= MAX_SUPPORTED_RESIDENTIAL_KEYS { - return Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL); - } - let shortid = self.cid_to_str(&new_credential.credential_id); - match self.pddb.borrow().get( - FIDO_CRED_DICT, - &shortid, - None, true, true, - Some(CRED_INITAL_SIZE), Some(crate::basis_change) - ) { - Ok(mut cred) => { - let value = serialize_credential(new_credential)?; - log::trace!("writing {}", shortid); - cred.write(&value) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - Ok(()) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the list of matching credentials. - /// - /// Does not return credentials that are not discoverable if `check_cred_protect` is set. - pub fn filter_credential( - &self, - rp_id: &str, - check_cred_protect: bool, - ) -> Result, Ctap2StatusCode> { - let mut result = Vec::::new(); - let cred_list = self.pddb.borrow().list_keys( - FIDO_CRED_DICT, None).unwrap_or(Vec::new()); - for cred_name in cred_list.iter() { - if let Some(mut cred_entry) = self.pddb.borrow().get( - FIDO_CRED_DICT, - cred_name, - None, false, false, - Some(CREDENTIAL_ID_SIZE), Some(crate::basis_change) - ).ok() { - log::trace!("checking {}", cred_name); - let mut data = Vec::::new(); - cred_entry.read_to_end(&mut data).or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - if let Some(cred) = deserialize_credential(&data) { - if cred.rp_id == rp_id - && (cred.is_discoverable() || !check_cred_protect) { - log::trace!("filtered {}", cred_name); - result.push(cred); - } - } - } - } - Ok(result) - } - - /// Returns the number of credentials. - pub fn count_credentials(&self) -> Result { - let cred_list = self.pddb.borrow().list_keys( - FIDO_CRED_DICT, None).unwrap_or(Vec::new()); - Ok(cred_list.len()) - } - - /// Returns the next creation order. - pub fn new_creation_order(&self) -> Result { - let mut max = 0; - let cred_list = self.pddb.borrow().list_keys( - FIDO_CRED_DICT, None).unwrap_or(Vec::new()); - if cred_list.len() == 0 { - return Ok(0) - } - for cred_name in cred_list.iter() { - if let Some(mut cred_entry) = self.pddb.borrow().get( - FIDO_CRED_DICT, - cred_name, - None, false, false, - Some(CREDENTIAL_ID_SIZE), Some(crate::basis_change) - ).ok() { - let mut data = Vec::::new(); - cred_entry.read_to_end(&mut data).or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - if let Some(cred) = deserialize_credential(&data) { - max = max.max(cred.creation_order) - } - } - } - Ok(max.wrapping_add(1)) - } - - /// Returns the global signature counter. - pub fn global_signature_counter(&self) -> Result { - match self.pddb.borrow().get( - FIDO_DICT, - key::GLOBAL_SIGNATURE_COUNTER, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(4), Some(crate::basis_change) - ) { - Ok(mut gsc) => { - let mut value = [0u8; 4]; - match gsc.read(&mut value) { - Ok(4) => Ok(u32::from_ne_bytes(value)), - Ok(_) => { - gsc.seek(SeekFrom::Start(0)).ok(); // make sure we're writing to the beginning position - gsc.write(&INITIAL_SIGNATURE_COUNTER.to_ne_bytes()) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - Ok(INITIAL_SIGNATURE_COUNTER) - }, - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - - /// Increments the global signature counter. - pub fn incr_global_signature_counter(&mut self, increment: u32) -> Result<(), Ctap2StatusCode> { - let old_value = self.global_signature_counter()?; - // In hopes that servers handle the wrapping gracefully. - let new_value = old_value.wrapping_add(increment); - match self.pddb.borrow().get( - FIDO_DICT, - key::GLOBAL_SIGNATURE_COUNTER, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(4), Some(crate::basis_change) - ) { - Ok(mut gsc) => { - gsc.write(&new_value.to_ne_bytes()) - .map(|_|()) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - - /// Returns the master keys. - pub fn master_keys(&self) -> Result { - match self.pddb.borrow().get( - FIDO_DICT, - key::MASTER_KEYS, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, None, Some(crate::basis_change) - ) { - Ok(mut mk) => { - let mut master_keys = [0u8; 64]; - match mk.read(&mut master_keys) { - Ok(64) => { - Ok(MasterKeys { - encryption: *array_ref![master_keys, 0, 32], - hmac: *array_ref![master_keys, 32, 32], - }) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the CredRandom secret. - pub fn cred_random_secret(&self, has_uv: bool) -> Result<[u8; 32], Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_DICT, - key::CRED_RANDOM_SECRET, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, None, Some(crate::basis_change) - ) { - Ok(mut crs) => { - let mut cred_random_secret = [0u8; 64]; - match crs.read(&mut cred_random_secret) { - Ok(64) => { - let offset = if has_uv { 32 } else { 0 }; - Ok(*array_ref![cred_random_secret, offset, 32]) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the PIN hash if defined. - pub fn pin_hash(&self) -> Result, Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_DICT, - key::PIN_HASH, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, None, Some(crate::basis_change) - ) { - Ok(mut ph) => { - let mut pin_hash = [0u8; PIN_AUTH_LENGTH]; - match ph.read(&mut pin_hash) { - Ok(PIN_AUTH_LENGTH) => { - log::trace!("pin_hash: {:x?}", &pin_hash); - Ok(Some(pin_hash)) - }, - Ok(l) => { - log::error!("pin_hash read length incorrect: {}", l); - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - Err(e) => { - log::error!("pin_hash returned an error: {:?}", e); - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - }, - } - } - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => { - log::info!("pin_hash not found"); - Ok(None) - }, - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - } - } - - /// Sets the PIN hash. - /// - /// If it was already defined, it is updated. - pub fn set_pin_hash( - &mut self, - pin_hash: &[u8; PIN_AUTH_LENGTH], - ) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_DICT, - key::PIN_HASH, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(PIN_AUTH_LENGTH), Some(crate::basis_change) - ) { - Ok(mut ph) => { - match ph.write(pin_hash) { - Ok(PIN_AUTH_LENGTH) => { - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - Ok(()) - }, - Ok(l) => { - log::error!("set_pin_hash incorrect length: {}", l); - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - Err(e) => { - log::error!("set_pin_hash write error: {:?}", e); - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the number of remaining PIN retries. - pub fn pin_retries(&self) -> Result { - match self.pddb.borrow().get( - FIDO_DICT, - key::PIN_RETRIES, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, None, Some(crate::basis_change) - ) { - Ok(mut pr) => { - let mut value = [0u8; 1]; - match pr.read(&mut value) { - Ok(1) => Ok(value[0]), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(MAX_PIN_RETRIES), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - } - } - - /// Decrements the number of remaining PIN retries. - pub fn decr_pin_retries(&mut self) -> Result<(), Ctap2StatusCode> { - let old_value = self.pin_retries()?; - let new_value = old_value.saturating_sub(1); - if new_value != old_value { - match self.pddb.borrow().get( - FIDO_DICT, - key::PIN_RETRIES, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(1), Some(crate::basis_change) - ) { - Ok(mut pr) => { - pr.write(&[new_value]) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - } - _ => return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - Ok(()) - } - - /// Resets the number of remaining PIN retries. - pub fn reset_pin_retries(&mut self) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().delete_key( - FIDO_DICT, - PIN_RETRIES, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS) - ) { - Ok(_) => Ok(()), - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(()), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - } - } - - /// Returns the minimum PIN length. - #[cfg(feature = "with_ctap2_1")] - pub fn min_pin_length(&self) -> Result { - match self.pddb.borrow().get( - FIDO_DICT, - key::MIN_PIN_LENGTH, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), - false, false, None, Some(crate::basis_change) - ) { - Ok(mut pr) => { - let mut value = [0u8; 1]; - match pr.read(&mut value) { - Ok(1) => Ok(value[0]), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(DEFAULT_MIN_PIN_LENGTH), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - } - } - - /// Sets the minimum PIN length. - #[cfg(feature = "with_ctap2_1")] - pub fn set_min_pin_length(&mut self, min_pin_length: u8) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_DICT, - key::MIN_PIN_LENGTH, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(1), Some(crate::basis_change) - ) { - Ok(mut pl) => { - match pl.write(&[min_pin_length]) { - Ok(1) => Ok(()), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - - /// Returns the list of RP IDs that are used to check if reading the minimum PIN length is - /// allowed. - #[cfg(feature = "with_ctap2_1")] - pub fn _min_pin_length_rp_ids(&self) -> Result, Ctap2StatusCode> { - if let Some(mut mplri) = self.pddb.borrow().get( - FIDO_DICT, - key::_MIN_PIN_LENGTH_RP_IDS, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, - None, Some(crate::basis_change) - ).ok() { - let mut data = Vec::::new(); - mplri.read_to_end(&mut data).or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - if let Some(list) = _deserialize_min_pin_length_rp_ids(&data) { - Ok(list) - } else { - Ok(vec![]) - } - } else { - Ok(vec![]) - } - } - - /// Sets the list of RP IDs that are used to check if reading the minimum PIN length is allowed. - #[cfg(feature = "with_ctap2_1")] - pub fn _set_min_pin_length_rp_ids( - &mut self, - min_pin_length_rp_ids: Vec, - ) -> Result<(), Ctap2StatusCode> { - let mut min_pin_length_rp_ids = min_pin_length_rp_ids; - for rp_id in _DEFAULT_MIN_PIN_LENGTH_RP_IDS { - if !min_pin_length_rp_ids.contains(&rp_id) { - min_pin_length_rp_ids.push(rp_id); - } - } - if min_pin_length_rp_ids.len() > _MAX_RP_IDS_LENGTH { - return Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL); - } - match self.pddb.borrow().get( - FIDO_DICT, - key::_MIN_PIN_LENGTH_RP_IDS, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(_MAX_RP_IDS_LENGTH), Some(crate::basis_change) - ) { - Ok(mut mrpli) => { - mrpli.write(&_serialize_min_pin_length_rp_ids(min_pin_length_rp_ids)?) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)).map(|_|()) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - // ---------------- persistent records ---------------------- - /// Returns the attestation private key if defined. - pub fn attestation_private_key( - &self, - ) -> Result, Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_PRIVATE_KEY, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, - None, Some(crate::basis_change) - ) { - Ok(mut apk) => { - let mut key = [0u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH]; - match apk.read(&mut key) { - Ok(key_material::ATTESTATION_PRIVATE_KEY_LENGTH) => { - Ok(Some(*array_ref![ - key, - 0, - key_material::ATTESTATION_PRIVATE_KEY_LENGTH - ])) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(None), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - } - } - - /// Sets the attestation private key. - /// - /// If it is already defined, it is overwritten. - pub fn set_attestation_private_key( - &mut self, - attestation_private_key: &[u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH], - ) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_PRIVATE_KEY, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(key_material::ATTESTATION_PRIVATE_KEY_LENGTH), Some(crate::basis_change) - ) { - Ok(mut apk) => { - apk.write(attestation_private_key) - .map(|_|()) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the attestation certificate if defined. - pub fn attestation_certificate(&self) -> Result>, Ctap2StatusCode> { - if let Some(mut acert) = self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_CERTIFICATE, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, - None, Some(crate::basis_change) - ).ok() { - let mut data = Vec::::new(); - match acert.read_to_end(&mut data) { - Ok(l) => log::trace!("read {} bytes", l), - Err(e) => log::error!("error reading certificate: {:?}", e), - } - log::trace!("read back cert: {:x?}", data); - Ok(Some(data)) - } else { - Ok(None) - } - } - - /// Sets the attestation certificate. - /// - /// If it is already defined, it is overwritten. - pub fn set_attestation_certificate( - &mut self, - attestation_certificate: &[u8], - ) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::ATTESTATION_CERTIFICATE, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(1024), Some(crate::basis_change) - ) { - Ok(mut acert) => { - acert.write(attestation_certificate) - .map(|_|()) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } - - /// Returns the AAGUID. - pub fn aaguid(&self) -> Result<[u8; key_material::AAGUID_LENGTH], Ctap2StatusCode> { - if let Some(mut guid) = self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::AAGUID, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false, - None, Some(crate::basis_change) - ).ok() { - let mut data = [0u8; key_material::AAGUID_LENGTH]; - match guid.read(&mut data) { - Ok(key_material::AAGUID_LENGTH) => { - Ok(data) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR) - } - } else { - Ok([0u8; key_material::AAGUID_LENGTH]) - } - } - - /// Sets the AAGUID. - /// - /// If it is already defined, it is overwritten. - #[allow(dead_code)] - pub fn set_aaguid( - &mut self, - aaguid: &[u8; key_material::AAGUID_LENGTH], - ) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().get( - FIDO_PERSISTENT_DICT, - key::AAGUID, - Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, true, - Some(key_material::AAGUID_LENGTH), Some(crate::basis_change) - ) { - Ok(mut guid) => { - guid.write(aaguid) - .map(|_|()) - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR))?; - self.pddb.borrow().sync() - .or(Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)) - } - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), - } - } - - /// Resets the store as for a CTAP reset. - /// - /// In particular persistent entries are not reset. - pub fn reset(&mut self, _rng: &mut impl Rng256) -> Result<(), Ctap2StatusCode> { - match self.pddb.borrow().delete_dict(FIDO_DICT, Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS)) { - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(()), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - Ok(_) => Ok(()), - }?; - for basis in self.pddb.borrow().list_basis().iter () { - match self.pddb.borrow().delete_dict(FIDO_CRED_DICT, Some(basis)) { - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(()), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - Ok(_) => Ok(()), - }?; - match self.pddb.borrow().delete_dict(crate::fido::U2F_APP_DICT, Some(basis)) { - Err(e) => match e.kind() { - std::io::ErrorKind::NotFound => Ok(()), - _ => Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR), // PDDB internal error - } - Ok(_) => Ok(()), - }?; - } - // don't delete the persistent dictionary... - self.init()?; - Ok(()) + None } } /// Deserializes a credential from storage representation. -pub(crate) fn deserialize_credential(data: &[u8]) -> Option { - let cbor = cbor::read(data).ok()?; +pub fn deserialize_credential(data: &[u8]) -> Option { + let cbor = super::cbor_read(data).ok()?; cbor.try_into().ok() } /// Serializes a credential to storage representation. -pub(crate) fn serialize_credential(credential: PublicKeyCredentialSource) -> Result, Ctap2StatusCode> { +pub fn serialize_credential(credential: PublicKeyCredentialSource) -> Result, Ctap2StatusCode> { let mut data = Vec::new(); - if cbor::write(credential.into(), &mut data) { - Ok(data) - } else { - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR) - } + super::cbor_write(credential.into(), &mut data)?; + Ok(data) } /// Deserializes a list of RP IDs from storage representation. -#[cfg(feature = "with_ctap2_1")] -fn _deserialize_min_pin_length_rp_ids(data: &[u8]) -> Option> { - let cbor = cbor::read(data).ok()?; +fn deserialize_min_pin_length_rp_ids(data: &[u8]) -> Option> { + let cbor = super::cbor_read(data).ok()?; extract_array(cbor) .ok()? .into_iter() @@ -1038,31 +636,32 @@ fn _deserialize_min_pin_length_rp_ids(data: &[u8]) -> Option> { } /// Serializes a list of RP IDs to storage representation. -#[cfg(feature = "with_ctap2_1")] -fn _serialize_min_pin_length_rp_ids(rp_ids: Vec) -> Result, Ctap2StatusCode> { +fn serialize_min_pin_length_rp_ids(rp_ids: Vec) -> Result, Ctap2StatusCode> { let mut data = Vec::new(); - if cbor::write(cbor_array_vec!(rp_ids), &mut data) { - Ok(data) - } else { - Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_RESPONSE_CANNOT_WRITE_CBOR) - } + super::cbor_write(cbor_array_vec!(rp_ids), &mut data)?; + Ok(data) } #[cfg(test)] mod test { use super::*; - use crate::ctap::data_formats::{PublicKeyCredentialSource, PublicKeyCredentialType}; - use ctap_crypto::rng256::{Rng256, ThreadRng256}; + use crate::api::attestation_store::{self, Attestation, AttestationStore}; + use crate::ctap::crypto_wrapper::PrivateKey; + use crate::ctap::data_formats::{ + CredentialProtectionPolicy, PublicKeyCredentialSource, PublicKeyCredentialType, + }; + use crate::env::test::TestEnv; + use ctap_crypto::rng256::Rng256; fn create_credential_source( - rng: &mut ThreadRng256, + env: &mut TestEnv, rp_id: &str, user_handle: Vec, ) -> PublicKeyCredentialSource { - let private_key = ctap_crypto::ecdsa::SecKey::gensk(rng); + let private_key = PrivateKey::new_ecdsa(env); PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, - credential_id: rng.gen_uniform_u8x32().to_vec(), + credential_id: env.rng().gen_uniform_u8x32().to_vec(), private_key, rp_id: String::from(rp_id), user_handle, @@ -1071,196 +670,191 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, } } #[test] fn test_store() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let credential_source = create_credential_source(&mut rng, "example.com", vec![]); - assert!(persistent_store.store_credential(credential_source).is_ok()); - assert!(persistent_store.count_credentials().unwrap() > 0); + let mut env = TestEnv::new(); + assert_eq!(count_credentials(&mut env).unwrap(), 0); + let credential_source = create_credential_source(&mut env, "example.com", vec![]); + assert!(store_credential(&mut env, credential_source).is_ok()); + assert!(count_credentials(&mut env).unwrap() > 0); + } + + #[test] + fn test_delete_credential() { + let mut env = TestEnv::new(); + assert_eq!(count_credentials(&mut env).unwrap(), 0); + + let mut credential_ids = vec![]; + for i in 0..env.customization().max_supported_resident_keys() { + let user_handle = (i as u32).to_ne_bytes().to_vec(); + let credential_source = create_credential_source(&mut env, "example.com", user_handle); + credential_ids.push(credential_source.credential_id.clone()); + assert!(store_credential(&mut env, credential_source).is_ok()); + assert_eq!(count_credentials(&mut env).unwrap(), i + 1); + } + let mut count = count_credentials(&mut env).unwrap(); + for credential_id in credential_ids { + assert!(delete_credential(&mut env, &credential_id).is_ok()); + count -= 1; + assert_eq!(count_credentials(&mut env).unwrap(), count); + } + } + + #[test] + fn test_update_credential() { + let mut env = TestEnv::new(); + let user = PublicKeyCredentialUserEntity { + // User ID is ignored. + user_id: vec![0x00], + user_name: Some("name".to_string()), + user_display_name: Some("display_name".to_string()), + user_icon: Some("icon".to_string()), + }; + assert_eq!( + update_credential(&mut env, &[0x1D], user.clone()), + Err(Ctap2StatusCode::CTAP2_ERR_NO_CREDENTIALS) + ); + + let credential_source = create_credential_source(&mut env, "example.com", vec![0x1D]); + let credential_id = credential_source.credential_id.clone(); + assert!(store_credential(&mut env, credential_source).is_ok()); + let stored_credential = find_credential(&mut env, "example.com", &credential_id) + .unwrap() + .unwrap(); + assert_eq!(stored_credential.user_name, None); + assert_eq!(stored_credential.user_display_name, None); + assert_eq!(stored_credential.user_icon, None); + assert!(update_credential(&mut env, &credential_id, user.clone()).is_ok()); + let stored_credential = find_credential(&mut env, "example.com", &credential_id) + .unwrap() + .unwrap(); + assert_eq!(stored_credential.user_name, user.user_name); + assert_eq!(stored_credential.user_display_name, user.user_display_name); + assert_eq!(stored_credential.user_icon, user.user_icon); } #[test] fn test_credential_order() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - let credential_source = create_credential_source(&mut rng, "example.com", vec![]); + let mut env = TestEnv::new(); + let credential_source = create_credential_source(&mut env, "example.com", vec![]); let current_latest_creation = credential_source.creation_order; - assert!(persistent_store.store_credential(credential_source).is_ok()); - let mut credential_source = create_credential_source(&mut rng, "example.com", vec![]); - credential_source.creation_order = persistent_store.new_creation_order().unwrap(); + assert!(store_credential(&mut env, credential_source).is_ok()); + let mut credential_source = create_credential_source(&mut env, "example.com", vec![]); + credential_source.creation_order = new_creation_order(&mut env).unwrap(); assert!(credential_source.creation_order > current_latest_creation); let current_latest_creation = credential_source.creation_order; - assert!(persistent_store.store_credential(credential_source).is_ok()); - assert!(persistent_store.new_creation_order().unwrap() > current_latest_creation); + assert!(store_credential(&mut env, credential_source).is_ok()); + assert!(new_creation_order(&mut env).unwrap() > current_latest_creation); } #[test] - #[allow(clippy::assertions_on_constants)] fn test_fill_store() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); + let mut env = TestEnv::new(); + assert_eq!(count_credentials(&mut env).unwrap(), 0); - // To make this test work for bigger storages, implement better int -> Vec conversion. - assert!(MAX_SUPPORTED_RESIDENTIAL_KEYS < 256); - for i in 0..MAX_SUPPORTED_RESIDENTIAL_KEYS { - let credential_source = - create_credential_source(&mut rng, "example.com", vec![i as u8]); - assert!(persistent_store.store_credential(credential_source).is_ok()); - assert_eq!(persistent_store.count_credentials().unwrap(), i + 1); + let max_supported_resident_keys = env.customization().max_supported_resident_keys(); + for i in 0..max_supported_resident_keys { + let user_handle = (i as u32).to_ne_bytes().to_vec(); + let credential_source = create_credential_source(&mut env, "example.com", user_handle); + assert!(store_credential(&mut env, credential_source).is_ok()); + assert_eq!(count_credentials(&mut env).unwrap(), i + 1); } let credential_source = create_credential_source( - &mut rng, + &mut env, "example.com", - vec![MAX_SUPPORTED_RESIDENTIAL_KEYS as u8], + vec![max_supported_resident_keys as u8], ); assert_eq!( - persistent_store.store_credential(credential_source), + store_credential(&mut env, credential_source), Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL) ); assert_eq!( - persistent_store.count_credentials().unwrap(), - MAX_SUPPORTED_RESIDENTIAL_KEYS + count_credentials(&mut env).unwrap(), + max_supported_resident_keys ); } #[test] - #[allow(clippy::assertions_on_constants)] fn test_overwrite() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); + let mut env = TestEnv::new(); + init(&mut env).unwrap(); + + assert_eq!(count_credentials(&mut env).unwrap(), 0); // These should have different IDs. - let credential_source0 = create_credential_source(&mut rng, "example.com", vec![0x00]); - let credential_source1 = create_credential_source(&mut rng, "example.com", vec![0x00]); - let expected_credential = credential_source1.clone(); + let credential_source0 = create_credential_source(&mut env, "example.com", vec![0x00]); + let credential_source1 = create_credential_source(&mut env, "example.com", vec![0x00]); + let credential_id0 = credential_source0.credential_id.clone(); + let credential_id1 = credential_source1.credential_id.clone(); - assert!(persistent_store - .store_credential(credential_source0) - .is_ok()); - assert!(persistent_store - .store_credential(credential_source1) - .is_ok()); - assert_eq!(persistent_store.count_credentials().unwrap(), 1); - assert_eq!( - &persistent_store - .filter_credential("example.com", false) - .unwrap(), - &[expected_credential] - ); + assert!(store_credential(&mut env, credential_source0).is_ok()); + assert!(store_credential(&mut env, credential_source1).is_ok()); + assert_eq!(count_credentials(&mut env).unwrap(), 1); + assert!(find_credential(&mut env, "example.com", &credential_id0) + .unwrap() + .is_none()); + assert!(find_credential(&mut env, "example.com", &credential_id1) + .unwrap() + .is_some()); - // To make this test work for bigger storages, implement better int -> Vec conversion. - assert!(MAX_SUPPORTED_RESIDENTIAL_KEYS < 256); - for i in 0..MAX_SUPPORTED_RESIDENTIAL_KEYS { - let credential_source = - create_credential_source(&mut rng, "example.com", vec![i as u8]); - assert!(persistent_store.store_credential(credential_source).is_ok()); - assert_eq!(persistent_store.count_credentials().unwrap(), i + 1); + reset(&mut env).unwrap(); + let max_supported_resident_keys = env.customization().max_supported_resident_keys(); + for i in 0..max_supported_resident_keys { + let user_handle = (i as u32).to_ne_bytes().to_vec(); + let credential_source = create_credential_source(&mut env, "example.com", user_handle); + assert!(store_credential(&mut env, credential_source).is_ok()); + assert_eq!(count_credentials(&mut env).unwrap(), i + 1); } let credential_source = create_credential_source( - &mut rng, + &mut env, "example.com", - vec![MAX_SUPPORTED_RESIDENTIAL_KEYS as u8], + vec![max_supported_resident_keys as u8], ); assert_eq!( - persistent_store.store_credential(credential_source), + store_credential(&mut env, credential_source), Err(Ctap2StatusCode::CTAP2_ERR_KEY_STORE_FULL) ); assert_eq!( - persistent_store.count_credentials().unwrap(), - MAX_SUPPORTED_RESIDENTIAL_KEYS + count_credentials(&mut env).unwrap(), + max_supported_resident_keys ); } #[test] - fn test_filter() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let credential_source0 = create_credential_source(&mut rng, "example.com", vec![0x00]); - let credential_source1 = create_credential_source(&mut rng, "example.com", vec![0x01]); + fn test_get_credential() { + let mut env = TestEnv::new(); + let credential_source0 = create_credential_source(&mut env, "example.com", vec![0x00]); + let credential_source1 = create_credential_source(&mut env, "example.com", vec![0x01]); let credential_source2 = - create_credential_source(&mut rng, "another.example.com", vec![0x02]); - let id0 = credential_source0.credential_id.clone(); - let id1 = credential_source1.credential_id.clone(); - assert!(persistent_store - .store_credential(credential_source0) - .is_ok()); - assert!(persistent_store - .store_credential(credential_source1) - .is_ok()); - assert!(persistent_store - .store_credential(credential_source2) - .is_ok()); - - let filtered_credentials = persistent_store - .filter_credential("example.com", false) - .unwrap(); - assert_eq!(filtered_credentials.len(), 2); - assert!( - (filtered_credentials[0].credential_id == id0 - && filtered_credentials[1].credential_id == id1) - || (filtered_credentials[1].credential_id == id0 - && filtered_credentials[0].credential_id == id1) - ); - } - - #[test] - fn test_filter_with_cred_protect() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let credential = PublicKeyCredentialSource { - key_type: PublicKeyCredentialType::PublicKey, - credential_id: rng.gen_uniform_u8x32().to_vec(), - private_key, - rp_id: String::from("example.com"), - user_handle: vec![0x00], - user_display_name: None, - cred_protect_policy: Some( - CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList, - ), - creation_order: 0, - user_name: None, - user_icon: None, - }; - assert!(persistent_store.store_credential(credential).is_ok()); - - let no_credential = persistent_store - .filter_credential("example.com", true) - .unwrap(); - assert_eq!(no_credential, vec![]); + create_credential_source(&mut env, "another.example.com", vec![0x02]); + let credential_sources = vec![credential_source0, credential_source1, credential_source2]; + for credential_source in credential_sources.into_iter() { + let cred_id = credential_source.credential_id.clone(); + assert!(store_credential(&mut env, credential_source).is_ok()); + let (key, _) = find_credential_item(&mut env, &cred_id).unwrap(); + let cred = get_credential(&mut env, key).unwrap(); + assert_eq!(&cred_id, &cred.credential_id); + } } #[test] fn test_find() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let credential_source0 = create_credential_source(&mut rng, "example.com", vec![0x00]); - let credential_source1 = create_credential_source(&mut rng, "example.com", vec![0x01]); + let mut env = TestEnv::new(); + assert_eq!(count_credentials(&mut env).unwrap(), 0); + let credential_source0 = create_credential_source(&mut env, "example.com", vec![0x00]); + let credential_source1 = create_credential_source(&mut env, "example.com", vec![0x01]); let id0 = credential_source0.credential_id.clone(); let key0 = credential_source0.private_key.clone(); - assert!(persistent_store - .store_credential(credential_source0) - .is_ok()); - assert!(persistent_store - .store_credential(credential_source1) - .is_ok()); + assert!(store_credential(&mut env, credential_source0).is_ok()); + assert!(store_credential(&mut env, credential_source1).is_ok()); - let no_credential = persistent_store - .find_credential("another.example.com", &id0, false) - .unwrap(); + let no_credential = find_credential(&mut env, "another.example.com", &id0).unwrap(); assert_eq!(no_credential, None); - let found_credential = persistent_store - .find_credential("example.com", &id0, false) - .unwrap(); + let found_credential = find_credential(&mut env, "example.com", &id0).unwrap(); let expected_credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: id0, @@ -1272,264 +866,313 @@ mod test { creation_order: 0, user_name: None, user_icon: None, + cred_blob: None, + large_blob_key: None, }; assert_eq!(found_credential, Some(expected_credential)); } - #[test] - fn test_find_with_cred_protect() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); - let credential = PublicKeyCredentialSource { - key_type: PublicKeyCredentialType::PublicKey, - credential_id: rng.gen_uniform_u8x32().to_vec(), - private_key, - rp_id: String::from("example.com"), - user_handle: vec![0x00], - user_display_name: None, - cred_protect_policy: Some(CredentialProtectionPolicy::UserVerificationRequired), - creation_order: 0, - user_name: None, - user_icon: None, - }; - assert!(persistent_store.store_credential(credential).is_ok()); - - let no_credential = persistent_store - .find_credential("example.com", &[0x00], true) - .unwrap(); - assert_eq!(no_credential, None); - } - - #[test] - fn test_master_keys() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); - - // Master keys stay the same within the same CTAP reset cycle. - let master_keys_1 = persistent_store.master_keys().unwrap(); - let master_keys_2 = persistent_store.master_keys().unwrap(); - assert_eq!(master_keys_2.encryption, master_keys_1.encryption); - assert_eq!(master_keys_2.hmac, master_keys_1.hmac); - - // Master keys change after reset. This test may fail if the random generator produces the - // same keys. - let master_encryption_key = master_keys_1.encryption.to_vec(); - let master_hmac_key = master_keys_1.hmac.to_vec(); - persistent_store.reset(&mut rng).unwrap(); - let master_keys_3 = persistent_store.master_keys().unwrap(); - assert!(master_keys_3.encryption != master_encryption_key.as_slice()); - assert!(master_keys_3.hmac != master_hmac_key.as_slice()); - } - #[test] fn test_cred_random_secret() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); + init(&mut env).unwrap(); // CredRandom secrets stay the same within the same CTAP reset cycle. - let cred_random_with_uv_1 = persistent_store.cred_random_secret(true).unwrap(); - let cred_random_without_uv_1 = persistent_store.cred_random_secret(false).unwrap(); - let cred_random_with_uv_2 = persistent_store.cred_random_secret(true).unwrap(); - let cred_random_without_uv_2 = persistent_store.cred_random_secret(false).unwrap(); + let cred_random_with_uv_1 = cred_random_secret(&mut env, true).unwrap(); + let cred_random_without_uv_1 = cred_random_secret(&mut env, false).unwrap(); + let cred_random_with_uv_2 = cred_random_secret(&mut env, true).unwrap(); + let cred_random_without_uv_2 = cred_random_secret(&mut env, false).unwrap(); assert_eq!(cred_random_with_uv_1, cred_random_with_uv_2); assert_eq!(cred_random_without_uv_1, cred_random_without_uv_2); // CredRandom secrets change after reset. This test may fail if the random generator produces the // same keys. - persistent_store.reset(&mut rng).unwrap(); - let cred_random_with_uv_3 = persistent_store.cred_random_secret(true).unwrap(); - let cred_random_without_uv_3 = persistent_store.cred_random_secret(false).unwrap(); + reset(&mut env).unwrap(); + let cred_random_with_uv_3 = cred_random_secret(&mut env, true).unwrap(); + let cred_random_without_uv_3 = cred_random_secret(&mut env, false).unwrap(); assert!(cred_random_with_uv_1 != cred_random_with_uv_3); assert!(cred_random_without_uv_1 != cred_random_without_uv_3); } #[test] - fn test_pin_hash() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + fn test_pin_hash_and_length() { + let mut env = TestEnv::new(); // Pin hash is initially not set. - assert!(persistent_store.pin_hash().unwrap().is_none()); + assert!(pin_hash(&mut env).unwrap().is_none()); + assert!(pin_code_point_length(&mut env).unwrap().is_none()); - // Setting the pin hash sets the pin hash. - let random_data = rng.gen_uniform_u8x32(); + // Setting the pin sets the pin hash. + let random_data = env.rng().gen_uniform_u8x32(); assert_eq!(random_data.len(), 2 * PIN_AUTH_LENGTH); let pin_hash_1 = *array_ref!(random_data, 0, PIN_AUTH_LENGTH); let pin_hash_2 = *array_ref!(random_data, PIN_AUTH_LENGTH, PIN_AUTH_LENGTH); - persistent_store.set_pin_hash(&pin_hash_1).unwrap(); - assert_eq!(persistent_store.pin_hash().unwrap(), Some(pin_hash_1)); - assert_eq!(persistent_store.pin_hash().unwrap(), Some(pin_hash_1)); - persistent_store.set_pin_hash(&pin_hash_2).unwrap(); - assert_eq!(persistent_store.pin_hash().unwrap(), Some(pin_hash_2)); - assert_eq!(persistent_store.pin_hash().unwrap(), Some(pin_hash_2)); + let pin_length_1 = 4; + let pin_length_2 = 63; + set_pin(&mut env, &pin_hash_1, pin_length_1).unwrap(); + assert_eq!(pin_hash(&mut env).unwrap(), Some(pin_hash_1)); + assert_eq!(pin_code_point_length(&mut env).unwrap(), Some(pin_length_1)); + set_pin(&mut env, &pin_hash_2, pin_length_2).unwrap(); + assert_eq!(pin_hash(&mut env).unwrap(), Some(pin_hash_2)); + assert_eq!(pin_code_point_length(&mut env).unwrap(), Some(pin_length_2)); // Resetting the storage resets the pin hash. - persistent_store.reset(&mut rng).unwrap(); - assert!(persistent_store.pin_hash().unwrap().is_none()); + reset(&mut env).unwrap(); + assert!(pin_hash(&mut env).unwrap().is_none()); + assert!(pin_code_point_length(&mut env).unwrap().is_none()); } #[test] fn test_pin_retries() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); // The pin retries is initially at the maximum. - assert_eq!(persistent_store.pin_retries(), Ok(MAX_PIN_RETRIES)); + assert_eq!( + pin_retries(&mut env), + Ok(env.customization().max_pin_retries()) + ); // Decrementing the pin retries decrements the pin retries. - for pin_retries in (0..MAX_PIN_RETRIES).rev() { - persistent_store.decr_pin_retries().unwrap(); - assert_eq!(persistent_store.pin_retries(), Ok(pin_retries)); + for retries in (0..env.customization().max_pin_retries()).rev() { + decr_pin_retries(&mut env).unwrap(); + assert_eq!(pin_retries(&mut env), Ok(retries)); } // Decrementing the pin retries after zero does not modify the pin retries. - persistent_store.decr_pin_retries().unwrap(); - assert_eq!(persistent_store.pin_retries(), Ok(0)); + decr_pin_retries(&mut env).unwrap(); + assert_eq!(pin_retries(&mut env), Ok(0)); // Resetting the pin retries resets the pin retries. - persistent_store.reset_pin_retries().unwrap(); - assert_eq!(persistent_store.pin_retries(), Ok(MAX_PIN_RETRIES)); + reset_pin_retries(&mut env).unwrap(); + assert_eq!( + pin_retries(&mut env), + Ok(env.customization().max_pin_retries()) + ); } #[test] fn test_persistent_keys() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); + init(&mut env).unwrap(); // Make sure the attestation are absent. There is no batch attestation in tests. - assert!(persistent_store - .attestation_private_key() - .unwrap() - .is_none()); - assert!(persistent_store - .attestation_certificate() - .unwrap() - .is_none()); + assert_eq!( + env.attestation_store().get(&attestation_store::Id::Batch), + Ok(None) + ); // Make sure the persistent keys are initialized to dummy values. - let dummy_key = [0x41u8; key_material::ATTESTATION_PRIVATE_KEY_LENGTH]; - let dummy_cert = [0xddu8; 20]; - persistent_store - .set_attestation_private_key(&dummy_key) + let dummy_attestation = Attestation { + private_key: [0x41; key_material::ATTESTATION_PRIVATE_KEY_LENGTH], + certificate: vec![0xdd; 20], + }; + env.attestation_store() + .set(&attestation_store::Id::Batch, Some(&dummy_attestation)) .unwrap(); - persistent_store - .set_attestation_certificate(&dummy_cert) - .unwrap(); - assert_eq!(&persistent_store.aaguid().unwrap(), key_material::AAGUID); + assert_eq!(&aaguid(&mut env).unwrap(), key_material::AAGUID); // The persistent keys stay initialized and preserve their value after a reset. - persistent_store.reset(&mut rng).unwrap(); + reset(&mut env).unwrap(); assert_eq!( - &persistent_store.attestation_private_key().unwrap().unwrap(), - &dummy_key + env.attestation_store().get(&attestation_store::Id::Batch), + Ok(Some(dummy_attestation)) ); - assert_eq!( - persistent_store.attestation_certificate().unwrap().unwrap(), - &dummy_cert - ); - assert_eq!(&persistent_store.aaguid().unwrap(), key_material::AAGUID); + assert_eq!(&aaguid(&mut env).unwrap(), key_material::AAGUID); } - #[cfg(feature = "with_ctap2_1")] #[test] fn test_min_pin_length() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); // The minimum PIN length is initially at the default. assert_eq!( - persistent_store.min_pin_length().unwrap(), - DEFAULT_MIN_PIN_LENGTH + min_pin_length(&mut env).unwrap(), + env.customization().default_min_pin_length() ); // Changes by the setter are reflected by the getter.. let new_min_pin_length = 8; - persistent_store - .set_min_pin_length(new_min_pin_length) - .unwrap(); - assert_eq!( - persistent_store.min_pin_length().unwrap(), - new_min_pin_length - ); + set_min_pin_length(&mut env, new_min_pin_length).unwrap(); + assert_eq!(min_pin_length(&mut env).unwrap(), new_min_pin_length); } - #[cfg(feature = "with_ctap2_1")] #[test] fn test_min_pin_length_rp_ids() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); // The minimum PIN length RP IDs are initially at the default. assert_eq!( - persistent_store._min_pin_length_rp_ids().unwrap(), - _DEFAULT_MIN_PIN_LENGTH_RP_IDS + min_pin_length_rp_ids(&mut env).unwrap(), + env.customization().default_min_pin_length_rp_ids() ); // Changes by the setter are reflected by the getter. let mut rp_ids = vec![String::from("example.com")]; - assert_eq!( - persistent_store._set_min_pin_length_rp_ids(rp_ids.clone()), - Ok(()) - ); - for rp_id in _DEFAULT_MIN_PIN_LENGTH_RP_IDS { + assert_eq!(set_min_pin_length_rp_ids(&mut env, rp_ids.clone()), Ok(())); + for rp_id in env.customization().default_min_pin_length_rp_ids() { if !rp_ids.contains(&rp_id) { rp_ids.push(rp_id); } } - assert_eq!(persistent_store._min_pin_length_rp_ids().unwrap(), rp_ids); + assert_eq!(min_pin_length_rp_ids(&mut env).unwrap(), rp_ids); + } + + #[test] + fn test_max_large_blob_array_size() { + let mut env = TestEnv::new(); + + assert!( + env.customization().max_large_blob_array_size() + <= env.store().max_value_length() + * (key::LARGE_BLOB_SHARDS.end - key::LARGE_BLOB_SHARDS.start) + ); + } + + #[test] + fn test_commit_get_large_blob_array() { + let mut env = TestEnv::new(); + + let large_blob_array = vec![0x01, 0x02, 0x03]; + assert!(commit_large_blob_array(&mut env, &large_blob_array).is_ok()); + let restored_large_blob_array = get_large_blob_array(&mut env, 0, 1).unwrap(); + assert_eq!(vec![0x01], restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 1, 1).unwrap(); + assert_eq!(vec![0x02], restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 2, 1).unwrap(); + assert_eq!(vec![0x03], restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 2, 2).unwrap(); + assert_eq!(vec![0x03], restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 3, 1).unwrap(); + assert_eq!(Vec::::new(), restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 4, 1).unwrap(); + assert_eq!(Vec::::new(), restored_large_blob_array); + } + + #[test] + fn test_commit_get_large_blob_array_overwrite() { + let mut env = TestEnv::new(); + + let large_blob_array = vec![0x11; 5]; + assert!(commit_large_blob_array(&mut env, &large_blob_array).is_ok()); + let large_blob_array = vec![0x22; 4]; + assert!(commit_large_blob_array(&mut env, &large_blob_array).is_ok()); + let restored_large_blob_array = get_large_blob_array(&mut env, 0, 5).unwrap(); + assert_eq!(large_blob_array, restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 4, 1).unwrap(); + assert_eq!(Vec::::new(), restored_large_blob_array); + + assert!(commit_large_blob_array(&mut env, &[]).is_ok()); + let restored_large_blob_array = get_large_blob_array(&mut env, 0, 20).unwrap(); + // Committing an empty array resets to the default blob of 17 byte. + assert_eq!(restored_large_blob_array.len(), 17); + } + + #[test] + fn test_commit_get_large_blob_array_no_commit() { + let mut env = TestEnv::new(); + + let empty_blob_array = vec![ + 0x80, 0x76, 0xBE, 0x8B, 0x52, 0x8D, 0x00, 0x75, 0xF7, 0xAA, 0xE9, 0x8D, 0x6F, 0xA5, + 0x7A, 0x6D, 0x3C, + ]; + let restored_large_blob_array = get_large_blob_array(&mut env, 0, 17).unwrap(); + assert_eq!(empty_blob_array, restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 0, 1).unwrap(); + assert_eq!(vec![0x80], restored_large_blob_array); + let restored_large_blob_array = get_large_blob_array(&mut env, 16, 1).unwrap(); + assert_eq!(vec![0x3C], restored_large_blob_array); } #[test] fn test_global_signature_counter() { - let mut rng = ThreadRng256 {}; - let mut persistent_store = PersistentStore::new(&mut rng); + let mut env = TestEnv::new(); let mut counter_value = 1; - assert_eq!( - persistent_store.global_signature_counter().unwrap(), - counter_value - ); + assert_eq!(global_signature_counter(&mut env).unwrap(), counter_value); for increment in 1..10 { - assert!(persistent_store - .incr_global_signature_counter(increment) - .is_ok()); + assert!(incr_global_signature_counter(&mut env, increment).is_ok()); counter_value += increment; + assert_eq!(global_signature_counter(&mut env).unwrap(), counter_value); + } + } + + #[test] + fn test_force_pin_change() { + let mut env = TestEnv::new(); + + assert!(!has_force_pin_change(&mut env).unwrap()); + assert_eq!(force_pin_change(&mut env), Ok(())); + assert!(has_force_pin_change(&mut env).unwrap()); + assert_eq!(set_pin(&mut env, &[0x88; 16], 8), Ok(())); + assert!(!has_force_pin_change(&mut env).unwrap()); + } + + #[test] + fn test_enterprise_attestation() { + let mut env = TestEnv::new(); + + let dummy_attestation = Attestation { + private_key: [0x41; key_material::ATTESTATION_PRIVATE_KEY_LENGTH], + certificate: vec![0xdd; 20], + }; + env.attestation_store() + .set(&attestation_store::Id::Enterprise, Some(&dummy_attestation)) + .unwrap(); + + assert!(!enterprise_attestation(&mut env).unwrap()); + assert_eq!(enable_enterprise_attestation(&mut env), Ok(())); + assert!(enterprise_attestation(&mut env).unwrap()); + reset(&mut env).unwrap(); + assert!(!enterprise_attestation(&mut env).unwrap()); + } + + #[test] + fn test_always_uv() { + let mut env = TestEnv::new(); + + if env.customization().enforce_always_uv() { + assert!(has_always_uv(&mut env).unwrap()); assert_eq!( - persistent_store.global_signature_counter().unwrap(), - counter_value + toggle_always_uv(&mut env), + Err(Ctap2StatusCode::CTAP2_ERR_OPERATION_DENIED) ); + } else { + assert!(!has_always_uv(&mut env).unwrap()); + assert_eq!(toggle_always_uv(&mut env), Ok(())); + assert!(has_always_uv(&mut env).unwrap()); + assert_eq!(toggle_always_uv(&mut env), Ok(())); + assert!(!has_always_uv(&mut env).unwrap()); } } #[test] fn test_serialize_deserialize_credential() { - let mut rng = ThreadRng256 {}; - let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); + let mut env = TestEnv::new(); + let private_key = PrivateKey::new_ecdsa(&mut env); let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, - credential_id: rng.gen_uniform_u8x32().to_vec(), + credential_id: env.rng().gen_uniform_u8x32().to_vec(), private_key, rp_id: String::from("example.com"), user_handle: vec![0x00], - user_display_name: None, - cred_protect_policy: None, + user_display_name: Some(String::from("Display Name")), + cred_protect_policy: Some(CredentialProtectionPolicy::UserVerificationOptional), creation_order: 0, - user_name: None, - user_icon: None, + user_name: Some(String::from("name")), + user_icon: Some(String::from("icon")), + cred_blob: Some(vec![0xCB]), + large_blob_key: Some(vec![0x1B]), }; let serialized = serialize_credential(credential.clone()).unwrap(); let reconstructed = deserialize_credential(&serialized).unwrap(); assert_eq!(credential, reconstructed); } - #[cfg(feature = "with_ctap2_1")] #[test] fn test_serialize_deserialize_min_pin_length_rp_ids() { let rp_ids = vec![String::from("example.com")]; - let serialized = _serialize_min_pin_length_rp_ids(rp_ids.clone()).unwrap(); - let reconstructed = _deserialize_min_pin_length_rp_ids(&serialized).unwrap(); + let serialized = serialize_min_pin_length_rp_ids(rp_ids.clone()).unwrap(); + let reconstructed = deserialize_min_pin_length_rp_ids(&serialized).unwrap(); assert_eq!(rp_ids, reconstructed); } } diff --git a/apps/vault/src/ctap/storage/key.rs b/apps/vault/src/ctap/storage/key.rs index 3f93e7645..6b6f28aed 100644 --- a/apps/vault/src/ctap/storage/key.rs +++ b/apps/vault/src/ctap/storage/key.rs @@ -12,41 +12,61 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::stringify; - /// Number of keys that persist the CTAP reset command. -#[allow(dead_code)] // OpenSK legacy pub const NUM_PERSISTENT_KEYS: usize = 20; -/// Helper to define keys as a partial partition of a range. -macro_rules! define_names { - ($( - $(#[$doc: meta])* - $name: ident = $key: literal $(.. $end: literal)?; - )*) => { - $( - pub const $name: &'static str = stringify!($name); - )* +/// Defines a key given its name and value or range of values. +macro_rules! make_key { + ($(#[$doc: meta])* $name: ident = $key: literal..$end: literal) => { + $(#[$doc])* pub const $name: core::ops::Range = $key..$end; + }; + ($(#[$doc: meta])* $name: ident = $key: literal) => { + $(#[$doc])* pub const $name: usize = $key; }; } -define_names! { +/// Returns the range of values of a key given its value description. +#[cfg(test)] +macro_rules! make_range { + ($key: literal..$end: literal) => { + $key..$end + }; + ($key: literal) => { + $key..$key + 1 + }; +} + +/// Helper to define keys as a partial partition of a range. +macro_rules! make_partition { + ($range: expr, + $( + $(#[$doc: meta])* + $name: ident = $key: literal $(.. $end: literal)?; + )*) => { + $( + make_key!($(#[$doc])* $name = $key $(.. $end)?); + )* + #[cfg(test)] + const KEY_RANGE: core::ops::Range = $range; + #[cfg(test)] + const ALL_KEYS: &[core::ops::Range] = &[$(make_range!($key $(.. $end)?)),*]; + }; + } + +make_partition! { + // We reserve 0 and 2048+ for possible migration purposes. We add persistent entries starting + // from 1 and going up. We add non-persistent entries starting from 2047 and going down. This + // way, we don't commit to a fixed number of persistent keys. + 1..2048, + // WARNING: Keys should not be deleted but prefixed with `_` to avoid accidentally reusing them. - // the range information is not used by Xous' PDDB, but it is kept around for easy comparison against - // the original OpenSK reference code. - /// The attestation private key. - ATTESTATION_PRIVATE_KEY = 1; - - /// The attestation certificate. - ATTESTATION_CERTIFICATE = 2; + /// Reserved for the attestation store implementation of the environment. + _RESERVED_ATTESTATION_STORE = 1..3; /// The aaguid. AAGUID = 3; - // In Xous, you can have as many persistent keys as you want, but, you just have to put them - // in the FIDO_PERSISTENT_DICT. This is controlled in the implementation in the storage.rs file. - // This is the persistent key limit: // - When adding a (persistent) key above this message, make sure its value is smaller than // NUM_PERSISTENT_KEYS. @@ -61,40 +81,81 @@ define_names! { /// The credentials. /// - /// Depending on `MAX_SUPPORTED_RESIDENTIAL_KEYS`, only a prefix of those keys is used. Each - /// board may configure `MAX_SUPPORTED_RESIDENTIAL_KEYS` depending on the storage size. - // CREDENTIALS = 1700..2000; + /// Depending on `Customization::max_supported_resident_keys()`, only a prefix of those keys is used. + /// Each board may configure `Customization::max_supported_resident_keys()` depending on the + /// storage size. + CREDENTIALS = 1700..2000; + + /// Storage for the serialized large blob array. + /// + /// The stored large blob can be too big for one key, so it has to be sharded. + LARGE_BLOB_SHARDS = 2000..2004; + + /// If this entry exists and is empty, alwaysUv is enabled. + ALWAYS_UV = 2038; + + /// If this entry exists and is empty, enterprise attestation is enabled. + ENTERPRISE_ATTESTATION = 2039; + + /// If this entry exists and is empty, the PIN needs to be changed. + FORCE_PIN_CHANGE = 2040; /// The secret of the CredRandom feature. CRED_RANDOM_SECRET = 2041; /// List of RP IDs allowed to read the minimum PIN length. - #[cfg(feature = "with_ctap2_1")] - _MIN_PIN_LENGTH_RP_IDS = 2042; + MIN_PIN_LENGTH_RP_IDS = 2042; /// The minimum PIN length. /// - /// If the entry is absent, the minimum PIN length is `DEFAULT_MIN_PIN_LENGTH`. - #[cfg(feature = "with_ctap2_1")] + /// If the entry is absent, the minimum PIN length is `Customization::default_min_pin_length()`. MIN_PIN_LENGTH = 2043; /// The number of PIN retries. /// - /// If the entry is absent, the number of PIN retries is `MAX_PIN_RETRIES`. + /// If the entry is absent, the number of PIN retries is `Customization::max_pin_retries()`. PIN_RETRIES = 2044; - /// The PIN hash. + /// The PIN hash and length. /// - /// If the entry is absent, there is no PIN set. - PIN_HASH = 2045; + /// If the entry is absent, there is no PIN set. The first byte represents + /// the length, the following are an array with the hash. + PIN_PROPERTIES = 2045; - /// The encryption and hmac keys. - /// - /// This entry is always present. It is generated at startup if absent. - MASTER_KEYS = 2046; + /// Reserved for the key store implementation of the environment. + _RESERVED_KEY_STORE = 2046; /// The global signature counter. /// /// If the entry is absent, the counter is 0. GLOBAL_SIGNATURE_COUNTER = 2047; } + +#[cfg(test)] +mod test { + use super::*; + use crate::api::customization::Customization; + use crate::env::test::TestEnv; + use crate::env::Env; + + #[test] + fn enough_credentials() { + let env = TestEnv::new(); + assert!( + env.customization().max_supported_resident_keys() + <= CREDENTIALS.end - CREDENTIALS.start + ); + } + + #[test] + fn keys_are_disjoint() { + // Check that keys are in the range. + for keys in ALL_KEYS { + assert!(KEY_RANGE.start <= keys.start && keys.end <= KEY_RANGE.end); + } + // Check that keys are assigned at most once, essentially partitioning the range. + for key in KEY_RANGE { + assert!(ALL_KEYS.iter().filter(|keys| keys.contains(&key)).count() <= 1); + } + } +} diff --git a/apps/vault/src/ctap/timed_permission.rs b/apps/vault/src/ctap/timed_permission.rs index e357b9a47..933b4669f 100644 --- a/apps/vault/src/ctap/timed_permission.rs +++ b/apps/vault/src/ctap/timed_permission.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2021 Google LLC // // Licensed under the Apache License, Version 2 (the "License"); // you may not use this file except in compliance with the License. @@ -12,12 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::shims::{ClockValue, Duration}; +use std::time::Instant; +use std::time::Duration; +#[cfg(feature="xous")] +use crate::env::Env; +#[cfg(feature="xous")] +use crate::api::user_presence::UserPresence; -#[derive(Clone, Copy, Debug)] +#[derive(Debug, Copy, Clone)] pub enum TimedPermission { Waiting, - Granted(ClockValue), + Granted(Instant), } impl TimedPermission { @@ -25,32 +30,25 @@ impl TimedPermission { TimedPermission::Waiting } - pub fn granted(now: ClockValue, grant_duration: Duration) -> TimedPermission { - TimedPermission::Granted(now.wrapping_add(grant_duration)) + pub fn granted(now: Instant, grant_duration: Duration) -> TimedPermission { + // TODO: Should panic or saturate if the grant duration is too big. + TimedPermission::Granted(now.checked_add(grant_duration).unwrap()) } // Checks if the timeout is not reached, false for differing ClockValue frequencies. - #[allow(dead_code)] // Tock legacy - pub fn is_granted(&self, now: ClockValue) -> bool { + pub fn is_granted(&self, now: Instant) -> bool { if let TimedPermission::Granted(timeout) = self { - log::info!("is_granted timeout: {}, now {}", timeout.ms(), now.ms()); - if let Some(remaining_duration) = timeout.wrapping_sub(now) { - return remaining_duration > Duration::from_ms(0); - } + return timeout.checked_duration_since(now).is_some(); } false } // Consumes the state and returns the current new permission state at time "now". // Returns a new state for differing ClockValue frequencies. - #[allow(dead_code)] // Tock legacy - pub fn check_expiration(self, now: ClockValue) -> TimedPermission { + pub fn check_expiration(self, now: Instant) -> TimedPermission { if let TimedPermission::Granted(timeout) = self { - log::info!("check_expiration timeout: {}, now {}", timeout.ms(), now.ms()); - if let Some(remaining_duration) = timeout.wrapping_sub(now) { - if remaining_duration > Duration::from_ms(0) { - return TimedPermission::Granted(timeout); - } + if timeout.checked_duration_since(now).is_some() { + return TimedPermission::Granted(timeout); } } TimedPermission::Waiting @@ -60,41 +58,77 @@ impl TimedPermission { #[cfg(feature = "with_ctap1")] #[derive(Debug)] pub struct U2fUserPresenceState { + // If user presence was recently requested, its timeout is saved here. + needs_up: TimedPermission, + // Button touch timeouts, while user presence is requested, are saved here. + has_up: TimedPermission, + // This is the timeout duration of user presence requests. + request_duration: Duration, + // This is the timeout duration of button touches. + presence_duration: Duration, } #[cfg(feature = "with_ctap1")] impl U2fUserPresenceState { pub fn new( - request_duration: Duration, - presence_duration: Duration, + request_duration: Duration, + presence_duration: Duration, ) -> U2fUserPresenceState { - crate::fido::set_durations(request_duration.ms(), presence_duration.ms()); U2fUserPresenceState { + needs_up: TimedPermission::Waiting, + has_up: TimedPermission::Waiting, + request_duration, + presence_duration, } } // Granting user presence is ignored if it needs activation, but waits. Also cleans up. - #[allow(dead_code)] - pub fn grant_up(&mut self, _now: ClockValue) { - // this is a NOP because it's handled by another thread + pub fn grant_up(&mut self, _now: Instant) { + #[cfg(not(feature="xous"))] + self.check_expiration(now); + #[cfg(not(feature="xous"))] + if self.needs_up.is_granted(now) { + self.needs_up = TimedPermission::Waiting; + self.has_up = TimedPermission::granted(now, self.presence_duration); + } } // This marks user presence as needed or uses it up if already granted. Also cleans up. - pub fn consume_up(&mut self, _now: ClockValue, reason: String, application: [u8; 32]) -> bool { - crate::fido::request_permission_polling(String::from(reason), application) + #[cfg(feature="xous")] + pub fn consume_up(&mut self, env: &mut impl Env, reason: String, app_id: [u8; 32]) -> bool { + env.user_presence().wait_ctap1(String::from(reason), app_id) + } + #[cfg(not(feature="xous"))] + pub fn consume_up(&mut self, now: Instant) -> bool { + self.check_expiration(now); + if self.has_up.is_granted(now) { + self.has_up = TimedPermission::Waiting; + true + } else { + self.needs_up = TimedPermission::granted(now, self.request_duration); + false + } } // Returns if user presence was requested. Also cleans up. - #[allow(dead_code)] - pub fn is_up_needed(&mut self, _now: ClockValue) -> bool { - // this is not used by Xous + #[cfg(not(feature="xous"))] + pub fn is_up_needed(&mut self, now: Instant) -> bool { + self.check_expiration(now); + self.needs_up.is_granted(now) + } + #[cfg(feature="xous")] + pub fn is_up_needed(&mut self, _now: Instant) -> bool { false } // If you don't regularly call any other function, not cleaning up leads to overflow problems. - #[allow(dead_code)] - pub fn check_expiration(&mut self, _now: ClockValue) { - // not needed by Xous because we use an i64 for time + #[cfg(not(feature="xous"))] + pub fn check_expiration(&mut self, now: Instant) { + self.needs_up = self.needs_up.check_expiration(now); + self.has_up = self.has_up.check_expiration(now); + } + #[cfg(feature="xous")] + pub fn check_expiration(&mut self, _now: Instant) { } } @@ -102,19 +136,29 @@ impl U2fUserPresenceState { #[cfg(test)] mod test { use super::*; - use core::isize; - const CLOCK_FREQUENCY_HZ: usize = 1000; - const ZERO: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); - const BIG_POSITIVE: ClockValue = ClockValue::new(i64::MAX / 1000 - 1, CLOCK_FREQUENCY_HZ); - const NEGATIVE: ClockValue = ClockValue::new(-1, CLOCK_FREQUENCY_HZ); - const SMALL_NEGATIVE: ClockValue = ClockValue::new(i64::MIN / 1000 + 1, CLOCK_FREQUENCY_HZ); - const REQUEST_DURATION: Duration = Duration::from_ms(1000); - const PRESENCE_DURATION: Duration = Duration::from_ms(1000); + fn zero() -> Instant { + Instant::new(0) + } - /* // ux tests not valid in xous - fn grant_up_when_needed(start_time: ClockValue) { - let mut u2f_state = U2fUserPresenceState::new(REQUEST_DURATION, PRESENCE_DURATION); + fn big_positive() -> Instant { + Instant::new(u64::MAX / 1000 - 1) + } + + fn request_duration() -> Duration { + Duration::from_millis(1000) + } + + fn presence_duration() -> Duration { + Duration::from_millis(1000) + } + + fn epsilon() -> Duration { + Duration::from_millis(1) + } + #[cfg(not(feature="xous"))] + fn grant_up_when_needed(start_time: Instant) { + let mut u2f_state = U2fUserPresenceState::new(request_duration(), presence_duration()); assert!(!u2f_state.consume_up(start_time)); assert!(u2f_state.is_up_needed(start_time)); u2f_state.grant_up(start_time); @@ -122,52 +166,57 @@ mod test { assert!(!u2f_state.consume_up(start_time)); } - fn need_up_timeout(start_time: ClockValue) { - let mut u2f_state = U2fUserPresenceState::new(REQUEST_DURATION, PRESENCE_DURATION); + #[cfg(not(feature="xous"))] + fn need_up_timeout(start_time: Instant) { + let mut u2f_state = U2fUserPresenceState::new(request_duration(), presence_duration()); assert!(!u2f_state.consume_up(start_time)); assert!(u2f_state.is_up_needed(start_time)); // The timeout excludes equality, so it should be over at this instant. - assert!(!u2f_state.is_up_needed(start_time.wrapping_add(REQUEST_DURATION))); + assert!(!u2f_state.is_up_needed( + start_time + .checked_add(presence_duration() + epsilon()) + .unwrap() + )); } - fn grant_up_timeout(start_time: ClockValue) { - let mut u2f_state = U2fUserPresenceState::new(REQUEST_DURATION, PRESENCE_DURATION); + #[cfg(not(feature="xous"))] + fn grant_up_timeout(start_time: Instant) { + let mut u2f_state = U2fUserPresenceState::new(request_duration(), presence_duration()); assert!(!u2f_state.consume_up(start_time)); assert!(u2f_state.is_up_needed(start_time)); u2f_state.grant_up(start_time); // The timeout excludes equality, so it should be over at this instant. - assert!(!u2f_state.consume_up(start_time.wrapping_add(PRESENCE_DURATION))); - } */ -/* + assert!(!u2f_state.consume_up( + start_time + .checked_add(presence_duration() + epsilon()) + .unwrap() + )); + } + #[test] fn test_grant_up_timeout() { - grant_up_timeout(ZERO); - grant_up_timeout(BIG_POSITIVE); - grant_up_timeout(NEGATIVE); - grant_up_timeout(SMALL_NEGATIVE); + grant_up_timeout(zero()); + grant_up_timeout(big_positive()); } #[test] fn test_need_up_timeout() { - need_up_timeout(ZERO); - need_up_timeout(BIG_POSITIVE); - need_up_timeout(NEGATIVE); - need_up_timeout(SMALL_NEGATIVE); + need_up_timeout(zero()); + need_up_timeout(big_positive()); } #[test] fn test_grant_up_when_needed() { - grant_up_when_needed(ZERO); - grant_up_when_needed(BIG_POSITIVE); - grant_up_when_needed(NEGATIVE); - grant_up_when_needed(SMALL_NEGATIVE); + grant_up_when_needed(zero()); + grant_up_when_needed(big_positive()); } + #[cfg(not(feature="xous"))] #[test] fn test_grant_up_without_need() { - let mut u2f_state = U2fUserPresenceState::new(REQUEST_DURATION, PRESENCE_DURATION); - u2f_state.grant_up(ZERO); - assert!(!u2f_state.is_up_needed(ZERO)); - assert!(!u2f_state.consume_up(ZERO)); - }*/ + let mut u2f_state = U2fUserPresenceState::new(request_duration(), presence_duration()); + u2f_state.grant_up(zero()); + assert!(!u2f_state.is_up_needed(zero())); + assert!(!u2f_state.consume_up(zero())); + } } diff --git a/apps/vault/src/ctap/token_state.rs b/apps/vault/src/ctap/token_state.rs new file mode 100644 index 000000000..133eb57b3 --- /dev/null +++ b/apps/vault/src/ctap/token_state.rs @@ -0,0 +1,277 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::ctap::client_pin::PinPermission; +use crate::ctap::status_code::Ctap2StatusCode; +use crate::ctap::timed_permission::TimedPermission; +use std::string::String; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; +use std::time::Duration; + +use std::time::Instant; + +/// Timeout for auth tokens. +/// +/// This usage time limit is correct for USB, BLE, and internal. +/// NFC only allows 19.8 seconds. +/// TODO(#15) multiplex over transports, add NFC +const INITIAL_USAGE_TIME_LIMIT: Duration = Duration::from_millis(30000); + +/// Implements pinUvAuthToken state from section 6.5.2.1. +/// +/// The userPresent flag is omitted as the only way to set it to true is +/// built-in user verification. Therefore, we never cache user presence. +/// +/// This implementation does not use a rolling timer. +pub struct PinUvAuthTokenState { + // Relies on the fact that all permissions are represented by powers of two. + permissions_set: u8, + permissions_rp_id: Option, + usage_timer: TimedPermission, + user_verified: bool, + in_use: bool, +} + +impl PinUvAuthTokenState { + /// Creates a pinUvAuthToken state without permissions. + pub fn new() -> PinUvAuthTokenState { + PinUvAuthTokenState { + permissions_set: 0, + permissions_rp_id: None, + usage_timer: TimedPermission::waiting(), + user_verified: false, + in_use: false, + } + } + + /// Returns whether the pinUvAuthToken is active. + pub fn is_in_use(&self) -> bool { + self.in_use + } + + /// Checks if the permission is granted. + pub fn has_permission(&self, permission: PinPermission) -> Result<(), Ctap2StatusCode> { + if permission as u8 & self.permissions_set != 0 { + Ok(()) + } else { + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + } + } + + /// Checks if there is no associated permissions RPID. + pub fn has_no_permissions_rp_id(&self) -> Result<(), Ctap2StatusCode> { + if self.permissions_rp_id.is_some() { + return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); + } + Ok(()) + } + + /// Checks if the permissions RPID is associated. + pub fn has_permissions_rp_id(&self, rp_id: &str) -> Result<(), Ctap2StatusCode> { + match &self.permissions_rp_id { + Some(p) if rp_id == p => Ok(()), + _ => Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID), + } + } + + /// Checks if the permissions RPID's association matches the hash. + pub fn has_permissions_rp_id_hash(&self, rp_id_hash: &[u8]) -> Result<(), Ctap2StatusCode> { + match &self.permissions_rp_id { + Some(p) if rp_id_hash == Sha256::hash(p.as_bytes()) => Ok(()), + _ => Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID), + } + } + + /// Sets the permissions, represented as bits in a byte. + pub fn set_permissions(&mut self, permissions: u8) { + self.permissions_set = permissions; + } + + /// Sets the permissions RPID. + pub fn set_permissions_rp_id(&mut self, permissions_rp_id: Option) { + self.permissions_rp_id = permissions_rp_id; + } + + /// Sets the default permissions. + /// + /// Allows MakeCredential and GetAssertion, without specifying a RP ID. + pub fn set_default_permissions(&mut self) { + self.set_permissions(0x03); + self.set_permissions_rp_id(None); + } + + /// Starts the timer for pinUvAuthToken usage. + pub fn begin_using_pin_uv_auth_token(&mut self, now: Instant) { + self.user_verified = true; + self.usage_timer = TimedPermission::granted(now, INITIAL_USAGE_TIME_LIMIT); + self.in_use = true; + } + + /// Updates the usage timer, and disables the pinUvAuthToken on timeout. + pub fn pin_uv_auth_token_usage_timer_observer(&mut self, now: Instant) { + if !self.in_use { + return; + } + self.usage_timer = self.usage_timer.check_expiration(now); + if !self.usage_timer.is_granted(now) { + self.stop_using_pin_uv_auth_token(); + } + } + + /// Returns whether the user is verified. + pub fn get_user_verified_flag_value(&self) -> bool { + self.in_use && self.user_verified + } + + /// Consumes the user verification. + pub fn clear_user_verified_flag(&mut self) { + self.user_verified = false; + } + + /// Clears all permissions except Large Blob Write. + pub fn clear_pin_uv_auth_token_permissions_except_lbw(&mut self) { + self.permissions_set &= PinPermission::LargeBlobWrite as u8; + } + + /// Resets to the initial state. + pub fn stop_using_pin_uv_auth_token(&mut self) { + self.permissions_rp_id = None; + self.permissions_set = 0; + self.usage_timer = TimedPermission::waiting(); + self.user_verified = false; + self.in_use = false; + } +} + +#[cfg(test)] +mod test { + use super::*; + use enum_iterator::IntoEnumIterator; + + #[test] + fn test_observer() { + let mut token_state = PinUvAuthTokenState::new(); + let mut now: Instant = Instant::new(0); + token_state.begin_using_pin_uv_auth_token(now); + assert!(token_state.is_in_use()); + now = now + Duration::from_millis(100_u32); + token_state.pin_uv_auth_token_usage_timer_observer(now); + assert!(token_state.is_in_use()); + now = now + INITIAL_USAGE_TIME_LIMIT; + token_state.pin_uv_auth_token_usage_timer_observer(now); + assert!(!token_state.is_in_use()); + } + + #[test] + fn test_stop() { + let mut token_state = PinUvAuthTokenState::new(); + let now: Instant = Instant::new(0); + token_state.begin_using_pin_uv_auth_token(now); + assert!(token_state.is_in_use()); + token_state.stop_using_pin_uv_auth_token(); + assert!(!token_state.is_in_use()); + } + + #[test] + fn test_permissions() { + let mut token_state = PinUvAuthTokenState::new(); + token_state.set_permissions(0xFF); + for permission in PinPermission::into_enum_iter() { + assert_eq!(token_state.has_permission(permission), Ok(())); + } + token_state.clear_pin_uv_auth_token_permissions_except_lbw(); + assert_eq!( + token_state.has_permission(PinPermission::CredentialManagement), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + token_state.has_permission(PinPermission::LargeBlobWrite), + Ok(()) + ); + token_state.stop_using_pin_uv_auth_token(); + for permission in PinPermission::into_enum_iter() { + assert_eq!( + token_state.has_permission(permission), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + } + + #[test] + fn test_permissions_rp_id_none() { + let mut token_state = PinUvAuthTokenState::new(); + let example_hash = Sha256::hash(b"example.com"); + token_state.set_permissions_rp_id(None); + assert_eq!(token_state.has_no_permissions_rp_id(), Ok(())); + assert_eq!( + token_state.has_permissions_rp_id("example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + token_state.has_permissions_rp_id_hash(&example_hash), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_permissions_rp_id_some() { + let mut token_state = PinUvAuthTokenState::new(); + let example_hash = Sha256::hash(b"example.com"); + token_state.set_permissions_rp_id(Some(String::from("example.com"))); + + assert_eq!( + token_state.has_no_permissions_rp_id(), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!(token_state.has_permissions_rp_id("example.com"), Ok(())); + assert_eq!( + token_state.has_permissions_rp_id("another.example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + token_state.has_permissions_rp_id_hash(&example_hash), + Ok(()) + ); + assert_eq!( + token_state.has_permissions_rp_id_hash(&[0x1D; 32]), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + + token_state.stop_using_pin_uv_auth_token(); + assert_eq!( + token_state.has_permissions_rp_id("example.com"), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + assert_eq!( + token_state.has_permissions_rp_id_hash(&example_hash), + Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID) + ); + } + + #[test] + fn test_user_verified_flag() { + let mut token_state = PinUvAuthTokenState::new(); + assert!(!token_state.get_user_verified_flag_value()); + let now: Instant = Instant::new(0); + token_state.begin_using_pin_uv_auth_token(now); + assert!(token_state.get_user_verified_flag_value()); + token_state.clear_user_verified_flag(); + assert!(!token_state.get_user_verified_flag_value()); + token_state.begin_using_pin_uv_auth_token(now); + assert!(token_state.get_user_verified_flag_value()); + token_state.stop_using_pin_uv_auth_token(); + assert!(!token_state.get_user_verified_flag_value()); + } +} diff --git a/apps/vault/src/ctap/vendor_hid.rs b/apps/vault/src/ctap/vendor_hid.rs new file mode 100644 index 000000000..159cecb14 --- /dev/null +++ b/apps/vault/src/ctap/vendor_hid.rs @@ -0,0 +1,174 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::clock::Instant; +use crate::ctap::hid::{ + CtapHid, CtapHidCommand, CtapHidError, HidPacket, HidPacketIterator, Message, +}; +use crate::ctap::{Channel, CtapState}; +use crate::env::Env; + +/// Implements the non-standard command processing for HID. +/// +/// Outside of the pure HID commands like INIT, only PING and CBOR commands are allowed. +pub struct VendorHid { + hid: CtapHid, +} + +impl VendorHid { + /// Instantiates a HID handler for CTAP1, CTAP2 and Wink. + pub fn new() -> Self { + let hid = CtapHid::new(CtapHid::CAPABILITY_CBOR | CtapHid::CAPABILITY_NMSG); + VendorHid { hid } + } + + /// Processes an incoming USB HID packet, and returns an iterator for all outgoing packets. + pub fn process_hid_packet( + &mut self, + env: &mut impl Env, + packet: &HidPacket, + now: Instant, + ctap_state: &mut CtapState, + ) -> HidPacketIterator { + if let Some(message) = self.hid.parse_packet(env, packet, now) { + let processed_message = self.process_message(env, message, now, ctap_state); + debug_ctap!( + env, + "Sending message through the second usage page: {:02x?}", + processed_message + ); + CtapHid::split_message(processed_message) + } else { + HidPacketIterator::none() + } + } + + /// Processes a message's commands that affect the protocol outside HID. + pub fn process_message( + &mut self, + env: &mut impl Env, + message: Message, + now: Instant, + ctap_state: &mut CtapState, + ) -> Message { + let cid = message.cid; + match message.cmd { + // There are no custom CTAP1 commands. + CtapHidCommand::Msg => CtapHid::error_message(cid, CtapHidError::InvalidCmd), + // The CTAP2 processing function multiplexes internally. + CtapHidCommand::Cbor => { + let response = + ctap_state.process_command(env, &message.payload, Channel::VendorHid(cid), now); + Message { + cid, + cmd: CtapHidCommand::Cbor, + payload: response, + } + } + // Call Wink over the main HID. + CtapHidCommand::Wink => CtapHid::error_message(cid, CtapHidError::InvalidCmd), + // All other commands have already been processed, keep them as is. + _ => message, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::ctap::hid::ChannelID; + use crate::env::test::TestEnv; + + fn new_initialized() -> (VendorHid, ChannelID) { + let (hid, cid) = CtapHid::new_initialized(); + (VendorHid { hid }, cid) + } + + #[test] + fn test_process_hid_packet() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut vendor_hid, cid) = new_initialized(); + + let mut ping_packet = [0x00; 64]; + ping_packet[..4].copy_from_slice(&cid); + ping_packet[4..9].copy_from_slice(&[0x81, 0x00, 0x02, 0x99, 0x99]); + + let mut response = vendor_hid.process_hid_packet( + &mut env, + &ping_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), Some(ping_packet)); + assert_eq!(response.next(), None); + } + + #[test] + fn test_process_hid_packet_empty() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut vendor_hid, cid) = new_initialized(); + + let mut cancel_packet = [0x00; 64]; + cancel_packet[..4].copy_from_slice(&cid); + cancel_packet[4..7].copy_from_slice(&[0x91, 0x00, 0x00]); + + let mut response = vendor_hid.process_hid_packet( + &mut env, + &cancel_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), None); + } + + #[test] + fn test_blocked_commands() { + let mut env = TestEnv::new(); + let mut ctap_state = CtapState::new(&mut env, Instant::new(0)); + let (mut vendor_hid, cid) = new_initialized(); + + // Usually longer, but we don't parse them anyway. + let mut msg_packet = [0x00; 64]; + msg_packet[..4].copy_from_slice(&cid); + msg_packet[4..7].copy_from_slice(&[0x83, 0x00, 0x00]); + + let mut wink_packet = [0x00; 64]; + wink_packet[..4].copy_from_slice(&cid); + wink_packet[4..7].copy_from_slice(&[0x88, 0x00, 0x00]); + + let mut error_packet = [0x00; 64]; + error_packet[..4].copy_from_slice(&cid); + error_packet[4..8].copy_from_slice(&[0xBF, 0x00, 0x01, 0x01]); + + let mut response = vendor_hid.process_hid_packet( + &mut env, + &msg_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), Some(error_packet)); + assert_eq!(response.next(), None); + + let mut response = vendor_hid.process_hid_packet( + &mut env, + &wink_packet, + Instant::new(0), + &mut ctap_state, + ); + assert_eq!(response.next(), Some(error_packet)); + assert_eq!(response.next(), None); + } +} diff --git a/apps/vault/src/env/mod.rs b/apps/vault/src/env/mod.rs new file mode 100644 index 000000000..7b00c8ed8 --- /dev/null +++ b/apps/vault/src/env/mod.rs @@ -0,0 +1,75 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::api::attestation_store::AttestationStore; +use crate::api::connection::HidConnection; +use crate::api::customization::Customization; +use crate::api::firmware_protection::FirmwareProtection; +use crate::api::key_store::KeyStore; +use crate::api::upgrade_storage::UpgradeStorage; +use crate::api::user_presence::UserPresence; +use persistent_store::{Storage, Store}; +use ctap_crypto::rng256::Rng256; + +#[cfg(feature = "std")] +pub mod test; +#[cfg(feature="xous")] +pub mod xous; +#[cfg(not(feature="xous"))] +pub mod tock; + +/// Describes what CTAP needs to function. +pub trait Env { + type Rng: Rng256; + type UserPresence: UserPresence; + type Storage: Storage; + type KeyStore: KeyStore; + type UpgradeStorage: UpgradeStorage; + type FirmwareProtection: FirmwareProtection; + type Write: core::fmt::Write; + type Customization: Customization; + type HidConnection: HidConnection; + type AttestationStore: AttestationStore; + + fn rng(&mut self) -> &mut Self::Rng; + fn user_presence(&mut self) -> &mut Self::UserPresence; + fn store(&mut self) -> &mut Store; + fn key_store(&mut self) -> &mut Self::KeyStore; + fn attestation_store(&mut self) -> &mut Self::AttestationStore; + + /// Returns the upgrade storage instance. + /// + /// Upgrade storage is optional, so implementations may return `None`. However, implementations + /// should either always return `None` or always return `Some`. + fn upgrade_storage(&mut self) -> Option<&mut Self::UpgradeStorage>; + + fn firmware_protection(&mut self) -> &mut Self::FirmwareProtection; + + /// Creates a write instance for debugging. + /// + /// This API doesn't return a reference such that drop may flush. This matches the Tock + /// environment. Non-Tock embedded environments should use the defmt feature (to be implemented + /// using the defmt crate) and ignore this API. Non-embedded environments may either use this + /// API or use the log feature (to be implemented using the log crate). + fn write(&mut self) -> Self::Write; + + fn customization(&self) -> &Self::Customization; + + /// I/O connection for sending packets implementing CTAP HID protocol. + fn main_hid_connection(&mut self) -> &mut Self::HidConnection; + + /// I/O connection for sending packets implementing vendor extensions to CTAP HID protocol. + #[cfg(feature = "vendor_hid")] + fn vendor_hid_connection(&mut self) -> &mut Self::HidConnection; +} diff --git a/apps/vault/src/env/test/customization.rs b/apps/vault/src/env/test/customization.rs new file mode 100644 index 000000000..0b7a64fda --- /dev/null +++ b/apps/vault/src/env/test/customization.rs @@ -0,0 +1,188 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::api::customization::{Customization, CustomizationImpl}; +use crate::ctap::data_formats::{CredentialProtectionPolicy, EnterpriseAttestationMode}; +use std::string::String; +use std::vec::Vec; + +pub struct TestCustomization { + allows_pin_protocol_v1: bool, + default_cred_protect: Option, + default_min_pin_length: u8, + default_min_pin_length_rp_ids: Vec, + enforce_always_uv: bool, + enterprise_attestation_mode: Option, + enterprise_rp_id_list: Vec, + max_msg_size: usize, + max_pin_retries: u8, + use_batch_attestation: bool, + use_signature_counter: bool, + max_cred_blob_length: usize, + max_credential_count_in_list: Option, + max_large_blob_array_size: usize, + max_rp_ids_length: usize, + max_supported_resident_keys: usize, +} + +impl TestCustomization { + pub fn set_allows_pin_protocol_v1(&mut self, is_allowed: bool) { + self.allows_pin_protocol_v1 = is_allowed; + } + + pub fn setup_enterprise_attestation( + &mut self, + mode: Option, + rp_id_list: Option>, + ) { + self.enterprise_attestation_mode = mode; + if let Some(rp_id_list) = rp_id_list { + self.enterprise_rp_id_list = rp_id_list; + } + } +} + +impl Customization for TestCustomization { + fn allows_pin_protocol_v1(&self) -> bool { + self.allows_pin_protocol_v1 + } + + fn default_cred_protect(&self) -> Option { + self.default_cred_protect + } + + fn default_min_pin_length(&self) -> u8 { + self.default_min_pin_length + } + + fn default_min_pin_length_rp_ids(&self) -> Vec { + self.default_min_pin_length_rp_ids.clone() + } + + fn enforce_always_uv(&self) -> bool { + self.enforce_always_uv + } + + fn enterprise_attestation_mode(&self) -> Option { + self.enterprise_attestation_mode + } + + fn enterprise_rp_id_list(&self) -> Vec { + self.enterprise_rp_id_list.clone() + } + + fn is_enterprise_rp_id(&self, rp_id: &str) -> bool { + self.enterprise_rp_id_list.iter().any(|id| id == rp_id) + } + + fn max_msg_size(&self) -> usize { + self.max_msg_size + } + + fn max_pin_retries(&self) -> u8 { + self.max_pin_retries + } + + fn use_batch_attestation(&self) -> bool { + self.use_batch_attestation + } + + fn use_signature_counter(&self) -> bool { + self.use_signature_counter + } + + fn max_cred_blob_length(&self) -> usize { + self.max_cred_blob_length + } + + fn max_credential_count_in_list(&self) -> Option { + self.max_credential_count_in_list + } + + fn max_large_blob_array_size(&self) -> usize { + self.max_large_blob_array_size + } + + fn max_rp_ids_length(&self) -> usize { + self.max_rp_ids_length + } + + fn max_supported_resident_keys(&self) -> usize { + self.max_supported_resident_keys + } +} + +impl From for TestCustomization { + fn from(c: CustomizationImpl) -> Self { + let CustomizationImpl { + allows_pin_protocol_v1, + default_cred_protect, + default_min_pin_length, + default_min_pin_length_rp_ids, + enforce_always_uv, + enterprise_attestation_mode, + enterprise_rp_id_list, + max_msg_size, + max_pin_retries, + use_batch_attestation, + use_signature_counter, + max_cred_blob_length, + max_credential_count_in_list, + max_large_blob_array_size, + max_rp_ids_length, + max_supported_resident_keys, + } = c; + + let default_min_pin_length_rp_ids = default_min_pin_length_rp_ids + .iter() + .map(|s| String::from(*s)) + .collect::>(); + + let enterprise_rp_id_list = enterprise_rp_id_list + .iter() + .map(|s| String::from(*s)) + .collect::>(); + + Self { + allows_pin_protocol_v1, + default_cred_protect, + default_min_pin_length, + default_min_pin_length_rp_ids, + enforce_always_uv, + enterprise_attestation_mode, + enterprise_rp_id_list, + max_msg_size, + max_pin_retries, + use_batch_attestation, + use_signature_counter, + max_cred_blob_length, + max_credential_count_in_list, + max_large_blob_array_size, + max_rp_ids_length, + max_supported_resident_keys, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::api::customization::{is_valid, DEFAULT_CUSTOMIZATION}; + + #[test] + fn test_invariants() { + let customization = TestCustomization::from(DEFAULT_CUSTOMIZATION.clone()); + assert!(is_valid(&customization)); + } +} diff --git a/apps/vault/src/env/test/mod.rs b/apps/vault/src/env/test/mod.rs new file mode 100644 index 000000000..50d918471 --- /dev/null +++ b/apps/vault/src/env/test/mod.rs @@ -0,0 +1,219 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use self::upgrade_storage::BufferUpgradeStorage; +use crate::api::attestation_store::AttestationStore; +use crate::api::connection::{HidConnection, SendOrRecvResult, SendOrRecvStatus}; +use crate::api::customization::DEFAULT_CUSTOMIZATION; +use crate::api::firmware_protection::FirmwareProtection; +use crate::api::user_presence::{UserPresence, UserPresenceResult}; +use crate::api::{attestation_store, key_store}; +use crate::clock::ClockInt; +use crate::env::Env; +use customization::TestCustomization; +use std::time::Duration; +use persistent_store::{BufferOptions, BufferStorage, Store}; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; +use ctap_crypto::rng256::Rng256; + +pub mod customization; +mod upgrade_storage; + +pub struct TestEnv { + rng: TestRng256, + user_presence: TestUserPresence, + store: Store, + upgrade_storage: Option, + customization: TestCustomization, +} + +pub struct TestRng256 { + rng: StdRng, +} + +impl TestRng256 { + pub fn seed_from_u64(&mut self, state: u64) { + self.rng = StdRng::seed_from_u64(state); + } +} + +impl Rng256 for TestRng256 { + fn gen_uniform_u8x32(&mut self) -> [u8; 32] { + let mut result = [Default::default(); 32]; + self.rng.fill(&mut result); + result + } +} + +pub struct TestUserPresence { + check: Box UserPresenceResult>, +} + +pub struct TestWrite; + +impl core::fmt::Write for TestWrite { + fn write_str(&mut self, _: &str) -> core::fmt::Result { + Ok(()) + } +} + +fn new_storage() -> BufferStorage { + // Use the Nordic configuration. + const PAGE_SIZE: usize = 0x1000; + const NUM_PAGES: usize = 20; + let store = vec![0xff; NUM_PAGES * PAGE_SIZE].into_boxed_slice(); + let options = BufferOptions { + word_size: 4, + page_size: PAGE_SIZE, + max_word_writes: 2, + max_page_erases: 10000, + strict_mode: true, + }; + BufferStorage::new(store, options) +} + +impl HidConnection for TestEnv { + fn send_and_maybe_recv( + &mut self, + _buf: &mut [u8; 64], + _timeout: Duration, + ) -> SendOrRecvResult { + // TODO: Implement I/O from canned requests/responses for integration testing. + Ok(SendOrRecvStatus::Sent) + } +} + +impl TestEnv { + pub fn new() -> Self { + let rng = TestRng256 { + rng: StdRng::seed_from_u64(0), + }; + let user_presence = TestUserPresence { + check: Box::new(|| Ok(())), + }; + let storage = new_storage(); + let store = Store::new(storage).ok().unwrap(); + let upgrade_storage = Some(BufferUpgradeStorage::new().unwrap()); + let customization = DEFAULT_CUSTOMIZATION.into(); + TestEnv { + rng, + user_presence, + store, + upgrade_storage, + customization, + } + } + + pub fn disable_upgrade_storage(&mut self) { + self.upgrade_storage = None; + } + + pub fn customization_mut(&mut self) -> &mut TestCustomization { + &mut self.customization + } + + pub fn rng(&mut self) -> &mut TestRng256 { + &mut self.rng + } +} + +impl TestUserPresence { + pub fn set(&mut self, check: impl Fn() -> UserPresenceResult + 'static) { + self.check = Box::new(check); + } +} + +impl FirmwareProtection for TestEnv { + fn lock(&mut self) -> bool { + true + } +} + +impl key_store::Helper for TestEnv {} + +impl AttestationStore for TestEnv { + fn get( + &mut self, + _id: &attestation_store::Id, + ) -> Result, attestation_store::Error> { + attestation_store::helper_get(self) + } + + fn set( + &mut self, + _id: &attestation_store::Id, + attestation: Option<&attestation_store::Attestation>, + ) -> Result<(), attestation_store::Error> { + attestation_store::helper_set(self, attestation) + } +} + +impl Env for TestEnv { + type Rng = TestRng256; + type UserPresence = TestUserPresence; + type Storage = BufferStorage; + type KeyStore = Self; + type AttestationStore = Self; + type UpgradeStorage = BufferUpgradeStorage; + type FirmwareProtection = Self; + type Write = TestWrite; + type Customization = TestCustomization; + type HidConnection = Self; + + fn rng(&mut self) -> &mut Self::Rng { + &mut self.rng + } + + fn user_presence(&mut self) -> &mut Self::UserPresence { + &mut self.user_presence + } + + fn store(&mut self) -> &mut Store { + &mut self.store + } + + fn key_store(&mut self) -> &mut Self { + self + } + + fn attestation_store(&mut self) -> &mut Self { + self + } + + fn upgrade_storage(&mut self) -> Option<&mut Self::UpgradeStorage> { + self.upgrade_storage.as_mut() + } + + fn firmware_protection(&mut self) -> &mut Self::FirmwareProtection { + self + } + + fn write(&mut self) -> Self::Write { + TestWrite + } + + fn customization(&self) -> &Self::Customization { + &self.customization + } + + fn main_hid_connection(&mut self) -> &mut Self::HidConnection { + self + } + + #[cfg(feature = "vendor_hid")] + fn vendor_hid_connection(&mut self) -> &mut Self::HidConnection { + self + } +} diff --git a/apps/vault/src/env/test/upgrade_storage.rs b/apps/vault/src/env/test/upgrade_storage.rs new file mode 100644 index 000000000..b935f3254 --- /dev/null +++ b/apps/vault/src/env/test/upgrade_storage.rs @@ -0,0 +1,117 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::api::upgrade_storage::helper::ModRange; +use crate::api::upgrade_storage::UpgradeStorage; +use std::boxed::Box; +use persistent_store::{StorageError, StorageResult}; + +const PARTITION_LENGTH: usize = 0x41000; +const METADATA_LENGTH: usize = 0x1000; + +pub struct BufferUpgradeStorage { + /// Content of the partition storage. + partition: Box<[u8]>, +} + +impl BufferUpgradeStorage { + pub fn new() -> StorageResult { + Ok(BufferUpgradeStorage { + partition: vec![0xff; PARTITION_LENGTH].into_boxed_slice(), + }) + } + + #[cfg(test)] + fn read_partition(&self, offset: usize, length: usize) -> StorageResult<&[u8]> { + if length == 0 { + return Err(StorageError::OutOfBounds); + } + let partition_range = ModRange::new(0, self.partition.len()); + if partition_range.contains_range(&ModRange::new(offset, length)) { + Ok(&self.partition[offset..][..length]) + } else { + Err(StorageError::OutOfBounds) + } + } +} + +impl UpgradeStorage for BufferUpgradeStorage { + fn write_bundle(&mut self, offset: usize, data: Vec) -> StorageResult<()> { + if offset == 0 && data.len() != METADATA_LENGTH { + return Err(StorageError::OutOfBounds); + } + if data.is_empty() { + return Err(StorageError::OutOfBounds); + } + let partition_range = ModRange::new(0, self.partition.len()); + if partition_range.contains_range(&ModRange::new(offset, data.len())) { + self.partition[offset..][..data.len()].copy_from_slice(&data); + Ok(()) + } else { + Err(StorageError::OutOfBounds) + } + } + + fn bundle_identifier(&self) -> u32 { + 0x60000 + } + + fn running_firmware_version(&self) -> u64 { + 0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn read_write_bundle() { + let mut storage = BufferUpgradeStorage::new().unwrap(); + assert_eq!(storage.read_partition(0, 2).unwrap(), &[0xFF, 0xFF]); + assert!(storage.write_bundle(1, vec![0x88, 0x88]).is_ok()); + assert_eq!(storage.read_partition(0, 2).unwrap(), &[0xFF, 0x88]); + assert_eq!( + storage.write_bundle(PARTITION_LENGTH - 1, vec![0x88, 0x88],), + Err(StorageError::OutOfBounds) + ); + assert_eq!( + storage.read_partition(PARTITION_LENGTH - 2, 2).unwrap(), + &[0xFF, 0xFF] + ); + assert_eq!( + storage.read_partition(PARTITION_LENGTH - 1, 2), + Err(StorageError::OutOfBounds) + ); + assert_eq!( + storage.write_bundle(4, vec![]), + Err(StorageError::OutOfBounds) + ); + assert_eq!( + storage.write_bundle(PARTITION_LENGTH + 4, vec![]), + Err(StorageError::OutOfBounds) + ); + assert_eq!(storage.read_partition(4, 0), Err(StorageError::OutOfBounds)); + assert_eq!( + storage.read_partition(PARTITION_LENGTH + 4, 0), + Err(StorageError::OutOfBounds) + ); + } + + #[test] + fn partition_slice() { + let storage = BufferUpgradeStorage::new().unwrap(); + assert_eq!(storage.bundle_identifier(), 0x60000); + } +} diff --git a/apps/vault/src/env/tock/mod.rs b/apps/vault/src/env/tock/mod.rs new file mode 100644 index 000000000..193369568 --- /dev/null +++ b/apps/vault/src/env/tock/mod.rs @@ -0,0 +1,328 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub use self::storage::{TockStorage, TockUpgradeStorage}; +use crate::api::attestation_store::AttestationStore; +use crate::api::connection::{HidConnection, SendOrRecvError, SendOrRecvResult, SendOrRecvStatus}; +use crate::api::customization::{CustomizationImpl, DEFAULT_CUSTOMIZATION}; +use crate::api::firmware_protection::FirmwareProtection; +use crate::api::user_presence::{UserPresence, UserPresenceError, UserPresenceResult}; +use crate::api::{attestation_store, key_store}; +use crate::clock::{ClockInt, KEEPALIVE_DELAY_MS}; +use crate::env::Env; +use core::cell::Cell; +use core::sync::atomic::{AtomicBool, Ordering}; +use std::time::duration::Milliseconds; +use std::time::fixed_point::FixedPoint; +use libtock_core::result::{CommandError, EALREADY}; +use libtock_drivers::buttons::{self, ButtonState}; +use libtock_drivers::console::Console; +use libtock_drivers::result::{FlexUnwrap, TockError}; +use libtock_drivers::timer::Duration; +use libtock_drivers::usb_ctap_hid::{self, UsbEndpoint}; +use libtock_drivers::{crp, led, timer}; +use persistent_store::{StorageResult, Store}; +use ctap_crypto::rng256::TockRng256; + +mod storage; + +pub struct TockHidConnection { + endpoint: UsbEndpoint, +} + +impl HidConnection for TockHidConnection { + fn send_and_maybe_recv( + &mut self, + buf: &mut [u8; 64], + timeout: Milliseconds, + ) -> SendOrRecvResult { + match usb_ctap_hid::send_or_recv_with_timeout( + buf, + timer::Duration::from_ms(timeout.integer() as isize), + self.endpoint, + ) { + Ok(usb_ctap_hid::SendOrRecvStatus::Timeout) => Ok(SendOrRecvStatus::Timeout), + Ok(usb_ctap_hid::SendOrRecvStatus::Sent) => Ok(SendOrRecvStatus::Sent), + Ok(usb_ctap_hid::SendOrRecvStatus::Received(recv_endpoint)) => { + Ok(SendOrRecvStatus::Received(recv_endpoint)) + } + _ => Err(SendOrRecvError), + } + } +} + +pub struct TockEnv { + rng: TockRng256, + store: Store, + upgrade_storage: Option, + main_connection: TockHidConnection, + #[cfg(feature = "vendor_hid")] + vendor_connection: TockHidConnection, + blink_pattern: usize, +} + +impl TockEnv { + /// Returns the unique instance of the Tock environment. + /// + /// # Panics + /// + /// - If called a second time. + pub fn new() -> Self { + // We rely on `take_storage` to ensure that this function is called only once. + let storage = take_storage().unwrap(); + let store = Store::new(storage).ok().unwrap(); + let upgrade_storage = TockUpgradeStorage::new().ok(); + TockEnv { + rng: TockRng256 {}, + store, + upgrade_storage, + main_connection: TockHidConnection { + endpoint: UsbEndpoint::MainHid, + }, + #[cfg(feature = "vendor_hid")] + vendor_connection: TockHidConnection { + endpoint: UsbEndpoint::VendorHid, + }, + blink_pattern: 0, + } + } +} + +/// Returns the unique storage instance. +/// +/// # Panics +/// +/// - If called a second time. +pub fn take_storage() -> StorageResult { + // Make sure the storage was not already taken. + static TAKEN: AtomicBool = AtomicBool::new(false); + assert!(!TAKEN.fetch_or(true, Ordering::SeqCst)); + TockStorage::new() +} + +impl UserPresence for TockEnv { + fn check_init(&mut self) { + self.blink_pattern = 0; + } + fn wait_with_timeout(&mut self, timeout: Milliseconds) -> UserPresenceResult { + if timeout.integer() == 0 { + return Err(UserPresenceError::Timeout); + } + blink_leds(self.blink_pattern); + self.blink_pattern += 1; + + let button_touched = Cell::new(false); + let mut buttons_callback = buttons::with_callback(|_button_num, state| { + match state { + ButtonState::Pressed => button_touched.set(true), + ButtonState::Released => (), + }; + }); + let mut buttons = buttons_callback.init().flex_unwrap(); + for mut button in &mut buttons { + button.enable().flex_unwrap(); + } + + // Setup a keep-alive callback. + let keepalive_expired = Cell::new(false); + let mut keepalive_callback = timer::with_callback(|_, _| { + keepalive_expired.set(true); + }); + let mut keepalive = keepalive_callback.init().flex_unwrap(); + let keepalive_alarm = keepalive + .set_alarm(timer::Duration::from_ms(timeout.integer() as isize)) + .flex_unwrap(); + + // Wait for a button touch or an alarm. + libtock_drivers::util::yieldk_for(|| button_touched.get() || keepalive_expired.get()); + + // Cleanup alarm callback. + match keepalive.stop_alarm(keepalive_alarm) { + Ok(()) => (), + Err(TockError::Command(CommandError { + return_code: EALREADY, + .. + })) => assert!(keepalive_expired.get()), + Err(_e) => { + #[cfg(feature = "debug_ctap")] + panic!("Unexpected error when stopping alarm: {:?}", _e); + #[cfg(not(feature = "debug_ctap"))] + panic!("Unexpected error when stopping alarm: "); + } + } + + for mut button in &mut buttons { + button.disable().flex_unwrap(); + } + + if button_touched.get() { + Ok(()) + } else if keepalive_expired.get() { + Err(UserPresenceError::Timeout) + } else { + panic!("Unexpected exit condition"); + } + } + + fn check_complete(&mut self) { + switch_off_leds(); + } +} + +impl FirmwareProtection for TockEnv { + fn lock(&mut self) -> bool { + matches!( + crp::set_protection(crp::ProtectionLevel::FullyLocked), + Ok(()) + | Err(TockError::Command(CommandError { + return_code: EALREADY, + .. + })) + ) + } +} + +impl key_store::Helper for TockEnv {} + +impl AttestationStore for TockEnv { + fn get( + &mut self, + id: &attestation_store::Id, + ) -> Result, attestation_store::Error> { + if !matches!(id, attestation_store::Id::Batch) { + return Err(attestation_store::Error::NoSupport); + } + attestation_store::helper_get(self) + } + + fn set( + &mut self, + id: &attestation_store::Id, + attestation: Option<&attestation_store::Attestation>, + ) -> Result<(), attestation_store::Error> { + if !matches!(id, attestation_store::Id::Batch) { + return Err(attestation_store::Error::NoSupport); + } + attestation_store::helper_set(self, attestation) + } +} + +impl Env for TockEnv { + type Rng = TockRng256; + type UserPresence = Self; + type Storage = TockStorage; + type KeyStore = Self; + type AttestationStore = Self; + type UpgradeStorage = TockUpgradeStorage; + type FirmwareProtection = Self; + type Write = Console; + type Customization = CustomizationImpl; + type HidConnection = TockHidConnection; + + fn rng(&mut self) -> &mut Self::Rng { + &mut self.rng + } + + fn user_presence(&mut self) -> &mut Self::UserPresence { + self + } + + fn store(&mut self) -> &mut Store { + &mut self.store + } + + fn key_store(&mut self) -> &mut Self { + self + } + + fn attestation_store(&mut self) -> &mut Self { + self + } + + fn upgrade_storage(&mut self) -> Option<&mut Self::UpgradeStorage> { + self.upgrade_storage.as_mut() + } + + fn firmware_protection(&mut self) -> &mut Self::FirmwareProtection { + self + } + + fn write(&mut self) -> Self::Write { + Console::new() + } + + fn customization(&self) -> &Self::Customization { + &DEFAULT_CUSTOMIZATION + } + + fn main_hid_connection(&mut self) -> &mut Self::HidConnection { + &mut self.main_connection + } + + #[cfg(feature = "vendor_hid")] + fn vendor_hid_connection(&mut self) -> &mut Self::HidConnection { + &mut self.vendor_connection + } +} + +pub fn blink_leds(pattern_seed: usize) { + for l in 0..led::count().flex_unwrap() { + if (pattern_seed ^ l).count_ones() & 1 != 0 { + led::get(l).flex_unwrap().on().flex_unwrap(); + } else { + led::get(l).flex_unwrap().off().flex_unwrap(); + } + } +} + +pub fn wink_leds(pattern_seed: usize) { + // This generates a "snake" pattern circling through the LEDs. + // Fox example with 4 LEDs the sequence of lit LEDs will be the following. + // 0 1 2 3 + // * * + // * * * + // * * + // * * * + // * * + // * * * + // * * + // * * * + // * * + let count = led::count().flex_unwrap(); + let a = (pattern_seed / 2) % count; + let b = ((pattern_seed + 1) / 2) % count; + let c = ((pattern_seed + 3) / 2) % count; + + for l in 0..count { + // On nRF52840-DK, logically swap LEDs 3 and 4 so that the order of LEDs form a circle. + let k = match l { + 2 => 3, + 3 => 2, + _ => l, + }; + if k == a || k == b || k == c { + led::get(l).flex_unwrap().on().flex_unwrap(); + } else { + led::get(l).flex_unwrap().off().flex_unwrap(); + } + } +} + +pub fn switch_off_leds() { + for l in 0..led::count().flex_unwrap() { + led::get(l).flex_unwrap().off().flex_unwrap(); + } +} + +pub const KEEPALIVE_DELAY_TOCK: Duration = Duration::from_ms(KEEPALIVE_DELAY_MS as isize); diff --git a/apps/vault/src/env/tock/storage.rs b/apps/vault/src/env/tock/storage.rs new file mode 100644 index 000000000..b5e6227c0 --- /dev/null +++ b/apps/vault/src/env/tock/storage.rs @@ -0,0 +1,572 @@ +// Copyright 2019-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::api::upgrade_storage::helper::{find_slice, is_aligned, ModRange, Partition}; +use crate::api::upgrade_storage::UpgradeStorage; +use std::borrow::Cow; +use std::vec::Vec; +use arrayref::array_ref; +use byteorder::{ByteOrder, LittleEndian}; +use core::cell::Cell; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::{ecdsa, Hash256}; +use libtock_core::{callback, syscalls}; +use persistent_store::{Storage, StorageError, StorageIndex, StorageResult}; + +const DRIVER_NUMBER: usize = 0x50003; +const METADATA_SIGN_OFFSET: usize = 0x800; + +const UPGRADE_PUBLIC_KEY: &[u8; 65] = + include_bytes!(concat!(env!("OUT_DIR"), "/opensk_upgrade_pubkey.bin")); + +mod subscribe_nr { + pub const DONE: usize = 0; +} + +mod command_nr { + pub const GET_INFO: usize = 1; + pub mod get_info_nr { + pub const WORD_SIZE: usize = 0; + pub const PAGE_SIZE: usize = 1; + pub const MAX_WORD_WRITES: usize = 2; + pub const MAX_PAGE_ERASES: usize = 3; + } + pub const WRITE_SLICE: usize = 2; + pub const ERASE_PAGE: usize = 3; +} + +mod allow_nr { + pub const WRITE_SLICE: usize = 0; +} + +mod memop_nr { + pub const STORAGE_CNT: u32 = 12; + pub const STORAGE_PTR: u32 = 13; + pub const STORAGE_LEN: u32 = 14; + pub const STORAGE_TYPE: u32 = 15; +} + +mod storage_type { + pub const STORE: usize = 1; + pub const PARTITION: usize = 2; +} + +fn get_info(nr: usize, arg: usize) -> StorageResult { + let code = syscalls::command(DRIVER_NUMBER, command_nr::GET_INFO, nr, arg); + code.map_err(|_| StorageError::CustomError) +} + +fn memop(nr: u32, arg: usize) -> StorageResult { + let code = unsafe { syscalls::raw::memop(nr, arg) }; + if code < 0 { + Err(StorageError::CustomError) + } else { + Ok(code as usize) + } +} + +fn block_command(driver: usize, cmd: usize, arg1: usize, arg2: usize) -> StorageResult<()> { + let done = Cell::new(None); + let mut alarm = |status| done.set(Some(status)); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::DONE, + &mut alarm, + ); + if subscription.is_err() { + return Err(StorageError::CustomError); + } + + let code = syscalls::command(driver, cmd, arg1, arg2); + if code.is_err() { + return Err(StorageError::CustomError); + } + + libtock_drivers::util::yieldk_for(|| done.get().is_some()); + if done.get().unwrap() == 0 { + Ok(()) + } else { + Err(StorageError::CustomError) + } +} + +unsafe fn read_slice(address: usize, length: usize) -> &'static [u8] { + core::slice::from_raw_parts(address as *const u8, length) +} + +fn write_slice(ptr: usize, value: &[u8]) -> StorageResult<()> { + let code = unsafe { + syscalls::raw::allow( + DRIVER_NUMBER, + allow_nr::WRITE_SLICE, + // We rely on the driver not writing to the slice. This should use read-only allow + // when available. See https://github.com/tock/tock/issues/1274. + value.as_ptr() as *mut u8, + value.len(), + ) + }; + if code < 0 { + return Err(StorageError::CustomError); + } + + block_command(DRIVER_NUMBER, command_nr::WRITE_SLICE, ptr, value.len()) +} + +fn erase_page(ptr: usize, page_length: usize) -> StorageResult<()> { + block_command(DRIVER_NUMBER, command_nr::ERASE_PAGE, ptr, page_length) +} + +pub struct TockStorage { + word_size: usize, + page_size: usize, + num_pages: usize, + max_word_writes: usize, + max_page_erases: usize, + storage_locations: Vec<&'static [u8]>, +} + +impl TockStorage { + /// Provides access to the embedded flash if available. + /// + /// # Errors + /// + /// Returns `CustomError` if any of the following conditions do not hold: + /// - The word size is a power of two. + /// - The page size is a power of two. + /// - The page size is a multiple of the word size. + /// - The storage is page-aligned. + pub fn new() -> StorageResult { + let mut syscall = TockStorage { + word_size: get_info(command_nr::get_info_nr::WORD_SIZE, 0)?, + page_size: get_info(command_nr::get_info_nr::PAGE_SIZE, 0)?, + num_pages: 0, + max_word_writes: get_info(command_nr::get_info_nr::MAX_WORD_WRITES, 0)?, + max_page_erases: get_info(command_nr::get_info_nr::MAX_PAGE_ERASES, 0)?, + storage_locations: Vec::new(), + }; + if !syscall.word_size.is_power_of_two() + || !syscall.page_size.is_power_of_two() + || !syscall.is_word_aligned(syscall.page_size) + { + return Err(StorageError::CustomError); + } + for i in 0..memop(memop_nr::STORAGE_CNT, 0)? { + if memop(memop_nr::STORAGE_TYPE, i)? != storage_type::STORE { + continue; + } + let storage_ptr = memop(memop_nr::STORAGE_PTR, i)?; + let storage_len = memop(memop_nr::STORAGE_LEN, i)?; + if !syscall.is_page_aligned(storage_ptr) || !syscall.is_page_aligned(storage_len) { + return Err(StorageError::CustomError); + } + syscall.num_pages += storage_len / syscall.page_size; + syscall + .storage_locations + .push(unsafe { core::slice::from_raw_parts(storage_ptr as *mut u8, storage_len) }); + } + Ok(syscall) + } + + fn is_word_aligned(&self, x: usize) -> bool { + is_aligned(self.word_size, x) + } + + fn is_page_aligned(&self, x: usize) -> bool { + is_aligned(self.page_size, x) + } +} + +impl Storage for TockStorage { + fn word_size(&self) -> usize { + self.word_size + } + + fn page_size(&self) -> usize { + self.page_size + } + + fn num_pages(&self) -> usize { + self.num_pages + } + + fn max_word_writes(&self) -> usize { + self.max_word_writes + } + + fn max_page_erases(&self) -> usize { + self.max_page_erases + } + + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult> { + let start = index.range(length, self)?.start; + find_slice(&self.storage_locations, start, length).map(Cow::Borrowed) + } + + fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()> { + if !self.is_word_aligned(index.byte) || !self.is_word_aligned(value.len()) { + return Err(StorageError::NotAligned); + } + let ptr = self.read_slice(index, value.len())?.as_ptr() as usize; + write_slice(ptr, value) + } + + fn erase_page(&mut self, page: usize) -> StorageResult<()> { + let index = StorageIndex { page, byte: 0 }; + let length = self.page_size(); + let ptr = self.read_slice(index, length)?.as_ptr() as usize; + erase_page(ptr, length) + } +} + +pub struct TockUpgradeStorage { + page_size: usize, + partition: Partition, + metadata: ModRange, + running_metadata: ModRange, + identifier: u32, +} + +impl TockUpgradeStorage { + // Ideally, the kernel should tell us metadata and partitions directly. + // This code only works for one layout, refactor this into the storage driver to support more. + const METADATA_ADDRESS: usize = 0x4000; + const PARTITION_ADDRESS_A: usize = 0x20000; + const PARTITION_ADDRESS_B: usize = 0x60000; + + /// Provides access to the other upgrade partition and metadata if available. + /// + /// The implementation assumes that storage locations returned by the kernel through + /// `memop_nr::STORAGE_*` calls are in address space order. + /// + /// # Errors + /// + /// Returns `CustomError` if any of the following conditions do not hold: + /// - The page size is a power of two. + /// - The storage slices are page-aligned. + /// - There are no partition or no metadata slices. + /// Returns a `NotAligned` error if partitions or metadata ranges are + /// - not exclusive or, + /// - not consecutive. + pub fn new() -> StorageResult { + let mut locations = TockUpgradeStorage { + page_size: get_info(command_nr::get_info_nr::PAGE_SIZE, 0)?, + partition: Partition::new(), + metadata: ModRange::new_empty(), + running_metadata: ModRange::new_empty(), + identifier: Self::PARTITION_ADDRESS_A as u32, + }; + if !locations.page_size.is_power_of_two() { + return Err(StorageError::CustomError); + } + let mut firmware_range = ModRange::new_empty(); + for i in 0..memop(memop_nr::STORAGE_CNT, 0)? { + let storage_type = memop(memop_nr::STORAGE_TYPE, i)?; + if !matches!(storage_type, storage_type::PARTITION) { + continue; + }; + let storage_ptr = memop(memop_nr::STORAGE_PTR, i)?; + let storage_len = memop(memop_nr::STORAGE_LEN, i)?; + if !locations.is_page_aligned(storage_ptr) || !locations.is_page_aligned(storage_len) { + return Err(StorageError::CustomError); + } + let range = ModRange::new(storage_ptr, storage_len); + match range.start() { + Self::METADATA_ADDRESS => { + // Will be swapped if we are on B. + locations.metadata = ModRange::new(range.start(), locations.page_size); + locations.running_metadata = + ModRange::new(range.start() + locations.page_size, locations.page_size); + } + _ => { + if !firmware_range.append(&range) { + return Err(StorageError::NotAligned); + } + } + } + } + if firmware_range.is_empty() + || locations.metadata.is_empty() + || locations.running_metadata.is_empty() + { + return Err(StorageError::CustomError); + } + if firmware_range.start() == Self::PARTITION_ADDRESS_B { + core::mem::swap(&mut locations.metadata, &mut locations.running_metadata); + locations.identifier = Self::PARTITION_ADDRESS_B as u32; + } + if !locations.partition.append(locations.metadata.clone()) { + return Err(StorageError::NotAligned); + } + if !locations.partition.append(firmware_range) { + return Err(StorageError::NotAligned); + } + Ok(locations) + } + + fn is_page_aligned(&self, x: usize) -> bool { + is_aligned(self.page_size, x) + } + + /// Returns whether the metadata is contained in this range or not. + /// + /// Assumes that metadata is written in one call per range. If the metadata is only partially + /// contained, returns an error. + fn contains_metadata(&self, checked_range: &ModRange) -> StorageResult { + if checked_range.intersects_range(&self.metadata) { + if checked_range.contains_range(&self.metadata) { + Ok(true) + } else { + Err(StorageError::NotAligned) + } + } else { + Ok(false) + } + } + + /// Checks if the metadata's hash matches the partition's content. + fn check_partition_hash(&self, metadata: &[u8]) -> StorageResult<()> { + let start_address = self.metadata.start() + METADATA_SIGN_OFFSET; + let mut hasher = Sha256::new(); + for range in self.partition.ranges_from(start_address) { + let partition_slice = unsafe { read_slice(range.start(), range.length()) }; + // The hash implementation handles this in chunks, so no memory issues. + hasher.update(partition_slice); + } + let computed_hash = hasher.finalize(); + if &computed_hash != parse_metadata_hash(metadata) { + return Err(StorageError::CustomError); + } + Ok(()) + } +} + +impl UpgradeStorage for TockUpgradeStorage { + fn write_bundle(&mut self, offset: usize, data: Vec) -> StorageResult<()> { + if data.is_empty() { + return Err(StorageError::OutOfBounds); + } + let address = self + .partition + .find_address(offset, data.len()) + .ok_or(StorageError::OutOfBounds)?; + let write_range = ModRange::new(address, data.len()); + if self.contains_metadata(&write_range)? { + let new_metadata = &data[self.metadata.start() - address..][..self.metadata.length()]; + check_metadata(self, UPGRADE_PUBLIC_KEY, new_metadata)?; + } + + // Erases all pages that have their first byte in the write range. + // Since we expect calls in order, we don't want to erase half-written pages. + for address in write_range.aligned_iter(self.page_size) { + erase_page(address, self.page_size)?; + } + write_slice(address, &data)?; + let written_slice = unsafe { read_slice(address, data.len()) }; + if written_slice != data { + return Err(StorageError::CustomError); + } + // Case: Last slice is written. + if data.len() == self.partition.length() - offset { + let metadata = unsafe { read_slice(self.metadata.start(), self.metadata.length()) }; + self.check_partition_hash(&metadata)?; + } + Ok(()) + } + + fn bundle_identifier(&self) -> u32 { + self.identifier + } + + fn running_firmware_version(&self) -> u64 { + let running_metadata = unsafe { + read_slice( + self.running_metadata.start(), + self.running_metadata.length(), + ) + }; + parse_metadata_version(running_metadata) + } +} + +/// Parses the metadata of an upgrade, and checks its correctness. +/// +/// The metadata is a page starting with: +/// - 32 B upgrade hash (SHA256) +/// - 64 B signature, +/// that are not signed over. The second part is included in the signature with +/// - 8 B version and +/// - 4 B partition address in little endian encoding +/// written at METADATA_SIGN_OFFSET. +/// +/// Checks signature correctness against the hash, and whether the partition offset matches. +/// Whether the hash matches the partition content is not tested here! +fn check_metadata( + upgrade_locations: &impl UpgradeStorage, + public_key_bytes: &[u8], + metadata: &[u8], +) -> StorageResult<()> { + const METADATA_LEN: usize = 0x1000; + if metadata.len() != METADATA_LEN { + return Err(StorageError::CustomError); + } + + let version = parse_metadata_version(metadata); + if version < upgrade_locations.running_firmware_version() { + return Err(StorageError::CustomError); + } + + let metadata_address = LittleEndian::read_u32(&metadata[METADATA_SIGN_OFFSET + 8..][..4]); + if metadata_address != upgrade_locations.bundle_identifier() { + return Err(StorageError::CustomError); + } + + verify_signature( + array_ref!(metadata, 32, 64), + public_key_bytes, + parse_metadata_hash(metadata), + )?; + Ok(()) +} + +/// Parses the metadata, returns the hash. +fn parse_metadata_hash(data: &[u8]) -> &[u8; 32] { + array_ref!(data, 0, 32) +} + +/// Parses the metadata, returns the firmware version. +fn parse_metadata_version(data: &[u8]) -> u64 { + LittleEndian::read_u64(&data[METADATA_SIGN_OFFSET..][..8]) +} + +/// Verifies the signature over the given hash. +/// +/// The public key is COSE encoded, and the hash is a SHA256. +fn verify_signature( + signature_bytes: &[u8; 64], + public_key_bytes: &[u8], + signed_hash: &[u8; 32], +) -> StorageResult<()> { + let signature = + ecdsa::Signature::from_bytes(signature_bytes).ok_or(StorageError::CustomError)?; + let public_key = ecdsa::PubKey::from_bytes_uncompressed(public_key_bytes) + .ok_or(StorageError::CustomError)?; + if !public_key.verify_hash_vartime(signed_hash, &signature) { + return Err(StorageError::CustomError); + } + Ok(()) +} + +#[cfg(test)] +mod test { + use super::*; + use crate::env::test::TestEnv; + use crate::env::Env; + + #[test] + fn test_check_metadata() { + let mut env = TestEnv::new(); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(env.rng()); + let upgrade_locations = env.upgrade_storage().unwrap(); + + const METADATA_LEN: usize = 0x1000; + const METADATA_SIGN_OFFSET: usize = 0x800; + let mut metadata = vec![0xFF; METADATA_LEN]; + LittleEndian::write_u32(&mut metadata[METADATA_SIGN_OFFSET + 8..][..4], 0x60000); + + let mut signed_over_data = metadata[METADATA_SIGN_OFFSET..].to_vec(); + signed_over_data.extend(&[0xFF; 0x20000]); + let signed_hash = Sha256::hash(&signed_over_data); + + metadata[..32].copy_from_slice(&signed_hash); + let signature = private_key.sign_rfc6979::(&signed_over_data); + let mut signature_bytes = [0; ecdsa::Signature::BYTES_LENGTH]; + signature.to_bytes(&mut signature_bytes); + metadata[32..96].copy_from_slice(&signature_bytes); + + let public_key = private_key.genpk(); + let mut public_key_bytes = [0; 65]; + public_key.to_bytes_uncompressed(&mut public_key_bytes); + + assert_eq!( + check_metadata(upgrade_locations, &public_key_bytes, &metadata), + Ok(()) + ); + + // Manipulating the partition address fails. + metadata[METADATA_SIGN_OFFSET + 8] = 0x88; + assert_eq!( + check_metadata(upgrade_locations, &public_key_bytes, &metadata), + Err(StorageError::CustomError) + ); + metadata[METADATA_SIGN_OFFSET + 8] = 0x00; + // Wrong metadata length fails. + assert_eq!( + check_metadata( + upgrade_locations, + &public_key_bytes, + &metadata[..METADATA_LEN - 1] + ), + Err(StorageError::CustomError) + ); + // Manipulating the hash fails. + metadata[0] ^= 0x01; + assert_eq!( + check_metadata(upgrade_locations, &public_key_bytes, &metadata), + Err(StorageError::CustomError) + ); + metadata[0] ^= 0x01; + // Manipulating the signature fails. + metadata[32] ^= 0x01; + assert_eq!( + check_metadata(upgrade_locations, &public_key_bytes, &metadata), + Err(StorageError::CustomError) + ); + } + + #[test] + fn test_verify_signature() { + let mut env = TestEnv::new(); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(env.rng()); + let message = [0x44; 64]; + let signed_hash = Sha256::hash(&message); + let signature = private_key.sign_rfc6979::(&message); + + let mut signature_bytes = [0; ecdsa::Signature::BYTES_LENGTH]; + signature.to_bytes(&mut signature_bytes); + + let public_key = private_key.genpk(); + let mut public_key_bytes = [0; 65]; + public_key.to_bytes_uncompressed(&mut public_key_bytes); + + assert_eq!( + verify_signature(&signature_bytes, &public_key_bytes, &signed_hash), + Ok(()) + ); + assert_eq!( + verify_signature(&signature_bytes, &public_key_bytes, &[0x55; 32]), + Err(StorageError::CustomError) + ); + public_key_bytes[0] ^= 0x01; + assert_eq!( + verify_signature(&signature_bytes, &public_key_bytes, &signed_hash), + Err(StorageError::CustomError) + ); + public_key_bytes[0] ^= 0x01; + signature_bytes[0] ^= 0x01; + assert_eq!( + verify_signature(&signature_bytes, &public_key_bytes, &signed_hash), + Err(StorageError::CustomError) + ); + } +} diff --git a/apps/vault/src/env/xous/mod.rs b/apps/vault/src/env/xous/mod.rs new file mode 100644 index 000000000..b1ddd221a --- /dev/null +++ b/apps/vault/src/env/xous/mod.rs @@ -0,0 +1,492 @@ +pub use self::storage::XousStorage; +use crate::api::attestation_store::AttestationStore; +use crate::api::connection::{HidConnection, SendOrRecvError, SendOrRecvResult, SendOrRecvStatus}; +use crate::api::customization::{CustomizationImpl, DEFAULT_CUSTOMIZATION}; +use crate::api::firmware_protection::FirmwareProtection; +use crate::api::user_presence::{UserPresence, UserPresenceError, UserPresenceResult}; +use crate::api::{attestation_store, key_store}; +use crate::KEEPALIVE_DELAY_MS; +use crate::env::Env; +use core::sync::atomic::{AtomicU32, Ordering}; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use persistent_store::Store; +use ctap_crypto::rng256::XousRng256; +use xous_names::XousNames; +use crate::env::xous::storage::XousUpgradeStorage; +use usbd_human_interface_device::device::fido::*; +use pddb::Pddb; +use modals::Modals; +use locales::t; +use std::io::{Read, Write}; +use num_traits::*; + +use crate::ctap::hid::{CtapHid, KeepaliveStatus, ProcessedPacket, CtapHidCommand}; +use crate::{basis_change, deserialize_app_info, AppInfo, serialize_app_info}; + +pub const U2F_APP_DICT: &'static str = "fido.u2fapps"; +const KEEPALIVE_DELAY: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS); +mod storage; + +pub struct XousHidConnection { + pub endpoint: usb_device_xous::UsbHid, +} +impl XousHidConnection { + pub fn recv_with_timeout( + &mut self, + buf: &mut [u8; 64], + timeout_delay: Duration, + ) -> SendOrRecvStatus { + match self.endpoint.u2f_wait_incoming_timeout(timeout_delay.as_millis() as u64) { + Ok(msg) => { + buf.copy_from_slice(&msg.packet); + SendOrRecvStatus::Received + } + Err(_e) => { + SendOrRecvStatus::Timeout + } + } + } +} + +impl HidConnection for XousHidConnection { + fn send_and_maybe_recv( + &mut self, + buf: &mut [u8; 64], + _timeout: Duration, + ) -> SendOrRecvResult { + let mut reply = RawFidoMsg::default(); + reply.packet.copy_from_slice(buf); + match self.endpoint.u2f_send(reply) { + Ok(()) => Ok(SendOrRecvStatus::Sent), + Err(e) => { + log::error!("FIDO error in sending: {:?}", e); + Err(SendOrRecvError) + } + } + } +} + +pub struct XousEnv { + rng: XousRng256, + store: Store, + main_connection: XousHidConnection, + #[cfg(feature = "vendor_hid")] + vendor_connection: XousHidConnection, + modals: Modals, + pddb: Pddb, + main_cid: xous::CID, +} + +impl XousEnv { + /// Returns the unique instance of the Xous environment. + /// Blocks until the PDDB is mounted + pub fn new(conn: xous::CID) -> Self { + // We rely on `take_storage` to ensure that this function is called only once. + let storage = XousStorage {}; + let store = Store::new(storage).ok().unwrap(); + let xns = XousNames::new().unwrap(); + XousEnv { + rng: XousRng256::new(&xns), + store, + main_connection: XousHidConnection { + endpoint: usb_device_xous::UsbHid::new(), + }, + #[cfg(feature = "vendor_hid")] + vendor_connection: XousHidConnection { + endpoint: UsbEndpoint::VendorHid, + }, + modals: modals::Modals::new(&xns).unwrap(), + pddb: pddb::Pddb::new(), + main_cid: conn, + } + } + /// Checks if the SoC is compatible with USB drivers (older versions of Precursor's FPGA don't have the USB device core) + pub fn is_soc_compatible(&self) -> bool { + self.main_connection.endpoint.is_soc_compatible() + } + + fn send_keepalive_up_needed( + &mut self, + timeout: Duration, + cid: [u8; 4] + ) -> Result<(), UserPresenceError> { + let keepalive_msg = CtapHid::keepalive(cid, KeepaliveStatus::UpNeeded); + for mut pkt in keepalive_msg { + match self.main_connection.send_and_maybe_recv(&mut pkt, timeout) { + Ok(SendOrRecvStatus::Timeout) => { + log::debug!("Sending a KEEPALIVE packet timed out"); + // TODO: abort user presence test? + } + Err(_) => panic!("Error sending KEEPALIVE packet"), + Ok(SendOrRecvStatus::Sent) => { + log::debug!("Sent KEEPALIVE packet"); + } + Ok(SendOrRecvStatus::Received) => { + // We only parse one packet, because we only care about CANCEL. + let (received_cid, processed_packet) = CtapHid::process_single_packet(&pkt); + if received_cid != cid { + log::debug!( + "Received a packet on channel ID {:?} while sending a KEEPALIVE packet", + received_cid, + ); + return Ok(()); + } + match processed_packet { + ProcessedPacket::InitPacket { cmd, .. } => { + if cmd == CtapHidCommand::Cancel as u8 { + // We ignore the payload, we can't answer with an error code anyway. + log::debug!("User presence check cancelled"); + return Err(UserPresenceError::Canceled); + } else { + log::debug!( + "Discarded packet with command {} received while sending a KEEPALIVE packet", + cmd, + ); + } + } + ProcessedPacket::ContinuationPacket { .. } => { + log::debug!( + "Discarded continuation packet received while sending a KEEPALIVE packet", + ); + } + } + } + } + } + Ok(()) + } +} + +impl UserPresence for XousEnv { + fn check_init(&mut self) { + + } + /// Implements FIDO behavior (CTAP2 protocol) + fn wait_with_timeout(&mut self, timeout: Duration, reason: Option::, cid: [u8; 4]) -> UserPresenceResult { + let reason = reason.unwrap_or(String::new()); + let kbhit = Arc::new(AtomicU32::new(0)); + let expiration = Instant::now().checked_add(timeout).expect("duration bug"); + self.modals.dynamic_notification( + Some(t!("vault.u2freq", xous::LANG)), + None, + ).unwrap(); + // start the keyboard hit listener thread + let _ = std::thread::spawn({ + let token = self.modals.token().clone(); + let conn = self.modals.conn().clone(); + let kbhit = kbhit.clone(); + move || { + // note that if no key is hit, we get None back on dialog box close automatically + match modals::dynamic_notification_blocking_listener(token, conn) { + Ok(Some(c)) => { + log::trace!("kbhit got {}", c); + kbhit.store(c as u32, Ordering::SeqCst) + }, + Ok(None) => { + log::trace!("kbhit exited or had no characters"); + kbhit.store(0, Ordering::SeqCst) + }, + Err(e) => log::error!("error waiting for keyboard hit from blocking listener: {:?}", e), + } + } + }); + + let mut last_remaining = u64::MAX; + loop { + let mut request_str = String::from(&reason); + let remaining = expiration.duration_since(Instant::now()).as_secs(); + if last_remaining != remaining { + // only update the UX once per second + request_str.push_str( + &format!("\n\n⚠ {}{} ⚠\n", + remaining, + t!("vault.fido.countdown", xous::LANG) + )); + self.modals.dynamic_notification_update( + Some(t!("vault.u2freq", xous::LANG)), + Some(&request_str), + ).unwrap(); + last_remaining = remaining; + } + + // handle exit cases + if remaining == 0 { + self.modals.dynamic_notification_close().ok(); + return Err(UserPresenceError::Timeout) + } + let key_hit = kbhit.load(Ordering::SeqCst); + if key_hit != 0 && key_hit != 0x11 { // 0x11 is the F1 key + self.modals.dynamic_notification_close().ok(); + return Ok(()) + } else if key_hit == 0x11 { + self.modals.dynamic_notification_close().ok(); + return Err(UserPresenceError::Declined) + } + + // delay, and keepalive + self.send_keepalive_up_needed(KEEPALIVE_DELAY, cid) + .map_err(|e| e.into())?; + std::thread::sleep(KEEPALIVE_DELAY); + } + } + + /// Wait for user approval of a CTAP1-type credential + /// In this flow, we try to map the `app_id` to a user-provided, memorable string + /// We also give users the option to abort out of approving. + fn wait_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool { + let app_id_str = hex::encode(app_id); + let mut first_time = false; + let mut info = { + // fetch the application info, if it exists + let app_id_str = hex::encode(app_id); + log::info!("querying U2F record {}", app_id_str); + // add code to query the PDDB here to look for the k/v mapping of this app ID + match self.pddb.get( + U2F_APP_DICT, + &app_id_str, + None, true, false, + Some(256), Some(basis_change) + ) { + Ok(mut app_data) => { + let app_attr = app_data.attributes().unwrap(); + if app_attr.len != 0 { + let mut descriptor = Vec::::new(); + match app_data.read_to_end(&mut descriptor) { + Ok(_) => { + deserialize_app_info(descriptor) + } + Err(e) => {log::error!("Couldn't read app info: {:?}", e); None} + } + } else { + None + } + } + _ => { + log::info!("couldn't find key {}", app_id_str); + None + } + } + }.unwrap_or_else( + || { + // otherwise, create it + match self.modals + .alert_builder(t!("vault.u2f.give_app_name", xous::LANG)) + .field(None, None) + .build() + { + Ok(name) => { + first_time = true; + let info = AppInfo { + name: name.content()[0].content.to_string(), + notes: t!("vault.notes", xous::LANG).to_string(), + id: app_id, + ctime: crate::utc_now().timestamp() as u64, + atime: 0, + count: 0, + }; + info + } + _ => { + log::error!("couldn't get name for app"); + panic!("couldn't get name for app"); + } + } + } + ); + // request approval, if we didn't just create the record + if !first_time { + let mut request_str = String::from(&reason); + // we have some prior record of the app, human-format it + request_str.push_str(&format!("\n{}{}", + t!("vault.u2f.appinfo.name", xous::LANG), info.name + )); + request_str.push_str(&format!("\n{}", + crate::atime_to_str(info.atime) + )); + request_str.push_str(&format!("\n{}{}", + t!("vault.u2f.appinfo.authcount", xous::LANG), + info.count, + )); + + self.modals.dynamic_notification( + Some(t!("vault.u2freq", xous::LANG)), + Some(&request_str), + ).unwrap(); + + // block until the user hits a key + match modals::dynamic_notification_blocking_listener(self.modals.token(), self.modals.conn()) { + Ok(Some(c)) => { + if c as u32 == 0x11 { // this is the F1 key + return false; + } else { + // any other key shall accept + } + }, + Ok(None) => { + log::trace!("kbhit exited or had no characters"); + return false; + }, + Err(e) => { + log::error!("error waiting for keyboard hit from blocking listener: {:?}", e); + return false; + }, + } + } + + // note the access + info.atime = crate::utc_now().timestamp() as u64; + info.count = info.count.saturating_add(1); + let ser = serialize_app_info(&info); + + // update the access time, by deleting the key and writing it back into the PDDB + let basis = match self.pddb.get( + U2F_APP_DICT, + &app_id_str, + None, true, true, + Some(256), Some(basis_change) + ) { + Ok(app_data) => { + let attr = app_data.attributes().expect("couldn't get attributes"); + attr.basis + } + Err(e) => { + log::error!("error updating app atime: {:?}", e); + return false; + } + }; + self.pddb.delete_key(U2F_APP_DICT, &app_id_str, Some(&basis)).ok(); + match self.pddb.get( + U2F_APP_DICT, + &app_id_str, + Some(&basis), true, true, + Some(256), Some(basis_change) + ) { + Ok(mut app_data) => { + app_data.write(&ser).expect("couldn't update atime"); + } + _ => log::error!("Error updating app atime"), + } + self.pddb.sync().ok(); + + log::info!("sycing UI state..."); + xous::send_message( + self.main_cid, + xous::Message::new_scalar( + crate::VaultOp::ReloadDbAndFullRedraw.to_usize().unwrap(), + 0, 0, 0, 0) + ).unwrap(); + true + } + + fn check_complete(&mut self) { + + } +} + +impl FirmwareProtection for XousEnv { + fn lock(&mut self) -> bool { + false + } +} + +impl key_store::Helper for XousEnv {} + +impl AttestationStore for XousEnv { + fn get( + &mut self, + id: &attestation_store::Id, + ) -> Result, attestation_store::Error> { + if !matches!(id, attestation_store::Id::Batch) { + return Err(attestation_store::Error::NoSupport); + } + attestation_store::helper_get(self) + } + + fn set( + &mut self, + id: &attestation_store::Id, + attestation: Option<&attestation_store::Attestation>, + ) -> Result<(), attestation_store::Error> { + if !matches!(id, attestation_store::Id::Batch) { + return Err(attestation_store::Error::NoSupport); + } + attestation_store::helper_set(self, attestation) + } +} + +use core::fmt; +pub struct Console { +} +impl Console { + pub fn new() -> Self { + Console { } + } +} +impl fmt::Write for Console { + fn write_str(&mut self, string: &str) -> Result<(), fmt::Error> { + log::info!("{}", string); + Ok(()) + } +} + +impl Env for XousEnv { + type Rng = XousRng256; + type UserPresence = Self; + type Storage = XousStorage; + type KeyStore = Self; + type AttestationStore = Self; + type FirmwareProtection = Self; + type Write = Console; + type Customization = CustomizationImpl; + type HidConnection = XousHidConnection; + type UpgradeStorage = XousUpgradeStorage; + + fn rng(&mut self) -> &mut Self::Rng { + &mut self.rng + } + + fn user_presence(&mut self) -> &mut Self::UserPresence { + self + } + + fn store(&mut self) -> &mut Store { + &mut self.store + } + + fn key_store(&mut self) -> &mut Self { + self + } + + fn attestation_store(&mut self) -> &mut Self { + self + } + + fn upgrade_storage(&mut self) -> Option<&mut Self::UpgradeStorage> { + None + } + + fn firmware_protection(&mut self) -> &mut Self::FirmwareProtection { + self + } + + fn write(&mut self) -> Self::Write { + Console::new() + } + + fn customization(&self) -> &Self::Customization { + &DEFAULT_CUSTOMIZATION + } + + fn main_hid_connection(&mut self) -> &mut Self::HidConnection { + &mut self.main_connection + } + + #[cfg(feature = "vendor_hid")] + fn vendor_hid_connection(&mut self) -> &mut Self::HidConnection { + &mut self.vendor_connection + } +} + +pub const KEEPALIVE_DELAY_XOUS: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS); + diff --git a/apps/vault/src/env/xous/storage.rs b/apps/vault/src/env/xous/storage.rs new file mode 100644 index 000000000..6324c4fdc --- /dev/null +++ b/apps/vault/src/env/xous/storage.rs @@ -0,0 +1,75 @@ +// Copyright 2019-2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::env::UpgradeStorage; +use persistent_store::{Storage, StorageIndex, StorageResult}; +use std::borrow::Cow; + +pub struct XousStorage { +} + +impl XousStorage { + /// Dummy shim; actual write layer is implemented using the PDDB + pub fn new() -> StorageResult { + Ok(XousStorage { }) + } +} + +impl Storage for XousStorage { + fn word_size(&self) -> usize { + 1 + } + + fn page_size(&self) -> usize { + 4096 + } + + fn num_pages(&self) -> usize { + xous::PDDB_LEN as usize / 4096 + } + + fn max_word_writes(&self) -> usize { + 100_000 + } + + fn max_page_erases(&self) -> usize { + 100_000 + } + + fn read_slice(&self, _index: StorageIndex, _length: usize) -> StorageResult> { + unimplemented!() + } + + fn write_slice(&mut self, _index: StorageIndex, _value: &[u8]) -> StorageResult<()> { + unimplemented!() + } + + fn erase_page(&mut self, _page: usize) -> StorageResult<()> { + unimplemented!() + } +} + +pub struct XousUpgradeStorage { +} +impl UpgradeStorage for XousUpgradeStorage { + fn write_bundle(&mut self, _offset: usize, _data: Vec) -> StorageResult<()> { + unimplemented!() + } + fn bundle_identifier(&self) -> u32 { + unimplemented!() + } + fn running_firmware_version(&self) -> u64 { + unimplemented!() + } +} \ No newline at end of file diff --git a/apps/vault/src/lib.rs b/apps/vault/src/lib.rs new file mode 100644 index 000000000..710340319 --- /dev/null +++ b/apps/vault/src/lib.rs @@ -0,0 +1,116 @@ +// Copyright 2019-2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// #![cfg_attr(not(feature = "std"), no_std)] + +#[macro_use] +extern crate arrayref; + +use crate::ctap::hid::{HidPacket, HidPacketIterator}; +use crate::ctap::main_hid::MainHid; +#[cfg(feature = "vendor_hid")] +use crate::ctap::vendor_hid::VendorHid; +use crate::ctap::CtapState; +pub use crate::ctap::Transport; +use crate::env::Env; +use std::time::{Instant, Duration}; + +// Those macros should eventually be split into trace, debug, info, warn, and error macros when +// adding either the defmt or log feature and crate dependency. +#[cfg(feature = "debug_ctap")] +macro_rules! debug_ctap { + ($env: expr, $($rest:tt)*) => {{ + use core::fmt::Write; + writeln!($env.write(), $($rest)*).unwrap(); + }}; +} + +pub mod api; +// TODO(kaczmarczyck): Refactor this so that ctap module isn't public. +pub mod ctap; +pub mod env; +#[cfg(feature = "std")] +pub mod test_helpers; + +pub const KEEPALIVE_DELAY_MS: u64 = 100; +pub const KEEPALIVE_DELAY: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS); + +/// CTAP implementation parameterized by its environment. +pub struct Ctap { + env: E, + state: CtapState, + hid: MainHid, + #[cfg(feature = "vendor_hid")] + vendor_hid: VendorHid, +} + +impl Ctap { + /// Instantiates a CTAP implementation given its environment. + // This should only take the environment, but it temporarily takes the boot time until the + // clock is part of the environment. + pub fn new(mut env: E, now: Instant) -> Self { + let state = CtapState::new(&mut env, now); + let hid = MainHid::new(); + #[cfg(feature = "vendor_hid")] + let vendor_hid = VendorHid::new(); + Ctap { + env, + state, + hid, + #[cfg(feature = "vendor_hid")] + vendor_hid, + } + } + + pub fn state(&mut self) -> &mut CtapState { + &mut self.state + } + + pub fn hid(&mut self) -> &mut MainHid { + &mut self.hid + } + + pub fn env(&mut self) -> &mut E { + &mut self.env + } + + pub fn process_hid_packet( + &mut self, + packet: &HidPacket, + transport: Transport, + now: Instant, + ) -> HidPacketIterator { + match transport { + Transport::MainHid => { + self.hid + .process_hid_packet(&mut self.env, packet, now, &mut self.state) + } + #[cfg(feature = "vendor_hid")] + Transport::VendorHid => { + self.vendor_hid + .process_hid_packet(&mut self.env, packet, now, &mut self.state) + } + } + } + + pub fn update_timeouts(&mut self, now: Instant) { + self.state.update_timeouts(now); + self.hid.update_wink_timeout(now); + } +} + +#[cfg(feature="xous")] +pub mod vault_api; +#[cfg(feature="xous")] +pub use vault_api::*; diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index e9237bdaf..eea73061e 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -2,19 +2,6 @@ #![cfg_attr(target_os = "none", no_main)] mod ux; -use ux::*; -use num_traits::*; -use xous_ipc::Buffer; -use xous::{send_message, Message}; -use usbd_human_interface_device::device::fido::*; -use std::thread; -use core::sync::atomic::{AtomicU32, AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; - -mod ctap; -use ctap::hid::{ChannelID, CtapHid}; -use ctap::status_code::Ctap2StatusCode; -use ctap::CtapState; mod shims; use shims::*; mod submenu; @@ -26,13 +13,33 @@ mod storage; use locales::t; -use framework::ListItem; -use actions::{ActionOp, start_actions_thread}; +use vault::ctap::status_code::Ctap2StatusCode; +use vault::ctap::hid::HidPacketIterator; +use vault::env::xous::XousEnv; +use vault::api::connection::{HidConnection, SendOrRecvStatus}; +use vault::env::Env; +use vault::{ + SELF_CONN, Transport, VaultOp +}; +use actions::{ActionOp, start_actions_thread}; +use crate::ux::framework::{ListItem, NavDir}; use crate::prereqs::ntp_updater; use crate::vendor_commands::VendorSession; + +use ux::framework::{VaultUx, DEFAULT_FONT, FONT_LIST, name_to_style}; +use xous_ipc::Buffer; +use xous::{send_message, Message}; +use usbd_human_interface_device::device::fido::*; +use num_traits::*; + +use std::thread; +use core::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Instant, Duration}; use std::collections::BTreeMap; + // CTAP2 testing notes: // run our branch and use this to forward the prompts on to the device: // netcat -k -u -l 6502 > /dev/ttyS0 @@ -114,53 +121,20 @@ To clear test entries: */ -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub(crate) enum VaultOp { - /// a line of text has arrived - Line = 0, // make sure we occupy opcodes with discriminants < 1000, as the rest are used for callbacks - /// incremental line of text - IncrementalLine, - /// redraw our UI - Redraw, - /// ignore dirty rectangles and redraw everything - FullRedraw, - /// reload the database (slow), and ignore dirty rectangles and redraw everything - ReloadDbAndFullRedraw, - /// change focus - ChangeFocus, - - /// Partial menu - MenuChangeFont, - MenuDeleteStage1, - MenuEditStage1, - MenuAutotype, - MenuReadoutMode, - - /// PDDB basis change - BasisChange, - - /// Nop while waiting for prerequisites to be filled - Nop, - - /// exit the application - Quit, -} - #[derive(Copy, Clone, PartialEq, Eq, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -enum VaultMode { +pub enum VaultMode { Fido, Totp, Password, } #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] -struct SelectedEntry { - key_name: xous_ipc::String::<256>, - description: xous_ipc::String::<256>, - mode: VaultMode, +pub struct SelectedEntry { + pub key_name: xous_ipc::String::<256>, + pub description: xous_ipc::String::<256>, + pub mode: VaultMode, } - -struct ItemLists { +pub struct ItemLists { pub fido: BTreeMap::, pub totp: BTreeMap::, pub pw: BTreeMap::, @@ -171,12 +145,55 @@ impl ItemLists { } } -static SELF_CONN: AtomicU32 = AtomicU32::new(0); const ERR_TIMEOUT_MS: usize = 5000; +const SEND_TIMEOUT: Duration = Duration::from_millis(1000); +const KEEPALIVE_DELAY: Duration = Duration::from_millis(100); +// The reply/replies that are queued for each endpoint. +struct EndpointReply { + reply: HidPacketIterator, +} + +impl EndpointReply { + pub fn new() -> Self { + EndpointReply { + reply: HidPacketIterator::none(), + } + } +} +const NUM_ENDPOINTS: usize = 1; +// A single packet to send. +struct SendPacket { + packet: [u8; 64], +} + +struct EndpointReplies { + replies: [EndpointReply; NUM_ENDPOINTS], +} + +impl EndpointReplies { + pub fn new() -> Self { + EndpointReplies { + replies: [ + EndpointReply::new(), + ], + } + } + + pub fn next_packet(&mut self) -> Option { + for ep in self.replies.iter_mut() { + if let Some(packet) = ep.reply.next() { + return Some(SendPacket { + packet, + }); + } + } + None + } +} fn main() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); log::info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -185,8 +202,6 @@ fn main() -> ! { let time_conn = xous::connect(xous::SID::from_bytes(b"timeserverpublic").unwrap()).unwrap(); let conn = xous::connect(sid).unwrap(); - start_fido_ux_thread(conn); - // global shared state between threads. let mode = Arc::new(Mutex::new(VaultMode::Fido)); let item_lists = Arc::new(Mutex::new(ItemLists::new())); @@ -211,94 +226,75 @@ fn main() -> ! { // spawn the FIDO2 USB handler let _ = thread::spawn({ let allow_host = allow_host.clone(); + let conn = conn.clone(); move || { let xns = xous_names::XousNames::new().unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap(); - let boot_time = ClockValue::new(tt.elapsed_ms() as i64, 1000); let mut vendor_session = VendorSession::default(); + // block until the PDDB is mounted + let pddb = pddb::Pddb::new(); + pddb.is_mounted_blocking(); - let mut rng = ctap_crypto::rng256::XousRng256::new(&xns); - // this call will block until the PDDB is mounted. - let usb = usb_device_xous::UsbHid::new(); + let env = XousEnv::new(conn); + let mut replies = EndpointReplies::new(); // only run the main loop if the SoC is compatible - if usb.is_soc_compatible() { - let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time); - let mut ctap_hid = CtapHid::new(); + if env.is_soc_compatible() { + let mut ctap = vault::Ctap::new(env, Instant::now()); + let mut pkt_request: Option::<[u8; 64]> = None; loop { - match usb.u2f_wait_incoming() { - Ok(msg) => { - log::trace!("FIDO listener got message: {:?}", msg); - let now = ClockValue::new(tt.elapsed_ms() as i64, 1000); - let reply = match ctap_hid.process_hid_packet(&msg.packet, now, &mut ctap_state) { - ctap::hid::send::CTAPHIDResponse::StandardCommand(iter) => iter, - ctap::hid::send::CTAPHIDResponse::VendorCommand(cmd, cid, payload) => { - match vendor_commands::handle_vendor_data(cmd, cid, payload, &mut vendor_session) { - Ok(return_payload) => { - // if None, this means we've finished parsing all that - // was needed, and we handle/respond with real data - - match return_payload { - Some(data) => data, - None => { - log::debug!("starting processing of vendor data..."); - let resp = vendor_commands::handle_vendor_command( - &mut vendor_session, - allow_host.load(Ordering::SeqCst) - ); - log::debug!("finished processing of vendor data!"); - - match vendor_session.is_backup() { - true => { - if vendor_session.has_backup_data() { - resp - } else { - vendor_session = VendorSession::default(); - resp - } - }, - false => { - vendor_session = VendorSession::default(); - resp - } - } - } - } - } - Err(session_error) => { - // reset the session - vendor_session = VendorSession::default(); - - session_error.ctaphid_error(cid) - } - } - //vendor_commands::handle_vendor_command(cmd, cid, payload) - } - }; - // This block handles sending packets. - for pkt_reply in reply { - let mut reply = RawFidoMsg::default(); - reply.packet.copy_from_slice(&pkt_reply); - let status = usb.u2f_send(reply); - match status { - Ok(()) => { - log::trace!("Sent U2F packet"); - } - Err(e) => { - log::error!("Error sending U2F packet: {:?}", e); - } - } + /////////// + // TODO: vendor command for gsora backup protocol has disappeared. + /////////// + if let Some(mut packet) = replies.next_packet() { + // send and receive. + match ctap.env().main_hid_connection().send_and_maybe_recv(&mut packet.packet, SEND_TIMEOUT) { + Ok(SendOrRecvStatus::Timeout) => { + log::debug!("Sending packet timed out"); + // TODO: reset the ctap_hid state. + // Since sending the packet timed out, we cancel this reply. + break; } + Ok(SendOrRecvStatus::Sent) => { + log::debug!("Sent packet"); + } + Ok(SendOrRecvStatus::Received) => { + log::debug!("Received another packet"); + + // Copy to incoming packet to local buffer to be consistent + // with the receive flow. + pkt_request = Some(packet.packet); + } + Err(_) => panic!("Error sending packet"), } - Err(e) => { - match e { - xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator - log::info!("CTAP unplug_reset"); - ctap_state.unplug_reset(); - }, - _ => { - log::warn!("FIDO listener got an error: {:?}", e); + } else { + // receive + let mut rx_buf = [0u8; 64]; + pkt_request = match ctap.env().main_hid_connection().recv_with_timeout(&mut rx_buf, KEEPALIVE_DELAY) + { + SendOrRecvStatus::Received => { + log::debug!("Received packet"); + Some(rx_buf) + } + SendOrRecvStatus::Sent => { + panic!("Returned transmit status on receive"); + } + SendOrRecvStatus::Timeout => { + None + }, + }; + } + + if let Some(pkt_request) = pkt_request.take() { + let reply = ctap.process_hid_packet(&pkt_request, Transport::MainHid, Instant::now()); + if reply.has_data() { + // Update endpoint with the reply. + for ep in replies.replies.iter_mut() { + if ep.reply.has_data() { + log::warn!("Warning overwriting existing reply"); } + ep.reply = reply; + break; } } } @@ -312,7 +308,7 @@ fn main() -> ! { // spawn the icontray handler let _ = thread::spawn({ move || { - icontray_server(conn); + crate::ux::icontray::icontray_server(conn); } }); // spawn the TOTP pumper @@ -335,10 +331,6 @@ fn main() -> ! { item_lists, action_active.clone() ); - // Trigger the mode update in the actions - send_message(actions_conn, - Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0) - ).ok(); vaultux.update_mode(); vaultux.get_glyph_style(); @@ -474,6 +466,11 @@ fn main() -> ! { // HID is always selected if the vault is foregrounded vaultux.ensure_hid(); allow_redraw = true; + // Update database & configs + send_message(actions_conn, + Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + vaultux.update_mode(); } } }), @@ -581,14 +578,3 @@ fn main() -> ! { xous::terminate_process(0) } -fn check_user_presence(_cid: ChannelID) -> Result<(), Ctap2StatusCode> { - log::warn!("check user presence called, but not implemented!"); - Ok(()) -} - -pub(crate) fn basis_change() { - log::info!("got basis change"); - xous::send_message(SELF_CONN.load(Ordering::SeqCst), - Message::new_scalar(VaultOp::BasisChange.to_usize().unwrap(), 0, 0, 0, 0) - ).unwrap(); -} diff --git a/apps/vault/src/prereqs.rs b/apps/vault/src/prereqs.rs index d1f8ef5fc..8fa7e28d1 100644 --- a/apps/vault/src/prereqs.rs +++ b/apps/vault/src/prereqs.rs @@ -22,7 +22,7 @@ pub(crate) fn prereqs(sid: xous::SID, time_conn: xous::CID) -> ([u32; 4], bool) let token = gam.register_ux(UxRegistration { app_name: xous_ipc::String::<128>::from_str(app_name_ref), ux_type: gam::UxType::Chat, - predictor: Some(xous_ipc::String::<64>::from_str(crate::icontray::SERVER_NAME_ICONTRAY)), + predictor: Some(xous_ipc::String::<64>::from_str(crate::ux::icontray::SERVER_NAME_ICONTRAY)), listener: sid.to_array(), // note disclosure of our SID to the GAM -- the secret is now shared with the GAM! redraw_id: VaultOp::Redraw.to_u32().unwrap(), gotinput_id: Some(VaultOp::Line.to_u32().unwrap()), diff --git a/apps/vault/src/storage.rs b/apps/vault/src/storage.rs index 649471143..8f55919e4 100644 --- a/apps/vault/src/storage.rs +++ b/apps/vault/src/storage.rs @@ -5,6 +5,7 @@ use std::{ io::Write, time::{SystemTime, UNIX_EPOCH}, }; +use crate::totp::TotpAlgorithm; use ctap_crypto::Hash256; @@ -98,7 +99,7 @@ impl Manager { false, false, None, - Some(crate::basis_change), + Some(vault::basis_change), ) { Ok(_) => return true, Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, @@ -131,7 +132,7 @@ impl Manager { // exist true, alloc_hint, - Some(crate::basis_change), + Some(vault::basis_change), ) { Ok(mut data) => match data.write(payload) { Ok(_) => match sync { @@ -152,7 +153,7 @@ impl Manager { false, false, None, - Some(crate::basis_change), + Some(vault::basis_change), ) { Ok(mut record) => { let mut data = Vec::::new(); @@ -171,7 +172,7 @@ impl Manager { false, false, None, - Some(crate::basis_change), + Some(vault::basis_change), ) { Ok(record) => Ok(record .attributes() @@ -317,7 +318,7 @@ pub struct TotpRecord { // as base32, RFC4648 no padding pub secret: String, pub name: String, - pub algorithm: crate::totp::TotpAlgorithm, + pub algorithm: TotpAlgorithm, pub notes: String, pub digits: u32, pub timestep: u64, @@ -368,7 +369,7 @@ impl StorageContent for TotpRecord { "secret" => pr.secret.push_str(data), "name" => pr.name.push_str(data), "algorithm" => { - pr.algorithm = match crate::totp::TotpAlgorithm::try_from(data) { + pr.algorithm = match TotpAlgorithm::try_from(data) { Ok(a) => a, Err(_) => return Err(TOTPSerializationError::BadAlgorithm)?, } @@ -468,7 +469,7 @@ impl TryFrom> for TotpRecord { version: VAULT_TOTP_REC_VERSION, secret: String::new(), name: String::new(), - algorithm: crate::totp::TotpAlgorithm::HmacSha1, + algorithm: TotpAlgorithm::HmacSha1, notes: String::new(), digits: 0, ctime: 0, @@ -490,7 +491,7 @@ impl TryFrom> for TotpRecord { "secret" => pr.secret.push_str(data), "name" => pr.name.push_str(data), "algorithm" => { - pr.algorithm = match crate::totp::TotpAlgorithm::try_from(data) { + pr.algorithm = match TotpAlgorithm::try_from(data) { Ok(a) => a, Err(_) => return Err(TOTPSerializationError::BadAlgorithm), } diff --git a/apps/vault/src/submenu.rs b/apps/vault/src/submenu.rs index b189d3200..7e95e3034 100644 --- a/apps/vault/src/submenu.rs +++ b/apps/vault/src/submenu.rs @@ -2,7 +2,7 @@ use gam::*; use num_traits::*; use locales::t; -use crate::VaultOp; +use vault::VaultOp; use crate::actions::ActionOp; pub fn create_submenu(vault_conn: xous::CID, actions_conn: xous::CID, menu_mgr: xous::SID) -> MenuMatic { diff --git a/apps/vault/src/totp.rs b/apps/vault/src/totp.rs index 03b42aea5..6c978583b 100644 --- a/apps/vault/src/totp.rs +++ b/apps/vault/src/totp.rs @@ -8,8 +8,8 @@ use std::{ use std::sync::{Arc, Mutex}; use xous::{Message, send_message}; use std::thread; -use crate::VaultMode; use num_traits::*; +use crate::VaultMode; // Derived from https://github.com/blakesmith/xous-core/blob/xtotp-time/apps/xtotp/src/main.rs #[derive(Clone, Copy)] @@ -60,7 +60,7 @@ impl core::fmt::Display for TotpAlgorithm { } #[derive(Debug)] -pub(crate) struct TotpEntry { +pub struct TotpEntry { pub step_seconds: u64, pub shared_secret: Vec, pub digit_count: u8, @@ -68,7 +68,7 @@ pub(crate) struct TotpEntry { } #[derive(Debug)] -pub(crate) enum Error { +pub enum Error { Io(std::io::Error), DigestLength(InvalidKeyLength), } @@ -85,7 +85,7 @@ impl From for Error { } } -pub(crate) fn get_current_unix_time() -> Result { +pub fn get_current_unix_time() -> Result { SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map(|duration| duration.as_secs()) @@ -136,7 +136,7 @@ fn generate_hmac_bytes(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result Result { +pub fn generate_totp_code(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result { let hash = generate_hmac_bytes(unix_timestamp, totp_entry)?; let offset: usize = (hash.last().unwrap_or(&0) & 0xf) as usize; let binary: u64 = (((hash[offset] & 0x7f) as u64) << 24) diff --git a/apps/vault/src/ux.rs b/apps/vault/src/ux.rs index 1a0ea647a..7eff7219c 100644 --- a/apps/vault/src/ux.rs +++ b/apps/vault/src/ux.rs @@ -1,56 +1,4 @@ -pub(crate) mod fido; -pub(crate) use fido::*; -pub(crate) mod framework; -pub(crate) use framework::*; -pub(crate) mod icontray; -pub(crate) use icontray::*; - -use locales::t; -use chrono::{Utc, DateTime, NaiveDateTime}; -use std::time::{SystemTime, UNIX_EPOCH}; - -pub(crate) fn atime_to_str(req_atime: u64) -> String { - let mut request_str = String::with_capacity( - // avoid allocations to speed up this routine, it is in the inner loop of rendering lists of passwords - t!("vault.u2f.appinfo.last_authtime", xous::LANG).len() + - t!("vault.u2f.appinfo.seconds_ago", xous::LANG).len() + - 16 // space for the actual duration + some slop for translation - ); - if req_atime == 0 { - request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); - request_str.push_str(t!("vault.u2f.appinfo.never", xous::LANG)); - } else { - let now = utc_now(); - let atime = DateTime::::from_utc( - NaiveDateTime::from_timestamp(req_atime as i64, 0), - Utc - ); - // avoid format! macro, it is too slow. - if now.signed_duration_since(atime).num_days() > 1 { - request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); - request_str.push_str(&now.signed_duration_since(atime).num_days().to_string()); - request_str.push_str(t!("vault.u2f.appinfo.days_ago", xous::LANG)); - } else if now.signed_duration_since(atime).num_hours() > 1 { - request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); - request_str.push_str(&now.signed_duration_since(atime).num_hours().to_string()); - request_str.push_str(t!("vault.u2f.appinfo.hours_ago", xous::LANG)); - } else if now.signed_duration_since(atime).num_minutes() > 1 { - request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); - request_str.push_str(&now.signed_duration_since(atime).num_minutes().to_string()); - request_str.push_str(t!("vault.u2f.appinfo.minutes_ago", xous::LANG)); - } else { - request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); - request_str.push_str(&now.signed_duration_since(atime).num_seconds().to_string()); - request_str.push_str(t!("vault.u2f.appinfo.seconds_ago", xous::LANG)); - } - } - request_str -} - -/// because we don't get Utc::now, as the crate checks your architecture and xous is not recognized as a valid target -pub(crate) fn utc_now() -> DateTime:: { - let now = - SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch"); - let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32); - DateTime::from_utc(naive, Utc) -} \ No newline at end of file +// pub(crate) mod fido; +// pub(crate) use fido::*; +pub mod framework; +pub mod icontray; diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 17a2d6d24..1beef8362 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -1,28 +1,34 @@ -use crate::*; -use crate::totp::{TotpAlgorithm, generate_totp_code}; +use vault::{VaultOp, utc_now}; +use crate::{ItemLists, VaultMode, SelectedEntry}; +use crate::actions::ActionOp; +use crate::totp::{TotpAlgorithm, generate_totp_code, get_current_unix_time, TotpEntry}; + use gam::{GlyphStyle, MenuMatic, MenuItem, MenuPayload}; use graphics_server::{Gid, Point, Rectangle, DrawStyle, PixelColor, TextView}; use std::fmt::Write; use pddb::Pddb; use std::cell::RefCell; -use std::cmp::Ordering; use std::io::{Read, Write as FsWrite}; -use actions::ActionOp; use std::sync::atomic::Ordering as AtomicOrdering; +use std::sync::{Arc, Mutex}; +use std::sync::atomic::AtomicBool; use std::convert::TryFrom; +use std::cmp::Ordering; use usb_device_xous::UsbDeviceType; +use locales::t; +use num_traits::*; /// Display list for items. "name" is the key by which the list is sorted. /// "extra" is more information about the item, which should not be part of the sort. -pub(crate) struct ListItem { - pub(crate) name: String, - pub(crate) extra: String, - pub(crate) dirty: bool, +pub struct ListItem { + pub name: String, + pub extra: String, + pub dirty: bool, /// this is the name of the key used to refer to the item - pub(crate) guid: String, + pub guid: String, /// stash a copy so we can compare to the DB record and avoid re-generating the atime/count string if it hasn't changed. - pub(crate) atime: u64, - pub(crate) count: u64, + pub atime: u64, + pub count: u64, } impl ListItem { pub fn clone(&self) -> ListItem { @@ -56,6 +62,7 @@ impl Ord for ListItem { } } impl PartialOrd for ListItem { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } @@ -67,7 +74,7 @@ impl PartialEq for ListItem { } impl Eq for ListItem {} -pub(crate) enum NavDir { +pub enum NavDir { Up, Down, PageUp, @@ -75,7 +82,7 @@ pub(crate) enum NavDir { } #[allow(dead_code)] -pub(crate) struct VaultUx { +pub struct VaultUx { /// the content area content: Gid, gam: gam::Gam, @@ -123,12 +130,12 @@ pub(crate) struct VaultUx { current_time: u64, } -pub(crate) const DEFAULT_FONT: GlyphStyle = GlyphStyle::Regular; -pub(crate) const FONT_LIST: [&'static str; 6] = [ +pub const DEFAULT_FONT: GlyphStyle = GlyphStyle::Regular; +pub const FONT_LIST: [&'static str; 6] = [ "regular", "mono", "cjk", "bold", "large", "small" ]; -pub(crate) fn name_to_style(name: &str) -> Option { +pub fn name_to_style(name: &str) -> Option { match name { "regular" => Some(GlyphStyle::Regular), "mono" => Some(GlyphStyle::Monospace), @@ -180,7 +187,7 @@ impl VaultUx { let item_height = (glyph_height * 2) as i16 + margin.y * 2 + 2; // +2 because of the border width let items_per_screen = available_height / item_height; - let current_time = totp::get_current_unix_time().unwrap_or(0); + let current_time = get_current_unix_time().unwrap_or(0); VaultUx { content, @@ -272,7 +279,7 @@ impl VaultUx { VAULT_CONFIG_DICT, VAULT_CONFIG_KEY_FONT, Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(32), Some(crate::basis_change) + Some(32), Some(vault::basis_change) ) { Ok(mut style_key) => { let mut name_bytes = Vec::::new(); @@ -309,7 +316,7 @@ impl VaultUx { VAULT_CONFIG_DICT, VAULT_CONFIG_KEY_FONT, Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true, - Some(32), Some(crate::basis_change) + Some(32), Some(vault::basis_change) ) { Ok(mut style_key) => { style_key.write(style_to_name(&style).as_bytes()).ok(); @@ -510,7 +517,7 @@ impl VaultUx { if mode_at_entry == VaultMode::Totp { // always redraw the title in TOTP mode self.title_dirty = true; // always grab the current time, regardless of the mode? saves an extra call to get time... - self.current_time = totp::get_current_unix_time().unwrap_or(0); + self.current_time = get_current_unix_time().unwrap_or(0); // duration bar is hard-coded to once every 30 seconds, even if the keys may not change that often. let epoch = self.current_time / 30; if self.last_epoch != epoch { @@ -626,7 +633,7 @@ impl VaultUx { let step_seconds = u64::from_str_radix(fields[2], 10).unwrap_or(30); let algorithm = TotpAlgorithm::try_from(fields[3]).unwrap_or(TotpAlgorithm::HmacSha1); let is_hotp = fields[4].to_uppercase() == "HOTP"; - let totp = totp::TotpEntry { + let totp = TotpEntry { step_seconds: if !is_hotp {step_seconds} else {1}, // step_seconds is re-used by hotp as the code. shared_secret, digit_count, @@ -634,7 +641,7 @@ impl VaultUx { }; if !is_hotp { let code = generate_totp_code( - totp::get_current_unix_time().unwrap_or(0), + get_current_unix_time().unwrap_or(0), &totp ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()); // why code on top? because the item.name can be very long, and it can wrap which would cause @@ -710,17 +717,17 @@ impl VaultUx { let entry = &self.filtered_list[self.selection_index].guid; // we re-fetch the entry for autotype, because the PDDB could have unmounted a basis. let updated_pw = match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, + vault::VAULT_PASSWORD_DICT, entry, None, false, false, None, - Some(crate::basis_change) + Some(vault::basis_change) ) { Ok(mut record) => { let mut data = Vec::::new(); match record.read_to_end(&mut data) { Ok(_len) => { - if let Some(mut pw) = storage::PasswordRecord::try_from(data).ok() { + if let Some(mut pw) = crate::storage::PasswordRecord::try_from(data).ok() { match self.usb_dev.send_str(&pw.password) { Ok(_) => { pw.count += 1; @@ -751,10 +758,10 @@ impl VaultUx { // this get determines which basis the key is in let basis = match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, + vault::VAULT_PASSWORD_DICT, entry, None, true, true, - Some(256), Some(crate::basis_change) + Some(256), Some(vault::basis_change) ) { Ok(app_data) => { let attr = app_data.attributes().expect("couldn't get attributes"); @@ -766,19 +773,19 @@ impl VaultUx { } }; - match self.pddb.borrow().delete_key(crate::actions::VAULT_PASSWORD_DICT, entry, Some(&basis)) { + match self.pddb.borrow().delete_key(vault::VAULT_PASSWORD_DICT, entry, Some(&basis)) { Ok(_) => {} Err(_e) => { return Err(xous::Error::InternalError); } } match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, entry, Some(&basis), - false, true, Some(crate::actions::VAULT_ALLOC_HINT), - Some(crate::basis_change) + vault::VAULT_PASSWORD_DICT, entry, Some(&basis), + false, true, Some(vault::VAULT_ALLOC_HINT), + Some(vault::basis_change) ) { Ok(mut record) => { - let ser: Vec = storage::PasswordRecord::into(updated_pw); + let ser: Vec = crate::storage::PasswordRecord::into(updated_pw); match record.write(&ser) { Ok(_) => {} Err(e) => { @@ -807,7 +814,7 @@ impl VaultUx { let step_seconds = u64::from_str_radix(fields[2], 10).unwrap_or(30); let algorithm = TotpAlgorithm::try_from(fields[3]).unwrap_or(TotpAlgorithm::HmacSha1); let is_hotp = fields[4].to_uppercase() == "HOTP"; - let totp = totp::TotpEntry { + let totp = TotpEntry { step_seconds: if !is_hotp {step_seconds} else {1}, // step_seconds is re-used by hotp as the code. shared_secret, digit_count, @@ -815,7 +822,7 @@ impl VaultUx { }; let code = if !is_hotp { generate_totp_code( - totp::get_current_unix_time().unwrap_or(0), + get_current_unix_time().unwrap_or(0), &totp ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()) } else { @@ -832,10 +839,10 @@ impl VaultUx { // this get determines which basis the key is in let (basis, hotp_rec) = match self.pddb.borrow().get( - crate::actions::VAULT_TOTP_DICT, + vault::VAULT_TOTP_DICT, &entry, None, true, true, - Some(256), Some(crate::basis_change) + Some(256), Some(vault::basis_change) ) { Ok(mut app_data) => { let attr = app_data.attributes().expect("couldn't get attributes"); @@ -844,7 +851,7 @@ impl VaultUx { data.resize(len, 0); match app_data.read_exact(&mut data) { Ok(_len) => { - if let Some(mut totp_rec) = storage::TotpRecord::try_from(data).ok() { + if let Some(mut totp_rec) = crate::storage::TotpRecord::try_from(data).ok() { totp_rec.timestep += 1; (attr.basis, totp_rec) } else { @@ -864,7 +871,7 @@ impl VaultUx { } }; // remove the old entry, specifically only in the most recently open basis. - match self.pddb.borrow().delete_key(crate::actions::VAULT_TOTP_DICT, &entry, Some(&basis)) { + match self.pddb.borrow().delete_key(vault::VAULT_TOTP_DICT, &entry, Some(&basis)) { Ok(_) => {} Err(_e) => { return Err(xous::Error::InternalError); @@ -882,12 +889,12 @@ impl VaultUx { self.backpropagate_item(self.filtered_list[self.selection_index].clone()); // now write to disk match self.pddb.borrow().get( - crate::actions::VAULT_TOTP_DICT, &entry, Some(&basis), - false, true, Some(crate::actions::VAULT_ALLOC_HINT), - Some(crate::basis_change) + vault::VAULT_TOTP_DICT, &entry, Some(&basis), + false, true, Some(vault::VAULT_ALLOC_HINT), + Some(vault::basis_change) ) { Ok(mut record) => { - let ser: Vec = storage::TotpRecord::into(hotp_rec); + let ser: Vec = crate::storage::TotpRecord::into(hotp_rec); match record.write(&ser) { Ok(_) => {} Err(e) => { diff --git a/apps/vault/src/vault_api.rs b/apps/vault/src/vault_api.rs new file mode 100644 index 000000000..d7f9851ae --- /dev/null +++ b/apps/vault/src/vault_api.rs @@ -0,0 +1,200 @@ +use num_traits::*; +use std::{ + time::{SystemTime, UNIX_EPOCH}, +}; +use locales::t; +use chrono::{Utc, DateTime, NaiveDateTime}; + +// This file contains items that are used simultaneously within OpenSK and the `vault` app itself. +// These items need to be pulled in via both `lib` and `main` scopes. + +pub const VAULT_PASSWORD_DICT: &'static str = "vault.passwords"; +pub const VAULT_TOTP_DICT: &'static str = "vault.totp"; +/// bytes to reserve for a key entry. Making this slightly larger saves on some churn as stuff gets updated +pub const VAULT_ALLOC_HINT: usize = 256; + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum VaultOp { + /// a line of text has arrived + Line = 0, // make sure we occupy opcodes with discriminants < 1000, as the rest are used for callbacks + /// incremental line of text + IncrementalLine, + /// redraw our UI + Redraw, + /// ignore dirty rectangles and redraw everything + FullRedraw, + /// reload the database (slow), and ignore dirty rectangles and redraw everything + ReloadDbAndFullRedraw, + /// change focus + ChangeFocus, + + /// Partial menu + MenuChangeFont, + MenuDeleteStage1, + MenuEditStage1, + MenuAutotype, + MenuReadoutMode, + + /// PDDB basis change + BasisChange, + + /// Nop while waiting for prerequisites to be filled + Nop, + + /// exit the application + Quit, +} + + +pub fn atime_to_str(req_atime: u64) -> String { + let mut request_str = String::with_capacity( + // avoid allocations to speed up this routine, it is in the inner loop of rendering lists of passwords + t!("vault.u2f.appinfo.last_authtime", xous::LANG).len() + + t!("vault.u2f.appinfo.seconds_ago", xous::LANG).len() + + 16 // space for the actual duration + some slop for translation + ); + if req_atime == 0 { + request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); + request_str.push_str(t!("vault.u2f.appinfo.never", xous::LANG)); + } else { + let now = utc_now(); + let atime = DateTime::::from_utc( + NaiveDateTime::from_timestamp(req_atime as i64, 0), + Utc + ); + // avoid format! macro, it is too slow. + if now.signed_duration_since(atime).num_days() > 1 { + request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); + request_str.push_str(&now.signed_duration_since(atime).num_days().to_string()); + request_str.push_str(t!("vault.u2f.appinfo.days_ago", xous::LANG)); + } else if now.signed_duration_since(atime).num_hours() > 1 { + request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); + request_str.push_str(&now.signed_duration_since(atime).num_hours().to_string()); + request_str.push_str(t!("vault.u2f.appinfo.hours_ago", xous::LANG)); + } else if now.signed_duration_since(atime).num_minutes() > 1 { + request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); + request_str.push_str(&now.signed_duration_since(atime).num_minutes().to_string()); + request_str.push_str(t!("vault.u2f.appinfo.minutes_ago", xous::LANG)); + } else { + request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG)); + request_str.push_str(&now.signed_duration_since(atime).num_seconds().to_string()); + request_str.push_str(t!("vault.u2f.appinfo.seconds_ago", xous::LANG)); + } + } + request_str +} + +/// because we don't get Utc::now, as the crate checks your architecture and xous is not recognized as a valid target +pub fn utc_now() -> DateTime:: { + let now = + SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch"); + let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32); + DateTime::from_utc(naive, Utc) +} + +/// app info format: +/// +/// name: free form text string until newline +/// hash: app hash in hex string, lowercase +/// created: decimal number representing epoch of the creation date +/// last auth: decimal number representing epoch of the last auth time +pub struct AppInfo { + pub name: String, + pub id: [u8; 32], + pub notes: String, + pub ctime: u64, + pub atime: u64, + pub count: u64, +} + +pub fn deserialize_app_info(descriptor: Vec::) -> Option:: { + if let Ok(desc_str) = String::from_utf8(descriptor) { + let mut appinfo = AppInfo { + name: String::new(), + notes: String::new(), + id: [0u8; 32], + ctime: 0, + atime: 0, + count: 0, + }; + let lines = desc_str.split('\n'); + for line in lines { + if let Some((tag, data)) = line.split_once(':') { + match tag { + "name" => { + appinfo.name.push_str(data); + } + "notes" => appinfo.notes.push_str(data), + "id" => { + if let Ok(id) = hex::decode(data) { + appinfo.id.copy_from_slice(&id); + } else { + return None; + } + } + "ctime" => { + if let Ok(ctime) = u64::from_str_radix(data, 10) { + appinfo.ctime = ctime; + } else { + return None; + } + } + "atime" => { + if let Ok(atime) = u64::from_str_radix(data, 10) { + appinfo.atime = atime; + } else { + return None; + } + } + "count" => { + if let Ok(count) = u64::from_str_radix(data, 10) { + appinfo.count = count; + } + // count was added later, so, we don't fail if we don't see the record. + } + _ => { + log::warn!("unexpected tag {} encountered parsing app info, aborting", tag); + return None; + } + } + } else { + log::trace!("invalid line skipped: {:?}", line); + } + } + #[cfg(any(feature="precursor", feature="renode"))] + if appinfo.name.len() > 0 + && appinfo.id != [0u8; 32] + && appinfo.ctime != 0 { // atime can be 0 - indicates never used + Some(appinfo) + } else { + None + } + #[cfg(not(target_os = "xous"))] + if appinfo.name.len() > 0 + && appinfo.id != [0u8; 32] { // atime can be 0 - indicates never used. In hosted mode, ctime is 0. + Some(appinfo) + } else { + None + } + } else { + None + } +} + +pub fn serialize_app_info<'a>(appinfo: &AppInfo) -> Vec:: { + format!("{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n", + "name", appinfo.name, + "id", hex::encode(appinfo.id), + "ctime", appinfo.ctime, + "atime", appinfo.atime, + "count", appinfo.count, + ).into_bytes() +} + +pub fn basis_change() { + log::info!("got basis change"); + xous::send_message(SELF_CONN.load(core::sync::atomic::Ordering::SeqCst), + xous::Message::new_scalar(VaultOp::BasisChange.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); +} +pub static SELF_CONN: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); diff --git a/apps/vault/src/vendor_commands.rs b/apps/vault/src/vendor_commands.rs index 95a4cc771..564302aab 100644 --- a/apps/vault/src/vendor_commands.rs +++ b/apps/vault/src/vendor_commands.rs @@ -2,10 +2,11 @@ use cbor::reader::DecoderError; use core::convert::TryFrom; use locales::t; -use crate::{ - ctap::hid::{send::HidPacketIterator, ChannelID, Message}, - storage::{Error, PasswordRecord, TotpRecord}, +use vault::{ + ctap::hid::{send::HidPacketIterator, ChannelID, Message, CtapHidCommand} }; +use crate::totp::TotpAlgorithm; +use crate::storage::{Error, PasswordRecord, TotpRecord}; // Vault-specific command to upload TOTP codes pub const COMMAND_RESTORE_TOTP_CODES: u8 = 0x71; @@ -102,7 +103,7 @@ pub fn handle_vendor_data( return Ok(Some( HidPacketIterator::new(Message { cid: channel_id, - cmd, + cmd: cmd.into(), payload: vec![0xca, 0xfe, 0xba, 0xbe], }) .unwrap(), @@ -156,7 +157,7 @@ pub fn handle_vendor_data( false => Ok(Some( HidPacketIterator::new(Message { cid: channel_id, - cmd, + cmd: cmd.into(), payload: backup::CONTINUE_RESPONSE.to_vec(), }) .unwrap(), @@ -177,7 +178,7 @@ pub fn handle_vendor_command(session: &mut VendorSession, allow_host: bool) -> H COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) { Ok(payload) => Message { cid: channel_id, - cmd, + cmd: cmd.into(), payload, }, Err(error) => { @@ -190,7 +191,7 @@ pub fn handle_vendor_command(session: &mut VendorSession, allow_host: bool) -> H log::debug!("sending over chunk: {:?}", payload); Message { cid: channel_id, - cmd, + cmd: cmd.into(), payload, } } @@ -266,9 +267,9 @@ fn handle_restore(data: Vec, xns: &xous_names::XousNames) -> Result, name: elem.name, secret: elem.shared_secret, algorithm: match elem.algorithm { - backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1, - backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256, - backup::HashAlgorithms::SHA512 => crate::totp::TotpAlgorithm::HmacSha512, + backup::HashAlgorithms::SHA1 => TotpAlgorithm::HmacSha1, + backup::HashAlgorithms::SHA256 => TotpAlgorithm::HmacSha256, + backup::HashAlgorithms::SHA512 => TotpAlgorithm::HmacSha512, }, digits: elem.digit_count, timestep: elem.step_seconds, @@ -356,9 +357,9 @@ fn handle_backup( shared_secret: raw_code.secret, digit_count: raw_code.digits, algorithm: match raw_code.algorithm { - crate::totp::TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1, - crate::totp::TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256, - crate::totp::TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512, + TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1, + TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256, + TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512, _ => panic!("invalid algorithm"), }, name: raw_code.name, @@ -417,7 +418,7 @@ pub(crate) fn error_message(cid: ChannelID, error_code: u8) -> Message { // This unwrap is safe because the payload length is 1 <= 7609 bytes. Message { cid, - cmd: 0x3F, // COMMAND_ERROR + cmd: CtapHidCommand::Error, // COMMAND_ERROR payload: vec![error_code], } } From 5ce6e1b7662a96a2d138f11c57111ef6ce3f892c Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 00:55:12 +0800 Subject: [PATCH 21/80] initial integration of fido2.1 now passing some basic tests --- apps/vault/locales/i18n.json | 7 + apps/vault/src/actions.rs | 116 ++--- apps/vault/src/api/user_presence.rs | 9 +- apps/vault/src/ctap/ctap1.rs | 2 + apps/vault/src/ctap/mod.rs | 10 +- apps/vault/src/ctap/storage.rs | 3 + apps/vault/src/ctap/timed_permission.rs | 6 +- apps/vault/src/env/xous/mod.rs | 558 ++++++++++++++++++------ apps/vault/src/main.rs | 88 ++-- 9 files changed, 543 insertions(+), 256 deletions(-) diff --git a/apps/vault/locales/i18n.json b/apps/vault/locales/i18n.json index b75525c9f..24cbbdea1 100644 --- a/apps/vault/locales/i18n.json +++ b/apps/vault/locales/i18n.json @@ -461,6 +461,13 @@ "ja": "アプリ名:", "zh": "应用名称:" }, + "vault.u2f.appinfo.newapp": { + "en": "New app: ", + "en-tts": "New app: ", + "fr": "Nouvelle application: ", + "ja": "新しいアプリ: ", + "zh": "新应用: " + }, "vault.u2f.appinfo.never": { "en": "never", "en-tts": "never", diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index f8594c640..47fcf70fb 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -58,10 +58,11 @@ pub(crate) fn start_actions_thread( sid: SID, mode: Arc::>, item_lists: Arc::>, action_active: Arc::, + opensk_mutex: Arc::>, ) { let _ = thread::spawn({ move || { - let mut manager = ActionManager::new(main_conn, mode, item_lists, action_active); + let mut manager = ActionManager::new(main_conn, mode, item_lists, action_active, opensk_mutex); loop { let msg = xous::receive_message(sid).unwrap(); let opcode: Option = FromPrimitive::from_usize(msg.body.id()); @@ -152,6 +153,7 @@ struct ActionManager<'a> { pddb: RefCell::, tt: ticktimer_server::Ticktimer, action_active: Arc::, + opensk_mutex: Arc::>, mode_cache: VaultMode, main_conn: xous::CID, #[cfg(feature="vaultperf")] @@ -168,7 +170,8 @@ impl<'a> ActionManager<'a> { main_conn: xous::CID, mode: Arc::>, item_lists: Arc::>, - action_active: Arc:: + action_active: Arc::, + opensk_mutex: Arc::>, ) -> ActionManager<'a> { let xns = xous_names::XousNames::new().unwrap(); let storage_manager = storage::Manager::new(&xns); @@ -199,6 +202,7 @@ impl<'a> ActionManager<'a> { pddb: RefCell::new(pddb::Pddb::new()), tt: ticktimer_server::Ticktimer::new().unwrap(), action_active, + opensk_mutex, main_conn, #[cfg(feature="vaultperf")] perfbuf, @@ -957,7 +961,7 @@ impl<'a> ActionManager<'a> { t!("vault.u2f.appinfo.authcount", xous::LANG), ai.count, ); - let desc = format!("{}", ai.name); + let desc = format!("{} (U2F)", ai.name); let li = ListItem { name: desc, extra, @@ -995,61 +999,67 @@ impl<'a> ActionManager<'a> { }, } - log::debug!("listing in {}", OPENSK2_DICT); - match self.pddb.borrow().read_dict(OPENSK2_DICT, None, Some(256 * 1024)) { - Ok(keys) => { - let mut oom_keys = 0; - for key in keys { - if let Some(data) = key.data { - match vault::ctap::storage::deserialize_credential(&data) { - Some(result) => { - let name = if let Some(display_name) = result.user_display_name { - display_name - } else { - String::from_utf8(result.user_handle).unwrap_or("".to_string()) - }; - let desc = format!("{} / {}", result.rp_id, String::from_utf8(result.credential_id).unwrap_or("---".to_string())); - let extra = format!("FIDO2 {}", name); - let li = ListItem { - name: desc, - extra, - dirty: true, - guid: key.name, - count: 0, - atime: 0, - }; - il.insert(li.key(), li); + { // this brace creates a block that defines the lifetime of `mutex`; it is released on drop as it goes out of scope + // access to OPENSK2_DICT has to be mutex-guarded, otherwise we get errors as the + // OpenSK thread mutates the dictionary while we query it + let mutex = self.opensk_mutex.lock().unwrap(); + log::debug!("listing in {}", OPENSK2_DICT); + match self.pddb.borrow().read_dict(OPENSK2_DICT, None, Some(256 * 1024)) { + Ok(keys) => { + let mut oom_keys = 0; + for key in keys { + let key_number = key.name.parse::().unwrap_or(0); + if vault::ctap::storage::key::CREDENTIALS.contains(&key_number) { + if let Some(data) = key.data { + match vault::ctap::storage::deserialize_credential(&data) { + Some(result) => { + let name = if let Some(display_name) = result.user_display_name { + display_name + } else { + String::from_utf8(result.user_handle).unwrap_or("".to_string()) + }; + let desc = format!("{} / {} (FIDO2)", result.rp_id, String::from_utf8(result.credential_id).unwrap_or("---".to_string())); + let extra = format!("{}", name); + let li = ListItem { + name: desc, + extra, + dirty: true, + guid: key.name, + count: 0, + atime: 0, + }; + il.insert(li.key(), li); + } + None => { + // Probably more indicative of a mismatch in OpenSK key range mapping, rather than a hard error. + let err = format!("{}:{}:{}: ({}){:x?}...", + key.basis, OPENSK2_DICT, key.name, key.len, &data[..16] + ); + log::info!("Couldn't deserialize FIDO2 key {}", err); + }, + } + } else { + oom_keys += 1; } - None => { - // this is "harmless" because we're now returning, like, everything - // print to info! for now, so we can be more selective down the road about - // deserializations and maybe save some time - let err = format!("{}:{}:{}: ({}){:x?}...", - key.basis, OPENSK2_DICT, key.name, key.len, &data[..16] - ); - log::info!("Couldn't deserialize FIDO2 key {}", err); - }, } - } else { - oom_keys += 1; + } + if oom_keys != 0 { + log::warn!("Ran out of cache space handling FIDO2 tokens. {} tokens are not loaded.", oom_keys); + self.report_err(&format!("Ran out of cache space handling FIDO2 entries. {} tokens not loaded", oom_keys), None::); } } - if oom_keys != 0 { - log::warn!("Ran out of cache space handling FIDO2 tokens. {} tokens are not loaded.", oom_keys); - self.report_err(&format!("Ran out of cache space handling FIDO2 entries. {} tokens not loaded", oom_keys), None::); - } + Err(e) => { + match e.kind() { + ErrorKind::NotFound => { + // this is fine, it just means no entries have been entered yet + }, + _ => { + log::error!("Error opening FIDO2 dictionary"); + self.report_err("Error opening FIDO2 dictionary", Some(e)) + } + } + }, } - Err(e) => { - match e.kind() { - ErrorKind::NotFound => { - // this is fine, it just means no entries have been entered yet - }, - _ => { - log::error!("Error opening FIDO2 dictionary"); - self.report_err("Error opening FIDO2 dictionary", Some(e)) - } - } - }, } } VaultMode::Totp => { diff --git a/apps/vault/src/api/user_presence.rs b/apps/vault/src/api/user_presence.rs index e8f46fa86..1509ebb4f 100644 --- a/apps/vault/src/api/user_presence.rs +++ b/apps/vault/src/api/user_presence.rs @@ -37,13 +37,16 @@ pub trait UserPresence { /// Must be called between calls to [`Self::check_init`] and [`Self::check_complete`]. #[cfg(feature="xous")] fn wait_with_timeout(&mut self, timeout: Duration, reason: Option, cid: [u8; 4]) -> UserPresenceResult; + #[cfg(feature="xous")] + fn recently_requested(&mut self) -> bool; #[cfg(not(feature="xous"))] fn wait_with_timeout(&mut self, timeout: Duration) -> UserPresenceResult; - /// CTAP1 waits don't require keepalive packets. They just block. However, they also have - /// an `app_id` which we have to lookup to turn into a human-readable identifier. + /// On the first call, pops up a request box for user presence approval + /// Subsequent calls simply return true/false if the user has finally approved. + /// The request box eventually times out on its own. #[cfg(feature="xous")] - fn wait_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool; + fn poll_approval_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool; /// Finalizes a user presence check. /// diff --git a/apps/vault/src/ctap/ctap1.rs b/apps/vault/src/ctap/ctap1.rs index 6d8365e01..587c09127 100644 --- a/apps/vault/src/ctap/ctap1.rs +++ b/apps/vault/src/ctap/ctap1.rs @@ -201,6 +201,7 @@ impl Ctap1Command { } #[cfg(feature="xous")] let regstr = String::from(t!("vault.u2f.register", xous::LANG)); + #[cfg(feature="xous")] if !ctap_state.u2f_up_state.consume_up(env, regstr, application) { log::debug!("still waiting..."); return Err(Ctap1StatusCode::SW_COND_USE_NOT_SATISFIED); @@ -224,6 +225,7 @@ impl Ctap1Command { #[cfg(feature="xous")] let authstr = String::from(t!("vault.u2f.authenticate", xous::LANG)); // The order is important due to side effects of checking user presence. + #[cfg(feature="xous")] if flags == Ctap1Flags::EnforceUpAndSign && !ctap_state.u2f_up_state.consume_up(env, authstr, application) { diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index 16a49c6e8..c1cc76aea 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -113,7 +113,7 @@ pub const TOUCH_TIMEOUT_MS: u64 = 30000; #[cfg(feature = "with_ctap1")] pub const TOUCH_TIMEOUT: Duration = Duration::from_millis(TOUCH_TIMEOUT_MS); #[cfg(feature = "with_ctap1")] -const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_millis(10000); +pub const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_millis(10000); // TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(10000); const STATEFUL_COMMAND_TIMEOUT_DURATION: Duration = Duration::from_millis(30000); @@ -569,10 +569,10 @@ impl CtapState { now: Instant, ) -> Vec { let cmd = Command::deserialize(command_cbor); - log::debug!("Received command: {:#?}", cmd); + log::debug!("Received command: {:?}", cmd); let response = cmd.and_then(|command| self.process_parsed_command(env, command, channel, now)); - log::debug!("Sending response: {:#?}", response); + log::debug!("Sending response: {:?}", response); match response { Ok(response_data) => { let mut response_vec = vec![Ctap2StatusCode::CTAP2_OK as u8]; @@ -1552,8 +1552,8 @@ impl CtapState { } #[cfg(feature = "with_ctap1")] - pub fn u2f_needs_user_presence(&mut self, now: Instant) -> bool { - self.u2f_up_state.is_up_needed(now) + pub fn u2f_needs_user_presence(&mut self, env: &mut impl Env, now: Instant) -> bool { + self.u2f_up_state.is_up_needed(env, now) } } diff --git a/apps/vault/src/ctap/storage.rs b/apps/vault/src/ctap/storage.rs index 64e44bc14..1cb360944 100644 --- a/apps/vault/src/ctap/storage.rs +++ b/apps/vault/src/ctap/storage.rs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(feature="xous")] +pub mod key; // allows us to access the table of key names so we can inspect the contents of OpenSK's database +#[cfg(not(feature="xous"))] mod key; use crate::api::attestation_store::{self, AttestationStore}; diff --git a/apps/vault/src/ctap/timed_permission.rs b/apps/vault/src/ctap/timed_permission.rs index 933b4669f..e17ec3210 100644 --- a/apps/vault/src/ctap/timed_permission.rs +++ b/apps/vault/src/ctap/timed_permission.rs @@ -96,7 +96,7 @@ impl U2fUserPresenceState { // This marks user presence as needed or uses it up if already granted. Also cleans up. #[cfg(feature="xous")] pub fn consume_up(&mut self, env: &mut impl Env, reason: String, app_id: [u8; 32]) -> bool { - env.user_presence().wait_ctap1(String::from(reason), app_id) + env.user_presence().poll_approval_ctap1(String::from(reason), app_id) } #[cfg(not(feature="xous"))] pub fn consume_up(&mut self, now: Instant) -> bool { @@ -117,8 +117,8 @@ impl U2fUserPresenceState { self.needs_up.is_granted(now) } #[cfg(feature="xous")] - pub fn is_up_needed(&mut self, _now: Instant) -> bool { - false + pub fn is_up_needed(&mut self, env: &mut impl Env, _now: Instant) -> bool { + env.user_presence().recently_requested() } // If you don't regularly call any other function, not cleaning up leads to overflow problems. diff --git a/apps/vault/src/env/xous/mod.rs b/apps/vault/src/env/xous/mod.rs index b1ddd221a..c9552e9be 100644 --- a/apps/vault/src/env/xous/mod.rs +++ b/apps/vault/src/env/xous/mod.rs @@ -19,6 +19,7 @@ use pddb::Pddb; use modals::Modals; use locales::t; use std::io::{Read, Write}; +use std::num::ParseIntError; use num_traits::*; use crate::ctap::hid::{CtapHid, KeepaliveStatus, ProcessedPacket, CtapHidCommand}; @@ -28,6 +29,13 @@ pub const U2F_APP_DICT: &'static str = "fido.u2fapps"; const KEEPALIVE_DELAY: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS); mod storage; +fn decode_hex(s: &str) -> Result, ParseIntError> { + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) + .collect() +} + pub struct XousHidConnection { pub endpoint: usb_device_xous::UsbHid, } @@ -47,6 +55,12 @@ impl XousHidConnection { } } } + pub fn u2f_wait_incoming(&self) -> Result { + self.endpoint.u2f_wait_incoming() + } + pub fn u2f_send(&self, msg: RawFidoMsg) -> Result<(), xous::Error> { + self.endpoint.u2f_send(msg) + } } impl HidConnection for XousHidConnection { @@ -67,6 +81,18 @@ impl HidConnection for XousHidConnection { } } +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +struct Ctap1Request { + reason: xous_ipc::String::<1024>, + app_id: [u8; 32], + approved: bool, +} +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum Ctap1Op { + PollPermission, + UpdateAppInfo, + Invalid, +} pub struct XousEnv { rng: XousRng256, store: Store, @@ -76,6 +102,8 @@ pub struct XousEnv { modals: Modals, pddb: Pddb, main_cid: xous::CID, + last_user_presence_request: Option::, + ctap1_cid: xous::CID, } impl XousEnv { @@ -86,6 +114,302 @@ impl XousEnv { let storage = XousStorage {}; let store = Store::new(storage).ok().unwrap(); let xns = XousNames::new().unwrap(); + let ctap1_sid = xous::create_server().unwrap(); + let ctap1_cid = xous::connect(ctap1_sid).unwrap(); + + std::thread::spawn({ + let main_cid = conn.clone(); + move || { + let xns = xous_names::XousNames::new().unwrap(); + let pddb = pddb::Pddb::new(); + let modals = modals::Modals::new(&xns).unwrap(); + let tt = ticktimer_server::Ticktimer::new().unwrap(); + + let mut msg_opt = None; + let mut _return_type = 0; + let mut current_id: Option<[u8; 32]> = None; + let mut denied_id: Option<[u8; 32]> = None; + let mut request_str = String::new(); + let mut request_start = 0u64; + let mut last_remaining = 0u64; + let kbhit = Arc::new(AtomicU32::new(0)); + loop { + xous::reply_and_receive_next_legacy(ctap1_sid, &mut msg_opt, &mut _return_type) + .unwrap(); + let msg = msg_opt.as_mut().unwrap(); + log::trace!("msg: {:x?}", msg); + match num_traits::FromPrimitive::from_usize(msg.body.id()) + .unwrap_or(Ctap1Op::Invalid) + { + Ctap1Op::PollPermission => { + let mut buf = unsafe { + xous_ipc::Buffer::from_memory_message_mut( + msg.body.memory_message_mut().unwrap(), + ) + }; + let mut request = buf.to_original::().unwrap(); + request.approved = false; + if let Some(id) = ¤t_id { + log::trace!("poll of existing ID"); + if *id != request.app_id { + log::error!("Request ID changed while request is in progress. Ignoring request"); + buf.replace(request).ok(); + modals.dynamic_notification_close().ok(); + current_id = None; + continue; + } else { + let key = kbhit.load(Ordering::SeqCst); + if key != 0 && key != 0x11 { // approved + log::trace!("approved"); + request.approved = true; + current_id = None; + modals.dynamic_notification_close().ok(); + } else if key == 0x11 { // denied + log::trace!("denied"); + request.approved = false; + denied_id = current_id.take(); + modals.dynamic_notification_close().ok(); + } else { + log::trace!("waiting"); + // keep waiting for an interaction, with a timeout + let now = tt.elapsed_ms(); + if (crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) > (now - request_start) { + let new_remaining = ((crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) - (now - request_start)) / 1000; + if new_remaining != last_remaining { + log::trace!("update timer"); + last_remaining = new_remaining; + let mut to_request_str = request_str.to_string(); + to_request_str.push_str( + &format!("\n\n⚠ {}{} ⚠\n", + last_remaining, + t!("vault.fido.countdown", xous::LANG) + )); + modals.dynamic_notification_update( + Some(t!("vault.u2freq", xous::LANG)), + Some(&to_request_str), + ).unwrap(); + } + } else { + log::trace!("timed out"); + // timed out + current_id = None; + denied_id = None; + request.approved = false; + modals.dynamic_notification_close().ok(); + } + } + buf.replace(request).ok(); + continue; + } + } else { + log::debug!("setup new ID query: {:?}", request.app_id); + if let Some(denied) = denied_id.take() { + if tt.elapsed_ms() - request_start < (crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) { + if denied == request.app_id { + // this is a repeat request for a denied ID + request.approved = false; + buf.replace(request).ok(); + denied_id = Some(denied); + continue; + } else { + // it's a different request, carry on; the denied_id will be reset as we handle this new request + } + } else { + // denial should automatically reset due to .take() + } + } + // determine the application info string + let app_id_str = hex::encode(request.app_id); + if let Some(mut info) = { + // fetch the application info, if it exists + log::debug!("querying U2F record {}", app_id_str); + // add code to query the PDDB here to look for the k/v mapping of this app ID + match pddb.get( + U2F_APP_DICT, + &app_id_str, + None, true, false, + Some(256), Some(basis_change) + ) { + Ok(mut app_data) => { + let app_attr = app_data.attributes().unwrap(); + if app_attr.len != 0 { + let mut descriptor = Vec::::new(); + match app_data.read_to_end(&mut descriptor) { + Ok(_) => { + deserialize_app_info(descriptor) + } + Err(e) => {log::error!("Couldn't read app info: {:?}", e); None} + } + } else { + None + } + } + _ => { + log::info!("couldn't find key {}", app_id_str); + None + } + } + } { + request_str.clear(); + // we have some prior record of the app, human-format it + request_str.push_str(&format!("\n{}{}", + t!("vault.u2f.appinfo.name", xous::LANG), info.name + )); + request_str.push_str(&format!("\n{}", + crate::atime_to_str(info.atime) + )); + request_str.push_str(&format!("\n{}{}", + t!("vault.u2f.appinfo.authcount", xous::LANG), + info.count, + )); + } else { + request_str.clear(); + // request approval of the new app ID + request_str.push_str(&format!("\n{}\nApp ID: {:x?}", + t!("vault.u2f.appinfo.newapp", xous::LANG), request.app_id + )); + } + let mut to_request_str = request_str.to_string(); + request_start = tt.elapsed_ms(); + last_remaining = ((crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) + - (tt.elapsed_ms() - request_start)) / 1000; + to_request_str.push_str( + &format!("\n\n⚠ {}{} ⚠\n", + last_remaining, + t!("vault.fido.countdown", xous::LANG) + )); + + modals.dynamic_notification( + Some(t!("vault.u2freq", xous::LANG)), + Some(&to_request_str), + ).unwrap(); + // start a keyboard listener + kbhit.store(0, Ordering::SeqCst); + let _ = std::thread::spawn({ + let token = modals.token().clone(); + let conn = modals.conn().clone(); + let kbhit = kbhit.clone(); + move || { + // note that if no key is hit, we get None back on dialog box close automatically + match modals::dynamic_notification_blocking_listener(token, conn) { + Ok(Some(c)) => { + log::trace!("kbhit got {}", c); + kbhit.store(c as u32, Ordering::SeqCst) + }, + Ok(None) => { + log::trace!("kbhit exited or had no characters"); + kbhit.store(0, Ordering::SeqCst) + }, + Err(e) => log::error!("error waiting for keyboard hit from blocking listener: {:?}", e), + } + } + }); + current_id = Some(request.app_id); + denied_id = None; // reset the denial timers, too + request.approved = false; + buf.replace(request).ok(); + } + } + Ctap1Op::UpdateAppInfo => { + let buf = unsafe { + xous_ipc::Buffer::from_memory_message( + msg.body.memory_message().unwrap(), + ) + }; + let update = buf.to_original::().unwrap(); + let app_id_str = hex::encode(update.app_id); + // note the access + let mut info = { + // fetch the application info, if it exists + log::info!("Updating U2F record {}", app_id_str); + // add code to query the PDDB here to look for the k/v mapping of this app ID + match pddb.get( + U2F_APP_DICT, + &app_id_str, + None, true, false, + Some(256), Some(basis_change) + ) { + Ok(mut app_data) => { + let app_attr = app_data.attributes().unwrap(); + if app_attr.len != 0 { + let mut descriptor = Vec::::new(); + match app_data.read_to_end(&mut descriptor) { + Ok(_) => { + deserialize_app_info(descriptor) + } + Err(e) => {log::error!("Couldn't read app info: {:?}", e); None} + } + } else { + None + } + } + _ => { + log::info!("couldn't find key {}", app_id_str); + None + } + } + }.unwrap_or_else( + || { + // otherwise, create it + match modals + .alert_builder(t!("vault.u2f.give_app_name", xous::LANG)) + .field(None, None) + .build() + { + Ok(name) => { + let info = AppInfo { + name: name.content()[0].content.to_string(), + notes: t!("vault.notes", xous::LANG).to_string(), + id: update.app_id, + ctime: crate::utc_now().timestamp() as u64, + atime: 0, + count: 0, + }; + info + } + _ => { + log::error!("couldn't get name for app"); + panic!("couldn't get name for app"); + } + } + } + ); + + info.atime = crate::utc_now().timestamp() as u64; + info.count = info.count.saturating_add(1); + let ser = serialize_app_info(&info); + + // update the access time, by deleting the key and writing it back into the PDDB + pddb.delete_key(U2F_APP_DICT, &app_id_str, None).ok(); + match pddb.get( + U2F_APP_DICT, + &app_id_str, + None, true, true, + Some(256), Some(basis_change) + ) { + Ok(mut app_data) => { + app_data.write(&ser).expect("couldn't update atime"); + } + _ => log::error!("Error updating app atime"), + } + pddb.sync().ok(); + + log::debug!("sycing UI state..."); + xous::send_message( + main_cid, + xous::Message::new_scalar( + crate::VaultOp::ReloadDbAndFullRedraw.to_usize().unwrap(), + 0, 0, 0, 0) + ).unwrap(); + } + Ctap1Op::Invalid => { + log::error!("got invalid opcode: {}, ignoring", msg.body.id()); + } + } + } + } + }); + XousEnv { rng: XousRng256::new(&xns), store, @@ -99,6 +423,8 @@ impl XousEnv { modals: modals::Modals::new(&xns).unwrap(), pddb: pddb::Pddb::new(), main_cid: conn, + last_user_presence_request: None, + ctap1_cid, } } /// Checks if the SoC is compatible with USB drivers (older versions of Precursor's FPGA don't have the USB device core) @@ -156,6 +482,68 @@ impl XousEnv { } Ok(()) } + + /// The point of the attestation key is to delegate the question of the authenticity of your U2F + /// key effectively to a transaction that happens between the factory and the server. If the + /// attestation private key is an impenetrable secret and you can trust the factory, then the + /// attestation key effectively proves the U2F token was made by the factory to its specifications. + /// + /// In the case of Precursor's threat model, you don't trust the factory, and you assume there is + /// basically no such thing as an impenetrable secret. So an attestation key is equivalent to any + /// private key created entirely within your own device; its use proves that you're you, or at + /// least, someone has possession of your device and your unlock password. + /// + /// Thus, for Precursor, there is no delegation of the question of authenticity -- you + /// choose to verify the device to the level you're satisfied with. If you're happy to + /// just take it out of the box and use it without checking anything, that's your choice, + /// but you can also check everything as much as you like. + /// + /// As a result, the "attestation key" serves no role for Precursor users, other than to + /// identify them as Precursor users. A unique per-device key would furthermore allow + /// a server to uniquely identify each Precursor user. Therefore, we commit the private key + /// into this public repository so any and all Precursor users may share it. The more + /// widely it is used, the harder it becomes to de-anonymize a Precursor users. + /// + /// This key was generated using the script in `tools/gen_key_materials.sh`, and then printed + /// out by calling + /// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem` + pub fn attestation_check_init(&mut self) { + // check if the certs exist; if they don't, populate them. + // STORAGE_KEYS[0] is the private key; [1] is the public key + match self.store().find(crate::api::attestation_store::STORAGE_KEYS[0]) { + Ok(Some(_)) => {}, + Ok(None) | + Err(_) => { + let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap(); + self.store().insert( + crate::api::attestation_store::STORAGE_KEYS[0], + &pk + ).expect("pddb error initializing OpenSK"); + } + } + match self.store().find(crate::api::attestation_store::STORAGE_KEYS[1]) { + Ok(Some(_)) => {} + Ok(None) | + Err(_) => { + let der = decode_hex( + "308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\ + 082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\ + 55324620444959204341301e170d3232303630353138313233395a170d333230\ + 3630343138313233395a30293127302506035504030c1e507265637572736f72\ + 2053656c662d5665726966696564204465766963653059301306072a8648ce3d\ + 020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\ + 1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\ + 55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\ + 45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\ + e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\ + 1f0bdf82d1f4").unwrap(); + self.store().insert( + crate::api::attestation_store::STORAGE_KEYS[1], + &der + ).expect("pddb error initializing OpenSK"); + } + } + } } impl UserPresence for XousEnv { @@ -231,152 +619,42 @@ impl UserPresence for XousEnv { } } + /// A ctap1-specific call to see if a request was recently made + fn recently_requested(&mut self) -> bool { + if let Some(last_req) = self.last_user_presence_request { + if Instant::now().duration_since(last_req).as_millis() < crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() { + true + } else { + false + } + } else { + false + } + } /// Wait for user approval of a CTAP1-type credential /// In this flow, we try to map the `app_id` to a user-provided, memorable string /// We also give users the option to abort out of approving. - fn wait_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool { - let app_id_str = hex::encode(app_id); - let mut first_time = false; - let mut info = { - // fetch the application info, if it exists - let app_id_str = hex::encode(app_id); - log::info!("querying U2F record {}", app_id_str); - // add code to query the PDDB here to look for the k/v mapping of this app ID - match self.pddb.get( - U2F_APP_DICT, - &app_id_str, - None, true, false, - Some(256), Some(basis_change) - ) { - Ok(mut app_data) => { - let app_attr = app_data.attributes().unwrap(); - if app_attr.len != 0 { - let mut descriptor = Vec::::new(); - match app_data.read_to_end(&mut descriptor) { - Ok(_) => { - deserialize_app_info(descriptor) - } - Err(e) => {log::error!("Couldn't read app info: {:?}", e); None} - } - } else { - None - } - } - _ => { - log::info!("couldn't find key {}", app_id_str); - None - } - } - }.unwrap_or_else( - || { - // otherwise, create it - match self.modals - .alert_builder(t!("vault.u2f.give_app_name", xous::LANG)) - .field(None, None) - .build() - { - Ok(name) => { - first_time = true; - let info = AppInfo { - name: name.content()[0].content.to_string(), - notes: t!("vault.notes", xous::LANG).to_string(), - id: app_id, - ctime: crate::utc_now().timestamp() as u64, - atime: 0, - count: 0, - }; - info - } - _ => { - log::error!("couldn't get name for app"); - panic!("couldn't get name for app"); - } - } - } - ); - // request approval, if we didn't just create the record - if !first_time { - let mut request_str = String::from(&reason); - // we have some prior record of the app, human-format it - request_str.push_str(&format!("\n{}{}", - t!("vault.u2f.appinfo.name", xous::LANG), info.name - )); - request_str.push_str(&format!("\n{}", - crate::atime_to_str(info.atime) - )); - request_str.push_str(&format!("\n{}{}", - t!("vault.u2f.appinfo.authcount", xous::LANG), - info.count, - )); + fn poll_approval_ctap1(&mut self, reason: String, app_id: [u8; 32]) -> bool { + // update the user presence request parameter + self.last_user_presence_request = Some(Instant::now()); - self.modals.dynamic_notification( - Some(t!("vault.u2freq", xous::LANG)), - Some(&request_str), - ).unwrap(); - - // block until the user hits a key - match modals::dynamic_notification_blocking_listener(self.modals.token(), self.modals.conn()) { - Ok(Some(c)) => { - if c as u32 == 0x11 { // this is the F1 key - return false; - } else { - // any other key shall accept - } - }, - Ok(None) => { - log::trace!("kbhit exited or had no characters"); - return false; - }, - Err(e) => { - log::error!("error waiting for keyboard hit from blocking listener: {:?}", e); - return false; - }, - } - } - - // note the access - info.atime = crate::utc_now().timestamp() as u64; - info.count = info.count.saturating_add(1); - let ser = serialize_app_info(&info); - - // update the access time, by deleting the key and writing it back into the PDDB - let basis = match self.pddb.get( - U2F_APP_DICT, - &app_id_str, - None, true, true, - Some(256), Some(basis_change) - ) { - Ok(app_data) => { - let attr = app_data.attributes().expect("couldn't get attributes"); - attr.basis - } - Err(e) => { - log::error!("error updating app atime: {:?}", e); - return false; - } + let request = Ctap1Request { + reason: xous_ipc::String::from_str(&reason), + app_id, + approved: false }; - self.pddb.delete_key(U2F_APP_DICT, &app_id_str, Some(&basis)).ok(); - match self.pddb.get( - U2F_APP_DICT, - &app_id_str, - Some(&basis), true, true, - Some(256), Some(basis_change) - ) { - Ok(mut app_data) => { - app_data.write(&ser).expect("couldn't update atime"); - } - _ => log::error!("Error updating app atime"), + let mut buf = xous_ipc::Buffer::into_buf(request).expect("couldn't convert IPC structure"); + buf.lend_mut(self.ctap1_cid, Ctap1Op::PollPermission.to_u32().unwrap()).expect("couldn't make CTAP1 poll request"); + let response = buf.to_original::().expect("couldn't convert IPC structure"); + if response.approved { + // serialize the response straight back to the app info update path + // we can't "block" this path because if we take too long to respond, the web client will assume we have failed + let buf = xous_ipc::Buffer::into_buf(response).expect("couldn't convert IPC structure"); + buf.send(self.ctap1_cid, Ctap1Op::UpdateAppInfo.to_u32().unwrap()).expect("couldn't make CTAP1 info update"); + true + } else { + false } - self.pddb.sync().ok(); - - log::info!("sycing UI state..."); - xous::send_message( - self.main_cid, - xous::Message::new_scalar( - crate::VaultOp::ReloadDbAndFullRedraw.to_usize().unwrap(), - 0, 0, 0, 0) - ).unwrap(); - true } fn check_complete(&mut self) { diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index eea73061e..159dd84c4 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -69,7 +69,7 @@ UI concept: |-----------------| F1-F4: switch between functions using F-keys. Functions are: - - FIDO2 (U2F authenicators) + - FIDO2 (U2F authenticators) - TOTP (time based authenticators) - Vault (passwords) - Prefs (preferences) @@ -193,7 +193,7 @@ impl EndpointReplies { fn main() -> ! { 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(); @@ -207,6 +207,8 @@ fn main() -> ! { let item_lists = Arc::new(Mutex::new(ItemLists::new())); let action_active = Arc::new(AtomicBool::new(false)); let allow_host = Arc::new(AtomicBool::new(false)); + // Protects access to the openSK PDDB entries from simultaneous readout on the UX while OpenSK is updating it + let opensk_mutex = Arc::new(Mutex::new(0)); // spawn the actions server. This is responsible for grooming the UX elements. It // has to be in its own thread because it uses blocking modal calls that would cause @@ -218,7 +220,8 @@ fn main() -> ! { actions_sid, mode.clone(), item_lists.clone(), - action_active.clone() + action_active.clone(), + opensk_mutex.clone(), ); let actions_conn = xous::connect(actions_sid).unwrap(); @@ -226,6 +229,7 @@ fn main() -> ! { // spawn the FIDO2 USB handler let _ = thread::spawn({ let allow_host = allow_host.clone(); + let opensk_mutex = opensk_mutex.clone(); let conn = conn.clone(); move || { let xns = xous_names::XousNames::new().unwrap(); @@ -236,7 +240,11 @@ fn main() -> ! { let pddb = pddb::Pddb::new(); pddb.is_mounted_blocking(); - let env = XousEnv::new(conn); + let mut env = XousEnv::new(conn); + { + let mutex = opensk_mutex.lock().unwrap(); + env.attestation_check_init(); // ensures that our attestation keys have been created + } let mut replies = EndpointReplies::new(); // only run the main loop if the SoC is compatible if env.is_soc_compatible() { @@ -246,55 +254,31 @@ fn main() -> ! { /////////// // TODO: vendor command for gsora backup protocol has disappeared. /////////// - if let Some(mut packet) = replies.next_packet() { - // send and receive. - match ctap.env().main_hid_connection().send_and_maybe_recv(&mut packet.packet, SEND_TIMEOUT) { - Ok(SendOrRecvStatus::Timeout) => { - log::debug!("Sending packet timed out"); - // TODO: reset the ctap_hid state. - // Since sending the packet timed out, we cancel this reply. - break; - } - Ok(SendOrRecvStatus::Sent) => { - log::debug!("Sent packet"); - } - Ok(SendOrRecvStatus::Received) => { - log::debug!("Received another packet"); - - // Copy to incoming packet to local buffer to be consistent - // with the receive flow. - pkt_request = Some(packet.packet); - } - Err(_) => panic!("Error sending packet"), - } - } else { - // receive - let mut rx_buf = [0u8; 64]; - pkt_request = match ctap.env().main_hid_connection().recv_with_timeout(&mut rx_buf, KEEPALIVE_DELAY) - { - SendOrRecvStatus::Received => { - log::debug!("Received packet"); - Some(rx_buf) - } - SendOrRecvStatus::Sent => { - panic!("Returned transmit status on receive"); - } - SendOrRecvStatus::Timeout => { - None - }, - }; - } - - if let Some(pkt_request) = pkt_request.take() { - let reply = ctap.process_hid_packet(&pkt_request, Transport::MainHid, Instant::now()); - if reply.has_data() { - // Update endpoint with the reply. - for ep in replies.replies.iter_mut() { - if ep.reply.has_data() { - log::warn!("Warning overwriting existing reply"); + match ctap.env().main_hid_connection().u2f_wait_incoming() { + Ok(msg) => { + let mutex = opensk_mutex.lock().unwrap(); + log::debug!("Received U2F packet"); + let reply = ctap.process_hid_packet(&msg.packet, Transport::MainHid, Instant::now()); + for pkt_reply in reply { + let mut reply = RawFidoMsg::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = ctap.env().main_hid_connection().u2f_send(reply); + match status { + Ok(()) => { + log::debug!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } + } + } + // mutex goes out of scope here, and is released + } + Err(e) => { + match e { + _ => { + log::warn!("FIDO listener got an error: {:?}", e); } - ep.reply = reply; - break; } } } From c083a9ca85b5a83bb11ce863b26a6ec85e676c40 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 00:55:32 +0800 Subject: [PATCH 22/80] chore: fix typos --- services/pddb/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index a652a6afc..a3c4f4f5d 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -217,7 +217,7 @@ impl Pddb { } } /// This is a non-blocking message that permanently halts the PDDB server by wedging it in an infinite loop. - /// It is meant to be called prior to system shutdows or backups, to ensure that other auto-mounting procesess + /// It is meant to be called prior to system shutdows or backups, to ensure that other auto-mounting processes /// don't undo the shutdown procedure because of an ill-timed "cron" job (the system doesn't literally have /// a cron daemon, but it does have the notion of long-running background jobs that might do something like /// trigger an NTP update, which would then try to write the updated time to the PDDB). @@ -819,14 +819,14 @@ impl Pddb { The main server then receives this, and if there are no tokens pending for `DictBulkRead`, it allocates a 128-bit token for this transaction and records it, locking out any other potential requests. - If a token is already allocated, it responds immedaitely with an error code. + If a token is already allocated, it responds immediately with an error code. The main server proceeds to serialize keys out of the dictionary into `PddbKeyRecord` structures. It will serialize exactly as many as can fit into the given buffer. In case the data is larger than the allocated buffer, the data will always be returned as `None`; in case the data is larger than the available space, the serialization stops, and the record is returned. - Subsequent calls from the client to the main server requries a `PddbDictRequest` structure + Subsequent calls from the client to the main server requires a `PddbDictRequest` structure to be in the buffer, but only the "token" field is considered. The rest of the fields are disregarded; changing the requested dictionary or other parameters has no impact on the call trajectory. From b7d5eca7201bbf442eac7590dad3e3e89b16d0e7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 13:50:06 +0800 Subject: [PATCH 23/80] fix missing corner case in cache refill --- services/pddb/src/backend/dictionary.rs | 42 +++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index b2eae3e35..b9cdd3079 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -376,26 +376,28 @@ impl DictCacheEntry { pub (crate) fn refill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, data_cache: &mut PlaintextCache, key_name: &str) { if let Some(kcache) = self.keys.get_mut(key_name) { - if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { - let ksp = &mut self.small_pool[pool_index]; - assert!(ksp.contents.contains(&key_name.to_string()), "refill called on a non-existent key. This is an illegal state."); - // now grab the *data* referred to by this key. Maybe this is a "bad" idea -- this can really eat up RAM fast to hold - // all the small pool data right away, but let's give it a try and see how it works. Later on we can always skip this. - // manage a separate small cache for data blocks, under the theory that small keys tend to be packed together - let data_vaddr = small_storage_base_vaddr_from_indices(self.index, pool_index); - data_cache.fill(hw, v2p_map, cipher, &self.aad, VirtAddr::new(data_vaddr).unwrap()); - if let Some(page) = data_cache.data.as_ref() { - let start_offset = size_of::() + (kcache.start % VPAGE_SIZE as u64) as usize; - let mut data = page[start_offset..start_offset + kcache.len as usize].to_vec(); - data.reserve_exact((kcache.reserved - kcache.len) as usize); - kcache.data = Some(KeyCacheData::Small( - KeySmallData { - clean: true, - data - } - )); - } else { - log::error!("Key {}'s data region at pp: {:x?} va: {:x} is unreadable", key_name, data_cache.tag, kcache.start); + if kcache.flags.valid() { // invalid keys were previously deleted, and have nothing to fill + if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { + let ksp = &mut self.small_pool[pool_index]; + assert!(ksp.contents.contains(&key_name.to_string()), "refill called on a non-existent key. This is an illegal state."); + // now grab the *data* referred to by this key. Maybe this is a "bad" idea -- this can really eat up RAM fast to hold + // all the small pool data right away, but let's give it a try and see how it works. Later on we can always skip this. + // manage a separate small cache for data blocks, under the theory that small keys tend to be packed together + let data_vaddr = small_storage_base_vaddr_from_indices(self.index, pool_index); + data_cache.fill(hw, v2p_map, cipher, &self.aad, VirtAddr::new(data_vaddr).unwrap()); + if let Some(page) = data_cache.data.as_ref() { + let start_offset = size_of::() + (kcache.start % VPAGE_SIZE as u64) as usize; + let mut data = page[start_offset..start_offset + kcache.len as usize].to_vec(); + data.reserve_exact((kcache.reserved - kcache.len) as usize); + kcache.data = Some(KeyCacheData::Small( + KeySmallData { + clean: true, + data + } + )); + } else { + log::error!("Key {}'s data region at pp: {:x?} va: {:x} is unreadable", key_name, data_cache.tag, kcache.start); + } } } } else { From 8974a86f243dd9034188849df92f6f923f95682f Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 15:34:54 +0800 Subject: [PATCH 24/80] add migration from old to new version confirmed this works for U2F and FIDO2 keys however, the PIN is reset. --- apps/vault/locales/i18n.json | 21 ++-- apps/vault/src/env/xous/mod.rs | 71 +---------- apps/vault/src/main.rs | 34 ++++-- apps/vault/src/migration_v1.rs | 207 +++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 88 deletions(-) create mode 100644 apps/vault/src/migration_v1.rs diff --git a/apps/vault/locales/i18n.json b/apps/vault/locales/i18n.json index 24cbbdea1..6f09c41cf 100644 --- a/apps/vault/locales/i18n.json +++ b/apps/vault/locales/i18n.json @@ -286,6 +286,13 @@ "ja": "シークレットベースのロックを解除", "zh": "解锁秘密基础" }, + "vault.migration_error": { + "en": "Migration from previous FIDO database encountered errors:", + "en-tts": "Migration from previous FIDO database encountered errors:", + "fr": "La migration depuis la base de données FIDO précédente a rencontré des erreurs: *MT*", + "ja": "以前の FIDO データベースからの移行でエラーが発生しました:", + "zh": "从以前的 FIDO 数据库迁移遇到错误:" + }, "vault.newitem.approve": { "en": "Is this password okay?", "en-tts": "Is this password okay?", @@ -461,13 +468,6 @@ "ja": "アプリ名:", "zh": "应用名称:" }, - "vault.u2f.appinfo.newapp": { - "en": "New app: ", - "en-tts": "New app: ", - "fr": "Nouvelle application: ", - "ja": "新しいアプリ: ", - "zh": "新应用: " - }, "vault.u2f.appinfo.never": { "en": "never", "en-tts": "never", @@ -475,6 +475,13 @@ "ja": "使われたことがない", "zh": "没用过" }, + "vault.u2f.appinfo.newapp": { + "en": "New app: ", + "en-tts": "New app: ", + "fr": "Nouvelle application: ", + "ja": "新しいアプリ: ", + "zh": "新应用: " + }, "vault.u2f.appinfo.seconds_ago": { "en": " secs ago", "en-tts": " seconds ago", diff --git a/apps/vault/src/env/xous/mod.rs b/apps/vault/src/env/xous/mod.rs index c9552e9be..964d04fc2 100644 --- a/apps/vault/src/env/xous/mod.rs +++ b/apps/vault/src/env/xous/mod.rs @@ -19,7 +19,6 @@ use pddb::Pddb; use modals::Modals; use locales::t; use std::io::{Read, Write}; -use std::num::ParseIntError; use num_traits::*; use crate::ctap::hid::{CtapHid, KeepaliveStatus, ProcessedPacket, CtapHidCommand}; @@ -29,13 +28,6 @@ pub const U2F_APP_DICT: &'static str = "fido.u2fapps"; const KEEPALIVE_DELAY: Duration = Duration::from_millis(KEEPALIVE_DELAY_MS); mod storage; -fn decode_hex(s: &str) -> Result, ParseIntError> { - (0..s.len()) - .step_by(2) - .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) - .collect() -} - pub struct XousHidConnection { pub endpoint: usb_device_xous::UsbHid, } @@ -176,7 +168,7 @@ impl XousEnv { if (crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) > (now - request_start) { let new_remaining = ((crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() as u64) - (now - request_start)) / 1000; if new_remaining != last_remaining { - log::trace!("update timer"); + log::info!("countdown: {}", new_remaining); last_remaining = new_remaining; let mut to_request_str = request_str.to_string(); to_request_str.push_str( @@ -483,67 +475,6 @@ impl XousEnv { Ok(()) } - /// The point of the attestation key is to delegate the question of the authenticity of your U2F - /// key effectively to a transaction that happens between the factory and the server. If the - /// attestation private key is an impenetrable secret and you can trust the factory, then the - /// attestation key effectively proves the U2F token was made by the factory to its specifications. - /// - /// In the case of Precursor's threat model, you don't trust the factory, and you assume there is - /// basically no such thing as an impenetrable secret. So an attestation key is equivalent to any - /// private key created entirely within your own device; its use proves that you're you, or at - /// least, someone has possession of your device and your unlock password. - /// - /// Thus, for Precursor, there is no delegation of the question of authenticity -- you - /// choose to verify the device to the level you're satisfied with. If you're happy to - /// just take it out of the box and use it without checking anything, that's your choice, - /// but you can also check everything as much as you like. - /// - /// As a result, the "attestation key" serves no role for Precursor users, other than to - /// identify them as Precursor users. A unique per-device key would furthermore allow - /// a server to uniquely identify each Precursor user. Therefore, we commit the private key - /// into this public repository so any and all Precursor users may share it. The more - /// widely it is used, the harder it becomes to de-anonymize a Precursor users. - /// - /// This key was generated using the script in `tools/gen_key_materials.sh`, and then printed - /// out by calling - /// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem` - pub fn attestation_check_init(&mut self) { - // check if the certs exist; if they don't, populate them. - // STORAGE_KEYS[0] is the private key; [1] is the public key - match self.store().find(crate::api::attestation_store::STORAGE_KEYS[0]) { - Ok(Some(_)) => {}, - Ok(None) | - Err(_) => { - let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap(); - self.store().insert( - crate::api::attestation_store::STORAGE_KEYS[0], - &pk - ).expect("pddb error initializing OpenSK"); - } - } - match self.store().find(crate::api::attestation_store::STORAGE_KEYS[1]) { - Ok(Some(_)) => {} - Ok(None) | - Err(_) => { - let der = decode_hex( - "308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\ - 082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\ - 55324620444959204341301e170d3232303630353138313233395a170d333230\ - 3630343138313233395a30293127302506035504030c1e507265637572736f72\ - 2053656c662d5665726966696564204465766963653059301306072a8648ce3d\ - 020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\ - 1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\ - 55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\ - 45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\ - e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\ - 1f0bdf82d1f4").unwrap(); - self.store().insert( - crate::api::attestation_store::STORAGE_KEYS[1], - &der - ).expect("pddb error initializing OpenSK"); - } - } - } } impl UserPresence for XousEnv { diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 159dd84c4..08e83cea4 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -10,6 +10,7 @@ mod totp; mod prereqs; mod vendor_commands; mod storage; +mod migration_v1; use locales::t; @@ -30,6 +31,7 @@ use crate::vendor_commands::VendorSession; use ux::framework::{VaultUx, DEFAULT_FONT, FONT_LIST, name_to_style}; use xous_ipc::Buffer; use xous::{send_message, Message}; +use pddb::Pddb; use usbd_human_interface_device::device::fido::*; use num_traits::*; @@ -193,7 +195,7 @@ impl EndpointReplies { fn main() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); log::info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -239,12 +241,19 @@ fn main() -> ! { // block until the PDDB is mounted let pddb = pddb::Pddb::new(); pddb.is_mounted_blocking(); + // Attempt a migration, if necessary. If none is necessary this call is fairly lightweight. + match crate::migration_v1::migrate(&pddb) { + Ok(_) => {}, + Err(e) => { + log::warn!("Migration encountered errors! {:?}", e); + let modals = modals::Modals::new(&xns).unwrap(); + modals.show_notification( + &format!("{}\n{:?}", t!("vault.migration_error", xous::LANG), e), None + ).ok(); + } + }; let mut env = XousEnv::new(conn); - { - let mutex = opensk_mutex.lock().unwrap(); - env.attestation_check_init(); // ensures that our attestation keys have been created - } let mut replies = EndpointReplies::new(); // only run the main loop if the SoC is compatible if env.is_soc_compatible() { @@ -323,6 +332,7 @@ fn main() -> ! { let modals = modals::Modals::new(&xns).unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap(); + let mut first_time = true; loop { let msg = xous::receive_message(sid).unwrap(); let opcode: Option = FromPrimitive::from_usize(msg.body.id()); @@ -450,11 +460,14 @@ fn main() -> ! { // HID is always selected if the vault is foregrounded vaultux.ensure_hid(); allow_redraw = true; - // Update database & configs - send_message(actions_conn, - Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0) - ).ok(); - vaultux.update_mode(); + if first_time { + // Populate the initial fields, just the first time + send_message(actions_conn, + Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + vaultux.update_mode(); + first_time = false; + } } } }), @@ -561,4 +574,3 @@ fn main() -> ! { log::trace!("quitting"); xous::terminate_process(0) } - diff --git a/apps/vault/src/migration_v1.rs b/apps/vault/src/migration_v1.rs new file mode 100644 index 000000000..b0a816e94 --- /dev/null +++ b/apps/vault/src/migration_v1.rs @@ -0,0 +1,207 @@ +use std::io::{Read, Write}; +use vault::ctap::storage::key; +use std::num::ParseIntError; +use pddb::Pddb; + +const FIDO_DICT: &'static str = "fido.cfg"; +const FIDO_CRED_DICT: &'static str = "fido.cred"; +const FIDO_PERSISTENT_DICT: &'static str = "fido.persistent"; + +fn decode_hex(s: &str) -> Result, ParseIntError> { + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) + .collect() +} +/// The point of the attestation key is to delegate the question of the authenticity of your U2F +/// key effectively to a transaction that happens between the factory and the server. If the +/// attestation private key is an impenetrable secret and you can trust the factory, then the +/// attestation key effectively proves the U2F token was made by the factory to its specifications. +/// +/// In the case of Precursor's threat model, you don't trust the factory, and you assume there is +/// basically no such thing as an impenetrable secret. So an attestation key is equivalent to any +/// private key created entirely within your own device; its use proves that you're you, or at +/// least, someone has possession of your device and your unlock password. +/// +/// Thus, for Precursor, there is no delegation of the question of authenticity -- you +/// choose to verify the device to the level you're satisfied with. If you're happy to +/// just take it out of the box and use it without checking anything, that's your choice, +/// but you can also check everything as much as you like. +/// +/// As a result, the "attestation key" serves no role for Precursor users, other than to +/// identify them as Precursor users. A unique per-device key would furthermore allow +/// a server to uniquely identify each Precursor user. Therefore, we commit the private key +/// into this public repository so any and all Precursor users may share it. The more +/// widely it is used, the harder it becomes to de-anonymize a Precursor users. +/// +/// This key was generated using the script in `tools/gen_key_materials.sh`, and then printed +/// out by calling +/// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem` +pub fn migrate(pddb: &Pddb) -> Result<(), xous::Error> { + if !pddb.list_dict(None).unwrap().contains(&persistent_store::store::OPENSK2_DICT.to_string()) { + // build the attestation keys + let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap(); + pddb.get( + persistent_store::store::OPENSK2_DICT, + &vault::api::attestation_store::STORAGE_KEYS[0].to_string(), + None, + true, true, Some(32), None:: + ).unwrap().write_all(&pk).map_err(|_| xous::Error::AccessDenied)?; + let der = decode_hex( + "308201423081e9021449a3e2a4e1078eae2f1a18567f0a734b09db2478300a06\ + 082a8648ce3d040302301f311d301b06035504030c14507265637572736f7220\ + 55324620444959204341301e170d3232303630353138313233395a170d333230\ + 3630343138313233395a30293127302506035504030c1e507265637572736f72\ + 2053656c662d5665726966696564204465766963653059301306072a8648ce3d\ + 020106082a8648ce3d030107034200042d8fad76c25851ee70ae651ab2361bee\ + 1b5d93769aff99d95ad112871060f2f342fb79566fee9e1bade250a29cd65012\ + 55e731531755284bfcbe85ada1f39f44300a06082a8648ce3d04030203480030\ + 45022100d27e39187da5efaa376254329499bb705f7188dd8c78e0725c7ed28d\ + e5d218e1022070853e1a43707298e07ebe9b9eb7cd5839b794db4c3d22209554\ + 1f0bdf82d1f4").unwrap(); + pddb.get( + persistent_store::store::OPENSK2_DICT, + &vault::api::attestation_store::STORAGE_KEYS[1].to_string(), + None, + true, true, None, None:: + ).unwrap().write_all(&der).map_err(|_| xous::Error::AccessDenied)?; + + // port the master secret + migrate_one(pddb, FIDO_DICT, "CRED_RANDOM_SECRET", key::CRED_RANDOM_SECRET)?; + // port AAGUID + migrate_one(pddb, FIDO_PERSISTENT_DICT, "AAGUID", key::AAGUID)?; + // port master keys + migrate_one(pddb, FIDO_DICT, "MASTER_KEYS", key::_RESERVED_KEY_STORE)?; + + // port other settings + // Turns out that porting over the PIN is a mistake. Just clear it. + + // migrate_one(pddb, FIDO_DICT, "MIN_PIN_LENGTH", key::MIN_PIN_LENGTH)?; + // migrate_one(pddb, FIDO_DICT, "PIN_RETRIES", key::PIN_RETRIES)?; + migrate_one(pddb, FIDO_DICT, "GLOBAL_SIGNATURE_COUNTER", key::GLOBAL_SIGNATURE_COUNTER)?; + // PIN_HASH requires appending a length to the end. This is an `advisory` field, so we...just make up a value of 8. + // This may not actually be the length used, but it's likely to be long enough to avoid forcing a re-PIN event because + // the PIN hash is too short. + // migrate_pin(pddb)?; + + // migrate credentials + let creds = match pddb.list_keys(FIDO_CRED_DICT, None) { + Ok(list) => list, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => Vec::new(), + _ => return Err(xous::Error::InternalError) + } + }; + let expected_creds = creds.len(); + let mut successful_migrations = 0; + let cred_start = key::CREDENTIALS.start; + for cred in creds { + let mut key = pddb.get( + FIDO_CRED_DICT, + &cred, + None, false, false, None, None:: + ).map_err(|_| xous::Error::AccessDenied)?; + let mut data = Vec::::new(); + key.read_to_end(&mut data).unwrap(); + match vault::ctap::storage::deserialize_credential(&data) { + Some(pk) => { + log::debug!("Valid pk: {:?}", pk); + let migrated_data = vault::ctap::storage::serialize_credential(pk).map_err(|_| xous::Error::InternalError)?; + let mut new_key = pddb.get( + persistent_store::store::OPENSK2_DICT, + &(cred_start + successful_migrations).to_string(), + None, true, true, Some(migrated_data.len()), None:: + ).map_err(|_| xous::Error::AccessDenied)?; + new_key.write_all(&migrated_data).map_err(|_| xous::Error::OutOfMemory)?; + successful_migrations += 1; + log::info!("Migrated credential {}:{}", FIDO_CRED_DICT, cred); + } + None => { + log::warn!("Credential {}:{} did not deserialize, cannot migrate!", FIDO_CRED_DICT, cred); + } + } + } + pddb.sync().ok(); + if successful_migrations == expected_creds { + Ok(()) + } else { + Err(xous::Error::InvalidString) + } + } else { + // OpenSK dict already exists; don't migrate anything. + Ok(()) + } +} + +/// Only migrates the data if it exists. Does not return an error if the record does not exist +fn migrate_one ( + pddb: &Pddb, + legacy_dict: &str, + legacy_key: &str, + new_key: usize, +) -> Result<(), xous::Error> { + match pddb.get( + legacy_dict, + legacy_key, + None, false, false, None, None:: + ) { + Ok(mut key) => { + let mut data = Vec::::new(); + let len = key.read_to_end(&mut data).map_err(|_| xous::Error::AccessDenied)?; + let mut new_key = pddb.get( + persistent_store::store::OPENSK2_DICT, + &new_key.to_string(), + None, true, true, Some(len), None:: + ).map_err(|_| xous::Error::AccessDenied)?; + new_key.write_all(&data).map_err(|_| xous::Error::OutOfMemory)?; + log::info!("successfully migrated {}:{}", legacy_dict, legacy_key); + Ok(()) + }, + Err(e) => { + match e.kind() { + std::io::ErrorKind::NotFound => { + log::info!("migration skipping {}:{}, as it does not exist", legacy_dict, legacy_key); + Ok(()) + }, + _ => Err(xous::Error::InternalError) + } + } + } +} + +/// Handle the special case of the PIN_HASH +fn migrate_pin ( + pddb: &Pddb, +) -> Result<(), xous::Error> { + let legacy_dict = FIDO_DICT; + let legacy_key = "PIN_HASH"; + let new_key = key::PIN_PROPERTIES; + match pddb.get( + legacy_dict, + legacy_key, + None, false, false, None, None:: + ) { + Ok(mut key) => { + let mut data = Vec::::new(); + let len = key.read_to_end(&mut data).map_err(|_| xous::Error::AccessDenied)? + 1; + data.push(5); // add a bogus "length" advisory field of 8 + let mut new_key = pddb.get( + persistent_store::store::OPENSK2_DICT, + &new_key.to_string(), + None, true, true, Some(len), None:: + ).map_err(|_| xous::Error::AccessDenied)?; + new_key.write_all(&data).map_err(|_| xous::Error::OutOfMemory)?; + log::info!("successfully migrated {}:{}", legacy_dict, legacy_key); + Ok(()) + }, + Err(e) => { + match e.kind() { + std::io::ErrorKind::NotFound => { + log::info!("migration skipping {}:{}, as it does not exist", legacy_dict, legacy_key); + Ok(()) + }, + _ => Err(xous::Error::InternalError) + } + } + } +} \ No newline at end of file From e9ae3fe72b44cbf0218e6ae8f576e96d61bed8b1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 22:36:15 +0800 Subject: [PATCH 25/80] fix key deletion in fido2 --- apps/vault/src/actions.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 47fcf70fb..3f2533bf8 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -502,14 +502,18 @@ impl<'a> ActionManager<'a> { if choice.is_none() { // we're dealing with FIDO stuff, use the custom code path - match self.pddb.borrow().get(U2F_APP_DICT, entry.key_name.as_str().unwrap_or("UTF8-error"), - None, false, false, None, None:: + let dictionary = match entry.key_name.as_str().unwrap_or("UTF8-error").parse::() { + Ok(fido_key) => persistent_store::store::OPENSK2_DICT, + Err(_) => U2F_APP_DICT, + }; + match self.pddb.borrow().get(dictionary, entry.key_name.as_str().unwrap_or("UTF8-error"), + None, false, false, None, None:: ) { Ok(candidate) => { let attr = candidate.attributes().expect("couldn't get key attributes"); match self.pddb.borrow() .delete_key( - U2F_APP_DICT, + dictionary, entry.key_name.as_str().unwrap_or("UTF8-error"), Some(&attr.basis) ) { @@ -519,7 +523,7 @@ impl<'a> ActionManager<'a> { Err(e) => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } } - Err(e) => { + Err(e) => { self.report_err(t!("vault.error.not_found", xous::LANG), Some(e)); } } From fd4e402d98f2836f93685c6b559189bd9263f8e6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 22:41:03 +0800 Subject: [PATCH 26/80] refine the heuristic for detecting keys --- apps/vault/src/actions.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 3f2533bf8..d9c75e056 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -502,9 +502,15 @@ impl<'a> ActionManager<'a> { if choice.is_none() { // we're dealing with FIDO stuff, use the custom code path - let dictionary = match entry.key_name.as_str().unwrap_or("UTF8-error").parse::() { - Ok(fido_key) => persistent_store::store::OPENSK2_DICT, - Err(_) => U2F_APP_DICT, + let dictionary = match usize::from_str_radix(entry.key_name.as_str().unwrap_or("UTF8-error"), 10) { + Ok(fido_key) => { + if vault::ctap::storage::key::CREDENTIALS.contains(&fido_key) { + persistent_store::store::OPENSK2_DICT // heuristic: all fido2 keys are simple integers + } else { + U2F_APP_DICT + } + } + Err(_) => U2F_APP_DICT, // u2f keys are long hex strings }; match self.pddb.borrow().get(dictionary, entry.key_name.as_str().unwrap_or("UTF8-error"), None, false, false, None, None:: From 78866199ea46f0ff53da6dbfba315b6373beea4f Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 25 Jan 2023 23:17:30 +0800 Subject: [PATCH 27/80] fix zero-length key readback error in bulk reads zero-length keys return a data section of None, instead of Some(), as the latter representation deserializes to a null pointer panic. --- services/pddb/src/api.rs | 7 ++++--- services/pddb/src/lib.rs | 3 ++- services/pddb/src/main.rs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 3fab0e010..df88f3b9b 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -376,9 +376,10 @@ pub struct PddbKeyRecord { pub age: usize, pub index: NonZeroU32, pub basis: String, - /// If this is None, it means that the data read exceeded the bulk read limit - /// and the record must be explicitly re-read with a non-bulk call to fetch its data. - /// If the key is actually zero-length, a zero-length Some(Vec) is returned. + /// If this is None, it means either that the data read exceeded the bulk read limit + /// and the record must be explicitly re-read with a non-bulk call to fetch its data, + /// or the key has zero length. Check the `len` field to differentiate the two. + /// (Turns out that `rkyv` crashes when deserializing a zero-length Vec) pub data: Option>, } diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index a3c4f4f5d..227de45d3 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -801,7 +801,8 @@ impl Pddb { /// Retrieve an entire dictionary of data in a single call. Will return data records up to but not /// over a total of `size_limit`. Keys that exceed the limit are still enumerated, but their - /// data sections are `None`, instead of `Some(Vec::)`. + /// data sections are `None`, instead of `Some(Vec::)`. Keys that are zero-length are also returned + /// with a data section of `None`; check the `len` field of the PddbKeyRecord to determine which is which. /// Defaults to a size limit of up to 32k of bulk data returned, if it is not explicitly specified. pub fn read_dict(&self, dict: &str, basis: Option<&str>, size_limit: Option) -> Result> { // about the biggest we can move around in Precursor and not break heap. diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 97f7a320e..7f4b7e0f4 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1586,7 +1586,7 @@ fn wrapped_main() -> ! { age: attr.age, index: attr.index, basis: attr.basis, - data: Some(d) + data: if d.len() > 0 { Some(d) } else { None } }; state.read_total += attr.len; // commit the read length early let (prebuf, sbuf) = buf.split_at_mut(size_of::()*2); From 10a64da826deecc1b5da59ba72d930660a89842f Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 02:38:03 +0800 Subject: [PATCH 28/80] allow vendor commands; get vaultbackup-rs working --- apps/vault/libraries/backup/src/lib.rs | 4 +- apps/vault/src/actions.rs | 3 +- apps/vault/src/ctap/hid/mod.rs | 17 ++- apps/vault/src/ctap/main_hid.rs | 22 +++- apps/vault/src/ctap/timed_permission.rs | 2 + apps/vault/src/env/xous/mod.rs | 7 +- apps/vault/src/lib.rs | 6 +- apps/vault/src/main.rs | 150 +++++++++++++----------- apps/vault/src/migration_v1.rs | 21 +++- apps/vault/src/shims.rs | 138 ---------------------- apps/vault/src/vault_api.rs | 4 + apps/vault/src/vendor_commands.rs | 8 +- 12 files changed, 146 insertions(+), 236 deletions(-) delete mode 100644 apps/vault/src/shims.rs diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs index b08e82d1b..47b387292 100644 --- a/apps/vault/libraries/backup/src/lib.rs +++ b/apps/vault/libraries/backup/src/lib.rs @@ -134,7 +134,9 @@ impl TryFrom for TotpEntry { let digit_count = extract_unsigned(digit_count.unwrap())? as u32; let algorithm: HashAlgorithms = algorithm.unwrap().try_into()?; let name = extract_string(name.unwrap())?; - let hotp = extract_bool(hotp.unwrap())?; + let hotp = extract_bool(hotp.unwrap_or( + cbor::Value::Simple(cbor::SimpleValue::FalseValue) + )).unwrap_or(false); Ok(TotpEntry { step_seconds, diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index d9c75e056..583740cff 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -1070,6 +1070,7 @@ impl<'a> ActionManager<'a> { } }, } + drop(mutex); } } VaultMode::Totp => { @@ -1264,8 +1265,6 @@ impl<'a> ActionManager<'a> { #[cfg(feature="vault-testing")] pub(crate) fn populate_tests(&mut self) { - use serialize_app_info; - self.modals.dynamic_notification(Some("Creating test entries..."), None).ok(); let words = [ "bunnie", "foo", "turtle.net", "Fox.ng", "Bear", "dog food", "Cat.com", "FUzzy", "1off", "www_test_site_com/long_name/stupid/foo.htm", diff --git a/apps/vault/src/ctap/hid/mod.rs b/apps/vault/src/ctap/hid/mod.rs index 4f969b21e..a720def8f 100644 --- a/apps/vault/src/ctap/hid/mod.rs +++ b/apps/vault/src/ctap/hid/mod.rs @@ -31,6 +31,10 @@ use std::time::Duration; #[cfg(test)] use enum_iterator::IntoEnumIterator; +use crate::vault_api::{ + COMMAND_BACKUP_TOTP_CODES, COMMAND_RESTORE_TOTP_CODES, COMMAND_RESET_SESSION +}; + pub type HidPacket = [u8; 64]; pub type ChannelID = [u8; 4]; @@ -51,6 +55,9 @@ pub enum CtapHidCommand { Keepalive = 0x3B, Error = 0x3F, // The vendor range starts here, going from 0x40 to 0x7F. + RestoreTotpCodes = COMMAND_RESTORE_TOTP_CODES as _, + BackupTotpCodes = COMMAND_BACKUP_TOTP_CODES as _, + ResetSession = COMMAND_RESET_SESSION as _, } impl From for CtapHidCommand { @@ -64,6 +71,9 @@ impl From for CtapHidCommand { x if x == CtapHidCommand::Cbor as u8 => CtapHidCommand::Cbor, x if x == CtapHidCommand::Cancel as u8 => CtapHidCommand::Cancel, x if x == CtapHidCommand::Keepalive as u8 => CtapHidCommand::Keepalive, + x if x == CtapHidCommand::RestoreTotpCodes as u8 => CtapHidCommand::RestoreTotpCodes, + x if x == CtapHidCommand::BackupTotpCodes as u8 => CtapHidCommand::BackupTotpCodes, + x if x == CtapHidCommand::ResetSession as u8 => CtapHidCommand::ResetSession, // This includes the actual error code 0x3F. Error is not used for incoming packets in // the specification, so we can safely reuse it for unknown bytes. _ => CtapHidCommand::Error, @@ -237,7 +247,7 @@ impl CtapHid { ) -> Option { match self.assembler.parse_packet(env, packet, clock_value) { Ok(Some(message)) => { - log::debug!("Received message: {:02x?}", message); + log::trace!("Received message: {:02x?}", message); self.preprocess_message(message) } Ok(None) => { @@ -315,6 +325,11 @@ impl CtapHid { None } CtapHidCommand::Wink => Some(message), + CtapHidCommand::BackupTotpCodes | + CtapHidCommand::RestoreTotpCodes | + CtapHidCommand::ResetSession => { + Some(message) + } _ => { // Unknown or unsupported command. Some(CtapHid::error_message(cid, CtapHidError::InvalidCmd)) diff --git a/apps/vault/src/ctap/main_hid.rs b/apps/vault/src/ctap/main_hid.rs index 74244f9ed..6afed98ea 100644 --- a/apps/vault/src/ctap/main_hid.rs +++ b/apps/vault/src/ctap/main_hid.rs @@ -30,6 +30,11 @@ pub struct MainHid { wink_permission: TimedPermission, } +pub enum HidIterType { + Ctap(HidPacketIterator), + Vendor(Message), +} + impl MainHid { const WINK_TIMEOUT_DURATION: Duration = Duration::from_millis(5000); @@ -56,13 +61,22 @@ impl MainHid { packet: &HidPacket, now: Instant, ctap_state: &mut CtapState, - ) -> HidPacketIterator { + ) -> HidIterType { if let Some(message) = self.hid.parse_packet(env, packet, now) { let processed_message = self.process_message(env, message, now, ctap_state); - log::debug!("Sending message: {:02x?}", processed_message); - CtapHid::split_message(processed_message) + match processed_message.cmd { + CtapHidCommand::RestoreTotpCodes | + CtapHidCommand::BackupTotpCodes | + CtapHidCommand::ResetSession => { + HidIterType::Vendor(processed_message) + } + _ => { + log::trace!("Sending message: {:02x?}", processed_message); + HidIterType::Ctap(CtapHid::split_message(processed_message)) + } + } } else { - HidPacketIterator::none() + HidIterType::Ctap(HidPacketIterator::none()) } } diff --git a/apps/vault/src/ctap/timed_permission.rs b/apps/vault/src/ctap/timed_permission.rs index e17ec3210..f0e833047 100644 --- a/apps/vault/src/ctap/timed_permission.rs +++ b/apps/vault/src/ctap/timed_permission.rs @@ -57,6 +57,7 @@ impl TimedPermission { #[cfg(feature = "with_ctap1")] #[derive(Debug)] +#[allow(dead_code)] pub struct U2fUserPresenceState { // If user presence was recently requested, its timeout is saved here. needs_up: TimedPermission, @@ -128,6 +129,7 @@ impl U2fUserPresenceState { self.has_up = self.has_up.check_expiration(now); } #[cfg(feature="xous")] + #[allow(dead_code)] pub fn check_expiration(&mut self, _now: Instant) { } } diff --git a/apps/vault/src/env/xous/mod.rs b/apps/vault/src/env/xous/mod.rs index 964d04fc2..40e2ccf90 100644 --- a/apps/vault/src/env/xous/mod.rs +++ b/apps/vault/src/env/xous/mod.rs @@ -15,7 +15,6 @@ use ctap_crypto::rng256::XousRng256; use xous_names::XousNames; use crate::env::xous::storage::XousUpgradeStorage; use usbd_human_interface_device::device::fido::*; -use pddb::Pddb; use modals::Modals; use locales::t; use std::io::{Read, Write}; @@ -92,8 +91,6 @@ pub struct XousEnv { #[cfg(feature = "vendor_hid")] vendor_connection: XousHidConnection, modals: Modals, - pddb: Pddb, - main_cid: xous::CID, last_user_presence_request: Option::, ctap1_cid: xous::CID, } @@ -212,7 +209,7 @@ impl XousEnv { } // determine the application info string let app_id_str = hex::encode(request.app_id); - if let Some(mut info) = { + if let Some(info) = { // fetch the application info, if it exists log::debug!("querying U2F record {}", app_id_str); // add code to query the PDDB here to look for the k/v mapping of this app ID @@ -413,8 +410,6 @@ impl XousEnv { endpoint: UsbEndpoint::VendorHid, }, modals: modals::Modals::new(&xns).unwrap(), - pddb: pddb::Pddb::new(), - main_cid: conn, last_user_presence_request: None, ctap1_cid, } diff --git a/apps/vault/src/lib.rs b/apps/vault/src/lib.rs index 710340319..1351bf5b5 100644 --- a/apps/vault/src/lib.rs +++ b/apps/vault/src/lib.rs @@ -17,7 +17,7 @@ #[macro_use] extern crate arrayref; -use crate::ctap::hid::{HidPacket, HidPacketIterator}; +use crate::ctap::hid::HidPacket; use crate::ctap::main_hid::MainHid; #[cfg(feature = "vendor_hid")] use crate::ctap::vendor_hid::VendorHid; @@ -25,6 +25,8 @@ use crate::ctap::CtapState; pub use crate::ctap::Transport; use crate::env::Env; use std::time::{Instant, Duration}; +#[cfg(feature="xous")] +use ctap::main_hid::HidIterType; // Those macros should eventually be split into trace, debug, info, warn, and error macros when // adding either the defmt or log feature and crate dependency. @@ -90,7 +92,7 @@ impl Ctap { packet: &HidPacket, transport: Transport, now: Instant, - ) -> HidPacketIterator { + ) -> HidIterType { match transport { Transport::MainHid => { self.hid diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 08e83cea4..84264404a 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -2,8 +2,6 @@ #![cfg_attr(target_os = "none", no_main)] mod ux; -mod shims; -use shims::*; mod submenu; mod actions; mod totp; @@ -14,10 +12,8 @@ mod migration_v1; use locales::t; -use vault::ctap::status_code::Ctap2StatusCode; -use vault::ctap::hid::HidPacketIterator; +use vault::ctap::main_hid::HidIterType; use vault::env::xous::XousEnv; -use vault::api::connection::{HidConnection, SendOrRecvStatus}; use vault::env::Env; use vault::{ SELF_CONN, Transport, VaultOp @@ -31,14 +27,13 @@ use crate::vendor_commands::VendorSession; use ux::framework::{VaultUx, DEFAULT_FONT, FONT_LIST, name_to_style}; use xous_ipc::Buffer; use xous::{send_message, Message}; -use pddb::Pddb; use usbd_human_interface_device::device::fido::*; use num_traits::*; use std::thread; use core::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; -use std::time::{Instant, Duration}; +use std::time::Instant; use std::collections::BTreeMap; @@ -148,50 +143,6 @@ impl ItemLists { } const ERR_TIMEOUT_MS: usize = 5000; -const SEND_TIMEOUT: Duration = Duration::from_millis(1000); -const KEEPALIVE_DELAY: Duration = Duration::from_millis(100); -// The reply/replies that are queued for each endpoint. -struct EndpointReply { - reply: HidPacketIterator, -} - -impl EndpointReply { - pub fn new() -> Self { - EndpointReply { - reply: HidPacketIterator::none(), - } - } -} -const NUM_ENDPOINTS: usize = 1; -// A single packet to send. -struct SendPacket { - packet: [u8; 64], -} - -struct EndpointReplies { - replies: [EndpointReply; NUM_ENDPOINTS], -} - -impl EndpointReplies { - pub fn new() -> Self { - EndpointReplies { - replies: [ - EndpointReply::new(), - ], - } - } - - pub fn next_packet(&mut self) -> Option { - for ep in self.replies.iter_mut() { - if let Some(packet) = ep.reply.next() { - return Some(SendPacket { - packet, - }); - } - } - None - } -} fn main() -> ! { log_server::init_wait().unwrap(); @@ -235,8 +186,6 @@ fn main() -> ! { let conn = conn.clone(); move || { let xns = xous_names::XousNames::new().unwrap(); - let tt = ticktimer_server::Ticktimer::new().unwrap(); - let mut vendor_session = VendorSession::default(); // block until the PDDB is mounted let pddb = pddb::Pddb::new(); @@ -253,35 +202,94 @@ fn main() -> ! { } }; - let mut env = XousEnv::new(conn); - let mut replies = EndpointReplies::new(); + let env = XousEnv::new(conn); // only run the main loop if the SoC is compatible if env.is_soc_compatible() { let mut ctap = vault::Ctap::new(env, Instant::now()); - let mut pkt_request: Option::<[u8; 64]> = None; loop { - /////////// - // TODO: vendor command for gsora backup protocol has disappeared. - /////////// match ctap.env().main_hid_connection().u2f_wait_incoming() { Ok(msg) => { let mutex = opensk_mutex.lock().unwrap(); - log::debug!("Received U2F packet"); - let reply = ctap.process_hid_packet(&msg.packet, Transport::MainHid, Instant::now()); - for pkt_reply in reply { - let mut reply = RawFidoMsg::default(); - reply.packet.copy_from_slice(&pkt_reply); - let status = ctap.env().main_hid_connection().u2f_send(reply); - match status { - Ok(()) => { - log::debug!("Sent U2F packet"); + log::trace!("Received U2F packet"); + let typed_reply = ctap.process_hid_packet(&msg.packet, Transport::MainHid, Instant::now()); + match typed_reply { + HidIterType::Ctap(reply) => { + for pkt_reply in reply { + let mut reply = RawFidoMsg::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = ctap.env().main_hid_connection().u2f_send(reply); + match status { + Ok(()) => { + log::trace!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } + } } - Err(e) => { - log::error!("Error sending U2F packet: {:?}", e); + } + HidIterType::Vendor(msg) => { + let reply = + match vendor_commands::handle_vendor_data( + msg.cmd as u8, + msg.cid, + msg.payload, + &mut vendor_session + ) { + Ok(return_payload) => { + // if None, this means we've finished parsing all that + // was needed, and we handle/respond with real data + + match return_payload { + Some(data) => data, + None => { + log::debug!("starting processing of vendor data..."); + let resp = vendor_commands::handle_vendor_command( + &mut vendor_session, + allow_host.load(Ordering::SeqCst) + ); + log::debug!("finished processing of vendor data!"); + + match vendor_session.is_backup() { + true => { + if vendor_session.has_backup_data() { + resp + } else { + vendor_session = VendorSession::default(); + resp + } + }, + false => { + vendor_session = VendorSession::default(); + resp + } + } + } + } + } + Err(session_error) => { + // reset the session + vendor_session = VendorSession::default(); + + session_error.ctaphid_error(msg.cid) + } + }; + for pkt_reply in reply { + let mut reply = RawFidoMsg::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = ctap.env().main_hid_connection().u2f_send(reply); + match status { + Ok(()) => { + log::trace!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } + } } } } - // mutex goes out of scope here, and is released + drop(mutex); } Err(e) => { match e { diff --git a/apps/vault/src/migration_v1.rs b/apps/vault/src/migration_v1.rs index b0a816e94..22f89b42b 100644 --- a/apps/vault/src/migration_v1.rs +++ b/apps/vault/src/migration_v1.rs @@ -39,6 +39,9 @@ fn decode_hex(s: &str) -> Result, ParseIntError> { /// `./configure.py --private-key=crypto_data/opensk.key --certificate=crypto_data/opensk_cert.pem` pub fn migrate(pddb: &Pddb) -> Result<(), xous::Error> { if !pddb.list_dict(None).unwrap().contains(&persistent_store::store::OPENSK2_DICT.to_string()) { + let xns = xous_names::XousNames::new().unwrap(); + let modals = modals::Modals::new(&xns).unwrap(); + modals.dynamic_notification(Some("Migrating FIDO Database"), Some("Please wait...")).ok(); // build the attestation keys let pk = decode_hex("b8c3abd05cbe17b2faf87659c6f73e8467832112a0e609807cb68996c9a0c6a8").unwrap(); pddb.get( @@ -72,17 +75,16 @@ pub fn migrate(pddb: &Pddb) -> Result<(), xous::Error> { migrate_one(pddb, FIDO_PERSISTENT_DICT, "AAGUID", key::AAGUID)?; // port master keys migrate_one(pddb, FIDO_DICT, "MASTER_KEYS", key::_RESERVED_KEY_STORE)?; + migrate_one(pddb, FIDO_DICT, "GLOBAL_SIGNATURE_COUNTER", key::GLOBAL_SIGNATURE_COUNTER)?; // port other settings // Turns out that porting over the PIN is a mistake. Just clear it. - - // migrate_one(pddb, FIDO_DICT, "MIN_PIN_LENGTH", key::MIN_PIN_LENGTH)?; - // migrate_one(pddb, FIDO_DICT, "PIN_RETRIES", key::PIN_RETRIES)?; - migrate_one(pddb, FIDO_DICT, "GLOBAL_SIGNATURE_COUNTER", key::GLOBAL_SIGNATURE_COUNTER)?; + migrate_one(pddb, FIDO_DICT, "MIN_PIN_LENGTH", key::MIN_PIN_LENGTH)?; + migrate_one(pddb, FIDO_DICT, "PIN_RETRIES", key::PIN_RETRIES)?; // PIN_HASH requires appending a length to the end. This is an `advisory` field, so we...just make up a value of 8. // This may not actually be the length used, but it's likely to be long enough to avoid forcing a re-PIN event because // the PIN hash is too short. - // migrate_pin(pddb)?; + migrate_pin(pddb)?; // migrate credentials let creds = match pddb.list_keys(FIDO_CRED_DICT, None) { @@ -122,6 +124,7 @@ pub fn migrate(pddb: &Pddb) -> Result<(), xous::Error> { } } pddb.sync().ok(); + modals.dynamic_notification_close().ok(); if successful_migrations == expected_creds { Ok(()) } else { @@ -173,6 +176,12 @@ fn migrate_one ( fn migrate_pin ( pddb: &Pddb, ) -> Result<(), xous::Error> { + // create the key for a forced PIN change + // pddb.get( + // persistent_store::store::OPENSK2_DICT, + // &key::FORCE_PIN_CHANGE.to_string(), + // None, true, true, None, None:: + // ).ok(); let legacy_dict = FIDO_DICT; let legacy_key = "PIN_HASH"; let new_key = key::PIN_PROPERTIES; @@ -183,8 +192,8 @@ fn migrate_pin ( ) { Ok(mut key) => { let mut data = Vec::::new(); + data.push(8); // add a bogus "length" advisory field of 8 let len = key.read_to_end(&mut data).map_err(|_| xous::Error::AccessDenied)? + 1; - data.push(5); // add a bogus "length" advisory field of 8 let mut new_key = pddb.get( persistent_store::store::OPENSK2_DICT, &new_key.to_string(), diff --git a/apps/vault/src/shims.rs b/apps/vault/src/shims.rs deleted file mode 100644 index 0c6b4a87b..000000000 --- a/apps/vault/src/shims.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![allow(dead_code)] // a fair bit of dead code here because we have adapters from Tock functions we don't use -// shims for Tock calls, to avoid having to recode some fairly pervasive patterns in OpenSK -use std::ops::{Sub, Add, AddAssign}; - - -#[derive(Copy, Clone, Debug)] -pub struct ClockValue { - num_ticks: i64, -} - -impl ClockValue { - pub const fn new(num_ticks: i64, _clock_hz: usize) -> ClockValue { - ClockValue { - num_ticks, - } - } - - pub fn num_ticks(&self) -> i64 { - self.num_ticks - } - - pub fn ms(&self) -> i64 { - self.num_ticks - } - - pub fn ms_f64(&self) -> f64 { - self.num_ticks as f64 - } - - pub fn wrapping_add(self, duration: Duration) -> ClockValue { - ClockValue { - num_ticks: self.num_ticks.wrapping_add(duration.ms), - } - } - - pub fn wrapping_sub(self, other: ClockValue) -> Option> { - Some(Duration::from_ms(self.num_ticks - other.num_ticks)) - } -} - - -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Duration { - ms: T, -} - -impl Duration { - pub const fn from_ms(ms: T) -> Duration { - Duration { ms } - } -} - -impl Duration -where - T: Copy, -{ - pub fn ms(&self) -> T { - self.ms - } -} - -impl Sub for Duration -where - T: Sub, -{ - type Output = Duration; - - fn sub(self, other: Duration) -> Duration { - Duration { - ms: self.ms - other.ms, - } - } -} - -#[derive(Copy, Clone, Debug)] -pub struct Timestamp { - ms: T, -} - -impl Timestamp { - pub const fn from_ms(ms: T) -> Timestamp { - Timestamp { ms } - } -} - -impl Timestamp -where - T: Copy, -{ - pub fn ms(&self) -> T { - self.ms - } -} - -impl Timestamp { - pub fn from_clock_value(value: ClockValue) -> Timestamp { - Timestamp { ms: value.ms() } - } -} - -impl Timestamp { - pub fn from_clock_value(value: ClockValue) -> Timestamp { - Timestamp { ms: value.ms_f64() } - } -} - -impl Sub for Timestamp -where - T: Sub, -{ - type Output = Duration; - - fn sub(self, other: Timestamp) -> Duration { - Duration::from_ms(self.ms - other.ms) - } -} - -impl Add> for Timestamp -where - T: Copy + Add, -{ - type Output = Timestamp; - - fn add(self, duration: Duration) -> Timestamp { - Timestamp { - ms: self.ms + duration.ms(), - } - } -} - -impl AddAssign> for Timestamp -where - T: Copy + AddAssign, -{ - fn add_assign(&mut self, duration: Duration) { - self.ms += duration.ms(); - } -} diff --git a/apps/vault/src/vault_api.rs b/apps/vault/src/vault_api.rs index d7f9851ae..9f1962bf3 100644 --- a/apps/vault/src/vault_api.rs +++ b/apps/vault/src/vault_api.rs @@ -7,6 +7,10 @@ use chrono::{Utc, DateTime, NaiveDateTime}; // This file contains items that are used simultaneously within OpenSK and the `vault` app itself. // These items need to be pulled in via both `lib` and `main` scopes. +// Vault-specific command to upload TOTP codes +pub const COMMAND_RESTORE_TOTP_CODES: u8 = 0x71; +pub const COMMAND_BACKUP_TOTP_CODES: u8 = 0x72; +pub const COMMAND_RESET_SESSION: u8 = 0x74; pub const VAULT_PASSWORD_DICT: &'static str = "vault.passwords"; pub const VAULT_TOTP_DICT: &'static str = "vault.totp"; diff --git a/apps/vault/src/vendor_commands.rs b/apps/vault/src/vendor_commands.rs index 564302aab..d030d1710 100644 --- a/apps/vault/src/vendor_commands.rs +++ b/apps/vault/src/vendor_commands.rs @@ -8,11 +8,9 @@ use vault::{ use crate::totp::TotpAlgorithm; use crate::storage::{Error, PasswordRecord, TotpRecord}; -// Vault-specific command to upload TOTP codes -pub const COMMAND_RESTORE_TOTP_CODES: u8 = 0x71; -pub const COMMAND_BACKUP_TOTP_CODES: u8 = 0x72; -pub const COMMAND_RESET_SESSION: u8 = 0x74; - +use vault::vault_api::{ + COMMAND_BACKUP_TOTP_CODES, COMMAND_RESTORE_TOTP_CODES, COMMAND_RESET_SESSION +}; // TODO(gsora): add something that checks whether or not a command works. pub enum SessionError { From 9e86b6d3fdbafe1a22c80f74eb0593a7e683c5d3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 02:42:17 +0800 Subject: [PATCH 29/80] add timeout variant for USB Rx -- note: not tested! turns out we didn't need this variant for things to work with the new OpenSK stack. --- services/usb-device-xous/src/api.rs | 5 + services/usb-device-xous/src/lib.rs | 34 +++++- services/usb-device-xous/src/main_hosted.rs | 11 ++ services/usb-device-xous/src/main_hw.rs | 113 +++++++++++++++++++- 4 files changed, 159 insertions(+), 4 deletions(-) diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 534bcdf60..f864badc6 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -27,6 +27,8 @@ pub(crate) enum Opcode { U2fTx, /// Blocks the caller, waiting for a U2F message U2fRxDeferred, + /// A bump from the timeout process to check if U2fRx has timed out + U2fRxTimeout, /// Query if the HID driver was able to start IsSocCompatible, @@ -51,6 +53,8 @@ pub struct U2fMsgIpc { pub data: [u8; 64], /// Encodes the state of the message pub code: U2fCode, + /// Specifies an optional timeout + pub timeout_ms: Option, } #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq)] @@ -59,6 +63,7 @@ pub enum U2fCode { TxAck, RxWait, RxAck, + RxTimeout, Hangup, Denied, } diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index bb7712a2c..4fcb722bf 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -235,7 +235,8 @@ impl UsbHid { pub fn u2f_wait_incoming(&self) -> Result { let req = U2fMsgIpc { data: [0; 64], - code: U2fCode::RxWait + code: U2fCode::RxWait, + timeout_ms: None, }; let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::U2fRxDeferred.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; @@ -249,13 +250,42 @@ impl UsbHid { U2fCode::Hangup => { Err(xous::Error::ProcessTerminated) }, + U2fCode::RxTimeout => { + Err(xous::Error::Timeout) + }, + _ => Err(xous::Error::InternalError) + } + } + /// Note: this variant is not tested. + pub fn u2f_wait_incoming_timeout(&self, timeout_ms: u64) -> Result { + let req = U2fMsgIpc { + data: [0; 64], + code: U2fCode::RxWait, + timeout_ms: Some(timeout_ms), + }; + let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, Opcode::U2fRxDeferred.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + let ack = buf.to_original::().unwrap(); + match ack.code { + U2fCode::RxAck => { + let mut u2fmsg = RawFidoMsg::default(); + u2fmsg.packet.copy_from_slice(&ack.data); + Ok(u2fmsg) + }, + U2fCode::Hangup => { + Err(xous::Error::ProcessTerminated) + }, + U2fCode::RxTimeout => { + Err(xous::Error::Timeout) + }, _ => Err(xous::Error::InternalError) } } pub fn u2f_send(&self, msg: RawFidoMsg) -> Result<(), xous::Error> { let mut req = U2fMsgIpc { data: [0; 64], - code: U2fCode::Tx + code: U2fCode::Tx, + timeout_ms: None, }; req.data.copy_from_slice(&msg.packet); let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError))?; diff --git a/services/usb-device-xous/src/main_hosted.rs b/services/usb-device-xous/src/main_hosted.rs index 16d1e706d..955ea58f2 100644 --- a/services/usb-device-xous/src/main_hosted.rs +++ b/services/usb-device-xous/src/main_hosted.rs @@ -142,6 +142,17 @@ pub(crate) fn main_hosted() -> ! { buffer.replace(u2f_ipc).unwrap(); } } + Some(Opcode::U2fRxTimeout) => { + if let Some(mut listener) = fido_listener.take() { + let mut response = unsafe { + Buffer::from_memory_message_mut(listener.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + assert_eq!(buf.code, U2fCode::RxWait, "Expected U2fcode::RxWait in wrapper"); + buf.code = U2fCode::RxTimeout; + response.replace(buf).unwrap(); + } + } Some(Opcode::U2fTx) => { if fido_listener_pid.is_none() { fido_listener_pid = msg.sender.pid(); diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 03aab15ec..81b7d76e6 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -9,6 +9,8 @@ use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack}; #[cfg(not(feature="minimal"))] use xous_semver::SemVer; use core::num::NonZeroU8; +use core::sync::atomic::{AtomicU32, AtomicBool, Ordering}; +use std::sync::Arc; use utralib::generated::*; use usb_device::prelude::*; @@ -55,6 +57,13 @@ enum Views { MassStorage = 2, } +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum TimeoutOp { + Pump, + InvalidCall, + Quit, +} + pub(crate) fn main_hw() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -237,12 +246,12 @@ pub(crate) fn main_hw() -> ! { let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default(); let mut fido_listener: Option = None; - // under the theory that PIDs are unforgeable. TODO: check that PIDs are unforgeable. + // under the theory that PIDs cannot be forged. TODO: check that PIDs cannot be forged. // also if someone commandeers a process, all bets are off within that process (this is a general statement) let mut fido_listener_pid: Option = None; let mut fido_rx_queue = VecDeque::<[u8; 64]>::new(); - let mut lockstatus_force_update = true; // some state to track if we've been through a susupend/resume, to help out the status thread with its UX update after a restart-from-cold + let mut lockstatus_force_update = true; // some state to track if we've been through a suspend/resume, to help out the status thread with its UX update after a restart-from-cold let mut was_suspend = true; #[cfg(feature="minimal")] @@ -264,6 +273,77 @@ pub(crate) fn main_hw() -> ! { tt.sleep_ms(EXTENDED_CORE_RESET_MS).ok(); usbmgmt.ll_reset(false); } + // manage FIDO Rx timeouts -- not tested yet + let to_server = xous::create_server().unwrap(); + let to_conn = xous::connect(to_server).unwrap(); + // we don't have AtomicU64 on this platform, so we suffer from a rollover condition in timeouts once every 46 days + // this manifests as a timeout that happens to be scheduled on the rollover being rounded to a max limit timeout of + // 5 seconds, and/or an immediate timeout happening during the 5 seconds before the 46-day limit + let target_time_lsb = Arc::new(AtomicU32::new(0)); + let to_run = Arc::new(AtomicBool::new(false)); + const MAX_TIMEOUT_LIMIT_MS: u32 = 5000; + const POLL_INTERVAL_MS: u64 = 50; + std::thread::spawn({ + let cid = cid; + let to_conn = to_conn; + let target_time_lsb = target_time_lsb.clone(); + let to_run = to_run.clone(); + move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let mut msg_opt = None; + let mut return_type = 0; + let mut next_wake = tt.elapsed_ms(); + loop { + xous::reply_and_receive_next_legacy(to_server, &mut msg_opt, &mut return_type) + .unwrap(); + let msg = msg_opt.as_mut().unwrap(); + // loop only consumes CPU time when a timeout is active. Once it has timed out, + // it will wait for a new pump call. + let now = tt.elapsed_ms(); + match num_traits::FromPrimitive::from_usize(msg.body.id()) + .unwrap_or(TimeoutOp::InvalidCall) + { + TimeoutOp::Pump => { + if to_run.load(Ordering::SeqCst) { + let tt_lsb = target_time_lsb.load(Ordering::SeqCst); + if tt_lsb >= (now as u32) || + (now as u32) - tt_lsb > MAX_TIMEOUT_LIMIT_MS // limits rollover case + { + xous::try_send_message(cid, + xous::Message::new_scalar(Opcode::U2fRxTimeout.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + // no need to set to_run to `false` because a Pump message isn't initiated; + // the loop de-facto stops from a lack of new Pump messages + } else { + if next_wake <= now { + next_wake = now + POLL_INTERVAL_MS; + tt.sleep_ms(POLL_INTERVAL_MS as usize).ok(); + xous::try_send_message(to_conn, + xous::Message::new_scalar(TimeoutOp::Pump.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + } else { + // don't issue more wakeups if we already have a wakeup scheduled + } + } + } + } + TimeoutOp::Quit => { + if let Some(scalar) = msg.body.scalar_message_mut() { + scalar.id = 0; + scalar.arg1 = 1; + break; + } + } + TimeoutOp::InvalidCall => { + log::error!("Unknown opcode received in FIDO Rx timeout handler: {:?}", msg.body.id()); + } + } + } + xous::destroy_server(to_server).unwrap(); + } + }); loop { let mut msg = xous::receive_message(usbdev_sid).unwrap(); @@ -327,6 +407,21 @@ pub(crate) fn main_hw() -> ! { response.replace(buf).unwrap(); } else { log::trace!("registering deferred listener"); + { // not tested + let spec = unsafe { + Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) + }; + if let Some(to) = spec.to_original::().unwrap().timeout_ms { + let target = tt.elapsed_ms() + to; + target_time_lsb.store(target as u32, Ordering::SeqCst); // this will keep updating the target time later and later + // run must always be set *after* target time is updated, because there is always a chance + // we timed out and checked target time between these two steps + to_run.store(true, Ordering::SeqCst); + xous::try_send_message(to_conn, + xous::Message::new_scalar(TimeoutOp::Pump.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + } + }; fido_listener = Some(msg); } } else { @@ -337,6 +432,18 @@ pub(crate) fn main_hw() -> ! { buffer.replace(u2f_ipc).unwrap(); } } + // Note: not tested + Some(Opcode::U2fRxTimeout) => { + if let Some(mut listener) = fido_listener.take() { + let mut response = unsafe { + Buffer::from_memory_message_mut(listener.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + assert_eq!(buf.code, U2fCode::RxWait, "Expected U2fcode::RxWait in wrapper"); + buf.code = U2fCode::RxTimeout; + response.replace(buf).unwrap(); + } + } Some(Opcode::U2fTx) => { if fido_listener_pid.is_none() { fido_listener_pid = msg.sender.pid(); @@ -397,6 +504,7 @@ pub(crate) fn main_hw() -> ! { match u2f.read_report() { Ok(u2f_report) => { if let Some(mut listener) = fido_listener.take() { + to_run.store(false, Ordering::SeqCst); // stop the timeout process from running let mut response = unsafe { Buffer::from_memory_message_mut(listener.body.memory_message_mut().unwrap()) }; @@ -426,6 +534,7 @@ pub(crate) fn main_hw() -> ! { if was_suspend == false { // FIDO listener needs to know when USB was unplugged, so that it can reset state per FIDO2 spec if let Some(mut listener) = fido_listener.take() { + to_run.store(false, Ordering::SeqCst); let mut response = unsafe { Buffer::from_memory_message_mut(listener.body.memory_message_mut().unwrap()) }; From 29b92b7ba416316accff56a728566434c7153d3f Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 02:42:45 +0800 Subject: [PATCH 30/80] bump lockfile --- Cargo.lock | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 33aa60674..d6528fdca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1116,6 +1116,12 @@ dependencies = [ "signature", ] +[[package]] +name = "ed25519-compact" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e18997d4604542d0736fae2c5ad6de987f0a50530cbcc14a7ce5a685328a252d" + [[package]] name = "ed25519-dalek" version = "1.0.1" @@ -2650,6 +2656,15 @@ dependencies = [ "xous 0.9.31 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "persistent_store" +version = "0.1.0" +dependencies = [ + "log", + "pddb", + "tempfile", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -4422,6 +4437,7 @@ dependencies = [ "crypto-mac", "ctap-crypto", "digest 0.9.0", + "ed25519-compact", "enum-iterator", "gam", "graphics-server", @@ -4437,6 +4453,8 @@ dependencies = [ "passwords", "pddb", "perflib", + "persistent_store", + "rand 0.8.5", "rand_core 0.6.3", "random-pick", "rkyv", From f1f486e9fb5374e0901888cf25f470229fdd44bb Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 04:42:40 +0800 Subject: [PATCH 31/80] fix PIN permissions; add support for autotest reset --- .../libraries/persistent_store/src/store.rs | 23 ++++++++++++++----- apps/vault/src/ctap/mod.rs | 1 + apps/vault/src/env/xous/mod.rs | 4 +++- apps/vault/src/main.rs | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/vault/libraries/persistent_store/src/store.rs b/apps/vault/libraries/persistent_store/src/store.rs index 25dbae758..8241dfc1c 100644 --- a/apps/vault/libraries/persistent_store/src/store.rs +++ b/apps/vault/libraries/persistent_store/src/store.rs @@ -278,13 +278,13 @@ impl Store { match *update { StoreUpdate::Insert { key, ref value } => { // if it exists, remove the key; if it doesn't exist, ignore the error - log::debug!("delete key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); + log::debug!("pre-delete key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); self.pddb.delete_key( crate::store::OPENSK2_DICT, &key.to_string(), None ).ok(); - log::debug!("access key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); + log::debug!("write key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); match self.pddb.get( crate::store::OPENSK2_DICT, &key.to_string(), @@ -308,11 +308,21 @@ impl Store { } } StoreUpdate::Remove { key } => { - self.pddb.delete_key( + log::debug!("remove key: {}:{}", crate::store::OPENSK2_DICT, key.to_string()); + match self.pddb.delete_key( crate::store::OPENSK2_DICT, &key.to_string(), None - ).map_err(|_|StoreError::StorageError)? + ) { + Ok(_) => {}, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => {}, + _ => { + log::warn!("Encountered error in StorageUpdate::Remove: {:?}", e); + return Err(StoreError::StorageError) + }, + } + } } } } @@ -363,6 +373,7 @@ impl Store { /// Returns the value of an entry given its key. pub fn find(&self, key: usize) -> StoreResult>> { + log::debug!("find key: {}", key); match self.pddb.get( crate::store::OPENSK2_DICT, &key.to_string(), @@ -453,14 +464,14 @@ impl Store { let attr = record.attributes().map_err(|_| StoreError::StorageError)?; Ok(attr.len) } - _ => Err(StoreError::StorageError) + _ => Err(StoreError::InvalidArgument) // key didn't exist } } fn get_value(&self, handle: &StoreHandle) -> StoreResult> { self.find( handle.key - ).map(|val| val.ok_or(StoreError::StorageError))? + ).map(|val| val.ok_or(StoreError::InvalidArgument))? } } diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index c1cc76aea..2ed1db10c 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -1385,6 +1385,7 @@ impl CtapState { env: &mut impl Env, channel: Channel, ) -> Result { + #[cfg(not(feature="autotest"))] match self.stateful_command_permission.get_command()? { StatefulCommand::Reset => (), _ => return Err(Ctap2StatusCode::CTAP2_ERR_NOT_ALLOWED), diff --git a/apps/vault/src/env/xous/mod.rs b/apps/vault/src/env/xous/mod.rs index 40e2ccf90..97ec8e68c 100644 --- a/apps/vault/src/env/xous/mod.rs +++ b/apps/vault/src/env/xous/mod.rs @@ -433,7 +433,7 @@ impl XousEnv { } Err(_) => panic!("Error sending KEEPALIVE packet"), Ok(SendOrRecvStatus::Sent) => { - log::debug!("Sent KEEPALIVE packet"); + log::trace!("Sent KEEPALIVE packet"); } Ok(SendOrRecvStatus::Received) => { // We only parse one packet, because we only care about CANCEL. @@ -478,6 +478,7 @@ impl UserPresence for XousEnv { } /// Implements FIDO behavior (CTAP2 protocol) fn wait_with_timeout(&mut self, timeout: Duration, reason: Option::, cid: [u8; 4]) -> UserPresenceResult { + log::info!("{}VAULT.PERMISSION,{}", xous::BOOKEND_START, xous::BOOKEND_END); let reason = reason.unwrap_or(String::new()); let kbhit = Arc::new(AtomicU32::new(0)); let expiration = Instant::now().checked_add(timeout).expect("duration bug"); @@ -511,6 +512,7 @@ impl UserPresence for XousEnv { let mut request_str = String::from(&reason); let remaining = expiration.duration_since(Instant::now()).as_secs(); if last_remaining != remaining { + log::info!("countdown: {}", remaining); // only update the UX once per second request_str.push_str( &format!("\n\n⚠ {}{} ⚠\n", diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 84264404a..c869ee8af 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -146,7 +146,7 @@ const ERR_TIMEOUT_MS: usize = 5000; fn main() -> ! { 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(); From c21f1069bc5867d7dd3dd75cea0d1a836b56a716 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 17:59:52 +0800 Subject: [PATCH 32/80] make reset timing a bit looser currently 10 seconds from boot before cred reset can be issued; automated testing can't always keep up with that. expand to 20 seconds. This is not "standards compliant" but the idea that you unplug the device and it stays on is also not "standards compliant". --- apps/vault/src/ctap/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index 2ed1db10c..bd8bfb0b9 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -115,7 +115,8 @@ pub const TOUCH_TIMEOUT: Duration = Duration::from_millis(TOUCH_TIMEOUT_MS); #[cfg(feature = "with_ctap1")] pub const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_millis(10000); // TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? -const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(10000); +// Needs more tolerance for Xous automated test bench, expanded to 20 seconds. +const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(20000); const STATEFUL_COMMAND_TIMEOUT_DURATION: Duration = Duration::from_millis(30000); pub const FIDO2_VERSION_STRING: &str = "FIDO_2_0"; From 31d958d41ef25c5d1fba8ad4ad1b7eeeb683658c Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 18:01:28 +0800 Subject: [PATCH 33/80] improve debug output on PDDB --- services/pddb/src/backend/dictionary.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index b9cdd3079..4c0a00670 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -784,8 +784,9 @@ impl DictCacheEntry { if let Some(small_index) = small_storage_index_from_key(kcache, self.index) { // handle the small pool case let ksp = &mut self.small_pool[small_index]; + let err_static = format!("Small pool did not contain the element we expected: {}, len: {}", &name, kcache.len); ksp.contents.swap_remove(ksp.contents.iter().position(|s| *s == name) - .expect("Small pool did not contain the element we expected")); + .expect(&err_static)); assert!(kcache.reserved <= SMALL_CAPACITY as u64, "error in small key entry size"); ksp.avail += kcache.reserved as u16; assert!(ksp.avail <= SMALL_CAPACITY as u16, "bookkeeping error in small pool capacity"); From f6a8d678cc232a8fbd17d79d1313986167973a9e Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 22:05:03 +0800 Subject: [PATCH 34/80] fix bug in small pool re-packing after delete the new offset address was being used for copying data, instead of the old offset, which was causing key data to refer to an incorrect entry after deletion --- services/pddb/src/backend/dictionary.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 4c0a00670..9bb1cb637 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -92,19 +92,19 @@ impl DictCacheEntry { /// Requires a descriptor for the hardware, and our virtual to physical page mapping. /// Does not overwrite existing cache entries, if they already exist -- only loads in ones that are missing. /// Todo: condense routines in common with ensure_key_entry() to make it easier to maintain. + /// + /// ASSUMES: all small/large pools have been sync'd prior to calling this pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv) -> VirtAddr { let mut try_entry = 1; let mut key_count = 0; let mut alloc_top = VirtAddr::new(LARGE_POOL_START).unwrap(); - let mut valid_keys = 0; - for key in self.keys.values() { - if key.flags.valid() { - valid_keys += 1; - } - } - assert!(valid_keys <= self.key_count, "Inconsistency in key count. See note on basis/dict_count for logic on why this assert should be true..."); - if valid_keys == self.key_count { + // prune invalid entries, then count how many we have + self.keys.retain(|_name, entry| entry.flags.valid()); + let valid_keys = self.keys.len(); + + assert!(valid_keys <= self.key_count as usize, "Inconsistency in key count. See note on basis/dict_count for logic on why this assert should be true..."); + if valid_keys == self.key_count as usize { // we've got an entry for every key, so we can safely skip the deep index search let mut smallkeys_to_fill = Vec::::new(); // work around inner mutability problem by copying the names of keys to fill for (name, key) in self.keys.iter() { @@ -771,6 +771,7 @@ impl DictCacheEntry { let mut need_free_key: Option = None; if let Some(kcache) = self.keys.get_mut(&name) { if !kcache.flags.valid() { + assert!(kcache.clean == false, "keys that are invalidated should be marked as not clean"); // key was previously deleted, already in cache, but not flushed. Nothing to do here. return; } @@ -910,6 +911,7 @@ impl DictCacheEntry { let kcache = self.keys.get_mut(key_name).expect("data record without index"); if kcache.flags.valid() { // only sync valid keys, not ones that were marked for deletion but not yet synced. log::debug!("sync {}/{}:0x{:x}..{}->{}", key_name, index, kcache.start, kcache.len, kcache.reserved); + let old_start = kcache.start; kcache.start = pool_vaddr.get() + pool_offset as u64; kcache.age = kcache.age.saturating_add(1); kcache.clean = false; @@ -928,7 +930,7 @@ impl DictCacheEntry { log::debug!("Small key sync filling {}", key_name); data_cache.fill(hw, v2p_map, cipher, &self.aad, VirtAddr::new(pool_vaddr.get()).unwrap()); if let Some(old_page) = data_cache.data.as_ref() { - let start_offset = size_of::() + (kcache.start % VPAGE_SIZE as u64) as usize; + let start_offset = size_of::() + (old_start % VPAGE_SIZE as u64) as usize; // the delta of len-reserved bytes is assumed zeroed by the original space allocator; we just copy that // here. However, if this assumption is broken, we could have leakage between keys in the reserved area!!! page[size_of::() + pool_offset .. From a8a220ff991bca7b62eb2b643707b1362c8b001d Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 23:31:25 +0800 Subject: [PATCH 35/80] bump release notes --- RELEASE-v0.9.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 234cb35d3..9450e3999 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -313,8 +313,11 @@ perform the Xous firmware upgrade. This requires running manual update commands, - Extend watchdog reset time to ~30s from 7s, to enable easier guru meditation reporting. - Add USB mass-storage drivers (thanks @gsora for all the help there!). Currently able to emulate a blank USB drive in RAM; more to come soon. - Performance fixes to xous scalar and memory messages. A subtle bug was uncovered and fixed in the way scalar messages were being returned, and memory messages now initialize memory using an unrolled loop that takes better advantage of the 32-bit architecture and cache line size. A corresponding `std` fix to an unnecessary `yield_slice()` inside `dl_malloc` improves `vault` PDDB large-key readout performance by 30%. -- Find and fix some edge cases in key deletion. Keys that were supposed to be deleted were being re-fetched from RAM cache, which leads to them re-appearing in the UX and when one attempts to re-delete the key it triggers a double-free error. +- Find and fix some edge cases in key deletion. + - Keys that were supposed to be deleted were being re-fetched from RAM cache, which leads to them re-appearing in the UX and when one attempts to re-delete the key it triggers a double-free error. + - Small pool key packing was incorrectly using an old key offset when repacking keys, leading to data corruption/loss after deletion events - `mtxcli` has a message filter and async message updates! (thanks @tmarble) +- OpenSK FIDO code upgraded to handle FIDO2.1, which means among other things Precursor now supports residential SSH keys (e.g. you can use it with ssh to log in, sign commits, etc.). Upgrading to the latest version will trigger a migration to the new database format. If there is a bug or compatibility issue, don't fear: the previous database is not affected, and you can downgrade to the previous version and continue using your original keys. ## Roadmap - Lots of testing and bug fixes From 6b93cfd567cea363b55c67ddd47470b21dcbf9df Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 23:31:43 +0800 Subject: [PATCH 36/80] add fscb sync and pruning to the delete test bench --- services/shellchat/src/cmds/pddb_cmd.rs | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 05ad1cd88..328816a6f 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -436,14 +436,25 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { log::debug!("name: {}, checksum: {}", junkname, checksum); junk_checksum.insert(junkname.to_string(), checksum); junk_keys.push(junkname); + + #[cfg(not(target_os = "xous"))] + if _env.trng.get_u32().unwrap() as usize % 1024 == 13 { + log::info!("pruning 1"); + self.pddb.dbg_prune().ok(); + } + if _env.trng.get_u32().unwrap() as usize % 8192 == 27 { + log::info!("flushing fscb 1"); + self.pddb.flush_space_update(); + } } + let mut prev_del = "uninit".to_string(); while junk_keys.len() > 0 { let to_remove = _env.trng.get_u32().unwrap() as usize % junk_keys.len(); match self.pddb.delete_key("deltest", &junk_keys[to_remove], None) { Ok(_) => log::debug!("remove {} OK", junk_keys[to_remove]), Err(e) => log::error!("remove {} error: {:?}", junk_keys[to_remove], e), } - self.pddb.sync().ok(); + // self.pddb.sync().ok(); let check = self.pddb.list_keys("deltest", None).unwrap(); assert!(check.len() == (junk_keys.len() - 1)); for (_key_index, key) in check.iter().enumerate() { @@ -465,19 +476,30 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } if checksum != *junk_checksum.get(key).unwrap() { log::info!("readback {}", readback_len); + log::info!("previously deleted: {} / just deleted: {}", prev_del, junk_keys[to_remove]); log::info!("array: {:?}", junk); log::error!("Key data did not match name: {}, checksum: {}, expected: {}", key, checksum, *junk_checksum.get(key).unwrap() ); + self.pddb.dbg_dump("deltest").unwrap(); panic!("data mismatch!"); } } Err(e) => log::error!("error reading back key that should exist: {:?}", e), } } - junk_keys.remove(to_remove); + prev_del = junk_keys.remove(to_remove); + #[cfg(not(target_os = "xous"))] + if _env.trng.get_u32().unwrap() as usize % 1024 == 13 { + log::info!("pruning 2"); + self.pddb.dbg_prune().ok(); + } + if _env.trng.get_u32().unwrap() as usize % 8192 == 27 { + log::info!("flushing fscb 2"); + self.pddb.flush_space_update(); + } } assert!(self.pddb.list_keys("deltest", None).unwrap().len() == 0, "Not all keys were deleted!"); // self.pddb.sync().ok(); From c3ab5ca126083c37cd5c3801770af23aa8013b9d Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 23:32:22 +0800 Subject: [PATCH 37/80] fix bug where old key data was not being zeroized on delete on valid pages were being updated in a dictionary entry as it is updated; actually, entries that are invalid should also be zeroized, otheriwse they "Stick around" as valid entries. --- services/pddb/src/backend/basis.rs | 45 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index b538e281b..d112ed5c1 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -572,6 +572,7 @@ impl BasisCache { // this implementation is still in progress dict_entry.key_erase(key); } + assert!(dict_entry.clean == false, "dictionary entry should have been marked unclean"); // sync the key pools to disk if !dict_entry.sync_small_pool(hw, &mut basis.v2p_map, &basis.cipher) { @@ -1042,16 +1043,20 @@ impl BasisCache { let mut candidates = BinaryHeap::new(); for basis in self.cache.iter() { for (dict_name, dict) in basis.dicts.iter() { - for (key_name, key) in dict.keys.iter() { - candidates.push(Reverse( - KeyAge { - atime: key.atime(), - _size: key.size(), - key: key_name.to_string(), - dict: dict_name.to_string(), - basis: basis.name.to_string(), + if dict.flags.valid() { + for (key_name, key) in dict.keys.iter() { + if key.flags.valid() { + candidates.push(Reverse( + KeyAge { + atime: key.atime(), + _size: key.size(), + key: key_name.to_string(), + dict: dict_name.to_string(), + basis: basis.name.to_string(), + } + )); } - )); + } } } } @@ -1063,7 +1068,7 @@ impl BasisCache { match basis.dicts.get_mut(&ka.dict) { Some(dentry) => { let freed = dentry.evict_keycache_entry(&ka.key); - log::debug!("pruned {} bytes from atime {} / total {}", freed, ka.atime, pruned); + log::debug!("pruned {} bytes from atime {} / total {} key {}", freed, ka.atime, pruned, ka.key); pruned += freed; if pruned >= target_bytes { return pruned; @@ -1475,7 +1480,7 @@ impl BasisCacheEntry { } } - /// This will sync the named Dictionary header + dirty *key descriptors*. It does not + /// This will sync the named Dictionary header + valid *key descriptors*. It does not /// sync the key data itself. Note this does not mark the surrounding basis structure as clean /// when it exits, even if there are no more dirty entries within. /// @@ -1502,7 +1507,8 @@ impl BasisCacheEntry { // but definitely all the dirty ones are in there (if they aren't, where else would they be??) // this is the virtual page within the dictionary region that we're currently serializing - let mut vpage_num = 0; + let mut vpage_num = 0; // because the root record is dirty, we always need to regenerate page 0. + // note: other pages are only generated on-demand, based on if there is a dirty key in that page or not. let mut loopcheck= 0; let mut sync_count = 0; loop { @@ -1534,7 +1540,7 @@ impl BasisCacheEntry { } // 2(b). fill in the target vpage with data: general key case - // Scan the DictCacheEntry.keys record for dirty keys within the current target vpage's range + // Scan the DictCacheEntry.keys record for valid keys within the current target vpage's range // this is not a terribly efficient operation right now, because the DictCacheEntry is designed to // be searched by name, but in this case, we want to check for an index range. There's a lot // of things we could do to optimize this, depending on the memory/time trade-off we want to @@ -1548,7 +1554,7 @@ impl BasisCacheEntry { key.start, key.clean, key.flags, key.descriptor_index, ); }*/ - if !key.clean && key.flags.valid() { + if key.flags.valid() { if key.descriptor_vaddr(dict_offset) >= cur_vpage && key.descriptor_vaddr(dict_offset) < next_vpage { log::debug!("merging in key {}", key_name); @@ -1589,9 +1595,10 @@ impl BasisCacheEntry { }; for (index, stride) in page[size_of::()..].chunks_mut(DK_STRIDE).enumerate() { if let Some(elem) = dk_vpage.elements[index] { - for (&src, dst) in elem.data.iter().zip(stride.iter_mut()) { - *dst = src; - } + stride.copy_from_slice(&elem.data); + } else { + // actively zero-ize fields that are `None` so they don't decrypt to valid records later on. + stride.copy_from_slice(&[0u8; DK_STRIDE]); } } // generate nonce and write out @@ -1615,6 +1622,9 @@ impl BasisCacheEntry { } sync_count += 1; } + // remove invalid keys from the dictionary cache + dict.keys.retain(|_name, entry| entry.flags.valid()); + log::debug!("done syncing dict"); dict.clean = true; } @@ -1691,6 +1701,7 @@ impl BasisCacheEntry { } pub(crate) fn sync(&mut self, hw: &mut PddbOs) -> Result<()> { + self.dicts.retain(|_name, entry| entry.flags.valid()); // prune any invalid dictionary entries, as they have been deleted // this is a bit awkward, but we have to make a copy of all the dictionary names // because otherwise we borrow self as immutable to enumerate the names, and then // we borrow it as mutable to do the sync. This deep-copy works around the issue. From dee4bac9560946a606e343396d5c246bf0c6e423 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 23:33:38 +0800 Subject: [PATCH 38/80] add some more time allowance for resets to process from boot --- apps/vault/src/ctap/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/vault/src/ctap/mod.rs b/apps/vault/src/ctap/mod.rs index bd8bfb0b9..218b14b7d 100644 --- a/apps/vault/src/ctap/mod.rs +++ b/apps/vault/src/ctap/mod.rs @@ -115,7 +115,10 @@ pub const TOUCH_TIMEOUT: Duration = Duration::from_millis(TOUCH_TIMEOUT_MS); #[cfg(feature = "with_ctap1")] pub const U2F_UP_PROMPT_TIMEOUT: Duration = Duration::from_millis(10000); // TODO(kaczmarczyck) 2.1 allows Reset after Reset and 15 seconds? -// Needs more tolerance for Xous automated test bench, expanded to 20 seconds. +#[cfg(not(feature="xous"))] +const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(10000); +// Needs more timing tolerance for Xous automated test bench, expanded to 20 seconds. +#[cfg(feature="xous")] const RESET_TIMEOUT_DURATION: Duration = Duration::from_millis(20000); const STATEFUL_COMMAND_TIMEOUT_DURATION: Duration = Duration::from_millis(30000); From 50458e837bac982bfe4acd903bec8532d88338de Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 26 Jan 2023 23:34:08 +0800 Subject: [PATCH 39/80] improve debug output in storage layer --- apps/vault/libraries/persistent_store/src/store.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/vault/libraries/persistent_store/src/store.rs b/apps/vault/libraries/persistent_store/src/store.rs index 8241dfc1c..d70f9a405 100644 --- a/apps/vault/libraries/persistent_store/src/store.rs +++ b/apps/vault/libraries/persistent_store/src/store.rs @@ -339,6 +339,7 @@ impl Store { for key in keys { if let Ok(key_as_usize) = usize::from_str_radix(&key, 10) { if key_as_usize >= min_key { + log::debug!("clear deleting: {}", key); self.pddb.delete_key(crate::store::OPENSK2_DICT, &key, None @@ -394,7 +395,7 @@ impl Store { /// Returns a handle to an entry given its key. pub fn find_handle(&self, key: usize) -> StoreResult> { - log::debug!("listing keys"); + log::debug!("listing all keys to find_handle {}", key); let keys = match self.pddb.list_keys( crate::store::OPENSK2_DICT, None @@ -402,16 +403,18 @@ impl Store { Ok(k) => k, Err(e) => match e.kind() { std::io::ErrorKind::NotFound => { - log::debug!("didn't find any keys, returning empty list"); + log::debug!("Dictionary does not exist, returning empty list"); Vec::new() }, _ => return Err(StoreError::StorageError) } }; - + log::debug!("keylist: {:?}", keys); if keys.contains(&key.to_string()) { + log::debug!("find_handle found: {}", key); Ok(Some(StoreHandle {key})) } else { + log::debug!("find_handle did not find: {}", key); Ok(None) } } From 99995f963da5e042020b7f155b149f511bfc8c31 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 01:42:50 +0800 Subject: [PATCH 40/80] use bulk delete to speed up the reset process this is necessary to pass the test after creating dozens of credentials; it takes too long to process the deletion if it's done one at a time --- .../libraries/persistent_store/src/store.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/vault/libraries/persistent_store/src/store.rs b/apps/vault/libraries/persistent_store/src/store.rs index d70f9a405..11279144a 100644 --- a/apps/vault/libraries/persistent_store/src/store.rs +++ b/apps/vault/libraries/persistent_store/src/store.rs @@ -334,16 +334,14 @@ impl Store { /// /// Entries with a key larger or equal to `min_key` are deleted. pub fn clear(&mut self, min_key: usize) -> StoreResult<()> { + let mut to_delete = Vec::new(); let keys = self.pddb.list_keys(crate::store::OPENSK2_DICT, None) .map_err(|_| StoreError::StorageError)?; for key in keys { if let Ok(key_as_usize) = usize::from_str_radix(&key, 10) { if key_as_usize >= min_key { log::debug!("clear deleting: {}", key); - self.pddb.delete_key(crate::store::OPENSK2_DICT, - &key, - None - ).map_err(|_| StoreError::StorageError)?; + to_delete.push(key); } } else { log::error!("Internal coding error: all keys in {} should be numeric. Got: {}", @@ -353,6 +351,16 @@ impl Store { return Err(StoreError::InvalidArgument); } } + match self.pddb.delete_key_list(crate::store::OPENSK2_DICT, to_delete, None) { + Ok(_) => {} + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => {}, + _ => { + log::warn!("Encountered error in StorageUpdate::clear {:?}", e); + return Err(StoreError::StorageError) + }, + } + } self.entries = self.pddb.list_keys(crate::store::OPENSK2_DICT, None).ok(); Ok(()) } From 9c5efebf387e4b4363c4f4dc02b15ee411ecc0a8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 01:43:24 +0800 Subject: [PATCH 41/80] add bulk deletion of keys from a single dictionary --- services/pddb/src/api.rs | 16 +++++++++- services/pddb/src/backend/basis.rs | 37 ++++++++++++++++++++++ services/pddb/src/lib.rs | 50 ++++++++++++++++++++++++++++++ services/pddb/src/main.rs | 42 +++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 1 deletion(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index df88f3b9b..282103ae4 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -242,6 +242,9 @@ pub(crate) enum Opcode { /// Basis monitoring - reports when the basis order has changed BasisMonitor = 54, + /// Bulk delete of keys within a dictionary; if non-existent keys are specified, no error is returned. + DictBulkDelete = 55, + /// This key type could not be decoded InvalidOpcode = u32::MAX as _, } @@ -332,9 +335,20 @@ pub (crate) struct PddbKeyList { pub end: bool, } +/// A structure for bulk deletion of keys +pub(crate) const MAX_PDDB_DELETE_LEN: usize = 3800; // approximate limit, might be a little higher if we cared to calculate it out +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub (crate) struct PddbDeleteList { + pub basis_specified: bool, + pub basis: xous_ipc::String::, + pub dict: xous_ipc::String::, + pub data: [u8; MAX_PDDB_DELETE_LEN], + pub retcode: PddbRetcode, +} + /// Return codes for Read/Write API calls to the main server #[repr(u8)] -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Eq, PartialEq)] pub(crate) enum PddbRetcode { Uninit = 0, Ok = 1, diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index d112ed5c1..a157505de 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -597,6 +597,43 @@ impl BasisCache { } } + pub(crate) fn key_list_remove(&mut self, + hw: &mut PddbOs, dict: &str, key_list: Vec::, basis_name: Option<&str> + ) -> Result<()> { + if let Some(basis_index) = self.select_basis(basis_name) { + let basis = &mut self.cache[basis_index]; + if !basis.ensure_dict_in_cache(hw, dict) { + return Err(Error::new(ErrorKind::NotFound, "dictionary not found")); + } + if let Some(dict_entry) = basis.dicts.get_mut(dict) { + basis.age = basis.age.saturating_add(1); + basis.clean = false; + for key in key_list { + if dict_entry.ensure_key_entry(hw, &mut basis.v2p_map, &basis.cipher, &key) { + dict_entry.key_remove(hw, &mut basis.v2p_map, &basis.cipher, &key, false); + assert!(dict_entry.clean == false, "dictionary entry should have been marked unclean"); + } + } + // sync the key pools to disk + if !dict_entry.sync_small_pool(hw, &mut basis.v2p_map, &basis.cipher) { + return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of memory syncing small pool")); + } + dict_entry.sync_large_pool(); + // encrypt and write the dict entry to disk + basis.dict_sync(hw, dict)?; + // sync the root basis structure as well, while we're at it... + basis.basis_sync(hw); + // finally, sync the page tables. + basis.pt_sync(hw); + Ok(()) + } else { + Err(Error::new(ErrorKind::NotFound, "dictionary not found")) + } + } else { + Err(Error::new(ErrorKind::NotFound, "Requested basis not found, or PDDB not mounted.")) + } + } + /// Updates a key in a dictionary; if it doesn't exist, creates it. User can specify a basis, /// or rely upon the auto-basis select algorithm. pub(crate) fn key_update(&mut self, diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 227de45d3..fd0fdadab 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -527,6 +527,56 @@ impl Pddb { _ => Err(Error::new(ErrorKind::Other, "Internal error")) } } + /// deletes a list of keys from a dictionary. The list of keys must be less than MAX_PDDB_DELETE_LEN characters long. + pub fn delete_key_list(&self, dict_name: &str, key_list: Vec::, basis_name: Option<&str>) -> Result<()> { + if key_list.len() == 0 { + return Ok(()) + } + if dict_name.len() > (DICT_NAME_LEN - 1) { + return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long")); + } + let bname = if let Some(bname) = basis_name { + if bname.len() > BASIS_NAME_LEN - 1 { + return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); + } + xous_ipc::String::::from_str(bname) + } else { + xous_ipc::String::::new() + }; + let mut request = PddbDeleteList { + basis_specified: basis_name.is_some(), + basis: xous_ipc::String::::from_str(&bname), + dict: xous_ipc::String::::from_str(dict_name), + retcode: PddbRetcode::Uninit, + data: [0u8; MAX_PDDB_DELETE_LEN] + }; + let mut index = 0; + for keyname in key_list { + if keyname.len() > (KEY_NAME_LEN - 1) { + return Err(Error::new(ErrorKind::InvalidInput, "one of the key names is too long")); + } + if keyname.len() + 1 + index < MAX_PDDB_DELETE_LEN { + assert!(keyname.len() < u8::MAX as usize); // this should always be true due to other limits in the PDDB + request.data[index] = keyname.len() as u8; + index += 1; + request.data[index..index + keyname.len()].copy_from_slice(keyname.as_bytes()); + index += keyname.len(); + } else { + return Err(Error::new(ErrorKind::OutOfMemory, "Key list total size exceeds MAX_PDDBKLISTLEN")); + } + } + let mut buf = Buffer::into_buf(request) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + buf.lend_mut(self.conn, Opcode::DictBulkDelete.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + let response = buf.as_flat::().unwrap(); + match response.retcode { + ArchivedPddbRetcode::Ok => Ok(()), + ArchivedPddbRetcode::AccessDenied => Err(Error::new(ErrorKind::NotFound, "Dictionary not found, or inaccessible")), + ArchivedPddbRetcode::Uninit => Err(Error::new(ErrorKind::ConnectionAborted, "Return code not set processing bulk delete, server aborted?")), + _ => Err(Error::new(ErrorKind::Other, "Internal Error handling bulk delete list")), + } + } /// deletes the entire dictionary pub fn delete_dict(&self, dict_name: &str, basis_name: Option<&str>) -> Result<()> { if dict_name.len() > (DICT_NAME_LEN - 1) { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 7f4b7e0f4..6546a0ef3 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1148,6 +1148,48 @@ fn wrapped_main() -> ! { } buffer.replace(req).unwrap(); } + Opcode::DictBulkDelete => { + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let mut req = buffer.to_original::().unwrap(); + let mut key_list = Vec::::new(); + // the [u8] data is structured as a packed list of u8-len + u8 data slice. The max length of + // a PDDB key name is guaranteed to be shorter than a u8. If the length field is 0, then this + // particular response has no more data in it to read. + let mut index = 0; + while req.data[index] != 0 && index < MAX_PDDB_DELETE_LEN { + let strlen = req.data[index] as usize; + index += 1; + if strlen + index >= MAX_PDDB_DELETE_LEN { + log::error!("Logic error in key list, index would be out of bounds. Aborting"); + req.retcode = PddbRetcode::InternalError; + break; + } + let key = String::from(std::str::from_utf8(&req.data[index..index+strlen]).unwrap_or("UTF8 error")); + key_list.push(key); + index += strlen; + } + if req.retcode == PddbRetcode::InternalError { + buffer.replace(req).ok(); + continue; + } + log::info!("Deleting key list: {:?}", key_list); + let start = tt.elapsed_ms(); + let bname = if req.basis_specified { + Some(req.basis.as_str().unwrap()) + } else { + None + }; + let dict = req.dict.as_str().expect("dict utf-8 decode error"); + match basis_cache.key_list_remove(&mut pddb_os, dict, key_list, bname) { + Ok(_) => req.retcode = PddbRetcode::Ok, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => req.retcode = PddbRetcode::AccessDenied, + _ => req.retcode = PddbRetcode::InternalError, + } + } + log::info!("Bulk delete finished in {}ms", tt.elapsed_ms() - start); + buffer.replace(req).ok(); + } Opcode::DeleteKeyStd => { if let Some(mem) = msg.body.memory_message_mut() { mem.offset = None; From 20a2388b5dd0b217c64f07ac83bf7593496ef1da Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 01:47:48 +0800 Subject: [PATCH 42/80] update commit ref of opensk code --- apps/vault/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index c869ee8af..91f731ecc 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -42,9 +42,9 @@ use std::collections::BTreeMap; // netcat -k -u -l 6502 > /dev/ttyS0 // use the "autotest" feature to remove some excess prompts that interfere with the test -// the OpenSK code is based off of commit f2496a8e6d71a4e838884996a1c9b62121f87df2 from the -// Google OpenSK repository. The last push was Nov 19 2021, and the initial merge into Xous -// was finished on June 9 2022. Any patches to this code base will have to be manually +// the OpenSK code is based off of commit 0db393bd1e9c3901f772b3c2107dbbeb71ff2dc5 from the +// Google OpenSK repository. This commit is dated Jan 4 2023. The initial merge into Xous +// was finished on Jan 27 2023. Any patches to this code base will have to be manually // applied. Please update the information here to reflect the latest patch status. /* From 9ea5182103de619de627896cb9f486c92d8a6599 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 02:23:34 +0800 Subject: [PATCH 43/80] cleanup minor UX issue (disappearing title when no items present) --- apps/vault/src/ux/framework.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 1beef8362..41e6976a5 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -427,6 +427,7 @@ impl VaultUx { stroke_width: 0 } )).expect("can't clear content area"); + self.title_dirty = true; // just blanked the whole area, have to redraw the title. return; } else if self.title_dirty && self.filtered_list.len() != 0 { // handle the title region separately From 25cc5712289fd44a22895e425366c0352b74f7ac Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 02:23:53 +0800 Subject: [PATCH 44/80] chore: remove warning, and old notes --- apps/vault/src/actions.rs | 2 +- apps/vault/src/main.rs | 26 -------------------------- 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 583740cff..4392d4b77 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -12,7 +12,7 @@ use passwords::PasswordGenerator; use std::cell::RefCell; use vault::{ - deserialize_app_info, serialize_app_info, AppInfo, basis_change, + deserialize_app_info, serialize_app_info, basis_change, VAULT_PASSWORD_DICT, VAULT_TOTP_DICT, VAULT_ALLOC_HINT, utc_now, atime_to_str }; diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 91f731ecc..73273d711 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -90,32 +90,6 @@ UI concept: - Actions thread (actions object): "blocking" UI operations - manages multi-sequence dialog queries, database access - Fido thread: handles USB interactions. Can always pop up a dialog box, but it cannot override a dialog-in-progress. - Icontray thread: a simple server that serves as a shim between the IME structure and this to create an icontray function - */ - - // TOTP test I65VU7K5ZQL7WB4E https://authenticationtest.com/totpChallenge/ - // otpauth-migration://offline?data=Ci8KCke7Wn1dzBf7B4QSG3RvdHBAYXV0aGVudGljYXRpb250ZXN0LmNvbSABKAEwAhABGAEgACjh8Yv%2B%2BP%2F%2F%2F%2F8B -/* -otp_parameters { - secret: "G\273Z}]\314\027\373\007\204" - name: "totp@authenticationtest.com" - algorithm: SHA1 - digits: SIX - type: TOTP -} -version: 1 -batch_size: 1 -batch_index: 0 -batch_id: -1883047711 - ->>> b32decode("I65VU7K5ZQL7WB4E").hex() -'47bb5a7d5dcc17fb0784' - -To clear test entries: - pddb dictdelete vault.passwords - pddb dictdelete vault.totp - pddb dictdelete fido.cred - pddb dictdelete fido.u2fapps - */ #[derive(Copy, Clone, PartialEq, Eq, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] From a3086357bd73d9b866f0a4ee2882d752b6dcd3fb Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 14:19:41 +0800 Subject: [PATCH 45/80] make backup timestamps windows-compatible --- tools/backup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/backup.py b/tools/backup.py index 70088ad11..6da896a95 100755 --- a/tools/backup.py +++ b/tools/backup.py @@ -557,12 +557,12 @@ def main(): "--peek", required=False, help="Inspect an address", type=auto_int, metavar=('ADDR') ) parser.add_argument( - "--output", help="Output file name. Defaults to ./backup_.pddb", type=str, default=None + "--output", help="Output file name. Defaults to ./backup_.pddb", type=str, default=None ) args = parser.parse_args() if args.output is None: - ofile = "backup_{}.pddb".format(datetime.now().isoformat()) + ofile = "backup_{}.pddb".format(datetime.now().strftime('%Y%m%d_%H%M%S')) else: ofile = args.output From 7cb98699aa6196900fc451c75f9e7392f7427ba4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 27 Jan 2023 15:26:46 +0800 Subject: [PATCH 46/80] add some consistency check and repair facility to PDDB --- services/pddb/src/api.rs | 4 ++ services/pddb/src/backend/basis.rs | 60 +++++++++++++++++-------- services/pddb/src/backend/dictionary.rs | 11 +++-- services/pddb/src/lib.rs | 42 +++++++++++++++++ services/pddb/src/libstd/mod.rs | 8 ++-- services/pddb/src/main.rs | 14 +++--- services/pddb/src/tests.rs | 27 ++++++----- services/shellchat/src/cmds/pddb_cmd.rs | 20 +++++++++ 8 files changed, 144 insertions(+), 42 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 282103ae4..61e85b1db 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -308,6 +308,10 @@ pub struct PddbDictRequest { pub code: PddbRequestCode, /// used only to specify a readback size limit for bulk-return requests pub bulk_limit: Option, + /// return value for key_count metadata (informative only) + pub key_count: u32, + /// return value for found_key_count metadata (informative only) + pub found_key_count: u32, } /// A structure for requesting a token to access a particular key/value pair diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index a157505de..ae63a7f7b 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -339,6 +339,7 @@ impl BasisCache { last_disk_key_index: 1, // we know we don't have to search past this in a new dictionary flags: init_flags, key_count: 0, + found_key_count: 0, small_pool: Vec::::new(), small_pool_free: BinaryHeap::::new(), aad: my_aad, @@ -348,7 +349,7 @@ impl BasisCache { basis.dicts.insert(String::from(name), dict_cache); basis.num_dicts += 1; // encrypt and write the dict entry to disk - basis.dict_sync(hw, name)?; + basis.dict_sync(hw, name, false)?; // sync the root basis structure as well, while we're at it... basis.basis_sync(hw); // finally, sync the page tables. @@ -386,9 +387,11 @@ impl BasisCache { } dict_set } - pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result> { + pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result<(BTreeSet::, u32, u32)> { let mut merge_list = BTreeSet::::new(); let mut found_dict = false; + let mut key_count: u32 = 0; + let mut found_key_count: u32 = 0; if basis_name.is_some() { if let Some(basis_index) = self.select_basis(basis_name) { let basis = &mut self.cache[basis_index]; @@ -396,6 +399,8 @@ impl BasisCache { if let Some(dcache) = basis.dicts.get_mut(dict) { dcache.key_list(hw, &basis.v2p_map, &basis.cipher, &mut merge_list); found_dict = true; + key_count += dcache.key_count; + found_key_count += dcache.found_key_count; } } } else { @@ -404,11 +409,13 @@ impl BasisCache { if let Some(dcache) = basis.dicts.get_mut(dict) { dcache.key_list(hw, &basis.v2p_map, &basis.cipher, &mut merge_list); found_dict = true; + key_count += dcache.key_count; + found_key_count += dcache.found_key_count; } } } if found_dict { - Ok(merge_list) + Ok((merge_list, key_count, found_key_count)) } else { return Err(Error::new(ErrorKind::NotFound, "dictionary not found")) } @@ -580,7 +587,7 @@ impl BasisCache { } dict_entry.sync_large_pool(); // encrypt and write the dict entry to disk - basis.dict_sync(hw, dict)?; + basis.dict_sync(hw, dict, false)?; // sync the root basis structure as well, while we're at it... basis.basis_sync(hw); // finally, sync the page tables. @@ -620,7 +627,7 @@ impl BasisCache { } dict_entry.sync_large_pool(); // encrypt and write the dict entry to disk - basis.dict_sync(hw, dict)?; + basis.dict_sync(hw, dict, false)?; // sync the root basis structure as well, while we're at it... basis.basis_sync(hw); // finally, sync the page tables. @@ -765,7 +772,7 @@ impl BasisCache { dict_entry.sync_large_pool(); // encrypt and write the dict entry to disk - basis.dict_sync(hw, dict)?; + basis.dict_sync(hw, dict, false)?; // sync the root basis structure as well, while we're at it... basis.basis_sync(hw); // finally, sync the page tables. @@ -986,7 +993,7 @@ impl BasisCache { pub(crate) fn basis_unmount(&mut self, hw: &mut PddbOs, basis_name: &str) -> Result<()> { if let Some(basis_index) = self.select_basis(Some(basis_name)) { let basis = &mut self.cache[basis_index]; - basis.sync(hw)?; + basis.sync(hw, false)?; self.cache.retain(|x| x.name != basis_name); Ok(()) } else { @@ -1014,22 +1021,25 @@ impl BasisCache { } } - pub(crate) fn sync(&mut self, hw: &mut PddbOs, basis_name: Option<&str>) -> Result<()> { + pub(crate) fn sync(&mut self, hw: &mut PddbOs, basis_name: Option<&str>, cleanup: bool) -> Result<()> { + if cleanup { + log::info!("calling sync with cleanup!"); + } if basis_name.is_some() { if let Some(basis_index) = self.select_basis(basis_name) { - self.cache[basis_index].sync(hw)? + self.cache[basis_index].sync(hw, cleanup)? } } else { for basis in self.cache.iter_mut() { log::debug!("syncing {}", basis.name); - basis.sync(hw)?; + basis.sync(hw, cleanup)?; } } Ok(()) } pub(crate) fn suspend(&mut self, hw: &mut PddbOs) { - self.sync(hw, None).expect("couldn't sync on suspend"); + self.sync(hw, None, false).expect("couldn't sync on suspend"); let mut lock_list = Vec::::new(); for basis in self.cache.iter_mut() { match basis.policy { @@ -1075,7 +1085,7 @@ impl BasisCache { // 1. iterate through all the known keys, creating a sorted heap of fully-specified keys (basis/dict/key) // sorted by access time // 2. evict keys with the oldest access time, until target_bytes is hit. - self.sync(hw, None).unwrap(); + self.sync(hw, None, false).unwrap(); let mut candidates = BinaryHeap::new(); for basis in self.cache.iter() { @@ -1285,7 +1295,7 @@ impl BasisCacheEntry { let mut largest_extent = 0; for dict in self.dicts.values_mut() { if dict.flags.valid() { - let extent = dict.fill(hw, &mut self.v2p_map, &self.cipher).get(); + let extent = dict.fill(hw, &mut self.v2p_map, &self.cipher, false).get(); if extent > largest_extent { largest_extent = extent; } @@ -1309,12 +1319,12 @@ impl BasisCacheEntry { }; if !dict_present_and_valid { let mut dcache = DictCacheEntry::new(dict, try_entry, &self.aad); - let max_large_alloc = dcache.fill(hw, &self.v2p_map, &self.cipher); + let max_large_alloc = dcache.fill(hw, &self.v2p_map, &self.cipher, false); self.dicts.insert(dict_name.to_string(), dcache); self.large_pool_update(max_large_alloc.get()); } else { let dcache = self.dicts.get_mut(&dict_name).expect("dict should be present, as we checked for it already..."); - let extent = dcache.fill(hw, &mut self.v2p_map, &self.cipher).get(); + let extent = dcache.fill(hw, &mut self.v2p_map, &self.cipher, false).get(); self.large_pool_update(extent); } dict_count += 1; @@ -1347,7 +1357,7 @@ impl BasisCacheEntry { let dcache = self.dicts.get_mut(name).expect("entry was ensured, but somehow missing"); log::trace!("dcache {} index {}, key_count {}", name, dcache.index, dcache.key_count); // ensure all the keys are in RAM - dcache.fill(hw, &self.v2p_map, &self.cipher); + dcache.fill(hw, &self.v2p_map, &self.cipher, false); // allocate a copy of the key list, to avoid interior mutability problems with the next remove step let mut key_list = Vec::::new(); @@ -1525,10 +1535,17 @@ impl BasisCacheEntry { /// allocated. You can try calling it without pre-allocating the entries, but if the FastSpace structure /// doesn't have enough space, the routine will return an error indicating we're out of memory. /// You could then try to allocate more FastSpace, and re-try the sync operation. - pub(crate) fn dict_sync(&mut self, hw: &mut PddbOs, name: &str) -> Result<()> { + /// + /// If `cleanup` is true, then the keycount field is set to the number of keys actually found. This + /// "fixes" inconsistencies by just trimming records that got lost. + pub(crate) fn dict_sync(&mut self, hw: &mut PddbOs, name: &str, cleanup: bool) -> Result<()> { self.last_sync = Some(hw.timestamp_now()); if let Some(dict) = self.dicts.get_mut(&String::from(name)) { let dict_offset = VirtAddr::new(dict.index.get() as u64 * DICT_VSIZE).unwrap(); + if cleanup && (dict.key_count != dict.found_key_count) { + log::info!("Cleaning up key_count inconsistency: {} -> {}", dict.key_count, dict.found_key_count); + dict.key_count = dict.found_key_count; + } if !dict.clean { let dict_name = DictName::try_from_str(name).or(Err(Error::new(ErrorKind::InvalidInput, "dictionary name invalid: invalid utf-8 or length")))?; let dict_disk = Dictionary { @@ -1737,7 +1754,7 @@ impl BasisCacheEntry { dict_found } - pub(crate) fn sync(&mut self, hw: &mut PddbOs) -> Result<()> { + pub(crate) fn sync(&mut self, hw: &mut PddbOs, cleanup: bool) -> Result<()> { self.dicts.retain(|_name, entry| entry.flags.valid()); // prune any invalid dictionary entries, as they have been deleted // this is a bit awkward, but we have to make a copy of all the dictionary names // because otherwise we borrow self as immutable to enumerate the names, and then @@ -1754,9 +1771,14 @@ impl BasisCacheEntry { return Err(Error::new(ErrorKind::OutOfMemory, "Ran out of memory syncing small pool")); } dict_entry.sync_large_pool(); + if cleanup { + // this will do a full-scan through all the key entries to discover any extra + // ones that are "way out" in the storage space that could have been lost + dict_entry.fill(hw, &mut self.v2p_map, &self.cipher, cleanup); + } } - match self.dict_sync(hw, &dict) { + match self.dict_sync(hw, &dict, cleanup) { Ok(_) => {}, Err(e) => { log::error!("Error encountered syncing dict {}: {:?}", dict, e); diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 9bb1cb637..dc8397b94 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -34,6 +34,8 @@ pub(crate) struct DictCacheEntry { pub(crate) keys: HashMap::, /// count of total keys in the dictionary -- may be equal to or larger than the number of elements in `keys` pub(crate) key_count: u32, + /// actual count of keys found -- this drifts from the actual key count due to write errors and ungraceful powerdowns + pub(crate) found_key_count: u32, /// track the pool of free key indices. Wrapped in a refcell so we can work the index mechanism while updating the keys HashMap pub(crate) free_keys: BinaryHeap::>, /// hint for when to stop doing a brute-force search for the existence of a key in the disk records. @@ -77,6 +79,7 @@ impl DictCacheEntry { index: NonZeroU32::new(index as u32).unwrap(), keys: HashMap::::new(), key_count: dict.num_keys, + found_key_count: dict.num_keys, free_keys, last_disk_key_index: dict.free_key_index, clean: false, @@ -94,7 +97,7 @@ impl DictCacheEntry { /// Todo: condense routines in common with ensure_key_entry() to make it easier to maintain. /// /// ASSUMES: all small/large pools have been sync'd prior to calling this - pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv) -> VirtAddr { + pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, cleanup: bool) -> VirtAddr { let mut try_entry = 1; let mut key_count = 0; let mut alloc_top = VirtAddr::new(LARGE_POOL_START).unwrap(); @@ -133,7 +136,7 @@ impl DictCacheEntry { let mut index_cache = PlaintextCache { data: None, tag: None }; let mut data_cache = PlaintextCache { data: None, tag: None }; let mut errcnt = 0; - while try_entry < KEY_MAXCOUNT && key_count < self.key_count { + while try_entry < KEY_MAXCOUNT && (cleanup || (key_count < self.key_count)) { #[cfg(feature="perfcounter")] hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); // cache our decryption data -- there's about 32 entries per page, and the scan is largely linear/sequential, so this should @@ -198,6 +201,8 @@ impl DictCacheEntry { } // note where the scan left off, so we don't have to brute-force it in the future self.last_disk_key_index = try_entry as u32; + // note the actual number of keys found + self.found_key_count = key_count; // now build the small_pool_free binary heap structure #[cfg(feature="perfcounter")] @@ -215,7 +220,7 @@ impl DictCacheEntry { hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_STARTBLOCK, 0, std::line!()); // ensure that the key cache is filled if self.keys.len() < self.key_count as usize { - self.fill(hw, v2p_map, cipher); + self.fill(hw, v2p_map, cipher, false); } #[cfg(feature="perfcounter")] hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, 0, std::line!()); diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index fd0fdadab..50e75342d 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -99,6 +99,9 @@ pub struct Pddb { /// like this are probably OK. keys: Arc >>>, trng: trng::Trng, + /// These are temporary fields only to be used by the consistency check feature. + key_count: RefCell, + found_key_count: RefCell, } impl Pddb { pub fn new() -> Self { @@ -112,6 +115,9 @@ impl Pddb { cb_handle: RefCell::new(None), keys, trng: trng::Trng::new(&xns).unwrap(), + /// These are record the result of the most recent call to list_keys() + key_count: RefCell::new(0), + found_key_count: RefCell::new(0), } } fn ensure_async_responder(&self) { @@ -640,6 +646,32 @@ impl Pddb { Err(Error::new(ErrorKind::Other, "Xous internal error")) } } + /// cleans up inconsistencies in the PDDB. fsck-like. + pub fn sync_cleanup(&self) -> Result<()> { + let response = send_message( + self.conn, + Message::new_blocking_scalar(Opcode::WriteKeyFlush.to_usize().unwrap(), 1, 0, 0, 0) + ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + if let xous::Result::Scalar1(rcode) = response { + match FromPrimitive::from_u8(rcode as u8) { + Some(PddbRetcode::Ok) => Ok(()), + Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + Some(PddbRetcode::DiskFull) => Err(Error::new(ErrorKind::OutOfMemory, "Out of disk space, some data lost on sync")), + _ => Err(Error::new(ErrorKind::Interrupted, "Flush failed for unspecified reasons")), + } + } else { + Err(Error::new(ErrorKind::Other, "Xous internal error")) + } + } + + /// Returns a tuple of (recorded, found) keys. If record != found then the dictionary is inconsistent. + /// Only valid if the most recent call to the pddb object was list_keys() (as called below) + /// This is meant to be used only for certain maintenance routines, so the ergonomics are crap on it. + /// + /// SAFETY: caller must guarantee that list_keys() was called prior to calling this + pub unsafe fn was_listed_dict_consistent(&self) -> (u32, u32) { + (*self.key_count.borrow(), *self.found_key_count.borrow()) + } pub fn list_keys(&self, dict_name: &str, basis_name: Option<&str>) -> Result> { if dict_name.len() > (DICT_NAME_LEN - 1) { @@ -664,6 +696,8 @@ impl Pddb { code: PddbRequestCode::Uninit, token, bulk_limit: None, + key_count: 0, + found_key_count: 0, }; let mut buf = Buffer::into_buf(request) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; @@ -678,6 +712,8 @@ impl Pddb { PddbRequestCode::Uninit => return Err(Error::new(ErrorKind::ConnectionAborted, "Return code not set getting key count, server aborted?")), _ => return Err(Error::new(ErrorKind::Other, "Internal error generating key count")), }; + *self.key_count.borrow_mut() = response.key_count; + *self.found_key_count.borrow_mut() = response.found_key_count; // v2 key listing packs the key list into a larger [u8] field that should cut down on the number of messages // required to list a large dictionary by about a factor of 50. let mut key_list = Vec::::new(); @@ -749,6 +785,8 @@ impl Pddb { code: PddbRequestCode::Uninit, token, bulk_limit: None, + key_count: 0, + found_key_count: 0, }; let mut buf = Buffer::into_buf(request) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; @@ -774,6 +812,8 @@ impl Pddb { code: PddbRequestCode::Uninit, token, bulk_limit: None, + key_count: 0, + found_key_count: 0, }; let mut buf = Buffer::into_buf(request) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; @@ -939,6 +979,8 @@ impl Pddb { token: [0u32; 4], code: PddbRequestCode::BulkRead, bulk_limit: Some(size_limit.unwrap_or(DEFAULT_LIMIT)), + key_count: 0, + found_key_count: 0, }; // main loop let mut ret = Vec::new(); diff --git a/services/pddb/src/libstd/mod.rs b/services/pddb/src/libstd/mod.rs index db4c3db9a..0cd11956b 100644 --- a/services/pddb/src/libstd/mod.rs +++ b/services/pddb/src/libstd/mod.rs @@ -114,7 +114,7 @@ pub(crate) fn stat_path( // Find all keys that are in this dict. Ignore errors, since sometimes // the dict doesn't exist, which is fine. if let Some((dict_path, key_path)) = stripped_path.rsplit_once(std::path::MAIN_SEPARATOR) { - if let Some(key_list) = basis_cache + if let Some((key_list, _, _)) = basis_cache .key_list(pddb_os, dict_path, basis.as_deref()) .map_err(|e| { // log::error!("unable to get key list: {:?}", e); @@ -196,7 +196,7 @@ pub(crate) fn list_path( let dict_list = basis_cache.dict_list(pddb_os, basis.as_deref()); // Find all keys that are in this dict. Ignore errors, since sometimes // the dict doesn't exist, which is fine. - let key_list = basis_cache + let (key_list, _, _) = basis_cache .key_list(pddb_os, dict, basis.as_deref()) .map_err(|e| { // log::error!("unable to get key list: {:?}", e); @@ -809,7 +809,7 @@ pub(crate) fn list_key( .writer(*b"LiKR") .ok_or(crate::PddbRetcode::InternalError)?; - let key_list = basis_cache + let (key_list, _, _) = basis_cache .key_list(pddb_os, &key, bname.as_deref()) .or_else(|e| { log::error!( @@ -848,7 +848,7 @@ pub(crate) fn delete_dict( .or(Err(crate::PddbRetcode::InternalError))?; let dict = dict.ok_or(crate::PddbRetcode::InternalError)?; - if let Some(key_list) = basis_cache + if let Some((key_list, _, _)) = basis_cache .key_list(pddb_os, &dict, bname.as_deref()) .map_err(|e| { // log::error!("unable to get key list: {:?}", e); diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 6546a0ef3..f2d7ae1ff 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1306,8 +1306,10 @@ fn wrapped_main() -> ! { #[cfg(feature="perfcounter")] pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 0, std::line!()); key_list = match basis_cache.key_list(&mut pddb_os, dict, bname) { - Ok(list) => { + Ok((list, key_count, found_key_count)) => { log::debug!("count: {}", list.len()); + req.key_count = key_count; + req.found_key_count = found_key_count; req.code = PddbRequestCode::NoErr; Some(list) } @@ -1528,7 +1530,7 @@ fn wrapped_main() -> ! { None } ) { - Ok(list) => list.into_iter().rev().collect(), // reverse order so Vec can just pop and get "first" item + Ok((list, _, _)) => list.into_iter().rev().collect(), // reverse order so Vec can just pop and get "first" item Err(e) => { match e.kind() { std::io::ErrorKind::NotFound => { @@ -1773,7 +1775,7 @@ fn wrapped_main() -> ! { // we don't need a "replace" operation because all ops happen in-place // for now, do an expensive sync operation after every write to ensure data integrity - basis_cache.sync(&mut pddb_os, None).expect("couldn't sync basis"); + basis_cache.sync(&mut pddb_os, None, false).expect("couldn't sync basis"); } Opcode::WriteKeyStd => { @@ -1786,8 +1788,8 @@ fn wrapped_main() -> ! { } } - Opcode::WriteKeyFlush => msg_blocking_scalar_unpack!(msg, _, _, _, _, { - match basis_cache.sync(&mut pddb_os, None) { + Opcode::WriteKeyFlush => msg_blocking_scalar_unpack!(msg, cleanup, _, _, _, { + match basis_cache.sync(&mut pddb_os, None, if cleanup == 1 { true } else { false }) { Ok(_) => xous::return_scalar(msg.sender, PddbRetcode::Ok.to_usize().unwrap()).unwrap(), Err(e) => match e.kind() { std::io::ErrorKind::OutOfMemory => xous::return_scalar(msg.sender, PddbRetcode::DiskFull.to_usize().unwrap()).unwrap(), @@ -1898,7 +1900,7 @@ fn wrapped_main() -> ! { xous::return_scalar(msg.sender, 1).unwrap(); // nothing to do, nothing mounted. success! continue; } - basis_cache.sync(&mut pddb_os, None).expect("can't sync for unmount"); + basis_cache.sync(&mut pddb_os, None, false).expect("can't sync for unmount"); // unmount all the open basis first let mut mounted_bases = basis_cache.basis_list(); mounted_bases.retain(|x| x != PDDB_DEFAULT_SYSTEM_BASIS); diff --git a/services/pddb/src/tests.rs b/services/pddb/src/tests.rs index 96560759b..a5d0cae6d 100644 --- a/services/pddb/src/tests.rs +++ b/services/pddb/src/tests.rs @@ -161,7 +161,7 @@ pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache, #[cfg(not(feature = "deterministic"))] let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { - if let Ok(key_list_unord) = basis_cache.key_list(hw, dict, None) { + if let Ok((key_list_unord, _, _)) = basis_cache.key_list(hw, dict, None) { #[cfg(feature = "deterministic")] let mut key_list = BTreeSet::::new(); #[cfg(feature = "deterministic")] @@ -244,7 +244,7 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, let mut evict_iter = 0; for (evicted, evict_dict) in dict_list.iter().enumerate() { if evicted < evict_count { - let key_list_unord = basis_cache.key_list(hw, evict_dict, None).unwrap(); + let (key_list_unord, _, _) = basis_cache.key_list(hw, evict_dict, None).unwrap(); let mut key_list = BTreeSet::::new(); for s in key_list_unord { key_list.insert(s); @@ -320,7 +320,7 @@ pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, } log::info!("doing all-basis sync"); - basis_cache.sync(hw, None).unwrap(); + basis_cache.sync(hw, None, false).unwrap(); log::info!("all-basis sync done"); list_all(hw, basis_cache); @@ -366,7 +366,8 @@ pub(crate) fn list_all(hw: &mut PddbOs, basis_cache: &mut BasisCache) { }; log::debug!("{:?}", da); let mut sanity_count = 0; - for key in basis_cache.key_list(hw, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(hw, dict, None).unwrap(); + for key in key_list.iter() { let attrs = match basis_cache.key_attributes(hw, dict, key, None) { Ok(a) => a, Err(e) => {log::debug!("key not in basis, searching another basis, {:?}", e); continue}, @@ -475,7 +476,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { let mut pre_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, None).iter() { - for key in basis_cache.key_list(pddb_os, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, None).unwrap(); + for key in key_list.iter() { pre_list.insert(key.to_string()); } } @@ -504,7 +506,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { let mut merge_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, None).iter() { - for key in basis_cache.key_list(pddb_os, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, None).unwrap(); + for key in key_list.iter() { merge_list.insert(key.to_string()); } } @@ -512,7 +515,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { let mut b2_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, Some(EXTRA_BASIS)).iter() { - for key in basis_cache.key_list(pddb_os, dict, Some(EXTRA_BASIS)).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, Some(EXTRA_BASIS)).unwrap(); + for key in key_list.iter() { b2_list.insert(key.to_string()); } } @@ -521,7 +525,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { basis_cache.basis_unmount(pddb_os, EXTRA_BASIS).unwrap(); let mut post_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, None).iter() { - for key in basis_cache.key_list(pddb_os, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, None).unwrap(); + for key in key_list.iter() { post_list.insert(key.to_string()); } } @@ -533,7 +538,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { } let mut remount_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, None).iter() { - for key in basis_cache.key_list(pddb_os, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, None).unwrap(); + for key in key_list.iter() { remount_list.insert(key.to_string()); } } @@ -546,7 +552,8 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> { } let mut merge2_list = HashSet::::new(); for dict in basis_cache.dict_list(pddb_os, None).iter() { - for key in basis_cache.key_list(pddb_os, dict, None).unwrap().iter() { + let (key_list, _, _) = basis_cache.key_list(pddb_os, dict, None).unwrap(); + for key in key_list.iter() { merge2_list.insert(key.to_string()); } } diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 328816a6f..671dcaf15 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -393,6 +393,26 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } } + "consistency" => { + if let Some(dict) = tokens.next() { + match self.pddb.list_keys(dict, None) { + Ok(_list) => { + unsafe{ + let (keys, found_keys) = self.pddb.was_listed_dict_consistent(); + write!(ret, "reported keys: {}, found keys: {}", keys, found_keys).ok(); + log::info!("reported keys: {}, found keys: {}", keys, found_keys); + }; + } + Err(_) => write!(ret, "{} does not exist or other error", dict).ok().unwrap_or(()), + } + } else { + write!(ret, "Missing dictionary name").unwrap(); + } + } + // Warning: this is experimental, it's advised you don't call this unless you know what you're doing. + "cleanup" => { + write!(ret, "Cleanup result code: {:?}\n", self.pddb.sync_cleanup()).ok(); + } #[cfg(feature="pddbtest")] "deltest" => { const JUNK_MIN: usize = 512; From 145e0469bccf24a85e1f378e28415e276ab62472 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 28 Jan 2023 19:08:01 +0800 Subject: [PATCH 47/80] fix another edge case in key deletion invalidated keys that were not dirty were not being removed also added routine to fill keys that are evicted from the cache during the update routine --- services/pddb/src/api.rs | 3 ++ services/pddb/src/backend/basis.rs | 11 ++-- services/pddb/src/backend/dictionary.rs | 70 ++++++++++++++++++++----- services/pddb/src/lib.rs | 7 +++ services/pddb/src/main.rs | 7 +++ services/shellchat/src/cmds/pddb_cmd.rs | 4 +- 6 files changed, 86 insertions(+), 16 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 61e85b1db..faa05ae61 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -245,6 +245,9 @@ pub(crate) enum Opcode { /// Bulk delete of keys within a dictionary; if non-existent keys are specified, no error is returned. DictBulkDelete = 55, + /// Prune the cache. Used mainly for diagnostics. + Prune = 56, + /// This key type could not be decoded InvalidOpcode = u32::MAX as _, } diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index ae63a7f7b..a6a202854 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -1601,6 +1601,7 @@ impl BasisCacheEntry { // make, but for now, let's do it with a dumb O(N) scan through the KeyCacheEntry, running under // the assumption that the KeyCacheEntry doesn't ever get to a very large N. let next_vpage = VirtAddr::new(cur_vpage.get() + VPAGE_SIZE as u64).unwrap(); + let mut invalidate = Vec::::new(); // remember which keys to invalidate for (key_name, key) in dict.keys.iter_mut() { /*if key_name.contains("dict2|key6|len2347") { log::warn!("TRACING: {}", key_name); @@ -1631,8 +1632,14 @@ impl BasisCacheEntry { } else { log::debug!("proposed key fell outside of our vpage: {} vpage{:x}/vaddr{:x}", key_name, cur_vpage.get(), key.descriptor_vaddr(dict_offset)); } + } else { + invalidate.push(key_name.to_string()); } } + // remove the invalidated keys from the cache + for key in invalidate { + dict.keys.remove(&key); + } // 3. merge the vpage modifications into the disk let mut page = if let Some(data) = hw.data_decrypt_page(&self.cipher, &self.aad, &pp) { @@ -1663,7 +1670,7 @@ impl BasisCacheEntry { // exit the loop let mut found_next = false; for key in dict.keys.values() { - if !key.clean && key.flags.valid() { + if !key.clean && key.flags.valid() || !key.flags.valid() { found_next = true; // note: we don't care *which* vpage we do next -- so we just break after finding the first one vpage_num = key.descriptor_vpage_num(); @@ -1676,8 +1683,6 @@ impl BasisCacheEntry { } sync_count += 1; } - // remove invalid keys from the dictionary cache - dict.keys.retain(|_name, entry| entry.flags.valid()); log::debug!("done syncing dict"); dict.clean = true; diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index dc8397b94..05897e6ce 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -239,10 +239,25 @@ impl DictCacheEntry { /// if cache is hot or key was found on search. pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str) -> bool { - // only fill if the key isn't in the cache. - if !self.keys.contains_key(name_str) { + let mut data_cache = PlaintextCache { data: None, tag: None }; + // if the key isn't valid, evict it now + self.keys.retain(|name, value| (name != name_str) || ((name == name_str) && value.flags.valid())); + // only fill if the key isn't in the cache, or if the data section has been evicted + let needs_fill = if let Some(entry) = self.keys.get(name_str) { + if entry.data.is_none() { + self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, name_str); + // it should be filled, so no further processing is needed + false + } else { + // data is already there + false + } + } else { + // entry doesn't exist + true + }; + if needs_fill { log::debug!("searching for key {}", name_str); - let mut data_cache = PlaintextCache { data: None, tag: None }; let mut try_entry = 1; let mut key_count = 0; let mut index_cache = PlaintextCache { data: None, tag: None }; @@ -496,17 +511,45 @@ impl DictCacheEntry { kcache.clean = false; // it's a small key; note that we didn't consult the *size* of the key to determine its pool type: // small-sized keys can still end up in the "large" space if the small pool allocation is exhausted. - if let KeyCacheData::Small(cache_data) = kcache.data.as_mut().expect("small pool should all have their data 'hot' if the index entry is also in cache") { - cache_data.clean = false; - // grow the data cache to accommodate the necessary length; this should be efficient because we reserved space when the vector was allocated - while cache_data.data.len() < data.len() + offset { - cache_data.data.push(0); + match kcache.data.as_mut() { + Some(KeyCacheData::Small(cache_data)) => { + cache_data.clean = false; + // grow the data cache to accommodate the necessary length; this should be efficient because we reserved space when the vector was allocated + while cache_data.data.len() < data.len() + offset { + cache_data.data.push(0); + } + for (&src, dst) in data.iter().zip(cache_data.data[offset..].iter_mut()) { + *dst = src; + } } - for (&src, dst) in data.iter().zip(cache_data.data[offset..].iter_mut()) { - *dst = src; + Some(KeyCacheData::Large(_)) => { + panic!("Key allocated to small area but its cache data was not of the small type"); + } + None => { + // cache data was evicted, fill it + assert!(kcache.flags.valid(), "Entry to be filled should be valid"); + let pool_index = small_storage_index_from_key(&kcache, self.index).expect("pool should be valid"); + let ksp = &mut self.small_pool[pool_index]; + assert!(ksp.contents.contains(&name.to_string()), "Entry to fill isn't in the expected pool"); + let data_vaddr = small_storage_base_vaddr_from_indices(self.index, pool_index); + let mut data_cache = PlaintextCache { data: None, tag: None }; + data_cache.fill(hw, v2p_map, cipher, &self.aad, VirtAddr::new(data_vaddr).unwrap()); + if let Some(page) = data_cache.data.as_ref() { + let start_offset = size_of::() + (kcache.start % VPAGE_SIZE as u64) as usize; + let mut data = page[start_offset..start_offset + kcache.len as usize].to_vec(); + data.reserve_exact((kcache.reserved - kcache.len) as usize); + kcache.data = Some(KeyCacheData::Small( + KeySmallData { + clean: true, + data + } + )); + } else { + let error = + format!("Key {}'s data region at pp: {:x?} va: {:x} is unreadable", name, data_cache.tag, kcache.start); + panic!("{}", error); + } } - } else { - panic!("Key allocated to small area but its cache data was not of the small type"); } // check if we grew the length if kcache.len < (data.len() + offset) as u64 { @@ -1090,6 +1133,9 @@ impl DictCacheEntry { if kcache.data.is_some() { let pruned = kcache.size(); kcache.data.take(); // this effectively frees up the key cache data + // mark the key's pool as unclean, so it is processed for filling + let pool_index = small_storage_index_from_key(&kcache, self.index).expect("index missing"); + self.small_pool[pool_index].clean = false; pruned } else { 0 diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 50e75342d..c403f558e 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -856,6 +856,13 @@ impl Pddb { Message::new_blocking_scalar(Opcode::FlushSpaceUpdate.to_usize().unwrap(), 0, 0, 0, 0) ).expect("couldn't send FlushSpaceUpdate"); } + /// Manually prune the PDDB cache. + /// Mostly provided for force-triggering for testing; normally this is done automatically + pub fn manual_prune(&self) { + send_message(self.conn, + Message::new_blocking_scalar(Opcode::Prune.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't send FlushSpaceUpdate"); + } /// Rekey the PDDB. This can be a very long-running blocking operation that will definitely. /// interrupt normal user flow. pub fn rekey_pddb(&self, op: PddbRekeyOp) -> Result<()> { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index f2d7ae1ff..de3208c2a 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1838,6 +1838,13 @@ fn wrapped_main() -> ! { pddb_os.reset_dont_ask_init(); xous::return_scalar(msg.sender, 1).ok(); } + Opcode::Prune => { + log::info!("PDDB prune manual request: {} heap, {} cache", latest_heap, basis_cache.cache_size()); + let pruned = basis_cache.cache_prune(&mut pddb_os, HEAP_GC_TARGET); + latest_heap = heap_usage(); + log::info!("{} pruned, now: {} heap, {} cache", pruned, latest_heap, basis_cache.cache_size()); + xous::return_scalar(msg.sender, 1).ok(); + } #[cfg(not(target_os = "xous"))] Opcode::DangerousDebug => { let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 671dcaf15..68c1b58a2 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -1135,9 +1135,11 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } } - #[cfg(not(target_os = "xous"))] "prune" => { + #[cfg(not(target_os = "xous"))] self.pddb.dbg_prune().ok(); + #[cfg(target_os = "xous")] + self.pddb.manual_prune(); write!(ret, "Prune finished").ok(); } #[cfg(not(target_os = "xous"))] From 27131f6e9aabaaecdb5c5ed01006582257dbaa85 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:15:34 +0800 Subject: [PATCH 48/80] add another test case to `deltest` simulates a very typical pattern of remove an element then populate it --- services/shellchat/src/cmds/pddb_cmd.rs | 73 ++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 68c1b58a2..60fd16b9d 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -420,7 +420,10 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { const TOTAL_KEYS: usize = 40; self.pddb.delete_dict("deltest", None).ok(); for outer_iter in 0..512 { - log::info!("fill junk iter {}", outer_iter); + log::info!("***************************fill junk iter {}*************************", outer_iter); + if outer_iter == 246 { + // self.pddb.dbg_set_debug().unwrap(); // used to dump debug when we find an error + } let mut junk_keys = Vec::::new(); let mut junk_checksum = std::collections::HashMap::::new(); for index in 0..TOTAL_KEYS { // this should allocate a few key small pools @@ -467,6 +470,57 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { self.pddb.flush_space_update(); } } + // single delete and re-create + // _env.ticktimer.sleep_ms(1000).ok(); + log::info!("***************************recreate junk iter {}*************************", outer_iter); + log::info!("key list: {:?}", self.pddb.list_keys("deltest", None).unwrap()); + for test_index in (0..TOTAL_KEYS).rev() { // test both rev and non-rev variants to catch more corner cases + log::debug!("recreating test index {}", test_index); + let junkname = format!("junk{}", test_index); + self.pddb.delete_key("deltest", &junkname, None).ok(); + let mut junk = Vec::::new(); + let total_junk = JUNK_MIN + _env.trng.get_u32().unwrap() as usize % JUNK_VAR; + let mut checksum = 0; + for i in 0..total_junk { + junk.push((i as usize + test_index) as u8); + checksum += ((i as usize + test_index) as u8) as usize; + } + match self.pddb.get( + "deltest", + &junkname, + None, true, true, + None, + None:: + ) { + Ok(mut junk_key) => { + match junk_key.write_all(&junk) { + Ok(_) => { + log::debug!("wrote {} of len {}", junkname, total_junk); + } + Err(e) => { + log::error!("couldn't write {}: {:?}", junkname, e); + } + } + } + Err(e) => { + log::error!("couldn't allocate junk key {}: {:?}", junkname, e); + } + } + log::debug!("name: {}, checksum: {}", junkname, checksum); + junk_checksum.insert(junkname.to_string(), checksum); + junk_keys[test_index] = junkname; + + #[cfg(not(target_os = "xous"))] + if _env.trng.get_u32().unwrap() as usize % 1024 == 13 { + log::info!("pruning 1"); + self.pddb.dbg_prune().ok(); + } + if _env.trng.get_u32().unwrap() as usize % 8192 == 27 { + log::info!("flushing fscb 1"); + self.pddb.flush_space_update(); + } + } + log::info!("***************************delete junk iter {}*************************", outer_iter); let mut prev_del = "uninit".to_string(); while junk_keys.len() > 0 { let to_remove = _env.trng.get_u32().unwrap() as usize % junk_keys.len(); @@ -477,7 +531,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { // self.pddb.sync().ok(); let check = self.pddb.list_keys("deltest", None).unwrap(); assert!(check.len() == (junk_keys.len() - 1)); - for (_key_index, key) in check.iter().enumerate() { + for (key_index, key) in check.iter().enumerate() { if key == &junk_keys[to_remove] { panic!("Removed key was not removed"); } @@ -497,12 +551,25 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { if checksum != *junk_checksum.get(key).unwrap() { log::info!("readback {}", readback_len); log::info!("previously deleted: {} / just deleted: {}", prev_del, junk_keys[to_remove]); - log::info!("array: {:?}", junk); + log::info!("array start: {:?}", junk); + log::info!("array end: {:?}", &junk[readback_len - 128..readback_len]); log::error!("Key data did not match name: {}, checksum: {}, expected: {}", key, checksum, *junk_checksum.get(key).unwrap() ); + let mut check: u8 = key_index as u8; + let mut deviations = 0; + for (index, &j) in junk.iter().enumerate() { + if j != check { + log::error!("deviation at {} {} <- {}", index, j, check); + deviations += 1; + if deviations > 32 { + break; + } + } + check += 1; + } self.pddb.dbg_dump("deltest").unwrap(); panic!("data mismatch!"); } From d77552899ba06d518118a897ba7259ad8a768ca8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:16:56 +0800 Subject: [PATCH 49/80] add some code to help find OOM keys --- apps/vault/src/actions.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 4392d4b77..c2ad44b6c 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -928,6 +928,19 @@ impl<'a> ActionManager<'a> { log::warn!("Couldn't interpret password record: {}", key.name); } } else { + /* // this code helps to trace down which key had OOM'd, if it turns out to be an issue + let mut oom_key = String::new(); + make_pw_name(&pw_rec.description, &pw_rec.username, &mut oom_key); + let li = ListItem { + name: oom_key.to_string(), + extra: "Maybe OOM record".to_string(), + dirty: true, + guid: key.name, + atime: 0, + count: 0, + }; + il.insert(li.key(), li); // this push is very slow, but we only have to do it once on boot + */ oom_keys += 1; } #[cfg(feature="vaultperf")] From df2dda32bec2930d1a0ef1608801e61bf7173ec7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:17:42 +0800 Subject: [PATCH 50/80] add api call to increase PDDB debug level at runtime --- services/pddb/src/api.rs | 1 + services/pddb/src/lib.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index faa05ae61..5b9aaa9b8 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -558,6 +558,7 @@ pub enum DebugRequest { Dump = 0, Remount = 1, Prune = 2, + SetDebug = 3, } #[cfg(test)] diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index c403f558e..46c618ea1 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -1130,6 +1130,18 @@ impl Pddb { buf.lend(self.conn, Opcode::DangerousDebug.to_u32().unwrap()) .or(Err(Error::new(ErrorKind::Other, "Xous internal error"))).map(|_| ()) } + /// Turns on debug spew + #[cfg(not(target_os = "xous"))] + pub fn dbg_set_debug(&self) -> Result<()> { + let ipc = PddbDangerousDebug { + request: DebugRequest::SetDebug, + dump_name: xous_ipc::String::new(), + }; + let buf = Buffer::into_buf(ipc) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; + buf.lend(self.conn, Opcode::DangerousDebug.to_u32().unwrap()) + .or(Err(Error::new(ErrorKind::Other, "Xous internal error"))).map(|_| ()) + } #[cfg(all(feature="pddbtest", feature="autobasis"))] pub fn basis_testing(&self, config: &[Option; 32]) { From 70c41ef244358005311feea02f1719b9631285fe Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:17:57 +0800 Subject: [PATCH 51/80] increase threshold for GC on PDDB for some reason it's being called a bit earlier than i expected. --- services/pddb/src/main.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index de3208c2a..aaf630386 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -601,8 +601,8 @@ fn wrapped_main() -> ! { let mut latest_heap: usize = 0; let mut latest_cache: usize = 0; const HEAP_LARGER_LIMIT: usize = 2048 * 1024; - const HEAP_GC_THRESH: usize = 1500 * 1024; // the largel limit is at 2048kiB, so try cleaning up at 1500k - const HEAP_GC_TARGET: usize = 1300 * 1024; // how much to try cleaning out in any one go. + const HEAP_GC_THRESH: usize = 1800 * 1024; // the larger limit is at 2048kiB, set this smaller + const HEAP_GC_TARGET: usize = 1500 * 1024; // how much to try cleaning out in any one go. let new_limit = HEAP_LARGER_LIMIT; let result = xous::rsyscall(xous::SysCall::AdjustProcessLimit( xous::Limits::HeapMaximum as usize, @@ -747,11 +747,11 @@ fn wrapped_main() -> ! { } latest_heap = current_heap; latest_cache = current_cache; - if (latest_heap > initial_heap - && (latest_heap - initial_heap) > HEAP_GC_THRESH) + if ((latest_heap > initial_heap) + && ((latest_heap - initial_heap) > HEAP_GC_THRESH)) // this line is mostly so this triggers occasionally in hosted mode where heap usage is faked; // in practice heap threshold will always hit before cache threshold - || current_cache > HEAP_GC_THRESH { + || (current_cache > HEAP_GC_THRESH) { log::info!("PDDB trim threshold reached: {} heap, {} cache", latest_heap, basis_cache.cache_size()); let pruned = basis_cache.cache_prune(&mut pddb_os, HEAP_GC_TARGET); latest_heap = heap_usage(); @@ -1451,8 +1451,8 @@ fn wrapped_main() -> ! { let token = pbuf.token; if let Some(rec) = token_dict.get(&token) { for basis in basis_cache.access_list().iter() { - let temp = if let Some (name) = &rec.basis {Some(name)} else {Some(basis)}; - log::debug!("read (spec: {:?}){:?} {} len {} pos {}", rec.basis, temp, rec.key, pbuf.len, pbuf.position); + // let temp = if let Some (name) = &rec.basis {Some(name)} else {Some(basis)}; + // log::debug!("read (spec: {:?}){:?} {} len {} pos {}", rec.basis, temp, rec.key, pbuf.len, pbuf.position); #[cfg(feature="perfcounter")] pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 4, std::line!()); match basis_cache.key_read(&mut pddb_os, @@ -1872,11 +1872,12 @@ fn wrapped_main() -> ! { }, DebugRequest::Prune => { log::info!("initiating prune"); - log::set_max_level(log::LevelFilter::Debug); basis_cache.cache_prune(&mut pddb_os, 262144); - log::set_max_level(log::LevelFilter::Info); log::info!("prune finished"); } + DebugRequest::SetDebug => { + log::set_max_level(log::LevelFilter::Debug); + } } } #[cfg(all(feature="pddbtest", feature="autobasis"))] From 99a88ff25a3e1b88b3d3d63a6dff52c8a1d5f637 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:19:24 +0800 Subject: [PATCH 52/80] only invalidate the key entry if it is within the current range of interest previously, we were invalidating at the first instance of encountering the key; instead we should only invalidate once we've processed it in our range of interest. --- services/pddb/src/backend/basis.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index a6a202854..782259a31 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -482,7 +482,7 @@ impl BasisCache { bytes_read += 1; } if bytes_read != data.len() { - log::debug!("Key shorter than read buffer {}:{} ({}/{})", dict, key, bytes_read, data.len()); + log::trace!("Key shorter than read buffer {}:{} ({}/{})", dict, key, bytes_read, data.len()); } return Ok(bytes_read) } else { @@ -1115,7 +1115,7 @@ impl BasisCache { match basis.dicts.get_mut(&ka.dict) { Some(dentry) => { let freed = dentry.evict_keycache_entry(&ka.key); - log::debug!("pruned {} bytes from atime {} / total {} key {}", freed, ka.atime, pruned, ka.key); + log::trace!("pruned {} bytes from atime {} / total {} key {}", freed, ka.atime, pruned, ka.key); pruned += freed; if pruned >= target_bytes { return pruned; @@ -1612,7 +1612,7 @@ impl BasisCacheEntry { if key.flags.valid() { if key.descriptor_vaddr(dict_offset) >= cur_vpage && key.descriptor_vaddr(dict_offset) < next_vpage { - log::debug!("merging in key {}", key_name); + log::trace!("merging in key {}", key_name); // key is within the current page, add it to the target list let mut dk_entry = DictKeyEntry::default(); let kn = KeyName::try_from_str(key_name).or(Err(Error::new(ErrorKind::InvalidInput, "key name invalid: invalid utf-8 or length")))?; @@ -1633,11 +1633,16 @@ impl BasisCacheEntry { log::debug!("proposed key fell outside of our vpage: {} vpage{:x}/vaddr{:x}", key_name, cur_vpage.get(), key.descriptor_vaddr(dict_offset)); } } else { - invalidate.push(key_name.to_string()); + if key.descriptor_vaddr(dict_offset) >= cur_vpage && + key.descriptor_vaddr(dict_offset) < next_vpage { + dk_vpage.elements[key.descriptor_index.get() as usize % DK_PER_VPAGE] = None; + invalidate.push(key_name.to_string()); + } } } // remove the invalidated keys from the cache for key in invalidate { + log::debug!("removing invalidated key: {}", key); dict.keys.remove(&key); } @@ -1669,16 +1674,18 @@ impl BasisCacheEntry { // 4. Check for dirty keys, if there are still some, update vpage_num to target them; otherwise // exit the loop let mut found_next = false; - for key in dict.keys.values() { + for (name, key) in dict.keys.iter() { if !key.clean && key.flags.valid() || !key.flags.valid() { found_next = true; // note: we don't care *which* vpage we do next -- so we just break after finding the first one vpage_num = key.descriptor_vpage_num(); + log::debug!("sync searching again because key {} was valid {} clean {}", name, key.clean, key.flags.valid()); + sync_count += 1; break; } } if !found_next { - log::debug!("breaking after syncing {} keys", sync_count); + log::debug!("breaking after syncing {} iterations", sync_count); break; } sync_count += 1; From e500588360fc116ba086acf7f65da1992803892f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:21:31 +0800 Subject: [PATCH 53/80] fix bugs in key invalidation/eviction logic 1. track evictions as separate from cleanliness. not clean means the data is dirty and needs writing back. eviction simply means the data needs be re-retrieved from disk. 2. actually fill the data when we do a sync -- previously we only copied the old data into the new page but then forgot to allocate the data and assign it to the cache (oops) 3. sync the small pools after doing a remove inside the update() command to make sure we've met invariants for future calls to cache fills so many corner cases, i'm going around in circles.... --- services/pddb/src/backend/dictionary.rs | 74 ++++++++++++++++++++----- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 05897e6ce..7e3ab7c5d 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -240,16 +240,19 @@ impl DictCacheEntry { pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str) -> bool { let mut data_cache = PlaintextCache { data: None, tag: None }; - // if the key isn't valid, evict it now - self.keys.retain(|name, value| (name != name_str) || ((name == name_str) && value.flags.valid())); // only fill if the key isn't in the cache, or if the data section has been evicted let needs_fill = if let Some(entry) = self.keys.get(name_str) { - if entry.data.is_none() { - self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, name_str); - // it should be filled, so no further processing is needed - false + if entry.flags.valid() { + if entry.data.is_none() { + self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, name_str); + // it should be filled, so no further processing is needed + false + } else { + // data is already there + false + } } else { - // data is already there + // invalid entry false } } else { @@ -287,12 +290,12 @@ impl DictCacheEntry { assert!(cache_pp.page_number() == pp.page_number(), "cache inconsistency error"); let mut keydesc = KeyDescriptor::default(); let start = size_of::() + (try_entry % DK_PER_VPAGE) * DK_STRIDE; - for (&src, dst) in cache[start..start + DK_STRIDE].iter().zip(keydesc.deref_mut().iter_mut()) { - *dst = src; - } + // there is 1 byte of extra padding that causes the slices of keydesc.deref_mut() to be 128 bytes long instead of 127 bytes... + keydesc.deref_mut()[..DK_STRIDE].copy_from_slice(&cache[start..start + DK_STRIDE]); let kname = std::str::from_utf8(&keydesc.name.data[..keydesc.name.len as usize]).expect("key is not valid utf-8"); if keydesc.flags.valid() { if kname == name_str { + log::debug!("found {} at entry {}/{}", name_str, try_entry, start); let kcache = KeyCacheEntry { start: keydesc.start, len: keydesc.len, @@ -326,6 +329,7 @@ impl DictCacheEntry { } fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, data_cache: &mut PlaintextCache, key_name: &str) { + let mut filled_index: Option = None; if let Some(kcache) = self.keys.get_mut(key_name) { if !kcache.flags.valid() { // nothing to fill, the key entry isn't valid @@ -344,7 +348,7 @@ impl DictCacheEntry { self.small_pool.push(ksp); } let ksp = &mut self.small_pool[pool_index]; - if !ksp.clean { + if !ksp.clean || ksp.evicted { if !ksp.contents.contains(&key_name.to_string()) { log::trace!("creating ksp entry for {}", key_name); ksp.contents.push(key_name.to_string()); @@ -380,6 +384,7 @@ impl DictCacheEntry { data } )); + filled_index = Some(pool_index); } else { log::error!("Key {}'s data region at pp: {:x?} va: {:x} is unreadable", key_name, data_cache.tag, kcache.start); } @@ -391,6 +396,24 @@ impl DictCacheEntry { log::error!("try_fill_small_key() can only be called after the key cache entry has been allocated"); panic!("consistency error"); } + if let Some(index) = filled_index { + // check to see if this fill can clear the eviction status of the entire pool + let ksp = &mut self.small_pool[index]; + let mut all_present = true; + for key in ksp.contents.iter() { + if let Some(kcache) = self.keys.get(key) { + if kcache.flags.valid() { + if kcache.data.is_none() { + all_present = false; + break; + } + } + } + } + if all_present { + ksp.evicted = false; + } + } } /// To be called only to re-fill key entries whose data have been evicted by a prune. pub (crate) fn refill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, @@ -474,6 +497,7 @@ impl DictCacheEntry { log::debug!("update/extend: removing {}", name); // now remove the old key entirely self.key_remove(hw, v2p_map, cipher, name, false); + self.sync_small_pool(hw, v2p_map, cipher); if update_data.len() > 4 { // just make sure that this log call doesn't fail on an index violation... log::debug!("update/extend: re-adding {} with data len {}: {:x?}...", name, update_data.len(), &update_data[..4]); } @@ -530,7 +554,12 @@ impl DictCacheEntry { assert!(kcache.flags.valid(), "Entry to be filled should be valid"); let pool_index = small_storage_index_from_key(&kcache, self.index).expect("pool should be valid"); let ksp = &mut self.small_pool[pool_index]; - assert!(ksp.contents.contains(&name.to_string()), "Entry to fill isn't in the expected pool"); + if !ksp.contents.contains(&name.to_string()) { + log::warn!("illegal state: key_update fill key pool contents: {:?}", ksp.contents); + let tt = ticktimer_server::Ticktimer::new().unwrap(); + tt.sleep_ms(1000).ok(); + panic!("Entry to fill isn't in the expected pool"); + } let data_vaddr = small_storage_base_vaddr_from_indices(self.index, pool_index); let mut data_cache = PlaintextCache { data: None, tag: None }; data_cache.fill(hw, v2p_map, cipher, &self.aad, VirtAddr::new(data_vaddr).unwrap()); @@ -697,6 +726,7 @@ impl DictCacheEntry { ksp.contents.push(name.to_string()); ksp.avail -= reservation as u16; ksp.clean = false; + log::debug!("ksp.clean = false {}", name); self.small_pool_free.push(KeySmallPoolOrd { avail: ksp.avail, index: pool_candidate.index }); pool_candidate.index } else { @@ -706,6 +736,7 @@ impl DictCacheEntry { ksp.contents.push(name.to_string()); ksp.avail -= reservation as u16; ksp.clean = false; + log::debug!("ksp.clean = false {}", name); // update the free pool with the current candidate // we don't subtract 1 from len because we're about to push the ksp onto the end of the small_pool, consuming it self.small_pool_free.push(KeySmallPoolOrd { avail: ksp.avail, index: self.small_pool.len() }); @@ -808,6 +839,7 @@ impl DictCacheEntry { /// overwritten in paranoid mode, but large keys are. pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str, paranoid: bool) { + log::debug!("removing key {}", name_str); if paranoid { // large records are paranoid-erased, by default, because of the pool-reuse problem. unimplemented!("Paranoid erase for small records not yet implemented. Calling sync after an update, however, effectively does a paranoid erase."); @@ -819,6 +851,7 @@ impl DictCacheEntry { let mut need_free_key: Option = None; if let Some(kcache) = self.keys.get_mut(&name) { if !kcache.flags.valid() { + log::debug!("ensure of invalid key: {}", name_str); assert!(kcache.clean == false, "keys that are invalidated should be marked as not clean"); // key was previously deleted, already in cache, but not flushed. Nothing to do here. return; @@ -834,6 +867,7 @@ impl DictCacheEntry { // handle the small pool case let ksp = &mut self.small_pool[small_index]; let err_static = format!("Small pool did not contain the element we expected: {}, len: {}", &name, kcache.len); + log::debug!("ksp swap_remove({}) flags: {:?}", name, kcache.flags); ksp.contents.swap_remove(ksp.contents.iter().position(|s| *s == name) .expect(&err_static)); assert!(kcache.reserved <= SMALL_CAPACITY as u64, "error in small key entry size"); @@ -865,6 +899,7 @@ impl DictCacheEntry { } // free up the key index in the dictionary, if necessary if let Some(key_to_free) = need_free_key { + log::debug!("freeing key: {}", key_to_free); self.put_free_key_index(key_to_free); self.key_count -= 1; log::debug!("key_count: {}", self.key_count); @@ -984,6 +1019,12 @@ impl DictCacheEntry { page[size_of::() + pool_offset .. size_of::() + pool_offset + kcache.len as usize] .copy_from_slice(&old_page[start_offset..start_offset + kcache.len as usize]); + // now take the previous data and shove it back in our cache + let data = old_page[start_offset..start_offset + kcache.len as usize].to_vec(); + kcache.data = Some(KeyCacheData::Small(KeySmallData { + clean: true, + data, + })); } else { log::error!("Key {}'s data region at pp: {:x?} va: {:x} is unreadable", key_name, data_cache.tag, kcache.start); } @@ -999,8 +1040,10 @@ impl DictCacheEntry { // now commit the sector to disk hw.data_encrypt_and_patch_page(cipher, &self.aad, &mut page, &pp); entry.clean = true; + entry.evicted = false; + log::debug!("Key pool[{}] is now clean, and not evicted: {:?}", index, entry.contents); } else { - log::debug!("Key pool entry was clean, no need to sync: {:?}", entry.contents); + log::debug!("Key pool[{}] was clean, no need to sync: {:?}", index, entry.contents); } } // we now have a bunch of dirty kcache entries. You should call `dict_sync` shortly after this to synchronize those entries to disk. @@ -1130,12 +1173,13 @@ impl DictCacheEntry { if kcache.flags.valid() && !kcache.clean { return 0; } - if kcache.data.is_some() { + if let Some(KeyCacheData::Small(_)) = kcache.data { // can only prune small records let pruned = kcache.size(); kcache.data.take(); // this effectively frees up the key cache data // mark the key's pool as unclean, so it is processed for filling let pool_index = small_storage_index_from_key(&kcache, self.index).expect("index missing"); - self.small_pool[pool_index].clean = false; + self.small_pool[pool_index].evicted = true; + log::debug!("pruned {} bytes from key {} / evicted ksp index: {}", pruned, key, pool_index); pruned } else { 0 From 004599543a21dbcd5fd13fb61c0b9dc847b2f93c Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 Jan 2023 04:26:08 +0800 Subject: [PATCH 54/80] add the evicted record and remove a debug line i forgot to trim --- services/pddb/src/backend/dictionary.rs | 4 +--- services/pddb/src/backend/key.rs | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 7e3ab7c5d..e3f7b2705 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -555,9 +555,7 @@ impl DictCacheEntry { let pool_index = small_storage_index_from_key(&kcache, self.index).expect("pool should be valid"); let ksp = &mut self.small_pool[pool_index]; if !ksp.contents.contains(&name.to_string()) { - log::warn!("illegal state: key_update fill key pool contents: {:?}", ksp.contents); - let tt = ticktimer_server::Ticktimer::new().unwrap(); - tt.sleep_ms(1000).ok(); + log::warn!("illegal state: key_update fill key pool contents: {:?}, missing {}", ksp.contents, name); panic!("Entry to fill isn't in the expected pool"); } let data_vaddr = small_storage_base_vaddr_from_indices(self.index, pool_index); diff --git a/services/pddb/src/backend/key.rs b/services/pddb/src/backend/key.rs index d7951cb5a..bda27519b 100644 --- a/services/pddb/src/backend/key.rs +++ b/services/pddb/src/backend/key.rs @@ -181,7 +181,10 @@ pub(crate) struct KeySmallPool { pub(crate) contents: Vec::, /// keeps track of the available space within the pool, avoiding an expensive lookup every time we want to query the available space pub(crate) avail: u16, + /// if false, contents are modified and need syncing to disk pub(crate) clean: bool, + /// if true, some elements of this pool were evicted to make room in RAM + pub(crate) evicted: bool, } impl KeySmallPool { pub(crate) fn new() -> KeySmallPool { @@ -189,6 +192,7 @@ impl KeySmallPool { contents: Vec::::new(), avail: SMALL_CAPACITY as u16, clean: false, + evicted: false, } } } From 0572eed4b4db9d246644bddbb29d73cc5cf7dfb7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:00:19 +0800 Subject: [PATCH 55/80] xous-ticktimer: don't panic in an interrupt If we are unable to send a message in an interrupt, simply ignore it. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index 6a4ffdb5d..bf3631278 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -85,8 +85,7 @@ mod implementation { // Safe because we're in an interrupt, and this interrupt is only // enabled when this value is not None. let response = xtt.current_response.take().unwrap(); - xous::return_scalar(response.sender, response.kind as usize) - .expect("couldn't send response"); + xous::return_scalar(response.sender, response.kind as usize).ok(); // Disable the timer xtt.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0); From 2707d744180e45d84ea334312eee4cf8c55391f6 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:01:18 +0800 Subject: [PATCH 56/80] ticktimer: use sleep_heap.pop_first() This function was stabilized in Rust 1.66.0. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index bf3631278..f4542e3ee 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -465,11 +465,7 @@ fn start_sleep( sleep_heap: &mut BTreeMap, // min-heap with Reverse ) { // If there are items in the sleep heap, take the next item that will expire. - // TODO: Replace this with `.min()` when it's stabilized: - // https://github.com/rust-lang/rust/issues/62924 - let next_timeout_msec = sleep_heap.iter().min().map(|(msec, _)| *msec); - if let Some(msec) = next_timeout_msec { - let next_response = sleep_heap.remove(&msec).unwrap(); + if let Some((_msec, next_response)) = sleep_heap.pop_first() { #[cfg(feature = "debug-print")] info!( "scheduling a response at {} to {} (heap: {:?})", From bd530f7a746f659bf91cb90df2f2958954bce4af Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:02:36 +0800 Subject: [PATCH 57/80] ticktimer: only allow RecalculateSleep from ourselves Rewrite the RecalculateSleep opcode handler so that it will only be processed if sent by our own process. As part of this rewrite, use the new `let else` pattern. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 79 ++++++++++++++++++----------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index f4542e3ee..4cec85ac0 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -655,41 +655,58 @@ fn main() -> ! { } } api::Opcode::RecalculateSleep => { - // let timeout_queue = timeout_heap.entry(msg.sender.pid()).or_default(); - if let Some(args) = msg.body.scalar_message() { - // If this is a Timeout message that fired, remove it from the Notification list - let sender = args.arg1; - let request_kind = args.arg2; - let condvar = args.arg3; - let sender_pid = xous::MessageSender::from_usize(sender).pid(); + if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32 + != xous::process::id() + { + recalculate_sleep(&mut ticktimer, &mut sleep_heap, None); + continue; + } - // If we're being asked to recalculate due to a timeout expiring, drop the sent - // message from the `entries` list. - // the first check confirms that the origin of the RecalculateSleep message is the Ticktimer, - // to prevent third-party servers from issuing the command and thus distorting the sleep - // calculations (since this is a public API, anything could happen). - if (msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32) - == xous::process::id() - && (request_kind == RequestKind::Timeout as usize) - && (sender > 0) - { - let entries = notify_hash - .entry(sender_pid) - .or_default() - .entry(condvar) - .or_default(); - let mut idx = None; - for (i, val) in entries.iter().enumerate() { - if val.to_usize() == sender { - idx = Some(i); - break; - } + let Some(args) = msg.body.scalar_message() else { + continue + }; + + // If this is a Timeout message that fired, remove it from the Notification list + let sender = args.arg1; + let request_kind = args.arg2; + let condvar = args.arg3; + let sender_pid = xous::MessageSender::from_usize(sender).pid(); + + // If we're being asked to recalculate due to a timeout expiring, drop the sent + // message from the `entries` list. + if (request_kind == RequestKind::Timeout as usize) && (sender > 0) { + let mut expired_values = 0; + let awaiting = notify_hash + .entry(sender_pid) + .or_default() + .entry(condvar) + .or_default(); + + awaiting.retain(|e| { + if e.to_usize() == sender { + expired_values += 1; + false + } else { + true } - if let Some(idx) = idx { - entries.remove(idx); + }); + + // Remove each entry in the timeout set + for (_msecs, timer_request) in sleep_heap.iter() { + if timer_request.sender.to_usize() == sender { + log::error!("we were just notified of PID {:?}/condvar {:08x} expiring, yet it's still in the sleep heap with sender {:08x}", sender_pid, condvar, timer_request.sender.to_usize()); } - // log::trace!("new entries for PID {:?}/condvar {:08x}: {:?}", sender_pid, condvar, notify_hash.get(&sender_pid).unwrap().get(&condvar)); } + + // log::info!( + // "removed {} entries for PID {:?}/condvar {:08x}, {} to start, {} remain", + // expired_values, + // sender_pid, + // condvar, + // len_before, + // awaiting.len(), + // ); + // log::trace!("new entries for PID {:?}/condvar {:08x}: {:?}", sender_pid, condvar, notify_hash.get(&sender_pid).unwrap().get(&condvar)); } recalculate_sleep(&mut ticktimer, &mut sleep_heap, None); } From e7983aef87dcf2ab59744aef6732f3efd266cadd Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:04:06 +0800 Subject: [PATCH 58/80] ticktimer: improve condvar handling Improve condvar so that it is more reliable. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 234 +++++++++++++++------------- 1 file changed, 129 insertions(+), 105 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index 4cec85ac0..29ccd4d95 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -787,124 +787,148 @@ fn main() -> ! { } api::Opcode::WaitForCondition => { let pid = msg.sender.pid(); - if let Some(scalar) = msg.body.scalar_message_mut() { - let condvar = scalar.arg1; - let timeout = scalar.arg2; - - log::trace!( - "sender in pid {:?} is waiting on a condition {:08x} with a timeout of {}", - pid, - condvar, - timeout - ); - - // If there's a condition waiting already, decrement the total list - // and return immediately. - if let Some(excess) = immedaite_notifications - .entry(pid) - .or_default() - .get_mut(&condvar) - { - if *excess > 0 { - *excess -= 1; - scalar.id = 0; - - // Rust libstd expects a `Scalar1` return type for this call - return_type = 1; - continue; - } - } - - // If there's a `timeout` argument, schedule a response. - if timeout != 0 { - recalculate_sleep( - &mut ticktimer, - &mut sleep_heap, - Some(TimerRequest { - msec: timeout as i64, - sender: msg.sender, - kind: RequestKind::Timeout, - data: condvar, - }), - ) - } - - // Add this to the list of entries waiting for a response. - notify_hash - .entry(pid) - .or_default() - .entry(condvar) - .or_default() - .push_back(msg.sender); - - // log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar)); - - // The message will be responded to as part of the notification hash - // when the condvar is unlocked. Forget the contents of the message - // in order to prevent it from being responded to early. - core::mem::forget(msg_opt.take()); - } else { + let Some(scalar) = msg.body.scalar_message_mut() else { info!( "sender made WaitForCondition request that wasn't a BlockingScalar Message" ); + continue; + }; + + let condvar = scalar.arg1; + let timeout = scalar.arg2; + + log::trace!( + "sender in pid {:?} is waiting on a condition {:08x} with a timeout of {}", + pid, + condvar, + timeout + ); + + // If there's a condition waiting already, decrement the total list + // and return immediately. + if let Some(excess) = immedaite_notifications + .entry(pid) + .or_default() + .get_mut(&condvar) + { + if *excess > 0 { + *excess -= 1; + scalar.id = 0; + + // Rust libstd expects a `Scalar1` return type for this call + return_type = 1; + continue; + } } + + // Add this to the list of entries waiting for a response. + notify_hash + .entry(pid) + .or_default() + .entry(condvar) + .or_default() + .push_back(msg.sender); + + // If there's a `timeout` argument, schedule a response. + if timeout != 0 { + recalculate_sleep( + &mut ticktimer, + &mut sleep_heap, + Some(TimerRequest { + msec: timeout as i64, + sender: msg.sender, + kind: RequestKind::Timeout, + data: condvar, + }), + ) + } + + // log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar)); + + // The message will be responded to as part of the notification hash + // when the condvar is unlocked. Forget the contents of the message + // in order to prevent it from being responded to early. + core::mem::forget(msg_opt.take()); } + api::Opcode::NotifyCondition => { let pid = msg.sender.pid(); - if msg.body.is_blocking() { - info!("sender made NotifyCondition request that was blocking"); - continue; - } - if let Some(scalar) = msg.body.scalar_message() { - let condvar = scalar.arg1; - - log::trace!( - "sender in pid {:?} is notifying {} entries for condition {:08x}", - pid, - scalar.arg2, - condvar, + let Some(scalar) = msg.body.scalar_message() else { + info!( + "sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message" ); + continue; + }; - let awaiting = notify_hash + if msg.body.is_blocking() { + return_type = 0; + } + + let condvar = scalar.arg1; + + log::trace!( + "sender in pid {:?} is notifying {} entries for condition {:08x}", + pid, + scalar.arg2, + condvar, + ); + + let awaiting = notify_hash + .entry(pid) + .or_default() + .entry(condvar) + .or_default(); + + // Wake threads, ensuring we don't run off the end. + let requested_count = scalar.arg2; + let available_count = core::cmp::min(requested_count, awaiting.len()); + + if requested_count > awaiting.len() { + log::trace!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len()); + } + if requested_count == 0 { + log::trace!("requested to wake no entries! weird, huh?"); + } + stop_sleep(&mut ticktimer, &mut sleep_heap); + for entry in awaiting.drain(..available_count) { + // Remove each entry in the timeout set + sleep_heap.retain(|_, v| if v.sender == entry { false } else { true }); + + // If there's an error waking up the sender, deal with it + match xous::return_scalar(entry, 0) { + Ok(_) => {} + Err(xous::Error::ProcessNotFound) => { + log::error!( + "process {} exited -- removing remaining entries for PID {:?}/condvar {:08x}", + pid.map(|v| v.get()).unwrap_or_default(), pid, condvar + ); + mutex_hash.remove(&pid); + } + Err(xous::Error::DoubleFree) => { + panic!("tried to wake up a thread twice"); + } + Err(e) => panic!("unexpected error responding to scalar: {:?}", e), + } + } + + // If there are leftover requested, add them to the list of + // notofications that will be responded to immediately. + if available_count - requested_count > 0 { + #[cfg(feature = "debug-print")] + log::trace!( + "Adding {} spare sleep requests to immediate_notifications list", + available_count - requested_count + ); + *immedaite_notifications .entry(pid) .or_default() .entry(condvar) - .or_default(); - - // Wake threads, ensuring we don't run off the end. - let requested_count = scalar.arg2; - let available_count = core::cmp::min(requested_count, awaiting.len()); - - stop_sleep(&mut ticktimer, &mut sleep_heap); - for entry in awaiting.drain(..available_count) { - // Remove each entry in the timeout set - sleep_heap.retain(|_, v| if v.sender == entry { false } else { true }); - xous::return_scalar(entry, 0).expect("couldn't send response"); - } - - // If there are leftover requested, add them to the list of - // notofications that will be responded to immediately. - if available_count - requested_count > 0 { - #[cfg(feature = "debug-print")] - log::trace!( - "Adding {} spare sleep requests to immediate_notifications list", - available_count - requested_count - ); - *immedaite_notifications - .entry(pid) - .or_default() - .entry(condvar) - .or_default() += available_count - requested_count; - } - - // Resume sleeping, which re-enables interrupts and queues the - // next timer event to fire. - start_sleep(&mut ticktimer, &mut sleep_heap); - } else { - info!( - "sender made WaitForCondition request that wasn't a BlockingScalar Message" - ); + .or_default() += available_count - requested_count; } + + // Resume sleeping, which re-enables interrupts and queues the + // next timer event to fire. + start_sleep(&mut ticktimer, &mut sleep_heap); } api::Opcode::InvalidCall => { error!("couldn't convert opcode"); From f2c20dfe75df4175695d41cf0e70b8aacf7fca59 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:31:44 +0800 Subject: [PATCH 59/80] xous-api-ticktimer: add calls for freeing objects Add calls to free Mutexes and Condvars. These will go in a destructor, and will remove any trace of these mutexes within the ticktimer server. Signed-off-by: Sean Cross --- api/xous-api-ticktimer/src/api.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api/xous-api-ticktimer/src/api.rs b/api/xous-api-ticktimer/src/api.rs index 034f30501..4f36d8fe0 100644 --- a/api/xous-api-ticktimer/src/api.rs +++ b/api/xous-api-ticktimer/src/api.rs @@ -50,6 +50,26 @@ pub enum Opcode { /// *arg2*: The number of conditions to notify NotifyCondition = 9, + /// Free a Mutex + /// + /// This call will free a Mutex that was previously Locked or Unlocked. + /// Doing so causes the Mutex to be unlocked. + /// + /// # Arguments + /// + /// *arg1*: The integer that matches the Mutex value + FreeMutex = 10, + + /// Free a Condition + /// + /// This call will free a Condition that was previously waited upon. All + /// pending threads will be forgotten. + /// + /// # Arguments + /// + /// *arg1*: The integer that matches the Condition value + FreeCondition = 11, + /// Invalid call -- an error occurred decoding the opcode InvalidCall = u32::MAX as usize, } From 5285346b4ec5facbfb39b73b169539be3a5fcf66 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:32:29 +0800 Subject: [PATCH 60/80] ticktimer: implement freemutex/freecondition With these two API Calls, the ticktimer server will no longer grow without bounds. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index 29ccd4d95..a71008e86 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -763,6 +763,7 @@ fn main() -> ! { info!("sender made LockMutex request that was not blocking"); } } + api::Opcode::UnlockMutex => { let pid = msg.sender.pid(); if msg.body.is_blocking() { @@ -785,6 +786,20 @@ fn main() -> ! { } } } + + api::Opcode::FreeMutex => { + let Some(scalar) = msg.body.scalar_message() else { + info!("sender tried to free a mutex using a non-scalar message"); + continue; + }; + + // Remove all instances of this mutex from the mutex hash + mutex_hash + .entry(msg.sender.pid()) + .or_default() + .remove(&scalar.arg1); + } + api::Opcode::WaitForCondition => { let pid = msg.sender.pid(); let Some(scalar) = msg.body.scalar_message_mut() else { @@ -930,6 +945,42 @@ fn main() -> ! { // next timer event to fire. start_sleep(&mut ticktimer, &mut sleep_heap); } + + api::Opcode::FreeCondition => { + let pid = msg.sender.pid(); + let Some(scalar) = msg.body.scalar_message() else { + info!( + "sender made FreeCondition request that wasn't a Scalar or BlockingScalar Message" + ); + continue; + }; + + if msg.body.is_blocking() { + return_type = 0; + } + + let condvar = scalar.arg1; + + let awaiting = notify_hash + .entry(pid) + .or_default() + .entry(condvar) + .or_default(); + + // Free all entries in the sleep heap that are waiting for this condition + stop_sleep(&mut ticktimer, &mut sleep_heap); + for entry in awaiting.drain(..) { + // Remove each entry in the timeout set + sleep_heap.retain(|_, v| v.sender != entry); + } + + // Remove all instances of this condvar + notify_hash.entry(pid).or_default().remove(&condvar); + + // Resume sleeping + start_sleep(&mut ticktimer, &mut sleep_heap); + } + api::Opcode::InvalidCall => { error!("couldn't convert opcode"); } From 9ab3180fc782ede608b66ae37cf916531be14f48 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:34:22 +0800 Subject: [PATCH 61/80] kernel: return DoubleFree on invalid Return calls When attempting to return an invalid item, return `DoubleFree`. This is because such cases are most likely due to instances where a message was responded to twice. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 48 +++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index cfc7e5a66..c8c3b11d5 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -330,11 +330,11 @@ fn return_memory( } WaitingMessage::ScalarMessage(_pid, _tid) => { println!("WARNING: Tried to wait on a message that was a scalar"); - return Err(xous_kernel::Error::InternalError); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::None => { - println!("WARNING: Tried to wait on a message that didn't exist"); - return Err(xous_kernel::Error::ProcessNotFound); + println!("WARNING: Tried to wait on a message that didn't exist -- return memory"); + return Err(xous_kernel::Error::DoubleFree); } }; // println!( @@ -415,23 +415,23 @@ fn return_scalar( println!( "WARNING: Tried to wait on a scalar message that was actually forgettingmemory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::BorrowedMemory(_, _, _, _, _) => { println!( "WARNING: Tried to wait on a scalar message that was actually borrowed memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::MovedMemory => { println!( "WARNING: Tried to wait on a scalar message that was actually moved memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::None => { - println!("WARNING: Tried to wait on a message that didn't exist"); - return Err(xous_kernel::Error::ProcessNotFound); + println!("WARNING ({}:{}): Tried to wait on a message that didn't exist (irq? {}) -- return Scalar1({})", server_pid.get(), server_tid, if in_irq { "yes"} else {"no"}, arg); + return Err(xous_kernel::Error::DoubleFree); } }; @@ -486,23 +486,25 @@ fn return_scalar2( WaitingMessage::ScalarMessage(pid, tid) => (pid, tid), WaitingMessage::ForgetMemory(_) => { println!("WARNING: Tried to wait on a scalar message that was actually forgetting memory"); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::BorrowedMemory(_, _, _, _, _) => { println!( "WARNING: Tried to wait on a scalar message that was actually borrowed memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::MovedMemory => { println!( "WARNING: Tried to wait on a scalar message that was actually moved memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::None => { - println!("WARNING: Tried to wait on a message that didn't exist"); - return Err(xous_kernel::Error::ProcessNotFound); + println!( + "WARNING: Tried to wait on a message that didn't exist -- return scalar 2" + ); + return Err(xous_kernel::Error::DoubleFree); } }; @@ -564,23 +566,25 @@ fn return_scalar5( WaitingMessage::ScalarMessage(pid, tid) => (pid, tid), WaitingMessage::ForgetMemory(_) => { println!("WARNING: Tried to wait on a scalar message that was actually forgetting memory"); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::BorrowedMemory(_, _, _, _, _) => { println!( "WARNING: Tried to wait on a scalar message that was actually borrowed memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::MovedMemory => { println!( "WARNING: Tried to wait on a scalar message that was actually moved memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::None => { - println!("WARNING: Tried to wait on a message that didn't exist"); - return Err(xous_kernel::Error::ProcessNotFound); + println!( + "WARNING: Tried to wait on a message that didn't exist -- return scalar 5" + ); + return Err(xous_kernel::Error::DoubleFree); } }; @@ -675,7 +679,7 @@ fn reply_and_receive_next( println!( "WARNING: Tried to wait on a scalar message that was actually forgetting memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::BorrowedMemory(pid, tid, _server_addr, client_addr, len) => { #[cfg(baremetal)] @@ -699,11 +703,11 @@ fn reply_and_receive_next( println!( "WARNING: Tried to wait on a scalar message that was actually moved memory" ); - return Err(xous_kernel::Error::ProcessNotFound); + return Err(xous_kernel::Error::DoubleFree); } WaitingMessage::None => { - println!("WARNING: Tried to wait on a message that didn't exist"); - return Err(xous_kernel::Error::ProcessNotFound); + println!("WARNING: Tried to wait on a message that didn't exist -- receive and return scalar"); + return Err(xous_kernel::Error::DoubleFree); } }; From 26c97dcc92ddf3d57ea1fbd39832c4024141e1ca Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:35:34 +0800 Subject: [PATCH 62/80] kernel: immediately switch to new thread on CreateThread When a new thread is created, immediately switch to the new thread. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index c8c3b11d5..39263db38 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1048,11 +1048,16 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR }), SysCall::CreateThread(thread_init) => SystemServices::with_mut(|ss| { ss.create_thread(pid, thread_init).map(|new_tid| { - if !cfg!(baremetal) { - ss.switch_to_thread(pid, Some(new_tid)) - .expect("couldn't activate new thread"); - } - xous_kernel::Result::ThreadID(new_tid) + // Immediately switch to the new thread + ss.switch_to_thread(pid, Some(new_tid)) + .expect("couldn't activate new thread"); + + // Set the return value of the existing thread to be the new thread ID + ss.set_thread_result(pid, tid, xous_kernel::Result::ThreadID(new_tid)) + .expect("couldn't set new thread ID"); + + // Return `ResumeProcess` since we're switching threads + xous_kernel::Result::ResumeProcess }) }), SysCall::CreateProcess(process_init) => SystemServices::with_mut(|ss| { From 217b66e9769c2b9275c1fc4d4698fa8d02bdbb93 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:36:38 +0800 Subject: [PATCH 63/80] kernel: don't yield when JoinThread returns immediately When waiting on a thread, it may be the case that the thread we're joining has exited already. In this case, the correct approach is to resume execution rather than switching to the parent process. This fixes an issue where `SWITCHTO_CALLER` is None when joining a short-lived thread. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 39263db38..1d27abba7 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1145,7 +1145,11 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR }), SysCall::JoinThread(other_tid) => { SystemServices::with_mut(|ss| ss.join_thread(pid, tid, other_tid)).map(|ret| { - unsafe { SWITCHTO_CALLER = None }; + // Successfully joining a thread causes this thread to sleep while the parent process + // is resumed. This is the same as a `Yield` + if ret == xous_kernel::Result::ResumeProcess { + unsafe { SWITCHTO_CALLER = None }; + } ret }) } From e37f3fd191f9acdd04c409801b723c702eaff3ab Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:38:45 +0800 Subject: [PATCH 64/80] kernel: limit memory regions to 4 MB This memory region is much smaller than the previous value of 256 MB, but it will limit the amount of memory used by superpages in long-running processes. Previously, we would march through memory as messages were passed back and forth and a process' memory space would quickly be eaten up by 1024 pages. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 533d628ab..f39dedfdc 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -330,12 +330,12 @@ impl MemoryManager { } xous_kernel::MemoryType::Default => ( process_inner.mem_default_base, - process_inner.mem_default_base + 0x1000_0000, + process_inner.mem_default_base + 0x40_0000, // Limit to one superpage process_inner.mem_default_last, ), xous_kernel::MemoryType::Messages => ( process_inner.mem_message_base, - process_inner.mem_message_base + 0x1000_0000, + process_inner.mem_message_base + 0x40_0000, // Limit to one superpage process_inner.mem_message_last, ), }; From dd4546c2c08cef4577aa76e273691750ce56c853 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 18:40:58 +0800 Subject: [PATCH 65/80] kernel: properly set current_thread on init Previously the `current_thread` was set to 0, which is invalid. Set the `current_thread` so that it defaults to the initial thread ID. Signed-off-by: Sean Cross --- kernel/src/services.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index e9f5fd0cb..47dcbe59b 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -272,7 +272,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices { ppid: unsafe { PID::new_unchecked(1) }, pid: unsafe { PID::new_unchecked(1) }, mapping: arch::mem::DEFAULT_MEMORY_MAPPING, - current_thread: 0_usize, + current_thread: INITIAL_TID, previous_thread: INITIAL_TID as TID, exception_handler: None, }; MAX_PROCESS_COUNT], From 27b777192d3ea958739c46ff45ce30a71c3a3e20 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 19:50:26 +0800 Subject: [PATCH 66/80] kernel: use common function for finding next thread THe act of finding a runnable thread is so common, yet the kernel contains three ways of doing this. Use a unified function to select the next available thread. Signed-off-by: Sean Cross --- kernel/src/services.rs | 115 ++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 59 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 47dcbe59b..5e0e565a1 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -694,6 +694,35 @@ impl SystemServices { Ok(()) } + fn find_next_thread(thread_mask: usize, current_thread: usize) -> usize { + // If there's only one thread runnable, run that one + if thread_mask == 0 { + panic!("no threads were available to run"); + } + + if thread_mask.count_ones() == 1 { + let new_tid = thread_mask.ilog2() as usize; + // println!( + // "thread mask: {:b} -- only one thread was ready -- picking thread {} ({:b})", + // thread_mask, + // new_tid, + // 1 << new_tid + // ); + new_tid + } else { + let mut new_tmask = (1usize << current_thread).rotate_left(1); + // println!("looking for a new thread -- existing thread was {} ({:b}) and new mask is {:b} (starting with {:b})", current_thread, 1< { - let new_thread = match tid { - None => { - let mut new_context = 0; - - while x & (1 << new_context) == 0 { - new_context += 1; - if new_context > arch::process::MAX_THREAD { - new_context = 0; - } - } - new_context - } - Some(ctx) => { - // Ensure the specified context is ready to run - if x & (1 << ctx) == 0 { - return Err(xous_kernel::Error::InvalidThread); - } - ctx - } - }; + let new_thread = + tid.unwrap_or_else(|| Self::find_next_thread(x, process.current_thread)); + if x & (1 << new_thread) == 0 { + panic!("invalid thread ID"); + } process.activate()?; let mut p = ArchProcess::current(); @@ -809,9 +823,7 @@ impl SystemServices { Some(tid) => { // Ensure the specified context is ready to run, or is // currently running. - if ready_threads & (1 << tid) == 0 - /*&& ctx != current_thread*/ - { + if ready_threads & (1 << tid) == 0 { return Err(xous_kernel::Error::InvalidThread); } tid @@ -973,32 +985,20 @@ impl SystemServices { // search for the next available context. assert!( x != 0, - "process was {:?} but had no free contexts", + "process was {:?} but had no runnable threads", new.state ); if new_tid == 0 { - new_tid = (new.current_thread + 1) as usize; - while x & (1 << new_tid) == 0 { - new_tid += 1; - if new_tid > arch::process::MAX_THREAD { - new_tid = 0; - } else if new_tid == (new.current_thread + 1) as usize { - // If we've looped around, return an error. - // println!("Looked through all contexts and couldn't find one that was ready"); - return Err(xous_kernel::Error::ProcessNotFound); - } - } - new.current_thread = new_tid as _; - klog!("picked thread ID {}", new_tid); - } else if x & (1 << new_tid) == 0 { - // println!( - // "thread is {:?}, which is not valid for new thread {}", - // new.state, new_tid - // ); - return Err(xous_kernel::Error::ProcessNotFound); - } else { - new.current_thread = new_tid as _; + new_tid = Self::find_next_thread(x, new.current_thread); } + if x & (1 << new_tid) == 0 { + println!( + "process state is {:?}, but new thread {} is not runnable", + new.state, new_tid + ); + return Err(xous_kernel::Error::ProcessNotFound); + } + new.current_thread = new_tid as _; } ProcessState::Sleeping | ProcessState::Debug(_) @@ -1121,21 +1121,18 @@ impl SystemServices { // If no new thread is specified, take the previous // thread. If that is not runnable, do a round-robin // search for the next available thread. - if new_tid == 0 { - new_tid = (new.current_thread + 1) as usize; - while x & (1 << new_tid) == 0 { - new_tid += 1; - if new_tid > arch::process::MAX_THREAD { - new_tid = 0; - } else if new_tid == (new.current_thread + 1) as usize { - // If we've looped around, return an error. - return Err(xous_kernel::Error::ProcessNotFound); - } - } - new.current_thread = new_tid as _; - } else if x & (1 << new_tid) == 0 { - return Err(xous_kernel::Error::ProcessNotFound); + let new_tid = if new_tid == 0 { + Self::find_next_thread(x, new.current_thread) + } else { + new_tid + }; + + if x & (1 << new_tid) == 0 { + return Err(xous_kernel::Error::ThreadNotAvailable); } + + new.current_thread = new_tid as _; + // Mark the previous TID as being runnable, and remove the new TID // from the list of threads that can be run. ProcessState::Running( From 94c30c0388025cd9a10096b2fa248f7bd9c21a9a Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 29 Jan 2023 21:27:14 +0800 Subject: [PATCH 67/80] kernel: minor formatting cleanups These files have had rustfmt run on them. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/irq.rs | 2 +- kernel/src/services.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index 23bbccfa7..a0b8d8ca7 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -45,7 +45,7 @@ pub unsafe fn set_isr_return_pair(pid: PID, tid: TID) { PREVIOUS_PAIR = Some((pid, tid)); } -#[cfg(feature="gdb-stub")] +#[cfg(feature = "gdb-stub")] pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> { PREVIOUS_PAIR.take() } diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 5e0e565a1..659998167 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -433,7 +433,11 @@ impl SystemServices { entry.state = ProcessState::Ready(1 << INITIAL_TID); } // entry.ppid = _ppid; - klog!("created new process for PID {} with PPID {}", new_pid, _ppid); + klog!( + "created new process for PID {} with PPID {}", + new_pid, + _ppid + ); return Ok(startup); } @@ -1570,7 +1574,6 @@ impl SystemServices { other ), }; - // log_process_update(file!(), line!(), process, old_state); Ok(new_tid) } @@ -2120,7 +2123,7 @@ impl SystemServices { Ok(parent_pid) } - #[cfg(feature="gdb-stub")] + #[cfg(feature = "gdb-stub")] pub fn suspend_process(&mut self, pid: PID) -> Result<(), xous_kernel::Error> { let (process_state, parent_pid) = { let process = self.get_process_mut(pid)?; @@ -2174,7 +2177,7 @@ impl SystemServices { Ok(()) } - #[cfg(feature="gdb-stub")] + #[cfg(feature = "gdb-stub")] pub fn continue_process(&mut self, pid: PID) -> Result<(), xous_kernel::Error> { let process = self.get_process_mut(pid)?; // let old_state = process.state; From a46fc0d217caab7f4707fe234f4d4af97cea77e2 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 11:23:16 +0800 Subject: [PATCH 68/80] Revert "renode: remove custom LiteX_Timer_32" The custom litex timer is more custom than we thought. This reverts commit 46fab8cbc8a224874817ac7e07d0b3625f0ab656. --- emulation/betrusted.resc | 1 + emulation/ec/betrusted-ec.repl | 2 +- emulation/peripherals/LiteX_Timer_32.cs | 156 ++++++++++++++++++++++++ emulation/soc/betrusted-soc.repl | 2 +- svd2repl/src/generate.rs | 2 +- 5 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 emulation/peripherals/LiteX_Timer_32.cs diff --git a/emulation/betrusted.resc b/emulation/betrusted.resc index d73191c15..4d8d7afcf 100644 --- a/emulation/betrusted.resc +++ b/emulation/betrusted.resc @@ -28,6 +28,7 @@ i @peripherals/engine.cs i @peripherals/Jtag.cs i @peripherals/keyboard.cs i @peripherals/keyrom.cs +i @peripherals/LiteX_Timer_32.cs i @peripherals/LM3509.cs i @peripherals/LSM6DS3.cs i @peripherals/memlcd.cs diff --git a/emulation/ec/betrusted-ec.repl b/emulation/ec/betrusted-ec.repl index 409585045..e1918ef07 100644 --- a/emulation/ec/betrusted-ec.repl +++ b/emulation/ec/betrusted-ec.repl @@ -7,7 +7,7 @@ uart: UART.LiteX_UART @ { } -> cpu@1001 -timer0: Timers.LiteX_Timer_CSR32 @ { +timer0: Timers.Betrusted.LiteX_Timer_32 @ { sysbus 0xe0001000 } frequency: 100000000 diff --git a/emulation/peripherals/LiteX_Timer_32.cs b/emulation/peripherals/LiteX_Timer_32.cs new file mode 100644 index 000000000..59fcb9b73 --- /dev/null +++ b/emulation/peripherals/LiteX_Timer_32.cs @@ -0,0 +1,156 @@ +// +// Copyright (c) 2010-2019 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// +using Antmicro.Renode.Core; +using Antmicro.Renode.Core.Structure.Registers; +using Antmicro.Renode.Logging; +using Antmicro.Renode.Peripherals.Bus; + +namespace Antmicro.Renode.Peripherals.Timers.Betrusted +{ + // this is a model of LiteX timer in the Betrusted configuration: + // * width: 32 bits + // * csr data width: 32 bit + [AllowedTranslations(AllowedTranslation.ByteToDoubleWord)] + public class LiteX_Timer_32 : BasicDoubleWordPeripheral, IKnownSize + { + public LiteX_Timer_32(Machine machine, long frequency) : base(machine) + { + IRQ = new GPIO(); + innerTimer = new LimitTimer(machine.ClockSource, frequency, this, "LiteXTimer32", eventEnabled: true, autoUpdate: true); + innerTimer.LimitReached += delegate + { + this.Log(LogLevel.Noisy, "Limit reached"); + irqPending.Value = true; + UpdateInterrupts(); + + if(reloadValue == 0) + { + this.Log(LogLevel.Noisy, "No realod value - disabling the timer"); + innerTimer.Enabled = false; + } + innerTimer.Limit = reloadValue; + }; + DefineRegisters(); + } + + public override void Reset() + { + base.Reset(); + innerTimer.Reset(); + latchedValue = 0; + loadValue = 0; + reloadValue = 0; + RegistersCollection.Reset(); + + UpdateInterrupts(); + } + + public GPIO IRQ { + get; + private set; + } + + public long Size { get { return 0x20; }} + + private void DefineRegisters() + { + Registers.Load.Define32(this) + .WithValueField(0, 32, name: "LOAD", writeCallback: (_, val) => + { + loadValue = val; + }); + ; + + Registers.Reload.Define32(this) + .WithValueField(0, 32, name: "RELOAD", writeCallback: (_, val) => + { + reloadValue = val; + }); + ; + + Registers.TimerEnable.Define32(this) + .WithFlag(0, name: "ENABLE", writeCallback: (_, val) => + { + if(innerTimer.Enabled == val) + { + return; + } + + if(val) + { + innerTimer.Limit = loadValue; + this.Log(LogLevel.Noisy, "Enabling timer. Load value: 0x{0:X}, reload value: 0x{1:X}", loadValue, reloadValue); + } + + innerTimer.Enabled = val; + }) + ; + + Registers.TimerUpdateValue.Define32(this) + .WithFlag(0, FieldMode.WriteOneToClear, name: "UPDATE_VALUE", writeCallback: (_, val) => + { + if(val) + { + latchedValue = (uint)innerTimer.Value; + } + }); + ; + + // VALUE0 contains most significant 8 bits + // VALUE3 contains least significant 8 bits + Registers.Value.Define32(this) + .WithValueField(0, 32, FieldMode.Read, name: "VALUE", valueProviderCallback: _ => + { + return latchedValue; + }); + ; + + Registers.EventStatus.Define32(this) + .WithFlag(0, FieldMode.Read, name: "EV_STATUS", valueProviderCallback: _ => innerTimer.Value == 0) + ; + + Registers.EventPending.Define32(this) + .WithFlag(0, out irqPending, FieldMode.Read | FieldMode.WriteOneToClear, name: "EV_PENDING", changeCallback: (_, __) => UpdateInterrupts()) + ; + + Registers.EventEnable.Define32(this) + .WithFlag(0, out irqEnabled, name: "EV_ENABLE", changeCallback: (_, __) => UpdateInterrupts()) + ; + } + + private void UpdateInterrupts() + { + this.Log(LogLevel.Noisy, "Setting IRQ: {0}", irqPending.Value && irqEnabled.Value); + IRQ.Set(irqPending.Value && irqEnabled.Value); + } + + private IFlagRegisterField irqEnabled; + private IFlagRegisterField irqPending; + + private uint latchedValue; + private uint loadValue; + private uint reloadValue; + + private readonly LimitTimer innerTimer; + + private enum Registers + { + Load = 0x00, + + Reload = 0x04, + + TimerEnable = 0x08, + TimerUpdateValue = 0x0C, + + Value = 0x10, + + EventStatus = 0x14, + EventPending = 0x18, + EventEnable = 0x1C + } + } +} diff --git a/emulation/soc/betrusted-soc.repl b/emulation/soc/betrusted-soc.repl index d06bd9d69..c6569e33b 100644 --- a/emulation/soc/betrusted-soc.repl +++ b/emulation/soc/betrusted-soc.repl @@ -7,7 +7,7 @@ cpu: CPU.Betrusted.AesVexRiscv @ sysbus // Platform Peripherals // Unrecognized peripheral: reboot @ 0xf0000000 -timer0: Timers.LiteX_Timer_CSR32 @ sysbus 0xf0001000 +timer0: Timers.Betrusted.LiteX_Timer_32 @ sysbus 0xf0001000 frequency: 100000000 IRQ -> cpu @ 1000 diff --git a/svd2repl/src/generate.rs b/svd2repl/src/generate.rs index 6b17af3b8..941af64f7 100644 --- a/svd2repl/src/generate.rs +++ b/svd2repl/src/generate.rs @@ -665,7 +665,7 @@ pub fn generate(src: T, dest: &mut U) -> Result<(), ParseErro cs_peripherals.insert("spinor_soft_int", "Miscellaneous.Betrusted.SpinorSoftInt"); cs_peripherals.insert("spinor", "SPI.Betrusted.BetrustedSpinor"); cs_peripherals.insert("susres", "Timers.Betrusted.SusRes"); - cs_peripherals.insert("timer0", "Timers.LiteX_Timer_CSR32"); + cs_peripherals.insert("timer0", "Timers.Betrusted.LiteX_Timer_32"); cs_peripherals.insert("trng_kernel", "Miscellaneous.Betrusted.BetrustedRNGKernel"); cs_peripherals.insert("trng_server", "Miscellaneous.Betrusted.BetrustedRNGServer"); cs_peripherals.insert("ticktimer", "Timers.Betrusted.TickTimer"); From 49a87dcfe2a722eb2bc9c0bbf6a7fe4c013c5c34 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 20:57:49 +0800 Subject: [PATCH 69/80] kernel: services: optimize find_next_thread() Use a multiply-based algorithm to count the number of leading zeroes. This results in a function that's just 20 instructions long. Signed-off-by: Sean Cross --- kernel/src/services.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 659998167..a708919b9 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -698,32 +698,32 @@ impl SystemServices { Ok(()) } - fn find_next_thread(thread_mask: usize, current_thread: usize) -> usize { + pub fn find_next_thread(thread_mask: usize, current_thread: usize) -> usize { + // From https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup + // This platform has a multiplier, so this is fast + fn trailing_zeros(v: usize) -> usize { + const MULTIPLY_DEBRUIJN_BIT_POSITION: [usize; 32] = [ + 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, + 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, + ]; + + MULTIPLY_DEBRUIJN_BIT_POSITION[((!v.wrapping_sub(1) & v) * 0x077CB531) >> 27] + } // If there's only one thread runnable, run that one if thread_mask == 0 { panic!("no threads were available to run"); } - if thread_mask.count_ones() == 1 { - let new_tid = thread_mask.ilog2() as usize; - // println!( - // "thread mask: {:b} -- only one thread was ready -- picking thread {} ({:b})", - // thread_mask, - // new_tid, - // 1 << new_tid - // ); - new_tid + // if thread_mask.is_power_of_two() { + if thread_mask & (thread_mask - 1) == 0 { + trailing_zeros(thread_mask) } else { - let mut new_tmask = (1usize << current_thread).rotate_left(1); - // println!("looking for a new thread -- existing thread was {} ({:b}) and new mask is {:b} (starting with {:b})", current_thread, 1< Date: Mon, 30 Jan 2023 21:48:25 +0800 Subject: [PATCH 70/80] xous-ticktimer: overhaul condvars The previous condvar implementation was unsound, and didn't do the right thing. Overhaul condvars so that they now pass the libstd tests. As part of this, function calls were added to free the mutex and condvar objects that may have been allocated. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 175 +++++++++++++++++++++------- 1 file changed, 132 insertions(+), 43 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index a71008e86..8b33f0592 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -72,6 +72,7 @@ mod implementation { pub struct XousTickTimer { csr: utralib::CSR, current_response: Option, + last_response: Option, connection: xous::CID, ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>, wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>, @@ -105,6 +106,9 @@ mod implementation { }), ) .ok(); + + // Save the response so we can be sure we don't double-return messages. + xtt.last_response = Some(response); } impl XousTickTimer { @@ -131,6 +135,7 @@ mod implementation { let mut xtt = XousTickTimer { csr: CSR::new(csr.as_mut_ptr() as *mut u32), current_response: None, + last_response: None, connection, ticktimer_sr_manager, wdt_sr_manager, @@ -164,6 +169,14 @@ mod implementation { xtt } + pub fn last_response(&self) -> &Option { + &self.last_response + } + + pub fn clear_last_response(&mut self) { + self.last_response = None; + } + pub fn reset(&mut self) { self.csr.wfo(utra::ticktimer::CONTROL_RESET, 0b1); self.csr.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run @@ -393,6 +406,12 @@ mod implementation { } } + pub fn last_response(&self) -> &Option { + &None + } + + pub fn clear_last_response(&mut self) {} + pub fn reset(&mut self) { self.start = std::time::Instant::now(); } @@ -494,6 +513,8 @@ fn recalculate_sleep( ) { stop_sleep(ticktimer, sleep_heap); + ticktimer.clear_last_response(); + // If we have a new sleep request, add it to the heap. if let Some(mut request) = new { #[cfg(feature = "debug-print")] @@ -621,6 +642,7 @@ fn main() -> ! { return_type = 2; } } + api::Opcode::SleepMs => { if let Some(scalar) = msg.body.scalar_message_mut() { let ms = scalar.arg1 as i64; @@ -654,6 +676,7 @@ fn main() -> ! { ); } } + api::Opcode::RecalculateSleep => { if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32 != xous::process::id() @@ -674,7 +697,7 @@ fn main() -> ! { // If we're being asked to recalculate due to a timeout expiring, drop the sent // message from the `entries` list. - if (request_kind == RequestKind::Timeout as usize) && (sender > 0) { + if (request_kind == RequestKind::Timeout as usize) && (sender != 0) { let mut expired_values = 0; let awaiting = notify_hash .entry(sender_pid) @@ -710,6 +733,7 @@ fn main() -> ! { } recalculate_sleep(&mut ticktimer, &mut sleep_heap, None); } + api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, { ticktimer.suspend(); susres @@ -717,9 +741,11 @@ fn main() -> ! { .expect("couldn't execute suspend/resume"); ticktimer.resume(); }), + api::Opcode::PingWdt => { ticktimer.reset_wdt(); } + api::Opcode::GetVersion => { let mut buf = unsafe { xous_ipc::Buffer::from_memory_message_mut( @@ -736,63 +762,76 @@ fn main() -> ! { buf.replace(v).unwrap(); } } + api::Opcode::LockMutex => { + let Some(scalar) = msg.body.scalar_message_mut() else { + log::error!("sender made LockMutex request that was not blocking"); + continue; + }; + let pid = msg.sender.pid(); - if let Some(scalar) = msg.body.scalar_message_mut() { - let ready = mutex_ready_hash.entry(pid).or_default(); - // If this item is in the Ready list, return right away without blocking - if ready.remove(&scalar.arg1) { - scalar.id = 0; - continue; - } + let ready = mutex_ready_hash.entry(pid).or_default(); - // This item is not in the Ready list, so add our sender to the list of processes - // to get called when UnlockMutex is invoked - let awaiting = mutex_hash.entry(pid).or_default(); - - // Add this to the end of the list of entries to call so that when `UnlockMutex` is sent - // the message will get a response. - let mutex_entry = awaiting.entry(scalar.arg1).or_default(); - mutex_entry.push_back(msg.sender); - - // We've saved the `msg.sender` above and will return the lock as part of the mutex - // work thread. Forget the contents of `msg_opt` without running its destructor. - core::mem::forget(msg_opt.take()); - } else { - info!("sender made LockMutex request that was not blocking"); + // If this item is in the Ready list, return right away without blocking + if ready.remove(&scalar.arg1) { + scalar.id = 0; + continue; } + + // This item is not in the Ready list, so add our sender to the list of processes + // to get called when UnlockMutex is invoked + let awaiting = mutex_hash.entry(pid).or_default(); + + // Add this to the end of the list of entries to call so that when `UnlockMutex` is sent + // the message will get a response. + let mutex_entry = awaiting.entry(scalar.arg1).or_default(); + mutex_entry.push_back(msg.sender); + + // We've saved the `msg.sender` above and will return the lock as part of the mutex + // work thread. Forget the contents of `msg_opt` without running its destructor. + core::mem::forget(msg_opt.take()); } api::Opcode::UnlockMutex => { - let pid = msg.sender.pid(); if msg.body.is_blocking() { - info!("sender made UnlockMutex request that was blocking"); + log::error!("sender made UnlockMutex request that was blocking"); continue; } - if let Some(scalar) = msg.body.scalar_message() { - // Get a list of awaiting mutexes for this process - let awaiting = mutex_hash.entry(pid).or_default(); + let Some(scalar) = msg.body.scalar_message() else { + log::error!("made a call to UnlockMutex with a non-scalar message"); + continue; + }; - // Get the vector of awaiting mutex entries. - let mutex_entry = awaiting.entry(scalar.arg1).or_default(); + let pid = msg.sender.pid(); - // If there's something waiting in the queue, respond to that message - if let Some(sender) = mutex_entry.pop_front() { - xous::return_scalar(sender, 0).unwrap(); - } else { - // Otherwise, mark this scalar as being ready to run - mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1); - } + // Get a list of awaiting mutexes for this process + let awaiting = mutex_hash.entry(pid).or_default(); + + // Get the vector of awaiting mutex entries. + let mutex_entry = awaiting.entry(scalar.arg1).or_default(); + + // If there's something waiting in the queue, respond to that message + if let Some(sender) = mutex_entry.pop_front() { + xous::return_scalar(sender, 0).unwrap(); + } else { + // Otherwise, mark this scalar as being ready to run + mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1); } } api::Opcode::FreeMutex => { let Some(scalar) = msg.body.scalar_message() else { - info!("sender tried to free a mutex using a non-scalar message"); + log::error!("sender tried to free a mutex using a non-scalar message"); continue; }; + // log::info!( + // "PID {}: freeing mutex {}", + // msg.sender.pid().map(|v| v.get()).unwrap_or_default(), + // scalar.arg1 + // ); + // Remove all instances of this mutex from the mutex hash mutex_hash .entry(msg.sender.pid()) @@ -803,7 +842,7 @@ fn main() -> ! { api::Opcode::WaitForCondition => { let pid = msg.sender.pid(); let Some(scalar) = msg.body.scalar_message_mut() else { - info!( + log::trace!( "sender made WaitForCondition request that wasn't a BlockingScalar Message" ); continue; @@ -869,7 +908,7 @@ fn main() -> ! { api::Opcode::NotifyCondition => { let pid = msg.sender.pid(); let Some(scalar) = msg.body.scalar_message() else { - info!( + log::error!( "sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message" ); continue; @@ -897,17 +936,34 @@ fn main() -> ! { // Wake threads, ensuring we don't run off the end. let requested_count = scalar.arg2; let available_count = core::cmp::min(requested_count, awaiting.len()); + let remaining_count = awaiting.len() - available_count; - if requested_count > awaiting.len() { - log::trace!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len()); + if remaining_count > 0 { + log::error!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len()); + if let Some(entry) = ticktimer.last_response() { + log::error!("but there is a last_response present"); + if entry.data == condvar { + log::error!("...which matches the condvar that was being unlocked"); + } + } } if requested_count == 0 { - log::trace!("requested to wake no entries! weird, huh?"); + log::error!("requested to wake no entries!"); } + stop_sleep(&mut ticktimer, &mut sleep_heap); for entry in awaiting.drain(..available_count) { // Remove each entry in the timeout set - sleep_heap.retain(|_, v| if v.sender == entry { false } else { true }); + sleep_heap.retain(|_, v| v.sender != entry); + + if let Some(last_response) = ticktimer.last_response() { + if last_response.sender == entry { + log::error!( + "got a request to wake a condvar ({:08x}) that we just responded to with entry {:08x} -- suspect there's a thread waiting in the server queue to clean it up", + condvar, entry.to_usize()); + continue; + } + } // If there's an error waking up the sender, deal with it match xous::return_scalar(entry, 0) { @@ -967,16 +1023,49 @@ fn main() -> ! { .entry(condvar) .or_default(); + if !awaiting.is_empty() { + log::info!( + "PID {}: freeing condvar {:08x} -- {} entries waiting", + pid.map(|v| v.get()).unwrap_or_default(), + condvar, + awaiting.len() + ); + } + // Free all entries in the sleep heap that are waiting for this condition stop_sleep(&mut ticktimer, &mut sleep_heap); for entry in awaiting.drain(..) { // Remove each entry in the timeout set sleep_heap.retain(|_, v| v.sender != entry); + + if let Some(last_response) = ticktimer.last_response() { + if last_response.sender == entry { + log::error!( + "got a request to destroy a condvar ({:08x}) that we just responded to with entry {:08x} -- suspect there's a thread waiting in the server queue to clean it up", + condvar, entry.to_usize()); + continue; + } + } + + // If there's an error waking up the sender, deal with it + xous::return_scalar(entry, 0).ok(); } // Remove all instances of this condvar notify_hash.entry(pid).or_default().remove(&condvar); + let immediate_notifications = immedaite_notifications + .entry(pid) + .or_default() + .remove(&condvar) + .unwrap_or_default(); + if immediate_notifications != 0 { + log::error!( + "there were {} threads that were notified without first waiting", + immediate_notifications + ); + } + // Resume sleeping start_sleep(&mut ticktimer, &mut sleep_heap); } From a406648bccd8f4db5fbcd37cef51eaae0d58ffd9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:49:24 +0800 Subject: [PATCH 71/80] xous: use `asm!()` macro for syscalls Previously we used externally-linked assembly in order to make syscalls. Now that `asm!()` is stable, use this macro. This will help to optimize IPC between programs and the kernel. Additionally, it resolves an unsoundness issue where we were filling in values on the stack and hoping that Rust didn't reorder the structs. Signed-off-by: Sean Cross --- xous-rs/src/arch/riscv/syscall.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/xous-rs/src/arch/riscv/syscall.rs b/xous-rs/src/arch/riscv/syscall.rs index 91227d1a5..49f5ca240 100644 --- a/xous-rs/src/arch/riscv/syscall.rs +++ b/xous-rs/src/arch/riscv/syscall.rs @@ -16,14 +16,21 @@ use crate::definitions::SysCallResult; use crate::syscall::SysCall; pub fn syscall(call: SysCall) -> SysCallResult { - let mut ret = crate::Result::Ok; - let args = call.as_args(); + let mut args = call.as_args(); unsafe { - _xous_syscall( - args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret, + core::arch::asm!( + "ecall", + inlateout("a0") args[0], + inlateout("a1") args[1], + inlateout("a2") args[2], + inlateout("a3") args[3], + inlateout("a4") args[4], + inlateout("a5") args[5], + inlateout("a6") args[6], + inlateout("a7") args[7], ) }; - match ret { + match crate::definitions::Result::from_args(args) { crate::Result::Error(e) => Err(e), other => Ok(other), } From aece50a8fa3f3766612a56216705483cec466e6e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:52:35 +0800 Subject: [PATCH 72/80] kernel: syscall: rename `context` to `tid` The word `context` is an older word. We now use "thread" or "tid". Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 1d27abba7..d95579b35 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -7,7 +7,6 @@ use crate::irq::interrupt_claim; use crate::mem::{MemoryManager, PAGE_SIZE}; use crate::server::{SenderID, WaitingMessage}; use crate::services::SystemServices; -use core::mem; use xous_kernel::*; /* Quoth Xobs: @@ -64,7 +63,7 @@ fn do_yield(_pid: PID, tid: TID) -> SysCallResult { }) } -fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallResult { +fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult { SystemServices::with_mut(|ss| { let sidx = ss .sidx_from_cid(cid) @@ -155,7 +154,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes // server_pid // ); let sender_idx = if message.is_blocking() { - ss.remember_server_message(sidx, pid, thread, &message, client_address) + ss.remember_server_message(sidx, pid, tid, &message, client_address) .map_err(|e| { klog!("error remembering server message: {:?}", e); ss.server_from_sidx_mut(sidx) @@ -202,19 +201,19 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes xous_kernel::Result::MessageEnvelope(envelope), ) .expect("couldn't set result for server thread"); - ss.activate_process_thread(thread, ppid, ptid, false) + ss.activate_process_thread(tid, ppid, ptid, false) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } else { // Switch to the server, since it's in a state to be run. klog!("Activating Server context and switching away from Client"); - ss.activate_process_thread(thread, server_pid, server_tid, false) + ss.activate_process_thread(tid, server_pid, server_tid, false) .map(|_| Ok(xous_kernel::Result::MessageEnvelope(envelope))) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } } else if blocking && !cfg!(baremetal) { klog!("Blocking client, since it sent a blocking message"); - ss.unschedule_thread(pid, thread)?; + ss.unschedule_thread(pid, tid)?; ss.switch_to_thread(server_pid, Some(server_tid))?; ss.set_thread_result( server_pid, @@ -253,7 +252,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes ); // Add this message to the queue. If the queue is full, this // returns an error. - let _queue_idx = ss.queue_server_message(sidx, pid, thread, message, client_address)?; + let _queue_idx = ss.queue_server_message(sidx, pid, tid, message, client_address)?; klog!("queued into index {:x}", _queue_idx); // Park this context if it's blocking. This is roughly @@ -264,11 +263,11 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes let process = ss.get_process(pid).expect("Can't get current process"); let ppid = process.ppid; unsafe { SWITCHTO_CALLER = None }; - ss.activate_process_thread(thread, ppid, 0, false) + ss.activate_process_thread(tid, ppid, 0, false) .map(|_| Ok(xous_kernel::Result::ResumeProcess)) .unwrap_or(Err(xous_kernel::Error::ProcessNotFound)) } else { - ss.unschedule_thread(pid, thread)?; + ss.unschedule_thread(pid, tid)?; Ok(xous_kernel::Result::BlockedProcess) } } else { @@ -1000,7 +999,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR MemoryRange::new(start, length).unwrap() })) } - SysCall::SwitchTo(new_pid, new_context) => SystemServices::with_mut(|ss| { + SysCall::SwitchTo(new_pid, new_tid) => SystemServices::with_mut(|ss| { unsafe { assert!( SWITCHTO_CALLER.is_none(), @@ -1013,7 +1012,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR // "Activating process thread {} in pid {} coming from pid {} thread {}", // new_context, new_pid, pid, tid // ); - ss.activate_process_thread(tid, new_pid, new_context, true) + ss.activate_process_thread(tid, new_pid, new_tid, true) .map(|_ctx| xous_kernel::Result::ResumeProcess) }), SysCall::ClaimInterrupt(no, callback, arg) => { From 25b7f72af47f66c0639996c95183236d1059de5b Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:53:18 +0800 Subject: [PATCH 73/80] kernel: syscall: short-circuit return_scalar irq When in an IRQ, short-circuit the return logic. This should make interrupt returns slightly faster. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index d95579b35..5c9823163 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -434,9 +434,11 @@ fn return_scalar( } }; - let client_is_runnable = ss.runnable(client_pid, Some(client_tid))?; + if cfg!(baremetal) { + ss.ready_thread(client_pid, client_tid)?; + } - if !cfg!(baremetal) || in_irq || !client_is_runnable { + if !cfg!(baremetal) || in_irq || !ss.runnable(client_pid, Some(client_tid))? { // In a hosted environment, `switch_to_thread()` doesn't continue // execution from the new thread. Instead it continues in the old // thread. Therefore, we need to instruct the client to resume, and @@ -444,9 +446,6 @@ fn return_scalar( // In a baremetal environment, the opposite is true -- we instruct // the server to resume and return to the client. ss.set_thread_result(client_pid, client_tid, xous_kernel::Result::Scalar1(arg))?; - if cfg!(baremetal) { - ss.ready_thread(client_pid, client_tid)?; - } Ok(xous_kernel::Result::Ok) } else { // Switch away from the server, but leave it as Runnable @@ -455,7 +454,6 @@ fn return_scalar( ss.set_thread_result(server_pid, server_tid, xous_kernel::Result::Ok)?; // Switch to the client - ss.ready_thread(client_pid, client_tid)?; ss.switch_to_thread(client_pid, Some(client_tid))?; Ok(xous_kernel::Result::Scalar1(arg)) } From 011f10c9d1252b921f8e7e45421b43b440d20929 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:54:08 +0800 Subject: [PATCH 74/80] kernel: riscv: print processes from tid #0 Start numbering threads from index 0. This matches up with what the rest of the system uses. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/irq.rs | 5 ++++- kernel/src/arch/riscv/process.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index a0b8d8ca7..20611d955 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -132,7 +132,10 @@ pub extern "C" fn trap_handler( && sstatus::read().spp() == sstatus::SPP::Supervisor && sc.bits() == 0xf { - panic!("Ran out of kernel stack"); + let pid = current_pid(); + let ex = RiscvException::from_regs(sc.bits(), sepc::read(), stval::read()); + print!("KERNEL({}): RISC-V fault: {} - ", pid, ex); + panic!("Maybe ran out of kernel stack?"); } let pid = current_pid(); diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index fe65bec87..24ec64966 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -435,7 +435,7 @@ impl Process { pub fn print_all_threads(&self) { let process = unsafe { &mut *PROCESS }; for (tid_idx, &thread) in process.threads.iter().enumerate() { - let tid = tid_idx + 1; + let tid = tid_idx; if thread.registers[1] != 0 { Self::print_thread(tid, &thread); } From 5dbad157449283332d695ff9d2aa916d43a2222c Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:55:04 +0800 Subject: [PATCH 75/80] kernel: main: simplify process search The process search now uses iterators properly. It's still not ideal because it needs to investigate every process, but it's better than it was. Signed-off-by: Sean Cross --- kernel/src/main.rs | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/kernel/src/main.rs b/kernel/src/main.rs index cefd0191d..bb3ef7db4 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -49,7 +49,11 @@ fn handle_panic(_arg: &PanicInfo) -> ! { /// # Safety /// /// This is safe to call only to initialize the kernel. -pub unsafe extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offset: *mut u32) { +pub unsafe extern "C" fn init( + arg_offset: *const u32, + init_offset: *const u32, + rpt_offset: *mut u32, +) { args::KernelArguments::init(arg_offset); let args = args::KernelArguments::get(); // Everything needs memory, so the first thing we should do is initialize the memory manager. @@ -79,33 +83,15 @@ fn next_pid_to_run(last_pid: Option) -> Option { // PIDs are 1-indexed but arrays are 0-indexed. By not subtracting // 1 from the PID when we use it as an array index, we automatically // pick the next process in the list. - let current_pid = last_pid.unwrap_or(unsafe { PID::new_unchecked(1) }).get() as usize; + let next_pid = last_pid.map(|v| v.get() as usize).unwrap_or(1); SystemServices::with(|system_services| { - for test_idx in current_pid..system_services.processes.len() { - if system_services.processes[test_idx].ppid.get() == 1 { - // print!("PID {} is owned by PID1... ", test_idx + 1); - if system_services.processes[test_idx].runnable() { - // println!(" and is runnable"); - return match pid_from_usize(test_idx + 1) { - Ok(x) => Some(x), - Err(_) => None, - }; - } - // println!(" and is NOT RUNNABLE"); - } - } - for test_idx in 0..current_pid { - if system_services.processes[test_idx].ppid.get() == 1 { - // print!("PID {} is owned by PID1... ", test_idx + 1); - if system_services.processes[test_idx].runnable() { - // println!(" and is runnable"); - return match pid_from_usize(test_idx + 1) { - Ok(x) => Some(x), - Err(_) => None, - }; - } - // println!(" and is NOT RUNNABLE"); + for process in system_services.processes[next_pid..] + .iter() + .chain(system_services.processes[..next_pid].iter()) + { + if process.runnable() { + return Some(process.pid); } } None From bdad4f920479ea13a8c1249434925420b31cf552 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:55:40 +0800 Subject: [PATCH 76/80] kernel: mem: increase default mem size The default mem size got shrunk down to 4 MB earlier, but there is a lingering issue that prevents specifying virtual addresses. As a result, pddb and others don't work. Temporarily revert this change. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index f39dedfdc..b4aab04e9 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -312,7 +312,7 @@ impl MemoryManager { kind: xous_kernel::MemoryType, ) -> Result<*mut u8, xous_kernel::Error> { // If we were supplied a perfectly good address, return that. - if virt_ptr as usize != 0 { + if !virt_ptr.is_null() { return Ok(virt_ptr); } @@ -330,7 +330,7 @@ impl MemoryManager { } xous_kernel::MemoryType::Default => ( process_inner.mem_default_base, - process_inner.mem_default_base + 0x40_0000, // Limit to one superpage + process_inner.mem_default_base + 0x1000_0000, process_inner.mem_default_last, ), xous_kernel::MemoryType::Messages => ( From 179048c1b2198c3fc29a46180db5cb9a9fbe6272 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:56:39 +0800 Subject: [PATCH 77/80] kernel: syscall: add notes for handing mem to user Add notes on how to properly hand memory to userspace. This does not yet work, but is committed here as a work-in-progress. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 5c9823163..e48952069 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -885,19 +885,31 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR // If we're handing back an address in main RAM, zero it out. If // phys is 0, then the page will be lazily allocated, so we // don't need to do this. - if phys.is_some() { + // if !phys_ptr.is_null() { + // for (phys, virt) in (phys_ptr as usize..range.len()).step_by(PAGE_SIZE).zip( + // (range.as_ptr() as usize..(range.as_ptr() as usize + range.len())) + // .step_by(PAGE_SIZE), + // ) { + // // Zero out the virtual page if it's in main memory + // if mm.is_main_memory(phys as *mut u8) { + // let start = virt as *mut usize; + // for offset in 0..(PAGE_SIZE / core::mem::size_of::()) { + // unsafe { start.add(offset).write_volatile(0) }; + // } + // } + + // // Hand the virtual page to the process + // crate::arch::mem::hand_page_to_user(virt as *mut u8) + // .expect("couldn't hand page to user"); + // } + + if !phys_ptr.is_null() { if mm.is_main_memory(phys_ptr) { - // println!( - // "Going to zero out {} bytes @ {:08x}", - // range.len(), - // range.as_ptr() as usize, - // ); unsafe { range .as_mut_ptr() - .write_bytes(0, range.len() / mem::size_of::()) + .write_bytes(0, range.len() / core::mem::size_of::()) }; - // println!("Done zeroing out"); } for offset in (range.as_ptr() as usize..(range.as_ptr() as usize + range.len())) .step_by(PAGE_SIZE) From c1728f482c2a94a8cf7657151a480414b739bcc0 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:57:46 +0800 Subject: [PATCH 78/80] kernel: services: fix corner cases on thread selection There were a few corner cases when it came to thread selection. Namely, the same thread had a higher chance of repeatedly being executed. Clean up these corner cases, and as a result get libstd tests to pass. Signed-off-by: Sean Cross --- kernel/src/services.rs | 131 +++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 70 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index a708919b9..92a1449cb 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -285,9 +285,10 @@ impl core::fmt::Debug for Process { fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> { write!( fmt, - "Process {} state: {:?} Memory mapping: {:?}", + "Process {} state: {:?} TID: {} Memory mapping: {:?}", self.pid.get(), self.state, + self.current_thread, self.mapping ) } @@ -547,8 +548,7 @@ impl SystemServices { process.activate()?; // Activate the current context - let mut arch_process = ArchProcess::current(); - arch_process.set_tid(tid)?; + ArchProcess::current().set_tid(tid)?; process.current_thread = tid; } // self.pid = pid; @@ -587,11 +587,10 @@ impl SystemServices { let mut current = self .get_process_mut(current_pid) .expect("couldn't get current PID"); + assert!(arch::process::current_tid() == current.current_thread); // let old_state = current.state; current.state = match current.state { - ProcessState::Running(x) => { - ProcessState::Ready(x | (1 << arch::process::current_tid())) - } + ProcessState::Running(x) => ProcessState::Ready(x | (1 << current.current_thread)), y => panic!("current process was {:?}, not 'Running(_)'", y), }; // log_process_update(file!(), line!(), current, old_state); @@ -657,7 +656,8 @@ impl SystemServices { let process = self.get_process(pid)?; if let Some(tid) = tid { Ok(match process.state { - ProcessState::Running(x) | ProcessState::Ready(x) if x & (1 << tid) != 0 => true, + ProcessState::Running(x) => tid == process.current_thread || x & (1 << tid) != 0, + ProcessState::Ready(x) if x & (1 << tid) != 0 => true, ProcessState::Sleeping => true, _ => false, }) @@ -785,63 +785,39 @@ impl SystemServices { ProcessState::Ready(0) => { panic!("ProcessState was `Ready(0)`, which is invalid!"); } - ProcessState::Ready(x) => { - let new_thread = - tid.unwrap_or_else(|| Self::find_next_thread(x, process.current_thread)); - if x & (1 << new_thread) == 0 { + ProcessState::Ready(ready_threads) => { + let new_thread = tid.unwrap_or_else(|| { + Self::find_next_thread(ready_threads, process.current_thread) + }); + + if ready_threads & (1 << new_thread) == 0 { panic!("invalid thread ID"); } process.activate()?; - let mut p = ArchProcess::current(); - // FIXME: What happens if this fails? We're currently in the new process - // but without a context to switch to. - p.set_tid(new_thread)?; + + ArchProcess::current().set_tid(new_thread)?; process.current_thread = new_thread as _; - - // Remove the new context from the available context list - ProcessState::Running(x & !(1 << new_thread)) - } - ProcessState::Running(0) => { - // TODO: If `context` is not `None`, what do we do here? - - // This process is already running, and there aren't any new available - // contexts, so keep on going. - ProcessState::Running(0) + ProcessState::Running(ready_threads & !(1 << new_thread)) } ProcessState::Running(ready_threads) => { - let mut p = ArchProcess::current(); - // let current_thread = p.current_thread(); - let new_thread = match tid { - None => { - let mut new_thread = 0; + // Ensure we can switch back to this thread, if necessary + let ready_threads = ready_threads | (1 << process.current_thread); - while ready_threads & (1 << new_thread) == 0 { - new_thread += 1; - if new_thread > arch::process::MAX_THREAD { - new_thread = 0; - } - } - new_thread - } - Some(tid) => { - // Ensure the specified context is ready to run, or is - // currently running. - if ready_threads & (1 << tid) == 0 { - return Err(xous_kernel::Error::InvalidThread); - } - tid - } - }; + let new_thread = tid.unwrap_or_else(|| { + Self::find_next_thread(ready_threads, process.current_thread) + }); - // Remove the new thread ID from the list of thread IDs - let new_mask = ready_threads & !(1 << new_thread); + // Ensure the specified context is ready to run, or is + // currently running. + if ready_threads & (1 << new_thread) == 0 { + return Err(xous_kernel::Error::InvalidThread); + } // Activate this process on this CPU - process.activate()?; - p.set_tid(new_thread)?; + ArchProcess::current().set_tid(new_thread)?; process.current_thread = new_thread as _; - ProcessState::Running(new_mask) + ProcessState::Running(ready_threads & !(1 << new_thread)) } }; // log_process_update(file!(), line!(), process, old_state); @@ -933,6 +909,11 @@ impl SystemServices { // Temporarily switch into the target process memory space // in order to pass the return value. let current_pid = self.current_pid(); + if current_pid == pid { + ArchProcess::current().set_thread_result(tid, result); + return Ok(()); + } + { let target_process = self.get_process(pid)?; target_process.activate()?; @@ -983,7 +964,7 @@ impl SystemServices { new_tid = crate::arch::process::EXCEPTION_TID; // new.current_thread = new_tid; } - ProcessState::Running(x) | ProcessState::Ready(x) => { + ProcessState::Ready(x) => { // If no new context is specified, take the previous // context. If that is not runnable, do a round-robin // search for the next available context. @@ -1004,6 +985,9 @@ impl SystemServices { } new.current_thread = new_tid as _; } + ProcessState::Running(_) => { + panic!("process was running even though the pid was different") + } ProcessState::Sleeping | ProcessState::Debug(_) | ProcessState::BlockedException(_) => { @@ -1053,15 +1037,20 @@ impl SystemServices { .get_process_mut(previous_pid) .expect("couldn't get previous pid"); let _oldstate = previous.state; // for tracking state in the debug print after the following closure + if previous.current_thread != previous_tid { + println!( + "WARNING: previous.current_thread {} != previous_tid {}", + previous.current_thread, previous_tid + ); + } + previous.current_thread = previous_tid; previous.state = match previous.state { // If the previous process had exactly one thread that can be // run, then the Running thread list will be 0. In that case, // we will either need to Sleep this process, or mark it as // being Ready to run. ProcessState::Running(x) if x == 0 => { - if can_resume - /*|| advance_thread*/ - { + if can_resume { ProcessState::Ready(1 << previous_tid) } else { ProcessState::Sleeping @@ -1121,15 +1110,22 @@ impl SystemServices { // Transition to the new state. // let old_state = new.state; new.state = if let ProcessState::Running(x) = new.state { - let previous_tid = new.current_thread; + assert!(x & (1 << new.current_thread) == 0); + + // If the current process can be resumed, add it to the list + // of potential threads + let x = x | if can_resume { + 1 << new.current_thread + } else { + 0 + }; + // If no new thread is specified, take the previous // thread. If that is not runnable, do a round-robin // search for the next available thread. - let new_tid = if new_tid == 0 { - Self::find_next_thread(x, new.current_thread) - } else { - new_tid - }; + if new_tid == 0 { + new_tid = Self::find_next_thread(x, new.current_thread); + } if x & (1 << new_tid) == 0 { return Err(xous_kernel::Error::ThreadNotAvailable); @@ -1137,11 +1133,8 @@ impl SystemServices { new.current_thread = new_tid as _; - // Mark the previous TID as being runnable, and remove the new TID - // from the list of threads that can be run. - ProcessState::Running( - x & !(1 << new_tid) | if can_resume { 1 << previous_tid } else { 0 }, - ) + // Remove the new TID from the list of threads that can be run. + ProcessState::Running(x & !(1 << new_tid)) } else { panic!( "PID {} invalid process state (not Running): {:?}", @@ -1151,10 +1144,8 @@ impl SystemServices { // log_process_update(file!(), line!(), new, old_state); } - let mut process = ArchProcess::current(); - // Restore the previous thread, if one exists. - process.set_tid(new_tid)?; + ArchProcess::current().set_tid(new_tid)?; klog!( "Activated process {}:{}, new state: {:?}", From b5b22c9119839f228c7363ad49830e15bda2f718 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:58:47 +0800 Subject: [PATCH 79/80] emulation: timer0: adapt timer0 to custom precursor Precursor has a custom TIMER0 module that is different from other LiteX timers. Adapt the Renode model to match the hardware. Signed-off-by: Sean Cross --- emulation/peripherals/LiteX_Timer_32.cs | 28 +++---------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/emulation/peripherals/LiteX_Timer_32.cs b/emulation/peripherals/LiteX_Timer_32.cs index 59fcb9b73..d458cb2d6 100644 --- a/emulation/peripherals/LiteX_Timer_32.cs +++ b/emulation/peripherals/LiteX_Timer_32.cs @@ -90,25 +90,6 @@ namespace Antmicro.Renode.Peripherals.Timers.Betrusted }) ; - Registers.TimerUpdateValue.Define32(this) - .WithFlag(0, FieldMode.WriteOneToClear, name: "UPDATE_VALUE", writeCallback: (_, val) => - { - if(val) - { - latchedValue = (uint)innerTimer.Value; - } - }); - ; - - // VALUE0 contains most significant 8 bits - // VALUE3 contains least significant 8 bits - Registers.Value.Define32(this) - .WithValueField(0, 32, FieldMode.Read, name: "VALUE", valueProviderCallback: _ => - { - return latchedValue; - }); - ; - Registers.EventStatus.Define32(this) .WithFlag(0, FieldMode.Read, name: "EV_STATUS", valueProviderCallback: _ => innerTimer.Value == 0) ; @@ -144,13 +125,10 @@ namespace Antmicro.Renode.Peripherals.Timers.Betrusted Reload = 0x04, TimerEnable = 0x08, - TimerUpdateValue = 0x0C, - Value = 0x10, - - EventStatus = 0x14, - EventPending = 0x18, - EventEnable = 0x1C + EventStatus = 0x0c, + EventPending = 0x10, + EventEnable = 0x14 } } } From 4133238d8a11399e9c70dc48d8594957205ea9e7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 21:59:30 +0800 Subject: [PATCH 80/80] emulation: ticktimer: fix higher bits of ticktimer The ticktimer wasn't properly adjusting the upper bits, meaning delays of more than 42 days did not function properly. This was discovered as part of the libstd tests. Signed-off-by: Sean Cross --- emulation/peripherals/ticktimer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emulation/peripherals/ticktimer.cs b/emulation/peripherals/ticktimer.cs index 854ec55af..b504e8877 100644 --- a/emulation/peripherals/ticktimer.cs +++ b/emulation/peripherals/ticktimer.cs @@ -100,7 +100,7 @@ namespace Antmicro.Renode.Peripherals.Timers.Betrusted Registers.MsleepTarget1.Define32(this) .WithValueField(0, 32, name: "MsleepTarget1", writeCallback: (_, value) => { - this.msleepTarget = (this.msleepTarget & 0x00000000ffffffff) | (value << 32); + this.msleepTarget = (this.msleepTarget & 0x00000000ffffffff) | (((ulong)value) << 32); this.Log(LogLevel.Noisy, "Setting MsleepTarget1: {0}, sleep target now: {1}", value, this.msleepTarget); }, valueProviderCallback: _ => @@ -142,7 +142,7 @@ namespace Antmicro.Renode.Peripherals.Timers.Betrusted public bool paused; public long tickValue; - ulong msleepTarget; + public ulong msleepTarget; public GPIO IRQ { get; private set; } private enum Registers