Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! IRemotelyProvisionedComponent HAL implementation. |
| 16 | |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 17 | use crate::rkpvm; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 18 | use android_hardware_security_rkp::aidl::android::hardware::security::keymint::{ |
| 19 | DeviceInfo::DeviceInfo, |
| 20 | IRemotelyProvisionedComponent::{ |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 21 | BnRemotelyProvisionedComponent, IRemotelyProvisionedComponent, STATUS_FAILED, |
| 22 | STATUS_REMOVED, |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 23 | }, |
| 24 | MacedPublicKey::MacedPublicKey, |
| 25 | ProtectedData::ProtectedData, |
| 26 | RpcHardwareInfo::{RpcHardwareInfo, CURVE_NONE, MIN_SUPPORTED_NUM_KEYS_IN_CSR}, |
| 27 | }; |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 28 | use anyhow::Context; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 29 | use avflog::LogResult; |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 30 | use binder::{BinderFeatures, Interface, IntoBinderResult, Result as BinderResult, Status, Strong}; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 31 | |
| 32 | /// Constructs a binder object that implements `IRemotelyProvisionedComponent`. |
| 33 | pub(crate) fn new_binder() -> Strong<dyn IRemotelyProvisionedComponent> { |
| 34 | BnRemotelyProvisionedComponent::new_binder( |
| 35 | AvfRemotelyProvisionedComponent {}, |
| 36 | BinderFeatures::default(), |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | struct AvfRemotelyProvisionedComponent {} |
| 41 | |
| 42 | impl Interface for AvfRemotelyProvisionedComponent {} |
| 43 | |
| 44 | #[allow(non_snake_case)] |
| 45 | impl IRemotelyProvisionedComponent for AvfRemotelyProvisionedComponent { |
| 46 | fn getHardwareInfo(&self) -> BinderResult<RpcHardwareInfo> { |
| 47 | Ok(RpcHardwareInfo { |
| 48 | versionNumber: 3, |
| 49 | rpcAuthorName: String::from("Android Virtualization Framework"), |
| 50 | supportedEekCurve: CURVE_NONE, |
| 51 | uniqueId: Some(String::from("Android Virtualization Framework 1")), |
| 52 | supportedNumKeysInCsr: MIN_SUPPORTED_NUM_KEYS_IN_CSR, |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | fn generateEcdsaP256KeyPair( |
| 57 | &self, |
Alice Wang | a723fe6 | 2023-09-06 12:38:59 +0000 | [diff] [blame] | 58 | testMode: bool, |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 59 | macedPublicKey: &mut MacedPublicKey, |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 60 | ) -> BinderResult<Vec<u8>> { |
Alice Wang | a723fe6 | 2023-09-06 12:38:59 +0000 | [diff] [blame] | 61 | if testMode { |
| 62 | return Err(Status::new_service_specific_error_str( |
| 63 | STATUS_REMOVED, |
| 64 | Some("generateEcdsaP256KeyPair does not support test mode in IRPC v3+ HAL."), |
| 65 | )) |
| 66 | .with_log(); |
| 67 | } |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 68 | let key_pair = rkpvm::generate_ecdsa_p256_key_pair() |
| 69 | .context("Failed to generate ECDSA P-256 key pair") |
| 70 | .with_log() |
| 71 | .or_service_specific_exception(STATUS_FAILED)?; |
| 72 | macedPublicKey.macedKey = key_pair.maced_public_key; |
| 73 | Ok(key_pair.key_blob) |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | fn generateCertificateRequest( |
| 77 | &self, |
| 78 | _testMode: bool, |
| 79 | _keysToSign: &[MacedPublicKey], |
| 80 | _endpointEncryptionCertChain: &[u8], |
| 81 | _challenge: &[u8], |
| 82 | _deviceInfo: &mut DeviceInfo, |
| 83 | _protectedData: &mut ProtectedData, |
| 84 | ) -> BinderResult<Vec<u8>> { |
| 85 | Err(Status::new_service_specific_error_str( |
| 86 | STATUS_REMOVED, |
| 87 | Some("This method was deprecated in v3 of the interface."), |
| 88 | )) |
| 89 | .with_log() |
| 90 | } |
| 91 | |
| 92 | fn generateCertificateRequestV2( |
| 93 | &self, |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 94 | keysToSign: &[MacedPublicKey], |
| 95 | challenge: &[u8], |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 96 | ) -> BinderResult<Vec<u8>> { |
Alice Wang | f348260 | 2023-09-08 11:51:29 +0000 | [diff] [blame^] | 97 | // TODO(b/299259624): Validate the MAC of the keys to certify. |
| 98 | rkpvm::generate_certificate_request(keysToSign, challenge) |
| 99 | .context("Failed to generate certificate request") |
| 100 | .with_log() |
| 101 | .or_service_specific_exception(STATUS_FAILED) |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 102 | } |
| 103 | } |