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 //! RemotelyProvisionedComponent HAL device implementation. 16 17 use super::{ChannelHalService, SerializedChannel}; 18 use crate::binder; 19 use crate::hal::{rkp, Innto}; 20 use kmr_wire::*; 21 use std::sync::{Arc, Mutex, MutexGuard}; 22 23 /// `IRemotelyProvisionedComponent` implementation which converts all method invocations to 24 /// serialized requests that are sent down the associated channel. 25 pub struct Device<T: SerializedChannel + 'static> { 26 channel: Arc<Mutex<T>>, 27 } 28 29 impl<T: SerializedChannel + 'static> Device<T> { 30 /// Construct a new instance that uses the provided channel. new(channel: Arc<Mutex<T>>) -> Self31 pub fn new(channel: Arc<Mutex<T>>) -> Self { 32 Self { channel } 33 } 34 35 /// Create a new instance wrapped in a proxy object. new_as_binder( channel: Arc<Mutex<T>>, ) -> binder::Strong<dyn rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent>36 pub fn new_as_binder( 37 channel: Arc<Mutex<T>>, 38 ) -> binder::Strong<dyn rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent> { 39 rkp::IRemotelyProvisionedComponent::BnRemotelyProvisionedComponent::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> binder::Interface for Device<T> {} 53 54 impl<T: SerializedChannel> rkp::IRemotelyProvisionedComponent::IRemotelyProvisionedComponent 55 for Device<T> 56 { getHardwareInfo(&self) -> binder::Result<rkp::RpcHardwareInfo::RpcHardwareInfo>57 fn getHardwareInfo(&self) -> binder::Result<rkp::RpcHardwareInfo::RpcHardwareInfo> { 58 let rsp: GetRpcHardwareInfoResponse = self.execute(GetRpcHardwareInfoRequest {})?; 59 Ok(rsp.ret.innto()) 60 } generateEcdsaP256KeyPair( &self, testMode: bool, macedPublicKey: &mut rkp::MacedPublicKey::MacedPublicKey, ) -> binder::Result<Vec<u8>>61 fn generateEcdsaP256KeyPair( 62 &self, 63 testMode: bool, 64 macedPublicKey: &mut rkp::MacedPublicKey::MacedPublicKey, 65 ) -> binder::Result<Vec<u8>> { 66 let rsp: GenerateEcdsaP256KeyPairResponse = 67 self.execute(GenerateEcdsaP256KeyPairRequest { test_mode: testMode })?; 68 *macedPublicKey = rsp.maced_public_key.innto(); 69 Ok(rsp.ret) 70 } 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>>71 fn generateCertificateRequest( 72 &self, 73 testMode: bool, 74 keysToSign: &[rkp::MacedPublicKey::MacedPublicKey], 75 endpointEncryptionCertChain: &[u8], 76 challenge: &[u8], 77 deviceInfo: &mut rkp::DeviceInfo::DeviceInfo, 78 protectedData: &mut rkp::ProtectedData::ProtectedData, 79 ) -> binder::Result<Vec<u8>> { 80 let rsp: GenerateCertificateRequestResponse = 81 self.execute(GenerateCertificateRequestRequest { 82 test_mode: testMode, 83 keys_to_sign: keysToSign.iter().map(|k| k.innto()).collect(), 84 endpoint_encryption_cert_chain: endpointEncryptionCertChain.to_vec(), 85 challenge: challenge.to_vec(), 86 })?; 87 *deviceInfo = rsp.device_info.innto(); 88 *protectedData = rsp.protected_data.innto(); 89 Ok(rsp.ret) 90 } generateCertificateRequestV2( &self, keysToSign: &[rkp::MacedPublicKey::MacedPublicKey], challenge: &[u8], ) -> binder::Result<Vec<u8>>91 fn generateCertificateRequestV2( 92 &self, 93 keysToSign: &[rkp::MacedPublicKey::MacedPublicKey], 94 challenge: &[u8], 95 ) -> binder::Result<Vec<u8>> { 96 let rsp: GenerateCertificateRequestV2Response = 97 self.execute(GenerateCertificateRequestV2Request { 98 keys_to_sign: keysToSign.iter().map(|k| k.innto()).collect(), 99 challenge: challenge.to_vec(), 100 })?; 101 Ok(rsp.ret) 102 } 103 } 104