Skip to content

Commit

Permalink
Fix default value and save config after factory-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Apr 11, 2024
1 parent a892970 commit da2ac3a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ version = "1.7.0-rc.1"
memory-regions = { path = "components/memory-regions" }

# forked
admin-app = { git = "https://github.com/Nitrokey/admin-app.git", rev = "054536c2b46722b657fdc4d5806a5edcb10b5256" }
admin-app = { git = "https://github.com/Nitrokey/admin-app.git", rev = "c257432dbe2efb53424d6847d82d90ddb527c53b" }
cbor-smol = { git = "https://github.com/Nitrokey/cbor-smol.git", tag = "v0.4.0-nitrokey.3"}
fido-authenticator = { git = "https://github.com/Nitrokey/fido-authenticator.git", tag = "v0.1.1-nitrokey.14" }
lpc55-hal = { git = "https://github.com/Nitrokey/lpc55-hal", tag = "v0.3.0-nitrokey.2" }
Expand Down
1 change: 1 addition & 0 deletions components/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = { workspace = true }
edition = "2021"

[dependencies]
delog = "0.1"
apdu-dispatch = "0.1"
bitflags = "2"
ctaphid-dispatch = "0.1"
Expand Down
46 changes: 31 additions & 15 deletions components/apps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ use littlefs2::path;
#[cfg(feature = "factory-reset")]
use admin_app::ResetConfigResult;

#[macro_use]
extern crate delog;

generate_macros!();

use serde::{Deserialize, Serialize};
#[cfg(all(feature = "opcard", feature = "se050"))]
use trussed::{api::NotBefore, service::Filestore};
Expand Down Expand Up @@ -151,23 +156,13 @@ impl FidoConfig {
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[derive(Debug, PartialEq, Deserialize, Serialize, Default)]
pub struct OpcardConfig {
#[cfg(feature = "se050")]
#[serde(default, rename = "s", skip_serializing_if = "is_default")]
use_se050_backend: bool,
}

#[allow(clippy::derivable_impls)]
impl Default for OpcardConfig {
fn default() -> Self {
Self {
#[cfg(feature = "se050")]
use_se050_backend: true,
}
}
}

#[cfg(feature = "opcard")]
impl OpcardConfig {
fn backends(&self) -> &'static [BackendId<Backend>] {
Expand All @@ -194,6 +189,18 @@ impl OpcardConfig {
}

impl OpcardConfig {
/// The config value used for initialization and after a factory-reset
///
/// This is distinct from the `Default` value because the old default config was not
/// enabled
#[cfg(any(feature = "factory-reset", feature = "se050"))]
fn init() -> Self {
Self {
#[cfg(feature = "se050")]
use_se050_backend: true,
}
}

fn field(&mut self, key: &str) -> Option<ConfigValueMut<'_>> {
match key {
#[cfg(feature = "se050")]
Expand All @@ -219,7 +226,7 @@ impl OpcardConfig {
#[cfg(feature = "factory-reset")]
fn reset_config(&mut self) -> ResetConfigResult {
use core::mem;
let old = mem::take(self);
let old = mem::replace(self, Self::init());

if &old == self {
ResetConfigResult::Unchanged
Expand Down Expand Up @@ -443,7 +450,8 @@ impl<R: Runner> Apps<R> {
&& app.config().fs_version == 0
&& !app.config().opcard.use_se050_backend
{
let trussed_auth_used = trussed_auth::AuthBackend::is_client_active(
use core::mem;
let opcard_trussed_auth_used = trussed_auth::AuthBackend::is_client_active(
trussed_auth::FilesystemLayout::V0,
dispatch::AUTH_LOCATION,
path!("opcard"),
Expand All @@ -456,9 +464,17 @@ impl<R: Runner> Apps<R> {
.unwrap_or_default()
.is_none();

if !trussed_auth_used && !opcard_used {
if !opcard_trussed_auth_used && !opcard_used {
// No need to factory reset because the app is not yet created yet
app.config_mut().opcard.use_se050_backend = true;
let mut config = OpcardConfig::init();
mem::swap(&mut app.config_mut().opcard, &mut config);
app.save_config_filestore(&mut filestore)
.map_err(|_err| {
// We reset the config to the old on file version to avoid invalid operations
mem::swap(&mut app.config_mut().opcard, &mut config);
error_now!("Failed to save config after migration: {_err:?}");
})
.ok();
}
}

Expand Down

0 comments on commit da2ac3a

Please sign in to comment.