xref: /aosp_15_r20/system/security/keystore2/src/remote_provisioning.rs (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker // Copyright 2020, The Android Open Source Project
2*e1997b9aSAndroid Build Coastguard Worker //
3*e1997b9aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*e1997b9aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*e1997b9aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*e1997b9aSAndroid Build Coastguard Worker //
7*e1997b9aSAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*e1997b9aSAndroid Build Coastguard Worker //
9*e1997b9aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*e1997b9aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*e1997b9aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e1997b9aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*e1997b9aSAndroid Build Coastguard Worker // limitations under the License.
14*e1997b9aSAndroid Build Coastguard Worker 
15*e1997b9aSAndroid Build Coastguard Worker //! This is the implementation for the remote provisioning AIDL interface between
16*e1997b9aSAndroid Build Coastguard Worker //! the network providers for remote provisioning and the system. This interface
17*e1997b9aSAndroid Build Coastguard Worker //! allows the caller to prompt the Remote Provisioning HAL to generate keys and
18*e1997b9aSAndroid Build Coastguard Worker //! CBOR blobs that can be ferried to a provisioning server that will return
19*e1997b9aSAndroid Build Coastguard Worker //! certificate chains signed by some root authority and stored in a keystore SQLite
20*e1997b9aSAndroid Build Coastguard Worker //! DB.
21*e1997b9aSAndroid Build Coastguard Worker 
22*e1997b9aSAndroid Build Coastguard Worker use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
23*e1997b9aSAndroid Build Coastguard Worker     Algorithm::Algorithm, AttestationKey::AttestationKey, KeyParameter::KeyParameter,
24*e1997b9aSAndroid Build Coastguard Worker     KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag,
25*e1997b9aSAndroid Build Coastguard Worker };
26*e1997b9aSAndroid Build Coastguard Worker use android_security_rkp_aidl::aidl::android::security::rkp::RemotelyProvisionedKey::RemotelyProvisionedKey;
27*e1997b9aSAndroid Build Coastguard Worker use android_system_keystore2::aidl::android::system::keystore2::{
28*e1997b9aSAndroid Build Coastguard Worker     Domain::Domain, KeyDescriptor::KeyDescriptor,
29*e1997b9aSAndroid Build Coastguard Worker };
30*e1997b9aSAndroid Build Coastguard Worker use anyhow::{Context, Result};
31*e1997b9aSAndroid Build Coastguard Worker use keystore2_crypto::parse_subject_from_certificate;
32*e1997b9aSAndroid Build Coastguard Worker 
33*e1997b9aSAndroid Build Coastguard Worker use crate::error::wrapped_rkpd_error_to_ks_error;
34*e1997b9aSAndroid Build Coastguard Worker use crate::globals::get_remotely_provisioned_component_name;
35*e1997b9aSAndroid Build Coastguard Worker use crate::ks_err;
36*e1997b9aSAndroid Build Coastguard Worker use crate::metrics_store::log_rkp_error_stats;
37*e1997b9aSAndroid Build Coastguard Worker use crate::watchdog_helper::watchdog as wd;
38*e1997b9aSAndroid Build Coastguard Worker use android_security_metrics::aidl::android::security::metrics::RkpError::RkpError as MetricsRkpError;
39*e1997b9aSAndroid Build Coastguard Worker 
40*e1997b9aSAndroid Build Coastguard Worker /// Contains helper functions to check if remote provisioning is enabled on the system and, if so,
41*e1997b9aSAndroid Build Coastguard Worker /// to assign and retrieve attestation keys and certificate chains.
42*e1997b9aSAndroid Build Coastguard Worker #[derive(Default)]
43*e1997b9aSAndroid Build Coastguard Worker pub struct RemProvState {
44*e1997b9aSAndroid Build Coastguard Worker     security_level: SecurityLevel,
45*e1997b9aSAndroid Build Coastguard Worker }
46*e1997b9aSAndroid Build Coastguard Worker 
47*e1997b9aSAndroid Build Coastguard Worker impl RemProvState {
48*e1997b9aSAndroid Build Coastguard Worker     /// Creates a RemProvState struct.
new(security_level: SecurityLevel) -> Self49*e1997b9aSAndroid Build Coastguard Worker     pub fn new(security_level: SecurityLevel) -> Self {
50*e1997b9aSAndroid Build Coastguard Worker         Self { security_level }
51*e1997b9aSAndroid Build Coastguard Worker     }
52*e1997b9aSAndroid Build Coastguard Worker 
is_rkp_only(&self) -> bool53*e1997b9aSAndroid Build Coastguard Worker     fn is_rkp_only(&self) -> bool {
54*e1997b9aSAndroid Build Coastguard Worker         let default_value = false;
55*e1997b9aSAndroid Build Coastguard Worker 
56*e1997b9aSAndroid Build Coastguard Worker         let property_name = match self.security_level {
57*e1997b9aSAndroid Build Coastguard Worker             SecurityLevel::STRONGBOX => "remote_provisioning.strongbox.rkp_only",
58*e1997b9aSAndroid Build Coastguard Worker             SecurityLevel::TRUSTED_ENVIRONMENT => "remote_provisioning.tee.rkp_only",
59*e1997b9aSAndroid Build Coastguard Worker             _ => return default_value,
60*e1997b9aSAndroid Build Coastguard Worker         };
61*e1997b9aSAndroid Build Coastguard Worker 
62*e1997b9aSAndroid Build Coastguard Worker         rustutils::system_properties::read_bool(property_name, default_value)
63*e1997b9aSAndroid Build Coastguard Worker             .unwrap_or(default_value)
64*e1997b9aSAndroid Build Coastguard Worker     }
65*e1997b9aSAndroid Build Coastguard Worker 
is_asymmetric_key(&self, params: &[KeyParameter]) -> bool66*e1997b9aSAndroid Build Coastguard Worker     fn is_asymmetric_key(&self, params: &[KeyParameter]) -> bool {
67*e1997b9aSAndroid Build Coastguard Worker         params.iter().any(|kp| {
68*e1997b9aSAndroid Build Coastguard Worker             matches!(
69*e1997b9aSAndroid Build Coastguard Worker                 kp,
70*e1997b9aSAndroid Build Coastguard Worker                 KeyParameter {
71*e1997b9aSAndroid Build Coastguard Worker                     tag: Tag::ALGORITHM,
72*e1997b9aSAndroid Build Coastguard Worker                     value: KeyParameterValue::Algorithm(Algorithm::RSA)
73*e1997b9aSAndroid Build Coastguard Worker                 } | KeyParameter {
74*e1997b9aSAndroid Build Coastguard Worker                     tag: Tag::ALGORITHM,
75*e1997b9aSAndroid Build Coastguard Worker                     value: KeyParameterValue::Algorithm(Algorithm::EC)
76*e1997b9aSAndroid Build Coastguard Worker                 }
77*e1997b9aSAndroid Build Coastguard Worker             )
78*e1997b9aSAndroid Build Coastguard Worker         })
79*e1997b9aSAndroid Build Coastguard Worker     }
80*e1997b9aSAndroid Build Coastguard Worker 
81*e1997b9aSAndroid Build Coastguard Worker     /// Fetches attestation key and corresponding certificates from RKPD.
get_rkpd_attestation_key_and_certs( &self, key: &KeyDescriptor, caller_uid: u32, params: &[KeyParameter], ) -> Result<Option<(AttestationKey, Vec<u8>)>>82*e1997b9aSAndroid Build Coastguard Worker     pub fn get_rkpd_attestation_key_and_certs(
83*e1997b9aSAndroid Build Coastguard Worker         &self,
84*e1997b9aSAndroid Build Coastguard Worker         key: &KeyDescriptor,
85*e1997b9aSAndroid Build Coastguard Worker         caller_uid: u32,
86*e1997b9aSAndroid Build Coastguard Worker         params: &[KeyParameter],
87*e1997b9aSAndroid Build Coastguard Worker     ) -> Result<Option<(AttestationKey, Vec<u8>)>> {
88*e1997b9aSAndroid Build Coastguard Worker         if !self.is_asymmetric_key(params) || key.domain != Domain::APP {
89*e1997b9aSAndroid Build Coastguard Worker             Ok(None)
90*e1997b9aSAndroid Build Coastguard Worker         } else {
91*e1997b9aSAndroid Build Coastguard Worker             match get_rkpd_attestation_key(&self.security_level, caller_uid) {
92*e1997b9aSAndroid Build Coastguard Worker                 Err(e) => {
93*e1997b9aSAndroid Build Coastguard Worker                     if self.is_rkp_only() {
94*e1997b9aSAndroid Build Coastguard Worker                         log::error!("Error occurred: {:?}", e);
95*e1997b9aSAndroid Build Coastguard Worker                         return Err(wrapped_rkpd_error_to_ks_error(&e)).context(format!("{e:?}"));
96*e1997b9aSAndroid Build Coastguard Worker                     }
97*e1997b9aSAndroid Build Coastguard Worker                     log::warn!("Error occurred: {:?}", e);
98*e1997b9aSAndroid Build Coastguard Worker                     log_rkp_error_stats(
99*e1997b9aSAndroid Build Coastguard Worker                         MetricsRkpError::FALL_BACK_DURING_HYBRID,
100*e1997b9aSAndroid Build Coastguard Worker                         &self.security_level,
101*e1997b9aSAndroid Build Coastguard Worker                     );
102*e1997b9aSAndroid Build Coastguard Worker                     Ok(None)
103*e1997b9aSAndroid Build Coastguard Worker                 }
104*e1997b9aSAndroid Build Coastguard Worker                 Ok(rkpd_key) => Ok(Some((
105*e1997b9aSAndroid Build Coastguard Worker                     AttestationKey {
106*e1997b9aSAndroid Build Coastguard Worker                         keyBlob: rkpd_key.keyBlob,
107*e1997b9aSAndroid Build Coastguard Worker                         attestKeyParams: vec![],
108*e1997b9aSAndroid Build Coastguard Worker                         // Batch certificate is at the beginning of the concatenated certificate
109*e1997b9aSAndroid Build Coastguard Worker                         // chain, and the helper function only looks at the first cert.
110*e1997b9aSAndroid Build Coastguard Worker                         issuerSubjectName: parse_subject_from_certificate(
111*e1997b9aSAndroid Build Coastguard Worker                             &rkpd_key.encodedCertChain,
112*e1997b9aSAndroid Build Coastguard Worker                         )
113*e1997b9aSAndroid Build Coastguard Worker                         .context(ks_err!("Failed to parse subject."))?,
114*e1997b9aSAndroid Build Coastguard Worker                     },
115*e1997b9aSAndroid Build Coastguard Worker                     rkpd_key.encodedCertChain,
116*e1997b9aSAndroid Build Coastguard Worker                 ))),
117*e1997b9aSAndroid Build Coastguard Worker             }
118*e1997b9aSAndroid Build Coastguard Worker         }
119*e1997b9aSAndroid Build Coastguard Worker     }
120*e1997b9aSAndroid Build Coastguard Worker }
121*e1997b9aSAndroid Build Coastguard Worker 
get_rkpd_attestation_key( security_level: &SecurityLevel, caller_uid: u32, ) -> Result<RemotelyProvisionedKey>122*e1997b9aSAndroid Build Coastguard Worker fn get_rkpd_attestation_key(
123*e1997b9aSAndroid Build Coastguard Worker     security_level: &SecurityLevel,
124*e1997b9aSAndroid Build Coastguard Worker     caller_uid: u32,
125*e1997b9aSAndroid Build Coastguard Worker ) -> Result<RemotelyProvisionedKey> {
126*e1997b9aSAndroid Build Coastguard Worker     // Depending on the Android release, RKP may not have been mandatory for the
127*e1997b9aSAndroid Build Coastguard Worker     // TEE or StrongBox KM instances. In such cases, lookup failure for the IRPC
128*e1997b9aSAndroid Build Coastguard Worker     // HAL service is WAI and should not cause a failure. The error should be caught
129*e1997b9aSAndroid Build Coastguard Worker     // by the calling function and allow for natural fallback to the factory key.
130*e1997b9aSAndroid Build Coastguard Worker     let rpc_name = get_remotely_provisioned_component_name(security_level)
131*e1997b9aSAndroid Build Coastguard Worker         .context(ks_err!("Trying to get IRPC name."))?;
132*e1997b9aSAndroid Build Coastguard Worker     let _wd = wd::watch("Calling get_rkpd_attestation_key()");
133*e1997b9aSAndroid Build Coastguard Worker     rkpd_client::get_rkpd_attestation_key(&rpc_name, caller_uid)
134*e1997b9aSAndroid Build Coastguard Worker }
135