Skip to content

Commit 1456bf1

Browse files
Merge pull request #71 from Nitrokey/activate-default
Set the default lifecycle to operational
2 parents 6f0edc0 + 3a65175 commit 1456bf1

File tree

5 files changed

+13
-40
lines changed

5 files changed

+13
-40
lines changed

examples/virtual.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@
2525

2626
// TODO: add CLI
2727

28-
use hex_literal::hex;
29-
3028
fn main() {
3129
env_logger::init();
3230

3331
trussed::virt::with_ram_client("opcard", |client| {
34-
let mut card = opcard::Card::new(client, opcard::Options::default());
35-
card.handle(
36-
&iso7816::Command::<10>::try_from(&hex!("00 44 00 00")).unwrap(),
37-
&mut heapless::Vec::<u8, 2>::new(),
38-
)
39-
.unwrap();
32+
let card = opcard::Card::new(client, opcard::Options::default());
4033
let mut virtual_card = opcard::VirtualCard::new(card);
4134
let vpicc = vpicc::connect().expect("failed to connect to vpicc");
4235
vpicc

src/command/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ mod tests {
12321232
};
12331233

12341234
let mut historical_bytes = options.historical_bytes.clone();
1235-
historical_bytes[7] = 3;
1235+
historical_bytes[7] = 5;
12361236

12371237
get_data(
12381238
context,

src/state.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,12 @@ impl State {
197197
}
198198
pub fn lifecycle(client: &mut impl trussed::Client) -> LifeCycle {
199199
match try_syscall!(client.entry_metadata(Location::Internal, Self::lifecycle_path())) {
200-
Ok(Metadata {
201-
metadata: Some(metadata),
202-
}) if metadata.is_file() => LifeCycle::Operational,
203-
_ => LifeCycle::Initialization,
200+
Ok(Metadata { metadata: Some(_) }) => LifeCycle::Initialization,
201+
_ => LifeCycle::Operational,
204202
}
205203
}
206204

207205
pub fn terminate_df(client: &mut impl trussed::Client) -> Result<(), Status> {
208-
try_syscall!(client.remove_file(Location::Internal, Self::lifecycle_path(),))
209-
.map(|_| {})
210-
.map_err(|_err| {
211-
error!("Failed to write lifecycle: {_err:?}");
212-
Status::UnspecifiedPersistentExecutionError
213-
})
214-
}
215-
216-
pub fn activate_file(client: &mut impl trussed::Client) -> Result<(), Status> {
217206
try_syscall!(client.write_file(
218207
Location::Internal,
219208
Self::lifecycle_path(),
@@ -226,6 +215,13 @@ impl State {
226215
Status::UnspecifiedPersistentExecutionError
227216
})
228217
}
218+
219+
pub fn activate_file(client: &mut impl trussed::Client) -> Result<(), Status> {
220+
try_syscall!(client.remove_file(Location::Internal, Self::lifecycle_path(),)).ok();
221+
// Errors can happen because of the removal of all files before the call to activate_file
222+
// so they are silenced
223+
Ok(())
224+
}
229225
}
230226

231227
#[derive(Debug, Eq, PartialEq)]

tests/card/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use std::sync::{Arc, Mutex};
66

7-
use hex_literal::hex;
87
use iso7816::{command::FromSliceError, Command, Status};
98
use opcard::Options;
109
use openpgp_card::{
@@ -115,7 +114,7 @@ impl<T: trussed::Client + Send + Sync + 'static> CardTransaction for Transaction
115114

116115
pub fn with_card_options<F: FnOnce(Card<Client<Ram>>) -> R, R>(options: Options, f: F) -> R {
117116
trussed::virt::with_ram_client("opcard", |client| {
118-
with_activated_card(opcard::Card::new(client, options), f)
117+
f(Card::from_opcard(opcard::Card::new(client, options)))
119118
})
120119
}
121120

@@ -140,13 +139,3 @@ pub fn error_to_retries(err: Result<(), openpgp_card::Error>) -> Option<u8> {
140139
Err(e) => panic!("Unexpected error {e}"),
141140
}
142141
}
143-
144-
fn with_activated_card<F: FnOnce(Card<Client<Ram>>) -> R, R>(
145-
mut card: opcard::Card<Client<Ram>>,
146-
f: F,
147-
) -> R {
148-
let command: iso7816::Command<4> = iso7816::Command::try_from(&hex!("00 44 0000")).unwrap();
149-
let mut rep: heapless::Vec<u8, 0> = heapless::Vec::new();
150-
card.handle(&command, &mut rep).unwrap();
151-
f(Card::from_opcard(card))
152-
}

tests/virt/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::{
1212
time::Duration,
1313
};
1414

15-
use hex_literal::hex;
1615
use regex::{Regex, RegexSet};
1716
use stoppable_thread::spawn;
1817

@@ -38,11 +37,7 @@ pub fn with_vsc<F: FnOnce() -> R, R>(f: F) -> R {
3837
let (tx, rx) = mpsc::channel();
3938
let handle = spawn(move |stopped| {
4039
trussed::virt::with_ram_client("opcard", |client| {
41-
let mut card = opcard::Card::new(client, opcard::Options::default());
42-
let command: iso7816::Command<4> =
43-
iso7816::Command::try_from(&hex!("00 44 0000")).unwrap();
44-
let mut rep: heapless::Vec<u8, 0> = heapless::Vec::new();
45-
card.handle(&command, &mut rep).unwrap();
40+
let card = opcard::Card::new(client, opcard::Options::default());
4641
let mut virtual_card = opcard::VirtualCard::new(card);
4742
let mut result = Ok(());
4843
while !stopped.get() && result.is_ok() {

0 commit comments

Comments
 (0)