xref: /aosp_15_r20/system/keymint/hal/src/secureclock.rs (revision 9860b7637a5f185913c70aa0caabe3ecb78441e4)
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! SecureClock HAL device implementation.
16 
17 use super::{ChannelHalService, SerializedChannel};
18 use crate::binder;
19 use crate::hal::secureclock::{ISecureClock, TimeStampToken::TimeStampToken};
20 use crate::hal::Innto;
21 use kmr_wire::*;
22 use std::sync::{Arc, Mutex, MutexGuard};
23 
24 /// `ISecureClock` implementation which converts all method invocations to serialized requests that
25 /// are sent down the associated channel.
26 pub struct Device<T: SerializedChannel + 'static> {
27     channel: Arc<Mutex<T>>,
28 }
29 
30 impl<T: SerializedChannel + Send> binder::Interface for Device<T> {}
31 
32 impl<T: SerializedChannel + 'static> Device<T> {
33     /// Construct a new instance that uses the provided channel.
new(channel: Arc<Mutex<T>>) -> Self34     pub fn new(channel: Arc<Mutex<T>>) -> Self {
35         Self { channel }
36     }
37     /// Create a new instance wrapped in a proxy object.
new_as_binder(channel: Arc<Mutex<T>>) -> binder::Strong<dyn ISecureClock::ISecureClock>38     pub fn new_as_binder(channel: Arc<Mutex<T>>) -> binder::Strong<dyn ISecureClock::ISecureClock> {
39         ISecureClock::BnSecureClock::new_binder(
40             Self::new(channel),
41             binder::BinderFeatures::default(),
42         )
43     }
44 }
45 
46 impl<T: SerializedChannel> ChannelHalService<T> for Device<T> {
channel(&self) -> MutexGuard<T>47     fn channel(&self) -> MutexGuard<T> {
48         self.channel.lock().unwrap()
49     }
50 }
51 
52 impl<T: SerializedChannel> ISecureClock::ISecureClock for Device<T> {
generateTimeStamp(&self, challenge: i64) -> binder::Result<TimeStampToken>53     fn generateTimeStamp(&self, challenge: i64) -> binder::Result<TimeStampToken> {
54         let rsp: GenerateTimeStampResponse =
55             self.execute(GenerateTimeStampRequest { challenge })?;
56         Ok(rsp.ret.innto())
57     }
58 }
59