blob: 06f8ad4d375c0145f840d197366361b5c467c785 [file] [log] [blame]
Alice Wang15f6d082023-08-25 09:11:07 +00001// 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 Wangf3482602023-09-08 11:51:29 +000017use crate::rkpvm;
Alice Wang15f6d082023-08-25 09:11:07 +000018use android_hardware_security_rkp::aidl::android::hardware::security::keymint::{
19 DeviceInfo::DeviceInfo,
20 IRemotelyProvisionedComponent::{
Alice Wangf3482602023-09-08 11:51:29 +000021 BnRemotelyProvisionedComponent, IRemotelyProvisionedComponent, STATUS_FAILED,
22 STATUS_REMOVED,
Alice Wang15f6d082023-08-25 09:11:07 +000023 },
24 MacedPublicKey::MacedPublicKey,
25 ProtectedData::ProtectedData,
26 RpcHardwareInfo::{RpcHardwareInfo, CURVE_NONE, MIN_SUPPORTED_NUM_KEYS_IN_CSR},
27};
Alice Wangf3482602023-09-08 11:51:29 +000028use anyhow::Context;
Alice Wang15f6d082023-08-25 09:11:07 +000029use avflog::LogResult;
Alice Wangf3482602023-09-08 11:51:29 +000030use binder::{BinderFeatures, Interface, IntoBinderResult, Result as BinderResult, Status, Strong};
Alice Wang15f6d082023-08-25 09:11:07 +000031
32/// Constructs a binder object that implements `IRemotelyProvisionedComponent`.
33pub(crate) fn new_binder() -> Strong<dyn IRemotelyProvisionedComponent> {
34 BnRemotelyProvisionedComponent::new_binder(
35 AvfRemotelyProvisionedComponent {},
36 BinderFeatures::default(),
37 )
38}
39
40struct AvfRemotelyProvisionedComponent {}
41
42impl Interface for AvfRemotelyProvisionedComponent {}
43
44#[allow(non_snake_case)]
45impl 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 Wanga723fe62023-09-06 12:38:59 +000058 testMode: bool,
Alice Wangf3482602023-09-08 11:51:29 +000059 macedPublicKey: &mut MacedPublicKey,
Alice Wang15f6d082023-08-25 09:11:07 +000060 ) -> BinderResult<Vec<u8>> {
Alice Wanga723fe62023-09-06 12:38:59 +000061 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 Wangf3482602023-09-08 11:51:29 +000068 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 Wang15f6d082023-08-25 09:11:07 +000074 }
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 Wangf3482602023-09-08 11:51:29 +000094 keysToSign: &[MacedPublicKey],
95 challenge: &[u8],
Alice Wang15f6d082023-08-25 09:11:07 +000096 ) -> BinderResult<Vec<u8>> {
Alice Wangf3482602023-09-08 11:51:29 +000097 // 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 Wang15f6d082023-08-25 09:11:07 +0000102 }
103}