blob: 2bdafd4738da1e4d4a12e72a94b938ebace7e83a [file] [log] [blame]
Max Bires148c08e2020-10-13 13:41:41 -07001// Copyright 2020, 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//! This is the implementation for the remote provisioning AIDL interface between
16//! the network providers for remote provisioning and the system. This interface
17//! allows the caller to prompt the Remote Provisioning HAL to generate keys and
18//! CBOR blobs that can be ferried to a provisioning server that will return
19//! certificate chains signed by some root authority and stored in a keystore SQLite
20//! DB.
21
Max Biresb2e1d032021-02-08 21:35:05 -080022use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
David Drysdale76d95ba2024-09-25 14:00:10 +010023 Algorithm::Algorithm, AttestationKey::AttestationKey, KeyParameter::KeyParameter,
24 KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag,
Max Biresb2e1d032021-02-08 21:35:05 -080025};
Alice Wangb03ed832023-11-22 16:35:54 +000026use android_security_rkp_aidl::aidl::android::security::rkp::RemotelyProvisionedKey::RemotelyProvisionedKey;
Max Bires97f96812021-02-23 23:44:57 -080027use android_system_keystore2::aidl::android::system::keystore2::{
Tri Voa1634bb2022-12-01 15:54:19 -080028 Domain::Domain, KeyDescriptor::KeyDescriptor,
Max Bires97f96812021-02-23 23:44:57 -080029};
Max Biresb2e1d032021-02-08 21:35:05 -080030use anyhow::{Context, Result};
Max Bires97f96812021-02-23 23:44:57 -080031use keystore2_crypto::parse_subject_from_certificate;
Max Bires148c08e2020-10-13 13:41:41 -070032
Alice Wang849cfe42023-11-10 12:43:36 +000033use crate::error::wrapped_rkpd_error_to_ks_error;
Alice Wangbf6a6932023-11-07 11:47:12 +000034use crate::globals::get_remotely_provisioned_component_name;
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +000035use crate::ks_err;
Hasini Gunasinghe8a1a2242021-08-02 22:28:39 +000036use crate::metrics_store::log_rkp_error_stats;
Alice Wang4277d2e2023-11-08 09:15:54 +000037use crate::watchdog_helper::watchdog as wd;
Hasini Gunasinghe8a1a2242021-08-02 22:28:39 +000038use android_security_metrics::aidl::android::security::metrics::RkpError::RkpError as MetricsRkpError;
Max Bires148c08e2020-10-13 13:41:41 -070039
Max Bires97f96812021-02-23 23:44:57 -080040/// Contains helper functions to check if remote provisioning is enabled on the system and, if so,
41/// to assign and retrieve attestation keys and certificate chains.
42#[derive(Default)]
43pub struct RemProvState {
44 security_level: SecurityLevel,
Max Bires97f96812021-02-23 23:44:57 -080045}
46
47impl RemProvState {
48 /// Creates a RemProvState struct.
David Drysdale8c4c4f32023-10-31 12:14:11 +000049 pub fn new(security_level: SecurityLevel) -> Self {
50 Self { security_level }
Max Bires55620ff2022-02-11 13:34:15 -080051 }
52
Seth Mooredfdcb872022-04-20 14:33:19 -070053 fn is_rkp_only(&self) -> bool {
Max Bires65207b52022-03-29 23:58:08 -070054 let default_value = false;
55
Seth Mooredfdcb872022-04-20 14:33:19 -070056 let property_name = match self.security_level {
57 SecurityLevel::STRONGBOX => "remote_provisioning.strongbox.rkp_only",
58 SecurityLevel::TRUSTED_ENVIRONMENT => "remote_provisioning.tee.rkp_only",
Max Bires65207b52022-03-29 23:58:08 -070059 _ => return default_value,
60 };
61
62 rustutils::system_properties::read_bool(property_name, default_value)
63 .unwrap_or(default_value)
64 }
65
Max Bires97f96812021-02-23 23:44:57 -080066 fn is_asymmetric_key(&self, params: &[KeyParameter]) -> bool {
67 params.iter().any(|kp| {
68 matches!(
69 kp,
70 KeyParameter {
71 tag: Tag::ALGORITHM,
72 value: KeyParameterValue::Algorithm(Algorithm::RSA)
73 } | KeyParameter {
74 tag: Tag::ALGORITHM,
75 value: KeyParameterValue::Algorithm(Algorithm::EC)
76 }
77 )
78 })
79 }
80
Tri Vob5e43d12022-12-21 08:54:14 -080081 /// Fetches attestation key and corresponding certificates from RKPD.
82 pub fn get_rkpd_attestation_key_and_certs(
83 &self,
84 key: &KeyDescriptor,
85 caller_uid: u32,
86 params: &[KeyParameter],
David Drysdale76d95ba2024-09-25 14:00:10 +010087 ) -> Result<Option<(AttestationKey, Vec<u8>)>> {
Tri Vob5e43d12022-12-21 08:54:14 -080088 if !self.is_asymmetric_key(params) || key.domain != Domain::APP {
89 Ok(None)
90 } else {
Alice Wangb03ed832023-11-22 16:35:54 +000091 match get_rkpd_attestation_key(&self.security_level, caller_uid) {
Tri Vob5e43d12022-12-21 08:54:14 -080092 Err(e) => {
93 if self.is_rkp_only() {
94 log::error!("Error occurred: {:?}", e);
Alice Wang849cfe42023-11-10 12:43:36 +000095 return Err(wrapped_rkpd_error_to_ks_error(&e)).context(format!("{e:?}"));
Tri Vob5e43d12022-12-21 08:54:14 -080096 }
97 log::warn!("Error occurred: {:?}", e);
98 log_rkp_error_stats(
99 MetricsRkpError::FALL_BACK_DURING_HYBRID,
100 &self.security_level,
101 );
102 Ok(None)
103 }
104 Ok(rkpd_key) => Ok(Some((
105 AttestationKey {
106 keyBlob: rkpd_key.keyBlob,
107 attestKeyParams: vec![],
David Drysdale76d95ba2024-09-25 14:00:10 +0100108 // Batch certificate is at the beginning of the concatenated certificate
109 // chain, and the helper function only looks at the first cert.
Tri Vob5e43d12022-12-21 08:54:14 -0800110 issuerSubjectName: parse_subject_from_certificate(
111 &rkpd_key.encodedCertChain,
112 )
113 .context(ks_err!("Failed to parse subject."))?,
114 },
David Drysdale76d95ba2024-09-25 14:00:10 +0100115 rkpd_key.encodedCertChain,
Tri Vob5e43d12022-12-21 08:54:14 -0800116 ))),
117 }
118 }
119 }
Max Bires97f96812021-02-23 23:44:57 -0800120}
Alice Wangb03ed832023-11-22 16:35:54 +0000121
122fn get_rkpd_attestation_key(
123 security_level: &SecurityLevel,
124 caller_uid: u32,
125) -> Result<RemotelyProvisionedKey> {
Alice Wangf7148402023-11-24 08:36:51 +0000126 // Depending on the Android release, RKP may not have been mandatory for the
127 // TEE or StrongBox KM instances. In such cases, lookup failure for the IRPC
128 // HAL service is WAI and should not cause a failure. The error should be caught
129 // by the calling function and allow for natural fallback to the factory key.
Alice Wangb03ed832023-11-22 16:35:54 +0000130 let rpc_name = get_remotely_provisioned_component_name(security_level)
131 .context(ks_err!("Trying to get IRPC name."))?;
David Drysdale541846b2024-05-23 13:16:07 +0100132 let _wd = wd::watch("Calling get_rkpd_attestation_key()");
Alice Wangb03ed832023-11-22 16:35:54 +0000133 rkpd_client::get_rkpd_attestation_key(&rpc_name, caller_uid)
134}