Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle reset signal from the admin app #188

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ SPDX-License-Identifier: CC0-1.0

## Unreleased

- Support factory reset through the admin app ([#188][])

[#188]: https://github.com/Nitrokey/opcard-rs/pull/188

## [v1.2.0][](2023-11-08)

### Bugfixes
Expand Down
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ trussed-staging = { version = "0.1.0", features = ["wrap-key-to-file", "chunked"
serde_repr = "0.1"
hex-literal = "0.4.1"
trussed-auth = "0.2.1"
admin-app = { version = "0.1.0", optional = true }

# optional dependencies
apdu-dispatch = { version = "0.1", optional = true }
Expand Down Expand Up @@ -88,13 +89,15 @@ log-error = []
[patch.crates-io]
iso7816 = { git = "https://github.com/Nitrokey/iso7816.git", tag = "v0.1.1-nitrokey.1" }
p256-cortex-m4 = { git = "https://github.com/Nitrokey/p256-cortex-m4", tag = "v0.1.0-alpha.6-nitrokey-1" }
trussed = { git = "https://github.com/nitrokey/trussed" , tag = "v0.1.0-nitrokey.11" }
trussed-auth = { git = "https://github.com/trussed-dev/trussed-auth.git", tag = "v0.2.2"}
trussed-rsa-alloc = { git = "https://github.com/Nitrokey/trussed-rsa-backend", tag = "v0.1.0" }
trussed-staging = { git = "https://github.com/Nitrokey/trussed-staging", tag = "v0.1.0" }
trussed = { git = "https://github.com/nitrokey/trussed" , rev = "976372331be2f1b37cab532420cb6c55e0d54473" }
trussed-auth = { git = "https://github.com/trussed-dev/trussed-auth.git", rev = "4b8191f248c26cb074cdac887c7f3f48f9c449a4"}
trussed-rsa-alloc = { git = "https://github.com/trussed-dev/trussed-rsa-backend", rev = "baced761b2be0f207d88aca47fbe2fad4736618a" }
trussed-staging = { git = "https://github.com/trussed-dev/trussed-staging.git", rev = "10baac2608e98e25ea77ade974a4e26f778d6c8e" }
apdu-dispatch = { git = "https://github.com/Nitrokey/apdu-dispatch", tag = "v0.1.2-nitrokey.2" }
trussed-usbip = { git = "https://github.com/Nitrokey/pc-usbip-runner.git", tag = "v0.0.1-nitrokey.1" }
usbd-ccid = { git = "https://github.com/Nitrokey/usbd-ccid", tag = "v0.2.0-nitrokey.1" }
admin-app = { git = "https://github.com/Nitrokey/admin-app.git", rev = "410899311ae7b194360366ff477f74d4d278e056" }
ctaphid-dispatch = { git = "https://github.com/trussed-dev/ctaphid-dispatch.git", rev = "57cb3317878a8593847595319aa03ef17c29ec5b" }

[package.metadata.docs.rs]
all-features = true
Expand Down
48 changes: 47 additions & 1 deletion src/card.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2022 Nitrokey GmbH
// SPDX-License-Identifier: LGPL-3.0-only

#[cfg(feature = "admin-app")]
use admin_app::{ResetSignal, ResetSignalAllocation};
use hex_literal::hex;
use iso7816::Status;
use trussed::types::Location;
Expand Down Expand Up @@ -43,6 +45,12 @@ impl<T: Client> Card<T> {
}
}

#[cfg(feature = "admin-app")]
fn ack_factory_reset(&mut self, reset_signal: &ResetSignalAllocation) -> bool {
self.state = State::default();
reset_signal.ack_factory_reset()
}

/// Handles an APDU command and writes the response to the given buffer.
///
/// The APDU command must be complete, i. e. chained commands must be resolved by the caller.
Expand All @@ -51,6 +59,21 @@ impl<T: Client> Card<T> {
command: &iso7816::Command<C>,
reply: &mut heapless::Vec<u8, R>,
) -> Result<(), Status> {
#[cfg(feature = "admin-app")]
if let Some(reset_signal) = self.options.reset_signal {
match reset_signal.load() {
ResetSignal::None => {}
ResetSignal::ConfigChanged => {
return Err(Status::SelectedFileInTerminationState);
}
ResetSignal::FactoryReset => {
if !self.ack_factory_reset(reset_signal) {
return Err(Status::SelectedFileInTerminationState);
}
}
}
}

trace!("Received APDU {:?}", command);
let card_command = Command::try_from(command).inspect_err_stable(|_err| {
warn!("Failed to parse command: {command:x?} {_err:?}");
Expand All @@ -68,6 +91,21 @@ impl<T: Client> Card<T> {

/// Resets the state of the card.
pub fn reset(&mut self) {
#[cfg(feature = "admin-app")]
if let Some(reset_signal) = self.options.reset_signal {
match reset_signal.load() {
ResetSignal::None => {}
ResetSignal::ConfigChanged => {
debug!("Attempt to reset opcard with reset signal active");
return;
}
ResetSignal::FactoryReset => {
self.ack_factory_reset(reset_signal);
return;
}
}
}

self.state.volatile.clear(self.backend.client_mut());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this in the factory reset case in handle too? Maybe introduce a helper method reset_state?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clear method removes the private keys in volatile memory. Since the Factory Reset has already cleared all the filesystems, including the volatile, it will fail.

It was actually a mistake to do it after a factory reset.

let state = State::default();
self.state = state;
Expand Down Expand Up @@ -121,7 +159,7 @@ impl<T: Client, const C: usize, const R: usize> apdu_dispatch::App<C, R> for Car
}

/// Options for the OpenPGP card.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Options {
/// The manufacturer ID returned in the AID, see § 4.2.1 of the spec.
Expand All @@ -137,6 +175,12 @@ pub struct Options {
pub button_available: bool,
/// Which trussed storage to use
pub storage: Location,

/// Flag to signal that the application has had its configuration changed or was factory-resetted by the admin application
///
/// Requires the feature-flag admin-app
#[cfg(feature = "admin-app")]
pub reset_signal: Option<&'static ResetSignalAllocation>,
}

impl Options {
Expand Down Expand Up @@ -175,6 +219,8 @@ impl Default for Options {
historical_bytes: heapless::Vec::from_slice(&hex!("0031F573C00160009000")).unwrap(),
button_available: true,
storage: Location::External,
#[cfg(feature = "admin-app")]
reset_signal: None,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub enum LifeCycle {
Operational = 0x05,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct State {
// Persistent state may not be loaded, or may error when loaded
pub persistent: Option<Persistent>,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl State {
}
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug)]
pub struct LoadedState<'s> {
pub persistent: &'s mut Persistent,
pub volatile: &'s mut Volatile,
Expand Down