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