Skip to content

Commit 02a9fb4

Browse files
committed
Public method to check if sleeping is allowed
1 parent ba0d717 commit 02a9fb4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

libraries/opensk/src/ctap/client_pin.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ impl<E: Env> ClientPin<E> {
140140
}
141141
}
142142

143+
/// Checks if a PIN UV token is in use.
144+
pub fn has_token(&mut self, env: &mut E) -> bool {
145+
self.update_timeouts(env);
146+
self.pin_uv_auth_token_state.is_in_use()
147+
}
148+
143149
/// Gets a reference to the PIN protocol of the given version.
144150
fn get_pin_protocol(&self, pin_uv_auth_protocol: PinUvAuthProtocol) -> &PinProtocol<E> {
145151
match pin_uv_auth_protocol {
@@ -1507,9 +1513,11 @@ mod test {
15071513
let mut env = TestEnv::default();
15081514
let mut client_pin = ClientPin::<TestEnv>::new(&mut env);
15091515
let message = [0xAA];
1516+
assert!(!client_pin.has_token(&mut env));
15101517
client_pin
15111518
.pin_uv_auth_token_state
15121519
.begin_using_pin_uv_auth_token(&mut env);
1520+
assert!(client_pin.has_token(&mut env));
15131521

15141522
let pin_uv_auth_token_v1 = client_pin
15151523
.get_pin_protocol(PinUvAuthProtocol::V1)
@@ -1655,6 +1663,7 @@ mod test {
16551663
.has_permissions_rp_id("example.com"),
16561664
Ok(())
16571665
);
1666+
assert!(client_pin.has_token(&mut env));
16581667

16591668
env.clock().advance(30001);
16601669
client_pin.update_timeouts(&mut env);
@@ -1672,6 +1681,7 @@ mod test {
16721681
.has_permissions_rp_id("example.com"),
16731682
Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID)
16741683
);
1684+
assert!(!client_pin.has_token(&mut env));
16751685
}
16761686

16771687
#[test]

libraries/opensk/src/ctap/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ impl<E: Env> CtapState<E> {
602602
self.stateful_command_permission.clear_old_channels(channel);
603603
}
604604

605+
/// Checks if the application has any timers running.
606+
pub fn can_sleep(&mut self, env: &mut E) -> bool {
607+
!self.client_pin.has_token(env)
608+
&& self.stateful_command_permission.get_command(env).is_err()
609+
}
610+
605611
pub fn process_command(
606612
&mut self,
607613
env: &mut E,

libraries/opensk/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ impl<E: Env> Ctap<E> {
116116
self.hid.should_wink(&mut self.env)
117117
}
118118

119+
pub fn can_sleep(&mut self) -> bool {
120+
!self.should_wink() && self.state.can_sleep(&mut self.env)
121+
}
122+
119123
#[cfg(feature = "with_ctap1")]
120124
pub fn u2f_grant_user_presence(&mut self) {
121125
self.state.u2f_grant_user_presence(&mut self.env)
@@ -201,6 +205,7 @@ mod test {
201205
fn test_hard_reset() {
202206
let env = TestEnv::default();
203207
let mut ctap = Ctap::<TestEnv>::new(env);
208+
assert!(!ctap.can_sleep());
204209

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

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

0 commit comments

Comments
 (0)