• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! RemotelyProvisionedComponent HAL device implementation.
2 
3 use super::{ChannelHalService, SerializedChannel};
4 use crate::binder;
5 use crate::hal::{rkp, Innto};
6 use kmr_wire::*;
7 use std::sync::{Arc, Mutex, MutexGuard};
8 
9 /// `IRemotelyProvisionedComponent` implementation which converts all method invocations to
10 /// serialized requests that are sent down the associated channel.
11 pub struct Device<T: SerializedChannel + 'static> {
12     channel: Arc<Mutex<T>>,
13 }
14 
15 impl<T: SerializedChannel + 'static> Device<T> {
16     /// Construct a new instance that uses the provided channel.
new(channel: Arc<Mutex<T>>) -> Self17     pub fn new(channel: Arc<Mutex<T>>) -> Self {
18         Self { channel }
19     }
20 
21     /// Create a new instance wrapped in a proxy object.
new_as_binder( channel: Arc<Mutex<T>>, ) -> binder::Strong<dyn rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent>22     pub fn new_as_binder(
23         channel: Arc<Mutex<T>>,
24     ) -> binder::Strong<dyn rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent> {
25         rkp::IRemotelyProvisionedComponent::BnRemotelyProvisionedComponent::new_binder(
26             Self::new(channel),
27             binder::BinderFeatures::default(),
28         )
29     }
30 }
31 
32 impl<T: SerializedChannel> ChannelHalService<T> for Device<T> {
channel(&self) -> MutexGuard<T>33     fn channel(&self) -> MutexGuard<T> {
34         self.channel.lock().unwrap()
35     }
36 }
37 
38 impl<T: SerializedChannel> binder::Interface for Device<T> {}
39 
40 impl<T: SerializedChannel> rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent
41     for Device<T>
42 {
getHardwareInfo(&self) -> binder::Result<rkp::RpcHardwareInfo::RpcHardwareInfo>43     fn getHardwareInfo(&self) -> binder::Result<rkp::RpcHardwareInfo::RpcHardwareInfo> {
44         let rsp: GetRpcHardwareInfoResponse = self.execute(GetRpcHardwareInfoRequest {})?;
45         Ok(rsp.ret.innto())
46     }
generateEcdsaP256KeyPair( &self, testMode: bool, macedPublicKey: &mut rkp::MacedPublicKey::MacedPublicKey, ) -> binder::Result<Vec<u8>>47     fn generateEcdsaP256KeyPair(
48         &self,
49         testMode: bool,
50         macedPublicKey: &mut rkp::MacedPublicKey::MacedPublicKey,
51     ) -> binder::Result<Vec<u8>> {
52         let rsp: GenerateEcdsaP256KeyPairResponse =
53             self.execute(GenerateEcdsaP256KeyPairRequest { test_mode: testMode })?;
54         *macedPublicKey = rsp.maced_public_key.innto();
55         Ok(rsp.ret)
56     }
generateCertificateRequest( &self, testMode: bool, keysToSign: &[rkp::MacedPublicKey::MacedPublicKey], endpointEncryptionCertChain: &[u8], challenge: &[u8], deviceInfo: &mut rkp::DeviceInfo::DeviceInfo, protectedData: &mut rkp::ProtectedData::ProtectedData, ) -> binder::Result<Vec<u8>>57     fn generateCertificateRequest(
58         &self,
59         testMode: bool,
60         keysToSign: &[rkp::MacedPublicKey::MacedPublicKey],
61         endpointEncryptionCertChain: &[u8],
62         challenge: &[u8],
63         deviceInfo: &mut rkp::DeviceInfo::DeviceInfo,
64         protectedData: &mut rkp::ProtectedData::ProtectedData,
65     ) -> binder::Result<Vec<u8>> {
66         let rsp: GenerateCertificateRequestResponse =
67             self.execute(GenerateCertificateRequestRequest {
68                 test_mode: testMode,
69                 keys_to_sign: keysToSign.iter().map(|k| k.innto()).collect(),
70                 endpoint_encryption_cert_chain: endpointEncryptionCertChain.to_vec(),
71                 challenge: challenge.to_vec(),
72             })?;
73         *deviceInfo = rsp.device_info.innto();
74         *protectedData = rsp.protected_data.innto();
75         Ok(rsp.ret)
76     }
generateCertificateRequestV2( &self, keysToSign: &[rkp::MacedPublicKey::MacedPublicKey], challenge: &[u8], ) -> binder::Result<Vec<u8>>77     fn generateCertificateRequestV2(
78         &self,
79         keysToSign: &[rkp::MacedPublicKey::MacedPublicKey],
80         challenge: &[u8],
81     ) -> binder::Result<Vec<u8>> {
82         let rsp: GenerateCertificateRequestV2Response =
83             self.execute(GenerateCertificateRequestV2Request {
84                 keys_to_sign: keysToSign.iter().map(|k| k.innto()).collect(),
85                 challenge: challenge.to_vec(),
86             })?;
87         Ok(rsp.ret)
88     }
89 }
90