Skip to content

Commit

Permalink
Public method to check if sleeping is allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczmarczyck committed Jan 9, 2024
1 parent ba0d717 commit 02a9fb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libraries/opensk/src/ctap/client_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ impl<E: Env> ClientPin<E> {
}
}

/// Checks if a PIN UV token is in use.
pub fn has_token(&mut self, env: &mut E) -> bool {
self.update_timeouts(env);
self.pin_uv_auth_token_state.is_in_use()
}

/// Gets a reference to the PIN protocol of the given version.
fn get_pin_protocol(&self, pin_uv_auth_protocol: PinUvAuthProtocol) -> &PinProtocol<E> {
match pin_uv_auth_protocol {
Expand Down Expand Up @@ -1507,9 +1513,11 @@ mod test {
let mut env = TestEnv::default();
let mut client_pin = ClientPin::<TestEnv>::new(&mut env);
let message = [0xAA];
assert!(!client_pin.has_token(&mut env));
client_pin
.pin_uv_auth_token_state
.begin_using_pin_uv_auth_token(&mut env);
assert!(client_pin.has_token(&mut env));

let pin_uv_auth_token_v1 = client_pin
.get_pin_protocol(PinUvAuthProtocol::V1)
Expand Down Expand Up @@ -1655,6 +1663,7 @@ mod test {
.has_permissions_rp_id("example.com"),
Ok(())
);
assert!(client_pin.has_token(&mut env));

env.clock().advance(30001);
client_pin.update_timeouts(&mut env);
Expand All @@ -1672,6 +1681,7 @@ mod test {
.has_permissions_rp_id("example.com"),
Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID)
);
assert!(!client_pin.has_token(&mut env));
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions libraries/opensk/src/ctap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ impl<E: Env> CtapState<E> {
self.stateful_command_permission.clear_old_channels(channel);
}

/// Checks if the application has any timers running.
pub fn can_sleep(&mut self, env: &mut E) -> bool {
!self.client_pin.has_token(env)
&& self.stateful_command_permission.get_command(env).is_err()
}

pub fn process_command(
&mut self,
env: &mut E,
Expand Down
6 changes: 6 additions & 0 deletions libraries/opensk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl<E: Env> Ctap<E> {
self.hid.should_wink(&mut self.env)
}

pub fn can_sleep(&mut self) -> bool {
!self.should_wink() && self.state.can_sleep(&mut self.env)
}

#[cfg(feature = "with_ctap1")]
pub fn u2f_grant_user_presence(&mut self) {
self.state.u2f_grant_user_presence(&mut self.env)
Expand Down Expand Up @@ -201,6 +205,7 @@ mod test {
fn test_hard_reset() {
let env = TestEnv::default();
let mut ctap = Ctap::<TestEnv>::new(env);
assert!(!ctap.can_sleep());

// Send Init, receive Init response.
let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid);
Expand All @@ -223,6 +228,7 @@ mod test {
let mut env = TestEnv::default();
env.set_boots_after_soft_reset(true);
let mut ctap = Ctap::<TestEnv>::new(env);
assert!(ctap.can_sleep());

// Send Init, receive Init response.
let mut init_response = ctap.process_hid_packet(&init_packet(), Transport::MainHid);
Expand Down

0 comments on commit 02a9fb4

Please sign in to comment.